super.c 49 KB

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