VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / ntfs / inode.c
1 /**
2  * inode.c - NTFS kernel inode handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2004 Anton Altaparmakov
5  *
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.
10  *
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.
15  *
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
20  */
21
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>
27
28 #include "ntfs.h"
29 #include "dir.h"
30 #include "inode.h"
31 #include "attrib.h"
32 #include "time.h"
33
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
38  *
39  * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
40  * inode @vi for equality with the ntfs attribute @na.
41  *
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.
44  *
45  * Return 1 if the attributes match and 0 if not.
46  *
47  * NOTE: This function runs with the inode_lock spin lock held so it is not
48  * allowed to sleep.
49  */
50 int ntfs_test_inode(struct inode *vi, ntfs_attr *na)
51 {
52         ntfs_inode *ni;
53
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;
71         }
72         /* Match! */
73         return 1;
74 }
75
76 /**
77  * ntfs_init_locked_inode - initialize an inode
78  * @vi:         vfs inode to initialize
79  * @na:         ntfs attribute which to initialize @vi to
80  *
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.
83  *
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.
88  *
89  * Return 0 on success and -errno on error.
90  *
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.)
93  */
94 static int ntfs_init_locked_inode(struct inode *vi, ntfs_attr *na)
95 {
96         ntfs_inode *ni = NTFS_I(vi);
97
98         vi->i_ino = na->mft_no;
99
100         ni->type = na->type;
101         if (na->type == AT_INDEX_ALLOCATION)
102                 NInoSetMstProtected(ni);
103
104         ni->name = na->name;
105         ni->name_len = na->name_len;
106
107         /* If initializing a normal inode, we are done. */
108         if (likely(na->type == AT_UNUSED))
109                 return 0;
110
111         /* It is a fake inode. */
112         NInoSetAttr(ni);
113
114         /*
115          * We have I30 global constant as an optimization as it is the name
116          * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
117          * allocation but that is ok. And most attributes are unnamed anyway,
118          * thus the fraction of named attributes with name != I30 is actually
119          * absolutely tiny.
120          */
121         if (na->name && na->name_len && na->name != I30) {
122                 unsigned int i;
123
124                 i = na->name_len * sizeof(ntfschar);
125                 ni->name = (ntfschar*)kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
126                 if (!ni->name)
127                         return -ENOMEM;
128                 memcpy(ni->name, na->name, i);
129                 ni->name[i] = cpu_to_le16('\0');
130         }
131         return 0;
132 }
133
134 typedef int (*set_t)(struct inode *, void *);
135 static int ntfs_read_locked_inode(struct inode *vi);
136 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
137 static int ntfs_read_locked_index_inode(struct inode *base_vi,
138                 struct inode *vi);
139
140 /**
141  * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
142  * @sb:         super block of mounted volume
143  * @mft_no:     mft record number / inode number to obtain
144  *
145  * Obtain the struct inode corresponding to a specific normal inode (i.e. a
146  * file or directory).
147  *
148  * If the inode is in the cache, it is just returned with an increased
149  * reference count. Otherwise, a new struct inode is allocated and initialized,
150  * and finally ntfs_read_locked_inode() is called to read in the inode and
151  * fill in the remainder of the inode structure.
152  *
153  * Return the struct inode on success. Check the return value with IS_ERR() and
154  * if true, the function failed and the error code is obtained from PTR_ERR().
155  */
156 struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
157 {
158         struct inode *vi;
159         ntfs_attr na;
160         int err;
161
162         na.mft_no = mft_no;
163         na.type = AT_UNUSED;
164         na.name = NULL;
165         na.name_len = 0;
166
167         vi = iget5_locked(sb, mft_no, (test_t)ntfs_test_inode,
168                         (set_t)ntfs_init_locked_inode, &na);
169         if (!vi)
170                 return ERR_PTR(-ENOMEM);
171
172         err = 0;
173
174         /* If this is a freshly allocated inode, need to read it now. */
175         if (vi->i_state & I_NEW) {
176                 err = ntfs_read_locked_inode(vi);
177                 unlock_new_inode(vi);
178         }
179         /*
180          * There is no point in keeping bad inodes around if the failure was
181          * due to ENOMEM. We want to be able to retry again later.
182          */
183         if (err == -ENOMEM) {
184                 iput(vi);
185                 vi = ERR_PTR(err);
186         }
187         return vi;
188 }
189
190 /**
191  * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
192  * @base_vi:    vfs base inode containing the attribute
193  * @type:       attribute type
194  * @name:       Unicode name of the attribute (NULL if unnamed)
195  * @name_len:   length of @name in Unicode characters (0 if unnamed)
196  *
197  * Obtain the (fake) struct inode corresponding to the attribute specified by
198  * @type, @name, and @name_len, which is present in the base mft record
199  * specified by the vfs inode @base_vi.
200  *
201  * If the attribute inode is in the cache, it is just returned with an
202  * increased reference count. Otherwise, a new struct inode is allocated and
203  * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
204  * attribute and fill in the inode structure.
205  *
206  * Note, for index allocation attributes, you need to use ntfs_index_iget()
207  * instead of ntfs_attr_iget() as working with indices is a lot more complex.
208  *
209  * Return the struct inode of the attribute inode on success. Check the return
210  * value with IS_ERR() and if true, the function failed and the error code is
211  * obtained from PTR_ERR().
212  */
213 struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPES type,
214                 ntfschar *name, u32 name_len)
215 {
216         struct inode *vi;
217         ntfs_attr na;
218         int err;
219
220         /* Make sure no one calls ntfs_attr_iget() for indices. */
221         BUG_ON(type == AT_INDEX_ALLOCATION);
222
223         na.mft_no = base_vi->i_ino;
224         na.type = type;
225         na.name = name;
226         na.name_len = name_len;
227
228         vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
229                         (set_t)ntfs_init_locked_inode, &na);
230         if (!vi)
231                 return ERR_PTR(-ENOMEM);
232
233         err = 0;
234
235         /* If this is a freshly allocated inode, need to read it now. */
236         if (vi->i_state & I_NEW) {
237                 err = ntfs_read_locked_attr_inode(base_vi, vi);
238                 unlock_new_inode(vi);
239         }
240         /*
241          * There is no point in keeping bad attribute inodes around. This also
242          * simplifies things in that we never need to check for bad attribute
243          * inodes elsewhere.
244          */
245         if (err) {
246                 iput(vi);
247                 vi = ERR_PTR(err);
248         }
249         return vi;
250 }
251
252 /**
253  * ntfs_index_iget - obtain a struct inode corresponding to an index
254  * @base_vi:    vfs base inode containing the index related attributes
255  * @name:       Unicode name of the index
256  * @name_len:   length of @name in Unicode characters
257  *
258  * Obtain the (fake) struct inode corresponding to the index specified by @name
259  * and @name_len, which is present in the base mft record specified by the vfs
260  * inode @base_vi.
261  *
262  * If the index inode is in the cache, it is just returned with an increased
263  * reference count.  Otherwise, a new struct inode is allocated and
264  * initialized, and finally ntfs_read_locked_index_inode() is called to read
265  * the index related attributes and fill in the inode structure.
266  *
267  * Return the struct inode of the index inode on success. Check the return
268  * value with IS_ERR() and if true, the function failed and the error code is
269  * obtained from PTR_ERR().
270  */
271 struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
272                 u32 name_len)
273 {
274         struct inode *vi;
275         ntfs_attr na;
276         int err;
277
278         na.mft_no = base_vi->i_ino;
279         na.type = AT_INDEX_ALLOCATION;
280         na.name = name;
281         na.name_len = name_len;
282
283         vi = iget5_locked(base_vi->i_sb, na.mft_no, (test_t)ntfs_test_inode,
284                         (set_t)ntfs_init_locked_inode, &na);
285         if (!vi)
286                 return ERR_PTR(-ENOMEM);
287
288         err = 0;
289
290         /* If this is a freshly allocated inode, need to read it now. */
291         if (vi->i_state & I_NEW) {
292                 err = ntfs_read_locked_index_inode(base_vi, vi);
293                 unlock_new_inode(vi);
294         }
295         /*
296          * There is no point in keeping bad index inodes around.  This also
297          * simplifies things in that we never need to check for bad index
298          * inodes elsewhere.
299          */
300         if (err) {
301                 iput(vi);
302                 vi = ERR_PTR(err);
303         }
304         return vi;
305 }
306
307 struct inode *ntfs_alloc_big_inode(struct super_block *sb)
308 {
309         ntfs_inode *ni;
310
311         ntfs_debug("Entering.");
312         ni = (ntfs_inode *)kmem_cache_alloc(ntfs_big_inode_cache,
313                         SLAB_NOFS);
314         if (likely(ni != NULL)) {
315                 ni->state = 0;
316                 return VFS_I(ni);
317         }
318         ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
319         return NULL;
320 }
321
322 void ntfs_destroy_big_inode(struct inode *inode)
323 {
324         ntfs_inode *ni = NTFS_I(inode);
325
326         ntfs_debug("Entering.");
327         BUG_ON(ni->page);
328         if (!atomic_dec_and_test(&ni->count))
329                 BUG();
330         kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
331 }
332
333 static inline ntfs_inode *ntfs_alloc_extent_inode(void)
334 {
335         ntfs_inode *ni;
336
337         ntfs_debug("Entering.");
338         ni = (ntfs_inode *)kmem_cache_alloc(ntfs_inode_cache, SLAB_NOFS);
339         if (likely(ni != NULL)) {
340                 ni->state = 0;
341                 return ni;
342         }
343         ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
344         return NULL;
345 }
346
347 void ntfs_destroy_extent_inode(ntfs_inode *ni)
348 {
349         ntfs_debug("Entering.");
350         BUG_ON(ni->page);
351         if (!atomic_dec_and_test(&ni->count))
352                 BUG();
353         kmem_cache_free(ntfs_inode_cache, ni);
354 }
355
356 /**
357  * __ntfs_init_inode - initialize ntfs specific part of an inode
358  * @sb:         super block of mounted volume
359  * @ni:         freshly allocated ntfs inode which to initialize
360  *
361  * Initialize an ntfs inode to defaults.
362  *
363  * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
364  * untouched. Make sure to initialize them elsewhere.
365  *
366  * Return zero on success and -ENOMEM on error.
367  */
368 static void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
369 {
370         ntfs_debug("Entering.");
371         ni->initialized_size = ni->allocated_size = 0;
372         ni->seq_no = 0;
373         atomic_set(&ni->count, 1);
374         ni->vol = NTFS_SB(sb);
375         init_run_list(&ni->run_list);
376         init_MUTEX(&ni->mrec_lock);
377         ni->page = NULL;
378         ni->page_ofs = 0;
379         ni->attr_list_size = 0;
380         ni->attr_list = NULL;
381         init_run_list(&ni->attr_list_rl);
382         ni->itype.index.bmp_ino = NULL;
383         ni->itype.index.block_size = 0;
384         ni->itype.index.vcn_size = 0;
385         ni->itype.index.collation_rule = 0;
386         ni->itype.index.block_size_bits = 0;
387         ni->itype.index.vcn_size_bits = 0;
388         init_MUTEX(&ni->extent_lock);
389         ni->nr_extents = 0;
390         ni->ext.base_ntfs_ino = NULL;
391         return;
392 }
393
394 static inline void ntfs_init_big_inode(struct inode *vi)
395 {
396         ntfs_inode *ni = NTFS_I(vi);
397
398         ntfs_debug("Entering.");
399         __ntfs_init_inode(vi->i_sb, ni);
400         ni->mft_no = vi->i_ino;
401         return;
402 }
403
404 inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
405                 unsigned long mft_no)
406 {
407         ntfs_inode *ni = ntfs_alloc_extent_inode();
408
409         ntfs_debug("Entering.");
410         if (likely(ni != NULL)) {
411                 __ntfs_init_inode(sb, ni);
412                 ni->mft_no = mft_no;
413                 ni->type = AT_UNUSED;
414                 ni->name = NULL;
415                 ni->name_len = 0;
416         }
417         return ni;
418 }
419
420 /**
421  * ntfs_is_extended_system_file - check if a file is in the $Extend directory
422  * @ctx:        initialized attribute search context
423  *
424  * Search all file name attributes in the inode described by the attribute
425  * search context @ctx and check if any of the names are in the $Extend system
426  * directory.
427  *
428  * Return values:
429  *         1: file is in $Extend directory
430  *         0: file is not in $Extend directory
431  *      -EIO: file is corrupt
432  */
433 static int ntfs_is_extended_system_file(attr_search_context *ctx)
434 {
435         int nr_links;
436
437         /* Restart search. */
438         reinit_attr_search_ctx(ctx);
439
440         /* Get number of hard links. */
441         nr_links = le16_to_cpu(ctx->mrec->link_count);
442
443         /* Loop through all hard links. */
444         while (lookup_attr(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) {
445                 FILE_NAME_ATTR *file_name_attr;
446                 ATTR_RECORD *attr = ctx->attr;
447                 u8 *p, *p2;
448
449                 nr_links--;
450                 /*
451                  * Maximum sanity checking as we are called on an inode that
452                  * we suspect might be corrupt.
453                  */
454                 p = (u8*)attr + le32_to_cpu(attr->length);
455                 if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
456                                 le32_to_cpu(ctx->mrec->bytes_in_use)) {
457 err_corrupt_attr:
458                         ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
459                                         "attribute. You should run chkdsk.");
460                         return -EIO;
461                 }
462                 if (attr->non_resident) {
463                         ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
464                                         "name. You should run chkdsk.");
465                         return -EIO;
466                 }
467                 if (attr->flags) {
468                         ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
469                                         "invalid flags. You should run "
470                                         "chkdsk.");
471                         return -EIO;
472                 }
473                 if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
474                         ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
475                                         "name. You should run chkdsk.");
476                         return -EIO;
477                 }
478                 file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
479                                 le16_to_cpu(attr->data.resident.value_offset));
480                 p2 = (u8*)attr + le32_to_cpu(attr->data.resident.value_length);
481                 if (p2 < (u8*)attr || p2 > p)
482                         goto err_corrupt_attr;
483                 /* This attribute is ok, but is it in the $Extend directory? */
484                 if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
485                         return 1;       /* YES, it's an extended system file. */
486         }
487         if (nr_links) {
488                 ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
489                                 "doesn't match number of name attributes. You "
490                                 "should run chkdsk.");
491                 return -EIO;
492         }
493         return 0;       /* NO, it is not an extended system file. */
494 }
495
496 /**
497  * ntfs_read_locked_inode - read an inode from its device
498  * @vi:         inode to read
499  *
500  * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
501  * described by @vi into memory from the device.
502  *
503  * The only fields in @vi that we need to/can look at when the function is
504  * called are i_sb, pointing to the mounted device's super block, and i_ino,
505  * the number of the inode to load.
506  *
507  * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
508  * for reading and sets up the necessary @vi fields as well as initializing
509  * the ntfs inode.
510  *
511  * Q: What locks are held when the function is called?
512  * A: i_state has I_LOCK set, hence the inode is locked, also
513  *    i_count is set to 1, so it is not going to go away
514  *    i_flags is set to 0 and we have no business touching it.  Only an ioctl()
515  *    is allowed to write to them. We should of course be honouring them but
516  *    we need to do that using the IS_* macros defined in include/linux/fs.h.
517  *    In any case ntfs_read_locked_inode() has nothing to do with i_flags.
518  *
519  * Return 0 on success and -errno on error.  In the error case, the inode will
520  * have had make_bad_inode() executed on it.
521  */
522 static int ntfs_read_locked_inode(struct inode *vi)
523 {
524         ntfs_volume *vol = NTFS_SB(vi->i_sb);
525         ntfs_inode *ni;
526         MFT_RECORD *m;
527         STANDARD_INFORMATION *si;
528         attr_search_context *ctx;
529         int err = 0;
530
531         ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
532
533         /* Setup the generic vfs inode parts now. */
534
535         /* This is the optimal IO size (for stat), not the fs block size. */
536         vi->i_blksize = PAGE_CACHE_SIZE;
537         /*
538          * This is for checking whether an inode has changed w.r.t. a file so
539          * that the file can be updated if necessary (compare with f_version).
540          */
541         vi->i_version = 1;
542
543         vi->i_uid = vol->uid;
544         vi->i_gid = vol->gid;
545         vi->i_mode = 0;
546
547         /*
548          * Initialize the ntfs specific part of @vi special casing
549          * FILE_MFT which we need to do at mount time.
550          */
551         if (vi->i_ino != FILE_MFT)
552                 ntfs_init_big_inode(vi);
553         ni = NTFS_I(vi);
554
555         m = map_mft_record(ni);
556         if (IS_ERR(m)) {
557                 err = PTR_ERR(m);
558                 goto err_out;
559         }
560         ctx = get_attr_search_ctx(ni, m);
561         if (!ctx) {
562                 err = -ENOMEM;
563                 goto unm_err_out;
564         }
565
566         if (!(m->flags & MFT_RECORD_IN_USE)) {
567                 ntfs_error(vi->i_sb, "Inode is not in use! You should "
568                                 "run chkdsk.");
569                 goto unm_err_out;
570         }
571         if (m->base_mft_record) {
572                 ntfs_error(vi->i_sb, "Inode is an extent inode! You should "
573                                 "run chkdsk.");
574                 goto unm_err_out;
575         }
576
577         /* Transfer information from mft record into vfs and ntfs inodes. */
578         vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
579
580         /*
581          * FIXME: Keep in mind that link_count is two for files which have both
582          * a long file name and a short file name as separate entries, so if
583          * we are hiding short file names this will be too high. Either we need
584          * to account for the short file names by subtracting them or we need
585          * to make sure we delete files even though i_nlink is not zero which
586          * might be tricky due to vfs interactions. Need to think about this
587          * some more when implementing the unlink command.
588          */
589         vi->i_nlink = le16_to_cpu(m->link_count);
590         /*
591          * FIXME: Reparse points can have the directory bit set even though
592          * they would be S_IFLNK. Need to deal with this further below when we
593          * implement reparse points / symbolic links but it will do for now.
594          * Also if not a directory, it could be something else, rather than
595          * a regular file. But again, will do for now.
596          */
597         if (m->flags & MFT_RECORD_IS_DIRECTORY) {
598                 vi->i_mode |= S_IFDIR;
599                 /* Things break without this kludge! */
600                 if (vi->i_nlink > 1)
601                         vi->i_nlink = 1;
602         } else
603                 vi->i_mode |= S_IFREG;
604
605         /*
606          * Find the standard information attribute in the mft record. At this
607          * stage we haven't setup the attribute list stuff yet, so this could
608          * in fact fail if the standard information is in an extent record, but
609          * I don't think this actually ever happens.
610          */
611         if (!lookup_attr(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
612                         ctx)) {
613                 /*
614                  * TODO: We should be performing a hot fix here (if the recover
615                  * mount option is set) by creating a new attribute.
616                  */
617                 ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute is "
618                                 "missing.");
619                 goto unm_err_out;
620         }
621         /* Get the standard information attribute value. */
622         si = (STANDARD_INFORMATION*)((char*)ctx->attr +
623                         le16_to_cpu(ctx->attr->data.resident.value_offset));
624
625         /* Transfer information from the standard information into vfs_ino. */
626         /*
627          * Note: The i_?times do not quite map perfectly onto the NTFS times,
628          * but they are close enough, and in the end it doesn't really matter
629          * that much...
630          */
631         /*
632          * mtime is the last change of the data within the file. Not changed
633          * when only metadata is changed, e.g. a rename doesn't affect mtime.
634          */
635         vi->i_mtime = ntfs2utc(si->last_data_change_time);
636         /*
637          * ctime is the last change of the metadata of the file. This obviously
638          * always changes, when mtime is changed. ctime can be changed on its
639          * own, mtime is then not changed, e.g. when a file is renamed.
640          */
641         vi->i_ctime = ntfs2utc(si->last_mft_change_time);
642         /*
643          * Last access to the data within the file. Not changed during a rename
644          * for example but changed whenever the file is written to.
645          */
646         vi->i_atime = ntfs2utc(si->last_access_time);
647
648         /* Find the attribute list attribute if present. */
649         reinit_attr_search_ctx(ctx);
650         if (lookup_attr(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx)) {
651                 if (vi->i_ino == FILE_MFT)
652                         goto skip_attr_list_load;
653                 ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
654                 NInoSetAttrList(ni);
655                 if (ctx->attr->flags & ATTR_IS_ENCRYPTED ||
656                                 ctx->attr->flags & ATTR_COMPRESSION_MASK ||
657                                 ctx->attr->flags & ATTR_IS_SPARSE) {
658                         ntfs_error(vi->i_sb, "Attribute list attribute is "
659                                         "compressed/encrypted/sparse. Not "
660                                         "allowed. Corrupt inode. You should "
661                                         "run chkdsk.");
662                         goto unm_err_out;
663                 }
664                 /* Now allocate memory for the attribute list. */
665                 ni->attr_list_size = (u32)attribute_value_length(ctx->attr);
666                 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
667                 if (!ni->attr_list) {
668                         ntfs_error(vi->i_sb, "Not enough memory to allocate "
669                                         "buffer for attribute list.");
670                         err = -ENOMEM;
671                         goto unm_err_out;
672                 }
673                 if (ctx->attr->non_resident) {
674                         NInoSetAttrListNonResident(ni);
675                         if (ctx->attr->data.non_resident.lowest_vcn) {
676                                 ntfs_error(vi->i_sb, "Attribute list has non "
677                                                 "zero lowest_vcn. Inode is "
678                                                 "corrupt. You should run "
679                                                 "chkdsk.");
680                                 goto unm_err_out;
681                         }
682                         /*
683                          * Setup the run list. No need for locking as we have
684                          * exclusive access to the inode at this time.
685                          */
686                         ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
687                                         ctx->attr, NULL);
688                         if (IS_ERR(ni->attr_list_rl.rl)) {
689                                 err = PTR_ERR(ni->attr_list_rl.rl);
690                                 ni->attr_list_rl.rl = NULL;
691                                 ntfs_error(vi->i_sb, "Mapping pairs "
692                                                 "decompression failed with "
693                                                 "error code %i. Corrupt "
694                                                 "attribute list in inode.",
695                                                 -err);
696                                 goto unm_err_out;
697                         }
698                         /* Now load the attribute list. */
699                         if ((err = load_attribute_list(vol, &ni->attr_list_rl,
700                                         ni->attr_list, ni->attr_list_size,
701                                         sle64_to_cpu(ctx->attr->data.
702                                         non_resident.initialized_size)))) {
703                                 ntfs_error(vi->i_sb, "Failed to load "
704                                                 "attribute list attribute.");
705                                 goto unm_err_out;
706                         }
707                 } else /* if (!ctx.attr->non_resident) */ {
708                         if ((u8*)ctx->attr + le16_to_cpu(
709                                         ctx->attr->data.resident.value_offset) +
710                                         le32_to_cpu(
711                                         ctx->attr->data.resident.value_length) >
712                                         (u8*)ctx->mrec + vol->mft_record_size) {
713                                 ntfs_error(vi->i_sb, "Corrupt attribute list "
714                                                 "in inode.");
715                                 goto unm_err_out;
716                         }
717                         /* Now copy the attribute list. */
718                         memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu(
719                                         ctx->attr->data.resident.value_offset),
720                                         le32_to_cpu(
721                                         ctx->attr->data.resident.value_length));
722                 }
723         }
724 skip_attr_list_load:
725         /*
726          * If an attribute list is present we now have the attribute list value
727          * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
728          */
729         if (S_ISDIR(vi->i_mode)) {
730                 struct inode *bvi;
731                 ntfs_inode *bni;
732                 INDEX_ROOT *ir;
733                 char *ir_end, *index_end;
734
735                 /* It is a directory, find index root attribute. */
736                 reinit_attr_search_ctx(ctx);
737                 if (!lookup_attr(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0,
738                                 NULL, 0, ctx)) {
739                         // FIXME: File is corrupt! Hot-fix with empty index
740                         // root attribute if recovery option is set.
741                         ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
742                                         "missing.");
743                         goto unm_err_out;
744                 }
745                 /* Set up the state. */
746                 if (ctx->attr->non_resident) {
747                         ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
748                                         "not resident. Not allowed.");
749                         goto unm_err_out;
750                 }
751                 /*
752                  * Compressed/encrypted index root just means that the newly
753                  * created files in that directory should be created compressed/
754                  * encrypted. However index root cannot be both compressed and
755                  * encrypted.
756                  */
757                 if (ctx->attr->flags & ATTR_COMPRESSION_MASK)
758                         NInoSetCompressed(ni);
759                 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
760                         if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
761                                 ntfs_error(vi->i_sb, "Found encrypted and "
762                                                 "compressed attribute. Not "
763                                                 "allowed.");
764                                 goto unm_err_out;
765                         }
766                         NInoSetEncrypted(ni);
767                 }
768                 if (ctx->attr->flags & ATTR_IS_SPARSE)
769                         NInoSetSparse(ni);
770                 ir = (INDEX_ROOT*)((char*)ctx->attr + le16_to_cpu(
771                                 ctx->attr->data.resident.value_offset));
772                 ir_end = (char*)ir + le32_to_cpu(
773                                 ctx->attr->data.resident.value_length);
774                 if (ir_end > (char*)ctx->mrec + vol->mft_record_size) {
775                         ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
776                                         "corrupt.");
777                         goto unm_err_out;
778                 }
779                 index_end = (char*)&ir->index +
780                                 le32_to_cpu(ir->index.index_length);
781                 if (index_end > ir_end) {
782                         ntfs_error(vi->i_sb, "Directory index is corrupt.");
783                         goto unm_err_out;
784                 }
785                 if (ir->type != AT_FILE_NAME) {
786                         ntfs_error(vi->i_sb, "Indexed attribute is not "
787                                         "$FILE_NAME. Not allowed.");
788                         goto unm_err_out;
789                 }
790                 if (ir->collation_rule != COLLATION_FILE_NAME) {
791                         ntfs_error(vi->i_sb, "Index collation rule is not "
792                                         "COLLATION_FILE_NAME. Not allowed.");
793                         goto unm_err_out;
794                 }
795                 ni->itype.index.collation_rule = ir->collation_rule;
796                 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
797                 if (ni->itype.index.block_size &
798                                 (ni->itype.index.block_size - 1)) {
799                         ntfs_error(vi->i_sb, "Index block size (%u) is not a "
800                                         "power of two.",
801                                         ni->itype.index.block_size);
802                         goto unm_err_out;
803                 }
804                 if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
805                         ntfs_error(vi->i_sb, "Index block size (%u) > "
806                                         "PAGE_CACHE_SIZE (%ld) is not "
807                                         "supported. Sorry.",
808                                         ni->itype.index.block_size,
809                                         PAGE_CACHE_SIZE);
810                         err = -EOPNOTSUPP;
811                         goto unm_err_out;
812                 }
813                 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
814                         ntfs_error(vi->i_sb, "Index block size (%u) < "
815                                         "NTFS_BLOCK_SIZE (%i) is not "
816                                         "supported. Sorry.",
817                                         ni->itype.index.block_size,
818                                         NTFS_BLOCK_SIZE);
819                         err = -EOPNOTSUPP;
820                         goto unm_err_out;
821                 }
822                 ni->itype.index.block_size_bits =
823                                 ffs(ni->itype.index.block_size) - 1;
824                 /* Determine the size of a vcn in the directory index. */
825                 if (vol->cluster_size <= ni->itype.index.block_size) {
826                         ni->itype.index.vcn_size = vol->cluster_size;
827                         ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
828                 } else {
829                         ni->itype.index.vcn_size = vol->sector_size;
830                         ni->itype.index.vcn_size_bits = vol->sector_size_bits;
831                 }
832
833                 /* Setup the index allocation attribute, even if not present. */
834                 NInoSetMstProtected(ni);
835                 ni->type = AT_INDEX_ALLOCATION;
836                 ni->name = I30;
837                 ni->name_len = 4;
838
839                 if (!(ir->index.flags & LARGE_INDEX)) {
840                         /* No index allocation. */
841                         vi->i_size = ni->initialized_size =
842                                         ni->allocated_size = 0;
843                         /* We are done with the mft record, so we release it. */
844                         put_attr_search_ctx(ctx);
845                         unmap_mft_record(ni);
846                         m = NULL;
847                         ctx = NULL;
848                         goto skip_large_dir_stuff;
849                 } /* LARGE_INDEX: Index allocation present. Setup state. */
850                 NInoSetIndexAllocPresent(ni);
851                 /* Find index allocation attribute. */
852                 reinit_attr_search_ctx(ctx);
853                 if (!lookup_attr(AT_INDEX_ALLOCATION, I30, 4, CASE_SENSITIVE,
854                                 0, NULL, 0, ctx)) {
855                         ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
856                                         "is not present but $INDEX_ROOT "
857                                         "indicated it is.");
858                         goto unm_err_out;
859                 }
860                 if (!ctx->attr->non_resident) {
861                         ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
862                                         "is resident.");
863                         goto unm_err_out;
864                 }
865                 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
866                         ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
867                                         "is encrypted.");
868                         goto unm_err_out;
869                 }
870                 if (ctx->attr->flags & ATTR_IS_SPARSE) {
871                         ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
872                                         "is sparse.");
873                         goto unm_err_out;
874                 }
875                 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
876                         ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
877                                         "is compressed.");
878                         goto unm_err_out;
879                 }
880                 if (ctx->attr->data.non_resident.lowest_vcn) {
881                         ntfs_error(vi->i_sb, "First extent of "
882                                         "$INDEX_ALLOCATION attribute has non "
883                                         "zero lowest_vcn. Inode is corrupt. "
884                                         "You should run chkdsk.");
885                         goto unm_err_out;
886                 }
887                 vi->i_size = sle64_to_cpu(
888                                 ctx->attr->data.non_resident.data_size);
889                 ni->initialized_size = sle64_to_cpu(
890                                 ctx->attr->data.non_resident.initialized_size);
891                 ni->allocated_size = sle64_to_cpu(
892                                 ctx->attr->data.non_resident.allocated_size);
893                 /*
894                  * We are done with the mft record, so we release it. Otherwise
895                  * we would deadlock in ntfs_attr_iget().
896                  */
897                 put_attr_search_ctx(ctx);
898                 unmap_mft_record(ni);
899                 m = NULL;
900                 ctx = NULL;
901                 /* Get the index bitmap attribute inode. */
902                 bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
903                 if (unlikely(IS_ERR(bvi))) {
904                         ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
905                         err = PTR_ERR(bvi);
906                         goto unm_err_out;
907                 }
908                 ni->itype.index.bmp_ino = bvi;
909                 bni = NTFS_I(bvi);
910                 if (NInoCompressed(bni) || NInoEncrypted(bni) ||
911                                 NInoSparse(bni)) {
912                         ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
913                                         "and/or encrypted and/or sparse.");
914                         goto unm_err_out;
915                 }
916                 /* Consistency check bitmap size vs. index allocation size. */
917                 if ((bvi->i_size << 3) < (vi->i_size >>
918                                 ni->itype.index.block_size_bits)) {
919                         ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
920                                         "for index allocation (0x%llx).",
921                                         bvi->i_size << 3, vi->i_size);
922                         goto unm_err_out;
923                 }
924 skip_large_dir_stuff:
925                 /* Everyone gets read and scan permissions. */
926                 vi->i_mode |= S_IRUGO | S_IXUGO;
927                 /* If not read-only, set write permissions. */
928                 if (!IS_RDONLY(vi))
929                         vi->i_mode |= S_IWUGO;
930                 /*
931                  * Apply the directory permissions mask set in the mount
932                  * options.
933                  */
934                 vi->i_mode &= ~vol->dmask;
935                 /* Setup the operations for this inode. */
936                 vi->i_op = &ntfs_dir_inode_ops;
937                 vi->i_fop = &ntfs_dir_ops;
938                 vi->i_mapping->a_ops = &ntfs_mst_aops;
939         } else {
940                 /* It is a file. */
941                 reinit_attr_search_ctx(ctx);
942
943                 /* Setup the data attribute, even if not present. */
944                 ni->type = AT_DATA;
945                 ni->name = NULL;
946                 ni->name_len = 0;
947
948                 /* Find first extent of the unnamed data attribute. */
949                 if (!lookup_attr(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx)) {
950                         vi->i_size = ni->initialized_size =
951                                         ni->allocated_size = 0LL;
952                         /*
953                          * FILE_Secure does not have an unnamed $DATA
954                          * attribute, so we special case it here.
955                          */
956                         if (vi->i_ino == FILE_Secure)
957                                 goto no_data_attr_special_case;
958                         /*
959                          * Most if not all the system files in the $Extend
960                          * system directory do not have unnamed data
961                          * attributes so we need to check if the parent
962                          * directory of the file is FILE_Extend and if it is
963                          * ignore this error. To do this we need to get the
964                          * name of this inode from the mft record as the name
965                          * contains the back reference to the parent directory.
966                          */
967                         if (ntfs_is_extended_system_file(ctx) > 0)
968                                 goto no_data_attr_special_case;
969                         // FIXME: File is corrupt! Hot-fix with empty data
970                         // attribute if recovery option is set.
971                         ntfs_error(vi->i_sb, "$DATA attribute is "
972                                         "missing.");
973                         goto unm_err_out;
974                 }
975                 /* Setup the state. */
976                 if (ctx->attr->non_resident) {
977                         NInoSetNonResident(ni);
978                         if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
979                                 NInoSetCompressed(ni);
980                                 if (vol->cluster_size > 4096) {
981                                         ntfs_error(vi->i_sb, "Found "
982                                                 "compressed data but "
983                                                 "compression is disabled due "
984                                                 "to cluster size (%i) > 4kiB.",
985                                                 vol->cluster_size);
986                                         goto unm_err_out;
987                                 }
988                                 if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
989                                                 != ATTR_IS_COMPRESSED) {
990                                         ntfs_error(vi->i_sb, "Found "
991                                                 "unknown compression method or "
992                                                 "corrupt file.");
993                                         goto unm_err_out;
994                                 }
995                                 ni->itype.compressed.block_clusters = 1U <<
996                                                 ctx->attr->data.non_resident.
997                                                 compression_unit;
998                                 if (ctx->attr->data.non_resident.
999                                                 compression_unit != 4) {
1000                                         ntfs_error(vi->i_sb, "Found "
1001                                                 "nonstandard compression unit "
1002                                                 "(%u instead of 4). Cannot "
1003                                                 "handle this. This might "
1004                                                 "indicate corruption so you "
1005                                                 "should run chkdsk.",
1006                                                 ctx->attr->data.non_resident.
1007                                                 compression_unit);
1008                                         err = -EOPNOTSUPP;
1009                                         goto unm_err_out;
1010                                 }
1011                                 ni->itype.compressed.block_size = 1U << (
1012                                                 ctx->attr->data.non_resident.
1013                                                 compression_unit +
1014                                                 vol->cluster_size_bits);
1015                                 ni->itype.compressed.block_size_bits = ffs(
1016                                         ni->itype.compressed.block_size) - 1;
1017                         }
1018                         if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1019                                 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1020                                         ntfs_error(vi->i_sb, "Found encrypted "
1021                                                         "and compressed data.");
1022                                         goto unm_err_out;
1023                                 }
1024                                 NInoSetEncrypted(ni);
1025                         }
1026                         if (ctx->attr->flags & ATTR_IS_SPARSE)
1027                                 NInoSetSparse(ni);
1028                         if (ctx->attr->data.non_resident.lowest_vcn) {
1029                                 ntfs_error(vi->i_sb, "First extent of $DATA "
1030                                                 "attribute has non zero "
1031                                                 "lowest_vcn. Inode is corrupt. "
1032                                                 "You should run chkdsk.");
1033                                 goto unm_err_out;
1034                         }
1035                         /* Setup all the sizes. */
1036                         vi->i_size = sle64_to_cpu(
1037                                         ctx->attr->data.non_resident.data_size);
1038                         ni->initialized_size = sle64_to_cpu(
1039                                         ctx->attr->data.non_resident.
1040                                         initialized_size);
1041                         ni->allocated_size = sle64_to_cpu(
1042                                         ctx->attr->data.non_resident.
1043                                         allocated_size);
1044                         if (NInoCompressed(ni)) {
1045                                 ni->itype.compressed.size = sle64_to_cpu(
1046                                                 ctx->attr->data.non_resident.
1047                                                 compressed_size);
1048                         }
1049                 } else { /* Resident attribute. */
1050                         /*
1051                          * Make all sizes equal for simplicity in read code
1052                          * paths. FIXME: Need to keep this in mind when
1053                          * converting to non-resident attribute in write code
1054                          * path. (Probably only affects truncate().)
1055                          */
1056                         vi->i_size = ni->initialized_size = ni->allocated_size =
1057                                         le32_to_cpu(
1058                                         ctx->attr->data.resident.value_length);
1059                 }
1060 no_data_attr_special_case:
1061                 /* We are done with the mft record, so we release it. */
1062                 put_attr_search_ctx(ctx);
1063                 unmap_mft_record(ni);
1064                 m = NULL;
1065                 ctx = NULL;
1066                 /* Everyone gets all permissions. */
1067                 vi->i_mode |= S_IRWXUGO;
1068                 /* If read-only, noone gets write permissions. */
1069                 if (IS_RDONLY(vi))
1070                         vi->i_mode &= ~S_IWUGO;
1071                 /* Apply the file permissions mask set in the mount options. */
1072                 vi->i_mode &= ~vol->fmask;
1073                 /* Setup the operations for this inode. */
1074                 vi->i_op = &ntfs_file_inode_ops;
1075                 vi->i_fop = &ntfs_file_ops;
1076                 vi->i_mapping->a_ops = &ntfs_aops;
1077         }
1078         /*
1079          * The number of 512-byte blocks used on disk (for stat). This is in so
1080          * far inaccurate as it doesn't account for any named streams or other
1081          * special non-resident attributes, but that is how Windows works, too,
1082          * so we are at least consistent with Windows, if not entirely
1083          * consistent with the Linux Way. Doing it the Linux Way would cause a
1084          * significant slowdown as it would involve iterating over all
1085          * attributes in the mft record and adding the allocated/compressed
1086          * sizes of all non-resident attributes present to give us the Linux
1087          * correct size that should go into i_blocks (after division by 512).
1088          */
1089         if (S_ISDIR(vi->i_mode) || !NInoCompressed(ni))
1090                 vi->i_blocks = ni->allocated_size >> 9;
1091         else
1092                 vi->i_blocks = ni->itype.compressed.size >> 9;
1093
1094         ntfs_debug("Done.");
1095         return 0;
1096
1097 unm_err_out:
1098         if (!err)
1099                 err = -EIO;
1100         if (ctx)
1101                 put_attr_search_ctx(ctx);
1102         if (m)
1103                 unmap_mft_record(ni);
1104 err_out:
1105         ntfs_error(vi->i_sb, "Failed with error code %i. Marking inode 0x%lx "
1106                         "as bad.", -err, vi->i_ino);
1107         make_bad_inode(vi);
1108         return err;
1109 }
1110
1111 /**
1112  * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
1113  * @base_vi:    base inode
1114  * @vi:         attribute inode to read
1115  *
1116  * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
1117  * attribute inode described by @vi into memory from the base mft record
1118  * described by @base_ni.
1119  *
1120  * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
1121  * reading and looks up the attribute described by @vi before setting up the
1122  * necessary fields in @vi as well as initializing the ntfs inode.
1123  *
1124  * Q: What locks are held when the function is called?
1125  * A: i_state has I_LOCK set, hence the inode is locked, also
1126  *    i_count is set to 1, so it is not going to go away
1127  *
1128  * Return 0 on success and -errno on error.  In the error case, the inode will
1129  * have had make_bad_inode() executed on it.
1130  */
1131 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
1132 {
1133         ntfs_volume *vol = NTFS_SB(vi->i_sb);
1134         ntfs_inode *ni, *base_ni;
1135         MFT_RECORD *m;
1136         attr_search_context *ctx;
1137         int err = 0;
1138
1139         ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
1140
1141         ntfs_init_big_inode(vi);
1142
1143         ni      = NTFS_I(vi);
1144         base_ni = NTFS_I(base_vi);
1145
1146         /* Just mirror the values from the base inode. */
1147         vi->i_blksize   = base_vi->i_blksize;
1148         vi->i_version   = base_vi->i_version;
1149         vi->i_uid       = base_vi->i_uid;
1150         vi->i_gid       = base_vi->i_gid;
1151         vi->i_nlink     = base_vi->i_nlink;
1152         vi->i_mtime     = base_vi->i_mtime;
1153         vi->i_ctime     = base_vi->i_ctime;
1154         vi->i_atime     = base_vi->i_atime;
1155         vi->i_generation = ni->seq_no = base_ni->seq_no;
1156
1157         /* Set inode type to zero but preserve permissions. */
1158         vi->i_mode      = base_vi->i_mode & ~S_IFMT;
1159
1160         m = map_mft_record(base_ni);
1161         if (IS_ERR(m)) {
1162                 err = PTR_ERR(m);
1163                 goto err_out;
1164         }
1165         ctx = get_attr_search_ctx(base_ni, m);
1166         if (!ctx) {
1167                 err = -ENOMEM;
1168                 goto unm_err_out;
1169         }
1170
1171         /* Find the attribute. */
1172         if (!lookup_attr(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 0,
1173                         NULL, 0, ctx))
1174                 goto unm_err_out;
1175
1176         if (!ctx->attr->non_resident) {
1177                 if (NInoMstProtected(ni) || ctx->attr->flags) {
1178                         ntfs_error(vi->i_sb, "Found mst protected attribute "
1179                                         "or attribute with non-zero flags but "
1180                                         "the attribute is resident (mft_no "
1181                                         "0x%lx, type 0x%x, name_len %i). "
1182                                         "Please report you saw this message "
1183                                         "to linux-ntfs-dev@lists."
1184                                         "sourceforge.net",
1185                                         vi->i_ino, ni->type, ni->name_len);
1186                         goto unm_err_out;
1187                 }
1188                 /*
1189                  * Resident attribute. Make all sizes equal for simplicity in
1190                  * read code paths.
1191                  */
1192                 vi->i_size = ni->initialized_size = ni->allocated_size =
1193                         le32_to_cpu(ctx->attr->data.resident.value_length);
1194         } else {
1195                 NInoSetNonResident(ni);
1196                 if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1197                         if (NInoMstProtected(ni)) {
1198                                 ntfs_error(vi->i_sb, "Found mst protected "
1199                                                 "attribute but the attribute "
1200                                                 "is compressed (mft_no 0x%lx, "
1201                                                 "type 0x%x, name_len %i). "
1202                                                 "Please report you saw this "
1203                                                 "message to linux-ntfs-dev@"
1204                                                 "lists.sourceforge.net",
1205                                                 vi->i_ino, ni->type,
1206                                                 ni->name_len);
1207                                 goto unm_err_out;
1208                         }
1209                         NInoSetCompressed(ni);
1210                         if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
1211                                         ni->name_len)) {
1212                                 ntfs_error(vi->i_sb, "Found compressed non-"
1213                                                 "data or named data attribute "
1214                                                 "(mft_no 0x%lx, type 0x%x, "
1215                                                 "name_len %i). Please report "
1216                                                 "you saw this message to "
1217                                                 "linux-ntfs-dev@lists."
1218                                                 "sourceforge.net",
1219                                                 vi->i_ino, ni->type,
1220                                                 ni->name_len);
1221                                 goto unm_err_out;
1222                         }
1223                         if (vol->cluster_size > 4096) {
1224                                 ntfs_error(vi->i_sb, "Found "
1225                                         "compressed attribute but "
1226                                         "compression is disabled due "
1227                                         "to cluster size (%i) > 4kiB.",
1228                                         vol->cluster_size);
1229                                 goto unm_err_out;
1230                         }
1231                         if ((ctx->attr->flags & ATTR_COMPRESSION_MASK)
1232                                         != ATTR_IS_COMPRESSED) {
1233                                 ntfs_error(vi->i_sb, "Found unknown "
1234                                                 "compression method or "
1235                                                 "corrupt file.");
1236                                 goto unm_err_out;
1237                         }
1238                         ni->itype.compressed.block_clusters = 1U <<
1239                                         ctx->attr->data.non_resident.
1240                                         compression_unit;
1241                         if (ctx->attr->data.non_resident.compression_unit != 4) {
1242                                 ntfs_error(vi->i_sb, "Found "
1243                                         "nonstandard compression unit "
1244                                         "(%u instead of 4). Cannot "
1245                                         "handle this. This might "
1246                                         "indicate corruption so you "
1247                                         "should run chkdsk.",
1248                                         ctx->attr->data.non_resident.
1249                                         compression_unit);
1250                                 err = -EOPNOTSUPP;
1251                                 goto unm_err_out;
1252                         }
1253                         ni->itype.compressed.block_size = 1U << (
1254                                         ctx->attr->data.non_resident.
1255                                         compression_unit +
1256                                         vol->cluster_size_bits);
1257                         ni->itype.compressed.block_size_bits = ffs(
1258                                 ni->itype.compressed.block_size) - 1;
1259                 }
1260                 if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1261                         if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1262                                 ntfs_error(vi->i_sb, "Found encrypted "
1263                                                 "and compressed data.");
1264                                 goto unm_err_out;
1265                         }
1266                         if (NInoMstProtected(ni)) {
1267                                 ntfs_error(vi->i_sb, "Found mst protected "
1268                                                 "attribute but the attribute "
1269                                                 "is encrypted (mft_no 0x%lx, "
1270                                                 "type 0x%x, name_len %i). "
1271                                                 "Please report you saw this "
1272                                                 "message to linux-ntfs-dev@"
1273                                                 "lists.sourceforge.net",
1274                                                 vi->i_ino, ni->type,
1275                                                 ni->name_len);
1276                                 goto unm_err_out;
1277                         }
1278                         NInoSetEncrypted(ni);
1279                 }
1280                 if (ctx->attr->flags & ATTR_IS_SPARSE) {
1281                         if (NInoMstProtected(ni)) {
1282                                 ntfs_error(vi->i_sb, "Found mst protected "
1283                                                 "attribute but the attribute "
1284                                                 "is sparse (mft_no 0x%lx, "
1285                                                 "type 0x%x, name_len %i). "
1286                                                 "Please report you saw this "
1287                                                 "message to linux-ntfs-dev@"
1288                                                 "lists.sourceforge.net",
1289                                                 vi->i_ino, ni->type,
1290                                                 ni->name_len);
1291                                 goto unm_err_out;
1292                         }
1293                         NInoSetSparse(ni);
1294                 }
1295                 if (ctx->attr->data.non_resident.lowest_vcn) {
1296                         ntfs_error(vi->i_sb, "First extent of attribute has "
1297                                         "non-zero lowest_vcn. Inode is "
1298                                         "corrupt. You should run chkdsk.");
1299                         goto unm_err_out;
1300                 }
1301                 /* Setup all the sizes. */
1302                 vi->i_size = sle64_to_cpu(
1303                                 ctx->attr->data.non_resident.data_size);
1304                 ni->initialized_size = sle64_to_cpu(
1305                                 ctx->attr->data.non_resident.initialized_size);
1306                 ni->allocated_size = sle64_to_cpu(
1307                                 ctx->attr->data.non_resident.allocated_size);
1308                 if (NInoCompressed(ni)) {
1309                         ni->itype.compressed.size = sle64_to_cpu(
1310                                         ctx->attr->data.non_resident.
1311                                         compressed_size);
1312                 }
1313         }
1314
1315         /* Setup the operations for this attribute inode. */
1316         vi->i_op = NULL;
1317         vi->i_fop = NULL;
1318         if (NInoMstProtected(ni))
1319                 vi->i_mapping->a_ops = &ntfs_mst_aops;
1320         else
1321                 vi->i_mapping->a_ops = &ntfs_aops;
1322
1323         if (!NInoCompressed(ni))
1324                 vi->i_blocks = ni->allocated_size >> 9;
1325         else
1326                 vi->i_blocks = ni->itype.compressed.size >> 9;
1327
1328         /*
1329          * Make sure the base inode doesn't go away and attach it to the
1330          * attribute inode.
1331          */
1332         igrab(base_vi);
1333         ni->ext.base_ntfs_ino = base_ni;
1334         ni->nr_extents = -1;
1335
1336         put_attr_search_ctx(ctx);
1337         unmap_mft_record(base_ni);
1338
1339         ntfs_debug("Done.");
1340         return 0;
1341
1342 unm_err_out:
1343         if (!err)
1344                 err = -EIO;
1345         if (ctx)
1346                 put_attr_search_ctx(ctx);
1347         unmap_mft_record(base_ni);
1348 err_out:
1349         ntfs_error(vi->i_sb, "Failed with error code %i while reading "
1350                         "attribute inode (mft_no 0x%lx, type 0x%x, name_len "
1351                         "%i.", -err, vi->i_ino, ni->type, ni->name_len);
1352         make_bad_inode(vi);
1353         return err;
1354 }
1355
1356 /**
1357  * ntfs_read_locked_index_inode - read an index inode from its base inode
1358  * @base_vi:    base inode
1359  * @vi:         index inode to read
1360  *
1361  * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
1362  * index inode described by @vi into memory from the base mft record described
1363  * by @base_ni.
1364  *
1365  * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
1366  * reading and looks up the attributes relating to the index described by @vi
1367  * before setting up the necessary fields in @vi as well as initializing the
1368  * ntfs inode.
1369  *
1370  * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
1371  * with the attribute type set to AT_INDEX_ALLOCATION.  Apart from that, they
1372  * are setup like directory inodes since directories are a special case of
1373  * indices ao they need to be treated in much the same way.  Most importantly,
1374  * for small indices the index allocation attribute might not actually exist.
1375  * However, the index root attribute always exists but this does not need to
1376  * have an inode associated with it and this is why we define a new inode type
1377  * index.  Also, like for directories, we need to have an attribute inode for
1378  * the bitmap attribute corresponding to the index allocation attribute and we
1379  * can store this in the appropriate field of the inode, just like we do for
1380  * normal directory inodes.
1381  *
1382  * Q: What locks are held when the function is called?
1383  * A: i_state has I_LOCK set, hence the inode is locked, also
1384  *    i_count is set to 1, so it is not going to go away
1385  *
1386  * Return 0 on success and -errno on error.  In the error case, the inode will
1387  * have had make_bad_inode() executed on it.
1388  */
1389 static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
1390 {
1391         ntfs_volume *vol = NTFS_SB(vi->i_sb);
1392         ntfs_inode *ni, *base_ni, *bni;
1393         struct inode *bvi;
1394         MFT_RECORD *m;
1395         attr_search_context *ctx;
1396         INDEX_ROOT *ir;
1397         u8 *ir_end, *index_end;
1398         int err = 0;
1399
1400         ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
1401         ntfs_init_big_inode(vi);
1402         ni      = NTFS_I(vi);
1403         base_ni = NTFS_I(base_vi);
1404         /* Just mirror the values from the base inode. */
1405         vi->i_blksize   = base_vi->i_blksize;
1406         vi->i_version   = base_vi->i_version;
1407         vi->i_uid       = base_vi->i_uid;
1408         vi->i_gid       = base_vi->i_gid;
1409         vi->i_nlink     = base_vi->i_nlink;
1410         vi->i_mtime     = base_vi->i_mtime;
1411         vi->i_ctime     = base_vi->i_ctime;
1412         vi->i_atime     = base_vi->i_atime;
1413         vi->i_generation = ni->seq_no = base_ni->seq_no;
1414         /* Set inode type to zero but preserve permissions. */
1415         vi->i_mode      = base_vi->i_mode & ~S_IFMT;
1416         /* Map the mft record for the base inode. */
1417         m = map_mft_record(base_ni);
1418         if (IS_ERR(m)) {
1419                 err = PTR_ERR(m);
1420                 goto err_out;
1421         }
1422         ctx = get_attr_search_ctx(base_ni, m);
1423         if (!ctx) {
1424                 err = -ENOMEM;
1425                 goto unm_err_out;
1426         }
1427         /* Find the index root attribute. */
1428         if (!lookup_attr(AT_INDEX_ROOT, ni->name, ni->name_len, CASE_SENSITIVE,
1429                         0, NULL, 0, ctx)) {
1430                 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing.");
1431                 goto unm_err_out;
1432         }
1433         /* Set up the state. */
1434         if (ctx->attr->non_resident) {
1435                 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is not resident.  "
1436                                 "Not allowed.");
1437                 goto unm_err_out;
1438         }
1439         /* Compressed/encrypted/sparse index root is not allowed. */
1440         if (ctx->attr->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
1441                         ATTR_IS_SPARSE)) {
1442                 ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
1443                                 "root attribute.  Not allowed.");
1444                 goto unm_err_out;
1445         }
1446         ir = (INDEX_ROOT*)((u8*)ctx->attr +
1447                         le16_to_cpu(ctx->attr->data.resident.value_offset));
1448         ir_end = (u8*)ir + le32_to_cpu(ctx->attr->data.resident.value_length);
1449         if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
1450                 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
1451                 goto unm_err_out;
1452         }
1453         index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
1454         if (index_end > ir_end) {
1455                 ntfs_error(vi->i_sb, "Index is corrupt.");
1456                 goto unm_err_out;
1457         }
1458         if (ir->type) {
1459                 ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).  "
1460                                 "Not allowed.", le32_to_cpu(ir->type));
1461                 goto unm_err_out;
1462         }
1463         ni->itype.index.collation_rule = ir->collation_rule;
1464         ntfs_debug("Index collation rule is 0x%x.",
1465                         le32_to_cpu(ir->collation_rule));
1466         ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
1467         if (ni->itype.index.block_size & (ni->itype.index.block_size - 1)) {
1468                 ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
1469                                 "two.", ni->itype.index.block_size);
1470                 goto unm_err_out;
1471         }
1472         if (ni->itype.index.block_size > PAGE_CACHE_SIZE) {
1473                 ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_CACHE_SIZE "
1474                                 "(%ld) is not supported.  Sorry.",
1475                                 ni->itype.index.block_size, PAGE_CACHE_SIZE);
1476                 err = -EOPNOTSUPP;
1477                 goto unm_err_out;
1478         }
1479         if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
1480                 ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
1481                                 "(%i) is not supported.  Sorry.",
1482                                 ni->itype.index.block_size, NTFS_BLOCK_SIZE);
1483                 err = -EOPNOTSUPP;
1484                 goto unm_err_out;
1485         }
1486         ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
1487         /* Determine the size of a vcn in the index. */
1488         if (vol->cluster_size <= ni->itype.index.block_size) {
1489                 ni->itype.index.vcn_size = vol->cluster_size;
1490                 ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
1491         } else {
1492                 ni->itype.index.vcn_size = vol->sector_size;
1493                 ni->itype.index.vcn_size_bits = vol->sector_size_bits;
1494         }
1495         /* Check for presence of index allocation attribute. */
1496         if (!(ir->index.flags & LARGE_INDEX)) {
1497                 /* No index allocation. */
1498                 vi->i_size = ni->initialized_size = ni->allocated_size = 0;
1499                 /* We are done with the mft record, so we release it. */
1500                 put_attr_search_ctx(ctx);
1501                 unmap_mft_record(base_ni);
1502                 m = NULL;
1503                 ctx = NULL;
1504                 goto skip_large_index_stuff;
1505         } /* LARGE_INDEX:  Index allocation present.  Setup state. */
1506         NInoSetIndexAllocPresent(ni);
1507         /* Find index allocation attribute. */
1508         reinit_attr_search_ctx(ctx);
1509         if (!lookup_attr(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
1510                         CASE_SENSITIVE, 0, NULL, 0, ctx)) {
1511                 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is not "
1512                                 "present but $INDEX_ROOT indicated it is.");
1513                 goto unm_err_out;
1514         }
1515         if (!ctx->attr->non_resident) {
1516                 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1517                                 "resident.");
1518                 goto unm_err_out;
1519         }
1520         if (ctx->attr->flags & ATTR_IS_ENCRYPTED) {
1521                 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1522                                 "encrypted.");
1523                 goto unm_err_out;
1524         }
1525         if (ctx->attr->flags & ATTR_IS_SPARSE) {
1526                 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
1527                 goto unm_err_out;
1528         }
1529         if (ctx->attr->flags & ATTR_COMPRESSION_MASK) {
1530                 ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
1531                                 "compressed.");
1532                 goto unm_err_out;
1533         }
1534         if (ctx->attr->data.non_resident.lowest_vcn) {
1535                 ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
1536                                 "attribute has non zero lowest_vcn.  Inode is "
1537                                 "corrupt. You should run chkdsk.");
1538                 goto unm_err_out;
1539         }
1540         vi->i_size = sle64_to_cpu(ctx->attr->data.non_resident.data_size);
1541         ni->initialized_size = sle64_to_cpu(
1542                         ctx->attr->data.non_resident.initialized_size);
1543         ni->allocated_size = sle64_to_cpu(
1544                         ctx->attr->data.non_resident.allocated_size);
1545         /*
1546          * We are done with the mft record, so we release it.  Otherwise
1547          * we would deadlock in ntfs_attr_iget().
1548          */
1549         put_attr_search_ctx(ctx);
1550         unmap_mft_record(base_ni);
1551         m = NULL;
1552         ctx = NULL;
1553         /* Get the index bitmap attribute inode. */
1554         bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
1555         if (unlikely(IS_ERR(bvi))) {
1556                 ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
1557                 err = PTR_ERR(bvi);
1558                 goto unm_err_out;
1559         }
1560         bni = NTFS_I(bvi);
1561         if (NInoCompressed(bni) || NInoEncrypted(bni) ||
1562                         NInoSparse(bni)) {
1563                 ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
1564                                 "and/or encrypted and/or sparse.");
1565                 goto iput_unm_err_out;
1566         }
1567         /* Consistency check bitmap size vs. index allocation size. */
1568         if ((bvi->i_size << 3) < (vi->i_size >>
1569                         ni->itype.index.block_size_bits)) {
1570                 ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
1571                                 "for index allocation (0x%llx).",
1572                                 bvi->i_size << 3, vi->i_size);
1573                 goto iput_unm_err_out;
1574         }
1575         ni->itype.index.bmp_ino = bvi;
1576 skip_large_index_stuff:
1577         /* Setup the operations for this index inode. */
1578         vi->i_op = NULL;
1579         vi->i_fop = NULL;
1580         vi->i_mapping->a_ops = &ntfs_mst_aops;
1581         vi->i_blocks = ni->allocated_size >> 9;
1582
1583         /*
1584          * Make sure the base inode doesn't go away and attach it to the
1585          * index inode.
1586          */
1587         igrab(base_vi);
1588         ni->ext.base_ntfs_ino = base_ni;
1589         ni->nr_extents = -1;
1590
1591         ntfs_debug("Done.");
1592         return 0;
1593
1594 iput_unm_err_out:
1595         iput(bvi);
1596 unm_err_out:
1597         if (!err)
1598                 err = -EIO;
1599         if (ctx)
1600                 put_attr_search_ctx(ctx);
1601         if (m)
1602                 unmap_mft_record(base_ni);
1603 err_out:
1604         ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
1605                         "inode (mft_no 0x%lx, name_len %i.", -err, vi->i_ino,
1606                         ni->name_len);
1607         make_bad_inode(vi);
1608         return err;
1609 }
1610
1611 /**
1612  * ntfs_read_inode_mount - special read_inode for mount time use only
1613  * @vi:         inode to read
1614  *
1615  * Read inode FILE_MFT at mount time, only called with super_block lock
1616  * held from within the read_super() code path.
1617  *
1618  * This function exists because when it is called the page cache for $MFT/$DATA
1619  * is not initialized and hence we cannot get at the contents of mft records
1620  * by calling map_mft_record*().
1621  *
1622  * Further it needs to cope with the circular references problem, i.e. can't
1623  * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
1624  * we don't know where the other extent mft records are yet and again, because
1625  * we cannot call map_mft_record*() yet. Obviously this applies only when an
1626  * attribute list is actually present in $MFT inode.
1627  *
1628  * We solve these problems by starting with the $DATA attribute before anything
1629  * else and iterating using lookup_attr($DATA) over all extents. As each extent
1630  * is found, we decompress_mapping_pairs() including the implied
1631  * merge_run_lists(). Each step of the iteration necessarily provides
1632  * sufficient information for the next step to complete.
1633  *
1634  * This should work but there are two possible pit falls (see inline comments
1635  * below), but only time will tell if they are real pits or just smoke...
1636  */
1637 int ntfs_read_inode_mount(struct inode *vi)
1638 {
1639         VCN next_vcn, last_vcn, highest_vcn;
1640         s64 block;
1641         struct super_block *sb = vi->i_sb;
1642         ntfs_volume *vol = NTFS_SB(sb);
1643         struct buffer_head *bh;
1644         ntfs_inode *ni;
1645         MFT_RECORD *m = NULL;
1646         ATTR_RECORD *attr;
1647         attr_search_context *ctx;
1648         unsigned int i, nr_blocks;
1649         int err;
1650
1651         ntfs_debug("Entering.");
1652
1653         /* Initialize the ntfs specific part of @vi. */
1654         ntfs_init_big_inode(vi);
1655
1656         ni = NTFS_I(vi);
1657
1658         /* Setup the data attribute. It is special as it is mst protected. */
1659         NInoSetNonResident(ni);
1660         NInoSetMstProtected(ni);
1661         ni->type = AT_DATA;
1662         ni->name = NULL;
1663         ni->name_len = 0;
1664
1665         /*
1666          * This sets up our little cheat allowing us to reuse the async read io
1667          * completion handler for directories.
1668          */
1669         ni->itype.index.block_size = vol->mft_record_size;
1670         ni->itype.index.block_size_bits = vol->mft_record_size_bits;
1671
1672         /* Very important! Needed to be able to call map_mft_record*(). */
1673         vol->mft_ino = vi;
1674
1675         /* Allocate enough memory to read the first mft record. */
1676         if (vol->mft_record_size > 64 * 1024) {
1677                 ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
1678                                 vol->mft_record_size);
1679                 goto err_out;
1680         }
1681         i = vol->mft_record_size;
1682         if (i < sb->s_blocksize)
1683                 i = sb->s_blocksize;
1684         m = (MFT_RECORD*)ntfs_malloc_nofs(i);
1685         if (!m) {
1686                 ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
1687                 goto err_out;
1688         }
1689
1690         /* Determine the first block of the $MFT/$DATA attribute. */
1691         block = vol->mft_lcn << vol->cluster_size_bits >>
1692                         sb->s_blocksize_bits;
1693         nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
1694         if (!nr_blocks)
1695                 nr_blocks = 1;
1696
1697         /* Load $MFT/$DATA's first mft record. */
1698         for (i = 0; i < nr_blocks; i++) {
1699                 bh = sb_bread(sb, block++);
1700                 if (!bh) {
1701                         ntfs_error(sb, "Device read failed.");
1702                         goto err_out;
1703                 }
1704                 memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
1705                                 sb->s_blocksize);
1706                 brelse(bh);
1707         }
1708
1709         /* Apply the mst fixups. */
1710         if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
1711                 /* FIXME: Try to use the $MFTMirr now. */
1712                 ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
1713                 goto err_out;
1714         }
1715
1716         /* Need this to sanity check attribute list references to $MFT. */
1717         vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
1718
1719         /* Provides readpage() and sync_page() for map_mft_record(). */
1720         vi->i_mapping->a_ops = &ntfs_mft_aops;
1721
1722         ctx = get_attr_search_ctx(ni, m);
1723         if (!ctx) {
1724                 err = -ENOMEM;
1725                 goto err_out;
1726         }
1727
1728         /* Find the attribute list attribute if present. */
1729         if (lookup_attr(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx)) {
1730                 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
1731                 u8 *al_end;
1732
1733                 ntfs_debug("Attribute list attribute found in $MFT.");
1734                 NInoSetAttrList(ni);
1735                 if (ctx->attr->flags & ATTR_IS_ENCRYPTED ||
1736                                 ctx->attr->flags & ATTR_COMPRESSION_MASK ||
1737                                 ctx->attr->flags & ATTR_IS_SPARSE) {
1738                         ntfs_error(sb, "Attribute list attribute is "
1739                                         "compressed/encrypted/sparse. Not "
1740                                         "allowed. $MFT is corrupt. You should "
1741                                         "run chkdsk.");
1742                         goto put_err_out;
1743                 }
1744                 /* Now allocate memory for the attribute list. */
1745                 ni->attr_list_size = (u32)attribute_value_length(ctx->attr);
1746                 ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
1747                 if (!ni->attr_list) {
1748                         ntfs_error(sb, "Not enough memory to allocate buffer "
1749                                         "for attribute list.");
1750                         goto put_err_out;
1751                 }
1752                 if (ctx->attr->non_resident) {
1753                         NInoSetAttrListNonResident(ni);
1754                         if (ctx->attr->data.non_resident.lowest_vcn) {
1755                                 ntfs_error(sb, "Attribute list has non zero "
1756                                                 "lowest_vcn. $MFT is corrupt. "
1757                                                 "You should run chkdsk.");
1758                                 goto put_err_out;
1759                         }
1760                         /* Setup the run list. */
1761                         ni->attr_list_rl.rl = decompress_mapping_pairs(vol,
1762                                         ctx->attr, NULL);
1763                         if (IS_ERR(ni->attr_list_rl.rl)) {
1764                                 err = PTR_ERR(ni->attr_list_rl.rl);
1765                                 ni->attr_list_rl.rl = NULL;
1766                                 ntfs_error(sb, "Mapping pairs decompression "
1767                                                 "failed with error code %i.",
1768                                                 -err);
1769                                 goto put_err_out;
1770                         }
1771                         /* Now load the attribute list. */
1772                         if ((err = load_attribute_list(vol, &ni->attr_list_rl,
1773                                         ni->attr_list, ni->attr_list_size,
1774                                         sle64_to_cpu(ctx->attr->data.
1775                                         non_resident.initialized_size)))) {
1776                                 ntfs_error(sb, "Failed to load attribute list "
1777                                                 "attribute with error code %i.",
1778                                                 -err);
1779                                 goto put_err_out;
1780                         }
1781                 } else /* if (!ctx.attr->non_resident) */ {
1782                         if ((u8*)ctx->attr + le16_to_cpu(
1783                                         ctx->attr->data.resident.value_offset) +
1784                                         le32_to_cpu(
1785                                         ctx->attr->data.resident.value_length) >
1786                                         (u8*)ctx->mrec + vol->mft_record_size) {
1787                                 ntfs_error(sb, "Corrupt attribute list "
1788                                                 "attribute.");
1789                                 goto put_err_out;
1790                         }
1791                         /* Now copy the attribute list. */
1792                         memcpy(ni->attr_list, (u8*)ctx->attr + le16_to_cpu(
1793                                         ctx->attr->data.resident.value_offset),
1794                                         le32_to_cpu(
1795                                         ctx->attr->data.resident.value_length));
1796                 }
1797                 /* The attribute list is now setup in memory. */
1798                 /*
1799                  * FIXME: I don't know if this case is actually possible.
1800                  * According to logic it is not possible but I have seen too
1801                  * many weird things in MS software to rely on logic... Thus we
1802                  * perform a manual search and make sure the first $MFT/$DATA
1803                  * extent is in the base inode. If it is not we abort with an
1804                  * error and if we ever see a report of this error we will need
1805                  * to do some magic in order to have the necessary mft record
1806                  * loaded and in the right place in the page cache. But
1807                  * hopefully logic will prevail and this never happens...
1808                  */
1809                 al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
1810                 al_end = (u8*)al_entry + ni->attr_list_size;
1811                 for (;; al_entry = next_al_entry) {
1812                         /* Out of bounds check. */
1813                         if ((u8*)al_entry < ni->attr_list ||
1814                                         (u8*)al_entry > al_end)
1815                                 goto em_put_err_out;
1816                         /* Catch the end of the attribute list. */
1817                         if ((u8*)al_entry == al_end)
1818                                 goto em_put_err_out;
1819                         if (!al_entry->length)
1820                                 goto em_put_err_out;
1821                         if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
1822                                         le16_to_cpu(al_entry->length) > al_end)
1823                                 goto em_put_err_out;
1824                         next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
1825                                         le16_to_cpu(al_entry->length));
1826                         if (le32_to_cpu(al_entry->type) >
1827                                         const_le32_to_cpu(AT_DATA))
1828                                 goto em_put_err_out;
1829                         if (AT_DATA != al_entry->type)
1830                                 continue;
1831                         /* We want an unnamed attribute. */
1832                         if (al_entry->name_length)
1833                                 goto em_put_err_out;
1834                         /* Want the first entry, i.e. lowest_vcn == 0. */
1835                         if (al_entry->lowest_vcn)
1836                                 goto em_put_err_out;
1837                         /* First entry has to be in the base mft record. */
1838                         if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
1839                                 /* MFT references do not match, logic fails. */
1840                                 ntfs_error(sb, "BUG: The first $DATA extent "
1841                                                 "of $MFT is not in the base "
1842                                                 "mft record. Please report "
1843                                                 "you saw this message to "
1844                                                 "linux-ntfs-dev@lists."
1845                                                 "sourceforge.net");
1846                                 goto put_err_out;
1847                         } else {
1848                                 /* Sequence numbers must match. */
1849                                 if (MSEQNO_LE(al_entry->mft_reference) !=
1850                                                 ni->seq_no)
1851                                         goto em_put_err_out;
1852                                 /* Got it. All is ok. We can stop now. */
1853                                 break;
1854                         }
1855                 }
1856         }
1857
1858         reinit_attr_search_ctx(ctx);
1859
1860         /* Now load all attribute extents. */
1861         attr = NULL;
1862         next_vcn = last_vcn = highest_vcn = 0;
1863         while (lookup_attr(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, ctx)) {
1864                 run_list_element *nrl;
1865
1866                 /* Cache the current attribute. */
1867                 attr = ctx->attr;
1868                 /* $MFT must be non-resident. */
1869                 if (!attr->non_resident) {
1870                         ntfs_error(sb, "$MFT must be non-resident but a "
1871                                         "resident extent was found. $MFT is "
1872                                         "corrupt. Run chkdsk.");
1873                         goto put_err_out;
1874                 }
1875                 /* $MFT must be uncompressed and unencrypted. */
1876                 if (attr->flags & ATTR_COMPRESSION_MASK ||
1877                                 attr->flags & ATTR_IS_ENCRYPTED ||
1878                                 attr->flags & ATTR_IS_SPARSE) {
1879                         ntfs_error(sb, "$MFT must be uncompressed, "
1880                                         "non-sparse, and unencrypted but a "
1881                                         "compressed/sparse/encrypted extent "
1882                                         "was found. $MFT is corrupt. Run "
1883                                         "chkdsk.");
1884                         goto put_err_out;
1885                 }
1886                 /*
1887                  * Decompress the mapping pairs array of this extent and merge
1888                  * the result into the existing run list. No need for locking
1889                  * as we have exclusive access to the inode at this time and we
1890                  * are a mount in progress task, too.
1891                  */
1892                 nrl = decompress_mapping_pairs(vol, attr, ni->run_list.rl);
1893                 if (IS_ERR(nrl)) {
1894                         ntfs_error(sb, "decompress_mapping_pairs() failed with "
1895                                         "error code %ld. $MFT is corrupt.",
1896                                         PTR_ERR(nrl));
1897                         goto put_err_out;
1898                 }
1899                 ni->run_list.rl = nrl;
1900
1901                 /* Are we in the first extent? */
1902                 if (!next_vcn) {
1903                         u64 ll;
1904
1905                         if (attr->data.non_resident.lowest_vcn) {
1906                                 ntfs_error(sb, "First extent of $DATA "
1907                                                 "attribute has non zero "
1908                                                 "lowest_vcn. $MFT is corrupt. "
1909                                                 "You should run chkdsk.");
1910                                 goto put_err_out;
1911                         }
1912                         /* Get the last vcn in the $DATA attribute. */
1913                         last_vcn = sle64_to_cpu(
1914                                         attr->data.non_resident.allocated_size)
1915                                         >> vol->cluster_size_bits;
1916                         /* Fill in the inode size. */
1917                         vi->i_size = sle64_to_cpu(
1918                                         attr->data.non_resident.data_size);
1919                         ni->initialized_size = sle64_to_cpu(attr->data.
1920                                         non_resident.initialized_size);
1921                         ni->allocated_size = sle64_to_cpu(
1922                                         attr->data.non_resident.allocated_size);
1923                         /* Set the number of mft records. */
1924                         ll = vi->i_size >> vol->mft_record_size_bits;
1925                         /*
1926                          * Verify the number of mft records does not exceed
1927                          * 2^32 - 1.
1928                          */
1929                         if (ll >= (1ULL << 32)) {
1930                                 ntfs_error(sb, "$MFT is too big! Aborting.");
1931                                 goto put_err_out;
1932                         }
1933                         vol->nr_mft_records = ll;
1934                         /*
1935                          * We have got the first extent of the run_list for
1936                          * $MFT which means it is now relatively safe to call
1937                          * the normal ntfs_read_inode() function.
1938                          * Complete reading the inode, this will actually
1939                          * re-read the mft record for $MFT, this time entering
1940                          * it into the page cache with which we complete the
1941                          * kick start of the volume. It should be safe to do
1942                          * this now as the first extent of $MFT/$DATA is
1943                          * already known and we would hope that we don't need
1944                          * further extents in order to find the other
1945                          * attributes belonging to $MFT. Only time will tell if
1946                          * this is really the case. If not we will have to play
1947                          * magic at this point, possibly duplicating a lot of
1948                          * ntfs_read_inode() at this point. We will need to
1949                          * ensure we do enough of its work to be able to call
1950                          * ntfs_read_inode() on extents of $MFT/$DATA. But lets
1951                          * hope this never happens...
1952                          */
1953                         ntfs_read_locked_inode(vi);
1954                         if (is_bad_inode(vi)) {
1955                                 ntfs_error(sb, "ntfs_read_inode() of $MFT "
1956                                                 "failed. BUG or corrupt $MFT. "
1957                                                 "Run chkdsk and if no errors "
1958                                                 "are found, please report you "
1959                                                 "saw this message to "
1960                                                 "linux-ntfs-dev@lists."
1961                                                 "sourceforge.net");
1962                                 put_attr_search_ctx(ctx);
1963                                 /* Revert to the safe super operations. */
1964                                 ntfs_free(m);
1965                                 return -1;
1966                         }
1967                         /*
1968                          * Re-initialize some specifics about $MFT's inode as
1969                          * ntfs_read_inode() will have set up the default ones.
1970                          */
1971                         /* Set uid and gid to root. */
1972                         vi->i_uid = vi->i_gid = 0;
1973                         /* Regular file. No access for anyone. */
1974                         vi->i_mode = S_IFREG;
1975                         /* No VFS initiated operations allowed for $MFT. */
1976                         vi->i_op = &ntfs_empty_inode_ops;
1977                         vi->i_fop = &ntfs_empty_file_ops;
1978                         /* Put back our special address space operations. */
1979                         vi->i_mapping->a_ops = &ntfs_mft_aops;
1980                 }
1981
1982                 /* Get the lowest vcn for the next extent. */
1983                 highest_vcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
1984                 next_vcn = highest_vcn + 1;
1985
1986                 /* Only one extent or error, which we catch below. */
1987                 if (next_vcn <= 0)
1988                         break;
1989
1990                 /* Avoid endless loops due to corruption. */
1991                 if (next_vcn < sle64_to_cpu(
1992                                 attr->data.non_resident.lowest_vcn)) {
1993                         ntfs_error(sb, "$MFT has corrupt attribute list "
1994                                         "attribute. Run chkdsk.");
1995                         goto put_err_out;
1996                 }
1997         }
1998         if (!attr) {
1999                 ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
2000                                 "corrupt. Run chkdsk.");
2001                 goto put_err_out;
2002         }
2003         if (highest_vcn && highest_vcn != last_vcn - 1) {
2004                 ntfs_error(sb, "Failed to load the complete run list "
2005                                 "for $MFT/$DATA. Driver bug or "
2006                                 "corrupt $MFT. Run chkdsk.");
2007                 ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
2008                                 (unsigned long long)highest_vcn,
2009                                 (unsigned long long)last_vcn - 1);
2010                 goto put_err_out;
2011         }
2012         put_attr_search_ctx(ctx);
2013         ntfs_debug("Done.");
2014         ntfs_free(m);
2015         return 0;
2016
2017 em_put_err_out:
2018         ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
2019                         "attribute list. $MFT is corrupt. Run chkdsk.");
2020 put_err_out:
2021         put_attr_search_ctx(ctx);
2022 err_out:
2023         ntfs_error(sb, "Failed. Marking inode as bad.");
2024         make_bad_inode(vi);
2025         ntfs_free(m);
2026         return -1;
2027 }
2028
2029 /**
2030  * ntfs_put_inode - handler for when the inode reference count is decremented
2031  * @vi:         vfs inode
2032  *
2033  * The VFS calls ntfs_put_inode() every time the inode reference count (i_count)
2034  * is about to be decremented (but before the decrement itself.
2035  *
2036  * If the inode @vi is a directory with two references, one of which is being
2037  * dropped, we need to put the attribute inode for the directory index bitmap,
2038  * if it is present, otherwise the directory inode would remain pinned for
2039  * ever.
2040  *
2041  * If the inode @vi is an index inode with only one reference which is being
2042  * dropped, we need to put the attribute inode for the index bitmap, if it is
2043  * present, otherwise the index inode would disappear and the attribute inode
2044  * for the index bitmap would no longer be referenced from anywhere and thus it
2045  * would remain pinned for ever.
2046  */
2047 void ntfs_put_inode(struct inode *vi)
2048 {
2049         ntfs_inode *ni;
2050
2051         if (S_ISDIR(vi->i_mode)) {
2052                 if (atomic_read(&vi->i_count) == 2) {
2053                         ni = NTFS_I(vi);
2054                         if (NInoIndexAllocPresent(ni) &&
2055                                         ni->itype.index.bmp_ino) {
2056                                 iput(ni->itype.index.bmp_ino);
2057                                 ni->itype.index.bmp_ino = NULL;
2058                         }
2059                 }
2060                 return;
2061         }
2062         if (atomic_read(&vi->i_count) != 1)
2063                 return;
2064         ni = NTFS_I(vi);
2065         if (NInoAttr(ni) && (ni->type == AT_INDEX_ALLOCATION) &&
2066                         NInoIndexAllocPresent(ni) && ni->itype.index.bmp_ino) {
2067                 iput(ni->itype.index.bmp_ino);
2068                 ni->itype.index.bmp_ino = NULL;
2069         }
2070         return;
2071 }
2072
2073 void __ntfs_clear_inode(ntfs_inode *ni)
2074 {
2075         /* Free all alocated memory. */
2076         down_write(&ni->run_list.lock);
2077         if (ni->run_list.rl) {
2078                 ntfs_free(ni->run_list.rl);
2079                 ni->run_list.rl = NULL;
2080         }
2081         up_write(&ni->run_list.lock);
2082
2083         if (ni->attr_list) {
2084                 ntfs_free(ni->attr_list);
2085                 ni->attr_list = NULL;
2086         }
2087
2088         down_write(&ni->attr_list_rl.lock);
2089         if (ni->attr_list_rl.rl) {
2090                 ntfs_free(ni->attr_list_rl.rl);
2091                 ni->attr_list_rl.rl = NULL;
2092         }
2093         up_write(&ni->attr_list_rl.lock);
2094
2095         if (ni->name_len && ni->name != I30) {
2096                 /* Catch bugs... */
2097                 BUG_ON(!ni->name);
2098                 kfree(ni->name);
2099         }
2100 }
2101
2102 void ntfs_clear_extent_inode(ntfs_inode *ni)
2103 {
2104         ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
2105
2106         BUG_ON(NInoAttr(ni));
2107         BUG_ON(ni->nr_extents != -1);
2108
2109 #ifdef NTFS_RW
2110         if (NInoDirty(ni)) {
2111                 if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
2112                         ntfs_error(ni->vol->sb, "Clearing dirty extent inode!  "
2113                                         "Losing data!  This is a BUG!!!");
2114                 // FIXME:  Do something!!!
2115         }
2116 #endif /* NTFS_RW */
2117
2118         __ntfs_clear_inode(ni);
2119
2120         /* Bye, bye... */
2121         ntfs_destroy_extent_inode(ni);
2122 }
2123
2124 /**
2125  * ntfs_clear_big_inode - clean up the ntfs specific part of an inode
2126  * @vi:         vfs inode pending annihilation
2127  *
2128  * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
2129  * is called, which deallocates all memory belonging to the NTFS specific part
2130  * of the inode and returns.
2131  *
2132  * If the MFT record is dirty, we commit it before doing anything else.
2133  */
2134 void ntfs_clear_big_inode(struct inode *vi)
2135 {
2136         ntfs_inode *ni = NTFS_I(vi);
2137
2138 #ifdef NTFS_RW
2139         if (NInoDirty(ni)) {
2140                 BOOL was_bad = (is_bad_inode(vi));
2141
2142                 /* Committing the inode also commits all extent inodes. */
2143                 ntfs_commit_inode(vi);
2144
2145                 if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
2146                         ntfs_error(vi->i_sb, "Failed to commit dirty inode "
2147                                         "0x%lx.  Losing data!", vi->i_ino);
2148                         // FIXME:  Do something!!!
2149                 }
2150         }
2151 #endif /* NTFS_RW */
2152
2153         /* No need to lock at this stage as no one else has a reference. */
2154         if (ni->nr_extents > 0) {
2155                 int i;
2156
2157                 for (i = 0; i < ni->nr_extents; i++)
2158                         ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
2159                 kfree(ni->ext.extent_ntfs_inos);
2160         }
2161
2162         __ntfs_clear_inode(ni);
2163
2164         if (NInoAttr(ni)) {
2165                 /* Release the base inode if we are holding it. */
2166                 if (ni->nr_extents == -1) {
2167                         iput(VFS_I(ni->ext.base_ntfs_ino));
2168                         ni->nr_extents = 0;
2169                         ni->ext.base_ntfs_ino = NULL;
2170                 }
2171         }
2172         return;
2173 }
2174
2175 /**
2176  * ntfs_show_options - show mount options in /proc/mounts
2177  * @sf:         seq_file in which to write our mount options
2178  * @mnt:        vfs mount whose mount options to display
2179  *
2180  * Called by the VFS once for each mounted ntfs volume when someone reads
2181  * /proc/mounts in order to display the NTFS specific mount options of each
2182  * mount. The mount options of the vfs mount @mnt are written to the seq file
2183  * @sf and success is returned.
2184  */
2185 int ntfs_show_options(struct seq_file *sf, struct vfsmount *mnt)
2186 {
2187         ntfs_volume *vol = NTFS_SB(mnt->mnt_sb);
2188         int i;
2189
2190         seq_printf(sf, ",uid=%i", vol->uid);
2191         seq_printf(sf, ",gid=%i", vol->gid);
2192         if (vol->fmask == vol->dmask)
2193                 seq_printf(sf, ",umask=0%o", vol->fmask);
2194         else {
2195                 seq_printf(sf, ",fmask=0%o", vol->fmask);
2196                 seq_printf(sf, ",dmask=0%o", vol->dmask);
2197         }
2198         seq_printf(sf, ",nls=%s", vol->nls_map->charset);
2199         if (NVolCaseSensitive(vol))
2200                 seq_printf(sf, ",case_sensitive");
2201         if (NVolShowSystemFiles(vol))
2202                 seq_printf(sf, ",show_sys_files");
2203         for (i = 0; on_errors_arr[i].val; i++) {
2204                 if (on_errors_arr[i].val & vol->on_errors)
2205                         seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
2206         }
2207         seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
2208         return 0;
2209 }
2210
2211 #ifdef NTFS_RW
2212
2213 /**
2214  * ntfs_truncate - called when the i_size of an ntfs inode is changed
2215  * @vi:         inode for which the i_size was changed
2216  *
2217  * We don't support i_size changes yet.
2218  *
2219  * Called with ->i_sem held.
2220  */
2221 void ntfs_truncate(struct inode *vi)
2222 {
2223         // TODO: Implement...
2224         ntfs_warning(vi->i_sb, "Eeek: i_size may have changed! If you see "
2225                         "this right after a message from "
2226                         "ntfs_{prepare,commit}_{,nonresident_}write() then "
2227                         "just ignore it. Otherwise it is bad news.");
2228         // TODO: reset i_size now!
2229         return;
2230 }
2231
2232 /**
2233  * ntfs_setattr - called from notify_change() when an attribute is being changed
2234  * @dentry:     dentry whose attributes to change
2235  * @attr:       structure describing the attributes and the changes
2236  *
2237  * We have to trap VFS attempts to truncate the file described by @dentry as
2238  * soon as possible, because we do not implement changes in i_size yet. So we
2239  * abort all i_size changes here.
2240  *
2241  * Called with ->i_sem held.
2242  *
2243  * Basically this is a copy of generic notify_change() and inode_setattr()
2244  * functionality, except we intercept and abort changes in i_size.
2245  */
2246 int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
2247 {
2248         struct inode *vi;
2249         int err;
2250         unsigned int ia_valid = attr->ia_valid;
2251
2252         vi = dentry->d_inode;
2253
2254         err = inode_change_ok(vi, attr);
2255         if (err)
2256                 return err;
2257
2258         if ((ia_valid & ATTR_UID && attr->ia_uid != vi->i_uid) ||
2259                         (ia_valid & ATTR_GID && attr->ia_gid != vi->i_gid)) {
2260                 err = DQUOT_TRANSFER(vi, attr) ? -EDQUOT : 0;
2261                 if (err)
2262                         return err;
2263         }
2264
2265         lock_kernel();
2266
2267         if (ia_valid & ATTR_SIZE) {
2268                 ntfs_error(vi->i_sb, "Changes in i_size are not supported "
2269                                 "yet. Sorry.");
2270                 // TODO: Implement...
2271                 // err = vmtruncate(vi, attr->ia_size);
2272                 err = -EOPNOTSUPP;
2273                 if (err)
2274                         goto trunc_err;
2275         }
2276
2277         if (ia_valid & ATTR_UID)
2278                 vi->i_uid = attr->ia_uid;
2279         if (ia_valid & ATTR_GID)
2280                 vi->i_gid = attr->ia_gid;
2281         if (ia_valid & ATTR_ATIME)
2282                 vi->i_atime = attr->ia_atime;
2283         if (ia_valid & ATTR_MTIME)
2284                 vi->i_mtime = attr->ia_mtime;
2285         if (ia_valid & ATTR_CTIME)
2286                 vi->i_ctime = attr->ia_ctime;
2287         if (ia_valid & ATTR_MODE) {
2288                 vi->i_mode = attr->ia_mode;
2289                 if (!in_group_p(vi->i_gid) &&
2290                                 !capable(CAP_FSETID))
2291                         vi->i_mode &= ~S_ISGID;
2292         }
2293         mark_inode_dirty(vi);
2294
2295 trunc_err:
2296
2297         unlock_kernel();
2298
2299         return err;
2300 }
2301
2302 /**
2303  * ntfs_write_inode - write out a dirty inode
2304  * @vi:         inode to write out
2305  * @sync:       if true, write out synchronously
2306  *
2307  * Write out a dirty inode to disk including any extent inodes if present.
2308  *
2309  * If @sync is true, commit the inode to disk and wait for io completion.  This
2310  * is done using write_mft_record().
2311  *
2312  * If @sync is false, just schedule the write to happen but do not wait for i/o
2313  * completion.  In 2.6 kernels, scheduling usually happens just by virtue of
2314  * marking the page (and in this case mft record) dirty but we do not implement
2315  * this yet as write_mft_record() largely ignores the @sync parameter and
2316  * always performs synchronous writes.
2317  */
2318 void ntfs_write_inode(struct inode *vi, int sync)
2319 {
2320         ntfs_inode *ni = NTFS_I(vi);
2321 #if 0
2322         attr_search_context *ctx;
2323 #endif
2324         MFT_RECORD *m;
2325         int err = 0;
2326
2327         ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
2328                         vi->i_ino);
2329         /*
2330          * Dirty attribute inodes are written via their real inodes so just
2331          * clean them here.  TODO:  Take care of access time updates.
2332          */
2333         if (NInoAttr(ni)) {
2334                 NInoClearDirty(ni);
2335                 return;
2336         }
2337         /* Map, pin, and lock the mft record belonging to the inode. */
2338         m = map_mft_record(ni);
2339         if (unlikely(IS_ERR(m))) {
2340                 err = PTR_ERR(m);
2341                 goto err_out;
2342         }
2343 #if 0
2344         /* Obtain the standard information attribute. */
2345         ctx = get_attr_search_ctx(ni, m);
2346         if (unlikely(!ctx)) {
2347                 err = -ENOMEM;
2348                 goto unm_err_out;
2349         }
2350         if (unlikely(!lookup_attr(AT_STANDARD_INFORMATION, NULL, 0,
2351                         CASE_SENSITIVE, 0, NULL, 0, ctx))) {
2352                 put_attr_search_ctx(ctx);
2353                 err = -ENOENT;
2354                 goto unm_err_out;
2355         }
2356         // TODO:  Update the access times in the standard information attribute
2357         // which is now in ctx->attr.
2358         // - Probably want to have use sops->dirty_inode() to set a flag that
2359         //   we need to update the times here rather than having to blindly do
2360         //   it every time.  Or even don't do it here at all and do it in
2361         //   sops->dirty_inode() instead.  Problem with this would be that
2362         //   sops->dirty_inode() must be atomic under certain circumstances
2363         //   and mapping mft records and such like is not atomic.
2364         // - For atime updates also need to check whether they are enabled in
2365         //   the superblock flags.
2366         ntfs_warning(vi->i_sb, "Access time updates not implement yet.");
2367         /*
2368          * We just modified the mft record containing the standard information
2369          * attribute.  So need to mark the mft record dirty, too, but we do it
2370          * manually so that mark_inode_dirty() is not called again.
2371          * TODO:  Only do this if there was a change in any of the times!
2372          */
2373         if (!NInoTestSetDirty(ctx->ntfs_ino))
2374                 __set_page_dirty_nobuffers(ctx->ntfs_ino->page);
2375         put_attr_search_ctx(ctx);
2376 #endif
2377         /* Write this base mft record. */
2378         if (NInoDirty(ni))
2379                 err = write_mft_record(ni, m, sync);
2380         /* Write all attached extent mft records. */
2381         down(&ni->extent_lock);
2382         if (ni->nr_extents > 0) {
2383                 ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
2384                 int i;
2385
2386                 ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
2387                 for (i = 0; i < ni->nr_extents; i++) {
2388                         ntfs_inode *tni = extent_nis[i];
2389
2390                         if (NInoDirty(tni)) {
2391                                 MFT_RECORD *tm = map_mft_record(tni);
2392                                 int ret;
2393
2394                                 if (unlikely(IS_ERR(tm))) {
2395                                         if (!err || err == -ENOMEM)
2396                                                 err = PTR_ERR(tm);
2397                                         continue;
2398                                 }
2399                                 ret = write_mft_record(tni, tm, sync);
2400                                 unmap_mft_record(tni);
2401                                 if (unlikely(ret)) {
2402                                         if (!err || err == -ENOMEM)
2403                                                 err = ret;
2404                                 }
2405                         }
2406                 }
2407         }
2408         up(&ni->extent_lock);
2409         unmap_mft_record(ni);
2410         if (unlikely(err))
2411                 goto err_out;
2412         ntfs_debug("Done.");
2413         return;
2414 #if 0
2415 unm_err_out:
2416         unmap_mft_record(ni);
2417 #endif
2418 err_out:
2419         if (err == -ENOMEM) {
2420                 ntfs_warning(vi->i_sb, "Not enough memory to write inode.  "
2421                                 "Marking the inode dirty again, so the VFS "
2422                                 "retries later.");
2423                 mark_inode_dirty(vi);
2424         } else {
2425                 ntfs_error(vi->i_sb, "Failed (error code %i):  Marking inode "
2426                                 "as bad.  You should run chkdsk.", -err);
2427                 make_bad_inode(vi);
2428         }
2429         return;
2430 }
2431
2432 #endif /* NTFS_RW */