24 #include <linux/sched.h>
25 #include <linux/slab.h>
94 bool ctx_is_temporary, ctx_needs_reset;
97 ntfs_debug(
"Mapping runlist part containing vcn 0x%llx.",
98 (
unsigned long long)vcn);
104 ctx_is_temporary = ctx_needs_reset =
true;
114 VCN allocated_size_vcn;
119 ctx_is_temporary =
false;
120 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
125 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
126 end_vcn = allocated_size_vcn - 1;
134 if (vcn >= allocated_size_vcn || (a->type == ni->
type &&
138 sle64_to_cpu(a->data.non_resident.lowest_vcn)
139 <= vcn && end_vcn >= vcn))
140 ctx_needs_reset =
false;
162 ctx_needs_reset =
true;
165 if (ctx_needs_reset) {
182 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
183 if (
unlikely(vcn && vcn >= end_vcn)) {
193 if (ctx_is_temporary) {
197 }
else if (ctx_needs_reset) {
205 if (NInoAttrList(base_ni)) {
218 unmap_extent_mft_record(ctx->
ntfs_ino);
242 if (IS_ERR(ctx->
mrec)) {
243 if (PTR_ERR(ctx->
mrec) ==
256 if (!IS_ERR(ctx->
mrec))
257 old_ctx.
attr = (ATTR_RECORD*)(
342 const bool write_locked)
346 bool is_retry =
false;
349 ntfs_debug(
"Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
350 ni->
mft_no, (
unsigned long long)vcn,
351 write_locked ?
"write" :
"read");
352 BUG_ON(!NInoNonResident(ni));
366 ntfs_debug(
"Done, lcn 0x%llx.", (
long long)lcn);
372 }
else if (!is_retry) {
470 bool is_retry =
false;
473 ntfs_debug(
"Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
474 ni->
mft_no, (
unsigned long long)vcn, ctx ?
"" :
"out");
475 BUG_ON(!NInoNonResident(ni));
487 if (
likely(rl && vcn >= rl[0].vcn)) {
505 if (!err && !is_retry) {
510 if (IS_ERR(ctx->
mrec))
511 err = PTR_ERR(ctx->
mrec);
606 a = (ATTR_RECORD*)((
u8*)ctx->
attr +
608 for (;; a = (ATTR_RECORD*)((
u8*)a +
le32_to_cpu(a->length))) {
630 a->name_length, ic, upcase, upcase_len)) {
669 a->data.resident.value_offset),
671 a->data.resident.value_length)));
680 a->data.resident.value_length);
717 struct buffer_head *bh;
720 unsigned long block, max_block;
722 unsigned char block_size_bits;
725 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
726 initialized_size > size)
728 if (!initialized_size) {
738 ntfs_error(sb,
"Cannot read attribute list since runlist is "
745 ntfs_debug(
"Reading vcn = 0x%llx, lcn = 0x%llx.",
746 (
unsigned long long)rl->
vcn,
747 (
unsigned long long)lcn);
750 ntfs_error(sb,
"ntfs_rl_vcn_to_lcn() failed. Cannot "
751 "read attribute list.");
761 bh = sb_bread(sb, block);
764 "read attribute list.");
767 if (al + block_size >= al_end)
769 memcpy(al, bh->b_data, block_size);
772 }
while (++block < max_block);
775 if (initialized_size < size) {
777 memset(al_start + initialized_size, 0, size - initialized_size);
792 memcpy(al, bh->b_data, al_end - al);
794 if (initialized_size < size)
800 ntfs_error(sb,
"Attribute list buffer overflow. Read attribute list "
857 static int ntfs_external_attr_find(
const ATTR_TYPE type,
864 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
865 u8 *al_start, *al_end;
870 static const char *
es =
" Unmount and run chkdsk.";
888 ctx->
al_entry = (ATTR_LIST_ENTRY*)al_start;
897 al_entry = (ATTR_LIST_ENTRY*)((
u8*)ctx->
al_entry +
899 for (;; al_entry = next_al_entry) {
902 (
u8*)al_entry > al_end)
906 if ((
u8*)al_entry == al_end)
908 if (!al_entry->length)
910 if ((
u8*)al_entry + 6 > al_end || (
u8*)al_entry +
913 next_al_entry = (ATTR_LIST_ENTRY*)((
u8*)al_entry +
917 if (type != al_entry->type)
923 al_name_len = al_entry->name_length;
924 al_name = (
ntfschar*)((
u8*)al_entry + al_entry->name_offset);
966 if (lowest_vcn && (
u8*)next_al_entry >= al_start &&
967 (
u8*)next_al_entry + 6 < al_end &&
969 next_al_entry->length) <= al_end &&
970 sle64_to_cpu(next_al_entry->lowest_vcn) <=
972 next_al_entry->type == al_entry->type &&
973 next_al_entry->name_length == al_name_len &&
976 next_al_entry->name_offset),
977 next_al_entry->name_length,
984 "reference in attribute list "
985 "of base inode 0x%lx.%s",
993 unmap_extent_mft_record(ni);
995 if (
MREF_LE(al_entry->mft_reference) ==
1003 al_entry->mft_reference), &ni);
1004 if (IS_ERR(ctx->
mrec)) {
1006 "extent mft record "
1007 "0x%lx of base inode "
1012 err = PTR_ERR(ctx->
mrec);
1052 if (al_entry->instance != a->instance)
1059 if (al_entry->type != a->type)
1072 a->data.resident.value_length) == val_len &&
1082 goto do_next_attr_loop;
1085 ntfs_error(vol->
sb,
"Base inode 0x%lx contains corrupt "
1086 "attribute list attribute.%s", base_ni->
mft_no,
1090 if (ni != base_ni) {
1092 unmap_extent_mft_record(ni);
1107 return ntfs_attr_find(
AT_END, name, name_len, ic, val, val_len,
1124 unmap_extent_mft_record(ni);
1141 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1189 const VCN lowest_vcn,
const u8 *val,
const u32 val_len,
1203 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1205 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1223 .attr = (ATTR_RECORD*)((
u8*)mrec +
1256 unmap_extent_mft_record(ctx->
ntfs_ino);
1275 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1289 unmap_extent_mft_record(ctx->
ntfs_ino);
1319 if (
likely(ad->type == type))
1325 ntfs_debug(
"Attribute type 0x%x not found in $AttrDef.",
1355 ad = ntfs_attr_find_in_attrdef(vol, type);
1359 if (((sle64_to_cpu(ad->min_size) > 0) &&
1360 size < sle64_to_cpu(ad->min_size)) ||
1361 ((sle64_to_cpu(ad->max_size) > 0) && size >
1362 sle64_to_cpu(ad->max_size)))
1383 ad = ntfs_attr_find_in_attrdef(vol, type);
1435 int ntfs_attr_record_resize(MFT_RECORD *
m, ATTR_RECORD *a,
u32 new_size)
1437 ntfs_debug(
"Entering for new_size %u.", new_size);
1440 new_size = (new_size + 7) & ~7;
1479 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1485 if (ntfs_attr_record_resize(m, a,
1486 le16_to_cpu(a->data.resident.value_offset) + new_size))
1492 old_size =
le32_to_cpu(a->data.resident.value_length);
1493 if (new_size > old_size)
1495 old_size, 0, new_size - old_size);
1497 a->data.resident.value_length =
cpu_to_le32(new_size);
1538 struct inode *vi = VFS_I(ni);
1547 unsigned long flags;
1548 int mp_size, mp_ofs, name_ofs, arec_size,
err, err2;
1550 u8 old_res_attr_flags;
1553 err = ntfs_attr_can_be_non_resident(vol, ni->
type);
1559 ntfs_debug(
"Attribute not defined on the NTFS "
1567 BUG_ON(NInoCompressed(ni));
1568 BUG_ON(NInoEncrypted(ni));
1585 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1589 ntfs_debug(
"Failed to allocate cluster%s, error code "
1600 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1603 ntfs_debug(
"Failed to get size for mapping pairs array, error "
1633 BUG_ON(NInoNonResident(ni));
1638 if (NInoSparse(ni) || NInoCompressed(ni))
1640 data.non_resident.compressed_size) +
1641 sizeof(a->data.non_resident.compressed_size) +
1645 data.non_resident.compressed_size) + 7) & ~7;
1646 mp_ofs = (name_ofs + a->name_length *
sizeof(
ntfschar) + 7) & ~7;
1651 arec_size = (mp_ofs + mp_size + 7) & ~7;
1656 attr_size =
le32_to_cpu(a->data.resident.value_length);
1657 BUG_ON(attr_size != data_size);
1658 if (page && !PageUptodate(page)) {
1666 SetPageUptodate(page);
1669 old_res_attr_flags = a->data.resident.flags;
1671 err = ntfs_attr_record_resize(m, a, arec_size);
1678 a->non_resident = 1;
1682 a->name_length *
sizeof(
ntfschar));
1685 a->data.non_resident.lowest_vcn = 0;
1686 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1688 a->data.non_resident.mapping_pairs_offset =
cpu_to_le16(mp_ofs);
1689 memset(&a->data.non_resident.reserved, 0,
1690 sizeof(a->data.non_resident.reserved));
1691 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1692 a->data.non_resident.data_size =
1693 a->data.non_resident.initialized_size =
1694 cpu_to_sle64(attr_size);
1695 if (NInoSparse(ni) || NInoCompressed(ni)) {
1696 a->data.non_resident.compression_unit = 0;
1697 if (NInoCompressed(ni) || vol->
major_ver < 3)
1698 a->data.non_resident.compression_unit = 4;
1699 a->data.non_resident.compressed_size =
1700 a->data.non_resident.allocated_size;
1702 a->data.non_resident.compression_unit = 0;
1704 err = ntfs_mapping_pairs_build(vol, (
u8*)a + mp_ofs,
1705 arec_size - mp_ofs, rl, 0, -1,
NULL);
1707 ntfs_debug(
"Failed to build mapping pairs, error code %i.",
1715 if (NInoSparse(ni) || NInoCompressed(ni)) {
1717 if (a->data.non_resident.compression_unit) {
1719 non_resident.compression_unit +
1725 a->data.non_resident.compression_unit;
1741 NInoSetNonResident(ni);
1743 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1744 mark_mft_record_dirty(ctx->
ntfs_ino);
1758 a->non_resident = 0;
1760 name_ofs = (
offsetof(ATTR_RECORD,
data.resident.reserved) +
1761 sizeof(a->data.resident.reserved) + 7) & ~7;
1764 a->name_length *
sizeof(
ntfschar));
1765 mp_ofs = (name_ofs + a->name_length *
sizeof(
ntfschar) + 7) & ~7;
1767 arec_size = (mp_ofs + attr_size + 7) & ~7;
1769 err2 = ntfs_attr_record_resize(m, a, arec_size);
1780 if ((mp_ofs + attr_size) > arec_size) {
1782 attr_size = arec_size - mp_ofs;
1784 "to non-resident attribute "
1785 "conversion. Truncating inode 0x%lx, "
1786 "attribute type 0x%x from %i bytes to "
1787 "%i bytes to maintain metadata "
1788 "consistency. THIS MEANS YOU ARE "
1789 "LOSING %i BYTES DATA FROM THIS %s.",
1792 err2, attr_size, err2 - attr_size,
1794 !ni->
name_len) ?
"FILE":
"ATTRIBUTE");
1797 i_size_write(vi, attr_size);
1802 a->data.resident.value_length =
cpu_to_le32(attr_size);
1803 a->data.resident.value_offset =
cpu_to_le16(mp_ofs);
1804 a->data.resident.flags = old_res_attr_flags;
1805 memset(&a->data.resident.reserved, 0,
1806 sizeof(a->data.resident.reserved));
1810 memcpy((
u8*)a + mp_ofs, kaddr, attr_size);
1818 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1819 mark_mft_record_dirty(ctx->
ntfs_ino);
1829 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1831 "cluster(s) in error code path. Run "
1832 "chkdsk to recover the lost "
1906 const s64 new_data_size,
const s64 data_start)
1910 struct inode *vi = VFS_I(ni);
1917 unsigned long flags;
1926 ntfs_debug(
"Entering for i_ino 0x%lx, attribute type 0x%x, "
1927 "old_allocated_size 0x%llx, "
1928 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1929 "data_start 0x%llx.", vi->
i_ino,
1931 (
unsigned long long)allocated_size,
1932 (
unsigned long long)new_alloc_size,
1933 (
unsigned long long)new_data_size,
1934 (
unsigned long long)start);
1941 if (NInoNonResident(ni)) {
1944 new_alloc_size = (new_alloc_size + vol->
cluster_size - 1) &
1947 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1949 err = ntfs_attr_size_bounds_check(vol, ni->
type, new_alloc_size);
1955 if (start < 0 || start >= allocated_size) {
1958 "of inode 0x%lx, attribute "
1959 "type 0x%x, because the new "
1960 "allocation would exceed the "
1961 "maximum allowed size for "
1962 "this attribute type.",
1963 vi->
i_ino, (
unsigned)
1967 "of inode 0x%lx, attribute "
1968 "type 0x%x, because this "
1969 "attribute type is not "
1970 "defined on the NTFS volume. "
1971 "Possible corruption! You "
1972 "should run chkdsk!",
1973 vi->
i_ino, (
unsigned)
2020 if (
unlikely(new_alloc_size <= allocated_size)) {
2021 ntfs_debug(
"Allocated size already exceeds requested size.");
2023 if (new_data_size < 0)
2041 if (a->non_resident)
2042 goto do_non_resident_extend;
2043 BUG_ON(NInoNonResident(ni));
2045 attr_len =
le32_to_cpu(a->data.resident.value_length);
2051 if (new_alloc_size < vol->mft_record_size &&
2052 !ntfs_attr_record_resize(m, a,
2060 if (new_data_size >= 0) {
2061 BUG_ON(new_data_size < attr_len);
2062 a->data.resident.value_length =
2082 err = ntfs_attr_make_non_resident(ni, attr_len);
2095 if (start < 0 || start >= allocated_size)
2097 "inode 0x%lx, attribute type 0x%x, "
2098 "because the conversion from resident "
2099 "to non-resident attribute failed "
2100 "with error code %i.", vi->
i_ino,
2110 if (start < 0 || start >= allocated_size) {
2113 "record/on disk for the non-resident "
2114 "attribute value. This case is not "
2115 "implemented yet.");
2118 "non-resident. This case is not "
2119 "implemented yet.");
2126 goto do_resident_extend;
2139 goto do_resident_extend;
2147 goto do_resident_extend;
2151 do_non_resident_extend:
2152 BUG_ON(!NInoNonResident(ni));
2153 if (new_alloc_size == allocated_size) {
2165 if ((start >= 0 && start <= allocated_size) || ni->
type !=
AT_DATA ||
2166 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2170 ntfs_debug(
"Inserting holes is not-implemented yet. Falling back to "
2171 "allocating real clusters instead.");
2183 if (!rl && !allocated_size)
2188 if (start < 0 || start >= allocated_size)
2190 "of inode 0x%lx, attribute "
2191 "type 0x%x, because the "
2192 "mapping of a runlist "
2193 "fragment failed with error "
2194 "code %i.", vi->
i_ino,
2221 (new_alloc_size - allocated_size) >>
2223 rl->
lcn + rl->
length : -1, DATA_ZONE,
true);
2226 if (start < 0 || start >= allocated_size)
2228 "inode 0x%lx, attribute type 0x%x, "
2229 "because the allocation of clusters "
2230 "failed with error code %i.", vi->
i_ino,
2239 if (start < 0 || start >= allocated_size)
2241 "inode 0x%lx, attribute type 0x%x, "
2242 "because the runlist merge failed "
2243 "with error code %i.", vi->
i_ino,
2247 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2249 "cluster(s) in error code path. Run "
2250 "chkdsk to recover the lost "
2258 ntfs_debug(
"Allocated 0x%llx clusters.", (
long long)(new_alloc_size -
2261 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2262 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2268 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2271 if (start < 0 || start >= allocated_size)
2273 "inode 0x%lx, attribute type 0x%x, "
2274 "because determining the size for the "
2275 "mapping pairs failed with error code "
2283 err = ntfs_attr_record_resize(m, a, mp_size +
2284 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2293 if (start < 0 || start >= allocated_size)
2295 "record for the extended attribute "
2296 "record. This case is not "
2297 "implemented yet.");
2303 err = ntfs_mapping_pairs_build(vol, (
u8*)a +
2304 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2305 mp_size, rl2, ll, -1,
NULL);
2307 if (start < 0 || start >= allocated_size)
2309 "inode 0x%lx, attribute type 0x%x, "
2310 "because building the mapping pairs "
2311 "failed with error code %i.", vi->
i_ino,
2317 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2323 if (a->data.non_resident.lowest_vcn) {
2328 flush_dcache_mft_record_page(ctx->
ntfs_ino);
2329 mark_mft_record_dirty(ctx->
ntfs_ino);
2334 goto restore_undo_alloc;
2340 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2351 if (NInoSparse(ni) || NInoCompressed(ni)) {
2353 a->data.non_resident.compressed_size =
2357 vi->
i_blocks = new_alloc_size >> 9;
2360 if (new_data_size >= 0) {
2362 sle64_to_cpu(a->data.non_resident.data_size));
2363 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2367 flush_dcache_mft_record_page(ctx->
ntfs_ino);
2368 mark_mft_record_dirty(ctx->
ntfs_ino);
2373 ntfs_debug(
"Done, new_allocated_size 0x%llx.",
2374 (
unsigned long long)new_alloc_size);
2375 return new_alloc_size;
2377 if (start < 0 || start >= allocated_size)
2378 ntfs_error(vol->
sb,
"Cannot complete extension of allocation "
2379 "of inode 0x%lx, attribute type 0x%x, because "
2380 "lookup of first attribute extent failed with "
2381 "error code %i.", vi->
i_ino,
2389 ntfs_error(vol->
sb,
"Failed to find last attribute extent of "
2390 "attribute in error code path. Run chkdsk to "
2399 if (NInoSparse(ni) || NInoCompressed(ni)) {
2404 vi->
i_blocks = new_alloc_size >> 9;
2416 ctx->
attr->data.non_resident.highest_vcn = cpu_to_sle64(
2420 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2421 ntfs_error(vol->
sb,
"Failed to release allocated cluster(s) "
2422 "in error code path. Run chkdsk to recover "
2423 "the lost cluster(s).");
2434 if (ntfs_rl_truncate_nolock(vol, &ni->
runlist, ll) || IS_ERR(m)) {
2435 ntfs_error(vol->
sb,
"Failed to %s in error code path. Run "
2436 "chkdsk to recover.", IS_ERR(m) ?
2437 "restore attribute search context" :
2438 "truncate attribute runlist");
2440 }
else if (mp_rebuilt) {
2441 if (ntfs_attr_record_resize(m, a, attr_len)) {
2443 "record in error code path. Run "
2444 "chkdsk to recover.");
2448 a->data.non_resident.
2454 "mapping pairs array in error "
2455 "code path. Run chkdsk to "
2459 flush_dcache_mft_record_page(ctx->
ntfs_ino);
2460 mark_mft_record_dirty(ctx->
ntfs_ino);
2470 ntfs_debug(
"Failed. Returning error code %i.", err);
2501 unsigned start_ofs, end_ofs,
size;
2503 ntfs_debug(
"Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2504 (
long long)ofs, (
long long)cnt, val);
2513 BUG_ON(NInoCompressed(ni));
2514 BUG_ON(NInoEncrypted(ni));
2515 mapping = VFS_I(ni)->i_mapping;
2523 if (
unlikely(end > i_size_read(VFS_I(ni)))) {
2524 ntfs_error(vol->
sb,
"Request exceeds end of attribute.");
2530 page = read_mapping_page(mapping, idx,
NULL);
2533 "page (error, index 0x%lx).", idx);
2534 return PTR_ERR(page);
2544 memset(kaddr + start_ofs, val, size - start_ofs);
2549 balance_dirty_pages_ratelimited(mapping);
2556 for (; idx <
end; idx++) {
2558 page = grab_cache_page(mapping, idx);
2561 "page (index 0x%lx).", idx);
2572 if (page_has_buffers(page)) {
2573 struct buffer_head *bh, *
head;
2575 bh = head = page_buffers(page);
2577 set_buffer_uptodate(bh);
2578 }
while ((bh = bh->b_this_page) != head);
2581 SetPageUptodate(page);
2590 balance_dirty_pages_ratelimited(mapping);
2595 page = read_mapping_page(mapping, idx,
NULL);
2597 ntfs_error(vol->
sb,
"Failed to read last partial page "
2598 "(error, index 0x%lx).", idx);
2599 return PTR_ERR(page);
2602 memset(kaddr, val, end_ofs);
2607 balance_dirty_pages_ratelimited(mapping);