super.c 47 KB

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