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