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