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 && ni->mi.mrec &&
106 is_rec_inuse(ni->mi.mrec) &&
107 !(ni->mi.sbi->flags & NTFS_FLAGS_LOG_REPLAYING))
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_folio) {
128 /* On-demand allocated page for offsets. */
129 folio_put(ni->file.offs_folio);
130 ni->file.offs_folio = 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_inode_err(&ni->vfs_inode, "failed to parse mft record");
242 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR);
247 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
249 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
250 struct ATTR_LIST_ENTRY **le,
251 struct mft_inode **mi)
253 struct mft_inode *mi2;
254 struct ATTR_LIST_ENTRY *le2;
256 /* Do we have an attribute list? */
257 if (!ni->attr_list.size) {
261 /* Enum attributes in primary record. */
262 return mi_enum_attr(&ni->mi, attr);
265 /* Get next list entry. */
266 le2 = *le = al_enumerate(ni, attr ? *le : NULL);
270 /* Load record that contains the required attribute. */
271 if (ni_load_mi(ni, le2, &mi2))
277 /* Find attribute in loaded record. */
278 return rec_find_attr_le(mi2, le2);
282 * ni_load_attr - Load attribute that contains given VCN.
284 struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
285 const __le16 *name, u8 name_len, CLST vcn,
286 struct mft_inode **pmi)
288 struct ATTR_LIST_ENTRY *le;
290 struct mft_inode *mi;
291 struct ATTR_LIST_ENTRY *next;
293 if (!ni->attr_list.size) {
296 return mi_find_attr(&ni->mi, NULL, type, name, name_len, NULL);
299 le = al_find_ex(ni, NULL, type, name, name_len, NULL);
304 * Unfortunately ATTR_LIST_ENTRY contains only start VCN.
305 * So to find the ATTRIB segment that contains 'vcn' we should
306 * enumerate some entries.
310 next = al_find_ex(ni, le, type, name, name_len, NULL);
311 if (!next || le64_to_cpu(next->vcn) > vcn)
316 if (ni_load_mi(ni, le, &mi))
322 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
329 if (le64_to_cpu(attr->nres.svcn) <= vcn &&
330 vcn <= le64_to_cpu(attr->nres.evcn))
337 * ni_load_all_mi - Load all subrecords.
339 int ni_load_all_mi(struct ntfs_inode *ni)
342 struct ATTR_LIST_ENTRY *le;
344 if (!ni->attr_list.size)
349 while ((le = al_enumerate(ni, le))) {
350 CLST rno = ino_get(&le->ref);
352 if (rno == ni->mi.rno)
355 err = ni_load_mi_ex(ni, rno, NULL);
364 * ni_add_subrecord - Allocate + format + attach a new subrecord.
366 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
370 m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
374 if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
379 mi_get_ref(&ni->mi, &m->mrec->parent_ref);
387 * ni_remove_attr - Remove all attributes for the given type/name/id.
389 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
390 const __le16 *name, u8 name_len, bool base_only,
395 struct ATTR_LIST_ENTRY *le;
396 struct mft_inode *mi;
400 if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
401 attr = mi_find_attr(&ni->mi, NULL, type, name, name_len, id);
405 mi_remove_attr(ni, &ni->mi, attr);
409 type_in = le32_to_cpu(type);
413 le = al_enumerate(ni, le);
418 diff = le32_to_cpu(le->type) - type_in;
425 if (le->name_len != name_len)
429 memcmp(le_name(le), name, name_len * sizeof(short)))
432 if (id && le->id != *id)
434 err = ni_load_mi(ni, le, &mi);
438 al_remove_le(ni, le);
440 attr = mi_find_attr(mi, NULL, type, name, name_len, id);
444 mi_remove_attr(ni, mi, attr);
446 if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
453 * ni_ins_new_attr - Insert the attribute into record.
455 * Return: Not full constructed attribute or NULL if not possible to create.
457 static struct ATTRIB *
458 ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
459 struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
460 const __le16 *name, u8 name_len, u32 asize, u16 name_off,
461 CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
465 bool le_added = false;
468 mi_get_ref(mi, &ref);
470 if (type != ATTR_LIST && !le && ni->attr_list.size) {
471 err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
474 /* No memory or no space. */
480 * al_add_le -> attr_set_size (list) -> ni_expand_list
481 * which moves some attributes out of primary record
482 * this means that name may point into moved memory
483 * reinit 'name' from le.
488 attr = mi_insert_attr(mi, type, name, name_len, asize, name_off);
491 al_remove_le(ni, le);
495 if (type == ATTR_LIST) {
496 /* Attr list is not in list entry array. */
503 /* Update ATTRIB Id and record reference. */
505 ni->attr_list.dirty = true;
517 * Random write access to sparsed or compressed file may result to
518 * not optimized packed runs.
519 * Here is the place to optimize it.
521 static int ni_repack(struct ntfs_inode *ni)
527 struct ntfs_sb_info *sbi = ni->mi.sbi;
528 struct mft_inode *mi, *mi_p = NULL;
529 struct ATTRIB *attr = NULL, *attr_p;
530 struct ATTR_LIST_ENTRY *le = NULL, *le_p;
532 u8 cluster_bits = sbi->cluster_bits;
533 CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
534 u32 roff, rs = sbi->record_size;
535 struct runs_tree run;
539 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
543 svcn = le64_to_cpu(attr->nres.svcn);
544 if (svcn != le64_to_cpu(le->vcn)) {
550 alloc = le64_to_cpu(attr->nres.alloc_size) >>
553 } else if (svcn != evcn + 1) {
558 evcn = le64_to_cpu(attr->nres.evcn);
560 if (svcn > evcn + 1) {
566 /* Do not try if not enough free space. */
567 if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
570 /* Do not try if last attribute segment. */
571 if (evcn + 1 == alloc)
576 roff = le16_to_cpu(attr->nres.run_off);
578 if (roff > le32_to_cpu(attr->size)) {
583 err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
585 le32_to_cpu(attr->size) - roff);
600 * Run contains data from two records: mi_p and mi
601 * Try to pack in one.
603 err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
607 next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
609 if (next_svcn >= evcn + 1) {
610 /* We can remove this attribute segment. */
611 al_remove_le(ni, le);
612 mi_remove_attr(NULL, mi, attr);
617 attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
619 ni->attr_list.dirty = true;
621 if (evcn + 1 == alloc) {
622 err = mi_pack_runs(mi, attr, &run,
623 evcn + 1 - next_svcn);
633 run_truncate_head(&run, next_svcn);
638 ntfs_inode_warn(&ni->vfs_inode, "repack problem");
639 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
641 /* Pack loaded but not packed runs. */
643 mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
652 * ni_try_remove_attr_list
654 * Can we remove attribute list?
655 * Check the case when primary record contains enough space for all attributes.
657 static int ni_try_remove_attr_list(struct ntfs_inode *ni)
660 struct ntfs_sb_info *sbi = ni->mi.sbi;
661 struct ATTRIB *attr, *attr_list, *attr_ins;
662 struct ATTR_LIST_ENTRY *le;
663 struct mft_inode *mi;
666 struct MFT_REC *mrec;
669 if (!ni->attr_list.dirty)
676 attr_list = mi_find_attr(&ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
680 asize = le32_to_cpu(attr_list->size);
682 /* Free space in primary record without attribute list. */
683 free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
684 mi_get_ref(&ni->mi, &ref);
687 while ((le = al_enumerate(ni, le))) {
688 if (!memcmp(&le->ref, &ref, sizeof(ref)))
694 mi = ni_find_mi(ni, ino_get(&le->ref));
698 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
699 le->name_len, &le->id);
703 asize = le32_to_cpu(attr->size);
710 /* Make a copy of primary record to restore if error. */
711 mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
713 return 0; /* Not critical. */
715 /* It seems that attribute list can be removed from primary record. */
716 mi_remove_attr(NULL, &ni->mi, attr_list);
719 * Repeat the cycle above and copy all attributes to primary record.
720 * Do not remove original attributes from subrecords!
721 * It should be success!
724 while ((le = al_enumerate(ni, le))) {
725 if (!memcmp(&le->ref, &ref, sizeof(ref)))
728 mi = ni_find_mi(ni, ino_get(&le->ref));
730 /* Should never happened, 'cause already checked. */
734 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
735 le->name_len, &le->id);
737 /* Should never happened, 'cause already checked. */
740 asize = le32_to_cpu(attr->size);
742 /* Insert into primary record. */
743 attr_ins = mi_insert_attr(&ni->mi, le->type, le_name(le),
745 le16_to_cpu(attr->name_off));
748 * No space in primary record (already checked).
753 /* Copy all except id. */
755 memcpy(attr_ins, attr, asize);
760 * Repeat the cycle above and remove all attributes from subrecords.
763 while ((le = al_enumerate(ni, le))) {
764 if (!memcmp(&le->ref, &ref, sizeof(ref)))
767 mi = ni_find_mi(ni, ino_get(&le->ref));
771 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
772 le->name_len, &le->id);
776 /* Remove from original record. */
777 mi_remove_attr(NULL, mi, attr);
780 run_deallocate(sbi, &ni->attr_list.run, true);
781 run_close(&ni->attr_list.run);
782 ni->attr_list.size = 0;
783 kvfree(ni->attr_list.le);
784 ni->attr_list.le = NULL;
785 ni->attr_list.dirty = false;
790 /* Restore primary record. */
791 swap(mrec, ni->mi.mrec);
797 * ni_create_attr_list - Generates an attribute list for this primary record.
799 int ni_create_attr_list(struct ntfs_inode *ni)
801 struct ntfs_sb_info *sbi = ni->mi.sbi;
805 struct ATTRIB *arr_move[7];
806 struct ATTR_LIST_ENTRY *le, *le_b[7];
810 struct mft_inode *mi;
811 u32 free_b, nb, to_free, rs;
814 is_mft = ni->mi.rno == MFT_REC_MFT;
816 rs = sbi->record_size;
819 * Skip estimating exact memory requirement.
820 * Looks like one record_size is always enough.
822 le = kmalloc(al_aligned(rs), GFP_NOFS);
826 mi_get_ref(&ni->mi, &le->ref);
827 ni->attr_list.le = le;
834 for (; (attr = mi_enum_attr(&ni->mi, attr)); le = Add2Ptr(le, sz)) {
835 sz = le_size(attr->name_len);
836 le->type = attr->type;
837 le->size = cpu_to_le16(sz);
838 le->name_len = attr->name_len;
839 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
841 if (le != ni->attr_list.le)
842 le->ref = ni->attr_list.le->ref;
846 memcpy(le->name, attr_name(attr),
847 sizeof(short) * attr->name_len);
848 else if (attr->type == ATTR_STD)
850 else if (attr->type == ATTR_LIST)
852 else if (is_mft && attr->type == ATTR_DATA)
855 if (!nb || nb < ARRAY_SIZE(arr_move)) {
857 arr_move[nb++] = attr;
858 free_b += le32_to_cpu(attr->size);
862 lsize = PtrOffset(ni->attr_list.le, le);
863 ni->attr_list.size = lsize;
865 to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
871 if (to_free > free_b) {
877 /* Allocate child MFT. */
878 err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
883 /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
884 while (to_free > 0) {
885 struct ATTRIB *b = arr_move[--nb];
886 u32 asize = le32_to_cpu(b->size);
887 u16 name_off = le16_to_cpu(b->name_off);
889 attr = mi_insert_attr(mi, b->type, Add2Ptr(b, name_off),
890 b->name_len, asize, name_off);
894 mi_get_ref(mi, &le_b[nb]->ref);
895 le_b[nb]->id = attr->id;
897 /* Copy all except id. */
898 memcpy(attr, b, asize);
899 attr->id = le_b[nb]->id;
901 /* Remove from primary record. */
902 if (!mi_remove_attr(NULL, &ni->mi, b))
905 if (to_free <= asize)
912 attr = mi_insert_attr(&ni->mi, ATTR_LIST, NULL, 0,
913 lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
919 attr->res.data_size = cpu_to_le32(lsize);
920 attr->res.data_off = SIZEOF_RESIDENT_LE;
924 memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
926 ni->attr_list.dirty = false;
928 mark_inode_dirty(&ni->vfs_inode);
932 kvfree(ni->attr_list.le);
933 ni->attr_list.le = NULL;
934 ni->attr_list.size = 0;
939 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
941 static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
942 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
943 u32 asize, CLST svcn, u16 name_off, bool force_ext,
944 struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
945 struct ATTR_LIST_ENTRY **ins_le)
948 struct mft_inode *mi;
951 struct rb_node *node;
953 bool is_mft, is_mft_data;
954 struct ntfs_sb_info *sbi = ni->mi.sbi;
956 is_mft = ni->mi.rno == MFT_REC_MFT;
957 is_mft_data = is_mft && type == ATTR_DATA && !name_len;
959 if (asize > sbi->max_bytes_per_attr) {
965 * Standard information and attr_list cannot be made external.
966 * The Log File cannot have any external attributes.
968 if (type == ATTR_STD || type == ATTR_LIST ||
969 ni->mi.rno == MFT_REC_LOG) {
974 /* Create attribute list if it is not already existed. */
975 if (!ni->attr_list.size) {
976 err = ni_create_attr_list(ni);
981 vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
986 /* Load all subrecords into memory. */
987 err = ni_load_all_mi(ni);
991 /* Check each of loaded subrecord. */
992 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
993 mi = rb_entry(node, struct mft_inode, node);
996 (mi_enum_attr(mi, NULL) ||
997 vbo <= ((u64)mi->rno << sbi->record_bits))) {
998 /* We can't accept this record 'cause MFT's bootstrapping. */
1002 mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
1004 * This child record already has a ATTR_DATA.
1005 * So it can't accept any other records.
1010 if ((type != ATTR_NAME || name_len) &&
1011 mi_find_attr(mi, NULL, type, name, name_len, NULL)) {
1012 /* Only indexed attributes can share same record. */
1017 * Do not try to insert this attribute
1018 * if there is no room in record.
1020 if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
1023 /* Try to insert attribute into this subrecord. */
1024 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1025 name_off, svcn, ins_le);
1029 return PTR_ERR(attr);
1039 /* We have to allocate a new child subrecord. */
1040 err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
1044 if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
1049 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1050 name_off, svcn, ins_le);
1057 err = PTR_ERR(attr);
1069 ni_remove_mi(ni, mi);
1073 ntfs_mark_rec_free(sbi, rno, is_mft);
1080 * ni_insert_attr - Insert an attribute into the file.
1082 * If the primary record has room, it will just insert the attribute.
1083 * If not, it may make the attribute external.
1084 * For $MFT::Data it may make room for the attribute by
1085 * making other attributes external.
1088 * The ATTR_LIST and ATTR_STD cannot be made external.
1089 * This function does not fill new attribute full.
1090 * It only fills 'size'/'type'/'id'/'name_len' fields.
1092 static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1093 const __le16 *name, u8 name_len, u32 asize,
1094 u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
1095 struct mft_inode **ins_mi,
1096 struct ATTR_LIST_ENTRY **ins_le)
1098 struct ntfs_sb_info *sbi = ni->mi.sbi;
1100 struct ATTRIB *attr, *eattr;
1101 struct MFT_REC *rec;
1103 struct ATTR_LIST_ENTRY *le;
1104 u32 list_reserve, max_free, free, used, t32;
1108 is_mft = ni->mi.rno == MFT_REC_MFT;
1111 list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1112 used = le32_to_cpu(rec->used);
1113 free = sbi->record_size - used;
1115 if (is_mft && type != ATTR_LIST) {
1116 /* Reserve space for the ATTRIB list. */
1117 if (free < list_reserve)
1120 free -= list_reserve;
1123 if (asize <= free) {
1124 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
1125 asize, name_off, svcn, ins_le);
1127 err = PTR_ERR(attr);
1141 if (!is_mft || type != ATTR_DATA || svcn) {
1142 /* This ATTRIB will be external. */
1143 err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
1144 svcn, name_off, false, ins_attr, ins_mi,
1150 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
1152 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1153 * Evict as many other attributes as possible.
1157 /* Estimate the result of moving all possible attributes away. */
1160 while ((attr = mi_enum_attr(&ni->mi, attr))) {
1161 if (attr->type == ATTR_STD)
1163 if (attr->type == ATTR_LIST)
1165 max_free += le32_to_cpu(attr->size);
1168 if (max_free < asize + list_reserve) {
1169 /* Impossible to insert this attribute into primary record. */
1174 /* Start real attribute moving. */
1178 attr = mi_enum_attr(&ni->mi, attr);
1180 /* We should never be here 'cause we have already check this case. */
1185 /* Skip attributes that MUST be primary record. */
1186 if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1190 if (ni->attr_list.size) {
1191 le = al_find_le(ni, NULL, attr);
1193 /* Really this is a serious bug. */
1199 t32 = le32_to_cpu(attr->size);
1200 t16 = le16_to_cpu(attr->name_off);
1201 err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1202 attr->name_len, t32, attr_svcn(attr), t16,
1203 false, &eattr, NULL, NULL);
1208 memcpy(eattr, attr, t32);
1211 /* Remove from primary record. */
1212 mi_remove_attr(NULL, &ni->mi, attr);
1214 /* attr now points to next attribute. */
1215 if (attr->type == ATTR_END)
1218 while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1221 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
1222 name_off, svcn, ins_le);
1229 err = PTR_ERR(attr);
1242 /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
1243 static int ni_expand_mft_list(struct ntfs_inode *ni)
1246 struct runs_tree *run = &ni->file.run;
1247 u32 asize, run_size, done = 0;
1248 struct ATTRIB *attr;
1249 struct rb_node *node;
1250 CLST mft_min, mft_new, svcn, evcn, plen;
1251 struct mft_inode *mi, *mi_min, *mi_new;
1252 struct ntfs_sb_info *sbi = ni->mi.sbi;
1254 /* Find the nearest MFT. */
1259 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1260 mi = rb_entry(node, struct mft_inode, node);
1262 attr = mi_enum_attr(mi, NULL);
1271 if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1273 /* Really this is not critical. */
1274 } else if (mft_min > mft_new) {
1278 ntfs_mark_rec_free(sbi, mft_new, true);
1280 ni_remove_mi(ni, mi_new);
1283 attr = mi_find_attr(&ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1289 asize = le32_to_cpu(attr->size);
1291 evcn = le64_to_cpu(attr->nres.evcn);
1292 svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1293 if (evcn + 1 >= svcn) {
1299 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
1301 * Update first part of ATTR_DATA in 'primary MFT.
1303 err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1304 asize - SIZEOF_NONRESIDENT, &plen);
1308 run_size = ALIGN(err, 8);
1316 attr->nres.evcn = cpu_to_le64(svcn - 1);
1317 attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
1318 /* 'done' - How many bytes of primary MFT becomes free. */
1319 done = asize - run_size - SIZEOF_NONRESIDENT;
1320 le32_sub_cpu(&ni->mi.mrec->used, done);
1322 /* Estimate packed size (run_buf=NULL). */
1323 err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1328 run_size = ALIGN(err, 8);
1331 if (plen < evcn + 1 - svcn) {
1337 * This function may implicitly call expand attr_list.
1338 * Insert second part of ATTR_DATA in 'mi_min'.
1340 attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1341 SIZEOF_NONRESIDENT + run_size,
1342 SIZEOF_NONRESIDENT, svcn, NULL);
1349 err = PTR_ERR(attr);
1354 attr->name_off = SIZEOF_NONRESIDENT_LE;
1357 /* This function can't fail - cause already checked above. */
1358 run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1361 attr->nres.svcn = cpu_to_le64(svcn);
1362 attr->nres.evcn = cpu_to_le64(evcn);
1363 attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1367 ntfs_mark_rec_free(sbi, mft_new, true);
1368 ni_remove_mi(ni, mi_new);
1371 return !err && !done ? -EOPNOTSUPP : err;
1375 * ni_expand_list - Move all possible attributes out of primary record.
1377 int ni_expand_list(struct ntfs_inode *ni)
1380 u32 asize, done = 0;
1381 struct ATTRIB *attr, *ins_attr;
1382 struct ATTR_LIST_ENTRY *le;
1383 bool is_mft = ni->mi.rno == MFT_REC_MFT;
1386 mi_get_ref(&ni->mi, &ref);
1389 while ((le = al_enumerate(ni, le))) {
1390 if (le->type == ATTR_STD)
1393 if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1396 if (is_mft && le->type == ATTR_DATA)
1399 /* Find attribute in primary record. */
1400 attr = rec_find_attr_le(&ni->mi, le);
1406 asize = le32_to_cpu(attr->size);
1408 /* Always insert into new record to avoid collisions (deep recursive). */
1409 err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1410 attr->name_len, asize, attr_svcn(attr),
1411 le16_to_cpu(attr->name_off), true,
1412 &ins_attr, NULL, NULL);
1417 memcpy(ins_attr, attr, asize);
1418 ins_attr->id = le->id;
1419 /* Remove from primary record. */
1420 mi_remove_attr(NULL, &ni->mi, attr);
1427 err = -EFBIG; /* Attr list is too big(?) */
1431 /* Split MFT data as much as possible. */
1432 err = ni_expand_mft_list(ni);
1435 return !err && !done ? -EOPNOTSUPP : err;
1439 * ni_insert_nonresident - Insert new nonresident attribute.
1441 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1442 const __le16 *name, u8 name_len,
1443 const struct runs_tree *run, CLST svcn, CLST len,
1444 __le16 flags, struct ATTRIB **new_attr,
1445 struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
1449 struct ATTRIB *attr;
1450 bool is_ext = (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) &&
1452 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1453 u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1454 u32 run_off = name_off + name_size;
1455 u32 run_size, asize;
1456 struct ntfs_sb_info *sbi = ni->mi.sbi;
1458 /* Estimate packed size (run_buf=NULL). */
1459 err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1464 run_size = ALIGN(err, 8);
1471 asize = run_off + run_size;
1473 if (asize > sbi->max_bytes_per_attr) {
1478 err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
1485 attr->name_off = cpu_to_le16(name_off);
1486 attr->flags = flags;
1488 /* This function can't fail - cause already checked above. */
1489 run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1491 attr->nres.svcn = cpu_to_le64(svcn);
1492 attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1497 *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1499 attr->nres.alloc_size =
1500 svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1501 attr->nres.data_size = attr->nres.alloc_size;
1502 attr->nres.valid_size = attr->nres.alloc_size;
1505 if (flags & ATTR_FLAG_COMPRESSED)
1506 attr->nres.c_unit = NTFS_LZNT_CUNIT;
1507 attr->nres.total_size = attr->nres.alloc_size;
1515 * ni_insert_resident - Inserts new resident attribute.
1517 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1518 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
1519 struct ATTRIB **new_attr, struct mft_inode **mi,
1520 struct ATTR_LIST_ENTRY **le)
1523 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1524 u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
1525 struct ATTRIB *attr;
1527 err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
1535 attr->res.data_size = cpu_to_le32(data_size);
1536 attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
1537 if (type == ATTR_NAME) {
1538 attr->res.flags = RESIDENT_FLAG_INDEXED;
1540 /* is_attr_indexed(attr)) == true */
1541 le16_add_cpu(&ni->mi.mrec->hard_links, 1);
1542 ni->mi.dirty = true;
1553 * ni_remove_attr_le - Remove attribute from record.
1555 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1556 struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
1558 mi_remove_attr(ni, mi, attr);
1561 al_remove_le(ni, le);
1565 * ni_delete_all - Remove all attributes and frees allocates space.
1567 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
1569 int ni_delete_all(struct ntfs_inode *ni)
1572 struct ATTR_LIST_ENTRY *le = NULL;
1573 struct ATTRIB *attr = NULL;
1574 struct rb_node *node;
1578 struct ntfs_sb_info *sbi = ni->mi.sbi;
1579 bool nt3 = is_ntfs3(sbi);
1582 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1583 if (!nt3 || attr->name_len) {
1585 } else if (attr->type == ATTR_REPARSE) {
1586 mi_get_ref(&ni->mi, &ref);
1587 ntfs_remove_reparse(sbi, 0, &ref);
1588 } else if (attr->type == ATTR_ID && !attr->non_res &&
1589 le32_to_cpu(attr->res.data_size) >=
1590 sizeof(struct GUID)) {
1591 ntfs_objid_remove(sbi, resident_data(attr));
1597 svcn = le64_to_cpu(attr->nres.svcn);
1598 evcn = le64_to_cpu(attr->nres.evcn);
1600 if (evcn + 1 <= svcn)
1603 asize = le32_to_cpu(attr->size);
1604 roff = le16_to_cpu(attr->nres.run_off);
1607 _ntfs_bad_inode(&ni->vfs_inode);
1611 /* run==1 means unpack and deallocate. */
1612 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1613 Add2Ptr(attr, roff), asize - roff);
1616 if (ni->attr_list.size) {
1617 run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1621 /* Free all subrecords. */
1622 for (node = rb_first(&ni->mi_tree); node;) {
1623 struct rb_node *next = rb_next(node);
1624 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1626 clear_rec_inuse(mi->mrec);
1630 ntfs_mark_rec_free(sbi, mi->rno, false);
1631 ni_remove_mi(ni, mi);
1636 /* Free base record. */
1637 clear_rec_inuse(ni->mi.mrec);
1638 ni->mi.dirty = true;
1639 err = mi_write(&ni->mi, 0);
1641 ntfs_mark_rec_free(sbi, ni->mi.rno, false);
1648 * Return: File name attribute by its value.
1650 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1651 const struct le_str *uni,
1652 const struct MFT_REF *home_dir,
1653 struct mft_inode **mi,
1654 struct ATTR_LIST_ENTRY **le)
1656 struct ATTRIB *attr = NULL;
1657 struct ATTR_FILE_NAME *fname;
1662 /* Enumerate all names. */
1664 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1668 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1672 if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1678 if (uni->len != fname->name_len)
1681 if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL,
1690 * Return: File name attribute with given type.
1692 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
1693 struct mft_inode **mi,
1694 struct ATTR_LIST_ENTRY **le)
1696 struct ATTRIB *attr = NULL;
1697 struct ATTR_FILE_NAME *fname;
1701 if (name_type == FILE_NAME_POSIX)
1704 /* Enumerate all names. */
1706 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1710 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1711 if (fname && name_type == fname->type)
1719 * Process compressed/sparsed in special way.
1720 * NOTE: You need to set ni->std_fa = new_fa
1721 * after this function to keep internal structures in consistency.
1723 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1725 struct ATTRIB *attr;
1726 struct mft_inode *mi;
1730 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1734 new_aflags = attr->flags;
1736 if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1737 new_aflags |= ATTR_FLAG_SPARSED;
1739 new_aflags &= ~ATTR_FLAG_SPARSED;
1741 if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1742 new_aflags |= ATTR_FLAG_COMPRESSED;
1744 new_aflags &= ~ATTR_FLAG_COMPRESSED;
1746 if (new_aflags == attr->flags)
1749 if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1750 (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1751 ntfs_inode_warn(&ni->vfs_inode,
1752 "file can't be sparsed and compressed");
1759 if (attr->nres.data_size) {
1762 "one can change sparsed/compressed only for empty files");
1766 /* Resize nonresident empty attribute in-place only. */
1767 new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
1768 (SIZEOF_NONRESIDENT_EX + 8) :
1769 (SIZEOF_NONRESIDENT + 8);
1771 if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1774 if (new_aflags & ATTR_FLAG_SPARSED) {
1775 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1776 /* Windows uses 16 clusters per frame but supports one cluster per frame too. */
1777 attr->nres.c_unit = 0;
1778 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1779 } else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1780 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1781 /* The only allowed: 16 clusters per frame. */
1782 attr->nres.c_unit = NTFS_LZNT_CUNIT;
1783 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1785 attr->name_off = SIZEOF_NONRESIDENT_LE;
1787 attr->nres.c_unit = 0;
1788 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1790 attr->nres.run_off = attr->name_off;
1792 attr->flags = new_aflags;
1801 * buffer - memory for reparse buffer header
1803 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
1804 struct REPARSE_DATA_BUFFER *buffer)
1806 const struct REPARSE_DATA_BUFFER *rp = NULL;
1809 typeof(rp->CompressReparseBuffer) *cmpr;
1811 /* Try to estimate reparse point. */
1812 if (!attr->non_res) {
1813 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1814 } else if (le64_to_cpu(attr->nres.data_size) >=
1815 sizeof(struct REPARSE_DATA_BUFFER)) {
1816 struct runs_tree run;
1820 if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1821 !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1822 sizeof(struct REPARSE_DATA_BUFFER),
1831 return REPARSE_NONE;
1833 len = le16_to_cpu(rp->ReparseDataLength);
1834 switch (rp->ReparseTag) {
1835 case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
1836 break; /* Symbolic link. */
1837 case IO_REPARSE_TAG_MOUNT_POINT:
1838 break; /* Mount points and junctions. */
1839 case IO_REPARSE_TAG_SYMLINK:
1841 case IO_REPARSE_TAG_COMPRESS:
1843 * WOF - Windows Overlay Filter - Used to compress files with
1846 * Unlike native NTFS file compression, the Windows
1847 * Overlay Filter supports only read operations. This means
1848 * that it doesn't need to sector-align each compressed chunk,
1849 * so the compressed data can be packed more tightly together.
1850 * If you open the file for writing, the WOF just decompresses
1851 * the entire file, turning it back into a plain file.
1853 * Ntfs3 driver decompresses the entire file only on write or
1854 * change size requests.
1857 cmpr = &rp->CompressReparseBuffer;
1858 if (len < sizeof(*cmpr) ||
1859 cmpr->WofVersion != WOF_CURRENT_VERSION ||
1860 cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1861 cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1862 return REPARSE_NONE;
1865 switch (cmpr->CompressionFormat) {
1866 case WOF_COMPRESSION_XPRESS4K:
1869 case WOF_COMPRESSION_XPRESS8K:
1872 case WOF_COMPRESSION_XPRESS16K:
1875 case WOF_COMPRESSION_LZX32K:
1882 ni_set_ext_compress_bits(ni, bits);
1883 return REPARSE_COMPRESSED;
1885 case IO_REPARSE_TAG_DEDUP:
1886 ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1887 return REPARSE_DEDUPLICATED;
1890 if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1893 return REPARSE_NONE;
1897 memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
1899 /* Looks like normal symlink. */
1900 return REPARSE_LINK;
1904 * ni_fiemap - Helper for file_fiemap().
1907 * TODO: Less aggressive locks.
1909 int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
1910 __u64 vbo, __u64 len)
1913 struct ntfs_sb_info *sbi = ni->mi.sbi;
1914 u8 cluster_bits = sbi->cluster_bits;
1915 struct runs_tree run;
1916 struct ATTRIB *attr;
1917 CLST vcn = vbo >> cluster_bits;
1919 u64 valid = ni->i_valid;
1921 u64 end, alloc_size;
1927 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1928 attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME,
1929 ARRAY_SIZE(I30_NAME), NULL, NULL);
1931 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
1937 if (is_attr_compressed(attr)) {
1938 /* Unfortunately cp -r incorrectly treats compressed clusters. */
1942 "fiemap is not supported for compressed file (cp -r)");
1947 if (!attr || !attr->non_res) {
1948 err = fiemap_fill_next_extent(
1950 attr ? le32_to_cpu(attr->res.data_size) : 0,
1951 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST |
1952 FIEMAP_EXTENT_MERGED);
1957 alloc_size = le64_to_cpu(attr->nres.alloc_size);
1958 if (end > alloc_size)
1963 ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx);
1965 CLST vcn_next = vcn;
1967 ok = run_get_entry(&run, ++idx, &vcn, &lcn, &clen) &&
1974 err = attr_load_runs_vcn(ni, attr->type,
1976 attr->name_len, &run, vcn);
1981 ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx);
1994 if (lcn == SPARSE_LCN) {
1996 vbo = (u64)vcn << cluster_bits;
2000 flags = FIEMAP_EXTENT_MERGED;
2001 if (S_ISDIR(ni->vfs_inode.i_mode)) {
2003 } else if (is_attr_compressed(attr)) {
2006 err = attr_is_frame_compressed(ni, attr,
2007 vcn >> attr->nres.c_unit,
2011 if (clst_data < NTFS_LZNT_CLUSTERS)
2012 flags |= FIEMAP_EXTENT_ENCODED;
2013 } else if (is_attr_encrypted(attr)) {
2014 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
2017 vbo = (u64)vcn << cluster_bits;
2018 bytes = (u64)clen << cluster_bits;
2019 lbo = (u64)lcn << cluster_bits;
2023 if (vbo + bytes >= end)
2026 if (vbo + bytes <= valid) {
2028 } else if (vbo >= valid) {
2029 flags |= FIEMAP_EXTENT_UNWRITTEN;
2031 /* vbo < valid && valid < vbo + bytes */
2032 u64 dlen = valid - vbo;
2034 if (vbo + dlen >= end)
2035 flags |= FIEMAP_EXTENT_LAST;
2037 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
2053 flags |= FIEMAP_EXTENT_UNWRITTEN;
2056 if (vbo + bytes >= end)
2057 flags |= FIEMAP_EXTENT_LAST;
2059 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
2078 * When decompressing, we typically obtain more than one page per reference.
2079 * We inject the additional pages into the page cache.
2081 int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio)
2084 struct ntfs_sb_info *sbi = ni->mi.sbi;
2085 struct address_space *mapping = folio->mapping;
2086 pgoff_t index = folio->index;
2087 u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT;
2088 struct page **pages = NULL; /* Array of at most 16 pages. stack? */
2091 u32 i, idx, frame_size, pages_per_frame;
2095 if (vbo >= i_size_read(&ni->vfs_inode)) {
2096 folio_zero_range(folio, 0, folio_size(folio));
2097 folio_mark_uptodate(folio);
2102 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2103 /* Xpress or LZX. */
2104 frame_bits = ni_ext_compress_bits(ni);
2106 /* LZNT compression. */
2107 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2109 frame_size = 1u << frame_bits;
2110 frame = vbo >> frame_bits;
2111 frame_vbo = (u64)frame << frame_bits;
2112 idx = (vbo - frame_vbo) >> PAGE_SHIFT;
2114 pages_per_frame = frame_size >> PAGE_SHIFT;
2115 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2121 pages[idx] = &folio->page;
2122 index = frame_vbo >> PAGE_SHIFT;
2123 gfp_mask = mapping_gfp_mask(mapping);
2125 for (i = 0; i < pages_per_frame; i++, index++) {
2129 pg = find_or_create_page(mapping, index, gfp_mask);
2137 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame);
2140 for (i = 0; i < pages_per_frame; i++) {
2142 if (i == idx || !pg)
2149 /* At this point, err contains 0 or -EIO depending on the "critical" page. */
2151 folio_unlock(folio);
2156 #ifdef CONFIG_NTFS3_LZX_XPRESS
2158 * ni_decompress_file - Decompress LZX/Xpress compressed file.
2160 * Remove ATTR_DATA::WofCompressedData.
2161 * Remove ATTR_REPARSE.
2163 int ni_decompress_file(struct ntfs_inode *ni)
2165 struct ntfs_sb_info *sbi = ni->mi.sbi;
2166 struct inode *inode = &ni->vfs_inode;
2167 loff_t i_size = i_size_read(inode);
2168 struct address_space *mapping = inode->i_mapping;
2169 gfp_t gfp_mask = mapping_gfp_mask(mapping);
2170 struct page **pages = NULL;
2171 struct ATTR_LIST_ENTRY *le;
2172 struct ATTRIB *attr;
2173 CLST vcn, cend, lcn, clen, end;
2177 u32 i, frame_size, pages_per_frame, bytes;
2178 struct mft_inode *mi;
2181 /* Clusters for decompressed data. */
2182 cend = bytes_to_cluster(sbi, i_size);
2187 /* Check in advance. */
2188 if (cend > wnd_zeroes(&sbi->used.bitmap)) {
2193 frame_bits = ni_ext_compress_bits(ni);
2194 frame_size = 1u << frame_bits;
2195 pages_per_frame = frame_size >> PAGE_SHIFT;
2196 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2203 * Step 1: Decompress data and copy to new allocated clusters.
2206 for (vbo = 0; vbo < i_size; vbo += bytes) {
2210 if (vbo + frame_size > i_size) {
2211 bytes = i_size - vbo;
2212 nr_pages = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
2214 nr_pages = pages_per_frame;
2218 end = bytes_to_cluster(sbi, vbo + bytes);
2220 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2221 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2222 &clen, &new, false);
2227 for (i = 0; i < pages_per_frame; i++, index++) {
2230 pg = find_or_create_page(mapping, index, gfp_mask);
2233 unlock_page(pages[i]);
2242 err = ni_read_frame(ni, vbo, pages, pages_per_frame);
2245 down_read(&ni->file.run_lock);
2246 err = ntfs_bio_pages(sbi, &ni->file.run, pages,
2247 nr_pages, vbo, bytes,
2249 up_read(&ni->file.run_lock);
2252 for (i = 0; i < pages_per_frame; i++) {
2253 unlock_page(pages[i]);
2265 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2270 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2274 if (attr->type == ATTR_REPARSE) {
2277 mi_get_ref(&ni->mi, &ref);
2278 ntfs_remove_reparse(sbi, 0, &ref);
2284 if (attr->type != ATTR_REPARSE &&
2285 (attr->type != ATTR_DATA ||
2286 attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2287 memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2290 svcn = le64_to_cpu(attr->nres.svcn);
2291 evcn = le64_to_cpu(attr->nres.evcn);
2293 if (evcn + 1 <= svcn)
2296 asize = le32_to_cpu(attr->size);
2297 roff = le16_to_cpu(attr->nres.run_off);
2304 /*run==1 Means unpack and deallocate. */
2305 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2306 Add2Ptr(attr, roff), asize - roff);
2310 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
2312 err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2318 * Step 4: Remove ATTR_REPARSE.
2320 err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2325 * Step 5: Remove sparse flag from data attribute.
2327 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2333 if (attr->non_res && is_attr_sparsed(attr)) {
2334 /* Sparsed attribute header is 8 bytes bigger than normal. */
2335 struct MFT_REC *rec = mi->mrec;
2336 u32 used = le32_to_cpu(rec->used);
2337 u32 asize = le32_to_cpu(attr->size);
2338 u16 roff = le16_to_cpu(attr->nres.run_off);
2339 char *rbuf = Add2Ptr(attr, roff);
2341 memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2342 attr->size = cpu_to_le32(asize - 8);
2343 attr->flags &= ~ATTR_FLAG_SPARSED;
2344 attr->nres.run_off = cpu_to_le16(roff - 8);
2345 attr->nres.c_unit = 0;
2346 rec->used = cpu_to_le32(used - 8);
2348 ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2349 FILE_ATTRIBUTE_REPARSE_POINT);
2351 mark_inode_dirty(inode);
2354 /* Clear cached flag. */
2355 ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2356 if (ni->file.offs_folio) {
2357 folio_put(ni->file.offs_folio);
2358 ni->file.offs_folio = NULL;
2360 mapping->a_ops = &ntfs_aops;
2365 _ntfs_bad_inode(inode);
2371 * decompress_lzx_xpress - External compression LZX/Xpress.
2373 static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2374 size_t cmpr_size, void *unc, size_t unc_size,
2380 if (cmpr_size == unc_size) {
2381 /* Frame not compressed. */
2382 memcpy(unc, cmpr, unc_size);
2387 if (frame_size == 0x8000) {
2388 mutex_lock(&sbi->compress.mtx_lzx);
2389 /* LZX: Frame compressed. */
2390 ctx = sbi->compress.lzx;
2392 /* Lazy initialize LZX decompress context. */
2393 ctx = lzx_allocate_decompressor();
2399 sbi->compress.lzx = ctx;
2402 if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2403 /* Treat all errors as "invalid argument". */
2407 mutex_unlock(&sbi->compress.mtx_lzx);
2409 /* XPRESS: Frame compressed. */
2410 mutex_lock(&sbi->compress.mtx_xpress);
2411 ctx = sbi->compress.xpress;
2413 /* Lazy initialize Xpress decompress context. */
2414 ctx = xpress_allocate_decompressor();
2420 sbi->compress.xpress = ctx;
2423 if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2424 /* Treat all errors as "invalid argument". */
2428 mutex_unlock(&sbi->compress.mtx_xpress);
2437 * Pages - Array of locked pages.
2439 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2440 u32 pages_per_frame)
2443 struct ntfs_sb_info *sbi = ni->mi.sbi;
2444 u8 cluster_bits = sbi->cluster_bits;
2445 char *frame_ondisk = NULL;
2446 char *frame_mem = NULL;
2447 struct page **pages_disk = NULL;
2448 struct ATTR_LIST_ENTRY *le = NULL;
2449 struct runs_tree *run = &ni->file.run;
2450 u64 valid_size = ni->i_valid;
2453 u32 frame_size, i, npages_disk, ondisk_size;
2455 struct ATTRIB *attr;
2456 CLST frame, clst_data;
2459 * To simplify decompress algorithm do vmap for source
2462 for (i = 0; i < pages_per_frame; i++)
2465 frame_size = pages_per_frame << PAGE_SHIFT;
2466 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2472 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2478 if (!attr->non_res) {
2479 u32 data_size = le32_to_cpu(attr->res.data_size);
2481 memset(frame_mem, 0, frame_size);
2482 if (frame_vbo < data_size) {
2483 ondisk_size = data_size - frame_vbo;
2484 memcpy(frame_mem, resident_data(attr) + frame_vbo,
2485 min(ondisk_size, frame_size));
2491 if (frame_vbo >= valid_size) {
2492 memset(frame_mem, 0, frame_size);
2497 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2498 #ifndef CONFIG_NTFS3_LZX_XPRESS
2502 loff_t i_size = i_size_read(&ni->vfs_inode);
2503 u32 frame_bits = ni_ext_compress_bits(ni);
2504 u64 frame64 = frame_vbo >> frame_bits;
2505 u64 frames, vbo_data;
2507 if (frame_size != (1u << frame_bits)) {
2511 switch (frame_size) {
2518 /* Unknown compression. */
2523 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2524 ARRAY_SIZE(WOF_NAME), NULL, NULL);
2528 "external compressed file should contains data attribute \"WofCompressedData\"");
2533 if (!attr->non_res) {
2543 frames = (i_size - 1) >> frame_bits;
2545 err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2546 frame_bits, &ondisk_size, &vbo_data);
2550 if (frame64 == frames) {
2551 unc_size = 1 + ((i_size - 1) & (frame_size - 1));
2552 ondisk_size = attr_size(attr) - vbo_data;
2554 unc_size = frame_size;
2557 if (ondisk_size > frame_size) {
2562 if (!attr->non_res) {
2563 if (vbo_data + ondisk_size >
2564 le32_to_cpu(attr->res.data_size)) {
2569 err = decompress_lzx_xpress(
2570 sbi, Add2Ptr(resident_data(attr), vbo_data),
2571 ondisk_size, frame_mem, unc_size, frame_size);
2574 vbo_disk = vbo_data;
2575 /* Load all runs to read [vbo_disk-vbo_to). */
2576 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2577 ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2578 vbo_data + ondisk_size);
2581 npages_disk = (ondisk_size + (vbo_disk & (PAGE_SIZE - 1)) +
2585 } else if (is_attr_compressed(attr)) {
2586 /* LZNT compression. */
2587 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2592 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2597 down_write(&ni->file.run_lock);
2598 run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2599 frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2600 err = attr_is_frame_compressed(ni, attr, frame, &clst_data,
2602 up_write(&ni->file.run_lock);
2607 memset(frame_mem, 0, frame_size);
2611 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2612 ondisk_size = clst_data << cluster_bits;
2614 if (clst_data >= NTFS_LZNT_CLUSTERS) {
2615 /* Frame is not compressed. */
2616 down_read(&ni->file.run_lock);
2617 err = ntfs_bio_pages(sbi, run, pages, pages_per_frame,
2618 frame_vbo, ondisk_size,
2620 up_read(&ni->file.run_lock);
2623 vbo_disk = frame_vbo;
2624 npages_disk = (ondisk_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2626 __builtin_unreachable();
2631 pages_disk = kcalloc(npages_disk, sizeof(*pages_disk), GFP_NOFS);
2637 for (i = 0; i < npages_disk; i++) {
2638 pg = alloc_page(GFP_KERNEL);
2648 /* Read 'ondisk_size' bytes from disk. */
2649 down_read(&ni->file.run_lock);
2650 err = ntfs_bio_pages(sbi, run, pages_disk, npages_disk, vbo_disk,
2651 ondisk_size, REQ_OP_READ);
2652 up_read(&ni->file.run_lock);
2657 * To simplify decompress algorithm do vmap for source and target pages.
2659 frame_ondisk = vmap(pages_disk, npages_disk, VM_MAP, PAGE_KERNEL_RO);
2660 if (!frame_ondisk) {
2665 /* Decompress: Frame_ondisk -> frame_mem. */
2666 #ifdef CONFIG_NTFS3_LZX_XPRESS
2667 if (run != &ni->file.run) {
2669 err = decompress_lzx_xpress(
2670 sbi, frame_ondisk + (vbo_disk & (PAGE_SIZE - 1)),
2671 ondisk_size, frame_mem, unc_size, frame_size);
2675 /* LZNT - Native NTFS compression. */
2676 unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2678 if ((ssize_t)unc_size < 0)
2680 else if (!unc_size || unc_size > frame_size)
2683 if (!err && valid_size < frame_vbo + frame_size) {
2684 size_t ok = valid_size - frame_vbo;
2686 memset(frame_mem + ok, 0, frame_size - ok);
2689 vunmap(frame_ondisk);
2692 for (i = 0; i < npages_disk; i++) {
2703 #ifdef CONFIG_NTFS3_LZX_XPRESS
2704 if (run != &ni->file.run)
2710 for (i = 0; i < pages_per_frame; i++) {
2713 SetPageUptodate(pg);
2722 * Pages - Array of locked pages.
2724 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2725 u32 pages_per_frame)
2728 struct ntfs_sb_info *sbi = ni->mi.sbi;
2729 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2730 u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2731 u64 frame_vbo = (u64)pages[0]->index << PAGE_SHIFT;
2732 CLST frame = frame_vbo >> frame_bits;
2733 char *frame_ondisk = NULL;
2734 struct page **pages_disk = NULL;
2735 struct ATTR_LIST_ENTRY *le = NULL;
2737 struct ATTRIB *attr;
2738 struct mft_inode *mi;
2741 size_t compr_size, ondisk_size;
2744 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2750 if (WARN_ON(!is_attr_compressed(attr))) {
2755 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2760 if (!attr->non_res) {
2761 down_write(&ni->file.run_lock);
2762 err = attr_make_nonresident(ni, attr, le, mi,
2763 le32_to_cpu(attr->res.data_size),
2764 &ni->file.run, &attr, pages[0]);
2765 up_write(&ni->file.run_lock);
2770 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2775 pages_disk = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2781 for (i = 0; i < pages_per_frame; i++) {
2782 pg = alloc_page(GFP_KERNEL);
2792 /* To simplify compress algorithm do vmap for source and target pages. */
2793 frame_ondisk = vmap(pages_disk, pages_per_frame, VM_MAP, PAGE_KERNEL);
2794 if (!frame_ondisk) {
2799 for (i = 0; i < pages_per_frame; i++)
2802 /* Map in-memory frame for read-only. */
2803 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2809 mutex_lock(&sbi->compress.mtx_lznt);
2811 if (!sbi->compress.lznt) {
2813 * LZNT implements two levels of compression:
2814 * 0 - Standard compression
2815 * 1 - Best compression, requires a lot of cpu
2818 lznt = get_lznt_ctx(0);
2820 mutex_unlock(&sbi->compress.mtx_lznt);
2825 sbi->compress.lznt = lznt;
2829 /* Compress: frame_mem -> frame_ondisk */
2830 compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2831 frame_size, sbi->compress.lznt);
2832 mutex_unlock(&sbi->compress.mtx_lznt);
2835 if (compr_size + sbi->cluster_size > frame_size) {
2836 /* Frame is not compressed. */
2837 compr_size = frame_size;
2838 ondisk_size = frame_size;
2839 } else if (compr_size) {
2840 /* Frame is compressed. */
2841 ondisk_size = ntfs_up_cluster(sbi, compr_size);
2842 memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2844 /* Frame is sparsed. */
2848 down_write(&ni->file.run_lock);
2849 run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2850 err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2851 up_write(&ni->file.run_lock);
2858 down_read(&ni->file.run_lock);
2859 err = ntfs_bio_pages(sbi, &ni->file.run,
2860 ondisk_size < frame_size ? pages_disk : pages,
2861 pages_per_frame, frame_vbo, ondisk_size,
2863 up_read(&ni->file.run_lock);
2869 for (i = 0; i < pages_per_frame; i++)
2872 vunmap(frame_ondisk);
2874 for (i = 0; i < pages_per_frame; i++) {
2888 * ni_remove_name - Removes name 'de' from MFT and from directory.
2889 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2891 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2892 struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2895 struct ntfs_sb_info *sbi = ni->mi.sbi;
2896 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2897 struct ATTR_FILE_NAME *fname;
2898 struct ATTR_LIST_ENTRY *le;
2899 struct mft_inode *mi;
2900 u16 de_key_size = le16_to_cpu(de->key_size);
2905 /* Find name in record. */
2906 mi_get_ref(&dir_ni->mi, &de_name->home);
2908 fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len,
2909 &de_name->home, &mi, &le);
2913 memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2914 name_type = paired_name(fname->type);
2916 /* Mark ntfs as dirty. It will be cleared at umount. */
2917 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2919 /* Step 1: Remove name from directory. */
2920 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2924 /* Step 2: Remove name from MFT. */
2925 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2929 /* Get paired name. */
2930 fname = ni_fname_type(ni, name_type, &mi, &le);
2932 u16 de2_key_size = fname_full_size(fname);
2934 *de2 = Add2Ptr(de, 1024);
2935 (*de2)->key_size = cpu_to_le16(de2_key_size);
2937 memcpy(*de2 + 1, fname, de2_key_size);
2939 /* Step 3: Remove paired name from directory. */
2940 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2945 /* Step 4: Remove paired name from MFT. */
2946 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2954 * ni_remove_name_undo - Paired function for ni_remove_name.
2956 * Return: True if ok
2958 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2959 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2961 struct ntfs_sb_info *sbi = ni->mi.sbi;
2962 struct ATTRIB *attr;
2965 switch (undo_step) {
2967 de_key_size = le16_to_cpu(de2->key_size);
2968 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2971 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2973 mi_get_ref(&ni->mi, &de2->ref);
2974 de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2975 sizeof(struct NTFS_DE));
2979 if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL, 1))
2984 de_key_size = le16_to_cpu(de->key_size);
2986 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2990 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2991 mi_get_ref(&ni->mi, &de->ref);
2993 if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
3001 * ni_add_name - Add new name into MFT and into directory.
3003 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
3007 struct ntfs_sb_info *sbi = ni->mi.sbi;
3008 struct ATTRIB *attr;
3009 struct ATTR_LIST_ENTRY *le;
3010 struct mft_inode *mi;
3011 struct ATTR_FILE_NAME *fname;
3012 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
3013 u16 de_key_size = le16_to_cpu(de->key_size);
3015 if (sbi->options->windows_names &&
3016 !valid_windows_name(sbi, (struct le_str *)&de_name->name_len))
3019 /* If option "hide_dot_files" then set hidden attribute for dot files. */
3020 if (ni->mi.sbi->options->hide_dot_files) {
3021 if (de_name->name_len > 0 &&
3022 le16_to_cpu(de_name->name[0]) == '.')
3023 ni->std_fa |= FILE_ATTRIBUTE_HIDDEN;
3025 ni->std_fa &= ~FILE_ATTRIBUTE_HIDDEN;
3028 mi_get_ref(&ni->mi, &de->ref);
3029 mi_get_ref(&dir_ni->mi, &de_name->home);
3031 /* Fill duplicate from any ATTR_NAME. */
3032 fname = ni_fname_name(ni, NULL, NULL, NULL, NULL);
3034 memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup));
3035 de_name->dup.fa = ni->std_fa;
3037 /* Insert new name into MFT. */
3038 err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
3043 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
3045 /* Insert new name into directory. */
3046 err = indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 0);
3048 ni_remove_attr_le(ni, attr, mi, le);
3054 * ni_rename - Remove one name and insert new name.
3056 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
3057 struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de,
3061 struct NTFS_DE *de2 = NULL;
3065 * There are two possible ways to rename:
3066 * 1) Add new name and remove old name.
3067 * 2) Remove old name and add new name.
3069 * In most cases (not all!) adding new name into MFT and into directory can
3070 * allocate additional cluster(s).
3071 * Second way may result to bad inode if we can't add new name
3072 * and then can't restore (add) old name.
3076 * Way 1 - Add new + remove old.
3078 err = ni_add_name(new_dir_ni, ni, new_de);
3080 err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3081 if (err && ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo))
3086 * Way 2 - Remove old + add new.
3089 * err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3091 * err = ni_add_name(new_dir_ni, ni, new_de);
3092 * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
3101 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
3103 bool ni_is_dirty(struct inode *inode)
3105 struct ntfs_inode *ni = ntfs_i(inode);
3106 struct rb_node *node;
3108 if (ni->mi.dirty || ni->attr_list.dirty ||
3109 (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3112 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
3113 if (rb_entry(node, struct mft_inode, node)->dirty)
3123 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
3125 static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
3128 struct ATTRIB *attr;
3129 struct mft_inode *mi;
3130 struct ATTR_LIST_ENTRY *le = NULL;
3131 struct ntfs_sb_info *sbi = ni->mi.sbi;
3132 struct super_block *sb = sbi->sb;
3133 bool re_dirty = false;
3135 if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
3136 dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
3138 dup->alloc_size = 0;
3141 dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
3143 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
3146 dup->alloc_size = dup->data_size = 0;
3147 } else if (!attr->non_res) {
3148 u32 data_size = le32_to_cpu(attr->res.data_size);
3150 dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
3151 dup->data_size = cpu_to_le64(data_size);
3153 u64 new_valid = ni->i_valid;
3154 u64 data_size = le64_to_cpu(attr->nres.data_size);
3157 dup->alloc_size = is_attr_ext(attr) ?
3158 attr->nres.total_size :
3159 attr->nres.alloc_size;
3160 dup->data_size = attr->nres.data_size;
3162 if (new_valid > data_size)
3163 new_valid = data_size;
3165 valid_le = cpu_to_le64(new_valid);
3166 if (valid_le != attr->nres.valid_size) {
3167 attr->nres.valid_size = valid_le;
3173 /* TODO: Fill reparse info. */
3177 if (ni->ni_flags & NI_FLAG_EA) {
3178 attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3181 const struct EA_INFO *info;
3183 info = resident_data_ex(attr, sizeof(struct EA_INFO));
3184 /* If ATTR_EA_INFO exists 'info' can't be NULL. */
3186 dup->ea_size = info->size_pack;
3193 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3196 struct ATTR_FILE_NAME *fname;
3198 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
3199 if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
3202 /* Check simple case when parent inode equals current inode. */
3203 if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
3204 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3208 /* ntfs_iget5 may sleep. */
3209 dir = ntfs_iget5(sb, &fname->home, NULL);
3213 "failed to open parent directory r=%lx to update",
3214 (long)ino_get(&fname->home));
3218 if (!is_bad_inode(dir)) {
3219 struct ntfs_inode *dir_ni = ntfs_i(dir);
3221 if (!ni_trylock(dir_ni)) {
3224 indx_update_dup(dir_ni, sbi, fname, dup, sync);
3226 memcpy(&fname->dup, dup, sizeof(fname->dup));
3237 * ni_write_inode - Write MFT base record and all subrecords to disk.
3239 int ni_write_inode(struct inode *inode, int sync, const char *hint)
3242 struct ntfs_inode *ni = ntfs_i(inode);
3243 struct super_block *sb = inode->i_sb;
3244 struct ntfs_sb_info *sbi = sb->s_fs_info;
3245 bool re_dirty = false;
3246 struct ATTR_STD_INFO *std;
3247 struct rb_node *node, *next;
3248 struct NTFS_DUP_INFO dup;
3250 if (is_bad_inode(inode) || sb_rdonly(sb))
3253 if (unlikely(ntfs3_forced_shutdown(sb)))
3256 if (!ni_trylock(ni)) {
3257 /* 'ni' is under modification, skip for now. */
3258 mark_inode_dirty_sync(inode);
3265 if (is_rec_inuse(ni->mi.mrec) &&
3266 !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3267 bool modified = false;
3268 struct timespec64 ts;
3270 /* Update times in standard attribute. */
3277 /* Update the access times if they have changed. */
3278 ts = inode_get_mtime(inode);
3279 dup.m_time = kernel2nt(&ts);
3280 if (std->m_time != dup.m_time) {
3281 std->m_time = dup.m_time;
3285 ts = inode_get_ctime(inode);
3286 dup.c_time = kernel2nt(&ts);
3287 if (std->c_time != dup.c_time) {
3288 std->c_time = dup.c_time;
3292 ts = inode_get_atime(inode);
3293 dup.a_time = kernel2nt(&ts);
3294 if (std->a_time != dup.a_time) {
3295 std->a_time = dup.a_time;
3299 dup.fa = ni->std_fa;
3300 if (std->fa != dup.fa) {
3305 /* std attribute is always in primary MFT record. */
3307 ni->mi.dirty = true;
3309 if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
3310 (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3311 /* Avoid __wait_on_freeing_inode(inode). */
3312 && (sb->s_flags & SB_ACTIVE)) {
3313 dup.cr_time = std->cr_time;
3314 /* Not critical if this function fail. */
3315 re_dirty = ni_update_parent(ni, &dup, sync);
3318 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3320 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3323 /* Update attribute list. */
3324 if (ni->attr_list.size && ni->attr_list.dirty) {
3325 if (inode->i_ino != MFT_REC_MFT || sync) {
3326 err = ni_try_remove_attr_list(ni);
3331 err = al_update(ni, sync);
3337 for (node = rb_first(&ni->mi_tree); node; node = next) {
3338 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3341 next = rb_next(node);
3346 is_empty = !mi_enum_attr(mi, NULL);
3349 clear_rec_inuse(mi->mrec);
3351 err2 = mi_write(mi, sync);
3356 ntfs_mark_rec_free(sbi, mi->rno, false);
3357 rb_erase(node, &ni->mi_tree);
3363 err2 = mi_write(&ni->mi, sync);
3371 ntfs_inode_err(inode, "%s failed, %d.", hint, err);
3372 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3377 mark_inode_dirty_sync(inode);
3385 * Helper for 'ntfs_fileattr_set'.
3386 * Changes compression for empty files and directories only.
3388 int ni_set_compress(struct inode *inode, bool compr)
3391 struct ntfs_inode *ni = ntfs_i(inode);
3392 struct ATTR_STD_INFO *std;
3393 const char *bad_inode;
3395 if (is_compressed(ni) == !!compr)
3398 if (is_sparsed(ni)) {
3399 /* sparse and compress not compatible. */
3403 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) {
3404 /*Skip other inodes. (symlink,fifo,...) */
3414 bad_inode = "no std";
3418 if (S_ISREG(inode->i_mode)) {
3419 err = attr_set_compress(ni, compr);
3421 if (err == -ENOENT) {
3422 /* Fix on the fly? */
3423 /* Each file must contain data attribute. */
3424 bad_inode = "no data attribute";
3430 ni->std_fa = std->fa;
3432 std->fa |= FILE_ATTRIBUTE_COMPRESSED;
3434 std->fa &= ~FILE_ATTRIBUTE_COMPRESSED;
3436 if (ni->std_fa != std->fa) {
3437 ni->std_fa = std->fa;
3438 ni->mi.dirty = true;
3440 /* update duplicate information and directory entries in ni_write_inode.*/
3441 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3447 ntfs_bad_inode(inode, bad_inode);