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