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