24 #include <linux/slab.h>
47 static inline MFT_RECORD *map_mft_record_page(
ntfs_inode *ni)
53 unsigned long index, end_index;
67 i_size = i_size_read(mft_vi);
77 "which is beyond the end of the mft. "
78 "This is probably a bug in the ntfs "
84 page = ntfs_map_page(mft_vi->
i_mapping, index);
85 if (
likely(!IS_ERR(page))) {
94 "Run chkdsk.", ni->
mft_no);
95 ntfs_unmap_page(page);
167 m = map_mft_record_page(ni);
191 static inline void unmap_mft_record_page(
ntfs_inode *ni)
196 ntfs_unmap_page(ni->
page);
215 struct page *page = ni->
page;
221 unmap_mft_record_page(ni);
253 unsigned long mft_no =
MREF(mref);
255 bool destroy_ni =
false;
257 ntfs_debug(
"Mapping extent mft record 0x%lx (base mft record 0x%lx).",
270 if (mft_no != extent_nis[i]->mft_no)
294 "reference! Corrupt filesystem. "
296 return ERR_PTR(-
EIO);
300 "mft record, error code %ld.", -PTR_ERR(m));
323 if (seq_no && (
le16_to_cpu(m->sequence_number) != seq_no)) {
325 "reference! Corrupt filesystem. Run chkdsk.");
412 static const char *ntfs_please_email =
"Please email "
414 "this message. Thank you.";
438 static int ntfs_sync_mft_mirror_umount(
ntfs_volume *vol,
439 const unsigned long mft_no, MFT_RECORD *
m)
442 ntfs_error(vol->
sb,
"Umount time mft mirror syncing is not "
443 "implemented yet. %s", ntfs_please_email);
465 int ntfs_sync_mft_mirror(
ntfs_volume *vol,
const unsigned long mft_no,
466 MFT_RECORD *
m,
int sync)
469 unsigned int blocksize = vol->
sb->s_blocksize;
471 struct buffer_head *bhs[max_bhs];
472 struct buffer_head *bh, *
head;
475 unsigned int block_start, block_end, m_start, m_end, page_ofs;
476 int i_bhs, nr_bhs,
err = 0;
477 unsigned char blocksize_bits = vol->
sb->s_blocksize_bits;
479 ntfs_debug(
"Entering for inode 0x%lx.", mft_no);
483 err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
489 page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
497 BUG_ON(!PageUptodate(page));
498 ClearPageUptodate(page);
506 if (
unlikely(!page_has_buffers(page))) {
507 struct buffer_head *
tail;
511 set_buffer_uptodate(bh);
513 bh = bh->b_this_page;
515 tail->b_this_page =
head;
516 attach_page_buffers(page, head);
518 bh = head = page_buffers(page);
526 block_end = block_start + blocksize;
528 if (block_end <= m_start)
536 unsigned int vcn_ofs;
538 bh->b_bdev = vol->
sb->s_bdev;
541 (block_start - m_start);
547 rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
561 bh->b_blocknr = ((lcn <<
563 vcn_ofs) >> blocksize_bits;
564 set_buffer_mapped(bh);
568 "record 0x%lx because its "
569 "location on disk could not "
570 "be determined (error code "
576 BUG_ON(!buffer_uptodate(bh));
577 BUG_ON(!nr_bhs && (m_start != block_start));
578 BUG_ON(nr_bhs >= max_bhs);
580 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
581 }
while (block_start = block_end, (bh = bh->b_this_page) != head);
583 up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
586 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
587 struct buffer_head *tbh = bhs[i_bhs];
589 if (!trylock_buffer(tbh))
591 BUG_ON(!buffer_uptodate(tbh));
592 clear_buffer_dirty(tbh);
598 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
599 struct buffer_head *tbh = bhs[i_bhs];
602 if (
unlikely(!buffer_uptodate(tbh))) {
608 set_buffer_uptodate(tbh);
613 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
614 clear_buffer_dirty(bhs[i_bhs]);
620 SetPageUptodate(page);
622 ntfs_unmap_page(page);
626 ntfs_error(vol->
sb,
"I/O error while writing mft mirror "
627 "record 0x%lx!", mft_no);
629 ntfs_error(vol->
sb,
"Failed to synchronize $MFTMirr (error "
630 "code %i). Volume will be left marked dirty "
631 "on umount. Run ntfsfix on the partition "
632 "after umounting to correct this.", -err);
669 int write_mft_record_nolock(
ntfs_inode *ni, MFT_RECORD *m,
int sync)
672 struct page *page = ni->
page;
673 unsigned int blocksize = vol->
sb->s_blocksize;
674 unsigned char blocksize_bits = vol->
sb->s_blocksize_bits;
676 struct buffer_head *bhs[max_bhs];
677 struct buffer_head *bh, *
head;
679 unsigned int block_start, block_end, m_start, m_end;
680 int i_bhs, nr_bhs, err = 0;
685 BUG_ON(!PageLocked(page));
692 if (!NInoTestClearDirty(ni))
694 bh = head = page_buffers(page);
702 block_end = block_start + blocksize;
704 if (block_end <= m_start)
713 if (block_start == m_start) {
715 if (!buffer_dirty(bh)) {
725 unsigned int vcn_ofs;
727 bh->b_bdev = vol->
sb->s_bdev;
730 (block_start - m_start);
735 rl = NTFS_I(vol->
mft_ino)->runlist.rl;
745 bh->b_blocknr = ((lcn <<
747 vcn_ofs) >> blocksize_bits;
748 set_buffer_mapped(bh);
752 "0x%lx because its location "
753 "on disk could not be "
754 "determined (error code %lli).",
755 ni->
mft_no, (
long long)lcn);
759 BUG_ON(!buffer_uptodate(bh));
760 BUG_ON(!nr_bhs && (m_start != block_start));
761 BUG_ON(nr_bhs >= max_bhs);
763 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
764 }
while (block_start = block_end, (bh = bh->b_this_page) != head);
777 flush_dcache_mft_record_page(ni);
779 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
780 struct buffer_head *tbh = bhs[i_bhs];
782 if (!trylock_buffer(tbh))
784 BUG_ON(!buffer_uptodate(tbh));
785 clear_buffer_dirty(tbh);
791 if (!sync && ni->
mft_no < vol->mftmirr_size)
792 ntfs_sync_mft_mirror(vol, ni->
mft_no, m, sync);
794 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
795 struct buffer_head *tbh = bhs[i_bhs];
798 if (
unlikely(!buffer_uptodate(tbh))) {
804 if (PageUptodate(page))
805 set_buffer_uptodate(tbh);
809 if (sync && ni->
mft_no < vol->mftmirr_size)
810 ntfs_sync_mft_mirror(vol, ni->
mft_no, m, sync);
813 flush_dcache_mft_record_page(ni);
816 ntfs_error(vol->
sb,
"I/O error while writing mft record "
817 "0x%lx! Marking base inode as bad. You "
818 "should unmount the volume and run chkdsk.",
827 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
828 clear_buffer_dirty(bhs[i_bhs]);
837 ntfs_error(vol->
sb,
"Not enough memory to write mft record. "
838 "Redirtying so the write is retried later.");
839 mark_mft_record_dirty(ni);
925 bool ntfs_may_write_mft_record(
ntfs_volume *vol,
const unsigned long mft_no,
935 ntfs_debug(
"Entering for inode 0x%lx.", mft_no);
945 ntfs_debug(
"Looking for inode 0x%lx in icache.", mft_no);
969 ntfs_debug(
"Base inode 0x%lx is in icache.", mft_no);
976 ntfs_debug(
"Inode 0x%lx is dirty, do not write it.",
982 ntfs_debug(
"Inode 0x%lx is not dirty.", mft_no);
985 ntfs_debug(
"Mft record 0x%lx is already locked, do "
986 "not write it.", mft_no);
991 ntfs_debug(
"Managed to lock mft record 0x%lx, write it.",
1000 ntfs_debug(
"Inode 0x%lx is not in icache.", mft_no);
1004 ntfs_debug(
"Mft record 0x%lx is not a FILE record, write it.",
1009 if (!m->base_mft_record) {
1010 ntfs_debug(
"Mft record 0x%lx is a base record, write it.",
1020 ntfs_debug(
"Mft record 0x%lx is an extent record. Looking for base "
1021 "inode 0x%lx in icache.", mft_no, na.
mft_no);
1034 ntfs_debug(
"Base inode 0x%lx is not in icache, write the "
1035 "extent record.", na.
mft_no);
1052 ntfs_debug(
"Base inode 0x%lx has no attached extent inodes, "
1053 "write the extent record.", na.
mft_no);
1059 if (mft_no == extent_nis[i]->mft_no) {
1064 eni = extent_nis[
i];
1075 ntfs_debug(
"Extent inode 0x%lx is not attached to its base "
1076 "inode 0x%lx, write the extent record.",
1080 ntfs_debug(
"Extent inode 0x%lx is attached to its base inode 0x%lx.",
1092 ntfs_debug(
"Extent mft record 0x%lx is already locked, do "
1093 "not write it.", mft_no);
1096 ntfs_debug(
"Managed to lock extent mft record 0x%lx, write it.",
1098 if (NInoTestClearDirty(eni))
1099 ntfs_debug(
"Extent inode 0x%lx is dirty, marking it clean.",
1109 static const char *
es =
" Leaving inconsistent metadata. Unmount and run "
1131 static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(
ntfs_volume *vol,
1134 s64 pass_end, ll, data_pos, pass_start, ofs,
bit;
1135 unsigned long flags;
1139 unsigned int page_ofs,
size;
1142 ntfs_debug(
"Searching for free mft record in the currently "
1143 "initialized mft bitmap.");
1150 pass_end = NTFS_I(vol->
mft_ino)->allocated_size >>
1154 ll = NTFS_I(vol->
mftbmp_ino)->initialized_size << 3;
1160 data_pos = vol->mft_data_pos;
1162 data_pos = base_ni->
mft_no + 1;
1165 if (data_pos >= pass_end) {
1169 if (data_pos >= pass_end)
1172 pass_start = data_pos;
1173 ntfs_debug(
"Starting bitmap search: pass %u, pass_start 0x%llx, "
1174 "pass_end 0x%llx, data_pos 0x%llx.", pass,
1175 (
long long)pass_start, (
long long)pass_end,
1176 (
long long)data_pos);
1178 for (; pass <= 2;) {
1180 ofs = data_pos >> 3;
1183 ll = ((pass_end + 7) >> 3) - ofs;
1192 page = ntfs_map_page(mftbmp_mapping,
1193 ofs >> PAGE_CACHE_SHIFT);
1196 "bitmap, aborting.");
1197 return PTR_ERR(page);
1202 ntfs_debug(
"Before inner for loop: size 0x%x, "
1203 "data_pos 0x%llx, bit 0x%llx", size,
1204 (
long long)data_pos, (
long long)bit);
1205 for (; bit < size && data_pos + bit < pass_end;
1206 bit &= ~7ull, bit += 8) {
1207 byte = buf + (bit >> 3);
1210 b =
ffz((
unsigned long)*byte);
1211 if (b < 8 && b >= (bit & 7)) {
1212 ll = data_pos + (bit & ~7ull) + b;
1214 ntfs_unmap_page(page);
1220 ntfs_unmap_page(page);
1222 "allocated mft record "
1228 ntfs_debug(
"After inner for loop: size 0x%x, "
1229 "data_pos 0x%llx, bit 0x%llx", size,
1230 (
long long)data_pos, (
long long)bit);
1232 ntfs_unmap_page(page);
1237 if (data_pos < pass_end)
1246 pass_end = pass_start;
1247 data_pos = pass_start = 24;
1248 ntfs_debug(
"pass %i, pass_start 0x%llx, pass_end "
1249 "0x%llx.", pass, (
long long)pass_start,
1250 (
long long)pass_end);
1251 if (data_pos >= pass_end)
1256 ntfs_debug(
"Done. (No free mft records left in currently initialized "
1278 static int ntfs_mft_bitmap_extend_allocation_nolock(
ntfs_volume *vol)
1282 unsigned long flags;
1288 ATTR_RECORD *
a =
NULL;
1298 ntfs_debug(
"Extending mft bitmap allocation.");
1299 mft_ni = NTFS_I(vol->
mft_ino);
1313 ntfs_error(vol->
sb,
"Failed to determine last allocated "
1314 "cluster of mft bitmap attribute.");
1322 ntfs_debug(
"Last lcn of mft bitmap attribute is 0x%llx.",
1330 page = ntfs_map_page(vol->
lcnbmp_ino->i_mapping,
1331 ll >> PAGE_CACHE_SHIFT);
1334 ntfs_error(vol->
sb,
"Failed to read from lcn bitmap.");
1335 return PTR_ERR(page);
1338 tb = 1 << (lcn & 7ull);
1340 if (*b != 0xff && !(*b & tb)) {
1346 ntfs_unmap_page(page);
1350 status.added_cluster = 1;
1351 ntfs_debug(
"Appending one cluster to mft bitmap.");
1354 ntfs_unmap_page(page);
1356 rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
1360 ntfs_error(vol->
sb,
"Failed to allocate a cluster for "
1362 return PTR_ERR(rl2);
1367 ntfs_error(vol->
sb,
"Failed to merge runlists for mft "
1369 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1371 "allocated cluster.%s", es);
1381 for (; rl[1].
length; rl++)
1391 ret = PTR_ERR(mrec);
1404 ntfs_error(vol->
sb,
"Failed to find last attribute extent of "
1405 "mft bitmap attribute.");
1411 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1413 for (rl2 = rl; rl2 > mftbmp_ni->
runlist.
rl; rl2--) {
1420 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1422 ntfs_error(vol->
sb,
"Get size for mapping pairs failed for "
1423 "mft bitmap attribute extent.");
1431 ret = ntfs_attr_record_resize(ctx->
mrec, a, mp_size +
1432 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1436 "record for mft bitmap attribute.");
1444 ntfs_error(vol->
sb,
"Not enough space in this mft record to "
1445 "accommodate extended mft bitmap attribute "
1446 "extent. Cannot handle this yet.");
1452 ret = ntfs_mapping_pairs_build(vol, (
u8*)a +
1453 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1454 mp_size, rl2, ll, -1,
NULL);
1456 ntfs_error(vol->
sb,
"Failed to build mapping pairs array for "
1457 "mft bitmap attribute.");
1461 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1466 if (a->data.non_resident.lowest_vcn) {
1471 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1472 mark_mft_record_dirty(ctx->
ntfs_ino);
1479 "extent of mft bitmap attribute.");
1480 goto restore_undo_alloc;
1486 a->data.non_resident.allocated_size =
1490 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1491 mark_mft_record_dirty(ctx->
ntfs_ino);
1502 ntfs_error(vol->
sb,
"Failed to find last attribute extent of "
1503 "mft bitmap attribute.%s", es);
1518 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
1520 if (
status.added_cluster) {
1524 }
else if (
status.added_run) {
1532 if (ntfs_bitmap_clear_bit(vol->
lcnbmp_ino, lcn)) {
1533 ntfs_error(vol->
sb,
"Failed to free allocated cluster.%s", es);
1539 a->data.non_resident.mapping_pairs_offset),
1541 a->data.non_resident.mapping_pairs_offset),
1542 rl2, ll, -1,
NULL)) {
1547 if (ntfs_attr_record_resize(ctx->
mrec, a, old_alen)) {
1552 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1553 mark_mft_record_dirty(ctx->
ntfs_ino);
1577 static int ntfs_mft_bitmap_extend_initialized_nolock(
ntfs_volume *vol)
1579 s64 old_data_size, old_initialized_size;
1580 unsigned long flags;
1581 struct inode *mftbmp_vi;
1588 ntfs_debug(
"Extending mft bitmap initiailized (and data) size.");
1589 mft_ni = NTFS_I(vol->
mft_ino);
1591 mftbmp_ni = NTFS_I(mftbmp_vi);
1596 return PTR_ERR(mrec);
1607 ntfs_error(vol->
sb,
"Failed to find first attribute extent of "
1608 "mft bitmap attribute.");
1615 old_data_size = i_size_read(mftbmp_vi);
1623 a->data.non_resident.initialized_size =
1627 a->data.non_resident.data_size =
1632 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1633 mark_mft_record_dirty(ctx->
ntfs_ino);
1637 ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
1639 ntfs_debug(
"Done. (Wrote eight initialized bytes to mft "
1647 ntfs_error(vol->
sb,
"Failed to map mft record.%s", es);
1653 ntfs_error(vol->
sb,
"Failed to get search context.%s", es);
1659 ntfs_error(vol->
sb,
"Failed to find first attribute extent of "
1660 "mft bitmap attribute.%s", es);
1671 a->data.non_resident.initialized_size =
1672 cpu_to_sle64(old_initialized_size);
1673 if (i_size_read(mftbmp_vi) != old_data_size) {
1674 i_size_write(mftbmp_vi, old_data_size);
1675 a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
1678 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1679 mark_mft_record_dirty(ctx->
ntfs_ino);
1684 ntfs_debug(
"Restored status of mftbmp: allocated_size 0x%llx, "
1685 "data_size 0x%llx, initialized_size 0x%llx.",
1687 (
long long)i_size_read(mftbmp_vi),
1714 static int ntfs_mft_data_extend_allocation_nolock(
ntfs_volume *vol)
1719 unsigned long flags;
1724 ATTR_RECORD *a =
NULL;
1727 bool mp_rebuilt =
false;
1729 ntfs_debug(
"Extending mft data allocation.");
1730 mft_ni = NTFS_I(vol->
mft_ino);
1744 ntfs_error(vol->
sb,
"Failed to determine last allocated "
1745 "cluster of mft data attribute.");
1753 ntfs_debug(
"Last lcn of mft data attribute is 0x%llx.", (
long long)lcn);
1772 "because the maximum number of inodes "
1773 "(2^32) has already been reached.");
1778 ntfs_debug(
"Trying mft data allocation with %s cluster count %lli.",
1779 nr > min_nr ?
"default" :
"minimal", (
long long)nr);
1780 old_last_vcn = rl[1].
vcn;
1782 rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE,
1784 if (
likely(!IS_ERR(rl2)))
1786 if (PTR_ERR(rl2) != -
ENOSPC || nr == min_nr) {
1788 "number of clusters (%lli) for the "
1789 "mft data attribute.", (
long long)nr);
1791 return PTR_ERR(rl2);
1799 ntfs_debug(
"Retrying mft data allocation with minimal cluster "
1800 "count %lli.", (
long long)nr);
1805 ntfs_error(vol->
sb,
"Failed to merge runlists for mft data "
1807 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1809 "from the mft data attribute.%s", es);
1816 ntfs_debug(
"Allocated %lli clusters.", (
long long)nr);
1818 for (; rl[1].
length; rl++)
1824 ret = PTR_ERR(mrec);
1836 ntfs_error(vol->
sb,
"Failed to find last attribute extent of "
1837 "mft data attribute.");
1843 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1845 for (rl2 = rl; rl2 > mft_ni->
runlist.
rl; rl2--) {
1852 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1854 ntfs_error(vol->
sb,
"Get size for mapping pairs failed for "
1855 "mft data attribute extent.");
1863 ret = ntfs_attr_record_resize(ctx->
mrec, a, mp_size +
1864 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1868 "record for mft data attribute.");
1881 ntfs_error(vol->
sb,
"Not enough space in this mft record to "
1882 "accommodate extended mft data attribute "
1883 "extent. Cannot handle this yet.");
1889 ret = ntfs_mapping_pairs_build(vol, (
u8*)a +
1890 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1891 mp_size, rl2, ll, -1,
NULL);
1893 ntfs_error(vol->
sb,
"Failed to build mapping pairs array of "
1894 "mft data attribute.");
1898 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1905 if (a->data.non_resident.lowest_vcn) {
1910 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1911 mark_mft_record_dirty(ctx->
ntfs_ino);
1918 "extent of mft data attribute.");
1919 goto restore_undo_alloc;
1925 a->data.non_resident.allocated_size =
1929 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1930 mark_mft_record_dirty(ctx->
ntfs_ino);
1940 ntfs_error(vol->
sb,
"Failed to find last attribute extent of "
1941 "mft data attribute.%s", es);
1955 ctx->
attr->data.non_resident.highest_vcn =
1956 cpu_to_sle64(old_last_vcn - 1);
1958 if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
1959 ntfs_error(vol->
sb,
"Failed to free clusters from mft data "
1960 "attribute.%s", es);
1964 if (ntfs_rl_truncate_nolock(vol, &mft_ni->
runlist, old_last_vcn)) {
1965 ntfs_error(vol->
sb,
"Failed to truncate mft data attribute "
1969 if (mp_rebuilt && !IS_ERR(ctx->
mrec)) {
1971 a->data.non_resident.mapping_pairs_offset),
1973 a->data.non_resident.mapping_pairs_offset),
1974 rl2, ll, -1,
NULL)) {
1979 if (ntfs_attr_record_resize(ctx->
mrec, a, old_alen)) {
1984 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1985 mark_mft_record_dirty(ctx->
ntfs_ino);
1986 }
else if (IS_ERR(ctx->
mrec)) {
1987 ntfs_error(vol->
sb,
"Failed to restore attribute search "
2012 static int ntfs_mft_record_layout(
const ntfs_volume *vol,
const s64 mft_no,
2017 ntfs_debug(
"Entering for mft record 0x%llx.", (
long long)mft_no);
2018 if (mft_no >= (1ll << 32)) {
2019 ntfs_error(vol->
sb,
"Mft record number 0x%llx exceeds "
2020 "maximum of 2^32.", (
long long)mft_no);
2027 m->usa_ofs =
cpu_to_le16((
sizeof(MFT_RECORD_OLD) + 1) & ~1);
2029 m->usa_ofs =
cpu_to_le16((
sizeof(MFT_RECORD) + 1) & ~1);
2044 "size. Setting usa_count to 1. If chkdsk "
2045 "reports this as corruption, please email "
2047 "that you saw this message and that the "
2048 "modified filesystem created was corrupt. "
2070 m->base_mft_record = 0;
2071 m->next_attr_instance = 0;
2091 static int ntfs_mft_record_format(
const ntfs_volume *vol,
const s64 mft_no)
2101 ntfs_debug(
"Entering for mft record 0x%llx.", (
long long)mft_no);
2109 i_size = i_size_read(mft_vi);
2111 if (
unlikely(index >= end_index)) {
2114 ntfs_error(vol->
sb,
"Tried to format non-existing mft "
2115 "record 0x%llx.", (
long long)mft_no);
2120 page = ntfs_map_page(mft_vi->
i_mapping, index);
2122 ntfs_error(vol->
sb,
"Failed to map page containing mft record "
2123 "to format 0x%llx.", (
long long)mft_no);
2124 return PTR_ERR(page);
2127 BUG_ON(!PageUptodate(page));
2128 ClearPageUptodate(page);
2130 err = ntfs_mft_record_layout(vol, mft_no, m);
2132 ntfs_error(vol->
sb,
"Failed to layout mft record 0x%llx.",
2134 SetPageUptodate(page);
2136 ntfs_unmap_page(page);
2140 SetPageUptodate(page);
2147 mark_ntfs_record_dirty(page, ofs);
2148 ntfs_unmap_page(page);
2246 s64 ll,
bit, old_data_initialized, old_data_size;
2247 unsigned long flags;
2258 bool record_formatted =
false;
2261 ntfs_debug(
"Entering (allocating an extent mft record for "
2262 "base mft record 0x%llx).",
2263 (
long long)base_ni->
mft_no);
2267 ntfs_debug(
"Entering (allocating a base mft record).");
2276 mft_ni = NTFS_I(vol->
mft_ino);
2279 bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
2281 ntfs_debug(
"Found and allocated free record (#1), bit 0x%llx.",
2283 goto have_alloc_rec;
2287 return ERR_PTR(bit);
2303 if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
2309 ntfs_debug(
"Found free record (#2), bit 0x%llx.",
2311 goto found_free_rec;
2318 bit = old_data_initialized << 3;
2323 ntfs_debug(
"Status of mftbmp before extension: allocated_size 0x%llx, "
2324 "data_size 0x%llx, initialized_size 0x%llx.",
2325 (
long long)old_data_size,
2327 (
long long)old_data_initialized);
2329 if (old_data_initialized + 8 > old_data_size) {
2331 ntfs_debug(
"mftbmp: initialized_size + 8 > allocated_size.");
2332 err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
2339 ntfs_debug(
"Status of mftbmp after allocation extension: "
2340 "allocated_size 0x%llx, data_size 0x%llx, "
2341 "initialized_size 0x%llx.",
2353 err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
2360 ntfs_debug(
"Status of mftbmp after initialized extension: "
2361 "allocated_size 0x%llx, data_size 0x%llx, "
2362 "initialized_size 0x%llx.",
2368 ntfs_debug(
"Found free record (#3), bit 0x%llx.", (
long long)bit);
2372 err = ntfs_bitmap_set_bit(vol->
mftbmp_ino, bit);
2374 ntfs_error(vol->
sb,
"Failed to allocate bit in mft bitmap.");
2378 ntfs_debug(
"Set bit 0x%llx in mft bitmap.", (
long long)bit);
2392 if (ll <= old_data_initialized) {
2393 ntfs_debug(
"Allocated mft record already initialized.");
2394 goto mft_rec_already_initialized;
2396 ntfs_debug(
"Initializing allocated mft record.");
2404 ntfs_debug(
"Status of mft data before extension: "
2405 "allocated_size 0x%llx, data_size 0x%llx, "
2406 "initialized_size 0x%llx.",
2408 (
long long)i_size_read(vol->
mft_ino),
2412 err = ntfs_mft_data_extend_allocation_nolock(vol);
2416 goto undo_mftbmp_alloc_nolock;
2419 ntfs_debug(
"Status of mft data after allocation extension: "
2420 "allocated_size 0x%llx, data_size 0x%llx, "
2421 "initialized_size 0x%llx.",
2423 (
long long)i_size_read(vol->
mft_ino),
2436 old_data_size = vol->
mft_ino->i_size;
2438 s64 new_initialized_size, mft_no;
2443 if (new_initialized_size > i_size_read(vol->
mft_ino))
2444 i_size_write(vol->
mft_ino, new_initialized_size);
2446 ntfs_debug(
"Initializing mft record 0x%llx.",
2448 err = ntfs_mft_record_format(vol, mft_no);
2451 goto undo_data_init;
2457 record_formatted =
true;
2463 goto undo_data_init;
2470 goto undo_data_init;
2475 ntfs_error(vol->
sb,
"Failed to find first attribute extent of "
2476 "mft data attribute.");
2479 goto undo_data_init;
2483 a->data.non_resident.initialized_size =
2485 a->data.non_resident.data_size =
2486 cpu_to_sle64(i_size_read(vol->
mft_ino));
2489 flush_dcache_mft_record_page(ctx->
ntfs_ino);
2490 mark_mft_record_dirty(ctx->
ntfs_ino);
2494 ntfs_debug(
"Status of mft data after mft record initialization: "
2495 "allocated_size 0x%llx, data_size 0x%llx, "
2496 "initialized_size 0x%llx.",
2498 (
long long)i_size_read(vol->
mft_ino),
2503 mft_rec_already_initialized:
2521 page = ntfs_map_page(vol->
mft_ino->i_mapping, index);
2523 ntfs_error(vol->
sb,
"Failed to map page containing allocated "
2524 "mft record 0x%llx.", (
long long)bit);
2525 err = PTR_ERR(page);
2526 goto undo_mftbmp_alloc;
2529 BUG_ON(!PageUptodate(page));
2530 ClearPageUptodate(page);
2533 if (!record_formatted) {
2538 "free in mft bitmap but is marked "
2539 "used itself. Corrupt filesystem. "
2540 "Unmount and run chkdsk.",
2543 SetPageUptodate(page);
2545 ntfs_unmap_page(page);
2547 goto undo_mftbmp_alloc;
2556 seq_no = m->sequence_number;
2558 err = ntfs_mft_record_layout(vol, bit, m);
2561 "record 0x%llx.", (
long long)bit);
2562 SetPageUptodate(page);
2564 ntfs_unmap_page(page);
2565 goto undo_mftbmp_alloc;
2568 m->sequence_number =
seq_no;
2577 SetPageUptodate(page);
2594 if (IS_ERR(m_tmp)) {
2596 "mft record 0x%llx.", (
long long)bit);
2597 err = PTR_ERR(m_tmp);
2603 mark_ntfs_record_dirty(page, ofs);
2605 ntfs_unmap_page(page);
2606 goto undo_mftbmp_alloc;
2616 mark_ntfs_record_dirty(page, ofs);
2622 ntfs_unmap_page(page);
2637 mark_ntfs_record_dirty(page, ofs);
2639 ntfs_unmap_page(page);
2640 goto undo_mftbmp_alloc;
2655 ntfs_init_big_inode(vi);
2665 NInoSetMstProtected(ni);
2671 ni->
itype.
index.block_size_bits = ntfs_ffs(4096) - 1;
2723 mark_ntfs_record_dirty(page, ofs);
2727 insert_inode_hash(vi);
2730 vol->mft_data_pos = bit + 1;
2736 ntfs_debug(
"Returning opened, allocated %sinode 0x%llx.",
2737 base_ni ?
"extent " :
"", (
long long)bit);
2743 i_size_write(vol->
mft_ino, old_data_size);
2745 goto undo_mftbmp_alloc_nolock;
2748 undo_mftbmp_alloc_nolock:
2749 if (ntfs_bitmap_clear_bit(vol->
mftbmp_ino, bit)) {
2750 ntfs_error(vol->
sb,
"Failed to clear bit in mft bitmap.%s", es);
2755 return ERR_PTR(err);
2757 ntfs_warning(vol->
sb,
"Cannot allocate mft record because the maximum "
2758 "number of inodes (2^32) has already been reached.");
2784 int ntfs_extent_mft_record_free(
ntfs_inode *ni, MFT_RECORD *m)
2786 unsigned long mft_no = ni->
mft_no;
2803 ntfs_debug(
"Entering for extent inode 0x%lx, base inode 0x%lx.\n",
2804 mft_no, base_ni->
mft_no);
2810 ntfs_error(vol->
sb,
"Tried to free busy extent inode 0x%lx, "
2811 "not freeing.", base_ni->
mft_no);
2820 if (ni != extent_nis[i])
2833 ntfs_error(vol->
sb,
"Extent inode 0x%lx is not attached to "
2834 "its base inode 0x%lx.", mft_no,
2848 old_seq_no = m->sequence_number;
2850 if (seq_no == 0xffff)
2862 err = write_mft_record(ni, m, 0);
2864 ntfs_error(vol->
sb,
"Failed to write mft record 0x%lx, not "
2865 "freeing.", mft_no);
2870 unmap_extent_mft_record(ni);
2875 err = ntfs_bitmap_clear_bit(vol->
mftbmp_ino, mft_no);
2883 ntfs_error(vol->
sb,
"Failed to clear bit in mft bitmap.%s", es);
2897 "buffer during rollback.%s", es);
2900 goto rollback_error;
2911 m->sequence_number = old_seq_no;
2914 mark_mft_record_dirty(ni);