super.c 41 KB

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