super.c 44 KB

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