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