super.c 33 KB

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