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