super.c 43 KB

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