super.c 47 KB

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