Btrfs: add skip_balance mount option
[deliverable/linux.git] / fs / btrfs / super.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
4b82d6e4 19#include <linux/blkdev.h>
2e635a27 20#include <linux/module.h>
e20d96d6 21#include <linux/buffer_head.h>
2e635a27
CM
22#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/highmem.h>
25#include <linux/time.h>
26#include <linux/init.h>
a9572a15 27#include <linux/seq_file.h>
2e635a27 28#include <linux/string.h>
2e635a27 29#include <linux/backing-dev.h>
4b82d6e4 30#include <linux/mount.h>
dee26a9f 31#include <linux/mpage.h>
75dfe396
CM
32#include <linux/swap.h>
33#include <linux/writeback.h>
8fd17795 34#include <linux/statfs.h>
08607c1b 35#include <linux/compat.h>
95e05289 36#include <linux/parser.h>
c59f8951 37#include <linux/ctype.h>
6da6abae 38#include <linux/namei.h>
a9218f6b 39#include <linux/miscdevice.h>
1bcbf313 40#include <linux/magic.h>
5a0e3ad6 41#include <linux/slab.h>
90a887c9 42#include <linux/cleancache.h>
830c4adb 43#include <linux/mnt_namespace.h>
22c44fe6 44#include <linux/ratelimit.h>
4b4e25f2 45#include "compat.h"
16cdcec7 46#include "delayed-inode.h"
2e635a27 47#include "ctree.h"
e20d96d6 48#include "disk-io.h"
d5719762 49#include "transaction.h"
2c90e5d6 50#include "btrfs_inode.h"
c5739bba 51#include "ioctl.h"
3a686375 52#include "print-tree.h"
5103e947 53#include "xattr.h"
8a4b83cc 54#include "volumes.h"
b3c3da71 55#include "version.h"
be6e8dc0 56#include "export.h"
c8b97818 57#include "compression.h"
2e635a27 58
1abe9b8a 59#define CREATE_TRACE_POINTS
60#include <trace/events/btrfs.h>
61
b87221de 62static const struct super_operations btrfs_super_ops;
830c4adb 63static struct file_system_type btrfs_fs_type;
75dfe396 64
acce952b 65static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno,
66 char nbuf[16])
67{
68 char *errstr = NULL;
69
70 switch (errno) {
71 case -EIO:
72 errstr = "IO failure";
73 break;
74 case -ENOMEM:
75 errstr = "Out of memory";
76 break;
77 case -EROFS:
78 errstr = "Readonly filesystem";
79 break;
80 default:
81 if (nbuf) {
82 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
83 errstr = nbuf;
84 }
85 break;
86 }
87
88 return errstr;
89}
90
91static void __save_error_info(struct btrfs_fs_info *fs_info)
92{
93 /*
94 * today we only save the error info into ram. Long term we'll
95 * also send it down to the disk
96 */
97 fs_info->fs_state = BTRFS_SUPER_FLAG_ERROR;
98}
99
100/* NOTE:
101 * We move write_super stuff at umount in order to avoid deadlock
102 * for umount hold all lock.
103 */
104static void save_error_info(struct btrfs_fs_info *fs_info)
105{
106 __save_error_info(fs_info);
107}
108
109/* btrfs handle error by forcing the filesystem readonly */
110static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
111{
112 struct super_block *sb = fs_info->sb;
113
114 if (sb->s_flags & MS_RDONLY)
115 return;
116
117 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
118 sb->s_flags |= MS_RDONLY;
119 printk(KERN_INFO "btrfs is forced readonly\n");
120 }
121}
122
123/*
124 * __btrfs_std_error decodes expected errors from the caller and
125 * invokes the approciate error response.
126 */
127void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
128 unsigned int line, int errno)
129{
130 struct super_block *sb = fs_info->sb;
131 char nbuf[16];
132 const char *errstr;
133
134 /*
135 * Special case: if the error is EROFS, and we're already
136 * under MS_RDONLY, then it is safe here.
137 */
138 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
139 return;
140
141 errstr = btrfs_decode_error(fs_info, errno, nbuf);
142 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: %s\n",
143 sb->s_id, function, line, errstr);
144 save_error_info(fs_info);
145
146 btrfs_handle_error(fs_info);
147}
148
d397712b 149static void btrfs_put_super(struct super_block *sb)
b18c6685 150{
39279cc3 151 struct btrfs_root *root = btrfs_sb(sb);
b18c6685 152 int ret;
b18c6685 153
39279cc3 154 ret = close_ctree(root);
39279cc3 155 sb->s_fs_info = NULL;
559af821
AK
156
157 (void)ret; /* FIXME: need to fix VFS to return error? */
75dfe396
CM
158}
159
95e05289 160enum {
73f73415 161 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
287a0ab9
JB
162 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
163 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
261507a0
LZ
164 Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
165 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
91435650 166 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
9555c6c1
ID
167 Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
168 Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
169 Opt_err,
95e05289
CM
170};
171
172static match_table_t tokens = {
dfe25020 173 {Opt_degraded, "degraded"},
95e05289 174 {Opt_subvol, "subvol=%s"},
73f73415 175 {Opt_subvolid, "subvolid=%d"},
43e570b0 176 {Opt_device, "device=%s"},
b6cda9bc 177 {Opt_nodatasum, "nodatasum"},
be20aa9d 178 {Opt_nodatacow, "nodatacow"},
21ad10cf 179 {Opt_nobarrier, "nobarrier"},
6f568d35 180 {Opt_max_inline, "max_inline=%s"},
8f662a76 181 {Opt_alloc_start, "alloc_start=%s"},
4543df7e 182 {Opt_thread_pool, "thread_pool=%d"},
c8b97818 183 {Opt_compress, "compress"},
261507a0 184 {Opt_compress_type, "compress=%s"},
a555f810 185 {Opt_compress_force, "compress-force"},
261507a0 186 {Opt_compress_force_type, "compress-force=%s"},
e18e4809 187 {Opt_ssd, "ssd"},
451d7585 188 {Opt_ssd_spread, "ssd_spread"},
3b30c22f 189 {Opt_nossd, "nossd"},
33268eaf 190 {Opt_noacl, "noacl"},
3a5e1404 191 {Opt_notreelog, "notreelog"},
dccae999 192 {Opt_flushoncommit, "flushoncommit"},
97e728d4 193 {Opt_ratio, "metadata_ratio=%d"},
e244a0ae 194 {Opt_discard, "discard"},
0af3d00b 195 {Opt_space_cache, "space_cache"},
88c2ba3b 196 {Opt_clear_cache, "clear_cache"},
4260f7c7 197 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
91435650 198 {Opt_enospc_debug, "enospc_debug"},
e15d0542 199 {Opt_subvolrootid, "subvolrootid=%d"},
4cb5300b 200 {Opt_defrag, "autodefrag"},
4b9465cb 201 {Opt_inode_cache, "inode_cache"},
8965593e 202 {Opt_no_space_cache, "nospace_cache"},
af31f5e5 203 {Opt_recovery, "recovery"},
9555c6c1 204 {Opt_skip_balance, "skip_balance"},
33268eaf 205 {Opt_err, NULL},
95e05289
CM
206};
207
edf24abe
CH
208/*
209 * Regular mount options parser. Everything that is needed only when
210 * reading in a new superblock is parsed here.
211 */
212int btrfs_parse_options(struct btrfs_root *root, char *options)
95e05289 213{
edf24abe 214 struct btrfs_fs_info *info = root->fs_info;
95e05289 215 substring_t args[MAX_OPT_ARGS];
73bc1876
JB
216 char *p, *num, *orig = NULL;
217 u64 cache_gen;
4543df7e 218 int intarg;
a7a3f7ca 219 int ret = 0;
261507a0
LZ
220 char *compress_type;
221 bool compress_force = false;
b6cda9bc 222
6c41761f 223 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
73bc1876
JB
224 if (cache_gen)
225 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
226
95e05289 227 if (!options)
73bc1876 228 goto out;
95e05289 229
be20aa9d
CM
230 /*
231 * strsep changes the string, duplicate it because parse_options
232 * gets called twice
233 */
234 options = kstrdup(options, GFP_NOFS);
235 if (!options)
236 return -ENOMEM;
237
da495ecc 238 orig = options;
be20aa9d 239
edf24abe 240 while ((p = strsep(&options, ",")) != NULL) {
95e05289
CM
241 int token;
242 if (!*p)
243 continue;
244
245 token = match_token(p, tokens, args);
246 switch (token) {
dfe25020 247 case Opt_degraded:
edf24abe
CH
248 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
249 btrfs_set_opt(info->mount_opt, DEGRADED);
dfe25020 250 break;
95e05289 251 case Opt_subvol:
73f73415 252 case Opt_subvolid:
e15d0542 253 case Opt_subvolrootid:
43e570b0 254 case Opt_device:
edf24abe 255 /*
43e570b0 256 * These are parsed by btrfs_parse_early_options
edf24abe
CH
257 * and can be happily ignored here.
258 */
b6cda9bc
CM
259 break;
260 case Opt_nodatasum:
067c28ad 261 printk(KERN_INFO "btrfs: setting nodatasum\n");
edf24abe 262 btrfs_set_opt(info->mount_opt, NODATASUM);
be20aa9d
CM
263 break;
264 case Opt_nodatacow:
edf24abe
CH
265 printk(KERN_INFO "btrfs: setting nodatacow\n");
266 btrfs_set_opt(info->mount_opt, NODATACOW);
267 btrfs_set_opt(info->mount_opt, NODATASUM);
95e05289 268 break;
a555f810 269 case Opt_compress_force:
261507a0
LZ
270 case Opt_compress_force_type:
271 compress_force = true;
272 case Opt_compress:
273 case Opt_compress_type:
274 if (token == Opt_compress ||
275 token == Opt_compress_force ||
276 strcmp(args[0].from, "zlib") == 0) {
277 compress_type = "zlib";
278 info->compress_type = BTRFS_COMPRESS_ZLIB;
a6fa6fae
LZ
279 } else if (strcmp(args[0].from, "lzo") == 0) {
280 compress_type = "lzo";
281 info->compress_type = BTRFS_COMPRESS_LZO;
261507a0
LZ
282 } else {
283 ret = -EINVAL;
284 goto out;
285 }
286
a555f810 287 btrfs_set_opt(info->mount_opt, COMPRESS);
261507a0
LZ
288 if (compress_force) {
289 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
290 pr_info("btrfs: force %s compression\n",
291 compress_type);
292 } else
293 pr_info("btrfs: use %s compression\n",
294 compress_type);
a555f810 295 break;
e18e4809 296 case Opt_ssd:
edf24abe
CH
297 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
298 btrfs_set_opt(info->mount_opt, SSD);
e18e4809 299 break;
451d7585
CM
300 case Opt_ssd_spread:
301 printk(KERN_INFO "btrfs: use spread ssd "
302 "allocation scheme\n");
303 btrfs_set_opt(info->mount_opt, SSD);
304 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
305 break;
3b30c22f 306 case Opt_nossd:
451d7585
CM
307 printk(KERN_INFO "btrfs: not using ssd allocation "
308 "scheme\n");
c289811c 309 btrfs_set_opt(info->mount_opt, NOSSD);
3b30c22f 310 btrfs_clear_opt(info->mount_opt, SSD);
451d7585 311 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3b30c22f 312 break;
21ad10cf 313 case Opt_nobarrier:
edf24abe
CH
314 printk(KERN_INFO "btrfs: turning off barriers\n");
315 btrfs_set_opt(info->mount_opt, NOBARRIER);
21ad10cf 316 break;
4543df7e
CM
317 case Opt_thread_pool:
318 intarg = 0;
319 match_int(&args[0], &intarg);
320 if (intarg) {
321 info->thread_pool_size = intarg;
322 printk(KERN_INFO "btrfs: thread pool %d\n",
323 info->thread_pool_size);
324 }
325 break;
6f568d35 326 case Opt_max_inline:
edf24abe
CH
327 num = match_strdup(&args[0]);
328 if (num) {
91748467 329 info->max_inline = memparse(num, NULL);
edf24abe
CH
330 kfree(num);
331
15ada040
CM
332 if (info->max_inline) {
333 info->max_inline = max_t(u64,
334 info->max_inline,
335 root->sectorsize);
336 }
edf24abe 337 printk(KERN_INFO "btrfs: max_inline at %llu\n",
21380931 338 (unsigned long long)info->max_inline);
6f568d35
CM
339 }
340 break;
8f662a76 341 case Opt_alloc_start:
edf24abe
CH
342 num = match_strdup(&args[0]);
343 if (num) {
91748467 344 info->alloc_start = memparse(num, NULL);
edf24abe
CH
345 kfree(num);
346 printk(KERN_INFO
347 "btrfs: allocations start at %llu\n",
21380931 348 (unsigned long long)info->alloc_start);
8f662a76
CM
349 }
350 break;
33268eaf
JB
351 case Opt_noacl:
352 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
353 break;
3a5e1404
SW
354 case Opt_notreelog:
355 printk(KERN_INFO "btrfs: disabling tree log\n");
356 btrfs_set_opt(info->mount_opt, NOTREELOG);
357 break;
dccae999
SW
358 case Opt_flushoncommit:
359 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
360 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
361 break;
97e728d4
JB
362 case Opt_ratio:
363 intarg = 0;
364 match_int(&args[0], &intarg);
365 if (intarg) {
366 info->metadata_ratio = intarg;
367 printk(KERN_INFO "btrfs: metadata ratio %d\n",
368 info->metadata_ratio);
369 }
370 break;
e244a0ae
CH
371 case Opt_discard:
372 btrfs_set_opt(info->mount_opt, DISCARD);
373 break;
0af3d00b 374 case Opt_space_cache:
0af3d00b 375 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
0de90876 376 break;
73bc1876
JB
377 case Opt_no_space_cache:
378 printk(KERN_INFO "btrfs: disabling disk space caching\n");
379 btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
380 break;
4b9465cb
CM
381 case Opt_inode_cache:
382 printk(KERN_INFO "btrfs: enabling inode map caching\n");
383 btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
384 break;
88c2ba3b
JB
385 case Opt_clear_cache:
386 printk(KERN_INFO "btrfs: force clearing of disk cache\n");
387 btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
0af3d00b 388 break;
4260f7c7
SW
389 case Opt_user_subvol_rm_allowed:
390 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
391 break;
91435650
CM
392 case Opt_enospc_debug:
393 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
394 break;
4cb5300b
CM
395 case Opt_defrag:
396 printk(KERN_INFO "btrfs: enabling auto defrag");
397 btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
398 break;
af31f5e5
CM
399 case Opt_recovery:
400 printk(KERN_INFO "btrfs: enabling auto recovery");
401 btrfs_set_opt(info->mount_opt, RECOVERY);
402 break;
9555c6c1
ID
403 case Opt_skip_balance:
404 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
405 break;
a7a3f7ca
SW
406 case Opt_err:
407 printk(KERN_INFO "btrfs: unrecognized mount option "
408 "'%s'\n", p);
409 ret = -EINVAL;
410 goto out;
95e05289 411 default:
be20aa9d 412 break;
95e05289
CM
413 }
414 }
a7a3f7ca 415out:
73bc1876
JB
416 if (!ret && btrfs_test_opt(root, SPACE_CACHE))
417 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
da495ecc 418 kfree(orig);
a7a3f7ca 419 return ret;
edf24abe
CH
420}
421
422/*
423 * Parse mount options that are required early in the mount process.
424 *
425 * All other options will be parsed on much later in the mount process and
426 * only when we need to allocate a new super block.
427 */
97288f2c 428static int btrfs_parse_early_options(const char *options, fmode_t flags,
73f73415 429 void *holder, char **subvol_name, u64 *subvol_objectid,
e15d0542 430 u64 *subvol_rootid, struct btrfs_fs_devices **fs_devices)
edf24abe
CH
431{
432 substring_t args[MAX_OPT_ARGS];
83c8c9bd 433 char *device_name, *opts, *orig, *p;
edf24abe 434 int error = 0;
73f73415 435 int intarg;
edf24abe
CH
436
437 if (!options)
830c4adb 438 return 0;
edf24abe
CH
439
440 /*
441 * strsep changes the string, duplicate it because parse_options
442 * gets called twice
443 */
444 opts = kstrdup(options, GFP_KERNEL);
445 if (!opts)
446 return -ENOMEM;
3f3d0bc0 447 orig = opts;
edf24abe
CH
448
449 while ((p = strsep(&opts, ",")) != NULL) {
450 int token;
451 if (!*p)
452 continue;
453
454 token = match_token(p, tokens, args);
455 switch (token) {
456 case Opt_subvol:
a90e8b6f 457 kfree(*subvol_name);
edf24abe
CH
458 *subvol_name = match_strdup(&args[0]);
459 break;
73f73415
JB
460 case Opt_subvolid:
461 intarg = 0;
4849f01d
JB
462 error = match_int(&args[0], &intarg);
463 if (!error) {
464 /* we want the original fs_tree */
465 if (!intarg)
466 *subvol_objectid =
467 BTRFS_FS_TREE_OBJECTID;
468 else
469 *subvol_objectid = intarg;
470 }
73f73415 471 break;
e15d0542
XZ
472 case Opt_subvolrootid:
473 intarg = 0;
474 error = match_int(&args[0], &intarg);
475 if (!error) {
476 /* we want the original fs_tree */
477 if (!intarg)
478 *subvol_rootid =
479 BTRFS_FS_TREE_OBJECTID;
480 else
481 *subvol_rootid = intarg;
482 }
483 break;
43e570b0 484 case Opt_device:
83c8c9bd
JL
485 device_name = match_strdup(&args[0]);
486 if (!device_name) {
487 error = -ENOMEM;
488 goto out;
489 }
490 error = btrfs_scan_one_device(device_name,
43e570b0 491 flags, holder, fs_devices);
83c8c9bd 492 kfree(device_name);
43e570b0 493 if (error)
830c4adb 494 goto out;
43e570b0 495 break;
edf24abe
CH
496 default:
497 break;
498 }
499 }
500
830c4adb 501out:
3f3d0bc0 502 kfree(orig);
edf24abe 503 return error;
95e05289
CM
504}
505
73f73415
JB
506static struct dentry *get_default_root(struct super_block *sb,
507 u64 subvol_objectid)
508{
509 struct btrfs_root *root = sb->s_fs_info;
510 struct btrfs_root *new_root;
511 struct btrfs_dir_item *di;
512 struct btrfs_path *path;
513 struct btrfs_key location;
514 struct inode *inode;
73f73415
JB
515 u64 dir_id;
516 int new = 0;
517
518 /*
519 * We have a specific subvol we want to mount, just setup location and
520 * go look up the root.
521 */
522 if (subvol_objectid) {
523 location.objectid = subvol_objectid;
524 location.type = BTRFS_ROOT_ITEM_KEY;
525 location.offset = (u64)-1;
526 goto find_root;
527 }
528
529 path = btrfs_alloc_path();
530 if (!path)
531 return ERR_PTR(-ENOMEM);
532 path->leave_spinning = 1;
533
534 /*
535 * Find the "default" dir item which points to the root item that we
536 * will mount by default if we haven't been given a specific subvolume
537 * to mount.
538 */
6c41761f 539 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
73f73415 540 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
b0839166
JL
541 if (IS_ERR(di)) {
542 btrfs_free_path(path);
fb4f6f91 543 return ERR_CAST(di);
b0839166 544 }
73f73415
JB
545 if (!di) {
546 /*
547 * Ok the default dir item isn't there. This is weird since
548 * it's always been there, but don't freak out, just try and
549 * mount to root most subvolume.
550 */
551 btrfs_free_path(path);
552 dir_id = BTRFS_FIRST_FREE_OBJECTID;
553 new_root = root->fs_info->fs_root;
554 goto setup_root;
555 }
556
557 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
558 btrfs_free_path(path);
559
560find_root:
561 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
562 if (IS_ERR(new_root))
d0b678cb 563 return ERR_CAST(new_root);
73f73415
JB
564
565 if (btrfs_root_refs(&new_root->root_item) == 0)
566 return ERR_PTR(-ENOENT);
567
568 dir_id = btrfs_root_dirid(&new_root->root_item);
569setup_root:
570 location.objectid = dir_id;
571 location.type = BTRFS_INODE_ITEM_KEY;
572 location.offset = 0;
573
574 inode = btrfs_iget(sb, &location, new_root, &new);
4cbd1149
DC
575 if (IS_ERR(inode))
576 return ERR_CAST(inode);
73f73415
JB
577
578 /*
579 * If we're just mounting the root most subvol put the inode and return
580 * a reference to the dentry. We will have already gotten a reference
581 * to the inode in btrfs_fill_super so we're good to go.
582 */
583 if (!new && sb->s_root->d_inode == inode) {
584 iput(inode);
585 return dget(sb->s_root);
586 }
587
ba5b8958 588 return d_obtain_alias(inode);
73f73415
JB
589}
590
d397712b 591static int btrfs_fill_super(struct super_block *sb,
8a4b83cc 592 struct btrfs_fs_devices *fs_devices,
d397712b 593 void *data, int silent)
75dfe396 594{
d397712b
CM
595 struct inode *inode;
596 struct dentry *root_dentry;
39279cc3 597 struct btrfs_root *tree_root;
5d4f98a2 598 struct btrfs_key key;
39279cc3 599 int err;
a429e513 600
39279cc3
CM
601 sb->s_maxbytes = MAX_LFS_FILESIZE;
602 sb->s_magic = BTRFS_SUPER_MAGIC;
603 sb->s_op = &btrfs_super_ops;
af53d29a 604 sb->s_d_op = &btrfs_dentry_operations;
be6e8dc0 605 sb->s_export_op = &btrfs_export_ops;
5103e947 606 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 607 sb->s_time_gran = 1;
0eda294d 608#ifdef CONFIG_BTRFS_FS_POSIX_ACL
33268eaf 609 sb->s_flags |= MS_POSIXACL;
49cf6f45 610#endif
a429e513 611
dfe25020 612 tree_root = open_ctree(sb, fs_devices, (char *)data);
6567e837 613
e58ca020 614 if (IS_ERR(tree_root)) {
39279cc3 615 printk("btrfs: open_ctree failed\n");
e58ca020 616 return PTR_ERR(tree_root);
a429e513 617 }
39279cc3 618 sb->s_fs_info = tree_root;
a429e513 619
5d4f98a2
YZ
620 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
621 key.type = BTRFS_INODE_ITEM_KEY;
622 key.offset = 0;
73f73415 623 inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
5d4f98a2
YZ
624 if (IS_ERR(inode)) {
625 err = PTR_ERR(inode);
39279cc3 626 goto fail_close;
f254e52c 627 }
f254e52c 628
39279cc3
CM
629 root_dentry = d_alloc_root(inode);
630 if (!root_dentry) {
631 iput(inode);
632 err = -ENOMEM;
633 goto fail_close;
f254e52c 634 }
58176a96 635
39279cc3 636 sb->s_root = root_dentry;
6885f308 637
6885f308 638 save_mount_options(sb, data);
90a887c9 639 cleancache_init_fs(sb);
2619ba1f 640 return 0;
39279cc3
CM
641
642fail_close:
643 close_ctree(tree_root);
644 return err;
2619ba1f
CM
645}
646
6bf13c0c 647int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
648{
649 struct btrfs_trans_handle *trans;
dccae999 650 struct btrfs_root *root = btrfs_sb(sb);
c5739bba 651 int ret;
2619ba1f 652
1abe9b8a 653 trace_btrfs_sync_fs(wait);
654
39279cc3
CM
655 if (!wait) {
656 filemap_flush(root->fs_info->btree_inode->i_mapping);
657 return 0;
658 }
771ed689 659
24bbcf04
YZ
660 btrfs_start_delalloc_inodes(root, 0);
661 btrfs_wait_ordered_extents(root, 0, 0);
771ed689 662
a22285a6 663 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
664 if (IS_ERR(trans))
665 return PTR_ERR(trans);
c5739bba 666 ret = btrfs_commit_transaction(trans, root);
54aa1f4d 667 return ret;
2c90e5d6
CM
668}
669
a9572a15
EP
670static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
671{
672 struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
673 struct btrfs_fs_info *info = root->fs_info;
200da64e 674 char *compress_type;
a9572a15
EP
675
676 if (btrfs_test_opt(root, DEGRADED))
677 seq_puts(seq, ",degraded");
678 if (btrfs_test_opt(root, NODATASUM))
679 seq_puts(seq, ",nodatasum");
680 if (btrfs_test_opt(root, NODATACOW))
681 seq_puts(seq, ",nodatacow");
682 if (btrfs_test_opt(root, NOBARRIER))
683 seq_puts(seq, ",nobarrier");
a9572a15 684 if (info->max_inline != 8192 * 1024)
21380931
JB
685 seq_printf(seq, ",max_inline=%llu",
686 (unsigned long long)info->max_inline);
a9572a15 687 if (info->alloc_start != 0)
21380931
JB
688 seq_printf(seq, ",alloc_start=%llu",
689 (unsigned long long)info->alloc_start);
a9572a15
EP
690 if (info->thread_pool_size != min_t(unsigned long,
691 num_online_cpus() + 2, 8))
692 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
200da64e
TI
693 if (btrfs_test_opt(root, COMPRESS)) {
694 if (info->compress_type == BTRFS_COMPRESS_ZLIB)
695 compress_type = "zlib";
696 else
697 compress_type = "lzo";
698 if (btrfs_test_opt(root, FORCE_COMPRESS))
699 seq_printf(seq, ",compress-force=%s", compress_type);
700 else
701 seq_printf(seq, ",compress=%s", compress_type);
702 }
c289811c
CM
703 if (btrfs_test_opt(root, NOSSD))
704 seq_puts(seq, ",nossd");
451d7585
CM
705 if (btrfs_test_opt(root, SSD_SPREAD))
706 seq_puts(seq, ",ssd_spread");
707 else if (btrfs_test_opt(root, SSD))
a9572a15 708 seq_puts(seq, ",ssd");
3a5e1404 709 if (btrfs_test_opt(root, NOTREELOG))
6b65c5c6 710 seq_puts(seq, ",notreelog");
dccae999 711 if (btrfs_test_opt(root, FLUSHONCOMMIT))
6b65c5c6 712 seq_puts(seq, ",flushoncommit");
20a5239a
MW
713 if (btrfs_test_opt(root, DISCARD))
714 seq_puts(seq, ",discard");
a9572a15
EP
715 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
716 seq_puts(seq, ",noacl");
200da64e
TI
717 if (btrfs_test_opt(root, SPACE_CACHE))
718 seq_puts(seq, ",space_cache");
73bc1876 719 else
8965593e 720 seq_puts(seq, ",nospace_cache");
200da64e
TI
721 if (btrfs_test_opt(root, CLEAR_CACHE))
722 seq_puts(seq, ",clear_cache");
723 if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
724 seq_puts(seq, ",user_subvol_rm_allowed");
0942caa3
DS
725 if (btrfs_test_opt(root, ENOSPC_DEBUG))
726 seq_puts(seq, ",enospc_debug");
727 if (btrfs_test_opt(root, AUTO_DEFRAG))
728 seq_puts(seq, ",autodefrag");
729 if (btrfs_test_opt(root, INODE_MAP_CACHE))
730 seq_puts(seq, ",inode_cache");
9555c6c1
ID
731 if (btrfs_test_opt(root, SKIP_BALANCE))
732 seq_puts(seq, ",skip_balance");
a9572a15
EP
733 return 0;
734}
735
a061fc8d 736static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4 737{
450ba0ea 738 struct btrfs_root *test_root = data;
a061fc8d 739 struct btrfs_root *root = btrfs_sb(s);
4b82d6e4 740
619c8c76
IK
741 /*
742 * If this super block is going away, return false as it
743 * can't match as an existing super block.
744 */
745 if (!atomic_read(&s->s_active))
746 return 0;
450ba0ea 747 return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
4b82d6e4
Y
748}
749
450ba0ea
JB
750static int btrfs_set_super(struct super_block *s, void *data)
751{
752 s->s_fs_info = data;
753
754 return set_anon_super(s, data);
4b82d6e4
Y
755}
756
f9d9ef62
DS
757/*
758 * subvolumes are identified by ino 256
759 */
760static inline int is_subvolume_inode(struct inode *inode)
761{
762 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
763 return 1;
764 return 0;
765}
766
830c4adb
JB
767/*
768 * This will strip out the subvol=%s argument for an argument string and add
769 * subvolid=0 to make sure we get the actual tree root for path walking to the
770 * subvol we want.
771 */
772static char *setup_root_args(char *args)
773{
774 unsigned copied = 0;
775 unsigned len = strlen(args) + 2;
776 char *pos;
777 char *ret;
778
779 /*
780 * We need the same args as before, but minus
781 *
782 * subvol=a
783 *
784 * and add
785 *
786 * subvolid=0
787 *
788 * which is a difference of 2 characters, so we allocate strlen(args) +
789 * 2 characters.
790 */
791 ret = kzalloc(len * sizeof(char), GFP_NOFS);
792 if (!ret)
793 return NULL;
794 pos = strstr(args, "subvol=");
795
796 /* This shouldn't happen, but just in case.. */
797 if (!pos) {
798 kfree(ret);
799 return NULL;
800 }
801
802 /*
803 * The subvol=<> arg is not at the front of the string, copy everybody
804 * up to that into ret.
805 */
806 if (pos != args) {
807 *pos = '\0';
808 strcpy(ret, args);
809 copied += strlen(args);
810 pos++;
811 }
812
813 strncpy(ret + copied, "subvolid=0", len - copied);
814
815 /* Length of subvolid=0 */
816 copied += 10;
817
818 /*
819 * If there is no , after the subvol= option then we know there's no
820 * other options and we can just return.
821 */
822 pos = strchr(pos, ',');
823 if (!pos)
824 return ret;
825
826 /* Copy the rest of the arguments into our buffer */
827 strncpy(ret + copied, pos, len - copied);
828 copied += strlen(pos);
829
830 return ret;
831}
832
833static struct dentry *mount_subvol(const char *subvol_name, int flags,
834 const char *device_name, char *data)
835{
836 struct super_block *s;
837 struct dentry *root;
838 struct vfsmount *mnt;
839 struct mnt_namespace *ns_private;
840 char *newargs;
841 struct path path;
842 int error;
843
844 newargs = setup_root_args(data);
845 if (!newargs)
846 return ERR_PTR(-ENOMEM);
847 mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
848 newargs);
849 kfree(newargs);
850 if (IS_ERR(mnt))
851 return ERR_CAST(mnt);
852
853 ns_private = create_mnt_ns(mnt);
854 if (IS_ERR(ns_private)) {
855 mntput(mnt);
856 return ERR_CAST(ns_private);
857 }
858
859 /*
860 * This will trigger the automount of the subvol so we can just
861 * drop the mnt we have here and return the dentry that we
862 * found.
863 */
864 error = vfs_path_lookup(mnt->mnt_root, mnt, subvol_name,
865 LOOKUP_FOLLOW, &path);
866 put_mnt_ns(ns_private);
867 if (error)
868 return ERR_PTR(error);
869
f9d9ef62
DS
870 if (!is_subvolume_inode(path.dentry->d_inode)) {
871 path_put(&path);
872 mntput(mnt);
873 error = -EINVAL;
874 printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
875 subvol_name);
876 return ERR_PTR(-EINVAL);
877 }
878
830c4adb
JB
879 /* Get a ref to the sb and the dentry we found and return it */
880 s = path.mnt->mnt_sb;
881 atomic_inc(&s->s_active);
882 root = dget(path.dentry);
883 path_put(&path);
884 down_write(&s->s_umount);
885
886 return root;
887}
450ba0ea 888
edf24abe
CH
889/*
890 * Find a superblock for the given device / mount point.
891 *
892 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
893 * for multiple device setup. Make sure to keep it in sync.
894 */
061dbc6b 895static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
306e16ce 896 const char *device_name, void *data)
4b82d6e4
Y
897{
898 struct block_device *bdev = NULL;
899 struct super_block *s;
900 struct dentry *root;
8a4b83cc 901 struct btrfs_fs_devices *fs_devices = NULL;
450ba0ea 902 struct btrfs_fs_info *fs_info = NULL;
97288f2c 903 fmode_t mode = FMODE_READ;
73f73415
JB
904 char *subvol_name = NULL;
905 u64 subvol_objectid = 0;
e15d0542 906 u64 subvol_rootid = 0;
4b82d6e4
Y
907 int error = 0;
908
97288f2c
CH
909 if (!(flags & MS_RDONLY))
910 mode |= FMODE_WRITE;
911
912 error = btrfs_parse_early_options(data, mode, fs_type,
73f73415 913 &subvol_name, &subvol_objectid,
e15d0542 914 &subvol_rootid, &fs_devices);
f23c8af8
ID
915 if (error) {
916 kfree(subvol_name);
061dbc6b 917 return ERR_PTR(error);
f23c8af8 918 }
edf24abe 919
830c4adb
JB
920 if (subvol_name) {
921 root = mount_subvol(subvol_name, flags, device_name, data);
922 kfree(subvol_name);
923 return root;
924 }
925
306e16ce 926 error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8a4b83cc 927 if (error)
830c4adb 928 return ERR_PTR(error);
4b82d6e4 929
450ba0ea
JB
930 /*
931 * Setup a dummy root and fs_info for test/set super. This is because
932 * we don't actually fill this stuff out until open_ctree, but we need
933 * it for searching for existing supers, so this lets us do that and
934 * then open_ctree will properly initialize everything later.
935 */
936 fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
04d21a24
ID
937 if (!fs_info)
938 return ERR_PTR(-ENOMEM);
939
940 fs_info->tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
941 if (!fs_info->tree_root) {
450ba0ea 942 error = -ENOMEM;
04d21a24 943 goto error_fs_info;
450ba0ea 944 }
04d21a24 945 fs_info->tree_root->fs_info = fs_info;
450ba0ea 946 fs_info->fs_devices = fs_devices;
450ba0ea 947
6c41761f
DS
948 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
949 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
950 if (!fs_info->super_copy || !fs_info->super_for_commit) {
951 error = -ENOMEM;
04d21a24
ID
952 goto error_fs_info;
953 }
954
955 error = btrfs_open_devices(fs_devices, mode, fs_type);
956 if (error)
957 goto error_fs_info;
958
959 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
960 error = -EACCES;
6c41761f
DS
961 goto error_close_devices;
962 }
963
dfe25020 964 bdev = fs_devices->latest_bdev;
04d21a24
ID
965 s = sget(fs_type, btrfs_test_super, btrfs_set_super,
966 fs_info->tree_root);
830c4adb
JB
967 if (IS_ERR(s)) {
968 error = PTR_ERR(s);
969 goto error_close_devices;
970 }
4b82d6e4
Y
971
972 if (s->s_root) {
973 if ((flags ^ s->s_flags) & MS_RDONLY) {
6f5bbff9 974 deactivate_locked_super(s);
04d21a24
ID
975 error = -EBUSY;
976 goto error_close_devices;
4b82d6e4
Y
977 }
978
2b82032c 979 btrfs_close_devices(fs_devices);
6c41761f 980 free_fs_info(fs_info);
4b82d6e4
Y
981 } else {
982 char b[BDEVNAME_SIZE];
983
9e1f1de0 984 s->s_flags = flags | MS_NOSEC;
4b82d6e4 985 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
5f524444 986 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
8a4b83cc
CM
987 error = btrfs_fill_super(s, fs_devices, data,
988 flags & MS_SILENT ? 1 : 0);
4b82d6e4 989 if (error) {
6f5bbff9 990 deactivate_locked_super(s);
830c4adb 991 return ERR_PTR(error);
4b82d6e4
Y
992 }
993
994 s->s_flags |= MS_ACTIVE;
995 }
996
830c4adb
JB
997 root = get_default_root(s, subvol_objectid);
998 if (IS_ERR(root)) {
999 deactivate_locked_super(s);
1000 return root;
4b82d6e4
Y
1001 }
1002
061dbc6b 1003 return root;
4b82d6e4 1004
c146afad 1005error_close_devices:
8a4b83cc 1006 btrfs_close_devices(fs_devices);
04d21a24 1007error_fs_info:
6c41761f 1008 free_fs_info(fs_info);
061dbc6b 1009 return ERR_PTR(error);
4b82d6e4 1010}
2e635a27 1011
c146afad
YZ
1012static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1013{
1014 struct btrfs_root *root = btrfs_sb(sb);
1015 int ret;
1016
b288052e
CM
1017 ret = btrfs_parse_options(root, data);
1018 if (ret)
1019 return -EINVAL;
1020
c146afad
YZ
1021 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1022 return 0;
1023
1024 if (*flags & MS_RDONLY) {
1025 sb->s_flags |= MS_RDONLY;
1026
1027 ret = btrfs_commit_super(root);
1028 WARN_ON(ret);
1029 } else {
2b82032c
YZ
1030 if (root->fs_info->fs_devices->rw_devices == 0)
1031 return -EACCES;
1032
6c41761f 1033 if (btrfs_super_log_root(root->fs_info->super_copy) != 0)
c146afad
YZ
1034 return -EINVAL;
1035
d68fc57b 1036 ret = btrfs_cleanup_fs_roots(root->fs_info);
c146afad
YZ
1037 WARN_ON(ret);
1038
d68fc57b
YZ
1039 /* recover relocation */
1040 ret = btrfs_recover_relocation(root);
c146afad
YZ
1041 WARN_ON(ret);
1042
1043 sb->s_flags &= ~MS_RDONLY;
1044 }
1045
1046 return 0;
1047}
1048
bcd53741
AJ
1049/* Used to sort the devices by max_avail(descending sort) */
1050static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1051 const void *dev_info2)
1052{
1053 if (((struct btrfs_device_info *)dev_info1)->max_avail >
1054 ((struct btrfs_device_info *)dev_info2)->max_avail)
1055 return -1;
1056 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1057 ((struct btrfs_device_info *)dev_info2)->max_avail)
1058 return 1;
1059 else
1060 return 0;
1061}
1062
1063/*
1064 * sort the devices by max_avail, in which max free extent size of each device
1065 * is stored.(Descending Sort)
1066 */
1067static inline void btrfs_descending_sort_devices(
1068 struct btrfs_device_info *devices,
1069 size_t nr_devices)
1070{
1071 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1072 btrfs_cmp_device_free_bytes, NULL);
1073}
1074
6d07bcec
MX
1075/*
1076 * The helper to calc the free space on the devices that can be used to store
1077 * file data.
1078 */
1079static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
1080{
1081 struct btrfs_fs_info *fs_info = root->fs_info;
1082 struct btrfs_device_info *devices_info;
1083 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1084 struct btrfs_device *device;
1085 u64 skip_space;
1086 u64 type;
1087 u64 avail_space;
1088 u64 used_space;
1089 u64 min_stripe_size;
39fb26c3 1090 int min_stripes = 1, num_stripes = 1;
6d07bcec
MX
1091 int i = 0, nr_devices;
1092 int ret;
1093
b772a86e 1094 nr_devices = fs_info->fs_devices->open_devices;
6d07bcec
MX
1095 BUG_ON(!nr_devices);
1096
1097 devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
1098 GFP_NOFS);
1099 if (!devices_info)
1100 return -ENOMEM;
1101
1102 /* calc min stripe number for data space alloction */
1103 type = btrfs_get_alloc_profile(root, 1);
39fb26c3 1104 if (type & BTRFS_BLOCK_GROUP_RAID0) {
6d07bcec 1105 min_stripes = 2;
39fb26c3
MX
1106 num_stripes = nr_devices;
1107 } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
6d07bcec 1108 min_stripes = 2;
39fb26c3
MX
1109 num_stripes = 2;
1110 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6d07bcec 1111 min_stripes = 4;
39fb26c3
MX
1112 num_stripes = 4;
1113 }
6d07bcec
MX
1114
1115 if (type & BTRFS_BLOCK_GROUP_DUP)
1116 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
1117 else
1118 min_stripe_size = BTRFS_STRIPE_LEN;
1119
b772a86e
LZ
1120 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1121 if (!device->in_fs_metadata || !device->bdev)
6d07bcec
MX
1122 continue;
1123
1124 avail_space = device->total_bytes - device->bytes_used;
1125
1126 /* align with stripe_len */
1127 do_div(avail_space, BTRFS_STRIPE_LEN);
1128 avail_space *= BTRFS_STRIPE_LEN;
1129
1130 /*
1131 * In order to avoid overwritting the superblock on the drive,
1132 * btrfs starts at an offset of at least 1MB when doing chunk
1133 * allocation.
1134 */
1135 skip_space = 1024 * 1024;
1136
1137 /* user can set the offset in fs_info->alloc_start. */
1138 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
1139 device->total_bytes)
1140 skip_space = max(fs_info->alloc_start, skip_space);
1141
1142 /*
1143 * btrfs can not use the free space in [0, skip_space - 1],
1144 * we must subtract it from the total. In order to implement
1145 * it, we account the used space in this range first.
1146 */
1147 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
1148 &used_space);
1149 if (ret) {
1150 kfree(devices_info);
1151 return ret;
1152 }
1153
1154 /* calc the free space in [0, skip_space - 1] */
1155 skip_space -= used_space;
1156
1157 /*
1158 * we can use the free space in [0, skip_space - 1], subtract
1159 * it from the total.
1160 */
1161 if (avail_space && avail_space >= skip_space)
1162 avail_space -= skip_space;
1163 else
1164 avail_space = 0;
1165
1166 if (avail_space < min_stripe_size)
1167 continue;
1168
1169 devices_info[i].dev = device;
1170 devices_info[i].max_avail = avail_space;
1171
1172 i++;
1173 }
1174
1175 nr_devices = i;
1176
1177 btrfs_descending_sort_devices(devices_info, nr_devices);
1178
1179 i = nr_devices - 1;
1180 avail_space = 0;
1181 while (nr_devices >= min_stripes) {
39fb26c3
MX
1182 if (num_stripes > nr_devices)
1183 num_stripes = nr_devices;
1184
6d07bcec
MX
1185 if (devices_info[i].max_avail >= min_stripe_size) {
1186 int j;
1187 u64 alloc_size;
1188
39fb26c3 1189 avail_space += devices_info[i].max_avail * num_stripes;
6d07bcec 1190 alloc_size = devices_info[i].max_avail;
39fb26c3 1191 for (j = i + 1 - num_stripes; j <= i; j++)
6d07bcec
MX
1192 devices_info[j].max_avail -= alloc_size;
1193 }
1194 i--;
1195 nr_devices--;
1196 }
1197
1198 kfree(devices_info);
1199 *free_bytes = avail_space;
1200 return 0;
1201}
1202
8fd17795
CM
1203static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1204{
1205 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
6c41761f 1206 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
bd4d1088
JB
1207 struct list_head *head = &root->fs_info->space_info;
1208 struct btrfs_space_info *found;
1209 u64 total_used = 0;
6d07bcec 1210 u64 total_free_data = 0;
db94535d 1211 int bits = dentry->d_sb->s_blocksize_bits;
9d03632e 1212 __be32 *fsid = (__be32 *)root->fs_info->fsid;
6d07bcec 1213 int ret;
8fd17795 1214
6d07bcec
MX
1215 /* holding chunk_muext to avoid allocating new chunks */
1216 mutex_lock(&root->fs_info->chunk_mutex);
bd4d1088 1217 rcu_read_lock();
89a55897 1218 list_for_each_entry_rcu(found, head, list) {
6d07bcec
MX
1219 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1220 total_free_data += found->disk_total - found->disk_used;
1221 total_free_data -=
1222 btrfs_account_ro_block_groups_free_space(found);
1223 }
1224
b742bb82 1225 total_used += found->disk_used;
89a55897 1226 }
bd4d1088
JB
1227 rcu_read_unlock();
1228
8fd17795 1229 buf->f_namelen = BTRFS_NAME_LEN;
db94535d 1230 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
bd4d1088 1231 buf->f_bfree = buf->f_blocks - (total_used >> bits);
8fd17795
CM
1232 buf->f_bsize = dentry->d_sb->s_blocksize;
1233 buf->f_type = BTRFS_SUPER_MAGIC;
6d07bcec
MX
1234 buf->f_bavail = total_free_data;
1235 ret = btrfs_calc_avail_data_space(root, &total_free_data);
1236 if (ret) {
1237 mutex_unlock(&root->fs_info->chunk_mutex);
1238 return ret;
1239 }
1240 buf->f_bavail += total_free_data;
1241 buf->f_bavail = buf->f_bavail >> bits;
1242 mutex_unlock(&root->fs_info->chunk_mutex);
d397712b 1243
9d03632e 1244 /* We treat it as constant endianness (it doesn't matter _which_)
d397712b 1245 because we want the fsid to come out the same whether mounted
9d03632e
DW
1246 on a big-endian or little-endian host */
1247 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
1248 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
32d48fa1
DW
1249 /* Mask in the root object ID too, to disambiguate subvols */
1250 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
1251 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
1252
8fd17795
CM
1253 return 0;
1254}
b5133862 1255
2e635a27
CM
1256static struct file_system_type btrfs_fs_type = {
1257 .owner = THIS_MODULE,
1258 .name = "btrfs",
061dbc6b 1259 .mount = btrfs_mount,
a061fc8d 1260 .kill_sb = kill_anon_super,
2e635a27
CM
1261 .fs_flags = FS_REQUIRES_DEV,
1262};
a9218f6b 1263
d352ac68
CM
1264/*
1265 * used by btrfsctl to scan devices when no FS is mounted
1266 */
8a4b83cc
CM
1267static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
1268 unsigned long arg)
1269{
1270 struct btrfs_ioctl_vol_args *vol;
1271 struct btrfs_fs_devices *fs_devices;
c071fcfd 1272 int ret = -ENOTTY;
8a4b83cc 1273
e441d54d
CM
1274 if (!capable(CAP_SYS_ADMIN))
1275 return -EPERM;
1276
dae7b665
LZ
1277 vol = memdup_user((void __user *)arg, sizeof(*vol));
1278 if (IS_ERR(vol))
1279 return PTR_ERR(vol);
c071fcfd 1280
8a4b83cc
CM
1281 switch (cmd) {
1282 case BTRFS_IOC_SCAN_DEV:
97288f2c 1283 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
8a4b83cc
CM
1284 &btrfs_fs_type, &fs_devices);
1285 break;
1286 }
dae7b665 1287
8a4b83cc 1288 kfree(vol);
f819d837 1289 return ret;
8a4b83cc
CM
1290}
1291
0176260f 1292static int btrfs_freeze(struct super_block *sb)
ed0dab6b
Y
1293{
1294 struct btrfs_root *root = btrfs_sb(sb);
a74a4b97
CM
1295 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1296 mutex_lock(&root->fs_info->cleaner_mutex);
0176260f 1297 return 0;
ed0dab6b
Y
1298}
1299
0176260f 1300static int btrfs_unfreeze(struct super_block *sb)
ed0dab6b
Y
1301{
1302 struct btrfs_root *root = btrfs_sb(sb);
a74a4b97
CM
1303 mutex_unlock(&root->fs_info->cleaner_mutex);
1304 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
0176260f 1305 return 0;
ed0dab6b 1306}
2e635a27 1307
22c44fe6
JB
1308static void btrfs_fs_dirty_inode(struct inode *inode, int flags)
1309{
1310 int ret;
1311
1312 ret = btrfs_dirty_inode(inode);
1313 if (ret)
1314 printk_ratelimited(KERN_ERR "btrfs: fail to dirty inode %Lu "
1315 "error %d\n", btrfs_ino(inode), ret);
1316}
1317
b87221de 1318static const struct super_operations btrfs_super_ops = {
76dda93c 1319 .drop_inode = btrfs_drop_inode,
bd555975 1320 .evict_inode = btrfs_evict_inode,
e20d96d6 1321 .put_super = btrfs_put_super,
d5719762 1322 .sync_fs = btrfs_sync_fs,
a9572a15 1323 .show_options = btrfs_show_options,
4730a4bc 1324 .write_inode = btrfs_write_inode,
22c44fe6 1325 .dirty_inode = btrfs_fs_dirty_inode,
2c90e5d6
CM
1326 .alloc_inode = btrfs_alloc_inode,
1327 .destroy_inode = btrfs_destroy_inode,
8fd17795 1328 .statfs = btrfs_statfs,
c146afad 1329 .remount_fs = btrfs_remount,
0176260f
LT
1330 .freeze_fs = btrfs_freeze,
1331 .unfreeze_fs = btrfs_unfreeze,
e20d96d6 1332};
a9218f6b
CM
1333
1334static const struct file_operations btrfs_ctl_fops = {
1335 .unlocked_ioctl = btrfs_control_ioctl,
1336 .compat_ioctl = btrfs_control_ioctl,
1337 .owner = THIS_MODULE,
6038f373 1338 .llseek = noop_llseek,
a9218f6b
CM
1339};
1340
1341static struct miscdevice btrfs_misc = {
578454ff 1342 .minor = BTRFS_MINOR,
a9218f6b
CM
1343 .name = "btrfs-control",
1344 .fops = &btrfs_ctl_fops
1345};
1346
578454ff
KS
1347MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1348MODULE_ALIAS("devname:btrfs-control");
1349
a9218f6b
CM
1350static int btrfs_interface_init(void)
1351{
1352 return misc_register(&btrfs_misc);
1353}
1354
b2950863 1355static void btrfs_interface_exit(void)
a9218f6b
CM
1356{
1357 if (misc_deregister(&btrfs_misc) < 0)
d397712b 1358 printk(KERN_INFO "misc_deregister failed for control device");
a9218f6b
CM
1359}
1360
2e635a27
CM
1361static int __init init_btrfs_fs(void)
1362{
2c90e5d6 1363 int err;
58176a96
JB
1364
1365 err = btrfs_init_sysfs();
1366 if (err)
1367 return err;
1368
261507a0 1369 err = btrfs_init_compress();
2c90e5d6 1370 if (err)
a74a4b97 1371 goto free_sysfs;
d1310b2e 1372
261507a0
LZ
1373 err = btrfs_init_cachep();
1374 if (err)
1375 goto free_compress;
1376
d1310b2e 1377 err = extent_io_init();
2f4cbe64
WB
1378 if (err)
1379 goto free_cachep;
1380
d1310b2e
CM
1381 err = extent_map_init();
1382 if (err)
1383 goto free_extent_io;
1384
16cdcec7 1385 err = btrfs_delayed_inode_init();
2f4cbe64
WB
1386 if (err)
1387 goto free_extent_map;
c8b97818 1388
16cdcec7
MX
1389 err = btrfs_interface_init();
1390 if (err)
1391 goto free_delayed_inode;
1392
a9218f6b
CM
1393 err = register_filesystem(&btrfs_fs_type);
1394 if (err)
1395 goto unregister_ioctl;
b3c3da71
CM
1396
1397 printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
2f4cbe64
WB
1398 return 0;
1399
a9218f6b
CM
1400unregister_ioctl:
1401 btrfs_interface_exit();
16cdcec7
MX
1402free_delayed_inode:
1403 btrfs_delayed_inode_exit();
2f4cbe64
WB
1404free_extent_map:
1405 extent_map_exit();
d1310b2e
CM
1406free_extent_io:
1407 extent_io_exit();
2f4cbe64
WB
1408free_cachep:
1409 btrfs_destroy_cachep();
261507a0
LZ
1410free_compress:
1411 btrfs_exit_compress();
a74a4b97 1412free_sysfs:
2f4cbe64
WB
1413 btrfs_exit_sysfs();
1414 return err;
2e635a27
CM
1415}
1416
1417static void __exit exit_btrfs_fs(void)
1418{
39279cc3 1419 btrfs_destroy_cachep();
16cdcec7 1420 btrfs_delayed_inode_exit();
a52d9a80 1421 extent_map_exit();
d1310b2e 1422 extent_io_exit();
a9218f6b 1423 btrfs_interface_exit();
2e635a27 1424 unregister_filesystem(&btrfs_fs_type);
58176a96 1425 btrfs_exit_sysfs();
8a4b83cc 1426 btrfs_cleanup_fs_uuids();
261507a0 1427 btrfs_exit_compress();
2e635a27
CM
1428}
1429
1430module_init(init_btrfs_fs)
1431module_exit(exit_btrfs_fs)
1432
1433MODULE_LICENSE("GPL");
This page took 0.292976 seconds and 5 git commands to generate.