Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 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 | |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 19 | #include <linux/blkdev.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 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> |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 30 | #include <linux/mount.h> |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 31 | #include <linux/mpage.h> |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 32 | #include <linux/swap.h> |
| 33 | #include <linux/writeback.h> |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 34 | #include <linux/statfs.h> |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 35 | #include <linux/compat.h> |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 36 | #include <linux/parser.h> |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 37 | #include <linux/ctype.h> |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 38 | #include <linux/namei.h> |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 39 | #include <linux/miscdevice.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 40 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 41 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 42 | #include "transaction.h" |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 43 | #include "btrfs_inode.h" |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 44 | #include "ioctl.h" |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 45 | #include "print-tree.h" |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 46 | #include "xattr.h" |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 47 | #include "volumes.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 48 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 49 | #define BTRFS_SUPER_MAGIC 0x9123683E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 50 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 51 | static struct super_operations btrfs_super_ops; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 52 | |
| 53 | static void btrfs_put_super (struct super_block * sb) |
| 54 | { |
| 55 | struct btrfs_root *root = btrfs_sb(sb); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 56 | struct btrfs_fs_info *fs = root->fs_info; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 57 | int ret; |
| 58 | |
| 59 | ret = close_ctree(root); |
| 60 | if (ret) { |
| 61 | printk("close ctree returns %d\n", ret); |
| 62 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 63 | btrfs_sysfs_del_super(fs); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 64 | sb->s_fs_info = NULL; |
| 65 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 66 | |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 67 | enum { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 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, |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static match_table_t tokens = { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 74 | {Opt_degraded, "degraded"}, |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 75 | {Opt_subvol, "subvol=%s"}, |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 76 | {Opt_nodatasum, "nodatasum"}, |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 77 | {Opt_nodatacow, "nodatacow"}, |
Chris Mason | 21ad10c | 2008-01-09 09:23:21 -0500 | [diff] [blame] | 78 | {Opt_nobarrier, "nobarrier"}, |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 79 | {Opt_max_extent, "max_extent=%s"}, |
Chris Mason | 6f568d3 | 2008-01-29 16:03:38 -0500 | [diff] [blame] | 80 | {Opt_max_inline, "max_inline=%s"}, |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 81 | {Opt_alloc_start, "alloc_start=%s"}, |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 82 | {Opt_ssd, "ssd"}, |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 83 | {Opt_err, NULL} |
| 84 | }; |
| 85 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 86 | u64 btrfs_parse_size(char *str) |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 87 | { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 88 | u64 res; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 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 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 111 | int btrfs_parse_options(char *options, struct btrfs_root *root, |
| 112 | char **subvol_name) |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 113 | { |
| 114 | char * p; |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 115 | struct btrfs_fs_info *info = NULL; |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 116 | substring_t args[MAX_OPT_ARGS]; |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 117 | |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 118 | if (!options) |
| 119 | return 1; |
| 120 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 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 | |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 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) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 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; |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 145 | case Opt_subvol: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 146 | if (subvol_name) { |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 147 | *subvol_name = match_strdup(&args[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 148 | } |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 149 | break; |
| 150 | case Opt_nodatasum: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 151 | if (info) { |
| 152 | printk("btrfs: setting nodatacsum\n"); |
Chris Mason | b6cda9b | 2007-12-14 15:30:32 -0500 | [diff] [blame] | 153 | btrfs_set_opt(info->mount_opt, NODATASUM); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 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 | } |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 162 | break; |
Chris Mason | e18e480 | 2008-01-18 10:54:22 -0500 | [diff] [blame] | 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; |
Chris Mason | 21ad10c | 2008-01-09 09:23:21 -0500 | [diff] [blame] | 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; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 175 | case Opt_max_extent: |
| 176 | if (info) { |
| 177 | char *num = match_strdup(&args[0]); |
| 178 | if (num) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 179 | info->max_extent = |
| 180 | btrfs_parse_size(num); |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 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; |
Chris Mason | 6f568d3 | 2008-01-29 16:03:38 -0500 | [diff] [blame] | 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; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 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; |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 219 | default: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 220 | break; |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 221 | } |
| 222 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 223 | kfree(options); |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 224 | return 1; |
| 225 | } |
| 226 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 227 | static int btrfs_fill_super(struct super_block * sb, |
| 228 | struct btrfs_fs_devices *fs_devices, |
| 229 | void * data, int silent) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 230 | { |
| 231 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 232 | struct dentry * root_dentry; |
| 233 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 234 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 235 | struct btrfs_inode *bi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 236 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 237 | |
| 238 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 239 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 240 | sb->s_op = &btrfs_super_ops; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 241 | sb->s_xattr = btrfs_xattr_handlers; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 242 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 243 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 244 | tree_root = open_ctree(sb, fs_devices, (char *)data); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 245 | |
Yan | e58ca02 | 2008-04-01 11:21:34 -0400 | [diff] [blame] | 246 | if (IS_ERR(tree_root)) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 247 | printk("btrfs: open_ctree failed\n"); |
Yan | e58ca02 | 2008-04-01 11:21:34 -0400 | [diff] [blame] | 248 | return PTR_ERR(tree_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 249 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 250 | sb->s_fs_info = tree_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 251 | disk_super = &tree_root->fs_info->super_copy; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 252 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 253 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 254 | bi = BTRFS_I(inode); |
| 255 | bi->location.objectid = inode->i_ino; |
| 256 | bi->location.offset = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 257 | bi->root = tree_root; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 258 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 259 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 260 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 261 | if (!inode) { |
| 262 | err = -ENOMEM; |
| 263 | goto fail_close; |
| 264 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 265 | if (inode->i_state & I_NEW) { |
| 266 | btrfs_read_locked_inode(inode); |
| 267 | unlock_new_inode(inode); |
| 268 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 269 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 270 | root_dentry = d_alloc_root(inode); |
| 271 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 272 | iput(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 273 | err = -ENOMEM; |
| 274 | goto fail_close; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 275 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 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 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 282 | sb->s_root = root_dentry; |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 283 | btrfs_transaction_queue_work(tree_root, HZ * 30); |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 284 | |
| 285 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25) |
| 286 | save_mount_options(sb, data); |
| 287 | #endif |
| 288 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 289 | return 0; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 290 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 291 | fail_close: |
| 292 | close_ctree(tree_root); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 293 | return err; |
| 294 | } |
| 295 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 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; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 301 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 302 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 303 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 304 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 305 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 308 | btrfs_clean_old_snapshots(root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 309 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 310 | btrfs_defrag_dirty_roots(root->fs_info); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 311 | trans = btrfs_start_transaction(root, 1); |
| 312 | ret = btrfs_commit_transaction(trans, root); |
| 313 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 314 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 315 | return ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 318 | static void btrfs_write_super(struct super_block *sb) |
| 319 | { |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 320 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 323 | static int btrfs_test_super(struct super_block *s, void *data) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 324 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 325 | struct btrfs_fs_devices *test_fs_devices = data; |
| 326 | struct btrfs_root *root = btrfs_sb(s); |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 327 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 328 | return root->fs_info->fs_devices == test_fs_devices; |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | int btrfs_get_sb_bdev(struct file_system_type *fs_type, |
| 332 | int flags, const char *dev_name, void *data, |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 333 | struct vfsmount *mnt, const char *subvol) |
| 334 | { |
| 335 | struct block_device *bdev = NULL; |
| 336 | struct super_block *s; |
| 337 | struct dentry *root; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 338 | struct btrfs_fs_devices *fs_devices = NULL; |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 339 | int error = 0; |
| 340 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 341 | error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices); |
| 342 | if (error) |
| 343 | return error; |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 344 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 345 | error = btrfs_open_devices(fs_devices, flags, fs_type); |
| 346 | if (error) |
| 347 | return error; |
| 348 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 349 | bdev = fs_devices->latest_bdev; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 350 | btrfs_lock_volumes(); |
| 351 | s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); |
| 352 | btrfs_unlock_volumes(); |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 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 | |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 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)); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 369 | error = btrfs_fill_super(s, fs_devices, data, |
| 370 | flags & MS_SILENT ? 1 : 0); |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 371 | if (error) { |
| 372 | up_write(&s->s_umount); |
| 373 | deactivate_super(s); |
| 374 | goto error; |
| 375 | } |
| 376 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 377 | btrfs_sb(s)->fs_info->bdev_holder = fs_type; |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 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: |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 407 | btrfs_close_devices(fs_devices); |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 408 | error: |
| 409 | return error; |
| 410 | } |
| 411 | /* end copy & paste */ |
| 412 | |
| 413 | static int btrfs_get_sb(struct file_system_type *fs_type, |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 414 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 415 | { |
| 416 | int ret; |
Chris Mason | 95e0528 | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 417 | char *subvol_name = NULL; |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 418 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 419 | btrfs_parse_options((char *)data, NULL, &subvol_name); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 420 | ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt, |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 421 | subvol_name ? subvol_name : "default"); |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 422 | if (subvol_name) |
| 423 | kfree(subvol_name); |
Yan | 4b82d6e | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 424 | return ret; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 427 | static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 428 | { |
| 429 | struct btrfs_root *root = btrfs_sb(dentry->d_sb); |
Chris Mason | 4b52dff | 2007-06-26 10:06:50 -0400 | [diff] [blame] | 430 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 431 | int bits = dentry->d_sb->s_blocksize_bits; |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 432 | |
| 433 | buf->f_namelen = BTRFS_NAME_LEN; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 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); |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 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 | } |
Chris Mason | b513386 | 2007-04-24 11:52:22 -0400 | [diff] [blame] | 442 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 443 | static struct file_system_type btrfs_fs_type = { |
| 444 | .owner = THIS_MODULE, |
| 445 | .name = "btrfs", |
| 446 | .get_sb = btrfs_get_sb, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 447 | .kill_sb = kill_anon_super, |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 448 | .fs_flags = FS_REQUIRES_DEV, |
| 449 | }; |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 450 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 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; |
Linda Knippers | f819d83 | 2008-06-09 22:17:11 -0400 | [diff] [blame^] | 456 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 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); |
Linda Knippers | f819d83 | 2008-06-09 22:17:11 -0400 | [diff] [blame^] | 473 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Yan | ed0dab6 | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 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 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 487 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 488 | static struct super_operations btrfs_super_ops = { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 489 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 490 | .put_super = btrfs_put_super, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 491 | .write_super = btrfs_write_super, |
| 492 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 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 |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 498 | .write_inode = btrfs_write_inode, |
Chris Mason | b513386 | 2007-04-24 11:52:22 -0400 | [diff] [blame] | 499 | .dirty_inode = btrfs_dirty_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 500 | .alloc_inode = btrfs_alloc_inode, |
| 501 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | 8fd1779 | 2007-04-19 21:01:03 -0400 | [diff] [blame] | 502 | .statfs = btrfs_statfs, |
Yan | ed0dab6 | 2008-01-22 12:46:56 -0500 | [diff] [blame] | 503 | .write_super_lockfs = btrfs_write_super_lockfs, |
| 504 | .unlockfs = btrfs_unlockfs, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 505 | }; |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 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 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 530 | static int __init init_btrfs_fs(void) |
| 531 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 532 | int err; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 533 | |
| 534 | err = btrfs_init_sysfs(); |
| 535 | if (err) |
| 536 | return err; |
| 537 | |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 538 | btrfs_init_transaction_sys(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 539 | err = btrfs_init_cachep(); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 540 | if (err) |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 541 | goto free_transaction_sys; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 542 | |
| 543 | err = extent_io_init(); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 544 | if (err) |
| 545 | goto free_cachep; |
| 546 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 547 | err = extent_map_init(); |
| 548 | if (err) |
| 549 | goto free_extent_io; |
| 550 | |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 551 | err = btrfs_interface_init(); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 552 | if (err) |
| 553 | goto free_extent_map; |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 554 | err = register_filesystem(&btrfs_fs_type); |
| 555 | if (err) |
| 556 | goto unregister_ioctl; |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 557 | return 0; |
| 558 | |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 559 | unregister_ioctl: |
| 560 | btrfs_interface_exit(); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 561 | free_extent_map: |
| 562 | extent_map_exit(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 563 | free_extent_io: |
| 564 | extent_io_exit(); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 565 | free_cachep: |
| 566 | btrfs_destroy_cachep(); |
| 567 | free_transaction_sys: |
| 568 | btrfs_exit_transaction_sys(); |
| 569 | btrfs_exit_sysfs(); |
| 570 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void __exit exit_btrfs_fs(void) |
| 574 | { |
Chris Mason | 08607c1 | 2007-06-08 15:33:54 -0400 | [diff] [blame] | 575 | btrfs_exit_transaction_sys(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 576 | btrfs_destroy_cachep(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 577 | extent_map_exit(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 578 | extent_io_exit(); |
Chris Mason | a9218f6 | 2008-03-24 15:02:04 -0400 | [diff] [blame] | 579 | btrfs_interface_exit(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 580 | unregister_filesystem(&btrfs_fs_type); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 581 | btrfs_exit_sysfs(); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 582 | btrfs_cleanup_fs_uuids(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | module_init(init_btrfs_fs) |
| 586 | module_exit(exit_btrfs_fs) |
| 587 | |
| 588 | MODULE_LICENSE("GPL"); |