blob: a68101ae1a3b600f10b9286b5e867f30f00227e8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -040020#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -040021#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
Chris Masondee26a92007-03-26 16:00:06 -040029#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040030#include <linux/swap.h>
31#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040032#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040033#include <linux/compat.h>
Chris Mason2e635a22007-03-21 11:12:56 -040034#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040035#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040036#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040037#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040038#include "ioctl.h"
Chris Mason3a686372007-05-24 13:35:57 -040039#include "print-tree.h"
Chris Mason2e635a22007-03-21 11:12:56 -040040
41#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040042
Chris Masone20d96d2007-03-22 12:13:20 -040043static struct super_operations btrfs_super_ops;
Chris Masone20d96d2007-03-22 12:13:20 -040044
45static void btrfs_put_super (struct super_block * sb)
46{
47 struct btrfs_root *root = btrfs_sb(sb);
Josef Bacik58176a92007-08-29 15:47:34 -040048 struct btrfs_fs_info *fs = root->fs_info;
Chris Masone20d96d2007-03-22 12:13:20 -040049 int ret;
50
51 ret = close_ctree(root);
52 if (ret) {
53 printk("close ctree returns %d\n", ret);
54 }
Josef Bacik58176a92007-08-29 15:47:34 -040055 btrfs_sysfs_del_super(fs);
Chris Masone20d96d2007-03-22 12:13:20 -040056 sb->s_fs_info = NULL;
57}
Chris Mason2e635a22007-03-21 11:12:56 -040058
59static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
60{
61 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -040062 struct dentry * root_dentry;
63 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -040064 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -040065 struct btrfs_inode *bi;
Chris Mason39279cc2007-06-12 06:35:45 -040066 int err;
Chris Mason2e635a22007-03-21 11:12:56 -040067
68 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -040069 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -040070 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -040071 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -040072
Chris Mason0f7d52f2007-04-09 10:42:37 -040073 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -040074
Chris Mason39279cc2007-06-12 06:35:45 -040075 if (!tree_root || IS_ERR(tree_root)) {
Chris Masone20d96d2007-03-22 12:13:20 -040076 printk("btrfs: open_ctree failed\n");
77 return -EIO;
78 }
Chris Mason0f7d52f2007-04-09 10:42:37 -040079 sb->s_fs_info = tree_root;
80 disk_super = tree_root->fs_info->disk_super;
Chris Masonc5739bb2007-04-10 09:27:04 -040081 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
82 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -040083 bi = BTRFS_I(inode);
84 bi->location.objectid = inode->i_ino;
85 bi->location.offset = 0;
86 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -040087 bi->root = tree_root;
Chris Masonb888db22007-08-27 16:49:44 -040088
Chris Masond6e4a422007-04-06 15:37:36 -040089 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
90
Chris Mason39279cc2007-06-12 06:35:45 -040091 if (!inode) {
92 err = -ENOMEM;
93 goto fail_close;
94 }
Chris Masone20d96d2007-03-22 12:13:20 -040095 if (inode->i_state & I_NEW) {
96 btrfs_read_locked_inode(inode);
97 unlock_new_inode(inode);
98 }
Chris Mason2e635a22007-03-21 11:12:56 -040099
Chris Masone20d96d2007-03-22 12:13:20 -0400100 root_dentry = d_alloc_root(inode);
101 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400102 iput(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400103 err = -ENOMEM;
104 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -0400105 }
Josef Bacik58176a92007-08-29 15:47:34 -0400106
107 /* this does the super kobj at the same time */
108 err = btrfs_sysfs_add_super(tree_root->fs_info);
109 if (err)
110 goto fail_close;
111
Chris Masone20d96d2007-03-22 12:13:20 -0400112 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -0400113 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason2e635a22007-03-21 11:12:56 -0400114 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -0400115
Chris Mason39279cc2007-06-12 06:35:45 -0400116fail_close:
117 close_ctree(tree_root);
Chris Masond5719762007-03-23 10:01:08 -0400118 return err;
119}
120
Chris Masond5719762007-03-23 10:01:08 -0400121static int btrfs_sync_fs(struct super_block *sb, int wait)
122{
123 struct btrfs_trans_handle *trans;
124 struct btrfs_root *root;
125 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400126 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400127
Chris Masond5719762007-03-23 10:01:08 -0400128 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400129 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400130 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400131 return 0;
132 }
Chris Masone9d0b132007-08-10 14:06:19 -0400133 btrfs_clean_old_snapshots(root);
Chris Masond561c022007-03-23 19:47:49 -0400134 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone9d0b132007-08-10 14:06:19 -0400135 btrfs_defrag_dirty_roots(root->fs_info);
Chris Masond5719762007-03-23 10:01:08 -0400136 trans = btrfs_start_transaction(root, 1);
137 ret = btrfs_commit_transaction(trans, root);
138 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400139 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -0400140 return ret;
Chris Masond5719762007-03-23 10:01:08 -0400141}
142
Chris Masond561c022007-03-23 19:47:49 -0400143static void btrfs_write_super(struct super_block *sb)
144{
Chris Mason08607c12007-06-08 15:33:54 -0400145 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400146}
147
Chris Mason2e635a22007-03-21 11:12:56 -0400148static int btrfs_get_sb(struct file_system_type *fs_type,
149 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
150{
151 return get_sb_bdev(fs_type, flags, dev_name, data,
152 btrfs_fill_super, mnt);
153}
154
Chris Mason8fd17792007-04-19 21:01:03 -0400155static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
156{
157 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
Chris Mason4b52dff2007-06-26 10:06:50 -0400158 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
Chris Mason8fd17792007-04-19 21:01:03 -0400159
160 buf->f_namelen = BTRFS_NAME_LEN;
161 buf->f_blocks = btrfs_super_total_blocks(disk_super);
162 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
163 buf->f_bavail = buf->f_bfree;
164 buf->f_bsize = dentry->d_sb->s_blocksize;
165 buf->f_type = BTRFS_SUPER_MAGIC;
166 return 0;
167}
Chris Masonb5133862007-04-24 11:52:22 -0400168
Chris Mason2e635a22007-03-21 11:12:56 -0400169static struct file_system_type btrfs_fs_type = {
170 .owner = THIS_MODULE,
171 .name = "btrfs",
172 .get_sb = btrfs_get_sb,
173 .kill_sb = kill_block_super,
174 .fs_flags = FS_REQUIRES_DEV,
175};
176
Chris Masone20d96d2007-03-22 12:13:20 -0400177static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -0400178 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400179 .put_super = btrfs_put_super,
180 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400181 .write_super = btrfs_write_super,
182 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -0400183 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -0400184 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -0400185 .alloc_inode = btrfs_alloc_inode,
186 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -0400187 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -0400188};
189
Chris Mason2e635a22007-03-21 11:12:56 -0400190static int __init init_btrfs_fs(void)
191{
Chris Mason2c90e5d2007-04-02 10:50:19 -0400192 int err;
Josef Bacik58176a92007-08-29 15:47:34 -0400193
194 err = btrfs_init_sysfs();
195 if (err)
196 return err;
197
Chris Mason08607c12007-06-08 15:33:54 -0400198 btrfs_init_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400199 err = btrfs_init_cachep();
Chris Mason2c90e5d2007-04-02 10:50:19 -0400200 if (err)
201 return err;
Chris Masona52d9a82007-08-27 16:49:44 -0400202 extent_map_init();
Chris Mason2e635a22007-03-21 11:12:56 -0400203 return register_filesystem(&btrfs_fs_type);
204}
205
206static void __exit exit_btrfs_fs(void)
207{
Chris Mason08607c12007-06-08 15:33:54 -0400208 btrfs_exit_transaction_sys();
Chris Mason39279cc2007-06-12 06:35:45 -0400209 btrfs_destroy_cachep();
Chris Masona52d9a82007-08-27 16:49:44 -0400210 extent_map_exit();
Chris Mason2e635a22007-03-21 11:12:56 -0400211 unregister_filesystem(&btrfs_fs_type);
Josef Bacik58176a92007-08-29 15:47:34 -0400212 btrfs_exit_sysfs();
Chris Mason2e635a22007-03-21 11:12:56 -0400213}
214
215module_init(init_btrfs_fs)
216module_exit(exit_btrfs_fs)
217
218MODULE_LICENSE("GPL");