xref: /linux/fs/ntfs3/attrib.c (revision 55f1b540d893da740a81200450014c45a8103f54)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16 
17 /*
18  * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage
19  * preallocate algorithm.
20  */
21 #ifndef NTFS_MIN_LOG2_OF_CLUMP
22 #define NTFS_MIN_LOG2_OF_CLUMP 16
23 #endif
24 
25 #ifndef NTFS_MAX_LOG2_OF_CLUMP
26 #define NTFS_MAX_LOG2_OF_CLUMP 26
27 #endif
28 
29 // 16M
30 #define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8))
31 // 16G
32 #define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8))
33 
34 static inline u64 get_pre_allocated(u64 size)
35 {
36 	u32 clump;
37 	u8 align_shift;
38 	u64 ret;
39 
40 	if (size <= NTFS_CLUMP_MIN) {
41 		clump = 1 << NTFS_MIN_LOG2_OF_CLUMP;
42 		align_shift = NTFS_MIN_LOG2_OF_CLUMP;
43 	} else if (size >= NTFS_CLUMP_MAX) {
44 		clump = 1 << NTFS_MAX_LOG2_OF_CLUMP;
45 		align_shift = NTFS_MAX_LOG2_OF_CLUMP;
46 	} else {
47 		align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 +
48 			      __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP));
49 		clump = 1u << align_shift;
50 	}
51 
52 	ret = (((size + clump - 1) >> align_shift)) << align_shift;
53 
54 	return ret;
55 }
56 
57 /*
58  * attr_load_runs - Load all runs stored in @attr.
59  */
60 static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
61 			  struct runs_tree *run, const CLST *vcn)
62 {
63 	int err;
64 	CLST svcn = le64_to_cpu(attr->nres.svcn);
65 	CLST evcn = le64_to_cpu(attr->nres.evcn);
66 	u32 asize;
67 	u16 run_off;
68 
69 	if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
70 		return 0;
71 
72 	if (vcn && (evcn < *vcn || *vcn < svcn))
73 		return -EINVAL;
74 
75 	asize = le32_to_cpu(attr->size);
76 	run_off = le16_to_cpu(attr->nres.run_off);
77 
78 	if (run_off > asize)
79 		return -EINVAL;
80 
81 	err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
82 			    vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
83 			    asize - run_off);
84 	if (err < 0)
85 		return err;
86 
87 	return 0;
88 }
89 
90 /*
91  * run_deallocate_ex - Deallocate clusters.
92  */
93 static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
94 			     CLST vcn, CLST len, CLST *done, bool trim)
95 {
96 	int err = 0;
97 	CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
98 	size_t idx;
99 
100 	if (!len)
101 		goto out;
102 
103 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
104 failed:
105 		run_truncate(run, vcn0);
106 		err = -EINVAL;
107 		goto out;
108 	}
109 
110 	for (;;) {
111 		if (clen > len)
112 			clen = len;
113 
114 		if (!clen) {
115 			err = -EINVAL;
116 			goto out;
117 		}
118 
119 		if (lcn != SPARSE_LCN) {
120 			if (sbi) {
121 				/* mark bitmap range [lcn + clen) as free and trim clusters. */
122 				mark_as_free_ex(sbi, lcn, clen, trim);
123 			}
124 			dn += clen;
125 		}
126 
127 		len -= clen;
128 		if (!len)
129 			break;
130 
131 		vcn_next = vcn + clen;
132 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
133 		    vcn != vcn_next) {
134 			/* Save memory - don't load entire run. */
135 			goto failed;
136 		}
137 	}
138 
139 out:
140 	if (done)
141 		*done += dn;
142 
143 	return err;
144 }
145 
146 /*
147  * attr_allocate_clusters - Find free space, mark it as used and store in @run.
148  */
149 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
150 			   CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
151 			   enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
152 			   CLST *new_lcn, CLST *new_len)
153 {
154 	int err;
155 	CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
156 	size_t cnt = run->count;
157 
158 	for (;;) {
159 		err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
160 					       opt);
161 
162 		if (err == -ENOSPC && pre) {
163 			pre = 0;
164 			if (*pre_alloc)
165 				*pre_alloc = 0;
166 			continue;
167 		}
168 
169 		if (err)
170 			goto out;
171 
172 		if (vcn == vcn0) {
173 			/* Return the first fragment. */
174 			if (new_lcn)
175 				*new_lcn = lcn;
176 			if (new_len)
177 				*new_len = flen;
178 		}
179 
180 		/* Add new fragment into run storage. */
181 		if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) {
182 			/* Undo last 'ntfs_look_for_free_space' */
183 			mark_as_free_ex(sbi, lcn, len, false);
184 			err = -ENOMEM;
185 			goto out;
186 		}
187 
188 		if (opt & ALLOCATE_ZERO) {
189 			u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
190 
191 			err = blkdev_issue_zeroout(sbi->sb->s_bdev,
192 						   (sector_t)lcn << shift,
193 						   (sector_t)flen << shift,
194 						   GFP_NOFS, 0);
195 			if (err)
196 				goto out;
197 		}
198 
199 		vcn += flen;
200 
201 		if (flen >= len || (opt & ALLOCATE_MFT) ||
202 		    (fr && run->count - cnt >= fr)) {
203 			*alen = vcn - vcn0;
204 			return 0;
205 		}
206 
207 		len -= flen;
208 	}
209 
210 out:
211 	/* Undo 'ntfs_look_for_free_space' */
212 	if (vcn - vcn0) {
213 		run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
214 		run_truncate(run, vcn0);
215 	}
216 
217 	return err;
218 }
219 
220 /*
221  * attr_make_nonresident
222  *
223  * If page is not NULL - it is already contains resident data
224  * and locked (called from ni_write_frame()).
225  */
226 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
227 			  struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
228 			  u64 new_size, struct runs_tree *run,
229 			  struct ATTRIB **ins_attr, struct page *page)
230 {
231 	struct ntfs_sb_info *sbi;
232 	struct ATTRIB *attr_s;
233 	struct MFT_REC *rec;
234 	u32 used, asize, rsize, aoff;
235 	bool is_data;
236 	CLST len, alen;
237 	char *next;
238 	int err;
239 
240 	if (attr->non_res) {
241 		*ins_attr = attr;
242 		return 0;
243 	}
244 
245 	sbi = mi->sbi;
246 	rec = mi->mrec;
247 	attr_s = NULL;
248 	used = le32_to_cpu(rec->used);
249 	asize = le32_to_cpu(attr->size);
250 	next = Add2Ptr(attr, asize);
251 	aoff = PtrOffset(rec, attr);
252 	rsize = le32_to_cpu(attr->res.data_size);
253 	is_data = attr->type == ATTR_DATA && !attr->name_len;
254 
255 	/* len - how many clusters required to store 'rsize' bytes */
256 	if (is_attr_compressed(attr)) {
257 		u8 shift = sbi->cluster_bits + NTFS_LZNT_CUNIT;
258 		len = ((rsize + (1u << shift) - 1) >> shift) << NTFS_LZNT_CUNIT;
259 	} else {
260 		len = bytes_to_cluster(sbi, rsize);
261 	}
262 
263 	run_init(run);
264 
265 	/* Make a copy of original attribute. */
266 	attr_s = kmemdup(attr, asize, GFP_NOFS);
267 	if (!attr_s) {
268 		err = -ENOMEM;
269 		goto out;
270 	}
271 
272 	if (!len) {
273 		/* Empty resident -> Empty nonresident. */
274 		alen = 0;
275 	} else {
276 		const char *data = resident_data(attr);
277 
278 		err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
279 					     ALLOCATE_DEF, &alen, 0, NULL,
280 					     NULL);
281 		if (err)
282 			goto out1;
283 
284 		if (!rsize) {
285 			/* Empty resident -> Non empty nonresident. */
286 		} else if (!is_data) {
287 			err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
288 			if (err)
289 				goto out2;
290 		} else if (!page) {
291 			struct address_space *mapping = ni->vfs_inode.i_mapping;
292 			struct folio *folio;
293 
294 			folio = __filemap_get_folio(
295 				mapping, 0, FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
296 				mapping_gfp_mask(mapping));
297 			if (IS_ERR(folio)) {
298 				err = PTR_ERR(folio);
299 				goto out2;
300 			}
301 			folio_fill_tail(folio, 0, data, rsize);
302 			folio_mark_uptodate(folio);
303 			folio_mark_dirty(folio);
304 			folio_unlock(folio);
305 			folio_put(folio);
306 		}
307 	}
308 
309 	/* Remove original attribute. */
310 	used -= asize;
311 	memmove(attr, Add2Ptr(attr, asize), used - aoff);
312 	rec->used = cpu_to_le32(used);
313 	mi->dirty = true;
314 	if (le)
315 		al_remove_le(ni, le);
316 
317 	err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
318 				    attr_s->name_len, run, 0, alen,
319 				    attr_s->flags, &attr, NULL, NULL);
320 	if (err)
321 		goto out3;
322 
323 	kfree(attr_s);
324 	attr->nres.data_size = cpu_to_le64(rsize);
325 	attr->nres.valid_size = attr->nres.data_size;
326 
327 	*ins_attr = attr;
328 
329 	if (is_data)
330 		ni->ni_flags &= ~NI_FLAG_RESIDENT;
331 
332 	/* Resident attribute becomes non resident. */
333 	return 0;
334 
335 out3:
336 	attr = Add2Ptr(rec, aoff);
337 	memmove(next, attr, used - aoff);
338 	memcpy(attr, attr_s, asize);
339 	rec->used = cpu_to_le32(used + asize);
340 	mi->dirty = true;
341 out2:
342 	/* Undo: do not trim new allocated clusters. */
343 	run_deallocate(sbi, run, false);
344 	run_close(run);
345 out1:
346 	kfree(attr_s);
347 out:
348 	return err;
349 }
350 
351 /*
352  * attr_set_size_res - Helper for attr_set_size().
353  */
354 static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
355 			     struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
356 			     u64 new_size, struct runs_tree *run,
357 			     struct ATTRIB **ins_attr)
358 {
359 	struct ntfs_sb_info *sbi = mi->sbi;
360 	struct MFT_REC *rec = mi->mrec;
361 	u32 used = le32_to_cpu(rec->used);
362 	u32 asize = le32_to_cpu(attr->size);
363 	u32 aoff = PtrOffset(rec, attr);
364 	u32 rsize = le32_to_cpu(attr->res.data_size);
365 	u32 tail = used - aoff - asize;
366 	char *next = Add2Ptr(attr, asize);
367 	s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
368 
369 	if (dsize < 0) {
370 		memmove(next + dsize, next, tail);
371 	} else if (dsize > 0) {
372 		if (used + dsize > sbi->max_bytes_per_attr)
373 			return attr_make_nonresident(ni, attr, le, mi, new_size,
374 						     run, ins_attr, NULL);
375 
376 		memmove(next + dsize, next, tail);
377 		memset(next, 0, dsize);
378 	}
379 
380 	if (new_size > rsize)
381 		memset(Add2Ptr(resident_data(attr), rsize), 0,
382 		       new_size - rsize);
383 
384 	rec->used = cpu_to_le32(used + dsize);
385 	attr->size = cpu_to_le32(asize + dsize);
386 	attr->res.data_size = cpu_to_le32(new_size);
387 	mi->dirty = true;
388 	*ins_attr = attr;
389 
390 	return 0;
391 }
392 
393 /*
394  * attr_set_size - Change the size of attribute.
395  *
396  * Extend:
397  *   - Sparse/compressed: No allocated clusters.
398  *   - Normal: Append allocated and preallocated new clusters.
399  * Shrink:
400  *   - No deallocate if @keep_prealloc is set.
401  */
402 int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
403 		  const __le16 *name, u8 name_len, struct runs_tree *run,
404 		  u64 new_size, const u64 *new_valid, bool keep_prealloc,
405 		  struct ATTRIB **ret)
406 {
407 	int err = 0;
408 	struct ntfs_sb_info *sbi = ni->mi.sbi;
409 	u8 cluster_bits = sbi->cluster_bits;
410 	bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
411 		      !name_len;
412 	u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
413 	struct ATTRIB *attr = NULL, *attr_b;
414 	struct ATTR_LIST_ENTRY *le, *le_b;
415 	struct mft_inode *mi, *mi_b;
416 	CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
417 	CLST next_svcn, pre_alloc = -1, done = 0;
418 	bool is_ext, is_bad = false;
419 	bool dirty = false;
420 	u32 align;
421 	struct MFT_REC *rec;
422 
423 again:
424 	alen = 0;
425 	le_b = NULL;
426 	attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
427 			      &mi_b);
428 	if (!attr_b) {
429 		err = -ENOENT;
430 		goto bad_inode;
431 	}
432 
433 	if (!attr_b->non_res) {
434 		err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
435 					&attr_b);
436 		if (err)
437 			return err;
438 
439 		/* Return if file is still resident. */
440 		if (!attr_b->non_res) {
441 			dirty = true;
442 			goto ok1;
443 		}
444 
445 		/* Layout of records may be changed, so do a full search. */
446 		goto again;
447 	}
448 
449 	is_ext = is_attr_ext(attr_b);
450 	align = sbi->cluster_size;
451 	if (is_ext)
452 		align <<= attr_b->nres.c_unit;
453 
454 	old_valid = le64_to_cpu(attr_b->nres.valid_size);
455 	old_size = le64_to_cpu(attr_b->nres.data_size);
456 	old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
457 
458 again_1:
459 	old_alen = old_alloc >> cluster_bits;
460 
461 	new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
462 	new_alen = new_alloc >> cluster_bits;
463 
464 	if (keep_prealloc && new_size < old_size) {
465 		attr_b->nres.data_size = cpu_to_le64(new_size);
466 		mi_b->dirty = dirty = true;
467 		goto ok;
468 	}
469 
470 	vcn = old_alen - 1;
471 
472 	svcn = le64_to_cpu(attr_b->nres.svcn);
473 	evcn = le64_to_cpu(attr_b->nres.evcn);
474 
475 	if (svcn <= vcn && vcn <= evcn) {
476 		attr = attr_b;
477 		le = le_b;
478 		mi = mi_b;
479 	} else if (!le_b) {
480 		err = -EINVAL;
481 		goto bad_inode;
482 	} else {
483 		le = le_b;
484 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
485 				    &mi);
486 		if (!attr) {
487 			err = -EINVAL;
488 			goto bad_inode;
489 		}
490 
491 next_le_1:
492 		svcn = le64_to_cpu(attr->nres.svcn);
493 		evcn = le64_to_cpu(attr->nres.evcn);
494 	}
495 	/*
496 	 * Here we have:
497 	 * attr,mi,le - last attribute segment (containing 'vcn').
498 	 * attr_b,mi_b,le_b - base (primary) attribute segment.
499 	 */
500 next_le:
501 	rec = mi->mrec;
502 	err = attr_load_runs(attr, ni, run, NULL);
503 	if (err)
504 		goto out;
505 
506 	if (new_size > old_size) {
507 		CLST to_allocate;
508 		size_t free;
509 
510 		if (new_alloc <= old_alloc) {
511 			attr_b->nres.data_size = cpu_to_le64(new_size);
512 			mi_b->dirty = dirty = true;
513 			goto ok;
514 		}
515 
516 		/*
517 		 * Add clusters. In simple case we have to:
518 		 *  - allocate space (vcn, lcn, len)
519 		 *  - update packed run in 'mi'
520 		 *  - update attr->nres.evcn
521 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
522 		 */
523 		to_allocate = new_alen - old_alen;
524 add_alloc_in_same_attr_seg:
525 		lcn = 0;
526 		if (is_mft) {
527 			/* MFT allocates clusters from MFT zone. */
528 			pre_alloc = 0;
529 		} else if (is_ext) {
530 			/* No preallocate for sparse/compress. */
531 			pre_alloc = 0;
532 		} else if (pre_alloc == -1) {
533 			pre_alloc = 0;
534 			if (type == ATTR_DATA && !name_len &&
535 			    sbi->options->prealloc) {
536 				pre_alloc = bytes_to_cluster(
537 						    sbi, get_pre_allocated(
538 								 new_size)) -
539 					    new_alen;
540 			}
541 
542 			/* Get the last LCN to allocate from. */
543 			if (old_alen &&
544 			    !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
545 				lcn = SPARSE_LCN;
546 			}
547 
548 			if (lcn == SPARSE_LCN)
549 				lcn = 0;
550 			else if (lcn)
551 				lcn += 1;
552 
553 			free = wnd_zeroes(&sbi->used.bitmap);
554 			if (to_allocate > free) {
555 				err = -ENOSPC;
556 				goto out;
557 			}
558 
559 			if (pre_alloc && to_allocate + pre_alloc > free)
560 				pre_alloc = 0;
561 		}
562 
563 		vcn = old_alen;
564 
565 		if (is_ext) {
566 			if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
567 					   false)) {
568 				err = -ENOMEM;
569 				goto out;
570 			}
571 			alen = to_allocate;
572 		} else {
573 			/* ~3 bytes per fragment. */
574 			err = attr_allocate_clusters(
575 				sbi, run, vcn, lcn, to_allocate, &pre_alloc,
576 				is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
577 				is_mft ? 0 :
578 					 (sbi->record_size -
579 					  le32_to_cpu(rec->used) + 8) /
580 							 3 +
581 						 1,
582 				NULL, NULL);
583 			if (err)
584 				goto out;
585 		}
586 
587 		done += alen;
588 		vcn += alen;
589 		if (to_allocate > alen)
590 			to_allocate -= alen;
591 		else
592 			to_allocate = 0;
593 
594 pack_runs:
595 		err = mi_pack_runs(mi, attr, run, vcn - svcn);
596 		if (err)
597 			goto undo_1;
598 
599 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
600 		new_alloc_tmp = (u64)next_svcn << cluster_bits;
601 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
602 		mi_b->dirty = dirty = true;
603 
604 		if (next_svcn >= vcn && !to_allocate) {
605 			/* Normal way. Update attribute and exit. */
606 			attr_b->nres.data_size = cpu_to_le64(new_size);
607 			goto ok;
608 		}
609 
610 		/* At least two MFT to avoid recursive loop. */
611 		if (is_mft && next_svcn == vcn &&
612 		    ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
613 			new_size = new_alloc_tmp;
614 			attr_b->nres.data_size = attr_b->nres.alloc_size;
615 			goto ok;
616 		}
617 
618 		if (le32_to_cpu(rec->used) < sbi->record_size) {
619 			old_alen = next_svcn;
620 			evcn = old_alen - 1;
621 			goto add_alloc_in_same_attr_seg;
622 		}
623 
624 		attr_b->nres.data_size = attr_b->nres.alloc_size;
625 		if (new_alloc_tmp < old_valid)
626 			attr_b->nres.valid_size = attr_b->nres.data_size;
627 
628 		if (type == ATTR_LIST) {
629 			err = ni_expand_list(ni);
630 			if (err)
631 				goto undo_2;
632 			if (next_svcn < vcn)
633 				goto pack_runs;
634 
635 			/* Layout of records is changed. */
636 			goto again;
637 		}
638 
639 		if (!ni->attr_list.size) {
640 			err = ni_create_attr_list(ni);
641 			/* In case of error layout of records is not changed. */
642 			if (err)
643 				goto undo_2;
644 			/* Layout of records is changed. */
645 		}
646 
647 		if (next_svcn >= vcn) {
648 			/* This is MFT data, repeat. */
649 			goto again;
650 		}
651 
652 		/* Insert new attribute segment. */
653 		err = ni_insert_nonresident(ni, type, name, name_len, run,
654 					    next_svcn, vcn - next_svcn,
655 					    attr_b->flags, &attr, &mi, NULL);
656 
657 		/*
658 		 * Layout of records maybe changed.
659 		 * Find base attribute to update.
660 		 */
661 		le_b = NULL;
662 		attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
663 				      NULL, &mi_b);
664 		if (!attr_b) {
665 			err = -EINVAL;
666 			goto bad_inode;
667 		}
668 
669 		if (err) {
670 			/* ni_insert_nonresident failed. */
671 			attr = NULL;
672 			goto undo_2;
673 		}
674 
675 		/* keep runs for $MFT::$ATTR_DATA and $MFT::$ATTR_BITMAP. */
676 		if (ni->mi.rno != MFT_REC_MFT)
677 			run_truncate_head(run, evcn + 1);
678 
679 		svcn = le64_to_cpu(attr->nres.svcn);
680 		evcn = le64_to_cpu(attr->nres.evcn);
681 
682 		/*
683 		 * Attribute is in consistency state.
684 		 * Save this point to restore to if next steps fail.
685 		 */
686 		old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
687 		attr_b->nres.valid_size = attr_b->nres.data_size =
688 			attr_b->nres.alloc_size = cpu_to_le64(old_size);
689 		mi_b->dirty = dirty = true;
690 		goto again_1;
691 	}
692 
693 	if (new_size != old_size ||
694 	    (new_alloc != old_alloc && !keep_prealloc)) {
695 		/*
696 		 * Truncate clusters. In simple case we have to:
697 		 *  - update packed run in 'mi'
698 		 *  - update attr->nres.evcn
699 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
700 		 *  - mark and trim clusters as free (vcn, lcn, len)
701 		 */
702 		CLST dlen = 0;
703 
704 		vcn = max(svcn, new_alen);
705 		new_alloc_tmp = (u64)vcn << cluster_bits;
706 
707 		if (vcn > svcn) {
708 			err = mi_pack_runs(mi, attr, run, vcn - svcn);
709 			if (err)
710 				goto out;
711 		} else if (le && le->vcn) {
712 			u16 le_sz = le16_to_cpu(le->size);
713 
714 			/*
715 			 * NOTE: List entries for one attribute are always
716 			 * the same size. We deal with last entry (vcn==0)
717 			 * and it is not first in entries array
718 			 * (list entry for std attribute always first).
719 			 * So it is safe to step back.
720 			 */
721 			mi_remove_attr(NULL, mi, attr);
722 
723 			if (!al_remove_le(ni, le)) {
724 				err = -EINVAL;
725 				goto bad_inode;
726 			}
727 
728 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
729 		} else {
730 			attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
731 			mi->dirty = true;
732 		}
733 
734 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
735 
736 		if (vcn == new_alen) {
737 			attr_b->nres.data_size = cpu_to_le64(new_size);
738 			if (new_size < old_valid)
739 				attr_b->nres.valid_size =
740 					attr_b->nres.data_size;
741 		} else {
742 			if (new_alloc_tmp <=
743 			    le64_to_cpu(attr_b->nres.data_size))
744 				attr_b->nres.data_size =
745 					attr_b->nres.alloc_size;
746 			if (new_alloc_tmp <
747 			    le64_to_cpu(attr_b->nres.valid_size))
748 				attr_b->nres.valid_size =
749 					attr_b->nres.alloc_size;
750 		}
751 		mi_b->dirty = dirty = true;
752 
753 		err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
754 					true);
755 		if (err)
756 			goto out;
757 
758 		if (is_ext) {
759 			/* dlen - really deallocated clusters. */
760 			le64_sub_cpu(&attr_b->nres.total_size,
761 				     ((u64)dlen << cluster_bits));
762 		}
763 
764 		run_truncate(run, vcn);
765 
766 		if (new_alloc_tmp <= new_alloc)
767 			goto ok;
768 
769 		old_size = new_alloc_tmp;
770 		vcn = svcn - 1;
771 
772 		if (le == le_b) {
773 			attr = attr_b;
774 			mi = mi_b;
775 			evcn = svcn - 1;
776 			svcn = 0;
777 			goto next_le;
778 		}
779 
780 		if (le->type != type || le->name_len != name_len ||
781 		    memcmp(le_name(le), name, name_len * sizeof(short))) {
782 			err = -EINVAL;
783 			goto bad_inode;
784 		}
785 
786 		err = ni_load_mi(ni, le, &mi);
787 		if (err)
788 			goto out;
789 
790 		attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
791 		if (!attr) {
792 			err = -EINVAL;
793 			goto bad_inode;
794 		}
795 		goto next_le_1;
796 	}
797 
798 ok:
799 	if (new_valid) {
800 		__le64 valid = cpu_to_le64(min(*new_valid, new_size));
801 
802 		if (attr_b->nres.valid_size != valid) {
803 			attr_b->nres.valid_size = valid;
804 			mi_b->dirty = true;
805 		}
806 	}
807 
808 ok1:
809 	if (ret)
810 		*ret = attr_b;
811 
812 	if (((type == ATTR_DATA && !name_len) ||
813 	     (type == ATTR_ALLOC && name == I30_NAME))) {
814 		/* Update inode_set_bytes. */
815 		if (attr_b->non_res) {
816 			new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
817 			if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
818 				inode_set_bytes(&ni->vfs_inode, new_alloc);
819 				dirty = true;
820 			}
821 		}
822 
823 		/* Don't forget to update duplicate information in parent. */
824 		if (dirty) {
825 			ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
826 			mark_inode_dirty(&ni->vfs_inode);
827 		}
828 	}
829 
830 	return 0;
831 
832 undo_2:
833 	vcn -= alen;
834 	attr_b->nres.data_size = cpu_to_le64(old_size);
835 	attr_b->nres.valid_size = cpu_to_le64(old_valid);
836 	attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
837 
838 	/* Restore 'attr' and 'mi'. */
839 	if (attr)
840 		goto restore_run;
841 
842 	if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
843 	    svcn <= le64_to_cpu(attr_b->nres.evcn)) {
844 		attr = attr_b;
845 		le = le_b;
846 		mi = mi_b;
847 	} else if (!le_b) {
848 		err = -EINVAL;
849 		goto bad_inode;
850 	} else {
851 		le = le_b;
852 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
853 				    &svcn, &mi);
854 		if (!attr)
855 			goto bad_inode;
856 	}
857 
858 restore_run:
859 	if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
860 		is_bad = true;
861 
862 undo_1:
863 	run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
864 
865 	run_truncate(run, vcn);
866 out:
867 	if (is_bad) {
868 bad_inode:
869 		_ntfs_bad_inode(&ni->vfs_inode);
870 	}
871 	return err;
872 }
873 
874 /*
875  * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
876  *
877  * @new == NULL means just to get current mapping for 'vcn'
878  * @new != NULL means allocate real cluster if 'vcn' maps to hole
879  * @zero - zeroout new allocated clusters
880  *
881  *  NOTE:
882  *  - @new != NULL is called only for sparsed or compressed attributes.
883  *  - new allocated clusters are zeroed via blkdev_issue_zeroout.
884  */
885 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
886 			CLST *len, bool *new, bool zero)
887 {
888 	int err = 0;
889 	struct runs_tree *run = &ni->file.run;
890 	struct ntfs_sb_info *sbi;
891 	u8 cluster_bits;
892 	struct ATTRIB *attr, *attr_b;
893 	struct ATTR_LIST_ENTRY *le, *le_b;
894 	struct mft_inode *mi, *mi_b;
895 	CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
896 	CLST alloc, evcn;
897 	unsigned fr;
898 	u64 total_size, total_size0;
899 	int step = 0;
900 
901 	if (new)
902 		*new = false;
903 
904 	/* Try to find in cache. */
905 	down_read(&ni->file.run_lock);
906 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
907 		*len = 0;
908 	up_read(&ni->file.run_lock);
909 
910 	if (*len && (*lcn != SPARSE_LCN || !new))
911 		return 0; /* Fast normal way without allocation. */
912 
913 	/* No cluster in cache or we need to allocate cluster in hole. */
914 	sbi = ni->mi.sbi;
915 	cluster_bits = sbi->cluster_bits;
916 
917 	ni_lock(ni);
918 	down_write(&ni->file.run_lock);
919 
920 	/* Repeat the code above (under write lock). */
921 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
922 		*len = 0;
923 
924 	if (*len) {
925 		if (*lcn != SPARSE_LCN || !new)
926 			goto out; /* normal way without allocation. */
927 		if (clen > *len)
928 			clen = *len;
929 	}
930 
931 	le_b = NULL;
932 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
933 	if (!attr_b) {
934 		err = -ENOENT;
935 		goto out;
936 	}
937 
938 	if (!attr_b->non_res) {
939 		*lcn = RESIDENT_LCN;
940 		*len = 1;
941 		goto out;
942 	}
943 
944 	asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
945 	if (vcn >= asize) {
946 		if (new) {
947 			err = -EINVAL;
948 		} else {
949 			*len = 1;
950 			*lcn = SPARSE_LCN;
951 		}
952 		goto out;
953 	}
954 
955 	svcn = le64_to_cpu(attr_b->nres.svcn);
956 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
957 
958 	attr = attr_b;
959 	le = le_b;
960 	mi = mi_b;
961 
962 	if (le_b && (vcn < svcn || evcn1 <= vcn)) {
963 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
964 				    &mi);
965 		if (!attr) {
966 			err = -EINVAL;
967 			goto out;
968 		}
969 		svcn = le64_to_cpu(attr->nres.svcn);
970 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
971 	}
972 
973 	/* Load in cache actual information. */
974 	err = attr_load_runs(attr, ni, run, NULL);
975 	if (err)
976 		goto out;
977 
978 	/* Check for compressed frame. */
979 	err = attr_is_frame_compressed(ni, attr_b, vcn >> NTFS_LZNT_CUNIT,
980 				       &hint);
981 	if (err)
982 		goto out;
983 
984 	if (hint) {
985 		/* if frame is compressed - don't touch it. */
986 		*lcn = COMPRESSED_LCN;
987 		/* length to the end of frame. */
988 		*len = NTFS_LZNT_CLUSTERS - (vcn & (NTFS_LZNT_CLUSTERS - 1));
989 		err = 0;
990 		goto out;
991 	}
992 
993 	if (!*len) {
994 		if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
995 			if (*lcn != SPARSE_LCN || !new)
996 				goto ok; /* Slow normal way without allocation. */
997 
998 			if (clen > *len)
999 				clen = *len;
1000 		} else if (!new) {
1001 			/* Here we may return -ENOENT.
1002 			 * In any case caller gets zero length. */
1003 			goto ok;
1004 		}
1005 	}
1006 
1007 	if (!is_attr_ext(attr_b)) {
1008 		/* The code below only for sparsed or compressed attributes. */
1009 		err = -EINVAL;
1010 		goto out;
1011 	}
1012 
1013 	vcn0 = vcn;
1014 	to_alloc = clen;
1015 	fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
1016 	/* Allocate frame aligned clusters.
1017 	 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
1018 	 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
1019 	if (attr_b->nres.c_unit) {
1020 		CLST clst_per_frame = 1u << attr_b->nres.c_unit;
1021 		CLST cmask = ~(clst_per_frame - 1);
1022 
1023 		/* Get frame aligned vcn and to_alloc. */
1024 		vcn = vcn0 & cmask;
1025 		to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
1026 		if (fr < clst_per_frame)
1027 			fr = clst_per_frame;
1028 		zero = true;
1029 
1030 		/* Check if 'vcn' and 'vcn0' in different attribute segments. */
1031 		if (vcn < svcn || evcn1 <= vcn) {
1032 			struct ATTRIB *attr2;
1033 			/* Load runs for truncated vcn. */
1034 			attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL,
1035 					     0, &vcn, &mi);
1036 			if (!attr2) {
1037 				err = -EINVAL;
1038 				goto out;
1039 			}
1040 			evcn1 = le64_to_cpu(attr2->nres.evcn) + 1;
1041 			err = attr_load_runs(attr2, ni, run, NULL);
1042 			if (err)
1043 				goto out;
1044 		}
1045 	}
1046 
1047 	if (vcn + to_alloc > asize)
1048 		to_alloc = asize - vcn;
1049 
1050 	/* Get the last LCN to allocate from. */
1051 	hint = 0;
1052 
1053 	if (vcn > evcn1) {
1054 		if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1055 				   false)) {
1056 			err = -ENOMEM;
1057 			goto out;
1058 		}
1059 	} else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1060 		hint = -1;
1061 	}
1062 
1063 	/* Allocate and zeroout new clusters. */
1064 	err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
1065 				     zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
1066 				     fr, lcn, len);
1067 	if (err)
1068 		goto out;
1069 	*new = true;
1070 	step = 1;
1071 
1072 	end = vcn + alen;
1073 	/* Save 'total_size0' to restore if error. */
1074 	total_size0 = le64_to_cpu(attr_b->nres.total_size);
1075 	total_size = total_size0 + ((u64)alen << cluster_bits);
1076 
1077 	if (vcn != vcn0) {
1078 		if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1079 			err = -EINVAL;
1080 			goto out;
1081 		}
1082 		if (*lcn == SPARSE_LCN) {
1083 			/* Internal error. Should not happened. */
1084 			WARN_ON(1);
1085 			err = -EINVAL;
1086 			goto out;
1087 		}
1088 		/* Check case when vcn0 + len overlaps new allocated clusters. */
1089 		if (vcn0 + *len > end)
1090 			*len = end - vcn0;
1091 	}
1092 
1093 repack:
1094 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1095 	if (err)
1096 		goto out;
1097 
1098 	attr_b->nres.total_size = cpu_to_le64(total_size);
1099 	inode_set_bytes(&ni->vfs_inode, total_size);
1100 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1101 
1102 	mi_b->dirty = true;
1103 	mark_inode_dirty(&ni->vfs_inode);
1104 
1105 	/* Stored [vcn : next_svcn) from [vcn : end). */
1106 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1107 
1108 	if (end <= evcn1) {
1109 		if (next_svcn == evcn1) {
1110 			/* Normal way. Update attribute and exit. */
1111 			goto ok;
1112 		}
1113 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1114 		if (!ni->attr_list.size) {
1115 			err = ni_create_attr_list(ni);
1116 			if (err)
1117 				goto undo1;
1118 			/* Layout of records is changed. */
1119 			le_b = NULL;
1120 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1121 					      0, NULL, &mi_b);
1122 			if (!attr_b) {
1123 				err = -ENOENT;
1124 				goto out;
1125 			}
1126 
1127 			attr = attr_b;
1128 			le = le_b;
1129 			mi = mi_b;
1130 			goto repack;
1131 		}
1132 	}
1133 
1134 	/*
1135 	 * The code below may require additional cluster (to extend attribute list)
1136 	 * and / or one MFT record
1137 	 * It is too complex to undo operations if -ENOSPC occurs deep inside
1138 	 * in 'ni_insert_nonresident'.
1139 	 * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
1140 	 */
1141 	if (!ntfs_check_for_free_space(sbi, 1, 1)) {
1142 		/* Undo step 1. */
1143 		err = -ENOSPC;
1144 		goto undo1;
1145 	}
1146 
1147 	step = 2;
1148 	svcn = evcn1;
1149 
1150 	/* Estimate next attribute. */
1151 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1152 
1153 	if (!attr) {
1154 		/* Insert new attribute segment. */
1155 		goto ins_ext;
1156 	}
1157 
1158 	/* Try to update existed attribute segment. */
1159 	alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
1160 	evcn = le64_to_cpu(attr->nres.evcn);
1161 
1162 	if (end < next_svcn)
1163 		end = next_svcn;
1164 	while (end > evcn) {
1165 		/* Remove segment [svcn : evcn). */
1166 		mi_remove_attr(NULL, mi, attr);
1167 
1168 		if (!al_remove_le(ni, le)) {
1169 			err = -EINVAL;
1170 			goto out;
1171 		}
1172 
1173 		if (evcn + 1 >= alloc) {
1174 			/* Last attribute segment. */
1175 			evcn1 = evcn + 1;
1176 			goto ins_ext;
1177 		}
1178 
1179 		if (ni_load_mi(ni, le, &mi)) {
1180 			attr = NULL;
1181 			goto out;
1182 		}
1183 
1184 		attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, &le->id);
1185 		if (!attr) {
1186 			err = -EINVAL;
1187 			goto out;
1188 		}
1189 		svcn = le64_to_cpu(attr->nres.svcn);
1190 		evcn = le64_to_cpu(attr->nres.evcn);
1191 	}
1192 
1193 	if (end < svcn)
1194 		end = svcn;
1195 
1196 	err = attr_load_runs(attr, ni, run, &end);
1197 	if (err)
1198 		goto out;
1199 
1200 	evcn1 = evcn + 1;
1201 	attr->nres.svcn = cpu_to_le64(next_svcn);
1202 	err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1203 	if (err)
1204 		goto out;
1205 
1206 	le->vcn = cpu_to_le64(next_svcn);
1207 	ni->attr_list.dirty = true;
1208 	mi->dirty = true;
1209 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1210 
1211 ins_ext:
1212 	if (evcn1 > next_svcn) {
1213 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1214 					    next_svcn, evcn1 - next_svcn,
1215 					    attr_b->flags, &attr, &mi, NULL);
1216 		if (err)
1217 			goto out;
1218 	}
1219 ok:
1220 	run_truncate_around(run, vcn);
1221 out:
1222 	if (err && step > 1) {
1223 		/* Too complex to restore. */
1224 		_ntfs_bad_inode(&ni->vfs_inode);
1225 	}
1226 	up_write(&ni->file.run_lock);
1227 	ni_unlock(ni);
1228 
1229 	return err;
1230 
1231 undo1:
1232 	/* Undo step1. */
1233 	attr_b->nres.total_size = cpu_to_le64(total_size0);
1234 	inode_set_bytes(&ni->vfs_inode, total_size0);
1235 
1236 	if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) ||
1237 	    !run_add_entry(run, vcn, SPARSE_LCN, alen, false) ||
1238 	    mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
1239 		_ntfs_bad_inode(&ni->vfs_inode);
1240 	}
1241 	goto out;
1242 }
1243 
1244 int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio)
1245 {
1246 	u64 vbo;
1247 	struct ATTRIB *attr;
1248 	u32 data_size;
1249 	size_t len;
1250 
1251 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1252 	if (!attr)
1253 		return -EINVAL;
1254 
1255 	if (attr->non_res)
1256 		return E_NTFS_NONRESIDENT;
1257 
1258 	vbo = folio->index << PAGE_SHIFT;
1259 	data_size = le32_to_cpu(attr->res.data_size);
1260 	if (vbo > data_size)
1261 		len = 0;
1262 	else
1263 		len = min(data_size - vbo, folio_size(folio));
1264 
1265 	folio_fill_tail(folio, 0, resident_data(attr) + vbo, len);
1266 	folio_mark_uptodate(folio);
1267 
1268 	return 0;
1269 }
1270 
1271 int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio)
1272 {
1273 	u64 vbo;
1274 	struct mft_inode *mi;
1275 	struct ATTRIB *attr;
1276 	u32 data_size;
1277 
1278 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1279 	if (!attr)
1280 		return -EINVAL;
1281 
1282 	if (attr->non_res) {
1283 		/* Return special error code to check this case. */
1284 		return E_NTFS_NONRESIDENT;
1285 	}
1286 
1287 	vbo = folio->index << PAGE_SHIFT;
1288 	data_size = le32_to_cpu(attr->res.data_size);
1289 	if (vbo < data_size) {
1290 		char *data = resident_data(attr);
1291 		size_t len = min(data_size - vbo, folio_size(folio));
1292 
1293 		memcpy_from_folio(data + vbo, folio, 0, len);
1294 		mi->dirty = true;
1295 	}
1296 	ni->i_valid = data_size;
1297 
1298 	return 0;
1299 }
1300 
1301 /*
1302  * attr_load_runs_vcn - Load runs with VCN.
1303  */
1304 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1305 		       const __le16 *name, u8 name_len, struct runs_tree *run,
1306 		       CLST vcn)
1307 {
1308 	struct ATTRIB *attr;
1309 	int err;
1310 	CLST svcn, evcn;
1311 	u16 ro;
1312 
1313 	if (!ni) {
1314 		/* Is record corrupted? */
1315 		return -ENOENT;
1316 	}
1317 
1318 	attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1319 	if (!attr) {
1320 		/* Is record corrupted? */
1321 		return -ENOENT;
1322 	}
1323 
1324 	svcn = le64_to_cpu(attr->nres.svcn);
1325 	evcn = le64_to_cpu(attr->nres.evcn);
1326 
1327 	if (evcn < vcn || vcn < svcn) {
1328 		/* Is record corrupted? */
1329 		return -EINVAL;
1330 	}
1331 
1332 	ro = le16_to_cpu(attr->nres.run_off);
1333 
1334 	if (ro > le32_to_cpu(attr->size))
1335 		return -EINVAL;
1336 
1337 	err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1338 			    Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1339 	if (err < 0)
1340 		return err;
1341 	return 0;
1342 }
1343 
1344 /*
1345  * attr_load_runs_range - Load runs for given range [from to).
1346  */
1347 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1348 			 const __le16 *name, u8 name_len, struct runs_tree *run,
1349 			 u64 from, u64 to)
1350 {
1351 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1352 	u8 cluster_bits = sbi->cluster_bits;
1353 	CLST vcn;
1354 	CLST vcn_last = (to - 1) >> cluster_bits;
1355 	CLST lcn, clen;
1356 	int err;
1357 
1358 	for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1359 		if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1360 			err = attr_load_runs_vcn(ni, type, name, name_len, run,
1361 						 vcn);
1362 			if (err)
1363 				return err;
1364 			clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1365 		}
1366 	}
1367 
1368 	return 0;
1369 }
1370 
1371 #ifdef CONFIG_NTFS3_LZX_XPRESS
1372 /*
1373  * attr_wof_frame_info
1374  *
1375  * Read header of Xpress/LZX file to get info about frame.
1376  */
1377 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1378 			struct runs_tree *run, u64 frame, u64 frames,
1379 			u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1380 {
1381 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1382 	u64 vbo[2], off[2], wof_size;
1383 	u32 voff;
1384 	u8 bytes_per_off;
1385 	char *addr;
1386 	struct folio *folio;
1387 	int i, err;
1388 	__le32 *off32;
1389 	__le64 *off64;
1390 
1391 	if (ni->vfs_inode.i_size < 0x100000000ull) {
1392 		/* File starts with array of 32 bit offsets. */
1393 		bytes_per_off = sizeof(__le32);
1394 		vbo[1] = frame << 2;
1395 		*vbo_data = frames << 2;
1396 	} else {
1397 		/* File starts with array of 64 bit offsets. */
1398 		bytes_per_off = sizeof(__le64);
1399 		vbo[1] = frame << 3;
1400 		*vbo_data = frames << 3;
1401 	}
1402 
1403 	/*
1404 	 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1405 	 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1406 	 */
1407 	if (!attr->non_res) {
1408 		if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1409 			ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1410 			return -EINVAL;
1411 		}
1412 		addr = resident_data(attr);
1413 
1414 		if (bytes_per_off == sizeof(__le32)) {
1415 			off32 = Add2Ptr(addr, vbo[1]);
1416 			off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1417 			off[1] = le32_to_cpu(off32[0]);
1418 		} else {
1419 			off64 = Add2Ptr(addr, vbo[1]);
1420 			off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1421 			off[1] = le64_to_cpu(off64[0]);
1422 		}
1423 
1424 		*vbo_data += off[0];
1425 		*ondisk_size = off[1] - off[0];
1426 		return 0;
1427 	}
1428 
1429 	wof_size = le64_to_cpu(attr->nres.data_size);
1430 	down_write(&ni->file.run_lock);
1431 	folio = ni->file.offs_folio;
1432 	if (!folio) {
1433 		folio = folio_alloc(GFP_KERNEL, 0);
1434 		if (!folio) {
1435 			err = -ENOMEM;
1436 			goto out;
1437 		}
1438 		folio->index = -1;
1439 		ni->file.offs_folio = folio;
1440 	}
1441 	folio_lock(folio);
1442 	addr = folio_address(folio);
1443 
1444 	if (vbo[1]) {
1445 		voff = vbo[1] & (PAGE_SIZE - 1);
1446 		vbo[0] = vbo[1] - bytes_per_off;
1447 		i = 0;
1448 	} else {
1449 		voff = 0;
1450 		vbo[0] = 0;
1451 		off[0] = 0;
1452 		i = 1;
1453 	}
1454 
1455 	do {
1456 		pgoff_t index = vbo[i] >> PAGE_SHIFT;
1457 
1458 		if (index != folio->index) {
1459 			struct page *page = &folio->page;
1460 			u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1461 			u64 to = min(from + PAGE_SIZE, wof_size);
1462 
1463 			err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1464 						   ARRAY_SIZE(WOF_NAME), run,
1465 						   from, to);
1466 			if (err)
1467 				goto out1;
1468 
1469 			err = ntfs_bio_pages(sbi, run, &page, 1, from,
1470 					     to - from, REQ_OP_READ);
1471 			if (err) {
1472 				folio->index = -1;
1473 				goto out1;
1474 			}
1475 			folio->index = index;
1476 		}
1477 
1478 		if (i) {
1479 			if (bytes_per_off == sizeof(__le32)) {
1480 				off32 = Add2Ptr(addr, voff);
1481 				off[1] = le32_to_cpu(*off32);
1482 			} else {
1483 				off64 = Add2Ptr(addr, voff);
1484 				off[1] = le64_to_cpu(*off64);
1485 			}
1486 		} else if (!voff) {
1487 			if (bytes_per_off == sizeof(__le32)) {
1488 				off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1489 				off[0] = le32_to_cpu(*off32);
1490 			} else {
1491 				off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1492 				off[0] = le64_to_cpu(*off64);
1493 			}
1494 		} else {
1495 			/* Two values in one page. */
1496 			if (bytes_per_off == sizeof(__le32)) {
1497 				off32 = Add2Ptr(addr, voff);
1498 				off[0] = le32_to_cpu(off32[-1]);
1499 				off[1] = le32_to_cpu(off32[0]);
1500 			} else {
1501 				off64 = Add2Ptr(addr, voff);
1502 				off[0] = le64_to_cpu(off64[-1]);
1503 				off[1] = le64_to_cpu(off64[0]);
1504 			}
1505 			break;
1506 		}
1507 	} while (++i < 2);
1508 
1509 	*vbo_data += off[0];
1510 	*ondisk_size = off[1] - off[0];
1511 
1512 out1:
1513 	folio_unlock(folio);
1514 out:
1515 	up_write(&ni->file.run_lock);
1516 	return err;
1517 }
1518 #endif
1519 
1520 /*
1521  * attr_is_frame_compressed - Used to detect compressed frame.
1522  *
1523  * attr - base (primary) attribute segment.
1524  * Only base segments contains valid 'attr->nres.c_unit'
1525  */
1526 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1527 			     CLST frame, CLST *clst_data)
1528 {
1529 	int err;
1530 	u32 clst_frame;
1531 	CLST clen, lcn, vcn, alen, slen, vcn_next;
1532 	size_t idx;
1533 	struct runs_tree *run;
1534 
1535 	*clst_data = 0;
1536 
1537 	if (!is_attr_compressed(attr))
1538 		return 0;
1539 
1540 	if (!attr->non_res)
1541 		return 0;
1542 
1543 	clst_frame = 1u << attr->nres.c_unit;
1544 	vcn = frame * clst_frame;
1545 	run = &ni->file.run;
1546 
1547 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1548 		err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1549 					 attr->name_len, run, vcn);
1550 		if (err)
1551 			return err;
1552 
1553 		if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1554 			return -EINVAL;
1555 	}
1556 
1557 	if (lcn == SPARSE_LCN) {
1558 		/* Sparsed frame. */
1559 		return 0;
1560 	}
1561 
1562 	if (clen >= clst_frame) {
1563 		/*
1564 		 * The frame is not compressed 'cause
1565 		 * it does not contain any sparse clusters.
1566 		 */
1567 		*clst_data = clst_frame;
1568 		return 0;
1569 	}
1570 
1571 	alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1572 	slen = 0;
1573 	*clst_data = clen;
1574 
1575 	/*
1576 	 * The frame is compressed if *clst_data + slen >= clst_frame.
1577 	 * Check next fragments.
1578 	 */
1579 	while ((vcn += clen) < alen) {
1580 		vcn_next = vcn;
1581 
1582 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1583 		    vcn_next != vcn) {
1584 			err = attr_load_runs_vcn(ni, attr->type,
1585 						 attr_name(attr),
1586 						 attr->name_len, run, vcn_next);
1587 			if (err)
1588 				return err;
1589 			vcn = vcn_next;
1590 
1591 			if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1592 				return -EINVAL;
1593 		}
1594 
1595 		if (lcn == SPARSE_LCN) {
1596 			slen += clen;
1597 		} else {
1598 			if (slen) {
1599 				/*
1600 				 * Data_clusters + sparse_clusters =
1601 				 * not enough for frame.
1602 				 */
1603 				return -EINVAL;
1604 			}
1605 			*clst_data += clen;
1606 		}
1607 
1608 		if (*clst_data + slen >= clst_frame) {
1609 			if (!slen) {
1610 				/*
1611 				 * There is no sparsed clusters in this frame
1612 				 * so it is not compressed.
1613 				 */
1614 				*clst_data = clst_frame;
1615 			} else {
1616 				/* Frame is compressed. */
1617 			}
1618 			break;
1619 		}
1620 	}
1621 
1622 	return 0;
1623 }
1624 
1625 /*
1626  * attr_allocate_frame - Allocate/free clusters for @frame.
1627  *
1628  * Assumed: down_write(&ni->file.run_lock);
1629  */
1630 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1631 			u64 new_valid)
1632 {
1633 	int err = 0;
1634 	struct runs_tree *run = &ni->file.run;
1635 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1636 	struct ATTRIB *attr = NULL, *attr_b;
1637 	struct ATTR_LIST_ENTRY *le, *le_b;
1638 	struct mft_inode *mi, *mi_b;
1639 	CLST svcn, evcn1, next_svcn, len;
1640 	CLST vcn, end, clst_data;
1641 	u64 total_size, valid_size, data_size;
1642 
1643 	le_b = NULL;
1644 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1645 	if (!attr_b)
1646 		return -ENOENT;
1647 
1648 	if (!is_attr_ext(attr_b))
1649 		return -EINVAL;
1650 
1651 	vcn = frame << NTFS_LZNT_CUNIT;
1652 	total_size = le64_to_cpu(attr_b->nres.total_size);
1653 
1654 	svcn = le64_to_cpu(attr_b->nres.svcn);
1655 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1656 	data_size = le64_to_cpu(attr_b->nres.data_size);
1657 
1658 	if (svcn <= vcn && vcn < evcn1) {
1659 		attr = attr_b;
1660 		le = le_b;
1661 		mi = mi_b;
1662 	} else if (!le_b) {
1663 		err = -EINVAL;
1664 		goto out;
1665 	} else {
1666 		le = le_b;
1667 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1668 				    &mi);
1669 		if (!attr) {
1670 			err = -EINVAL;
1671 			goto out;
1672 		}
1673 		svcn = le64_to_cpu(attr->nres.svcn);
1674 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1675 	}
1676 
1677 	err = attr_load_runs(attr, ni, run, NULL);
1678 	if (err)
1679 		goto out;
1680 
1681 	err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data);
1682 	if (err)
1683 		goto out;
1684 
1685 	total_size -= (u64)clst_data << sbi->cluster_bits;
1686 
1687 	len = bytes_to_cluster(sbi, compr_size);
1688 
1689 	if (len == clst_data)
1690 		goto out;
1691 
1692 	if (len < clst_data) {
1693 		err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1694 					NULL, true);
1695 		if (err)
1696 			goto out;
1697 
1698 		if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1699 				   false)) {
1700 			err = -ENOMEM;
1701 			goto out;
1702 		}
1703 		end = vcn + clst_data;
1704 		/* Run contains updated range [vcn + len : end). */
1705 	} else {
1706 		CLST alen, hint = 0;
1707 		/* Get the last LCN to allocate from. */
1708 		if (vcn + clst_data &&
1709 		    !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1710 				      NULL)) {
1711 			hint = -1;
1712 		}
1713 
1714 		err = attr_allocate_clusters(sbi, run, vcn + clst_data,
1715 					     hint + 1, len - clst_data, NULL,
1716 					     ALLOCATE_DEF, &alen, 0, NULL,
1717 					     NULL);
1718 		if (err)
1719 			goto out;
1720 
1721 		end = vcn + len;
1722 		/* Run contains updated range [vcn + clst_data : end). */
1723 	}
1724 
1725 	total_size += (u64)len << sbi->cluster_bits;
1726 
1727 repack:
1728 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1729 	if (err)
1730 		goto out;
1731 
1732 	attr_b->nres.total_size = cpu_to_le64(total_size);
1733 	inode_set_bytes(&ni->vfs_inode, total_size);
1734 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1735 
1736 	mi_b->dirty = true;
1737 	mark_inode_dirty(&ni->vfs_inode);
1738 
1739 	/* Stored [vcn : next_svcn) from [vcn : end). */
1740 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1741 
1742 	if (end <= evcn1) {
1743 		if (next_svcn == evcn1) {
1744 			/* Normal way. Update attribute and exit. */
1745 			goto ok;
1746 		}
1747 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1748 		if (!ni->attr_list.size) {
1749 			err = ni_create_attr_list(ni);
1750 			if (err)
1751 				goto out;
1752 			/* Layout of records is changed. */
1753 			le_b = NULL;
1754 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1755 					      0, NULL, &mi_b);
1756 			if (!attr_b) {
1757 				err = -ENOENT;
1758 				goto out;
1759 			}
1760 
1761 			attr = attr_b;
1762 			le = le_b;
1763 			mi = mi_b;
1764 			goto repack;
1765 		}
1766 	}
1767 
1768 	svcn = evcn1;
1769 
1770 	/* Estimate next attribute. */
1771 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1772 
1773 	if (attr) {
1774 		CLST alloc = bytes_to_cluster(
1775 			sbi, le64_to_cpu(attr_b->nres.alloc_size));
1776 		CLST evcn = le64_to_cpu(attr->nres.evcn);
1777 
1778 		if (end < next_svcn)
1779 			end = next_svcn;
1780 		while (end > evcn) {
1781 			/* Remove segment [svcn : evcn). */
1782 			mi_remove_attr(NULL, mi, attr);
1783 
1784 			if (!al_remove_le(ni, le)) {
1785 				err = -EINVAL;
1786 				goto out;
1787 			}
1788 
1789 			if (evcn + 1 >= alloc) {
1790 				/* Last attribute segment. */
1791 				evcn1 = evcn + 1;
1792 				goto ins_ext;
1793 			}
1794 
1795 			if (ni_load_mi(ni, le, &mi)) {
1796 				attr = NULL;
1797 				goto out;
1798 			}
1799 
1800 			attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1801 					    &le->id);
1802 			if (!attr) {
1803 				err = -EINVAL;
1804 				goto out;
1805 			}
1806 			svcn = le64_to_cpu(attr->nres.svcn);
1807 			evcn = le64_to_cpu(attr->nres.evcn);
1808 		}
1809 
1810 		if (end < svcn)
1811 			end = svcn;
1812 
1813 		err = attr_load_runs(attr, ni, run, &end);
1814 		if (err)
1815 			goto out;
1816 
1817 		evcn1 = evcn + 1;
1818 		attr->nres.svcn = cpu_to_le64(next_svcn);
1819 		err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1820 		if (err)
1821 			goto out;
1822 
1823 		le->vcn = cpu_to_le64(next_svcn);
1824 		ni->attr_list.dirty = true;
1825 		mi->dirty = true;
1826 
1827 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1828 	}
1829 ins_ext:
1830 	if (evcn1 > next_svcn) {
1831 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1832 					    next_svcn, evcn1 - next_svcn,
1833 					    attr_b->flags, &attr, &mi, NULL);
1834 		if (err)
1835 			goto out;
1836 	}
1837 ok:
1838 	run_truncate_around(run, vcn);
1839 out:
1840 	if (attr_b) {
1841 		if (new_valid > data_size)
1842 			new_valid = data_size;
1843 
1844 		valid_size = le64_to_cpu(attr_b->nres.valid_size);
1845 		if (new_valid != valid_size) {
1846 			attr_b->nres.valid_size = cpu_to_le64(valid_size);
1847 			mi_b->dirty = true;
1848 		}
1849 	}
1850 
1851 	return err;
1852 }
1853 
1854 /*
1855  * attr_collapse_range - Collapse range in file.
1856  */
1857 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1858 {
1859 	int err = 0;
1860 	struct runs_tree *run = &ni->file.run;
1861 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1862 	struct ATTRIB *attr = NULL, *attr_b;
1863 	struct ATTR_LIST_ENTRY *le, *le_b;
1864 	struct mft_inode *mi, *mi_b;
1865 	CLST svcn, evcn1, len, dealloc, alen;
1866 	CLST vcn, end;
1867 	u64 valid_size, data_size, alloc_size, total_size;
1868 	u32 mask;
1869 	__le16 a_flags;
1870 
1871 	if (!bytes)
1872 		return 0;
1873 
1874 	le_b = NULL;
1875 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1876 	if (!attr_b)
1877 		return -ENOENT;
1878 
1879 	if (!attr_b->non_res) {
1880 		/* Attribute is resident. Nothing to do? */
1881 		return 0;
1882 	}
1883 
1884 	data_size = le64_to_cpu(attr_b->nres.data_size);
1885 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1886 	a_flags = attr_b->flags;
1887 
1888 	if (is_attr_ext(attr_b)) {
1889 		total_size = le64_to_cpu(attr_b->nres.total_size);
1890 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1891 	} else {
1892 		total_size = alloc_size;
1893 		mask = sbi->cluster_mask;
1894 	}
1895 
1896 	if ((vbo & mask) || (bytes & mask)) {
1897 		/* Allow to collapse only cluster aligned ranges. */
1898 		return -EINVAL;
1899 	}
1900 
1901 	if (vbo > data_size)
1902 		return -EINVAL;
1903 
1904 	down_write(&ni->file.run_lock);
1905 
1906 	if (vbo + bytes >= data_size) {
1907 		u64 new_valid = min(ni->i_valid, vbo);
1908 
1909 		/* Simple truncate file at 'vbo'. */
1910 		truncate_setsize(&ni->vfs_inode, vbo);
1911 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1912 				    &new_valid, true, NULL);
1913 
1914 		if (!err && new_valid < ni->i_valid)
1915 			ni->i_valid = new_valid;
1916 
1917 		goto out;
1918 	}
1919 
1920 	/*
1921 	 * Enumerate all attribute segments and collapse.
1922 	 */
1923 	alen = alloc_size >> sbi->cluster_bits;
1924 	vcn = vbo >> sbi->cluster_bits;
1925 	len = bytes >> sbi->cluster_bits;
1926 	end = vcn + len;
1927 	dealloc = 0;
1928 
1929 	svcn = le64_to_cpu(attr_b->nres.svcn);
1930 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1931 
1932 	if (svcn <= vcn && vcn < evcn1) {
1933 		attr = attr_b;
1934 		le = le_b;
1935 		mi = mi_b;
1936 	} else if (!le_b) {
1937 		err = -EINVAL;
1938 		goto out;
1939 	} else {
1940 		le = le_b;
1941 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1942 				    &mi);
1943 		if (!attr) {
1944 			err = -EINVAL;
1945 			goto out;
1946 		}
1947 
1948 		svcn = le64_to_cpu(attr->nres.svcn);
1949 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1950 	}
1951 
1952 	for (;;) {
1953 		if (svcn >= end) {
1954 			/* Shift VCN- */
1955 			attr->nres.svcn = cpu_to_le64(svcn - len);
1956 			attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1957 			if (le) {
1958 				le->vcn = attr->nres.svcn;
1959 				ni->attr_list.dirty = true;
1960 			}
1961 			mi->dirty = true;
1962 		} else if (svcn < vcn || end < evcn1) {
1963 			CLST vcn1, eat, next_svcn;
1964 
1965 			/* Collapse a part of this attribute segment. */
1966 			err = attr_load_runs(attr, ni, run, &svcn);
1967 			if (err)
1968 				goto out;
1969 			vcn1 = max(vcn, svcn);
1970 			eat = min(end, evcn1) - vcn1;
1971 
1972 			err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1973 						true);
1974 			if (err)
1975 				goto out;
1976 
1977 			if (!run_collapse_range(run, vcn1, eat)) {
1978 				err = -ENOMEM;
1979 				goto out;
1980 			}
1981 
1982 			if (svcn >= vcn) {
1983 				/* Shift VCN */
1984 				attr->nres.svcn = cpu_to_le64(vcn);
1985 				if (le) {
1986 					le->vcn = attr->nres.svcn;
1987 					ni->attr_list.dirty = true;
1988 				}
1989 			}
1990 
1991 			err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
1992 			if (err)
1993 				goto out;
1994 
1995 			next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1996 			if (next_svcn + eat < evcn1) {
1997 				err = ni_insert_nonresident(
1998 					ni, ATTR_DATA, NULL, 0, run, next_svcn,
1999 					evcn1 - eat - next_svcn, a_flags, &attr,
2000 					&mi, &le);
2001 				if (err)
2002 					goto out;
2003 
2004 				/* Layout of records maybe changed. */
2005 				attr_b = NULL;
2006 			}
2007 
2008 			/* Free all allocated memory. */
2009 			run_truncate(run, 0);
2010 		} else {
2011 			u16 le_sz;
2012 			u16 roff = le16_to_cpu(attr->nres.run_off);
2013 
2014 			if (roff > le32_to_cpu(attr->size)) {
2015 				err = -EINVAL;
2016 				goto out;
2017 			}
2018 
2019 			run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
2020 				      evcn1 - 1, svcn, Add2Ptr(attr, roff),
2021 				      le32_to_cpu(attr->size) - roff);
2022 
2023 			/* Delete this attribute segment. */
2024 			mi_remove_attr(NULL, mi, attr);
2025 			if (!le)
2026 				break;
2027 
2028 			le_sz = le16_to_cpu(le->size);
2029 			if (!al_remove_le(ni, le)) {
2030 				err = -EINVAL;
2031 				goto out;
2032 			}
2033 
2034 			if (evcn1 >= alen)
2035 				break;
2036 
2037 			if (!svcn) {
2038 				/* Load next record that contains this attribute. */
2039 				if (ni_load_mi(ni, le, &mi)) {
2040 					err = -EINVAL;
2041 					goto out;
2042 				}
2043 
2044 				/* Look for required attribute. */
2045 				attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
2046 						    0, &le->id);
2047 				if (!attr) {
2048 					err = -EINVAL;
2049 					goto out;
2050 				}
2051 				goto next_attr;
2052 			}
2053 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2054 		}
2055 
2056 		if (evcn1 >= alen)
2057 			break;
2058 
2059 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2060 		if (!attr) {
2061 			err = -EINVAL;
2062 			goto out;
2063 		}
2064 
2065 next_attr:
2066 		svcn = le64_to_cpu(attr->nres.svcn);
2067 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2068 	}
2069 
2070 	if (!attr_b) {
2071 		le_b = NULL;
2072 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2073 				      &mi_b);
2074 		if (!attr_b) {
2075 			err = -ENOENT;
2076 			goto out;
2077 		}
2078 	}
2079 
2080 	data_size -= bytes;
2081 	valid_size = ni->i_valid;
2082 	if (vbo + bytes <= valid_size)
2083 		valid_size -= bytes;
2084 	else if (vbo < valid_size)
2085 		valid_size = vbo;
2086 
2087 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2088 	attr_b->nres.data_size = cpu_to_le64(data_size);
2089 	attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2090 	total_size -= (u64)dealloc << sbi->cluster_bits;
2091 	if (is_attr_ext(attr_b))
2092 		attr_b->nres.total_size = cpu_to_le64(total_size);
2093 	mi_b->dirty = true;
2094 
2095 	/* Update inode size. */
2096 	ni->i_valid = valid_size;
2097 	i_size_write(&ni->vfs_inode, data_size);
2098 	inode_set_bytes(&ni->vfs_inode, total_size);
2099 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2100 	mark_inode_dirty(&ni->vfs_inode);
2101 
2102 out:
2103 	up_write(&ni->file.run_lock);
2104 	if (err)
2105 		_ntfs_bad_inode(&ni->vfs_inode);
2106 
2107 	return err;
2108 }
2109 
2110 /*
2111  * attr_punch_hole
2112  *
2113  * Not for normal files.
2114  */
2115 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2116 {
2117 	int err = 0;
2118 	struct runs_tree *run = &ni->file.run;
2119 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2120 	struct ATTRIB *attr = NULL, *attr_b;
2121 	struct ATTR_LIST_ENTRY *le, *le_b;
2122 	struct mft_inode *mi, *mi_b;
2123 	CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2124 	u64 total_size, alloc_size;
2125 	u32 mask;
2126 	__le16 a_flags;
2127 	struct runs_tree run2;
2128 
2129 	if (!bytes)
2130 		return 0;
2131 
2132 	le_b = NULL;
2133 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2134 	if (!attr_b)
2135 		return -ENOENT;
2136 
2137 	if (!attr_b->non_res) {
2138 		u32 data_size = le32_to_cpu(attr_b->res.data_size);
2139 		u32 from, to;
2140 
2141 		if (vbo > data_size)
2142 			return 0;
2143 
2144 		from = vbo;
2145 		to = min_t(u64, vbo + bytes, data_size);
2146 		memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2147 		return 0;
2148 	}
2149 
2150 	if (!is_attr_ext(attr_b))
2151 		return -EOPNOTSUPP;
2152 
2153 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2154 	total_size = le64_to_cpu(attr_b->nres.total_size);
2155 
2156 	if (vbo >= alloc_size) {
2157 		/* NOTE: It is allowed. */
2158 		return 0;
2159 	}
2160 
2161 	mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2162 
2163 	bytes += vbo;
2164 	if (bytes > alloc_size)
2165 		bytes = alloc_size;
2166 	bytes -= vbo;
2167 
2168 	if ((vbo & mask) || (bytes & mask)) {
2169 		/* We have to zero a range(s). */
2170 		if (frame_size == NULL) {
2171 			/* Caller insists range is aligned. */
2172 			return -EINVAL;
2173 		}
2174 		*frame_size = mask + 1;
2175 		return E_NTFS_NOTALIGNED;
2176 	}
2177 
2178 	down_write(&ni->file.run_lock);
2179 	run_init(&run2);
2180 	run_truncate(run, 0);
2181 
2182 	/*
2183 	 * Enumerate all attribute segments and punch hole where necessary.
2184 	 */
2185 	alen = alloc_size >> sbi->cluster_bits;
2186 	vcn = vbo >> sbi->cluster_bits;
2187 	len = bytes >> sbi->cluster_bits;
2188 	end = vcn + len;
2189 	hole = 0;
2190 
2191 	svcn = le64_to_cpu(attr_b->nres.svcn);
2192 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2193 	a_flags = attr_b->flags;
2194 
2195 	if (svcn <= vcn && vcn < evcn1) {
2196 		attr = attr_b;
2197 		le = le_b;
2198 		mi = mi_b;
2199 	} else if (!le_b) {
2200 		err = -EINVAL;
2201 		goto bad_inode;
2202 	} else {
2203 		le = le_b;
2204 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2205 				    &mi);
2206 		if (!attr) {
2207 			err = -EINVAL;
2208 			goto bad_inode;
2209 		}
2210 
2211 		svcn = le64_to_cpu(attr->nres.svcn);
2212 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2213 	}
2214 
2215 	while (svcn < end) {
2216 		CLST vcn1, zero, hole2 = hole;
2217 
2218 		err = attr_load_runs(attr, ni, run, &svcn);
2219 		if (err)
2220 			goto done;
2221 		vcn1 = max(vcn, svcn);
2222 		zero = min(end, evcn1) - vcn1;
2223 
2224 		/*
2225 		 * Check range [vcn1 + zero).
2226 		 * Calculate how many clusters there are.
2227 		 * Don't do any destructive actions.
2228 		 */
2229 		err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
2230 		if (err)
2231 			goto done;
2232 
2233 		/* Check if required range is already hole. */
2234 		if (hole2 == hole)
2235 			goto next_attr;
2236 
2237 		/* Make a clone of run to undo. */
2238 		err = run_clone(run, &run2);
2239 		if (err)
2240 			goto done;
2241 
2242 		/* Make a hole range (sparse) [vcn1 + zero). */
2243 		if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2244 			err = -ENOMEM;
2245 			goto done;
2246 		}
2247 
2248 		/* Update run in attribute segment. */
2249 		err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2250 		if (err)
2251 			goto done;
2252 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2253 		if (next_svcn < evcn1) {
2254 			/* Insert new attribute segment. */
2255 			err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2256 						    next_svcn,
2257 						    evcn1 - next_svcn, a_flags,
2258 						    &attr, &mi, &le);
2259 			if (err)
2260 				goto undo_punch;
2261 
2262 			/* Layout of records maybe changed. */
2263 			attr_b = NULL;
2264 		}
2265 
2266 		/* Real deallocate. Should not fail. */
2267 		run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
2268 
2269 next_attr:
2270 		/* Free all allocated memory. */
2271 		run_truncate(run, 0);
2272 
2273 		if (evcn1 >= alen)
2274 			break;
2275 
2276 		/* Get next attribute segment. */
2277 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2278 		if (!attr) {
2279 			err = -EINVAL;
2280 			goto bad_inode;
2281 		}
2282 
2283 		svcn = le64_to_cpu(attr->nres.svcn);
2284 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2285 	}
2286 
2287 done:
2288 	if (!hole)
2289 		goto out;
2290 
2291 	if (!attr_b) {
2292 		attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2293 				      &mi_b);
2294 		if (!attr_b) {
2295 			err = -EINVAL;
2296 			goto bad_inode;
2297 		}
2298 	}
2299 
2300 	total_size -= (u64)hole << sbi->cluster_bits;
2301 	attr_b->nres.total_size = cpu_to_le64(total_size);
2302 	mi_b->dirty = true;
2303 
2304 	/* Update inode size. */
2305 	inode_set_bytes(&ni->vfs_inode, total_size);
2306 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2307 	mark_inode_dirty(&ni->vfs_inode);
2308 
2309 out:
2310 	run_close(&run2);
2311 	up_write(&ni->file.run_lock);
2312 	return err;
2313 
2314 bad_inode:
2315 	_ntfs_bad_inode(&ni->vfs_inode);
2316 	goto out;
2317 
2318 undo_punch:
2319 	/*
2320 	 * Restore packed runs.
2321 	 * 'mi_pack_runs' should not fail, cause we restore original.
2322 	 */
2323 	if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
2324 		goto bad_inode;
2325 
2326 	goto done;
2327 }
2328 
2329 /*
2330  * attr_insert_range - Insert range (hole) in file.
2331  * Not for normal files.
2332  */
2333 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2334 {
2335 	int err = 0;
2336 	struct runs_tree *run = &ni->file.run;
2337 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2338 	struct ATTRIB *attr = NULL, *attr_b;
2339 	struct ATTR_LIST_ENTRY *le, *le_b;
2340 	struct mft_inode *mi, *mi_b;
2341 	CLST vcn, svcn, evcn1, len, next_svcn;
2342 	u64 data_size, alloc_size;
2343 	u32 mask;
2344 	__le16 a_flags;
2345 
2346 	if (!bytes)
2347 		return 0;
2348 
2349 	le_b = NULL;
2350 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2351 	if (!attr_b)
2352 		return -ENOENT;
2353 
2354 	if (!is_attr_ext(attr_b)) {
2355 		/* It was checked above. See fallocate. */
2356 		return -EOPNOTSUPP;
2357 	}
2358 
2359 	if (!attr_b->non_res) {
2360 		data_size = le32_to_cpu(attr_b->res.data_size);
2361 		alloc_size = data_size;
2362 		mask = sbi->cluster_mask; /* cluster_size - 1 */
2363 	} else {
2364 		data_size = le64_to_cpu(attr_b->nres.data_size);
2365 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2366 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2367 	}
2368 
2369 	if (vbo >= data_size) {
2370 		/*
2371 		 * Insert range after the file size is not allowed.
2372 		 * If the offset is equal to or greater than the end of
2373 		 * file, an error is returned.  For such operations (i.e., inserting
2374 		 * a hole at the end of file), ftruncate(2) should be used.
2375 		 */
2376 		return -EINVAL;
2377 	}
2378 
2379 	if ((vbo & mask) || (bytes & mask)) {
2380 		/* Allow to insert only frame aligned ranges. */
2381 		return -EINVAL;
2382 	}
2383 
2384 	/*
2385 	 * valid_size <= data_size <= alloc_size
2386 	 * Check alloc_size for maximum possible.
2387 	 */
2388 	if (bytes > sbi->maxbytes_sparse - alloc_size)
2389 		return -EFBIG;
2390 
2391 	vcn = vbo >> sbi->cluster_bits;
2392 	len = bytes >> sbi->cluster_bits;
2393 
2394 	down_write(&ni->file.run_lock);
2395 
2396 	if (!attr_b->non_res) {
2397 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
2398 				    data_size + bytes, NULL, false, NULL);
2399 
2400 		le_b = NULL;
2401 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2402 				      &mi_b);
2403 		if (!attr_b) {
2404 			err = -EINVAL;
2405 			goto bad_inode;
2406 		}
2407 
2408 		if (err)
2409 			goto out;
2410 
2411 		if (!attr_b->non_res) {
2412 			/* Still resident. */
2413 			char *data = Add2Ptr(attr_b,
2414 					     le16_to_cpu(attr_b->res.data_off));
2415 
2416 			memmove(data + bytes, data, bytes);
2417 			memset(data, 0, bytes);
2418 			goto done;
2419 		}
2420 
2421 		/* Resident files becomes nonresident. */
2422 		data_size = le64_to_cpu(attr_b->nres.data_size);
2423 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2424 	}
2425 
2426 	/*
2427 	 * Enumerate all attribute segments and shift start vcn.
2428 	 */
2429 	a_flags = attr_b->flags;
2430 	svcn = le64_to_cpu(attr_b->nres.svcn);
2431 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2432 
2433 	if (svcn <= vcn && vcn < evcn1) {
2434 		attr = attr_b;
2435 		le = le_b;
2436 		mi = mi_b;
2437 	} else if (!le_b) {
2438 		err = -EINVAL;
2439 		goto bad_inode;
2440 	} else {
2441 		le = le_b;
2442 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2443 				    &mi);
2444 		if (!attr) {
2445 			err = -EINVAL;
2446 			goto bad_inode;
2447 		}
2448 
2449 		svcn = le64_to_cpu(attr->nres.svcn);
2450 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2451 	}
2452 
2453 	run_truncate(run, 0); /* clear cached values. */
2454 	err = attr_load_runs(attr, ni, run, NULL);
2455 	if (err)
2456 		goto out;
2457 
2458 	if (!run_insert_range(run, vcn, len)) {
2459 		err = -ENOMEM;
2460 		goto out;
2461 	}
2462 
2463 	/* Try to pack in current record as much as possible. */
2464 	err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2465 	if (err)
2466 		goto out;
2467 
2468 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2469 
2470 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2471 	       attr->type == ATTR_DATA && !attr->name_len) {
2472 		le64_add_cpu(&attr->nres.svcn, len);
2473 		le64_add_cpu(&attr->nres.evcn, len);
2474 		if (le) {
2475 			le->vcn = attr->nres.svcn;
2476 			ni->attr_list.dirty = true;
2477 		}
2478 		mi->dirty = true;
2479 	}
2480 
2481 	if (next_svcn < evcn1 + len) {
2482 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2483 					    next_svcn, evcn1 + len - next_svcn,
2484 					    a_flags, NULL, NULL, NULL);
2485 
2486 		le_b = NULL;
2487 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2488 				      &mi_b);
2489 		if (!attr_b) {
2490 			err = -EINVAL;
2491 			goto bad_inode;
2492 		}
2493 
2494 		if (err) {
2495 			/* ni_insert_nonresident failed. Try to undo. */
2496 			goto undo_insert_range;
2497 		}
2498 	}
2499 
2500 	/*
2501 	 * Update primary attribute segment.
2502 	 */
2503 	if (vbo <= ni->i_valid)
2504 		ni->i_valid += bytes;
2505 
2506 	attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
2507 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
2508 
2509 	/* ni->valid may be not equal valid_size (temporary). */
2510 	if (ni->i_valid > data_size + bytes)
2511 		attr_b->nres.valid_size = attr_b->nres.data_size;
2512 	else
2513 		attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2514 	mi_b->dirty = true;
2515 
2516 done:
2517 	i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes);
2518 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2519 	mark_inode_dirty(&ni->vfs_inode);
2520 
2521 out:
2522 	run_truncate(run, 0); /* clear cached values. */
2523 
2524 	up_write(&ni->file.run_lock);
2525 
2526 	return err;
2527 
2528 bad_inode:
2529 	_ntfs_bad_inode(&ni->vfs_inode);
2530 	goto out;
2531 
2532 undo_insert_range:
2533 	svcn = le64_to_cpu(attr_b->nres.svcn);
2534 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2535 
2536 	if (svcn <= vcn && vcn < evcn1) {
2537 		attr = attr_b;
2538 		le = le_b;
2539 		mi = mi_b;
2540 	} else if (!le_b) {
2541 		goto bad_inode;
2542 	} else {
2543 		le = le_b;
2544 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2545 				    &mi);
2546 		if (!attr) {
2547 			goto bad_inode;
2548 		}
2549 
2550 		svcn = le64_to_cpu(attr->nres.svcn);
2551 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2552 	}
2553 
2554 	if (attr_load_runs(attr, ni, run, NULL))
2555 		goto bad_inode;
2556 
2557 	if (!run_collapse_range(run, vcn, len))
2558 		goto bad_inode;
2559 
2560 	if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
2561 		goto bad_inode;
2562 
2563 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2564 	       attr->type == ATTR_DATA && !attr->name_len) {
2565 		le64_sub_cpu(&attr->nres.svcn, len);
2566 		le64_sub_cpu(&attr->nres.evcn, len);
2567 		if (le) {
2568 			le->vcn = attr->nres.svcn;
2569 			ni->attr_list.dirty = true;
2570 		}
2571 		mi->dirty = true;
2572 	}
2573 
2574 	goto out;
2575 }
2576 
2577 /*
2578  * attr_force_nonresident
2579  *
2580  * Convert default data attribute into non resident form.
2581  */
2582 int attr_force_nonresident(struct ntfs_inode *ni)
2583 {
2584 	int err;
2585 	struct ATTRIB *attr;
2586 	struct ATTR_LIST_ENTRY *le = NULL;
2587 	struct mft_inode *mi;
2588 
2589 	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2590 	if (!attr) {
2591 		ntfs_bad_inode(&ni->vfs_inode, "no data attribute");
2592 		return -ENOENT;
2593 	}
2594 
2595 	if (attr->non_res) {
2596 		/* Already non resident. */
2597 		return 0;
2598 	}
2599 
2600 	down_write(&ni->file.run_lock);
2601 	err = attr_make_nonresident(ni, attr, le, mi,
2602 				    le32_to_cpu(attr->res.data_size),
2603 				    &ni->file.run, &attr, NULL);
2604 	up_write(&ni->file.run_lock);
2605 
2606 	return err;
2607 }
2608 
2609 /*
2610  * Change the compression of data attribute
2611  */
2612 int attr_set_compress(struct ntfs_inode *ni, bool compr)
2613 {
2614 	struct ATTRIB *attr;
2615 	struct mft_inode *mi;
2616 
2617 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2618 	if (!attr)
2619 		return -ENOENT;
2620 
2621 	if (is_attr_compressed(attr) == !!compr) {
2622 		/* Already required compressed state. */
2623 		return 0;
2624 	}
2625 
2626 	if (attr->non_res) {
2627 		u16 run_off;
2628 		u32 run_size;
2629 		char *run;
2630 
2631 		if (attr->nres.data_size) {
2632 			/*
2633 			 * There are rare cases when it possible to change
2634 			 * compress state without big changes.
2635 			 * TODO: Process these cases.
2636 			 */
2637 			return -EOPNOTSUPP;
2638 		}
2639 
2640 		run_off = le16_to_cpu(attr->nres.run_off);
2641 		run_size = le32_to_cpu(attr->size) - run_off;
2642 		run = Add2Ptr(attr, run_off);
2643 
2644 		if (!compr) {
2645 			/* remove field 'attr->nres.total_size'. */
2646 			memmove(run - 8, run, run_size);
2647 			run_off -= 8;
2648 		}
2649 
2650 		if (!mi_resize_attr(mi, attr, compr ? +8 : -8)) {
2651 			/*
2652 			 * Ignore rare case when there are no 8 bytes in record with attr.
2653 			 * TODO: split attribute.
2654 			 */
2655 			return -EOPNOTSUPP;
2656 		}
2657 
2658 		if (compr) {
2659 			/* Make a gap for 'attr->nres.total_size'. */
2660 			memmove(run + 8, run, run_size);
2661 			run_off += 8;
2662 			attr->nres.total_size = attr->nres.alloc_size;
2663 		}
2664 		attr->nres.run_off = cpu_to_le16(run_off);
2665 	}
2666 
2667 	/* Update data attribute flags. */
2668 	if (compr) {
2669 		attr->flags |= ATTR_FLAG_COMPRESSED;
2670 		attr->nres.c_unit = NTFS_LZNT_CUNIT;
2671 	} else {
2672 		attr->flags &= ~ATTR_FLAG_COMPRESSED;
2673 		attr->nres.c_unit = 0;
2674 	}
2675 	mi->dirty = true;
2676 
2677 	return 0;
2678 }
2679