1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/fiemap.h>
10 #include <linux/minmax.h>
11 #include <linux/vmalloc.h>
16 #ifdef CONFIG_NTFS3_LZX_XPRESS
20 static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
21 CLST ino, struct rb_node *ins)
23 struct rb_node **p = &tree->rb_node;
24 struct rb_node *pr = NULL;
30 mi = rb_entry(pr, struct mft_inode, node);
33 else if (mi->rno < ino)
42 rb_link_node(ins, pr, p);
43 rb_insert_color(ins, tree);
44 return rb_entry(ins, struct mft_inode, node);
48 * ni_find_mi - Find mft_inode by record number.
50 static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
52 return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
56 * ni_add_mi - Add new mft_inode into ntfs_inode.
58 static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
60 ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
64 * ni_remove_mi - Remove mft_inode from ntfs_inode.
66 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
68 rb_erase(&mi->node, &ni->mi_tree);
72 * ni_std - Return: Pointer into std_info from primary record.
74 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
76 const struct ATTRIB *attr;
78 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
79 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO))
86 * Return: Pointer into std_info from primary record.
88 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
90 const struct ATTRIB *attr;
92 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
94 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5))
99 * ni_clear - Clear resources allocated by ntfs_inode.
101 void ni_clear(struct ntfs_inode *ni)
103 struct rb_node *node;
105 if (!ni->vfs_inode.i_nlink && is_rec_inuse(ni->mi.mrec))
110 for (node = rb_first(&ni->mi_tree); node;) {
111 struct rb_node *next = rb_next(node);
112 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
114 rb_erase(node, &ni->mi_tree);
119 /* Bad inode always has mode == S_IFREG. */
120 if (ni->ni_flags & NI_FLAG_DIR)
121 indx_clear(&ni->dir);
123 run_close(&ni->file.run);
124 #ifdef CONFIG_NTFS3_LZX_XPRESS
125 if (ni->file.offs_page) {
126 /* On-demand allocated page for offsets. */
127 put_page(ni->file.offs_page);
128 ni->file.offs_page = NULL;
137 * ni_load_mi_ex - Find mft_inode by record number.
139 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
144 r = ni_find_mi(ni, rno);
148 err = mi_get(ni->mi.sbi, rno, &r);
161 * ni_load_mi - Load mft_inode corresponded list_entry.
163 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
164 struct mft_inode **mi)
173 rno = ino_get(&le->ref);
174 if (rno == ni->mi.rno) {
178 return ni_load_mi_ex(ni, rno, mi);
184 * Return: Attribute and record this attribute belongs to.
186 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
187 struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
188 const __le16 *name, u8 name_len, const CLST *vcn,
189 struct mft_inode **mi)
191 struct ATTR_LIST_ENTRY *le;
194 if (!ni->attr_list.size ||
195 (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
201 /* Look for required attribute in primary record. */
202 return mi_find_attr(&ni->mi, attr, type, name, name_len, NULL);
205 /* First look for list entry of required type. */
206 le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
213 /* Load record that contains this attribute. */
214 if (ni_load_mi(ni, le, &m))
217 /* Look for required attribute. */
218 attr = mi_find_attr(m, NULL, type, name, name_len, &le->id);
223 if (!attr->non_res) {
229 } else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
230 *vcn > le64_to_cpu(attr->nres.evcn)) {
239 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR);
244 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
246 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
247 struct ATTR_LIST_ENTRY **le,
248 struct mft_inode **mi)
250 struct mft_inode *mi2;
251 struct ATTR_LIST_ENTRY *le2;
253 /* Do we have an attribute list? */
254 if (!ni->attr_list.size) {
258 /* Enum attributes in primary record. */
259 return mi_enum_attr(&ni->mi, attr);
262 /* Get next list entry. */
263 le2 = *le = al_enumerate(ni, attr ? *le : NULL);
267 /* Load record that contains the required attribute. */
268 if (ni_load_mi(ni, le2, &mi2))
274 /* Find attribute in loaded record. */
275 return rec_find_attr_le(mi2, le2);
279 * ni_load_attr - Load attribute that contains given VCN.
281 struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
282 const __le16 *name, u8 name_len, CLST vcn,
283 struct mft_inode **pmi)
285 struct ATTR_LIST_ENTRY *le;
287 struct mft_inode *mi;
288 struct ATTR_LIST_ENTRY *next;
290 if (!ni->attr_list.size) {
293 return mi_find_attr(&ni->mi, NULL, type, name, name_len, NULL);
296 le = al_find_ex(ni, NULL, type, name, name_len, NULL);
301 * Unfortunately ATTR_LIST_ENTRY contains only start VCN.
302 * So to find the ATTRIB segment that contains 'vcn' we should
303 * enumerate some entries.
307 next = al_find_ex(ni, le, type, name, name_len, NULL);
308 if (!next || le64_to_cpu(next->vcn) > vcn)
313 if (ni_load_mi(ni, le, &mi))
319 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
326 if (le64_to_cpu(attr->nres.svcn) <= vcn &&
327 vcn <= le64_to_cpu(attr->nres.evcn))
334 * ni_load_all_mi - Load all subrecords.
336 int ni_load_all_mi(struct ntfs_inode *ni)
339 struct ATTR_LIST_ENTRY *le;
341 if (!ni->attr_list.size)
346 while ((le = al_enumerate(ni, le))) {
347 CLST rno = ino_get(&le->ref);
349 if (rno == ni->mi.rno)
352 err = ni_load_mi_ex(ni, rno, NULL);
361 * ni_add_subrecord - Allocate + format + attach a new subrecord.
363 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
367 m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
371 if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
376 mi_get_ref(&ni->mi, &m->mrec->parent_ref);
384 * ni_remove_attr - Remove all attributes for the given type/name/id.
386 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
387 const __le16 *name, size_t name_len, bool base_only,
392 struct ATTR_LIST_ENTRY *le;
393 struct mft_inode *mi;
397 if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
398 attr = mi_find_attr(&ni->mi, NULL, type, name, name_len, id);
402 mi_remove_attr(ni, &ni->mi, attr);
406 type_in = le32_to_cpu(type);
410 le = al_enumerate(ni, le);
415 diff = le32_to_cpu(le->type) - type_in;
422 if (le->name_len != name_len)
426 memcmp(le_name(le), name, name_len * sizeof(short)))
429 if (id && le->id != *id)
431 err = ni_load_mi(ni, le, &mi);
435 al_remove_le(ni, le);
437 attr = mi_find_attr(mi, NULL, type, name, name_len, id);
441 mi_remove_attr(ni, mi, attr);
443 if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
450 * ni_ins_new_attr - Insert the attribute into record.
452 * Return: Not full constructed attribute or NULL if not possible to create.
454 static struct ATTRIB *
455 ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
456 struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
457 const __le16 *name, u8 name_len, u32 asize, u16 name_off,
458 CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
462 bool le_added = false;
465 mi_get_ref(mi, &ref);
467 if (type != ATTR_LIST && !le && ni->attr_list.size) {
468 err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
471 /* No memory or no space. */
477 * al_add_le -> attr_set_size (list) -> ni_expand_list
478 * which moves some attributes out of primary record
479 * this means that name may point into moved memory
480 * reinit 'name' from le.
485 attr = mi_insert_attr(mi, type, name, name_len, asize, name_off);
488 al_remove_le(ni, le);
492 if (type == ATTR_LIST) {
493 /* Attr list is not in list entry array. */
500 /* Update ATTRIB Id and record reference. */
502 ni->attr_list.dirty = true;
514 * Random write access to sparsed or compressed file may result to
515 * not optimized packed runs.
516 * Here is the place to optimize it.
518 static int ni_repack(struct ntfs_inode *ni)
521 struct ntfs_sb_info *sbi = ni->mi.sbi;
522 struct mft_inode *mi, *mi_p = NULL;
523 struct ATTRIB *attr = NULL, *attr_p;
524 struct ATTR_LIST_ENTRY *le = NULL, *le_p;
526 u8 cluster_bits = sbi->cluster_bits;
527 CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
528 u32 roff, rs = sbi->record_size;
529 struct runs_tree run;
533 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
537 svcn = le64_to_cpu(attr->nres.svcn);
538 if (svcn != le64_to_cpu(le->vcn)) {
544 alloc = le64_to_cpu(attr->nres.alloc_size) >>
547 } else if (svcn != evcn + 1) {
552 evcn = le64_to_cpu(attr->nres.evcn);
554 if (svcn > evcn + 1) {
560 /* Do not try if not enogh free space. */
561 if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
564 /* Do not try if last attribute segment. */
565 if (evcn + 1 == alloc)
570 roff = le16_to_cpu(attr->nres.run_off);
572 if (roff > le32_to_cpu(attr->size)) {
577 err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
579 le32_to_cpu(attr->size) - roff);
594 * Run contains data from two records: mi_p and mi
595 * Try to pack in one.
597 err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
601 next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
603 if (next_svcn >= evcn + 1) {
604 /* We can remove this attribute segment. */
605 al_remove_le(ni, le);
606 mi_remove_attr(NULL, mi, attr);
611 attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
613 ni->attr_list.dirty = true;
615 if (evcn + 1 == alloc) {
616 err = mi_pack_runs(mi, attr, &run,
617 evcn + 1 - next_svcn);
627 run_truncate_head(&run, next_svcn);
632 ntfs_inode_warn(&ni->vfs_inode, "repack problem");
633 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
635 /* Pack loaded but not packed runs. */
637 mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
645 * ni_try_remove_attr_list
647 * Can we remove attribute list?
648 * Check the case when primary record contains enough space for all attributes.
650 static int ni_try_remove_attr_list(struct ntfs_inode *ni)
653 struct ntfs_sb_info *sbi = ni->mi.sbi;
654 struct ATTRIB *attr, *attr_list, *attr_ins;
655 struct ATTR_LIST_ENTRY *le;
656 struct mft_inode *mi;
659 struct MFT_REC *mrec;
662 if (!ni->attr_list.dirty)
669 attr_list = mi_find_attr(&ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
673 asize = le32_to_cpu(attr_list->size);
675 /* Free space in primary record without attribute list. */
676 free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
677 mi_get_ref(&ni->mi, &ref);
680 while ((le = al_enumerate(ni, le))) {
681 if (!memcmp(&le->ref, &ref, sizeof(ref)))
687 mi = ni_find_mi(ni, ino_get(&le->ref));
691 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
692 le->name_len, &le->id);
696 asize = le32_to_cpu(attr->size);
703 /* Make a copy of primary record to restore if error. */
704 mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
706 return 0; /* Not critical. */
708 /* It seems that attribute list can be removed from primary record. */
709 mi_remove_attr(NULL, &ni->mi, attr_list);
712 * Repeat the cycle above and copy all attributes to primary record.
713 * Do not remove original attributes from subrecords!
714 * It should be success!
717 while ((le = al_enumerate(ni, le))) {
718 if (!memcmp(&le->ref, &ref, sizeof(ref)))
721 mi = ni_find_mi(ni, ino_get(&le->ref));
723 /* Should never happened, 'cause already checked. */
727 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
728 le->name_len, &le->id);
730 /* Should never happened, 'cause already checked. */
733 asize = le32_to_cpu(attr->size);
735 /* Insert into primary record. */
736 attr_ins = mi_insert_attr(&ni->mi, le->type, le_name(le),
738 le16_to_cpu(attr->name_off));
741 * No space in primary record (already checked).
746 /* Copy all except id. */
748 memcpy(attr_ins, attr, asize);
753 * Repeat the cycle above and remove all attributes from subrecords.
756 while ((le = al_enumerate(ni, le))) {
757 if (!memcmp(&le->ref, &ref, sizeof(ref)))
760 mi = ni_find_mi(ni, ino_get(&le->ref));
764 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
765 le->name_len, &le->id);
769 /* Remove from original record. */
770 mi_remove_attr(NULL, mi, attr);
773 run_deallocate(sbi, &ni->attr_list.run, true);
774 run_close(&ni->attr_list.run);
775 ni->attr_list.size = 0;
776 kfree(ni->attr_list.le);
777 ni->attr_list.le = NULL;
778 ni->attr_list.dirty = false;
783 /* Restore primary record. */
784 swap(mrec, ni->mi.mrec);
790 * ni_create_attr_list - Generates an attribute list for this primary record.
792 int ni_create_attr_list(struct ntfs_inode *ni)
794 struct ntfs_sb_info *sbi = ni->mi.sbi;
798 struct ATTRIB *arr_move[7];
799 struct ATTR_LIST_ENTRY *le, *le_b[7];
803 struct mft_inode *mi;
804 u32 free_b, nb, to_free, rs;
807 is_mft = ni->mi.rno == MFT_REC_MFT;
809 rs = sbi->record_size;
812 * Skip estimating exact memory requirement.
813 * Looks like one record_size is always enough.
815 le = kmalloc(al_aligned(rs), GFP_NOFS);
821 mi_get_ref(&ni->mi, &le->ref);
822 ni->attr_list.le = le;
829 for (; (attr = mi_enum_attr(&ni->mi, attr)); le = Add2Ptr(le, sz)) {
830 sz = le_size(attr->name_len);
831 le->type = attr->type;
832 le->size = cpu_to_le16(sz);
833 le->name_len = attr->name_len;
834 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
836 if (le != ni->attr_list.le)
837 le->ref = ni->attr_list.le->ref;
841 memcpy(le->name, attr_name(attr),
842 sizeof(short) * attr->name_len);
843 else if (attr->type == ATTR_STD)
845 else if (attr->type == ATTR_LIST)
847 else if (is_mft && attr->type == ATTR_DATA)
850 if (!nb || nb < ARRAY_SIZE(arr_move)) {
852 arr_move[nb++] = attr;
853 free_b += le32_to_cpu(attr->size);
857 lsize = PtrOffset(ni->attr_list.le, le);
858 ni->attr_list.size = lsize;
860 to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
866 if (to_free > free_b) {
872 /* Allocate child MFT. */
873 err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
877 /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
878 while (to_free > 0) {
879 struct ATTRIB *b = arr_move[--nb];
880 u32 asize = le32_to_cpu(b->size);
881 u16 name_off = le16_to_cpu(b->name_off);
883 attr = mi_insert_attr(mi, b->type, Add2Ptr(b, name_off),
884 b->name_len, asize, name_off);
887 mi_get_ref(mi, &le_b[nb]->ref);
888 le_b[nb]->id = attr->id;
890 /* Copy all except id. */
891 memcpy(attr, b, asize);
892 attr->id = le_b[nb]->id;
894 /* Remove from primary record. */
895 WARN_ON(!mi_remove_attr(NULL, &ni->mi, b));
897 if (to_free <= asize)
903 attr = mi_insert_attr(&ni->mi, ATTR_LIST, NULL, 0,
904 lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
909 attr->res.data_size = cpu_to_le32(lsize);
910 attr->res.data_off = SIZEOF_RESIDENT_LE;
914 memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
916 ni->attr_list.dirty = false;
918 mark_inode_dirty(&ni->vfs_inode);
922 kfree(ni->attr_list.le);
923 ni->attr_list.le = NULL;
924 ni->attr_list.size = 0;
931 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
933 static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
934 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
935 u32 asize, CLST svcn, u16 name_off, bool force_ext,
936 struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
937 struct ATTR_LIST_ENTRY **ins_le)
940 struct mft_inode *mi;
943 struct rb_node *node;
945 bool is_mft, is_mft_data;
946 struct ntfs_sb_info *sbi = ni->mi.sbi;
948 is_mft = ni->mi.rno == MFT_REC_MFT;
949 is_mft_data = is_mft && type == ATTR_DATA && !name_len;
951 if (asize > sbi->max_bytes_per_attr) {
957 * Standard information and attr_list cannot be made external.
958 * The Log File cannot have any external attributes.
960 if (type == ATTR_STD || type == ATTR_LIST ||
961 ni->mi.rno == MFT_REC_LOG) {
966 /* Create attribute list if it is not already existed. */
967 if (!ni->attr_list.size) {
968 err = ni_create_attr_list(ni);
973 vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
978 /* Load all subrecords into memory. */
979 err = ni_load_all_mi(ni);
983 /* Check each of loaded subrecord. */
984 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
985 mi = rb_entry(node, struct mft_inode, node);
988 (mi_enum_attr(mi, NULL) ||
989 vbo <= ((u64)mi->rno << sbi->record_bits))) {
990 /* We can't accept this record 'cause MFT's bootstrapping. */
994 mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
996 * This child record already has a ATTR_DATA.
997 * So it can't accept any other records.
1002 if ((type != ATTR_NAME || name_len) &&
1003 mi_find_attr(mi, NULL, type, name, name_len, NULL)) {
1004 /* Only indexed attributes can share same record. */
1009 * Do not try to insert this attribute
1010 * if there is no room in record.
1012 if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
1015 /* Try to insert attribute into this subrecord. */
1016 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1017 name_off, svcn, ins_le);
1021 return PTR_ERR(attr);
1031 /* We have to allocate a new child subrecord. */
1032 err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
1036 if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
1041 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1042 name_off, svcn, ins_le);
1049 err = PTR_ERR(attr);
1061 ni_remove_mi(ni, mi);
1065 ntfs_mark_rec_free(sbi, rno, is_mft);
1072 * ni_insert_attr - Insert an attribute into the file.
1074 * If the primary record has room, it will just insert the attribute.
1075 * If not, it may make the attribute external.
1076 * For $MFT::Data it may make room for the attribute by
1077 * making other attributes external.
1080 * The ATTR_LIST and ATTR_STD cannot be made external.
1081 * This function does not fill new attribute full.
1082 * It only fills 'size'/'type'/'id'/'name_len' fields.
1084 static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1085 const __le16 *name, u8 name_len, u32 asize,
1086 u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
1087 struct mft_inode **ins_mi,
1088 struct ATTR_LIST_ENTRY **ins_le)
1090 struct ntfs_sb_info *sbi = ni->mi.sbi;
1092 struct ATTRIB *attr, *eattr;
1093 struct MFT_REC *rec;
1095 struct ATTR_LIST_ENTRY *le;
1096 u32 list_reserve, max_free, free, used, t32;
1100 is_mft = ni->mi.rno == MFT_REC_MFT;
1103 list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1104 used = le32_to_cpu(rec->used);
1105 free = sbi->record_size - used;
1107 if (is_mft && type != ATTR_LIST) {
1108 /* Reserve space for the ATTRIB list. */
1109 if (free < list_reserve)
1112 free -= list_reserve;
1115 if (asize <= free) {
1116 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
1117 asize, name_off, svcn, ins_le);
1119 err = PTR_ERR(attr);
1133 if (!is_mft || type != ATTR_DATA || svcn) {
1134 /* This ATTRIB will be external. */
1135 err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
1136 svcn, name_off, false, ins_attr, ins_mi,
1142 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
1144 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1145 * Evict as many other attributes as possible.
1149 /* Estimate the result of moving all possible attributes away. */
1152 while ((attr = mi_enum_attr(&ni->mi, attr))) {
1153 if (attr->type == ATTR_STD)
1155 if (attr->type == ATTR_LIST)
1157 max_free += le32_to_cpu(attr->size);
1160 if (max_free < asize + list_reserve) {
1161 /* Impossible to insert this attribute into primary record. */
1166 /* Start real attribute moving. */
1170 attr = mi_enum_attr(&ni->mi, attr);
1172 /* We should never be here 'cause we have already check this case. */
1177 /* Skip attributes that MUST be primary record. */
1178 if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1182 if (ni->attr_list.size) {
1183 le = al_find_le(ni, NULL, attr);
1185 /* Really this is a serious bug. */
1191 t32 = le32_to_cpu(attr->size);
1192 t16 = le16_to_cpu(attr->name_off);
1193 err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1194 attr->name_len, t32, attr_svcn(attr), t16,
1195 false, &eattr, NULL, NULL);
1200 memcpy(eattr, attr, t32);
1203 /* Remove from primary record. */
1204 mi_remove_attr(NULL, &ni->mi, attr);
1206 /* attr now points to next attribute. */
1207 if (attr->type == ATTR_END)
1210 while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1213 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
1214 name_off, svcn, ins_le);
1221 err = PTR_ERR(attr);
1234 /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
1235 static int ni_expand_mft_list(struct ntfs_inode *ni)
1238 struct runs_tree *run = &ni->file.run;
1239 u32 asize, run_size, done = 0;
1240 struct ATTRIB *attr;
1241 struct rb_node *node;
1242 CLST mft_min, mft_new, svcn, evcn, plen;
1243 struct mft_inode *mi, *mi_min, *mi_new;
1244 struct ntfs_sb_info *sbi = ni->mi.sbi;
1246 /* Find the nearest MFT. */
1251 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1252 mi = rb_entry(node, struct mft_inode, node);
1254 attr = mi_enum_attr(mi, NULL);
1263 if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1265 /* Really this is not critical. */
1266 } else if (mft_min > mft_new) {
1270 ntfs_mark_rec_free(sbi, mft_new, true);
1272 ni_remove_mi(ni, mi_new);
1275 attr = mi_find_attr(&ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1281 asize = le32_to_cpu(attr->size);
1283 evcn = le64_to_cpu(attr->nres.evcn);
1284 svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1285 if (evcn + 1 >= svcn) {
1291 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
1293 * Update first part of ATTR_DATA in 'primary MFT.
1295 err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1296 asize - SIZEOF_NONRESIDENT, &plen);
1300 run_size = ALIGN(err, 8);
1308 attr->nres.evcn = cpu_to_le64(svcn - 1);
1309 attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
1310 /* 'done' - How many bytes of primary MFT becomes free. */
1311 done = asize - run_size - SIZEOF_NONRESIDENT;
1312 le32_sub_cpu(&ni->mi.mrec->used, done);
1314 /* Estimate packed size (run_buf=NULL). */
1315 err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1320 run_size = ALIGN(err, 8);
1323 if (plen < evcn + 1 - svcn) {
1329 * This function may implicitly call expand attr_list.
1330 * Insert second part of ATTR_DATA in 'mi_min'.
1332 attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1333 SIZEOF_NONRESIDENT + run_size,
1334 SIZEOF_NONRESIDENT, svcn, NULL);
1341 err = PTR_ERR(attr);
1346 attr->name_off = SIZEOF_NONRESIDENT_LE;
1349 /* This function can't fail - cause already checked above. */
1350 run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1353 attr->nres.svcn = cpu_to_le64(svcn);
1354 attr->nres.evcn = cpu_to_le64(evcn);
1355 attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1359 ntfs_mark_rec_free(sbi, mft_new, true);
1360 ni_remove_mi(ni, mi_new);
1363 return !err && !done ? -EOPNOTSUPP : err;
1367 * ni_expand_list - Move all possible attributes out of primary record.
1369 int ni_expand_list(struct ntfs_inode *ni)
1372 u32 asize, done = 0;
1373 struct ATTRIB *attr, *ins_attr;
1374 struct ATTR_LIST_ENTRY *le;
1375 bool is_mft = ni->mi.rno == MFT_REC_MFT;
1378 mi_get_ref(&ni->mi, &ref);
1381 while ((le = al_enumerate(ni, le))) {
1382 if (le->type == ATTR_STD)
1385 if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1388 if (is_mft && le->type == ATTR_DATA)
1391 /* Find attribute in primary record. */
1392 attr = rec_find_attr_le(&ni->mi, le);
1398 asize = le32_to_cpu(attr->size);
1400 /* Always insert into new record to avoid collisions (deep recursive). */
1401 err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1402 attr->name_len, asize, attr_svcn(attr),
1403 le16_to_cpu(attr->name_off), true,
1404 &ins_attr, NULL, NULL);
1409 memcpy(ins_attr, attr, asize);
1410 ins_attr->id = le->id;
1411 /* Remove from primary record. */
1412 mi_remove_attr(NULL, &ni->mi, attr);
1419 err = -EFBIG; /* Attr list is too big(?) */
1423 /* Split MFT data as much as possible. */
1424 err = ni_expand_mft_list(ni);
1427 return !err && !done ? -EOPNOTSUPP : err;
1431 * ni_insert_nonresident - Insert new nonresident attribute.
1433 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1434 const __le16 *name, u8 name_len,
1435 const struct runs_tree *run, CLST svcn, CLST len,
1436 __le16 flags, struct ATTRIB **new_attr,
1437 struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
1441 struct ATTRIB *attr;
1443 (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && !svcn;
1444 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1445 u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1446 u32 run_off = name_off + name_size;
1447 u32 run_size, asize;
1448 struct ntfs_sb_info *sbi = ni->mi.sbi;
1450 /* Estimate packed size (run_buf=NULL). */
1451 err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1456 run_size = ALIGN(err, 8);
1463 asize = run_off + run_size;
1465 if (asize > sbi->max_bytes_per_attr) {
1470 err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
1477 attr->name_off = cpu_to_le16(name_off);
1478 attr->flags = flags;
1480 /* This function can't fail - cause already checked above. */
1481 run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1483 attr->nres.svcn = cpu_to_le64(svcn);
1484 attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1489 *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1491 attr->nres.alloc_size =
1492 svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1493 attr->nres.data_size = attr->nres.alloc_size;
1494 attr->nres.valid_size = attr->nres.alloc_size;
1497 if (flags & ATTR_FLAG_COMPRESSED)
1498 attr->nres.c_unit = COMPRESSION_UNIT;
1499 attr->nres.total_size = attr->nres.alloc_size;
1507 * ni_insert_resident - Inserts new resident attribute.
1509 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1510 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
1511 struct ATTRIB **new_attr, struct mft_inode **mi,
1512 struct ATTR_LIST_ENTRY **le)
1515 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1516 u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
1517 struct ATTRIB *attr;
1519 err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
1527 attr->res.data_size = cpu_to_le32(data_size);
1528 attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
1529 if (type == ATTR_NAME) {
1530 attr->res.flags = RESIDENT_FLAG_INDEXED;
1532 /* is_attr_indexed(attr)) == true */
1533 le16_add_cpu(&ni->mi.mrec->hard_links, 1);
1534 ni->mi.dirty = true;
1545 * ni_remove_attr_le - Remove attribute from record.
1547 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1548 struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
1550 mi_remove_attr(ni, mi, attr);
1553 al_remove_le(ni, le);
1557 * ni_delete_all - Remove all attributes and frees allocates space.
1559 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
1561 int ni_delete_all(struct ntfs_inode *ni)
1564 struct ATTR_LIST_ENTRY *le = NULL;
1565 struct ATTRIB *attr = NULL;
1566 struct rb_node *node;
1570 struct ntfs_sb_info *sbi = ni->mi.sbi;
1571 bool nt3 = is_ntfs3(sbi);
1574 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1575 if (!nt3 || attr->name_len) {
1577 } else if (attr->type == ATTR_REPARSE) {
1578 mi_get_ref(&ni->mi, &ref);
1579 ntfs_remove_reparse(sbi, 0, &ref);
1580 } else if (attr->type == ATTR_ID && !attr->non_res &&
1581 le32_to_cpu(attr->res.data_size) >=
1582 sizeof(struct GUID)) {
1583 ntfs_objid_remove(sbi, resident_data(attr));
1589 svcn = le64_to_cpu(attr->nres.svcn);
1590 evcn = le64_to_cpu(attr->nres.evcn);
1592 if (evcn + 1 <= svcn)
1595 asize = le32_to_cpu(attr->size);
1596 roff = le16_to_cpu(attr->nres.run_off);
1601 /* run==1 means unpack and deallocate. */
1602 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1603 Add2Ptr(attr, roff), asize - roff);
1606 if (ni->attr_list.size) {
1607 run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1611 /* Free all subrecords. */
1612 for (node = rb_first(&ni->mi_tree); node;) {
1613 struct rb_node *next = rb_next(node);
1614 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1616 clear_rec_inuse(mi->mrec);
1620 ntfs_mark_rec_free(sbi, mi->rno, false);
1621 ni_remove_mi(ni, mi);
1626 /* Free base record. */
1627 clear_rec_inuse(ni->mi.mrec);
1628 ni->mi.dirty = true;
1629 err = mi_write(&ni->mi, 0);
1631 ntfs_mark_rec_free(sbi, ni->mi.rno, false);
1638 * Return: File name attribute by its value.
1640 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1641 const struct cpu_str *uni,
1642 const struct MFT_REF *home_dir,
1643 struct mft_inode **mi,
1644 struct ATTR_LIST_ENTRY **le)
1646 struct ATTRIB *attr = NULL;
1647 struct ATTR_FILE_NAME *fname;
1652 /* Enumerate all names. */
1654 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1658 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1662 if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1668 if (uni->len != fname->name_len)
1671 if (ntfs_cmp_names_cpu(uni, (struct le_str *)&fname->name_len, NULL,
1681 * Return: File name attribute with given type.
1683 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
1684 struct mft_inode **mi,
1685 struct ATTR_LIST_ENTRY **le)
1687 struct ATTRIB *attr = NULL;
1688 struct ATTR_FILE_NAME *fname;
1692 if (name_type == FILE_NAME_POSIX)
1695 /* Enumerate all names. */
1697 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1701 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1702 if (fname && name_type == fname->type)
1710 * Process compressed/sparsed in special way.
1711 * NOTE: You need to set ni->std_fa = new_fa
1712 * after this function to keep internal structures in consistency.
1714 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1716 struct ATTRIB *attr;
1717 struct mft_inode *mi;
1721 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1725 new_aflags = attr->flags;
1727 if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1728 new_aflags |= ATTR_FLAG_SPARSED;
1730 new_aflags &= ~ATTR_FLAG_SPARSED;
1732 if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1733 new_aflags |= ATTR_FLAG_COMPRESSED;
1735 new_aflags &= ~ATTR_FLAG_COMPRESSED;
1737 if (new_aflags == attr->flags)
1740 if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1741 (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1742 ntfs_inode_warn(&ni->vfs_inode,
1743 "file can't be sparsed and compressed");
1750 if (attr->nres.data_size) {
1753 "one can change sparsed/compressed only for empty files");
1757 /* Resize nonresident empty attribute in-place only. */
1758 new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED))
1759 ? (SIZEOF_NONRESIDENT_EX + 8)
1760 : (SIZEOF_NONRESIDENT + 8);
1762 if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1765 if (new_aflags & ATTR_FLAG_SPARSED) {
1766 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1767 /* Windows uses 16 clusters per frame but supports one cluster per frame too. */
1768 attr->nres.c_unit = 0;
1769 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1770 } else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1771 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1772 /* The only allowed: 16 clusters per frame. */
1773 attr->nres.c_unit = NTFS_LZNT_CUNIT;
1774 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1776 attr->name_off = SIZEOF_NONRESIDENT_LE;
1778 attr->nres.c_unit = 0;
1779 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1781 attr->nres.run_off = attr->name_off;
1783 attr->flags = new_aflags;
1792 * buffer - memory for reparse buffer header
1794 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
1795 struct REPARSE_DATA_BUFFER *buffer)
1797 const struct REPARSE_DATA_BUFFER *rp = NULL;
1800 typeof(rp->CompressReparseBuffer) *cmpr;
1802 /* Try to estimate reparse point. */
1803 if (!attr->non_res) {
1804 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1805 } else if (le64_to_cpu(attr->nres.data_size) >=
1806 sizeof(struct REPARSE_DATA_BUFFER)) {
1807 struct runs_tree run;
1811 if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1812 !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1813 sizeof(struct REPARSE_DATA_BUFFER),
1822 return REPARSE_NONE;
1824 len = le16_to_cpu(rp->ReparseDataLength);
1825 switch (rp->ReparseTag) {
1826 case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
1827 break; /* Symbolic link. */
1828 case IO_REPARSE_TAG_MOUNT_POINT:
1829 break; /* Mount points and junctions. */
1830 case IO_REPARSE_TAG_SYMLINK:
1832 case IO_REPARSE_TAG_COMPRESS:
1834 * WOF - Windows Overlay Filter - Used to compress files with
1837 * Unlike native NTFS file compression, the Windows
1838 * Overlay Filter supports only read operations. This means
1839 * that it doesn't need to sector-align each compressed chunk,
1840 * so the compressed data can be packed more tightly together.
1841 * If you open the file for writing, the WOF just decompresses
1842 * the entire file, turning it back into a plain file.
1844 * Ntfs3 driver decompresses the entire file only on write or
1845 * change size requests.
1848 cmpr = &rp->CompressReparseBuffer;
1849 if (len < sizeof(*cmpr) ||
1850 cmpr->WofVersion != WOF_CURRENT_VERSION ||
1851 cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1852 cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1853 return REPARSE_NONE;
1856 switch (cmpr->CompressionFormat) {
1857 case WOF_COMPRESSION_XPRESS4K:
1860 case WOF_COMPRESSION_XPRESS8K:
1863 case WOF_COMPRESSION_XPRESS16K:
1866 case WOF_COMPRESSION_LZX32K:
1873 ni_set_ext_compress_bits(ni, bits);
1874 return REPARSE_COMPRESSED;
1876 case IO_REPARSE_TAG_DEDUP:
1877 ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1878 return REPARSE_DEDUPLICATED;
1881 if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1884 return REPARSE_NONE;
1888 memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
1890 /* Looks like normal symlink. */
1891 return REPARSE_LINK;
1895 * ni_fiemap - Helper for file_fiemap().
1898 * TODO: Less aggressive locks.
1900 int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
1901 __u64 vbo, __u64 len)
1904 struct ntfs_sb_info *sbi = ni->mi.sbi;
1905 u8 cluster_bits = sbi->cluster_bits;
1906 struct runs_tree *run;
1907 struct rw_semaphore *run_lock;
1908 struct ATTRIB *attr;
1909 CLST vcn = vbo >> cluster_bits;
1911 u64 valid = ni->i_valid;
1913 u64 end, alloc_size;
1918 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1919 run = &ni->dir.alloc_run;
1920 attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME,
1921 ARRAY_SIZE(I30_NAME), NULL, NULL);
1922 run_lock = &ni->dir.run_lock;
1924 run = &ni->file.run;
1925 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
1931 if (is_attr_compressed(attr)) {
1932 /* Unfortunately cp -r incorrectly treats compressed clusters. */
1936 "fiemap is not supported for compressed file (cp -r)");
1939 run_lock = &ni->file.run_lock;
1942 if (!attr || !attr->non_res) {
1943 err = fiemap_fill_next_extent(
1945 attr ? le32_to_cpu(attr->res.data_size) : 0,
1946 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST |
1947 FIEMAP_EXTENT_MERGED);
1952 alloc_size = le64_to_cpu(attr->nres.alloc_size);
1953 if (end > alloc_size)
1956 down_read(run_lock);
1960 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1962 CLST vcn_next = vcn;
1964 ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) &&
1972 down_write(run_lock);
1974 err = attr_load_runs_vcn(ni, attr->type,
1976 attr->name_len, run, vcn);
1979 down_read(run_lock);
1984 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1997 if (lcn == SPARSE_LCN) {
1999 vbo = (u64)vcn << cluster_bits;
2003 flags = FIEMAP_EXTENT_MERGED;
2004 if (S_ISDIR(ni->vfs_inode.i_mode)) {
2006 } else if (is_attr_compressed(attr)) {
2009 err = attr_is_frame_compressed(
2010 ni, attr, vcn >> attr->nres.c_unit, &clst_data);
2013 if (clst_data < NTFS_LZNT_CLUSTERS)
2014 flags |= FIEMAP_EXTENT_ENCODED;
2015 } else if (is_attr_encrypted(attr)) {
2016 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
2019 vbo = (u64)vcn << cluster_bits;
2020 bytes = (u64)clen << cluster_bits;
2021 lbo = (u64)lcn << cluster_bits;
2025 if (vbo + bytes >= end)
2028 if (vbo + bytes <= valid) {
2030 } else if (vbo >= valid) {
2031 flags |= FIEMAP_EXTENT_UNWRITTEN;
2033 /* vbo < valid && valid < vbo + bytes */
2034 u64 dlen = valid - vbo;
2036 if (vbo + dlen >= end)
2037 flags |= FIEMAP_EXTENT_LAST;
2039 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
2054 flags |= FIEMAP_EXTENT_UNWRITTEN;
2057 if (vbo + bytes >= end)
2058 flags |= FIEMAP_EXTENT_LAST;
2060 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
2080 * When decompressing, we typically obtain more than one page per reference.
2081 * We inject the additional pages into the page cache.
2083 int ni_readpage_cmpr(struct ntfs_inode *ni, struct page *page)
2086 struct ntfs_sb_info *sbi = ni->mi.sbi;
2087 struct address_space *mapping = page->mapping;
2088 pgoff_t index = page->index;
2089 u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT;
2090 struct page **pages = NULL; /* Array of at most 16 pages. stack? */
2093 u32 i, idx, frame_size, pages_per_frame;
2097 if (vbo >= ni->vfs_inode.i_size) {
2098 SetPageUptodate(page);
2103 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2104 /* Xpress or LZX. */
2105 frame_bits = ni_ext_compress_bits(ni);
2107 /* LZNT compression. */
2108 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2110 frame_size = 1u << frame_bits;
2111 frame = vbo >> frame_bits;
2112 frame_vbo = (u64)frame << frame_bits;
2113 idx = (vbo - frame_vbo) >> PAGE_SHIFT;
2115 pages_per_frame = frame_size >> PAGE_SHIFT;
2116 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2123 index = frame_vbo >> PAGE_SHIFT;
2124 gfp_mask = mapping_gfp_mask(mapping);
2126 for (i = 0; i < pages_per_frame; i++, index++) {
2130 pg = find_or_create_page(mapping, index, gfp_mask);
2138 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame);
2144 for (i = 0; i < pages_per_frame; i++) {
2153 /* At this point, err contains 0 or -EIO depending on the "critical" page. */
2160 #ifdef CONFIG_NTFS3_LZX_XPRESS
2162 * ni_decompress_file - Decompress LZX/Xpress compressed file.
2164 * Remove ATTR_DATA::WofCompressedData.
2165 * Remove ATTR_REPARSE.
2167 int ni_decompress_file(struct ntfs_inode *ni)
2169 struct ntfs_sb_info *sbi = ni->mi.sbi;
2170 struct inode *inode = &ni->vfs_inode;
2171 loff_t i_size = inode->i_size;
2172 struct address_space *mapping = inode->i_mapping;
2173 gfp_t gfp_mask = mapping_gfp_mask(mapping);
2174 struct page **pages = NULL;
2175 struct ATTR_LIST_ENTRY *le;
2176 struct ATTRIB *attr;
2177 CLST vcn, cend, lcn, clen, end;
2181 u32 i, frame_size, pages_per_frame, bytes;
2182 struct mft_inode *mi;
2185 /* Clusters for decompressed data. */
2186 cend = bytes_to_cluster(sbi, i_size);
2191 /* Check in advance. */
2192 if (cend > wnd_zeroes(&sbi->used.bitmap)) {
2197 frame_bits = ni_ext_compress_bits(ni);
2198 frame_size = 1u << frame_bits;
2199 pages_per_frame = frame_size >> PAGE_SHIFT;
2200 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2207 * Step 1: Decompress data and copy to new allocated clusters.
2210 for (vbo = 0; vbo < i_size; vbo += bytes) {
2214 if (vbo + frame_size > i_size) {
2215 bytes = i_size - vbo;
2216 nr_pages = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
2218 nr_pages = pages_per_frame;
2222 end = bytes_to_cluster(sbi, vbo + bytes);
2224 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2225 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2231 for (i = 0; i < pages_per_frame; i++, index++) {
2234 pg = find_or_create_page(mapping, index, gfp_mask);
2237 unlock_page(pages[i]);
2246 err = ni_read_frame(ni, vbo, pages, pages_per_frame);
2249 down_read(&ni->file.run_lock);
2250 err = ntfs_bio_pages(sbi, &ni->file.run, pages,
2251 nr_pages, vbo, bytes,
2253 up_read(&ni->file.run_lock);
2256 for (i = 0; i < pages_per_frame; i++) {
2257 unlock_page(pages[i]);
2269 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2274 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2278 if (attr->type == ATTR_REPARSE) {
2281 mi_get_ref(&ni->mi, &ref);
2282 ntfs_remove_reparse(sbi, 0, &ref);
2288 if (attr->type != ATTR_REPARSE &&
2289 (attr->type != ATTR_DATA ||
2290 attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2291 memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2294 svcn = le64_to_cpu(attr->nres.svcn);
2295 evcn = le64_to_cpu(attr->nres.evcn);
2297 if (evcn + 1 <= svcn)
2300 asize = le32_to_cpu(attr->size);
2301 roff = le16_to_cpu(attr->nres.run_off);
2308 /*run==1 Means unpack and deallocate. */
2309 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2310 Add2Ptr(attr, roff), asize - roff);
2314 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
2316 err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2322 * Step 4: Remove ATTR_REPARSE.
2324 err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2329 * Step 5: Remove sparse flag from data attribute.
2331 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2337 if (attr->non_res && is_attr_sparsed(attr)) {
2338 /* Sparsed attribute header is 8 bytes bigger than normal. */
2339 struct MFT_REC *rec = mi->mrec;
2340 u32 used = le32_to_cpu(rec->used);
2341 u32 asize = le32_to_cpu(attr->size);
2342 u16 roff = le16_to_cpu(attr->nres.run_off);
2343 char *rbuf = Add2Ptr(attr, roff);
2345 memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2346 attr->size = cpu_to_le32(asize - 8);
2347 attr->flags &= ~ATTR_FLAG_SPARSED;
2348 attr->nres.run_off = cpu_to_le16(roff - 8);
2349 attr->nres.c_unit = 0;
2350 rec->used = cpu_to_le32(used - 8);
2352 ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2353 FILE_ATTRIBUTE_REPARSE_POINT);
2355 mark_inode_dirty(inode);
2358 /* Clear cached flag. */
2359 ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2360 if (ni->file.offs_page) {
2361 put_page(ni->file.offs_page);
2362 ni->file.offs_page = NULL;
2364 mapping->a_ops = &ntfs_aops;
2369 _ntfs_bad_inode(inode);
2375 * decompress_lzx_xpress - External compression LZX/Xpress.
2377 static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2378 size_t cmpr_size, void *unc, size_t unc_size,
2384 if (cmpr_size == unc_size) {
2385 /* Frame not compressed. */
2386 memcpy(unc, cmpr, unc_size);
2391 if (frame_size == 0x8000) {
2392 mutex_lock(&sbi->compress.mtx_lzx);
2393 /* LZX: Frame compressed. */
2394 ctx = sbi->compress.lzx;
2396 /* Lazy initialize LZX decompress context. */
2397 ctx = lzx_allocate_decompressor();
2403 sbi->compress.lzx = ctx;
2406 if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2407 /* Treat all errors as "invalid argument". */
2411 mutex_unlock(&sbi->compress.mtx_lzx);
2413 /* XPRESS: Frame compressed. */
2414 mutex_lock(&sbi->compress.mtx_xpress);
2415 ctx = sbi->compress.xpress;
2417 /* Lazy initialize Xpress decompress context. */
2418 ctx = xpress_allocate_decompressor();
2424 sbi->compress.xpress = ctx;
2427 if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2428 /* Treat all errors as "invalid argument". */
2432 mutex_unlock(&sbi->compress.mtx_xpress);
2441 * Pages - Array of locked pages.
2443 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2444 u32 pages_per_frame)
2447 struct ntfs_sb_info *sbi = ni->mi.sbi;
2448 u8 cluster_bits = sbi->cluster_bits;
2449 char *frame_ondisk = NULL;
2450 char *frame_mem = NULL;
2451 struct page **pages_disk = NULL;
2452 struct ATTR_LIST_ENTRY *le = NULL;
2453 struct runs_tree *run = &ni->file.run;
2454 u64 valid_size = ni->i_valid;
2457 u32 frame_size, i, npages_disk, ondisk_size;
2459 struct ATTRIB *attr;
2460 CLST frame, clst_data;
2463 * To simplify decompress algorithm do vmap for source
2466 for (i = 0; i < pages_per_frame; i++)
2469 frame_size = pages_per_frame << PAGE_SHIFT;
2470 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2476 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2482 if (!attr->non_res) {
2483 u32 data_size = le32_to_cpu(attr->res.data_size);
2485 memset(frame_mem, 0, frame_size);
2486 if (frame_vbo < data_size) {
2487 ondisk_size = data_size - frame_vbo;
2488 memcpy(frame_mem, resident_data(attr) + frame_vbo,
2489 min(ondisk_size, frame_size));
2495 if (frame_vbo >= valid_size) {
2496 memset(frame_mem, 0, frame_size);
2501 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2502 #ifndef CONFIG_NTFS3_LZX_XPRESS
2506 u32 frame_bits = ni_ext_compress_bits(ni);
2507 u64 frame64 = frame_vbo >> frame_bits;
2508 u64 frames, vbo_data;
2510 if (frame_size != (1u << frame_bits)) {
2514 switch (frame_size) {
2521 /* Unknown compression. */
2526 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2527 ARRAY_SIZE(WOF_NAME), NULL, NULL);
2531 "external compressed file should contains data attribute \"WofCompressedData\"");
2536 if (!attr->non_res) {
2546 frames = (ni->vfs_inode.i_size - 1) >> frame_bits;
2548 err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2549 frame_bits, &ondisk_size, &vbo_data);
2553 if (frame64 == frames) {
2554 unc_size = 1 + ((ni->vfs_inode.i_size - 1) &
2556 ondisk_size = attr_size(attr) - vbo_data;
2558 unc_size = frame_size;
2561 if (ondisk_size > frame_size) {
2566 if (!attr->non_res) {
2567 if (vbo_data + ondisk_size >
2568 le32_to_cpu(attr->res.data_size)) {
2573 err = decompress_lzx_xpress(
2574 sbi, Add2Ptr(resident_data(attr), vbo_data),
2575 ondisk_size, frame_mem, unc_size, frame_size);
2578 vbo_disk = vbo_data;
2579 /* Load all runs to read [vbo_disk-vbo_to). */
2580 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2581 ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2582 vbo_data + ondisk_size);
2585 npages_disk = (ondisk_size + (vbo_disk & (PAGE_SIZE - 1)) +
2589 } else if (is_attr_compressed(attr)) {
2590 /* LZNT compression. */
2591 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2596 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2601 down_write(&ni->file.run_lock);
2602 run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2603 frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2604 err = attr_is_frame_compressed(ni, attr, frame, &clst_data);
2605 up_write(&ni->file.run_lock);
2610 memset(frame_mem, 0, frame_size);
2614 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2615 ondisk_size = clst_data << cluster_bits;
2617 if (clst_data >= NTFS_LZNT_CLUSTERS) {
2618 /* Frame is not compressed. */
2619 down_read(&ni->file.run_lock);
2620 err = ntfs_bio_pages(sbi, run, pages, pages_per_frame,
2621 frame_vbo, ondisk_size,
2623 up_read(&ni->file.run_lock);
2626 vbo_disk = frame_vbo;
2627 npages_disk = (ondisk_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2629 __builtin_unreachable();
2634 pages_disk = kzalloc(npages_disk * sizeof(struct page *), GFP_NOFS);
2640 for (i = 0; i < npages_disk; i++) {
2641 pg = alloc_page(GFP_KERNEL);
2651 /* Read 'ondisk_size' bytes from disk. */
2652 down_read(&ni->file.run_lock);
2653 err = ntfs_bio_pages(sbi, run, pages_disk, npages_disk, vbo_disk,
2654 ondisk_size, REQ_OP_READ);
2655 up_read(&ni->file.run_lock);
2660 * To simplify decompress algorithm do vmap for source and target pages.
2662 frame_ondisk = vmap(pages_disk, npages_disk, VM_MAP, PAGE_KERNEL_RO);
2663 if (!frame_ondisk) {
2668 /* Decompress: Frame_ondisk -> frame_mem. */
2669 #ifdef CONFIG_NTFS3_LZX_XPRESS
2670 if (run != &ni->file.run) {
2672 err = decompress_lzx_xpress(
2673 sbi, frame_ondisk + (vbo_disk & (PAGE_SIZE - 1)),
2674 ondisk_size, frame_mem, unc_size, frame_size);
2678 /* LZNT - Native NTFS compression. */
2679 unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2681 if ((ssize_t)unc_size < 0)
2683 else if (!unc_size || unc_size > frame_size)
2686 if (!err && valid_size < frame_vbo + frame_size) {
2687 size_t ok = valid_size - frame_vbo;
2689 memset(frame_mem + ok, 0, frame_size - ok);
2692 vunmap(frame_ondisk);
2695 for (i = 0; i < npages_disk; i++) {
2706 #ifdef CONFIG_NTFS3_LZX_XPRESS
2707 if (run != &ni->file.run)
2713 for (i = 0; i < pages_per_frame; i++) {
2717 SetPageUptodate(pg);
2726 * Pages - Array of locked pages.
2728 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2729 u32 pages_per_frame)
2732 struct ntfs_sb_info *sbi = ni->mi.sbi;
2733 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2734 u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2735 u64 frame_vbo = (u64)pages[0]->index << PAGE_SHIFT;
2736 CLST frame = frame_vbo >> frame_bits;
2737 char *frame_ondisk = NULL;
2738 struct page **pages_disk = NULL;
2739 struct ATTR_LIST_ENTRY *le = NULL;
2741 struct ATTRIB *attr;
2742 struct mft_inode *mi;
2745 size_t compr_size, ondisk_size;
2748 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2754 if (WARN_ON(!is_attr_compressed(attr))) {
2759 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2764 if (!attr->non_res) {
2765 down_write(&ni->file.run_lock);
2766 err = attr_make_nonresident(ni, attr, le, mi,
2767 le32_to_cpu(attr->res.data_size),
2768 &ni->file.run, &attr, pages[0]);
2769 up_write(&ni->file.run_lock);
2774 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2779 pages_disk = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2785 for (i = 0; i < pages_per_frame; i++) {
2786 pg = alloc_page(GFP_KERNEL);
2796 /* To simplify compress algorithm do vmap for source and target pages. */
2797 frame_ondisk = vmap(pages_disk, pages_per_frame, VM_MAP, PAGE_KERNEL);
2798 if (!frame_ondisk) {
2803 for (i = 0; i < pages_per_frame; i++)
2806 /* Map in-memory frame for read-only. */
2807 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2813 mutex_lock(&sbi->compress.mtx_lznt);
2815 if (!sbi->compress.lznt) {
2817 * LZNT implements two levels of compression:
2818 * 0 - Standard compression
2819 * 1 - Best compression, requires a lot of cpu
2822 lznt = get_lznt_ctx(0);
2824 mutex_unlock(&sbi->compress.mtx_lznt);
2829 sbi->compress.lznt = lznt;
2833 /* Compress: frame_mem -> frame_ondisk */
2834 compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2835 frame_size, sbi->compress.lznt);
2836 mutex_unlock(&sbi->compress.mtx_lznt);
2839 if (compr_size + sbi->cluster_size > frame_size) {
2840 /* Frame is not compressed. */
2841 compr_size = frame_size;
2842 ondisk_size = frame_size;
2843 } else if (compr_size) {
2844 /* Frame is compressed. */
2845 ondisk_size = ntfs_up_cluster(sbi, compr_size);
2846 memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2848 /* Frame is sparsed. */
2852 down_write(&ni->file.run_lock);
2853 run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2854 err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2855 up_write(&ni->file.run_lock);
2862 down_read(&ni->file.run_lock);
2863 err = ntfs_bio_pages(sbi, &ni->file.run,
2864 ondisk_size < frame_size ? pages_disk : pages,
2865 pages_per_frame, frame_vbo, ondisk_size,
2867 up_read(&ni->file.run_lock);
2873 for (i = 0; i < pages_per_frame; i++)
2876 vunmap(frame_ondisk);
2878 for (i = 0; i < pages_per_frame; i++) {
2892 * ni_remove_name - Removes name 'de' from MFT and from directory.
2893 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2895 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2896 struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2899 struct ntfs_sb_info *sbi = ni->mi.sbi;
2900 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2901 struct ATTR_FILE_NAME *fname;
2902 struct ATTR_LIST_ENTRY *le;
2903 struct mft_inode *mi;
2904 u16 de_key_size = le16_to_cpu(de->key_size);
2909 /* Find name in record. */
2910 mi_get_ref(&dir_ni->mi, &de_name->home);
2912 fname = ni_fname_name(ni, (struct cpu_str *)&de_name->name_len,
2913 &de_name->home, &mi, &le);
2917 memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2918 name_type = paired_name(fname->type);
2920 /* Mark ntfs as dirty. It will be cleared at umount. */
2921 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2923 /* Step 1: Remove name from directory. */
2924 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2928 /* Step 2: Remove name from MFT. */
2929 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2933 /* Get paired name. */
2934 fname = ni_fname_type(ni, name_type, &mi, &le);
2936 u16 de2_key_size = fname_full_size(fname);
2938 *de2 = Add2Ptr(de, 1024);
2939 (*de2)->key_size = cpu_to_le16(de2_key_size);
2941 memcpy(*de2 + 1, fname, de2_key_size);
2943 /* Step 3: Remove paired name from directory. */
2944 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2949 /* Step 4: Remove paired name from MFT. */
2950 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2958 * ni_remove_name_undo - Paired function for ni_remove_name.
2960 * Return: True if ok
2962 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2963 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2965 struct ntfs_sb_info *sbi = ni->mi.sbi;
2966 struct ATTRIB *attr;
2967 u16 de_key_size = de2 ? le16_to_cpu(de2->key_size) : 0;
2969 switch (undo_step) {
2971 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2972 &attr, NULL, NULL)) {
2975 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2977 mi_get_ref(&ni->mi, &de2->ref);
2978 de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2979 sizeof(struct NTFS_DE));
2983 if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL,
2990 de_key_size = le16_to_cpu(de->key_size);
2992 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2993 &attr, NULL, NULL)) {
2997 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2998 mi_get_ref(&ni->mi, &de->ref);
3000 if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
3008 * ni_add_name - Add new name into MFT and into directory.
3010 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
3014 struct ATTRIB *attr;
3015 struct ATTR_LIST_ENTRY *le;
3016 struct mft_inode *mi;
3017 struct ATTR_FILE_NAME *fname;
3018 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
3019 u16 de_key_size = le16_to_cpu(de->key_size);
3021 mi_get_ref(&ni->mi, &de->ref);
3022 mi_get_ref(&dir_ni->mi, &de_name->home);
3024 /* Fill duplicate from any ATTR_NAME. */
3025 fname = ni_fname_name(ni, NULL, NULL, NULL, NULL);
3027 memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup));
3028 de_name->dup.fa = ni->std_fa;
3030 /* Insert new name into MFT. */
3031 err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
3036 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
3038 /* Insert new name into directory. */
3039 err = indx_insert_entry(&dir_ni->dir, dir_ni, de, ni->mi.sbi, NULL, 0);
3041 ni_remove_attr_le(ni, attr, mi, le);
3047 * ni_rename - Remove one name and insert new name.
3049 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
3050 struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de,
3054 struct NTFS_DE *de2 = NULL;
3058 * There are two possible ways to rename:
3059 * 1) Add new name and remove old name.
3060 * 2) Remove old name and add new name.
3062 * In most cases (not all!) adding new name into MFT and into directory can
3063 * allocate additional cluster(s).
3064 * Second way may result to bad inode if we can't add new name
3065 * and then can't restore (add) old name.
3069 * Way 1 - Add new + remove old.
3071 err = ni_add_name(new_dir_ni, ni, new_de);
3073 err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3074 if (err && ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo))
3079 * Way 2 - Remove old + add new.
3082 * err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3084 * err = ni_add_name(new_dir_ni, ni, new_de);
3085 * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
3094 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
3096 bool ni_is_dirty(struct inode *inode)
3098 struct ntfs_inode *ni = ntfs_i(inode);
3099 struct rb_node *node;
3101 if (ni->mi.dirty || ni->attr_list.dirty ||
3102 (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3105 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
3106 if (rb_entry(node, struct mft_inode, node)->dirty)
3116 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
3118 static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
3121 struct ATTRIB *attr;
3122 struct mft_inode *mi;
3123 struct ATTR_LIST_ENTRY *le = NULL;
3124 struct ntfs_sb_info *sbi = ni->mi.sbi;
3125 struct super_block *sb = sbi->sb;
3126 bool re_dirty = false;
3128 if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
3129 dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
3131 dup->alloc_size = 0;
3134 dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
3136 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
3139 dup->alloc_size = dup->data_size = 0;
3140 } else if (!attr->non_res) {
3141 u32 data_size = le32_to_cpu(attr->res.data_size);
3143 dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
3144 dup->data_size = cpu_to_le64(data_size);
3146 u64 new_valid = ni->i_valid;
3147 u64 data_size = le64_to_cpu(attr->nres.data_size);
3150 dup->alloc_size = is_attr_ext(attr)
3151 ? attr->nres.total_size
3152 : attr->nres.alloc_size;
3153 dup->data_size = attr->nres.data_size;
3155 if (new_valid > data_size)
3156 new_valid = data_size;
3158 valid_le = cpu_to_le64(new_valid);
3159 if (valid_le != attr->nres.valid_size) {
3160 attr->nres.valid_size = valid_le;
3166 /* TODO: Fill reparse info. */
3170 if (ni->ni_flags & NI_FLAG_EA) {
3171 attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3174 const struct EA_INFO *info;
3176 info = resident_data_ex(attr, sizeof(struct EA_INFO));
3177 /* If ATTR_EA_INFO exists 'info' can't be NULL. */
3179 dup->ea_size = info->size_pack;
3186 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3189 struct ATTR_FILE_NAME *fname;
3191 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
3192 if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
3195 /* ntfs_iget5 may sleep. */
3196 dir = ntfs_iget5(sb, &fname->home, NULL);
3200 "failed to open parent directory r=%lx to update",
3201 (long)ino_get(&fname->home));
3205 if (!is_bad_inode(dir)) {
3206 struct ntfs_inode *dir_ni = ntfs_i(dir);
3208 if (!ni_trylock(dir_ni)) {
3211 indx_update_dup(dir_ni, sbi, fname, dup, sync);
3213 memcpy(&fname->dup, dup, sizeof(fname->dup));
3224 * ni_write_inode - Write MFT base record and all subrecords to disk.
3226 int ni_write_inode(struct inode *inode, int sync, const char *hint)
3229 struct ntfs_inode *ni = ntfs_i(inode);
3230 struct super_block *sb = inode->i_sb;
3231 struct ntfs_sb_info *sbi = sb->s_fs_info;
3232 bool re_dirty = false;
3233 struct ATTR_STD_INFO *std;
3234 struct rb_node *node, *next;
3235 struct NTFS_DUP_INFO dup;
3237 if (is_bad_inode(inode) || sb_rdonly(sb))
3240 if (!ni_trylock(ni)) {
3241 /* 'ni' is under modification, skip for now. */
3242 mark_inode_dirty_sync(inode);
3246 if (is_rec_inuse(ni->mi.mrec) &&
3247 !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3248 bool modified = false;
3250 /* Update times in standard attribute. */
3257 /* Update the access times if they have changed. */
3258 dup.m_time = kernel2nt(&inode->i_mtime);
3259 if (std->m_time != dup.m_time) {
3260 std->m_time = dup.m_time;
3264 dup.c_time = kernel2nt(&inode->i_ctime);
3265 if (std->c_time != dup.c_time) {
3266 std->c_time = dup.c_time;
3270 dup.a_time = kernel2nt(&inode->i_atime);
3271 if (std->a_time != dup.a_time) {
3272 std->a_time = dup.a_time;
3276 dup.fa = ni->std_fa;
3277 if (std->fa != dup.fa) {
3283 ni->mi.dirty = true;
3285 if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
3286 (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3287 /* Avoid __wait_on_freeing_inode(inode). */
3288 && (sb->s_flags & SB_ACTIVE)) {
3289 dup.cr_time = std->cr_time;
3290 /* Not critical if this function fail. */
3291 re_dirty = ni_update_parent(ni, &dup, sync);
3294 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3296 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3299 /* Update attribute list. */
3300 if (ni->attr_list.size && ni->attr_list.dirty) {
3301 if (inode->i_ino != MFT_REC_MFT || sync) {
3302 err = ni_try_remove_attr_list(ni);
3307 err = al_update(ni, sync);
3313 for (node = rb_first(&ni->mi_tree); node; node = next) {
3314 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3317 next = rb_next(node);
3322 is_empty = !mi_enum_attr(mi, NULL);
3325 clear_rec_inuse(mi->mrec);
3327 err2 = mi_write(mi, sync);
3332 ntfs_mark_rec_free(sbi, mi->rno, false);
3333 rb_erase(node, &ni->mi_tree);
3339 err2 = mi_write(&ni->mi, sync);
3347 ntfs_err(sb, "%s r=%lx failed, %d.", hint, inode->i_ino, err);
3348 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3353 mark_inode_dirty_sync(inode);