1/* 2 * super.c 3 * 4 * Copyright (C) 1995-1997, 1999 Martin von L�wis 5 * Copyright (C) 1996-1997 R�gis Duchesne 6 * Copyright (C) 1999 Steve Dodd 7 * Copyright (C) 2000-2001 Anton Altparmakov (AIA) 8 */ 9 10#include <linux/ntfs_fs.h> 11#include <linux/errno.h> 12#include <linux/bitops.h> 13#include <linux/module.h> 14#include "ntfstypes.h" 15#include "struct.h" 16#include "super.h" 17#include "macros.h" 18#include "inode.h" 19#include "support.h" 20#include "util.h" 21#include <linux/smp_lock.h> 22 23/* All important structures in NTFS use 2 consistency checks: 24 * . a magic structure identifier (FILE, INDX, RSTR, RCRD...) 25 * . a fixup technique : the last word of each sector (called a fixup) of a 26 * structure's record should end with the word at offset <n> of the first 27 * sector, and if it is the case, must be replaced with the words following 28 * <n>. The value of <n> and the number of fixups is taken from the fields 29 * at the offsets 4 and 6. Note that the sector size is defined as 30 * NTFS_SECTOR_SIZE and not as the hardware sector size (this is concordant 31 * with what the Windows NTFS driver does). 32 * 33 * This function performs these 2 checks, and _fails_ if: 34 * . the input size is invalid 35 * . the fixup header is invalid 36 * . the size does not match the number of sectors 37 * . the magic identifier is wrong 38 * . a fixup is invalid 39 */ 40int ntfs_fixup_record(char *record, char *magic, int size) 41{ 42 int start, count, offset; 43 ntfs_u16 fixup; 44 45 if (!IS_MAGIC(record, magic)) 46 return 0; 47 start = NTFS_GETU16(record + 4); 48 count = NTFS_GETU16(record + 6) - 1; 49 if (size & (NTFS_SECTOR_SIZE - 1) || start & 1 || 50 start + count * 2 > size || size >> 9 != count) { 51 if (size <= 0) 52 printk(KERN_ERR "NTFS: BUG: ntfs_fixup_record() got " 53 "zero size! Please report this to " 54 "linux-ntfs-dev@lists.sf.net\n"); 55 return 0; 56 } 57 fixup = NTFS_GETU16(record + start); 58 start += 2; 59 offset = NTFS_SECTOR_SIZE - 2; 60 while (count--) { 61 if (NTFS_GETU16(record + offset) != fixup) 62 return 0; 63 NTFS_PUTU16(record + offset, NTFS_GETU16(record + start)); 64 start += 2; 65 offset += NTFS_SECTOR_SIZE; 66 } 67 return 1; 68} 69 70/* 71 * Get vital informations about the ntfs partition from the boot sector. 72 * Return 0 on success or -1 on error. 73 */ 74int ntfs_init_volume(ntfs_volume *vol, char *boot) 75{ 76 int sectors_per_cluster_bits; 77 __s64 ll; 78 ntfs_cluster_t mft_zone_size, tc; 79 80 /* System defined default values, in case we don't load $AttrDef. */ 81 vol->at_standard_information = 0x10; 82 vol->at_attribute_list = 0x20; 83 vol->at_file_name = 0x30; 84 vol->at_volume_version = 0x40; 85 vol->at_security_descriptor = 0x50; 86 vol->at_volume_name = 0x60; 87 vol->at_volume_information = 0x70; 88 vol->at_data = 0x80; 89 vol->at_index_root = 0x90; 90 vol->at_index_allocation = 0xA0; 91 vol->at_bitmap = 0xB0; 92 vol->at_symlink = 0xC0; 93 /* Sector size. */ 94 vol->sector_size = NTFS_GETU16(boot + 0xB); 95 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->sector_size = 0x%x\n", 96 vol->sector_size); 97 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: sectors_per_cluster = " 98 "0x%x\n", NTFS_GETU8(boot + 0xD)); 99 sectors_per_cluster_bits = ffs(NTFS_GETU8(boot + 0xD)) - 1; 100 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: sectors_per_cluster_bits " 101 "= 0x%x\n", sectors_per_cluster_bits); 102 vol->mft_clusters_per_record = NTFS_GETS8(boot + 0x40); 103 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_clusters_per_record" 104 " = 0x%x\n", vol->mft_clusters_per_record); 105 vol->index_clusters_per_record = NTFS_GETS8(boot + 0x44); 106 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: " 107 "vol->index_clusters_per_record = 0x%x\n", 108 vol->index_clusters_per_record); 109 vol->cluster_size = vol->sector_size << sectors_per_cluster_bits; 110 vol->cluster_size_bits = ffs(vol->cluster_size) - 1; 111 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->cluster_size = 0x%x\n", 112 vol->cluster_size); 113 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->cluster_size_bits = " 114 "0x%x\n", vol->cluster_size_bits); 115 if (vol->mft_clusters_per_record > 0) 116 vol->mft_record_size = vol->cluster_size << 117 (ffs(vol->mft_clusters_per_record) - 1); 118 else 119 /* 120 * When mft_record_size < cluster_size, mft_clusters_per_record 121 * = -log2(mft_record_size) bytes. mft_record_size normaly is 122 * 1024 bytes, which is encoded as 0xF6 (-10 in decimal). 123 */ 124 vol->mft_record_size = 1 << -vol->mft_clusters_per_record; 125 vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1; 126 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_record_size = 0x%x" 127 "\n", vol->mft_record_size); 128 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_record_size_bits = " 129 "0x%x\n", vol->mft_record_size_bits); 130 if (vol->index_clusters_per_record > 0) 131 vol->index_record_size = vol->cluster_size << 132 (ffs(vol->index_clusters_per_record) - 1); 133 else 134 /* 135 * When index_record_size < cluster_size, 136 * index_clusters_per_record = -log2(index_record_size) bytes. 137 * index_record_size normaly equals 4096 bytes, which is 138 * encoded as 0xF4 (-12 in decimal). 139 */ 140 vol->index_record_size = 1 << -vol->index_clusters_per_record; 141 vol->index_record_size_bits = ffs(vol->index_record_size) - 1; 142 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->index_record_size = " 143 "0x%x\n", vol->index_record_size); 144 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->index_record_size_bits " 145 "= 0x%x\n", vol->index_record_size_bits); 146 /* 147 * Get the size of the volume in clusters (ofs 0x28 is nr_sectors) and 148 * check for 64-bit-ness. Windows currently only uses 32 bits to save 149 * the clusters so we do the same as it is much faster on 32-bit CPUs. 150 */ 151 ll = NTFS_GETS64(boot + 0x28) >> sectors_per_cluster_bits; 152 if (ll >= (__s64)1 << 31) { 153 ntfs_error("Cannot handle 64-bit clusters. Please inform " 154 "linux-ntfs-dev@lists.sf.net that you got this " 155 "error.\n"); 156 return -1; 157 } 158 vol->nr_clusters = (ntfs_cluster_t)ll; 159 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->nr_clusters = 0x%x\n", 160 vol->nr_clusters); 161 vol->mft_lcn = (ntfs_cluster_t)NTFS_GETS64(boot + 0x30); 162 vol->mft_mirr_lcn = (ntfs_cluster_t)NTFS_GETS64(boot + 0x38); 163 /* Determine MFT zone size. */ 164 mft_zone_size = vol->nr_clusters; 165 switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */ 166 case 4: 167 mft_zone_size >>= 1; /* 50% */ 168 break; 169 case 3: 170 mft_zone_size = mft_zone_size * 3 >> 3; /* 37.5% */ 171 break; 172 case 2: 173 mft_zone_size >>= 2; /* 25% */ 174 break; 175 /* case 1: */ 176 default: 177 mft_zone_size >>= 3; /* 12.5% */ 178 break; 179 } 180 /* Setup mft zone. */ 181 vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn; 182 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_zone_pos = %x\n", 183 vol->mft_zone_pos); 184 /* 185 * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs 186 * source) and if the actual mft_lcn is in the expected place or even 187 * further to the front of the volume, extend the mft_zone to cover the 188 * beginning of the volume as well. This is in order to protect the 189 * area reserved for the mft bitmap as well within the mft_zone itself. 190 * On non-standard volumes we don't protect it as well as the overhead 191 * would be higher than the speed increase we would get by doing it. 192 */ 193 tc = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size; 194 if (tc * vol->cluster_size < 16 * 1024) 195 tc = (16 * 1024 + vol->cluster_size - 1) / vol->cluster_size; 196 if (vol->mft_zone_start <= tc) 197 vol->mft_zone_start = (ntfs_cluster_t)0; 198 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_zone_start = %x\n", 199 vol->mft_zone_start); 200 /* 201 * Need to cap the mft zone on non-standard volumes so that it does 202 * not point outside the boundaries of the volume, we do this by 203 * halving the zone size until we are inside the volume. 204 */ 205 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 206 while (vol->mft_zone_end >= vol->nr_clusters) { 207 mft_zone_size >>= 1; 208 vol->mft_zone_end = vol->mft_lcn + mft_zone_size; 209 } 210 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->mft_zone_end = %x\n", 211 vol->mft_zone_end); 212 /* 213 * Set the current position within each data zone to the start of the 214 * respective zone. 215 */ 216 vol->data1_zone_pos = vol->mft_zone_end; 217 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->data1_zone_pos = %x\n", 218 vol->data1_zone_pos); 219 vol->data2_zone_pos = (ntfs_cluster_t)0; 220 ntfs_debug(DEBUG_FILE3, "ntfs_init_volume: vol->data2_zone_pos = %x\n", 221 vol->data2_zone_pos); 222 /* Set the mft data allocation position to mft record 24. */ 223 vol->mft_data_pos = 24UL; 224 /* This will be initialized later. */ 225 vol->upcase = 0; 226 vol->upcase_length = 0; 227 vol->mft_ino = 0; 228 return 0; 229} 230 231static void ntfs_init_upcase(ntfs_inode *upcase) 232{ 233 ntfs_io io; 234#define UPCASE_LENGTH 256 235 upcase->vol->upcase = ntfs_malloc(UPCASE_LENGTH << 1); 236 if (!upcase->vol->upcase) 237 return; 238 io.fn_put = ntfs_put; 239 io.fn_get = 0; 240 io.param = (char*)upcase->vol->upcase; 241 io.size = UPCASE_LENGTH << 1; 242 ntfs_read_attr(upcase, upcase->vol->at_data, 0, 0, &io); 243 upcase->vol->upcase_length = io.size >> 1; 244} 245 246static int process_attrdef(ntfs_inode* attrdef, ntfs_u8* def) 247{ 248 int type = NTFS_GETU32(def+0x80); 249 int check_type = 0; 250 ntfs_volume *vol = attrdef->vol; 251 ntfs_u16* name = (ntfs_u16*)def; 252 253 if (!type) { 254 ntfs_debug(DEBUG_OTHER, "process_atrdef: finished processing " 255 "and returning 1\n"); 256 return 1; 257 } 258 if (ntfs_ua_strncmp(name, "$STANDARD_INFORMATION", 64) == 0) { 259 vol->at_standard_information = type; 260 check_type = 0x10; 261 } else if (ntfs_ua_strncmp(name, "$ATTRIBUTE_LIST", 64) == 0) { 262 vol->at_attribute_list = type; 263 check_type = 0x20; 264 } else if (ntfs_ua_strncmp(name, "$FILE_NAME", 64) == 0) { 265 vol->at_file_name = type; 266 check_type = 0x30; 267 } else if (ntfs_ua_strncmp(name, "$VOLUME_VERSION", 64) == 0) { 268 vol->at_volume_version = type; 269 check_type = 0x40; 270 } else if (ntfs_ua_strncmp(name, "$SECURITY_DESCRIPTOR", 64) == 0) { 271 vol->at_security_descriptor = type; 272 check_type = 0x50; 273 } else if (ntfs_ua_strncmp(name, "$VOLUME_NAME", 64) == 0) { 274 vol->at_volume_name = type; 275 check_type = 0x60; 276 } else if (ntfs_ua_strncmp(name, "$VOLUME_INFORMATION", 64) == 0) { 277 vol->at_volume_information = type; 278 check_type = 0x70; 279 } else if (ntfs_ua_strncmp(name, "$DATA", 64) == 0) { 280 vol->at_data = type; 281 check_type = 0x80; 282 } else if (ntfs_ua_strncmp(name, "$INDEX_ROOT", 64) == 0) { 283 vol->at_index_root = type; 284 check_type = 0x90; 285 } else if (ntfs_ua_strncmp(name, "$INDEX_ALLOCATION", 64) == 0) { 286 vol->at_index_allocation = type; 287 check_type = 0xA0; 288 } else if (ntfs_ua_strncmp(name, "$BITMAP", 64) == 0) { 289 vol->at_bitmap = type; 290 check_type = 0xB0; 291 } else if (ntfs_ua_strncmp(name, "$SYMBOLIC_LINK", 64) == 0 || 292 ntfs_ua_strncmp(name, "$REPARSE_POINT", 64) == 0) { 293 vol->at_symlink = type; 294 check_type = 0xC0; 295 } 296 if (check_type && check_type != type) { 297 ntfs_error("process_attrdef: unexpected type 0x%x for 0x%x\n", 298 type, check_type); 299 return -EINVAL; 300 } 301 ntfs_debug(DEBUG_OTHER, "process_attrdef: found %s attribute of type " 302 "0x%x\n", check_type ? "known" : "unknown", type); 303 return 0; 304} 305 306int ntfs_init_attrdef(ntfs_inode* attrdef) 307{ 308 ntfs_u8 *buf; 309 ntfs_io io; 310 __s64 offset; 311 unsigned i; 312 int error; 313 ntfs_attribute *data; 314 315 ntfs_debug(DEBUG_BSD, "Entered ntfs_init_attrdef()\n"); 316 buf = ntfs_malloc(4050); /* 90*45 */ 317 if (!buf) 318 return -ENOMEM; 319 io.fn_put = ntfs_put; 320 io.fn_get = ntfs_get; 321 io.do_read = 1; 322 offset = 0; 323 data = ntfs_find_attr(attrdef, attrdef->vol->at_data, 0); 324 ntfs_debug(DEBUG_BSD, "In ntfs_init_attrdef() after call to " 325 "ntfs_find_attr.\n"); 326 if (!data) { 327 ntfs_free(buf); 328 return -EINVAL; 329 } 330 do { 331 io.param = buf; 332 io.size = 4050; 333 ntfs_debug(DEBUG_BSD, "In ntfs_init_attrdef() going to call " 334 "ntfs_readwrite_attr.\n"); 335 error = ntfs_readwrite_attr(attrdef, data, offset, &io); 336 ntfs_debug(DEBUG_BSD, "In ntfs_init_attrdef() after call to " 337 "ntfs_readwrite_attr.\n"); 338 for (i = 0; !error && i <= io.size - 0xA0; i += 0xA0) { 339 ntfs_debug(DEBUG_BSD, "In ntfs_init_attrdef() going " 340 "to call process_attrdef.\n"); 341 error = process_attrdef(attrdef, buf + i); 342 ntfs_debug(DEBUG_BSD, "In ntfs_init_attrdef() after " 343 "call to process_attrdef.\n"); 344 } 345 offset += 4096; 346 } while (!error && io.size); 347 ntfs_debug(DEBUG_BSD, "Exiting ntfs_init_attrdef()\n"); 348 ntfs_free(buf); 349 return error == 1 ? 0 : error; 350} 351 352/* ntfs_get_version will determine the NTFS version of the volume and will 353 * return the version in a BCD format, with the MSB being the major version 354 * number and the LSB the minor one. Otherwise return <0 on error. 355 * Example: version 3.1 will be returned as 0x0301. This has the obvious 356 * limitation of not coping with version numbers above 0x80 but that shouldn't 357 * be a problem... */ 358int ntfs_get_version(ntfs_inode* volume) 359{ 360 ntfs_attribute *volinfo; 361 362 volinfo = ntfs_find_attr(volume, volume->vol->at_volume_information, 0); 363 if (!volinfo) 364 return -EINVAL; 365 if (!volinfo->resident) { 366 ntfs_error("Volume information attribute is not resident!\n"); 367 return -EINVAL; 368 } 369 return ((ntfs_u8*)volinfo->d.data)[8] << 8 | 370 ((ntfs_u8*)volinfo->d.data)[9]; 371} 372 373int ntfs_load_special_files(ntfs_volume *vol) 374{ 375 int error; 376 ntfs_inode upcase, attrdef, volume; 377 378 vol->mft_ino = (ntfs_inode*)ntfs_calloc(sizeof(ntfs_inode)); 379 vol->mftmirr = (ntfs_inode*)ntfs_calloc(sizeof(ntfs_inode)); 380 vol->bitmap = (ntfs_inode*)ntfs_calloc(sizeof(ntfs_inode)); 381 vol->ino_flags = 4 | 2 | 1; 382 error = -ENOMEM; 383 ntfs_debug(DEBUG_BSD, "Going to load MFT\n"); 384 if (!vol->mft_ino || (error = ntfs_init_inode(vol->mft_ino, vol, 385 FILE_Mft))) { 386 ntfs_error("Problem loading MFT\n"); 387 return error; 388 } 389 ntfs_debug(DEBUG_BSD, "Going to load MIRR\n"); 390 if ((error = ntfs_init_inode(vol->mftmirr, vol, FILE_MftMirr))) { 391 ntfs_error("Problem %d loading MFTMirr\n", error); 392 return error; 393 } 394 ntfs_debug(DEBUG_BSD, "Going to load BITMAP\n"); 395 if ((error = ntfs_init_inode(vol->bitmap, vol, FILE_BitMap))) { 396 ntfs_error("Problem loading Bitmap\n"); 397 return error; 398 } 399 ntfs_debug(DEBUG_BSD, "Going to load UPCASE\n"); 400 error = ntfs_init_inode(&upcase, vol, FILE_UpCase); 401 if (error) 402 return error; 403 ntfs_init_upcase(&upcase); 404 ntfs_clear_inode(&upcase); 405 ntfs_debug(DEBUG_BSD, "Going to load ATTRDEF\n"); 406 error = ntfs_init_inode(&attrdef, vol, FILE_AttrDef); 407 if (error) 408 return error; 409 error = ntfs_init_attrdef(&attrdef); 410 ntfs_clear_inode(&attrdef); 411 if (error) 412 return error; 413 414 /* Check for NTFS version and if Win2k version (ie. 3.0+) do not allow 415 * write access since the driver write support is broken. */ 416 ntfs_debug(DEBUG_BSD, "Going to load VOLUME\n"); 417 error = ntfs_init_inode(&volume, vol, FILE_Volume); 418 if (error) 419 return error; 420 if ((error = ntfs_get_version(&volume)) >= 0x0300 && 421 !(NTFS_SB(vol)->s_flags & MS_RDONLY)) { 422 NTFS_SB(vol)->s_flags |= MS_RDONLY; 423 ntfs_error("Warning! NTFS volume version is Win2k+: Mounting " 424 "read-only\n"); 425 } 426 ntfs_clear_inode(&volume); 427 if (error < 0) 428 return error; 429 ntfs_debug(DEBUG_BSD, "NTFS volume is v%d.%d\n", error >> 8, 430 error & 0xff); 431 return 0; 432} 433 434int ntfs_release_volume(ntfs_volume *vol) 435{ 436 if (((vol->ino_flags & 1) == 1) && vol->mft_ino) { 437 ntfs_clear_inode(vol->mft_ino); 438 ntfs_free(vol->mft_ino); 439 vol->mft_ino = 0; 440 } 441 if (((vol->ino_flags & 2) == 2) && vol->mftmirr) { 442 ntfs_clear_inode(vol->mftmirr); 443 ntfs_free(vol->mftmirr); 444 vol->mftmirr = 0; 445 } 446 if (((vol->ino_flags & 4) == 4) && vol->bitmap) { 447 ntfs_clear_inode(vol->bitmap); 448 ntfs_free(vol->bitmap); 449 vol->bitmap = 0; 450 } 451 ntfs_free(vol->mft); 452 ntfs_free(vol->upcase); 453 return 0; 454} 455 456/* 457 * Writes the volume size (units of clusters) into vol_size. 458 * Returns 0 if successful or error. 459 */ 460int ntfs_get_volumesize(ntfs_volume *vol, ntfs_s64 *vol_size) 461{ 462 ntfs_io io; 463 char *cluster0; 464 465 if (!vol_size) 466 return -EFAULT; 467 cluster0 = ntfs_malloc(vol->cluster_size); 468 if (!cluster0) 469 return -ENOMEM; 470 io.fn_put = ntfs_put; 471 io.fn_get = ntfs_get; 472 io.param = cluster0; 473 io.do_read = 1; 474 io.size = vol->cluster_size; 475 ntfs_getput_clusters(vol, 0, 0, &io); 476 *vol_size = NTFS_GETU64(cluster0 + 0x28) >> 477 (ffs(NTFS_GETU8(cluster0 + 0xD)) - 1); 478 ntfs_free(cluster0); 479 return 0; 480} 481 482static int nc[16]={4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0}; 483 484int ntfs_get_free_cluster_count(ntfs_inode *bitmap) 485{ 486 ntfs_io io; 487 int offset, error, clusters; 488 unsigned char *bits = ntfs_malloc(2048); 489 if (!bits) 490 return -ENOMEM; 491 offset = clusters = 0; 492 io.fn_put = ntfs_put; 493 io.fn_get = ntfs_get; 494 while (1) { 495 register int i; 496 io.param = bits; 497 io.size = 2048; 498 error = ntfs_read_attr(bitmap, bitmap->vol->at_data, 0, offset, 499 &io); 500 if (error || io.size == 0) 501 break; 502 /* I never thought I would do loop unrolling some day */ 503 for (i = 0; i < io.size - 8; ) { 504 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 505 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 506 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 507 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 508 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 509 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 510 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 511 clusters+=nc[bits[i]>>4];clusters+=nc[bits[i++] & 0xF]; 512 } 513 while (i < io.size) { 514 clusters += nc[bits[i] >> 4]; 515 clusters += nc[bits[i++] & 0xF]; 516 } 517 offset += io.size; 518 } 519 ntfs_free(bits); 520 return clusters; 521} 522 523/* 524 * Insert the fixups for the record. The number and location of the fixes 525 * is obtained from the record header but we double check with @rec_size and 526 * use that as the upper boundary, if necessary overwriting the count value in 527 * the record header. 528 * 529 * We return 0 on success or -1 if fixup header indicated the beginning of the 530 * update sequence array to be beyond the valid limit. 531 */ 532int ntfs_insert_fixups(unsigned char *rec, int rec_size) 533{ 534 int first; 535 int count; 536 int offset = -2; 537 ntfs_u16 fix; 538 539 first = NTFS_GETU16(rec + 4); 540 count = (rec_size >> NTFS_SECTOR_BITS) + 1; 541 if (first + count * 2 > NTFS_SECTOR_SIZE - 2) { 542 printk(KERN_CRIT "NTFS: ntfs_insert_fixups() detected corrupt " 543 "NTFS record update sequence array position. - " 544 "Cannot hotfix.\n"); 545 return -1; 546 } 547 if (count != NTFS_GETU16(rec + 6)) { 548 printk(KERN_ERR "NTFS: ntfs_insert_fixups() detected corrupt " 549 "NTFS record update sequence array size. - " 550 "Applying hotfix.\n"); 551 NTFS_PUTU16(rec + 6, count); 552 } 553 fix = (NTFS_GETU16(rec + first) + 1) & 0xffff; 554 if (fix == 0xffff || !fix) 555 fix = 1; 556 NTFS_PUTU16(rec + first, fix); 557 count--; 558 while (count--) { 559 first += 2; 560 offset += NTFS_SECTOR_SIZE; 561 NTFS_PUTU16(rec + first, NTFS_GETU16(rec + offset)); 562 NTFS_PUTU16(rec + offset, fix); 563 } 564 return 0; 565} 566 567int ntfs_allocate_clusters(ntfs_volume *vol, ntfs_cluster_t *location, 568 ntfs_cluster_t *count, ntfs_runlist **rl, int *rl_len, 569 const NTFS_CLUSTER_ALLOCATION_ZONES zone) 570{ 571 ntfs_runlist *rl2 = NULL, *rlt; 572 ntfs_attribute *data; 573 ntfs_cluster_t buf_pos, zone_start, zone_end, mft_zone_size; 574 ntfs_cluster_t lcn, last_read_pos, prev_lcn = (ntfs_cluster_t)0; 575 ntfs_cluster_t initial_location, prev_run_len = (ntfs_cluster_t)0; 576 ntfs_cluster_t clusters = (ntfs_cluster_t)0; 577 unsigned char *buf, *byte, bit, search_zone, done_zones; 578 unsigned char pass, need_writeback; 579 int rlpos = 0, rlsize, buf_size, err = 0; 580 ntfs_io io; 581 582 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Entering with *location = " 583 "0x%x, *count = 0x%x, zone = %s_ZONE.\n", *location, 584 *count, zone == DATA_ZONE ? "DATA" : "MFT"); 585 buf = (char*)__get_free_page(GFP_NOFS); 586 if (!buf) { 587 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Returning " 588 "-ENOMEM.\n"); 589 return -ENOMEM; 590 } 591 io.fn_put = ntfs_put; 592 io.fn_get = ntfs_get; 593 lock_kernel(); 594 /* Get the $DATA attribute of $Bitmap. */ 595 data = ntfs_find_attr(vol->bitmap, vol->at_data, 0); 596 if (!data) { 597 err = -EINVAL; 598 goto err_ret; 599 } 600 /* 601 * If no specific location was requested, use the current data zone 602 * position, otherwise use the requested location but make sure it lies 603 * outside the mft zone. Also set done_zones to 0 (no zones done) and 604 * pass depending on whether we are starting inside a zone (1) or 605 * at the beginning of a zone (2). If requesting from the MFT_ZONE, then 606 * we either start at the current position within the mft zone or at the 607 * specified position and if the latter is out of bounds then we start 608 * at the beginning of the MFT_ZONE. 609 */ 610 done_zones = 0; 611 pass = 1; 612 /* 613 * zone_start and zone_end are the current search range. search_zone 614 * is 1 for mft zone, 2 for data zone 1 (end of mft zone till end of 615 * volume) and 4 for data zone 2 (start of volume till start of mft 616 * zone). 617 */ 618 zone_start = *location; 619 if (zone_start < 0) { 620 if (zone == DATA_ZONE) 621 zone_start = vol->data1_zone_pos; 622 else 623 zone_start = vol->mft_zone_pos; 624 if (!zone_start) 625 /* 626 * Zone starts at beginning of volume which means a 627 * single pass is sufficient. 628 */ 629 pass = 2; 630 } else if (zone_start >= vol->mft_zone_start && zone_start < 631 vol->mft_zone_end && zone == DATA_ZONE) { 632 zone_start = vol->mft_zone_end; 633 pass = 2; 634 } else if ((zone_start < vol->mft_zone_start || zone_start >= 635 vol->mft_zone_end) && zone == MFT_ZONE) { 636 zone_start = vol->mft_lcn; 637 if (!vol->mft_zone_end) 638 zone_start = (ntfs_cluster_t)0; 639 pass = 2; 640 } 641 if (zone == DATA_ZONE) { 642 /* Skip searching the mft zone. */ 643 done_zones |= 1; 644 if (zone_start >= vol->mft_zone_end) { 645 zone_end = vol->nr_clusters; 646 search_zone = 2; 647 } else { 648 zone_end = vol->mft_zone_start; 649 search_zone = 4; 650 } 651 } else /* if (zone == MFT_ZONE) */ { 652 zone_end = vol->mft_zone_end; 653 search_zone = 1; 654 } 655 /* 656 * buf_pos is the current bit position inside the bitmap. We use 657 * initial_location to determine whether or not to do a zone switch. 658 */ 659 buf_pos = initial_location = zone_start; 660 /* Loop until all clusters are allocated, i.e. clusters == 0. */ 661 clusters = *count; 662 rlpos = rlsize = 0; 663 if (*count <= 0) { 664 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): *count <= 0, " 665 "returning -EINVAL.\n"); 666 err = -EINVAL; 667 goto err_ret; 668 } 669 while (1) { 670 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Start of outer while " 671 "loop: done_zones = 0x%x, search_zone = %i, " 672 "pass = %i, zone_start = 0x%x, zone_end = " 673 "0x%x, initial_location = 0x%x, buf_pos = " 674 "0x%x, rlpos = %i, rlsize = %i.\n", 675 done_zones, search_zone, pass, zone_start, 676 zone_end, initial_location, buf_pos, rlpos, 677 rlsize); 678 /* Loop until we run out of free clusters. */ 679 io.param = buf; 680 io.size = PAGE_SIZE; 681 io.do_read = 1; 682 last_read_pos = buf_pos >> 3; 683 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): last_read_pos = " 684 "0x%x.\n", last_read_pos); 685 err = ntfs_readwrite_attr(vol->bitmap, data, last_read_pos, 686 &io); 687 if (err) { 688 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 689 "ntfs_read_attr failed with error " 690 "code %i, going to err_ret.\n", -err); 691 goto err_ret; 692 } 693 if (!io.size) { 694 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): !io.size, " 695 "going to zone_pass_done.\n"); 696 goto zone_pass_done; 697 } 698 buf_size = io.size << 3; 699 lcn = buf_pos & 7; 700 buf_pos &= ~7; 701 need_writeback = 0; 702 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Before inner while " 703 "loop: buf_size = 0x%x, lcn = 0x%x, buf_pos = " 704 "0x%x, need_writeback = %i.\n", buf_size, lcn, 705 buf_pos, need_writeback); 706 while (lcn < buf_size && lcn + buf_pos < zone_end) { 707 byte = buf + (lcn >> 3); 708 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): In inner " 709 "while loop: buf_size = 0x%x, lcn = " 710 "0x%x, buf_pos = 0x%x, need_writeback " 711 "= %i, byte ofs = 0x%x, *byte = " 712 "0x%x.\n", buf_size, lcn, buf_pos, 713 need_writeback, lcn >> 3, *byte); 714 /* Skip full bytes. */ 715 if (*byte == 0xff) { 716 lcn += 8; 717 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 718 "continuing while loop 1.\n"); 719 continue; 720 } 721 bit = 1 << (lcn & 7); 722 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): bit = %i.\n", 723 bit); 724 /* If the bit is already set, go onto the next one. */ 725 if (*byte & bit) { 726 lcn++; 727 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 728 "continuing while loop 2.\n"); 729 continue; 730 } 731 /* Allocate the bitmap bit. */ 732 *byte |= bit; 733 /* We need to write this bitmap buffer back to disk! */ 734 need_writeback = 1; 735 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): *byte = " 736 "0x%x, need_writeback = %i.\n", *byte, 737 need_writeback); 738 /* Reallocate memory if necessary. */ 739 if ((rlpos + 2) * sizeof(ntfs_runlist) >= rlsize) { 740 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 741 "Reallocating space.\n"); 742 /* Setup first free bit return value. */ 743 if (!rl2) { 744 *location = lcn + buf_pos; 745 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 746 "(): *location = " 747 "0x%x.\n", *location); 748 } 749 rlsize += PAGE_SIZE; 750 rlt = ntfs_vmalloc(rlsize); 751 if (!rlt) { 752 err = -ENOMEM; 753 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 754 "(): Failed to " 755 "allocate memory, " 756 "returning -ENOMEM, " 757 "going to " 758 "wb_err_ret.\n"); 759 goto wb_err_ret; 760 } 761 if (rl2) { 762 ntfs_memcpy(rlt, rl2, rlsize - 763 PAGE_SIZE); 764 ntfs_vfree(rl2); 765 } 766 rl2 = rlt; 767 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 768 "Reallocated memory, rlsize = " 769 "0x%x.\n", rlsize); 770 } 771 /* 772 * Coalesce with previous run if adjacent LCNs. 773 * Otherwise, append a new run. 774 */ 775 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Adding run " 776 "(lcn 0x%x, len 0x%x), prev_lcn = " 777 "0x%x, lcn = 0x%x, buf_pos = 0x%x, " 778 "prev_run_len = 0x%x, rlpos = %i.\n", 779 lcn + buf_pos, 1, prev_lcn, lcn, 780 buf_pos, prev_run_len, rlpos); 781 if (prev_lcn == lcn + buf_pos - prev_run_len && rlpos) { 782 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 783 "Coalescing to run (lcn 0x%x, " 784 "len 0x%x).\n", 785 rl2[rlpos - 1].lcn, 786 rl2[rlpos - 1].len); 787 rl2[rlpos - 1].len = ++prev_run_len; 788 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 789 "Run now (lcn 0x%x, len 0x%x), " 790 "prev_run_len = 0x%x.\n", 791 rl2[rlpos - 1].lcn, 792 rl2[rlpos - 1].len, 793 prev_run_len); 794 } else { 795 if (rlpos) 796 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 797 "(): Adding new run, " 798 "(previous run lcn " 799 "0x%x, len 0x%x).\n", 800 rl2[rlpos - 1].lcn, 801 rl2[rlpos - 1].len); 802 else 803 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 804 "(): Adding new run, " 805 "is first run.\n"); 806 rl2[rlpos].lcn = prev_lcn = lcn + buf_pos; 807 rl2[rlpos].len = prev_run_len = 808 (ntfs_cluster_t)1; 809 810 rlpos++; 811 } 812 /* Done? */ 813 if (!--clusters) { 814 ntfs_cluster_t tc; 815 /* 816 * Update the current zone position. Positions 817 * of already scanned zones have been updated 818 * during the respective zone switches. 819 */ 820 tc = lcn + buf_pos + 1; 821 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 822 "Done. Updating current zone " 823 "position, tc = 0x%x, " 824 "search_zone = %i.\n", tc, 825 search_zone); 826 switch (search_zone) { 827 case 1: 828 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 829 "(): Before checks, " 830 "vol->mft_zone_pos = " 831 "0x%x.\n", 832 vol->mft_zone_pos); 833 if (tc >= vol->mft_zone_end) { 834 vol->mft_zone_pos = 835 vol->mft_lcn; 836 if (!vol->mft_zone_end) 837 vol->mft_zone_pos = 838 (ntfs_cluster_t)0; 839 } else if ((initial_location >= 840 vol->mft_zone_pos || 841 tc > vol->mft_zone_pos) 842 && tc >= vol->mft_lcn) 843 vol->mft_zone_pos = tc; 844 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 845 "(): After checks, " 846 "vol->mft_zone_pos = " 847 "0x%x.\n", 848 vol->mft_zone_pos); 849 break; 850 case 2: 851 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 852 "(): Before checks, " 853 "vol->data1_zone_pos = " 854 "0x%x.\n", 855 vol->data1_zone_pos); 856 if (tc >= vol->nr_clusters) 857 vol->data1_zone_pos = 858 vol->mft_zone_end; 859 else if ((initial_location >= 860 vol->data1_zone_pos || 861 tc > vol->data1_zone_pos) 862 && tc >= vol->mft_zone_end) 863 vol->data1_zone_pos = tc; 864 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 865 "(): After checks, " 866 "vol->data1_zone_pos = " 867 "0x%x.\n", 868 vol->data1_zone_pos); 869 break; 870 case 4: 871 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 872 "(): Before checks, " 873 "vol->data2_zone_pos = " 874 "0x%x.\n", 875 vol->data2_zone_pos); 876 if (tc >= vol->mft_zone_start) 877 vol->data2_zone_pos = 878 (ntfs_cluster_t)0; 879 else if (initial_location >= 880 vol->data2_zone_pos || 881 tc > vol->data2_zone_pos) 882 vol->data2_zone_pos = tc; 883 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 884 "(): After checks, " 885 "vol->data2_zone_pos = " 886 "0x%x.\n", 887 vol->data2_zone_pos); 888 break; 889 default: 890 BUG(); 891 } 892 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 893 "Going to done_ret.\n"); 894 goto done_ret; 895 } 896 lcn++; 897 } 898 buf_pos += buf_size; 899 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): After inner while " 900 "loop: buf_size = 0x%x, lcn = 0x%x, buf_pos = " 901 "0x%x, need_writeback = %i.\n", buf_size, lcn, 902 buf_pos, need_writeback); 903 if (need_writeback) { 904 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Writing " 905 "back.\n"); 906 need_writeback = 0; 907 io.param = buf; 908 io.do_read = 0; 909 err = ntfs_readwrite_attr(vol->bitmap, data, 910 last_read_pos, &io); 911 if (err) { 912 ntfs_error(__FUNCTION__ "(): Bitmap writeback " 913 "failed in read next buffer " 914 "code path with error code " 915 "%i.\n", -err); 916 goto err_ret; 917 } 918 } 919 if (buf_pos < zone_end) { 920 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Continuing " 921 "outer while loop, buf_pos = 0x%x, " 922 "zone_end = 0x%x.\n", buf_pos, 923 zone_end); 924 continue; 925 } 926zone_pass_done: /* Finished with the current zone pass. */ 927 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At zone_pass_done, " 928 "pass = %i.\n", pass); 929 if (pass == 1) { 930 /* 931 * Now do pass 2, scanning the first part of the zone 932 * we omitted in pass 1. 933 */ 934 pass = 2; 935 zone_end = zone_start; 936 switch (search_zone) { 937 case 1: /* mft_zone */ 938 zone_start = vol->mft_zone_start; 939 break; 940 case 2: /* data1_zone */ 941 zone_start = vol->mft_zone_end; 942 break; 943 case 4: /* data2_zone */ 944 zone_start = (ntfs_cluster_t)0; 945 break; 946 default: 947 BUG(); 948 } 949 /* Sanity check. */ 950 if (zone_end < zone_start) 951 zone_end = zone_start; 952 buf_pos = zone_start; 953 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Continuing " 954 "outer while loop, pass = 2, " 955 "zone_start = 0x%x, zone_end = 0x%x, " 956 "buf_pos = 0x%x.\n"); 957 continue; 958 } /* pass == 2 */ 959done_zones_check: 960 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At done_zones_check, " 961 "search_zone = %i, done_zones before = 0x%x, " 962 "done_zones after = 0x%x.\n", 963 search_zone, done_zones, done_zones | 964 search_zone); 965 done_zones |= search_zone; 966 if (done_zones < 7) { 967 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Switching " 968 "zone.\n"); 969 /* Now switch to the next zone we haven't done yet. */ 970 pass = 1; 971 switch (search_zone) { 972 case 1: 973 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 974 "Switching from mft zone to " 975 "data1 zone.\n"); 976 /* Update mft zone position. */ 977 if (rlpos) { 978 ntfs_cluster_t tc; 979 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 980 "(): Before checks, " 981 "vol->mft_zone_pos = " 982 "0x%x.\n", 983 vol->mft_zone_pos); 984 tc = rl2[rlpos - 1].lcn + 985 rl2[rlpos - 1].len; 986 if (tc >= vol->mft_zone_end) { 987 vol->mft_zone_pos = 988 vol->mft_lcn; 989 if (!vol->mft_zone_end) 990 vol->mft_zone_pos = 991 (ntfs_cluster_t)0; 992 } else if ((initial_location >= 993 vol->mft_zone_pos || 994 tc > vol->mft_zone_pos) 995 && tc >= vol->mft_lcn) 996 vol->mft_zone_pos = tc; 997 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 998 "(): After checks, " 999 "vol->mft_zone_pos = " 1000 "0x%x.\n", 1001 vol->mft_zone_pos); 1002 } 1003 /* Switch from mft zone to data1 zone. */ 1004switch_to_data1_zone: search_zone = 2; 1005 zone_start = initial_location = 1006 vol->data1_zone_pos; 1007 zone_end = vol->nr_clusters; 1008 if (zone_start == vol->mft_zone_end) 1009 pass = 2; 1010 if (zone_start >= zone_end) { 1011 vol->data1_zone_pos = zone_start = 1012 vol->mft_zone_end; 1013 pass = 2; 1014 } 1015 break; 1016 case 2: 1017 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 1018 "Switching from data1 zone to " 1019 "data2 zone.\n"); 1020 /* Update data1 zone position. */ 1021 if (rlpos) { 1022 ntfs_cluster_t tc; 1023 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 1024 "(): Before checks, " 1025 "vol->data1_zone_pos = " 1026 "0x%x.\n", 1027 vol->data1_zone_pos); 1028 tc = rl2[rlpos - 1].lcn + 1029 rl2[rlpos - 1].len; 1030 if (tc >= vol->nr_clusters) 1031 vol->data1_zone_pos = 1032 vol->mft_zone_end; 1033 else if ((initial_location >= 1034 vol->data1_zone_pos || 1035 tc > vol->data1_zone_pos) 1036 && tc >= vol->mft_zone_end) 1037 vol->data1_zone_pos = tc; 1038 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 1039 "(): After checks, " 1040 "vol->data1_zone_pos = " 1041 "0x%x.\n", 1042 vol->data1_zone_pos); 1043 } 1044 /* Switch from data1 zone to data2 zone. */ 1045 search_zone = 4; 1046 zone_start = initial_location = 1047 vol->data2_zone_pos; 1048 zone_end = vol->mft_zone_start; 1049 if (!zone_start) 1050 pass = 2; 1051 if (zone_start >= zone_end) { 1052 vol->data2_zone_pos = zone_start = 1053 initial_location = 1054 (ntfs_cluster_t)0; 1055 pass = 2; 1056 } 1057 break; 1058 case 4: 1059 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 1060 "Switching from data2 zone to " 1061 "data1 zone.\n"); 1062 /* Update data2 zone position. */ 1063 if (rlpos) { 1064 ntfs_cluster_t tc; 1065 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 1066 "(): Before checks, " 1067 "vol->data2_zone_pos = " 1068 "0x%x.\n", 1069 vol->data2_zone_pos); 1070 tc = rl2[rlpos - 1].lcn + 1071 rl2[rlpos - 1].len; 1072 if (tc >= vol->mft_zone_start) 1073 vol->data2_zone_pos = 1074 (ntfs_cluster_t)0; 1075 else if (initial_location >= 1076 vol->data2_zone_pos || 1077 tc > vol->data2_zone_pos) 1078 vol->data2_zone_pos = tc; 1079 ntfs_debug(DEBUG_OTHER, __FUNCTION__ 1080 "(): After checks, " 1081 "vol->data2_zone_pos = " 1082 "0x%x.\n", 1083 vol->data2_zone_pos); 1084 } 1085 /* Switch from data2 zone to data1 zone. */ 1086 goto switch_to_data1_zone; /* See above. */ 1087 default: 1088 BUG(); 1089 } 1090 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): After zone " 1091 "switch, search_zone = %i, pass = %i, " 1092 "initial_location = 0x%x, zone_start " 1093 "= 0x%x, zone_end = 0x%x.\n", 1094 search_zone, pass, initial_location, 1095 zone_start, zone_end); 1096 buf_pos = zone_start; 1097 if (zone_start == zone_end) { 1098 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): " 1099 "Empty zone, going to " 1100 "done_zones_check.\n"); 1101 /* Empty zone. Don't bother searching it. */ 1102 goto done_zones_check; 1103 } 1104 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Continuing " 1105 "outer while loop.\n"); 1106 continue; 1107 } /* done_zones == 7 */ 1108 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): All zones are " 1109 "finished.\n"); 1110 /* 1111 * All zones are finished! If DATA_ZONE, shrink mft zone. If 1112 * MFT_ZONE, we have really run out of space. 1113 */ 1114 mft_zone_size = vol->mft_zone_end - vol->mft_zone_start; 1115 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): vol->mft_zone_start " 1116 "= 0x%x, vol->mft_zone_end = 0x%x, " 1117 "mft_zone_size = 0x%x.\n", vol->mft_zone_start, 1118 vol->mft_zone_end, mft_zone_size); 1119 if (zone == MFT_ZONE || mft_zone_size <= (ntfs_cluster_t)0) { 1120 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): No free " 1121 "clusters left, returning -ENOSPC, " 1122 "going to fail_ret.\n"); 1123 /* Really no more space left on device. */ 1124 err = -ENOSPC; 1125 goto fail_ret; 1126 } /* zone == DATA_ZONE && mft_zone_size > 0 */ 1127 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Shrinking mft " 1128 "zone.\n"); 1129 zone_end = vol->mft_zone_end; 1130 mft_zone_size >>= 1; 1131 if (mft_zone_size > (ntfs_cluster_t)0) 1132 vol->mft_zone_end = vol->mft_zone_start + mft_zone_size; 1133 else /* mft zone and data2 zone no longer exist. */ 1134 vol->data2_zone_pos = vol->mft_zone_start = 1135 vol->mft_zone_end = (ntfs_cluster_t)0; 1136 if (vol->mft_zone_pos >= vol->mft_zone_end) { 1137 vol->mft_zone_pos = vol->mft_lcn; 1138 if (!vol->mft_zone_end) 1139 vol->mft_zone_pos = (ntfs_cluster_t)0; 1140 } 1141 buf_pos = zone_start = initial_location = 1142 vol->data1_zone_pos = vol->mft_zone_end; 1143 search_zone = 2; 1144 pass = 2; 1145 done_zones &= ~2; 1146 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): After shrinking mft " 1147 "zone, mft_zone_size = 0x%x, " 1148 "vol->mft_zone_start = 0x%x, vol->mft_zone_end " 1149 "= 0x%x, vol->mft_zone_pos = 0x%x, search_zone " 1150 "= 2, pass = 2, dones_zones = 0x%x, zone_start " 1151 "= 0x%x, zone_end = 0x%x, vol->data1_zone_pos " 1152 "= 0x%x, continuing outer while loop.\n", 1153 mft_zone_size, vol->mft_zone_start, 1154 vol->mft_zone_end, vol->mft_zone_pos, 1155 search_zone, pass, done_zones, zone_start, 1156 zone_end, vol->data1_zone_pos); 1157 } 1158 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): After outer while loop.\n"); 1159done_ret: 1160 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At done_ret.\n"); 1161 rl2[rlpos].lcn = (ntfs_cluster_t)-1; 1162 rl2[rlpos].len = (ntfs_cluster_t)0; 1163 *rl = rl2; 1164 *rl_len = rlpos; 1165 if (need_writeback) { 1166 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Writing back.\n"); 1167 need_writeback = 0; 1168 io.param = buf; 1169 io.do_read = 0; 1170 err = ntfs_readwrite_attr(vol->bitmap, data, last_read_pos, 1171 &io); 1172 if (err) { 1173 ntfs_error(__FUNCTION__ "(): Bitmap writeback failed " 1174 "in done code path with error code " 1175 "%i.\n", -err); 1176 goto err_ret; 1177 } 1178 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Wrote 0x%Lx bytes.\n", 1179 io.size); 1180 } 1181done_fail_ret: 1182 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At done_fail_ret (follows " 1183 "done_ret).\n"); 1184 unlock_kernel(); 1185 free_page((unsigned long)buf); 1186 if (err) 1187 ntfs_debug(DEBUG_FILE3, __FUNCTION__ "(): Failed to allocate " 1188 "clusters. Returning with error code %i.\n", 1189 -err); 1190 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Syncing $Bitmap inode.\n"); 1191 if (ntfs_update_inode(vol->bitmap)) 1192 ntfs_error(__FUNCTION__ "(): Failed to sync inode $Bitmap. " 1193 "Continuing anyway.\n"); 1194 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Returning with code %i.\n", 1195 err); 1196 return err; 1197fail_ret: 1198 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At fail_ret.\n"); 1199 if (rl2) { 1200 if (err == -ENOSPC) { 1201 /* Return first free lcn and count of free clusters. */ 1202 *location = rl2[0].lcn; 1203 *count -= clusters; 1204 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): err = " 1205 "-ENOSPC, *location = 0x%x, *count = " 1206 "0x%x.\n", *location, *count); 1207 } 1208 /* Deallocate all allocated clusters. */ 1209 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Deallocating " 1210 "allocated clusters.\n"); 1211 ntfs_deallocate_clusters(vol, rl2, rlpos); 1212 /* Free the runlist. */ 1213 ntfs_vfree(rl2); 1214 } else { 1215 if (err == -ENOSPC) { 1216 /* Nothing free at all. */ 1217 *location = vol->data1_zone_pos; /* Irrelevant... */ 1218 *count = 0; 1219 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): No space " 1220 "left at all, err = -ENOSPC, *location " 1221 "= 0x%x, *count = 0.\n", *location); 1222 } 1223 } 1224 *rl = NULL; 1225 *rl_len = 0; 1226 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): *rl = NULL, *rl_len = 0, " 1227 "going to done_fail_ret.\n"); 1228 goto done_fail_ret; 1229wb_err_ret: 1230 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At wb_err_ret.\n"); 1231 if (need_writeback) { 1232 int __err; 1233 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): Writing back.\n"); 1234 io.param = buf; 1235 io.do_read = 0; 1236 __err = ntfs_readwrite_attr(vol->bitmap, data, last_read_pos, 1237 &io); 1238 if (__err) 1239 ntfs_error(__FUNCTION__ "(): Bitmap writeback failed " 1240 "in error code path with error code " 1241 "%i.\n", -__err); 1242 need_writeback = 0; 1243 } 1244err_ret: 1245 ntfs_debug(DEBUG_OTHER, __FUNCTION__ "(): At err_ret, *location = -1, " 1246 "*count = 0, going to fail_ret.\n"); 1247 *location = -1; 1248 *count = 0; 1249 goto fail_ret; 1250} 1251 1252/* 1253 * IMPORTANT: Caller has to hold big kernel lock or the race monster will come 1254 * to get you! (-; 1255 * TODO: Need our own lock for bitmap accesses but BKL is more secure for now, 1256 * considering we might not have covered all places with a lock yet. In that 1257 * case the BKL offers a one way exclusion which is better than no exclusion 1258 * at all... (AIA) 1259 */ 1260static int ntfs_clear_bitrange(ntfs_inode *bitmap, 1261 const ntfs_cluster_t start_bit, const ntfs_cluster_t count) 1262{ 1263 ntfs_cluster_t buf_size, bit, nr_bits = count; 1264 unsigned char *buf, *byte; 1265 int err; 1266 ntfs_io io; 1267 1268 io.fn_put = ntfs_put; 1269 io.fn_get = ntfs_get; 1270 /* Calculate the required buffer size in bytes. */ 1271 buf_size = (ntfs_cluster_t)((start_bit & 7) + nr_bits + 7) >> 3; 1272 if (buf_size <= (ntfs_cluster_t)(64 * 1024)) 1273 buf = ntfs_malloc(buf_size); 1274 else 1275 buf = ntfs_vmalloc(buf_size); 1276 if (!buf) 1277 return -ENOMEM; 1278 /* Read the bitmap from the data attribute. */ 1279 io.param = byte = buf; 1280 io.size = buf_size; 1281 err = ntfs_read_attr(bitmap, bitmap->vol->at_data, 0, start_bit >> 3, 1282 &io); 1283 if (err || io.size != buf_size) 1284 goto err_out; 1285 /* Now clear the bits in the read bitmap. */ 1286 bit = start_bit & 7; 1287 while (bit && nr_bits) { /* Process first partial byte, if present. */ 1288 *byte &= ~(1 << bit++); 1289 nr_bits--; 1290 bit &= 7; 1291 if (!bit) 1292 byte++; 1293 } 1294 while (nr_bits >= 8) { /* Process full bytes. */ 1295 *byte = 0; 1296 nr_bits -= 8; 1297 byte++; 1298 } 1299 bit = 0; 1300 while (nr_bits) { /* Process last partial byte, if present. */ 1301 *byte &= ~(1 << bit); 1302 nr_bits--; 1303 bit++; 1304 } 1305 /* Write the modified bitmap back to disk. */ 1306 io.param = buf; 1307 io.size = buf_size; 1308 err = ntfs_write_attr(bitmap, bitmap->vol->at_data, 0, start_bit >> 3, 1309 &io); 1310err_out: 1311 if (buf_size <= (ntfs_cluster_t)(64 * 1024)) 1312 ntfs_free(buf); 1313 else 1314 ntfs_vfree(buf); 1315 if (!err && io.size != buf_size) 1316 err = -EIO; 1317 return err; 1318} 1319 1320/* 1321 * See comments for lack of zone adjustments below in the description of the 1322 * function ntfs_deallocate_clusters(). 1323 */ 1324int ntfs_deallocate_cluster_run(const ntfs_volume *vol, 1325 const ntfs_cluster_t lcn, const ntfs_cluster_t len) 1326{ 1327 int err; 1328 1329 lock_kernel(); 1330 err = ntfs_clear_bitrange(vol->bitmap, lcn, len); 1331 unlock_kernel(); 1332 return err; 1333} 1334 1335/* 1336 * This is inefficient, but logically trivial, so will do for now. Note, we 1337 * do not touch the mft nor the data zones here because we want to minimize 1338 * recycling of clusters to enhance the chances of data being undeleteable. 1339 * Also we don't want the overhead. Instead we do one additional sweep of the 1340 * current data zone during cluster allocation to check for freed clusters. 1341 */ 1342int ntfs_deallocate_clusters(const ntfs_volume *vol, const ntfs_runlist *rl, 1343 const int rl_len) 1344{ 1345 int i, err; 1346 1347 lock_kernel(); 1348 for (i = err = 0; i < rl_len && !err; i++) 1349 err = ntfs_clear_bitrange(vol->bitmap, rl[i].lcn, rl[i].len); 1350 unlock_kernel(); 1351 return err; 1352} 1353 1354