[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / fs / ntfs / runlist.c
blob8438fb1da219f61ca464dd6d2f4903d6681d12fd
1 /**
2 * runlist.c - NTFS runlist handling code. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2004 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "debug.h"
24 #include "dir.h"
25 #include "endian.h"
26 #include "malloc.h"
27 #include "ntfs.h"
29 /**
30 * ntfs_rl_mm - runlist memmove
32 * It is up to the caller to serialize access to the runlist @base.
34 static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
35 int size)
37 if (likely((dst != src) && (size > 0)))
38 memmove(base + dst, base + src, size * sizeof (*base));
41 /**
42 * ntfs_rl_mc - runlist memory copy
44 * It is up to the caller to serialize access to the runlists @dstbase and
45 * @srcbase.
47 static inline void ntfs_rl_mc(runlist_element *dstbase, int dst,
48 runlist_element *srcbase, int src, int size)
50 if (likely(size > 0))
51 memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
54 /**
55 * ntfs_rl_realloc - Reallocate memory for runlists
56 * @rl: original runlist
57 * @old_size: number of runlist elements in the original runlist @rl
58 * @new_size: number of runlist elements we need space for
60 * As the runlists grow, more memory will be required. To prevent the
61 * kernel having to allocate and reallocate large numbers of small bits of
62 * memory, this function returns and entire page of memory.
64 * It is up to the caller to serialize access to the runlist @rl.
66 * N.B. If the new allocation doesn't require a different number of pages in
67 * memory, the function will return the original pointer.
69 * On success, return a pointer to the newly allocated, or recycled, memory.
70 * On error, return -errno. The following error codes are defined:
71 * -ENOMEM - Not enough memory to allocate runlist array.
72 * -EINVAL - Invalid parameters were passed in.
74 static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
75 int old_size, int new_size)
77 runlist_element *new_rl;
79 old_size = PAGE_ALIGN(old_size * sizeof(*rl));
80 new_size = PAGE_ALIGN(new_size * sizeof(*rl));
81 if (old_size == new_size)
82 return rl;
84 new_rl = ntfs_malloc_nofs(new_size);
85 if (unlikely(!new_rl))
86 return ERR_PTR(-ENOMEM);
88 if (likely(rl != NULL)) {
89 if (unlikely(old_size > new_size))
90 old_size = new_size;
91 memcpy(new_rl, rl, old_size);
92 ntfs_free(rl);
94 return new_rl;
97 /**
98 * ntfs_are_rl_mergeable - test if two runlists can be joined together
99 * @dst: original runlist
100 * @src: new runlist to test for mergeability with @dst
102 * Test if two runlists can be joined together. For this, their VCNs and LCNs
103 * must be adjacent.
105 * It is up to the caller to serialize access to the runlists @dst and @src.
107 * Return: TRUE Success, the runlists can be merged.
108 * FALSE Failure, the runlists cannot be merged.
110 static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
111 runlist_element *src)
113 BUG_ON(!dst);
114 BUG_ON(!src);
116 if ((dst->lcn < 0) || (src->lcn < 0)) /* Are we merging holes? */
117 return FALSE;
118 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
119 return FALSE;
120 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
121 return FALSE;
123 return TRUE;
127 * __ntfs_rl_merge - merge two runlists without testing if they can be merged
128 * @dst: original, destination runlist
129 * @src: new runlist to merge with @dst
131 * Merge the two runlists, writing into the destination runlist @dst. The
132 * caller must make sure the runlists can be merged or this will corrupt the
133 * destination runlist.
135 * It is up to the caller to serialize access to the runlists @dst and @src.
137 static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
139 dst->length += src->length;
143 * ntfs_rl_append - append a runlist after a given element
144 * @dst: original runlist to be worked on
145 * @dsize: number of elements in @dst (including end marker)
146 * @src: runlist to be inserted into @dst
147 * @ssize: number of elements in @src (excluding end marker)
148 * @loc: append the new runlist @src after this element in @dst
150 * Append the runlist @src after element @loc in @dst. Merge the right end of
151 * the new runlist, if necessary. Adjust the size of the hole before the
152 * appended runlist.
154 * It is up to the caller to serialize access to the runlists @dst and @src.
156 * On success, return a pointer to the new, combined, runlist. Note, both
157 * runlists @dst and @src are deallocated before returning so you cannot use
158 * the pointers for anything any more. (Strictly speaking the returned runlist
159 * may be the same as @dst but this is irrelevant.)
161 * On error, return -errno. Both runlists are left unmodified. The following
162 * error codes are defined:
163 * -ENOMEM - Not enough memory to allocate runlist array.
164 * -EINVAL - Invalid parameters were passed in.
166 static inline runlist_element *ntfs_rl_append(runlist_element *dst,
167 int dsize, runlist_element *src, int ssize, int loc)
169 BOOL right;
170 int magic;
172 BUG_ON(!dst);
173 BUG_ON(!src);
175 /* First, check if the right hand end needs merging. */
176 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
178 /* Space required: @dst size + @src size, less one if we merged. */
179 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
180 if (IS_ERR(dst))
181 return dst;
183 * We are guaranteed to succeed from here so can start modifying the
184 * original runlists.
187 /* First, merge the right hand end, if necessary. */
188 if (right)
189 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
191 magic = loc + ssize;
193 /* Move the tail of @dst out of the way, then copy in @src. */
194 ntfs_rl_mm(dst, magic + 1, loc + 1 + right, dsize - loc - 1 - right);
195 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
197 /* Adjust the size of the preceding hole. */
198 dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
200 /* We may have changed the length of the file, so fix the end marker */
201 if (dst[magic + 1].lcn == LCN_ENOENT)
202 dst[magic + 1].vcn = dst[magic].vcn + dst[magic].length;
204 return dst;
208 * ntfs_rl_insert - insert a runlist into another
209 * @dst: original runlist to be worked on
210 * @dsize: number of elements in @dst (including end marker)
211 * @src: new runlist to be inserted
212 * @ssize: number of elements in @src (excluding end marker)
213 * @loc: insert the new runlist @src before this element in @dst
215 * Insert the runlist @src before element @loc in the runlist @dst. Merge the
216 * left end of the new runlist, if necessary. Adjust the size of the hole
217 * after the inserted runlist.
219 * It is up to the caller to serialize access to the runlists @dst and @src.
221 * On success, return a pointer to the new, combined, runlist. Note, both
222 * runlists @dst and @src are deallocated before returning so you cannot use
223 * the pointers for anything any more. (Strictly speaking the returned runlist
224 * may be the same as @dst but this is irrelevant.)
226 * On error, return -errno. Both runlists are left unmodified. The following
227 * error codes are defined:
228 * -ENOMEM - Not enough memory to allocate runlist array.
229 * -EINVAL - Invalid parameters were passed in.
231 static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
232 int dsize, runlist_element *src, int ssize, int loc)
234 BOOL left = FALSE;
235 BOOL disc = FALSE; /* Discontinuity */
236 BOOL hole = FALSE; /* Following a hole */
237 int magic;
239 BUG_ON(!dst);
240 BUG_ON(!src);
242 /* disc => Discontinuity between the end of @dst and the start of @src.
243 * This means we might need to insert a hole.
244 * hole => @dst ends with a hole or an unmapped region which we can
245 * extend to match the discontinuity. */
246 if (loc == 0)
247 disc = (src[0].vcn > 0);
248 else {
249 s64 merged_length;
251 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
253 merged_length = dst[loc - 1].length;
254 if (left)
255 merged_length += src->length;
257 disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
258 if (disc)
259 hole = (dst[loc - 1].lcn == LCN_HOLE);
262 /* Space required: @dst size + @src size, less one if we merged, plus
263 * one if there was a discontinuity, less one for a trailing hole. */
264 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc - hole);
265 if (IS_ERR(dst))
266 return dst;
268 * We are guaranteed to succeed from here so can start modifying the
269 * original runlist.
272 if (left)
273 __ntfs_rl_merge(dst + loc - 1, src);
275 magic = loc + ssize - left + disc - hole;
277 /* Move the tail of @dst out of the way, then copy in @src. */
278 ntfs_rl_mm(dst, magic, loc, dsize - loc);
279 ntfs_rl_mc(dst, loc + disc - hole, src, left, ssize - left);
281 /* Adjust the VCN of the last run ... */
282 if (dst[magic].lcn <= LCN_HOLE)
283 dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
284 /* ... and the length. */
285 if (dst[magic].lcn == LCN_HOLE || dst[magic].lcn == LCN_RL_NOT_MAPPED)
286 dst[magic].length = dst[magic + 1].vcn - dst[magic].vcn;
288 /* Writing beyond the end of the file and there's a discontinuity. */
289 if (disc) {
290 if (hole)
291 dst[loc - 1].length = dst[loc].vcn - dst[loc - 1].vcn;
292 else {
293 if (loc > 0) {
294 dst[loc].vcn = dst[loc - 1].vcn +
295 dst[loc - 1].length;
296 dst[loc].length = dst[loc + 1].vcn -
297 dst[loc].vcn;
298 } else {
299 dst[loc].vcn = 0;
300 dst[loc].length = dst[loc + 1].vcn;
302 dst[loc].lcn = LCN_RL_NOT_MAPPED;
305 magic += hole;
307 if (dst[magic].lcn == LCN_ENOENT)
308 dst[magic].vcn = dst[magic - 1].vcn +
309 dst[magic - 1].length;
311 return dst;
315 * ntfs_rl_replace - overwrite a runlist element with another runlist
316 * @dst: original runlist to be worked on
317 * @dsize: number of elements in @dst (including end marker)
318 * @src: new runlist to be inserted
319 * @ssize: number of elements in @src (excluding end marker)
320 * @loc: index in runlist @dst to overwrite with @src
322 * Replace the runlist element @dst at @loc with @src. Merge the left and
323 * right ends of the inserted runlist, if necessary.
325 * It is up to the caller to serialize access to the runlists @dst and @src.
327 * On success, return a pointer to the new, combined, runlist. Note, both
328 * runlists @dst and @src are deallocated before returning so you cannot use
329 * the pointers for anything any more. (Strictly speaking the returned runlist
330 * may be the same as @dst but this is irrelevant.)
332 * On error, return -errno. Both runlists are left unmodified. The following
333 * error codes are defined:
334 * -ENOMEM - Not enough memory to allocate runlist array.
335 * -EINVAL - Invalid parameters were passed in.
337 static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
338 int dsize, runlist_element *src, int ssize, int loc)
340 BOOL left = FALSE;
341 BOOL right;
342 int magic;
344 BUG_ON(!dst);
345 BUG_ON(!src);
347 /* First, merge the left and right ends, if necessary. */
348 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
349 if (loc > 0)
350 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
352 /* Allocate some space. We'll need less if the left, right, or both
353 * ends were merged. */
354 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left - right);
355 if (IS_ERR(dst))
356 return dst;
358 * We are guaranteed to succeed from here so can start modifying the
359 * original runlists.
361 if (right)
362 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
363 if (left)
364 __ntfs_rl_merge(dst + loc - 1, src);
366 /* FIXME: What does this mean? (AIA) */
367 magic = loc + ssize - left;
369 /* Move the tail of @dst out of the way, then copy in @src. */
370 ntfs_rl_mm(dst, magic, loc + right + 1, dsize - loc - right - 1);
371 ntfs_rl_mc(dst, loc, src, left, ssize - left);
373 /* We may have changed the length of the file, so fix the end marker */
374 if (dst[magic].lcn == LCN_ENOENT)
375 dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
376 return dst;
380 * ntfs_rl_split - insert a runlist into the centre of a hole
381 * @dst: original runlist to be worked on
382 * @dsize: number of elements in @dst (including end marker)
383 * @src: new runlist to be inserted
384 * @ssize: number of elements in @src (excluding end marker)
385 * @loc: index in runlist @dst at which to split and insert @src
387 * Split the runlist @dst at @loc into two and insert @new in between the two
388 * fragments. No merging of runlists is necessary. Adjust the size of the
389 * holes either side.
391 * It is up to the caller to serialize access to the runlists @dst and @src.
393 * On success, return a pointer to the new, combined, runlist. Note, both
394 * runlists @dst and @src are deallocated before returning so you cannot use
395 * the pointers for anything any more. (Strictly speaking the returned runlist
396 * may be the same as @dst but this is irrelevant.)
398 * On error, return -errno. Both runlists are left unmodified. The following
399 * error codes are defined:
400 * -ENOMEM - Not enough memory to allocate runlist array.
401 * -EINVAL - Invalid parameters were passed in.
403 static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
404 runlist_element *src, int ssize, int loc)
406 BUG_ON(!dst);
407 BUG_ON(!src);
409 /* Space required: @dst size + @src size + one new hole. */
410 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
411 if (IS_ERR(dst))
412 return dst;
414 * We are guaranteed to succeed from here so can start modifying the
415 * original runlists.
418 /* Move the tail of @dst out of the way, then copy in @src. */
419 ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
420 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
422 /* Adjust the size of the holes either size of @src. */
423 dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
424 dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
425 dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
427 return dst;
431 * ntfs_runlists_merge - merge two runlists into one
432 * @drl: original runlist to be worked on
433 * @srl: new runlist to be merged into @drl
435 * First we sanity check the two runlists @srl and @drl to make sure that they
436 * are sensible and can be merged. The runlist @srl must be either after the
437 * runlist @drl or completely within a hole (or unmapped region) in @drl.
439 * It is up to the caller to serialize access to the runlists @drl and @srl.
441 * Merging of runlists is necessary in two cases:
442 * 1. When attribute lists are used and a further extent is being mapped.
443 * 2. When new clusters are allocated to fill a hole or extend a file.
445 * There are four possible ways @srl can be merged. It can:
446 * - be inserted at the beginning of a hole,
447 * - split the hole in two and be inserted between the two fragments,
448 * - be appended at the end of a hole, or it can
449 * - replace the whole hole.
450 * It can also be appended to the end of the runlist, which is just a variant
451 * of the insert case.
453 * On success, return a pointer to the new, combined, runlist. Note, both
454 * runlists @drl and @srl are deallocated before returning so you cannot use
455 * the pointers for anything any more. (Strictly speaking the returned runlist
456 * may be the same as @dst but this is irrelevant.)
458 * On error, return -errno. Both runlists are left unmodified. The following
459 * error codes are defined:
460 * -ENOMEM - Not enough memory to allocate runlist array.
461 * -EINVAL - Invalid parameters were passed in.
462 * -ERANGE - The runlists overlap and cannot be merged.
464 runlist_element *ntfs_runlists_merge(runlist_element *drl,
465 runlist_element *srl)
467 int di, si; /* Current index into @[ds]rl. */
468 int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
469 int dins; /* Index into @drl at which to insert @srl. */
470 int dend, send; /* Last index into @[ds]rl. */
471 int dfinal, sfinal; /* The last index into @[ds]rl with
472 lcn >= LCN_HOLE. */
473 int marker = 0;
474 VCN marker_vcn = 0;
476 #ifdef DEBUG
477 ntfs_debug("dst:");
478 ntfs_debug_dump_runlist(drl);
479 ntfs_debug("src:");
480 ntfs_debug_dump_runlist(srl);
481 #endif
483 /* Check for silly calling... */
484 if (unlikely(!srl))
485 return drl;
486 if (IS_ERR(srl) || IS_ERR(drl))
487 return ERR_PTR(-EINVAL);
489 /* Check for the case where the first mapping is being done now. */
490 if (unlikely(!drl)) {
491 drl = srl;
492 /* Complete the source runlist if necessary. */
493 if (unlikely(drl[0].vcn)) {
494 /* Scan to the end of the source runlist. */
495 for (dend = 0; likely(drl[dend].length); dend++)
497 drl = ntfs_rl_realloc(drl, dend, dend + 1);
498 if (IS_ERR(drl))
499 return drl;
500 /* Insert start element at the front of the runlist. */
501 ntfs_rl_mm(drl, 1, 0, dend);
502 drl[0].vcn = 0;
503 drl[0].lcn = LCN_RL_NOT_MAPPED;
504 drl[0].length = drl[1].vcn;
506 goto finished;
509 si = di = 0;
511 /* Skip any unmapped start element(s) in the source runlist. */
512 while (srl[si].length && srl[si].lcn < LCN_HOLE)
513 si++;
515 /* Can't have an entirely unmapped source runlist. */
516 BUG_ON(!srl[si].length);
518 /* Record the starting points. */
519 sstart = si;
522 * Skip forward in @drl until we reach the position where @srl needs to
523 * be inserted. If we reach the end of @drl, @srl just needs to be
524 * appended to @drl.
526 for (; drl[di].length; di++) {
527 if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
528 break;
530 dins = di;
532 /* Sanity check for illegal overlaps. */
533 if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
534 (srl[si].lcn >= 0)) {
535 ntfs_error(NULL, "Run lists overlap. Cannot merge!");
536 return ERR_PTR(-ERANGE);
539 /* Scan to the end of both runlists in order to know their sizes. */
540 for (send = si; srl[send].length; send++)
542 for (dend = di; drl[dend].length; dend++)
545 if (srl[send].lcn == LCN_ENOENT)
546 marker_vcn = srl[marker = send].vcn;
548 /* Scan to the last element with lcn >= LCN_HOLE. */
549 for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
551 for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
555 BOOL start;
556 BOOL finish;
557 int ds = dend + 1; /* Number of elements in drl & srl */
558 int ss = sfinal - sstart + 1;
560 start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
561 (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
562 finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
563 ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
564 (srl[send - 1].vcn + srl[send - 1].length)));
566 /* Or we'll lose an end marker */
567 if (start && finish && (drl[dins].length == 0))
568 ss++;
569 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
570 finish = FALSE;
571 #if 0
572 ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
573 ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
574 ntfs_debug("start = %i, finish = %i", start, finish);
575 ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
576 #endif
577 if (start) {
578 if (finish)
579 drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
580 else
581 drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
582 } else {
583 if (finish)
584 drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
585 else
586 drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
588 if (IS_ERR(drl)) {
589 ntfs_error(NULL, "Merge failed.");
590 return drl;
592 ntfs_free(srl);
593 if (marker) {
594 ntfs_debug("Triggering marker code.");
595 for (ds = dend; drl[ds].length; ds++)
597 /* We only need to care if @srl ended after @drl. */
598 if (drl[ds].vcn <= marker_vcn) {
599 int slots = 0;
601 if (drl[ds].vcn == marker_vcn) {
602 ntfs_debug("Old marker = 0x%llx, replacing "
603 "with LCN_ENOENT.",
604 (unsigned long long)
605 drl[ds].lcn);
606 drl[ds].lcn = LCN_ENOENT;
607 goto finished;
610 * We need to create an unmapped runlist element in
611 * @drl or extend an existing one before adding the
612 * ENOENT terminator.
614 if (drl[ds].lcn == LCN_ENOENT) {
615 ds--;
616 slots = 1;
618 if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
619 /* Add an unmapped runlist element. */
620 if (!slots) {
621 /* FIXME/TODO: We need to have the
622 * extra memory already! (AIA) */
623 drl = ntfs_rl_realloc(drl, ds, ds + 2);
624 if (!drl)
625 goto critical_error;
626 slots = 2;
628 ds++;
629 /* Need to set vcn if it isn't set already. */
630 if (slots != 1)
631 drl[ds].vcn = drl[ds - 1].vcn +
632 drl[ds - 1].length;
633 drl[ds].lcn = LCN_RL_NOT_MAPPED;
634 /* We now used up a slot. */
635 slots--;
637 drl[ds].length = marker_vcn - drl[ds].vcn;
638 /* Finally add the ENOENT terminator. */
639 ds++;
640 if (!slots) {
641 /* FIXME/TODO: We need to have the extra
642 * memory already! (AIA) */
643 drl = ntfs_rl_realloc(drl, ds, ds + 1);
644 if (!drl)
645 goto critical_error;
647 drl[ds].vcn = marker_vcn;
648 drl[ds].lcn = LCN_ENOENT;
649 drl[ds].length = (s64)0;
654 finished:
655 /* The merge was completed successfully. */
656 ntfs_debug("Merged runlist:");
657 ntfs_debug_dump_runlist(drl);
658 return drl;
660 critical_error:
661 /* Critical error! We cannot afford to fail here. */
662 ntfs_error(NULL, "Critical error! Not enough memory.");
663 panic("NTFS: Cannot continue.");
667 * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
668 * @vol: ntfs volume on which the attribute resides
669 * @attr: attribute record whose mapping pairs array to decompress
670 * @old_rl: optional runlist in which to insert @attr's runlist
672 * It is up to the caller to serialize access to the runlist @old_rl.
674 * Decompress the attribute @attr's mapping pairs array into a runlist. On
675 * success, return the decompressed runlist.
677 * If @old_rl is not NULL, decompressed runlist is inserted into the
678 * appropriate place in @old_rl and the resultant, combined runlist is
679 * returned. The original @old_rl is deallocated.
681 * On error, return -errno. @old_rl is left unmodified in that case.
683 * The following error codes are defined:
684 * -ENOMEM - Not enough memory to allocate runlist array.
685 * -EIO - Corrupt runlist.
686 * -EINVAL - Invalid parameters were passed in.
687 * -ERANGE - The two runlists overlap.
689 * FIXME: For now we take the conceptionally simplest approach of creating the
690 * new runlist disregarding the already existing one and then splicing the
691 * two into one, if that is possible (we check for overlap and discard the new
692 * runlist if overlap present before returning ERR_PTR(-ERANGE)).
694 runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
695 const ATTR_RECORD *attr, runlist_element *old_rl)
697 VCN vcn; /* Current vcn. */
698 LCN lcn; /* Current lcn. */
699 s64 deltaxcn; /* Change in [vl]cn. */
700 runlist_element *rl; /* The output runlist. */
701 u8 *buf; /* Current position in mapping pairs array. */
702 u8 *attr_end; /* End of attribute. */
703 int rlsize; /* Size of runlist buffer. */
704 u16 rlpos; /* Current runlist position in units of
705 runlist_elements. */
706 u8 b; /* Current byte offset in buf. */
708 #ifdef DEBUG
709 /* Make sure attr exists and is non-resident. */
710 if (!attr || !attr->non_resident || sle64_to_cpu(
711 attr->data.non_resident.lowest_vcn) < (VCN)0) {
712 ntfs_error(vol->sb, "Invalid arguments.");
713 return ERR_PTR(-EINVAL);
715 #endif
716 /* Start at vcn = lowest_vcn and lcn 0. */
717 vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
718 lcn = 0;
719 /* Get start of the mapping pairs array. */
720 buf = (u8*)attr + le16_to_cpu(
721 attr->data.non_resident.mapping_pairs_offset);
722 attr_end = (u8*)attr + le32_to_cpu(attr->length);
723 if (unlikely(buf < (u8*)attr || buf > attr_end)) {
724 ntfs_error(vol->sb, "Corrupt attribute.");
725 return ERR_PTR(-EIO);
727 /* Current position in runlist array. */
728 rlpos = 0;
729 /* Allocate first page and set current runlist size to one page. */
730 rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
731 if (unlikely(!rl))
732 return ERR_PTR(-ENOMEM);
733 /* Insert unmapped starting element if necessary. */
734 if (vcn) {
735 rl->vcn = 0;
736 rl->lcn = LCN_RL_NOT_MAPPED;
737 rl->length = vcn;
738 rlpos++;
740 while (buf < attr_end && *buf) {
742 * Allocate more memory if needed, including space for the
743 * not-mapped and terminator elements. ntfs_malloc_nofs()
744 * operates on whole pages only.
746 if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
747 runlist_element *rl2;
749 rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
750 if (unlikely(!rl2)) {
751 ntfs_free(rl);
752 return ERR_PTR(-ENOMEM);
754 memcpy(rl2, rl, rlsize);
755 ntfs_free(rl);
756 rl = rl2;
757 rlsize += PAGE_SIZE;
759 /* Enter the current vcn into the current runlist element. */
760 rl[rlpos].vcn = vcn;
762 * Get the change in vcn, i.e. the run length in clusters.
763 * Doing it this way ensures that we signextend negative values.
764 * A negative run length doesn't make any sense, but hey, I
765 * didn't make up the NTFS specs and Windows NT4 treats the run
766 * length as a signed value so that's how it is...
768 b = *buf & 0xf;
769 if (b) {
770 if (unlikely(buf + b > attr_end))
771 goto io_error;
772 for (deltaxcn = (s8)buf[b--]; b; b--)
773 deltaxcn = (deltaxcn << 8) + buf[b];
774 } else { /* The length entry is compulsory. */
775 ntfs_error(vol->sb, "Missing length entry in mapping "
776 "pairs array.");
777 deltaxcn = (s64)-1;
780 * Assume a negative length to indicate data corruption and
781 * hence clean-up and return NULL.
783 if (unlikely(deltaxcn < 0)) {
784 ntfs_error(vol->sb, "Invalid length in mapping pairs "
785 "array.");
786 goto err_out;
789 * Enter the current run length into the current runlist
790 * element.
792 rl[rlpos].length = deltaxcn;
793 /* Increment the current vcn by the current run length. */
794 vcn += deltaxcn;
796 * There might be no lcn change at all, as is the case for
797 * sparse clusters on NTFS 3.0+, in which case we set the lcn
798 * to LCN_HOLE.
800 if (!(*buf & 0xf0))
801 rl[rlpos].lcn = LCN_HOLE;
802 else {
803 /* Get the lcn change which really can be negative. */
804 u8 b2 = *buf & 0xf;
805 b = b2 + ((*buf >> 4) & 0xf);
806 if (buf + b > attr_end)
807 goto io_error;
808 for (deltaxcn = (s8)buf[b--]; b > b2; b--)
809 deltaxcn = (deltaxcn << 8) + buf[b];
810 /* Change the current lcn to its new value. */
811 lcn += deltaxcn;
812 #ifdef DEBUG
814 * On NTFS 1.2-, apparently can have lcn == -1 to
815 * indicate a hole. But we haven't verified ourselves
816 * whether it is really the lcn or the deltaxcn that is
817 * -1. So if either is found give us a message so we
818 * can investigate it further!
820 if (vol->major_ver < 3) {
821 if (unlikely(deltaxcn == (LCN)-1))
822 ntfs_error(vol->sb, "lcn delta == -1");
823 if (unlikely(lcn == (LCN)-1))
824 ntfs_error(vol->sb, "lcn == -1");
826 #endif
827 /* Check lcn is not below -1. */
828 if (unlikely(lcn < (LCN)-1)) {
829 ntfs_error(vol->sb, "Invalid LCN < -1 in "
830 "mapping pairs array.");
831 goto err_out;
833 /* Enter the current lcn into the runlist element. */
834 rl[rlpos].lcn = lcn;
836 /* Get to the next runlist element. */
837 rlpos++;
838 /* Increment the buffer position to the next mapping pair. */
839 buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
841 if (unlikely(buf >= attr_end))
842 goto io_error;
844 * If there is a highest_vcn specified, it must be equal to the final
845 * vcn in the runlist - 1, or something has gone badly wrong.
847 deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
848 if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
849 mpa_err:
850 ntfs_error(vol->sb, "Corrupt mapping pairs array in "
851 "non-resident attribute.");
852 goto err_out;
854 /* Setup not mapped runlist element if this is the base extent. */
855 if (!attr->data.non_resident.lowest_vcn) {
856 VCN max_cluster;
858 max_cluster = (sle64_to_cpu(
859 attr->data.non_resident.allocated_size) +
860 vol->cluster_size - 1) >>
861 vol->cluster_size_bits;
863 * If there is a difference between the highest_vcn and the
864 * highest cluster, the runlist is either corrupt or, more
865 * likely, there are more extents following this one.
867 if (deltaxcn < --max_cluster) {
868 ntfs_debug("More extents to follow; deltaxcn = 0x%llx, "
869 "max_cluster = 0x%llx",
870 (unsigned long long)deltaxcn,
871 (unsigned long long)max_cluster);
872 rl[rlpos].vcn = vcn;
873 vcn += rl[rlpos].length = max_cluster - deltaxcn;
874 rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
875 rlpos++;
876 } else if (unlikely(deltaxcn > max_cluster)) {
877 ntfs_error(vol->sb, "Corrupt attribute. deltaxcn = "
878 "0x%llx, max_cluster = 0x%llx",
879 (unsigned long long)deltaxcn,
880 (unsigned long long)max_cluster);
881 goto mpa_err;
883 rl[rlpos].lcn = LCN_ENOENT;
884 } else /* Not the base extent. There may be more extents to follow. */
885 rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
887 /* Setup terminating runlist element. */
888 rl[rlpos].vcn = vcn;
889 rl[rlpos].length = (s64)0;
890 /* If no existing runlist was specified, we are done. */
891 if (!old_rl) {
892 ntfs_debug("Mapping pairs array successfully decompressed:");
893 ntfs_debug_dump_runlist(rl);
894 return rl;
896 /* Now combine the new and old runlists checking for overlaps. */
897 old_rl = ntfs_runlists_merge(old_rl, rl);
898 if (likely(!IS_ERR(old_rl)))
899 return old_rl;
900 ntfs_free(rl);
901 ntfs_error(vol->sb, "Failed to merge runlists.");
902 return old_rl;
903 io_error:
904 ntfs_error(vol->sb, "Corrupt attribute.");
905 err_out:
906 ntfs_free(rl);
907 return ERR_PTR(-EIO);
911 * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
912 * @rl: runlist to use for conversion
913 * @vcn: vcn to convert
915 * Convert the virtual cluster number @vcn of an attribute into a logical
916 * cluster number (lcn) of a device using the runlist @rl to map vcns to their
917 * corresponding lcns.
919 * It is up to the caller to serialize access to the runlist @rl.
921 * Since lcns must be >= 0, we use negative return values with special meaning:
923 * Return value Meaning / Description
924 * ==================================================
925 * -1 = LCN_HOLE Hole / not allocated on disk.
926 * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been
927 * inserted into the runlist yet.
928 * -3 = LCN_ENOENT There is no such vcn in the attribute.
930 * Locking: - The caller must have locked the runlist (for reading or writing).
931 * - This function does not touch the lock.
933 LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
935 int i;
937 BUG_ON(vcn < 0);
939 * If rl is NULL, assume that we have found an unmapped runlist. The
940 * caller can then attempt to map it and fail appropriately if
941 * necessary.
943 if (unlikely(!rl))
944 return LCN_RL_NOT_MAPPED;
946 /* Catch out of lower bounds vcn. */
947 if (unlikely(vcn < rl[0].vcn))
948 return LCN_ENOENT;
950 for (i = 0; likely(rl[i].length); i++) {
951 if (unlikely(vcn < rl[i+1].vcn)) {
952 if (likely(rl[i].lcn >= (LCN)0))
953 return rl[i].lcn + (vcn - rl[i].vcn);
954 return rl[i].lcn;
958 * The terminator element is setup to the correct value, i.e. one of
959 * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
961 if (likely(rl[i].lcn < (LCN)0))
962 return rl[i].lcn;
963 /* Just in case... We could replace this with BUG() some day. */
964 return LCN_ENOENT;
968 * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
969 * @n: number for which to get the number of bytes for
971 * Return the number of bytes required to store @n unambiguously as
972 * a signed number.
974 * This is used in the context of the mapping pairs array to determine how
975 * many bytes will be needed in the array to store a given logical cluster
976 * number (lcn) or a specific run length.
978 * Return the number of bytes written. This function cannot fail.
980 static inline int ntfs_get_nr_significant_bytes(const s64 n)
982 s64 l = n;
983 int i;
984 s8 j;
986 i = 0;
987 do {
988 l >>= 8;
989 i++;
990 } while (l != 0 && l != -1);
991 j = (n >> 8 * (i - 1)) & 0xff;
992 /* If the sign bit is wrong, we need an extra byte. */
993 if ((n < 0 && j >= 0) || (n > 0 && j < 0))
994 i++;
995 return i;
999 * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
1000 * @vol: ntfs volume (needed for the ntfs version)
1001 * @rl: locked runlist to determine the size of the mapping pairs of
1002 * @start_vcn: vcn at which to start the mapping pairs array
1004 * Walk the locked runlist @rl and calculate the size in bytes of the mapping
1005 * pairs array corresponding to the runlist @rl, starting at vcn @start_vcn.
1006 * This for example allows us to allocate a buffer of the right size when
1007 * building the mapping pairs array.
1009 * If @rl is NULL, just return 1 (for the single terminator byte).
1011 * Return the calculated size in bytes on success. On error, return -errno.
1012 * The following error codes are defined:
1013 * -EINVAL - Run list contains unmapped elements. Make sure to only pass
1014 * fully mapped runlists to this function.
1015 * -EIO - The runlist is corrupt.
1017 * Locking: @rl must be locked on entry (either for reading or writing), it
1018 * remains locked throughout, and is left locked upon return.
1020 int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
1021 const runlist_element *rl, const VCN start_vcn)
1023 LCN prev_lcn;
1024 int rls;
1026 BUG_ON(start_vcn < 0);
1027 if (!rl) {
1028 BUG_ON(start_vcn);
1029 return 1;
1031 /* Skip to runlist element containing @start_vcn. */
1032 while (rl->length && start_vcn >= rl[1].vcn)
1033 rl++;
1034 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
1035 return -EINVAL;
1036 prev_lcn = 0;
1037 /* Always need the termining zero byte. */
1038 rls = 1;
1039 /* Do the first partial run if present. */
1040 if (start_vcn > rl->vcn) {
1041 s64 delta;
1043 /* We know rl->length != 0 already. */
1044 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1045 goto err_out;
1046 delta = start_vcn - rl->vcn;
1047 /* Header byte + length. */
1048 rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta);
1050 * If the logical cluster number (lcn) denotes a hole and we
1051 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1052 * zero space. On earlier NTFS versions we just store the lcn.
1053 * Note: this assumes that on NTFS 1.2-, holes are stored with
1054 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1056 if (rl->lcn >= 0 || vol->major_ver < 3) {
1057 prev_lcn = rl->lcn;
1058 if (rl->lcn >= 0)
1059 prev_lcn += delta;
1060 /* Change in lcn. */
1061 rls += ntfs_get_nr_significant_bytes(prev_lcn);
1063 /* Go to next runlist element. */
1064 rl++;
1066 /* Do the full runs. */
1067 for (; rl->length; rl++) {
1068 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1069 goto err_out;
1070 /* Header byte + length. */
1071 rls += 1 + ntfs_get_nr_significant_bytes(rl->length);
1073 * If the logical cluster number (lcn) denotes a hole and we
1074 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1075 * zero space. On earlier NTFS versions we just store the lcn.
1076 * Note: this assumes that on NTFS 1.2-, holes are stored with
1077 * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
1079 if (rl->lcn >= 0 || vol->major_ver < 3) {
1080 /* Change in lcn. */
1081 rls += ntfs_get_nr_significant_bytes(rl->lcn -
1082 prev_lcn);
1083 prev_lcn = rl->lcn;
1086 return rls;
1087 err_out:
1088 if (rl->lcn == LCN_RL_NOT_MAPPED)
1089 rls = -EINVAL;
1090 else
1091 rls = -EIO;
1092 return rls;
1096 * ntfs_write_significant_bytes - write the significant bytes of a number
1097 * @dst: destination buffer to write to
1098 * @dst_max: pointer to last byte of destination buffer for bounds checking
1099 * @n: number whose significant bytes to write
1101 * Store in @dst, the minimum bytes of the number @n which are required to
1102 * identify @n unambiguously as a signed number, taking care not to exceed
1103 * @dest_max, the maximum position within @dst to which we are allowed to
1104 * write.
1106 * This is used when building the mapping pairs array of a runlist to compress
1107 * a given logical cluster number (lcn) or a specific run length to the minumum
1108 * size possible.
1110 * Return the number of bytes written on success. On error, i.e. the
1111 * destination buffer @dst is too small, return -ENOSPC.
1113 static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
1114 const s64 n)
1116 s64 l = n;
1117 int i;
1118 s8 j;
1120 i = 0;
1121 do {
1122 if (dst > dst_max)
1123 goto err_out;
1124 *dst++ = l & 0xffll;
1125 l >>= 8;
1126 i++;
1127 } while (l != 0 && l != -1);
1128 j = (n >> 8 * (i - 1)) & 0xff;
1129 /* If the sign bit is wrong, we need an extra byte. */
1130 if (n < 0 && j >= 0) {
1131 if (dst > dst_max)
1132 goto err_out;
1133 i++;
1134 *dst = (s8)-1;
1135 } else if (n > 0 && j < 0) {
1136 if (dst > dst_max)
1137 goto err_out;
1138 i++;
1139 *dst = (s8)0;
1141 return i;
1142 err_out:
1143 return -ENOSPC;
1147 * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
1148 * @vol: ntfs volume (needed for the ntfs version)
1149 * @dst: destination buffer to which to write the mapping pairs array
1150 * @dst_len: size of destination buffer @dst in bytes
1151 * @rl: locked runlist for which to build the mapping pairs array
1152 * @start_vcn: vcn at which to start the mapping pairs array
1153 * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
1155 * Create the mapping pairs array from the locked runlist @rl, starting at vcn
1156 * @start_vcn and save the array in @dst. @dst_len is the size of @dst in
1157 * bytes and it should be at least equal to the value obtained by calling
1158 * ntfs_get_size_for_mapping_pairs().
1160 * If @rl is NULL, just write a single terminator byte to @dst.
1162 * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
1163 * the first vcn outside the destination buffer. Note that on error, @dst has
1164 * been filled with all the mapping pairs that will fit, thus it can be treated
1165 * as partial success, in that a new attribute extent needs to be created or
1166 * the next extent has to be used and the mapping pairs build has to be
1167 * continued with @start_vcn set to *@stop_vcn.
1169 * Return 0 on success and -errno on error. The following error codes are
1170 * defined:
1171 * -EINVAL - Run list contains unmapped elements. Make sure to only pass
1172 * fully mapped runlists to this function.
1173 * -EIO - The runlist is corrupt.
1174 * -ENOSPC - The destination buffer is too small.
1176 * Locking: @rl must be locked on entry (either for reading or writing), it
1177 * remains locked throughout, and is left locked upon return.
1179 int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
1180 const int dst_len, const runlist_element *rl,
1181 const VCN start_vcn, VCN *const stop_vcn)
1183 LCN prev_lcn;
1184 s8 *dst_max, *dst_next;
1185 int err = -ENOSPC;
1186 s8 len_len, lcn_len;
1188 BUG_ON(start_vcn < 0);
1189 BUG_ON(dst_len < 1);
1190 if (!rl) {
1191 BUG_ON(start_vcn);
1192 if (stop_vcn)
1193 *stop_vcn = 0;
1194 /* Terminator byte. */
1195 *dst = 0;
1196 return 0;
1198 /* Skip to runlist element containing @start_vcn. */
1199 while (rl->length && start_vcn >= rl[1].vcn)
1200 rl++;
1201 if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
1202 return -EINVAL;
1204 * @dst_max is used for bounds checking in
1205 * ntfs_write_significant_bytes().
1207 dst_max = dst + dst_len - 1;
1208 prev_lcn = 0;
1209 /* Do the first partial run if present. */
1210 if (start_vcn > rl->vcn) {
1211 s64 delta;
1213 /* We know rl->length != 0 already. */
1214 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1215 goto err_out;
1216 delta = start_vcn - rl->vcn;
1217 /* Write length. */
1218 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1219 rl->length - delta);
1220 if (len_len < 0)
1221 goto size_err;
1223 * If the logical cluster number (lcn) denotes a hole and we
1224 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1225 * zero space. On earlier NTFS versions we just write the lcn
1226 * change. FIXME: Do we need to write the lcn change or just
1227 * the lcn in that case? Not sure as I have never seen this
1228 * case on NT4. - We assume that we just need to write the lcn
1229 * change until someone tells us otherwise... (AIA)
1231 if (rl->lcn >= 0 || vol->major_ver < 3) {
1232 prev_lcn = rl->lcn;
1233 if (rl->lcn >= 0)
1234 prev_lcn += delta;
1235 /* Write change in lcn. */
1236 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1237 len_len, dst_max, prev_lcn);
1238 if (lcn_len < 0)
1239 goto size_err;
1240 } else
1241 lcn_len = 0;
1242 dst_next = dst + len_len + lcn_len + 1;
1243 if (dst_next > dst_max)
1244 goto size_err;
1245 /* Update header byte. */
1246 *dst = lcn_len << 4 | len_len;
1247 /* Position at next mapping pairs array element. */
1248 dst = dst_next;
1249 /* Go to next runlist element. */
1250 rl++;
1252 /* Do the full runs. */
1253 for (; rl->length; rl++) {
1254 if (rl->length < 0 || rl->lcn < LCN_HOLE)
1255 goto err_out;
1256 /* Write length. */
1257 len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
1258 rl->length);
1259 if (len_len < 0)
1260 goto size_err;
1262 * If the logical cluster number (lcn) denotes a hole and we
1263 * are on NTFS 3.0+, we don't store it at all, i.e. we need
1264 * zero space. On earlier NTFS versions we just write the lcn
1265 * change. FIXME: Do we need to write the lcn change or just
1266 * the lcn in that case? Not sure as I have never seen this
1267 * case on NT4. - We assume that we just need to write the lcn
1268 * change until someone tells us otherwise... (AIA)
1270 if (rl->lcn >= 0 || vol->major_ver < 3) {
1271 /* Write change in lcn. */
1272 lcn_len = ntfs_write_significant_bytes(dst + 1 +
1273 len_len, dst_max, rl->lcn - prev_lcn);
1274 if (lcn_len < 0)
1275 goto size_err;
1276 prev_lcn = rl->lcn;
1277 } else
1278 lcn_len = 0;
1279 dst_next = dst + len_len + lcn_len + 1;
1280 if (dst_next > dst_max)
1281 goto size_err;
1282 /* Update header byte. */
1283 *dst = lcn_len << 4 | len_len;
1284 /* Position at next mapping pairs array element. */
1285 dst = dst_next;
1287 /* Success. */
1288 err = 0;
1289 size_err:
1290 /* Set stop vcn. */
1291 if (stop_vcn)
1292 *stop_vcn = rl->vcn;
1293 /* Add terminator byte. */
1294 *dst = 0;
1295 return err;
1296 err_out:
1297 if (rl->lcn == LCN_RL_NOT_MAPPED)
1298 err = -EINVAL;
1299 else
1300 err = -EIO;
1301 return err;
1305 * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
1306 * @runlist: runlist to truncate
1307 * @new_length: the new length of the runlist in VCNs
1309 * Truncate the runlist described by @runlist as well as the memory buffer
1310 * holding the runlist elements to a length of @new_length VCNs.
1312 * If @new_length lies within the runlist, the runlist elements with VCNs of
1313 * @new_length and above are discarded.
1315 * If @new_length lies beyond the runlist, a sparse runlist element is added to
1316 * the end of the runlist @runlist or if the last runlist element is a sparse
1317 * one already, this is extended.
1319 * Return 0 on success and -errno on error.
1321 * Locking: The caller must hold @runlist->lock for writing.
1323 int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
1324 const s64 new_length)
1326 runlist_element *rl;
1327 int old_size;
1329 ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
1330 BUG_ON(!runlist);
1331 BUG_ON(new_length < 0);
1332 rl = runlist->rl;
1333 if (unlikely(!rl)) {
1335 * Create a runlist consisting of a sparse runlist element of
1336 * length @new_length followed by a terminator runlist element.
1338 rl = ntfs_malloc_nofs(PAGE_SIZE);
1339 if (unlikely(!rl)) {
1340 ntfs_error(vol->sb, "Not enough memory to allocate "
1341 "runlist element buffer.");
1342 return -ENOMEM;
1344 runlist->rl = rl;
1345 rl[1].length = rl->vcn = 0;
1346 rl->lcn = LCN_HOLE;
1347 rl[1].vcn = rl->length = new_length;
1348 rl[1].lcn = LCN_ENOENT;
1349 return 0;
1351 BUG_ON(new_length < rl->vcn);
1352 /* Find @new_length in the runlist. */
1353 while (likely(rl->length && new_length >= rl[1].vcn))
1354 rl++;
1356 * If not at the end of the runlist we need to shrink it.
1357 * If at the end of the runlist we need to expand it.
1359 if (rl->length) {
1360 runlist_element *trl;
1361 BOOL is_end;
1363 ntfs_debug("Shrinking runlist.");
1364 /* Determine the runlist size. */
1365 trl = rl + 1;
1366 while (likely(trl->length))
1367 trl++;
1368 old_size = trl - runlist->rl + 1;
1369 /* Truncate the run. */
1370 rl->length = new_length - rl->vcn;
1372 * If a run was partially truncated, make the following runlist
1373 * element a terminator.
1375 is_end = FALSE;
1376 if (rl->length) {
1377 rl++;
1378 if (!rl->length)
1379 is_end = TRUE;
1380 rl->vcn = new_length;
1381 rl->length = 0;
1383 rl->lcn = LCN_ENOENT;
1384 /* Reallocate memory if necessary. */
1385 if (!is_end) {
1386 int new_size = rl - runlist->rl + 1;
1387 rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
1388 if (IS_ERR(rl))
1389 ntfs_warning(vol->sb, "Failed to shrink "
1390 "runlist buffer. This just "
1391 "wastes a bit of memory "
1392 "temporarily so we ignore it "
1393 "and return success.");
1394 else
1395 runlist->rl = rl;
1397 } else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
1398 ntfs_debug("Expanding runlist.");
1400 * If there is a previous runlist element and it is a sparse
1401 * one, extend it. Otherwise need to add a new, sparse runlist
1402 * element.
1404 if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
1405 (rl - 1)->length = new_length - (rl - 1)->vcn;
1406 else {
1407 /* Determine the runlist size. */
1408 old_size = rl - runlist->rl + 1;
1409 /* Reallocate memory if necessary. */
1410 rl = ntfs_rl_realloc(runlist->rl, old_size,
1411 old_size + 1);
1412 if (IS_ERR(rl)) {
1413 ntfs_error(vol->sb, "Failed to expand runlist "
1414 "buffer, aborting.");
1415 return PTR_ERR(rl);
1417 runlist->rl = rl;
1419 * Set @rl to the same runlist element in the new
1420 * runlist as before in the old runlist.
1422 rl += old_size - 1;
1423 /* Add a new, sparse runlist element. */
1424 rl->lcn = LCN_HOLE;
1425 rl->length = new_length - rl->vcn;
1426 /* Add a new terminator runlist element. */
1427 rl++;
1428 rl->length = 0;
1430 rl->vcn = new_length;
1431 rl->lcn = LCN_ENOENT;
1432 } else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
1433 /* Runlist already has same size as requested. */
1434 rl->lcn = LCN_ENOENT;
1436 ntfs_debug("Done.");
1437 return 0;