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