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