blob: 1610f1cd28626701177ab5819c14c9ac70b08845 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
3 *
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +00004 * Copyright (c) 2001-2005 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
25#include "attrib.h"
26#include "debug.h"
27#include "layout.h"
28#include "mft.h"
29#include "ntfs.h"
30#include "types.h"
31
32/**
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000033 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * @ni: ntfs inode for which to map (part of) a runlist
35 * @vcn: map runlist part containing this vcn
36 *
37 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
38 *
39 * Return 0 on success and -errno on error.
40 *
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000041 * Locking: - The runlist must be locked for writing.
42 * - This function modifies the runlist.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000044int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 ntfs_inode *base_ni;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 MFT_RECORD *mrec;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000048 ntfs_attr_search_ctx *ctx;
49 runlist_element *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int err = 0;
51
52 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
53 (unsigned long long)vcn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (!NInoAttr(ni))
55 base_ni = ni;
56 else
57 base_ni = ni->ext.base_ntfs_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mrec = map_mft_record(base_ni);
59 if (IS_ERR(mrec))
60 return PTR_ERR(mrec);
61 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
62 if (unlikely(!ctx)) {
63 err = -ENOMEM;
64 goto err_out;
65 }
66 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
67 CASE_SENSITIVE, vcn, NULL, 0, ctx);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000068 if (likely(!err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 rl = ntfs_mapping_pairs_decompress(ni->vol, ctx->attr,
70 ni->runlist.rl);
71 if (IS_ERR(rl))
72 err = PTR_ERR(rl);
73 else
74 ni->runlist.rl = rl;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ntfs_attr_put_search_ctx(ctx);
77err_out:
78 unmap_mft_record(base_ni);
79 return err;
80}
81
82/**
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +000083 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
84 * @ni: ntfs inode for which to map (part of) a runlist
85 * @vcn: map runlist part containing this vcn
86 *
87 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
88 *
89 * Return 0 on success and -errno on error.
90 *
91 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
92 * - This function takes the runlist lock for writing and modifies the
93 * runlist.
94 */
95int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
96{
97 int err = 0;
98
99 down_write(&ni->runlist.lock);
100 /* Make sure someone else didn't do the work while we were sleeping. */
101 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
102 LCN_RL_NOT_MAPPED))
103 err = ntfs_map_runlist_nolock(ni, vcn);
104 up_write(&ni->runlist.lock);
105 return err;
106}
107
108/**
Anton Altaparmakov271849a2005-03-07 21:36:18 +0000109 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
110 * @ni: ntfs inode of the attribute whose runlist to search
111 * @vcn: vcn to convert
112 * @write_locked: true if the runlist is locked for writing
113 *
114 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
115 * described by the ntfs inode @ni and return the corresponding logical cluster
116 * number (lcn).
117 *
118 * If the @vcn is not mapped yet, the attempt is made to map the attribute
119 * extent containing the @vcn and the vcn to lcn conversion is retried.
120 *
121 * If @write_locked is true the caller has locked the runlist for writing and
122 * if false for reading.
123 *
124 * Since lcns must be >= 0, we use negative return codes with special meaning:
125 *
126 * Return code Meaning / Description
127 * ==========================================
128 * LCN_HOLE Hole / not allocated on disk.
129 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
130 * LCN_ENOMEM Not enough memory to map runlist.
131 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
132 *
133 * Locking: - The runlist must be locked on entry and is left locked on return.
134 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
135 * the lock may be dropped inside the function so you cannot rely on
136 * the runlist still being the same when this function returns.
137 */
138LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
139 const BOOL write_locked)
140{
141 LCN lcn;
142 BOOL is_retry = FALSE;
143
144 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
145 ni->mft_no, (unsigned long long)vcn,
146 write_locked ? "write" : "read");
147 BUG_ON(!ni);
148 BUG_ON(!NInoNonResident(ni));
149 BUG_ON(vcn < 0);
150retry_remap:
151 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
152 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
153 if (likely(lcn >= LCN_HOLE)) {
154 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
155 return lcn;
156 }
157 if (lcn != LCN_RL_NOT_MAPPED) {
158 if (lcn != LCN_ENOENT)
159 lcn = LCN_EIO;
160 } else if (!is_retry) {
161 int err;
162
163 if (!write_locked) {
164 up_read(&ni->runlist.lock);
165 down_write(&ni->runlist.lock);
166 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
167 LCN_RL_NOT_MAPPED)) {
168 up_write(&ni->runlist.lock);
169 down_read(&ni->runlist.lock);
170 goto retry_remap;
171 }
172 }
173 err = ntfs_map_runlist_nolock(ni, vcn);
174 if (!write_locked) {
175 up_write(&ni->runlist.lock);
176 down_read(&ni->runlist.lock);
177 }
178 if (likely(!err)) {
179 is_retry = TRUE;
180 goto retry_remap;
181 }
182 if (err == -ENOENT)
183 lcn = LCN_ENOENT;
184 else if (err == -ENOMEM)
185 lcn = LCN_ENOMEM;
186 else
187 lcn = LCN_EIO;
188 }
189 if (lcn != LCN_ENOENT)
190 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
191 (long long)lcn);
192 return lcn;
193}
194
195/**
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000196 * ntfs_find_vcn_nolock - find a vcn in the runlist described by an ntfs inode
197 * @ni: ntfs inode describing the runlist to search
198 * @vcn: vcn to find
199 * @write_locked: true if the runlist is locked for writing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
201 * Find the virtual cluster number @vcn in the runlist described by the ntfs
202 * inode @ni and return the address of the runlist element containing the @vcn.
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000203 * The runlist is left locked and the caller has to unlock it. In the error
204 * case, the runlist is left in the same locking state as on entry.
205 *
206 * Note if @write_locked is FALSE the lock may be dropped inside the function
207 * so you cannot rely on the runlist still being the same when this function
208 * returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
210 * Note you need to distinguish between the lcn of the returned runlist element
211 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
212 * read and allocate clusters on write.
213 *
214 * Return the runlist element containing the @vcn on success and
215 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
216 * to decide if the return is success or failure and PTR_ERR() to get to the
217 * error code if IS_ERR() is true.
218 *
219 * The possible error return codes are:
220 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
221 * -ENOMEM - Not enough memory to map runlist.
222 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
223 *
224 * Locking: - The runlist must be unlocked on entry.
225 * - On failing return, the runlist is unlocked.
226 * - On successful return, the runlist is locked. If @need_write us
227 * true, it is locked for writing. Otherwise is is locked for
228 * reading.
229 */
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000230runlist_element *ntfs_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
231 const BOOL write_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 runlist_element *rl;
234 int err = 0;
235 BOOL is_retry = FALSE;
236
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000237 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ni->mft_no, (unsigned long long)vcn,
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000239 write_locked ? "write" : "read");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 BUG_ON(!ni);
241 BUG_ON(!NInoNonResident(ni));
242 BUG_ON(vcn < 0);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000243retry_remap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 rl = ni->runlist.rl;
245 if (likely(rl && vcn >= rl[0].vcn)) {
246 while (likely(rl->length)) {
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000247 if (unlikely(vcn < rl[1].vcn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (likely(rl->lcn >= LCN_HOLE)) {
249 ntfs_debug("Done.");
250 return rl;
251 }
252 break;
253 }
254 rl++;
255 }
256 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
257 if (likely(rl->lcn == LCN_ENOENT))
258 err = -ENOENT;
259 else
260 err = -EIO;
261 }
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!err && !is_retry) {
264 /*
265 * The @vcn is in an unmapped region, map the runlist and
266 * retry.
267 */
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000268 if (!write_locked) {
269 up_read(&ni->runlist.lock);
270 down_write(&ni->runlist.lock);
271 }
272 err = ntfs_map_runlist_nolock(ni, vcn);
273 if (!write_locked) {
274 up_write(&ni->runlist.lock);
275 down_read(&ni->runlist.lock);
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (likely(!err)) {
278 is_retry = TRUE;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000279 goto retry_remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 /*
282 * -EINVAL and -ENOENT coming from a failed mapping attempt are
283 * equivalent to i/o errors for us as they should not happen in
284 * our code paths.
285 */
286 if (err == -EINVAL || err == -ENOENT)
287 err = -EIO;
288 } else if (!err)
289 err = -EIO;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000290 if (err != -ENOENT)
291 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return ERR_PTR(err);
293}
294
295/**
296 * ntfs_attr_find - find (next) attribute in mft record
297 * @type: attribute type to find
298 * @name: attribute name to find (optional, i.e. NULL means don't care)
299 * @name_len: attribute name length (only needed if @name present)
300 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
301 * @val: attribute value to find (optional, resident attributes only)
302 * @val_len: attribute value length
303 * @ctx: search context with mft record and attribute to search from
304 *
305 * You should not need to call this function directly. Use ntfs_attr_lookup()
306 * instead.
307 *
308 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
309 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
310 * attribute of @type, optionally @name and @val.
311 *
312 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
313 * point to the found attribute.
314 *
315 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
316 * @ctx->attr will point to the attribute before which the attribute being
317 * searched for would need to be inserted if such an action were to be desired.
318 *
319 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
320 * undefined and in particular do not rely on it not changing.
321 *
322 * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
323 * is FALSE, the search begins after @ctx->attr.
324 *
325 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
326 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
327 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
328 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
329 * sensitive. When @name is present, @name_len is the @name length in Unicode
330 * characters.
331 *
332 * If @name is not present (NULL), we assume that the unnamed attribute is
333 * being searched for.
334 *
335 * Finally, the resident attribute value @val is looked for, if present. If
336 * @val is not present (NULL), @val_len is ignored.
337 *
338 * ntfs_attr_find() only searches the specified mft record and it ignores the
339 * presence of an attribute list attribute (unless it is the one being searched
340 * for, obviously). If you need to take attribute lists into consideration,
341 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
342 * use ntfs_attr_find() to search for extent records of non-resident
343 * attributes, as extents with lowest_vcn != 0 are usually described by the
344 * attribute list attribute only. - Note that it is possible that the first
345 * extent is only in the attribute list while the last extent is in the base
346 * mft record, so do not rely on being able to find the first extent in the
347 * base mft record.
348 *
349 * Warning: Never use @val when looking for attribute types which can be
350 * non-resident as this most likely will result in a crash!
351 */
352static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
353 const u32 name_len, const IGNORE_CASE_BOOL ic,
354 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
355{
356 ATTR_RECORD *a;
357 ntfs_volume *vol = ctx->ntfs_ino->vol;
358 ntfschar *upcase = vol->upcase;
359 u32 upcase_len = vol->upcase_len;
360
361 /*
362 * Iterate over attributes in mft record starting at @ctx->attr, or the
363 * attribute following that, if @ctx->is_first is TRUE.
364 */
365 if (ctx->is_first) {
366 a = ctx->attr;
367 ctx->is_first = FALSE;
368 } else
369 a = (ATTR_RECORD*)((u8*)ctx->attr +
370 le32_to_cpu(ctx->attr->length));
371 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
372 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
373 le32_to_cpu(ctx->mrec->bytes_allocated))
374 break;
375 ctx->attr = a;
376 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
377 a->type == AT_END))
378 return -ENOENT;
379 if (unlikely(!a->length))
380 break;
381 if (a->type != type)
382 continue;
383 /*
384 * If @name is present, compare the two names. If @name is
385 * missing, assume we want an unnamed attribute.
386 */
387 if (!name) {
388 /* The search failed if the found attribute is named. */
389 if (a->name_length)
390 return -ENOENT;
391 } else if (!ntfs_are_names_equal(name, name_len,
392 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
393 a->name_length, ic, upcase, upcase_len)) {
394 register int rc;
395
396 rc = ntfs_collate_names(name, name_len,
397 (ntfschar*)((u8*)a +
398 le16_to_cpu(a->name_offset)),
399 a->name_length, 1, IGNORE_CASE,
400 upcase, upcase_len);
401 /*
402 * If @name collates before a->name, there is no
403 * matching attribute.
404 */
405 if (rc == -1)
406 return -ENOENT;
407 /* If the strings are not equal, continue search. */
408 if (rc)
409 continue;
410 rc = ntfs_collate_names(name, name_len,
411 (ntfschar*)((u8*)a +
412 le16_to_cpu(a->name_offset)),
413 a->name_length, 1, CASE_SENSITIVE,
414 upcase, upcase_len);
415 if (rc == -1)
416 return -ENOENT;
417 if (rc)
418 continue;
419 }
420 /*
421 * The names match or @name not present and attribute is
422 * unnamed. If no @val specified, we have found the attribute
423 * and are done.
424 */
425 if (!val)
426 return 0;
427 /* @val is present; compare values. */
428 else {
429 register int rc;
430
431 rc = memcmp(val, (u8*)a + le16_to_cpu(
432 a->data.resident.value_offset),
433 min_t(u32, val_len, le32_to_cpu(
434 a->data.resident.value_length)));
435 /*
436 * If @val collates before the current attribute's
437 * value, there is no matching attribute.
438 */
439 if (!rc) {
440 register u32 avl;
441
442 avl = le32_to_cpu(
443 a->data.resident.value_length);
444 if (val_len == avl)
445 return 0;
446 if (val_len < avl)
447 return -ENOENT;
448 } else if (rc < 0)
449 return -ENOENT;
450 }
451 }
452 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
453 NVolSetErrors(vol);
454 return -EIO;
455}
456
457/**
458 * load_attribute_list - load an attribute list into memory
459 * @vol: ntfs volume from which to read
460 * @runlist: runlist of the attribute list
461 * @al_start: destination buffer
462 * @size: size of the destination buffer in bytes
463 * @initialized_size: initialized size of the attribute list
464 *
465 * Walk the runlist @runlist and load all clusters from it copying them into
466 * the linear buffer @al. The maximum number of bytes copied to @al is @size
467 * bytes. Note, @size does not need to be a multiple of the cluster size. If
468 * @initialized_size is less than @size, the region in @al between
469 * @initialized_size and @size will be zeroed and not read from disk.
470 *
471 * Return 0 on success or -errno on error.
472 */
473int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
474 const s64 size, const s64 initialized_size)
475{
476 LCN lcn;
477 u8 *al = al_start;
478 u8 *al_end = al + initialized_size;
479 runlist_element *rl;
480 struct buffer_head *bh;
481 struct super_block *sb;
482 unsigned long block_size;
483 unsigned long block, max_block;
484 int err = 0;
485 unsigned char block_size_bits;
486
487 ntfs_debug("Entering.");
488 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
489 initialized_size > size)
490 return -EINVAL;
491 if (!initialized_size) {
492 memset(al, 0, size);
493 return 0;
494 }
495 sb = vol->sb;
496 block_size = sb->s_blocksize;
497 block_size_bits = sb->s_blocksize_bits;
498 down_read(&runlist->lock);
499 rl = runlist->rl;
500 /* Read all clusters specified by the runlist one run at a time. */
501 while (rl->length) {
502 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
503 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
504 (unsigned long long)rl->vcn,
505 (unsigned long long)lcn);
506 /* The attribute list cannot be sparse. */
507 if (lcn < 0) {
508 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
509 "read attribute list.");
510 goto err_out;
511 }
512 block = lcn << vol->cluster_size_bits >> block_size_bits;
513 /* Read the run from device in chunks of block_size bytes. */
514 max_block = block + (rl->length << vol->cluster_size_bits >>
515 block_size_bits);
516 ntfs_debug("max_block = 0x%lx.", max_block);
517 do {
518 ntfs_debug("Reading block = 0x%lx.", block);
519 bh = sb_bread(sb, block);
520 if (!bh) {
521 ntfs_error(sb, "sb_bread() failed. Cannot "
522 "read attribute list.");
523 goto err_out;
524 }
525 if (al + block_size >= al_end)
526 goto do_final;
527 memcpy(al, bh->b_data, block_size);
528 brelse(bh);
529 al += block_size;
530 } while (++block < max_block);
531 rl++;
532 }
533 if (initialized_size < size) {
534initialize:
535 memset(al_start + initialized_size, 0, size - initialized_size);
536 }
537done:
538 up_read(&runlist->lock);
539 return err;
540do_final:
541 if (al < al_end) {
542 /*
543 * Partial block.
544 *
545 * Note: The attribute list can be smaller than its allocation
546 * by multiple clusters. This has been encountered by at least
547 * two people running Windows XP, thus we cannot do any
548 * truncation sanity checking here. (AIA)
549 */
550 memcpy(al, bh->b_data, al_end - al);
551 brelse(bh);
552 if (initialized_size < size)
553 goto initialize;
554 goto done;
555 }
556 brelse(bh);
557 /* Real overflow! */
558 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
559 "is truncated.");
560err_out:
561 err = -EIO;
562 goto done;
563}
564
565/**
566 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
567 * @type: attribute type to find
568 * @name: attribute name to find (optional, i.e. NULL means don't care)
569 * @name_len: attribute name length (only needed if @name present)
570 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
571 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
572 * @val: attribute value to find (optional, resident attributes only)
573 * @val_len: attribute value length
574 * @ctx: search context with mft record and attribute to search from
575 *
576 * You should not need to call this function directly. Use ntfs_attr_lookup()
577 * instead.
578 *
579 * Find an attribute by searching the attribute list for the corresponding
580 * attribute list entry. Having found the entry, map the mft record if the
581 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
582 * in there and return it.
583 *
584 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
585 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
586 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
587 * then the base inode).
588 *
589 * After finishing with the attribute/mft record you need to call
590 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
591 * mapped inodes, etc).
592 *
593 * If the attribute is found, ntfs_external_attr_find() returns 0 and
594 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
595 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
596 * the attribute list entry for the attribute.
597 *
598 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
599 * @ctx->attr will point to the attribute in the base mft record before which
600 * the attribute being searched for would need to be inserted if such an action
601 * were to be desired. @ctx->mrec will point to the mft record in which
602 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
603 * entry of the attribute before which the attribute being searched for would
604 * need to be inserted if such an action were to be desired.
605 *
606 * Thus to insert the not found attribute, one wants to add the attribute to
607 * @ctx->mrec (the base mft record) and if there is not enough space, the
608 * attribute should be placed in a newly allocated extent mft record. The
609 * attribute list entry for the inserted attribute should be inserted in the
610 * attribute list attribute at @ctx->al_entry.
611 *
612 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
613 * @ctx->attr is undefined and in particular do not rely on it not changing.
614 */
615static int ntfs_external_attr_find(const ATTR_TYPE type,
616 const ntfschar *name, const u32 name_len,
617 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
618 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
619{
620 ntfs_inode *base_ni, *ni;
621 ntfs_volume *vol;
622 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
623 u8 *al_start, *al_end;
624 ATTR_RECORD *a;
625 ntfschar *al_name;
626 u32 al_name_len;
627 int err = 0;
628 static const char *es = " Unmount and run chkdsk.";
629
630 ni = ctx->ntfs_ino;
631 base_ni = ctx->base_ntfs_ino;
632 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
633 if (!base_ni) {
634 /* First call happens with the base mft record. */
635 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
636 ctx->base_mrec = ctx->mrec;
637 }
638 if (ni == base_ni)
639 ctx->base_attr = ctx->attr;
640 if (type == AT_END)
641 goto not_found;
642 vol = base_ni->vol;
643 al_start = base_ni->attr_list;
644 al_end = al_start + base_ni->attr_list_size;
645 if (!ctx->al_entry)
646 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
647 /*
648 * Iterate over entries in attribute list starting at @ctx->al_entry,
649 * or the entry following that, if @ctx->is_first is TRUE.
650 */
651 if (ctx->is_first) {
652 al_entry = ctx->al_entry;
653 ctx->is_first = FALSE;
654 } else
655 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
656 le16_to_cpu(ctx->al_entry->length));
657 for (;; al_entry = next_al_entry) {
658 /* Out of bounds check. */
659 if ((u8*)al_entry < base_ni->attr_list ||
660 (u8*)al_entry > al_end)
661 break; /* Inode is corrupt. */
662 ctx->al_entry = al_entry;
663 /* Catch the end of the attribute list. */
664 if ((u8*)al_entry == al_end)
665 goto not_found;
666 if (!al_entry->length)
667 break;
668 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
669 le16_to_cpu(al_entry->length) > al_end)
670 break;
671 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
672 le16_to_cpu(al_entry->length));
673 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
674 goto not_found;
675 if (type != al_entry->type)
676 continue;
677 /*
678 * If @name is present, compare the two names. If @name is
679 * missing, assume we want an unnamed attribute.
680 */
681 al_name_len = al_entry->name_length;
682 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
683 if (!name) {
684 if (al_name_len)
685 goto not_found;
686 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
687 name_len, ic, vol->upcase, vol->upcase_len)) {
688 register int rc;
689
690 rc = ntfs_collate_names(name, name_len, al_name,
691 al_name_len, 1, IGNORE_CASE,
692 vol->upcase, vol->upcase_len);
693 /*
694 * If @name collates before al_name, there is no
695 * matching attribute.
696 */
697 if (rc == -1)
698 goto not_found;
699 /* If the strings are not equal, continue search. */
700 if (rc)
701 continue;
702 /*
703 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
704 * that is inconsistent with ntfs_attr_find(). The
705 * subsequent rc checks were also different. Perhaps I
706 * made a mistake in one of the two. Need to recheck
707 * which is correct or at least see what is going on...
708 * (AIA)
709 */
710 rc = ntfs_collate_names(name, name_len, al_name,
711 al_name_len, 1, CASE_SENSITIVE,
712 vol->upcase, vol->upcase_len);
713 if (rc == -1)
714 goto not_found;
715 if (rc)
716 continue;
717 }
718 /*
719 * The names match or @name not present and attribute is
720 * unnamed. Now check @lowest_vcn. Continue search if the
721 * next attribute list entry still fits @lowest_vcn. Otherwise
722 * we have reached the right one or the search has failed.
723 */
724 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
725 (u8*)next_al_entry + 6 < al_end &&
726 (u8*)next_al_entry + le16_to_cpu(
727 next_al_entry->length) <= al_end &&
728 sle64_to_cpu(next_al_entry->lowest_vcn) <=
729 lowest_vcn &&
730 next_al_entry->type == al_entry->type &&
731 next_al_entry->name_length == al_name_len &&
732 ntfs_are_names_equal((ntfschar*)((u8*)
733 next_al_entry +
734 next_al_entry->name_offset),
735 next_al_entry->name_length,
736 al_name, al_name_len, CASE_SENSITIVE,
737 vol->upcase, vol->upcase_len))
738 continue;
739 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
740 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
741 ntfs_error(vol->sb, "Found stale mft "
742 "reference in attribute list "
743 "of base inode 0x%lx.%s",
744 base_ni->mft_no, es);
745 err = -EIO;
746 break;
747 }
748 } else { /* Mft references do not match. */
749 /* If there is a mapped record unmap it first. */
750 if (ni != base_ni)
751 unmap_extent_mft_record(ni);
752 /* Do we want the base record back? */
753 if (MREF_LE(al_entry->mft_reference) ==
754 base_ni->mft_no) {
755 ni = ctx->ntfs_ino = base_ni;
756 ctx->mrec = ctx->base_mrec;
757 } else {
758 /* We want an extent record. */
759 ctx->mrec = map_extent_mft_record(base_ni,
760 le64_to_cpu(
761 al_entry->mft_reference), &ni);
762 if (IS_ERR(ctx->mrec)) {
763 ntfs_error(vol->sb, "Failed to map "
764 "extent mft record "
765 "0x%lx of base inode "
766 "0x%lx.%s",
767 MREF_LE(al_entry->
768 mft_reference),
769 base_ni->mft_no, es);
770 err = PTR_ERR(ctx->mrec);
771 if (err == -ENOENT)
772 err = -EIO;
773 /* Cause @ctx to be sanitized below. */
774 ni = NULL;
775 break;
776 }
777 ctx->ntfs_ino = ni;
778 }
779 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
780 le16_to_cpu(ctx->mrec->attrs_offset));
781 }
782 /*
783 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
784 * mft record containing the attribute represented by the
785 * current al_entry.
786 */
787 /*
788 * We could call into ntfs_attr_find() to find the right
789 * attribute in this mft record but this would be less
790 * efficient and not quite accurate as ntfs_attr_find() ignores
791 * the attribute instance numbers for example which become
792 * important when one plays with attribute lists. Also,
793 * because a proper match has been found in the attribute list
794 * entry above, the comparison can now be optimized. So it is
795 * worth re-implementing a simplified ntfs_attr_find() here.
796 */
797 a = ctx->attr;
798 /*
799 * Use a manual loop so we can still use break and continue
800 * with the same meanings as above.
801 */
802do_next_attr_loop:
803 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
804 le32_to_cpu(ctx->mrec->bytes_allocated))
805 break;
806 if (a->type == AT_END)
807 continue;
808 if (!a->length)
809 break;
810 if (al_entry->instance != a->instance)
811 goto do_next_attr;
812 /*
813 * If the type and/or the name are mismatched between the
814 * attribute list entry and the attribute record, there is
815 * corruption so we break and return error EIO.
816 */
817 if (al_entry->type != a->type)
818 break;
819 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
820 le16_to_cpu(a->name_offset)), a->name_length,
821 al_name, al_name_len, CASE_SENSITIVE,
822 vol->upcase, vol->upcase_len))
823 break;
824 ctx->attr = a;
825 /*
826 * If no @val specified or @val specified and it matches, we
827 * have found it!
828 */
829 if (!val || (!a->non_resident && le32_to_cpu(
830 a->data.resident.value_length) == val_len &&
831 !memcmp((u8*)a +
832 le16_to_cpu(a->data.resident.value_offset),
833 val, val_len))) {
834 ntfs_debug("Done, found.");
835 return 0;
836 }
837do_next_attr:
838 /* Proceed to the next attribute in the current mft record. */
839 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
840 goto do_next_attr_loop;
841 }
842 if (!err) {
843 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
844 "attribute list attribute.%s", base_ni->mft_no,
845 es);
846 err = -EIO;
847 }
848 if (ni != base_ni) {
849 if (ni)
850 unmap_extent_mft_record(ni);
851 ctx->ntfs_ino = base_ni;
852 ctx->mrec = ctx->base_mrec;
853 ctx->attr = ctx->base_attr;
854 }
855 if (err != -ENOMEM)
856 NVolSetErrors(vol);
857 return err;
858not_found:
859 /*
860 * If we were looking for AT_END, we reset the search context @ctx and
861 * use ntfs_attr_find() to seek to the end of the base mft record.
862 */
863 if (type == AT_END) {
864 ntfs_attr_reinit_search_ctx(ctx);
865 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
866 ctx);
867 }
868 /*
869 * The attribute was not found. Before we return, we want to ensure
870 * @ctx->mrec and @ctx->attr indicate the position at which the
871 * attribute should be inserted in the base mft record. Since we also
872 * want to preserve @ctx->al_entry we cannot reinitialize the search
873 * context using ntfs_attr_reinit_search_ctx() as this would set
874 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
875 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
876 * @ctx->al_entry as the remaining fields (base_*) are identical to
877 * their non base_ counterparts and we cannot set @ctx->base_attr
878 * correctly yet as we do not know what @ctx->attr will be set to by
879 * the call to ntfs_attr_find() below.
880 */
881 if (ni != base_ni)
882 unmap_extent_mft_record(ni);
883 ctx->mrec = ctx->base_mrec;
884 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
885 le16_to_cpu(ctx->mrec->attrs_offset));
886 ctx->is_first = TRUE;
887 ctx->ntfs_ino = base_ni;
888 ctx->base_ntfs_ino = NULL;
889 ctx->base_mrec = NULL;
890 ctx->base_attr = NULL;
891 /*
892 * In case there are multiple matches in the base mft record, need to
893 * keep enumerating until we get an attribute not found response (or
894 * another error), otherwise we would keep returning the same attribute
895 * over and over again and all programs using us for enumeration would
896 * lock up in a tight loop.
897 */
898 do {
899 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
900 ctx);
901 } while (!err);
902 ntfs_debug("Done, not found.");
903 return err;
904}
905
906/**
907 * ntfs_attr_lookup - find an attribute in an ntfs inode
908 * @type: attribute type to find
909 * @name: attribute name to find (optional, i.e. NULL means don't care)
910 * @name_len: attribute name length (only needed if @name present)
911 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
912 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
913 * @val: attribute value to find (optional, resident attributes only)
914 * @val_len: attribute value length
915 * @ctx: search context with mft record and attribute to search from
916 *
917 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
918 * be the base mft record and @ctx must have been obtained from a call to
919 * ntfs_attr_get_search_ctx().
920 *
921 * This function transparently handles attribute lists and @ctx is used to
922 * continue searches where they were left off at.
923 *
924 * After finishing with the attribute/mft record you need to call
925 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
926 * mapped inodes, etc).
927 *
928 * Return 0 if the search was successful and -errno if not.
929 *
930 * When 0, @ctx->attr is the found attribute and it is in mft record
931 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
932 * the attribute list entry of the found attribute.
933 *
934 * When -ENOENT, @ctx->attr is the attribute which collates just after the
935 * attribute being searched for, i.e. if one wants to add the attribute to the
936 * mft record this is the correct place to insert it into. If an attribute
937 * list attribute is present, @ctx->al_entry is the attribute list entry which
938 * collates just after the attribute list entry of the attribute being searched
939 * for, i.e. if one wants to add the attribute to the mft record this is the
940 * correct place to insert its attribute list entry into.
941 *
942 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
943 * then undefined and in particular you should not rely on it not changing.
944 */
945int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
946 const u32 name_len, const IGNORE_CASE_BOOL ic,
947 const VCN lowest_vcn, const u8 *val, const u32 val_len,
948 ntfs_attr_search_ctx *ctx)
949{
950 ntfs_inode *base_ni;
951
952 ntfs_debug("Entering.");
953 if (ctx->base_ntfs_ino)
954 base_ni = ctx->base_ntfs_ino;
955 else
956 base_ni = ctx->ntfs_ino;
957 /* Sanity check, just for debugging really. */
958 BUG_ON(!base_ni);
959 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
960 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
961 ctx);
962 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
963 val, val_len, ctx);
964}
965
966/**
967 * ntfs_attr_init_search_ctx - initialize an attribute search context
968 * @ctx: attribute search context to initialize
969 * @ni: ntfs inode with which to initialize the search context
970 * @mrec: mft record with which to initialize the search context
971 *
972 * Initialize the attribute search context @ctx with @ni and @mrec.
973 */
974static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
975 ntfs_inode *ni, MFT_RECORD *mrec)
976{
977 ctx->mrec = mrec;
978 /* Sanity checks are performed elsewhere. */
979 ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset));
980 ctx->is_first = TRUE;
981 ctx->ntfs_ino = ni;
982 ctx->al_entry = NULL;
983 ctx->base_ntfs_ino = NULL;
984 ctx->base_mrec = NULL;
985 ctx->base_attr = NULL;
986}
987
988/**
989 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
990 * @ctx: attribute search context to reinitialize
991 *
992 * Reinitialize the attribute search context @ctx, unmapping an associated
993 * extent mft record if present, and initialize the search context again.
994 *
995 * This is used when a search for a new attribute is being started to reset
996 * the search context to the beginning.
997 */
998void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
999{
1000 if (likely(!ctx->base_ntfs_ino)) {
1001 /* No attribute list. */
1002 ctx->is_first = TRUE;
1003 /* Sanity checks are performed elsewhere. */
1004 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1005 le16_to_cpu(ctx->mrec->attrs_offset));
1006 /*
1007 * This needs resetting due to ntfs_external_attr_find() which
1008 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1009 */
1010 ctx->al_entry = NULL;
1011 return;
1012 } /* Attribute list. */
1013 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1014 unmap_extent_mft_record(ctx->ntfs_ino);
1015 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1016 return;
1017}
1018
1019/**
1020 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1021 * @ni: ntfs inode with which to initialize the search context
1022 * @mrec: mft record with which to initialize the search context
1023 *
1024 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1025 * and return it. Return NULL if allocation failed.
1026 */
1027ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1028{
1029 ntfs_attr_search_ctx *ctx;
1030
1031 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1032 if (ctx)
1033 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1034 return ctx;
1035}
1036
1037/**
1038 * ntfs_attr_put_search_ctx - release an attribute search context
1039 * @ctx: attribute search context to free
1040 *
1041 * Release the attribute search context @ctx, unmapping an associated extent
1042 * mft record if present.
1043 */
1044void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1045{
1046 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1047 unmap_extent_mft_record(ctx->ntfs_ino);
1048 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1049 return;
1050}
1051
1052/**
1053 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1054 * @vol: ntfs volume to which the attribute belongs
1055 * @type: attribute type which to find
1056 *
1057 * Search for the attribute definition record corresponding to the attribute
1058 * @type in the $AttrDef system file.
1059 *
1060 * Return the attribute type definition record if found and NULL if not found.
1061 */
1062static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1063 const ATTR_TYPE type)
1064{
1065 ATTR_DEF *ad;
1066
1067 BUG_ON(!vol->attrdef);
1068 BUG_ON(!type);
1069 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1070 vol->attrdef_size && ad->type; ++ad) {
1071 /* We have not found it yet, carry on searching. */
1072 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1073 continue;
1074 /* We found the attribute; return it. */
1075 if (likely(ad->type == type))
1076 return ad;
1077 /* We have gone too far already. No point in continuing. */
1078 break;
1079 }
1080 /* Attribute not found. */
1081 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1082 le32_to_cpu(type));
1083 return NULL;
1084}
1085
1086/**
1087 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1088 * @vol: ntfs volume to which the attribute belongs
1089 * @type: attribute type which to check
1090 * @size: size which to check
1091 *
1092 * Check whether the @size in bytes is valid for an attribute of @type on the
1093 * ntfs volume @vol. This information is obtained from $AttrDef system file.
1094 *
1095 * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1096 * listed in $AttrDef.
1097 */
1098int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1099 const s64 size)
1100{
1101 ATTR_DEF *ad;
1102
1103 BUG_ON(size < 0);
1104 /*
1105 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1106 * listed in $AttrDef.
1107 */
1108 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1109 return -ERANGE;
1110 /* Get the $AttrDef entry for the attribute @type. */
1111 ad = ntfs_attr_find_in_attrdef(vol, type);
1112 if (unlikely(!ad))
1113 return -ENOENT;
1114 /* Do the bounds check. */
1115 if (((sle64_to_cpu(ad->min_size) > 0) &&
1116 size < sle64_to_cpu(ad->min_size)) ||
1117 ((sle64_to_cpu(ad->max_size) > 0) && size >
1118 sle64_to_cpu(ad->max_size)))
1119 return -ERANGE;
1120 return 0;
1121}
1122
1123/**
1124 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1125 * @vol: ntfs volume to which the attribute belongs
1126 * @type: attribute type which to check
1127 *
1128 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1129 * be non-resident. This information is obtained from $AttrDef system file.
1130 *
1131 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, or
1132 * -ENOENT if the attribute is not listed in $AttrDef.
1133 */
1134int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1135{
1136 ATTR_DEF *ad;
1137
1138 /*
Anton Altaparmakov7e693072005-03-03 16:38:59 +00001139 * $DATA and $EA are always allowed to be non-resident even if $AttrDef
1140 * does not specify this in the flags of the $DATA attribute definition
1141 * record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Anton Altaparmakov7e693072005-03-03 16:38:59 +00001143 if (type == AT_DATA || type == AT_EA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145 /* Find the attribute definition record in $AttrDef. */
1146 ad = ntfs_attr_find_in_attrdef(vol, type);
1147 if (unlikely(!ad))
1148 return -ENOENT;
1149 /* Check the flags and return the result. */
1150 if (ad->flags & CAN_BE_NON_RESIDENT)
1151 return 0;
1152 return -EPERM;
1153}
1154
1155/**
1156 * ntfs_attr_can_be_resident - check if an attribute can be 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 resident. This information is derived from our ntfs knowledge and may
1162 * not be completely accurate, especially when user defined attributes are
1163 * present. Basically we allow everything to be resident except for index
1164 * allocation and $EA attributes.
1165 *
1166 * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1167 *
1168 * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1169 * otherwise windows will not boot (blue screen of death)! We cannot
1170 * check for this here as we do not know which inode's $Bitmap is
1171 * being asked about so the caller needs to special case this.
1172 */
1173int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1174{
1175 if (type != AT_INDEX_ALLOCATION && type != AT_EA)
1176 return 0;
1177 return -EPERM;
1178}
1179
1180/**
1181 * ntfs_attr_record_resize - resize an attribute record
1182 * @m: mft record containing attribute record
1183 * @a: attribute record to resize
1184 * @new_size: new size in bytes to which to resize the attribute record @a
1185 *
1186 * Resize the attribute record @a, i.e. the resident part of the attribute, in
1187 * the mft record @m to @new_size bytes.
1188 *
1189 * Return 0 on success and -errno on error. The following error codes are
1190 * defined:
1191 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1192 *
1193 * Note: On error, no modifications have been performed whatsoever.
1194 *
1195 * Warning: If you make a record smaller without having copied all the data you
1196 * are interested in the data may be overwritten.
1197 */
1198int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1199{
1200 ntfs_debug("Entering for new_size %u.", new_size);
1201 /* Align to 8 bytes if it is not already done. */
1202 if (new_size & 7)
1203 new_size = (new_size + 7) & ~7;
1204 /* If the actual attribute length has changed, move things around. */
1205 if (new_size != le32_to_cpu(a->length)) {
1206 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1207 le32_to_cpu(a->length) + new_size;
1208 /* Not enough space in this mft record. */
1209 if (new_muse > le32_to_cpu(m->bytes_allocated))
1210 return -ENOSPC;
1211 /* Move attributes following @a to their new location. */
1212 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1213 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1214 (u8*)m) - le32_to_cpu(a->length));
1215 /* Adjust @m to reflect the change in used space. */
1216 m->bytes_in_use = cpu_to_le32(new_muse);
1217 /* Adjust @a to reflect the new size. */
1218 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1219 a->length = cpu_to_le32(new_size);
1220 }
1221 return 0;
1222}
1223
1224/**
1225 * ntfs_attr_set - fill (a part of) an attribute with a byte
1226 * @ni: ntfs inode describing the attribute to fill
1227 * @ofs: offset inside the attribute at which to start to fill
1228 * @cnt: number of bytes to fill
1229 * @val: the unsigned 8-bit value with which to fill the attribute
1230 *
1231 * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
1232 * byte offset @ofs inside the attribute with the constant byte @val.
1233 *
1234 * This function is effectively like memset() applied to an ntfs attribute.
Anton Altaparmakovda284382004-11-11 11:18:10 +00001235 * Note thie function actually only operates on the page cache pages belonging
1236 * to the ntfs attribute and it marks them dirty after doing the memset().
1237 * Thus it relies on the vm dirty page write code paths to cause the modified
1238 * pages to be written to the mft record/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 *
1240 * Return 0 on success and -errno on error. An error code of -ESPIPE means
1241 * that @ofs + @cnt were outside the end of the attribute and no write was
1242 * performed.
1243 */
1244int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1245{
1246 ntfs_volume *vol = ni->vol;
1247 struct address_space *mapping;
1248 struct page *page;
1249 u8 *kaddr;
1250 pgoff_t idx, end;
1251 unsigned int start_ofs, end_ofs, size;
1252
1253 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
1254 (long long)ofs, (long long)cnt, val);
1255 BUG_ON(ofs < 0);
1256 BUG_ON(cnt < 0);
1257 if (!cnt)
1258 goto done;
1259 mapping = VFS_I(ni)->i_mapping;
1260 /* Work out the starting index and page offset. */
1261 idx = ofs >> PAGE_CACHE_SHIFT;
1262 start_ofs = ofs & ~PAGE_CACHE_MASK;
1263 /* Work out the ending index and page offset. */
1264 end = ofs + cnt;
1265 end_ofs = end & ~PAGE_CACHE_MASK;
1266 /* If the end is outside the inode size return -ESPIPE. */
Anton Altaparmakovda284382004-11-11 11:18:10 +00001267 if (unlikely(end > i_size_read(VFS_I(ni)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ntfs_error(vol->sb, "Request exceeds end of attribute.");
1269 return -ESPIPE;
1270 }
1271 end >>= PAGE_CACHE_SHIFT;
1272 /* If there is a first partial page, need to do it the slow way. */
1273 if (start_ofs) {
1274 page = read_cache_page(mapping, idx,
1275 (filler_t*)mapping->a_ops->readpage, NULL);
1276 if (IS_ERR(page)) {
1277 ntfs_error(vol->sb, "Failed to read first partial "
1278 "page (sync error, index 0x%lx).", idx);
1279 return PTR_ERR(page);
1280 }
1281 wait_on_page_locked(page);
1282 if (unlikely(!PageUptodate(page))) {
1283 ntfs_error(vol->sb, "Failed to read first partial page "
1284 "(async error, index 0x%lx).", idx);
1285 page_cache_release(page);
1286 return PTR_ERR(page);
1287 }
1288 /*
1289 * If the last page is the same as the first page, need to
1290 * limit the write to the end offset.
1291 */
1292 size = PAGE_CACHE_SIZE;
1293 if (idx == end)
1294 size = end_ofs;
1295 kaddr = kmap_atomic(page, KM_USER0);
1296 memset(kaddr + start_ofs, val, size - start_ofs);
1297 flush_dcache_page(page);
1298 kunmap_atomic(kaddr, KM_USER0);
1299 set_page_dirty(page);
1300 page_cache_release(page);
1301 if (idx == end)
1302 goto done;
1303 idx++;
1304 }
1305 /* Do the whole pages the fast way. */
1306 for (; idx < end; idx++) {
1307 /* Find or create the current page. (The page is locked.) */
1308 page = grab_cache_page(mapping, idx);
1309 if (unlikely(!page)) {
1310 ntfs_error(vol->sb, "Insufficient memory to grab "
1311 "page (index 0x%lx).", idx);
1312 return -ENOMEM;
1313 }
1314 kaddr = kmap_atomic(page, KM_USER0);
1315 memset(kaddr, val, PAGE_CACHE_SIZE);
1316 flush_dcache_page(page);
1317 kunmap_atomic(kaddr, KM_USER0);
1318 /*
1319 * If the page has buffers, mark them uptodate since buffer
1320 * state and not page state is definitive in 2.6 kernels.
1321 */
1322 if (page_has_buffers(page)) {
1323 struct buffer_head *bh, *head;
1324
1325 bh = head = page_buffers(page);
1326 do {
1327 set_buffer_uptodate(bh);
1328 } while ((bh = bh->b_this_page) != head);
1329 }
1330 /* Now that buffers are uptodate, set the page uptodate, too. */
1331 SetPageUptodate(page);
1332 /*
1333 * Set the page and all its buffers dirty and mark the inode
1334 * dirty, too. The VM will write the page later on.
1335 */
1336 set_page_dirty(page);
1337 /* Finally unlock and release the page. */
1338 unlock_page(page);
1339 page_cache_release(page);
1340 }
1341 /* If there is a last partial page, need to do it the slow way. */
1342 if (end_ofs) {
1343 page = read_cache_page(mapping, idx,
1344 (filler_t*)mapping->a_ops->readpage, NULL);
1345 if (IS_ERR(page)) {
1346 ntfs_error(vol->sb, "Failed to read last partial page "
1347 "(sync error, index 0x%lx).", idx);
1348 return PTR_ERR(page);
1349 }
1350 wait_on_page_locked(page);
1351 if (unlikely(!PageUptodate(page))) {
1352 ntfs_error(vol->sb, "Failed to read last partial page "
1353 "(async error, index 0x%lx).", idx);
1354 page_cache_release(page);
1355 return PTR_ERR(page);
1356 }
1357 kaddr = kmap_atomic(page, KM_USER0);
1358 memset(kaddr, val, end_ofs);
1359 flush_dcache_page(page);
1360 kunmap_atomic(kaddr, KM_USER0);
1361 set_page_dirty(page);
1362 page_cache_release(page);
1363 }
1364done:
1365 ntfs_debug("Done.");
1366 return 0;
1367}