2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2005 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
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.
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.
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
23 #include <linux/buffer_head.h>
24 #include <linux/swap.h>
36 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
37 * @ni: ntfs inode for which to map (part of) a runlist
38 * @vcn: map runlist part containing this vcn
39 * @ctx: active attribute search context if present or NULL if not
41 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
43 * If @ctx is specified, it is an active search context of @ni and its base mft
44 * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
45 * runlist fragments and allows their mapping. If you do not have the mft
46 * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
47 * will perform the necessary mapping and unmapping.
49 * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
50 * restores it before returning. Thus, @ctx will be left pointing to the same
51 * attribute on return as on entry. However, the actual pointers in @ctx may
52 * point to different memory locations on return, so you must remember to reset
53 * any cached pointers from the @ctx, i.e. after the call to
54 * ntfs_map_runlist_nolock(), you will probably want to do:
57 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
58 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
60 * Return 0 on success and -errno on error. There is one special error code
61 * which is not an error as such. This is -ENOENT. It means that @vcn is out
62 * of bounds of the runlist.
64 * Note the runlist can be NULL after this function returns if @vcn is zero and
65 * the attribute has zero allocated size, i.e. there simply is no runlist.
67 * WARNING: If @ctx is supplied, regardless of whether success or failure is
68 * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
69 * is no longer valid, i.e. you need to either call
70 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
71 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
72 * why the mapping of the old inode failed.
74 * Locking: - The runlist described by @ni must be locked for writing on entry
75 * and is locked on return. Note the runlist will be modified.
76 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
77 * entry and it will be left unmapped on return.
78 * - If @ctx is not NULL, the base mft record must be mapped on entry
79 * and it will be left mapped on return.
81 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
89 struct page *put_this_page = NULL;
91 BOOL ctx_is_temporary, ctx_needs_reset;
92 ntfs_attr_search_ctx old_ctx;
94 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
95 (unsigned long long)vcn);
99 base_ni = ni->ext.base_ntfs_ino;
101 ctx_is_temporary = ctx_needs_reset = TRUE;
102 m = map_mft_record(base_ni);
105 ctx = ntfs_attr_get_search_ctx(base_ni, m);
106 if (unlikely(!ctx)) {
111 VCN allocated_size_vcn;
113 BUG_ON(IS_ERR(ctx->mrec));
115 BUG_ON(!a->non_resident);
116 ctx_is_temporary = FALSE;
117 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
118 read_lock_irqsave(&ni->size_lock, flags);
119 allocated_size_vcn = ni->allocated_size >>
120 ni->vol->cluster_size_bits;
121 read_unlock_irqrestore(&ni->size_lock, flags);
122 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
123 end_vcn = allocated_size_vcn - 1;
125 * If we already have the attribute extent containing @vcn in
126 * @ctx, no need to look it up again. We slightly cheat in
127 * that if vcn exceeds the allocated size, we will refuse to
128 * map the runlist below, so there is definitely no need to get
129 * the right attribute extent.
131 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
132 a->name_length == ni->name_len &&
133 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
134 ni->name, ni->name_len) &&
135 sle64_to_cpu(a->data.non_resident.lowest_vcn)
136 <= vcn && end_vcn >= vcn))
137 ctx_needs_reset = FALSE;
139 /* Save the old search context. */
142 * If the currently mapped (extent) inode is not the
143 * base inode we will unmap it when we reinitialize the
144 * search context which means we need to get a
145 * reference to the page containing the mapped mft
146 * record so we do not accidentally drop changes to the
147 * mft record when it has not been marked dirty yet.
149 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
150 old_ctx.base_ntfs_ino) {
151 put_this_page = old_ctx.ntfs_ino->page;
152 page_cache_get(put_this_page);
155 * Reinitialize the search context so we can lookup the
156 * needed attribute extent.
158 ntfs_attr_reinit_search_ctx(ctx);
159 ctx_needs_reset = TRUE;
162 if (ctx_needs_reset) {
163 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
164 CASE_SENSITIVE, vcn, NULL, 0, ctx);
170 BUG_ON(!ctx->attr->non_resident);
174 * Only decompress the mapping pairs if @vcn is inside it. Otherwise
175 * we get into problems when we try to map an out of bounds vcn because
176 * we then try to map the already mapped runlist fragment and
177 * ntfs_mapping_pairs_decompress() fails.
179 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
180 if (!a->data.non_resident.lowest_vcn && end_vcn == 1)
181 end_vcn = sle64_to_cpu(a->data.non_resident.allocated_size) >>
182 ni->vol->cluster_size_bits;
183 if (unlikely(vcn >= end_vcn)) {
187 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
193 if (ctx_is_temporary) {
195 ntfs_attr_put_search_ctx(ctx);
196 unmap_mft_record(base_ni);
197 } else if (ctx_needs_reset) {
199 * If there is no attribute list, restoring the search context
200 * is acomplished simply by copying the saved context back over
201 * the caller supplied context. If there is an attribute list,
202 * things are more complicated as we need to deal with mapping
203 * of mft records and resulting potential changes in pointers.
205 if (NInoAttrList(base_ni)) {
207 * If the currently mapped (extent) inode is not the
208 * one we had before, we need to unmap it and map the
211 if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
213 * If the currently mapped inode is not the
214 * base inode, unmap it.
216 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
217 ctx->base_ntfs_ino) {
218 unmap_extent_mft_record(ctx->ntfs_ino);
219 ctx->mrec = ctx->base_mrec;
223 * If the old mapped inode is not the base
226 if (old_ctx.base_ntfs_ino &&
228 old_ctx.base_ntfs_ino) {
230 ctx->mrec = map_mft_record(
233 * Something bad has happened. If out
234 * of memory retry till it succeeds.
235 * Any other errors are fatal and we
236 * return the error code in ctx->mrec.
237 * Let the caller deal with it... We
238 * just need to fudge things so the
239 * caller can reinit and/or put the
240 * search context safely.
242 if (IS_ERR(ctx->mrec)) {
243 if (PTR_ERR(ctx->mrec) ==
254 /* Update the changed pointers in the saved context. */
255 if (ctx->mrec != old_ctx.mrec) {
256 if (!IS_ERR(ctx->mrec))
257 old_ctx.attr = (ATTR_RECORD*)(
261 old_ctx.mrec = ctx->mrec;
264 /* Restore the search context to the saved one. */
267 * We drop the reference on the page we took earlier. In the
268 * case that IS_ERR(ctx->mrec) is true this means we might lose
269 * some changes to the mft record that had been made between
270 * the last time it was marked dirty/written out and now. This
271 * at this stage is not a problem as the mapping error is fatal
272 * enough that the mft record cannot be written out anyway and
273 * the caller is very likely to shutdown the whole inode
274 * immediately and mark the volume dirty for chkdsk to pick up
278 page_cache_release(put_this_page);
284 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
285 * @ni: ntfs inode for which to map (part of) a runlist
286 * @vcn: map runlist part containing this vcn
288 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
290 * Return 0 on success and -errno on error. There is one special error code
291 * which is not an error as such. This is -ENOENT. It means that @vcn is out
292 * of bounds of the runlist.
294 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
295 * - This function takes the runlist lock for writing and may modify
298 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
302 down_write(&ni->runlist.lock);
303 /* Make sure someone else didn't do the work while we were sleeping. */
304 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
306 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
307 up_write(&ni->runlist.lock);
312 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
313 * @ni: ntfs inode of the attribute whose runlist to search
314 * @vcn: vcn to convert
315 * @write_locked: true if the runlist is locked for writing
317 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
318 * described by the ntfs inode @ni and return the corresponding logical cluster
321 * If the @vcn is not mapped yet, the attempt is made to map the attribute
322 * extent containing the @vcn and the vcn to lcn conversion is retried.
324 * If @write_locked is true the caller has locked the runlist for writing and
325 * if false for reading.
327 * Since lcns must be >= 0, we use negative return codes with special meaning:
329 * Return code Meaning / Description
330 * ==========================================
331 * LCN_HOLE Hole / not allocated on disk.
332 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
333 * LCN_ENOMEM Not enough memory to map runlist.
334 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
336 * Locking: - The runlist must be locked on entry and is left locked on return.
337 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
338 * the lock may be dropped inside the function so you cannot rely on
339 * the runlist still being the same when this function returns.
341 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
342 const BOOL write_locked)
346 BOOL is_retry = FALSE;
348 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
349 ni->mft_no, (unsigned long long)vcn,
350 write_locked ? "write" : "read");
352 BUG_ON(!NInoNonResident(ni));
354 if (!ni->runlist.rl) {
355 read_lock_irqsave(&ni->size_lock, flags);
356 if (!ni->allocated_size) {
357 read_unlock_irqrestore(&ni->size_lock, flags);
360 read_unlock_irqrestore(&ni->size_lock, flags);
363 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
364 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
365 if (likely(lcn >= LCN_HOLE)) {
366 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
369 if (lcn != LCN_RL_NOT_MAPPED) {
370 if (lcn != LCN_ENOENT)
372 } else if (!is_retry) {
376 up_read(&ni->runlist.lock);
377 down_write(&ni->runlist.lock);
378 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
379 LCN_RL_NOT_MAPPED)) {
380 up_write(&ni->runlist.lock);
381 down_read(&ni->runlist.lock);
385 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
387 up_write(&ni->runlist.lock);
388 down_read(&ni->runlist.lock);
396 else if (err == -ENOMEM)
401 if (lcn != LCN_ENOENT)
402 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
408 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
409 * @ni: ntfs inode describing the runlist to search
411 * @write_locked: true if the runlist is locked for writing
413 * Find the virtual cluster number @vcn in the runlist described by the ntfs
414 * inode @ni and return the address of the runlist element containing the @vcn.
416 * If the @vcn is not mapped yet, the attempt is made to map the attribute
417 * extent containing the @vcn and the vcn to lcn conversion is retried.
419 * If @write_locked is true the caller has locked the runlist for writing and
420 * if false for reading.
422 * Note you need to distinguish between the lcn of the returned runlist element
423 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
424 * read and allocate clusters on write.
426 * Return the runlist element containing the @vcn on success and
427 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
428 * to decide if the return is success or failure and PTR_ERR() to get to the
429 * error code if IS_ERR() is true.
431 * The possible error return codes are:
432 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
433 * -ENOMEM - Not enough memory to map runlist.
434 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
436 * Locking: - The runlist must be locked on entry and is left locked on return.
437 * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
438 * the lock may be dropped inside the function so you cannot rely on
439 * the runlist still being the same when this function returns.
441 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
442 const BOOL write_locked)
447 BOOL is_retry = FALSE;
449 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
450 ni->mft_no, (unsigned long long)vcn,
451 write_locked ? "write" : "read");
453 BUG_ON(!NInoNonResident(ni));
455 if (!ni->runlist.rl) {
456 read_lock_irqsave(&ni->size_lock, flags);
457 if (!ni->allocated_size) {
458 read_unlock_irqrestore(&ni->size_lock, flags);
459 return ERR_PTR(-ENOENT);
461 read_unlock_irqrestore(&ni->size_lock, flags);
465 if (likely(rl && vcn >= rl[0].vcn)) {
466 while (likely(rl->length)) {
467 if (unlikely(vcn < rl[1].vcn)) {
468 if (likely(rl->lcn >= LCN_HOLE)) {
476 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
477 if (likely(rl->lcn == LCN_ENOENT))
483 if (!err && !is_retry) {
485 * The @vcn is in an unmapped region, map the runlist and
489 up_read(&ni->runlist.lock);
490 down_write(&ni->runlist.lock);
491 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
492 LCN_RL_NOT_MAPPED)) {
493 up_write(&ni->runlist.lock);
494 down_read(&ni->runlist.lock);
498 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
500 up_write(&ni->runlist.lock);
501 down_read(&ni->runlist.lock);
508 * -EINVAL coming from a failed mapping attempt is equivalent
509 * to i/o error for us as it should not happen in our code
517 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
522 * ntfs_attr_find - find (next) attribute in mft record
523 * @type: attribute type to find
524 * @name: attribute name to find (optional, i.e. NULL means don't care)
525 * @name_len: attribute name length (only needed if @name present)
526 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
527 * @val: attribute value to find (optional, resident attributes only)
528 * @val_len: attribute value length
529 * @ctx: search context with mft record and attribute to search from
531 * You should not need to call this function directly. Use ntfs_attr_lookup()
534 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
535 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
536 * attribute of @type, optionally @name and @val.
538 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
539 * point to the found attribute.
541 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
542 * @ctx->attr will point to the attribute before which the attribute being
543 * searched for would need to be inserted if such an action were to be desired.
545 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
546 * undefined and in particular do not rely on it not changing.
548 * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
549 * is FALSE, the search begins after @ctx->attr.
551 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
552 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
553 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
554 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
555 * sensitive. When @name is present, @name_len is the @name length in Unicode
558 * If @name is not present (NULL), we assume that the unnamed attribute is
559 * being searched for.
561 * Finally, the resident attribute value @val is looked for, if present. If
562 * @val is not present (NULL), @val_len is ignored.
564 * ntfs_attr_find() only searches the specified mft record and it ignores the
565 * presence of an attribute list attribute (unless it is the one being searched
566 * for, obviously). If you need to take attribute lists into consideration,
567 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
568 * use ntfs_attr_find() to search for extent records of non-resident
569 * attributes, as extents with lowest_vcn != 0 are usually described by the
570 * attribute list attribute only. - Note that it is possible that the first
571 * extent is only in the attribute list while the last extent is in the base
572 * mft record, so do not rely on being able to find the first extent in the
575 * Warning: Never use @val when looking for attribute types which can be
576 * non-resident as this most likely will result in a crash!
578 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
579 const u32 name_len, const IGNORE_CASE_BOOL ic,
580 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
583 ntfs_volume *vol = ctx->ntfs_ino->vol;
584 ntfschar *upcase = vol->upcase;
585 u32 upcase_len = vol->upcase_len;
588 * Iterate over attributes in mft record starting at @ctx->attr, or the
589 * attribute following that, if @ctx->is_first is TRUE.
593 ctx->is_first = FALSE;
595 a = (ATTR_RECORD*)((u8*)ctx->attr +
596 le32_to_cpu(ctx->attr->length));
597 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
598 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
599 le32_to_cpu(ctx->mrec->bytes_allocated))
602 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
605 if (unlikely(!a->length))
610 * If @name is present, compare the two names. If @name is
611 * missing, assume we want an unnamed attribute.
614 /* The search failed if the found attribute is named. */
617 } else if (!ntfs_are_names_equal(name, name_len,
618 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
619 a->name_length, ic, upcase, upcase_len)) {
622 rc = ntfs_collate_names(name, name_len,
624 le16_to_cpu(a->name_offset)),
625 a->name_length, 1, IGNORE_CASE,
628 * If @name collates before a->name, there is no
629 * matching attribute.
633 /* If the strings are not equal, continue search. */
636 rc = ntfs_collate_names(name, name_len,
638 le16_to_cpu(a->name_offset)),
639 a->name_length, 1, CASE_SENSITIVE,
647 * The names match or @name not present and attribute is
648 * unnamed. If no @val specified, we have found the attribute
653 /* @val is present; compare values. */
657 rc = memcmp(val, (u8*)a + le16_to_cpu(
658 a->data.resident.value_offset),
659 min_t(u32, val_len, le32_to_cpu(
660 a->data.resident.value_length)));
662 * If @val collates before the current attribute's
663 * value, there is no matching attribute.
669 a->data.resident.value_length);
678 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
684 * load_attribute_list - load an attribute list into memory
685 * @vol: ntfs volume from which to read
686 * @runlist: runlist of the attribute list
687 * @al_start: destination buffer
688 * @size: size of the destination buffer in bytes
689 * @initialized_size: initialized size of the attribute list
691 * Walk the runlist @runlist and load all clusters from it copying them into
692 * the linear buffer @al. The maximum number of bytes copied to @al is @size
693 * bytes. Note, @size does not need to be a multiple of the cluster size. If
694 * @initialized_size is less than @size, the region in @al between
695 * @initialized_size and @size will be zeroed and not read from disk.
697 * Return 0 on success or -errno on error.
699 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
700 const s64 size, const s64 initialized_size)
704 u8 *al_end = al + initialized_size;
706 struct buffer_head *bh;
707 struct super_block *sb;
708 unsigned long block_size;
709 unsigned long block, max_block;
711 unsigned char block_size_bits;
713 ntfs_debug("Entering.");
714 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
715 initialized_size > size)
717 if (!initialized_size) {
722 block_size = sb->s_blocksize;
723 block_size_bits = sb->s_blocksize_bits;
724 down_read(&runlist->lock);
727 ntfs_error(sb, "Cannot read attribute list since runlist is "
731 /* Read all clusters specified by the runlist one run at a time. */
733 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
734 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
735 (unsigned long long)rl->vcn,
736 (unsigned long long)lcn);
737 /* The attribute list cannot be sparse. */
739 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
740 "read attribute list.");
743 block = lcn << vol->cluster_size_bits >> block_size_bits;
744 /* Read the run from device in chunks of block_size bytes. */
745 max_block = block + (rl->length << vol->cluster_size_bits >>
747 ntfs_debug("max_block = 0x%lx.", max_block);
749 ntfs_debug("Reading block = 0x%lx.", block);
750 bh = sb_bread(sb, block);
752 ntfs_error(sb, "sb_bread() failed. Cannot "
753 "read attribute list.");
756 if (al + block_size >= al_end)
758 memcpy(al, bh->b_data, block_size);
761 } while (++block < max_block);
764 if (initialized_size < size) {
766 memset(al_start + initialized_size, 0, size - initialized_size);
769 up_read(&runlist->lock);
776 * Note: The attribute list can be smaller than its allocation
777 * by multiple clusters. This has been encountered by at least
778 * two people running Windows XP, thus we cannot do any
779 * truncation sanity checking here. (AIA)
781 memcpy(al, bh->b_data, al_end - al);
783 if (initialized_size < size)
789 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
797 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
798 * @type: attribute type to find
799 * @name: attribute name to find (optional, i.e. NULL means don't care)
800 * @name_len: attribute name length (only needed if @name present)
801 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
802 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
803 * @val: attribute value to find (optional, resident attributes only)
804 * @val_len: attribute value length
805 * @ctx: search context with mft record and attribute to search from
807 * You should not need to call this function directly. Use ntfs_attr_lookup()
810 * Find an attribute by searching the attribute list for the corresponding
811 * attribute list entry. Having found the entry, map the mft record if the
812 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
813 * in there and return it.
815 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
816 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
817 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
818 * then the base inode).
820 * After finishing with the attribute/mft record you need to call
821 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
822 * mapped inodes, etc).
824 * If the attribute is found, ntfs_external_attr_find() returns 0 and
825 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
826 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
827 * the attribute list entry for the attribute.
829 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
830 * @ctx->attr will point to the attribute in the base mft record before which
831 * the attribute being searched for would need to be inserted if such an action
832 * were to be desired. @ctx->mrec will point to the mft record in which
833 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
834 * entry of the attribute before which the attribute being searched for would
835 * need to be inserted if such an action were to be desired.
837 * Thus to insert the not found attribute, one wants to add the attribute to
838 * @ctx->mrec (the base mft record) and if there is not enough space, the
839 * attribute should be placed in a newly allocated extent mft record. The
840 * attribute list entry for the inserted attribute should be inserted in the
841 * attribute list attribute at @ctx->al_entry.
843 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
844 * @ctx->attr is undefined and in particular do not rely on it not changing.
846 static int ntfs_external_attr_find(const ATTR_TYPE type,
847 const ntfschar *name, const u32 name_len,
848 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
849 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
851 ntfs_inode *base_ni, *ni;
853 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
854 u8 *al_start, *al_end;
859 static const char *es = " Unmount and run chkdsk.";
862 base_ni = ctx->base_ntfs_ino;
863 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
865 /* First call happens with the base mft record. */
866 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
867 ctx->base_mrec = ctx->mrec;
870 ctx->base_attr = ctx->attr;
874 al_start = base_ni->attr_list;
875 al_end = al_start + base_ni->attr_list_size;
877 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
879 * Iterate over entries in attribute list starting at @ctx->al_entry,
880 * or the entry following that, if @ctx->is_first is TRUE.
883 al_entry = ctx->al_entry;
884 ctx->is_first = FALSE;
886 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
887 le16_to_cpu(ctx->al_entry->length));
888 for (;; al_entry = next_al_entry) {
889 /* Out of bounds check. */
890 if ((u8*)al_entry < base_ni->attr_list ||
891 (u8*)al_entry > al_end)
892 break; /* Inode is corrupt. */
893 ctx->al_entry = al_entry;
894 /* Catch the end of the attribute list. */
895 if ((u8*)al_entry == al_end)
897 if (!al_entry->length)
899 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
900 le16_to_cpu(al_entry->length) > al_end)
902 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
903 le16_to_cpu(al_entry->length));
904 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
906 if (type != al_entry->type)
909 * If @name is present, compare the two names. If @name is
910 * missing, assume we want an unnamed attribute.
912 al_name_len = al_entry->name_length;
913 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
917 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
918 name_len, ic, vol->upcase, vol->upcase_len)) {
921 rc = ntfs_collate_names(name, name_len, al_name,
922 al_name_len, 1, IGNORE_CASE,
923 vol->upcase, vol->upcase_len);
925 * If @name collates before al_name, there is no
926 * matching attribute.
930 /* If the strings are not equal, continue search. */
934 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
935 * that is inconsistent with ntfs_attr_find(). The
936 * subsequent rc checks were also different. Perhaps I
937 * made a mistake in one of the two. Need to recheck
938 * which is correct or at least see what is going on...
941 rc = ntfs_collate_names(name, name_len, al_name,
942 al_name_len, 1, CASE_SENSITIVE,
943 vol->upcase, vol->upcase_len);
950 * The names match or @name not present and attribute is
951 * unnamed. Now check @lowest_vcn. Continue search if the
952 * next attribute list entry still fits @lowest_vcn. Otherwise
953 * we have reached the right one or the search has failed.
955 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
956 (u8*)next_al_entry + 6 < al_end &&
957 (u8*)next_al_entry + le16_to_cpu(
958 next_al_entry->length) <= al_end &&
959 sle64_to_cpu(next_al_entry->lowest_vcn) <=
961 next_al_entry->type == al_entry->type &&
962 next_al_entry->name_length == al_name_len &&
963 ntfs_are_names_equal((ntfschar*)((u8*)
965 next_al_entry->name_offset),
966 next_al_entry->name_length,
967 al_name, al_name_len, CASE_SENSITIVE,
968 vol->upcase, vol->upcase_len))
970 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
971 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
972 ntfs_error(vol->sb, "Found stale mft "
973 "reference in attribute list "
974 "of base inode 0x%lx.%s",
975 base_ni->mft_no, es);
979 } else { /* Mft references do not match. */
980 /* If there is a mapped record unmap it first. */
982 unmap_extent_mft_record(ni);
983 /* Do we want the base record back? */
984 if (MREF_LE(al_entry->mft_reference) ==
986 ni = ctx->ntfs_ino = base_ni;
987 ctx->mrec = ctx->base_mrec;
989 /* We want an extent record. */
990 ctx->mrec = map_extent_mft_record(base_ni,
992 al_entry->mft_reference), &ni);
993 if (IS_ERR(ctx->mrec)) {
994 ntfs_error(vol->sb, "Failed to map "
996 "0x%lx of base inode "
1000 base_ni->mft_no, es);
1001 err = PTR_ERR(ctx->mrec);
1004 /* Cause @ctx to be sanitized below. */
1010 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1011 le16_to_cpu(ctx->mrec->attrs_offset));
1014 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1015 * mft record containing the attribute represented by the
1019 * We could call into ntfs_attr_find() to find the right
1020 * attribute in this mft record but this would be less
1021 * efficient and not quite accurate as ntfs_attr_find() ignores
1022 * the attribute instance numbers for example which become
1023 * important when one plays with attribute lists. Also,
1024 * because a proper match has been found in the attribute list
1025 * entry above, the comparison can now be optimized. So it is
1026 * worth re-implementing a simplified ntfs_attr_find() here.
1030 * Use a manual loop so we can still use break and continue
1031 * with the same meanings as above.
1034 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1035 le32_to_cpu(ctx->mrec->bytes_allocated))
1037 if (a->type == AT_END)
1041 if (al_entry->instance != a->instance)
1044 * If the type and/or the name are mismatched between the
1045 * attribute list entry and the attribute record, there is
1046 * corruption so we break and return error EIO.
1048 if (al_entry->type != a->type)
1050 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1051 le16_to_cpu(a->name_offset)), a->name_length,
1052 al_name, al_name_len, CASE_SENSITIVE,
1053 vol->upcase, vol->upcase_len))
1057 * If no @val specified or @val specified and it matches, we
1060 if (!val || (!a->non_resident && le32_to_cpu(
1061 a->data.resident.value_length) == val_len &&
1063 le16_to_cpu(a->data.resident.value_offset),
1065 ntfs_debug("Done, found.");
1069 /* Proceed to the next attribute in the current mft record. */
1070 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1071 goto do_next_attr_loop;
1074 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1075 "attribute list attribute.%s", base_ni->mft_no,
1079 if (ni != base_ni) {
1081 unmap_extent_mft_record(ni);
1082 ctx->ntfs_ino = base_ni;
1083 ctx->mrec = ctx->base_mrec;
1084 ctx->attr = ctx->base_attr;
1091 * If we were looking for AT_END, we reset the search context @ctx and
1092 * use ntfs_attr_find() to seek to the end of the base mft record.
1094 if (type == AT_END) {
1095 ntfs_attr_reinit_search_ctx(ctx);
1096 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1100 * The attribute was not found. Before we return, we want to ensure
1101 * @ctx->mrec and @ctx->attr indicate the position at which the
1102 * attribute should be inserted in the base mft record. Since we also
1103 * want to preserve @ctx->al_entry we cannot reinitialize the search
1104 * context using ntfs_attr_reinit_search_ctx() as this would set
1105 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
1106 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
1107 * @ctx->al_entry as the remaining fields (base_*) are identical to
1108 * their non base_ counterparts and we cannot set @ctx->base_attr
1109 * correctly yet as we do not know what @ctx->attr will be set to by
1110 * the call to ntfs_attr_find() below.
1113 unmap_extent_mft_record(ni);
1114 ctx->mrec = ctx->base_mrec;
1115 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1116 le16_to_cpu(ctx->mrec->attrs_offset));
1117 ctx->is_first = TRUE;
1118 ctx->ntfs_ino = base_ni;
1119 ctx->base_ntfs_ino = NULL;
1120 ctx->base_mrec = NULL;
1121 ctx->base_attr = NULL;
1123 * In case there are multiple matches in the base mft record, need to
1124 * keep enumerating until we get an attribute not found response (or
1125 * another error), otherwise we would keep returning the same attribute
1126 * over and over again and all programs using us for enumeration would
1127 * lock up in a tight loop.
1130 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1133 ntfs_debug("Done, not found.");
1138 * ntfs_attr_lookup - find an attribute in an ntfs inode
1139 * @type: attribute type to find
1140 * @name: attribute name to find (optional, i.e. NULL means don't care)
1141 * @name_len: attribute name length (only needed if @name present)
1142 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1143 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1144 * @val: attribute value to find (optional, resident attributes only)
1145 * @val_len: attribute value length
1146 * @ctx: search context with mft record and attribute to search from
1148 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1149 * be the base mft record and @ctx must have been obtained from a call to
1150 * ntfs_attr_get_search_ctx().
1152 * This function transparently handles attribute lists and @ctx is used to
1153 * continue searches where they were left off at.
1155 * After finishing with the attribute/mft record you need to call
1156 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1157 * mapped inodes, etc).
1159 * Return 0 if the search was successful and -errno if not.
1161 * When 0, @ctx->attr is the found attribute and it is in mft record
1162 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
1163 * the attribute list entry of the found attribute.
1165 * When -ENOENT, @ctx->attr is the attribute which collates just after the
1166 * attribute being searched for, i.e. if one wants to add the attribute to the
1167 * mft record this is the correct place to insert it into. If an attribute
1168 * list attribute is present, @ctx->al_entry is the attribute list entry which
1169 * collates just after the attribute list entry of the attribute being searched
1170 * for, i.e. if one wants to add the attribute to the mft record this is the
1171 * correct place to insert its attribute list entry into.
1173 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
1174 * then undefined and in particular you should not rely on it not changing.
1176 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1177 const u32 name_len, const IGNORE_CASE_BOOL ic,
1178 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1179 ntfs_attr_search_ctx *ctx)
1181 ntfs_inode *base_ni;
1183 ntfs_debug("Entering.");
1184 if (ctx->base_ntfs_ino)
1185 base_ni = ctx->base_ntfs_ino;
1187 base_ni = ctx->ntfs_ino;
1188 /* Sanity check, just for debugging really. */
1190 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1191 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1193 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1198 * ntfs_attr_init_search_ctx - initialize an attribute search context
1199 * @ctx: attribute search context to initialize
1200 * @ni: ntfs inode with which to initialize the search context
1201 * @mrec: mft record with which to initialize the search context
1203 * Initialize the attribute search context @ctx with @ni and @mrec.
1205 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1206 ntfs_inode *ni, MFT_RECORD *mrec)
1208 *ctx = (ntfs_attr_search_ctx) {
1210 /* Sanity checks are performed elsewhere. */
1211 .attr = (ATTR_RECORD*)((u8*)mrec +
1212 le16_to_cpu(mrec->attrs_offset)),
1219 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1220 * @ctx: attribute search context to reinitialize
1222 * Reinitialize the attribute search context @ctx, unmapping an associated
1223 * extent mft record if present, and initialize the search context again.
1225 * This is used when a search for a new attribute is being started to reset
1226 * the search context to the beginning.
1228 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1230 if (likely(!ctx->base_ntfs_ino)) {
1231 /* No attribute list. */
1232 ctx->is_first = TRUE;
1233 /* Sanity checks are performed elsewhere. */
1234 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1235 le16_to_cpu(ctx->mrec->attrs_offset));
1237 * This needs resetting due to ntfs_external_attr_find() which
1238 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1240 ctx->al_entry = NULL;
1242 } /* Attribute list. */
1243 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1244 unmap_extent_mft_record(ctx->ntfs_ino);
1245 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1250 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1251 * @ni: ntfs inode with which to initialize the search context
1252 * @mrec: mft record with which to initialize the search context
1254 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1255 * and return it. Return NULL if allocation failed.
1257 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1259 ntfs_attr_search_ctx *ctx;
1261 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1263 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1268 * ntfs_attr_put_search_ctx - release an attribute search context
1269 * @ctx: attribute search context to free
1271 * Release the attribute search context @ctx, unmapping an associated extent
1272 * mft record if present.
1274 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1276 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1277 unmap_extent_mft_record(ctx->ntfs_ino);
1278 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1285 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1286 * @vol: ntfs volume to which the attribute belongs
1287 * @type: attribute type which to find
1289 * Search for the attribute definition record corresponding to the attribute
1290 * @type in the $AttrDef system file.
1292 * Return the attribute type definition record if found and NULL if not found.
1294 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1295 const ATTR_TYPE type)
1299 BUG_ON(!vol->attrdef);
1301 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1302 vol->attrdef_size && ad->type; ++ad) {
1303 /* We have not found it yet, carry on searching. */
1304 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1306 /* We found the attribute; return it. */
1307 if (likely(ad->type == type))
1309 /* We have gone too far already. No point in continuing. */
1312 /* Attribute not found. */
1313 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1319 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1320 * @vol: ntfs volume to which the attribute belongs
1321 * @type: attribute type which to check
1322 * @size: size which to check
1324 * Check whether the @size in bytes is valid for an attribute of @type on the
1325 * ntfs volume @vol. This information is obtained from $AttrDef system file.
1327 * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1328 * listed in $AttrDef.
1330 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1337 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1338 * listed in $AttrDef.
1340 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1342 /* Get the $AttrDef entry for the attribute @type. */
1343 ad = ntfs_attr_find_in_attrdef(vol, type);
1346 /* Do the bounds check. */
1347 if (((sle64_to_cpu(ad->min_size) > 0) &&
1348 size < sle64_to_cpu(ad->min_size)) ||
1349 ((sle64_to_cpu(ad->max_size) > 0) && size >
1350 sle64_to_cpu(ad->max_size)))
1356 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1357 * @vol: ntfs volume to which the attribute belongs
1358 * @type: attribute type which to check
1360 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1361 * be non-resident. This information is obtained from $AttrDef system file.
1363 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1364 * -ENOENT if the attribute is not listed in $AttrDef.
1366 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1370 /* Find the attribute definition record in $AttrDef. */
1371 ad = ntfs_attr_find_in_attrdef(vol, type);
1374 /* Check the flags and return the result. */
1375 if (ad->flags & ATTR_DEF_RESIDENT)
1381 * ntfs_attr_can_be_resident - check if an attribute can be resident
1382 * @vol: ntfs volume to which the attribute belongs
1383 * @type: attribute type which to check
1385 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1386 * be resident. This information is derived from our ntfs knowledge and may
1387 * not be completely accurate, especially when user defined attributes are
1388 * present. Basically we allow everything to be resident except for index
1389 * allocation and $EA attributes.
1391 * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1393 * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1394 * otherwise windows will not boot (blue screen of death)! We cannot
1395 * check for this here as we do not know which inode's $Bitmap is
1396 * being asked about so the caller needs to special case this.
1398 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1400 if (type == AT_INDEX_ALLOCATION || type == AT_EA)
1406 * ntfs_attr_record_resize - resize an attribute record
1407 * @m: mft record containing attribute record
1408 * @a: attribute record to resize
1409 * @new_size: new size in bytes to which to resize the attribute record @a
1411 * Resize the attribute record @a, i.e. the resident part of the attribute, in
1412 * the mft record @m to @new_size bytes.
1414 * Return 0 on success and -errno on error. The following error codes are
1416 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1418 * Note: On error, no modifications have been performed whatsoever.
1420 * Warning: If you make a record smaller without having copied all the data you
1421 * are interested in the data may be overwritten.
1423 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1425 ntfs_debug("Entering for new_size %u.", new_size);
1426 /* Align to 8 bytes if it is not already done. */
1428 new_size = (new_size + 7) & ~7;
1429 /* If the actual attribute length has changed, move things around. */
1430 if (new_size != le32_to_cpu(a->length)) {
1431 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1432 le32_to_cpu(a->length) + new_size;
1433 /* Not enough space in this mft record. */
1434 if (new_muse > le32_to_cpu(m->bytes_allocated))
1436 /* Move attributes following @a to their new location. */
1437 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1438 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1439 (u8*)m) - le32_to_cpu(a->length));
1440 /* Adjust @m to reflect the change in used space. */
1441 m->bytes_in_use = cpu_to_le32(new_muse);
1442 /* Adjust @a to reflect the new size. */
1443 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1444 a->length = cpu_to_le32(new_size);
1450 * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1451 * @m: mft record containing attribute record
1452 * @a: attribute record whose value to resize
1453 * @new_size: new size in bytes to which to resize the attribute value of @a
1455 * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1456 * If the value is made bigger, the newly allocated space is cleared.
1458 * Return 0 on success and -errno on error. The following error codes are
1460 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1462 * Note: On error, no modifications have been performed whatsoever.
1464 * Warning: If you make a record smaller without having copied all the data you
1465 * are interested in the data may be overwritten.
1467 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1472 /* Resize the resident part of the attribute record. */
1473 if (ntfs_attr_record_resize(m, a,
1474 le16_to_cpu(a->data.resident.value_offset) + new_size))
1477 * The resize succeeded! If we made the attribute value bigger, clear
1478 * the area between the old size and @new_size.
1480 old_size = le32_to_cpu(a->data.resident.value_length);
1481 if (new_size > old_size)
1482 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1483 old_size, 0, new_size - old_size);
1484 /* Finally update the length of the attribute value. */
1485 a->data.resident.value_length = cpu_to_le32(new_size);
1490 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1491 * @ni: ntfs inode describing the attribute to convert
1493 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1496 * Return 0 on success and -errno on error. The following error return codes
1498 * -EPERM - The attribute is not allowed to be non-resident.
1499 * -ENOMEM - Not enough memory.
1500 * -ENOSPC - Not enough disk space.
1501 * -EINVAL - Attribute not defined on the volume.
1502 * -EIO - I/o error or other error.
1503 * Note that -ENOSPC is also returned in the case that there is not enough
1504 * space in the mft record to do the conversion. This can happen when the mft
1505 * record is already very full. The caller is responsible for trying to make
1506 * space in the mft record and trying again. FIXME: Do we need a separate
1507 * error return code for this kind of -ENOSPC or is it always worth trying
1508 * again in case the attribute may then fit in a resident state so no need to
1509 * make it non-resident at all? Ho-hum... (AIA)
1511 * NOTE to self: No changes in the attribute list are required to move from
1512 * a resident to a non-resident attribute.
1514 * Locking: - The caller must hold i_sem on the inode.
1516 int ntfs_attr_make_non_resident(ntfs_inode *ni)
1519 struct inode *vi = VFS_I(ni);
1520 ntfs_volume *vol = ni->vol;
1521 ntfs_inode *base_ni;
1524 ntfs_attr_search_ctx *ctx;
1526 runlist_element *rl;
1528 unsigned long flags;
1529 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1531 u8 old_res_attr_flags;
1533 /* Check that the attribute is allowed to be non-resident. */
1534 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1535 if (unlikely(err)) {
1537 ntfs_debug("Attribute is not allowed to be "
1540 ntfs_debug("Attribute not defined on the NTFS "
1545 * FIXME: Compressed and encrypted attributes are not supported when
1546 * writing and we should never have gotten here for them.
1548 BUG_ON(NInoCompressed(ni));
1549 BUG_ON(NInoEncrypted(ni));
1551 * The size needs to be aligned to a cluster boundary for allocation
1554 new_size = (i_size_read(vi) + vol->cluster_size - 1) &
1555 ~(vol->cluster_size - 1);
1557 runlist_element *rl2;
1560 * Will need the page later and since the page lock nests
1561 * outside all ntfs locks, we need to get the page now.
1563 page = find_or_create_page(vi->i_mapping, 0,
1564 mapping_gfp_mask(vi->i_mapping));
1565 if (unlikely(!page))
1567 /* Start by allocating clusters to hold the attribute value. */
1568 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1569 vol->cluster_size_bits, -1, DATA_ZONE);
1572 ntfs_debug("Failed to allocate cluster%s, error code "
1574 vol->cluster_size_bits) > 1 ? "s" : "",
1578 /* Change the runlist terminator to LCN_ENOENT. */
1582 BUG_ON(rl2->lcn != LCN_RL_NOT_MAPPED);
1583 rl2->lcn = LCN_ENOENT;
1588 /* Determine the size of the mapping pairs array. */
1589 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1590 if (unlikely(mp_size < 0)) {
1592 ntfs_debug("Failed to get size for mapping pairs array, error "
1596 down_write(&ni->runlist.lock);
1600 base_ni = ni->ext.base_ntfs_ino;
1601 m = map_mft_record(base_ni);
1608 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1609 if (unlikely(!ctx)) {
1613 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1614 CASE_SENSITIVE, 0, NULL, 0, ctx);
1615 if (unlikely(err)) {
1622 BUG_ON(NInoNonResident(ni));
1623 BUG_ON(a->non_resident);
1625 * Calculate new offsets for the name and the mapping pairs array.
1627 if (NInoSparse(ni) || NInoCompressed(ni))
1628 name_ofs = (offsetof(ATTR_REC,
1629 data.non_resident.compressed_size) +
1630 sizeof(a->data.non_resident.compressed_size) +
1633 name_ofs = (offsetof(ATTR_REC,
1634 data.non_resident.compressed_size) + 7) & ~7;
1635 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1637 * Determine the size of the resident part of the now non-resident
1640 arec_size = (mp_ofs + mp_size + 7) & ~7;
1642 * If the page is not uptodate bring it uptodate by copying from the
1645 attr_size = le32_to_cpu(a->data.resident.value_length);
1646 BUG_ON(attr_size != i_size_read(vi));
1647 if (page && !PageUptodate(page)) {
1648 kaddr = kmap_atomic(page, KM_USER0);
1649 memcpy(kaddr, (u8*)a +
1650 le16_to_cpu(a->data.resident.value_offset),
1652 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1653 kunmap_atomic(kaddr, KM_USER0);
1654 flush_dcache_page(page);
1655 SetPageUptodate(page);
1657 /* Backup the attribute flag. */
1658 old_res_attr_flags = a->data.resident.flags;
1659 /* Resize the resident part of the attribute record. */
1660 err = ntfs_attr_record_resize(m, a, arec_size);
1664 * Convert the resident part of the attribute record to describe a
1665 * non-resident attribute.
1667 a->non_resident = 1;
1668 /* Move the attribute name if it exists and update the offset. */
1670 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1671 a->name_length * sizeof(ntfschar));
1672 a->name_offset = cpu_to_le16(name_ofs);
1673 /* Setup the fields specific to non-resident attributes. */
1674 a->data.non_resident.lowest_vcn = 0;
1675 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1676 vol->cluster_size_bits);
1677 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1678 memset(&a->data.non_resident.reserved, 0,
1679 sizeof(a->data.non_resident.reserved));
1680 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1681 a->data.non_resident.data_size =
1682 a->data.non_resident.initialized_size =
1683 cpu_to_sle64(attr_size);
1684 if (NInoSparse(ni) || NInoCompressed(ni)) {
1685 a->data.non_resident.compression_unit = 4;
1686 a->data.non_resident.compressed_size =
1687 a->data.non_resident.allocated_size;
1689 a->data.non_resident.compression_unit = 0;
1690 /* Generate the mapping pairs array into the attribute record. */
1691 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1692 arec_size - mp_ofs, rl, 0, -1, NULL);
1693 if (unlikely(err)) {
1694 ntfs_debug("Failed to build mapping pairs, error code %i.",
1698 /* Setup the in-memory attribute structure to be non-resident. */
1699 ni->runlist.rl = rl;
1700 write_lock_irqsave(&ni->size_lock, flags);
1701 ni->allocated_size = new_size;
1702 if (NInoSparse(ni) || NInoCompressed(ni)) {
1703 ni->itype.compressed.size = ni->allocated_size;
1704 ni->itype.compressed.block_size = 1U <<
1705 (a->data.non_resident.compression_unit +
1706 vol->cluster_size_bits);
1707 ni->itype.compressed.block_size_bits =
1708 ffs(ni->itype.compressed.block_size) - 1;
1709 ni->itype.compressed.block_clusters = 1U <<
1710 a->data.non_resident.compression_unit;
1712 write_unlock_irqrestore(&ni->size_lock, flags);
1714 * This needs to be last since the address space operations ->readpage
1715 * and ->writepage can run concurrently with us as they are not
1716 * serialized on i_sem. Note, we are not allowed to fail once we flip
1717 * this switch, which is another reason to do this last.
1719 NInoSetNonResident(ni);
1720 /* Mark the mft record dirty, so it gets written back. */
1721 flush_dcache_mft_record_page(ctx->ntfs_ino);
1722 mark_mft_record_dirty(ctx->ntfs_ino);
1723 ntfs_attr_put_search_ctx(ctx);
1724 unmap_mft_record(base_ni);
1725 up_write(&ni->runlist.lock);
1727 set_page_dirty(page);
1729 mark_page_accessed(page);
1730 page_cache_release(page);
1732 ntfs_debug("Done.");
1735 /* Convert the attribute back into a resident attribute. */
1736 a->non_resident = 0;
1737 /* Move the attribute name if it exists and update the offset. */
1738 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1739 sizeof(a->data.resident.reserved) + 7) & ~7;
1741 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1742 a->name_length * sizeof(ntfschar));
1743 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1744 a->name_offset = cpu_to_le16(name_ofs);
1745 arec_size = (mp_ofs + attr_size + 7) & ~7;
1746 /* Resize the resident part of the attribute record. */
1747 err2 = ntfs_attr_record_resize(m, a, arec_size);
1748 if (unlikely(err2)) {
1750 * This cannot happen (well if memory corruption is at work it
1751 * could happen in theory), but deal with it as well as we can.
1752 * If the old size is too small, truncate the attribute,
1753 * otherwise simply give it a larger allocated size.
1754 * FIXME: Should check whether chkdsk complains when the
1755 * allocated size is much bigger than the resident value size.
1757 arec_size = le32_to_cpu(a->length);
1758 if ((mp_ofs + attr_size) > arec_size) {
1760 attr_size = arec_size - mp_ofs;
1761 ntfs_error(vol->sb, "Failed to undo partial resident "
1762 "to non-resident attribute "
1763 "conversion. Truncating inode 0x%lx, "
1764 "attribute type 0x%x from %i bytes to "
1765 "%i bytes to maintain metadata "
1766 "consistency. THIS MEANS YOU ARE "
1767 "LOSING %i BYTES DATA FROM THIS %s.",
1769 (unsigned)le32_to_cpu(ni->type),
1770 err2, attr_size, err2 - attr_size,
1771 ((ni->type == AT_DATA) &&
1772 !ni->name_len) ? "FILE": "ATTRIBUTE");
1773 write_lock_irqsave(&ni->size_lock, flags);
1774 ni->initialized_size = attr_size;
1775 i_size_write(vi, attr_size);
1776 write_unlock_irqrestore(&ni->size_lock, flags);
1779 /* Setup the fields specific to resident attributes. */
1780 a->data.resident.value_length = cpu_to_le32(attr_size);
1781 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1782 a->data.resident.flags = old_res_attr_flags;
1783 memset(&a->data.resident.reserved, 0,
1784 sizeof(a->data.resident.reserved));
1785 /* Copy the data from the page back to the attribute value. */
1787 kaddr = kmap_atomic(page, KM_USER0);
1788 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1789 kunmap_atomic(kaddr, KM_USER0);
1791 /* Setup the allocated size in the ntfs inode in case it changed. */
1792 write_lock_irqsave(&ni->size_lock, flags);
1793 ni->allocated_size = arec_size - mp_ofs;
1794 write_unlock_irqrestore(&ni->size_lock, flags);
1795 /* Mark the mft record dirty, so it gets written back. */
1796 flush_dcache_mft_record_page(ctx->ntfs_ino);
1797 mark_mft_record_dirty(ctx->ntfs_ino);
1800 ntfs_attr_put_search_ctx(ctx);
1802 unmap_mft_record(base_ni);
1803 ni->runlist.rl = NULL;
1804 up_write(&ni->runlist.lock);
1807 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1808 ntfs_error(vol->sb, "Failed to release allocated "
1809 "cluster(s) in error code path. Run "
1810 "chkdsk to recover the lost "
1817 page_cache_release(page);
1825 * ntfs_attr_set - fill (a part of) an attribute with a byte
1826 * @ni: ntfs inode describing the attribute to fill
1827 * @ofs: offset inside the attribute at which to start to fill
1828 * @cnt: number of bytes to fill
1829 * @val: the unsigned 8-bit value with which to fill the attribute
1831 * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
1832 * byte offset @ofs inside the attribute with the constant byte @val.
1834 * This function is effectively like memset() applied to an ntfs attribute.
1835 * Note thie function actually only operates on the page cache pages belonging
1836 * to the ntfs attribute and it marks them dirty after doing the memset().
1837 * Thus it relies on the vm dirty page write code paths to cause the modified
1838 * pages to be written to the mft record/disk.
1840 * Return 0 on success and -errno on error. An error code of -ESPIPE means
1841 * that @ofs + @cnt were outside the end of the attribute and no write was
1844 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
1846 ntfs_volume *vol = ni->vol;
1847 struct address_space *mapping;
1851 unsigned int start_ofs, end_ofs, size;
1853 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
1854 (long long)ofs, (long long)cnt, val);
1860 * FIXME: Compressed and encrypted attributes are not supported when
1861 * writing and we should never have gotten here for them.
1863 BUG_ON(NInoCompressed(ni));
1864 BUG_ON(NInoEncrypted(ni));
1865 mapping = VFS_I(ni)->i_mapping;
1866 /* Work out the starting index and page offset. */
1867 idx = ofs >> PAGE_CACHE_SHIFT;
1868 start_ofs = ofs & ~PAGE_CACHE_MASK;
1869 /* Work out the ending index and page offset. */
1871 end_ofs = end & ~PAGE_CACHE_MASK;
1872 /* If the end is outside the inode size return -ESPIPE. */
1873 if (unlikely(end > i_size_read(VFS_I(ni)))) {
1874 ntfs_error(vol->sb, "Request exceeds end of attribute.");
1877 end >>= PAGE_CACHE_SHIFT;
1878 /* If there is a first partial page, need to do it the slow way. */
1880 page = read_cache_page(mapping, idx,
1881 (filler_t*)mapping->a_ops->readpage, NULL);
1883 ntfs_error(vol->sb, "Failed to read first partial "
1884 "page (sync error, index 0x%lx).", idx);
1885 return PTR_ERR(page);
1887 wait_on_page_locked(page);
1888 if (unlikely(!PageUptodate(page))) {
1889 ntfs_error(vol->sb, "Failed to read first partial page "
1890 "(async error, index 0x%lx).", idx);
1891 page_cache_release(page);
1892 return PTR_ERR(page);
1895 * If the last page is the same as the first page, need to
1896 * limit the write to the end offset.
1898 size = PAGE_CACHE_SIZE;
1901 kaddr = kmap_atomic(page, KM_USER0);
1902 memset(kaddr + start_ofs, val, size - start_ofs);
1903 flush_dcache_page(page);
1904 kunmap_atomic(kaddr, KM_USER0);
1905 set_page_dirty(page);
1906 page_cache_release(page);
1911 /* Do the whole pages the fast way. */
1912 for (; idx < end; idx++) {
1913 /* Find or create the current page. (The page is locked.) */
1914 page = grab_cache_page(mapping, idx);
1915 if (unlikely(!page)) {
1916 ntfs_error(vol->sb, "Insufficient memory to grab "
1917 "page (index 0x%lx).", idx);
1920 kaddr = kmap_atomic(page, KM_USER0);
1921 memset(kaddr, val, PAGE_CACHE_SIZE);
1922 flush_dcache_page(page);
1923 kunmap_atomic(kaddr, KM_USER0);
1925 * If the page has buffers, mark them uptodate since buffer
1926 * state and not page state is definitive in 2.6 kernels.
1928 if (page_has_buffers(page)) {
1929 struct buffer_head *bh, *head;
1931 bh = head = page_buffers(page);
1933 set_buffer_uptodate(bh);
1934 } while ((bh = bh->b_this_page) != head);
1936 /* Now that buffers are uptodate, set the page uptodate, too. */
1937 SetPageUptodate(page);
1939 * Set the page and all its buffers dirty and mark the inode
1940 * dirty, too. The VM will write the page later on.
1942 set_page_dirty(page);
1943 /* Finally unlock and release the page. */
1945 page_cache_release(page);
1947 /* If there is a last partial page, need to do it the slow way. */
1949 page = read_cache_page(mapping, idx,
1950 (filler_t*)mapping->a_ops->readpage, NULL);
1952 ntfs_error(vol->sb, "Failed to read last partial page "
1953 "(sync error, index 0x%lx).", idx);
1954 return PTR_ERR(page);
1956 wait_on_page_locked(page);
1957 if (unlikely(!PageUptodate(page))) {
1958 ntfs_error(vol->sb, "Failed to read last partial page "
1959 "(async error, index 0x%lx).", idx);
1960 page_cache_release(page);
1961 return PTR_ERR(page);
1963 kaddr = kmap_atomic(page, KM_USER0);
1964 memset(kaddr, val, end_ofs);
1965 flush_dcache_page(page);
1966 kunmap_atomic(kaddr, KM_USER0);
1967 set_page_dirty(page);
1968 page_cache_release(page);
1971 ntfs_debug("Done.");
1975 #endif /* NTFS_RW */