Merge with /usr/src/ntfs-2.6.git
[cascardo/linux.git] / fs / ntfs / super.c
1 /*
2  * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2005 Anton Altaparmakov
5  * Copyright (c) 2001,2002 Richard Russon
6  *
7  * This program/include file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program/include file is distributed in the hope that it will be
13  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in the main directory of the Linux-NTFS
19  * distribution in the file COPYING); if not, write to the Free Software
20  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/spinlock.h>
27 #include <linux/blkdev.h>       /* For bdev_hardsect_size(). */
28 #include <linux/backing-dev.h>
29 #include <linux/buffer_head.h>
30 #include <linux/vfs.h>
31 #include <linux/moduleparam.h>
32 #include <linux/smp_lock.h>
33
34 #include "sysctl.h"
35 #include "logfile.h"
36 #include "quota.h"
37 #include "dir.h"
38 #include "debug.h"
39 #include "index.h"
40 #include "aops.h"
41 #include "layout.h"
42 #include "malloc.h"
43 #include "ntfs.h"
44
45 /* Number of mounted filesystems which have compression enabled. */
46 static unsigned long ntfs_nr_compression_users;
47
48 /* A global default upcase table and a corresponding reference count. */
49 static ntfschar *default_upcase = NULL;
50 static unsigned long ntfs_nr_upcase_users = 0;
51
52 /* Error constants/strings used in inode.c::ntfs_show_options(). */
53 typedef enum {
54         /* One of these must be present, default is ON_ERRORS_CONTINUE. */
55         ON_ERRORS_PANIC                 = 0x01,
56         ON_ERRORS_REMOUNT_RO            = 0x02,
57         ON_ERRORS_CONTINUE              = 0x04,
58         /* Optional, can be combined with any of the above. */
59         ON_ERRORS_RECOVER               = 0x10,
60 } ON_ERRORS_ACTIONS;
61
62 const option_t on_errors_arr[] = {
63         { ON_ERRORS_PANIC,      "panic" },
64         { ON_ERRORS_REMOUNT_RO, "remount-ro", },
65         { ON_ERRORS_CONTINUE,   "continue", },
66         { ON_ERRORS_RECOVER,    "recover" },
67         { 0,                    NULL }
68 };
69
70 /**
71  * simple_getbool -
72  *
73  * Copied from old ntfs driver (which copied from vfat driver).
74  */
75 static int simple_getbool(char *s, BOOL *setval)
76 {
77         if (s) {
78                 if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
79                         *setval = TRUE;
80                 else if (!strcmp(s, "0") || !strcmp(s, "no") ||
81                                                         !strcmp(s, "false"))
82                         *setval = FALSE;
83                 else
84                         return 0;
85         } else
86                 *setval = TRUE;
87         return 1;
88 }
89
90 /**
91  * parse_options - parse the (re)mount options
92  * @vol:        ntfs volume
93  * @opt:        string containing the (re)mount options
94  *
95  * Parse the recognized options in @opt for the ntfs volume described by @vol.
96  */
97 static BOOL parse_options(ntfs_volume *vol, char *opt)
98 {
99         char *p, *v, *ov;
100         static char *utf8 = "utf8";
101         int errors = 0, sloppy = 0;
102         uid_t uid = (uid_t)-1;
103         gid_t gid = (gid_t)-1;
104         mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
105         int mft_zone_multiplier = -1, on_errors = -1;
106         int show_sys_files = -1, case_sensitive = -1, disable_sparse = -1;
107         struct nls_table *nls_map = NULL, *old_nls;
108
109         /* I am lazy... (-8 */
110 #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value)       \
111         if (!strcmp(p, option)) {                                       \
112                 if (!v || !*v)                                          \
113                         variable = default_value;                       \
114                 else {                                                  \
115                         variable = simple_strtoul(ov = v, &v, 0);       \
116                         if (*v)                                         \
117                                 goto needs_val;                         \
118                 }                                                       \
119         }
120 #define NTFS_GETOPT(option, variable)                                   \
121         if (!strcmp(p, option)) {                                       \
122                 if (!v || !*v)                                          \
123                         goto needs_arg;                                 \
124                 variable = simple_strtoul(ov = v, &v, 0);               \
125                 if (*v)                                                 \
126                         goto needs_val;                                 \
127         }
128 #define NTFS_GETOPT_BOOL(option, variable)                              \
129         if (!strcmp(p, option)) {                                       \
130                 BOOL val;                                               \
131                 if (!simple_getbool(v, &val))                           \
132                         goto needs_bool;                                \
133                 variable = val;                                         \
134         }
135 #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array)          \
136         if (!strcmp(p, option)) {                                       \
137                 int _i;                                                 \
138                 if (!v || !*v)                                          \
139                         goto needs_arg;                                 \
140                 ov = v;                                                 \
141                 if (variable == -1)                                     \
142                         variable = 0;                                   \
143                 for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
144                         if (!strcmp(opt_array[_i].str, v)) {            \
145                                 variable |= opt_array[_i].val;          \
146                                 break;                                  \
147                         }                                               \
148                 if (!opt_array[_i].str || !*opt_array[_i].str)          \
149                         goto needs_val;                                 \
150         }
151         if (!opt || !*opt)
152                 goto no_mount_options;
153         ntfs_debug("Entering with mount options string: %s", opt);
154         while ((p = strsep(&opt, ","))) {
155                 if ((v = strchr(p, '=')))
156                         *v++ = 0;
157                 NTFS_GETOPT("uid", uid)
158                 else NTFS_GETOPT("gid", gid)
159                 else NTFS_GETOPT("umask", fmask = dmask)
160                 else NTFS_GETOPT("fmask", fmask)
161                 else NTFS_GETOPT("dmask", dmask)
162                 else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
163                 else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
164                 else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
165                 else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
166                 else NTFS_GETOPT_BOOL("disable_sparse", disable_sparse)
167                 else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
168                                 on_errors_arr)
169                 else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
170                         ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
171                                         p);
172                 else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
173                         if (!strcmp(p, "iocharset"))
174                                 ntfs_warning(vol->sb, "Option iocharset is "
175                                                 "deprecated. Please use "
176                                                 "option nls=<charsetname> in "
177                                                 "the future.");
178                         if (!v || !*v)
179                                 goto needs_arg;
180 use_utf8:
181                         old_nls = nls_map;
182                         nls_map = load_nls(v);
183                         if (!nls_map) {
184                                 if (!old_nls) {
185                                         ntfs_error(vol->sb, "NLS character set "
186                                                         "%s not found.", v);
187                                         return FALSE;
188                                 }
189                                 ntfs_error(vol->sb, "NLS character set %s not "
190                                                 "found. Using previous one %s.",
191                                                 v, old_nls->charset);
192                                 nls_map = old_nls;
193                         } else /* nls_map */ {
194                                 if (old_nls)
195                                         unload_nls(old_nls);
196                         }
197                 } else if (!strcmp(p, "utf8")) {
198                         BOOL val = FALSE;
199                         ntfs_warning(vol->sb, "Option utf8 is no longer "
200                                    "supported, using option nls=utf8. Please "
201                                    "use option nls=utf8 in the future and "
202                                    "make sure utf8 is compiled either as a "
203                                    "module or into the kernel.");
204                         if (!v || !*v)
205                                 val = TRUE;
206                         else if (!simple_getbool(v, &val))
207                                 goto needs_bool;
208                         if (val) {
209                                 v = utf8;
210                                 goto use_utf8;
211                         }
212                 } else {
213                         ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
214                         if (errors < INT_MAX)
215                                 errors++;
216                 }
217 #undef NTFS_GETOPT_OPTIONS_ARRAY
218 #undef NTFS_GETOPT_BOOL
219 #undef NTFS_GETOPT
220 #undef NTFS_GETOPT_WITH_DEFAULT
221         }
222 no_mount_options:
223         if (errors && !sloppy)
224                 return FALSE;
225         if (sloppy)
226                 ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
227                                 "unrecognized mount option(s) and continuing.");
228         /* Keep this first! */
229         if (on_errors != -1) {
230                 if (!on_errors) {
231                         ntfs_error(vol->sb, "Invalid errors option argument "
232                                         "or bug in options parser.");
233                         return FALSE;
234                 }
235         }
236         if (nls_map) {
237                 if (vol->nls_map && vol->nls_map != nls_map) {
238                         ntfs_error(vol->sb, "Cannot change NLS character set "
239                                         "on remount.");
240                         return FALSE;
241                 } /* else (!vol->nls_map) */
242                 ntfs_debug("Using NLS character set %s.", nls_map->charset);
243                 vol->nls_map = nls_map;
244         } else /* (!nls_map) */ {
245                 if (!vol->nls_map) {
246                         vol->nls_map = load_nls_default();
247                         if (!vol->nls_map) {
248                                 ntfs_error(vol->sb, "Failed to load default "
249                                                 "NLS character set.");
250                                 return FALSE;
251                         }
252                         ntfs_debug("Using default NLS character set (%s).",
253                                         vol->nls_map->charset);
254                 }
255         }
256         if (mft_zone_multiplier != -1) {
257                 if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
258                                 mft_zone_multiplier) {
259                         ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
260                                         "on remount.");
261                         return FALSE;
262                 }
263                 if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
264                         ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
265                                         "Using default value, i.e. 1.");
266                         mft_zone_multiplier = 1;
267                 }
268                 vol->mft_zone_multiplier = mft_zone_multiplier;
269         }
270         if (!vol->mft_zone_multiplier)
271                 vol->mft_zone_multiplier = 1;
272         if (on_errors != -1)
273                 vol->on_errors = on_errors;
274         if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
275                 vol->on_errors |= ON_ERRORS_CONTINUE;
276         if (uid != (uid_t)-1)
277                 vol->uid = uid;
278         if (gid != (gid_t)-1)
279                 vol->gid = gid;
280         if (fmask != (mode_t)-1)
281                 vol->fmask = fmask;
282         if (dmask != (mode_t)-1)
283                 vol->dmask = dmask;
284         if (show_sys_files != -1) {
285                 if (show_sys_files)
286                         NVolSetShowSystemFiles(vol);
287                 else
288                         NVolClearShowSystemFiles(vol);
289         }
290         if (case_sensitive != -1) {
291                 if (case_sensitive)
292                         NVolSetCaseSensitive(vol);
293                 else
294                         NVolClearCaseSensitive(vol);
295         }
296         if (disable_sparse != -1) {
297                 if (disable_sparse)
298                         NVolClearSparseEnabled(vol);
299                 else {
300                         if (!NVolSparseEnabled(vol) &&
301                                         vol->major_ver && vol->major_ver < 3)
302                                 ntfs_warning(vol->sb, "Not enabling sparse "
303                                                 "support due to NTFS volume "
304                                                 "version %i.%i (need at least "
305                                                 "version 3.0).", vol->major_ver,
306                                                 vol->minor_ver);
307                         else
308                                 NVolSetSparseEnabled(vol);
309                 }
310         }
311         return TRUE;
312 needs_arg:
313         ntfs_error(vol->sb, "The %s option requires an argument.", p);
314         return FALSE;
315 needs_bool:
316         ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
317         return FALSE;
318 needs_val:
319         ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
320         return FALSE;
321 }
322
323 #ifdef NTFS_RW
324
325 /**
326  * ntfs_write_volume_flags - write new flags to the volume information flags
327  * @vol:        ntfs volume on which to modify the flags
328  * @flags:      new flags value for the volume information flags
329  *
330  * Internal function.  You probably want to use ntfs_{set,clear}_volume_flags()
331  * instead (see below).
332  *
333  * Replace the volume information flags on the volume @vol with the value
334  * supplied in @flags.  Note, this overwrites the volume information flags, so
335  * make sure to combine the flags you want to modify with the old flags and use
336  * the result when calling ntfs_write_volume_flags().
337  *
338  * Return 0 on success and -errno on error.
339  */
340 static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
341 {
342         ntfs_inode *ni = NTFS_I(vol->vol_ino);
343         MFT_RECORD *m;
344         VOLUME_INFORMATION *vi;
345         ntfs_attr_search_ctx *ctx;
346         int err;
347
348         ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
349                         le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
350         if (vol->vol_flags == flags)
351                 goto done;
352         BUG_ON(!ni);
353         m = map_mft_record(ni);
354         if (IS_ERR(m)) {
355                 err = PTR_ERR(m);
356                 goto err_out;
357         }
358         ctx = ntfs_attr_get_search_ctx(ni, m);
359         if (!ctx) {
360                 err = -ENOMEM;
361                 goto put_unm_err_out;
362         }
363         err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
364                         ctx);
365         if (err)
366                 goto put_unm_err_out;
367         vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
368                         le16_to_cpu(ctx->attr->data.resident.value_offset));
369         vol->vol_flags = vi->flags = flags;
370         flush_dcache_mft_record_page(ctx->ntfs_ino);
371         mark_mft_record_dirty(ctx->ntfs_ino);
372         ntfs_attr_put_search_ctx(ctx);
373         unmap_mft_record(ni);
374 done:
375         ntfs_debug("Done.");
376         return 0;
377 put_unm_err_out:
378         if (ctx)
379                 ntfs_attr_put_search_ctx(ctx);
380         unmap_mft_record(ni);
381 err_out:
382         ntfs_error(vol->sb, "Failed with error code %i.", -err);
383         return err;
384 }
385
386 /**
387  * ntfs_set_volume_flags - set bits in the volume information flags
388  * @vol:        ntfs volume on which to modify the flags
389  * @flags:      flags to set on the volume
390  *
391  * Set the bits in @flags in the volume information flags on the volume @vol.
392  *
393  * Return 0 on success and -errno on error.
394  */
395 static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
396 {
397         flags &= VOLUME_FLAGS_MASK;
398         return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
399 }
400
401 /**
402  * ntfs_clear_volume_flags - clear bits in the volume information flags
403  * @vol:        ntfs volume on which to modify the flags
404  * @flags:      flags to clear on the volume
405  *
406  * Clear the bits in @flags in the volume information flags on the volume @vol.
407  *
408  * Return 0 on success and -errno on error.
409  */
410 static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
411 {
412         flags &= VOLUME_FLAGS_MASK;
413         flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
414         return ntfs_write_volume_flags(vol, flags);
415 }
416
417 #endif /* NTFS_RW */
418
419 /**
420  * ntfs_remount - change the mount options of a mounted ntfs filesystem
421  * @sb:         superblock of mounted ntfs filesystem
422  * @flags:      remount flags
423  * @opt:        remount options string
424  *
425  * Change the mount options of an already mounted ntfs filesystem.
426  *
427  * NOTE:  The VFS sets the @sb->s_flags remount flags to @flags after
428  * ntfs_remount() returns successfully (i.e. returns 0).  Otherwise,
429  * @sb->s_flags are not changed.
430  */
431 static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
432 {
433         ntfs_volume *vol = NTFS_SB(sb);
434
435         ntfs_debug("Entering with remount options string: %s", opt);
436 #ifndef NTFS_RW
437         /* For read-only compiled driver, enforce all read-only flags. */
438         *flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
439 #else /* NTFS_RW */
440         /*
441          * For the read-write compiled driver, if we are remounting read-write,
442          * make sure there are no volume errors and that no unsupported volume
443          * flags are set.  Also, empty the logfile journal as it would become
444          * stale as soon as something is written to the volume and mark the
445          * volume dirty so that chkdsk is run if the volume is not umounted
446          * cleanly.  Finally, mark the quotas out of date so Windows rescans
447          * the volume on boot and updates them.
448          *
449          * When remounting read-only, mark the volume clean if no volume errors
450          * have occured.
451          */
452         if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
453                 static const char *es = ".  Cannot remount read-write.";
454
455                 /* Remounting read-write. */
456                 if (NVolErrors(vol)) {
457                         ntfs_error(sb, "Volume has errors and is read-only%s",
458                                         es);
459                         return -EROFS;
460                 }
461                 if (vol->vol_flags & VOLUME_IS_DIRTY) {
462                         ntfs_error(sb, "Volume is dirty and read-only%s", es);
463                         return -EROFS;
464                 }
465                 if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
466                         ntfs_error(sb, "Volume has unsupported flags set and "
467                                         "is read-only%s", es);
468                         return -EROFS;
469                 }
470                 if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
471                         ntfs_error(sb, "Failed to set dirty bit in volume "
472                                         "information flags%s", es);
473                         return -EROFS;
474                 }
475 #if 0
476                 // TODO: Enable this code once we start modifying anything that
477                 //       is different between NTFS 1.2 and 3.x...
478                 /* Set NT4 compatibility flag on newer NTFS version volumes. */
479                 if ((vol->major_ver > 1)) {
480                         if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
481                                 ntfs_error(sb, "Failed to set NT4 "
482                                                 "compatibility flag%s", es);
483                                 NVolSetErrors(vol);
484                                 return -EROFS;
485                         }
486                 }
487 #endif
488                 if (!ntfs_empty_logfile(vol->logfile_ino)) {
489                         ntfs_error(sb, "Failed to empty journal $LogFile%s",
490                                         es);
491                         NVolSetErrors(vol);
492                         return -EROFS;
493                 }
494                 if (!ntfs_mark_quotas_out_of_date(vol)) {
495                         ntfs_error(sb, "Failed to mark quotas out of date%s",
496                                         es);
497                         NVolSetErrors(vol);
498                         return -EROFS;
499                 }
500         } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
501                 /* Remounting read-only. */
502                 if (!NVolErrors(vol)) {
503                         if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
504                                 ntfs_warning(sb, "Failed to clear dirty bit "
505                                                 "in volume information "
506                                                 "flags.  Run chkdsk.");
507                 }
508         }
509 #endif /* NTFS_RW */
510
511         // TODO: Deal with *flags.
512
513         if (!parse_options(vol, opt))
514                 return -EINVAL;
515         ntfs_debug("Done.");
516         return 0;
517 }
518
519 /**
520  * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
521  * @sb:         Super block of the device to which @b belongs.
522  * @b:          Boot sector of device @sb to check.
523  * @silent:     If TRUE, all output will be silenced.
524  *
525  * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
526  * sector. Returns TRUE if it is valid and FALSE if not.
527  *
528  * @sb is only needed for warning/error output, i.e. it can be NULL when silent
529  * is TRUE.
530  */
531 static BOOL is_boot_sector_ntfs(const struct super_block *sb,
532                 const NTFS_BOOT_SECTOR *b, const BOOL silent)
533 {
534         /*
535          * Check that checksum == sum of u32 values from b to the checksum
536          * field.  If checksum is zero, no checking is done.  We will work when
537          * the checksum test fails, since some utilities update the boot sector
538          * ignoring the checksum which leaves the checksum out-of-date.  We
539          * report a warning if this is the case.
540          */
541         if ((void*)b < (void*)&b->checksum && b->checksum && !silent) {
542                 le32 *u;
543                 u32 i;
544
545                 for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
546                         i += le32_to_cpup(u);
547                 if (le32_to_cpu(b->checksum) != i)
548                         ntfs_warning(sb, "Invalid boot sector checksum.");
549         }
550         /* Check OEMidentifier is "NTFS    " */
551         if (b->oem_id != magicNTFS)
552                 goto not_ntfs;
553         /* Check bytes per sector value is between 256 and 4096. */
554         if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
555                         le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
556                 goto not_ntfs;
557         /* Check sectors per cluster value is valid. */
558         switch (b->bpb.sectors_per_cluster) {
559         case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
560                 break;
561         default:
562                 goto not_ntfs;
563         }
564         /* Check the cluster size is not above the maximum (64kiB). */
565         if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
566                         b->bpb.sectors_per_cluster > NTFS_MAX_CLUSTER_SIZE)
567                 goto not_ntfs;
568         /* Check reserved/unused fields are really zero. */
569         if (le16_to_cpu(b->bpb.reserved_sectors) ||
570                         le16_to_cpu(b->bpb.root_entries) ||
571                         le16_to_cpu(b->bpb.sectors) ||
572                         le16_to_cpu(b->bpb.sectors_per_fat) ||
573                         le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
574                 goto not_ntfs;
575         /* Check clusters per file mft record value is valid. */
576         if ((u8)b->clusters_per_mft_record < 0xe1 ||
577                         (u8)b->clusters_per_mft_record > 0xf7)
578                 switch (b->clusters_per_mft_record) {
579                 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
580                         break;
581                 default:
582                         goto not_ntfs;
583                 }
584         /* Check clusters per index block value is valid. */
585         if ((u8)b->clusters_per_index_record < 0xe1 ||
586                         (u8)b->clusters_per_index_record > 0xf7)
587                 switch (b->clusters_per_index_record) {
588                 case 1: case 2: case 4: case 8: case 16: case 32: case 64:
589                         break;
590                 default:
591                         goto not_ntfs;
592                 }
593         /*
594          * Check for valid end of sector marker. We will work without it, but
595          * many BIOSes will refuse to boot from a bootsector if the magic is
596          * incorrect, so we emit a warning.
597          */
598         if (!silent && b->end_of_sector_marker != const_cpu_to_le16(0xaa55))
599                 ntfs_warning(sb, "Invalid end of sector marker.");
600         return TRUE;
601 not_ntfs:
602         return FALSE;
603 }
604
605 /**
606  * read_ntfs_boot_sector - read the NTFS boot sector of a device
607  * @sb:         super block of device to read the boot sector from
608  * @silent:     if true, suppress all output
609  *
610  * Reads the boot sector from the device and validates it. If that fails, tries
611  * to read the backup boot sector, first from the end of the device a-la NT4 and
612  * later and then from the middle of the device a-la NT3.51 and before.
613  *
614  * If a valid boot sector is found but it is not the primary boot sector, we
615  * repair the primary boot sector silently (unless the device is read-only or
616  * the primary boot sector is not accessible).
617  *
618  * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
619  * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
620  * to their respective values.
621  *
622  * Return the unlocked buffer head containing the boot sector or NULL on error.
623  */
624 static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
625                 const int silent)
626 {
627         const char *read_err_str = "Unable to read %s boot sector.";
628         struct buffer_head *bh_primary, *bh_backup;
629         long nr_blocks = NTFS_SB(sb)->nr_blocks;
630
631         /* Try to read primary boot sector. */
632         if ((bh_primary = sb_bread(sb, 0))) {
633                 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
634                                 bh_primary->b_data, silent))
635                         return bh_primary;
636                 if (!silent)
637                         ntfs_error(sb, "Primary boot sector is invalid.");
638         } else if (!silent)
639                 ntfs_error(sb, read_err_str, "primary");
640         if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
641                 if (bh_primary)
642                         brelse(bh_primary);
643                 if (!silent)
644                         ntfs_error(sb, "Mount option errors=recover not used. "
645                                         "Aborting without trying to recover.");
646                 return NULL;
647         }
648         /* Try to read NT4+ backup boot sector. */
649         if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
650                 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
651                                 bh_backup->b_data, silent))
652                         goto hotfix_primary_boot_sector;
653                 brelse(bh_backup);
654         } else if (!silent)
655                 ntfs_error(sb, read_err_str, "backup");
656         /* Try to read NT3.51- backup boot sector. */
657         if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
658                 if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
659                                 bh_backup->b_data, silent))
660                         goto hotfix_primary_boot_sector;
661                 if (!silent)
662                         ntfs_error(sb, "Could not find a valid backup boot "
663                                         "sector.");
664                 brelse(bh_backup);
665         } else if (!silent)
666                 ntfs_error(sb, read_err_str, "backup");
667         /* We failed. Cleanup and return. */
668         if (bh_primary)
669                 brelse(bh_primary);
670         return NULL;
671 hotfix_primary_boot_sector:
672         if (bh_primary) {
673                 /*
674                  * If we managed to read sector zero and the volume is not
675                  * read-only, copy the found, valid backup boot sector to the
676                  * primary boot sector.
677                  */
678                 if (!(sb->s_flags & MS_RDONLY)) {
679                         ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
680                                         "boot sector from backup copy.");
681                         memcpy(bh_primary->b_data, bh_backup->b_data,
682                                         sb->s_blocksize);
683                         mark_buffer_dirty(bh_primary);
684                         sync_dirty_buffer(bh_primary);
685                         if (buffer_uptodate(bh_primary)) {
686                                 brelse(bh_backup);
687                                 return bh_primary;
688                         }
689                         ntfs_error(sb, "Hot-fix: Device write error while "
690                                         "recovering primary boot sector.");
691                 } else {
692                         ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
693                                         "sector failed: Read-only mount.");
694                 }
695                 brelse(bh_primary);
696         }
697         ntfs_warning(sb, "Using backup boot sector.");
698         return bh_backup;
699 }
700
701 /**
702  * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
703  * @vol:        volume structure to initialise with data from boot sector
704  * @b:          boot sector to parse
705  *
706  * Parse the ntfs boot sector @b and store all imporant information therein in
707  * the ntfs super block @vol.  Return TRUE on success and FALSE on error.
708  */
709 static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
710 {
711         unsigned int sectors_per_cluster_bits, nr_hidden_sects;
712         int clusters_per_mft_record, clusters_per_index_record;
713         s64 ll;
714
715         vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
716         vol->sector_size_bits = ffs(vol->sector_size) - 1;
717         ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
718                         vol->sector_size);
719         ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
720                         vol->sector_size_bits);
721         if (vol->sector_size != vol->sb->s_blocksize)
722                 ntfs_warning(vol->sb, "The boot sector indicates a sector size "
723                                 "different from the device sector size.");
724         ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
725         sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
726         ntfs_debug("sectors_per_cluster_bits = 0x%x",
727                         sectors_per_cluster_bits);
728         nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
729         ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
730         vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
731         vol->cluster_size_mask = vol->cluster_size - 1;
732         vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
733         ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
734                         vol->cluster_size);
735         ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
736         ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
737                         vol->cluster_size_bits, vol->cluster_size_bits);
738         if (vol->sector_size > vol->cluster_size) {
739                 ntfs_error(vol->sb, "Sector sizes above the cluster size are "
740                                 "not supported.  Sorry.");
741                 return FALSE;
742         }
743         if (vol->sb->s_blocksize > vol->cluster_size) {
744                 ntfs_error(vol->sb, "Cluster sizes smaller than the device "
745                                 "sector size are not supported.  Sorry.");
746                 return FALSE;
747         }
748         clusters_per_mft_record = b->clusters_per_mft_record;
749         ntfs_debug("clusters_per_mft_record = %i (0x%x)",
750                         clusters_per_mft_record, clusters_per_mft_record);
751         if (clusters_per_mft_record > 0)
752                 vol->mft_record_size = vol->cluster_size <<
753                                 (ffs(clusters_per_mft_record) - 1);
754         else
755                 /*
756                  * When mft_record_size < cluster_size, clusters_per_mft_record
757                  * = -log2(mft_record_size) bytes. mft_record_size normaly is
758                  * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
759                  */
760                 vol->mft_record_size = 1 << -clusters_per_mft_record;
761         vol->mft_record_size_mask = vol->mft_record_size - 1;
762         vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
763         ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
764                         vol->mft_record_size);
765         ntfs_debug("vol->mft_record_size_mask = 0x%x",
766                         vol->mft_record_size_mask);
767         ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
768                         vol->mft_record_size_bits, vol->mft_record_size_bits);
769         /*
770          * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
771          * we store $MFT/$DATA, the table of mft records in the page cache.
772          */
773         if (vol->mft_record_size > PAGE_CACHE_SIZE) {
774                 ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
775                                 "page cache size on your system %lu (0x%lx).  "
776                                 "This is not supported.  Sorry.",
777                                 vol->mft_record_size, vol->mft_record_size,
778                                 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
779                 return FALSE;
780         }
781         clusters_per_index_record = b->clusters_per_index_record;
782         ntfs_debug("clusters_per_index_record = %i (0x%x)",
783                         clusters_per_index_record, clusters_per_index_record);
784         if (clusters_per_index_record > 0)
785                 vol->index_record_size = vol->cluster_size <<
786                                 (ffs(clusters_per_index_record) - 1);
787         else
788                 /*
789                  * When index_record_size < cluster_size,
790                  * clusters_per_index_record = -log2(index_record_size) bytes.
791                  * index_record_size normaly equals 4096 bytes, which is
792                  * encoded as 0xF4 (-12 in decimal).
793                  */
794                 vol->index_record_size = 1 << -clusters_per_index_record;
795         vol->index_record_size_mask = vol->index_record_size - 1;
796         vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
797         ntfs_debug("vol->index_record_size = %i (0x%x)",
798                         vol->index_record_size, vol->index_record_size);
799         ntfs_debug("vol->index_record_size_mask = 0x%x",
800                         vol->index_record_size_mask);
801         ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
802                         vol->index_record_size_bits,
803                         vol->index_record_size_bits);
804         /*
805          * Get the size of the volume in clusters and check for 64-bit-ness.
806          * Windows currently only uses 32 bits to save the clusters so we do
807          * the same as it is much faster on 32-bit CPUs.
808          */
809         ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
810         if ((u64)ll >= 1ULL << 32) {
811                 ntfs_error(vol->sb, "Cannot handle 64-bit clusters.  Sorry.");
812                 return FALSE;
813         }
814         vol->nr_clusters = ll;
815         ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
816         /*
817          * On an architecture where unsigned long is 32-bits, we restrict the
818          * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
819          * will hopefully optimize the whole check away.
820          */
821         if (sizeof(unsigned long) < 8) {
822                 if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
823                         ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
824                                         "large for this architecture.  "
825                                         "Maximum supported is 2TiB.  Sorry.",
826                                         (unsigned long long)ll >> (40 -
827                                         vol->cluster_size_bits));
828                         return FALSE;
829                 }
830         }
831         ll = sle64_to_cpu(b->mft_lcn);
832         if (ll >= vol->nr_clusters) {
833                 ntfs_error(vol->sb, "MFT LCN is beyond end of volume.  Weird.");
834                 return FALSE;
835         }
836         vol->mft_lcn = ll;
837         ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
838         ll = sle64_to_cpu(b->mftmirr_lcn);
839         if (ll >= vol->nr_clusters) {
840                 ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume.  "
841                                 "Weird.");
842                 return FALSE;
843         }
844         vol->mftmirr_lcn = ll;
845         ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
846 #ifdef NTFS_RW
847         /*
848          * Work out the size of the mft mirror in number of mft records. If the
849          * cluster size is less than or equal to the size taken by four mft
850          * records, the mft mirror stores the first four mft records. If the
851          * cluster size is bigger than the size taken by four mft records, the
852          * mft mirror contains as many mft records as will fit into one
853          * cluster.
854          */
855         if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
856                 vol->mftmirr_size = 4;
857         else
858                 vol->mftmirr_size = vol->cluster_size >>
859                                 vol->mft_record_size_bits;
860         ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
861 #endif /* NTFS_RW */
862         vol->serial_no = le64_to_cpu(b->volume_serial_number);
863         ntfs_debug("vol->serial_no = 0x%llx",
864                         (unsigned long long)vol->serial_no);
865         return TRUE;
866 }
867
868 /**
869  * ntfs_setup_allocators - initialize the cluster and mft allocators
870  * @vol:        volume structure for which to setup the allocators
871  *
872  * Setup the cluster (lcn) and mft allocators to the starting values.
873  */
874 static void ntfs_setup_allocators(ntfs_volume *vol)
875 {
876 #ifdef NTFS_RW
877         LCN mft_zone_size, mft_lcn;
878 #endif /* NTFS_RW */
879
880         ntfs_debug("vol->mft_zone_multiplier = 0x%x",
881                         vol->mft_zone_multiplier);
882 #ifdef NTFS_RW
883         /* Determine the size of the MFT zone. */
884         mft_zone_size = vol->nr_clusters;
885         switch (vol->mft_zone_multiplier) {  /* % of volume size in clusters */
886         case 4:
887                 mft_zone_size >>= 1;                    /* 50%   */
888                 break;
889         case 3:
890                 mft_zone_size = (mft_zone_size +
891                                 (mft_zone_size >> 1)) >> 2;     /* 37.5% */
892                 break;
893         case 2:
894                 mft_zone_size >>= 2;                    /* 25%   */
895                 break;
896         /* case 1: */
897         default:
898                 mft_zone_size >>= 3;                    /* 12.5% */
899                 break;
900         }
901         /* Setup the mft zone. */
902         vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
903         ntfs_debug("vol->mft_zone_pos = 0x%llx",
904                         (unsigned long long)vol->mft_zone_pos);
905         /*
906          * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
907          * source) and if the actual mft_lcn is in the expected place or even
908          * further to the front of the volume, extend the mft_zone to cover the
909          * beginning of the volume as well.  This is in order to protect the
910          * area reserved for the mft bitmap as well within the mft_zone itself.
911          * On non-standard volumes we do not protect it as the overhead would
912          * be higher than the speed increase we would get by doing it.
913          */
914         mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
915         if (mft_lcn * vol->cluster_size < 16 * 1024)
916                 mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
917                                 vol->cluster_size;
918         if (vol->mft_zone_start <= mft_lcn)
919                 vol->mft_zone_start = 0;
920         ntfs_debug("vol->mft_zone_start = 0x%llx",
921                         (unsigned long long)vol->mft_zone_start);
922         /*
923          * Need to cap the mft zone on non-standard volumes so that it does
924          * not point outside the boundaries of the volume.  We do this by
925          * halving the zone size until we are inside the volume.
926          */
927         vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
928         while (vol->mft_zone_end >= vol->nr_clusters) {
929                 mft_zone_size >>= 1;
930                 vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
931         }
932         ntfs_debug("vol->mft_zone_end = 0x%llx",
933                         (unsigned long long)vol->mft_zone_end);
934         /*
935          * Set the current position within each data zone to the start of the
936          * respective zone.
937          */
938         vol->data1_zone_pos = vol->mft_zone_end;
939         ntfs_debug("vol->data1_zone_pos = 0x%llx",
940                         (unsigned long long)vol->data1_zone_pos);
941         vol->data2_zone_pos = 0;
942         ntfs_debug("vol->data2_zone_pos = 0x%llx",
943                         (unsigned long long)vol->data2_zone_pos);
944
945         /* Set the mft data allocation position to mft record 24. */
946         vol->mft_data_pos = 24;
947         ntfs_debug("vol->mft_data_pos = 0x%llx",
948                         (unsigned long long)vol->mft_data_pos);
949 #endif /* NTFS_RW */
950 }
951
952 #ifdef NTFS_RW
953
954 /**
955  * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
956  * @vol:        ntfs super block describing device whose mft mirror to load
957  *
958  * Return TRUE on success or FALSE on error.
959  */
960 static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
961 {
962         struct inode *tmp_ino;
963         ntfs_inode *tmp_ni;
964
965         ntfs_debug("Entering.");
966         /* Get mft mirror inode. */
967         tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
968         if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
969                 if (!IS_ERR(tmp_ino))
970                         iput(tmp_ino);
971                 /* Caller will display error message. */
972                 return FALSE;
973         }
974         /*
975          * Re-initialize some specifics about $MFTMirr's inode as
976          * ntfs_read_inode() will have set up the default ones.
977          */
978         /* Set uid and gid to root. */
979         tmp_ino->i_uid = tmp_ino->i_gid = 0;
980         /* Regular file.  No access for anyone. */
981         tmp_ino->i_mode = S_IFREG;
982         /* No VFS initiated operations allowed for $MFTMirr. */
983         tmp_ino->i_op = &ntfs_empty_inode_ops;
984         tmp_ino->i_fop = &ntfs_empty_file_ops;
985         /* Put in our special address space operations. */
986         tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
987         tmp_ni = NTFS_I(tmp_ino);
988         /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
989         NInoSetMstProtected(tmp_ni);
990         NInoSetSparseDisabled(tmp_ni);
991         /*
992          * Set up our little cheat allowing us to reuse the async read io
993          * completion handler for directories.
994          */
995         tmp_ni->itype.index.block_size = vol->mft_record_size;
996         tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
997         vol->mftmirr_ino = tmp_ino;
998         ntfs_debug("Done.");
999         return TRUE;
1000 }
1001
1002 /**
1003  * check_mft_mirror - compare contents of the mft mirror with the mft
1004  * @vol:        ntfs super block describing device whose mft mirror to check
1005  *
1006  * Return TRUE on success or FALSE on error.
1007  *
1008  * Note, this function also results in the mft mirror runlist being completely
1009  * mapped into memory.  The mft mirror write code requires this and will BUG()
1010  * should it find an unmapped runlist element.
1011  */
1012 static BOOL check_mft_mirror(ntfs_volume *vol)
1013 {
1014         struct super_block *sb = vol->sb;
1015         ntfs_inode *mirr_ni;
1016         struct page *mft_page, *mirr_page;
1017         u8 *kmft, *kmirr;
1018         runlist_element *rl, rl2[2];
1019         pgoff_t index;
1020         int mrecs_per_page, i;
1021
1022         ntfs_debug("Entering.");
1023         /* Compare contents of $MFT and $MFTMirr. */
1024         mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
1025         BUG_ON(!mrecs_per_page);
1026         BUG_ON(!vol->mftmirr_size);
1027         mft_page = mirr_page = NULL;
1028         kmft = kmirr = NULL;
1029         index = i = 0;
1030         do {
1031                 u32 bytes;
1032
1033                 /* Switch pages if necessary. */
1034                 if (!(i % mrecs_per_page)) {
1035                         if (index) {
1036                                 ntfs_unmap_page(mft_page);
1037                                 ntfs_unmap_page(mirr_page);
1038                         }
1039                         /* Get the $MFT page. */
1040                         mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
1041                                         index);
1042                         if (IS_ERR(mft_page)) {
1043                                 ntfs_error(sb, "Failed to read $MFT.");
1044                                 return FALSE;
1045                         }
1046                         kmft = page_address(mft_page);
1047                         /* Get the $MFTMirr page. */
1048                         mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
1049                                         index);
1050                         if (IS_ERR(mirr_page)) {
1051                                 ntfs_error(sb, "Failed to read $MFTMirr.");
1052                                 goto mft_unmap_out;
1053                         }
1054                         kmirr = page_address(mirr_page);
1055                         ++index;
1056                 }
1057                 /* Make sure the record is ok. */
1058                 if (ntfs_is_baad_recordp((le32*)kmft)) {
1059                         ntfs_error(sb, "Incomplete multi sector transfer "
1060                                         "detected in mft record %i.", i);
1061 mm_unmap_out:
1062                         ntfs_unmap_page(mirr_page);
1063 mft_unmap_out:
1064                         ntfs_unmap_page(mft_page);
1065                         return FALSE;
1066                 }
1067                 if (ntfs_is_baad_recordp((le32*)kmirr)) {
1068                         ntfs_error(sb, "Incomplete multi sector transfer "
1069                                         "detected in mft mirror record %i.", i);
1070                         goto mm_unmap_out;
1071                 }
1072                 /* Get the amount of data in the current record. */
1073                 bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
1074                 if (!bytes || bytes > vol->mft_record_size) {
1075                         bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
1076                         if (!bytes || bytes > vol->mft_record_size)
1077                                 bytes = vol->mft_record_size;
1078                 }
1079                 /* Compare the two records. */
1080                 if (memcmp(kmft, kmirr, bytes)) {
1081                         ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
1082                                         "match.  Run ntfsfix or chkdsk.", i);
1083                         goto mm_unmap_out;
1084                 }
1085                 kmft += vol->mft_record_size;
1086                 kmirr += vol->mft_record_size;
1087         } while (++i < vol->mftmirr_size);
1088         /* Release the last pages. */
1089         ntfs_unmap_page(mft_page);
1090         ntfs_unmap_page(mirr_page);
1091
1092         /* Construct the mft mirror runlist by hand. */
1093         rl2[0].vcn = 0;
1094         rl2[0].lcn = vol->mftmirr_lcn;
1095         rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
1096                         vol->cluster_size - 1) / vol->cluster_size;
1097         rl2[1].vcn = rl2[0].length;
1098         rl2[1].lcn = LCN_ENOENT;
1099         rl2[1].length = 0;
1100         /*
1101          * Because we have just read all of the mft mirror, we know we have
1102          * mapped the full runlist for it.
1103          */
1104         mirr_ni = NTFS_I(vol->mftmirr_ino);
1105         down_read(&mirr_ni->runlist.lock);
1106         rl = mirr_ni->runlist.rl;
1107         /* Compare the two runlists.  They must be identical. */
1108         i = 0;
1109         do {
1110                 if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
1111                                 rl2[i].length != rl[i].length) {
1112                         ntfs_error(sb, "$MFTMirr location mismatch.  "
1113                                         "Run chkdsk.");
1114                         up_read(&mirr_ni->runlist.lock);
1115                         return FALSE;
1116                 }
1117         } while (rl2[i++].length);
1118         up_read(&mirr_ni->runlist.lock);
1119         ntfs_debug("Done.");
1120         return TRUE;
1121 }
1122
1123 /**
1124  * load_and_check_logfile - load and check the logfile inode for a volume
1125  * @vol:        ntfs super block describing device whose logfile to load
1126  *
1127  * Return TRUE on success or FALSE on error.
1128  */
1129 static BOOL load_and_check_logfile(ntfs_volume *vol)
1130 {
1131         struct inode *tmp_ino;
1132
1133         ntfs_debug("Entering.");
1134         tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
1135         if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1136                 if (!IS_ERR(tmp_ino))
1137                         iput(tmp_ino);
1138                 /* Caller will display error message. */
1139                 return FALSE;
1140         }
1141         if (!ntfs_check_logfile(tmp_ino)) {
1142                 iput(tmp_ino);
1143                 /* ntfs_check_logfile() will have displayed error output. */
1144                 return FALSE;
1145         }
1146         NInoSetSparseDisabled(NTFS_I(tmp_ino));
1147         vol->logfile_ino = tmp_ino;
1148         ntfs_debug("Done.");
1149         return TRUE;
1150 }
1151
1152 /**
1153  * load_and_init_quota - load and setup the quota file for a volume if present
1154  * @vol:        ntfs super block describing device whose quota file to load
1155  *
1156  * Return TRUE on success or FALSE on error.  If $Quota is not present, we
1157  * leave vol->quota_ino as NULL and return success.
1158  */
1159 static BOOL load_and_init_quota(ntfs_volume *vol)
1160 {
1161         MFT_REF mref;
1162         struct inode *tmp_ino;
1163         ntfs_name *name = NULL;
1164         static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
1165                         const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
1166                         const_cpu_to_le16('o'), const_cpu_to_le16('t'),
1167                         const_cpu_to_le16('a'), 0 };
1168         static ntfschar Q[3] = { const_cpu_to_le16('$'),
1169                         const_cpu_to_le16('Q'), 0 };
1170
1171         ntfs_debug("Entering.");
1172         /*
1173          * Find the inode number for the quota file by looking up the filename
1174          * $Quota in the extended system files directory $Extend.
1175          */
1176         down(&vol->extend_ino->i_sem);
1177         mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
1178                         &name);
1179         up(&vol->extend_ino->i_sem);
1180         if (IS_ERR_MREF(mref)) {
1181                 /*
1182                  * If the file does not exist, quotas are disabled and have
1183                  * never been enabled on this volume, just return success.
1184                  */
1185                 if (MREF_ERR(mref) == -ENOENT) {
1186                         ntfs_debug("$Quota not present.  Volume does not have "
1187                                         "quotas enabled.");
1188                         /*
1189                          * No need to try to set quotas out of date if they are
1190                          * not enabled.
1191                          */
1192                         NVolSetQuotaOutOfDate(vol);
1193                         return TRUE;
1194                 }
1195                 /* A real error occured. */
1196                 ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
1197                 return FALSE;
1198         }
1199         /* We do not care for the type of match that was found. */
1200         kfree(name);
1201         /* Get the inode. */
1202         tmp_ino = ntfs_iget(vol->sb, MREF(mref));
1203         if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
1204                 if (!IS_ERR(tmp_ino))
1205                         iput(tmp_ino);
1206                 ntfs_error(vol->sb, "Failed to load $Quota.");
1207                 return FALSE;
1208         }
1209         vol->quota_ino = tmp_ino;
1210         /* Get the $Q index allocation attribute. */
1211         tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
1212         if (IS_ERR(tmp_ino)) {
1213                 ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
1214                 return FALSE;
1215         }
1216         vol->quota_q_ino = tmp_ino;
1217         ntfs_debug("Done.");
1218         return TRUE;
1219 }
1220
1221 /**
1222  * load_and_init_attrdef - load the attribute definitions table for a volume
1223  * @vol:        ntfs super block describing device whose attrdef to load
1224  *
1225  * Return TRUE on success or FALSE on error.
1226  */
1227 static BOOL load_and_init_attrdef(ntfs_volume *vol)
1228 {
1229         loff_t i_size;
1230         struct super_block *sb = vol->sb;
1231         struct inode *ino;
1232         struct page *page;
1233         pgoff_t index, max_index;
1234         unsigned int size;
1235
1236         ntfs_debug("Entering.");
1237         /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
1238         ino = ntfs_iget(sb, FILE_AttrDef);
1239         if (IS_ERR(ino) || is_bad_inode(ino)) {
1240                 if (!IS_ERR(ino))
1241                         iput(ino);
1242                 goto failed;
1243         }
1244         NInoSetSparseDisabled(NTFS_I(ino));
1245         /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
1246         i_size = i_size_read(ino);
1247         if (i_size <= 0 || i_size > 0x7fffffff)
1248                 goto iput_failed;
1249         vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(i_size);
1250         if (!vol->attrdef)
1251                 goto iput_failed;
1252         index = 0;
1253         max_index = i_size >> PAGE_CACHE_SHIFT;
1254         size = PAGE_CACHE_SIZE;
1255         while (index < max_index) {
1256                 /* Read the attrdef table and copy it into the linear buffer. */
1257 read_partial_attrdef_page:
1258                 page = ntfs_map_page(ino->i_mapping, index);
1259                 if (IS_ERR(page))
1260                         goto free_iput_failed;
1261                 memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
1262                                 page_address(page), size);
1263                 ntfs_unmap_page(page);
1264         };
1265         if (size == PAGE_CACHE_SIZE) {
1266                 size = i_size & ~PAGE_CACHE_MASK;
1267                 if (size)
1268                         goto read_partial_attrdef_page;
1269         }
1270         vol->attrdef_size = i_size;
1271         ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
1272         iput(ino);
1273         return TRUE;
1274 free_iput_failed:
1275         ntfs_free(vol->attrdef);
1276         vol->attrdef = NULL;
1277 iput_failed:
1278         iput(ino);
1279 failed:
1280         ntfs_error(sb, "Failed to initialize attribute definition table.");
1281         return FALSE;
1282 }
1283
1284 #endif /* NTFS_RW */
1285
1286 /**
1287  * load_and_init_upcase - load the upcase table for an ntfs volume
1288  * @vol:        ntfs super block describing device whose upcase to load
1289  *
1290  * Return TRUE on success or FALSE on error.
1291  */
1292 static BOOL load_and_init_upcase(ntfs_volume *vol)
1293 {
1294         loff_t i_size;
1295         struct super_block *sb = vol->sb;
1296         struct inode *ino;
1297         struct page *page;
1298         pgoff_t index, max_index;
1299         unsigned int size;
1300         int i, max;
1301
1302         ntfs_debug("Entering.");
1303         /* Read upcase table and setup vol->upcase and vol->upcase_len. */
1304         ino = ntfs_iget(sb, FILE_UpCase);
1305         if (IS_ERR(ino) || is_bad_inode(ino)) {
1306                 if (!IS_ERR(ino))
1307                         iput(ino);
1308                 goto upcase_failed;
1309         }
1310         /*
1311          * The upcase size must not be above 64k Unicode characters, must not
1312          * be zero and must be a multiple of sizeof(ntfschar).
1313          */
1314         i_size = i_size_read(ino);
1315         if (!i_size || i_size & (sizeof(ntfschar) - 1) ||
1316                         i_size > 64ULL * 1024 * sizeof(ntfschar))
1317                 goto iput_upcase_failed;
1318         vol->upcase = (ntfschar*)ntfs_malloc_nofs(i_size);
1319         if (!vol->upcase)
1320                 goto iput_upcase_failed;
1321         index = 0;
1322         max_index = i_size >> PAGE_CACHE_SHIFT;
1323         size = PAGE_CACHE_SIZE;
1324         while (index < max_index) {
1325                 /* Read the upcase table and copy it into the linear buffer. */
1326 read_partial_upcase_page:
1327                 page = ntfs_map_page(ino->i_mapping, index);
1328                 if (IS_ERR(page))
1329                         goto iput_upcase_failed;
1330                 memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
1331                                 page_address(page), size);
1332                 ntfs_unmap_page(page);
1333         };
1334         if (size == PAGE_CACHE_SIZE) {
1335                 size = i_size & ~PAGE_CACHE_MASK;
1336                 if (size)
1337                         goto read_partial_upcase_page;
1338         }
1339         vol->upcase_len = i_size >> UCHAR_T_SIZE_BITS;
1340         ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
1341                         i_size, 64 * 1024 * sizeof(ntfschar));
1342         iput(ino);
1343         down(&ntfs_lock);
1344         if (!default_upcase) {
1345                 ntfs_debug("Using volume specified $UpCase since default is "
1346                                 "not present.");
1347                 up(&ntfs_lock);
1348                 return TRUE;
1349         }
1350         max = default_upcase_len;
1351         if (max > vol->upcase_len)
1352                 max = vol->upcase_len;
1353         for (i = 0; i < max; i++)
1354                 if (vol->upcase[i] != default_upcase[i])
1355                         break;
1356         if (i == max) {
1357                 ntfs_free(vol->upcase);
1358                 vol->upcase = default_upcase;
1359                 vol->upcase_len = max;
1360                 ntfs_nr_upcase_users++;
1361                 up(&ntfs_lock);
1362                 ntfs_debug("Volume specified $UpCase matches default. Using "
1363                                 "default.");
1364                 return TRUE;
1365         }
1366         up(&ntfs_lock);
1367         ntfs_debug("Using volume specified $UpCase since it does not match "
1368                         "the default.");
1369         return TRUE;
1370 iput_upcase_failed:
1371         iput(ino);
1372         ntfs_free(vol->upcase);
1373         vol->upcase = NULL;
1374 upcase_failed:
1375         down(&ntfs_lock);
1376         if (default_upcase) {
1377                 vol->upcase = default_upcase;
1378                 vol->upcase_len = default_upcase_len;
1379                 ntfs_nr_upcase_users++;
1380                 up(&ntfs_lock);
1381                 ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
1382                                 "default.");
1383                 return TRUE;
1384         }
1385         up(&ntfs_lock);
1386         ntfs_error(sb, "Failed to initialize upcase table.");
1387         return FALSE;
1388 }
1389
1390 /**
1391  * load_system_files - open the system files using normal functions
1392  * @vol:        ntfs super block describing device whose system files to load
1393  *
1394  * Open the system files with normal access functions and complete setting up
1395  * the ntfs super block @vol.
1396  *
1397  * Return TRUE on success or FALSE on error.
1398  */
1399 static BOOL load_system_files(ntfs_volume *vol)
1400 {
1401         struct super_block *sb = vol->sb;
1402         MFT_RECORD *m;
1403         VOLUME_INFORMATION *vi;
1404         ntfs_attr_search_ctx *ctx;
1405
1406         ntfs_debug("Entering.");
1407 #ifdef NTFS_RW
1408         /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
1409         if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
1410                 static const char *es1 = "Failed to load $MFTMirr";
1411                 static const char *es2 = "$MFTMirr does not match $MFT";
1412                 static const char *es3 = ".  Run ntfsfix and/or chkdsk.";
1413
1414                 /* If a read-write mount, convert it to a read-only mount. */
1415                 if (!(sb->s_flags & MS_RDONLY)) {
1416                         if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1417                                         ON_ERRORS_CONTINUE))) {
1418                                 ntfs_error(sb, "%s and neither on_errors="
1419                                                 "continue nor on_errors="
1420                                                 "remount-ro was specified%s",
1421                                                 !vol->mftmirr_ino ? es1 : es2,
1422                                                 es3);
1423                                 goto iput_mirr_err_out;
1424                         }
1425                         sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1426                         ntfs_error(sb, "%s.  Mounting read-only%s",
1427                                         !vol->mftmirr_ino ? es1 : es2, es3);
1428                 } else
1429                         ntfs_warning(sb, "%s.  Will not be able to remount "
1430                                         "read-write%s",
1431                                         !vol->mftmirr_ino ? es1 : es2, es3);
1432                 /* This will prevent a read-write remount. */
1433                 NVolSetErrors(vol);
1434         }
1435 #endif /* NTFS_RW */
1436         /* Get mft bitmap attribute inode. */
1437         vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
1438         if (IS_ERR(vol->mftbmp_ino)) {
1439                 ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
1440                 goto iput_mirr_err_out;
1441         }
1442         /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
1443         if (!load_and_init_upcase(vol))
1444                 goto iput_mftbmp_err_out;
1445 #ifdef NTFS_RW
1446         /*
1447          * Read attribute definitions table and setup @vol->attrdef and
1448          * @vol->attrdef_size.
1449          */
1450         if (!load_and_init_attrdef(vol))
1451                 goto iput_upcase_err_out;
1452 #endif /* NTFS_RW */
1453         /*
1454          * Get the cluster allocation bitmap inode and verify the size, no
1455          * need for any locking at this stage as we are already running
1456          * exclusively as we are mount in progress task.
1457          */
1458         vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
1459         if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
1460                 if (!IS_ERR(vol->lcnbmp_ino))
1461                         iput(vol->lcnbmp_ino);
1462                 goto bitmap_failed;
1463         }
1464         NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
1465         if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
1466                 iput(vol->lcnbmp_ino);
1467 bitmap_failed:
1468                 ntfs_error(sb, "Failed to load $Bitmap.");
1469                 goto iput_attrdef_err_out;
1470         }
1471         /*
1472          * Get the volume inode and setup our cache of the volume flags and
1473          * version.
1474          */
1475         vol->vol_ino = ntfs_iget(sb, FILE_Volume);
1476         if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
1477                 if (!IS_ERR(vol->vol_ino))
1478                         iput(vol->vol_ino);
1479 volume_failed:
1480                 ntfs_error(sb, "Failed to load $Volume.");
1481                 goto iput_lcnbmp_err_out;
1482         }
1483         m = map_mft_record(NTFS_I(vol->vol_ino));
1484         if (IS_ERR(m)) {
1485 iput_volume_failed:
1486                 iput(vol->vol_ino);
1487                 goto volume_failed;
1488         }
1489         if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
1490                 ntfs_error(sb, "Failed to get attribute search context.");
1491                 goto get_ctx_vol_failed;
1492         }
1493         if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
1494                         ctx) || ctx->attr->non_resident || ctx->attr->flags) {
1495 err_put_vol:
1496                 ntfs_attr_put_search_ctx(ctx);
1497 get_ctx_vol_failed:
1498                 unmap_mft_record(NTFS_I(vol->vol_ino));
1499                 goto iput_volume_failed;
1500         }
1501         vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
1502                         le16_to_cpu(ctx->attr->data.resident.value_offset));
1503         /* Some bounds checks. */
1504         if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
1505                         le32_to_cpu(ctx->attr->data.resident.value_length) >
1506                         (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
1507                 goto err_put_vol;
1508         /* Copy the volume flags and version to the ntfs_volume structure. */
1509         vol->vol_flags = vi->flags;
1510         vol->major_ver = vi->major_ver;
1511         vol->minor_ver = vi->minor_ver;
1512         ntfs_attr_put_search_ctx(ctx);
1513         unmap_mft_record(NTFS_I(vol->vol_ino));
1514         printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
1515                         vol->minor_ver);
1516         if (vol->major_ver < 3 && NVolSparseEnabled(vol)) {
1517                 ntfs_warning(vol->sb, "Disabling sparse support due to NTFS "
1518                                 "volume version %i.%i (need at least version "
1519                                 "3.0).", vol->major_ver, vol->minor_ver);
1520                 NVolClearSparseEnabled(vol);
1521         }
1522 #ifdef NTFS_RW
1523         /* Make sure that no unsupported volume flags are set. */
1524         if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
1525                 static const char *es1a = "Volume is dirty";
1526                 static const char *es1b = "Volume has unsupported flags set";
1527                 static const char *es2 = ".  Run chkdsk and mount in Windows.";
1528                 const char *es1;
1529                 
1530                 es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
1531                 /* If a read-write mount, convert it to a read-only mount. */
1532                 if (!(sb->s_flags & MS_RDONLY)) {
1533                         if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1534                                         ON_ERRORS_CONTINUE))) {
1535                                 ntfs_error(sb, "%s and neither on_errors="
1536                                                 "continue nor on_errors="
1537                                                 "remount-ro was specified%s",
1538                                                 es1, es2);
1539                                 goto iput_vol_err_out;
1540                         }
1541                         sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1542                         ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1543                 } else
1544                         ntfs_warning(sb, "%s.  Will not be able to remount "
1545                                         "read-write%s", es1, es2);
1546                 /*
1547                  * Do not set NVolErrors() because ntfs_remount() re-checks the
1548                  * flags which we need to do in case any flags have changed.
1549                  */
1550         }
1551         /*
1552          * Get the inode for the logfile, check it and determine if the volume
1553          * was shutdown cleanly.
1554          */
1555         if (!load_and_check_logfile(vol) ||
1556                         !ntfs_is_logfile_clean(vol->logfile_ino)) {
1557                 static const char *es1a = "Failed to load $LogFile";
1558                 static const char *es1b = "$LogFile is not clean";
1559                 static const char *es2 = ".  Mount in Windows.";
1560                 const char *es1;
1561
1562                 es1 = !vol->logfile_ino ? es1a : es1b;
1563                 /* If a read-write mount, convert it to a read-only mount. */
1564                 if (!(sb->s_flags & MS_RDONLY)) {
1565                         if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1566                                         ON_ERRORS_CONTINUE))) {
1567                                 ntfs_error(sb, "%s and neither on_errors="
1568                                                 "continue nor on_errors="
1569                                                 "remount-ro was specified%s",
1570                                                 es1, es2);
1571                                 goto iput_logfile_err_out;
1572                         }
1573                         sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1574                         ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1575                 } else
1576                         ntfs_warning(sb, "%s.  Will not be able to remount "
1577                                         "read-write%s", es1, es2);
1578                 /* This will prevent a read-write remount. */
1579                 NVolSetErrors(vol);
1580         }
1581         /* If (still) a read-write mount, mark the volume dirty. */
1582         if (!(sb->s_flags & MS_RDONLY) &&
1583                         ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
1584                 static const char *es1 = "Failed to set dirty bit in volume "
1585                                 "information flags";
1586                 static const char *es2 = ".  Run chkdsk.";
1587
1588                 /* Convert to a read-only mount. */
1589                 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1590                                 ON_ERRORS_CONTINUE))) {
1591                         ntfs_error(sb, "%s and neither on_errors=continue nor "
1592                                         "on_errors=remount-ro was specified%s",
1593                                         es1, es2);
1594                         goto iput_logfile_err_out;
1595                 }
1596                 ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1597                 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1598                 /*
1599                  * Do not set NVolErrors() because ntfs_remount() might manage
1600                  * to set the dirty flag in which case all would be well.
1601                  */
1602         }
1603 #if 0
1604         // TODO: Enable this code once we start modifying anything that is
1605         //       different between NTFS 1.2 and 3.x...
1606         /*
1607          * If (still) a read-write mount, set the NT4 compatibility flag on
1608          * newer NTFS version volumes.
1609          */
1610         if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
1611                         ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
1612                 static const char *es1 = "Failed to set NT4 compatibility flag";
1613                 static const char *es2 = ".  Run chkdsk.";
1614
1615                 /* Convert to a read-only mount. */
1616                 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1617                                 ON_ERRORS_CONTINUE))) {
1618                         ntfs_error(sb, "%s and neither on_errors=continue nor "
1619                                         "on_errors=remount-ro was specified%s",
1620                                         es1, es2);
1621                         goto iput_logfile_err_out;
1622                 }
1623                 ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1624                 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1625                 NVolSetErrors(vol);
1626         }
1627 #endif
1628         /* If (still) a read-write mount, empty the logfile. */
1629         if (!(sb->s_flags & MS_RDONLY) &&
1630                         !ntfs_empty_logfile(vol->logfile_ino)) {
1631                 static const char *es1 = "Failed to empty $LogFile";
1632                 static const char *es2 = ".  Mount in Windows.";
1633
1634                 /* Convert to a read-only mount. */
1635                 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1636                                 ON_ERRORS_CONTINUE))) {
1637                         ntfs_error(sb, "%s and neither on_errors=continue nor "
1638                                         "on_errors=remount-ro was specified%s",
1639                                         es1, es2);
1640                         goto iput_logfile_err_out;
1641                 }
1642                 ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1643                 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1644                 NVolSetErrors(vol);
1645         }
1646 #endif /* NTFS_RW */
1647         /* Get the root directory inode. */
1648         vol->root_ino = ntfs_iget(sb, FILE_root);
1649         if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
1650                 if (!IS_ERR(vol->root_ino))
1651                         iput(vol->root_ino);
1652                 ntfs_error(sb, "Failed to load root directory.");
1653                 goto iput_logfile_err_out;
1654         }
1655         /* If on NTFS versions before 3.0, we are done. */
1656         if (vol->major_ver < 3)
1657                 return TRUE;
1658         /* NTFS 3.0+ specific initialization. */
1659         /* Get the security descriptors inode. */
1660         vol->secure_ino = ntfs_iget(sb, FILE_Secure);
1661         if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
1662                 if (!IS_ERR(vol->secure_ino))
1663                         iput(vol->secure_ino);
1664                 ntfs_error(sb, "Failed to load $Secure.");
1665                 goto iput_root_err_out;
1666         }
1667         // FIXME: Initialize security.
1668         /* Get the extended system files' directory inode. */
1669         vol->extend_ino = ntfs_iget(sb, FILE_Extend);
1670         if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
1671                 if (!IS_ERR(vol->extend_ino))
1672                         iput(vol->extend_ino);
1673                 ntfs_error(sb, "Failed to load $Extend.");
1674                 goto iput_sec_err_out;
1675         }
1676 #ifdef NTFS_RW
1677         /* Find the quota file, load it if present, and set it up. */
1678         if (!load_and_init_quota(vol)) {
1679                 static const char *es1 = "Failed to load $Quota";
1680                 static const char *es2 = ".  Run chkdsk.";
1681
1682                 /* If a read-write mount, convert it to a read-only mount. */
1683                 if (!(sb->s_flags & MS_RDONLY)) {
1684                         if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1685                                         ON_ERRORS_CONTINUE))) {
1686                                 ntfs_error(sb, "%s and neither on_errors="
1687                                                 "continue nor on_errors="
1688                                                 "remount-ro was specified%s",
1689                                                 es1, es2);
1690                                 goto iput_quota_err_out;
1691                         }
1692                         sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1693                         ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1694                 } else
1695                         ntfs_warning(sb, "%s.  Will not be able to remount "
1696                                         "read-write%s", es1, es2);
1697                 /* This will prevent a read-write remount. */
1698                 NVolSetErrors(vol);
1699         }
1700         /* If (still) a read-write mount, mark the quotas out of date. */
1701         if (!(sb->s_flags & MS_RDONLY) &&
1702                         !ntfs_mark_quotas_out_of_date(vol)) {
1703                 static const char *es1 = "Failed to mark quotas out of date";
1704                 static const char *es2 = ".  Run chkdsk.";
1705
1706                 /* Convert to a read-only mount. */
1707                 if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
1708                                 ON_ERRORS_CONTINUE))) {
1709                         ntfs_error(sb, "%s and neither on_errors=continue nor "
1710                                         "on_errors=remount-ro was specified%s",
1711                                         es1, es2);
1712                         goto iput_quota_err_out;
1713                 }
1714                 ntfs_error(sb, "%s.  Mounting read-only%s", es1, es2);
1715                 sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
1716                 NVolSetErrors(vol);
1717         }
1718         // TODO: Delete or checkpoint the $UsnJrnl if it exists.
1719 #endif /* NTFS_RW */
1720         return TRUE;
1721 #ifdef NTFS_RW
1722 iput_quota_err_out:
1723         if (vol->quota_q_ino)
1724                 iput(vol->quota_q_ino);
1725         if (vol->quota_ino)
1726                 iput(vol->quota_ino);
1727         iput(vol->extend_ino);
1728 #endif /* NTFS_RW */
1729 iput_sec_err_out:
1730         iput(vol->secure_ino);
1731 iput_root_err_out:
1732         iput(vol->root_ino);
1733 iput_logfile_err_out:
1734 #ifdef NTFS_RW
1735         if (vol->logfile_ino)
1736                 iput(vol->logfile_ino);
1737 iput_vol_err_out:
1738 #endif /* NTFS_RW */
1739         iput(vol->vol_ino);
1740 iput_lcnbmp_err_out:
1741         iput(vol->lcnbmp_ino);
1742 iput_attrdef_err_out:
1743         vol->attrdef_size = 0;
1744         if (vol->attrdef) {
1745                 ntfs_free(vol->attrdef);
1746                 vol->attrdef = NULL;
1747         }
1748 #ifdef NTFS_RW
1749 iput_upcase_err_out:
1750 #endif /* NTFS_RW */
1751         vol->upcase_len = 0;
1752         down(&ntfs_lock);
1753         if (vol->upcase == default_upcase) {
1754                 ntfs_nr_upcase_users--;
1755                 vol->upcase = NULL;
1756         }
1757         up(&ntfs_lock);
1758         if (vol->upcase) {
1759                 ntfs_free(vol->upcase);
1760                 vol->upcase = NULL;
1761         }
1762 iput_mftbmp_err_out:
1763         iput(vol->mftbmp_ino);
1764 iput_mirr_err_out:
1765 #ifdef NTFS_RW
1766         if (vol->mftmirr_ino)
1767                 iput(vol->mftmirr_ino);
1768 #endif /* NTFS_RW */
1769         return FALSE;
1770 }
1771
1772 /**
1773  * ntfs_put_super - called by the vfs to unmount a volume
1774  * @sb:         vfs superblock of volume to unmount
1775  *
1776  * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
1777  * the volume is being unmounted (umount system call has been invoked) and it
1778  * releases all inodes and memory belonging to the NTFS specific part of the
1779  * super block.
1780  */
1781 static void ntfs_put_super(struct super_block *sb)
1782 {
1783         ntfs_volume *vol = NTFS_SB(sb);
1784
1785         ntfs_debug("Entering.");
1786 #ifdef NTFS_RW
1787         /*
1788          * Commit all inodes while they are still open in case some of them
1789          * cause others to be dirtied.
1790          */
1791         ntfs_commit_inode(vol->vol_ino);
1792
1793         /* NTFS 3.0+ specific. */
1794         if (vol->major_ver >= 3) {
1795                 if (vol->quota_q_ino)
1796                         ntfs_commit_inode(vol->quota_q_ino);
1797                 if (vol->quota_ino)
1798                         ntfs_commit_inode(vol->quota_ino);
1799                 if (vol->extend_ino)
1800                         ntfs_commit_inode(vol->extend_ino);
1801                 if (vol->secure_ino)
1802                         ntfs_commit_inode(vol->secure_ino);
1803         }
1804
1805         ntfs_commit_inode(vol->root_ino);
1806
1807         down_write(&vol->lcnbmp_lock);
1808         ntfs_commit_inode(vol->lcnbmp_ino);
1809         up_write(&vol->lcnbmp_lock);
1810
1811         down_write(&vol->mftbmp_lock);
1812         ntfs_commit_inode(vol->mftbmp_ino);
1813         up_write(&vol->mftbmp_lock);
1814
1815         if (vol->logfile_ino)
1816                 ntfs_commit_inode(vol->logfile_ino);
1817
1818         if (vol->mftmirr_ino)
1819                 ntfs_commit_inode(vol->mftmirr_ino);
1820         ntfs_commit_inode(vol->mft_ino);
1821
1822         /*
1823          * If a read-write mount and no volume errors have occured, mark the
1824          * volume clean.  Also, re-commit all affected inodes.
1825          */
1826         if (!(sb->s_flags & MS_RDONLY)) {
1827                 if (!NVolErrors(vol)) {
1828                         if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
1829                                 ntfs_warning(sb, "Failed to clear dirty bit "
1830                                                 "in volume information "
1831                                                 "flags.  Run chkdsk.");
1832                         ntfs_commit_inode(vol->vol_ino);
1833                         ntfs_commit_inode(vol->root_ino);
1834                         if (vol->mftmirr_ino)
1835                                 ntfs_commit_inode(vol->mftmirr_ino);
1836                         ntfs_commit_inode(vol->mft_ino);
1837                 } else {
1838                         ntfs_warning(sb, "Volume has errors.  Leaving volume "
1839                                         "marked dirty.  Run chkdsk.");
1840                 }
1841         }
1842 #endif /* NTFS_RW */
1843
1844         iput(vol->vol_ino);
1845         vol->vol_ino = NULL;
1846
1847         /* NTFS 3.0+ specific clean up. */
1848         if (vol->major_ver >= 3) {
1849 #ifdef NTFS_RW
1850                 if (vol->quota_q_ino) {
1851                         iput(vol->quota_q_ino);
1852                         vol->quota_q_ino = NULL;
1853                 }
1854                 if (vol->quota_ino) {
1855                         iput(vol->quota_ino);
1856                         vol->quota_ino = NULL;
1857                 }
1858 #endif /* NTFS_RW */
1859                 if (vol->extend_ino) {
1860                         iput(vol->extend_ino);
1861                         vol->extend_ino = NULL;
1862                 }
1863                 if (vol->secure_ino) {
1864                         iput(vol->secure_ino);
1865                         vol->secure_ino = NULL;
1866                 }
1867         }
1868
1869         iput(vol->root_ino);
1870         vol->root_ino = NULL;
1871
1872         down_write(&vol->lcnbmp_lock);
1873         iput(vol->lcnbmp_ino);
1874         vol->lcnbmp_ino = NULL;
1875         up_write(&vol->lcnbmp_lock);
1876
1877         down_write(&vol->mftbmp_lock);
1878         iput(vol->mftbmp_ino);
1879         vol->mftbmp_ino = NULL;
1880         up_write(&vol->mftbmp_lock);
1881
1882 #ifdef NTFS_RW
1883         if (vol->logfile_ino) {
1884                 iput(vol->logfile_ino);
1885                 vol->logfile_ino = NULL;
1886         }
1887         if (vol->mftmirr_ino) {
1888                 /* Re-commit the mft mirror and mft just in case. */
1889                 ntfs_commit_inode(vol->mftmirr_ino);
1890                 ntfs_commit_inode(vol->mft_ino);
1891                 iput(vol->mftmirr_ino);
1892                 vol->mftmirr_ino = NULL;
1893         }
1894         /*
1895          * If any dirty inodes are left, throw away all mft data page cache
1896          * pages to allow a clean umount.  This should never happen any more
1897          * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
1898          * the underlying mft records are written out and cleaned.  If it does,
1899          * happen anyway, we want to know...
1900          */
1901         ntfs_commit_inode(vol->mft_ino);
1902         write_inode_now(vol->mft_ino, 1);
1903         if (!list_empty(&sb->s_dirty)) {
1904                 const char *s1, *s2;
1905
1906                 down(&vol->mft_ino->i_sem);
1907                 truncate_inode_pages(vol->mft_ino->i_mapping, 0);
1908                 up(&vol->mft_ino->i_sem);
1909                 write_inode_now(vol->mft_ino, 1);
1910                 if (!list_empty(&sb->s_dirty)) {
1911                         static const char *_s1 = "inodes";
1912                         static const char *_s2 = "";
1913                         s1 = _s1;
1914                         s2 = _s2;
1915                 } else {
1916                         static const char *_s1 = "mft pages";
1917                         static const char *_s2 = "They have been thrown "
1918                                         "away.  ";
1919                         s1 = _s1;
1920                         s2 = _s2;
1921                 }
1922                 ntfs_error(sb, "Dirty %s found at umount time.  %sYou should "
1923                                 "run chkdsk.  Please email "
1924                                 "linux-ntfs-dev@lists.sourceforge.net and say "
1925                                 "that you saw this message.  Thank you.", s1,
1926                                 s2);
1927         }
1928 #endif /* NTFS_RW */
1929
1930         iput(vol->mft_ino);
1931         vol->mft_ino = NULL;
1932
1933         /* Throw away the table of attribute definitions. */
1934         vol->attrdef_size = 0;
1935         if (vol->attrdef) {
1936                 ntfs_free(vol->attrdef);
1937                 vol->attrdef = NULL;
1938         }
1939         vol->upcase_len = 0;
1940         /*
1941          * Destroy the global default upcase table if necessary.  Also decrease
1942          * the number of upcase users if we are a user.
1943          */
1944         down(&ntfs_lock);
1945         if (vol->upcase == default_upcase) {
1946                 ntfs_nr_upcase_users--;
1947                 vol->upcase = NULL;
1948         }
1949         if (!ntfs_nr_upcase_users && default_upcase) {
1950                 ntfs_free(default_upcase);
1951                 default_upcase = NULL;
1952         }
1953         if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
1954                 free_compression_buffers();
1955         up(&ntfs_lock);
1956         if (vol->upcase) {
1957                 ntfs_free(vol->upcase);
1958                 vol->upcase = NULL;
1959         }
1960         if (vol->nls_map) {
1961                 unload_nls(vol->nls_map);
1962                 vol->nls_map = NULL;
1963         }
1964         sb->s_fs_info = NULL;
1965         kfree(vol);
1966         return;
1967 }
1968
1969 /**
1970  * get_nr_free_clusters - return the number of free clusters on a volume
1971  * @vol:        ntfs volume for which to obtain free cluster count
1972  *
1973  * Calculate the number of free clusters on the mounted NTFS volume @vol. We
1974  * actually calculate the number of clusters in use instead because this
1975  * allows us to not care about partial pages as these will be just zero filled
1976  * and hence not be counted as allocated clusters.
1977  *
1978  * The only particularity is that clusters beyond the end of the logical ntfs
1979  * volume will be marked as allocated to prevent errors which means we have to
1980  * discount those at the end. This is important as the cluster bitmap always
1981  * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
1982  * the logical volume and marked in use when they are not as they do not exist.
1983  *
1984  * If any pages cannot be read we assume all clusters in the erroring pages are
1985  * in use. This means we return an underestimate on errors which is better than
1986  * an overestimate.
1987  */
1988 static s64 get_nr_free_clusters(ntfs_volume *vol)
1989 {
1990         s64 nr_free = vol->nr_clusters;
1991         u32 *kaddr;
1992         struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
1993         filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
1994         struct page *page;
1995         pgoff_t index, max_index;
1996
1997         ntfs_debug("Entering.");
1998         /* Serialize accesses to the cluster bitmap. */
1999         down_read(&vol->lcnbmp_lock);
2000         /*
2001          * Convert the number of bits into bytes rounded up, then convert into
2002          * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
2003          * full and one partial page max_index = 2.
2004          */
2005         max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
2006                         PAGE_CACHE_SHIFT;
2007         /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2008         ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
2009                         max_index, PAGE_CACHE_SIZE / 4);
2010         for (index = 0; index < max_index; index++) {
2011                 unsigned int i;
2012                 /*
2013                  * Read the page from page cache, getting it from backing store
2014                  * if necessary, and increment the use count.
2015                  */
2016                 page = read_cache_page(mapping, index, (filler_t*)readpage,
2017                                 NULL);
2018                 /* Ignore pages which errored synchronously. */
2019                 if (IS_ERR(page)) {
2020                         ntfs_debug("Sync read_cache_page() error. Skipping "
2021                                         "page (index 0x%lx).", index);
2022                         nr_free -= PAGE_CACHE_SIZE * 8;
2023                         continue;
2024                 }
2025                 wait_on_page_locked(page);
2026                 /* Ignore pages which errored asynchronously. */
2027                 if (!PageUptodate(page)) {
2028                         ntfs_debug("Async read_cache_page() error. Skipping "
2029                                         "page (index 0x%lx).", index);
2030                         page_cache_release(page);
2031                         nr_free -= PAGE_CACHE_SIZE * 8;
2032                         continue;
2033                 }
2034                 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2035                 /*
2036                  * For each 4 bytes, subtract the number of set bits. If this
2037                  * is the last page and it is partial we don't really care as
2038                  * it just means we do a little extra work but it won't affect
2039                  * the result as all out of range bytes are set to zero by
2040                  * ntfs_readpage().
2041                  */
2042                 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2043                         nr_free -= (s64)hweight32(kaddr[i]);
2044                 kunmap_atomic(kaddr, KM_USER0);
2045                 page_cache_release(page);
2046         }
2047         ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
2048         /*
2049          * Fixup for eventual bits outside logical ntfs volume (see function
2050          * description above).
2051          */
2052         if (vol->nr_clusters & 63)
2053                 nr_free += 64 - (vol->nr_clusters & 63);
2054         up_read(&vol->lcnbmp_lock);
2055         /* If errors occured we may well have gone below zero, fix this. */
2056         if (nr_free < 0)
2057                 nr_free = 0;
2058         ntfs_debug("Exiting.");
2059         return nr_free;
2060 }
2061
2062 /**
2063  * __get_nr_free_mft_records - return the number of free inodes on a volume
2064  * @vol:        ntfs volume for which to obtain free inode count
2065  * @nr_free:    number of mft records in filesystem
2066  * @max_index:  maximum number of pages containing set bits
2067  *
2068  * Calculate the number of free mft records (inodes) on the mounted NTFS
2069  * volume @vol. We actually calculate the number of mft records in use instead
2070  * because this allows us to not care about partial pages as these will be just
2071  * zero filled and hence not be counted as allocated mft record.
2072  *
2073  * If any pages cannot be read we assume all mft records in the erroring pages
2074  * are in use. This means we return an underestimate on errors which is better
2075  * than an overestimate.
2076  *
2077  * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
2078  */
2079 static unsigned long __get_nr_free_mft_records(ntfs_volume *vol,
2080                 s64 nr_free, const pgoff_t max_index)
2081 {
2082         u32 *kaddr;
2083         struct address_space *mapping = vol->mftbmp_ino->i_mapping;
2084         filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
2085         struct page *page;
2086         pgoff_t index;
2087
2088         ntfs_debug("Entering.");
2089         /* Use multiples of 4 bytes, thus max_size is PAGE_CACHE_SIZE / 4. */
2090         ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
2091                         "0x%lx.", max_index, PAGE_CACHE_SIZE / 4);
2092         for (index = 0; index < max_index; index++) {
2093                 unsigned int i;
2094                 /*
2095                  * Read the page from page cache, getting it from backing store
2096                  * if necessary, and increment the use count.
2097                  */
2098                 page = read_cache_page(mapping, index, (filler_t*)readpage,
2099                                 NULL);
2100                 /* Ignore pages which errored synchronously. */
2101                 if (IS_ERR(page)) {
2102                         ntfs_debug("Sync read_cache_page() error. Skipping "
2103                                         "page (index 0x%lx).", index);
2104                         nr_free -= PAGE_CACHE_SIZE * 8;
2105                         continue;
2106                 }
2107                 wait_on_page_locked(page);
2108                 /* Ignore pages which errored asynchronously. */
2109                 if (!PageUptodate(page)) {
2110                         ntfs_debug("Async read_cache_page() error. Skipping "
2111                                         "page (index 0x%lx).", index);
2112                         page_cache_release(page);
2113                         nr_free -= PAGE_CACHE_SIZE * 8;
2114                         continue;
2115                 }
2116                 kaddr = (u32*)kmap_atomic(page, KM_USER0);
2117                 /*
2118                  * For each 4 bytes, subtract the number of set bits. If this
2119                  * is the last page and it is partial we don't really care as
2120                  * it just means we do a little extra work but it won't affect
2121                  * the result as all out of range bytes are set to zero by
2122                  * ntfs_readpage().
2123                  */
2124                 for (i = 0; i < PAGE_CACHE_SIZE / 4; i++)
2125                         nr_free -= (s64)hweight32(kaddr[i]);
2126                 kunmap_atomic(kaddr, KM_USER0);
2127                 page_cache_release(page);
2128         }
2129         ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
2130                         index - 1);
2131         /* If errors occured we may well have gone below zero, fix this. */
2132         if (nr_free < 0)
2133                 nr_free = 0;
2134         ntfs_debug("Exiting.");
2135         return nr_free;
2136 }
2137
2138 /**
2139  * ntfs_statfs - return information about mounted NTFS volume
2140  * @sb:         super block of mounted volume
2141  * @sfs:        statfs structure in which to return the information
2142  *
2143  * Return information about the mounted NTFS volume @sb in the statfs structure
2144  * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
2145  * called). We interpret the values to be correct of the moment in time at
2146  * which we are called. Most values are variable otherwise and this isn't just
2147  * the free values but the totals as well. For example we can increase the
2148  * total number of file nodes if we run out and we can keep doing this until
2149  * there is no more space on the volume left at all.
2150  *
2151  * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
2152  * ustat system calls.
2153  *
2154  * Return 0 on success or -errno on error.
2155  */
2156 static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
2157 {
2158         s64 size;
2159         ntfs_volume *vol = NTFS_SB(sb);
2160         ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
2161         pgoff_t max_index;
2162         unsigned long flags;
2163
2164         ntfs_debug("Entering.");
2165         /* Type of filesystem. */
2166         sfs->f_type   = NTFS_SB_MAGIC;
2167         /* Optimal transfer block size. */
2168         sfs->f_bsize  = PAGE_CACHE_SIZE;
2169         /*
2170          * Total data blocks in filesystem in units of f_bsize and since
2171          * inodes are also stored in data blocs ($MFT is a file) this is just
2172          * the total clusters.
2173          */
2174         sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
2175                                 PAGE_CACHE_SHIFT;
2176         /* Free data blocks in filesystem in units of f_bsize. */
2177         size          = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
2178                                 PAGE_CACHE_SHIFT;
2179         if (size < 0LL)
2180                 size = 0LL;
2181         /* Free blocks avail to non-superuser, same as above on NTFS. */
2182         sfs->f_bavail = sfs->f_bfree = size;
2183         /* Serialize accesses to the inode bitmap. */
2184         down_read(&vol->mftbmp_lock);
2185         read_lock_irqsave(&mft_ni->size_lock, flags);
2186         size = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
2187         /*
2188          * Convert the maximum number of set bits into bytes rounded up, then
2189          * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
2190          * have one full and one partial page max_index = 2.
2191          */
2192         max_index = ((((mft_ni->initialized_size >> vol->mft_record_size_bits)
2193                         + 7) >> 3) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2194         read_unlock_irqrestore(&mft_ni->size_lock, flags);
2195         /* Number of inodes in filesystem (at this point in time). */
2196         sfs->f_files = size;
2197         /* Free inodes in fs (based on current total count). */
2198         sfs->f_ffree = __get_nr_free_mft_records(vol, size, max_index);
2199         up_read(&vol->mftbmp_lock);
2200         /*
2201          * File system id. This is extremely *nix flavour dependent and even
2202          * within Linux itself all fs do their own thing. I interpret this to
2203          * mean a unique id associated with the mounted fs and not the id
2204          * associated with the filesystem driver, the latter is already given
2205          * by the filesystem type in sfs->f_type. Thus we use the 64-bit
2206          * volume serial number splitting it into two 32-bit parts. We enter
2207          * the least significant 32-bits in f_fsid[0] and the most significant
2208          * 32-bits in f_fsid[1].
2209          */
2210         sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
2211         sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
2212         /* Maximum length of filenames. */
2213         sfs->f_namelen     = NTFS_MAX_NAME_LEN;
2214         return 0;
2215 }
2216
2217 /**
2218  * The complete super operations.
2219  */
2220 static struct super_operations ntfs_sops = {
2221         .alloc_inode    = ntfs_alloc_big_inode,   /* VFS: Allocate new inode. */
2222         .destroy_inode  = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
2223         .put_inode      = ntfs_put_inode,         /* VFS: Called just before
2224                                                      the inode reference count
2225                                                      is decreased. */
2226 #ifdef NTFS_RW
2227         //.dirty_inode  = NULL,                 /* VFS: Called from
2228         //                                         __mark_inode_dirty(). */
2229         .write_inode    = ntfs_write_inode,     /* VFS: Write dirty inode to
2230                                                    disk. */
2231         //.drop_inode   = NULL,                 /* VFS: Called just after the
2232         //                                         inode reference count has
2233         //                                         been decreased to zero.
2234         //                                         NOTE: The inode lock is
2235         //                                         held. See fs/inode.c::
2236         //                                         generic_drop_inode(). */
2237         //.delete_inode = NULL,                 /* VFS: Delete inode from disk.
2238         //                                         Called when i_count becomes
2239         //                                         0 and i_nlink is also 0. */
2240         //.write_super  = NULL,                 /* Flush dirty super block to
2241         //                                         disk. */
2242         //.sync_fs      = NULL,                 /* ? */
2243         //.write_super_lockfs   = NULL,         /* ? */
2244         //.unlockfs     = NULL,                 /* ? */
2245 #endif /* NTFS_RW */
2246         .put_super      = ntfs_put_super,       /* Syscall: umount. */
2247         .statfs         = ntfs_statfs,          /* Syscall: statfs */
2248         .remount_fs     = ntfs_remount,         /* Syscall: mount -o remount. */
2249         .clear_inode    = ntfs_clear_big_inode, /* VFS: Called when an inode is
2250                                                    removed from memory. */
2251         //.umount_begin = NULL,                 /* Forced umount. */
2252         .show_options   = ntfs_show_options,    /* Show mount options in
2253                                                    proc. */
2254 };
2255
2256 /**
2257  * ntfs_fill_super - mount an ntfs filesystem
2258  * @sb:         super block of ntfs filesystem to mount
2259  * @opt:        string containing the mount options
2260  * @silent:     silence error output
2261  *
2262  * ntfs_fill_super() is called by the VFS to mount the device described by @sb
2263  * with the mount otions in @data with the NTFS filesystem.
2264  *
2265  * If @silent is true, remain silent even if errors are detected. This is used
2266  * during bootup, when the kernel tries to mount the root filesystem with all
2267  * registered filesystems one after the other until one succeeds. This implies
2268  * that all filesystems except the correct one will quite correctly and
2269  * expectedly return an error, but nobody wants to see error messages when in
2270  * fact this is what is supposed to happen.
2271  *
2272  * NOTE: @sb->s_flags contains the mount options flags.
2273  */
2274 static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
2275 {
2276         ntfs_volume *vol;
2277         struct buffer_head *bh;
2278         struct inode *tmp_ino;
2279         int result;
2280
2281         ntfs_debug("Entering.");
2282 #ifndef NTFS_RW
2283         sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
2284 #endif /* ! NTFS_RW */
2285         /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
2286         sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
2287         vol = NTFS_SB(sb);
2288         if (!vol) {
2289                 if (!silent)
2290                         ntfs_error(sb, "Allocation of NTFS volume structure "
2291                                         "failed. Aborting mount...");
2292                 return -ENOMEM;
2293         }
2294         /* Initialize ntfs_volume structure. */
2295         memset(vol, 0, sizeof(ntfs_volume));
2296         vol->sb = sb;
2297         vol->upcase = NULL;
2298         vol->attrdef = NULL;
2299         vol->mft_ino = NULL;
2300         vol->mftbmp_ino = NULL;
2301         init_rwsem(&vol->mftbmp_lock);
2302 #ifdef NTFS_RW
2303         vol->mftmirr_ino = NULL;
2304         vol->logfile_ino = NULL;
2305 #endif /* NTFS_RW */
2306         vol->lcnbmp_ino = NULL;
2307         init_rwsem(&vol->lcnbmp_lock);
2308         vol->vol_ino = NULL;
2309         vol->root_ino = NULL;
2310         vol->secure_ino = NULL;
2311         vol->extend_ino = NULL;
2312 #ifdef NTFS_RW
2313         vol->quota_ino = NULL;
2314         vol->quota_q_ino = NULL;
2315 #endif /* NTFS_RW */
2316         vol->nls_map = NULL;
2317
2318         /*
2319          * Default is group and other don't have any access to files or
2320          * directories while owner has full access. Further, files by default
2321          * are not executable but directories are of course browseable.
2322          */
2323         vol->fmask = 0177;
2324         vol->dmask = 0077;
2325
2326         unlock_kernel();
2327
2328         /* By default, enable sparse support. */
2329         NVolSetSparseEnabled(vol);
2330
2331         /* Important to get the mount options dealt with now. */
2332         if (!parse_options(vol, (char*)opt))
2333                 goto err_out_now;
2334
2335         /*
2336          * TODO: Fail safety check. In the future we should really be able to
2337          * cope with this being the case, but for now just bail out.
2338          */
2339         if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
2340                 if (!silent)
2341                         ntfs_error(sb, "Device has unsupported hardsect_size.");
2342                 goto err_out_now;
2343         }
2344
2345         /* Setup the device access block size to NTFS_BLOCK_SIZE. */
2346         if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
2347                 if (!silent)
2348                         ntfs_error(sb, "Unable to set block size.");
2349                 goto err_out_now;
2350         }
2351
2352         /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
2353         vol->nr_blocks = i_size_read(sb->s_bdev->bd_inode) >>
2354                         NTFS_BLOCK_SIZE_BITS;
2355
2356         /* Read the boot sector and return unlocked buffer head to it. */
2357         if (!(bh = read_ntfs_boot_sector(sb, silent))) {
2358                 if (!silent)
2359                         ntfs_error(sb, "Not an NTFS volume.");
2360                 goto err_out_now;
2361         }
2362
2363         /*
2364          * Extract the data from the boot sector and setup the ntfs super block
2365          * using it.
2366          */
2367         result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
2368
2369         /* Initialize the cluster and mft allocators. */
2370         ntfs_setup_allocators(vol);
2371
2372         brelse(bh);
2373
2374         if (!result) {
2375                 if (!silent)
2376                         ntfs_error(sb, "Unsupported NTFS filesystem.");
2377                 goto err_out_now;
2378         }
2379
2380         /*
2381          * TODO: When we start coping with sector sizes different from
2382          * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
2383          * device (probably to NTFS_BLOCK_SIZE).
2384          */
2385
2386         /* Setup remaining fields in the super block. */
2387         sb->s_magic = NTFS_SB_MAGIC;
2388
2389         /*
2390          * Ntfs allows 63 bits for the file size, i.e. correct would be:
2391          *      sb->s_maxbytes = ~0ULL >> 1;
2392          * But the kernel uses a long as the page cache page index which on
2393          * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
2394          * defined to the maximum the page cache page index can cope with
2395          * without overflowing the index or to 2^63 - 1, whichever is smaller.
2396          */
2397         sb->s_maxbytes = MAX_LFS_FILESIZE;
2398
2399         sb->s_time_gran = 100;
2400
2401         /*
2402          * Now load the metadata required for the page cache and our address
2403          * space operations to function. We do this by setting up a specialised
2404          * read_inode method and then just calling the normal iget() to obtain
2405          * the inode for $MFT which is sufficient to allow our normal inode
2406          * operations and associated address space operations to function.
2407          */
2408         sb->s_op = &ntfs_sops;
2409         tmp_ino = new_inode(sb);
2410         if (!tmp_ino) {
2411                 if (!silent)
2412                         ntfs_error(sb, "Failed to load essential metadata.");
2413                 goto err_out_now;
2414         }
2415         tmp_ino->i_ino = FILE_MFT;
2416         insert_inode_hash(tmp_ino);
2417         if (ntfs_read_inode_mount(tmp_ino) < 0) {
2418                 if (!silent)
2419                         ntfs_error(sb, "Failed to load essential metadata.");
2420                 goto iput_tmp_ino_err_out_now;
2421         }
2422         down(&ntfs_lock);
2423         /*
2424          * The current mount is a compression user if the cluster size is
2425          * less than or equal 4kiB.
2426          */
2427         if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
2428                 result = allocate_compression_buffers();
2429                 if (result) {
2430                         ntfs_error(NULL, "Failed to allocate buffers "
2431                                         "for compression engine.");
2432                         ntfs_nr_compression_users--;
2433                         up(&ntfs_lock);
2434                         goto iput_tmp_ino_err_out_now;
2435                 }
2436         }
2437         /*
2438          * Generate the global default upcase table if necessary.  Also
2439          * temporarily increment the number of upcase users to avoid race
2440          * conditions with concurrent (u)mounts.
2441          */
2442         if (!default_upcase)
2443                 default_upcase = generate_default_upcase();
2444         ntfs_nr_upcase_users++;
2445         up(&ntfs_lock);
2446         /*
2447          * From now on, ignore @silent parameter. If we fail below this line,
2448          * it will be due to a corrupt fs or a system error, so we report it.
2449          */
2450         /*
2451          * Open the system files with normal access functions and complete
2452          * setting up the ntfs super block.
2453          */
2454         if (!load_system_files(vol)) {
2455                 ntfs_error(sb, "Failed to load system files.");
2456                 goto unl_upcase_iput_tmp_ino_err_out_now;
2457         }
2458         if ((sb->s_root = d_alloc_root(vol->root_ino))) {
2459                 /* We increment i_count simulating an ntfs_iget(). */
2460                 atomic_inc(&vol->root_ino->i_count);
2461                 ntfs_debug("Exiting, status successful.");
2462                 /* Release the default upcase if it has no users. */
2463                 down(&ntfs_lock);
2464                 if (!--ntfs_nr_upcase_users && default_upcase) {
2465                         ntfs_free(default_upcase);
2466                         default_upcase = NULL;
2467                 }
2468                 up(&ntfs_lock);
2469                 sb->s_export_op = &ntfs_export_ops;
2470                 lock_kernel();
2471                 return 0;
2472         }
2473         ntfs_error(sb, "Failed to allocate root directory.");
2474         /* Clean up after the successful load_system_files() call from above. */
2475         // TODO: Use ntfs_put_super() instead of repeating all this code...
2476         // FIXME: Should mark the volume clean as the error is most likely
2477         //        -ENOMEM.
2478         iput(vol->vol_ino);
2479         vol->vol_ino = NULL;
2480         /* NTFS 3.0+ specific clean up. */
2481         if (vol->major_ver >= 3) {
2482 #ifdef NTFS_RW
2483                 if (vol->quota_q_ino) {
2484                         iput(vol->quota_q_ino);
2485                         vol->quota_q_ino = NULL;
2486                 }
2487                 if (vol->quota_ino) {
2488                         iput(vol->quota_ino);
2489                         vol->quota_ino = NULL;
2490                 }
2491 #endif /* NTFS_RW */
2492                 if (vol->extend_ino) {
2493                         iput(vol->extend_ino);
2494                         vol->extend_ino = NULL;
2495                 }
2496                 if (vol->secure_ino) {
2497                         iput(vol->secure_ino);
2498                         vol->secure_ino = NULL;
2499                 }
2500         }
2501         iput(vol->root_ino);
2502         vol->root_ino = NULL;
2503         iput(vol->lcnbmp_ino);
2504         vol->lcnbmp_ino = NULL;
2505         iput(vol->mftbmp_ino);
2506         vol->mftbmp_ino = NULL;
2507 #ifdef NTFS_RW
2508         if (vol->logfile_ino) {
2509                 iput(vol->logfile_ino);
2510                 vol->logfile_ino = NULL;
2511         }
2512         if (vol->mftmirr_ino) {
2513                 iput(vol->mftmirr_ino);
2514                 vol->mftmirr_ino = NULL;
2515         }
2516 #endif /* NTFS_RW */
2517         /* Throw away the table of attribute definitions. */
2518         vol->attrdef_size = 0;
2519         if (vol->attrdef) {
2520                 ntfs_free(vol->attrdef);
2521                 vol->attrdef = NULL;
2522         }
2523         vol->upcase_len = 0;
2524         down(&ntfs_lock);
2525         if (vol->upcase == default_upcase) {
2526                 ntfs_nr_upcase_users--;
2527                 vol->upcase = NULL;
2528         }
2529         up(&ntfs_lock);
2530         if (vol->upcase) {
2531                 ntfs_free(vol->upcase);
2532                 vol->upcase = NULL;
2533         }
2534         if (vol->nls_map) {
2535                 unload_nls(vol->nls_map);
2536                 vol->nls_map = NULL;
2537         }
2538         /* Error exit code path. */
2539 unl_upcase_iput_tmp_ino_err_out_now:
2540         /*
2541          * Decrease the number of upcase users and destroy the global default
2542          * upcase table if necessary.
2543          */
2544         down(&ntfs_lock);
2545         if (!--ntfs_nr_upcase_users && default_upcase) {
2546                 ntfs_free(default_upcase);
2547                 default_upcase = NULL;
2548         }
2549         if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
2550                 free_compression_buffers();
2551         up(&ntfs_lock);
2552 iput_tmp_ino_err_out_now:
2553         iput(tmp_ino);
2554         if (vol->mft_ino && vol->mft_ino != tmp_ino)
2555                 iput(vol->mft_ino);
2556         vol->mft_ino = NULL;
2557         /*
2558          * This is needed to get ntfs_clear_extent_inode() called for each
2559          * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
2560          * leak resources and B) a subsequent mount fails automatically due to
2561          * ntfs_iget() never calling down into our ntfs_read_locked_inode()
2562          * method again... FIXME: Do we need to do this twice now because of
2563          * attribute inodes? I think not, so leave as is for now... (AIA)
2564          */
2565         if (invalidate_inodes(sb)) {
2566                 ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
2567                                 "driver bug.");
2568                 /* Copied from fs/super.c. I just love this message. (-; */
2569                 printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
2570                                 "seconds.  Have a nice day...\n");
2571         }
2572         /* Errors at this stage are irrelevant. */
2573 err_out_now:
2574         lock_kernel();
2575         sb->s_fs_info = NULL;
2576         kfree(vol);
2577         ntfs_debug("Failed, returning -EINVAL.");
2578         return -EINVAL;
2579 }
2580
2581 /*
2582  * This is a slab cache to optimize allocations and deallocations of Unicode
2583  * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
2584  * (255) Unicode characters + a terminating NULL Unicode character.
2585  */
2586 kmem_cache_t *ntfs_name_cache;
2587
2588 /* Slab caches for efficient allocation/deallocation of inodes. */
2589 kmem_cache_t *ntfs_inode_cache;
2590 kmem_cache_t *ntfs_big_inode_cache;
2591
2592 /* Init once constructor for the inode slab cache. */
2593 static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
2594                 unsigned long flags)
2595 {
2596         ntfs_inode *ni = (ntfs_inode *)foo;
2597
2598         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2599                         SLAB_CTOR_CONSTRUCTOR)
2600                 inode_init_once(VFS_I(ni));
2601 }
2602
2603 /*
2604  * Slab caches to optimize allocations and deallocations of attribute search
2605  * contexts and index contexts, respectively.
2606  */
2607 kmem_cache_t *ntfs_attr_ctx_cache;
2608 kmem_cache_t *ntfs_index_ctx_cache;
2609
2610 /* Driver wide semaphore. */
2611 DECLARE_MUTEX(ntfs_lock);
2612
2613 static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
2614         int flags, const char *dev_name, void *data)
2615 {
2616         return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
2617 }
2618
2619 static struct file_system_type ntfs_fs_type = {
2620         .owner          = THIS_MODULE,
2621         .name           = "ntfs",
2622         .get_sb         = ntfs_get_sb,
2623         .kill_sb        = kill_block_super,
2624         .fs_flags       = FS_REQUIRES_DEV,
2625 };
2626
2627 /* Stable names for the slab caches. */
2628 static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
2629 static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
2630 static const char ntfs_name_cache_name[] = "ntfs_name_cache";
2631 static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
2632 static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
2633
2634 static int __init init_ntfs_fs(void)
2635 {
2636         int err = 0;
2637
2638         /* This may be ugly but it results in pretty output so who cares. (-8 */
2639         printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
2640 #ifdef NTFS_RW
2641                         "W"
2642 #else
2643                         "O"
2644 #endif
2645 #ifdef DEBUG
2646                         " DEBUG"
2647 #endif
2648 #ifdef MODULE
2649                         " MODULE"
2650 #endif
2651                         "].\n");
2652
2653         ntfs_debug("Debug messages are enabled.");
2654
2655         ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
2656                         sizeof(ntfs_index_context), 0 /* offset */,
2657                         SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
2658         if (!ntfs_index_ctx_cache) {
2659                 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2660                                 ntfs_index_ctx_cache_name);
2661                 goto ictx_err_out;
2662         }
2663         ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
2664                         sizeof(ntfs_attr_search_ctx), 0 /* offset */,
2665                         SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
2666         if (!ntfs_attr_ctx_cache) {
2667                 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2668                                 ntfs_attr_ctx_cache_name);
2669                 goto actx_err_out;
2670         }
2671
2672         ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
2673                         (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
2674                         SLAB_HWCACHE_ALIGN, NULL, NULL);
2675         if (!ntfs_name_cache) {
2676                 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2677                                 ntfs_name_cache_name);
2678                 goto name_err_out;
2679         }
2680
2681         ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
2682                         sizeof(ntfs_inode), 0,
2683                         SLAB_RECLAIM_ACCOUNT, NULL, NULL);
2684         if (!ntfs_inode_cache) {
2685                 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2686                                 ntfs_inode_cache_name);
2687                 goto inode_err_out;
2688         }
2689
2690         ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
2691                         sizeof(big_ntfs_inode), 0,
2692                         SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
2693                         ntfs_big_inode_init_once, NULL);
2694         if (!ntfs_big_inode_cache) {
2695                 printk(KERN_CRIT "NTFS: Failed to create %s!\n",
2696                                 ntfs_big_inode_cache_name);
2697                 goto big_inode_err_out;
2698         }
2699
2700         /* Register the ntfs sysctls. */
2701         err = ntfs_sysctl(1);
2702         if (err) {
2703                 printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
2704                 goto sysctl_err_out;
2705         }
2706
2707         err = register_filesystem(&ntfs_fs_type);
2708         if (!err) {
2709                 ntfs_debug("NTFS driver registered successfully.");
2710                 return 0; /* Success! */
2711         }
2712         printk(KERN_CRIT "NTFS: Failed to register NTFS filesystem driver!\n");
2713
2714 sysctl_err_out:
2715         kmem_cache_destroy(ntfs_big_inode_cache);
2716 big_inode_err_out:
2717         kmem_cache_destroy(ntfs_inode_cache);
2718 inode_err_out:
2719         kmem_cache_destroy(ntfs_name_cache);
2720 name_err_out:
2721         kmem_cache_destroy(ntfs_attr_ctx_cache);
2722 actx_err_out:
2723         kmem_cache_destroy(ntfs_index_ctx_cache);
2724 ictx_err_out:
2725         if (!err) {
2726                 printk(KERN_CRIT "NTFS: Aborting NTFS filesystem driver "
2727                                 "registration...\n");
2728                 err = -ENOMEM;
2729         }
2730         return err;
2731 }
2732
2733 static void __exit exit_ntfs_fs(void)
2734 {
2735         int err = 0;
2736
2737         ntfs_debug("Unregistering NTFS driver.");
2738
2739         unregister_filesystem(&ntfs_fs_type);
2740
2741         if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
2742                 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2743                                 ntfs_big_inode_cache_name);
2744         if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
2745                 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2746                                 ntfs_inode_cache_name);
2747         if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
2748                 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2749                                 ntfs_name_cache_name);
2750         if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
2751                 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2752                                 ntfs_attr_ctx_cache_name);
2753         if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
2754                 printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
2755                                 ntfs_index_ctx_cache_name);
2756         if (err)
2757                 printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
2758                                 "probably a BUG in the driver! Please report "
2759                                 "you saw this message to "
2760                                 "linux-ntfs-dev@lists.sourceforge.net\n");
2761         /* Unregister the ntfs sysctls. */
2762         ntfs_sysctl(0);
2763 }
2764
2765 MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
2766 MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2005 Anton Altaparmakov");
2767 MODULE_VERSION(NTFS_VERSION);
2768 MODULE_LICENSE("GPL");
2769 #ifdef DEBUG
2770 module_param(debug_msgs, bool, 0);
2771 MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
2772 #endif
2773
2774 module_init(init_ntfs_fs)
2775 module_exit(exit_ntfs_fs)