super.c 42 KB

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