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