Btrfs: make df be a little bit more understandable
[cascardo/linux.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 "compat.h"
42 #include "ctree.h"
43 #include "disk-io.h"
44 #include "transaction.h"
45 #include "btrfs_inode.h"
46 #include "ioctl.h"
47 #include "print-tree.h"
48 #include "xattr.h"
49 #include "volumes.h"
50 #include "version.h"
51 #include "export.h"
52 #include "compression.h"
53
54 static const struct super_operations btrfs_super_ops;
55
56 static void btrfs_put_super(struct super_block *sb)
57 {
58         struct btrfs_root *root = btrfs_sb(sb);
59         int ret;
60
61         ret = close_ctree(root);
62         sb->s_fs_info = NULL;
63 }
64
65 enum {
66         Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
67         Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start,
68         Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool,
69         Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio,
70         Opt_flushoncommit,
71         Opt_discard, Opt_err,
72 };
73
74 static match_table_t tokens = {
75         {Opt_degraded, "degraded"},
76         {Opt_subvol, "subvol=%s"},
77         {Opt_subvolid, "subvolid=%d"},
78         {Opt_device, "device=%s"},
79         {Opt_nodatasum, "nodatasum"},
80         {Opt_nodatacow, "nodatacow"},
81         {Opt_nobarrier, "nobarrier"},
82         {Opt_max_extent, "max_extent=%s"},
83         {Opt_max_inline, "max_inline=%s"},
84         {Opt_alloc_start, "alloc_start=%s"},
85         {Opt_thread_pool, "thread_pool=%d"},
86         {Opt_compress, "compress"},
87         {Opt_compress_force, "compress-force"},
88         {Opt_ssd, "ssd"},
89         {Opt_ssd_spread, "ssd_spread"},
90         {Opt_nossd, "nossd"},
91         {Opt_noacl, "noacl"},
92         {Opt_notreelog, "notreelog"},
93         {Opt_flushoncommit, "flushoncommit"},
94         {Opt_ratio, "metadata_ratio=%d"},
95         {Opt_discard, "discard"},
96         {Opt_err, NULL},
97 };
98
99 u64 btrfs_parse_size(char *str)
100 {
101         u64 res;
102         int mult = 1;
103         char *end;
104         char last;
105
106         res = simple_strtoul(str, &end, 10);
107
108         last = end[0];
109         if (isalpha(last)) {
110                 last = tolower(last);
111                 switch (last) {
112                 case 'g':
113                         mult *= 1024;
114                 case 'm':
115                         mult *= 1024;
116                 case 'k':
117                         mult *= 1024;
118                 }
119                 res = res * mult;
120         }
121         return res;
122 }
123
124 /*
125  * Regular mount options parser.  Everything that is needed only when
126  * reading in a new superblock is parsed here.
127  */
128 int btrfs_parse_options(struct btrfs_root *root, char *options)
129 {
130         struct btrfs_fs_info *info = root->fs_info;
131         substring_t args[MAX_OPT_ARGS];
132         char *p, *num, *orig;
133         int intarg;
134         int ret = 0;
135
136         if (!options)
137                 return 0;
138
139         /*
140          * strsep changes the string, duplicate it because parse_options
141          * gets called twice
142          */
143         options = kstrdup(options, GFP_NOFS);
144         if (!options)
145                 return -ENOMEM;
146
147         orig = options;
148
149         while ((p = strsep(&options, ",")) != NULL) {
150                 int token;
151                 if (!*p)
152                         continue;
153
154                 token = match_token(p, tokens, args);
155                 switch (token) {
156                 case Opt_degraded:
157                         printk(KERN_INFO "btrfs: allowing degraded mounts\n");
158                         btrfs_set_opt(info->mount_opt, DEGRADED);
159                         break;
160                 case Opt_subvol:
161                 case Opt_subvolid:
162                 case Opt_device:
163                         /*
164                          * These are parsed by btrfs_parse_early_options
165                          * and can be happily ignored here.
166                          */
167                         break;
168                 case Opt_nodatasum:
169                         printk(KERN_INFO "btrfs: setting nodatasum\n");
170                         btrfs_set_opt(info->mount_opt, NODATASUM);
171                         break;
172                 case Opt_nodatacow:
173                         printk(KERN_INFO "btrfs: setting nodatacow\n");
174                         btrfs_set_opt(info->mount_opt, NODATACOW);
175                         btrfs_set_opt(info->mount_opt, NODATASUM);
176                         break;
177                 case Opt_compress:
178                         printk(KERN_INFO "btrfs: use compression\n");
179                         btrfs_set_opt(info->mount_opt, COMPRESS);
180                         break;
181                 case Opt_compress_force:
182                         printk(KERN_INFO "btrfs: forcing compression\n");
183                         btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
184                         btrfs_set_opt(info->mount_opt, COMPRESS);
185                         break;
186                 case Opt_ssd:
187                         printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
188                         btrfs_set_opt(info->mount_opt, SSD);
189                         break;
190                 case Opt_ssd_spread:
191                         printk(KERN_INFO "btrfs: use spread ssd "
192                                "allocation scheme\n");
193                         btrfs_set_opt(info->mount_opt, SSD);
194                         btrfs_set_opt(info->mount_opt, SSD_SPREAD);
195                         break;
196                 case Opt_nossd:
197                         printk(KERN_INFO "btrfs: not using ssd allocation "
198                                "scheme\n");
199                         btrfs_set_opt(info->mount_opt, NOSSD);
200                         btrfs_clear_opt(info->mount_opt, SSD);
201                         btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
202                         break;
203                 case Opt_nobarrier:
204                         printk(KERN_INFO "btrfs: turning off barriers\n");
205                         btrfs_set_opt(info->mount_opt, NOBARRIER);
206                         break;
207                 case Opt_thread_pool:
208                         intarg = 0;
209                         match_int(&args[0], &intarg);
210                         if (intarg) {
211                                 info->thread_pool_size = intarg;
212                                 printk(KERN_INFO "btrfs: thread pool %d\n",
213                                        info->thread_pool_size);
214                         }
215                         break;
216                 case Opt_max_extent:
217                         num = match_strdup(&args[0]);
218                         if (num) {
219                                 info->max_extent = btrfs_parse_size(num);
220                                 kfree(num);
221
222                                 info->max_extent = max_t(u64,
223                                         info->max_extent, root->sectorsize);
224                                 printk(KERN_INFO "btrfs: max_extent at %llu\n",
225                                        (unsigned long long)info->max_extent);
226                         }
227                         break;
228                 case Opt_max_inline:
229                         num = match_strdup(&args[0]);
230                         if (num) {
231                                 info->max_inline = btrfs_parse_size(num);
232                                 kfree(num);
233
234                                 if (info->max_inline) {
235                                         info->max_inline = max_t(u64,
236                                                 info->max_inline,
237                                                 root->sectorsize);
238                                 }
239                                 printk(KERN_INFO "btrfs: max_inline at %llu\n",
240                                         (unsigned long long)info->max_inline);
241                         }
242                         break;
243                 case Opt_alloc_start:
244                         num = match_strdup(&args[0]);
245                         if (num) {
246                                 info->alloc_start = btrfs_parse_size(num);
247                                 kfree(num);
248                                 printk(KERN_INFO
249                                         "btrfs: allocations start at %llu\n",
250                                         (unsigned long long)info->alloc_start);
251                         }
252                         break;
253                 case Opt_noacl:
254                         root->fs_info->sb->s_flags &= ~MS_POSIXACL;
255                         break;
256                 case Opt_notreelog:
257                         printk(KERN_INFO "btrfs: disabling tree log\n");
258                         btrfs_set_opt(info->mount_opt, NOTREELOG);
259                         break;
260                 case Opt_flushoncommit:
261                         printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
262                         btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
263                         break;
264                 case Opt_ratio:
265                         intarg = 0;
266                         match_int(&args[0], &intarg);
267                         if (intarg) {
268                                 info->metadata_ratio = intarg;
269                                 printk(KERN_INFO "btrfs: metadata ratio %d\n",
270                                        info->metadata_ratio);
271                         }
272                         break;
273                 case Opt_discard:
274                         btrfs_set_opt(info->mount_opt, DISCARD);
275                         break;
276                 case Opt_err:
277                         printk(KERN_INFO "btrfs: unrecognized mount option "
278                                "'%s'\n", p);
279                         ret = -EINVAL;
280                         goto out;
281                 default:
282                         break;
283                 }
284         }
285 out:
286         kfree(orig);
287         return ret;
288 }
289
290 /*
291  * Parse mount options that are required early in the mount process.
292  *
293  * All other options will be parsed on much later in the mount process and
294  * only when we need to allocate a new super block.
295  */
296 static int btrfs_parse_early_options(const char *options, fmode_t flags,
297                 void *holder, char **subvol_name, u64 *subvol_objectid,
298                 struct btrfs_fs_devices **fs_devices)
299 {
300         substring_t args[MAX_OPT_ARGS];
301         char *opts, *p;
302         int error = 0;
303         int intarg;
304
305         if (!options)
306                 goto out;
307
308         /*
309          * strsep changes the string, duplicate it because parse_options
310          * gets called twice
311          */
312         opts = kstrdup(options, GFP_KERNEL);
313         if (!opts)
314                 return -ENOMEM;
315
316         while ((p = strsep(&opts, ",")) != NULL) {
317                 int token;
318                 if (!*p)
319                         continue;
320
321                 token = match_token(p, tokens, args);
322                 switch (token) {
323                 case Opt_subvol:
324                         *subvol_name = match_strdup(&args[0]);
325                         break;
326                 case Opt_subvolid:
327                         intarg = 0;
328                         error = match_int(&args[0], &intarg);
329                         if (!error) {
330                                 /* we want the original fs_tree */
331                                 if (!intarg)
332                                         *subvol_objectid =
333                                                 BTRFS_FS_TREE_OBJECTID;
334                                 else
335                                         *subvol_objectid = intarg;
336                         }
337                         break;
338                 case Opt_device:
339                         error = btrfs_scan_one_device(match_strdup(&args[0]),
340                                         flags, holder, fs_devices);
341                         if (error)
342                                 goto out_free_opts;
343                         break;
344                 default:
345                         break;
346                 }
347         }
348
349  out_free_opts:
350         kfree(opts);
351  out:
352         /*
353          * If no subvolume name is specified we use the default one.  Allocate
354          * a copy of the string "." here so that code later in the
355          * mount path doesn't care if it's the default volume or another one.
356          */
357         if (!*subvol_name) {
358                 *subvol_name = kstrdup(".", GFP_KERNEL);
359                 if (!*subvol_name)
360                         return -ENOMEM;
361         }
362         return error;
363 }
364
365 static struct dentry *get_default_root(struct super_block *sb,
366                                        u64 subvol_objectid)
367 {
368         struct btrfs_root *root = sb->s_fs_info;
369         struct btrfs_root *new_root;
370         struct btrfs_dir_item *di;
371         struct btrfs_path *path;
372         struct btrfs_key location;
373         struct inode *inode;
374         struct dentry *dentry;
375         u64 dir_id;
376         int new = 0;
377
378         /*
379          * We have a specific subvol we want to mount, just setup location and
380          * go look up the root.
381          */
382         if (subvol_objectid) {
383                 location.objectid = subvol_objectid;
384                 location.type = BTRFS_ROOT_ITEM_KEY;
385                 location.offset = (u64)-1;
386                 goto find_root;
387         }
388
389         path = btrfs_alloc_path();
390         if (!path)
391                 return ERR_PTR(-ENOMEM);
392         path->leave_spinning = 1;
393
394         /*
395          * Find the "default" dir item which points to the root item that we
396          * will mount by default if we haven't been given a specific subvolume
397          * to mount.
398          */
399         dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
400         di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
401         if (!di) {
402                 /*
403                  * Ok the default dir item isn't there.  This is weird since
404                  * it's always been there, but don't freak out, just try and
405                  * mount to root most subvolume.
406                  */
407                 btrfs_free_path(path);
408                 dir_id = BTRFS_FIRST_FREE_OBJECTID;
409                 new_root = root->fs_info->fs_root;
410                 goto setup_root;
411         }
412
413         btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
414         btrfs_free_path(path);
415
416 find_root:
417         new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
418         if (IS_ERR(new_root))
419                 return ERR_PTR(PTR_ERR(new_root));
420
421         if (btrfs_root_refs(&new_root->root_item) == 0)
422                 return ERR_PTR(-ENOENT);
423
424         dir_id = btrfs_root_dirid(&new_root->root_item);
425 setup_root:
426         location.objectid = dir_id;
427         location.type = BTRFS_INODE_ITEM_KEY;
428         location.offset = 0;
429
430         inode = btrfs_iget(sb, &location, new_root, &new);
431         if (!inode)
432                 return ERR_PTR(-ENOMEM);
433
434         /*
435          * If we're just mounting the root most subvol put the inode and return
436          * a reference to the dentry.  We will have already gotten a reference
437          * to the inode in btrfs_fill_super so we're good to go.
438          */
439         if (!new && sb->s_root->d_inode == inode) {
440                 iput(inode);
441                 return dget(sb->s_root);
442         }
443
444         if (new) {
445                 const struct qstr name = { .name = "/", .len = 1 };
446
447                 /*
448                  * New inode, we need to make the dentry a sibling of s_root so
449                  * everything gets cleaned up properly on unmount.
450                  */
451                 dentry = d_alloc(sb->s_root, &name);
452                 if (!dentry) {
453                         iput(inode);
454                         return ERR_PTR(-ENOMEM);
455                 }
456                 d_splice_alias(inode, dentry);
457         } else {
458                 /*
459                  * We found the inode in cache, just find a dentry for it and
460                  * put the reference to the inode we just got.
461                  */
462                 dentry = d_find_alias(inode);
463                 iput(inode);
464         }
465
466         return dentry;
467 }
468
469 static int btrfs_fill_super(struct super_block *sb,
470                             struct btrfs_fs_devices *fs_devices,
471                             void *data, int silent)
472 {
473         struct inode *inode;
474         struct dentry *root_dentry;
475         struct btrfs_super_block *disk_super;
476         struct btrfs_root *tree_root;
477         struct btrfs_key key;
478         int err;
479
480         sb->s_maxbytes = MAX_LFS_FILESIZE;
481         sb->s_magic = BTRFS_SUPER_MAGIC;
482         sb->s_op = &btrfs_super_ops;
483         sb->s_export_op = &btrfs_export_ops;
484         sb->s_xattr = btrfs_xattr_handlers;
485         sb->s_time_gran = 1;
486 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
487         sb->s_flags |= MS_POSIXACL;
488 #endif
489
490         tree_root = open_ctree(sb, fs_devices, (char *)data);
491
492         if (IS_ERR(tree_root)) {
493                 printk("btrfs: open_ctree failed\n");
494                 return PTR_ERR(tree_root);
495         }
496         sb->s_fs_info = tree_root;
497         disk_super = &tree_root->fs_info->super_copy;
498
499         key.objectid = BTRFS_FIRST_FREE_OBJECTID;
500         key.type = BTRFS_INODE_ITEM_KEY;
501         key.offset = 0;
502         inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
503         if (IS_ERR(inode)) {
504                 err = PTR_ERR(inode);
505                 goto fail_close;
506         }
507
508         root_dentry = d_alloc_root(inode);
509         if (!root_dentry) {
510                 iput(inode);
511                 err = -ENOMEM;
512                 goto fail_close;
513         }
514
515         sb->s_root = root_dentry;
516
517         save_mount_options(sb, data);
518         return 0;
519
520 fail_close:
521         close_ctree(tree_root);
522         return err;
523 }
524
525 int btrfs_sync_fs(struct super_block *sb, int wait)
526 {
527         struct btrfs_trans_handle *trans;
528         struct btrfs_root *root = btrfs_sb(sb);
529         int ret;
530
531         if (!wait) {
532                 filemap_flush(root->fs_info->btree_inode->i_mapping);
533                 return 0;
534         }
535
536         btrfs_start_delalloc_inodes(root, 0);
537         btrfs_wait_ordered_extents(root, 0, 0);
538
539         trans = btrfs_start_transaction(root, 1);
540         ret = btrfs_commit_transaction(trans, root);
541         return ret;
542 }
543
544 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
545 {
546         struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
547         struct btrfs_fs_info *info = root->fs_info;
548
549         if (btrfs_test_opt(root, DEGRADED))
550                 seq_puts(seq, ",degraded");
551         if (btrfs_test_opt(root, NODATASUM))
552                 seq_puts(seq, ",nodatasum");
553         if (btrfs_test_opt(root, NODATACOW))
554                 seq_puts(seq, ",nodatacow");
555         if (btrfs_test_opt(root, NOBARRIER))
556                 seq_puts(seq, ",nobarrier");
557         if (info->max_extent != (u64)-1)
558                 seq_printf(seq, ",max_extent=%llu",
559                            (unsigned long long)info->max_extent);
560         if (info->max_inline != 8192 * 1024)
561                 seq_printf(seq, ",max_inline=%llu",
562                            (unsigned long long)info->max_inline);
563         if (info->alloc_start != 0)
564                 seq_printf(seq, ",alloc_start=%llu",
565                            (unsigned long long)info->alloc_start);
566         if (info->thread_pool_size !=  min_t(unsigned long,
567                                              num_online_cpus() + 2, 8))
568                 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
569         if (btrfs_test_opt(root, COMPRESS))
570                 seq_puts(seq, ",compress");
571         if (btrfs_test_opt(root, NOSSD))
572                 seq_puts(seq, ",nossd");
573         if (btrfs_test_opt(root, SSD_SPREAD))
574                 seq_puts(seq, ",ssd_spread");
575         else if (btrfs_test_opt(root, SSD))
576                 seq_puts(seq, ",ssd");
577         if (btrfs_test_opt(root, NOTREELOG))
578                 seq_puts(seq, ",notreelog");
579         if (btrfs_test_opt(root, FLUSHONCOMMIT))
580                 seq_puts(seq, ",flushoncommit");
581         if (btrfs_test_opt(root, DISCARD))
582                 seq_puts(seq, ",discard");
583         if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
584                 seq_puts(seq, ",noacl");
585         return 0;
586 }
587
588 static int btrfs_test_super(struct super_block *s, void *data)
589 {
590         struct btrfs_fs_devices *test_fs_devices = data;
591         struct btrfs_root *root = btrfs_sb(s);
592
593         return root->fs_info->fs_devices == test_fs_devices;
594 }
595
596 /*
597  * Find a superblock for the given device / mount point.
598  *
599  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
600  *        for multiple device setup.  Make sure to keep it in sync.
601  */
602 static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
603                 const char *dev_name, void *data, struct vfsmount *mnt)
604 {
605         struct block_device *bdev = NULL;
606         struct super_block *s;
607         struct dentry *root;
608         struct btrfs_fs_devices *fs_devices = NULL;
609         fmode_t mode = FMODE_READ;
610         char *subvol_name = NULL;
611         u64 subvol_objectid = 0;
612         int error = 0;
613         int found = 0;
614
615         if (!(flags & MS_RDONLY))
616                 mode |= FMODE_WRITE;
617
618         error = btrfs_parse_early_options(data, mode, fs_type,
619                                           &subvol_name, &subvol_objectid,
620                                           &fs_devices);
621         if (error)
622                 return error;
623
624         error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
625         if (error)
626                 goto error_free_subvol_name;
627
628         error = btrfs_open_devices(fs_devices, mode, fs_type);
629         if (error)
630                 goto error_free_subvol_name;
631
632         if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
633                 error = -EACCES;
634                 goto error_close_devices;
635         }
636
637         bdev = fs_devices->latest_bdev;
638         s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
639         if (IS_ERR(s))
640                 goto error_s;
641
642         if (s->s_root) {
643                 if ((flags ^ s->s_flags) & MS_RDONLY) {
644                         deactivate_locked_super(s);
645                         error = -EBUSY;
646                         goto error_close_devices;
647                 }
648
649                 found = 1;
650                 btrfs_close_devices(fs_devices);
651         } else {
652                 char b[BDEVNAME_SIZE];
653
654                 s->s_flags = flags;
655                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
656                 error = btrfs_fill_super(s, fs_devices, data,
657                                          flags & MS_SILENT ? 1 : 0);
658                 if (error) {
659                         deactivate_locked_super(s);
660                         goto error_free_subvol_name;
661                 }
662
663                 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
664                 s->s_flags |= MS_ACTIVE;
665         }
666
667         root = get_default_root(s, subvol_objectid);
668         if (IS_ERR(root)) {
669                 error = PTR_ERR(root);
670                 deactivate_locked_super(s);
671                 goto error;
672         }
673         /* if they gave us a subvolume name bind mount into that */
674         if (strcmp(subvol_name, ".")) {
675                 struct dentry *new_root;
676                 mutex_lock(&root->d_inode->i_mutex);
677                 new_root = lookup_one_len(subvol_name, root,
678                                       strlen(subvol_name));
679                 mutex_unlock(&root->d_inode->i_mutex);
680
681                 if (IS_ERR(new_root)) {
682                         deactivate_locked_super(s);
683                         error = PTR_ERR(new_root);
684                         dput(root);
685                         goto error_close_devices;
686                 }
687                 if (!new_root->d_inode) {
688                         dput(root);
689                         dput(new_root);
690                         deactivate_locked_super(s);
691                         error = -ENXIO;
692                         goto error_close_devices;
693                 }
694                 dput(root);
695                 root = new_root;
696         }
697
698         mnt->mnt_sb = s;
699         mnt->mnt_root = root;
700
701         kfree(subvol_name);
702         return 0;
703
704 error_s:
705         error = PTR_ERR(s);
706 error_close_devices:
707         btrfs_close_devices(fs_devices);
708 error_free_subvol_name:
709         kfree(subvol_name);
710 error:
711         return error;
712 }
713
714 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
715 {
716         struct btrfs_root *root = btrfs_sb(sb);
717         int ret;
718
719         ret = btrfs_parse_options(root, data);
720         if (ret)
721                 return -EINVAL;
722
723         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
724                 return 0;
725
726         if (*flags & MS_RDONLY) {
727                 sb->s_flags |= MS_RDONLY;
728
729                 ret =  btrfs_commit_super(root);
730                 WARN_ON(ret);
731         } else {
732                 if (root->fs_info->fs_devices->rw_devices == 0)
733                         return -EACCES;
734
735                 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
736                         return -EINVAL;
737
738                 /* recover relocation */
739                 ret = btrfs_recover_relocation(root);
740                 WARN_ON(ret);
741
742                 ret = btrfs_cleanup_fs_roots(root->fs_info);
743                 WARN_ON(ret);
744
745                 sb->s_flags &= ~MS_RDONLY;
746         }
747
748         return 0;
749 }
750
751 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
752 {
753         struct btrfs_root *root = btrfs_sb(dentry->d_sb);
754         struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
755         struct list_head *head = &root->fs_info->space_info;
756         struct btrfs_space_info *found;
757         u64 total_used = 0;
758         u64 data_used = 0;
759         int bits = dentry->d_sb->s_blocksize_bits;
760         __be32 *fsid = (__be32 *)root->fs_info->fsid;
761
762         rcu_read_lock();
763         list_for_each_entry_rcu(found, head, list) {
764                 if (found->flags & (BTRFS_BLOCK_GROUP_DUP|
765                                     BTRFS_BLOCK_GROUP_RAID10|
766                                     BTRFS_BLOCK_GROUP_RAID1)) {
767                         total_used += found->bytes_used;
768                         if (found->flags & BTRFS_BLOCK_GROUP_DATA)
769                                 data_used += found->bytes_used;
770                         else
771                                 data_used += found->total_bytes;
772                 }
773
774                 total_used += found->bytes_used;
775                 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
776                         data_used += found->bytes_used;
777                 else
778                         data_used += found->total_bytes;
779         }
780         rcu_read_unlock();
781
782         buf->f_namelen = BTRFS_NAME_LEN;
783         buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
784         buf->f_bfree = buf->f_blocks - (total_used >> bits);
785         buf->f_bavail = buf->f_blocks - (data_used >> bits);
786         buf->f_bsize = dentry->d_sb->s_blocksize;
787         buf->f_type = BTRFS_SUPER_MAGIC;
788
789         /* We treat it as constant endianness (it doesn't matter _which_)
790            because we want the fsid to come out the same whether mounted
791            on a big-endian or little-endian host */
792         buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
793         buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
794         /* Mask in the root object ID too, to disambiguate subvols */
795         buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
796         buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
797
798         return 0;
799 }
800
801 static struct file_system_type btrfs_fs_type = {
802         .owner          = THIS_MODULE,
803         .name           = "btrfs",
804         .get_sb         = btrfs_get_sb,
805         .kill_sb        = kill_anon_super,
806         .fs_flags       = FS_REQUIRES_DEV,
807 };
808
809 /*
810  * used by btrfsctl to scan devices when no FS is mounted
811  */
812 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
813                                 unsigned long arg)
814 {
815         struct btrfs_ioctl_vol_args *vol;
816         struct btrfs_fs_devices *fs_devices;
817         int ret = -ENOTTY;
818
819         if (!capable(CAP_SYS_ADMIN))
820                 return -EPERM;
821
822         vol = memdup_user((void __user *)arg, sizeof(*vol));
823         if (IS_ERR(vol))
824                 return PTR_ERR(vol);
825
826         switch (cmd) {
827         case BTRFS_IOC_SCAN_DEV:
828                 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
829                                             &btrfs_fs_type, &fs_devices);
830                 break;
831         }
832
833         kfree(vol);
834         return ret;
835 }
836
837 static int btrfs_freeze(struct super_block *sb)
838 {
839         struct btrfs_root *root = btrfs_sb(sb);
840         mutex_lock(&root->fs_info->transaction_kthread_mutex);
841         mutex_lock(&root->fs_info->cleaner_mutex);
842         return 0;
843 }
844
845 static int btrfs_unfreeze(struct super_block *sb)
846 {
847         struct btrfs_root *root = btrfs_sb(sb);
848         mutex_unlock(&root->fs_info->cleaner_mutex);
849         mutex_unlock(&root->fs_info->transaction_kthread_mutex);
850         return 0;
851 }
852
853 static const struct super_operations btrfs_super_ops = {
854         .drop_inode     = btrfs_drop_inode,
855         .delete_inode   = btrfs_delete_inode,
856         .put_super      = btrfs_put_super,
857         .sync_fs        = btrfs_sync_fs,
858         .show_options   = btrfs_show_options,
859         .write_inode    = btrfs_write_inode,
860         .dirty_inode    = btrfs_dirty_inode,
861         .alloc_inode    = btrfs_alloc_inode,
862         .destroy_inode  = btrfs_destroy_inode,
863         .statfs         = btrfs_statfs,
864         .remount_fs     = btrfs_remount,
865         .freeze_fs      = btrfs_freeze,
866         .unfreeze_fs    = btrfs_unfreeze,
867 };
868
869 static const struct file_operations btrfs_ctl_fops = {
870         .unlocked_ioctl  = btrfs_control_ioctl,
871         .compat_ioctl = btrfs_control_ioctl,
872         .owner   = THIS_MODULE,
873 };
874
875 static struct miscdevice btrfs_misc = {
876         .minor          = MISC_DYNAMIC_MINOR,
877         .name           = "btrfs-control",
878         .fops           = &btrfs_ctl_fops
879 };
880
881 static int btrfs_interface_init(void)
882 {
883         return misc_register(&btrfs_misc);
884 }
885
886 static void btrfs_interface_exit(void)
887 {
888         if (misc_deregister(&btrfs_misc) < 0)
889                 printk(KERN_INFO "misc_deregister failed for control device");
890 }
891
892 static int __init init_btrfs_fs(void)
893 {
894         int err;
895
896         err = btrfs_init_sysfs();
897         if (err)
898                 return err;
899
900         err = btrfs_init_cachep();
901         if (err)
902                 goto free_sysfs;
903
904         err = extent_io_init();
905         if (err)
906                 goto free_cachep;
907
908         err = extent_map_init();
909         if (err)
910                 goto free_extent_io;
911
912         err = btrfs_interface_init();
913         if (err)
914                 goto free_extent_map;
915
916         err = register_filesystem(&btrfs_fs_type);
917         if (err)
918                 goto unregister_ioctl;
919
920         printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
921         return 0;
922
923 unregister_ioctl:
924         btrfs_interface_exit();
925 free_extent_map:
926         extent_map_exit();
927 free_extent_io:
928         extent_io_exit();
929 free_cachep:
930         btrfs_destroy_cachep();
931 free_sysfs:
932         btrfs_exit_sysfs();
933         return err;
934 }
935
936 static void __exit exit_btrfs_fs(void)
937 {
938         btrfs_destroy_cachep();
939         extent_map_exit();
940         extent_io_exit();
941         btrfs_interface_exit();
942         unregister_filesystem(&btrfs_fs_type);
943         btrfs_exit_sysfs();
944         btrfs_cleanup_fs_uuids();
945         btrfs_zlib_exit();
946 }
947
948 module_init(init_btrfs_fs)
949 module_exit(exit_btrfs_fs)
950
951 MODULE_LICENSE("GPL");