super.c 30 KB

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