xref: /linux/fs/ntfs3/frecord.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7 
8 #include <linux/fiemap.h>
9 #include <linux/fs.h>
10 #include <linux/minmax.h>
11 #include <linux/vmalloc.h>
12 
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16 #ifdef CONFIG_NTFS3_LZX_XPRESS
17 #include "lib/lib.h"
18 #endif
19 
20 static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
21 				   CLST ino, struct rb_node *ins)
22 {
23 	struct rb_node **p = &tree->rb_node;
24 	struct rb_node *pr = NULL;
25 
26 	while (*p) {
27 		struct mft_inode *mi;
28 
29 		pr = *p;
30 		mi = rb_entry(pr, struct mft_inode, node);
31 		if (mi->rno > ino)
32 			p = &pr->rb_left;
33 		else if (mi->rno < ino)
34 			p = &pr->rb_right;
35 		else
36 			return mi;
37 	}
38 
39 	if (!ins)
40 		return NULL;
41 
42 	rb_link_node(ins, pr, p);
43 	rb_insert_color(ins, tree);
44 	return rb_entry(ins, struct mft_inode, node);
45 }
46 
47 /*
48  * ni_find_mi - Find mft_inode by record number.
49  */
50 static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
51 {
52 	return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
53 }
54 
55 /*
56  * ni_add_mi - Add new mft_inode into ntfs_inode.
57  */
58 static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
59 {
60 	ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
61 }
62 
63 /*
64  * ni_remove_mi - Remove mft_inode from ntfs_inode.
65  */
66 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
67 {
68 	rb_erase(&mi->node, &ni->mi_tree);
69 }
70 
71 /*
72  * ni_std - Return: Pointer into std_info from primary record.
73  */
74 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
75 {
76 	const struct ATTRIB *attr;
77 
78 	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
79 	return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO)) :
80 		      NULL;
81 }
82 
83 /*
84  * ni_std5
85  *
86  * Return: Pointer into std_info from primary record.
87  */
88 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
89 {
90 	const struct ATTRIB *attr;
91 
92 	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
93 
94 	return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5)) :
95 		      NULL;
96 }
97 
98 /*
99  * ni_clear - Clear resources allocated by ntfs_inode.
100  */
101 void ni_clear(struct ntfs_inode *ni)
102 {
103 	struct rb_node *node;
104 
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))
108 		ni_delete_all(ni);
109 
110 	al_destroy(ni);
111 
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);
115 
116 		rb_erase(node, &ni->mi_tree);
117 		mi_put(mi);
118 		node = next;
119 	}
120 
121 	/* Bad inode always has mode == S_IFREG. */
122 	if (ni->ni_flags & NI_FLAG_DIR)
123 		indx_clear(&ni->dir);
124 	else {
125 		run_close(&ni->file.run);
126 		ntfs_sub_da(ni->mi.sbi, run_len(&ni->file.run_da));
127 		run_close(&ni->file.run_da);
128 #ifdef CONFIG_NTFS3_LZX_XPRESS
129 		if (ni->file.offs_folio) {
130 			/* On-demand allocated page for offsets. */
131 			folio_put(ni->file.offs_folio);
132 			ni->file.offs_folio = NULL;
133 		}
134 #endif
135 	}
136 
137 	mi_clear(&ni->mi);
138 }
139 
140 /*
141  * ni_load_mi_ex - Find mft_inode by record number.
142  */
143 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
144 {
145 	int err;
146 	struct mft_inode *r;
147 
148 	r = ni_find_mi(ni, rno);
149 	if (r)
150 		goto out;
151 
152 	err = mi_get(ni->mi.sbi, rno, &r);
153 	if (err) {
154 		_ntfs_bad_inode(&ni->vfs_inode);
155 		return err;
156 	}
157 
158 	ni_add_mi(ni, r);
159 
160 out:
161 	if (mi)
162 		*mi = r;
163 	return 0;
164 }
165 
166 /*
167  * ni_load_mi - Load mft_inode corresponded list_entry.
168  */
169 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
170 	       struct mft_inode **mi)
171 {
172 	CLST rno;
173 
174 	if (!le) {
175 		*mi = &ni->mi;
176 		return 0;
177 	}
178 
179 	rno = ino_get(&le->ref);
180 	if (rno == ni->mi.rno) {
181 		*mi = &ni->mi;
182 		return 0;
183 	}
184 	return ni_load_mi_ex(ni, rno, mi);
185 }
186 
187 /*
188  * ni_find_attr
189  *
190  * Return: Attribute and record this attribute belongs to.
191  */
192 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
193 			    struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
194 			    const __le16 *name, u8 name_len, const CLST *vcn,
195 			    struct mft_inode **mi)
196 {
197 	struct ATTR_LIST_ENTRY *le;
198 	struct mft_inode *m;
199 
200 	if (!ni->attr_list.size ||
201 	    (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
202 		if (le_o)
203 			*le_o = NULL;
204 		if (mi)
205 			*mi = &ni->mi;
206 
207 		/* Look for required attribute in primary record. */
208 		return mi_find_attr(ni, &ni->mi, attr, type, name, name_len,
209 				    NULL);
210 	}
211 
212 	/* First look for list entry of required type. */
213 	le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
214 	if (!le)
215 		return NULL;
216 
217 	if (le_o)
218 		*le_o = le;
219 
220 	/* Load record that contains this attribute. */
221 	if (ni_load_mi(ni, le, &m))
222 		return NULL;
223 
224 	/* Look for required attribute. */
225 	attr = mi_find_attr(ni, m, NULL, type, name, name_len, &le->id);
226 
227 	if (!attr)
228 		goto out;
229 
230 	if (!attr->non_res) {
231 		if (vcn && *vcn)
232 			goto out;
233 	} else if (!vcn) {
234 		if (attr->nres.svcn)
235 			goto out;
236 	} else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
237 		   *vcn > le64_to_cpu(attr->nres.evcn)) {
238 		goto out;
239 	}
240 
241 	if (mi)
242 		*mi = m;
243 	return attr;
244 
245 out:
246 	_ntfs_bad_inode(&ni->vfs_inode);
247 	return NULL;
248 }
249 
250 /*
251  * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
252  */
253 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
254 			       struct ATTR_LIST_ENTRY **le,
255 			       struct mft_inode **mi)
256 {
257 	struct mft_inode *mi2;
258 	struct ATTR_LIST_ENTRY *le2;
259 
260 	/* Do we have an attribute list? */
261 	if (!ni->attr_list.size) {
262 		*le = NULL;
263 		if (mi)
264 			*mi = &ni->mi;
265 		/* Enum attributes in primary record. */
266 		return mi_enum_attr(ni, &ni->mi, attr);
267 	}
268 
269 	/* Get next list entry. */
270 	le2 = *le = al_enumerate(ni, attr ? *le : NULL);
271 	if (!le2)
272 		return NULL;
273 
274 	/* Load record that contains the required attribute. */
275 	if (ni_load_mi(ni, le2, &mi2))
276 		return NULL;
277 
278 	if (mi)
279 		*mi = mi2;
280 
281 	/* Find attribute in loaded record. */
282 	return rec_find_attr_le(ni, mi2, le2);
283 }
284 
285 /*
286  * ni_load_all_mi - Load all subrecords.
287  */
288 int ni_load_all_mi(struct ntfs_inode *ni)
289 {
290 	int err;
291 	struct ATTR_LIST_ENTRY *le;
292 
293 	if (!ni->attr_list.size)
294 		return 0;
295 
296 	le = NULL;
297 
298 	while ((le = al_enumerate(ni, le))) {
299 		CLST rno = ino_get(&le->ref);
300 
301 		if (rno == ni->mi.rno)
302 			continue;
303 
304 		err = ni_load_mi_ex(ni, rno, NULL);
305 		if (err)
306 			return err;
307 	}
308 
309 	return 0;
310 }
311 
312 /*
313  * ni_add_subrecord - Allocate + format + attach a new subrecord.
314  */
315 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
316 {
317 	struct mft_inode *m;
318 
319 	m = kzalloc_obj(struct mft_inode, GFP_NOFS);
320 	if (!m)
321 		return false;
322 
323 	if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
324 		mi_put(m);
325 		return false;
326 	}
327 
328 	mi_get_ref(&ni->mi, &m->mrec->parent_ref);
329 
330 	*mi = ni_ins_mi(ni, &ni->mi_tree, m->rno, &m->node);
331 	if (*mi != m)
332 		mi_put(m);
333 
334 	return true;
335 }
336 
337 /*
338  * ni_remove_attr - Remove all attributes for the given type/name/id.
339  */
340 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
341 		   const __le16 *name, u8 name_len, bool base_only,
342 		   const __le16 *id)
343 {
344 	int err;
345 	struct ATTRIB *attr;
346 	struct ATTR_LIST_ENTRY *le;
347 	struct mft_inode *mi;
348 	u32 type_in;
349 	int diff;
350 
351 	if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
352 		attr = mi_find_attr(ni, &ni->mi, NULL, type, name, name_len,
353 				    id);
354 		if (!attr)
355 			return -ENOENT;
356 
357 		mi_remove_attr(ni, &ni->mi, attr);
358 		return 0;
359 	}
360 
361 	type_in = le32_to_cpu(type);
362 	le = NULL;
363 
364 	for (;;) {
365 		le = al_enumerate(ni, le);
366 		if (!le)
367 			return 0;
368 
369 next_le2:
370 		diff = le32_to_cpu(le->type) - type_in;
371 		if (diff < 0)
372 			continue;
373 
374 		if (diff > 0)
375 			return 0;
376 
377 		if (le->name_len != name_len)
378 			continue;
379 
380 		if (name_len &&
381 		    memcmp(le_name(le), name, name_len * sizeof(short)))
382 			continue;
383 
384 		if (id && le->id != *id)
385 			continue;
386 		err = ni_load_mi(ni, le, &mi);
387 		if (err)
388 			return err;
389 
390 		al_remove_le(ni, le);
391 
392 		attr = mi_find_attr(ni, mi, NULL, type, name, name_len, id);
393 		if (!attr)
394 			return -ENOENT;
395 
396 		mi_remove_attr(ni, mi, attr);
397 
398 		if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
399 			return 0;
400 		goto next_le2;
401 	}
402 }
403 
404 /*
405  * ni_ins_new_attr - Insert the attribute into record.
406  *
407  * Return: Not full constructed attribute or NULL if not possible to create.
408  */
409 static struct ATTRIB *
410 ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
411 		struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
412 		const __le16 *name, u8 name_len, u32 asize, u16 name_off,
413 		CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
414 {
415 	int err;
416 	struct ATTRIB *attr;
417 	bool le_added = false;
418 	struct MFT_REF ref;
419 
420 	mi_get_ref(mi, &ref);
421 
422 	if (type != ATTR_LIST && !le && ni->attr_list.size) {
423 		err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
424 				&ref, &le);
425 		if (err) {
426 			/* No memory or no space. */
427 			return ERR_PTR(err);
428 		}
429 		le_added = true;
430 
431 		/*
432 		 * al_add_le -> attr_set_size (list) -> ni_expand_list
433 		 * which moves some attributes out of primary record
434 		 * this means that name may point into moved memory
435 		 * reinit 'name' from le.
436 		 */
437 		name = le->name;
438 	}
439 
440 	attr = mi_insert_attr(ni, mi, type, name, name_len, asize, name_off);
441 	if (!attr) {
442 		if (le_added)
443 			al_remove_le(ni, le);
444 		return NULL;
445 	}
446 
447 	if (type == ATTR_LIST) {
448 		/* Attr list is not in list entry array. */
449 		goto out;
450 	}
451 
452 	if (!le)
453 		goto out;
454 
455 	/* Update ATTRIB Id and record reference. */
456 	le->id = attr->id;
457 	ni->attr_list.dirty = true;
458 	le->ref = ref;
459 
460 out:
461 	if (ins_le)
462 		*ins_le = le;
463 	return attr;
464 }
465 
466 /*
467  * ni_repack
468  *
469  * Random write access to sparsed or compressed file may result to
470  * not optimized packed runs.
471  * Here is the place to optimize it.
472  */
473 static int ni_repack(struct ntfs_inode *ni)
474 {
475 #if 1
476 	return 0;
477 #else
478 	int err = 0;
479 	struct ntfs_sb_info *sbi = ni->mi.sbi;
480 	struct mft_inode *mi, *mi_p = NULL;
481 	struct ATTRIB *attr = NULL, *attr_p;
482 	struct ATTR_LIST_ENTRY *le = NULL, *le_p;
483 	CLST alloc = 0;
484 	u8 cluster_bits = sbi->cluster_bits;
485 	CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
486 	u32 roff, rs = sbi->record_size;
487 	struct runs_tree run;
488 
489 	run_init(&run);
490 
491 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
492 		if (!attr->non_res)
493 			continue;
494 
495 		svcn = le64_to_cpu(attr->nres.svcn);
496 		if (svcn != le64_to_cpu(le->vcn)) {
497 			err = -EINVAL;
498 			break;
499 		}
500 
501 		if (!svcn) {
502 			alloc = le64_to_cpu(attr->nres.alloc_size) >>
503 				cluster_bits;
504 			mi_p = NULL;
505 		} else if (svcn != evcn + 1) {
506 			err = -EINVAL;
507 			break;
508 		}
509 
510 		evcn = le64_to_cpu(attr->nres.evcn);
511 
512 		if (svcn > evcn + 1) {
513 			err = -EINVAL;
514 			break;
515 		}
516 
517 		if (!mi_p) {
518 			/* Do not try if not enough free space. */
519 			if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
520 				continue;
521 
522 			/* Do not try if last attribute segment. */
523 			if (evcn + 1 == alloc)
524 				continue;
525 			run_close(&run);
526 		}
527 
528 		roff = le16_to_cpu(attr->nres.run_off);
529 
530 		if (roff > le32_to_cpu(attr->size)) {
531 			err = -EINVAL;
532 			break;
533 		}
534 
535 		err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
536 				 Add2Ptr(attr, roff),
537 				 le32_to_cpu(attr->size) - roff);
538 		if (err < 0)
539 			break;
540 
541 		if (!mi_p) {
542 			mi_p = mi;
543 			attr_p = attr;
544 			svcn_p = svcn;
545 			evcn_p = evcn;
546 			le_p = le;
547 			err = 0;
548 			continue;
549 		}
550 
551 		/*
552 		 * Run contains data from two records: mi_p and mi
553 		 * Try to pack in one.
554 		 */
555 		err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
556 		if (err)
557 			break;
558 
559 		next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
560 
561 		if (next_svcn >= evcn + 1) {
562 			/* We can remove this attribute segment. */
563 			al_remove_le(ni, le);
564 			mi_remove_attr(NULL, mi, attr);
565 			le = le_p;
566 			continue;
567 		}
568 
569 		attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
570 		mi->dirty = true;
571 		ni->attr_list.dirty = true;
572 
573 		if (evcn + 1 == alloc) {
574 			err = mi_pack_runs(mi, attr, &run,
575 					   evcn + 1 - next_svcn);
576 			if (err)
577 				break;
578 			mi_p = NULL;
579 		} else {
580 			mi_p = mi;
581 			attr_p = attr;
582 			svcn_p = next_svcn;
583 			evcn_p = evcn;
584 			le_p = le;
585 			run_truncate_head(&run, next_svcn);
586 		}
587 	}
588 
589 	if (err) {
590 		ntfs_inode_warn(&ni->vfs_inode, "repack problem");
591 		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
592 
593 		/* Pack loaded but not packed runs. */
594 		if (mi_p)
595 			mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
596 	}
597 
598 	run_close(&run);
599 	return err;
600 #endif
601 }
602 
603 /*
604  * ni_try_remove_attr_list
605  *
606  * Can we remove attribute list?
607  * Check the case when primary record contains enough space for all attributes.
608  */
609 static int ni_try_remove_attr_list(struct ntfs_inode *ni)
610 {
611 	int err = 0;
612 	struct ntfs_sb_info *sbi = ni->mi.sbi;
613 	struct ATTRIB *attr, *attr_list, *attr_ins;
614 	struct ATTR_LIST_ENTRY *le;
615 	struct mft_inode *mi;
616 	u32 asize, free;
617 	struct MFT_REF ref;
618 	struct MFT_REC *mrec;
619 	__le16 id;
620 
621 	if (!ni->attr_list.dirty)
622 		return 0;
623 
624 	err = ni_repack(ni);
625 	if (err)
626 		return err;
627 
628 	attr_list = mi_find_attr(ni, &ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
629 	if (!attr_list)
630 		return 0;
631 
632 	asize = le32_to_cpu(attr_list->size);
633 
634 	/* Free space in primary record without attribute list. */
635 	free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
636 	mi_get_ref(&ni->mi, &ref);
637 
638 	le = NULL;
639 	while ((le = al_enumerate(ni, le))) {
640 		if (!memcmp(&le->ref, &ref, sizeof(ref)))
641 			continue;
642 
643 		if (le->vcn)
644 			return 0;
645 
646 		mi = ni_find_mi(ni, ino_get(&le->ref));
647 		if (!mi)
648 			return 0;
649 
650 		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
651 				    le->name_len, &le->id);
652 		if (!attr)
653 			return 0;
654 
655 		asize = le32_to_cpu(attr->size);
656 		if (asize > free)
657 			return 0;
658 
659 		free -= asize;
660 	}
661 
662 	/* Make a copy of primary record to restore if error. */
663 	mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
664 	if (!mrec)
665 		return 0; /* Not critical. */
666 
667 	/* It seems that attribute list can be removed from primary record. */
668 	mi_remove_attr(NULL, &ni->mi, attr_list);
669 
670 	/*
671 	 * Repeat the cycle above and copy all attributes to primary record.
672 	 * Do not remove original attributes from subrecords!
673 	 * It should be success!
674 	 */
675 	le = NULL;
676 	while ((le = al_enumerate(ni, le))) {
677 		if (!memcmp(&le->ref, &ref, sizeof(ref)))
678 			continue;
679 
680 		mi = ni_find_mi(ni, ino_get(&le->ref));
681 		if (!mi) {
682 			/* Should never happened, 'cause already checked. */
683 			goto out;
684 		}
685 
686 		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
687 				    le->name_len, &le->id);
688 		if (!attr) {
689 			/* Should never happened, 'cause already checked. */
690 			goto out;
691 		}
692 		asize = le32_to_cpu(attr->size);
693 
694 		/* Insert into primary record. */
695 		attr_ins = mi_insert_attr(ni, &ni->mi, le->type, le_name(le),
696 					  le->name_len, asize,
697 					  le16_to_cpu(attr->name_off));
698 		if (!attr_ins) {
699 			/*
700 			 * No space in primary record (already checked).
701 			 */
702 			goto out;
703 		}
704 
705 		/* Copy all except id. */
706 		id = attr_ins->id;
707 		memcpy(attr_ins, attr, asize);
708 		attr_ins->id = id;
709 	}
710 
711 	/*
712 	 * Repeat the cycle above and remove all attributes from subrecords.
713 	 */
714 	le = NULL;
715 	while ((le = al_enumerate(ni, le))) {
716 		if (!memcmp(&le->ref, &ref, sizeof(ref)))
717 			continue;
718 
719 		mi = ni_find_mi(ni, ino_get(&le->ref));
720 		if (!mi)
721 			continue;
722 
723 		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
724 				    le->name_len, &le->id);
725 		if (!attr)
726 			continue;
727 
728 		/* Remove from original record. */
729 		mi_remove_attr(NULL, mi, attr);
730 	}
731 
732 	run_deallocate(sbi, &ni->attr_list.run, true);
733 	run_close(&ni->attr_list.run);
734 	ni->attr_list.size = 0;
735 	kvfree(ni->attr_list.le);
736 	ni->attr_list.le = NULL;
737 	ni->attr_list.dirty = false;
738 
739 	kfree(mrec);
740 	return 0;
741 out:
742 	/* Restore primary record. */
743 	swap(mrec, ni->mi.mrec);
744 	kfree(mrec);
745 	return 0;
746 }
747 
748 /*
749  * ni_create_attr_list - Generates an attribute list for this primary record.
750  */
751 int ni_create_attr_list(struct ntfs_inode *ni)
752 {
753 	struct ntfs_sb_info *sbi = ni->mi.sbi;
754 	int err;
755 	u32 lsize;
756 	struct ATTRIB *attr;
757 	struct ATTRIB *arr_move[7];
758 	struct ATTR_LIST_ENTRY *le, *le_b[7];
759 	struct MFT_REC *rec;
760 	bool is_mft;
761 	CLST rno = 0;
762 	struct mft_inode *mi;
763 	u32 free_b, nb, to_free, rs;
764 	u16 sz;
765 
766 	is_mft = ni->mi.rno == MFT_REC_MFT;
767 	rec = ni->mi.mrec;
768 	rs = sbi->record_size;
769 
770 	/*
771 	 * Skip estimating exact memory requirement.
772 	 * Looks like one record_size is always enough.
773 	 */
774 	le = kzalloc(al_aligned(rs), GFP_NOFS);
775 	if (!le)
776 		return -ENOMEM;
777 
778 	mi_get_ref(&ni->mi, &le->ref);
779 	ni->attr_list.le = le;
780 
781 	attr = NULL;
782 	nb = 0;
783 	free_b = 0;
784 	attr = NULL;
785 
786 	for (; (attr = mi_enum_attr(ni, &ni->mi, attr)); le = Add2Ptr(le, sz)) {
787 		sz = le_size(attr->name_len);
788 		le->type = attr->type;
789 		le->size = cpu_to_le16(sz);
790 		le->name_len = attr->name_len;
791 		le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
792 		le->vcn = 0;
793 		if (le != ni->attr_list.le)
794 			le->ref = ni->attr_list.le->ref;
795 		le->id = attr->id;
796 
797 		if (attr->name_len)
798 			memcpy(le->name, attr_name(attr),
799 			       sizeof(short) * attr->name_len);
800 		else if (attr->type == ATTR_STD)
801 			continue;
802 		else if (attr->type == ATTR_LIST)
803 			continue;
804 		else if (is_mft && attr->type == ATTR_DATA)
805 			continue;
806 
807 		if (!nb || nb < ARRAY_SIZE(arr_move)) {
808 			le_b[nb] = le;
809 			arr_move[nb++] = attr;
810 			free_b += le32_to_cpu(attr->size);
811 		}
812 	}
813 
814 	lsize = PtrOffset(ni->attr_list.le, le);
815 	ni->attr_list.size = lsize;
816 
817 	to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
818 	if (to_free <= rs) {
819 		to_free = 0;
820 	} else {
821 		to_free -= rs;
822 
823 		if (to_free > free_b) {
824 			err = -EINVAL;
825 			goto out;
826 		}
827 	}
828 
829 	/* Allocate child MFT. */
830 	err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
831 	if (err)
832 		goto out;
833 
834 	err = -EINVAL;
835 	/* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
836 	while (to_free > 0) {
837 		struct ATTRIB *b = arr_move[--nb];
838 		u32 asize = le32_to_cpu(b->size);
839 		u16 name_off = le16_to_cpu(b->name_off);
840 
841 		attr = mi_insert_attr(ni, mi, b->type, Add2Ptr(b, name_off),
842 				      b->name_len, asize, name_off);
843 		if (!attr)
844 			goto out;
845 
846 		mi_get_ref(mi, &le_b[nb]->ref);
847 		le_b[nb]->id = attr->id;
848 
849 		/* Copy all except id. */
850 		memcpy(attr, b, asize);
851 		attr->id = le_b[nb]->id;
852 
853 		/* Remove from primary record. */
854 		if (!mi_remove_attr(NULL, &ni->mi, b))
855 			goto out;
856 
857 		if (to_free <= asize)
858 			break;
859 		to_free -= asize;
860 		if (!nb)
861 			goto out;
862 	}
863 
864 	attr = mi_insert_attr(ni, &ni->mi, ATTR_LIST, NULL, 0,
865 			      lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
866 	if (!attr)
867 		goto out;
868 
869 	attr->non_res = 0;
870 	attr->flags = 0;
871 	attr->res.data_size = cpu_to_le32(lsize);
872 	attr->res.data_off = SIZEOF_RESIDENT_LE;
873 	attr->res.flags = 0;
874 	attr->res.res = 0;
875 
876 	memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
877 
878 	ni->attr_list.dirty = false;
879 
880 	mark_inode_dirty(&ni->vfs_inode);
881 	return 0;
882 
883 out:
884 	kvfree(ni->attr_list.le);
885 	ni->attr_list.le = NULL;
886 	ni->attr_list.size = 0;
887 	return err;
888 }
889 
890 /*
891  * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
892  */
893 static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
894 			   enum ATTR_TYPE type, const __le16 *name, u8 name_len,
895 			   u32 asize, CLST svcn, u16 name_off, bool force_ext,
896 			   struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
897 			   struct ATTR_LIST_ENTRY **ins_le)
898 {
899 	struct ATTRIB *attr;
900 	struct mft_inode *mi;
901 	CLST rno;
902 	u64 vbo;
903 	struct rb_node *node;
904 	int err;
905 	bool is_mft, is_mft_data;
906 	struct ntfs_sb_info *sbi = ni->mi.sbi;
907 
908 	is_mft = ni->mi.rno == MFT_REC_MFT;
909 	is_mft_data = is_mft && type == ATTR_DATA && !name_len;
910 
911 	if (asize > sbi->max_bytes_per_attr) {
912 		err = -EINVAL;
913 		goto out;
914 	}
915 
916 	/*
917 	 * Standard information and attr_list cannot be made external.
918 	 * The Log File cannot have any external attributes.
919 	 */
920 	if (type == ATTR_STD || type == ATTR_LIST ||
921 	    ni->mi.rno == MFT_REC_LOG) {
922 		err = -EINVAL;
923 		goto out;
924 	}
925 
926 	/* Create attribute list if it is not already existed. */
927 	if (!ni->attr_list.size) {
928 		err = ni_create_attr_list(ni);
929 		if (err)
930 			goto out;
931 	}
932 
933 	vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
934 
935 	if (force_ext)
936 		goto insert_ext;
937 
938 	/* Load all subrecords into memory. */
939 	err = ni_load_all_mi(ni);
940 	if (err)
941 		goto out;
942 
943 	/* Check each of loaded subrecord. */
944 	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
945 		mi = rb_entry(node, struct mft_inode, node);
946 
947 		if (is_mft_data &&
948 		    (mi_enum_attr(ni, mi, NULL) ||
949 		     vbo <= ((u64)mi->rno << sbi->record_bits))) {
950 			/* We can't accept this record 'cause MFT's bootstrapping. */
951 			continue;
952 		}
953 		if (is_mft &&
954 		    mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
955 			/*
956 			 * This child record already has a ATTR_DATA.
957 			 * So it can't accept any other records.
958 			 */
959 			continue;
960 		}
961 
962 		if ((type != ATTR_NAME || name_len) &&
963 		    mi_find_attr(ni, mi, NULL, type, name, name_len, NULL)) {
964 			/* Only indexed attributes can share same record. */
965 			continue;
966 		}
967 
968 		/*
969 		 * Do not try to insert this attribute
970 		 * if there is no room in record.
971 		 */
972 		if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
973 			continue;
974 
975 		/* Try to insert attribute into this subrecord. */
976 		attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
977 				       name_off, svcn, ins_le);
978 		if (!attr)
979 			continue;
980 		if (IS_ERR(attr))
981 			return PTR_ERR(attr);
982 
983 		if (ins_attr)
984 			*ins_attr = attr;
985 		if (ins_mi)
986 			*ins_mi = mi;
987 		return 0;
988 	}
989 
990 insert_ext:
991 	/* We have to allocate a new child subrecord. */
992 	err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
993 	if (err)
994 		goto out;
995 
996 	if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
997 		err = -EINVAL;
998 		goto out1;
999 	}
1000 
1001 	attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1002 			       name_off, svcn, ins_le);
1003 	if (!attr) {
1004 		err = -EINVAL;
1005 		goto out2;
1006 	}
1007 
1008 	if (IS_ERR(attr)) {
1009 		err = PTR_ERR(attr);
1010 		goto out2;
1011 	}
1012 
1013 	if (ins_attr)
1014 		*ins_attr = attr;
1015 	if (ins_mi)
1016 		*ins_mi = mi;
1017 
1018 	return 0;
1019 
1020 out2:
1021 	ni_remove_mi(ni, mi);
1022 
1023 out1:
1024 	mi_put(mi);
1025 	ntfs_mark_rec_free(sbi, rno, is_mft);
1026 
1027 out:
1028 	return err;
1029 }
1030 
1031 /*
1032  * ni_insert_attr - Insert an attribute into the file.
1033  *
1034  * If the primary record has room, it will just insert the attribute.
1035  * If not, it may make the attribute external.
1036  * For $MFT::Data it may make room for the attribute by
1037  * making other attributes external.
1038  *
1039  * NOTE:
1040  * The ATTR_LIST and ATTR_STD cannot be made external.
1041  * This function does not fill new attribute full.
1042  * It only fills 'size'/'type'/'id'/'name_len' fields.
1043  */
1044 static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1045 			  const __le16 *name, u8 name_len, u32 asize,
1046 			  u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
1047 			  struct mft_inode **ins_mi,
1048 			  struct ATTR_LIST_ENTRY **ins_le)
1049 {
1050 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1051 	int err;
1052 	struct ATTRIB *attr, *eattr;
1053 	struct MFT_REC *rec;
1054 	bool is_mft;
1055 	struct ATTR_LIST_ENTRY *le;
1056 	u32 list_reserve, max_free, free, used, t32;
1057 	__le16 id;
1058 	u16 t16;
1059 
1060 	is_mft = ni->mi.rno == MFT_REC_MFT;
1061 	rec = ni->mi.mrec;
1062 
1063 	list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1064 	used = le32_to_cpu(rec->used);
1065 	free = sbi->record_size - used;
1066 
1067 	if (is_mft && type != ATTR_LIST) {
1068 		/* Reserve space for the ATTRIB list. */
1069 		if (free < list_reserve)
1070 			free = 0;
1071 		else
1072 			free -= list_reserve;
1073 	}
1074 
1075 	if (asize <= free) {
1076 		attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
1077 				       asize, name_off, svcn, ins_le);
1078 		if (IS_ERR(attr)) {
1079 			err = PTR_ERR(attr);
1080 			goto out;
1081 		}
1082 
1083 		if (attr) {
1084 			if (ins_attr)
1085 				*ins_attr = attr;
1086 			if (ins_mi)
1087 				*ins_mi = &ni->mi;
1088 			err = 0;
1089 			goto out;
1090 		}
1091 	}
1092 
1093 	if (!is_mft || type != ATTR_DATA || svcn) {
1094 		/* This ATTRIB will be external. */
1095 		err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
1096 				      svcn, name_off, false, ins_attr, ins_mi,
1097 				      ins_le);
1098 		goto out;
1099 	}
1100 
1101 	/*
1102 	 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
1103 	 *
1104 	 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1105 	 * Evict as many other attributes as possible.
1106 	 */
1107 	max_free = free;
1108 
1109 	/* Estimate the result of moving all possible attributes away. */
1110 	attr = NULL;
1111 
1112 	while ((attr = mi_enum_attr(ni, &ni->mi, attr))) {
1113 		if (attr->type == ATTR_STD)
1114 			continue;
1115 		if (attr->type == ATTR_LIST)
1116 			continue;
1117 		max_free += le32_to_cpu(attr->size);
1118 	}
1119 
1120 	if (max_free < asize + list_reserve) {
1121 		/* Impossible to insert this attribute into primary record. */
1122 		err = -EINVAL;
1123 		goto out;
1124 	}
1125 
1126 	/* Start real attribute moving. */
1127 	attr = NULL;
1128 
1129 	for (;;) {
1130 		attr = mi_enum_attr(ni, &ni->mi, attr);
1131 		if (!attr) {
1132 			/* We should never be here 'cause we have already check this case. */
1133 			err = -EINVAL;
1134 			goto out;
1135 		}
1136 
1137 		/* Skip attributes that MUST be primary record. */
1138 		if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1139 			continue;
1140 
1141 		le = NULL;
1142 		if (ni->attr_list.size) {
1143 			le = al_find_le(ni, NULL, attr);
1144 			if (!le) {
1145 				/* Really this is a serious bug. */
1146 				err = -EINVAL;
1147 				goto out;
1148 			}
1149 		}
1150 
1151 		t32 = le32_to_cpu(attr->size);
1152 		t16 = le16_to_cpu(attr->name_off);
1153 		err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1154 				      attr->name_len, t32, attr_svcn(attr), t16,
1155 				      false, &eattr, NULL, NULL);
1156 		if (err)
1157 			return err;
1158 
1159 		id = eattr->id;
1160 		memcpy(eattr, attr, t32);
1161 		eattr->id = id;
1162 
1163 		/* Remove from primary record. */
1164 		mi_remove_attr(NULL, &ni->mi, attr);
1165 
1166 		/* attr now points to next attribute. */
1167 		if (attr->type == ATTR_END)
1168 			goto out;
1169 	}
1170 	while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1171 		;
1172 
1173 	attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
1174 			       name_off, svcn, ins_le);
1175 	if (!attr) {
1176 		err = -EINVAL;
1177 		goto out;
1178 	}
1179 
1180 	if (IS_ERR(attr)) {
1181 		err = PTR_ERR(attr);
1182 		goto out;
1183 	}
1184 
1185 	if (ins_attr)
1186 		*ins_attr = attr;
1187 	if (ins_mi)
1188 		*ins_mi = &ni->mi;
1189 
1190 out:
1191 	return err;
1192 }
1193 
1194 /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
1195 static int ni_expand_mft_list(struct ntfs_inode *ni)
1196 {
1197 	int err = 0;
1198 	struct runs_tree *run = &ni->file.run;
1199 	u32 asize, run_size, done = 0;
1200 	struct ATTRIB *attr;
1201 	struct rb_node *node;
1202 	CLST mft_min, mft_new, svcn, evcn, plen;
1203 	struct mft_inode *mi, *mi_min, *mi_new;
1204 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1205 
1206 	/* Find the nearest MFT. */
1207 	mft_min = 0;
1208 	mft_new = 0;
1209 	mi_min = NULL;
1210 
1211 	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1212 		mi = rb_entry(node, struct mft_inode, node);
1213 
1214 		attr = mi_enum_attr(ni, mi, NULL);
1215 
1216 		if (!attr) {
1217 			mft_min = mi->rno;
1218 			mi_min = mi;
1219 			break;
1220 		}
1221 	}
1222 
1223 	if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1224 		mft_new = 0;
1225 		/* Really this is not critical. */
1226 	} else if (mft_min > mft_new) {
1227 		mft_min = mft_new;
1228 		mi_min = mi_new;
1229 	} else {
1230 		ntfs_mark_rec_free(sbi, mft_new, true);
1231 		mft_new = 0;
1232 		ni_remove_mi(ni, mi_new);
1233 	}
1234 
1235 	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1236 	if (!attr) {
1237 		err = -EINVAL;
1238 		goto out;
1239 	}
1240 
1241 	asize = le32_to_cpu(attr->size);
1242 
1243 	evcn = le64_to_cpu(attr->nres.evcn);
1244 	svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1245 	if (evcn + 1 >= svcn) {
1246 		err = -EINVAL;
1247 		goto out;
1248 	}
1249 
1250 	/*
1251 	 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
1252 	 *
1253 	 * Update first part of ATTR_DATA in 'primary MFT.
1254 	 */
1255 	err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1256 		       asize - SIZEOF_NONRESIDENT, &plen);
1257 	if (err < 0)
1258 		goto out;
1259 
1260 	run_size = ALIGN(err, 8);
1261 	err = 0;
1262 
1263 	if (plen < svcn) {
1264 		err = -EINVAL;
1265 		goto out;
1266 	}
1267 
1268 	attr->nres.evcn = cpu_to_le64(svcn - 1);
1269 	attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
1270 	/* 'done' - How many bytes of primary MFT becomes free. */
1271 	done = asize - run_size - SIZEOF_NONRESIDENT;
1272 	le32_sub_cpu(&ni->mi.mrec->used, done);
1273 
1274 	/* Estimate packed size (run_buf=NULL). */
1275 	err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1276 		       &plen);
1277 	if (err < 0)
1278 		goto out;
1279 
1280 	run_size = ALIGN(err, 8);
1281 	err = 0;
1282 
1283 	if (plen < evcn + 1 - svcn) {
1284 		err = -EINVAL;
1285 		goto out;
1286 	}
1287 
1288 	/*
1289 	 * This function may implicitly call expand attr_list.
1290 	 * Insert second part of ATTR_DATA in 'mi_min'.
1291 	 */
1292 	attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1293 			       SIZEOF_NONRESIDENT + run_size,
1294 			       SIZEOF_NONRESIDENT, svcn, NULL);
1295 	if (!attr) {
1296 		err = -EINVAL;
1297 		goto out;
1298 	}
1299 
1300 	if (IS_ERR(attr)) {
1301 		err = PTR_ERR(attr);
1302 		goto out;
1303 	}
1304 
1305 	attr->non_res = 1;
1306 	attr->name_off = SIZEOF_NONRESIDENT_LE;
1307 	attr->flags = 0;
1308 
1309 	/* This function can't fail - cause already checked above. */
1310 	run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1311 		 run_size, &plen);
1312 
1313 	attr->nres.svcn = cpu_to_le64(svcn);
1314 	attr->nres.evcn = cpu_to_le64(evcn);
1315 	attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1316 
1317 out:
1318 	if (mft_new) {
1319 		ntfs_mark_rec_free(sbi, mft_new, true);
1320 		ni_remove_mi(ni, mi_new);
1321 	}
1322 
1323 	return !err && !done ? -EOPNOTSUPP : err;
1324 }
1325 
1326 /*
1327  * ni_expand_list - Move all possible attributes out of primary record.
1328  */
1329 int ni_expand_list(struct ntfs_inode *ni)
1330 {
1331 	int err = 0;
1332 	u32 asize, done = 0;
1333 	struct ATTRIB *attr, *ins_attr;
1334 	struct ATTR_LIST_ENTRY *le;
1335 	bool is_mft = ni->mi.rno == MFT_REC_MFT;
1336 	struct MFT_REF ref;
1337 
1338 	mi_get_ref(&ni->mi, &ref);
1339 	le = NULL;
1340 
1341 	while ((le = al_enumerate(ni, le))) {
1342 		if (le->type == ATTR_STD)
1343 			continue;
1344 
1345 		if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1346 			continue;
1347 
1348 		if (is_mft && le->type == ATTR_DATA)
1349 			continue;
1350 
1351 		/* Find attribute in primary record. */
1352 		attr = rec_find_attr_le(ni, &ni->mi, le);
1353 		if (!attr) {
1354 			err = -EINVAL;
1355 			goto out;
1356 		}
1357 
1358 		asize = le32_to_cpu(attr->size);
1359 
1360 		/* Always insert into new record to avoid collisions (deep recursive). */
1361 		err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1362 				      attr->name_len, asize, attr_svcn(attr),
1363 				      le16_to_cpu(attr->name_off), true,
1364 				      &ins_attr, NULL, NULL);
1365 
1366 		if (err)
1367 			goto out;
1368 
1369 		memcpy(ins_attr, attr, asize);
1370 		ins_attr->id = le->id;
1371 		/* Remove from primary record. */
1372 		mi_remove_attr(NULL, &ni->mi, attr);
1373 
1374 		done += asize;
1375 		goto out;
1376 	}
1377 
1378 	if (!is_mft) {
1379 		err = -EFBIG; /* Attr list is too big(?) */
1380 		goto out;
1381 	}
1382 
1383 	/* Split MFT data as much as possible. */
1384 	err = ni_expand_mft_list(ni);
1385 
1386 out:
1387 	return !err && !done ? -EOPNOTSUPP : err;
1388 }
1389 
1390 /*
1391  * ni_insert_nonresident - Insert new nonresident attribute.
1392  */
1393 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1394 			  const __le16 *name, u8 name_len,
1395 			  const struct runs_tree *run, CLST svcn, CLST len,
1396 			  __le16 flags, struct ATTRIB **new_attr,
1397 			  struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
1398 {
1399 	int err;
1400 	CLST plen;
1401 	struct ATTRIB *attr;
1402 	bool is_ext = (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) &&
1403 		      !svcn;
1404 	u32 name_size = ALIGN(name_len * sizeof(short), 8);
1405 	u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1406 	u32 run_off = name_off + name_size;
1407 	u32 run_size, asize;
1408 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1409 
1410 	/* Estimate packed size (run_buf=NULL). */
1411 	err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1412 		       &plen);
1413 	if (err < 0)
1414 		goto out;
1415 
1416 	run_size = ALIGN(err, 8);
1417 
1418 	if (plen < len) {
1419 		err = -EINVAL;
1420 		goto out;
1421 	}
1422 
1423 	asize = run_off + run_size;
1424 
1425 	if (asize > sbi->max_bytes_per_attr) {
1426 		err = -EINVAL;
1427 		goto out;
1428 	}
1429 
1430 	err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
1431 			     &attr, mi, le);
1432 
1433 	if (err)
1434 		goto out;
1435 
1436 	attr->non_res = 1;
1437 	attr->name_off = cpu_to_le16(name_off);
1438 	attr->flags = flags;
1439 
1440 	/* This function can't fail - cause already checked above. */
1441 	run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1442 
1443 	attr->nres.svcn = cpu_to_le64(svcn);
1444 	attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1445 
1446 	if (new_attr)
1447 		*new_attr = attr;
1448 
1449 	*(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1450 
1451 	attr->nres.alloc_size =
1452 		svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1453 	attr->nres.data_size = attr->nres.alloc_size;
1454 	attr->nres.valid_size = attr->nres.alloc_size;
1455 
1456 	if (is_ext) {
1457 		if (flags & ATTR_FLAG_COMPRESSED)
1458 			attr->nres.c_unit = NTFS_LZNT_CUNIT;
1459 		attr->nres.total_size = attr->nres.alloc_size;
1460 	}
1461 
1462 out:
1463 	return err;
1464 }
1465 
1466 /*
1467  * ni_insert_resident - Inserts new resident attribute.
1468  */
1469 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1470 		       enum ATTR_TYPE type, const __le16 *name, u8 name_len,
1471 		       struct ATTRIB **new_attr, struct mft_inode **mi,
1472 		       struct ATTR_LIST_ENTRY **le)
1473 {
1474 	int err;
1475 	u32 name_size = ALIGN(name_len * sizeof(short), 8);
1476 	u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
1477 	struct ATTRIB *attr;
1478 
1479 	err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
1480 			     0, &attr, mi, le);
1481 	if (err)
1482 		return err;
1483 
1484 	attr->non_res = 0;
1485 	attr->flags = 0;
1486 
1487 	attr->res.data_size = cpu_to_le32(data_size);
1488 	attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
1489 	if (type == ATTR_NAME) {
1490 		attr->res.flags = RESIDENT_FLAG_INDEXED;
1491 
1492 		/* is_attr_indexed(attr)) == true */
1493 		le16_add_cpu(&ni->mi.mrec->hard_links, 1);
1494 		ni->mi.dirty = true;
1495 	}
1496 	attr->res.res = 0;
1497 
1498 	if (new_attr)
1499 		*new_attr = attr;
1500 
1501 	return 0;
1502 }
1503 
1504 /*
1505  * ni_remove_attr_le - Remove attribute from record.
1506  */
1507 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1508 		       struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
1509 {
1510 	mi_remove_attr(ni, mi, attr);
1511 
1512 	if (le)
1513 		al_remove_le(ni, le);
1514 }
1515 
1516 /*
1517  * ni_delete_all - Remove all attributes and frees allocates space.
1518  *
1519  * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
1520  */
1521 int ni_delete_all(struct ntfs_inode *ni)
1522 {
1523 	int err;
1524 	struct ATTR_LIST_ENTRY *le = NULL;
1525 	struct ATTRIB *attr = NULL;
1526 	struct rb_node *node;
1527 	u16 roff;
1528 	u32 asize;
1529 	CLST svcn, evcn;
1530 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1531 	bool nt3 = is_ntfs3(sbi);
1532 	struct MFT_REF ref;
1533 
1534 	while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1535 		if (!nt3 || attr->name_len) {
1536 			;
1537 		} else if (attr->type == ATTR_REPARSE) {
1538 			mi_get_ref(&ni->mi, &ref);
1539 			ntfs_remove_reparse(sbi, 0, &ref);
1540 		} else if (attr->type == ATTR_ID && !attr->non_res &&
1541 			   le32_to_cpu(attr->res.data_size) >=
1542 				   sizeof(struct GUID)) {
1543 			ntfs_objid_remove(sbi, resident_data(attr));
1544 		}
1545 
1546 		if (!attr->non_res)
1547 			continue;
1548 
1549 		svcn = le64_to_cpu(attr->nres.svcn);
1550 		evcn = le64_to_cpu(attr->nres.evcn);
1551 
1552 		if (evcn + 1 <= svcn)
1553 			continue;
1554 
1555 		asize = le32_to_cpu(attr->size);
1556 		roff = le16_to_cpu(attr->nres.run_off);
1557 
1558 		if (roff > asize) {
1559 			/* ni_enum_attr_ex checks this case. */
1560 			continue;
1561 		}
1562 
1563 		/* run==1 means unpack and deallocate. */
1564 		run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1565 			      Add2Ptr(attr, roff), asize - roff);
1566 	}
1567 
1568 	if (ni->attr_list.size) {
1569 		run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1570 		al_destroy(ni);
1571 	}
1572 
1573 	/* Free all subrecords. */
1574 	for (node = rb_first(&ni->mi_tree); node;) {
1575 		struct rb_node *next = rb_next(node);
1576 		struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1577 
1578 		clear_rec_inuse(mi->mrec);
1579 		mi->dirty = true;
1580 		mi_write(mi, 0);
1581 
1582 		ntfs_mark_rec_free(sbi, mi->rno, false);
1583 		ni_remove_mi(ni, mi);
1584 		mi_put(mi);
1585 		node = next;
1586 	}
1587 
1588 	/* Free base record. */
1589 	clear_rec_inuse(ni->mi.mrec);
1590 	ni->mi.dirty = true;
1591 	err = mi_write(&ni->mi, 0);
1592 
1593 	ntfs_mark_rec_free(sbi, ni->mi.rno, false);
1594 
1595 	return err;
1596 }
1597 
1598 /* ni_fname_name
1599  *
1600  * Return: File name attribute by its value.
1601  */
1602 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1603 				     const struct le_str *uni,
1604 				     const struct MFT_REF *home_dir,
1605 				     struct mft_inode **mi,
1606 				     struct ATTR_LIST_ENTRY **le)
1607 {
1608 	struct ATTRIB *attr = NULL;
1609 	struct ATTR_FILE_NAME *fname;
1610 
1611 	if (le)
1612 		*le = NULL;
1613 
1614 	/* Enumerate all names. */
1615 next:
1616 	attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1617 	if (!attr)
1618 		return NULL;
1619 
1620 	fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1621 	if (!fname)
1622 		goto next;
1623 
1624 	if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1625 		goto next;
1626 
1627 	if (!uni)
1628 		return fname;
1629 
1630 	if (uni->len != fname->name_len)
1631 		goto next;
1632 
1633 	if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL,
1634 			   false))
1635 		goto next;
1636 	return fname;
1637 }
1638 
1639 /*
1640  * ni_fname_type
1641  *
1642  * Return: File name attribute with given type.
1643  */
1644 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
1645 				     struct mft_inode **mi,
1646 				     struct ATTR_LIST_ENTRY **le)
1647 {
1648 	struct ATTRIB *attr = NULL;
1649 	struct ATTR_FILE_NAME *fname;
1650 
1651 	*le = NULL;
1652 
1653 	if (name_type == FILE_NAME_POSIX)
1654 		return NULL;
1655 
1656 	/* Enumerate all names. */
1657 	for (;;) {
1658 		attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1659 		if (!attr)
1660 			return NULL;
1661 
1662 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1663 		if (fname && name_type == fname->type)
1664 			return fname;
1665 	}
1666 }
1667 
1668 /*
1669  * ni_new_attr_flags
1670  *
1671  * Process compressed/sparsed in special way.
1672  * NOTE: You need to set ni->std_fa = new_fa
1673  * after this function to keep internal structures in consistency.
1674  */
1675 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1676 {
1677 	struct ATTRIB *attr;
1678 	struct mft_inode *mi;
1679 	__le16 new_aflags;
1680 	u32 new_asize;
1681 
1682 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1683 	if (!attr)
1684 		return -EINVAL;
1685 
1686 	new_aflags = attr->flags;
1687 
1688 	if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1689 		new_aflags |= ATTR_FLAG_SPARSED;
1690 	else
1691 		new_aflags &= ~ATTR_FLAG_SPARSED;
1692 
1693 	if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1694 		new_aflags |= ATTR_FLAG_COMPRESSED;
1695 	else
1696 		new_aflags &= ~ATTR_FLAG_COMPRESSED;
1697 
1698 	if (new_aflags == attr->flags)
1699 		return 0;
1700 
1701 	if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1702 	    (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1703 		ntfs_inode_warn(&ni->vfs_inode,
1704 				"file can't be sparsed and compressed");
1705 		return -EOPNOTSUPP;
1706 	}
1707 
1708 	if (!attr->non_res)
1709 		goto out;
1710 
1711 	if (attr->nres.data_size) {
1712 		ntfs_inode_warn(
1713 			&ni->vfs_inode,
1714 			"one can change sparsed/compressed only for empty files");
1715 		return -EOPNOTSUPP;
1716 	}
1717 
1718 	/* Resize nonresident empty attribute in-place only. */
1719 	new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
1720 			    (SIZEOF_NONRESIDENT_EX + 8) :
1721 			    (SIZEOF_NONRESIDENT + 8);
1722 
1723 	if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1724 		return -EOPNOTSUPP;
1725 
1726 	if (new_aflags & ATTR_FLAG_SPARSED) {
1727 		attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1728 		/* Windows uses 16 clusters per frame but supports one cluster per frame too. */
1729 		attr->nres.c_unit = 0;
1730 		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1731 	} else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1732 		attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1733 		/* The only allowed: 16 clusters per frame. */
1734 		attr->nres.c_unit = NTFS_LZNT_CUNIT;
1735 		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1736 	} else {
1737 		attr->name_off = SIZEOF_NONRESIDENT_LE;
1738 		/* Normal files. */
1739 		attr->nres.c_unit = 0;
1740 		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1741 	}
1742 	attr->nres.run_off = attr->name_off;
1743 out:
1744 	attr->flags = new_aflags;
1745 	mi->dirty = true;
1746 
1747 	return 0;
1748 }
1749 
1750 /*
1751  * ni_parse_reparse
1752  *
1753  * buffer - memory for reparse buffer header
1754  */
1755 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
1756 				   struct REPARSE_DATA_BUFFER *buffer)
1757 {
1758 	const struct REPARSE_DATA_BUFFER *rp = NULL;
1759 	u8 bits;
1760 	u16 len;
1761 	typeof(rp->CompressReparseBuffer) *cmpr;
1762 
1763 	/* Try to estimate reparse point. */
1764 	if (!attr->non_res) {
1765 		rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1766 	} else if (le64_to_cpu(attr->nres.data_size) >=
1767 		   sizeof(struct REPARSE_DATA_BUFFER)) {
1768 		struct runs_tree run;
1769 
1770 		run_init(&run);
1771 
1772 		if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1773 		    !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1774 				      sizeof(struct REPARSE_DATA_BUFFER),
1775 				      NULL)) {
1776 			rp = buffer;
1777 		}
1778 
1779 		run_close(&run);
1780 	}
1781 
1782 	if (!rp)
1783 		return REPARSE_NONE;
1784 
1785 	len = le16_to_cpu(rp->ReparseDataLength);
1786 	switch (rp->ReparseTag) {
1787 	case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
1788 		break; /* Symbolic link. */
1789 	case IO_REPARSE_TAG_MOUNT_POINT:
1790 		break; /* Mount points and junctions. */
1791 	case IO_REPARSE_TAG_SYMLINK:
1792 		break;
1793 	case IO_REPARSE_TAG_COMPRESS:
1794 		/*
1795 		 * WOF - Windows Overlay Filter - Used to compress files with
1796 		 * LZX/Xpress.
1797 		 *
1798 		 * Unlike native NTFS file compression, the Windows
1799 		 * Overlay Filter supports only read operations. This means
1800 		 * that it doesn't need to sector-align each compressed chunk,
1801 		 * so the compressed data can be packed more tightly together.
1802 		 * If you open the file for writing, the WOF just decompresses
1803 		 * the entire file, turning it back into a plain file.
1804 		 *
1805 		 * Ntfs3 driver decompresses the entire file only on write or
1806 		 * change size requests.
1807 		 */
1808 
1809 		cmpr = &rp->CompressReparseBuffer;
1810 		if (len < sizeof(*cmpr) ||
1811 		    cmpr->WofVersion != WOF_CURRENT_VERSION ||
1812 		    cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1813 		    cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1814 			return REPARSE_NONE;
1815 		}
1816 
1817 		switch (cmpr->CompressionFormat) {
1818 		case WOF_COMPRESSION_XPRESS4K:
1819 			bits = 0xc; // 4k
1820 			break;
1821 		case WOF_COMPRESSION_XPRESS8K:
1822 			bits = 0xd; // 8k
1823 			break;
1824 		case WOF_COMPRESSION_XPRESS16K:
1825 			bits = 0xe; // 16k
1826 			break;
1827 		case WOF_COMPRESSION_LZX32K:
1828 			bits = 0xf; // 32k
1829 			break;
1830 		default:
1831 			bits = 0x10; // 64k
1832 			break;
1833 		}
1834 		ni_set_ext_compress_bits(ni, bits);
1835 		return REPARSE_COMPRESSED;
1836 
1837 	case IO_REPARSE_TAG_DEDUP:
1838 		ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1839 		return REPARSE_DEDUPLICATED;
1840 
1841 	default:
1842 		if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1843 			break;
1844 
1845 		return REPARSE_NONE;
1846 	}
1847 
1848 	if (buffer != rp)
1849 		memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
1850 
1851 	/* Looks like normal symlink. */
1852 	return REPARSE_LINK;
1853 }
1854 
1855 static struct page *ntfs_lock_new_page(struct address_space *mapping,
1856 				       pgoff_t index, gfp_t gfp)
1857 {
1858 	struct folio *folio = __filemap_get_folio(
1859 		mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
1860 	struct page *page;
1861 
1862 	if (IS_ERR(folio))
1863 		return ERR_CAST(folio);
1864 
1865 	if (!folio_test_uptodate(folio))
1866 		return folio_file_page(folio, index);
1867 
1868 	/* Use a temporary page to avoid data corruption */
1869 	folio_unlock(folio);
1870 	folio_put(folio);
1871 	page = alloc_page(gfp);
1872 	if (!page)
1873 		return ERR_PTR(-ENOMEM);
1874 	__SetPageLocked(page);
1875 	return page;
1876 }
1877 
1878 /*
1879  * ni_read_folio_cmpr
1880  *
1881  * When decompressing, we typically obtain more than one page per reference.
1882  * We inject the additional pages into the page cache.
1883  */
1884 int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio)
1885 {
1886 	int err;
1887 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1888 	struct address_space *mapping = folio->mapping;
1889 	pgoff_t index;
1890 	u64 frame_vbo, vbo = folio_pos(folio);
1891 	struct page **pages = NULL; /* Array of at most 16 pages. stack? */
1892 	u8 frame_bits;
1893 	CLST frame;
1894 	u32 i, idx, frame_size, pages_per_frame;
1895 	gfp_t gfp_mask;
1896 	struct page *pg;
1897 
1898 	if (vbo >= i_size_read(&ni->vfs_inode)) {
1899 		folio_zero_range(folio, 0, folio_size(folio));
1900 		folio_mark_uptodate(folio);
1901 		err = 0;
1902 		goto out;
1903 	}
1904 
1905 	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
1906 		/* Xpress or LZX. */
1907 		frame_bits = ni_ext_compress_bits(ni);
1908 	} else {
1909 		/* LZNT compression. */
1910 		frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
1911 	}
1912 	frame_size = 1u << frame_bits;
1913 	frame = vbo >> frame_bits;
1914 	frame_vbo = (u64)frame << frame_bits;
1915 	idx = (vbo - frame_vbo) >> PAGE_SHIFT;
1916 
1917 	pages_per_frame = frame_size >> PAGE_SHIFT;
1918 	pages = kzalloc_objs(struct page *, pages_per_frame, GFP_NOFS);
1919 	if (!pages) {
1920 		err = -ENOMEM;
1921 		goto out;
1922 	}
1923 
1924 	pages[idx] = &folio->page;
1925 	index = frame_vbo >> PAGE_SHIFT;
1926 	gfp_mask = mapping_gfp_mask(mapping);
1927 
1928 	for (i = 0; i < pages_per_frame; i++, index++) {
1929 		if (i == idx)
1930 			continue;
1931 
1932 		pg = ntfs_lock_new_page(mapping, index, gfp_mask);
1933 		if (IS_ERR(pg)) {
1934 			err = PTR_ERR(pg);
1935 			goto out1;
1936 		}
1937 		pages[i] = pg;
1938 	}
1939 
1940 	ni_lock(ni);
1941 	err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0);
1942 	ni_unlock(ni);
1943 
1944 out1:
1945 	for (i = 0; i < pages_per_frame; i++) {
1946 		pg = pages[i];
1947 		if (i == idx || !pg)
1948 			continue;
1949 		unlock_page(pg);
1950 		put_page(pg);
1951 	}
1952 
1953 out:
1954 	/* At this point, err contains 0 or -EIO depending on the "critical" page. */
1955 	kfree(pages);
1956 	folio_unlock(folio);
1957 
1958 	return err;
1959 }
1960 
1961 #ifdef CONFIG_NTFS3_LZX_XPRESS
1962 /*
1963  * ni_decompress_file - Decompress LZX/Xpress compressed file.
1964  *
1965  * Remove ATTR_DATA::WofCompressedData.
1966  * Remove ATTR_REPARSE.
1967  */
1968 int ni_decompress_file(struct ntfs_inode *ni)
1969 {
1970 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1971 	struct inode *inode = &ni->vfs_inode;
1972 	loff_t i_size = i_size_read(inode);
1973 	struct address_space *mapping = inode->i_mapping;
1974 	gfp_t gfp_mask = mapping_gfp_mask(mapping);
1975 	struct page **pages = NULL;
1976 	struct ATTR_LIST_ENTRY *le;
1977 	struct ATTRIB *attr;
1978 	CLST vcn, cend, lcn, clen, end;
1979 	pgoff_t index;
1980 	u64 vbo;
1981 	u8 frame_bits;
1982 	u32 i, frame_size, pages_per_frame, bytes;
1983 	struct mft_inode *mi;
1984 	int err;
1985 
1986 	/* Clusters for decompressed data. */
1987 	cend = bytes_to_cluster(sbi, i_size);
1988 
1989 	if (!i_size)
1990 		goto remove_wof;
1991 
1992 	/* Check in advance. */
1993 	if (cend > wnd_zeroes(&sbi->used.bitmap)) {
1994 		err = -ENOSPC;
1995 		goto out;
1996 	}
1997 
1998 	frame_bits = ni_ext_compress_bits(ni);
1999 	frame_size = 1u << frame_bits;
2000 	pages_per_frame = frame_size >> PAGE_SHIFT;
2001 	pages = kzalloc_objs(struct page *, pages_per_frame, GFP_NOFS);
2002 	if (!pages) {
2003 		err = -ENOMEM;
2004 		goto out;
2005 	}
2006 
2007 	/*
2008 	 * Step 1: Decompress data and copy to new allocated clusters.
2009 	 */
2010 	index = 0;
2011 	for (vbo = 0; vbo < i_size; vbo += bytes) {
2012 		bool new;
2013 
2014 		bytes = vbo + frame_size > i_size ? (i_size - vbo) : frame_size;
2015 		end = bytes_to_cluster(sbi, vbo + bytes);
2016 
2017 		for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2018 			err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2019 						  &clen, &new, false, NULL,
2020 						  false);
2021 			if (err)
2022 				goto out;
2023 		}
2024 
2025 		for (i = 0; i < pages_per_frame; i++, index++) {
2026 			struct page *pg;
2027 
2028 			pg = ntfs_lock_new_page(mapping, index, gfp_mask);
2029 			if (IS_ERR(pg)) {
2030 				while (i--) {
2031 					unlock_page(pages[i]);
2032 					put_page(pages[i]);
2033 				}
2034 				err = PTR_ERR(pg);
2035 				goto out;
2036 			}
2037 			pages[i] = pg;
2038 		}
2039 
2040 		err = ni_read_frame(ni, vbo, pages, pages_per_frame, 1);
2041 
2042 		for (i = 0; i < pages_per_frame; i++) {
2043 			unlock_page(pages[i]);
2044 			put_page(pages[i]);
2045 		}
2046 
2047 		if (err)
2048 			goto out;
2049 
2050 		cond_resched();
2051 	}
2052 
2053 remove_wof:
2054 	/*
2055 	 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2056 	 * and ATTR_REPARSE.
2057 	 */
2058 	attr = NULL;
2059 	le = NULL;
2060 	while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2061 		CLST svcn, evcn;
2062 		u32 asize, roff;
2063 
2064 		if (attr->type == ATTR_REPARSE) {
2065 			struct MFT_REF ref;
2066 
2067 			mi_get_ref(&ni->mi, &ref);
2068 			ntfs_remove_reparse(sbi, 0, &ref);
2069 		}
2070 
2071 		if (!attr->non_res)
2072 			continue;
2073 
2074 		if (attr->type != ATTR_REPARSE &&
2075 		    (attr->type != ATTR_DATA ||
2076 		     attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2077 		     memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2078 			continue;
2079 
2080 		svcn = le64_to_cpu(attr->nres.svcn);
2081 		evcn = le64_to_cpu(attr->nres.evcn);
2082 
2083 		if (evcn + 1 <= svcn)
2084 			continue;
2085 
2086 		asize = le32_to_cpu(attr->size);
2087 		roff = le16_to_cpu(attr->nres.run_off);
2088 
2089 		if (roff > asize) {
2090 			err = -EINVAL;
2091 			goto out;
2092 		}
2093 
2094 		/*run==1  Means unpack and deallocate. */
2095 		run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2096 			      Add2Ptr(attr, roff), asize - roff);
2097 	}
2098 
2099 	/*
2100 	 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
2101 	 */
2102 	err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2103 			     false, NULL);
2104 	if (err)
2105 		goto out;
2106 
2107 	/*
2108 	 * Step 4: Remove ATTR_REPARSE.
2109 	 */
2110 	err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2111 	if (err)
2112 		goto out;
2113 
2114 	/*
2115 	 * Step 5: Remove sparse flag from data attribute.
2116 	 */
2117 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2118 	if (!attr) {
2119 		err = -EINVAL;
2120 		goto out;
2121 	}
2122 
2123 	if (attr->non_res && is_attr_sparsed(attr)) {
2124 		/* Sparsed attribute header is 8 bytes bigger than normal. */
2125 		struct MFT_REC *rec = mi->mrec;
2126 		u32 used = le32_to_cpu(rec->used);
2127 		u32 asize = le32_to_cpu(attr->size);
2128 		u16 roff = le16_to_cpu(attr->nres.run_off);
2129 		char *rbuf = Add2Ptr(attr, roff);
2130 
2131 		memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2132 		attr->size = cpu_to_le32(asize - 8);
2133 		attr->flags &= ~ATTR_FLAG_SPARSED;
2134 		attr->nres.run_off = cpu_to_le16(roff - 8);
2135 		attr->nres.c_unit = 0;
2136 		rec->used = cpu_to_le32(used - 8);
2137 		mi->dirty = true;
2138 		ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2139 				FILE_ATTRIBUTE_REPARSE_POINT);
2140 
2141 		mark_inode_dirty(inode);
2142 	}
2143 
2144 	/* Clear cached flag. */
2145 	ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2146 	if (ni->file.offs_folio) {
2147 		folio_put(ni->file.offs_folio);
2148 		ni->file.offs_folio = NULL;
2149 	}
2150 	mapping->a_ops = &ntfs_aops;
2151 
2152 out:
2153 	kfree(pages);
2154 	if (err)
2155 		_ntfs_bad_inode(inode);
2156 
2157 	return err;
2158 }
2159 
2160 /*
2161  * decompress_lzx_xpress - External compression LZX/Xpress.
2162  */
2163 static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2164 				 size_t cmpr_size, void *unc, size_t unc_size,
2165 				 u32 frame_size)
2166 {
2167 	int err;
2168 	void *ctx;
2169 
2170 	if (cmpr_size == unc_size) {
2171 		/* Frame not compressed. */
2172 		memcpy(unc, cmpr, unc_size);
2173 		return 0;
2174 	}
2175 
2176 	err = 0;
2177 	if (frame_size == 0x8000) {
2178 		mutex_lock(&sbi->compress.mtx_lzx);
2179 		/* LZX: Frame compressed. */
2180 		ctx = sbi->compress.lzx;
2181 		if (!ctx) {
2182 			/* Lazy initialize LZX decompress context. */
2183 			ctx = lzx_allocate_decompressor();
2184 			if (!ctx) {
2185 				err = -ENOMEM;
2186 				goto out1;
2187 			}
2188 
2189 			sbi->compress.lzx = ctx;
2190 		}
2191 
2192 		if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2193 			/* Treat all errors as "invalid argument". */
2194 			err = -EINVAL;
2195 		}
2196 out1:
2197 		mutex_unlock(&sbi->compress.mtx_lzx);
2198 	} else {
2199 		/* XPRESS: Frame compressed. */
2200 		mutex_lock(&sbi->compress.mtx_xpress);
2201 		ctx = sbi->compress.xpress;
2202 		if (!ctx) {
2203 			/* Lazy initialize Xpress decompress context. */
2204 			ctx = xpress_allocate_decompressor();
2205 			if (!ctx) {
2206 				err = -ENOMEM;
2207 				goto out2;
2208 			}
2209 
2210 			sbi->compress.xpress = ctx;
2211 		}
2212 
2213 		if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2214 			/* Treat all errors as "invalid argument". */
2215 			err = -EINVAL;
2216 		}
2217 out2:
2218 		mutex_unlock(&sbi->compress.mtx_xpress);
2219 	}
2220 	return err;
2221 }
2222 #endif
2223 
2224 /*
2225  * ni_read_frame
2226  *
2227  * Pages - Array of locked pages.
2228  */
2229 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2230 		  u32 pages_per_frame, int copy)
2231 {
2232 	int err;
2233 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2234 	u8 cluster_bits = sbi->cluster_bits;
2235 	char *frame_ondisk = NULL;
2236 	char *frame_mem = NULL;
2237 	struct ATTR_LIST_ENTRY *le = NULL;
2238 	struct runs_tree *run = &ni->file.run;
2239 	u64 valid_size = ni->i_valid;
2240 	u64 vbo_disk;
2241 	size_t unc_size = 0;
2242 	u32 frame_size, i, ondisk_size;
2243 	struct page *pg;
2244 	struct ATTRIB *attr;
2245 	CLST frame, clst_data;
2246 
2247 	/*
2248 	 * To simplify decompress algorithm do vmap for source
2249 	 * and target pages.
2250 	 */
2251 	frame_size = pages_per_frame << PAGE_SHIFT;
2252 	frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2253 	if (!frame_mem) {
2254 		err = -ENOMEM;
2255 		goto out;
2256 	}
2257 
2258 	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2259 	if (!attr) {
2260 		err = -ENOENT;
2261 		goto out1;
2262 	}
2263 
2264 	if (!attr->non_res) {
2265 		u32 data_size = le32_to_cpu(attr->res.data_size);
2266 
2267 		memset(frame_mem, 0, frame_size);
2268 		if (frame_vbo < data_size) {
2269 			ondisk_size = data_size - frame_vbo;
2270 			memcpy(frame_mem, resident_data(attr) + frame_vbo,
2271 			       min(ondisk_size, frame_size));
2272 		}
2273 		err = 0;
2274 		goto out1;
2275 	}
2276 
2277 	if (frame_vbo >= valid_size) {
2278 		memset(frame_mem, 0, frame_size);
2279 		err = 0;
2280 		goto out1;
2281 	}
2282 
2283 	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2284 #ifndef CONFIG_NTFS3_LZX_XPRESS
2285 		err = -EOPNOTSUPP;
2286 		goto out1;
2287 #else
2288 		loff_t i_size = i_size_read(&ni->vfs_inode);
2289 		u32 frame_bits = ni_ext_compress_bits(ni);
2290 		u64 frame64 = frame_vbo >> frame_bits;
2291 		u64 frames, vbo_data;
2292 
2293 		if (frame_size != (1u << frame_bits)) {
2294 			err = -EINVAL;
2295 			goto out1;
2296 		}
2297 		switch (frame_size) {
2298 		case 0x1000:
2299 		case 0x2000:
2300 		case 0x4000:
2301 		case 0x8000:
2302 			break;
2303 		default:
2304 			/* Unknown compression. */
2305 			err = -EOPNOTSUPP;
2306 			goto out1;
2307 		}
2308 
2309 		attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2310 				    ARRAY_SIZE(WOF_NAME), NULL, NULL);
2311 		if (!attr) {
2312 			ntfs_inode_err(
2313 				&ni->vfs_inode,
2314 				"external compressed file should contains data attribute \"WofCompressedData\"");
2315 			err = -EINVAL;
2316 			goto out1;
2317 		}
2318 
2319 		if (!attr->non_res) {
2320 			run = NULL;
2321 		} else {
2322 			run = run_alloc();
2323 			if (!run) {
2324 				err = -ENOMEM;
2325 				goto out1;
2326 			}
2327 		}
2328 
2329 		frames = (i_size - 1) >> frame_bits;
2330 
2331 		err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2332 					  frame_bits, &ondisk_size, &vbo_data);
2333 		if (err)
2334 			goto out1;
2335 
2336 		if (frame64 == frames) {
2337 			unc_size = 1 + ((i_size - 1) & (frame_size - 1));
2338 			ondisk_size = attr_size(attr) - vbo_data;
2339 		} else {
2340 			unc_size = frame_size;
2341 		}
2342 
2343 		if (ondisk_size > frame_size) {
2344 			err = -EINVAL;
2345 			goto out1;
2346 		}
2347 
2348 		if (!attr->non_res) {
2349 			if (vbo_data + ondisk_size >
2350 			    le32_to_cpu(attr->res.data_size)) {
2351 				err = -EINVAL;
2352 				goto out1;
2353 			}
2354 
2355 			err = decompress_lzx_xpress(
2356 				sbi, Add2Ptr(resident_data(attr), vbo_data),
2357 				ondisk_size, frame_mem, unc_size, frame_size);
2358 			goto out1;
2359 		}
2360 		vbo_disk = vbo_data;
2361 		/* Load all runs to read [vbo_disk-vbo_to). */
2362 		err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2363 					   ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2364 					   vbo_data + ondisk_size);
2365 		if (err)
2366 			goto out1;
2367 #endif
2368 	} else if (is_attr_compressed(attr)) {
2369 		/* LZNT compression. */
2370 		if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2371 			err = -EOPNOTSUPP;
2372 			goto out1;
2373 		}
2374 
2375 		if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2376 			err = -EOPNOTSUPP;
2377 			goto out1;
2378 		}
2379 
2380 		down_write(&ni->file.run_lock);
2381 		run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2382 		frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2383 		err = attr_is_frame_compressed(ni, attr, frame, &clst_data,
2384 					       run);
2385 		up_write(&ni->file.run_lock);
2386 		if (err)
2387 			goto out1;
2388 
2389 		if (!clst_data) {
2390 			memset(frame_mem, 0, frame_size);
2391 			goto out1;
2392 		}
2393 
2394 		frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2395 		ondisk_size = clst_data << cluster_bits;
2396 
2397 		if (clst_data >= NTFS_LZNT_CLUSTERS) {
2398 			/* Frame is not compressed. */
2399 			down_read(&ni->file.run_lock);
2400 			err = ntfs_read_run(sbi, run, frame_mem, frame_vbo,
2401 					    ondisk_size);
2402 			up_read(&ni->file.run_lock);
2403 			goto out1;
2404 		}
2405 		vbo_disk = frame_vbo;
2406 	} else {
2407 		__builtin_unreachable();
2408 		err = -EINVAL;
2409 		goto out1;
2410 	}
2411 
2412 	/* Allocate memory to read compressed data to. */
2413 	frame_ondisk = kvmalloc(ondisk_size, GFP_KERNEL);
2414 	if (!frame_ondisk) {
2415 		err = -ENOMEM;
2416 		goto out1;
2417 	}
2418 
2419 	/* Read 'ondisk_size' bytes from disk. */
2420 	down_read(&ni->file.run_lock);
2421 	err = ntfs_read_run(sbi, run, frame_ondisk, vbo_disk, ondisk_size);
2422 	up_read(&ni->file.run_lock);
2423 	if (err)
2424 		goto out2;
2425 
2426 #ifdef CONFIG_NTFS3_LZX_XPRESS
2427 	if (run != &ni->file.run) {
2428 		/* LZX or XPRESS */
2429 		err = decompress_lzx_xpress(sbi, frame_ondisk, ondisk_size,
2430 					    frame_mem, unc_size, frame_size);
2431 	} else
2432 #endif
2433 	{
2434 		/* LZNT - Native NTFS compression. */
2435 		unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2436 					   frame_size);
2437 		if ((ssize_t)unc_size < 0)
2438 			err = unc_size;
2439 		else if (!unc_size || unc_size > frame_size)
2440 			err = -EINVAL;
2441 	}
2442 	if (!err && valid_size < frame_vbo + frame_size) {
2443 		size_t ok = valid_size - frame_vbo;
2444 
2445 		memset(frame_mem + ok, 0, frame_size - ok);
2446 	}
2447 
2448 out2:
2449 	kvfree(frame_ondisk);
2450 out1:
2451 #ifdef CONFIG_NTFS3_LZX_XPRESS
2452 	if (run != &ni->file.run)
2453 		run_free(run);
2454 	if (!err && copy) {
2455 		/* We are called from 'ni_decompress_file' */
2456 		/* Copy decompressed LZX or XPRESS data into new place. */
2457 		down_read(&ni->file.run_lock);
2458 		err = ntfs_write_run(sbi, &ni->file.run, frame_mem, frame_vbo,
2459 				     frame_size);
2460 		up_read(&ni->file.run_lock);
2461 	}
2462 #endif
2463 	vunmap(frame_mem);
2464 out:
2465 	for (i = 0; i < pages_per_frame; i++) {
2466 		pg = pages[i];
2467 		SetPageUptodate(pg);
2468 	}
2469 
2470 	return err;
2471 }
2472 
2473 /*
2474  * ni_write_frame
2475  *
2476  * Pages - Array of locked pages.
2477  */
2478 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2479 		   u32 pages_per_frame)
2480 {
2481 	int err;
2482 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2483 	struct folio *folio = page_folio(pages[0]);
2484 	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2485 	u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2486 	u64 frame_vbo = folio_pos(folio);
2487 	CLST frame = frame_vbo >> frame_bits;
2488 	char *frame_ondisk = NULL;
2489 	struct ATTR_LIST_ENTRY *le = NULL;
2490 	char *frame_mem;
2491 	struct ATTRIB *attr;
2492 	struct mft_inode *mi;
2493 	size_t compr_size, ondisk_size;
2494 	struct lznt *lznt;
2495 
2496 	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2497 	if (!attr) {
2498 		err = -ENOENT;
2499 		goto out;
2500 	}
2501 
2502 	if (WARN_ON(!is_attr_compressed(attr))) {
2503 		err = -EINVAL;
2504 		goto out;
2505 	}
2506 
2507 	if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2508 		err = -EOPNOTSUPP;
2509 		goto out;
2510 	}
2511 
2512 	if (!attr->non_res) {
2513 		down_write(&ni->file.run_lock);
2514 		err = attr_make_nonresident(ni, attr, le, mi,
2515 					    le32_to_cpu(attr->res.data_size),
2516 					    &ni->file.run, &attr, pages[0]);
2517 		up_write(&ni->file.run_lock);
2518 		if (err)
2519 			goto out;
2520 	}
2521 
2522 	if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2523 		err = -EOPNOTSUPP;
2524 		goto out;
2525 	}
2526 
2527 	/* Allocate memory to write compressed data to. */
2528 	frame_ondisk = kvmalloc(frame_size, GFP_KERNEL);
2529 	if (!frame_ondisk) {
2530 		err = -ENOMEM;
2531 		goto out;
2532 	}
2533 
2534 	/* Map in-memory frame for read-only. */
2535 	frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2536 	if (!frame_mem) {
2537 		err = -ENOMEM;
2538 		goto out1;
2539 	}
2540 
2541 	mutex_lock(&sbi->compress.mtx_lznt);
2542 	lznt = NULL;
2543 	if (!sbi->compress.lznt) {
2544 		/*
2545 		 * LZNT implements two levels of compression:
2546 		 * 0 - Standard compression
2547 		 * 1 - Best compression, requires a lot of cpu
2548 		 * use mount option?
2549 		 */
2550 		lznt = get_lznt_ctx(0);
2551 		if (!lznt) {
2552 			mutex_unlock(&sbi->compress.mtx_lznt);
2553 			err = -ENOMEM;
2554 			goto out2;
2555 		}
2556 
2557 		sbi->compress.lznt = lznt;
2558 		lznt = NULL;
2559 	}
2560 
2561 	/* Compress: frame_mem -> frame_ondisk */
2562 	compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2563 				   frame_size, sbi->compress.lznt);
2564 	mutex_unlock(&sbi->compress.mtx_lznt);
2565 	kfree(lznt);
2566 
2567 	if (compr_size + sbi->cluster_size > frame_size) {
2568 		/* Frame is not compressed. */
2569 		compr_size = frame_size;
2570 		ondisk_size = frame_size;
2571 	} else if (compr_size) {
2572 		/* Frame is compressed. */
2573 		ondisk_size = ntfs_up_cluster(sbi, compr_size);
2574 		memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2575 	} else {
2576 		/* Frame is sparsed. */
2577 		ondisk_size = 0;
2578 	}
2579 
2580 	down_write(&ni->file.run_lock);
2581 	run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2582 	err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2583 	up_write(&ni->file.run_lock);
2584 	if (err)
2585 		goto out2;
2586 
2587 	if (!ondisk_size)
2588 		goto out2;
2589 
2590 	down_read(&ni->file.run_lock);
2591 	err = ntfs_write_run(sbi, &ni->file.run,
2592 			     ondisk_size < frame_size ? frame_ondisk :
2593 							frame_mem,
2594 			     frame_vbo, ondisk_size);
2595 	up_read(&ni->file.run_lock);
2596 
2597 out2:
2598 	vunmap(frame_mem);
2599 out1:
2600 	kvfree(frame_ondisk);
2601 out:
2602 	return err;
2603 }
2604 
2605 /*
2606  * ni_remove_name - Removes name 'de' from MFT and from directory.
2607  * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2608  */
2609 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2610 		   struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2611 {
2612 	int err;
2613 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2614 	struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2615 	struct ATTR_FILE_NAME *fname;
2616 	struct ATTR_LIST_ENTRY *le;
2617 	struct mft_inode *mi;
2618 	u16 de_key_size = le16_to_cpu(de->key_size);
2619 	u8 name_type;
2620 
2621 	*undo_step = 0;
2622 
2623 	/* Find name in record. */
2624 	mi_get_ref(&dir_ni->mi, &de_name->home);
2625 
2626 	fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len,
2627 			      &de_name->home, &mi, &le);
2628 	if (!fname)
2629 		return -ENOENT;
2630 
2631 	memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2632 	name_type = paired_name(fname->type);
2633 
2634 	/* Mark ntfs as dirty. It will be cleared at umount. */
2635 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2636 
2637 	/* Step 1: Remove name from directory. */
2638 	err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2639 	if (err)
2640 		return err;
2641 
2642 	/* Step 2: Remove name from MFT. */
2643 	ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2644 
2645 	*undo_step = 2;
2646 
2647 	/* Get paired name. */
2648 	fname = ni_fname_type(ni, name_type, &mi, &le);
2649 	if (fname) {
2650 		u16 de2_key_size = fname_full_size(fname);
2651 
2652 		*de2 = Add2Ptr(de, 1024);
2653 		(*de2)->key_size = cpu_to_le16(de2_key_size);
2654 
2655 		memcpy(*de2 + 1, fname, de2_key_size);
2656 
2657 		/* Step 3: Remove paired name from directory. */
2658 		err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2659 					de2_key_size, sbi);
2660 		if (err)
2661 			return err;
2662 
2663 		/* Step 4: Remove paired name from MFT. */
2664 		ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2665 
2666 		*undo_step = 4;
2667 	}
2668 	return 0;
2669 }
2670 
2671 /*
2672  * ni_remove_name_undo - Paired function for ni_remove_name.
2673  *
2674  * Return: True if ok
2675  */
2676 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2677 			 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2678 {
2679 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2680 	struct ATTRIB *attr;
2681 	u16 de_key_size;
2682 
2683 	switch (undo_step) {
2684 	case 4:
2685 		de_key_size = le16_to_cpu(de2->key_size);
2686 		if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2687 				       &attr, NULL, NULL))
2688 			return false;
2689 		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2690 
2691 		mi_get_ref(&ni->mi, &de2->ref);
2692 		de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2693 					sizeof(struct NTFS_DE));
2694 		de2->flags = 0;
2695 		de2->res = 0;
2696 
2697 		if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL, 1))
2698 			return false;
2699 		fallthrough;
2700 
2701 	case 2:
2702 		de_key_size = le16_to_cpu(de->key_size);
2703 
2704 		if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2705 				       &attr, NULL, NULL))
2706 			return false;
2707 
2708 		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2709 		mi_get_ref(&ni->mi, &de->ref);
2710 
2711 		if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
2712 			return false;
2713 	}
2714 
2715 	return true;
2716 }
2717 
2718 /*
2719  * ni_add_name - Add new name into MFT and into directory.
2720  */
2721 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2722 		struct NTFS_DE *de)
2723 {
2724 	int err;
2725 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2726 	struct ATTRIB *attr;
2727 	struct ATTR_LIST_ENTRY *le;
2728 	struct mft_inode *mi;
2729 	struct ATTR_FILE_NAME *fname;
2730 	struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2731 	u16 de_key_size = le16_to_cpu(de->key_size);
2732 
2733 	if (sbi->options->windows_names &&
2734 	    !valid_windows_name(sbi, (struct le_str *)&de_name->name_len))
2735 		return -EINVAL;
2736 
2737 	/* If option "hide_dot_files" then set hidden attribute for dot files. */
2738 	if (ni->mi.sbi->options->hide_dot_files) {
2739 		if (de_name->name_len > 0 &&
2740 		    le16_to_cpu(de_name->name[0]) == '.')
2741 			ni->std_fa |= FILE_ATTRIBUTE_HIDDEN;
2742 		else
2743 			ni->std_fa &= ~FILE_ATTRIBUTE_HIDDEN;
2744 	}
2745 
2746 	mi_get_ref(&ni->mi, &de->ref);
2747 	mi_get_ref(&dir_ni->mi, &de_name->home);
2748 
2749 	/* Fill duplicate from any ATTR_NAME. */
2750 	fname = ni_fname_name(ni, NULL, NULL, NULL, NULL);
2751 	if (fname)
2752 		memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup));
2753 	de_name->dup.fa = ni->std_fa;
2754 
2755 	/* Insert new name into MFT. */
2756 	err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
2757 				 &mi, &le);
2758 	if (err)
2759 		return err;
2760 
2761 	memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
2762 
2763 	/* Insert new name into directory. */
2764 	err = indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 0);
2765 	if (err)
2766 		ni_remove_attr_le(ni, attr, mi, le);
2767 
2768 	return err;
2769 }
2770 
2771 /*
2772  * ni_rename - Remove one name and insert new name.
2773  */
2774 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
2775 	      struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de)
2776 {
2777 	int err;
2778 	struct NTFS_DE *de2 = NULL;
2779 	int undo = 0;
2780 
2781 	/*
2782 	 * There are two possible ways to rename:
2783 	 * 1) Add new name and remove old name.
2784 	 * 2) Remove old name and add new name.
2785 	 *
2786 	 * In most cases (not all!) adding new name into MFT and into directory can
2787 	 * allocate additional cluster(s).
2788 	 * Second way may result to bad inode if we can't add new name
2789 	 * and then can't restore (add) old name.
2790 	 */
2791 
2792 	/*
2793 	 * Way 1 - Add new + remove old.
2794 	 */
2795 	err = ni_add_name(new_dir_ni, ni, new_de);
2796 	if (!err) {
2797 		err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2798 		WARN_ON(err &&
2799 			ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo));
2800 	}
2801 
2802 	/*
2803 	 * Way 2 - Remove old + add new.
2804 	 */
2805 	/*
2806 	 *	err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2807 	 *	if (!err) {
2808 	 *		err = ni_add_name(new_dir_ni, ni, new_de);
2809 	 *		if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
2810 	 *			*is_bad = true;
2811 	 *	}
2812 	 */
2813 
2814 	return err;
2815 }
2816 
2817 /*
2818  * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
2819  */
2820 bool ni_is_dirty(struct inode *inode)
2821 {
2822 	struct ntfs_inode *ni = ntfs_i(inode);
2823 	struct rb_node *node;
2824 
2825 	if (ni->mi.dirty || ni->attr_list.dirty ||
2826 	    (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
2827 		return true;
2828 
2829 	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
2830 		if (rb_entry(node, struct mft_inode, node)->dirty)
2831 			return true;
2832 	}
2833 
2834 	return false;
2835 }
2836 
2837 /*
2838  * ni_seek_data_or_hole
2839  *
2840  * Helper function for ntfs_llseek( SEEK_DATA/SEEK_HOLE )
2841  */
2842 loff_t ni_seek_data_or_hole(struct ntfs_inode *ni, loff_t offset, bool data)
2843 {
2844 	int err;
2845 	u8 cluster_bits = ni->mi.sbi->cluster_bits;
2846 	CLST vcn, lcn, clen;
2847 	loff_t vbo;
2848 
2849 	/* Enumerate all fragments. */
2850 	for (vcn = offset >> cluster_bits;; vcn += clen) {
2851 		err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL, false,
2852 					  NULL, false);
2853 		if (err) {
2854 			return err;
2855 		}
2856 
2857 		if (lcn == RESIDENT_LCN) {
2858 			/* clen - resident size in bytes. clen == ni->vfs_inode.i_size */
2859 			if (offset >= clen) {
2860 				/* check eof. */
2861 				return -ENXIO;
2862 			}
2863 
2864 			if (data) {
2865 				return offset;
2866 			}
2867 
2868 			return clen;
2869 		}
2870 
2871 		if (lcn == EOF_LCN) {
2872 			if (data) {
2873 				return -ENXIO;
2874 			}
2875 
2876 			/* implicit hole at the end of file. */
2877 			return ni->vfs_inode.i_size;
2878 		}
2879 
2880 		if (data) {
2881 			/*
2882 			 * Adjust the file offset to the next location in the file greater than
2883 			 * or equal to offset containing data. If offset points to data, then
2884 			 * the file offset is set to offset.
2885 			 */
2886 			if (lcn != SPARSE_LCN) {
2887 				vbo = (u64)vcn << cluster_bits;
2888 				return max(vbo, offset);
2889 			}
2890 		} else {
2891 			/*
2892 			 * Adjust the file offset to the next hole in the file greater than or
2893 			 * equal to offset. If offset points into the middle of a hole, then the
2894 			 * file offset is set to offset. If there is no hole past offset, then the
2895 			 * file offset is adjusted to the end of the file
2896 			 * (i.e., there is an implicit hole at the end of any file).
2897 			 */
2898 			if (lcn == SPARSE_LCN &&
2899 			    /* native compression hole begins at aligned vcn. */
2900 			    (!(ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) ||
2901 			     !(vcn & (NTFS_LZNT_CLUSTERS - 1)))) {
2902 				vbo = (u64)vcn << cluster_bits;
2903 				return max(vbo, offset);
2904 			}
2905 		}
2906 
2907 		if (!clen) {
2908 			/* Corrupted file. */
2909 			return -EINVAL;
2910 		}
2911 	}
2912 }
2913 
2914 /*
2915  * ni_write_parents
2916  *
2917  * Helper function for ntfs_file_fsync.
2918  */
2919 int ni_write_parents(struct ntfs_inode *ni, int sync)
2920 {
2921 	int err = 0;
2922 	struct ATTRIB *attr = NULL;
2923 	struct ATTR_LIST_ENTRY *le = NULL;
2924 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2925 	struct super_block *sb = sbi->sb;
2926 
2927 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
2928 				    NULL))) {
2929 		struct inode *dir;
2930 		struct ATTR_FILE_NAME *fname;
2931 
2932 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
2933 		if (!fname)
2934 			continue;
2935 
2936 		/* Check simple case when parent inode equals current inode. */
2937 		if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
2938 			if (MFT_REC_ROOT != ni->vfs_inode.i_ino) {
2939 				ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
2940 				err = -EINVAL;
2941 			}
2942 			continue;
2943 		}
2944 
2945 		dir = ntfs_iget5(sb, &fname->home, NULL);
2946 		if (IS_ERR(dir)) {
2947 			ntfs_inode_warn(
2948 				&ni->vfs_inode,
2949 				"failed to open parent directory r=%lx to write",
2950 				(long)ino_get(&fname->home));
2951 			continue;
2952 		}
2953 
2954 		if (!is_bad_inode(dir)) {
2955 			int err2 = write_inode_now(dir, sync);
2956 			if (!err)
2957 				err = err2;
2958 		}
2959 		iput(dir);
2960 	}
2961 
2962 	return err;
2963 }
2964 
2965 /*
2966  * ni_update_parent
2967  *
2968  * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
2969  */
2970 static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
2971 			     int sync)
2972 {
2973 	struct ATTRIB *attr;
2974 	struct mft_inode *mi;
2975 	struct ATTR_LIST_ENTRY *le = NULL;
2976 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2977 	struct super_block *sb = sbi->sb;
2978 	bool re_dirty = false;
2979 
2980 	if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
2981 		dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
2982 		attr = NULL;
2983 		dup->alloc_size = 0;
2984 		dup->data_size = 0;
2985 	} else {
2986 		dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
2987 
2988 		attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
2989 				    &mi);
2990 		if (!attr) {
2991 			dup->alloc_size = dup->data_size = 0;
2992 		} else if (!attr->non_res) {
2993 			u32 data_size = le32_to_cpu(attr->res.data_size);
2994 
2995 			dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
2996 			dup->data_size = cpu_to_le64(data_size);
2997 		} else {
2998 			u64 new_valid = ni->i_valid;
2999 			u64 data_size = le64_to_cpu(attr->nres.data_size);
3000 			__le64 valid_le;
3001 
3002 			dup->alloc_size = is_attr_ext(attr) ?
3003 						  attr->nres.total_size :
3004 						  attr->nres.alloc_size;
3005 			dup->data_size = attr->nres.data_size;
3006 
3007 			if (new_valid > data_size)
3008 				new_valid = data_size;
3009 
3010 			valid_le = cpu_to_le64(new_valid);
3011 			if (valid_le != attr->nres.valid_size) {
3012 				attr->nres.valid_size = valid_le;
3013 				mi->dirty = true;
3014 			}
3015 		}
3016 	}
3017 
3018 	dup->extend_data = 0;
3019 
3020 	if (dup->fa & FILE_ATTRIBUTE_REPARSE_POINT) {
3021 		attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL,
3022 				    NULL);
3023 
3024 		if (attr) {
3025 			const struct REPARSE_POINT *rp;
3026 
3027 			rp = resident_data_ex(attr,
3028 					      sizeof(struct REPARSE_POINT));
3029 			/* If ATTR_REPARSE exists 'rp' can't be NULL. */
3030 			if (rp)
3031 				dup->extend_data = rp->ReparseTag;
3032 		}
3033 	} else if (ni->ni_flags & NI_FLAG_EA) {
3034 		attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3035 				    NULL);
3036 		if (attr) {
3037 			const struct EA_INFO *info;
3038 
3039 			info = resident_data_ex(attr, sizeof(struct EA_INFO));
3040 			/* If ATTR_EA_INFO exists 'info' can't be NULL. */
3041 			if (info)
3042 				dup->extend_data = info->size;
3043 		}
3044 	}
3045 
3046 	attr = NULL;
3047 	le = NULL;
3048 
3049 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3050 				    &mi))) {
3051 		struct inode *dir;
3052 		struct ATTR_FILE_NAME *fname;
3053 
3054 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
3055 		if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
3056 			continue;
3057 
3058 		/* Check simple case when parent inode equals current inode. */
3059 		if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
3060 			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3061 			continue;
3062 		}
3063 
3064 		/* ntfs_iget5 may sleep. */
3065 		dir = ntfs_iget5(sb, &fname->home, NULL);
3066 		if (IS_ERR(dir)) {
3067 			ntfs_inode_warn(
3068 				&ni->vfs_inode,
3069 				"failed to open parent directory r=%lx to update",
3070 				(long)ino_get(&fname->home));
3071 			continue;
3072 		}
3073 
3074 		if (!is_bad_inode(dir)) {
3075 			struct ntfs_inode *dir_ni = ntfs_i(dir);
3076 
3077 			if (!ni_trylock(dir_ni)) {
3078 				re_dirty = true;
3079 			} else {
3080 				indx_update_dup(dir_ni, sbi, fname, dup, sync);
3081 				ni_unlock(dir_ni);
3082 				memcpy(&fname->dup, dup, sizeof(fname->dup));
3083 				mi->dirty = true;
3084 			}
3085 		}
3086 		iput(dir);
3087 	}
3088 
3089 	return re_dirty;
3090 }
3091 
3092 /*
3093  * ni_write_inode - Write MFT base record and all subrecords to disk.
3094  */
3095 int ni_write_inode(struct inode *inode, int sync, const char *hint)
3096 {
3097 	int err = 0, err2;
3098 	struct ntfs_inode *ni = ntfs_i(inode);
3099 	struct super_block *sb = inode->i_sb;
3100 	struct ntfs_sb_info *sbi = sb->s_fs_info;
3101 	bool re_dirty = false;
3102 	struct ATTR_STD_INFO *std;
3103 	struct rb_node *node, *next;
3104 	struct NTFS_DUP_INFO dup;
3105 
3106 	if (is_bad_inode(inode) || sb_rdonly(sb))
3107 		return 0;
3108 
3109 	/* Avoid any operation if inode is bad. */
3110 	if (unlikely(is_bad_ni(ni)))
3111 		return -EINVAL;
3112 
3113 	if (unlikely(ntfs3_forced_shutdown(sb)))
3114 		return -EIO;
3115 
3116 	if (!ni_trylock(ni)) {
3117 		/* 'ni' is under modification, skip for now. */
3118 		mark_inode_dirty_sync(inode);
3119 		return 0;
3120 	}
3121 
3122 	if (!ni->mi.mrec)
3123 		goto out;
3124 
3125 	if (is_rec_inuse(ni->mi.mrec) &&
3126 	    !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3127 		bool modified = false;
3128 		struct timespec64 ts;
3129 
3130 		/* Update times in standard attribute. */
3131 		std = ni_std(ni);
3132 		if (!std) {
3133 			err = -EINVAL;
3134 			goto out;
3135 		}
3136 
3137 		/* Update the access times if they have changed. */
3138 		ts = inode_get_mtime(inode);
3139 		dup.m_time = kernel2nt(&ts);
3140 		if (std->m_time != dup.m_time) {
3141 			std->m_time = dup.m_time;
3142 			modified = true;
3143 		}
3144 
3145 		ts = inode_get_ctime(inode);
3146 		dup.c_time = kernel2nt(&ts);
3147 		if (std->c_time != dup.c_time) {
3148 			std->c_time = dup.c_time;
3149 			modified = true;
3150 		}
3151 
3152 		ts = inode_get_atime(inode);
3153 		dup.a_time = kernel2nt(&ts);
3154 		if (std->a_time != dup.a_time) {
3155 			std->a_time = dup.a_time;
3156 			modified = true;
3157 		}
3158 
3159 		dup.fa = ni->std_fa;
3160 		if (std->fa != dup.fa) {
3161 			std->fa = dup.fa;
3162 			modified = true;
3163 		}
3164 
3165 		/* std attribute is always in primary MFT record. */
3166 		if (modified)
3167 			ni->mi.dirty = true;
3168 
3169 		if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
3170 		    (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3171 		    /* Avoid __wait_on_freeing_inode(inode). */
3172 		    && (sb->s_flags & SB_ACTIVE)) {
3173 			dup.cr_time = std->cr_time;
3174 			/* Not critical if this function fail. */
3175 			re_dirty = ni_update_parent(ni, &dup, sync);
3176 
3177 			if (re_dirty)
3178 				ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3179 			else
3180 				ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3181 		}
3182 
3183 		/* Update attribute list. */
3184 		if (ni->attr_list.size && ni->attr_list.dirty) {
3185 			if (inode->i_ino != MFT_REC_MFT || sync) {
3186 				err = ni_try_remove_attr_list(ni);
3187 				if (err)
3188 					goto out;
3189 			}
3190 
3191 			err = al_update(ni, sync);
3192 			if (err)
3193 				goto out;
3194 		}
3195 	}
3196 
3197 	for (node = rb_first(&ni->mi_tree); node; node = next) {
3198 		struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3199 		bool is_empty;
3200 
3201 		next = rb_next(node);
3202 
3203 		if (!mi->dirty)
3204 			continue;
3205 
3206 		is_empty = !mi_enum_attr(ni, mi, NULL);
3207 
3208 		if (is_empty)
3209 			clear_rec_inuse(mi->mrec);
3210 
3211 		err2 = mi_write(mi, sync);
3212 		if (!err && err2)
3213 			err = err2;
3214 
3215 		if (is_empty) {
3216 			ntfs_mark_rec_free(sbi, mi->rno, false);
3217 			rb_erase(node, &ni->mi_tree);
3218 			mi_put(mi);
3219 		}
3220 	}
3221 
3222 	if (ni->mi.dirty) {
3223 		err2 = mi_write(&ni->mi, sync);
3224 		if (!err && err2)
3225 			err = err2;
3226 	}
3227 out:
3228 	ni_unlock(ni);
3229 
3230 	if (err) {
3231 		ntfs_inode_err(inode, "%s failed, %d.", hint, err);
3232 		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3233 		return err;
3234 	}
3235 
3236 	if (re_dirty)
3237 		mark_inode_dirty_sync(inode);
3238 
3239 	return 0;
3240 }
3241 
3242 /*
3243  * Force to allocate all delay allocated clusters.
3244  */
3245 int ni_allocate_da_blocks(struct ntfs_inode *ni)
3246 {
3247 	int err;
3248 
3249 	ni_lock(ni);
3250 	down_write(&ni->file.run_lock);
3251 
3252 	err = ni_allocate_da_blocks_locked(ni);
3253 
3254 	up_write(&ni->file.run_lock);
3255 	ni_unlock(ni);
3256 
3257 	return err;
3258 }
3259 
3260 /*
3261  * Force to allocate all delay allocated clusters.
3262  */
3263 int ni_allocate_da_blocks_locked(struct ntfs_inode *ni)
3264 {
3265 	int err;
3266 
3267 	if (!ni->file.run_da.count)
3268 		return 0;
3269 
3270 	if (is_sparsed(ni)) {
3271 		CLST vcn, lcn, clen, alen;
3272 		bool new;
3273 
3274 		/*
3275 		 * Sparse file allocates clusters in 'attr_data_get_block_locked'
3276 		 */
3277 		while (run_get_entry(&ni->file.run_da, 0, &vcn, &lcn, &clen)) {
3278 			/* TODO: zero=true? */
3279 			err = attr_data_get_block_locked(ni, vcn, clen, &lcn,
3280 							 &alen, &new, true,
3281 							 NULL, true);
3282 			if (err)
3283 				break;
3284 			if (!new) {
3285 				err = -EINVAL;
3286 				break;
3287 			}
3288 		}
3289 	} else {
3290 		/*
3291 		 * Normal file allocates clusters in 'attr_set_size'
3292 		 */
3293 		err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run,
3294 				       ni->vfs_inode.i_size, &ni->i_valid,
3295 				       false, NULL, true);
3296 	}
3297 
3298 	return err;
3299 }
3300