lcnalloc.c 31 KB

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