super.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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_d_op = &btrfs_dentry_operations;
  523. sb->s_export_op = &btrfs_export_ops;
  524. sb->s_xattr = btrfs_xattr_handlers;
  525. sb->s_time_gran = 1;
  526. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  527. sb->s_flags |= MS_POSIXACL;
  528. #endif
  529. tree_root = open_ctree(sb, fs_devices, (char *)data);
  530. if (IS_ERR(tree_root)) {
  531. printk("btrfs: open_ctree failed\n");
  532. return PTR_ERR(tree_root);
  533. }
  534. sb->s_fs_info = tree_root;
  535. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  536. key.type = BTRFS_INODE_ITEM_KEY;
  537. key.offset = 0;
  538. inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL);
  539. if (IS_ERR(inode)) {
  540. err = PTR_ERR(inode);
  541. goto fail_close;
  542. }
  543. root_dentry = d_alloc_root(inode);
  544. if (!root_dentry) {
  545. iput(inode);
  546. err = -ENOMEM;
  547. goto fail_close;
  548. }
  549. sb->s_root = root_dentry;
  550. save_mount_options(sb, data);
  551. return 0;
  552. fail_close:
  553. close_ctree(tree_root);
  554. return err;
  555. }
  556. int btrfs_sync_fs(struct super_block *sb, int wait)
  557. {
  558. struct btrfs_trans_handle *trans;
  559. struct btrfs_root *root = btrfs_sb(sb);
  560. int ret;
  561. if (!wait) {
  562. filemap_flush(root->fs_info->btree_inode->i_mapping);
  563. return 0;
  564. }
  565. btrfs_start_delalloc_inodes(root, 0);
  566. btrfs_wait_ordered_extents(root, 0, 0);
  567. trans = btrfs_start_transaction(root, 0);
  568. if (IS_ERR(trans))
  569. return PTR_ERR(trans);
  570. ret = btrfs_commit_transaction(trans, root);
  571. return ret;
  572. }
  573. static int btrfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  574. {
  575. struct btrfs_root *root = btrfs_sb(vfs->mnt_sb);
  576. struct btrfs_fs_info *info = root->fs_info;
  577. if (btrfs_test_opt(root, DEGRADED))
  578. seq_puts(seq, ",degraded");
  579. if (btrfs_test_opt(root, NODATASUM))
  580. seq_puts(seq, ",nodatasum");
  581. if (btrfs_test_opt(root, NODATACOW))
  582. seq_puts(seq, ",nodatacow");
  583. if (btrfs_test_opt(root, NOBARRIER))
  584. seq_puts(seq, ",nobarrier");
  585. if (info->max_inline != 8192 * 1024)
  586. seq_printf(seq, ",max_inline=%llu",
  587. (unsigned long long)info->max_inline);
  588. if (info->alloc_start != 0)
  589. seq_printf(seq, ",alloc_start=%llu",
  590. (unsigned long long)info->alloc_start);
  591. if (info->thread_pool_size != min_t(unsigned long,
  592. num_online_cpus() + 2, 8))
  593. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  594. if (btrfs_test_opt(root, COMPRESS))
  595. seq_puts(seq, ",compress");
  596. if (btrfs_test_opt(root, NOSSD))
  597. seq_puts(seq, ",nossd");
  598. if (btrfs_test_opt(root, SSD_SPREAD))
  599. seq_puts(seq, ",ssd_spread");
  600. else if (btrfs_test_opt(root, SSD))
  601. seq_puts(seq, ",ssd");
  602. if (btrfs_test_opt(root, NOTREELOG))
  603. seq_puts(seq, ",notreelog");
  604. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  605. seq_puts(seq, ",flushoncommit");
  606. if (btrfs_test_opt(root, DISCARD))
  607. seq_puts(seq, ",discard");
  608. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  609. seq_puts(seq, ",noacl");
  610. return 0;
  611. }
  612. static int btrfs_test_super(struct super_block *s, void *data)
  613. {
  614. struct btrfs_root *test_root = data;
  615. struct btrfs_root *root = btrfs_sb(s);
  616. /*
  617. * If this super block is going away, return false as it
  618. * can't match as an existing super block.
  619. */
  620. if (!atomic_read(&s->s_active))
  621. return 0;
  622. return root->fs_info->fs_devices == test_root->fs_info->fs_devices;
  623. }
  624. static int btrfs_set_super(struct super_block *s, void *data)
  625. {
  626. s->s_fs_info = data;
  627. return set_anon_super(s, data);
  628. }
  629. /*
  630. * Find a superblock for the given device / mount point.
  631. *
  632. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  633. * for multiple device setup. Make sure to keep it in sync.
  634. */
  635. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  636. const char *dev_name, void *data)
  637. {
  638. struct block_device *bdev = NULL;
  639. struct super_block *s;
  640. struct dentry *root;
  641. struct btrfs_fs_devices *fs_devices = NULL;
  642. struct btrfs_root *tree_root = NULL;
  643. struct btrfs_fs_info *fs_info = NULL;
  644. fmode_t mode = FMODE_READ;
  645. char *subvol_name = NULL;
  646. u64 subvol_objectid = 0;
  647. int error = 0;
  648. if (!(flags & MS_RDONLY))
  649. mode |= FMODE_WRITE;
  650. error = btrfs_parse_early_options(data, mode, fs_type,
  651. &subvol_name, &subvol_objectid,
  652. &fs_devices);
  653. if (error)
  654. return ERR_PTR(error);
  655. error = btrfs_scan_one_device(dev_name, mode, fs_type, &fs_devices);
  656. if (error)
  657. goto error_free_subvol_name;
  658. error = btrfs_open_devices(fs_devices, mode, fs_type);
  659. if (error)
  660. goto error_free_subvol_name;
  661. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  662. error = -EACCES;
  663. goto error_close_devices;
  664. }
  665. /*
  666. * Setup a dummy root and fs_info for test/set super. This is because
  667. * we don't actually fill this stuff out until open_ctree, but we need
  668. * it for searching for existing supers, so this lets us do that and
  669. * then open_ctree will properly initialize everything later.
  670. */
  671. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  672. tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
  673. if (!fs_info || !tree_root) {
  674. error = -ENOMEM;
  675. goto error_close_devices;
  676. }
  677. fs_info->tree_root = tree_root;
  678. fs_info->fs_devices = fs_devices;
  679. tree_root->fs_info = fs_info;
  680. bdev = fs_devices->latest_bdev;
  681. s = sget(fs_type, btrfs_test_super, btrfs_set_super, tree_root);
  682. if (IS_ERR(s))
  683. goto error_s;
  684. if (s->s_root) {
  685. if ((flags ^ s->s_flags) & MS_RDONLY) {
  686. deactivate_locked_super(s);
  687. error = -EBUSY;
  688. goto error_close_devices;
  689. }
  690. btrfs_close_devices(fs_devices);
  691. kfree(fs_info);
  692. kfree(tree_root);
  693. } else {
  694. char b[BDEVNAME_SIZE];
  695. s->s_flags = flags;
  696. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  697. error = btrfs_fill_super(s, fs_devices, data,
  698. flags & MS_SILENT ? 1 : 0);
  699. if (error) {
  700. deactivate_locked_super(s);
  701. goto error_free_subvol_name;
  702. }
  703. btrfs_sb(s)->fs_info->bdev_holder = fs_type;
  704. s->s_flags |= MS_ACTIVE;
  705. }
  706. root = get_default_root(s, subvol_objectid);
  707. if (IS_ERR(root)) {
  708. error = PTR_ERR(root);
  709. deactivate_locked_super(s);
  710. goto error_free_subvol_name;
  711. }
  712. /* if they gave us a subvolume name bind mount into that */
  713. if (strcmp(subvol_name, ".")) {
  714. struct dentry *new_root;
  715. mutex_lock(&root->d_inode->i_mutex);
  716. new_root = lookup_one_len(subvol_name, root,
  717. strlen(subvol_name));
  718. mutex_unlock(&root->d_inode->i_mutex);
  719. if (IS_ERR(new_root)) {
  720. dput(root);
  721. deactivate_locked_super(s);
  722. error = PTR_ERR(new_root);
  723. goto error_free_subvol_name;
  724. }
  725. if (!new_root->d_inode) {
  726. dput(root);
  727. dput(new_root);
  728. deactivate_locked_super(s);
  729. error = -ENXIO;
  730. goto error_free_subvol_name;
  731. }
  732. dput(root);
  733. root = new_root;
  734. }
  735. kfree(subvol_name);
  736. return root;
  737. error_s:
  738. error = PTR_ERR(s);
  739. error_close_devices:
  740. btrfs_close_devices(fs_devices);
  741. kfree(fs_info);
  742. kfree(tree_root);
  743. error_free_subvol_name:
  744. kfree(subvol_name);
  745. return ERR_PTR(error);
  746. }
  747. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  748. {
  749. struct btrfs_root *root = btrfs_sb(sb);
  750. int ret;
  751. ret = btrfs_parse_options(root, data);
  752. if (ret)
  753. return -EINVAL;
  754. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  755. return 0;
  756. if (*flags & MS_RDONLY) {
  757. sb->s_flags |= MS_RDONLY;
  758. ret = btrfs_commit_super(root);
  759. WARN_ON(ret);
  760. } else {
  761. if (root->fs_info->fs_devices->rw_devices == 0)
  762. return -EACCES;
  763. if (btrfs_super_log_root(&root->fs_info->super_copy) != 0)
  764. return -EINVAL;
  765. ret = btrfs_cleanup_fs_roots(root->fs_info);
  766. WARN_ON(ret);
  767. /* recover relocation */
  768. ret = btrfs_recover_relocation(root);
  769. WARN_ON(ret);
  770. sb->s_flags &= ~MS_RDONLY;
  771. }
  772. return 0;
  773. }
  774. /*
  775. * The helper to calc the free space on the devices that can be used to store
  776. * file data.
  777. */
  778. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  779. {
  780. struct btrfs_fs_info *fs_info = root->fs_info;
  781. struct btrfs_device_info *devices_info;
  782. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  783. struct btrfs_device *device;
  784. u64 skip_space;
  785. u64 type;
  786. u64 avail_space;
  787. u64 used_space;
  788. u64 min_stripe_size;
  789. int min_stripes = 1;
  790. int i = 0, nr_devices;
  791. int ret;
  792. nr_devices = fs_info->fs_devices->rw_devices;
  793. BUG_ON(!nr_devices);
  794. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  795. GFP_NOFS);
  796. if (!devices_info)
  797. return -ENOMEM;
  798. /* calc min stripe number for data space alloction */
  799. type = btrfs_get_alloc_profile(root, 1);
  800. if (type & BTRFS_BLOCK_GROUP_RAID0)
  801. min_stripes = 2;
  802. else if (type & BTRFS_BLOCK_GROUP_RAID1)
  803. min_stripes = 2;
  804. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  805. min_stripes = 4;
  806. if (type & BTRFS_BLOCK_GROUP_DUP)
  807. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  808. else
  809. min_stripe_size = BTRFS_STRIPE_LEN;
  810. list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
  811. if (!device->in_fs_metadata)
  812. continue;
  813. avail_space = device->total_bytes - device->bytes_used;
  814. /* align with stripe_len */
  815. do_div(avail_space, BTRFS_STRIPE_LEN);
  816. avail_space *= BTRFS_STRIPE_LEN;
  817. /*
  818. * In order to avoid overwritting the superblock on the drive,
  819. * btrfs starts at an offset of at least 1MB when doing chunk
  820. * allocation.
  821. */
  822. skip_space = 1024 * 1024;
  823. /* user can set the offset in fs_info->alloc_start. */
  824. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  825. device->total_bytes)
  826. skip_space = max(fs_info->alloc_start, skip_space);
  827. /*
  828. * btrfs can not use the free space in [0, skip_space - 1],
  829. * we must subtract it from the total. In order to implement
  830. * it, we account the used space in this range first.
  831. */
  832. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  833. &used_space);
  834. if (ret) {
  835. kfree(devices_info);
  836. return ret;
  837. }
  838. /* calc the free space in [0, skip_space - 1] */
  839. skip_space -= used_space;
  840. /*
  841. * we can use the free space in [0, skip_space - 1], subtract
  842. * it from the total.
  843. */
  844. if (avail_space && avail_space >= skip_space)
  845. avail_space -= skip_space;
  846. else
  847. avail_space = 0;
  848. if (avail_space < min_stripe_size)
  849. continue;
  850. devices_info[i].dev = device;
  851. devices_info[i].max_avail = avail_space;
  852. i++;
  853. }
  854. nr_devices = i;
  855. btrfs_descending_sort_devices(devices_info, nr_devices);
  856. i = nr_devices - 1;
  857. avail_space = 0;
  858. while (nr_devices >= min_stripes) {
  859. if (devices_info[i].max_avail >= min_stripe_size) {
  860. int j;
  861. u64 alloc_size;
  862. avail_space += devices_info[i].max_avail * min_stripes;
  863. alloc_size = devices_info[i].max_avail;
  864. for (j = i + 1 - min_stripes; j <= i; j++)
  865. devices_info[j].max_avail -= alloc_size;
  866. }
  867. i--;
  868. nr_devices--;
  869. }
  870. kfree(devices_info);
  871. *free_bytes = avail_space;
  872. return 0;
  873. }
  874. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  875. {
  876. struct btrfs_root *root = btrfs_sb(dentry->d_sb);
  877. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  878. struct list_head *head = &root->fs_info->space_info;
  879. struct btrfs_space_info *found;
  880. u64 total_used = 0;
  881. u64 total_free_data = 0;
  882. int bits = dentry->d_sb->s_blocksize_bits;
  883. __be32 *fsid = (__be32 *)root->fs_info->fsid;
  884. int ret;
  885. /* holding chunk_muext to avoid allocating new chunks */
  886. mutex_lock(&root->fs_info->chunk_mutex);
  887. rcu_read_lock();
  888. list_for_each_entry_rcu(found, head, list) {
  889. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  890. total_free_data += found->disk_total - found->disk_used;
  891. total_free_data -=
  892. btrfs_account_ro_block_groups_free_space(found);
  893. }
  894. total_used += found->disk_used;
  895. }
  896. rcu_read_unlock();
  897. buf->f_namelen = BTRFS_NAME_LEN;
  898. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  899. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  900. buf->f_bsize = dentry->d_sb->s_blocksize;
  901. buf->f_type = BTRFS_SUPER_MAGIC;
  902. buf->f_bavail = total_free_data;
  903. ret = btrfs_calc_avail_data_space(root, &total_free_data);
  904. if (ret) {
  905. mutex_unlock(&root->fs_info->chunk_mutex);
  906. return ret;
  907. }
  908. buf->f_bavail += total_free_data;
  909. buf->f_bavail = buf->f_bavail >> bits;
  910. mutex_unlock(&root->fs_info->chunk_mutex);
  911. /* We treat it as constant endianness (it doesn't matter _which_)
  912. because we want the fsid to come out the same whether mounted
  913. on a big-endian or little-endian host */
  914. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  915. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  916. /* Mask in the root object ID too, to disambiguate subvols */
  917. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  918. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  919. return 0;
  920. }
  921. static struct file_system_type btrfs_fs_type = {
  922. .owner = THIS_MODULE,
  923. .name = "btrfs",
  924. .mount = btrfs_mount,
  925. .kill_sb = kill_anon_super,
  926. .fs_flags = FS_REQUIRES_DEV,
  927. };
  928. /*
  929. * used by btrfsctl to scan devices when no FS is mounted
  930. */
  931. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  932. unsigned long arg)
  933. {
  934. struct btrfs_ioctl_vol_args *vol;
  935. struct btrfs_fs_devices *fs_devices;
  936. int ret = -ENOTTY;
  937. if (!capable(CAP_SYS_ADMIN))
  938. return -EPERM;
  939. vol = memdup_user((void __user *)arg, sizeof(*vol));
  940. if (IS_ERR(vol))
  941. return PTR_ERR(vol);
  942. switch (cmd) {
  943. case BTRFS_IOC_SCAN_DEV:
  944. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  945. &btrfs_fs_type, &fs_devices);
  946. break;
  947. }
  948. kfree(vol);
  949. return ret;
  950. }
  951. static int btrfs_freeze(struct super_block *sb)
  952. {
  953. struct btrfs_root *root = btrfs_sb(sb);
  954. mutex_lock(&root->fs_info->transaction_kthread_mutex);
  955. mutex_lock(&root->fs_info->cleaner_mutex);
  956. return 0;
  957. }
  958. static int btrfs_unfreeze(struct super_block *sb)
  959. {
  960. struct btrfs_root *root = btrfs_sb(sb);
  961. mutex_unlock(&root->fs_info->cleaner_mutex);
  962. mutex_unlock(&root->fs_info->transaction_kthread_mutex);
  963. return 0;
  964. }
  965. static const struct super_operations btrfs_super_ops = {
  966. .drop_inode = btrfs_drop_inode,
  967. .evict_inode = btrfs_evict_inode,
  968. .put_super = btrfs_put_super,
  969. .sync_fs = btrfs_sync_fs,
  970. .show_options = btrfs_show_options,
  971. .write_inode = btrfs_write_inode,
  972. .dirty_inode = btrfs_dirty_inode,
  973. .alloc_inode = btrfs_alloc_inode,
  974. .destroy_inode = btrfs_destroy_inode,
  975. .statfs = btrfs_statfs,
  976. .remount_fs = btrfs_remount,
  977. .freeze_fs = btrfs_freeze,
  978. .unfreeze_fs = btrfs_unfreeze,
  979. };
  980. static const struct file_operations btrfs_ctl_fops = {
  981. .unlocked_ioctl = btrfs_control_ioctl,
  982. .compat_ioctl = btrfs_control_ioctl,
  983. .owner = THIS_MODULE,
  984. .llseek = noop_llseek,
  985. };
  986. static struct miscdevice btrfs_misc = {
  987. .minor = BTRFS_MINOR,
  988. .name = "btrfs-control",
  989. .fops = &btrfs_ctl_fops
  990. };
  991. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  992. MODULE_ALIAS("devname:btrfs-control");
  993. static int btrfs_interface_init(void)
  994. {
  995. return misc_register(&btrfs_misc);
  996. }
  997. static void btrfs_interface_exit(void)
  998. {
  999. if (misc_deregister(&btrfs_misc) < 0)
  1000. printk(KERN_INFO "misc_deregister failed for control device");
  1001. }
  1002. static int __init init_btrfs_fs(void)
  1003. {
  1004. int err;
  1005. err = btrfs_init_sysfs();
  1006. if (err)
  1007. return err;
  1008. err = btrfs_init_compress();
  1009. if (err)
  1010. goto free_sysfs;
  1011. err = btrfs_init_cachep();
  1012. if (err)
  1013. goto free_compress;
  1014. err = extent_io_init();
  1015. if (err)
  1016. goto free_cachep;
  1017. err = extent_map_init();
  1018. if (err)
  1019. goto free_extent_io;
  1020. err = btrfs_interface_init();
  1021. if (err)
  1022. goto free_extent_map;
  1023. err = register_filesystem(&btrfs_fs_type);
  1024. if (err)
  1025. goto unregister_ioctl;
  1026. printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
  1027. return 0;
  1028. unregister_ioctl:
  1029. btrfs_interface_exit();
  1030. free_extent_map:
  1031. extent_map_exit();
  1032. free_extent_io:
  1033. extent_io_exit();
  1034. free_cachep:
  1035. btrfs_destroy_cachep();
  1036. free_compress:
  1037. btrfs_exit_compress();
  1038. free_sysfs:
  1039. btrfs_exit_sysfs();
  1040. return err;
  1041. }
  1042. static void __exit exit_btrfs_fs(void)
  1043. {
  1044. btrfs_destroy_cachep();
  1045. extent_map_exit();
  1046. extent_io_exit();
  1047. btrfs_interface_exit();
  1048. unregister_filesystem(&btrfs_fs_type);
  1049. btrfs_exit_sysfs();
  1050. btrfs_cleanup_fs_uuids();
  1051. btrfs_exit_compress();
  1052. }
  1053. module_init(init_btrfs_fs)
  1054. module_exit(exit_btrfs_fs)
  1055. MODULE_LICENSE("GPL");