super.c 40 KB

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