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