lcnalloc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lcnalloc.c - Cluster (de)allocation code. Part of the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2004-2005 Anton Altaparmakov
  6. */
  7. #ifdef NTFS_RW
  8. #include <linux/pagemap.h>
  9. #include "lcnalloc.h"
  10. #include "debug.h"
  11. #include "bitmap.h"
  12. #include "inode.h"
  13. #include "volume.h"
  14. #include "attrib.h"
  15. #include "malloc.h"
  16. #include "aops.h"
  17. #include "ntfs.h"
  18. /**
  19. * ntfs_cluster_free_from_rl_nolock - free clusters from runlist
  20. * @vol: mounted ntfs volume on which to free the clusters
  21. * @rl: runlist describing the clusters to free
  22. *
  23. * Free all the clusters described by the runlist @rl on the volume @vol. In
  24. * the case of an error being returned, at least some of the clusters were not
  25. * freed.
  26. *
  27. * Return 0 on success and -errno on error.
  28. *
  29. * Locking: - The volume lcn bitmap must be locked for writing on entry and is
  30. * left locked on return.
  31. */
  32. int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
  33. const runlist_element *rl)
  34. {
  35. struct inode *lcnbmp_vi = vol->lcnbmp_ino;
  36. int ret = 0;
  37. ntfs_debug("Entering.");
  38. if (!rl)
  39. return 0;
  40. for (; rl->length; rl++) {
  41. int err;
  42. if (rl->lcn < 0)
  43. continue;
  44. err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length);
  45. if (unlikely(err && (!ret || ret == -ENOMEM) && ret != err))
  46. ret = err;
  47. }
  48. ntfs_debug("Done.");
  49. return ret;
  50. }
  51. /**
  52. * ntfs_cluster_alloc - allocate clusters on an ntfs volume
  53. * @vol: mounted ntfs volume on which to allocate the clusters
  54. * @start_vcn: vcn to use for the first allocated cluster
  55. * @count: number of clusters to allocate
  56. * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none)
  57. * @zone: zone from which to allocate the clusters
  58. * @is_extension: if 'true', this is an attribute extension
  59. *
  60. * Allocate @count clusters preferably starting at cluster @start_lcn or at the
  61. * current allocator position if @start_lcn is -1, on the mounted ntfs volume
  62. * @vol. @zone is either DATA_ZONE for allocation of normal clusters or
  63. * MFT_ZONE for allocation of clusters for the master file table, i.e. the
  64. * $MFT/$DATA attribute.
  65. *
  66. * @start_vcn specifies the vcn of the first allocated cluster. This makes
  67. * merging the resulting runlist with the old runlist easier.
  68. *
  69. * If @is_extension is 'true', the caller is allocating clusters to extend an
  70. * attribute and if it is 'false', the caller is allocating clusters to fill a
  71. * hole in an attribute. Practically the difference is that if @is_extension
  72. * is 'true' the returned runlist will be terminated with LCN_ENOENT and if
  73. * @is_extension is 'false' the runlist will be terminated with
  74. * LCN_RL_NOT_MAPPED.
  75. *
  76. * You need to check the return value with IS_ERR(). If this is false, the
  77. * function was successful and the return value is a runlist describing the
  78. * allocated cluster(s). If IS_ERR() is true, the function failed and
  79. * PTR_ERR() gives you the error code.
  80. *
  81. * Notes on the allocation algorithm
  82. * =================================
  83. *
  84. * There are two data zones. First is the area between the end of the mft zone
  85. * and the end of the volume, and second is the area between the start of the
  86. * volume and the start of the mft zone. On unmodified/standard NTFS 1.x
  87. * volumes, the second data zone does not exist due to the mft zone being
  88. * expanded to cover the start of the volume in order to reserve space for the
  89. * mft bitmap attribute.
  90. *
  91. * This is not the prettiest function but the complexity stems from the need of
  92. * implementing the mft vs data zoned approach and from the fact that we have
  93. * access to the lcn bitmap in portions of up to 8192 bytes at a time, so we
  94. * need to cope with crossing over boundaries of two buffers. Further, the
  95. * fact that the allocator allows for caller supplied hints as to the location
  96. * of where allocation should begin and the fact that the allocator keeps track
  97. * of where in the data zones the next natural allocation should occur,
  98. * contribute to the complexity of the function. But it should all be
  99. * worthwhile, because this allocator should: 1) be a full implementation of
  100. * the MFT zone approach used by Windows NT, 2) cause reduction in
  101. * fragmentation, and 3) be speedy in allocations (the code is not optimized
  102. * for speed, but the algorithm is, so further speed improvements are probably
  103. * possible).
  104. *
  105. * FIXME: We should be monitoring cluster allocation and increment the MFT zone
  106. * size dynamically but this is something for the future. We will just cause
  107. * heavier fragmentation by not doing it and I am not even sure Windows would
  108. * grow the MFT zone dynamically, so it might even be correct not to do this.
  109. * The overhead in doing dynamic MFT zone expansion would be very large and
  110. * unlikely worth the effort. (AIA)
  111. *
  112. * TODO: I have added in double the required zone position pointer wrap around
  113. * logic which can be optimized to having only one of the two logic sets.
  114. * However, having the double logic will work fine, but if we have only one of
  115. * the sets and we get it wrong somewhere, then we get into trouble, so
  116. * removing the duplicate logic requires _very_ careful consideration of _all_
  117. * possible code paths. So at least for now, I am leaving the double logic -
  118. * better safe than sorry... (AIA)
  119. *
  120. * Locking: - The volume lcn bitmap must be unlocked on entry and is unlocked
  121. * on return.
  122. * - This function takes the volume lcn bitmap lock for writing and
  123. * modifies the bitmap contents.
  124. */
  125. runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
  126. const s64 count, const LCN start_lcn,
  127. const NTFS_CLUSTER_ALLOCATION_ZONES zone,
  128. const bool is_extension)
  129. {
  130. LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn;
  131. LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size;
  132. s64 clusters;
  133. loff_t i_size;
  134. struct inode *lcnbmp_vi;
  135. runlist_element *rl = NULL;
  136. struct address_space *mapping;
  137. struct page *page = NULL;
  138. u8 *buf, *byte;
  139. int err = 0, rlpos, rlsize, buf_size;
  140. u8 pass, done_zones, search_zone, need_writeback = 0, bit;
  141. ntfs_debug("Entering for start_vcn 0x%llx, count 0x%llx, start_lcn "
  142. "0x%llx, zone %s_ZONE.", (unsigned long long)start_vcn,
  143. (unsigned long long)count,
  144. (unsigned long long)start_lcn,
  145. zone == MFT_ZONE ? "MFT" : "DATA");
  146. BUG_ON(!vol);
  147. lcnbmp_vi = vol->lcnbmp_ino;
  148. BUG_ON(!lcnbmp_vi);
  149. BUG_ON(start_vcn < 0);
  150. BUG_ON(count < 0);
  151. BUG_ON(start_lcn < -1);
  152. BUG_ON(zone < FIRST_ZONE);
  153. BUG_ON(zone > LAST_ZONE);
  154. /* Return NULL if @count is zero. */
  155. if (!count)
  156. return NULL;
  157. /* Take the lcnbmp lock for writing. */
  158. down_write(&vol->lcnbmp_lock);
  159. /*
  160. * If no specific @start_lcn was requested, use the current data zone
  161. * position, otherwise use the requested @start_lcn but make sure it
  162. * lies outside the mft zone. Also set done_zones to 0 (no zones done)
  163. * and pass depending on whether we are starting inside a zone (1) or
  164. * at the beginning of a zone (2). If requesting from the MFT_ZONE,
  165. * we either start at the current position within the mft zone or at
  166. * the specified position. If the latter is out of bounds then we start
  167. * at the beginning of the MFT_ZONE.
  168. */
  169. done_zones = 0;
  170. pass = 1;
  171. /*
  172. * zone_start and zone_end are the current search range. search_zone
  173. * is 1 for mft zone, 2 for data zone 1 (end of mft zone till end of
  174. * volume) and 4 for data zone 2 (start of volume till start of mft
  175. * zone).
  176. */
  177. zone_start = start_lcn;
  178. if (zone_start < 0) {
  179. if (zone == DATA_ZONE)
  180. zone_start = vol->data1_zone_pos;
  181. else
  182. zone_start = vol->mft_zone_pos;
  183. if (!zone_start) {
  184. /*
  185. * Zone starts at beginning of volume which means a
  186. * single pass is sufficient.
  187. */
  188. pass = 2;
  189. }
  190. } else if (zone == DATA_ZONE && zone_start >= vol->mft_zone_start &&
  191. zone_start < vol->mft_zone_end) {
  192. zone_start = vol->mft_zone_end;
  193. /*
  194. * Starting at beginning of data1_zone which means a single
  195. * pass in this zone is sufficient.
  196. */
  197. pass = 2;
  198. } else if (zone == MFT_ZONE && (zone_start < vol->mft_zone_start ||
  199. zone_start >= vol->mft_zone_end)) {
  200. zone_start = vol->mft_lcn;
  201. if (!vol->mft_zone_end)
  202. zone_start = 0;
  203. /*
  204. * Starting at beginning of volume which means a single pass
  205. * is sufficient.
  206. */
  207. pass = 2;
  208. }
  209. if (zone == MFT_ZONE) {
  210. zone_end = vol->mft_zone_end;
  211. search_zone = 1;
  212. } else /* if (zone == DATA_ZONE) */ {
  213. /* Skip searching the mft zone. */
  214. done_zones |= 1;
  215. if (zone_start >= vol->mft_zone_end) {
  216. zone_end = vol->nr_clusters;
  217. search_zone = 2;
  218. } else {
  219. zone_end = vol->mft_zone_start;
  220. search_zone = 4;
  221. }
  222. }
  223. /*
  224. * bmp_pos is the current bit position inside the bitmap. We use
  225. * bmp_initial_pos to determine whether or not to do a zone switch.
  226. */
  227. bmp_pos = bmp_initial_pos = zone_start;
  228. /* Loop until all clusters are allocated, i.e. clusters == 0. */
  229. clusters = count;
  230. rlpos = rlsize = 0;
  231. mapping = lcnbmp_vi->i_mapping;
  232. i_size = i_size_read(lcnbmp_vi);
  233. while (1) {
  234. ntfs_debug("Start of outer while loop: done_zones 0x%x, "
  235. "search_zone %i, pass %i, zone_start 0x%llx, "
  236. "zone_end 0x%llx, bmp_initial_pos 0x%llx, "
  237. "bmp_pos 0x%llx, rlpos %i, rlsize %i.",
  238. done_zones, search_zone, pass,
  239. (unsigned long long)zone_start,
  240. (unsigned long long)zone_end,
  241. (unsigned long long)bmp_initial_pos,
  242. (unsigned long long)bmp_pos, rlpos, rlsize);
  243. /* Loop until we run out of free clusters. */
  244. last_read_pos = bmp_pos >> 3;
  245. ntfs_debug("last_read_pos 0x%llx.",
  246. (unsigned long long)last_read_pos);
  247. if (last_read_pos > i_size) {
  248. ntfs_debug("End of attribute reached. "
  249. "Skipping to zone_pass_done.");
  250. goto zone_pass_done;
  251. }
  252. if (likely(page)) {
  253. if (need_writeback) {
  254. ntfs_debug("Marking page dirty.");
  255. flush_dcache_page(page);
  256. set_page_dirty(page);
  257. need_writeback = 0;
  258. }
  259. ntfs_unmap_page(page);
  260. }
  261. page = ntfs_map_page(mapping, last_read_pos >>
  262. PAGE_SHIFT);
  263. if (IS_ERR(page)) {
  264. err = PTR_ERR(page);
  265. ntfs_error(vol->sb, "Failed to map page.");
  266. goto out;
  267. }
  268. buf_size = last_read_pos & ~PAGE_MASK;
  269. buf = page_address(page) + buf_size;
  270. buf_size = PAGE_SIZE - buf_size;
  271. if (unlikely(last_read_pos + buf_size > i_size))
  272. buf_size = i_size - last_read_pos;
  273. buf_size <<= 3;
  274. lcn = bmp_pos & 7;
  275. bmp_pos &= ~(LCN)7;
  276. ntfs_debug("Before inner while loop: buf_size %i, lcn 0x%llx, "
  277. "bmp_pos 0x%llx, need_writeback %i.", buf_size,
  278. (unsigned long long)lcn,
  279. (unsigned long long)bmp_pos, need_writeback);
  280. while (lcn < buf_size && lcn + bmp_pos < zone_end) {
  281. byte = buf + (lcn >> 3);
  282. ntfs_debug("In inner while loop: buf_size %i, "
  283. "lcn 0x%llx, bmp_pos 0x%llx, "
  284. "need_writeback %i, byte ofs 0x%x, "
  285. "*byte 0x%x.", buf_size,
  286. (unsigned long long)lcn,
  287. (unsigned long long)bmp_pos,
  288. need_writeback,
  289. (unsigned int)(lcn >> 3),
  290. (unsigned int)*byte);
  291. /* Skip full bytes. */
  292. if (*byte == 0xff) {
  293. lcn = (lcn + 8) & ~(LCN)7;
  294. ntfs_debug("Continuing while loop 1.");
  295. continue;
  296. }
  297. bit = 1 << (lcn & 7);
  298. ntfs_debug("bit 0x%x.", bit);
  299. /* If the bit is already set, go onto the next one. */
  300. if (*byte & bit) {
  301. lcn++;
  302. ntfs_debug("Continuing while loop 2.");
  303. continue;
  304. }
  305. /*
  306. * Allocate more memory if needed, including space for
  307. * the terminator element.
  308. * ntfs_malloc_nofs() operates on whole pages only.
  309. */
  310. if ((rlpos + 2) * sizeof(*rl) > rlsize) {
  311. runlist_element *rl2;
  312. ntfs_debug("Reallocating memory.");
  313. if (!rl)
  314. ntfs_debug("First free bit is at LCN "
  315. "0x%llx.",
  316. (unsigned long long)
  317. (lcn + bmp_pos));
  318. rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
  319. if (unlikely(!rl2)) {
  320. err = -ENOMEM;
  321. ntfs_error(vol->sb, "Failed to "
  322. "allocate memory.");
  323. goto out;
  324. }
  325. memcpy(rl2, rl, rlsize);
  326. ntfs_free(rl);
  327. rl = rl2;
  328. rlsize += PAGE_SIZE;
  329. ntfs_debug("Reallocated memory, rlsize 0x%x.",
  330. rlsize);
  331. }
  332. /* Allocate the bitmap bit. */
  333. *byte |= bit;
  334. /* We need to write this bitmap page to disk. */
  335. need_writeback = 1;
  336. ntfs_debug("*byte 0x%x, need_writeback is set.",
  337. (unsigned int)*byte);
  338. /*
  339. * Coalesce with previous run if adjacent LCNs.
  340. * Otherwise, append a new run.
  341. */
  342. ntfs_debug("Adding run (lcn 0x%llx, len 0x%llx), "
  343. "prev_lcn 0x%llx, lcn 0x%llx, "
  344. "bmp_pos 0x%llx, prev_run_len 0x%llx, "
  345. "rlpos %i.",
  346. (unsigned long long)(lcn + bmp_pos),
  347. 1ULL, (unsigned long long)prev_lcn,
  348. (unsigned long long)lcn,
  349. (unsigned long long)bmp_pos,
  350. (unsigned long long)prev_run_len,
  351. rlpos);
  352. if (prev_lcn == lcn + bmp_pos - prev_run_len && rlpos) {
  353. ntfs_debug("Coalescing to run (lcn 0x%llx, "
  354. "len 0x%llx).",
  355. (unsigned long long)
  356. rl[rlpos - 1].lcn,
  357. (unsigned long long)
  358. rl[rlpos - 1].length);
  359. rl[rlpos - 1].length = ++prev_run_len;
  360. ntfs_debug("Run now (lcn 0x%llx, len 0x%llx), "
  361. "prev_run_len 0x%llx.",
  362. (unsigned long long)
  363. rl[rlpos - 1].lcn,
  364. (unsigned long long)
  365. rl[rlpos - 1].length,
  366. (unsigned long long)
  367. prev_run_len);
  368. } else {
  369. if (likely(rlpos)) {
  370. ntfs_debug("Adding new run, (previous "
  371. "run lcn 0x%llx, "
  372. "len 0x%llx).",
  373. (unsigned long long)
  374. rl[rlpos - 1].lcn,
  375. (unsigned long long)
  376. rl[rlpos - 1].length);
  377. rl[rlpos].vcn = rl[rlpos - 1].vcn +
  378. prev_run_len;
  379. } else {
  380. ntfs_debug("Adding new run, is first "
  381. "run.");
  382. rl[rlpos].vcn = start_vcn;
  383. }
  384. rl[rlpos].lcn = prev_lcn = lcn + bmp_pos;
  385. rl[rlpos].length = prev_run_len = 1;
  386. rlpos++;
  387. }
  388. /* Done? */
  389. if (!--clusters) {
  390. LCN tc;
  391. /*
  392. * Update the current zone position. Positions
  393. * of already scanned zones have been updated
  394. * during the respective zone switches.
  395. */
  396. tc = lcn + bmp_pos + 1;
  397. ntfs_debug("Done. Updating current zone "
  398. "position, tc 0x%llx, "
  399. "search_zone %i.",
  400. (unsigned long long)tc,
  401. search_zone);
  402. switch (search_zone) {
  403. case 1:
  404. ntfs_debug("Before checks, "
  405. "vol->mft_zone_pos "
  406. "0x%llx.",
  407. (unsigned long long)
  408. vol->mft_zone_pos);
  409. if (tc >= vol->mft_zone_end) {
  410. vol->mft_zone_pos =
  411. vol->mft_lcn;
  412. if (!vol->mft_zone_end)
  413. vol->mft_zone_pos = 0;
  414. } else if ((bmp_initial_pos >=
  415. vol->mft_zone_pos ||
  416. tc > vol->mft_zone_pos)
  417. && tc >= vol->mft_lcn)
  418. vol->mft_zone_pos = tc;
  419. ntfs_debug("After checks, "
  420. "vol->mft_zone_pos "
  421. "0x%llx.",
  422. (unsigned long long)
  423. vol->mft_zone_pos);
  424. break;
  425. case 2:
  426. ntfs_debug("Before checks, "
  427. "vol->data1_zone_pos "
  428. "0x%llx.",
  429. (unsigned long long)
  430. vol->data1_zone_pos);
  431. if (tc >= vol->nr_clusters)
  432. vol->data1_zone_pos =
  433. vol->mft_zone_end;
  434. else if ((bmp_initial_pos >=
  435. vol->data1_zone_pos ||
  436. tc > vol->data1_zone_pos)
  437. && tc >= vol->mft_zone_end)
  438. vol->data1_zone_pos = tc;
  439. ntfs_debug("After checks, "
  440. "vol->data1_zone_pos "
  441. "0x%llx.",
  442. (unsigned long long)
  443. vol->data1_zone_pos);
  444. break;
  445. case 4:
  446. ntfs_debug("Before checks, "
  447. "vol->data2_zone_pos "
  448. "0x%llx.",
  449. (unsigned long long)
  450. vol->data2_zone_pos);
  451. if (tc >= vol->mft_zone_start)
  452. vol->data2_zone_pos = 0;
  453. else if (bmp_initial_pos >=
  454. vol->data2_zone_pos ||
  455. tc > vol->data2_zone_pos)
  456. vol->data2_zone_pos = tc;
  457. ntfs_debug("After checks, "
  458. "vol->data2_zone_pos "
  459. "0x%llx.",
  460. (unsigned long long)
  461. vol->data2_zone_pos);
  462. break;
  463. default:
  464. BUG();
  465. }
  466. ntfs_debug("Finished. Going to out.");
  467. goto out;
  468. }
  469. lcn++;
  470. }
  471. bmp_pos += buf_size;
  472. ntfs_debug("After inner while loop: buf_size 0x%x, lcn "
  473. "0x%llx, bmp_pos 0x%llx, need_writeback %i.",
  474. buf_size, (unsigned long long)lcn,
  475. (unsigned long long)bmp_pos, need_writeback);
  476. if (bmp_pos < zone_end) {
  477. ntfs_debug("Continuing outer while loop, "
  478. "bmp_pos 0x%llx, zone_end 0x%llx.",
  479. (unsigned long long)bmp_pos,
  480. (unsigned long long)zone_end);
  481. continue;
  482. }
  483. zone_pass_done: /* Finished with the current zone pass. */
  484. ntfs_debug("At zone_pass_done, pass %i.", pass);
  485. if (pass == 1) {
  486. /*
  487. * Now do pass 2, scanning the first part of the zone
  488. * we omitted in pass 1.
  489. */
  490. pass = 2;
  491. zone_end = zone_start;
  492. switch (search_zone) {
  493. case 1: /* mft_zone */
  494. zone_start = vol->mft_zone_start;
  495. break;
  496. case 2: /* data1_zone */
  497. zone_start = vol->mft_zone_end;
  498. break;
  499. case 4: /* data2_zone */
  500. zone_start = 0;
  501. break;
  502. default:
  503. BUG();
  504. }
  505. /* Sanity check. */
  506. if (zone_end < zone_start)
  507. zone_end = zone_start;
  508. bmp_pos = zone_start;
  509. ntfs_debug("Continuing outer while loop, pass 2, "
  510. "zone_start 0x%llx, zone_end 0x%llx, "
  511. "bmp_pos 0x%llx.",
  512. (unsigned long long)zone_start,
  513. (unsigned long long)zone_end,
  514. (unsigned long long)bmp_pos);
  515. continue;
  516. } /* pass == 2 */
  517. done_zones_check:
  518. ntfs_debug("At done_zones_check, search_zone %i, done_zones "
  519. "before 0x%x, done_zones after 0x%x.",
  520. search_zone, done_zones,
  521. done_zones | search_zone);
  522. done_zones |= search_zone;
  523. if (done_zones < 7) {
  524. ntfs_debug("Switching zone.");
  525. /* Now switch to the next zone we haven't done yet. */
  526. pass = 1;
  527. switch (search_zone) {
  528. case 1:
  529. ntfs_debug("Switching from mft zone to data1 "
  530. "zone.");
  531. /* Update mft zone position. */
  532. if (rlpos) {
  533. LCN tc;
  534. ntfs_debug("Before checks, "
  535. "vol->mft_zone_pos "
  536. "0x%llx.",
  537. (unsigned long long)
  538. vol->mft_zone_pos);
  539. tc = rl[rlpos - 1].lcn +
  540. rl[rlpos - 1].length;
  541. if (tc >= vol->mft_zone_end) {
  542. vol->mft_zone_pos =
  543. vol->mft_lcn;
  544. if (!vol->mft_zone_end)
  545. vol->mft_zone_pos = 0;
  546. } else if ((bmp_initial_pos >=
  547. vol->mft_zone_pos ||
  548. tc > vol->mft_zone_pos)
  549. && tc >= vol->mft_lcn)
  550. vol->mft_zone_pos = tc;
  551. ntfs_debug("After checks, "
  552. "vol->mft_zone_pos "
  553. "0x%llx.",
  554. (unsigned long long)
  555. vol->mft_zone_pos);
  556. }
  557. /* Switch from mft zone to data1 zone. */
  558. switch_to_data1_zone: search_zone = 2;
  559. zone_start = bmp_initial_pos =
  560. vol->data1_zone_pos;
  561. zone_end = vol->nr_clusters;
  562. if (zone_start == vol->mft_zone_end)
  563. pass = 2;
  564. if (zone_start >= zone_end) {
  565. vol->data1_zone_pos = zone_start =
  566. vol->mft_zone_end;
  567. pass = 2;
  568. }
  569. break;
  570. case 2:
  571. ntfs_debug("Switching from data1 zone to "
  572. "data2 zone.");
  573. /* Update data1 zone position. */
  574. if (rlpos) {
  575. LCN tc;
  576. ntfs_debug("Before checks, "
  577. "vol->data1_zone_pos "
  578. "0x%llx.",
  579. (unsigned long long)
  580. vol->data1_zone_pos);
  581. tc = rl[rlpos - 1].lcn +
  582. rl[rlpos - 1].length;
  583. if (tc >= vol->nr_clusters)
  584. vol->data1_zone_pos =
  585. vol->mft_zone_end;
  586. else if ((bmp_initial_pos >=
  587. vol->data1_zone_pos ||
  588. tc > vol->data1_zone_pos)
  589. && tc >= vol->mft_zone_end)
  590. vol->data1_zone_pos = tc;
  591. ntfs_debug("After checks, "
  592. "vol->data1_zone_pos "
  593. "0x%llx.",
  594. (unsigned long long)
  595. vol->data1_zone_pos);
  596. }
  597. /* Switch from data1 zone to data2 zone. */
  598. search_zone = 4;
  599. zone_start = bmp_initial_pos =
  600. vol->data2_zone_pos;
  601. zone_end = vol->mft_zone_start;
  602. if (!zone_start)
  603. pass = 2;
  604. if (zone_start >= zone_end) {
  605. vol->data2_zone_pos = zone_start =
  606. bmp_initial_pos = 0;
  607. pass = 2;
  608. }
  609. break;
  610. case 4:
  611. ntfs_debug("Switching from data2 zone to "
  612. "data1 zone.");
  613. /* Update data2 zone position. */
  614. if (rlpos) {
  615. LCN tc;
  616. ntfs_debug("Before checks, "
  617. "vol->data2_zone_pos "
  618. "0x%llx.",
  619. (unsigned long long)
  620. vol->data2_zone_pos);
  621. tc = rl[rlpos - 1].lcn +
  622. rl[rlpos - 1].length;
  623. if (tc >= vol->mft_zone_start)
  624. vol->data2_zone_pos = 0;
  625. else if (bmp_initial_pos >=
  626. vol->data2_zone_pos ||
  627. tc > vol->data2_zone_pos)
  628. vol->data2_zone_pos = tc;
  629. ntfs_debug("After checks, "
  630. "vol->data2_zone_pos "
  631. "0x%llx.",
  632. (unsigned long long)
  633. vol->data2_zone_pos);
  634. }
  635. /* Switch from data2 zone to data1 zone. */
  636. goto switch_to_data1_zone;
  637. default:
  638. BUG();
  639. }
  640. ntfs_debug("After zone switch, search_zone %i, "
  641. "pass %i, bmp_initial_pos 0x%llx, "
  642. "zone_start 0x%llx, zone_end 0x%llx.",
  643. search_zone, pass,
  644. (unsigned long long)bmp_initial_pos,
  645. (unsigned long long)zone_start,
  646. (unsigned long long)zone_end);
  647. bmp_pos = zone_start;
  648. if (zone_start == zone_end) {
  649. ntfs_debug("Empty zone, going to "
  650. "done_zones_check.");
  651. /* Empty zone. Don't bother searching it. */
  652. goto done_zones_check;
  653. }
  654. ntfs_debug("Continuing outer while loop.");
  655. continue;
  656. } /* done_zones == 7 */
  657. ntfs_debug("All zones are finished.");
  658. /*
  659. * All zones are finished! If DATA_ZONE, shrink mft zone. If
  660. * MFT_ZONE, we have really run out of space.
  661. */
  662. mft_zone_size = vol->mft_zone_end - vol->mft_zone_start;
  663. ntfs_debug("vol->mft_zone_start 0x%llx, vol->mft_zone_end "
  664. "0x%llx, mft_zone_size 0x%llx.",
  665. (unsigned long long)vol->mft_zone_start,
  666. (unsigned long long)vol->mft_zone_end,
  667. (unsigned long long)mft_zone_size);
  668. if (zone == MFT_ZONE || mft_zone_size <= 0) {
  669. ntfs_debug("No free clusters left, going to out.");
  670. /* Really no more space left on device. */
  671. err = -ENOSPC;
  672. goto out;
  673. } /* zone == DATA_ZONE && mft_zone_size > 0 */
  674. ntfs_debug("Shrinking mft zone.");
  675. zone_end = vol->mft_zone_end;
  676. mft_zone_size >>= 1;
  677. if (mft_zone_size > 0)
  678. vol->mft_zone_end = vol->mft_zone_start + mft_zone_size;
  679. else /* mft zone and data2 zone no longer exist. */
  680. vol->data2_zone_pos = vol->mft_zone_start =
  681. vol->mft_zone_end = 0;
  682. if (vol->mft_zone_pos >= vol->mft_zone_end) {
  683. vol->mft_zone_pos = vol->mft_lcn;
  684. if (!vol->mft_zone_end)
  685. vol->mft_zone_pos = 0;
  686. }
  687. bmp_pos = zone_start = bmp_initial_pos =
  688. vol->data1_zone_pos = vol->mft_zone_end;
  689. search_zone = 2;
  690. pass = 2;
  691. done_zones &= ~2;
  692. ntfs_debug("After shrinking mft zone, mft_zone_size 0x%llx, "
  693. "vol->mft_zone_start 0x%llx, "
  694. "vol->mft_zone_end 0x%llx, "
  695. "vol->mft_zone_pos 0x%llx, search_zone 2, "
  696. "pass 2, dones_zones 0x%x, zone_start 0x%llx, "
  697. "zone_end 0x%llx, vol->data1_zone_pos 0x%llx, "
  698. "continuing outer while loop.",
  699. (unsigned long long)mft_zone_size,
  700. (unsigned long long)vol->mft_zone_start,
  701. (unsigned long long)vol->mft_zone_end,
  702. (unsigned long long)vol->mft_zone_pos,
  703. done_zones, (unsigned long long)zone_start,
  704. (unsigned long long)zone_end,
  705. (unsigned long long)vol->data1_zone_pos);
  706. }
  707. ntfs_debug("After outer while loop.");
  708. out:
  709. ntfs_debug("At out.");
  710. /* Add runlist terminator element. */
  711. if (likely(rl)) {
  712. rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length;
  713. rl[rlpos].lcn = is_extension ? LCN_ENOENT : LCN_RL_NOT_MAPPED;
  714. rl[rlpos].length = 0;
  715. }
  716. if (likely(page && !IS_ERR(page))) {
  717. if (need_writeback) {
  718. ntfs_debug("Marking page dirty.");
  719. flush_dcache_page(page);
  720. set_page_dirty(page);
  721. need_writeback = 0;
  722. }
  723. ntfs_unmap_page(page);
  724. }
  725. if (likely(!err)) {
  726. up_write(&vol->lcnbmp_lock);
  727. ntfs_debug("Done.");
  728. return rl;
  729. }
  730. ntfs_error(vol->sb, "Failed to allocate clusters, aborting "
  731. "(error %i).", err);
  732. if (rl) {
  733. int err2;
  734. if (err == -ENOSPC)
  735. ntfs_debug("Not enough space to complete allocation, "
  736. "err -ENOSPC, first free lcn 0x%llx, "
  737. "could allocate up to 0x%llx "
  738. "clusters.",
  739. (unsigned long long)rl[0].lcn,
  740. (unsigned long long)(count - clusters));
  741. /* Deallocate all allocated clusters. */
  742. ntfs_debug("Attempting rollback...");
  743. err2 = ntfs_cluster_free_from_rl_nolock(vol, rl);
  744. if (err2) {
  745. ntfs_error(vol->sb, "Failed to rollback (error %i). "
  746. "Leaving inconsistent metadata! "
  747. "Unmount and run chkdsk.", err2);
  748. NVolSetErrors(vol);
  749. }
  750. /* Free the runlist. */
  751. ntfs_free(rl);
  752. } else if (err == -ENOSPC)
  753. ntfs_debug("No space left at all, err = -ENOSPC, first free "
  754. "lcn = 0x%llx.",
  755. (long long)vol->data1_zone_pos);
  756. up_write(&vol->lcnbmp_lock);
  757. return ERR_PTR(err);
  758. }
  759. /**
  760. * __ntfs_cluster_free - free clusters on an ntfs volume
  761. * @ni: ntfs inode whose runlist describes the clusters to free
  762. * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
  763. * @count: number of clusters to free or -1 for all clusters
  764. * @ctx: active attribute search context if present or NULL if not
  765. * @is_rollback: true if this is a rollback operation
  766. *
  767. * Free @count clusters starting at the cluster @start_vcn in the runlist
  768. * described by the vfs inode @ni.
  769. *
  770. * If @count is -1, all clusters from @start_vcn to the end of the runlist are
  771. * deallocated. Thus, to completely free all clusters in a runlist, use
  772. * @start_vcn = 0 and @count = -1.
  773. *
  774. * If @ctx is specified, it is an active search context of @ni and its base mft
  775. * record. This is needed when __ntfs_cluster_free() encounters unmapped
  776. * runlist fragments and allows their mapping. If you do not have the mft
  777. * record mapped, you can specify @ctx as NULL and __ntfs_cluster_free() will
  778. * perform the necessary mapping and unmapping.
  779. *
  780. * Note, __ntfs_cluster_free() saves the state of @ctx on entry and restores it
  781. * before returning. Thus, @ctx will be left pointing to the same attribute on
  782. * return as on entry. However, the actual pointers in @ctx may point to
  783. * different memory locations on return, so you must remember to reset any
  784. * cached pointers from the @ctx, i.e. after the call to __ntfs_cluster_free(),
  785. * you will probably want to do:
  786. * m = ctx->mrec;
  787. * a = ctx->attr;
  788. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  789. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  790. *
  791. * @is_rollback should always be 'false', it is for internal use to rollback
  792. * errors. You probably want to use ntfs_cluster_free() instead.
  793. *
  794. * Note, __ntfs_cluster_free() does not modify the runlist, so you have to
  795. * remove from the runlist or mark sparse the freed runs later.
  796. *
  797. * Return the number of deallocated clusters (not counting sparse ones) on
  798. * success and -errno on error.
  799. *
  800. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  801. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  802. * is no longer valid, i.e. you need to either call
  803. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  804. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  805. * why the mapping of the old inode failed.
  806. *
  807. * Locking: - The runlist described by @ni must be locked for writing on entry
  808. * and is locked on return. Note the runlist may be modified when
  809. * needed runlist fragments need to be mapped.
  810. * - The volume lcn bitmap must be unlocked on entry and is unlocked
  811. * on return.
  812. * - This function takes the volume lcn bitmap lock for writing and
  813. * modifies the bitmap contents.
  814. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  815. * entry and it will be left unmapped on return.
  816. * - If @ctx is not NULL, the base mft record must be mapped on entry
  817. * and it will be left mapped on return.
  818. */
  819. s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
  820. ntfs_attr_search_ctx *ctx, const bool is_rollback)
  821. {
  822. s64 delta, to_free, total_freed, real_freed;
  823. ntfs_volume *vol;
  824. struct inode *lcnbmp_vi;
  825. runlist_element *rl;
  826. int err;
  827. BUG_ON(!ni);
  828. ntfs_debug("Entering for i_ino 0x%lx, start_vcn 0x%llx, count "
  829. "0x%llx.%s", ni->mft_no, (unsigned long long)start_vcn,
  830. (unsigned long long)count,
  831. is_rollback ? " (rollback)" : "");
  832. vol = ni->vol;
  833. lcnbmp_vi = vol->lcnbmp_ino;
  834. BUG_ON(!lcnbmp_vi);
  835. BUG_ON(start_vcn < 0);
  836. BUG_ON(count < -1);
  837. /*
  838. * Lock the lcn bitmap for writing but only if not rolling back. We
  839. * must hold the lock all the way including through rollback otherwise
  840. * rollback is not possible because once we have cleared a bit and
  841. * dropped the lock, anyone could have set the bit again, thus
  842. * allocating the cluster for another use.
  843. */
  844. if (likely(!is_rollback))
  845. down_write(&vol->lcnbmp_lock);
  846. total_freed = real_freed = 0;
  847. rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, ctx);
  848. if (IS_ERR(rl)) {
  849. if (!is_rollback)
  850. ntfs_error(vol->sb, "Failed to find first runlist "
  851. "element (error %li), aborting.",
  852. PTR_ERR(rl));
  853. err = PTR_ERR(rl);
  854. goto err_out;
  855. }
  856. if (unlikely(rl->lcn < LCN_HOLE)) {
  857. if (!is_rollback)
  858. ntfs_error(vol->sb, "First runlist element has "
  859. "invalid lcn, aborting.");
  860. err = -EIO;
  861. goto err_out;
  862. }
  863. /* Find the starting cluster inside the run that needs freeing. */
  864. delta = start_vcn - rl->vcn;
  865. /* The number of clusters in this run that need freeing. */
  866. to_free = rl->length - delta;
  867. if (count >= 0 && to_free > count)
  868. to_free = count;
  869. if (likely(rl->lcn >= 0)) {
  870. /* Do the actual freeing of the clusters in this run. */
  871. err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->lcn + delta,
  872. to_free, likely(!is_rollback) ? 0 : 1);
  873. if (unlikely(err)) {
  874. if (!is_rollback)
  875. ntfs_error(vol->sb, "Failed to clear first run "
  876. "(error %i), aborting.", err);
  877. goto err_out;
  878. }
  879. /* We have freed @to_free real clusters. */
  880. real_freed = to_free;
  881. };
  882. /* Go to the next run and adjust the number of clusters left to free. */
  883. ++rl;
  884. if (count >= 0)
  885. count -= to_free;
  886. /* Keep track of the total "freed" clusters, including sparse ones. */
  887. total_freed = to_free;
  888. /*
  889. * Loop over the remaining runs, using @count as a capping value, and
  890. * free them.
  891. */
  892. for (; rl->length && count != 0; ++rl) {
  893. if (unlikely(rl->lcn < LCN_HOLE)) {
  894. VCN vcn;
  895. /* Attempt to map runlist. */
  896. vcn = rl->vcn;
  897. rl = ntfs_attr_find_vcn_nolock(ni, vcn, ctx);
  898. if (IS_ERR(rl)) {
  899. err = PTR_ERR(rl);
  900. if (!is_rollback)
  901. ntfs_error(vol->sb, "Failed to map "
  902. "runlist fragment or "
  903. "failed to find "
  904. "subsequent runlist "
  905. "element.");
  906. goto err_out;
  907. }
  908. if (unlikely(rl->lcn < LCN_HOLE)) {
  909. if (!is_rollback)
  910. ntfs_error(vol->sb, "Runlist element "
  911. "has invalid lcn "
  912. "(0x%llx).",
  913. (unsigned long long)
  914. rl->lcn);
  915. err = -EIO;
  916. goto err_out;
  917. }
  918. }
  919. /* The number of clusters in this run that need freeing. */
  920. to_free = rl->length;
  921. if (count >= 0 && to_free > count)
  922. to_free = count;
  923. if (likely(rl->lcn >= 0)) {
  924. /* Do the actual freeing of the clusters in the run. */
  925. err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->lcn,
  926. to_free, likely(!is_rollback) ? 0 : 1);
  927. if (unlikely(err)) {
  928. if (!is_rollback)
  929. ntfs_error(vol->sb, "Failed to clear "
  930. "subsequent run.");
  931. goto err_out;
  932. }
  933. /* We have freed @to_free real clusters. */
  934. real_freed += to_free;
  935. }
  936. /* Adjust the number of clusters left to free. */
  937. if (count >= 0)
  938. count -= to_free;
  939. /* Update the total done clusters. */
  940. total_freed += to_free;
  941. }
  942. if (likely(!is_rollback))
  943. up_write(&vol->lcnbmp_lock);
  944. BUG_ON(count > 0);
  945. /* We are done. Return the number of actually freed clusters. */
  946. ntfs_debug("Done.");
  947. return real_freed;
  948. err_out:
  949. if (is_rollback)
  950. return err;
  951. /* If no real clusters were freed, no need to rollback. */
  952. if (!real_freed) {
  953. up_write(&vol->lcnbmp_lock);
  954. return err;
  955. }
  956. /*
  957. * Attempt to rollback and if that succeeds just return the error code.
  958. * If rollback fails, set the volume errors flag, emit an error
  959. * message, and return the error code.
  960. */
  961. delta = __ntfs_cluster_free(ni, start_vcn, total_freed, ctx, true);
  962. if (delta < 0) {
  963. ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving "
  964. "inconsistent metadata! Unmount and run "
  965. "chkdsk.", (int)delta);
  966. NVolSetErrors(vol);
  967. }
  968. up_write(&vol->lcnbmp_lock);
  969. ntfs_error(vol->sb, "Aborting (error %i).", err);
  970. return err;
  971. }
  972. #endif /* NTFS_RW */