super.c 43 KB

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