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