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