xref: /openbmc/linux/fs/ntfs3/attrib.c (revision 9144f784f852f9a125cabe9927b986d909bfa439)
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;
234c4df2d48SKonstantin Komarov 	u32 used, asize, rsize, aoff;
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 
255c4df2d48SKonstantin Komarov 	/* len - how many clusters required to store 'rsize' bytes */
256c4df2d48SKonstantin Komarov 	if (is_attr_compressed(attr)) {
257c4df2d48SKonstantin Komarov 		u8 shift = sbi->cluster_bits + NTFS_LZNT_CUNIT;
258c4df2d48SKonstantin Komarov 		len = ((rsize + (1u << shift) - 1) >> shift) << NTFS_LZNT_CUNIT;
259c4df2d48SKonstantin Komarov 	} else {
260c4df2d48SKonstantin Komarov 		len = bytes_to_cluster(sbi, rsize);
261c4df2d48SKonstantin Komarov 	}
262be71b5cbSKonstantin Komarov 
263be71b5cbSKonstantin Komarov 	run_init(run);
264be71b5cbSKonstantin Komarov 
265e8b8e97fSKari Argillander 	/* Make a copy of original attribute. */
266195c52bdSKari Argillander 	attr_s = kmemdup(attr, asize, GFP_NOFS);
267be71b5cbSKonstantin Komarov 	if (!attr_s) {
268be71b5cbSKonstantin Komarov 		err = -ENOMEM;
269be71b5cbSKonstantin Komarov 		goto out;
270be71b5cbSKonstantin Komarov 	}
271be71b5cbSKonstantin Komarov 
272be71b5cbSKonstantin Komarov 	if (!len) {
273e8b8e97fSKari Argillander 		/* Empty resident -> Empty nonresident. */
274be71b5cbSKonstantin Komarov 		alen = 0;
275be71b5cbSKonstantin Komarov 	} else {
276be71b5cbSKonstantin Komarov 		const char *data = resident_data(attr);
277be71b5cbSKonstantin Komarov 
278be71b5cbSKonstantin Komarov 		err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
279c380b52fSKonstantin Komarov 					     ALLOCATE_DEF, &alen, 0, NULL,
280c380b52fSKonstantin Komarov 					     NULL);
281be71b5cbSKonstantin Komarov 		if (err)
282be71b5cbSKonstantin Komarov 			goto out1;
283be71b5cbSKonstantin Komarov 
284be71b5cbSKonstantin Komarov 		if (!rsize) {
285e8b8e97fSKari Argillander 			/* Empty resident -> Non empty nonresident. */
286be71b5cbSKonstantin Komarov 		} else if (!is_data) {
28763544672SKonstantin Komarov 			err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
288be71b5cbSKonstantin Komarov 			if (err)
289be71b5cbSKonstantin Komarov 				goto out2;
290be71b5cbSKonstantin Komarov 		} else if (!page) {
291be71b5cbSKonstantin Komarov 			char *kaddr;
292be71b5cbSKonstantin Komarov 
293be71b5cbSKonstantin Komarov 			page = grab_cache_page(ni->vfs_inode.i_mapping, 0);
294be71b5cbSKonstantin Komarov 			if (!page) {
295be71b5cbSKonstantin Komarov 				err = -ENOMEM;
296be71b5cbSKonstantin Komarov 				goto out2;
297be71b5cbSKonstantin Komarov 			}
298be71b5cbSKonstantin Komarov 			kaddr = kmap_atomic(page);
299be71b5cbSKonstantin Komarov 			memcpy(kaddr, data, rsize);
300be71b5cbSKonstantin Komarov 			memset(kaddr + rsize, 0, PAGE_SIZE - rsize);
301be71b5cbSKonstantin Komarov 			kunmap_atomic(kaddr);
302be71b5cbSKonstantin Komarov 			flush_dcache_page(page);
303be71b5cbSKonstantin Komarov 			SetPageUptodate(page);
304be71b5cbSKonstantin Komarov 			set_page_dirty(page);
305be71b5cbSKonstantin Komarov 			unlock_page(page);
306be71b5cbSKonstantin Komarov 			put_page(page);
307be71b5cbSKonstantin Komarov 		}
308be71b5cbSKonstantin Komarov 	}
309be71b5cbSKonstantin Komarov 
310e8b8e97fSKari Argillander 	/* Remove original attribute. */
311be71b5cbSKonstantin Komarov 	used -= asize;
312be71b5cbSKonstantin Komarov 	memmove(attr, Add2Ptr(attr, asize), used - aoff);
313be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used);
314be71b5cbSKonstantin Komarov 	mi->dirty = true;
315be71b5cbSKonstantin Komarov 	if (le)
316be71b5cbSKonstantin Komarov 		al_remove_le(ni, le);
317be71b5cbSKonstantin Komarov 
318be71b5cbSKonstantin Komarov 	err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
319be71b5cbSKonstantin Komarov 				    attr_s->name_len, run, 0, alen,
320c1e0ab37SKonstantin Komarov 				    attr_s->flags, &attr, NULL, NULL);
321be71b5cbSKonstantin Komarov 	if (err)
322be71b5cbSKonstantin Komarov 		goto out3;
323be71b5cbSKonstantin Komarov 
324195c52bdSKari Argillander 	kfree(attr_s);
325be71b5cbSKonstantin Komarov 	attr->nres.data_size = cpu_to_le64(rsize);
326be71b5cbSKonstantin Komarov 	attr->nres.valid_size = attr->nres.data_size;
327be71b5cbSKonstantin Komarov 
328be71b5cbSKonstantin Komarov 	*ins_attr = attr;
329be71b5cbSKonstantin Komarov 
330be71b5cbSKonstantin Komarov 	if (is_data)
331be71b5cbSKonstantin Komarov 		ni->ni_flags &= ~NI_FLAG_RESIDENT;
332be71b5cbSKonstantin Komarov 
333e8b8e97fSKari Argillander 	/* Resident attribute becomes non resident. */
334be71b5cbSKonstantin Komarov 	return 0;
335be71b5cbSKonstantin Komarov 
336be71b5cbSKonstantin Komarov out3:
337be71b5cbSKonstantin Komarov 	attr = Add2Ptr(rec, aoff);
338be71b5cbSKonstantin Komarov 	memmove(next, attr, used - aoff);
339be71b5cbSKonstantin Komarov 	memcpy(attr, attr_s, asize);
340be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used + asize);
341be71b5cbSKonstantin Komarov 	mi->dirty = true;
342be71b5cbSKonstantin Komarov out2:
343e8b8e97fSKari Argillander 	/* Undo: do not trim new allocated clusters. */
344be71b5cbSKonstantin Komarov 	run_deallocate(sbi, run, false);
345be71b5cbSKonstantin Komarov 	run_close(run);
346be71b5cbSKonstantin Komarov out1:
347195c52bdSKari Argillander 	kfree(attr_s);
348be71b5cbSKonstantin Komarov out:
349be71b5cbSKonstantin Komarov 	return err;
350be71b5cbSKonstantin Komarov }
351be71b5cbSKonstantin Komarov 
352be71b5cbSKonstantin Komarov /*
353e8b8e97fSKari Argillander  * attr_set_size_res - Helper for attr_set_size().
354be71b5cbSKonstantin 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)355be71b5cbSKonstantin Komarov static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
356be71b5cbSKonstantin Komarov 			     struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
357be71b5cbSKonstantin Komarov 			     u64 new_size, struct runs_tree *run,
358be71b5cbSKonstantin Komarov 			     struct ATTRIB **ins_attr)
359be71b5cbSKonstantin Komarov {
360be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = mi->sbi;
361be71b5cbSKonstantin Komarov 	struct MFT_REC *rec = mi->mrec;
362be71b5cbSKonstantin Komarov 	u32 used = le32_to_cpu(rec->used);
363be71b5cbSKonstantin Komarov 	u32 asize = le32_to_cpu(attr->size);
364be71b5cbSKonstantin Komarov 	u32 aoff = PtrOffset(rec, attr);
365be71b5cbSKonstantin Komarov 	u32 rsize = le32_to_cpu(attr->res.data_size);
366be71b5cbSKonstantin Komarov 	u32 tail = used - aoff - asize;
367be71b5cbSKonstantin Komarov 	char *next = Add2Ptr(attr, asize);
368fa3cacf5SKari Argillander 	s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
369be71b5cbSKonstantin Komarov 
370be71b5cbSKonstantin Komarov 	if (dsize < 0) {
371be71b5cbSKonstantin Komarov 		memmove(next + dsize, next, tail);
372be71b5cbSKonstantin Komarov 	} else if (dsize > 0) {
373be71b5cbSKonstantin Komarov 		if (used + dsize > sbi->max_bytes_per_attr)
374be71b5cbSKonstantin Komarov 			return attr_make_nonresident(ni, attr, le, mi, new_size,
375be71b5cbSKonstantin Komarov 						     run, ins_attr, NULL);
376be71b5cbSKonstantin Komarov 
377be71b5cbSKonstantin Komarov 		memmove(next + dsize, next, tail);
378be71b5cbSKonstantin Komarov 		memset(next, 0, dsize);
379be71b5cbSKonstantin Komarov 	}
380be71b5cbSKonstantin Komarov 
381be71b5cbSKonstantin Komarov 	if (new_size > rsize)
382be71b5cbSKonstantin Komarov 		memset(Add2Ptr(resident_data(attr), rsize), 0,
383be71b5cbSKonstantin Komarov 		       new_size - rsize);
384be71b5cbSKonstantin Komarov 
385be71b5cbSKonstantin Komarov 	rec->used = cpu_to_le32(used + dsize);
386be71b5cbSKonstantin Komarov 	attr->size = cpu_to_le32(asize + dsize);
387be71b5cbSKonstantin Komarov 	attr->res.data_size = cpu_to_le32(new_size);
388be71b5cbSKonstantin Komarov 	mi->dirty = true;
389be71b5cbSKonstantin Komarov 	*ins_attr = attr;
390be71b5cbSKonstantin Komarov 
391be71b5cbSKonstantin Komarov 	return 0;
392be71b5cbSKonstantin Komarov }
393be71b5cbSKonstantin Komarov 
394be71b5cbSKonstantin Komarov /*
395e8b8e97fSKari Argillander  * attr_set_size - Change the size of attribute.
396be71b5cbSKonstantin Komarov  *
397be71b5cbSKonstantin Komarov  * Extend:
398e8b8e97fSKari Argillander  *   - Sparse/compressed: No allocated clusters.
399e8b8e97fSKari Argillander  *   - Normal: Append allocated and preallocated new clusters.
400be71b5cbSKonstantin Komarov  * Shrink:
401e8b8e97fSKari Argillander  *   - No deallocate if @keep_prealloc is set.
402be71b5cbSKonstantin 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)403be71b5cbSKonstantin Komarov int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
404be71b5cbSKonstantin Komarov 		  const __le16 *name, u8 name_len, struct runs_tree *run,
405be71b5cbSKonstantin Komarov 		  u64 new_size, const u64 *new_valid, bool keep_prealloc,
406be71b5cbSKonstantin Komarov 		  struct ATTRIB **ret)
407be71b5cbSKonstantin Komarov {
408be71b5cbSKonstantin Komarov 	int err = 0;
409be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
410be71b5cbSKonstantin Komarov 	u8 cluster_bits = sbi->cluster_bits;
41196de65a9SKonstantin Komarov 	bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
41296de65a9SKonstantin Komarov 		      !name_len;
413be71b5cbSKonstantin Komarov 	u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
414be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
415be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
416be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
417be71b5cbSKonstantin Komarov 	CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
418be71b5cbSKonstantin Komarov 	CLST next_svcn, pre_alloc = -1, done = 0;
4190e5b044cSKonstantin Komarov 	bool is_ext, is_bad = false;
420ad26a9c8SKonstantin Komarov 	bool dirty = false;
421be71b5cbSKonstantin Komarov 	u32 align;
422be71b5cbSKonstantin Komarov 	struct MFT_REC *rec;
423be71b5cbSKonstantin Komarov 
424be71b5cbSKonstantin Komarov again:
4250e5b044cSKonstantin Komarov 	alen = 0;
426be71b5cbSKonstantin Komarov 	le_b = NULL;
427be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
428be71b5cbSKonstantin Komarov 			      &mi_b);
429be71b5cbSKonstantin Komarov 	if (!attr_b) {
430be71b5cbSKonstantin Komarov 		err = -ENOENT;
4310e5b044cSKonstantin Komarov 		goto bad_inode;
432be71b5cbSKonstantin Komarov 	}
433be71b5cbSKonstantin Komarov 
434be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
435be71b5cbSKonstantin Komarov 		err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
436be71b5cbSKonstantin Komarov 					&attr_b);
4370e5b044cSKonstantin Komarov 		if (err)
4380e5b044cSKonstantin Komarov 			return err;
4390e5b044cSKonstantin Komarov 
4400e5b044cSKonstantin Komarov 		/* Return if file is still resident. */
441ad26a9c8SKonstantin Komarov 		if (!attr_b->non_res) {
442ad26a9c8SKonstantin Komarov 			dirty = true;
4430e5b044cSKonstantin Komarov 			goto ok1;
444ad26a9c8SKonstantin Komarov 		}
445be71b5cbSKonstantin Komarov 
446e8b8e97fSKari Argillander 		/* Layout of records may be changed, so do a full search. */
447be71b5cbSKonstantin Komarov 		goto again;
448be71b5cbSKonstantin Komarov 	}
449be71b5cbSKonstantin Komarov 
450be71b5cbSKonstantin Komarov 	is_ext = is_attr_ext(attr_b);
451be71b5cbSKonstantin Komarov 	align = sbi->cluster_size;
452ce46ae0cSKonstantin Komarov 	if (is_ext)
453be71b5cbSKonstantin Komarov 		align <<= attr_b->nres.c_unit;
454be71b5cbSKonstantin Komarov 
455be71b5cbSKonstantin Komarov 	old_valid = le64_to_cpu(attr_b->nres.valid_size);
456be71b5cbSKonstantin Komarov 	old_size = le64_to_cpu(attr_b->nres.data_size);
457be71b5cbSKonstantin Komarov 	old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
4580e5b044cSKonstantin Komarov 
4590e5b044cSKonstantin Komarov again_1:
460be71b5cbSKonstantin Komarov 	old_alen = old_alloc >> cluster_bits;
461be71b5cbSKonstantin Komarov 
462be71b5cbSKonstantin Komarov 	new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
463be71b5cbSKonstantin Komarov 	new_alen = new_alloc >> cluster_bits;
464be71b5cbSKonstantin Komarov 
465be71b5cbSKonstantin Komarov 	if (keep_prealloc && new_size < old_size) {
466be71b5cbSKonstantin Komarov 		attr_b->nres.data_size = cpu_to_le64(new_size);
467ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
468be71b5cbSKonstantin Komarov 		goto ok;
469be71b5cbSKonstantin Komarov 	}
470be71b5cbSKonstantin Komarov 
471be71b5cbSKonstantin Komarov 	vcn = old_alen - 1;
472be71b5cbSKonstantin Komarov 
473be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
474be71b5cbSKonstantin Komarov 	evcn = le64_to_cpu(attr_b->nres.evcn);
475be71b5cbSKonstantin Komarov 
476be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn <= evcn) {
477be71b5cbSKonstantin Komarov 		attr = attr_b;
478be71b5cbSKonstantin Komarov 		le = le_b;
479be71b5cbSKonstantin Komarov 		mi = mi_b;
480be71b5cbSKonstantin Komarov 	} else if (!le_b) {
481be71b5cbSKonstantin Komarov 		err = -EINVAL;
4820e5b044cSKonstantin Komarov 		goto bad_inode;
483be71b5cbSKonstantin Komarov 	} else {
484be71b5cbSKonstantin Komarov 		le = le_b;
485be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
486be71b5cbSKonstantin Komarov 				    &mi);
487be71b5cbSKonstantin Komarov 		if (!attr) {
488be71b5cbSKonstantin Komarov 			err = -EINVAL;
4890e5b044cSKonstantin Komarov 			goto bad_inode;
490be71b5cbSKonstantin Komarov 		}
491be71b5cbSKonstantin Komarov 
492be71b5cbSKonstantin Komarov next_le_1:
493be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
494be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
495be71b5cbSKonstantin Komarov 	}
4960e5b044cSKonstantin Komarov 	/*
4970e5b044cSKonstantin Komarov 	 * Here we have:
4980e5b044cSKonstantin Komarov 	 * attr,mi,le - last attribute segment (containing 'vcn').
4990e5b044cSKonstantin Komarov 	 * attr_b,mi_b,le_b - base (primary) attribute segment.
5000e5b044cSKonstantin Komarov 	 */
501be71b5cbSKonstantin Komarov next_le:
502be71b5cbSKonstantin Komarov 	rec = mi->mrec;
503be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
504be71b5cbSKonstantin Komarov 	if (err)
505be71b5cbSKonstantin Komarov 		goto out;
506be71b5cbSKonstantin Komarov 
507be71b5cbSKonstantin Komarov 	if (new_size > old_size) {
508be71b5cbSKonstantin Komarov 		CLST to_allocate;
509be71b5cbSKonstantin Komarov 		size_t free;
510be71b5cbSKonstantin Komarov 
511be71b5cbSKonstantin Komarov 		if (new_alloc <= old_alloc) {
512be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
513ad26a9c8SKonstantin Komarov 			mi_b->dirty = dirty = true;
514be71b5cbSKonstantin Komarov 			goto ok;
515be71b5cbSKonstantin Komarov 		}
516be71b5cbSKonstantin Komarov 
5170e5b044cSKonstantin Komarov 		/*
5180e5b044cSKonstantin Komarov 		 * Add clusters. In simple case we have to:
5190e5b044cSKonstantin Komarov 		 *  - allocate space (vcn, lcn, len)
5200e5b044cSKonstantin Komarov 		 *  - update packed run in 'mi'
5210e5b044cSKonstantin Komarov 		 *  - update attr->nres.evcn
5220e5b044cSKonstantin Komarov 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
5230e5b044cSKonstantin Komarov 		 */
524be71b5cbSKonstantin Komarov 		to_allocate = new_alen - old_alen;
525be71b5cbSKonstantin Komarov add_alloc_in_same_attr_seg:
526be71b5cbSKonstantin Komarov 		lcn = 0;
527be71b5cbSKonstantin Komarov 		if (is_mft) {
528e8b8e97fSKari Argillander 			/* MFT allocates clusters from MFT zone. */
529be71b5cbSKonstantin Komarov 			pre_alloc = 0;
530be71b5cbSKonstantin Komarov 		} else if (is_ext) {
531e8b8e97fSKari Argillander 			/* No preallocate for sparse/compress. */
532be71b5cbSKonstantin Komarov 			pre_alloc = 0;
533be71b5cbSKonstantin Komarov 		} else if (pre_alloc == -1) {
534be71b5cbSKonstantin Komarov 			pre_alloc = 0;
535be71b5cbSKonstantin Komarov 			if (type == ATTR_DATA && !name_len &&
536564c97bdSKari Argillander 			    sbi->options->prealloc) {
53796de65a9SKonstantin Komarov 				pre_alloc = bytes_to_cluster(
53896de65a9SKonstantin Komarov 						    sbi, get_pre_allocated(
53996de65a9SKonstantin Komarov 								 new_size)) -
5400e5b044cSKonstantin Komarov 					    new_alen;
541be71b5cbSKonstantin Komarov 			}
542be71b5cbSKonstantin Komarov 
543e8b8e97fSKari Argillander 			/* Get the last LCN to allocate from. */
544be71b5cbSKonstantin Komarov 			if (old_alen &&
545be71b5cbSKonstantin Komarov 			    !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
546be71b5cbSKonstantin Komarov 				lcn = SPARSE_LCN;
547be71b5cbSKonstantin Komarov 			}
548be71b5cbSKonstantin Komarov 
549be71b5cbSKonstantin Komarov 			if (lcn == SPARSE_LCN)
550be71b5cbSKonstantin Komarov 				lcn = 0;
551be71b5cbSKonstantin Komarov 			else if (lcn)
552be71b5cbSKonstantin Komarov 				lcn += 1;
553be71b5cbSKonstantin Komarov 
554be71b5cbSKonstantin Komarov 			free = wnd_zeroes(&sbi->used.bitmap);
555be71b5cbSKonstantin Komarov 			if (to_allocate > free) {
556be71b5cbSKonstantin Komarov 				err = -ENOSPC;
557be71b5cbSKonstantin Komarov 				goto out;
558be71b5cbSKonstantin Komarov 			}
559be71b5cbSKonstantin Komarov 
560be71b5cbSKonstantin Komarov 			if (pre_alloc && to_allocate + pre_alloc > free)
561be71b5cbSKonstantin Komarov 				pre_alloc = 0;
562be71b5cbSKonstantin Komarov 		}
563be71b5cbSKonstantin Komarov 
564be71b5cbSKonstantin Komarov 		vcn = old_alen;
565be71b5cbSKonstantin Komarov 
566be71b5cbSKonstantin Komarov 		if (is_ext) {
567be71b5cbSKonstantin Komarov 			if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
568be71b5cbSKonstantin Komarov 					   false)) {
569be71b5cbSKonstantin Komarov 				err = -ENOMEM;
570be71b5cbSKonstantin Komarov 				goto out;
571be71b5cbSKonstantin Komarov 			}
572be71b5cbSKonstantin Komarov 			alen = to_allocate;
573be71b5cbSKonstantin Komarov 		} else {
574e8b8e97fSKari Argillander 			/* ~3 bytes per fragment. */
575be71b5cbSKonstantin Komarov 			err = attr_allocate_clusters(
576be71b5cbSKonstantin Komarov 				sbi, run, vcn, lcn, to_allocate, &pre_alloc,
577c380b52fSKonstantin Komarov 				is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
57896de65a9SKonstantin Komarov 				is_mft ? 0 :
57996de65a9SKonstantin Komarov 					 (sbi->record_size -
580be71b5cbSKonstantin Komarov 					  le32_to_cpu(rec->used) + 8) /
581be71b5cbSKonstantin Komarov 							 3 +
582be71b5cbSKonstantin Komarov 						 1,
583c380b52fSKonstantin Komarov 				NULL, NULL);
584be71b5cbSKonstantin Komarov 			if (err)
585be71b5cbSKonstantin Komarov 				goto out;
586be71b5cbSKonstantin Komarov 		}
587be71b5cbSKonstantin Komarov 
588be71b5cbSKonstantin Komarov 		done += alen;
589be71b5cbSKonstantin Komarov 		vcn += alen;
590be71b5cbSKonstantin Komarov 		if (to_allocate > alen)
591be71b5cbSKonstantin Komarov 			to_allocate -= alen;
592be71b5cbSKonstantin Komarov 		else
593be71b5cbSKonstantin Komarov 			to_allocate = 0;
594be71b5cbSKonstantin Komarov 
595be71b5cbSKonstantin Komarov pack_runs:
596be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, vcn - svcn);
597be71b5cbSKonstantin Komarov 		if (err)
5980e5b044cSKonstantin Komarov 			goto undo_1;
599be71b5cbSKonstantin Komarov 
600be71b5cbSKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
601be71b5cbSKonstantin Komarov 		new_alloc_tmp = (u64)next_svcn << cluster_bits;
602be71b5cbSKonstantin Komarov 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
603ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
604be71b5cbSKonstantin Komarov 
605be71b5cbSKonstantin Komarov 		if (next_svcn >= vcn && !to_allocate) {
606e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
607be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
608be71b5cbSKonstantin Komarov 			goto ok;
609be71b5cbSKonstantin Komarov 		}
610be71b5cbSKonstantin Komarov 
611e8b8e97fSKari Argillander 		/* At least two MFT to avoid recursive loop. */
612be71b5cbSKonstantin Komarov 		if (is_mft && next_svcn == vcn &&
613be71b5cbSKonstantin Komarov 		    ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
614be71b5cbSKonstantin Komarov 			new_size = new_alloc_tmp;
615be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = attr_b->nres.alloc_size;
616be71b5cbSKonstantin Komarov 			goto ok;
617be71b5cbSKonstantin Komarov 		}
618be71b5cbSKonstantin Komarov 
619be71b5cbSKonstantin Komarov 		if (le32_to_cpu(rec->used) < sbi->record_size) {
620be71b5cbSKonstantin Komarov 			old_alen = next_svcn;
621be71b5cbSKonstantin Komarov 			evcn = old_alen - 1;
622be71b5cbSKonstantin Komarov 			goto add_alloc_in_same_attr_seg;
623be71b5cbSKonstantin Komarov 		}
624be71b5cbSKonstantin Komarov 
625be71b5cbSKonstantin Komarov 		attr_b->nres.data_size = attr_b->nres.alloc_size;
626be71b5cbSKonstantin Komarov 		if (new_alloc_tmp < old_valid)
627be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = attr_b->nres.data_size;
628be71b5cbSKonstantin Komarov 
629be71b5cbSKonstantin Komarov 		if (type == ATTR_LIST) {
630be71b5cbSKonstantin Komarov 			err = ni_expand_list(ni);
631be71b5cbSKonstantin Komarov 			if (err)
6320e5b044cSKonstantin Komarov 				goto undo_2;
633be71b5cbSKonstantin Komarov 			if (next_svcn < vcn)
634be71b5cbSKonstantin Komarov 				goto pack_runs;
635be71b5cbSKonstantin Komarov 
636e8b8e97fSKari Argillander 			/* Layout of records is changed. */
637be71b5cbSKonstantin Komarov 			goto again;
638be71b5cbSKonstantin Komarov 		}
639be71b5cbSKonstantin Komarov 
640be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
641be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
6420e5b044cSKonstantin Komarov 			/* In case of error layout of records is not changed. */
643be71b5cbSKonstantin Komarov 			if (err)
6440e5b044cSKonstantin Komarov 				goto undo_2;
645e8b8e97fSKari Argillander 			/* Layout of records is changed. */
646be71b5cbSKonstantin Komarov 		}
647be71b5cbSKonstantin Komarov 
648be71b5cbSKonstantin Komarov 		if (next_svcn >= vcn) {
649e8b8e97fSKari Argillander 			/* This is MFT data, repeat. */
650be71b5cbSKonstantin Komarov 			goto again;
651be71b5cbSKonstantin Komarov 		}
652be71b5cbSKonstantin Komarov 
653e8b8e97fSKari Argillander 		/* Insert new attribute segment. */
654be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, type, name, name_len, run,
655be71b5cbSKonstantin Komarov 					    next_svcn, vcn - next_svcn,
656c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
6570e5b044cSKonstantin Komarov 
6580e5b044cSKonstantin Komarov 		/*
6590e5b044cSKonstantin Komarov 		 * Layout of records maybe changed.
6600e5b044cSKonstantin Komarov 		 * Find base attribute to update.
6610e5b044cSKonstantin Komarov 		 */
6620e5b044cSKonstantin Komarov 		le_b = NULL;
6630e5b044cSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
6640e5b044cSKonstantin Komarov 				      NULL, &mi_b);
6650e5b044cSKonstantin Komarov 		if (!attr_b) {
6660e5b044cSKonstantin Komarov 			err = -EINVAL;
6670e5b044cSKonstantin Komarov 			goto bad_inode;
6680e5b044cSKonstantin Komarov 		}
6690e5b044cSKonstantin Komarov 
6700e5b044cSKonstantin Komarov 		if (err) {
6710e5b044cSKonstantin Komarov 			/* ni_insert_nonresident failed. */
6720e5b044cSKonstantin Komarov 			attr = NULL;
6730e5b044cSKonstantin Komarov 			goto undo_2;
6740e5b044cSKonstantin Komarov 		}
675be71b5cbSKonstantin Komarov 
676acb2835aSKonstantin Komarov 		/* keep runs for $MFT::$ATTR_DATA and $MFT::$ATTR_BITMAP. */
677acb2835aSKonstantin Komarov 		if (ni->mi.rno != MFT_REC_MFT)
678be71b5cbSKonstantin Komarov 			run_truncate_head(run, evcn + 1);
679be71b5cbSKonstantin Komarov 
680be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
681be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
682be71b5cbSKonstantin Komarov 
683e8b8e97fSKari Argillander 		/*
6840e5b044cSKonstantin Komarov 		 * Attribute is in consistency state.
6850e5b044cSKonstantin Komarov 		 * Save this point to restore to if next steps fail.
686e8b8e97fSKari Argillander 		 */
6870e5b044cSKonstantin Komarov 		old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
6880e5b044cSKonstantin Komarov 		attr_b->nres.valid_size = attr_b->nres.data_size =
6890e5b044cSKonstantin Komarov 			attr_b->nres.alloc_size = cpu_to_le64(old_size);
690ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
691be71b5cbSKonstantin Komarov 		goto again_1;
692be71b5cbSKonstantin Komarov 	}
693be71b5cbSKonstantin Komarov 
694be71b5cbSKonstantin Komarov 	if (new_size != old_size ||
695be71b5cbSKonstantin Komarov 	    (new_alloc != old_alloc && !keep_prealloc)) {
6960e5b044cSKonstantin Komarov 		/*
6970e5b044cSKonstantin Komarov 		 * Truncate clusters. In simple case we have to:
6980e5b044cSKonstantin Komarov 		 *  - update packed run in 'mi'
6990e5b044cSKonstantin Komarov 		 *  - update attr->nres.evcn
7000e5b044cSKonstantin Komarov 		 *  - update attr_b->nres.data_size/attr_b->nres.alloc_size
7010e5b044cSKonstantin Komarov 		 *  - mark and trim clusters as free (vcn, lcn, len)
7020e5b044cSKonstantin Komarov 		 */
7030e5b044cSKonstantin Komarov 		CLST dlen = 0;
7040e5b044cSKonstantin Komarov 
705be71b5cbSKonstantin Komarov 		vcn = max(svcn, new_alen);
706be71b5cbSKonstantin Komarov 		new_alloc_tmp = (u64)vcn << cluster_bits;
707be71b5cbSKonstantin Komarov 
708be71b5cbSKonstantin Komarov 		if (vcn > svcn) {
709be71b5cbSKonstantin Komarov 			err = mi_pack_runs(mi, attr, run, vcn - svcn);
710be71b5cbSKonstantin Komarov 			if (err)
711be71b5cbSKonstantin Komarov 				goto out;
712be71b5cbSKonstantin Komarov 		} else if (le && le->vcn) {
713be71b5cbSKonstantin Komarov 			u16 le_sz = le16_to_cpu(le->size);
714be71b5cbSKonstantin Komarov 
715be71b5cbSKonstantin Komarov 			/*
716e8b8e97fSKari Argillander 			 * NOTE: List entries for one attribute are always
717be71b5cbSKonstantin Komarov 			 * the same size. We deal with last entry (vcn==0)
718be71b5cbSKonstantin Komarov 			 * and it is not first in entries array
719e8b8e97fSKari Argillander 			 * (list entry for std attribute always first).
720e8b8e97fSKari Argillander 			 * So it is safe to step back.
721be71b5cbSKonstantin Komarov 			 */
72278ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
723be71b5cbSKonstantin Komarov 
724be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
725be71b5cbSKonstantin Komarov 				err = -EINVAL;
7260e5b044cSKonstantin Komarov 				goto bad_inode;
727be71b5cbSKonstantin Komarov 			}
728be71b5cbSKonstantin Komarov 
729be71b5cbSKonstantin Komarov 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
730be71b5cbSKonstantin Komarov 		} else {
731be71b5cbSKonstantin Komarov 			attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
732be71b5cbSKonstantin Komarov 			mi->dirty = true;
733be71b5cbSKonstantin Komarov 		}
734be71b5cbSKonstantin Komarov 
735be71b5cbSKonstantin Komarov 		attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
736be71b5cbSKonstantin Komarov 
737be71b5cbSKonstantin Komarov 		if (vcn == new_alen) {
738be71b5cbSKonstantin Komarov 			attr_b->nres.data_size = cpu_to_le64(new_size);
739be71b5cbSKonstantin Komarov 			if (new_size < old_valid)
740be71b5cbSKonstantin Komarov 				attr_b->nres.valid_size =
741be71b5cbSKonstantin Komarov 					attr_b->nres.data_size;
742be71b5cbSKonstantin Komarov 		} else {
743be71b5cbSKonstantin Komarov 			if (new_alloc_tmp <=
744be71b5cbSKonstantin Komarov 			    le64_to_cpu(attr_b->nres.data_size))
745be71b5cbSKonstantin Komarov 				attr_b->nres.data_size =
746be71b5cbSKonstantin Komarov 					attr_b->nres.alloc_size;
747be71b5cbSKonstantin Komarov 			if (new_alloc_tmp <
748be71b5cbSKonstantin Komarov 			    le64_to_cpu(attr_b->nres.valid_size))
749be71b5cbSKonstantin Komarov 				attr_b->nres.valid_size =
750be71b5cbSKonstantin Komarov 					attr_b->nres.alloc_size;
751be71b5cbSKonstantin Komarov 		}
752ad26a9c8SKonstantin Komarov 		mi_b->dirty = dirty = true;
753be71b5cbSKonstantin Komarov 
7540e5b044cSKonstantin Komarov 		err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
7550e5b044cSKonstantin Komarov 					true);
7560e5b044cSKonstantin Komarov 		if (err)
7570e5b044cSKonstantin Komarov 			goto out;
7580e5b044cSKonstantin Komarov 
7590e5b044cSKonstantin Komarov 		if (is_ext) {
7600e5b044cSKonstantin Komarov 			/* dlen - really deallocated clusters. */
7610e5b044cSKonstantin Komarov 			le64_sub_cpu(&attr_b->nres.total_size,
7620e5b044cSKonstantin Komarov 				     ((u64)dlen << cluster_bits));
7630e5b044cSKonstantin Komarov 		}
7640e5b044cSKonstantin Komarov 
7650e5b044cSKonstantin Komarov 		run_truncate(run, vcn);
7660e5b044cSKonstantin Komarov 
767be71b5cbSKonstantin Komarov 		if (new_alloc_tmp <= new_alloc)
768be71b5cbSKonstantin Komarov 			goto ok;
769be71b5cbSKonstantin Komarov 
770be71b5cbSKonstantin Komarov 		old_size = new_alloc_tmp;
771be71b5cbSKonstantin Komarov 		vcn = svcn - 1;
772be71b5cbSKonstantin Komarov 
773be71b5cbSKonstantin Komarov 		if (le == le_b) {
774be71b5cbSKonstantin Komarov 			attr = attr_b;
775be71b5cbSKonstantin Komarov 			mi = mi_b;
776be71b5cbSKonstantin Komarov 			evcn = svcn - 1;
777be71b5cbSKonstantin Komarov 			svcn = 0;
778be71b5cbSKonstantin Komarov 			goto next_le;
779be71b5cbSKonstantin Komarov 		}
780be71b5cbSKonstantin Komarov 
781be71b5cbSKonstantin Komarov 		if (le->type != type || le->name_len != name_len ||
782be71b5cbSKonstantin Komarov 		    memcmp(le_name(le), name, name_len * sizeof(short))) {
783be71b5cbSKonstantin Komarov 			err = -EINVAL;
7840e5b044cSKonstantin Komarov 			goto bad_inode;
785be71b5cbSKonstantin Komarov 		}
786be71b5cbSKonstantin Komarov 
787be71b5cbSKonstantin Komarov 		err = ni_load_mi(ni, le, &mi);
788be71b5cbSKonstantin Komarov 		if (err)
789be71b5cbSKonstantin Komarov 			goto out;
790be71b5cbSKonstantin Komarov 
791be71b5cbSKonstantin Komarov 		attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
792be71b5cbSKonstantin Komarov 		if (!attr) {
793be71b5cbSKonstantin Komarov 			err = -EINVAL;
7940e5b044cSKonstantin Komarov 			goto bad_inode;
795be71b5cbSKonstantin Komarov 		}
796be71b5cbSKonstantin Komarov 		goto next_le_1;
797be71b5cbSKonstantin Komarov 	}
798be71b5cbSKonstantin Komarov 
799be71b5cbSKonstantin Komarov ok:
800be71b5cbSKonstantin Komarov 	if (new_valid) {
801be71b5cbSKonstantin Komarov 		__le64 valid = cpu_to_le64(min(*new_valid, new_size));
802be71b5cbSKonstantin Komarov 
803be71b5cbSKonstantin Komarov 		if (attr_b->nres.valid_size != valid) {
804be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = valid;
805be71b5cbSKonstantin Komarov 			mi_b->dirty = true;
806be71b5cbSKonstantin Komarov 		}
807be71b5cbSKonstantin Komarov 	}
808be71b5cbSKonstantin Komarov 
8090e5b044cSKonstantin Komarov ok1:
8100e5b044cSKonstantin Komarov 	if (ret)
811be71b5cbSKonstantin Komarov 		*ret = attr_b;
812be71b5cbSKonstantin Komarov 
8130e5b044cSKonstantin Komarov 	if (((type == ATTR_DATA && !name_len) ||
814be71b5cbSKonstantin Komarov 	     (type == ATTR_ALLOC && name == I30_NAME))) {
815ad26a9c8SKonstantin Komarov 		/* Update inode_set_bytes. */
8160e5b044cSKonstantin Komarov 		if (attr_b->non_res) {
817be71b5cbSKonstantin Komarov 			new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
818be71b5cbSKonstantin Komarov 			if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
819be71b5cbSKonstantin Komarov 				inode_set_bytes(&ni->vfs_inode, new_alloc);
820be71b5cbSKonstantin Komarov 				dirty = true;
821be71b5cbSKonstantin Komarov 			}
822be71b5cbSKonstantin Komarov 		}
823be71b5cbSKonstantin Komarov 
824ad26a9c8SKonstantin Komarov 		/* Don't forget to update duplicate information in parent. */
825be71b5cbSKonstantin Komarov 		if (dirty) {
826be71b5cbSKonstantin Komarov 			ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
827be71b5cbSKonstantin Komarov 			mark_inode_dirty(&ni->vfs_inode);
828be71b5cbSKonstantin Komarov 		}
829be71b5cbSKonstantin Komarov 	}
830be71b5cbSKonstantin Komarov 
8310e5b044cSKonstantin Komarov 	return 0;
8320e5b044cSKonstantin Komarov 
8330e5b044cSKonstantin Komarov undo_2:
8340e5b044cSKonstantin Komarov 	vcn -= alen;
8350e5b044cSKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(old_size);
8360e5b044cSKonstantin Komarov 	attr_b->nres.valid_size = cpu_to_le64(old_valid);
8370e5b044cSKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
8380e5b044cSKonstantin Komarov 
8390e5b044cSKonstantin Komarov 	/* Restore 'attr' and 'mi'. */
8400e5b044cSKonstantin Komarov 	if (attr)
8410e5b044cSKonstantin Komarov 		goto restore_run;
8420e5b044cSKonstantin Komarov 
8430e5b044cSKonstantin Komarov 	if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
8440e5b044cSKonstantin Komarov 	    svcn <= le64_to_cpu(attr_b->nres.evcn)) {
8450e5b044cSKonstantin Komarov 		attr = attr_b;
8460e5b044cSKonstantin Komarov 		le = le_b;
8470e5b044cSKonstantin Komarov 		mi = mi_b;
8480e5b044cSKonstantin Komarov 	} else if (!le_b) {
8490e5b044cSKonstantin Komarov 		err = -EINVAL;
8500e5b044cSKonstantin Komarov 		goto bad_inode;
8510e5b044cSKonstantin Komarov 	} else {
8520e5b044cSKonstantin Komarov 		le = le_b;
8530e5b044cSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
8540e5b044cSKonstantin Komarov 				    &svcn, &mi);
8550e5b044cSKonstantin Komarov 		if (!attr)
8560e5b044cSKonstantin Komarov 			goto bad_inode;
8570e5b044cSKonstantin Komarov 	}
8580e5b044cSKonstantin Komarov 
8590e5b044cSKonstantin Komarov restore_run:
8600e5b044cSKonstantin Komarov 	if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
8610e5b044cSKonstantin Komarov 		is_bad = true;
8620e5b044cSKonstantin Komarov 
8630e5b044cSKonstantin Komarov undo_1:
8640e5b044cSKonstantin Komarov 	run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
8650e5b044cSKonstantin Komarov 
8660e5b044cSKonstantin Komarov 	run_truncate(run, vcn);
8670e5b044cSKonstantin Komarov out:
8680e5b044cSKonstantin Komarov 	if (is_bad) {
8690e5b044cSKonstantin Komarov bad_inode:
8700e5b044cSKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
8710e5b044cSKonstantin Komarov 	}
872be71b5cbSKonstantin Komarov 	return err;
873be71b5cbSKonstantin Komarov }
874be71b5cbSKonstantin Komarov 
875c380b52fSKonstantin Komarov /*
876c380b52fSKonstantin Komarov  * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
877c380b52fSKonstantin Komarov  *
878c380b52fSKonstantin Komarov  * @new == NULL means just to get current mapping for 'vcn'
879c380b52fSKonstantin Komarov  * @new != NULL means allocate real cluster if 'vcn' maps to hole
880c380b52fSKonstantin Komarov  * @zero - zeroout new allocated clusters
881c380b52fSKonstantin Komarov  *
882c380b52fSKonstantin Komarov  *  NOTE:
883c380b52fSKonstantin Komarov  *  - @new != NULL is called only for sparsed or compressed attributes.
884c380b52fSKonstantin Komarov  *  - new allocated clusters are zeroed via blkdev_issue_zeroout.
885c380b52fSKonstantin Komarov  */
attr_data_get_block(struct ntfs_inode * ni,CLST vcn,CLST clen,CLST * lcn,CLST * len,bool * new,bool zero)886be71b5cbSKonstantin Komarov int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
887c380b52fSKonstantin Komarov 			CLST *len, bool *new, bool zero)
888be71b5cbSKonstantin Komarov {
889be71b5cbSKonstantin Komarov 	int err = 0;
890be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
891be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi;
892be71b5cbSKonstantin Komarov 	u8 cluster_bits;
89328925712SKonstantin Komarov 	struct ATTRIB *attr, *attr_b;
894be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
895be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
896c380b52fSKonstantin Komarov 	CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
897910013f7SKonstantin Komarov 	CLST alloc, evcn;
898c380b52fSKonstantin Komarov 	unsigned fr;
899910013f7SKonstantin Komarov 	u64 total_size, total_size0;
900910013f7SKonstantin Komarov 	int step = 0;
901be71b5cbSKonstantin Komarov 
902be71b5cbSKonstantin Komarov 	if (new)
903be71b5cbSKonstantin Komarov 		*new = false;
904be71b5cbSKonstantin Komarov 
905c380b52fSKonstantin Komarov 	/* Try to find in cache. */
906be71b5cbSKonstantin Komarov 	down_read(&ni->file.run_lock);
907c380b52fSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
908c380b52fSKonstantin Komarov 		*len = 0;
909be71b5cbSKonstantin Komarov 	up_read(&ni->file.run_lock);
910be71b5cbSKonstantin Komarov 
91128925712SKonstantin Komarov 	if (*len && (*lcn != SPARSE_LCN || !new))
912c380b52fSKonstantin Komarov 		return 0; /* Fast normal way without allocation. */
913be71b5cbSKonstantin Komarov 
914c380b52fSKonstantin Komarov 	/* No cluster in cache or we need to allocate cluster in hole. */
915be71b5cbSKonstantin Komarov 	sbi = ni->mi.sbi;
916be71b5cbSKonstantin Komarov 	cluster_bits = sbi->cluster_bits;
917be71b5cbSKonstantin Komarov 
918be71b5cbSKonstantin Komarov 	ni_lock(ni);
919be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
920be71b5cbSKonstantin Komarov 
92128925712SKonstantin Komarov 	/* Repeat the code above (under write lock). */
92228925712SKonstantin Komarov 	if (!run_lookup_entry(run, vcn, lcn, len, NULL))
92328925712SKonstantin Komarov 		*len = 0;
92428925712SKonstantin Komarov 
92528925712SKonstantin Komarov 	if (*len) {
92628925712SKonstantin Komarov 		if (*lcn != SPARSE_LCN || !new)
92728925712SKonstantin Komarov 			goto out; /* normal way without allocation. */
92828925712SKonstantin Komarov 		if (clen > *len)
92928925712SKonstantin Komarov 			clen = *len;
93028925712SKonstantin Komarov 	}
93128925712SKonstantin Komarov 
932be71b5cbSKonstantin Komarov 	le_b = NULL;
933be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
934be71b5cbSKonstantin Komarov 	if (!attr_b) {
935be71b5cbSKonstantin Komarov 		err = -ENOENT;
936be71b5cbSKonstantin Komarov 		goto out;
937be71b5cbSKonstantin Komarov 	}
938be71b5cbSKonstantin Komarov 
939be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
940be71b5cbSKonstantin Komarov 		*lcn = RESIDENT_LCN;
941be71b5cbSKonstantin Komarov 		*len = 1;
942be71b5cbSKonstantin Komarov 		goto out;
943be71b5cbSKonstantin Komarov 	}
944be71b5cbSKonstantin Komarov 
945c1e0ab37SKonstantin Komarov 	asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
946be71b5cbSKonstantin Komarov 	if (vcn >= asize) {
947910013f7SKonstantin Komarov 		if (new) {
948be71b5cbSKonstantin Komarov 			err = -EINVAL;
949910013f7SKonstantin Komarov 		} else {
950910013f7SKonstantin Komarov 			*len = 1;
951910013f7SKonstantin Komarov 			*lcn = SPARSE_LCN;
952910013f7SKonstantin Komarov 		}
953be71b5cbSKonstantin Komarov 		goto out;
954be71b5cbSKonstantin Komarov 	}
955be71b5cbSKonstantin Komarov 
956be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
957be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
958be71b5cbSKonstantin Komarov 
959be71b5cbSKonstantin Komarov 	attr = attr_b;
960be71b5cbSKonstantin Komarov 	le = le_b;
961be71b5cbSKonstantin Komarov 	mi = mi_b;
962be71b5cbSKonstantin Komarov 
963be71b5cbSKonstantin Komarov 	if (le_b && (vcn < svcn || evcn1 <= vcn)) {
964be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
965be71b5cbSKonstantin Komarov 				    &mi);
966be71b5cbSKonstantin Komarov 		if (!attr) {
967be71b5cbSKonstantin Komarov 			err = -EINVAL;
968be71b5cbSKonstantin Komarov 			goto out;
969be71b5cbSKonstantin Komarov 		}
970be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
971be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
972be71b5cbSKonstantin Komarov 	}
973be71b5cbSKonstantin Komarov 
974c380b52fSKonstantin Komarov 	/* Load in cache actual information. */
975be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
976be71b5cbSKonstantin Komarov 	if (err)
977be71b5cbSKonstantin Komarov 		goto out;
978be71b5cbSKonstantin Komarov 
979645da4f9SKonstantin Komarov 	/* Check for compressed frame. */
98048ebb93fSKonstantin Komarov 	err = attr_is_frame_compressed(ni, attr_b, vcn >> NTFS_LZNT_CUNIT,
981*c2556801SKonstantin Komarov 				       &hint, run);
982645da4f9SKonstantin Komarov 	if (err)
983645da4f9SKonstantin Komarov 		goto out;
984645da4f9SKonstantin Komarov 
985645da4f9SKonstantin Komarov 	if (hint) {
986645da4f9SKonstantin Komarov 		/* if frame is compressed - don't touch it. */
987645da4f9SKonstantin Komarov 		*lcn = COMPRESSED_LCN;
98848ebb93fSKonstantin Komarov 		/* length to the end of frame. */
98948ebb93fSKonstantin Komarov 		*len = NTFS_LZNT_CLUSTERS - (vcn & (NTFS_LZNT_CLUSTERS - 1));
99048ebb93fSKonstantin Komarov 		err = 0;
991645da4f9SKonstantin Komarov 		goto out;
992645da4f9SKonstantin Komarov 	}
993645da4f9SKonstantin Komarov 
994c380b52fSKonstantin Komarov 	if (!*len) {
995c380b52fSKonstantin Komarov 		if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
996c380b52fSKonstantin Komarov 			if (*lcn != SPARSE_LCN || !new)
997c380b52fSKonstantin Komarov 				goto ok; /* Slow normal way without allocation. */
998be71b5cbSKonstantin Komarov 
999c380b52fSKonstantin Komarov 			if (clen > *len)
1000be71b5cbSKonstantin Komarov 				clen = *len;
1001c380b52fSKonstantin Komarov 		} else if (!new) {
1002c380b52fSKonstantin Komarov 			/* Here we may return -ENOENT.
1003c380b52fSKonstantin Komarov 			 * In any case caller gets zero length. */
1004c380b52fSKonstantin Komarov 			goto ok;
1005be71b5cbSKonstantin Komarov 		}
1006be71b5cbSKonstantin Komarov 	}
1007be71b5cbSKonstantin Komarov 
1008be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b)) {
1009c380b52fSKonstantin Komarov 		/* The code below only for sparsed or compressed attributes. */
1010be71b5cbSKonstantin Komarov 		err = -EINVAL;
1011be71b5cbSKonstantin Komarov 		goto out;
1012be71b5cbSKonstantin Komarov 	}
1013be71b5cbSKonstantin Komarov 
1014c380b52fSKonstantin Komarov 	vcn0 = vcn;
1015c380b52fSKonstantin Komarov 	to_alloc = clen;
1016c380b52fSKonstantin Komarov 	fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
1017c380b52fSKonstantin Komarov 	/* Allocate frame aligned clusters.
1018c380b52fSKonstantin Komarov 	 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
1019c380b52fSKonstantin Komarov 	 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
1020c380b52fSKonstantin Komarov 	if (attr_b->nres.c_unit) {
1021c380b52fSKonstantin Komarov 		CLST clst_per_frame = 1u << attr_b->nres.c_unit;
1022c380b52fSKonstantin Komarov 		CLST cmask = ~(clst_per_frame - 1);
1023c380b52fSKonstantin Komarov 
1024c380b52fSKonstantin Komarov 		/* Get frame aligned vcn and to_alloc. */
1025c380b52fSKonstantin Komarov 		vcn = vcn0 & cmask;
1026c380b52fSKonstantin Komarov 		to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
1027c380b52fSKonstantin Komarov 		if (fr < clst_per_frame)
1028c380b52fSKonstantin Komarov 			fr = clst_per_frame;
1029c380b52fSKonstantin Komarov 		zero = true;
1030c380b52fSKonstantin Komarov 
1031c380b52fSKonstantin Komarov 		/* Check if 'vcn' and 'vcn0' in different attribute segments. */
1032c380b52fSKonstantin Komarov 		if (vcn < svcn || evcn1 <= vcn) {
103348ebb93fSKonstantin Komarov 			struct ATTRIB *attr2;
103448ebb93fSKonstantin Komarov 			/* Load runs for truncated vcn. */
103548ebb93fSKonstantin Komarov 			attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL,
103648ebb93fSKonstantin Komarov 					     0, &vcn, &mi);
103748ebb93fSKonstantin Komarov 			if (!attr2) {
1038c380b52fSKonstantin Komarov 				err = -EINVAL;
1039c380b52fSKonstantin Komarov 				goto out;
1040c380b52fSKonstantin Komarov 			}
104148ebb93fSKonstantin Komarov 			evcn1 = le64_to_cpu(attr2->nres.evcn) + 1;
104248ebb93fSKonstantin Komarov 			err = attr_load_runs(attr2, ni, run, NULL);
1043c380b52fSKonstantin Komarov 			if (err)
1044c380b52fSKonstantin Komarov 				goto out;
1045c380b52fSKonstantin Komarov 		}
1046c380b52fSKonstantin Komarov 	}
1047c380b52fSKonstantin Komarov 
1048c380b52fSKonstantin Komarov 	if (vcn + to_alloc > asize)
1049c380b52fSKonstantin Komarov 		to_alloc = asize - vcn;
1050c380b52fSKonstantin Komarov 
1051e8b8e97fSKari Argillander 	/* Get the last LCN to allocate from. */
1052be71b5cbSKonstantin Komarov 	hint = 0;
1053be71b5cbSKonstantin Komarov 
1054be71b5cbSKonstantin Komarov 	if (vcn > evcn1) {
1055be71b5cbSKonstantin Komarov 		if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1056be71b5cbSKonstantin Komarov 				   false)) {
1057be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1058be71b5cbSKonstantin Komarov 			goto out;
1059be71b5cbSKonstantin Komarov 		}
1060be71b5cbSKonstantin Komarov 	} else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1061be71b5cbSKonstantin Komarov 		hint = -1;
1062be71b5cbSKonstantin Komarov 	}
1063be71b5cbSKonstantin Komarov 
1064c380b52fSKonstantin Komarov 	/* Allocate and zeroout new clusters. */
1065c380b52fSKonstantin Komarov 	err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
1066c380b52fSKonstantin Komarov 				     zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
1067c380b52fSKonstantin Komarov 				     fr, lcn, len);
1068be71b5cbSKonstantin Komarov 	if (err)
1069be71b5cbSKonstantin Komarov 		goto out;
1070be71b5cbSKonstantin Komarov 	*new = true;
1071910013f7SKonstantin Komarov 	step = 1;
1072be71b5cbSKonstantin Komarov 
1073c380b52fSKonstantin Komarov 	end = vcn + alen;
1074910013f7SKonstantin Komarov 	/* Save 'total_size0' to restore if error. */
1075910013f7SKonstantin Komarov 	total_size0 = le64_to_cpu(attr_b->nres.total_size);
1076910013f7SKonstantin Komarov 	total_size = total_size0 + ((u64)alen << cluster_bits);
1077c380b52fSKonstantin Komarov 
1078c380b52fSKonstantin Komarov 	if (vcn != vcn0) {
1079c380b52fSKonstantin Komarov 		if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1080c380b52fSKonstantin Komarov 			err = -EINVAL;
1081c380b52fSKonstantin Komarov 			goto out;
1082c380b52fSKonstantin Komarov 		}
1083c380b52fSKonstantin Komarov 		if (*lcn == SPARSE_LCN) {
1084c380b52fSKonstantin Komarov 			/* Internal error. Should not happened. */
1085c380b52fSKonstantin Komarov 			WARN_ON(1);
1086c380b52fSKonstantin Komarov 			err = -EINVAL;
1087c380b52fSKonstantin Komarov 			goto out;
1088c380b52fSKonstantin Komarov 		}
1089c380b52fSKonstantin Komarov 		/* Check case when vcn0 + len overlaps new allocated clusters. */
1090c380b52fSKonstantin Komarov 		if (vcn0 + *len > end)
1091c380b52fSKonstantin Komarov 			*len = end - vcn0;
1092c380b52fSKonstantin Komarov 	}
1093be71b5cbSKonstantin Komarov 
1094be71b5cbSKonstantin Komarov repack:
1095be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1096be71b5cbSKonstantin Komarov 	if (err)
1097be71b5cbSKonstantin Komarov 		goto out;
1098be71b5cbSKonstantin Komarov 
1099be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
1100be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
1101be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1102be71b5cbSKonstantin Komarov 
1103be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
1104be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
1105be71b5cbSKonstantin Komarov 
1106e8b8e97fSKari Argillander 	/* Stored [vcn : next_svcn) from [vcn : end). */
1107be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1108be71b5cbSKonstantin Komarov 
1109be71b5cbSKonstantin Komarov 	if (end <= evcn1) {
1110be71b5cbSKonstantin Komarov 		if (next_svcn == evcn1) {
1111e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
1112be71b5cbSKonstantin Komarov 			goto ok;
1113be71b5cbSKonstantin Komarov 		}
1114e8b8e97fSKari Argillander 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1115be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
1116be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
1117be71b5cbSKonstantin Komarov 			if (err)
1118910013f7SKonstantin Komarov 				goto undo1;
1119e8b8e97fSKari Argillander 			/* Layout of records is changed. */
1120be71b5cbSKonstantin Komarov 			le_b = NULL;
1121be71b5cbSKonstantin Komarov 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1122be71b5cbSKonstantin Komarov 					      0, NULL, &mi_b);
1123be71b5cbSKonstantin Komarov 			if (!attr_b) {
1124be71b5cbSKonstantin Komarov 				err = -ENOENT;
1125be71b5cbSKonstantin Komarov 				goto out;
1126be71b5cbSKonstantin Komarov 			}
1127be71b5cbSKonstantin Komarov 
1128be71b5cbSKonstantin Komarov 			attr = attr_b;
1129be71b5cbSKonstantin Komarov 			le = le_b;
1130be71b5cbSKonstantin Komarov 			mi = mi_b;
1131be71b5cbSKonstantin Komarov 			goto repack;
1132be71b5cbSKonstantin Komarov 		}
1133be71b5cbSKonstantin Komarov 	}
1134be71b5cbSKonstantin Komarov 
1135910013f7SKonstantin Komarov 	/*
1136910013f7SKonstantin Komarov 	 * The code below may require additional cluster (to extend attribute list)
1137910013f7SKonstantin Komarov 	 * and / or one MFT record
1138910013f7SKonstantin Komarov 	 * It is too complex to undo operations if -ENOSPC occurs deep inside
1139910013f7SKonstantin Komarov 	 * in 'ni_insert_nonresident'.
1140910013f7SKonstantin Komarov 	 * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
1141910013f7SKonstantin Komarov 	 */
1142910013f7SKonstantin Komarov 	if (!ntfs_check_for_free_space(sbi, 1, 1)) {
1143910013f7SKonstantin Komarov 		/* Undo step 1. */
1144910013f7SKonstantin Komarov 		err = -ENOSPC;
1145910013f7SKonstantin Komarov 		goto undo1;
1146910013f7SKonstantin Komarov 	}
1147910013f7SKonstantin Komarov 
1148910013f7SKonstantin Komarov 	step = 2;
1149be71b5cbSKonstantin Komarov 	svcn = evcn1;
1150be71b5cbSKonstantin Komarov 
1151e8b8e97fSKari Argillander 	/* Estimate next attribute. */
1152be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1153be71b5cbSKonstantin Komarov 
1154910013f7SKonstantin Komarov 	if (!attr) {
1155910013f7SKonstantin Komarov 		/* Insert new attribute segment. */
1156910013f7SKonstantin Komarov 		goto ins_ext;
1157910013f7SKonstantin Komarov 	}
1158910013f7SKonstantin Komarov 
1159910013f7SKonstantin Komarov 	/* Try to update existed attribute segment. */
1160910013f7SKonstantin Komarov 	alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
1161910013f7SKonstantin Komarov 	evcn = le64_to_cpu(attr->nres.evcn);
1162be71b5cbSKonstantin Komarov 
1163be71b5cbSKonstantin Komarov 	if (end < next_svcn)
1164be71b5cbSKonstantin Komarov 		end = next_svcn;
1165be71b5cbSKonstantin Komarov 	while (end > evcn) {
1166e8b8e97fSKari Argillander 		/* Remove segment [svcn : evcn). */
116778ab59feSKonstantin Komarov 		mi_remove_attr(NULL, mi, attr);
1168be71b5cbSKonstantin Komarov 
1169be71b5cbSKonstantin Komarov 		if (!al_remove_le(ni, le)) {
1170be71b5cbSKonstantin Komarov 			err = -EINVAL;
1171be71b5cbSKonstantin Komarov 			goto out;
1172be71b5cbSKonstantin Komarov 		}
1173be71b5cbSKonstantin Komarov 
1174be71b5cbSKonstantin Komarov 		if (evcn + 1 >= alloc) {
1175e8b8e97fSKari Argillander 			/* Last attribute segment. */
1176be71b5cbSKonstantin Komarov 			evcn1 = evcn + 1;
1177be71b5cbSKonstantin Komarov 			goto ins_ext;
1178be71b5cbSKonstantin Komarov 		}
1179be71b5cbSKonstantin Komarov 
1180be71b5cbSKonstantin Komarov 		if (ni_load_mi(ni, le, &mi)) {
1181be71b5cbSKonstantin Komarov 			attr = NULL;
1182be71b5cbSKonstantin Komarov 			goto out;
1183be71b5cbSKonstantin Komarov 		}
1184be71b5cbSKonstantin Komarov 
1185910013f7SKonstantin Komarov 		attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, &le->id);
1186be71b5cbSKonstantin Komarov 		if (!attr) {
1187be71b5cbSKonstantin Komarov 			err = -EINVAL;
1188be71b5cbSKonstantin Komarov 			goto out;
1189be71b5cbSKonstantin Komarov 		}
1190be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1191be71b5cbSKonstantin Komarov 		evcn = le64_to_cpu(attr->nres.evcn);
1192be71b5cbSKonstantin Komarov 	}
1193be71b5cbSKonstantin Komarov 
1194be71b5cbSKonstantin Komarov 	if (end < svcn)
1195be71b5cbSKonstantin Komarov 		end = svcn;
1196be71b5cbSKonstantin Komarov 
1197be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, &end);
1198be71b5cbSKonstantin Komarov 	if (err)
1199be71b5cbSKonstantin Komarov 		goto out;
1200be71b5cbSKonstantin Komarov 
1201be71b5cbSKonstantin Komarov 	evcn1 = evcn + 1;
1202be71b5cbSKonstantin Komarov 	attr->nres.svcn = cpu_to_le64(next_svcn);
1203be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1204be71b5cbSKonstantin Komarov 	if (err)
1205be71b5cbSKonstantin Komarov 		goto out;
1206be71b5cbSKonstantin Komarov 
1207be71b5cbSKonstantin Komarov 	le->vcn = cpu_to_le64(next_svcn);
1208be71b5cbSKonstantin Komarov 	ni->attr_list.dirty = true;
1209be71b5cbSKonstantin Komarov 	mi->dirty = true;
1210be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1211910013f7SKonstantin Komarov 
1212be71b5cbSKonstantin Komarov ins_ext:
1213be71b5cbSKonstantin Komarov 	if (evcn1 > next_svcn) {
1214be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1215be71b5cbSKonstantin Komarov 					    next_svcn, evcn1 - next_svcn,
1216c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
1217be71b5cbSKonstantin Komarov 		if (err)
1218be71b5cbSKonstantin Komarov 			goto out;
1219be71b5cbSKonstantin Komarov 	}
1220be71b5cbSKonstantin Komarov ok:
1221be71b5cbSKonstantin Komarov 	run_truncate_around(run, vcn);
1222be71b5cbSKonstantin Komarov out:
1223910013f7SKonstantin Komarov 	if (err && step > 1) {
1224910013f7SKonstantin Komarov 		/* Too complex to restore. */
1225910013f7SKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
1226910013f7SKonstantin Komarov 	}
1227be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
1228be71b5cbSKonstantin Komarov 	ni_unlock(ni);
1229be71b5cbSKonstantin Komarov 
1230be71b5cbSKonstantin Komarov 	return err;
1231910013f7SKonstantin Komarov 
1232910013f7SKonstantin Komarov undo1:
1233910013f7SKonstantin Komarov 	/* Undo step1. */
1234910013f7SKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size0);
1235910013f7SKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size0);
1236910013f7SKonstantin Komarov 
1237910013f7SKonstantin Komarov 	if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) ||
1238910013f7SKonstantin Komarov 	    !run_add_entry(run, vcn, SPARSE_LCN, alen, false) ||
1239910013f7SKonstantin Komarov 	    mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
1240910013f7SKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
1241910013f7SKonstantin Komarov 	}
1242910013f7SKonstantin Komarov 	goto out;
1243be71b5cbSKonstantin Komarov }
1244be71b5cbSKonstantin Komarov 
attr_data_read_resident(struct ntfs_inode * ni,struct page * page)1245be71b5cbSKonstantin Komarov int attr_data_read_resident(struct ntfs_inode *ni, struct page *page)
1246be71b5cbSKonstantin Komarov {
1247be71b5cbSKonstantin Komarov 	u64 vbo;
1248be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1249be71b5cbSKonstantin Komarov 	u32 data_size;
1250be71b5cbSKonstantin Komarov 
1251be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1252be71b5cbSKonstantin Komarov 	if (!attr)
1253be71b5cbSKonstantin Komarov 		return -EINVAL;
1254be71b5cbSKonstantin Komarov 
1255be71b5cbSKonstantin Komarov 	if (attr->non_res)
1256be71b5cbSKonstantin Komarov 		return E_NTFS_NONRESIDENT;
1257be71b5cbSKonstantin Komarov 
1258be71b5cbSKonstantin Komarov 	vbo = page->index << PAGE_SHIFT;
1259be71b5cbSKonstantin Komarov 	data_size = le32_to_cpu(attr->res.data_size);
1260be71b5cbSKonstantin Komarov 	if (vbo < data_size) {
1261be71b5cbSKonstantin Komarov 		const char *data = resident_data(attr);
1262be71b5cbSKonstantin Komarov 		char *kaddr = kmap_atomic(page);
1263be71b5cbSKonstantin Komarov 		u32 use = data_size - vbo;
1264be71b5cbSKonstantin Komarov 
1265be71b5cbSKonstantin Komarov 		if (use > PAGE_SIZE)
1266be71b5cbSKonstantin Komarov 			use = PAGE_SIZE;
1267be71b5cbSKonstantin Komarov 
1268be71b5cbSKonstantin Komarov 		memcpy(kaddr, data + vbo, use);
1269be71b5cbSKonstantin Komarov 		memset(kaddr + use, 0, PAGE_SIZE - use);
1270be71b5cbSKonstantin Komarov 		kunmap_atomic(kaddr);
1271be71b5cbSKonstantin Komarov 		flush_dcache_page(page);
1272be71b5cbSKonstantin Komarov 		SetPageUptodate(page);
1273be71b5cbSKonstantin Komarov 	} else if (!PageUptodate(page)) {
1274be71b5cbSKonstantin Komarov 		zero_user_segment(page, 0, PAGE_SIZE);
1275be71b5cbSKonstantin Komarov 		SetPageUptodate(page);
1276be71b5cbSKonstantin Komarov 	}
1277be71b5cbSKonstantin Komarov 
1278be71b5cbSKonstantin Komarov 	return 0;
1279be71b5cbSKonstantin Komarov }
1280be71b5cbSKonstantin Komarov 
attr_data_write_resident(struct ntfs_inode * ni,struct page * page)1281be71b5cbSKonstantin Komarov int attr_data_write_resident(struct ntfs_inode *ni, struct page *page)
1282be71b5cbSKonstantin Komarov {
1283be71b5cbSKonstantin Komarov 	u64 vbo;
1284be71b5cbSKonstantin Komarov 	struct mft_inode *mi;
1285be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1286be71b5cbSKonstantin Komarov 	u32 data_size;
1287be71b5cbSKonstantin Komarov 
1288be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1289be71b5cbSKonstantin Komarov 	if (!attr)
1290be71b5cbSKonstantin Komarov 		return -EINVAL;
1291be71b5cbSKonstantin Komarov 
1292be71b5cbSKonstantin Komarov 	if (attr->non_res) {
1293e8b8e97fSKari Argillander 		/* Return special error code to check this case. */
1294be71b5cbSKonstantin Komarov 		return E_NTFS_NONRESIDENT;
1295be71b5cbSKonstantin Komarov 	}
1296be71b5cbSKonstantin Komarov 
1297be71b5cbSKonstantin Komarov 	vbo = page->index << PAGE_SHIFT;
1298be71b5cbSKonstantin Komarov 	data_size = le32_to_cpu(attr->res.data_size);
1299be71b5cbSKonstantin Komarov 	if (vbo < data_size) {
1300be71b5cbSKonstantin Komarov 		char *data = resident_data(attr);
1301be71b5cbSKonstantin Komarov 		char *kaddr = kmap_atomic(page);
1302be71b5cbSKonstantin Komarov 		u32 use = data_size - vbo;
1303be71b5cbSKonstantin Komarov 
1304be71b5cbSKonstantin Komarov 		if (use > PAGE_SIZE)
1305be71b5cbSKonstantin Komarov 			use = PAGE_SIZE;
1306be71b5cbSKonstantin Komarov 		memcpy(data + vbo, kaddr, use);
1307be71b5cbSKonstantin Komarov 		kunmap_atomic(kaddr);
1308be71b5cbSKonstantin Komarov 		mi->dirty = true;
1309be71b5cbSKonstantin Komarov 	}
1310be71b5cbSKonstantin Komarov 	ni->i_valid = data_size;
1311be71b5cbSKonstantin Komarov 
1312be71b5cbSKonstantin Komarov 	return 0;
1313be71b5cbSKonstantin Komarov }
1314be71b5cbSKonstantin Komarov 
1315be71b5cbSKonstantin Komarov /*
1316e8b8e97fSKari Argillander  * attr_load_runs_vcn - Load runs with VCN.
1317be71b5cbSKonstantin 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)1318be71b5cbSKonstantin Komarov int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1319be71b5cbSKonstantin Komarov 		       const __le16 *name, u8 name_len, struct runs_tree *run,
1320be71b5cbSKonstantin Komarov 		       CLST vcn)
1321be71b5cbSKonstantin Komarov {
1322be71b5cbSKonstantin Komarov 	struct ATTRIB *attr;
1323be71b5cbSKonstantin Komarov 	int err;
1324be71b5cbSKonstantin Komarov 	CLST svcn, evcn;
1325be71b5cbSKonstantin Komarov 	u16 ro;
1326be71b5cbSKonstantin Komarov 
13272681631cSEdward Lo 	if (!ni) {
13282681631cSEdward Lo 		/* Is record corrupted? */
13292681631cSEdward Lo 		return -ENOENT;
13302681631cSEdward Lo 	}
13312681631cSEdward Lo 
1332be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1333d3624466SKonstantin Komarov 	if (!attr) {
1334d3624466SKonstantin Komarov 		/* Is record corrupted? */
1335be71b5cbSKonstantin Komarov 		return -ENOENT;
1336d3624466SKonstantin Komarov 	}
1337be71b5cbSKonstantin Komarov 
1338be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr->nres.svcn);
1339be71b5cbSKonstantin Komarov 	evcn = le64_to_cpu(attr->nres.evcn);
1340be71b5cbSKonstantin Komarov 
1341d3624466SKonstantin Komarov 	if (evcn < vcn || vcn < svcn) {
1342d3624466SKonstantin Komarov 		/* Is record corrupted? */
1343be71b5cbSKonstantin Komarov 		return -EINVAL;
1344d3624466SKonstantin Komarov 	}
1345be71b5cbSKonstantin Komarov 
1346be71b5cbSKonstantin Komarov 	ro = le16_to_cpu(attr->nres.run_off);
13476db62086SEdward Lo 
13486db62086SEdward Lo 	if (ro > le32_to_cpu(attr->size))
13496db62086SEdward Lo 		return -EINVAL;
13506db62086SEdward Lo 
1351be71b5cbSKonstantin Komarov 	err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1352be71b5cbSKonstantin Komarov 			    Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1353be71b5cbSKonstantin Komarov 	if (err < 0)
1354be71b5cbSKonstantin Komarov 		return err;
1355be71b5cbSKonstantin Komarov 	return 0;
1356be71b5cbSKonstantin Komarov }
1357be71b5cbSKonstantin Komarov 
1358be71b5cbSKonstantin Komarov /*
1359d3624466SKonstantin Komarov  * attr_load_runs_range - Load runs for given range [from to).
1360be71b5cbSKonstantin 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)1361be71b5cbSKonstantin Komarov int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1362be71b5cbSKonstantin Komarov 			 const __le16 *name, u8 name_len, struct runs_tree *run,
1363be71b5cbSKonstantin Komarov 			 u64 from, u64 to)
1364be71b5cbSKonstantin Komarov {
1365be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1366be71b5cbSKonstantin Komarov 	u8 cluster_bits = sbi->cluster_bits;
1367dc8965abSColin Ian King 	CLST vcn;
1368be71b5cbSKonstantin Komarov 	CLST vcn_last = (to - 1) >> cluster_bits;
1369be71b5cbSKonstantin Komarov 	CLST lcn, clen;
1370be71b5cbSKonstantin Komarov 	int err;
1371be71b5cbSKonstantin Komarov 
1372be71b5cbSKonstantin Komarov 	for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1373be71b5cbSKonstantin Komarov 		if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1374be71b5cbSKonstantin Komarov 			err = attr_load_runs_vcn(ni, type, name, name_len, run,
1375be71b5cbSKonstantin Komarov 						 vcn);
1376be71b5cbSKonstantin Komarov 			if (err)
1377be71b5cbSKonstantin Komarov 				return err;
1378e8b8e97fSKari Argillander 			clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1379be71b5cbSKonstantin Komarov 		}
1380be71b5cbSKonstantin Komarov 	}
1381be71b5cbSKonstantin Komarov 
1382be71b5cbSKonstantin Komarov 	return 0;
1383be71b5cbSKonstantin Komarov }
1384be71b5cbSKonstantin Komarov 
1385be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
1386be71b5cbSKonstantin Komarov /*
1387be71b5cbSKonstantin Komarov  * attr_wof_frame_info
1388be71b5cbSKonstantin Komarov  *
1389e8b8e97fSKari Argillander  * Read header of Xpress/LZX file to get info about frame.
1390be71b5cbSKonstantin 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)1391be71b5cbSKonstantin Komarov int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1392be71b5cbSKonstantin Komarov 			struct runs_tree *run, u64 frame, u64 frames,
1393be71b5cbSKonstantin Komarov 			u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1394be71b5cbSKonstantin Komarov {
1395be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1396be71b5cbSKonstantin Komarov 	u64 vbo[2], off[2], wof_size;
1397be71b5cbSKonstantin Komarov 	u32 voff;
1398be71b5cbSKonstantin Komarov 	u8 bytes_per_off;
1399be71b5cbSKonstantin Komarov 	char *addr;
1400be71b5cbSKonstantin Komarov 	struct page *page;
1401be71b5cbSKonstantin Komarov 	int i, err;
1402be71b5cbSKonstantin Komarov 	__le32 *off32;
1403be71b5cbSKonstantin Komarov 	__le64 *off64;
1404be71b5cbSKonstantin Komarov 
1405be71b5cbSKonstantin Komarov 	if (ni->vfs_inode.i_size < 0x100000000ull) {
1406e8b8e97fSKari Argillander 		/* File starts with array of 32 bit offsets. */
1407be71b5cbSKonstantin Komarov 		bytes_per_off = sizeof(__le32);
1408be71b5cbSKonstantin Komarov 		vbo[1] = frame << 2;
1409be71b5cbSKonstantin Komarov 		*vbo_data = frames << 2;
1410be71b5cbSKonstantin Komarov 	} else {
1411e8b8e97fSKari Argillander 		/* File starts with array of 64 bit offsets. */
1412be71b5cbSKonstantin Komarov 		bytes_per_off = sizeof(__le64);
1413be71b5cbSKonstantin Komarov 		vbo[1] = frame << 3;
1414be71b5cbSKonstantin Komarov 		*vbo_data = frames << 3;
1415be71b5cbSKonstantin Komarov 	}
1416be71b5cbSKonstantin Komarov 
1417be71b5cbSKonstantin Komarov 	/*
1418e8b8e97fSKari Argillander 	 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1419e8b8e97fSKari Argillander 	 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1420be71b5cbSKonstantin Komarov 	 */
1421be71b5cbSKonstantin Komarov 	if (!attr->non_res) {
1422be71b5cbSKonstantin Komarov 		if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1423be71b5cbSKonstantin Komarov 			ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1424be71b5cbSKonstantin Komarov 			return -EINVAL;
1425be71b5cbSKonstantin Komarov 		}
1426be71b5cbSKonstantin Komarov 		addr = resident_data(attr);
1427be71b5cbSKonstantin Komarov 
1428be71b5cbSKonstantin Komarov 		if (bytes_per_off == sizeof(__le32)) {
1429be71b5cbSKonstantin Komarov 			off32 = Add2Ptr(addr, vbo[1]);
1430be71b5cbSKonstantin Komarov 			off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1431be71b5cbSKonstantin Komarov 			off[1] = le32_to_cpu(off32[0]);
1432be71b5cbSKonstantin Komarov 		} else {
1433be71b5cbSKonstantin Komarov 			off64 = Add2Ptr(addr, vbo[1]);
1434be71b5cbSKonstantin Komarov 			off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1435be71b5cbSKonstantin Komarov 			off[1] = le64_to_cpu(off64[0]);
1436be71b5cbSKonstantin Komarov 		}
1437be71b5cbSKonstantin Komarov 
1438be71b5cbSKonstantin Komarov 		*vbo_data += off[0];
1439be71b5cbSKonstantin Komarov 		*ondisk_size = off[1] - off[0];
1440be71b5cbSKonstantin Komarov 		return 0;
1441be71b5cbSKonstantin Komarov 	}
1442be71b5cbSKonstantin Komarov 
1443be71b5cbSKonstantin Komarov 	wof_size = le64_to_cpu(attr->nres.data_size);
1444be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
1445be71b5cbSKonstantin Komarov 	page = ni->file.offs_page;
1446be71b5cbSKonstantin Komarov 	if (!page) {
1447be71b5cbSKonstantin Komarov 		page = alloc_page(GFP_KERNEL);
1448be71b5cbSKonstantin Komarov 		if (!page) {
1449be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1450be71b5cbSKonstantin Komarov 			goto out;
1451be71b5cbSKonstantin Komarov 		}
1452be71b5cbSKonstantin Komarov 		page->index = -1;
1453be71b5cbSKonstantin Komarov 		ni->file.offs_page = page;
1454be71b5cbSKonstantin Komarov 	}
1455be71b5cbSKonstantin Komarov 	lock_page(page);
1456be71b5cbSKonstantin Komarov 	addr = page_address(page);
1457be71b5cbSKonstantin Komarov 
1458be71b5cbSKonstantin Komarov 	if (vbo[1]) {
1459be71b5cbSKonstantin Komarov 		voff = vbo[1] & (PAGE_SIZE - 1);
1460be71b5cbSKonstantin Komarov 		vbo[0] = vbo[1] - bytes_per_off;
1461be71b5cbSKonstantin Komarov 		i = 0;
1462be71b5cbSKonstantin Komarov 	} else {
1463be71b5cbSKonstantin Komarov 		voff = 0;
1464be71b5cbSKonstantin Komarov 		vbo[0] = 0;
1465be71b5cbSKonstantin Komarov 		off[0] = 0;
1466be71b5cbSKonstantin Komarov 		i = 1;
1467be71b5cbSKonstantin Komarov 	}
1468be71b5cbSKonstantin Komarov 
1469be71b5cbSKonstantin Komarov 	do {
1470be71b5cbSKonstantin Komarov 		pgoff_t index = vbo[i] >> PAGE_SHIFT;
1471be71b5cbSKonstantin Komarov 
1472be71b5cbSKonstantin Komarov 		if (index != page->index) {
1473be71b5cbSKonstantin Komarov 			u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1474be71b5cbSKonstantin Komarov 			u64 to = min(from + PAGE_SIZE, wof_size);
1475be71b5cbSKonstantin Komarov 
1476be71b5cbSKonstantin Komarov 			err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1477be71b5cbSKonstantin Komarov 						   ARRAY_SIZE(WOF_NAME), run,
1478be71b5cbSKonstantin Komarov 						   from, to);
1479be71b5cbSKonstantin Komarov 			if (err)
1480be71b5cbSKonstantin Komarov 				goto out1;
1481be71b5cbSKonstantin Komarov 
1482be71b5cbSKonstantin Komarov 			err = ntfs_bio_pages(sbi, run, &page, 1, from,
1483be71b5cbSKonstantin Komarov 					     to - from, REQ_OP_READ);
1484be71b5cbSKonstantin Komarov 			if (err) {
1485be71b5cbSKonstantin Komarov 				page->index = -1;
1486be71b5cbSKonstantin Komarov 				goto out1;
1487be71b5cbSKonstantin Komarov 			}
1488be71b5cbSKonstantin Komarov 			page->index = index;
1489be71b5cbSKonstantin Komarov 		}
1490be71b5cbSKonstantin Komarov 
1491be71b5cbSKonstantin Komarov 		if (i) {
1492be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1493be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, voff);
1494be71b5cbSKonstantin Komarov 				off[1] = le32_to_cpu(*off32);
1495be71b5cbSKonstantin Komarov 			} else {
1496be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, voff);
1497be71b5cbSKonstantin Komarov 				off[1] = le64_to_cpu(*off64);
1498be71b5cbSKonstantin Komarov 			}
1499be71b5cbSKonstantin Komarov 		} else if (!voff) {
1500be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1501be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1502be71b5cbSKonstantin Komarov 				off[0] = le32_to_cpu(*off32);
1503be71b5cbSKonstantin Komarov 			} else {
1504be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1505be71b5cbSKonstantin Komarov 				off[0] = le64_to_cpu(*off64);
1506be71b5cbSKonstantin Komarov 			}
1507be71b5cbSKonstantin Komarov 		} else {
1508e8b8e97fSKari Argillander 			/* Two values in one page. */
1509be71b5cbSKonstantin Komarov 			if (bytes_per_off == sizeof(__le32)) {
1510be71b5cbSKonstantin Komarov 				off32 = Add2Ptr(addr, voff);
1511be71b5cbSKonstantin Komarov 				off[0] = le32_to_cpu(off32[-1]);
1512be71b5cbSKonstantin Komarov 				off[1] = le32_to_cpu(off32[0]);
1513be71b5cbSKonstantin Komarov 			} else {
1514be71b5cbSKonstantin Komarov 				off64 = Add2Ptr(addr, voff);
1515be71b5cbSKonstantin Komarov 				off[0] = le64_to_cpu(off64[-1]);
1516be71b5cbSKonstantin Komarov 				off[1] = le64_to_cpu(off64[0]);
1517be71b5cbSKonstantin Komarov 			}
1518be71b5cbSKonstantin Komarov 			break;
1519be71b5cbSKonstantin Komarov 		}
1520be71b5cbSKonstantin Komarov 	} while (++i < 2);
1521be71b5cbSKonstantin Komarov 
1522be71b5cbSKonstantin Komarov 	*vbo_data += off[0];
1523be71b5cbSKonstantin Komarov 	*ondisk_size = off[1] - off[0];
1524be71b5cbSKonstantin Komarov 
1525be71b5cbSKonstantin Komarov out1:
1526be71b5cbSKonstantin Komarov 	unlock_page(page);
1527be71b5cbSKonstantin Komarov out:
1528be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
1529be71b5cbSKonstantin Komarov 	return err;
1530be71b5cbSKonstantin Komarov }
1531be71b5cbSKonstantin Komarov #endif
1532be71b5cbSKonstantin Komarov 
1533be71b5cbSKonstantin Komarov /*
1534e8b8e97fSKari Argillander  * attr_is_frame_compressed - Used to detect compressed frame.
153548ebb93fSKonstantin Komarov  *
153648ebb93fSKonstantin Komarov  * attr - base (primary) attribute segment.
1537*c2556801SKonstantin Komarov  * run  - run to use, usually == &ni->file.run.
153848ebb93fSKonstantin Komarov  * Only base segments contains valid 'attr->nres.c_unit'
1539be71b5cbSKonstantin Komarov  */
attr_is_frame_compressed(struct ntfs_inode * ni,struct ATTRIB * attr,CLST frame,CLST * clst_data,struct runs_tree * run)1540be71b5cbSKonstantin Komarov int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1541*c2556801SKonstantin Komarov 			     CLST frame, CLST *clst_data, struct runs_tree *run)
1542be71b5cbSKonstantin Komarov {
1543be71b5cbSKonstantin Komarov 	int err;
1544be71b5cbSKonstantin Komarov 	u32 clst_frame;
1545be71b5cbSKonstantin Komarov 	CLST clen, lcn, vcn, alen, slen, vcn_next;
1546be71b5cbSKonstantin Komarov 	size_t idx;
1547be71b5cbSKonstantin Komarov 
1548be71b5cbSKonstantin Komarov 	*clst_data = 0;
1549be71b5cbSKonstantin Komarov 
1550be71b5cbSKonstantin Komarov 	if (!is_attr_compressed(attr))
1551be71b5cbSKonstantin Komarov 		return 0;
1552be71b5cbSKonstantin Komarov 
1553be71b5cbSKonstantin Komarov 	if (!attr->non_res)
1554be71b5cbSKonstantin Komarov 		return 0;
1555be71b5cbSKonstantin Komarov 
1556be71b5cbSKonstantin Komarov 	clst_frame = 1u << attr->nres.c_unit;
1557be71b5cbSKonstantin Komarov 	vcn = frame * clst_frame;
1558be71b5cbSKonstantin Komarov 
1559be71b5cbSKonstantin Komarov 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1560be71b5cbSKonstantin Komarov 		err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1561be71b5cbSKonstantin Komarov 					 attr->name_len, run, vcn);
1562be71b5cbSKonstantin Komarov 		if (err)
1563be71b5cbSKonstantin Komarov 			return err;
1564be71b5cbSKonstantin Komarov 
1565be71b5cbSKonstantin Komarov 		if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1566be71b5cbSKonstantin Komarov 			return -EINVAL;
1567be71b5cbSKonstantin Komarov 	}
1568be71b5cbSKonstantin Komarov 
1569be71b5cbSKonstantin Komarov 	if (lcn == SPARSE_LCN) {
1570e8b8e97fSKari Argillander 		/* Sparsed frame. */
1571be71b5cbSKonstantin Komarov 		return 0;
1572be71b5cbSKonstantin Komarov 	}
1573be71b5cbSKonstantin Komarov 
1574be71b5cbSKonstantin Komarov 	if (clen >= clst_frame) {
1575be71b5cbSKonstantin Komarov 		/*
1576be71b5cbSKonstantin Komarov 		 * The frame is not compressed 'cause
1577e8b8e97fSKari Argillander 		 * it does not contain any sparse clusters.
1578be71b5cbSKonstantin Komarov 		 */
1579be71b5cbSKonstantin Komarov 		*clst_data = clst_frame;
1580be71b5cbSKonstantin Komarov 		return 0;
1581be71b5cbSKonstantin Komarov 	}
1582be71b5cbSKonstantin Komarov 
1583be71b5cbSKonstantin Komarov 	alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1584be71b5cbSKonstantin Komarov 	slen = 0;
1585be71b5cbSKonstantin Komarov 	*clst_data = clen;
1586be71b5cbSKonstantin Komarov 
1587be71b5cbSKonstantin Komarov 	/*
1588e8b8e97fSKari Argillander 	 * The frame is compressed if *clst_data + slen >= clst_frame.
1589e8b8e97fSKari Argillander 	 * Check next fragments.
1590be71b5cbSKonstantin Komarov 	 */
1591be71b5cbSKonstantin Komarov 	while ((vcn += clen) < alen) {
1592be71b5cbSKonstantin Komarov 		vcn_next = vcn;
1593be71b5cbSKonstantin Komarov 
1594be71b5cbSKonstantin Komarov 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1595be71b5cbSKonstantin Komarov 		    vcn_next != vcn) {
1596be71b5cbSKonstantin Komarov 			err = attr_load_runs_vcn(ni, attr->type,
1597be71b5cbSKonstantin Komarov 						 attr_name(attr),
1598be71b5cbSKonstantin Komarov 						 attr->name_len, run, vcn_next);
1599be71b5cbSKonstantin Komarov 			if (err)
1600be71b5cbSKonstantin Komarov 				return err;
1601be71b5cbSKonstantin Komarov 			vcn = vcn_next;
1602be71b5cbSKonstantin Komarov 
1603be71b5cbSKonstantin Komarov 			if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1604be71b5cbSKonstantin Komarov 				return -EINVAL;
1605be71b5cbSKonstantin Komarov 		}
1606be71b5cbSKonstantin Komarov 
1607be71b5cbSKonstantin Komarov 		if (lcn == SPARSE_LCN) {
1608be71b5cbSKonstantin Komarov 			slen += clen;
1609be71b5cbSKonstantin Komarov 		} else {
1610be71b5cbSKonstantin Komarov 			if (slen) {
1611be71b5cbSKonstantin Komarov 				/*
1612e8b8e97fSKari Argillander 				 * Data_clusters + sparse_clusters =
1613e8b8e97fSKari Argillander 				 * not enough for frame.
1614be71b5cbSKonstantin Komarov 				 */
1615be71b5cbSKonstantin Komarov 				return -EINVAL;
1616be71b5cbSKonstantin Komarov 			}
1617be71b5cbSKonstantin Komarov 			*clst_data += clen;
1618be71b5cbSKonstantin Komarov 		}
1619be71b5cbSKonstantin Komarov 
1620be71b5cbSKonstantin Komarov 		if (*clst_data + slen >= clst_frame) {
1621be71b5cbSKonstantin Komarov 			if (!slen) {
1622be71b5cbSKonstantin Komarov 				/*
1623be71b5cbSKonstantin Komarov 				 * There is no sparsed clusters in this frame
1624e8b8e97fSKari Argillander 				 * so it is not compressed.
1625be71b5cbSKonstantin Komarov 				 */
1626be71b5cbSKonstantin Komarov 				*clst_data = clst_frame;
1627be71b5cbSKonstantin Komarov 			} else {
1628e8b8e97fSKari Argillander 				/* Frame is compressed. */
1629be71b5cbSKonstantin Komarov 			}
1630be71b5cbSKonstantin Komarov 			break;
1631be71b5cbSKonstantin Komarov 		}
1632be71b5cbSKonstantin Komarov 	}
1633be71b5cbSKonstantin Komarov 
1634be71b5cbSKonstantin Komarov 	return 0;
1635be71b5cbSKonstantin Komarov }
1636be71b5cbSKonstantin Komarov 
1637be71b5cbSKonstantin Komarov /*
1638e8b8e97fSKari Argillander  * attr_allocate_frame - Allocate/free clusters for @frame.
1639be71b5cbSKonstantin Komarov  *
1640e8b8e97fSKari Argillander  * Assumed: down_write(&ni->file.run_lock);
1641be71b5cbSKonstantin Komarov  */
attr_allocate_frame(struct ntfs_inode * ni,CLST frame,size_t compr_size,u64 new_valid)1642be71b5cbSKonstantin Komarov int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1643be71b5cbSKonstantin Komarov 			u64 new_valid)
1644be71b5cbSKonstantin Komarov {
1645be71b5cbSKonstantin Komarov 	int err = 0;
1646be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
1647be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1648be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
1649be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
1650be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
1651c380b52fSKonstantin Komarov 	CLST svcn, evcn1, next_svcn, len;
1652be71b5cbSKonstantin Komarov 	CLST vcn, end, clst_data;
1653be71b5cbSKonstantin Komarov 	u64 total_size, valid_size, data_size;
1654be71b5cbSKonstantin Komarov 
1655be71b5cbSKonstantin Komarov 	le_b = NULL;
1656be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1657be71b5cbSKonstantin Komarov 	if (!attr_b)
1658be71b5cbSKonstantin Komarov 		return -ENOENT;
1659be71b5cbSKonstantin Komarov 
1660be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b))
1661be71b5cbSKonstantin Komarov 		return -EINVAL;
1662be71b5cbSKonstantin Komarov 
1663be71b5cbSKonstantin Komarov 	vcn = frame << NTFS_LZNT_CUNIT;
1664be71b5cbSKonstantin Komarov 	total_size = le64_to_cpu(attr_b->nres.total_size);
1665be71b5cbSKonstantin Komarov 
1666be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
1667be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1668be71b5cbSKonstantin Komarov 	data_size = le64_to_cpu(attr_b->nres.data_size);
1669be71b5cbSKonstantin Komarov 
1670be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
1671be71b5cbSKonstantin Komarov 		attr = attr_b;
1672be71b5cbSKonstantin Komarov 		le = le_b;
1673be71b5cbSKonstantin Komarov 		mi = mi_b;
1674be71b5cbSKonstantin Komarov 	} else if (!le_b) {
1675be71b5cbSKonstantin Komarov 		err = -EINVAL;
1676be71b5cbSKonstantin Komarov 		goto out;
1677be71b5cbSKonstantin Komarov 	} else {
1678be71b5cbSKonstantin Komarov 		le = le_b;
1679be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1680be71b5cbSKonstantin Komarov 				    &mi);
1681be71b5cbSKonstantin Komarov 		if (!attr) {
1682be71b5cbSKonstantin Komarov 			err = -EINVAL;
1683be71b5cbSKonstantin Komarov 			goto out;
1684be71b5cbSKonstantin Komarov 		}
1685be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1686be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1687be71b5cbSKonstantin Komarov 	}
1688be71b5cbSKonstantin Komarov 
1689be71b5cbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
1690be71b5cbSKonstantin Komarov 	if (err)
1691be71b5cbSKonstantin Komarov 		goto out;
1692be71b5cbSKonstantin Komarov 
1693*c2556801SKonstantin Komarov 	err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data, run);
1694be71b5cbSKonstantin Komarov 	if (err)
1695be71b5cbSKonstantin Komarov 		goto out;
1696be71b5cbSKonstantin Komarov 
1697be71b5cbSKonstantin Komarov 	total_size -= (u64)clst_data << sbi->cluster_bits;
1698be71b5cbSKonstantin Komarov 
1699be71b5cbSKonstantin Komarov 	len = bytes_to_cluster(sbi, compr_size);
1700be71b5cbSKonstantin Komarov 
1701be71b5cbSKonstantin Komarov 	if (len == clst_data)
1702be71b5cbSKonstantin Komarov 		goto out;
1703be71b5cbSKonstantin Komarov 
1704be71b5cbSKonstantin Komarov 	if (len < clst_data) {
1705be71b5cbSKonstantin Komarov 		err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1706be71b5cbSKonstantin Komarov 					NULL, true);
1707be71b5cbSKonstantin Komarov 		if (err)
1708be71b5cbSKonstantin Komarov 			goto out;
1709be71b5cbSKonstantin Komarov 
1710be71b5cbSKonstantin Komarov 		if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1711be71b5cbSKonstantin Komarov 				   false)) {
1712be71b5cbSKonstantin Komarov 			err = -ENOMEM;
1713be71b5cbSKonstantin Komarov 			goto out;
1714be71b5cbSKonstantin Komarov 		}
1715be71b5cbSKonstantin Komarov 		end = vcn + clst_data;
1716e8b8e97fSKari Argillander 		/* Run contains updated range [vcn + len : end). */
1717be71b5cbSKonstantin Komarov 	} else {
1718be71b5cbSKonstantin Komarov 		CLST alen, hint = 0;
1719e8b8e97fSKari Argillander 		/* Get the last LCN to allocate from. */
1720be71b5cbSKonstantin Komarov 		if (vcn + clst_data &&
1721be71b5cbSKonstantin Komarov 		    !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1722be71b5cbSKonstantin Komarov 				      NULL)) {
1723be71b5cbSKonstantin Komarov 			hint = -1;
1724be71b5cbSKonstantin Komarov 		}
1725be71b5cbSKonstantin Komarov 
1726be71b5cbSKonstantin Komarov 		err = attr_allocate_clusters(sbi, run, vcn + clst_data,
1727c380b52fSKonstantin Komarov 					     hint + 1, len - clst_data, NULL,
1728c380b52fSKonstantin Komarov 					     ALLOCATE_DEF, &alen, 0, NULL,
1729c380b52fSKonstantin Komarov 					     NULL);
1730be71b5cbSKonstantin Komarov 		if (err)
1731be71b5cbSKonstantin Komarov 			goto out;
1732be71b5cbSKonstantin Komarov 
1733be71b5cbSKonstantin Komarov 		end = vcn + len;
1734e8b8e97fSKari Argillander 		/* Run contains updated range [vcn + clst_data : end). */
1735be71b5cbSKonstantin Komarov 	}
1736be71b5cbSKonstantin Komarov 
1737be71b5cbSKonstantin Komarov 	total_size += (u64)len << sbi->cluster_bits;
1738be71b5cbSKonstantin Komarov 
1739be71b5cbSKonstantin Komarov repack:
1740be71b5cbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1741be71b5cbSKonstantin Komarov 	if (err)
1742be71b5cbSKonstantin Komarov 		goto out;
1743be71b5cbSKonstantin Komarov 
1744be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
1745be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
17462f4ddb5aSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1747be71b5cbSKonstantin Komarov 
1748be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
1749be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
1750be71b5cbSKonstantin Komarov 
1751e8b8e97fSKari Argillander 	/* Stored [vcn : next_svcn) from [vcn : end). */
1752be71b5cbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1753be71b5cbSKonstantin Komarov 
1754be71b5cbSKonstantin Komarov 	if (end <= evcn1) {
1755be71b5cbSKonstantin Komarov 		if (next_svcn == evcn1) {
1756e8b8e97fSKari Argillander 			/* Normal way. Update attribute and exit. */
1757be71b5cbSKonstantin Komarov 			goto ok;
1758be71b5cbSKonstantin Komarov 		}
1759e8b8e97fSKari Argillander 		/* Add new segment [next_svcn : evcn1 - next_svcn). */
1760be71b5cbSKonstantin Komarov 		if (!ni->attr_list.size) {
1761be71b5cbSKonstantin Komarov 			err = ni_create_attr_list(ni);
1762be71b5cbSKonstantin Komarov 			if (err)
1763be71b5cbSKonstantin Komarov 				goto out;
1764e8b8e97fSKari Argillander 			/* Layout of records is changed. */
1765be71b5cbSKonstantin Komarov 			le_b = NULL;
1766be71b5cbSKonstantin Komarov 			attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1767be71b5cbSKonstantin Komarov 					      0, NULL, &mi_b);
1768947c3f3dSKonstantin Komarov 			if (!attr_b) {
1769947c3f3dSKonstantin Komarov 				err = -ENOENT;
1770947c3f3dSKonstantin Komarov 				goto out;
1771947c3f3dSKonstantin Komarov 			}
1772be71b5cbSKonstantin Komarov 
1773be71b5cbSKonstantin Komarov 			attr = attr_b;
1774be71b5cbSKonstantin Komarov 			le = le_b;
1775be71b5cbSKonstantin Komarov 			mi = mi_b;
1776be71b5cbSKonstantin Komarov 			goto repack;
1777be71b5cbSKonstantin Komarov 		}
1778be71b5cbSKonstantin Komarov 	}
1779be71b5cbSKonstantin Komarov 
1780be71b5cbSKonstantin Komarov 	svcn = evcn1;
1781be71b5cbSKonstantin Komarov 
1782e8b8e97fSKari Argillander 	/* Estimate next attribute. */
1783be71b5cbSKonstantin Komarov 	attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1784be71b5cbSKonstantin Komarov 
1785be71b5cbSKonstantin Komarov 	if (attr) {
1786be71b5cbSKonstantin Komarov 		CLST alloc = bytes_to_cluster(
1787be71b5cbSKonstantin Komarov 			sbi, le64_to_cpu(attr_b->nres.alloc_size));
1788be71b5cbSKonstantin Komarov 		CLST evcn = le64_to_cpu(attr->nres.evcn);
1789be71b5cbSKonstantin Komarov 
1790be71b5cbSKonstantin Komarov 		if (end < next_svcn)
1791be71b5cbSKonstantin Komarov 			end = next_svcn;
1792be71b5cbSKonstantin Komarov 		while (end > evcn) {
1793e8b8e97fSKari Argillander 			/* Remove segment [svcn : evcn). */
179478ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
1795be71b5cbSKonstantin Komarov 
1796be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
1797be71b5cbSKonstantin Komarov 				err = -EINVAL;
1798be71b5cbSKonstantin Komarov 				goto out;
1799be71b5cbSKonstantin Komarov 			}
1800be71b5cbSKonstantin Komarov 
1801be71b5cbSKonstantin Komarov 			if (evcn + 1 >= alloc) {
1802e8b8e97fSKari Argillander 				/* Last attribute segment. */
1803be71b5cbSKonstantin Komarov 				evcn1 = evcn + 1;
1804be71b5cbSKonstantin Komarov 				goto ins_ext;
1805be71b5cbSKonstantin Komarov 			}
1806be71b5cbSKonstantin Komarov 
1807be71b5cbSKonstantin Komarov 			if (ni_load_mi(ni, le, &mi)) {
1808be71b5cbSKonstantin Komarov 				attr = NULL;
1809be71b5cbSKonstantin Komarov 				goto out;
1810be71b5cbSKonstantin Komarov 			}
1811be71b5cbSKonstantin Komarov 
1812be71b5cbSKonstantin Komarov 			attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1813be71b5cbSKonstantin Komarov 					    &le->id);
1814be71b5cbSKonstantin Komarov 			if (!attr) {
1815be71b5cbSKonstantin Komarov 				err = -EINVAL;
1816be71b5cbSKonstantin Komarov 				goto out;
1817be71b5cbSKonstantin Komarov 			}
1818be71b5cbSKonstantin Komarov 			svcn = le64_to_cpu(attr->nres.svcn);
1819be71b5cbSKonstantin Komarov 			evcn = le64_to_cpu(attr->nres.evcn);
1820be71b5cbSKonstantin Komarov 		}
1821be71b5cbSKonstantin Komarov 
1822be71b5cbSKonstantin Komarov 		if (end < svcn)
1823be71b5cbSKonstantin Komarov 			end = svcn;
1824be71b5cbSKonstantin Komarov 
1825be71b5cbSKonstantin Komarov 		err = attr_load_runs(attr, ni, run, &end);
1826be71b5cbSKonstantin Komarov 		if (err)
1827be71b5cbSKonstantin Komarov 			goto out;
1828be71b5cbSKonstantin Komarov 
1829be71b5cbSKonstantin Komarov 		evcn1 = evcn + 1;
1830be71b5cbSKonstantin Komarov 		attr->nres.svcn = cpu_to_le64(next_svcn);
1831be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1832be71b5cbSKonstantin Komarov 		if (err)
1833be71b5cbSKonstantin Komarov 			goto out;
1834be71b5cbSKonstantin Komarov 
1835be71b5cbSKonstantin Komarov 		le->vcn = cpu_to_le64(next_svcn);
1836be71b5cbSKonstantin Komarov 		ni->attr_list.dirty = true;
1837be71b5cbSKonstantin Komarov 		mi->dirty = true;
1838be71b5cbSKonstantin Komarov 
1839be71b5cbSKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1840be71b5cbSKonstantin Komarov 	}
1841be71b5cbSKonstantin Komarov ins_ext:
1842be71b5cbSKonstantin Komarov 	if (evcn1 > next_svcn) {
1843be71b5cbSKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1844be71b5cbSKonstantin Komarov 					    next_svcn, evcn1 - next_svcn,
1845c1e0ab37SKonstantin Komarov 					    attr_b->flags, &attr, &mi, NULL);
1846be71b5cbSKonstantin Komarov 		if (err)
1847be71b5cbSKonstantin Komarov 			goto out;
1848be71b5cbSKonstantin Komarov 	}
1849be71b5cbSKonstantin Komarov ok:
1850be71b5cbSKonstantin Komarov 	run_truncate_around(run, vcn);
1851be71b5cbSKonstantin Komarov out:
1852947c3f3dSKonstantin Komarov 	if (attr_b) {
1853be71b5cbSKonstantin Komarov 		if (new_valid > data_size)
1854be71b5cbSKonstantin Komarov 			new_valid = data_size;
1855be71b5cbSKonstantin Komarov 
1856be71b5cbSKonstantin Komarov 		valid_size = le64_to_cpu(attr_b->nres.valid_size);
1857be71b5cbSKonstantin Komarov 		if (new_valid != valid_size) {
1858be71b5cbSKonstantin Komarov 			attr_b->nres.valid_size = cpu_to_le64(valid_size);
1859be71b5cbSKonstantin Komarov 			mi_b->dirty = true;
1860be71b5cbSKonstantin Komarov 		}
1861947c3f3dSKonstantin Komarov 	}
1862be71b5cbSKonstantin Komarov 
1863be71b5cbSKonstantin Komarov 	return err;
1864be71b5cbSKonstantin Komarov }
1865be71b5cbSKonstantin Komarov 
1866e8b8e97fSKari Argillander /*
1867e8b8e97fSKari Argillander  * attr_collapse_range - Collapse range in file.
1868e8b8e97fSKari Argillander  */
attr_collapse_range(struct ntfs_inode * ni,u64 vbo,u64 bytes)1869be71b5cbSKonstantin Komarov int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1870be71b5cbSKonstantin Komarov {
1871be71b5cbSKonstantin Komarov 	int err = 0;
1872be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
1873be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1874be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
1875be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
1876be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
1877be71b5cbSKonstantin Komarov 	CLST svcn, evcn1, len, dealloc, alen;
1878be71b5cbSKonstantin Komarov 	CLST vcn, end;
1879be71b5cbSKonstantin Komarov 	u64 valid_size, data_size, alloc_size, total_size;
1880be71b5cbSKonstantin Komarov 	u32 mask;
1881be71b5cbSKonstantin Komarov 	__le16 a_flags;
1882be71b5cbSKonstantin Komarov 
1883be71b5cbSKonstantin Komarov 	if (!bytes)
1884be71b5cbSKonstantin Komarov 		return 0;
1885be71b5cbSKonstantin Komarov 
1886be71b5cbSKonstantin Komarov 	le_b = NULL;
1887be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1888be71b5cbSKonstantin Komarov 	if (!attr_b)
1889be71b5cbSKonstantin Komarov 		return -ENOENT;
1890be71b5cbSKonstantin Komarov 
1891be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
1892be71b5cbSKonstantin Komarov 		/* Attribute is resident. Nothing to do? */
1893be71b5cbSKonstantin Komarov 		return 0;
1894be71b5cbSKonstantin Komarov 	}
1895be71b5cbSKonstantin Komarov 
1896be71b5cbSKonstantin Komarov 	data_size = le64_to_cpu(attr_b->nres.data_size);
1897be71b5cbSKonstantin Komarov 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1898be71b5cbSKonstantin Komarov 	a_flags = attr_b->flags;
1899be71b5cbSKonstantin Komarov 
1900be71b5cbSKonstantin Komarov 	if (is_attr_ext(attr_b)) {
1901be71b5cbSKonstantin Komarov 		total_size = le64_to_cpu(attr_b->nres.total_size);
1902be71b5cbSKonstantin Komarov 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1903be71b5cbSKonstantin Komarov 	} else {
1904be71b5cbSKonstantin Komarov 		total_size = alloc_size;
1905be71b5cbSKonstantin Komarov 		mask = sbi->cluster_mask;
1906be71b5cbSKonstantin Komarov 	}
1907be71b5cbSKonstantin Komarov 
1908be71b5cbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
1909e8b8e97fSKari Argillander 		/* Allow to collapse only cluster aligned ranges. */
1910be71b5cbSKonstantin Komarov 		return -EINVAL;
1911be71b5cbSKonstantin Komarov 	}
1912be71b5cbSKonstantin Komarov 
1913be71b5cbSKonstantin Komarov 	if (vbo > data_size)
1914be71b5cbSKonstantin Komarov 		return -EINVAL;
1915be71b5cbSKonstantin Komarov 
1916be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
1917be71b5cbSKonstantin Komarov 
1918be71b5cbSKonstantin Komarov 	if (vbo + bytes >= data_size) {
1919be71b5cbSKonstantin Komarov 		u64 new_valid = min(ni->i_valid, vbo);
1920be71b5cbSKonstantin Komarov 
1921e8b8e97fSKari Argillander 		/* Simple truncate file at 'vbo'. */
1922be71b5cbSKonstantin Komarov 		truncate_setsize(&ni->vfs_inode, vbo);
1923be71b5cbSKonstantin Komarov 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1924be71b5cbSKonstantin Komarov 				    &new_valid, true, NULL);
1925be71b5cbSKonstantin Komarov 
1926be71b5cbSKonstantin Komarov 		if (!err && new_valid < ni->i_valid)
1927be71b5cbSKonstantin Komarov 			ni->i_valid = new_valid;
1928be71b5cbSKonstantin Komarov 
1929be71b5cbSKonstantin Komarov 		goto out;
1930be71b5cbSKonstantin Komarov 	}
1931be71b5cbSKonstantin Komarov 
1932be71b5cbSKonstantin Komarov 	/*
1933e8b8e97fSKari Argillander 	 * Enumerate all attribute segments and collapse.
1934be71b5cbSKonstantin Komarov 	 */
1935be71b5cbSKonstantin Komarov 	alen = alloc_size >> sbi->cluster_bits;
1936be71b5cbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
1937be71b5cbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
1938be71b5cbSKonstantin Komarov 	end = vcn + len;
1939be71b5cbSKonstantin Komarov 	dealloc = 0;
1940be71b5cbSKonstantin Komarov 
1941be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
1942be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1943be71b5cbSKonstantin Komarov 
1944be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
1945be71b5cbSKonstantin Komarov 		attr = attr_b;
1946be71b5cbSKonstantin Komarov 		le = le_b;
1947be71b5cbSKonstantin Komarov 		mi = mi_b;
1948be71b5cbSKonstantin Komarov 	} else if (!le_b) {
1949be71b5cbSKonstantin Komarov 		err = -EINVAL;
1950be71b5cbSKonstantin Komarov 		goto out;
1951be71b5cbSKonstantin Komarov 	} else {
1952be71b5cbSKonstantin Komarov 		le = le_b;
1953be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1954be71b5cbSKonstantin Komarov 				    &mi);
1955be71b5cbSKonstantin Komarov 		if (!attr) {
1956be71b5cbSKonstantin Komarov 			err = -EINVAL;
1957be71b5cbSKonstantin Komarov 			goto out;
1958be71b5cbSKonstantin Komarov 		}
1959be71b5cbSKonstantin Komarov 
1960be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
1961be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1962be71b5cbSKonstantin Komarov 	}
1963be71b5cbSKonstantin Komarov 
1964be71b5cbSKonstantin Komarov 	for (;;) {
1965be71b5cbSKonstantin Komarov 		if (svcn >= end) {
1966e8b8e97fSKari Argillander 			/* Shift VCN- */
1967be71b5cbSKonstantin Komarov 			attr->nres.svcn = cpu_to_le64(svcn - len);
1968be71b5cbSKonstantin Komarov 			attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1969be71b5cbSKonstantin Komarov 			if (le) {
1970be71b5cbSKonstantin Komarov 				le->vcn = attr->nres.svcn;
1971be71b5cbSKonstantin Komarov 				ni->attr_list.dirty = true;
1972be71b5cbSKonstantin Komarov 			}
1973be71b5cbSKonstantin Komarov 			mi->dirty = true;
1974be71b5cbSKonstantin Komarov 		} else if (svcn < vcn || end < evcn1) {
1975be71b5cbSKonstantin Komarov 			CLST vcn1, eat, next_svcn;
1976be71b5cbSKonstantin Komarov 
1977e8b8e97fSKari Argillander 			/* Collapse a part of this attribute segment. */
1978be71b5cbSKonstantin Komarov 			err = attr_load_runs(attr, ni, run, &svcn);
1979be71b5cbSKonstantin Komarov 			if (err)
1980be71b5cbSKonstantin Komarov 				goto out;
1981be71b5cbSKonstantin Komarov 			vcn1 = max(vcn, svcn);
1982be71b5cbSKonstantin Komarov 			eat = min(end, evcn1) - vcn1;
1983be71b5cbSKonstantin Komarov 
1984be71b5cbSKonstantin Komarov 			err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1985be71b5cbSKonstantin Komarov 						true);
1986be71b5cbSKonstantin Komarov 			if (err)
1987be71b5cbSKonstantin Komarov 				goto out;
1988be71b5cbSKonstantin Komarov 
1989be71b5cbSKonstantin Komarov 			if (!run_collapse_range(run, vcn1, eat)) {
1990be71b5cbSKonstantin Komarov 				err = -ENOMEM;
1991be71b5cbSKonstantin Komarov 				goto out;
1992be71b5cbSKonstantin Komarov 			}
1993be71b5cbSKonstantin Komarov 
1994be71b5cbSKonstantin Komarov 			if (svcn >= vcn) {
1995e8b8e97fSKari Argillander 				/* Shift VCN */
1996be71b5cbSKonstantin Komarov 				attr->nres.svcn = cpu_to_le64(vcn);
1997be71b5cbSKonstantin Komarov 				if (le) {
1998be71b5cbSKonstantin Komarov 					le->vcn = attr->nres.svcn;
1999be71b5cbSKonstantin Komarov 					ni->attr_list.dirty = true;
2000be71b5cbSKonstantin Komarov 				}
2001be71b5cbSKonstantin Komarov 			}
2002be71b5cbSKonstantin Komarov 
2003be71b5cbSKonstantin Komarov 			err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
2004be71b5cbSKonstantin Komarov 			if (err)
2005be71b5cbSKonstantin Komarov 				goto out;
2006be71b5cbSKonstantin Komarov 
2007be71b5cbSKonstantin Komarov 			next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2008be71b5cbSKonstantin Komarov 			if (next_svcn + eat < evcn1) {
2009be71b5cbSKonstantin Komarov 				err = ni_insert_nonresident(
2010be71b5cbSKonstantin Komarov 					ni, ATTR_DATA, NULL, 0, run, next_svcn,
2011be71b5cbSKonstantin Komarov 					evcn1 - eat - next_svcn, a_flags, &attr,
2012c1e0ab37SKonstantin Komarov 					&mi, &le);
2013be71b5cbSKonstantin Komarov 				if (err)
2014be71b5cbSKonstantin Komarov 					goto out;
2015be71b5cbSKonstantin Komarov 
2016e8b8e97fSKari Argillander 				/* Layout of records maybe changed. */
2017be71b5cbSKonstantin Komarov 				attr_b = NULL;
2018be71b5cbSKonstantin Komarov 			}
2019be71b5cbSKonstantin Komarov 
2020e8b8e97fSKari Argillander 			/* Free all allocated memory. */
2021be71b5cbSKonstantin Komarov 			run_truncate(run, 0);
2022be71b5cbSKonstantin Komarov 		} else {
2023be71b5cbSKonstantin Komarov 			u16 le_sz;
2024be71b5cbSKonstantin Komarov 			u16 roff = le16_to_cpu(attr->nres.run_off);
2025be71b5cbSKonstantin Komarov 
20266db62086SEdward Lo 			if (roff > le32_to_cpu(attr->size)) {
20276db62086SEdward Lo 				err = -EINVAL;
20286db62086SEdward Lo 				goto out;
20296db62086SEdward Lo 			}
20306db62086SEdward Lo 
2031be71b5cbSKonstantin Komarov 			run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
2032be71b5cbSKonstantin Komarov 				      evcn1 - 1, svcn, Add2Ptr(attr, roff),
2033be71b5cbSKonstantin Komarov 				      le32_to_cpu(attr->size) - roff);
2034be71b5cbSKonstantin Komarov 
2035e8b8e97fSKari Argillander 			/* Delete this attribute segment. */
203678ab59feSKonstantin Komarov 			mi_remove_attr(NULL, mi, attr);
2037be71b5cbSKonstantin Komarov 			if (!le)
2038be71b5cbSKonstantin Komarov 				break;
2039be71b5cbSKonstantin Komarov 
2040be71b5cbSKonstantin Komarov 			le_sz = le16_to_cpu(le->size);
2041be71b5cbSKonstantin Komarov 			if (!al_remove_le(ni, le)) {
2042be71b5cbSKonstantin Komarov 				err = -EINVAL;
2043be71b5cbSKonstantin Komarov 				goto out;
2044be71b5cbSKonstantin Komarov 			}
2045be71b5cbSKonstantin Komarov 
2046be71b5cbSKonstantin Komarov 			if (evcn1 >= alen)
2047be71b5cbSKonstantin Komarov 				break;
2048be71b5cbSKonstantin Komarov 
2049be71b5cbSKonstantin Komarov 			if (!svcn) {
2050e8b8e97fSKari Argillander 				/* Load next record that contains this attribute. */
2051be71b5cbSKonstantin Komarov 				if (ni_load_mi(ni, le, &mi)) {
2052be71b5cbSKonstantin Komarov 					err = -EINVAL;
2053be71b5cbSKonstantin Komarov 					goto out;
2054be71b5cbSKonstantin Komarov 				}
2055be71b5cbSKonstantin Komarov 
2056e8b8e97fSKari Argillander 				/* Look for required attribute. */
2057be71b5cbSKonstantin Komarov 				attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
2058be71b5cbSKonstantin Komarov 						    0, &le->id);
2059be71b5cbSKonstantin Komarov 				if (!attr) {
2060be71b5cbSKonstantin Komarov 					err = -EINVAL;
2061be71b5cbSKonstantin Komarov 					goto out;
2062be71b5cbSKonstantin Komarov 				}
2063be71b5cbSKonstantin Komarov 				goto next_attr;
2064be71b5cbSKonstantin Komarov 			}
2065be71b5cbSKonstantin Komarov 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2066be71b5cbSKonstantin Komarov 		}
2067be71b5cbSKonstantin Komarov 
2068be71b5cbSKonstantin Komarov 		if (evcn1 >= alen)
2069be71b5cbSKonstantin Komarov 			break;
2070be71b5cbSKonstantin Komarov 
2071be71b5cbSKonstantin Komarov 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2072be71b5cbSKonstantin Komarov 		if (!attr) {
2073be71b5cbSKonstantin Komarov 			err = -EINVAL;
2074be71b5cbSKonstantin Komarov 			goto out;
2075be71b5cbSKonstantin Komarov 		}
2076be71b5cbSKonstantin Komarov 
2077be71b5cbSKonstantin Komarov next_attr:
2078be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2079be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2080be71b5cbSKonstantin Komarov 	}
2081be71b5cbSKonstantin Komarov 
2082be71b5cbSKonstantin Komarov 	if (!attr_b) {
2083be71b5cbSKonstantin Komarov 		le_b = NULL;
2084be71b5cbSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2085be71b5cbSKonstantin Komarov 				      &mi_b);
2086be71b5cbSKonstantin Komarov 		if (!attr_b) {
2087be71b5cbSKonstantin Komarov 			err = -ENOENT;
2088be71b5cbSKonstantin Komarov 			goto out;
2089be71b5cbSKonstantin Komarov 		}
2090be71b5cbSKonstantin Komarov 	}
2091be71b5cbSKonstantin Komarov 
2092be71b5cbSKonstantin Komarov 	data_size -= bytes;
2093be71b5cbSKonstantin Komarov 	valid_size = ni->i_valid;
2094be71b5cbSKonstantin Komarov 	if (vbo + bytes <= valid_size)
2095be71b5cbSKonstantin Komarov 		valid_size -= bytes;
2096be71b5cbSKonstantin Komarov 	else if (vbo < valid_size)
2097be71b5cbSKonstantin Komarov 		valid_size = vbo;
2098be71b5cbSKonstantin Komarov 
2099be71b5cbSKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2100be71b5cbSKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(data_size);
2101be71b5cbSKonstantin Komarov 	attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2102be71b5cbSKonstantin Komarov 	total_size -= (u64)dealloc << sbi->cluster_bits;
2103be71b5cbSKonstantin Komarov 	if (is_attr_ext(attr_b))
2104be71b5cbSKonstantin Komarov 		attr_b->nres.total_size = cpu_to_le64(total_size);
2105be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
2106be71b5cbSKonstantin Komarov 
2107e8b8e97fSKari Argillander 	/* Update inode size. */
2108be71b5cbSKonstantin Komarov 	ni->i_valid = valid_size;
2109f4cf29c6SKonstantin Komarov 	i_size_write(&ni->vfs_inode, data_size);
2110be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
2111be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2112be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2113be71b5cbSKonstantin Komarov 
2114be71b5cbSKonstantin Komarov out:
2115be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2116be71b5cbSKonstantin Komarov 	if (err)
2117c12df45eSKonstantin Komarov 		_ntfs_bad_inode(&ni->vfs_inode);
2118be71b5cbSKonstantin Komarov 
2119be71b5cbSKonstantin Komarov 	return err;
2120be71b5cbSKonstantin Komarov }
2121be71b5cbSKonstantin Komarov 
2122e8b8e97fSKari Argillander /*
2123e8b8e97fSKari Argillander  * attr_punch_hole
2124e8b8e97fSKari Argillander  *
2125e8b8e97fSKari Argillander  * Not for normal files.
2126e8b8e97fSKari Argillander  */
attr_punch_hole(struct ntfs_inode * ni,u64 vbo,u64 bytes,u32 * frame_size)2127be71b5cbSKonstantin Komarov int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2128be71b5cbSKonstantin Komarov {
2129be71b5cbSKonstantin Komarov 	int err = 0;
2130be71b5cbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
2131be71b5cbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2132be71b5cbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
2133be71b5cbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
2134be71b5cbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
213520abc64fSKonstantin Komarov 	CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2136be71b5cbSKonstantin Komarov 	u64 total_size, alloc_size;
2137be71b5cbSKonstantin Komarov 	u32 mask;
2138c1e0ab37SKonstantin Komarov 	__le16 a_flags;
213920abc64fSKonstantin Komarov 	struct runs_tree run2;
2140be71b5cbSKonstantin Komarov 
2141be71b5cbSKonstantin Komarov 	if (!bytes)
2142be71b5cbSKonstantin Komarov 		return 0;
2143be71b5cbSKonstantin Komarov 
2144be71b5cbSKonstantin Komarov 	le_b = NULL;
2145be71b5cbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2146be71b5cbSKonstantin Komarov 	if (!attr_b)
2147be71b5cbSKonstantin Komarov 		return -ENOENT;
2148be71b5cbSKonstantin Komarov 
2149be71b5cbSKonstantin Komarov 	if (!attr_b->non_res) {
21506d5c9e79SAlon Zahavi 		u32 data_size = le32_to_cpu(attr_b->res.data_size);
2151be71b5cbSKonstantin Komarov 		u32 from, to;
2152be71b5cbSKonstantin Komarov 
2153be71b5cbSKonstantin Komarov 		if (vbo > data_size)
2154be71b5cbSKonstantin Komarov 			return 0;
2155be71b5cbSKonstantin Komarov 
2156be71b5cbSKonstantin Komarov 		from = vbo;
21576e3331eeSKari Argillander 		to = min_t(u64, vbo + bytes, data_size);
2158be71b5cbSKonstantin Komarov 		memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2159be71b5cbSKonstantin Komarov 		return 0;
2160be71b5cbSKonstantin Komarov 	}
2161be71b5cbSKonstantin Komarov 
2162be71b5cbSKonstantin Komarov 	if (!is_attr_ext(attr_b))
2163be71b5cbSKonstantin Komarov 		return -EOPNOTSUPP;
2164be71b5cbSKonstantin Komarov 
2165be71b5cbSKonstantin Komarov 	alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2166be71b5cbSKonstantin Komarov 	total_size = le64_to_cpu(attr_b->nres.total_size);
2167be71b5cbSKonstantin Komarov 
2168be71b5cbSKonstantin Komarov 	if (vbo >= alloc_size) {
2169d3624466SKonstantin Komarov 		/* NOTE: It is allowed. */
2170be71b5cbSKonstantin Komarov 		return 0;
2171be71b5cbSKonstantin Komarov 	}
2172be71b5cbSKonstantin Komarov 
2173be71b5cbSKonstantin Komarov 	mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2174be71b5cbSKonstantin Komarov 
2175be71b5cbSKonstantin Komarov 	bytes += vbo;
2176be71b5cbSKonstantin Komarov 	if (bytes > alloc_size)
2177be71b5cbSKonstantin Komarov 		bytes = alloc_size;
2178be71b5cbSKonstantin Komarov 	bytes -= vbo;
2179be71b5cbSKonstantin Komarov 
2180be71b5cbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
2181d3624466SKonstantin Komarov 		/* We have to zero a range(s). */
2182be71b5cbSKonstantin Komarov 		if (frame_size == NULL) {
2183d3624466SKonstantin Komarov 			/* Caller insists range is aligned. */
2184be71b5cbSKonstantin Komarov 			return -EINVAL;
2185be71b5cbSKonstantin Komarov 		}
2186be71b5cbSKonstantin Komarov 		*frame_size = mask + 1;
2187be71b5cbSKonstantin Komarov 		return E_NTFS_NOTALIGNED;
2188be71b5cbSKonstantin Komarov 	}
2189be71b5cbSKonstantin Komarov 
2190be71b5cbSKonstantin Komarov 	down_write(&ni->file.run_lock);
219120abc64fSKonstantin Komarov 	run_init(&run2);
219220abc64fSKonstantin Komarov 	run_truncate(run, 0);
219320abc64fSKonstantin Komarov 
2194be71b5cbSKonstantin Komarov 	/*
2195e8b8e97fSKari Argillander 	 * Enumerate all attribute segments and punch hole where necessary.
2196be71b5cbSKonstantin Komarov 	 */
2197be71b5cbSKonstantin Komarov 	alen = alloc_size >> sbi->cluster_bits;
2198be71b5cbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
2199be71b5cbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
2200be71b5cbSKonstantin Komarov 	end = vcn + len;
220120abc64fSKonstantin Komarov 	hole = 0;
2202be71b5cbSKonstantin Komarov 
2203be71b5cbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
2204be71b5cbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2205c1e0ab37SKonstantin Komarov 	a_flags = attr_b->flags;
2206be71b5cbSKonstantin Komarov 
2207be71b5cbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
2208be71b5cbSKonstantin Komarov 		attr = attr_b;
2209be71b5cbSKonstantin Komarov 		le = le_b;
2210be71b5cbSKonstantin Komarov 		mi = mi_b;
2211be71b5cbSKonstantin Komarov 	} else if (!le_b) {
2212be71b5cbSKonstantin Komarov 		err = -EINVAL;
221320abc64fSKonstantin Komarov 		goto bad_inode;
2214be71b5cbSKonstantin Komarov 	} else {
2215be71b5cbSKonstantin Komarov 		le = le_b;
2216be71b5cbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2217be71b5cbSKonstantin Komarov 				    &mi);
2218be71b5cbSKonstantin Komarov 		if (!attr) {
2219be71b5cbSKonstantin Komarov 			err = -EINVAL;
222020abc64fSKonstantin Komarov 			goto bad_inode;
2221be71b5cbSKonstantin Komarov 		}
2222be71b5cbSKonstantin Komarov 
2223be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2224be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2225be71b5cbSKonstantin Komarov 	}
2226be71b5cbSKonstantin Komarov 
2227be71b5cbSKonstantin Komarov 	while (svcn < end) {
222820abc64fSKonstantin Komarov 		CLST vcn1, zero, hole2 = hole;
2229be71b5cbSKonstantin Komarov 
2230be71b5cbSKonstantin Komarov 		err = attr_load_runs(attr, ni, run, &svcn);
2231be71b5cbSKonstantin Komarov 		if (err)
223220abc64fSKonstantin Komarov 			goto done;
2233be71b5cbSKonstantin Komarov 		vcn1 = max(vcn, svcn);
2234be71b5cbSKonstantin Komarov 		zero = min(end, evcn1) - vcn1;
2235be71b5cbSKonstantin Komarov 
223620abc64fSKonstantin Komarov 		/*
223720abc64fSKonstantin Komarov 		 * Check range [vcn1 + zero).
223820abc64fSKonstantin Komarov 		 * Calculate how many clusters there are.
223920abc64fSKonstantin Komarov 		 * Don't do any destructive actions.
224020abc64fSKonstantin Komarov 		 */
224120abc64fSKonstantin Komarov 		err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
2242be71b5cbSKonstantin Komarov 		if (err)
224320abc64fSKonstantin Komarov 			goto done;
2244be71b5cbSKonstantin Komarov 
224520abc64fSKonstantin Komarov 		/* Check if required range is already hole. */
224620abc64fSKonstantin Komarov 		if (hole2 == hole)
224720abc64fSKonstantin Komarov 			goto next_attr;
224820abc64fSKonstantin Komarov 
224920abc64fSKonstantin Komarov 		/* Make a clone of run to undo. */
225020abc64fSKonstantin Komarov 		err = run_clone(run, &run2);
225120abc64fSKonstantin Komarov 		if (err)
225220abc64fSKonstantin Komarov 			goto done;
225320abc64fSKonstantin Komarov 
225420abc64fSKonstantin Komarov 		/* Make a hole range (sparse) [vcn1 + zero). */
225520abc64fSKonstantin Komarov 		if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2256be71b5cbSKonstantin Komarov 			err = -ENOMEM;
225720abc64fSKonstantin Komarov 			goto done;
2258be71b5cbSKonstantin Komarov 		}
2259be71b5cbSKonstantin Komarov 
226020abc64fSKonstantin Komarov 		/* Update run in attribute segment. */
2261be71b5cbSKonstantin Komarov 		err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2262be71b5cbSKonstantin Komarov 		if (err)
226320abc64fSKonstantin Komarov 			goto done;
2264c1e0ab37SKonstantin Komarov 		next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2265c1e0ab37SKonstantin Komarov 		if (next_svcn < evcn1) {
226620abc64fSKonstantin Komarov 			/* Insert new attribute segment. */
226720abc64fSKonstantin Komarov 			err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
226820abc64fSKonstantin Komarov 						    next_svcn,
226920abc64fSKonstantin Komarov 						    evcn1 - next_svcn, a_flags,
227020abc64fSKonstantin Komarov 						    &attr, &mi, &le);
2271c1e0ab37SKonstantin Komarov 			if (err)
227220abc64fSKonstantin Komarov 				goto undo_punch;
227320abc64fSKonstantin Komarov 
2274c1e0ab37SKonstantin Komarov 			/* Layout of records maybe changed. */
2275560f7736SKonstantin Komarov 			attr_b = NULL;
2276c1e0ab37SKonstantin Komarov 		}
227720abc64fSKonstantin Komarov 
227820abc64fSKonstantin Komarov 		/* Real deallocate. Should not fail. */
227920abc64fSKonstantin Komarov 		run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
228020abc64fSKonstantin Komarov 
228120abc64fSKonstantin Komarov next_attr:
2282e8b8e97fSKari Argillander 		/* Free all allocated memory. */
2283be71b5cbSKonstantin Komarov 		run_truncate(run, 0);
2284be71b5cbSKonstantin Komarov 
2285be71b5cbSKonstantin Komarov 		if (evcn1 >= alen)
2286be71b5cbSKonstantin Komarov 			break;
2287be71b5cbSKonstantin Komarov 
228820abc64fSKonstantin Komarov 		/* Get next attribute segment. */
2289be71b5cbSKonstantin Komarov 		attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2290be71b5cbSKonstantin Komarov 		if (!attr) {
2291be71b5cbSKonstantin Komarov 			err = -EINVAL;
229220abc64fSKonstantin Komarov 			goto bad_inode;
2293be71b5cbSKonstantin Komarov 		}
2294be71b5cbSKonstantin Komarov 
2295be71b5cbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2296be71b5cbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2297be71b5cbSKonstantin Komarov 	}
2298be71b5cbSKonstantin Komarov 
229920abc64fSKonstantin Komarov done:
230020abc64fSKonstantin Komarov 	if (!hole)
230120abc64fSKonstantin Komarov 		goto out;
230220abc64fSKonstantin Komarov 
2303560f7736SKonstantin Komarov 	if (!attr_b) {
2304560f7736SKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2305560f7736SKonstantin Komarov 				      &mi_b);
2306560f7736SKonstantin Komarov 		if (!attr_b) {
2307560f7736SKonstantin Komarov 			err = -EINVAL;
230820abc64fSKonstantin Komarov 			goto bad_inode;
2309560f7736SKonstantin Komarov 		}
2310560f7736SKonstantin Komarov 	}
231120abc64fSKonstantin Komarov 
231220abc64fSKonstantin Komarov 	total_size -= (u64)hole << sbi->cluster_bits;
2313be71b5cbSKonstantin Komarov 	attr_b->nres.total_size = cpu_to_le64(total_size);
2314be71b5cbSKonstantin Komarov 	mi_b->dirty = true;
2315be71b5cbSKonstantin Komarov 
2316e8b8e97fSKari Argillander 	/* Update inode size. */
2317be71b5cbSKonstantin Komarov 	inode_set_bytes(&ni->vfs_inode, total_size);
2318be71b5cbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2319be71b5cbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2320be71b5cbSKonstantin Komarov 
2321be71b5cbSKonstantin Komarov out:
232220abc64fSKonstantin Komarov 	run_close(&run2);
2323be71b5cbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2324be71b5cbSKonstantin Komarov 	return err;
232520abc64fSKonstantin Komarov 
232620abc64fSKonstantin Komarov bad_inode:
232720abc64fSKonstantin Komarov 	_ntfs_bad_inode(&ni->vfs_inode);
232820abc64fSKonstantin Komarov 	goto out;
232920abc64fSKonstantin Komarov 
233020abc64fSKonstantin Komarov undo_punch:
233120abc64fSKonstantin Komarov 	/*
233220abc64fSKonstantin Komarov 	 * Restore packed runs.
233320abc64fSKonstantin Komarov 	 * 'mi_pack_runs' should not fail, cause we restore original.
233420abc64fSKonstantin Komarov 	 */
233520abc64fSKonstantin Komarov 	if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
233620abc64fSKonstantin Komarov 		goto bad_inode;
233720abc64fSKonstantin Komarov 
233820abc64fSKonstantin Komarov 	goto done;
2339be71b5cbSKonstantin Komarov }
2340aa30eccbSKonstantin Komarov 
2341aa30eccbSKonstantin Komarov /*
2342aa30eccbSKonstantin Komarov  * attr_insert_range - Insert range (hole) in file.
2343aa30eccbSKonstantin Komarov  * Not for normal files.
2344aa30eccbSKonstantin Komarov  */
attr_insert_range(struct ntfs_inode * ni,u64 vbo,u64 bytes)2345aa30eccbSKonstantin Komarov int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2346aa30eccbSKonstantin Komarov {
2347aa30eccbSKonstantin Komarov 	int err = 0;
2348aa30eccbSKonstantin Komarov 	struct runs_tree *run = &ni->file.run;
2349aa30eccbSKonstantin Komarov 	struct ntfs_sb_info *sbi = ni->mi.sbi;
2350aa30eccbSKonstantin Komarov 	struct ATTRIB *attr = NULL, *attr_b;
2351aa30eccbSKonstantin Komarov 	struct ATTR_LIST_ENTRY *le, *le_b;
2352aa30eccbSKonstantin Komarov 	struct mft_inode *mi, *mi_b;
2353aa30eccbSKonstantin Komarov 	CLST vcn, svcn, evcn1, len, next_svcn;
2354aa30eccbSKonstantin Komarov 	u64 data_size, alloc_size;
2355aa30eccbSKonstantin Komarov 	u32 mask;
2356aa30eccbSKonstantin Komarov 	__le16 a_flags;
2357aa30eccbSKonstantin Komarov 
2358aa30eccbSKonstantin Komarov 	if (!bytes)
2359aa30eccbSKonstantin Komarov 		return 0;
2360aa30eccbSKonstantin Komarov 
2361aa30eccbSKonstantin Komarov 	le_b = NULL;
2362aa30eccbSKonstantin Komarov 	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2363aa30eccbSKonstantin Komarov 	if (!attr_b)
2364aa30eccbSKonstantin Komarov 		return -ENOENT;
2365aa30eccbSKonstantin Komarov 
2366aa30eccbSKonstantin Komarov 	if (!is_attr_ext(attr_b)) {
2367aa30eccbSKonstantin Komarov 		/* It was checked above. See fallocate. */
2368aa30eccbSKonstantin Komarov 		return -EOPNOTSUPP;
2369aa30eccbSKonstantin Komarov 	}
2370aa30eccbSKonstantin Komarov 
2371aa30eccbSKonstantin Komarov 	if (!attr_b->non_res) {
2372aa30eccbSKonstantin Komarov 		data_size = le32_to_cpu(attr_b->res.data_size);
237313747aacSKonstantin Komarov 		alloc_size = data_size;
2374aa30eccbSKonstantin Komarov 		mask = sbi->cluster_mask; /* cluster_size - 1 */
2375aa30eccbSKonstantin Komarov 	} else {
2376aa30eccbSKonstantin Komarov 		data_size = le64_to_cpu(attr_b->nres.data_size);
237713747aacSKonstantin Komarov 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2378aa30eccbSKonstantin Komarov 		mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2379aa30eccbSKonstantin Komarov 	}
2380aa30eccbSKonstantin Komarov 
2381aa30eccbSKonstantin Komarov 	if (vbo > data_size) {
2382aa30eccbSKonstantin Komarov 		/* Insert range after the file size is not allowed. */
2383aa30eccbSKonstantin Komarov 		return -EINVAL;
2384aa30eccbSKonstantin Komarov 	}
2385aa30eccbSKonstantin Komarov 
2386aa30eccbSKonstantin Komarov 	if ((vbo & mask) || (bytes & mask)) {
2387aa30eccbSKonstantin Komarov 		/* Allow to insert only frame aligned ranges. */
2388aa30eccbSKonstantin Komarov 		return -EINVAL;
2389aa30eccbSKonstantin Komarov 	}
2390aa30eccbSKonstantin Komarov 
239113747aacSKonstantin Komarov 	/*
239213747aacSKonstantin Komarov 	 * valid_size <= data_size <= alloc_size
239313747aacSKonstantin Komarov 	 * Check alloc_size for maximum possible.
239413747aacSKonstantin Komarov 	 */
239513747aacSKonstantin Komarov 	if (bytes > sbi->maxbytes_sparse - alloc_size)
239613747aacSKonstantin Komarov 		return -EFBIG;
239713747aacSKonstantin Komarov 
2398aa30eccbSKonstantin Komarov 	vcn = vbo >> sbi->cluster_bits;
2399aa30eccbSKonstantin Komarov 	len = bytes >> sbi->cluster_bits;
2400aa30eccbSKonstantin Komarov 
2401aa30eccbSKonstantin Komarov 	down_write(&ni->file.run_lock);
2402aa30eccbSKonstantin Komarov 
2403aa30eccbSKonstantin Komarov 	if (!attr_b->non_res) {
2404aa30eccbSKonstantin Komarov 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
24059256ec35SKonstantin Komarov 				    data_size + bytes, NULL, false, NULL);
2406aa30eccbSKonstantin Komarov 
2407aa30eccbSKonstantin Komarov 		le_b = NULL;
2408aa30eccbSKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2409aa30eccbSKonstantin Komarov 				      &mi_b);
24104838ec0dSDan Carpenter 		if (!attr_b) {
2411aa30eccbSKonstantin Komarov 			err = -EINVAL;
24129256ec35SKonstantin Komarov 			goto bad_inode;
2413aa30eccbSKonstantin Komarov 		}
24149256ec35SKonstantin Komarov 
24159256ec35SKonstantin Komarov 		if (err)
24169256ec35SKonstantin Komarov 			goto out;
24179256ec35SKonstantin Komarov 
24189256ec35SKonstantin Komarov 		if (!attr_b->non_res) {
24199256ec35SKonstantin Komarov 			/* Still resident. */
24209144b438SKonstantin Komarov 			char *data = Add2Ptr(attr_b,
24219144b438SKonstantin Komarov 					     le16_to_cpu(attr_b->res.data_off));
24229256ec35SKonstantin Komarov 
24239256ec35SKonstantin Komarov 			memmove(data + bytes, data, bytes);
24249256ec35SKonstantin Komarov 			memset(data, 0, bytes);
24259256ec35SKonstantin Komarov 			goto done;
24269256ec35SKonstantin Komarov 		}
24279256ec35SKonstantin Komarov 
24289256ec35SKonstantin Komarov 		/* Resident files becomes nonresident. */
2429aa30eccbSKonstantin Komarov 		data_size = le64_to_cpu(attr_b->nres.data_size);
2430aa30eccbSKonstantin Komarov 		alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2431aa30eccbSKonstantin Komarov 	}
2432aa30eccbSKonstantin Komarov 
2433aa30eccbSKonstantin Komarov 	/*
2434aa30eccbSKonstantin Komarov 	 * Enumerate all attribute segments and shift start vcn.
2435aa30eccbSKonstantin Komarov 	 */
2436aa30eccbSKonstantin Komarov 	a_flags = attr_b->flags;
2437aa30eccbSKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
2438aa30eccbSKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2439aa30eccbSKonstantin Komarov 
2440aa30eccbSKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
2441aa30eccbSKonstantin Komarov 		attr = attr_b;
2442aa30eccbSKonstantin Komarov 		le = le_b;
2443aa30eccbSKonstantin Komarov 		mi = mi_b;
2444aa30eccbSKonstantin Komarov 	} else if (!le_b) {
2445aa30eccbSKonstantin Komarov 		err = -EINVAL;
24469256ec35SKonstantin Komarov 		goto bad_inode;
2447aa30eccbSKonstantin Komarov 	} else {
2448aa30eccbSKonstantin Komarov 		le = le_b;
2449aa30eccbSKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2450aa30eccbSKonstantin Komarov 				    &mi);
2451aa30eccbSKonstantin Komarov 		if (!attr) {
2452aa30eccbSKonstantin Komarov 			err = -EINVAL;
24539256ec35SKonstantin Komarov 			goto bad_inode;
2454aa30eccbSKonstantin Komarov 		}
2455aa30eccbSKonstantin Komarov 
2456aa30eccbSKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
2457aa30eccbSKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2458aa30eccbSKonstantin Komarov 	}
2459aa30eccbSKonstantin Komarov 
2460aa30eccbSKonstantin Komarov 	run_truncate(run, 0); /* clear cached values. */
2461aa30eccbSKonstantin Komarov 	err = attr_load_runs(attr, ni, run, NULL);
2462aa30eccbSKonstantin Komarov 	if (err)
2463aa30eccbSKonstantin Komarov 		goto out;
2464aa30eccbSKonstantin Komarov 
2465aa30eccbSKonstantin Komarov 	if (!run_insert_range(run, vcn, len)) {
2466aa30eccbSKonstantin Komarov 		err = -ENOMEM;
2467aa30eccbSKonstantin Komarov 		goto out;
2468aa30eccbSKonstantin Komarov 	}
2469aa30eccbSKonstantin Komarov 
2470aa30eccbSKonstantin Komarov 	/* Try to pack in current record as much as possible. */
2471aa30eccbSKonstantin Komarov 	err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2472aa30eccbSKonstantin Komarov 	if (err)
2473aa30eccbSKonstantin Komarov 		goto out;
2474aa30eccbSKonstantin Komarov 
2475aa30eccbSKonstantin Komarov 	next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2476aa30eccbSKonstantin Komarov 
2477aa30eccbSKonstantin Komarov 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2478aa30eccbSKonstantin Komarov 	       attr->type == ATTR_DATA && !attr->name_len) {
2479aa30eccbSKonstantin Komarov 		le64_add_cpu(&attr->nres.svcn, len);
2480aa30eccbSKonstantin Komarov 		le64_add_cpu(&attr->nres.evcn, len);
2481aa30eccbSKonstantin Komarov 		if (le) {
2482aa30eccbSKonstantin Komarov 			le->vcn = attr->nres.svcn;
2483aa30eccbSKonstantin Komarov 			ni->attr_list.dirty = true;
2484aa30eccbSKonstantin Komarov 		}
2485aa30eccbSKonstantin Komarov 		mi->dirty = true;
2486aa30eccbSKonstantin Komarov 	}
2487aa30eccbSKonstantin Komarov 
24889256ec35SKonstantin Komarov 	if (next_svcn < evcn1 + len) {
24899256ec35SKonstantin Komarov 		err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
24909256ec35SKonstantin Komarov 					    next_svcn, evcn1 + len - next_svcn,
24919256ec35SKonstantin Komarov 					    a_flags, NULL, NULL, NULL);
24929256ec35SKonstantin Komarov 
24939256ec35SKonstantin Komarov 		le_b = NULL;
24949256ec35SKonstantin Komarov 		attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
24959256ec35SKonstantin Komarov 				      &mi_b);
24969256ec35SKonstantin Komarov 		if (!attr_b) {
24979256ec35SKonstantin Komarov 			err = -EINVAL;
24989256ec35SKonstantin Komarov 			goto bad_inode;
24999256ec35SKonstantin Komarov 		}
25009256ec35SKonstantin Komarov 
25019256ec35SKonstantin Komarov 		if (err) {
25029256ec35SKonstantin Komarov 			/* ni_insert_nonresident failed. Try to undo. */
25039256ec35SKonstantin Komarov 			goto undo_insert_range;
25049256ec35SKonstantin Komarov 		}
25059256ec35SKonstantin Komarov 	}
25069256ec35SKonstantin Komarov 
2507aa30eccbSKonstantin Komarov 	/*
25089256ec35SKonstantin Komarov 	 * Update primary attribute segment.
2509aa30eccbSKonstantin Komarov 	 */
2510aa30eccbSKonstantin Komarov 	if (vbo <= ni->i_valid)
2511aa30eccbSKonstantin Komarov 		ni->i_valid += bytes;
2512aa30eccbSKonstantin Komarov 
25139144b438SKonstantin Komarov 	attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
25149144b438SKonstantin Komarov 	attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
2515aa30eccbSKonstantin Komarov 
2516aa30eccbSKonstantin Komarov 	/* ni->valid may be not equal valid_size (temporary). */
2517aa30eccbSKonstantin Komarov 	if (ni->i_valid > data_size + bytes)
2518aa30eccbSKonstantin Komarov 		attr_b->nres.valid_size = attr_b->nres.data_size;
2519aa30eccbSKonstantin Komarov 	else
2520aa30eccbSKonstantin Komarov 		attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2521aa30eccbSKonstantin Komarov 	mi_b->dirty = true;
2522aa30eccbSKonstantin Komarov 
25239256ec35SKonstantin Komarov done:
2524f4cf29c6SKonstantin Komarov 	i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes);
2525aa30eccbSKonstantin Komarov 	ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2526aa30eccbSKonstantin Komarov 	mark_inode_dirty(&ni->vfs_inode);
2527aa30eccbSKonstantin Komarov 
2528aa30eccbSKonstantin Komarov out:
2529aa30eccbSKonstantin Komarov 	run_truncate(run, 0); /* clear cached values. */
2530aa30eccbSKonstantin Komarov 
2531aa30eccbSKonstantin Komarov 	up_write(&ni->file.run_lock);
2532aa30eccbSKonstantin Komarov 
2533aa30eccbSKonstantin Komarov 	return err;
25349256ec35SKonstantin Komarov 
25359256ec35SKonstantin Komarov bad_inode:
25369256ec35SKonstantin Komarov 	_ntfs_bad_inode(&ni->vfs_inode);
25379256ec35SKonstantin Komarov 	goto out;
25389256ec35SKonstantin Komarov 
25399256ec35SKonstantin Komarov undo_insert_range:
25409256ec35SKonstantin Komarov 	svcn = le64_to_cpu(attr_b->nres.svcn);
25419256ec35SKonstantin Komarov 	evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
25429256ec35SKonstantin Komarov 
25439256ec35SKonstantin Komarov 	if (svcn <= vcn && vcn < evcn1) {
25449256ec35SKonstantin Komarov 		attr = attr_b;
25459256ec35SKonstantin Komarov 		le = le_b;
25469256ec35SKonstantin Komarov 		mi = mi_b;
25479256ec35SKonstantin Komarov 	} else if (!le_b) {
25489256ec35SKonstantin Komarov 		goto bad_inode;
25499256ec35SKonstantin Komarov 	} else {
25509256ec35SKonstantin Komarov 		le = le_b;
25519256ec35SKonstantin Komarov 		attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
25529256ec35SKonstantin Komarov 				    &mi);
25539256ec35SKonstantin Komarov 		if (!attr) {
25549256ec35SKonstantin Komarov 			goto bad_inode;
25559256ec35SKonstantin Komarov 		}
25569256ec35SKonstantin Komarov 
25579256ec35SKonstantin Komarov 		svcn = le64_to_cpu(attr->nres.svcn);
25589256ec35SKonstantin Komarov 		evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
25599256ec35SKonstantin Komarov 	}
25609256ec35SKonstantin Komarov 
25619256ec35SKonstantin Komarov 	if (attr_load_runs(attr, ni, run, NULL))
25629256ec35SKonstantin Komarov 		goto bad_inode;
25639256ec35SKonstantin Komarov 
25649256ec35SKonstantin Komarov 	if (!run_collapse_range(run, vcn, len))
25659256ec35SKonstantin Komarov 		goto bad_inode;
25669256ec35SKonstantin Komarov 
25679256ec35SKonstantin Komarov 	if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
25689256ec35SKonstantin Komarov 		goto bad_inode;
25699256ec35SKonstantin Komarov 
25709256ec35SKonstantin Komarov 	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
25719256ec35SKonstantin Komarov 	       attr->type == ATTR_DATA && !attr->name_len) {
25729256ec35SKonstantin Komarov 		le64_sub_cpu(&attr->nres.svcn, len);
25739256ec35SKonstantin Komarov 		le64_sub_cpu(&attr->nres.evcn, len);
25749256ec35SKonstantin Komarov 		if (le) {
25759256ec35SKonstantin Komarov 			le->vcn = attr->nres.svcn;
25769256ec35SKonstantin Komarov 			ni->attr_list.dirty = true;
25779256ec35SKonstantin Komarov 		}
25789256ec35SKonstantin Komarov 		mi->dirty = true;
25799256ec35SKonstantin Komarov 	}
25809256ec35SKonstantin Komarov 
25819256ec35SKonstantin Komarov 	goto out;
2582aa30eccbSKonstantin Komarov }
2583