super.c 30 KB

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