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