super.c 34 KB

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