super.c 45 KB

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