1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/blkdev.h>
9 #include <linux/buffer_head.h>
10 #include <linux/fiemap.h>
12 #include <linux/nls.h>
13 #include <linux/vmalloc.h>
18 #ifdef CONFIG_NTFS3_LZX_XPRESS
22 static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
23 CLST ino, struct rb_node *ins)
25 struct rb_node **p = &tree->rb_node;
26 struct rb_node *pr = NULL;
32 mi = rb_entry(pr, struct mft_inode, node);
35 else if (mi->rno < ino)
44 rb_link_node(ins, pr, p);
45 rb_insert_color(ins, tree);
46 return rb_entry(ins, struct mft_inode, node);
50 * ni_find_mi - Find mft_inode by record number.
52 static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
54 return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
58 * ni_add_mi - Add new mft_inode into ntfs_inode.
60 static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
62 ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
66 * ni_remove_mi - Remove mft_inode from ntfs_inode.
68 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
70 rb_erase(&mi->node, &ni->mi_tree);
74 * ni_std - Return: Pointer into std_info from primary record.
76 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
78 const struct ATTRIB *attr;
80 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
81 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO))
88 * Return: Pointer into std_info from primary record.
90 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
92 const struct ATTRIB *attr;
94 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
96 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5))
101 * ni_clear - Clear resources allocated by ntfs_inode.
103 void ni_clear(struct ntfs_inode *ni)
105 struct rb_node *node;
107 if (!ni->vfs_inode.i_nlink && is_rec_inuse(ni->mi.mrec))
112 for (node = rb_first(&ni->mi_tree); node;) {
113 struct rb_node *next = rb_next(node);
114 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
116 rb_erase(node, &ni->mi_tree);
121 /* Bad inode always has mode == S_IFREG. */
122 if (ni->ni_flags & NI_FLAG_DIR)
123 indx_clear(&ni->dir);
125 run_close(&ni->file.run);
126 #ifdef CONFIG_NTFS3_LZX_XPRESS
127 if (ni->file.offs_page) {
128 /* On-demand allocated page for offsets. */
129 put_page(ni->file.offs_page);
130 ni->file.offs_page = NULL;
139 * ni_load_mi_ex - Find mft_inode by record number.
141 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
146 r = ni_find_mi(ni, rno);
150 err = mi_get(ni->mi.sbi, rno, &r);
163 * ni_load_mi - Load mft_inode corresponded list_entry.
165 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
166 struct mft_inode **mi)
175 rno = ino_get(&le->ref);
176 if (rno == ni->mi.rno) {
180 return ni_load_mi_ex(ni, rno, mi);
186 * Return: Attribute and record this attribute belongs to.
188 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
189 struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
190 const __le16 *name, u8 name_len, const CLST *vcn,
191 struct mft_inode **mi)
193 struct ATTR_LIST_ENTRY *le;
196 if (!ni->attr_list.size ||
197 (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
203 /* Look for required attribute in primary record. */
204 return mi_find_attr(&ni->mi, attr, type, name, name_len, NULL);
207 /* First look for list entry of required type. */
208 le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
215 /* Load record that contains this attribute. */
216 if (ni_load_mi(ni, le, &m))
219 /* Look for required attribute. */
220 attr = mi_find_attr(m, NULL, type, name, name_len, &le->id);
225 if (!attr->non_res) {
231 } else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
232 *vcn > le64_to_cpu(attr->nres.evcn)) {
241 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR);
246 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
248 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
249 struct ATTR_LIST_ENTRY **le,
250 struct mft_inode **mi)
252 struct mft_inode *mi2;
253 struct ATTR_LIST_ENTRY *le2;
255 /* Do we have an attribute list? */
256 if (!ni->attr_list.size) {
260 /* Enum attributes in primary record. */
261 return mi_enum_attr(&ni->mi, attr);
264 /* Get next list entry. */
265 le2 = *le = al_enumerate(ni, attr ? *le : NULL);
269 /* Load record that contains the required attribute. */
270 if (ni_load_mi(ni, le2, &mi2))
276 /* Find attribute in loaded record. */
277 return rec_find_attr_le(mi2, le2);
281 * ni_load_attr - Load attribute that contains given VCN.
283 struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
284 const __le16 *name, u8 name_len, CLST vcn,
285 struct mft_inode **pmi)
287 struct ATTR_LIST_ENTRY *le;
289 struct mft_inode *mi;
290 struct ATTR_LIST_ENTRY *next;
292 if (!ni->attr_list.size) {
295 return mi_find_attr(&ni->mi, NULL, type, name, name_len, NULL);
298 le = al_find_ex(ni, NULL, type, name, name_len, NULL);
303 * Unfortunately ATTR_LIST_ENTRY contains only start VCN.
304 * So to find the ATTRIB segment that contains 'vcn' we should
305 * enumerate some entries.
309 next = al_find_ex(ni, le, type, name, name_len, NULL);
310 if (!next || le64_to_cpu(next->vcn) > vcn)
315 if (ni_load_mi(ni, le, &mi))
321 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
328 if (le64_to_cpu(attr->nres.svcn) <= vcn &&
329 vcn <= le64_to_cpu(attr->nres.evcn))
336 * ni_load_all_mi - Load all subrecords.
338 int ni_load_all_mi(struct ntfs_inode *ni)
341 struct ATTR_LIST_ENTRY *le;
343 if (!ni->attr_list.size)
348 while ((le = al_enumerate(ni, le))) {
349 CLST rno = ino_get(&le->ref);
351 if (rno == ni->mi.rno)
354 err = ni_load_mi_ex(ni, rno, NULL);
363 * ni_add_subrecord - Allocate + format + attach a new subrecord.
365 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
369 m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
373 if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
378 mi_get_ref(&ni->mi, &m->mrec->parent_ref);
386 * ni_remove_attr - Remove all attributes for the given type/name/id.
388 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
389 const __le16 *name, size_t name_len, bool base_only,
394 struct ATTR_LIST_ENTRY *le;
395 struct mft_inode *mi;
399 if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
400 attr = mi_find_attr(&ni->mi, NULL, type, name, name_len, id);
404 mi_remove_attr(ni, &ni->mi, attr);
408 type_in = le32_to_cpu(type);
412 le = al_enumerate(ni, le);
417 diff = le32_to_cpu(le->type) - type_in;
424 if (le->name_len != name_len)
428 memcmp(le_name(le), name, name_len * sizeof(short)))
431 if (id && le->id != *id)
433 err = ni_load_mi(ni, le, &mi);
437 al_remove_le(ni, le);
439 attr = mi_find_attr(mi, NULL, type, name, name_len, id);
443 mi_remove_attr(ni, mi, attr);
445 if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
452 * ni_ins_new_attr - Insert the attribute into record.
454 * Return: Not full constructed attribute or NULL if not possible to create.
456 static struct ATTRIB *
457 ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
458 struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
459 const __le16 *name, u8 name_len, u32 asize, u16 name_off,
460 CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
464 bool le_added = false;
467 mi_get_ref(mi, &ref);
469 if (type != ATTR_LIST && !le && ni->attr_list.size) {
470 err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
473 /* No memory or no space. */
479 * al_add_le -> attr_set_size (list) -> ni_expand_list
480 * which moves some attributes out of primary record
481 * this means that name may point into moved memory
482 * reinit 'name' from le.
487 attr = mi_insert_attr(mi, type, name, name_len, asize, name_off);
490 al_remove_le(ni, le);
494 if (type == ATTR_LIST) {
495 /* Attr list is not in list entry array. */
502 /* Update ATTRIB Id and record reference. */
504 ni->attr_list.dirty = true;
516 * Random write access to sparsed or compressed file may result to
517 * not optimized packed runs.
518 * Here is the place to optimize it.
520 static int ni_repack(struct ntfs_inode *ni)
523 struct ntfs_sb_info *sbi = ni->mi.sbi;
524 struct mft_inode *mi, *mi_p = NULL;
525 struct ATTRIB *attr = NULL, *attr_p;
526 struct ATTR_LIST_ENTRY *le = NULL, *le_p;
528 u8 cluster_bits = sbi->cluster_bits;
529 CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
530 u32 roff, rs = sbi->record_size;
531 struct runs_tree run;
535 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
539 svcn = le64_to_cpu(attr->nres.svcn);
540 if (svcn != le64_to_cpu(le->vcn)) {
546 alloc = le64_to_cpu(attr->nres.alloc_size) >>
549 } else if (svcn != evcn + 1) {
554 evcn = le64_to_cpu(attr->nres.evcn);
556 if (svcn > evcn + 1) {
562 /* Do not try if not enogh free space. */
563 if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
566 /* Do not try if last attribute segment. */
567 if (evcn + 1 == alloc)
572 roff = le16_to_cpu(attr->nres.run_off);
573 err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
575 le32_to_cpu(attr->size) - roff);
590 * Run contains data from two records: mi_p and mi
591 * Try to pack in one.
593 err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
597 next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
599 if (next_svcn >= evcn + 1) {
600 /* We can remove this attribute segment. */
601 al_remove_le(ni, le);
602 mi_remove_attr(NULL, mi, attr);
607 attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
609 ni->attr_list.dirty = true;
611 if (evcn + 1 == alloc) {
612 err = mi_pack_runs(mi, attr, &run,
613 evcn + 1 - next_svcn);
623 run_truncate_head(&run, next_svcn);
628 ntfs_inode_warn(&ni->vfs_inode, "repack problem");
629 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
631 /* Pack loaded but not packed runs. */
633 mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
641 * ni_try_remove_attr_list
643 * Can we remove attribute list?
644 * Check the case when primary record contains enough space for all attributes.
646 static int ni_try_remove_attr_list(struct ntfs_inode *ni)
649 struct ntfs_sb_info *sbi = ni->mi.sbi;
650 struct ATTRIB *attr, *attr_list, *attr_ins;
651 struct ATTR_LIST_ENTRY *le;
652 struct mft_inode *mi;
657 if (!ni->attr_list.dirty)
664 attr_list = mi_find_attr(&ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
668 asize = le32_to_cpu(attr_list->size);
670 /* Free space in primary record without attribute list. */
671 free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
672 mi_get_ref(&ni->mi, &ref);
675 while ((le = al_enumerate(ni, le))) {
676 if (!memcmp(&le->ref, &ref, sizeof(ref)))
682 mi = ni_find_mi(ni, ino_get(&le->ref));
686 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
687 le->name_len, &le->id);
691 asize = le32_to_cpu(attr->size);
698 /* It seems that attribute list can be removed from primary record. */
699 mi_remove_attr(NULL, &ni->mi, attr_list);
702 * Repeat the cycle above and move all attributes to primary record.
703 * It should be success!
706 while ((le = al_enumerate(ni, le))) {
707 if (!memcmp(&le->ref, &ref, sizeof(ref)))
710 mi = ni_find_mi(ni, ino_get(&le->ref));
712 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
713 le->name_len, &le->id);
714 asize = le32_to_cpu(attr->size);
716 /* Insert into primary record. */
717 attr_ins = mi_insert_attr(&ni->mi, le->type, le_name(le),
719 le16_to_cpu(attr->name_off));
722 /* Copy all except id. */
723 memcpy(attr_ins, attr, asize);
726 /* Remove from original record. */
727 mi_remove_attr(NULL, mi, attr);
730 run_deallocate(sbi, &ni->attr_list.run, true);
731 run_close(&ni->attr_list.run);
732 ni->attr_list.size = 0;
733 kfree(ni->attr_list.le);
734 ni->attr_list.le = NULL;
735 ni->attr_list.dirty = false;
741 * ni_create_attr_list - Generates an attribute list for this primary record.
743 int ni_create_attr_list(struct ntfs_inode *ni)
745 struct ntfs_sb_info *sbi = ni->mi.sbi;
749 struct ATTRIB *arr_move[7];
750 struct ATTR_LIST_ENTRY *le, *le_b[7];
754 struct mft_inode *mi;
755 u32 free_b, nb, to_free, rs;
758 is_mft = ni->mi.rno == MFT_REC_MFT;
760 rs = sbi->record_size;
763 * Skip estimating exact memory requirement.
764 * Looks like one record_size is always enough.
766 le = kmalloc(al_aligned(rs), GFP_NOFS);
772 mi_get_ref(&ni->mi, &le->ref);
773 ni->attr_list.le = le;
780 for (; (attr = mi_enum_attr(&ni->mi, attr)); le = Add2Ptr(le, sz)) {
781 sz = le_size(attr->name_len);
782 le->type = attr->type;
783 le->size = cpu_to_le16(sz);
784 le->name_len = attr->name_len;
785 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
787 if (le != ni->attr_list.le)
788 le->ref = ni->attr_list.le->ref;
792 memcpy(le->name, attr_name(attr),
793 sizeof(short) * attr->name_len);
794 else if (attr->type == ATTR_STD)
796 else if (attr->type == ATTR_LIST)
798 else if (is_mft && attr->type == ATTR_DATA)
801 if (!nb || nb < ARRAY_SIZE(arr_move)) {
803 arr_move[nb++] = attr;
804 free_b += le32_to_cpu(attr->size);
808 lsize = PtrOffset(ni->attr_list.le, le);
809 ni->attr_list.size = lsize;
811 to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
817 if (to_free > free_b) {
823 /* Allocate child MFT. */
824 err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
828 /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
829 while (to_free > 0) {
830 struct ATTRIB *b = arr_move[--nb];
831 u32 asize = le32_to_cpu(b->size);
832 u16 name_off = le16_to_cpu(b->name_off);
834 attr = mi_insert_attr(mi, b->type, Add2Ptr(b, name_off),
835 b->name_len, asize, name_off);
838 mi_get_ref(mi, &le_b[nb]->ref);
839 le_b[nb]->id = attr->id;
841 /* Copy all except id. */
842 memcpy(attr, b, asize);
843 attr->id = le_b[nb]->id;
845 /* Remove from primary record. */
846 WARN_ON(!mi_remove_attr(NULL, &ni->mi, b));
848 if (to_free <= asize)
854 attr = mi_insert_attr(&ni->mi, ATTR_LIST, NULL, 0,
855 lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
860 attr->res.data_size = cpu_to_le32(lsize);
861 attr->res.data_off = SIZEOF_RESIDENT_LE;
865 memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
867 ni->attr_list.dirty = false;
869 mark_inode_dirty(&ni->vfs_inode);
873 kfree(ni->attr_list.le);
874 ni->attr_list.le = NULL;
875 ni->attr_list.size = 0;
882 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
884 static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
885 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
886 u32 asize, CLST svcn, u16 name_off, bool force_ext,
887 struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
888 struct ATTR_LIST_ENTRY **ins_le)
891 struct mft_inode *mi;
894 struct rb_node *node;
896 bool is_mft, is_mft_data;
897 struct ntfs_sb_info *sbi = ni->mi.sbi;
899 is_mft = ni->mi.rno == MFT_REC_MFT;
900 is_mft_data = is_mft && type == ATTR_DATA && !name_len;
902 if (asize > sbi->max_bytes_per_attr) {
908 * Standard information and attr_list cannot be made external.
909 * The Log File cannot have any external attributes.
911 if (type == ATTR_STD || type == ATTR_LIST ||
912 ni->mi.rno == MFT_REC_LOG) {
917 /* Create attribute list if it is not already existed. */
918 if (!ni->attr_list.size) {
919 err = ni_create_attr_list(ni);
924 vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
929 /* Load all subrecords into memory. */
930 err = ni_load_all_mi(ni);
934 /* Check each of loaded subrecord. */
935 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
936 mi = rb_entry(node, struct mft_inode, node);
939 (mi_enum_attr(mi, NULL) ||
940 vbo <= ((u64)mi->rno << sbi->record_bits))) {
941 /* We can't accept this record 'cause MFT's bootstrapping. */
945 mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
947 * This child record already has a ATTR_DATA.
948 * So it can't accept any other records.
953 if ((type != ATTR_NAME || name_len) &&
954 mi_find_attr(mi, NULL, type, name, name_len, NULL)) {
955 /* Only indexed attributes can share same record. */
959 /* Try to insert attribute into this subrecord. */
960 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
961 name_off, svcn, ins_le);
973 /* We have to allocate a new child subrecord. */
974 err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
978 if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
983 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
984 name_off, svcn, ins_le);
996 ni_remove_mi(ni, mi);
1001 ntfs_mark_rec_free(sbi, rno);
1008 * ni_insert_attr - Insert an attribute into the file.
1010 * If the primary record has room, it will just insert the attribute.
1011 * If not, it may make the attribute external.
1012 * For $MFT::Data it may make room for the attribute by
1013 * making other attributes external.
1016 * The ATTR_LIST and ATTR_STD cannot be made external.
1017 * This function does not fill new attribute full.
1018 * It only fills 'size'/'type'/'id'/'name_len' fields.
1020 static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1021 const __le16 *name, u8 name_len, u32 asize,
1022 u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
1023 struct mft_inode **ins_mi,
1024 struct ATTR_LIST_ENTRY **ins_le)
1026 struct ntfs_sb_info *sbi = ni->mi.sbi;
1028 struct ATTRIB *attr, *eattr;
1029 struct MFT_REC *rec;
1031 struct ATTR_LIST_ENTRY *le;
1032 u32 list_reserve, max_free, free, used, t32;
1036 is_mft = ni->mi.rno == MFT_REC_MFT;
1039 list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1040 used = le32_to_cpu(rec->used);
1041 free = sbi->record_size - used;
1043 if (is_mft && type != ATTR_LIST) {
1044 /* Reserve space for the ATTRIB list. */
1045 if (free < list_reserve)
1048 free -= list_reserve;
1051 if (asize <= free) {
1052 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
1053 asize, name_off, svcn, ins_le);
1064 if (!is_mft || type != ATTR_DATA || svcn) {
1065 /* This ATTRIB will be external. */
1066 err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
1067 svcn, name_off, false, ins_attr, ins_mi,
1073 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
1075 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1076 * Evict as many other attributes as possible.
1080 /* Estimate the result of moving all possible attributes away. */
1083 while ((attr = mi_enum_attr(&ni->mi, attr))) {
1084 if (attr->type == ATTR_STD)
1086 if (attr->type == ATTR_LIST)
1088 max_free += le32_to_cpu(attr->size);
1091 if (max_free < asize + list_reserve) {
1092 /* Impossible to insert this attribute into primary record. */
1097 /* Start real attribute moving. */
1101 attr = mi_enum_attr(&ni->mi, attr);
1103 /* We should never be here 'cause we have already check this case. */
1108 /* Skip attributes that MUST be primary record. */
1109 if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1113 if (ni->attr_list.size) {
1114 le = al_find_le(ni, NULL, attr);
1116 /* Really this is a serious bug. */
1122 t32 = le32_to_cpu(attr->size);
1123 t16 = le16_to_cpu(attr->name_off);
1124 err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1125 attr->name_len, t32, attr_svcn(attr), t16,
1126 false, &eattr, NULL, NULL);
1131 memcpy(eattr, attr, t32);
1134 /* Remove from primary record. */
1135 mi_remove_attr(NULL, &ni->mi, attr);
1137 /* attr now points to next attribute. */
1138 if (attr->type == ATTR_END)
1141 while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1144 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
1145 name_off, svcn, ins_le);
1160 /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
1161 static int ni_expand_mft_list(struct ntfs_inode *ni)
1164 struct runs_tree *run = &ni->file.run;
1165 u32 asize, run_size, done = 0;
1166 struct ATTRIB *attr;
1167 struct rb_node *node;
1168 CLST mft_min, mft_new, svcn, evcn, plen;
1169 struct mft_inode *mi, *mi_min, *mi_new;
1170 struct ntfs_sb_info *sbi = ni->mi.sbi;
1172 /* Find the nearest MFT. */
1177 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1178 mi = rb_entry(node, struct mft_inode, node);
1180 attr = mi_enum_attr(mi, NULL);
1189 if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1191 /* Really this is not critical. */
1192 } else if (mft_min > mft_new) {
1196 ntfs_mark_rec_free(sbi, mft_new);
1198 ni_remove_mi(ni, mi_new);
1201 attr = mi_find_attr(&ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1207 asize = le32_to_cpu(attr->size);
1209 evcn = le64_to_cpu(attr->nres.evcn);
1210 svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1211 if (evcn + 1 >= svcn) {
1217 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
1219 * Update first part of ATTR_DATA in 'primary MFT.
1221 err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1222 asize - SIZEOF_NONRESIDENT, &plen);
1226 run_size = ALIGN(err, 8);
1234 attr->nres.evcn = cpu_to_le64(svcn - 1);
1235 attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
1236 /* 'done' - How many bytes of primary MFT becomes free. */
1237 done = asize - run_size - SIZEOF_NONRESIDENT;
1238 le32_sub_cpu(&ni->mi.mrec->used, done);
1240 /* Estimate the size of second part: run_buf=NULL. */
1241 err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1246 run_size = ALIGN(err, 8);
1249 if (plen < evcn + 1 - svcn) {
1255 * This function may implicitly call expand attr_list.
1256 * Insert second part of ATTR_DATA in 'mi_min'.
1258 attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1259 SIZEOF_NONRESIDENT + run_size,
1260 SIZEOF_NONRESIDENT, svcn, NULL);
1267 attr->name_off = SIZEOF_NONRESIDENT_LE;
1270 run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1273 attr->nres.svcn = cpu_to_le64(svcn);
1274 attr->nres.evcn = cpu_to_le64(evcn);
1275 attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1279 ntfs_mark_rec_free(sbi, mft_new);
1280 ni_remove_mi(ni, mi_new);
1283 return !err && !done ? -EOPNOTSUPP : err;
1287 * ni_expand_list - Move all possible attributes out of primary record.
1289 int ni_expand_list(struct ntfs_inode *ni)
1292 u32 asize, done = 0;
1293 struct ATTRIB *attr, *ins_attr;
1294 struct ATTR_LIST_ENTRY *le;
1295 bool is_mft = ni->mi.rno == MFT_REC_MFT;
1298 mi_get_ref(&ni->mi, &ref);
1301 while ((le = al_enumerate(ni, le))) {
1302 if (le->type == ATTR_STD)
1305 if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1308 if (is_mft && le->type == ATTR_DATA)
1311 /* Find attribute in primary record. */
1312 attr = rec_find_attr_le(&ni->mi, le);
1318 asize = le32_to_cpu(attr->size);
1320 /* Always insert into new record to avoid collisions (deep recursive). */
1321 err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1322 attr->name_len, asize, attr_svcn(attr),
1323 le16_to_cpu(attr->name_off), true,
1324 &ins_attr, NULL, NULL);
1329 memcpy(ins_attr, attr, asize);
1330 ins_attr->id = le->id;
1331 /* Remove from primary record. */
1332 mi_remove_attr(NULL, &ni->mi, attr);
1339 err = -EFBIG; /* Attr list is too big(?) */
1343 /* Split MFT data as much as possible. */
1344 err = ni_expand_mft_list(ni);
1349 return !err && !done ? -EOPNOTSUPP : err;
1353 * ni_insert_nonresident - Insert new nonresident attribute.
1355 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1356 const __le16 *name, u8 name_len,
1357 const struct runs_tree *run, CLST svcn, CLST len,
1358 __le16 flags, struct ATTRIB **new_attr,
1359 struct mft_inode **mi)
1363 struct ATTRIB *attr;
1365 (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && !svcn;
1366 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1367 u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1368 u32 run_off = name_off + name_size;
1369 u32 run_size, asize;
1370 struct ntfs_sb_info *sbi = ni->mi.sbi;
1372 err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1377 run_size = ALIGN(err, 8);
1384 asize = run_off + run_size;
1386 if (asize > sbi->max_bytes_per_attr) {
1391 err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
1398 attr->name_off = cpu_to_le16(name_off);
1399 attr->flags = flags;
1401 run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1403 attr->nres.svcn = cpu_to_le64(svcn);
1404 attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1410 *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1412 attr->nres.alloc_size =
1413 svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1414 attr->nres.data_size = attr->nres.alloc_size;
1415 attr->nres.valid_size = attr->nres.alloc_size;
1418 if (flags & ATTR_FLAG_COMPRESSED)
1419 attr->nres.c_unit = COMPRESSION_UNIT;
1420 attr->nres.total_size = attr->nres.alloc_size;
1428 * ni_insert_resident - Inserts new resident attribute.
1430 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1431 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
1432 struct ATTRIB **new_attr, struct mft_inode **mi,
1433 struct ATTR_LIST_ENTRY **le)
1436 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1437 u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
1438 struct ATTRIB *attr;
1440 err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
1448 attr->res.data_size = cpu_to_le32(data_size);
1449 attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
1450 if (type == ATTR_NAME) {
1451 attr->res.flags = RESIDENT_FLAG_INDEXED;
1453 /* is_attr_indexed(attr)) == true */
1454 le16_add_cpu(&ni->mi.mrec->hard_links, +1);
1455 ni->mi.dirty = true;
1466 * ni_remove_attr_le - Remove attribute from record.
1468 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1469 struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
1471 mi_remove_attr(ni, mi, attr);
1474 al_remove_le(ni, le);
1478 * ni_delete_all - Remove all attributes and frees allocates space.
1480 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
1482 int ni_delete_all(struct ntfs_inode *ni)
1485 struct ATTR_LIST_ENTRY *le = NULL;
1486 struct ATTRIB *attr = NULL;
1487 struct rb_node *node;
1491 struct ntfs_sb_info *sbi = ni->mi.sbi;
1492 bool nt3 = is_ntfs3(sbi);
1495 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1496 if (!nt3 || attr->name_len) {
1498 } else if (attr->type == ATTR_REPARSE) {
1499 mi_get_ref(&ni->mi, &ref);
1500 ntfs_remove_reparse(sbi, 0, &ref);
1501 } else if (attr->type == ATTR_ID && !attr->non_res &&
1502 le32_to_cpu(attr->res.data_size) >=
1503 sizeof(struct GUID)) {
1504 ntfs_objid_remove(sbi, resident_data(attr));
1510 svcn = le64_to_cpu(attr->nres.svcn);
1511 evcn = le64_to_cpu(attr->nres.evcn);
1513 if (evcn + 1 <= svcn)
1516 asize = le32_to_cpu(attr->size);
1517 roff = le16_to_cpu(attr->nres.run_off);
1519 /* run==1 means unpack and deallocate. */
1520 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1521 Add2Ptr(attr, roff), asize - roff);
1524 if (ni->attr_list.size) {
1525 run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1529 /* Free all subrecords. */
1530 for (node = rb_first(&ni->mi_tree); node;) {
1531 struct rb_node *next = rb_next(node);
1532 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1534 clear_rec_inuse(mi->mrec);
1538 ntfs_mark_rec_free(sbi, mi->rno);
1539 ni_remove_mi(ni, mi);
1544 /* Free base record. */
1545 clear_rec_inuse(ni->mi.mrec);
1546 ni->mi.dirty = true;
1547 err = mi_write(&ni->mi, 0);
1549 ntfs_mark_rec_free(sbi, ni->mi.rno);
1556 * Return: File name attribute by its value.
1558 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1559 const struct cpu_str *uni,
1560 const struct MFT_REF *home_dir,
1561 struct mft_inode **mi,
1562 struct ATTR_LIST_ENTRY **le)
1564 struct ATTRIB *attr = NULL;
1565 struct ATTR_FILE_NAME *fname;
1569 /* Enumerate all names. */
1571 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1575 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1579 if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1585 if (uni->len != fname->name_len)
1588 if (ntfs_cmp_names_cpu(uni, (struct le_str *)&fname->name_len, NULL,
1598 * Return: File name attribute with given type.
1600 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
1601 struct mft_inode **mi,
1602 struct ATTR_LIST_ENTRY **le)
1604 struct ATTRIB *attr = NULL;
1605 struct ATTR_FILE_NAME *fname;
1609 if (FILE_NAME_POSIX == name_type)
1612 /* Enumerate all names. */
1614 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1618 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1619 if (fname && name_type == fname->type)
1627 * Process compressed/sparsed in special way.
1628 * NOTE: You need to set ni->std_fa = new_fa
1629 * after this function to keep internal structures in consistency.
1631 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1633 struct ATTRIB *attr;
1634 struct mft_inode *mi;
1638 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1642 new_aflags = attr->flags;
1644 if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1645 new_aflags |= ATTR_FLAG_SPARSED;
1647 new_aflags &= ~ATTR_FLAG_SPARSED;
1649 if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1650 new_aflags |= ATTR_FLAG_COMPRESSED;
1652 new_aflags &= ~ATTR_FLAG_COMPRESSED;
1654 if (new_aflags == attr->flags)
1657 if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1658 (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1659 ntfs_inode_warn(&ni->vfs_inode,
1660 "file can't be sparsed and compressed");
1667 if (attr->nres.data_size) {
1670 "one can change sparsed/compressed only for empty files");
1674 /* Resize nonresident empty attribute in-place only. */
1675 new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED))
1676 ? (SIZEOF_NONRESIDENT_EX + 8)
1677 : (SIZEOF_NONRESIDENT + 8);
1679 if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1682 if (new_aflags & ATTR_FLAG_SPARSED) {
1683 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1684 /* Windows uses 16 clusters per frame but supports one cluster per frame too. */
1685 attr->nres.c_unit = 0;
1686 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1687 } else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1688 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1689 /* The only allowed: 16 clusters per frame. */
1690 attr->nres.c_unit = NTFS_LZNT_CUNIT;
1691 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1693 attr->name_off = SIZEOF_NONRESIDENT_LE;
1695 attr->nres.c_unit = 0;
1696 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1698 attr->nres.run_off = attr->name_off;
1700 attr->flags = new_aflags;
1709 * Buffer is at least 24 bytes.
1711 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
1714 const struct REPARSE_DATA_BUFFER *rp = NULL;
1717 typeof(rp->CompressReparseBuffer) *cmpr;
1719 static_assert(sizeof(struct REPARSE_DATA_BUFFER) <= 24);
1721 /* Try to estimate reparse point. */
1722 if (!attr->non_res) {
1723 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1724 } else if (le64_to_cpu(attr->nres.data_size) >=
1725 sizeof(struct REPARSE_DATA_BUFFER)) {
1726 struct runs_tree run;
1730 if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1731 !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1732 sizeof(struct REPARSE_DATA_BUFFER),
1741 return REPARSE_NONE;
1743 len = le16_to_cpu(rp->ReparseDataLength);
1744 switch (rp->ReparseTag) {
1745 case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
1746 break; /* Symbolic link. */
1747 case IO_REPARSE_TAG_MOUNT_POINT:
1748 break; /* Mount points and junctions. */
1749 case IO_REPARSE_TAG_SYMLINK:
1751 case IO_REPARSE_TAG_COMPRESS:
1753 * WOF - Windows Overlay Filter - Used to compress files with
1756 * Unlike native NTFS file compression, the Windows
1757 * Overlay Filter supports only read operations. This means
1758 * that it doesn't need to sector-align each compressed chunk,
1759 * so the compressed data can be packed more tightly together.
1760 * If you open the file for writing, the WOF just decompresses
1761 * the entire file, turning it back into a plain file.
1763 * Ntfs3 driver decompresses the entire file only on write or
1764 * change size requests.
1767 cmpr = &rp->CompressReparseBuffer;
1768 if (len < sizeof(*cmpr) ||
1769 cmpr->WofVersion != WOF_CURRENT_VERSION ||
1770 cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1771 cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1772 return REPARSE_NONE;
1775 switch (cmpr->CompressionFormat) {
1776 case WOF_COMPRESSION_XPRESS4K:
1779 case WOF_COMPRESSION_XPRESS8K:
1782 case WOF_COMPRESSION_XPRESS16K:
1785 case WOF_COMPRESSION_LZX32K:
1792 ni_set_ext_compress_bits(ni, bits);
1793 return REPARSE_COMPRESSED;
1795 case IO_REPARSE_TAG_DEDUP:
1796 ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1797 return REPARSE_DEDUPLICATED;
1800 if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1803 return REPARSE_NONE;
1806 /* Looks like normal symlink. */
1807 return REPARSE_LINK;
1811 * ni_fiemap - Helper for file_fiemap().
1814 * TODO: Less aggressive locks.
1816 int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
1817 __u64 vbo, __u64 len)
1820 struct ntfs_sb_info *sbi = ni->mi.sbi;
1821 u8 cluster_bits = sbi->cluster_bits;
1822 struct runs_tree *run;
1823 struct rw_semaphore *run_lock;
1824 struct ATTRIB *attr;
1825 CLST vcn = vbo >> cluster_bits;
1827 u64 valid = ni->i_valid;
1829 u64 end, alloc_size;
1834 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1835 run = &ni->dir.alloc_run;
1836 attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME,
1837 ARRAY_SIZE(I30_NAME), NULL, NULL);
1838 run_lock = &ni->dir.run_lock;
1840 run = &ni->file.run;
1841 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
1847 if (is_attr_compressed(attr)) {
1848 /* Unfortunately cp -r incorrectly treats compressed clusters. */
1852 "fiemap is not supported for compressed file (cp -r)");
1855 run_lock = &ni->file.run_lock;
1858 if (!attr || !attr->non_res) {
1859 err = fiemap_fill_next_extent(
1861 attr ? le32_to_cpu(attr->res.data_size) : 0,
1862 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST |
1863 FIEMAP_EXTENT_MERGED);
1868 alloc_size = le64_to_cpu(attr->nres.alloc_size);
1869 if (end > alloc_size)
1872 down_read(run_lock);
1876 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1878 CLST vcn_next = vcn;
1880 ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) &&
1888 down_write(run_lock);
1890 err = attr_load_runs_vcn(ni, attr->type,
1892 attr->name_len, run, vcn);
1895 down_read(run_lock);
1900 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1913 if (lcn == SPARSE_LCN) {
1915 vbo = (u64)vcn << cluster_bits;
1919 flags = FIEMAP_EXTENT_MERGED;
1920 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1922 } else if (is_attr_compressed(attr)) {
1925 err = attr_is_frame_compressed(
1926 ni, attr, vcn >> attr->nres.c_unit, &clst_data);
1929 if (clst_data < NTFS_LZNT_CLUSTERS)
1930 flags |= FIEMAP_EXTENT_ENCODED;
1931 } else if (is_attr_encrypted(attr)) {
1932 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1935 vbo = (u64)vcn << cluster_bits;
1936 bytes = (u64)clen << cluster_bits;
1937 lbo = (u64)lcn << cluster_bits;
1941 if (vbo + bytes >= end) {
1943 flags |= FIEMAP_EXTENT_LAST;
1946 if (vbo + bytes <= valid) {
1948 } else if (vbo >= valid) {
1949 flags |= FIEMAP_EXTENT_UNWRITTEN;
1951 /* vbo < valid && valid < vbo + bytes */
1952 u64 dlen = valid - vbo;
1954 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
1969 flags |= FIEMAP_EXTENT_UNWRITTEN;
1972 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
1992 * When decompressing, we typically obtain more than one page per reference.
1993 * We inject the additional pages into the page cache.
1995 int ni_readpage_cmpr(struct ntfs_inode *ni, struct page *page)
1998 struct ntfs_sb_info *sbi = ni->mi.sbi;
1999 struct address_space *mapping = page->mapping;
2000 pgoff_t index = page->index;
2001 u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT;
2002 struct page **pages = NULL; /* Array of at most 16 pages. stack? */
2005 u32 i, idx, frame_size, pages_per_frame;
2009 if (vbo >= ni->vfs_inode.i_size) {
2010 SetPageUptodate(page);
2015 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2016 /* Xpress or LZX. */
2017 frame_bits = ni_ext_compress_bits(ni);
2019 /* LZNT compression. */
2020 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2022 frame_size = 1u << frame_bits;
2023 frame = vbo >> frame_bits;
2024 frame_vbo = (u64)frame << frame_bits;
2025 idx = (vbo - frame_vbo) >> PAGE_SHIFT;
2027 pages_per_frame = frame_size >> PAGE_SHIFT;
2028 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2035 index = frame_vbo >> PAGE_SHIFT;
2036 gfp_mask = mapping_gfp_mask(mapping);
2038 for (i = 0; i < pages_per_frame; i++, index++) {
2042 pg = find_or_create_page(mapping, index, gfp_mask);
2050 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame);
2056 for (i = 0; i < pages_per_frame; i++) {
2065 /* At this point, err contains 0 or -EIO depending on the "critical" page. */
2072 #ifdef CONFIG_NTFS3_LZX_XPRESS
2074 * ni_decompress_file - Decompress LZX/Xpress compressed file.
2076 * Remove ATTR_DATA::WofCompressedData.
2077 * Remove ATTR_REPARSE.
2079 int ni_decompress_file(struct ntfs_inode *ni)
2081 struct ntfs_sb_info *sbi = ni->mi.sbi;
2082 struct inode *inode = &ni->vfs_inode;
2083 loff_t i_size = inode->i_size;
2084 struct address_space *mapping = inode->i_mapping;
2085 gfp_t gfp_mask = mapping_gfp_mask(mapping);
2086 struct page **pages = NULL;
2087 struct ATTR_LIST_ENTRY *le;
2088 struct ATTRIB *attr;
2089 CLST vcn, cend, lcn, clen, end;
2093 u32 i, frame_size, pages_per_frame, bytes;
2094 struct mft_inode *mi;
2097 /* Clusters for decompressed data. */
2098 cend = bytes_to_cluster(sbi, i_size);
2103 /* Check in advance. */
2104 if (cend > wnd_zeroes(&sbi->used.bitmap)) {
2109 frame_bits = ni_ext_compress_bits(ni);
2110 frame_size = 1u << frame_bits;
2111 pages_per_frame = frame_size >> PAGE_SHIFT;
2112 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2119 * Step 1: Decompress data and copy to new allocated clusters.
2122 for (vbo = 0; vbo < i_size; vbo += bytes) {
2126 if (vbo + frame_size > i_size) {
2127 bytes = i_size - vbo;
2128 nr_pages = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
2130 nr_pages = pages_per_frame;
2134 end = bytes_to_cluster(sbi, vbo + bytes);
2136 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2137 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2143 for (i = 0; i < pages_per_frame; i++, index++) {
2146 pg = find_or_create_page(mapping, index, gfp_mask);
2149 unlock_page(pages[i]);
2158 err = ni_read_frame(ni, vbo, pages, pages_per_frame);
2161 down_read(&ni->file.run_lock);
2162 err = ntfs_bio_pages(sbi, &ni->file.run, pages,
2163 nr_pages, vbo, bytes,
2165 up_read(&ni->file.run_lock);
2168 for (i = 0; i < pages_per_frame; i++) {
2169 unlock_page(pages[i]);
2181 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2186 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2190 if (attr->type == ATTR_REPARSE) {
2193 mi_get_ref(&ni->mi, &ref);
2194 ntfs_remove_reparse(sbi, 0, &ref);
2200 if (attr->type != ATTR_REPARSE &&
2201 (attr->type != ATTR_DATA ||
2202 attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2203 memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2206 svcn = le64_to_cpu(attr->nres.svcn);
2207 evcn = le64_to_cpu(attr->nres.evcn);
2209 if (evcn + 1 <= svcn)
2212 asize = le32_to_cpu(attr->size);
2213 roff = le16_to_cpu(attr->nres.run_off);
2215 /*run==1 Means unpack and deallocate. */
2216 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2217 Add2Ptr(attr, roff), asize - roff);
2221 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
2223 err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2229 * Step 4: Remove ATTR_REPARSE.
2231 err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2236 * Step 5: Remove sparse flag from data attribute.
2238 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2244 if (attr->non_res && is_attr_sparsed(attr)) {
2245 /* Sparsed attribute header is 8 bytes bigger than normal. */
2246 struct MFT_REC *rec = mi->mrec;
2247 u32 used = le32_to_cpu(rec->used);
2248 u32 asize = le32_to_cpu(attr->size);
2249 u16 roff = le16_to_cpu(attr->nres.run_off);
2250 char *rbuf = Add2Ptr(attr, roff);
2252 memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2253 attr->size = cpu_to_le32(asize - 8);
2254 attr->flags &= ~ATTR_FLAG_SPARSED;
2255 attr->nres.run_off = cpu_to_le16(roff - 8);
2256 attr->nres.c_unit = 0;
2257 rec->used = cpu_to_le32(used - 8);
2259 ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2260 FILE_ATTRIBUTE_REPARSE_POINT);
2262 mark_inode_dirty(inode);
2265 /* Clear cached flag. */
2266 ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2267 if (ni->file.offs_page) {
2268 put_page(ni->file.offs_page);
2269 ni->file.offs_page = NULL;
2271 mapping->a_ops = &ntfs_aops;
2276 make_bad_inode(inode);
2277 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
2284 * decompress_lzx_xpress - External compression LZX/Xpress.
2286 static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2287 size_t cmpr_size, void *unc, size_t unc_size,
2293 if (cmpr_size == unc_size) {
2294 /* Frame not compressed. */
2295 memcpy(unc, cmpr, unc_size);
2300 if (frame_size == 0x8000) {
2301 mutex_lock(&sbi->compress.mtx_lzx);
2302 /* LZX: Frame compressed. */
2303 ctx = sbi->compress.lzx;
2305 /* Lazy initialize LZX decompress context. */
2306 ctx = lzx_allocate_decompressor();
2312 sbi->compress.lzx = ctx;
2315 if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2316 /* Treat all errors as "invalid argument". */
2320 mutex_unlock(&sbi->compress.mtx_lzx);
2322 /* XPRESS: Frame compressed. */
2323 mutex_lock(&sbi->compress.mtx_xpress);
2324 ctx = sbi->compress.xpress;
2326 /* Lazy initialize Xpress decompress context. */
2327 ctx = xpress_allocate_decompressor();
2333 sbi->compress.xpress = ctx;
2336 if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2337 /* Treat all errors as "invalid argument". */
2341 mutex_unlock(&sbi->compress.mtx_xpress);
2350 * Pages - Array of locked pages.
2352 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2353 u32 pages_per_frame)
2356 struct ntfs_sb_info *sbi = ni->mi.sbi;
2357 u8 cluster_bits = sbi->cluster_bits;
2358 char *frame_ondisk = NULL;
2359 char *frame_mem = NULL;
2360 struct page **pages_disk = NULL;
2361 struct ATTR_LIST_ENTRY *le = NULL;
2362 struct runs_tree *run = &ni->file.run;
2363 u64 valid_size = ni->i_valid;
2366 u32 frame_size, i, npages_disk, ondisk_size;
2368 struct ATTRIB *attr;
2369 CLST frame, clst_data;
2372 * To simplify decompress algorithm do vmap for source
2375 for (i = 0; i < pages_per_frame; i++)
2378 frame_size = pages_per_frame << PAGE_SHIFT;
2379 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2385 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2391 if (!attr->non_res) {
2392 u32 data_size = le32_to_cpu(attr->res.data_size);
2394 memset(frame_mem, 0, frame_size);
2395 if (frame_vbo < data_size) {
2396 ondisk_size = data_size - frame_vbo;
2397 memcpy(frame_mem, resident_data(attr) + frame_vbo,
2398 min(ondisk_size, frame_size));
2404 if (frame_vbo >= valid_size) {
2405 memset(frame_mem, 0, frame_size);
2410 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2411 #ifndef CONFIG_NTFS3_LZX_XPRESS
2415 u32 frame_bits = ni_ext_compress_bits(ni);
2416 u64 frame64 = frame_vbo >> frame_bits;
2417 u64 frames, vbo_data;
2419 if (frame_size != (1u << frame_bits)) {
2423 switch (frame_size) {
2430 /* Unknown compression. */
2435 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2436 ARRAY_SIZE(WOF_NAME), NULL, NULL);
2440 "external compressed file should contains data attribute \"WofCompressedData\"");
2445 if (!attr->non_res) {
2455 frames = (ni->vfs_inode.i_size - 1) >> frame_bits;
2457 err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2458 frame_bits, &ondisk_size, &vbo_data);
2462 if (frame64 == frames) {
2463 unc_size = 1 + ((ni->vfs_inode.i_size - 1) &
2465 ondisk_size = attr_size(attr) - vbo_data;
2467 unc_size = frame_size;
2470 if (ondisk_size > frame_size) {
2475 if (!attr->non_res) {
2476 if (vbo_data + ondisk_size >
2477 le32_to_cpu(attr->res.data_size)) {
2482 err = decompress_lzx_xpress(
2483 sbi, Add2Ptr(resident_data(attr), vbo_data),
2484 ondisk_size, frame_mem, unc_size, frame_size);
2487 vbo_disk = vbo_data;
2488 /* Load all runs to read [vbo_disk-vbo_to). */
2489 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2490 ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2491 vbo_data + ondisk_size);
2494 npages_disk = (ondisk_size + (vbo_disk & (PAGE_SIZE - 1)) +
2498 } else if (is_attr_compressed(attr)) {
2499 /* LZNT compression. */
2500 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2505 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2510 down_write(&ni->file.run_lock);
2511 run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2512 frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2513 err = attr_is_frame_compressed(ni, attr, frame, &clst_data);
2514 up_write(&ni->file.run_lock);
2519 memset(frame_mem, 0, frame_size);
2523 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2524 ondisk_size = clst_data << cluster_bits;
2526 if (clst_data >= NTFS_LZNT_CLUSTERS) {
2527 /* Frame is not compressed. */
2528 down_read(&ni->file.run_lock);
2529 err = ntfs_bio_pages(sbi, run, pages, pages_per_frame,
2530 frame_vbo, ondisk_size,
2532 up_read(&ni->file.run_lock);
2535 vbo_disk = frame_vbo;
2536 npages_disk = (ondisk_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2538 __builtin_unreachable();
2543 pages_disk = kzalloc(npages_disk * sizeof(struct page *), GFP_NOFS);
2549 for (i = 0; i < npages_disk; i++) {
2550 pg = alloc_page(GFP_KERNEL);
2560 /* Read 'ondisk_size' bytes from disk. */
2561 down_read(&ni->file.run_lock);
2562 err = ntfs_bio_pages(sbi, run, pages_disk, npages_disk, vbo_disk,
2563 ondisk_size, REQ_OP_READ);
2564 up_read(&ni->file.run_lock);
2569 * To simplify decompress algorithm do vmap for source and target pages.
2571 frame_ondisk = vmap(pages_disk, npages_disk, VM_MAP, PAGE_KERNEL_RO);
2572 if (!frame_ondisk) {
2577 /* Decompress: Frame_ondisk -> frame_mem. */
2578 #ifdef CONFIG_NTFS3_LZX_XPRESS
2579 if (run != &ni->file.run) {
2581 err = decompress_lzx_xpress(
2582 sbi, frame_ondisk + (vbo_disk & (PAGE_SIZE - 1)),
2583 ondisk_size, frame_mem, unc_size, frame_size);
2587 /* LZNT - Native NTFS compression. */
2588 unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2590 if ((ssize_t)unc_size < 0)
2592 else if (!unc_size || unc_size > frame_size)
2595 if (!err && valid_size < frame_vbo + frame_size) {
2596 size_t ok = valid_size - frame_vbo;
2598 memset(frame_mem + ok, 0, frame_size - ok);
2601 vunmap(frame_ondisk);
2604 for (i = 0; i < npages_disk; i++) {
2615 #ifdef CONFIG_NTFS3_LZX_XPRESS
2616 if (run != &ni->file.run)
2622 for (i = 0; i < pages_per_frame; i++) {
2626 SetPageUptodate(pg);
2635 * Pages - Array of locked pages.
2637 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2638 u32 pages_per_frame)
2641 struct ntfs_sb_info *sbi = ni->mi.sbi;
2642 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2643 u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2644 u64 frame_vbo = (u64)pages[0]->index << PAGE_SHIFT;
2645 CLST frame = frame_vbo >> frame_bits;
2646 char *frame_ondisk = NULL;
2647 struct page **pages_disk = NULL;
2648 struct ATTR_LIST_ENTRY *le = NULL;
2650 struct ATTRIB *attr;
2651 struct mft_inode *mi;
2654 size_t compr_size, ondisk_size;
2657 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2663 if (WARN_ON(!is_attr_compressed(attr))) {
2668 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2673 if (!attr->non_res) {
2674 down_write(&ni->file.run_lock);
2675 err = attr_make_nonresident(ni, attr, le, mi,
2676 le32_to_cpu(attr->res.data_size),
2677 &ni->file.run, &attr, pages[0]);
2678 up_write(&ni->file.run_lock);
2683 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2688 pages_disk = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2694 for (i = 0; i < pages_per_frame; i++) {
2695 pg = alloc_page(GFP_KERNEL);
2705 /* To simplify compress algorithm do vmap for source and target pages. */
2706 frame_ondisk = vmap(pages_disk, pages_per_frame, VM_MAP, PAGE_KERNEL);
2707 if (!frame_ondisk) {
2712 for (i = 0; i < pages_per_frame; i++)
2715 /* Map in-memory frame for read-only. */
2716 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2722 mutex_lock(&sbi->compress.mtx_lznt);
2724 if (!sbi->compress.lznt) {
2726 * LZNT implements two levels of compression:
2727 * 0 - Standard compression
2728 * 1 - Best compression, requires a lot of cpu
2731 lznt = get_lznt_ctx(0);
2733 mutex_unlock(&sbi->compress.mtx_lznt);
2738 sbi->compress.lznt = lznt;
2742 /* Compress: frame_mem -> frame_ondisk */
2743 compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2744 frame_size, sbi->compress.lznt);
2745 mutex_unlock(&sbi->compress.mtx_lznt);
2748 if (compr_size + sbi->cluster_size > frame_size) {
2749 /* Frame is not compressed. */
2750 compr_size = frame_size;
2751 ondisk_size = frame_size;
2752 } else if (compr_size) {
2753 /* Frame is compressed. */
2754 ondisk_size = ntfs_up_cluster(sbi, compr_size);
2755 memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2757 /* Frame is sparsed. */
2761 down_write(&ni->file.run_lock);
2762 run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2763 err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2764 up_write(&ni->file.run_lock);
2771 down_read(&ni->file.run_lock);
2772 err = ntfs_bio_pages(sbi, &ni->file.run,
2773 ondisk_size < frame_size ? pages_disk : pages,
2774 pages_per_frame, frame_vbo, ondisk_size,
2776 up_read(&ni->file.run_lock);
2782 for (i = 0; i < pages_per_frame; i++)
2785 vunmap(frame_ondisk);
2787 for (i = 0; i < pages_per_frame; i++) {
2801 * ni_remove_name - Removes name 'de' from MFT and from directory.
2802 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2804 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2805 struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2808 struct ntfs_sb_info *sbi = ni->mi.sbi;
2809 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2810 struct ATTR_FILE_NAME *fname;
2811 struct ATTR_LIST_ENTRY *le;
2812 struct mft_inode *mi;
2813 u16 de_key_size = le16_to_cpu(de->key_size);
2818 /* Find name in record. */
2819 mi_get_ref(&dir_ni->mi, &de_name->home);
2821 fname = ni_fname_name(ni, (struct cpu_str *)&de_name->name_len,
2822 &de_name->home, &mi, &le);
2826 memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2827 name_type = paired_name(fname->type);
2829 /* Mark ntfs as dirty. It will be cleared at umount. */
2830 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2832 /* Step 1: Remove name from directory. */
2833 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2837 /* Step 2: Remove name from MFT. */
2838 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2842 /* Get paired name. */
2843 fname = ni_fname_type(ni, name_type, &mi, &le);
2845 u16 de2_key_size = fname_full_size(fname);
2847 *de2 = Add2Ptr(de, 1024);
2848 (*de2)->key_size = cpu_to_le16(de2_key_size);
2850 memcpy(*de2 + 1, fname, de2_key_size);
2852 /* Step 3: Remove paired name from directory. */
2853 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2858 /* Step 4: Remove paired name from MFT. */
2859 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2867 * ni_remove_name_undo - Paired function for ni_remove_name.
2869 * Return: True if ok
2871 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2872 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2874 struct ntfs_sb_info *sbi = ni->mi.sbi;
2875 struct ATTRIB *attr;
2876 u16 de_key_size = de2 ? le16_to_cpu(de2->key_size) : 0;
2878 switch (undo_step) {
2880 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2881 &attr, NULL, NULL)) {
2884 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2886 mi_get_ref(&ni->mi, &de2->ref);
2887 de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2888 sizeof(struct NTFS_DE));
2892 if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL,
2899 de_key_size = le16_to_cpu(de->key_size);
2901 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2902 &attr, NULL, NULL)) {
2906 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2907 mi_get_ref(&ni->mi, &de->ref);
2909 if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1)) {
2918 * ni_add_name - Add new name in MFT and in directory.
2920 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2924 struct ATTRIB *attr;
2925 struct ATTR_LIST_ENTRY *le;
2926 struct mft_inode *mi;
2927 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2928 u16 de_key_size = le16_to_cpu(de->key_size);
2930 mi_get_ref(&ni->mi, &de->ref);
2931 mi_get_ref(&dir_ni->mi, &de_name->home);
2933 /* Insert new name in MFT. */
2934 err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
2939 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
2941 /* Insert new name in directory. */
2942 err = indx_insert_entry(&dir_ni->dir, dir_ni, de, ni->mi.sbi, NULL, 0);
2944 ni_remove_attr_le(ni, attr, mi, le);
2950 * ni_rename - Remove one name and insert new name.
2952 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
2953 struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de,
2957 struct NTFS_DE *de2 = NULL;
2961 * There are two possible ways to rename:
2962 * 1) Add new name and remove old name.
2963 * 2) Remove old name and add new name.
2965 * In most cases (not all!) adding new name in MFT and in directory can
2966 * allocate additional cluster(s).
2967 * Second way may result to bad inode if we can't add new name
2968 * and then can't restore (add) old name.
2972 * Way 1 - Add new + remove old.
2974 err = ni_add_name(new_dir_ni, ni, new_de);
2976 err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2977 if (err && ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo))
2982 * Way 2 - Remove old + add new.
2985 * err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2987 * err = ni_add_name(new_dir_ni, ni, new_de);
2988 * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
2997 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
2999 bool ni_is_dirty(struct inode *inode)
3001 struct ntfs_inode *ni = ntfs_i(inode);
3002 struct rb_node *node;
3004 if (ni->mi.dirty || ni->attr_list.dirty ||
3005 (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3008 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
3009 if (rb_entry(node, struct mft_inode, node)->dirty)
3019 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
3021 static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
3024 struct ATTRIB *attr;
3025 struct mft_inode *mi;
3026 struct ATTR_LIST_ENTRY *le = NULL;
3027 struct ntfs_sb_info *sbi = ni->mi.sbi;
3028 struct super_block *sb = sbi->sb;
3029 bool re_dirty = false;
3031 if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
3032 dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
3034 dup->alloc_size = 0;
3037 dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
3039 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
3042 dup->alloc_size = dup->data_size = 0;
3043 } else if (!attr->non_res) {
3044 u32 data_size = le32_to_cpu(attr->res.data_size);
3046 dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
3047 dup->data_size = cpu_to_le64(data_size);
3049 u64 new_valid = ni->i_valid;
3050 u64 data_size = le64_to_cpu(attr->nres.data_size);
3053 dup->alloc_size = is_attr_ext(attr)
3054 ? attr->nres.total_size
3055 : attr->nres.alloc_size;
3056 dup->data_size = attr->nres.data_size;
3058 if (new_valid > data_size)
3059 new_valid = data_size;
3061 valid_le = cpu_to_le64(new_valid);
3062 if (valid_le != attr->nres.valid_size) {
3063 attr->nres.valid_size = valid_le;
3069 /* TODO: Fill reparse info. */
3073 if (ni->ni_flags & NI_FLAG_EA) {
3074 attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3077 const struct EA_INFO *info;
3079 info = resident_data_ex(attr, sizeof(struct EA_INFO));
3080 dup->ea_size = info->size_pack;
3087 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3090 struct ATTR_FILE_NAME *fname;
3092 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
3093 if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
3096 /* ntfs_iget5 may sleep. */
3097 dir = ntfs_iget5(sb, &fname->home, NULL);
3101 "failed to open parent directory r=%lx to update",
3102 (long)ino_get(&fname->home));
3106 if (!is_bad_inode(dir)) {
3107 struct ntfs_inode *dir_ni = ntfs_i(dir);
3109 if (!ni_trylock(dir_ni)) {
3112 indx_update_dup(dir_ni, sbi, fname, dup, sync);
3114 memcpy(&fname->dup, dup, sizeof(fname->dup));
3125 * ni_write_inode - Write MFT base record and all subrecords to disk.
3127 int ni_write_inode(struct inode *inode, int sync, const char *hint)
3130 struct ntfs_inode *ni = ntfs_i(inode);
3131 struct super_block *sb = inode->i_sb;
3132 struct ntfs_sb_info *sbi = sb->s_fs_info;
3133 bool re_dirty = false;
3134 struct ATTR_STD_INFO *std;
3135 struct rb_node *node, *next;
3136 struct NTFS_DUP_INFO dup;
3138 if (is_bad_inode(inode) || sb_rdonly(sb))
3141 if (!ni_trylock(ni)) {
3142 /* 'ni' is under modification, skip for now. */
3143 mark_inode_dirty_sync(inode);
3147 if (is_rec_inuse(ni->mi.mrec) &&
3148 !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3149 bool modified = false;
3151 /* Update times in standard attribute. */
3158 /* Update the access times if they have changed. */
3159 dup.m_time = kernel2nt(&inode->i_mtime);
3160 if (std->m_time != dup.m_time) {
3161 std->m_time = dup.m_time;
3165 dup.c_time = kernel2nt(&inode->i_ctime);
3166 if (std->c_time != dup.c_time) {
3167 std->c_time = dup.c_time;
3171 dup.a_time = kernel2nt(&inode->i_atime);
3172 if (std->a_time != dup.a_time) {
3173 std->a_time = dup.a_time;
3177 dup.fa = ni->std_fa;
3178 if (std->fa != dup.fa) {
3184 ni->mi.dirty = true;
3186 if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
3187 (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3188 /* Avoid __wait_on_freeing_inode(inode). */
3189 && (sb->s_flags & SB_ACTIVE)) {
3190 dup.cr_time = std->cr_time;
3191 /* Not critical if this function fail. */
3192 re_dirty = ni_update_parent(ni, &dup, sync);
3195 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3197 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3200 /* Update attribute list. */
3201 if (ni->attr_list.size && ni->attr_list.dirty) {
3202 if (inode->i_ino != MFT_REC_MFT || sync) {
3203 err = ni_try_remove_attr_list(ni);
3208 err = al_update(ni);
3214 for (node = rb_first(&ni->mi_tree); node; node = next) {
3215 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3218 next = rb_next(node);
3223 is_empty = !mi_enum_attr(mi, NULL);
3226 clear_rec_inuse(mi->mrec);
3228 err2 = mi_write(mi, sync);
3233 ntfs_mark_rec_free(sbi, mi->rno);
3234 rb_erase(node, &ni->mi_tree);
3240 err2 = mi_write(&ni->mi, sync);
3248 ntfs_err(sb, "%s r=%lx failed, %d.", hint, inode->i_ino, err);
3249 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3254 mark_inode_dirty_sync(inode);