super.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. #include <linux/blkdev.h>
  19. #include <linux/module.h>
  20. #include <linux/buffer_head.h>
  21. #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/seq_file.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 <linux/magic.h>
  41. #include "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "xattr.h"
  49. #include "volumes.h"
  50. #include "version.h"
  51. #include "export.h"
  52. #include "compression.h"
  53. static struct super_operations btrfs_super_ops;
  54. static void btrfs_put_super(struct super_block *sb)
  55. {
  56. struct btrfs_root *root = btrfs_sb(sb);
  57. int ret;
  58. ret = close_ctree(root);
  59. sb->s_fs_info = NULL;
  60. }
  61. enum {
  62. Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow,
  63. Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
  64. Opt_ssd, Opt_thread_pool, Opt_noacl, Opt_compress, Opt_notreelog,
  65. Opt_ratio, Opt_flushoncommit, Opt_err,
  66. };
  67. static match_table_t tokens = {
  68. {Opt_degraded, "degraded"},
  69. {Opt_subvol, "subvol=%s"},
  70. {Opt_device, "device=%s"},
  71. {Opt_nodatasum, "nodatasum"},
  72. {Opt_nodatacow, "nodatacow"},
  73. {Opt_nobarrier, "nobarrier"},
  74. {Opt_max_extent, "max_extent=%s"},
  75. {Opt_max_inline, "max_inline=%s"},
  76. {Opt_alloc_start, "alloc_start=%s"},
  77. {Opt_thread_pool, "thread_pool=%d"},
  78. {Opt_compress, "compress"},
  79. {Opt_ssd, "ssd"},
  80. {Opt_noacl, "noacl"},
  81. {Opt_notreelog, "notreelog"},
  82. {Opt_flushoncommit, "flushoncommit"},
  83. {Opt_ratio, "metadata_ratio=%d"},
  84. {Opt_err, NULL},
  85. };
  86. u64 btrfs_parse_size(char *str)
  87. {
  88. u64 res;
  89. int mult = 1;
  90. char *end;
  91. char last;
  92. res = simple_strtoul(str, &end, 10);
  93. last = end[0];
  94. if (isalpha(last)) {
  95. last = tolower(last);
  96. switch (last) {
  97. case 'g':
  98. mult *= 1024;
  99. case 'm':
  100. mult *= 1024;
  101. case 'k':
  102. mult *= 1024;
  103. }
  104. res = res * mult;
  105. }
  106. return res;
  107. }
  108. /*
  109. * Regular mount options parser. Everything that is needed only when
  110. * reading in a new superblock is parsed here.
  111. */
  112. int btrfs_parse_options(struct btrfs_root *root, char *options)
  113. {
  114. struct btrfs_fs_info *info = root->fs_info;
  115. substring_t args[MAX_OPT_ARGS];
  116. char *p, *num;
  117. int intarg;
  118. if (!options)
  119. return 0;
  120. /*
  121. * strsep changes the string, duplicate it because parse_options
  122. * gets called twice
  123. */
  124. options = kstrdup(options, GFP_NOFS);
  125. if (!options)
  126. return -ENOMEM;
  127. while ((p = strsep(&options, ",")) != NULL) {
  128. int token;
  129. if (!*p)
  130. continue;
  131. token = match_token(p, tokens, args);
  132. switch (token) {
  133. case Opt_degraded:
  134. printk(KERN_INFO "btrfs: allowing degraded mounts\n");
  135. btrfs_set_opt(info->mount_opt, DEGRADED);
  136. break;
  137. case Opt_subvol:
  138. case Opt_device:
  139. /*
  140. * These are parsed by btrfs_parse_early_options
  141. * and can be happily ignored here.
  142. */
  143. break;
  144. case Opt_nodatasum:
  145. printk(KERN_INFO "btrfs: setting nodatacsum\n");
  146. btrfs_set_opt(info->mount_opt, NODATASUM);
  147. break;
  148. case Opt_nodatacow:
  149. printk(KERN_INFO "btrfs: setting nodatacow\n");
  150. btrfs_set_opt(info->mount_opt, NODATACOW);
  151. btrfs_set_opt(info->mount_opt, NODATASUM);
  152. break;
  153. case Opt_compress:
  154. printk(KERN_INFO "btrfs: use compression\n");
  155. btrfs_set_opt(info->mount_opt, COMPRESS);
  156. break;
  157. case Opt_ssd:
  158. printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
  159. btrfs_set_opt(info->mount_opt, SSD);
  160. break;
  161. case Opt_nobarrier:
  162. printk(KERN_INFO "btrfs: turning off barriers\n");
  163. btrfs_set_opt(info->mount_opt, NOBARRIER);
  164. break;
  165. case Opt_thread_pool:
  166. intarg = 0;
  167. match_int(&args[0], &intarg);
  168. if (intarg) {
  169. info->thread_pool_size = intarg;
  170. printk(KERN_INFO "btrfs: thread pool %d\n",
  171. info->thread_pool_size);
  172. }
  173. break;
  174. case Opt_max_extent:
  175. num = match_strdup(&args[0]);
  176. if (num) {
  177. info->max_extent = btrfs_parse_size(num);
  178. kfree(num);
  179. info->max_extent = max_t(u64,
  180. info->max_extent, root->sectorsize);
  181. printk(KERN_INFO "btrfs: max_extent at %llu\n",
  182. (unsigned long long)info->max_extent);
  183. }
  184. break;
  185. case Opt_max_inline:
  186. num = match_strdup(&args[0]);
  187. if (num) {
  188. info->max_inline = btrfs_parse_size(num);
  189. kfree(num);
  190. if (info->max_inline) {
  191. info->max_inline = max_t(u64,
  192. info->max_inline,
  193. root->sectorsize);
  194. }
  195. printk(KERN_INFO "btrfs: max_inline at %llu\n",
  196. (unsigned long long)info->max_inline);
  197. }
  198. break;
  199. case Opt_alloc_start:
  200. num = match_strdup(&args[0]);
  201. if (num) {
  202. info->alloc_start = btrfs_parse_size(num);
  203. kfree(num);
  204. printk(KERN_INFO
  205. "btrfs: allocations start at %llu\n",
  206. (unsigned long long)info->alloc_start);
  207. }
  208. break;
  209. case Opt_noacl:
  210. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  211. break;
  212. case Opt_notreelog:
  213. printk(KERN_INFO "btrfs: disabling tree log\n");
  214. btrfs_set_opt(info->mount_opt, NOTREELOG);
  215. break;
  216. case Opt_flushoncommit:
  217. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  218. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  219. break;
  220. case Opt_ratio:
  221. intarg = 0;
  222. match_int(&args[0], &intarg);
  223. if (intarg) {
  224. info->metadata_ratio = intarg;
  225. printk(KERN_INFO "btrfs: metadata ratio %d\n",
  226. info->metadata_ratio);
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. kfree(options);
  234. return 0;
  235. }
  236. /*
  237. * Parse mount options that are required early in the mount process.
  238. *
  239. * All other options will be parsed on much later in the mount process and
  240. * only when we need to allocate a new super block.
  241. */
  242. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  243. void *holder, char **subvol_name,
  244. struct btrfs_fs_devices **fs_devices)
  245. {
  246. substring_t args[MAX_OPT_ARGS];
  247. char *opts, *p;
  248. int error = 0;
  249. if (!options)
  250. goto out;
  251. /*
  252. * strsep changes the string, duplicate it because parse_options
  253. * gets called twice
  254. */
  255. opts = kstrdup(options, GFP_KERNEL);
  256. if (!opts)
  257. return -ENOMEM;
  258. while ((p = strsep(&opts, ",")) != NULL) {
  259. int token;
  260. if (!*p)
  261. continue;
  262. token = match_token(p, tokens, args);
  263. switch (token) {
  264. case Opt_subvol:
  265. *subvol_name = match_strdup(&args[0]);
  266. break;
  267. case Opt_device:
  268. error = btrfs_scan_one_device(match_strdup(&args[0]),
  269. flags, holder, fs_devices);
  270. if (error)
  271. goto out_free_opts;
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. out_free_opts:
  278. kfree(opts);
  279. out:
  280. /*
  281. * If no subvolume name is specified we use the default one. Allocate
  282. * a copy of the string "." here so that code later in the
  283. * mount path doesn't care if it's the default volume or another one.
  284. */
  285. if (!*subvol_name) {
  286. *subvol_name = kstrdup(".", GFP_KERNEL);
  287. if (!*subvol_name)
  288. return -ENOMEM;
  289. }
  290. return error;
  291. }
  292. static int btrfs_fill_super(struct super_block *sb,
  293. struct btrfs_fs_devices *fs_devices,
  294. void *data, int silent)
  295. {
  296. struct inode *inode;
  297. struct dentry *root_dentry;
  298. struct btrfs_super_block *disk_super;
  299. struct btrfs_root *tree_root;
  300. struct btrfs_inode *bi;
  301. int err;
  302. sb->s_maxbytes = MAX_LFS_FILESIZE;
  303. sb->s_magic = BTRFS_SUPER_MAGIC;
  304. sb->s_op = &btrfs_super_ops;
  305. sb->s_export_op = &btrfs_export_ops;
  306. sb->s_xattr = btrfs_xattr_handlers;
  307. sb->s_time_gran = 1;
  308. sb->s_flags |= MS_POSIXACL;
  309. tree_root = open_ctree(sb, fs_devices, (char *)data);
  310. if (IS_ERR(tree_root)) {
  311. printk("btrfs: open_ctree failed\n");
  312. return PTR_ERR(tree_root);
  313. }
  314. sb->s_fs_info = tree_root;
  315. disk_super = &tree_root->fs_info->super_copy;
  316. inode = btrfs_iget_locked(sb, BTRFS_FIRST_FREE_OBJECTID,
  317. tree_root->fs_info->fs_root);
  318. bi = BTRFS_I(inode);
  319. bi->location.objectid = inode->i_ino;
  320. bi->location.offset = 0;
  321. bi->root = tree_root->fs_info->fs_root;
  322. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  323. if (!inode) {
  324. err = -ENOMEM;
  325. goto fail_close;
  326. }
  327. if (inode->i_state & I_NEW) {
  328. btrfs_read_locked_inode(inode);
  329. unlock_new_inode(inode);
  330. }
  331. root_dentry = d_alloc_root(inode);
  332. if (!root_dentry) {
  333. iput(inode);
  334. err = -ENOMEM;
  335. goto fail_close;
  336. }
  337. #if 0
  338. /* this does the super kobj at the same time */
  339. err = btrfs_sysfs_add_super(tree_root->fs_info);
  340. if (err)
  341. goto fail_close;
  342. #endif
  343. sb->s_root = root_dentry;
  344. save_mount_options(sb, data);
  345. return 0;
  346. fail_close:
  347. close_ctree(tree_root);
  348. return err;
  349. }
  350. int btrfs_sync_fs(struct super_block *sb, int wait)
  351. {
  352. struct btrfs_trans_handle *trans;
  353. struct btrfs_root *root = btrfs_sb(sb);
  354. int ret;
  355. if (sb->s_flags & MS_RDONLY)
  356. return 0;
  357. sb->s_dirt = 0;
  358. if (!wait) {
  359. filemap_flush(root->fs_info->btree_inode->i_mapping);
  360. return 0;
  361. }
  362. btrfs_start_delalloc_inodes(root);
  363. btrfs_wait_ordered_extents(root, 0);
  364. trans = btrfs_start_transaction(root, 1);
  365. ret = btrfs_commit_transaction(trans, root);
  366. sb->s_dirt = 0;
  367. return ret;
  368. }
  369. static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  370. {
  371. struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
  372. struct btrfs_fs_info *info = root->fs_info;
  373. if (btrfs_test_opt(root, DEGRADED))
  374. seq_puts(seq, ",degraded");
  375. if (btrfs_test_opt(root, NODATASUM))
  376. seq_puts(seq, ",nodatasum");
  377. if (btrfs_test_opt(root, NODATACOW))
  378. seq_puts(seq, ",nodatacow");
  379. if (btrfs_test_opt(root, NOBARRIER))
  380. seq_puts(seq, ",nobarrier");
  381. if (info->max_extent != (u64)-1)
  382. seq_printf(seq, ",max_extent=%llu",
  383. (unsigned long long)info->max_extent);
  384. if (info->max_inline != 8192 * 1024)
  385. seq_printf(seq, ",max_inline=%llu",
  386. (unsigned long long)info->max_inline);
  387. if (info->alloc_start != 0)
  388. seq_printf(seq, ",alloc_start=%llu",
  389. (unsigned long long)info->alloc_start);
  390. if (info->thread_pool_size != min_t(unsigned long,
  391. num_online_cpus() + 2, 8))
  392. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  393. if (btrfs_test_opt(root, COMPRESS))
  394. seq_puts(seq, ",compress");
  395. if (btrfs_test_opt(root, SSD))
  396. seq_puts(seq, ",ssd");
  397. if (btrfs_test_opt(root, NOTREELOG))
  398. seq_puts(seq, ",notreelog");
  399. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  400. seq_puts(seq, ",flushoncommit");
  401. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  402. seq_puts(seq, ",noacl");
  403. return 0;
  404. }
  405. static void btrfs_write_super(struct super_block *sb)
  406. {
  407. sb->s_dirt = 0;
  408. }
  409. static int btrfs_test_super(struct super_block *s, void *data)
  410. {
  411. struct btrfs_fs_devices *test_fs_devices = data;
  412. struct btrfs_root *root = btrfs_sb(s);
  413. return root->fs_info->fs_devices == test_fs_devices;
  414. }
  415. /*
  416. * Find a superblock for the given device / mount point.
  417. *
  418. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  419. * for multiple device setup. Make sure to keep it in sync.
  420. */
  421. static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
  422. const char *dev_name, void *data, struct vfsmount *mnt)
  423. {
  424. char *subvol_name = NULL;
  425. struct block_device *bdev = NULL;
  426. struct super_block *s;
  427. struct dentry *root;
  428. struct btrfs_fs_devices *fs_devices = NULL;
  429. fmode_t mode = FMODE_READ;
  430. int error = 0;
  431. if (!(flags & MS_RDONLY))
  432. mode |= FMODE_WRITE;
  433. error = btrfs_parse_early_options(data, mode, fs_type,
  434. &subvol_name, &fs_devices);
  435. if (error)
  436. return error;
  437. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  438. if (error)
  439. goto error_free_subvol_name;
  440. error = btrfs_open_devices(fs_devices, mode, fs_type);
  441. if (error)
  442. goto error_free_subvol_name;
  443. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  444. error = -EACCES;
  445. goto error_close_devices;
  446. }
  447. bdev = fs_devices->latest_bdev;
  448. s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
  449. if (IS_ERR(s))
  450. goto error_s;
  451. if (s->s_root) {
  452. if ((flags ^ s->s_flags) & MS_RDONLY) {
  453. deactivate_locked_super(s);
  454. error = -EBUSY;
  455. goto error_close_devices;
  456. }
  457. btrfs_close_devices(fs_devices);
  458. } else {
  459. char b[BDEVNAME_SIZE];
  460. s->s_flags = flags;
  461. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  462. error = btrfs_fill_super(s, fs_devices, data,
  463. flags & MS_SILENT ? 1 : 0);
  464. if (error) {
  465. deactivate_locked_super(s);
  466. goto error_free_subvol_name;
  467. }
  468. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  469. s->s_flags |= MS_ACTIVE;
  470. }
  471. if (!strcmp(subvol_name, "."))
  472. root = dget(s->s_root);
  473. else {
  474. mutex_lock(&s->s_root->d_inode->i_mutex);
  475. root = lookup_one_len(subvol_name, s->s_root,
  476. strlen(subvol_name));
  477. mutex_unlock(&s->s_root->d_inode->i_mutex);
  478. if (IS_ERR(root)) {
  479. deactivate_locked_super(s);
  480. error = PTR_ERR(root);
  481. goto error_free_subvol_name;
  482. }
  483. if (!root->d_inode) {
  484. dput(root);
  485. deactivate_locked_super(s);
  486. error = -ENXIO;
  487. goto error_free_subvol_name;
  488. }
  489. }
  490. mnt->mnt_sb = s;
  491. mnt->mnt_root = root;
  492. kfree(subvol_name);
  493. return 0;
  494. error_s:
  495. error = PTR_ERR(s);
  496. error_close_devices:
  497. btrfs_close_devices(fs_devices);
  498. error_free_subvol_name:
  499. kfree(subvol_name);
  500. return error;
  501. }
  502. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  503. {
  504. struct btrfs_root *root = btrfs_sb(sb);
  505. int ret;
  506. ret = btrfs_parse_options(root, data);
  507. if (ret)
  508. return -EINVAL;
  509. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  510. return 0;
  511. if (*flags & MS_RDONLY) {
  512. sb->s_flags |= MS_RDONLY;
  513. ret = btrfs_commit_super(root);
  514. WARN_ON(ret);
  515. } else {
  516. if (root->fs_info->fs_devices->rw_devices == 0)
  517. return -EACCES;
  518. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  519. return -EINVAL;
  520. ret = btrfs_cleanup_reloc_trees(root);
  521. WARN_ON(ret);
  522. ret = btrfs_cleanup_fs_roots(root->fs_info);
  523. WARN_ON(ret);
  524. sb->s_flags &= ~MS_RDONLY;
  525. }
  526. return 0;
  527. }
  528. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  529. {
  530. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  531. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  532. int bits = dentry->d_sb->s_blocksize_bits;
  533. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  534. buf->f_namelen = BTRFS_NAME_LEN;
  535. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  536. buf->f_bfree = buf->f_blocks -
  537. (btrfs_super_bytes_used(disk_super) >> bits);
  538. buf->f_bavail = buf->f_bfree;
  539. buf->f_bsize = dentry->d_sb->s_blocksize;
  540. buf->f_type = BTRFS_SUPER_MAGIC;
  541. /* We treat it as constant endianness (it doesn't matter _which_)
  542. because we want the fsid to come out the same whether mounted
  543. on a big-endian or little-endian host */
  544. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  545. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  546. /* Mask in the root object ID too, to disambiguate subvols */
  547. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  548. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  549. return 0;
  550. }
  551. static struct file_system_type btrfs_fs_type = {
  552. .owner = THIS_MODULE,
  553. .name = "btrfs",
  554. .get_sb = btrfs_get_sb,
  555. .kill_sb = kill_anon_super,
  556. .fs_flags = FS_REQUIRES_DEV,
  557. };
  558. /*
  559. * used by btrfsctl to scan devices when no FS is mounted
  560. */
  561. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  562. unsigned long arg)
  563. {
  564. struct btrfs_ioctl_vol_args *vol;
  565. struct btrfs_fs_devices *fs_devices;
  566. int ret = -ENOTTY;
  567. if (!capable(CAP_SYS_ADMIN))
  568. return -EPERM;
  569. vol = memdup_user((void __user *)arg, sizeof(*vol));
  570. if (IS_ERR(vol))
  571. return PTR_ERR(vol);
  572. switch (cmd) {
  573. case BTRFS_IOC_SCAN_DEV:
  574. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  575. &btrfs_fs_type, &fs_devices);
  576. break;
  577. }
  578. kfree(vol);
  579. return ret;
  580. }
  581. static int btrfs_freeze(struct super_block *sb)
  582. {
  583. struct btrfs_root *root = btrfs_sb(sb);
  584. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  585. mutex_lock(&root->fs_info->cleaner_mutex);
  586. return 0;
  587. }
  588. static int btrfs_unfreeze(struct super_block *sb)
  589. {
  590. struct btrfs_root *root = btrfs_sb(sb);
  591. mutex_unlock(&root->fs_info->cleaner_mutex);
  592. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  593. return 0;
  594. }
  595. static struct super_operations btrfs_super_ops = {
  596. .delete_inode = btrfs_delete_inode,
  597. .put_super = btrfs_put_super,
  598. .write_super = btrfs_write_super,
  599. .sync_fs = btrfs_sync_fs,
  600. .show_options = btrfs_show_options,
  601. .write_inode = btrfs_write_inode,
  602. .dirty_inode = btrfs_dirty_inode,
  603. .alloc_inode = btrfs_alloc_inode,
  604. .destroy_inode = btrfs_destroy_inode,
  605. .statfs = btrfs_statfs,
  606. .remount_fs = btrfs_remount,
  607. .freeze_fs = btrfs_freeze,
  608. .unfreeze_fs = btrfs_unfreeze,
  609. };
  610. static const struct file_operations btrfs_ctl_fops = {
  611. .unlocked_ioctl = btrfs_control_ioctl,
  612. .compat_ioctl = btrfs_control_ioctl,
  613. .owner = THIS_MODULE,
  614. };
  615. static struct miscdevice btrfs_misc = {
  616. .minor = MISC_DYNAMIC_MINOR,
  617. .name = "btrfs-control",
  618. .fops = &btrfs_ctl_fops
  619. };
  620. static int btrfs_interface_init(void)
  621. {
  622. return misc_register(&btrfs_misc);
  623. }
  624. static void btrfs_interface_exit(void)
  625. {
  626. if (misc_deregister(&btrfs_misc) < 0)
  627. printk(KERN_INFO "misc_deregister failed for control device");
  628. }
  629. static int __init init_btrfs_fs(void)
  630. {
  631. int err;
  632. err = btrfs_init_sysfs();
  633. if (err)
  634. return err;
  635. err = btrfs_init_cachep();
  636. if (err)
  637. goto free_sysfs;
  638. err = extent_io_init();
  639. if (err)
  640. goto free_cachep;
  641. err = extent_map_init();
  642. if (err)
  643. goto free_extent_io;
  644. err = btrfs_interface_init();
  645. if (err)
  646. goto free_extent_map;
  647. err = register_filesystem(&btrfs_fs_type);
  648. if (err)
  649. goto unregister_ioctl;
  650. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  651. return 0;
  652. unregister_ioctl:
  653. btrfs_interface_exit();
  654. free_extent_map:
  655. extent_map_exit();
  656. free_extent_io:
  657. extent_io_exit();
  658. free_cachep:
  659. btrfs_destroy_cachep();
  660. free_sysfs:
  661. btrfs_exit_sysfs();
  662. return err;
  663. }
  664. static void __exit exit_btrfs_fs(void)
  665. {
  666. btrfs_destroy_cachep();
  667. extent_map_exit();
  668. extent_io_exit();
  669. btrfs_interface_exit();
  670. unregister_filesystem(&btrfs_fs_type);
  671. btrfs_exit_sysfs();
  672. btrfs_cleanup_fs_uuids();
  673. btrfs_zlib_exit();
  674. }
  675. module_init(init_btrfs_fs)
  676. module_exit(exit_btrfs_fs)
  677. MODULE_LICENSE("GPL");