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