64 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->
lcn, rl->
length);
148 const NTFS_CLUSTER_ALLOCATION_ZONES
zone,
149 const bool is_extension)
151 LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn;
152 LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size;
155 struct inode *lcnbmp_vi;
160 int err = 0, rlpos, rlsize,
buf_size;
161 u8 pass, done_zones, search_zone, need_writeback = 0,
bit;
163 ntfs_debug(
"Entering for start_vcn 0x%llx, count 0x%llx, start_lcn "
164 "0x%llx, zone %s_ZONE.", (
unsigned long long)start_vcn,
165 (
unsigned long long)count,
166 (
unsigned long long)start_lcn,
167 zone == MFT_ZONE ?
"MFT" :
"DATA");
174 BUG_ON(zone < FIRST_ZONE);
200 zone_start = start_lcn;
201 if (zone_start < 0) {
202 if (zone == DATA_ZONE)
203 zone_start = vol->data1_zone_pos;
205 zone_start = vol->mft_zone_pos;
213 }
else if (zone == DATA_ZONE && zone_start >= vol->mft_zone_start &&
214 zone_start < vol->mft_zone_end) {
215 zone_start = vol->mft_zone_end;
221 }
else if (zone == MFT_ZONE && (zone_start < vol->mft_zone_start ||
222 zone_start >= vol->mft_zone_end)) {
224 if (!vol->mft_zone_end)
232 if (zone == MFT_ZONE) {
233 zone_end = vol->mft_zone_end;
238 if (zone_start >= vol->mft_zone_end) {
242 zone_end = vol->mft_zone_start;
250 bmp_pos = bmp_initial_pos = zone_start;
256 i_size = i_size_read(lcnbmp_vi);
258 ntfs_debug(
"Start of outer while loop: done_zones 0x%x, "
259 "search_zone %i, pass %i, zone_start 0x%llx, "
260 "zone_end 0x%llx, bmp_initial_pos 0x%llx, "
261 "bmp_pos 0x%llx, rlpos %i, rlsize %i.",
262 done_zones, search_zone, pass,
263 (
unsigned long long)zone_start,
264 (
unsigned long long)zone_end,
265 (
unsigned long long)bmp_initial_pos,
266 (
unsigned long long)bmp_pos, rlpos, rlsize);
268 last_read_pos = bmp_pos >> 3;
270 (
unsigned long long)last_read_pos);
271 if (last_read_pos > i_size) {
273 "Skipping to zone_pass_done.");
277 if (need_writeback) {
283 ntfs_unmap_page(page);
285 page = ntfs_map_page(mapping, last_read_pos >>
295 if (
unlikely(last_read_pos + buf_size > i_size))
296 buf_size = i_size - last_read_pos;
300 ntfs_debug(
"Before inner while loop: buf_size %i, lcn 0x%llx, "
301 "bmp_pos 0x%llx, need_writeback %i.", buf_size,
302 (
unsigned long long)lcn,
303 (
unsigned long long)bmp_pos, need_writeback);
304 while (lcn < buf_size && lcn + bmp_pos < zone_end) {
305 byte = buf + (lcn >> 3);
306 ntfs_debug(
"In inner while loop: buf_size %i, "
307 "lcn 0x%llx, bmp_pos 0x%llx, "
308 "need_writeback %i, byte ofs 0x%x, "
309 "*byte 0x%x.", buf_size,
310 (
unsigned long long)lcn,
311 (
unsigned long long)bmp_pos,
313 (
unsigned int)(lcn >> 3),
314 (
unsigned int)*byte);
317 lcn = (lcn + 8) & ~(
LCN)7;
321 bit = 1 << (lcn & 7);
334 if ((rlpos + 2) *
sizeof(*rl) > rlsize) {
343 rl2 = ntfs_malloc_nofs(rlsize + (
int)
PAGE_SIZE);
354 ntfs_debug(
"Reallocated memory, rlsize 0x%x.",
361 ntfs_debug(
"*byte 0x%x, need_writeback is set.",
362 (
unsigned int)*byte);
367 ntfs_debug(
"Adding run (lcn 0x%llx, len 0x%llx), "
368 "prev_lcn 0x%llx, lcn 0x%llx, "
369 "bmp_pos 0x%llx, prev_run_len 0x%llx, "
371 (
unsigned long long)(lcn + bmp_pos),
372 1ULL, (
unsigned long long)prev_lcn,
373 (
unsigned long long)lcn,
374 (
unsigned long long)bmp_pos,
375 (
unsigned long long)prev_run_len,
377 if (prev_lcn == lcn + bmp_pos - prev_run_len && rlpos) {
384 rl[rlpos - 1].
length = ++prev_run_len;
385 ntfs_debug(
"Run now (lcn 0x%llx, len 0x%llx), "
386 "prev_run_len 0x%llx.",
402 rl[rlpos].
vcn = rl[rlpos - 1].
vcn +
407 rl[rlpos].
vcn = start_vcn;
409 rl[rlpos].
lcn = prev_lcn = lcn + bmp_pos;
410 rl[rlpos].
length = prev_run_len = 1;
421 tc = lcn + bmp_pos + 1;
423 "position, tc 0x%llx, "
425 (
unsigned long long)tc,
427 switch (search_zone) {
434 if (tc >= vol->mft_zone_end) {
437 if (!vol->mft_zone_end)
438 vol->mft_zone_pos = 0;
439 }
else if ((bmp_initial_pos >=
441 tc > vol->mft_zone_pos)
443 vol->mft_zone_pos =
tc;
452 "vol->data1_zone_pos "
455 vol->data1_zone_pos);
457 vol->data1_zone_pos =
459 else if ((bmp_initial_pos >=
460 vol->data1_zone_pos ||
461 tc > vol->data1_zone_pos)
462 && tc >= vol->mft_zone_end)
463 vol->data1_zone_pos =
tc;
465 "vol->data1_zone_pos "
468 vol->data1_zone_pos);
472 "vol->data2_zone_pos "
475 vol->data2_zone_pos);
476 if (tc >= vol->mft_zone_start)
477 vol->data2_zone_pos = 0;
478 else if (bmp_initial_pos >=
479 vol->data2_zone_pos ||
480 tc > vol->data2_zone_pos)
481 vol->data2_zone_pos =
tc;
483 "vol->data2_zone_pos "
486 vol->data2_zone_pos);
497 ntfs_debug(
"After inner while loop: buf_size 0x%x, lcn "
498 "0x%llx, bmp_pos 0x%llx, need_writeback %i.",
499 buf_size, (
unsigned long long)lcn,
500 (
unsigned long long)bmp_pos, need_writeback);
501 if (bmp_pos < zone_end) {
503 "bmp_pos 0x%llx, zone_end 0x%llx.",
504 (
unsigned long long)bmp_pos,
505 (
unsigned long long)zone_end);
509 ntfs_debug(
"At zone_pass_done, pass %i.", pass);
516 zone_end = zone_start;
517 switch (search_zone) {
519 zone_start = vol->mft_zone_start;
522 zone_start = vol->mft_zone_end;
531 if (zone_end < zone_start)
532 zone_end = zone_start;
533 bmp_pos = zone_start;
534 ntfs_debug(
"Continuing outer while loop, pass 2, "
535 "zone_start 0x%llx, zone_end 0x%llx, "
537 (
unsigned long long)zone_start,
538 (
unsigned long long)zone_end,
539 (
unsigned long long)bmp_pos);
543 ntfs_debug(
"At done_zones_check, search_zone %i, done_zones "
544 "before 0x%x, done_zones after 0x%x.",
545 search_zone, done_zones,
546 done_zones | search_zone);
547 done_zones |= search_zone;
548 if (done_zones < 7) {
552 switch (search_zone) {
554 ntfs_debug(
"Switching from mft zone to data1 "
565 tc = rl[rlpos - 1].
lcn +
567 if (tc >= vol->mft_zone_end) {
570 if (!vol->mft_zone_end)
571 vol->mft_zone_pos = 0;
572 }
else if ((bmp_initial_pos >=
574 tc > vol->mft_zone_pos)
576 vol->mft_zone_pos =
tc;
584 switch_to_data1_zone: search_zone = 2;
585 zone_start = bmp_initial_pos =
588 if (zone_start == vol->mft_zone_end)
590 if (zone_start >= zone_end) {
591 vol->data1_zone_pos = zone_start =
604 "vol->data1_zone_pos "
607 vol->data1_zone_pos);
608 tc = rl[rlpos - 1].
lcn +
611 vol->data1_zone_pos =
613 else if ((bmp_initial_pos >=
614 vol->data1_zone_pos ||
615 tc > vol->data1_zone_pos)
616 && tc >= vol->mft_zone_end)
617 vol->data1_zone_pos =
tc;
619 "vol->data1_zone_pos "
622 vol->data1_zone_pos);
626 zone_start = bmp_initial_pos =
628 zone_end = vol->mft_zone_start;
631 if (zone_start >= zone_end) {
632 vol->data2_zone_pos = zone_start =
645 "vol->data2_zone_pos "
648 vol->data2_zone_pos);
649 tc = rl[rlpos - 1].
lcn +
651 if (tc >= vol->mft_zone_start)
652 vol->data2_zone_pos = 0;
653 else if (bmp_initial_pos >=
654 vol->data2_zone_pos ||
655 tc > vol->data2_zone_pos)
656 vol->data2_zone_pos =
tc;
658 "vol->data2_zone_pos "
661 vol->data2_zone_pos);
664 goto switch_to_data1_zone;
668 ntfs_debug(
"After zone switch, search_zone %i, "
669 "pass %i, bmp_initial_pos 0x%llx, "
670 "zone_start 0x%llx, zone_end 0x%llx.",
672 (
unsigned long long)bmp_initial_pos,
673 (
unsigned long long)zone_start,
674 (
unsigned long long)zone_end);
675 bmp_pos = zone_start;
676 if (zone_start == zone_end) {
678 "done_zones_check.");
680 goto done_zones_check;
690 mft_zone_size = vol->mft_zone_end - vol->mft_zone_start;
691 ntfs_debug(
"vol->mft_zone_start 0x%llx, vol->mft_zone_end "
692 "0x%llx, mft_zone_size 0x%llx.",
693 (
unsigned long long)vol->mft_zone_start,
694 (
unsigned long long)vol->mft_zone_end,
695 (
unsigned long long)mft_zone_size);
696 if (zone == MFT_ZONE || mft_zone_size <= 0) {
697 ntfs_debug(
"No free clusters left, going to out.");
703 zone_end = vol->mft_zone_end;
705 if (mft_zone_size > 0)
706 vol->mft_zone_end = vol->mft_zone_start + mft_zone_size;
708 vol->data2_zone_pos = vol->mft_zone_start =
709 vol->mft_zone_end = 0;
710 if (vol->mft_zone_pos >= vol->mft_zone_end) {
711 vol->mft_zone_pos = vol->
mft_lcn;
712 if (!vol->mft_zone_end)
713 vol->mft_zone_pos = 0;
715 bmp_pos = zone_start = bmp_initial_pos =
716 vol->data1_zone_pos = vol->mft_zone_end;
720 ntfs_debug(
"After shrinking mft zone, mft_zone_size 0x%llx, "
721 "vol->mft_zone_start 0x%llx, "
722 "vol->mft_zone_end 0x%llx, "
723 "vol->mft_zone_pos 0x%llx, search_zone 2, "
724 "pass 2, dones_zones 0x%x, zone_start 0x%llx, "
725 "zone_end 0x%llx, vol->data1_zone_pos 0x%llx, "
726 "continuing outer while loop.",
727 (
unsigned long long)mft_zone_size,
728 (
unsigned long long)vol->mft_zone_start,
729 (
unsigned long long)vol->mft_zone_end,
730 (
unsigned long long)vol->mft_zone_pos,
731 done_zones, (
unsigned long long)zone_start,
732 (
unsigned long long)zone_end,
733 (
unsigned long long)vol->data1_zone_pos);
740 rl[rlpos].
vcn = rl[rlpos - 1].
vcn + rl[rlpos - 1].
length;
744 if (
likely(page && !IS_ERR(page))) {
745 if (need_writeback) {
751 ntfs_unmap_page(page);
758 ntfs_error(vol->
sb,
"Failed to allocate clusters, aborting "
764 ntfs_debug(
"Not enough space to complete allocation, "
765 "err -ENOSPC, first free lcn 0x%llx, "
766 "could allocate up to 0x%llx "
768 (
unsigned long long)rl[0].lcn,
769 (
unsigned long long)(count - clusters));
772 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl);
775 "Leaving inconsistent metadata! "
776 "Unmount and run chkdsk.", err2);
781 }
else if (err == -
ENOSPC)
782 ntfs_debug(
"No space left at all, err = -ENOSPC, first free "
784 (
long long)vol->data1_zone_pos);
852 s64 delta, to_free, total_freed, real_freed;
854 struct inode *lcnbmp_vi;
859 ntfs_debug(
"Entering for i_ino 0x%lx, start_vcn 0x%llx, count "
860 "0x%llx.%s", ni->
mft_no, (
unsigned long long)start_vcn,
861 (
unsigned long long)count,
862 is_rollback ?
" (rollback)" :
"");
878 total_freed = real_freed = 0;
884 "element (error %li), aborting.",
892 "invalid lcn, aborting.");
897 delta = start_vcn - rl->
vcn;
901 if (count >= 0 && to_free > count)
906 err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->
lcn + delta,
907 to_free,
likely(!is_rollback) ? 0 : 1);
911 "(error %i), aborting.", err);
915 real_freed = to_free;
923 total_freed = to_free;
928 for (; rl->
length && count != 0; ++rl) {
939 "runlist fragment or "
941 "subsequent runlist "
958 if (count >= 0 && to_free > count)
963 err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->
lcn,
964 to_free,
likely(!is_rollback) ? 0 : 1);
972 real_freed += to_free;
979 total_freed += to_free;
1002 delta = __ntfs_cluster_free(ni, start_vcn, total_freed, ctx,
true);
1004 ntfs_error(vol->
sb,
"Failed to rollback (error %i). Leaving "
1005 "inconsistent metadata! Unmount and run "
1006 "chkdsk.", (
int)delta);