cd0c7cd2c8fb775533e97e3d3158f2fe6b98f5e5
[deliverable/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 <linux/slab.h>
42 #include "compat.h"
43 #include "delayed-inode.h"
44 #include "ctree.h"
45 #include "disk-io.h"
46 #include "transaction.h"
47 #include "btrfs_inode.h"
48 #include "ioctl.h"
49 #include "print-tree.h"
50 #include "xattr.h"
51 #include "volumes.h"
52 #include "version.h"
53 #include "export.h"
54 #include "compression.h"
55
56 #define CREATE_TRACE_POINTS
57 #include <trace/events/btrfs.h>
58
59 static const struct super_operations btrfs_super_ops;
60
61 static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
62 char nbuf[16])
63 {
64 char *errstr = NULL;
65
66 switch (errno) {
67 case -EIO:
68 errstr = "IO failure";
69 break;
70 case -ENOMEM:
71 errstr = "Out of memory";
72 break;
73 case -EROFS:
74 errstr = "Readonly filesystem";
75 break;
76 default:
77 if (nbuf) {
78 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
79 errstr = nbuf;
80 }
81 break;
82 }
83
84 return errstr;
85 }
86
87 static void __save_error_info(struct btrfs_fs_info *fs_info)
88 {
89 /*
90 * today we only save the error info into ram. Long term we'll
91 * also send it down to the disk
92 */
93 fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
94 }
95
96 /* NOTE:
97 * We move write_super stuff at umount in order to avoid deadlock
98 * for umount hold all lock.
99 */
100 static void save_error_info(struct btrfs_fs_info *fs_info)
101 {
102 __save_error_info(fs_info);
103 }
104
105 /* btrfs handle error by forcing the filesystem readonly */
106 static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
107 {
108 struct super_block *sb = fs_info->sb;
109
110 if (sb->s_flags & MS_RDONLY)
111 return;
112
113 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
114 sb->s_flags |= MS_RDONLY;
115 printk(KERN_INFO "btrfs is forced readonly\n");
116 }
117 }
118
119 /*
120 * __btrfs_std_error decodes expected errors from the caller and
121 * invokes the approciate error response.
122 */
123 void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
124 unsigned int line, int errno)
125 {
126 struct super_block *sb = fs_info->sb;
127 char nbuf[16];
128 const char *errstr;
129
130 /*
131 * Special case: if the error is EROFS, and we're already
132 * under MS_RDONLY, then it is safe here.
133 */
134 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
135 return;
136
137 errstr = btrfs_decode_error(fs_info, errno, nbuf);
138 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
139 sb->s_id, function, line, errstr);
140 save_error_info(fs_info);
141
142 btrfs_handle_error(fs_info);
143 }
144
145 static void btrfs_put_super(struct super_block *sb)
146 {
147 struct btrfs_root *root = btrfs_sb(sb);
148 int ret;
149
150 ret = close_ctree(root);
151 sb->s_fs_info = NULL;
152
153 (void)ret; /* FIXME: need to fix VFS to return error? */
154 }
155
156 enum {
157 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
158 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
159 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
160 Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
161 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
162 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
163 Opt_enospc_debug, Opt_subvolrootid, Opt_err,
164 };
165
166 static match_table_t tokens = {
167 {Opt_degraded, "degraded"},
168 {Opt_subvol, "subvol=%s"},
169 {Opt_subvolid, "subvolid=%d"},
170 {Opt_device, "device=%s"},
171 {Opt_nodatasum, "nodatasum"},
172 {Opt_nodatacow, "nodatacow"},
173 {Opt_nobarrier, "nobarrier"},
174 {Opt_max_inline, "max_inline=%s"},
175 {Opt_alloc_start, "alloc_start=%s"},
176 {Opt_thread_pool, "thread_pool=%d"},
177 {Opt_compress, "compress"},
178 {Opt_compress_type, "compress=%s"},
179 {Opt_compress_force, "compress-force"},
180 {Opt_compress_force_type, "compress-force=%s"},
181 {Opt_ssd, "ssd"},
182 {Opt_ssd_spread, "ssd_spread"},
183 {Opt_nossd, "nossd"},
184 {Opt_noacl, "noacl"},
185 {Opt_notreelog, "notreelog"},
186 {Opt_flushoncommit, "flushoncommit"},
187 {Opt_ratio, "metadata_ratio=%d"},
188 {Opt_discard, "discard"},
189 {Opt_space_cache, "space_cache"},
190 {Opt_clear_cache, "clear_cache"},
191 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
192 {Opt_enospc_debug, "enospc_debug"},
193 {Opt_subvolrootid, "subvolrootid=%d"},
194 {Opt_err, NULL},
195 };
196
197 /*
198 * Regular mount options parser. Everything that is needed only when
199 * reading in a new superblock is parsed here.
200 */
201 int btrfs_parse_options(struct btrfs_root *root, char *options)
202 {
203 struct btrfs_fs_info *info = root->fs_info;
204 substring_t args[MAX_OPT_ARGS];
205 char *p, *num, *orig;
206 int intarg;
207 int ret = 0;
208 char *compress_type;
209 bool compress_force = false;
210
211 if (!options)
212 return 0;
213
214 /*
215 * strsep changes the string, duplicate it because parse_options
216 * gets called twice
217 */
218 options = kstrdup(options, GFP_NOFS);
219 if (!options)
220 return -ENOMEM;
221
222 orig = options;
223
224 while ((p = strsep(&options, ",")) != NULL) {
225 int token;
226 if (!*p)
227 continue;
228
229 token = match_token(p, tokens, args);
230 switch (token) {
231 case Opt_degraded:
232 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
233 btrfs_set_opt(info->mount_opt, DEGRADED);
234 break;
235 case Opt_subvol:
236 case Opt_subvolid:
237 case Opt_subvolrootid:
238 case Opt_device:
239 /*
240 * These are parsed by btrfs_parse_early_options
241 * and can be happily ignored here.
242 */
243 break;
244 case Opt_nodatasum:
245 printk(KERN_INFO "btrfs: setting nodatasum\n");
246 btrfs_set_opt(info->mount_opt, NODATASUM);
247 break;
248 case Opt_nodatacow:
249 printk(KERN_INFO "btrfs: setting nodatacow\n");
250 btrfs_set_opt(info->mount_opt, NODATACOW);
251 btrfs_set_opt(info->mount_opt, NODATASUM);
252 break;
253 case Opt_compress_force:
254 case Opt_compress_force_type:
255 compress_force = true;
256 case Opt_compress:
257 case Opt_compress_type:
258 if (token == Opt_compress ||
259 token == Opt_compress_force ||
260 strcmp(args[0].from, "zlib") == 0) {
261 compress_type = "zlib";
262 info->compress_type = BTRFS_COMPRESS_ZLIB;
263 } else if (strcmp(args[0].from, "lzo") == 0) {
264 compress_type = "lzo";
265 info->compress_type = BTRFS_COMPRESS_LZO;
266 } else {
267 ret = -EINVAL;
268 goto out;
269 }
270
271 btrfs_set_opt(info->mount_opt, COMPRESS);
272 if (compress_force) {
273 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
274 pr_info("btrfs: force %s compression\n",
275 compress_type);
276 } else
277 pr_info("btrfs: use %s compression\n",
278 compress_type);
279 break;
280 case Opt_ssd:
281 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
282 btrfs_set_opt(info->mount_opt, SSD);
283 break;
284 case Opt_ssd_spread:
285 printk(KERN_INFO "btrfs: use spread ssd "
286 "allocation scheme\n");
287 btrfs_set_opt(info->mount_opt, SSD);
288 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
289 break;
290 case Opt_nossd:
291 printk(KERN_INFO "btrfs: not using ssd allocation "
292 "scheme\n");
293 btrfs_set_opt(info->mount_opt, NOSSD);
294 btrfs_clear_opt(info->mount_opt, SSD);
295 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
296 break;
297 case Opt_nobarrier:
298 printk(KERN_INFO "btrfs: turning off barriers\n");
299 btrfs_set_opt(info->mount_opt, NOBARRIER);
300 break;
301 case Opt_thread_pool:
302 intarg = 0;
303 match_int(&args[0], &intarg);
304 if (intarg) {
305 info->thread_pool_size = intarg;
306 printk(KERN_INFO "btrfs: thread pool %d\n",
307 info->thread_pool_size);
308 }
309 break;
310 case Opt_max_inline:
311 num = match_strdup(&args[0]);
312 if (num) {
313 info->max_inline = memparse(num, NULL);
314 kfree(num);
315
316 if (info->max_inline) {
317 info->max_inline = max_t(u64,
318 info->max_inline,
319 root->sectorsize);
320 }
321 printk(KERN_INFO "btrfs: max_inline at %llu\n",
322 (unsigned long long)info->max_inline);
323 }
324 break;
325 case Opt_alloc_start:
326 num = match_strdup(&args[0]);
327 if (num) {
328 info->alloc_start = memparse(num, NULL);
329 kfree(num);
330 printk(KERN_INFO
331 "btrfs: allocations start at %llu\n",
332 (unsigned long long)info->alloc_start);
333 }
334 break;
335 case Opt_noacl:
336 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
337 break;
338 case Opt_notreelog:
339 printk(KERN_INFO "btrfs: disabling tree log\n");
340 btrfs_set_opt(info->mount_opt, NOTREELOG);
341 break;
342 case Opt_flushoncommit:
343 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
344 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
345 break;
346 case Opt_ratio:
347 intarg = 0;
348 match_int(&args[0], &intarg);
349 if (intarg) {
350 info->metadata_ratio = intarg;
351 printk(KERN_INFO "btrfs: metadata ratio %d\n",
352 info->metadata_ratio);
353 }
354 break;
355 case Opt_discard:
356 btrfs_set_opt(info->mount_opt, DISCARD);
357 break;
358 case Opt_space_cache:
359 printk(KERN_INFO "btrfs: enabling disk space caching\n");
360 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
361 break;
362 case Opt_clear_cache:
363 printk(KERN_INFO "btrfs: force clearing of disk cache\n");
364 btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
365 break;
366 case Opt_user_subvol_rm_allowed:
367 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
368 break;
369 case Opt_enospc_debug:
370 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
371 break;
372 case Opt_err:
373 printk(KERN_INFO "btrfs: unrecognized mount option "
374 "'%s'\n", p);
375 ret = -EINVAL;
376 goto out;
377 default:
378 break;
379 }
380 }
381 out:
382 kfree(orig);
383 return ret;
384 }
385
386 /*
387 * Parse mount options that are required early in the mount process.
388 *
389 * All other options will be parsed on much later in the mount process and
390 * only when we need to allocate a new super block.
391 */
392 static int btrfs_parse_early_options(const char *options, fmode_t flags,
393 void *holder, char **subvol_name, u64 *subvol_objectid,
394 u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
395 {
396 substring_t args[MAX_OPT_ARGS];
397 char *opts, *orig, *p;
398 int error = 0;
399 int intarg;
400
401 if (!options)
402 goto out;
403
404 /*
405 * strsep changes the string, duplicate it because parse_options
406 * gets called twice
407 */
408 opts = kstrdup(options, GFP_KERNEL);
409 if (!opts)
410 return -ENOMEM;
411 orig = opts;
412
413 while ((p = strsep(&opts, ",")) != NULL) {
414 int token;
415 if (!*p)
416 continue;
417
418 token = match_token(p, tokens, args);
419 switch (token) {
420 case Opt_subvol:
421 *subvol_name = match_strdup(&args[0]);
422 break;
423 case Opt_subvolid:
424 intarg = 0;
425 error = match_int(&args[0], &intarg);
426 if (!error) {
427 /* we want the original fs_tree */
428 if (!intarg)
429 *subvol_objectid =
430 BTRFS_FS_TREE_OBJECTID;
431 else
432 *subvol_objectid = intarg;
433 }
434 break;
435 case Opt_subvolrootid:
436 intarg = 0;
437 error = match_int(&args[0], &intarg);
438 if (!error) {
439 /* we want the original fs_tree */
440 if (!intarg)
441 *subvol_rootid =
442 BTRFS_FS_TREE_OBJECTID;
443 else
444 *subvol_rootid = intarg;
445 }
446 break;
447 case Opt_device:
448 error = btrfs_scan_one_device(match_strdup(&args[0]),
449 flags, holder, fs_devices);
450 if (error)
451 goto out_free_opts;
452 break;
453 default:
454 break;
455 }
456 }
457
458 out_free_opts:
459 kfree(orig);
460 out:
461 /*
462 * If no subvolume name is specified we use the default one. Allocate
463 * a copy of the string "." here so that code later in the
464 * mount path doesn't care if it's the default volume or another one.
465 */
466 if (!*subvol_name) {
467 *subvol_name = kstrdup(".", GFP_KERNEL);
468 if (!*subvol_name)
469 return -ENOMEM;
470 }
471 return error;
472 }
473
474 static struct dentry *get_default_root(struct super_block *sb,
475 u64 subvol_objectid)
476 {
477 struct btrfs_root *root = sb->s_fs_info;
478 struct btrfs_root *new_root;
479 struct btrfs_dir_item *di;
480 struct btrfs_path *path;
481 struct btrfs_key location;
482 struct inode *inode;
483 struct dentry *dentry;
484 u64 dir_id;
485 int new = 0;
486
487 /*
488 * We have a specific subvol we want to mount, just setup location and
489 * go look up the root.
490 */
491 if (subvol_objectid) {
492 location.objectid = subvol_objectid;
493 location.type = BTRFS_ROOT_ITEM_KEY;
494 location.offset = (u64)-1;
495 goto find_root;
496 }
497
498 path = btrfs_alloc_path();
499 if (!path)
500 return ERR_PTR(-ENOMEM);
501 path->leave_spinning = 1;
502
503 /*
504 * Find the "default" dir item which points to the root item that we
505 * will mount by default if we haven't been given a specific subvolume
506 * to mount.
507 */
508 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
509 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
510 if (IS_ERR(di)) {
511 btrfs_free_path(path);
512 return ERR_CAST(di);
513 }
514 if (!di) {
515 /*
516 * Ok the default dir item isn't there. This is weird since
517 * it's always been there, but don't freak out, just try and
518 * mount to root most subvolume.
519 */
520 btrfs_free_path(path);
521 dir_id = BTRFS_FIRST_FREE_OBJECTID;
522 new_root = root->fs_info->fs_root;
523 goto setup_root;
524 }
525
526 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
527 btrfs_free_path(path);
528
529 find_root:
530 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
531 if (IS_ERR(new_root))
532 return ERR_CAST(new_root);
533
534 if (btrfs_root_refs(&new_root->root_item) == 0)
535 return ERR_PTR(-ENOENT);
536
537 dir_id = btrfs_root_dirid(&new_root->root_item);
538 setup_root:
539 location.objectid = dir_id;
540 location.type = BTRFS_INODE_ITEM_KEY;
541 location.offset = 0;
542
543 inode = btrfs_iget(sb, &location, new_root, &new);
544 if (IS_ERR(inode))
545 return ERR_CAST(inode);
546
547 /*
548 * If we're just mounting the root most subvol put the inode and return
549 * a reference to the dentry. We will have already gotten a reference
550 * to the inode in btrfs_fill_super so we're good to go.
551 */
552 if (!new && sb->s_root->d_inode == inode) {
553 iput(inode);
554 return dget(sb->s_root);
555 }
556
557 if (new) {
558 const struct qstr name = { .name = "/", .len = 1 };
559
560 /*
561 * New inode, we need to make the dentry a sibling of s_root so
562 * everything gets cleaned up properly on unmount.
563 */
564 dentry = d_alloc(sb->s_root, &name);
565 if (!dentry) {
566 iput(inode);
567 return ERR_PTR(-ENOMEM);
568 }
569 d_splice_alias(inode, dentry);
570 } else {
571 /*
572 * We found the inode in cache, just find a dentry for it and
573 * put the reference to the inode we just got.
574 */
575 dentry = d_find_alias(inode);
576 iput(inode);
577 }
578
579 return dentry;
580 }
581
582 static int btrfs_fill_super(struct super_block *sb,
583 struct btrfs_fs_devices *fs_devices,
584 void *data, int silent)
585 {
586 struct inode *inode;
587 struct dentry *root_dentry;
588 struct btrfs_root *tree_root;
589 struct btrfs_key key;
590 int err;
591
592 sb->s_maxbytes = MAX_LFS_FILESIZE;
593 sb->s_magic = BTRFS_SUPER_MAGIC;
594 sb->s_op = &btrfs_super_ops;
595 sb->s_d_op = &btrfs_dentry_operations;
596 sb->s_export_op = &btrfs_export_ops;
597 sb->s_xattr = btrfs_xattr_handlers;
598 sb->s_time_gran = 1;
599 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
600 sb->s_flags |= MS_POSIXACL;
601 #endif
602
603 tree_root = open_ctree(sb, fs_devices, (char *)data);
604
605 if (IS_ERR(tree_root)) {
606 printk("btrfs: open_ctree failed\n");
607 return PTR_ERR(tree_root);
608 }
609 sb->s_fs_info = tree_root;
610
611 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
612 key.type = BTRFS_INODE_ITEM_KEY;
613 key.offset = 0;
614 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
615 if (IS_ERR(inode)) {
616 err = PTR_ERR(inode);
617 goto fail_close;
618 }
619
620 root_dentry = d_alloc_root(inode);
621 if (!root_dentry) {
622 iput(inode);
623 err = -ENOMEM;
624 goto fail_close;
625 }
626
627 sb->s_root = root_dentry;
628
629 save_mount_options(sb, data);
630 return 0;
631
632 fail_close:
633 close_ctree(tree_root);
634 return err;
635 }
636
637 int btrfs_sync_fs(struct super_block *sb, int wait)
638 {
639 struct btrfs_trans_handle *trans;
640 struct btrfs_root *root = btrfs_sb(sb);
641 int ret;
642
643 trace_btrfs_sync_fs(wait);
644
645 if (!wait) {
646 filemap_flush(root->fs_info->btree_inode->i_mapping);
647 return 0;
648 }
649
650 btrfs_start_delalloc_inodes(root, 0);
651 btrfs_wait_ordered_extents(root, 0, 0);
652
653 trans = btrfs_start_transaction(root, 0);
654 if (IS_ERR(trans))
655 return PTR_ERR(trans);
656 ret = btrfs_commit_transaction(trans, root);
657 return ret;
658 }
659
660 static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
661 {
662 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
663 struct btrfs_fs_info *info = root->fs_info;
664 char *compress_type;
665
666 if (btrfs_test_opt(root, DEGRADED))
667 seq_puts(seq, ",degraded");
668 if (btrfs_test_opt(root, NODATASUM))
669 seq_puts(seq, ",nodatasum");
670 if (btrfs_test_opt(root, NODATACOW))
671 seq_puts(seq, ",nodatacow");
672 if (btrfs_test_opt(root, NOBARRIER))
673 seq_puts(seq, ",nobarrier");
674 if (info->max_inline != 8192 * 1024)
675 seq_printf(seq, ",max_inline=%llu",
676 (unsigned long long)info->max_inline);
677 if (info->alloc_start != 0)
678 seq_printf(seq, ",alloc_start=%llu",
679 (unsigned long long)info->alloc_start);
680 if (info->thread_pool_size != min_t(unsigned long,
681 num_online_cpus() + 2, 8))
682 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
683 if (btrfs_test_opt(root, COMPRESS)) {
684 if (info->compress_type == BTRFS_COMPRESS_ZLIB)
685 compress_type = "zlib";
686 else
687 compress_type = "lzo";
688 if (btrfs_test_opt(root, FORCE_COMPRESS))
689 seq_printf(seq, ",compress-force=%s", compress_type);
690 else
691 seq_printf(seq, ",compress=%s", compress_type);
692 }
693 if (btrfs_test_opt(root, NOSSD))
694 seq_puts(seq, ",nossd");
695 if (btrfs_test_opt(root, SSD_SPREAD))
696 seq_puts(seq, ",ssd_spread");
697 else if (btrfs_test_opt(root, SSD))
698 seq_puts(seq, ",ssd");
699 if (btrfs_test_opt(root, NOTREELOG))
700 seq_puts(seq, ",notreelog");
701 if (btrfs_test_opt(root, FLUSHONCOMMIT))
702 seq_puts(seq, ",flushoncommit");
703 if (btrfs_test_opt(root, DISCARD))
704 seq_puts(seq, ",discard");
705 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
706 seq_puts(seq, ",noacl");
707 if (btrfs_test_opt(root, SPACE_CACHE))
708 seq_puts(seq, ",space_cache");
709 if (btrfs_test_opt(root, CLEAR_CACHE))
710 seq_puts(seq, ",clear_cache");
711 if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
712 seq_puts(seq, ",user_subvol_rm_allowed");
713 return 0;
714 }
715
716 static int btrfs_test_super(struct super_block *s, void *data)
717 {
718 struct btrfs_root *test_root = data;
719 struct btrfs_root *root = btrfs_sb(s);
720
721 /*
722 * If this super block is going away, return false as it
723 * can't match as an existing super block.
724 */
725 if (!atomic_read(&s->s_active))
726 return 0;
727 return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
728 }
729
730 static int btrfs_set_super(struct super_block *s, void *data)
731 {
732 s->s_fs_info = data;
733
734 return set_anon_super(s, data);
735 }
736
737
738 /*
739 * Find a superblock for the given device / mount point.
740 *
741 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
742 * for multiple device setup. Make sure to keep it in sync.
743 */
744 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
745 const char *device_name, void *data)
746 {
747 struct block_device *bdev = NULL;
748 struct super_block *s;
749 struct dentry *root;
750 struct btrfs_fs_devices *fs_devices = NULL;
751 struct btrfs_root *tree_root = NULL;
752 struct btrfs_fs_info *fs_info = NULL;
753 fmode_t mode = FMODE_READ;
754 char *subvol_name = NULL;
755 u64 subvol_objectid = 0;
756 u64 subvol_rootid = 0;
757 int error = 0;
758
759 if (!(flags & MS_RDONLY))
760 mode |= FMODE_WRITE;
761
762 error = btrfs_parse_early_options(data, mode, fs_type,
763 &subvol_name, &subvol_objectid,
764 &subvol_rootid, &fs_devices);
765 if (error)
766 return ERR_PTR(error);
767
768 error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
769 if (error)
770 goto error_free_subvol_name;
771
772 error = btrfs_open_devices(fs_devices, mode, fs_type);
773 if (error)
774 goto error_free_subvol_name;
775
776 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
777 error = -EACCES;
778 goto error_close_devices;
779 }
780
781 /*
782 * Setup a dummy root and fs_info for test/set super. This is because
783 * we don't actually fill this stuff out until open_ctree, but we need
784 * it for searching for existing supers, so this lets us do that and
785 * then open_ctree will properly initialize everything later.
786 */
787 fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
788 tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
789 if (!fs_info || !tree_root) {
790 error = -ENOMEM;
791 goto error_close_devices;
792 }
793 fs_info->tree_root = tree_root;
794 fs_info->fs_devices = fs_devices;
795 tree_root->fs_info = fs_info;
796
797 bdev = fs_devices->latest_bdev;
798 s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
799 if (IS_ERR(s))
800 goto error_s;
801
802 if (s->s_root) {
803 if ((flags ^ s->s_flags) & MS_RDONLY) {
804 deactivate_locked_super(s);
805 error = -EBUSY;
806 goto error_close_devices;
807 }
808
809 btrfs_close_devices(fs_devices);
810 kfree(fs_info);
811 kfree(tree_root);
812 } else {
813 char b[BDEVNAME_SIZE];
814
815 s->s_flags = flags;
816 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
817 error = btrfs_fill_super(s, fs_devices, data,
818 flags & MS_SILENT ? 1 : 0);
819 if (error) {
820 deactivate_locked_super(s);
821 goto error_free_subvol_name;
822 }
823
824 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
825 s->s_flags |= MS_ACTIVE;
826 }
827
828 /* if they gave us a subvolume name bind mount into that */
829 if (strcmp(subvol_name, ".")) {
830 struct dentry *new_root;
831
832 root = get_default_root(s, subvol_rootid);
833 if (IS_ERR(root)) {
834 error = PTR_ERR(root);
835 deactivate_locked_super(s);
836 goto error_free_subvol_name;
837 }
838
839 mutex_lock(&root->d_inode->i_mutex);
840 new_root = lookup_one_len(subvol_name, root,
841 strlen(subvol_name));
842 mutex_unlock(&root->d_inode->i_mutex);
843
844 if (IS_ERR(new_root)) {
845 dput(root);
846 deactivate_locked_super(s);
847 error = PTR_ERR(new_root);
848 goto error_free_subvol_name;
849 }
850 if (!new_root->d_inode) {
851 dput(root);
852 dput(new_root);
853 deactivate_locked_super(s);
854 error = -ENXIO;
855 goto error_free_subvol_name;
856 }
857 dput(root);
858 root = new_root;
859 } else {
860 root = get_default_root(s, subvol_objectid);
861 if (IS_ERR(root)) {
862 error = PTR_ERR(root);
863 deactivate_locked_super(s);
864 goto error_free_subvol_name;
865 }
866 }
867
868 kfree(subvol_name);
869 return root;
870
871 error_s:
872 error = PTR_ERR(s);
873 error_close_devices:
874 btrfs_close_devices(fs_devices);
875 kfree(fs_info);
876 kfree(tree_root);
877 error_free_subvol_name:
878 kfree(subvol_name);
879 return ERR_PTR(error);
880 }
881
882 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
883 {
884 struct btrfs_root *root = btrfs_sb(sb);
885 int ret;
886
887 ret = btrfs_parse_options(root, data);
888 if (ret)
889 return -EINVAL;
890
891 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
892 return 0;
893
894 if (*flags & MS_RDONLY) {
895 sb->s_flags |= MS_RDONLY;
896
897 ret = btrfs_commit_super(root);
898 WARN_ON(ret);
899 } else {
900 if (root->fs_info->fs_devices->rw_devices == 0)
901 return -EACCES;
902
903 if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
904 return -EINVAL;
905
906 ret = btrfs_cleanup_fs_roots(root->fs_info);
907 WARN_ON(ret);
908
909 /* recover relocation */
910 ret = btrfs_recover_relocation(root);
911 WARN_ON(ret);
912
913 sb->s_flags &= ~MS_RDONLY;
914 }
915
916 return 0;
917 }
918
919 /* Used to sort the devices by max_avail(descending sort) */
920 static int btrfs_cmp_device_free_bytes(const void *dev_info1,
921 const void *dev_info2)
922 {
923 if (((struct btrfs_device_info *)dev_info1)->max_avail >
924 ((struct btrfs_device_info *)dev_info2)->max_avail)
925 return -1;
926 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
927 ((struct btrfs_device_info *)dev_info2)->max_avail)
928 return 1;
929 else
930 return 0;
931 }
932
933 /*
934 * sort the devices by max_avail, in which max free extent size of each device
935 * is stored.(Descending Sort)
936 */
937 static inline void btrfs_descending_sort_devices(
938 struct btrfs_device_info *devices,
939 size_t nr_devices)
940 {
941 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
942 btrfs_cmp_device_free_bytes, NULL);
943 }
944
945 /*
946 * The helper to calc the free space on the devices that can be used to store
947 * file data.
948 */
949 static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
950 {
951 struct btrfs_fs_info *fs_info = root->fs_info;
952 struct btrfs_device_info *devices_info;
953 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
954 struct btrfs_device *device;
955 u64 skip_space;
956 u64 type;
957 u64 avail_space;
958 u64 used_space;
959 u64 min_stripe_size;
960 int min_stripes = 1;
961 int i = 0, nr_devices;
962 int ret;
963
964 nr_devices = fs_info->fs_devices->rw_devices;
965 BUG_ON(!nr_devices);
966
967 devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
968 GFP_NOFS);
969 if (!devices_info)
970 return -ENOMEM;
971
972 /* calc min stripe number for data space alloction */
973 type = btrfs_get_alloc_profile(root, 1);
974 if (type & BTRFS_BLOCK_GROUP_RAID0)
975 min_stripes = 2;
976 else if (type & BTRFS_BLOCK_GROUP_RAID1)
977 min_stripes = 2;
978 else if (type & BTRFS_BLOCK_GROUP_RAID10)
979 min_stripes = 4;
980
981 if (type & BTRFS_BLOCK_GROUP_DUP)
982 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
983 else
984 min_stripe_size = BTRFS_STRIPE_LEN;
985
986 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
987 if (!device->in_fs_metadata)
988 continue;
989
990 avail_space = device->total_bytes - device->bytes_used;
991
992 /* align with stripe_len */
993 do_div(avail_space, BTRFS_STRIPE_LEN);
994 avail_space *= BTRFS_STRIPE_LEN;
995
996 /*
997 * In order to avoid overwritting the superblock on the drive,
998 * btrfs starts at an offset of at least 1MB when doing chunk
999 * allocation.
1000 */
1001 skip_space = 1024 * 1024;
1002
1003 /* user can set the offset in fs_info->alloc_start. */
1004 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
1005 device->total_bytes)
1006 skip_space = max(fs_info->alloc_start, skip_space);
1007
1008 /*
1009 * btrfs can not use the free space in [0, skip_space - 1],
1010 * we must subtract it from the total. In order to implement
1011 * it, we account the used space in this range first.
1012 */
1013 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
1014 &used_space);
1015 if (ret) {
1016 kfree(devices_info);
1017 return ret;
1018 }
1019
1020 /* calc the free space in [0, skip_space - 1] */
1021 skip_space -= used_space;
1022
1023 /*
1024 * we can use the free space in [0, skip_space - 1], subtract
1025 * it from the total.
1026 */
1027 if (avail_space && avail_space >= skip_space)
1028 avail_space -= skip_space;
1029 else
1030 avail_space = 0;
1031
1032 if (avail_space < min_stripe_size)
1033 continue;
1034
1035 devices_info[i].dev = device;
1036 devices_info[i].max_avail = avail_space;
1037
1038 i++;
1039 }
1040
1041 nr_devices = i;
1042
1043 btrfs_descending_sort_devices(devices_info, nr_devices);
1044
1045 i = nr_devices - 1;
1046 avail_space = 0;
1047 while (nr_devices >= min_stripes) {
1048 if (devices_info[i].max_avail >= min_stripe_size) {
1049 int j;
1050 u64 alloc_size;
1051
1052 avail_space += devices_info[i].max_avail * min_stripes;
1053 alloc_size = devices_info[i].max_avail;
1054 for (j = i + 1 - min_stripes; j <= i; j++)
1055 devices_info[j].max_avail -= alloc_size;
1056 }
1057 i--;
1058 nr_devices--;
1059 }
1060
1061 kfree(devices_info);
1062 *free_bytes = avail_space;
1063 return 0;
1064 }
1065
1066 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1067 {
1068 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
1069 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1070 struct list_head *head = &root->fs_info->space_info;
1071 struct btrfs_space_info *found;
1072 u64 total_used = 0;
1073 u64 total_free_data = 0;
1074 int bits = dentry->d_sb->s_blocksize_bits;
1075 __be32 *fsid = (__be32 *)root->fs_info->fsid;
1076 int ret;
1077
1078 /* holding chunk_muext to avoid allocating new chunks */
1079 mutex_lock(&root->fs_info->chunk_mutex);
1080 rcu_read_lock();
1081 list_for_each_entry_rcu(found, head, list) {
1082 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1083 total_free_data += found->disk_total - found->disk_used;
1084 total_free_data -=
1085 btrfs_account_ro_block_groups_free_space(found);
1086 }
1087
1088 total_used += found->disk_used;
1089 }
1090 rcu_read_unlock();
1091
1092 buf->f_namelen = BTRFS_NAME_LEN;
1093 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1094 buf->f_bfree = buf->f_blocks - (total_used >> bits);
1095 buf->f_bsize = dentry->d_sb->s_blocksize;
1096 buf->f_type = BTRFS_SUPER_MAGIC;
1097 buf->f_bavail = total_free_data;
1098 ret = btrfs_calc_avail_data_space(root, &total_free_data);
1099 if (ret) {
1100 mutex_unlock(&root->fs_info->chunk_mutex);
1101 return ret;
1102 }
1103 buf->f_bavail += total_free_data;
1104 buf->f_bavail = buf->f_bavail >> bits;
1105 mutex_unlock(&root->fs_info->chunk_mutex);
1106
1107 /* We treat it as constant endianness (it doesn't matter _which_)
1108 because we want the fsid to come out the same whether mounted
1109 on a big-endian or little-endian host */
1110 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
1111 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
1112 /* Mask in the root object ID too, to disambiguate subvols */
1113 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
1114 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
1115
1116 return 0;
1117 }
1118
1119 static struct file_system_type btrfs_fs_type = {
1120 .owner = THIS_MODULE,
1121 .name = "btrfs",
1122 .mount = btrfs_mount,
1123 .kill_sb = kill_anon_super,
1124 .fs_flags = FS_REQUIRES_DEV,
1125 };
1126
1127 /*
1128 * used by btrfsctl to scan devices when no FS is mounted
1129 */
1130 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
1131 unsigned long arg)
1132 {
1133 struct btrfs_ioctl_vol_args *vol;
1134 struct btrfs_fs_devices *fs_devices;
1135 int ret = -ENOTTY;
1136
1137 if (!capable(CAP_SYS_ADMIN))
1138 return -EPERM;
1139
1140 vol = memdup_user((void __user *)arg, sizeof(*vol));
1141 if (IS_ERR(vol))
1142 return PTR_ERR(vol);
1143
1144 switch (cmd) {
1145 case BTRFS_IOC_SCAN_DEV:
1146 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
1147 &btrfs_fs_type, &fs_devices);
1148 break;
1149 }
1150
1151 kfree(vol);
1152 return ret;
1153 }
1154
1155 static int btrfs_freeze(struct super_block *sb)
1156 {
1157 struct btrfs_root *root = btrfs_sb(sb);
1158 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1159 mutex_lock(&root->fs_info->cleaner_mutex);
1160 return 0;
1161 }
1162
1163 static int btrfs_unfreeze(struct super_block *sb)
1164 {
1165 struct btrfs_root *root = btrfs_sb(sb);
1166 mutex_unlock(&root->fs_info->cleaner_mutex);
1167 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1168 return 0;
1169 }
1170
1171 static const struct super_operations btrfs_super_ops = {
1172 .drop_inode = btrfs_drop_inode,
1173 .evict_inode = btrfs_evict_inode,
1174 .put_super = btrfs_put_super,
1175 .sync_fs = btrfs_sync_fs,
1176 .show_options = btrfs_show_options,
1177 .write_inode = btrfs_write_inode,
1178 .dirty_inode = btrfs_dirty_inode,
1179 .alloc_inode = btrfs_alloc_inode,
1180 .destroy_inode = btrfs_destroy_inode,
1181 .statfs = btrfs_statfs,
1182 .remount_fs = btrfs_remount,
1183 .freeze_fs = btrfs_freeze,
1184 .unfreeze_fs = btrfs_unfreeze,
1185 };
1186
1187 static const struct file_operations btrfs_ctl_fops = {
1188 .unlocked_ioctl = btrfs_control_ioctl,
1189 .compat_ioctl = btrfs_control_ioctl,
1190 .owner = THIS_MODULE,
1191 .llseek = noop_llseek,
1192 };
1193
1194 static struct miscdevice btrfs_misc = {
1195 .minor = BTRFS_MINOR,
1196 .name = "btrfs-control",
1197 .fops = &btrfs_ctl_fops
1198 };
1199
1200 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1201 MODULE_ALIAS("devname:btrfs-control");
1202
1203 static int btrfs_interface_init(void)
1204 {
1205 return misc_register(&btrfs_misc);
1206 }
1207
1208 static void btrfs_interface_exit(void)
1209 {
1210 if (misc_deregister(&btrfs_misc) < 0)
1211 printk(KERN_INFO "misc_deregister failed for control device");
1212 }
1213
1214 static int __init init_btrfs_fs(void)
1215 {
1216 int err;
1217
1218 err = btrfs_init_sysfs();
1219 if (err)
1220 return err;
1221
1222 err = btrfs_init_compress();
1223 if (err)
1224 goto free_sysfs;
1225
1226 err = btrfs_init_cachep();
1227 if (err)
1228 goto free_compress;
1229
1230 err = extent_io_init();
1231 if (err)
1232 goto free_cachep;
1233
1234 err = extent_map_init();
1235 if (err)
1236 goto free_extent_io;
1237
1238 err = btrfs_delayed_inode_init();
1239 if (err)
1240 goto free_extent_map;
1241
1242 err = btrfs_interface_init();
1243 if (err)
1244 goto free_delayed_inode;
1245
1246 err = register_filesystem(&btrfs_fs_type);
1247 if (err)
1248 goto unregister_ioctl;
1249
1250 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
1251 return 0;
1252
1253 unregister_ioctl:
1254 btrfs_interface_exit();
1255 free_delayed_inode:
1256 btrfs_delayed_inode_exit();
1257 free_extent_map:
1258 extent_map_exit();
1259 free_extent_io:
1260 extent_io_exit();
1261 free_cachep:
1262 btrfs_destroy_cachep();
1263 free_compress:
1264 btrfs_exit_compress();
1265 free_sysfs:
1266 btrfs_exit_sysfs();
1267 return err;
1268 }
1269
1270 static void __exit exit_btrfs_fs(void)
1271 {
1272 btrfs_destroy_cachep();
1273 btrfs_delayed_inode_exit();
1274 extent_map_exit();
1275 extent_io_exit();
1276 btrfs_interface_exit();
1277 unregister_filesystem(&btrfs_fs_type);
1278 btrfs_exit_sysfs();
1279 btrfs_cleanup_fs_uuids();
1280 btrfs_exit_compress();
1281 }
1282
1283 module_init(init_btrfs_fs)
1284 module_exit(exit_btrfs_fs)
1285
1286 MODULE_LICENSE("GPL");
This page took 0.094655 seconds and 4 git commands to generate.