Lines Matching refs:na

86 static int NAttrFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag)
88 if (na->type == AT_DATA && na->name == AT_UNNAMED)
89 return (na->ni->flags & flag);
93 static void NAttrSetFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag)
95 if (na->type == AT_DATA && na->name == AT_UNNAMED)
96 na->ni->flags |= flag;
102 static void NAttrClearFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag)
104 if (na->type == AT_DATA && na->name == AT_UNNAMED)
105 na->ni->flags &= ~flag;
109 int NAttr##func_name(ntfs_attr *na) { return NAttrFlag (na, flag); } \
110 void NAttrSet##func_name(ntfs_attr *na) { NAttrSetFlag (na, flag); } \
111 void NAttrClear##func_name(ntfs_attr *na){ NAttrClearFlag(na, flag); }
325 * @na: ntfs attribute to initialize
331 * Initialize the ntfs attribute @na with @ni, @type, @name, and @name_len.
333 static void __ntfs_attr_init(ntfs_attr *na, ntfs_inode *ni,
336 na->rl = NULL;
337 na->ni = ni;
338 na->type = type;
339 na->name = name;
341 na->name_len = name_len;
343 na->name_len = 0;
348 * @na:
361 void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident,
368 if (!NAttrInitialized(na)) {
369 na->data_flags = data_flags;
371 NAttrSetNonResident(na);
373 NAttrSetCompressed(na);
375 NAttrSetEncrypted(na);
377 NAttrSetSparse(na);
378 na->allocated_size = allocated_size;
379 na->data_size = data_size;
380 na->initialized_size = initialized_size;
382 ntfs_volume *vol = na->ni->vol;
384 na->compressed_size = compressed_size;
385 na->compression_block_clusters = 1 << compression_unit;
386 na->compression_block_size = 1 << (compression_unit +
388 na->compression_block_size_bits = ffs(
389 na->compression_block_size) - 1;
391 NAttrSetInitialized(na);
414 ntfs_attr *na = NULL;
426 na = ntfs_calloc(sizeof(ntfs_attr));
427 if (!na)
478 __ntfs_attr_init(na, ni, type, name, name_len);
512 if (na->type == AT_DATA && na->name == AT_UNNAMED &&
513 (((a->flags & ATTR_IS_SPARSE) && !NAttrSparse(na)) ||
514 (!(a->flags & ATTR_IS_ENCRYPTED) != !NAttrEncrypted(na)))) {
518 le16_to_cpu(a->flags), le32_to_cpu(na->ni->flags));
553 ntfs_attr_init(na, TRUE, a->flags,
563 ntfs_attr_init(na, FALSE, a->flags,
571 return na;
577 free(na);
578 na = NULL;
584 * @na: ntfs attribute structure to free
586 * Release all memory associated with the ntfs attribute @na and then release
587 * @na itself.
589 void ntfs_attr_close(ntfs_attr *na)
591 if (!na)
593 if (NAttrNonResident(na) && na->rl)
594 free(na->rl);
596 if (na->name != AT_UNNAMED && na->name != NTFS_INDEX_I30
597 && na->name != STREAM_SDS)
598 free(na->name);
599 free(na);
604 * @na: ntfs attribute for which to map (part of) a runlist
607 * Map the part of a runlist containing the @vcn of the ntfs attribute @na.
611 int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn)
617 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), (long long)vcn);
619 lcn = ntfs_rl_vcn_to_lcn(na->rl, vcn);
623 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
628 if (!ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE,
633 rl = ntfs_mapping_pairs_decompress(na->ni->vol, ctx->attr,
634 na->rl);
636 na->rl = rl;
655 static int ntfs_attr_map_partial_runlist(ntfs_attr *na, VCN vcn)
667 if (NAttrFullyMapped(na))
670 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
675 last_vcn = na->allocated_size >> na->ni->vol->cluster_size_bits;
685 if (!ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE,
690 if (ntfs_rl_vcn_to_lcn(na->rl, needed)
692 rl = ntfs_mapping_pairs_decompress(na->ni->vol,
693 a, na->rl);
696 rl = na->rl;
698 na->rl = rl;
732 NAttrSetFullyMapped(na);
740 * @na: ntfs attribute for which to map the runlist
742 * Map the whole runlist of the ntfs attribute @na. For an attribute made up
744 * ntfs_attr_map_runlist(na, 0) but for an attribute with multiple extents this
750 int ntfs_attr_map_whole_runlist(ntfs_attr *na)
754 ntfs_volume *vol = na->ni->vol;
760 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type));
763 if (NAttrFullyMapped(na)) {
767 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
778 if (ntfs_rl_vcn_to_lcn(na->rl, next_vcn) == LCN_RL_NOT_MAPPED)
781 if (ntfs_attr_lookup(na->type, na->name, na->name_len,
789 rl = ntfs_mapping_pairs_decompress(na->ni->vol,
790 a, na->rl);
793 na->rl = rl;
802 (unsigned long long)na->ni->mft_no);
824 (unsigned long long)na->ni->mft_no);
842 (unsigned long long)na->ni->mft_no,
847 NAttrSetFullyMapped(na);
859 * @na: ntfs attribute whose runlist to use for conversion
863 * cluster number (lcn) of a device using the runlist @na->rl to map vcns to
878 LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn)
883 if (!na || !NAttrNonResident(na) || vcn < 0)
887 long)na->ni->mft_no, le32_to_cpu(na->type));
890 lcn = ntfs_rl_vcn_to_lcn(na->rl, vcn);
893 if (!is_retry && !ntfs_attr_map_runlist(na, vcn)) {
910 * @na: ntfs attribute whose runlist to search
914 * @na and return the the address of the runlist element containing the @vcn.
928 runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn)
933 if (!na || !NAttrNonResident(na) || vcn < 0) {
939 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
942 rl = na->rl;
971 if (!is_retry && !ntfs_attr_map_runlist(na, vcn)) {
989 static s64 ntfs_attr_pread_i(ntfs_attr *na, const s64 pos, s64 count, void *b)
998 if ((na->data_flags & ATTR_COMPRESSION_MASK) && NAttrNonResident(na)) {
999 if ((na->data_flags & ATTR_COMPRESSION_MASK)
1001 return ntfs_compressed_attr_pread(na, pos, count, b);
1013 vol = na->ni->vol;
1014 if (!vol->efs_raw && NAttrEncrypted(na) && NAttrNonResident(na)) {
1026 max_read = na->data_size;
1027 max_init = na->initialized_size;
1028 if (na->ni->vol->efs_raw
1029 && (na->data_flags & ATTR_IS_ENCRYPTED)
1030 && NAttrNonResident(na)) {
1031 if (na->data_size != na->initialized_size) {
1036 max_init = max_read = ((na->data_size + 511) & ~511) + 2;
1044 if (!NAttrNonResident(na)) {
1048 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
1051 if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0,
1086 if (na->ni->vol->efs_raw &&
1087 (na->data_flags & ATTR_IS_ENCRYPTED) &&
1089 efs_padding_length = 511 - ((na->data_size - 1) & 511);
1108 rl = ntfs_attr_find_vcn(na, pos >> vol->cluster_size_bits);
1129 rl = ntfs_attr_find_vcn(na, rl->vcn);
1201 * @na: ntfs attribute to read from
1207 * attribute @na into the data buffer @b.
1218 s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count, void *b)
1222 if (!na || !na->ni || !na->ni->vol || !b || pos < 0 || count < 0) {
1224 ntfs_log_perror("%s: na=%p b=%p pos=%lld count=%lld",
1225 __FUNCTION__, na, b, (long long)pos,
1231 "%lld\n", (unsigned long long)na->ni->mft_no,
1232 le32_to_cpu(na->type), (long long)pos, (long long)count);
1234 ret = ntfs_attr_pread_i(na, pos, count, b);
1240 static int ntfs_attr_fill_zero(ntfs_attr *na, s64 pos, s64 count)
1252 if (!na || pos < 0 || count < 0) {
1261 rli = na->rl;
1263 vol = na->ni->vol;
1301 static int ntfs_attr_fill_hole(ntfs_attr *na, s64 count, s64 *ofs,
1306 ntfs_volume *vol = na->ni->vol;
1312 if (na->ni->mft_no == FILE_Bitmap) {
1334 if (!na->rl) {
1335 if (ntfs_attr_map_whole_runlist(na))
1340 if (ntfs_attr_map_partial_runlist(na,
1346 if (ntfs_attr_map_whole_runlist(na))
1351 *rl = ntfs_attr_find_vcn(na, cur_vcn);
1370 if (na->data_flags & ATTR_COMPRESSION_MASK)
1393 if ((na->data_flags & ATTR_COMPRESSION_MASK)
1394 && (need < na->compression_block_clusters)) {
1404 if ((from_vcn & -na->compression_block_clusters) <= (*rl)->vcn)
1407 alloc_vcn = from_vcn & -na->compression_block_clusters;
1408 need = (alloc_vcn | (na->compression_block_clusters - 1))
1425 if (na->data_flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE))
1426 na->compressed_size += need << vol->cluster_size_bits;
1428 *rl = ntfs_runlists_merge(na->rl, rlc);
1429 NAttrSetRunlistDirty(na);
1434 if (*rl && (na->data_flags & ATTR_COMPRESSION_MASK)) {
1435 runlist_element *oldrl = na->rl;
1436 na->rl = *rl;
1437 *rl = ntfs_rl_extend(na,*rl,2);
1438 if (!*rl) na->rl = oldrl; /* restore to original if failed */
1450 na->unused_runs = 2;
1451 na->rl = *rl;
1454 *rl = ntfs_attr_find_vcn(na, cur_vcn);
1477 if (ntfs_attr_fill_zero(na, cur_vcn << vol->cluster_size_bits,
1500 static int stuff_hole(ntfs_attr *na, const s64 pos);
1517 static int split_compressed_hole(ntfs_attr *na, runlist_element **prl,
1521 int cluster_size_bits = na->ni->vol->cluster_size_bits;
1525 = na->compression_block_clusters;
1527 if (rl->length > na->compression_block_clusters) {
1528 *prl = ntfs_rl_extend(na,*prl,2);
1533 na->unused_runs = 2;
1536 if (*prl && (rl->length > na->compression_block_clusters)) {
1543 | (na->compression_block_clusters - 1)) + 1 - rl->vcn;
1545 compressed_part = na->compression_block_clusters
1546 - (rl->length & (na->compression_block_clusters - 1));
1547 if ((beginwrite + compressed_part) >= na->compression_block_clusters)
1548 compressed_part = na->compression_block_clusters;
1563 if (endblock > na->compression_block_clusters) {
1564 if (na->unused_runs < 2) {
1567 na->unused_runs -= 2;
1578 rl[1].length = na->compression_block_clusters;
1581 - na->compression_block_clusters;
1586 + na->compression_block_clusters;
1593 if (!na->unused_runs) {
1596 na->unused_runs--;
1632 if (!na->unused_runs) {
1635 na->unused_runs--;
1651 rl[1].vcn -= na->compression_block_clusters;
1653 rl[1].length = na->compression_block_clusters;
1654 rl[0].length -= na->compression_block_clusters;
1659 NAttrSetRunlistDirty(na);
1679 static int borrow_from_hole(ntfs_attr *na, runlist_element **prl,
1683 int cluster_size_bits = na->ni->vol->cluster_size_bits;
1693 endblock = (((pos + count - 1) >> cluster_size_bits) | (na->compression_block_clusters - 1)) + 1 - rl->vcn;
1707 runlist_element *orl = na->rl;
1717 irl = *prl - na->rl;
1722 if (!NAttrBeingNonResident(na)
1723 && (NAttrDataAppending(na)
1724 ? ntfs_attr_map_partial_runlist(na,prevblock)
1725 : ntfs_attr_map_whole_runlist(na))) {
1727 if (!NAttrBeingNonResident(na)
1728 && ntfs_attr_map_whole_runlist(na)) {
1739 if ((na->rl != orl) || ((*prl)->lcn != olcn)) {
1740 zrl = &na->rl[irl];
1748 (long long)na->ni->mft_no,
1752 rl = ntfs_rl_extend(na,*prl,2);
1753 na->unused_runs = 2;
1782 *prl = ntfs_rl_extend(na,*prl,1);
1787 na->unused_runs = 0;
1796 NAttrSetRunlistDirty(na);
1801 compressed_part = na->compression_block_clusters
1804 if (ntfs_attr_fill_hole(na,
1825 static int ntfs_attr_truncate_i(ntfs_attr *na, const s64 newsize,
1830 * @na: ntfs attribute to write to
1836 * @na at position @pos.
1847 static s64 ntfs_attr_pwrite_i(ntfs_attr *na, const s64 pos, s64 count,
1867 vol = na->ni->vol;
1868 compressed = (na->data_flags & ATTR_COMPRESSION_MASK)
1870 na->unused_runs = 0; /* prepare overflow checks */
1876 if ((na->data_flags & ATTR_IS_ENCRYPTED)
1886 && (na->type == AT_DATA)
1887 && (pos > na->initialized_size)
1888 && stuff_hole(na,pos))
1891 wasnonresident = NAttrNonResident(na) != 0;
1897 && ((na->type != AT_DATA)
1898 || ((na->data_flags & ATTR_COMPRESSION_MASK)
1909 old_data_size = na->data_size;
1911 if ((na->type == AT_DATA) && (pos >= old_data_size)
1912 && NAttrNonResident(na))
1913 NAttrSetDataAppending(na);
1914 if (pos + count > na->data_size) {
1924 if (ntfs_attr_truncate_i(na, pos + count,
1925 (NAttrDataAppending(na) ?
1935 if (NAttrDataAppending(na))
1938 if (ntfs_attr_truncate_i(na, pos + count, HOLES_OK)) {
1944 compressed = (na->data_flags & ATTR_COMPRESSION_MASK)
1957 fullcount = (pos | (na->compression_block_size - 1)) + 1 - pos;
1961 old_initialized_size = na->initialized_size;
1963 if (!NAttrNonResident(na)) {
1966 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
1969 if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0,
2002 if (pos + count > na->initialized_size) {
2011 if (compressed && !NAttrDataAppending(na)) {
2012 if (ntfs_attr_map_whole_runlist(na))
2017 if (NAttrDataAppending(na)
2018 || (pos < na->initialized_size))
2021 block_begin = na->initialized_size >> vol->cluster_size_bits;
2024 block_begin &= -na->compression_block_clusters;
2027 if (ntfs_attr_map_partial_runlist(na, block_begin))
2033 if (ntfs_attr_map_whole_runlist(na))
2043 na->rl = ntfs_rl_extend(na,na->rl,2);
2044 if (!na->rl)
2046 na->unused_runs = 2;
2049 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
2052 if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0,
2057 if (pos > na->initialized_size)
2058 if (ntfs_attr_fill_zero(na, na->initialized_size,
2059 pos - na->initialized_size))
2065 na->data_size = pos + count;
2080 na->initialized_size = pos + count;
2082 if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY
2083 ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30
2084 : na->type == AT_DATA && na->name == AT_UNNAMED) {
2085 na->ni->data_size = na->data_size;
2086 if ((compressed || NAttrSparse(na))
2087 && NAttrNonResident(na))
2088 na->ni->allocated_size = na->compressed_size;
2090 na->ni->allocated_size = na->allocated_size;
2091 set_nino_flag(na->ni,KnownSize);
2105 rl = ntfs_attr_find_vcn(na, pos >> vol->cluster_size_bits);
2137 if (rl->length < na->compression_block_clusters)
2144 = na->compression_block_clusters
2152 compressed_part = split_compressed_hole(na,
2162 compressed_part = borrow_from_hole(na,
2172 if (NAttrBeingNonResident(na)
2173 && (compressed_part < na->compression_block_clusters))
2184 rl = ntfs_attr_find_vcn(na, rl->vcn);
2211 if (ntfs_attr_fill_hole(na, fullcount, &ofs, &rl,
2246 if (rounding && ((wend == na->initialized_size) ||
2261 written = ntfs_compressed_pwrite(na,
2275 written = ntfs_compressed_pwrite(na,
2312 if (NAttrRunlistDirty(na)) {
2313 if (ntfs_attr_update_mapping_pairs(na,
2323 NAttrClearBeingNonResident(na);
2324 NAttrClearDataAppending(na);
2332 if (pos + total > na->initialized_size)
2350 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
2356 err = ntfs_attr_lookup(na->type, na->name,
2357 na->name_len, 0, 0, NULL, 0, ctx);
2359 na->initialized_size = old_initialized_size;
2381 if (NAttrRunlistDirty(na))
2382 ntfs_attr_update_mapping_pairs(na, 0);
2385 && ntfs_attr_truncate_i(na, old_data_size, HOLES_OK))
2393 s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, const void *b)
2399 "0x%llx.\n", (long long)na->ni->mft_no, le32_to_cpu(na->type),
2403 if (!na || !na->ni || !na->ni->vol || !b || pos < 0 || count < 0) {
2415 written = ntfs_attr_pwrite_i(na, pos + total,
2426 int ntfs_attr_pclose(ntfs_attr *na)
2440 (unsigned long long)na->ni->mft_no,
2441 le32_to_cpu(na->type));
2443 if (!na || !na->ni || !na->ni->vol) {
2448 vol = na->ni->vol;
2449 na->unused_runs = 0;
2450 compressed = (na->data_flags & ATTR_COMPRESSION_MASK)
2456 if (NAttrEncrypted(na) && NAttrNonResident(na)) {
2462 if (!compressed || !NAttrNonResident(na))
2466 if (NAttrComprClosing(na)) {
2470 (long long)na->ni->mft_no);
2473 NAttrSetComprClosing(na);
2478 if (ntfs_attr_map_whole_runlist(na))
2480 na->rl = ntfs_rl_extend(na,na->rl,2);
2481 if (!na->rl)
2483 na->unused_runs = 2;
2485 rl = ntfs_attr_find_vcn(na, (na->initialized_size - 1) >> vol->cluster_size_bits);
2512 & (na->compression_block_clusters - 1);
2515 if (rl->length < na->compression_block_clusters)
2517 = na->compression_block_clusters
2521 = na->compression_block_clusters;
2527 ofs = na->initialized_size - (rl->vcn << vol->cluster_size_bits);
2530 rl = ntfs_attr_find_vcn(na, rl->vcn);
2540 ofs = na->initialized_size - (rl->vcn << vol->cluster_size_bits);
2556 if (ntfs_attr_fill_hole(na, (s64)0, &ofs, &rl, &update_from))
2569 failed = ntfs_compressed_close(na, rl, ofs, &update_from);
2571 if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY
2572 ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30
2573 : na->type == AT_DATA && na->name == AT_UNNAMED) {
2574 na->ni->data_size = na->data_size;
2575 na->ni->allocated_size = na->compressed_size;
2576 set_nino_flag(na->ni,KnownSize);
2590 if (NAttrFullyMapped(na))
2591 if (ntfs_attr_update_mapping_pairs(na, update_from)) {
2600 NAttrClearComprClosing(na);
2615 if (NAttrFullyMapped(na))
2616 ntfs_attr_update_mapping_pairs(na, 0);
2625 * @na: multi sector transfer protected ntfs attribute to read from
2632 * at offset @pos from the ntfs attribute @na into the data buffer @b.
2652 s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos, const s64 bk_cnt,
2660 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
2667 br = ntfs_attr_pread(na, pos, bk_cnt * bk_size, dst);
2672 warn = !na->ni || !na->ni->vol || !NVolNoFixupWarn(na->ni->vol);
2682 * @na: multi sector transfer protected ntfs attribute to write to
2689 * data buffer @b to multi sector transfer (mst) protected ntfs attribute @na
2710 s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos, s64 bk_cnt,
2716 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
2740 written = ntfs_attr_pwrite(na, pos, bk_cnt * bk_size, src);
4520 ntfs_attr *na;
4669 na = ntfs_attr_open(ni, type, name, name_len);
4670 if (!na) {
4676 if (ntfs_attr_truncate_i(na, size, HOLES_OK) ||
4677 (val && (ntfs_attr_pwrite(na, 0, size, val) != size))) {
4680 if (ntfs_attr_rm(na))
4682 ntfs_attr_close(na);
4685 ntfs_attr_close(na);
4734 * @na: opened ntfs attribute to delete
4741 int ntfs_attr_rm(ntfs_attr *na)
4746 if (!na) {
4753 (long long) na->ni->mft_no, le32_to_cpu(na->type));
4756 if (NAttrNonResident(na)) {
4757 if (ntfs_attr_map_whole_runlist(na))
4759 if (ntfs_cluster_free(na->ni->vol, na, 0, -1) < 0) {
4767 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
4770 while (!ntfs_attr_lookup(na->type, na->name, na->name_len,
5088 * @na: open ntfs attribute to make non-resident
5105 int ntfs_attr_make_non_resident(ntfs_attr *na,
5109 ntfs_volume *vol = na->ni->vol;
5115 long)na->ni->mft_no, le32_to_cpu(na->type));
5118 if (NAttrNonResident(na)) {
5126 if (ntfs_attr_can_be_non_resident(vol, na->type, na->name, na->name_len))
5151 NAttrSetNonResident(na);
5152 NAttrSetBeingNonResident(na);
5153 na->rl = rl;
5154 na->allocated_size = new_allocated_size;
5155 na->data_size = na->initialized_size = le32_to_cpu(a->value_length);
5160 NAttrClearSparse(na);
5161 NAttrClearEncrypted(na);
5164 na->compression_block_size
5166 na->compression_block_clusters = 1 << STANDARD_COMPRESSION_UNIT;
5171 bw = ntfs_attr_pwrite(na, 0, le32_to_cpu(a->value_length),
5192 if (na->ni->flags & FILE_ATTR_COMPRESSED)
5244 na->data_flags = a->flags;
5250 a->data_size = a->initialized_size = cpu_to_sle64(na->data_size);
5267 if (rl && ntfs_cluster_free(vol, na, 0, -1) < 0)
5270 NAttrClearNonResident(na);
5271 NAttrClearFullyMapped(na);
5272 na->allocated_size = na->data_size;
5273 na->rl = NULL;
5280 static int ntfs_resident_attr_resize(ntfs_attr *na, const s64 newsize);
5284 * @na: resident ntfs attribute to resize
5287 * Change the size of a resident, open ntfs attribute @na to @newsize bytes.
5297 * ERANGE - @newsize is not valid for the attribute type of @na.
5300 static int ntfs_resident_attr_resize_i(ntfs_attr *na, const s64 newsize,
5309 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
5313 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
5316 if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0, 0, NULL, 0,
5322 vol = na->ni->vol;
5327 if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) {
5344 na->data_size = na->initialized_size = newsize;
5345 na->allocated_size = (newsize + 7) & ~7;
5346 if ((na->data_flags & ATTR_COMPRESSION_MASK)
5347 || NAttrSparse(na))
5348 na->compressed_size = na->allocated_size;
5349 if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY
5350 ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30
5351 : na->type == AT_DATA && na->name == AT_UNNAMED) {
5352 na->ni->data_size = na->data_size;
5353 if (((na->data_flags & ATTR_COMPRESSION_MASK)
5354 || NAttrSparse(na))
5355 && NAttrNonResident(na))
5356 na->ni->allocated_size
5357 = na->compressed_size;
5359 na->ni->allocated_size
5360 = na->allocated_size;
5361 set_nino_flag(na->ni,KnownSize);
5362 if (na->type == AT_DATA)
5363 NInoFileNameSetDirty(na->ni);
5376 if (!ntfs_attr_make_non_resident(na, ctx)) {
5386 if (newsize != na->data_size) {
5395 return ntfs_attr_truncate_i(na, newsize, holes);
5403 ntfs_attr_init_search_ctx(ctx, NULL, na->ni->mrec);
5421 tna = ntfs_attr_open(na->ni, a->type, (ntfschar*)((u8*)a +
5451 return ntfs_resident_attr_resize_i(na, newsize, holes);
5464 if (na->type==AT_STANDARD_INFORMATION || na->type==AT_ATTRIBUTE_LIST) {
5466 if (!NInoAttrList(na->ni) && ntfs_inode_add_attrlist(na->ni)) {
5470 if (ntfs_inode_free_space(na->ni, offsetof(ATTR_RECORD,
5475 return ntfs_resident_attr_resize_i(na, newsize, holes);
5484 ntfs_attr_init_search_ctx(ctx, na->ni, NULL);
5485 if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE,
5506 if (na->ni->nr_extents == -1)
5507 ni = na->ni->base_ni;
5509 ni = na->ni;
5514 return ntfs_resident_attr_resize_i(na, newsize, holes);
5530 if (na->ni->nr_extents == -1)
5531 na->ni = ni;
5535 return ntfs_resident_attr_resize_i(na, newsize, holes);
5551 static int ntfs_resident_attr_resize(ntfs_attr *na, const s64 newsize)
5556 ret = ntfs_resident_attr_resize_i(na, newsize, HOLES_OK);
5576 int ntfs_attr_force_non_resident(ntfs_attr *na)
5580 res = ntfs_resident_attr_resize_i(na, na->data_size, HOLES_NONRES);
5581 if (!res && !NAttrNonResident(na)) {
5591 * @na: open ntfs attribute to make resident
5608 static int ntfs_attr_make_resident(ntfs_attr *na, ntfs_attr_search_ctx *ctx)
5610 ntfs_volume *vol = na->ni->vol;
5616 long)na->ni->mft_no, le32_to_cpu(na->type));
5627 if (!NAttrNonResident(na)) {
5635 if (na->type == AT_BITMAP && na->ni->mft_no == FILE_MFT) {
5641 if (ntfs_attr_can_be_resident(vol, na->type))
5644 if (na->data_flags & ATTR_IS_ENCRYPTED) {
5654 arec_size = (val_ofs + na->data_size + 7) & ~7;
5665 if (ntfs_attr_map_whole_runlist(na))
5691 a->value_length = cpu_to_le32(na->data_size);
5697 if (!na->data_size
5698 && (na->type == AT_DATA)
5699 && (na->ni->vol->major_ver >= 3)
5700 && NVolCompression(na->ni->vol)
5701 && (na->ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE)
5702 && (na->ni->flags & FILE_ATTR_COMPRESSED)) {
5704 na->data_flags = a->flags;
5718 if (na->initialized_size > na->data_size)
5719 na->initialized_size = na->data_size;
5722 bytes_read = ntfs_rl_pread(vol, na->rl, 0, na->initialized_size,
5724 if (bytes_read != na->initialized_size) {
5735 if (na->initialized_size < na->data_size)
5736 memset((u8*)a + val_ofs + na->initialized_size, 0,
5737 na->data_size - na->initialized_size);
5746 if (ntfs_cluster_free(vol, na, 0, -1) < 0) {
5753 free(na->rl);
5754 na->rl = NULL;
5757 NAttrClearNonResident(na);
5758 NAttrClearFullyMapped(na);
5759 NAttrClearSparse(na);
5760 NAttrClearEncrypted(na);
5761 na->initialized_size = na->data_size;
5762 na->allocated_size = na->compressed_size = (na->data_size + 7) & ~7;
5763 na->compression_block_size = 0;
5764 na->compression_block_size_bits = na->compression_block_clusters = 0;
5772 static int ntfs_attr_update_meta(ATTR_RECORD *a, ntfs_attr *na, MFT_RECORD *m,
5778 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type));
5783 a->allocated_size = cpu_to_sle64(na->allocated_size);
5789 sparse = ntfs_rl_sparse(na->rl);
5810 if (!NInoAttrList(na->ni)) {
5812 if (ntfs_inode_add_attrlist(na->ni))
5830 NAttrSetSparse(na);
5832 na->data_flags = a->flags;
5850 NAttrClearSparse(na);
5852 na->data_flags = a->flags;
5867 if (NAttrFullyMapped(na)
5868 && (sparse || (na->data_flags & ATTR_COMPRESSION_MASK))) {
5871 new_compr_size = ntfs_rl_get_compressed_size(na->ni->vol, na->rl);
5875 na->compressed_size = new_compr_size;
5882 if (na->type == AT_DATA && na->name == AT_UNNAMED) {
5883 if (sparse || (na->data_flags & ATTR_COMPRESSION_MASK))
5884 na->ni->allocated_size = na->compressed_size;
5886 na->ni->allocated_size = na->allocated_size;
5887 NInoFileNameSetDirty(na->ni);
5900 static int ntfs_attr_update_mapping_pairs_i(ntfs_attr *na, VCN from_vcn,
5914 if (!na || !na->rl) {
5916 ntfs_log_perror("%s: na=%p", __FUNCTION__, na);
5921 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type));
5923 if (!NAttrNonResident(na)) {
5938 && (!NAttrFullyMapped(na) || from_vcn)
5939 && !(na->data_flags & ATTR_IS_COMPRESSED)) {
5942 if (!(na->data_flags & ATTR_IS_SPARSE)) {
5951 for (xrl = na->rl; xrl->length; xrl++) {
5976 changed = (((na->data_size - 1)
5977 | (na->ni->vol->cluster_size - 1)) + 1)
5978 == na->compressed_size;
5981 if (ntfs_attr_map_whole_runlist(na)) {
5990 if (na->ni->nr_extents == -1)
5991 base_ni = na->ni->base_ni;
5993 base_ni = na->ni;
6001 stop_rl = na->rl;
6003 while (!ntfs_attr_lookup(na->type, na->name, na->name_len,
6025 first_lcn = ntfs_rl_vcn_to_lcn(na->rl, stop_vcn);
6051 switch (ntfs_attr_update_meta(a, na, m, holes, ctx)) {
6071 mp_size = ntfs_get_size_for_mapping_pairs(na->ni->vol, stop_rl,
6084 if (na->type == AT_ATTRIBUTE_LIST) {
6086 if (ntfs_inode_free_space(na->ni, mp_size -
6139 if (!ntfs_mapping_pairs_build(na->ni->vol, (u8*)a + le16_to_cpu(
6140 a->mapping_pairs_offset), mp_size, na->rl,
6166 if (!ntfs_attr_lookup(na->type, na->name, na->name_len,
6169 a->allocated_size = cpu_to_sle64(na->allocated_size);
6170 spcomp = na->data_flags
6173 a->compressed_size = cpu_to_sle64(na->compressed_size);
6177 if ((na->type == AT_DATA) && (na->name == AT_UNNAMED)) {
6178 na->ni->allocated_size
6180 ? na->compressed_size
6181 : na->allocated_size);
6182 NInoFileNameSetDirty(na->ni);
6194 while (!ntfs_attr_lookup(na->type, na->name, na->name_len,
6220 mp_size = ntfs_get_size_for_mapping_pairs(na->ni->vol,
6221 na->rl, stop_vcn, INT_MAX);
6227 if (!na->ni->mft_no)
6228 ni = ntfs_mft_rec_alloc(na->ni->vol,
6229 na->type == AT_DATA);
6231 ni = ntfs_mft_record_alloc(na->ni->vol, base_ni);
6244 (((na->data_flags & ATTR_COMPRESSION_MASK)
6245 || NAttrSparse(na)) ?
6247 ((sizeof(ntfschar) * na->name_len + 7) & ~7);
6251 err = ntfs_non_resident_attr_record_add(ni, na->type,
6252 na->name, na->name_len, stop_vcn, mp_size,
6253 na->data_flags);
6257 if (ntfs_mft_record_free(na->ni->vol, ni))
6264 err = ntfs_mapping_pairs_build(na->ni->vol, (u8*)a +
6265 le16_to_cpu(a->mapping_pairs_offset), mp_size, na->rl,
6274 if (ntfs_mft_record_free(na->ni->vol, ni))
6286 NAttrClearRunlistDirty(na);
6299 * @na: non-resident ntfs open attribute for which we need update
6302 * Build mapping pairs from @na->rl and write them to the disk. Also, this
6306 * @na->allocated_size should be set to correct value for the new runlist before
6307 * call to this function. Vice-versa @na->compressed_size will be calculated and
6322 int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn)
6327 ret = ntfs_attr_update_mapping_pairs_i(na, from_vcn, HOLES_OK);
6334 * @na: non-resident ntfs attribute to shrink
6337 * Reduce the size of a non-resident, open ntfs attribute @na to @newsize bytes.
6342 * ERANGE - @newsize is not valid for the attribute type of @na.
6344 static int ntfs_non_resident_attr_shrink(ntfs_attr *na, const s64 newsize)
6353 na->ni->mft_no, le32_to_cpu(na->type), (long long)newsize);
6355 vol = na->ni->vol;
6361 if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) {
6371 if (na->data_flags & ATTR_COMPRESSION_MASK)
6379 | (na->compression_block_size - 1)) + 1)
6388 if ((na->allocated_size >> vol->cluster_size_bits) != first_free_vcn) {
6389 if (ntfs_attr_map_whole_runlist(na)) {
6395 nr_freed_clusters = ntfs_cluster_free(vol, na, first_free_vcn,
6404 if (ntfs_rl_truncate(&na->rl, first_free_vcn)) {
6409 free(na->rl);
6410 na->rl = NULL;
6414 NAttrSetRunlistDirty(na);
6417 na->allocated_size = first_free_vcn << vol->cluster_size_bits;
6419 if (ntfs_attr_update_mapping_pairs(na, 0 /*first_free_vcn*/)) {
6428 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
6432 if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE,
6443 na->data_size = newsize;
6445 if (newsize < na->initialized_size) {
6446 na->initialized_size = newsize;
6450 if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
6451 if (na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30) {
6452 na->ni->data_size = na->data_size;
6453 na->ni->allocated_size = na->allocated_size;
6454 set_nino_flag(na->ni,KnownSize);
6457 if (na->type == AT_DATA && na->name == AT_UNNAMED) {
6458 na->ni->data_size = na->data_size;
6459 NInoFileNameSetDirty(na->ni);
6465 if (!(na->data_flags & ATTR_IS_ENCRYPTED)
6466 && ntfs_attr_make_resident(na, ctx)) {
6487 * @na: non-resident ntfs attribute to expand
6490 * Expand the size of a non-resident, open ntfs attribute @na to @newsize bytes,
6496 * ERANGE - @newsize is not valid for the attribute type of @na.
6499 static int ntfs_non_resident_attr_expand_i(ntfs_attr *na, const s64 newsize,
6511 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
6512 (long long)newsize, (long long)na->data_size);
6514 vol = na->ni->vol;
6520 if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) {
6527 if (na->type == AT_DATA)
6528 NAttrSetDataAppending(na);
6530 org_alloc_size = na->allocated_size;
6538 if ((na->allocated_size >> vol->cluster_size_bits) < first_free_vcn) {
6546 start_update = na->allocated_size >> vol->cluster_size_bits;
6549 if (ntfs_attr_map_partial_runlist(na, start_update)) {
6554 if (ntfs_attr_map_whole_runlist(na)) {
6564 if ((na->type == AT_DATA) && (vol->major_ver >= 3)
6570 rl[0].vcn = (na->allocated_size >>
6574 (na->allocated_size >> vol->cluster_size_bits);
6587 if (na->rl->length) {
6589 for (rl = na->rl; (rl + 1)->length; rl++)
6595 while (rl->lcn < 0 && rl != na->rl)
6604 rl = ntfs_cluster_alloc(vol, na->allocated_size >>
6606 (na->allocated_size >>
6613 ((long long)na->allocated_size >>
6620 rln = ntfs_runlists_merge(na->rl, rl);
6630 na->rl = rln;
6631 NAttrSetRunlistDirty(na);
6634 na->allocated_size = first_free_vcn << vol->cluster_size_bits;
6643 && ntfs_attr_update_mapping_pairs_i(na, start_update,
6647 if (ntfs_attr_update_mapping_pairs(na, 0)) {
6655 ctx = ntfs_attr_get_search_ctx(na->ni, NULL);
6658 if (na->allocated_size == org_alloc_size) {
6665 if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE,
6671 if (na->allocated_size != org_alloc_size) {
6679 na->data_size = newsize;
6682 if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
6683 if (na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30) {
6684 na->ni->data_size = na->data_size;
6685 na->ni->allocated_size = na->allocated_size;
6686 set_nino_flag(na->ni,KnownSize);
6689 if (na->type == AT_DATA && na->name == AT_UNNAMED) {
6690 na->ni->data_size = na->data_size;
6691 NInoFileNameSetDirty(na->ni);
6701 if (ntfs_cluster_free(vol, na, org_alloc_size >>
6707 if (ntfs_rl_truncate(&na->rl, org_alloc_size >>
6713 free(na->rl);
6714 na->rl = NULL;
6717 NAttrSetRunlistDirty(na);
6719 na->allocated_size = org_alloc_size;
6721 if (ntfs_attr_update_mapping_pairs(na, 0 /*na->allocated_size >>
6735 static int ntfs_non_resident_attr_expand(ntfs_attr *na, const s64 newsize,
6741 ret = ntfs_non_resident_attr_expand_i(na, newsize, holes);
6748 * @na: open ntfs attribute to resize
6752 * Change the size of an open ntfs attribute @na to @newsize bytes. If the
6767 static int ntfs_attr_truncate_i(ntfs_attr *na, const s64 newsize,
6774 if (!na || newsize < 0 ||
6775 (na->ni->mft_no == FILE_MFT && na->type == AT_DATA)) {
6782 (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type),
6785 if (na->data_size == newsize) {
6794 if ((na->data_flags & ATTR_IS_ENCRYPTED) && !na->ni->vol->efs_raw) {
6806 compressed = (na->data_flags & ATTR_COMPRESSION_MASK)
6809 && NAttrNonResident(na)
6810 && ((na->data_flags & ATTR_COMPRESSION_MASK) != ATTR_IS_COMPRESSED)) {
6815 if (NAttrNonResident(na)) {
6827 if (compressed && newsize && (newsize > na->data_size))
6828 fullsize = (na->initialized_size
6829 | (na->compression_block_size - 1)) + 1;
6832 if (fullsize > na->data_size)
6833 ret = ntfs_non_resident_attr_expand(na, fullsize,
6836 ret = ntfs_non_resident_attr_shrink(na, fullsize);
6838 ret = ntfs_resident_attr_resize_i(na, newsize, holes);
6848 int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize)
6852 r = ntfs_attr_truncate_i(na, newsize, HOLES_OK);
6853 NAttrClearDataAppending(na);
6854 NAttrClearBeingNonResident(na);
6862 int ntfs_attr_truncate_solid(ntfs_attr *na, const s64 newsize)
6864 return (ntfs_attr_truncate_i(na, newsize, HOLES_NO));
6877 static int stuff_hole(ntfs_attr *na, const s64 pos)
6892 if (!NAttrNonResident(na)) {
6893 ret = ntfs_resident_attr_resize(na, pos);
6894 if (!ret && !NAttrNonResident(na))
6895 na->initialized_size = na->data_size = pos;
6897 if (!ret && NAttrNonResident(na)) {
6899 if ((pos ^ na->initialized_size)
6900 & ~(na->compression_block_size - 1)) {
6901 begin_size = ((na->initialized_size - 1)
6902 | (na->compression_block_size - 1))
6903 + 1 - na->initialized_size;
6904 end_size = pos & (na->compression_block_size - 1);
6908 begin_size = size = pos - na->initialized_size;
6919 && (ntfs_attr_pwrite(na,
6920 na->initialized_size, begin_size, buf)
6925 && ((na->initialized_size + end_size) < pos)
6926 && ntfs_non_resident_attr_expand(na,
6930 na->initialized_size
6931 = na->data_size = pos - end_size;
6934 && (ntfs_attr_pwrite(na,
6935 na->initialized_size, end_size, buf)
6944 if (!ret && (na->initialized_size != pos)) {
6947 (long long)pos, (long long)na->initialized_size);
6976 ntfs_attr *na;
6982 na = ntfs_attr_open(ni, type, name, name_len);
6983 if (!na) {
6993 if (((u64)na->data_size > 65536)
6995 || ((u64)na->data_size >
7002 data = ntfs_malloc(na->data_size);
7006 size = ntfs_attr_pread(na, 0, na->data_size, data);
7007 if (size != na->data_size) {
7016 ntfs_attr_close(na);
7032 ntfs_attr *na = NULL;
7035 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
7036 if (!na) {
7040 if ((size_t)offset < (size_t)na->data_size) {
7041 if (offset + size > (size_t)na->data_size)
7042 size = na->data_size - offset;
7044 res = ntfs_attr_pread(na, offset, size, buf + total);
7061 if (na)
7062 ntfs_attr_close(na);
7076 ntfs_attr *na = NULL;
7079 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
7080 if (!na) {
7085 res = ntfs_attr_pwrite(na, offset, size, buf + total);
7100 if (na)
7101 ntfs_attr_close(na);
7165 ntfs_attr *na;
7176 na = ntfs_attr_open(ni, type, name, name_len);
7177 if (!na) {
7186 ret = ntfs_attr_rm(na);
7190 ntfs_attr_close(na);
7213 s64 ntfs_attr_get_free_bits(ntfs_attr *na)
7230 br = ntfs_attr_pread(na, total, 65536, buf);