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