The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ntfs/super.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: aac31fe8ca7f640c0d758a2e9ad3cd15


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.