05029656e42e9c871238175ea692ade0cc031b66
[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/string.h>
28 #include <linux/smp_lock.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 "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "btrfs_inode.h"
44 #include "ioctl.h"
45 #include "print-tree.h"
46 #include "xattr.h"
47 #include "volumes.h"
48
49 #define BTRFS_SUPER_MAGIC 0x9123683E
50
51 static struct super_operations btrfs_super_ops;
52
53 static void btrfs_put_super (struct super_block * sb)
54 {
55 struct btrfs_root *root = btrfs_sb(sb);
56 struct btrfs_fs_info *fs = root->fs_info;
57 int ret;
58
59 ret = close_ctree(root);
60 if (ret) {
61 printk("close ctree returns %d\n", ret);
62 }
63 btrfs_sysfs_del_super(fs);
64 sb->s_fs_info = NULL;
65 }
66
67 enum {
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,
71 };
72
73 static match_table_t tokens = {
74 {Opt_degraded, "degraded"},
75 {Opt_subvol, "subvol=%s"},
76 {Opt_nodatasum, "nodatasum"},
77 {Opt_nodatacow, "nodatacow"},
78 {Opt_nobarrier, "nobarrier"},
79 {Opt_max_extent, "max_extent=%s"},
80 {Opt_max_inline, "max_inline=%s"},
81 {Opt_alloc_start, "alloc_start=%s"},
82 {Opt_ssd, "ssd"},
83 {Opt_err, NULL}
84 };
85
86 u64 btrfs_parse_size(char *str)
87 {
88 u64 res;
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
111 int btrfs_parse_options(char *options, struct btrfs_root *root,
112 char **subvol_name)
113 {
114 char * p;
115 struct btrfs_fs_info *info = NULL;
116 substring_t args[MAX_OPT_ARGS];
117
118 if (!options)
119 return 1;
120
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
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) {
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;
145 case Opt_subvol:
146 if (subvol_name) {
147 *subvol_name = match_strdup(&args[0]);
148 }
149 break;
150 case Opt_nodatasum:
151 if (info) {
152 printk("btrfs: setting nodatacsum\n");
153 btrfs_set_opt(info->mount_opt, NODATASUM);
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 }
162 break;
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;
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;
175 case Opt_max_extent:
176 if (info) {
177 char *num = match_strdup(&args[0]);
178 if (num) {
179 info->max_extent =
180 btrfs_parse_size(num);
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;
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;
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;
219 default:
220 break;
221 }
222 }
223 kfree(options);
224 return 1;
225 }
226
227 static int btrfs_fill_super(struct super_block * sb,
228 struct btrfs_fs_devices *fs_devices,
229 void * data, int silent)
230 {
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;
237
238 sb->s_maxbytes = MAX_LFS_FILESIZE;
239 sb->s_magic = BTRFS_SUPER_MAGIC;
240 sb->s_op = &btrfs_super_ops;
241 sb->s_xattr = btrfs_xattr_handlers;
242 sb->s_time_gran = 1;
243
244 tree_root = open_ctree(sb, fs_devices, (char *)data);
245
246 if (IS_ERR(tree_root)) {
247 printk("btrfs: open_ctree failed\n");
248 return PTR_ERR(tree_root);
249 }
250 sb->s_fs_info = tree_root;
251 disk_super = &tree_root->fs_info->super_copy;
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;
257 bi->root = tree_root;
258
259 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
260
261 if (!inode) {
262 err = -ENOMEM;
263 goto fail_close;
264 }
265 if (inode->i_state & I_NEW) {
266 btrfs_read_locked_inode(inode);
267 unlock_new_inode(inode);
268 }
269
270 root_dentry = d_alloc_root(inode);
271 if (!root_dentry) {
272 iput(inode);
273 err = -ENOMEM;
274 goto fail_close;
275 }
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
282 sb->s_root = root_dentry;
283 btrfs_transaction_queue_work(tree_root, HZ * 30);
284
285 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
286 save_mount_options(sb, data);
287 #endif
288
289 return 0;
290
291 fail_close:
292 close_ctree(tree_root);
293 return err;
294 }
295
296 static int btrfs_sync_fs(struct super_block *sb, int wait)
297 {
298 struct btrfs_trans_handle *trans;
299 struct btrfs_root *root;
300 int ret;
301 root = btrfs_sb(sb);
302
303 sb->s_dirt = 0;
304 if (!wait) {
305 filemap_flush(root->fs_info->btree_inode->i_mapping);
306 return 0;
307 }
308 btrfs_clean_old_snapshots(root);
309 mutex_lock(&root->fs_info->fs_mutex);
310 btrfs_defrag_dirty_roots(root->fs_info);
311 trans = btrfs_start_transaction(root, 1);
312 ret = btrfs_commit_transaction(trans, root);
313 sb->s_dirt = 0;
314 mutex_unlock(&root->fs_info->fs_mutex);
315 return ret;
316 }
317
318 static void btrfs_write_super(struct super_block *sb)
319 {
320 sb->s_dirt = 0;
321 }
322
323 static int btrfs_test_super(struct super_block *s, void *data)
324 {
325 struct btrfs_fs_devices *test_fs_devices = data;
326 struct btrfs_root *root = btrfs_sb(s);
327
328 return root->fs_info->fs_devices == test_fs_devices;
329 }
330
331 int btrfs_get_sb_bdev(struct file_system_type *fs_type,
332 int flags, const char *dev_name, void *data,
333 struct vfsmount *mnt, const char *subvol)
334 {
335 struct block_device *bdev = NULL;
336 struct super_block *s;
337 struct dentry *root;
338 struct btrfs_fs_devices *fs_devices = NULL;
339 int error = 0;
340
341 error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
342 if (error)
343 return error;
344
345 error = btrfs_open_devices(fs_devices, flags, fs_type);
346 if (error)
347 return error;
348
349 bdev = fs_devices->latest_bdev;
350 btrfs_lock_volumes();
351 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
352 btrfs_unlock_volumes();
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
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));
369 error = btrfs_fill_super(s, fs_devices, data,
370 flags & MS_SILENT ? 1 : 0);
371 if (error) {
372 up_write(&s->s_umount);
373 deactivate_super(s);
374 goto error;
375 }
376
377 btrfs_sb(s)->fs_info->bdev_holder = fs_type;
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
404 error_s:
405 error = PTR_ERR(s);
406 error_bdev:
407 btrfs_close_devices(fs_devices);
408 error:
409 return error;
410 }
411 /* end copy & paste */
412
413 static int btrfs_get_sb(struct file_system_type *fs_type,
414 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
415 {
416 int ret;
417 char *subvol_name = NULL;
418
419 btrfs_parse_options((char *)data, NULL, &subvol_name);
420 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
421 subvol_name ? subvol_name : "default");
422 if (subvol_name)
423 kfree(subvol_name);
424 return ret;
425 }
426
427 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
428 {
429 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
430 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
431 int bits = dentry->d_sb->s_blocksize_bits;
432
433 buf->f_namelen = BTRFS_NAME_LEN;
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);
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 }
442
443 static struct file_system_type btrfs_fs_type = {
444 .owner = THIS_MODULE,
445 .name = "btrfs",
446 .get_sb = btrfs_get_sb,
447 .kill_sb = kill_anon_super,
448 .fs_flags = FS_REQUIRES_DEV,
449 };
450
451 static 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;
456 int ret;
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 }
471 out:
472 kfree(vol);
473 return 0;
474 }
475
476 static 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
482 static 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 }
487
488 static struct super_operations btrfs_super_ops = {
489 .delete_inode = btrfs_delete_inode,
490 .put_super = btrfs_put_super,
491 .write_super = btrfs_write_super,
492 .sync_fs = btrfs_sync_fs,
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
498 .write_inode = btrfs_write_inode,
499 .dirty_inode = btrfs_dirty_inode,
500 .alloc_inode = btrfs_alloc_inode,
501 .destroy_inode = btrfs_destroy_inode,
502 .statfs = btrfs_statfs,
503 .write_super_lockfs = btrfs_write_super_lockfs,
504 .unlockfs = btrfs_unlockfs,
505 };
506
507 static 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
513 static struct miscdevice btrfs_misc = {
514 .minor = MISC_DYNAMIC_MINOR,
515 .name = "btrfs-control",
516 .fops = &btrfs_ctl_fops
517 };
518
519 static int btrfs_interface_init(void)
520 {
521 return misc_register(&btrfs_misc);
522 }
523
524 void btrfs_interface_exit(void)
525 {
526 if (misc_deregister(&btrfs_misc) < 0)
527 printk("misc_deregister failed for control device");
528 }
529
530 static int __init init_btrfs_fs(void)
531 {
532 int err;
533
534 err = btrfs_init_sysfs();
535 if (err)
536 return err;
537
538 btrfs_init_transaction_sys();
539 err = btrfs_init_cachep();
540 if (err)
541 goto free_transaction_sys;
542
543 err = extent_io_init();
544 if (err)
545 goto free_cachep;
546
547 err = extent_map_init();
548 if (err)
549 goto free_extent_io;
550
551 err = btrfs_interface_init();
552 if (err)
553 goto free_extent_map;
554 err = register_filesystem(&btrfs_fs_type);
555 if (err)
556 goto unregister_ioctl;
557 return 0;
558
559 unregister_ioctl:
560 btrfs_interface_exit();
561 free_extent_map:
562 extent_map_exit();
563 free_extent_io:
564 extent_io_exit();
565 free_cachep:
566 btrfs_destroy_cachep();
567 free_transaction_sys:
568 btrfs_exit_transaction_sys();
569 btrfs_exit_sysfs();
570 return err;
571 }
572
573 static void __exit exit_btrfs_fs(void)
574 {
575 btrfs_exit_transaction_sys();
576 btrfs_destroy_cachep();
577 extent_map_exit();
578 extent_io_exit();
579 btrfs_interface_exit();
580 unregister_filesystem(&btrfs_fs_type);
581 btrfs_exit_sysfs();
582 btrfs_cleanup_fs_uuids();
583 }
584
585 module_init(init_btrfs_fs)
586 module_exit(exit_btrfs_fs)
587
588 MODULE_LICENSE("GPL");
This page took 0.05263 seconds and 5 git commands to generate.