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