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