Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. |
| 5 | * |
| 6 | * |
| 7 | * terminology |
| 8 | * |
| 9 | * cluster - allocation unit - 512,1K,2K,4K,...,2M |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 10 | * vcn - virtual cluster number - Offset inside the file in clusters. |
| 11 | * vbo - virtual byte offset - Offset inside the file in bytes. |
| 12 | * lcn - logical cluster number - 0 based cluster in clusters heap. |
| 13 | * lbo - logical byte offset - Absolute position inside volume. |
| 14 | * run - maps VCN to LCN - Stored in attributes in packed form. |
| 15 | * attr - attribute segment - std/name/data etc records inside MFT. |
| 16 | * mi - MFT inode - One MFT record(usually 1024 bytes or 4K), consists of attributes. |
| 17 | * ni - NTFS inode - Extends linux inode. consists of one or more mft inodes. |
| 18 | * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 19 | * |
| 20 | * WSL - Windows Subsystem for Linux |
| 21 | * https://docs.microsoft.com/en-us/windows/wsl/file-permissions |
| 22 | * It stores uid/gid/mode/dev in xattr |
| 23 | * |
| 24 | */ |
| 25 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/buffer_head.h> |
| 28 | #include <linux/exportfs.h> |
| 29 | #include <linux/fs.h> |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 30 | #include <linux/fs_context.h> |
| 31 | #include <linux/fs_parser.h> |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 32 | #include <linux/log2.h> |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 33 | #include <linux/module.h> |
| 34 | #include <linux/nls.h> |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 35 | #include <linux/seq_file.h> |
| 36 | #include <linux/statfs.h> |
| 37 | |
| 38 | #include "debug.h" |
| 39 | #include "ntfs.h" |
| 40 | #include "ntfs_fs.h" |
| 41 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 42 | #include "lib/lib.h" |
| 43 | #endif |
| 44 | |
| 45 | #ifdef CONFIG_PRINTK |
| 46 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 47 | * ntfs_printk - Trace warnings/notices/errors. |
| 48 | * |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 49 | * Thanks Joe Perches <joe@perches.com> for implementation |
| 50 | */ |
| 51 | void ntfs_printk(const struct super_block *sb, const char *fmt, ...) |
| 52 | { |
| 53 | struct va_format vaf; |
| 54 | va_list args; |
| 55 | int level; |
| 56 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 57 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 58 | /* Should we use different ratelimits for warnings/notices/errors? */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 59 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 60 | return; |
| 61 | |
| 62 | va_start(args, fmt); |
| 63 | |
| 64 | level = printk_get_level(fmt); |
| 65 | vaf.fmt = printk_skip_level(fmt); |
| 66 | vaf.va = &args; |
| 67 | printk("%c%cntfs3: %s: %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf); |
| 68 | |
| 69 | va_end(args); |
| 70 | } |
| 71 | |
| 72 | static char s_name_buf[512]; |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 73 | static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 74 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 75 | /* |
| 76 | * ntfs_inode_printk |
| 77 | * |
| 78 | * Print warnings/notices/errors about inode using name or inode number. |
| 79 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 80 | void ntfs_inode_printk(struct inode *inode, const char *fmt, ...) |
| 81 | { |
| 82 | struct super_block *sb = inode->i_sb; |
| 83 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 84 | char *name; |
| 85 | va_list args; |
| 86 | struct va_format vaf; |
| 87 | int level; |
| 88 | |
| 89 | if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3")) |
| 90 | return; |
| 91 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 92 | /* Use static allocated buffer, if possible. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 93 | name = atomic_dec_and_test(&s_name_buf_cnt) |
| 94 | ? s_name_buf |
| 95 | : kmalloc(sizeof(s_name_buf), GFP_NOFS); |
| 96 | |
| 97 | if (name) { |
| 98 | struct dentry *de = d_find_alias(inode); |
| 99 | const u32 name_len = ARRAY_SIZE(s_name_buf) - 1; |
| 100 | |
| 101 | if (de) { |
| 102 | spin_lock(&de->d_lock); |
| 103 | snprintf(name, name_len, " \"%s\"", de->d_name.name); |
| 104 | spin_unlock(&de->d_lock); |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 105 | name[name_len] = 0; /* To be sure. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 106 | } else { |
| 107 | name[0] = 0; |
| 108 | } |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 109 | dput(de); /* Cocci warns if placed in branch "if (de)" */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | va_start(args, fmt); |
| 113 | |
| 114 | level = printk_get_level(fmt); |
| 115 | vaf.fmt = printk_skip_level(fmt); |
| 116 | vaf.va = &args; |
| 117 | |
| 118 | printk("%c%cntfs3: %s: ino=%lx,%s %pV\n", KERN_SOH_ASCII, level, |
| 119 | sb->s_id, inode->i_ino, name ? name : "", &vaf); |
| 120 | |
| 121 | va_end(args); |
| 122 | |
| 123 | atomic_inc(&s_name_buf_cnt); |
| 124 | if (name != s_name_buf) |
| 125 | kfree(name); |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | /* |
| 130 | * Shared memory struct. |
| 131 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 132 | * On-disk ntfs's upcase table is created by ntfs formatter. |
| 133 | * 'upcase' table is 128K bytes of memory. |
| 134 | * We should read it into memory when mounting. |
| 135 | * Several ntfs volumes likely use the same 'upcase' table. |
| 136 | * It is good idea to share in-memory 'upcase' table between different volumes. |
| 137 | * Unfortunately winxp/vista/win7 use different upcase tables. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 138 | */ |
| 139 | static DEFINE_SPINLOCK(s_shared_lock); |
| 140 | |
| 141 | static struct { |
| 142 | void *ptr; |
| 143 | u32 len; |
| 144 | int cnt; |
| 145 | } s_shared[8]; |
| 146 | |
| 147 | /* |
| 148 | * ntfs_set_shared |
| 149 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 150 | * Return: |
| 151 | * * @ptr - If pointer was saved in shared memory. |
| 152 | * * NULL - If pointer was not shared. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 153 | */ |
| 154 | void *ntfs_set_shared(void *ptr, u32 bytes) |
| 155 | { |
| 156 | void *ret = NULL; |
| 157 | int i, j = -1; |
| 158 | |
| 159 | spin_lock(&s_shared_lock); |
| 160 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 161 | if (!s_shared[i].cnt) { |
| 162 | j = i; |
| 163 | } else if (bytes == s_shared[i].len && |
| 164 | !memcmp(s_shared[i].ptr, ptr, bytes)) { |
| 165 | s_shared[i].cnt += 1; |
| 166 | ret = s_shared[i].ptr; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (!ret && j != -1) { |
| 172 | s_shared[j].ptr = ptr; |
| 173 | s_shared[j].len = bytes; |
| 174 | s_shared[j].cnt = 1; |
| 175 | ret = ptr; |
| 176 | } |
| 177 | spin_unlock(&s_shared_lock); |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * ntfs_put_shared |
| 184 | * |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 185 | * Return: |
| 186 | * * @ptr - If pointer is not shared anymore. |
| 187 | * * NULL - If pointer is still shared. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 188 | */ |
| 189 | void *ntfs_put_shared(void *ptr) |
| 190 | { |
| 191 | void *ret = ptr; |
| 192 | int i; |
| 193 | |
| 194 | spin_lock(&s_shared_lock); |
| 195 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { |
| 196 | if (s_shared[i].cnt && s_shared[i].ptr == ptr) { |
| 197 | if (--s_shared[i].cnt) |
| 198 | ret = NULL; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | spin_unlock(&s_shared_lock); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 207 | static inline void put_mount_options(struct ntfs_mount_options *options) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 208 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 209 | kfree(options->nls_name); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 210 | unload_nls(options->nls); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 211 | kfree(options); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | enum Opt { |
| 215 | Opt_uid, |
| 216 | Opt_gid, |
| 217 | Opt_umask, |
| 218 | Opt_dmask, |
| 219 | Opt_fmask, |
| 220 | Opt_immutable, |
| 221 | Opt_discard, |
| 222 | Opt_force, |
| 223 | Opt_sparse, |
| 224 | Opt_nohidden, |
| 225 | Opt_showmeta, |
| 226 | Opt_acl, |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 227 | Opt_iocharset, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 228 | Opt_prealloc, |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 229 | Opt_noacsrules, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 230 | Opt_err, |
| 231 | }; |
| 232 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 233 | static const struct fs_parameter_spec ntfs_fs_parameters[] = { |
| 234 | fsparam_u32("uid", Opt_uid), |
| 235 | fsparam_u32("gid", Opt_gid), |
| 236 | fsparam_u32oct("umask", Opt_umask), |
| 237 | fsparam_u32oct("dmask", Opt_dmask), |
| 238 | fsparam_u32oct("fmask", Opt_fmask), |
| 239 | fsparam_flag_no("sys_immutable", Opt_immutable), |
| 240 | fsparam_flag_no("discard", Opt_discard), |
| 241 | fsparam_flag_no("force", Opt_force), |
| 242 | fsparam_flag_no("sparse", Opt_sparse), |
Kari Argillander | 9d1939f | 2021-09-07 18:35:54 +0300 | [diff] [blame] | 243 | fsparam_flag_no("hidden", Opt_nohidden), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 244 | fsparam_flag_no("acl", Opt_acl), |
| 245 | fsparam_flag_no("showmeta", Opt_showmeta), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 246 | fsparam_flag_no("prealloc", Opt_prealloc), |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 247 | fsparam_flag_no("acsrules", Opt_noacsrules), |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 248 | fsparam_string("iocharset", Opt_iocharset), |
| 249 | |
| 250 | __fsparam(fs_param_is_string, |
| 251 | "nls", Opt_iocharset, |
| 252 | fs_param_deprecated, NULL), |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 253 | {} |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 254 | }; |
| 255 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 256 | /* |
| 257 | * Load nls table or if @nls is utf8 then return NULL. |
| 258 | */ |
| 259 | static struct nls_table *ntfs_load_nls(char *nls) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 260 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 261 | struct nls_table *ret; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 262 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 263 | if (!nls) |
| 264 | nls = CONFIG_NLS_DEFAULT; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 265 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 266 | if (strcmp(nls, "utf8") == 0) |
| 267 | return NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 268 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 269 | if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0) |
| 270 | return load_nls_default(); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 271 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 272 | ret = load_nls(nls); |
| 273 | if (ret) |
| 274 | return ret; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 275 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 276 | return ERR_PTR(-EINVAL); |
| 277 | } |
| 278 | |
| 279 | static int ntfs_fs_parse_param(struct fs_context *fc, |
| 280 | struct fs_parameter *param) |
| 281 | { |
| 282 | struct ntfs_mount_options *opts = fc->fs_private; |
| 283 | struct fs_parse_result result; |
| 284 | int opt; |
| 285 | |
| 286 | opt = fs_parse(fc, ntfs_fs_parameters, param, &result); |
| 287 | if (opt < 0) |
| 288 | return opt; |
| 289 | |
| 290 | switch (opt) { |
| 291 | case Opt_uid: |
| 292 | opts->fs_uid = make_kuid(current_user_ns(), result.uint_32); |
| 293 | if (!uid_valid(opts->fs_uid)) |
| 294 | return invalf(fc, "ntfs3: Invalid value for uid."); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 295 | break; |
| 296 | case Opt_gid: |
| 297 | opts->fs_gid = make_kgid(current_user_ns(), result.uint_32); |
| 298 | if (!gid_valid(opts->fs_gid)) |
| 299 | return invalf(fc, "ntfs3: Invalid value for gid."); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 300 | break; |
| 301 | case Opt_umask: |
| 302 | if (result.uint_32 & ~07777) |
| 303 | return invalf(fc, "ntfs3: Invalid value for umask."); |
| 304 | opts->fs_fmask_inv = ~result.uint_32; |
| 305 | opts->fs_dmask_inv = ~result.uint_32; |
| 306 | opts->fmask = 1; |
| 307 | opts->dmask = 1; |
| 308 | break; |
| 309 | case Opt_dmask: |
| 310 | if (result.uint_32 & ~07777) |
| 311 | return invalf(fc, "ntfs3: Invalid value for dmask."); |
| 312 | opts->fs_dmask_inv = ~result.uint_32; |
| 313 | opts->dmask = 1; |
| 314 | break; |
| 315 | case Opt_fmask: |
| 316 | if (result.uint_32 & ~07777) |
| 317 | return invalf(fc, "ntfs3: Invalid value for fmask."); |
| 318 | opts->fs_fmask_inv = ~result.uint_32; |
| 319 | opts->fmask = 1; |
| 320 | break; |
| 321 | case Opt_immutable: |
| 322 | opts->sys_immutable = result.negated ? 0 : 1; |
| 323 | break; |
| 324 | case Opt_discard: |
| 325 | opts->discard = result.negated ? 0 : 1; |
| 326 | break; |
| 327 | case Opt_force: |
| 328 | opts->force = result.negated ? 0 : 1; |
| 329 | break; |
| 330 | case Opt_sparse: |
| 331 | opts->sparse = result.negated ? 0 : 1; |
| 332 | break; |
| 333 | case Opt_nohidden: |
Kari Argillander | 9d1939f | 2021-09-07 18:35:54 +0300 | [diff] [blame] | 334 | opts->nohidden = result.negated ? 1 : 0; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 335 | break; |
| 336 | case Opt_acl: |
| 337 | if (!result.negated) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 338 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 339 | fc->sb_flags |= SB_POSIXACL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 340 | #else |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 341 | return invalf(fc, "ntfs3: Support for ACL not compiled in!"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 342 | #endif |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 343 | else |
| 344 | fc->sb_flags &= ~SB_POSIXACL; |
| 345 | break; |
| 346 | case Opt_showmeta: |
| 347 | opts->showmeta = result.negated ? 0 : 1; |
| 348 | break; |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 349 | case Opt_iocharset: |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 350 | kfree(opts->nls_name); |
| 351 | opts->nls_name = param->string; |
| 352 | param->string = NULL; |
| 353 | break; |
| 354 | case Opt_prealloc: |
| 355 | opts->prealloc = result.negated ? 0 : 1; |
| 356 | break; |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 357 | case Opt_noacsrules: |
| 358 | opts->noacsrules = result.negated ? 1 : 0; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 359 | break; |
| 360 | default: |
| 361 | /* Should not be here unless we forget add case. */ |
| 362 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 363 | } |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 367 | static int ntfs_fs_reconfigure(struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 368 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 369 | struct super_block *sb = fc->root->d_sb; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 370 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 371 | struct ntfs_mount_options *new_opts = fc->fs_private; |
| 372 | int ro_rw; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 373 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 374 | ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 375 | if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 376 | errorf(fc, "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n"); |
| 377 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 378 | } |
| 379 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 380 | new_opts->nls = ntfs_load_nls(new_opts->nls_name); |
| 381 | if (IS_ERR(new_opts->nls)) { |
| 382 | new_opts->nls = NULL; |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 383 | errorf(fc, "ntfs3: Cannot load iocharset %s", new_opts->nls_name); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | } |
| 386 | if (new_opts->nls != sbi->options->nls) |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 387 | return invalf(fc, "ntfs3: Cannot use different iocharset when remounting!"); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 388 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 389 | sync_filesystem(sb); |
| 390 | |
| 391 | if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) && |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 392 | !new_opts->force) { |
| 393 | errorf(fc, "ntfs3: Volume is dirty and \"force\" flag is not set!"); |
| 394 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 395 | } |
| 396 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 397 | memcpy(sbi->options, new_opts, sizeof(*new_opts)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 398 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 399 | return 0; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static struct kmem_cache *ntfs_inode_cachep; |
| 403 | |
| 404 | static struct inode *ntfs_alloc_inode(struct super_block *sb) |
| 405 | { |
| 406 | struct ntfs_inode *ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS); |
| 407 | |
| 408 | if (!ni) |
| 409 | return NULL; |
| 410 | |
| 411 | memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode)); |
| 412 | |
| 413 | mutex_init(&ni->ni_lock); |
| 414 | |
| 415 | return &ni->vfs_inode; |
| 416 | } |
| 417 | |
| 418 | static void ntfs_i_callback(struct rcu_head *head) |
| 419 | { |
| 420 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 421 | struct ntfs_inode *ni = ntfs_i(inode); |
| 422 | |
| 423 | mutex_destroy(&ni->ni_lock); |
| 424 | |
| 425 | kmem_cache_free(ntfs_inode_cachep, ni); |
| 426 | } |
| 427 | |
| 428 | static void ntfs_destroy_inode(struct inode *inode) |
| 429 | { |
| 430 | call_rcu(&inode->i_rcu, ntfs_i_callback); |
| 431 | } |
| 432 | |
| 433 | static void init_once(void *foo) |
| 434 | { |
| 435 | struct ntfs_inode *ni = foo; |
| 436 | |
| 437 | inode_init_once(&ni->vfs_inode); |
| 438 | } |
| 439 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 440 | /* |
| 441 | * put_ntfs - Noinline to reduce binary size. |
| 442 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 443 | static noinline void put_ntfs(struct ntfs_sb_info *sbi) |
| 444 | { |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 445 | kfree(sbi->new_rec); |
| 446 | kvfree(ntfs_put_shared(sbi->upcase)); |
| 447 | kfree(sbi->def_table); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 448 | |
| 449 | wnd_close(&sbi->mft.bitmap); |
| 450 | wnd_close(&sbi->used.bitmap); |
| 451 | |
| 452 | if (sbi->mft.ni) |
| 453 | iput(&sbi->mft.ni->vfs_inode); |
| 454 | |
| 455 | if (sbi->security.ni) |
| 456 | iput(&sbi->security.ni->vfs_inode); |
| 457 | |
| 458 | if (sbi->reparse.ni) |
| 459 | iput(&sbi->reparse.ni->vfs_inode); |
| 460 | |
| 461 | if (sbi->objid.ni) |
| 462 | iput(&sbi->objid.ni->vfs_inode); |
| 463 | |
| 464 | if (sbi->volume.ni) |
| 465 | iput(&sbi->volume.ni->vfs_inode); |
| 466 | |
| 467 | ntfs_update_mftmirr(sbi, 0); |
| 468 | |
| 469 | indx_clear(&sbi->security.index_sii); |
| 470 | indx_clear(&sbi->security.index_sdh); |
| 471 | indx_clear(&sbi->reparse.index_r); |
| 472 | indx_clear(&sbi->objid.index_o); |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 473 | kfree(sbi->compress.lznt); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 474 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 475 | xpress_free_decompressor(sbi->compress.xpress); |
| 476 | lzx_free_decompressor(sbi->compress.lzx); |
| 477 | #endif |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 478 | kfree(sbi); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void ntfs_put_super(struct super_block *sb) |
| 482 | { |
| 483 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 484 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 485 | /* Mark rw ntfs as clear, if possible. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 486 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 487 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 488 | put_mount_options(sbi->options); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 489 | put_ntfs(sbi); |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 490 | sb->s_fs_info = NULL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 491 | |
| 492 | sync_blockdev(sb->s_bdev); |
| 493 | } |
| 494 | |
| 495 | static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 496 | { |
| 497 | struct super_block *sb = dentry->d_sb; |
| 498 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 499 | struct wnd_bitmap *wnd = &sbi->used.bitmap; |
| 500 | |
| 501 | buf->f_type = sb->s_magic; |
| 502 | buf->f_bsize = sbi->cluster_size; |
| 503 | buf->f_blocks = wnd->nbits; |
| 504 | |
| 505 | buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd); |
| 506 | buf->f_fsid.val[0] = sbi->volume.ser_num; |
| 507 | buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32); |
| 508 | buf->f_namelen = NTFS_NAME_LEN; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int ntfs_show_options(struct seq_file *m, struct dentry *root) |
| 514 | { |
| 515 | struct super_block *sb = root->d_sb; |
| 516 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Kari Argillander | 564c97b | 2021-09-07 18:35:51 +0300 | [diff] [blame] | 517 | struct ntfs_mount_options *opts = sbi->options; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 518 | struct user_namespace *user_ns = seq_user_ns(m); |
| 519 | |
Kari Argillander | 15b2ae7 | 2021-09-07 18:35:57 +0300 | [diff] [blame] | 520 | seq_printf(m, ",uid=%u", |
| 521 | from_kuid_munged(user_ns, opts->fs_uid)); |
| 522 | seq_printf(m, ",gid=%u", |
| 523 | from_kgid_munged(user_ns, opts->fs_gid)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 524 | if (opts->fmask) |
| 525 | seq_printf(m, ",fmask=%04o", ~opts->fs_fmask_inv); |
| 526 | if (opts->dmask) |
| 527 | seq_printf(m, ",dmask=%04o", ~opts->fs_dmask_inv); |
| 528 | if (opts->nls) |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 529 | seq_printf(m, ",iocharset=%s", opts->nls->charset); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 530 | else |
Kari Argillander | e274cde | 2021-09-07 18:35:55 +0300 | [diff] [blame] | 531 | seq_puts(m, ",iocharset=utf8"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 532 | if (opts->sys_immutable) |
| 533 | seq_puts(m, ",sys_immutable"); |
| 534 | if (opts->discard) |
| 535 | seq_puts(m, ",discard"); |
| 536 | if (opts->sparse) |
| 537 | seq_puts(m, ",sparse"); |
| 538 | if (opts->showmeta) |
| 539 | seq_puts(m, ",showmeta"); |
| 540 | if (opts->nohidden) |
| 541 | seq_puts(m, ",nohidden"); |
| 542 | if (opts->force) |
| 543 | seq_puts(m, ",force"); |
Kari Argillander | 28a941f | 2021-09-07 18:35:56 +0300 | [diff] [blame] | 544 | if (opts->noacsrules) |
| 545 | seq_puts(m, ",noacsrules"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 546 | if (opts->prealloc) |
| 547 | seq_puts(m, ",prealloc"); |
| 548 | if (sb->s_flags & SB_POSIXACL) |
| 549 | seq_puts(m, ",acl"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 554 | /* |
| 555 | * ntfs_sync_fs - super_operations::sync_fs |
| 556 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 557 | static int ntfs_sync_fs(struct super_block *sb, int wait) |
| 558 | { |
| 559 | int err = 0, err2; |
| 560 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 561 | struct ntfs_inode *ni; |
| 562 | struct inode *inode; |
| 563 | |
| 564 | ni = sbi->security.ni; |
| 565 | if (ni) { |
| 566 | inode = &ni->vfs_inode; |
| 567 | err2 = _ni_write_inode(inode, wait); |
| 568 | if (err2 && !err) |
| 569 | err = err2; |
| 570 | } |
| 571 | |
| 572 | ni = sbi->objid.ni; |
| 573 | if (ni) { |
| 574 | inode = &ni->vfs_inode; |
| 575 | err2 = _ni_write_inode(inode, wait); |
| 576 | if (err2 && !err) |
| 577 | err = err2; |
| 578 | } |
| 579 | |
| 580 | ni = sbi->reparse.ni; |
| 581 | if (ni) { |
| 582 | inode = &ni->vfs_inode; |
| 583 | err2 = _ni_write_inode(inode, wait); |
| 584 | if (err2 && !err) |
| 585 | err = err2; |
| 586 | } |
| 587 | |
| 588 | if (!err) |
| 589 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 590 | |
| 591 | ntfs_update_mftmirr(sbi, wait); |
| 592 | |
| 593 | return err; |
| 594 | } |
| 595 | |
| 596 | static const struct super_operations ntfs_sops = { |
| 597 | .alloc_inode = ntfs_alloc_inode, |
| 598 | .destroy_inode = ntfs_destroy_inode, |
| 599 | .evict_inode = ntfs_evict_inode, |
| 600 | .put_super = ntfs_put_super, |
| 601 | .statfs = ntfs_statfs, |
| 602 | .show_options = ntfs_show_options, |
| 603 | .sync_fs = ntfs_sync_fs, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 604 | .write_inode = ntfs3_write_inode, |
| 605 | }; |
| 606 | |
| 607 | static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino, |
| 608 | u32 generation) |
| 609 | { |
| 610 | struct MFT_REF ref; |
| 611 | struct inode *inode; |
| 612 | |
| 613 | ref.low = cpu_to_le32(ino); |
| 614 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 615 | ref.high = cpu_to_le16(ino >> 32); |
| 616 | #else |
| 617 | ref.high = 0; |
| 618 | #endif |
| 619 | ref.seq = cpu_to_le16(generation); |
| 620 | |
| 621 | inode = ntfs_iget5(sb, &ref, NULL); |
| 622 | if (!IS_ERR(inode) && is_bad_inode(inode)) { |
| 623 | iput(inode); |
| 624 | inode = ERR_PTR(-ESTALE); |
| 625 | } |
| 626 | |
| 627 | return inode; |
| 628 | } |
| 629 | |
| 630 | static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 631 | int fh_len, int fh_type) |
| 632 | { |
| 633 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 634 | ntfs_export_get_inode); |
| 635 | } |
| 636 | |
| 637 | static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 638 | int fh_len, int fh_type) |
| 639 | { |
| 640 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 641 | ntfs_export_get_inode); |
| 642 | } |
| 643 | |
| 644 | /* TODO: == ntfs_sync_inode */ |
| 645 | static int ntfs_nfs_commit_metadata(struct inode *inode) |
| 646 | { |
| 647 | return _ni_write_inode(inode, 1); |
| 648 | } |
| 649 | |
| 650 | static const struct export_operations ntfs_export_ops = { |
| 651 | .fh_to_dentry = ntfs_fh_to_dentry, |
| 652 | .fh_to_parent = ntfs_fh_to_parent, |
| 653 | .get_parent = ntfs3_get_parent, |
| 654 | .commit_metadata = ntfs_nfs_commit_metadata, |
| 655 | }; |
| 656 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 657 | /* |
| 658 | * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb". |
| 659 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 660 | static u32 format_size_gb(const u64 bytes, u32 *mb) |
| 661 | { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 662 | /* Do simple right 30 bit shift of 64 bit value. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 663 | u64 kbytes = bytes >> 10; |
| 664 | u32 kbytes32 = kbytes; |
| 665 | |
| 666 | *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20; |
| 667 | if (*mb >= 100) |
| 668 | *mb = 99; |
| 669 | |
| 670 | return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12); |
| 671 | } |
| 672 | |
| 673 | static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot) |
| 674 | { |
| 675 | return boot->sectors_per_clusters <= 0x80 |
| 676 | ? boot->sectors_per_clusters |
| 677 | : (1u << (0 - boot->sectors_per_clusters)); |
| 678 | } |
| 679 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 680 | /* |
| 681 | * ntfs_init_from_boot - Init internal info from on-disk boot sector. |
| 682 | */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 683 | static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size, |
| 684 | u64 dev_size) |
| 685 | { |
| 686 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 687 | int err; |
| 688 | u32 mb, gb, boot_sector_size, sct_per_clst, record_size; |
| 689 | u64 sectors, clusters, fs_size, mlcn, mlcn2; |
| 690 | struct NTFS_BOOT *boot; |
| 691 | struct buffer_head *bh; |
| 692 | struct MFT_REC *rec; |
| 693 | u16 fn, ao; |
| 694 | |
| 695 | sbi->volume.blocks = dev_size >> PAGE_SHIFT; |
| 696 | |
| 697 | bh = ntfs_bread(sb, 0); |
| 698 | if (!bh) |
| 699 | return -EIO; |
| 700 | |
| 701 | err = -EINVAL; |
| 702 | boot = (struct NTFS_BOOT *)bh->b_data; |
| 703 | |
| 704 | if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) |
| 705 | goto out; |
| 706 | |
| 707 | /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/ |
| 708 | /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1]) |
| 709 | * goto out; |
| 710 | */ |
| 711 | |
| 712 | boot_sector_size = (u32)boot->bytes_per_sector[1] << 8; |
| 713 | if (boot->bytes_per_sector[0] || boot_sector_size < SECTOR_SIZE || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 714 | !is_power_of_2(boot_sector_size)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 715 | goto out; |
| 716 | } |
| 717 | |
| 718 | /* cluster size: 512, 1K, 2K, 4K, ... 2M */ |
| 719 | sct_per_clst = true_sectors_per_clst(boot); |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 720 | if (!is_power_of_2(sct_per_clst)) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 721 | goto out; |
| 722 | |
| 723 | mlcn = le64_to_cpu(boot->mft_clst); |
| 724 | mlcn2 = le64_to_cpu(boot->mft2_clst); |
| 725 | sectors = le64_to_cpu(boot->sectors_per_volume); |
| 726 | |
| 727 | if (mlcn * sct_per_clst >= sectors) |
| 728 | goto out; |
| 729 | |
| 730 | if (mlcn2 * sct_per_clst >= sectors) |
| 731 | goto out; |
| 732 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 733 | /* Check MFT record size. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 734 | if ((boot->record_size < 0 && |
| 735 | SECTOR_SIZE > (2U << (-boot->record_size))) || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 736 | (boot->record_size >= 0 && !is_power_of_2(boot->record_size))) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 737 | goto out; |
| 738 | } |
| 739 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 740 | /* Check index record size. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 741 | if ((boot->index_size < 0 && |
| 742 | SECTOR_SIZE > (2U << (-boot->index_size))) || |
Kari Argillander | 528c9b3 | 2021-08-16 13:37:32 +0300 | [diff] [blame] | 743 | (boot->index_size >= 0 && !is_power_of_2(boot->index_size))) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 744 | goto out; |
| 745 | } |
| 746 | |
| 747 | sbi->sector_size = boot_sector_size; |
| 748 | sbi->sector_bits = blksize_bits(boot_sector_size); |
| 749 | fs_size = (sectors + 1) << sbi->sector_bits; |
| 750 | |
| 751 | gb = format_size_gb(fs_size, &mb); |
| 752 | |
| 753 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 754 | * - Volume formatted and mounted with the same sector size. |
| 755 | * - Volume formatted 4K and mounted as 512. |
| 756 | * - Volume formatted 512 and mounted as 4K. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 757 | */ |
| 758 | if (sbi->sector_size != sector_size) { |
| 759 | ntfs_warn(sb, |
| 760 | "Different NTFS' sector size and media sector size"); |
| 761 | dev_size += sector_size - 1; |
| 762 | } |
| 763 | |
| 764 | sbi->cluster_size = boot_sector_size * sct_per_clst; |
| 765 | sbi->cluster_bits = blksize_bits(sbi->cluster_size); |
| 766 | |
| 767 | sbi->mft.lbo = mlcn << sbi->cluster_bits; |
| 768 | sbi->mft.lbo2 = mlcn2 << sbi->cluster_bits; |
| 769 | |
| 770 | if (sbi->cluster_size < sbi->sector_size) |
| 771 | goto out; |
| 772 | |
| 773 | sbi->cluster_mask = sbi->cluster_size - 1; |
| 774 | sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask; |
| 775 | sbi->record_size = record_size = boot->record_size < 0 |
| 776 | ? 1 << (-boot->record_size) |
| 777 | : (u32)boot->record_size |
| 778 | << sbi->cluster_bits; |
| 779 | |
| 780 | if (record_size > MAXIMUM_BYTES_PER_MFT) |
| 781 | goto out; |
| 782 | |
| 783 | sbi->record_bits = blksize_bits(record_size); |
| 784 | sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes |
| 785 | |
| 786 | sbi->max_bytes_per_attr = |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 787 | record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) - |
| 788 | ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) - |
| 789 | ALIGN(sizeof(enum ATTR_TYPE), 8); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 790 | |
| 791 | sbi->index_size = boot->index_size < 0 |
| 792 | ? 1u << (-boot->index_size) |
| 793 | : (u32)boot->index_size << sbi->cluster_bits; |
| 794 | |
| 795 | sbi->volume.ser_num = le64_to_cpu(boot->serial_num); |
| 796 | sbi->volume.size = sectors << sbi->sector_bits; |
| 797 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 798 | /* Warning if RAW volume. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 799 | if (dev_size < fs_size) { |
| 800 | u32 mb0, gb0; |
| 801 | |
| 802 | gb0 = format_size_gb(dev_size, &mb0); |
| 803 | ntfs_warn( |
| 804 | sb, |
| 805 | "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only", |
| 806 | gb, mb, gb0, mb0); |
| 807 | sb->s_flags |= SB_RDONLY; |
| 808 | } |
| 809 | |
| 810 | clusters = sbi->volume.size >> sbi->cluster_bits; |
| 811 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 812 | /* 32 bits per cluster. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 813 | if (clusters >> 32) { |
| 814 | ntfs_notice( |
| 815 | sb, |
| 816 | "NTFS %u.%02u Gb is too big to use 32 bits per cluster", |
| 817 | gb, mb); |
| 818 | goto out; |
| 819 | } |
| 820 | #elif BITS_PER_LONG < 64 |
| 821 | #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS" |
| 822 | #endif |
| 823 | |
| 824 | sbi->used.bitmap.nbits = clusters; |
| 825 | |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 826 | rec = kzalloc(record_size, GFP_NOFS); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 827 | if (!rec) { |
| 828 | err = -ENOMEM; |
| 829 | goto out; |
| 830 | } |
| 831 | |
| 832 | sbi->new_rec = rec; |
| 833 | rec->rhdr.sign = NTFS_FILE_SIGNATURE; |
| 834 | rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1); |
| 835 | fn = (sbi->record_size >> SECTOR_SHIFT) + 1; |
| 836 | rec->rhdr.fix_num = cpu_to_le16(fn); |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 837 | ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 838 | rec->attr_off = cpu_to_le16(ao); |
Kari Argillander | fa3cacf | 2021-08-26 11:56:29 +0300 | [diff] [blame] | 839 | rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8)); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 840 | rec->total = cpu_to_le32(sbi->record_size); |
| 841 | ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END; |
| 842 | |
| 843 | if (sbi->cluster_size < PAGE_SIZE) |
| 844 | sb_set_blocksize(sb, sbi->cluster_size); |
| 845 | |
| 846 | sbi->block_mask = sb->s_blocksize - 1; |
| 847 | sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits; |
| 848 | sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits; |
| 849 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 850 | /* Maximum size for normal files. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 851 | sbi->maxbytes = (clusters << sbi->cluster_bits) - 1; |
| 852 | |
| 853 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 854 | if (clusters >= (1ull << (64 - sbi->cluster_bits))) |
| 855 | sbi->maxbytes = -1; |
| 856 | sbi->maxbytes_sparse = -1; |
| 857 | #else |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 858 | /* Maximum size for sparse file. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 859 | sbi->maxbytes_sparse = (1ull << (sbi->cluster_bits + 32)) - 1; |
| 860 | #endif |
| 861 | |
| 862 | err = 0; |
| 863 | |
| 864 | out: |
| 865 | brelse(bh); |
| 866 | |
| 867 | return err; |
| 868 | } |
| 869 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 870 | /* |
| 871 | * ntfs_fill_super - Try to mount. |
| 872 | */ |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 873 | static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 874 | { |
| 875 | int err; |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 876 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 877 | struct block_device *bdev = sb->s_bdev; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 878 | struct request_queue *rq = bdev_get_queue(bdev); |
Kari Argillander | 10b4f12 | 2021-09-09 21:09:36 +0300 | [diff] [blame] | 879 | struct inode *inode; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 880 | struct ntfs_inode *ni; |
| 881 | size_t i, tt; |
| 882 | CLST vcn, lcn, len; |
| 883 | struct ATTRIB *attr; |
| 884 | const struct VOLUME_INFO *info; |
| 885 | u32 idx, done, bytes; |
| 886 | struct ATTR_DEF_ENTRY *t; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 887 | u16 *shared; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 888 | struct MFT_REF ref; |
| 889 | |
| 890 | ref.high = 0; |
| 891 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 892 | sbi->sb = sb; |
| 893 | sb->s_flags |= SB_NODIRATIME; |
| 894 | sb->s_magic = 0x7366746e; // "ntfs" |
| 895 | sb->s_op = &ntfs_sops; |
| 896 | sb->s_export_op = &ntfs_export_ops; |
| 897 | sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec |
| 898 | sb->s_xattr = ntfs_xattr_handlers; |
| 899 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 900 | sbi->options->nls = ntfs_load_nls(sbi->options->nls_name); |
| 901 | if (IS_ERR(sbi->options->nls)) { |
| 902 | sbi->options->nls = NULL; |
| 903 | errorf(fc, "Cannot load nls %s", sbi->options->nls_name); |
| 904 | return -EINVAL; |
| 905 | } |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 906 | |
| 907 | if (!rq || !blk_queue_discard(rq) || !rq->limits.discard_granularity) { |
| 908 | ; |
| 909 | } else { |
| 910 | sbi->discard_granularity = rq->limits.discard_granularity; |
| 911 | sbi->discard_granularity_mask_inv = |
| 912 | ~(u64)(sbi->discard_granularity - 1); |
| 913 | } |
| 914 | |
| 915 | sb_set_blocksize(sb, PAGE_SIZE); |
| 916 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 917 | /* Parse boot. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 918 | err = ntfs_init_from_boot(sb, rq ? queue_logical_block_size(rq) : 512, |
Kari Argillander | 4ea41b3 | 2021-09-09 21:09:39 +0300 | [diff] [blame] | 919 | bdev->bd_inode->i_size); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 920 | if (err) |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 921 | return err; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 922 | |
| 923 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
| 924 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 925 | #else |
| 926 | sb->s_maxbytes = 0xFFFFFFFFull << sbi->cluster_bits; |
| 927 | #endif |
| 928 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 929 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 930 | * Load $Volume. This should be done before $LogFile |
| 931 | * 'cause 'sbi->volume.ni' is used 'ntfs_set_state'. |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 932 | */ |
| 933 | ref.low = cpu_to_le32(MFT_REC_VOL); |
| 934 | ref.seq = cpu_to_le16(MFT_REC_VOL); |
| 935 | inode = ntfs_iget5(sb, &ref, &NAME_VOLUME); |
| 936 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 937 | ntfs_err(sb, "Failed to load $Volume."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 938 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | ni = ntfs_i(inode); |
| 942 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 943 | /* Load and save label (not necessary). */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 944 | attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL); |
| 945 | |
| 946 | if (!attr) { |
| 947 | /* It is ok if no ATTR_LABEL */ |
| 948 | } else if (!attr->non_res && !is_attr_ext(attr)) { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 949 | /* $AttrDef allows labels to be up to 128 symbols. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 950 | err = utf16s_to_utf8s(resident_data(attr), |
| 951 | le32_to_cpu(attr->res.data_size) >> 1, |
| 952 | UTF16_LITTLE_ENDIAN, sbi->volume.label, |
| 953 | sizeof(sbi->volume.label)); |
| 954 | if (err < 0) |
| 955 | sbi->volume.label[0] = 0; |
| 956 | } else { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 957 | /* Should we break mounting here? */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 958 | //err = -EINVAL; |
| 959 | //goto out; |
| 960 | } |
| 961 | |
| 962 | attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL); |
| 963 | if (!attr || is_attr_ext(attr)) { |
| 964 | err = -EINVAL; |
| 965 | goto out; |
| 966 | } |
| 967 | |
| 968 | info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO); |
| 969 | if (!info) { |
| 970 | err = -EINVAL; |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | sbi->volume.major_ver = info->major_ver; |
| 975 | sbi->volume.minor_ver = info->minor_ver; |
| 976 | sbi->volume.flags = info->flags; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 977 | sbi->volume.ni = ni; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 978 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 979 | /* Load $MFTMirr to estimate recs_mirr. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 980 | ref.low = cpu_to_le32(MFT_REC_MIRR); |
| 981 | ref.seq = cpu_to_le16(MFT_REC_MIRR); |
| 982 | inode = ntfs_iget5(sb, &ref, &NAME_MIRROR); |
| 983 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 984 | ntfs_err(sb, "Failed to load $MFTMirr."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 985 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | sbi->mft.recs_mirr = |
| 989 | ntfs_up_cluster(sbi, inode->i_size) >> sbi->record_bits; |
| 990 | |
| 991 | iput(inode); |
| 992 | |
Konstantin Komarov | d362446 | 2021-08-31 16:57:40 +0300 | [diff] [blame] | 993 | /* Load LogFile to replay. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 994 | ref.low = cpu_to_le32(MFT_REC_LOG); |
| 995 | ref.seq = cpu_to_le16(MFT_REC_LOG); |
| 996 | inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE); |
| 997 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 998 | ntfs_err(sb, "Failed to load \x24LogFile."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 999 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | ni = ntfs_i(inode); |
| 1003 | |
| 1004 | err = ntfs_loadlog_and_replay(ni, sbi); |
| 1005 | if (err) |
| 1006 | goto out; |
| 1007 | |
| 1008 | iput(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1009 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1010 | if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) { |
Kari Argillander | 0cde7e81 | 2021-09-09 21:09:38 +0300 | [diff] [blame] | 1011 | if (!sb_rdonly(sb)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1012 | ntfs_warn(sb, |
| 1013 | "failed to replay log file. Can't mount rw!"); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1014 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1015 | } |
| 1016 | } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) { |
Kari Argillander | 0cde7e81 | 2021-09-09 21:09:38 +0300 | [diff] [blame] | 1017 | if (!sb_rdonly(sb) && !sbi->options->force) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1018 | ntfs_warn( |
| 1019 | sb, |
| 1020 | "volume is dirty and \"force\" flag is not set!"); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1021 | return -EINVAL; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1025 | /* Load $MFT. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1026 | ref.low = cpu_to_le32(MFT_REC_MFT); |
| 1027 | ref.seq = cpu_to_le16(1); |
| 1028 | |
| 1029 | inode = ntfs_iget5(sb, &ref, &NAME_MFT); |
| 1030 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1031 | ntfs_err(sb, "Failed to load $MFT."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1032 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | ni = ntfs_i(inode); |
| 1036 | |
| 1037 | sbi->mft.used = ni->i_valid >> sbi->record_bits; |
| 1038 | tt = inode->i_size >> sbi->record_bits; |
| 1039 | sbi->mft.next_free = MFT_REC_USER; |
| 1040 | |
| 1041 | err = wnd_init(&sbi->mft.bitmap, sb, tt); |
| 1042 | if (err) |
| 1043 | goto out; |
| 1044 | |
| 1045 | err = ni_load_all_mi(ni); |
| 1046 | if (err) |
| 1047 | goto out; |
| 1048 | |
| 1049 | sbi->mft.ni = ni; |
| 1050 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1051 | /* Load $BadClus. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1052 | ref.low = cpu_to_le32(MFT_REC_BADCLUST); |
| 1053 | ref.seq = cpu_to_le16(MFT_REC_BADCLUST); |
| 1054 | inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS); |
| 1055 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1056 | ntfs_err(sb, "Failed to load $BadClus."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1057 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | ni = ntfs_i(inode); |
| 1061 | |
| 1062 | for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) { |
| 1063 | if (lcn == SPARSE_LCN) |
| 1064 | continue; |
| 1065 | |
| 1066 | if (!sbi->bad_clusters) |
| 1067 | ntfs_notice(sb, "Volume contains bad blocks"); |
| 1068 | |
| 1069 | sbi->bad_clusters += len; |
| 1070 | } |
| 1071 | |
| 1072 | iput(inode); |
| 1073 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1074 | /* Load $Bitmap. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1075 | ref.low = cpu_to_le32(MFT_REC_BITMAP); |
| 1076 | ref.seq = cpu_to_le16(MFT_REC_BITMAP); |
| 1077 | inode = ntfs_iget5(sb, &ref, &NAME_BITMAP); |
| 1078 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1079 | ntfs_err(sb, "Failed to load $Bitmap."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1080 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1081 | } |
| 1082 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1083 | #ifndef CONFIG_NTFS3_64BIT_CLUSTER |
| 1084 | if (inode->i_size >> 32) { |
| 1085 | err = -EINVAL; |
| 1086 | goto out; |
| 1087 | } |
| 1088 | #endif |
| 1089 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1090 | /* Check bitmap boundary. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1091 | tt = sbi->used.bitmap.nbits; |
| 1092 | if (inode->i_size < bitmap_size(tt)) { |
| 1093 | err = -EINVAL; |
| 1094 | goto out; |
| 1095 | } |
| 1096 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1097 | /* Not necessary. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1098 | sbi->used.bitmap.set_tail = true; |
Kari Argillander | b4f110d | 2021-09-09 21:09:37 +0300 | [diff] [blame] | 1099 | err = wnd_init(&sbi->used.bitmap, sb, tt); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1100 | if (err) |
| 1101 | goto out; |
| 1102 | |
| 1103 | iput(inode); |
| 1104 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1105 | /* Compute the MFT zone. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1106 | err = ntfs_refresh_zone(sbi); |
| 1107 | if (err) |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1108 | return err; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1109 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1110 | /* Load $AttrDef. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1111 | ref.low = cpu_to_le32(MFT_REC_ATTR); |
| 1112 | ref.seq = cpu_to_le16(MFT_REC_ATTR); |
Kari Argillander | b4f110d | 2021-09-09 21:09:37 +0300 | [diff] [blame] | 1113 | inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1114 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1115 | ntfs_err(sb, "Failed to load $AttrDef -> %d", err); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1116 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY)) { |
| 1120 | err = -EINVAL; |
| 1121 | goto out; |
| 1122 | } |
| 1123 | bytes = inode->i_size; |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 1124 | sbi->def_table = t = kmalloc(bytes, GFP_NOFS); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1125 | if (!t) { |
| 1126 | err = -ENOMEM; |
| 1127 | goto out; |
| 1128 | } |
| 1129 | |
| 1130 | for (done = idx = 0; done < bytes; done += PAGE_SIZE, idx++) { |
| 1131 | unsigned long tail = bytes - done; |
| 1132 | struct page *page = ntfs_map_page(inode->i_mapping, idx); |
| 1133 | |
| 1134 | if (IS_ERR(page)) { |
| 1135 | err = PTR_ERR(page); |
| 1136 | goto out; |
| 1137 | } |
| 1138 | memcpy(Add2Ptr(t, done), page_address(page), |
| 1139 | min(PAGE_SIZE, tail)); |
| 1140 | ntfs_unmap_page(page); |
| 1141 | |
| 1142 | if (!idx && ATTR_STD != t->type) { |
| 1143 | err = -EINVAL; |
| 1144 | goto out; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | t += 1; |
| 1149 | sbi->def_entries = 1; |
| 1150 | done = sizeof(struct ATTR_DEF_ENTRY); |
| 1151 | sbi->reparse.max_size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE; |
Colin Ian King | f8d87ed | 2021-08-16 11:13:08 +0100 | [diff] [blame] | 1152 | sbi->ea_max_size = 0x10000; /* default formatter value */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1153 | |
| 1154 | while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) { |
| 1155 | u32 t32 = le32_to_cpu(t->type); |
| 1156 | u64 sz = le64_to_cpu(t->max_sz); |
| 1157 | |
| 1158 | if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32) |
| 1159 | break; |
| 1160 | |
| 1161 | if (t->type == ATTR_REPARSE) |
| 1162 | sbi->reparse.max_size = sz; |
| 1163 | else if (t->type == ATTR_EA) |
| 1164 | sbi->ea_max_size = sz; |
| 1165 | |
| 1166 | done += sizeof(struct ATTR_DEF_ENTRY); |
| 1167 | t += 1; |
| 1168 | sbi->def_entries += 1; |
| 1169 | } |
| 1170 | iput(inode); |
| 1171 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1172 | /* Load $UpCase. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1173 | ref.low = cpu_to_le32(MFT_REC_UPCASE); |
| 1174 | ref.seq = cpu_to_le16(MFT_REC_UPCASE); |
| 1175 | inode = ntfs_iget5(sb, &ref, &NAME_UPCASE); |
| 1176 | if (IS_ERR(inode)) { |
Kari Argillander | 0412016 | 2021-09-09 21:09:32 +0300 | [diff] [blame] | 1177 | ntfs_err(sb, "Failed to load $UpCase."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1178 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1179 | } |
| 1180 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1181 | if (inode->i_size != 0x10000 * sizeof(short)) { |
| 1182 | err = -EINVAL; |
| 1183 | goto out; |
| 1184 | } |
| 1185 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1186 | for (idx = 0; idx < (0x10000 * sizeof(short) >> PAGE_SHIFT); idx++) { |
| 1187 | const __le16 *src; |
Kari Argillander | 0056b27 | 2021-09-09 21:09:40 +0300 | [diff] [blame^] | 1188 | u16 *dst = Add2Ptr(sbi->upcase, idx << PAGE_SHIFT); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1189 | struct page *page = ntfs_map_page(inode->i_mapping, idx); |
| 1190 | |
| 1191 | if (IS_ERR(page)) { |
| 1192 | err = PTR_ERR(page); |
| 1193 | goto out; |
| 1194 | } |
| 1195 | |
| 1196 | src = page_address(page); |
| 1197 | |
| 1198 | #ifdef __BIG_ENDIAN |
| 1199 | for (i = 0; i < PAGE_SIZE / sizeof(u16); i++) |
| 1200 | *dst++ = le16_to_cpu(*src++); |
| 1201 | #else |
| 1202 | memcpy(dst, src, PAGE_SIZE); |
| 1203 | #endif |
| 1204 | ntfs_unmap_page(page); |
| 1205 | } |
| 1206 | |
Kari Argillander | 0056b27 | 2021-09-09 21:09:40 +0300 | [diff] [blame^] | 1207 | shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short)); |
| 1208 | if (shared && sbi->upcase != shared) { |
| 1209 | kvfree(sbi->upcase); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1210 | sbi->upcase = shared; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | iput(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1214 | |
| 1215 | if (is_ntfs3(sbi)) { |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1216 | /* Load $Secure. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1217 | err = ntfs_security_init(sbi); |
| 1218 | if (err) |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1219 | return err; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1220 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1221 | /* Load $Extend. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1222 | err = ntfs_extend_init(sbi); |
| 1223 | if (err) |
| 1224 | goto load_root; |
| 1225 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1226 | /* Load $Extend\$Reparse. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1227 | err = ntfs_reparse_init(sbi); |
| 1228 | if (err) |
| 1229 | goto load_root; |
| 1230 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1231 | /* Load $Extend\$ObjId. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1232 | err = ntfs_objid_init(sbi); |
| 1233 | if (err) |
| 1234 | goto load_root; |
| 1235 | } |
| 1236 | |
| 1237 | load_root: |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1238 | /* Load root. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1239 | ref.low = cpu_to_le32(MFT_REC_ROOT); |
| 1240 | ref.seq = cpu_to_le16(MFT_REC_ROOT); |
| 1241 | inode = ntfs_iget5(sb, &ref, &NAME_ROOT); |
| 1242 | if (IS_ERR(inode)) { |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1243 | ntfs_err(sb, "Failed to load root."); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1244 | return PTR_ERR(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1245 | } |
| 1246 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1247 | sb->s_root = d_make_root(inode); |
Kari Argillander | bce1828 | 2021-09-09 21:09:35 +0300 | [diff] [blame] | 1248 | if (!sb->s_root) |
| 1249 | return -ENOMEM; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1250 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1251 | fc->fs_private = NULL; |
| 1252 | fc->s_fs_info = NULL; |
| 1253 | |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1254 | return 0; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1255 | out: |
| 1256 | iput(inode); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1257 | return err; |
| 1258 | } |
| 1259 | |
| 1260 | void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len) |
| 1261 | { |
| 1262 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1263 | struct block_device *bdev = sb->s_bdev; |
| 1264 | sector_t devblock = (u64)lcn * sbi->blocks_per_cluster; |
| 1265 | unsigned long blocks = (u64)len * sbi->blocks_per_cluster; |
| 1266 | unsigned long cnt = 0; |
| 1267 | unsigned long limit = global_zone_page_state(NR_FREE_PAGES) |
| 1268 | << (PAGE_SHIFT - sb->s_blocksize_bits); |
| 1269 | |
| 1270 | if (limit >= 0x2000) |
| 1271 | limit -= 0x1000; |
| 1272 | else if (limit < 32) |
| 1273 | limit = 32; |
| 1274 | else |
| 1275 | limit >>= 1; |
| 1276 | |
| 1277 | while (blocks--) { |
| 1278 | clean_bdev_aliases(bdev, devblock++, 1); |
| 1279 | if (cnt++ >= limit) { |
| 1280 | sync_blockdev(bdev); |
| 1281 | cnt = 0; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /* |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1287 | * ntfs_discard - Issue a discard request (trim for SSD). |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1288 | */ |
| 1289 | int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len) |
| 1290 | { |
| 1291 | int err; |
| 1292 | u64 lbo, bytes, start, end; |
| 1293 | struct super_block *sb; |
| 1294 | |
| 1295 | if (sbi->used.next_free_lcn == lcn + len) |
| 1296 | sbi->used.next_free_lcn = lcn; |
| 1297 | |
| 1298 | if (sbi->flags & NTFS_FLAGS_NODISCARD) |
| 1299 | return -EOPNOTSUPP; |
| 1300 | |
Kari Argillander | 564c97b | 2021-09-07 18:35:51 +0300 | [diff] [blame] | 1301 | if (!sbi->options->discard) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1302 | return -EOPNOTSUPP; |
| 1303 | |
| 1304 | lbo = (u64)lcn << sbi->cluster_bits; |
| 1305 | bytes = (u64)len << sbi->cluster_bits; |
| 1306 | |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1307 | /* Align up 'start' on discard_granularity. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1308 | start = (lbo + sbi->discard_granularity - 1) & |
| 1309 | sbi->discard_granularity_mask_inv; |
Kari Argillander | e8b8e97 | 2021-08-03 14:57:09 +0300 | [diff] [blame] | 1310 | /* Align down 'end' on discard_granularity. */ |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1311 | end = (lbo + bytes) & sbi->discard_granularity_mask_inv; |
| 1312 | |
| 1313 | sb = sbi->sb; |
| 1314 | if (start >= end) |
| 1315 | return 0; |
| 1316 | |
| 1317 | err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9, |
| 1318 | GFP_NOFS, 0); |
| 1319 | |
| 1320 | if (err == -EOPNOTSUPP) |
| 1321 | sbi->flags |= NTFS_FLAGS_NODISCARD; |
| 1322 | |
| 1323 | return err; |
| 1324 | } |
| 1325 | |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1326 | static int ntfs_fs_get_tree(struct fs_context *fc) |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1327 | { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1328 | return get_tree_bdev(fc, ntfs_fill_super); |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * ntfs_fs_free - Free fs_context. |
| 1333 | * |
| 1334 | * Note that this will be called after fill_super and reconfigure |
| 1335 | * even when they pass. So they have to take pointers if they pass. |
| 1336 | */ |
| 1337 | static void ntfs_fs_free(struct fs_context *fc) |
| 1338 | { |
| 1339 | struct ntfs_mount_options *opts = fc->fs_private; |
| 1340 | struct ntfs_sb_info *sbi = fc->s_fs_info; |
| 1341 | |
| 1342 | if (sbi) |
| 1343 | put_ntfs(sbi); |
| 1344 | |
| 1345 | if (opts) |
| 1346 | put_mount_options(opts); |
| 1347 | } |
| 1348 | |
| 1349 | static const struct fs_context_operations ntfs_context_ops = { |
| 1350 | .parse_param = ntfs_fs_parse_param, |
| 1351 | .get_tree = ntfs_fs_get_tree, |
| 1352 | .reconfigure = ntfs_fs_reconfigure, |
| 1353 | .free = ntfs_fs_free, |
| 1354 | }; |
| 1355 | |
| 1356 | /* |
| 1357 | * ntfs_init_fs_context - Initialize spi and opts |
| 1358 | * |
| 1359 | * This will called when mount/remount. We will first initiliaze |
| 1360 | * options so that if remount we can use just that. |
| 1361 | */ |
| 1362 | static int ntfs_init_fs_context(struct fs_context *fc) |
| 1363 | { |
| 1364 | struct ntfs_mount_options *opts; |
| 1365 | struct ntfs_sb_info *sbi; |
| 1366 | |
| 1367 | opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS); |
| 1368 | if (!opts) |
| 1369 | return -ENOMEM; |
| 1370 | |
| 1371 | /* Default options. */ |
| 1372 | opts->fs_uid = current_uid(); |
| 1373 | opts->fs_gid = current_gid(); |
| 1374 | opts->fs_fmask_inv = ~current_umask(); |
| 1375 | opts->fs_dmask_inv = ~current_umask(); |
| 1376 | |
| 1377 | if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) |
| 1378 | goto ok; |
| 1379 | |
| 1380 | sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS); |
Kari Argillander | 27fac77 | 2021-09-07 18:35:53 +0300 | [diff] [blame] | 1381 | if (!sbi) |
| 1382 | goto free_opts; |
| 1383 | |
| 1384 | sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL); |
| 1385 | if (!sbi->upcase) |
| 1386 | goto free_sbi; |
| 1387 | |
| 1388 | ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL, |
| 1389 | DEFAULT_RATELIMIT_BURST); |
| 1390 | |
| 1391 | mutex_init(&sbi->compress.mtx_lznt); |
| 1392 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1393 | mutex_init(&sbi->compress.mtx_xpress); |
| 1394 | mutex_init(&sbi->compress.mtx_lzx); |
| 1395 | #endif |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1396 | |
| 1397 | sbi->options = opts; |
| 1398 | fc->s_fs_info = sbi; |
| 1399 | ok: |
| 1400 | fc->fs_private = opts; |
| 1401 | fc->ops = &ntfs_context_ops; |
| 1402 | |
| 1403 | return 0; |
Kari Argillander | 27fac77 | 2021-09-07 18:35:53 +0300 | [diff] [blame] | 1404 | free_opts: |
| 1405 | kfree(opts); |
| 1406 | free_sbi: |
| 1407 | kfree(sbi); |
| 1408 | return -ENOMEM; |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | // clang-format off |
| 1412 | static struct file_system_type ntfs_fs_type = { |
Kari Argillander | 610f8f5 | 2021-09-07 18:35:52 +0300 | [diff] [blame] | 1413 | .owner = THIS_MODULE, |
| 1414 | .name = "ntfs3", |
| 1415 | .init_fs_context = ntfs_init_fs_context, |
| 1416 | .parameters = ntfs_fs_parameters, |
| 1417 | .kill_sb = kill_block_super, |
| 1418 | .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1419 | }; |
| 1420 | // clang-format on |
| 1421 | |
| 1422 | static int __init init_ntfs_fs(void) |
| 1423 | { |
| 1424 | int err; |
| 1425 | |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1426 | pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1427 | |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1428 | if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL)) |
| 1429 | pr_info("ntfs3: Enabled Linux POSIX ACLs support\n"); |
| 1430 | if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER)) |
| 1431 | pr_notice("ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n"); |
| 1432 | if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS)) |
| 1433 | pr_info("ntfs3: Read-only LZX/Xpress compression included\n"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1434 | |
| 1435 | err = ntfs3_init_bitmap(); |
| 1436 | if (err) |
| 1437 | return err; |
| 1438 | |
| 1439 | ntfs_inode_cachep = kmem_cache_create( |
| 1440 | "ntfs_inode_cache", sizeof(struct ntfs_inode), 0, |
| 1441 | (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT), |
| 1442 | init_once); |
| 1443 | if (!ntfs_inode_cachep) { |
| 1444 | err = -ENOMEM; |
| 1445 | goto out1; |
| 1446 | } |
| 1447 | |
| 1448 | err = register_filesystem(&ntfs_fs_type); |
| 1449 | if (err) |
| 1450 | goto out; |
| 1451 | |
| 1452 | return 0; |
| 1453 | out: |
| 1454 | kmem_cache_destroy(ntfs_inode_cachep); |
| 1455 | out1: |
| 1456 | ntfs3_exit_bitmap(); |
| 1457 | return err; |
| 1458 | } |
| 1459 | |
| 1460 | static void __exit exit_ntfs_fs(void) |
| 1461 | { |
| 1462 | if (ntfs_inode_cachep) { |
| 1463 | rcu_barrier(); |
| 1464 | kmem_cache_destroy(ntfs_inode_cachep); |
| 1465 | } |
| 1466 | |
| 1467 | unregister_filesystem(&ntfs_fs_type); |
| 1468 | ntfs3_exit_bitmap(); |
| 1469 | } |
| 1470 | |
| 1471 | MODULE_LICENSE("GPL"); |
| 1472 | MODULE_DESCRIPTION("ntfs3 read/write filesystem"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1473 | #ifdef CONFIG_NTFS3_FS_POSIX_ACL |
| 1474 | MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support"); |
| 1475 | #endif |
| 1476 | #ifdef CONFIG_NTFS3_64BIT_CLUSTER |
Kari Argillander | 2e3a51b | 2021-08-29 17:42:39 +0300 | [diff] [blame] | 1477 | MODULE_INFO(cluster, "Warning: Activated 64 bits per cluster. Windows does not support this"); |
Konstantin Komarov | 82cae26 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1478 | #endif |
| 1479 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1480 | MODULE_INFO(compression, "Read-only lzx/xpress compression included"); |
| 1481 | #endif |
| 1482 | |
| 1483 | MODULE_AUTHOR("Konstantin Komarov"); |
| 1484 | MODULE_ALIAS_FS("ntfs3"); |
| 1485 | |
| 1486 | module_init(init_ntfs_fs); |
| 1487 | module_exit(exit_ntfs_fs); |