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