super.c 41 KB

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