super.c 30 KB

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