super.c 32 KB

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