minidlna support now Samsung TV C550/C650 (thx amir909)
[tomato.git] / release / src / router / ntfs-3g / libntfs-3g / runlist.c
blob383a80b2877894bb570422a12b515ccc5dc71cf6
1 /**
2 * runlist.c - Run list handling code. Originated from the Linux-NTFS project.
4 * Copyright (c) 2002-2005 Anton Altaparmakov
5 * Copyright (c) 2002-2005 Richard Russon
6 * Copyright (c) 2002-2008 Szabolcs Szakacsits
7 * Copyright (c) 2004 Yura Pakhuchiy
8 * Copyright (c) 2007-2010 Jean-Pierre Andre
10 * This program/include file is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program/include file is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the NTFS-3G
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #ifdef HAVE_STDIO_H
31 #include <stdio.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
43 #include "compat.h"
44 #include "types.h"
45 #include "volume.h"
46 #include "layout.h"
47 #include "debug.h"
48 #include "device.h"
49 #include "logging.h"
50 #include "misc.h"
52 /**
53 * ntfs_rl_mm - runlist memmove
54 * @base:
55 * @dst:
56 * @src:
57 * @size:
59 * Description...
61 * Returns:
63 static void ntfs_rl_mm(runlist_element *base, int dst, int src, int size)
65 if ((dst != src) && (size > 0))
66 memmove(base + dst, base + src, size * sizeof(*base));
69 /**
70 * ntfs_rl_mc - runlist memory copy
71 * @dstbase:
72 * @dst:
73 * @srcbase:
74 * @src:
75 * @size:
77 * Description...
79 * Returns:
81 static void ntfs_rl_mc(runlist_element *dstbase, int dst,
82 runlist_element *srcbase, int src, int size)
84 if (size > 0)
85 memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
88 /**
89 * ntfs_rl_realloc - Reallocate memory for runlists
90 * @rl: original runlist
91 * @old_size: number of runlist elements in the original runlist @rl
92 * @new_size: number of runlist elements we need space for
94 * As the runlists grow, more memory will be required. To prevent large
95 * numbers of small reallocations of memory, this function returns a 4kiB block
96 * of memory.
98 * N.B. If the new allocation doesn't require a different number of 4kiB
99 * blocks in memory, the function will return the original pointer.
101 * On success, return a pointer to the newly allocated, or recycled, memory.
102 * On error, return NULL with errno set to the error code.
104 static runlist_element *ntfs_rl_realloc(runlist_element *rl, int old_size,
105 int new_size)
107 old_size = (old_size * sizeof(runlist_element) + 0xfff) & ~0xfff;
108 new_size = (new_size * sizeof(runlist_element) + 0xfff) & ~0xfff;
109 if (old_size == new_size)
110 return rl;
111 return realloc(rl, new_size);
115 * Extend a runlist by some entry count
116 * The runlist may have to be reallocated
118 * Returns the reallocated runlist
119 * or NULL if reallocation was not possible (with errno set)
120 * the runlist is left unchanged if the reallocation fails
123 runlist_element *ntfs_rl_extend(ntfs_attr *na, runlist_element *rl,
124 int more_entries)
126 runlist_element *newrl;
127 int last;
128 int irl;
130 if (na->rl && rl) {
131 irl = (int)(rl - na->rl);
132 last = irl;
133 while (na->rl[last].length)
134 last++;
135 newrl = ntfs_rl_realloc(na->rl,last+1,last+more_entries+1);
136 if (!newrl) {
137 errno = ENOMEM;
138 rl = (runlist_element*)NULL;
139 } else
140 na->rl = newrl;
141 rl = &newrl[irl];
142 } else {
143 ntfs_log_error("Cannot extend unmapped runlist");
144 errno = EIO;
145 rl = (runlist_element*)NULL;
147 return (rl);
151 * ntfs_rl_are_mergeable - test if two runlists can be joined together
152 * @dst: original runlist
153 * @src: new runlist to test for mergeability with @dst
155 * Test if two runlists can be joined together. For this, their VCNs and LCNs
156 * must be adjacent.
158 * Return: TRUE Success, the runlists can be merged.
159 * FALSE Failure, the runlists cannot be merged.
161 static BOOL ntfs_rl_are_mergeable(runlist_element *dst, runlist_element *src)
163 if (!dst || !src) {
164 ntfs_log_debug("Eeek. ntfs_rl_are_mergeable() invoked with NULL "
165 "pointer!\n");
166 return FALSE;
169 /* We can merge unmapped regions even if they are misaligned. */
170 if ((dst->lcn == LCN_RL_NOT_MAPPED) && (src->lcn == LCN_RL_NOT_MAPPED))
171 return TRUE;
172 /* If the runs are misaligned, we cannot merge them. */
173 if ((dst->vcn + dst->length) != src->vcn)
174 return FALSE;
175 /* If both runs are non-sparse and contiguous, we can merge them. */
176 if ((dst->lcn >= 0) && (src->lcn >= 0) &&
177 ((dst->lcn + dst->length) == src->lcn))
178 return TRUE;
179 /* If we are merging two holes, we can merge them. */
180 if ((dst->lcn == LCN_HOLE) && (src->lcn == LCN_HOLE))
181 return TRUE;
182 /* Cannot merge. */
183 return FALSE;
187 * __ntfs_rl_merge - merge two runlists without testing if they can be merged
188 * @dst: original, destination runlist
189 * @src: new runlist to merge with @dst
191 * Merge the two runlists, writing into the destination runlist @dst. The
192 * caller must make sure the runlists can be merged or this will corrupt the
193 * destination runlist.
195 static void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
197 dst->length += src->length;
201 * ntfs_rl_append - append a runlist after a given element
202 * @dst: original runlist to be worked on
203 * @dsize: number of elements in @dst (including end marker)
204 * @src: runlist to be inserted into @dst
205 * @ssize: number of elements in @src (excluding end marker)
206 * @loc: append the new runlist @src after this element in @dst
208 * Append the runlist @src after element @loc in @dst. Merge the right end of
209 * the new runlist, if necessary. Adjust the size of the hole before the
210 * appended runlist.
212 * On success, return a pointer to the new, combined, runlist. Note, both
213 * runlists @dst and @src are deallocated before returning so you cannot use
214 * the pointers for anything any more. (Strictly speaking the returned runlist
215 * may be the same as @dst but this is irrelevant.)
217 * On error, return NULL, with errno set to the error code. Both runlists are
218 * left unmodified.
220 static runlist_element *ntfs_rl_append(runlist_element *dst, int dsize,
221 runlist_element *src, int ssize, int loc)
223 BOOL right = FALSE; /* Right end of @src needs merging */
224 int marker; /* End of the inserted runs */
226 if (!dst || !src) {
227 ntfs_log_debug("Eeek. ntfs_rl_append() invoked with NULL "
228 "pointer!\n");
229 errno = EINVAL;
230 return NULL;
233 /* First, check if the right hand end needs merging. */
234 if ((loc + 1) < dsize)
235 right = ntfs_rl_are_mergeable(src + ssize - 1, dst + loc + 1);
237 /* Space required: @dst size + @src size, less one if we merged. */
238 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
239 if (!dst)
240 return NULL;
242 * We are guaranteed to succeed from here so can start modifying the
243 * original runlists.
246 /* First, merge the right hand end, if necessary. */
247 if (right)
248 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
250 /* marker - First run after the @src runs that have been inserted */
251 marker = loc + ssize + 1;
253 /* Move the tail of @dst out of the way, then copy in @src. */
254 ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - loc - 1 - right);
255 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
257 /* Adjust the size of the preceding hole. */
258 dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
260 /* We may have changed the length of the file, so fix the end marker */
261 if (dst[marker].lcn == LCN_ENOENT)
262 dst[marker].vcn = dst[marker-1].vcn + dst[marker-1].length;
264 return dst;
268 * ntfs_rl_insert - insert a runlist into another
269 * @dst: original runlist to be worked on
270 * @dsize: number of elements in @dst (including end marker)
271 * @src: new runlist to be inserted
272 * @ssize: number of elements in @src (excluding end marker)
273 * @loc: insert the new runlist @src before this element in @dst
275 * Insert the runlist @src before element @loc in the runlist @dst. Merge the
276 * left end of the new runlist, if necessary. Adjust the size of the hole
277 * after the inserted runlist.
279 * On success, return a pointer to the new, combined, runlist. Note, both
280 * runlists @dst and @src are deallocated before returning so you cannot use
281 * the pointers for anything any more. (Strictly speaking the returned runlist
282 * may be the same as @dst but this is irrelevant.)
284 * On error, return NULL, with errno set to the error code. Both runlists are
285 * left unmodified.
287 static runlist_element *ntfs_rl_insert(runlist_element *dst, int dsize,
288 runlist_element *src, int ssize, int loc)
290 BOOL left = FALSE; /* Left end of @src needs merging */
291 BOOL disc = FALSE; /* Discontinuity between @dst and @src */
292 int marker; /* End of the inserted runs */
294 if (!dst || !src) {
295 ntfs_log_debug("Eeek. ntfs_rl_insert() invoked with NULL "
296 "pointer!\n");
297 errno = EINVAL;
298 return NULL;
301 /* disc => Discontinuity between the end of @dst and the start of @src.
302 * This means we might need to insert a "notmapped" run.
304 if (loc == 0)
305 disc = (src[0].vcn > 0);
306 else {
307 s64 merged_length;
309 left = ntfs_rl_are_mergeable(dst + loc - 1, src);
311 merged_length = dst[loc - 1].length;
312 if (left)
313 merged_length += src->length;
315 disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
318 /* Space required: @dst size + @src size, less one if we merged, plus
319 * one if there was a discontinuity.
321 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc);
322 if (!dst)
323 return NULL;
325 * We are guaranteed to succeed from here so can start modifying the
326 * original runlist.
329 if (left)
330 __ntfs_rl_merge(dst + loc - 1, src);
333 * marker - First run after the @src runs that have been inserted
334 * Nominally: marker = @loc + @ssize (location + number of runs in @src)
335 * If "left", then the first run in @src has been merged with one in @dst.
336 * If "disc", then @dst and @src don't meet and we need an extra run to fill the gap.
338 marker = loc + ssize - left + disc;
340 /* Move the tail of @dst out of the way, then copy in @src. */
341 ntfs_rl_mm(dst, marker, loc, dsize - loc);
342 ntfs_rl_mc(dst, loc + disc, src, left, ssize - left);
344 /* Adjust the VCN of the first run after the insertion ... */
345 dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
346 /* ... and the length. */
347 if (dst[marker].lcn == LCN_HOLE || dst[marker].lcn == LCN_RL_NOT_MAPPED)
348 dst[marker].length = dst[marker + 1].vcn - dst[marker].vcn;
350 /* Writing beyond the end of the file and there's a discontinuity. */
351 if (disc) {
352 if (loc > 0) {
353 dst[loc].vcn = dst[loc - 1].vcn + dst[loc - 1].length;
354 dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
355 } else {
356 dst[loc].vcn = 0;
357 dst[loc].length = dst[loc + 1].vcn;
359 dst[loc].lcn = LCN_RL_NOT_MAPPED;
361 return dst;
365 * ntfs_rl_replace - overwrite a runlist element with another runlist
366 * @dst: original runlist to be worked on
367 * @dsize: number of elements in @dst (including end marker)
368 * @src: new runlist to be inserted
369 * @ssize: number of elements in @src (excluding end marker)
370 * @loc: index in runlist @dst to overwrite with @src
372 * Replace the runlist element @dst at @loc with @src. Merge the left and
373 * right ends of the inserted runlist, if necessary.
375 * On success, return a pointer to the new, combined, runlist. Note, both
376 * runlists @dst and @src are deallocated before returning so you cannot use
377 * the pointers for anything any more. (Strictly speaking the returned runlist
378 * may be the same as @dst but this is irrelevant.)
380 * On error, return NULL, with errno set to the error code. Both runlists are
381 * left unmodified.
383 static runlist_element *ntfs_rl_replace(runlist_element *dst, int dsize,
384 runlist_element *src, int ssize,
385 int loc)
387 signed delta;
388 BOOL left = FALSE; /* Left end of @src needs merging */
389 BOOL right = FALSE; /* Right end of @src needs merging */
390 int tail; /* Start of tail of @dst */
391 int marker; /* End of the inserted runs */
393 if (!dst || !src) {
394 ntfs_log_debug("Eeek. ntfs_rl_replace() invoked with NULL "
395 "pointer!\n");
396 errno = EINVAL;
397 return NULL;
400 /* First, see if the left and right ends need merging. */
401 if ((loc + 1) < dsize)
402 right = ntfs_rl_are_mergeable(src + ssize - 1, dst + loc + 1);
403 if (loc > 0)
404 left = ntfs_rl_are_mergeable(dst + loc - 1, src);
406 /* Allocate some space. We'll need less if the left, right, or both
407 * ends get merged. The -1 accounts for the run being replaced.
409 delta = ssize - 1 - left - right;
410 if (delta > 0) {
411 dst = ntfs_rl_realloc(dst, dsize, dsize + delta);
412 if (!dst)
413 return NULL;
416 * We are guaranteed to succeed from here so can start modifying the
417 * original runlists.
420 /* First, merge the left and right ends, if necessary. */
421 if (right)
422 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
423 if (left)
424 __ntfs_rl_merge(dst + loc - 1, src);
427 * tail - Offset of the tail of @dst
428 * Nominally: @tail = @loc + 1 (location, skipping the replaced run)
429 * If "right", then one of @dst's runs is already merged into @src.
431 tail = loc + right + 1;
434 * marker - First run after the @src runs that have been inserted
435 * Nominally: @marker = @loc + @ssize (location + number of runs in @src)
436 * If "left", then the first run in @src has been merged with one in @dst.
438 marker = loc + ssize - left;
440 /* Move the tail of @dst out of the way, then copy in @src. */
441 ntfs_rl_mm(dst, marker, tail, dsize - tail);
442 ntfs_rl_mc(dst, loc, src, left, ssize - left);
444 /* We may have changed the length of the file, so fix the end marker */
445 if (((dsize - tail) > 0) && (dst[marker].lcn == LCN_ENOENT))
446 dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
448 return dst;
452 * ntfs_rl_split - insert a runlist into the centre of a hole
453 * @dst: original runlist to be worked on
454 * @dsize: number of elements in @dst (including end marker)
455 * @src: new runlist to be inserted
456 * @ssize: number of elements in @src (excluding end marker)
457 * @loc: index in runlist @dst at which to split and insert @src
459 * Split the runlist @dst at @loc into two and insert @new in between the two
460 * fragments. No merging of runlists is necessary. Adjust the size of the
461 * holes either side.
463 * On success, return a pointer to the new, combined, runlist. Note, both
464 * runlists @dst and @src are deallocated before returning so you cannot use
465 * the pointers for anything any more. (Strictly speaking the returned runlist
466 * may be the same as @dst but this is irrelevant.)
468 * On error, return NULL, with errno set to the error code. Both runlists are
469 * left unmodified.
471 static runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
472 runlist_element *src, int ssize, int loc)
474 if (!dst || !src) {
475 ntfs_log_debug("Eeek. ntfs_rl_split() invoked with NULL pointer!\n");
476 errno = EINVAL;
477 return NULL;
480 /* Space required: @dst size + @src size + one new hole. */
481 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
482 if (!dst)
483 return dst;
485 * We are guaranteed to succeed from here so can start modifying the
486 * original runlists.
489 /* Move the tail of @dst out of the way, then copy in @src. */
490 ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
491 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
493 /* Adjust the size of the holes either size of @src. */
494 dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
495 dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
496 dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
498 return dst;
503 * ntfs_runlists_merge_i - see ntfs_runlists_merge
505 static runlist_element *ntfs_runlists_merge_i(runlist_element *drl,
506 runlist_element *srl)
508 int di, si; /* Current index into @[ds]rl. */
509 int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
510 int dins; /* Index into @drl at which to insert @srl. */
511 int dend, send; /* Last index into @[ds]rl. */
512 int dfinal, sfinal; /* The last index into @[ds]rl with
513 lcn >= LCN_HOLE. */
514 int marker = 0;
515 VCN marker_vcn = 0;
517 ntfs_log_debug("dst:\n");
518 ntfs_debug_runlist_dump(drl);
519 ntfs_log_debug("src:\n");
520 ntfs_debug_runlist_dump(srl);
522 /* Check for silly calling... */
523 if (!srl)
524 return drl;
526 /* Check for the case where the first mapping is being done now. */
527 if (!drl) {
528 drl = srl;
529 /* Complete the source runlist if necessary. */
530 if (drl[0].vcn) {
531 /* Scan to the end of the source runlist. */
532 for (dend = 0; drl[dend].length; dend++)
534 dend++;
535 drl = ntfs_rl_realloc(drl, dend, dend + 1);
536 if (!drl)
537 return drl;
538 /* Insert start element at the front of the runlist. */
539 ntfs_rl_mm(drl, 1, 0, dend);
540 drl[0].vcn = 0;
541 drl[0].lcn = LCN_RL_NOT_MAPPED;
542 drl[0].length = drl[1].vcn;
544 goto finished;
547 si = di = 0;
549 /* Skip any unmapped start element(s) in the source runlist. */
550 while (srl[si].length && srl[si].lcn < (LCN)LCN_HOLE)
551 si++;
553 /* Can't have an entirely unmapped source runlist. */
554 if (!srl[si].length) {
555 errno = EINVAL;
556 ntfs_log_perror("%s: unmapped source runlist", __FUNCTION__);
557 return NULL;
560 /* Record the starting points. */
561 sstart = si;
564 * Skip forward in @drl until we reach the position where @srl needs to
565 * be inserted. If we reach the end of @drl, @srl just needs to be
566 * appended to @drl.
568 for (; drl[di].length; di++) {
569 if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
570 break;
572 dins = di;
574 /* Sanity check for illegal overlaps. */
575 if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
576 (srl[si].lcn >= 0)) {
577 errno = ERANGE;
578 ntfs_log_perror("Run lists overlap. Cannot merge");
579 return NULL;
582 /* Scan to the end of both runlists in order to know their sizes. */
583 for (send = si; srl[send].length; send++)
585 for (dend = di; drl[dend].length; dend++)
588 if (srl[send].lcn == (LCN)LCN_ENOENT)
589 marker_vcn = srl[marker = send].vcn;
591 /* Scan to the last element with lcn >= LCN_HOLE. */
592 for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
594 for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
598 BOOL start;
599 BOOL finish;
600 int ds = dend + 1; /* Number of elements in drl & srl */
601 int ss = sfinal - sstart + 1;
603 start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
604 (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
605 finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
606 ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
607 (srl[send - 1].vcn + srl[send - 1].length)));
609 /* Or we'll lose an end marker */
610 if (finish && !drl[dins].length)
611 ss++;
612 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
613 finish = FALSE;
615 ntfs_log_debug("dfinal = %i, dend = %i\n", dfinal, dend);
616 ntfs_log_debug("sstart = %i, sfinal = %i, send = %i\n", sstart, sfinal, send);
617 ntfs_log_debug("start = %i, finish = %i\n", start, finish);
618 ntfs_log_debug("ds = %i, ss = %i, dins = %i\n", ds, ss, dins);
620 if (start) {
621 if (finish)
622 drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
623 else
624 drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
625 } else {
626 if (finish)
627 drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
628 else
629 drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
631 if (!drl) {
632 ntfs_log_perror("Merge failed");
633 return drl;
635 free(srl);
636 if (marker) {
637 ntfs_log_debug("Triggering marker code.\n");
638 for (ds = dend; drl[ds].length; ds++)
640 /* We only need to care if @srl ended after @drl. */
641 if (drl[ds].vcn <= marker_vcn) {
642 int slots = 0;
644 if (drl[ds].vcn == marker_vcn) {
645 ntfs_log_debug("Old marker = %lli, replacing with "
646 "LCN_ENOENT.\n",
647 (long long)drl[ds].lcn);
648 drl[ds].lcn = (LCN)LCN_ENOENT;
649 goto finished;
652 * We need to create an unmapped runlist element in
653 * @drl or extend an existing one before adding the
654 * ENOENT terminator.
656 if (drl[ds].lcn == (LCN)LCN_ENOENT) {
657 ds--;
658 slots = 1;
660 if (drl[ds].lcn != (LCN)LCN_RL_NOT_MAPPED) {
661 /* Add an unmapped runlist element. */
662 if (!slots) {
663 /* FIXME/TODO: We need to have the
664 * extra memory already! (AIA)
666 drl = ntfs_rl_realloc(drl, ds, ds + 2);
667 if (!drl)
668 goto critical_error;
669 slots = 2;
671 ds++;
672 /* Need to set vcn if it isn't set already. */
673 if (slots != 1)
674 drl[ds].vcn = drl[ds - 1].vcn +
675 drl[ds - 1].length;
676 drl[ds].lcn = (LCN)LCN_RL_NOT_MAPPED;
677 /* We now used up a slot. */
678 slots--;
680 drl[ds].length = marker_vcn - drl[ds].vcn;
681 /* Finally add the ENOENT terminator. */
682 ds++;
683 if (!slots) {
684 /* FIXME/TODO: We need to have the extra
685 * memory already! (AIA)
687 drl = ntfs_rl_realloc(drl, ds, ds + 1);
688 if (!drl)
689 goto critical_error;
691 drl[ds].vcn = marker_vcn;
692 drl[ds].lcn = (LCN)LCN_ENOENT;
693 drl[ds].length = (s64)0;
698 finished:
699 /* The merge was completed successfully. */
700 ntfs_log_debug("Merged runlist:\n");
701 ntfs_debug_runlist_dump(drl);
702 return drl;
704 critical_error:
705 /* Critical error! We cannot afford to fail here. */
706 ntfs_log_perror("libntfs: Critical error");
707 ntfs_log_debug("Forcing segmentation fault!\n");
708 marker_vcn = ((runlist*)NULL)->lcn;
709 return drl;
713 * ntfs_runlists_merge - merge two runlists into one
714 * @drl: original runlist to be worked on
715 * @srl: new runlist to be merged into @drl
717 * First we sanity check the two runlists @srl and @drl to make sure that they
718 * are sensible and can be merged. The runlist @srl must be either after the
719 * runlist @drl or completely within a hole (or unmapped region) in @drl.
721 * Merging of runlists is necessary in two cases:
722 * 1. When attribute lists are used and a further extent is being mapped.
723 * 2. When new clusters are allocated to fill a hole or extend a file.
725 * There are four possible ways @srl can be merged. It can:
726 * - be inserted at the beginning of a hole,
727 * - split the hole in two and be inserted between the two fragments,
728 * - be appended at the end of a hole, or it can
729 * - replace the whole hole.
730 * It can also be appended to the end of the runlist, which is just a variant
731 * of the insert case.
733 * On success, return a pointer to the new, combined, runlist. Note, both
734 * runlists @drl and @srl are deallocated before returning so you cannot use
735 * the pointers for anything any more. (Strictly speaking the returned runlist
736 * may be the same as @dst but this is irrelevant.)
738 * On error, return NULL, with errno set to the error code. Both runlists are
739 * left unmodified. The following error codes are defined:
740 * ENOMEM Not enough memory to allocate runlist array.
741 * EINVAL Invalid parameters were passed in.
742 * ERANGE The runlists overlap and cannot be merged.
744 runlist_element *ntfs_runlists_merge(runlist_element *drl,
745 runlist_element *srl)
747 runlist_element *rl;
749 ntfs_log_enter("Entering\n");
750 rl = ntfs_runlists_merge_i(drl, srl);
751 ntfs_log_leave("\n");
752 return rl;
756 * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
757 * @vol: ntfs volume on which the attribute resides
758 * @attr: attribute record whose mapping pairs array to decompress
759 * @old_rl: optional runlist in which to insert @attr's runlist
761 * Decompress the attribute @attr's mapping pairs array into a runlist. On
762 * success, return the decompressed runlist.
764 * If @old_rl is not NULL, decompressed runlist is inserted into the
765 * appropriate place in @old_rl and the resultant, combined runlist is
766 * returned. The original @old_rl is deallocated.
768 * On error, return NULL with errno set to the error code. @old_rl is left
769 * unmodified in that case.
771 * The following error codes are defined:
772 * ENOMEM Not enough memory to allocate runlist array.
773 * EIO Corrupt runlist.
774 * EINVAL Invalid parameters were passed in.
775 * ERANGE The two runlists overlap.
777 * FIXME: For now we take the conceptionally simplest approach of creating the
778 * new runlist disregarding the already existing one and then splicing the
779 * two into one, if that is possible (we check for overlap and discard the new
780 * runlist if overlap present before returning NULL, with errno = ERANGE).
782 static runlist_element *ntfs_mapping_pairs_decompress_i(const ntfs_volume *vol,
783 const ATTR_RECORD *attr, runlist_element *old_rl)
785 VCN vcn; /* Current vcn. */
786 LCN lcn; /* Current lcn. */
787 s64 deltaxcn; /* Change in [vl]cn. */
788 runlist_element *rl; /* The output runlist. */
789 const u8 *buf; /* Current position in mapping pairs array. */
790 const u8 *attr_end; /* End of attribute. */
791 int err, rlsize; /* Size of runlist buffer. */
792 u16 rlpos; /* Current runlist position in units of
793 runlist_elements. */
794 u8 b; /* Current byte offset in buf. */
796 ntfs_log_trace("Entering for attr 0x%x.\n",
797 (unsigned)le32_to_cpu(attr->type));
798 /* Make sure attr exists and is non-resident. */
799 if (!attr || !attr->non_resident ||
800 sle64_to_cpu(attr->lowest_vcn) < (VCN)0) {
801 errno = EINVAL;
802 return NULL;
804 /* Start at vcn = lowest_vcn and lcn 0. */
805 vcn = sle64_to_cpu(attr->lowest_vcn);
806 lcn = 0;
807 /* Get start of the mapping pairs array. */
808 buf = (const u8*)attr + le16_to_cpu(attr->mapping_pairs_offset);
809 attr_end = (const u8*)attr + le32_to_cpu(attr->length);
810 if (buf < (const u8*)attr || buf > attr_end) {
811 ntfs_log_debug("Corrupt attribute.\n");
812 errno = EIO;
813 return NULL;
815 /* Current position in runlist array. */
816 rlpos = 0;
817 /* Allocate first 4kiB block and set current runlist size to 4kiB. */
818 rlsize = 0x1000;
819 rl = ntfs_malloc(rlsize);
820 if (!rl)
821 return NULL;
822 /* Insert unmapped starting element if necessary. */
823 if (vcn) {
824 rl->vcn = (VCN)0;
825 rl->lcn = (LCN)LCN_RL_NOT_MAPPED;
826 rl->length = vcn;
827 rlpos++;
829 while (buf < attr_end && *buf) {
831 * Allocate more memory if needed, including space for the
832 * not-mapped and terminator elements.
834 if ((int)((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
835 runlist_element *rl2;
837 rlsize += 0x1000;
838 rl2 = realloc(rl, rlsize);
839 if (!rl2) {
840 int eo = errno;
841 free(rl);
842 errno = eo;
843 return NULL;
845 rl = rl2;
847 /* Enter the current vcn into the current runlist element. */
848 rl[rlpos].vcn = vcn;
850 * Get the change in vcn, i.e. the run length in clusters.
851 * Doing it this way ensures that we signextend negative values.
852 * A negative run length doesn't make any sense, but hey, I
853 * didn't make up the NTFS specs and Windows NT4 treats the run
854 * length as a signed value so that's how it is...
856 b = *buf & 0xf;
857 if (b) {
858 if (buf + b > attr_end)
859 goto io_error;
860 for (deltaxcn = (s8)buf[b--]; b; b--)
861 deltaxcn = (deltaxcn << 8) + buf[b];
862 } else { /* The length entry is compulsory. */
863 ntfs_log_debug("Missing length entry in mapping pairs "
864 "array.\n");
865 deltaxcn = (s64)-1;
868 * Assume a negative length to indicate data corruption and
869 * hence clean-up and return NULL.
871 if (deltaxcn < 0) {
872 ntfs_log_debug("Invalid length in mapping pairs array.\n");
873 goto err_out;
876 * Enter the current run length into the current runlist
877 * element.
879 rl[rlpos].length = deltaxcn;
880 /* Increment the current vcn by the current run length. */
881 vcn += deltaxcn;
883 * There might be no lcn change at all, as is the case for
884 * sparse clusters on NTFS 3.0+, in which case we set the lcn
885 * to LCN_HOLE.
887 if (!(*buf & 0xf0))
888 rl[rlpos].lcn = (LCN)LCN_HOLE;
889 else {
890 /* Get the lcn change which really can be negative. */
891 u8 b2 = *buf & 0xf;
892 b = b2 + ((*buf >> 4) & 0xf);
893 if (buf + b > attr_end)
894 goto io_error;
895 for (deltaxcn = (s8)buf[b--]; b > b2; b--)
896 deltaxcn = (deltaxcn << 8) + buf[b];
897 /* Change the current lcn to it's new value. */
898 lcn += deltaxcn;
899 #ifdef DEBUG
901 * On NTFS 1.2-, apparently can have lcn == -1 to
902 * indicate a hole. But we haven't verified ourselves
903 * whether it is really the lcn or the deltaxcn that is
904 * -1. So if either is found give us a message so we
905 * can investigate it further!
907 if (vol->major_ver < 3) {
908 if (deltaxcn == (LCN)-1)
909 ntfs_log_debug("lcn delta == -1\n");
910 if (lcn == (LCN)-1)
911 ntfs_log_debug("lcn == -1\n");
913 #endif
914 /* Check lcn is not below -1. */
915 if (lcn < (LCN)-1) {
916 ntfs_log_debug("Invalid LCN < -1 in mapping pairs "
917 "array.\n");
918 goto err_out;
920 /* Enter the current lcn into the runlist element. */
921 rl[rlpos].lcn = lcn;
923 /* Get to the next runlist element. */
924 rlpos++;
925 /* Increment the buffer position to the next mapping pair. */
926 buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
928 if (buf >= attr_end)
929 goto io_error;
931 * If there is a highest_vcn specified, it must be equal to the final
932 * vcn in the runlist - 1, or something has gone badly wrong.
934 deltaxcn = sle64_to_cpu(attr->highest_vcn);
935 if (deltaxcn && vcn - 1 != deltaxcn) {
936 mpa_err:
937 ntfs_log_debug("Corrupt mapping pairs array in non-resident "
938 "attribute.\n");
939 goto err_out;
941 /* Setup not mapped runlist element if this is the base extent. */
942 if (!attr->lowest_vcn) {
943 VCN max_cluster;
945 max_cluster = ((sle64_to_cpu(attr->allocated_size) +
946 vol->cluster_size - 1) >>
947 vol->cluster_size_bits) - 1;
949 * A highest_vcn of zero means this is a single extent
950 * attribute so simply terminate the runlist with LCN_ENOENT).
952 if (deltaxcn) {
954 * If there is a difference between the highest_vcn and
955 * the highest cluster, the runlist is either corrupt
956 * or, more likely, there are more extents following
957 * this one.
959 if (deltaxcn < max_cluster) {
960 ntfs_log_debug("More extents to follow; deltaxcn = "
961 "0x%llx, max_cluster = 0x%llx\n",
962 (long long)deltaxcn,
963 (long long)max_cluster);
964 rl[rlpos].vcn = vcn;
965 vcn += rl[rlpos].length = max_cluster - deltaxcn;
966 rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED;
967 rlpos++;
968 } else if (deltaxcn > max_cluster) {
969 ntfs_log_debug("Corrupt attribute. deltaxcn = "
970 "0x%llx, max_cluster = 0x%llx\n",
971 (long long)deltaxcn,
972 (long long)max_cluster);
973 goto mpa_err;
976 rl[rlpos].lcn = (LCN)LCN_ENOENT;
977 } else /* Not the base extent. There may be more extents to follow. */
978 rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED;
980 /* Setup terminating runlist element. */
981 rl[rlpos].vcn = vcn;
982 rl[rlpos].length = (s64)0;
983 /* If no existing runlist was specified, we are done. */
984 if (!old_rl) {
985 ntfs_log_debug("Mapping pairs array successfully decompressed:\n");
986 ntfs_debug_runlist_dump(rl);
987 return rl;
989 /* Now combine the new and old runlists checking for overlaps. */
990 old_rl = ntfs_runlists_merge(old_rl, rl);
991 if (old_rl)
992 return old_rl;
993 err = errno;
994 free(rl);
995 ntfs_log_debug("Failed to merge runlists.\n");
996 errno = err;
997 return NULL;
998 io_error:
999 ntfs_log_debug("Corrupt attribute.\n");
1000 err_out:
1001 free(rl);
1002 errno = EIO;
1003 return NULL;
1006 runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
1007 const ATTR_RECORD *attr, runlist_element *old_rl)
1009 runlist_element *rle;
1011 ntfs_log_enter("Entering\n");
1012 rle = ntfs_mapping_pairs_decompress_i(vol, attr, old_rl);
1013 ntfs_log_leave("\n");
1014 return rle;
1018 * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
1019 * @rl: runlist to use for conversion
1020 * @vcn: vcn to convert
1022 * Convert the virtual cluster number @vcn of an attribute into a logical
1023 * cluster number (lcn) of a device using the runlist @rl to map vcns to their
1024 * corresponding lcns.
1026 * Since lcns must be >= 0, we use negative return values with special meaning:
1028 * Return value Meaning / Description
1029 * ==================================================
1030 * -1 = LCN_HOLE Hole / not allocated on disk.
1031 * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been
1032 * inserted into the runlist yet.
1033 * -3 = LCN_ENOENT There is no such vcn in the attribute.
1034 * -4 = LCN_EINVAL Input parameter error.
1036 LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
1038 int i;
1040 if (vcn < (VCN)0)
1041 return (LCN)LCN_EINVAL;
1043 * If rl is NULL, assume that we have found an unmapped runlist. The
1044 * caller can then attempt to map it and fail appropriately if
1045 * necessary.
1047 if (!rl)
1048 return (LCN)LCN_RL_NOT_MAPPED;
1050 /* Catch out of lower bounds vcn. */
1051 if (vcn < rl[0].vcn)
1052 return (LCN)LCN_ENOENT;
1054 for (i = 0; rl[i].length; i++) {
1055 if (vcn < rl[i+1].vcn) {
1056 if (rl[i].lcn >= (LCN)0)
1057 return rl[i].lcn + (vcn - rl[i].vcn);
1058 return rl[i].lcn;
1062 * The terminator element is setup to the correct value, i.e. one of
1063 * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
1065 if (rl[i].lcn < (LCN)0)
1066 return rl[i].lcn;
1067 /* Just in case... We could replace this with BUG() some day. */
1068 return (LCN)LCN_ENOENT;
1072 * ntfs_rl_pread - gather read from disk
1073 * @vol: ntfs volume to read from
1074 * @rl: runlist specifying where to read the data from
1075 * @pos: byte position within runlist @rl at which to begin the read
1076 * @count: number of bytes to read
1077 * @b: data buffer into which to read from disk
1079 * This function will read @count bytes from the volume @vol to the data buffer
1080 * @b gathering the data as specified by the runlist @rl. The read begins at
1081 * offset @pos into the runlist @rl.
1083 * On success, return the number of successfully read bytes. If this number is
1084 * lower than @count this means that the read reached end of file or that an
1085 * error was encountered during the read so that the read is partial. 0 means
1086 * nothing was read (also return 0 when @count is 0).
1088 * On error and nothing has been read, return -1 with errno set appropriately
1089 * to the return code of ntfs_pread(), or to EINVAL in case of invalid
1090 * arguments.
1092 * NOTE: If we encounter EOF while reading we return EIO because we assume that
1093 * the run list must point to valid locations within the ntfs volume.
1095 s64 ntfs_rl_pread(const ntfs_volume *vol, const runlist_element *rl,
1096 const s64 pos, s64 count, void *b)
1098 s64 bytes_read, to_read, ofs, total;
1099 int err = EIO;
1101 if (!vol || !rl || pos < 0 || count < 0) {
1102 errno = EINVAL;
1103 ntfs_log_perror("Failed to read runlist [vol: %p rl: %p "
1104 "pos: %lld count: %lld]", vol, rl,
1105 (long long)pos, (long long)count);
1106 return -1;
1108 if (!count)
1109 return count;
1110 /* Seek in @rl to the run containing @pos. */
1111 for (ofs = 0; rl->length && (ofs + (rl->length <<
1112 vol->cluster_size_bits) <= pos); rl++)
1113 ofs += (rl->length << vol->cluster_size_bits);
1114 /* Offset in the run at which to begin reading. */
1115 ofs = pos - ofs;
1116 for (total = 0LL; count; rl++, ofs = 0) {
1117 if (!rl->length)
1118 goto rl_err_out;
1119 if (rl->lcn < (LCN)0) {
1120 if (rl->lcn != (LCN)LCN_HOLE)
1121 goto rl_err_out;
1122 /* It is a hole. Just fill buffer @b with zeroes. */
1123 to_read = min(count, (rl->length <<
1124 vol->cluster_size_bits) - ofs);
1125 memset(b, 0, to_read);
1126 /* Update counters and proceed with next run. */
1127 total += to_read;
1128 count -= to_read;
1129 b = (u8*)b + to_read;
1130 continue;
1132 /* It is a real lcn, read it from the volume. */
1133 to_read = min(count, (rl->length << vol->cluster_size_bits) -
1134 ofs);
1135 retry:
1136 bytes_read = ntfs_pread(vol->dev, (rl->lcn <<
1137 vol->cluster_size_bits) + ofs, to_read, b);
1138 /* If everything ok, update progress counters and continue. */
1139 if (bytes_read > 0) {
1140 total += bytes_read;
1141 count -= bytes_read;
1142 b = (u8*)b + bytes_read;
1143 continue;
1145 /* If the syscall was interrupted, try again. */
1146 if (bytes_read == (s64)-1 && errno == EINTR)
1147 goto retry;
1148 if (bytes_read == (s64)-1)
1149 err = errno;
1150 goto rl_err_out;
1152 /* Finally, return the number of bytes read. */
1153 return total;
1154 rl_err_out:
1155 if (total)
1156 return total;
1157 errno = err;
1158 return -1;
1162 * ntfs_rl_pwrite - scatter write to disk
1163 * @vol: ntfs volume to write to
1164 * @rl: runlist entry specifying where to write the data to
1165 * @ofs: offset in file for runlist element indicated in @rl
1166 * @pos: byte position from runlist beginning at which to begin the write
1167 * @count: number of bytes to write
1168 * @b: data buffer to write to disk
1170 * This function will write @count bytes from data buffer @b to the volume @vol
1171 * scattering the data as specified by the runlist @rl. The write begins at
1172 * offset @pos into the runlist @rl. If a run is sparse then the related buffer
1173 * data is ignored which means that the caller must ensure they are consistent.
1175 * On success, return the number of successfully written bytes. If this number
1176 * is lower than @count this means that the write has been interrupted in
1177 * flight or that an error was encountered during the write so that the write
1178 * is partial. 0 means nothing was written (also return 0 when @count is 0).
1180 * On error and nothing has been written, return -1 with errno set
1181 * appropriately to the return code of ntfs_pwrite(), or to to EINVAL in case
1182 * of invalid arguments.
1184 s64 ntfs_rl_pwrite(const ntfs_volume *vol, const runlist_element *rl,
1185 s64 ofs, const s64 pos, s64 count, void *b)
1187 s64 written, to_write, total = 0;
1188 int err = EIO;
1190 if (!vol || !rl || pos < 0 || count < 0) {
1191 errno = EINVAL;
1192 ntfs_log_perror("Failed to write runlist [vol: %p rl: %p "
1193 "pos: %lld count: %lld]", vol, rl,
1194 (long long)pos, (long long)count);
1195 goto errno_set;
1197 if (!count)
1198 goto out;
1199 /* Seek in @rl to the run containing @pos. */
1200 while (rl->length && (ofs + (rl->length <<
1201 vol->cluster_size_bits) <= pos)) {
1202 ofs += (rl->length << vol->cluster_size_bits);
1203 rl++;
1205 /* Offset in the run at which to begin writing. */
1206 ofs = pos - ofs;
1207 for (total = 0LL; count; rl++, ofs = 0) {
1208 if (!rl->length)
1209 goto rl_err_out;
1210 if (rl->lcn < (LCN)0) {
1212 if (rl->lcn != (LCN)LCN_HOLE)
1213 goto rl_err_out;
1215 to_write = min(count, (rl->length <<
1216 vol->cluster_size_bits) - ofs);
1218 total += to_write;
1219 count -= to_write;
1220 b = (u8*)b + to_write;
1221 continue;
1223 /* It is a real lcn, write it to the volume. */
1224 to_write = min(count, (rl->length << vol->cluster_size_bits) -
1225 ofs);
1226 retry:
1227 if (!NVolReadOnly(vol))
1228 written = ntfs_pwrite(vol->dev, (rl->lcn <<
1229 vol->cluster_size_bits) + ofs,
1230 to_write, b);
1231 else
1232 written = to_write;
1233 /* If everything ok, update progress counters and continue. */
1234 if (written > 0) {
1235 total += written;
1236 count -= written;
1237 b = (u8*)b + written;
1238 continue;
1240 /* If the syscall was interrupted, try again. */
1241 if (written == (s64)-1 && errno == EINTR)
1242 goto retry;
1243 if (written == (s64)-1)
1244 err = errno;
1245 goto rl_err_out;
1247 out:
1248 return total;
1249 rl_err_out:
1250 if (total)
1251 goto out;
1252 errno = err;
1253 errno_set:
1254 total = -1;
1255 goto out;
1259 * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
1260 * @n: number for which to get the number of bytes for
1262 * Return the number of bytes required to store @n unambiguously as
1263 * a signed number.
1265 * This is used in the context of the mapping pairs array to determine how
1266 * many bytes will be needed in the array to store a given logical cluster
1267 * number (lcn) or a specific run length.
1269 * Return the number of bytes written. This function cannot fail.
1271 int ntfs_get_nr_significant_bytes(const s64 n)
1273 u64 l;
1274 int i;
1276 l = (n < 0 ? ~n : n);
1277 i = 1;
1278 if (l >= 128) {
1279 l >>= 7;
1280 do {
1281 i++;
1282 l >>= 8;
1283 } while (l);
1285 return i;
1289 * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
1290 * @vol: ntfs volume (needed for the ntfs version)
1291 * @rl: runlist for which to determine the size of the mapping pairs
1292 * @start_vcn: vcn at which to start the mapping pairs array
1294 * Walk the runlist @rl and calculate the size in bytes of the mapping pairs
1295 * array corresponding to the runlist @rl, starting at vcn @start_vcn. This
1296 * for example allows us to allocate a buffer of the right size when building
1297 * the mapping pairs array.
1299 * If @rl is NULL, just return 1 (for the single terminator byte).
1301 * Return the calculated size in bytes on success. On error, return -1 with
1302 * errno set to the error code. The following error codes are defined:
1303 * EINVAL - Run list contains unmapped elements. Make sure to only pass
1304 * fully mapped runlists to this function.
1305 * - @start_vcn is invalid.
1306 * EIO - The runlist is corrupt.
1308 int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1309 const runlist_element *rl, const VCN start_vcn, int max_size)
1311 LCN prev_lcn;
1312 int rls;
1314 if (start_vcn < 0) {
1315 ntfs_log_trace("start_vcn %lld (should be >= 0)\n",
1316 (long long) start_vcn);
1317 errno = EINVAL;
1318 goto errno_set;
1320 if (!rl) {
1321 if (start_vcn) {
1322 ntfs_log_trace("rl NULL, start_vcn %lld (should be > 0)\n",
1323 (long long) start_vcn);
1324 errno = EINVAL;
1325 goto errno_set;
1327 rls = 1;
1328 goto out;
1330 /* Skip to runlist element containing @start_vcn. */
1331 while (rl->length && start_vcn >= rl[1].vcn)
1332 rl++;
1333 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn) {
1334 errno = EINVAL;
1335 goto errno_set;
1337 prev_lcn = 0;
1338 /* Always need the terminating zero byte. */
1339 rls = 1;
1340 /* Do the first partial run if present. */
1341 if (start_vcn > rl->vcn) {
1342 s64 delta;
1344 /* We know rl->length != 0 already. */
1345 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1346 goto err_out;
1347 delta = start_vcn - rl->vcn;
1348 /* Header byte + length. */
1349 rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta);
1351 * If the logical cluster number (lcn) denotes a hole and we
1352 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1353 * zero space. On earlier NTFS versions we just store the lcn.
1354 * Note: this assumes that on NTFS 1.2-, holes are stored with
1355 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1357 if (rl->lcn >= 0 || vol->major_ver < 3) {
1358 prev_lcn = rl->lcn;
1359 if (rl->lcn >= 0)
1360 prev_lcn += delta;
1361 /* Change in lcn. */
1362 rls += ntfs_get_nr_significant_bytes(prev_lcn);
1364 /* Go to next runlist element. */
1365 rl++;
1367 /* Do the full runs. */
1368 for (; rl->length && (rls <= max_size); rl++) {
1369 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1370 goto err_out;
1371 /* Header byte + length. */
1372 rls += 1 + ntfs_get_nr_significant_bytes(rl->length);
1374 * If the logical cluster number (lcn) denotes a hole and we
1375 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1376 * zero space. On earlier NTFS versions we just store the lcn.
1377 * Note: this assumes that on NTFS 1.2-, holes are stored with
1378 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1380 if (rl->lcn >= 0 || vol->major_ver < 3) {
1381 /* Change in lcn. */
1382 rls += ntfs_get_nr_significant_bytes(rl->lcn -
1383 prev_lcn);
1384 prev_lcn = rl->lcn;
1387 out:
1388 return rls;
1389 err_out:
1390 if (rl->lcn == LCN_RL_NOT_MAPPED)
1391 errno = EINVAL;
1392 else
1393 errno = EIO;
1394 errno_set:
1395 rls = -1;
1396 goto out;
1400 * ntfs_write_significant_bytes - write the significant bytes of a number
1401 * @dst: destination buffer to write to
1402 * @dst_max: pointer to last byte of destination buffer for bounds checking
1403 * @n: number whose significant bytes to write
1405 * Store in @dst, the minimum bytes of the number @n which are required to
1406 * identify @n unambiguously as a signed number, taking care not to exceed
1407 * @dest_max, the maximum position within @dst to which we are allowed to
1408 * write.
1410 * This is used when building the mapping pairs array of a runlist to compress
1411 * a given logical cluster number (lcn) or a specific run length to the minimum
1412 * size possible.
1414 * Return the number of bytes written on success. On error, i.e. the
1415 * destination buffer @dst is too small, return -1 with errno set ENOSPC.
1417 int ntfs_write_significant_bytes(u8 *dst, const u8 *dst_max, const s64 n)
1419 s64 l = n;
1420 int i;
1422 i = 0;
1423 if (dst > dst_max)
1424 goto err_out;
1425 *dst++ = l;
1426 i++;
1427 while ((l > 0x7f) || (l < -0x80)) {
1428 if (dst > dst_max)
1429 goto err_out;
1430 l >>= 8;
1431 *dst++ = l;
1432 i++;
1434 return i;
1435 err_out:
1436 errno = ENOSPC;
1437 return -1;
1441 * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
1442 * @vol: ntfs volume (needed for the ntfs version)
1443 * @dst: destination buffer to which to write the mapping pairs array
1444 * @dst_len: size of destination buffer @dst in bytes
1445 * @rl: runlist for which to build the mapping pairs array
1446 * @start_vcn: vcn at which to start the mapping pairs array
1447 * @stop_vcn: first vcn outside destination buffer on success or ENOSPC error
1449 * Create the mapping pairs array from the runlist @rl, starting at vcn
1450 * @start_vcn and save the array in @dst. @dst_len is the size of @dst in
1451 * bytes and it should be at least equal to the value obtained by calling
1452 * ntfs_get_size_for_mapping_pairs().
1454 * If @rl is NULL, just write a single terminator byte to @dst.
1456 * On success or ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
1457 * the first vcn outside the destination buffer. Note that on error @dst has
1458 * been filled with all the mapping pairs that will fit, thus it can be treated
1459 * as partial success, in that a new attribute extent needs to be created or the
1460 * next extent has to be used and the mapping pairs build has to be continued
1461 * with @start_vcn set to *@stop_vcn.
1463 * Return 0 on success. On error, return -1 with errno set to the error code.
1464 * The following error codes are defined:
1465 * EINVAL - Run list contains unmapped elements. Make sure to only pass
1466 * fully mapped runlists to this function.
1467 * - @start_vcn is invalid.
1468 * EIO - The runlist is corrupt.
1469 * ENOSPC - The destination buffer is too small.
1471 int ntfs_mapping_pairs_build(const ntfs_volume *vol, u8 *dst,
1472 const int dst_len, const runlist_element *rl,
1473 const VCN start_vcn, runlist_element const **stop_rl)
1475 LCN prev_lcn;
1476 u8 *dst_max, *dst_next;
1477 s8 len_len, lcn_len;
1478 int ret = 0;
1480 if (start_vcn < 0)
1481 goto val_err;
1482 if (!rl) {
1483 if (start_vcn)
1484 goto val_err;
1485 if (stop_rl)
1486 *stop_rl = rl;
1487 if (dst_len < 1)
1488 goto nospc_err;
1489 goto ok;
1491 /* Skip to runlist element containing @start_vcn. */
1492 while (rl->length && start_vcn >= rl[1].vcn)
1493 rl++;
1494 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
1495 goto val_err;
1497 * @dst_max is used for bounds checking in
1498 * ntfs_write_significant_bytes().
1500 dst_max = dst + dst_len - 1;
1501 prev_lcn = 0;
1502 /* Do the first partial run if present. */
1503 if (start_vcn > rl->vcn) {
1504 s64 delta;
1506 /* We know rl->length != 0 already. */
1507 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1508 goto err_out;
1509 delta = start_vcn - rl->vcn;
1510 /* Write length. */
1511 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1512 rl->length - delta);
1513 if (len_len < 0)
1514 goto size_err;
1516 * If the logical cluster number (lcn) denotes a hole and we
1517 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1518 * zero space. On earlier NTFS versions we just write the lcn
1519 * change. FIXME: Do we need to write the lcn change or just
1520 * the lcn in that case? Not sure as I have never seen this
1521 * case on NT4. - We assume that we just need to write the lcn
1522 * change until someone tells us otherwise... (AIA)
1524 if (rl->lcn >= 0 || vol->major_ver < 3) {
1525 prev_lcn = rl->lcn;
1526 if (rl->lcn >= 0)
1527 prev_lcn += delta;
1528 /* Write change in lcn. */
1529 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1530 len_len, dst_max, prev_lcn);
1531 if (lcn_len < 0)
1532 goto size_err;
1533 } else
1534 lcn_len = 0;
1535 dst_next = dst + len_len + lcn_len + 1;
1536 if (dst_next > dst_max)
1537 goto size_err;
1538 /* Update header byte. */
1539 *dst = lcn_len << 4 | len_len;
1540 /* Position at next mapping pairs array element. */
1541 dst = dst_next;
1542 /* Go to next runlist element. */
1543 rl++;
1545 /* Do the full runs. */
1546 for (; rl->length; rl++) {
1547 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1548 goto err_out;
1549 /* Write length. */
1550 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1551 rl->length);
1552 if (len_len < 0)
1553 goto size_err;
1555 * If the logical cluster number (lcn) denotes a hole and we
1556 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1557 * zero space. On earlier NTFS versions we just write the lcn
1558 * change. FIXME: Do we need to write the lcn change or just
1559 * the lcn in that case? Not sure as I have never seen this
1560 * case on NT4. - We assume that we just need to write the lcn
1561 * change until someone tells us otherwise... (AIA)
1563 if (rl->lcn >= 0 || vol->major_ver < 3) {
1564 /* Write change in lcn. */
1565 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1566 len_len, dst_max, rl->lcn - prev_lcn);
1567 if (lcn_len < 0)
1568 goto size_err;
1569 prev_lcn = rl->lcn;
1570 } else
1571 lcn_len = 0;
1572 dst_next = dst + len_len + lcn_len + 1;
1573 if (dst_next > dst_max)
1574 goto size_err;
1575 /* Update header byte. */
1576 *dst = lcn_len << 4 | len_len;
1577 /* Position at next mapping pairs array element. */
1578 dst += 1 + len_len + lcn_len;
1580 /* Set stop vcn. */
1581 if (stop_rl)
1582 *stop_rl = rl;
1583 ok:
1584 /* Add terminator byte. */
1585 *dst = 0;
1586 out:
1587 return ret;
1588 size_err:
1589 /* Set stop vcn. */
1590 if (stop_rl)
1591 *stop_rl = rl;
1592 /* Add terminator byte. */
1593 *dst = 0;
1594 nospc_err:
1595 errno = ENOSPC;
1596 goto errno_set;
1597 val_err:
1598 errno = EINVAL;
1599 goto errno_set;
1600 err_out:
1601 if (rl->lcn == LCN_RL_NOT_MAPPED)
1602 errno = EINVAL;
1603 else
1604 errno = EIO;
1605 errno_set:
1606 ret = -1;
1607 goto out;
1611 * ntfs_rl_truncate - truncate a runlist starting at a specified vcn
1612 * @arl: address of runlist to truncate
1613 * @start_vcn: first vcn which should be cut off
1615 * Truncate the runlist *@arl starting at vcn @start_vcn as well as the memory
1616 * buffer holding the runlist.
1618 * Return 0 on success and -1 on error with errno set to the error code.
1620 * NOTE: @arl is the address of the runlist. We need the address so we can
1621 * modify the pointer to the runlist with the new, reallocated memory buffer.
1623 int ntfs_rl_truncate(runlist **arl, const VCN start_vcn)
1625 runlist *rl;
1626 BOOL is_end = FALSE;
1628 if (!arl || !*arl) {
1629 errno = EINVAL;
1630 if (!arl)
1631 ntfs_log_perror("rl_truncate error: arl: %p", arl);
1632 else
1633 ntfs_log_perror("rl_truncate error:"
1634 " arl: %p *arl: %p", arl, *arl);
1635 return -1;
1638 rl = *arl;
1640 if (start_vcn < rl->vcn) {
1641 errno = EINVAL;
1642 ntfs_log_perror("Start_vcn lies outside front of runlist");
1643 return -1;
1646 /* Find the starting vcn in the run list. */
1647 while (rl->length) {
1648 if (start_vcn < rl[1].vcn)
1649 break;
1650 rl++;
1653 if (!rl->length) {
1654 errno = EIO;
1655 ntfs_log_trace("Truncating already truncated runlist?\n");
1656 return -1;
1659 /* Truncate the run. */
1660 rl->length = start_vcn - rl->vcn;
1663 * If a run was partially truncated, make the following runlist
1664 * element a terminator instead of the truncated runlist
1665 * element itself.
1667 if (rl->length) {
1668 ++rl;
1669 if (!rl->length)
1670 is_end = TRUE;
1671 rl->vcn = start_vcn;
1672 rl->length = 0;
1674 rl->lcn = (LCN)LCN_ENOENT;
1676 * Reallocate memory if necessary.
1677 * FIXME: Below code is broken, because runlist allocations must be
1678 * a multiply of 4096. The code caused crashes and corruptions.
1681 if (!is_end) {
1682 size_t new_size = (rl - *arl + 1) * sizeof(runlist_element);
1683 rl = realloc(*arl, new_size);
1684 if (rl)
1685 *arl = rl;
1688 return 0;
1692 * ntfs_rl_sparse - check whether runlist have sparse regions or not.
1693 * @rl: runlist to check
1695 * Return 1 if have, 0 if not, -1 on error with errno set to the error code.
1697 int ntfs_rl_sparse(runlist *rl)
1699 runlist *rlc;
1701 if (!rl) {
1702 errno = EINVAL;
1703 ntfs_log_perror("%s: ", __FUNCTION__);
1704 return -1;
1707 for (rlc = rl; rlc->length; rlc++)
1708 if (rlc->lcn < 0) {
1709 if (rlc->lcn != LCN_HOLE) {
1710 errno = EINVAL;
1711 ntfs_log_perror("%s: bad runlist", __FUNCTION__);
1712 return -1;
1714 return 1;
1716 return 0;
1720 * ntfs_rl_get_compressed_size - calculate length of non sparse regions
1721 * @vol: ntfs volume (need for cluster size)
1722 * @rl: runlist to calculate for
1724 * Return compressed size or -1 on error with errno set to the error code.
1726 s64 ntfs_rl_get_compressed_size(ntfs_volume *vol, runlist *rl)
1728 runlist *rlc;
1729 s64 ret = 0;
1731 if (!rl) {
1732 errno = EINVAL;
1733 ntfs_log_perror("%s: ", __FUNCTION__);
1734 return -1;
1737 for (rlc = rl; rlc->length; rlc++) {
1738 if (rlc->lcn < 0) {
1739 if (rlc->lcn != LCN_HOLE) {
1740 errno = EINVAL;
1741 ntfs_log_perror("%s: bad runlist", __FUNCTION__);
1742 return -1;
1744 } else
1745 ret += rlc->length;
1747 return ret << vol->cluster_size_bits;
1751 #ifdef NTFS_TEST
1753 * test_rl_helper
1755 #define MKRL(R,V,L,S) \
1756 (R)->vcn = V; \
1757 (R)->lcn = L; \
1758 (R)->length = S;
1763 * test_rl_dump_runlist - Runlist test: Display the contents of a runlist
1764 * @rl:
1766 * Description...
1768 * Returns:
1770 static void test_rl_dump_runlist(const runlist_element *rl)
1772 int abbr = 0; /* abbreviate long lists */
1773 int len = 0;
1774 int i;
1775 const char *lcn_str[5] = { "HOLE", "NOTMAP", "ENOENT", "XXXX" };
1777 if (!rl) {
1778 printf(" Run list not present.\n");
1779 return;
1782 if (abbr)
1783 for (len = 0; rl[len].length; len++) ;
1785 printf(" VCN LCN len\n");
1786 for (i = 0; ; i++, rl++) {
1787 LCN lcn = rl->lcn;
1789 if ((abbr) && (len > 20)) {
1790 if (i == 4)
1791 printf(" ...\n");
1792 if ((i > 3) && (i < (len - 3)))
1793 continue;
1796 if (lcn < (LCN)0) {
1797 int ind = -lcn - 1;
1799 if (ind > -LCN_ENOENT - 1)
1800 ind = 3;
1801 printf("%8lld %8s %8lld\n",
1802 rl->vcn, lcn_str[ind], rl->length);
1803 } else
1804 printf("%8lld %8lld %8lld\n",
1805 rl->vcn, rl->lcn, rl->length);
1806 if (!rl->length)
1807 break;
1809 if ((abbr) && (len > 20))
1810 printf(" (%d entries)\n", len+1);
1811 printf("\n");
1815 * test_rl_runlists_merge - Runlist test: Merge two runlists
1816 * @drl:
1817 * @srl:
1819 * Description...
1821 * Returns:
1823 static runlist_element * test_rl_runlists_merge(runlist_element *drl, runlist_element *srl)
1825 runlist_element *res = NULL;
1827 printf("dst:\n");
1828 test_rl_dump_runlist(drl);
1829 printf("src:\n");
1830 test_rl_dump_runlist(srl);
1832 res = ntfs_runlists_merge(drl, srl);
1834 printf("res:\n");
1835 test_rl_dump_runlist(res);
1837 return res;
1841 * test_rl_read_buffer - Runlist test: Read a file containing a runlist
1842 * @file:
1843 * @buf:
1844 * @bufsize:
1846 * Description...
1848 * Returns:
1850 static int test_rl_read_buffer(const char *file, u8 *buf, int bufsize)
1852 FILE *fptr;
1854 fptr = fopen(file, "r");
1855 if (!fptr) {
1856 printf("open %s\n", file);
1857 return 0;
1860 if (fread(buf, bufsize, 1, fptr) == 99) {
1861 printf("read %s\n", file);
1862 return 0;
1865 fclose(fptr);
1866 return 1;
1870 * test_rl_pure_src - Runlist test: Complicate the simple tests a little
1871 * @contig:
1872 * @multi:
1873 * @vcn:
1874 * @len:
1876 * Description...
1878 * Returns:
1880 static runlist_element * test_rl_pure_src(BOOL contig, BOOL multi, int vcn, int len)
1882 runlist_element *result;
1883 int fudge;
1885 if (contig)
1886 fudge = 0;
1887 else
1888 fudge = 999;
1890 result = ntfs_malloc(4096);
1891 if (!result)
1892 return NULL;
1894 if (multi) {
1895 MKRL(result+0, vcn + (0*len/4), fudge + vcn + 1000 + (0*len/4), len / 4)
1896 MKRL(result+1, vcn + (1*len/4), fudge + vcn + 1000 + (1*len/4), len / 4)
1897 MKRL(result+2, vcn + (2*len/4), fudge + vcn + 1000 + (2*len/4), len / 4)
1898 MKRL(result+3, vcn + (3*len/4), fudge + vcn + 1000 + (3*len/4), len / 4)
1899 MKRL(result+4, vcn + (4*len/4), LCN_RL_NOT_MAPPED, 0)
1900 } else {
1901 MKRL(result+0, vcn, fudge + vcn + 1000, len)
1902 MKRL(result+1, vcn + len, LCN_RL_NOT_MAPPED, 0)
1904 return result;
1908 * test_rl_pure_test - Runlist test: Perform tests using simple runlists
1909 * @test:
1910 * @contig:
1911 * @multi:
1912 * @vcn:
1913 * @len:
1914 * @file:
1915 * @size:
1917 * Description...
1919 * Returns:
1921 static void test_rl_pure_test(int test, BOOL contig, BOOL multi, int vcn, int len, runlist_element *file, int size)
1923 runlist_element *src;
1924 runlist_element *dst;
1925 runlist_element *res;
1927 src = test_rl_pure_src(contig, multi, vcn, len);
1928 dst = ntfs_malloc(4096);
1929 if (!src || !dst) {
1930 printf("Test %2d ---------- FAILED! (no free memory?)\n", test);
1931 return;
1934 memcpy(dst, file, size);
1936 printf("Test %2d ----------\n", test);
1937 res = test_rl_runlists_merge(dst, src);
1939 free(res);
1943 * test_rl_pure - Runlist test: Create tests using simple runlists
1944 * @contig:
1945 * @multi:
1947 * Description...
1949 * Returns:
1951 static void test_rl_pure(char *contig, char *multi)
1953 /* VCN, LCN, len */
1954 static runlist_element file1[] = {
1955 { 0, -1, 100 }, /* HOLE */
1956 { 100, 1100, 100 }, /* DATA */
1957 { 200, -1, 100 }, /* HOLE */
1958 { 300, 1300, 100 }, /* DATA */
1959 { 400, -1, 100 }, /* HOLE */
1960 { 500, -3, 0 } /* NOENT */
1962 static runlist_element file2[] = {
1963 { 0, 1000, 100 }, /* DATA */
1964 { 100, -1, 100 }, /* HOLE */
1965 { 200, -3, 0 } /* NOENT */
1967 static runlist_element file3[] = {
1968 { 0, 1000, 100 }, /* DATA */
1969 { 100, -3, 0 } /* NOENT */
1971 static runlist_element file4[] = {
1972 { 0, -3, 0 } /* NOENT */
1974 static runlist_element file5[] = {
1975 { 0, -2, 100 }, /* NOTMAP */
1976 { 100, 1100, 100 }, /* DATA */
1977 { 200, -2, 100 }, /* NOTMAP */
1978 { 300, 1300, 100 }, /* DATA */
1979 { 400, -2, 100 }, /* NOTMAP */
1980 { 500, -3, 0 } /* NOENT */
1982 static runlist_element file6[] = {
1983 { 0, 1000, 100 }, /* DATA */
1984 { 100, -2, 100 }, /* NOTMAP */
1985 { 200, -3, 0 } /* NOENT */
1987 BOOL c, m;
1989 if (strcmp(contig, "contig") == 0)
1990 c = TRUE;
1991 else if (strcmp(contig, "noncontig") == 0)
1992 c = FALSE;
1993 else {
1994 printf("rl pure [contig|noncontig] [single|multi]\n");
1995 return;
1997 if (strcmp(multi, "multi") == 0)
1998 m = TRUE;
1999 else if (strcmp(multi, "single") == 0)
2000 m = FALSE;
2001 else {
2002 printf("rl pure [contig|noncontig] [single|multi]\n");
2003 return;
2006 test_rl_pure_test(1, c, m, 0, 40, file1, sizeof(file1));
2007 test_rl_pure_test(2, c, m, 40, 40, file1, sizeof(file1));
2008 test_rl_pure_test(3, c, m, 60, 40, file1, sizeof(file1));
2009 test_rl_pure_test(4, c, m, 0, 100, file1, sizeof(file1));
2010 test_rl_pure_test(5, c, m, 200, 40, file1, sizeof(file1));
2011 test_rl_pure_test(6, c, m, 240, 40, file1, sizeof(file1));
2012 test_rl_pure_test(7, c, m, 260, 40, file1, sizeof(file1));
2013 test_rl_pure_test(8, c, m, 200, 100, file1, sizeof(file1));
2014 test_rl_pure_test(9, c, m, 400, 40, file1, sizeof(file1));
2015 test_rl_pure_test(10, c, m, 440, 40, file1, sizeof(file1));
2016 test_rl_pure_test(11, c, m, 460, 40, file1, sizeof(file1));
2017 test_rl_pure_test(12, c, m, 400, 100, file1, sizeof(file1));
2018 test_rl_pure_test(13, c, m, 160, 100, file2, sizeof(file2));
2019 test_rl_pure_test(14, c, m, 100, 140, file2, sizeof(file2));
2020 test_rl_pure_test(15, c, m, 200, 40, file2, sizeof(file2));
2021 test_rl_pure_test(16, c, m, 240, 40, file2, sizeof(file2));
2022 test_rl_pure_test(17, c, m, 100, 40, file3, sizeof(file3));
2023 test_rl_pure_test(18, c, m, 140, 40, file3, sizeof(file3));
2024 test_rl_pure_test(19, c, m, 0, 40, file4, sizeof(file4));
2025 test_rl_pure_test(20, c, m, 40, 40, file4, sizeof(file4));
2026 test_rl_pure_test(21, c, m, 0, 40, file5, sizeof(file5));
2027 test_rl_pure_test(22, c, m, 40, 40, file5, sizeof(file5));
2028 test_rl_pure_test(23, c, m, 60, 40, file5, sizeof(file5));
2029 test_rl_pure_test(24, c, m, 0, 100, file5, sizeof(file5));
2030 test_rl_pure_test(25, c, m, 200, 40, file5, sizeof(file5));
2031 test_rl_pure_test(26, c, m, 240, 40, file5, sizeof(file5));
2032 test_rl_pure_test(27, c, m, 260, 40, file5, sizeof(file5));
2033 test_rl_pure_test(28, c, m, 200, 100, file5, sizeof(file5));
2034 test_rl_pure_test(29, c, m, 400, 40, file5, sizeof(file5));
2035 test_rl_pure_test(30, c, m, 440, 40, file5, sizeof(file5));
2036 test_rl_pure_test(31, c, m, 460, 40, file5, sizeof(file5));
2037 test_rl_pure_test(32, c, m, 400, 100, file5, sizeof(file5));
2038 test_rl_pure_test(33, c, m, 160, 100, file6, sizeof(file6));
2039 test_rl_pure_test(34, c, m, 100, 140, file6, sizeof(file6));
2043 * test_rl_zero - Runlist test: Merge a zero-length runlist
2045 * Description...
2047 * Returns:
2049 static void test_rl_zero(void)
2051 runlist_element *jim = NULL;
2052 runlist_element *bob = NULL;
2054 bob = calloc(3, sizeof(runlist_element));
2055 if (!bob)
2056 return;
2058 MKRL(bob+0, 10, 99, 5)
2059 MKRL(bob+1, 15, LCN_RL_NOT_MAPPED, 0)
2061 jim = test_rl_runlists_merge(jim, bob);
2062 if (!jim)
2063 return;
2065 free(jim);
2069 * test_rl_frag_combine - Runlist test: Perform tests using fragmented files
2070 * @vol:
2071 * @attr1:
2072 * @attr2:
2073 * @attr3:
2075 * Description...
2077 * Returns:
2079 static void test_rl_frag_combine(ntfs_volume *vol, ATTR_RECORD *attr1, ATTR_RECORD *attr2, ATTR_RECORD *attr3)
2081 runlist_element *run1;
2082 runlist_element *run2;
2083 runlist_element *run3;
2085 run1 = ntfs_mapping_pairs_decompress(vol, attr1, NULL);
2086 if (!run1)
2087 return;
2089 run2 = ntfs_mapping_pairs_decompress(vol, attr2, NULL);
2090 if (!run2)
2091 return;
2093 run1 = test_rl_runlists_merge(run1, run2);
2095 run3 = ntfs_mapping_pairs_decompress(vol, attr3, NULL);
2096 if (!run3)
2097 return;
2099 run1 = test_rl_runlists_merge(run1, run3);
2101 free(run1);
2105 * test_rl_frag - Runlist test: Create tests using very fragmented files
2106 * @test:
2108 * Description...
2110 * Returns:
2112 static void test_rl_frag(char *test)
2114 ntfs_volume vol;
2115 ATTR_RECORD *attr1 = ntfs_malloc(1024);
2116 ATTR_RECORD *attr2 = ntfs_malloc(1024);
2117 ATTR_RECORD *attr3 = ntfs_malloc(1024);
2119 if (!attr1 || !attr2 || !attr3)
2120 goto out;
2122 vol.sb = NULL;
2123 vol.sector_size_bits = 9;
2124 vol.cluster_size = 2048;
2125 vol.cluster_size_bits = 11;
2126 vol.major_ver = 3;
2128 if (!test_rl_read_buffer("runlist-data/attr1.bin", (u8*) attr1, 1024))
2129 goto out;
2130 if (!test_rl_read_buffer("runlist-data/attr2.bin", (u8*) attr2, 1024))
2131 goto out;
2132 if (!test_rl_read_buffer("runlist-data/attr3.bin", (u8*) attr3, 1024))
2133 goto out;
2135 if (strcmp(test, "123") == 0) test_rl_frag_combine(&vol, attr1, attr2, attr3);
2136 else if (strcmp(test, "132") == 0) test_rl_frag_combine(&vol, attr1, attr3, attr2);
2137 else if (strcmp(test, "213") == 0) test_rl_frag_combine(&vol, attr2, attr1, attr3);
2138 else if (strcmp(test, "231") == 0) test_rl_frag_combine(&vol, attr2, attr3, attr1);
2139 else if (strcmp(test, "312") == 0) test_rl_frag_combine(&vol, attr3, attr1, attr2);
2140 else if (strcmp(test, "321") == 0) test_rl_frag_combine(&vol, attr3, attr2, attr1);
2141 else
2142 printf("Frag: No such test '%s'\n", test);
2144 out:
2145 free(attr1);
2146 free(attr2);
2147 free(attr3);
2151 * test_rl_main - Runlist test: Program start (main)
2152 * @argc:
2153 * @argv:
2155 * Description...
2157 * Returns:
2159 int test_rl_main(int argc, char *argv[])
2161 if ((argc == 2) && (strcmp(argv[1], "zero") == 0)) test_rl_zero();
2162 else if ((argc == 3) && (strcmp(argv[1], "frag") == 0)) test_rl_frag(argv[2]);
2163 else if ((argc == 4) && (strcmp(argv[1], "pure") == 0)) test_rl_pure(argv[2], argv[3]);
2164 else
2165 printf("rl [zero|frag|pure] {args}\n");
2167 return 0;
2170 #endif