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 * @ctx: active attribute search context if present or NULL if not
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 @ctx is specified, it is an active search context of @ni and its base mft
420 * record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
421 * runlist fragments and allows their mapping. If you do not have the mft
422 * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
423 * will perform the necessary mapping and unmapping.
425 * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
426 * restores it before returning. Thus, @ctx will be left pointing to the same
427 * attribute on return as on entry. However, the actual pointers in @ctx may
428 * point to different memory locations on return, so you must remember to reset
429 * any cached pointers from the @ctx, i.e. after the call to
430 * ntfs_attr_find_vcn_nolock(), you will probably want to do:
433 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
434 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
435 * Note you need to distinguish between the lcn of the returned runlist element
436 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
437 * read and allocate clusters on write.
439 * Return the runlist element containing the @vcn on success and
440 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
441 * to decide if the return is success or failure and PTR_ERR() to get to the
442 * error code if IS_ERR() is true.
444 * The possible error return codes are:
445 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
446 * -ENOMEM - Not enough memory to map runlist.
447 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
449 * WARNING: If @ctx is supplied, regardless of whether success or failure is
450 * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
451 * is no longer valid, i.e. you need to either call
452 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
453 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
454 * why the mapping of the old inode failed.
456 * Locking: - The runlist described by @ni must be locked for writing on entry
457 * and is locked on return. Note the runlist may be modified when
458 * needed runlist fragments need to be mapped.
459 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
460 * entry and it will be left unmapped on return.
461 * - If @ctx is not NULL, the base mft record must be mapped on entry
462 * and it will be left mapped on return.
464 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
465 ntfs_attr_search_ctx *ctx)
470 BOOL is_retry = FALSE;
472 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
473 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
475 BUG_ON(!NInoNonResident(ni));
477 if (!ni->runlist.rl) {
478 read_lock_irqsave(&ni->size_lock, flags);
479 if (!ni->allocated_size) {
480 read_unlock_irqrestore(&ni->size_lock, flags);
481 return ERR_PTR(-ENOENT);
483 read_unlock_irqrestore(&ni->size_lock, flags);
487 if (likely(rl && vcn >= rl[0].vcn)) {
488 while (likely(rl->length)) {
489 if (unlikely(vcn < rl[1].vcn)) {
490 if (likely(rl->lcn >= LCN_HOLE)) {
498 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
499 if (likely(rl->lcn == LCN_ENOENT))
505 if (!err && !is_retry) {
507 * If the search context is invalid we cannot map the unmapped
510 if (IS_ERR(ctx->mrec))
511 err = PTR_ERR(ctx->mrec);
514 * The @vcn is in an unmapped region, map the runlist
517 err = ntfs_map_runlist_nolock(ni, vcn, ctx);
528 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
533 * ntfs_attr_find - find (next) attribute in mft record
534 * @type: attribute type to find
535 * @name: attribute name to find (optional, i.e. NULL means don't care)
536 * @name_len: attribute name length (only needed if @name present)
537 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
538 * @val: attribute value to find (optional, resident attributes only)
539 * @val_len: attribute value length
540 * @ctx: search context with mft record and attribute to search from
542 * You should not need to call this function directly. Use ntfs_attr_lookup()
545 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
546 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
547 * attribute of @type, optionally @name and @val.
549 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
550 * point to the found attribute.
552 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
553 * @ctx->attr will point to the attribute before which the attribute being
554 * searched for would need to be inserted if such an action were to be desired.
556 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
557 * undefined and in particular do not rely on it not changing.
559 * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
560 * is FALSE, the search begins after @ctx->attr.
562 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
563 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
564 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
565 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
566 * sensitive. When @name is present, @name_len is the @name length in Unicode
569 * If @name is not present (NULL), we assume that the unnamed attribute is
570 * being searched for.
572 * Finally, the resident attribute value @val is looked for, if present. If
573 * @val is not present (NULL), @val_len is ignored.
575 * ntfs_attr_find() only searches the specified mft record and it ignores the
576 * presence of an attribute list attribute (unless it is the one being searched
577 * for, obviously). If you need to take attribute lists into consideration,
578 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
579 * use ntfs_attr_find() to search for extent records of non-resident
580 * attributes, as extents with lowest_vcn != 0 are usually described by the
581 * attribute list attribute only. - Note that it is possible that the first
582 * extent is only in the attribute list while the last extent is in the base
583 * mft record, so do not rely on being able to find the first extent in the
586 * Warning: Never use @val when looking for attribute types which can be
587 * non-resident as this most likely will result in a crash!
589 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
590 const u32 name_len, const IGNORE_CASE_BOOL ic,
591 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
594 ntfs_volume *vol = ctx->ntfs_ino->vol;
595 ntfschar *upcase = vol->upcase;
596 u32 upcase_len = vol->upcase_len;
599 * Iterate over attributes in mft record starting at @ctx->attr, or the
600 * attribute following that, if @ctx->is_first is TRUE.
604 ctx->is_first = FALSE;
606 a = (ATTR_RECORD*)((u8*)ctx->attr +
607 le32_to_cpu(ctx->attr->length));
608 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
609 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
610 le32_to_cpu(ctx->mrec->bytes_allocated))
613 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
616 if (unlikely(!a->length))
621 * If @name is present, compare the two names. If @name is
622 * missing, assume we want an unnamed attribute.
625 /* The search failed if the found attribute is named. */
628 } else if (!ntfs_are_names_equal(name, name_len,
629 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
630 a->name_length, ic, upcase, upcase_len)) {
633 rc = ntfs_collate_names(name, name_len,
635 le16_to_cpu(a->name_offset)),
636 a->name_length, 1, IGNORE_CASE,
639 * If @name collates before a->name, there is no
640 * matching attribute.
644 /* If the strings are not equal, continue search. */
647 rc = ntfs_collate_names(name, name_len,
649 le16_to_cpu(a->name_offset)),
650 a->name_length, 1, CASE_SENSITIVE,
658 * The names match or @name not present and attribute is
659 * unnamed. If no @val specified, we have found the attribute
664 /* @val is present; compare values. */
668 rc = memcmp(val, (u8*)a + le16_to_cpu(
669 a->data.resident.value_offset),
670 min_t(u32, val_len, le32_to_cpu(
671 a->data.resident.value_length)));
673 * If @val collates before the current attribute's
674 * value, there is no matching attribute.
680 a->data.resident.value_length);
689 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
695 * load_attribute_list - load an attribute list into memory
696 * @vol: ntfs volume from which to read
697 * @runlist: runlist of the attribute list
698 * @al_start: destination buffer
699 * @size: size of the destination buffer in bytes
700 * @initialized_size: initialized size of the attribute list
702 * Walk the runlist @runlist and load all clusters from it copying them into
703 * the linear buffer @al. The maximum number of bytes copied to @al is @size
704 * bytes. Note, @size does not need to be a multiple of the cluster size. If
705 * @initialized_size is less than @size, the region in @al between
706 * @initialized_size and @size will be zeroed and not read from disk.
708 * Return 0 on success or -errno on error.
710 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
711 const s64 size, const s64 initialized_size)
715 u8 *al_end = al + initialized_size;
717 struct buffer_head *bh;
718 struct super_block *sb;
719 unsigned long block_size;
720 unsigned long block, max_block;
722 unsigned char block_size_bits;
724 ntfs_debug("Entering.");
725 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
726 initialized_size > size)
728 if (!initialized_size) {
733 block_size = sb->s_blocksize;
734 block_size_bits = sb->s_blocksize_bits;
735 down_read(&runlist->lock);
738 ntfs_error(sb, "Cannot read attribute list since runlist is "
742 /* Read all clusters specified by the runlist one run at a time. */
744 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
745 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
746 (unsigned long long)rl->vcn,
747 (unsigned long long)lcn);
748 /* The attribute list cannot be sparse. */
750 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
751 "read attribute list.");
754 block = lcn << vol->cluster_size_bits >> block_size_bits;
755 /* Read the run from device in chunks of block_size bytes. */
756 max_block = block + (rl->length << vol->cluster_size_bits >>
758 ntfs_debug("max_block = 0x%lx.", max_block);
760 ntfs_debug("Reading block = 0x%lx.", block);
761 bh = sb_bread(sb, block);
763 ntfs_error(sb, "sb_bread() failed. Cannot "
764 "read attribute list.");
767 if (al + block_size >= al_end)
769 memcpy(al, bh->b_data, block_size);
772 } while (++block < max_block);
775 if (initialized_size < size) {
777 memset(al_start + initialized_size, 0, size - initialized_size);
780 up_read(&runlist->lock);
787 * Note: The attribute list can be smaller than its allocation
788 * by multiple clusters. This has been encountered by at least
789 * two people running Windows XP, thus we cannot do any
790 * truncation sanity checking here. (AIA)
792 memcpy(al, bh->b_data, al_end - al);
794 if (initialized_size < size)
800 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
808 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
809 * @type: attribute type to find
810 * @name: attribute name to find (optional, i.e. NULL means don't care)
811 * @name_len: attribute name length (only needed if @name present)
812 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
813 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
814 * @val: attribute value to find (optional, resident attributes only)
815 * @val_len: attribute value length
816 * @ctx: search context with mft record and attribute to search from
818 * You should not need to call this function directly. Use ntfs_attr_lookup()
821 * Find an attribute by searching the attribute list for the corresponding
822 * attribute list entry. Having found the entry, map the mft record if the
823 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
824 * in there and return it.
826 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
827 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
828 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
829 * then the base inode).
831 * After finishing with the attribute/mft record you need to call
832 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
833 * mapped inodes, etc).
835 * If the attribute is found, ntfs_external_attr_find() returns 0 and
836 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
837 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
838 * the attribute list entry for the attribute.
840 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
841 * @ctx->attr will point to the attribute in the base mft record before which
842 * the attribute being searched for would need to be inserted if such an action
843 * were to be desired. @ctx->mrec will point to the mft record in which
844 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
845 * entry of the attribute before which the attribute being searched for would
846 * need to be inserted if such an action were to be desired.
848 * Thus to insert the not found attribute, one wants to add the attribute to
849 * @ctx->mrec (the base mft record) and if there is not enough space, the
850 * attribute should be placed in a newly allocated extent mft record. The
851 * attribute list entry for the inserted attribute should be inserted in the
852 * attribute list attribute at @ctx->al_entry.
854 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
855 * @ctx->attr is undefined and in particular do not rely on it not changing.
857 static int ntfs_external_attr_find(const ATTR_TYPE type,
858 const ntfschar *name, const u32 name_len,
859 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
860 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
862 ntfs_inode *base_ni, *ni;
864 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
865 u8 *al_start, *al_end;
870 static const char *es = " Unmount and run chkdsk.";
873 base_ni = ctx->base_ntfs_ino;
874 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
876 /* First call happens with the base mft record. */
877 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
878 ctx->base_mrec = ctx->mrec;
881 ctx->base_attr = ctx->attr;
885 al_start = base_ni->attr_list;
886 al_end = al_start + base_ni->attr_list_size;
888 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
890 * Iterate over entries in attribute list starting at @ctx->al_entry,
891 * or the entry following that, if @ctx->is_first is TRUE.
894 al_entry = ctx->al_entry;
895 ctx->is_first = FALSE;
897 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
898 le16_to_cpu(ctx->al_entry->length));
899 for (;; al_entry = next_al_entry) {
900 /* Out of bounds check. */
901 if ((u8*)al_entry < base_ni->attr_list ||
902 (u8*)al_entry > al_end)
903 break; /* Inode is corrupt. */
904 ctx->al_entry = al_entry;
905 /* Catch the end of the attribute list. */
906 if ((u8*)al_entry == al_end)
908 if (!al_entry->length)
910 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
911 le16_to_cpu(al_entry->length) > al_end)
913 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
914 le16_to_cpu(al_entry->length));
915 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
917 if (type != al_entry->type)
920 * If @name is present, compare the two names. If @name is
921 * missing, assume we want an unnamed attribute.
923 al_name_len = al_entry->name_length;
924 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
928 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
929 name_len, ic, vol->upcase, vol->upcase_len)) {
932 rc = ntfs_collate_names(name, name_len, al_name,
933 al_name_len, 1, IGNORE_CASE,
934 vol->upcase, vol->upcase_len);
936 * If @name collates before al_name, there is no
937 * matching attribute.
941 /* If the strings are not equal, continue search. */
945 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
946 * that is inconsistent with ntfs_attr_find(). The
947 * subsequent rc checks were also different. Perhaps I
948 * made a mistake in one of the two. Need to recheck
949 * which is correct or at least see what is going on...
952 rc = ntfs_collate_names(name, name_len, al_name,
953 al_name_len, 1, CASE_SENSITIVE,
954 vol->upcase, vol->upcase_len);
961 * The names match or @name not present and attribute is
962 * unnamed. Now check @lowest_vcn. Continue search if the
963 * next attribute list entry still fits @lowest_vcn. Otherwise
964 * we have reached the right one or the search has failed.
966 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
967 (u8*)next_al_entry + 6 < al_end &&
968 (u8*)next_al_entry + le16_to_cpu(
969 next_al_entry->length) <= al_end &&
970 sle64_to_cpu(next_al_entry->lowest_vcn) <=
972 next_al_entry->type == al_entry->type &&
973 next_al_entry->name_length == al_name_len &&
974 ntfs_are_names_equal((ntfschar*)((u8*)
976 next_al_entry->name_offset),
977 next_al_entry->name_length,
978 al_name, al_name_len, CASE_SENSITIVE,
979 vol->upcase, vol->upcase_len))
981 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
982 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
983 ntfs_error(vol->sb, "Found stale mft "
984 "reference in attribute list "
985 "of base inode 0x%lx.%s",
986 base_ni->mft_no, es);
990 } else { /* Mft references do not match. */
991 /* If there is a mapped record unmap it first. */
993 unmap_extent_mft_record(ni);
994 /* Do we want the base record back? */
995 if (MREF_LE(al_entry->mft_reference) ==
997 ni = ctx->ntfs_ino = base_ni;
998 ctx->mrec = ctx->base_mrec;
1000 /* We want an extent record. */
1001 ctx->mrec = map_extent_mft_record(base_ni,
1003 al_entry->mft_reference), &ni);
1004 if (IS_ERR(ctx->mrec)) {
1005 ntfs_error(vol->sb, "Failed to map "
1006 "extent mft record "
1007 "0x%lx of base inode "
1011 base_ni->mft_no, es);
1012 err = PTR_ERR(ctx->mrec);
1015 /* Cause @ctx to be sanitized below. */
1021 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1022 le16_to_cpu(ctx->mrec->attrs_offset));
1025 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1026 * mft record containing the attribute represented by the
1030 * We could call into ntfs_attr_find() to find the right
1031 * attribute in this mft record but this would be less
1032 * efficient and not quite accurate as ntfs_attr_find() ignores
1033 * the attribute instance numbers for example which become
1034 * important when one plays with attribute lists. Also,
1035 * because a proper match has been found in the attribute list
1036 * entry above, the comparison can now be optimized. So it is
1037 * worth re-implementing a simplified ntfs_attr_find() here.
1041 * Use a manual loop so we can still use break and continue
1042 * with the same meanings as above.
1045 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1046 le32_to_cpu(ctx->mrec->bytes_allocated))
1048 if (a->type == AT_END)
1052 if (al_entry->instance != a->instance)
1055 * If the type and/or the name are mismatched between the
1056 * attribute list entry and the attribute record, there is
1057 * corruption so we break and return error EIO.
1059 if (al_entry->type != a->type)
1061 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1062 le16_to_cpu(a->name_offset)), a->name_length,
1063 al_name, al_name_len, CASE_SENSITIVE,
1064 vol->upcase, vol->upcase_len))
1068 * If no @val specified or @val specified and it matches, we
1071 if (!val || (!a->non_resident && le32_to_cpu(
1072 a->data.resident.value_length) == val_len &&
1074 le16_to_cpu(a->data.resident.value_offset),
1076 ntfs_debug("Done, found.");
1080 /* Proceed to the next attribute in the current mft record. */
1081 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1082 goto do_next_attr_loop;
1085 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1086 "attribute list attribute.%s", base_ni->mft_no,
1090 if (ni != base_ni) {
1092 unmap_extent_mft_record(ni);
1093 ctx->ntfs_ino = base_ni;
1094 ctx->mrec = ctx->base_mrec;
1095 ctx->attr = ctx->base_attr;
1102 * If we were looking for AT_END, we reset the search context @ctx and
1103 * use ntfs_attr_find() to seek to the end of the base mft record.
1105 if (type == AT_END) {
1106 ntfs_attr_reinit_search_ctx(ctx);
1107 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1111 * The attribute was not found. Before we return, we want to ensure
1112 * @ctx->mrec and @ctx->attr indicate the position at which the
1113 * attribute should be inserted in the base mft record. Since we also
1114 * want to preserve @ctx->al_entry we cannot reinitialize the search
1115 * context using ntfs_attr_reinit_search_ctx() as this would set
1116 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
1117 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
1118 * @ctx->al_entry as the remaining fields (base_*) are identical to
1119 * their non base_ counterparts and we cannot set @ctx->base_attr
1120 * correctly yet as we do not know what @ctx->attr will be set to by
1121 * the call to ntfs_attr_find() below.
1124 unmap_extent_mft_record(ni);
1125 ctx->mrec = ctx->base_mrec;
1126 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1127 le16_to_cpu(ctx->mrec->attrs_offset));
1128 ctx->is_first = TRUE;
1129 ctx->ntfs_ino = base_ni;
1130 ctx->base_ntfs_ino = NULL;
1131 ctx->base_mrec = NULL;
1132 ctx->base_attr = NULL;
1134 * In case there are multiple matches in the base mft record, need to
1135 * keep enumerating until we get an attribute not found response (or
1136 * another error), otherwise we would keep returning the same attribute
1137 * over and over again and all programs using us for enumeration would
1138 * lock up in a tight loop.
1141 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1144 ntfs_debug("Done, not found.");
1149 * ntfs_attr_lookup - find an attribute in an ntfs inode
1150 * @type: attribute type to find
1151 * @name: attribute name to find (optional, i.e. NULL means don't care)
1152 * @name_len: attribute name length (only needed if @name present)
1153 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1154 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1155 * @val: attribute value to find (optional, resident attributes only)
1156 * @val_len: attribute value length
1157 * @ctx: search context with mft record and attribute to search from
1159 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1160 * be the base mft record and @ctx must have been obtained from a call to
1161 * ntfs_attr_get_search_ctx().
1163 * This function transparently handles attribute lists and @ctx is used to
1164 * continue searches where they were left off at.
1166 * After finishing with the attribute/mft record you need to call
1167 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1168 * mapped inodes, etc).
1170 * Return 0 if the search was successful and -errno if not.
1172 * When 0, @ctx->attr is the found attribute and it is in mft record
1173 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
1174 * the attribute list entry of the found attribute.
1176 * When -ENOENT, @ctx->attr is the attribute which collates just after the
1177 * attribute being searched for, i.e. if one wants to add the attribute to the
1178 * mft record this is the correct place to insert it into. If an attribute
1179 * list attribute is present, @ctx->al_entry is the attribute list entry which
1180 * collates just after the attribute list entry of the attribute being searched
1181 * for, i.e. if one wants to add the attribute to the mft record this is the
1182 * correct place to insert its attribute list entry into.
1184 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
1185 * then undefined and in particular you should not rely on it not changing.
1187 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1188 const u32 name_len, const IGNORE_CASE_BOOL ic,
1189 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1190 ntfs_attr_search_ctx *ctx)
1192 ntfs_inode *base_ni;
1194 ntfs_debug("Entering.");
1195 BUG_ON(IS_ERR(ctx->mrec));
1196 if (ctx->base_ntfs_ino)
1197 base_ni = ctx->base_ntfs_ino;
1199 base_ni = ctx->ntfs_ino;
1200 /* Sanity check, just for debugging really. */
1202 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1203 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1205 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1210 * ntfs_attr_init_search_ctx - initialize an attribute search context
1211 * @ctx: attribute search context to initialize
1212 * @ni: ntfs inode with which to initialize the search context
1213 * @mrec: mft record with which to initialize the search context
1215 * Initialize the attribute search context @ctx with @ni and @mrec.
1217 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1218 ntfs_inode *ni, MFT_RECORD *mrec)
1220 *ctx = (ntfs_attr_search_ctx) {
1222 /* Sanity checks are performed elsewhere. */
1223 .attr = (ATTR_RECORD*)((u8*)mrec +
1224 le16_to_cpu(mrec->attrs_offset)),
1231 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1232 * @ctx: attribute search context to reinitialize
1234 * Reinitialize the attribute search context @ctx, unmapping an associated
1235 * extent mft record if present, and initialize the search context again.
1237 * This is used when a search for a new attribute is being started to reset
1238 * the search context to the beginning.
1240 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1242 if (likely(!ctx->base_ntfs_ino)) {
1243 /* No attribute list. */
1244 ctx->is_first = TRUE;
1245 /* Sanity checks are performed elsewhere. */
1246 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1247 le16_to_cpu(ctx->mrec->attrs_offset));
1249 * This needs resetting due to ntfs_external_attr_find() which
1250 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1252 ctx->al_entry = NULL;
1254 } /* Attribute list. */
1255 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1256 unmap_extent_mft_record(ctx->ntfs_ino);
1257 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1262 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1263 * @ni: ntfs inode with which to initialize the search context
1264 * @mrec: mft record with which to initialize the search context
1266 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1267 * and return it. Return NULL if allocation failed.
1269 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1271 ntfs_attr_search_ctx *ctx;
1273 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
1275 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1280 * ntfs_attr_put_search_ctx - release an attribute search context
1281 * @ctx: attribute search context to free
1283 * Release the attribute search context @ctx, unmapping an associated extent
1284 * mft record if present.
1286 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1288 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1289 unmap_extent_mft_record(ctx->ntfs_ino);
1290 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1297 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1298 * @vol: ntfs volume to which the attribute belongs
1299 * @type: attribute type which to find
1301 * Search for the attribute definition record corresponding to the attribute
1302 * @type in the $AttrDef system file.
1304 * Return the attribute type definition record if found and NULL if not found.
1306 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1307 const ATTR_TYPE type)
1311 BUG_ON(!vol->attrdef);
1313 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1314 vol->attrdef_size && ad->type; ++ad) {
1315 /* We have not found it yet, carry on searching. */
1316 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1318 /* We found the attribute; return it. */
1319 if (likely(ad->type == type))
1321 /* We have gone too far already. No point in continuing. */
1324 /* Attribute not found. */
1325 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1331 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1332 * @vol: ntfs volume to which the attribute belongs
1333 * @type: attribute type which to check
1334 * @size: size which to check
1336 * Check whether the @size in bytes is valid for an attribute of @type on the
1337 * ntfs volume @vol. This information is obtained from $AttrDef system file.
1339 * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1340 * listed in $AttrDef.
1342 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1349 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1350 * listed in $AttrDef.
1352 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1354 /* Get the $AttrDef entry for the attribute @type. */
1355 ad = ntfs_attr_find_in_attrdef(vol, type);
1358 /* Do the bounds check. */
1359 if (((sle64_to_cpu(ad->min_size) > 0) &&
1360 size < sle64_to_cpu(ad->min_size)) ||
1361 ((sle64_to_cpu(ad->max_size) > 0) && size >
1362 sle64_to_cpu(ad->max_size)))
1368 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1369 * @vol: ntfs volume to which the attribute belongs
1370 * @type: attribute type which to check
1372 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1373 * be non-resident. This information is obtained from $AttrDef system file.
1375 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1376 * -ENOENT if the attribute is not listed in $AttrDef.
1378 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1382 /* Find the attribute definition record in $AttrDef. */
1383 ad = ntfs_attr_find_in_attrdef(vol, type);
1386 /* Check the flags and return the result. */
1387 if (ad->flags & ATTR_DEF_RESIDENT)
1393 * ntfs_attr_can_be_resident - check if an attribute can be resident
1394 * @vol: ntfs volume to which the attribute belongs
1395 * @type: attribute type which to check
1397 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1398 * be resident. This information is derived from our ntfs knowledge and may
1399 * not be completely accurate, especially when user defined attributes are
1400 * present. Basically we allow everything to be resident except for index
1401 * allocation and $EA attributes.
1403 * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1405 * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1406 * otherwise windows will not boot (blue screen of death)! We cannot
1407 * check for this here as we do not know which inode's $Bitmap is
1408 * being asked about so the caller needs to special case this.
1410 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1412 if (type == AT_INDEX_ALLOCATION || type == AT_EA)
1418 * ntfs_attr_record_resize - resize an attribute record
1419 * @m: mft record containing attribute record
1420 * @a: attribute record to resize
1421 * @new_size: new size in bytes to which to resize the attribute record @a
1423 * Resize the attribute record @a, i.e. the resident part of the attribute, in
1424 * the mft record @m to @new_size bytes.
1426 * Return 0 on success and -errno on error. The following error codes are
1428 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1430 * Note: On error, no modifications have been performed whatsoever.
1432 * Warning: If you make a record smaller without having copied all the data you
1433 * are interested in the data may be overwritten.
1435 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1437 ntfs_debug("Entering for new_size %u.", new_size);
1438 /* Align to 8 bytes if it is not already done. */
1440 new_size = (new_size + 7) & ~7;
1441 /* If the actual attribute length has changed, move things around. */
1442 if (new_size != le32_to_cpu(a->length)) {
1443 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1444 le32_to_cpu(a->length) + new_size;
1445 /* Not enough space in this mft record. */
1446 if (new_muse > le32_to_cpu(m->bytes_allocated))
1448 /* Move attributes following @a to their new location. */
1449 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1450 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1451 (u8*)m) - le32_to_cpu(a->length));
1452 /* Adjust @m to reflect the change in used space. */
1453 m->bytes_in_use = cpu_to_le32(new_muse);
1454 /* Adjust @a to reflect the new size. */
1455 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1456 a->length = cpu_to_le32(new_size);
1462 * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1463 * @m: mft record containing attribute record
1464 * @a: attribute record whose value to resize
1465 * @new_size: new size in bytes to which to resize the attribute value of @a
1467 * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1468 * If the value is made bigger, the newly allocated space is cleared.
1470 * Return 0 on success and -errno on error. The following error codes are
1472 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1474 * Note: On error, no modifications have been performed whatsoever.
1476 * Warning: If you make a record smaller without having copied all the data you
1477 * are interested in the data may be overwritten.
1479 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1484 /* Resize the resident part of the attribute record. */
1485 if (ntfs_attr_record_resize(m, a,
1486 le16_to_cpu(a->data.resident.value_offset) + new_size))
1489 * The resize succeeded! If we made the attribute value bigger, clear
1490 * the area between the old size and @new_size.
1492 old_size = le32_to_cpu(a->data.resident.value_length);
1493 if (new_size > old_size)
1494 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1495 old_size, 0, new_size - old_size);
1496 /* Finally update the length of the attribute value. */
1497 a->data.resident.value_length = cpu_to_le32(new_size);
1502 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1503 * @ni: ntfs inode describing the attribute to convert
1504 * @data_size: size of the resident data to copy to the non-resident attribute
1506 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1509 * @data_size must be equal to the attribute value size. This is needed since
1510 * we need to know the size before we can map the mft record and our callers
1511 * always know it. The reason we cannot simply read the size from the vfs
1512 * inode i_size is that this is not necessarily uptodate. This happens when
1513 * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
1515 * Return 0 on success and -errno on error. The following error return codes
1517 * -EPERM - The attribute is not allowed to be non-resident.
1518 * -ENOMEM - Not enough memory.
1519 * -ENOSPC - Not enough disk space.
1520 * -EINVAL - Attribute not defined on the volume.
1521 * -EIO - I/o error or other error.
1522 * Note that -ENOSPC is also returned in the case that there is not enough
1523 * space in the mft record to do the conversion. This can happen when the mft
1524 * record is already very full. The caller is responsible for trying to make
1525 * space in the mft record and trying again. FIXME: Do we need a separate
1526 * error return code for this kind of -ENOSPC or is it always worth trying
1527 * again in case the attribute may then fit in a resident state so no need to
1528 * make it non-resident at all? Ho-hum... (AIA)
1530 * NOTE to self: No changes in the attribute list are required to move from
1531 * a resident to a non-resident attribute.
1533 * Locking: - The caller must hold i_sem on the inode.
1535 int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1538 struct inode *vi = VFS_I(ni);
1539 ntfs_volume *vol = ni->vol;
1540 ntfs_inode *base_ni;
1543 ntfs_attr_search_ctx *ctx;
1545 runlist_element *rl;
1547 unsigned long flags;
1548 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1550 u8 old_res_attr_flags;
1552 /* Check that the attribute is allowed to be non-resident. */
1553 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1554 if (unlikely(err)) {
1556 ntfs_debug("Attribute is not allowed to be "
1559 ntfs_debug("Attribute not defined on the NTFS "
1564 * FIXME: Compressed and encrypted attributes are not supported when
1565 * writing and we should never have gotten here for them.
1567 BUG_ON(NInoCompressed(ni));
1568 BUG_ON(NInoEncrypted(ni));
1570 * The size needs to be aligned to a cluster boundary for allocation
1573 new_size = (data_size + vol->cluster_size - 1) &
1574 ~(vol->cluster_size - 1);
1577 * Will need the page later and since the page lock nests
1578 * outside all ntfs locks, we need to get the page now.
1580 page = find_or_create_page(vi->i_mapping, 0,
1581 mapping_gfp_mask(vi->i_mapping));
1582 if (unlikely(!page))
1584 /* Start by allocating clusters to hold the attribute value. */
1585 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1586 vol->cluster_size_bits, -1, DATA_ZONE, TRUE);
1589 ntfs_debug("Failed to allocate cluster%s, error code "
1591 vol->cluster_size_bits) > 1 ? "s" : "",
1599 /* Determine the size of the mapping pairs array. */
1600 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1601 if (unlikely(mp_size < 0)) {
1603 ntfs_debug("Failed to get size for mapping pairs array, error "
1607 down_write(&ni->runlist.lock);
1611 base_ni = ni->ext.base_ntfs_ino;
1612 m = map_mft_record(base_ni);
1619 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1620 if (unlikely(!ctx)) {
1624 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1625 CASE_SENSITIVE, 0, NULL, 0, ctx);
1626 if (unlikely(err)) {
1633 BUG_ON(NInoNonResident(ni));
1634 BUG_ON(a->non_resident);
1636 * Calculate new offsets for the name and the mapping pairs array.
1638 if (NInoSparse(ni) || NInoCompressed(ni))
1639 name_ofs = (offsetof(ATTR_REC,
1640 data.non_resident.compressed_size) +
1641 sizeof(a->data.non_resident.compressed_size) +
1644 name_ofs = (offsetof(ATTR_REC,
1645 data.non_resident.compressed_size) + 7) & ~7;
1646 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1648 * Determine the size of the resident part of the now non-resident
1651 arec_size = (mp_ofs + mp_size + 7) & ~7;
1653 * If the page is not uptodate bring it uptodate by copying from the
1656 attr_size = le32_to_cpu(a->data.resident.value_length);
1657 BUG_ON(attr_size != data_size);
1658 if (page && !PageUptodate(page)) {
1659 kaddr = kmap_atomic(page, KM_USER0);
1660 memcpy(kaddr, (u8*)a +
1661 le16_to_cpu(a->data.resident.value_offset),
1663 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1664 kunmap_atomic(kaddr, KM_USER0);
1665 flush_dcache_page(page);
1666 SetPageUptodate(page);
1668 /* Backup the attribute flag. */
1669 old_res_attr_flags = a->data.resident.flags;
1670 /* Resize the resident part of the attribute record. */
1671 err = ntfs_attr_record_resize(m, a, arec_size);
1675 * Convert the resident part of the attribute record to describe a
1676 * non-resident attribute.
1678 a->non_resident = 1;
1679 /* Move the attribute name if it exists and update the offset. */
1681 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1682 a->name_length * sizeof(ntfschar));
1683 a->name_offset = cpu_to_le16(name_ofs);
1684 /* Setup the fields specific to non-resident attributes. */
1685 a->data.non_resident.lowest_vcn = 0;
1686 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1687 vol->cluster_size_bits);
1688 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1689 memset(&a->data.non_resident.reserved, 0,
1690 sizeof(a->data.non_resident.reserved));
1691 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1692 a->data.non_resident.data_size =
1693 a->data.non_resident.initialized_size =
1694 cpu_to_sle64(attr_size);
1695 if (NInoSparse(ni) || NInoCompressed(ni)) {
1696 a->data.non_resident.compression_unit = 4;
1697 a->data.non_resident.compressed_size =
1698 a->data.non_resident.allocated_size;
1700 a->data.non_resident.compression_unit = 0;
1701 /* Generate the mapping pairs array into the attribute record. */
1702 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1703 arec_size - mp_ofs, rl, 0, -1, NULL);
1704 if (unlikely(err)) {
1705 ntfs_debug("Failed to build mapping pairs, error code %i.",
1709 /* Setup the in-memory attribute structure to be non-resident. */
1710 ni->runlist.rl = rl;
1711 write_lock_irqsave(&ni->size_lock, flags);
1712 ni->allocated_size = new_size;
1713 if (NInoSparse(ni) || NInoCompressed(ni)) {
1714 ni->itype.compressed.size = ni->allocated_size;
1715 ni->itype.compressed.block_size = 1U <<
1716 (a->data.non_resident.compression_unit +
1717 vol->cluster_size_bits);
1718 ni->itype.compressed.block_size_bits =
1719 ffs(ni->itype.compressed.block_size) - 1;
1720 ni->itype.compressed.block_clusters = 1U <<
1721 a->data.non_resident.compression_unit;
1722 vi->i_blocks = ni->itype.compressed.size >> 9;
1724 vi->i_blocks = ni->allocated_size >> 9;
1725 write_unlock_irqrestore(&ni->size_lock, flags);
1727 * This needs to be last since the address space operations ->readpage
1728 * and ->writepage can run concurrently with us as they are not
1729 * serialized on i_sem. Note, we are not allowed to fail once we flip
1730 * this switch, which is another reason to do this last.
1732 NInoSetNonResident(ni);
1733 /* Mark the mft record dirty, so it gets written back. */
1734 flush_dcache_mft_record_page(ctx->ntfs_ino);
1735 mark_mft_record_dirty(ctx->ntfs_ino);
1736 ntfs_attr_put_search_ctx(ctx);
1737 unmap_mft_record(base_ni);
1738 up_write(&ni->runlist.lock);
1740 set_page_dirty(page);
1742 mark_page_accessed(page);
1743 page_cache_release(page);
1745 ntfs_debug("Done.");
1748 /* Convert the attribute back into a resident attribute. */
1749 a->non_resident = 0;
1750 /* Move the attribute name if it exists and update the offset. */
1751 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1752 sizeof(a->data.resident.reserved) + 7) & ~7;
1754 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1755 a->name_length * sizeof(ntfschar));
1756 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1757 a->name_offset = cpu_to_le16(name_ofs);
1758 arec_size = (mp_ofs + attr_size + 7) & ~7;
1759 /* Resize the resident part of the attribute record. */
1760 err2 = ntfs_attr_record_resize(m, a, arec_size);
1761 if (unlikely(err2)) {
1763 * This cannot happen (well if memory corruption is at work it
1764 * could happen in theory), but deal with it as well as we can.
1765 * If the old size is too small, truncate the attribute,
1766 * otherwise simply give it a larger allocated size.
1767 * FIXME: Should check whether chkdsk complains when the
1768 * allocated size is much bigger than the resident value size.
1770 arec_size = le32_to_cpu(a->length);
1771 if ((mp_ofs + attr_size) > arec_size) {
1773 attr_size = arec_size - mp_ofs;
1774 ntfs_error(vol->sb, "Failed to undo partial resident "
1775 "to non-resident attribute "
1776 "conversion. Truncating inode 0x%lx, "
1777 "attribute type 0x%x from %i bytes to "
1778 "%i bytes to maintain metadata "
1779 "consistency. THIS MEANS YOU ARE "
1780 "LOSING %i BYTES DATA FROM THIS %s.",
1782 (unsigned)le32_to_cpu(ni->type),
1783 err2, attr_size, err2 - attr_size,
1784 ((ni->type == AT_DATA) &&
1785 !ni->name_len) ? "FILE": "ATTRIBUTE");
1786 write_lock_irqsave(&ni->size_lock, flags);
1787 ni->initialized_size = attr_size;
1788 i_size_write(vi, attr_size);
1789 write_unlock_irqrestore(&ni->size_lock, flags);
1792 /* Setup the fields specific to resident attributes. */
1793 a->data.resident.value_length = cpu_to_le32(attr_size);
1794 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1795 a->data.resident.flags = old_res_attr_flags;
1796 memset(&a->data.resident.reserved, 0,
1797 sizeof(a->data.resident.reserved));
1798 /* Copy the data from the page back to the attribute value. */
1800 kaddr = kmap_atomic(page, KM_USER0);
1801 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1802 kunmap_atomic(kaddr, KM_USER0);
1804 /* Setup the allocated size in the ntfs inode in case it changed. */
1805 write_lock_irqsave(&ni->size_lock, flags);
1806 ni->allocated_size = arec_size - mp_ofs;
1807 write_unlock_irqrestore(&ni->size_lock, flags);
1808 /* Mark the mft record dirty, so it gets written back. */
1809 flush_dcache_mft_record_page(ctx->ntfs_ino);
1810 mark_mft_record_dirty(ctx->ntfs_ino);
1813 ntfs_attr_put_search_ctx(ctx);
1815 unmap_mft_record(base_ni);
1816 ni->runlist.rl = NULL;
1817 up_write(&ni->runlist.lock);
1820 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1821 ntfs_error(vol->sb, "Failed to release allocated "
1822 "cluster(s) in error code path. Run "
1823 "chkdsk to recover the lost "
1830 page_cache_release(page);
1838 * ntfs_attr_extend_allocation - extend the allocated space of an attribute
1839 * @ni: ntfs inode of the attribute whose allocation to extend
1840 * @new_alloc_size: new size in bytes to which to extend the allocation to
1841 * @new_data_size: new size in bytes to which to extend the data to
1842 * @data_start: beginning of region which is required to be non-sparse
1844 * Extend the allocated space of an attribute described by the ntfs inode @ni
1845 * to @new_alloc_size bytes. If @data_start is -1, the whole extension may be
1846 * implemented as a hole in the file (as long as both the volume and the ntfs
1847 * inode @ni have sparse support enabled). If @data_start is >= 0, then the
1848 * region between the old allocated size and @data_start - 1 may be made sparse
1849 * but the regions between @data_start and @new_alloc_size must be backed by
1852 * If @new_data_size is -1, it is ignored. If it is >= 0, then the data size
1853 * of the attribute is extended to @new_data_size. Note that the i_size of the
1854 * vfs inode is not updated. Only the data size in the base attribute record
1855 * is updated. The caller has to update i_size separately if this is required.
1856 * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
1857 * size as well as for @new_data_size to be greater than @new_alloc_size.
1859 * For resident attributes this involves resizing the attribute record and if
1860 * necessary moving it and/or other attributes into extent mft records and/or
1861 * converting the attribute to a non-resident attribute which in turn involves
1862 * extending the allocation of a non-resident attribute as described below.
1864 * For non-resident attributes this involves allocating clusters in the data
1865 * zone on the volume (except for regions that are being made sparse) and
1866 * extending the run list to describe the allocated clusters as well as
1867 * updating the mapping pairs array of the attribute. This in turn involves
1868 * resizing the attribute record and if necessary moving it and/or other
1869 * attributes into extent mft records and/or splitting the attribute record
1870 * into multiple extent attribute records.
1872 * Also, the attribute list attribute is updated if present and in some of the
1873 * above cases (the ones where extent mft records/attributes come into play),
1874 * an attribute list attribute is created if not already present.
1876 * Return the new allocated size on success and -errno on error. In the case
1877 * that an error is encountered but a partial extension at least up to
1878 * @data_start (if present) is possible, the allocation is partially extended
1879 * and this is returned. This means the caller must check the returned size to
1880 * determine if the extension was partial. If @data_start is -1 then partial
1881 * allocations are not performed.
1883 * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
1885 * Locking: This function takes the runlist lock of @ni for writing as well as
1886 * locking the mft record of the base ntfs inode. These locks are maintained
1887 * throughout execution of the function. These locks are required so that the
1888 * attribute can be resized safely and so that it can for example be converted
1889 * from resident to non-resident safely.
1891 * TODO: At present attribute list attribute handling is not implemented.
1893 * TODO: At present it is not safe to call this function for anything other
1894 * than the $DATA attribute(s) of an uncompressed and unencrypted file.
1896 s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1897 const s64 new_data_size, const s64 data_start)
1900 s64 ll, allocated_size, start = data_start;
1901 struct inode *vi = VFS_I(ni);
1902 ntfs_volume *vol = ni->vol;
1903 ntfs_inode *base_ni;
1906 ntfs_attr_search_ctx *ctx;
1907 runlist_element *rl, *rl2;
1908 unsigned long flags;
1910 u32 attr_len = 0; /* Silence stupid gcc warning. */
1914 read_lock_irqsave(&ni->size_lock, flags);
1915 allocated_size = ni->allocated_size;
1916 read_unlock_irqrestore(&ni->size_lock, flags);
1917 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1918 "old_allocated_size 0x%llx, "
1919 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1920 "data_start 0x%llx.", vi->i_ino,
1921 (unsigned)le32_to_cpu(ni->type),
1922 (unsigned long long)allocated_size,
1923 (unsigned long long)new_alloc_size,
1924 (unsigned long long)new_data_size,
1925 (unsigned long long)start);
1929 * For non-resident attributes, @start and @new_size need to be aligned
1930 * to cluster boundaries for allocation purposes.
1932 if (NInoNonResident(ni)) {
1934 start &= ~(s64)vol->cluster_size_mask;
1935 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1936 ~(s64)vol->cluster_size_mask;
1938 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1939 /* Check if new size is allowed in $AttrDef. */
1940 err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1941 if (unlikely(err)) {
1942 /* Only emit errors when the write will fail completely. */
1943 read_lock_irqsave(&ni->size_lock, flags);
1944 allocated_size = ni->allocated_size;
1945 read_unlock_irqrestore(&ni->size_lock, flags);
1946 if (start < 0 || start >= allocated_size) {
1947 if (err == -ERANGE) {
1948 ntfs_error(vol->sb, "Cannot extend allocation "
1949 "of inode 0x%lx, attribute "
1950 "type 0x%x, because the new "
1951 "allocation would exceed the "
1952 "maximum allowed size for "
1953 "this attribute type.",
1954 vi->i_ino, (unsigned)
1955 le32_to_cpu(ni->type));
1957 ntfs_error(vol->sb, "Cannot extend allocation "
1958 "of inode 0x%lx, attribute "
1959 "type 0x%x, because this "
1960 "attribute type is not "
1961 "defined on the NTFS volume. "
1962 "Possible corruption! You "
1963 "should run chkdsk!",
1964 vi->i_ino, (unsigned)
1965 le32_to_cpu(ni->type));
1968 /* Translate error code to be POSIX conformant for write(2). */
1978 base_ni = ni->ext.base_ntfs_ino;
1980 * We will be modifying both the runlist (if non-resident) and the mft
1981 * record so lock them both down.
1983 down_write(&ni->runlist.lock);
1984 m = map_mft_record(base_ni);
1991 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1992 if (unlikely(!ctx)) {
1996 read_lock_irqsave(&ni->size_lock, flags);
1997 allocated_size = ni->allocated_size;
1998 read_unlock_irqrestore(&ni->size_lock, flags);
2000 * If non-resident, seek to the last extent. If resident, there is
2001 * only one extent, so seek to that.
2003 vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2006 * Abort if someone did the work whilst we waited for the locks. If we
2007 * just converted the attribute from resident to non-resident it is
2008 * likely that exactly this has happened already. We cannot quite
2009 * abort if we need to update the data size.
2011 if (unlikely(new_alloc_size <= allocated_size)) {
2012 ntfs_debug("Allocated size already exceeds requested size.");
2013 new_alloc_size = allocated_size;
2014 if (new_data_size < 0)
2017 * We want the first attribute extent so that we can update the
2022 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2023 CASE_SENSITIVE, vcn, NULL, 0, ctx);
2024 if (unlikely(err)) {
2031 /* Use goto to reduce indentation. */
2032 if (a->non_resident)
2033 goto do_non_resident_extend;
2034 BUG_ON(NInoNonResident(ni));
2035 /* The total length of the attribute value. */
2036 attr_len = le32_to_cpu(a->data.resident.value_length);
2038 * Extend the attribute record to be able to store the new attribute
2039 * size. ntfs_attr_record_resize() will not do anything if the size is
2042 if (new_alloc_size < vol->mft_record_size &&
2043 !ntfs_attr_record_resize(m, a,
2044 le16_to_cpu(a->data.resident.value_offset) +
2046 /* The resize succeeded! */
2047 write_lock_irqsave(&ni->size_lock, flags);
2048 ni->allocated_size = le32_to_cpu(a->length) -
2049 le16_to_cpu(a->data.resident.value_offset);
2050 write_unlock_irqrestore(&ni->size_lock, flags);
2051 if (new_data_size >= 0) {
2052 BUG_ON(new_data_size < attr_len);
2053 a->data.resident.value_length =
2054 cpu_to_le32((u32)new_data_size);
2059 * We have to drop all the locks so we can call
2060 * ntfs_attr_make_non_resident(). This could be optimised by try-
2061 * locking the first page cache page and only if that fails dropping
2062 * the locks, locking the page, and redoing all the locking and
2063 * lookups. While this would be a huge optimisation, it is not worth
2064 * it as this is definitely a slow code path.
2066 ntfs_attr_put_search_ctx(ctx);
2067 unmap_mft_record(base_ni);
2068 up_write(&ni->runlist.lock);
2070 * Not enough space in the mft record, try to make the attribute
2071 * non-resident and if successful restart the extension process.
2073 err = ntfs_attr_make_non_resident(ni, attr_len);
2077 * Could not make non-resident. If this is due to this not being
2078 * permitted for this attribute type or there not being enough space,
2079 * try to make other attributes non-resident. Otherwise fail.
2081 if (unlikely(err != -EPERM && err != -ENOSPC)) {
2082 /* Only emit errors when the write will fail completely. */
2083 read_lock_irqsave(&ni->size_lock, flags);
2084 allocated_size = ni->allocated_size;
2085 read_unlock_irqrestore(&ni->size_lock, flags);
2086 if (start < 0 || start >= allocated_size)
2087 ntfs_error(vol->sb, "Cannot extend allocation of "
2088 "inode 0x%lx, attribute type 0x%x, "
2089 "because the conversion from resident "
2090 "to non-resident attribute failed "
2091 "with error code %i.", vi->i_ino,
2092 (unsigned)le32_to_cpu(ni->type), err);
2097 /* TODO: Not implemented from here, abort. */
2098 read_lock_irqsave(&ni->size_lock, flags);
2099 allocated_size = ni->allocated_size;
2100 read_unlock_irqrestore(&ni->size_lock, flags);
2101 if (start < 0 || start >= allocated_size) {
2103 ntfs_error(vol->sb, "Not enough space in the mft "
2104 "record/on disk for the non-resident "
2105 "attribute value. This case is not "
2106 "implemented yet.");
2107 else /* if (err == -EPERM) */
2108 ntfs_error(vol->sb, "This attribute type may not be "
2109 "non-resident. This case is not "
2110 "implemented yet.");
2115 // TODO: Attempt to make other attributes non-resident.
2117 goto do_resident_extend;
2119 * Both the attribute list attribute and the standard information
2120 * attribute must remain in the base inode. Thus, if this is one of
2121 * these attributes, we have to try to move other attributes out into
2122 * extent mft records instead.
2124 if (ni->type == AT_ATTRIBUTE_LIST ||
2125 ni->type == AT_STANDARD_INFORMATION) {
2126 // TODO: Attempt to move other attributes into extent mft
2130 goto do_resident_extend;
2133 // TODO: Attempt to move this attribute to an extent mft record, but
2134 // only if it is not already the only attribute in an mft record in
2135 // which case there would be nothing to gain.
2138 goto do_resident_extend;
2139 /* There is nothing we can do to make enough space. )-: */
2142 do_non_resident_extend:
2143 BUG_ON(!NInoNonResident(ni));
2144 if (new_alloc_size == allocated_size) {
2149 * If the data starts after the end of the old allocation, this is a
2150 * $DATA attribute and sparse attributes are enabled on the volume and
2151 * for this inode, then create a sparse region between the old
2152 * allocated size and the start of the data. Otherwise simply proceed
2153 * with filling the whole space between the old allocated size and the
2154 * new allocated size with clusters.
2156 if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2157 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2159 // TODO: This is not implemented yet. We just fill in with real
2160 // clusters for now...
2161 ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
2162 "allocating real clusters instead.");
2164 rl = ni->runlist.rl;
2166 /* Seek to the end of the runlist. */
2170 /* If this attribute extent is not mapped, map it now. */
2171 if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2172 (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2173 (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2174 if (!rl && !allocated_size)
2176 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2179 if (start < 0 || start >= allocated_size)
2180 ntfs_error(vol->sb, "Cannot extend allocation "
2181 "of inode 0x%lx, attribute "
2182 "type 0x%x, because the "
2183 "mapping of a runlist "
2184 "fragment failed with error "
2185 "code %i.", vi->i_ino,
2186 (unsigned)le32_to_cpu(ni->type),
2192 ni->runlist.rl = rl;
2193 /* Seek to the end of the runlist. */
2198 * We now know the runlist of the last extent is mapped and @rl is at
2199 * the end of the runlist. We want to begin allocating clusters
2200 * starting at the last allocated cluster to reduce fragmentation. If
2201 * there are no valid LCNs in the attribute we let the cluster
2202 * allocator choose the starting cluster.
2204 /* If the last LCN is a hole or simillar seek back to last real LCN. */
2205 while (rl->lcn < 0 && rl > ni->runlist.rl)
2208 // FIXME: Need to implement partial allocations so at least part of the
2209 // write can be performed when start >= 0. (Needed for POSIX write(2)
2211 rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2212 (new_alloc_size - allocated_size) >>
2213 vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2214 rl->lcn + rl->length : -1, DATA_ZONE, TRUE);
2217 if (start < 0 || start >= allocated_size)
2218 ntfs_error(vol->sb, "Cannot extend allocation of "
2219 "inode 0x%lx, attribute type 0x%x, "
2220 "because the allocation of clusters "
2221 "failed with error code %i.", vi->i_ino,
2222 (unsigned)le32_to_cpu(ni->type), err);
2223 if (err != -ENOMEM && err != -ENOSPC)
2227 rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2230 if (start < 0 || start >= allocated_size)
2231 ntfs_error(vol->sb, "Cannot extend allocation of "
2232 "inode 0x%lx, attribute type 0x%x, "
2233 "because the runlist merge failed "
2234 "with error code %i.", vi->i_ino,
2235 (unsigned)le32_to_cpu(ni->type), err);
2238 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2239 ntfs_error(vol->sb, "Failed to release allocated "
2240 "cluster(s) in error code path. Run "
2241 "chkdsk to recover the lost "
2248 ni->runlist.rl = rl;
2249 ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2250 allocated_size) >> vol->cluster_size_bits);
2251 /* Find the runlist element with which the attribute extent starts. */
2252 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2253 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2255 BUG_ON(!rl2->length);
2256 BUG_ON(rl2->lcn < LCN_HOLE);
2258 /* Get the size for the new mapping pairs array for this extent. */
2259 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2260 if (unlikely(mp_size <= 0)) {
2262 if (start < 0 || start >= allocated_size)
2263 ntfs_error(vol->sb, "Cannot extend allocation of "
2264 "inode 0x%lx, attribute type 0x%x, "
2265 "because determining the size for the "
2266 "mapping pairs failed with error code "
2268 (unsigned)le32_to_cpu(ni->type), err);
2272 /* Extend the attribute record to fit the bigger mapping pairs array. */
2273 attr_len = le32_to_cpu(a->length);
2274 err = ntfs_attr_record_resize(m, a, mp_size +
2275 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2276 if (unlikely(err)) {
2277 BUG_ON(err != -ENOSPC);
2278 // TODO: Deal with this by moving this extent to a new mft
2279 // record or by starting a new extent in a new mft record,
2280 // possibly by extending this extent partially and filling it
2281 // and creating a new extent for the remainder, or by making
2282 // other attributes non-resident and/or by moving other
2283 // attributes out of this mft record.
2284 if (start < 0 || start >= allocated_size)
2285 ntfs_error(vol->sb, "Not enough space in the mft "
2286 "record for the extended attribute "
2287 "record. This case is not "
2288 "implemented yet.");
2293 /* Generate the mapping pairs array directly into the attr record. */
2294 err = ntfs_mapping_pairs_build(vol, (u8*)a +
2295 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2296 mp_size, rl2, ll, -1, NULL);
2297 if (unlikely(err)) {
2298 if (start < 0 || start >= allocated_size)
2299 ntfs_error(vol->sb, "Cannot extend allocation of "
2300 "inode 0x%lx, attribute type 0x%x, "
2301 "because building the mapping pairs "
2302 "failed with error code %i.", vi->i_ino,
2303 (unsigned)le32_to_cpu(ni->type), err);
2307 /* Update the highest_vcn. */
2308 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2309 vol->cluster_size_bits) - 1);
2311 * We now have extended the allocated size of the attribute. Reflect
2312 * this in the ntfs_inode structure and the attribute record.
2314 if (a->data.non_resident.lowest_vcn) {
2316 * We are not in the first attribute extent, switch to it, but
2317 * first ensure the changes will make it to disk later.
2319 flush_dcache_mft_record_page(ctx->ntfs_ino);
2320 mark_mft_record_dirty(ctx->ntfs_ino);
2321 ntfs_attr_reinit_search_ctx(ctx);
2322 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2323 CASE_SENSITIVE, 0, NULL, 0, ctx);
2325 goto restore_undo_alloc;
2326 /* @m is not used any more so no need to set it. */
2329 write_lock_irqsave(&ni->size_lock, flags);
2330 ni->allocated_size = new_alloc_size;
2331 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2333 * FIXME: This would fail if @ni is a directory, $MFT, or an index,
2334 * since those can have sparse/compressed set. For example can be
2335 * set compressed even though it is not compressed itself and in that
2336 * case the bit means that files are to be created compressed in the
2337 * directory... At present this is ok as this code is only called for
2338 * regular files, and only for their $DATA attribute(s).
2339 * FIXME: The calculation is wrong if we created a hole above. For now
2340 * it does not matter as we never create holes.
2342 if (NInoSparse(ni) || NInoCompressed(ni)) {
2343 ni->itype.compressed.size += new_alloc_size - allocated_size;
2344 a->data.non_resident.compressed_size =
2345 cpu_to_sle64(ni->itype.compressed.size);
2346 vi->i_blocks = ni->itype.compressed.size >> 9;
2348 vi->i_blocks = new_alloc_size >> 9;
2349 write_unlock_irqrestore(&ni->size_lock, flags);
2351 if (new_data_size >= 0) {
2352 BUG_ON(new_data_size <
2353 sle64_to_cpu(a->data.non_resident.data_size));
2354 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2357 /* Ensure the changes make it to disk. */
2358 flush_dcache_mft_record_page(ctx->ntfs_ino);
2359 mark_mft_record_dirty(ctx->ntfs_ino);
2361 ntfs_attr_put_search_ctx(ctx);
2362 unmap_mft_record(base_ni);
2363 up_write(&ni->runlist.lock);
2364 ntfs_debug("Done, new_allocated_size 0x%llx.",
2365 (unsigned long long)new_alloc_size);
2366 return new_alloc_size;
2368 if (start < 0 || start >= allocated_size)
2369 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2370 "of inode 0x%lx, attribute type 0x%x, because "
2371 "lookup of first attribute extent failed with "
2372 "error code %i.", vi->i_ino,
2373 (unsigned)le32_to_cpu(ni->type), err);
2376 ntfs_attr_reinit_search_ctx(ctx);
2377 if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2378 allocated_size >> vol->cluster_size_bits, NULL, 0,
2380 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2381 "attribute in error code path. Run chkdsk to "
2383 write_lock_irqsave(&ni->size_lock, flags);
2384 ni->allocated_size = new_alloc_size;
2386 * FIXME: This would fail if @ni is a directory... See above.
2387 * FIXME: The calculation is wrong if we created a hole above.
2388 * For now it does not matter as we never create holes.
2390 if (NInoSparse(ni) || NInoCompressed(ni)) {
2391 ni->itype.compressed.size += new_alloc_size -
2393 vi->i_blocks = ni->itype.compressed.size >> 9;
2395 vi->i_blocks = new_alloc_size >> 9;
2396 write_unlock_irqrestore(&ni->size_lock, flags);
2397 ntfs_attr_put_search_ctx(ctx);
2398 unmap_mft_record(base_ni);
2399 up_write(&ni->runlist.lock);
2401 * The only thing that is now wrong is the allocated size of the
2402 * base attribute extent which chkdsk should be able to fix.
2407 ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2408 (allocated_size >> vol->cluster_size_bits) - 1);
2410 ll = allocated_size >> vol->cluster_size_bits;
2411 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2412 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2413 "in error code path. Run chkdsk to recover "
2414 "the lost cluster(s).");
2420 * If the runlist truncation fails and/or the search context is no
2421 * longer valid, we cannot resize the attribute record or build the
2422 * mapping pairs array thus we mark the inode bad so that no access to
2423 * the freed clusters can happen.
2425 if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2426 ntfs_error(vol->sb, "Failed to %s in error code path. Run "
2427 "chkdsk to recover.", IS_ERR(m) ?
2428 "restore attribute search context" :
2429 "truncate attribute runlist");
2431 make_bad_inode(VFS_I(base_ni));
2433 } else if (mp_rebuilt) {
2434 if (ntfs_attr_record_resize(m, a, attr_len)) {
2435 ntfs_error(vol->sb, "Failed to restore attribute "
2436 "record in error code path. Run "
2437 "chkdsk to recover.");
2439 make_bad_inode(VFS_I(base_ni));
2441 } else /* if (success) */ {
2442 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2443 a->data.non_resident.
2444 mapping_pairs_offset), attr_len -
2445 le16_to_cpu(a->data.non_resident.
2446 mapping_pairs_offset), rl2, ll, -1,
2448 ntfs_error(vol->sb, "Failed to restore "
2449 "mapping pairs array in error "
2450 "code path. Run chkdsk to "
2453 make_bad_inode(VFS_I(base_ni));
2456 flush_dcache_mft_record_page(ctx->ntfs_ino);
2457 mark_mft_record_dirty(ctx->ntfs_ino);
2462 ntfs_attr_put_search_ctx(ctx);
2464 unmap_mft_record(base_ni);
2465 up_write(&ni->runlist.lock);
2467 ntfs_debug("Failed. Returning error code %i.", err);
2472 * ntfs_attr_set - fill (a part of) an attribute with a byte
2473 * @ni: ntfs inode describing the attribute to fill
2474 * @ofs: offset inside the attribute at which to start to fill
2475 * @cnt: number of bytes to fill
2476 * @val: the unsigned 8-bit value with which to fill the attribute
2478 * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
2479 * byte offset @ofs inside the attribute with the constant byte @val.
2481 * This function is effectively like memset() applied to an ntfs attribute.
2482 * Note thie function actually only operates on the page cache pages belonging
2483 * to the ntfs attribute and it marks them dirty after doing the memset().
2484 * Thus it relies on the vm dirty page write code paths to cause the modified
2485 * pages to be written to the mft record/disk.
2487 * Return 0 on success and -errno on error. An error code of -ESPIPE means
2488 * that @ofs + @cnt were outside the end of the attribute and no write was
2491 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2493 ntfs_volume *vol = ni->vol;
2494 struct address_space *mapping;
2498 unsigned int start_ofs, end_ofs, size;
2500 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2501 (long long)ofs, (long long)cnt, val);
2507 * FIXME: Compressed and encrypted attributes are not supported when
2508 * writing and we should never have gotten here for them.
2510 BUG_ON(NInoCompressed(ni));
2511 BUG_ON(NInoEncrypted(ni));
2512 mapping = VFS_I(ni)->i_mapping;
2513 /* Work out the starting index and page offset. */
2514 idx = ofs >> PAGE_CACHE_SHIFT;
2515 start_ofs = ofs & ~PAGE_CACHE_MASK;
2516 /* Work out the ending index and page offset. */
2518 end_ofs = end & ~PAGE_CACHE_MASK;
2519 /* If the end is outside the inode size return -ESPIPE. */
2520 if (unlikely(end > i_size_read(VFS_I(ni)))) {
2521 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2524 end >>= PAGE_CACHE_SHIFT;
2525 /* If there is a first partial page, need to do it the slow way. */
2527 page = read_cache_page(mapping, idx,
2528 (filler_t*)mapping->a_ops->readpage, NULL);
2530 ntfs_error(vol->sb, "Failed to read first partial "
2531 "page (sync error, index 0x%lx).", idx);
2532 return PTR_ERR(page);
2534 wait_on_page_locked(page);
2535 if (unlikely(!PageUptodate(page))) {
2536 ntfs_error(vol->sb, "Failed to read first partial page "
2537 "(async error, index 0x%lx).", idx);
2538 page_cache_release(page);
2539 return PTR_ERR(page);
2542 * If the last page is the same as the first page, need to
2543 * limit the write to the end offset.
2545 size = PAGE_CACHE_SIZE;
2548 kaddr = kmap_atomic(page, KM_USER0);
2549 memset(kaddr + start_ofs, val, size - start_ofs);
2550 flush_dcache_page(page);
2551 kunmap_atomic(kaddr, KM_USER0);
2552 set_page_dirty(page);
2553 page_cache_release(page);
2558 /* Do the whole pages the fast way. */
2559 for (; idx < end; idx++) {
2560 /* Find or create the current page. (The page is locked.) */
2561 page = grab_cache_page(mapping, idx);
2562 if (unlikely(!page)) {
2563 ntfs_error(vol->sb, "Insufficient memory to grab "
2564 "page (index 0x%lx).", idx);
2567 kaddr = kmap_atomic(page, KM_USER0);
2568 memset(kaddr, val, PAGE_CACHE_SIZE);
2569 flush_dcache_page(page);
2570 kunmap_atomic(kaddr, KM_USER0);
2572 * If the page has buffers, mark them uptodate since buffer
2573 * state and not page state is definitive in 2.6 kernels.
2575 if (page_has_buffers(page)) {
2576 struct buffer_head *bh, *head;
2578 bh = head = page_buffers(page);
2580 set_buffer_uptodate(bh);
2581 } while ((bh = bh->b_this_page) != head);
2583 /* Now that buffers are uptodate, set the page uptodate, too. */
2584 SetPageUptodate(page);
2586 * Set the page and all its buffers dirty and mark the inode
2587 * dirty, too. The VM will write the page later on.
2589 set_page_dirty(page);
2590 /* Finally unlock and release the page. */
2592 page_cache_release(page);
2594 /* If there is a last partial page, need to do it the slow way. */
2596 page = read_cache_page(mapping, idx,
2597 (filler_t*)mapping->a_ops->readpage, NULL);
2599 ntfs_error(vol->sb, "Failed to read last partial page "
2600 "(sync error, index 0x%lx).", idx);
2601 return PTR_ERR(page);
2603 wait_on_page_locked(page);
2604 if (unlikely(!PageUptodate(page))) {
2605 ntfs_error(vol->sb, "Failed to read last partial page "
2606 "(async error, index 0x%lx).", idx);
2607 page_cache_release(page);
2608 return PTR_ERR(page);
2610 kaddr = kmap_atomic(page, KM_USER0);
2611 memset(kaddr, val, end_ofs);
2612 flush_dcache_page(page);
2613 kunmap_atomic(kaddr, KM_USER0);
2614 set_page_dirty(page);
2615 page_cache_release(page);
2618 ntfs_debug("Done.");
2622 #endif /* NTFS_RW */