lcnalloc.c 31 KB

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