super.c 43 KB

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