Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6cbd5570 CM |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
6cbd5570 CM |
4 | */ |
5 | ||
4b82d6e4 | 6 | #include <linux/blkdev.h> |
2e635a27 CM |
7 | #include <linux/module.h> |
8 | #include <linux/fs.h> | |
9 | #include <linux/pagemap.h> | |
10 | #include <linux/highmem.h> | |
11 | #include <linux/time.h> | |
12 | #include <linux/init.h> | |
a9572a15 | 13 | #include <linux/seq_file.h> |
2e635a27 | 14 | #include <linux/string.h> |
2e635a27 | 15 | #include <linux/backing-dev.h> |
4b82d6e4 | 16 | #include <linux/mount.h> |
75dfe396 | 17 | #include <linux/writeback.h> |
8fd17795 | 18 | #include <linux/statfs.h> |
08607c1b | 19 | #include <linux/compat.h> |
95e05289 | 20 | #include <linux/parser.h> |
c59f8951 | 21 | #include <linux/ctype.h> |
6da6abae | 22 | #include <linux/namei.h> |
a9218f6b | 23 | #include <linux/miscdevice.h> |
1bcbf313 | 24 | #include <linux/magic.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
22c44fe6 | 26 | #include <linux/ratelimit.h> |
9678c543 | 27 | #include <linux/crc32c.h> |
55e301fd | 28 | #include <linux/btrfs.h> |
9b569ea0 | 29 | #include "messages.h" |
16cdcec7 | 30 | #include "delayed-inode.h" |
2e635a27 | 31 | #include "ctree.h" |
e20d96d6 | 32 | #include "disk-io.h" |
d5719762 | 33 | #include "transaction.h" |
2c90e5d6 | 34 | #include "btrfs_inode.h" |
3a686375 | 35 | #include "print-tree.h" |
63541927 | 36 | #include "props.h" |
5103e947 | 37 | #include "xattr.h" |
103c1972 | 38 | #include "bio.h" |
be6e8dc0 | 39 | #include "export.h" |
c8b97818 | 40 | #include "compression.h" |
9c5085c1 | 41 | #include "rcu-string.h" |
8dabb742 | 42 | #include "dev-replace.h" |
74255aa0 | 43 | #include "free-space-cache.h" |
b9e9a6cb | 44 | #include "backref.h" |
8719aaae | 45 | #include "space-info.h" |
89439109 | 46 | #include "sysfs.h" |
b70f5097 | 47 | #include "zoned.h" |
dc11dd5d | 48 | #include "tests/btrfs-tests.h" |
aac0023c | 49 | #include "block-group.h" |
b0643e59 | 50 | #include "discard.h" |
d3982100 | 51 | #include "qgroup.h" |
b8bea09a | 52 | #include "raid56.h" |
c7f13d42 | 53 | #include "fs.h" |
07e81dc9 | 54 | #include "accessors.h" |
59b818e0 | 55 | #include "defrag.h" |
f2b39277 | 56 | #include "dir-item.h" |
7572dec8 | 57 | #include "ioctl.h" |
2fc6822c | 58 | #include "scrub.h" |
5c11adcc | 59 | #include "verity.h" |
c03b2207 | 60 | #include "super.h" |
cfc2de0f | 61 | #include "extent-tree.h" |
1abe9b8a | 62 | #define CREATE_TRACE_POINTS |
63 | #include <trace/events/btrfs.h> | |
64 | ||
b87221de | 65 | static const struct super_operations btrfs_super_ops; |
72fa39f5 MT |
66 | |
67 | /* | |
68 | * Types for mounting the default subvolume and a subvolume explicitly | |
69 | * requested by subvol=/path. That way the callchain is straightforward and we | |
70 | * don't have to play tricks with the mount options and recursive calls to | |
71 | * btrfs_mount. | |
312c89fb MT |
72 | * |
73 | * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. | |
72fa39f5 | 74 | */ |
830c4adb | 75 | static struct file_system_type btrfs_fs_type; |
72fa39f5 | 76 | static struct file_system_type btrfs_root_fs_type; |
75dfe396 | 77 | |
0723a047 HH |
78 | static int btrfs_remount(struct super_block *sb, int *flags, char *data); |
79 | ||
d397712b | 80 | static void btrfs_put_super(struct super_block *sb) |
b18c6685 | 81 | { |
6bccf3ab | 82 | close_ctree(btrfs_sb(sb)); |
75dfe396 CM |
83 | } |
84 | ||
95e05289 | 85 | enum { |
416a7202 DS |
86 | Opt_acl, Opt_noacl, |
87 | Opt_clear_cache, | |
88 | Opt_commit_interval, | |
89 | Opt_compress, | |
90 | Opt_compress_force, | |
91 | Opt_compress_force_type, | |
92 | Opt_compress_type, | |
93 | Opt_degraded, | |
94 | Opt_device, | |
95 | Opt_fatal_errors, | |
96 | Opt_flushoncommit, Opt_noflushoncommit, | |
416a7202 DS |
97 | Opt_max_inline, |
98 | Opt_barrier, Opt_nobarrier, | |
99 | Opt_datacow, Opt_nodatacow, | |
100 | Opt_datasum, Opt_nodatasum, | |
101 | Opt_defrag, Opt_nodefrag, | |
102 | Opt_discard, Opt_nodiscard, | |
b0643e59 | 103 | Opt_discard_mode, |
416a7202 DS |
104 | Opt_norecovery, |
105 | Opt_ratio, | |
106 | Opt_rescan_uuid_tree, | |
107 | Opt_skip_balance, | |
108 | Opt_space_cache, Opt_no_space_cache, | |
109 | Opt_space_cache_version, | |
110 | Opt_ssd, Opt_nossd, | |
111 | Opt_ssd_spread, Opt_nossd_spread, | |
112 | Opt_subvol, | |
37becec9 | 113 | Opt_subvol_empty, |
416a7202 DS |
114 | Opt_subvolid, |
115 | Opt_thread_pool, | |
116 | Opt_treelog, Opt_notreelog, | |
416a7202 DS |
117 | Opt_user_subvol_rm_allowed, |
118 | ||
74ef0018 QW |
119 | /* Rescue options */ |
120 | Opt_rescue, | |
121 | Opt_usebackuproot, | |
122 | Opt_nologreplay, | |
42437a63 | 123 | Opt_ignorebadroots, |
882dbe0c | 124 | Opt_ignoredatacsums, |
9037d3cb | 125 | Opt_rescue_all, |
74ef0018 | 126 | |
416a7202 | 127 | /* Deprecated options */ |
416a7202 | 128 | Opt_recovery, |
5297199a | 129 | Opt_inode_cache, Opt_noinode_cache, |
416a7202 DS |
130 | |
131 | /* Debugging options */ | |
132 | Opt_check_integrity, | |
70f6d82e | 133 | Opt_check_integrity_including_extent_data, |
416a7202 DS |
134 | Opt_check_integrity_print_mask, |
135 | Opt_enospc_debug, Opt_noenospc_debug, | |
d0bd4560 JB |
136 | #ifdef CONFIG_BTRFS_DEBUG |
137 | Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, | |
fb592373 JB |
138 | #endif |
139 | #ifdef CONFIG_BTRFS_FS_REF_VERIFY | |
140 | Opt_ref_verify, | |
d0bd4560 | 141 | #endif |
9555c6c1 | 142 | Opt_err, |
95e05289 CM |
143 | }; |
144 | ||
4d4ab6d6 | 145 | static const match_table_t tokens = { |
416a7202 DS |
146 | {Opt_acl, "acl"}, |
147 | {Opt_noacl, "noacl"}, | |
148 | {Opt_clear_cache, "clear_cache"}, | |
149 | {Opt_commit_interval, "commit=%u"}, | |
c8b97818 | 150 | {Opt_compress, "compress"}, |
261507a0 | 151 | {Opt_compress_type, "compress=%s"}, |
a555f810 | 152 | {Opt_compress_force, "compress-force"}, |
261507a0 | 153 | {Opt_compress_force_type, "compress-force=%s"}, |
416a7202 DS |
154 | {Opt_degraded, "degraded"}, |
155 | {Opt_device, "device=%s"}, | |
156 | {Opt_fatal_errors, "fatal_errors=%s"}, | |
dccae999 | 157 | {Opt_flushoncommit, "flushoncommit"}, |
2c9ee856 | 158 | {Opt_noflushoncommit, "noflushoncommit"}, |
416a7202 DS |
159 | {Opt_inode_cache, "inode_cache"}, |
160 | {Opt_noinode_cache, "noinode_cache"}, | |
161 | {Opt_max_inline, "max_inline=%s"}, | |
162 | {Opt_barrier, "barrier"}, | |
163 | {Opt_nobarrier, "nobarrier"}, | |
164 | {Opt_datacow, "datacow"}, | |
165 | {Opt_nodatacow, "nodatacow"}, | |
166 | {Opt_datasum, "datasum"}, | |
167 | {Opt_nodatasum, "nodatasum"}, | |
168 | {Opt_defrag, "autodefrag"}, | |
169 | {Opt_nodefrag, "noautodefrag"}, | |
e244a0ae | 170 | {Opt_discard, "discard"}, |
b0643e59 | 171 | {Opt_discard_mode, "discard=%s"}, |
e07a2ade | 172 | {Opt_nodiscard, "nodiscard"}, |
416a7202 DS |
173 | {Opt_norecovery, "norecovery"}, |
174 | {Opt_ratio, "metadata_ratio=%u"}, | |
175 | {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, | |
176 | {Opt_skip_balance, "skip_balance"}, | |
0af3d00b | 177 | {Opt_space_cache, "space_cache"}, |
8965593e | 178 | {Opt_no_space_cache, "nospace_cache"}, |
416a7202 DS |
179 | {Opt_space_cache_version, "space_cache=%s"}, |
180 | {Opt_ssd, "ssd"}, | |
181 | {Opt_nossd, "nossd"}, | |
182 | {Opt_ssd_spread, "ssd_spread"}, | |
183 | {Opt_nossd_spread, "nossd_spread"}, | |
184 | {Opt_subvol, "subvol=%s"}, | |
37becec9 | 185 | {Opt_subvol_empty, "subvol="}, |
416a7202 DS |
186 | {Opt_subvolid, "subvolid=%s"}, |
187 | {Opt_thread_pool, "thread_pool=%u"}, | |
188 | {Opt_treelog, "treelog"}, | |
189 | {Opt_notreelog, "notreelog"}, | |
416a7202 DS |
190 | {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, |
191 | ||
74ef0018 QW |
192 | /* Rescue options */ |
193 | {Opt_rescue, "rescue=%s"}, | |
194 | /* Deprecated, with alias rescue=nologreplay */ | |
195 | {Opt_nologreplay, "nologreplay"}, | |
196 | /* Deprecated, with alias rescue=usebackuproot */ | |
197 | {Opt_usebackuproot, "usebackuproot"}, | |
198 | ||
416a7202 | 199 | /* Deprecated options */ |
416a7202 | 200 | {Opt_recovery, "recovery"}, |
416a7202 DS |
201 | |
202 | /* Debugging options */ | |
21adbd5c SB |
203 | {Opt_check_integrity, "check_int"}, |
204 | {Opt_check_integrity_including_extent_data, "check_int_data"}, | |
02453bde | 205 | {Opt_check_integrity_print_mask, "check_int_print_mask=%u"}, |
416a7202 DS |
206 | {Opt_enospc_debug, "enospc_debug"}, |
207 | {Opt_noenospc_debug, "noenospc_debug"}, | |
d0bd4560 JB |
208 | #ifdef CONFIG_BTRFS_DEBUG |
209 | {Opt_fragment_data, "fragment=data"}, | |
210 | {Opt_fragment_metadata, "fragment=metadata"}, | |
211 | {Opt_fragment_all, "fragment=all"}, | |
fb592373 JB |
212 | #endif |
213 | #ifdef CONFIG_BTRFS_FS_REF_VERIFY | |
214 | {Opt_ref_verify, "ref_verify"}, | |
d0bd4560 | 215 | #endif |
33268eaf | 216 | {Opt_err, NULL}, |
95e05289 CM |
217 | }; |
218 | ||
74ef0018 QW |
219 | static const match_table_t rescue_tokens = { |
220 | {Opt_usebackuproot, "usebackuproot"}, | |
221 | {Opt_nologreplay, "nologreplay"}, | |
42437a63 JB |
222 | {Opt_ignorebadroots, "ignorebadroots"}, |
223 | {Opt_ignorebadroots, "ibadroots"}, | |
882dbe0c JB |
224 | {Opt_ignoredatacsums, "ignoredatacsums"}, |
225 | {Opt_ignoredatacsums, "idatacsums"}, | |
9037d3cb | 226 | {Opt_rescue_all, "all"}, |
74ef0018 QW |
227 | {Opt_err, NULL}, |
228 | }; | |
229 | ||
d70bf748 JB |
230 | static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt, |
231 | const char *opt_name) | |
232 | { | |
233 | if (fs_info->mount_opt & opt) { | |
234 | btrfs_err(fs_info, "%s must be used with ro mount option", | |
235 | opt_name); | |
236 | return true; | |
237 | } | |
238 | return false; | |
239 | } | |
240 | ||
74ef0018 QW |
241 | static int parse_rescue_options(struct btrfs_fs_info *info, const char *options) |
242 | { | |
243 | char *opts; | |
244 | char *orig; | |
245 | char *p; | |
246 | substring_t args[MAX_OPT_ARGS]; | |
247 | int ret = 0; | |
248 | ||
249 | opts = kstrdup(options, GFP_KERNEL); | |
250 | if (!opts) | |
251 | return -ENOMEM; | |
252 | orig = opts; | |
253 | ||
254 | while ((p = strsep(&opts, ":")) != NULL) { | |
255 | int token; | |
256 | ||
257 | if (!*p) | |
258 | continue; | |
259 | token = match_token(p, rescue_tokens, args); | |
260 | switch (token){ | |
261 | case Opt_usebackuproot: | |
262 | btrfs_info(info, | |
263 | "trying to use backup root at mount time"); | |
264 | btrfs_set_opt(info->mount_opt, USEBACKUPROOT); | |
265 | break; | |
266 | case Opt_nologreplay: | |
267 | btrfs_set_and_info(info, NOLOGREPLAY, | |
268 | "disabling log replay at mount time"); | |
269 | break; | |
42437a63 JB |
270 | case Opt_ignorebadroots: |
271 | btrfs_set_and_info(info, IGNOREBADROOTS, | |
272 | "ignoring bad roots"); | |
273 | break; | |
882dbe0c JB |
274 | case Opt_ignoredatacsums: |
275 | btrfs_set_and_info(info, IGNOREDATACSUMS, | |
276 | "ignoring data csums"); | |
277 | break; | |
9037d3cb JB |
278 | case Opt_rescue_all: |
279 | btrfs_info(info, "enabling all of the rescue options"); | |
280 | btrfs_set_and_info(info, IGNOREDATACSUMS, | |
281 | "ignoring data csums"); | |
282 | btrfs_set_and_info(info, IGNOREBADROOTS, | |
283 | "ignoring bad roots"); | |
284 | btrfs_set_and_info(info, NOLOGREPLAY, | |
285 | "disabling log replay at mount time"); | |
286 | break; | |
74ef0018 QW |
287 | case Opt_err: |
288 | btrfs_info(info, "unrecognized rescue option '%s'", p); | |
289 | ret = -EINVAL; | |
290 | goto out; | |
291 | default: | |
292 | break; | |
293 | } | |
294 | ||
295 | } | |
296 | out: | |
297 | kfree(orig); | |
298 | return ret; | |
299 | } | |
300 | ||
edf24abe CH |
301 | /* |
302 | * Regular mount options parser. Everything that is needed only when | |
303 | * reading in a new superblock is parsed here. | |
49b25e05 | 304 | * XXX JDM: This needs to be cleaned up for remount. |
edf24abe | 305 | */ |
2ff7e61e | 306 | int btrfs_parse_options(struct btrfs_fs_info *info, char *options, |
96da0919 | 307 | unsigned long new_flags) |
95e05289 | 308 | { |
95e05289 | 309 | substring_t args[MAX_OPT_ARGS]; |
e215772c | 310 | char *p, *num; |
4543df7e | 311 | int intarg; |
a7a3f7ca | 312 | int ret = 0; |
261507a0 LZ |
313 | char *compress_type; |
314 | bool compress_force = false; | |
b7c47bbb | 315 | enum btrfs_compression_type saved_compress_type; |
27942c99 | 316 | int saved_compress_level; |
b7c47bbb TI |
317 | bool saved_compress_force; |
318 | int no_compress = 0; | |
dbecac26 | 319 | const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state); |
b6cda9bc | 320 | |
0b246afa | 321 | if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) |
70f6d82e | 322 | btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); |
94846229 | 323 | else if (btrfs_free_space_cache_v1_active(info)) { |
5d1ab66c NA |
324 | if (btrfs_is_zoned(info)) { |
325 | btrfs_info(info, | |
326 | "zoned: clearing existing space cache"); | |
327 | btrfs_set_super_cache_generation(info->super_copy, 0); | |
328 | } else { | |
329 | btrfs_set_opt(info->mount_opt, SPACE_CACHE); | |
330 | } | |
331 | } | |
73bc1876 | 332 | |
96da0919 QW |
333 | /* |
334 | * Even the options are empty, we still need to do extra check | |
335 | * against new flags | |
336 | */ | |
95e05289 | 337 | if (!options) |
96da0919 | 338 | goto check; |
95e05289 | 339 | |
edf24abe | 340 | while ((p = strsep(&options, ",")) != NULL) { |
95e05289 CM |
341 | int token; |
342 | if (!*p) | |
343 | continue; | |
344 | ||
345 | token = match_token(p, tokens, args); | |
346 | switch (token) { | |
dfe25020 | 347 | case Opt_degraded: |
0b246afa | 348 | btrfs_info(info, "allowing degraded mounts"); |
edf24abe | 349 | btrfs_set_opt(info->mount_opt, DEGRADED); |
dfe25020 | 350 | break; |
95e05289 | 351 | case Opt_subvol: |
37becec9 | 352 | case Opt_subvol_empty: |
73f73415 | 353 | case Opt_subvolid: |
43e570b0 | 354 | case Opt_device: |
edf24abe | 355 | /* |
fa59f27c AJ |
356 | * These are parsed by btrfs_parse_subvol_options or |
357 | * btrfs_parse_device_options and can be ignored here. | |
edf24abe | 358 | */ |
b6cda9bc CM |
359 | break; |
360 | case Opt_nodatasum: | |
3cdde224 | 361 | btrfs_set_and_info(info, NODATASUM, |
07802534 | 362 | "setting nodatasum"); |
be20aa9d | 363 | break; |
d399167d | 364 | case Opt_datasum: |
3cdde224 JM |
365 | if (btrfs_test_opt(info, NODATASUM)) { |
366 | if (btrfs_test_opt(info, NODATACOW)) | |
0b246afa | 367 | btrfs_info(info, |
5d163e0e | 368 | "setting datasum, datacow enabled"); |
07802534 | 369 | else |
0b246afa | 370 | btrfs_info(info, "setting datasum"); |
07802534 | 371 | } |
d399167d QW |
372 | btrfs_clear_opt(info->mount_opt, NODATACOW); |
373 | btrfs_clear_opt(info->mount_opt, NODATASUM); | |
374 | break; | |
be20aa9d | 375 | case Opt_nodatacow: |
3cdde224 JM |
376 | if (!btrfs_test_opt(info, NODATACOW)) { |
377 | if (!btrfs_test_opt(info, COMPRESS) || | |
378 | !btrfs_test_opt(info, FORCE_COMPRESS)) { | |
0b246afa | 379 | btrfs_info(info, |
07802534 QW |
380 | "setting nodatacow, compression disabled"); |
381 | } else { | |
0b246afa | 382 | btrfs_info(info, "setting nodatacow"); |
07802534 | 383 | } |
bedb2cca | 384 | } |
bedb2cca AP |
385 | btrfs_clear_opt(info->mount_opt, COMPRESS); |
386 | btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); | |
edf24abe CH |
387 | btrfs_set_opt(info->mount_opt, NODATACOW); |
388 | btrfs_set_opt(info->mount_opt, NODATASUM); | |
95e05289 | 389 | break; |
a258af7a | 390 | case Opt_datacow: |
3cdde224 | 391 | btrfs_clear_and_info(info, NODATACOW, |
07802534 | 392 | "setting datacow"); |
a258af7a | 393 | break; |
a555f810 | 394 | case Opt_compress_force: |
261507a0 LZ |
395 | case Opt_compress_force_type: |
396 | compress_force = true; | |
c730ae0c | 397 | fallthrough; |
261507a0 LZ |
398 | case Opt_compress: |
399 | case Opt_compress_type: | |
3cdde224 JM |
400 | saved_compress_type = btrfs_test_opt(info, |
401 | COMPRESS) ? | |
b7c47bbb TI |
402 | info->compress_type : BTRFS_COMPRESS_NONE; |
403 | saved_compress_force = | |
3cdde224 | 404 | btrfs_test_opt(info, FORCE_COMPRESS); |
27942c99 | 405 | saved_compress_level = info->compress_level; |
261507a0 LZ |
406 | if (token == Opt_compress || |
407 | token == Opt_compress_force || | |
a7164fa4 | 408 | strncmp(args[0].from, "zlib", 4) == 0) { |
261507a0 | 409 | compress_type = "zlib"; |
eae8d825 | 410 | |
261507a0 | 411 | info->compress_type = BTRFS_COMPRESS_ZLIB; |
eae8d825 QW |
412 | info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; |
413 | /* | |
414 | * args[0] contains uninitialized data since | |
415 | * for these tokens we don't expect any | |
416 | * parameter. | |
417 | */ | |
418 | if (token != Opt_compress && | |
419 | token != Opt_compress_force) | |
420 | info->compress_level = | |
d0ab62ce DZ |
421 | btrfs_compress_str2level( |
422 | BTRFS_COMPRESS_ZLIB, | |
423 | args[0].from + 4); | |
063849ea | 424 | btrfs_set_opt(info->mount_opt, COMPRESS); |
bedb2cca AP |
425 | btrfs_clear_opt(info->mount_opt, NODATACOW); |
426 | btrfs_clear_opt(info->mount_opt, NODATASUM); | |
b7c47bbb | 427 | no_compress = 0; |
a7164fa4 | 428 | } else if (strncmp(args[0].from, "lzo", 3) == 0) { |
a6fa6fae LZ |
429 | compress_type = "lzo"; |
430 | info->compress_type = BTRFS_COMPRESS_LZO; | |
282dd7d7 | 431 | info->compress_level = 0; |
063849ea | 432 | btrfs_set_opt(info->mount_opt, COMPRESS); |
bedb2cca AP |
433 | btrfs_clear_opt(info->mount_opt, NODATACOW); |
434 | btrfs_clear_opt(info->mount_opt, NODATASUM); | |
2b0ce2c2 | 435 | btrfs_set_fs_incompat(info, COMPRESS_LZO); |
b7c47bbb | 436 | no_compress = 0; |
3f93aef5 | 437 | } else if (strncmp(args[0].from, "zstd", 4) == 0) { |
5c1aab1d NT |
438 | compress_type = "zstd"; |
439 | info->compress_type = BTRFS_COMPRESS_ZSTD; | |
3f93aef5 DZ |
440 | info->compress_level = |
441 | btrfs_compress_str2level( | |
442 | BTRFS_COMPRESS_ZSTD, | |
443 | args[0].from + 4); | |
5c1aab1d NT |
444 | btrfs_set_opt(info->mount_opt, COMPRESS); |
445 | btrfs_clear_opt(info->mount_opt, NODATACOW); | |
446 | btrfs_clear_opt(info->mount_opt, NODATASUM); | |
447 | btrfs_set_fs_incompat(info, COMPRESS_ZSTD); | |
448 | no_compress = 0; | |
063849ea AH |
449 | } else if (strncmp(args[0].from, "no", 2) == 0) { |
450 | compress_type = "no"; | |
27942c99 DS |
451 | info->compress_level = 0; |
452 | info->compress_type = 0; | |
063849ea AH |
453 | btrfs_clear_opt(info->mount_opt, COMPRESS); |
454 | btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); | |
455 | compress_force = false; | |
b7c47bbb | 456 | no_compress++; |
261507a0 | 457 | } else { |
e3a4167c DS |
458 | btrfs_err(info, "unrecognized compression value %s", |
459 | args[0].from); | |
261507a0 LZ |
460 | ret = -EINVAL; |
461 | goto out; | |
462 | } | |
463 | ||
261507a0 | 464 | if (compress_force) { |
b7c47bbb | 465 | btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); |
143f3636 | 466 | } else { |
4027e0f4 WS |
467 | /* |
468 | * If we remount from compress-force=xxx to | |
469 | * compress=xxx, we need clear FORCE_COMPRESS | |
470 | * flag, otherwise, there is no way for users | |
471 | * to disable forcible compression separately. | |
472 | */ | |
473 | btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); | |
a7e252af | 474 | } |
27942c99 DS |
475 | if (no_compress == 1) { |
476 | btrfs_info(info, "use no compression"); | |
477 | } else if ((info->compress_type != saved_compress_type) || | |
478 | (compress_force != saved_compress_force) || | |
479 | (info->compress_level != saved_compress_level)) { | |
f51d2b59 | 480 | btrfs_info(info, "%s %s compression, level %d", |
b7c47bbb | 481 | (compress_force) ? "force" : "use", |
f51d2b59 | 482 | compress_type, info->compress_level); |
b7c47bbb TI |
483 | } |
484 | compress_force = false; | |
a555f810 | 485 | break; |
e18e4809 | 486 | case Opt_ssd: |
3cdde224 | 487 | btrfs_set_and_info(info, SSD, |
583b7231 | 488 | "enabling ssd optimizations"); |
951e7966 | 489 | btrfs_clear_opt(info->mount_opt, NOSSD); |
e18e4809 | 490 | break; |
451d7585 | 491 | case Opt_ssd_spread: |
583b7231 HK |
492 | btrfs_set_and_info(info, SSD, |
493 | "enabling ssd optimizations"); | |
3cdde224 | 494 | btrfs_set_and_info(info, SSD_SPREAD, |
583b7231 | 495 | "using spread ssd allocation scheme"); |
951e7966 | 496 | btrfs_clear_opt(info->mount_opt, NOSSD); |
451d7585 | 497 | break; |
3b30c22f | 498 | case Opt_nossd: |
583b7231 HK |
499 | btrfs_set_opt(info->mount_opt, NOSSD); |
500 | btrfs_clear_and_info(info, SSD, | |
501 | "not using ssd optimizations"); | |
c730ae0c | 502 | fallthrough; |
62b8e077 | 503 | case Opt_nossd_spread: |
583b7231 HK |
504 | btrfs_clear_and_info(info, SSD_SPREAD, |
505 | "not using spread ssd allocation scheme"); | |
3b30c22f | 506 | break; |
842bef58 | 507 | case Opt_barrier: |
3cdde224 | 508 | btrfs_clear_and_info(info, NOBARRIER, |
07802534 | 509 | "turning on barriers"); |
842bef58 | 510 | break; |
21ad10cf | 511 | case Opt_nobarrier: |
3cdde224 | 512 | btrfs_set_and_info(info, NOBARRIER, |
07802534 | 513 | "turning off barriers"); |
21ad10cf | 514 | break; |
4543df7e | 515 | case Opt_thread_pool: |
2c334e87 WS |
516 | ret = match_int(&args[0], &intarg); |
517 | if (ret) { | |
e3a4167c DS |
518 | btrfs_err(info, "unrecognized thread_pool value %s", |
519 | args[0].from); | |
2c334e87 | 520 | goto out; |
f7b885be | 521 | } else if (intarg == 0) { |
e3a4167c | 522 | btrfs_err(info, "invalid value 0 for thread_pool"); |
2c334e87 WS |
523 | ret = -EINVAL; |
524 | goto out; | |
525 | } | |
f7b885be | 526 | info->thread_pool_size = intarg; |
4543df7e | 527 | break; |
6f568d35 | 528 | case Opt_max_inline: |
edf24abe CH |
529 | num = match_strdup(&args[0]); |
530 | if (num) { | |
91748467 | 531 | info->max_inline = memparse(num, NULL); |
edf24abe CH |
532 | kfree(num); |
533 | ||
15ada040 | 534 | if (info->max_inline) { |
feb5f965 | 535 | info->max_inline = min_t(u64, |
15ada040 | 536 | info->max_inline, |
0b246afa | 537 | info->sectorsize); |
15ada040 | 538 | } |
0b246afa JM |
539 | btrfs_info(info, "max_inline at %llu", |
540 | info->max_inline); | |
2c334e87 WS |
541 | } else { |
542 | ret = -ENOMEM; | |
543 | goto out; | |
6f568d35 CM |
544 | } |
545 | break; | |
bd0330ad | 546 | case Opt_acl: |
45ff35d6 | 547 | #ifdef CONFIG_BTRFS_FS_POSIX_ACL |
1751e8a6 | 548 | info->sb->s_flags |= SB_POSIXACL; |
bd0330ad | 549 | break; |
45ff35d6 | 550 | #else |
0b246afa | 551 | btrfs_err(info, "support for ACL not compiled in!"); |
45ff35d6 GZ |
552 | ret = -EINVAL; |
553 | goto out; | |
554 | #endif | |
33268eaf | 555 | case Opt_noacl: |
1751e8a6 | 556 | info->sb->s_flags &= ~SB_POSIXACL; |
33268eaf | 557 | break; |
3a5e1404 | 558 | case Opt_notreelog: |
3cdde224 | 559 | btrfs_set_and_info(info, NOTREELOG, |
07802534 | 560 | "disabling tree log"); |
a88998f2 QW |
561 | break; |
562 | case Opt_treelog: | |
3cdde224 | 563 | btrfs_clear_and_info(info, NOTREELOG, |
07802534 | 564 | "enabling tree log"); |
3a5e1404 | 565 | break; |
fed8f166 | 566 | case Opt_norecovery: |
96da0919 | 567 | case Opt_nologreplay: |
74ef0018 QW |
568 | btrfs_warn(info, |
569 | "'nologreplay' is deprecated, use 'rescue=nologreplay' instead"); | |
3cdde224 | 570 | btrfs_set_and_info(info, NOLOGREPLAY, |
96da0919 QW |
571 | "disabling log replay at mount time"); |
572 | break; | |
dccae999 | 573 | case Opt_flushoncommit: |
3cdde224 | 574 | btrfs_set_and_info(info, FLUSHONCOMMIT, |
07802534 | 575 | "turning on flush-on-commit"); |
dccae999 | 576 | break; |
2c9ee856 | 577 | case Opt_noflushoncommit: |
3cdde224 | 578 | btrfs_clear_and_info(info, FLUSHONCOMMIT, |
07802534 | 579 | "turning off flush-on-commit"); |
2c9ee856 | 580 | break; |
97e728d4 | 581 | case Opt_ratio: |
2c334e87 | 582 | ret = match_int(&args[0], &intarg); |
e3a4167c DS |
583 | if (ret) { |
584 | btrfs_err(info, "unrecognized metadata_ratio value %s", | |
585 | args[0].from); | |
2c334e87 | 586 | goto out; |
e3a4167c | 587 | } |
764cb8b4 AJ |
588 | info->metadata_ratio = intarg; |
589 | btrfs_info(info, "metadata ratio %u", | |
590 | info->metadata_ratio); | |
97e728d4 | 591 | break; |
e244a0ae | 592 | case Opt_discard: |
b0643e59 DZ |
593 | case Opt_discard_mode: |
594 | if (token == Opt_discard || | |
595 | strcmp(args[0].from, "sync") == 0) { | |
596 | btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC); | |
597 | btrfs_set_and_info(info, DISCARD_SYNC, | |
598 | "turning on sync discard"); | |
599 | } else if (strcmp(args[0].from, "async") == 0) { | |
600 | btrfs_clear_opt(info->mount_opt, DISCARD_SYNC); | |
601 | btrfs_set_and_info(info, DISCARD_ASYNC, | |
602 | "turning on async discard"); | |
603 | } else { | |
e3a4167c DS |
604 | btrfs_err(info, "unrecognized discard mode value %s", |
605 | args[0].from); | |
b0643e59 DZ |
606 | ret = -EINVAL; |
607 | goto out; | |
608 | } | |
63a7cb13 | 609 | btrfs_clear_opt(info->mount_opt, NODISCARD); |
e244a0ae | 610 | break; |
e07a2ade | 611 | case Opt_nodiscard: |
46b27f50 | 612 | btrfs_clear_and_info(info, DISCARD_SYNC, |
07802534 | 613 | "turning off discard"); |
b0643e59 DZ |
614 | btrfs_clear_and_info(info, DISCARD_ASYNC, |
615 | "turning off async discard"); | |
63a7cb13 | 616 | btrfs_set_opt(info->mount_opt, NODISCARD); |
e07a2ade | 617 | break; |
0af3d00b | 618 | case Opt_space_cache: |
70f6d82e | 619 | case Opt_space_cache_version: |
63cd070d JB |
620 | /* |
621 | * We already set FREE_SPACE_TREE above because we have | |
622 | * compat_ro(FREE_SPACE_TREE) set, and we aren't going | |
623 | * to allow v1 to be set for extent tree v2, simply | |
624 | * ignore this setting if we're extent tree v2. | |
625 | */ | |
626 | if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) | |
627 | break; | |
70f6d82e OS |
628 | if (token == Opt_space_cache || |
629 | strcmp(args[0].from, "v1") == 0) { | |
0b246afa | 630 | btrfs_clear_opt(info->mount_opt, |
70f6d82e | 631 | FREE_SPACE_TREE); |
3cdde224 | 632 | btrfs_set_and_info(info, SPACE_CACHE, |
0b246afa | 633 | "enabling disk space caching"); |
70f6d82e | 634 | } else if (strcmp(args[0].from, "v2") == 0) { |
0b246afa | 635 | btrfs_clear_opt(info->mount_opt, |
70f6d82e | 636 | SPACE_CACHE); |
0b246afa | 637 | btrfs_set_and_info(info, FREE_SPACE_TREE, |
70f6d82e OS |
638 | "enabling free space tree"); |
639 | } else { | |
e3a4167c DS |
640 | btrfs_err(info, "unrecognized space_cache value %s", |
641 | args[0].from); | |
70f6d82e OS |
642 | ret = -EINVAL; |
643 | goto out; | |
644 | } | |
0de90876 | 645 | break; |
f420ee1e SB |
646 | case Opt_rescan_uuid_tree: |
647 | btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); | |
648 | break; | |
73bc1876 | 649 | case Opt_no_space_cache: |
63cd070d JB |
650 | /* |
651 | * We cannot operate without the free space tree with | |
652 | * extent tree v2, ignore this option. | |
653 | */ | |
654 | if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) | |
655 | break; | |
3cdde224 | 656 | if (btrfs_test_opt(info, SPACE_CACHE)) { |
0b246afa JM |
657 | btrfs_clear_and_info(info, SPACE_CACHE, |
658 | "disabling disk space caching"); | |
70f6d82e | 659 | } |
3cdde224 | 660 | if (btrfs_test_opt(info, FREE_SPACE_TREE)) { |
0b246afa JM |
661 | btrfs_clear_and_info(info, FREE_SPACE_TREE, |
662 | "disabling free space tree"); | |
70f6d82e | 663 | } |
73bc1876 | 664 | break; |
4b9465cb | 665 | case Opt_inode_cache: |
3818aea2 | 666 | case Opt_noinode_cache: |
5297199a NB |
667 | btrfs_warn(info, |
668 | "the 'inode_cache' option is deprecated and has no effect since 5.11"); | |
4b9465cb | 669 | break; |
88c2ba3b | 670 | case Opt_clear_cache: |
63cd070d JB |
671 | /* |
672 | * We cannot clear the free space tree with extent tree | |
673 | * v2, ignore this option. | |
674 | */ | |
675 | if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) | |
676 | break; | |
3cdde224 | 677 | btrfs_set_and_info(info, CLEAR_CACHE, |
07802534 | 678 | "force clearing of disk cache"); |
0af3d00b | 679 | break; |
4260f7c7 SW |
680 | case Opt_user_subvol_rm_allowed: |
681 | btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); | |
682 | break; | |
91435650 CM |
683 | case Opt_enospc_debug: |
684 | btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); | |
685 | break; | |
53036293 QW |
686 | case Opt_noenospc_debug: |
687 | btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); | |
688 | break; | |
4cb5300b | 689 | case Opt_defrag: |
3cdde224 | 690 | btrfs_set_and_info(info, AUTO_DEFRAG, |
07802534 | 691 | "enabling auto defrag"); |
4cb5300b | 692 | break; |
fc0ca9af | 693 | case Opt_nodefrag: |
3cdde224 | 694 | btrfs_clear_and_info(info, AUTO_DEFRAG, |
07802534 | 695 | "disabling auto defrag"); |
fc0ca9af | 696 | break; |
af31f5e5 | 697 | case Opt_recovery: |
8dcddfa0 | 698 | case Opt_usebackuproot: |
74ef0018 QW |
699 | btrfs_warn(info, |
700 | "'%s' is deprecated, use 'rescue=usebackuproot' instead", | |
701 | token == Opt_recovery ? "recovery" : | |
702 | "usebackuproot"); | |
0b246afa | 703 | btrfs_info(info, |
8dcddfa0 QW |
704 | "trying to use backup root at mount time"); |
705 | btrfs_set_opt(info->mount_opt, USEBACKUPROOT); | |
af31f5e5 | 706 | break; |
9555c6c1 ID |
707 | case Opt_skip_balance: |
708 | btrfs_set_opt(info->mount_opt, SKIP_BALANCE); | |
709 | break; | |
21adbd5c SB |
710 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
711 | case Opt_check_integrity_including_extent_data: | |
16c3a476 QW |
712 | btrfs_warn(info, |
713 | "integrity checker is deprecated and will be removed in 6.7"); | |
0b246afa | 714 | btrfs_info(info, |
efe120a0 | 715 | "enabling check integrity including extent data"); |
cbeaae4f | 716 | btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA); |
21adbd5c SB |
717 | btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); |
718 | break; | |
719 | case Opt_check_integrity: | |
16c3a476 QW |
720 | btrfs_warn(info, |
721 | "integrity checker is deprecated and will be removed in 6.7"); | |
0b246afa | 722 | btrfs_info(info, "enabling check integrity"); |
21adbd5c SB |
723 | btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); |
724 | break; | |
725 | case Opt_check_integrity_print_mask: | |
2c334e87 | 726 | ret = match_int(&args[0], &intarg); |
e3a4167c DS |
727 | if (ret) { |
728 | btrfs_err(info, | |
729 | "unrecognized check_integrity_print_mask value %s", | |
730 | args[0].from); | |
2c334e87 | 731 | goto out; |
e3a4167c | 732 | } |
02453bde | 733 | info->check_integrity_print_mask = intarg; |
16c3a476 QW |
734 | btrfs_warn(info, |
735 | "integrity checker is deprecated and will be removed in 6.7"); | |
02453bde AJ |
736 | btrfs_info(info, "check_integrity_print_mask 0x%x", |
737 | info->check_integrity_print_mask); | |
21adbd5c SB |
738 | break; |
739 | #else | |
740 | case Opt_check_integrity_including_extent_data: | |
741 | case Opt_check_integrity: | |
742 | case Opt_check_integrity_print_mask: | |
0b246afa JM |
743 | btrfs_err(info, |
744 | "support for check_integrity* not compiled in!"); | |
21adbd5c SB |
745 | ret = -EINVAL; |
746 | goto out; | |
747 | #endif | |
8c342930 | 748 | case Opt_fatal_errors: |
e3a4167c | 749 | if (strcmp(args[0].from, "panic") == 0) { |
8c342930 JM |
750 | btrfs_set_opt(info->mount_opt, |
751 | PANIC_ON_FATAL_ERROR); | |
e3a4167c | 752 | } else if (strcmp(args[0].from, "bug") == 0) { |
8c342930 JM |
753 | btrfs_clear_opt(info->mount_opt, |
754 | PANIC_ON_FATAL_ERROR); | |
e3a4167c DS |
755 | } else { |
756 | btrfs_err(info, "unrecognized fatal_errors value %s", | |
757 | args[0].from); | |
8c342930 JM |
758 | ret = -EINVAL; |
759 | goto out; | |
760 | } | |
761 | break; | |
8b87dc17 DS |
762 | case Opt_commit_interval: |
763 | intarg = 0; | |
764 | ret = match_int(&args[0], &intarg); | |
e3a4167c DS |
765 | if (ret) { |
766 | btrfs_err(info, "unrecognized commit_interval value %s", | |
767 | args[0].from); | |
768 | ret = -EINVAL; | |
8b87dc17 | 769 | goto out; |
e3a4167c | 770 | } |
d3740608 | 771 | if (intarg == 0) { |
0b246afa | 772 | btrfs_info(info, |
d3740608 | 773 | "using default commit interval %us", |
5d163e0e | 774 | BTRFS_DEFAULT_COMMIT_INTERVAL); |
d3740608 AJ |
775 | intarg = BTRFS_DEFAULT_COMMIT_INTERVAL; |
776 | } else if (intarg > 300) { | |
777 | btrfs_warn(info, "excessive commit interval %d", | |
778 | intarg); | |
8b87dc17 | 779 | } |
d3740608 | 780 | info->commit_interval = intarg; |
8b87dc17 | 781 | break; |
74ef0018 QW |
782 | case Opt_rescue: |
783 | ret = parse_rescue_options(info, args[0].from); | |
e3a4167c DS |
784 | if (ret < 0) { |
785 | btrfs_err(info, "unrecognized rescue value %s", | |
786 | args[0].from); | |
74ef0018 | 787 | goto out; |
e3a4167c | 788 | } |
74ef0018 | 789 | break; |
d0bd4560 JB |
790 | #ifdef CONFIG_BTRFS_DEBUG |
791 | case Opt_fragment_all: | |
0b246afa | 792 | btrfs_info(info, "fragmenting all space"); |
d0bd4560 JB |
793 | btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); |
794 | btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); | |
795 | break; | |
796 | case Opt_fragment_metadata: | |
0b246afa | 797 | btrfs_info(info, "fragmenting metadata"); |
d0bd4560 JB |
798 | btrfs_set_opt(info->mount_opt, |
799 | FRAGMENT_METADATA); | |
800 | break; | |
801 | case Opt_fragment_data: | |
0b246afa | 802 | btrfs_info(info, "fragmenting data"); |
d0bd4560 JB |
803 | btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); |
804 | break; | |
fb592373 JB |
805 | #endif |
806 | #ifdef CONFIG_BTRFS_FS_REF_VERIFY | |
807 | case Opt_ref_verify: | |
808 | btrfs_info(info, "doing ref verification"); | |
809 | btrfs_set_opt(info->mount_opt, REF_VERIFY); | |
810 | break; | |
d0bd4560 | 811 | #endif |
a7a3f7ca | 812 | case Opt_err: |
7e8f19e5 | 813 | btrfs_err(info, "unrecognized mount option '%s'", p); |
a7a3f7ca SW |
814 | ret = -EINVAL; |
815 | goto out; | |
95e05289 | 816 | default: |
be20aa9d | 817 | break; |
95e05289 CM |
818 | } |
819 | } | |
96da0919 | 820 | check: |
d70bf748 JB |
821 | /* We're read-only, don't have to check. */ |
822 | if (new_flags & SB_RDONLY) | |
823 | goto out; | |
824 | ||
42437a63 | 825 | if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") || |
882dbe0c JB |
826 | check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") || |
827 | check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")) | |
96da0919 | 828 | ret = -EINVAL; |
a7a3f7ca | 829 | out: |
0b246afa | 830 | if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && |
3cdde224 JM |
831 | !btrfs_test_opt(info, FREE_SPACE_TREE) && |
832 | !btrfs_test_opt(info, CLEAR_CACHE)) { | |
0b246afa | 833 | btrfs_err(info, "cannot disable free space tree"); |
70f6d82e | 834 | ret = -EINVAL; |
64b5d5b2 QW |
835 | } |
836 | if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) && | |
1d6a4fc8 | 837 | !btrfs_test_opt(info, FREE_SPACE_TREE)) { |
64b5d5b2 QW |
838 | btrfs_err(info, "cannot disable free space tree with block-group-tree feature"); |
839 | ret = -EINVAL; | |
70f6d82e | 840 | } |
5d1ab66c NA |
841 | if (!ret) |
842 | ret = btrfs_check_mountopts_zoned(info); | |
dbecac26 MS |
843 | if (!ret && !remounting) { |
844 | if (btrfs_test_opt(info, SPACE_CACHE)) | |
845 | btrfs_info(info, "disk space caching is enabled"); | |
846 | if (btrfs_test_opt(info, FREE_SPACE_TREE)) | |
847 | btrfs_info(info, "using free space tree"); | |
848 | } | |
a7a3f7ca | 849 | return ret; |
edf24abe CH |
850 | } |
851 | ||
852 | /* | |
853 | * Parse mount options that are required early in the mount process. | |
854 | * | |
855 | * All other options will be parsed on much later in the mount process and | |
856 | * only when we need to allocate a new super block. | |
857 | */ | |
05bdb996 | 858 | static int btrfs_parse_device_options(const char *options, blk_mode_t flags) |
edf24abe CH |
859 | { |
860 | substring_t args[MAX_OPT_ARGS]; | |
83c8c9bd | 861 | char *device_name, *opts, *orig, *p; |
36350e95 | 862 | struct btrfs_device *device = NULL; |
d7407606 MT |
863 | int error = 0; |
864 | ||
5139cff5 DS |
865 | lockdep_assert_held(&uuid_mutex); |
866 | ||
d7407606 MT |
867 | if (!options) |
868 | return 0; | |
869 | ||
870 | /* | |
871 | * strsep changes the string, duplicate it because btrfs_parse_options | |
872 | * gets called later | |
873 | */ | |
874 | opts = kstrdup(options, GFP_KERNEL); | |
875 | if (!opts) | |
876 | return -ENOMEM; | |
877 | orig = opts; | |
878 | ||
879 | while ((p = strsep(&opts, ",")) != NULL) { | |
880 | int token; | |
881 | ||
882 | if (!*p) | |
883 | continue; | |
884 | ||
885 | token = match_token(p, tokens, args); | |
886 | if (token == Opt_device) { | |
887 | device_name = match_strdup(&args[0]); | |
888 | if (!device_name) { | |
889 | error = -ENOMEM; | |
890 | goto out; | |
891 | } | |
2ef78928 | 892 | device = btrfs_scan_one_device(device_name, flags); |
d7407606 | 893 | kfree(device_name); |
36350e95 GJ |
894 | if (IS_ERR(device)) { |
895 | error = PTR_ERR(device); | |
d7407606 | 896 | goto out; |
36350e95 | 897 | } |
d7407606 MT |
898 | } |
899 | } | |
900 | ||
901 | out: | |
902 | kfree(orig); | |
903 | return error; | |
904 | } | |
905 | ||
906 | /* | |
907 | * Parse mount options that are related to subvolume id | |
908 | * | |
909 | * The value is later passed to mount_subvol() | |
910 | */ | |
93b9bcdf GJ |
911 | static int btrfs_parse_subvol_options(const char *options, char **subvol_name, |
912 | u64 *subvol_objectid) | |
d7407606 MT |
913 | { |
914 | substring_t args[MAX_OPT_ARGS]; | |
915 | char *opts, *orig, *p; | |
edf24abe | 916 | int error = 0; |
ccb0e7d1 | 917 | u64 subvolid; |
edf24abe CH |
918 | |
919 | if (!options) | |
830c4adb | 920 | return 0; |
edf24abe CH |
921 | |
922 | /* | |
d7407606 | 923 | * strsep changes the string, duplicate it because |
fa59f27c | 924 | * btrfs_parse_device_options gets called later |
edf24abe CH |
925 | */ |
926 | opts = kstrdup(options, GFP_KERNEL); | |
927 | if (!opts) | |
928 | return -ENOMEM; | |
3f3d0bc0 | 929 | orig = opts; |
edf24abe CH |
930 | |
931 | while ((p = strsep(&opts, ",")) != NULL) { | |
932 | int token; | |
933 | if (!*p) | |
934 | continue; | |
935 | ||
936 | token = match_token(p, tokens, args); | |
937 | switch (token) { | |
938 | case Opt_subvol: | |
a90e8b6f | 939 | kfree(*subvol_name); |
edf24abe | 940 | *subvol_name = match_strdup(&args[0]); |
2c334e87 WS |
941 | if (!*subvol_name) { |
942 | error = -ENOMEM; | |
943 | goto out; | |
944 | } | |
edf24abe | 945 | break; |
73f73415 | 946 | case Opt_subvolid: |
ccb0e7d1 AJ |
947 | error = match_u64(&args[0], &subvolid); |
948 | if (error) | |
2c334e87 | 949 | goto out; |
ccb0e7d1 AJ |
950 | |
951 | /* we want the original fs_tree */ | |
952 | if (subvolid == 0) | |
953 | subvolid = BTRFS_FS_TREE_OBJECTID; | |
954 | ||
955 | *subvol_objectid = subvolid; | |
73f73415 | 956 | break; |
edf24abe CH |
957 | default: |
958 | break; | |
959 | } | |
960 | } | |
961 | ||
830c4adb | 962 | out: |
3f3d0bc0 | 963 | kfree(orig); |
edf24abe | 964 | return error; |
95e05289 CM |
965 | } |
966 | ||
c0c907a4 MPS |
967 | char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, |
968 | u64 subvol_objectid) | |
73f73415 | 969 | { |
815745cf | 970 | struct btrfs_root *root = fs_info->tree_root; |
5168489a | 971 | struct btrfs_root *fs_root = NULL; |
05dbe683 OS |
972 | struct btrfs_root_ref *root_ref; |
973 | struct btrfs_inode_ref *inode_ref; | |
974 | struct btrfs_key key; | |
975 | struct btrfs_path *path = NULL; | |
976 | char *name = NULL, *ptr; | |
977 | u64 dirid; | |
978 | int len; | |
979 | int ret; | |
980 | ||
981 | path = btrfs_alloc_path(); | |
982 | if (!path) { | |
983 | ret = -ENOMEM; | |
984 | goto err; | |
985 | } | |
05dbe683 | 986 | |
3ec83621 | 987 | name = kmalloc(PATH_MAX, GFP_KERNEL); |
05dbe683 OS |
988 | if (!name) { |
989 | ret = -ENOMEM; | |
990 | goto err; | |
991 | } | |
992 | ptr = name + PATH_MAX - 1; | |
993 | ptr[0] = '\0'; | |
73f73415 JB |
994 | |
995 | /* | |
05dbe683 OS |
996 | * Walk up the subvolume trees in the tree of tree roots by root |
997 | * backrefs until we hit the top-level subvolume. | |
73f73415 | 998 | */ |
05dbe683 OS |
999 | while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { |
1000 | key.objectid = subvol_objectid; | |
1001 | key.type = BTRFS_ROOT_BACKREF_KEY; | |
1002 | key.offset = (u64)-1; | |
1003 | ||
0ff40a91 | 1004 | ret = btrfs_search_backwards(root, &key, path); |
05dbe683 OS |
1005 | if (ret < 0) { |
1006 | goto err; | |
1007 | } else if (ret > 0) { | |
0ff40a91 MPS |
1008 | ret = -ENOENT; |
1009 | goto err; | |
05dbe683 OS |
1010 | } |
1011 | ||
05dbe683 OS |
1012 | subvol_objectid = key.offset; |
1013 | ||
1014 | root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1015 | struct btrfs_root_ref); | |
1016 | len = btrfs_root_ref_name_len(path->nodes[0], root_ref); | |
1017 | ptr -= len + 1; | |
1018 | if (ptr < name) { | |
1019 | ret = -ENAMETOOLONG; | |
1020 | goto err; | |
1021 | } | |
1022 | read_extent_buffer(path->nodes[0], ptr + 1, | |
1023 | (unsigned long)(root_ref + 1), len); | |
1024 | ptr[0] = '/'; | |
1025 | dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); | |
1026 | btrfs_release_path(path); | |
1027 | ||
56e9357a | 1028 | fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true); |
05dbe683 OS |
1029 | if (IS_ERR(fs_root)) { |
1030 | ret = PTR_ERR(fs_root); | |
5168489a JB |
1031 | fs_root = NULL; |
1032 | goto err; | |
1033 | } | |
05dbe683 OS |
1034 | |
1035 | /* | |
1036 | * Walk up the filesystem tree by inode refs until we hit the | |
1037 | * root directory. | |
1038 | */ | |
1039 | while (dirid != BTRFS_FIRST_FREE_OBJECTID) { | |
1040 | key.objectid = dirid; | |
1041 | key.type = BTRFS_INODE_REF_KEY; | |
1042 | key.offset = (u64)-1; | |
1043 | ||
0ff40a91 | 1044 | ret = btrfs_search_backwards(fs_root, &key, path); |
05dbe683 OS |
1045 | if (ret < 0) { |
1046 | goto err; | |
1047 | } else if (ret > 0) { | |
0ff40a91 MPS |
1048 | ret = -ENOENT; |
1049 | goto err; | |
05dbe683 OS |
1050 | } |
1051 | ||
05dbe683 OS |
1052 | dirid = key.offset; |
1053 | ||
1054 | inode_ref = btrfs_item_ptr(path->nodes[0], | |
1055 | path->slots[0], | |
1056 | struct btrfs_inode_ref); | |
1057 | len = btrfs_inode_ref_name_len(path->nodes[0], | |
1058 | inode_ref); | |
1059 | ptr -= len + 1; | |
1060 | if (ptr < name) { | |
1061 | ret = -ENAMETOOLONG; | |
1062 | goto err; | |
1063 | } | |
1064 | read_extent_buffer(path->nodes[0], ptr + 1, | |
1065 | (unsigned long)(inode_ref + 1), len); | |
1066 | ptr[0] = '/'; | |
1067 | btrfs_release_path(path); | |
1068 | } | |
00246528 | 1069 | btrfs_put_root(fs_root); |
5168489a | 1070 | fs_root = NULL; |
73f73415 JB |
1071 | } |
1072 | ||
05dbe683 OS |
1073 | btrfs_free_path(path); |
1074 | if (ptr == name + PATH_MAX - 1) { | |
1075 | name[0] = '/'; | |
1076 | name[1] = '\0'; | |
1077 | } else { | |
1078 | memmove(name, ptr, name + PATH_MAX - ptr); | |
1079 | } | |
1080 | return name; | |
1081 | ||
1082 | err: | |
00246528 | 1083 | btrfs_put_root(fs_root); |
05dbe683 OS |
1084 | btrfs_free_path(path); |
1085 | kfree(name); | |
1086 | return ERR_PTR(ret); | |
1087 | } | |
1088 | ||
1089 | static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) | |
1090 | { | |
1091 | struct btrfs_root *root = fs_info->tree_root; | |
1092 | struct btrfs_dir_item *di; | |
1093 | struct btrfs_path *path; | |
1094 | struct btrfs_key location; | |
6db75318 | 1095 | struct fscrypt_str name = FSTR_INIT("default", 7); |
05dbe683 OS |
1096 | u64 dir_id; |
1097 | ||
73f73415 JB |
1098 | path = btrfs_alloc_path(); |
1099 | if (!path) | |
05dbe683 | 1100 | return -ENOMEM; |
73f73415 JB |
1101 | |
1102 | /* | |
1103 | * Find the "default" dir item which points to the root item that we | |
1104 | * will mount by default if we haven't been given a specific subvolume | |
1105 | * to mount. | |
1106 | */ | |
815745cf | 1107 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
e43eec81 | 1108 | di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0); |
b0839166 JL |
1109 | if (IS_ERR(di)) { |
1110 | btrfs_free_path(path); | |
05dbe683 | 1111 | return PTR_ERR(di); |
b0839166 | 1112 | } |
73f73415 JB |
1113 | if (!di) { |
1114 | /* | |
1115 | * Ok the default dir item isn't there. This is weird since | |
1116 | * it's always been there, but don't freak out, just try and | |
05dbe683 | 1117 | * mount the top-level subvolume. |
73f73415 JB |
1118 | */ |
1119 | btrfs_free_path(path); | |
05dbe683 OS |
1120 | *objectid = BTRFS_FS_TREE_OBJECTID; |
1121 | return 0; | |
73f73415 JB |
1122 | } |
1123 | ||
1124 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); | |
1125 | btrfs_free_path(path); | |
05dbe683 OS |
1126 | *objectid = location.objectid; |
1127 | return 0; | |
73f73415 JB |
1128 | } |
1129 | ||
d397712b | 1130 | static int btrfs_fill_super(struct super_block *sb, |
8a4b83cc | 1131 | struct btrfs_fs_devices *fs_devices, |
56e033a7 | 1132 | void *data) |
75dfe396 | 1133 | { |
d397712b | 1134 | struct inode *inode; |
815745cf | 1135 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
39279cc3 | 1136 | int err; |
a429e513 | 1137 | |
39279cc3 CM |
1138 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
1139 | sb->s_magic = BTRFS_SUPER_MAGIC; | |
1140 | sb->s_op = &btrfs_super_ops; | |
af53d29a | 1141 | sb->s_d_op = &btrfs_dentry_operations; |
be6e8dc0 | 1142 | sb->s_export_op = &btrfs_export_ops; |
14605409 BB |
1143 | #ifdef CONFIG_FS_VERITY |
1144 | sb->s_vop = &btrfs_verityops; | |
1145 | #endif | |
5103e947 | 1146 | sb->s_xattr = btrfs_xattr_handlers; |
39279cc3 | 1147 | sb->s_time_gran = 1; |
0eda294d | 1148 | #ifdef CONFIG_BTRFS_FS_POSIX_ACL |
1751e8a6 | 1149 | sb->s_flags |= SB_POSIXACL; |
49cf6f45 | 1150 | #endif |
357fdad0 | 1151 | sb->s_flags |= SB_I_VERSION; |
da2f0f74 | 1152 | sb->s_iflags |= SB_I_CGROUPWB; |
9e11ceee JK |
1153 | |
1154 | err = super_setup_bdi(sb); | |
1155 | if (err) { | |
1156 | btrfs_err(fs_info, "super_setup_bdi failed"); | |
1157 | return err; | |
1158 | } | |
1159 | ||
ad2b2c80 AV |
1160 | err = open_ctree(sb, fs_devices, (char *)data); |
1161 | if (err) { | |
ab8d0fc4 | 1162 | btrfs_err(fs_info, "open_ctree failed"); |
ad2b2c80 | 1163 | return err; |
a429e513 CM |
1164 | } |
1165 | ||
0202e83f | 1166 | inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root); |
5d4f98a2 YZ |
1167 | if (IS_ERR(inode)) { |
1168 | err = PTR_ERR(inode); | |
13b98989 | 1169 | btrfs_handle_fs_error(fs_info, err, NULL); |
39279cc3 | 1170 | goto fail_close; |
f254e52c | 1171 | } |
f254e52c | 1172 | |
48fde701 AV |
1173 | sb->s_root = d_make_root(inode); |
1174 | if (!sb->s_root) { | |
39279cc3 CM |
1175 | err = -ENOMEM; |
1176 | goto fail_close; | |
f254e52c | 1177 | } |
58176a96 | 1178 | |
1751e8a6 | 1179 | sb->s_flags |= SB_ACTIVE; |
2619ba1f | 1180 | return 0; |
39279cc3 CM |
1181 | |
1182 | fail_close: | |
6bccf3ab | 1183 | close_ctree(fs_info); |
39279cc3 | 1184 | return err; |
2619ba1f CM |
1185 | } |
1186 | ||
6bf13c0c | 1187 | int btrfs_sync_fs(struct super_block *sb, int wait) |
c5739bba CM |
1188 | { |
1189 | struct btrfs_trans_handle *trans; | |
815745cf AV |
1190 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
1191 | struct btrfs_root *root = fs_info->tree_root; | |
2619ba1f | 1192 | |
bc074524 | 1193 | trace_btrfs_sync_fs(fs_info, wait); |
1abe9b8a | 1194 | |
39279cc3 | 1195 | if (!wait) { |
815745cf | 1196 | filemap_flush(fs_info->btree_inode->i_mapping); |
39279cc3 CM |
1197 | return 0; |
1198 | } | |
771ed689 | 1199 | |
6374e57a | 1200 | btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); |
771ed689 | 1201 | |
d4edf39b | 1202 | trans = btrfs_attach_transaction_barrier(root); |
60376ce4 | 1203 | if (IS_ERR(trans)) { |
354aa0fb | 1204 | /* no transaction, don't bother */ |
6b5fe46d DS |
1205 | if (PTR_ERR(trans) == -ENOENT) { |
1206 | /* | |
1207 | * Exit unless we have some pending changes | |
1208 | * that need to go through commit | |
1209 | */ | |
c52cc7b7 JB |
1210 | if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT, |
1211 | &fs_info->flags)) | |
6b5fe46d | 1212 | return 0; |
a53f4f8e QW |
1213 | /* |
1214 | * A non-blocking test if the fs is frozen. We must not | |
1215 | * start a new transaction here otherwise a deadlock | |
1216 | * happens. The pending operations are delayed to the | |
1217 | * next commit after thawing. | |
1218 | */ | |
a7e3c5f2 RP |
1219 | if (sb_start_write_trylock(sb)) |
1220 | sb_end_write(sb); | |
a53f4f8e QW |
1221 | else |
1222 | return 0; | |
6b5fe46d | 1223 | trans = btrfs_start_transaction(root, 0); |
6b5fe46d | 1224 | } |
98bd5c54 DS |
1225 | if (IS_ERR(trans)) |
1226 | return PTR_ERR(trans); | |
60376ce4 | 1227 | } |
3a45bb20 | 1228 | return btrfs_commit_transaction(trans); |
2c90e5d6 CM |
1229 | } |
1230 | ||
ab0b4a3e JB |
1231 | static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed) |
1232 | { | |
1233 | seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s); | |
1234 | *printed = true; | |
1235 | } | |
1236 | ||
34c80b1d | 1237 | static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) |
a9572a15 | 1238 | { |
815745cf | 1239 | struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); |
0f628c63 | 1240 | const char *compress_type; |
3ef3959b | 1241 | const char *subvol_name; |
ab0b4a3e | 1242 | bool printed = false; |
a9572a15 | 1243 | |
3cdde224 | 1244 | if (btrfs_test_opt(info, DEGRADED)) |
a9572a15 | 1245 | seq_puts(seq, ",degraded"); |
3cdde224 | 1246 | if (btrfs_test_opt(info, NODATASUM)) |
a9572a15 | 1247 | seq_puts(seq, ",nodatasum"); |
3cdde224 | 1248 | if (btrfs_test_opt(info, NODATACOW)) |
a9572a15 | 1249 | seq_puts(seq, ",nodatacow"); |
3cdde224 | 1250 | if (btrfs_test_opt(info, NOBARRIER)) |
a9572a15 | 1251 | seq_puts(seq, ",nobarrier"); |
95ac567a | 1252 | if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) |
c1c9ff7c | 1253 | seq_printf(seq, ",max_inline=%llu", info->max_inline); |
a9572a15 EP |
1254 | if (info->thread_pool_size != min_t(unsigned long, |
1255 | num_online_cpus() + 2, 8)) | |
f7b885be | 1256 | seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); |
3cdde224 | 1257 | if (btrfs_test_opt(info, COMPRESS)) { |
0f628c63 | 1258 | compress_type = btrfs_compress_type2str(info->compress_type); |
3cdde224 | 1259 | if (btrfs_test_opt(info, FORCE_COMPRESS)) |
200da64e TI |
1260 | seq_printf(seq, ",compress-force=%s", compress_type); |
1261 | else | |
1262 | seq_printf(seq, ",compress=%s", compress_type); | |
f51d2b59 | 1263 | if (info->compress_level) |
fa4d885a | 1264 | seq_printf(seq, ":%d", info->compress_level); |
200da64e | 1265 | } |
3cdde224 | 1266 | if (btrfs_test_opt(info, NOSSD)) |
c289811c | 1267 | seq_puts(seq, ",nossd"); |
3cdde224 | 1268 | if (btrfs_test_opt(info, SSD_SPREAD)) |
451d7585 | 1269 | seq_puts(seq, ",ssd_spread"); |
3cdde224 | 1270 | else if (btrfs_test_opt(info, SSD)) |
a9572a15 | 1271 | seq_puts(seq, ",ssd"); |
3cdde224 | 1272 | if (btrfs_test_opt(info, NOTREELOG)) |
6b65c5c6 | 1273 | seq_puts(seq, ",notreelog"); |
3cdde224 | 1274 | if (btrfs_test_opt(info, NOLOGREPLAY)) |
ab0b4a3e | 1275 | print_rescue_option(seq, "nologreplay", &printed); |
68319c18 JB |
1276 | if (btrfs_test_opt(info, USEBACKUPROOT)) |
1277 | print_rescue_option(seq, "usebackuproot", &printed); | |
42437a63 JB |
1278 | if (btrfs_test_opt(info, IGNOREBADROOTS)) |
1279 | print_rescue_option(seq, "ignorebadroots", &printed); | |
882dbe0c JB |
1280 | if (btrfs_test_opt(info, IGNOREDATACSUMS)) |
1281 | print_rescue_option(seq, "ignoredatacsums", &printed); | |
3cdde224 | 1282 | if (btrfs_test_opt(info, FLUSHONCOMMIT)) |
6b65c5c6 | 1283 | seq_puts(seq, ",flushoncommit"); |
46b27f50 | 1284 | if (btrfs_test_opt(info, DISCARD_SYNC)) |
20a5239a | 1285 | seq_puts(seq, ",discard"); |
b0643e59 DZ |
1286 | if (btrfs_test_opt(info, DISCARD_ASYNC)) |
1287 | seq_puts(seq, ",discard=async"); | |
1751e8a6 | 1288 | if (!(info->sb->s_flags & SB_POSIXACL)) |
a9572a15 | 1289 | seq_puts(seq, ",noacl"); |
04c41559 | 1290 | if (btrfs_free_space_cache_v1_active(info)) |
200da64e | 1291 | seq_puts(seq, ",space_cache"); |
04c41559 | 1292 | else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) |
70f6d82e | 1293 | seq_puts(seq, ",space_cache=v2"); |
73bc1876 | 1294 | else |
8965593e | 1295 | seq_puts(seq, ",nospace_cache"); |
3cdde224 | 1296 | if (btrfs_test_opt(info, RESCAN_UUID_TREE)) |
f420ee1e | 1297 | seq_puts(seq, ",rescan_uuid_tree"); |
3cdde224 | 1298 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
200da64e | 1299 | seq_puts(seq, ",clear_cache"); |
3cdde224 | 1300 | if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) |
200da64e | 1301 | seq_puts(seq, ",user_subvol_rm_allowed"); |
3cdde224 | 1302 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
0942caa3 | 1303 | seq_puts(seq, ",enospc_debug"); |
3cdde224 | 1304 | if (btrfs_test_opt(info, AUTO_DEFRAG)) |
0942caa3 | 1305 | seq_puts(seq, ",autodefrag"); |
3cdde224 | 1306 | if (btrfs_test_opt(info, SKIP_BALANCE)) |
9555c6c1 | 1307 | seq_puts(seq, ",skip_balance"); |
8507d216 | 1308 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
cbeaae4f | 1309 | if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA)) |
8507d216 | 1310 | seq_puts(seq, ",check_int_data"); |
3cdde224 | 1311 | else if (btrfs_test_opt(info, CHECK_INTEGRITY)) |
8507d216 WS |
1312 | seq_puts(seq, ",check_int"); |
1313 | if (info->check_integrity_print_mask) | |
1314 | seq_printf(seq, ",check_int_print_mask=%d", | |
1315 | info->check_integrity_print_mask); | |
1316 | #endif | |
1317 | if (info->metadata_ratio) | |
764cb8b4 | 1318 | seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); |
3cdde224 | 1319 | if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) |
8c342930 | 1320 | seq_puts(seq, ",fatal_errors=panic"); |
8b87dc17 | 1321 | if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) |
d3740608 | 1322 | seq_printf(seq, ",commit=%u", info->commit_interval); |
d0bd4560 | 1323 | #ifdef CONFIG_BTRFS_DEBUG |
3cdde224 | 1324 | if (btrfs_test_opt(info, FRAGMENT_DATA)) |
d0bd4560 | 1325 | seq_puts(seq, ",fragment=data"); |
3cdde224 | 1326 | if (btrfs_test_opt(info, FRAGMENT_METADATA)) |
d0bd4560 JB |
1327 | seq_puts(seq, ",fragment=metadata"); |
1328 | #endif | |
fb592373 JB |
1329 | if (btrfs_test_opt(info, REF_VERIFY)) |
1330 | seq_puts(seq, ",ref_verify"); | |
c8d3fe02 OS |
1331 | seq_printf(seq, ",subvolid=%llu", |
1332 | BTRFS_I(d_inode(dentry))->root->root_key.objectid); | |
3ef3959b JB |
1333 | subvol_name = btrfs_get_subvol_name_from_objectid(info, |
1334 | BTRFS_I(d_inode(dentry))->root->root_key.objectid); | |
1335 | if (!IS_ERR(subvol_name)) { | |
1336 | seq_puts(seq, ",subvol="); | |
1337 | seq_escape(seq, subvol_name, " \t\n\\"); | |
1338 | kfree(subvol_name); | |
1339 | } | |
a9572a15 EP |
1340 | return 0; |
1341 | } | |
1342 | ||
a061fc8d | 1343 | static int btrfs_test_super(struct super_block *s, void *data) |
4b82d6e4 | 1344 | { |
815745cf AV |
1345 | struct btrfs_fs_info *p = data; |
1346 | struct btrfs_fs_info *fs_info = btrfs_sb(s); | |
4b82d6e4 | 1347 | |
815745cf | 1348 | return fs_info->fs_devices == p->fs_devices; |
4b82d6e4 Y |
1349 | } |
1350 | ||
450ba0ea JB |
1351 | static int btrfs_set_super(struct super_block *s, void *data) |
1352 | { | |
6de1d09d AV |
1353 | int err = set_anon_super(s, data); |
1354 | if (!err) | |
1355 | s->s_fs_info = data; | |
1356 | return err; | |
4b82d6e4 Y |
1357 | } |
1358 | ||
f9d9ef62 DS |
1359 | /* |
1360 | * subvolumes are identified by ino 256 | |
1361 | */ | |
1362 | static inline int is_subvolume_inode(struct inode *inode) | |
1363 | { | |
1364 | if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) | |
1365 | return 1; | |
1366 | return 0; | |
1367 | } | |
1368 | ||
bb289b7b | 1369 | static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, |
ae0bc863 | 1370 | struct vfsmount *mnt) |
830c4adb | 1371 | { |
830c4adb | 1372 | struct dentry *root; |
fa330659 | 1373 | int ret; |
830c4adb | 1374 | |
05dbe683 OS |
1375 | if (!subvol_name) { |
1376 | if (!subvol_objectid) { | |
1377 | ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), | |
1378 | &subvol_objectid); | |
1379 | if (ret) { | |
1380 | root = ERR_PTR(ret); | |
1381 | goto out; | |
1382 | } | |
1383 | } | |
c0c907a4 MPS |
1384 | subvol_name = btrfs_get_subvol_name_from_objectid( |
1385 | btrfs_sb(mnt->mnt_sb), subvol_objectid); | |
05dbe683 OS |
1386 | if (IS_ERR(subvol_name)) { |
1387 | root = ERR_CAST(subvol_name); | |
1388 | subvol_name = NULL; | |
1389 | goto out; | |
1390 | } | |
1391 | ||
1392 | } | |
1393 | ||
ea441d11 | 1394 | root = mount_subtree(mnt, subvol_name); |
fa330659 OS |
1395 | /* mount_subtree() drops our reference on the vfsmount. */ |
1396 | mnt = NULL; | |
830c4adb | 1397 | |
bb289b7b | 1398 | if (!IS_ERR(root)) { |
ea441d11 | 1399 | struct super_block *s = root->d_sb; |
ab8d0fc4 | 1400 | struct btrfs_fs_info *fs_info = btrfs_sb(s); |
bb289b7b OS |
1401 | struct inode *root_inode = d_inode(root); |
1402 | u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; | |
1403 | ||
1404 | ret = 0; | |
1405 | if (!is_subvolume_inode(root_inode)) { | |
ab8d0fc4 | 1406 | btrfs_err(fs_info, "'%s' is not a valid subvolume", |
bb289b7b OS |
1407 | subvol_name); |
1408 | ret = -EINVAL; | |
1409 | } | |
1410 | if (subvol_objectid && root_objectid != subvol_objectid) { | |
05dbe683 OS |
1411 | /* |
1412 | * This will also catch a race condition where a | |
1413 | * subvolume which was passed by ID is renamed and | |
1414 | * another subvolume is renamed over the old location. | |
1415 | */ | |
ab8d0fc4 JM |
1416 | btrfs_err(fs_info, |
1417 | "subvol '%s' does not match subvolid %llu", | |
1418 | subvol_name, subvol_objectid); | |
bb289b7b OS |
1419 | ret = -EINVAL; |
1420 | } | |
1421 | if (ret) { | |
1422 | dput(root); | |
1423 | root = ERR_PTR(ret); | |
1424 | deactivate_locked_super(s); | |
1425 | } | |
f9d9ef62 DS |
1426 | } |
1427 | ||
fa330659 OS |
1428 | out: |
1429 | mntput(mnt); | |
fa330659 | 1430 | kfree(subvol_name); |
830c4adb JB |
1431 | return root; |
1432 | } | |
450ba0ea | 1433 | |
312c89fb MT |
1434 | /* |
1435 | * Find a superblock for the given device / mount point. | |
1436 | * | |
1437 | * Note: This is based on mount_bdev from fs/super.c with a few additions | |
1438 | * for multiple device setup. Make sure to keep it in sync. | |
1439 | */ | |
72fa39f5 MT |
1440 | static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, |
1441 | int flags, const char *device_name, void *data) | |
1442 | { | |
1443 | struct block_device *bdev = NULL; | |
1444 | struct super_block *s; | |
36350e95 | 1445 | struct btrfs_device *device = NULL; |
72fa39f5 MT |
1446 | struct btrfs_fs_devices *fs_devices = NULL; |
1447 | struct btrfs_fs_info *fs_info = NULL; | |
204cc0cc | 1448 | void *new_sec_opts = NULL; |
05bdb996 | 1449 | blk_mode_t mode = sb_open_mode(flags); |
72fa39f5 MT |
1450 | int error = 0; |
1451 | ||
72fa39f5 | 1452 | if (data) { |
a65001e8 | 1453 | error = security_sb_eat_lsm_opts(data, &new_sec_opts); |
72fa39f5 MT |
1454 | if (error) |
1455 | return ERR_PTR(error); | |
1456 | } | |
1457 | ||
72fa39f5 MT |
1458 | /* |
1459 | * Setup a dummy root and fs_info for test/set super. This is because | |
1460 | * we don't actually fill this stuff out until open_ctree, but we need | |
8260edba JB |
1461 | * then open_ctree will properly initialize the file system specific |
1462 | * settings later. btrfs_init_fs_info initializes the static elements | |
1463 | * of the fs_info (locks and such) to make cleanup easier if we find a | |
1464 | * superblock with our given fs_devices later on at sget() time. | |
72fa39f5 | 1465 | */ |
a8fd1f71 | 1466 | fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); |
72fa39f5 MT |
1467 | if (!fs_info) { |
1468 | error = -ENOMEM; | |
1469 | goto error_sec_opts; | |
1470 | } | |
8260edba | 1471 | btrfs_init_fs_info(fs_info); |
72fa39f5 | 1472 | |
72fa39f5 MT |
1473 | fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); |
1474 | fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); | |
72fa39f5 MT |
1475 | if (!fs_info->super_copy || !fs_info->super_for_commit) { |
1476 | error = -ENOMEM; | |
1477 | goto error_fs_info; | |
1478 | } | |
1479 | ||
399f7f4c | 1480 | mutex_lock(&uuid_mutex); |
2ef78928 | 1481 | error = btrfs_parse_device_options(data, mode); |
81ffd56b DS |
1482 | if (error) { |
1483 | mutex_unlock(&uuid_mutex); | |
399f7f4c | 1484 | goto error_fs_info; |
81ffd56b | 1485 | } |
399f7f4c | 1486 | |
2ef78928 | 1487 | device = btrfs_scan_one_device(device_name, mode); |
36350e95 | 1488 | if (IS_ERR(device)) { |
81ffd56b | 1489 | mutex_unlock(&uuid_mutex); |
36350e95 | 1490 | error = PTR_ERR(device); |
399f7f4c | 1491 | goto error_fs_info; |
81ffd56b | 1492 | } |
399f7f4c | 1493 | |
36350e95 | 1494 | fs_devices = device->fs_devices; |
399f7f4c DS |
1495 | fs_info->fs_devices = fs_devices; |
1496 | ||
72fa39f5 | 1497 | error = btrfs_open_devices(fs_devices, mode, fs_type); |
f5194e34 | 1498 | mutex_unlock(&uuid_mutex); |
72fa39f5 MT |
1499 | if (error) |
1500 | goto error_fs_info; | |
1501 | ||
1502 | if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { | |
1503 | error = -EACCES; | |
1504 | goto error_close_devices; | |
1505 | } | |
1506 | ||
d24fa5c1 | 1507 | bdev = fs_devices->latest_dev->bdev; |
72fa39f5 MT |
1508 | s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, |
1509 | fs_info); | |
1510 | if (IS_ERR(s)) { | |
1511 | error = PTR_ERR(s); | |
1512 | goto error_close_devices; | |
1513 | } | |
1514 | ||
1515 | if (s->s_root) { | |
1516 | btrfs_close_devices(fs_devices); | |
0d4b0463 | 1517 | btrfs_free_fs_info(fs_info); |
72fa39f5 MT |
1518 | if ((flags ^ s->s_flags) & SB_RDONLY) |
1519 | error = -EBUSY; | |
1520 | } else { | |
1521 | snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); | |
e33c267a RG |
1522 | shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name, |
1523 | s->s_id); | |
72fa39f5 MT |
1524 | btrfs_sb(s)->bdev_holder = fs_type; |
1525 | error = btrfs_fill_super(s, fs_devices, data); | |
1526 | } | |
a65001e8 | 1527 | if (!error) |
204cc0cc | 1528 | error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL); |
a65001e8 | 1529 | security_free_mnt_opts(&new_sec_opts); |
72fa39f5 MT |
1530 | if (error) { |
1531 | deactivate_locked_super(s); | |
a65001e8 | 1532 | return ERR_PTR(error); |
72fa39f5 MT |
1533 | } |
1534 | ||
1535 | return dget(s->s_root); | |
1536 | ||
1537 | error_close_devices: | |
1538 | btrfs_close_devices(fs_devices); | |
1539 | error_fs_info: | |
0d4b0463 | 1540 | btrfs_free_fs_info(fs_info); |
72fa39f5 MT |
1541 | error_sec_opts: |
1542 | security_free_mnt_opts(&new_sec_opts); | |
1543 | return ERR_PTR(error); | |
1544 | } | |
312c89fb | 1545 | |
edf24abe | 1546 | /* |
312c89fb | 1547 | * Mount function which is called by VFS layer. |
edf24abe | 1548 | * |
312c89fb MT |
1549 | * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() |
1550 | * which needs vfsmount* of device's root (/). This means device's root has to | |
1551 | * be mounted internally in any case. | |
1552 | * | |
1553 | * Operation flow: | |
1554 | * 1. Parse subvol id related options for later use in mount_subvol(). | |
1555 | * | |
1556 | * 2. Mount device's root (/) by calling vfs_kern_mount(). | |
1557 | * | |
1558 | * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the | |
1559 | * first place. In order to avoid calling btrfs_mount() again, we use | |
1560 | * different file_system_type which is not registered to VFS by | |
1561 | * register_filesystem() (btrfs_root_fs_type). As a result, | |
1562 | * btrfs_mount_root() is called. The return value will be used by | |
1563 | * mount_subtree() in mount_subvol(). | |
1564 | * | |
1565 | * 3. Call mount_subvol() to get the dentry of subvolume. Since there is | |
1566 | * "btrfs subvolume set-default", mount_subvol() is called always. | |
edf24abe | 1567 | */ |
061dbc6b | 1568 | static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, |
306e16ce | 1569 | const char *device_name, void *data) |
4b82d6e4 | 1570 | { |
312c89fb MT |
1571 | struct vfsmount *mnt_root; |
1572 | struct dentry *root; | |
73f73415 JB |
1573 | char *subvol_name = NULL; |
1574 | u64 subvol_objectid = 0; | |
4b82d6e4 Y |
1575 | int error = 0; |
1576 | ||
93b9bcdf GJ |
1577 | error = btrfs_parse_subvol_options(data, &subvol_name, |
1578 | &subvol_objectid); | |
f23c8af8 ID |
1579 | if (error) { |
1580 | kfree(subvol_name); | |
061dbc6b | 1581 | return ERR_PTR(error); |
f23c8af8 | 1582 | } |
edf24abe | 1583 | |
312c89fb MT |
1584 | /* mount device's root (/) */ |
1585 | mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); | |
1586 | if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { | |
1587 | if (flags & SB_RDONLY) { | |
1588 | mnt_root = vfs_kern_mount(&btrfs_root_fs_type, | |
1589 | flags & ~SB_RDONLY, device_name, data); | |
1590 | } else { | |
1591 | mnt_root = vfs_kern_mount(&btrfs_root_fs_type, | |
1592 | flags | SB_RDONLY, device_name, data); | |
1593 | if (IS_ERR(mnt_root)) { | |
1594 | root = ERR_CAST(mnt_root); | |
532b618b | 1595 | kfree(subvol_name); |
312c89fb MT |
1596 | goto out; |
1597 | } | |
4b82d6e4 | 1598 | |
312c89fb MT |
1599 | down_write(&mnt_root->mnt_sb->s_umount); |
1600 | error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); | |
1601 | up_write(&mnt_root->mnt_sb->s_umount); | |
1602 | if (error < 0) { | |
1603 | root = ERR_PTR(error); | |
1604 | mntput(mnt_root); | |
532b618b | 1605 | kfree(subvol_name); |
312c89fb MT |
1606 | goto out; |
1607 | } | |
1608 | } | |
f667aef6 | 1609 | } |
312c89fb MT |
1610 | if (IS_ERR(mnt_root)) { |
1611 | root = ERR_CAST(mnt_root); | |
532b618b | 1612 | kfree(subvol_name); |
312c89fb | 1613 | goto out; |
f667aef6 | 1614 | } |
4b82d6e4 | 1615 | |
312c89fb | 1616 | /* mount_subvol() will free subvol_name and mnt_root */ |
ae0bc863 | 1617 | root = mount_subvol(subvol_name, subvol_objectid, mnt_root); |
4b82d6e4 | 1618 | |
312c89fb MT |
1619 | out: |
1620 | return root; | |
4b82d6e4 | 1621 | } |
2e635a27 | 1622 | |
0d2450ab | 1623 | static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, |
f7b885be | 1624 | u32 new_pool_size, u32 old_pool_size) |
0d2450ab ST |
1625 | { |
1626 | if (new_pool_size == old_pool_size) | |
1627 | return; | |
1628 | ||
1629 | fs_info->thread_pool_size = new_pool_size; | |
1630 | ||
efe120a0 | 1631 | btrfs_info(fs_info, "resize thread pool %d -> %d", |
0d2450ab ST |
1632 | old_pool_size, new_pool_size); |
1633 | ||
5cdc7ad3 | 1634 | btrfs_workqueue_set_max(fs_info->workers, new_pool_size); |
afe3d242 | 1635 | btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); |
e66f0bb1 | 1636 | btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); |
40fac647 CH |
1637 | workqueue_set_max_active(fs_info->endio_workers, new_pool_size); |
1638 | workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size); | |
fccb5d86 QW |
1639 | btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); |
1640 | btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); | |
5b3bc44e | 1641 | btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); |
0d2450ab ST |
1642 | } |
1643 | ||
f42a34b2 MX |
1644 | static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, |
1645 | unsigned long old_opts, int flags) | |
1646 | { | |
dc81cdc5 MX |
1647 | if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && |
1648 | (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || | |
1751e8a6 | 1649 | (flags & SB_RDONLY))) { |
dc81cdc5 MX |
1650 | /* wait for any defraggers to finish */ |
1651 | wait_event(fs_info->transaction_wait, | |
1652 | (atomic_read(&fs_info->defrag_running) == 0)); | |
1751e8a6 | 1653 | if (flags & SB_RDONLY) |
dc81cdc5 MX |
1654 | sync_filesystem(fs_info->sb); |
1655 | } | |
1656 | } | |
1657 | ||
1658 | static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, | |
1659 | unsigned long old_opts) | |
1660 | { | |
94846229 BB |
1661 | const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); |
1662 | ||
dc81cdc5 | 1663 | /* |
180e4d47 LB |
1664 | * We need to cleanup all defragable inodes if the autodefragment is |
1665 | * close or the filesystem is read only. | |
dc81cdc5 MX |
1666 | */ |
1667 | if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && | |
bc98a42c | 1668 | (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { |
dc81cdc5 MX |
1669 | btrfs_cleanup_defrag_inodes(fs_info); |
1670 | } | |
1671 | ||
b0643e59 DZ |
1672 | /* If we toggled discard async */ |
1673 | if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && | |
1674 | btrfs_test_opt(fs_info, DISCARD_ASYNC)) | |
1675 | btrfs_discard_resume(fs_info); | |
1676 | else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && | |
1677 | !btrfs_test_opt(fs_info, DISCARD_ASYNC)) | |
1678 | btrfs_discard_cleanup(fs_info); | |
94846229 BB |
1679 | |
1680 | /* If we toggled space cache */ | |
1681 | if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) | |
1682 | btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); | |
dc81cdc5 MX |
1683 | } |
1684 | ||
c146afad YZ |
1685 | static int btrfs_remount(struct super_block *sb, int *flags, char *data) |
1686 | { | |
815745cf | 1687 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
49b25e05 JM |
1688 | unsigned old_flags = sb->s_flags; |
1689 | unsigned long old_opts = fs_info->mount_opt; | |
1690 | unsigned long old_compress_type = fs_info->compress_type; | |
1691 | u64 old_max_inline = fs_info->max_inline; | |
f7b885be | 1692 | u32 old_thread_pool_size = fs_info->thread_pool_size; |
d612ac59 | 1693 | u32 old_metadata_ratio = fs_info->metadata_ratio; |
c146afad YZ |
1694 | int ret; |
1695 | ||
02b9984d | 1696 | sync_filesystem(sb); |
88c4703f | 1697 | set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); |
dc81cdc5 | 1698 | |
f667aef6 | 1699 | if (data) { |
204cc0cc | 1700 | void *new_sec_opts = NULL; |
f667aef6 | 1701 | |
a65001e8 AV |
1702 | ret = security_sb_eat_lsm_opts(data, &new_sec_opts); |
1703 | if (!ret) | |
204cc0cc | 1704 | ret = security_sb_remount(sb, new_sec_opts); |
a65001e8 | 1705 | security_free_mnt_opts(&new_sec_opts); |
f667aef6 QW |
1706 | if (ret) |
1707 | goto restore; | |
f667aef6 QW |
1708 | } |
1709 | ||
2ff7e61e | 1710 | ret = btrfs_parse_options(fs_info, data, *flags); |
891f41cb | 1711 | if (ret) |
49b25e05 | 1712 | goto restore; |
b288052e | 1713 | |
2ba48b20 | 1714 | ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY)); |
d7f67ac9 | 1715 | if (ret < 0) |
0591f040 | 1716 | goto restore; |
d7f67ac9 | 1717 | |
f42a34b2 | 1718 | btrfs_remount_begin(fs_info, old_opts, *flags); |
0d2450ab ST |
1719 | btrfs_resize_thread_pool(fs_info, |
1720 | fs_info->thread_pool_size, old_thread_pool_size); | |
1721 | ||
c55a4319 BB |
1722 | if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) != |
1723 | (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && | |
2838d255 BB |
1724 | (!sb_rdonly(sb) || (*flags & SB_RDONLY))) { |
1725 | btrfs_warn(fs_info, | |
1726 | "remount supports changing free space tree only from ro to rw"); | |
1727 | /* Make sure free space cache options match the state on disk */ | |
1728 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { | |
1729 | btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE); | |
1730 | btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); | |
1731 | } | |
1732 | if (btrfs_free_space_cache_v1_active(fs_info)) { | |
1733 | btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE); | |
1734 | btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE); | |
1735 | } | |
1736 | } | |
1737 | ||
1751e8a6 | 1738 | if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) |
dc81cdc5 | 1739 | goto out; |
c146afad | 1740 | |
1751e8a6 | 1741 | if (*flags & SB_RDONLY) { |
8dabb742 SB |
1742 | /* |
1743 | * this also happens on 'umount -rf' or on shutdown, when | |
1744 | * the filesystem is busy. | |
1745 | */ | |
21c7e756 | 1746 | cancel_work_sync(&fs_info->async_reclaim_work); |
57056740 | 1747 | cancel_work_sync(&fs_info->async_data_reclaim_work); |
361c093d | 1748 | |
b0643e59 DZ |
1749 | btrfs_discard_cleanup(fs_info); |
1750 | ||
361c093d SB |
1751 | /* wait for the uuid_scan task to finish */ |
1752 | down(&fs_info->uuid_tree_rescan_sem); | |
1753 | /* avoid complains from lockdep et al. */ | |
1754 | up(&fs_info->uuid_tree_rescan_sem); | |
1755 | ||
a0a1db70 | 1756 | btrfs_set_sb_rdonly(sb); |
c146afad | 1757 | |
e44163e1 | 1758 | /* |
1751e8a6 | 1759 | * Setting SB_RDONLY will put the cleaner thread to |
e44163e1 JM |
1760 | * sleep at the next loop if it's already active. |
1761 | * If it's already asleep, we'll leave unused block | |
1762 | * groups on disk until we're mounted read-write again | |
1763 | * unless we clean them up here. | |
1764 | */ | |
e44163e1 | 1765 | btrfs_delete_unused_bgs(fs_info); |
e44163e1 | 1766 | |
a0a1db70 FM |
1767 | /* |
1768 | * The cleaner task could be already running before we set the | |
1769 | * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). | |
1770 | * We must make sure that after we finish the remount, i.e. after | |
1771 | * we call btrfs_commit_super(), the cleaner can no longer start | |
1772 | * a transaction - either because it was dropping a dead root, | |
1773 | * running delayed iputs or deleting an unused block group (the | |
1774 | * cleaner picked a block group from the list of unused block | |
1775 | * groups before we were able to in the previous call to | |
1776 | * btrfs_delete_unused_bgs()). | |
1777 | */ | |
1778 | wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, | |
1779 | TASK_UNINTERRUPTIBLE); | |
1780 | ||
a8cc263e FM |
1781 | /* |
1782 | * We've set the superblock to RO mode, so we might have made | |
1783 | * the cleaner task sleep without running all pending delayed | |
1784 | * iputs. Go through all the delayed iputs here, so that if an | |
1785 | * unmount happens without remounting RW we don't end up at | |
1786 | * finishing close_ctree() with a non-empty list of delayed | |
1787 | * iputs. | |
1788 | */ | |
1789 | btrfs_run_delayed_iputs(fs_info); | |
1790 | ||
8dabb742 SB |
1791 | btrfs_dev_replace_suspend_for_unmount(fs_info); |
1792 | btrfs_scrub_cancel(fs_info); | |
061594ef | 1793 | btrfs_pause_balance(fs_info); |
8dabb742 | 1794 | |
cb13eea3 FM |
1795 | /* |
1796 | * Pause the qgroup rescan worker if it is running. We don't want | |
1797 | * it to be still running after we are in RO mode, as after that, | |
1798 | * by the time we unmount, it might have left a transaction open, | |
1799 | * so we would leak the transaction and/or crash. | |
1800 | */ | |
1801 | btrfs_qgroup_wait_for_completion(fs_info, false); | |
1802 | ||
6bccf3ab | 1803 | ret = btrfs_commit_super(fs_info); |
49b25e05 JM |
1804 | if (ret) |
1805 | goto restore; | |
c146afad | 1806 | } else { |
84961539 | 1807 | if (BTRFS_FS_ERROR(fs_info)) { |
6ef3de9c | 1808 | btrfs_err(fs_info, |
efe120a0 | 1809 | "Remounting read-write after error is not allowed"); |
6ef3de9c DS |
1810 | ret = -EINVAL; |
1811 | goto restore; | |
1812 | } | |
8a3db184 | 1813 | if (fs_info->fs_devices->rw_devices == 0) { |
49b25e05 JM |
1814 | ret = -EACCES; |
1815 | goto restore; | |
8a3db184 | 1816 | } |
2b82032c | 1817 | |
6528b99d | 1818 | if (!btrfs_check_rw_degradable(fs_info, NULL)) { |
efe120a0 | 1819 | btrfs_warn(fs_info, |
52042d8e | 1820 | "too many missing devices, writable remount is not allowed"); |
292fd7fc SB |
1821 | ret = -EACCES; |
1822 | goto restore; | |
1823 | } | |
1824 | ||
8a3db184 | 1825 | if (btrfs_super_log_root(fs_info->super_copy) != 0) { |
10a3a3ed DS |
1826 | btrfs_warn(fs_info, |
1827 | "mount required to replay tree-log, cannot remount read-write"); | |
49b25e05 JM |
1828 | ret = -EINVAL; |
1829 | goto restore; | |
8a3db184 | 1830 | } |
c146afad | 1831 | |
44c0ca21 BB |
1832 | /* |
1833 | * NOTE: when remounting with a change that does writes, don't | |
1834 | * put it anywhere above this point, as we are not sure to be | |
1835 | * safe to write until we pass the above checks. | |
1836 | */ | |
1837 | ret = btrfs_start_pre_rw_mount(fs_info); | |
2b6ba629 ID |
1838 | if (ret) |
1839 | goto restore; | |
1840 | ||
a0a1db70 | 1841 | btrfs_clear_sb_rdonly(sb); |
90c711ab | 1842 | |
afcdd129 | 1843 | set_bit(BTRFS_FS_OPEN, &fs_info->flags); |
981a37ba CM |
1844 | |
1845 | /* | |
1846 | * If we've gone from readonly -> read/write, we need to get | |
1847 | * our sync/async discard lists in the right state. | |
1848 | */ | |
1849 | btrfs_discard_resume(fs_info); | |
c146afad | 1850 | } |
dc81cdc5 | 1851 | out: |
faa00889 JB |
1852 | /* |
1853 | * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS, | |
1854 | * since the absence of the flag means it can be toggled off by remount. | |
1855 | */ | |
1856 | *flags |= SB_I_VERSION; | |
1857 | ||
2c6a92b0 | 1858 | wake_up_process(fs_info->transaction_kthread); |
dc81cdc5 | 1859 | btrfs_remount_cleanup(fs_info, old_opts); |
8cd29088 | 1860 | btrfs_clear_oneshot_options(fs_info); |
88c4703f JT |
1861 | clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); |
1862 | ||
c146afad | 1863 | return 0; |
49b25e05 JM |
1864 | |
1865 | restore: | |
1751e8a6 | 1866 | /* We've hit an error - don't reset SB_RDONLY */ |
bc98a42c | 1867 | if (sb_rdonly(sb)) |
1751e8a6 | 1868 | old_flags |= SB_RDONLY; |
a0a1db70 FM |
1869 | if (!(old_flags & SB_RDONLY)) |
1870 | clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); | |
49b25e05 JM |
1871 | sb->s_flags = old_flags; |
1872 | fs_info->mount_opt = old_opts; | |
1873 | fs_info->compress_type = old_compress_type; | |
1874 | fs_info->max_inline = old_max_inline; | |
0d2450ab ST |
1875 | btrfs_resize_thread_pool(fs_info, |
1876 | old_thread_pool_size, fs_info->thread_pool_size); | |
49b25e05 | 1877 | fs_info->metadata_ratio = old_metadata_ratio; |
dc81cdc5 | 1878 | btrfs_remount_cleanup(fs_info, old_opts); |
88c4703f JT |
1879 | clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); |
1880 | ||
49b25e05 | 1881 | return ret; |
c146afad YZ |
1882 | } |
1883 | ||
bcd53741 | 1884 | /* Used to sort the devices by max_avail(descending sort) */ |
214cc184 | 1885 | static int btrfs_cmp_device_free_bytes(const void *a, const void *b) |
bcd53741 | 1886 | { |
214cc184 DS |
1887 | const struct btrfs_device_info *dev_info1 = a; |
1888 | const struct btrfs_device_info *dev_info2 = b; | |
1889 | ||
1890 | if (dev_info1->max_avail > dev_info2->max_avail) | |
bcd53741 | 1891 | return -1; |
214cc184 | 1892 | else if (dev_info1->max_avail < dev_info2->max_avail) |
bcd53741 | 1893 | return 1; |
bcd53741 AJ |
1894 | return 0; |
1895 | } | |
1896 | ||
1897 | /* | |
1898 | * sort the devices by max_avail, in which max free extent size of each device | |
1899 | * is stored.(Descending Sort) | |
1900 | */ | |
1901 | static inline void btrfs_descending_sort_devices( | |
1902 | struct btrfs_device_info *devices, | |
1903 | size_t nr_devices) | |
1904 | { | |
1905 | sort(devices, nr_devices, sizeof(struct btrfs_device_info), | |
1906 | btrfs_cmp_device_free_bytes, NULL); | |
1907 | } | |
1908 | ||
6d07bcec MX |
1909 | /* |
1910 | * The helper to calc the free space on the devices that can be used to store | |
1911 | * file data. | |
1912 | */ | |
7e17916b AB |
1913 | static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, |
1914 | u64 *free_bytes) | |
6d07bcec | 1915 | { |
6d07bcec MX |
1916 | struct btrfs_device_info *devices_info; |
1917 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
1918 | struct btrfs_device *device; | |
6d07bcec MX |
1919 | u64 type; |
1920 | u64 avail_space; | |
6d07bcec | 1921 | u64 min_stripe_size; |
559ca6ea | 1922 | int num_stripes = 1; |
6d07bcec | 1923 | int i = 0, nr_devices; |
4f080f57 | 1924 | const struct btrfs_raid_attr *rattr; |
6d07bcec | 1925 | |
7e33fd99 | 1926 | /* |
01327610 | 1927 | * We aren't under the device list lock, so this is racy-ish, but good |
7e33fd99 JB |
1928 | * enough for our purposes. |
1929 | */ | |
b772a86e | 1930 | nr_devices = fs_info->fs_devices->open_devices; |
7e33fd99 JB |
1931 | if (!nr_devices) { |
1932 | smp_mb(); | |
1933 | nr_devices = fs_info->fs_devices->open_devices; | |
1934 | ASSERT(nr_devices); | |
1935 | if (!nr_devices) { | |
1936 | *free_bytes = 0; | |
1937 | return 0; | |
1938 | } | |
1939 | } | |
6d07bcec | 1940 | |
d9b0d9ba | 1941 | devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), |
6a44517d | 1942 | GFP_KERNEL); |
6d07bcec MX |
1943 | if (!devices_info) |
1944 | return -ENOMEM; | |
1945 | ||
01327610 | 1946 | /* calc min stripe number for data space allocation */ |
1b86826d | 1947 | type = btrfs_data_alloc_profile(fs_info); |
4f080f57 DS |
1948 | rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)]; |
1949 | ||
e1ea2bee | 1950 | if (type & BTRFS_BLOCK_GROUP_RAID0) |
39fb26c3 | 1951 | num_stripes = nr_devices; |
d09cb9e1 DS |
1952 | else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK) |
1953 | num_stripes = rattr->ncopies; | |
e1ea2bee | 1954 | else if (type & BTRFS_BLOCK_GROUP_RAID10) |
39fb26c3 | 1955 | num_stripes = 4; |
6d07bcec | 1956 | |
4f080f57 DS |
1957 | /* Adjust for more than 1 stripe per device */ |
1958 | min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN; | |
6d07bcec | 1959 | |
7e33fd99 JB |
1960 | rcu_read_lock(); |
1961 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { | |
e12c9621 AJ |
1962 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
1963 | &device->dev_state) || | |
401e29c1 AJ |
1964 | !device->bdev || |
1965 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) | |
6d07bcec MX |
1966 | continue; |
1967 | ||
7e33fd99 JB |
1968 | if (i >= nr_devices) |
1969 | break; | |
1970 | ||
6d07bcec MX |
1971 | avail_space = device->total_bytes - device->bytes_used; |
1972 | ||
1973 | /* align with stripe_len */ | |
559ca6ea | 1974 | avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN); |
6d07bcec MX |
1975 | |
1976 | /* | |
37f85ec3 QW |
1977 | * Ensure we have at least min_stripe_size on top of the |
1978 | * reserved space on the device. | |
6d07bcec | 1979 | */ |
37f85ec3 | 1980 | if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size) |
6d07bcec MX |
1981 | continue; |
1982 | ||
37f85ec3 | 1983 | avail_space -= BTRFS_DEVICE_RANGE_RESERVED; |
559ca6ea | 1984 | |
6d07bcec MX |
1985 | devices_info[i].dev = device; |
1986 | devices_info[i].max_avail = avail_space; | |
1987 | ||
1988 | i++; | |
1989 | } | |
7e33fd99 | 1990 | rcu_read_unlock(); |
6d07bcec MX |
1991 | |
1992 | nr_devices = i; | |
1993 | ||
1994 | btrfs_descending_sort_devices(devices_info, nr_devices); | |
1995 | ||
1996 | i = nr_devices - 1; | |
1997 | avail_space = 0; | |
559ca6ea NB |
1998 | while (nr_devices >= rattr->devs_min) { |
1999 | num_stripes = min(num_stripes, nr_devices); | |
39fb26c3 | 2000 | |
6d07bcec MX |
2001 | if (devices_info[i].max_avail >= min_stripe_size) { |
2002 | int j; | |
2003 | u64 alloc_size; | |
2004 | ||
39fb26c3 | 2005 | avail_space += devices_info[i].max_avail * num_stripes; |
6d07bcec | 2006 | alloc_size = devices_info[i].max_avail; |
39fb26c3 | 2007 | for (j = i + 1 - num_stripes; j <= i; j++) |
6d07bcec MX |
2008 | devices_info[j].max_avail -= alloc_size; |
2009 | } | |
2010 | i--; | |
2011 | nr_devices--; | |
2012 | } | |
2013 | ||
2014 | kfree(devices_info); | |
2015 | *free_bytes = avail_space; | |
2016 | return 0; | |
2017 | } | |
2018 | ||
ba7b6e62 DS |
2019 | /* |
2020 | * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. | |
2021 | * | |
2022 | * If there's a redundant raid level at DATA block groups, use the respective | |
2023 | * multiplier to scale the sizes. | |
2024 | * | |
2025 | * Unused device space usage is based on simulating the chunk allocator | |
0d0c71b3 DS |
2026 | * algorithm that respects the device sizes and order of allocations. This is |
2027 | * a close approximation of the actual use but there are other factors that may | |
2028 | * change the result (like a new metadata chunk). | |
ba7b6e62 | 2029 | * |
ca8a51b3 | 2030 | * If metadata is exhausted, f_bavail will be 0. |
ba7b6e62 | 2031 | */ |
8fd17795 CM |
2032 | static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
2033 | { | |
815745cf AV |
2034 | struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); |
2035 | struct btrfs_super_block *disk_super = fs_info->super_copy; | |
bd4d1088 JB |
2036 | struct btrfs_space_info *found; |
2037 | u64 total_used = 0; | |
6d07bcec | 2038 | u64 total_free_data = 0; |
ca8a51b3 | 2039 | u64 total_free_meta = 0; |
265fdfa6 | 2040 | u32 bits = fs_info->sectorsize_bits; |
de37aa51 | 2041 | __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; |
ba7b6e62 DS |
2042 | unsigned factor = 1; |
2043 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; | |
6d07bcec | 2044 | int ret; |
ca8a51b3 | 2045 | u64 thresh = 0; |
ae02d1bd | 2046 | int mixed = 0; |
8fd17795 | 2047 | |
72804905 | 2048 | list_for_each_entry(found, &fs_info->space_info, list) { |
6d07bcec | 2049 | if (found->flags & BTRFS_BLOCK_GROUP_DATA) { |
ba7b6e62 DS |
2050 | int i; |
2051 | ||
6d07bcec MX |
2052 | total_free_data += found->disk_total - found->disk_used; |
2053 | total_free_data -= | |
2054 | btrfs_account_ro_block_groups_free_space(found); | |
ba7b6e62 DS |
2055 | |
2056 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { | |
46df06b8 DS |
2057 | if (!list_empty(&found->block_groups[i])) |
2058 | factor = btrfs_bg_type_to_factor( | |
2059 | btrfs_raid_array[i].bg_flag); | |
ba7b6e62 | 2060 | } |
6d07bcec | 2061 | } |
ae02d1bd LB |
2062 | |
2063 | /* | |
67da05b3 | 2064 | * Metadata in mixed block group profiles are accounted in data |
ae02d1bd LB |
2065 | */ |
2066 | if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { | |
2067 | if (found->flags & BTRFS_BLOCK_GROUP_DATA) | |
2068 | mixed = 1; | |
2069 | else | |
2070 | total_free_meta += found->disk_total - | |
2071 | found->disk_used; | |
2072 | } | |
6d07bcec | 2073 | |
b742bb82 | 2074 | total_used += found->disk_used; |
89a55897 | 2075 | } |
ba7b6e62 | 2076 | |
ba7b6e62 DS |
2077 | buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); |
2078 | buf->f_blocks >>= bits; | |
2079 | buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); | |
2080 | ||
2081 | /* Account global block reserve as used, it's in logical size already */ | |
2082 | spin_lock(&block_rsv->lock); | |
41b34acc LB |
2083 | /* Mixed block groups accounting is not byte-accurate, avoid overflow */ |
2084 | if (buf->f_bfree >= block_rsv->size >> bits) | |
2085 | buf->f_bfree -= block_rsv->size >> bits; | |
2086 | else | |
2087 | buf->f_bfree = 0; | |
ba7b6e62 DS |
2088 | spin_unlock(&block_rsv->lock); |
2089 | ||
0d95c1be | 2090 | buf->f_bavail = div_u64(total_free_data, factor); |
6bccf3ab | 2091 | ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); |
7e33fd99 | 2092 | if (ret) |
6d07bcec | 2093 | return ret; |
ba7b6e62 | 2094 | buf->f_bavail += div_u64(total_free_data, factor); |
6d07bcec | 2095 | buf->f_bavail = buf->f_bavail >> bits; |
d397712b | 2096 | |
ca8a51b3 DS |
2097 | /* |
2098 | * We calculate the remaining metadata space minus global reserve. If | |
2099 | * this is (supposedly) smaller than zero, there's no space. But this | |
2100 | * does not hold in practice, the exhausted state happens where's still | |
2101 | * some positive delta. So we apply some guesswork and compare the | |
2102 | * delta to a 4M threshold. (Practically observed delta was ~2M.) | |
2103 | * | |
2104 | * We probably cannot calculate the exact threshold value because this | |
2105 | * depends on the internal reservations requested by various | |
2106 | * operations, so some operations that consume a few metadata will | |
2107 | * succeed even if the Avail is zero. But this is better than the other | |
2108 | * way around. | |
2109 | */ | |
d4417e22 | 2110 | thresh = SZ_4M; |
ca8a51b3 | 2111 | |
d55966c4 JB |
2112 | /* |
2113 | * We only want to claim there's no available space if we can no longer | |
2114 | * allocate chunks for our metadata profile and our global reserve will | |
2115 | * not fit in the free metadata space. If we aren't ->full then we | |
2116 | * still can allocate chunks and thus are fine using the currently | |
2117 | * calculated f_bavail. | |
2118 | */ | |
2119 | if (!mixed && block_rsv->space_info->full && | |
2120 | total_free_meta - thresh < block_rsv->size) | |
ca8a51b3 DS |
2121 | buf->f_bavail = 0; |
2122 | ||
ba7b6e62 DS |
2123 | buf->f_type = BTRFS_SUPER_MAGIC; |
2124 | buf->f_bsize = dentry->d_sb->s_blocksize; | |
2125 | buf->f_namelen = BTRFS_NAME_LEN; | |
2126 | ||
9d03632e | 2127 | /* We treat it as constant endianness (it doesn't matter _which_) |
d397712b | 2128 | because we want the fsid to come out the same whether mounted |
9d03632e DW |
2129 | on a big-endian or little-endian host */ |
2130 | buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); | |
2131 | buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); | |
32d48fa1 | 2132 | /* Mask in the root object ID too, to disambiguate subvols */ |
4fd786e6 MT |
2133 | buf->f_fsid.val[0] ^= |
2134 | BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; | |
2135 | buf->f_fsid.val[1] ^= | |
2136 | BTRFS_I(d_inode(dentry))->root->root_key.objectid; | |
32d48fa1 | 2137 | |
8fd17795 CM |
2138 | return 0; |
2139 | } | |
b5133862 | 2140 | |
aea52e19 AV |
2141 | static void btrfs_kill_super(struct super_block *sb) |
2142 | { | |
815745cf | 2143 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
aea52e19 | 2144 | kill_anon_super(sb); |
0d4b0463 | 2145 | btrfs_free_fs_info(fs_info); |
aea52e19 AV |
2146 | } |
2147 | ||
2e635a27 CM |
2148 | static struct file_system_type btrfs_fs_type = { |
2149 | .owner = THIS_MODULE, | |
2150 | .name = "btrfs", | |
061dbc6b | 2151 | .mount = btrfs_mount, |
aea52e19 | 2152 | .kill_sb = btrfs_kill_super, |
50e9ceef | 2153 | .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_MGTIME, |
2e635a27 | 2154 | }; |
72fa39f5 MT |
2155 | |
2156 | static struct file_system_type btrfs_root_fs_type = { | |
2157 | .owner = THIS_MODULE, | |
2158 | .name = "btrfs", | |
2159 | .mount = btrfs_mount_root, | |
2160 | .kill_sb = btrfs_kill_super, | |
50e9ceef JL |
2161 | .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | |
2162 | FS_ALLOW_IDMAP | FS_MGTIME, | |
72fa39f5 MT |
2163 | }; |
2164 | ||
7f78e035 | 2165 | MODULE_ALIAS_FS("btrfs"); |
a9218f6b | 2166 | |
d8620958 TVB |
2167 | static int btrfs_control_open(struct inode *inode, struct file *file) |
2168 | { | |
2169 | /* | |
2170 | * The control file's private_data is used to hold the | |
2171 | * transaction when it is started and is used to keep | |
2172 | * track of whether a transaction is already in progress. | |
2173 | */ | |
2174 | file->private_data = NULL; | |
2175 | return 0; | |
2176 | } | |
2177 | ||
d352ac68 | 2178 | /* |
cfe953c8 | 2179 | * Used by /dev/btrfs-control for devices ioctls. |
d352ac68 | 2180 | */ |
8a4b83cc CM |
2181 | static long btrfs_control_ioctl(struct file *file, unsigned int cmd, |
2182 | unsigned long arg) | |
2183 | { | |
2184 | struct btrfs_ioctl_vol_args *vol; | |
36350e95 | 2185 | struct btrfs_device *device = NULL; |
16cab91a | 2186 | dev_t devt = 0; |
c071fcfd | 2187 | int ret = -ENOTTY; |
8a4b83cc | 2188 | |
e441d54d CM |
2189 | if (!capable(CAP_SYS_ADMIN)) |
2190 | return -EPERM; | |
2191 | ||
dae7b665 LZ |
2192 | vol = memdup_user((void __user *)arg, sizeof(*vol)); |
2193 | if (IS_ERR(vol)) | |
2194 | return PTR_ERR(vol); | |
f505754f | 2195 | vol->name[BTRFS_PATH_NAME_MAX] = '\0'; |
c071fcfd | 2196 | |
8a4b83cc CM |
2197 | switch (cmd) { |
2198 | case BTRFS_IOC_SCAN_DEV: | |
899f9307 | 2199 | mutex_lock(&uuid_mutex); |
05bdb996 | 2200 | device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ); |
36350e95 | 2201 | ret = PTR_ERR_OR_ZERO(device); |
899f9307 | 2202 | mutex_unlock(&uuid_mutex); |
8a4b83cc | 2203 | break; |
228a73ab | 2204 | case BTRFS_IOC_FORGET_DEV: |
16cab91a AJ |
2205 | if (vol->name[0] != 0) { |
2206 | ret = lookup_bdev(vol->name, &devt); | |
2207 | if (ret) | |
2208 | break; | |
2209 | } | |
2210 | ret = btrfs_forget_devices(devt); | |
228a73ab | 2211 | break; |
02db0844 | 2212 | case BTRFS_IOC_DEVICES_READY: |
899f9307 | 2213 | mutex_lock(&uuid_mutex); |
05bdb996 | 2214 | device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ); |
36350e95 | 2215 | if (IS_ERR(device)) { |
899f9307 | 2216 | mutex_unlock(&uuid_mutex); |
36350e95 | 2217 | ret = PTR_ERR(device); |
02db0844 | 2218 | break; |
899f9307 | 2219 | } |
36350e95 GJ |
2220 | ret = !(device->fs_devices->num_devices == |
2221 | device->fs_devices->total_devices); | |
899f9307 | 2222 | mutex_unlock(&uuid_mutex); |
02db0844 | 2223 | break; |
c5868f83 | 2224 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
d5131b65 | 2225 | ret = btrfs_ioctl_get_supported_features((void __user*)arg); |
c5868f83 | 2226 | break; |
8a4b83cc | 2227 | } |
dae7b665 | 2228 | |
8a4b83cc | 2229 | kfree(vol); |
f819d837 | 2230 | return ret; |
8a4b83cc CM |
2231 | } |
2232 | ||
0176260f | 2233 | static int btrfs_freeze(struct super_block *sb) |
ed0dab6b | 2234 | { |
354aa0fb | 2235 | struct btrfs_trans_handle *trans; |
0b246afa JM |
2236 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
2237 | struct btrfs_root *root = fs_info->tree_root; | |
354aa0fb | 2238 | |
fac03c8d | 2239 | set_bit(BTRFS_FS_FROZEN, &fs_info->flags); |
9e7cc91a WX |
2240 | /* |
2241 | * We don't need a barrier here, we'll wait for any transaction that | |
2242 | * could be in progress on other threads (and do delayed iputs that | |
2243 | * we want to avoid on a frozen filesystem), or do the commit | |
2244 | * ourselves. | |
2245 | */ | |
d4edf39b | 2246 | trans = btrfs_attach_transaction_barrier(root); |
354aa0fb MX |
2247 | if (IS_ERR(trans)) { |
2248 | /* no transaction, don't bother */ | |
2249 | if (PTR_ERR(trans) == -ENOENT) | |
2250 | return 0; | |
2251 | return PTR_ERR(trans); | |
2252 | } | |
3a45bb20 | 2253 | return btrfs_commit_transaction(trans); |
ed0dab6b Y |
2254 | } |
2255 | ||
a05d3c91 QW |
2256 | static int check_dev_super(struct btrfs_device *dev) |
2257 | { | |
2258 | struct btrfs_fs_info *fs_info = dev->fs_info; | |
2259 | struct btrfs_super_block *sb; | |
3d17adea | 2260 | u16 csum_type; |
a05d3c91 QW |
2261 | int ret = 0; |
2262 | ||
2263 | /* This should be called with fs still frozen. */ | |
2264 | ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags)); | |
2265 | ||
2266 | /* Missing dev, no need to check. */ | |
2267 | if (!dev->bdev) | |
2268 | return 0; | |
2269 | ||
2270 | /* Only need to check the primary super block. */ | |
2271 | sb = btrfs_read_dev_one_super(dev->bdev, 0, true); | |
2272 | if (IS_ERR(sb)) | |
2273 | return PTR_ERR(sb); | |
2274 | ||
3d17adea QW |
2275 | /* Verify the checksum. */ |
2276 | csum_type = btrfs_super_csum_type(sb); | |
2277 | if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) { | |
2278 | btrfs_err(fs_info, "csum type changed, has %u expect %u", | |
2279 | csum_type, btrfs_super_csum_type(fs_info->super_copy)); | |
2280 | ret = -EUCLEAN; | |
2281 | goto out; | |
2282 | } | |
2283 | ||
2284 | if (btrfs_check_super_csum(fs_info, sb)) { | |
2285 | btrfs_err(fs_info, "csum for on-disk super block no longer matches"); | |
2286 | ret = -EUCLEAN; | |
2287 | goto out; | |
2288 | } | |
2289 | ||
a05d3c91 QW |
2290 | /* Btrfs_validate_super() includes fsid check against super->fsid. */ |
2291 | ret = btrfs_validate_super(fs_info, sb, 0); | |
2292 | if (ret < 0) | |
2293 | goto out; | |
2294 | ||
2295 | if (btrfs_super_generation(sb) != fs_info->last_trans_committed) { | |
2296 | btrfs_err(fs_info, "transid mismatch, has %llu expect %llu", | |
2297 | btrfs_super_generation(sb), | |
2298 | fs_info->last_trans_committed); | |
2299 | ret = -EUCLEAN; | |
2300 | goto out; | |
2301 | } | |
2302 | out: | |
2303 | btrfs_release_disk_super(sb); | |
2304 | return ret; | |
2305 | } | |
2306 | ||
9e7cc91a WX |
2307 | static int btrfs_unfreeze(struct super_block *sb) |
2308 | { | |
fac03c8d | 2309 | struct btrfs_fs_info *fs_info = btrfs_sb(sb); |
a05d3c91 QW |
2310 | struct btrfs_device *device; |
2311 | int ret = 0; | |
fac03c8d | 2312 | |
a05d3c91 QW |
2313 | /* |
2314 | * Make sure the fs is not changed by accident (like hibernation then | |
2315 | * modified by other OS). | |
2316 | * If we found anything wrong, we mark the fs error immediately. | |
2317 | * | |
2318 | * And since the fs is frozen, no one can modify the fs yet, thus | |
2319 | * we don't need to hold device_list_mutex. | |
2320 | */ | |
2321 | list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { | |
2322 | ret = check_dev_super(device); | |
2323 | if (ret < 0) { | |
2324 | btrfs_handle_fs_error(fs_info, ret, | |
2325 | "super block on devid %llu got modified unexpectedly", | |
2326 | device->devid); | |
2327 | break; | |
2328 | } | |
2329 | } | |
fac03c8d | 2330 | clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); |
a05d3c91 QW |
2331 | |
2332 | /* | |
2333 | * We still return 0, to allow VFS layer to unfreeze the fs even the | |
2334 | * above checks failed. Since the fs is either fine or read-only, we're | |
2335 | * safe to continue, without causing further damage. | |
2336 | */ | |
9e7cc91a WX |
2337 | return 0; |
2338 | } | |
2339 | ||
9c5085c1 JB |
2340 | static int btrfs_show_devname(struct seq_file *m, struct dentry *root) |
2341 | { | |
2342 | struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); | |
9c5085c1 | 2343 | |
88c14590 | 2344 | /* |
6605fd2f AJ |
2345 | * There should be always a valid pointer in latest_dev, it may be stale |
2346 | * for a short moment in case it's being deleted but still valid until | |
2347 | * the end of RCU grace period. | |
88c14590 DS |
2348 | */ |
2349 | rcu_read_lock(); | |
cb3e217b | 2350 | seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\"); |
88c14590 | 2351 | rcu_read_unlock(); |
6605fd2f | 2352 | |
9c5085c1 JB |
2353 | return 0; |
2354 | } | |
2355 | ||
b87221de | 2356 | static const struct super_operations btrfs_super_ops = { |
76dda93c | 2357 | .drop_inode = btrfs_drop_inode, |
bd555975 | 2358 | .evict_inode = btrfs_evict_inode, |
e20d96d6 | 2359 | .put_super = btrfs_put_super, |
d5719762 | 2360 | .sync_fs = btrfs_sync_fs, |
a9572a15 | 2361 | .show_options = btrfs_show_options, |
9c5085c1 | 2362 | .show_devname = btrfs_show_devname, |
2c90e5d6 CM |
2363 | .alloc_inode = btrfs_alloc_inode, |
2364 | .destroy_inode = btrfs_destroy_inode, | |
26602cab | 2365 | .free_inode = btrfs_free_inode, |
8fd17795 | 2366 | .statfs = btrfs_statfs, |
c146afad | 2367 | .remount_fs = btrfs_remount, |
0176260f | 2368 | .freeze_fs = btrfs_freeze, |
9e7cc91a | 2369 | .unfreeze_fs = btrfs_unfreeze, |
e20d96d6 | 2370 | }; |
a9218f6b CM |
2371 | |
2372 | static const struct file_operations btrfs_ctl_fops = { | |
d8620958 | 2373 | .open = btrfs_control_open, |
a9218f6b | 2374 | .unlocked_ioctl = btrfs_control_ioctl, |
1832f2d8 | 2375 | .compat_ioctl = compat_ptr_ioctl, |
a9218f6b | 2376 | .owner = THIS_MODULE, |
6038f373 | 2377 | .llseek = noop_llseek, |
a9218f6b CM |
2378 | }; |
2379 | ||
2380 | static struct miscdevice btrfs_misc = { | |
578454ff | 2381 | .minor = BTRFS_MINOR, |
a9218f6b CM |
2382 | .name = "btrfs-control", |
2383 | .fops = &btrfs_ctl_fops | |
2384 | }; | |
2385 | ||
578454ff KS |
2386 | MODULE_ALIAS_MISCDEV(BTRFS_MINOR); |
2387 | MODULE_ALIAS("devname:btrfs-control"); | |
2388 | ||
f5c29bd9 | 2389 | static int __init btrfs_interface_init(void) |
a9218f6b CM |
2390 | { |
2391 | return misc_register(&btrfs_misc); | |
2392 | } | |
2393 | ||
e67c718b | 2394 | static __cold void btrfs_interface_exit(void) |
a9218f6b | 2395 | { |
f368ed60 | 2396 | misc_deregister(&btrfs_misc); |
a9218f6b CM |
2397 | } |
2398 | ||
5565b8e0 | 2399 | static int __init btrfs_print_mod_info(void) |
85965600 | 2400 | { |
edf57cbf | 2401 | static const char options[] = "" |
85965600 DS |
2402 | #ifdef CONFIG_BTRFS_DEBUG |
2403 | ", debug=on" | |
2404 | #endif | |
79556c3d SB |
2405 | #ifdef CONFIG_BTRFS_ASSERT |
2406 | ", assert=on" | |
2407 | #endif | |
85965600 DS |
2408 | #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY |
2409 | ", integrity-checker=on" | |
fb592373 JB |
2410 | #endif |
2411 | #ifdef CONFIG_BTRFS_FS_REF_VERIFY | |
2412 | ", ref-verify=on" | |
5b316468 NA |
2413 | #endif |
2414 | #ifdef CONFIG_BLK_DEV_ZONED | |
2415 | ", zoned=yes" | |
2416 | #else | |
2417 | ", zoned=no" | |
ea3dc7d2 DS |
2418 | #endif |
2419 | #ifdef CONFIG_FS_VERITY | |
2420 | ", fsverity=yes" | |
2421 | #else | |
2422 | ", fsverity=no" | |
85965600 | 2423 | #endif |
edf57cbf | 2424 | ; |
6e7a367e | 2425 | pr_info("Btrfs loaded%s\n", options); |
5565b8e0 | 2426 | return 0; |
85965600 DS |
2427 | } |
2428 | ||
5565b8e0 | 2429 | static int register_btrfs(void) |
2e635a27 | 2430 | { |
5565b8e0 QW |
2431 | return register_filesystem(&btrfs_fs_type); |
2432 | } | |
97eb6b69 | 2433 | |
5565b8e0 QW |
2434 | static void unregister_btrfs(void) |
2435 | { | |
2436 | unregister_filesystem(&btrfs_fs_type); | |
2437 | } | |
dc11dd5d | 2438 | |
5565b8e0 QW |
2439 | /* Helper structure for long init/exit functions. */ |
2440 | struct init_sequence { | |
2441 | int (*init_func)(void); | |
2442 | /* Can be NULL if the init_func doesn't need cleanup. */ | |
2443 | void (*exit_func)(void); | |
2444 | }; | |
dc11dd5d | 2445 | |
5565b8e0 QW |
2446 | static const struct init_sequence mod_init_seq[] = { |
2447 | { | |
2448 | .init_func = btrfs_props_init, | |
2449 | .exit_func = NULL, | |
2450 | }, { | |
2451 | .init_func = btrfs_init_sysfs, | |
2452 | .exit_func = btrfs_exit_sysfs, | |
2453 | }, { | |
2454 | .init_func = btrfs_init_compress, | |
2455 | .exit_func = btrfs_exit_compress, | |
2456 | }, { | |
2457 | .init_func = btrfs_init_cachep, | |
2458 | .exit_func = btrfs_destroy_cachep, | |
2459 | }, { | |
2460 | .init_func = btrfs_transaction_init, | |
2461 | .exit_func = btrfs_transaction_exit, | |
2462 | }, { | |
2463 | .init_func = btrfs_ctree_init, | |
2464 | .exit_func = btrfs_ctree_exit, | |
2465 | }, { | |
2466 | .init_func = btrfs_free_space_init, | |
2467 | .exit_func = btrfs_free_space_exit, | |
2468 | }, { | |
2469 | .init_func = extent_state_init_cachep, | |
2470 | .exit_func = extent_state_free_cachep, | |
2471 | }, { | |
2472 | .init_func = extent_buffer_init_cachep, | |
2473 | .exit_func = extent_buffer_free_cachep, | |
2474 | }, { | |
2475 | .init_func = btrfs_bioset_init, | |
2476 | .exit_func = btrfs_bioset_exit, | |
2477 | }, { | |
2478 | .init_func = extent_map_init, | |
2479 | .exit_func = extent_map_exit, | |
2480 | }, { | |
2481 | .init_func = ordered_data_init, | |
2482 | .exit_func = ordered_data_exit, | |
2483 | }, { | |
2484 | .init_func = btrfs_delayed_inode_init, | |
2485 | .exit_func = btrfs_delayed_inode_exit, | |
2486 | }, { | |
2487 | .init_func = btrfs_auto_defrag_init, | |
2488 | .exit_func = btrfs_auto_defrag_exit, | |
2489 | }, { | |
2490 | .init_func = btrfs_delayed_ref_init, | |
2491 | .exit_func = btrfs_delayed_ref_exit, | |
2492 | }, { | |
2493 | .init_func = btrfs_prelim_ref_init, | |
2494 | .exit_func = btrfs_prelim_ref_exit, | |
2495 | }, { | |
2496 | .init_func = btrfs_interface_init, | |
2497 | .exit_func = btrfs_interface_exit, | |
2498 | }, { | |
2499 | .init_func = btrfs_print_mod_info, | |
2500 | .exit_func = NULL, | |
2501 | }, { | |
2502 | .init_func = btrfs_run_sanity_tests, | |
2503 | .exit_func = NULL, | |
2504 | }, { | |
2505 | .init_func = register_btrfs, | |
2506 | .exit_func = unregister_btrfs, | |
2507 | } | |
2508 | }; | |
74255aa0 | 2509 | |
5565b8e0 | 2510 | static bool mod_init_result[ARRAY_SIZE(mod_init_seq)]; |
2f4cbe64 | 2511 | |
82c0efd3 | 2512 | static __always_inline void btrfs_exit_btrfs_fs(void) |
5565b8e0 QW |
2513 | { |
2514 | int i; | |
9678c543 | 2515 | |
5565b8e0 QW |
2516 | for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) { |
2517 | if (!mod_init_result[i]) | |
2518 | continue; | |
2519 | if (mod_init_seq[i].exit_func) | |
2520 | mod_init_seq[i].exit_func(); | |
2521 | mod_init_result[i] = false; | |
2522 | } | |
2e635a27 CM |
2523 | } |
2524 | ||
82c0efd3 AJ |
2525 | static void __exit exit_btrfs_fs(void) |
2526 | { | |
2527 | btrfs_exit_btrfs_fs(); | |
c68f7290 | 2528 | btrfs_cleanup_fs_uuids(); |
82c0efd3 AJ |
2529 | } |
2530 | ||
5565b8e0 | 2531 | static int __init init_btrfs_fs(void) |
2e635a27 | 2532 | { |
5565b8e0 QW |
2533 | int ret; |
2534 | int i; | |
2535 | ||
2536 | for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) { | |
2537 | ASSERT(!mod_init_result[i]); | |
2538 | ret = mod_init_seq[i].init_func(); | |
82c0efd3 AJ |
2539 | if (ret < 0) { |
2540 | btrfs_exit_btrfs_fs(); | |
2541 | return ret; | |
2542 | } | |
5565b8e0 QW |
2543 | mod_init_result[i] = true; |
2544 | } | |
2545 | return 0; | |
2e635a27 CM |
2546 | } |
2547 | ||
60efa5eb | 2548 | late_initcall(init_btrfs_fs); |
2e635a27 CM |
2549 | module_exit(exit_btrfs_fs) |
2550 | ||
2551 | MODULE_LICENSE("GPL"); | |
d5178578 | 2552 | MODULE_SOFTDEP("pre: crc32c"); |
3951e7f0 | 2553 | MODULE_SOFTDEP("pre: xxhash64"); |
3831bf00 | 2554 | MODULE_SOFTDEP("pre: sha256"); |
352ae07b | 2555 | MODULE_SOFTDEP("pre: blake2b-256"); |