super.c 50 KB

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