super.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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/ratelimit.h>
  43. #include <linux/btrfs.h>
  44. #include "compat.h"
  45. #include "delayed-inode.h"
  46. #include "ctree.h"
  47. #include "disk-io.h"
  48. #include "transaction.h"
  49. #include "btrfs_inode.h"
  50. #include "print-tree.h"
  51. #include "xattr.h"
  52. #include "volumes.h"
  53. #include "export.h"
  54. #include "compression.h"
  55. #include "rcu-string.h"
  56. #include "dev-replace.h"
  57. #include "free-space-cache.h"
  58. #include "backref.h"
  59. #include "tests/btrfs-tests.h"
  60. #define CREATE_TRACE_POINTS
  61. #include <trace/events/btrfs.h>
  62. static const struct super_operations btrfs_super_ops;
  63. static struct file_system_type btrfs_fs_type;
  64. static const char *btrfs_decode_error(int errno)
  65. {
  66. char *errstr = "unknown";
  67. switch (errno) {
  68. case -EIO:
  69. errstr = "IO failure";
  70. break;
  71. case -ENOMEM:
  72. errstr = "Out of memory";
  73. break;
  74. case -EROFS:
  75. errstr = "Readonly filesystem";
  76. break;
  77. case -EEXIST:
  78. errstr = "Object already exists";
  79. break;
  80. case -ENOSPC:
  81. errstr = "No space left";
  82. break;
  83. case -ENOENT:
  84. errstr = "No such entry";
  85. break;
  86. }
  87. return errstr;
  88. }
  89. static void save_error_info(struct btrfs_fs_info *fs_info)
  90. {
  91. /*
  92. * today we only save the error info into ram. Long term we'll
  93. * also send it down to the disk
  94. */
  95. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  96. }
  97. /* btrfs handle error by forcing the filesystem readonly */
  98. static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  99. {
  100. struct super_block *sb = fs_info->sb;
  101. if (sb->s_flags & MS_RDONLY)
  102. return;
  103. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  104. sb->s_flags |= MS_RDONLY;
  105. btrfs_info(fs_info, "forced readonly");
  106. /*
  107. * Note that a running device replace operation is not
  108. * canceled here although there is no way to update
  109. * the progress. It would add the risk of a deadlock,
  110. * therefore the canceling is ommited. The only penalty
  111. * is that some I/O remains active until the procedure
  112. * completes. The next time when the filesystem is
  113. * mounted writeable again, the device replace
  114. * operation continues.
  115. */
  116. }
  117. }
  118. #ifdef CONFIG_PRINTK
  119. /*
  120. * __btrfs_std_error decodes expected errors from the caller and
  121. * invokes the approciate error response.
  122. */
  123. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  124. unsigned int line, int errno, const char *fmt, ...)
  125. {
  126. struct super_block *sb = fs_info->sb;
  127. const char *errstr;
  128. /*
  129. * Special case: if the error is EROFS, and we're already
  130. * under MS_RDONLY, then it is safe here.
  131. */
  132. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  133. return;
  134. errstr = btrfs_decode_error(errno);
  135. if (fmt) {
  136. struct va_format vaf;
  137. va_list args;
  138. va_start(args, fmt);
  139. vaf.fmt = fmt;
  140. vaf.va = &args;
  141. printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
  142. sb->s_id, function, line, errno, errstr, &vaf);
  143. va_end(args);
  144. } else {
  145. printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
  146. sb->s_id, function, line, errno, errstr);
  147. }
  148. /* Don't go through full error handling during mount */
  149. save_error_info(fs_info);
  150. if (sb->s_flags & MS_BORN)
  151. btrfs_handle_error(fs_info);
  152. }
  153. static const char * const logtypes[] = {
  154. "emergency",
  155. "alert",
  156. "critical",
  157. "error",
  158. "warning",
  159. "notice",
  160. "info",
  161. "debug",
  162. };
  163. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  164. {
  165. struct super_block *sb = fs_info->sb;
  166. char lvl[4];
  167. struct va_format vaf;
  168. va_list args;
  169. const char *type = logtypes[4];
  170. int kern_level;
  171. va_start(args, fmt);
  172. kern_level = printk_get_level(fmt);
  173. if (kern_level) {
  174. size_t size = printk_skip_level(fmt) - fmt;
  175. memcpy(lvl, fmt, size);
  176. lvl[size] = '\0';
  177. fmt += size;
  178. type = logtypes[kern_level - '0'];
  179. } else
  180. *lvl = '\0';
  181. vaf.fmt = fmt;
  182. vaf.va = &args;
  183. printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
  184. va_end(args);
  185. }
  186. #else
  187. void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
  188. unsigned int line, int errno, const char *fmt, ...)
  189. {
  190. struct super_block *sb = fs_info->sb;
  191. /*
  192. * Special case: if the error is EROFS, and we're already
  193. * under MS_RDONLY, then it is safe here.
  194. */
  195. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  196. return;
  197. /* Don't go through full error handling during mount */
  198. if (sb->s_flags & MS_BORN) {
  199. save_error_info(fs_info);
  200. btrfs_handle_error(fs_info);
  201. }
  202. }
  203. #endif
  204. /*
  205. * We only mark the transaction aborted and then set the file system read-only.
  206. * This will prevent new transactions from starting or trying to join this
  207. * one.
  208. *
  209. * This means that error recovery at the call site is limited to freeing
  210. * any local memory allocations and passing the error code up without
  211. * further cleanup. The transaction should complete as it normally would
  212. * in the call path but will return -EIO.
  213. *
  214. * We'll complete the cleanup in btrfs_end_transaction and
  215. * btrfs_commit_transaction.
  216. */
  217. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  218. struct btrfs_root *root, const char *function,
  219. unsigned int line, int errno)
  220. {
  221. /*
  222. * Report first abort since mount
  223. */
  224. if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
  225. &root->fs_info->fs_state)) {
  226. WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
  227. errno);
  228. }
  229. trans->aborted = errno;
  230. /* Nothing used. The other threads that have joined this
  231. * transaction may be able to continue. */
  232. if (!trans->blocks_used) {
  233. const char *errstr;
  234. errstr = btrfs_decode_error(errno);
  235. btrfs_warn(root->fs_info,
  236. "%s:%d: Aborting unused transaction(%s).",
  237. function, line, errstr);
  238. return;
  239. }
  240. ACCESS_ONCE(trans->transaction->aborted) = errno;
  241. /* Wake up anybody who may be waiting on this transaction */
  242. wake_up(&root->fs_info->transaction_wait);
  243. wake_up(&root->fs_info->transaction_blocked_wait);
  244. __btrfs_std_error(root->fs_info, function, line, errno, NULL);
  245. }
  246. /*
  247. * __btrfs_panic decodes unexpected, fatal errors from the caller,
  248. * issues an alert, and either panics or BUGs, depending on mount options.
  249. */
  250. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  251. unsigned int line, int errno, const char *fmt, ...)
  252. {
  253. char *s_id = "<unknown>";
  254. const char *errstr;
  255. struct va_format vaf = { .fmt = fmt };
  256. va_list args;
  257. if (fs_info)
  258. s_id = fs_info->sb->s_id;
  259. va_start(args, fmt);
  260. vaf.va = &args;
  261. errstr = btrfs_decode_error(errno);
  262. if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
  263. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  264. s_id, function, line, &vaf, errno, errstr);
  265. printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  266. s_id, function, line, &vaf, errno, errstr);
  267. va_end(args);
  268. /* Caller calls BUG() */
  269. }
  270. static void btrfs_put_super(struct super_block *sb)
  271. {
  272. (void)close_ctree(btrfs_sb(sb)->tree_root);
  273. /* FIXME: need to fix VFS to return error? */
  274. /* AV: return it _where_? ->put_super() can be triggered by any number
  275. * of async events, up to and including delivery of SIGKILL to the
  276. * last process that kept it busy. Or segfault in the aforementioned
  277. * process... Whom would you report that to?
  278. */
  279. }
  280. enum {
  281. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  282. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  283. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  284. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  285. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  286. Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
  287. Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
  288. Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
  289. Opt_check_integrity, Opt_check_integrity_including_extent_data,
  290. Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
  291. Opt_commit_interval,
  292. Opt_err,
  293. };
  294. static match_table_t tokens = {
  295. {Opt_degraded, "degraded"},
  296. {Opt_subvol, "subvol=%s"},
  297. {Opt_subvolid, "subvolid=%s"},
  298. {Opt_device, "device=%s"},
  299. {Opt_nodatasum, "nodatasum"},
  300. {Opt_nodatacow, "nodatacow"},
  301. {Opt_nobarrier, "nobarrier"},
  302. {Opt_max_inline, "max_inline=%s"},
  303. {Opt_alloc_start, "alloc_start=%s"},
  304. {Opt_thread_pool, "thread_pool=%d"},
  305. {Opt_compress, "compress"},
  306. {Opt_compress_type, "compress=%s"},
  307. {Opt_compress_force, "compress-force"},
  308. {Opt_compress_force_type, "compress-force=%s"},
  309. {Opt_ssd, "ssd"},
  310. {Opt_ssd_spread, "ssd_spread"},
  311. {Opt_nossd, "nossd"},
  312. {Opt_noacl, "noacl"},
  313. {Opt_notreelog, "notreelog"},
  314. {Opt_flushoncommit, "flushoncommit"},
  315. {Opt_ratio, "metadata_ratio=%d"},
  316. {Opt_discard, "discard"},
  317. {Opt_space_cache, "space_cache"},
  318. {Opt_clear_cache, "clear_cache"},
  319. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  320. {Opt_enospc_debug, "enospc_debug"},
  321. {Opt_subvolrootid, "subvolrootid=%d"},
  322. {Opt_defrag, "autodefrag"},
  323. {Opt_inode_cache, "inode_cache"},
  324. {Opt_no_space_cache, "nospace_cache"},
  325. {Opt_recovery, "recovery"},
  326. {Opt_skip_balance, "skip_balance"},
  327. {Opt_check_integrity, "check_int"},
  328. {Opt_check_integrity_including_extent_data, "check_int_data"},
  329. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  330. {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
  331. {Opt_fatal_errors, "fatal_errors=%s"},
  332. {Opt_commit_interval, "commit=%d"},
  333. {Opt_err, NULL},
  334. };
  335. /*
  336. * Regular mount options parser. Everything that is needed only when
  337. * reading in a new superblock is parsed here.
  338. * XXX JDM: This needs to be cleaned up for remount.
  339. */
  340. int btrfs_parse_options(struct btrfs_root *root, char *options)
  341. {
  342. struct btrfs_fs_info *info = root->fs_info;
  343. substring_t args[MAX_OPT_ARGS];
  344. char *p, *num, *orig = NULL;
  345. u64 cache_gen;
  346. int intarg;
  347. int ret = 0;
  348. char *compress_type;
  349. bool compress_force = false;
  350. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  351. if (cache_gen)
  352. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  353. if (!options)
  354. goto out;
  355. /*
  356. * strsep changes the string, duplicate it because parse_options
  357. * gets called twice
  358. */
  359. options = kstrdup(options, GFP_NOFS);
  360. if (!options)
  361. return -ENOMEM;
  362. orig = options;
  363. while ((p = strsep(&options, ",")) != NULL) {
  364. int token;
  365. if (!*p)
  366. continue;
  367. token = match_token(p, tokens, args);
  368. switch (token) {
  369. case Opt_degraded:
  370. printk(KERN_INFO "btrfs: allowing degraded mounts\n");
  371. btrfs_set_opt(info->mount_opt, DEGRADED);
  372. break;
  373. case Opt_subvol:
  374. case Opt_subvolid:
  375. case Opt_subvolrootid:
  376. case Opt_device:
  377. /*
  378. * These are parsed by btrfs_parse_early_options
  379. * and can be happily ignored here.
  380. */
  381. break;
  382. case Opt_nodatasum:
  383. printk(KERN_INFO "btrfs: setting nodatasum\n");
  384. btrfs_set_opt(info->mount_opt, NODATASUM);
  385. break;
  386. case Opt_nodatacow:
  387. if (!btrfs_test_opt(root, COMPRESS) ||
  388. !btrfs_test_opt(root, FORCE_COMPRESS)) {
  389. printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
  390. } else {
  391. printk(KERN_INFO "btrfs: setting nodatacow\n");
  392. }
  393. info->compress_type = BTRFS_COMPRESS_NONE;
  394. btrfs_clear_opt(info->mount_opt, COMPRESS);
  395. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  396. btrfs_set_opt(info->mount_opt, NODATACOW);
  397. btrfs_set_opt(info->mount_opt, NODATASUM);
  398. break;
  399. case Opt_compress_force:
  400. case Opt_compress_force_type:
  401. compress_force = true;
  402. /* Fallthrough */
  403. case Opt_compress:
  404. case Opt_compress_type:
  405. if (token == Opt_compress ||
  406. token == Opt_compress_force ||
  407. strcmp(args[0].from, "zlib") == 0) {
  408. compress_type = "zlib";
  409. info->compress_type = BTRFS_COMPRESS_ZLIB;
  410. btrfs_set_opt(info->mount_opt, COMPRESS);
  411. btrfs_clear_opt(info->mount_opt, NODATACOW);
  412. btrfs_clear_opt(info->mount_opt, NODATASUM);
  413. } else if (strcmp(args[0].from, "lzo") == 0) {
  414. compress_type = "lzo";
  415. info->compress_type = BTRFS_COMPRESS_LZO;
  416. btrfs_set_opt(info->mount_opt, COMPRESS);
  417. btrfs_clear_opt(info->mount_opt, NODATACOW);
  418. btrfs_clear_opt(info->mount_opt, NODATASUM);
  419. btrfs_set_fs_incompat(info, COMPRESS_LZO);
  420. } else if (strncmp(args[0].from, "no", 2) == 0) {
  421. compress_type = "no";
  422. info->compress_type = BTRFS_COMPRESS_NONE;
  423. btrfs_clear_opt(info->mount_opt, COMPRESS);
  424. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  425. compress_force = false;
  426. } else {
  427. ret = -EINVAL;
  428. goto out;
  429. }
  430. if (compress_force) {
  431. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  432. pr_info("btrfs: force %s compression\n",
  433. compress_type);
  434. } else
  435. pr_info("btrfs: use %s compression\n",
  436. compress_type);
  437. break;
  438. case Opt_ssd:
  439. printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
  440. btrfs_set_opt(info->mount_opt, SSD);
  441. break;
  442. case Opt_ssd_spread:
  443. printk(KERN_INFO "btrfs: use spread ssd "
  444. "allocation scheme\n");
  445. btrfs_set_opt(info->mount_opt, SSD);
  446. btrfs_set_opt(info->mount_opt, SSD_SPREAD);
  447. break;
  448. case Opt_nossd:
  449. printk(KERN_INFO "btrfs: not using ssd allocation "
  450. "scheme\n");
  451. btrfs_set_opt(info->mount_opt, NOSSD);
  452. btrfs_clear_opt(info->mount_opt, SSD);
  453. btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
  454. break;
  455. case Opt_nobarrier:
  456. printk(KERN_INFO "btrfs: turning off barriers\n");
  457. btrfs_set_opt(info->mount_opt, NOBARRIER);
  458. break;
  459. case Opt_thread_pool:
  460. ret = match_int(&args[0], &intarg);
  461. if (ret) {
  462. goto out;
  463. } else if (intarg > 0) {
  464. info->thread_pool_size = intarg;
  465. } else {
  466. ret = -EINVAL;
  467. goto out;
  468. }
  469. break;
  470. case Opt_max_inline:
  471. num = match_strdup(&args[0]);
  472. if (num) {
  473. info->max_inline = memparse(num, NULL);
  474. kfree(num);
  475. if (info->max_inline) {
  476. info->max_inline = max_t(u64,
  477. info->max_inline,
  478. root->sectorsize);
  479. }
  480. printk(KERN_INFO "btrfs: max_inline at %llu\n",
  481. info->max_inline);
  482. } else {
  483. ret = -ENOMEM;
  484. goto out;
  485. }
  486. break;
  487. case Opt_alloc_start:
  488. num = match_strdup(&args[0]);
  489. if (num) {
  490. mutex_lock(&info->chunk_mutex);
  491. info->alloc_start = memparse(num, NULL);
  492. mutex_unlock(&info->chunk_mutex);
  493. kfree(num);
  494. printk(KERN_INFO
  495. "btrfs: allocations start at %llu\n",
  496. info->alloc_start);
  497. } else {
  498. ret = -ENOMEM;
  499. goto out;
  500. }
  501. break;
  502. case Opt_noacl:
  503. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  504. break;
  505. case Opt_notreelog:
  506. printk(KERN_INFO "btrfs: disabling tree log\n");
  507. btrfs_set_opt(info->mount_opt, NOTREELOG);
  508. break;
  509. case Opt_flushoncommit:
  510. printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
  511. btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
  512. break;
  513. case Opt_ratio:
  514. ret = match_int(&args[0], &intarg);
  515. if (ret) {
  516. goto out;
  517. } else if (intarg >= 0) {
  518. info->metadata_ratio = intarg;
  519. printk(KERN_INFO "btrfs: metadata ratio %d\n",
  520. info->metadata_ratio);
  521. } else {
  522. ret = -EINVAL;
  523. goto out;
  524. }
  525. break;
  526. case Opt_discard:
  527. btrfs_set_opt(info->mount_opt, DISCARD);
  528. break;
  529. case Opt_space_cache:
  530. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  531. break;
  532. case Opt_rescan_uuid_tree:
  533. btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
  534. break;
  535. case Opt_no_space_cache:
  536. printk(KERN_INFO "btrfs: disabling disk space caching\n");
  537. btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
  538. break;
  539. case Opt_inode_cache:
  540. printk(KERN_INFO "btrfs: enabling inode map caching\n");
  541. btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
  542. break;
  543. case Opt_clear_cache:
  544. printk(KERN_INFO "btrfs: force clearing of disk cache\n");
  545. btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
  546. break;
  547. case Opt_user_subvol_rm_allowed:
  548. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  549. break;
  550. case Opt_enospc_debug:
  551. btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  552. break;
  553. case Opt_defrag:
  554. printk(KERN_INFO "btrfs: enabling auto defrag\n");
  555. btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
  556. break;
  557. case Opt_recovery:
  558. printk(KERN_INFO "btrfs: enabling auto recovery\n");
  559. btrfs_set_opt(info->mount_opt, RECOVERY);
  560. break;
  561. case Opt_skip_balance:
  562. btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  563. break;
  564. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  565. case Opt_check_integrity_including_extent_data:
  566. printk(KERN_INFO "btrfs: enabling check integrity"
  567. " including extent data\n");
  568. btrfs_set_opt(info->mount_opt,
  569. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  570. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  571. break;
  572. case Opt_check_integrity:
  573. printk(KERN_INFO "btrfs: enabling check integrity\n");
  574. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  575. break;
  576. case Opt_check_integrity_print_mask:
  577. ret = match_int(&args[0], &intarg);
  578. if (ret) {
  579. goto out;
  580. } else if (intarg >= 0) {
  581. info->check_integrity_print_mask = intarg;
  582. printk(KERN_INFO "btrfs:"
  583. " check_integrity_print_mask 0x%x\n",
  584. info->check_integrity_print_mask);
  585. } else {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. break;
  590. #else
  591. case Opt_check_integrity_including_extent_data:
  592. case Opt_check_integrity:
  593. case Opt_check_integrity_print_mask:
  594. printk(KERN_ERR "btrfs: support for check_integrity*"
  595. " not compiled in!\n");
  596. ret = -EINVAL;
  597. goto out;
  598. #endif
  599. case Opt_fatal_errors:
  600. if (strcmp(args[0].from, "panic") == 0)
  601. btrfs_set_opt(info->mount_opt,
  602. PANIC_ON_FATAL_ERROR);
  603. else if (strcmp(args[0].from, "bug") == 0)
  604. btrfs_clear_opt(info->mount_opt,
  605. PANIC_ON_FATAL_ERROR);
  606. else {
  607. ret = -EINVAL;
  608. goto out;
  609. }
  610. break;
  611. case Opt_commit_interval:
  612. intarg = 0;
  613. ret = match_int(&args[0], &intarg);
  614. if (ret < 0) {
  615. printk(KERN_ERR
  616. "btrfs: invalid commit interval\n");
  617. ret = -EINVAL;
  618. goto out;
  619. }
  620. if (intarg > 0) {
  621. if (intarg > 300) {
  622. printk(KERN_WARNING
  623. "btrfs: excessive commit interval %d\n",
  624. intarg);
  625. }
  626. info->commit_interval = intarg;
  627. } else {
  628. printk(KERN_INFO
  629. "btrfs: using default commit interval %ds\n",
  630. BTRFS_DEFAULT_COMMIT_INTERVAL);
  631. info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  632. }
  633. break;
  634. case Opt_err:
  635. printk(KERN_INFO "btrfs: unrecognized mount option "
  636. "'%s'\n", p);
  637. ret = -EINVAL;
  638. goto out;
  639. default:
  640. break;
  641. }
  642. }
  643. out:
  644. if (!ret && btrfs_test_opt(root, SPACE_CACHE))
  645. printk(KERN_INFO "btrfs: disk space caching is enabled\n");
  646. kfree(orig);
  647. return ret;
  648. }
  649. /*
  650. * Parse mount options that are required early in the mount process.
  651. *
  652. * All other options will be parsed on much later in the mount process and
  653. * only when we need to allocate a new super block.
  654. */
  655. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  656. void *holder, char **subvol_name, u64 *subvol_objectid,
  657. struct btrfs_fs_devices **fs_devices)
  658. {
  659. substring_t args[MAX_OPT_ARGS];
  660. char *device_name, *opts, *orig, *p;
  661. char *num = NULL;
  662. int error = 0;
  663. if (!options)
  664. return 0;
  665. /*
  666. * strsep changes the string, duplicate it because parse_options
  667. * gets called twice
  668. */
  669. opts = kstrdup(options, GFP_KERNEL);
  670. if (!opts)
  671. return -ENOMEM;
  672. orig = opts;
  673. while ((p = strsep(&opts, ",")) != NULL) {
  674. int token;
  675. if (!*p)
  676. continue;
  677. token = match_token(p, tokens, args);
  678. switch (token) {
  679. case Opt_subvol:
  680. kfree(*subvol_name);
  681. *subvol_name = match_strdup(&args[0]);
  682. if (!*subvol_name) {
  683. error = -ENOMEM;
  684. goto out;
  685. }
  686. break;
  687. case Opt_subvolid:
  688. num = match_strdup(&args[0]);
  689. if (num) {
  690. *subvol_objectid = memparse(num, NULL);
  691. kfree(num);
  692. /* we want the original fs_tree */
  693. if (!*subvol_objectid)
  694. *subvol_objectid =
  695. BTRFS_FS_TREE_OBJECTID;
  696. } else {
  697. error = -EINVAL;
  698. goto out;
  699. }
  700. break;
  701. case Opt_subvolrootid:
  702. printk(KERN_WARNING
  703. "btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
  704. break;
  705. case Opt_device:
  706. device_name = match_strdup(&args[0]);
  707. if (!device_name) {
  708. error = -ENOMEM;
  709. goto out;
  710. }
  711. error = btrfs_scan_one_device(device_name,
  712. flags, holder, fs_devices);
  713. kfree(device_name);
  714. if (error)
  715. goto out;
  716. break;
  717. default:
  718. break;
  719. }
  720. }
  721. out:
  722. kfree(orig);
  723. return error;
  724. }
  725. static struct dentry *get_default_root(struct super_block *sb,
  726. u64 subvol_objectid)
  727. {
  728. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  729. struct btrfs_root *root = fs_info->tree_root;
  730. struct btrfs_root *new_root;
  731. struct btrfs_dir_item *di;
  732. struct btrfs_path *path;
  733. struct btrfs_key location;
  734. struct inode *inode;
  735. u64 dir_id;
  736. int new = 0;
  737. /*
  738. * We have a specific subvol we want to mount, just setup location and
  739. * go look up the root.
  740. */
  741. if (subvol_objectid) {
  742. location.objectid = subvol_objectid;
  743. location.type = BTRFS_ROOT_ITEM_KEY;
  744. location.offset = (u64)-1;
  745. goto find_root;
  746. }
  747. path = btrfs_alloc_path();
  748. if (!path)
  749. return ERR_PTR(-ENOMEM);
  750. path->leave_spinning = 1;
  751. /*
  752. * Find the "default" dir item which points to the root item that we
  753. * will mount by default if we haven't been given a specific subvolume
  754. * to mount.
  755. */
  756. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  757. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  758. if (IS_ERR(di)) {
  759. btrfs_free_path(path);
  760. return ERR_CAST(di);
  761. }
  762. if (!di) {
  763. /*
  764. * Ok the default dir item isn't there. This is weird since
  765. * it's always been there, but don't freak out, just try and
  766. * mount to root most subvolume.
  767. */
  768. btrfs_free_path(path);
  769. dir_id = BTRFS_FIRST_FREE_OBJECTID;
  770. new_root = fs_info->fs_root;
  771. goto setup_root;
  772. }
  773. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  774. btrfs_free_path(path);
  775. find_root:
  776. new_root = btrfs_read_fs_root_no_name(fs_info, &location);
  777. if (IS_ERR(new_root))
  778. return ERR_CAST(new_root);
  779. dir_id = btrfs_root_dirid(&new_root->root_item);
  780. setup_root:
  781. location.objectid = dir_id;
  782. location.type = BTRFS_INODE_ITEM_KEY;
  783. location.offset = 0;
  784. inode = btrfs_iget(sb, &location, new_root, &new);
  785. if (IS_ERR(inode))
  786. return ERR_CAST(inode);
  787. /*
  788. * If we're just mounting the root most subvol put the inode and return
  789. * a reference to the dentry. We will have already gotten a reference
  790. * to the inode in btrfs_fill_super so we're good to go.
  791. */
  792. if (!new && sb->s_root->d_inode == inode) {
  793. iput(inode);
  794. return dget(sb->s_root);
  795. }
  796. return d_obtain_alias(inode);
  797. }
  798. static int btrfs_fill_super(struct super_block *sb,
  799. struct btrfs_fs_devices *fs_devices,
  800. void *data, int silent)
  801. {
  802. struct inode *inode;
  803. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  804. struct btrfs_key key;
  805. int err;
  806. sb->s_maxbytes = MAX_LFS_FILESIZE;
  807. sb->s_magic = BTRFS_SUPER_MAGIC;
  808. sb->s_op = &btrfs_super_ops;
  809. sb->s_d_op = &btrfs_dentry_operations;
  810. sb->s_export_op = &btrfs_export_ops;
  811. sb->s_xattr = btrfs_xattr_handlers;
  812. sb->s_time_gran = 1;
  813. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  814. sb->s_flags |= MS_POSIXACL;
  815. #endif
  816. sb->s_flags |= MS_I_VERSION;
  817. err = open_ctree(sb, fs_devices, (char *)data);
  818. if (err) {
  819. printk("btrfs: open_ctree failed\n");
  820. return err;
  821. }
  822. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  823. key.type = BTRFS_INODE_ITEM_KEY;
  824. key.offset = 0;
  825. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  826. if (IS_ERR(inode)) {
  827. err = PTR_ERR(inode);
  828. goto fail_close;
  829. }
  830. sb->s_root = d_make_root(inode);
  831. if (!sb->s_root) {
  832. err = -ENOMEM;
  833. goto fail_close;
  834. }
  835. save_mount_options(sb, data);
  836. cleancache_init_fs(sb);
  837. sb->s_flags |= MS_ACTIVE;
  838. return 0;
  839. fail_close:
  840. close_ctree(fs_info->tree_root);
  841. return err;
  842. }
  843. int btrfs_sync_fs(struct super_block *sb, int wait)
  844. {
  845. struct btrfs_trans_handle *trans;
  846. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  847. struct btrfs_root *root = fs_info->tree_root;
  848. trace_btrfs_sync_fs(wait);
  849. if (!wait) {
  850. filemap_flush(fs_info->btree_inode->i_mapping);
  851. return 0;
  852. }
  853. btrfs_wait_all_ordered_extents(fs_info);
  854. trans = btrfs_attach_transaction_barrier(root);
  855. if (IS_ERR(trans)) {
  856. /* no transaction, don't bother */
  857. if (PTR_ERR(trans) == -ENOENT)
  858. return 0;
  859. return PTR_ERR(trans);
  860. }
  861. return btrfs_commit_transaction(trans, root);
  862. }
  863. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  864. {
  865. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  866. struct btrfs_root *root = info->tree_root;
  867. char *compress_type;
  868. if (btrfs_test_opt(root, DEGRADED))
  869. seq_puts(seq, ",degraded");
  870. if (btrfs_test_opt(root, NODATASUM))
  871. seq_puts(seq, ",nodatasum");
  872. if (btrfs_test_opt(root, NODATACOW))
  873. seq_puts(seq, ",nodatacow");
  874. if (btrfs_test_opt(root, NOBARRIER))
  875. seq_puts(seq, ",nobarrier");
  876. if (info->max_inline != 8192 * 1024)
  877. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  878. if (info->alloc_start != 0)
  879. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  880. if (info->thread_pool_size != min_t(unsigned long,
  881. num_online_cpus() + 2, 8))
  882. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  883. if (btrfs_test_opt(root, COMPRESS)) {
  884. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  885. compress_type = "zlib";
  886. else
  887. compress_type = "lzo";
  888. if (btrfs_test_opt(root, FORCE_COMPRESS))
  889. seq_printf(seq, ",compress-force=%s", compress_type);
  890. else
  891. seq_printf(seq, ",compress=%s", compress_type);
  892. }
  893. if (btrfs_test_opt(root, NOSSD))
  894. seq_puts(seq, ",nossd");
  895. if (btrfs_test_opt(root, SSD_SPREAD))
  896. seq_puts(seq, ",ssd_spread");
  897. else if (btrfs_test_opt(root, SSD))
  898. seq_puts(seq, ",ssd");
  899. if (btrfs_test_opt(root, NOTREELOG))
  900. seq_puts(seq, ",notreelog");
  901. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  902. seq_puts(seq, ",flushoncommit");
  903. if (btrfs_test_opt(root, DISCARD))
  904. seq_puts(seq, ",discard");
  905. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  906. seq_puts(seq, ",noacl");
  907. if (btrfs_test_opt(root, SPACE_CACHE))
  908. seq_puts(seq, ",space_cache");
  909. else
  910. seq_puts(seq, ",nospace_cache");
  911. if (btrfs_test_opt(root, RESCAN_UUID_TREE))
  912. seq_puts(seq, ",rescan_uuid_tree");
  913. if (btrfs_test_opt(root, CLEAR_CACHE))
  914. seq_puts(seq, ",clear_cache");
  915. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  916. seq_puts(seq, ",user_subvol_rm_allowed");
  917. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  918. seq_puts(seq, ",enospc_debug");
  919. if (btrfs_test_opt(root, AUTO_DEFRAG))
  920. seq_puts(seq, ",autodefrag");
  921. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  922. seq_puts(seq, ",inode_cache");
  923. if (btrfs_test_opt(root, SKIP_BALANCE))
  924. seq_puts(seq, ",skip_balance");
  925. if (btrfs_test_opt(root, RECOVERY))
  926. seq_puts(seq, ",recovery");
  927. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  928. if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  929. seq_puts(seq, ",check_int_data");
  930. else if (btrfs_test_opt(root, CHECK_INTEGRITY))
  931. seq_puts(seq, ",check_int");
  932. if (info->check_integrity_print_mask)
  933. seq_printf(seq, ",check_int_print_mask=%d",
  934. info->check_integrity_print_mask);
  935. #endif
  936. if (info->metadata_ratio)
  937. seq_printf(seq, ",metadata_ratio=%d",
  938. info->metadata_ratio);
  939. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  940. seq_puts(seq, ",fatal_errors=panic");
  941. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  942. seq_printf(seq, ",commit=%d", info->commit_interval);
  943. return 0;
  944. }
  945. static int btrfs_test_super(struct super_block *s, void *data)
  946. {
  947. struct btrfs_fs_info *p = data;
  948. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  949. return fs_info->fs_devices == p->fs_devices;
  950. }
  951. static int btrfs_set_super(struct super_block *s, void *data)
  952. {
  953. int err = set_anon_super(s, data);
  954. if (!err)
  955. s->s_fs_info = data;
  956. return err;
  957. }
  958. /*
  959. * subvolumes are identified by ino 256
  960. */
  961. static inline int is_subvolume_inode(struct inode *inode)
  962. {
  963. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  964. return 1;
  965. return 0;
  966. }
  967. /*
  968. * This will strip out the subvol=%s argument for an argument string and add
  969. * subvolid=0 to make sure we get the actual tree root for path walking to the
  970. * subvol we want.
  971. */
  972. static char *setup_root_args(char *args)
  973. {
  974. unsigned len = strlen(args) + 2 + 1;
  975. char *src, *dst, *buf;
  976. /*
  977. * We need the same args as before, but with this substitution:
  978. * s!subvol=[^,]+!subvolid=0!
  979. *
  980. * Since the replacement string is up to 2 bytes longer than the
  981. * original, allocate strlen(args) + 2 + 1 bytes.
  982. */
  983. src = strstr(args, "subvol=");
  984. /* This shouldn't happen, but just in case.. */
  985. if (!src)
  986. return NULL;
  987. buf = dst = kmalloc(len, GFP_NOFS);
  988. if (!buf)
  989. return NULL;
  990. /*
  991. * If the subvol= arg is not at the start of the string,
  992. * copy whatever precedes it into buf.
  993. */
  994. if (src != args) {
  995. *src++ = '\0';
  996. strcpy(buf, args);
  997. dst += strlen(args);
  998. }
  999. strcpy(dst, "subvolid=0");
  1000. dst += strlen("subvolid=0");
  1001. /*
  1002. * If there is a "," after the original subvol=... string,
  1003. * copy that suffix into our buffer. Otherwise, we're done.
  1004. */
  1005. src = strchr(src, ',');
  1006. if (src)
  1007. strcpy(dst, src);
  1008. return buf;
  1009. }
  1010. static struct dentry *mount_subvol(const char *subvol_name, int flags,
  1011. const char *device_name, char *data)
  1012. {
  1013. struct dentry *root;
  1014. struct vfsmount *mnt;
  1015. char *newargs;
  1016. newargs = setup_root_args(data);
  1017. if (!newargs)
  1018. return ERR_PTR(-ENOMEM);
  1019. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
  1020. newargs);
  1021. kfree(newargs);
  1022. if (IS_ERR(mnt))
  1023. return ERR_CAST(mnt);
  1024. root = mount_subtree(mnt, subvol_name);
  1025. if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
  1026. struct super_block *s = root->d_sb;
  1027. dput(root);
  1028. root = ERR_PTR(-EINVAL);
  1029. deactivate_locked_super(s);
  1030. printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
  1031. subvol_name);
  1032. }
  1033. return root;
  1034. }
  1035. /*
  1036. * Find a superblock for the given device / mount point.
  1037. *
  1038. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1039. * for multiple device setup. Make sure to keep it in sync.
  1040. */
  1041. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1042. const char *device_name, void *data)
  1043. {
  1044. struct block_device *bdev = NULL;
  1045. struct super_block *s;
  1046. struct dentry *root;
  1047. struct btrfs_fs_devices *fs_devices = NULL;
  1048. struct btrfs_fs_info *fs_info = NULL;
  1049. fmode_t mode = FMODE_READ;
  1050. char *subvol_name = NULL;
  1051. u64 subvol_objectid = 0;
  1052. int error = 0;
  1053. if (!(flags & MS_RDONLY))
  1054. mode |= FMODE_WRITE;
  1055. error = btrfs_parse_early_options(data, mode, fs_type,
  1056. &subvol_name, &subvol_objectid,
  1057. &fs_devices);
  1058. if (error) {
  1059. kfree(subvol_name);
  1060. return ERR_PTR(error);
  1061. }
  1062. if (subvol_name) {
  1063. root = mount_subvol(subvol_name, flags, device_name, data);
  1064. kfree(subvol_name);
  1065. return root;
  1066. }
  1067. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1068. if (error)
  1069. return ERR_PTR(error);
  1070. /*
  1071. * Setup a dummy root and fs_info for test/set super. This is because
  1072. * we don't actually fill this stuff out until open_ctree, but we need
  1073. * it for searching for existing supers, so this lets us do that and
  1074. * then open_ctree will properly initialize everything later.
  1075. */
  1076. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1077. if (!fs_info)
  1078. return ERR_PTR(-ENOMEM);
  1079. fs_info->fs_devices = fs_devices;
  1080. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1081. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1082. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1083. error = -ENOMEM;
  1084. goto error_fs_info;
  1085. }
  1086. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1087. if (error)
  1088. goto error_fs_info;
  1089. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1090. error = -EACCES;
  1091. goto error_close_devices;
  1092. }
  1093. bdev = fs_devices->latest_bdev;
  1094. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1095. fs_info);
  1096. if (IS_ERR(s)) {
  1097. error = PTR_ERR(s);
  1098. goto error_close_devices;
  1099. }
  1100. if (s->s_root) {
  1101. btrfs_close_devices(fs_devices);
  1102. free_fs_info(fs_info);
  1103. if ((flags ^ s->s_flags) & MS_RDONLY)
  1104. error = -EBUSY;
  1105. } else {
  1106. char b[BDEVNAME_SIZE];
  1107. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  1108. btrfs_sb(s)->bdev_holder = fs_type;
  1109. error = btrfs_fill_super(s, fs_devices, data,
  1110. flags & MS_SILENT ? 1 : 0);
  1111. }
  1112. root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
  1113. if (IS_ERR(root))
  1114. deactivate_locked_super(s);
  1115. return root;
  1116. error_close_devices:
  1117. btrfs_close_devices(fs_devices);
  1118. error_fs_info:
  1119. free_fs_info(fs_info);
  1120. return ERR_PTR(error);
  1121. }
  1122. static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
  1123. {
  1124. spin_lock_irq(&workers->lock);
  1125. workers->max_workers = new_limit;
  1126. spin_unlock_irq(&workers->lock);
  1127. }
  1128. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1129. int new_pool_size, int old_pool_size)
  1130. {
  1131. if (new_pool_size == old_pool_size)
  1132. return;
  1133. fs_info->thread_pool_size = new_pool_size;
  1134. printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
  1135. old_pool_size, new_pool_size);
  1136. btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
  1137. btrfs_set_max_workers(&fs_info->workers, new_pool_size);
  1138. btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
  1139. btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
  1140. btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
  1141. btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
  1142. btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
  1143. btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
  1144. btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
  1145. btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
  1146. btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
  1147. btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
  1148. btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
  1149. btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
  1150. new_pool_size);
  1151. }
  1152. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1153. {
  1154. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1155. }
  1156. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1157. unsigned long old_opts, int flags)
  1158. {
  1159. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1160. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1161. (flags & MS_RDONLY))) {
  1162. /* wait for any defraggers to finish */
  1163. wait_event(fs_info->transaction_wait,
  1164. (atomic_read(&fs_info->defrag_running) == 0));
  1165. if (flags & MS_RDONLY)
  1166. sync_filesystem(fs_info->sb);
  1167. }
  1168. }
  1169. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1170. unsigned long old_opts)
  1171. {
  1172. /*
  1173. * We need cleanup all defragable inodes if the autodefragment is
  1174. * close or the fs is R/O.
  1175. */
  1176. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1177. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1178. (fs_info->sb->s_flags & MS_RDONLY))) {
  1179. btrfs_cleanup_defrag_inodes(fs_info);
  1180. }
  1181. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1182. }
  1183. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1184. {
  1185. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1186. struct btrfs_root *root = fs_info->tree_root;
  1187. unsigned old_flags = sb->s_flags;
  1188. unsigned long old_opts = fs_info->mount_opt;
  1189. unsigned long old_compress_type = fs_info->compress_type;
  1190. u64 old_max_inline = fs_info->max_inline;
  1191. u64 old_alloc_start = fs_info->alloc_start;
  1192. int old_thread_pool_size = fs_info->thread_pool_size;
  1193. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1194. int ret;
  1195. btrfs_remount_prepare(fs_info);
  1196. ret = btrfs_parse_options(root, data);
  1197. if (ret) {
  1198. ret = -EINVAL;
  1199. goto restore;
  1200. }
  1201. btrfs_remount_begin(fs_info, old_opts, *flags);
  1202. btrfs_resize_thread_pool(fs_info,
  1203. fs_info->thread_pool_size, old_thread_pool_size);
  1204. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1205. goto out;
  1206. if (*flags & MS_RDONLY) {
  1207. /*
  1208. * this also happens on 'umount -rf' or on shutdown, when
  1209. * the filesystem is busy.
  1210. */
  1211. sb->s_flags |= MS_RDONLY;
  1212. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1213. btrfs_scrub_cancel(fs_info);
  1214. btrfs_pause_balance(fs_info);
  1215. ret = btrfs_commit_super(root);
  1216. if (ret)
  1217. goto restore;
  1218. } else {
  1219. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1220. btrfs_err(fs_info,
  1221. "Remounting read-write after error is not allowed\n");
  1222. ret = -EINVAL;
  1223. goto restore;
  1224. }
  1225. if (fs_info->fs_devices->rw_devices == 0) {
  1226. ret = -EACCES;
  1227. goto restore;
  1228. }
  1229. if (fs_info->fs_devices->missing_devices >
  1230. fs_info->num_tolerated_disk_barrier_failures &&
  1231. !(*flags & MS_RDONLY)) {
  1232. printk(KERN_WARNING
  1233. "Btrfs: too many missing devices, writeable remount is not allowed\n");
  1234. ret = -EACCES;
  1235. goto restore;
  1236. }
  1237. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1238. ret = -EINVAL;
  1239. goto restore;
  1240. }
  1241. ret = btrfs_cleanup_fs_roots(fs_info);
  1242. if (ret)
  1243. goto restore;
  1244. /* recover relocation */
  1245. ret = btrfs_recover_relocation(root);
  1246. if (ret)
  1247. goto restore;
  1248. ret = btrfs_resume_balance_async(fs_info);
  1249. if (ret)
  1250. goto restore;
  1251. ret = btrfs_resume_dev_replace_async(fs_info);
  1252. if (ret) {
  1253. pr_warn("btrfs: failed to resume dev_replace\n");
  1254. goto restore;
  1255. }
  1256. if (!fs_info->uuid_root) {
  1257. pr_info("btrfs: creating UUID tree\n");
  1258. ret = btrfs_create_uuid_tree(fs_info);
  1259. if (ret) {
  1260. pr_warn("btrfs: failed to create the uuid tree"
  1261. "%d\n", ret);
  1262. goto restore;
  1263. }
  1264. }
  1265. sb->s_flags &= ~MS_RDONLY;
  1266. }
  1267. out:
  1268. btrfs_remount_cleanup(fs_info, old_opts);
  1269. return 0;
  1270. restore:
  1271. /* We've hit an error - don't reset MS_RDONLY */
  1272. if (sb->s_flags & MS_RDONLY)
  1273. old_flags |= MS_RDONLY;
  1274. sb->s_flags = old_flags;
  1275. fs_info->mount_opt = old_opts;
  1276. fs_info->compress_type = old_compress_type;
  1277. fs_info->max_inline = old_max_inline;
  1278. mutex_lock(&fs_info->chunk_mutex);
  1279. fs_info->alloc_start = old_alloc_start;
  1280. mutex_unlock(&fs_info->chunk_mutex);
  1281. btrfs_resize_thread_pool(fs_info,
  1282. old_thread_pool_size, fs_info->thread_pool_size);
  1283. fs_info->metadata_ratio = old_metadata_ratio;
  1284. btrfs_remount_cleanup(fs_info, old_opts);
  1285. return ret;
  1286. }
  1287. /* Used to sort the devices by max_avail(descending sort) */
  1288. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1289. const void *dev_info2)
  1290. {
  1291. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1292. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1293. return -1;
  1294. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1295. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1296. return 1;
  1297. else
  1298. return 0;
  1299. }
  1300. /*
  1301. * sort the devices by max_avail, in which max free extent size of each device
  1302. * is stored.(Descending Sort)
  1303. */
  1304. static inline void btrfs_descending_sort_devices(
  1305. struct btrfs_device_info *devices,
  1306. size_t nr_devices)
  1307. {
  1308. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1309. btrfs_cmp_device_free_bytes, NULL);
  1310. }
  1311. /*
  1312. * The helper to calc the free space on the devices that can be used to store
  1313. * file data.
  1314. */
  1315. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1316. {
  1317. struct btrfs_fs_info *fs_info = root->fs_info;
  1318. struct btrfs_device_info *devices_info;
  1319. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1320. struct btrfs_device *device;
  1321. u64 skip_space;
  1322. u64 type;
  1323. u64 avail_space;
  1324. u64 used_space;
  1325. u64 min_stripe_size;
  1326. int min_stripes = 1, num_stripes = 1;
  1327. int i = 0, nr_devices;
  1328. int ret;
  1329. nr_devices = fs_info->fs_devices->open_devices;
  1330. BUG_ON(!nr_devices);
  1331. devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
  1332. GFP_NOFS);
  1333. if (!devices_info)
  1334. return -ENOMEM;
  1335. /* calc min stripe number for data space alloction */
  1336. type = btrfs_get_alloc_profile(root, 1);
  1337. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1338. min_stripes = 2;
  1339. num_stripes = nr_devices;
  1340. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1341. min_stripes = 2;
  1342. num_stripes = 2;
  1343. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1344. min_stripes = 4;
  1345. num_stripes = 4;
  1346. }
  1347. if (type & BTRFS_BLOCK_GROUP_DUP)
  1348. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1349. else
  1350. min_stripe_size = BTRFS_STRIPE_LEN;
  1351. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  1352. if (!device->in_fs_metadata || !device->bdev ||
  1353. device->is_tgtdev_for_dev_replace)
  1354. continue;
  1355. avail_space = device->total_bytes - device->bytes_used;
  1356. /* align with stripe_len */
  1357. do_div(avail_space, BTRFS_STRIPE_LEN);
  1358. avail_space *= BTRFS_STRIPE_LEN;
  1359. /*
  1360. * In order to avoid overwritting the superblock on the drive,
  1361. * btrfs starts at an offset of at least 1MB when doing chunk
  1362. * allocation.
  1363. */
  1364. skip_space = 1024 * 1024;
  1365. /* user can set the offset in fs_info->alloc_start. */
  1366. if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1367. device->total_bytes)
  1368. skip_space = max(fs_info->alloc_start, skip_space);
  1369. /*
  1370. * btrfs can not use the free space in [0, skip_space - 1],
  1371. * we must subtract it from the total. In order to implement
  1372. * it, we account the used space in this range first.
  1373. */
  1374. ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
  1375. &used_space);
  1376. if (ret) {
  1377. kfree(devices_info);
  1378. return ret;
  1379. }
  1380. /* calc the free space in [0, skip_space - 1] */
  1381. skip_space -= used_space;
  1382. /*
  1383. * we can use the free space in [0, skip_space - 1], subtract
  1384. * it from the total.
  1385. */
  1386. if (avail_space && avail_space >= skip_space)
  1387. avail_space -= skip_space;
  1388. else
  1389. avail_space = 0;
  1390. if (avail_space < min_stripe_size)
  1391. continue;
  1392. devices_info[i].dev = device;
  1393. devices_info[i].max_avail = avail_space;
  1394. i++;
  1395. }
  1396. nr_devices = i;
  1397. btrfs_descending_sort_devices(devices_info, nr_devices);
  1398. i = nr_devices - 1;
  1399. avail_space = 0;
  1400. while (nr_devices >= min_stripes) {
  1401. if (num_stripes > nr_devices)
  1402. num_stripes = nr_devices;
  1403. if (devices_info[i].max_avail >= min_stripe_size) {
  1404. int j;
  1405. u64 alloc_size;
  1406. avail_space += devices_info[i].max_avail * num_stripes;
  1407. alloc_size = devices_info[i].max_avail;
  1408. for (j = i + 1 - num_stripes; j <= i; j++)
  1409. devices_info[j].max_avail -= alloc_size;
  1410. }
  1411. i--;
  1412. nr_devices--;
  1413. }
  1414. kfree(devices_info);
  1415. *free_bytes = avail_space;
  1416. return 0;
  1417. }
  1418. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1419. {
  1420. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1421. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1422. struct list_head *head = &fs_info->space_info;
  1423. struct btrfs_space_info *found;
  1424. u64 total_used = 0;
  1425. u64 total_free_data = 0;
  1426. int bits = dentry->d_sb->s_blocksize_bits;
  1427. __be32 *fsid = (__be32 *)fs_info->fsid;
  1428. int ret;
  1429. /* holding chunk_muext to avoid allocating new chunks */
  1430. mutex_lock(&fs_info->chunk_mutex);
  1431. rcu_read_lock();
  1432. list_for_each_entry_rcu(found, head, list) {
  1433. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1434. total_free_data += found->disk_total - found->disk_used;
  1435. total_free_data -=
  1436. btrfs_account_ro_block_groups_free_space(found);
  1437. }
  1438. total_used += found->disk_used;
  1439. }
  1440. rcu_read_unlock();
  1441. buf->f_namelen = BTRFS_NAME_LEN;
  1442. buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
  1443. buf->f_bfree = buf->f_blocks - (total_used >> bits);
  1444. buf->f_bsize = dentry->d_sb->s_blocksize;
  1445. buf->f_type = BTRFS_SUPER_MAGIC;
  1446. buf->f_bavail = total_free_data;
  1447. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1448. if (ret) {
  1449. mutex_unlock(&fs_info->chunk_mutex);
  1450. return ret;
  1451. }
  1452. buf->f_bavail += total_free_data;
  1453. buf->f_bavail = buf->f_bavail >> bits;
  1454. mutex_unlock(&fs_info->chunk_mutex);
  1455. /* We treat it as constant endianness (it doesn't matter _which_)
  1456. because we want the fsid to come out the same whether mounted
  1457. on a big-endian or little-endian host */
  1458. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1459. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1460. /* Mask in the root object ID too, to disambiguate subvols */
  1461. buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
  1462. buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
  1463. return 0;
  1464. }
  1465. static void btrfs_kill_super(struct super_block *sb)
  1466. {
  1467. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1468. kill_anon_super(sb);
  1469. free_fs_info(fs_info);
  1470. }
  1471. static struct file_system_type btrfs_fs_type = {
  1472. .owner = THIS_MODULE,
  1473. .name = "btrfs",
  1474. .mount = btrfs_mount,
  1475. .kill_sb = btrfs_kill_super,
  1476. .fs_flags = FS_REQUIRES_DEV,
  1477. };
  1478. MODULE_ALIAS_FS("btrfs");
  1479. /*
  1480. * used by btrfsctl to scan devices when no FS is mounted
  1481. */
  1482. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1483. unsigned long arg)
  1484. {
  1485. struct btrfs_ioctl_vol_args *vol;
  1486. struct btrfs_fs_devices *fs_devices;
  1487. int ret = -ENOTTY;
  1488. if (!capable(CAP_SYS_ADMIN))
  1489. return -EPERM;
  1490. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1491. if (IS_ERR(vol))
  1492. return PTR_ERR(vol);
  1493. switch (cmd) {
  1494. case BTRFS_IOC_SCAN_DEV:
  1495. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1496. &btrfs_fs_type, &fs_devices);
  1497. break;
  1498. case BTRFS_IOC_DEVICES_READY:
  1499. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1500. &btrfs_fs_type, &fs_devices);
  1501. if (ret)
  1502. break;
  1503. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  1504. break;
  1505. }
  1506. kfree(vol);
  1507. return ret;
  1508. }
  1509. static int btrfs_freeze(struct super_block *sb)
  1510. {
  1511. struct btrfs_trans_handle *trans;
  1512. struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  1513. trans = btrfs_attach_transaction_barrier(root);
  1514. if (IS_ERR(trans)) {
  1515. /* no transaction, don't bother */
  1516. if (PTR_ERR(trans) == -ENOENT)
  1517. return 0;
  1518. return PTR_ERR(trans);
  1519. }
  1520. return btrfs_commit_transaction(trans, root);
  1521. }
  1522. static int btrfs_unfreeze(struct super_block *sb)
  1523. {
  1524. return 0;
  1525. }
  1526. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  1527. {
  1528. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  1529. struct btrfs_fs_devices *cur_devices;
  1530. struct btrfs_device *dev, *first_dev = NULL;
  1531. struct list_head *head;
  1532. struct rcu_string *name;
  1533. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1534. cur_devices = fs_info->fs_devices;
  1535. while (cur_devices) {
  1536. head = &cur_devices->devices;
  1537. list_for_each_entry(dev, head, dev_list) {
  1538. if (dev->missing)
  1539. continue;
  1540. if (!first_dev || dev->devid < first_dev->devid)
  1541. first_dev = dev;
  1542. }
  1543. cur_devices = cur_devices->seed;
  1544. }
  1545. if (first_dev) {
  1546. rcu_read_lock();
  1547. name = rcu_dereference(first_dev->name);
  1548. seq_escape(m, name->str, " \t\n\\");
  1549. rcu_read_unlock();
  1550. } else {
  1551. WARN_ON(1);
  1552. }
  1553. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1554. return 0;
  1555. }
  1556. static const struct super_operations btrfs_super_ops = {
  1557. .drop_inode = btrfs_drop_inode,
  1558. .evict_inode = btrfs_evict_inode,
  1559. .put_super = btrfs_put_super,
  1560. .sync_fs = btrfs_sync_fs,
  1561. .show_options = btrfs_show_options,
  1562. .show_devname = btrfs_show_devname,
  1563. .write_inode = btrfs_write_inode,
  1564. .alloc_inode = btrfs_alloc_inode,
  1565. .destroy_inode = btrfs_destroy_inode,
  1566. .statfs = btrfs_statfs,
  1567. .remount_fs = btrfs_remount,
  1568. .freeze_fs = btrfs_freeze,
  1569. .unfreeze_fs = btrfs_unfreeze,
  1570. };
  1571. static const struct file_operations btrfs_ctl_fops = {
  1572. .unlocked_ioctl = btrfs_control_ioctl,
  1573. .compat_ioctl = btrfs_control_ioctl,
  1574. .owner = THIS_MODULE,
  1575. .llseek = noop_llseek,
  1576. };
  1577. static struct miscdevice btrfs_misc = {
  1578. .minor = BTRFS_MINOR,
  1579. .name = "btrfs-control",
  1580. .fops = &btrfs_ctl_fops
  1581. };
  1582. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  1583. MODULE_ALIAS("devname:btrfs-control");
  1584. static int btrfs_interface_init(void)
  1585. {
  1586. return misc_register(&btrfs_misc);
  1587. }
  1588. static void btrfs_interface_exit(void)
  1589. {
  1590. if (misc_deregister(&btrfs_misc) < 0)
  1591. printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
  1592. }
  1593. static void btrfs_print_info(void)
  1594. {
  1595. printk(KERN_INFO "Btrfs loaded"
  1596. #ifdef CONFIG_BTRFS_DEBUG
  1597. ", debug=on"
  1598. #endif
  1599. #ifdef CONFIG_BTRFS_ASSERT
  1600. ", assert=on"
  1601. #endif
  1602. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1603. ", integrity-checker=on"
  1604. #endif
  1605. "\n");
  1606. }
  1607. static int btrfs_run_sanity_tests(void)
  1608. {
  1609. return btrfs_test_free_space_cache();
  1610. }
  1611. static int __init init_btrfs_fs(void)
  1612. {
  1613. int err;
  1614. err = btrfs_init_sysfs();
  1615. if (err)
  1616. return err;
  1617. btrfs_init_compress();
  1618. err = btrfs_init_cachep();
  1619. if (err)
  1620. goto free_compress;
  1621. err = extent_io_init();
  1622. if (err)
  1623. goto free_cachep;
  1624. err = extent_map_init();
  1625. if (err)
  1626. goto free_extent_io;
  1627. err = ordered_data_init();
  1628. if (err)
  1629. goto free_extent_map;
  1630. err = btrfs_delayed_inode_init();
  1631. if (err)
  1632. goto free_ordered_data;
  1633. err = btrfs_auto_defrag_init();
  1634. if (err)
  1635. goto free_delayed_inode;
  1636. err = btrfs_delayed_ref_init();
  1637. if (err)
  1638. goto free_auto_defrag;
  1639. err = btrfs_prelim_ref_init();
  1640. if (err)
  1641. goto free_prelim_ref;
  1642. err = btrfs_interface_init();
  1643. if (err)
  1644. goto free_delayed_ref;
  1645. btrfs_init_lockdep();
  1646. btrfs_print_info();
  1647. err = btrfs_run_sanity_tests();
  1648. if (err)
  1649. goto unregister_ioctl;
  1650. err = register_filesystem(&btrfs_fs_type);
  1651. if (err)
  1652. goto unregister_ioctl;
  1653. return 0;
  1654. unregister_ioctl:
  1655. btrfs_interface_exit();
  1656. free_prelim_ref:
  1657. btrfs_prelim_ref_exit();
  1658. free_delayed_ref:
  1659. btrfs_delayed_ref_exit();
  1660. free_auto_defrag:
  1661. btrfs_auto_defrag_exit();
  1662. free_delayed_inode:
  1663. btrfs_delayed_inode_exit();
  1664. free_ordered_data:
  1665. ordered_data_exit();
  1666. free_extent_map:
  1667. extent_map_exit();
  1668. free_extent_io:
  1669. extent_io_exit();
  1670. free_cachep:
  1671. btrfs_destroy_cachep();
  1672. free_compress:
  1673. btrfs_exit_compress();
  1674. btrfs_exit_sysfs();
  1675. return err;
  1676. }
  1677. static void __exit exit_btrfs_fs(void)
  1678. {
  1679. btrfs_destroy_cachep();
  1680. btrfs_delayed_ref_exit();
  1681. btrfs_auto_defrag_exit();
  1682. btrfs_delayed_inode_exit();
  1683. btrfs_prelim_ref_exit();
  1684. ordered_data_exit();
  1685. extent_map_exit();
  1686. extent_io_exit();
  1687. btrfs_interface_exit();
  1688. unregister_filesystem(&btrfs_fs_type);
  1689. btrfs_exit_sysfs();
  1690. btrfs_cleanup_fs_uuids();
  1691. btrfs_exit_compress();
  1692. }
  1693. module_init(init_btrfs_fs)
  1694. module_exit(exit_btrfs_fs)
  1695. MODULE_LICENSE("GPL");