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
53 * ntfs_rl_mm - runlist memmove
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
));
70 * ntfs_rl_mc - runlist memory copy
81 static void ntfs_rl_mc(runlist_element
*dstbase
, int dst
,
82 runlist_element
*srcbase
, int src
, int size
)
85 memcpy(dstbase
+ dst
, srcbase
+ src
, size
* sizeof(*dstbase
));
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
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
,
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
)
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
,
126 runlist_element
*newrl
;
131 irl
= (int)(rl
- na
->rl
);
133 while (na
->rl
[last
].length
)
135 newrl
= ntfs_rl_realloc(na
->rl
,last
+1,last
+more_entries
+1);
138 rl
= (runlist_element
*)NULL
;
143 ntfs_log_error("Cannot extend unmapped runlist");
145 rl
= (runlist_element
*)NULL
;
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
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
)
164 ntfs_log_debug("Eeek. ntfs_rl_are_mergeable() invoked with NULL "
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
))
172 /* If the runs are misaligned, we cannot merge them. */
173 if ((dst
->vcn
+ dst
->length
) != src
->vcn
)
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
))
179 /* If we are merging two holes, we can merge them. */
180 if ((dst
->lcn
== LCN_HOLE
) && (src
->lcn
== LCN_HOLE
))
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
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
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 */
227 ntfs_log_debug("Eeek. ntfs_rl_append() invoked with 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
);
242 * We are guaranteed to succeed from here so can start modifying the
246 /* First, merge the right hand end, if necessary. */
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
;
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
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 */
295 ntfs_log_debug("Eeek. ntfs_rl_insert() invoked with 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.
305 disc
= (src
[0].vcn
> 0);
309 left
= ntfs_rl_are_mergeable(dst
+ loc
- 1, src
);
311 merged_length
= dst
[loc
- 1].length
;
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
);
325 * We are guaranteed to succeed from here so can start modifying the
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. */
353 dst
[loc
].vcn
= dst
[loc
- 1].vcn
+ dst
[loc
- 1].length
;
354 dst
[loc
].length
= dst
[loc
+ 1].vcn
- dst
[loc
].vcn
;
357 dst
[loc
].length
= dst
[loc
+ 1].vcn
;
359 dst
[loc
].lcn
= LCN_RL_NOT_MAPPED
;
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
383 static runlist_element
*ntfs_rl_replace(runlist_element
*dst
, int dsize
,
384 runlist_element
*src
, int ssize
,
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 */
394 ntfs_log_debug("Eeek. ntfs_rl_replace() invoked with 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);
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
;
411 dst
= ntfs_rl_realloc(dst
, dsize
, dsize
+ delta
);
416 * We are guaranteed to succeed from here so can start modifying the
420 /* First, merge the left and right ends, if necessary. */
422 __ntfs_rl_merge(src
+ ssize
- 1, dst
+ loc
+ 1);
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
;
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
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
471 static runlist_element
*ntfs_rl_split(runlist_element
*dst
, int dsize
,
472 runlist_element
*src
, int ssize
, int loc
)
475 ntfs_log_debug("Eeek. ntfs_rl_split() invoked with NULL pointer!\n");
480 /* Space required: @dst size + @src size + one new hole. */
481 dst
= ntfs_rl_realloc(dst
, dsize
, dsize
+ ssize
+ 1);
485 * We are guaranteed to succeed from here so can start modifying the
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
;
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
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... */
526 /* Check for the case where the first mapping is being done now. */
529 /* Complete the source runlist if necessary. */
531 /* Scan to the end of the source runlist. */
532 for (dend
= 0; drl
[dend
].length
; dend
++)
535 drl
= ntfs_rl_realloc(drl
, dend
, dend
+ 1);
538 /* Insert start element at the front of the runlist. */
539 ntfs_rl_mm(drl
, 1, 0, dend
);
541 drl
[0].lcn
= LCN_RL_NOT_MAPPED
;
542 drl
[0].length
= drl
[1].vcn
;
549 /* Skip any unmapped start element(s) in the source runlist. */
550 while (srl
[si
].length
&& srl
[si
].lcn
< (LCN
)LCN_HOLE
)
553 /* Can't have an entirely unmapped source runlist. */
554 if (!srl
[si
].length
) {
556 ntfs_log_perror("%s: unmapped source runlist", __FUNCTION__
);
560 /* Record the starting points. */
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
568 for (; drl
[di
].length
; di
++) {
569 if (drl
[di
].vcn
+ drl
[di
].length
> srl
[sstart
].vcn
)
574 /* Sanity check for illegal overlaps. */
575 if ((drl
[di
].vcn
== srl
[si
].vcn
) && (drl
[di
].lcn
>= 0) &&
576 (srl
[si
].lcn
>= 0)) {
578 ntfs_log_perror("Run lists overlap. Cannot merge");
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
--)
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
)
612 if (marker
&& (drl
[dins
].vcn
+ drl
[dins
].length
> srl
[send
- 1].vcn
))
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
);
622 drl
= ntfs_rl_replace(drl
, ds
, srl
+ sstart
, ss
, dins
);
624 drl
= ntfs_rl_insert(drl
, ds
, srl
+ sstart
, ss
, dins
);
627 drl
= ntfs_rl_append(drl
, ds
, srl
+ sstart
, ss
, dins
);
629 drl
= ntfs_rl_split(drl
, ds
, srl
+ sstart
, ss
, dins
);
632 ntfs_log_perror("Merge failed");
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
) {
644 if (drl
[ds
].vcn
== marker_vcn
) {
645 ntfs_log_debug("Old marker = %lli, replacing with "
647 (long long)drl
[ds
].lcn
);
648 drl
[ds
].lcn
= (LCN
)LCN_ENOENT
;
652 * We need to create an unmapped runlist element in
653 * @drl or extend an existing one before adding the
656 if (drl
[ds
].lcn
== (LCN
)LCN_ENOENT
) {
660 if (drl
[ds
].lcn
!= (LCN
)LCN_RL_NOT_MAPPED
) {
661 /* Add an unmapped runlist element. */
663 /* FIXME/TODO: We need to have the
664 * extra memory already! (AIA)
666 drl
= ntfs_rl_realloc(drl
, ds
, ds
+ 2);
672 /* Need to set vcn if it isn't set already. */
674 drl
[ds
].vcn
= drl
[ds
- 1].vcn
+
676 drl
[ds
].lcn
= (LCN
)LCN_RL_NOT_MAPPED
;
677 /* We now used up a slot. */
680 drl
[ds
].length
= marker_vcn
- drl
[ds
].vcn
;
681 /* Finally add the ENOENT terminator. */
684 /* FIXME/TODO: We need to have the extra
685 * memory already! (AIA)
687 drl
= ntfs_rl_realloc(drl
, ds
, ds
+ 1);
691 drl
[ds
].vcn
= marker_vcn
;
692 drl
[ds
].lcn
= (LCN
)LCN_ENOENT
;
693 drl
[ds
].length
= (s64
)0;
699 /* The merge was completed successfully. */
700 ntfs_log_debug("Merged runlist:\n");
701 ntfs_debug_runlist_dump(drl
);
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
;
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
)
749 ntfs_log_enter("Entering\n");
750 rl
= ntfs_runlists_merge_i(drl
, srl
);
751 ntfs_log_leave("\n");
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
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) {
804 /* Start at vcn = lowest_vcn and lcn 0. */
805 vcn
= sle64_to_cpu(attr
->lowest_vcn
);
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");
815 /* Current position in runlist array. */
817 /* Allocate first 4kiB block and set current runlist size to 4kiB. */
819 rl
= ntfs_malloc(rlsize
);
822 /* Insert unmapped starting element if necessary. */
825 rl
->lcn
= (LCN
)LCN_RL_NOT_MAPPED
;
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
;
838 rl2
= realloc(rl
, rlsize
);
847 /* Enter the current vcn into the current runlist element. */
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...
858 if (buf
+ b
> attr_end
)
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 "
868 * Assume a negative length to indicate data corruption and
869 * hence clean-up and return NULL.
872 ntfs_log_debug("Invalid length in mapping pairs array.\n");
876 * Enter the current run length into the current runlist
879 rl
[rlpos
].length
= deltaxcn
;
880 /* Increment the current vcn by the current run length. */
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
888 rl
[rlpos
].lcn
= (LCN
)LCN_HOLE
;
890 /* Get the lcn change which really can be negative. */
892 b
= b2
+ ((*buf
>> 4) & 0xf);
893 if (buf
+ b
> attr_end
)
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. */
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");
911 ntfs_log_debug("lcn == -1\n");
914 /* Check lcn is not below -1. */
916 ntfs_log_debug("Invalid LCN < -1 in mapping pairs "
920 /* Enter the current lcn into the runlist element. */
923 /* Get to the next runlist element. */
925 /* Increment the buffer position to the next mapping pair. */
926 buf
+= (*buf
& 0xf) + ((*buf
>> 4) & 0xf) + 1;
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
) {
937 ntfs_log_debug("Corrupt mapping pairs array in non-resident "
941 /* Setup not mapped runlist element if this is the base extent. */
942 if (!attr
->lowest_vcn
) {
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).
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
959 if (deltaxcn
< max_cluster
) {
960 ntfs_log_debug("More extents to follow; deltaxcn = "
961 "0x%llx, max_cluster = 0x%llx\n",
963 (long long)max_cluster
);
965 vcn
+= rl
[rlpos
].length
= max_cluster
- deltaxcn
;
966 rl
[rlpos
].lcn
= (LCN
)LCN_RL_NOT_MAPPED
;
968 } else if (deltaxcn
> max_cluster
) {
969 ntfs_log_debug("Corrupt attribute. deltaxcn = "
970 "0x%llx, max_cluster = 0x%llx\n",
972 (long long)max_cluster
);
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. */
982 rl
[rlpos
].length
= (s64
)0;
983 /* If no existing runlist was specified, we are done. */
985 ntfs_log_debug("Mapping pairs array successfully decompressed:\n");
986 ntfs_debug_runlist_dump(rl
);
989 /* Now combine the new and old runlists checking for overlaps. */
990 old_rl
= ntfs_runlists_merge(old_rl
, rl
);
995 ntfs_log_debug("Failed to merge runlists.\n");
999 ntfs_log_debug("Corrupt attribute.\n");
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");
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
)
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
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
);
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)
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
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
;
1101 if (!vol
|| !rl
|| pos
< 0 || count
< 0) {
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
);
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. */
1116 for (total
= 0LL; count
; rl
++, ofs
= 0) {
1119 if (rl
->lcn
< (LCN
)0) {
1120 if (rl
->lcn
!= (LCN
)LCN_HOLE
)
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. */
1129 b
= (u8
*)b
+ to_read
;
1132 /* It is a real lcn, read it from the volume. */
1133 to_read
= min(count
, (rl
->length
<< vol
->cluster_size_bits
) -
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
;
1145 /* If the syscall was interrupted, try again. */
1146 if (bytes_read
== (s64
)-1 && errno
== EINTR
)
1148 if (bytes_read
== (s64
)-1)
1152 /* Finally, return the number of bytes read. */
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;
1190 if (!vol
|| !rl
|| pos
< 0 || count
< 0) {
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
);
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
);
1205 /* Offset in the run at which to begin writing. */
1207 for (total
= 0LL; count
; rl
++, ofs
= 0) {
1210 if (rl
->lcn
< (LCN
)0) {
1212 if (rl
->lcn
!= (LCN
)LCN_HOLE
)
1215 to_write
= min(count
, (rl
->length
<<
1216 vol
->cluster_size_bits
) - ofs
);
1220 b
= (u8
*)b
+ to_write
;
1223 /* It is a real lcn, write it to the volume. */
1224 to_write
= min(count
, (rl
->length
<< vol
->cluster_size_bits
) -
1227 if (!NVolReadOnly(vol
))
1228 written
= ntfs_pwrite(vol
->dev
, (rl
->lcn
<<
1229 vol
->cluster_size_bits
) + ofs
,
1233 /* If everything ok, update progress counters and continue. */
1237 b
= (u8
*)b
+ written
;
1240 /* If the syscall was interrupted, try again. */
1241 if (written
== (s64
)-1 && errno
== EINTR
)
1243 if (written
== (s64
)-1)
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
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
)
1276 l
= (n
< 0 ? ~n
: n
);
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
)
1314 if (start_vcn
< 0) {
1315 ntfs_log_trace("start_vcn %lld (should be >= 0)\n",
1316 (long long) start_vcn
);
1322 ntfs_log_trace("rl NULL, start_vcn %lld (should be > 0)\n",
1323 (long long) start_vcn
);
1330 /* Skip to runlist element containing @start_vcn. */
1331 while (rl
->length
&& start_vcn
>= rl
[1].vcn
)
1333 if ((!rl
->length
&& start_vcn
> rl
->vcn
) || start_vcn
< rl
->vcn
) {
1338 /* Always need the terminating zero byte. */
1340 /* Do the first partial run if present. */
1341 if (start_vcn
> rl
->vcn
) {
1344 /* We know rl->length != 0 already. */
1345 if (rl
->length
< 0 || rl
->lcn
< LCN_HOLE
)
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) {
1361 /* Change in lcn. */
1362 rls
+= ntfs_get_nr_significant_bytes(prev_lcn
);
1364 /* Go to next runlist element. */
1367 /* Do the full runs. */
1368 for (; rl
->length
&& (rls
<= max_size
); rl
++) {
1369 if (rl
->length
< 0 || rl
->lcn
< LCN_HOLE
)
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
-
1390 if (rl
->lcn
== LCN_RL_NOT_MAPPED
)
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
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
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
)
1427 while ((l
> 0x7f) || (l
< -0x80)) {
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
)
1476 u8
*dst_max
, *dst_next
;
1477 s8 len_len
, lcn_len
;
1491 /* Skip to runlist element containing @start_vcn. */
1492 while (rl
->length
&& start_vcn
>= rl
[1].vcn
)
1494 if ((!rl
->length
&& start_vcn
> rl
->vcn
) || start_vcn
< rl
->vcn
)
1497 * @dst_max is used for bounds checking in
1498 * ntfs_write_significant_bytes().
1500 dst_max
= dst
+ dst_len
- 1;
1502 /* Do the first partial run if present. */
1503 if (start_vcn
> rl
->vcn
) {
1506 /* We know rl->length != 0 already. */
1507 if (rl
->length
< 0 || rl
->lcn
< LCN_HOLE
)
1509 delta
= start_vcn
- rl
->vcn
;
1511 len_len
= ntfs_write_significant_bytes(dst
+ 1, dst_max
,
1512 rl
->length
- delta
);
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) {
1528 /* Write change in lcn. */
1529 lcn_len
= ntfs_write_significant_bytes(dst
+ 1 +
1530 len_len
, dst_max
, prev_lcn
);
1535 dst_next
= dst
+ len_len
+ lcn_len
+ 1;
1536 if (dst_next
> dst_max
)
1538 /* Update header byte. */
1539 *dst
= lcn_len
<< 4 | len_len
;
1540 /* Position at next mapping pairs array element. */
1542 /* Go to next runlist element. */
1545 /* Do the full runs. */
1546 for (; rl
->length
; rl
++) {
1547 if (rl
->length
< 0 || rl
->lcn
< LCN_HOLE
)
1550 len_len
= ntfs_write_significant_bytes(dst
+ 1, dst_max
,
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
);
1572 dst_next
= dst
+ len_len
+ lcn_len
+ 1;
1573 if (dst_next
> dst_max
)
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
;
1584 /* Add terminator byte. */
1592 /* Add terminator byte. */
1601 if (rl
->lcn
== LCN_RL_NOT_MAPPED
)
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
)
1626 BOOL is_end
= FALSE
;
1628 if (!arl
|| !*arl
) {
1631 ntfs_log_perror("rl_truncate error: arl: %p", arl
);
1633 ntfs_log_perror("rl_truncate error:"
1634 " arl: %p *arl: %p", arl
, *arl
);
1640 if (start_vcn
< rl
->vcn
) {
1642 ntfs_log_perror("Start_vcn lies outside front of runlist");
1646 /* Find the starting vcn in the run list. */
1647 while (rl
->length
) {
1648 if (start_vcn
< rl
[1].vcn
)
1655 ntfs_log_trace("Truncating already truncated runlist?\n");
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
1671 rl
->vcn
= start_vcn
;
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.
1682 size_t new_size = (rl - *arl + 1) * sizeof(runlist_element);
1683 rl = realloc(*arl, new_size);
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
)
1703 ntfs_log_perror("%s: ", __FUNCTION__
);
1707 for (rlc
= rl
; rlc
->length
; rlc
++)
1709 if (rlc
->lcn
!= LCN_HOLE
) {
1711 ntfs_log_perror("%s: bad runlist", __FUNCTION__
);
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
)
1733 ntfs_log_perror("%s: ", __FUNCTION__
);
1737 for (rlc
= rl
; rlc
->length
; rlc
++) {
1739 if (rlc
->lcn
!= LCN_HOLE
) {
1741 ntfs_log_perror("%s: bad runlist", __FUNCTION__
);
1747 return ret
<< vol
->cluster_size_bits
;
1755 #define MKRL(R,V,L,S) \
1763 * test_rl_dump_runlist - Runlist test: Display the contents of a runlist
1770 static void test_rl_dump_runlist(const runlist_element
*rl
)
1772 int abbr
= 0; /* abbreviate long lists */
1775 const char *lcn_str
[5] = { "HOLE", "NOTMAP", "ENOENT", "XXXX" };
1778 printf(" Run list not present.\n");
1783 for (len
= 0; rl
[len
].length
; len
++) ;
1785 printf(" VCN LCN len\n");
1786 for (i
= 0; ; i
++, rl
++) {
1789 if ((abbr
) && (len
> 20)) {
1792 if ((i
> 3) && (i
< (len
- 3)))
1799 if (ind
> -LCN_ENOENT
- 1)
1801 printf("%8lld %8s %8lld\n",
1802 rl
->vcn
, lcn_str
[ind
], rl
->length
);
1804 printf("%8lld %8lld %8lld\n",
1805 rl
->vcn
, rl
->lcn
, rl
->length
);
1809 if ((abbr
) && (len
> 20))
1810 printf(" (%d entries)\n", len
+1);
1815 * test_rl_runlists_merge - Runlist test: Merge two runlists
1823 static runlist_element
* test_rl_runlists_merge(runlist_element
*drl
, runlist_element
*srl
)
1825 runlist_element
*res
= NULL
;
1828 test_rl_dump_runlist(drl
);
1830 test_rl_dump_runlist(srl
);
1832 res
= ntfs_runlists_merge(drl
, srl
);
1835 test_rl_dump_runlist(res
);
1841 * test_rl_read_buffer - Runlist test: Read a file containing a runlist
1850 static int test_rl_read_buffer(const char *file
, u8
*buf
, int bufsize
)
1854 fptr
= fopen(file
, "r");
1856 printf("open %s\n", file
);
1860 if (fread(buf
, bufsize
, 1, fptr
) == 99) {
1861 printf("read %s\n", file
);
1870 * test_rl_pure_src - Runlist test: Complicate the simple tests a little
1880 static runlist_element
* test_rl_pure_src(BOOL contig
, BOOL multi
, int vcn
, int len
)
1882 runlist_element
*result
;
1890 result
= ntfs_malloc(4096);
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)
1901 MKRL(result
+0, vcn
, fudge
+ vcn
+ 1000, len
)
1902 MKRL(result
+1, vcn
+ len
, LCN_RL_NOT_MAPPED
, 0)
1908 * test_rl_pure_test - Runlist test: Perform tests using simple runlists
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);
1930 printf("Test %2d ---------- FAILED! (no free memory?)\n", test
);
1934 memcpy(dst
, file
, size
);
1936 printf("Test %2d ----------\n", test
);
1937 res
= test_rl_runlists_merge(dst
, src
);
1943 * test_rl_pure - Runlist test: Create tests using simple runlists
1951 static void test_rl_pure(char *contig
, char *multi
)
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 */
1989 if (strcmp(contig
, "contig") == 0)
1991 else if (strcmp(contig
, "noncontig") == 0)
1994 printf("rl pure [contig|noncontig] [single|multi]\n");
1997 if (strcmp(multi
, "multi") == 0)
1999 else if (strcmp(multi
, "single") == 0)
2002 printf("rl pure [contig|noncontig] [single|multi]\n");
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
2049 static void test_rl_zero(void)
2051 runlist_element
*jim
= NULL
;
2052 runlist_element
*bob
= NULL
;
2054 bob
= calloc(3, sizeof(runlist_element
));
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
);
2069 * test_rl_frag_combine - Runlist test: Perform tests using fragmented files
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
);
2089 run2
= ntfs_mapping_pairs_decompress(vol
, attr2
, NULL
);
2093 run1
= test_rl_runlists_merge(run1
, run2
);
2095 run3
= ntfs_mapping_pairs_decompress(vol
, attr3
, NULL
);
2099 run1
= test_rl_runlists_merge(run1
, run3
);
2105 * test_rl_frag - Runlist test: Create tests using very fragmented files
2112 static void test_rl_frag(char *test
)
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
)
2123 vol
.sector_size_bits
= 9;
2124 vol
.cluster_size
= 2048;
2125 vol
.cluster_size_bits
= 11;
2128 if (!test_rl_read_buffer("runlist-data/attr1.bin", (u8
*) attr1
, 1024))
2130 if (!test_rl_read_buffer("runlist-data/attr2.bin", (u8
*) attr2
, 1024))
2132 if (!test_rl_read_buffer("runlist-data/attr3.bin", (u8
*) attr3
, 1024))
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
);
2142 printf("Frag: No such test '%s'\n", test
);
2151 * test_rl_main - Runlist test: Program start (main)
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]);
2165 printf("rl [zero|frag|pure] {args}\n");