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