xref: /OK3568_Linux_fs/kernel/fs/ntfs/runlist.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * runlist.c - NTFS runlist handling code.  Part of the Linux-NTFS project.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2001-2007 Anton Altaparmakov
6*4882a593Smuzhiyun  * Copyright (c) 2002-2005 Richard Russon
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include "debug.h"
10*4882a593Smuzhiyun #include "dir.h"
11*4882a593Smuzhiyun #include "endian.h"
12*4882a593Smuzhiyun #include "malloc.h"
13*4882a593Smuzhiyun #include "ntfs.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun  * ntfs_rl_mm - runlist memmove
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlist @base.
19*4882a593Smuzhiyun  */
ntfs_rl_mm(runlist_element * base,int dst,int src,int size)20*4882a593Smuzhiyun static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
21*4882a593Smuzhiyun 		int size)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	if (likely((dst != src) && (size > 0)))
24*4882a593Smuzhiyun 		memmove(base + dst, base + src, size * sizeof(*base));
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /**
28*4882a593Smuzhiyun  * ntfs_rl_mc - runlist memory copy
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dstbase and
31*4882a593Smuzhiyun  * @srcbase.
32*4882a593Smuzhiyun  */
ntfs_rl_mc(runlist_element * dstbase,int dst,runlist_element * srcbase,int src,int size)33*4882a593Smuzhiyun static inline void ntfs_rl_mc(runlist_element *dstbase, int dst,
34*4882a593Smuzhiyun 		runlist_element *srcbase, int src, int size)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	if (likely(size > 0))
37*4882a593Smuzhiyun 		memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun  * ntfs_rl_realloc - Reallocate memory for runlists
42*4882a593Smuzhiyun  * @rl:		original runlist
43*4882a593Smuzhiyun  * @old_size:	number of runlist elements in the original runlist @rl
44*4882a593Smuzhiyun  * @new_size:	number of runlist elements we need space for
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * As the runlists grow, more memory will be required.  To prevent the
47*4882a593Smuzhiyun  * kernel having to allocate and reallocate large numbers of small bits of
48*4882a593Smuzhiyun  * memory, this function returns an entire page of memory.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlist @rl.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * N.B.  If the new allocation doesn't require a different number of pages in
53*4882a593Smuzhiyun  *       memory, the function will return the original pointer.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * On success, return a pointer to the newly allocated, or recycled, memory.
56*4882a593Smuzhiyun  * On error, return -errno. The following error codes are defined:
57*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
58*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
59*4882a593Smuzhiyun  */
ntfs_rl_realloc(runlist_element * rl,int old_size,int new_size)60*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
61*4882a593Smuzhiyun 		int old_size, int new_size)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	runlist_element *new_rl;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	old_size = PAGE_ALIGN(old_size * sizeof(*rl));
66*4882a593Smuzhiyun 	new_size = PAGE_ALIGN(new_size * sizeof(*rl));
67*4882a593Smuzhiyun 	if (old_size == new_size)
68*4882a593Smuzhiyun 		return rl;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	new_rl = ntfs_malloc_nofs(new_size);
71*4882a593Smuzhiyun 	if (unlikely(!new_rl))
72*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (likely(rl != NULL)) {
75*4882a593Smuzhiyun 		if (unlikely(old_size > new_size))
76*4882a593Smuzhiyun 			old_size = new_size;
77*4882a593Smuzhiyun 		memcpy(new_rl, rl, old_size);
78*4882a593Smuzhiyun 		ntfs_free(rl);
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 	return new_rl;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * ntfs_rl_realloc_nofail - Reallocate memory for runlists
85*4882a593Smuzhiyun  * @rl:		original runlist
86*4882a593Smuzhiyun  * @old_size:	number of runlist elements in the original runlist @rl
87*4882a593Smuzhiyun  * @new_size:	number of runlist elements we need space for
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * As the runlists grow, more memory will be required.  To prevent the
90*4882a593Smuzhiyun  * kernel having to allocate and reallocate large numbers of small bits of
91*4882a593Smuzhiyun  * memory, this function returns an entire page of memory.
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * This function guarantees that the allocation will succeed.  It will sleep
94*4882a593Smuzhiyun  * for as long as it takes to complete the allocation.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlist @rl.
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * N.B.  If the new allocation doesn't require a different number of pages in
99*4882a593Smuzhiyun  *       memory, the function will return the original pointer.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * On success, return a pointer to the newly allocated, or recycled, memory.
102*4882a593Smuzhiyun  * On error, return -errno. The following error codes are defined:
103*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
104*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
105*4882a593Smuzhiyun  */
ntfs_rl_realloc_nofail(runlist_element * rl,int old_size,int new_size)106*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_realloc_nofail(runlist_element *rl,
107*4882a593Smuzhiyun 		int old_size, int new_size)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	runlist_element *new_rl;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	old_size = PAGE_ALIGN(old_size * sizeof(*rl));
112*4882a593Smuzhiyun 	new_size = PAGE_ALIGN(new_size * sizeof(*rl));
113*4882a593Smuzhiyun 	if (old_size == new_size)
114*4882a593Smuzhiyun 		return rl;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	new_rl = ntfs_malloc_nofs_nofail(new_size);
117*4882a593Smuzhiyun 	BUG_ON(!new_rl);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (likely(rl != NULL)) {
120*4882a593Smuzhiyun 		if (unlikely(old_size > new_size))
121*4882a593Smuzhiyun 			old_size = new_size;
122*4882a593Smuzhiyun 		memcpy(new_rl, rl, old_size);
123*4882a593Smuzhiyun 		ntfs_free(rl);
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 	return new_rl;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun  * ntfs_are_rl_mergeable - test if two runlists can be joined together
130*4882a593Smuzhiyun  * @dst:	original runlist
131*4882a593Smuzhiyun  * @src:	new runlist to test for mergeability with @dst
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * Test if two runlists can be joined together. For this, their VCNs and LCNs
134*4882a593Smuzhiyun  * must be adjacent.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * Return: true   Success, the runlists can be merged.
139*4882a593Smuzhiyun  *	   false  Failure, the runlists cannot be merged.
140*4882a593Smuzhiyun  */
ntfs_are_rl_mergeable(runlist_element * dst,runlist_element * src)141*4882a593Smuzhiyun static inline bool ntfs_are_rl_mergeable(runlist_element *dst,
142*4882a593Smuzhiyun 		runlist_element *src)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	BUG_ON(!dst);
145*4882a593Smuzhiyun 	BUG_ON(!src);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* We can merge unmapped regions even if they are misaligned. */
148*4882a593Smuzhiyun 	if ((dst->lcn == LCN_RL_NOT_MAPPED) && (src->lcn == LCN_RL_NOT_MAPPED))
149*4882a593Smuzhiyun 		return true;
150*4882a593Smuzhiyun 	/* If the runs are misaligned, we cannot merge them. */
151*4882a593Smuzhiyun 	if ((dst->vcn + dst->length) != src->vcn)
152*4882a593Smuzhiyun 		return false;
153*4882a593Smuzhiyun 	/* If both runs are non-sparse and contiguous, we can merge them. */
154*4882a593Smuzhiyun 	if ((dst->lcn >= 0) && (src->lcn >= 0) &&
155*4882a593Smuzhiyun 			((dst->lcn + dst->length) == src->lcn))
156*4882a593Smuzhiyun 		return true;
157*4882a593Smuzhiyun 	/* If we are merging two holes, we can merge them. */
158*4882a593Smuzhiyun 	if ((dst->lcn == LCN_HOLE) && (src->lcn == LCN_HOLE))
159*4882a593Smuzhiyun 		return true;
160*4882a593Smuzhiyun 	/* Cannot merge. */
161*4882a593Smuzhiyun 	return false;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun  * __ntfs_rl_merge - merge two runlists without testing if they can be merged
166*4882a593Smuzhiyun  * @dst:	original, destination runlist
167*4882a593Smuzhiyun  * @src:	new runlist to merge with @dst
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * Merge the two runlists, writing into the destination runlist @dst. The
170*4882a593Smuzhiyun  * caller must make sure the runlists can be merged or this will corrupt the
171*4882a593Smuzhiyun  * destination runlist.
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
174*4882a593Smuzhiyun  */
__ntfs_rl_merge(runlist_element * dst,runlist_element * src)175*4882a593Smuzhiyun static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	dst->length += src->length;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun  * ntfs_rl_append - append a runlist after a given element
182*4882a593Smuzhiyun  * @dst:	original runlist to be worked on
183*4882a593Smuzhiyun  * @dsize:	number of elements in @dst (including end marker)
184*4882a593Smuzhiyun  * @src:	runlist to be inserted into @dst
185*4882a593Smuzhiyun  * @ssize:	number of elements in @src (excluding end marker)
186*4882a593Smuzhiyun  * @loc:	append the new runlist @src after this element in @dst
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * Append the runlist @src after element @loc in @dst.  Merge the right end of
189*4882a593Smuzhiyun  * the new runlist, if necessary. Adjust the size of the hole before the
190*4882a593Smuzhiyun  * appended runlist.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * On success, return a pointer to the new, combined, runlist. Note, both
195*4882a593Smuzhiyun  * runlists @dst and @src are deallocated before returning so you cannot use
196*4882a593Smuzhiyun  * the pointers for anything any more. (Strictly speaking the returned runlist
197*4882a593Smuzhiyun  * may be the same as @dst but this is irrelevant.)
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * On error, return -errno. Both runlists are left unmodified. The following
200*4882a593Smuzhiyun  * error codes are defined:
201*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
202*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
203*4882a593Smuzhiyun  */
ntfs_rl_append(runlist_element * dst,int dsize,runlist_element * src,int ssize,int loc)204*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_append(runlist_element *dst,
205*4882a593Smuzhiyun 		int dsize, runlist_element *src, int ssize, int loc)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	bool right = false;	/* Right end of @src needs merging. */
208*4882a593Smuzhiyun 	int marker;		/* End of the inserted runs. */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	BUG_ON(!dst);
211*4882a593Smuzhiyun 	BUG_ON(!src);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* First, check if the right hand end needs merging. */
214*4882a593Smuzhiyun 	if ((loc + 1) < dsize)
215*4882a593Smuzhiyun 		right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* Space required: @dst size + @src size, less one if we merged. */
218*4882a593Smuzhiyun 	dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
219*4882a593Smuzhiyun 	if (IS_ERR(dst))
220*4882a593Smuzhiyun 		return dst;
221*4882a593Smuzhiyun 	/*
222*4882a593Smuzhiyun 	 * We are guaranteed to succeed from here so can start modifying the
223*4882a593Smuzhiyun 	 * original runlists.
224*4882a593Smuzhiyun 	 */
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* First, merge the right hand end, if necessary. */
227*4882a593Smuzhiyun 	if (right)
228*4882a593Smuzhiyun 		__ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* First run after the @src runs that have been inserted. */
231*4882a593Smuzhiyun 	marker = loc + ssize + 1;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* Move the tail of @dst out of the way, then copy in @src. */
234*4882a593Smuzhiyun 	ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - (loc + 1 + right));
235*4882a593Smuzhiyun 	ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Adjust the size of the preceding hole. */
238*4882a593Smuzhiyun 	dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* We may have changed the length of the file, so fix the end marker */
241*4882a593Smuzhiyun 	if (dst[marker].lcn == LCN_ENOENT)
242*4882a593Smuzhiyun 		dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	return dst;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun  * ntfs_rl_insert - insert a runlist into another
249*4882a593Smuzhiyun  * @dst:	original runlist to be worked on
250*4882a593Smuzhiyun  * @dsize:	number of elements in @dst (including end marker)
251*4882a593Smuzhiyun  * @src:	new runlist to be inserted
252*4882a593Smuzhiyun  * @ssize:	number of elements in @src (excluding end marker)
253*4882a593Smuzhiyun  * @loc:	insert the new runlist @src before this element in @dst
254*4882a593Smuzhiyun  *
255*4882a593Smuzhiyun  * Insert the runlist @src before element @loc in the runlist @dst. Merge the
256*4882a593Smuzhiyun  * left end of the new runlist, if necessary. Adjust the size of the hole
257*4882a593Smuzhiyun  * after the inserted runlist.
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * On success, return a pointer to the new, combined, runlist. Note, both
262*4882a593Smuzhiyun  * runlists @dst and @src are deallocated before returning so you cannot use
263*4882a593Smuzhiyun  * the pointers for anything any more. (Strictly speaking the returned runlist
264*4882a593Smuzhiyun  * may be the same as @dst but this is irrelevant.)
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  * On error, return -errno. Both runlists are left unmodified. The following
267*4882a593Smuzhiyun  * error codes are defined:
268*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
269*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
270*4882a593Smuzhiyun  */
ntfs_rl_insert(runlist_element * dst,int dsize,runlist_element * src,int ssize,int loc)271*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
272*4882a593Smuzhiyun 		int dsize, runlist_element *src, int ssize, int loc)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	bool left = false;	/* Left end of @src needs merging. */
275*4882a593Smuzhiyun 	bool disc = false;	/* Discontinuity between @dst and @src. */
276*4882a593Smuzhiyun 	int marker;		/* End of the inserted runs. */
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	BUG_ON(!dst);
279*4882a593Smuzhiyun 	BUG_ON(!src);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/*
282*4882a593Smuzhiyun 	 * disc => Discontinuity between the end of @dst and the start of @src.
283*4882a593Smuzhiyun 	 *	   This means we might need to insert a "not mapped" run.
284*4882a593Smuzhiyun 	 */
285*4882a593Smuzhiyun 	if (loc == 0)
286*4882a593Smuzhiyun 		disc = (src[0].vcn > 0);
287*4882a593Smuzhiyun 	else {
288*4882a593Smuzhiyun 		s64 merged_length;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		left = ntfs_are_rl_mergeable(dst + loc - 1, src);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 		merged_length = dst[loc - 1].length;
293*4882a593Smuzhiyun 		if (left)
294*4882a593Smuzhiyun 			merged_length += src->length;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 		disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
297*4882a593Smuzhiyun 	}
298*4882a593Smuzhiyun 	/*
299*4882a593Smuzhiyun 	 * Space required: @dst size + @src size, less one if we merged, plus
300*4882a593Smuzhiyun 	 * one if there was a discontinuity.
301*4882a593Smuzhiyun 	 */
302*4882a593Smuzhiyun 	dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc);
303*4882a593Smuzhiyun 	if (IS_ERR(dst))
304*4882a593Smuzhiyun 		return dst;
305*4882a593Smuzhiyun 	/*
306*4882a593Smuzhiyun 	 * We are guaranteed to succeed from here so can start modifying the
307*4882a593Smuzhiyun 	 * original runlist.
308*4882a593Smuzhiyun 	 */
309*4882a593Smuzhiyun 	if (left)
310*4882a593Smuzhiyun 		__ntfs_rl_merge(dst + loc - 1, src);
311*4882a593Smuzhiyun 	/*
312*4882a593Smuzhiyun 	 * First run after the @src runs that have been inserted.
313*4882a593Smuzhiyun 	 * Nominally,  @marker equals @loc + @ssize, i.e. location + number of
314*4882a593Smuzhiyun 	 * runs in @src.  However, if @left, then the first run in @src has
315*4882a593Smuzhiyun 	 * been merged with one in @dst.  And if @disc, then @dst and @src do
316*4882a593Smuzhiyun 	 * not meet and we need an extra run to fill the gap.
317*4882a593Smuzhiyun 	 */
318*4882a593Smuzhiyun 	marker = loc + ssize - left + disc;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* Move the tail of @dst out of the way, then copy in @src. */
321*4882a593Smuzhiyun 	ntfs_rl_mm(dst, marker, loc, dsize - loc);
322*4882a593Smuzhiyun 	ntfs_rl_mc(dst, loc + disc, src, left, ssize - left);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* Adjust the VCN of the first run after the insertion... */
325*4882a593Smuzhiyun 	dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
326*4882a593Smuzhiyun 	/* ... and the length. */
327*4882a593Smuzhiyun 	if (dst[marker].lcn == LCN_HOLE || dst[marker].lcn == LCN_RL_NOT_MAPPED)
328*4882a593Smuzhiyun 		dst[marker].length = dst[marker + 1].vcn - dst[marker].vcn;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* Writing beyond the end of the file and there is a discontinuity. */
331*4882a593Smuzhiyun 	if (disc) {
332*4882a593Smuzhiyun 		if (loc > 0) {
333*4882a593Smuzhiyun 			dst[loc].vcn = dst[loc - 1].vcn + dst[loc - 1].length;
334*4882a593Smuzhiyun 			dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
335*4882a593Smuzhiyun 		} else {
336*4882a593Smuzhiyun 			dst[loc].vcn = 0;
337*4882a593Smuzhiyun 			dst[loc].length = dst[loc + 1].vcn;
338*4882a593Smuzhiyun 		}
339*4882a593Smuzhiyun 		dst[loc].lcn = LCN_RL_NOT_MAPPED;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 	return dst;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  * ntfs_rl_replace - overwrite a runlist element with another runlist
346*4882a593Smuzhiyun  * @dst:	original runlist to be worked on
347*4882a593Smuzhiyun  * @dsize:	number of elements in @dst (including end marker)
348*4882a593Smuzhiyun  * @src:	new runlist to be inserted
349*4882a593Smuzhiyun  * @ssize:	number of elements in @src (excluding end marker)
350*4882a593Smuzhiyun  * @loc:	index in runlist @dst to overwrite with @src
351*4882a593Smuzhiyun  *
352*4882a593Smuzhiyun  * Replace the runlist element @dst at @loc with @src. Merge the left and
353*4882a593Smuzhiyun  * right ends of the inserted runlist, if necessary.
354*4882a593Smuzhiyun  *
355*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * On success, return a pointer to the new, combined, runlist. Note, both
358*4882a593Smuzhiyun  * runlists @dst and @src are deallocated before returning so you cannot use
359*4882a593Smuzhiyun  * the pointers for anything any more. (Strictly speaking the returned runlist
360*4882a593Smuzhiyun  * may be the same as @dst but this is irrelevant.)
361*4882a593Smuzhiyun  *
362*4882a593Smuzhiyun  * On error, return -errno. Both runlists are left unmodified. The following
363*4882a593Smuzhiyun  * error codes are defined:
364*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
365*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
366*4882a593Smuzhiyun  */
ntfs_rl_replace(runlist_element * dst,int dsize,runlist_element * src,int ssize,int loc)367*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
368*4882a593Smuzhiyun 		int dsize, runlist_element *src, int ssize, int loc)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	signed delta;
371*4882a593Smuzhiyun 	bool left = false;	/* Left end of @src needs merging. */
372*4882a593Smuzhiyun 	bool right = false;	/* Right end of @src needs merging. */
373*4882a593Smuzhiyun 	int tail;		/* Start of tail of @dst. */
374*4882a593Smuzhiyun 	int marker;		/* End of the inserted runs. */
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	BUG_ON(!dst);
377*4882a593Smuzhiyun 	BUG_ON(!src);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/* First, see if the left and right ends need merging. */
380*4882a593Smuzhiyun 	if ((loc + 1) < dsize)
381*4882a593Smuzhiyun 		right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
382*4882a593Smuzhiyun 	if (loc > 0)
383*4882a593Smuzhiyun 		left = ntfs_are_rl_mergeable(dst + loc - 1, src);
384*4882a593Smuzhiyun 	/*
385*4882a593Smuzhiyun 	 * Allocate some space.  We will need less if the left, right, or both
386*4882a593Smuzhiyun 	 * ends get merged.  The -1 accounts for the run being replaced.
387*4882a593Smuzhiyun 	 */
388*4882a593Smuzhiyun 	delta = ssize - 1 - left - right;
389*4882a593Smuzhiyun 	if (delta > 0) {
390*4882a593Smuzhiyun 		dst = ntfs_rl_realloc(dst, dsize, dsize + delta);
391*4882a593Smuzhiyun 		if (IS_ERR(dst))
392*4882a593Smuzhiyun 			return dst;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 	/*
395*4882a593Smuzhiyun 	 * We are guaranteed to succeed from here so can start modifying the
396*4882a593Smuzhiyun 	 * original runlists.
397*4882a593Smuzhiyun 	 */
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* First, merge the left and right ends, if necessary. */
400*4882a593Smuzhiyun 	if (right)
401*4882a593Smuzhiyun 		__ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
402*4882a593Smuzhiyun 	if (left)
403*4882a593Smuzhiyun 		__ntfs_rl_merge(dst + loc - 1, src);
404*4882a593Smuzhiyun 	/*
405*4882a593Smuzhiyun 	 * Offset of the tail of @dst.  This needs to be moved out of the way
406*4882a593Smuzhiyun 	 * to make space for the runs to be copied from @src, i.e. the first
407*4882a593Smuzhiyun 	 * run of the tail of @dst.
408*4882a593Smuzhiyun 	 * Nominally, @tail equals @loc + 1, i.e. location, skipping the
409*4882a593Smuzhiyun 	 * replaced run.  However, if @right, then one of @dst's runs is
410*4882a593Smuzhiyun 	 * already merged into @src.
411*4882a593Smuzhiyun 	 */
412*4882a593Smuzhiyun 	tail = loc + right + 1;
413*4882a593Smuzhiyun 	/*
414*4882a593Smuzhiyun 	 * First run after the @src runs that have been inserted, i.e. where
415*4882a593Smuzhiyun 	 * the tail of @dst needs to be moved to.
416*4882a593Smuzhiyun 	 * Nominally, @marker equals @loc + @ssize, i.e. location + number of
417*4882a593Smuzhiyun 	 * runs in @src.  However, if @left, then the first run in @src has
418*4882a593Smuzhiyun 	 * been merged with one in @dst.
419*4882a593Smuzhiyun 	 */
420*4882a593Smuzhiyun 	marker = loc + ssize - left;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	/* Move the tail of @dst out of the way, then copy in @src. */
423*4882a593Smuzhiyun 	ntfs_rl_mm(dst, marker, tail, dsize - tail);
424*4882a593Smuzhiyun 	ntfs_rl_mc(dst, loc, src, left, ssize - left);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	/* We may have changed the length of the file, so fix the end marker. */
427*4882a593Smuzhiyun 	if (dsize - tail > 0 && dst[marker].lcn == LCN_ENOENT)
428*4882a593Smuzhiyun 		dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
429*4882a593Smuzhiyun 	return dst;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /**
433*4882a593Smuzhiyun  * ntfs_rl_split - insert a runlist into the centre of a hole
434*4882a593Smuzhiyun  * @dst:	original runlist to be worked on
435*4882a593Smuzhiyun  * @dsize:	number of elements in @dst (including end marker)
436*4882a593Smuzhiyun  * @src:	new runlist to be inserted
437*4882a593Smuzhiyun  * @ssize:	number of elements in @src (excluding end marker)
438*4882a593Smuzhiyun  * @loc:	index in runlist @dst at which to split and insert @src
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * Split the runlist @dst at @loc into two and insert @new in between the two
441*4882a593Smuzhiyun  * fragments. No merging of runlists is necessary. Adjust the size of the
442*4882a593Smuzhiyun  * holes either side.
443*4882a593Smuzhiyun  *
444*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @dst and @src.
445*4882a593Smuzhiyun  *
446*4882a593Smuzhiyun  * On success, return a pointer to the new, combined, runlist. Note, both
447*4882a593Smuzhiyun  * runlists @dst and @src are deallocated before returning so you cannot use
448*4882a593Smuzhiyun  * the pointers for anything any more. (Strictly speaking the returned runlist
449*4882a593Smuzhiyun  * may be the same as @dst but this is irrelevant.)
450*4882a593Smuzhiyun  *
451*4882a593Smuzhiyun  * On error, return -errno. Both runlists are left unmodified. The following
452*4882a593Smuzhiyun  * error codes are defined:
453*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
454*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
455*4882a593Smuzhiyun  */
ntfs_rl_split(runlist_element * dst,int dsize,runlist_element * src,int ssize,int loc)456*4882a593Smuzhiyun static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
457*4882a593Smuzhiyun 		runlist_element *src, int ssize, int loc)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	BUG_ON(!dst);
460*4882a593Smuzhiyun 	BUG_ON(!src);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	/* Space required: @dst size + @src size + one new hole. */
463*4882a593Smuzhiyun 	dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
464*4882a593Smuzhiyun 	if (IS_ERR(dst))
465*4882a593Smuzhiyun 		return dst;
466*4882a593Smuzhiyun 	/*
467*4882a593Smuzhiyun 	 * We are guaranteed to succeed from here so can start modifying the
468*4882a593Smuzhiyun 	 * original runlists.
469*4882a593Smuzhiyun 	 */
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	/* Move the tail of @dst out of the way, then copy in @src. */
472*4882a593Smuzhiyun 	ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
473*4882a593Smuzhiyun 	ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	/* Adjust the size of the holes either size of @src. */
476*4882a593Smuzhiyun 	dst[loc].length		= dst[loc+1].vcn       - dst[loc].vcn;
477*4882a593Smuzhiyun 	dst[loc+ssize+1].vcn    = dst[loc+ssize].vcn   + dst[loc+ssize].length;
478*4882a593Smuzhiyun 	dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	return dst;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun /**
484*4882a593Smuzhiyun  * ntfs_runlists_merge - merge two runlists into one
485*4882a593Smuzhiyun  * @drl:	original runlist to be worked on
486*4882a593Smuzhiyun  * @srl:	new runlist to be merged into @drl
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * First we sanity check the two runlists @srl and @drl to make sure that they
489*4882a593Smuzhiyun  * are sensible and can be merged. The runlist @srl must be either after the
490*4882a593Smuzhiyun  * runlist @drl or completely within a hole (or unmapped region) in @drl.
491*4882a593Smuzhiyun  *
492*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlists @drl and @srl.
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * Merging of runlists is necessary in two cases:
495*4882a593Smuzhiyun  *   1. When attribute lists are used and a further extent is being mapped.
496*4882a593Smuzhiyun  *   2. When new clusters are allocated to fill a hole or extend a file.
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  * There are four possible ways @srl can be merged. It can:
499*4882a593Smuzhiyun  *	- be inserted at the beginning of a hole,
500*4882a593Smuzhiyun  *	- split the hole in two and be inserted between the two fragments,
501*4882a593Smuzhiyun  *	- be appended at the end of a hole, or it can
502*4882a593Smuzhiyun  *	- replace the whole hole.
503*4882a593Smuzhiyun  * It can also be appended to the end of the runlist, which is just a variant
504*4882a593Smuzhiyun  * of the insert case.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * On success, return a pointer to the new, combined, runlist. Note, both
507*4882a593Smuzhiyun  * runlists @drl and @srl are deallocated before returning so you cannot use
508*4882a593Smuzhiyun  * the pointers for anything any more. (Strictly speaking the returned runlist
509*4882a593Smuzhiyun  * may be the same as @dst but this is irrelevant.)
510*4882a593Smuzhiyun  *
511*4882a593Smuzhiyun  * On error, return -errno. Both runlists are left unmodified. The following
512*4882a593Smuzhiyun  * error codes are defined:
513*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
514*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
515*4882a593Smuzhiyun  *	-ERANGE	- The runlists overlap and cannot be merged.
516*4882a593Smuzhiyun  */
ntfs_runlists_merge(runlist_element * drl,runlist_element * srl)517*4882a593Smuzhiyun runlist_element *ntfs_runlists_merge(runlist_element *drl,
518*4882a593Smuzhiyun 		runlist_element *srl)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	int di, si;		/* Current index into @[ds]rl. */
521*4882a593Smuzhiyun 	int sstart;		/* First index with lcn > LCN_RL_NOT_MAPPED. */
522*4882a593Smuzhiyun 	int dins;		/* Index into @drl at which to insert @srl. */
523*4882a593Smuzhiyun 	int dend, send;		/* Last index into @[ds]rl. */
524*4882a593Smuzhiyun 	int dfinal, sfinal;	/* The last index into @[ds]rl with
525*4882a593Smuzhiyun 				   lcn >= LCN_HOLE. */
526*4882a593Smuzhiyun 	int marker = 0;
527*4882a593Smuzhiyun 	VCN marker_vcn = 0;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun #ifdef DEBUG
530*4882a593Smuzhiyun 	ntfs_debug("dst:");
531*4882a593Smuzhiyun 	ntfs_debug_dump_runlist(drl);
532*4882a593Smuzhiyun 	ntfs_debug("src:");
533*4882a593Smuzhiyun 	ntfs_debug_dump_runlist(srl);
534*4882a593Smuzhiyun #endif
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	/* Check for silly calling... */
537*4882a593Smuzhiyun 	if (unlikely(!srl))
538*4882a593Smuzhiyun 		return drl;
539*4882a593Smuzhiyun 	if (IS_ERR(srl) || IS_ERR(drl))
540*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	/* Check for the case where the first mapping is being done now. */
543*4882a593Smuzhiyun 	if (unlikely(!drl)) {
544*4882a593Smuzhiyun 		drl = srl;
545*4882a593Smuzhiyun 		/* Complete the source runlist if necessary. */
546*4882a593Smuzhiyun 		if (unlikely(drl[0].vcn)) {
547*4882a593Smuzhiyun 			/* Scan to the end of the source runlist. */
548*4882a593Smuzhiyun 			for (dend = 0; likely(drl[dend].length); dend++)
549*4882a593Smuzhiyun 				;
550*4882a593Smuzhiyun 			dend++;
551*4882a593Smuzhiyun 			drl = ntfs_rl_realloc(drl, dend, dend + 1);
552*4882a593Smuzhiyun 			if (IS_ERR(drl))
553*4882a593Smuzhiyun 				return drl;
554*4882a593Smuzhiyun 			/* Insert start element at the front of the runlist. */
555*4882a593Smuzhiyun 			ntfs_rl_mm(drl, 1, 0, dend);
556*4882a593Smuzhiyun 			drl[0].vcn = 0;
557*4882a593Smuzhiyun 			drl[0].lcn = LCN_RL_NOT_MAPPED;
558*4882a593Smuzhiyun 			drl[0].length = drl[1].vcn;
559*4882a593Smuzhiyun 		}
560*4882a593Smuzhiyun 		goto finished;
561*4882a593Smuzhiyun 	}
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	si = di = 0;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* Skip any unmapped start element(s) in the source runlist. */
566*4882a593Smuzhiyun 	while (srl[si].length && srl[si].lcn < LCN_HOLE)
567*4882a593Smuzhiyun 		si++;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* Can't have an entirely unmapped source runlist. */
570*4882a593Smuzhiyun 	BUG_ON(!srl[si].length);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/* Record the starting points. */
573*4882a593Smuzhiyun 	sstart = si;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	/*
576*4882a593Smuzhiyun 	 * Skip forward in @drl until we reach the position where @srl needs to
577*4882a593Smuzhiyun 	 * be inserted. If we reach the end of @drl, @srl just needs to be
578*4882a593Smuzhiyun 	 * appended to @drl.
579*4882a593Smuzhiyun 	 */
580*4882a593Smuzhiyun 	for (; drl[di].length; di++) {
581*4882a593Smuzhiyun 		if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
582*4882a593Smuzhiyun 			break;
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 	dins = di;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	/* Sanity check for illegal overlaps. */
587*4882a593Smuzhiyun 	if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
588*4882a593Smuzhiyun 			(srl[si].lcn >= 0)) {
589*4882a593Smuzhiyun 		ntfs_error(NULL, "Run lists overlap. Cannot merge!");
590*4882a593Smuzhiyun 		return ERR_PTR(-ERANGE);
591*4882a593Smuzhiyun 	}
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	/* Scan to the end of both runlists in order to know their sizes. */
594*4882a593Smuzhiyun 	for (send = si; srl[send].length; send++)
595*4882a593Smuzhiyun 		;
596*4882a593Smuzhiyun 	for (dend = di; drl[dend].length; dend++)
597*4882a593Smuzhiyun 		;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	if (srl[send].lcn == LCN_ENOENT)
600*4882a593Smuzhiyun 		marker_vcn = srl[marker = send].vcn;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* Scan to the last element with lcn >= LCN_HOLE. */
603*4882a593Smuzhiyun 	for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
604*4882a593Smuzhiyun 		;
605*4882a593Smuzhiyun 	for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
606*4882a593Smuzhiyun 		;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	{
609*4882a593Smuzhiyun 	bool start;
610*4882a593Smuzhiyun 	bool finish;
611*4882a593Smuzhiyun 	int ds = dend + 1;		/* Number of elements in drl & srl */
612*4882a593Smuzhiyun 	int ss = sfinal - sstart + 1;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	start  = ((drl[dins].lcn <  LCN_RL_NOT_MAPPED) ||    /* End of file   */
615*4882a593Smuzhiyun 		  (drl[dins].vcn == srl[sstart].vcn));	     /* Start of hole */
616*4882a593Smuzhiyun 	finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) &&    /* End of file   */
617*4882a593Smuzhiyun 		 ((drl[dins].vcn + drl[dins].length) <=      /* End of hole   */
618*4882a593Smuzhiyun 		  (srl[send - 1].vcn + srl[send - 1].length)));
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	/* Or we will lose an end marker. */
621*4882a593Smuzhiyun 	if (finish && !drl[dins].length)
622*4882a593Smuzhiyun 		ss++;
623*4882a593Smuzhiyun 	if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
624*4882a593Smuzhiyun 		finish = false;
625*4882a593Smuzhiyun #if 0
626*4882a593Smuzhiyun 	ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
627*4882a593Smuzhiyun 	ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
628*4882a593Smuzhiyun 	ntfs_debug("start = %i, finish = %i", start, finish);
629*4882a593Smuzhiyun 	ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
630*4882a593Smuzhiyun #endif
631*4882a593Smuzhiyun 	if (start) {
632*4882a593Smuzhiyun 		if (finish)
633*4882a593Smuzhiyun 			drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
634*4882a593Smuzhiyun 		else
635*4882a593Smuzhiyun 			drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
636*4882a593Smuzhiyun 	} else {
637*4882a593Smuzhiyun 		if (finish)
638*4882a593Smuzhiyun 			drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
639*4882a593Smuzhiyun 		else
640*4882a593Smuzhiyun 			drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 	if (IS_ERR(drl)) {
643*4882a593Smuzhiyun 		ntfs_error(NULL, "Merge failed.");
644*4882a593Smuzhiyun 		return drl;
645*4882a593Smuzhiyun 	}
646*4882a593Smuzhiyun 	ntfs_free(srl);
647*4882a593Smuzhiyun 	if (marker) {
648*4882a593Smuzhiyun 		ntfs_debug("Triggering marker code.");
649*4882a593Smuzhiyun 		for (ds = dend; drl[ds].length; ds++)
650*4882a593Smuzhiyun 			;
651*4882a593Smuzhiyun 		/* We only need to care if @srl ended after @drl. */
652*4882a593Smuzhiyun 		if (drl[ds].vcn <= marker_vcn) {
653*4882a593Smuzhiyun 			int slots = 0;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 			if (drl[ds].vcn == marker_vcn) {
656*4882a593Smuzhiyun 				ntfs_debug("Old marker = 0x%llx, replacing "
657*4882a593Smuzhiyun 						"with LCN_ENOENT.",
658*4882a593Smuzhiyun 						(unsigned long long)
659*4882a593Smuzhiyun 						drl[ds].lcn);
660*4882a593Smuzhiyun 				drl[ds].lcn = LCN_ENOENT;
661*4882a593Smuzhiyun 				goto finished;
662*4882a593Smuzhiyun 			}
663*4882a593Smuzhiyun 			/*
664*4882a593Smuzhiyun 			 * We need to create an unmapped runlist element in
665*4882a593Smuzhiyun 			 * @drl or extend an existing one before adding the
666*4882a593Smuzhiyun 			 * ENOENT terminator.
667*4882a593Smuzhiyun 			 */
668*4882a593Smuzhiyun 			if (drl[ds].lcn == LCN_ENOENT) {
669*4882a593Smuzhiyun 				ds--;
670*4882a593Smuzhiyun 				slots = 1;
671*4882a593Smuzhiyun 			}
672*4882a593Smuzhiyun 			if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
673*4882a593Smuzhiyun 				/* Add an unmapped runlist element. */
674*4882a593Smuzhiyun 				if (!slots) {
675*4882a593Smuzhiyun 					drl = ntfs_rl_realloc_nofail(drl, ds,
676*4882a593Smuzhiyun 							ds + 2);
677*4882a593Smuzhiyun 					slots = 2;
678*4882a593Smuzhiyun 				}
679*4882a593Smuzhiyun 				ds++;
680*4882a593Smuzhiyun 				/* Need to set vcn if it isn't set already. */
681*4882a593Smuzhiyun 				if (slots != 1)
682*4882a593Smuzhiyun 					drl[ds].vcn = drl[ds - 1].vcn +
683*4882a593Smuzhiyun 							drl[ds - 1].length;
684*4882a593Smuzhiyun 				drl[ds].lcn = LCN_RL_NOT_MAPPED;
685*4882a593Smuzhiyun 				/* We now used up a slot. */
686*4882a593Smuzhiyun 				slots--;
687*4882a593Smuzhiyun 			}
688*4882a593Smuzhiyun 			drl[ds].length = marker_vcn - drl[ds].vcn;
689*4882a593Smuzhiyun 			/* Finally add the ENOENT terminator. */
690*4882a593Smuzhiyun 			ds++;
691*4882a593Smuzhiyun 			if (!slots)
692*4882a593Smuzhiyun 				drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
693*4882a593Smuzhiyun 			drl[ds].vcn = marker_vcn;
694*4882a593Smuzhiyun 			drl[ds].lcn = LCN_ENOENT;
695*4882a593Smuzhiyun 			drl[ds].length = (s64)0;
696*4882a593Smuzhiyun 		}
697*4882a593Smuzhiyun 	}
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun finished:
701*4882a593Smuzhiyun 	/* The merge was completed successfully. */
702*4882a593Smuzhiyun 	ntfs_debug("Merged runlist:");
703*4882a593Smuzhiyun 	ntfs_debug_dump_runlist(drl);
704*4882a593Smuzhiyun 	return drl;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun /**
708*4882a593Smuzhiyun  * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
709*4882a593Smuzhiyun  * @vol:	ntfs volume on which the attribute resides
710*4882a593Smuzhiyun  * @attr:	attribute record whose mapping pairs array to decompress
711*4882a593Smuzhiyun  * @old_rl:	optional runlist in which to insert @attr's runlist
712*4882a593Smuzhiyun  *
713*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlist @old_rl.
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * Decompress the attribute @attr's mapping pairs array into a runlist. On
716*4882a593Smuzhiyun  * success, return the decompressed runlist.
717*4882a593Smuzhiyun  *
718*4882a593Smuzhiyun  * If @old_rl is not NULL, decompressed runlist is inserted into the
719*4882a593Smuzhiyun  * appropriate place in @old_rl and the resultant, combined runlist is
720*4882a593Smuzhiyun  * returned. The original @old_rl is deallocated.
721*4882a593Smuzhiyun  *
722*4882a593Smuzhiyun  * On error, return -errno. @old_rl is left unmodified in that case.
723*4882a593Smuzhiyun  *
724*4882a593Smuzhiyun  * The following error codes are defined:
725*4882a593Smuzhiyun  *	-ENOMEM	- Not enough memory to allocate runlist array.
726*4882a593Smuzhiyun  *	-EIO	- Corrupt runlist.
727*4882a593Smuzhiyun  *	-EINVAL	- Invalid parameters were passed in.
728*4882a593Smuzhiyun  *	-ERANGE	- The two runlists overlap.
729*4882a593Smuzhiyun  *
730*4882a593Smuzhiyun  * FIXME: For now we take the conceptionally simplest approach of creating the
731*4882a593Smuzhiyun  * new runlist disregarding the already existing one and then splicing the
732*4882a593Smuzhiyun  * two into one, if that is possible (we check for overlap and discard the new
733*4882a593Smuzhiyun  * runlist if overlap present before returning ERR_PTR(-ERANGE)).
734*4882a593Smuzhiyun  */
ntfs_mapping_pairs_decompress(const ntfs_volume * vol,const ATTR_RECORD * attr,runlist_element * old_rl)735*4882a593Smuzhiyun runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
736*4882a593Smuzhiyun 		const ATTR_RECORD *attr, runlist_element *old_rl)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun 	VCN vcn;		/* Current vcn. */
739*4882a593Smuzhiyun 	LCN lcn;		/* Current lcn. */
740*4882a593Smuzhiyun 	s64 deltaxcn;		/* Change in [vl]cn. */
741*4882a593Smuzhiyun 	runlist_element *rl;	/* The output runlist. */
742*4882a593Smuzhiyun 	u8 *buf;		/* Current position in mapping pairs array. */
743*4882a593Smuzhiyun 	u8 *attr_end;		/* End of attribute. */
744*4882a593Smuzhiyun 	int rlsize;		/* Size of runlist buffer. */
745*4882a593Smuzhiyun 	u16 rlpos;		/* Current runlist position in units of
746*4882a593Smuzhiyun 				   runlist_elements. */
747*4882a593Smuzhiyun 	u8 b;			/* Current byte offset in buf. */
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun #ifdef DEBUG
750*4882a593Smuzhiyun 	/* Make sure attr exists and is non-resident. */
751*4882a593Smuzhiyun 	if (!attr || !attr->non_resident || sle64_to_cpu(
752*4882a593Smuzhiyun 			attr->data.non_resident.lowest_vcn) < (VCN)0) {
753*4882a593Smuzhiyun 		ntfs_error(vol->sb, "Invalid arguments.");
754*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
755*4882a593Smuzhiyun 	}
756*4882a593Smuzhiyun #endif
757*4882a593Smuzhiyun 	/* Start at vcn = lowest_vcn and lcn 0. */
758*4882a593Smuzhiyun 	vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
759*4882a593Smuzhiyun 	lcn = 0;
760*4882a593Smuzhiyun 	/* Get start of the mapping pairs array. */
761*4882a593Smuzhiyun 	buf = (u8*)attr + le16_to_cpu(
762*4882a593Smuzhiyun 			attr->data.non_resident.mapping_pairs_offset);
763*4882a593Smuzhiyun 	attr_end = (u8*)attr + le32_to_cpu(attr->length);
764*4882a593Smuzhiyun 	if (unlikely(buf < (u8*)attr || buf > attr_end)) {
765*4882a593Smuzhiyun 		ntfs_error(vol->sb, "Corrupt attribute.");
766*4882a593Smuzhiyun 		return ERR_PTR(-EIO);
767*4882a593Smuzhiyun 	}
768*4882a593Smuzhiyun 	/* If the mapping pairs array is valid but empty, nothing to do. */
769*4882a593Smuzhiyun 	if (!vcn && !*buf)
770*4882a593Smuzhiyun 		return old_rl;
771*4882a593Smuzhiyun 	/* Current position in runlist array. */
772*4882a593Smuzhiyun 	rlpos = 0;
773*4882a593Smuzhiyun 	/* Allocate first page and set current runlist size to one page. */
774*4882a593Smuzhiyun 	rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
775*4882a593Smuzhiyun 	if (unlikely(!rl))
776*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
777*4882a593Smuzhiyun 	/* Insert unmapped starting element if necessary. */
778*4882a593Smuzhiyun 	if (vcn) {
779*4882a593Smuzhiyun 		rl->vcn = 0;
780*4882a593Smuzhiyun 		rl->lcn = LCN_RL_NOT_MAPPED;
781*4882a593Smuzhiyun 		rl->length = vcn;
782*4882a593Smuzhiyun 		rlpos++;
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 	while (buf < attr_end && *buf) {
785*4882a593Smuzhiyun 		/*
786*4882a593Smuzhiyun 		 * Allocate more memory if needed, including space for the
787*4882a593Smuzhiyun 		 * not-mapped and terminator elements. ntfs_malloc_nofs()
788*4882a593Smuzhiyun 		 * operates on whole pages only.
789*4882a593Smuzhiyun 		 */
790*4882a593Smuzhiyun 		if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
791*4882a593Smuzhiyun 			runlist_element *rl2;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 			rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
794*4882a593Smuzhiyun 			if (unlikely(!rl2)) {
795*4882a593Smuzhiyun 				ntfs_free(rl);
796*4882a593Smuzhiyun 				return ERR_PTR(-ENOMEM);
797*4882a593Smuzhiyun 			}
798*4882a593Smuzhiyun 			memcpy(rl2, rl, rlsize);
799*4882a593Smuzhiyun 			ntfs_free(rl);
800*4882a593Smuzhiyun 			rl = rl2;
801*4882a593Smuzhiyun 			rlsize += PAGE_SIZE;
802*4882a593Smuzhiyun 		}
803*4882a593Smuzhiyun 		/* Enter the current vcn into the current runlist element. */
804*4882a593Smuzhiyun 		rl[rlpos].vcn = vcn;
805*4882a593Smuzhiyun 		/*
806*4882a593Smuzhiyun 		 * Get the change in vcn, i.e. the run length in clusters.
807*4882a593Smuzhiyun 		 * Doing it this way ensures that we signextend negative values.
808*4882a593Smuzhiyun 		 * A negative run length doesn't make any sense, but hey, I
809*4882a593Smuzhiyun 		 * didn't make up the NTFS specs and Windows NT4 treats the run
810*4882a593Smuzhiyun 		 * length as a signed value so that's how it is...
811*4882a593Smuzhiyun 		 */
812*4882a593Smuzhiyun 		b = *buf & 0xf;
813*4882a593Smuzhiyun 		if (b) {
814*4882a593Smuzhiyun 			if (unlikely(buf + b > attr_end))
815*4882a593Smuzhiyun 				goto io_error;
816*4882a593Smuzhiyun 			for (deltaxcn = (s8)buf[b--]; b; b--)
817*4882a593Smuzhiyun 				deltaxcn = (deltaxcn << 8) + buf[b];
818*4882a593Smuzhiyun 		} else { /* The length entry is compulsory. */
819*4882a593Smuzhiyun 			ntfs_error(vol->sb, "Missing length entry in mapping "
820*4882a593Smuzhiyun 					"pairs array.");
821*4882a593Smuzhiyun 			deltaxcn = (s64)-1;
822*4882a593Smuzhiyun 		}
823*4882a593Smuzhiyun 		/*
824*4882a593Smuzhiyun 		 * Assume a negative length to indicate data corruption and
825*4882a593Smuzhiyun 		 * hence clean-up and return NULL.
826*4882a593Smuzhiyun 		 */
827*4882a593Smuzhiyun 		if (unlikely(deltaxcn < 0)) {
828*4882a593Smuzhiyun 			ntfs_error(vol->sb, "Invalid length in mapping pairs "
829*4882a593Smuzhiyun 					"array.");
830*4882a593Smuzhiyun 			goto err_out;
831*4882a593Smuzhiyun 		}
832*4882a593Smuzhiyun 		/*
833*4882a593Smuzhiyun 		 * Enter the current run length into the current runlist
834*4882a593Smuzhiyun 		 * element.
835*4882a593Smuzhiyun 		 */
836*4882a593Smuzhiyun 		rl[rlpos].length = deltaxcn;
837*4882a593Smuzhiyun 		/* Increment the current vcn by the current run length. */
838*4882a593Smuzhiyun 		vcn += deltaxcn;
839*4882a593Smuzhiyun 		/*
840*4882a593Smuzhiyun 		 * There might be no lcn change at all, as is the case for
841*4882a593Smuzhiyun 		 * sparse clusters on NTFS 3.0+, in which case we set the lcn
842*4882a593Smuzhiyun 		 * to LCN_HOLE.
843*4882a593Smuzhiyun 		 */
844*4882a593Smuzhiyun 		if (!(*buf & 0xf0))
845*4882a593Smuzhiyun 			rl[rlpos].lcn = LCN_HOLE;
846*4882a593Smuzhiyun 		else {
847*4882a593Smuzhiyun 			/* Get the lcn change which really can be negative. */
848*4882a593Smuzhiyun 			u8 b2 = *buf & 0xf;
849*4882a593Smuzhiyun 			b = b2 + ((*buf >> 4) & 0xf);
850*4882a593Smuzhiyun 			if (buf + b > attr_end)
851*4882a593Smuzhiyun 				goto io_error;
852*4882a593Smuzhiyun 			for (deltaxcn = (s8)buf[b--]; b > b2; b--)
853*4882a593Smuzhiyun 				deltaxcn = (deltaxcn << 8) + buf[b];
854*4882a593Smuzhiyun 			/* Change the current lcn to its new value. */
855*4882a593Smuzhiyun 			lcn += deltaxcn;
856*4882a593Smuzhiyun #ifdef DEBUG
857*4882a593Smuzhiyun 			/*
858*4882a593Smuzhiyun 			 * On NTFS 1.2-, apparently can have lcn == -1 to
859*4882a593Smuzhiyun 			 * indicate a hole. But we haven't verified ourselves
860*4882a593Smuzhiyun 			 * whether it is really the lcn or the deltaxcn that is
861*4882a593Smuzhiyun 			 * -1. So if either is found give us a message so we
862*4882a593Smuzhiyun 			 * can investigate it further!
863*4882a593Smuzhiyun 			 */
864*4882a593Smuzhiyun 			if (vol->major_ver < 3) {
865*4882a593Smuzhiyun 				if (unlikely(deltaxcn == (LCN)-1))
866*4882a593Smuzhiyun 					ntfs_error(vol->sb, "lcn delta == -1");
867*4882a593Smuzhiyun 				if (unlikely(lcn == (LCN)-1))
868*4882a593Smuzhiyun 					ntfs_error(vol->sb, "lcn == -1");
869*4882a593Smuzhiyun 			}
870*4882a593Smuzhiyun #endif
871*4882a593Smuzhiyun 			/* Check lcn is not below -1. */
872*4882a593Smuzhiyun 			if (unlikely(lcn < (LCN)-1)) {
873*4882a593Smuzhiyun 				ntfs_error(vol->sb, "Invalid LCN < -1 in "
874*4882a593Smuzhiyun 						"mapping pairs array.");
875*4882a593Smuzhiyun 				goto err_out;
876*4882a593Smuzhiyun 			}
877*4882a593Smuzhiyun 			/* Enter the current lcn into the runlist element. */
878*4882a593Smuzhiyun 			rl[rlpos].lcn = lcn;
879*4882a593Smuzhiyun 		}
880*4882a593Smuzhiyun 		/* Get to the next runlist element. */
881*4882a593Smuzhiyun 		rlpos++;
882*4882a593Smuzhiyun 		/* Increment the buffer position to the next mapping pair. */
883*4882a593Smuzhiyun 		buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 	if (unlikely(buf >= attr_end))
886*4882a593Smuzhiyun 		goto io_error;
887*4882a593Smuzhiyun 	/*
888*4882a593Smuzhiyun 	 * If there is a highest_vcn specified, it must be equal to the final
889*4882a593Smuzhiyun 	 * vcn in the runlist - 1, or something has gone badly wrong.
890*4882a593Smuzhiyun 	 */
891*4882a593Smuzhiyun 	deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
892*4882a593Smuzhiyun 	if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
893*4882a593Smuzhiyun mpa_err:
894*4882a593Smuzhiyun 		ntfs_error(vol->sb, "Corrupt mapping pairs array in "
895*4882a593Smuzhiyun 				"non-resident attribute.");
896*4882a593Smuzhiyun 		goto err_out;
897*4882a593Smuzhiyun 	}
898*4882a593Smuzhiyun 	/* Setup not mapped runlist element if this is the base extent. */
899*4882a593Smuzhiyun 	if (!attr->data.non_resident.lowest_vcn) {
900*4882a593Smuzhiyun 		VCN max_cluster;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		max_cluster = ((sle64_to_cpu(
903*4882a593Smuzhiyun 				attr->data.non_resident.allocated_size) +
904*4882a593Smuzhiyun 				vol->cluster_size - 1) >>
905*4882a593Smuzhiyun 				vol->cluster_size_bits) - 1;
906*4882a593Smuzhiyun 		/*
907*4882a593Smuzhiyun 		 * A highest_vcn of zero means this is a single extent
908*4882a593Smuzhiyun 		 * attribute so simply terminate the runlist with LCN_ENOENT).
909*4882a593Smuzhiyun 		 */
910*4882a593Smuzhiyun 		if (deltaxcn) {
911*4882a593Smuzhiyun 			/*
912*4882a593Smuzhiyun 			 * If there is a difference between the highest_vcn and
913*4882a593Smuzhiyun 			 * the highest cluster, the runlist is either corrupt
914*4882a593Smuzhiyun 			 * or, more likely, there are more extents following
915*4882a593Smuzhiyun 			 * this one.
916*4882a593Smuzhiyun 			 */
917*4882a593Smuzhiyun 			if (deltaxcn < max_cluster) {
918*4882a593Smuzhiyun 				ntfs_debug("More extents to follow; deltaxcn "
919*4882a593Smuzhiyun 						"= 0x%llx, max_cluster = "
920*4882a593Smuzhiyun 						"0x%llx",
921*4882a593Smuzhiyun 						(unsigned long long)deltaxcn,
922*4882a593Smuzhiyun 						(unsigned long long)
923*4882a593Smuzhiyun 						max_cluster);
924*4882a593Smuzhiyun 				rl[rlpos].vcn = vcn;
925*4882a593Smuzhiyun 				vcn += rl[rlpos].length = max_cluster -
926*4882a593Smuzhiyun 						deltaxcn;
927*4882a593Smuzhiyun 				rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
928*4882a593Smuzhiyun 				rlpos++;
929*4882a593Smuzhiyun 			} else if (unlikely(deltaxcn > max_cluster)) {
930*4882a593Smuzhiyun 				ntfs_error(vol->sb, "Corrupt attribute.  "
931*4882a593Smuzhiyun 						"deltaxcn = 0x%llx, "
932*4882a593Smuzhiyun 						"max_cluster = 0x%llx",
933*4882a593Smuzhiyun 						(unsigned long long)deltaxcn,
934*4882a593Smuzhiyun 						(unsigned long long)
935*4882a593Smuzhiyun 						max_cluster);
936*4882a593Smuzhiyun 				goto mpa_err;
937*4882a593Smuzhiyun 			}
938*4882a593Smuzhiyun 		}
939*4882a593Smuzhiyun 		rl[rlpos].lcn = LCN_ENOENT;
940*4882a593Smuzhiyun 	} else /* Not the base extent. There may be more extents to follow. */
941*4882a593Smuzhiyun 		rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	/* Setup terminating runlist element. */
944*4882a593Smuzhiyun 	rl[rlpos].vcn = vcn;
945*4882a593Smuzhiyun 	rl[rlpos].length = (s64)0;
946*4882a593Smuzhiyun 	/* If no existing runlist was specified, we are done. */
947*4882a593Smuzhiyun 	if (!old_rl) {
948*4882a593Smuzhiyun 		ntfs_debug("Mapping pairs array successfully decompressed:");
949*4882a593Smuzhiyun 		ntfs_debug_dump_runlist(rl);
950*4882a593Smuzhiyun 		return rl;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 	/* Now combine the new and old runlists checking for overlaps. */
953*4882a593Smuzhiyun 	old_rl = ntfs_runlists_merge(old_rl, rl);
954*4882a593Smuzhiyun 	if (!IS_ERR(old_rl))
955*4882a593Smuzhiyun 		return old_rl;
956*4882a593Smuzhiyun 	ntfs_free(rl);
957*4882a593Smuzhiyun 	ntfs_error(vol->sb, "Failed to merge runlists.");
958*4882a593Smuzhiyun 	return old_rl;
959*4882a593Smuzhiyun io_error:
960*4882a593Smuzhiyun 	ntfs_error(vol->sb, "Corrupt attribute.");
961*4882a593Smuzhiyun err_out:
962*4882a593Smuzhiyun 	ntfs_free(rl);
963*4882a593Smuzhiyun 	return ERR_PTR(-EIO);
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun /**
967*4882a593Smuzhiyun  * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
968*4882a593Smuzhiyun  * @rl:		runlist to use for conversion
969*4882a593Smuzhiyun  * @vcn:	vcn to convert
970*4882a593Smuzhiyun  *
971*4882a593Smuzhiyun  * Convert the virtual cluster number @vcn of an attribute into a logical
972*4882a593Smuzhiyun  * cluster number (lcn) of a device using the runlist @rl to map vcns to their
973*4882a593Smuzhiyun  * corresponding lcns.
974*4882a593Smuzhiyun  *
975*4882a593Smuzhiyun  * It is up to the caller to serialize access to the runlist @rl.
976*4882a593Smuzhiyun  *
977*4882a593Smuzhiyun  * Since lcns must be >= 0, we use negative return codes with special meaning:
978*4882a593Smuzhiyun  *
979*4882a593Smuzhiyun  * Return code		Meaning / Description
980*4882a593Smuzhiyun  * ==================================================
981*4882a593Smuzhiyun  *  LCN_HOLE		Hole / not allocated on disk.
982*4882a593Smuzhiyun  *  LCN_RL_NOT_MAPPED	This is part of the runlist which has not been
983*4882a593Smuzhiyun  *			inserted into the runlist yet.
984*4882a593Smuzhiyun  *  LCN_ENOENT		There is no such vcn in the attribute.
985*4882a593Smuzhiyun  *
986*4882a593Smuzhiyun  * Locking: - The caller must have locked the runlist (for reading or writing).
987*4882a593Smuzhiyun  *	    - This function does not touch the lock, nor does it modify the
988*4882a593Smuzhiyun  *	      runlist.
989*4882a593Smuzhiyun  */
ntfs_rl_vcn_to_lcn(const runlist_element * rl,const VCN vcn)990*4882a593Smuzhiyun LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun 	int i;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	BUG_ON(vcn < 0);
995*4882a593Smuzhiyun 	/*
996*4882a593Smuzhiyun 	 * If rl is NULL, assume that we have found an unmapped runlist. The
997*4882a593Smuzhiyun 	 * caller can then attempt to map it and fail appropriately if
998*4882a593Smuzhiyun 	 * necessary.
999*4882a593Smuzhiyun 	 */
1000*4882a593Smuzhiyun 	if (unlikely(!rl))
1001*4882a593Smuzhiyun 		return LCN_RL_NOT_MAPPED;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	/* Catch out of lower bounds vcn. */
1004*4882a593Smuzhiyun 	if (unlikely(vcn < rl[0].vcn))
1005*4882a593Smuzhiyun 		return LCN_ENOENT;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	for (i = 0; likely(rl[i].length); i++) {
1008*4882a593Smuzhiyun 		if (unlikely(vcn < rl[i+1].vcn)) {
1009*4882a593Smuzhiyun 			if (likely(rl[i].lcn >= (LCN)0))
1010*4882a593Smuzhiyun 				return rl[i].lcn + (vcn - rl[i].vcn);
1011*4882a593Smuzhiyun 			return rl[i].lcn;
1012*4882a593Smuzhiyun 		}
1013*4882a593Smuzhiyun 	}
1014*4882a593Smuzhiyun 	/*
1015*4882a593Smuzhiyun 	 * The terminator element is setup to the correct value, i.e. one of
1016*4882a593Smuzhiyun 	 * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
1017*4882a593Smuzhiyun 	 */
1018*4882a593Smuzhiyun 	if (likely(rl[i].lcn < (LCN)0))
1019*4882a593Smuzhiyun 		return rl[i].lcn;
1020*4882a593Smuzhiyun 	/* Just in case... We could replace this with BUG() some day. */
1021*4882a593Smuzhiyun 	return LCN_ENOENT;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun #ifdef NTFS_RW
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun /**
1027*4882a593Smuzhiyun  * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
1028*4882a593Smuzhiyun  * @rl:		runlist to search
1029*4882a593Smuzhiyun  * @vcn:	vcn to find
1030*4882a593Smuzhiyun  *
1031*4882a593Smuzhiyun  * Find the virtual cluster number @vcn in the runlist @rl and return the
1032*4882a593Smuzhiyun  * address of the runlist element containing the @vcn on success.
1033*4882a593Smuzhiyun  *
1034*4882a593Smuzhiyun  * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
1035*4882a593Smuzhiyun  * the runlist.
1036*4882a593Smuzhiyun  *
1037*4882a593Smuzhiyun  * Locking: The runlist must be locked on entry.
1038*4882a593Smuzhiyun  */
ntfs_rl_find_vcn_nolock(runlist_element * rl,const VCN vcn)1039*4882a593Smuzhiyun runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	BUG_ON(vcn < 0);
1042*4882a593Smuzhiyun 	if (unlikely(!rl || vcn < rl[0].vcn))
1043*4882a593Smuzhiyun 		return NULL;
1044*4882a593Smuzhiyun 	while (likely(rl->length)) {
1045*4882a593Smuzhiyun 		if (unlikely(vcn < rl[1].vcn)) {
1046*4882a593Smuzhiyun 			if (likely(rl->lcn >= LCN_HOLE))
1047*4882a593Smuzhiyun 				return rl;
1048*4882a593Smuzhiyun 			return NULL;
1049*4882a593Smuzhiyun 		}
1050*4882a593Smuzhiyun 		rl++;
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 	if (likely(rl->lcn == LCN_ENOENT))
1053*4882a593Smuzhiyun 		return rl;
1054*4882a593Smuzhiyun 	return NULL;
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun /**
1058*4882a593Smuzhiyun  * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
1059*4882a593Smuzhiyun  * @n:		number for which to get the number of bytes for
1060*4882a593Smuzhiyun  *
1061*4882a593Smuzhiyun  * Return the number of bytes required to store @n unambiguously as
1062*4882a593Smuzhiyun  * a signed number.
1063*4882a593Smuzhiyun  *
1064*4882a593Smuzhiyun  * This is used in the context of the mapping pairs array to determine how
1065*4882a593Smuzhiyun  * many bytes will be needed in the array to store a given logical cluster
1066*4882a593Smuzhiyun  * number (lcn) or a specific run length.
1067*4882a593Smuzhiyun  *
1068*4882a593Smuzhiyun  * Return the number of bytes written.  This function cannot fail.
1069*4882a593Smuzhiyun  */
ntfs_get_nr_significant_bytes(const s64 n)1070*4882a593Smuzhiyun static inline int ntfs_get_nr_significant_bytes(const s64 n)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun 	s64 l = n;
1073*4882a593Smuzhiyun 	int i;
1074*4882a593Smuzhiyun 	s8 j;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	i = 0;
1077*4882a593Smuzhiyun 	do {
1078*4882a593Smuzhiyun 		l >>= 8;
1079*4882a593Smuzhiyun 		i++;
1080*4882a593Smuzhiyun 	} while (l != 0 && l != -1);
1081*4882a593Smuzhiyun 	j = (n >> 8 * (i - 1)) & 0xff;
1082*4882a593Smuzhiyun 	/* If the sign bit is wrong, we need an extra byte. */
1083*4882a593Smuzhiyun 	if ((n < 0 && j >= 0) || (n > 0 && j < 0))
1084*4882a593Smuzhiyun 		i++;
1085*4882a593Smuzhiyun 	return i;
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun /**
1089*4882a593Smuzhiyun  * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
1090*4882a593Smuzhiyun  * @vol:	ntfs volume (needed for the ntfs version)
1091*4882a593Smuzhiyun  * @rl:		locked runlist to determine the size of the mapping pairs of
1092*4882a593Smuzhiyun  * @first_vcn:	first vcn which to include in the mapping pairs array
1093*4882a593Smuzhiyun  * @last_vcn:	last vcn which to include in the mapping pairs array
1094*4882a593Smuzhiyun  *
1095*4882a593Smuzhiyun  * Walk the locked runlist @rl and calculate the size in bytes of the mapping
1096*4882a593Smuzhiyun  * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
1097*4882a593Smuzhiyun  * finishing with vcn @last_vcn.
1098*4882a593Smuzhiyun  *
1099*4882a593Smuzhiyun  * A @last_vcn of -1 means end of runlist and in that case the size of the
1100*4882a593Smuzhiyun  * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
1101*4882a593Smuzhiyun  * and finishing at the end of the runlist is determined.
1102*4882a593Smuzhiyun  *
1103*4882a593Smuzhiyun  * This for example allows us to allocate a buffer of the right size when
1104*4882a593Smuzhiyun  * building the mapping pairs array.
1105*4882a593Smuzhiyun  *
1106*4882a593Smuzhiyun  * If @rl is NULL, just return 1 (for the single terminator byte).
1107*4882a593Smuzhiyun  *
1108*4882a593Smuzhiyun  * Return the calculated size in bytes on success.  On error, return -errno.
1109*4882a593Smuzhiyun  * The following error codes are defined:
1110*4882a593Smuzhiyun  *	-EINVAL	- Run list contains unmapped elements.  Make sure to only pass
1111*4882a593Smuzhiyun  *		  fully mapped runlists to this function.
1112*4882a593Smuzhiyun  *	-EIO	- The runlist is corrupt.
1113*4882a593Smuzhiyun  *
1114*4882a593Smuzhiyun  * Locking: @rl must be locked on entry (either for reading or writing), it
1115*4882a593Smuzhiyun  *	    remains locked throughout, and is left locked upon return.
1116*4882a593Smuzhiyun  */
ntfs_get_size_for_mapping_pairs(const ntfs_volume * vol,const runlist_element * rl,const VCN first_vcn,const VCN last_vcn)1117*4882a593Smuzhiyun int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1118*4882a593Smuzhiyun 		const runlist_element *rl, const VCN first_vcn,
1119*4882a593Smuzhiyun 		const VCN last_vcn)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	LCN prev_lcn;
1122*4882a593Smuzhiyun 	int rls;
1123*4882a593Smuzhiyun 	bool the_end = false;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	BUG_ON(first_vcn < 0);
1126*4882a593Smuzhiyun 	BUG_ON(last_vcn < -1);
1127*4882a593Smuzhiyun 	BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1128*4882a593Smuzhiyun 	if (!rl) {
1129*4882a593Smuzhiyun 		BUG_ON(first_vcn);
1130*4882a593Smuzhiyun 		BUG_ON(last_vcn > 0);
1131*4882a593Smuzhiyun 		return 1;
1132*4882a593Smuzhiyun 	}
1133*4882a593Smuzhiyun 	/* Skip to runlist element containing @first_vcn. */
1134*4882a593Smuzhiyun 	while (rl->length && first_vcn >= rl[1].vcn)
1135*4882a593Smuzhiyun 		rl++;
1136*4882a593Smuzhiyun 	if (unlikely((!rl->length && first_vcn > rl->vcn) ||
1137*4882a593Smuzhiyun 			first_vcn < rl->vcn))
1138*4882a593Smuzhiyun 		return -EINVAL;
1139*4882a593Smuzhiyun 	prev_lcn = 0;
1140*4882a593Smuzhiyun 	/* Always need the termining zero byte. */
1141*4882a593Smuzhiyun 	rls = 1;
1142*4882a593Smuzhiyun 	/* Do the first partial run if present. */
1143*4882a593Smuzhiyun 	if (first_vcn > rl->vcn) {
1144*4882a593Smuzhiyun 		s64 delta, length = rl->length;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 		/* We know rl->length != 0 already. */
1147*4882a593Smuzhiyun 		if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1148*4882a593Smuzhiyun 			goto err_out;
1149*4882a593Smuzhiyun 		/*
1150*4882a593Smuzhiyun 		 * If @stop_vcn is given and finishes inside this run, cap the
1151*4882a593Smuzhiyun 		 * run length.
1152*4882a593Smuzhiyun 		 */
1153*4882a593Smuzhiyun 		if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1154*4882a593Smuzhiyun 			s64 s1 = last_vcn + 1;
1155*4882a593Smuzhiyun 			if (unlikely(rl[1].vcn > s1))
1156*4882a593Smuzhiyun 				length = s1 - rl->vcn;
1157*4882a593Smuzhiyun 			the_end = true;
1158*4882a593Smuzhiyun 		}
1159*4882a593Smuzhiyun 		delta = first_vcn - rl->vcn;
1160*4882a593Smuzhiyun 		/* Header byte + length. */
1161*4882a593Smuzhiyun 		rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
1162*4882a593Smuzhiyun 		/*
1163*4882a593Smuzhiyun 		 * If the logical cluster number (lcn) denotes a hole and we
1164*4882a593Smuzhiyun 		 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1165*4882a593Smuzhiyun 		 * zero space.  On earlier NTFS versions we just store the lcn.
1166*4882a593Smuzhiyun 		 * Note: this assumes that on NTFS 1.2-, holes are stored with
1167*4882a593Smuzhiyun 		 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1168*4882a593Smuzhiyun 		 */
1169*4882a593Smuzhiyun 		if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1170*4882a593Smuzhiyun 			prev_lcn = rl->lcn;
1171*4882a593Smuzhiyun 			if (likely(rl->lcn >= 0))
1172*4882a593Smuzhiyun 				prev_lcn += delta;
1173*4882a593Smuzhiyun 			/* Change in lcn. */
1174*4882a593Smuzhiyun 			rls += ntfs_get_nr_significant_bytes(prev_lcn);
1175*4882a593Smuzhiyun 		}
1176*4882a593Smuzhiyun 		/* Go to next runlist element. */
1177*4882a593Smuzhiyun 		rl++;
1178*4882a593Smuzhiyun 	}
1179*4882a593Smuzhiyun 	/* Do the full runs. */
1180*4882a593Smuzhiyun 	for (; rl->length && !the_end; rl++) {
1181*4882a593Smuzhiyun 		s64 length = rl->length;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 		if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1184*4882a593Smuzhiyun 			goto err_out;
1185*4882a593Smuzhiyun 		/*
1186*4882a593Smuzhiyun 		 * If @stop_vcn is given and finishes inside this run, cap the
1187*4882a593Smuzhiyun 		 * run length.
1188*4882a593Smuzhiyun 		 */
1189*4882a593Smuzhiyun 		if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1190*4882a593Smuzhiyun 			s64 s1 = last_vcn + 1;
1191*4882a593Smuzhiyun 			if (unlikely(rl[1].vcn > s1))
1192*4882a593Smuzhiyun 				length = s1 - rl->vcn;
1193*4882a593Smuzhiyun 			the_end = true;
1194*4882a593Smuzhiyun 		}
1195*4882a593Smuzhiyun 		/* Header byte + length. */
1196*4882a593Smuzhiyun 		rls += 1 + ntfs_get_nr_significant_bytes(length);
1197*4882a593Smuzhiyun 		/*
1198*4882a593Smuzhiyun 		 * If the logical cluster number (lcn) denotes a hole and we
1199*4882a593Smuzhiyun 		 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1200*4882a593Smuzhiyun 		 * zero space.  On earlier NTFS versions we just store the lcn.
1201*4882a593Smuzhiyun 		 * Note: this assumes that on NTFS 1.2-, holes are stored with
1202*4882a593Smuzhiyun 		 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1203*4882a593Smuzhiyun 		 */
1204*4882a593Smuzhiyun 		if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1205*4882a593Smuzhiyun 			/* Change in lcn. */
1206*4882a593Smuzhiyun 			rls += ntfs_get_nr_significant_bytes(rl->lcn -
1207*4882a593Smuzhiyun 					prev_lcn);
1208*4882a593Smuzhiyun 			prev_lcn = rl->lcn;
1209*4882a593Smuzhiyun 		}
1210*4882a593Smuzhiyun 	}
1211*4882a593Smuzhiyun 	return rls;
1212*4882a593Smuzhiyun err_out:
1213*4882a593Smuzhiyun 	if (rl->lcn == LCN_RL_NOT_MAPPED)
1214*4882a593Smuzhiyun 		rls = -EINVAL;
1215*4882a593Smuzhiyun 	else
1216*4882a593Smuzhiyun 		rls = -EIO;
1217*4882a593Smuzhiyun 	return rls;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun /**
1221*4882a593Smuzhiyun  * ntfs_write_significant_bytes - write the significant bytes of a number
1222*4882a593Smuzhiyun  * @dst:	destination buffer to write to
1223*4882a593Smuzhiyun  * @dst_max:	pointer to last byte of destination buffer for bounds checking
1224*4882a593Smuzhiyun  * @n:		number whose significant bytes to write
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * Store in @dst, the minimum bytes of the number @n which are required to
1227*4882a593Smuzhiyun  * identify @n unambiguously as a signed number, taking care not to exceed
1228*4882a593Smuzhiyun  * @dest_max, the maximum position within @dst to which we are allowed to
1229*4882a593Smuzhiyun  * write.
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  * This is used when building the mapping pairs array of a runlist to compress
1232*4882a593Smuzhiyun  * a given logical cluster number (lcn) or a specific run length to the minimum
1233*4882a593Smuzhiyun  * size possible.
1234*4882a593Smuzhiyun  *
1235*4882a593Smuzhiyun  * Return the number of bytes written on success.  On error, i.e. the
1236*4882a593Smuzhiyun  * destination buffer @dst is too small, return -ENOSPC.
1237*4882a593Smuzhiyun  */
ntfs_write_significant_bytes(s8 * dst,const s8 * dst_max,const s64 n)1238*4882a593Smuzhiyun static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
1239*4882a593Smuzhiyun 		const s64 n)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	s64 l = n;
1242*4882a593Smuzhiyun 	int i;
1243*4882a593Smuzhiyun 	s8 j;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	i = 0;
1246*4882a593Smuzhiyun 	do {
1247*4882a593Smuzhiyun 		if (unlikely(dst > dst_max))
1248*4882a593Smuzhiyun 			goto err_out;
1249*4882a593Smuzhiyun 		*dst++ = l & 0xffll;
1250*4882a593Smuzhiyun 		l >>= 8;
1251*4882a593Smuzhiyun 		i++;
1252*4882a593Smuzhiyun 	} while (l != 0 && l != -1);
1253*4882a593Smuzhiyun 	j = (n >> 8 * (i - 1)) & 0xff;
1254*4882a593Smuzhiyun 	/* If the sign bit is wrong, we need an extra byte. */
1255*4882a593Smuzhiyun 	if (n < 0 && j >= 0) {
1256*4882a593Smuzhiyun 		if (unlikely(dst > dst_max))
1257*4882a593Smuzhiyun 			goto err_out;
1258*4882a593Smuzhiyun 		i++;
1259*4882a593Smuzhiyun 		*dst = (s8)-1;
1260*4882a593Smuzhiyun 	} else if (n > 0 && j < 0) {
1261*4882a593Smuzhiyun 		if (unlikely(dst > dst_max))
1262*4882a593Smuzhiyun 			goto err_out;
1263*4882a593Smuzhiyun 		i++;
1264*4882a593Smuzhiyun 		*dst = (s8)0;
1265*4882a593Smuzhiyun 	}
1266*4882a593Smuzhiyun 	return i;
1267*4882a593Smuzhiyun err_out:
1268*4882a593Smuzhiyun 	return -ENOSPC;
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun /**
1272*4882a593Smuzhiyun  * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
1273*4882a593Smuzhiyun  * @vol:	ntfs volume (needed for the ntfs version)
1274*4882a593Smuzhiyun  * @dst:	destination buffer to which to write the mapping pairs array
1275*4882a593Smuzhiyun  * @dst_len:	size of destination buffer @dst in bytes
1276*4882a593Smuzhiyun  * @rl:		locked runlist for which to build the mapping pairs array
1277*4882a593Smuzhiyun  * @first_vcn:	first vcn which to include in the mapping pairs array
1278*4882a593Smuzhiyun  * @last_vcn:	last vcn which to include in the mapping pairs array
1279*4882a593Smuzhiyun  * @stop_vcn:	first vcn outside destination buffer on success or -ENOSPC
1280*4882a593Smuzhiyun  *
1281*4882a593Smuzhiyun  * Create the mapping pairs array from the locked runlist @rl, starting at vcn
1282*4882a593Smuzhiyun  * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
1283*4882a593Smuzhiyun  * @dst_len is the size of @dst in bytes and it should be at least equal to the
1284*4882a593Smuzhiyun  * value obtained by calling ntfs_get_size_for_mapping_pairs().
1285*4882a593Smuzhiyun  *
1286*4882a593Smuzhiyun  * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
1287*4882a593Smuzhiyun  * array corresponding to the runlist starting at vcn @first_vcn and finishing
1288*4882a593Smuzhiyun  * at the end of the runlist is created.
1289*4882a593Smuzhiyun  *
1290*4882a593Smuzhiyun  * If @rl is NULL, just write a single terminator byte to @dst.
1291*4882a593Smuzhiyun  *
1292*4882a593Smuzhiyun  * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
1293*4882a593Smuzhiyun  * the first vcn outside the destination buffer.  Note that on error, @dst has
1294*4882a593Smuzhiyun  * been filled with all the mapping pairs that will fit, thus it can be treated
1295*4882a593Smuzhiyun  * as partial success, in that a new attribute extent needs to be created or
1296*4882a593Smuzhiyun  * the next extent has to be used and the mapping pairs build has to be
1297*4882a593Smuzhiyun  * continued with @first_vcn set to *@stop_vcn.
1298*4882a593Smuzhiyun  *
1299*4882a593Smuzhiyun  * Return 0 on success and -errno on error.  The following error codes are
1300*4882a593Smuzhiyun  * defined:
1301*4882a593Smuzhiyun  *	-EINVAL	- Run list contains unmapped elements.  Make sure to only pass
1302*4882a593Smuzhiyun  *		  fully mapped runlists to this function.
1303*4882a593Smuzhiyun  *	-EIO	- The runlist is corrupt.
1304*4882a593Smuzhiyun  *	-ENOSPC	- The destination buffer is too small.
1305*4882a593Smuzhiyun  *
1306*4882a593Smuzhiyun  * Locking: @rl must be locked on entry (either for reading or writing), it
1307*4882a593Smuzhiyun  *	    remains locked throughout, and is left locked upon return.
1308*4882a593Smuzhiyun  */
ntfs_mapping_pairs_build(const ntfs_volume * vol,s8 * dst,const int dst_len,const runlist_element * rl,const VCN first_vcn,const VCN last_vcn,VCN * const stop_vcn)1309*4882a593Smuzhiyun int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1310*4882a593Smuzhiyun 		const int dst_len, const runlist_element *rl,
1311*4882a593Smuzhiyun 		const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	LCN prev_lcn;
1314*4882a593Smuzhiyun 	s8 *dst_max, *dst_next;
1315*4882a593Smuzhiyun 	int err = -ENOSPC;
1316*4882a593Smuzhiyun 	bool the_end = false;
1317*4882a593Smuzhiyun 	s8 len_len, lcn_len;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	BUG_ON(first_vcn < 0);
1320*4882a593Smuzhiyun 	BUG_ON(last_vcn < -1);
1321*4882a593Smuzhiyun 	BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
1322*4882a593Smuzhiyun 	BUG_ON(dst_len < 1);
1323*4882a593Smuzhiyun 	if (!rl) {
1324*4882a593Smuzhiyun 		BUG_ON(first_vcn);
1325*4882a593Smuzhiyun 		BUG_ON(last_vcn > 0);
1326*4882a593Smuzhiyun 		if (stop_vcn)
1327*4882a593Smuzhiyun 			*stop_vcn = 0;
1328*4882a593Smuzhiyun 		/* Terminator byte. */
1329*4882a593Smuzhiyun 		*dst = 0;
1330*4882a593Smuzhiyun 		return 0;
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 	/* Skip to runlist element containing @first_vcn. */
1333*4882a593Smuzhiyun 	while (rl->length && first_vcn >= rl[1].vcn)
1334*4882a593Smuzhiyun 		rl++;
1335*4882a593Smuzhiyun 	if (unlikely((!rl->length && first_vcn > rl->vcn) ||
1336*4882a593Smuzhiyun 			first_vcn < rl->vcn))
1337*4882a593Smuzhiyun 		return -EINVAL;
1338*4882a593Smuzhiyun 	/*
1339*4882a593Smuzhiyun 	 * @dst_max is used for bounds checking in
1340*4882a593Smuzhiyun 	 * ntfs_write_significant_bytes().
1341*4882a593Smuzhiyun 	 */
1342*4882a593Smuzhiyun 	dst_max = dst + dst_len - 1;
1343*4882a593Smuzhiyun 	prev_lcn = 0;
1344*4882a593Smuzhiyun 	/* Do the first partial run if present. */
1345*4882a593Smuzhiyun 	if (first_vcn > rl->vcn) {
1346*4882a593Smuzhiyun 		s64 delta, length = rl->length;
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 		/* We know rl->length != 0 already. */
1349*4882a593Smuzhiyun 		if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1350*4882a593Smuzhiyun 			goto err_out;
1351*4882a593Smuzhiyun 		/*
1352*4882a593Smuzhiyun 		 * If @stop_vcn is given and finishes inside this run, cap the
1353*4882a593Smuzhiyun 		 * run length.
1354*4882a593Smuzhiyun 		 */
1355*4882a593Smuzhiyun 		if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1356*4882a593Smuzhiyun 			s64 s1 = last_vcn + 1;
1357*4882a593Smuzhiyun 			if (unlikely(rl[1].vcn > s1))
1358*4882a593Smuzhiyun 				length = s1 - rl->vcn;
1359*4882a593Smuzhiyun 			the_end = true;
1360*4882a593Smuzhiyun 		}
1361*4882a593Smuzhiyun 		delta = first_vcn - rl->vcn;
1362*4882a593Smuzhiyun 		/* Write length. */
1363*4882a593Smuzhiyun 		len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1364*4882a593Smuzhiyun 				length - delta);
1365*4882a593Smuzhiyun 		if (unlikely(len_len < 0))
1366*4882a593Smuzhiyun 			goto size_err;
1367*4882a593Smuzhiyun 		/*
1368*4882a593Smuzhiyun 		 * If the logical cluster number (lcn) denotes a hole and we
1369*4882a593Smuzhiyun 		 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1370*4882a593Smuzhiyun 		 * zero space.  On earlier NTFS versions we just write the lcn
1371*4882a593Smuzhiyun 		 * change.  FIXME: Do we need to write the lcn change or just
1372*4882a593Smuzhiyun 		 * the lcn in that case?  Not sure as I have never seen this
1373*4882a593Smuzhiyun 		 * case on NT4. - We assume that we just need to write the lcn
1374*4882a593Smuzhiyun 		 * change until someone tells us otherwise... (AIA)
1375*4882a593Smuzhiyun 		 */
1376*4882a593Smuzhiyun 		if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1377*4882a593Smuzhiyun 			prev_lcn = rl->lcn;
1378*4882a593Smuzhiyun 			if (likely(rl->lcn >= 0))
1379*4882a593Smuzhiyun 				prev_lcn += delta;
1380*4882a593Smuzhiyun 			/* Write change in lcn. */
1381*4882a593Smuzhiyun 			lcn_len = ntfs_write_significant_bytes(dst + 1 +
1382*4882a593Smuzhiyun 					len_len, dst_max, prev_lcn);
1383*4882a593Smuzhiyun 			if (unlikely(lcn_len < 0))
1384*4882a593Smuzhiyun 				goto size_err;
1385*4882a593Smuzhiyun 		} else
1386*4882a593Smuzhiyun 			lcn_len = 0;
1387*4882a593Smuzhiyun 		dst_next = dst + len_len + lcn_len + 1;
1388*4882a593Smuzhiyun 		if (unlikely(dst_next > dst_max))
1389*4882a593Smuzhiyun 			goto size_err;
1390*4882a593Smuzhiyun 		/* Update header byte. */
1391*4882a593Smuzhiyun 		*dst = lcn_len << 4 | len_len;
1392*4882a593Smuzhiyun 		/* Position at next mapping pairs array element. */
1393*4882a593Smuzhiyun 		dst = dst_next;
1394*4882a593Smuzhiyun 		/* Go to next runlist element. */
1395*4882a593Smuzhiyun 		rl++;
1396*4882a593Smuzhiyun 	}
1397*4882a593Smuzhiyun 	/* Do the full runs. */
1398*4882a593Smuzhiyun 	for (; rl->length && !the_end; rl++) {
1399*4882a593Smuzhiyun 		s64 length = rl->length;
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 		if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
1402*4882a593Smuzhiyun 			goto err_out;
1403*4882a593Smuzhiyun 		/*
1404*4882a593Smuzhiyun 		 * If @stop_vcn is given and finishes inside this run, cap the
1405*4882a593Smuzhiyun 		 * run length.
1406*4882a593Smuzhiyun 		 */
1407*4882a593Smuzhiyun 		if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
1408*4882a593Smuzhiyun 			s64 s1 = last_vcn + 1;
1409*4882a593Smuzhiyun 			if (unlikely(rl[1].vcn > s1))
1410*4882a593Smuzhiyun 				length = s1 - rl->vcn;
1411*4882a593Smuzhiyun 			the_end = true;
1412*4882a593Smuzhiyun 		}
1413*4882a593Smuzhiyun 		/* Write length. */
1414*4882a593Smuzhiyun 		len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1415*4882a593Smuzhiyun 				length);
1416*4882a593Smuzhiyun 		if (unlikely(len_len < 0))
1417*4882a593Smuzhiyun 			goto size_err;
1418*4882a593Smuzhiyun 		/*
1419*4882a593Smuzhiyun 		 * If the logical cluster number (lcn) denotes a hole and we
1420*4882a593Smuzhiyun 		 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1421*4882a593Smuzhiyun 		 * zero space.  On earlier NTFS versions we just write the lcn
1422*4882a593Smuzhiyun 		 * change.  FIXME: Do we need to write the lcn change or just
1423*4882a593Smuzhiyun 		 * the lcn in that case?  Not sure as I have never seen this
1424*4882a593Smuzhiyun 		 * case on NT4. - We assume that we just need to write the lcn
1425*4882a593Smuzhiyun 		 * change until someone tells us otherwise... (AIA)
1426*4882a593Smuzhiyun 		 */
1427*4882a593Smuzhiyun 		if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
1428*4882a593Smuzhiyun 			/* Write change in lcn. */
1429*4882a593Smuzhiyun 			lcn_len = ntfs_write_significant_bytes(dst + 1 +
1430*4882a593Smuzhiyun 					len_len, dst_max, rl->lcn - prev_lcn);
1431*4882a593Smuzhiyun 			if (unlikely(lcn_len < 0))
1432*4882a593Smuzhiyun 				goto size_err;
1433*4882a593Smuzhiyun 			prev_lcn = rl->lcn;
1434*4882a593Smuzhiyun 		} else
1435*4882a593Smuzhiyun 			lcn_len = 0;
1436*4882a593Smuzhiyun 		dst_next = dst + len_len + lcn_len + 1;
1437*4882a593Smuzhiyun 		if (unlikely(dst_next > dst_max))
1438*4882a593Smuzhiyun 			goto size_err;
1439*4882a593Smuzhiyun 		/* Update header byte. */
1440*4882a593Smuzhiyun 		*dst = lcn_len << 4 | len_len;
1441*4882a593Smuzhiyun 		/* Position at next mapping pairs array element. */
1442*4882a593Smuzhiyun 		dst = dst_next;
1443*4882a593Smuzhiyun 	}
1444*4882a593Smuzhiyun 	/* Success. */
1445*4882a593Smuzhiyun 	err = 0;
1446*4882a593Smuzhiyun size_err:
1447*4882a593Smuzhiyun 	/* Set stop vcn. */
1448*4882a593Smuzhiyun 	if (stop_vcn)
1449*4882a593Smuzhiyun 		*stop_vcn = rl->vcn;
1450*4882a593Smuzhiyun 	/* Add terminator byte. */
1451*4882a593Smuzhiyun 	*dst = 0;
1452*4882a593Smuzhiyun 	return err;
1453*4882a593Smuzhiyun err_out:
1454*4882a593Smuzhiyun 	if (rl->lcn == LCN_RL_NOT_MAPPED)
1455*4882a593Smuzhiyun 		err = -EINVAL;
1456*4882a593Smuzhiyun 	else
1457*4882a593Smuzhiyun 		err = -EIO;
1458*4882a593Smuzhiyun 	return err;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun /**
1462*4882a593Smuzhiyun  * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
1463*4882a593Smuzhiyun  * @vol:	ntfs volume (needed for error output)
1464*4882a593Smuzhiyun  * @runlist:	runlist to truncate
1465*4882a593Smuzhiyun  * @new_length:	the new length of the runlist in VCNs
1466*4882a593Smuzhiyun  *
1467*4882a593Smuzhiyun  * Truncate the runlist described by @runlist as well as the memory buffer
1468*4882a593Smuzhiyun  * holding the runlist elements to a length of @new_length VCNs.
1469*4882a593Smuzhiyun  *
1470*4882a593Smuzhiyun  * If @new_length lies within the runlist, the runlist elements with VCNs of
1471*4882a593Smuzhiyun  * @new_length and above are discarded.  As a special case if @new_length is
1472*4882a593Smuzhiyun  * zero, the runlist is discarded and set to NULL.
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * If @new_length lies beyond the runlist, a sparse runlist element is added to
1475*4882a593Smuzhiyun  * the end of the runlist @runlist or if the last runlist element is a sparse
1476*4882a593Smuzhiyun  * one already, this is extended.
1477*4882a593Smuzhiyun  *
1478*4882a593Smuzhiyun  * Note, no checking is done for unmapped runlist elements.  It is assumed that
1479*4882a593Smuzhiyun  * the caller has mapped any elements that need to be mapped already.
1480*4882a593Smuzhiyun  *
1481*4882a593Smuzhiyun  * Return 0 on success and -errno on error.
1482*4882a593Smuzhiyun  *
1483*4882a593Smuzhiyun  * Locking: The caller must hold @runlist->lock for writing.
1484*4882a593Smuzhiyun  */
ntfs_rl_truncate_nolock(const ntfs_volume * vol,runlist * const runlist,const s64 new_length)1485*4882a593Smuzhiyun int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
1486*4882a593Smuzhiyun 		const s64 new_length)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun 	runlist_element *rl;
1489*4882a593Smuzhiyun 	int old_size;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
1492*4882a593Smuzhiyun 	BUG_ON(!runlist);
1493*4882a593Smuzhiyun 	BUG_ON(new_length < 0);
1494*4882a593Smuzhiyun 	rl = runlist->rl;
1495*4882a593Smuzhiyun 	if (!new_length) {
1496*4882a593Smuzhiyun 		ntfs_debug("Freeing runlist.");
1497*4882a593Smuzhiyun 		runlist->rl = NULL;
1498*4882a593Smuzhiyun 		if (rl)
1499*4882a593Smuzhiyun 			ntfs_free(rl);
1500*4882a593Smuzhiyun 		return 0;
1501*4882a593Smuzhiyun 	}
1502*4882a593Smuzhiyun 	if (unlikely(!rl)) {
1503*4882a593Smuzhiyun 		/*
1504*4882a593Smuzhiyun 		 * Create a runlist consisting of a sparse runlist element of
1505*4882a593Smuzhiyun 		 * length @new_length followed by a terminator runlist element.
1506*4882a593Smuzhiyun 		 */
1507*4882a593Smuzhiyun 		rl = ntfs_malloc_nofs(PAGE_SIZE);
1508*4882a593Smuzhiyun 		if (unlikely(!rl)) {
1509*4882a593Smuzhiyun 			ntfs_error(vol->sb, "Not enough memory to allocate "
1510*4882a593Smuzhiyun 					"runlist element buffer.");
1511*4882a593Smuzhiyun 			return -ENOMEM;
1512*4882a593Smuzhiyun 		}
1513*4882a593Smuzhiyun 		runlist->rl = rl;
1514*4882a593Smuzhiyun 		rl[1].length = rl->vcn = 0;
1515*4882a593Smuzhiyun 		rl->lcn = LCN_HOLE;
1516*4882a593Smuzhiyun 		rl[1].vcn = rl->length = new_length;
1517*4882a593Smuzhiyun 		rl[1].lcn = LCN_ENOENT;
1518*4882a593Smuzhiyun 		return 0;
1519*4882a593Smuzhiyun 	}
1520*4882a593Smuzhiyun 	BUG_ON(new_length < rl->vcn);
1521*4882a593Smuzhiyun 	/* Find @new_length in the runlist. */
1522*4882a593Smuzhiyun 	while (likely(rl->length && new_length >= rl[1].vcn))
1523*4882a593Smuzhiyun 		rl++;
1524*4882a593Smuzhiyun 	/*
1525*4882a593Smuzhiyun 	 * If not at the end of the runlist we need to shrink it.
1526*4882a593Smuzhiyun 	 * If at the end of the runlist we need to expand it.
1527*4882a593Smuzhiyun 	 */
1528*4882a593Smuzhiyun 	if (rl->length) {
1529*4882a593Smuzhiyun 		runlist_element *trl;
1530*4882a593Smuzhiyun 		bool is_end;
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 		ntfs_debug("Shrinking runlist.");
1533*4882a593Smuzhiyun 		/* Determine the runlist size. */
1534*4882a593Smuzhiyun 		trl = rl + 1;
1535*4882a593Smuzhiyun 		while (likely(trl->length))
1536*4882a593Smuzhiyun 			trl++;
1537*4882a593Smuzhiyun 		old_size = trl - runlist->rl + 1;
1538*4882a593Smuzhiyun 		/* Truncate the run. */
1539*4882a593Smuzhiyun 		rl->length = new_length - rl->vcn;
1540*4882a593Smuzhiyun 		/*
1541*4882a593Smuzhiyun 		 * If a run was partially truncated, make the following runlist
1542*4882a593Smuzhiyun 		 * element a terminator.
1543*4882a593Smuzhiyun 		 */
1544*4882a593Smuzhiyun 		is_end = false;
1545*4882a593Smuzhiyun 		if (rl->length) {
1546*4882a593Smuzhiyun 			rl++;
1547*4882a593Smuzhiyun 			if (!rl->length)
1548*4882a593Smuzhiyun 				is_end = true;
1549*4882a593Smuzhiyun 			rl->vcn = new_length;
1550*4882a593Smuzhiyun 			rl->length = 0;
1551*4882a593Smuzhiyun 		}
1552*4882a593Smuzhiyun 		rl->lcn = LCN_ENOENT;
1553*4882a593Smuzhiyun 		/* Reallocate memory if necessary. */
1554*4882a593Smuzhiyun 		if (!is_end) {
1555*4882a593Smuzhiyun 			int new_size = rl - runlist->rl + 1;
1556*4882a593Smuzhiyun 			rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
1557*4882a593Smuzhiyun 			if (IS_ERR(rl))
1558*4882a593Smuzhiyun 				ntfs_warning(vol->sb, "Failed to shrink "
1559*4882a593Smuzhiyun 						"runlist buffer.  This just "
1560*4882a593Smuzhiyun 						"wastes a bit of memory "
1561*4882a593Smuzhiyun 						"temporarily so we ignore it "
1562*4882a593Smuzhiyun 						"and return success.");
1563*4882a593Smuzhiyun 			else
1564*4882a593Smuzhiyun 				runlist->rl = rl;
1565*4882a593Smuzhiyun 		}
1566*4882a593Smuzhiyun 	} else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
1567*4882a593Smuzhiyun 		ntfs_debug("Expanding runlist.");
1568*4882a593Smuzhiyun 		/*
1569*4882a593Smuzhiyun 		 * If there is a previous runlist element and it is a sparse
1570*4882a593Smuzhiyun 		 * one, extend it.  Otherwise need to add a new, sparse runlist
1571*4882a593Smuzhiyun 		 * element.
1572*4882a593Smuzhiyun 		 */
1573*4882a593Smuzhiyun 		if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
1574*4882a593Smuzhiyun 			(rl - 1)->length = new_length - (rl - 1)->vcn;
1575*4882a593Smuzhiyun 		else {
1576*4882a593Smuzhiyun 			/* Determine the runlist size. */
1577*4882a593Smuzhiyun 			old_size = rl - runlist->rl + 1;
1578*4882a593Smuzhiyun 			/* Reallocate memory if necessary. */
1579*4882a593Smuzhiyun 			rl = ntfs_rl_realloc(runlist->rl, old_size,
1580*4882a593Smuzhiyun 					old_size + 1);
1581*4882a593Smuzhiyun 			if (IS_ERR(rl)) {
1582*4882a593Smuzhiyun 				ntfs_error(vol->sb, "Failed to expand runlist "
1583*4882a593Smuzhiyun 						"buffer, aborting.");
1584*4882a593Smuzhiyun 				return PTR_ERR(rl);
1585*4882a593Smuzhiyun 			}
1586*4882a593Smuzhiyun 			runlist->rl = rl;
1587*4882a593Smuzhiyun 			/*
1588*4882a593Smuzhiyun 			 * Set @rl to the same runlist element in the new
1589*4882a593Smuzhiyun 			 * runlist as before in the old runlist.
1590*4882a593Smuzhiyun 			 */
1591*4882a593Smuzhiyun 			rl += old_size - 1;
1592*4882a593Smuzhiyun 			/* Add a new, sparse runlist element. */
1593*4882a593Smuzhiyun 			rl->lcn = LCN_HOLE;
1594*4882a593Smuzhiyun 			rl->length = new_length - rl->vcn;
1595*4882a593Smuzhiyun 			/* Add a new terminator runlist element. */
1596*4882a593Smuzhiyun 			rl++;
1597*4882a593Smuzhiyun 			rl->length = 0;
1598*4882a593Smuzhiyun 		}
1599*4882a593Smuzhiyun 		rl->vcn = new_length;
1600*4882a593Smuzhiyun 		rl->lcn = LCN_ENOENT;
1601*4882a593Smuzhiyun 	} else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
1602*4882a593Smuzhiyun 		/* Runlist already has same size as requested. */
1603*4882a593Smuzhiyun 		rl->lcn = LCN_ENOENT;
1604*4882a593Smuzhiyun 	}
1605*4882a593Smuzhiyun 	ntfs_debug("Done.");
1606*4882a593Smuzhiyun 	return 0;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun /**
1610*4882a593Smuzhiyun  * ntfs_rl_punch_nolock - punch a hole into a runlist
1611*4882a593Smuzhiyun  * @vol:	ntfs volume (needed for error output)
1612*4882a593Smuzhiyun  * @runlist:	runlist to punch a hole into
1613*4882a593Smuzhiyun  * @start:	starting VCN of the hole to be created
1614*4882a593Smuzhiyun  * @length:	size of the hole to be created in units of clusters
1615*4882a593Smuzhiyun  *
1616*4882a593Smuzhiyun  * Punch a hole into the runlist @runlist starting at VCN @start and of size
1617*4882a593Smuzhiyun  * @length clusters.
1618*4882a593Smuzhiyun  *
1619*4882a593Smuzhiyun  * Return 0 on success and -errno on error, in which case @runlist has not been
1620*4882a593Smuzhiyun  * modified.
1621*4882a593Smuzhiyun  *
1622*4882a593Smuzhiyun  * If @start and/or @start + @length are outside the runlist return error code
1623*4882a593Smuzhiyun  * -ENOENT.
1624*4882a593Smuzhiyun  *
1625*4882a593Smuzhiyun  * If the runlist contains unmapped or error elements between @start and @start
1626*4882a593Smuzhiyun  * + @length return error code -EINVAL.
1627*4882a593Smuzhiyun  *
1628*4882a593Smuzhiyun  * Locking: The caller must hold @runlist->lock for writing.
1629*4882a593Smuzhiyun  */
ntfs_rl_punch_nolock(const ntfs_volume * vol,runlist * const runlist,const VCN start,const s64 length)1630*4882a593Smuzhiyun int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist,
1631*4882a593Smuzhiyun 		const VCN start, const s64 length)
1632*4882a593Smuzhiyun {
1633*4882a593Smuzhiyun 	const VCN end = start + length;
1634*4882a593Smuzhiyun 	s64 delta;
1635*4882a593Smuzhiyun 	runlist_element *rl, *rl_end, *rl_real_end, *trl;
1636*4882a593Smuzhiyun 	int old_size;
1637*4882a593Smuzhiyun 	bool lcn_fixup = false;
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	ntfs_debug("Entering for start 0x%llx, length 0x%llx.",
1640*4882a593Smuzhiyun 			(long long)start, (long long)length);
1641*4882a593Smuzhiyun 	BUG_ON(!runlist);
1642*4882a593Smuzhiyun 	BUG_ON(start < 0);
1643*4882a593Smuzhiyun 	BUG_ON(length < 0);
1644*4882a593Smuzhiyun 	BUG_ON(end < 0);
1645*4882a593Smuzhiyun 	rl = runlist->rl;
1646*4882a593Smuzhiyun 	if (unlikely(!rl)) {
1647*4882a593Smuzhiyun 		if (likely(!start && !length))
1648*4882a593Smuzhiyun 			return 0;
1649*4882a593Smuzhiyun 		return -ENOENT;
1650*4882a593Smuzhiyun 	}
1651*4882a593Smuzhiyun 	/* Find @start in the runlist. */
1652*4882a593Smuzhiyun 	while (likely(rl->length && start >= rl[1].vcn))
1653*4882a593Smuzhiyun 		rl++;
1654*4882a593Smuzhiyun 	rl_end = rl;
1655*4882a593Smuzhiyun 	/* Find @end in the runlist. */
1656*4882a593Smuzhiyun 	while (likely(rl_end->length && end >= rl_end[1].vcn)) {
1657*4882a593Smuzhiyun 		/* Verify there are no unmapped or error elements. */
1658*4882a593Smuzhiyun 		if (unlikely(rl_end->lcn < LCN_HOLE))
1659*4882a593Smuzhiyun 			return -EINVAL;
1660*4882a593Smuzhiyun 		rl_end++;
1661*4882a593Smuzhiyun 	}
1662*4882a593Smuzhiyun 	/* Check the last element. */
1663*4882a593Smuzhiyun 	if (unlikely(rl_end->length && rl_end->lcn < LCN_HOLE))
1664*4882a593Smuzhiyun 		return -EINVAL;
1665*4882a593Smuzhiyun 	/* This covers @start being out of bounds, too. */
1666*4882a593Smuzhiyun 	if (!rl_end->length && end > rl_end->vcn)
1667*4882a593Smuzhiyun 		return -ENOENT;
1668*4882a593Smuzhiyun 	if (!length)
1669*4882a593Smuzhiyun 		return 0;
1670*4882a593Smuzhiyun 	if (!rl->length)
1671*4882a593Smuzhiyun 		return -ENOENT;
1672*4882a593Smuzhiyun 	rl_real_end = rl_end;
1673*4882a593Smuzhiyun 	/* Determine the runlist size. */
1674*4882a593Smuzhiyun 	while (likely(rl_real_end->length))
1675*4882a593Smuzhiyun 		rl_real_end++;
1676*4882a593Smuzhiyun 	old_size = rl_real_end - runlist->rl + 1;
1677*4882a593Smuzhiyun 	/* If @start is in a hole simply extend the hole. */
1678*4882a593Smuzhiyun 	if (rl->lcn == LCN_HOLE) {
1679*4882a593Smuzhiyun 		/*
1680*4882a593Smuzhiyun 		 * If both @start and @end are in the same sparse run, we are
1681*4882a593Smuzhiyun 		 * done.
1682*4882a593Smuzhiyun 		 */
1683*4882a593Smuzhiyun 		if (end <= rl[1].vcn) {
1684*4882a593Smuzhiyun 			ntfs_debug("Done (requested hole is already sparse).");
1685*4882a593Smuzhiyun 			return 0;
1686*4882a593Smuzhiyun 		}
1687*4882a593Smuzhiyun extend_hole:
1688*4882a593Smuzhiyun 		/* Extend the hole. */
1689*4882a593Smuzhiyun 		rl->length = end - rl->vcn;
1690*4882a593Smuzhiyun 		/* If @end is in a hole, merge it with the current one. */
1691*4882a593Smuzhiyun 		if (rl_end->lcn == LCN_HOLE) {
1692*4882a593Smuzhiyun 			rl_end++;
1693*4882a593Smuzhiyun 			rl->length = rl_end->vcn - rl->vcn;
1694*4882a593Smuzhiyun 		}
1695*4882a593Smuzhiyun 		/* We have done the hole.  Now deal with the remaining tail. */
1696*4882a593Smuzhiyun 		rl++;
1697*4882a593Smuzhiyun 		/* Cut out all runlist elements up to @end. */
1698*4882a593Smuzhiyun 		if (rl < rl_end)
1699*4882a593Smuzhiyun 			memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1700*4882a593Smuzhiyun 					sizeof(*rl));
1701*4882a593Smuzhiyun 		/* Adjust the beginning of the tail if necessary. */
1702*4882a593Smuzhiyun 		if (end > rl->vcn) {
1703*4882a593Smuzhiyun 			delta = end - rl->vcn;
1704*4882a593Smuzhiyun 			rl->vcn = end;
1705*4882a593Smuzhiyun 			rl->length -= delta;
1706*4882a593Smuzhiyun 			/* Only adjust the lcn if it is real. */
1707*4882a593Smuzhiyun 			if (rl->lcn >= 0)
1708*4882a593Smuzhiyun 				rl->lcn += delta;
1709*4882a593Smuzhiyun 		}
1710*4882a593Smuzhiyun shrink_allocation:
1711*4882a593Smuzhiyun 		/* Reallocate memory if the allocation changed. */
1712*4882a593Smuzhiyun 		if (rl < rl_end) {
1713*4882a593Smuzhiyun 			rl = ntfs_rl_realloc(runlist->rl, old_size,
1714*4882a593Smuzhiyun 					old_size - (rl_end - rl));
1715*4882a593Smuzhiyun 			if (IS_ERR(rl))
1716*4882a593Smuzhiyun 				ntfs_warning(vol->sb, "Failed to shrink "
1717*4882a593Smuzhiyun 						"runlist buffer.  This just "
1718*4882a593Smuzhiyun 						"wastes a bit of memory "
1719*4882a593Smuzhiyun 						"temporarily so we ignore it "
1720*4882a593Smuzhiyun 						"and return success.");
1721*4882a593Smuzhiyun 			else
1722*4882a593Smuzhiyun 				runlist->rl = rl;
1723*4882a593Smuzhiyun 		}
1724*4882a593Smuzhiyun 		ntfs_debug("Done (extend hole).");
1725*4882a593Smuzhiyun 		return 0;
1726*4882a593Smuzhiyun 	}
1727*4882a593Smuzhiyun 	/*
1728*4882a593Smuzhiyun 	 * If @start is at the beginning of a run things are easier as there is
1729*4882a593Smuzhiyun 	 * no need to split the first run.
1730*4882a593Smuzhiyun 	 */
1731*4882a593Smuzhiyun 	if (start == rl->vcn) {
1732*4882a593Smuzhiyun 		/*
1733*4882a593Smuzhiyun 		 * @start is at the beginning of a run.
1734*4882a593Smuzhiyun 		 *
1735*4882a593Smuzhiyun 		 * If the previous run is sparse, extend its hole.
1736*4882a593Smuzhiyun 		 *
1737*4882a593Smuzhiyun 		 * If @end is not in the same run, switch the run to be sparse
1738*4882a593Smuzhiyun 		 * and extend the newly created hole.
1739*4882a593Smuzhiyun 		 *
1740*4882a593Smuzhiyun 		 * Thus both of these cases reduce the problem to the above
1741*4882a593Smuzhiyun 		 * case of "@start is in a hole".
1742*4882a593Smuzhiyun 		 */
1743*4882a593Smuzhiyun 		if (rl > runlist->rl && (rl - 1)->lcn == LCN_HOLE) {
1744*4882a593Smuzhiyun 			rl--;
1745*4882a593Smuzhiyun 			goto extend_hole;
1746*4882a593Smuzhiyun 		}
1747*4882a593Smuzhiyun 		if (end >= rl[1].vcn) {
1748*4882a593Smuzhiyun 			rl->lcn = LCN_HOLE;
1749*4882a593Smuzhiyun 			goto extend_hole;
1750*4882a593Smuzhiyun 		}
1751*4882a593Smuzhiyun 		/*
1752*4882a593Smuzhiyun 		 * The final case is when @end is in the same run as @start.
1753*4882a593Smuzhiyun 		 * For this need to split the run into two.  One run for the
1754*4882a593Smuzhiyun 		 * sparse region between the beginning of the old run, i.e.
1755*4882a593Smuzhiyun 		 * @start, and @end and one for the remaining non-sparse
1756*4882a593Smuzhiyun 		 * region, i.e. between @end and the end of the old run.
1757*4882a593Smuzhiyun 		 */
1758*4882a593Smuzhiyun 		trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
1759*4882a593Smuzhiyun 		if (IS_ERR(trl))
1760*4882a593Smuzhiyun 			goto enomem_out;
1761*4882a593Smuzhiyun 		old_size++;
1762*4882a593Smuzhiyun 		if (runlist->rl != trl) {
1763*4882a593Smuzhiyun 			rl = trl + (rl - runlist->rl);
1764*4882a593Smuzhiyun 			rl_end = trl + (rl_end - runlist->rl);
1765*4882a593Smuzhiyun 			rl_real_end = trl + (rl_real_end - runlist->rl);
1766*4882a593Smuzhiyun 			runlist->rl = trl;
1767*4882a593Smuzhiyun 		}
1768*4882a593Smuzhiyun split_end:
1769*4882a593Smuzhiyun 		/* Shift all the runs up by one. */
1770*4882a593Smuzhiyun 		memmove(rl + 1, rl, (rl_real_end - rl + 1) * sizeof(*rl));
1771*4882a593Smuzhiyun 		/* Finally, setup the two split runs. */
1772*4882a593Smuzhiyun 		rl->lcn = LCN_HOLE;
1773*4882a593Smuzhiyun 		rl->length = length;
1774*4882a593Smuzhiyun 		rl++;
1775*4882a593Smuzhiyun 		rl->vcn += length;
1776*4882a593Smuzhiyun 		/* Only adjust the lcn if it is real. */
1777*4882a593Smuzhiyun 		if (rl->lcn >= 0 || lcn_fixup)
1778*4882a593Smuzhiyun 			rl->lcn += length;
1779*4882a593Smuzhiyun 		rl->length -= length;
1780*4882a593Smuzhiyun 		ntfs_debug("Done (split one).");
1781*4882a593Smuzhiyun 		return 0;
1782*4882a593Smuzhiyun 	}
1783*4882a593Smuzhiyun 	/*
1784*4882a593Smuzhiyun 	 * @start is neither in a hole nor at the beginning of a run.
1785*4882a593Smuzhiyun 	 *
1786*4882a593Smuzhiyun 	 * If @end is in a hole, things are easier as simply truncating the run
1787*4882a593Smuzhiyun 	 * @start is in to end at @start - 1, deleting all runs after that up
1788*4882a593Smuzhiyun 	 * to @end, and finally extending the beginning of the run @end is in
1789*4882a593Smuzhiyun 	 * to be @start is all that is needed.
1790*4882a593Smuzhiyun 	 */
1791*4882a593Smuzhiyun 	if (rl_end->lcn == LCN_HOLE) {
1792*4882a593Smuzhiyun 		/* Truncate the run containing @start. */
1793*4882a593Smuzhiyun 		rl->length = start - rl->vcn;
1794*4882a593Smuzhiyun 		rl++;
1795*4882a593Smuzhiyun 		/* Cut out all runlist elements up to @end. */
1796*4882a593Smuzhiyun 		if (rl < rl_end)
1797*4882a593Smuzhiyun 			memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
1798*4882a593Smuzhiyun 					sizeof(*rl));
1799*4882a593Smuzhiyun 		/* Extend the beginning of the run @end is in to be @start. */
1800*4882a593Smuzhiyun 		rl->vcn = start;
1801*4882a593Smuzhiyun 		rl->length = rl[1].vcn - start;
1802*4882a593Smuzhiyun 		goto shrink_allocation;
1803*4882a593Smuzhiyun 	}
1804*4882a593Smuzhiyun 	/*
1805*4882a593Smuzhiyun 	 * If @end is not in a hole there are still two cases to distinguish.
1806*4882a593Smuzhiyun 	 * Either @end is or is not in the same run as @start.
1807*4882a593Smuzhiyun 	 *
1808*4882a593Smuzhiyun 	 * The second case is easier as it can be reduced to an already solved
1809*4882a593Smuzhiyun 	 * problem by truncating the run @start is in to end at @start - 1.
1810*4882a593Smuzhiyun 	 * Then, if @end is in the next run need to split the run into a sparse
1811*4882a593Smuzhiyun 	 * run followed by a non-sparse run (already covered above) and if @end
1812*4882a593Smuzhiyun 	 * is not in the next run switching it to be sparse, again reduces the
1813*4882a593Smuzhiyun 	 * problem to the already covered case of "@start is in a hole".
1814*4882a593Smuzhiyun 	 */
1815*4882a593Smuzhiyun 	if (end >= rl[1].vcn) {
1816*4882a593Smuzhiyun 		/*
1817*4882a593Smuzhiyun 		 * If @end is not in the next run, reduce the problem to the
1818*4882a593Smuzhiyun 		 * case of "@start is in a hole".
1819*4882a593Smuzhiyun 		 */
1820*4882a593Smuzhiyun 		if (rl[1].length && end >= rl[2].vcn) {
1821*4882a593Smuzhiyun 			/* Truncate the run containing @start. */
1822*4882a593Smuzhiyun 			rl->length = start - rl->vcn;
1823*4882a593Smuzhiyun 			rl++;
1824*4882a593Smuzhiyun 			rl->vcn = start;
1825*4882a593Smuzhiyun 			rl->lcn = LCN_HOLE;
1826*4882a593Smuzhiyun 			goto extend_hole;
1827*4882a593Smuzhiyun 		}
1828*4882a593Smuzhiyun 		trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
1829*4882a593Smuzhiyun 		if (IS_ERR(trl))
1830*4882a593Smuzhiyun 			goto enomem_out;
1831*4882a593Smuzhiyun 		old_size++;
1832*4882a593Smuzhiyun 		if (runlist->rl != trl) {
1833*4882a593Smuzhiyun 			rl = trl + (rl - runlist->rl);
1834*4882a593Smuzhiyun 			rl_end = trl + (rl_end - runlist->rl);
1835*4882a593Smuzhiyun 			rl_real_end = trl + (rl_real_end - runlist->rl);
1836*4882a593Smuzhiyun 			runlist->rl = trl;
1837*4882a593Smuzhiyun 		}
1838*4882a593Smuzhiyun 		/* Truncate the run containing @start. */
1839*4882a593Smuzhiyun 		rl->length = start - rl->vcn;
1840*4882a593Smuzhiyun 		rl++;
1841*4882a593Smuzhiyun 		/*
1842*4882a593Smuzhiyun 		 * @end is in the next run, reduce the problem to the case
1843*4882a593Smuzhiyun 		 * where "@start is at the beginning of a run and @end is in
1844*4882a593Smuzhiyun 		 * the same run as @start".
1845*4882a593Smuzhiyun 		 */
1846*4882a593Smuzhiyun 		delta = rl->vcn - start;
1847*4882a593Smuzhiyun 		rl->vcn = start;
1848*4882a593Smuzhiyun 		if (rl->lcn >= 0) {
1849*4882a593Smuzhiyun 			rl->lcn -= delta;
1850*4882a593Smuzhiyun 			/* Need this in case the lcn just became negative. */
1851*4882a593Smuzhiyun 			lcn_fixup = true;
1852*4882a593Smuzhiyun 		}
1853*4882a593Smuzhiyun 		rl->length += delta;
1854*4882a593Smuzhiyun 		goto split_end;
1855*4882a593Smuzhiyun 	}
1856*4882a593Smuzhiyun 	/*
1857*4882a593Smuzhiyun 	 * The first case from above, i.e. @end is in the same run as @start.
1858*4882a593Smuzhiyun 	 * We need to split the run into three.  One run for the non-sparse
1859*4882a593Smuzhiyun 	 * region between the beginning of the old run and @start, one for the
1860*4882a593Smuzhiyun 	 * sparse region between @start and @end, and one for the remaining
1861*4882a593Smuzhiyun 	 * non-sparse region, i.e. between @end and the end of the old run.
1862*4882a593Smuzhiyun 	 */
1863*4882a593Smuzhiyun 	trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 2);
1864*4882a593Smuzhiyun 	if (IS_ERR(trl))
1865*4882a593Smuzhiyun 		goto enomem_out;
1866*4882a593Smuzhiyun 	old_size += 2;
1867*4882a593Smuzhiyun 	if (runlist->rl != trl) {
1868*4882a593Smuzhiyun 		rl = trl + (rl - runlist->rl);
1869*4882a593Smuzhiyun 		rl_end = trl + (rl_end - runlist->rl);
1870*4882a593Smuzhiyun 		rl_real_end = trl + (rl_real_end - runlist->rl);
1871*4882a593Smuzhiyun 		runlist->rl = trl;
1872*4882a593Smuzhiyun 	}
1873*4882a593Smuzhiyun 	/* Shift all the runs up by two. */
1874*4882a593Smuzhiyun 	memmove(rl + 2, rl, (rl_real_end - rl + 1) * sizeof(*rl));
1875*4882a593Smuzhiyun 	/* Finally, setup the three split runs. */
1876*4882a593Smuzhiyun 	rl->length = start - rl->vcn;
1877*4882a593Smuzhiyun 	rl++;
1878*4882a593Smuzhiyun 	rl->vcn = start;
1879*4882a593Smuzhiyun 	rl->lcn = LCN_HOLE;
1880*4882a593Smuzhiyun 	rl->length = length;
1881*4882a593Smuzhiyun 	rl++;
1882*4882a593Smuzhiyun 	delta = end - rl->vcn;
1883*4882a593Smuzhiyun 	rl->vcn = end;
1884*4882a593Smuzhiyun 	rl->lcn += delta;
1885*4882a593Smuzhiyun 	rl->length -= delta;
1886*4882a593Smuzhiyun 	ntfs_debug("Done (split both).");
1887*4882a593Smuzhiyun 	return 0;
1888*4882a593Smuzhiyun enomem_out:
1889*4882a593Smuzhiyun 	ntfs_error(vol->sb, "Not enough memory to extend runlist buffer.");
1890*4882a593Smuzhiyun 	return -ENOMEM;
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun #endif /* NTFS_RW */
1894