super.c 52 KB

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