2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2004 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/buffer_head.h>
27 /* Temporary helper functions -- might become macros */
30 * ntfs_rl_mm - runlist memmove
32 * It is up to the caller to serialize access to the runlist @base.
34 static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
37 if (likely((dst != src) && (size > 0)))
38 memmove(base + dst, base + src, size * sizeof (*base));
42 * ntfs_rl_mc - runlist memory copy
44 * It is up to the caller to serialize access to the runlists @dstbase and
47 static inline void ntfs_rl_mc(runlist_element *dstbase, int dst,
48 runlist_element *srcbase, int src, int size)
51 memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
55 * ntfs_rl_realloc - Reallocate memory for runlists
56 * @rl: original runlist
57 * @old_size: number of runlist elements in the original runlist @rl
58 * @new_size: number of runlist elements we need space for
60 * As the runlists grow, more memory will be required. To prevent the
61 * kernel having to allocate and reallocate large numbers of small bits of
62 * memory, this function returns and entire page of memory.
64 * It is up to the caller to serialize access to the runlist @rl.
66 * N.B. If the new allocation doesn't require a different number of pages in
67 * memory, the function will return the original pointer.
69 * On success, return a pointer to the newly allocated, or recycled, memory.
70 * On error, return -errno. The following error codes are defined:
71 * -ENOMEM - Not enough memory to allocate runlist array.
72 * -EINVAL - Invalid parameters were passed in.
74 static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
75 int old_size, int new_size)
77 runlist_element *new_rl;
79 old_size = PAGE_ALIGN(old_size * sizeof(*rl));
80 new_size = PAGE_ALIGN(new_size * sizeof(*rl));
81 if (old_size == new_size)
84 new_rl = ntfs_malloc_nofs(new_size);
85 if (unlikely(!new_rl))
86 return ERR_PTR(-ENOMEM);
88 if (likely(rl != NULL)) {
89 if (unlikely(old_size > new_size))
91 memcpy(new_rl, rl, old_size);
98 * ntfs_are_rl_mergeable - test if two runlists can be joined together
99 * @dst: original runlist
100 * @src: new runlist to test for mergeability with @dst
102 * Test if two runlists can be joined together. For this, their VCNs and LCNs
105 * It is up to the caller to serialize access to the runlists @dst and @src.
107 * Return: TRUE Success, the runlists can be merged.
108 * FALSE Failure, the runlists cannot be merged.
110 static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
111 runlist_element *src)
116 if ((dst->lcn < 0) || (src->lcn < 0)) /* Are we merging holes? */
118 if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
120 if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
127 * __ntfs_rl_merge - merge two runlists without testing if they can be merged
128 * @dst: original, destination runlist
129 * @src: new runlist to merge with @dst
131 * Merge the two runlists, writing into the destination runlist @dst. The
132 * caller must make sure the runlists can be merged or this will corrupt the
133 * destination runlist.
135 * It is up to the caller to serialize access to the runlists @dst and @src.
137 static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
139 dst->length += src->length;
143 * ntfs_rl_merge - test if two runlists can be joined together and merge them
144 * @dst: original, destination runlist
145 * @src: new runlist to merge with @dst
147 * Test if two runlists can be joined together. For this, their VCNs and LCNs
148 * must be adjacent. If they can be merged, perform the merge, writing into
149 * the destination runlist @dst.
151 * It is up to the caller to serialize access to the runlists @dst and @src.
153 * Return: TRUE Success, the runlists have been merged.
154 * FALSE Failure, the runlists cannot be merged and have not been
157 static inline BOOL ntfs_rl_merge(runlist_element *dst, runlist_element *src)
159 BOOL merge = ntfs_are_rl_mergeable(dst, src);
162 __ntfs_rl_merge(dst, src);
167 * ntfs_rl_append - append a runlist after a given element
168 * @dst: original runlist to be worked on
169 * @dsize: number of elements in @dst (including end marker)
170 * @src: runlist to be inserted into @dst
171 * @ssize: number of elements in @src (excluding end marker)
172 * @loc: append the new runlist @src after this element in @dst
174 * Append the runlist @src after element @loc in @dst. Merge the right end of
175 * the new runlist, if necessary. Adjust the size of the hole before the
178 * It is up to the caller to serialize access to the runlists @dst and @src.
180 * On success, return a pointer to the new, combined, runlist. Note, both
181 * runlists @dst and @src are deallocated before returning so you cannot use
182 * the pointers for anything any more. (Strictly speaking the returned runlist
183 * may be the same as @dst but this is irrelevant.)
185 * On error, return -errno. Both runlists are left unmodified. The following
186 * error codes are defined:
187 * -ENOMEM - Not enough memory to allocate runlist array.
188 * -EINVAL - Invalid parameters were passed in.
190 static inline runlist_element *ntfs_rl_append(runlist_element *dst,
191 int dsize, runlist_element *src, int ssize, int loc)
199 /* First, check if the right hand end needs merging. */
200 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
202 /* Space required: @dst size + @src size, less one if we merged. */
203 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
207 * We are guaranteed to succeed from here so can start modifying the
211 /* First, merge the right hand end, if necessary. */
213 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
217 /* Move the tail of @dst out of the way, then copy in @src. */
218 ntfs_rl_mm(dst, magic + 1, loc + 1 + right, dsize - loc - 1 - right);
219 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
221 /* Adjust the size of the preceding hole. */
222 dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
224 /* We may have changed the length of the file, so fix the end marker */
225 if (dst[magic + 1].lcn == LCN_ENOENT)
226 dst[magic + 1].vcn = dst[magic].vcn + dst[magic].length;
232 * ntfs_rl_insert - insert a runlist into another
233 * @dst: original runlist to be worked on
234 * @dsize: number of elements in @dst (including end marker)
235 * @src: new runlist to be inserted
236 * @ssize: number of elements in @src (excluding end marker)
237 * @loc: insert the new runlist @src before this element in @dst
239 * Insert the runlist @src before element @loc in the runlist @dst. Merge the
240 * left end of the new runlist, if necessary. Adjust the size of the hole
241 * after the inserted runlist.
243 * It is up to the caller to serialize access to the runlists @dst and @src.
245 * On success, return a pointer to the new, combined, runlist. Note, both
246 * runlists @dst and @src are deallocated before returning so you cannot use
247 * the pointers for anything any more. (Strictly speaking the returned runlist
248 * may be the same as @dst but this is irrelevant.)
250 * On error, return -errno. Both runlists are left unmodified. The following
251 * error codes are defined:
252 * -ENOMEM - Not enough memory to allocate runlist array.
253 * -EINVAL - Invalid parameters were passed in.
255 static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
256 int dsize, runlist_element *src, int ssize, int loc)
259 BOOL disc = FALSE; /* Discontinuity */
260 BOOL hole = FALSE; /* Following a hole */
266 /* disc => Discontinuity between the end of @dst and the start of @src.
267 * This means we might need to insert a hole.
268 * hole => @dst ends with a hole or an unmapped region which we can
269 * extend to match the discontinuity. */
271 disc = (src[0].vcn > 0);
275 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
277 merged_length = dst[loc - 1].length;
279 merged_length += src->length;
281 disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
283 hole = (dst[loc - 1].lcn == LCN_HOLE);
286 /* Space required: @dst size + @src size, less one if we merged, plus
287 * one if there was a discontinuity, less one for a trailing hole. */
288 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc - hole);
292 * We are guaranteed to succeed from here so can start modifying the
297 __ntfs_rl_merge(dst + loc - 1, src);
299 magic = loc + ssize - left + disc - hole;
301 /* Move the tail of @dst out of the way, then copy in @src. */
302 ntfs_rl_mm(dst, magic, loc, dsize - loc);
303 ntfs_rl_mc(dst, loc + disc - hole, src, left, ssize - left);
305 /* Adjust the VCN of the last run ... */
306 if (dst[magic].lcn <= LCN_HOLE)
307 dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
308 /* ... and the length. */
309 if (dst[magic].lcn == LCN_HOLE || dst[magic].lcn == LCN_RL_NOT_MAPPED)
310 dst[magic].length = dst[magic + 1].vcn - dst[magic].vcn;
312 /* Writing beyond the end of the file and there's a discontinuity. */
315 dst[loc - 1].length = dst[loc].vcn - dst[loc - 1].vcn;
318 dst[loc].vcn = dst[loc - 1].vcn +
320 dst[loc].length = dst[loc + 1].vcn -
324 dst[loc].length = dst[loc + 1].vcn;
326 dst[loc].lcn = LCN_RL_NOT_MAPPED;
331 if (dst[magic].lcn == LCN_ENOENT)
332 dst[magic].vcn = dst[magic - 1].vcn +
333 dst[magic - 1].length;
339 * ntfs_rl_replace - overwrite a runlist element with another runlist
340 * @dst: original runlist to be worked on
341 * @dsize: number of elements in @dst (including end marker)
342 * @src: new runlist to be inserted
343 * @ssize: number of elements in @src (excluding end marker)
344 * @loc: index in runlist @dst to overwrite with @src
346 * Replace the runlist element @dst at @loc with @src. Merge the left and
347 * right ends of the inserted runlist, if necessary.
349 * It is up to the caller to serialize access to the runlists @dst and @src.
351 * On success, return a pointer to the new, combined, runlist. Note, both
352 * runlists @dst and @src are deallocated before returning so you cannot use
353 * the pointers for anything any more. (Strictly speaking the returned runlist
354 * may be the same as @dst but this is irrelevant.)
356 * On error, return -errno. Both runlists are left unmodified. The following
357 * error codes are defined:
358 * -ENOMEM - Not enough memory to allocate runlist array.
359 * -EINVAL - Invalid parameters were passed in.
361 static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
362 int dsize, runlist_element *src, int ssize, int loc)
371 /* First, merge the left and right ends, if necessary. */
372 right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
374 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
376 /* Allocate some space. We'll need less if the left, right, or both
377 * ends were merged. */
378 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left - right);
382 * We are guaranteed to succeed from here so can start modifying the
386 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
388 __ntfs_rl_merge(dst + loc - 1, src);
390 /* FIXME: What does this mean? (AIA) */
391 magic = loc + ssize - left;
393 /* Move the tail of @dst out of the way, then copy in @src. */
394 ntfs_rl_mm(dst, magic, loc + right + 1, dsize - loc - right - 1);
395 ntfs_rl_mc(dst, loc, src, left, ssize - left);
397 /* We may have changed the length of the file, so fix the end marker */
398 if (dst[magic].lcn == LCN_ENOENT)
399 dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
404 * ntfs_rl_split - insert a runlist into the centre of a hole
405 * @dst: original runlist to be worked on
406 * @dsize: number of elements in @dst (including end marker)
407 * @src: new runlist to be inserted
408 * @ssize: number of elements in @src (excluding end marker)
409 * @loc: index in runlist @dst at which to split and insert @src
411 * Split the runlist @dst at @loc into two and insert @new in between the two
412 * fragments. No merging of runlists is necessary. Adjust the size of the
415 * It is up to the caller to serialize access to the runlists @dst and @src.
417 * On success, return a pointer to the new, combined, runlist. Note, both
418 * runlists @dst and @src are deallocated before returning so you cannot use
419 * the pointers for anything any more. (Strictly speaking the returned runlist
420 * may be the same as @dst but this is irrelevant.)
422 * On error, return -errno. Both runlists are left unmodified. The following
423 * error codes are defined:
424 * -ENOMEM - Not enough memory to allocate runlist array.
425 * -EINVAL - Invalid parameters were passed in.
427 static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
428 runlist_element *src, int ssize, int loc)
433 /* Space required: @dst size + @src size + one new hole. */
434 dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
438 * We are guaranteed to succeed from here so can start modifying the
442 /* Move the tail of @dst out of the way, then copy in @src. */
443 ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
444 ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
446 /* Adjust the size of the holes either size of @src. */
447 dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
448 dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
449 dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
455 * ntfs_merge_runlists - merge two runlists into one
456 * @drl: original runlist to be worked on
457 * @srl: new runlist to be merged into @drl
459 * First we sanity check the two runlists @srl and @drl to make sure that they
460 * are sensible and can be merged. The runlist @srl must be either after the
461 * runlist @drl or completely within a hole (or unmapped region) in @drl.
463 * It is up to the caller to serialize access to the runlists @drl and @srl.
465 * Merging of runlists is necessary in two cases:
466 * 1. When attribute lists are used and a further extent is being mapped.
467 * 2. When new clusters are allocated to fill a hole or extend a file.
469 * There are four possible ways @srl can be merged. It can:
470 * - be inserted at the beginning of a hole,
471 * - split the hole in two and be inserted between the two fragments,
472 * - be appended at the end of a hole, or it can
473 * - replace the whole hole.
474 * It can also be appended to the end of the runlist, which is just a variant
475 * of the insert case.
477 * On success, return a pointer to the new, combined, runlist. Note, both
478 * runlists @drl and @srl are deallocated before returning so you cannot use
479 * the pointers for anything any more. (Strictly speaking the returned runlist
480 * may be the same as @dst but this is irrelevant.)
482 * On error, return -errno. Both runlists are left unmodified. The following
483 * error codes are defined:
484 * -ENOMEM - Not enough memory to allocate runlist array.
485 * -EINVAL - Invalid parameters were passed in.
486 * -ERANGE - The runlists overlap and cannot be merged.
488 runlist_element *ntfs_merge_runlists(runlist_element *drl,
489 runlist_element *srl)
491 int di, si; /* Current index into @[ds]rl. */
492 int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
493 int dins; /* Index into @drl at which to insert @srl. */
494 int dend, send; /* Last index into @[ds]rl. */
495 int dfinal, sfinal; /* The last index into @[ds]rl with
502 ntfs_debug_dump_runlist(drl);
504 ntfs_debug_dump_runlist(srl);
507 /* Check for silly calling... */
510 if (IS_ERR(srl) || IS_ERR(drl))
511 return ERR_PTR(-EINVAL);
513 /* Check for the case where the first mapping is being done now. */
514 if (unlikely(!drl)) {
516 /* Complete the source runlist if necessary. */
517 if (unlikely(drl[0].vcn)) {
518 /* Scan to the end of the source runlist. */
519 for (dend = 0; likely(drl[dend].length); dend++)
521 drl = ntfs_rl_realloc(drl, dend, dend + 1);
524 /* Insert start element at the front of the runlist. */
525 ntfs_rl_mm(drl, 1, 0, dend);
527 drl[0].lcn = LCN_RL_NOT_MAPPED;
528 drl[0].length = drl[1].vcn;
535 /* Skip any unmapped start element(s) in the source runlist. */
536 while (srl[si].length && srl[si].lcn < (LCN)LCN_HOLE)
539 /* Can't have an entirely unmapped source runlist. */
540 BUG_ON(!srl[si].length);
542 /* Record the starting points. */
546 * Skip forward in @drl until we reach the position where @srl needs to
547 * be inserted. If we reach the end of @drl, @srl just needs to be
550 for (; drl[di].length; di++) {
551 if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
556 /* Sanity check for illegal overlaps. */
557 if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
558 (srl[si].lcn >= 0)) {
559 ntfs_error(NULL, "Run lists overlap. Cannot merge!");
560 return ERR_PTR(-ERANGE);
563 /* Scan to the end of both runlists in order to know their sizes. */
564 for (send = si; srl[send].length; send++)
566 for (dend = di; drl[dend].length; dend++)
569 if (srl[send].lcn == (LCN)LCN_ENOENT)
570 marker_vcn = srl[marker = send].vcn;
572 /* Scan to the last element with lcn >= LCN_HOLE. */
573 for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
575 for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
581 int ds = dend + 1; /* Number of elements in drl & srl */
582 int ss = sfinal - sstart + 1;
584 start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
585 (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
586 finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
587 ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
588 (srl[send - 1].vcn + srl[send - 1].length)));
590 /* Or we'll lose an end marker */
591 if (start && finish && (drl[dins].length == 0))
593 if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
596 ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
597 ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
598 ntfs_debug("start = %i, finish = %i", start, finish);
599 ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
603 drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
605 drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
608 drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
610 drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
613 ntfs_error(NULL, "Merge failed.");
618 ntfs_debug("Triggering marker code.");
619 for (ds = dend; drl[ds].length; ds++)
621 /* We only need to care if @srl ended after @drl. */
622 if (drl[ds].vcn <= marker_vcn) {
625 if (drl[ds].vcn == marker_vcn) {
626 ntfs_debug("Old marker = 0x%llx, replacing "
630 drl[ds].lcn = (LCN)LCN_ENOENT;
634 * We need to create an unmapped runlist element in
635 * @drl or extend an existing one before adding the
638 if (drl[ds].lcn == (LCN)LCN_ENOENT) {
642 if (drl[ds].lcn != (LCN)LCN_RL_NOT_MAPPED) {
643 /* Add an unmapped runlist element. */
645 /* FIXME/TODO: We need to have the
646 * extra memory already! (AIA) */
647 drl = ntfs_rl_realloc(drl, ds, ds + 2);
653 /* Need to set vcn if it isn't set already. */
655 drl[ds].vcn = drl[ds - 1].vcn +
657 drl[ds].lcn = (LCN)LCN_RL_NOT_MAPPED;
658 /* We now used up a slot. */
661 drl[ds].length = marker_vcn - drl[ds].vcn;
662 /* Finally add the ENOENT terminator. */
665 /* FIXME/TODO: We need to have the extra
666 * memory already! (AIA) */
667 drl = ntfs_rl_realloc(drl, ds, ds + 1);
671 drl[ds].vcn = marker_vcn;
672 drl[ds].lcn = (LCN)LCN_ENOENT;
673 drl[ds].length = (s64)0;
679 /* The merge was completed successfully. */
680 ntfs_debug("Merged runlist:");
681 ntfs_debug_dump_runlist(drl);
685 /* Critical error! We cannot afford to fail here. */
686 ntfs_error(NULL, "Critical error! Not enough memory.");
687 panic("NTFS: Cannot continue.");
691 * decompress_mapping_pairs - convert mapping pairs array to runlist
692 * @vol: ntfs volume on which the attribute resides
693 * @attr: attribute record whose mapping pairs array to decompress
694 * @old_rl: optional runlist in which to insert @attr's runlist
696 * It is up to the caller to serialize access to the runlist @old_rl.
698 * Decompress the attribute @attr's mapping pairs array into a runlist. On
699 * success, return the decompressed runlist.
701 * If @old_rl is not NULL, decompressed runlist is inserted into the
702 * appropriate place in @old_rl and the resultant, combined runlist is
703 * returned. The original @old_rl is deallocated.
705 * On error, return -errno. @old_rl is left unmodified in that case.
707 * The following error codes are defined:
708 * -ENOMEM - Not enough memory to allocate runlist array.
709 * -EIO - Corrupt runlist.
710 * -EINVAL - Invalid parameters were passed in.
711 * -ERANGE - The two runlists overlap.
713 * FIXME: For now we take the conceptionally simplest approach of creating the
714 * new runlist disregarding the already existing one and then splicing the
715 * two into one, if that is possible (we check for overlap and discard the new
716 * runlist if overlap present before returning ERR_PTR(-ERANGE)).
718 runlist_element *decompress_mapping_pairs(const ntfs_volume *vol,
719 const ATTR_RECORD *attr, runlist_element *old_rl)
721 VCN vcn; /* Current vcn. */
722 LCN lcn; /* Current lcn. */
723 s64 deltaxcn; /* Change in [vl]cn. */
724 runlist_element *rl; /* The output runlist. */
725 u8 *buf; /* Current position in mapping pairs array. */
726 u8 *attr_end; /* End of attribute. */
727 int rlsize; /* Size of runlist buffer. */
728 u16 rlpos; /* Current runlist position in units of
730 u8 b; /* Current byte offset in buf. */
733 /* Make sure attr exists and is non-resident. */
734 if (!attr || !attr->non_resident || sle64_to_cpu(
735 attr->data.non_resident.lowest_vcn) < (VCN)0) {
736 ntfs_error(vol->sb, "Invalid arguments.");
737 return ERR_PTR(-EINVAL);
740 /* Start at vcn = lowest_vcn and lcn 0. */
741 vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
743 /* Get start of the mapping pairs array. */
744 buf = (u8*)attr + le16_to_cpu(
745 attr->data.non_resident.mapping_pairs_offset);
746 attr_end = (u8*)attr + le32_to_cpu(attr->length);
747 if (unlikely(buf < (u8*)attr || buf > attr_end)) {
748 ntfs_error(vol->sb, "Corrupt attribute.");
749 return ERR_PTR(-EIO);
751 /* Current position in runlist array. */
753 /* Allocate first page and set current runlist size to one page. */
754 rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
756 return ERR_PTR(-ENOMEM);
757 /* Insert unmapped starting element if necessary. */
760 rl->lcn = (LCN)LCN_RL_NOT_MAPPED;
764 while (buf < attr_end && *buf) {
766 * Allocate more memory if needed, including space for the
767 * not-mapped and terminator elements. ntfs_malloc_nofs()
768 * operates on whole pages only.
770 if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
771 runlist_element *rl2;
773 rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
774 if (unlikely(!rl2)) {
776 return ERR_PTR(-ENOMEM);
778 memcpy(rl2, rl, rlsize);
783 /* Enter the current vcn into the current runlist element. */
786 * Get the change in vcn, i.e. the run length in clusters.
787 * Doing it this way ensures that we signextend negative values.
788 * A negative run length doesn't make any sense, but hey, I
789 * didn't make up the NTFS specs and Windows NT4 treats the run
790 * length as a signed value so that's how it is...
794 if (unlikely(buf + b > attr_end))
796 for (deltaxcn = (s8)buf[b--]; b; b--)
797 deltaxcn = (deltaxcn << 8) + buf[b];
798 } else { /* The length entry is compulsory. */
799 ntfs_error(vol->sb, "Missing length entry in mapping "
804 * Assume a negative length to indicate data corruption and
805 * hence clean-up and return NULL.
807 if (unlikely(deltaxcn < 0)) {
808 ntfs_error(vol->sb, "Invalid length in mapping pairs "
813 * Enter the current run length into the current runlist
816 rl[rlpos].length = deltaxcn;
817 /* Increment the current vcn by the current run length. */
820 * There might be no lcn change at all, as is the case for
821 * sparse clusters on NTFS 3.0+, in which case we set the lcn
825 rl[rlpos].lcn = (LCN)LCN_HOLE;
827 /* Get the lcn change which really can be negative. */
829 b = b2 + ((*buf >> 4) & 0xf);
830 if (buf + b > attr_end)
832 for (deltaxcn = (s8)buf[b--]; b > b2; b--)
833 deltaxcn = (deltaxcn << 8) + buf[b];
834 /* Change the current lcn to its new value. */
838 * On NTFS 1.2-, apparently can have lcn == -1 to
839 * indicate a hole. But we haven't verified ourselves
840 * whether it is really the lcn or the deltaxcn that is
841 * -1. So if either is found give us a message so we
842 * can investigate it further!
844 if (vol->major_ver < 3) {
845 if (unlikely(deltaxcn == (LCN)-1))
846 ntfs_error(vol->sb, "lcn delta == -1");
847 if (unlikely(lcn == (LCN)-1))
848 ntfs_error(vol->sb, "lcn == -1");
851 /* Check lcn is not below -1. */
852 if (unlikely(lcn < (LCN)-1)) {
853 ntfs_error(vol->sb, "Invalid LCN < -1 in "
854 "mapping pairs array.");
857 /* Enter the current lcn into the runlist element. */
860 /* Get to the next runlist element. */
862 /* Increment the buffer position to the next mapping pair. */
863 buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
865 if (unlikely(buf >= attr_end))
868 * If there is a highest_vcn specified, it must be equal to the final
869 * vcn in the runlist - 1, or something has gone badly wrong.
871 deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
872 if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
874 ntfs_error(vol->sb, "Corrupt mapping pairs array in "
875 "non-resident attribute.");
878 /* Setup not mapped runlist element if this is the base extent. */
879 if (!attr->data.non_resident.lowest_vcn) {
882 max_cluster = (sle64_to_cpu(
883 attr->data.non_resident.allocated_size) +
884 vol->cluster_size - 1) >>
885 vol->cluster_size_bits;
887 * If there is a difference between the highest_vcn and the
888 * highest cluster, the runlist is either corrupt or, more
889 * likely, there are more extents following this one.
891 if (deltaxcn < --max_cluster) {
892 ntfs_debug("More extents to follow; deltaxcn = 0x%llx, "
893 "max_cluster = 0x%llx",
894 (unsigned long long)deltaxcn,
895 (unsigned long long)max_cluster);
897 vcn += rl[rlpos].length = max_cluster - deltaxcn;
898 rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED;
900 } else if (unlikely(deltaxcn > max_cluster)) {
901 ntfs_error(vol->sb, "Corrupt attribute. deltaxcn = "
902 "0x%llx, max_cluster = 0x%llx",
903 (unsigned long long)deltaxcn,
904 (unsigned long long)max_cluster);
907 rl[rlpos].lcn = (LCN)LCN_ENOENT;
908 } else /* Not the base extent. There may be more extents to follow. */
909 rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED;
911 /* Setup terminating runlist element. */
913 rl[rlpos].length = (s64)0;
914 /* If no existing runlist was specified, we are done. */
916 ntfs_debug("Mapping pairs array successfully decompressed:");
917 ntfs_debug_dump_runlist(rl);
920 /* Now combine the new and old runlists checking for overlaps. */
921 old_rl = ntfs_merge_runlists(old_rl, rl);
922 if (likely(!IS_ERR(old_rl)))
925 ntfs_error(vol->sb, "Failed to merge runlists.");
928 ntfs_error(vol->sb, "Corrupt attribute.");
931 return ERR_PTR(-EIO);
935 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
936 * @ni: ntfs inode for which to map (part of) a runlist
937 * @vcn: map runlist part containing this vcn
939 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
941 * Return 0 on success and -errno on error.
943 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
944 * - This function takes the lock for writing and modifies the runlist.
946 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
949 ntfs_attr_search_ctx *ctx;
953 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
954 (unsigned long long)vcn);
959 base_ni = ni->ext.base_ntfs_ino;
961 mrec = map_mft_record(base_ni);
963 return PTR_ERR(mrec);
964 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
965 if (unlikely(!ctx)) {
969 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
970 CASE_SENSITIVE, vcn, NULL, 0, ctx);
974 down_write(&ni->runlist.lock);
975 /* Make sure someone else didn't do the work while we were sleeping. */
976 if (likely(ntfs_vcn_to_lcn(ni->runlist.rl, vcn) <= LCN_RL_NOT_MAPPED)) {
979 rl = decompress_mapping_pairs(ni->vol, ctx->attr,
986 up_write(&ni->runlist.lock);
989 ntfs_attr_put_search_ctx(ctx);
991 unmap_mft_record(base_ni);
996 * ntfs_vcn_to_lcn - convert a vcn into a lcn given a runlist
997 * @rl: runlist to use for conversion
998 * @vcn: vcn to convert
1000 * Convert the virtual cluster number @vcn of an attribute into a logical
1001 * cluster number (lcn) of a device using the runlist @rl to map vcns to their
1002 * corresponding lcns.
1004 * It is up to the caller to serialize access to the runlist @rl.
1006 * Since lcns must be >= 0, we use negative return values with special meaning:
1008 * Return value Meaning / Description
1009 * ==================================================
1010 * -1 = LCN_HOLE Hole / not allocated on disk.
1011 * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been
1012 * inserted into the runlist yet.
1013 * -3 = LCN_ENOENT There is no such vcn in the attribute.
1015 * Locking: - The caller must have locked the runlist (for reading or writing).
1016 * - This function does not touch the lock.
1018 LCN ntfs_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
1024 * If rl is NULL, assume that we have found an unmapped runlist. The
1025 * caller can then attempt to map it and fail appropriately if
1029 return (LCN)LCN_RL_NOT_MAPPED;
1031 /* Catch out of lower bounds vcn. */
1032 if (unlikely(vcn < rl[0].vcn))
1033 return (LCN)LCN_ENOENT;
1035 for (i = 0; likely(rl[i].length); i++) {
1036 if (unlikely(vcn < rl[i+1].vcn)) {
1037 if (likely(rl[i].lcn >= (LCN)0))
1038 return rl[i].lcn + (vcn - rl[i].vcn);
1043 * The terminator element is setup to the correct value, i.e. one of
1044 * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
1046 if (likely(rl[i].lcn < (LCN)0))
1048 /* Just in case... We could replace this with BUG() some day. */
1049 return (LCN)LCN_ENOENT;
1053 * ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode
1054 * @ni: ntfs inode describing the runlist to search
1056 * @need_write: if false, lock for reading and if true, lock for writing
1058 * Find the virtual cluster number @vcn in the runlist described by the ntfs
1059 * inode @ni and return the address of the runlist element containing the @vcn.
1060 * The runlist is left locked and the caller has to unlock it. If @need_write
1061 * is true, the runlist is locked for writing and if @need_write is false, the
1062 * runlist is locked for reading. In the error case, the runlist is not left
1065 * Note you need to distinguish between the lcn of the returned runlist element
1066 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
1067 * read and allocate clusters on write.
1069 * Return the runlist element containing the @vcn on success and
1070 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
1071 * to decide if the return is success or failure and PTR_ERR() to get to the
1072 * error code if IS_ERR() is true.
1074 * The possible error return codes are:
1075 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
1076 * -ENOMEM - Not enough memory to map runlist.
1077 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
1079 * Locking: - The runlist must be unlocked on entry.
1080 * - On failing return, the runlist is unlocked.
1081 * - On successful return, the runlist is locked. If @need_write us
1082 * true, it is locked for writing. Otherwise is is locked for
1085 runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
1086 const BOOL need_write)
1088 runlist_element *rl;
1090 BOOL is_retry = FALSE;
1092 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, lock for %sing.",
1093 ni->mft_no, (unsigned long long)vcn,
1094 !need_write ? "read" : "writ");
1096 BUG_ON(!NInoNonResident(ni));
1100 down_read(&ni->runlist.lock);
1102 down_write(&ni->runlist.lock);
1103 rl = ni->runlist.rl;
1104 if (likely(rl && vcn >= rl[0].vcn)) {
1105 while (likely(rl->length)) {
1106 if (likely(vcn < rl[1].vcn)) {
1107 if (likely(rl->lcn >= (LCN)LCN_HOLE)) {
1108 ntfs_debug("Done.");
1115 if (likely(rl->lcn != (LCN)LCN_RL_NOT_MAPPED)) {
1116 if (likely(rl->lcn == (LCN)LCN_ENOENT))
1123 up_read(&ni->runlist.lock);
1125 up_write(&ni->runlist.lock);
1126 if (!err && !is_retry) {
1128 * The @vcn is in an unmapped region, map the runlist and
1131 err = ntfs_map_runlist(ni, vcn);
1134 goto lock_retry_remap;
1137 * -EINVAL and -ENOENT coming from a failed mapping attempt are
1138 * equivalent to i/o errors for us as they should not happen in
1141 if (err == -EINVAL || err == -ENOENT)
1145 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
1146 return ERR_PTR(err);
1150 * ntfs_attr_find - find (next) attribute in mft record
1151 * @type: attribute type to find
1152 * @name: attribute name to find (optional, i.e. NULL means don't care)
1153 * @name_len: attribute name length (only needed if @name present)
1154 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1155 * @val: attribute value to find (optional, resident attributes only)
1156 * @val_len: attribute value length
1157 * @ctx: search context with mft record and attribute to search from
1159 * You should not need to call this function directly. Use ntfs_attr_lookup()
1162 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
1163 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
1164 * attribute of @type, optionally @name and @val.
1166 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
1167 * point to the found attribute.
1169 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
1170 * @ctx->attr will point to the attribute before which the attribute being
1171 * searched for would need to be inserted if such an action were to be desired.
1173 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
1174 * undefined and in particular do not rely on it not changing.
1176 * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
1177 * is FALSE, the search begins after @ctx->attr.
1179 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
1180 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
1181 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
1182 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
1183 * sensitive. When @name is present, @name_len is the @name length in Unicode
1186 * If @name is not present (NULL), we assume that the unnamed attribute is
1187 * being searched for.
1189 * Finally, the resident attribute value @val is looked for, if present. If
1190 * @val is not present (NULL), @val_len is ignored.
1192 * ntfs_attr_find() only searches the specified mft record and it ignores the
1193 * presence of an attribute list attribute (unless it is the one being searched
1194 * for, obviously). If you need to take attribute lists into consideration,
1195 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
1196 * use ntfs_attr_find() to search for extent records of non-resident
1197 * attributes, as extents with lowest_vcn != 0 are usually described by the
1198 * attribute list attribute only. - Note that it is possible that the first
1199 * extent is only in the attribute list while the last extent is in the base
1200 * mft record, so do not rely on being able to find the first extent in the
1203 * Warning: Never use @val when looking for attribute types which can be
1204 * non-resident as this most likely will result in a crash!
1206 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
1207 const u32 name_len, const IGNORE_CASE_BOOL ic,
1208 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
1215 if (ic == IGNORE_CASE) {
1216 vol = ctx->ntfs_ino->vol;
1217 upcase = vol->upcase;
1218 upcase_len = vol->upcase_len;
1225 * Iterate over attributes in mft record starting at @ctx->attr, or the
1226 * attribute following that, if @ctx->is_first is TRUE.
1228 if (ctx->is_first) {
1230 ctx->is_first = FALSE;
1232 a = (ATTR_RECORD*)((u8*)ctx->attr +
1233 le32_to_cpu(ctx->attr->length));
1234 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
1235 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1236 le32_to_cpu(ctx->mrec->bytes_allocated))
1239 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
1242 if (unlikely(!a->length))
1244 if (a->type != type)
1247 * If @name is present, compare the two names. If @name is
1248 * missing, assume we want an unnamed attribute.
1251 /* The search failed if the found attribute is named. */
1254 } else if (!ntfs_are_names_equal(name, name_len,
1255 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
1256 a->name_length, ic, upcase, upcase_len)) {
1259 rc = ntfs_collate_names(name, name_len,
1260 (ntfschar*)((u8*)a +
1261 le16_to_cpu(a->name_offset)),
1262 a->name_length, 1, IGNORE_CASE,
1263 upcase, upcase_len);
1265 * If @name collates before a->name, there is no
1266 * matching attribute.
1270 /* If the strings are not equal, continue search. */
1273 rc = ntfs_collate_names(name, name_len,
1274 (ntfschar*)((u8*)a +
1275 le16_to_cpu(a->name_offset)),
1276 a->name_length, 1, CASE_SENSITIVE,
1277 upcase, upcase_len);
1284 * The names match or @name not present and attribute is
1285 * unnamed. If no @val specified, we have found the attribute
1290 /* @val is present; compare values. */
1294 rc = memcmp(val, (u8*)a + le16_to_cpu(
1295 a->data.resident.value_offset),
1296 min_t(u32, val_len, le32_to_cpu(
1297 a->data.resident.value_length)));
1299 * If @val collates before the current attribute's
1300 * value, there is no matching attribute.
1306 a->data.resident.value_length);
1315 ntfs_error(NULL, "Inode is corrupt. Run chkdsk.");
1321 * load_attribute_list - load an attribute list into memory
1322 * @vol: ntfs volume from which to read
1323 * @runlist: runlist of the attribute list
1324 * @al_start: destination buffer
1325 * @size: size of the destination buffer in bytes
1326 * @initialized_size: initialized size of the attribute list
1328 * Walk the runlist @runlist and load all clusters from it copying them into
1329 * the linear buffer @al. The maximum number of bytes copied to @al is @size
1330 * bytes. Note, @size does not need to be a multiple of the cluster size. If
1331 * @initialized_size is less than @size, the region in @al between
1332 * @initialized_size and @size will be zeroed and not read from disk.
1334 * Return 0 on success or -errno on error.
1336 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
1337 const s64 size, const s64 initialized_size)
1341 u8 *al_end = al + initialized_size;
1342 runlist_element *rl;
1343 struct buffer_head *bh;
1344 struct super_block *sb;
1345 unsigned long block_size;
1346 unsigned long block, max_block;
1348 unsigned char block_size_bits;
1350 ntfs_debug("Entering.");
1351 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
1352 initialized_size > size)
1354 if (!initialized_size) {
1355 memset(al, 0, size);
1359 block_size = sb->s_blocksize;
1360 block_size_bits = sb->s_blocksize_bits;
1361 down_read(&runlist->lock);
1363 /* Read all clusters specified by the runlist one run at a time. */
1364 while (rl->length) {
1365 lcn = ntfs_vcn_to_lcn(rl, rl->vcn);
1366 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
1367 (unsigned long long)rl->vcn,
1368 (unsigned long long)lcn);
1369 /* The attribute list cannot be sparse. */
1371 ntfs_error(sb, "ntfs_vcn_to_lcn() failed. Cannot read "
1375 block = lcn << vol->cluster_size_bits >> block_size_bits;
1376 /* Read the run from device in chunks of block_size bytes. */
1377 max_block = block + (rl->length << vol->cluster_size_bits >>
1379 ntfs_debug("max_block = 0x%lx.", max_block);
1381 ntfs_debug("Reading block = 0x%lx.", block);
1382 bh = sb_bread(sb, block);
1384 ntfs_error(sb, "sb_bread() failed. Cannot "
1385 "read attribute list.");
1388 if (al + block_size >= al_end)
1390 memcpy(al, bh->b_data, block_size);
1393 } while (++block < max_block);
1396 if (initialized_size < size) {
1398 memset(al_start + initialized_size, 0, size - initialized_size);
1401 up_read(&runlist->lock);
1408 * Note: The attribute list can be smaller than its allocation
1409 * by multiple clusters. This has been encountered by at least
1410 * two people running Windows XP, thus we cannot do any
1411 * truncation sanity checking here. (AIA)
1413 memcpy(al, bh->b_data, al_end - al);
1415 if (initialized_size < size)
1420 /* Real overflow! */
1421 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
1429 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
1430 * @type: attribute type to find
1431 * @name: attribute name to find (optional, i.e. NULL means don't care)
1432 * @name_len: attribute name length (only needed if @name present)
1433 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1434 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1435 * @val: attribute value to find (optional, resident attributes only)
1436 * @val_len: attribute value length
1437 * @ctx: search context with mft record and attribute to search from
1439 * You should not need to call this function directly. Use ntfs_attr_lookup()
1442 * Find an attribute by searching the attribute list for the corresponding
1443 * attribute list entry. Having found the entry, map the mft record if the
1444 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
1445 * in there and return it.
1447 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
1448 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
1449 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
1450 * then the base inode).
1452 * After finishing with the attribute/mft record you need to call
1453 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1454 * mapped inodes, etc).
1456 * If the attribute is found, ntfs_external_attr_find() returns 0 and
1457 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
1458 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
1459 * the attribute list entry for the attribute.
1461 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
1462 * @ctx->attr will point to the attribute in the base mft record before which
1463 * the attribute being searched for would need to be inserted if such an action
1464 * were to be desired. @ctx->mrec will point to the mft record in which
1465 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
1466 * entry of the attribute before which the attribute being searched for would
1467 * need to be inserted if such an action were to be desired.
1469 * Thus to insert the not found attribute, one wants to add the attribute to
1470 * @ctx->mrec (the base mft record) and if there is not enough space, the
1471 * attribute should be placed in a newly allocated extent mft record. The
1472 * attribute list entry for the inserted attribute should be inserted in the
1473 * attribute list attribute at @ctx->al_entry.
1475 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
1476 * @ctx->attr is undefined and in particular do not rely on it not changing.
1478 static int ntfs_external_attr_find(const ATTR_TYPE type,
1479 const ntfschar *name, const u32 name_len,
1480 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
1481 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
1483 ntfs_inode *base_ni, *ni;
1485 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
1486 u8 *al_start, *al_end;
1491 static const char *es = " Unmount and run chkdsk.";
1494 base_ni = ctx->base_ntfs_ino;
1495 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
1497 /* First call happens with the base mft record. */
1498 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
1499 ctx->base_mrec = ctx->mrec;
1502 ctx->base_attr = ctx->attr;
1506 al_start = base_ni->attr_list;
1507 al_end = al_start + base_ni->attr_list_size;
1509 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
1511 * Iterate over entries in attribute list starting at @ctx->al_entry,
1512 * or the entry following that, if @ctx->is_first is TRUE.
1514 if (ctx->is_first) {
1515 al_entry = ctx->al_entry;
1516 ctx->is_first = FALSE;
1518 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
1519 le16_to_cpu(ctx->al_entry->length));
1520 for (;; al_entry = next_al_entry) {
1521 /* Out of bounds check. */
1522 if ((u8*)al_entry < base_ni->attr_list ||
1523 (u8*)al_entry > al_end)
1524 break; /* Inode is corrupt. */
1525 ctx->al_entry = al_entry;
1526 /* Catch the end of the attribute list. */
1527 if ((u8*)al_entry == al_end)
1529 if (!al_entry->length)
1531 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
1532 le16_to_cpu(al_entry->length) > al_end)
1534 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
1535 le16_to_cpu(al_entry->length));
1536 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
1538 if (type != al_entry->type)
1541 * If @name is present, compare the two names. If @name is
1542 * missing, assume we want an unnamed attribute.
1544 al_name_len = al_entry->name_length;
1545 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
1549 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
1550 name_len, ic, vol->upcase, vol->upcase_len)) {
1553 rc = ntfs_collate_names(name, name_len, al_name,
1554 al_name_len, 1, IGNORE_CASE,
1555 vol->upcase, vol->upcase_len);
1557 * If @name collates before al_name, there is no
1558 * matching attribute.
1562 /* If the strings are not equal, continue search. */
1566 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
1567 * that is inconsistent with ntfs_attr_find(). The
1568 * subsequent rc checks were also different. Perhaps I
1569 * made a mistake in one of the two. Need to recheck
1570 * which is correct or at least see what is going on...
1573 rc = ntfs_collate_names(name, name_len, al_name,
1574 al_name_len, 1, CASE_SENSITIVE,
1575 vol->upcase, vol->upcase_len);
1582 * The names match or @name not present and attribute is
1583 * unnamed. Now check @lowest_vcn. Continue search if the
1584 * next attribute list entry still fits @lowest_vcn. Otherwise
1585 * we have reached the right one or the search has failed.
1587 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
1588 (u8*)next_al_entry + 6 < al_end &&
1589 (u8*)next_al_entry + le16_to_cpu(
1590 next_al_entry->length) <= al_end &&
1591 sle64_to_cpu(next_al_entry->lowest_vcn) <=
1593 next_al_entry->type == al_entry->type &&
1594 next_al_entry->name_length == al_name_len &&
1595 ntfs_are_names_equal((ntfschar*)((u8*)
1597 next_al_entry->name_offset),
1598 next_al_entry->name_length,
1599 al_name, al_name_len, CASE_SENSITIVE,
1600 vol->upcase, vol->upcase_len))
1602 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
1603 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
1604 ntfs_error(vol->sb, "Found stale mft "
1605 "reference in attribute list "
1606 "of base inode 0x%lx.%s",
1607 base_ni->mft_no, es);
1611 } else { /* Mft references do not match. */
1612 /* If there is a mapped record unmap it first. */
1614 unmap_extent_mft_record(ni);
1615 /* Do we want the base record back? */
1616 if (MREF_LE(al_entry->mft_reference) ==
1618 ni = ctx->ntfs_ino = base_ni;
1619 ctx->mrec = ctx->base_mrec;
1621 /* We want an extent record. */
1622 ctx->mrec = map_extent_mft_record(base_ni,
1624 al_entry->mft_reference), &ni);
1626 if (IS_ERR(ctx->mrec)) {
1627 ntfs_error(vol->sb, "Failed to map "
1628 "extent mft record "
1629 "0x%lx of base inode "
1633 base_ni->mft_no, es);
1634 err = PTR_ERR(ctx->mrec);
1640 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1641 le16_to_cpu(ctx->mrec->attrs_offset));
1644 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1645 * mft record containing the attribute represented by the
1649 * We could call into ntfs_attr_find() to find the right
1650 * attribute in this mft record but this would be less
1651 * efficient and not quite accurate as ntfs_attr_find() ignores
1652 * the attribute instance numbers for example which become
1653 * important when one plays with attribute lists. Also,
1654 * because a proper match has been found in the attribute list
1655 * entry above, the comparison can now be optimized. So it is
1656 * worth re-implementing a simplified ntfs_attr_find() here.
1660 * Use a manual loop so we can still use break and continue
1661 * with the same meanings as above.
1664 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1665 le32_to_cpu(ctx->mrec->bytes_allocated))
1667 if (a->type == AT_END)
1671 if (al_entry->instance != a->instance)
1674 * If the type and/or the name are mismatched between the
1675 * attribute list entry and the attribute record, there is
1676 * corruption so we break and return error EIO.
1678 if (al_entry->type != a->type)
1680 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1681 le16_to_cpu(a->name_offset)), a->name_length,
1682 al_name, al_name_len, CASE_SENSITIVE,
1683 vol->upcase, vol->upcase_len))
1687 * If no @val specified or @val specified and it matches, we
1690 if (!val || (!a->non_resident && le32_to_cpu(
1691 a->data.resident.value_length) == val_len &&
1693 le16_to_cpu(a->data.resident.value_offset),
1695 ntfs_debug("Done, found.");
1699 /* Proceed to the next attribute in the current mft record. */
1700 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1701 goto do_next_attr_loop;
1704 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1705 "attribute list attribute.%s", base_ni->mft_no,
1709 if (ni != base_ni) {
1710 unmap_extent_mft_record(ni);
1711 ctx->ntfs_ino = base_ni;
1712 ctx->mrec = ctx->base_mrec;
1713 ctx->attr = ctx->base_attr;
1720 * If we were looking for AT_END, we reset the search context @ctx and
1721 * use ntfs_attr_find() to seek to the end of the base mft record.
1723 if (type == AT_END) {
1724 ntfs_attr_reinit_search_ctx(ctx);
1725 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1729 * The attribute was not found. Before we return, we want to ensure
1730 * @ctx->mrec and @ctx->attr indicate the position at which the
1731 * attribute should be inserted in the base mft record. Since we also
1732 * want to preserve @ctx->al_entry we cannot reinitialize the search
1733 * context using ntfs_attr_reinit_search_ctx() as this would set
1734 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
1735 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
1736 * @ctx->al_entry as the remaining fields (base_*) are identical to
1737 * their non base_ counterparts and we cannot set @ctx->base_attr
1738 * correctly yet as we do not know what @ctx->attr will be set to by
1739 * the call to ntfs_attr_find() below.
1742 unmap_extent_mft_record(ni);
1743 ctx->mrec = ctx->base_mrec;
1744 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1745 le16_to_cpu(ctx->mrec->attrs_offset));
1746 ctx->is_first = TRUE;
1747 ctx->ntfs_ino = base_ni;
1748 ctx->base_ntfs_ino = NULL;
1749 ctx->base_mrec = NULL;
1750 ctx->base_attr = NULL;
1752 * In case there are multiple matches in the base mft record, need to
1753 * keep enumerating until we get an attribute not found response (or
1754 * another error), otherwise we would keep returning the same attribute
1755 * over and over again and all programs using us for enumeration would
1756 * lock up in a tight loop.
1759 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1762 ntfs_debug("Done, not found.");
1767 * ntfs_attr_lookup - find an attribute in an ntfs inode
1768 * @type: attribute type to find
1769 * @name: attribute name to find (optional, i.e. NULL means don't care)
1770 * @name_len: attribute name length (only needed if @name present)
1771 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1772 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1773 * @val: attribute value to find (optional, resident attributes only)
1774 * @val_len: attribute value length
1775 * @ctx: search context with mft record and attribute to search from
1777 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1778 * be the base mft record and @ctx must have been obtained from a call to
1779 * ntfs_attr_get_search_ctx().
1781 * This function transparently handles attribute lists and @ctx is used to
1782 * continue searches where they were left off at.
1784 * After finishing with the attribute/mft record you need to call
1785 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1786 * mapped inodes, etc).
1788 * Return 0 if the search was successful and -errno if not.
1790 * When 0, @ctx->attr is the found attribute and it is in mft record
1791 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
1792 * the attribute list entry of the found attribute.
1794 * When -ENOENT, @ctx->attr is the attribute which collates just after the
1795 * attribute being searched for, i.e. if one wants to add the attribute to the
1796 * mft record this is the correct place to insert it into. If an attribute
1797 * list attribute is present, @ctx->al_entry is the attribute list entry which
1798 * collates just after the attribute list entry of the attribute being searched
1799 * for, i.e. if one wants to add the attribute to the mft record this is the
1800 * correct place to insert its attribute list entry into.
1802 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
1803 * then undefined and in particular you should not rely on it not changing.
1805 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1806 const u32 name_len, const IGNORE_CASE_BOOL ic,
1807 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1808 ntfs_attr_search_ctx *ctx)
1810 ntfs_inode *base_ni;
1812 ntfs_debug("Entering.");
1813 if (ctx->base_ntfs_ino)
1814 base_ni = ctx->base_ntfs_ino;
1816 base_ni = ctx->ntfs_ino;
1817 /* Sanity check, just for debugging really. */
1819 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1820 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1822 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1827 * ntfs_attr_init_search_ctx - initialize an attribute search context
1828 * @ctx: attribute search context to initialize
1829 * @ni: ntfs inode with which to initialize the search context
1830 * @mrec: mft record with which to initialize the search context
1832 * Initialize the attribute search context @ctx with @ni and @mrec.
1834 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1835 ntfs_inode *ni, MFT_RECORD *mrec)
1838 /* Sanity checks are performed elsewhere. */
1839 ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset));
1840 ctx->is_first = TRUE;
1842 ctx->al_entry = NULL;
1843 ctx->base_ntfs_ino = NULL;
1844 ctx->base_mrec = NULL;
1845 ctx->base_attr = NULL;
1849 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1850 * @ctx: attribute search context to reinitialize
1852 * Reinitialize the attribute search context @ctx, unmapping an associated
1853 * extent mft record if present, and initialize the search context again.
1855 * This is used when a search for a new attribute is being started to reset
1856 * the search context to the beginning.
1858 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1860 if (likely(!ctx->base_ntfs_ino)) {
1861 /* No attribute list. */
1862 ctx->is_first = TRUE;
1863 /* Sanity checks are performed elsewhere. */
1864 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1865 le16_to_cpu(ctx->mrec->attrs_offset));
1867 * This needs resetting due to ntfs_external_attr_find() which
1868 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1870 ctx->al_entry = NULL;
1872 } /* Attribute list. */
1873 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1874 unmap_extent_mft_record(ctx->ntfs_ino);
1875 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1880 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1881 * @ni: ntfs inode with which to initialize the search context
1882 * @mrec: mft record with which to initialize the search context
1884 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1885 * and return it. Return NULL if allocation failed.
1887 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1889 ntfs_attr_search_ctx *ctx;
1891 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1893 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1898 * ntfs_attr_put_search_ctx - release an attribute search context
1899 * @ctx: attribute search context to free
1901 * Release the attribute search context @ctx, unmapping an associated extent
1902 * mft record if present.
1904 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1906 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1907 unmap_extent_mft_record(ctx->ntfs_ino);
1908 kmem_cache_free(ntfs_attr_ctx_cache, ctx);