]>
Commit | Line | Data |
---|---|---|
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> | |
a9572a15 | 27 | #include <linux/seq_file.h> |
2e635a27 | 28 | #include <linux/string.h> |
2e635a27 | 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> |
1bcbf313 | 40 | #include <linux/magic.h> |
4b4e25f2 | 41 | #include "compat.h" |
2e635a27 | 42 | #include "ctree.h" |
e20d96d6 | 43 | #include "disk-io.h" |
d5719762 | 44 | #include "transaction.h" |
2c90e5d6 | 45 | #include "btrfs_inode.h" |
c5739bba | 46 | #include "ioctl.h" |
3a686375 | 47 | #include "print-tree.h" |
5103e947 | 48 | #include "xattr.h" |
8a4b83cc | 49 | #include "volumes.h" |
b3c3da71 | 50 | #include "version.h" |
be6e8dc0 | 51 | #include "export.h" |
c8b97818 | 52 | #include "compression.h" |
2e635a27 | 53 | |
b87221de | 54 | static const struct super_operations btrfs_super_ops; |
75dfe396 | 55 | |
d397712b | 56 | static void btrfs_put_super(struct super_block *sb) |
b18c6685 | 57 | { |
39279cc3 | 58 | struct btrfs_root *root = btrfs_sb(sb); |
b18c6685 | 59 | int ret; |
b18c6685 | 60 | |
39279cc3 | 61 | ret = close_ctree(root); |
39279cc3 | 62 | sb->s_fs_info = NULL; |
75dfe396 CM |
63 | } |
64 | ||
95e05289 | 65 | enum { |
43e570b0 | 66 | Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, |
dfe25020 | 67 | Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, |
451d7585 | 68 | Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, |
a555f810 CM |
69 | Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio, |
70 | Opt_flushoncommit, | |
e244a0ae | 71 | Opt_discard, Opt_err, |
95e05289 CM |
72 | }; |
73 | ||
74 | static match_table_t tokens = { | |
dfe25020 | 75 | {Opt_degraded, "degraded"}, |
95e05289 | 76 | {Opt_subvol, "subvol=%s"}, |
43e570b0 | 77 | {Opt_device, "device=%s"}, |
b6cda9bc | 78 | {Opt_nodatasum, "nodatasum"}, |
be20aa9d | 79 | {Opt_nodatacow, "nodatacow"}, |
21ad10cf | 80 | {Opt_nobarrier, "nobarrier"}, |
c59f8951 | 81 | {Opt_max_extent, "max_extent=%s"}, |
6f568d35 | 82 | {Opt_max_inline, "max_inline=%s"}, |
8f662a76 | 83 | {Opt_alloc_start, "alloc_start=%s"}, |
4543df7e | 84 | {Opt_thread_pool, "thread_pool=%d"}, |
c8b97818 | 85 | {Opt_compress, "compress"}, |
a555f810 | 86 | {Opt_compress_force, "compress-force"}, |
e18e4809 | 87 | {Opt_ssd, "ssd"}, |
451d7585 | 88 | {Opt_ssd_spread, "ssd_spread"}, |
3b30c22f | 89 | {Opt_nossd, "nossd"}, |
33268eaf | 90 | {Opt_noacl, "noacl"}, |
3a5e1404 | 91 | {Opt_notreelog, "notreelog"}, |
dccae999 | 92 | {Opt_flushoncommit, "flushoncommit"}, |
97e728d4 | 93 | {Opt_ratio, "metadata_ratio=%d"}, |
e244a0ae | 94 | {Opt_discard, "discard"}, |
33268eaf | 95 | {Opt_err, NULL}, |
95e05289 CM |
96 | }; |
97 | ||
edbd8d4e | 98 | u64 btrfs_parse_size(char *str) |
c59f8951 | 99 | { |
edbd8d4e | 100 | u64 res; |
c59f8951 CM |
101 | int mult = 1; |
102 | char *end; | |
103 | char last; | |
104 | ||
105 | res = simple_strtoul(str, &end, 10); | |
106 | ||
107 | last = end[0]; | |
108 | if (isalpha(last)) { | |
109 | last = tolower(last); | |
110 | switch (last) { | |
111 | case 'g': | |
112 | mult *= 1024; | |
113 | case 'm': | |
114 | mult *= 1024; | |
115 | case 'k': | |
116 | mult *= 1024; | |
117 | } | |
118 | res = res * mult; | |
119 | } | |
120 | return res; | |
121 | } | |
122 | ||
edf24abe CH |
123 | /* |
124 | * Regular mount options parser. Everything that is needed only when | |
125 | * reading in a new superblock is parsed here. | |
126 | */ | |
127 | int btrfs_parse_options(struct btrfs_root *root, char *options) | |
95e05289 | 128 | { |
edf24abe | 129 | struct btrfs_fs_info *info = root->fs_info; |
95e05289 | 130 | substring_t args[MAX_OPT_ARGS]; |
da495ecc | 131 | char *p, *num, *orig; |
4543df7e | 132 | int intarg; |
a7a3f7ca | 133 | int ret = 0; |
b6cda9bc | 134 | |
95e05289 | 135 | if (!options) |
edf24abe | 136 | return 0; |
95e05289 | 137 | |
be20aa9d CM |
138 | /* |
139 | * strsep changes the string, duplicate it because parse_options | |
140 | * gets called twice | |
141 | */ | |
142 | options = kstrdup(options, GFP_NOFS); | |
143 | if (!options) | |
144 | return -ENOMEM; | |
145 | ||
da495ecc | 146 | orig = options; |
be20aa9d | 147 | |
edf24abe | 148 | while ((p = strsep(&options, ",")) != NULL) { |
95e05289 CM |
149 | int token; |
150 | if (!*p) | |
151 | continue; | |
152 | ||
153 | token = match_token(p, tokens, args); | |
154 | switch (token) { | |
dfe25020 | 155 | case Opt_degraded: |
edf24abe CH |
156 | printk(KERN_INFO "btrfs: allowing degraded mounts\n"); |
157 | btrfs_set_opt(info->mount_opt, DEGRADED); | |
dfe25020 | 158 | break; |
95e05289 | 159 | case Opt_subvol: |
43e570b0 | 160 | case Opt_device: |
edf24abe | 161 | /* |
43e570b0 | 162 | * These are parsed by btrfs_parse_early_options |
edf24abe CH |
163 | * and can be happily ignored here. |
164 | */ | |
b6cda9bc CM |
165 | break; |
166 | case Opt_nodatasum: | |
067c28ad | 167 | printk(KERN_INFO "btrfs: setting nodatasum\n"); |
edf24abe | 168 | btrfs_set_opt(info->mount_opt, NODATASUM); |
be20aa9d CM |
169 | break; |
170 | case Opt_nodatacow: | |
edf24abe CH |
171 | printk(KERN_INFO "btrfs: setting nodatacow\n"); |
172 | btrfs_set_opt(info->mount_opt, NODATACOW); | |
173 | btrfs_set_opt(info->mount_opt, NODATASUM); | |
95e05289 | 174 | break; |
c8b97818 CM |
175 | case Opt_compress: |
176 | printk(KERN_INFO "btrfs: use compression\n"); | |
177 | btrfs_set_opt(info->mount_opt, COMPRESS); | |
178 | break; | |
a555f810 CM |
179 | case Opt_compress_force: |
180 | printk(KERN_INFO "btrfs: forcing compression\n"); | |
181 | btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); | |
182 | btrfs_set_opt(info->mount_opt, COMPRESS); | |
183 | break; | |
e18e4809 | 184 | case Opt_ssd: |
edf24abe CH |
185 | printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); |
186 | btrfs_set_opt(info->mount_opt, SSD); | |
e18e4809 | 187 | break; |
451d7585 CM |
188 | case Opt_ssd_spread: |
189 | printk(KERN_INFO "btrfs: use spread ssd " | |
190 | "allocation scheme\n"); | |
191 | btrfs_set_opt(info->mount_opt, SSD); | |
192 | btrfs_set_opt(info->mount_opt, SSD_SPREAD); | |
193 | break; | |
3b30c22f | 194 | case Opt_nossd: |
451d7585 CM |
195 | printk(KERN_INFO "btrfs: not using ssd allocation " |
196 | "scheme\n"); | |
c289811c | 197 | btrfs_set_opt(info->mount_opt, NOSSD); |
3b30c22f | 198 | btrfs_clear_opt(info->mount_opt, SSD); |
451d7585 | 199 | btrfs_clear_opt(info->mount_opt, SSD_SPREAD); |
3b30c22f | 200 | break; |
21ad10cf | 201 | case Opt_nobarrier: |
edf24abe CH |
202 | printk(KERN_INFO "btrfs: turning off barriers\n"); |
203 | btrfs_set_opt(info->mount_opt, NOBARRIER); | |
21ad10cf | 204 | break; |
4543df7e CM |
205 | case Opt_thread_pool: |
206 | intarg = 0; | |
207 | match_int(&args[0], &intarg); | |
208 | if (intarg) { | |
209 | info->thread_pool_size = intarg; | |
210 | printk(KERN_INFO "btrfs: thread pool %d\n", | |
211 | info->thread_pool_size); | |
212 | } | |
213 | break; | |
c59f8951 | 214 | case Opt_max_extent: |
edf24abe CH |
215 | num = match_strdup(&args[0]); |
216 | if (num) { | |
217 | info->max_extent = btrfs_parse_size(num); | |
218 | kfree(num); | |
219 | ||
220 | info->max_extent = max_t(u64, | |
221 | info->max_extent, root->sectorsize); | |
222 | printk(KERN_INFO "btrfs: max_extent at %llu\n", | |
21380931 | 223 | (unsigned long long)info->max_extent); |
c59f8951 CM |
224 | } |
225 | break; | |
6f568d35 | 226 | case Opt_max_inline: |
edf24abe CH |
227 | num = match_strdup(&args[0]); |
228 | if (num) { | |
229 | info->max_inline = btrfs_parse_size(num); | |
230 | kfree(num); | |
231 | ||
15ada040 CM |
232 | if (info->max_inline) { |
233 | info->max_inline = max_t(u64, | |
234 | info->max_inline, | |
235 | root->sectorsize); | |
236 | } | |
edf24abe | 237 | printk(KERN_INFO "btrfs: max_inline at %llu\n", |
21380931 | 238 | (unsigned long long)info->max_inline); |
6f568d35 CM |
239 | } |
240 | break; | |
8f662a76 | 241 | case Opt_alloc_start: |
edf24abe CH |
242 | num = match_strdup(&args[0]); |
243 | if (num) { | |
244 | info->alloc_start = btrfs_parse_size(num); | |
245 | kfree(num); | |
246 | printk(KERN_INFO | |
247 | "btrfs: allocations start at %llu\n", | |
21380931 | 248 | (unsigned long long)info->alloc_start); |
8f662a76 CM |
249 | } |
250 | break; | |
33268eaf JB |
251 | case Opt_noacl: |
252 | root->fs_info->sb->s_flags &= ~MS_POSIXACL; | |
253 | break; | |
3a5e1404 SW |
254 | case Opt_notreelog: |
255 | printk(KERN_INFO "btrfs: disabling tree log\n"); | |
256 | btrfs_set_opt(info->mount_opt, NOTREELOG); | |
257 | break; | |
dccae999 SW |
258 | case Opt_flushoncommit: |
259 | printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); | |
260 | btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); | |
261 | break; | |
97e728d4 JB |
262 | case Opt_ratio: |
263 | intarg = 0; | |
264 | match_int(&args[0], &intarg); | |
265 | if (intarg) { | |
266 | info->metadata_ratio = intarg; | |
267 | printk(KERN_INFO "btrfs: metadata ratio %d\n", | |
268 | info->metadata_ratio); | |
269 | } | |
270 | break; | |
e244a0ae CH |
271 | case Opt_discard: |
272 | btrfs_set_opt(info->mount_opt, DISCARD); | |
273 | break; | |
a7a3f7ca SW |
274 | case Opt_err: |
275 | printk(KERN_INFO "btrfs: unrecognized mount option " | |
276 | "'%s'\n", p); | |
277 | ret = -EINVAL; | |
278 | goto out; | |
95e05289 | 279 | default: |
be20aa9d | 280 | break; |
95e05289 CM |
281 | } |
282 | } | |
a7a3f7ca | 283 | out: |
da495ecc | 284 | kfree(orig); |
a7a3f7ca | 285 | return ret; |
edf24abe CH |
286 | } |
287 | ||
288 | /* | |
289 | * Parse mount options that are required early in the mount process. | |
290 | * | |
291 | * All other options will be parsed on much later in the mount process and | |
292 | * only when we need to allocate a new super block. | |
293 | */ | |
97288f2c | 294 | static int btrfs_parse_early_options(const char *options, fmode_t flags, |
43e570b0 CH |
295 | void *holder, char **subvol_name, |
296 | struct btrfs_fs_devices **fs_devices) | |
edf24abe CH |
297 | { |
298 | substring_t args[MAX_OPT_ARGS]; | |
299 | char *opts, *p; | |
300 | int error = 0; | |
301 | ||
302 | if (!options) | |
303 | goto out; | |
304 | ||
305 | /* | |
306 | * strsep changes the string, duplicate it because parse_options | |
307 | * gets called twice | |
308 | */ | |
309 | opts = kstrdup(options, GFP_KERNEL); | |
310 | if (!opts) | |
311 | return -ENOMEM; | |
312 | ||
313 | while ((p = strsep(&opts, ",")) != NULL) { | |
314 | int token; | |
315 | if (!*p) | |
316 | continue; | |
317 | ||
318 | token = match_token(p, tokens, args); | |
319 | switch (token) { | |
320 | case Opt_subvol: | |
321 | *subvol_name = match_strdup(&args[0]); | |
322 | break; | |
43e570b0 CH |
323 | case Opt_device: |
324 | error = btrfs_scan_one_device(match_strdup(&args[0]), | |
325 | flags, holder, fs_devices); | |
326 | if (error) | |
327 | goto out_free_opts; | |
328 | break; | |
edf24abe CH |
329 | default: |
330 | break; | |
331 | } | |
332 | } | |
333 | ||
43e570b0 | 334 | out_free_opts: |
edf24abe CH |
335 | kfree(opts); |
336 | out: | |
337 | /* | |
338 | * If no subvolume name is specified we use the default one. Allocate | |
3de4586c | 339 | * a copy of the string "." here so that code later in the |
edf24abe CH |
340 | * mount path doesn't care if it's the default volume or another one. |
341 | */ | |
342 | if (!*subvol_name) { | |
3de4586c | 343 | *subvol_name = kstrdup(".", GFP_KERNEL); |
edf24abe CH |
344 | if (!*subvol_name) |
345 | return -ENOMEM; | |
346 | } | |
347 | return error; | |
95e05289 CM |
348 | } |
349 | ||
d397712b | 350 | static int btrfs_fill_super(struct super_block *sb, |
8a4b83cc | 351 | struct btrfs_fs_devices *fs_devices, |
d397712b | 352 | void *data, int silent) |
75dfe396 | 353 | { |
d397712b CM |
354 | struct inode *inode; |
355 | struct dentry *root_dentry; | |
39279cc3 CM |
356 | struct btrfs_super_block *disk_super; |
357 | struct btrfs_root *tree_root; | |
5d4f98a2 | 358 | struct btrfs_key key; |
39279cc3 | 359 | int err; |
a429e513 | 360 | |
39279cc3 CM |
361 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
362 | sb->s_magic = BTRFS_SUPER_MAGIC; | |
363 | sb->s_op = &btrfs_super_ops; | |
be6e8dc0 | 364 | sb->s_export_op = &btrfs_export_ops; |
5103e947 | 365 | sb->s_xattr = btrfs_xattr_handlers; |
39279cc3 | 366 | sb->s_time_gran = 1; |
0eda294d | 367 | #ifdef CONFIG_BTRFS_FS_POSIX_ACL |
33268eaf | 368 | sb->s_flags |= MS_POSIXACL; |
49cf6f45 | 369 | #endif |
a429e513 | 370 | |
dfe25020 | 371 | tree_root = open_ctree(sb, fs_devices, (char *)data); |
6567e837 | 372 | |
e58ca020 | 373 | if (IS_ERR(tree_root)) { |
39279cc3 | 374 | printk("btrfs: open_ctree failed\n"); |
e58ca020 | 375 | return PTR_ERR(tree_root); |
a429e513 | 376 | } |
39279cc3 | 377 | sb->s_fs_info = tree_root; |
5f39d397 | 378 | disk_super = &tree_root->fs_info->super_copy; |
a429e513 | 379 | |
5d4f98a2 YZ |
380 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
381 | key.type = BTRFS_INODE_ITEM_KEY; | |
382 | key.offset = 0; | |
383 | inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); | |
384 | if (IS_ERR(inode)) { | |
385 | err = PTR_ERR(inode); | |
39279cc3 | 386 | goto fail_close; |
f254e52c | 387 | } |
f254e52c | 388 | |
39279cc3 CM |
389 | root_dentry = d_alloc_root(inode); |
390 | if (!root_dentry) { | |
391 | iput(inode); | |
392 | err = -ENOMEM; | |
393 | goto fail_close; | |
f254e52c | 394 | } |
e4404d6e | 395 | #if 0 |
58176a96 JB |
396 | /* this does the super kobj at the same time */ |
397 | err = btrfs_sysfs_add_super(tree_root->fs_info); | |
398 | if (err) | |
399 | goto fail_close; | |
e4404d6e | 400 | #endif |
58176a96 | 401 | |
39279cc3 | 402 | sb->s_root = root_dentry; |
6885f308 | 403 | |
6885f308 | 404 | save_mount_options(sb, data); |
2619ba1f | 405 | return 0; |
39279cc3 CM |
406 | |
407 | fail_close: | |
408 | close_ctree(tree_root); | |
409 | return err; | |
2619ba1f CM |
410 | } |
411 | ||
6bf13c0c | 412 | int btrfs_sync_fs(struct super_block *sb, int wait) |
c5739bba CM |
413 | { |
414 | struct btrfs_trans_handle *trans; | |
dccae999 | 415 | struct btrfs_root *root = btrfs_sb(sb); |
c5739bba | 416 | int ret; |
2619ba1f | 417 | |
39279cc3 CM |
418 | if (!wait) { |
419 | filemap_flush(root->fs_info->btree_inode->i_mapping); | |
420 | return 0; | |
421 | } | |
771ed689 | 422 | |
24bbcf04 YZ |
423 | btrfs_start_delalloc_inodes(root, 0); |
424 | btrfs_wait_ordered_extents(root, 0, 0); | |
771ed689 | 425 | |
c5739bba | 426 | trans = btrfs_start_transaction(root, 1); |
c5739bba | 427 | ret = btrfs_commit_transaction(trans, root); |
54aa1f4d | 428 | return ret; |
2c90e5d6 CM |
429 | } |
430 | ||
a9572a15 EP |
431 | static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs) |
432 | { | |
433 | struct btrfs_root *root = btrfs_sb(vfs->mnt_sb); | |
434 | struct btrfs_fs_info *info = root->fs_info; | |
435 | ||
436 | if (btrfs_test_opt(root, DEGRADED)) | |
437 | seq_puts(seq, ",degraded"); | |
438 | if (btrfs_test_opt(root, NODATASUM)) | |
439 | seq_puts(seq, ",nodatasum"); | |
440 | if (btrfs_test_opt(root, NODATACOW)) | |
441 | seq_puts(seq, ",nodatacow"); | |
442 | if (btrfs_test_opt(root, NOBARRIER)) | |
443 | seq_puts(seq, ",nobarrier"); | |
444 | if (info->max_extent != (u64)-1) | |
21380931 JB |
445 | seq_printf(seq, ",max_extent=%llu", |
446 | (unsigned long long)info->max_extent); | |
a9572a15 | 447 | if (info->max_inline != 8192 * 1024) |
21380931 JB |
448 | seq_printf(seq, ",max_inline=%llu", |
449 | (unsigned long long)info->max_inline); | |
a9572a15 | 450 | if (info->alloc_start != 0) |
21380931 JB |
451 | seq_printf(seq, ",alloc_start=%llu", |
452 | (unsigned long long)info->alloc_start); | |
a9572a15 EP |
453 | if (info->thread_pool_size != min_t(unsigned long, |
454 | num_online_cpus() + 2, 8)) | |
455 | seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); | |
456 | if (btrfs_test_opt(root, COMPRESS)) | |
457 | seq_puts(seq, ",compress"); | |
c289811c CM |
458 | if (btrfs_test_opt(root, NOSSD)) |
459 | seq_puts(seq, ",nossd"); | |
451d7585 CM |
460 | if (btrfs_test_opt(root, SSD_SPREAD)) |
461 | seq_puts(seq, ",ssd_spread"); | |
462 | else if (btrfs_test_opt(root, SSD)) | |
a9572a15 | 463 | seq_puts(seq, ",ssd"); |
3a5e1404 | 464 | if (btrfs_test_opt(root, NOTREELOG)) |
6b65c5c6 | 465 | seq_puts(seq, ",notreelog"); |
dccae999 | 466 | if (btrfs_test_opt(root, FLUSHONCOMMIT)) |
6b65c5c6 | 467 | seq_puts(seq, ",flushoncommit"); |
20a5239a MW |
468 | if (btrfs_test_opt(root, DISCARD)) |
469 | seq_puts(seq, ",discard"); | |
a9572a15 EP |
470 | if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) |
471 | seq_puts(seq, ",noacl"); | |
472 | return 0; | |
473 | } | |
474 | ||
a061fc8d | 475 | static int btrfs_test_super(struct super_block *s, void *data) |
4b82d6e4 | 476 | { |
a061fc8d CM |
477 | struct btrfs_fs_devices *test_fs_devices = data; |
478 | struct btrfs_root *root = btrfs_sb(s); | |
4b82d6e4 | 479 | |
a061fc8d | 480 | return root->fs_info->fs_devices == test_fs_devices; |
4b82d6e4 Y |
481 | } |
482 | ||
edf24abe CH |
483 | /* |
484 | * Find a superblock for the given device / mount point. | |
485 | * | |
486 | * Note: This is based on get_sb_bdev from fs/super.c with a few additions | |
487 | * for multiple device setup. Make sure to keep it in sync. | |
488 | */ | |
489 | static int btrfs_get_sb(struct file_system_type *fs_type, int flags, | |
490 | const char *dev_name, void *data, struct vfsmount *mnt) | |
4b82d6e4 | 491 | { |
edf24abe | 492 | char *subvol_name = NULL; |
4b82d6e4 Y |
493 | struct block_device *bdev = NULL; |
494 | struct super_block *s; | |
495 | struct dentry *root; | |
8a4b83cc | 496 | struct btrfs_fs_devices *fs_devices = NULL; |
97288f2c | 497 | fmode_t mode = FMODE_READ; |
4b82d6e4 Y |
498 | int error = 0; |
499 | ||
97288f2c CH |
500 | if (!(flags & MS_RDONLY)) |
501 | mode |= FMODE_WRITE; | |
502 | ||
503 | error = btrfs_parse_early_options(data, mode, fs_type, | |
43e570b0 | 504 | &subvol_name, &fs_devices); |
edf24abe | 505 | if (error) |
1f483660 | 506 | return error; |
edf24abe | 507 | |
97288f2c | 508 | error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices); |
8a4b83cc | 509 | if (error) |
edf24abe | 510 | goto error_free_subvol_name; |
4b82d6e4 | 511 | |
97288f2c | 512 | error = btrfs_open_devices(fs_devices, mode, fs_type); |
8a4b83cc | 513 | if (error) |
edf24abe | 514 | goto error_free_subvol_name; |
8a4b83cc | 515 | |
2b82032c YZ |
516 | if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { |
517 | error = -EACCES; | |
518 | goto error_close_devices; | |
519 | } | |
520 | ||
dfe25020 | 521 | bdev = fs_devices->latest_bdev; |
a061fc8d | 522 | s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); |
4b82d6e4 Y |
523 | if (IS_ERR(s)) |
524 | goto error_s; | |
525 | ||
526 | if (s->s_root) { | |
527 | if ((flags ^ s->s_flags) & MS_RDONLY) { | |
6f5bbff9 | 528 | deactivate_locked_super(s); |
4b82d6e4 | 529 | error = -EBUSY; |
c146afad | 530 | goto error_close_devices; |
4b82d6e4 Y |
531 | } |
532 | ||
2b82032c | 533 | btrfs_close_devices(fs_devices); |
4b82d6e4 Y |
534 | } else { |
535 | char b[BDEVNAME_SIZE]; | |
536 | ||
537 | s->s_flags = flags; | |
538 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); | |
8a4b83cc CM |
539 | error = btrfs_fill_super(s, fs_devices, data, |
540 | flags & MS_SILENT ? 1 : 0); | |
4b82d6e4 | 541 | if (error) { |
6f5bbff9 | 542 | deactivate_locked_super(s); |
1f483660 | 543 | goto error_free_subvol_name; |
4b82d6e4 Y |
544 | } |
545 | ||
788f20eb | 546 | btrfs_sb(s)->fs_info->bdev_holder = fs_type; |
4b82d6e4 Y |
547 | s->s_flags |= MS_ACTIVE; |
548 | } | |
549 | ||
76fcef19 DW |
550 | if (!strcmp(subvol_name, ".")) |
551 | root = dget(s->s_root); | |
552 | else { | |
553 | mutex_lock(&s->s_root->d_inode->i_mutex); | |
d397712b CM |
554 | root = lookup_one_len(subvol_name, s->s_root, |
555 | strlen(subvol_name)); | |
76fcef19 | 556 | mutex_unlock(&s->s_root->d_inode->i_mutex); |
d397712b | 557 | |
76fcef19 | 558 | if (IS_ERR(root)) { |
6f5bbff9 | 559 | deactivate_locked_super(s); |
76fcef19 | 560 | error = PTR_ERR(root); |
1f483660 | 561 | goto error_free_subvol_name; |
76fcef19 DW |
562 | } |
563 | if (!root->d_inode) { | |
564 | dput(root); | |
6f5bbff9 | 565 | deactivate_locked_super(s); |
76fcef19 | 566 | error = -ENXIO; |
1f483660 | 567 | goto error_free_subvol_name; |
76fcef19 | 568 | } |
4b82d6e4 Y |
569 | } |
570 | ||
571 | mnt->mnt_sb = s; | |
572 | mnt->mnt_root = root; | |
edf24abe CH |
573 | |
574 | kfree(subvol_name); | |
4b82d6e4 Y |
575 | return 0; |
576 | ||
577 | error_s: | |
578 | error = PTR_ERR(s); | |
c146afad | 579 | error_close_devices: |
8a4b83cc | 580 | btrfs_close_devices(fs_devices); |
edf24abe CH |
581 | error_free_subvol_name: |
582 | kfree(subvol_name); | |
4b82d6e4 Y |
583 | return error; |
584 | } | |
2e635a27 | 585 | |
c146afad YZ |
586 | static int btrfs_remount(struct super_block *sb, int *flags, char *data) |
587 | { | |
588 | struct btrfs_root *root = btrfs_sb(sb); | |
589 | int ret; | |
590 | ||
b288052e CM |
591 | ret = btrfs_parse_options(root, data); |
592 | if (ret) | |
593 | return -EINVAL; | |
594 | ||
c146afad YZ |
595 | if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) |
596 | return 0; | |
597 | ||
598 | if (*flags & MS_RDONLY) { | |
599 | sb->s_flags |= MS_RDONLY; | |
600 | ||
601 | ret = btrfs_commit_super(root); | |
602 | WARN_ON(ret); | |
603 | } else { | |
2b82032c YZ |
604 | if (root->fs_info->fs_devices->rw_devices == 0) |
605 | return -EACCES; | |
606 | ||
c146afad YZ |
607 | if (btrfs_super_log_root(&root->fs_info->super_copy) != 0) |
608 | return -EINVAL; | |
609 | ||
5d4f98a2 YZ |
610 | /* recover relocation */ |
611 | ret = btrfs_recover_relocation(root); | |
c146afad YZ |
612 | WARN_ON(ret); |
613 | ||
614 | ret = btrfs_cleanup_fs_roots(root->fs_info); | |
615 | WARN_ON(ret); | |
616 | ||
617 | sb->s_flags &= ~MS_RDONLY; | |
618 | } | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
8fd17795 CM |
623 | static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
624 | { | |
625 | struct btrfs_root *root = btrfs_sb(dentry->d_sb); | |
4b52dff6 | 626 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
db94535d | 627 | int bits = dentry->d_sb->s_blocksize_bits; |
9d03632e | 628 | __be32 *fsid = (__be32 *)root->fs_info->fsid; |
8fd17795 CM |
629 | |
630 | buf->f_namelen = BTRFS_NAME_LEN; | |
db94535d CM |
631 | buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; |
632 | buf->f_bfree = buf->f_blocks - | |
633 | (btrfs_super_bytes_used(disk_super) >> bits); | |
8fd17795 CM |
634 | buf->f_bavail = buf->f_bfree; |
635 | buf->f_bsize = dentry->d_sb->s_blocksize; | |
636 | buf->f_type = BTRFS_SUPER_MAGIC; | |
d397712b | 637 | |
9d03632e | 638 | /* We treat it as constant endianness (it doesn't matter _which_) |
d397712b | 639 | because we want the fsid to come out the same whether mounted |
9d03632e DW |
640 | on a big-endian or little-endian host */ |
641 | buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); | |
642 | buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); | |
32d48fa1 DW |
643 | /* Mask in the root object ID too, to disambiguate subvols */ |
644 | buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; | |
645 | buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; | |
646 | ||
8fd17795 CM |
647 | return 0; |
648 | } | |
b5133862 | 649 | |
2e635a27 CM |
650 | static struct file_system_type btrfs_fs_type = { |
651 | .owner = THIS_MODULE, | |
652 | .name = "btrfs", | |
653 | .get_sb = btrfs_get_sb, | |
a061fc8d | 654 | .kill_sb = kill_anon_super, |
2e635a27 CM |
655 | .fs_flags = FS_REQUIRES_DEV, |
656 | }; | |
a9218f6b | 657 | |
d352ac68 CM |
658 | /* |
659 | * used by btrfsctl to scan devices when no FS is mounted | |
660 | */ | |
8a4b83cc CM |
661 | static long btrfs_control_ioctl(struct file *file, unsigned int cmd, |
662 | unsigned long arg) | |
663 | { | |
664 | struct btrfs_ioctl_vol_args *vol; | |
665 | struct btrfs_fs_devices *fs_devices; | |
c071fcfd | 666 | int ret = -ENOTTY; |
8a4b83cc | 667 | |
e441d54d CM |
668 | if (!capable(CAP_SYS_ADMIN)) |
669 | return -EPERM; | |
670 | ||
dae7b665 LZ |
671 | vol = memdup_user((void __user *)arg, sizeof(*vol)); |
672 | if (IS_ERR(vol)) | |
673 | return PTR_ERR(vol); | |
c071fcfd | 674 | |
8a4b83cc CM |
675 | switch (cmd) { |
676 | case BTRFS_IOC_SCAN_DEV: | |
97288f2c | 677 | ret = btrfs_scan_one_device(vol->name, FMODE_READ, |
8a4b83cc CM |
678 | &btrfs_fs_type, &fs_devices); |
679 | break; | |
680 | } | |
dae7b665 | 681 | |
8a4b83cc | 682 | kfree(vol); |
f819d837 | 683 | return ret; |
8a4b83cc CM |
684 | } |
685 | ||
0176260f | 686 | static int btrfs_freeze(struct super_block *sb) |
ed0dab6b Y |
687 | { |
688 | struct btrfs_root *root = btrfs_sb(sb); | |
a74a4b97 CM |
689 | mutex_lock(&root->fs_info->transaction_kthread_mutex); |
690 | mutex_lock(&root->fs_info->cleaner_mutex); | |
0176260f | 691 | return 0; |
ed0dab6b Y |
692 | } |
693 | ||
0176260f | 694 | static int btrfs_unfreeze(struct super_block *sb) |
ed0dab6b Y |
695 | { |
696 | struct btrfs_root *root = btrfs_sb(sb); | |
a74a4b97 CM |
697 | mutex_unlock(&root->fs_info->cleaner_mutex); |
698 | mutex_unlock(&root->fs_info->transaction_kthread_mutex); | |
0176260f | 699 | return 0; |
ed0dab6b | 700 | } |
2e635a27 | 701 | |
b87221de | 702 | static const struct super_operations btrfs_super_ops = { |
76dda93c | 703 | .drop_inode = btrfs_drop_inode, |
134e9731 | 704 | .delete_inode = btrfs_delete_inode, |
e20d96d6 | 705 | .put_super = btrfs_put_super, |
d5719762 | 706 | .sync_fs = btrfs_sync_fs, |
a9572a15 | 707 | .show_options = btrfs_show_options, |
4730a4bc | 708 | .write_inode = btrfs_write_inode, |
b5133862 | 709 | .dirty_inode = btrfs_dirty_inode, |
2c90e5d6 CM |
710 | .alloc_inode = btrfs_alloc_inode, |
711 | .destroy_inode = btrfs_destroy_inode, | |
8fd17795 | 712 | .statfs = btrfs_statfs, |
c146afad | 713 | .remount_fs = btrfs_remount, |
0176260f LT |
714 | .freeze_fs = btrfs_freeze, |
715 | .unfreeze_fs = btrfs_unfreeze, | |
e20d96d6 | 716 | }; |
a9218f6b CM |
717 | |
718 | static const struct file_operations btrfs_ctl_fops = { | |
719 | .unlocked_ioctl = btrfs_control_ioctl, | |
720 | .compat_ioctl = btrfs_control_ioctl, | |
721 | .owner = THIS_MODULE, | |
722 | }; | |
723 | ||
724 | static struct miscdevice btrfs_misc = { | |
725 | .minor = MISC_DYNAMIC_MINOR, | |
726 | .name = "btrfs-control", | |
727 | .fops = &btrfs_ctl_fops | |
728 | }; | |
729 | ||
730 | static int btrfs_interface_init(void) | |
731 | { | |
732 | return misc_register(&btrfs_misc); | |
733 | } | |
734 | ||
b2950863 | 735 | static void btrfs_interface_exit(void) |
a9218f6b CM |
736 | { |
737 | if (misc_deregister(&btrfs_misc) < 0) | |
d397712b | 738 | printk(KERN_INFO "misc_deregister failed for control device"); |
a9218f6b CM |
739 | } |
740 | ||
2e635a27 CM |
741 | static int __init init_btrfs_fs(void) |
742 | { | |
2c90e5d6 | 743 | int err; |
58176a96 JB |
744 | |
745 | err = btrfs_init_sysfs(); | |
746 | if (err) | |
747 | return err; | |
748 | ||
39279cc3 | 749 | err = btrfs_init_cachep(); |
2c90e5d6 | 750 | if (err) |
a74a4b97 | 751 | goto free_sysfs; |
d1310b2e CM |
752 | |
753 | err = extent_io_init(); | |
2f4cbe64 WB |
754 | if (err) |
755 | goto free_cachep; | |
756 | ||
d1310b2e CM |
757 | err = extent_map_init(); |
758 | if (err) | |
759 | goto free_extent_io; | |
760 | ||
a9218f6b | 761 | err = btrfs_interface_init(); |
2f4cbe64 WB |
762 | if (err) |
763 | goto free_extent_map; | |
c8b97818 | 764 | |
a9218f6b CM |
765 | err = register_filesystem(&btrfs_fs_type); |
766 | if (err) | |
767 | goto unregister_ioctl; | |
b3c3da71 CM |
768 | |
769 | printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION); | |
2f4cbe64 WB |
770 | return 0; |
771 | ||
a9218f6b CM |
772 | unregister_ioctl: |
773 | btrfs_interface_exit(); | |
2f4cbe64 WB |
774 | free_extent_map: |
775 | extent_map_exit(); | |
d1310b2e CM |
776 | free_extent_io: |
777 | extent_io_exit(); | |
2f4cbe64 WB |
778 | free_cachep: |
779 | btrfs_destroy_cachep(); | |
a74a4b97 | 780 | free_sysfs: |
2f4cbe64 WB |
781 | btrfs_exit_sysfs(); |
782 | return err; | |
2e635a27 CM |
783 | } |
784 | ||
785 | static void __exit exit_btrfs_fs(void) | |
786 | { | |
39279cc3 | 787 | btrfs_destroy_cachep(); |
a52d9a80 | 788 | extent_map_exit(); |
d1310b2e | 789 | extent_io_exit(); |
a9218f6b | 790 | btrfs_interface_exit(); |
2e635a27 | 791 | unregister_filesystem(&btrfs_fs_type); |
58176a96 | 792 | btrfs_exit_sysfs(); |
8a4b83cc | 793 | btrfs_cleanup_fs_uuids(); |
c8b97818 | 794 | btrfs_zlib_exit(); |
2e635a27 CM |
795 | } |
796 | ||
797 | module_init(init_btrfs_fs) | |
798 | module_exit(exit_btrfs_fs) | |
799 | ||
800 | MODULE_LICENSE("GPL"); |