super.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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/backing-dev.h>
  29. #include <linux/mount.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/parser.h>
  36. #include <linux/ctype.h>
  37. #include <linux/namei.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/magic.h>
  40. #include <linux/slab.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 const 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. (void)ret; /* FIXME: need to fix VFS to return error? */
  61. }
  62. enum {
  63. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  64. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  65. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  66. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  67. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  68. Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, Opt_err,
  69. };
  70. static match_table_t tokens = {
  71. {Opt_degraded, "degraded"},
  72. {Opt_subvol, "subvol=%s"},
  73. {Opt_subvolid, "subvolid=%d"},
  74. {Opt_device, "device=%s"},
  75. {Opt_nodatasum, "nodatasum"},
  76. {Opt_nodatacow, "nodatacow"},
  77. {Opt_nobarrier, "nobarrier"},
  78. {Opt_max_inline, "max_inline=%s"},
  79. {Opt_alloc_start, "alloc_start=%s"},
  80. {Opt_thread_pool, "thread_pool=%d"},
  81. {Opt_compress, "compress"},
  82. {Opt_compress_type, "compress=%s"},
  83. {Opt_compress_force, "compress-force"},
  84. {Opt_compress_force_type, "compress-force=%s"},
  85. {Opt_ssd, "ssd"},
  86. {Opt_ssd_spread, "ssd_spread"},
  87. {Opt_nossd, "nossd"},
  88. {Opt_noacl, "noacl"},
  89. {Opt_notreelog, "notreelog"},
  90. {Opt_flushoncommit, "flushoncommit"},
  91. {Opt_ratio, "metadata_ratio=%d"},
  92. {Opt_discard, "discard"},
  93. {Opt_space_cache, "space_cache"},
  94. {Opt_clear_cache, "clear_cache"},
  95. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  96. {Opt_err, NULL},
  97. };
  98. /*
  99. * Regular mount options parser. Everything that is needed only when
  100. * reading in a new superblock is parsed here.
  101. */
  102. int btrfs_parse_options(struct btrfs_root *root, char *options)
  103. {
  104. struct btrfs_fs_info *info = root->fs_info;
  105. substring_t args[MAX_OPT_ARGS];
  106. char *p, *num, *orig;
  107. int intarg;
  108. int ret = 0;
  109. char *compress_type;
  110. bool compress_force = false;
  111. if (!options)
  112. return 0;
  113. /*
  114. * strsep changes the string, duplicate it because parse_options
  115. * gets called twice
  116. */
  117. options = kstrdup(options, GFP_NOFS);
  118. if (!options)
  119. return -ENOMEM;
  120. orig = options;
  121. while ((p = strsep(&options, ",")) != NULL) {
  122. int token;
  123. if (!*p)
  124. continue;
  125. token = match_token(p, tokens, args);
  126. switch (token) {
  127. case Opt_degraded:
  128. printk(KERN_INFO "btrfs: allowing degraded mounts\n");
  129. btrfs_set_opt(info->mount_opt, DEGRADED);
  130. break;
  131. case Opt_subvol:
  132. case Opt_subvolid:
  133. case Opt_device:
  134. /*
  135. * These are parsed by btrfs_parse_early_options
  136. * and can be happily ignored here.
  137. */
  138. break;
  139. case Opt_nodatasum:
  140. printk(KERN_INFO "btrfs: setting nodatasum\n");
  141. btrfs_set_opt(info->mount_opt, NODATASUM);
  142. break;
  143. case Opt_nodatacow:
  144. printk(KERN_INFO "btrfs: setting nodatacow\n");
  145. btrfs_set_opt(info->mount_opt, NODATACOW);
  146. btrfs_set_opt(info->mount_opt, NODATASUM);
  147. break;
  148. case Opt_compress_force:
  149. case Opt_compress_force_type:
  150. compress_force = true;
  151. case Opt_compress:
  152. case Opt_compress_type:
  153. if (token == Opt_compress ||
  154. token == Opt_compress_force ||
  155. strcmp(args[0].from, "zlib") == 0) {
  156. compress_type = "zlib";
  157. info->compress_type = BTRFS_COMPRESS_ZLIB;
  158. } else if (strcmp(args[0].from, "lzo") == 0) {
  159. compress_type = "lzo";
  160. info->compress_type = BTRFS_COMPRESS_LZO;
  161. } else {
  162. ret = -EINVAL;
  163. goto out;
  164. }
  165. btrfs_set_opt(info->mount_opt, COMPRESS);
  166. if (compress_force) {
  167. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  168. pr_info("btrfs: force %s compression\n",
  169. compress_type);
  170. } else
  171. pr_info("btrfs: use %s compression\n",
  172. compress_type);
  173. break;
  174. case Opt_ssd:
  175. printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
  176. btrfs_set_opt(info->mount_opt, SSD);
  177. break;
  178. case Opt_ssd_spread:
  179. printk(KERN_INFO "btrfs: use spread ssd "
  180. "allocation scheme\n");
  181. btrfs_set_opt(info->mount_opt, SSD);
  182. btrfs_set_opt(info->mount_opt, SSD_SPREAD);
  183. break;
  184. case Opt_nossd:
  185. printk(KERN_INFO "btrfs: not using ssd allocation "
  186. "scheme\n");
  187. btrfs_set_opt(info->mount_opt, NOSSD);
  188. btrfs_clear_opt(info->mount_opt, SSD);
  189. btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
  190. break;
  191. case Opt_nobarrier:
  192. printk(KERN_INFO "btrfs: turning off barriers\n");
  193. btrfs_set_opt(info->mount_opt, NOBARRIER);
  194. break;
  195. case Opt_thread_pool:
  196. intarg = 0;
  197. match_int(&args[0], &intarg);
  198. if (intarg) {
  199. info->thread_pool_size = intarg;
  200. printk(KERN_INFO "btrfs: thread pool %d\n",
  201. info->thread_pool_size);
  202. }
  203. break;
  204. case Opt_max_inline:
  205. num = match_strdup(&args[0]);
  206. if (num) {
  207. info->max_inline = memparse(num, NULL);
  208. kfree(num);
  209. if (info->max_inline) {
  210. info->max_inline = max_t(u64,
  211. info->max_inline,
  212. root->sectorsize);
  213. }
  214. printk(KERN_INFO "btrfs: max_inline at %llu\n",
  215. (unsigned long long)info->max_inline);
  216. }
  217. break;
  218. case Opt_alloc_start:
  219. num = match_strdup(&args[0]);
  220. if (num) {
  221. info->alloc_start = memparse(num, NULL);
  222. kfree(num);
  223. printk(KERN_INFO
  224. "btrfs: allocations start at %llu\n",
  225. (unsigned long long)info->alloc_start);
  226. }
  227. break;
  228. case Opt_noacl:
  229. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  230. break;
  231. case Opt_notreelog:
  232. printk(KERN_INFO "btrfs: disabling tree log\n");
  233. btrfs_set_opt(info->mount_opt, NOTREELOG);
  234. break;
  235. case Opt_flushoncommit:
  236. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  237. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  238. break;
  239. case Opt_ratio:
  240. intarg = 0;
  241. match_int(&args[0], &intarg);
  242. if (intarg) {
  243. info->metadata_ratio = intarg;
  244. printk(KERN_INFO "btrfs: metadata ratio %d\n",
  245. info->metadata_ratio);
  246. }
  247. break;
  248. case Opt_discard:
  249. btrfs_set_opt(info->mount_opt, DISCARD);
  250. break;
  251. case Opt_space_cache:
  252. printk(KERN_INFO "btrfs: enabling disk space caching\n");
  253. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  254. break;
  255. case Opt_clear_cache:
  256. printk(KERN_INFO "btrfs: force clearing of disk cache\n");
  257. btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
  258. break;
  259. case Opt_user_subvol_rm_allowed:
  260. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  261. break;
  262. case Opt_err:
  263. printk(KERN_INFO "btrfs: unrecognized mount option "
  264. "'%s'\n", p);
  265. ret = -EINVAL;
  266. goto out;
  267. default:
  268. break;
  269. }
  270. }
  271. out:
  272. kfree(orig);
  273. return ret;
  274. }
  275. /*
  276. * Parse mount options that are required early in the mount process.
  277. *
  278. * All other options will be parsed on much later in the mount process and
  279. * only when we need to allocate a new super block.
  280. */
  281. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  282. void *holder, char **subvol_name, u64 *subvol_objectid,
  283. struct btrfs_fs_devices **fs_devices)
  284. {
  285. substring_t args[MAX_OPT_ARGS];
  286. char *opts, *p;
  287. int error = 0;
  288. int intarg;
  289. if (!options)
  290. goto out;
  291. /*
  292. * strsep changes the string, duplicate it because parse_options
  293. * gets called twice
  294. */
  295. opts = kstrdup(options, GFP_KERNEL);
  296. if (!opts)
  297. return -ENOMEM;
  298. while ((p = strsep(&opts, ",")) != NULL) {
  299. int token;
  300. if (!*p)
  301. continue;
  302. token = match_token(p, tokens, args);
  303. switch (token) {
  304. case Opt_subvol:
  305. *subvol_name = match_strdup(&args[0]);
  306. break;
  307. case Opt_subvolid:
  308. intarg = 0;
  309. error = match_int(&args[0], &intarg);
  310. if (!error) {
  311. /* we want the original fs_tree */
  312. if (!intarg)
  313. *subvol_objectid =
  314. BTRFS_FS_TREE_OBJECTID;
  315. else
  316. *subvol_objectid = intarg;
  317. }
  318. break;
  319. case Opt_device:
  320. error = btrfs_scan_one_device(match_strdup(&args[0]),
  321. flags, holder, fs_devices);
  322. if (error)
  323. goto out_free_opts;
  324. break;
  325. default:
  326. break;
  327. }
  328. }
  329. out_free_opts:
  330. kfree(opts);
  331. out:
  332. /*
  333. * If no subvolume name is specified we use the default one. Allocate
  334. * a copy of the string "." here so that code later in the
  335. * mount path doesn't care if it's the default volume or another one.
  336. */
  337. if (!*subvol_name) {
  338. *subvol_name = kstrdup(".", GFP_KERNEL);
  339. if (!*subvol_name)
  340. return -ENOMEM;
  341. }
  342. return error;
  343. }
  344. static struct dentry *get_default_root(struct super_block *sb,
  345. u64 subvol_objectid)
  346. {
  347. struct btrfs_root *root = sb->s_fs_info;
  348. struct btrfs_root *new_root;
  349. struct btrfs_dir_item *di;
  350. struct btrfs_path *path;
  351. struct btrfs_key location;
  352. struct inode *inode;
  353. struct dentry *dentry;
  354. u64 dir_id;
  355. int new = 0;
  356. /*
  357. * We have a specific subvol we want to mount, just setup location and
  358. * go look up the root.
  359. */
  360. if (subvol_objectid) {
  361. location.objectid = subvol_objectid;
  362. location.type = BTRFS_ROOT_ITEM_KEY;
  363. location.offset = (u64)-1;
  364. goto find_root;
  365. }
  366. path = btrfs_alloc_path();
  367. if (!path)
  368. return ERR_PTR(-ENOMEM);
  369. path->leave_spinning = 1;
  370. /*
  371. * Find the "default" dir item which points to the root item that we
  372. * will mount by default if we haven't been given a specific subvolume
  373. * to mount.
  374. */
  375. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  376. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  377. if (IS_ERR(di))
  378. return ERR_CAST(di);
  379. if (!di) {
  380. /*
  381. * Ok the default dir item isn't there. This is weird since
  382. * it's always been there, but don't freak out, just try and
  383. * mount to root most subvolume.
  384. */
  385. btrfs_free_path(path);
  386. dir_id = BTRFS_FIRST_FREE_OBJECTID;
  387. new_root = root->fs_info->fs_root;
  388. goto setup_root;
  389. }
  390. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  391. btrfs_free_path(path);
  392. find_root:
  393. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  394. if (IS_ERR(new_root))
  395. return ERR_CAST(new_root);
  396. if (btrfs_root_refs(&new_root->root_item) == 0)
  397. return ERR_PTR(-ENOENT);
  398. dir_id = btrfs_root_dirid(&new_root->root_item);
  399. setup_root:
  400. location.objectid = dir_id;
  401. location.type = BTRFS_INODE_ITEM_KEY;
  402. location.offset = 0;
  403. inode = btrfs_iget(sb, &location, new_root, &new);
  404. if (IS_ERR(inode))
  405. return ERR_CAST(inode);
  406. /*
  407. * If we're just mounting the root most subvol put the inode and return
  408. * a reference to the dentry. We will have already gotten a reference
  409. * to the inode in btrfs_fill_super so we're good to go.
  410. */
  411. if (!new && sb->s_root->d_inode == inode) {
  412. iput(inode);
  413. return dget(sb->s_root);
  414. }
  415. if (new) {
  416. const struct qstr name = { .name = "/", .len = 1 };
  417. /*
  418. * New inode, we need to make the dentry a sibling of s_root so
  419. * everything gets cleaned up properly on unmount.
  420. */
  421. dentry = d_alloc(sb->s_root, &name);
  422. if (!dentry) {
  423. iput(inode);
  424. return ERR_PTR(-ENOMEM);
  425. }
  426. d_splice_alias(inode, dentry);
  427. } else {
  428. /*
  429. * We found the inode in cache, just find a dentry for it and
  430. * put the reference to the inode we just got.
  431. */
  432. dentry = d_find_alias(inode);
  433. iput(inode);
  434. }
  435. return dentry;
  436. }
  437. static int btrfs_fill_super(struct super_block *sb,
  438. struct btrfs_fs_devices *fs_devices,
  439. void *data, int silent)
  440. {
  441. struct inode *inode;
  442. struct dentry *root_dentry;
  443. struct btrfs_root *tree_root;
  444. struct btrfs_key key;
  445. int err;
  446. sb->s_maxbytes = MAX_LFS_FILESIZE;
  447. sb->s_magic = BTRFS_SUPER_MAGIC;
  448. sb->s_op = &btrfs_super_ops;
  449. sb->s_export_op = &btrfs_export_ops;
  450. sb->s_xattr = btrfs_xattr_handlers;
  451. sb->s_time_gran = 1;
  452. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  453. sb->s_flags |= MS_POSIXACL;
  454. #endif
  455. tree_root = open_ctree(sb, fs_devices, (char *)data);
  456. if (IS_ERR(tree_root)) {
  457. printk("btrfs: open_ctree failed\n");
  458. return PTR_ERR(tree_root);
  459. }
  460. sb->s_fs_info = tree_root;
  461. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  462. key.type = BTRFS_INODE_ITEM_KEY;
  463. key.offset = 0;
  464. inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
  465. if (IS_ERR(inode)) {
  466. err = PTR_ERR(inode);
  467. goto fail_close;
  468. }
  469. root_dentry = d_alloc_root(inode);
  470. if (!root_dentry) {
  471. iput(inode);
  472. err = -ENOMEM;
  473. goto fail_close;
  474. }
  475. sb->s_root = root_dentry;
  476. save_mount_options(sb, data);
  477. return 0;
  478. fail_close:
  479. close_ctree(tree_root);
  480. return err;
  481. }
  482. int btrfs_sync_fs(struct super_block *sb, int wait)
  483. {
  484. struct btrfs_trans_handle *trans;
  485. struct btrfs_root *root = btrfs_sb(sb);
  486. int ret;
  487. if (!wait) {
  488. filemap_flush(root->fs_info->btree_inode->i_mapping);
  489. return 0;
  490. }
  491. btrfs_start_delalloc_inodes(root, 0);
  492. btrfs_wait_ordered_extents(root, 0, 0);
  493. trans = btrfs_start_transaction(root, 0);
  494. ret = btrfs_commit_transaction(trans, root);
  495. return ret;
  496. }
  497. static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  498. {
  499. struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
  500. struct btrfs_fs_info *info = root->fs_info;
  501. if (btrfs_test_opt(root, DEGRADED))
  502. seq_puts(seq, ",degraded");
  503. if (btrfs_test_opt(root, NODATASUM))
  504. seq_puts(seq, ",nodatasum");
  505. if (btrfs_test_opt(root, NODATACOW))
  506. seq_puts(seq, ",nodatacow");
  507. if (btrfs_test_opt(root, NOBARRIER))
  508. seq_puts(seq, ",nobarrier");
  509. if (info->max_inline != 8192 * 1024)
  510. seq_printf(seq, ",max_inline=%llu",
  511. (unsigned long long)info->max_inline);
  512. if (info->alloc_start != 0)
  513. seq_printf(seq, ",alloc_start=%llu",
  514. (unsigned long long)info->alloc_start);
  515. if (info->thread_pool_size != min_t(unsigned long,
  516. num_online_cpus() + 2, 8))
  517. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  518. if (btrfs_test_opt(root, COMPRESS))
  519. seq_puts(seq, ",compress");
  520. if (btrfs_test_opt(root, NOSSD))
  521. seq_puts(seq, ",nossd");
  522. if (btrfs_test_opt(root, SSD_SPREAD))
  523. seq_puts(seq, ",ssd_spread");
  524. else if (btrfs_test_opt(root, SSD))
  525. seq_puts(seq, ",ssd");
  526. if (btrfs_test_opt(root, NOTREELOG))
  527. seq_puts(seq, ",notreelog");
  528. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  529. seq_puts(seq, ",flushoncommit");
  530. if (btrfs_test_opt(root, DISCARD))
  531. seq_puts(seq, ",discard");
  532. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  533. seq_puts(seq, ",noacl");
  534. return 0;
  535. }
  536. static int btrfs_test_super(struct super_block *s, void *data)
  537. {
  538. struct btrfs_root *test_root = data;
  539. struct btrfs_root *root = btrfs_sb(s);
  540. /*
  541. * If this super block is going away, return false as it
  542. * can't match as an existing super block.
  543. */
  544. if (!atomic_read(&s->s_active))
  545. return 0;
  546. return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
  547. }
  548. static int btrfs_set_super(struct super_block *s, void *data)
  549. {
  550. s->s_fs_info = data;
  551. return set_anon_super(s, data);
  552. }
  553. /*
  554. * Find a superblock for the given device / mount point.
  555. *
  556. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  557. * for multiple device setup. Make sure to keep it in sync.
  558. */
  559. static int btrfs_get_sb(struct file_system_type *fs_type, int flags,
  560. const char *dev_name, void *data, struct vfsmount *mnt)
  561. {
  562. struct block_device *bdev = NULL;
  563. struct super_block *s;
  564. struct dentry *root;
  565. struct btrfs_fs_devices *fs_devices = NULL;
  566. struct btrfs_root *tree_root = NULL;
  567. struct btrfs_fs_info *fs_info = NULL;
  568. fmode_t mode = FMODE_READ;
  569. char *subvol_name = NULL;
  570. u64 subvol_objectid = 0;
  571. int error = 0;
  572. if (!(flags & MS_RDONLY))
  573. mode |= FMODE_WRITE;
  574. error = btrfs_parse_early_options(data, mode, fs_type,
  575. &subvol_name, &subvol_objectid,
  576. &fs_devices);
  577. if (error)
  578. return error;
  579. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  580. if (error)
  581. goto error_free_subvol_name;
  582. error = btrfs_open_devices(fs_devices, mode, fs_type);
  583. if (error)
  584. goto error_free_subvol_name;
  585. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  586. error = -EACCES;
  587. goto error_close_devices;
  588. }
  589. /*
  590. * Setup a dummy root and fs_info for test/set super. This is because
  591. * we don't actually fill this stuff out until open_ctree, but we need
  592. * it for searching for existing supers, so this lets us do that and
  593. * then open_ctree will properly initialize everything later.
  594. */
  595. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  596. tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
  597. if (!fs_info || !tree_root) {
  598. error = -ENOMEM;
  599. goto error_close_devices;
  600. }
  601. fs_info->tree_root = tree_root;
  602. fs_info->fs_devices = fs_devices;
  603. tree_root->fs_info = fs_info;
  604. bdev = fs_devices->latest_bdev;
  605. s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
  606. if (IS_ERR(s))
  607. goto error_s;
  608. if (s->s_root) {
  609. if ((flags ^ s->s_flags) & MS_RDONLY) {
  610. deactivate_locked_super(s);
  611. error = -EBUSY;
  612. goto error_close_devices;
  613. }
  614. btrfs_close_devices(fs_devices);
  615. } else {
  616. char b[BDEVNAME_SIZE];
  617. s->s_flags = flags;
  618. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  619. error = btrfs_fill_super(s, fs_devices, data,
  620. flags & MS_SILENT ? 1 : 0);
  621. if (error) {
  622. deactivate_locked_super(s);
  623. goto error_free_subvol_name;
  624. }
  625. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  626. s->s_flags |= MS_ACTIVE;
  627. }
  628. root = get_default_root(s, subvol_objectid);
  629. if (IS_ERR(root)) {
  630. error = PTR_ERR(root);
  631. deactivate_locked_super(s);
  632. goto error_free_subvol_name;
  633. }
  634. /* if they gave us a subvolume name bind mount into that */
  635. if (strcmp(subvol_name, ".")) {
  636. struct dentry *new_root;
  637. mutex_lock(&root->d_inode->i_mutex);
  638. new_root = lookup_one_len(subvol_name, root,
  639. strlen(subvol_name));
  640. mutex_unlock(&root->d_inode->i_mutex);
  641. if (IS_ERR(new_root)) {
  642. dput(root);
  643. deactivate_locked_super(s);
  644. error = PTR_ERR(new_root);
  645. goto error_free_subvol_name;
  646. }
  647. if (!new_root->d_inode) {
  648. dput(root);
  649. dput(new_root);
  650. deactivate_locked_super(s);
  651. error = -ENXIO;
  652. goto error_free_subvol_name;
  653. }
  654. dput(root);
  655. root = new_root;
  656. }
  657. mnt->mnt_sb = s;
  658. mnt->mnt_root = root;
  659. kfree(subvol_name);
  660. return 0;
  661. error_s:
  662. error = PTR_ERR(s);
  663. error_close_devices:
  664. btrfs_close_devices(fs_devices);
  665. kfree(fs_info);
  666. kfree(tree_root);
  667. error_free_subvol_name:
  668. kfree(subvol_name);
  669. return error;
  670. }
  671. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  672. {
  673. struct btrfs_root *root = btrfs_sb(sb);
  674. int ret;
  675. ret = btrfs_parse_options(root, data);
  676. if (ret)
  677. return -EINVAL;
  678. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  679. return 0;
  680. if (*flags & MS_RDONLY) {
  681. sb->s_flags |= MS_RDONLY;
  682. ret = btrfs_commit_super(root);
  683. WARN_ON(ret);
  684. } else {
  685. if (root->fs_info->fs_devices->rw_devices == 0)
  686. return -EACCES;
  687. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  688. return -EINVAL;
  689. ret = btrfs_cleanup_fs_roots(root->fs_info);
  690. WARN_ON(ret);
  691. /* recover relocation */
  692. ret = btrfs_recover_relocation(root);
  693. WARN_ON(ret);
  694. sb->s_flags &= ~MS_RDONLY;
  695. }
  696. return 0;
  697. }
  698. /*
  699. * The helper to calc the free space on the devices that can be used to store
  700. * file data.
  701. */
  702. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  703. {
  704. struct btrfs_fs_info *fs_info = root->fs_info;
  705. struct btrfs_device_info *devices_info;
  706. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  707. struct btrfs_device *device;
  708. u64 skip_space;
  709. u64 type;
  710. u64 avail_space;
  711. u64 used_space;
  712. u64 min_stripe_size;
  713. int min_stripes = 1;
  714. int i = 0, nr_devices;
  715. int ret;
  716. nr_devices = fs_info->fs_devices->rw_devices;
  717. BUG_ON(!nr_devices);
  718. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  719. GFP_NOFS);
  720. if (!devices_info)
  721. return -ENOMEM;
  722. /* calc min stripe number for data space alloction */
  723. type = btrfs_get_alloc_profile(root, 1);
  724. if (type & BTRFS_BLOCK_GROUP_RAID0)
  725. min_stripes = 2;
  726. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  727. min_stripes = 2;
  728. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  729. min_stripes = 4;
  730. if (type & BTRFS_BLOCK_GROUP_DUP)
  731. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  732. else
  733. min_stripe_size = BTRFS_STRIPE_LEN;
  734. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  735. if (!device->in_fs_metadata)
  736. continue;
  737. avail_space = device->total_bytes - device->bytes_used;
  738. /* align with stripe_len */
  739. do_div(avail_space, BTRFS_STRIPE_LEN);
  740. avail_space *= BTRFS_STRIPE_LEN;
  741. /*
  742. * In order to avoid overwritting the superblock on the drive,
  743. * btrfs starts at an offset of at least 1MB when doing chunk
  744. * allocation.
  745. */
  746. skip_space = 1024 * 1024;
  747. /* user can set the offset in fs_info->alloc_start. */
  748. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  749. device->total_bytes)
  750. skip_space = max(fs_info->alloc_start, skip_space);
  751. /*
  752. * btrfs can not use the free space in [0, skip_space - 1],
  753. * we must subtract it from the total. In order to implement
  754. * it, we account the used space in this range first.
  755. */
  756. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  757. &used_space);
  758. if (ret) {
  759. kfree(devices_info);
  760. return ret;
  761. }
  762. /* calc the free space in [0, skip_space - 1] */
  763. skip_space -= used_space;
  764. /*
  765. * we can use the free space in [0, skip_space - 1], subtract
  766. * it from the total.
  767. */
  768. if (avail_space && avail_space >= skip_space)
  769. avail_space -= skip_space;
  770. else
  771. avail_space = 0;
  772. if (avail_space < min_stripe_size)
  773. continue;
  774. devices_info[i].dev = device;
  775. devices_info[i].max_avail = avail_space;
  776. i++;
  777. }
  778. nr_devices = i;
  779. btrfs_descending_sort_devices(devices_info, nr_devices);
  780. i = nr_devices - 1;
  781. avail_space = 0;
  782. while (nr_devices >= min_stripes) {
  783. if (devices_info[i].max_avail >= min_stripe_size) {
  784. int j;
  785. u64 alloc_size;
  786. avail_space += devices_info[i].max_avail * min_stripes;
  787. alloc_size = devices_info[i].max_avail;
  788. for (j = i + 1 - min_stripes; j <= i; j++)
  789. devices_info[j].max_avail -= alloc_size;
  790. }
  791. i--;
  792. nr_devices--;
  793. }
  794. kfree(devices_info);
  795. *free_bytes = avail_space;
  796. return 0;
  797. }
  798. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  799. {
  800. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  801. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  802. struct list_head *head = &root->fs_info->space_info;
  803. struct btrfs_space_info *found;
  804. u64 total_used = 0;
  805. u64 total_free_data = 0;
  806. int bits = dentry->d_sb->s_blocksize_bits;
  807. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  808. int ret;
  809. /* holding chunk_muext to avoid allocating new chunks */
  810. mutex_lock(&root->fs_info->chunk_mutex);
  811. rcu_read_lock();
  812. list_for_each_entry_rcu(found, head, list) {
  813. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  814. total_free_data += found->disk_total - found->disk_used;
  815. total_free_data -=
  816. btrfs_account_ro_block_groups_free_space(found);
  817. }
  818. total_used += found->disk_used;
  819. }
  820. rcu_read_unlock();
  821. buf->f_namelen = BTRFS_NAME_LEN;
  822. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  823. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  824. buf->f_bsize = dentry->d_sb->s_blocksize;
  825. buf->f_type = BTRFS_SUPER_MAGIC;
  826. buf->f_bavail = total_free_data;
  827. ret = btrfs_calc_avail_data_space(root, &total_free_data);
  828. if (ret) {
  829. mutex_unlock(&root->fs_info->chunk_mutex);
  830. return ret;
  831. }
  832. buf->f_bavail += total_free_data;
  833. buf->f_bavail = buf->f_bavail >> bits;
  834. mutex_unlock(&root->fs_info->chunk_mutex);
  835. /* We treat it as constant endianness (it doesn't matter _which_)
  836. because we want the fsid to come out the same whether mounted
  837. on a big-endian or little-endian host */
  838. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  839. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  840. /* Mask in the root object ID too, to disambiguate subvols */
  841. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  842. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  843. return 0;
  844. }
  845. static struct file_system_type btrfs_fs_type = {
  846. .owner = THIS_MODULE,
  847. .name = "btrfs",
  848. .get_sb = btrfs_get_sb,
  849. .kill_sb = kill_anon_super,
  850. .fs_flags = FS_REQUIRES_DEV,
  851. };
  852. /*
  853. * used by btrfsctl to scan devices when no FS is mounted
  854. */
  855. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  856. unsigned long arg)
  857. {
  858. struct btrfs_ioctl_vol_args *vol;
  859. struct btrfs_fs_devices *fs_devices;
  860. int ret = -ENOTTY;
  861. if (!capable(CAP_SYS_ADMIN))
  862. return -EPERM;
  863. vol = memdup_user((void __user *)arg, sizeof(*vol));
  864. if (IS_ERR(vol))
  865. return PTR_ERR(vol);
  866. switch (cmd) {
  867. case BTRFS_IOC_SCAN_DEV:
  868. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  869. &btrfs_fs_type, &fs_devices);
  870. break;
  871. }
  872. kfree(vol);
  873. return ret;
  874. }
  875. static int btrfs_freeze(struct super_block *sb)
  876. {
  877. struct btrfs_root *root = btrfs_sb(sb);
  878. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  879. mutex_lock(&root->fs_info->cleaner_mutex);
  880. return 0;
  881. }
  882. static int btrfs_unfreeze(struct super_block *sb)
  883. {
  884. struct btrfs_root *root = btrfs_sb(sb);
  885. mutex_unlock(&root->fs_info->cleaner_mutex);
  886. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  887. return 0;
  888. }
  889. static const struct super_operations btrfs_super_ops = {
  890. .drop_inode = btrfs_drop_inode,
  891. .evict_inode = btrfs_evict_inode,
  892. .put_super = btrfs_put_super,
  893. .sync_fs = btrfs_sync_fs,
  894. .show_options = btrfs_show_options,
  895. .write_inode = btrfs_write_inode,
  896. .dirty_inode = btrfs_dirty_inode,
  897. .alloc_inode = btrfs_alloc_inode,
  898. .destroy_inode = btrfs_destroy_inode,
  899. .statfs = btrfs_statfs,
  900. .remount_fs = btrfs_remount,
  901. .freeze_fs = btrfs_freeze,
  902. .unfreeze_fs = btrfs_unfreeze,
  903. };
  904. static const struct file_operations btrfs_ctl_fops = {
  905. .unlocked_ioctl = btrfs_control_ioctl,
  906. .compat_ioctl = btrfs_control_ioctl,
  907. .owner = THIS_MODULE,
  908. };
  909. static struct miscdevice btrfs_misc = {
  910. .minor = BTRFS_MINOR,
  911. .name = "btrfs-control",
  912. .fops = &btrfs_ctl_fops
  913. };
  914. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  915. MODULE_ALIAS("devname:btrfs-control");
  916. static int btrfs_interface_init(void)
  917. {
  918. return misc_register(&btrfs_misc);
  919. }
  920. static void btrfs_interface_exit(void)
  921. {
  922. if (misc_deregister(&btrfs_misc) < 0)
  923. printk(KERN_INFO "misc_deregister failed for control device");
  924. }
  925. static int __init init_btrfs_fs(void)
  926. {
  927. int err;
  928. err = btrfs_init_sysfs();
  929. if (err)
  930. return err;
  931. err = btrfs_init_compress();
  932. if (err)
  933. goto free_sysfs;
  934. err = btrfs_init_cachep();
  935. if (err)
  936. goto free_compress;
  937. err = extent_io_init();
  938. if (err)
  939. goto free_cachep;
  940. err = extent_map_init();
  941. if (err)
  942. goto free_extent_io;
  943. err = btrfs_interface_init();
  944. if (err)
  945. goto free_extent_map;
  946. err = register_filesystem(&btrfs_fs_type);
  947. if (err)
  948. goto unregister_ioctl;
  949. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  950. return 0;
  951. unregister_ioctl:
  952. btrfs_interface_exit();
  953. free_extent_map:
  954. extent_map_exit();
  955. free_extent_io:
  956. extent_io_exit();
  957. free_cachep:
  958. btrfs_destroy_cachep();
  959. free_compress:
  960. btrfs_exit_compress();
  961. free_sysfs:
  962. btrfs_exit_sysfs();
  963. return err;
  964. }
  965. static void __exit exit_btrfs_fs(void)
  966. {
  967. btrfs_destroy_cachep();
  968. extent_map_exit();
  969. extent_io_exit();
  970. btrfs_interface_exit();
  971. unregister_filesystem(&btrfs_fs_type);
  972. btrfs_exit_sysfs();
  973. btrfs_cleanup_fs_uuids();
  974. btrfs_exit_compress();
  975. }
  976. module_init(init_btrfs_fs)
  977. module_exit(exit_btrfs_fs)
  978. MODULE_LICENSE("GPL");