lcnalloc.c 30 KB

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