initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / ntfs / inode.c
blob873c25d9830ffb625beb5f3b03f4bf3ca3373bf2
1 /**
2 * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2004 Anton Altaparmakov
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the Linux-NTFS
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/pagemap.h>
23 #include <linux/buffer_head.h>
24 #include <linux/smp_lock.h>
25 #include <linux/quotaops.h>
26 #include <linux/mount.h>
28 #include "ntfs.h"
29 #include "dir.h"
30 #include "inode.h"
31 #include "attrib.h"
32 #include "time.h"
34 /**
35 * ntfs_test_inode - compare two (possibly fake) inodes for equality
36 * @vi: vfs inode which to test
37 * @na: ntfs attribute which is being tested with
39 * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
40 * inode @vi for equality with the ntfs attribute @na.
42 * If searching for the normal file/directory inode, set @na->type to AT_UNUSED.
43 * @na->name and @na->name_len are then ignored.
45 * Return 1 if the attributes match and 0 if not.
47 * NOTE: This function runs with the inode_lock spin lock held so it is not
48 * allowed to sleep.
50 int ntfs_test_inode(struct inode *vi, ntfs_attr *na)
52 ntfs_inode *ni;
54 if (vi->i_ino != na->mft_no)
55 return 0;
56 ni = NTFS_I(vi);
57 /* If !NInoAttr(ni), @vi is a normal file or directory inode. */
58 if (likely(!NInoAttr(ni))) {
59 /* If not looking for a normal inode this is a mismatch. */
60 if (unlikely(na->type != AT_UNUSED))
61 return 0;
62 } else {
63 /* A fake inode describing an attribute. */
64 if (ni->type != na->type)
65 return 0;
66 if (ni->name_len != na->name_len)
67 return 0;
68 if (na->name_len && memcmp(ni->name, na->name,
69 na->name_len * sizeof(ntfschar)))
70 return 0;
72 /* Match! */
73 return 1;
76 /**
77 * ntfs_init_locked_inode - initialize an inode
78 * @vi: vfs inode to initialize
79 * @na: ntfs attribute which to initialize @vi to
81 * Initialize the vfs inode @vi with the values from the ntfs attribute @na in
82 * order to enable ntfs_test_inode() to do its work.
84 * If initializing the normal file/directory inode, set @na->type to AT_UNUSED.
85 * In that case, @na->name and @na->name_len should be set to NULL and 0,
86 * respectively. Although that is not strictly necessary as
87 * ntfs_read_inode_locked() will fill them in later.
89 * Return 0 on success and -errno on error.
91 * NOTE: This function runs with the inode_lock spin lock held so it is not
92 * allowed to sleep. (Hence the GFP_ATOMIC allocation.)
94 static int ntfs_init_locked_inode(struct inode *vi, ntfs_attr *na)
96 ntfs_inode *ni = NTFS_I(vi);
98 vi->i_ino = na->mft_no;
100 ni->type = na->type;
101 if (na->type == AT_INDEX_ALLOCATION)
102 NInoSetMstProtected(ni);
104 ni->name = na->name;
105 ni->name_len = na->name_len;
107 /* If initializing a normal inode, we are done. */
108 if (likely(na->type == AT_UNUSED)) {
109 BUG_ON(na->name);
110 BUG_ON(na->name_len);
111 return 0;
114 /* It is a fake inode. */
115 NInoSetAttr(ni);
118 * We have I30 global constant as an optimization as it is the name
119 * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
120 * allocation but that is ok. And most attributes are unnamed anyway,
121 * thus the fraction of named attributes with name != I30 is actually
122 * absolutely tiny.
124 if (na->name_len && na->name != I30) {
125 unsigned int i;
127 BUG_ON(!na->name);
128 i = na->name_len * sizeof(ntfschar);
129 ni->name = (ntfschar*)kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
130 if (!ni->name)
131 return -ENOMEM;
132 memcpy(ni->name, na->name, i);
133 ni->name[i] = 0;
135 return 0;
138 typedef int (*set_t)(struct inode *, void *);
139 static int ntfs_read_locked_inode(struct inode *vi);
140 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
141 static int ntfs_read_locked_index_inode(struct inode *base_vi,
142 struct inode *vi);
145 * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
146 * @sb: super block of mounted volume
147 * @mft_no: mft record number / inode number to obtain
149 * Obtain the struct inode corresponding to a specific normal inode (i.e. a
150 * file or directory).
152 * If the inode is in the cache, it is just returned with an increased
153 * reference count. Otherwise, a new struct inode is allocated and initialized,
154 * and finally ntfs_read_locked_inode() is called to read in the inode and
155 * fill in the remainder of the inode structure.
157 * Return the struct inode on success. Check the return value with IS_ERR() and
158 * if true, the function failed and the error code is obtained from PTR_ERR().
160 struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
162 struct inode *vi;
163 ntfs_attr na;
164 int err;
166 na.mft_no = mft_no;
167 na.type = AT_UNUSED;
168 na.name = NULL;
169 na.name_len = 0;
171 vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode,
172 (set_t)ntfs_init_locked_inode, &na);
173 if (!vi)
174 return ERR_PTR(-ENOMEM);
176 err = 0;
178 /* If this is a freshly allocated inode, need to read it now. */
179 if (vi->i_state & I_NEW) {
180 err = ntfs_read_locked_inode(vi);
181 unlock_new_inode(vi);
184 * There is no point in keeping bad inodes around if the failure was
185 * due to ENOMEM. We want to be able to retry again later.
187 if (err == -ENOMEM) {
188 iput(vi);
189 vi = ERR_PTR(err);
191 return vi;
195 * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
196 * @base_vi: vfs base inode containing the attribute
197 * @type: attribute type
198 * @name: Unicode name of the attribute (NULL if unnamed)
199 * @name_len: length of @name in Unicode characters (0 if unnamed)
201 * Obtain the (fake) struct inode corresponding to the attribute specified by
202 * @type, @name, and @name_len, which is present in the base mft record
203 * specified by the vfs inode @base_vi.
205 * If the attribute inode is in the cache, it is just returned with an
206 * increased reference count. Otherwise, a new struct inode is allocated and
207 * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
208 * attribute and fill in the inode structure.
210 * Note, for index allocation attributes, you need to use ntfs_index_iget()
211 * instead of ntfs_attr_iget() as working with indices is a lot more complex.
213 * Return the struct inode of the attribute inode on success. Check the return
214 * value with IS_ERR() and if true, the function failed and the error code is
215 * obtained from PTR_ERR().
217 struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
218 ntfschar *name, u32 name_len)
220 struct inode *vi;
221 ntfs_attr na;
222 int err;
224 /* Make sure no one calls ntfs_attr_iget() for indices. */
225 BUG_ON(type == AT_INDEX_ALLOCATION);
227 na.mft_no = base_vi->i_ino;
228 na.type = type;
229 na.name = name;
230 na.name_len = name_len;
232 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
233 (set_t)ntfs_init_locked_inode, &na);
234 if (!vi)
235 return ERR_PTR(-ENOMEM);
237 err = 0;
239 /* If this is a freshly allocated inode, need to read it now. */
240 if (vi->i_state & I_NEW) {
241 err = ntfs_read_locked_attr_inode(base_vi, vi);
242 unlock_new_inode(vi);
245 * There is no point in keeping bad attribute inodes around. This also
246 * simplifies things in that we never need to check for bad attribute
247 * inodes elsewhere.
249 if (err) {
250 iput(vi);
251 vi = ERR_PTR(err);
253 return vi;
257 * ntfs_index_iget - obtain a struct inode corresponding to an index
258 * @base_vi: vfs base inode containing the index related attributes
259 * @name: Unicode name of the index
260 * @name_len: length of @name in Unicode characters
262 * Obtain the (fake) struct inode corresponding to the index specified by @name
263 * and @name_len, which is present in the base mft record specified by the vfs
264 * inode @base_vi.
266 * If the index inode is in the cache, it is just returned with an increased
267 * reference count. Otherwise, a new struct inode is allocated and
268 * initialized, and finally ntfs_read_locked_index_inode() is called to read
269 * the index related attributes and fill in the inode structure.
271 * Return the struct inode of the index inode on success. Check the return
272 * value with IS_ERR() and if true, the function failed and the error code is
273 * obtained from PTR_ERR().
275 struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
276 u32 name_len)
278 struct inode *vi;
279 ntfs_attr na;
280 int err;
282 na.mft_no = base_vi->i_ino;
283 na.type = AT_INDEX_ALLOCATION;
284 na.name = name;
285 na.name_len = name_len;
287 vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
288 (set_t)ntfs_init_locked_inode, &na);
289 if (!vi)
290 return ERR_PTR(-ENOMEM);
292 err = 0;
294 /* If this is a freshly allocated inode, need to read it now. */
295 if (vi->i_state & I_NEW) {
296 err = ntfs_read_locked_index_inode(base_vi, vi);
297 unlock_new_inode(vi);
300 * There is no point in keeping bad index inodes around. This also
301 * simplifies things in that we never need to check for bad index
302 * inodes elsewhere.
304 if (err) {
305 iput(vi);
306 vi = ERR_PTR(err);
308 return vi;
311 struct inode *ntfs_alloc_big_inode(struct super_block *sb)
313 ntfs_inode *ni;
315 ntfs_debug("Entering.");
316 ni = (ntfs_inode *)kmem_cache_alloc(ntfs_big_inode_cache,
317 SLAB_NOFS);
318 if (likely(ni != NULL)) {
319 ni->state = 0;
320 return VFS_I(ni);
322 ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
323 return NULL;
326 void ntfs_destroy_big_inode(struct inode *inode)
328 ntfs_inode *ni = NTFS_I(inode);
330 ntfs_debug("Entering.");
331 BUG_ON(ni->page);
332 if (!atomic_dec_and_test(&ni->count))
333 BUG();
334 kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
337 static inline ntfs_inode *ntfs_alloc_extent_inode(void)
339 ntfs_inode *ni;
341 ntfs_debug("Entering.");
342 ni = (ntfs_inode *)kmem_cache_alloc(ntfs_inode_cache, SLAB_NOFS);
343 if (likely(ni != NULL)) {
344 ni->state = 0;
345 return ni;
347 ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
348 return NULL;
351 void ntfs_destroy_extent_inode(ntfs_inode *ni)
353 ntfs_debug("Entering.");
354 BUG_ON(ni->page);
355 if (!atomic_dec_and_test(&ni->count))
356 BUG();
357 kmem_cache_free(ntfs_inode_cache, ni);
361 * __ntfs_init_inode - initialize ntfs specific part of an inode
362 * @sb: super block of mounted volume
363 * @ni: freshly allocated ntfs inode which to initialize
365 * Initialize an ntfs inode to defaults.
367 * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
368 * untouched. Make sure to initialize them elsewhere.
370 * Return zero on success and -ENOMEM on error.
372 static void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
374 ntfs_debug("Entering.");
375 ni->initialized_size = ni->allocated_size = 0;
376 ni->seq_no = 0;
377 atomic_set(&ni->count, 1);
378 ni->vol = NTFS_SB(sb);
379 init_runlist(&ni->runlist);
380 init_MUTEX(&ni->mrec_lock);
381 ni->page = NULL;
382 ni->page_ofs = 0;
383 ni->attr_list_size = 0;
384 ni->attr_list = NULL;
385 init_runlist(&ni->attr_list_rl);
386 ni->itype.index.bmp_ino = NULL;
387 ni->itype.index.block_size = 0;
388 ni->itype.index.vcn_size = 0;
389 ni->itype.index.collation_rule = 0;
390 ni->itype.index.block_size_bits = 0;
391 ni->itype.index.vcn_size_bits = 0;
392 init_MUTEX(&ni->extent_lock);
393 ni->nr_extents = 0;
394 ni->ext.base_ntfs_ino = NULL;
395 return;
398 static inline void ntfs_init_big_inode(struct inode *vi)
400 ntfs_inode *ni = NTFS_I(vi);
402 ntfs_debug("Entering.");
403 __ntfs_init_inode(vi->i_sb, ni);
404 ni->mft_no = vi->i_ino;
405 return;
408 inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
409 unsigned long mft_no)
411 ntfs_inode *ni = ntfs_alloc_extent_inode();
413 ntfs_debug("Entering.");
414 if (likely(ni != NULL)) {
415 __ntfs_init_inode(sb, ni);
416 ni->mft_no = mft_no;
417 ni->type = AT_UNUSED;
418 ni->name = NULL;
419 ni->name_len = 0;
421 return ni;
425 * ntfs_is_extended_system_file - check if a file is in the $Extend directory
426 * @ctx: initialized attribute search context
428 * Search all file name attributes in the inode described by the attribute
429 * search context @ctx and check if any of the names are in the $Extend system
430 * directory.
432 * Return values:
433 * 1: file is in $Extend directory
434 * 0: file is not in $Extend directory
435 * -errno: failed to determine if the file is in the $Extend directory
437 static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx)
439 int nr_links, err;
441 /* Restart search. */
442 ntfs_attr_reinit_search_ctx(ctx);
444 /* Get number of hard links. */
445 nr_links = le16_to_cpu(ctx->mrec->link_count);
447 /* Loop through all hard links. */
448 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
449 ctx))) {
450 FILE_NAME_ATTR *file_name_attr;
451 ATTR_RECORD *attr = ctx->attr;
452 u8 *p, *p2;
454 nr_links--;
456 * Maximum sanity checking as we are called on an inode that
457 * we suspect might be corrupt.
459 p = (u8*)attr + le32_to_cpu(attr->length);
460 if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
461 le32_to_cpu(ctx->mrec->bytes_in_use)) {
462 err_corrupt_attr:
463 ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
464 "attribute. You should run chkdsk.");
465 return -EIO;
467 if (attr->non_resident) {
468 ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
469 "name. You should run chkdsk.");
470 return -EIO;
472 if (attr->flags) {
473 ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
474 "invalid flags. You should run "
475 "chkdsk.");
476 return -EIO;
478 if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
479 ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
480 "name. You should run chkdsk.");
481 return -EIO;
483 file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
484 le16_to_cpu(attr->data.resident.value_offset));
485 p2 = (u8*)attr + le32_to_cpu(attr->data.resident.value_length);
486 if (p2 < (u8*)attr || p2 > p)
487 goto err_corrupt_attr;
488 /* This attribute is ok, but is it in the $Extend directory? */
489 if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
490 return 1; /* YES, it's an extended system file. */
492 if (unlikely(err != -ENOENT))
493 return err;
494 if (unlikely(nr_links)) {
495 ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
496 "doesn't match number of name attributes. You "
497 "should run chkdsk.");
498 return -EIO;
500 return 0; /* NO, it is not an extended system file. */
504 * ntfs_read_locked_inode - read an inode from its device
505 * @vi: inode to read
507 * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
508 * described by @vi into memory from the device.
510 * The only fields in @vi that we need to/can look at when the function is
511 * called are i_sb, pointing to the mounted device's super block, and i_ino,
512 * the number of the inode to load.
514 * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
515 * for reading and sets up the necessary @vi fields as well as initializing
516 * the ntfs inode.
518 * Q: What locks are held when the function is called?
519 * A: i_state has I_LOCK set, hence the inode is locked, also
520 * i_count is set to 1, so it is not going to go away
521 * i_flags is set to 0 and we have no business touching it. Only an ioctl()
522 * is allowed to write to them. We should of course be honouring them but
523 * we need to do that using the IS_* macros defined in include/linux/fs.h.
524 * In any case ntfs_read_locked_inode() has nothing to do with i_flags.
526 * Return 0 on success and -errno on error. In the error case, the inode will
527 * have had make_bad_inode() executed on it.
529 static int ntfs_read_locked_inode(struct inode *vi)
531 ntfs_volume *vol = NTFS_SB(vi->i_sb);
532 ntfs_inode *ni;
533 MFT_RECORD *m;
534 STANDARD_INFORMATION *si;
535 ntfs_attr_search_ctx *ctx;
536 int err = 0;
538 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
540 /* Setup the generic vfs inode parts now. */
542 /* This is the optimal IO size (for stat), not the fs block size. */
543 vi->i_blksize = PAGE_CACHE_SIZE;
545 * This is for checking whether an inode has changed w.r.t. a file so
546 * that the file can be updated if necessary (compare with f_version).
548 vi->i_version = 1;
550 vi->i_uid = vol->uid;
551 vi->i_gid = vol->gid;
552 vi->i_mode = 0;
555 * Initialize the ntfs specific part of @vi special casing
556 * FILE_MFT which we need to do at mount time.
558 if (vi->i_ino != FILE_MFT)
559 ntfs_init_big_inode(vi);
560 ni = NTFS_I(vi);
562 m = map_mft_record(ni);
563 if (IS_ERR(m)) {
564 err = PTR_ERR(m);
565 goto err_out;
567 ctx = ntfs_attr_get_search_ctx(ni, m);
568 if (!ctx) {
569 err = -ENOMEM;
570 goto unm_err_out;
573 if (!(m->flags & MFT_RECORD_IN_USE)) {
574 ntfs_error(vi->i_sb, "Inode is not in use! You should "
575 "run chkdsk.");
576 goto unm_err_out;
578 if (m->base_mft_record) {
579 ntfs_error(vi->i_sb, "Inode is an extent inode! You should "
580 "run chkdsk.");
581 goto unm_err_out;
584 /* Transfer information from mft record into vfs and ntfs inodes. */
585 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
588 * FIXME: Keep in mind that link_count is two for files which have both
589 * a long file name and a short file name as separate entries, so if
590 * we are hiding short file names this will be too high. Either we need
591 * to account for the short file names by subtracting them or we need
592 * to make sure we delete files even though i_nlink is not zero which
593 * might be tricky due to vfs interactions. Need to think about this
594 * some more when implementing the unlink command.
596 vi->i_nlink = le16_to_cpu(m->link_count);
598 * FIXME: Reparse points can have the directory bit set even though
599 * they would be S_IFLNK. Need to deal with this further below when we
600 * implement reparse points / symbolic links but it will do for now.
601 * Also if not a directory, it could be something else, rather than
602 * a regular file. But again, will do for now.
604 if (m->flags & MFT_RECORD_IS_DIRECTORY) {
605 vi->i_mode |= S_IFDIR;
606 /* Things break without this kludge! */
607 if (vi->i_nlink > 1)
608 vi->i_nlink = 1;
609 } else
610 vi->i_mode |= S_IFREG;
613 * Find the standard information attribute in the mft record. At this
614 * stage we haven't setup the attribute list stuff yet, so this could
615 * in fact fail if the standard information is in an extent record, but
616 * I don't think this actually ever happens.
618 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
619 ctx);
620 if (unlikely(err)) {
621 if (err == -ENOENT) {
623 * TODO: We should be performing a hot fix here (if the
624 * recover mount option is set) by creating a new
625 * attribute.
627 ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute "
628 "is missing.");
630 goto unm_err_out;
632 /* Get the standard information attribute value. */
633 si = (STANDARD_INFORMATION*)((char*)ctx->attr +
634 le16_to_cpu(ctx->attr->data.resident.value_offset));
636 /* Transfer information from the standard information into vi. */
638 * Note: The i_?times do not quite map perfectly onto the NTFS times,
639 * but they are close enough, and in the end it doesn't really matter
640 * that much...
643 * mtime is the last change of the data within the file. Not changed
644 * when only metadata is changed, e.g. a rename doesn't affect mtime.
646 vi->i_mtime = ntfs2utc(si->last_data_change_time);
648 * ctime is the last change of the metadata of the file. This obviously
649 * always changes, when mtime is changed. ctime can be changed on its
650 * own, mtime is then not changed, e.g. when a file is renamed.
652 vi->i_ctime = ntfs2utc(si->last_mft_change_time);
654 * Last access to the data within the file. Not changed during a rename
655 * for example but changed whenever the file is written to.
657 vi->i_atime = ntfs2utc(si->last_access_time);
659 /* Find the attribute list attribute if present. */
660 ntfs_attr_reinit_search_ctx(ctx);
661 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
662 if (err) {
663 if (unlikely(err != -ENOENT)) {
664 ntfs_error(vi->i_sb, "Failed to lookup attribute list "
665 "attribute. You should run chkdsk.");
666 goto unm_err_out;
668 } else /* if (!err) */ {
669 if (vi->i_ino == FILE_MFT)
670 goto skip_attr_list_load;
671 ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
672 NInoSetAttrList(ni);
673 if (ctx->attr->flags & ATTR_IS_ENCRYPTED ||
674 ctx->attr->flags & ATTR_COMPRESSION_MASK ||
675 ctx->attr->flags & ATTR_IS_SPARSE) {
676 ntfs_error(vi->i_sb, "Attribute list attribute is "
677 "compressed/encrypted/sparse. Not "
678 "allowed. Corrupt inode. You should "
679 "run chkdsk.");
680 goto unm_err_out;
682 /* Now allocate memory for the attribute list. */
683 ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr);
684 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
685 if (!ni->attr_list) {
686 ntfs_error(vi->i_sb, "Not enough memory to allocate "
687 "buffer for attribute list.");
688 err = -ENOMEM;
689 goto unm_err_out;
691 if (ctx->attr->non_resident) {
692 NInoSetAttrListNonResident(ni);
693 if (ctx->attr->data.non_resident.lowest_vcn) {
694 ntfs_error(vi->i_sb, "Attribute list has non "
695 "zero lowest_vcn. Inode is "
696 "corrupt. You should run "
697 "chkdsk.");
698 goto unm_err_out;
701 * Setup the runlist. No need for locking as we have
702 * exclusive access to the inode at this time.
704 ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
705 ctx->attr, NULL);
706 if (IS_ERR(ni->attr_list_rl.rl)) {
707 err = PTR_ERR(ni->attr_list_rl.rl);
708 ni->attr_list_rl.rl = NULL;
709 ntfs_error(vi->i_sb, "Mapping pairs "
710 "decompression failed with "
711 "error code %i. Corrupt "
712 "attribute list in inode.",
713 -err);
714 goto unm_err_out;
716 /* Now load the attribute list. */
717 if ((err = load_attribute_list(vol, &ni->attr_list_rl,
718 ni->attr_list, ni->attr_list_size,
719 sle64_to_cpu(ctx->attr->data.
720 non_resident.initialized_size)))) {
721 ntfs_error(vi->i_sb, "Failed to load "
722 "attribute list attribute.");
723 goto unm_err_out;
725 } else /* if (!ctx.attr->non_resident) */ {
726 if ((u8*)ctx->attr + le16_to_cpu(
727 ctx->attr->data.resident.value_offset) +
728 le32_to_cpu(
729 ctx->attr->data.resident.value_length) >
730 (u8*)ctx->mrec + vol->mft_record_size) {
731 ntfs_error(vi->i_sb, "Corrupt attribute list "
732 "in inode.");
733 goto unm_err_out;
735 /* Now copy the attribute list. */
736 memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu(
737 ctx->attr->data.resident.value_offset),
738 le32_to_cpu(
739 ctx->attr->data.resident.value_length));
742 skip_attr_list_load:
744 * If an attribute list is present we now have the attribute list value
745 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
747 if (S_ISDIR(vi->i_mode)) {
748 struct inode *bvi;
749 ntfs_inode *bni;
750 INDEX_ROOT *ir;
751 char *ir_end, *index_end;
753 /* It is a directory, find index root attribute. */
754 ntfs_attr_reinit_search_ctx(ctx);
755 err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE,
756 0, NULL, 0, ctx);
757 if (unlikely(err)) {
758 if (err == -ENOENT) {
759 // FIXME: File is corrupt! Hot-fix with empty
760 // index root attribute if recovery option is
761 // set.
762 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute "
763 "is missing.");
765 goto unm_err_out;
767 /* Set up the state. */
768 if (ctx->attr->non_resident) {
769 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
770 "not resident. Not allowed.");
771 goto unm_err_out;
774 * Compressed/encrypted index root just means that the newly
775 * created files in that directory should be created compressed/
776 * encrypted. However index root cannot be both compressed and
777 * encrypted.
779 if (ctx->attr->flags & ATTR_COMPRESSION_MASK)
780 NInoSetCompressed(ni);
781 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
782 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
783 ntfs_error(vi->i_sb, "Found encrypted and "
784 "compressed attribute. Not "
785 "allowed.");
786 goto unm_err_out;
788 NInoSetEncrypted(ni);
790 if (ctx->attr->flags & ATTR_IS_SPARSE)
791 NInoSetSparse(ni);
792 ir = (INDEX_ROOT*)((char*)ctx->attr + le16_to_cpu(
793 ctx->attr->data.resident.value_offset));
794 ir_end = (char*)ir + le32_to_cpu(
795 ctx->attr->data.resident.value_length);
796 if (ir_end > (char*)ctx->mrec + vol->mft_record_size) {
797 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
798 "corrupt.");
799 goto unm_err_out;
801 index_end = (char*)&ir->index +
802 le32_to_cpu(ir->index.index_length);
803 if (index_end > ir_end) {
804 ntfs_error(vi->i_sb, "Directory index is corrupt.");
805 goto unm_err_out;
807 if (ir->type != AT_FILE_NAME) {
808 ntfs_error(vi->i_sb, "Indexed attribute is not "
809 "$FILE_NAME. Not allowed.");
810 goto unm_err_out;
812 if (ir->collation_rule != COLLATION_FILE_NAME) {
813 ntfs_error(vi->i_sb, "Index collation rule is not "
814 "COLLATION_FILE_NAME. Not allowed.");
815 goto unm_err_out;
817 ni->itype.index.collation_rule = ir->collation_rule;
818 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
819 if (ni->itype.index.block_size &
820 (ni->itype.index.block_size - 1)) {
821 ntfs_error(vi->i_sb, "Index block size (%u) is not a "
822 "power of two.",
823 ni->itype.index.block_size);
824 goto unm_err_out;
826 if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
827 ntfs_error(vi->i_sb, "Index block size (%u) > "
828 "PAGE_CACHE_SIZE (%ld) is not "
829 "supported. Sorry.",
830 ni->itype.index.block_size,
831 PAGE_CACHE_SIZE);
832 err = -EOPNOTSUPP;
833 goto unm_err_out;
835 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
836 ntfs_error(vi->i_sb, "Index block size (%u) < "
837 "NTFS_BLOCK_SIZE (%i) is not "
838 "supported. Sorry.",
839 ni->itype.index.block_size,
840 NTFS_BLOCK_SIZE);
841 err = -EOPNOTSUPP;
842 goto unm_err_out;
844 ni->itype.index.block_size_bits =
845 ffs(ni->itype.index.block_size) - 1;
846 /* Determine the size of a vcn in the directory index. */
847 if (vol->cluster_size <= ni->itype.index.block_size) {
848 ni->itype.index.vcn_size = vol->cluster_size;
849 ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
850 } else {
851 ni->itype.index.vcn_size = vol->sector_size;
852 ni->itype.index.vcn_size_bits = vol->sector_size_bits;
855 /* Setup the index allocation attribute, even if not present. */
856 NInoSetMstProtected(ni);
857 ni->type = AT_INDEX_ALLOCATION;
858 ni->name = I30;
859 ni->name_len = 4;
861 if (!(ir->index.flags & LARGE_INDEX)) {
862 /* No index allocation. */
863 vi->i_size = ni->initialized_size =
864 ni->allocated_size = 0;
865 /* We are done with the mft record, so we release it. */
866 ntfs_attr_put_search_ctx(ctx);
867 unmap_mft_record(ni);
868 m = NULL;
869 ctx = NULL;
870 goto skip_large_dir_stuff;
871 } /* LARGE_INDEX: Index allocation present. Setup state. */
872 NInoSetIndexAllocPresent(ni);
873 /* Find index allocation attribute. */
874 ntfs_attr_reinit_search_ctx(ctx);
875 err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4,
876 CASE_SENSITIVE, 0, NULL, 0, ctx);
877 if (unlikely(err)) {
878 if (err == -ENOENT)
879 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
880 "attribute is not present but "
881 "$INDEX_ROOT indicated it "
882 "is.");
883 else
884 ntfs_error(vi->i_sb, "Failed to lookup "
885 "$INDEX_ALLOCATION "
886 "attribute.");
887 goto unm_err_out;
889 if (!ctx->attr->non_resident) {
890 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
891 "is resident.");
892 goto unm_err_out;
894 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
895 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
896 "is encrypted.");
897 goto unm_err_out;
899 if (ctx->attr->flags & ATTR_IS_SPARSE) {
900 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
901 "is sparse.");
902 goto unm_err_out;
904 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
905 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
906 "is compressed.");
907 goto unm_err_out;
909 if (ctx->attr->data.non_resident.lowest_vcn) {
910 ntfs_error(vi->i_sb, "First extent of "
911 "$INDEX_ALLOCATION attribute has non "
912 "zero lowest_vcn. Inode is corrupt. "
913 "You should run chkdsk.");
914 goto unm_err_out;
916 vi->i_size = sle64_to_cpu(
917 ctx->attr->data.non_resident.data_size);
918 ni->initialized_size = sle64_to_cpu(
919 ctx->attr->data.non_resident.initialized_size);
920 ni->allocated_size = sle64_to_cpu(
921 ctx->attr->data.non_resident.allocated_size);
923 * We are done with the mft record, so we release it. Otherwise
924 * we would deadlock in ntfs_attr_iget().
926 ntfs_attr_put_search_ctx(ctx);
927 unmap_mft_record(ni);
928 m = NULL;
929 ctx = NULL;
930 /* Get the index bitmap attribute inode. */
931 bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
932 if (IS_ERR(bvi)) {
933 ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
934 err = PTR_ERR(bvi);
935 goto unm_err_out;
937 ni->itype.index.bmp_ino = bvi;
938 bni = NTFS_I(bvi);
939 if (NInoCompressed(bni) || NInoEncrypted(bni) ||
940 NInoSparse(bni)) {
941 ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
942 "and/or encrypted and/or sparse.");
943 goto unm_err_out;
945 /* Consistency check bitmap size vs. index allocation size. */
946 if ((bvi->i_size << 3) < (vi->i_size >>
947 ni->itype.index.block_size_bits)) {
948 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
949 "for index allocation (0x%llx).",
950 bvi->i_size << 3, vi->i_size);
951 goto unm_err_out;
953 skip_large_dir_stuff:
954 /* Everyone gets read and scan permissions. */
955 vi->i_mode |= S_IRUGO | S_IXUGO;
956 /* If not read-only, set write permissions. */
957 if (!IS_RDONLY(vi))
958 vi->i_mode |= S_IWUGO;
960 * Apply the directory permissions mask set in the mount
961 * options.
963 vi->i_mode &= ~vol->dmask;
964 /* Setup the operations for this inode. */
965 vi->i_op = &ntfs_dir_inode_ops;
966 vi->i_fop = &ntfs_dir_ops;
967 vi->i_mapping->a_ops = &ntfs_mst_aops;
968 } else {
969 /* It is a file. */
970 ntfs_attr_reinit_search_ctx(ctx);
972 /* Setup the data attribute, even if not present. */
973 ni->type = AT_DATA;
974 ni->name = NULL;
975 ni->name_len = 0;
977 /* Find first extent of the unnamed data attribute. */
978 err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx);
979 if (unlikely(err)) {
980 vi->i_size = ni->initialized_size =
981 ni->allocated_size = 0;
982 if (err != -ENOENT) {
983 ntfs_error(vi->i_sb, "Failed to lookup $DATA "
984 "attribute.");
985 goto unm_err_out;
988 * FILE_Secure does not have an unnamed $DATA
989 * attribute, so we special case it here.
991 if (vi->i_ino == FILE_Secure)
992 goto no_data_attr_special_case;
994 * Most if not all the system files in the $Extend
995 * system directory do not have unnamed data
996 * attributes so we need to check if the parent
997 * directory of the file is FILE_Extend and if it is
998 * ignore this error. To do this we need to get the
999 * name of this inode from the mft record as the name
1000 * contains the back reference to the parent directory.
1002 if (ntfs_is_extended_system_file(ctx) > 0)
1003 goto no_data_attr_special_case;
1004 // FIXME: File is corrupt! Hot-fix with empty data
1005 // attribute if recovery option is set.
1006 ntfs_error(vi->i_sb, "$DATA attribute is "
1007 "missing.");
1008 goto unm_err_out;
1010 /* Setup the state. */
1011 if (ctx->attr->non_resident) {
1012 NInoSetNonResident(ni);
1013 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1014 NInoSetCompressed(ni);
1015 if (vol->cluster_size > 4096) {
1016 ntfs_error(vi->i_sb, "Found "
1017 "compressed data but "
1018 "compression is disabled due "
1019 "to cluster size (%i) > 4kiB.",
1020 vol->cluster_size);
1021 goto unm_err_out;
1023 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
1024 != ATTR_IS_COMPRESSED) {
1025 ntfs_error(vi->i_sb, "Found "
1026 "unknown compression method or "
1027 "corrupt file.");
1028 goto unm_err_out;
1030 ni->itype.compressed.block_clusters = 1U <<
1031 ctx->attr->data.non_resident.
1032 compression_unit;
1033 if (ctx->attr->data.non_resident.
1034 compression_unit != 4) {
1035 ntfs_error(vi->i_sb, "Found "
1036 "nonstandard compression unit "
1037 "(%u instead of 4). Cannot "
1038 "handle this. This might "
1039 "indicate corruption so you "
1040 "should run chkdsk.",
1041 ctx->attr->data.non_resident.
1042 compression_unit);
1043 err = -EOPNOTSUPP;
1044 goto unm_err_out;
1046 ni->itype.compressed.block_size = 1U << (
1047 ctx->attr->data.non_resident.
1048 compression_unit +
1049 vol->cluster_size_bits);
1050 ni->itype.compressed.block_size_bits = ffs(
1051 ni->itype.compressed.block_size) - 1;
1053 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1054 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1055 ntfs_error(vi->i_sb, "Found encrypted "
1056 "and compressed data.");
1057 goto unm_err_out;
1059 NInoSetEncrypted(ni);
1061 if (ctx->attr->flags & ATTR_IS_SPARSE)
1062 NInoSetSparse(ni);
1063 if (ctx->attr->data.non_resident.lowest_vcn) {
1064 ntfs_error(vi->i_sb, "First extent of $DATA "
1065 "attribute has non zero "
1066 "lowest_vcn. Inode is corrupt. "
1067 "You should run chkdsk.");
1068 goto unm_err_out;
1070 /* Setup all the sizes. */
1071 vi->i_size = sle64_to_cpu(
1072 ctx->attr->data.non_resident.data_size);
1073 ni->initialized_size = sle64_to_cpu(
1074 ctx->attr->data.non_resident.
1075 initialized_size);
1076 ni->allocated_size = sle64_to_cpu(
1077 ctx->attr->data.non_resident.
1078 allocated_size);
1079 if (NInoCompressed(ni)) {
1080 ni->itype.compressed.size = sle64_to_cpu(
1081 ctx->attr->data.non_resident.
1082 compressed_size);
1084 } else { /* Resident attribute. */
1086 * Make all sizes equal for simplicity in read code
1087 * paths. FIXME: Need to keep this in mind when
1088 * converting to non-resident attribute in write code
1089 * path. (Probably only affects truncate().)
1091 vi->i_size = ni->initialized_size = ni->allocated_size =
1092 le32_to_cpu(
1093 ctx->attr->data.resident.value_length);
1095 no_data_attr_special_case:
1096 /* We are done with the mft record, so we release it. */
1097 ntfs_attr_put_search_ctx(ctx);
1098 unmap_mft_record(ni);
1099 m = NULL;
1100 ctx = NULL;
1101 /* Everyone gets all permissions. */
1102 vi->i_mode |= S_IRWXUGO;
1103 /* If read-only, noone gets write permissions. */
1104 if (IS_RDONLY(vi))
1105 vi->i_mode &= ~S_IWUGO;
1106 /* Apply the file permissions mask set in the mount options. */
1107 vi->i_mode &= ~vol->fmask;
1108 /* Setup the operations for this inode. */
1109 vi->i_op = &ntfs_file_inode_ops;
1110 vi->i_fop = &ntfs_file_ops;
1111 vi->i_mapping->a_ops = &ntfs_aops;
1114 * The number of 512-byte blocks used on disk (for stat). This is in so
1115 * far inaccurate as it doesn't account for any named streams or other
1116 * special non-resident attributes, but that is how Windows works, too,
1117 * so we are at least consistent with Windows, if not entirely
1118 * consistent with the Linux Way. Doing it the Linux Way would cause a
1119 * significant slowdown as it would involve iterating over all
1120 * attributes in the mft record and adding the allocated/compressed
1121 * sizes of all non-resident attributes present to give us the Linux
1122 * correct size that should go into i_blocks (after division by 512).
1124 if (S_ISDIR(vi->i_mode) || !NInoCompressed(ni))
1125 vi->i_blocks = ni->allocated_size >> 9;
1126 else
1127 vi->i_blocks = ni->itype.compressed.size >> 9;
1129 ntfs_debug("Done.");
1130 return 0;
1132 unm_err_out:
1133 if (!err)
1134 err = -EIO;
1135 if (ctx)
1136 ntfs_attr_put_search_ctx(ctx);
1137 if (m)
1138 unmap_mft_record(ni);
1139 err_out:
1140 ntfs_error(vi->i_sb, "Failed with error code %i. Marking inode 0x%lx "
1141 "as bad.", -err, vi->i_ino);
1142 make_bad_inode(vi);
1143 return err;
1147 * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
1148 * @base_vi: base inode
1149 * @vi: attribute inode to read
1151 * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
1152 * attribute inode described by @vi into memory from the base mft record
1153 * described by @base_ni.
1155 * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
1156 * reading and looks up the attribute described by @vi before setting up the
1157 * necessary fields in @vi as well as initializing the ntfs inode.
1159 * Q: What locks are held when the function is called?
1160 * A: i_state has I_LOCK set, hence the inode is locked, also
1161 * i_count is set to 1, so it is not going to go away
1163 * Return 0 on success and -errno on error. In the error case, the inode will
1164 * have had make_bad_inode() executed on it.
1166 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1168 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1169 ntfs_inode *ni, *base_ni;
1170 MFT_RECORD *m;
1171 ntfs_attr_search_ctx *ctx;
1172 int err = 0;
1174 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
1176 ntfs_init_big_inode(vi);
1178 ni = NTFS_I(vi);
1179 base_ni = NTFS_I(base_vi);
1181 /* Just mirror the values from the base inode. */
1182 vi->i_blksize = base_vi->i_blksize;
1183 vi->i_version = base_vi->i_version;
1184 vi->i_uid = base_vi->i_uid;
1185 vi->i_gid = base_vi->i_gid;
1186 vi->i_nlink = base_vi->i_nlink;
1187 vi->i_mtime = base_vi->i_mtime;
1188 vi->i_ctime = base_vi->i_ctime;
1189 vi->i_atime = base_vi->i_atime;
1190 vi->i_generation = ni->seq_no = base_ni->seq_no;
1192 /* Set inode type to zero but preserve permissions. */
1193 vi->i_mode = base_vi->i_mode & ~S_IFMT;
1195 m = map_mft_record(base_ni);
1196 if (IS_ERR(m)) {
1197 err = PTR_ERR(m);
1198 goto err_out;
1200 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1201 if (!ctx) {
1202 err = -ENOMEM;
1203 goto unm_err_out;
1206 /* Find the attribute. */
1207 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1208 CASE_SENSITIVE, 0, NULL, 0, ctx);
1209 if (unlikely(err))
1210 goto unm_err_out;
1212 if (!ctx->attr->non_resident) {
1213 if (NInoMstProtected(ni) || ctx->attr->flags) {
1214 ntfs_error(vi->i_sb, "Found mst protected attribute "
1215 "or attribute with non-zero flags but "
1216 "the attribute is resident (mft_no "
1217 "0x%lx, type 0x%x, name_len %i). "
1218 "Please report you saw this message "
1219 "to linux-ntfs-dev@lists."
1220 "sourceforge.net",
1221 vi->i_ino, ni->type, ni->name_len);
1222 goto unm_err_out;
1225 * Resident attribute. Make all sizes equal for simplicity in
1226 * read code paths.
1228 vi->i_size = ni->initialized_size = ni->allocated_size =
1229 le32_to_cpu(ctx->attr->data.resident.value_length);
1230 } else {
1231 NInoSetNonResident(ni);
1232 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1233 if (NInoMstProtected(ni)) {
1234 ntfs_error(vi->i_sb, "Found mst protected "
1235 "attribute but the attribute "
1236 "is compressed (mft_no 0x%lx, "
1237 "type 0x%x, name_len %i). "
1238 "Please report you saw this "
1239 "message to linux-ntfs-dev@"
1240 "lists.sourceforge.net",
1241 vi->i_ino, ni->type,
1242 ni->name_len);
1243 goto unm_err_out;
1245 NInoSetCompressed(ni);
1246 if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
1247 ni->name_len)) {
1248 ntfs_error(vi->i_sb, "Found compressed non-"
1249 "data or named data attribute "
1250 "(mft_no 0x%lx, type 0x%x, "
1251 "name_len %i). Please report "
1252 "you saw this message to "
1253 "linux-ntfs-dev@lists."
1254 "sourceforge.net",
1255 vi->i_ino, ni->type,
1256 ni->name_len);
1257 goto unm_err_out;
1259 if (vol->cluster_size > 4096) {
1260 ntfs_error(vi->i_sb, "Found "
1261 "compressed attribute but "
1262 "compression is disabled due "
1263 "to cluster size (%i) > 4kiB.",
1264 vol->cluster_size);
1265 goto unm_err_out;
1267 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
1268 != ATTR_IS_COMPRESSED) {
1269 ntfs_error(vi->i_sb, "Found unknown "
1270 "compression method or "
1271 "corrupt file.");
1272 goto unm_err_out;
1274 ni->itype.compressed.block_clusters = 1U <<
1275 ctx->attr->data.non_resident.
1276 compression_unit;
1277 if (ctx->attr->data.non_resident.compression_unit != 4) {
1278 ntfs_error(vi->i_sb, "Found "
1279 "nonstandard compression unit "
1280 "(%u instead of 4). Cannot "
1281 "handle this. This might "
1282 "indicate corruption so you "
1283 "should run chkdsk.",
1284 ctx->attr->data.non_resident.
1285 compression_unit);
1286 err = -EOPNOTSUPP;
1287 goto unm_err_out;
1289 ni->itype.compressed.block_size = 1U << (
1290 ctx->attr->data.non_resident.
1291 compression_unit +
1292 vol->cluster_size_bits);
1293 ni->itype.compressed.block_size_bits = ffs(
1294 ni->itype.compressed.block_size) - 1;
1296 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1297 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1298 ntfs_error(vi->i_sb, "Found encrypted "
1299 "and compressed data.");
1300 goto unm_err_out;
1302 if (NInoMstProtected(ni)) {
1303 ntfs_error(vi->i_sb, "Found mst protected "
1304 "attribute but the attribute "
1305 "is encrypted (mft_no 0x%lx, "
1306 "type 0x%x, name_len %i). "
1307 "Please report you saw this "
1308 "message to linux-ntfs-dev@"
1309 "lists.sourceforge.net",
1310 vi->i_ino, ni->type,
1311 ni->name_len);
1312 goto unm_err_out;
1314 NInoSetEncrypted(ni);
1316 if (ctx->attr->flags & ATTR_IS_SPARSE) {
1317 if (NInoMstProtected(ni)) {
1318 ntfs_error(vi->i_sb, "Found mst protected "
1319 "attribute but the attribute "
1320 "is sparse (mft_no 0x%lx, "
1321 "type 0x%x, name_len %i). "
1322 "Please report you saw this "
1323 "message to linux-ntfs-dev@"
1324 "lists.sourceforge.net",
1325 vi->i_ino, ni->type,
1326 ni->name_len);
1327 goto unm_err_out;
1329 NInoSetSparse(ni);
1331 if (ctx->attr->data.non_resident.lowest_vcn) {
1332 ntfs_error(vi->i_sb, "First extent of attribute has "
1333 "non-zero lowest_vcn. Inode is "
1334 "corrupt. You should run chkdsk.");
1335 goto unm_err_out;
1337 /* Setup all the sizes. */
1338 vi->i_size = sle64_to_cpu(
1339 ctx->attr->data.non_resident.data_size);
1340 ni->initialized_size = sle64_to_cpu(
1341 ctx->attr->data.non_resident.initialized_size);
1342 ni->allocated_size = sle64_to_cpu(
1343 ctx->attr->data.non_resident.allocated_size);
1344 if (NInoCompressed(ni)) {
1345 ni->itype.compressed.size = sle64_to_cpu(
1346 ctx->attr->data.non_resident.
1347 compressed_size);
1351 /* Setup the operations for this attribute inode. */
1352 vi->i_op = NULL;
1353 vi->i_fop = NULL;
1354 if (NInoMstProtected(ni))
1355 vi->i_mapping->a_ops = &ntfs_mst_aops;
1356 else
1357 vi->i_mapping->a_ops = &ntfs_aops;
1359 if (!NInoCompressed(ni))
1360 vi->i_blocks = ni->allocated_size >> 9;
1361 else
1362 vi->i_blocks = ni->itype.compressed.size >> 9;
1365 * Make sure the base inode doesn't go away and attach it to the
1366 * attribute inode.
1368 igrab(base_vi);
1369 ni->ext.base_ntfs_ino = base_ni;
1370 ni->nr_extents = -1;
1372 ntfs_attr_put_search_ctx(ctx);
1373 unmap_mft_record(base_ni);
1375 ntfs_debug("Done.");
1376 return 0;
1378 unm_err_out:
1379 if (!err)
1380 err = -EIO;
1381 if (ctx)
1382 ntfs_attr_put_search_ctx(ctx);
1383 unmap_mft_record(base_ni);
1384 err_out:
1385 ntfs_error(vi->i_sb, "Failed with error code %i while reading "
1386 "attribute inode (mft_no 0x%lx, type 0x%x, name_len "
1387 "%i.", -err, vi->i_ino, ni->type, ni->name_len);
1388 make_bad_inode(vi);
1389 return err;
1393 * ntfs_read_locked_index_inode - read an index inode from its base inode
1394 * @base_vi: base inode
1395 * @vi: index inode to read
1397 * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
1398 * index inode described by @vi into memory from the base mft record described
1399 * by @base_ni.
1401 * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
1402 * reading and looks up the attributes relating to the index described by @vi
1403 * before setting up the necessary fields in @vi as well as initializing the
1404 * ntfs inode.
1406 * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
1407 * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they
1408 * are setup like directory inodes since directories are a special case of
1409 * indices ao they need to be treated in much the same way. Most importantly,
1410 * for small indices the index allocation attribute might not actually exist.
1411 * However, the index root attribute always exists but this does not need to
1412 * have an inode associated with it and this is why we define a new inode type
1413 * index. Also, like for directories, we need to have an attribute inode for
1414 * the bitmap attribute corresponding to the index allocation attribute and we
1415 * can store this in the appropriate field of the inode, just like we do for
1416 * normal directory inodes.
1418 * Q: What locks are held when the function is called?
1419 * A: i_state has I_LOCK set, hence the inode is locked, also
1420 * i_count is set to 1, so it is not going to go away
1422 * Return 0 on success and -errno on error. In the error case, the inode will
1423 * have had make_bad_inode() executed on it.
1425 static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1427 ntfs_volume *vol = NTFS_SB(vi->i_sb);
1428 ntfs_inode *ni, *base_ni, *bni;
1429 struct inode *bvi;
1430 MFT_RECORD *m;
1431 ntfs_attr_search_ctx *ctx;
1432 INDEX_ROOT *ir;
1433 u8 *ir_end, *index_end;
1434 int err = 0;
1436 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
1437 ntfs_init_big_inode(vi);
1438 ni = NTFS_I(vi);
1439 base_ni = NTFS_I(base_vi);
1440 /* Just mirror the values from the base inode. */
1441 vi->i_blksize = base_vi->i_blksize;
1442 vi->i_version = base_vi->i_version;
1443 vi->i_uid = base_vi->i_uid;
1444 vi->i_gid = base_vi->i_gid;
1445 vi->i_nlink = base_vi->i_nlink;
1446 vi->i_mtime = base_vi->i_mtime;
1447 vi->i_ctime = base_vi->i_ctime;
1448 vi->i_atime = base_vi->i_atime;
1449 vi->i_generation = ni->seq_no = base_ni->seq_no;
1450 /* Set inode type to zero but preserve permissions. */
1451 vi->i_mode = base_vi->i_mode & ~S_IFMT;
1452 /* Map the mft record for the base inode. */
1453 m = map_mft_record(base_ni);
1454 if (IS_ERR(m)) {
1455 err = PTR_ERR(m);
1456 goto err_out;
1458 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1459 if (!ctx) {
1460 err = -ENOMEM;
1461 goto unm_err_out;
1463 /* Find the index root attribute. */
1464 err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len,
1465 CASE_SENSITIVE, 0, NULL, 0, ctx);
1466 if (unlikely(err)) {
1467 if (err == -ENOENT)
1468 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
1469 "missing.");
1470 goto unm_err_out;
1472 /* Set up the state. */
1473 if (ctx->attr->non_resident) {
1474 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is not resident. "
1475 "Not allowed.");
1476 goto unm_err_out;
1478 /* Compressed/encrypted/sparse index root is not allowed. */
1479 if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
1480 ATTR_IS_SPARSE)) {
1481 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
1482 "root attribute. Not allowed.");
1483 goto unm_err_out;
1485 ir = (INDEX_ROOT*)((u8*)ctx->attr +
1486 le16_to_cpu(ctx->attr->data.resident.value_offset));
1487 ir_end = (u8*)ir + le32_to_cpu(ctx->attr->data.resident.value_length);
1488 if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
1489 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
1490 goto unm_err_out;
1492 index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1493 if (index_end > ir_end) {
1494 ntfs_error(vi->i_sb, "Index is corrupt.");
1495 goto unm_err_out;
1497 if (ir->type) {
1498 ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x). "
1499 "Not allowed.", le32_to_cpu(ir->type));
1500 goto unm_err_out;
1502 ni->itype.index.collation_rule = ir->collation_rule;
1503 ntfs_debug("Index collation rule is 0x%x.",
1504 le32_to_cpu(ir->collation_rule));
1505 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
1506 if (ni->itype.index.block_size & (ni->itype.index.block_size - 1)) {
1507 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
1508 "two.", ni->itype.index.block_size);
1509 goto unm_err_out;
1511 if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
1512 ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_CACHE_SIZE "
1513 "(%ld) is not supported. Sorry.",
1514 ni->itype.index.block_size, PAGE_CACHE_SIZE);
1515 err = -EOPNOTSUPP;
1516 goto unm_err_out;
1518 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
1519 ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
1520 "(%i) is not supported. Sorry.",
1521 ni->itype.index.block_size, NTFS_BLOCK_SIZE);
1522 err = -EOPNOTSUPP;
1523 goto unm_err_out;
1525 ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
1526 /* Determine the size of a vcn in the index. */
1527 if (vol->cluster_size <= ni->itype.index.block_size) {
1528 ni->itype.index.vcn_size = vol->cluster_size;
1529 ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
1530 } else {
1531 ni->itype.index.vcn_size = vol->sector_size;
1532 ni->itype.index.vcn_size_bits = vol->sector_size_bits;
1534 /* Check for presence of index allocation attribute. */
1535 if (!(ir->index.flags & LARGE_INDEX)) {
1536 /* No index allocation. */
1537 vi->i_size = ni->initialized_size = ni->allocated_size = 0;
1538 /* We are done with the mft record, so we release it. */
1539 ntfs_attr_put_search_ctx(ctx);
1540 unmap_mft_record(base_ni);
1541 m = NULL;
1542 ctx = NULL;
1543 goto skip_large_index_stuff;
1544 } /* LARGE_INDEX: Index allocation present. Setup state. */
1545 NInoSetIndexAllocPresent(ni);
1546 /* Find index allocation attribute. */
1547 ntfs_attr_reinit_search_ctx(ctx);
1548 err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
1549 CASE_SENSITIVE, 0, NULL, 0, ctx);
1550 if (unlikely(err)) {
1551 if (err == -ENOENT)
1552 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1553 "not present but $INDEX_ROOT "
1554 "indicated it is.");
1555 else
1556 ntfs_error(vi->i_sb, "Failed to lookup "
1557 "$INDEX_ALLOCATION attribute.");
1558 goto unm_err_out;
1560 if (!ctx->attr->non_resident) {
1561 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1562 "resident.");
1563 goto unm_err_out;
1565 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1566 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1567 "encrypted.");
1568 goto unm_err_out;
1570 if (ctx->attr->flags & ATTR_IS_SPARSE) {
1571 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
1572 goto unm_err_out;
1574 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1575 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1576 "compressed.");
1577 goto unm_err_out;
1579 if (ctx->attr->data.non_resident.lowest_vcn) {
1580 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
1581 "attribute has non zero lowest_vcn. Inode is "
1582 "corrupt. You should run chkdsk.");
1583 goto unm_err_out;
1585 vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size);
1586 ni->initialized_size = sle64_to_cpu(
1587 ctx->attr->data.non_resident.initialized_size);
1588 ni->allocated_size = sle64_to_cpu(
1589 ctx->attr->data.non_resident.allocated_size);
1591 * We are done with the mft record, so we release it. Otherwise
1592 * we would deadlock in ntfs_attr_iget().
1594 ntfs_attr_put_search_ctx(ctx);
1595 unmap_mft_record(base_ni);
1596 m = NULL;
1597 ctx = NULL;
1598 /* Get the index bitmap attribute inode. */
1599 bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
1600 if (IS_ERR(bvi)) {
1601 ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
1602 err = PTR_ERR(bvi);
1603 goto unm_err_out;
1605 bni = NTFS_I(bvi);
1606 if (NInoCompressed(bni) || NInoEncrypted(bni) ||
1607 NInoSparse(bni)) {
1608 ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
1609 "and/or encrypted and/or sparse.");
1610 goto iput_unm_err_out;
1612 /* Consistency check bitmap size vs. index allocation size. */
1613 if ((bvi->i_size << 3) < (vi->i_size >>
1614 ni->itype.index.block_size_bits)) {
1615 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
1616 "for index allocation (0x%llx).",
1617 bvi->i_size << 3, vi->i_size);
1618 goto iput_unm_err_out;
1620 ni->itype.index.bmp_ino = bvi;
1621 skip_large_index_stuff:
1622 /* Setup the operations for this index inode. */
1623 vi->i_op = NULL;
1624 vi->i_fop = NULL;
1625 vi->i_mapping->a_ops = &ntfs_mst_aops;
1626 vi->i_blocks = ni->allocated_size >> 9;
1629 * Make sure the base inode doesn't go away and attach it to the
1630 * index inode.
1632 igrab(base_vi);
1633 ni->ext.base_ntfs_ino = base_ni;
1634 ni->nr_extents = -1;
1636 ntfs_debug("Done.");
1637 return 0;
1639 iput_unm_err_out:
1640 iput(bvi);
1641 unm_err_out:
1642 if (!err)
1643 err = -EIO;
1644 if (ctx)
1645 ntfs_attr_put_search_ctx(ctx);
1646 if (m)
1647 unmap_mft_record(base_ni);
1648 err_out:
1649 ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
1650 "inode (mft_no 0x%lx, name_len %i.", -err, vi->i_ino,
1651 ni->name_len);
1652 make_bad_inode(vi);
1653 return err;
1657 * ntfs_read_inode_mount - special read_inode for mount time use only
1658 * @vi: inode to read
1660 * Read inode FILE_MFT at mount time, only called with super_block lock
1661 * held from within the read_super() code path.
1663 * This function exists because when it is called the page cache for $MFT/$DATA
1664 * is not initialized and hence we cannot get at the contents of mft records
1665 * by calling map_mft_record*().
1667 * Further it needs to cope with the circular references problem, i.e. cannot
1668 * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
1669 * we do not know where the other extent mft records are yet and again, because
1670 * we cannot call map_mft_record*() yet. Obviously this applies only when an
1671 * attribute list is actually present in $MFT inode.
1673 * We solve these problems by starting with the $DATA attribute before anything
1674 * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each
1675 * extent is found, we decompress_mapping_pairs() including the implied
1676 * ntfs_merge_runlists(). Each step of the iteration necessarily provides
1677 * sufficient information for the next step to complete.
1679 * This should work but there are two possible pit falls (see inline comments
1680 * below), but only time will tell if they are real pits or just smoke...
1682 int ntfs_read_inode_mount(struct inode *vi)
1684 VCN next_vcn, last_vcn, highest_vcn;
1685 s64 block;
1686 struct super_block *sb = vi->i_sb;
1687 ntfs_volume *vol = NTFS_SB(sb);
1688 struct buffer_head *bh;
1689 ntfs_inode *ni;
1690 MFT_RECORD *m = NULL;
1691 ATTR_RECORD *attr;
1692 ntfs_attr_search_ctx *ctx;
1693 unsigned int i, nr_blocks;
1694 int err;
1696 ntfs_debug("Entering.");
1698 /* Initialize the ntfs specific part of @vi. */
1699 ntfs_init_big_inode(vi);
1701 ni = NTFS_I(vi);
1703 /* Setup the data attribute. It is special as it is mst protected. */
1704 NInoSetNonResident(ni);
1705 NInoSetMstProtected(ni);
1706 ni->type = AT_DATA;
1707 ni->name = NULL;
1708 ni->name_len = 0;
1711 * This sets up our little cheat allowing us to reuse the async read io
1712 * completion handler for directories.
1714 ni->itype.index.block_size = vol->mft_record_size;
1715 ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1717 /* Very important! Needed to be able to call map_mft_record*(). */
1718 vol->mft_ino = vi;
1720 /* Allocate enough memory to read the first mft record. */
1721 if (vol->mft_record_size > 64 * 1024) {
1722 ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
1723 vol->mft_record_size);
1724 goto err_out;
1726 i = vol->mft_record_size;
1727 if (i < sb->s_blocksize)
1728 i = sb->s_blocksize;
1729 m = (MFT_RECORD*)ntfs_malloc_nofs(i);
1730 if (!m) {
1731 ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
1732 goto err_out;
1735 /* Determine the first block of the $MFT/$DATA attribute. */
1736 block = vol->mft_lcn << vol->cluster_size_bits >>
1737 sb->s_blocksize_bits;
1738 nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
1739 if (!nr_blocks)
1740 nr_blocks = 1;
1742 /* Load $MFT/$DATA's first mft record. */
1743 for (i = 0; i < nr_blocks; i++) {
1744 bh = sb_bread(sb, block++);
1745 if (!bh) {
1746 ntfs_error(sb, "Device read failed.");
1747 goto err_out;
1749 memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
1750 sb->s_blocksize);
1751 brelse(bh);
1754 /* Apply the mst fixups. */
1755 if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
1756 /* FIXME: Try to use the $MFTMirr now. */
1757 ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
1758 goto err_out;
1761 /* Need this to sanity check attribute list references to $MFT. */
1762 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
1764 /* Provides readpage() and sync_page() for map_mft_record(). */
1765 vi->i_mapping->a_ops = &ntfs_mft_aops;
1767 ctx = ntfs_attr_get_search_ctx(ni, m);
1768 if (!ctx) {
1769 err = -ENOMEM;
1770 goto err_out;
1773 /* Find the attribute list attribute if present. */
1774 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
1775 if (err) {
1776 if (unlikely(err != -ENOENT)) {
1777 ntfs_error(sb, "Failed to lookup attribute list "
1778 "attribute. You should run chkdsk.");
1779 goto put_err_out;
1781 } else /* if (!err) */ {
1782 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
1783 u8 *al_end;
1785 ntfs_debug("Attribute list attribute found in $MFT.");
1786 NInoSetAttrList(ni);
1787 if (ctx->attr->flags & ATTR_IS_ENCRYPTED ||
1788 ctx->attr->flags & ATTR_COMPRESSION_MASK ||
1789 ctx->attr->flags & ATTR_IS_SPARSE) {
1790 ntfs_error(sb, "Attribute list attribute is "
1791 "compressed/encrypted/sparse. Not "
1792 "allowed. $MFT is corrupt. You should "
1793 "run chkdsk.");
1794 goto put_err_out;
1796 /* Now allocate memory for the attribute list. */
1797 ni->attr_list_size = (u32)ntfs_attr_size(ctx->attr);
1798 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
1799 if (!ni->attr_list) {
1800 ntfs_error(sb, "Not enough memory to allocate buffer "
1801 "for attribute list.");
1802 goto put_err_out;
1804 if (ctx->attr->non_resident) {
1805 NInoSetAttrListNonResident(ni);
1806 if (ctx->attr->data.non_resident.lowest_vcn) {
1807 ntfs_error(sb, "Attribute list has non zero "
1808 "lowest_vcn. $MFT is corrupt. "
1809 "You should run chkdsk.");
1810 goto put_err_out;
1812 /* Setup the runlist. */
1813 ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
1814 ctx->attr, NULL);
1815 if (IS_ERR(ni->attr_list_rl.rl)) {
1816 err = PTR_ERR(ni->attr_list_rl.rl);
1817 ni->attr_list_rl.rl = NULL;
1818 ntfs_error(sb, "Mapping pairs decompression "
1819 "failed with error code %i.",
1820 -err);
1821 goto put_err_out;
1823 /* Now load the attribute list. */
1824 if ((err = load_attribute_list(vol, &ni->attr_list_rl,
1825 ni->attr_list, ni->attr_list_size,
1826 sle64_to_cpu(ctx->attr->data.
1827 non_resident.initialized_size)))) {
1828 ntfs_error(sb, "Failed to load attribute list "
1829 "attribute with error code %i.",
1830 -err);
1831 goto put_err_out;
1833 } else /* if (!ctx.attr->non_resident) */ {
1834 if ((u8*)ctx->attr + le16_to_cpu(
1835 ctx->attr->data.resident.value_offset) +
1836 le32_to_cpu(
1837 ctx->attr->data.resident.value_length) >
1838 (u8*)ctx->mrec + vol->mft_record_size) {
1839 ntfs_error(sb, "Corrupt attribute list "
1840 "attribute.");
1841 goto put_err_out;
1843 /* Now copy the attribute list. */
1844 memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu(
1845 ctx->attr->data.resident.value_offset),
1846 le32_to_cpu(
1847 ctx->attr->data.resident.value_length));
1849 /* The attribute list is now setup in memory. */
1851 * FIXME: I don't know if this case is actually possible.
1852 * According to logic it is not possible but I have seen too
1853 * many weird things in MS software to rely on logic... Thus we
1854 * perform a manual search and make sure the first $MFT/$DATA
1855 * extent is in the base inode. If it is not we abort with an
1856 * error and if we ever see a report of this error we will need
1857 * to do some magic in order to have the necessary mft record
1858 * loaded and in the right place in the page cache. But
1859 * hopefully logic will prevail and this never happens...
1861 al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
1862 al_end = (u8*)al_entry + ni->attr_list_size;
1863 for (;; al_entry = next_al_entry) {
1864 /* Out of bounds check. */
1865 if ((u8*)al_entry < ni->attr_list ||
1866 (u8*)al_entry > al_end)
1867 goto em_put_err_out;
1868 /* Catch the end of the attribute list. */
1869 if ((u8*)al_entry == al_end)
1870 goto em_put_err_out;
1871 if (!al_entry->length)
1872 goto em_put_err_out;
1873 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
1874 le16_to_cpu(al_entry->length) > al_end)
1875 goto em_put_err_out;
1876 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
1877 le16_to_cpu(al_entry->length));
1878 if (le32_to_cpu(al_entry->type) >
1879 const_le32_to_cpu(AT_DATA))
1880 goto em_put_err_out;
1881 if (AT_DATA != al_entry->type)
1882 continue;
1883 /* We want an unnamed attribute. */
1884 if (al_entry->name_length)
1885 goto em_put_err_out;
1886 /* Want the first entry, i.e. lowest_vcn == 0. */
1887 if (al_entry->lowest_vcn)
1888 goto em_put_err_out;
1889 /* First entry has to be in the base mft record. */
1890 if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
1891 /* MFT references do not match, logic fails. */
1892 ntfs_error(sb, "BUG: The first $DATA extent "
1893 "of $MFT is not in the base "
1894 "mft record. Please report "
1895 "you saw this message to "
1896 "linux-ntfs-dev@lists."
1897 "sourceforge.net");
1898 goto put_err_out;
1899 } else {
1900 /* Sequence numbers must match. */
1901 if (MSEQNO_LE(al_entry->mft_reference) !=
1902 ni->seq_no)
1903 goto em_put_err_out;
1904 /* Got it. All is ok. We can stop now. */
1905 break;
1910 ntfs_attr_reinit_search_ctx(ctx);
1912 /* Now load all attribute extents. */
1913 attr = NULL;
1914 next_vcn = last_vcn = highest_vcn = 0;
1915 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
1916 ctx))) {
1917 runlist_element *nrl;
1919 /* Cache the current attribute. */
1920 attr = ctx->attr;
1921 /* $MFT must be non-resident. */
1922 if (!attr->non_resident) {
1923 ntfs_error(sb, "$MFT must be non-resident but a "
1924 "resident extent was found. $MFT is "
1925 "corrupt. Run chkdsk.");
1926 goto put_err_out;
1928 /* $MFT must be uncompressed and unencrypted. */
1929 if (attr->flags & ATTR_COMPRESSION_MASK ||
1930 attr->flags & ATTR_IS_ENCRYPTED ||
1931 attr->flags & ATTR_IS_SPARSE) {
1932 ntfs_error(sb, "$MFT must be uncompressed, "
1933 "non-sparse, and unencrypted but a "
1934 "compressed/sparse/encrypted extent "
1935 "was found. $MFT is corrupt. Run "
1936 "chkdsk.");
1937 goto put_err_out;
1940 * Decompress the mapping pairs array of this extent and merge
1941 * the result into the existing runlist. No need for locking
1942 * as we have exclusive access to the inode at this time and we
1943 * are a mount in progress task, too.
1945 nrl = decompress_mapping_pairs(vol, attr, ni->runlist.rl);
1946 if (IS_ERR(nrl)) {
1947 ntfs_error(sb, "decompress_mapping_pairs() failed with "
1948 "error code %ld. $MFT is corrupt.",
1949 PTR_ERR(nrl));
1950 goto put_err_out;
1952 ni->runlist.rl = nrl;
1954 /* Are we in the first extent? */
1955 if (!next_vcn) {
1956 if (attr->data.non_resident.lowest_vcn) {
1957 ntfs_error(sb, "First extent of $DATA "
1958 "attribute has non zero "
1959 "lowest_vcn. $MFT is corrupt. "
1960 "You should run chkdsk.");
1961 goto put_err_out;
1963 /* Get the last vcn in the $DATA attribute. */
1964 last_vcn = sle64_to_cpu(
1965 attr->data.non_resident.allocated_size)
1966 >> vol->cluster_size_bits;
1967 /* Fill in the inode size. */
1968 vi->i_size = sle64_to_cpu(
1969 attr->data.non_resident.data_size);
1970 ni->initialized_size = sle64_to_cpu(attr->data.
1971 non_resident.initialized_size);
1972 ni->allocated_size = sle64_to_cpu(
1973 attr->data.non_resident.allocated_size);
1975 * Verify the number of mft records does not exceed
1976 * 2^32 - 1.
1978 if ((vi->i_size >> vol->mft_record_size_bits) >=
1979 (1ULL << 32)) {
1980 ntfs_error(sb, "$MFT is too big! Aborting.");
1981 goto put_err_out;
1984 * We have got the first extent of the runlist for
1985 * $MFT which means it is now relatively safe to call
1986 * the normal ntfs_read_inode() function.
1987 * Complete reading the inode, this will actually
1988 * re-read the mft record for $MFT, this time entering
1989 * it into the page cache with which we complete the
1990 * kick start of the volume. It should be safe to do
1991 * this now as the first extent of $MFT/$DATA is
1992 * already known and we would hope that we don't need
1993 * further extents in order to find the other
1994 * attributes belonging to $MFT. Only time will tell if
1995 * this is really the case. If not we will have to play
1996 * magic at this point, possibly duplicating a lot of
1997 * ntfs_read_inode() at this point. We will need to
1998 * ensure we do enough of its work to be able to call
1999 * ntfs_read_inode() on extents of $MFT/$DATA. But lets
2000 * hope this never happens...
2002 ntfs_read_locked_inode(vi);
2003 if (is_bad_inode(vi)) {
2004 ntfs_error(sb, "ntfs_read_inode() of $MFT "
2005 "failed. BUG or corrupt $MFT. "
2006 "Run chkdsk and if no errors "
2007 "are found, please report you "
2008 "saw this message to "
2009 "linux-ntfs-dev@lists."
2010 "sourceforge.net");
2011 ntfs_attr_put_search_ctx(ctx);
2012 /* Revert to the safe super operations. */
2013 ntfs_free(m);
2014 return -1;
2017 * Re-initialize some specifics about $MFT's inode as
2018 * ntfs_read_inode() will have set up the default ones.
2020 /* Set uid and gid to root. */
2021 vi->i_uid = vi->i_gid = 0;
2022 /* Regular file. No access for anyone. */
2023 vi->i_mode = S_IFREG;
2024 /* No VFS initiated operations allowed for $MFT. */
2025 vi->i_op = &ntfs_empty_inode_ops;
2026 vi->i_fop = &ntfs_empty_file_ops;
2027 /* Put back our special address space operations. */
2028 vi->i_mapping->a_ops = &ntfs_mft_aops;
2031 /* Get the lowest vcn for the next extent. */
2032 highest_vcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
2033 next_vcn = highest_vcn + 1;
2035 /* Only one extent or error, which we catch below. */
2036 if (next_vcn <= 0)
2037 break;
2039 /* Avoid endless loops due to corruption. */
2040 if (next_vcn < sle64_to_cpu(
2041 attr->data.non_resident.lowest_vcn)) {
2042 ntfs_error(sb, "$MFT has corrupt attribute list "
2043 "attribute. Run chkdsk.");
2044 goto put_err_out;
2047 if (err != -ENOENT) {
2048 ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. "
2049 "$MFT is corrupt. Run chkdsk.");
2050 goto put_err_out;
2052 if (!attr) {
2053 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
2054 "corrupt. Run chkdsk.");
2055 goto put_err_out;
2057 if (highest_vcn && highest_vcn != last_vcn - 1) {
2058 ntfs_error(sb, "Failed to load the complete runlist for "
2059 "$MFT/$DATA. Driver bug or corrupt $MFT. "
2060 "Run chkdsk.");
2061 ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
2062 (unsigned long long)highest_vcn,
2063 (unsigned long long)last_vcn - 1);
2064 goto put_err_out;
2066 ntfs_attr_put_search_ctx(ctx);
2067 ntfs_debug("Done.");
2068 ntfs_free(m);
2069 return 0;
2071 em_put_err_out:
2072 ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
2073 "attribute list. $MFT is corrupt. Run chkdsk.");
2074 put_err_out:
2075 ntfs_attr_put_search_ctx(ctx);
2076 err_out:
2077 ntfs_error(sb, "Failed. Marking inode as bad.");
2078 make_bad_inode(vi);
2079 ntfs_free(m);
2080 return -1;
2084 * ntfs_put_inode - handler for when the inode reference count is decremented
2085 * @vi: vfs inode
2087 * The VFS calls ntfs_put_inode() every time the inode reference count (i_count)
2088 * is about to be decremented (but before the decrement itself.
2090 * If the inode @vi is a directory with two references, one of which is being
2091 * dropped, we need to put the attribute inode for the directory index bitmap,
2092 * if it is present, otherwise the directory inode would remain pinned for
2093 * ever.
2095 * If the inode @vi is an index inode with only one reference which is being
2096 * dropped, we need to put the attribute inode for the index bitmap, if it is
2097 * present, otherwise the index inode would disappear and the attribute inode
2098 * for the index bitmap would no longer be referenced from anywhere and thus it
2099 * would remain pinned for ever.
2101 void ntfs_put_inode(struct inode *vi)
2103 ntfs_inode *ni;
2105 if (S_ISDIR(vi->i_mode)) {
2106 if (atomic_read(&vi->i_count) == 2) {
2107 ni = NTFS_I(vi);
2108 if (NInoIndexAllocPresent(ni) &&
2109 ni->itype.index.bmp_ino) {
2110 iput(ni->itype.index.bmp_ino);
2111 ni->itype.index.bmp_ino = NULL;
2114 return;
2116 if (atomic_read(&vi->i_count) != 1)
2117 return;
2118 ni = NTFS_I(vi);
2119 if (NInoAttr(ni) && (ni->type == AT_INDEX_ALLOCATION) &&
2120 NInoIndexAllocPresent(ni) && ni->itype.index.bmp_ino) {
2121 iput(ni->itype.index.bmp_ino);
2122 ni->itype.index.bmp_ino = NULL;
2124 return;
2127 void __ntfs_clear_inode(ntfs_inode *ni)
2129 /* Free all alocated memory. */
2130 down_write(&ni->runlist.lock);
2131 if (ni->runlist.rl) {
2132 ntfs_free(ni->runlist.rl);
2133 ni->runlist.rl = NULL;
2135 up_write(&ni->runlist.lock);
2137 if (ni->attr_list) {
2138 ntfs_free(ni->attr_list);
2139 ni->attr_list = NULL;
2142 down_write(&ni->attr_list_rl.lock);
2143 if (ni->attr_list_rl.rl) {
2144 ntfs_free(ni->attr_list_rl.rl);
2145 ni->attr_list_rl.rl = NULL;
2147 up_write(&ni->attr_list_rl.lock);
2149 if (ni->name_len && ni->name != I30) {
2150 /* Catch bugs... */
2151 BUG_ON(!ni->name);
2152 kfree(ni->name);
2156 void ntfs_clear_extent_inode(ntfs_inode *ni)
2158 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
2160 BUG_ON(NInoAttr(ni));
2161 BUG_ON(ni->nr_extents != -1);
2163 #ifdef NTFS_RW
2164 if (NInoDirty(ni)) {
2165 if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
2166 ntfs_error(ni->vol->sb, "Clearing dirty extent inode! "
2167 "Losing data! This is a BUG!!!");
2168 // FIXME: Do something!!!
2170 #endif /* NTFS_RW */
2172 __ntfs_clear_inode(ni);
2174 /* Bye, bye... */
2175 ntfs_destroy_extent_inode(ni);
2179 * ntfs_clear_big_inode - clean up the ntfs specific part of an inode
2180 * @vi: vfs inode pending annihilation
2182 * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
2183 * is called, which deallocates all memory belonging to the NTFS specific part
2184 * of the inode and returns.
2186 * If the MFT record is dirty, we commit it before doing anything else.
2188 void ntfs_clear_big_inode(struct inode *vi)
2190 ntfs_inode *ni = NTFS_I(vi);
2192 #ifdef NTFS_RW
2193 if (NInoDirty(ni)) {
2194 BOOL was_bad = (is_bad_inode(vi));
2196 /* Committing the inode also commits all extent inodes. */
2197 ntfs_commit_inode(vi);
2199 if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
2200 ntfs_error(vi->i_sb, "Failed to commit dirty inode "
2201 "0x%lx. Losing data!", vi->i_ino);
2202 // FIXME: Do something!!!
2205 #endif /* NTFS_RW */
2207 /* No need to lock at this stage as no one else has a reference. */
2208 if (ni->nr_extents > 0) {
2209 int i;
2211 for (i = 0; i < ni->nr_extents; i++)
2212 ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
2213 kfree(ni->ext.extent_ntfs_inos);
2216 __ntfs_clear_inode(ni);
2218 if (NInoAttr(ni)) {
2219 /* Release the base inode if we are holding it. */
2220 if (ni->nr_extents == -1) {
2221 iput(VFS_I(ni->ext.base_ntfs_ino));
2222 ni->nr_extents = 0;
2223 ni->ext.base_ntfs_ino = NULL;
2226 return;
2230 * ntfs_show_options - show mount options in /proc/mounts
2231 * @sf: seq_file in which to write our mount options
2232 * @mnt: vfs mount whose mount options to display
2234 * Called by the VFS once for each mounted ntfs volume when someone reads
2235 * /proc/mounts in order to display the NTFS specific mount options of each
2236 * mount. The mount options of the vfs mount @mnt are written to the seq file
2237 * @sf and success is returned.
2239 int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
2241 ntfs_volume *vol = NTFS_SB(mnt->mnt_sb);
2242 int i;
2244 seq_printf(sf, ",uid=%i", vol->uid);
2245 seq_printf(sf, ",gid=%i", vol->gid);
2246 if (vol->fmask == vol->dmask)
2247 seq_printf(sf, ",umask=0%o", vol->fmask);
2248 else {
2249 seq_printf(sf, ",fmask=0%o", vol->fmask);
2250 seq_printf(sf, ",dmask=0%o", vol->dmask);
2252 seq_printf(sf, ",nls=%s", vol->nls_map->charset);
2253 if (NVolCaseSensitive(vol))
2254 seq_printf(sf, ",case_sensitive");
2255 if (NVolShowSystemFiles(vol))
2256 seq_printf(sf, ",show_sys_files");
2257 for (i = 0; on_errors_arr[i].val; i++) {
2258 if (on_errors_arr[i].val & vol->on_errors)
2259 seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
2261 seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
2262 return 0;
2265 #ifdef NTFS_RW
2268 * ntfs_truncate - called when the i_size of an ntfs inode is changed
2269 * @vi: inode for which the i_size was changed
2271 * We don't support i_size changes yet.
2273 * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
2274 * that the change is allowed.
2276 * This implies for us that @vi is a file inode rather than a directory, index,
2277 * or attribute inode as well as that @vi is a base inode.
2279 * Called with ->i_sem held. In all but one case ->i_alloc_sem is held for
2280 * writing. The only case where ->i_alloc_sem is not held is
2281 * mm/filemap.c::generic_file_buffered_write() where vmtruncate() is called
2282 * with the current i_size as the offset which means that it is a noop as far
2283 * as ntfs_truncate() is concerned.
2285 void ntfs_truncate(struct inode *vi)
2287 ntfs_inode *ni = NTFS_I(vi);
2288 ntfs_attr_search_ctx *ctx;
2289 MFT_RECORD *m;
2290 int err;
2292 m = map_mft_record(ni);
2293 if (IS_ERR(m)) {
2294 ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
2295 "(error code %ld).", vi->i_ino, PTR_ERR(m));
2296 if (PTR_ERR(m) != ENOMEM)
2297 make_bad_inode(vi);
2298 return;
2300 ctx = ntfs_attr_get_search_ctx(ni, m);
2301 if (unlikely(!ctx)) {
2302 ntfs_error(vi->i_sb, "Failed to allocate a search context: "
2303 "Not enough memory");
2304 // FIXME: We can't report an error code upstream. So what do
2305 // we do?!? make_bad_inode() seems a bit harsh...
2306 unmap_mft_record(ni);
2307 return;
2309 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2310 CASE_SENSITIVE, 0, NULL, 0, ctx);
2311 if (unlikely(err)) {
2312 if (err == -ENOENT) {
2313 ntfs_error(vi->i_sb, "Open attribute is missing from "
2314 "mft record. Inode 0x%lx is corrupt. "
2315 "Run chkdsk.", vi->i_ino);
2316 make_bad_inode(vi);
2317 } else {
2318 ntfs_error(vi->i_sb, "Failed to lookup attribute in "
2319 "inode 0x%lx (error code %d).",
2320 vi->i_ino, err);
2321 // FIXME: We can't report an error code upstream. So
2322 // what do we do?!? make_bad_inode() seems a bit
2323 // harsh...
2325 goto out;
2327 /* If the size has not changed there is nothing to do. */
2328 if (ntfs_attr_size(ctx->attr) == i_size_read(vi))
2329 goto out;
2330 // TODO: Implement the truncate...
2331 ntfs_error(vi->i_sb, "Inode size has changed but this is not "
2332 "implemented yet. Resetting inode size to old value. "
2333 " This is most likely a bug in the ntfs driver!");
2334 i_size_write(vi, ntfs_attr_size(ctx->attr));
2335 out:
2336 ntfs_attr_put_search_ctx(ctx);
2337 unmap_mft_record(ni);
2338 return;
2342 * ntfs_setattr - called from notify_change() when an attribute is being changed
2343 * @dentry: dentry whose attributes to change
2344 * @attr: structure describing the attributes and the changes
2346 * We have to trap VFS attempts to truncate the file described by @dentry as
2347 * soon as possible, because we do not implement changes in i_size yet. So we
2348 * abort all i_size changes here.
2350 * We also abort all changes of user, group, and mode as we do not implement
2351 * the NTFS ACLs yet.
2353 * Called with ->i_sem held. For the ATTR_SIZE (i.e. ->truncate) case, also
2354 * called with ->i_alloc_sem held for writing.
2356 * Basically this is a copy of generic notify_change() and inode_setattr()
2357 * functionality, except we intercept and abort changes in i_size.
2359 int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
2361 struct inode *vi = dentry->d_inode;
2362 int err;
2363 unsigned int ia_valid = attr->ia_valid;
2365 err = inode_change_ok(vi, attr);
2366 if (err)
2367 return err;
2369 /* We do not support NTFS ACLs yet. */
2370 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
2371 ntfs_warning(vi->i_sb, "Changes in user/group/mode are not "
2372 "supported yet, ignoring.");
2373 err = -EOPNOTSUPP;
2374 goto out;
2377 if (ia_valid & ATTR_SIZE) {
2378 if (attr->ia_size != i_size_read(vi)) {
2379 ntfs_warning(vi->i_sb, "Changes in inode size are not "
2380 "supported yet, ignoring.");
2381 err = -EOPNOTSUPP;
2382 // TODO: Implement...
2383 // err = vmtruncate(vi, attr->ia_size);
2384 if (err || ia_valid == ATTR_SIZE)
2385 goto out;
2386 } else {
2388 * We skipped the truncate but must still update
2389 * timestamps.
2391 ia_valid |= ATTR_MTIME|ATTR_CTIME;
2395 if (ia_valid & ATTR_ATIME)
2396 vi->i_atime = attr->ia_atime;
2397 if (ia_valid & ATTR_MTIME)
2398 vi->i_mtime = attr->ia_mtime;
2399 if (ia_valid & ATTR_CTIME)
2400 vi->i_ctime = attr->ia_ctime;
2401 mark_inode_dirty(vi);
2402 out:
2403 return err;
2407 * ntfs_write_inode - write out a dirty inode
2408 * @vi: inode to write out
2409 * @sync: if true, write out synchronously
2411 * Write out a dirty inode to disk including any extent inodes if present.
2413 * If @sync is true, commit the inode to disk and wait for io completion. This
2414 * is done using write_mft_record().
2416 * If @sync is false, just schedule the write to happen but do not wait for i/o
2417 * completion. In 2.6 kernels, scheduling usually happens just by virtue of
2418 * marking the page (and in this case mft record) dirty but we do not implement
2419 * this yet as write_mft_record() largely ignores the @sync parameter and
2420 * always performs synchronous writes.
2422 * Return 0 on success and -errno on error.
2424 int ntfs_write_inode(struct inode *vi, int sync)
2426 sle64 nt;
2427 ntfs_inode *ni = NTFS_I(vi);
2428 ntfs_attr_search_ctx *ctx;
2429 MFT_RECORD *m;
2430 STANDARD_INFORMATION *si;
2431 int err = 0;
2432 BOOL modified = FALSE;
2434 ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
2435 vi->i_ino);
2437 * Dirty attribute inodes are written via their real inodes so just
2438 * clean them here. Access time updates are taken care off when the
2439 * real inode is written.
2441 if (NInoAttr(ni)) {
2442 NInoClearDirty(ni);
2443 ntfs_debug("Done.");
2444 return 0;
2446 /* Map, pin, and lock the mft record belonging to the inode. */
2447 m = map_mft_record(ni);
2448 if (IS_ERR(m)) {
2449 err = PTR_ERR(m);
2450 goto err_out;
2452 /* Update the access times in the standard information attribute. */
2453 ctx = ntfs_attr_get_search_ctx(ni, m);
2454 if (unlikely(!ctx)) {
2455 err = -ENOMEM;
2456 goto unm_err_out;
2458 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0,
2459 CASE_SENSITIVE, 0, NULL, 0, ctx);
2460 if (unlikely(err)) {
2461 ntfs_attr_put_search_ctx(ctx);
2462 goto unm_err_out;
2464 si = (STANDARD_INFORMATION*)((u8*)ctx->attr +
2465 le16_to_cpu(ctx->attr->data.resident.value_offset));
2466 /* Update the access times if they have changed. */
2467 nt = utc2ntfs(vi->i_mtime);
2468 if (si->last_data_change_time != nt) {
2469 ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
2470 "new = 0x%llx", vi->i_ino,
2471 sle64_to_cpu(si->last_data_change_time),
2472 sle64_to_cpu(nt));
2473 si->last_data_change_time = nt;
2474 modified = TRUE;
2476 nt = utc2ntfs(vi->i_ctime);
2477 if (si->last_mft_change_time != nt) {
2478 ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
2479 "new = 0x%llx", vi->i_ino,
2480 sle64_to_cpu(si->last_mft_change_time),
2481 sle64_to_cpu(nt));
2482 si->last_mft_change_time = nt;
2483 modified = TRUE;
2485 nt = utc2ntfs(vi->i_atime);
2486 if (si->last_access_time != nt) {
2487 ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
2488 "new = 0x%llx", vi->i_ino,
2489 sle64_to_cpu(si->last_access_time),
2490 sle64_to_cpu(nt));
2491 si->last_access_time = nt;
2492 modified = TRUE;
2495 * If we just modified the standard information attribute we need to
2496 * mark the mft record it is in dirty. We do this manually so that
2497 * mark_inode_dirty() is not called which would redirty the inode and
2498 * hence result in an infinite loop of trying to write the inode.
2499 * There is no need to mark the base inode nor the base mft record
2500 * dirty, since we are going to write this mft record below in any case
2501 * and the base mft record may actually not have been modified so it
2502 * might not need to be written out.
2504 if (modified && !NInoTestSetDirty(ctx->ntfs_ino))
2505 __set_page_dirty_nobuffers(ctx->ntfs_ino->page);
2506 ntfs_attr_put_search_ctx(ctx);
2507 /* Now the access times are updated, write the base mft record. */
2508 if (NInoDirty(ni))
2509 err = write_mft_record(ni, m, sync);
2510 /* Write all attached extent mft records. */
2511 down(&ni->extent_lock);
2512 if (ni->nr_extents > 0) {
2513 ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
2514 int i;
2516 ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
2517 for (i = 0; i < ni->nr_extents; i++) {
2518 ntfs_inode *tni = extent_nis[i];
2520 if (NInoDirty(tni)) {
2521 MFT_RECORD *tm = map_mft_record(tni);
2522 int ret;
2524 if (IS_ERR(tm)) {
2525 if (!err || err == -ENOMEM)
2526 err = PTR_ERR(tm);
2527 continue;
2529 ret = write_mft_record(tni, tm, sync);
2530 unmap_mft_record(tni);
2531 if (unlikely(ret)) {
2532 if (!err || err == -ENOMEM)
2533 err = ret;
2538 up(&ni->extent_lock);
2539 unmap_mft_record(ni);
2540 if (unlikely(err))
2541 goto err_out;
2542 ntfs_debug("Done.");
2543 return 0;
2544 unm_err_out:
2545 unmap_mft_record(ni);
2546 err_out:
2547 if (err == -ENOMEM) {
2548 ntfs_warning(vi->i_sb, "Not enough memory to write inode. "
2549 "Marking the inode dirty again, so the VFS "
2550 "retries later.");
2551 mark_inode_dirty(vi);
2552 } else {
2553 ntfs_error(vi->i_sb, "Failed (error code %i): Marking inode "
2554 "as bad. You should run chkdsk.", -err);
2555 make_bad_inode(vi);
2557 return err;
2560 #endif /* NTFS_RW */