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