]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/btrfs/super.c
Btrfs: Add mount -o nodatasum to turn of file data checksumming
[mirror_ubuntu-bionic-kernel.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 "ctree.h"
38 #include "disk-io.h"
39 #include "transaction.h"
40 #include "btrfs_inode.h"
41 #include "ioctl.h"
42 #include "print-tree.h"
43 #include "xattr.h"
44
45 #define BTRFS_SUPER_MAGIC 0x9123683E
46
47 static struct super_operations btrfs_super_ops;
48
49 static void btrfs_put_super (struct super_block * sb)
50 {
51 struct btrfs_root *root = btrfs_sb(sb);
52 struct btrfs_fs_info *fs = root->fs_info;
53 int ret;
54
55 ret = close_ctree(root);
56 if (ret) {
57 printk("close ctree returns %d\n", ret);
58 }
59 btrfs_sysfs_del_super(fs);
60 sb->s_fs_info = NULL;
61 }
62
63 enum {
64 Opt_subvol, Opt_nodatasum, Opt_err,
65 };
66
67 static match_table_t tokens = {
68 {Opt_subvol, "subvol=%s"},
69 {Opt_nodatasum, "nodatasum"},
70 {Opt_err, NULL}
71 };
72
73 static int parse_options (char * options,
74 struct btrfs_root *root,
75 char **subvol_name)
76 {
77 char * p;
78 struct btrfs_fs_info *info = NULL;
79 substring_t args[MAX_OPT_ARGS];
80
81 if (root)
82 info = root->fs_info;
83
84 if (!options)
85 return 1;
86
87 while ((p = strsep (&options, ",")) != NULL) {
88 int token;
89 if (!*p)
90 continue;
91
92 token = match_token(p, tokens, args);
93 switch (token) {
94 case Opt_subvol:
95 if (subvol_name)
96 *subvol_name = match_strdup(&args[0]);
97 break;
98 case Opt_nodatasum:
99 if (root)
100 btrfs_set_opt(info->mount_opt, NODATASUM);
101 break;
102 default:
103 return 0;
104 }
105 }
106 return 1;
107 }
108
109 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
110 {
111 struct inode * inode;
112 struct dentry * root_dentry;
113 struct btrfs_super_block *disk_super;
114 struct btrfs_root *tree_root;
115 struct btrfs_inode *bi;
116 int err;
117
118 sb->s_maxbytes = MAX_LFS_FILESIZE;
119 sb->s_magic = BTRFS_SUPER_MAGIC;
120 sb->s_op = &btrfs_super_ops;
121 sb->s_xattr = btrfs_xattr_handlers;
122 sb->s_time_gran = 1;
123
124 tree_root = open_ctree(sb);
125
126 if (!tree_root || IS_ERR(tree_root)) {
127 printk("btrfs: open_ctree failed\n");
128 return -EIO;
129 }
130 sb->s_fs_info = tree_root;
131 disk_super = &tree_root->fs_info->super_copy;
132 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
133 tree_root);
134 bi = BTRFS_I(inode);
135 bi->location.objectid = inode->i_ino;
136 bi->location.offset = 0;
137 bi->root = tree_root;
138
139 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
140
141 if (!inode) {
142 err = -ENOMEM;
143 goto fail_close;
144 }
145 if (inode->i_state & I_NEW) {
146 btrfs_read_locked_inode(inode);
147 unlock_new_inode(inode);
148 }
149
150 root_dentry = d_alloc_root(inode);
151 if (!root_dentry) {
152 iput(inode);
153 err = -ENOMEM;
154 goto fail_close;
155 }
156
157 parse_options((char *)data, tree_root, NULL);
158
159 /* this does the super kobj at the same time */
160 err = btrfs_sysfs_add_super(tree_root->fs_info);
161 if (err)
162 goto fail_close;
163
164 sb->s_root = root_dentry;
165 btrfs_transaction_queue_work(tree_root, HZ * 30);
166 return 0;
167
168 fail_close:
169 close_ctree(tree_root);
170 return err;
171 }
172
173 static int btrfs_sync_fs(struct super_block *sb, int wait)
174 {
175 struct btrfs_trans_handle *trans;
176 struct btrfs_root *root;
177 int ret;
178 root = btrfs_sb(sb);
179
180 sb->s_dirt = 0;
181 if (!wait) {
182 filemap_flush(root->fs_info->btree_inode->i_mapping);
183 return 0;
184 }
185 btrfs_clean_old_snapshots(root);
186 mutex_lock(&root->fs_info->fs_mutex);
187 btrfs_defrag_dirty_roots(root->fs_info);
188 trans = btrfs_start_transaction(root, 1);
189 ret = btrfs_commit_transaction(trans, root);
190 sb->s_dirt = 0;
191 mutex_unlock(&root->fs_info->fs_mutex);
192 return ret;
193 }
194
195 static void btrfs_write_super(struct super_block *sb)
196 {
197 sb->s_dirt = 0;
198 }
199
200 /*
201 * This is almost a copy of get_sb_bdev in fs/super.c.
202 * We need the local copy to allow direct mounting of
203 * subvolumes, but this could be easily integrated back
204 * into the generic version. --hch
205 */
206
207 /* start copy & paste */
208 static int set_bdev_super(struct super_block *s, void *data)
209 {
210 s->s_bdev = data;
211 s->s_dev = s->s_bdev->bd_dev;
212 return 0;
213 }
214
215 static int test_bdev_super(struct super_block *s, void *data)
216 {
217 return (void *)s->s_bdev == data;
218 }
219
220 int btrfs_get_sb_bdev(struct file_system_type *fs_type,
221 int flags, const char *dev_name, void *data,
222 int (*fill_super)(struct super_block *, void *, int),
223 struct vfsmount *mnt, const char *subvol)
224 {
225 struct block_device *bdev = NULL;
226 struct super_block *s;
227 struct dentry *root;
228 int error = 0;
229
230 bdev = open_bdev_excl(dev_name, flags, fs_type);
231 if (IS_ERR(bdev))
232 return PTR_ERR(bdev);
233
234 /*
235 * once the super is inserted into the list by sget, s_umount
236 * will protect the lockfs code from trying to start a snapshot
237 * while we are mounting
238 */
239 down(&bdev->bd_mount_sem);
240 s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
241 up(&bdev->bd_mount_sem);
242 if (IS_ERR(s))
243 goto error_s;
244
245 if (s->s_root) {
246 if ((flags ^ s->s_flags) & MS_RDONLY) {
247 up_write(&s->s_umount);
248 deactivate_super(s);
249 error = -EBUSY;
250 goto error_bdev;
251 }
252
253 close_bdev_excl(bdev);
254 } else {
255 char b[BDEVNAME_SIZE];
256
257 s->s_flags = flags;
258 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
259 sb_set_blocksize(s, block_size(bdev));
260 error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
261 if (error) {
262 up_write(&s->s_umount);
263 deactivate_super(s);
264 goto error;
265 }
266
267 s->s_flags |= MS_ACTIVE;
268 }
269
270 if (subvol) {
271 root = lookup_one_len(subvol, s->s_root, strlen(subvol));
272 if (IS_ERR(root)) {
273 up_write(&s->s_umount);
274 deactivate_super(s);
275 error = PTR_ERR(root);
276 goto error;
277 }
278 if (!root->d_inode) {
279 dput(root);
280 up_write(&s->s_umount);
281 deactivate_super(s);
282 error = -ENXIO;
283 goto error;
284 }
285 } else {
286 root = dget(s->s_root);
287 }
288
289 mnt->mnt_sb = s;
290 mnt->mnt_root = root;
291 return 0;
292
293 error_s:
294 error = PTR_ERR(s);
295 error_bdev:
296 close_bdev_excl(bdev);
297 error:
298 return error;
299 }
300 /* end copy & paste */
301
302 static int btrfs_get_sb(struct file_system_type *fs_type,
303 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
304 {
305 int ret;
306 char *subvol_name = NULL;
307
308 parse_options((char *)data, NULL, &subvol_name);
309 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
310 btrfs_fill_super, mnt,
311 subvol_name ? subvol_name : "default");
312 return ret;
313 }
314
315 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
316 {
317 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
318 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
319 int bits = dentry->d_sb->s_blocksize_bits;
320
321 buf->f_namelen = BTRFS_NAME_LEN;
322 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
323 buf->f_bfree = buf->f_blocks -
324 (btrfs_super_bytes_used(disk_super) >> bits);
325 buf->f_bavail = buf->f_bfree;
326 buf->f_bsize = dentry->d_sb->s_blocksize;
327 buf->f_type = BTRFS_SUPER_MAGIC;
328 return 0;
329 }
330
331 static struct file_system_type btrfs_fs_type = {
332 .owner = THIS_MODULE,
333 .name = "btrfs",
334 .get_sb = btrfs_get_sb,
335 .kill_sb = kill_block_super,
336 .fs_flags = FS_REQUIRES_DEV,
337 };
338
339 static struct super_operations btrfs_super_ops = {
340 .delete_inode = btrfs_delete_inode,
341 .put_super = btrfs_put_super,
342 .read_inode = btrfs_read_locked_inode,
343 .write_super = btrfs_write_super,
344 .sync_fs = btrfs_sync_fs,
345 .write_inode = btrfs_write_inode,
346 .dirty_inode = btrfs_dirty_inode,
347 .alloc_inode = btrfs_alloc_inode,
348 .destroy_inode = btrfs_destroy_inode,
349 .statfs = btrfs_statfs,
350 };
351
352 static int __init init_btrfs_fs(void)
353 {
354 int err;
355
356 err = btrfs_init_sysfs();
357 if (err)
358 return err;
359
360 btrfs_init_transaction_sys();
361 err = btrfs_init_cachep();
362 if (err)
363 goto free_transaction_sys;
364 err = extent_map_init();
365 if (err)
366 goto free_cachep;
367
368 err = register_filesystem(&btrfs_fs_type);
369 if (err)
370 goto free_extent_map;
371 return 0;
372
373 free_extent_map:
374 extent_map_exit();
375 free_cachep:
376 btrfs_destroy_cachep();
377 free_transaction_sys:
378 btrfs_exit_transaction_sys();
379 btrfs_exit_sysfs();
380 return err;
381 }
382
383 static void __exit exit_btrfs_fs(void)
384 {
385 btrfs_exit_transaction_sys();
386 btrfs_destroy_cachep();
387 extent_map_exit();
388 unregister_filesystem(&btrfs_fs_type);
389 btrfs_exit_sysfs();
390 }
391
392 module_init(init_btrfs_fs)
393 module_exit(exit_btrfs_fs)
394
395 MODULE_LICENSE("GPL");