Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.git] / fs / ntfs / attrib.c
1 /**
2  * attrib.c - NTFS attribute operations.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2006 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 <linux/sched.h>
25 #include <linux/swap.h>
26 #include <linux/writeback.h>
27
28 #include "attrib.h"
29 #include "debug.h"
30 #include "layout.h"
31 #include "lcnalloc.h"
32 #include "malloc.h"
33 #include "mft.h"
34 #include "ntfs.h"
35 #include "types.h"
36
37 /**
38  * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
39  * @ni:         ntfs inode for which to map (part of) a runlist
40  * @vcn:        map runlist part containing this vcn
41  * @ctx:        active attribute search context if present or NULL if not
42  *
43  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
44  *
45  * If @ctx is specified, it is an active search context of @ni and its base mft
46  * record.  This is needed when ntfs_map_runlist_nolock() encounters unmapped
47  * runlist fragments and allows their mapping.  If you do not have the mft
48  * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
49  * will perform the necessary mapping and unmapping.
50  *
51  * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
52  * restores it before returning.  Thus, @ctx will be left pointing to the same
53  * attribute on return as on entry.  However, the actual pointers in @ctx may
54  * point to different memory locations on return, so you must remember to reset
55  * any cached pointers from the @ctx, i.e. after the call to
56  * ntfs_map_runlist_nolock(), you will probably want to do:
57  *      m = ctx->mrec;
58  *      a = ctx->attr;
59  * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
60  * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
61  *
62  * Return 0 on success and -errno on error.  There is one special error code
63  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
64  * of bounds of the runlist.
65  *
66  * Note the runlist can be NULL after this function returns if @vcn is zero and
67  * the attribute has zero allocated size, i.e. there simply is no runlist.
68  *
69  * WARNING: If @ctx is supplied, regardless of whether success or failure is
70  *          returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
71  *          is no longer valid, i.e. you need to either call
72  *          ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
73  *          In that case PTR_ERR(@ctx->mrec) will give you the error code for
74  *          why the mapping of the old inode failed.
75  *
76  * Locking: - The runlist described by @ni must be locked for writing on entry
77  *            and is locked on return.  Note the runlist will be modified.
78  *          - If @ctx is NULL, the base mft record of @ni must not be mapped on
79  *            entry and it will be left unmapped on return.
80  *          - If @ctx is not NULL, the base mft record must be mapped on entry
81  *            and it will be left mapped on return.
82  */
83 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
84 {
85         VCN end_vcn;
86         unsigned long flags;
87         ntfs_inode *base_ni;
88         MFT_RECORD *m;
89         ATTR_RECORD *a;
90         runlist_element *rl;
91         struct page *put_this_page = NULL;
92         int err = 0;
93         bool ctx_is_temporary, ctx_needs_reset;
94         ntfs_attr_search_ctx old_ctx = { NULL, };
95
96         ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
97                         (unsigned long long)vcn);
98         if (!NInoAttr(ni))
99                 base_ni = ni;
100         else
101                 base_ni = ni->ext.base_ntfs_ino;
102         if (!ctx) {
103                 ctx_is_temporary = ctx_needs_reset = true;
104                 m = map_mft_record(base_ni);
105                 if (IS_ERR(m))
106                         return PTR_ERR(m);
107                 ctx = ntfs_attr_get_search_ctx(base_ni, m);
108                 if (unlikely(!ctx)) {
109                         err = -ENOMEM;
110                         goto err_out;
111                 }
112         } else {
113                 VCN allocated_size_vcn;
114
115                 BUG_ON(IS_ERR(ctx->mrec));
116                 a = ctx->attr;
117                 BUG_ON(!a->non_resident);
118                 ctx_is_temporary = false;
119                 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
120                 read_lock_irqsave(&ni->size_lock, flags);
121                 allocated_size_vcn = ni->allocated_size >>
122                                 ni->vol->cluster_size_bits;
123                 read_unlock_irqrestore(&ni->size_lock, flags);
124                 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
125                         end_vcn = allocated_size_vcn - 1;
126                 /*
127                  * If we already have the attribute extent containing @vcn in
128                  * @ctx, no need to look it up again.  We slightly cheat in
129                  * that if vcn exceeds the allocated size, we will refuse to
130                  * map the runlist below, so there is definitely no need to get
131                  * the right attribute extent.
132                  */
133                 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
134                                 a->name_length == ni->name_len &&
135                                 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
136                                 ni->name, ni->name_len) &&
137                                 sle64_to_cpu(a->data.non_resident.lowest_vcn)
138                                 <= vcn && end_vcn >= vcn))
139                         ctx_needs_reset = false;
140                 else {
141                         /* Save the old search context. */
142                         old_ctx = *ctx;
143                         /*
144                          * If the currently mapped (extent) inode is not the
145                          * base inode we will unmap it when we reinitialize the
146                          * search context which means we need to get a
147                          * reference to the page containing the mapped mft
148                          * record so we do not accidentally drop changes to the
149                          * mft record when it has not been marked dirty yet.
150                          */
151                         if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
152                                         old_ctx.base_ntfs_ino) {
153                                 put_this_page = old_ctx.ntfs_ino->page;
154                                 page_cache_get(put_this_page);
155                         }
156                         /*
157                          * Reinitialize the search context so we can lookup the
158                          * needed attribute extent.
159                          */
160                         ntfs_attr_reinit_search_ctx(ctx);
161                         ctx_needs_reset = true;
162                 }
163         }
164         if (ctx_needs_reset) {
165                 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
166                                 CASE_SENSITIVE, vcn, NULL, 0, ctx);
167                 if (unlikely(err)) {
168                         if (err == -ENOENT)
169                                 err = -EIO;
170                         goto err_out;
171                 }
172                 BUG_ON(!ctx->attr->non_resident);
173         }
174         a = ctx->attr;
175         /*
176          * Only decompress the mapping pairs if @vcn is inside it.  Otherwise
177          * we get into problems when we try to map an out of bounds vcn because
178          * we then try to map the already mapped runlist fragment and
179          * ntfs_mapping_pairs_decompress() fails.
180          */
181         end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
182         if (!a->data.non_resident.lowest_vcn && end_vcn == 1)
183                 end_vcn = sle64_to_cpu(a->data.non_resident.allocated_size) >>
184                                 ni->vol->cluster_size_bits;
185         if (unlikely(vcn >= end_vcn)) {
186                 err = -ENOENT;
187                 goto err_out;
188         }
189         rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
190         if (IS_ERR(rl))
191                 err = PTR_ERR(rl);
192         else
193                 ni->runlist.rl = rl;
194 err_out:
195         if (ctx_is_temporary) {
196                 if (likely(ctx))
197                         ntfs_attr_put_search_ctx(ctx);
198                 unmap_mft_record(base_ni);
199         } else if (ctx_needs_reset) {
200                 /*
201                  * If there is no attribute list, restoring the search context
202                  * is acomplished simply by copying the saved context back over
203                  * the caller supplied context.  If there is an attribute list,
204                  * things are more complicated as we need to deal with mapping
205                  * of mft records and resulting potential changes in pointers.
206                  */
207                 if (NInoAttrList(base_ni)) {
208                         /*
209                          * If the currently mapped (extent) inode is not the
210                          * one we had before, we need to unmap it and map the
211                          * old one.
212                          */
213                         if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
214                                 /*
215                                  * If the currently mapped inode is not the
216                                  * base inode, unmap it.
217                                  */
218                                 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
219                                                 ctx->base_ntfs_ino) {
220                                         unmap_extent_mft_record(ctx->ntfs_ino);
221                                         ctx->mrec = ctx->base_mrec;
222                                         BUG_ON(!ctx->mrec);
223                                 }
224                                 /*
225                                  * If the old mapped inode is not the base
226                                  * inode, map it.
227                                  */
228                                 if (old_ctx.base_ntfs_ino &&
229                                                 old_ctx.ntfs_ino !=
230                                                 old_ctx.base_ntfs_ino) {
231 retry_map:
232                                         ctx->mrec = map_mft_record(
233                                                         old_ctx.ntfs_ino);
234                                         /*
235                                          * Something bad has happened.  If out
236                                          * of memory retry till it succeeds.
237                                          * Any other errors are fatal and we
238                                          * return the error code in ctx->mrec.
239                                          * Let the caller deal with it...  We
240                                          * just need to fudge things so the
241                                          * caller can reinit and/or put the
242                                          * search context safely.
243                                          */
244                                         if (IS_ERR(ctx->mrec)) {
245                                                 if (PTR_ERR(ctx->mrec) ==
246                                                                 -ENOMEM) {
247                                                         schedule();
248                                                         goto retry_map;
249                                                 } else
250                                                         old_ctx.ntfs_ino =
251                                                                 old_ctx.
252                                                                 base_ntfs_ino;
253                                         }
254                                 }
255                         }
256                         /* Update the changed pointers in the saved context. */
257                         if (ctx->mrec != old_ctx.mrec) {
258                                 if (!IS_ERR(ctx->mrec))
259                                         old_ctx.attr = (ATTR_RECORD*)(
260                                                         (u8*)ctx->mrec +
261                                                         ((u8*)old_ctx.attr -
262                                                         (u8*)old_ctx.mrec));
263                                 old_ctx.mrec = ctx->mrec;
264                         }
265                 }
266                 /* Restore the search context to the saved one. */
267                 *ctx = old_ctx;
268                 /*
269                  * We drop the reference on the page we took earlier.  In the
270                  * case that IS_ERR(ctx->mrec) is true this means we might lose
271                  * some changes to the mft record that had been made between
272                  * the last time it was marked dirty/written out and now.  This
273                  * at this stage is not a problem as the mapping error is fatal
274                  * enough that the mft record cannot be written out anyway and
275                  * the caller is very likely to shutdown the whole inode
276                  * immediately and mark the volume dirty for chkdsk to pick up
277                  * the pieces anyway.
278                  */
279                 if (put_this_page)
280                         page_cache_release(put_this_page);
281         }
282         return err;
283 }
284
285 /**
286  * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
287  * @ni:         ntfs inode for which to map (part of) a runlist
288  * @vcn:        map runlist part containing this vcn
289  *
290  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
291  *
292  * Return 0 on success and -errno on error.  There is one special error code
293  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
294  * of bounds of the runlist.
295  *
296  * Locking: - The runlist must be unlocked on entry and is unlocked on return.
297  *          - This function takes the runlist lock for writing and may modify
298  *            the runlist.
299  */
300 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
301 {
302         int err = 0;
303
304         down_write(&ni->runlist.lock);
305         /* Make sure someone else didn't do the work while we were sleeping. */
306         if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
307                         LCN_RL_NOT_MAPPED))
308                 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
309         up_write(&ni->runlist.lock);
310         return err;
311 }
312
313 /**
314  * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
315  * @ni:                 ntfs inode of the attribute whose runlist to search
316  * @vcn:                vcn to convert
317  * @write_locked:       true if the runlist is locked for writing
318  *
319  * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
320  * described by the ntfs inode @ni and return the corresponding logical cluster
321  * number (lcn).
322  *
323  * If the @vcn is not mapped yet, the attempt is made to map the attribute
324  * extent containing the @vcn and the vcn to lcn conversion is retried.
325  *
326  * If @write_locked is true the caller has locked the runlist for writing and
327  * if false for reading.
328  *
329  * Since lcns must be >= 0, we use negative return codes with special meaning:
330  *
331  * Return code  Meaning / Description
332  * ==========================================
333  *  LCN_HOLE    Hole / not allocated on disk.
334  *  LCN_ENOENT  There is no such vcn in the runlist, i.e. @vcn is out of bounds.
335  *  LCN_ENOMEM  Not enough memory to map runlist.
336  *  LCN_EIO     Critical error (runlist/file is corrupt, i/o error, etc).
337  *
338  * Locking: - The runlist must be locked on entry and is left locked on return.
339  *          - If @write_locked is 'false', i.e. the runlist is locked for reading,
340  *            the lock may be dropped inside the function so you cannot rely on
341  *            the runlist still being the same when this function returns.
342  */
343 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
344                 const bool write_locked)
345 {
346         LCN lcn;
347         unsigned long flags;
348         bool is_retry = false;
349
350         ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
351                         ni->mft_no, (unsigned long long)vcn,
352                         write_locked ? "write" : "read");
353         BUG_ON(!ni);
354         BUG_ON(!NInoNonResident(ni));
355         BUG_ON(vcn < 0);
356         if (!ni->runlist.rl) {
357                 read_lock_irqsave(&ni->size_lock, flags);
358                 if (!ni->allocated_size) {
359                         read_unlock_irqrestore(&ni->size_lock, flags);
360                         return LCN_ENOENT;
361                 }
362                 read_unlock_irqrestore(&ni->size_lock, flags);
363         }
364 retry_remap:
365         /* Convert vcn to lcn.  If that fails map the runlist and retry once. */
366         lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
367         if (likely(lcn >= LCN_HOLE)) {
368                 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
369                 return lcn;
370         }
371         if (lcn != LCN_RL_NOT_MAPPED) {
372                 if (lcn != LCN_ENOENT)
373                         lcn = LCN_EIO;
374         } else if (!is_retry) {
375                 int err;
376
377                 if (!write_locked) {
378                         up_read(&ni->runlist.lock);
379                         down_write(&ni->runlist.lock);
380                         if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
381                                         LCN_RL_NOT_MAPPED)) {
382                                 up_write(&ni->runlist.lock);
383                                 down_read(&ni->runlist.lock);
384                                 goto retry_remap;
385                         }
386                 }
387                 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
388                 if (!write_locked) {
389                         up_write(&ni->runlist.lock);
390                         down_read(&ni->runlist.lock);
391                 }
392                 if (likely(!err)) {
393                         is_retry = true;
394                         goto retry_remap;
395                 }
396                 if (err == -ENOENT)
397                         lcn = LCN_ENOENT;
398                 else if (err == -ENOMEM)
399                         lcn = LCN_ENOMEM;
400                 else
401                         lcn = LCN_EIO;
402         }
403         if (lcn != LCN_ENOENT)
404                 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
405                                 (long long)lcn);
406         return lcn;
407 }
408
409 /**
410  * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
411  * @ni:         ntfs inode describing the runlist to search
412  * @vcn:        vcn to find
413  * @ctx:        active attribute search context if present or NULL if not
414  *
415  * Find the virtual cluster number @vcn in the runlist described by the ntfs
416  * inode @ni and return the address of the runlist element containing the @vcn.
417  *
418  * If the @vcn is not mapped yet, the attempt is made to map the attribute
419  * extent containing the @vcn and the vcn to lcn conversion is retried.
420  *
421  * If @ctx is specified, it is an active search context of @ni and its base mft
422  * record.  This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
423  * runlist fragments and allows their mapping.  If you do not have the mft
424  * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
425  * will perform the necessary mapping and unmapping.
426  *
427  * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
428  * restores it before returning.  Thus, @ctx will be left pointing to the same
429  * attribute on return as on entry.  However, the actual pointers in @ctx may
430  * point to different memory locations on return, so you must remember to reset
431  * any cached pointers from the @ctx, i.e. after the call to
432  * ntfs_attr_find_vcn_nolock(), you will probably want to do:
433  *      m = ctx->mrec;
434  *      a = ctx->attr;
435  * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
436  * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
437  * Note you need to distinguish between the lcn of the returned runlist element
438  * being >= 0 and LCN_HOLE.  In the later case you have to return zeroes on
439  * read and allocate clusters on write.
440  *
441  * Return the runlist element containing the @vcn on success and
442  * ERR_PTR(-errno) on error.  You need to test the return value with IS_ERR()
443  * to decide if the return is success or failure and PTR_ERR() to get to the
444  * error code if IS_ERR() is true.
445  *
446  * The possible error return codes are:
447  *      -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
448  *      -ENOMEM - Not enough memory to map runlist.
449  *      -EIO    - Critical error (runlist/file is corrupt, i/o error, etc).
450  *
451  * WARNING: If @ctx is supplied, regardless of whether success or failure is
452  *          returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
453  *          is no longer valid, i.e. you need to either call
454  *          ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
455  *          In that case PTR_ERR(@ctx->mrec) will give you the error code for
456  *          why the mapping of the old inode failed.
457  *
458  * Locking: - The runlist described by @ni must be locked for writing on entry
459  *            and is locked on return.  Note the runlist may be modified when
460  *            needed runlist fragments need to be mapped.
461  *          - If @ctx is NULL, the base mft record of @ni must not be mapped on
462  *            entry and it will be left unmapped on return.
463  *          - If @ctx is not NULL, the base mft record must be mapped on entry
464  *            and it will be left mapped on return.
465  */
466 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
467                 ntfs_attr_search_ctx *ctx)
468 {
469         unsigned long flags;
470         runlist_element *rl;
471         int err = 0;
472         bool is_retry = false;
473
474         ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
475                         ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
476         BUG_ON(!ni);
477         BUG_ON(!NInoNonResident(ni));
478         BUG_ON(vcn < 0);
479         if (!ni->runlist.rl) {
480                 read_lock_irqsave(&ni->size_lock, flags);
481                 if (!ni->allocated_size) {
482                         read_unlock_irqrestore(&ni->size_lock, flags);
483                         return ERR_PTR(-ENOENT);
484                 }
485                 read_unlock_irqrestore(&ni->size_lock, flags);
486         }
487 retry_remap:
488         rl = ni->runlist.rl;
489         if (likely(rl && vcn >= rl[0].vcn)) {
490                 while (likely(rl->length)) {
491                         if (unlikely(vcn < rl[1].vcn)) {
492                                 if (likely(rl->lcn >= LCN_HOLE)) {
493                                         ntfs_debug("Done.");
494                                         return rl;
495                                 }
496                                 break;
497                         }
498                         rl++;
499                 }
500                 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
501                         if (likely(rl->lcn == LCN_ENOENT))
502                                 err = -ENOENT;
503                         else
504                                 err = -EIO;
505                 }
506         }
507         if (!err && !is_retry) {
508                 /*
509                  * If the search context is invalid we cannot map the unmapped
510                  * region.
511                  */
512                 if (IS_ERR(ctx->mrec))
513                         err = PTR_ERR(ctx->mrec);
514                 else {
515                         /*
516                          * The @vcn is in an unmapped region, map the runlist
517                          * and retry.
518                          */
519                         err = ntfs_map_runlist_nolock(ni, vcn, ctx);
520                         if (likely(!err)) {
521                                 is_retry = true;
522                                 goto retry_remap;
523                         }
524                 }
525                 if (err == -EINVAL)
526                         err = -EIO;
527         } else if (!err)
528                 err = -EIO;
529         if (err != -ENOENT)
530                 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
531         return ERR_PTR(err);
532 }
533
534 /**
535  * ntfs_attr_find - find (next) attribute in mft record
536  * @type:       attribute type to find
537  * @name:       attribute name to find (optional, i.e. NULL means don't care)
538  * @name_len:   attribute name length (only needed if @name present)
539  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
540  * @val:        attribute value to find (optional, resident attributes only)
541  * @val_len:    attribute value length
542  * @ctx:        search context with mft record and attribute to search from
543  *
544  * You should not need to call this function directly.  Use ntfs_attr_lookup()
545  * instead.
546  *
547  * ntfs_attr_find() takes a search context @ctx as parameter and searches the
548  * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
549  * attribute of @type, optionally @name and @val.
550  *
551  * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
552  * point to the found attribute.
553  *
554  * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
555  * @ctx->attr will point to the attribute before which the attribute being
556  * searched for would need to be inserted if such an action were to be desired.
557  *
558  * On actual error, ntfs_attr_find() returns -EIO.  In this case @ctx->attr is
559  * undefined and in particular do not rely on it not changing.
560  *
561  * If @ctx->is_first is 'true', the search begins with @ctx->attr itself.  If it
562  * is 'false', the search begins after @ctx->attr.
563  *
564  * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
565  * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
566  * @ctx->mrec belongs.  This is so we can get at the ntfs volume and hence at
567  * the upcase table.  If @ic is CASE_SENSITIVE, the comparison is case
568  * sensitive.  When @name is present, @name_len is the @name length in Unicode
569  * characters.
570  *
571  * If @name is not present (NULL), we assume that the unnamed attribute is
572  * being searched for.
573  *
574  * Finally, the resident attribute value @val is looked for, if present.  If
575  * @val is not present (NULL), @val_len is ignored.
576  *
577  * ntfs_attr_find() only searches the specified mft record and it ignores the
578  * presence of an attribute list attribute (unless it is the one being searched
579  * for, obviously).  If you need to take attribute lists into consideration,
580  * use ntfs_attr_lookup() instead (see below).  This also means that you cannot
581  * use ntfs_attr_find() to search for extent records of non-resident
582  * attributes, as extents with lowest_vcn != 0 are usually described by the
583  * attribute list attribute only. - Note that it is possible that the first
584  * extent is only in the attribute list while the last extent is in the base
585  * mft record, so do not rely on being able to find the first extent in the
586  * base mft record.
587  *
588  * Warning: Never use @val when looking for attribute types which can be
589  *          non-resident as this most likely will result in a crash!
590  */
591 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
592                 const u32 name_len, const IGNORE_CASE_BOOL ic,
593                 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
594 {
595         ATTR_RECORD *a;
596         ntfs_volume *vol = ctx->ntfs_ino->vol;
597         ntfschar *upcase = vol->upcase;
598         u32 upcase_len = vol->upcase_len;
599
600         /*
601          * Iterate over attributes in mft record starting at @ctx->attr, or the
602          * attribute following that, if @ctx->is_first is 'true'.
603          */
604         if (ctx->is_first) {
605                 a = ctx->attr;
606                 ctx->is_first = false;
607         } else
608                 a = (ATTR_RECORD*)((u8*)ctx->attr +
609                                 le32_to_cpu(ctx->attr->length));
610         for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
611                 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
612                                 le32_to_cpu(ctx->mrec->bytes_allocated))
613                         break;
614                 ctx->attr = a;
615                 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
616                                 a->type == AT_END))
617                         return -ENOENT;
618                 if (unlikely(!a->length))
619                         break;
620                 if (a->type != type)
621                         continue;
622                 /*
623                  * If @name is present, compare the two names.  If @name is
624                  * missing, assume we want an unnamed attribute.
625                  */
626                 if (!name) {
627                         /* The search failed if the found attribute is named. */
628                         if (a->name_length)
629                                 return -ENOENT;
630                 } else if (!ntfs_are_names_equal(name, name_len,
631                             (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
632                             a->name_length, ic, upcase, upcase_len)) {
633                         register int rc;
634
635                         rc = ntfs_collate_names(name, name_len,
636                                         (ntfschar*)((u8*)a +
637                                         le16_to_cpu(a->name_offset)),
638                                         a->name_length, 1, IGNORE_CASE,
639                                         upcase, upcase_len);
640                         /*
641                          * If @name collates before a->name, there is no
642                          * matching attribute.
643                          */
644                         if (rc == -1)
645                                 return -ENOENT;
646                         /* If the strings are not equal, continue search. */
647                         if (rc)
648                                 continue;
649                         rc = ntfs_collate_names(name, name_len,
650                                         (ntfschar*)((u8*)a +
651                                         le16_to_cpu(a->name_offset)),
652                                         a->name_length, 1, CASE_SENSITIVE,
653                                         upcase, upcase_len);
654                         if (rc == -1)
655                                 return -ENOENT;
656                         if (rc)
657                                 continue;
658                 }
659                 /*
660                  * The names match or @name not present and attribute is
661                  * unnamed.  If no @val specified, we have found the attribute
662                  * and are done.
663                  */
664                 if (!val)
665                         return 0;
666                 /* @val is present; compare values. */
667                 else {
668                         register int rc;
669
670                         rc = memcmp(val, (u8*)a + le16_to_cpu(
671                                         a->data.resident.value_offset),
672                                         min_t(u32, val_len, le32_to_cpu(
673                                         a->data.resident.value_length)));
674                         /*
675                          * If @val collates before the current attribute's
676                          * value, there is no matching attribute.
677                          */
678                         if (!rc) {
679                                 register u32 avl;
680
681                                 avl = le32_to_cpu(
682                                                 a->data.resident.value_length);
683                                 if (val_len == avl)
684                                         return 0;
685                                 if (val_len < avl)
686                                         return -ENOENT;
687                         } else if (rc < 0)
688                                 return -ENOENT;
689                 }
690         }
691         ntfs_error(vol->sb, "Inode is corrupt.  Run chkdsk.");
692         NVolSetErrors(vol);
693         return -EIO;
694 }
695
696 /**
697  * load_attribute_list - load an attribute list into memory
698  * @vol:                ntfs volume from which to read
699  * @runlist:            runlist of the attribute list
700  * @al_start:           destination buffer
701  * @size:               size of the destination buffer in bytes
702  * @initialized_size:   initialized size of the attribute list
703  *
704  * Walk the runlist @runlist and load all clusters from it copying them into
705  * the linear buffer @al. The maximum number of bytes copied to @al is @size
706  * bytes. Note, @size does not need to be a multiple of the cluster size. If
707  * @initialized_size is less than @size, the region in @al between
708  * @initialized_size and @size will be zeroed and not read from disk.
709  *
710  * Return 0 on success or -errno on error.
711  */
712 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
713                 const s64 size, const s64 initialized_size)
714 {
715         LCN lcn;
716         u8 *al = al_start;
717         u8 *al_end = al + initialized_size;
718         runlist_element *rl;
719         struct buffer_head *bh;
720         struct super_block *sb;
721         unsigned long block_size;
722         unsigned long block, max_block;
723         int err = 0;
724         unsigned char block_size_bits;
725
726         ntfs_debug("Entering.");
727         if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
728                         initialized_size > size)
729                 return -EINVAL;
730         if (!initialized_size) {
731                 memset(al, 0, size);
732                 return 0;
733         }
734         sb = vol->sb;
735         block_size = sb->s_blocksize;
736         block_size_bits = sb->s_blocksize_bits;
737         down_read(&runlist->lock);
738         rl = runlist->rl;
739         if (!rl) {
740                 ntfs_error(sb, "Cannot read attribute list since runlist is "
741                                 "missing.");
742                 goto err_out;   
743         }
744         /* Read all clusters specified by the runlist one run at a time. */
745         while (rl->length) {
746                 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
747                 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
748                                 (unsigned long long)rl->vcn,
749                                 (unsigned long long)lcn);
750                 /* The attribute list cannot be sparse. */
751                 if (lcn < 0) {
752                         ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed.  Cannot "
753                                         "read attribute list.");
754                         goto err_out;
755                 }
756                 block = lcn << vol->cluster_size_bits >> block_size_bits;
757                 /* Read the run from device in chunks of block_size bytes. */
758                 max_block = block + (rl->length << vol->cluster_size_bits >>
759                                 block_size_bits);
760                 ntfs_debug("max_block = 0x%lx.", max_block);
761                 do {
762                         ntfs_debug("Reading block = 0x%lx.", block);
763                         bh = sb_bread(sb, block);
764                         if (!bh) {
765                                 ntfs_error(sb, "sb_bread() failed. Cannot "
766                                                 "read attribute list.");
767                                 goto err_out;
768                         }
769                         if (al + block_size >= al_end)
770                                 goto do_final;
771                         memcpy(al, bh->b_data, block_size);
772                         brelse(bh);
773                         al += block_size;
774                 } while (++block < max_block);
775                 rl++;
776         }
777         if (initialized_size < size) {
778 initialize:
779                 memset(al_start + initialized_size, 0, size - initialized_size);
780         }
781 done:
782         up_read(&runlist->lock);
783         return err;
784 do_final:
785         if (al < al_end) {
786                 /*
787                  * Partial block.
788                  *
789                  * Note: The attribute list can be smaller than its allocation
790                  * by multiple clusters.  This has been encountered by at least
791                  * two people running Windows XP, thus we cannot do any
792                  * truncation sanity checking here. (AIA)
793                  */
794                 memcpy(al, bh->b_data, al_end - al);
795                 brelse(bh);
796                 if (initialized_size < size)
797                         goto initialize;
798                 goto done;
799         }
800         brelse(bh);
801         /* Real overflow! */
802         ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
803                         "is truncated.");
804 err_out:
805         err = -EIO;
806         goto done;
807 }
808
809 /**
810  * ntfs_external_attr_find - find an attribute in the attribute list of an inode
811  * @type:       attribute type to find
812  * @name:       attribute name to find (optional, i.e. NULL means don't care)
813  * @name_len:   attribute name length (only needed if @name present)
814  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
815  * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
816  * @val:        attribute value to find (optional, resident attributes only)
817  * @val_len:    attribute value length
818  * @ctx:        search context with mft record and attribute to search from
819  *
820  * You should not need to call this function directly.  Use ntfs_attr_lookup()
821  * instead.
822  *
823  * Find an attribute by searching the attribute list for the corresponding
824  * attribute list entry.  Having found the entry, map the mft record if the
825  * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
826  * in there and return it.
827  *
828  * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
829  * have been obtained from a call to ntfs_attr_get_search_ctx().  On subsequent
830  * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
831  * then the base inode).
832  *
833  * After finishing with the attribute/mft record you need to call
834  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
835  * mapped inodes, etc).
836  *
837  * If the attribute is found, ntfs_external_attr_find() returns 0 and
838  * @ctx->attr will point to the found attribute.  @ctx->mrec will point to the
839  * mft record in which @ctx->attr is located and @ctx->al_entry will point to
840  * the attribute list entry for the attribute.
841  *
842  * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
843  * @ctx->attr will point to the attribute in the base mft record before which
844  * the attribute being searched for would need to be inserted if such an action
845  * were to be desired.  @ctx->mrec will point to the mft record in which
846  * @ctx->attr is located and @ctx->al_entry will point to the attribute list
847  * entry of the attribute before which the attribute being searched for would
848  * need to be inserted if such an action were to be desired.
849  *
850  * Thus to insert the not found attribute, one wants to add the attribute to
851  * @ctx->mrec (the base mft record) and if there is not enough space, the
852  * attribute should be placed in a newly allocated extent mft record.  The
853  * attribute list entry for the inserted attribute should be inserted in the
854  * attribute list attribute at @ctx->al_entry.
855  *
856  * On actual error, ntfs_external_attr_find() returns -EIO.  In this case
857  * @ctx->attr is undefined and in particular do not rely on it not changing.
858  */
859 static int ntfs_external_attr_find(const ATTR_TYPE type,
860                 const ntfschar *name, const u32 name_len,
861                 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
862                 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
863 {
864         ntfs_inode *base_ni, *ni;
865         ntfs_volume *vol;
866         ATTR_LIST_ENTRY *al_entry, *next_al_entry;
867         u8 *al_start, *al_end;
868         ATTR_RECORD *a;
869         ntfschar *al_name;
870         u32 al_name_len;
871         int err = 0;
872         static const char *es = " Unmount and run chkdsk.";
873
874         ni = ctx->ntfs_ino;
875         base_ni = ctx->base_ntfs_ino;
876         ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
877         if (!base_ni) {
878                 /* First call happens with the base mft record. */
879                 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
880                 ctx->base_mrec = ctx->mrec;
881         }
882         if (ni == base_ni)
883                 ctx->base_attr = ctx->attr;
884         if (type == AT_END)
885                 goto not_found;
886         vol = base_ni->vol;
887         al_start = base_ni->attr_list;
888         al_end = al_start + base_ni->attr_list_size;
889         if (!ctx->al_entry)
890                 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
891         /*
892          * Iterate over entries in attribute list starting at @ctx->al_entry,
893          * or the entry following that, if @ctx->is_first is 'true'.
894          */
895         if (ctx->is_first) {
896                 al_entry = ctx->al_entry;
897                 ctx->is_first = false;
898         } else
899                 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
900                                 le16_to_cpu(ctx->al_entry->length));
901         for (;; al_entry = next_al_entry) {
902                 /* Out of bounds check. */
903                 if ((u8*)al_entry < base_ni->attr_list ||
904                                 (u8*)al_entry > al_end)
905                         break;  /* Inode is corrupt. */
906                 ctx->al_entry = al_entry;
907                 /* Catch the end of the attribute list. */
908                 if ((u8*)al_entry == al_end)
909                         goto not_found;
910                 if (!al_entry->length)
911                         break;
912                 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
913                                 le16_to_cpu(al_entry->length) > al_end)
914                         break;
915                 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
916                                 le16_to_cpu(al_entry->length));
917                 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
918                         goto not_found;
919                 if (type != al_entry->type)
920                         continue;
921                 /*
922                  * If @name is present, compare the two names.  If @name is
923                  * missing, assume we want an unnamed attribute.
924                  */
925                 al_name_len = al_entry->name_length;
926                 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
927                 if (!name) {
928                         if (al_name_len)
929                                 goto not_found;
930                 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
931                                 name_len, ic, vol->upcase, vol->upcase_len)) {
932                         register int rc;
933
934                         rc = ntfs_collate_names(name, name_len, al_name,
935                                         al_name_len, 1, IGNORE_CASE,
936                                         vol->upcase, vol->upcase_len);
937                         /*
938                          * If @name collates before al_name, there is no
939                          * matching attribute.
940                          */
941                         if (rc == -1)
942                                 goto not_found;
943                         /* If the strings are not equal, continue search. */
944                         if (rc)
945                                 continue;
946                         /*
947                          * FIXME: Reverse engineering showed 0, IGNORE_CASE but
948                          * that is inconsistent with ntfs_attr_find().  The
949                          * subsequent rc checks were also different.  Perhaps I
950                          * made a mistake in one of the two.  Need to recheck
951                          * which is correct or at least see what is going on...
952                          * (AIA)
953                          */
954                         rc = ntfs_collate_names(name, name_len, al_name,
955                                         al_name_len, 1, CASE_SENSITIVE,
956                                         vol->upcase, vol->upcase_len);
957                         if (rc == -1)
958                                 goto not_found;
959                         if (rc)
960                                 continue;
961                 }
962                 /*
963                  * The names match or @name not present and attribute is
964                  * unnamed.  Now check @lowest_vcn.  Continue search if the
965                  * next attribute list entry still fits @lowest_vcn.  Otherwise
966                  * we have reached the right one or the search has failed.
967                  */
968                 if (lowest_vcn && (u8*)next_al_entry >= al_start            &&
969                                 (u8*)next_al_entry + 6 < al_end             &&
970                                 (u8*)next_al_entry + le16_to_cpu(
971                                         next_al_entry->length) <= al_end    &&
972                                 sle64_to_cpu(next_al_entry->lowest_vcn) <=
973                                         lowest_vcn                          &&
974                                 next_al_entry->type == al_entry->type       &&
975                                 next_al_entry->name_length == al_name_len   &&
976                                 ntfs_are_names_equal((ntfschar*)((u8*)
977                                         next_al_entry +
978                                         next_al_entry->name_offset),
979                                         next_al_entry->name_length,
980                                         al_name, al_name_len, CASE_SENSITIVE,
981                                         vol->upcase, vol->upcase_len))
982                         continue;
983                 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
984                         if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
985                                 ntfs_error(vol->sb, "Found stale mft "
986                                                 "reference in attribute list "
987                                                 "of base inode 0x%lx.%s",
988                                                 base_ni->mft_no, es);
989                                 err = -EIO;
990                                 break;
991                         }
992                 } else { /* Mft references do not match. */
993                         /* If there is a mapped record unmap it first. */
994                         if (ni != base_ni)
995                                 unmap_extent_mft_record(ni);
996                         /* Do we want the base record back? */
997                         if (MREF_LE(al_entry->mft_reference) ==
998                                         base_ni->mft_no) {
999                                 ni = ctx->ntfs_ino = base_ni;
1000                                 ctx->mrec = ctx->base_mrec;
1001                         } else {
1002                                 /* We want an extent record. */
1003                                 ctx->mrec = map_extent_mft_record(base_ni,
1004                                                 le64_to_cpu(
1005                                                 al_entry->mft_reference), &ni);
1006                                 if (IS_ERR(ctx->mrec)) {
1007                                         ntfs_error(vol->sb, "Failed to map "
1008                                                         "extent mft record "
1009                                                         "0x%lx of base inode "
1010                                                         "0x%lx.%s",
1011                                                         MREF_LE(al_entry->
1012                                                         mft_reference),
1013                                                         base_ni->mft_no, es);
1014                                         err = PTR_ERR(ctx->mrec);
1015                                         if (err == -ENOENT)
1016                                                 err = -EIO;
1017                                         /* Cause @ctx to be sanitized below. */
1018                                         ni = NULL;
1019                                         break;
1020                                 }
1021                                 ctx->ntfs_ino = ni;
1022                         }
1023                         ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1024                                         le16_to_cpu(ctx->mrec->attrs_offset));
1025                 }
1026                 /*
1027                  * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1028                  * mft record containing the attribute represented by the
1029                  * current al_entry.
1030                  */
1031                 /*
1032                  * We could call into ntfs_attr_find() to find the right
1033                  * attribute in this mft record but this would be less
1034                  * efficient and not quite accurate as ntfs_attr_find() ignores
1035                  * the attribute instance numbers for example which become
1036                  * important when one plays with attribute lists.  Also,
1037                  * because a proper match has been found in the attribute list
1038                  * entry above, the comparison can now be optimized.  So it is
1039                  * worth re-implementing a simplified ntfs_attr_find() here.
1040                  */
1041                 a = ctx->attr;
1042                 /*
1043                  * Use a manual loop so we can still use break and continue
1044                  * with the same meanings as above.
1045                  */
1046 do_next_attr_loop:
1047                 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1048                                 le32_to_cpu(ctx->mrec->bytes_allocated))
1049                         break;
1050                 if (a->type == AT_END)
1051                         break;
1052                 if (!a->length)
1053                         break;
1054                 if (al_entry->instance != a->instance)
1055                         goto do_next_attr;
1056                 /*
1057                  * If the type and/or the name are mismatched between the
1058                  * attribute list entry and the attribute record, there is
1059                  * corruption so we break and return error EIO.
1060                  */
1061                 if (al_entry->type != a->type)
1062                         break;
1063                 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1064                                 le16_to_cpu(a->name_offset)), a->name_length,
1065                                 al_name, al_name_len, CASE_SENSITIVE,
1066                                 vol->upcase, vol->upcase_len))
1067                         break;
1068                 ctx->attr = a;
1069                 /*
1070                  * If no @val specified or @val specified and it matches, we
1071                  * have found it!
1072                  */
1073                 if (!val || (!a->non_resident && le32_to_cpu(
1074                                 a->data.resident.value_length) == val_len &&
1075                                 !memcmp((u8*)a +
1076                                 le16_to_cpu(a->data.resident.value_offset),
1077                                 val, val_len))) {
1078                         ntfs_debug("Done, found.");
1079                         return 0;
1080                 }
1081 do_next_attr:
1082                 /* Proceed to the next attribute in the current mft record. */
1083                 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1084                 goto do_next_attr_loop;
1085         }
1086         if (!err) {
1087                 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1088                                 "attribute list attribute.%s", base_ni->mft_no,
1089                                 es);
1090                 err = -EIO;
1091         }
1092         if (ni != base_ni) {
1093                 if (ni)
1094                         unmap_extent_mft_record(ni);
1095                 ctx->ntfs_ino = base_ni;
1096                 ctx->mrec = ctx->base_mrec;
1097                 ctx->attr = ctx->base_attr;
1098         }
1099         if (err != -ENOMEM)
1100                 NVolSetErrors(vol);
1101         return err;
1102 not_found:
1103         /*
1104          * If we were looking for AT_END, we reset the search context @ctx and
1105          * use ntfs_attr_find() to seek to the end of the base mft record.
1106          */
1107         if (type == AT_END) {
1108                 ntfs_attr_reinit_search_ctx(ctx);
1109                 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1110                                 ctx);
1111         }
1112         /*
1113          * The attribute was not found.  Before we return, we want to ensure
1114          * @ctx->mrec and @ctx->attr indicate the position at which the
1115          * attribute should be inserted in the base mft record.  Since we also
1116          * want to preserve @ctx->al_entry we cannot reinitialize the search
1117          * context using ntfs_attr_reinit_search_ctx() as this would set
1118          * @ctx->al_entry to NULL.  Thus we do the necessary bits manually (see
1119          * ntfs_attr_init_search_ctx() below).  Note, we _only_ preserve
1120          * @ctx->al_entry as the remaining fields (base_*) are identical to
1121          * their non base_ counterparts and we cannot set @ctx->base_attr
1122          * correctly yet as we do not know what @ctx->attr will be set to by
1123          * the call to ntfs_attr_find() below.
1124          */
1125         if (ni != base_ni)
1126                 unmap_extent_mft_record(ni);
1127         ctx->mrec = ctx->base_mrec;
1128         ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1129                         le16_to_cpu(ctx->mrec->attrs_offset));
1130         ctx->is_first = true;
1131         ctx->ntfs_ino = base_ni;
1132         ctx->base_ntfs_ino = NULL;
1133         ctx->base_mrec = NULL;
1134         ctx->base_attr = NULL;
1135         /*
1136          * In case there are multiple matches in the base mft record, need to
1137          * keep enumerating until we get an attribute not found response (or
1138          * another error), otherwise we would keep returning the same attribute
1139          * over and over again and all programs using us for enumeration would
1140          * lock up in a tight loop.
1141          */
1142         do {
1143                 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1144                                 ctx);
1145         } while (!err);
1146         ntfs_debug("Done, not found.");
1147         return err;
1148 }
1149
1150 /**
1151  * ntfs_attr_lookup - find an attribute in an ntfs inode
1152  * @type:       attribute type to find
1153  * @name:       attribute name to find (optional, i.e. NULL means don't care)
1154  * @name_len:   attribute name length (only needed if @name present)
1155  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1156  * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1157  * @val:        attribute value to find (optional, resident attributes only)
1158  * @val_len:    attribute value length
1159  * @ctx:        search context with mft record and attribute to search from
1160  *
1161  * Find an attribute in an ntfs inode.  On first search @ctx->ntfs_ino must
1162  * be the base mft record and @ctx must have been obtained from a call to
1163  * ntfs_attr_get_search_ctx().
1164  *
1165  * This function transparently handles attribute lists and @ctx is used to
1166  * continue searches where they were left off at.
1167  *
1168  * After finishing with the attribute/mft record you need to call
1169  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1170  * mapped inodes, etc).
1171  *
1172  * Return 0 if the search was successful and -errno if not.
1173  *
1174  * When 0, @ctx->attr is the found attribute and it is in mft record
1175  * @ctx->mrec.  If an attribute list attribute is present, @ctx->al_entry is
1176  * the attribute list entry of the found attribute.
1177  *
1178  * When -ENOENT, @ctx->attr is the attribute which collates just after the
1179  * attribute being searched for, i.e. if one wants to add the attribute to the
1180  * mft record this is the correct place to insert it into.  If an attribute
1181  * list attribute is present, @ctx->al_entry is the attribute list entry which
1182  * collates just after the attribute list entry of the attribute being searched
1183  * for, i.e. if one wants to add the attribute to the mft record this is the
1184  * correct place to insert its attribute list entry into.
1185  *
1186  * When -errno != -ENOENT, an error occured during the lookup.  @ctx->attr is
1187  * then undefined and in particular you should not rely on it not changing.
1188  */
1189 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1190                 const u32 name_len, const IGNORE_CASE_BOOL ic,
1191                 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1192                 ntfs_attr_search_ctx *ctx)
1193 {
1194         ntfs_inode *base_ni;
1195
1196         ntfs_debug("Entering.");
1197         BUG_ON(IS_ERR(ctx->mrec));
1198         if (ctx->base_ntfs_ino)
1199                 base_ni = ctx->base_ntfs_ino;
1200         else
1201                 base_ni = ctx->ntfs_ino;
1202         /* Sanity check, just for debugging really. */
1203         BUG_ON(!base_ni);
1204         if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1205                 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1206                                 ctx);
1207         return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1208                         val, val_len, ctx);
1209 }
1210
1211 /**
1212  * ntfs_attr_init_search_ctx - initialize an attribute search context
1213  * @ctx:        attribute search context to initialize
1214  * @ni:         ntfs inode with which to initialize the search context
1215  * @mrec:       mft record with which to initialize the search context
1216  *
1217  * Initialize the attribute search context @ctx with @ni and @mrec.
1218  */
1219 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1220                 ntfs_inode *ni, MFT_RECORD *mrec)
1221 {
1222         *ctx = (ntfs_attr_search_ctx) {
1223                 .mrec = mrec,
1224                 /* Sanity checks are performed elsewhere. */
1225                 .attr = (ATTR_RECORD*)((u8*)mrec +
1226                                 le16_to_cpu(mrec->attrs_offset)),
1227                 .is_first = true,
1228                 .ntfs_ino = ni,
1229         };
1230 }
1231
1232 /**
1233  * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1234  * @ctx:        attribute search context to reinitialize
1235  *
1236  * Reinitialize the attribute search context @ctx, unmapping an associated
1237  * extent mft record if present, and initialize the search context again.
1238  *
1239  * This is used when a search for a new attribute is being started to reset
1240  * the search context to the beginning.
1241  */
1242 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1243 {
1244         if (likely(!ctx->base_ntfs_ino)) {
1245                 /* No attribute list. */
1246                 ctx->is_first = true;
1247                 /* Sanity checks are performed elsewhere. */
1248                 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1249                                 le16_to_cpu(ctx->mrec->attrs_offset));
1250                 /*
1251                  * This needs resetting due to ntfs_external_attr_find() which
1252                  * can leave it set despite having zeroed ctx->base_ntfs_ino.
1253                  */
1254                 ctx->al_entry = NULL;
1255                 return;
1256         } /* Attribute list. */
1257         if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1258                 unmap_extent_mft_record(ctx->ntfs_ino);
1259         ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1260         return;
1261 }
1262
1263 /**
1264  * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1265  * @ni:         ntfs inode with which to initialize the search context
1266  * @mrec:       mft record with which to initialize the search context
1267  *
1268  * Allocate a new attribute search context, initialize it with @ni and @mrec,
1269  * and return it. Return NULL if allocation failed.
1270  */
1271 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1272 {
1273         ntfs_attr_search_ctx *ctx;
1274
1275         ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
1276         if (ctx)
1277                 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1278         return ctx;
1279 }
1280
1281 /**
1282  * ntfs_attr_put_search_ctx - release an attribute search context
1283  * @ctx:        attribute search context to free
1284  *
1285  * Release the attribute search context @ctx, unmapping an associated extent
1286  * mft record if present.
1287  */
1288 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1289 {
1290         if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1291                 unmap_extent_mft_record(ctx->ntfs_ino);
1292         kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1293         return;
1294 }
1295
1296 #ifdef NTFS_RW
1297
1298 /**
1299  * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1300  * @vol:        ntfs volume to which the attribute belongs
1301  * @type:       attribute type which to find
1302  *
1303  * Search for the attribute definition record corresponding to the attribute
1304  * @type in the $AttrDef system file.
1305  *
1306  * Return the attribute type definition record if found and NULL if not found.
1307  */
1308 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1309                 const ATTR_TYPE type)
1310 {
1311         ATTR_DEF *ad;
1312
1313         BUG_ON(!vol->attrdef);
1314         BUG_ON(!type);
1315         for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1316                         vol->attrdef_size && ad->type; ++ad) {
1317                 /* We have not found it yet, carry on searching. */
1318                 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1319                         continue;
1320                 /* We found the attribute; return it. */
1321                 if (likely(ad->type == type))
1322                         return ad;
1323                 /* We have gone too far already.  No point in continuing. */
1324                 break;
1325         }
1326         /* Attribute not found. */
1327         ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1328                         le32_to_cpu(type));
1329         return NULL;
1330 }
1331
1332 /**
1333  * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1334  * @vol:        ntfs volume to which the attribute belongs
1335  * @type:       attribute type which to check
1336  * @size:       size which to check
1337  *
1338  * Check whether the @size in bytes is valid for an attribute of @type on the
1339  * ntfs volume @vol.  This information is obtained from $AttrDef system file.
1340  *
1341  * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1342  * listed in $AttrDef.
1343  */
1344 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1345                 const s64 size)
1346 {
1347         ATTR_DEF *ad;
1348
1349         BUG_ON(size < 0);
1350         /*
1351          * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1352          * listed in $AttrDef.
1353          */
1354         if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1355                 return -ERANGE;
1356         /* Get the $AttrDef entry for the attribute @type. */
1357         ad = ntfs_attr_find_in_attrdef(vol, type);
1358         if (unlikely(!ad))
1359                 return -ENOENT;
1360         /* Do the bounds check. */
1361         if (((sle64_to_cpu(ad->min_size) > 0) &&
1362                         size < sle64_to_cpu(ad->min_size)) ||
1363                         ((sle64_to_cpu(ad->max_size) > 0) && size >
1364                         sle64_to_cpu(ad->max_size)))
1365                 return -ERANGE;
1366         return 0;
1367 }
1368
1369 /**
1370  * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1371  * @vol:        ntfs volume to which the attribute belongs
1372  * @type:       attribute type which to check
1373  *
1374  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1375  * be non-resident.  This information is obtained from $AttrDef system file.
1376  *
1377  * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1378  * -ENOENT if the attribute is not listed in $AttrDef.
1379  */
1380 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1381 {
1382         ATTR_DEF *ad;
1383
1384         /* Find the attribute definition record in $AttrDef. */
1385         ad = ntfs_attr_find_in_attrdef(vol, type);
1386         if (unlikely(!ad))
1387                 return -ENOENT;
1388         /* Check the flags and return the result. */
1389         if (ad->flags & ATTR_DEF_RESIDENT)
1390                 return -EPERM;
1391         return 0;
1392 }
1393
1394 /**
1395  * ntfs_attr_can_be_resident - check if an attribute can be resident
1396  * @vol:        ntfs volume to which the attribute belongs
1397  * @type:       attribute type which to check
1398  *
1399  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1400  * be resident.  This information is derived from our ntfs knowledge and may
1401  * not be completely accurate, especially when user defined attributes are
1402  * present.  Basically we allow everything to be resident except for index
1403  * allocation and $EA attributes.
1404  *
1405  * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1406  *
1407  * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1408  *          otherwise windows will not boot (blue screen of death)!  We cannot
1409  *          check for this here as we do not know which inode's $Bitmap is
1410  *          being asked about so the caller needs to special case this.
1411  */
1412 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1413 {
1414         if (type == AT_INDEX_ALLOCATION)
1415                 return -EPERM;
1416         return 0;
1417 }
1418
1419 /**
1420  * ntfs_attr_record_resize - resize an attribute record
1421  * @m:          mft record containing attribute record
1422  * @a:          attribute record to resize
1423  * @new_size:   new size in bytes to which to resize the attribute record @a
1424  *
1425  * Resize the attribute record @a, i.e. the resident part of the attribute, in
1426  * the mft record @m to @new_size bytes.
1427  *
1428  * Return 0 on success and -errno on error.  The following error codes are
1429  * defined:
1430  *      -ENOSPC - Not enough space in the mft record @m to perform the resize.
1431  *
1432  * Note: On error, no modifications have been performed whatsoever.
1433  *
1434  * Warning: If you make a record smaller without having copied all the data you
1435  *          are interested in the data may be overwritten.
1436  */
1437 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1438 {
1439         ntfs_debug("Entering for new_size %u.", new_size);
1440         /* Align to 8 bytes if it is not already done. */
1441         if (new_size & 7)
1442                 new_size = (new_size + 7) & ~7;
1443         /* If the actual attribute length has changed, move things around. */
1444         if (new_size != le32_to_cpu(a->length)) {
1445                 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1446                                 le32_to_cpu(a->length) + new_size;
1447                 /* Not enough space in this mft record. */
1448                 if (new_muse > le32_to_cpu(m->bytes_allocated))
1449                         return -ENOSPC;
1450                 /* Move attributes following @a to their new location. */
1451                 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1452                                 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1453                                 (u8*)m) - le32_to_cpu(a->length));
1454                 /* Adjust @m to reflect the change in used space. */
1455                 m->bytes_in_use = cpu_to_le32(new_muse);
1456                 /* Adjust @a to reflect the new size. */
1457                 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1458                         a->length = cpu_to_le32(new_size);
1459         }
1460         return 0;
1461 }
1462
1463 /**
1464  * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1465  * @m:          mft record containing attribute record
1466  * @a:          attribute record whose value to resize
1467  * @new_size:   new size in bytes to which to resize the attribute value of @a
1468  *
1469  * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1470  * If the value is made bigger, the newly allocated space is cleared.
1471  *
1472  * Return 0 on success and -errno on error.  The following error codes are
1473  * defined:
1474  *      -ENOSPC - Not enough space in the mft record @m to perform the resize.
1475  *
1476  * Note: On error, no modifications have been performed whatsoever.
1477  *
1478  * Warning: If you make a record smaller without having copied all the data you
1479  *          are interested in the data may be overwritten.
1480  */
1481 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1482                 const u32 new_size)
1483 {
1484         u32 old_size;
1485
1486         /* Resize the resident part of the attribute record. */
1487         if (ntfs_attr_record_resize(m, a,
1488                         le16_to_cpu(a->data.resident.value_offset) + new_size))
1489                 return -ENOSPC;
1490         /*
1491          * The resize succeeded!  If we made the attribute value bigger, clear
1492          * the area between the old size and @new_size.
1493          */
1494         old_size = le32_to_cpu(a->data.resident.value_length);
1495         if (new_size > old_size)
1496                 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1497                                 old_size, 0, new_size - old_size);
1498         /* Finally update the length of the attribute value. */
1499         a->data.resident.value_length = cpu_to_le32(new_size);
1500         return 0;
1501 }
1502
1503 /**
1504  * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1505  * @ni:         ntfs inode describing the attribute to convert
1506  * @data_size:  size of the resident data to copy to the non-resident attribute
1507  *
1508  * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1509  * non-resident one.
1510  *
1511  * @data_size must be equal to the attribute value size.  This is needed since
1512  * we need to know the size before we can map the mft record and our callers
1513  * always know it.  The reason we cannot simply read the size from the vfs
1514  * inode i_size is that this is not necessarily uptodate.  This happens when
1515  * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
1516  *
1517  * Return 0 on success and -errno on error.  The following error return codes
1518  * are defined:
1519  *      -EPERM  - The attribute is not allowed to be non-resident.
1520  *      -ENOMEM - Not enough memory.
1521  *      -ENOSPC - Not enough disk space.
1522  *      -EINVAL - Attribute not defined on the volume.
1523  *      -EIO    - I/o error or other error.
1524  * Note that -ENOSPC is also returned in the case that there is not enough
1525  * space in the mft record to do the conversion.  This can happen when the mft
1526  * record is already very full.  The caller is responsible for trying to make
1527  * space in the mft record and trying again.  FIXME: Do we need a separate
1528  * error return code for this kind of -ENOSPC or is it always worth trying
1529  * again in case the attribute may then fit in a resident state so no need to
1530  * make it non-resident at all?  Ho-hum...  (AIA)
1531  *
1532  * NOTE to self: No changes in the attribute list are required to move from
1533  *               a resident to a non-resident attribute.
1534  *
1535  * Locking: - The caller must hold i_mutex on the inode.
1536  */
1537 int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1538 {
1539         s64 new_size;
1540         struct inode *vi = VFS_I(ni);
1541         ntfs_volume *vol = ni->vol;
1542         ntfs_inode *base_ni;
1543         MFT_RECORD *m;
1544         ATTR_RECORD *a;
1545         ntfs_attr_search_ctx *ctx;
1546         struct page *page;
1547         runlist_element *rl;
1548         u8 *kaddr;
1549         unsigned long flags;
1550         int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1551         u32 attr_size;
1552         u8 old_res_attr_flags;
1553
1554         /* Check that the attribute is allowed to be non-resident. */
1555         err = ntfs_attr_can_be_non_resident(vol, ni->type);
1556         if (unlikely(err)) {
1557                 if (err == -EPERM)
1558                         ntfs_debug("Attribute is not allowed to be "
1559                                         "non-resident.");
1560                 else
1561                         ntfs_debug("Attribute not defined on the NTFS "
1562                                         "volume!");
1563                 return err;
1564         }
1565         /*
1566          * FIXME: Compressed and encrypted attributes are not supported when
1567          * writing and we should never have gotten here for them.
1568          */
1569         BUG_ON(NInoCompressed(ni));
1570         BUG_ON(NInoEncrypted(ni));
1571         /*
1572          * The size needs to be aligned to a cluster boundary for allocation
1573          * purposes.
1574          */
1575         new_size = (data_size + vol->cluster_size - 1) &
1576                         ~(vol->cluster_size - 1);
1577         if (new_size > 0) {
1578                 /*
1579                  * Will need the page later and since the page lock nests
1580                  * outside all ntfs locks, we need to get the page now.
1581                  */
1582                 page = find_or_create_page(vi->i_mapping, 0,
1583                                 mapping_gfp_mask(vi->i_mapping));
1584                 if (unlikely(!page))
1585                         return -ENOMEM;
1586                 /* Start by allocating clusters to hold the attribute value. */
1587                 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1588                                 vol->cluster_size_bits, -1, DATA_ZONE, true);
1589                 if (IS_ERR(rl)) {
1590                         err = PTR_ERR(rl);
1591                         ntfs_debug("Failed to allocate cluster%s, error code "
1592                                         "%i.", (new_size >>
1593                                         vol->cluster_size_bits) > 1 ? "s" : "",
1594                                         err);
1595                         goto page_err_out;
1596                 }
1597         } else {
1598                 rl = NULL;
1599                 page = NULL;
1600         }
1601         /* Determine the size of the mapping pairs array. */
1602         mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1603         if (unlikely(mp_size < 0)) {
1604                 err = mp_size;
1605                 ntfs_debug("Failed to get size for mapping pairs array, error "
1606                                 "code %i.", err);
1607                 goto rl_err_out;
1608         }
1609         down_write(&ni->runlist.lock);
1610         if (!NInoAttr(ni))
1611                 base_ni = ni;
1612         else
1613                 base_ni = ni->ext.base_ntfs_ino;
1614         m = map_mft_record(base_ni);
1615         if (IS_ERR(m)) {
1616                 err = PTR_ERR(m);
1617                 m = NULL;
1618                 ctx = NULL;
1619                 goto err_out;
1620         }
1621         ctx = ntfs_attr_get_search_ctx(base_ni, m);
1622         if (unlikely(!ctx)) {
1623                 err = -ENOMEM;
1624                 goto err_out;
1625         }
1626         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1627                         CASE_SENSITIVE, 0, NULL, 0, ctx);
1628         if (unlikely(err)) {
1629                 if (err == -ENOENT)
1630                         err = -EIO;
1631                 goto err_out;
1632         }
1633         m = ctx->mrec;
1634         a = ctx->attr;
1635         BUG_ON(NInoNonResident(ni));
1636         BUG_ON(a->non_resident);
1637         /*
1638          * Calculate new offsets for the name and the mapping pairs array.
1639          */
1640         if (NInoSparse(ni) || NInoCompressed(ni))
1641                 name_ofs = (offsetof(ATTR_REC,
1642                                 data.non_resident.compressed_size) +
1643                                 sizeof(a->data.non_resident.compressed_size) +
1644                                 7) & ~7;
1645         else
1646                 name_ofs = (offsetof(ATTR_REC,
1647                                 data.non_resident.compressed_size) + 7) & ~7;
1648         mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1649         /*
1650          * Determine the size of the resident part of the now non-resident
1651          * attribute record.
1652          */
1653         arec_size = (mp_ofs + mp_size + 7) & ~7;
1654         /*
1655          * If the page is not uptodate bring it uptodate by copying from the
1656          * attribute value.
1657          */
1658         attr_size = le32_to_cpu(a->data.resident.value_length);
1659         BUG_ON(attr_size != data_size);
1660         if (page && !PageUptodate(page)) {
1661                 kaddr = kmap_atomic(page, KM_USER0);
1662                 memcpy(kaddr, (u8*)a +
1663                                 le16_to_cpu(a->data.resident.value_offset),
1664                                 attr_size);
1665                 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1666                 kunmap_atomic(kaddr, KM_USER0);
1667                 flush_dcache_page(page);
1668                 SetPageUptodate(page);
1669         }
1670         /* Backup the attribute flag. */
1671         old_res_attr_flags = a->data.resident.flags;
1672         /* Resize the resident part of the attribute record. */
1673         err = ntfs_attr_record_resize(m, a, arec_size);
1674         if (unlikely(err))
1675                 goto err_out;
1676         /*
1677          * Convert the resident part of the attribute record to describe a
1678          * non-resident attribute.
1679          */
1680         a->non_resident = 1;
1681         /* Move the attribute name if it exists and update the offset. */
1682         if (a->name_length)
1683                 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1684                                 a->name_length * sizeof(ntfschar));
1685         a->name_offset = cpu_to_le16(name_ofs);
1686         /* Setup the fields specific to non-resident attributes. */
1687         a->data.non_resident.lowest_vcn = 0;
1688         a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1689                         vol->cluster_size_bits);
1690         a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1691         memset(&a->data.non_resident.reserved, 0,
1692                         sizeof(a->data.non_resident.reserved));
1693         a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1694         a->data.non_resident.data_size =
1695                         a->data.non_resident.initialized_size =
1696                         cpu_to_sle64(attr_size);
1697         if (NInoSparse(ni) || NInoCompressed(ni)) {
1698                 a->data.non_resident.compression_unit = 0;
1699                 if (NInoCompressed(ni) || vol->major_ver < 3)
1700                         a->data.non_resident.compression_unit = 4;
1701                 a->data.non_resident.compressed_size =
1702                                 a->data.non_resident.allocated_size;
1703         } else
1704                 a->data.non_resident.compression_unit = 0;
1705         /* Generate the mapping pairs array into the attribute record. */
1706         err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1707                         arec_size - mp_ofs, rl, 0, -1, NULL);
1708         if (unlikely(err)) {
1709                 ntfs_debug("Failed to build mapping pairs, error code %i.",
1710                                 err);
1711                 goto undo_err_out;
1712         }
1713         /* Setup the in-memory attribute structure to be non-resident. */
1714         ni->runlist.rl = rl;
1715         write_lock_irqsave(&ni->size_lock, flags);
1716         ni->allocated_size = new_size;
1717         if (NInoSparse(ni) || NInoCompressed(ni)) {
1718                 ni->itype.compressed.size = ni->allocated_size;
1719                 if (a->data.non_resident.compression_unit) {
1720                         ni->itype.compressed.block_size = 1U << (a->data.
1721                                         non_resident.compression_unit +
1722                                         vol->cluster_size_bits);
1723                         ni->itype.compressed.block_size_bits =
1724                                         ffs(ni->itype.compressed.block_size) -
1725                                         1;
1726                         ni->itype.compressed.block_clusters = 1U <<
1727                                         a->data.non_resident.compression_unit;
1728                 } else {
1729                         ni->itype.compressed.block_size = 0;
1730                         ni->itype.compressed.block_size_bits = 0;
1731                         ni->itype.compressed.block_clusters = 0;
1732                 }
1733                 vi->i_blocks = ni->itype.compressed.size >> 9;
1734         } else
1735                 vi->i_blocks = ni->allocated_size >> 9;
1736         write_unlock_irqrestore(&ni->size_lock, flags);
1737         /*
1738          * This needs to be last since the address space operations ->readpage
1739          * and ->writepage can run concurrently with us as they are not
1740          * serialized on i_mutex.  Note, we are not allowed to fail once we flip
1741          * this switch, which is another reason to do this last.
1742          */
1743         NInoSetNonResident(ni);
1744         /* Mark the mft record dirty, so it gets written back. */
1745         flush_dcache_mft_record_page(ctx->ntfs_ino);
1746         mark_mft_record_dirty(ctx->ntfs_ino);
1747         ntfs_attr_put_search_ctx(ctx);
1748         unmap_mft_record(base_ni);
1749         up_write(&ni->runlist.lock);
1750         if (page) {
1751                 set_page_dirty(page);
1752                 unlock_page(page);
1753                 mark_page_accessed(page);
1754                 page_cache_release(page);
1755         }
1756         ntfs_debug("Done.");
1757         return 0;
1758 undo_err_out:
1759         /* Convert the attribute back into a resident attribute. */
1760         a->non_resident = 0;
1761         /* Move the attribute name if it exists and update the offset. */
1762         name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1763                         sizeof(a->data.resident.reserved) + 7) & ~7;
1764         if (a->name_length)
1765                 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1766                                 a->name_length * sizeof(ntfschar));
1767         mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1768         a->name_offset = cpu_to_le16(name_ofs);
1769         arec_size = (mp_ofs + attr_size + 7) & ~7;
1770         /* Resize the resident part of the attribute record. */
1771         err2 = ntfs_attr_record_resize(m, a, arec_size);
1772         if (unlikely(err2)) {
1773                 /*
1774                  * This cannot happen (well if memory corruption is at work it
1775                  * could happen in theory), but deal with it as well as we can.
1776                  * If the old size is too small, truncate the attribute,
1777                  * otherwise simply give it a larger allocated size.
1778                  * FIXME: Should check whether chkdsk complains when the
1779                  * allocated size is much bigger than the resident value size.
1780                  */
1781                 arec_size = le32_to_cpu(a->length);
1782                 if ((mp_ofs + attr_size) > arec_size) {
1783                         err2 = attr_size;
1784                         attr_size = arec_size - mp_ofs;
1785                         ntfs_error(vol->sb, "Failed to undo partial resident "
1786                                         "to non-resident attribute "
1787                                         "conversion.  Truncating inode 0x%lx, "
1788                                         "attribute type 0x%x from %i bytes to "
1789                                         "%i bytes to maintain metadata "
1790                                         "consistency.  THIS MEANS YOU ARE "
1791                                         "LOSING %i BYTES DATA FROM THIS %s.",
1792                                         vi->i_ino,
1793                                         (unsigned)le32_to_cpu(ni->type),
1794                                         err2, attr_size, err2 - attr_size,
1795                                         ((ni->type == AT_DATA) &&
1796                                         !ni->name_len) ? "FILE": "ATTRIBUTE");
1797                         write_lock_irqsave(&ni->size_lock, flags);
1798                         ni->initialized_size = attr_size;
1799                         i_size_write(vi, attr_size);
1800                         write_unlock_irqrestore(&ni->size_lock, flags);
1801                 }
1802         }
1803         /* Setup the fields specific to resident attributes. */
1804         a->data.resident.value_length = cpu_to_le32(attr_size);
1805         a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1806         a->data.resident.flags = old_res_attr_flags;
1807         memset(&a->data.resident.reserved, 0,
1808                         sizeof(a->data.resident.reserved));
1809         /* Copy the data from the page back to the attribute value. */
1810         if (page) {
1811                 kaddr = kmap_atomic(page, KM_USER0);
1812                 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1813                 kunmap_atomic(kaddr, KM_USER0);
1814         }
1815         /* Setup the allocated size in the ntfs inode in case it changed. */
1816         write_lock_irqsave(&ni->size_lock, flags);
1817         ni->allocated_size = arec_size - mp_ofs;
1818         write_unlock_irqrestore(&ni->size_lock, flags);
1819         /* Mark the mft record dirty, so it gets written back. */
1820         flush_dcache_mft_record_page(ctx->ntfs_ino);
1821         mark_mft_record_dirty(ctx->ntfs_ino);
1822 err_out:
1823         if (ctx)
1824                 ntfs_attr_put_search_ctx(ctx);
1825         if (m)
1826                 unmap_mft_record(base_ni);
1827         ni->runlist.rl = NULL;
1828         up_write(&ni->runlist.lock);
1829 rl_err_out:
1830         if (rl) {
1831                 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1832                         ntfs_error(vol->sb, "Failed to release allocated "
1833                                         "cluster(s) in error code path.  Run "
1834                                         "chkdsk to recover the lost "
1835                                         "cluster(s).");
1836                         NVolSetErrors(vol);
1837                 }
1838                 ntfs_free(rl);
1839 page_err_out:
1840                 unlock_page(page);
1841                 page_cache_release(page);
1842         }
1843         if (err == -EINVAL)
1844                 err = -EIO;
1845         return err;
1846 }
1847
1848 /**
1849  * ntfs_attr_extend_allocation - extend the allocated space of an attribute
1850  * @ni:                 ntfs inode of the attribute whose allocation to extend
1851  * @new_alloc_size:     new size in bytes to which to extend the allocation to
1852  * @new_data_size:      new size in bytes to which to extend the data to
1853  * @data_start:         beginning of region which is required to be non-sparse
1854  *
1855  * Extend the allocated space of an attribute described by the ntfs inode @ni
1856  * to @new_alloc_size bytes.  If @data_start is -1, the whole extension may be
1857  * implemented as a hole in the file (as long as both the volume and the ntfs
1858  * inode @ni have sparse support enabled).  If @data_start is >= 0, then the
1859  * region between the old allocated size and @data_start - 1 may be made sparse
1860  * but the regions between @data_start and @new_alloc_size must be backed by
1861  * actual clusters.
1862  *
1863  * If @new_data_size is -1, it is ignored.  If it is >= 0, then the data size
1864  * of the attribute is extended to @new_data_size.  Note that the i_size of the
1865  * vfs inode is not updated.  Only the data size in the base attribute record
1866  * is updated.  The caller has to update i_size separately if this is required.
1867  * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
1868  * size as well as for @new_data_size to be greater than @new_alloc_size.
1869  *
1870  * For resident attributes this involves resizing the attribute record and if
1871  * necessary moving it and/or other attributes into extent mft records and/or
1872  * converting the attribute to a non-resident attribute which in turn involves
1873  * extending the allocation of a non-resident attribute as described below.
1874  *
1875  * For non-resident attributes this involves allocating clusters in the data
1876  * zone on the volume (except for regions that are being made sparse) and
1877  * extending the run list to describe the allocated clusters as well as
1878  * updating the mapping pairs array of the attribute.  This in turn involves
1879  * resizing the attribute record and if necessary moving it and/or other
1880  * attributes into extent mft records and/or splitting the attribute record
1881  * into multiple extent attribute records.
1882  *
1883  * Also, the attribute list attribute is updated if present and in some of the
1884  * above cases (the ones where extent mft records/attributes come into play),
1885  * an attribute list attribute is created if not already present.
1886  *
1887  * Return the new allocated size on success and -errno on error.  In the case
1888  * that an error is encountered but a partial extension at least up to
1889  * @data_start (if present) is possible, the allocation is partially extended
1890  * and this is returned.  This means the caller must check the returned size to
1891  * determine if the extension was partial.  If @data_start is -1 then partial
1892  * allocations are not performed.
1893  *
1894  * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
1895  *
1896  * Locking: This function takes the runlist lock of @ni for writing as well as
1897  * locking the mft record of the base ntfs inode.  These locks are maintained
1898  * throughout execution of the function.  These locks are required so that the
1899  * attribute can be resized safely and so that it can for example be converted
1900  * from resident to non-resident safely.
1901  *
1902  * TODO: At present attribute list attribute handling is not implemented.
1903  *
1904  * TODO: At present it is not safe to call this function for anything other
1905  * than the $DATA attribute(s) of an uncompressed and unencrypted file.
1906  */
1907 s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1908                 const s64 new_data_size, const s64 data_start)
1909 {
1910         VCN vcn;
1911         s64 ll, allocated_size, start = data_start;
1912         struct inode *vi = VFS_I(ni);
1913         ntfs_volume *vol = ni->vol;
1914         ntfs_inode *base_ni;
1915         MFT_RECORD *m;
1916         ATTR_RECORD *a;
1917         ntfs_attr_search_ctx *ctx;
1918         runlist_element *rl, *rl2;
1919         unsigned long flags;
1920         int err, mp_size;
1921         u32 attr_len = 0; /* Silence stupid gcc warning. */
1922         bool mp_rebuilt;
1923
1924 #ifdef DEBUG
1925         read_lock_irqsave(&ni->size_lock, flags);
1926         allocated_size = ni->allocated_size;
1927         read_unlock_irqrestore(&ni->size_lock, flags);
1928         ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1929                         "old_allocated_size 0x%llx, "
1930                         "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1931                         "data_start 0x%llx.", vi->i_ino,
1932                         (unsigned)le32_to_cpu(ni->type),
1933                         (unsigned long long)allocated_size,
1934                         (unsigned long long)new_alloc_size,
1935                         (unsigned long long)new_data_size,
1936                         (unsigned long long)start);
1937 #endif
1938 retry_extend:
1939         /*
1940          * For non-resident attributes, @start and @new_size need to be aligned
1941          * to cluster boundaries for allocation purposes.
1942          */
1943         if (NInoNonResident(ni)) {
1944                 if (start > 0)
1945                         start &= ~(s64)vol->cluster_size_mask;
1946                 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1947                                 ~(s64)vol->cluster_size_mask;
1948         }
1949         BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1950         /* Check if new size is allowed in $AttrDef. */
1951         err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1952         if (unlikely(err)) {
1953                 /* Only emit errors when the write will fail completely. */
1954                 read_lock_irqsave(&ni->size_lock, flags);
1955                 allocated_size = ni->allocated_size;
1956                 read_unlock_irqrestore(&ni->size_lock, flags);
1957                 if (start < 0 || start >= allocated_size) {
1958                         if (err == -ERANGE) {
1959                                 ntfs_error(vol->sb, "Cannot extend allocation "
1960                                                 "of inode 0x%lx, attribute "
1961                                                 "type 0x%x, because the new "
1962                                                 "allocation would exceed the "
1963                                                 "maximum allowed size for "
1964                                                 "this attribute type.",
1965                                                 vi->i_ino, (unsigned)
1966                                                 le32_to_cpu(ni->type));
1967                         } else {
1968                                 ntfs_error(vol->sb, "Cannot extend allocation "
1969                                                 "of inode 0x%lx, attribute "
1970                                                 "type 0x%x, because this "
1971                                                 "attribute type is not "
1972                                                 "defined on the NTFS volume.  "
1973                                                 "Possible corruption!  You "
1974                                                 "should run chkdsk!",
1975                                                 vi->i_ino, (unsigned)
1976                                                 le32_to_cpu(ni->type));
1977                         }
1978                 }
1979                 /* Translate error code to be POSIX conformant for write(2). */
1980                 if (err == -ERANGE)
1981                         err = -EFBIG;
1982                 else
1983                         err = -EIO;
1984                 return err;
1985         }
1986         if (!NInoAttr(ni))
1987                 base_ni = ni;
1988         else
1989                 base_ni = ni->ext.base_ntfs_ino;
1990         /*
1991          * We will be modifying both the runlist (if non-resident) and the mft
1992          * record so lock them both down.
1993          */
1994         down_write(&ni->runlist.lock);
1995         m = map_mft_record(base_ni);
1996         if (IS_ERR(m)) {
1997                 err = PTR_ERR(m);
1998                 m = NULL;
1999                 ctx = NULL;
2000                 goto err_out;
2001         }
2002         ctx = ntfs_attr_get_search_ctx(base_ni, m);
2003         if (unlikely(!ctx)) {
2004                 err = -ENOMEM;
2005                 goto err_out;
2006         }
2007         read_lock_irqsave(&ni->size_lock, flags);
2008         allocated_size = ni->allocated_size;
2009         read_unlock_irqrestore(&ni->size_lock, flags);
2010         /*
2011          * If non-resident, seek to the last extent.  If resident, there is
2012          * only one extent, so seek to that.
2013          */
2014         vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2015                         0;
2016         /*
2017          * Abort if someone did the work whilst we waited for the locks.  If we
2018          * just converted the attribute from resident to non-resident it is
2019          * likely that exactly this has happened already.  We cannot quite
2020          * abort if we need to update the data size.
2021          */
2022         if (unlikely(new_alloc_size <= allocated_size)) {
2023                 ntfs_debug("Allocated size already exceeds requested size.");
2024                 new_alloc_size = allocated_size;
2025                 if (new_data_size < 0)
2026                         goto done;
2027                 /*
2028                  * We want the first attribute extent so that we can update the
2029                  * data size.
2030                  */
2031                 vcn = 0;
2032         }
2033         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2034                         CASE_SENSITIVE, vcn, NULL, 0, ctx);
2035         if (unlikely(err)) {
2036                 if (err == -ENOENT)
2037                         err = -EIO;
2038                 goto err_out;
2039         }
2040         m = ctx->mrec;
2041         a = ctx->attr;
2042         /* Use goto to reduce indentation. */
2043         if (a->non_resident)
2044                 goto do_non_resident_extend;
2045         BUG_ON(NInoNonResident(ni));
2046         /* The total length of the attribute value. */
2047         attr_len = le32_to_cpu(a->data.resident.value_length);
2048         /*
2049          * Extend the attribute record to be able to store the new attribute
2050          * size.  ntfs_attr_record_resize() will not do anything if the size is
2051          * not changing.
2052          */
2053         if (new_alloc_size < vol->mft_record_size &&
2054                         !ntfs_attr_record_resize(m, a,
2055                         le16_to_cpu(a->data.resident.value_offset) +
2056                         new_alloc_size)) {
2057                 /* The resize succeeded! */
2058                 write_lock_irqsave(&ni->size_lock, flags);
2059                 ni->allocated_size = le32_to_cpu(a->length) -
2060                                 le16_to_cpu(a->data.resident.value_offset);
2061                 write_unlock_irqrestore(&ni->size_lock, flags);
2062                 if (new_data_size >= 0) {
2063                         BUG_ON(new_data_size < attr_len);
2064                         a->data.resident.value_length =
2065                                         cpu_to_le32((u32)new_data_size);
2066                 }
2067                 goto flush_done;
2068         }
2069         /*
2070          * We have to drop all the locks so we can call
2071          * ntfs_attr_make_non_resident().  This could be optimised by try-
2072          * locking the first page cache page and only if that fails dropping
2073          * the locks, locking the page, and redoing all the locking and
2074          * lookups.  While this would be a huge optimisation, it is not worth
2075          * it as this is definitely a slow code path.
2076          */
2077         ntfs_attr_put_search_ctx(ctx);
2078         unmap_mft_record(base_ni);
2079         up_write(&ni->runlist.lock);
2080         /*
2081          * Not enough space in the mft record, try to make the attribute
2082          * non-resident and if successful restart the extension process.
2083          */
2084         err = ntfs_attr_make_non_resident(ni, attr_len);
2085         if (likely(!err))
2086                 goto retry_extend;
2087         /*
2088          * Could not make non-resident.  If this is due to this not being
2089          * permitted for this attribute type or there not being enough space,
2090          * try to make other attributes non-resident.  Otherwise fail.
2091          */
2092         if (unlikely(err != -EPERM && err != -ENOSPC)) {
2093                 /* Only emit errors when the write will fail completely. */
2094                 read_lock_irqsave(&ni->size_lock, flags);
2095                 allocated_size = ni->allocated_size;
2096                 read_unlock_irqrestore(&ni->size_lock, flags);
2097                 if (start < 0 || start >= allocated_size)
2098                         ntfs_error(vol->sb, "Cannot extend allocation of "
2099                                         "inode 0x%lx, attribute type 0x%x, "
2100                                         "because the conversion from resident "
2101                                         "to non-resident attribute failed "
2102                                         "with error code %i.", vi->i_ino,
2103                                         (unsigned)le32_to_cpu(ni->type), err);
2104                 if (err != -ENOMEM)
2105                         err = -EIO;
2106                 goto conv_err_out;
2107         }
2108         /* TODO: Not implemented from here, abort. */
2109         read_lock_irqsave(&ni->size_lock, flags);
2110         allocated_size = ni->allocated_size;
2111         read_unlock_irqrestore(&ni->size_lock, flags);
2112         if (start < 0 || start >= allocated_size) {
2113                 if (err == -ENOSPC)
2114                         ntfs_error(vol->sb, "Not enough space in the mft "
2115                                         "record/on disk for the non-resident "
2116                                         "attribute value.  This case is not "
2117                                         "implemented yet.");
2118                 else /* if (err == -EPERM) */
2119                         ntfs_error(vol->sb, "This attribute type may not be "
2120                                         "non-resident.  This case is not "
2121                                         "implemented yet.");
2122         }
2123         err = -EOPNOTSUPP;
2124         goto conv_err_out;
2125 #if 0
2126         // TODO: Attempt to make other attributes non-resident.
2127         if (!err)
2128                 goto do_resident_extend;
2129         /*
2130          * Both the attribute list attribute and the standard information
2131          * attribute must remain in the base inode.  Thus, if this is one of
2132          * these attributes, we have to try to move other attributes out into
2133          * extent mft records instead.
2134          */
2135         if (ni->type == AT_ATTRIBUTE_LIST ||
2136                         ni->type == AT_STANDARD_INFORMATION) {
2137                 // TODO: Attempt to move other attributes into extent mft
2138                 // records.
2139                 err = -EOPNOTSUPP;
2140                 if (!err)
2141                         goto do_resident_extend;
2142                 goto err_out;
2143         }
2144         // TODO: Attempt to move this attribute to an extent mft record, but
2145         // only if it is not already the only attribute in an mft record in
2146         // which case there would be nothing to gain.
2147         err = -EOPNOTSUPP;
2148         if (!err)
2149                 goto do_resident_extend;
2150         /* There is nothing we can do to make enough space. )-: */
2151         goto err_out;
2152 #endif
2153 do_non_resident_extend:
2154         BUG_ON(!NInoNonResident(ni));
2155         if (new_alloc_size == allocated_size) {
2156                 BUG_ON(vcn);
2157                 goto alloc_done;
2158         }
2159         /*
2160          * If the data starts after the end of the old allocation, this is a
2161          * $DATA attribute and sparse attributes are enabled on the volume and
2162          * for this inode, then create a sparse region between the old
2163          * allocated size and the start of the data.  Otherwise simply proceed
2164          * with filling the whole space between the old allocated size and the
2165          * new allocated size with clusters.
2166          */
2167         if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2168                         !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2169                 goto skip_sparse;
2170         // TODO: This is not implemented yet.  We just fill in with real
2171         // clusters for now...
2172         ntfs_debug("Inserting holes is not-implemented yet.  Falling back to "
2173                         "allocating real clusters instead.");
2174 skip_sparse:
2175         rl = ni->runlist.rl;
2176         if (likely(rl)) {
2177                 /* Seek to the end of the runlist. */
2178                 while (rl->length)
2179                         rl++;
2180         }
2181         /* If this attribute extent is not mapped, map it now. */
2182         if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2183                         (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2184                         (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2185                 if (!rl && !allocated_size)
2186                         goto first_alloc;
2187                 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2188                 if (IS_ERR(rl)) {
2189                         err = PTR_ERR(rl);
2190                         if (start < 0 || start >= allocated_size)
2191                                 ntfs_error(vol->sb, "Cannot extend allocation "
2192                                                 "of inode 0x%lx, attribute "
2193                                                 "type 0x%x, because the "
2194                                                 "mapping of a runlist "
2195                                                 "fragment failed with error "
2196                                                 "code %i.", vi->i_ino,
2197                                                 (unsigned)le32_to_cpu(ni->type),
2198                                                 err);
2199                         if (err != -ENOMEM)
2200                                 err = -EIO;
2201                         goto err_out;
2202                 }
2203                 ni->runlist.rl = rl;
2204                 /* Seek to the end of the runlist. */
2205                 while (rl->length)
2206                         rl++;
2207         }
2208         /*
2209          * We now know the runlist of the last extent is mapped and @rl is at
2210          * the end of the runlist.  We want to begin allocating clusters
2211          * starting at the last allocated cluster to reduce fragmentation.  If
2212          * there are no valid LCNs in the attribute we let the cluster
2213          * allocator choose the starting cluster.
2214          */
2215         /* If the last LCN is a hole or simillar seek back to last real LCN. */
2216         while (rl->lcn < 0 && rl > ni->runlist.rl)
2217                 rl--;
2218 first_alloc:
2219         // FIXME: Need to implement partial allocations so at least part of the
2220         // write can be performed when start >= 0.  (Needed for POSIX write(2)
2221         // conformance.)
2222         rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2223                         (new_alloc_size - allocated_size) >>
2224                         vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2225                         rl->lcn + rl->length : -1, DATA_ZONE, true);
2226         if (IS_ERR(rl2)) {
2227                 err = PTR_ERR(rl2);
2228                 if (start < 0 || start >= allocated_size)
2229                         ntfs_error(vol->sb, "Cannot extend allocation of "
2230                                         "inode 0x%lx, attribute type 0x%x, "
2231                                         "because the allocation of clusters "
2232                                         "failed with error code %i.", vi->i_ino,
2233                                         (unsigned)le32_to_cpu(ni->type), err);
2234                 if (err != -ENOMEM && err != -ENOSPC)
2235                         err = -EIO;
2236                 goto err_out;
2237         }
2238         rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2239         if (IS_ERR(rl)) {
2240                 err = PTR_ERR(rl);
2241                 if (start < 0 || start >= allocated_size)
2242                         ntfs_error(vol->sb, "Cannot extend allocation of "
2243                                         "inode 0x%lx, attribute type 0x%x, "
2244                                         "because the runlist merge failed "
2245                                         "with error code %i.", vi->i_ino,
2246                                         (unsigned)le32_to_cpu(ni->type), err);
2247                 if (err != -ENOMEM)
2248                         err = -EIO;
2249                 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2250                         ntfs_error(vol->sb, "Failed to release allocated "
2251                                         "cluster(s) in error code path.  Run "
2252                                         "chkdsk to recover the lost "
2253                                         "cluster(s).");
2254                         NVolSetErrors(vol);
2255                 }
2256                 ntfs_free(rl2);
2257                 goto err_out;
2258         }
2259         ni->runlist.rl = rl;
2260         ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2261                         allocated_size) >> vol->cluster_size_bits);
2262         /* Find the runlist element with which the attribute extent starts. */
2263         ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2264         rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2265         BUG_ON(!rl2);
2266         BUG_ON(!rl2->length);
2267         BUG_ON(rl2->lcn < LCN_HOLE);
2268         mp_rebuilt = false;
2269         /* Get the size for the new mapping pairs array for this extent. */
2270         mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2271         if (unlikely(mp_size <= 0)) {
2272                 err = mp_size;
2273                 if (start < 0 || start >= allocated_size)
2274                         ntfs_error(vol->sb, "Cannot extend allocation of "
2275                                         "inode 0x%lx, attribute type 0x%x, "
2276                                         "because determining the size for the "
2277                                         "mapping pairs failed with error code "
2278                                         "%i.", vi->i_ino,
2279                                         (unsigned)le32_to_cpu(ni->type), err);
2280                 err = -EIO;
2281                 goto undo_alloc;
2282         }
2283         /* Extend the attribute record to fit the bigger mapping pairs array. */
2284         attr_len = le32_to_cpu(a->length);
2285         err = ntfs_attr_record_resize(m, a, mp_size +
2286                         le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2287         if (unlikely(err)) {
2288                 BUG_ON(err != -ENOSPC);
2289                 // TODO: Deal with this by moving this extent to a new mft
2290                 // record or by starting a new extent in a new mft record,
2291                 // possibly by extending this extent partially and filling it
2292                 // and creating a new extent for the remainder, or by making
2293                 // other attributes non-resident and/or by moving other
2294                 // attributes out of this mft record.
2295                 if (start < 0 || start >= allocated_size)
2296                         ntfs_error(vol->sb, "Not enough space in the mft "
2297                                         "record for the extended attribute "
2298                                         "record.  This case is not "
2299                                         "implemented yet.");
2300                 err = -EOPNOTSUPP;
2301                 goto undo_alloc;
2302         }
2303         mp_rebuilt = true;
2304         /* Generate the mapping pairs array directly into the attr record. */
2305         err = ntfs_mapping_pairs_build(vol, (u8*)a +
2306                         le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2307                         mp_size, rl2, ll, -1, NULL);
2308         if (unlikely(err)) {
2309                 if (start < 0 || start >= allocated_size)
2310                         ntfs_error(vol->sb, "Cannot extend allocation of "
2311                                         "inode 0x%lx, attribute type 0x%x, "
2312                                         "because building the mapping pairs "
2313                                         "failed with error code %i.", vi->i_ino,
2314                                         (unsigned)le32_to_cpu(ni->type), err);
2315                 err = -EIO;
2316                 goto undo_alloc;
2317         }
2318         /* Update the highest_vcn. */
2319         a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2320                         vol->cluster_size_bits) - 1);
2321         /*
2322          * We now have extended the allocated size of the attribute.  Reflect
2323          * this in the ntfs_inode structure and the attribute record.
2324          */
2325         if (a->data.non_resident.lowest_vcn) {
2326                 /*
2327                  * We are not in the first attribute extent, switch to it, but
2328                  * first ensure the changes will make it to disk later.
2329                  */
2330                 flush_dcache_mft_record_page(ctx->ntfs_ino);
2331                 mark_mft_record_dirty(ctx->ntfs_ino);
2332                 ntfs_attr_reinit_search_ctx(ctx);
2333                 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2334                                 CASE_SENSITIVE, 0, NULL, 0, ctx);
2335                 if (unlikely(err))
2336                         goto restore_undo_alloc;
2337                 /* @m is not used any more so no need to set it. */
2338                 a = ctx->attr;
2339         }
2340         write_lock_irqsave(&ni->size_lock, flags);
2341         ni->allocated_size = new_alloc_size;
2342         a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2343         /*
2344          * FIXME: This would fail if @ni is a directory, $MFT, or an index,
2345          * since those can have sparse/compressed set.  For example can be
2346          * set compressed even though it is not compressed itself and in that
2347          * case the bit means that files are to be created compressed in the
2348          * directory...  At present this is ok as this code is only called for
2349          * regular files, and only for their $DATA attribute(s).
2350          * FIXME: The calculation is wrong if we created a hole above.  For now
2351          * it does not matter as we never create holes.
2352          */
2353         if (NInoSparse(ni) || NInoCompressed(ni)) {
2354                 ni->itype.compressed.size += new_alloc_size - allocated_size;
2355                 a->data.non_resident.compressed_size =
2356                                 cpu_to_sle64(ni->itype.compressed.size);
2357                 vi->i_blocks = ni->itype.compressed.size >> 9;
2358         } else
2359                 vi->i_blocks = new_alloc_size >> 9;
2360         write_unlock_irqrestore(&ni->size_lock, flags);
2361 alloc_done:
2362         if (new_data_size >= 0) {
2363                 BUG_ON(new_data_size <
2364                                 sle64_to_cpu(a->data.non_resident.data_size));
2365                 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2366         }
2367 flush_done:
2368         /* Ensure the changes make it to disk. */
2369         flush_dcache_mft_record_page(ctx->ntfs_ino);
2370         mark_mft_record_dirty(ctx->ntfs_ino);
2371 done:
2372         ntfs_attr_put_search_ctx(ctx);
2373         unmap_mft_record(base_ni);
2374         up_write(&ni->runlist.lock);
2375         ntfs_debug("Done, new_allocated_size 0x%llx.",
2376                         (unsigned long long)new_alloc_size);
2377         return new_alloc_size;
2378 restore_undo_alloc:
2379         if (start < 0 || start >= allocated_size)
2380                 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2381                                 "of inode 0x%lx, attribute type 0x%x, because "
2382                                 "lookup of first attribute extent failed with "
2383                                 "error code %i.", vi->i_ino,
2384                                 (unsigned)le32_to_cpu(ni->type), err);
2385         if (err == -ENOENT)
2386                 err = -EIO;
2387         ntfs_attr_reinit_search_ctx(ctx);
2388         if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2389                         allocated_size >> vol->cluster_size_bits, NULL, 0,
2390                         ctx)) {
2391                 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2392                                 "attribute in error code path.  Run chkdsk to "
2393                                 "recover.");
2394                 write_lock_irqsave(&ni->size_lock, flags);
2395                 ni->allocated_size = new_alloc_size;
2396                 /*
2397                  * FIXME: This would fail if @ni is a directory...  See above.
2398                  * FIXME: The calculation is wrong if we created a hole above.
2399                  * For now it does not matter as we never create holes.
2400                  */
2401                 if (NInoSparse(ni) || NInoCompressed(ni)) {
2402                         ni->itype.compressed.size += new_alloc_size -
2403                                         allocated_size;
2404                         vi->i_blocks = ni->itype.compressed.size >> 9;
2405                 } else
2406                         vi->i_blocks = new_alloc_size >> 9;
2407                 write_unlock_irqrestore(&ni->size_lock, flags);
2408                 ntfs_attr_put_search_ctx(ctx);
2409                 unmap_mft_record(base_ni);
2410                 up_write(&ni->runlist.lock);
2411                 /*
2412                  * The only thing that is now wrong is the allocated size of the
2413                  * base attribute extent which chkdsk should be able to fix.
2414                  */
2415                 NVolSetErrors(vol);
2416                 return err;
2417         }
2418         ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2419                         (allocated_size >> vol->cluster_size_bits) - 1);
2420 undo_alloc:
2421         ll = allocated_size >> vol->cluster_size_bits;
2422         if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2423                 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2424                                 "in error code path.  Run chkdsk to recover "
2425                                 "the lost cluster(s).");
2426                 NVolSetErrors(vol);
2427         }
2428         m = ctx->mrec;
2429         a = ctx->attr;
2430         /*
2431          * If the runlist truncation fails and/or the search context is no
2432          * longer valid, we cannot resize the attribute record or build the
2433          * mapping pairs array thus we mark the inode bad so that no access to
2434          * the freed clusters can happen.
2435          */
2436         if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2437                 ntfs_error(vol->sb, "Failed to %s in error code path.  Run "
2438                                 "chkdsk to recover.", IS_ERR(m) ?
2439                                 "restore attribute search context" :
2440                                 "truncate attribute runlist");
2441                 NVolSetErrors(vol);
2442         } else if (mp_rebuilt) {
2443                 if (ntfs_attr_record_resize(m, a, attr_len)) {
2444                         ntfs_error(vol->sb, "Failed to restore attribute "
2445                                         "record in error code path.  Run "
2446                                         "chkdsk to recover.");
2447                         NVolSetErrors(vol);
2448                 } else /* if (success) */ {
2449                         if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2450                                         a->data.non_resident.
2451                                         mapping_pairs_offset), attr_len -
2452                                         le16_to_cpu(a->data.non_resident.
2453                                         mapping_pairs_offset), rl2, ll, -1,
2454                                         NULL)) {
2455                                 ntfs_error(vol->sb, "Failed to restore "
2456                                                 "mapping pairs array in error "
2457                                                 "code path.  Run chkdsk to "
2458                                                 "recover.");
2459                                 NVolSetErrors(vol);
2460                         }
2461                         flush_dcache_mft_record_page(ctx->ntfs_ino);
2462                         mark_mft_record_dirty(ctx->ntfs_ino);
2463                 }
2464         }
2465 err_out:
2466         if (ctx)
2467                 ntfs_attr_put_search_ctx(ctx);
2468         if (m)
2469                 unmap_mft_record(base_ni);
2470         up_write(&ni->runlist.lock);
2471 conv_err_out:
2472         ntfs_debug("Failed.  Returning error code %i.", err);
2473         return err;
2474 }
2475
2476 /**
2477  * ntfs_attr_set - fill (a part of) an attribute with a byte
2478  * @ni:         ntfs inode describing the attribute to fill
2479  * @ofs:        offset inside the attribute at which to start to fill
2480  * @cnt:        number of bytes to fill
2481  * @val:        the unsigned 8-bit value with which to fill the attribute
2482  *
2483  * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
2484  * byte offset @ofs inside the attribute with the constant byte @val.
2485  *
2486  * This function is effectively like memset() applied to an ntfs attribute.
2487  * Note thie function actually only operates on the page cache pages belonging
2488  * to the ntfs attribute and it marks them dirty after doing the memset().
2489  * Thus it relies on the vm dirty page write code paths to cause the modified
2490  * pages to be written to the mft record/disk.
2491  *
2492  * Return 0 on success and -errno on error.  An error code of -ESPIPE means
2493  * that @ofs + @cnt were outside the end of the attribute and no write was
2494  * performed.
2495  */
2496 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2497 {
2498         ntfs_volume *vol = ni->vol;
2499         struct address_space *mapping;
2500         struct page *page;
2501         u8 *kaddr;
2502         pgoff_t idx, end;
2503         unsigned int start_ofs, end_ofs, size;
2504
2505         ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2506                         (long long)ofs, (long long)cnt, val);
2507         BUG_ON(ofs < 0);
2508         BUG_ON(cnt < 0);
2509         if (!cnt)
2510                 goto done;
2511         /*
2512          * FIXME: Compressed and encrypted attributes are not supported when
2513          * writing and we should never have gotten here for them.
2514          */
2515         BUG_ON(NInoCompressed(ni));
2516         BUG_ON(NInoEncrypted(ni));
2517         mapping = VFS_I(ni)->i_mapping;
2518         /* Work out the starting index and page offset. */
2519         idx = ofs >> PAGE_CACHE_SHIFT;
2520         start_ofs = ofs & ~PAGE_CACHE_MASK;
2521         /* Work out the ending index and page offset. */
2522         end = ofs + cnt;
2523         end_ofs = end & ~PAGE_CACHE_MASK;
2524         /* If the end is outside the inode size return -ESPIPE. */
2525         if (unlikely(end > i_size_read(VFS_I(ni)))) {
2526                 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2527                 return -ESPIPE;
2528         }
2529         end >>= PAGE_CACHE_SHIFT;
2530         /* If there is a first partial page, need to do it the slow way. */
2531         if (start_ofs) {
2532                 page = read_mapping_page(mapping, idx, NULL);
2533                 if (IS_ERR(page)) {
2534                         ntfs_error(vol->sb, "Failed to read first partial "
2535                                         "page (sync error, index 0x%lx).", idx);
2536                         return PTR_ERR(page);
2537                 }
2538                 wait_on_page_locked(page);
2539                 if (unlikely(!PageUptodate(page))) {
2540                         ntfs_error(vol->sb, "Failed to read first partial page "
2541                                         "(async error, index 0x%lx).", idx);
2542                         page_cache_release(page);
2543                         return PTR_ERR(page);
2544                 }
2545                 /*
2546                  * If the last page is the same as the first page, need to
2547                  * limit the write to the end offset.
2548                  */
2549                 size = PAGE_CACHE_SIZE;
2550                 if (idx == end)
2551                         size = end_ofs;
2552                 kaddr = kmap_atomic(page, KM_USER0);
2553                 memset(kaddr + start_ofs, val, size - start_ofs);
2554                 flush_dcache_page(page);
2555                 kunmap_atomic(kaddr, KM_USER0);
2556                 set_page_dirty(page);
2557                 page_cache_release(page);
2558                 if (idx == end)
2559                         goto done;
2560                 idx++;
2561         }
2562         /* Do the whole pages the fast way. */
2563         for (; idx < end; idx++) {
2564                 /* Find or create the current page.  (The page is locked.) */
2565                 page = grab_cache_page(mapping, idx);
2566                 if (unlikely(!page)) {
2567                         ntfs_error(vol->sb, "Insufficient memory to grab "
2568                                         "page (index 0x%lx).", idx);
2569                         return -ENOMEM;
2570                 }
2571                 kaddr = kmap_atomic(page, KM_USER0);
2572                 memset(kaddr, val, PAGE_CACHE_SIZE);
2573                 flush_dcache_page(page);
2574                 kunmap_atomic(kaddr, KM_USER0);
2575                 /*
2576                  * If the page has buffers, mark them uptodate since buffer
2577                  * state and not page state is definitive in 2.6 kernels.
2578                  */
2579                 if (page_has_buffers(page)) {
2580                         struct buffer_head *bh, *head;
2581
2582                         bh = head = page_buffers(page);
2583                         do {
2584                                 set_buffer_uptodate(bh);
2585                         } while ((bh = bh->b_this_page) != head);
2586                 }
2587                 /* Now that buffers are uptodate, set the page uptodate, too. */
2588                 SetPageUptodate(page);
2589                 /*
2590                  * Set the page and all its buffers dirty and mark the inode
2591                  * dirty, too.  The VM will write the page later on.
2592                  */
2593                 set_page_dirty(page);
2594                 /* Finally unlock and release the page. */
2595                 unlock_page(page);
2596                 page_cache_release(page);
2597                 balance_dirty_pages_ratelimited(mapping);
2598                 cond_resched();
2599         }
2600         /* If there is a last partial page, need to do it the slow way. */
2601         if (end_ofs) {
2602                 page = read_mapping_page(mapping, idx, NULL);
2603                 if (IS_ERR(page)) {
2604                         ntfs_error(vol->sb, "Failed to read last partial page "
2605                                         "(sync error, index 0x%lx).", idx);
2606                         return PTR_ERR(page);
2607                 }
2608                 wait_on_page_locked(page);
2609                 if (unlikely(!PageUptodate(page))) {
2610                         ntfs_error(vol->sb, "Failed to read last partial page "
2611                                         "(async error, index 0x%lx).", idx);
2612                         page_cache_release(page);
2613                         return PTR_ERR(page);
2614                 }
2615                 kaddr = kmap_atomic(page, KM_USER0);
2616                 memset(kaddr, val, end_ofs);
2617                 flush_dcache_page(page);
2618                 kunmap_atomic(kaddr, KM_USER0);
2619                 set_page_dirty(page);
2620                 page_cache_release(page);
2621         }
2622 done:
2623         ntfs_debug("Done.");
2624         return 0;
2625 }
2626
2627 #endif /* NTFS_RW */