2 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2006 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/sched.h>
25 #include <linux/swap.h>
26 #include <linux/writeback.h>
38 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
39 * @ni: ntfs inode for which to map (part of) a runlist
40 * @vcn: map runlist part containing this vcn
41 * @ctx: active attribute search context if present or NULL if not
43 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
45 * If @ctx is specified, it is an active search context of @ni and its base mft
46 * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
47 * runlist fragments and allows their mapping. If you do not have the mft
48 * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
49 * will perform the necessary mapping and unmapping.
51 * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
52 * restores it before returning. Thus, @ctx will be left pointing to the same
53 * attribute on return as on entry. However, the actual pointers in @ctx may
54 * point to different memory locations on return, so you must remember to reset
55 * any cached pointers from the @ctx, i.e. after the call to
56 * ntfs_map_runlist_nolock(), you will probably want to do:
59 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
60 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
62 * Return 0 on success and -errno on error. There is one special error code
63 * which is not an error as such. This is -ENOENT. It means that @vcn is out
64 * of bounds of the runlist.
66 * Note the runlist can be NULL after this function returns if @vcn is zero and
67 * the attribute has zero allocated size, i.e. there simply is no runlist.
69 * WARNING: If @ctx is supplied, regardless of whether success or failure is
70 * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
71 * is no longer valid, i.e. you need to either call
72 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
73 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
74 * why the mapping of the old inode failed.
76 * Locking: - The runlist described by @ni must be locked for writing on entry
77 * and is locked on return. Note the runlist will be modified.
78 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
79 * entry and it will be left unmapped on return.
80 * - If @ctx is not NULL, the base mft record must be mapped on entry
81 * and it will be left mapped on return.
83 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
91 struct page *put_this_page = NULL;
93 bool ctx_is_temporary, ctx_needs_reset;
94 ntfs_attr_search_ctx old_ctx = { NULL, };
96 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
97 (unsigned long long)vcn);
101 base_ni = ni->ext.base_ntfs_ino;
103 ctx_is_temporary = ctx_needs_reset = true;
104 m = map_mft_record(base_ni);
107 ctx = ntfs_attr_get_search_ctx(base_ni, m);
108 if (unlikely(!ctx)) {
113 VCN allocated_size_vcn;
115 BUG_ON(IS_ERR(ctx->mrec));
117 BUG_ON(!a->non_resident);
118 ctx_is_temporary = false;
119 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
120 read_lock_irqsave(&ni->size_lock, flags);
121 allocated_size_vcn = ni->allocated_size >>
122 ni->vol->cluster_size_bits;
123 read_unlock_irqrestore(&ni->size_lock, flags);
124 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
125 end_vcn = allocated_size_vcn - 1;
127 * If we already have the attribute extent containing @vcn in
128 * @ctx, no need to look it up again. We slightly cheat in
129 * that if vcn exceeds the allocated size, we will refuse to
130 * map the runlist below, so there is definitely no need to get
131 * the right attribute extent.
133 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
134 a->name_length == ni->name_len &&
135 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
136 ni->name, ni->name_len) &&
137 sle64_to_cpu(a->data.non_resident.lowest_vcn)
138 <= vcn && end_vcn >= vcn))
139 ctx_needs_reset = false;
141 /* Save the old search context. */
144 * If the currently mapped (extent) inode is not the
145 * base inode we will unmap it when we reinitialize the
146 * search context which means we need to get a
147 * reference to the page containing the mapped mft
148 * record so we do not accidentally drop changes to the
149 * mft record when it has not been marked dirty yet.
151 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
152 old_ctx.base_ntfs_ino) {
153 put_this_page = old_ctx.ntfs_ino->page;
154 page_cache_get(put_this_page);
157 * Reinitialize the search context so we can lookup the
158 * needed attribute extent.
160 ntfs_attr_reinit_search_ctx(ctx);
161 ctx_needs_reset = true;
164 if (ctx_needs_reset) {
165 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
166 CASE_SENSITIVE, vcn, NULL, 0, ctx);
172 BUG_ON(!ctx->attr->non_resident);
176 * Only decompress the mapping pairs if @vcn is inside it. Otherwise
177 * we get into problems when we try to map an out of bounds vcn because
178 * we then try to map the already mapped runlist fragment and
179 * ntfs_mapping_pairs_decompress() fails.
181 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
182 if (!a->data.non_resident.lowest_vcn && end_vcn == 1)
183 end_vcn = sle64_to_cpu(a->data.non_resident.allocated_size) >>
184 ni->vol->cluster_size_bits;
185 if (unlikely(vcn >= end_vcn)) {
189 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
195 if (ctx_is_temporary) {
197 ntfs_attr_put_search_ctx(ctx);
198 unmap_mft_record(base_ni);
199 } else if (ctx_needs_reset) {
201 * If there is no attribute list, restoring the search context
202 * is acomplished simply by copying the saved context back over
203 * the caller supplied context. If there is an attribute list,
204 * things are more complicated as we need to deal with mapping
205 * of mft records and resulting potential changes in pointers.
207 if (NInoAttrList(base_ni)) {
209 * If the currently mapped (extent) inode is not the
210 * one we had before, we need to unmap it and map the
213 if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
215 * If the currently mapped inode is not the
216 * base inode, unmap it.
218 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
219 ctx->base_ntfs_ino) {
220 unmap_extent_mft_record(ctx->ntfs_ino);
221 ctx->mrec = ctx->base_mrec;
225 * If the old mapped inode is not the base
228 if (old_ctx.base_ntfs_ino &&
230 old_ctx.base_ntfs_ino) {
232 ctx->mrec = map_mft_record(
235 * Something bad has happened. If out
236 * of memory retry till it succeeds.
237 * Any other errors are fatal and we
238 * return the error code in ctx->mrec.
239 * Let the caller deal with it... We
240 * just need to fudge things so the
241 * caller can reinit and/or put the
242 * search context safely.
244 if (IS_ERR(ctx->mrec)) {
245 if (PTR_ERR(ctx->mrec) ==
256 /* Update the changed pointers in the saved context. */
257 if (ctx->mrec != old_ctx.mrec) {
258 if (!IS_ERR(ctx->mrec))
259 old_ctx.attr = (ATTR_RECORD*)(
263 old_ctx.mrec = ctx->mrec;
266 /* Restore the search context to the saved one. */
269 * We drop the reference on the page we took earlier. In the
270 * case that IS_ERR(ctx->mrec) is true this means we might lose
271 * some changes to the mft record that had been made between
272 * the last time it was marked dirty/written out and now. This
273 * at this stage is not a problem as the mapping error is fatal
274 * enough that the mft record cannot be written out anyway and
275 * the caller is very likely to shutdown the whole inode
276 * immediately and mark the volume dirty for chkdsk to pick up
280 page_cache_release(put_this_page);
286 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
287 * @ni: ntfs inode for which to map (part of) a runlist
288 * @vcn: map runlist part containing this vcn
290 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
292 * Return 0 on success and -errno on error. There is one special error code
293 * which is not an error as such. This is -ENOENT. It means that @vcn is out
294 * of bounds of the runlist.
296 * Locking: - The runlist must be unlocked on entry and is unlocked on return.
297 * - This function takes the runlist lock for writing and may modify
300 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
304 down_write(&ni->runlist.lock);
305 /* Make sure someone else didn't do the work while we were sleeping. */
306 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
308 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
309 up_write(&ni->runlist.lock);
314 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
315 * @ni: ntfs inode of the attribute whose runlist to search
316 * @vcn: vcn to convert
317 * @write_locked: true if the runlist is locked for writing
319 * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
320 * described by the ntfs inode @ni and return the corresponding logical cluster
323 * If the @vcn is not mapped yet, the attempt is made to map the attribute
324 * extent containing the @vcn and the vcn to lcn conversion is retried.
326 * If @write_locked is true the caller has locked the runlist for writing and
327 * if false for reading.
329 * Since lcns must be >= 0, we use negative return codes with special meaning:
331 * Return code Meaning / Description
332 * ==========================================
333 * LCN_HOLE Hole / not allocated on disk.
334 * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
335 * LCN_ENOMEM Not enough memory to map runlist.
336 * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
338 * Locking: - The runlist must be locked on entry and is left locked on return.
339 * - If @write_locked is 'false', i.e. the runlist is locked for reading,
340 * the lock may be dropped inside the function so you cannot rely on
341 * the runlist still being the same when this function returns.
343 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
344 const bool write_locked)
348 bool is_retry = false;
350 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
351 ni->mft_no, (unsigned long long)vcn,
352 write_locked ? "write" : "read");
354 BUG_ON(!NInoNonResident(ni));
356 if (!ni->runlist.rl) {
357 read_lock_irqsave(&ni->size_lock, flags);
358 if (!ni->allocated_size) {
359 read_unlock_irqrestore(&ni->size_lock, flags);
362 read_unlock_irqrestore(&ni->size_lock, flags);
365 /* Convert vcn to lcn. If that fails map the runlist and retry once. */
366 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
367 if (likely(lcn >= LCN_HOLE)) {
368 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
371 if (lcn != LCN_RL_NOT_MAPPED) {
372 if (lcn != LCN_ENOENT)
374 } else if (!is_retry) {
378 up_read(&ni->runlist.lock);
379 down_write(&ni->runlist.lock);
380 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
381 LCN_RL_NOT_MAPPED)) {
382 up_write(&ni->runlist.lock);
383 down_read(&ni->runlist.lock);
387 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
389 up_write(&ni->runlist.lock);
390 down_read(&ni->runlist.lock);
398 else if (err == -ENOMEM)
403 if (lcn != LCN_ENOENT)
404 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
410 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
411 * @ni: ntfs inode describing the runlist to search
413 * @ctx: active attribute search context if present or NULL if not
415 * Find the virtual cluster number @vcn in the runlist described by the ntfs
416 * inode @ni and return the address of the runlist element containing the @vcn.
418 * If the @vcn is not mapped yet, the attempt is made to map the attribute
419 * extent containing the @vcn and the vcn to lcn conversion is retried.
421 * If @ctx is specified, it is an active search context of @ni and its base mft
422 * record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
423 * runlist fragments and allows their mapping. If you do not have the mft
424 * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
425 * will perform the necessary mapping and unmapping.
427 * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
428 * restores it before returning. Thus, @ctx will be left pointing to the same
429 * attribute on return as on entry. However, the actual pointers in @ctx may
430 * point to different memory locations on return, so you must remember to reset
431 * any cached pointers from the @ctx, i.e. after the call to
432 * ntfs_attr_find_vcn_nolock(), you will probably want to do:
435 * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
436 * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
437 * Note you need to distinguish between the lcn of the returned runlist element
438 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
439 * read and allocate clusters on write.
441 * Return the runlist element containing the @vcn on success and
442 * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
443 * to decide if the return is success or failure and PTR_ERR() to get to the
444 * error code if IS_ERR() is true.
446 * The possible error return codes are:
447 * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
448 * -ENOMEM - Not enough memory to map runlist.
449 * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
451 * WARNING: If @ctx is supplied, regardless of whether success or failure is
452 * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
453 * is no longer valid, i.e. you need to either call
454 * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
455 * In that case PTR_ERR(@ctx->mrec) will give you the error code for
456 * why the mapping of the old inode failed.
458 * Locking: - The runlist described by @ni must be locked for writing on entry
459 * and is locked on return. Note the runlist may be modified when
460 * needed runlist fragments need to be mapped.
461 * - If @ctx is NULL, the base mft record of @ni must not be mapped on
462 * entry and it will be left unmapped on return.
463 * - If @ctx is not NULL, the base mft record must be mapped on entry
464 * and it will be left mapped on return.
466 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
467 ntfs_attr_search_ctx *ctx)
472 bool is_retry = false;
474 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
475 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
477 BUG_ON(!NInoNonResident(ni));
479 if (!ni->runlist.rl) {
480 read_lock_irqsave(&ni->size_lock, flags);
481 if (!ni->allocated_size) {
482 read_unlock_irqrestore(&ni->size_lock, flags);
483 return ERR_PTR(-ENOENT);
485 read_unlock_irqrestore(&ni->size_lock, flags);
489 if (likely(rl && vcn >= rl[0].vcn)) {
490 while (likely(rl->length)) {
491 if (unlikely(vcn < rl[1].vcn)) {
492 if (likely(rl->lcn >= LCN_HOLE)) {
500 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
501 if (likely(rl->lcn == LCN_ENOENT))
507 if (!err && !is_retry) {
509 * If the search context is invalid we cannot map the unmapped
512 if (IS_ERR(ctx->mrec))
513 err = PTR_ERR(ctx->mrec);
516 * The @vcn is in an unmapped region, map the runlist
519 err = ntfs_map_runlist_nolock(ni, vcn, ctx);
530 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
535 * ntfs_attr_find - find (next) attribute in mft record
536 * @type: attribute type to find
537 * @name: attribute name to find (optional, i.e. NULL means don't care)
538 * @name_len: attribute name length (only needed if @name present)
539 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
540 * @val: attribute value to find (optional, resident attributes only)
541 * @val_len: attribute value length
542 * @ctx: search context with mft record and attribute to search from
544 * You should not need to call this function directly. Use ntfs_attr_lookup()
547 * ntfs_attr_find() takes a search context @ctx as parameter and searches the
548 * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
549 * attribute of @type, optionally @name and @val.
551 * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
552 * point to the found attribute.
554 * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
555 * @ctx->attr will point to the attribute before which the attribute being
556 * searched for would need to be inserted if such an action were to be desired.
558 * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
559 * undefined and in particular do not rely on it not changing.
561 * If @ctx->is_first is 'true', the search begins with @ctx->attr itself. If it
562 * is 'false', the search begins after @ctx->attr.
564 * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
565 * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
566 * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
567 * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
568 * sensitive. When @name is present, @name_len is the @name length in Unicode
571 * If @name is not present (NULL), we assume that the unnamed attribute is
572 * being searched for.
574 * Finally, the resident attribute value @val is looked for, if present. If
575 * @val is not present (NULL), @val_len is ignored.
577 * ntfs_attr_find() only searches the specified mft record and it ignores the
578 * presence of an attribute list attribute (unless it is the one being searched
579 * for, obviously). If you need to take attribute lists into consideration,
580 * use ntfs_attr_lookup() instead (see below). This also means that you cannot
581 * use ntfs_attr_find() to search for extent records of non-resident
582 * attributes, as extents with lowest_vcn != 0 are usually described by the
583 * attribute list attribute only. - Note that it is possible that the first
584 * extent is only in the attribute list while the last extent is in the base
585 * mft record, so do not rely on being able to find the first extent in the
588 * Warning: Never use @val when looking for attribute types which can be
589 * non-resident as this most likely will result in a crash!
591 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
592 const u32 name_len, const IGNORE_CASE_BOOL ic,
593 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
596 ntfs_volume *vol = ctx->ntfs_ino->vol;
597 ntfschar *upcase = vol->upcase;
598 u32 upcase_len = vol->upcase_len;
601 * Iterate over attributes in mft record starting at @ctx->attr, or the
602 * attribute following that, if @ctx->is_first is 'true'.
606 ctx->is_first = false;
608 a = (ATTR_RECORD*)((u8*)ctx->attr +
609 le32_to_cpu(ctx->attr->length));
610 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
611 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
612 le32_to_cpu(ctx->mrec->bytes_allocated))
615 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
618 if (unlikely(!a->length))
623 * If @name is present, compare the two names. If @name is
624 * missing, assume we want an unnamed attribute.
627 /* The search failed if the found attribute is named. */
630 } else if (!ntfs_are_names_equal(name, name_len,
631 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
632 a->name_length, ic, upcase, upcase_len)) {
635 rc = ntfs_collate_names(name, name_len,
637 le16_to_cpu(a->name_offset)),
638 a->name_length, 1, IGNORE_CASE,
641 * If @name collates before a->name, there is no
642 * matching attribute.
646 /* If the strings are not equal, continue search. */
649 rc = ntfs_collate_names(name, name_len,
651 le16_to_cpu(a->name_offset)),
652 a->name_length, 1, CASE_SENSITIVE,
660 * The names match or @name not present and attribute is
661 * unnamed. If no @val specified, we have found the attribute
666 /* @val is present; compare values. */
670 rc = memcmp(val, (u8*)a + le16_to_cpu(
671 a->data.resident.value_offset),
672 min_t(u32, val_len, le32_to_cpu(
673 a->data.resident.value_length)));
675 * If @val collates before the current attribute's
676 * value, there is no matching attribute.
682 a->data.resident.value_length);
691 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
697 * load_attribute_list - load an attribute list into memory
698 * @vol: ntfs volume from which to read
699 * @runlist: runlist of the attribute list
700 * @al_start: destination buffer
701 * @size: size of the destination buffer in bytes
702 * @initialized_size: initialized size of the attribute list
704 * Walk the runlist @runlist and load all clusters from it copying them into
705 * the linear buffer @al. The maximum number of bytes copied to @al is @size
706 * bytes. Note, @size does not need to be a multiple of the cluster size. If
707 * @initialized_size is less than @size, the region in @al between
708 * @initialized_size and @size will be zeroed and not read from disk.
710 * Return 0 on success or -errno on error.
712 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
713 const s64 size, const s64 initialized_size)
717 u8 *al_end = al + initialized_size;
719 struct buffer_head *bh;
720 struct super_block *sb;
721 unsigned long block_size;
722 unsigned long block, max_block;
724 unsigned char block_size_bits;
726 ntfs_debug("Entering.");
727 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
728 initialized_size > size)
730 if (!initialized_size) {
735 block_size = sb->s_blocksize;
736 block_size_bits = sb->s_blocksize_bits;
737 down_read(&runlist->lock);
740 ntfs_error(sb, "Cannot read attribute list since runlist is "
744 /* Read all clusters specified by the runlist one run at a time. */
746 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
747 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
748 (unsigned long long)rl->vcn,
749 (unsigned long long)lcn);
750 /* The attribute list cannot be sparse. */
752 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
753 "read attribute list.");
756 block = lcn << vol->cluster_size_bits >> block_size_bits;
757 /* Read the run from device in chunks of block_size bytes. */
758 max_block = block + (rl->length << vol->cluster_size_bits >>
760 ntfs_debug("max_block = 0x%lx.", max_block);
762 ntfs_debug("Reading block = 0x%lx.", block);
763 bh = sb_bread(sb, block);
765 ntfs_error(sb, "sb_bread() failed. Cannot "
766 "read attribute list.");
769 if (al + block_size >= al_end)
771 memcpy(al, bh->b_data, block_size);
774 } while (++block < max_block);
777 if (initialized_size < size) {
779 memset(al_start + initialized_size, 0, size - initialized_size);
782 up_read(&runlist->lock);
789 * Note: The attribute list can be smaller than its allocation
790 * by multiple clusters. This has been encountered by at least
791 * two people running Windows XP, thus we cannot do any
792 * truncation sanity checking here. (AIA)
794 memcpy(al, bh->b_data, al_end - al);
796 if (initialized_size < size)
802 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
810 * ntfs_external_attr_find - find an attribute in the attribute list of an inode
811 * @type: attribute type to find
812 * @name: attribute name to find (optional, i.e. NULL means don't care)
813 * @name_len: attribute name length (only needed if @name present)
814 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
815 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
816 * @val: attribute value to find (optional, resident attributes only)
817 * @val_len: attribute value length
818 * @ctx: search context with mft record and attribute to search from
820 * You should not need to call this function directly. Use ntfs_attr_lookup()
823 * Find an attribute by searching the attribute list for the corresponding
824 * attribute list entry. Having found the entry, map the mft record if the
825 * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
826 * in there and return it.
828 * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
829 * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
830 * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
831 * then the base inode).
833 * After finishing with the attribute/mft record you need to call
834 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
835 * mapped inodes, etc).
837 * If the attribute is found, ntfs_external_attr_find() returns 0 and
838 * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
839 * mft record in which @ctx->attr is located and @ctx->al_entry will point to
840 * the attribute list entry for the attribute.
842 * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
843 * @ctx->attr will point to the attribute in the base mft record before which
844 * the attribute being searched for would need to be inserted if such an action
845 * were to be desired. @ctx->mrec will point to the mft record in which
846 * @ctx->attr is located and @ctx->al_entry will point to the attribute list
847 * entry of the attribute before which the attribute being searched for would
848 * need to be inserted if such an action were to be desired.
850 * Thus to insert the not found attribute, one wants to add the attribute to
851 * @ctx->mrec (the base mft record) and if there is not enough space, the
852 * attribute should be placed in a newly allocated extent mft record. The
853 * attribute list entry for the inserted attribute should be inserted in the
854 * attribute list attribute at @ctx->al_entry.
856 * On actual error, ntfs_external_attr_find() returns -EIO. In this case
857 * @ctx->attr is undefined and in particular do not rely on it not changing.
859 static int ntfs_external_attr_find(const ATTR_TYPE type,
860 const ntfschar *name, const u32 name_len,
861 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
862 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
864 ntfs_inode *base_ni, *ni;
866 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
867 u8 *al_start, *al_end;
872 static const char *es = " Unmount and run chkdsk.";
875 base_ni = ctx->base_ntfs_ino;
876 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
878 /* First call happens with the base mft record. */
879 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
880 ctx->base_mrec = ctx->mrec;
883 ctx->base_attr = ctx->attr;
887 al_start = base_ni->attr_list;
888 al_end = al_start + base_ni->attr_list_size;
890 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
892 * Iterate over entries in attribute list starting at @ctx->al_entry,
893 * or the entry following that, if @ctx->is_first is 'true'.
896 al_entry = ctx->al_entry;
897 ctx->is_first = false;
899 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
900 le16_to_cpu(ctx->al_entry->length));
901 for (;; al_entry = next_al_entry) {
902 /* Out of bounds check. */
903 if ((u8*)al_entry < base_ni->attr_list ||
904 (u8*)al_entry > al_end)
905 break; /* Inode is corrupt. */
906 ctx->al_entry = al_entry;
907 /* Catch the end of the attribute list. */
908 if ((u8*)al_entry == al_end)
910 if (!al_entry->length)
912 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
913 le16_to_cpu(al_entry->length) > al_end)
915 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
916 le16_to_cpu(al_entry->length));
917 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
919 if (type != al_entry->type)
922 * If @name is present, compare the two names. If @name is
923 * missing, assume we want an unnamed attribute.
925 al_name_len = al_entry->name_length;
926 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
930 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
931 name_len, ic, vol->upcase, vol->upcase_len)) {
934 rc = ntfs_collate_names(name, name_len, al_name,
935 al_name_len, 1, IGNORE_CASE,
936 vol->upcase, vol->upcase_len);
938 * If @name collates before al_name, there is no
939 * matching attribute.
943 /* If the strings are not equal, continue search. */
947 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
948 * that is inconsistent with ntfs_attr_find(). The
949 * subsequent rc checks were also different. Perhaps I
950 * made a mistake in one of the two. Need to recheck
951 * which is correct or at least see what is going on...
954 rc = ntfs_collate_names(name, name_len, al_name,
955 al_name_len, 1, CASE_SENSITIVE,
956 vol->upcase, vol->upcase_len);
963 * The names match or @name not present and attribute is
964 * unnamed. Now check @lowest_vcn. Continue search if the
965 * next attribute list entry still fits @lowest_vcn. Otherwise
966 * we have reached the right one or the search has failed.
968 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
969 (u8*)next_al_entry + 6 < al_end &&
970 (u8*)next_al_entry + le16_to_cpu(
971 next_al_entry->length) <= al_end &&
972 sle64_to_cpu(next_al_entry->lowest_vcn) <=
974 next_al_entry->type == al_entry->type &&
975 next_al_entry->name_length == al_name_len &&
976 ntfs_are_names_equal((ntfschar*)((u8*)
978 next_al_entry->name_offset),
979 next_al_entry->name_length,
980 al_name, al_name_len, CASE_SENSITIVE,
981 vol->upcase, vol->upcase_len))
983 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
984 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
985 ntfs_error(vol->sb, "Found stale mft "
986 "reference in attribute list "
987 "of base inode 0x%lx.%s",
988 base_ni->mft_no, es);
992 } else { /* Mft references do not match. */
993 /* If there is a mapped record unmap it first. */
995 unmap_extent_mft_record(ni);
996 /* Do we want the base record back? */
997 if (MREF_LE(al_entry->mft_reference) ==
999 ni = ctx->ntfs_ino = base_ni;
1000 ctx->mrec = ctx->base_mrec;
1002 /* We want an extent record. */
1003 ctx->mrec = map_extent_mft_record(base_ni,
1005 al_entry->mft_reference), &ni);
1006 if (IS_ERR(ctx->mrec)) {
1007 ntfs_error(vol->sb, "Failed to map "
1008 "extent mft record "
1009 "0x%lx of base inode "
1013 base_ni->mft_no, es);
1014 err = PTR_ERR(ctx->mrec);
1017 /* Cause @ctx to be sanitized below. */
1023 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1024 le16_to_cpu(ctx->mrec->attrs_offset));
1027 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1028 * mft record containing the attribute represented by the
1032 * We could call into ntfs_attr_find() to find the right
1033 * attribute in this mft record but this would be less
1034 * efficient and not quite accurate as ntfs_attr_find() ignores
1035 * the attribute instance numbers for example which become
1036 * important when one plays with attribute lists. Also,
1037 * because a proper match has been found in the attribute list
1038 * entry above, the comparison can now be optimized. So it is
1039 * worth re-implementing a simplified ntfs_attr_find() here.
1043 * Use a manual loop so we can still use break and continue
1044 * with the same meanings as above.
1047 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1048 le32_to_cpu(ctx->mrec->bytes_allocated))
1050 if (a->type == AT_END)
1054 if (al_entry->instance != a->instance)
1057 * If the type and/or the name are mismatched between the
1058 * attribute list entry and the attribute record, there is
1059 * corruption so we break and return error EIO.
1061 if (al_entry->type != a->type)
1063 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1064 le16_to_cpu(a->name_offset)), a->name_length,
1065 al_name, al_name_len, CASE_SENSITIVE,
1066 vol->upcase, vol->upcase_len))
1070 * If no @val specified or @val specified and it matches, we
1073 if (!val || (!a->non_resident && le32_to_cpu(
1074 a->data.resident.value_length) == val_len &&
1076 le16_to_cpu(a->data.resident.value_offset),
1078 ntfs_debug("Done, found.");
1082 /* Proceed to the next attribute in the current mft record. */
1083 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1084 goto do_next_attr_loop;
1087 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1088 "attribute list attribute.%s", base_ni->mft_no,
1092 if (ni != base_ni) {
1094 unmap_extent_mft_record(ni);
1095 ctx->ntfs_ino = base_ni;
1096 ctx->mrec = ctx->base_mrec;
1097 ctx->attr = ctx->base_attr;
1104 * If we were looking for AT_END, we reset the search context @ctx and
1105 * use ntfs_attr_find() to seek to the end of the base mft record.
1107 if (type == AT_END) {
1108 ntfs_attr_reinit_search_ctx(ctx);
1109 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1113 * The attribute was not found. Before we return, we want to ensure
1114 * @ctx->mrec and @ctx->attr indicate the position at which the
1115 * attribute should be inserted in the base mft record. Since we also
1116 * want to preserve @ctx->al_entry we cannot reinitialize the search
1117 * context using ntfs_attr_reinit_search_ctx() as this would set
1118 * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
1119 * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
1120 * @ctx->al_entry as the remaining fields (base_*) are identical to
1121 * their non base_ counterparts and we cannot set @ctx->base_attr
1122 * correctly yet as we do not know what @ctx->attr will be set to by
1123 * the call to ntfs_attr_find() below.
1126 unmap_extent_mft_record(ni);
1127 ctx->mrec = ctx->base_mrec;
1128 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1129 le16_to_cpu(ctx->mrec->attrs_offset));
1130 ctx->is_first = true;
1131 ctx->ntfs_ino = base_ni;
1132 ctx->base_ntfs_ino = NULL;
1133 ctx->base_mrec = NULL;
1134 ctx->base_attr = NULL;
1136 * In case there are multiple matches in the base mft record, need to
1137 * keep enumerating until we get an attribute not found response (or
1138 * another error), otherwise we would keep returning the same attribute
1139 * over and over again and all programs using us for enumeration would
1140 * lock up in a tight loop.
1143 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1146 ntfs_debug("Done, not found.");
1151 * ntfs_attr_lookup - find an attribute in an ntfs inode
1152 * @type: attribute type to find
1153 * @name: attribute name to find (optional, i.e. NULL means don't care)
1154 * @name_len: attribute name length (only needed if @name present)
1155 * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1156 * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
1157 * @val: attribute value to find (optional, resident attributes only)
1158 * @val_len: attribute value length
1159 * @ctx: search context with mft record and attribute to search from
1161 * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
1162 * be the base mft record and @ctx must have been obtained from a call to
1163 * ntfs_attr_get_search_ctx().
1165 * This function transparently handles attribute lists and @ctx is used to
1166 * continue searches where they were left off at.
1168 * After finishing with the attribute/mft record you need to call
1169 * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1170 * mapped inodes, etc).
1172 * Return 0 if the search was successful and -errno if not.
1174 * When 0, @ctx->attr is the found attribute and it is in mft record
1175 * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
1176 * the attribute list entry of the found attribute.
1178 * When -ENOENT, @ctx->attr is the attribute which collates just after the
1179 * attribute being searched for, i.e. if one wants to add the attribute to the
1180 * mft record this is the correct place to insert it into. If an attribute
1181 * list attribute is present, @ctx->al_entry is the attribute list entry which
1182 * collates just after the attribute list entry of the attribute being searched
1183 * for, i.e. if one wants to add the attribute to the mft record this is the
1184 * correct place to insert its attribute list entry into.
1186 * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
1187 * then undefined and in particular you should not rely on it not changing.
1189 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1190 const u32 name_len, const IGNORE_CASE_BOOL ic,
1191 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1192 ntfs_attr_search_ctx *ctx)
1194 ntfs_inode *base_ni;
1196 ntfs_debug("Entering.");
1197 BUG_ON(IS_ERR(ctx->mrec));
1198 if (ctx->base_ntfs_ino)
1199 base_ni = ctx->base_ntfs_ino;
1201 base_ni = ctx->ntfs_ino;
1202 /* Sanity check, just for debugging really. */
1204 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1205 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1207 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1212 * ntfs_attr_init_search_ctx - initialize an attribute search context
1213 * @ctx: attribute search context to initialize
1214 * @ni: ntfs inode with which to initialize the search context
1215 * @mrec: mft record with which to initialize the search context
1217 * Initialize the attribute search context @ctx with @ni and @mrec.
1219 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1220 ntfs_inode *ni, MFT_RECORD *mrec)
1222 *ctx = (ntfs_attr_search_ctx) {
1224 /* Sanity checks are performed elsewhere. */
1225 .attr = (ATTR_RECORD*)((u8*)mrec +
1226 le16_to_cpu(mrec->attrs_offset)),
1233 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1234 * @ctx: attribute search context to reinitialize
1236 * Reinitialize the attribute search context @ctx, unmapping an associated
1237 * extent mft record if present, and initialize the search context again.
1239 * This is used when a search for a new attribute is being started to reset
1240 * the search context to the beginning.
1242 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1244 if (likely(!ctx->base_ntfs_ino)) {
1245 /* No attribute list. */
1246 ctx->is_first = true;
1247 /* Sanity checks are performed elsewhere. */
1248 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1249 le16_to_cpu(ctx->mrec->attrs_offset));
1251 * This needs resetting due to ntfs_external_attr_find() which
1252 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1254 ctx->al_entry = NULL;
1256 } /* Attribute list. */
1257 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1258 unmap_extent_mft_record(ctx->ntfs_ino);
1259 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1264 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1265 * @ni: ntfs inode with which to initialize the search context
1266 * @mrec: mft record with which to initialize the search context
1268 * Allocate a new attribute search context, initialize it with @ni and @mrec,
1269 * and return it. Return NULL if allocation failed.
1271 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1273 ntfs_attr_search_ctx *ctx;
1275 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
1277 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1282 * ntfs_attr_put_search_ctx - release an attribute search context
1283 * @ctx: attribute search context to free
1285 * Release the attribute search context @ctx, unmapping an associated extent
1286 * mft record if present.
1288 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1290 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1291 unmap_extent_mft_record(ctx->ntfs_ino);
1292 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1299 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1300 * @vol: ntfs volume to which the attribute belongs
1301 * @type: attribute type which to find
1303 * Search for the attribute definition record corresponding to the attribute
1304 * @type in the $AttrDef system file.
1306 * Return the attribute type definition record if found and NULL if not found.
1308 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1309 const ATTR_TYPE type)
1313 BUG_ON(!vol->attrdef);
1315 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1316 vol->attrdef_size && ad->type; ++ad) {
1317 /* We have not found it yet, carry on searching. */
1318 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1320 /* We found the attribute; return it. */
1321 if (likely(ad->type == type))
1323 /* We have gone too far already. No point in continuing. */
1326 /* Attribute not found. */
1327 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1333 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1334 * @vol: ntfs volume to which the attribute belongs
1335 * @type: attribute type which to check
1336 * @size: size which to check
1338 * Check whether the @size in bytes is valid for an attribute of @type on the
1339 * ntfs volume @vol. This information is obtained from $AttrDef system file.
1341 * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1342 * listed in $AttrDef.
1344 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1351 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1352 * listed in $AttrDef.
1354 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1356 /* Get the $AttrDef entry for the attribute @type. */
1357 ad = ntfs_attr_find_in_attrdef(vol, type);
1360 /* Do the bounds check. */
1361 if (((sle64_to_cpu(ad->min_size) > 0) &&
1362 size < sle64_to_cpu(ad->min_size)) ||
1363 ((sle64_to_cpu(ad->max_size) > 0) && size >
1364 sle64_to_cpu(ad->max_size)))
1370 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1371 * @vol: ntfs volume to which the attribute belongs
1372 * @type: attribute type which to check
1374 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1375 * be non-resident. This information is obtained from $AttrDef system file.
1377 * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1378 * -ENOENT if the attribute is not listed in $AttrDef.
1380 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1384 /* Find the attribute definition record in $AttrDef. */
1385 ad = ntfs_attr_find_in_attrdef(vol, type);
1388 /* Check the flags and return the result. */
1389 if (ad->flags & ATTR_DEF_RESIDENT)
1395 * ntfs_attr_can_be_resident - check if an attribute can be resident
1396 * @vol: ntfs volume to which the attribute belongs
1397 * @type: attribute type which to check
1399 * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1400 * be resident. This information is derived from our ntfs knowledge and may
1401 * not be completely accurate, especially when user defined attributes are
1402 * present. Basically we allow everything to be resident except for index
1403 * allocation and $EA attributes.
1405 * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1407 * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1408 * otherwise windows will not boot (blue screen of death)! We cannot
1409 * check for this here as we do not know which inode's $Bitmap is
1410 * being asked about so the caller needs to special case this.
1412 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1414 if (type == AT_INDEX_ALLOCATION)
1420 * ntfs_attr_record_resize - resize an attribute record
1421 * @m: mft record containing attribute record
1422 * @a: attribute record to resize
1423 * @new_size: new size in bytes to which to resize the attribute record @a
1425 * Resize the attribute record @a, i.e. the resident part of the attribute, in
1426 * the mft record @m to @new_size bytes.
1428 * Return 0 on success and -errno on error. The following error codes are
1430 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1432 * Note: On error, no modifications have been performed whatsoever.
1434 * Warning: If you make a record smaller without having copied all the data you
1435 * are interested in the data may be overwritten.
1437 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1439 ntfs_debug("Entering for new_size %u.", new_size);
1440 /* Align to 8 bytes if it is not already done. */
1442 new_size = (new_size + 7) & ~7;
1443 /* If the actual attribute length has changed, move things around. */
1444 if (new_size != le32_to_cpu(a->length)) {
1445 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1446 le32_to_cpu(a->length) + new_size;
1447 /* Not enough space in this mft record. */
1448 if (new_muse > le32_to_cpu(m->bytes_allocated))
1450 /* Move attributes following @a to their new location. */
1451 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1452 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1453 (u8*)m) - le32_to_cpu(a->length));
1454 /* Adjust @m to reflect the change in used space. */
1455 m->bytes_in_use = cpu_to_le32(new_muse);
1456 /* Adjust @a to reflect the new size. */
1457 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1458 a->length = cpu_to_le32(new_size);
1464 * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1465 * @m: mft record containing attribute record
1466 * @a: attribute record whose value to resize
1467 * @new_size: new size in bytes to which to resize the attribute value of @a
1469 * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1470 * If the value is made bigger, the newly allocated space is cleared.
1472 * Return 0 on success and -errno on error. The following error codes are
1474 * -ENOSPC - Not enough space in the mft record @m to perform the resize.
1476 * Note: On error, no modifications have been performed whatsoever.
1478 * Warning: If you make a record smaller without having copied all the data you
1479 * are interested in the data may be overwritten.
1481 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1486 /* Resize the resident part of the attribute record. */
1487 if (ntfs_attr_record_resize(m, a,
1488 le16_to_cpu(a->data.resident.value_offset) + new_size))
1491 * The resize succeeded! If we made the attribute value bigger, clear
1492 * the area between the old size and @new_size.
1494 old_size = le32_to_cpu(a->data.resident.value_length);
1495 if (new_size > old_size)
1496 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1497 old_size, 0, new_size - old_size);
1498 /* Finally update the length of the attribute value. */
1499 a->data.resident.value_length = cpu_to_le32(new_size);
1504 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1505 * @ni: ntfs inode describing the attribute to convert
1506 * @data_size: size of the resident data to copy to the non-resident attribute
1508 * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1511 * @data_size must be equal to the attribute value size. This is needed since
1512 * we need to know the size before we can map the mft record and our callers
1513 * always know it. The reason we cannot simply read the size from the vfs
1514 * inode i_size is that this is not necessarily uptodate. This happens when
1515 * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
1517 * Return 0 on success and -errno on error. The following error return codes
1519 * -EPERM - The attribute is not allowed to be non-resident.
1520 * -ENOMEM - Not enough memory.
1521 * -ENOSPC - Not enough disk space.
1522 * -EINVAL - Attribute not defined on the volume.
1523 * -EIO - I/o error or other error.
1524 * Note that -ENOSPC is also returned in the case that there is not enough
1525 * space in the mft record to do the conversion. This can happen when the mft
1526 * record is already very full. The caller is responsible for trying to make
1527 * space in the mft record and trying again. FIXME: Do we need a separate
1528 * error return code for this kind of -ENOSPC or is it always worth trying
1529 * again in case the attribute may then fit in a resident state so no need to
1530 * make it non-resident at all? Ho-hum... (AIA)
1532 * NOTE to self: No changes in the attribute list are required to move from
1533 * a resident to a non-resident attribute.
1535 * Locking: - The caller must hold i_mutex on the inode.
1537 int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1540 struct inode *vi = VFS_I(ni);
1541 ntfs_volume *vol = ni->vol;
1542 ntfs_inode *base_ni;
1545 ntfs_attr_search_ctx *ctx;
1547 runlist_element *rl;
1549 unsigned long flags;
1550 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1552 u8 old_res_attr_flags;
1554 /* Check that the attribute is allowed to be non-resident. */
1555 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1556 if (unlikely(err)) {
1558 ntfs_debug("Attribute is not allowed to be "
1561 ntfs_debug("Attribute not defined on the NTFS "
1566 * FIXME: Compressed and encrypted attributes are not supported when
1567 * writing and we should never have gotten here for them.
1569 BUG_ON(NInoCompressed(ni));
1570 BUG_ON(NInoEncrypted(ni));
1572 * The size needs to be aligned to a cluster boundary for allocation
1575 new_size = (data_size + vol->cluster_size - 1) &
1576 ~(vol->cluster_size - 1);
1579 * Will need the page later and since the page lock nests
1580 * outside all ntfs locks, we need to get the page now.
1582 page = find_or_create_page(vi->i_mapping, 0,
1583 mapping_gfp_mask(vi->i_mapping));
1584 if (unlikely(!page))
1586 /* Start by allocating clusters to hold the attribute value. */
1587 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1588 vol->cluster_size_bits, -1, DATA_ZONE, true);
1591 ntfs_debug("Failed to allocate cluster%s, error code "
1593 vol->cluster_size_bits) > 1 ? "s" : "",
1601 /* Determine the size of the mapping pairs array. */
1602 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1603 if (unlikely(mp_size < 0)) {
1605 ntfs_debug("Failed to get size for mapping pairs array, error "
1609 down_write(&ni->runlist.lock);
1613 base_ni = ni->ext.base_ntfs_ino;
1614 m = map_mft_record(base_ni);
1621 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1622 if (unlikely(!ctx)) {
1626 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1627 CASE_SENSITIVE, 0, NULL, 0, ctx);
1628 if (unlikely(err)) {
1635 BUG_ON(NInoNonResident(ni));
1636 BUG_ON(a->non_resident);
1638 * Calculate new offsets for the name and the mapping pairs array.
1640 if (NInoSparse(ni) || NInoCompressed(ni))
1641 name_ofs = (offsetof(ATTR_REC,
1642 data.non_resident.compressed_size) +
1643 sizeof(a->data.non_resident.compressed_size) +
1646 name_ofs = (offsetof(ATTR_REC,
1647 data.non_resident.compressed_size) + 7) & ~7;
1648 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1650 * Determine the size of the resident part of the now non-resident
1653 arec_size = (mp_ofs + mp_size + 7) & ~7;
1655 * If the page is not uptodate bring it uptodate by copying from the
1658 attr_size = le32_to_cpu(a->data.resident.value_length);
1659 BUG_ON(attr_size != data_size);
1660 if (page && !PageUptodate(page)) {
1661 kaddr = kmap_atomic(page, KM_USER0);
1662 memcpy(kaddr, (u8*)a +
1663 le16_to_cpu(a->data.resident.value_offset),
1665 memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1666 kunmap_atomic(kaddr, KM_USER0);
1667 flush_dcache_page(page);
1668 SetPageUptodate(page);
1670 /* Backup the attribute flag. */
1671 old_res_attr_flags = a->data.resident.flags;
1672 /* Resize the resident part of the attribute record. */
1673 err = ntfs_attr_record_resize(m, a, arec_size);
1677 * Convert the resident part of the attribute record to describe a
1678 * non-resident attribute.
1680 a->non_resident = 1;
1681 /* Move the attribute name if it exists and update the offset. */
1683 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1684 a->name_length * sizeof(ntfschar));
1685 a->name_offset = cpu_to_le16(name_ofs);
1686 /* Setup the fields specific to non-resident attributes. */
1687 a->data.non_resident.lowest_vcn = 0;
1688 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1689 vol->cluster_size_bits);
1690 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1691 memset(&a->data.non_resident.reserved, 0,
1692 sizeof(a->data.non_resident.reserved));
1693 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1694 a->data.non_resident.data_size =
1695 a->data.non_resident.initialized_size =
1696 cpu_to_sle64(attr_size);
1697 if (NInoSparse(ni) || NInoCompressed(ni)) {
1698 a->data.non_resident.compression_unit = 0;
1699 if (NInoCompressed(ni) || vol->major_ver < 3)
1700 a->data.non_resident.compression_unit = 4;
1701 a->data.non_resident.compressed_size =
1702 a->data.non_resident.allocated_size;
1704 a->data.non_resident.compression_unit = 0;
1705 /* Generate the mapping pairs array into the attribute record. */
1706 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1707 arec_size - mp_ofs, rl, 0, -1, NULL);
1708 if (unlikely(err)) {
1709 ntfs_debug("Failed to build mapping pairs, error code %i.",
1713 /* Setup the in-memory attribute structure to be non-resident. */
1714 ni->runlist.rl = rl;
1715 write_lock_irqsave(&ni->size_lock, flags);
1716 ni->allocated_size = new_size;
1717 if (NInoSparse(ni) || NInoCompressed(ni)) {
1718 ni->itype.compressed.size = ni->allocated_size;
1719 if (a->data.non_resident.compression_unit) {
1720 ni->itype.compressed.block_size = 1U << (a->data.
1721 non_resident.compression_unit +
1722 vol->cluster_size_bits);
1723 ni->itype.compressed.block_size_bits =
1724 ffs(ni->itype.compressed.block_size) -
1726 ni->itype.compressed.block_clusters = 1U <<
1727 a->data.non_resident.compression_unit;
1729 ni->itype.compressed.block_size = 0;
1730 ni->itype.compressed.block_size_bits = 0;
1731 ni->itype.compressed.block_clusters = 0;
1733 vi->i_blocks = ni->itype.compressed.size >> 9;
1735 vi->i_blocks = ni->allocated_size >> 9;
1736 write_unlock_irqrestore(&ni->size_lock, flags);
1738 * This needs to be last since the address space operations ->readpage
1739 * and ->writepage can run concurrently with us as they are not
1740 * serialized on i_mutex. Note, we are not allowed to fail once we flip
1741 * this switch, which is another reason to do this last.
1743 NInoSetNonResident(ni);
1744 /* Mark the mft record dirty, so it gets written back. */
1745 flush_dcache_mft_record_page(ctx->ntfs_ino);
1746 mark_mft_record_dirty(ctx->ntfs_ino);
1747 ntfs_attr_put_search_ctx(ctx);
1748 unmap_mft_record(base_ni);
1749 up_write(&ni->runlist.lock);
1751 set_page_dirty(page);
1753 mark_page_accessed(page);
1754 page_cache_release(page);
1756 ntfs_debug("Done.");
1759 /* Convert the attribute back into a resident attribute. */
1760 a->non_resident = 0;
1761 /* Move the attribute name if it exists and update the offset. */
1762 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1763 sizeof(a->data.resident.reserved) + 7) & ~7;
1765 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1766 a->name_length * sizeof(ntfschar));
1767 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1768 a->name_offset = cpu_to_le16(name_ofs);
1769 arec_size = (mp_ofs + attr_size + 7) & ~7;
1770 /* Resize the resident part of the attribute record. */
1771 err2 = ntfs_attr_record_resize(m, a, arec_size);
1772 if (unlikely(err2)) {
1774 * This cannot happen (well if memory corruption is at work it
1775 * could happen in theory), but deal with it as well as we can.
1776 * If the old size is too small, truncate the attribute,
1777 * otherwise simply give it a larger allocated size.
1778 * FIXME: Should check whether chkdsk complains when the
1779 * allocated size is much bigger than the resident value size.
1781 arec_size = le32_to_cpu(a->length);
1782 if ((mp_ofs + attr_size) > arec_size) {
1784 attr_size = arec_size - mp_ofs;
1785 ntfs_error(vol->sb, "Failed to undo partial resident "
1786 "to non-resident attribute "
1787 "conversion. Truncating inode 0x%lx, "
1788 "attribute type 0x%x from %i bytes to "
1789 "%i bytes to maintain metadata "
1790 "consistency. THIS MEANS YOU ARE "
1791 "LOSING %i BYTES DATA FROM THIS %s.",
1793 (unsigned)le32_to_cpu(ni->type),
1794 err2, attr_size, err2 - attr_size,
1795 ((ni->type == AT_DATA) &&
1796 !ni->name_len) ? "FILE": "ATTRIBUTE");
1797 write_lock_irqsave(&ni->size_lock, flags);
1798 ni->initialized_size = attr_size;
1799 i_size_write(vi, attr_size);
1800 write_unlock_irqrestore(&ni->size_lock, flags);
1803 /* Setup the fields specific to resident attributes. */
1804 a->data.resident.value_length = cpu_to_le32(attr_size);
1805 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1806 a->data.resident.flags = old_res_attr_flags;
1807 memset(&a->data.resident.reserved, 0,
1808 sizeof(a->data.resident.reserved));
1809 /* Copy the data from the page back to the attribute value. */
1811 kaddr = kmap_atomic(page, KM_USER0);
1812 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1813 kunmap_atomic(kaddr, KM_USER0);
1815 /* Setup the allocated size in the ntfs inode in case it changed. */
1816 write_lock_irqsave(&ni->size_lock, flags);
1817 ni->allocated_size = arec_size - mp_ofs;
1818 write_unlock_irqrestore(&ni->size_lock, flags);
1819 /* Mark the mft record dirty, so it gets written back. */
1820 flush_dcache_mft_record_page(ctx->ntfs_ino);
1821 mark_mft_record_dirty(ctx->ntfs_ino);
1824 ntfs_attr_put_search_ctx(ctx);
1826 unmap_mft_record(base_ni);
1827 ni->runlist.rl = NULL;
1828 up_write(&ni->runlist.lock);
1831 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1832 ntfs_error(vol->sb, "Failed to release allocated "
1833 "cluster(s) in error code path. Run "
1834 "chkdsk to recover the lost "
1841 page_cache_release(page);
1849 * ntfs_attr_extend_allocation - extend the allocated space of an attribute
1850 * @ni: ntfs inode of the attribute whose allocation to extend
1851 * @new_alloc_size: new size in bytes to which to extend the allocation to
1852 * @new_data_size: new size in bytes to which to extend the data to
1853 * @data_start: beginning of region which is required to be non-sparse
1855 * Extend the allocated space of an attribute described by the ntfs inode @ni
1856 * to @new_alloc_size bytes. If @data_start is -1, the whole extension may be
1857 * implemented as a hole in the file (as long as both the volume and the ntfs
1858 * inode @ni have sparse support enabled). If @data_start is >= 0, then the
1859 * region between the old allocated size and @data_start - 1 may be made sparse
1860 * but the regions between @data_start and @new_alloc_size must be backed by
1863 * If @new_data_size is -1, it is ignored. If it is >= 0, then the data size
1864 * of the attribute is extended to @new_data_size. Note that the i_size of the
1865 * vfs inode is not updated. Only the data size in the base attribute record
1866 * is updated. The caller has to update i_size separately if this is required.
1867 * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
1868 * size as well as for @new_data_size to be greater than @new_alloc_size.
1870 * For resident attributes this involves resizing the attribute record and if
1871 * necessary moving it and/or other attributes into extent mft records and/or
1872 * converting the attribute to a non-resident attribute which in turn involves
1873 * extending the allocation of a non-resident attribute as described below.
1875 * For non-resident attributes this involves allocating clusters in the data
1876 * zone on the volume (except for regions that are being made sparse) and
1877 * extending the run list to describe the allocated clusters as well as
1878 * updating the mapping pairs array of the attribute. This in turn involves
1879 * resizing the attribute record and if necessary moving it and/or other
1880 * attributes into extent mft records and/or splitting the attribute record
1881 * into multiple extent attribute records.
1883 * Also, the attribute list attribute is updated if present and in some of the
1884 * above cases (the ones where extent mft records/attributes come into play),
1885 * an attribute list attribute is created if not already present.
1887 * Return the new allocated size on success and -errno on error. In the case
1888 * that an error is encountered but a partial extension at least up to
1889 * @data_start (if present) is possible, the allocation is partially extended
1890 * and this is returned. This means the caller must check the returned size to
1891 * determine if the extension was partial. If @data_start is -1 then partial
1892 * allocations are not performed.
1894 * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
1896 * Locking: This function takes the runlist lock of @ni for writing as well as
1897 * locking the mft record of the base ntfs inode. These locks are maintained
1898 * throughout execution of the function. These locks are required so that the
1899 * attribute can be resized safely and so that it can for example be converted
1900 * from resident to non-resident safely.
1902 * TODO: At present attribute list attribute handling is not implemented.
1904 * TODO: At present it is not safe to call this function for anything other
1905 * than the $DATA attribute(s) of an uncompressed and unencrypted file.
1907 s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1908 const s64 new_data_size, const s64 data_start)
1911 s64 ll, allocated_size, start = data_start;
1912 struct inode *vi = VFS_I(ni);
1913 ntfs_volume *vol = ni->vol;
1914 ntfs_inode *base_ni;
1917 ntfs_attr_search_ctx *ctx;
1918 runlist_element *rl, *rl2;
1919 unsigned long flags;
1921 u32 attr_len = 0; /* Silence stupid gcc warning. */
1925 read_lock_irqsave(&ni->size_lock, flags);
1926 allocated_size = ni->allocated_size;
1927 read_unlock_irqrestore(&ni->size_lock, flags);
1928 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1929 "old_allocated_size 0x%llx, "
1930 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1931 "data_start 0x%llx.", vi->i_ino,
1932 (unsigned)le32_to_cpu(ni->type),
1933 (unsigned long long)allocated_size,
1934 (unsigned long long)new_alloc_size,
1935 (unsigned long long)new_data_size,
1936 (unsigned long long)start);
1940 * For non-resident attributes, @start and @new_size need to be aligned
1941 * to cluster boundaries for allocation purposes.
1943 if (NInoNonResident(ni)) {
1945 start &= ~(s64)vol->cluster_size_mask;
1946 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1947 ~(s64)vol->cluster_size_mask;
1949 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1950 /* Check if new size is allowed in $AttrDef. */
1951 err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1952 if (unlikely(err)) {
1953 /* Only emit errors when the write will fail completely. */
1954 read_lock_irqsave(&ni->size_lock, flags);
1955 allocated_size = ni->allocated_size;
1956 read_unlock_irqrestore(&ni->size_lock, flags);
1957 if (start < 0 || start >= allocated_size) {
1958 if (err == -ERANGE) {
1959 ntfs_error(vol->sb, "Cannot extend allocation "
1960 "of inode 0x%lx, attribute "
1961 "type 0x%x, because the new "
1962 "allocation would exceed the "
1963 "maximum allowed size for "
1964 "this attribute type.",
1965 vi->i_ino, (unsigned)
1966 le32_to_cpu(ni->type));
1968 ntfs_error(vol->sb, "Cannot extend allocation "
1969 "of inode 0x%lx, attribute "
1970 "type 0x%x, because this "
1971 "attribute type is not "
1972 "defined on the NTFS volume. "
1973 "Possible corruption! You "
1974 "should run chkdsk!",
1975 vi->i_ino, (unsigned)
1976 le32_to_cpu(ni->type));
1979 /* Translate error code to be POSIX conformant for write(2). */
1989 base_ni = ni->ext.base_ntfs_ino;
1991 * We will be modifying both the runlist (if non-resident) and the mft
1992 * record so lock them both down.
1994 down_write(&ni->runlist.lock);
1995 m = map_mft_record(base_ni);
2002 ctx = ntfs_attr_get_search_ctx(base_ni, m);
2003 if (unlikely(!ctx)) {
2007 read_lock_irqsave(&ni->size_lock, flags);
2008 allocated_size = ni->allocated_size;
2009 read_unlock_irqrestore(&ni->size_lock, flags);
2011 * If non-resident, seek to the last extent. If resident, there is
2012 * only one extent, so seek to that.
2014 vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2017 * Abort if someone did the work whilst we waited for the locks. If we
2018 * just converted the attribute from resident to non-resident it is
2019 * likely that exactly this has happened already. We cannot quite
2020 * abort if we need to update the data size.
2022 if (unlikely(new_alloc_size <= allocated_size)) {
2023 ntfs_debug("Allocated size already exceeds requested size.");
2024 new_alloc_size = allocated_size;
2025 if (new_data_size < 0)
2028 * We want the first attribute extent so that we can update the
2033 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2034 CASE_SENSITIVE, vcn, NULL, 0, ctx);
2035 if (unlikely(err)) {
2042 /* Use goto to reduce indentation. */
2043 if (a->non_resident)
2044 goto do_non_resident_extend;
2045 BUG_ON(NInoNonResident(ni));
2046 /* The total length of the attribute value. */
2047 attr_len = le32_to_cpu(a->data.resident.value_length);
2049 * Extend the attribute record to be able to store the new attribute
2050 * size. ntfs_attr_record_resize() will not do anything if the size is
2053 if (new_alloc_size < vol->mft_record_size &&
2054 !ntfs_attr_record_resize(m, a,
2055 le16_to_cpu(a->data.resident.value_offset) +
2057 /* The resize succeeded! */
2058 write_lock_irqsave(&ni->size_lock, flags);
2059 ni->allocated_size = le32_to_cpu(a->length) -
2060 le16_to_cpu(a->data.resident.value_offset);
2061 write_unlock_irqrestore(&ni->size_lock, flags);
2062 if (new_data_size >= 0) {
2063 BUG_ON(new_data_size < attr_len);
2064 a->data.resident.value_length =
2065 cpu_to_le32((u32)new_data_size);
2070 * We have to drop all the locks so we can call
2071 * ntfs_attr_make_non_resident(). This could be optimised by try-
2072 * locking the first page cache page and only if that fails dropping
2073 * the locks, locking the page, and redoing all the locking and
2074 * lookups. While this would be a huge optimisation, it is not worth
2075 * it as this is definitely a slow code path.
2077 ntfs_attr_put_search_ctx(ctx);
2078 unmap_mft_record(base_ni);
2079 up_write(&ni->runlist.lock);
2081 * Not enough space in the mft record, try to make the attribute
2082 * non-resident and if successful restart the extension process.
2084 err = ntfs_attr_make_non_resident(ni, attr_len);
2088 * Could not make non-resident. If this is due to this not being
2089 * permitted for this attribute type or there not being enough space,
2090 * try to make other attributes non-resident. Otherwise fail.
2092 if (unlikely(err != -EPERM && err != -ENOSPC)) {
2093 /* Only emit errors when the write will fail completely. */
2094 read_lock_irqsave(&ni->size_lock, flags);
2095 allocated_size = ni->allocated_size;
2096 read_unlock_irqrestore(&ni->size_lock, flags);
2097 if (start < 0 || start >= allocated_size)
2098 ntfs_error(vol->sb, "Cannot extend allocation of "
2099 "inode 0x%lx, attribute type 0x%x, "
2100 "because the conversion from resident "
2101 "to non-resident attribute failed "
2102 "with error code %i.", vi->i_ino,
2103 (unsigned)le32_to_cpu(ni->type), err);
2108 /* TODO: Not implemented from here, abort. */
2109 read_lock_irqsave(&ni->size_lock, flags);
2110 allocated_size = ni->allocated_size;
2111 read_unlock_irqrestore(&ni->size_lock, flags);
2112 if (start < 0 || start >= allocated_size) {
2114 ntfs_error(vol->sb, "Not enough space in the mft "
2115 "record/on disk for the non-resident "
2116 "attribute value. This case is not "
2117 "implemented yet.");
2118 else /* if (err == -EPERM) */
2119 ntfs_error(vol->sb, "This attribute type may not be "
2120 "non-resident. This case is not "
2121 "implemented yet.");
2126 // TODO: Attempt to make other attributes non-resident.
2128 goto do_resident_extend;
2130 * Both the attribute list attribute and the standard information
2131 * attribute must remain in the base inode. Thus, if this is one of
2132 * these attributes, we have to try to move other attributes out into
2133 * extent mft records instead.
2135 if (ni->type == AT_ATTRIBUTE_LIST ||
2136 ni->type == AT_STANDARD_INFORMATION) {
2137 // TODO: Attempt to move other attributes into extent mft
2141 goto do_resident_extend;
2144 // TODO: Attempt to move this attribute to an extent mft record, but
2145 // only if it is not already the only attribute in an mft record in
2146 // which case there would be nothing to gain.
2149 goto do_resident_extend;
2150 /* There is nothing we can do to make enough space. )-: */
2153 do_non_resident_extend:
2154 BUG_ON(!NInoNonResident(ni));
2155 if (new_alloc_size == allocated_size) {
2160 * If the data starts after the end of the old allocation, this is a
2161 * $DATA attribute and sparse attributes are enabled on the volume and
2162 * for this inode, then create a sparse region between the old
2163 * allocated size and the start of the data. Otherwise simply proceed
2164 * with filling the whole space between the old allocated size and the
2165 * new allocated size with clusters.
2167 if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2168 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2170 // TODO: This is not implemented yet. We just fill in with real
2171 // clusters for now...
2172 ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
2173 "allocating real clusters instead.");
2175 rl = ni->runlist.rl;
2177 /* Seek to the end of the runlist. */
2181 /* If this attribute extent is not mapped, map it now. */
2182 if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2183 (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2184 (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2185 if (!rl && !allocated_size)
2187 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2190 if (start < 0 || start >= allocated_size)
2191 ntfs_error(vol->sb, "Cannot extend allocation "
2192 "of inode 0x%lx, attribute "
2193 "type 0x%x, because the "
2194 "mapping of a runlist "
2195 "fragment failed with error "
2196 "code %i.", vi->i_ino,
2197 (unsigned)le32_to_cpu(ni->type),
2203 ni->runlist.rl = rl;
2204 /* Seek to the end of the runlist. */
2209 * We now know the runlist of the last extent is mapped and @rl is at
2210 * the end of the runlist. We want to begin allocating clusters
2211 * starting at the last allocated cluster to reduce fragmentation. If
2212 * there are no valid LCNs in the attribute we let the cluster
2213 * allocator choose the starting cluster.
2215 /* If the last LCN is a hole or simillar seek back to last real LCN. */
2216 while (rl->lcn < 0 && rl > ni->runlist.rl)
2219 // FIXME: Need to implement partial allocations so at least part of the
2220 // write can be performed when start >= 0. (Needed for POSIX write(2)
2222 rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2223 (new_alloc_size - allocated_size) >>
2224 vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2225 rl->lcn + rl->length : -1, DATA_ZONE, true);
2228 if (start < 0 || start >= allocated_size)
2229 ntfs_error(vol->sb, "Cannot extend allocation of "
2230 "inode 0x%lx, attribute type 0x%x, "
2231 "because the allocation of clusters "
2232 "failed with error code %i.", vi->i_ino,
2233 (unsigned)le32_to_cpu(ni->type), err);
2234 if (err != -ENOMEM && err != -ENOSPC)
2238 rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2241 if (start < 0 || start >= allocated_size)
2242 ntfs_error(vol->sb, "Cannot extend allocation of "
2243 "inode 0x%lx, attribute type 0x%x, "
2244 "because the runlist merge failed "
2245 "with error code %i.", vi->i_ino,
2246 (unsigned)le32_to_cpu(ni->type), err);
2249 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2250 ntfs_error(vol->sb, "Failed to release allocated "
2251 "cluster(s) in error code path. Run "
2252 "chkdsk to recover the lost "
2259 ni->runlist.rl = rl;
2260 ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2261 allocated_size) >> vol->cluster_size_bits);
2262 /* Find the runlist element with which the attribute extent starts. */
2263 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2264 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2266 BUG_ON(!rl2->length);
2267 BUG_ON(rl2->lcn < LCN_HOLE);
2269 /* Get the size for the new mapping pairs array for this extent. */
2270 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2271 if (unlikely(mp_size <= 0)) {
2273 if (start < 0 || start >= allocated_size)
2274 ntfs_error(vol->sb, "Cannot extend allocation of "
2275 "inode 0x%lx, attribute type 0x%x, "
2276 "because determining the size for the "
2277 "mapping pairs failed with error code "
2279 (unsigned)le32_to_cpu(ni->type), err);
2283 /* Extend the attribute record to fit the bigger mapping pairs array. */
2284 attr_len = le32_to_cpu(a->length);
2285 err = ntfs_attr_record_resize(m, a, mp_size +
2286 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2287 if (unlikely(err)) {
2288 BUG_ON(err != -ENOSPC);
2289 // TODO: Deal with this by moving this extent to a new mft
2290 // record or by starting a new extent in a new mft record,
2291 // possibly by extending this extent partially and filling it
2292 // and creating a new extent for the remainder, or by making
2293 // other attributes non-resident and/or by moving other
2294 // attributes out of this mft record.
2295 if (start < 0 || start >= allocated_size)
2296 ntfs_error(vol->sb, "Not enough space in the mft "
2297 "record for the extended attribute "
2298 "record. This case is not "
2299 "implemented yet.");
2304 /* Generate the mapping pairs array directly into the attr record. */
2305 err = ntfs_mapping_pairs_build(vol, (u8*)a +
2306 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2307 mp_size, rl2, ll, -1, NULL);
2308 if (unlikely(err)) {
2309 if (start < 0 || start >= allocated_size)
2310 ntfs_error(vol->sb, "Cannot extend allocation of "
2311 "inode 0x%lx, attribute type 0x%x, "
2312 "because building the mapping pairs "
2313 "failed with error code %i.", vi->i_ino,
2314 (unsigned)le32_to_cpu(ni->type), err);
2318 /* Update the highest_vcn. */
2319 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2320 vol->cluster_size_bits) - 1);
2322 * We now have extended the allocated size of the attribute. Reflect
2323 * this in the ntfs_inode structure and the attribute record.
2325 if (a->data.non_resident.lowest_vcn) {
2327 * We are not in the first attribute extent, switch to it, but
2328 * first ensure the changes will make it to disk later.
2330 flush_dcache_mft_record_page(ctx->ntfs_ino);
2331 mark_mft_record_dirty(ctx->ntfs_ino);
2332 ntfs_attr_reinit_search_ctx(ctx);
2333 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2334 CASE_SENSITIVE, 0, NULL, 0, ctx);
2336 goto restore_undo_alloc;
2337 /* @m is not used any more so no need to set it. */
2340 write_lock_irqsave(&ni->size_lock, flags);
2341 ni->allocated_size = new_alloc_size;
2342 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2344 * FIXME: This would fail if @ni is a directory, $MFT, or an index,
2345 * since those can have sparse/compressed set. For example can be
2346 * set compressed even though it is not compressed itself and in that
2347 * case the bit means that files are to be created compressed in the
2348 * directory... At present this is ok as this code is only called for
2349 * regular files, and only for their $DATA attribute(s).
2350 * FIXME: The calculation is wrong if we created a hole above. For now
2351 * it does not matter as we never create holes.
2353 if (NInoSparse(ni) || NInoCompressed(ni)) {
2354 ni->itype.compressed.size += new_alloc_size - allocated_size;
2355 a->data.non_resident.compressed_size =
2356 cpu_to_sle64(ni->itype.compressed.size);
2357 vi->i_blocks = ni->itype.compressed.size >> 9;
2359 vi->i_blocks = new_alloc_size >> 9;
2360 write_unlock_irqrestore(&ni->size_lock, flags);
2362 if (new_data_size >= 0) {
2363 BUG_ON(new_data_size <
2364 sle64_to_cpu(a->data.non_resident.data_size));
2365 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2368 /* Ensure the changes make it to disk. */
2369 flush_dcache_mft_record_page(ctx->ntfs_ino);
2370 mark_mft_record_dirty(ctx->ntfs_ino);
2372 ntfs_attr_put_search_ctx(ctx);
2373 unmap_mft_record(base_ni);
2374 up_write(&ni->runlist.lock);
2375 ntfs_debug("Done, new_allocated_size 0x%llx.",
2376 (unsigned long long)new_alloc_size);
2377 return new_alloc_size;
2379 if (start < 0 || start >= allocated_size)
2380 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2381 "of inode 0x%lx, attribute type 0x%x, because "
2382 "lookup of first attribute extent failed with "
2383 "error code %i.", vi->i_ino,
2384 (unsigned)le32_to_cpu(ni->type), err);
2387 ntfs_attr_reinit_search_ctx(ctx);
2388 if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2389 allocated_size >> vol->cluster_size_bits, NULL, 0,
2391 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2392 "attribute in error code path. Run chkdsk to "
2394 write_lock_irqsave(&ni->size_lock, flags);
2395 ni->allocated_size = new_alloc_size;
2397 * FIXME: This would fail if @ni is a directory... See above.
2398 * FIXME: The calculation is wrong if we created a hole above.
2399 * For now it does not matter as we never create holes.
2401 if (NInoSparse(ni) || NInoCompressed(ni)) {
2402 ni->itype.compressed.size += new_alloc_size -
2404 vi->i_blocks = ni->itype.compressed.size >> 9;
2406 vi->i_blocks = new_alloc_size >> 9;
2407 write_unlock_irqrestore(&ni->size_lock, flags);
2408 ntfs_attr_put_search_ctx(ctx);
2409 unmap_mft_record(base_ni);
2410 up_write(&ni->runlist.lock);
2412 * The only thing that is now wrong is the allocated size of the
2413 * base attribute extent which chkdsk should be able to fix.
2418 ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2419 (allocated_size >> vol->cluster_size_bits) - 1);
2421 ll = allocated_size >> vol->cluster_size_bits;
2422 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2423 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2424 "in error code path. Run chkdsk to recover "
2425 "the lost cluster(s).");
2431 * If the runlist truncation fails and/or the search context is no
2432 * longer valid, we cannot resize the attribute record or build the
2433 * mapping pairs array thus we mark the inode bad so that no access to
2434 * the freed clusters can happen.
2436 if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2437 ntfs_error(vol->sb, "Failed to %s in error code path. Run "
2438 "chkdsk to recover.", IS_ERR(m) ?
2439 "restore attribute search context" :
2440 "truncate attribute runlist");
2442 } else if (mp_rebuilt) {
2443 if (ntfs_attr_record_resize(m, a, attr_len)) {
2444 ntfs_error(vol->sb, "Failed to restore attribute "
2445 "record in error code path. Run "
2446 "chkdsk to recover.");
2448 } else /* if (success) */ {
2449 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2450 a->data.non_resident.
2451 mapping_pairs_offset), attr_len -
2452 le16_to_cpu(a->data.non_resident.
2453 mapping_pairs_offset), rl2, ll, -1,
2455 ntfs_error(vol->sb, "Failed to restore "
2456 "mapping pairs array in error "
2457 "code path. Run chkdsk to "
2461 flush_dcache_mft_record_page(ctx->ntfs_ino);
2462 mark_mft_record_dirty(ctx->ntfs_ino);
2467 ntfs_attr_put_search_ctx(ctx);
2469 unmap_mft_record(base_ni);
2470 up_write(&ni->runlist.lock);
2472 ntfs_debug("Failed. Returning error code %i.", err);
2477 * ntfs_attr_set - fill (a part of) an attribute with a byte
2478 * @ni: ntfs inode describing the attribute to fill
2479 * @ofs: offset inside the attribute at which to start to fill
2480 * @cnt: number of bytes to fill
2481 * @val: the unsigned 8-bit value with which to fill the attribute
2483 * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
2484 * byte offset @ofs inside the attribute with the constant byte @val.
2486 * This function is effectively like memset() applied to an ntfs attribute.
2487 * Note thie function actually only operates on the page cache pages belonging
2488 * to the ntfs attribute and it marks them dirty after doing the memset().
2489 * Thus it relies on the vm dirty page write code paths to cause the modified
2490 * pages to be written to the mft record/disk.
2492 * Return 0 on success and -errno on error. An error code of -ESPIPE means
2493 * that @ofs + @cnt were outside the end of the attribute and no write was
2496 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2498 ntfs_volume *vol = ni->vol;
2499 struct address_space *mapping;
2503 unsigned int start_ofs, end_ofs, size;
2505 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2506 (long long)ofs, (long long)cnt, val);
2512 * FIXME: Compressed and encrypted attributes are not supported when
2513 * writing and we should never have gotten here for them.
2515 BUG_ON(NInoCompressed(ni));
2516 BUG_ON(NInoEncrypted(ni));
2517 mapping = VFS_I(ni)->i_mapping;
2518 /* Work out the starting index and page offset. */
2519 idx = ofs >> PAGE_CACHE_SHIFT;
2520 start_ofs = ofs & ~PAGE_CACHE_MASK;
2521 /* Work out the ending index and page offset. */
2523 end_ofs = end & ~PAGE_CACHE_MASK;
2524 /* If the end is outside the inode size return -ESPIPE. */
2525 if (unlikely(end > i_size_read(VFS_I(ni)))) {
2526 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2529 end >>= PAGE_CACHE_SHIFT;
2530 /* If there is a first partial page, need to do it the slow way. */
2532 page = read_mapping_page(mapping, idx, NULL);
2534 ntfs_error(vol->sb, "Failed to read first partial "
2535 "page (sync error, index 0x%lx).", idx);
2536 return PTR_ERR(page);
2538 wait_on_page_locked(page);
2539 if (unlikely(!PageUptodate(page))) {
2540 ntfs_error(vol->sb, "Failed to read first partial page "
2541 "(async error, index 0x%lx).", idx);
2542 page_cache_release(page);
2543 return PTR_ERR(page);
2546 * If the last page is the same as the first page, need to
2547 * limit the write to the end offset.
2549 size = PAGE_CACHE_SIZE;
2552 kaddr = kmap_atomic(page, KM_USER0);
2553 memset(kaddr + start_ofs, val, size - start_ofs);
2554 flush_dcache_page(page);
2555 kunmap_atomic(kaddr, KM_USER0);
2556 set_page_dirty(page);
2557 page_cache_release(page);
2562 /* Do the whole pages the fast way. */
2563 for (; idx < end; idx++) {
2564 /* Find or create the current page. (The page is locked.) */
2565 page = grab_cache_page(mapping, idx);
2566 if (unlikely(!page)) {
2567 ntfs_error(vol->sb, "Insufficient memory to grab "
2568 "page (index 0x%lx).", idx);
2571 kaddr = kmap_atomic(page, KM_USER0);
2572 memset(kaddr, val, PAGE_CACHE_SIZE);
2573 flush_dcache_page(page);
2574 kunmap_atomic(kaddr, KM_USER0);
2576 * If the page has buffers, mark them uptodate since buffer
2577 * state and not page state is definitive in 2.6 kernels.
2579 if (page_has_buffers(page)) {
2580 struct buffer_head *bh, *head;
2582 bh = head = page_buffers(page);
2584 set_buffer_uptodate(bh);
2585 } while ((bh = bh->b_this_page) != head);
2587 /* Now that buffers are uptodate, set the page uptodate, too. */
2588 SetPageUptodate(page);
2590 * Set the page and all its buffers dirty and mark the inode
2591 * dirty, too. The VM will write the page later on.
2593 set_page_dirty(page);
2594 /* Finally unlock and release the page. */
2596 page_cache_release(page);
2597 balance_dirty_pages_ratelimited(mapping);
2600 /* If there is a last partial page, need to do it the slow way. */
2602 page = read_mapping_page(mapping, idx, NULL);
2604 ntfs_error(vol->sb, "Failed to read last partial page "
2605 "(sync error, index 0x%lx).", idx);
2606 return PTR_ERR(page);
2608 wait_on_page_locked(page);
2609 if (unlikely(!PageUptodate(page))) {
2610 ntfs_error(vol->sb, "Failed to read last partial page "
2611 "(async error, index 0x%lx).", idx);
2612 page_cache_release(page);
2613 return PTR_ERR(page);
2615 kaddr = kmap_atomic(page, KM_USER0);
2616 memset(kaddr, val, end_ofs);
2617 flush_dcache_page(page);
2618 kunmap_atomic(kaddr, KM_USER0);
2619 set_page_dirty(page);
2620 page_cache_release(page);
2623 ntfs_debug("Done.");
2627 #endif /* NTFS_RW */