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