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