btrfs: fix strange indentation in lookup_extent_mapping
[linux-2.6-block.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>
27#include <linux/string.h>
28#include <linux/smp_lock.h>
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>
2e635a27 40#include "ctree.h"
e20d96d6 41#include "disk-io.h"
d5719762 42#include "transaction.h"
2c90e5d6 43#include "btrfs_inode.h"
c5739bba 44#include "ioctl.h"
3a686375 45#include "print-tree.h"
5103e947 46#include "xattr.h"
8a4b83cc 47#include "volumes.h"
2e635a27 48
5f39d397 49#define BTRFS_SUPER_MAGIC 0x9123683E
f254e52c 50
39279cc3 51static struct super_operations btrfs_super_ops;
75dfe396 52
39279cc3 53static void btrfs_put_super (struct super_block * sb)
b18c6685 54{
39279cc3 55 struct btrfs_root *root = btrfs_sb(sb);
58176a96 56 struct btrfs_fs_info *fs = root->fs_info;
b18c6685 57 int ret;
b18c6685 58
39279cc3
CM
59 ret = close_ctree(root);
60 if (ret) {
61 printk("close ctree returns %d\n", ret);
75dfe396 62 }
58176a96 63 btrfs_sysfs_del_super(fs);
39279cc3 64 sb->s_fs_info = NULL;
75dfe396
CM
65}
66
95e05289 67enum {
dfe25020
CM
68 Opt_degraded, Opt_subvol, Opt_nodatasum, Opt_nodatacow,
69 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
70 Opt_ssd, Opt_err,
95e05289
CM
71};
72
73static match_table_t tokens = {
dfe25020 74 {Opt_degraded, "degraded"},
95e05289 75 {Opt_subvol, "subvol=%s"},
b6cda9bc 76 {Opt_nodatasum, "nodatasum"},
be20aa9d 77 {Opt_nodatacow, "nodatacow"},
21ad10cf 78 {Opt_nobarrier, "nobarrier"},
c59f8951 79 {Opt_max_extent, "max_extent=%s"},
6f568d35 80 {Opt_max_inline, "max_inline=%s"},
8f662a76 81 {Opt_alloc_start, "alloc_start=%s"},
e18e4809 82 {Opt_ssd, "ssd"},
95e05289
CM
83 {Opt_err, NULL}
84};
85
edbd8d4e 86u64 btrfs_parse_size(char *str)
c59f8951 87{
edbd8d4e 88 u64 res;
c59f8951
CM
89 int mult = 1;
90 char *end;
91 char last;
92
93 res = simple_strtoul(str, &end, 10);
94
95 last = end[0];
96 if (isalpha(last)) {
97 last = tolower(last);
98 switch (last) {
99 case 'g':
100 mult *= 1024;
101 case 'm':
102 mult *= 1024;
103 case 'k':
104 mult *= 1024;
105 }
106 res = res * mult;
107 }
108 return res;
109}
110
dfe25020
CM
111int btrfs_parse_options(char *options, struct btrfs_root *root,
112 char **subvol_name)
95e05289
CM
113{
114 char * p;
b6cda9bc 115 struct btrfs_fs_info *info = NULL;
95e05289 116 substring_t args[MAX_OPT_ARGS];
b6cda9bc 117
95e05289
CM
118 if (!options)
119 return 1;
120
be20aa9d
CM
121 /*
122 * strsep changes the string, duplicate it because parse_options
123 * gets called twice
124 */
125 options = kstrdup(options, GFP_NOFS);
126 if (!options)
127 return -ENOMEM;
128
129 if (root)
130 info = root->fs_info;
131
95e05289
CM
132 while ((p = strsep (&options, ",")) != NULL) {
133 int token;
134 if (!*p)
135 continue;
136
137 token = match_token(p, tokens, args);
138 switch (token) {
dfe25020
CM
139 case Opt_degraded:
140 if (info) {
141 printk("btrfs: allowing degraded mounts\n");
142 btrfs_set_opt(info->mount_opt, DEGRADED);
143 }
144 break;
95e05289 145 case Opt_subvol:
be20aa9d 146 if (subvol_name) {
b6cda9bc 147 *subvol_name = match_strdup(&args[0]);
be20aa9d 148 }
b6cda9bc
CM
149 break;
150 case Opt_nodatasum:
be20aa9d
CM
151 if (info) {
152 printk("btrfs: setting nodatacsum\n");
b6cda9bc 153 btrfs_set_opt(info->mount_opt, NODATASUM);
be20aa9d
CM
154 }
155 break;
156 case Opt_nodatacow:
157 if (info) {
158 printk("btrfs: setting nodatacow\n");
159 btrfs_set_opt(info->mount_opt, NODATACOW);
160 btrfs_set_opt(info->mount_opt, NODATASUM);
161 }
95e05289 162 break;
e18e4809
CM
163 case Opt_ssd:
164 if (info) {
165 printk("btrfs: use ssd allocation scheme\n");
166 btrfs_set_opt(info->mount_opt, SSD);
167 }
168 break;
21ad10cf
CM
169 case Opt_nobarrier:
170 if (info) {
171 printk("btrfs: turning off barriers\n");
172 btrfs_set_opt(info->mount_opt, NOBARRIER);
173 }
174 break;
c59f8951
CM
175 case Opt_max_extent:
176 if (info) {
177 char *num = match_strdup(&args[0]);
178 if (num) {
edbd8d4e
CM
179 info->max_extent =
180 btrfs_parse_size(num);
c59f8951
CM
181 kfree(num);
182
183 info->max_extent = max_t(u64,
184 info->max_extent,
185 root->sectorsize);
186 printk("btrfs: max_extent at %Lu\n",
187 info->max_extent);
188 }
189 }
190 break;
6f568d35
CM
191 case Opt_max_inline:
192 if (info) {
193 char *num = match_strdup(&args[0]);
194 if (num) {
195 info->max_inline =
196 btrfs_parse_size(num);
197 kfree(num);
198
199 info->max_inline = max_t(u64,
200 info->max_inline,
201 root->sectorsize);
202 printk("btrfs: max_inline at %Lu\n",
203 info->max_inline);
204 }
205 }
206 break;
8f662a76
CM
207 case Opt_alloc_start:
208 if (info) {
209 char *num = match_strdup(&args[0]);
210 if (num) {
211 info->alloc_start =
212 btrfs_parse_size(num);
213 kfree(num);
214 printk("btrfs: allocations start at "
215 "%Lu\n", info->alloc_start);
216 }
217 }
218 break;
95e05289 219 default:
be20aa9d 220 break;
95e05289
CM
221 }
222 }
be20aa9d 223 kfree(options);
95e05289
CM
224 return 1;
225}
226
8a4b83cc
CM
227static int btrfs_fill_super(struct super_block * sb,
228 struct btrfs_fs_devices *fs_devices,
229 void * data, int silent)
75dfe396 230{
39279cc3
CM
231 struct inode * inode;
232 struct dentry * root_dentry;
233 struct btrfs_super_block *disk_super;
234 struct btrfs_root *tree_root;
235 struct btrfs_inode *bi;
236 int err;
a429e513 237
39279cc3
CM
238 sb->s_maxbytes = MAX_LFS_FILESIZE;
239 sb->s_magic = BTRFS_SUPER_MAGIC;
240 sb->s_op = &btrfs_super_ops;
5103e947 241 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 242 sb->s_time_gran = 1;
a429e513 243
dfe25020 244 tree_root = open_ctree(sb, fs_devices, (char *)data);
6567e837 245
e58ca020 246 if (IS_ERR(tree_root)) {
39279cc3 247 printk("btrfs: open_ctree failed\n");
e58ca020 248 return PTR_ERR(tree_root);
a429e513 249 }
39279cc3 250 sb->s_fs_info = tree_root;
5f39d397 251 disk_super = &tree_root->fs_info->super_copy;
39279cc3
CM
252 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
253 tree_root);
254 bi = BTRFS_I(inode);
255 bi->location.objectid = inode->i_ino;
256 bi->location.offset = 0;
39279cc3 257 bi->root = tree_root;
b888db2b 258
39279cc3 259 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
a429e513 260
39279cc3 261 if (!inode) {
6567e837 262 err = -ENOMEM;
39279cc3 263 goto fail_close;
f254e52c 264 }
39279cc3
CM
265 if (inode->i_state & I_NEW) {
266 btrfs_read_locked_inode(inode);
267 unlock_new_inode(inode);
f254e52c 268 }
f254e52c 269
39279cc3
CM
270 root_dentry = d_alloc_root(inode);
271 if (!root_dentry) {
272 iput(inode);
273 err = -ENOMEM;
274 goto fail_close;
f254e52c 275 }
58176a96
JB
276
277 /* this does the super kobj at the same time */
278 err = btrfs_sysfs_add_super(tree_root->fs_info);
279 if (err)
280 goto fail_close;
281
39279cc3
CM
282 sb->s_root = root_dentry;
283 btrfs_transaction_queue_work(tree_root, HZ * 30);
6885f308
CM
284
285#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
286 save_mount_options(sb, data);
287#endif
288
2619ba1f 289 return 0;
39279cc3
CM
290
291fail_close:
292 close_ctree(tree_root);
293 return err;
2619ba1f
CM
294}
295
6bf13c0c 296int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
297{
298 struct btrfs_trans_handle *trans;
39279cc3 299 struct btrfs_root *root;
c5739bba 300 int ret;
39279cc3 301 root = btrfs_sb(sb);
2619ba1f 302
39279cc3
CM
303 sb->s_dirt = 0;
304 if (!wait) {
305 filemap_flush(root->fs_info->btree_inode->i_mapping);
306 return 0;
307 }
e9d0b13b 308 btrfs_clean_old_snapshots(root);
c5739bba 309 mutex_lock(&root->fs_info->fs_mutex);
e9d0b13b 310 btrfs_defrag_dirty_roots(root->fs_info);
c5739bba 311 trans = btrfs_start_transaction(root, 1);
c5739bba 312 ret = btrfs_commit_transaction(trans, root);
39279cc3 313 sb->s_dirt = 0;
c5739bba 314 mutex_unlock(&root->fs_info->fs_mutex);
54aa1f4d 315 return ret;
2c90e5d6
CM
316}
317
39279cc3 318static void btrfs_write_super(struct super_block *sb)
2c90e5d6 319{
39279cc3 320 sb->s_dirt = 0;
2c90e5d6
CM
321}
322
a061fc8d 323static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4 324{
a061fc8d
CM
325 struct btrfs_fs_devices *test_fs_devices = data;
326 struct btrfs_root *root = btrfs_sb(s);
4b82d6e4 327
a061fc8d 328 return root->fs_info->fs_devices == test_fs_devices;
4b82d6e4
Y
329}
330
331int btrfs_get_sb_bdev(struct file_system_type *fs_type,
332 int flags, const char *dev_name, void *data,
4b82d6e4
Y
333 struct vfsmount *mnt, const char *subvol)
334{
335 struct block_device *bdev = NULL;
336 struct super_block *s;
337 struct dentry *root;
8a4b83cc 338 struct btrfs_fs_devices *fs_devices = NULL;
4b82d6e4
Y
339 int error = 0;
340
8a4b83cc
CM
341 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
342 if (error)
343 return error;
4b82d6e4 344
8a4b83cc
CM
345 error = btrfs_open_devices(fs_devices, flags, fs_type);
346 if (error)
347 return error;
348
dfe25020 349 bdev = fs_devices->latest_bdev;
a061fc8d
CM
350 btrfs_lock_volumes();
351 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
352 btrfs_unlock_volumes();
4b82d6e4
Y
353 if (IS_ERR(s))
354 goto error_s;
355
356 if (s->s_root) {
357 if ((flags ^ s->s_flags) & MS_RDONLY) {
358 up_write(&s->s_umount);
359 deactivate_super(s);
360 error = -EBUSY;
361 goto error_bdev;
362 }
363
4b82d6e4
Y
364 } else {
365 char b[BDEVNAME_SIZE];
366
367 s->s_flags = flags;
368 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
8a4b83cc
CM
369 error = btrfs_fill_super(s, fs_devices, data,
370 flags & MS_SILENT ? 1 : 0);
4b82d6e4
Y
371 if (error) {
372 up_write(&s->s_umount);
373 deactivate_super(s);
374 goto error;
375 }
376
788f20eb 377 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
4b82d6e4
Y
378 s->s_flags |= MS_ACTIVE;
379 }
380
381 if (subvol) {
382 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
383 if (IS_ERR(root)) {
384 up_write(&s->s_umount);
385 deactivate_super(s);
386 error = PTR_ERR(root);
387 goto error;
388 }
389 if (!root->d_inode) {
390 dput(root);
391 up_write(&s->s_umount);
392 deactivate_super(s);
393 error = -ENXIO;
394 goto error;
395 }
396 } else {
397 root = dget(s->s_root);
398 }
399
400 mnt->mnt_sb = s;
401 mnt->mnt_root = root;
402 return 0;
403
404error_s:
405 error = PTR_ERR(s);
406error_bdev:
8a4b83cc 407 btrfs_close_devices(fs_devices);
4b82d6e4
Y
408error:
409 return error;
410}
411/* end copy & paste */
412
2e635a27 413static int btrfs_get_sb(struct file_system_type *fs_type,
95e05289 414 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2e635a27 415{
4b82d6e4 416 int ret;
95e05289 417 char *subvol_name = NULL;
4b82d6e4 418
dfe25020 419 btrfs_parse_options((char *)data, NULL, &subvol_name);
8a4b83cc 420 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
4b82d6e4 421 subvol_name ? subvol_name : "default");
c59f8951
CM
422 if (subvol_name)
423 kfree(subvol_name);
4b82d6e4 424 return ret;
2e635a27
CM
425}
426
8fd17795
CM
427static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
428{
429 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
4b52dff6 430 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
db94535d 431 int bits = dentry->d_sb->s_blocksize_bits;
8fd17795
CM
432
433 buf->f_namelen = BTRFS_NAME_LEN;
db94535d
CM
434 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
435 buf->f_bfree = buf->f_blocks -
436 (btrfs_super_bytes_used(disk_super) >> bits);
8fd17795
CM
437 buf->f_bavail = buf->f_bfree;
438 buf->f_bsize = dentry->d_sb->s_blocksize;
439 buf->f_type = BTRFS_SUPER_MAGIC;
440 return 0;
441}
b5133862 442
2e635a27
CM
443static struct file_system_type btrfs_fs_type = {
444 .owner = THIS_MODULE,
445 .name = "btrfs",
446 .get_sb = btrfs_get_sb,
a061fc8d 447 .kill_sb = kill_anon_super,
2e635a27
CM
448 .fs_flags = FS_REQUIRES_DEV,
449};
a9218f6b 450
8a4b83cc
CM
451static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
452 unsigned long arg)
453{
454 struct btrfs_ioctl_vol_args *vol;
455 struct btrfs_fs_devices *fs_devices;
f819d837 456 int ret = 0;
8a4b83cc
CM
457 int len;
458
459 vol = kmalloc(sizeof(*vol), GFP_KERNEL);
460 if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
461 ret = -EFAULT;
462 goto out;
463 }
464 len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
465 switch (cmd) {
466 case BTRFS_IOC_SCAN_DEV:
467 ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
468 &btrfs_fs_type, &fs_devices);
469 break;
470 }
471out:
472 kfree(vol);
f819d837 473 return ret;
8a4b83cc
CM
474}
475
ed0dab6b
Y
476static void btrfs_write_super_lockfs(struct super_block *sb)
477{
478 struct btrfs_root *root = btrfs_sb(sb);
479 btrfs_transaction_flush_work(root);
480}
481
482static void btrfs_unlockfs(struct super_block *sb)
483{
484 struct btrfs_root *root = btrfs_sb(sb);
485 btrfs_transaction_queue_work(root, HZ * 30);
486}
2e635a27 487
e20d96d6 488static struct super_operations btrfs_super_ops = {
134e9731 489 .delete_inode = btrfs_delete_inode,
e20d96d6 490 .put_super = btrfs_put_super,
d5719762
CM
491 .write_super = btrfs_write_super,
492 .sync_fs = btrfs_sync_fs,
6885f308
CM
493#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
494 .read_inode = btrfs_read_locked_inode,
495#else
496 .show_options = generic_show_options,
497#endif
4730a4bc 498 .write_inode = btrfs_write_inode,
b5133862 499 .dirty_inode = btrfs_dirty_inode,
2c90e5d6
CM
500 .alloc_inode = btrfs_alloc_inode,
501 .destroy_inode = btrfs_destroy_inode,
8fd17795 502 .statfs = btrfs_statfs,
ed0dab6b
Y
503 .write_super_lockfs = btrfs_write_super_lockfs,
504 .unlockfs = btrfs_unlockfs,
e20d96d6 505};
a9218f6b
CM
506
507static const struct file_operations btrfs_ctl_fops = {
508 .unlocked_ioctl = btrfs_control_ioctl,
509 .compat_ioctl = btrfs_control_ioctl,
510 .owner = THIS_MODULE,
511};
512
513static struct miscdevice btrfs_misc = {
514 .minor = MISC_DYNAMIC_MINOR,
515 .name = "btrfs-control",
516 .fops = &btrfs_ctl_fops
517};
518
519static int btrfs_interface_init(void)
520{
521 return misc_register(&btrfs_misc);
522}
523
524void btrfs_interface_exit(void)
525{
526 if (misc_deregister(&btrfs_misc) < 0)
527 printk("misc_deregister failed for control device");
528}
529
2e635a27
CM
530static int __init init_btrfs_fs(void)
531{
2c90e5d6 532 int err;
58176a96
JB
533
534 err = btrfs_init_sysfs();
535 if (err)
536 return err;
537
08607c1b 538 btrfs_init_transaction_sys();
39279cc3 539 err = btrfs_init_cachep();
2c90e5d6 540 if (err)
2f4cbe64 541 goto free_transaction_sys;
d1310b2e
CM
542
543 err = extent_io_init();
2f4cbe64
WB
544 if (err)
545 goto free_cachep;
546
d1310b2e
CM
547 err = extent_map_init();
548 if (err)
549 goto free_extent_io;
550
a9218f6b 551 err = btrfs_interface_init();
2f4cbe64
WB
552 if (err)
553 goto free_extent_map;
a9218f6b
CM
554 err = register_filesystem(&btrfs_fs_type);
555 if (err)
556 goto unregister_ioctl;
2f4cbe64
WB
557 return 0;
558
a9218f6b
CM
559unregister_ioctl:
560 btrfs_interface_exit();
2f4cbe64
WB
561free_extent_map:
562 extent_map_exit();
d1310b2e
CM
563free_extent_io:
564 extent_io_exit();
2f4cbe64
WB
565free_cachep:
566 btrfs_destroy_cachep();
567free_transaction_sys:
568 btrfs_exit_transaction_sys();
569 btrfs_exit_sysfs();
570 return err;
2e635a27
CM
571}
572
573static void __exit exit_btrfs_fs(void)
574{
08607c1b 575 btrfs_exit_transaction_sys();
39279cc3 576 btrfs_destroy_cachep();
a52d9a80 577 extent_map_exit();
d1310b2e 578 extent_io_exit();
a9218f6b 579 btrfs_interface_exit();
2e635a27 580 unregister_filesystem(&btrfs_fs_type);
58176a96 581 btrfs_exit_sysfs();
8a4b83cc 582 btrfs_cleanup_fs_uuids();
2e635a27
CM
583}
584
585module_init(init_btrfs_fs)
586module_exit(exit_btrfs_fs)
587
588MODULE_LICENSE("GPL");