super.c 43 KB

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