xref: /linux/fs/ntfs3/attrib.c (revision 40bb3c59)
1be71b5cbSKonstantin Komarov // SPDX-License-Identifier: GPL-2.0
2be71b5cbSKonstantin Komarov /*
3be71b5cbSKonstantin Komarov  *
4be71b5cbSKonstantin Komarov  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5be71b5cbSKonstantin Komarov  *
6e8b8e97fSKari Argillander  * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
7be71b5cbSKonstantin Komarov  */
8be71b5cbSKonstantin Komarov 
9be71b5cbSKonstantin Komarov #include <linux/fs.h>
10be71b5cbSKonstantin Komarov #include <linux/slab.h>
116e3331eeSKari Argillander #include <linux/kernel.h>
12be71b5cbSKonstantin Komarov 
13be71b5cbSKonstantin Komarov #include "debug.h"
14be71b5cbSKonstantin Komarov #include "ntfs.h"
15be71b5cbSKonstantin Komarov #include "ntfs_fs.h"
16be71b5cbSKonstantin Komarov 
17be71b5cbSKonstantin Komarov /*
18be71b5cbSKonstantin Komarov  * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage
19e8b8e97fSKari Argillander  * preallocate algorithm.
20be71b5cbSKonstantin Komarov  */
21be71b5cbSKonstantin Komarov #ifndef NTFS_MIN_LOG2_OF_CLUMP
22be71b5cbSKonstantin Komarov #define NTFS_MIN_LOG2_OF_CLUMP 16
23be71b5cbSKonstantin Komarov #endif
24be71b5cbSKonstantin Komarov 
25be71b5cbSKonstantin Komarov #ifndef NTFS_MAX_LOG2_OF_CLUMP
26be71b5cbSKonstantin Komarov #define NTFS_MAX_LOG2_OF_CLUMP 26
27be71b5cbSKonstantin Komarov #endif
28be71b5cbSKonstantin Komarov 
29be71b5cbSKonstantin Komarov // 16M
30be71b5cbSKonstantin Komarov #define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8))
31be71b5cbSKonstantin Komarov // 16G
32be71b5cbSKonstantin Komarov #define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8))
33be71b5cbSKonstantin Komarov 
get_pre_allocated(u64 size)34be71b5cbSKonstantin Komarov static inline u64 get_pre_allocated(u64 size)
35be71b5cbSKonstantin Komarov {
36be71b5cbSKonstantin Komarov 	u32 clump;
37be71b5cbSKonstantin Komarov 	u8 align_shift;
38be71b5cbSKonstantin Komarov 	u64 ret;
39be71b5cbSKonstantin Komarov 
40be71b5cbSKonstantin Komarov 	if (size <= NTFS_CLUMP_MIN) {
41be71b5cbSKonstantin Komarov 		clump = 1 << NTFS_MIN_LOG2_OF_CLUMP;
42be71b5cbSKonstantin Komarov 		align_shift = NTFS_MIN_LOG2_OF_CLUMP;
43be71b5cbSKonstantin Komarov 	} else if (size >= NTFS_CLUMP_MAX) {
44be71b5cbSKonstantin Komarov 		clump = 1 << NTFS_MAX_LOG2_OF_CLUMP;
45be71b5cbSKonstantin Komarov 		align_shift = NTFS_MAX_LOG2_OF_CLUMP;
46be71b5cbSKonstantin Komarov 	} else {
47be71b5cbSKonstantin Komarov 		align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 +
48be71b5cbSKonstantin Komarov 			      __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP));
49be71b5cbSKonstantin Komarov 		clump = 1u << align_shift;
50be71b5cbSKonstantin Komarov 	}
51be71b5cbSKonstantin Komarov 
52be71b5cbSKonstantin Komarov 	ret = (((size + clump - 1) >> align_shift)) << align_shift;
53be71b5cbSKonstantin Komarov 
54be71b5cbSKonstantin Komarov 	return ret;
55be71b5cbSKonstantin Komarov }
56be71b5cbSKonstantin Komarov 
57be71b5cbSKonstantin Komarov /*
58e8b8e97fSKari Argillander  * attr_load_runs - Load all runs stored in @attr.
59be71b5cbSKonstantin Komarov  */
attr_load_runs(struct ATTRIB * attr,struct ntfs_inode * ni,struct runs_tree * run,const CLST * vcn)60cf760ec0SKonstantin Komarov static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
61be71b5cbSKonstantin Komarov 			  struct runs_tree *run, const CLST *vcn)
62be71b5cbSKonstantin Komarov {
63be71b5cbSKonstantin Komarov 	int err;
64be71b5cbSKonstantin Komarov 	CLST svcn = le64_to_cpu(attr->nres.svcn);
65be71b5cbSKonstantin Komarov 	CLST evcn = le64_to_cpu(attr->nres.evcn);
66be71b5cbSKonstantin Komarov 	u32 asize;
67be71b5cbSKonstantin Komarov 	u16 run_off;
68be71b5cbSKonstantin Komarov 
69be71b5cbSKonstantin Komarov 	if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
70be71b5cbSKonstantin Komarov 		return 0;
71be71b5cbSKonstantin Komarov 
72be71b5cbSKonstantin Komarov 	if (vcn && (evcn < *vcn || *vcn < svcn))
73be71b5cbSKonstantin Komarov 		return -EINVAL;
74be71b5cbSKonstantin Komarov 
75be71b5cbSKonstantin Komarov 	asize = le32_to_cpu(attr->size);
76be71b5cbSKonstantin Komarov 	run_off = le16_to_cpu(attr->nres.run_off);
776db62086SEdward Lo 
786db62086SEdward Lo 	if (run_off > asize)
796db62086SEdward Lo 		return -EINVAL;
806db62086SEdward Lo 
81be71b5cbSKonstantin Komarov 	err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
82be71b5cbSKonstantin Komarov 			    vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
83be71b5cbSKonstantin Komarov 			    asize - run_off);
84be71b5cbSKonstantin Komarov 	if (err < 0)
85be71b5cbSKonstantin Komarov 		return err;
86be71b5cbSKonstantin Komarov 
87be71b5cbSKonstantin Komarov 	return 0;
88be71b5cbSKonstantin Komarov }
89be71b5cbSKonstantin Komarov 
90be71b5cbSKonstantin Komarov /*
91e8b8e97fSKari Argillander  * run_deallocate_ex - Deallocate clusters.
92be71b5cbSKonstantin Komarov  */
run_deallocate_ex(struct ntfs_sb_info * sbi,struct runs_tree * run,CLST vcn,CLST len,CLST * done,bool trim)93be71b5cbSKonstantin Komarov static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
94be71b5cbSKonstantin Komarov 			     CLST vcn, CLST len, CLST *done, bool trim)
95be71b5cbSKonstantin Komarov {
96be71b5cbSKonstantin Komarov 	int err = 0;
97be71b5cbSKonstantin Komarov 	CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
98be71b5cbSKonstantin Komarov 	size_t idx;
99be71b5cbSKonstantin Komarov 
100be71b5cbSKonstantin Komarov 	if (!len)
101be71b5cbSKonstantin Komarov 		goto out;
102be71b5cbSKonstantin Komarov 
103be71b5cbSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
104be71b5cbSKonstantin Komarov failed:
105be71b5cbSKonstantin Komarov 		run_truncate(run, vcn0);
106be71b5cbSKonstantin Komarov 		err = -EINVAL;
107be71b5cbSKonstantin Komarov 		goto out;
108be71b5cbSKonstantin Komarov 	}
109be71b5cbSKonstantin Komarov 
110be71b5cbSKonstantin Komarov 	for (;;) {
111be71b5cbSKonstantin Komarov 		if (clen > len)
112be71b5cbSKonstantin Komarov 			clen = len;
113be71b5cbSKonstantin Komarov 
114be71b5cbSKonstantin Komarov 		if (!clen) {
115be71b5cbSKonstantin Komarov 			err = -EINVAL;
116be71b5cbSKonstantin Komarov 			goto out;
117be71b5cbSKonstantin Komarov 		}
118be71b5cbSKonstantin Komarov 
119be71b5cbSKonstantin Komarov 		if (lcn != SPARSE_LCN) {
12020abc64fSKonstantin Komarov 			if (sbi) {
12120abc64fSKonstantin Komarov 				/* mark bitmap range [lcn + clen) as free and trim clusters. */
122be71b5cbSKonstantin Komarov 				mark_as_free_ex(sbi, lcn, clen, trim);
12320abc64fSKonstantin Komarov 			}
124be71b5cbSKonstantin Komarov 			dn += clen;
125be71b5cbSKonstantin Komarov 		}
126be71b5cbSKonstantin Komarov 
127be71b5cbSKonstantin Komarov 		len -= clen;
128be71b5cbSKonstantin Komarov 		if (!len)
129be71b5cbSKonstantin Komarov 			break;
130be71b5cbSKonstantin Komarov 
131be71b5cbSKonstantin Komarov 		vcn_next = vcn + clen;
132be71b5cbSKonstantin Komarov 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
133be71b5cbSKonstantin Komarov 		    vcn != vcn_next) {
134e8b8e97fSKari Argillander 			/* Save memory - don't load entire run. */
135be71b5cbSKonstantin Komarov 			goto failed;
136be71b5cbSKonstantin Komarov 		}
137be71b5cbSKonstantin Komarov 	}
138be71b5cbSKonstantin Komarov 
139be71b5cbSKonstantin Komarov out:
140be71b5cbSKonstantin Komarov 	if (done)
141be71b5cbSKonstantin Komarov 		*done += dn;
142be71b5cbSKonstantin Komarov 
143be71b5cbSKonstantin Komarov 	return err;
144be71b5cbSKonstantin Komarov }
145be71b5cbSKonstantin Komarov 
146be71b5cbSKonstantin Komarov /*
147e8b8e97fSKari Argillander  * attr_allocate_clusters - Find free space, mark it as used and store in @run.
148be71b5cbSKonstantin Komarov  */
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)149be71b5cbSKonstantin Komarov int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
150be71b5cbSKonstantin Komarov 			   CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
151be71b5cbSKonstantin Komarov 			   enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
152c380b52fSKonstantin Komarov 			   CLST *new_lcn, CLST *new_len)
153be71b5cbSKonstantin Komarov {
154be71b5cbSKonstantin Komarov 	int err;
155be71b5cbSKonstantin Komarov 	CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
156be71b5cbSKonstantin Komarov 	size_t cnt = run->count;
157be71b5cbSKonstantin Komarov 
158be71b5cbSKonstantin Komarov 	for (;;) {
159be71b5cbSKonstantin Komarov 		err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
160be71b5cbSKonstantin Komarov 					       opt);
161be71b5cbSKonstantin Komarov 
162be71b5cbSKonstantin Komarov 		if (err == -ENOSPC && pre) {
163be71b5cbSKonstantin Komarov 			pre = 0;
164be71b5cbSKonstantin Komarov 			if (*pre_alloc)
165be71b5cbSKonstantin Komarov 				*pre_alloc = 0;
166be71b5cbSKonstantin Komarov 			continue;
167be71b5cbSKonstantin Komarov 		}
168be71b5cbSKonstantin Komarov 
169be71b5cbSKonstantin Komarov 		if (err)
170be71b5cbSKonstantin Komarov 			goto out;
171be71b5cbSKonstantin Komarov 
172c380b52fSKonstantin Komarov 		if (vcn == vcn0) {
173c380b52fSKonstantin Komarov 			/* Return the first fragment. */
174c380b52fSKonstantin Komarov 			if (new_lcn)
175be71b5cbSKonstantin Komarov 				*new_lcn = lcn;
176c380b52fSKonstantin Komarov 			if (new_len)
177c380b52fSKonstantin Komarov 				*new_len = flen;
178c380b52fSKonstantin Komarov 		}
179be71b5cbSKonstantin Komarov 
180e8b8e97fSKari Argillander 		/* Add new fragment into run storage. */
181c380b52fSKonstantin Komarov 		if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) {
182d3624466SKonstantin Komarov 			/* Undo last 'ntfs_look_for_free_space' */
1830e5b044cSKonstantin Komarov 			mark_as_free_ex(sbi, lcn, len, false);
184be71b5cbSKonstantin Komarov 			err = -ENOMEM;
185be71b5cbSKonstantin Komarov 			goto out;
186be71b5cbSKonstantin Komarov 		}
187be71b5cbSKonstantin Komarov 
188c380b52fSKonstantin Komarov 		if (opt & ALLOCATE_ZERO) {
189c380b52fSKonstantin Komarov 			u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
190c380b52fSKonstantin Komarov 
191c380b52fSKonstantin Komarov 			err = blkdev_issue_zeroout(sbi->sb->s_bdev,
192c380b52fSKonstantin Komarov 						   (sector_t)lcn << shift,
193c380b52fSKonstantin Komarov 						   (sector_t)flen << shift,
194c380b52fSKonstantin Komarov 						   GFP_NOFS, 0);
195c380b52fSKonstantin Komarov 			if (err)
196c380b52fSKonstantin Komarov 				goto out;
197c380b52fSKonstantin Komarov 		}
198c380b52fSKonstantin Komarov 
199be71b5cbSKonstantin Komarov 		vcn += flen;
200be71b5cbSKonstantin Komarov 
201c380b52fSKonstantin Komarov 		if (flen >= len || (opt & ALLOCATE_MFT) ||
202be71b5cbSKonstantin Komarov 		    (fr && run->count - cnt >= fr)) {
203be71b5cbSKonstantin Komarov 			*alen = vcn - vcn0;
204be71b5cbSKonstantin Komarov 			return 0;
205be71b5cbSKonstantin Komarov 		}
206be71b5cbSKonstantin Komarov 
207be71b5cbSKonstantin Komarov 		len -= flen;
208be71b5cbSKonstantin Komarov 	}
209be71b5cbSKonstantin Komarov 
210be71b5cbSKonstantin Komarov out:
21178ab59feSKonstantin Komarov 	/* Undo 'ntfs_look_for_free_space' */
21278ab59feSKonstantin Komarov 	if (vcn - vcn0) {
213be71b5cbSKonstantin Komarov 		run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
214be71b5cbSKonstantin Komarov 		run_truncate(run, vcn0);
21578ab59feSKonstantin Komarov 	}
216be71b5cbSKonstantin Komarov 
217be71b5cbSKonstantin Komarov 	return err;
218be71b5cbSKonstantin Komarov }
219be71b5cbSKonstantin Komarov 
220be71b5cbSKonstantin Komarov /*
221e8b8e97fSKari Argillander  * attr_make_nonresident
222e8b8e97fSKari Argillander  *
223e8b8e97fSKari Argillander  * If page is not NULL - it is already contains resident data
224e8b8e97fSKari Argillander  * and locked (called from ni_write_frame()).
225be71b5cbSKonstantin Komarov  */
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)226be71b5cbSKonstantin Komarov int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
227be71b5cbSKonstantin Komarov 			  struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
228be71b5cbSKonstantin Komarov 			  u64 new_size, struct runs_tree *run,
229be71b5cbSKonstantin Komarov 			  struct ATTRIB **ins_attr, struct page *page)
230be71b5cbSKonstantin Komarov {
231be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi;
232be71b5cbSKonstantin Komarov 	struct ATTRIB *attr_s;
233be71b5cbSKonstantin Komarov 	struct MFT_REC *rec;
234be71b5cbSKonstantin Komarov 	u32 used, asize, rsize, aoff, align;
235be71b5cbSKonstantin Komarov 	bool is_data;
236be71b5cbSKonstantin Komarov 	CLST len, alen;
237be71b5cbSKonstantin Komarov 	char *next;
238be71b5cbSKonstantin Komarov 	int err;
239be71b5cbSKonstantin Komarov 
240be71b5cbSKonstantin Komarov 	if (attr->non_res) {
241be71b5cbSKonstantin Komarov 		*ins_attr = attr;
242be71b5cbSKonstantin Komarov 		return 0;
243be71b5cbSKonstantin Komarov 	}
244be71b5cbSKonstantin Komarov 
245be71b5cbSKonstantin Komarov 	sbi = mi->sbi;
246be71b5cbSKonstantin Komarov 	rec = mi->mrec;
247be71b5cbSKonstantin Komarov 	attr_s = NULL;
248be71b5cbSKonstantin Komarov 	used = le32_to_cpu(rec->used);
249be71b5cbSKonstantin Komarov 	asize = le32_to_cpu(attr->size);
250be71b5cbSKonstantin Komarov 	next = Add2Ptr(attr, asize);
251be71b5cbSKonstantin Komarov 	aoff = PtrOffset(rec, attr);
252be71b5cbSKonstantin Komarov 	rsize = le32_to_cpu(attr->res.data_size);
253be71b5cbSKonstantin Komarov 	is_data = attr->type == ATTR_DATA && !attr->name_len;
254be71b5cbSKonstantin Komarov 
255be71b5cbSKonstantin Komarov 	align = sbi->cluster_size;
256be71b5cbSKonstantin Komarov 	if (is_attr_compressed(attr))
257be71b5cbSKonstantin Komarov 		align <<= COMPRESSION_UNIT;
258be71b5cbSKonstantin Komarov 	len = (rsize + align - 1) >> sbi->cluster_bits;
259be71b5cbSKonstantin Komarov 
260be71b5cbSKonstantin Komarov 	run_init(run);
261be71b5cbSKonstantin Komarov 
262e8b8e97fSKari Argillander 	/* Make a copy of original attribute. */
263195c52bdSKari Argillander 	attr_s = kmemdup(attr, asize, GFP_NOFS);
264be71b5cbSKonstantin Komarov 	if (!attr_s) {
265be71b5cbSKonstantin Komarov 		err = -ENOMEM;
266be71b5cbSKonstantin Komarov 		goto out;
267be71b5cbSKonstantin Komarov 	}
268be71b5cbSKonstantin Komarov 
269be71b5cbSKonstantin Komarov 	if (!len) {
270e8b8e97fSKari Argillander 		/* Empty resident -> Empty nonresident. */
271be71b5cbSKonstantin Komarov 		alen = 0;
272be71b5cbSKonstantin Komarov 	} else {
273be71b5cbSKonstantin Komarov 		const char *data = resident_data(attr);
274be71b5cbSKonstantin Komarov 
275be71b5cbSKonstantin Komarov 		err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
276c380b52fSKonstantin Komarov 					     ALLOCATE_DEF, &alen, 0, NULL,
277c380b52fSKonstantin Komarov 					     NULL);
278be71b5cbSKonstantin Komarov 		if (err)
279be71b5cbSKonstantin Komarov 			goto out1;
280be71b5cbSKonstantin Komarov 
281be71b5cbSKonstantin Komarov 		if (!rsize) {
282e8b8e97fSKari Argillander 			/* Empty resident -> Non empty nonresident. */
283be71b5cbSKonstantin Komarov 		} else if (!is_data) {
28463544672SKonstantin Komarov 			err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
285be71b5cbSKonstantin Komarov 			if (err)
286be71b5cbSKonstantin Komarov 				goto out2;
287be71b5cbSKonstantin Komarov 		} else if (!page) {
288be71b5cbSKonstantin Komarov 			char *kaddr;
289be71b5cbSKonstantin Komarov 
290be71b5cbSKonstantin Komarov 			page = grab_cache_page(ni->vfs_inode.i_mapping, 0);
291be71b5cbSKonstantin Komarov 			if (!page) {
292be71b5cbSKonstantin Komarov 				err = -ENOMEM;
293be71b5cbSKonstantin Komarov 				goto out2;
294be71b5cbSKonstantin Komarov 			}
295be71b5cbSKonstantin Komarov 			kaddr = kmap_atomic(page);
296be71b5cbSKonstantin Komarov 			memcpy(kaddr, data, rsize);
297be71b5cbSKonstantin Komarov 			memset(kaddr + rsize, 0, PAGE_SIZE - rsize);
298be71b5cbSKonstantin Komarov 			kunmap_atomic(kaddr);
299be71b5cbSKonstantin Komarov 			flush_dcache_page(page);
300be71b5cbSKonstantin Komarov 			SetPageUptodate(page);
301be71b5cbSKonstantin Komarov 			set_page_dirty(page);
302be71b5cbSKonstantin Komarov 			unlock_page(page);
303be71b5cbSKonstantin Komarov 			put_page(page);
304be71b5cbSKonstantin Komarov 		}
305be71b5cbSKonstantin Komarov 	}
306be71b5cbSKonstantin Komarov 
307e8b8e97fSKari Argillander 	/* Remove original attribute. */
308be71b5cbSKonstantin Komarov 	used -= asize;
309be71b5cbSKonstantin Komarov 	memmove(attr, Add2Ptr(attr, asize), used - aoff);
310be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used);
311be71b5cbSKonstantin Komarov 	mi->dirty = true;
312be71b5cbSKonstantin Komarov 	if (le)
313be71b5cbSKonstantin Komarov 		al_remove_le(ni, le);
314be71b5cbSKonstantin Komarov 
315be71b5cbSKonstantin Komarov 	err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
316be71b5cbSKonstantin Komarov 				    attr_s->name_len, run, 0, alen,
317c1e0ab37SKonstantin Komarov 				    attr_s->flags, &attr, NULL, NULL);
318be71b5cbSKonstantin Komarov 	if (err)
319be71b5cbSKonstantin Komarov 		goto out3;
320be71b5cbSKonstantin Komarov 
321195c52bdSKari Argillander 	kfree(attr_s);
322be71b5cbSKonstantin Komarov 	attr->nres.data_size = cpu_to_le64(rsize);
323be71b5cbSKonstantin Komarov 	attr->nres.valid_size = attr->nres.data_size;
324be71b5cbSKonstantin Komarov 
325be71b5cbSKonstantin Komarov 	*ins_attr = attr;
326be71b5cbSKonstantin Komarov 
327be71b5cbSKonstantin Komarov 	if (is_data)
328be71b5cbSKonstantin Komarov 		ni->ni_flags &= ~NI_FLAG_RESIDENT;
329be71b5cbSKonstantin Komarov 
330e8b8e97fSKari Argillander 	/* Resident attribute becomes non resident. */
331be71b5cbSKonstantin Komarov 	return 0;
332be71b5cbSKonstantin Komarov 
333be71b5cbSKonstantin Komarov out3:
334be71b5cbSKonstantin Komarov 	attr = Add2Ptr(rec, aoff);
335be71b5cbSKonstantin Komarov 	memmove(next, attr, used - aoff);
336be71b5cbSKonstantin Komarov 	memcpy(attr, attr_s, asize);
337be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used + asize);
338be71b5cbSKonstantin Komarov 	mi->dirty = true;
339be71b5cbSKonstantin Komarov out2:
340e8b8e97fSKari Argillander 	/* Undo: do not trim new allocated clusters. */
341be71b5cbSKonstantin Komarov 	run_deallocate(sbi, run, false);
342be71b5cbSKonstantin Komarov 	run_close(run);
343be71b5cbSKonstantin Komarov out1:
344195c52bdSKari Argillander 	kfree(attr_s);
345be71b5cbSKonstantin Komarov out:
346be71b5cbSKonstantin Komarov 	return err;
347be71b5cbSKonstantin Komarov }
348be71b5cbSKonstantin Komarov 
349be71b5cbSKonstantin Komarov /*
350e8b8e97fSKari Argillander  * attr_set_size_res - Helper for attr_set_size().
351be71b5cbSKonstantin Komarov  */
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)352be71b5cbSKonstantin Komarov static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
353be71b5cbSKonstantin Komarov 			     struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
354be71b5cbSKonstantin Komarov 			     u64 new_size, struct runs_tree *run,
355be71b5cbSKonstantin Komarov 			     struct ATTRIB **ins_attr)
356be71b5cbSKonstantin Komarov {
357be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = mi->sbi;
358be71b5cbSKonstantin Komarov 	struct MFT_REC *rec = mi->mrec;
359be71b5cbSKonstantin Komarov 	u32 used = le32_to_cpu(rec->used);
360be71b5cbSKonstantin Komarov 	u32 asize = le32_to_cpu(attr->size);
361be71b5cbSKonstantin Komarov 	u32 aoff = PtrOffset(rec, attr);
362be71b5cbSKonstantin Komarov 	u32 rsize = le32_to_cpu(attr->res.data_size);
363be71b5cbSKonstantin Komarov 	u32 tail = used - aoff - asize;
364be71b5cbSKonstantin Komarov 	char *next = Add2Ptr(attr, asize);
365fa3cacf5SKari Argillander 	s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
366be71b5cbSKonstantin Komarov 
367be71b5cbSKonstantin Komarov 	if (dsize < 0) {
368be71b5cbSKonstantin Komarov 		memmove(next + dsize, next, tail);
369be71b5cbSKonstantin Komarov 	} else if (dsize > 0) {
370be71b5cbSKonstantin Komarov 		if (used + dsize > sbi->max_bytes_per_attr)
371be71b5cbSKonstantin Komarov 			return attr_make_nonresident(ni, attr, le, mi, new_size,
372be71b5cbSKonstantin Komarov 						     run, ins_attr, NULL);
373be71b5cbSKonstantin Komarov 
374be71b5cbSKonstantin Komarov 		memmove(next + dsize, next, tail);
375be71b5cbSKonstantin Komarov 		memset(next, 0, dsize);
376be71b5cbSKonstantin Komarov 	}
377be71b5cbSKonstantin Komarov 
378be71b5cbSKonstantin Komarov 	if (new_size > rsize)
379be71b5cbSKonstantin Komarov 		memset(Add2Ptr(resident_data(attr), rsize), 0,
380be71b5cbSKonstantin Komarov 		       new_size - rsize);
381be71b5cbSKonstantin Komarov 
382be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used + dsize);
383be71b5cbSKonstantin Komarov 	attr->size = cpu_to_le32(asize + dsize);
384be71b5cbSKonstantin Komarov 	attr->res.data_size = cpu_to_le32(new_size);
385be71b5cbSKonstantin Komarov 	mi->dirty = true;
386be71b5cbSKonstantin Komarov 	*ins_attr = attr;
387be71b5cbSKonstantin Komarov 
388be71b5cbSKonstantin Komarov 	return 0;
389be71b5cbSKonstantin Komarov }
390be71b5cbSKonstantin Komarov 
391be71b5cbSKonstantin Komarov /*
392e8b8e97fSKari Argillander  * attr_set_size - Change the size of attribute.
393be71b5cbSKonstantin Komarov  *
394be71b5cbSKonstantin Komarov  * Extend:
395e8b8e97fSKari Argillander  *   - Sparse/compressed: No allocated clusters.
396e8b8e97fSKari Argillander  *   - Normal: Append allocated and preallocated new clusters.
397be71b5cbSKonstantin Komarov  * Shrink:
398e8b8e97fSKari Argillander  *   - No deallocate if @keep_prealloc is set.
399be71b5cbSKonstantin Komarov  */
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)400be71b5cbSKonstantin Komarov int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
401be71b5cbSKonstantin Komarov 		  const __le16 *name, u8 name_len, struct runs_tree *run,
402be71b5cbSKonstantin Komarov 		  u64 new_size, const u64 *new_valid, bool keep_prealloc,
403be71b5cbSKonstantin Komarov 		  struct ATTRIB **ret)
404be71b5cbSKonstantin Komarov {
405be71b5cbSKonstantin Komarov 	int err = 0;
406be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
407be71b5cbSKonstantin Komarov 	u8 cluster_bits = sbi->cluster_bits;
40896de65a9SKonstantin Komarov 	bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
40996de65a9SKonstantin Komarov 		      !name_len;
410be71b5cbSKonstantin Komarov 	u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
411be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
412be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
413be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
414be71b5cbSKonstantin Komarov 	CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
415be71b5cbSKonstantin Komarov 	CLST next_svcn, pre_alloc = -1, done = 0;
4160e5b044cSKonstantin Komarov 	bool is_ext, is_bad = false;
417ad26a9c8SKonstantin Komarov 	bool dirty = false;
418be71b5cbSKonstantin Komarov 	u32 align;
419be71b5cbSKonstantin Komarov 	struct MFT_REC *rec;
420be71b5cbSKonstantin Komarov 
421be71b5cbSKonstantin Komarov again:
4220e5b044cSKonstantin Komarov 	alen = 0;
423be71b5cbSKonstantin Komarov 	le_b = NULL;
424be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
425be71b5cbSKonstantin Komarov 			      &mi_b);
426be71b5cbSKonstantin Komarov 	if (!attr_b) {
427be71b5cbSKonstantin Komarov 		err = -ENOENT;
4280e5b044cSKonstantin Komarov 		goto bad_inode;
429be71b5cbSKonstantin Komarov 	}
430be71b5cbSKonstantin Komarov 
431be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
432be71b5cbSKonstantin Komarov 		err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
433be71b5cbSKonstantin Komarov 					&attr_b);
4340e5b044cSKonstantin Komarov 		if (err)
4350e5b044cSKonstantin Komarov 			return err;
4360e5b044cSKonstantin Komarov 
4370e5b044cSKonstantin Komarov 		/* Return if file is still resident. */
438ad26a9c8SKonstantin Komarov 		if (!attr_b->non_res) {
439ad26a9c8SKonstantin Komarov 			dirty = true;
4400e5b044cSKonstantin Komarov 			goto ok1;
441ad26a9c8SKonstantin Komarov 		}
442be71b5cbSKonstantin Komarov 
443e8b8e97fSKari Argillander 		/* Layout of records may be changed, so do a full search. */
444be71b5cbSKonstantin Komarov 		goto again;
445be71b5cbSKonstantin Komarov 	}
446be71b5cbSKonstantin Komarov 
447be71b5cbSKonstantin Komarov 	is_ext = is_attr_ext(attr_b);
448be71b5cbSKonstantin Komarov 	align = sbi->cluster_size;
449ce46ae0cSKonstantin Komarov 	if (is_ext)
450be71b5cbSKonstantin Komarov 		align <<= attr_b->nres.c_unit;
451be71b5cbSKonstantin Komarov 
452be71b5cbSKonstantin Komarov 	old_valid = le64_to_cpu(attr_b->nres.valid_size);
453be71b5cbSKonstantin Komarov 	old_size = le64_to_cpu(attr_b->nres.data_size);
454be71b5cbSKonstantin Komarov 	old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
4550e5b044cSKonstantin Komarov 
4560e5b044cSKonstantin Komarov again_1:
457be71b5cbSKonstantin Komarov 	old_alen = old_alloc >> cluster_bits;
458be71b5cbSKonstantin Komarov 
459be71b5cbSKonstantin Komarov 	new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
460be71b5cbSKonstantin Komarov 	new_alen = new_alloc >> cluster_bits;
461be71b5cbSKonstantin Komarov 
462be71b5cbSKonstantin Komarov 	if (keep_prealloc && new_size < old_size) {
463be71b5cbSKonstantin Komarov 		attr_b->nres.data_size = cpu_to_le64(new_size);
464ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
465be71b5cbSKonstantin Komarov 		goto ok;
466be71b5cbSKonstantin Komarov 	}
467be71b5cbSKonstantin Komarov 
468be71b5cbSKonstantin Komarov 	vcn = old_alen - 1;
469be71b5cbSKonstantin Komarov 
470be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
471be71b5cbSKonstantin Komarov 	evcn = le64_to_cpu(attr_b->nres.evcn);
472be71b5cbSKonstantin Komarov 
473be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn <= evcn) {
474be71b5cbSKonstantin Komarov 		attr = attr_b;
475be71b5cbSKonstantin Komarov 		le = le_b;
476be71b5cbSKonstantin Komarov 		mi = mi_b;
477be71b5cbSKonstantin Komarov 	} else if (!le_b) {
478be71b5cbSKonstantin Komarov 		err = -EINVAL;
4790e5b044cSKonstantin Komarov 		goto bad_inode;
480be71b5cbSKonstantin Komarov 	} else {
481be71b5cbSKonstantin Komarov 		le = le_b;
482be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
483be71b5cbSKonstantin Komarov 				    &mi);
484be71b5cbSKonstantin Komarov 		if (!attr) {
485be71b5cbSKonstantin Komarov 			err = -EINVAL;
4860e5b044cSKonstantin Komarov 			goto bad_inode;
487be71b5cbSKonstantin Komarov 		}
488be71b5cbSKonstantin Komarov 
489be71b5cbSKonstantin Komarov next_le_1:
490be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
491be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
492be71b5cbSKonstantin Komarov 	}
4930e5b044cSKonstantin Komarov 	/*
4940e5b044cSKonstantin Komarov 	 * Here we have:
4950e5b044cSKonstantin Komarov 	 * attr,mi,le - last attribute segment (containing 'vcn').
4960e5b044cSKonstantin Komarov 	 * attr_b,mi_b,le_b - base (primary) attribute segment.
4970e5b044cSKonstantin Komarov 	 */
498be71b5cbSKonstantin Komarov next_le:
499be71b5cbSKonstantin Komarov 	rec = mi->mrec;
500be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
501be71b5cbSKonstantin Komarov 	if (err)
502be71b5cbSKonstantin Komarov 		goto out;
503be71b5cbSKonstantin Komarov 
504be71b5cbSKonstantin Komarov 	if (new_size > old_size) {
505be71b5cbSKonstantin Komarov 		CLST to_allocate;
506be71b5cbSKonstantin Komarov 		size_t free;
507be71b5cbSKonstantin Komarov 
508be71b5cbSKonstantin Komarov 		if (new_alloc <= old_alloc) {
509be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
510ad26a9c8SKonstantin Komarov 			mi_b->dirty = dirty = true;
511be71b5cbSKonstantin Komarov 			goto ok;
512be71b5cbSKonstantin Komarov 		}
513be71b5cbSKonstantin Komarov 
5140e5b044cSKonstantin Komarov 		/*
5150e5b044cSKonstantin Komarov 		 * Add clusters. In simple case we have to:
5160e5b044cSKonstantin Komarov 		 *  - allocate space (vcn, lcn, len)
5170e5b044cSKonstantin Komarov 		 *  - update packed run in 'mi'
5180e5b044cSKonstantin Komarov 		 *  - update attr->nres.evcn
5190e5b044cSKonstantin Komarov 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
5200e5b044cSKonstantin Komarov 		 */
521be71b5cbSKonstantin Komarov 		to_allocate = new_alen - old_alen;
522be71b5cbSKonstantin Komarov add_alloc_in_same_attr_seg:
523be71b5cbSKonstantin Komarov 		lcn = 0;
524be71b5cbSKonstantin Komarov 		if (is_mft) {
525e8b8e97fSKari Argillander 			/* MFT allocates clusters from MFT zone. */
526be71b5cbSKonstantin Komarov 			pre_alloc = 0;
527be71b5cbSKonstantin Komarov 		} else if (is_ext) {
528e8b8e97fSKari Argillander 			/* No preallocate for sparse/compress. */
529be71b5cbSKonstantin Komarov 			pre_alloc = 0;
530be71b5cbSKonstantin Komarov 		} else if (pre_alloc == -1) {
531be71b5cbSKonstantin Komarov 			pre_alloc = 0;
532be71b5cbSKonstantin Komarov 			if (type == ATTR_DATA && !name_len &&
533564c97bdSKari Argillander 			    sbi->options->prealloc) {
53496de65a9SKonstantin Komarov 				pre_alloc = bytes_to_cluster(
53596de65a9SKonstantin Komarov 						    sbi, get_pre_allocated(
53696de65a9SKonstantin Komarov 								 new_size)) -
5370e5b044cSKonstantin Komarov 					    new_alen;
538be71b5cbSKonstantin Komarov 			}
539be71b5cbSKonstantin Komarov 
540e8b8e97fSKari Argillander 			/* Get the last LCN to allocate from. */
541be71b5cbSKonstantin Komarov 			if (old_alen &&
542be71b5cbSKonstantin Komarov 			    !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
543be71b5cbSKonstantin Komarov 				lcn = SPARSE_LCN;
544be71b5cbSKonstantin Komarov 			}
545be71b5cbSKonstantin Komarov 
546be71b5cbSKonstantin Komarov 			if (lcn == SPARSE_LCN)
547be71b5cbSKonstantin Komarov 				lcn = 0;
548be71b5cbSKonstantin Komarov 			else if (lcn)
549be71b5cbSKonstantin Komarov 				lcn += 1;
550be71b5cbSKonstantin Komarov 
551be71b5cbSKonstantin Komarov 			free = wnd_zeroes(&sbi->used.bitmap);
552be71b5cbSKonstantin Komarov 			if (to_allocate > free) {
553be71b5cbSKonstantin Komarov 				err = -ENOSPC;
554be71b5cbSKonstantin Komarov 				goto out;
555be71b5cbSKonstantin Komarov 			}
556be71b5cbSKonstantin Komarov 
557be71b5cbSKonstantin Komarov 			if (pre_alloc && to_allocate + pre_alloc > free)
558be71b5cbSKonstantin Komarov 				pre_alloc = 0;
559be71b5cbSKonstantin Komarov 		}
560be71b5cbSKonstantin Komarov 
561be71b5cbSKonstantin Komarov 		vcn = old_alen;
562be71b5cbSKonstantin Komarov 
563be71b5cbSKonstantin Komarov 		if (is_ext) {
564be71b5cbSKonstantin Komarov 			if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
565be71b5cbSKonstantin Komarov 					   false)) {
566be71b5cbSKonstantin Komarov 				err = -ENOMEM;
567be71b5cbSKonstantin Komarov 				goto out;
568be71b5cbSKonstantin Komarov 			}
569be71b5cbSKonstantin Komarov 			alen = to_allocate;
570be71b5cbSKonstantin Komarov 		} else {
571e8b8e97fSKari Argillander 			/* ~3 bytes per fragment. */
572be71b5cbSKonstantin Komarov 			err = attr_allocate_clusters(
573be71b5cbSKonstantin Komarov 				sbi, run, vcn, lcn, to_allocate, &pre_alloc,
574c380b52fSKonstantin Komarov 				is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
57596de65a9SKonstantin Komarov 				is_mft ? 0 :
57696de65a9SKonstantin Komarov 					 (sbi->record_size -
577be71b5cbSKonstantin Komarov 					  le32_to_cpu(rec->used) + 8) /
578be71b5cbSKonstantin Komarov 							 3 +
579be71b5cbSKonstantin Komarov 						 1,
580c380b52fSKonstantin Komarov 				NULL, NULL);
581be71b5cbSKonstantin Komarov 			if (err)
582be71b5cbSKonstantin Komarov 				goto out;
583be71b5cbSKonstantin Komarov 		}
584be71b5cbSKonstantin Komarov 
585be71b5cbSKonstantin Komarov 		done += alen;
586be71b5cbSKonstantin Komarov 		vcn += alen;
587be71b5cbSKonstantin Komarov 		if (to_allocate > alen)
588be71b5cbSKonstantin Komarov 			to_allocate -= alen;
589be71b5cbSKonstantin Komarov 		else
590be71b5cbSKonstantin Komarov 			to_allocate = 0;
591be71b5cbSKonstantin Komarov 
592be71b5cbSKonstantin Komarov pack_runs:
593be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, vcn - svcn);
594be71b5cbSKonstantin Komarov 		if (err)
5950e5b044cSKonstantin Komarov 			goto undo_1;
596be71b5cbSKonstantin Komarov 
597be71b5cbSKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
598be71b5cbSKonstantin Komarov 		new_alloc_tmp = (u64)next_svcn << cluster_bits;
599be71b5cbSKonstantin Komarov 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
600ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
601be71b5cbSKonstantin Komarov 
602be71b5cbSKonstantin Komarov 		if (next_svcn >= vcn && !to_allocate) {
603e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
604be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
605be71b5cbSKonstantin Komarov 			goto ok;
606be71b5cbSKonstantin Komarov 		}
607be71b5cbSKonstantin Komarov 
608e8b8e97fSKari Argillander 		/* At least two MFT to avoid recursive loop. */
609be71b5cbSKonstantin Komarov 		if (is_mft && next_svcn == vcn &&
610be71b5cbSKonstantin Komarov 		    ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
611be71b5cbSKonstantin Komarov 			new_size = new_alloc_tmp;
612be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = attr_b->nres.alloc_size;
613be71b5cbSKonstantin Komarov 			goto ok;
614be71b5cbSKonstantin Komarov 		}
615be71b5cbSKonstantin Komarov 
616be71b5cbSKonstantin Komarov 		if (le32_to_cpu(rec->used) < sbi->record_size) {
617be71b5cbSKonstantin Komarov 			old_alen = next_svcn;
618be71b5cbSKonstantin Komarov 			evcn = old_alen - 1;
619be71b5cbSKonstantin Komarov 			goto add_alloc_in_same_attr_seg;
620be71b5cbSKonstantin Komarov 		}
621be71b5cbSKonstantin Komarov 
622be71b5cbSKonstantin Komarov 		attr_b->nres.data_size = attr_b->nres.alloc_size;
623be71b5cbSKonstantin Komarov 		if (new_alloc_tmp < old_valid)
624be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = attr_b->nres.data_size;
625be71b5cbSKonstantin Komarov 
626be71b5cbSKonstantin Komarov 		if (type == ATTR_LIST) {
627be71b5cbSKonstantin Komarov 			err = ni_expand_list(ni);
628be71b5cbSKonstantin Komarov 			if (err)
6290e5b044cSKonstantin Komarov 				goto undo_2;
630be71b5cbSKonstantin Komarov 			if (next_svcn < vcn)
631be71b5cbSKonstantin Komarov 				goto pack_runs;
632be71b5cbSKonstantin Komarov 
633e8b8e97fSKari Argillander 			/* Layout of records is changed. */
634be71b5cbSKonstantin Komarov 			goto again;
635be71b5cbSKonstantin Komarov 		}
636be71b5cbSKonstantin Komarov 
637be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
638be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
6390e5b044cSKonstantin Komarov 			/* In case of error layout of records is not changed. */
640be71b5cbSKonstantin Komarov 			if (err)
6410e5b044cSKonstantin Komarov 				goto undo_2;
642e8b8e97fSKari Argillander 			/* Layout of records is changed. */
643be71b5cbSKonstantin Komarov 		}
644be71b5cbSKonstantin Komarov 
645be71b5cbSKonstantin Komarov 		if (next_svcn >= vcn) {
646e8b8e97fSKari Argillander 			/* This is MFT data, repeat. */
647be71b5cbSKonstantin Komarov 			goto again;
648be71b5cbSKonstantin Komarov 		}
649be71b5cbSKonstantin Komarov 
650e8b8e97fSKari Argillander 		/* Insert new attribute segment. */
651be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, type, name, name_len, run,
652be71b5cbSKonstantin Komarov 					    next_svcn, vcn - next_svcn,
653c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
6540e5b044cSKonstantin Komarov 
6550e5b044cSKonstantin Komarov 		/*
6560e5b044cSKonstantin Komarov 		 * Layout of records maybe changed.
6570e5b044cSKonstantin Komarov 		 * Find base attribute to update.
6580e5b044cSKonstantin Komarov 		 */
6590e5b044cSKonstantin Komarov 		le_b = NULL;
6600e5b044cSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
6610e5b044cSKonstantin Komarov 				      NULL, &mi_b);
6620e5b044cSKonstantin Komarov 		if (!attr_b) {
6630e5b044cSKonstantin Komarov 			err = -EINVAL;
6640e5b044cSKonstantin Komarov 			goto bad_inode;
6650e5b044cSKonstantin Komarov 		}
6660e5b044cSKonstantin Komarov 
6670e5b044cSKonstantin Komarov 		if (err) {
6680e5b044cSKonstantin Komarov 			/* ni_insert_nonresident failed. */
6690e5b044cSKonstantin Komarov 			attr = NULL;
6700e5b044cSKonstantin Komarov 			goto undo_2;
6710e5b044cSKonstantin Komarov 		}
672be71b5cbSKonstantin Komarov 
673be71b5cbSKonstantin Komarov 		if (!is_mft)
674be71b5cbSKonstantin Komarov 			run_truncate_head(run, evcn + 1);
675be71b5cbSKonstantin Komarov 
676be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
677be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
678be71b5cbSKonstantin Komarov 
679e8b8e97fSKari Argillander 		/*
6800e5b044cSKonstantin Komarov 		 * Attribute is in consistency state.
6810e5b044cSKonstantin Komarov 		 * Save this point to restore to if next steps fail.
682e8b8e97fSKari Argillander 		 */
6830e5b044cSKonstantin Komarov 		old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
6840e5b044cSKonstantin Komarov 		attr_b->nres.valid_size = attr_b->nres.data_size =
6850e5b044cSKonstantin Komarov 			attr_b->nres.alloc_size = cpu_to_le64(old_size);
686ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
687be71b5cbSKonstantin Komarov 		goto again_1;
688be71b5cbSKonstantin Komarov 	}
689be71b5cbSKonstantin Komarov 
690be71b5cbSKonstantin Komarov 	if (new_size != old_size ||
691be71b5cbSKonstantin Komarov 	    (new_alloc != old_alloc && !keep_prealloc)) {
6920e5b044cSKonstantin Komarov 		/*
6930e5b044cSKonstantin Komarov 		 * Truncate clusters. In simple case we have to:
6940e5b044cSKonstantin Komarov 		 *  - update packed run in 'mi'
6950e5b044cSKonstantin Komarov 		 *  - update attr->nres.evcn
6960e5b044cSKonstantin Komarov 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
6970e5b044cSKonstantin Komarov 		 *  - mark and trim clusters as free (vcn, lcn, len)
6980e5b044cSKonstantin Komarov 		 */
6990e5b044cSKonstantin Komarov 		CLST dlen = 0;
7000e5b044cSKonstantin Komarov 
701be71b5cbSKonstantin Komarov 		vcn = max(svcn, new_alen);
702be71b5cbSKonstantin Komarov 		new_alloc_tmp = (u64)vcn << cluster_bits;
703be71b5cbSKonstantin Komarov 
704be71b5cbSKonstantin Komarov 		if (vcn > svcn) {
705be71b5cbSKonstantin Komarov 			err = mi_pack_runs(mi, attr, run, vcn - svcn);
706be71b5cbSKonstantin Komarov 			if (err)
707be71b5cbSKonstantin Komarov 				goto out;
708be71b5cbSKonstantin Komarov 		} else if (le && le->vcn) {
709be71b5cbSKonstantin Komarov 			u16 le_sz = le16_to_cpu(le->size);
710be71b5cbSKonstantin Komarov 
711be71b5cbSKonstantin Komarov 			/*
712e8b8e97fSKari Argillander 			 * NOTE: List entries for one attribute are always
713be71b5cbSKonstantin Komarov 			 * the same size. We deal with last entry (vcn==0)
714be71b5cbSKonstantin Komarov 			 * and it is not first in entries array
715e8b8e97fSKari Argillander 			 * (list entry for std attribute always first).
716e8b8e97fSKari Argillander 			 * So it is safe to step back.
717be71b5cbSKonstantin Komarov 			 */
71878ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
719be71b5cbSKonstantin Komarov 
720be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
721be71b5cbSKonstantin Komarov 				err = -EINVAL;
7220e5b044cSKonstantin Komarov 				goto bad_inode;
723be71b5cbSKonstantin Komarov 			}
724be71b5cbSKonstantin Komarov 
725be71b5cbSKonstantin Komarov 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
726be71b5cbSKonstantin Komarov 		} else {
727be71b5cbSKonstantin Komarov 			attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
728be71b5cbSKonstantin Komarov 			mi->dirty = true;
729be71b5cbSKonstantin Komarov 		}
730be71b5cbSKonstantin Komarov 
731be71b5cbSKonstantin Komarov 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
732be71b5cbSKonstantin Komarov 
733be71b5cbSKonstantin Komarov 		if (vcn == new_alen) {
734be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
735be71b5cbSKonstantin Komarov 			if (new_size < old_valid)
736be71b5cbSKonstantin Komarov 				attr_b->nres.valid_size =
737be71b5cbSKonstantin Komarov 					attr_b->nres.data_size;
738be71b5cbSKonstantin Komarov 		} else {
739be71b5cbSKonstantin Komarov 			if (new_alloc_tmp <=
740be71b5cbSKonstantin Komarov 			    le64_to_cpu(attr_b->nres.data_size))
741be71b5cbSKonstantin Komarov 				attr_b->nres.data_size =
742be71b5cbSKonstantin Komarov 					attr_b->nres.alloc_size;
743be71b5cbSKonstantin Komarov 			if (new_alloc_tmp <
744be71b5cbSKonstantin Komarov 			    le64_to_cpu(attr_b->nres.valid_size))
745be71b5cbSKonstantin Komarov 				attr_b->nres.valid_size =
746be71b5cbSKonstantin Komarov 					attr_b->nres.alloc_size;
747be71b5cbSKonstantin Komarov 		}
748ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
749be71b5cbSKonstantin Komarov 
7500e5b044cSKonstantin Komarov 		err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
7510e5b044cSKonstantin Komarov 					true);
7520e5b044cSKonstantin Komarov 		if (err)
7530e5b044cSKonstantin Komarov 			goto out;
7540e5b044cSKonstantin Komarov 
7550e5b044cSKonstantin Komarov 		if (is_ext) {
7560e5b044cSKonstantin Komarov 			/* dlen - really deallocated clusters. */
7570e5b044cSKonstantin Komarov 			le64_sub_cpu(&attr_b->nres.total_size,
7580e5b044cSKonstantin Komarov 				     ((u64)dlen << cluster_bits));
7590e5b044cSKonstantin Komarov 		}
7600e5b044cSKonstantin Komarov 
7610e5b044cSKonstantin Komarov 		run_truncate(run, vcn);
7620e5b044cSKonstantin Komarov 
763be71b5cbSKonstantin Komarov 		if (new_alloc_tmp <= new_alloc)
764be71b5cbSKonstantin Komarov 			goto ok;
765be71b5cbSKonstantin Komarov 
766be71b5cbSKonstantin Komarov 		old_size = new_alloc_tmp;
767be71b5cbSKonstantin Komarov 		vcn = svcn - 1;
768be71b5cbSKonstantin Komarov 
769be71b5cbSKonstantin Komarov 		if (le == le_b) {
770be71b5cbSKonstantin Komarov 			attr = attr_b;
771be71b5cbSKonstantin Komarov 			mi = mi_b;
772be71b5cbSKonstantin Komarov 			evcn = svcn - 1;
773be71b5cbSKonstantin Komarov 			svcn = 0;
774be71b5cbSKonstantin Komarov 			goto next_le;
775be71b5cbSKonstantin Komarov 		}
776be71b5cbSKonstantin Komarov 
777be71b5cbSKonstantin Komarov 		if (le->type != type || le->name_len != name_len ||
778be71b5cbSKonstantin Komarov 		    memcmp(le_name(le), name, name_len * sizeof(short))) {
779be71b5cbSKonstantin Komarov 			err = -EINVAL;
7800e5b044cSKonstantin Komarov 			goto bad_inode;
781be71b5cbSKonstantin Komarov 		}
782be71b5cbSKonstantin Komarov 
783be71b5cbSKonstantin Komarov 		err = ni_load_mi(ni, le, &mi);
784be71b5cbSKonstantin Komarov 		if (err)
785be71b5cbSKonstantin Komarov 			goto out;
786be71b5cbSKonstantin Komarov 
787be71b5cbSKonstantin Komarov 		attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
788be71b5cbSKonstantin Komarov 		if (!attr) {
789be71b5cbSKonstantin Komarov 			err = -EINVAL;
7900e5b044cSKonstantin Komarov 			goto bad_inode;
791be71b5cbSKonstantin Komarov 		}
792be71b5cbSKonstantin Komarov 		goto next_le_1;
793be71b5cbSKonstantin Komarov 	}
794be71b5cbSKonstantin Komarov 
795be71b5cbSKonstantin Komarov ok:
796be71b5cbSKonstantin Komarov 	if (new_valid) {
797be71b5cbSKonstantin Komarov 		__le64 valid = cpu_to_le64(min(*new_valid, new_size));
798be71b5cbSKonstantin Komarov 
799be71b5cbSKonstantin Komarov 		if (attr_b->nres.valid_size != valid) {
800be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = valid;
801be71b5cbSKonstantin Komarov 			mi_b->dirty = true;
802be71b5cbSKonstantin Komarov 		}
803be71b5cbSKonstantin Komarov 	}
804be71b5cbSKonstantin Komarov 
8050e5b044cSKonstantin Komarov ok1:
8060e5b044cSKonstantin Komarov 	if (ret)
807be71b5cbSKonstantin Komarov 		*ret = attr_b;
808be71b5cbSKonstantin Komarov 
8090e5b044cSKonstantin Komarov 	if (((type == ATTR_DATA && !name_len) ||
810be71b5cbSKonstantin Komarov 	     (type == ATTR_ALLOC && name == I30_NAME))) {
811ad26a9c8SKonstantin Komarov 		/* Update inode_set_bytes. */
8120e5b044cSKonstantin Komarov 		if (attr_b->non_res) {
813be71b5cbSKonstantin Komarov 			new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
814be71b5cbSKonstantin Komarov 			if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
815be71b5cbSKonstantin Komarov 				inode_set_bytes(&ni->vfs_inode, new_alloc);
816be71b5cbSKonstantin Komarov 				dirty = true;
817be71b5cbSKonstantin Komarov 			}
818be71b5cbSKonstantin Komarov 		}
819be71b5cbSKonstantin Komarov 
820ad26a9c8SKonstantin Komarov 		/* Don't forget to update duplicate information in parent. */
821be71b5cbSKonstantin Komarov 		if (dirty) {
822be71b5cbSKonstantin Komarov 			ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
823be71b5cbSKonstantin Komarov 			mark_inode_dirty(&ni->vfs_inode);
824be71b5cbSKonstantin Komarov 		}
825be71b5cbSKonstantin Komarov 	}
826be71b5cbSKonstantin Komarov 
8270e5b044cSKonstantin Komarov 	return 0;
8280e5b044cSKonstantin Komarov 
8290e5b044cSKonstantin Komarov undo_2:
8300e5b044cSKonstantin Komarov 	vcn -= alen;
8310e5b044cSKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(old_size);
8320e5b044cSKonstantin Komarov 	attr_b->nres.valid_size = cpu_to_le64(old_valid);
8330e5b044cSKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
8340e5b044cSKonstantin Komarov 
8350e5b044cSKonstantin Komarov 	/* Restore 'attr' and 'mi'. */
8360e5b044cSKonstantin Komarov 	if (attr)
8370e5b044cSKonstantin Komarov 		goto restore_run;
8380e5b044cSKonstantin Komarov 
8390e5b044cSKonstantin Komarov 	if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
8400e5b044cSKonstantin Komarov 	    svcn <= le64_to_cpu(attr_b->nres.evcn)) {
8410e5b044cSKonstantin Komarov 		attr = attr_b;
8420e5b044cSKonstantin Komarov 		le = le_b;
8430e5b044cSKonstantin Komarov 		mi = mi_b;
8440e5b044cSKonstantin Komarov 	} else if (!le_b) {
8450e5b044cSKonstantin Komarov 		err = -EINVAL;
8460e5b044cSKonstantin Komarov 		goto bad_inode;
8470e5b044cSKonstantin Komarov 	} else {
8480e5b044cSKonstantin Komarov 		le = le_b;
8490e5b044cSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
8500e5b044cSKonstantin Komarov 				    &svcn, &mi);
8510e5b044cSKonstantin Komarov 		if (!attr)
8520e5b044cSKonstantin Komarov 			goto bad_inode;
8530e5b044cSKonstantin Komarov 	}
8540e5b044cSKonstantin Komarov 
8550e5b044cSKonstantin Komarov restore_run:
8560e5b044cSKonstantin Komarov 	if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
8570e5b044cSKonstantin Komarov 		is_bad = true;
8580e5b044cSKonstantin Komarov 
8590e5b044cSKonstantin Komarov undo_1:
8600e5b044cSKonstantin Komarov 	run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
8610e5b044cSKonstantin Komarov 
8620e5b044cSKonstantin Komarov 	run_truncate(run, vcn);
8630e5b044cSKonstantin Komarov out:
8640e5b044cSKonstantin Komarov 	if (is_bad) {
8650e5b044cSKonstantin Komarov bad_inode:
8660e5b044cSKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
8670e5b044cSKonstantin Komarov 	}
868be71b5cbSKonstantin Komarov 	return err;
869be71b5cbSKonstantin Komarov }
870be71b5cbSKonstantin Komarov 
871c380b52fSKonstantin Komarov /*
872c380b52fSKonstantin Komarov  * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
873c380b52fSKonstantin Komarov  *
874c380b52fSKonstantin Komarov  * @new == NULL means just to get current mapping for 'vcn'
875c380b52fSKonstantin Komarov  * @new != NULL means allocate real cluster if 'vcn' maps to hole
876c380b52fSKonstantin Komarov  * @zero - zeroout new allocated clusters
877c380b52fSKonstantin Komarov  *
878c380b52fSKonstantin Komarov  *  NOTE:
879c380b52fSKonstantin Komarov  *  - @new != NULL is called only for sparsed or compressed attributes.
880c380b52fSKonstantin Komarov  *  - new allocated clusters are zeroed via blkdev_issue_zeroout.
881c380b52fSKonstantin Komarov  */
attr_data_get_block(struct ntfs_inode * ni,CLST vcn,CLST clen,CLST * lcn,CLST * len,bool * new,bool zero)882be71b5cbSKonstantin Komarov int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
883c380b52fSKonstantin Komarov 			CLST *len, bool *new, bool zero)
884be71b5cbSKonstantin Komarov {
885be71b5cbSKonstantin Komarov 	int err = 0;
886be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
887be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi;
888be71b5cbSKonstantin Komarov 	u8 cluster_bits;
889a8b0c9fcSKonstantin Komarov 	struct ATTRIB *attr, *attr_b;
890be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
891be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
892c380b52fSKonstantin Komarov 	CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
893910013f7SKonstantin Komarov 	CLST alloc, evcn;
894c380b52fSKonstantin Komarov 	unsigned fr;
895910013f7SKonstantin Komarov 	u64 total_size, total_size0;
896910013f7SKonstantin Komarov 	int step = 0;
897be71b5cbSKonstantin Komarov 
898be71b5cbSKonstantin Komarov 	if (new)
899be71b5cbSKonstantin Komarov 		*new = false;
900be71b5cbSKonstantin Komarov 
901c380b52fSKonstantin Komarov 	/* Try to find in cache. */
902be71b5cbSKonstantin Komarov 	down_read(&ni->file.run_lock);
903c380b52fSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
904c380b52fSKonstantin Komarov 		*len = 0;
905be71b5cbSKonstantin Komarov 	up_read(&ni->file.run_lock);
906be71b5cbSKonstantin Komarov 
907a8b0c9fcSKonstantin Komarov 	if (*len && (*lcn != SPARSE_LCN || !new))
908c380b52fSKonstantin Komarov 		return 0; /* Fast normal way without allocation. */
909be71b5cbSKonstantin Komarov 
910c380b52fSKonstantin Komarov 	/* No cluster in cache or we need to allocate cluster in hole. */
911be71b5cbSKonstantin Komarov 	sbi = ni->mi.sbi;
912be71b5cbSKonstantin Komarov 	cluster_bits = sbi->cluster_bits;
913be71b5cbSKonstantin Komarov 
914be71b5cbSKonstantin Komarov 	ni_lock(ni);
915be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
916be71b5cbSKonstantin Komarov 
917a8b0c9fcSKonstantin Komarov 	/* Repeat the code above (under write lock). */
918a8b0c9fcSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
919a8b0c9fcSKonstantin Komarov 		*len = 0;
920a8b0c9fcSKonstantin Komarov 
921a8b0c9fcSKonstantin Komarov 	if (*len) {
922a8b0c9fcSKonstantin Komarov 		if (*lcn != SPARSE_LCN || !new)
923a8b0c9fcSKonstantin Komarov 			goto out; /* normal way without allocation. */
924a8b0c9fcSKonstantin Komarov 		if (clen > *len)
925a8b0c9fcSKonstantin Komarov 			clen = *len;
926a8b0c9fcSKonstantin Komarov 	}
927a8b0c9fcSKonstantin Komarov 
928be71b5cbSKonstantin Komarov 	le_b = NULL;
929be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
930be71b5cbSKonstantin Komarov 	if (!attr_b) {
931be71b5cbSKonstantin Komarov 		err = -ENOENT;
932be71b5cbSKonstantin Komarov 		goto out;
933be71b5cbSKonstantin Komarov 	}
934be71b5cbSKonstantin Komarov 
935be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
936be71b5cbSKonstantin Komarov 		*lcn = RESIDENT_LCN;
937be71b5cbSKonstantin Komarov 		*len = 1;
938be71b5cbSKonstantin Komarov 		goto out;
939be71b5cbSKonstantin Komarov 	}
940be71b5cbSKonstantin Komarov 
941c1e0ab37SKonstantin Komarov 	asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
942be71b5cbSKonstantin Komarov 	if (vcn >= asize) {
943910013f7SKonstantin Komarov 		if (new) {
944be71b5cbSKonstantin Komarov 			err = -EINVAL;
945910013f7SKonstantin Komarov 		} else {
946910013f7SKonstantin Komarov 			*len = 1;
947910013f7SKonstantin Komarov 			*lcn = SPARSE_LCN;
948910013f7SKonstantin Komarov 		}
949be71b5cbSKonstantin Komarov 		goto out;
950be71b5cbSKonstantin Komarov 	}
951be71b5cbSKonstantin Komarov 
952be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
953be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
954be71b5cbSKonstantin Komarov 
955be71b5cbSKonstantin Komarov 	attr = attr_b;
956be71b5cbSKonstantin Komarov 	le = le_b;
957be71b5cbSKonstantin Komarov 	mi = mi_b;
958be71b5cbSKonstantin Komarov 
959be71b5cbSKonstantin Komarov 	if (le_b && (vcn < svcn || evcn1 <= vcn)) {
960be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
961be71b5cbSKonstantin Komarov 				    &mi);
962be71b5cbSKonstantin Komarov 		if (!attr) {
963be71b5cbSKonstantin Komarov 			err = -EINVAL;
964be71b5cbSKonstantin Komarov 			goto out;
965be71b5cbSKonstantin Komarov 		}
966be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
967be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
968be71b5cbSKonstantin Komarov 	}
969be71b5cbSKonstantin Komarov 
970c380b52fSKonstantin Komarov 	/* Load in cache actual information. */
971be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
972be71b5cbSKonstantin Komarov 	if (err)
973be71b5cbSKonstantin Komarov 		goto out;
974be71b5cbSKonstantin Komarov 
975c380b52fSKonstantin Komarov 	if (!*len) {
976c380b52fSKonstantin Komarov 		if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
977c380b52fSKonstantin Komarov 			if (*lcn != SPARSE_LCN || !new)
978c380b52fSKonstantin Komarov 				goto ok; /* Slow normal way without allocation. */
979be71b5cbSKonstantin Komarov 
980c380b52fSKonstantin Komarov 			if (clen > *len)
981be71b5cbSKonstantin Komarov 				clen = *len;
982c380b52fSKonstantin Komarov 		} else if (!new) {
983c380b52fSKonstantin Komarov 			/* Here we may return -ENOENT.
984c380b52fSKonstantin Komarov 			 * In any case caller gets zero length. */
985c380b52fSKonstantin Komarov 			goto ok;
986be71b5cbSKonstantin Komarov 		}
987be71b5cbSKonstantin Komarov 	}
988be71b5cbSKonstantin Komarov 
989be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b)) {
990c380b52fSKonstantin Komarov 		/* The code below only for sparsed or compressed attributes. */
991be71b5cbSKonstantin Komarov 		err = -EINVAL;
992be71b5cbSKonstantin Komarov 		goto out;
993be71b5cbSKonstantin Komarov 	}
994be71b5cbSKonstantin Komarov 
995c380b52fSKonstantin Komarov 	vcn0 = vcn;
996c380b52fSKonstantin Komarov 	to_alloc = clen;
997c380b52fSKonstantin Komarov 	fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
998c380b52fSKonstantin Komarov 	/* Allocate frame aligned clusters.
999c380b52fSKonstantin Komarov 	 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
1000c380b52fSKonstantin Komarov 	 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
1001c380b52fSKonstantin Komarov 	if (attr_b->nres.c_unit) {
1002c380b52fSKonstantin Komarov 		CLST clst_per_frame = 1u << attr_b->nres.c_unit;
1003c380b52fSKonstantin Komarov 		CLST cmask = ~(clst_per_frame - 1);
1004c380b52fSKonstantin Komarov 
1005c380b52fSKonstantin Komarov 		/* Get frame aligned vcn and to_alloc. */
1006c380b52fSKonstantin Komarov 		vcn = vcn0 & cmask;
1007c380b52fSKonstantin Komarov 		to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
1008c380b52fSKonstantin Komarov 		if (fr < clst_per_frame)
1009c380b52fSKonstantin Komarov 			fr = clst_per_frame;
1010c380b52fSKonstantin Komarov 		zero = true;
1011c380b52fSKonstantin Komarov 
1012c380b52fSKonstantin Komarov 		/* Check if 'vcn' and 'vcn0' in different attribute segments. */
1013c380b52fSKonstantin Komarov 		if (vcn < svcn || evcn1 <= vcn) {
1014c380b52fSKonstantin Komarov 			/* Load attribute for truncated vcn. */
1015c380b52fSKonstantin Komarov 			attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0,
1016c380b52fSKonstantin Komarov 					    &vcn, &mi);
1017c380b52fSKonstantin Komarov 			if (!attr) {
1018c380b52fSKonstantin Komarov 				err = -EINVAL;
1019c380b52fSKonstantin Komarov 				goto out;
1020c380b52fSKonstantin Komarov 			}
1021c380b52fSKonstantin Komarov 			svcn = le64_to_cpu(attr->nres.svcn);
1022c380b52fSKonstantin Komarov 			evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1023c380b52fSKonstantin Komarov 			err = attr_load_runs(attr, ni, run, NULL);
1024c380b52fSKonstantin Komarov 			if (err)
1025c380b52fSKonstantin Komarov 				goto out;
1026c380b52fSKonstantin Komarov 		}
1027c380b52fSKonstantin Komarov 	}
1028c380b52fSKonstantin Komarov 
1029c380b52fSKonstantin Komarov 	if (vcn + to_alloc > asize)
1030c380b52fSKonstantin Komarov 		to_alloc = asize - vcn;
1031c380b52fSKonstantin Komarov 
1032e8b8e97fSKari Argillander 	/* Get the last LCN to allocate from. */
1033be71b5cbSKonstantin Komarov 	hint = 0;
1034be71b5cbSKonstantin Komarov 
1035be71b5cbSKonstantin Komarov 	if (vcn > evcn1) {
1036be71b5cbSKonstantin Komarov 		if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1037be71b5cbSKonstantin Komarov 				   false)) {
1038be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1039be71b5cbSKonstantin Komarov 			goto out;
1040be71b5cbSKonstantin Komarov 		}
1041be71b5cbSKonstantin Komarov 	} else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1042be71b5cbSKonstantin Komarov 		hint = -1;
1043be71b5cbSKonstantin Komarov 	}
1044be71b5cbSKonstantin Komarov 
1045c380b52fSKonstantin Komarov 	/* Allocate and zeroout new clusters. */
1046c380b52fSKonstantin Komarov 	err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
1047c380b52fSKonstantin Komarov 				     zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
1048c380b52fSKonstantin Komarov 				     fr, lcn, len);
1049be71b5cbSKonstantin Komarov 	if (err)
1050be71b5cbSKonstantin Komarov 		goto out;
1051be71b5cbSKonstantin Komarov 	*new = true;
1052910013f7SKonstantin Komarov 	step = 1;
1053be71b5cbSKonstantin Komarov 
1054c380b52fSKonstantin Komarov 	end = vcn + alen;
1055910013f7SKonstantin Komarov 	/* Save 'total_size0' to restore if error. */
1056910013f7SKonstantin Komarov 	total_size0 = le64_to_cpu(attr_b->nres.total_size);
1057910013f7SKonstantin Komarov 	total_size = total_size0 + ((u64)alen << cluster_bits);
1058c380b52fSKonstantin Komarov 
1059c380b52fSKonstantin Komarov 	if (vcn != vcn0) {
1060c380b52fSKonstantin Komarov 		if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1061c380b52fSKonstantin Komarov 			err = -EINVAL;
1062c380b52fSKonstantin Komarov 			goto out;
1063c380b52fSKonstantin Komarov 		}
1064c380b52fSKonstantin Komarov 		if (*lcn == SPARSE_LCN) {
1065c380b52fSKonstantin Komarov 			/* Internal error. Should not happened. */
1066c380b52fSKonstantin Komarov 			WARN_ON(1);
1067c380b52fSKonstantin Komarov 			err = -EINVAL;
1068c380b52fSKonstantin Komarov 			goto out;
1069c380b52fSKonstantin Komarov 		}
1070c380b52fSKonstantin Komarov 		/* Check case when vcn0 + len overlaps new allocated clusters. */
1071c380b52fSKonstantin Komarov 		if (vcn0 + *len > end)
1072c380b52fSKonstantin Komarov 			*len = end - vcn0;
1073c380b52fSKonstantin Komarov 	}
1074be71b5cbSKonstantin Komarov 
1075be71b5cbSKonstantin Komarov repack:
1076be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1077be71b5cbSKonstantin Komarov 	if (err)
1078be71b5cbSKonstantin Komarov 		goto out;
1079be71b5cbSKonstantin Komarov 
1080be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
1081be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
1082be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1083be71b5cbSKonstantin Komarov 
1084be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
1085be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
1086be71b5cbSKonstantin Komarov 
1087e8b8e97fSKari Argillander 	/* Stored [vcn : next_svcn) from [vcn : end). */
1088be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1089be71b5cbSKonstantin Komarov 
1090be71b5cbSKonstantin Komarov 	if (end <= evcn1) {
1091be71b5cbSKonstantin Komarov 		if (next_svcn == evcn1) {
1092e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
1093be71b5cbSKonstantin Komarov 			goto ok;
1094be71b5cbSKonstantin Komarov 		}
1095e8b8e97fSKari Argillander 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1096be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
1097be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
1098be71b5cbSKonstantin Komarov 			if (err)
1099910013f7SKonstantin Komarov 				goto undo1;
1100e8b8e97fSKari Argillander 			/* Layout of records is changed. */
1101be71b5cbSKonstantin Komarov 			le_b = NULL;
1102be71b5cbSKonstantin Komarov 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1103be71b5cbSKonstantin Komarov 					      0, NULL, &mi_b);
1104be71b5cbSKonstantin Komarov 			if (!attr_b) {
1105be71b5cbSKonstantin Komarov 				err = -ENOENT;
1106be71b5cbSKonstantin Komarov 				goto out;
1107be71b5cbSKonstantin Komarov 			}
1108be71b5cbSKonstantin Komarov 
1109be71b5cbSKonstantin Komarov 			attr = attr_b;
1110be71b5cbSKonstantin Komarov 			le = le_b;
1111be71b5cbSKonstantin Komarov 			mi = mi_b;
1112be71b5cbSKonstantin Komarov 			goto repack;
1113be71b5cbSKonstantin Komarov 		}
1114be71b5cbSKonstantin Komarov 	}
1115be71b5cbSKonstantin Komarov 
1116910013f7SKonstantin Komarov 	/*
1117910013f7SKonstantin Komarov 	 * The code below may require additional cluster (to extend attribute list)
1118910013f7SKonstantin Komarov 	 * and / or one MFT record
1119910013f7SKonstantin Komarov 	 * It is too complex to undo operations if -ENOSPC occurs deep inside
1120910013f7SKonstantin Komarov 	 * in 'ni_insert_nonresident'.
1121910013f7SKonstantin Komarov 	 * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
1122910013f7SKonstantin Komarov 	 */
1123910013f7SKonstantin Komarov 	if (!ntfs_check_for_free_space(sbi, 1, 1)) {
1124910013f7SKonstantin Komarov 		/* Undo step 1. */
1125910013f7SKonstantin Komarov 		err = -ENOSPC;
1126910013f7SKonstantin Komarov 		goto undo1;
1127910013f7SKonstantin Komarov 	}
1128910013f7SKonstantin Komarov 
1129910013f7SKonstantin Komarov 	step = 2;
1130be71b5cbSKonstantin Komarov 	svcn = evcn1;
1131be71b5cbSKonstantin Komarov 
1132e8b8e97fSKari Argillander 	/* Estimate next attribute. */
1133be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1134be71b5cbSKonstantin Komarov 
1135910013f7SKonstantin Komarov 	if (!attr) {
1136910013f7SKonstantin Komarov 		/* Insert new attribute segment. */
1137910013f7SKonstantin Komarov 		goto ins_ext;
1138910013f7SKonstantin Komarov 	}
1139910013f7SKonstantin Komarov 
1140910013f7SKonstantin Komarov 	/* Try to update existed attribute segment. */
1141910013f7SKonstantin Komarov 	alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
1142910013f7SKonstantin Komarov 	evcn = le64_to_cpu(attr->nres.evcn);
1143be71b5cbSKonstantin Komarov 
1144be71b5cbSKonstantin Komarov 	if (end < next_svcn)
1145be71b5cbSKonstantin Komarov 		end = next_svcn;
1146be71b5cbSKonstantin Komarov 	while (end > evcn) {
1147e8b8e97fSKari Argillander 		/* Remove segment [svcn : evcn). */
114878ab59feSKonstantin Komarov 		mi_remove_attr(NULL, mi, attr);
1149be71b5cbSKonstantin Komarov 
1150be71b5cbSKonstantin Komarov 		if (!al_remove_le(ni, le)) {
1151be71b5cbSKonstantin Komarov 			err = -EINVAL;
1152be71b5cbSKonstantin Komarov 			goto out;
1153be71b5cbSKonstantin Komarov 		}
1154be71b5cbSKonstantin Komarov 
1155be71b5cbSKonstantin Komarov 		if (evcn + 1 >= alloc) {
1156e8b8e97fSKari Argillander 			/* Last attribute segment. */
1157be71b5cbSKonstantin Komarov 			evcn1 = evcn + 1;
1158be71b5cbSKonstantin Komarov 			goto ins_ext;
1159be71b5cbSKonstantin Komarov 		}
1160be71b5cbSKonstantin Komarov 
1161be71b5cbSKonstantin Komarov 		if (ni_load_mi(ni, le, &mi)) {
1162be71b5cbSKonstantin Komarov 			attr = NULL;
1163be71b5cbSKonstantin Komarov 			goto out;
1164be71b5cbSKonstantin Komarov 		}
1165be71b5cbSKonstantin Komarov 
1166910013f7SKonstantin Komarov 		attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, &le->id);
1167be71b5cbSKonstantin Komarov 		if (!attr) {
1168be71b5cbSKonstantin Komarov 			err = -EINVAL;
1169be71b5cbSKonstantin Komarov 			goto out;
1170be71b5cbSKonstantin Komarov 		}
1171be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1172be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
1173be71b5cbSKonstantin Komarov 	}
1174be71b5cbSKonstantin Komarov 
1175be71b5cbSKonstantin Komarov 	if (end < svcn)
1176be71b5cbSKonstantin Komarov 		end = svcn;
1177be71b5cbSKonstantin Komarov 
1178be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, &end);
1179be71b5cbSKonstantin Komarov 	if (err)
1180be71b5cbSKonstantin Komarov 		goto out;
1181be71b5cbSKonstantin Komarov 
1182be71b5cbSKonstantin Komarov 	evcn1 = evcn + 1;
1183be71b5cbSKonstantin Komarov 	attr->nres.svcn = cpu_to_le64(next_svcn);
1184be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1185be71b5cbSKonstantin Komarov 	if (err)
1186be71b5cbSKonstantin Komarov 		goto out;
1187be71b5cbSKonstantin Komarov 
1188be71b5cbSKonstantin Komarov 	le->vcn = cpu_to_le64(next_svcn);
1189be71b5cbSKonstantin Komarov 	ni->attr_list.dirty = true;
1190be71b5cbSKonstantin Komarov 	mi->dirty = true;
1191be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1192910013f7SKonstantin Komarov 
1193be71b5cbSKonstantin Komarov ins_ext:
1194be71b5cbSKonstantin Komarov 	if (evcn1 > next_svcn) {
1195be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1196be71b5cbSKonstantin Komarov 					    next_svcn, evcn1 - next_svcn,
1197c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
1198be71b5cbSKonstantin Komarov 		if (err)
1199be71b5cbSKonstantin Komarov 			goto out;
1200be71b5cbSKonstantin Komarov 	}
1201be71b5cbSKonstantin Komarov ok:
1202be71b5cbSKonstantin Komarov 	run_truncate_around(run, vcn);
1203be71b5cbSKonstantin Komarov out:
1204910013f7SKonstantin Komarov 	if (err && step > 1) {
1205910013f7SKonstantin Komarov 		/* Too complex to restore. */
1206910013f7SKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
1207910013f7SKonstantin Komarov 	}
1208be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
1209be71b5cbSKonstantin Komarov 	ni_unlock(ni);
1210be71b5cbSKonstantin Komarov 
1211be71b5cbSKonstantin Komarov 	return err;
1212910013f7SKonstantin Komarov 
1213910013f7SKonstantin Komarov undo1:
1214910013f7SKonstantin Komarov 	/* Undo step1. */
1215910013f7SKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size0);
1216910013f7SKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size0);
1217910013f7SKonstantin Komarov 
1218910013f7SKonstantin Komarov 	if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) ||
1219910013f7SKonstantin Komarov 	    !run_add_entry(run, vcn, SPARSE_LCN, alen, false) ||
1220910013f7SKonstantin Komarov 	    mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
1221910013f7SKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
1222910013f7SKonstantin Komarov 	}
1223910013f7SKonstantin Komarov 	goto out;
1224be71b5cbSKonstantin Komarov }
1225be71b5cbSKonstantin Komarov 
attr_data_read_resident(struct ntfs_inode * ni,struct page * page)1226be71b5cbSKonstantin Komarov int attr_data_read_resident(struct ntfs_inode *ni, struct page *page)
1227be71b5cbSKonstantin Komarov {
1228be71b5cbSKonstantin Komarov 	u64 vbo;
1229be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1230be71b5cbSKonstantin Komarov 	u32 data_size;
1231be71b5cbSKonstantin Komarov 
1232be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1233be71b5cbSKonstantin Komarov 	if (!attr)
1234be71b5cbSKonstantin Komarov 		return -EINVAL;
1235be71b5cbSKonstantin Komarov 
1236be71b5cbSKonstantin Komarov 	if (attr->non_res)
1237be71b5cbSKonstantin Komarov 		return E_NTFS_NONRESIDENT;
1238be71b5cbSKonstantin Komarov 
1239be71b5cbSKonstantin Komarov 	vbo = page->index << PAGE_SHIFT;
1240be71b5cbSKonstantin Komarov 	data_size = le32_to_cpu(attr->res.data_size);
1241be71b5cbSKonstantin Komarov 	if (vbo < data_size) {
1242be71b5cbSKonstantin Komarov 		const char *data = resident_data(attr);
1243be71b5cbSKonstantin Komarov 		char *kaddr = kmap_atomic(page);
1244be71b5cbSKonstantin Komarov 		u32 use = data_size - vbo;
1245be71b5cbSKonstantin Komarov 
1246be71b5cbSKonstantin Komarov 		if (use > PAGE_SIZE)
1247be71b5cbSKonstantin Komarov 			use = PAGE_SIZE;
1248be71b5cbSKonstantin Komarov 
1249be71b5cbSKonstantin Komarov 		memcpy(kaddr, data + vbo, use);
1250be71b5cbSKonstantin Komarov 		memset(kaddr + use, 0, PAGE_SIZE - use);
1251be71b5cbSKonstantin Komarov 		kunmap_atomic(kaddr);
1252be71b5cbSKonstantin Komarov 		flush_dcache_page(page);
1253be71b5cbSKonstantin Komarov 		SetPageUptodate(page);
1254be71b5cbSKonstantin Komarov 	} else if (!PageUptodate(page)) {
1255be71b5cbSKonstantin Komarov 		zero_user_segment(page, 0, PAGE_SIZE);
1256be71b5cbSKonstantin Komarov 		SetPageUptodate(page);
1257be71b5cbSKonstantin Komarov 	}
1258be71b5cbSKonstantin Komarov 
1259be71b5cbSKonstantin Komarov 	return 0;
1260be71b5cbSKonstantin Komarov }
1261be71b5cbSKonstantin Komarov 
attr_data_write_resident(struct ntfs_inode * ni,struct page * page)1262be71b5cbSKonstantin Komarov int attr_data_write_resident(struct ntfs_inode *ni, struct page *page)
1263be71b5cbSKonstantin Komarov {
1264be71b5cbSKonstantin Komarov 	u64 vbo;
1265be71b5cbSKonstantin Komarov 	struct mft_inode *mi;
1266be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1267be71b5cbSKonstantin Komarov 	u32 data_size;
1268be71b5cbSKonstantin Komarov 
1269be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1270be71b5cbSKonstantin Komarov 	if (!attr)
1271be71b5cbSKonstantin Komarov 		return -EINVAL;
1272be71b5cbSKonstantin Komarov 
1273be71b5cbSKonstantin Komarov 	if (attr->non_res) {
1274e8b8e97fSKari Argillander 		/* Return special error code to check this case. */
1275be71b5cbSKonstantin Komarov 		return E_NTFS_NONRESIDENT;
1276be71b5cbSKonstantin Komarov 	}
1277be71b5cbSKonstantin Komarov 
1278be71b5cbSKonstantin Komarov 	vbo = page->index << PAGE_SHIFT;
1279be71b5cbSKonstantin Komarov 	data_size = le32_to_cpu(attr->res.data_size);
1280be71b5cbSKonstantin Komarov 	if (vbo < data_size) {
1281be71b5cbSKonstantin Komarov 		char *data = resident_data(attr);
1282be71b5cbSKonstantin Komarov 		char *kaddr = kmap_atomic(page);
1283be71b5cbSKonstantin Komarov 		u32 use = data_size - vbo;
1284be71b5cbSKonstantin Komarov 
1285be71b5cbSKonstantin Komarov 		if (use > PAGE_SIZE)
1286be71b5cbSKonstantin Komarov 			use = PAGE_SIZE;
1287be71b5cbSKonstantin Komarov 		memcpy(data + vbo, kaddr, use);
1288be71b5cbSKonstantin Komarov 		kunmap_atomic(kaddr);
1289be71b5cbSKonstantin Komarov 		mi->dirty = true;
1290be71b5cbSKonstantin Komarov 	}
1291be71b5cbSKonstantin Komarov 	ni->i_valid = data_size;
1292be71b5cbSKonstantin Komarov 
1293be71b5cbSKonstantin Komarov 	return 0;
1294be71b5cbSKonstantin Komarov }
1295be71b5cbSKonstantin Komarov 
1296be71b5cbSKonstantin Komarov /*
1297e8b8e97fSKari Argillander  * attr_load_runs_vcn - Load runs with VCN.
1298be71b5cbSKonstantin Komarov  */
attr_load_runs_vcn(struct ntfs_inode * ni,enum ATTR_TYPE type,const __le16 * name,u8 name_len,struct runs_tree * run,CLST vcn)1299be71b5cbSKonstantin Komarov int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1300be71b5cbSKonstantin Komarov 		       const __le16 *name, u8 name_len, struct runs_tree *run,
1301be71b5cbSKonstantin Komarov 		       CLST vcn)
1302be71b5cbSKonstantin Komarov {
1303be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1304be71b5cbSKonstantin Komarov 	int err;
1305be71b5cbSKonstantin Komarov 	CLST svcn, evcn;
1306be71b5cbSKonstantin Komarov 	u16 ro;
1307be71b5cbSKonstantin Komarov 
13082681631cSEdward Lo 	if (!ni) {
13092681631cSEdward Lo 		/* Is record corrupted? */
13102681631cSEdward Lo 		return -ENOENT;
13112681631cSEdward Lo 	}
13122681631cSEdward Lo 
1313be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1314d3624466SKonstantin Komarov 	if (!attr) {
1315d3624466SKonstantin Komarov 		/* Is record corrupted? */
1316be71b5cbSKonstantin Komarov 		return -ENOENT;
1317d3624466SKonstantin Komarov 	}
1318be71b5cbSKonstantin Komarov 
1319be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr->nres.svcn);
1320be71b5cbSKonstantin Komarov 	evcn = le64_to_cpu(attr->nres.evcn);
1321be71b5cbSKonstantin Komarov 
1322d3624466SKonstantin Komarov 	if (evcn < vcn || vcn < svcn) {
1323d3624466SKonstantin Komarov 		/* Is record corrupted? */
1324be71b5cbSKonstantin Komarov 		return -EINVAL;
1325d3624466SKonstantin Komarov 	}
1326be71b5cbSKonstantin Komarov 
1327be71b5cbSKonstantin Komarov 	ro = le16_to_cpu(attr->nres.run_off);
13286db62086SEdward Lo 
13296db62086SEdward Lo 	if (ro > le32_to_cpu(attr->size))
13306db62086SEdward Lo 		return -EINVAL;
13316db62086SEdward Lo 
1332be71b5cbSKonstantin Komarov 	err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1333be71b5cbSKonstantin Komarov 			    Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1334be71b5cbSKonstantin Komarov 	if (err < 0)
1335be71b5cbSKonstantin Komarov 		return err;
1336be71b5cbSKonstantin Komarov 	return 0;
1337be71b5cbSKonstantin Komarov }
1338be71b5cbSKonstantin Komarov 
1339be71b5cbSKonstantin Komarov /*
1340d3624466SKonstantin Komarov  * attr_load_runs_range - Load runs for given range [from to).
1341be71b5cbSKonstantin Komarov  */
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)1342be71b5cbSKonstantin Komarov int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1343be71b5cbSKonstantin Komarov 			 const __le16 *name, u8 name_len, struct runs_tree *run,
1344be71b5cbSKonstantin Komarov 			 u64 from, u64 to)
1345be71b5cbSKonstantin Komarov {
1346be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1347be71b5cbSKonstantin Komarov 	u8 cluster_bits = sbi->cluster_bits;
1348dc8965abSColin Ian King 	CLST vcn;
1349be71b5cbSKonstantin Komarov 	CLST vcn_last = (to - 1) >> cluster_bits;
1350be71b5cbSKonstantin Komarov 	CLST lcn, clen;
1351be71b5cbSKonstantin Komarov 	int err;
1352be71b5cbSKonstantin Komarov 
1353be71b5cbSKonstantin Komarov 	for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1354be71b5cbSKonstantin Komarov 		if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1355be71b5cbSKonstantin Komarov 			err = attr_load_runs_vcn(ni, type, name, name_len, run,
1356be71b5cbSKonstantin Komarov 						 vcn);
1357be71b5cbSKonstantin Komarov 			if (err)
1358be71b5cbSKonstantin Komarov 				return err;
1359e8b8e97fSKari Argillander 			clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1360be71b5cbSKonstantin Komarov 		}
1361be71b5cbSKonstantin Komarov 	}
1362be71b5cbSKonstantin Komarov 
1363be71b5cbSKonstantin Komarov 	return 0;
1364be71b5cbSKonstantin Komarov }
1365be71b5cbSKonstantin Komarov 
1366be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
1367be71b5cbSKonstantin Komarov /*
1368be71b5cbSKonstantin Komarov  * attr_wof_frame_info
1369be71b5cbSKonstantin Komarov  *
1370e8b8e97fSKari Argillander  * Read header of Xpress/LZX file to get info about frame.
1371be71b5cbSKonstantin Komarov  */
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)1372be71b5cbSKonstantin Komarov int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1373be71b5cbSKonstantin Komarov 			struct runs_tree *run, u64 frame, u64 frames,
1374be71b5cbSKonstantin Komarov 			u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1375be71b5cbSKonstantin Komarov {
1376be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1377be71b5cbSKonstantin Komarov 	u64 vbo[2], off[2], wof_size;
1378be71b5cbSKonstantin Komarov 	u32 voff;
1379be71b5cbSKonstantin Komarov 	u8 bytes_per_off;
1380be71b5cbSKonstantin Komarov 	char *addr;
1381be71b5cbSKonstantin Komarov 	struct page *page;
1382be71b5cbSKonstantin Komarov 	int i, err;
1383be71b5cbSKonstantin Komarov 	__le32 *off32;
1384be71b5cbSKonstantin Komarov 	__le64 *off64;
1385be71b5cbSKonstantin Komarov 
1386be71b5cbSKonstantin Komarov 	if (ni->vfs_inode.i_size < 0x100000000ull) {
1387e8b8e97fSKari Argillander 		/* File starts with array of 32 bit offsets. */
1388be71b5cbSKonstantin Komarov 		bytes_per_off = sizeof(__le32);
1389be71b5cbSKonstantin Komarov 		vbo[1] = frame << 2;
1390be71b5cbSKonstantin Komarov 		*vbo_data = frames << 2;
1391be71b5cbSKonstantin Komarov 	} else {
1392e8b8e97fSKari Argillander 		/* File starts with array of 64 bit offsets. */
1393be71b5cbSKonstantin Komarov 		bytes_per_off = sizeof(__le64);
1394be71b5cbSKonstantin Komarov 		vbo[1] = frame << 3;
1395be71b5cbSKonstantin Komarov 		*vbo_data = frames << 3;
1396be71b5cbSKonstantin Komarov 	}
1397be71b5cbSKonstantin Komarov 
1398be71b5cbSKonstantin Komarov 	/*
1399e8b8e97fSKari Argillander 	 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1400e8b8e97fSKari Argillander 	 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1401be71b5cbSKonstantin Komarov 	 */
1402be71b5cbSKonstantin Komarov 	if (!attr->non_res) {
1403be71b5cbSKonstantin Komarov 		if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1404be71b5cbSKonstantin Komarov 			ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1405be71b5cbSKonstantin Komarov 			return -EINVAL;
1406be71b5cbSKonstantin Komarov 		}
1407be71b5cbSKonstantin Komarov 		addr = resident_data(attr);
1408be71b5cbSKonstantin Komarov 
1409be71b5cbSKonstantin Komarov 		if (bytes_per_off == sizeof(__le32)) {
1410be71b5cbSKonstantin Komarov 			off32 = Add2Ptr(addr, vbo[1]);
1411be71b5cbSKonstantin Komarov 			off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1412be71b5cbSKonstantin Komarov 			off[1] = le32_to_cpu(off32[0]);
1413be71b5cbSKonstantin Komarov 		} else {
1414be71b5cbSKonstantin Komarov 			off64 = Add2Ptr(addr, vbo[1]);
1415be71b5cbSKonstantin Komarov 			off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1416be71b5cbSKonstantin Komarov 			off[1] = le64_to_cpu(off64[0]);
1417be71b5cbSKonstantin Komarov 		}
1418be71b5cbSKonstantin Komarov 
1419be71b5cbSKonstantin Komarov 		*vbo_data += off[0];
1420be71b5cbSKonstantin Komarov 		*ondisk_size = off[1] - off[0];
1421be71b5cbSKonstantin Komarov 		return 0;
1422be71b5cbSKonstantin Komarov 	}
1423be71b5cbSKonstantin Komarov 
1424be71b5cbSKonstantin Komarov 	wof_size = le64_to_cpu(attr->nres.data_size);
1425be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
1426be71b5cbSKonstantin Komarov 	page = ni->file.offs_page;
1427be71b5cbSKonstantin Komarov 	if (!page) {
1428be71b5cbSKonstantin Komarov 		page = alloc_page(GFP_KERNEL);
1429be71b5cbSKonstantin Komarov 		if (!page) {
1430be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1431be71b5cbSKonstantin Komarov 			goto out;
1432be71b5cbSKonstantin Komarov 		}
1433be71b5cbSKonstantin Komarov 		page->index = -1;
1434be71b5cbSKonstantin Komarov 		ni->file.offs_page = page;
1435be71b5cbSKonstantin Komarov 	}
1436be71b5cbSKonstantin Komarov 	lock_page(page);
1437be71b5cbSKonstantin Komarov 	addr = page_address(page);
1438be71b5cbSKonstantin Komarov 
1439be71b5cbSKonstantin Komarov 	if (vbo[1]) {
1440be71b5cbSKonstantin Komarov 		voff = vbo[1] & (PAGE_SIZE - 1);
1441be71b5cbSKonstantin Komarov 		vbo[0] = vbo[1] - bytes_per_off;
1442be71b5cbSKonstantin Komarov 		i = 0;
1443be71b5cbSKonstantin Komarov 	} else {
1444be71b5cbSKonstantin Komarov 		voff = 0;
1445be71b5cbSKonstantin Komarov 		vbo[0] = 0;
1446be71b5cbSKonstantin Komarov 		off[0] = 0;
1447be71b5cbSKonstantin Komarov 		i = 1;
1448be71b5cbSKonstantin Komarov 	}
1449be71b5cbSKonstantin Komarov 
1450be71b5cbSKonstantin Komarov 	do {
1451be71b5cbSKonstantin Komarov 		pgoff_t index = vbo[i] >> PAGE_SHIFT;
1452be71b5cbSKonstantin Komarov 
1453be71b5cbSKonstantin Komarov 		if (index != page->index) {
1454be71b5cbSKonstantin Komarov 			u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1455be71b5cbSKonstantin Komarov 			u64 to = min(from + PAGE_SIZE, wof_size);
1456be71b5cbSKonstantin Komarov 
1457be71b5cbSKonstantin Komarov 			err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1458be71b5cbSKonstantin Komarov 						   ARRAY_SIZE(WOF_NAME), run,
1459be71b5cbSKonstantin Komarov 						   from, to);
1460be71b5cbSKonstantin Komarov 			if (err)
1461be71b5cbSKonstantin Komarov 				goto out1;
1462be71b5cbSKonstantin Komarov 
1463be71b5cbSKonstantin Komarov 			err = ntfs_bio_pages(sbi, run, &page, 1, from,
1464be71b5cbSKonstantin Komarov 					     to - from, REQ_OP_READ);
1465be71b5cbSKonstantin Komarov 			if (err) {
1466be71b5cbSKonstantin Komarov 				page->index = -1;
1467be71b5cbSKonstantin Komarov 				goto out1;
1468be71b5cbSKonstantin Komarov 			}
1469be71b5cbSKonstantin Komarov 			page->index = index;
1470be71b5cbSKonstantin Komarov 		}
1471be71b5cbSKonstantin Komarov 
1472be71b5cbSKonstantin Komarov 		if (i) {
1473be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1474be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, voff);
1475be71b5cbSKonstantin Komarov 				off[1] = le32_to_cpu(*off32);
1476be71b5cbSKonstantin Komarov 			} else {
1477be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, voff);
1478be71b5cbSKonstantin Komarov 				off[1] = le64_to_cpu(*off64);
1479be71b5cbSKonstantin Komarov 			}
1480be71b5cbSKonstantin Komarov 		} else if (!voff) {
1481be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1482be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1483be71b5cbSKonstantin Komarov 				off[0] = le32_to_cpu(*off32);
1484be71b5cbSKonstantin Komarov 			} else {
1485be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1486be71b5cbSKonstantin Komarov 				off[0] = le64_to_cpu(*off64);
1487be71b5cbSKonstantin Komarov 			}
1488be71b5cbSKonstantin Komarov 		} else {
1489e8b8e97fSKari Argillander 			/* Two values in one page. */
1490be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1491be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, voff);
1492be71b5cbSKonstantin Komarov 				off[0] = le32_to_cpu(off32[-1]);
1493be71b5cbSKonstantin Komarov 				off[1] = le32_to_cpu(off32[0]);
1494be71b5cbSKonstantin Komarov 			} else {
1495be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, voff);
1496be71b5cbSKonstantin Komarov 				off[0] = le64_to_cpu(off64[-1]);
1497be71b5cbSKonstantin Komarov 				off[1] = le64_to_cpu(off64[0]);
1498be71b5cbSKonstantin Komarov 			}
1499be71b5cbSKonstantin Komarov 			break;
1500be71b5cbSKonstantin Komarov 		}
1501be71b5cbSKonstantin Komarov 	} while (++i < 2);
1502be71b5cbSKonstantin Komarov 
1503be71b5cbSKonstantin Komarov 	*vbo_data += off[0];
1504be71b5cbSKonstantin Komarov 	*ondisk_size = off[1] - off[0];
1505be71b5cbSKonstantin Komarov 
1506be71b5cbSKonstantin Komarov out1:
1507be71b5cbSKonstantin Komarov 	unlock_page(page);
1508be71b5cbSKonstantin Komarov out:
1509be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
1510be71b5cbSKonstantin Komarov 	return err;
1511be71b5cbSKonstantin Komarov }
1512be71b5cbSKonstantin Komarov #endif
1513be71b5cbSKonstantin Komarov 
1514be71b5cbSKonstantin Komarov /*
1515e8b8e97fSKari Argillander  * attr_is_frame_compressed - Used to detect compressed frame.
1516be71b5cbSKonstantin Komarov  */
attr_is_frame_compressed(struct ntfs_inode * ni,struct ATTRIB * attr,CLST frame,CLST * clst_data)1517be71b5cbSKonstantin Komarov int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1518be71b5cbSKonstantin Komarov 			     CLST frame, CLST *clst_data)
1519be71b5cbSKonstantin Komarov {
1520be71b5cbSKonstantin Komarov 	int err;
1521be71b5cbSKonstantin Komarov 	u32 clst_frame;
1522be71b5cbSKonstantin Komarov 	CLST clen, lcn, vcn, alen, slen, vcn_next;
1523be71b5cbSKonstantin Komarov 	size_t idx;
1524be71b5cbSKonstantin Komarov 	struct runs_tree *run;
1525be71b5cbSKonstantin Komarov 
1526be71b5cbSKonstantin Komarov 	*clst_data = 0;
1527be71b5cbSKonstantin Komarov 
1528be71b5cbSKonstantin Komarov 	if (!is_attr_compressed(attr))
1529be71b5cbSKonstantin Komarov 		return 0;
1530be71b5cbSKonstantin Komarov 
1531be71b5cbSKonstantin Komarov 	if (!attr->non_res)
1532be71b5cbSKonstantin Komarov 		return 0;
1533be71b5cbSKonstantin Komarov 
1534be71b5cbSKonstantin Komarov 	clst_frame = 1u << attr->nres.c_unit;
1535be71b5cbSKonstantin Komarov 	vcn = frame * clst_frame;
1536be71b5cbSKonstantin Komarov 	run = &ni->file.run;
1537be71b5cbSKonstantin Komarov 
1538be71b5cbSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1539be71b5cbSKonstantin Komarov 		err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1540be71b5cbSKonstantin Komarov 					 attr->name_len, run, vcn);
1541be71b5cbSKonstantin Komarov 		if (err)
1542be71b5cbSKonstantin Komarov 			return err;
1543be71b5cbSKonstantin Komarov 
1544be71b5cbSKonstantin Komarov 		if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1545be71b5cbSKonstantin Komarov 			return -EINVAL;
1546be71b5cbSKonstantin Komarov 	}
1547be71b5cbSKonstantin Komarov 
1548be71b5cbSKonstantin Komarov 	if (lcn == SPARSE_LCN) {
1549e8b8e97fSKari Argillander 		/* Sparsed frame. */
1550be71b5cbSKonstantin Komarov 		return 0;
1551be71b5cbSKonstantin Komarov 	}
1552be71b5cbSKonstantin Komarov 
1553be71b5cbSKonstantin Komarov 	if (clen >= clst_frame) {
1554be71b5cbSKonstantin Komarov 		/*
1555be71b5cbSKonstantin Komarov 		 * The frame is not compressed 'cause
1556e8b8e97fSKari Argillander 		 * it does not contain any sparse clusters.
1557be71b5cbSKonstantin Komarov 		 */
1558be71b5cbSKonstantin Komarov 		*clst_data = clst_frame;
1559be71b5cbSKonstantin Komarov 		return 0;
1560be71b5cbSKonstantin Komarov 	}
1561be71b5cbSKonstantin Komarov 
1562be71b5cbSKonstantin Komarov 	alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1563be71b5cbSKonstantin Komarov 	slen = 0;
1564be71b5cbSKonstantin Komarov 	*clst_data = clen;
1565be71b5cbSKonstantin Komarov 
1566be71b5cbSKonstantin Komarov 	/*
1567e8b8e97fSKari Argillander 	 * The frame is compressed if *clst_data + slen >= clst_frame.
1568e8b8e97fSKari Argillander 	 * Check next fragments.
1569be71b5cbSKonstantin Komarov 	 */
1570be71b5cbSKonstantin Komarov 	while ((vcn += clen) < alen) {
1571be71b5cbSKonstantin Komarov 		vcn_next = vcn;
1572be71b5cbSKonstantin Komarov 
1573be71b5cbSKonstantin Komarov 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1574be71b5cbSKonstantin Komarov 		    vcn_next != vcn) {
1575be71b5cbSKonstantin Komarov 			err = attr_load_runs_vcn(ni, attr->type,
1576be71b5cbSKonstantin Komarov 						 attr_name(attr),
1577be71b5cbSKonstantin Komarov 						 attr->name_len, run, vcn_next);
1578be71b5cbSKonstantin Komarov 			if (err)
1579be71b5cbSKonstantin Komarov 				return err;
1580be71b5cbSKonstantin Komarov 			vcn = vcn_next;
1581be71b5cbSKonstantin Komarov 
1582be71b5cbSKonstantin Komarov 			if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1583be71b5cbSKonstantin Komarov 				return -EINVAL;
1584be71b5cbSKonstantin Komarov 		}
1585be71b5cbSKonstantin Komarov 
1586be71b5cbSKonstantin Komarov 		if (lcn == SPARSE_LCN) {
1587be71b5cbSKonstantin Komarov 			slen += clen;
1588be71b5cbSKonstantin Komarov 		} else {
1589be71b5cbSKonstantin Komarov 			if (slen) {
1590be71b5cbSKonstantin Komarov 				/*
1591e8b8e97fSKari Argillander 				 * Data_clusters + sparse_clusters =
1592e8b8e97fSKari Argillander 				 * not enough for frame.
1593be71b5cbSKonstantin Komarov 				 */
1594be71b5cbSKonstantin Komarov 				return -EINVAL;
1595be71b5cbSKonstantin Komarov 			}
1596be71b5cbSKonstantin Komarov 			*clst_data += clen;
1597be71b5cbSKonstantin Komarov 		}
1598be71b5cbSKonstantin Komarov 
1599be71b5cbSKonstantin Komarov 		if (*clst_data + slen >= clst_frame) {
1600be71b5cbSKonstantin Komarov 			if (!slen) {
1601be71b5cbSKonstantin Komarov 				/*
1602be71b5cbSKonstantin Komarov 				 * There is no sparsed clusters in this frame
1603e8b8e97fSKari Argillander 				 * so it is not compressed.
1604be71b5cbSKonstantin Komarov 				 */
1605be71b5cbSKonstantin Komarov 				*clst_data = clst_frame;
1606be71b5cbSKonstantin Komarov 			} else {
1607e8b8e97fSKari Argillander 				/* Frame is compressed. */
1608be71b5cbSKonstantin Komarov 			}
1609be71b5cbSKonstantin Komarov 			break;
1610be71b5cbSKonstantin Komarov 		}
1611be71b5cbSKonstantin Komarov 	}
1612be71b5cbSKonstantin Komarov 
1613be71b5cbSKonstantin Komarov 	return 0;
1614be71b5cbSKonstantin Komarov }
1615be71b5cbSKonstantin Komarov 
1616be71b5cbSKonstantin Komarov /*
1617e8b8e97fSKari Argillander  * attr_allocate_frame - Allocate/free clusters for @frame.
1618be71b5cbSKonstantin Komarov  *
1619e8b8e97fSKari Argillander  * Assumed: down_write(&ni->file.run_lock);
1620be71b5cbSKonstantin Komarov  */
attr_allocate_frame(struct ntfs_inode * ni,CLST frame,size_t compr_size,u64 new_valid)1621be71b5cbSKonstantin Komarov int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1622be71b5cbSKonstantin Komarov 			u64 new_valid)
1623be71b5cbSKonstantin Komarov {
1624be71b5cbSKonstantin Komarov 	int err = 0;
1625be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
1626be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1627be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
1628be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
1629be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
1630c380b52fSKonstantin Komarov 	CLST svcn, evcn1, next_svcn, len;
1631be71b5cbSKonstantin Komarov 	CLST vcn, end, clst_data;
1632be71b5cbSKonstantin Komarov 	u64 total_size, valid_size, data_size;
1633be71b5cbSKonstantin Komarov 
1634be71b5cbSKonstantin Komarov 	le_b = NULL;
1635be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1636be71b5cbSKonstantin Komarov 	if (!attr_b)
1637be71b5cbSKonstantin Komarov 		return -ENOENT;
1638be71b5cbSKonstantin Komarov 
1639be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b))
1640be71b5cbSKonstantin Komarov 		return -EINVAL;
1641be71b5cbSKonstantin Komarov 
1642be71b5cbSKonstantin Komarov 	vcn = frame << NTFS_LZNT_CUNIT;
1643be71b5cbSKonstantin Komarov 	total_size = le64_to_cpu(attr_b->nres.total_size);
1644be71b5cbSKonstantin Komarov 
1645be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
1646be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1647be71b5cbSKonstantin Komarov 	data_size = le64_to_cpu(attr_b->nres.data_size);
1648be71b5cbSKonstantin Komarov 
1649be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
1650be71b5cbSKonstantin Komarov 		attr = attr_b;
1651be71b5cbSKonstantin Komarov 		le = le_b;
1652be71b5cbSKonstantin Komarov 		mi = mi_b;
1653be71b5cbSKonstantin Komarov 	} else if (!le_b) {
1654be71b5cbSKonstantin Komarov 		err = -EINVAL;
1655be71b5cbSKonstantin Komarov 		goto out;
1656be71b5cbSKonstantin Komarov 	} else {
1657be71b5cbSKonstantin Komarov 		le = le_b;
1658be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1659be71b5cbSKonstantin Komarov 				    &mi);
1660be71b5cbSKonstantin Komarov 		if (!attr) {
1661be71b5cbSKonstantin Komarov 			err = -EINVAL;
1662be71b5cbSKonstantin Komarov 			goto out;
1663be71b5cbSKonstantin Komarov 		}
1664be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1665be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1666be71b5cbSKonstantin Komarov 	}
1667be71b5cbSKonstantin Komarov 
1668be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
1669be71b5cbSKonstantin Komarov 	if (err)
1670be71b5cbSKonstantin Komarov 		goto out;
1671be71b5cbSKonstantin Komarov 
1672be71b5cbSKonstantin Komarov 	err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data);
1673be71b5cbSKonstantin Komarov 	if (err)
1674be71b5cbSKonstantin Komarov 		goto out;
1675be71b5cbSKonstantin Komarov 
1676be71b5cbSKonstantin Komarov 	total_size -= (u64)clst_data << sbi->cluster_bits;
1677be71b5cbSKonstantin Komarov 
1678be71b5cbSKonstantin Komarov 	len = bytes_to_cluster(sbi, compr_size);
1679be71b5cbSKonstantin Komarov 
1680be71b5cbSKonstantin Komarov 	if (len == clst_data)
1681be71b5cbSKonstantin Komarov 		goto out;
1682be71b5cbSKonstantin Komarov 
1683be71b5cbSKonstantin Komarov 	if (len < clst_data) {
1684be71b5cbSKonstantin Komarov 		err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1685be71b5cbSKonstantin Komarov 					NULL, true);
1686be71b5cbSKonstantin Komarov 		if (err)
1687be71b5cbSKonstantin Komarov 			goto out;
1688be71b5cbSKonstantin Komarov 
1689be71b5cbSKonstantin Komarov 		if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1690be71b5cbSKonstantin Komarov 				   false)) {
1691be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1692be71b5cbSKonstantin Komarov 			goto out;
1693be71b5cbSKonstantin Komarov 		}
1694be71b5cbSKonstantin Komarov 		end = vcn + clst_data;
1695e8b8e97fSKari Argillander 		/* Run contains updated range [vcn + len : end). */
1696be71b5cbSKonstantin Komarov 	} else {
1697be71b5cbSKonstantin Komarov 		CLST alen, hint = 0;
1698e8b8e97fSKari Argillander 		/* Get the last LCN to allocate from. */
1699be71b5cbSKonstantin Komarov 		if (vcn + clst_data &&
1700be71b5cbSKonstantin Komarov 		    !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1701be71b5cbSKonstantin Komarov 				      NULL)) {
1702be71b5cbSKonstantin Komarov 			hint = -1;
1703be71b5cbSKonstantin Komarov 		}
1704be71b5cbSKonstantin Komarov 
1705be71b5cbSKonstantin Komarov 		err = attr_allocate_clusters(sbi, run, vcn + clst_data,
1706c380b52fSKonstantin Komarov 					     hint + 1, len - clst_data, NULL,
1707c380b52fSKonstantin Komarov 					     ALLOCATE_DEF, &alen, 0, NULL,
1708c380b52fSKonstantin Komarov 					     NULL);
1709be71b5cbSKonstantin Komarov 		if (err)
1710be71b5cbSKonstantin Komarov 			goto out;
1711be71b5cbSKonstantin Komarov 
1712be71b5cbSKonstantin Komarov 		end = vcn + len;
1713e8b8e97fSKari Argillander 		/* Run contains updated range [vcn + clst_data : end). */
1714be71b5cbSKonstantin Komarov 	}
1715be71b5cbSKonstantin Komarov 
1716be71b5cbSKonstantin Komarov 	total_size += (u64)len << sbi->cluster_bits;
1717be71b5cbSKonstantin Komarov 
1718be71b5cbSKonstantin Komarov repack:
1719be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1720be71b5cbSKonstantin Komarov 	if (err)
1721be71b5cbSKonstantin Komarov 		goto out;
1722be71b5cbSKonstantin Komarov 
1723be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
1724be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
1725be71b5cbSKonstantin Komarov 
1726be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
1727be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
1728be71b5cbSKonstantin Komarov 
1729e8b8e97fSKari Argillander 	/* Stored [vcn : next_svcn) from [vcn : end). */
1730be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1731be71b5cbSKonstantin Komarov 
1732be71b5cbSKonstantin Komarov 	if (end <= evcn1) {
1733be71b5cbSKonstantin Komarov 		if (next_svcn == evcn1) {
1734e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
1735be71b5cbSKonstantin Komarov 			goto ok;
1736be71b5cbSKonstantin Komarov 		}
1737e8b8e97fSKari Argillander 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1738be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
1739be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
1740be71b5cbSKonstantin Komarov 			if (err)
1741be71b5cbSKonstantin Komarov 				goto out;
1742e8b8e97fSKari Argillander 			/* Layout of records is changed. */
1743be71b5cbSKonstantin Komarov 			le_b = NULL;
1744be71b5cbSKonstantin Komarov 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1745be71b5cbSKonstantin Komarov 					      0, NULL, &mi_b);
1746aaab47f2SKonstantin Komarov 			if (!attr_b) {
1747aaab47f2SKonstantin Komarov 				err = -ENOENT;
1748aaab47f2SKonstantin Komarov 				goto out;
1749aaab47f2SKonstantin Komarov 			}
1750be71b5cbSKonstantin Komarov 
1751be71b5cbSKonstantin Komarov 			attr = attr_b;
1752be71b5cbSKonstantin Komarov 			le = le_b;
1753be71b5cbSKonstantin Komarov 			mi = mi_b;
1754be71b5cbSKonstantin Komarov 			goto repack;
1755be71b5cbSKonstantin Komarov 		}
1756be71b5cbSKonstantin Komarov 	}
1757be71b5cbSKonstantin Komarov 
1758be71b5cbSKonstantin Komarov 	svcn = evcn1;
1759be71b5cbSKonstantin Komarov 
1760e8b8e97fSKari Argillander 	/* Estimate next attribute. */
1761be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1762be71b5cbSKonstantin Komarov 
1763be71b5cbSKonstantin Komarov 	if (attr) {
1764be71b5cbSKonstantin Komarov 		CLST alloc = bytes_to_cluster(
1765be71b5cbSKonstantin Komarov 			sbi, le64_to_cpu(attr_b->nres.alloc_size));
1766be71b5cbSKonstantin Komarov 		CLST evcn = le64_to_cpu(attr->nres.evcn);
1767be71b5cbSKonstantin Komarov 
1768be71b5cbSKonstantin Komarov 		if (end < next_svcn)
1769be71b5cbSKonstantin Komarov 			end = next_svcn;
1770be71b5cbSKonstantin Komarov 		while (end > evcn) {
1771e8b8e97fSKari Argillander 			/* Remove segment [svcn : evcn). */
177278ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
1773be71b5cbSKonstantin Komarov 
1774be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
1775be71b5cbSKonstantin Komarov 				err = -EINVAL;
1776be71b5cbSKonstantin Komarov 				goto out;
1777be71b5cbSKonstantin Komarov 			}
1778be71b5cbSKonstantin Komarov 
1779be71b5cbSKonstantin Komarov 			if (evcn + 1 >= alloc) {
1780e8b8e97fSKari Argillander 				/* Last attribute segment. */
1781be71b5cbSKonstantin Komarov 				evcn1 = evcn + 1;
1782be71b5cbSKonstantin Komarov 				goto ins_ext;
1783be71b5cbSKonstantin Komarov 			}
1784be71b5cbSKonstantin Komarov 
1785be71b5cbSKonstantin Komarov 			if (ni_load_mi(ni, le, &mi)) {
1786be71b5cbSKonstantin Komarov 				attr = NULL;
1787be71b5cbSKonstantin Komarov 				goto out;
1788be71b5cbSKonstantin Komarov 			}
1789be71b5cbSKonstantin Komarov 
1790be71b5cbSKonstantin Komarov 			attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1791be71b5cbSKonstantin Komarov 					    &le->id);
1792be71b5cbSKonstantin Komarov 			if (!attr) {
1793be71b5cbSKonstantin Komarov 				err = -EINVAL;
1794be71b5cbSKonstantin Komarov 				goto out;
1795be71b5cbSKonstantin Komarov 			}
1796be71b5cbSKonstantin Komarov 			svcn = le64_to_cpu(attr->nres.svcn);
1797be71b5cbSKonstantin Komarov 			evcn = le64_to_cpu(attr->nres.evcn);
1798be71b5cbSKonstantin Komarov 		}
1799be71b5cbSKonstantin Komarov 
1800be71b5cbSKonstantin Komarov 		if (end < svcn)
1801be71b5cbSKonstantin Komarov 			end = svcn;
1802be71b5cbSKonstantin Komarov 
1803be71b5cbSKonstantin Komarov 		err = attr_load_runs(attr, ni, run, &end);
1804be71b5cbSKonstantin Komarov 		if (err)
1805be71b5cbSKonstantin Komarov 			goto out;
1806be71b5cbSKonstantin Komarov 
1807be71b5cbSKonstantin Komarov 		evcn1 = evcn + 1;
1808be71b5cbSKonstantin Komarov 		attr->nres.svcn = cpu_to_le64(next_svcn);
1809be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1810be71b5cbSKonstantin Komarov 		if (err)
1811be71b5cbSKonstantin Komarov 			goto out;
1812be71b5cbSKonstantin Komarov 
1813be71b5cbSKonstantin Komarov 		le->vcn = cpu_to_le64(next_svcn);
1814be71b5cbSKonstantin Komarov 		ni->attr_list.dirty = true;
1815be71b5cbSKonstantin Komarov 		mi->dirty = true;
1816be71b5cbSKonstantin Komarov 
1817be71b5cbSKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1818be71b5cbSKonstantin Komarov 	}
1819be71b5cbSKonstantin Komarov ins_ext:
1820be71b5cbSKonstantin Komarov 	if (evcn1 > next_svcn) {
1821be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1822be71b5cbSKonstantin Komarov 					    next_svcn, evcn1 - next_svcn,
1823c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
1824be71b5cbSKonstantin Komarov 		if (err)
1825be71b5cbSKonstantin Komarov 			goto out;
1826be71b5cbSKonstantin Komarov 	}
1827be71b5cbSKonstantin Komarov ok:
1828be71b5cbSKonstantin Komarov 	run_truncate_around(run, vcn);
1829be71b5cbSKonstantin Komarov out:
1830aaab47f2SKonstantin Komarov 	if (attr_b) {
1831be71b5cbSKonstantin Komarov 		if (new_valid > data_size)
1832be71b5cbSKonstantin Komarov 			new_valid = data_size;
1833be71b5cbSKonstantin Komarov 
1834be71b5cbSKonstantin Komarov 		valid_size = le64_to_cpu(attr_b->nres.valid_size);
1835be71b5cbSKonstantin Komarov 		if (new_valid != valid_size) {
1836be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = cpu_to_le64(valid_size);
1837be71b5cbSKonstantin Komarov 			mi_b->dirty = true;
1838be71b5cbSKonstantin Komarov 		}
1839aaab47f2SKonstantin Komarov 	}
1840be71b5cbSKonstantin Komarov 
1841be71b5cbSKonstantin Komarov 	return err;
1842be71b5cbSKonstantin Komarov }
1843be71b5cbSKonstantin Komarov 
1844e8b8e97fSKari Argillander /*
1845e8b8e97fSKari Argillander  * attr_collapse_range - Collapse range in file.
1846e8b8e97fSKari Argillander  */
attr_collapse_range(struct ntfs_inode * ni,u64 vbo,u64 bytes)1847be71b5cbSKonstantin Komarov int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1848be71b5cbSKonstantin Komarov {
1849be71b5cbSKonstantin Komarov 	int err = 0;
1850be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
1851be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1852be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
1853be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
1854be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
1855be71b5cbSKonstantin Komarov 	CLST svcn, evcn1, len, dealloc, alen;
1856be71b5cbSKonstantin Komarov 	CLST vcn, end;
1857be71b5cbSKonstantin Komarov 	u64 valid_size, data_size, alloc_size, total_size;
1858be71b5cbSKonstantin Komarov 	u32 mask;
1859be71b5cbSKonstantin Komarov 	__le16 a_flags;
1860be71b5cbSKonstantin Komarov 
1861be71b5cbSKonstantin Komarov 	if (!bytes)
1862be71b5cbSKonstantin Komarov 		return 0;
1863be71b5cbSKonstantin Komarov 
1864be71b5cbSKonstantin Komarov 	le_b = NULL;
1865be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1866be71b5cbSKonstantin Komarov 	if (!attr_b)
1867be71b5cbSKonstantin Komarov 		return -ENOENT;
1868be71b5cbSKonstantin Komarov 
1869be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
1870be71b5cbSKonstantin Komarov 		/* Attribute is resident. Nothing to do? */
1871be71b5cbSKonstantin Komarov 		return 0;
1872be71b5cbSKonstantin Komarov 	}
1873be71b5cbSKonstantin Komarov 
1874be71b5cbSKonstantin Komarov 	data_size = le64_to_cpu(attr_b->nres.data_size);
1875be71b5cbSKonstantin Komarov 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1876be71b5cbSKonstantin Komarov 	a_flags = attr_b->flags;
1877be71b5cbSKonstantin Komarov 
1878be71b5cbSKonstantin Komarov 	if (is_attr_ext(attr_b)) {
1879be71b5cbSKonstantin Komarov 		total_size = le64_to_cpu(attr_b->nres.total_size);
1880be71b5cbSKonstantin Komarov 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1881be71b5cbSKonstantin Komarov 	} else {
1882be71b5cbSKonstantin Komarov 		total_size = alloc_size;
1883be71b5cbSKonstantin Komarov 		mask = sbi->cluster_mask;
1884be71b5cbSKonstantin Komarov 	}
1885be71b5cbSKonstantin Komarov 
1886be71b5cbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
1887e8b8e97fSKari Argillander 		/* Allow to collapse only cluster aligned ranges. */
1888be71b5cbSKonstantin Komarov 		return -EINVAL;
1889be71b5cbSKonstantin Komarov 	}
1890be71b5cbSKonstantin Komarov 
1891be71b5cbSKonstantin Komarov 	if (vbo > data_size)
1892be71b5cbSKonstantin Komarov 		return -EINVAL;
1893be71b5cbSKonstantin Komarov 
1894be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
1895be71b5cbSKonstantin Komarov 
1896be71b5cbSKonstantin Komarov 	if (vbo + bytes >= data_size) {
1897be71b5cbSKonstantin Komarov 		u64 new_valid = min(ni->i_valid, vbo);
1898be71b5cbSKonstantin Komarov 
1899e8b8e97fSKari Argillander 		/* Simple truncate file at 'vbo'. */
1900be71b5cbSKonstantin Komarov 		truncate_setsize(&ni->vfs_inode, vbo);
1901be71b5cbSKonstantin Komarov 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1902be71b5cbSKonstantin Komarov 				    &new_valid, true, NULL);
1903be71b5cbSKonstantin Komarov 
1904be71b5cbSKonstantin Komarov 		if (!err && new_valid < ni->i_valid)
1905be71b5cbSKonstantin Komarov 			ni->i_valid = new_valid;
1906be71b5cbSKonstantin Komarov 
1907be71b5cbSKonstantin Komarov 		goto out;
1908be71b5cbSKonstantin Komarov 	}
1909be71b5cbSKonstantin Komarov 
1910be71b5cbSKonstantin Komarov 	/*
1911e8b8e97fSKari Argillander 	 * Enumerate all attribute segments and collapse.
1912be71b5cbSKonstantin Komarov 	 */
1913be71b5cbSKonstantin Komarov 	alen = alloc_size >> sbi->cluster_bits;
1914be71b5cbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
1915be71b5cbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
1916be71b5cbSKonstantin Komarov 	end = vcn + len;
1917be71b5cbSKonstantin Komarov 	dealloc = 0;
1918be71b5cbSKonstantin Komarov 
1919be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
1920be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1921be71b5cbSKonstantin Komarov 
1922be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
1923be71b5cbSKonstantin Komarov 		attr = attr_b;
1924be71b5cbSKonstantin Komarov 		le = le_b;
1925be71b5cbSKonstantin Komarov 		mi = mi_b;
1926be71b5cbSKonstantin Komarov 	} else if (!le_b) {
1927be71b5cbSKonstantin Komarov 		err = -EINVAL;
1928be71b5cbSKonstantin Komarov 		goto out;
1929be71b5cbSKonstantin Komarov 	} else {
1930be71b5cbSKonstantin Komarov 		le = le_b;
1931be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1932be71b5cbSKonstantin Komarov 				    &mi);
1933be71b5cbSKonstantin Komarov 		if (!attr) {
1934be71b5cbSKonstantin Komarov 			err = -EINVAL;
1935be71b5cbSKonstantin Komarov 			goto out;
1936be71b5cbSKonstantin Komarov 		}
1937be71b5cbSKonstantin Komarov 
1938be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1939be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1940be71b5cbSKonstantin Komarov 	}
1941be71b5cbSKonstantin Komarov 
1942be71b5cbSKonstantin Komarov 	for (;;) {
1943be71b5cbSKonstantin Komarov 		if (svcn >= end) {
1944e8b8e97fSKari Argillander 			/* Shift VCN- */
1945be71b5cbSKonstantin Komarov 			attr->nres.svcn = cpu_to_le64(svcn - len);
1946be71b5cbSKonstantin Komarov 			attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1947be71b5cbSKonstantin Komarov 			if (le) {
1948be71b5cbSKonstantin Komarov 				le->vcn = attr->nres.svcn;
1949be71b5cbSKonstantin Komarov 				ni->attr_list.dirty = true;
1950be71b5cbSKonstantin Komarov 			}
1951be71b5cbSKonstantin Komarov 			mi->dirty = true;
1952be71b5cbSKonstantin Komarov 		} else if (svcn < vcn || end < evcn1) {
1953be71b5cbSKonstantin Komarov 			CLST vcn1, eat, next_svcn;
1954be71b5cbSKonstantin Komarov 
1955e8b8e97fSKari Argillander 			/* Collapse a part of this attribute segment. */
1956be71b5cbSKonstantin Komarov 			err = attr_load_runs(attr, ni, run, &svcn);
1957be71b5cbSKonstantin Komarov 			if (err)
1958be71b5cbSKonstantin Komarov 				goto out;
1959be71b5cbSKonstantin Komarov 			vcn1 = max(vcn, svcn);
1960be71b5cbSKonstantin Komarov 			eat = min(end, evcn1) - vcn1;
1961be71b5cbSKonstantin Komarov 
1962be71b5cbSKonstantin Komarov 			err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1963be71b5cbSKonstantin Komarov 						true);
1964be71b5cbSKonstantin Komarov 			if (err)
1965be71b5cbSKonstantin Komarov 				goto out;
1966be71b5cbSKonstantin Komarov 
1967be71b5cbSKonstantin Komarov 			if (!run_collapse_range(run, vcn1, eat)) {
1968be71b5cbSKonstantin Komarov 				err = -ENOMEM;
1969be71b5cbSKonstantin Komarov 				goto out;
1970be71b5cbSKonstantin Komarov 			}
1971be71b5cbSKonstantin Komarov 
1972be71b5cbSKonstantin Komarov 			if (svcn >= vcn) {
1973e8b8e97fSKari Argillander 				/* Shift VCN */
1974be71b5cbSKonstantin Komarov 				attr->nres.svcn = cpu_to_le64(vcn);
1975be71b5cbSKonstantin Komarov 				if (le) {
1976be71b5cbSKonstantin Komarov 					le->vcn = attr->nres.svcn;
1977be71b5cbSKonstantin Komarov 					ni->attr_list.dirty = true;
1978be71b5cbSKonstantin Komarov 				}
1979be71b5cbSKonstantin Komarov 			}
1980be71b5cbSKonstantin Komarov 
1981be71b5cbSKonstantin Komarov 			err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
1982be71b5cbSKonstantin Komarov 			if (err)
1983be71b5cbSKonstantin Komarov 				goto out;
1984be71b5cbSKonstantin Komarov 
1985be71b5cbSKonstantin Komarov 			next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1986be71b5cbSKonstantin Komarov 			if (next_svcn + eat < evcn1) {
1987be71b5cbSKonstantin Komarov 				err = ni_insert_nonresident(
1988be71b5cbSKonstantin Komarov 					ni, ATTR_DATA, NULL, 0, run, next_svcn,
1989be71b5cbSKonstantin Komarov 					evcn1 - eat - next_svcn, a_flags, &attr,
1990c1e0ab37SKonstantin Komarov 					&mi, &le);
1991be71b5cbSKonstantin Komarov 				if (err)
1992be71b5cbSKonstantin Komarov 					goto out;
1993be71b5cbSKonstantin Komarov 
1994e8b8e97fSKari Argillander 				/* Layout of records maybe changed. */
1995be71b5cbSKonstantin Komarov 				attr_b = NULL;
1996be71b5cbSKonstantin Komarov 			}
1997be71b5cbSKonstantin Komarov 
1998e8b8e97fSKari Argillander 			/* Free all allocated memory. */
1999be71b5cbSKonstantin Komarov 			run_truncate(run, 0);
2000be71b5cbSKonstantin Komarov 		} else {
2001be71b5cbSKonstantin Komarov 			u16 le_sz;
2002be71b5cbSKonstantin Komarov 			u16 roff = le16_to_cpu(attr->nres.run_off);
2003be71b5cbSKonstantin Komarov 
20046db62086SEdward Lo 			if (roff > le32_to_cpu(attr->size)) {
20056db62086SEdward Lo 				err = -EINVAL;
20066db62086SEdward Lo 				goto out;
20076db62086SEdward Lo 			}
20086db62086SEdward Lo 
2009be71b5cbSKonstantin Komarov 			run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
2010be71b5cbSKonstantin Komarov 				      evcn1 - 1, svcn, Add2Ptr(attr, roff),
2011be71b5cbSKonstantin Komarov 				      le32_to_cpu(attr->size) - roff);
2012be71b5cbSKonstantin Komarov 
2013e8b8e97fSKari Argillander 			/* Delete this attribute segment. */
201478ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
2015be71b5cbSKonstantin Komarov 			if (!le)
2016be71b5cbSKonstantin Komarov 				break;
2017be71b5cbSKonstantin Komarov 
2018be71b5cbSKonstantin Komarov 			le_sz = le16_to_cpu(le->size);
2019be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
2020be71b5cbSKonstantin Komarov 				err = -EINVAL;
2021be71b5cbSKonstantin Komarov 				goto out;
2022be71b5cbSKonstantin Komarov 			}
2023be71b5cbSKonstantin Komarov 
2024be71b5cbSKonstantin Komarov 			if (evcn1 >= alen)
2025be71b5cbSKonstantin Komarov 				break;
2026be71b5cbSKonstantin Komarov 
2027be71b5cbSKonstantin Komarov 			if (!svcn) {
2028e8b8e97fSKari Argillander 				/* Load next record that contains this attribute. */
2029be71b5cbSKonstantin Komarov 				if (ni_load_mi(ni, le, &mi)) {
2030be71b5cbSKonstantin Komarov 					err = -EINVAL;
2031be71b5cbSKonstantin Komarov 					goto out;
2032be71b5cbSKonstantin Komarov 				}
2033be71b5cbSKonstantin Komarov 
2034e8b8e97fSKari Argillander 				/* Look for required attribute. */
2035be71b5cbSKonstantin Komarov 				attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
2036be71b5cbSKonstantin Komarov 						    0, &le->id);
2037be71b5cbSKonstantin Komarov 				if (!attr) {
2038be71b5cbSKonstantin Komarov 					err = -EINVAL;
2039be71b5cbSKonstantin Komarov 					goto out;
2040be71b5cbSKonstantin Komarov 				}
2041be71b5cbSKonstantin Komarov 				goto next_attr;
2042be71b5cbSKonstantin Komarov 			}
2043be71b5cbSKonstantin Komarov 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2044be71b5cbSKonstantin Komarov 		}
2045be71b5cbSKonstantin Komarov 
2046be71b5cbSKonstantin Komarov 		if (evcn1 >= alen)
2047be71b5cbSKonstantin Komarov 			break;
2048be71b5cbSKonstantin Komarov 
2049be71b5cbSKonstantin Komarov 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2050be71b5cbSKonstantin Komarov 		if (!attr) {
2051be71b5cbSKonstantin Komarov 			err = -EINVAL;
2052be71b5cbSKonstantin Komarov 			goto out;
2053be71b5cbSKonstantin Komarov 		}
2054be71b5cbSKonstantin Komarov 
2055be71b5cbSKonstantin Komarov next_attr:
2056be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2057be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2058be71b5cbSKonstantin Komarov 	}
2059be71b5cbSKonstantin Komarov 
2060be71b5cbSKonstantin Komarov 	if (!attr_b) {
2061be71b5cbSKonstantin Komarov 		le_b = NULL;
2062be71b5cbSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2063be71b5cbSKonstantin Komarov 				      &mi_b);
2064be71b5cbSKonstantin Komarov 		if (!attr_b) {
2065be71b5cbSKonstantin Komarov 			err = -ENOENT;
2066be71b5cbSKonstantin Komarov 			goto out;
2067be71b5cbSKonstantin Komarov 		}
2068be71b5cbSKonstantin Komarov 	}
2069be71b5cbSKonstantin Komarov 
2070be71b5cbSKonstantin Komarov 	data_size -= bytes;
2071be71b5cbSKonstantin Komarov 	valid_size = ni->i_valid;
2072be71b5cbSKonstantin Komarov 	if (vbo + bytes <= valid_size)
2073be71b5cbSKonstantin Komarov 		valid_size -= bytes;
2074be71b5cbSKonstantin Komarov 	else if (vbo < valid_size)
2075be71b5cbSKonstantin Komarov 		valid_size = vbo;
2076be71b5cbSKonstantin Komarov 
2077be71b5cbSKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2078be71b5cbSKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(data_size);
2079be71b5cbSKonstantin Komarov 	attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2080be71b5cbSKonstantin Komarov 	total_size -= (u64)dealloc << sbi->cluster_bits;
2081be71b5cbSKonstantin Komarov 	if (is_attr_ext(attr_b))
2082be71b5cbSKonstantin Komarov 		attr_b->nres.total_size = cpu_to_le64(total_size);
2083be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
2084be71b5cbSKonstantin Komarov 
2085e8b8e97fSKari Argillander 	/* Update inode size. */
2086be71b5cbSKonstantin Komarov 	ni->i_valid = valid_size;
20874fd6c08aSKonstantin Komarov 	i_size_write(&ni->vfs_inode, data_size);
2088be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
2089be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2090be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2091be71b5cbSKonstantin Komarov 
2092be71b5cbSKonstantin Komarov out:
2093be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2094be71b5cbSKonstantin Komarov 	if (err)
2095c12df45eSKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
2096be71b5cbSKonstantin Komarov 
2097be71b5cbSKonstantin Komarov 	return err;
2098be71b5cbSKonstantin Komarov }
2099be71b5cbSKonstantin Komarov 
2100e8b8e97fSKari Argillander /*
2101e8b8e97fSKari Argillander  * attr_punch_hole
2102e8b8e97fSKari Argillander  *
2103e8b8e97fSKari Argillander  * Not for normal files.
2104e8b8e97fSKari Argillander  */
attr_punch_hole(struct ntfs_inode * ni,u64 vbo,u64 bytes,u32 * frame_size)2105be71b5cbSKonstantin Komarov int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2106be71b5cbSKonstantin Komarov {
2107be71b5cbSKonstantin Komarov 	int err = 0;
2108be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
2109be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2110be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
2111be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
2112be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
211320abc64fSKonstantin Komarov 	CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2114be71b5cbSKonstantin Komarov 	u64 total_size, alloc_size;
2115be71b5cbSKonstantin Komarov 	u32 mask;
2116c1e0ab37SKonstantin Komarov 	__le16 a_flags;
211720abc64fSKonstantin Komarov 	struct runs_tree run2;
2118be71b5cbSKonstantin Komarov 
2119be71b5cbSKonstantin Komarov 	if (!bytes)
2120be71b5cbSKonstantin Komarov 		return 0;
2121be71b5cbSKonstantin Komarov 
2122be71b5cbSKonstantin Komarov 	le_b = NULL;
2123be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2124be71b5cbSKonstantin Komarov 	if (!attr_b)
2125be71b5cbSKonstantin Komarov 		return -ENOENT;
2126be71b5cbSKonstantin Komarov 
2127be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
21286d5c9e79SAlon Zahavi 		u32 data_size = le32_to_cpu(attr_b->res.data_size);
2129be71b5cbSKonstantin Komarov 		u32 from, to;
2130be71b5cbSKonstantin Komarov 
2131be71b5cbSKonstantin Komarov 		if (vbo > data_size)
2132be71b5cbSKonstantin Komarov 			return 0;
2133be71b5cbSKonstantin Komarov 
2134be71b5cbSKonstantin Komarov 		from = vbo;
21356e3331eeSKari Argillander 		to = min_t(u64, vbo + bytes, data_size);
2136be71b5cbSKonstantin Komarov 		memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2137be71b5cbSKonstantin Komarov 		return 0;
2138be71b5cbSKonstantin Komarov 	}
2139be71b5cbSKonstantin Komarov 
2140be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b))
2141be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
2142be71b5cbSKonstantin Komarov 
2143be71b5cbSKonstantin Komarov 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2144be71b5cbSKonstantin Komarov 	total_size = le64_to_cpu(attr_b->nres.total_size);
2145be71b5cbSKonstantin Komarov 
2146be71b5cbSKonstantin Komarov 	if (vbo >= alloc_size) {
2147d3624466SKonstantin Komarov 		/* NOTE: It is allowed. */
2148be71b5cbSKonstantin Komarov 		return 0;
2149be71b5cbSKonstantin Komarov 	}
2150be71b5cbSKonstantin Komarov 
2151be71b5cbSKonstantin Komarov 	mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2152be71b5cbSKonstantin Komarov 
2153be71b5cbSKonstantin Komarov 	bytes += vbo;
2154be71b5cbSKonstantin Komarov 	if (bytes > alloc_size)
2155be71b5cbSKonstantin Komarov 		bytes = alloc_size;
2156be71b5cbSKonstantin Komarov 	bytes -= vbo;
2157be71b5cbSKonstantin Komarov 
2158be71b5cbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
2159d3624466SKonstantin Komarov 		/* We have to zero a range(s). */
2160be71b5cbSKonstantin Komarov 		if (frame_size == NULL) {
2161d3624466SKonstantin Komarov 			/* Caller insists range is aligned. */
2162be71b5cbSKonstantin Komarov 			return -EINVAL;
2163be71b5cbSKonstantin Komarov 		}
2164be71b5cbSKonstantin Komarov 		*frame_size = mask + 1;
2165be71b5cbSKonstantin Komarov 		return E_NTFS_NOTALIGNED;
2166be71b5cbSKonstantin Komarov 	}
2167be71b5cbSKonstantin Komarov 
2168be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
216920abc64fSKonstantin Komarov 	run_init(&run2);
217020abc64fSKonstantin Komarov 	run_truncate(run, 0);
217120abc64fSKonstantin Komarov 
2172be71b5cbSKonstantin Komarov 	/*
2173e8b8e97fSKari Argillander 	 * Enumerate all attribute segments and punch hole where necessary.
2174be71b5cbSKonstantin Komarov 	 */
2175be71b5cbSKonstantin Komarov 	alen = alloc_size >> sbi->cluster_bits;
2176be71b5cbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
2177be71b5cbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
2178be71b5cbSKonstantin Komarov 	end = vcn + len;
217920abc64fSKonstantin Komarov 	hole = 0;
2180be71b5cbSKonstantin Komarov 
2181be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
2182be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2183c1e0ab37SKonstantin Komarov 	a_flags = attr_b->flags;
2184be71b5cbSKonstantin Komarov 
2185be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
2186be71b5cbSKonstantin Komarov 		attr = attr_b;
2187be71b5cbSKonstantin Komarov 		le = le_b;
2188be71b5cbSKonstantin Komarov 		mi = mi_b;
2189be71b5cbSKonstantin Komarov 	} else if (!le_b) {
2190be71b5cbSKonstantin Komarov 		err = -EINVAL;
219120abc64fSKonstantin Komarov 		goto bad_inode;
2192be71b5cbSKonstantin Komarov 	} else {
2193be71b5cbSKonstantin Komarov 		le = le_b;
2194be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2195be71b5cbSKonstantin Komarov 				    &mi);
2196be71b5cbSKonstantin Komarov 		if (!attr) {
2197be71b5cbSKonstantin Komarov 			err = -EINVAL;
219820abc64fSKonstantin Komarov 			goto bad_inode;
2199be71b5cbSKonstantin Komarov 		}
2200be71b5cbSKonstantin Komarov 
2201be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2202be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2203be71b5cbSKonstantin Komarov 	}
2204be71b5cbSKonstantin Komarov 
2205be71b5cbSKonstantin Komarov 	while (svcn < end) {
220620abc64fSKonstantin Komarov 		CLST vcn1, zero, hole2 = hole;
2207be71b5cbSKonstantin Komarov 
2208be71b5cbSKonstantin Komarov 		err = attr_load_runs(attr, ni, run, &svcn);
2209be71b5cbSKonstantin Komarov 		if (err)
221020abc64fSKonstantin Komarov 			goto done;
2211be71b5cbSKonstantin Komarov 		vcn1 = max(vcn, svcn);
2212be71b5cbSKonstantin Komarov 		zero = min(end, evcn1) - vcn1;
2213be71b5cbSKonstantin Komarov 
221420abc64fSKonstantin Komarov 		/*
221520abc64fSKonstantin Komarov 		 * Check range [vcn1 + zero).
221620abc64fSKonstantin Komarov 		 * Calculate how many clusters there are.
221720abc64fSKonstantin Komarov 		 * Don't do any destructive actions.
221820abc64fSKonstantin Komarov 		 */
221920abc64fSKonstantin Komarov 		err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
2220be71b5cbSKonstantin Komarov 		if (err)
222120abc64fSKonstantin Komarov 			goto done;
2222be71b5cbSKonstantin Komarov 
222320abc64fSKonstantin Komarov 		/* Check if required range is already hole. */
222420abc64fSKonstantin Komarov 		if (hole2 == hole)
222520abc64fSKonstantin Komarov 			goto next_attr;
222620abc64fSKonstantin Komarov 
222720abc64fSKonstantin Komarov 		/* Make a clone of run to undo. */
222820abc64fSKonstantin Komarov 		err = run_clone(run, &run2);
222920abc64fSKonstantin Komarov 		if (err)
223020abc64fSKonstantin Komarov 			goto done;
223120abc64fSKonstantin Komarov 
223220abc64fSKonstantin Komarov 		/* Make a hole range (sparse) [vcn1 + zero). */
223320abc64fSKonstantin Komarov 		if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2234be71b5cbSKonstantin Komarov 			err = -ENOMEM;
223520abc64fSKonstantin Komarov 			goto done;
2236be71b5cbSKonstantin Komarov 		}
2237be71b5cbSKonstantin Komarov 
223820abc64fSKonstantin Komarov 		/* Update run in attribute segment. */
2239be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2240be71b5cbSKonstantin Komarov 		if (err)
224120abc64fSKonstantin Komarov 			goto done;
2242c1e0ab37SKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2243c1e0ab37SKonstantin Komarov 		if (next_svcn < evcn1) {
224420abc64fSKonstantin Komarov 			/* Insert new attribute segment. */
224520abc64fSKonstantin Komarov 			err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
224620abc64fSKonstantin Komarov 						    next_svcn,
224720abc64fSKonstantin Komarov 						    evcn1 - next_svcn, a_flags,
224820abc64fSKonstantin Komarov 						    &attr, &mi, &le);
2249c1e0ab37SKonstantin Komarov 			if (err)
225020abc64fSKonstantin Komarov 				goto undo_punch;
225120abc64fSKonstantin Komarov 
2252c1e0ab37SKonstantin Komarov 			/* Layout of records maybe changed. */
2253560f7736SKonstantin Komarov 			attr_b = NULL;
2254c1e0ab37SKonstantin Komarov 		}
225520abc64fSKonstantin Komarov 
225620abc64fSKonstantin Komarov 		/* Real deallocate. Should not fail. */
225720abc64fSKonstantin Komarov 		run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
225820abc64fSKonstantin Komarov 
225920abc64fSKonstantin Komarov next_attr:
2260e8b8e97fSKari Argillander 		/* Free all allocated memory. */
2261be71b5cbSKonstantin Komarov 		run_truncate(run, 0);
2262be71b5cbSKonstantin Komarov 
2263be71b5cbSKonstantin Komarov 		if (evcn1 >= alen)
2264be71b5cbSKonstantin Komarov 			break;
2265be71b5cbSKonstantin Komarov 
226620abc64fSKonstantin Komarov 		/* Get next attribute segment. */
2267be71b5cbSKonstantin Komarov 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2268be71b5cbSKonstantin Komarov 		if (!attr) {
2269be71b5cbSKonstantin Komarov 			err = -EINVAL;
227020abc64fSKonstantin Komarov 			goto bad_inode;
2271be71b5cbSKonstantin Komarov 		}
2272be71b5cbSKonstantin Komarov 
2273be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2274be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2275be71b5cbSKonstantin Komarov 	}
2276be71b5cbSKonstantin Komarov 
227720abc64fSKonstantin Komarov done:
227820abc64fSKonstantin Komarov 	if (!hole)
227920abc64fSKonstantin Komarov 		goto out;
228020abc64fSKonstantin Komarov 
2281560f7736SKonstantin Komarov 	if (!attr_b) {
2282560f7736SKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2283560f7736SKonstantin Komarov 				      &mi_b);
2284560f7736SKonstantin Komarov 		if (!attr_b) {
2285560f7736SKonstantin Komarov 			err = -EINVAL;
228620abc64fSKonstantin Komarov 			goto bad_inode;
2287560f7736SKonstantin Komarov 		}
2288560f7736SKonstantin Komarov 	}
228920abc64fSKonstantin Komarov 
229020abc64fSKonstantin Komarov 	total_size -= (u64)hole << sbi->cluster_bits;
2291be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
2292be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
2293be71b5cbSKonstantin Komarov 
2294e8b8e97fSKari Argillander 	/* Update inode size. */
2295be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
2296be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2297be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2298be71b5cbSKonstantin Komarov 
2299be71b5cbSKonstantin Komarov out:
230020abc64fSKonstantin Komarov 	run_close(&run2);
2301be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2302be71b5cbSKonstantin Komarov 	return err;
230320abc64fSKonstantin Komarov 
230420abc64fSKonstantin Komarov bad_inode:
230520abc64fSKonstantin Komarov 	_ntfs_bad_inode(&ni->vfs_inode);
230620abc64fSKonstantin Komarov 	goto out;
230720abc64fSKonstantin Komarov 
230820abc64fSKonstantin Komarov undo_punch:
230920abc64fSKonstantin Komarov 	/*
231020abc64fSKonstantin Komarov 	 * Restore packed runs.
231120abc64fSKonstantin Komarov 	 * 'mi_pack_runs' should not fail, cause we restore original.
231220abc64fSKonstantin Komarov 	 */
231320abc64fSKonstantin Komarov 	if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
231420abc64fSKonstantin Komarov 		goto bad_inode;
231520abc64fSKonstantin Komarov 
231620abc64fSKonstantin Komarov 	goto done;
2317be71b5cbSKonstantin Komarov }
2318aa30eccbSKonstantin Komarov 
2319aa30eccbSKonstantin Komarov /*
2320aa30eccbSKonstantin Komarov  * attr_insert_range - Insert range (hole) in file.
2321aa30eccbSKonstantin Komarov  * Not for normal files.
2322aa30eccbSKonstantin Komarov  */
attr_insert_range(struct ntfs_inode * ni,u64 vbo,u64 bytes)2323aa30eccbSKonstantin Komarov int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2324aa30eccbSKonstantin Komarov {
2325aa30eccbSKonstantin Komarov 	int err = 0;
2326aa30eccbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
2327aa30eccbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2328aa30eccbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
2329aa30eccbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
2330aa30eccbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
2331aa30eccbSKonstantin Komarov 	CLST vcn, svcn, evcn1, len, next_svcn;
2332aa30eccbSKonstantin Komarov 	u64 data_size, alloc_size;
2333aa30eccbSKonstantin Komarov 	u32 mask;
2334aa30eccbSKonstantin Komarov 	__le16 a_flags;
2335aa30eccbSKonstantin Komarov 
2336aa30eccbSKonstantin Komarov 	if (!bytes)
2337aa30eccbSKonstantin Komarov 		return 0;
2338aa30eccbSKonstantin Komarov 
2339aa30eccbSKonstantin Komarov 	le_b = NULL;
2340aa30eccbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2341aa30eccbSKonstantin Komarov 	if (!attr_b)
2342aa30eccbSKonstantin Komarov 		return -ENOENT;
2343aa30eccbSKonstantin Komarov 
2344aa30eccbSKonstantin Komarov 	if (!is_attr_ext(attr_b)) {
2345aa30eccbSKonstantin Komarov 		/* It was checked above. See fallocate. */
2346aa30eccbSKonstantin Komarov 		return -EOPNOTSUPP;
2347aa30eccbSKonstantin Komarov 	}
2348aa30eccbSKonstantin Komarov 
2349aa30eccbSKonstantin Komarov 	if (!attr_b->non_res) {
2350aa30eccbSKonstantin Komarov 		data_size = le32_to_cpu(attr_b->res.data_size);
235113747aacSKonstantin Komarov 		alloc_size = data_size;
2352aa30eccbSKonstantin Komarov 		mask = sbi->cluster_mask; /* cluster_size - 1 */
2353aa30eccbSKonstantin Komarov 	} else {
2354aa30eccbSKonstantin Komarov 		data_size = le64_to_cpu(attr_b->nres.data_size);
235513747aacSKonstantin Komarov 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2356aa30eccbSKonstantin Komarov 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2357aa30eccbSKonstantin Komarov 	}
2358aa30eccbSKonstantin Komarov 
2359aa30eccbSKonstantin Komarov 	if (vbo > data_size) {
2360aa30eccbSKonstantin Komarov 		/* Insert range after the file size is not allowed. */
2361aa30eccbSKonstantin Komarov 		return -EINVAL;
2362aa30eccbSKonstantin Komarov 	}
2363aa30eccbSKonstantin Komarov 
2364aa30eccbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
2365aa30eccbSKonstantin Komarov 		/* Allow to insert only frame aligned ranges. */
2366aa30eccbSKonstantin Komarov 		return -EINVAL;
2367aa30eccbSKonstantin Komarov 	}
2368aa30eccbSKonstantin Komarov 
236913747aacSKonstantin Komarov 	/*
237013747aacSKonstantin Komarov 	 * valid_size <= data_size <= alloc_size
237113747aacSKonstantin Komarov 	 * Check alloc_size for maximum possible.
237213747aacSKonstantin Komarov 	 */
237313747aacSKonstantin Komarov 	if (bytes > sbi->maxbytes_sparse - alloc_size)
237413747aacSKonstantin Komarov 		return -EFBIG;
237513747aacSKonstantin Komarov 
2376aa30eccbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
2377aa30eccbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
2378aa30eccbSKonstantin Komarov 
2379aa30eccbSKonstantin Komarov 	down_write(&ni->file.run_lock);
2380aa30eccbSKonstantin Komarov 
2381aa30eccbSKonstantin Komarov 	if (!attr_b->non_res) {
2382aa30eccbSKonstantin Komarov 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
23839256ec35SKonstantin Komarov 				    data_size + bytes, NULL, false, NULL);
2384aa30eccbSKonstantin Komarov 
2385aa30eccbSKonstantin Komarov 		le_b = NULL;
2386aa30eccbSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2387aa30eccbSKonstantin Komarov 				      &mi_b);
23884838ec0dSDan Carpenter 		if (!attr_b) {
2389aa30eccbSKonstantin Komarov 			err = -EINVAL;
23909256ec35SKonstantin Komarov 			goto bad_inode;
2391aa30eccbSKonstantin Komarov 		}
23929256ec35SKonstantin Komarov 
23939256ec35SKonstantin Komarov 		if (err)
23949256ec35SKonstantin Komarov 			goto out;
23959256ec35SKonstantin Komarov 
23969256ec35SKonstantin Komarov 		if (!attr_b->non_res) {
23979256ec35SKonstantin Komarov 			/* Still resident. */
23989144b438SKonstantin Komarov 			char *data = Add2Ptr(attr_b,
23999144b438SKonstantin Komarov 					     le16_to_cpu(attr_b->res.data_off));
24009256ec35SKonstantin Komarov 
24019256ec35SKonstantin Komarov 			memmove(data + bytes, data, bytes);
24029256ec35SKonstantin Komarov 			memset(data, 0, bytes);
24039256ec35SKonstantin Komarov 			goto done;
24049256ec35SKonstantin Komarov 		}
24059256ec35SKonstantin Komarov 
24069256ec35SKonstantin Komarov 		/* Resident files becomes nonresident. */
2407aa30eccbSKonstantin Komarov 		data_size = le64_to_cpu(attr_b->nres.data_size);
2408aa30eccbSKonstantin Komarov 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2409aa30eccbSKonstantin Komarov 	}
2410aa30eccbSKonstantin Komarov 
2411aa30eccbSKonstantin Komarov 	/*
2412aa30eccbSKonstantin Komarov 	 * Enumerate all attribute segments and shift start vcn.
2413aa30eccbSKonstantin Komarov 	 */
2414aa30eccbSKonstantin Komarov 	a_flags = attr_b->flags;
2415aa30eccbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
2416aa30eccbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2417aa30eccbSKonstantin Komarov 
2418aa30eccbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
2419aa30eccbSKonstantin Komarov 		attr = attr_b;
2420aa30eccbSKonstantin Komarov 		le = le_b;
2421aa30eccbSKonstantin Komarov 		mi = mi_b;
2422aa30eccbSKonstantin Komarov 	} else if (!le_b) {
2423aa30eccbSKonstantin Komarov 		err = -EINVAL;
24249256ec35SKonstantin Komarov 		goto bad_inode;
2425aa30eccbSKonstantin Komarov 	} else {
2426aa30eccbSKonstantin Komarov 		le = le_b;
2427aa30eccbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2428aa30eccbSKonstantin Komarov 				    &mi);
2429aa30eccbSKonstantin Komarov 		if (!attr) {
2430aa30eccbSKonstantin Komarov 			err = -EINVAL;
24319256ec35SKonstantin Komarov 			goto bad_inode;
2432aa30eccbSKonstantin Komarov 		}
2433aa30eccbSKonstantin Komarov 
2434aa30eccbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2435aa30eccbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2436aa30eccbSKonstantin Komarov 	}
2437aa30eccbSKonstantin Komarov 
2438aa30eccbSKonstantin Komarov 	run_truncate(run, 0); /* clear cached values. */
2439aa30eccbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
2440aa30eccbSKonstantin Komarov 	if (err)
2441aa30eccbSKonstantin Komarov 		goto out;
2442aa30eccbSKonstantin Komarov 
2443aa30eccbSKonstantin Komarov 	if (!run_insert_range(run, vcn, len)) {
2444aa30eccbSKonstantin Komarov 		err = -ENOMEM;
2445aa30eccbSKonstantin Komarov 		goto out;
2446aa30eccbSKonstantin Komarov 	}
2447aa30eccbSKonstantin Komarov 
2448aa30eccbSKonstantin Komarov 	/* Try to pack in current record as much as possible. */
2449aa30eccbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2450aa30eccbSKonstantin Komarov 	if (err)
2451aa30eccbSKonstantin Komarov 		goto out;
2452aa30eccbSKonstantin Komarov 
2453aa30eccbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2454aa30eccbSKonstantin Komarov 
2455aa30eccbSKonstantin Komarov 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2456aa30eccbSKonstantin Komarov 	       attr->type == ATTR_DATA && !attr->name_len) {
2457aa30eccbSKonstantin Komarov 		le64_add_cpu(&attr->nres.svcn, len);
2458aa30eccbSKonstantin Komarov 		le64_add_cpu(&attr->nres.evcn, len);
2459aa30eccbSKonstantin Komarov 		if (le) {
2460aa30eccbSKonstantin Komarov 			le->vcn = attr->nres.svcn;
2461aa30eccbSKonstantin Komarov 			ni->attr_list.dirty = true;
2462aa30eccbSKonstantin Komarov 		}
2463aa30eccbSKonstantin Komarov 		mi->dirty = true;
2464aa30eccbSKonstantin Komarov 	}
2465aa30eccbSKonstantin Komarov 
24669256ec35SKonstantin Komarov 	if (next_svcn < evcn1 + len) {
24679256ec35SKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
24689256ec35SKonstantin Komarov 					    next_svcn, evcn1 + len - next_svcn,
24699256ec35SKonstantin Komarov 					    a_flags, NULL, NULL, NULL);
24709256ec35SKonstantin Komarov 
24719256ec35SKonstantin Komarov 		le_b = NULL;
24729256ec35SKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
24739256ec35SKonstantin Komarov 				      &mi_b);
24749256ec35SKonstantin Komarov 		if (!attr_b) {
24759256ec35SKonstantin Komarov 			err = -EINVAL;
24769256ec35SKonstantin Komarov 			goto bad_inode;
24779256ec35SKonstantin Komarov 		}
24789256ec35SKonstantin Komarov 
24799256ec35SKonstantin Komarov 		if (err) {
24809256ec35SKonstantin Komarov 			/* ni_insert_nonresident failed. Try to undo. */
24819256ec35SKonstantin Komarov 			goto undo_insert_range;
24829256ec35SKonstantin Komarov 		}
24839256ec35SKonstantin Komarov 	}
24849256ec35SKonstantin Komarov 
2485aa30eccbSKonstantin Komarov 	/*
24869256ec35SKonstantin Komarov 	 * Update primary attribute segment.
2487aa30eccbSKonstantin Komarov 	 */
2488aa30eccbSKonstantin Komarov 	if (vbo <= ni->i_valid)
2489aa30eccbSKonstantin Komarov 		ni->i_valid += bytes;
2490aa30eccbSKonstantin Komarov 
24919144b438SKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
24929144b438SKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
2493aa30eccbSKonstantin Komarov 
2494aa30eccbSKonstantin Komarov 	/* ni->valid may be not equal valid_size (temporary). */
2495aa30eccbSKonstantin Komarov 	if (ni->i_valid > data_size + bytes)
2496aa30eccbSKonstantin Komarov 		attr_b->nres.valid_size = attr_b->nres.data_size;
2497aa30eccbSKonstantin Komarov 	else
2498aa30eccbSKonstantin Komarov 		attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2499aa30eccbSKonstantin Komarov 	mi_b->dirty = true;
2500aa30eccbSKonstantin Komarov 
25019256ec35SKonstantin Komarov done:
25024fd6c08aSKonstantin Komarov 	i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes);
2503aa30eccbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2504aa30eccbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2505aa30eccbSKonstantin Komarov 
2506aa30eccbSKonstantin Komarov out:
2507aa30eccbSKonstantin Komarov 	run_truncate(run, 0); /* clear cached values. */
2508aa30eccbSKonstantin Komarov 
2509aa30eccbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2510aa30eccbSKonstantin Komarov 
2511aa30eccbSKonstantin Komarov 	return err;
25129256ec35SKonstantin Komarov 
25139256ec35SKonstantin Komarov bad_inode:
25149256ec35SKonstantin Komarov 	_ntfs_bad_inode(&ni->vfs_inode);
25159256ec35SKonstantin Komarov 	goto out;
25169256ec35SKonstantin Komarov 
25179256ec35SKonstantin Komarov undo_insert_range:
25189256ec35SKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
25199256ec35SKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
25209256ec35SKonstantin Komarov 
25219256ec35SKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
25229256ec35SKonstantin Komarov 		attr = attr_b;
25239256ec35SKonstantin Komarov 		le = le_b;
25249256ec35SKonstantin Komarov 		mi = mi_b;
25259256ec35SKonstantin Komarov 	} else if (!le_b) {
25269256ec35SKonstantin Komarov 		goto bad_inode;
25279256ec35SKonstantin Komarov 	} else {
25289256ec35SKonstantin Komarov 		le = le_b;
25299256ec35SKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
25309256ec35SKonstantin Komarov 				    &mi);
25319256ec35SKonstantin Komarov 		if (!attr) {
25329256ec35SKonstantin Komarov 			goto bad_inode;
25339256ec35SKonstantin Komarov 		}
25349256ec35SKonstantin Komarov 
25359256ec35SKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
25369256ec35SKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
25379256ec35SKonstantin Komarov 	}
25389256ec35SKonstantin Komarov 
25399256ec35SKonstantin Komarov 	if (attr_load_runs(attr, ni, run, NULL))
25409256ec35SKonstantin Komarov 		goto bad_inode;
25419256ec35SKonstantin Komarov 
25429256ec35SKonstantin Komarov 	if (!run_collapse_range(run, vcn, len))
25439256ec35SKonstantin Komarov 		goto bad_inode;
25449256ec35SKonstantin Komarov 
25459256ec35SKonstantin Komarov 	if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
25469256ec35SKonstantin Komarov 		goto bad_inode;
25479256ec35SKonstantin Komarov 
25489256ec35SKonstantin Komarov 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
25499256ec35SKonstantin Komarov 	       attr->type == ATTR_DATA && !attr->name_len) {
25509256ec35SKonstantin Komarov 		le64_sub_cpu(&attr->nres.svcn, len);
25519256ec35SKonstantin Komarov 		le64_sub_cpu(&attr->nres.evcn, len);
25529256ec35SKonstantin Komarov 		if (le) {
25539256ec35SKonstantin Komarov 			le->vcn = attr->nres.svcn;
25549256ec35SKonstantin Komarov 			ni->attr_list.dirty = true;
25559256ec35SKonstantin Komarov 		}
25569256ec35SKonstantin Komarov 		mi->dirty = true;
25579256ec35SKonstantin Komarov 	}
25589256ec35SKonstantin Komarov 
25599256ec35SKonstantin Komarov 	goto out;
2560aa30eccbSKonstantin Komarov }
2561*40bb3c59SKonstantin Komarov 
2562*40bb3c59SKonstantin Komarov /*
2563*40bb3c59SKonstantin Komarov  * attr_force_nonresident
2564*40bb3c59SKonstantin Komarov  *
2565*40bb3c59SKonstantin Komarov  * Convert default data attribute into non resident form.
2566*40bb3c59SKonstantin Komarov  */
attr_force_nonresident(struct ntfs_inode * ni)2567*40bb3c59SKonstantin Komarov int attr_force_nonresident(struct ntfs_inode *ni)
2568*40bb3c59SKonstantin Komarov {
2569*40bb3c59SKonstantin Komarov 	int err;
2570*40bb3c59SKonstantin Komarov 	struct ATTRIB *attr;
2571*40bb3c59SKonstantin Komarov 	struct ATTR_LIST_ENTRY *le = NULL;
2572*40bb3c59SKonstantin Komarov 	struct mft_inode *mi;
2573*40bb3c59SKonstantin Komarov 
2574*40bb3c59SKonstantin Komarov 	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2575*40bb3c59SKonstantin Komarov 	if (!attr) {
2576*40bb3c59SKonstantin Komarov 		ntfs_bad_inode(&ni->vfs_inode, "no data attribute");
2577*40bb3c59SKonstantin Komarov 		return -ENOENT;
2578*40bb3c59SKonstantin Komarov 	}
2579*40bb3c59SKonstantin Komarov 
2580*40bb3c59SKonstantin Komarov 	if (attr->non_res) {
2581*40bb3c59SKonstantin Komarov 		/* Already non resident. */
2582*40bb3c59SKonstantin Komarov 		return 0;
2583*40bb3c59SKonstantin Komarov 	}
2584*40bb3c59SKonstantin Komarov 
2585*40bb3c59SKonstantin Komarov 	down_write(&ni->file.run_lock);
2586*40bb3c59SKonstantin Komarov 	err = attr_make_nonresident(ni, attr, le, mi,
2587*40bb3c59SKonstantin Komarov 				    le32_to_cpu(attr->res.data_size),
2588*40bb3c59SKonstantin Komarov 				    &ni->file.run, &attr, NULL);
2589*40bb3c59SKonstantin Komarov 	up_write(&ni->file.run_lock);
2590*40bb3c59SKonstantin Komarov 
2591*40bb3c59SKonstantin Komarov 	return err;
2592*40bb3c59SKonstantin Komarov }
2593