super.c 52 KB

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