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