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