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