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