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