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