/spare/repo/netdev-2.6 branch 'ieee80211'
[pandora-kernel.git] / fs / ntfs / attrib.c
1 /**
2  * attrib.c - NTFS attribute operations.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2005 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/swap.h>
25
26 #include "attrib.h"
27 #include "debug.h"
28 #include "layout.h"
29 #include "lcnalloc.h"
30 #include "malloc.h"
31 #include "mft.h"
32 #include "ntfs.h"
33 #include "types.h"
34
35 /**
36  * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
37  * @ni:         ntfs inode for which to map (part of) a runlist
38  * @vcn:        map runlist part containing this vcn
39  *
40  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
41  *
42  * Return 0 on success and -errno on error.  There is one special error code
43  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
44  * of bounds of the runlist.
45  *
46  * Locking: - The runlist must be locked for writing.
47  *          - This function modifies the runlist.
48  */
49 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn)
50 {
51         VCN end_vcn;
52         ntfs_inode *base_ni;
53         MFT_RECORD *m;
54         ATTR_RECORD *a;
55         ntfs_attr_search_ctx *ctx;
56         runlist_element *rl;
57         int err = 0;
58
59         ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
60                         (unsigned long long)vcn);
61         if (!NInoAttr(ni))
62                 base_ni = ni;
63         else
64                 base_ni = ni->ext.base_ntfs_ino;
65         m = map_mft_record(base_ni);
66         if (IS_ERR(m))
67                 return PTR_ERR(m);
68         ctx = ntfs_attr_get_search_ctx(base_ni, m);
69         if (unlikely(!ctx)) {
70                 err = -ENOMEM;
71                 goto err_out;
72         }
73         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
74                         CASE_SENSITIVE, vcn, NULL, 0, ctx);
75         if (unlikely(err)) {
76                 if (err == -ENOENT)
77                         err = -EIO;
78                 goto err_out;
79         }
80         a = ctx->attr;
81         /*
82          * Only decompress the mapping pairs if @vcn is inside it.  Otherwise
83          * we get into problems when we try to map an out of bounds vcn because
84          * we then try to map the already mapped runlist fragment and
85          * ntfs_mapping_pairs_decompress() fails.
86          */
87         end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
88         if (unlikely(!a->data.non_resident.lowest_vcn && end_vcn <= 1))
89                 end_vcn = ni->allocated_size >> ni->vol->cluster_size_bits;
90         if (unlikely(vcn >= end_vcn)) {
91                 err = -ENOENT;
92                 goto err_out;
93         }
94         rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
95         if (IS_ERR(rl))
96                 err = PTR_ERR(rl);
97         else
98                 ni->runlist.rl = rl;
99 err_out:
100         if (likely(ctx))
101                 ntfs_attr_put_search_ctx(ctx);
102         unmap_mft_record(base_ni);
103         return err;
104 }
105
106 /**
107  * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
108  * @ni:         ntfs inode for which to map (part of) a runlist
109  * @vcn:        map runlist part containing this vcn
110  *
111  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
112  *
113  * Return 0 on success and -errno on error.  There is one special error code
114  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
115  * of bounds of the runlist.
116  *
117  * Locking: - The runlist must be unlocked on entry and is unlocked on return.
118  *          - This function takes the runlist lock for writing and modifies the
119  *            runlist.
120  */
121 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
122 {
123         int err = 0;
124
125         down_write(&ni->runlist.lock);
126         /* Make sure someone else didn't do the work while we were sleeping. */
127         if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
128                         LCN_RL_NOT_MAPPED))
129                 err = ntfs_map_runlist_nolock(ni, vcn);
130         up_write(&ni->runlist.lock);
131         return err;
132 }
133
134 /**
135  * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
136  * @ni:                 ntfs inode of the attribute whose runlist to search
137  * @vcn:                vcn to convert
138  * @write_locked:       true if the runlist is locked for writing
139  *
140  * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
141  * described by the ntfs inode @ni and return the corresponding logical cluster
142  * number (lcn).
143  *
144  * If the @vcn is not mapped yet, the attempt is made to map the attribute
145  * extent containing the @vcn and the vcn to lcn conversion is retried.
146  *
147  * If @write_locked is true the caller has locked the runlist for writing and
148  * if false for reading.
149  *
150  * Since lcns must be >= 0, we use negative return codes with special meaning:
151  *
152  * Return code  Meaning / Description
153  * ==========================================
154  *  LCN_HOLE    Hole / not allocated on disk.
155  *  LCN_ENOENT  There is no such vcn in the runlist, i.e. @vcn is out of bounds.
156  *  LCN_ENOMEM  Not enough memory to map runlist.
157  *  LCN_EIO     Critical error (runlist/file is corrupt, i/o error, etc).
158  *
159  * Locking: - The runlist must be locked on entry and is left locked on return.
160  *          - If @write_locked is FALSE, i.e. the runlist is locked for reading,
161  *            the lock may be dropped inside the function so you cannot rely on
162  *            the runlist still being the same when this function returns.
163  */
164 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
165                 const BOOL write_locked)
166 {
167         LCN lcn;
168         BOOL is_retry = FALSE;
169
170         ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
171                         ni->mft_no, (unsigned long long)vcn,
172                         write_locked ? "write" : "read");
173         BUG_ON(!ni);
174         BUG_ON(!NInoNonResident(ni));
175         BUG_ON(vcn < 0);
176 retry_remap:
177         /* Convert vcn to lcn.  If that fails map the runlist and retry once. */
178         lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
179         if (likely(lcn >= LCN_HOLE)) {
180                 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
181                 return lcn;
182         }
183         if (lcn != LCN_RL_NOT_MAPPED) {
184                 if (lcn != LCN_ENOENT)
185                         lcn = LCN_EIO;
186         } else if (!is_retry) {
187                 int err;
188
189                 if (!write_locked) {
190                         up_read(&ni->runlist.lock);
191                         down_write(&ni->runlist.lock);
192                         if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
193                                         LCN_RL_NOT_MAPPED)) {
194                                 up_write(&ni->runlist.lock);
195                                 down_read(&ni->runlist.lock);
196                                 goto retry_remap;
197                         }
198                 }
199                 err = ntfs_map_runlist_nolock(ni, vcn);
200                 if (!write_locked) {
201                         up_write(&ni->runlist.lock);
202                         down_read(&ni->runlist.lock);
203                 }
204                 if (likely(!err)) {
205                         is_retry = TRUE;
206                         goto retry_remap;
207                 }
208                 if (err == -ENOENT)
209                         lcn = LCN_ENOENT;
210                 else if (err == -ENOMEM)
211                         lcn = LCN_ENOMEM;
212                 else
213                         lcn = LCN_EIO;
214         }
215         if (lcn != LCN_ENOENT)
216                 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
217                                 (long long)lcn);
218         return lcn;
219 }
220
221 /**
222  * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
223  * @ni:                 ntfs inode describing the runlist to search
224  * @vcn:                vcn to find
225  * @write_locked:       true if the runlist is locked for writing
226  *
227  * Find the virtual cluster number @vcn in the runlist described by the ntfs
228  * inode @ni and return the address of the runlist element containing the @vcn.
229  *
230  * If the @vcn is not mapped yet, the attempt is made to map the attribute
231  * extent containing the @vcn and the vcn to lcn conversion is retried.
232  *
233  * If @write_locked is true the caller has locked the runlist for writing and
234  * if false for reading.
235  *
236  * Note you need to distinguish between the lcn of the returned runlist element
237  * being >= 0 and LCN_HOLE.  In the later case you have to return zeroes on
238  * read and allocate clusters on write.
239  *
240  * Return the runlist element containing the @vcn on success and
241  * ERR_PTR(-errno) on error.  You need to test the return value with IS_ERR()
242  * to decide if the return is success or failure and PTR_ERR() to get to the
243  * error code if IS_ERR() is true.
244  *
245  * The possible error return codes are:
246  *      -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
247  *      -ENOMEM - Not enough memory to map runlist.
248  *      -EIO    - Critical error (runlist/file is corrupt, i/o error, etc).
249  *
250  * Locking: - The runlist must be locked on entry and is left locked on return.
251  *          - If @write_locked is FALSE, i.e. the runlist is locked for reading,
252  *            the lock may be dropped inside the function so you cannot rely on
253  *            the runlist still being the same when this function returns.
254  */
255 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
256                 const BOOL write_locked)
257 {
258         runlist_element *rl;
259         int err = 0;
260         BOOL is_retry = FALSE;
261
262         ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
263                         ni->mft_no, (unsigned long long)vcn,
264                         write_locked ? "write" : "read");
265         BUG_ON(!ni);
266         BUG_ON(!NInoNonResident(ni));
267         BUG_ON(vcn < 0);
268 retry_remap:
269         rl = ni->runlist.rl;
270         if (likely(rl && vcn >= rl[0].vcn)) {
271                 while (likely(rl->length)) {
272                         if (unlikely(vcn < rl[1].vcn)) {
273                                 if (likely(rl->lcn >= LCN_HOLE)) {
274                                         ntfs_debug("Done.");
275                                         return rl;
276                                 }
277                                 break;
278                         }
279                         rl++;
280                 }
281                 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
282                         if (likely(rl->lcn == LCN_ENOENT))
283                                 err = -ENOENT;
284                         else
285                                 err = -EIO;
286                 }
287         }
288         if (!err && !is_retry) {
289                 /*
290                  * The @vcn is in an unmapped region, map the runlist and
291                  * retry.
292                  */
293                 if (!write_locked) {
294                         up_read(&ni->runlist.lock);
295                         down_write(&ni->runlist.lock);
296                         if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
297                                         LCN_RL_NOT_MAPPED)) {
298                                 up_write(&ni->runlist.lock);
299                                 down_read(&ni->runlist.lock);
300                                 goto retry_remap;
301                         }
302                 }
303                 err = ntfs_map_runlist_nolock(ni, vcn);
304                 if (!write_locked) {
305                         up_write(&ni->runlist.lock);
306                         down_read(&ni->runlist.lock);
307                 }
308                 if (likely(!err)) {
309                         is_retry = TRUE;
310                         goto retry_remap;
311                 }
312                 /*
313                  * -EINVAL coming from a failed mapping attempt is equivalent
314                  * to i/o error for us as it should not happen in our code
315                  * paths.
316                  */
317                 if (err == -EINVAL)
318                         err = -EIO;
319         } else if (!err)
320                 err = -EIO;
321         if (err != -ENOENT)
322                 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
323         return ERR_PTR(err);
324 }
325
326 /**
327  * ntfs_attr_find - find (next) attribute in mft record
328  * @type:       attribute type to find
329  * @name:       attribute name to find (optional, i.e. NULL means don't care)
330  * @name_len:   attribute name length (only needed if @name present)
331  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
332  * @val:        attribute value to find (optional, resident attributes only)
333  * @val_len:    attribute value length
334  * @ctx:        search context with mft record and attribute to search from
335  *
336  * You should not need to call this function directly.  Use ntfs_attr_lookup()
337  * instead.
338  *
339  * ntfs_attr_find() takes a search context @ctx as parameter and searches the
340  * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
341  * attribute of @type, optionally @name and @val.
342  *
343  * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
344  * point to the found attribute.
345  *
346  * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
347  * @ctx->attr will point to the attribute before which the attribute being
348  * searched for would need to be inserted if such an action were to be desired.
349  *
350  * On actual error, ntfs_attr_find() returns -EIO.  In this case @ctx->attr is
351  * undefined and in particular do not rely on it not changing.
352  *
353  * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself.  If it
354  * is FALSE, the search begins after @ctx->attr.
355  *
356  * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
357  * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
358  * @ctx->mrec belongs.  This is so we can get at the ntfs volume and hence at
359  * the upcase table.  If @ic is CASE_SENSITIVE, the comparison is case
360  * sensitive.  When @name is present, @name_len is the @name length in Unicode
361  * characters.
362  *
363  * If @name is not present (NULL), we assume that the unnamed attribute is
364  * being searched for.
365  *
366  * Finally, the resident attribute value @val is looked for, if present.  If
367  * @val is not present (NULL), @val_len is ignored.
368  *
369  * ntfs_attr_find() only searches the specified mft record and it ignores the
370  * presence of an attribute list attribute (unless it is the one being searched
371  * for, obviously).  If you need to take attribute lists into consideration,
372  * use ntfs_attr_lookup() instead (see below).  This also means that you cannot
373  * use ntfs_attr_find() to search for extent records of non-resident
374  * attributes, as extents with lowest_vcn != 0 are usually described by the
375  * attribute list attribute only. - Note that it is possible that the first
376  * extent is only in the attribute list while the last extent is in the base
377  * mft record, so do not rely on being able to find the first extent in the
378  * base mft record.
379  *
380  * Warning: Never use @val when looking for attribute types which can be
381  *          non-resident as this most likely will result in a crash!
382  */
383 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
384                 const u32 name_len, const IGNORE_CASE_BOOL ic,
385                 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
386 {
387         ATTR_RECORD *a;
388         ntfs_volume *vol = ctx->ntfs_ino->vol;
389         ntfschar *upcase = vol->upcase;
390         u32 upcase_len = vol->upcase_len;
391
392         /*
393          * Iterate over attributes in mft record starting at @ctx->attr, or the
394          * attribute following that, if @ctx->is_first is TRUE.
395          */
396         if (ctx->is_first) {
397                 a = ctx->attr;
398                 ctx->is_first = FALSE;
399         } else
400                 a = (ATTR_RECORD*)((u8*)ctx->attr +
401                                 le32_to_cpu(ctx->attr->length));
402         for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
403                 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
404                                 le32_to_cpu(ctx->mrec->bytes_allocated))
405                         break;
406                 ctx->attr = a;
407                 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
408                                 a->type == AT_END))
409                         return -ENOENT;
410                 if (unlikely(!a->length))
411                         break;
412                 if (a->type != type)
413                         continue;
414                 /*
415                  * If @name is present, compare the two names.  If @name is
416                  * missing, assume we want an unnamed attribute.
417                  */
418                 if (!name) {
419                         /* The search failed if the found attribute is named. */
420                         if (a->name_length)
421                                 return -ENOENT;
422                 } else if (!ntfs_are_names_equal(name, name_len,
423                             (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
424                             a->name_length, ic, upcase, upcase_len)) {
425                         register int rc;
426
427                         rc = ntfs_collate_names(name, name_len,
428                                         (ntfschar*)((u8*)a +
429                                         le16_to_cpu(a->name_offset)),
430                                         a->name_length, 1, IGNORE_CASE,
431                                         upcase, upcase_len);
432                         /*
433                          * If @name collates before a->name, there is no
434                          * matching attribute.
435                          */
436                         if (rc == -1)
437                                 return -ENOENT;
438                         /* If the strings are not equal, continue search. */
439                         if (rc)
440                                 continue;
441                         rc = ntfs_collate_names(name, name_len,
442                                         (ntfschar*)((u8*)a +
443                                         le16_to_cpu(a->name_offset)),
444                                         a->name_length, 1, CASE_SENSITIVE,
445                                         upcase, upcase_len);
446                         if (rc == -1)
447                                 return -ENOENT;
448                         if (rc)
449                                 continue;
450                 }
451                 /*
452                  * The names match or @name not present and attribute is
453                  * unnamed.  If no @val specified, we have found the attribute
454                  * and are done.
455                  */
456                 if (!val)
457                         return 0;
458                 /* @val is present; compare values. */
459                 else {
460                         register int rc;
461
462                         rc = memcmp(val, (u8*)a + le16_to_cpu(
463                                         a->data.resident.value_offset),
464                                         min_t(u32, val_len, le32_to_cpu(
465                                         a->data.resident.value_length)));
466                         /*
467                          * If @val collates before the current attribute's
468                          * value, there is no matching attribute.
469                          */
470                         if (!rc) {
471                                 register u32 avl;
472
473                                 avl = le32_to_cpu(
474                                                 a->data.resident.value_length);
475                                 if (val_len == avl)
476                                         return 0;
477                                 if (val_len < avl)
478                                         return -ENOENT;
479                         } else if (rc < 0)
480                                 return -ENOENT;
481                 }
482         }
483         ntfs_error(vol->sb, "Inode is corrupt.  Run chkdsk.");
484         NVolSetErrors(vol);
485         return -EIO;
486 }
487
488 /**
489  * load_attribute_list - load an attribute list into memory
490  * @vol:                ntfs volume from which to read
491  * @runlist:            runlist of the attribute list
492  * @al_start:           destination buffer
493  * @size:               size of the destination buffer in bytes
494  * @initialized_size:   initialized size of the attribute list
495  *
496  * Walk the runlist @runlist and load all clusters from it copying them into
497  * the linear buffer @al. The maximum number of bytes copied to @al is @size
498  * bytes. Note, @size does not need to be a multiple of the cluster size. If
499  * @initialized_size is less than @size, the region in @al between
500  * @initialized_size and @size will be zeroed and not read from disk.
501  *
502  * Return 0 on success or -errno on error.
503  */
504 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
505                 const s64 size, const s64 initialized_size)
506 {
507         LCN lcn;
508         u8 *al = al_start;
509         u8 *al_end = al + initialized_size;
510         runlist_element *rl;
511         struct buffer_head *bh;
512         struct super_block *sb;
513         unsigned long block_size;
514         unsigned long block, max_block;
515         int err = 0;
516         unsigned char block_size_bits;
517
518         ntfs_debug("Entering.");
519         if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
520                         initialized_size > size)
521                 return -EINVAL;
522         if (!initialized_size) {
523                 memset(al, 0, size);
524                 return 0;
525         }
526         sb = vol->sb;
527         block_size = sb->s_blocksize;
528         block_size_bits = sb->s_blocksize_bits;
529         down_read(&runlist->lock);
530         rl = runlist->rl;
531         /* Read all clusters specified by the runlist one run at a time. */
532         while (rl->length) {
533                 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
534                 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
535                                 (unsigned long long)rl->vcn,
536                                 (unsigned long long)lcn);
537                 /* The attribute list cannot be sparse. */
538                 if (lcn < 0) {
539                         ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed.  Cannot "
540                                         "read attribute list.");
541                         goto err_out;
542                 }
543                 block = lcn << vol->cluster_size_bits >> block_size_bits;
544                 /* Read the run from device in chunks of block_size bytes. */
545                 max_block = block + (rl->length << vol->cluster_size_bits >>
546                                 block_size_bits);
547                 ntfs_debug("max_block = 0x%lx.", max_block);
548                 do {
549                         ntfs_debug("Reading block = 0x%lx.", block);
550                         bh = sb_bread(sb, block);
551                         if (!bh) {
552                                 ntfs_error(sb, "sb_bread() failed. Cannot "
553                                                 "read attribute list.");
554                                 goto err_out;
555                         }
556                         if (al + block_size >= al_end)
557                                 goto do_final;
558                         memcpy(al, bh->b_data, block_size);
559                         brelse(bh);
560                         al += block_size;
561                 } while (++block < max_block);
562                 rl++;
563         }
564         if (initialized_size < size) {
565 initialize:
566                 memset(al_start + initialized_size, 0, size - initialized_size);
567         }
568 done:
569         up_read(&runlist->lock);
570         return err;
571 do_final:
572         if (al < al_end) {
573                 /*
574                  * Partial block.
575                  *
576                  * Note: The attribute list can be smaller than its allocation
577                  * by multiple clusters.  This has been encountered by at least
578                  * two people running Windows XP, thus we cannot do any
579                  * truncation sanity checking here. (AIA)
580                  */
581                 memcpy(al, bh->b_data, al_end - al);
582                 brelse(bh);
583                 if (initialized_size < size)
584                         goto initialize;
585                 goto done;
586         }
587         brelse(bh);
588         /* Real overflow! */
589         ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
590                         "is truncated.");
591 err_out:
592         err = -EIO;
593         goto done;
594 }
595
596 /**
597  * ntfs_external_attr_find - find an attribute in the attribute list of an inode
598  * @type:       attribute type to find
599  * @name:       attribute name to find (optional, i.e. NULL means don't care)
600  * @name_len:   attribute name length (only needed if @name present)
601  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
602  * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
603  * @val:        attribute value to find (optional, resident attributes only)
604  * @val_len:    attribute value length
605  * @ctx:        search context with mft record and attribute to search from
606  *
607  * You should not need to call this function directly.  Use ntfs_attr_lookup()
608  * instead.
609  *
610  * Find an attribute by searching the attribute list for the corresponding
611  * attribute list entry.  Having found the entry, map the mft record if the
612  * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
613  * in there and return it.
614  *
615  * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
616  * have been obtained from a call to ntfs_attr_get_search_ctx().  On subsequent
617  * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
618  * then the base inode).
619  *
620  * After finishing with the attribute/mft record you need to call
621  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
622  * mapped inodes, etc).
623  *
624  * If the attribute is found, ntfs_external_attr_find() returns 0 and
625  * @ctx->attr will point to the found attribute.  @ctx->mrec will point to the
626  * mft record in which @ctx->attr is located and @ctx->al_entry will point to
627  * the attribute list entry for the attribute.
628  *
629  * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
630  * @ctx->attr will point to the attribute in the base mft record before which
631  * the attribute being searched for would need to be inserted if such an action
632  * were to be desired.  @ctx->mrec will point to the mft record in which
633  * @ctx->attr is located and @ctx->al_entry will point to the attribute list
634  * entry of the attribute before which the attribute being searched for would
635  * need to be inserted if such an action were to be desired.
636  *
637  * Thus to insert the not found attribute, one wants to add the attribute to
638  * @ctx->mrec (the base mft record) and if there is not enough space, the
639  * attribute should be placed in a newly allocated extent mft record.  The
640  * attribute list entry for the inserted attribute should be inserted in the
641  * attribute list attribute at @ctx->al_entry.
642  *
643  * On actual error, ntfs_external_attr_find() returns -EIO.  In this case
644  * @ctx->attr is undefined and in particular do not rely on it not changing.
645  */
646 static int ntfs_external_attr_find(const ATTR_TYPE type,
647                 const ntfschar *name, const u32 name_len,
648                 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
649                 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
650 {
651         ntfs_inode *base_ni, *ni;
652         ntfs_volume *vol;
653         ATTR_LIST_ENTRY *al_entry, *next_al_entry;
654         u8 *al_start, *al_end;
655         ATTR_RECORD *a;
656         ntfschar *al_name;
657         u32 al_name_len;
658         int err = 0;
659         static const char *es = " Unmount and run chkdsk.";
660
661         ni = ctx->ntfs_ino;
662         base_ni = ctx->base_ntfs_ino;
663         ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
664         if (!base_ni) {
665                 /* First call happens with the base mft record. */
666                 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
667                 ctx->base_mrec = ctx->mrec;
668         }
669         if (ni == base_ni)
670                 ctx->base_attr = ctx->attr;
671         if (type == AT_END)
672                 goto not_found;
673         vol = base_ni->vol;
674         al_start = base_ni->attr_list;
675         al_end = al_start + base_ni->attr_list_size;
676         if (!ctx->al_entry)
677                 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
678         /*
679          * Iterate over entries in attribute list starting at @ctx->al_entry,
680          * or the entry following that, if @ctx->is_first is TRUE.
681          */
682         if (ctx->is_first) {
683                 al_entry = ctx->al_entry;
684                 ctx->is_first = FALSE;
685         } else
686                 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
687                                 le16_to_cpu(ctx->al_entry->length));
688         for (;; al_entry = next_al_entry) {
689                 /* Out of bounds check. */
690                 if ((u8*)al_entry < base_ni->attr_list ||
691                                 (u8*)al_entry > al_end)
692                         break;  /* Inode is corrupt. */
693                 ctx->al_entry = al_entry;
694                 /* Catch the end of the attribute list. */
695                 if ((u8*)al_entry == al_end)
696                         goto not_found;
697                 if (!al_entry->length)
698                         break;
699                 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
700                                 le16_to_cpu(al_entry->length) > al_end)
701                         break;
702                 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
703                                 le16_to_cpu(al_entry->length));
704                 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
705                         goto not_found;
706                 if (type != al_entry->type)
707                         continue;
708                 /*
709                  * If @name is present, compare the two names.  If @name is
710                  * missing, assume we want an unnamed attribute.
711                  */
712                 al_name_len = al_entry->name_length;
713                 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
714                 if (!name) {
715                         if (al_name_len)
716                                 goto not_found;
717                 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
718                                 name_len, ic, vol->upcase, vol->upcase_len)) {
719                         register int rc;
720
721                         rc = ntfs_collate_names(name, name_len, al_name,
722                                         al_name_len, 1, IGNORE_CASE,
723                                         vol->upcase, vol->upcase_len);
724                         /*
725                          * If @name collates before al_name, there is no
726                          * matching attribute.
727                          */
728                         if (rc == -1)
729                                 goto not_found;
730                         /* If the strings are not equal, continue search. */
731                         if (rc)
732                                 continue;
733                         /*
734                          * FIXME: Reverse engineering showed 0, IGNORE_CASE but
735                          * that is inconsistent with ntfs_attr_find().  The
736                          * subsequent rc checks were also different.  Perhaps I
737                          * made a mistake in one of the two.  Need to recheck
738                          * which is correct or at least see what is going on...
739                          * (AIA)
740                          */
741                         rc = ntfs_collate_names(name, name_len, al_name,
742                                         al_name_len, 1, CASE_SENSITIVE,
743                                         vol->upcase, vol->upcase_len);
744                         if (rc == -1)
745                                 goto not_found;
746                         if (rc)
747                                 continue;
748                 }
749                 /*
750                  * The names match or @name not present and attribute is
751                  * unnamed.  Now check @lowest_vcn.  Continue search if the
752                  * next attribute list entry still fits @lowest_vcn.  Otherwise
753                  * we have reached the right one or the search has failed.
754                  */
755                 if (lowest_vcn && (u8*)next_al_entry >= al_start            &&
756                                 (u8*)next_al_entry + 6 < al_end             &&
757                                 (u8*)next_al_entry + le16_to_cpu(
758                                         next_al_entry->length) <= al_end    &&
759                                 sle64_to_cpu(next_al_entry->lowest_vcn) <=
760                                         lowest_vcn                          &&
761                                 next_al_entry->type == al_entry->type       &&
762                                 next_al_entry->name_length == al_name_len   &&
763                                 ntfs_are_names_equal((ntfschar*)((u8*)
764                                         next_al_entry +
765                                         next_al_entry->name_offset),
766                                         next_al_entry->name_length,
767                                         al_name, al_name_len, CASE_SENSITIVE,
768                                         vol->upcase, vol->upcase_len))
769                         continue;
770                 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
771                         if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
772                                 ntfs_error(vol->sb, "Found stale mft "
773                                                 "reference in attribute list "
774                                                 "of base inode 0x%lx.%s",
775                                                 base_ni->mft_no, es);
776                                 err = -EIO;
777                                 break;
778                         }
779                 } else { /* Mft references do not match. */
780                         /* If there is a mapped record unmap it first. */
781                         if (ni != base_ni)
782                                 unmap_extent_mft_record(ni);
783                         /* Do we want the base record back? */
784                         if (MREF_LE(al_entry->mft_reference) ==
785                                         base_ni->mft_no) {
786                                 ni = ctx->ntfs_ino = base_ni;
787                                 ctx->mrec = ctx->base_mrec;
788                         } else {
789                                 /* We want an extent record. */
790                                 ctx->mrec = map_extent_mft_record(base_ni,
791                                                 le64_to_cpu(
792                                                 al_entry->mft_reference), &ni);
793                                 if (IS_ERR(ctx->mrec)) {
794                                         ntfs_error(vol->sb, "Failed to map "
795                                                         "extent mft record "
796                                                         "0x%lx of base inode "
797                                                         "0x%lx.%s",
798                                                         MREF_LE(al_entry->
799                                                         mft_reference),
800                                                         base_ni->mft_no, es);
801                                         err = PTR_ERR(ctx->mrec);
802                                         if (err == -ENOENT)
803                                                 err = -EIO;
804                                         /* Cause @ctx to be sanitized below. */
805                                         ni = NULL;
806                                         break;
807                                 }
808                                 ctx->ntfs_ino = ni;
809                         }
810                         ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
811                                         le16_to_cpu(ctx->mrec->attrs_offset));
812                 }
813                 /*
814                  * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
815                  * mft record containing the attribute represented by the
816                  * current al_entry.
817                  */
818                 /*
819                  * We could call into ntfs_attr_find() to find the right
820                  * attribute in this mft record but this would be less
821                  * efficient and not quite accurate as ntfs_attr_find() ignores
822                  * the attribute instance numbers for example which become
823                  * important when one plays with attribute lists.  Also,
824                  * because a proper match has been found in the attribute list
825                  * entry above, the comparison can now be optimized.  So it is
826                  * worth re-implementing a simplified ntfs_attr_find() here.
827                  */
828                 a = ctx->attr;
829                 /*
830                  * Use a manual loop so we can still use break and continue
831                  * with the same meanings as above.
832                  */
833 do_next_attr_loop:
834                 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
835                                 le32_to_cpu(ctx->mrec->bytes_allocated))
836                         break;
837                 if (a->type == AT_END)
838                         continue;
839                 if (!a->length)
840                         break;
841                 if (al_entry->instance != a->instance)
842                         goto do_next_attr;
843                 /*
844                  * If the type and/or the name are mismatched between the
845                  * attribute list entry and the attribute record, there is
846                  * corruption so we break and return error EIO.
847                  */
848                 if (al_entry->type != a->type)
849                         break;
850                 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
851                                 le16_to_cpu(a->name_offset)), a->name_length,
852                                 al_name, al_name_len, CASE_SENSITIVE,
853                                 vol->upcase, vol->upcase_len))
854                         break;
855                 ctx->attr = a;
856                 /*
857                  * If no @val specified or @val specified and it matches, we
858                  * have found it!
859                  */
860                 if (!val || (!a->non_resident && le32_to_cpu(
861                                 a->data.resident.value_length) == val_len &&
862                                 !memcmp((u8*)a +
863                                 le16_to_cpu(a->data.resident.value_offset),
864                                 val, val_len))) {
865                         ntfs_debug("Done, found.");
866                         return 0;
867                 }
868 do_next_attr:
869                 /* Proceed to the next attribute in the current mft record. */
870                 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
871                 goto do_next_attr_loop;
872         }
873         if (!err) {
874                 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
875                                 "attribute list attribute.%s", base_ni->mft_no,
876                                 es);
877                 err = -EIO;
878         }
879         if (ni != base_ni) {
880                 if (ni)
881                         unmap_extent_mft_record(ni);
882                 ctx->ntfs_ino = base_ni;
883                 ctx->mrec = ctx->base_mrec;
884                 ctx->attr = ctx->base_attr;
885         }
886         if (err != -ENOMEM)
887                 NVolSetErrors(vol);
888         return err;
889 not_found:
890         /*
891          * If we were looking for AT_END, we reset the search context @ctx and
892          * use ntfs_attr_find() to seek to the end of the base mft record.
893          */
894         if (type == AT_END) {
895                 ntfs_attr_reinit_search_ctx(ctx);
896                 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
897                                 ctx);
898         }
899         /*
900          * The attribute was not found.  Before we return, we want to ensure
901          * @ctx->mrec and @ctx->attr indicate the position at which the
902          * attribute should be inserted in the base mft record.  Since we also
903          * want to preserve @ctx->al_entry we cannot reinitialize the search
904          * context using ntfs_attr_reinit_search_ctx() as this would set
905          * @ctx->al_entry to NULL.  Thus we do the necessary bits manually (see
906          * ntfs_attr_init_search_ctx() below).  Note, we _only_ preserve
907          * @ctx->al_entry as the remaining fields (base_*) are identical to
908          * their non base_ counterparts and we cannot set @ctx->base_attr
909          * correctly yet as we do not know what @ctx->attr will be set to by
910          * the call to ntfs_attr_find() below.
911          */
912         if (ni != base_ni)
913                 unmap_extent_mft_record(ni);
914         ctx->mrec = ctx->base_mrec;
915         ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
916                         le16_to_cpu(ctx->mrec->attrs_offset));
917         ctx->is_first = TRUE;
918         ctx->ntfs_ino = base_ni;
919         ctx->base_ntfs_ino = NULL;
920         ctx->base_mrec = NULL;
921         ctx->base_attr = NULL;
922         /*
923          * In case there are multiple matches in the base mft record, need to
924          * keep enumerating until we get an attribute not found response (or
925          * another error), otherwise we would keep returning the same attribute
926          * over and over again and all programs using us for enumeration would
927          * lock up in a tight loop.
928          */
929         do {
930                 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
931                                 ctx);
932         } while (!err);
933         ntfs_debug("Done, not found.");
934         return err;
935 }
936
937 /**
938  * ntfs_attr_lookup - find an attribute in an ntfs inode
939  * @type:       attribute type to find
940  * @name:       attribute name to find (optional, i.e. NULL means don't care)
941  * @name_len:   attribute name length (only needed if @name present)
942  * @ic:         IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
943  * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
944  * @val:        attribute value to find (optional, resident attributes only)
945  * @val_len:    attribute value length
946  * @ctx:        search context with mft record and attribute to search from
947  *
948  * Find an attribute in an ntfs inode.  On first search @ctx->ntfs_ino must
949  * be the base mft record and @ctx must have been obtained from a call to
950  * ntfs_attr_get_search_ctx().
951  *
952  * This function transparently handles attribute lists and @ctx is used to
953  * continue searches where they were left off at.
954  *
955  * After finishing with the attribute/mft record you need to call
956  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
957  * mapped inodes, etc).
958  *
959  * Return 0 if the search was successful and -errno if not.
960  *
961  * When 0, @ctx->attr is the found attribute and it is in mft record
962  * @ctx->mrec.  If an attribute list attribute is present, @ctx->al_entry is
963  * the attribute list entry of the found attribute.
964  *
965  * When -ENOENT, @ctx->attr is the attribute which collates just after the
966  * attribute being searched for, i.e. if one wants to add the attribute to the
967  * mft record this is the correct place to insert it into.  If an attribute
968  * list attribute is present, @ctx->al_entry is the attribute list entry which
969  * collates just after the attribute list entry of the attribute being searched
970  * for, i.e. if one wants to add the attribute to the mft record this is the
971  * correct place to insert its attribute list entry into.
972  *
973  * When -errno != -ENOENT, an error occured during the lookup.  @ctx->attr is
974  * then undefined and in particular you should not rely on it not changing.
975  */
976 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
977                 const u32 name_len, const IGNORE_CASE_BOOL ic,
978                 const VCN lowest_vcn, const u8 *val, const u32 val_len,
979                 ntfs_attr_search_ctx *ctx)
980 {
981         ntfs_inode *base_ni;
982
983         ntfs_debug("Entering.");
984         if (ctx->base_ntfs_ino)
985                 base_ni = ctx->base_ntfs_ino;
986         else
987                 base_ni = ctx->ntfs_ino;
988         /* Sanity check, just for debugging really. */
989         BUG_ON(!base_ni);
990         if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
991                 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
992                                 ctx);
993         return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
994                         val, val_len, ctx);
995 }
996
997 /**
998  * ntfs_attr_init_search_ctx - initialize an attribute search context
999  * @ctx:        attribute search context to initialize
1000  * @ni:         ntfs inode with which to initialize the search context
1001  * @mrec:       mft record with which to initialize the search context
1002  *
1003  * Initialize the attribute search context @ctx with @ni and @mrec.
1004  */
1005 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1006                 ntfs_inode *ni, MFT_RECORD *mrec)
1007 {
1008         *ctx = (ntfs_attr_search_ctx) {
1009                 .mrec = mrec,
1010                 /* Sanity checks are performed elsewhere. */
1011                 .attr = (ATTR_RECORD*)((u8*)mrec +
1012                                 le16_to_cpu(mrec->attrs_offset)),
1013                 .is_first = TRUE,
1014                 .ntfs_ino = ni,
1015         };
1016 }
1017
1018 /**
1019  * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1020  * @ctx:        attribute search context to reinitialize
1021  *
1022  * Reinitialize the attribute search context @ctx, unmapping an associated
1023  * extent mft record if present, and initialize the search context again.
1024  *
1025  * This is used when a search for a new attribute is being started to reset
1026  * the search context to the beginning.
1027  */
1028 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1029 {
1030         if (likely(!ctx->base_ntfs_ino)) {
1031                 /* No attribute list. */
1032                 ctx->is_first = TRUE;
1033                 /* Sanity checks are performed elsewhere. */
1034                 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1035                                 le16_to_cpu(ctx->mrec->attrs_offset));
1036                 /*
1037                  * This needs resetting due to ntfs_external_attr_find() which
1038                  * can leave it set despite having zeroed ctx->base_ntfs_ino.
1039                  */
1040                 ctx->al_entry = NULL;
1041                 return;
1042         } /* Attribute list. */
1043         if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1044                 unmap_extent_mft_record(ctx->ntfs_ino);
1045         ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1046         return;
1047 }
1048
1049 /**
1050  * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1051  * @ni:         ntfs inode with which to initialize the search context
1052  * @mrec:       mft record with which to initialize the search context
1053  *
1054  * Allocate a new attribute search context, initialize it with @ni and @mrec,
1055  * and return it. Return NULL if allocation failed.
1056  */
1057 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1058 {
1059         ntfs_attr_search_ctx *ctx;
1060
1061         ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1062         if (ctx)
1063                 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1064         return ctx;
1065 }
1066
1067 /**
1068  * ntfs_attr_put_search_ctx - release an attribute search context
1069  * @ctx:        attribute search context to free
1070  *
1071  * Release the attribute search context @ctx, unmapping an associated extent
1072  * mft record if present.
1073  */
1074 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1075 {
1076         if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1077                 unmap_extent_mft_record(ctx->ntfs_ino);
1078         kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1079         return;
1080 }
1081
1082 #ifdef NTFS_RW
1083
1084 /**
1085  * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1086  * @vol:        ntfs volume to which the attribute belongs
1087  * @type:       attribute type which to find
1088  *
1089  * Search for the attribute definition record corresponding to the attribute
1090  * @type in the $AttrDef system file.
1091  *
1092  * Return the attribute type definition record if found and NULL if not found.
1093  */
1094 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1095                 const ATTR_TYPE type)
1096 {
1097         ATTR_DEF *ad;
1098
1099         BUG_ON(!vol->attrdef);
1100         BUG_ON(!type);
1101         for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1102                         vol->attrdef_size && ad->type; ++ad) {
1103                 /* We have not found it yet, carry on searching. */
1104                 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1105                         continue;
1106                 /* We found the attribute; return it. */
1107                 if (likely(ad->type == type))
1108                         return ad;
1109                 /* We have gone too far already.  No point in continuing. */
1110                 break;
1111         }
1112         /* Attribute not found. */
1113         ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1114                         le32_to_cpu(type));
1115         return NULL;
1116 }
1117
1118 /**
1119  * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1120  * @vol:        ntfs volume to which the attribute belongs
1121  * @type:       attribute type which to check
1122  * @size:       size which to check
1123  *
1124  * Check whether the @size in bytes is valid for an attribute of @type on the
1125  * ntfs volume @vol.  This information is obtained from $AttrDef system file.
1126  *
1127  * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1128  * listed in $AttrDef.
1129  */
1130 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1131                 const s64 size)
1132 {
1133         ATTR_DEF *ad;
1134
1135         BUG_ON(size < 0);
1136         /*
1137          * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1138          * listed in $AttrDef.
1139          */
1140         if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1141                 return -ERANGE;
1142         /* Get the $AttrDef entry for the attribute @type. */
1143         ad = ntfs_attr_find_in_attrdef(vol, type);
1144         if (unlikely(!ad))
1145                 return -ENOENT;
1146         /* Do the bounds check. */
1147         if (((sle64_to_cpu(ad->min_size) > 0) &&
1148                         size < sle64_to_cpu(ad->min_size)) ||
1149                         ((sle64_to_cpu(ad->max_size) > 0) && size >
1150                         sle64_to_cpu(ad->max_size)))
1151                 return -ERANGE;
1152         return 0;
1153 }
1154
1155 /**
1156  * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1157  * @vol:        ntfs volume to which the attribute belongs
1158  * @type:       attribute type which to check
1159  *
1160  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1161  * be non-resident.  This information is obtained from $AttrDef system file.
1162  *
1163  * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1164  * -ENOENT if the attribute is not listed in $AttrDef.
1165  */
1166 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1167 {
1168         ATTR_DEF *ad;
1169
1170         /* Find the attribute definition record in $AttrDef. */
1171         ad = ntfs_attr_find_in_attrdef(vol, type);
1172         if (unlikely(!ad))
1173                 return -ENOENT;
1174         /* Check the flags and return the result. */
1175         if (ad->flags & ATTR_DEF_RESIDENT)
1176                 return -EPERM;
1177         return 0;
1178 }
1179
1180 /**
1181  * ntfs_attr_can_be_resident - check if an attribute can be resident
1182  * @vol:        ntfs volume to which the attribute belongs
1183  * @type:       attribute type which to check
1184  *
1185  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1186  * be resident.  This information is derived from our ntfs knowledge and may
1187  * not be completely accurate, especially when user defined attributes are
1188  * present.  Basically we allow everything to be resident except for index
1189  * allocation and $EA attributes.
1190  *
1191  * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1192  *
1193  * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1194  *          otherwise windows will not boot (blue screen of death)!  We cannot
1195  *          check for this here as we do not know which inode's $Bitmap is
1196  *          being asked about so the caller needs to special case this.
1197  */
1198 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1199 {
1200         if (type == AT_INDEX_ALLOCATION || type == AT_EA)
1201                 return -EPERM;
1202         return 0;
1203 }
1204
1205 /**
1206  * ntfs_attr_record_resize - resize an attribute record
1207  * @m:          mft record containing attribute record
1208  * @a:          attribute record to resize
1209  * @new_size:   new size in bytes to which to resize the attribute record @a
1210  *
1211  * Resize the attribute record @a, i.e. the resident part of the attribute, in
1212  * the mft record @m to @new_size bytes.
1213  *
1214  * Return 0 on success and -errno on error.  The following error codes are
1215  * defined:
1216  *      -ENOSPC - Not enough space in the mft record @m to perform the resize.
1217  *
1218  * Note: On error, no modifications have been performed whatsoever.
1219  *
1220  * Warning: If you make a record smaller without having copied all the data you
1221  *          are interested in the data may be overwritten.
1222  */
1223 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1224 {
1225         ntfs_debug("Entering for new_size %u.", new_size);
1226         /* Align to 8 bytes if it is not already done. */
1227         if (new_size & 7)
1228                 new_size = (new_size + 7) & ~7;
1229         /* If the actual attribute length has changed, move things around. */
1230         if (new_size != le32_to_cpu(a->length)) {
1231                 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1232                                 le32_to_cpu(a->length) + new_size;
1233                 /* Not enough space in this mft record. */
1234                 if (new_muse > le32_to_cpu(m->bytes_allocated))
1235                         return -ENOSPC;
1236                 /* Move attributes following @a to their new location. */
1237                 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1238                                 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1239                                 (u8*)m) - le32_to_cpu(a->length));
1240                 /* Adjust @m to reflect the change in used space. */
1241                 m->bytes_in_use = cpu_to_le32(new_muse);
1242                 /* Adjust @a to reflect the new size. */
1243                 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1244                         a->length = cpu_to_le32(new_size);
1245         }
1246         return 0;
1247 }
1248
1249 /**
1250  * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1251  * @ni:         ntfs inode describing the attribute to convert
1252  *
1253  * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1254  * non-resident one.
1255  *
1256  * Return 0 on success and -errno on error.  The following error return codes
1257  * are defined:
1258  *      -EPERM  - The attribute is not allowed to be non-resident.
1259  *      -ENOMEM - Not enough memory.
1260  *      -ENOSPC - Not enough disk space.
1261  *      -EINVAL - Attribute not defined on the volume.
1262  *      -EIO    - I/o error or other error.
1263  * Note that -ENOSPC is also returned in the case that there is not enough
1264  * space in the mft record to do the conversion.  This can happen when the mft
1265  * record is already very full.  The caller is responsible for trying to make
1266  * space in the mft record and trying again.  FIXME: Do we need a separate
1267  * error return code for this kind of -ENOSPC or is it always worth trying
1268  * again in case the attribute may then fit in a resident state so no need to
1269  * make it non-resident at all?  Ho-hum...  (AIA)
1270  *
1271  * NOTE to self: No changes in the attribute list are required to move from
1272  *               a resident to a non-resident attribute.
1273  *
1274  * Locking: - The caller must hold i_sem on the inode.
1275  */
1276 int ntfs_attr_make_non_resident(ntfs_inode *ni)
1277 {
1278         s64 new_size;
1279         struct inode *vi = VFS_I(ni);
1280         ntfs_volume *vol = ni->vol;
1281         ntfs_inode *base_ni;
1282         MFT_RECORD *m;
1283         ATTR_RECORD *a;
1284         ntfs_attr_search_ctx *ctx;
1285         struct page *page;
1286         runlist_element *rl;
1287         u8 *kaddr;
1288         unsigned long flags;
1289         int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1290         u32 attr_size;
1291         u8 old_res_attr_flags;
1292
1293         /* Check that the attribute is allowed to be non-resident. */
1294         err = ntfs_attr_can_be_non_resident(vol, ni->type);
1295         if (unlikely(err)) {
1296                 if (err == -EPERM)
1297                         ntfs_debug("Attribute is not allowed to be "
1298                                         "non-resident.");
1299                 else
1300                         ntfs_debug("Attribute not defined on the NTFS "
1301                                         "volume!");
1302                 return err;
1303         }
1304         /*
1305          * The size needs to be aligned to a cluster boundary for allocation
1306          * purposes.
1307          */
1308         new_size = (i_size_read(vi) + vol->cluster_size - 1) &
1309                         ~(vol->cluster_size - 1);
1310         if (new_size > 0) {
1311                 runlist_element *rl2;
1312
1313                 /*
1314                  * Will need the page later and since the page lock nests
1315                  * outside all ntfs locks, we need to get the page now.
1316                  */
1317                 page = find_or_create_page(vi->i_mapping, 0,
1318                                 mapping_gfp_mask(vi->i_mapping));
1319                 if (unlikely(!page))
1320                         return -ENOMEM;
1321                 /* Start by allocating clusters to hold the attribute value. */
1322                 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1323                                 vol->cluster_size_bits, -1, DATA_ZONE);
1324                 if (IS_ERR(rl)) {
1325                         err = PTR_ERR(rl);
1326                         ntfs_debug("Failed to allocate cluster%s, error code "
1327                                         "%i.", (new_size >>
1328                                         vol->cluster_size_bits) > 1 ? "s" : "",
1329                                         err);
1330                         goto page_err_out;
1331                 }
1332                 /* Change the runlist terminator to LCN_ENOENT. */
1333                 rl2 = rl;
1334                 while (rl2->length)
1335                         rl2++;
1336                 BUG_ON(rl2->lcn != LCN_RL_NOT_MAPPED);
1337                 rl2->lcn = LCN_ENOENT;
1338         } else {
1339                 rl = NULL;
1340                 page = NULL;
1341         }
1342         /* Determine the size of the mapping pairs array. */
1343         mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1344         if (unlikely(mp_size < 0)) {
1345                 err = mp_size;
1346                 ntfs_debug("Failed to get size for mapping pairs array, error "
1347                                 "code %i.", err);
1348                 goto rl_err_out;
1349         }
1350         down_write(&ni->runlist.lock);
1351         if (!NInoAttr(ni))
1352                 base_ni = ni;
1353         else
1354                 base_ni = ni->ext.base_ntfs_ino;
1355         m = map_mft_record(base_ni);
1356         if (IS_ERR(m)) {
1357                 err = PTR_ERR(m);
1358                 m = NULL;
1359                 ctx = NULL;
1360                 goto err_out;
1361         }
1362         ctx = ntfs_attr_get_search_ctx(base_ni, m);
1363         if (unlikely(!ctx)) {
1364                 err = -ENOMEM;
1365                 goto err_out;
1366         }
1367         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1368                         CASE_SENSITIVE, 0, NULL, 0, ctx);
1369         if (unlikely(err)) {
1370                 if (err == -ENOENT)
1371                         err = -EIO;
1372                 goto err_out;
1373         }
1374         m = ctx->mrec;
1375         a = ctx->attr;
1376         BUG_ON(NInoNonResident(ni));
1377         BUG_ON(a->non_resident);
1378         /*
1379          * Calculate new offsets for the name and the mapping pairs array.
1380          * We assume the attribute is not compressed or sparse.
1381          */
1382         name_ofs = (offsetof(ATTR_REC,
1383                         data.non_resident.compressed_size) + 7) & ~7;
1384         mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1385         /*
1386          * Determine the size of the resident part of the now non-resident
1387          * attribute record.
1388          */
1389         arec_size = (mp_ofs + mp_size + 7) & ~7;
1390         /*
1391          * If the page is not uptodate bring it uptodate by copying from the
1392          * attribute value.
1393          */
1394         attr_size = le32_to_cpu(a->data.resident.value_length);
1395         BUG_ON(attr_size != i_size_read(vi));
1396         if (page && !PageUptodate(page)) {
1397                 kaddr = kmap_atomic(page, KM_USER0);
1398                 memcpy(kaddr, (u8*)a +
1399                                 le16_to_cpu(a->data.resident.value_offset),
1400                                 attr_size);
1401                 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1402                 kunmap_atomic(kaddr, KM_USER0);
1403                 flush_dcache_page(page);
1404                 SetPageUptodate(page);
1405         }
1406         /* Backup the attribute flag. */
1407         old_res_attr_flags = a->data.resident.flags;
1408         /* Resize the resident part of the attribute record. */
1409         err = ntfs_attr_record_resize(m, a, arec_size);
1410         if (unlikely(err))
1411                 goto err_out;
1412         /*
1413          * Convert the resident part of the attribute record to describe a
1414          * non-resident attribute.
1415          */
1416         a->non_resident = 1;
1417         /* Move the attribute name if it exists and update the offset. */
1418         if (a->name_length)
1419                 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1420                                 a->name_length * sizeof(ntfschar));
1421         a->name_offset = cpu_to_le16(name_ofs);
1422         /*
1423          * FIXME: For now just clear all of these as we do not support them
1424          * when writing.
1425          */
1426         a->flags &= cpu_to_le16(0xffff & ~le16_to_cpu(ATTR_IS_SPARSE |
1427                         ATTR_IS_ENCRYPTED | ATTR_COMPRESSION_MASK));
1428         /* Setup the fields specific to non-resident attributes. */
1429         a->data.non_resident.lowest_vcn = 0;
1430         a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1431                         vol->cluster_size_bits);
1432         a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1433         a->data.non_resident.compression_unit = 0;
1434         memset(&a->data.non_resident.reserved, 0,
1435                         sizeof(a->data.non_resident.reserved));
1436         a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1437         a->data.non_resident.data_size =
1438                         a->data.non_resident.initialized_size =
1439                         cpu_to_sle64(attr_size);
1440         /* Generate the mapping pairs array into the attribute record. */
1441         err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1442                         arec_size - mp_ofs, rl, 0, -1, NULL);
1443         if (unlikely(err)) {
1444                 ntfs_debug("Failed to build mapping pairs, error code %i.",
1445                                 err);
1446                 goto undo_err_out;
1447         }
1448         /* Setup the in-memory attribute structure to be non-resident. */
1449         /*
1450          * FIXME: For now just clear all of these as we do not support them
1451          * when writing.
1452          */
1453         NInoClearSparse(ni);
1454         NInoClearEncrypted(ni);
1455         NInoClearCompressed(ni);
1456         ni->runlist.rl = rl;
1457         write_lock_irqsave(&ni->size_lock, flags);
1458         ni->allocated_size = new_size;
1459         write_unlock_irqrestore(&ni->size_lock, flags);
1460         /*
1461          * This needs to be last since the address space operations ->readpage
1462          * and ->writepage can run concurrently with us as they are not
1463          * serialized on i_sem.  Note, we are not allowed to fail once we flip
1464          * this switch, which is another reason to do this last.
1465          */
1466         NInoSetNonResident(ni);
1467         /* Mark the mft record dirty, so it gets written back. */
1468         flush_dcache_mft_record_page(ctx->ntfs_ino);
1469         mark_mft_record_dirty(ctx->ntfs_ino);
1470         ntfs_attr_put_search_ctx(ctx);
1471         unmap_mft_record(base_ni);
1472         up_write(&ni->runlist.lock);
1473         if (page) {
1474                 set_page_dirty(page);
1475                 unlock_page(page);
1476                 mark_page_accessed(page);
1477                 page_cache_release(page);
1478         }
1479         ntfs_debug("Done.");
1480         return 0;
1481 undo_err_out:
1482         /* Convert the attribute back into a resident attribute. */
1483         a->non_resident = 0;
1484         /* Move the attribute name if it exists and update the offset. */
1485         name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1486                         sizeof(a->data.resident.reserved) + 7) & ~7;
1487         if (a->name_length)
1488                 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1489                                 a->name_length * sizeof(ntfschar));
1490         mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1491         a->name_offset = cpu_to_le16(name_ofs);
1492         arec_size = (mp_ofs + attr_size + 7) & ~7;
1493         /* Resize the resident part of the attribute record. */
1494         err2 = ntfs_attr_record_resize(m, a, arec_size);
1495         if (unlikely(err2)) {
1496                 /*
1497                  * This cannot happen (well if memory corruption is at work it
1498                  * could happen in theory), but deal with it as well as we can.
1499                  * If the old size is too small, truncate the attribute,
1500                  * otherwise simply give it a larger allocated size.
1501                  * FIXME: Should check whether chkdsk complains when the
1502                  * allocated size is much bigger than the resident value size.
1503                  */
1504                 arec_size = le32_to_cpu(a->length);
1505                 if ((mp_ofs + attr_size) > arec_size) {
1506                         err2 = attr_size;
1507                         attr_size = arec_size - mp_ofs;
1508                         ntfs_error(vol->sb, "Failed to undo partial resident "
1509                                         "to non-resident attribute "
1510                                         "conversion.  Truncating inode 0x%lx, "
1511                                         "attribute type 0x%x from %i bytes to "
1512                                         "%i bytes to maintain metadata "
1513                                         "consistency.  THIS MEANS YOU ARE "
1514                                         "LOSING %i BYTES DATA FROM THIS %s.",
1515                                         vi->i_ino,
1516                                         (unsigned)le32_to_cpu(ni->type),
1517                                         err2, attr_size, err2 - attr_size,
1518                                         ((ni->type == AT_DATA) &&
1519                                         !ni->name_len) ? "FILE": "ATTRIBUTE");
1520                         write_lock_irqsave(&ni->size_lock, flags);
1521                         ni->initialized_size = attr_size;
1522                         i_size_write(vi, attr_size);
1523                         write_unlock_irqrestore(&ni->size_lock, flags);
1524                 }
1525         }
1526         /* Setup the fields specific to resident attributes. */
1527         a->data.resident.value_length = cpu_to_le32(attr_size);
1528         a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1529         a->data.resident.flags = old_res_attr_flags;
1530         memset(&a->data.resident.reserved, 0,
1531                         sizeof(a->data.resident.reserved));
1532         /* Copy the data from the page back to the attribute value. */
1533         if (page) {
1534                 kaddr = kmap_atomic(page, KM_USER0);
1535                 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1536                 kunmap_atomic(kaddr, KM_USER0);
1537         }
1538         /* Setup the allocated size in the ntfs inode in case it changed. */
1539         write_lock_irqsave(&ni->size_lock, flags);
1540         ni->allocated_size = arec_size - mp_ofs;
1541         write_unlock_irqrestore(&ni->size_lock, flags);
1542         /* Mark the mft record dirty, so it gets written back. */
1543         flush_dcache_mft_record_page(ctx->ntfs_ino);
1544         mark_mft_record_dirty(ctx->ntfs_ino);
1545 err_out:
1546         if (ctx)
1547                 ntfs_attr_put_search_ctx(ctx);
1548         if (m)
1549                 unmap_mft_record(base_ni);
1550         ni->runlist.rl = NULL;
1551         up_write(&ni->runlist.lock);
1552 rl_err_out:
1553         if (rl) {
1554                 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1555                         ntfs_error(vol->sb, "Failed to release allocated "
1556                                         "cluster(s) in error code path.  Run "
1557                                         "chkdsk to recover the lost "
1558                                         "cluster(s).");
1559                         NVolSetErrors(vol);
1560                 }
1561                 ntfs_free(rl);
1562 page_err_out:
1563                 unlock_page(page);
1564                 page_cache_release(page);
1565         }
1566         if (err == -EINVAL)
1567                 err = -EIO;
1568         return err;
1569 }
1570
1571 /**
1572  * ntfs_attr_set - fill (a part of) an attribute with a byte
1573  * @ni:         ntfs inode describing the attribute to fill
1574  * @ofs:        offset inside the attribute at which to start to fill
1575  * @cnt:        number of bytes to fill
1576  * @val:        the unsigned 8-bit value with which to fill the attribute
1577  *
1578  * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
1579  * byte offset @ofs inside the attribute with the constant byte @val.
1580  *
1581  * This function is effectively like memset() applied to an ntfs attribute.
1582  * Note thie function actually only operates on the page cache pages belonging
1583  * to the ntfs attribute and it marks them dirty after doing the memset().
1584  * Thus it relies on the vm dirty page write code paths to cause the modified
1585  * pages to be written to the mft record/disk.
1586  *
1587  * Return 0 on success and -errno on error.  An error code of -ESPIPE means
1588  * that @ofs + @cnt were outside the end of the attribute and no write was
1589  * performed.
1590  */
1591 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1592 {
1593         ntfs_volume *vol = ni->vol;
1594         struct address_space *mapping;
1595         struct page *page;
1596         u8 *kaddr;
1597         pgoff_t idx, end;
1598         unsigned int start_ofs, end_ofs, size;
1599
1600         ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
1601                         (long long)ofs, (long long)cnt, val);
1602         BUG_ON(ofs < 0);
1603         BUG_ON(cnt < 0);
1604         if (!cnt)
1605                 goto done;
1606         mapping = VFS_I(ni)->i_mapping;
1607         /* Work out the starting index and page offset. */
1608         idx = ofs >> PAGE_CACHE_SHIFT;
1609         start_ofs = ofs & ~PAGE_CACHE_MASK;
1610         /* Work out the ending index and page offset. */
1611         end = ofs + cnt;
1612         end_ofs = end & ~PAGE_CACHE_MASK;
1613         /* If the end is outside the inode size return -ESPIPE. */
1614         if (unlikely(end > i_size_read(VFS_I(ni)))) {
1615                 ntfs_error(vol->sb, "Request exceeds end of attribute.");
1616                 return -ESPIPE;
1617         }
1618         end >>= PAGE_CACHE_SHIFT;
1619         /* If there is a first partial page, need to do it the slow way. */
1620         if (start_ofs) {
1621                 page = read_cache_page(mapping, idx,
1622                                 (filler_t*)mapping->a_ops->readpage, NULL);
1623                 if (IS_ERR(page)) {
1624                         ntfs_error(vol->sb, "Failed to read first partial "
1625                                         "page (sync error, index 0x%lx).", idx);
1626                         return PTR_ERR(page);
1627                 }
1628                 wait_on_page_locked(page);
1629                 if (unlikely(!PageUptodate(page))) {
1630                         ntfs_error(vol->sb, "Failed to read first partial page "
1631                                         "(async error, index 0x%lx).", idx);
1632                         page_cache_release(page);
1633                         return PTR_ERR(page);
1634                 }
1635                 /*
1636                  * If the last page is the same as the first page, need to
1637                  * limit the write to the end offset.
1638                  */
1639                 size = PAGE_CACHE_SIZE;
1640                 if (idx == end)
1641                         size = end_ofs;
1642                 kaddr = kmap_atomic(page, KM_USER0);
1643                 memset(kaddr + start_ofs, val, size - start_ofs);
1644                 flush_dcache_page(page);
1645                 kunmap_atomic(kaddr, KM_USER0);
1646                 set_page_dirty(page);
1647                 page_cache_release(page);
1648                 if (idx == end)
1649                         goto done;
1650                 idx++;
1651         }
1652         /* Do the whole pages the fast way. */
1653         for (; idx < end; idx++) {
1654                 /* Find or create the current page.  (The page is locked.) */
1655                 page = grab_cache_page(mapping, idx);
1656                 if (unlikely(!page)) {
1657                         ntfs_error(vol->sb, "Insufficient memory to grab "
1658                                         "page (index 0x%lx).", idx);
1659                         return -ENOMEM;
1660                 }
1661                 kaddr = kmap_atomic(page, KM_USER0);
1662                 memset(kaddr, val, PAGE_CACHE_SIZE);
1663                 flush_dcache_page(page);
1664                 kunmap_atomic(kaddr, KM_USER0);
1665                 /*
1666                  * If the page has buffers, mark them uptodate since buffer
1667                  * state and not page state is definitive in 2.6 kernels.
1668                  */
1669                 if (page_has_buffers(page)) {
1670                         struct buffer_head *bh, *head;
1671
1672                         bh = head = page_buffers(page);
1673                         do {
1674                                 set_buffer_uptodate(bh);
1675                         } while ((bh = bh->b_this_page) != head);
1676                 }
1677                 /* Now that buffers are uptodate, set the page uptodate, too. */
1678                 SetPageUptodate(page);
1679                 /*
1680                  * Set the page and all its buffers dirty and mark the inode
1681                  * dirty, too.  The VM will write the page later on.
1682                  */
1683                 set_page_dirty(page);
1684                 /* Finally unlock and release the page. */
1685                 unlock_page(page);
1686                 page_cache_release(page);
1687         }
1688         /* If there is a last partial page, need to do it the slow way. */
1689         if (end_ofs) {
1690                 page = read_cache_page(mapping, idx,
1691                                 (filler_t*)mapping->a_ops->readpage, NULL);
1692                 if (IS_ERR(page)) {
1693                         ntfs_error(vol->sb, "Failed to read last partial page "
1694                                         "(sync error, index 0x%lx).", idx);
1695                         return PTR_ERR(page);
1696                 }
1697                 wait_on_page_locked(page);
1698                 if (unlikely(!PageUptodate(page))) {
1699                         ntfs_error(vol->sb, "Failed to read last partial page "
1700                                         "(async error, index 0x%lx).", idx);
1701                         page_cache_release(page);
1702                         return PTR_ERR(page);
1703                 }
1704                 kaddr = kmap_atomic(page, KM_USER0);
1705                 memset(kaddr, val, end_ofs);
1706                 flush_dcache_page(page);
1707                 kunmap_atomic(kaddr, KM_USER0);
1708                 set_page_dirty(page);
1709                 page_cache_release(page);
1710         }
1711 done:
1712         ntfs_debug("Done.");
1713         return 0;
1714 }
1715
1716 #endif /* NTFS_RW */