super.c 31 KB

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