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