super.c 42 KB

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