vserver 1.9.3
[linux-2.6.git] / fs / ntfs / attrib.c
1 /**
2  * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2004 Anton Altaparmakov
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 <linux/buffer_head.h>
24 #include "ntfs.h"
25 #include "dir.h"
26
27 /* Temporary helper functions -- might become macros */
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)))
38                 memmove(base + dst, base + src, size * sizeof (*base));
39 }
40
41 /**
42  * ntfs_rl_mc - runlist memory copy
43  *
44  * It is up to the caller to serialize access to the runlists @dstbase and
45  * @srcbase.
46  */
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
62  * memory, this function returns and entire page of memory.
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
97 /**
98  * ntfs_are_rl_mergeable - test if two runlists can be joined together
99  * @dst:        original runlist
100  * @src:        new runlist to test for mergeability with @dst
101  *
102  * Test if two runlists can be joined together. For this, their VCNs and LCNs
103  * must be adjacent.
104  *
105  * It is up to the caller to serialize access to the runlists @dst and @src.
106  *
107  * Return: TRUE   Success, the runlists can be merged.
108  *         FALSE  Failure, the runlists cannot be merged.
109  */
110 static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
111                 runlist_element *src)
112 {
113         BUG_ON(!dst);
114         BUG_ON(!src);
115
116         if ((dst->lcn < 0) || (src->lcn < 0))     /* Are we merging holes? */
117                 return FALSE;
118         if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
119                 return FALSE;
120         if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
121                 return FALSE;
122
123         return TRUE;
124 }
125
126 /**
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
130  *
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.
134  *
135  * It is up to the caller to serialize access to the runlists @dst and @src.
136  */
137 static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
138 {
139         dst->length += src->length;
140 }
141
142 /**
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
146  *
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.
150  *
151  * It is up to the caller to serialize access to the runlists @dst and @src.
152  *
153  * Return: TRUE   Success, the runlists have been merged.
154  *         FALSE  Failure, the runlists cannot be merged and have not been
155  *                modified.
156  */
157 static inline BOOL ntfs_rl_merge(runlist_element *dst, runlist_element *src)
158 {
159         BOOL merge = ntfs_are_rl_mergeable(dst, src);
160
161         if (merge)
162                 __ntfs_rl_merge(dst, src);
163         return merge;
164 }
165
166 /**
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
173  *
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
176  * appended runlist.
177  *
178  * It is up to the caller to serialize access to the runlists @dst and @src.
179  *
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.)
184  *
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.
189  */
190 static inline runlist_element *ntfs_rl_append(runlist_element *dst,
191                 int dsize, runlist_element *src, int ssize, int loc)
192 {
193         BOOL right;
194         int magic;
195
196         BUG_ON(!dst);
197         BUG_ON(!src);
198
199         /* First, check if the right hand end needs merging. */
200         right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
201
202         /* Space required: @dst size + @src size, less one if we merged. */
203         dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
204         if (IS_ERR(dst))
205                 return dst;
206         /*
207          * We are guaranteed to succeed from here so can start modifying the
208          * original runlists.
209          */
210
211         /* First, merge the right hand end, if necessary. */
212         if (right)
213                 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
214
215         magic = loc + ssize;
216
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);
220
221         /* Adjust the size of the preceding hole. */
222         dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
223
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;
227
228         return dst;
229 }
230
231 /**
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
238  *
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.
242  *
243  * It is up to the caller to serialize access to the runlists @dst and @src.
244  *
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.)
249  *
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.
254  */
255 static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
256                 int dsize, runlist_element *src, int ssize, int loc)
257 {
258         BOOL left = FALSE;
259         BOOL disc = FALSE;      /* Discontinuity */
260         BOOL hole = FALSE;      /* Following a hole */
261         int magic;
262
263         BUG_ON(!dst);
264         BUG_ON(!src);
265
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. */
270         if (loc == 0)
271                 disc = (src[0].vcn > 0);
272         else {
273                 s64 merged_length;
274
275                 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
276
277                 merged_length = dst[loc - 1].length;
278                 if (left)
279                         merged_length += src->length;
280
281                 disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
282                 if (disc)
283                         hole = (dst[loc - 1].lcn == LCN_HOLE);
284         }
285
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);
289         if (IS_ERR(dst))
290                 return dst;
291         /*
292          * We are guaranteed to succeed from here so can start modifying the
293          * original runlist.
294          */
295
296         if (left)
297                 __ntfs_rl_merge(dst + loc - 1, src);
298
299         magic = loc + ssize - left + disc - hole;
300
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);
304
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;
311
312         /* Writing beyond the end of the file and there's a discontinuity. */
313         if (disc) {
314                 if (hole)
315                         dst[loc - 1].length = dst[loc].vcn - dst[loc - 1].vcn;
316                 else {
317                         if (loc > 0) {
318                                 dst[loc].vcn = dst[loc - 1].vcn +
319                                                 dst[loc - 1].length;
320                                 dst[loc].length = dst[loc + 1].vcn -
321                                                 dst[loc].vcn;
322                         } else {
323                                 dst[loc].vcn = 0;
324                                 dst[loc].length = dst[loc + 1].vcn;
325                         }
326                         dst[loc].lcn = LCN_RL_NOT_MAPPED;
327                 }
328
329                 magic += hole;
330
331                 if (dst[magic].lcn == LCN_ENOENT)
332                         dst[magic].vcn = dst[magic - 1].vcn +
333                                         dst[magic - 1].length;
334         }
335         return dst;
336 }
337
338 /**
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
345  *
346  * Replace the runlist element @dst at @loc with @src. Merge the left and
347  * right ends of the inserted runlist, if necessary.
348  *
349  * It is up to the caller to serialize access to the runlists @dst and @src.
350  *
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.)
355  *
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.
360  */
361 static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
362                 int dsize, runlist_element *src, int ssize, int loc)
363 {
364         BOOL left = FALSE;
365         BOOL right;
366         int magic;
367
368         BUG_ON(!dst);
369         BUG_ON(!src);
370
371         /* First, merge the left and right ends, if necessary. */
372         right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
373         if (loc > 0)
374                 left = ntfs_are_rl_mergeable(dst + loc - 1, src);
375
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);
379         if (IS_ERR(dst))
380                 return dst;
381         /*
382          * We are guaranteed to succeed from here so can start modifying the
383          * original runlists.
384          */
385         if (right)
386                 __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
387         if (left)
388                 __ntfs_rl_merge(dst + loc - 1, src);
389
390         /* FIXME: What does this mean? (AIA) */
391         magic = loc + ssize - left;
392
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);
396
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;
400         return dst;
401 }
402
403 /**
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
410  *
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
413  * holes either side.
414  *
415  * It is up to the caller to serialize access to the runlists @dst and @src.
416  *
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.)
421  *
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.
426  */
427 static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
428                 runlist_element *src, int ssize, int loc)
429 {
430         BUG_ON(!dst);
431         BUG_ON(!src);
432
433         /* Space required: @dst size + @src size + one new hole. */
434         dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
435         if (IS_ERR(dst))
436                 return dst;
437         /*
438          * We are guaranteed to succeed from here so can start modifying the
439          * original runlists.
440          */
441
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);
445
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;
450
451         return dst;
452 }
453
454 /**
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
458  *
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.
462  *
463  * It is up to the caller to serialize access to the runlists @drl and @srl.
464  *
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.
468  *
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.
476  *
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.)
481  *
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.
487  */
488 runlist_element *ntfs_merge_runlists(runlist_element *drl,
489                 runlist_element *srl)
490 {
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
496                                    lcn >= LCN_HOLE. */
497         int marker = 0;
498         VCN marker_vcn = 0;
499
500 #ifdef DEBUG
501         ntfs_debug("dst:");
502         ntfs_debug_dump_runlist(drl);
503         ntfs_debug("src:");
504         ntfs_debug_dump_runlist(srl);
505 #endif
506
507         /* Check for silly calling... */
508         if (unlikely(!srl))
509                 return drl;
510         if (IS_ERR(srl) || IS_ERR(drl))
511                 return ERR_PTR(-EINVAL);
512
513         /* Check for the case where the first mapping is being done now. */
514         if (unlikely(!drl)) {
515                 drl = srl;
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++)
520                                 ;
521                         drl = ntfs_rl_realloc(drl, dend, dend + 1);
522                         if (IS_ERR(drl))
523                                 return drl;
524                         /* Insert start element at the front of the runlist. */
525                         ntfs_rl_mm(drl, 1, 0, dend);
526                         drl[0].vcn = 0;
527                         drl[0].lcn = LCN_RL_NOT_MAPPED;
528                         drl[0].length = drl[1].vcn;
529                 }
530                 goto finished;
531         }
532
533         si = di = 0;
534
535         /* Skip any unmapped start element(s) in the source runlist. */
536         while (srl[si].length && srl[si].lcn < (LCN)LCN_HOLE)
537                 si++;
538
539         /* Can't have an entirely unmapped source runlist. */
540         BUG_ON(!srl[si].length);
541
542         /* Record the starting points. */
543         sstart = si;
544
545         /*
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
548          * appended to @drl.
549          */
550         for (; drl[di].length; di++) {
551                 if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
552                         break;
553         }
554         dins = di;
555
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);
561         }
562
563         /* Scan to the end of both runlists in order to know their sizes. */
564         for (send = si; srl[send].length; send++)
565                 ;
566         for (dend = di; drl[dend].length; dend++)
567                 ;
568
569         if (srl[send].lcn == (LCN)LCN_ENOENT)
570                 marker_vcn = srl[marker = send].vcn;
571
572         /* Scan to the last element with lcn >= LCN_HOLE. */
573         for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
574                 ;
575         for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
576                 ;
577
578         {
579         BOOL start;
580         BOOL finish;
581         int ds = dend + 1;              /* Number of elements in drl & srl */
582         int ss = sfinal - sstart + 1;
583
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)));
589
590         /* Or we'll lose an end marker */
591         if (start && finish && (drl[dins].length == 0))
592                 ss++;
593         if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
594                 finish = FALSE;
595 #if 0
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);
600 #endif
601         if (start) {
602                 if (finish)
603                         drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
604                 else
605                         drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
606         } else {
607                 if (finish)
608                         drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
609                 else
610                         drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
611         }
612         if (IS_ERR(drl)) {
613                 ntfs_error(NULL, "Merge failed.");
614                 return drl;
615         }
616         ntfs_free(srl);
617         if (marker) {
618                 ntfs_debug("Triggering marker code.");
619                 for (ds = dend; drl[ds].length; ds++)
620                         ;
621                 /* We only need to care if @srl ended after @drl. */
622                 if (drl[ds].vcn <= marker_vcn) {
623                         int slots = 0;
624
625                         if (drl[ds].vcn == marker_vcn) {
626                                 ntfs_debug("Old marker = 0x%llx, replacing "
627                                                 "with LCN_ENOENT.",
628                                                 (unsigned long long)
629                                                 drl[ds].lcn);
630                                 drl[ds].lcn = (LCN)LCN_ENOENT;
631                                 goto finished;
632                         }
633                         /*
634                          * We need to create an unmapped runlist element in
635                          * @drl or extend an existing one before adding the
636                          * ENOENT terminator.
637                          */
638                         if (drl[ds].lcn == (LCN)LCN_ENOENT) {
639                                 ds--;
640                                 slots = 1;
641                         }
642                         if (drl[ds].lcn != (LCN)LCN_RL_NOT_MAPPED) {
643                                 /* Add an unmapped runlist element. */
644                                 if (!slots) {
645                                         /* FIXME/TODO: We need to have the
646                                          * extra memory already! (AIA) */
647                                         drl = ntfs_rl_realloc(drl, ds, ds + 2);
648                                         if (!drl)
649                                                 goto critical_error;
650                                         slots = 2;
651                                 }
652                                 ds++;
653                                 /* Need to set vcn if it isn't set already. */
654                                 if (slots != 1)
655                                         drl[ds].vcn = drl[ds - 1].vcn +
656                                                         drl[ds - 1].length;
657                                 drl[ds].lcn = (LCN)LCN_RL_NOT_MAPPED;
658                                 /* We now used up a slot. */
659                                 slots--;
660                         }
661                         drl[ds].length = marker_vcn - drl[ds].vcn;
662                         /* Finally add the ENOENT terminator. */
663                         ds++;
664                         if (!slots) {
665                                 /* FIXME/TODO: We need to have the extra
666                                  * memory already! (AIA) */
667                                 drl = ntfs_rl_realloc(drl, ds, ds + 1);
668                                 if (!drl)
669                                         goto critical_error;
670                         }
671                         drl[ds].vcn = marker_vcn;
672                         drl[ds].lcn = (LCN)LCN_ENOENT;
673                         drl[ds].length = (s64)0;
674                 }
675         }
676         }
677
678 finished:
679         /* The merge was completed successfully. */
680         ntfs_debug("Merged runlist:");
681         ntfs_debug_dump_runlist(drl);
682         return drl;
683
684 critical_error:
685         /* Critical error! We cannot afford to fail here. */
686         ntfs_error(NULL, "Critical error! Not enough memory.");
687         panic("NTFS: Cannot continue.");
688 }
689
690 /**
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
695  *
696  * It is up to the caller to serialize access to the runlist @old_rl.
697  *
698  * Decompress the attribute @attr's mapping pairs array into a runlist. On
699  * success, return the decompressed runlist.
700  *
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.
704  *
705  * On error, return -errno. @old_rl is left unmodified in that case.
706  *
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.
712  *
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)).
717  */
718 runlist_element *decompress_mapping_pairs(const ntfs_volume *vol,
719                 const ATTR_RECORD *attr, runlist_element *old_rl)
720 {
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
729                                    runlist_elements. */
730         u8 b;                   /* Current byte offset in buf. */
731
732 #ifdef DEBUG
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);
738         }
739 #endif
740         /* Start at vcn = lowest_vcn and lcn 0. */
741         vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
742         lcn = 0;
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);
750         }
751         /* Current position in runlist array. */
752         rlpos = 0;
753         /* Allocate first page and set current runlist size to one page. */
754         rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
755         if (unlikely(!rl))
756                 return ERR_PTR(-ENOMEM);
757         /* Insert unmapped starting element if necessary. */
758         if (vcn) {
759                 rl->vcn = (VCN)0;
760                 rl->lcn = (LCN)LCN_RL_NOT_MAPPED;
761                 rl->length = vcn;
762                 rlpos++;
763         }
764         while (buf < attr_end && *buf) {
765                 /*
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.
769                  */
770                 if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
771                         runlist_element *rl2;
772
773                         rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
774                         if (unlikely(!rl2)) {
775                                 ntfs_free(rl);
776                                 return ERR_PTR(-ENOMEM);
777                         }
778                         memcpy(rl2, rl, rlsize);
779                         ntfs_free(rl);
780                         rl = rl2;
781                         rlsize += PAGE_SIZE;
782                 }
783                 /* Enter the current vcn into the current runlist element. */
784                 rl[rlpos].vcn = vcn;
785                 /*
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...
791                  */
792                 b = *buf & 0xf;
793                 if (b) {
794                         if (unlikely(buf + b > attr_end))
795                                 goto io_error;
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 "
800                                         "pairs array.");
801                         deltaxcn = (s64)-1;
802                 }
803                 /*
804                  * Assume a negative length to indicate data corruption and
805                  * hence clean-up and return NULL.
806                  */
807                 if (unlikely(deltaxcn < 0)) {
808                         ntfs_error(vol->sb, "Invalid length in mapping pairs "
809                                         "array.");
810                         goto err_out;
811                 }
812                 /*
813                  * Enter the current run length into the current runlist
814                  * element.
815                  */
816                 rl[rlpos].length = deltaxcn;
817                 /* Increment the current vcn by the current run length. */
818                 vcn += deltaxcn;
819                 /*
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
822                  * to LCN_HOLE.
823                  */
824                 if (!(*buf & 0xf0))
825                         rl[rlpos].lcn = (LCN)LCN_HOLE;
826                 else {
827                         /* Get the lcn change which really can be negative. */
828                         u8 b2 = *buf & 0xf;
829                         b = b2 + ((*buf >> 4) & 0xf);
830                         if (buf + b > attr_end)
831                                 goto io_error;
832                         for (deltaxcn = (s8)buf[b--]; b > b2; b--)
833                                 deltaxcn = (deltaxcn << 8) + buf[b];
834                         /* Change the current lcn to its new value. */
835                         lcn += deltaxcn;
836 #ifdef DEBUG
837                         /*
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!
843                          */
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");
849                         }
850 #endif
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.");
855                                 goto err_out;
856                         }
857                         /* Enter the current lcn into the runlist element. */
858                         rl[rlpos].lcn = lcn;
859                 }
860                 /* Get to the next runlist element. */
861                 rlpos++;
862                 /* Increment the buffer position to the next mapping pair. */
863                 buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
864         }
865         if (unlikely(buf >= attr_end))
866                 goto io_error;
867         /*
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.
870          */
871         deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
872         if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
873 mpa_err:
874                 ntfs_error(vol->sb, "Corrupt mapping pairs array in "
875                                 "non-resident attribute.");
876                 goto err_out;
877         }
878         /* Setup not mapped runlist element if this is the base extent. */
879         if (!attr->data.non_resident.lowest_vcn) {
880                 VCN max_cluster;
881
882                 max_cluster = (sle64_to_cpu(
883                                 attr->data.non_resident.allocated_size) +
884                                 vol->cluster_size - 1) >>
885                                 vol->cluster_size_bits;
886                 /*
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.
890                  */
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);
896                         rl[rlpos].vcn = vcn;
897                         vcn += rl[rlpos].length = max_cluster - deltaxcn;
898                         rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED;
899                         rlpos++;
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);
905                         goto mpa_err;
906                 }
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;
910
911         /* Setup terminating runlist element. */
912         rl[rlpos].vcn = vcn;
913         rl[rlpos].length = (s64)0;
914         /* If no existing runlist was specified, we are done. */
915         if (!old_rl) {
916                 ntfs_debug("Mapping pairs array successfully decompressed:");
917                 ntfs_debug_dump_runlist(rl);
918                 return rl;
919         }
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)))
923                 return old_rl;
924         ntfs_free(rl);
925         ntfs_error(vol->sb, "Failed to merge runlists.");
926         return old_rl;
927 io_error:
928         ntfs_error(vol->sb, "Corrupt attribute.");
929 err_out:
930         ntfs_free(rl);
931         return ERR_PTR(-EIO);
932 }
933
934 /**
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
938  *
939  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
940  *
941  * Return 0 on success and -errno on error.
942  *
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.
945  */
946 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
947 {
948         ntfs_inode *base_ni;
949         ntfs_attr_search_ctx *ctx;
950         MFT_RECORD *mrec;
951         int err = 0;
952
953         ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
954                         (unsigned long long)vcn);
955
956         if (!NInoAttr(ni))
957                 base_ni = ni;
958         else
959                 base_ni = ni->ext.base_ntfs_ino;
960
961         mrec = map_mft_record(base_ni);
962         if (IS_ERR(mrec))
963                 return PTR_ERR(mrec);
964         ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
965         if (unlikely(!ctx)) {
966                 err = -ENOMEM;
967                 goto err_out;
968         }
969         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
970                         CASE_SENSITIVE, vcn, NULL, 0, ctx);
971         if (unlikely(err))
972                 goto put_err_out;
973
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)) {
977                 runlist_element *rl;
978
979                 rl = decompress_mapping_pairs(ni->vol, ctx->attr,
980                                 ni->runlist.rl);
981                 if (IS_ERR(rl))
982                         err = PTR_ERR(rl);
983                 else
984                         ni->runlist.rl = rl;
985         }
986         up_write(&ni->runlist.lock);
987
988 put_err_out:
989         ntfs_attr_put_search_ctx(ctx);
990 err_out:
991         unmap_mft_record(base_ni);
992         return err;
993 }
994
995 /**
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
999  *
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.
1003  *
1004  * It is up to the caller to serialize access to the runlist @rl.
1005  *
1006  * Since lcns must be >= 0, we use negative return values with special meaning:
1007  *
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.
1014  *
1015  * Locking: - The caller must have locked the runlist (for reading or writing).
1016  *          - This function does not touch the lock.
1017  */
1018 LCN ntfs_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
1019 {
1020         int i;
1021
1022         BUG_ON(vcn < 0);
1023         /*
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
1026          * necessary.
1027          */
1028         if (unlikely(!rl))
1029                 return (LCN)LCN_RL_NOT_MAPPED;
1030
1031         /* Catch out of lower bounds vcn. */
1032         if (unlikely(vcn < rl[0].vcn))
1033                 return (LCN)LCN_ENOENT;
1034
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);
1039                         return rl[i].lcn;
1040                 }
1041         }
1042         /*
1043          * The terminator element is setup to the correct value, i.e. one of
1044          * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
1045          */
1046         if (likely(rl[i].lcn < (LCN)0))
1047                 return rl[i].lcn;
1048         /* Just in case... We could replace this with BUG() some day. */
1049         return (LCN)LCN_ENOENT;
1050 }
1051
1052 /**
1053  * ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode
1054  * @ni:         ntfs inode describing the runlist to search
1055  * @vcn:        vcn to find
1056  * @need_write: if false, lock for reading and if true, lock for writing
1057  *
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
1063  * locked.
1064  *
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.
1068  *
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.
1073  *
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).
1078  *
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
1083  *            reading.
1084  */
1085 runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
1086                 const BOOL need_write)
1087 {
1088         runlist_element *rl;
1089         int err = 0;
1090         BOOL is_retry = FALSE;
1091
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");
1095         BUG_ON(!ni);
1096         BUG_ON(!NInoNonResident(ni));
1097         BUG_ON(vcn < 0);
1098 lock_retry_remap:
1099         if (!need_write)
1100                 down_read(&ni->runlist.lock);
1101         else
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.");
1109                                         return rl;
1110                                 }
1111                                 break;
1112                         }
1113                         rl++;
1114                 }
1115                 if (likely(rl->lcn != (LCN)LCN_RL_NOT_MAPPED)) {
1116                         if (likely(rl->lcn == (LCN)LCN_ENOENT))
1117                                 err = -ENOENT;
1118                         else
1119                                 err = -EIO;
1120                 }
1121         }
1122         if (!need_write)
1123                 up_read(&ni->runlist.lock);
1124         else
1125                 up_write(&ni->runlist.lock);
1126         if (!err && !is_retry) {
1127                 /*
1128                  * The @vcn is in an unmapped region, map the runlist and
1129                  * retry.
1130                  */
1131                 err = ntfs_map_runlist(ni, vcn);
1132                 if (likely(!err)) {
1133                         is_retry = TRUE;
1134                         goto lock_retry_remap;
1135                 }
1136                 /*
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
1139                  * our code paths.
1140                  */
1141                 if (err == -EINVAL || err == -ENOENT)
1142                         err = -EIO;
1143         } else if (!err)
1144                 err = -EIO;
1145         ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
1146         return ERR_PTR(err);
1147 }
1148
1149 /**
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
1158  *
1159  * You should not need to call this function directly.  Use ntfs_attr_lookup()
1160  * instead.
1161  *
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.
1165  *
1166  * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
1167  * point to the found attribute.
1168  *
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.
1172  *
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.
1175  *
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.
1178  *
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
1184  * characters.
1185  *
1186  * If @name is not present (NULL), we assume that the unnamed attribute is
1187  * being searched for.
1188  *
1189  * Finally, the resident attribute value @val is looked for, if present.  If
1190  * @val is not present (NULL), @val_len is ignored.
1191  *
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
1201  * base mft record.
1202  *
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!
1205  */
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)
1209 {
1210         ATTR_RECORD *a;
1211         ntfs_volume *vol;
1212         ntfschar *upcase;
1213         u32 upcase_len;
1214
1215         if (ic == IGNORE_CASE) {
1216                 vol = ctx->ntfs_ino->vol;
1217                 upcase = vol->upcase;
1218                 upcase_len = vol->upcase_len;
1219         } else {
1220                 vol = NULL;
1221                 upcase = NULL;
1222                 upcase_len = 0;
1223         }
1224         /*
1225          * Iterate over attributes in mft record starting at @ctx->attr, or the
1226          * attribute following that, if @ctx->is_first is TRUE.
1227          */
1228         if (ctx->is_first) {
1229                 a = ctx->attr;
1230                 ctx->is_first = FALSE;
1231         } else
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))
1237                         break;
1238                 ctx->attr = a;
1239                 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
1240                                 a->type == AT_END))
1241                         return -ENOENT;
1242                 if (unlikely(!a->length))
1243                         break;
1244                 if (a->type != type)
1245                         continue;
1246                 /*
1247                  * If @name is present, compare the two names.  If @name is
1248                  * missing, assume we want an unnamed attribute.
1249                  */
1250                 if (!name) {
1251                         /* The search failed if the found attribute is named. */
1252                         if (a->name_length)
1253                                 return -ENOENT;
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)) {
1257                         register int rc;
1258
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);
1264                         /*
1265                          * If @name collates before a->name, there is no
1266                          * matching attribute.
1267                          */
1268                         if (rc == -1)
1269                                 return -ENOENT;
1270                         /* If the strings are not equal, continue search. */
1271                         if (rc)
1272                                 continue;
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);
1278                         if (rc == -1)
1279                                 return -ENOENT;
1280                         if (rc)
1281                                 continue;
1282                 }
1283                 /*
1284                  * The names match or @name not present and attribute is
1285                  * unnamed.  If no @val specified, we have found the attribute
1286                  * and are done.
1287                  */
1288                 if (!val)
1289                         return 0;
1290                 /* @val is present; compare values. */
1291                 else {
1292                         register int rc;
1293
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)));
1298                         /*
1299                          * If @val collates before the current attribute's
1300                          * value, there is no matching attribute.
1301                          */
1302                         if (!rc) {
1303                                 register u32 avl;
1304
1305                                 avl = le32_to_cpu(
1306                                                 a->data.resident.value_length);
1307                                 if (val_len == avl)
1308                                         return 0;
1309                                 if (val_len < avl)
1310                                         return -ENOENT;
1311                         } else if (rc < 0)
1312                                 return -ENOENT;
1313                 }
1314         }
1315         ntfs_error(NULL, "Inode is corrupt.  Run chkdsk.");
1316         NVolSetErrors(vol);
1317         return -EIO;
1318 }
1319
1320 /**
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
1327  *
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.
1333  *
1334  * Return 0 on success or -errno on error.
1335  */
1336 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
1337                 const s64 size, const s64 initialized_size)
1338 {
1339         LCN lcn;
1340         u8 *al = al_start;
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;
1347         int err = 0;
1348         unsigned char block_size_bits;
1349
1350         ntfs_debug("Entering.");
1351         if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
1352                         initialized_size > size)
1353                 return -EINVAL;
1354         if (!initialized_size) {
1355                 memset(al, 0, size);
1356                 return 0;
1357         }
1358         sb = vol->sb;
1359         block_size = sb->s_blocksize;
1360         block_size_bits = sb->s_blocksize_bits;
1361         down_read(&runlist->lock);
1362         rl = runlist->rl;
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. */
1370                 if (lcn < 0) {
1371                         ntfs_error(sb, "ntfs_vcn_to_lcn() failed. Cannot read "
1372                                         "attribute list.");
1373                         goto err_out;
1374                 }
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 >>
1378                                 block_size_bits);
1379                 ntfs_debug("max_block = 0x%lx.", max_block);
1380                 do {
1381                         ntfs_debug("Reading block = 0x%lx.", block);
1382                         bh = sb_bread(sb, block);
1383                         if (!bh) {
1384                                 ntfs_error(sb, "sb_bread() failed. Cannot "
1385                                                 "read attribute list.");
1386                                 goto err_out;
1387                         }
1388                         if (al + block_size >= al_end)
1389                                 goto do_final;
1390                         memcpy(al, bh->b_data, block_size);
1391                         brelse(bh);
1392                         al += block_size;
1393                 } while (++block < max_block);
1394                 rl++;
1395         }
1396         if (initialized_size < size) {
1397 initialize:
1398                 memset(al_start + initialized_size, 0, size - initialized_size);
1399         }
1400 done:
1401         up_read(&runlist->lock);
1402         return err;
1403 do_final:
1404         if (al < al_end) {
1405                 /*
1406                  * Partial block.
1407                  *
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)
1412                  */
1413                 memcpy(al, bh->b_data, al_end - al);
1414                 brelse(bh);
1415                 if (initialized_size < size)
1416                         goto initialize;
1417                 goto done;
1418         }
1419         brelse(bh);
1420         /* Real overflow! */
1421         ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
1422                         "is truncated.");
1423 err_out:
1424         err = -EIO;
1425         goto done;
1426 }
1427
1428 /**
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
1438  *
1439  * You should not need to call this function directly.  Use ntfs_attr_lookup()
1440  * instead.
1441  *
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.
1446  *
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).
1451  *
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).
1455  *
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.
1460  *
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.
1468  *
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.
1474  *
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.
1477  */
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)
1482 {
1483         ntfs_inode *base_ni, *ni;
1484         ntfs_volume *vol;
1485         ATTR_LIST_ENTRY *al_entry, *next_al_entry;
1486         u8 *al_start, *al_end;
1487         ATTR_RECORD *a;
1488         ntfschar *al_name;
1489         u32 al_name_len;
1490         int err = 0;
1491         static const char *es = " Unmount and run chkdsk.";
1492
1493         ni = ctx->ntfs_ino;
1494         base_ni = ctx->base_ntfs_ino;
1495         ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
1496         if (!base_ni) {
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;
1500         }
1501         if (ni == base_ni)
1502                 ctx->base_attr = ctx->attr;
1503         if (type == AT_END)
1504                 goto not_found;
1505         vol = base_ni->vol;
1506         al_start = base_ni->attr_list;
1507         al_end = al_start + base_ni->attr_list_size;
1508         if (!ctx->al_entry)
1509                 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
1510         /*
1511          * Iterate over entries in attribute list starting at @ctx->al_entry,
1512          * or the entry following that, if @ctx->is_first is TRUE.
1513          */
1514         if (ctx->is_first) {
1515                 al_entry = ctx->al_entry;
1516                 ctx->is_first = FALSE;
1517         } else
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)
1528                         goto not_found;
1529                 if (!al_entry->length)
1530                         break;
1531                 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
1532                                 le16_to_cpu(al_entry->length) > al_end)
1533                         break;
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))
1537                         goto not_found;
1538                 if (type != al_entry->type)
1539                         continue;
1540                 /*
1541                  * If @name is present, compare the two names.  If @name is
1542                  * missing, assume we want an unnamed attribute.
1543                  */
1544                 al_name_len = al_entry->name_length;
1545                 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
1546                 if (!name) {
1547                         if (al_name_len)
1548                                 goto not_found;
1549                 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
1550                                 name_len, ic, vol->upcase, vol->upcase_len)) {
1551                         register int rc;
1552
1553                         rc = ntfs_collate_names(name, name_len, al_name,
1554                                         al_name_len, 1, IGNORE_CASE,
1555                                         vol->upcase, vol->upcase_len);
1556                         /*
1557                          * If @name collates before al_name, there is no
1558                          * matching attribute.
1559                          */
1560                         if (rc == -1)
1561                                 goto not_found;
1562                         /* If the strings are not equal, continue search. */
1563                         if (rc)
1564                                 continue;
1565                         /*
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...
1571                          * (AIA)
1572                          */
1573                         rc = ntfs_collate_names(name, name_len, al_name,
1574                                         al_name_len, 1, CASE_SENSITIVE,
1575                                         vol->upcase, vol->upcase_len);
1576                         if (rc == -1)
1577                                 goto not_found;
1578                         if (rc)
1579                                 continue;
1580                 }
1581                 /*
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.
1586                  */
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) <=
1592                                         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*)
1596                                         next_al_entry +
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))
1601                         continue;
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);
1608                                 err = -EIO;
1609                                 break;
1610                         }
1611                 } else { /* Mft references do not match. */
1612                         /* If there is a mapped record unmap it first. */
1613                         if (ni != base_ni)
1614                                 unmap_extent_mft_record(ni);
1615                         /* Do we want the base record back? */
1616                         if (MREF_LE(al_entry->mft_reference) ==
1617                                         base_ni->mft_no) {
1618                                 ni = ctx->ntfs_ino = base_ni;
1619                                 ctx->mrec = ctx->base_mrec;
1620                         } else {
1621                                 /* We want an extent record. */
1622                                 ctx->mrec = map_extent_mft_record(base_ni,
1623                                                 le64_to_cpu(
1624                                                 al_entry->mft_reference), &ni);
1625                                 ctx->ntfs_ino = 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 "
1630                                                         "0x%lx.%s",
1631                                                         MREF_LE(al_entry->
1632                                                         mft_reference),
1633                                                         base_ni->mft_no, es);
1634                                         err = PTR_ERR(ctx->mrec);
1635                                         if (err == -ENOENT)
1636                                                 err = -EIO;
1637                                         break;
1638                                 }
1639                         }
1640                         ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1641                                         le16_to_cpu(ctx->mrec->attrs_offset));
1642                 }
1643                 /*
1644                  * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1645                  * mft record containing the attribute represented by the
1646                  * current al_entry.
1647                  */
1648                 /*
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.
1657                  */
1658                 a = ctx->attr;
1659                 /*
1660                  * Use a manual loop so we can still use break and continue
1661                  * with the same meanings as above.
1662                  */
1663 do_next_attr_loop:
1664                 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1665                                 le32_to_cpu(ctx->mrec->bytes_allocated))
1666                         break;
1667                 if (a->type == AT_END)
1668                         continue;
1669                 if (!a->length)
1670                         break;
1671                 if (al_entry->instance != a->instance)
1672                         goto do_next_attr;
1673                 /*
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.
1677                  */
1678                 if (al_entry->type != a->type)
1679                         break;
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))
1684                         break;
1685                 ctx->attr = a;
1686                 /*
1687                  * If no @val specified or @val specified and it matches, we
1688                  * have found it!
1689                  */
1690                 if (!val || (!a->non_resident && le32_to_cpu(
1691                                 a->data.resident.value_length) == val_len &&
1692                                 !memcmp((u8*)a +
1693                                 le16_to_cpu(a->data.resident.value_offset),
1694                                 val, val_len))) {
1695                         ntfs_debug("Done, found.");
1696                         return 0;
1697                 }
1698 do_next_attr:
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;
1702         }
1703         if (!err) {
1704                 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1705                                 "attribute list attribute.%s", base_ni->mft_no,
1706                                 es);
1707                 err = -EIO;
1708         }
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;
1714         }
1715         if (err != -ENOMEM)
1716                 NVolSetErrors(vol);
1717         return err;
1718 not_found:
1719         /*
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.
1722          */
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,
1726                                 ctx);
1727         }
1728         /*
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.
1740          */
1741         if (ni != base_ni)
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;
1751         /*
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.
1757          */
1758         do {
1759                 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1760                                 ctx);
1761         } while (!err);
1762         ntfs_debug("Done, not found.");
1763         return err;
1764 }
1765
1766 /**
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
1776  *
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().
1780  *
1781  * This function transparently handles attribute lists and @ctx is used to
1782  * continue searches where they were left off at.
1783  *
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).
1787  *
1788  * Return 0 if the search was successful and -errno if not.
1789  *
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.
1793  *
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.
1801  *
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.
1804  */
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)
1809 {
1810         ntfs_inode *base_ni;
1811
1812         ntfs_debug("Entering.");
1813         if (ctx->base_ntfs_ino)
1814                 base_ni = ctx->base_ntfs_ino;
1815         else
1816                 base_ni = ctx->ntfs_ino;
1817         /* Sanity check, just for debugging really. */
1818         BUG_ON(!base_ni);
1819         if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1820                 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1821                                 ctx);
1822         return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1823                         val, val_len, ctx);
1824 }
1825
1826 /**
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
1831  *
1832  * Initialize the attribute search context @ctx with @ni and @mrec.
1833  */
1834 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1835                 ntfs_inode *ni, MFT_RECORD *mrec)
1836 {
1837         ctx->mrec = 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;
1841         ctx->ntfs_ino = ni;
1842         ctx->al_entry = NULL;
1843         ctx->base_ntfs_ino = NULL;
1844         ctx->base_mrec = NULL;
1845         ctx->base_attr = NULL;
1846 }
1847
1848 /**
1849  * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1850  * @ctx:        attribute search context to reinitialize
1851  *
1852  * Reinitialize the attribute search context @ctx, unmapping an associated
1853  * extent mft record if present, and initialize the search context again.
1854  *
1855  * This is used when a search for a new attribute is being started to reset
1856  * the search context to the beginning.
1857  */
1858 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1859 {
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));
1866                 /*
1867                  * This needs resetting due to ntfs_external_attr_find() which
1868                  * can leave it set despite having zeroed ctx->base_ntfs_ino.
1869                  */
1870                 ctx->al_entry = NULL;
1871                 return;
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);
1876         return;
1877 }
1878
1879 /**
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
1883  *
1884  * Allocate a new attribute search context, initialize it with @ni and @mrec,
1885  * and return it. Return NULL if allocation failed.
1886  */
1887 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1888 {
1889         ntfs_attr_search_ctx *ctx;
1890
1891         ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1892         if (ctx)
1893                 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1894         return ctx;
1895 }
1896
1897 /**
1898  * ntfs_attr_put_search_ctx - release an attribute search context
1899  * @ctx:        attribute search context to free
1900  *
1901  * Release the attribute search context @ctx, unmapping an associated extent
1902  * mft record if present.
1903  */
1904 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1905 {
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);
1909         return;
1910 }