super.c 47 KB

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