super.c 45 KB

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