super.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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 "hash.h"
  51. #include "props.h"
  52. #include "xattr.h"
  53. #include "volumes.h"
  54. #include "export.h"
  55. #include "compression.h"
  56. #include "rcu-string.h"
  57. #include "dev-replace.h"
  58. #include "free-space-cache.h"
  59. #include "backref.h"
  60. #include "tests/btrfs-tests.h"
  61. #include "qgroup.h"
  62. #define CREATE_TRACE_POINTS
  63. #include <trace/events/btrfs.h>
  64. static const struct super_operations btrfs_super_ops;
  65. static struct file_system_type btrfs_fs_type;
  66. static int btrfs_remount(struct super_block *sb, int *flags, char *data);
  67. const char *btrfs_decode_error(int errno)
  68. {
  69. char *errstr = "unknown";
  70. switch (errno) {
  71. case -EIO:
  72. errstr = "IO failure";
  73. break;
  74. case -ENOMEM:
  75. errstr = "Out of memory";
  76. break;
  77. case -EROFS:
  78. errstr = "Readonly filesystem";
  79. break;
  80. case -EEXIST:
  81. errstr = "Object already exists";
  82. break;
  83. case -ENOSPC:
  84. errstr = "No space left";
  85. break;
  86. case -ENOENT:
  87. errstr = "No such entry";
  88. break;
  89. }
  90. return errstr;
  91. }
  92. /* btrfs handle error by forcing the filesystem readonly */
  93. static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  94. {
  95. struct super_block *sb = fs_info->sb;
  96. if (sb->s_flags & MS_RDONLY)
  97. return;
  98. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  99. sb->s_flags |= MS_RDONLY;
  100. btrfs_info(fs_info, "forced readonly");
  101. /*
  102. * Note that a running device replace operation is not
  103. * canceled here although there is no way to update
  104. * the progress. It would add the risk of a deadlock,
  105. * therefore the canceling is omitted. The only penalty
  106. * is that some I/O remains active until the procedure
  107. * completes. The next time when the filesystem is
  108. * mounted writeable again, the device replace
  109. * operation continues.
  110. */
  111. }
  112. }
  113. /*
  114. * __btrfs_handle_fs_error decodes expected errors from the caller and
  115. * invokes the approciate error response.
  116. */
  117. __cold
  118. void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
  119. unsigned int line, int errno, const char *fmt, ...)
  120. {
  121. struct super_block *sb = fs_info->sb;
  122. #ifdef CONFIG_PRINTK
  123. const char *errstr;
  124. #endif
  125. /*
  126. * Special case: if the error is EROFS, and we're already
  127. * under MS_RDONLY, then it is safe here.
  128. */
  129. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  130. return;
  131. #ifdef CONFIG_PRINTK
  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
  140. "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. #endif
  148. /*
  149. * Today we only save the error info to memory. Long term we'll
  150. * also send it down to the disk
  151. */
  152. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  153. /* Don't go through full error handling during mount */
  154. if (sb->s_flags & MS_BORN)
  155. btrfs_handle_error(fs_info);
  156. }
  157. #ifdef CONFIG_PRINTK
  158. static const char * const logtypes[] = {
  159. "emergency",
  160. "alert",
  161. "critical",
  162. "error",
  163. "warning",
  164. "notice",
  165. "info",
  166. "debug",
  167. };
  168. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  169. {
  170. struct super_block *sb = fs_info->sb;
  171. char lvl[4];
  172. struct va_format vaf;
  173. va_list args;
  174. const char *type = logtypes[4];
  175. int kern_level;
  176. va_start(args, fmt);
  177. kern_level = printk_get_level(fmt);
  178. if (kern_level) {
  179. size_t size = printk_skip_level(fmt) - fmt;
  180. memcpy(lvl, fmt, size);
  181. lvl[size] = '\0';
  182. fmt += size;
  183. type = logtypes[kern_level - '0'];
  184. } else
  185. *lvl = '\0';
  186. vaf.fmt = fmt;
  187. vaf.va = &args;
  188. printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
  189. va_end(args);
  190. }
  191. #endif
  192. /*
  193. * We only mark the transaction aborted and then set the file system read-only.
  194. * This will prevent new transactions from starting or trying to join this
  195. * one.
  196. *
  197. * This means that error recovery at the call site is limited to freeing
  198. * any local memory allocations and passing the error code up without
  199. * further cleanup. The transaction should complete as it normally would
  200. * in the call path but will return -EIO.
  201. *
  202. * We'll complete the cleanup in btrfs_end_transaction and
  203. * btrfs_commit_transaction.
  204. */
  205. __cold
  206. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  207. struct btrfs_root *root, const char *function,
  208. unsigned int line, int errno)
  209. {
  210. trans->aborted = errno;
  211. /* Nothing used. The other threads that have joined this
  212. * transaction may be able to continue. */
  213. if (!trans->blocks_used && list_empty(&trans->new_bgs)) {
  214. const char *errstr;
  215. errstr = btrfs_decode_error(errno);
  216. btrfs_warn(root->fs_info,
  217. "%s:%d: Aborting unused transaction(%s).",
  218. function, line, errstr);
  219. return;
  220. }
  221. ACCESS_ONCE(trans->transaction->aborted) = errno;
  222. /* Wake up anybody who may be waiting on this transaction */
  223. wake_up(&root->fs_info->transaction_wait);
  224. wake_up(&root->fs_info->transaction_blocked_wait);
  225. __btrfs_handle_fs_error(root->fs_info, function, line, errno, NULL);
  226. }
  227. /*
  228. * __btrfs_panic decodes unexpected, fatal errors from the caller,
  229. * issues an alert, and either panics or BUGs, depending on mount options.
  230. */
  231. __cold
  232. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  233. unsigned int line, int errno, const char *fmt, ...)
  234. {
  235. char *s_id = "<unknown>";
  236. const char *errstr;
  237. struct va_format vaf = { .fmt = fmt };
  238. va_list args;
  239. if (fs_info)
  240. s_id = fs_info->sb->s_id;
  241. va_start(args, fmt);
  242. vaf.va = &args;
  243. errstr = btrfs_decode_error(errno);
  244. if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
  245. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  246. s_id, function, line, &vaf, errno, errstr);
  247. btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  248. function, line, &vaf, errno, errstr);
  249. va_end(args);
  250. /* Caller calls BUG() */
  251. }
  252. static void btrfs_put_super(struct super_block *sb)
  253. {
  254. close_ctree(btrfs_sb(sb)->tree_root);
  255. }
  256. enum {
  257. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  258. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  259. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  260. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  261. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  262. Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
  263. Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
  264. Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
  265. Opt_skip_balance, Opt_check_integrity,
  266. Opt_check_integrity_including_extent_data,
  267. Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
  268. Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
  269. Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
  270. Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
  271. Opt_nologreplay, Opt_norecovery,
  272. #ifdef CONFIG_BTRFS_DEBUG
  273. Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
  274. #endif
  275. Opt_err,
  276. };
  277. static const match_table_t tokens = {
  278. {Opt_degraded, "degraded"},
  279. {Opt_subvol, "subvol=%s"},
  280. {Opt_subvolid, "subvolid=%s"},
  281. {Opt_device, "device=%s"},
  282. {Opt_nodatasum, "nodatasum"},
  283. {Opt_datasum, "datasum"},
  284. {Opt_nodatacow, "nodatacow"},
  285. {Opt_datacow, "datacow"},
  286. {Opt_nobarrier, "nobarrier"},
  287. {Opt_barrier, "barrier"},
  288. {Opt_max_inline, "max_inline=%s"},
  289. {Opt_alloc_start, "alloc_start=%s"},
  290. {Opt_thread_pool, "thread_pool=%d"},
  291. {Opt_compress, "compress"},
  292. {Opt_compress_type, "compress=%s"},
  293. {Opt_compress_force, "compress-force"},
  294. {Opt_compress_force_type, "compress-force=%s"},
  295. {Opt_ssd, "ssd"},
  296. {Opt_ssd_spread, "ssd_spread"},
  297. {Opt_nossd, "nossd"},
  298. {Opt_acl, "acl"},
  299. {Opt_noacl, "noacl"},
  300. {Opt_notreelog, "notreelog"},
  301. {Opt_treelog, "treelog"},
  302. {Opt_nologreplay, "nologreplay"},
  303. {Opt_norecovery, "norecovery"},
  304. {Opt_flushoncommit, "flushoncommit"},
  305. {Opt_noflushoncommit, "noflushoncommit"},
  306. {Opt_ratio, "metadata_ratio=%d"},
  307. {Opt_discard, "discard"},
  308. {Opt_nodiscard, "nodiscard"},
  309. {Opt_space_cache, "space_cache"},
  310. {Opt_space_cache_version, "space_cache=%s"},
  311. {Opt_clear_cache, "clear_cache"},
  312. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  313. {Opt_enospc_debug, "enospc_debug"},
  314. {Opt_noenospc_debug, "noenospc_debug"},
  315. {Opt_subvolrootid, "subvolrootid=%d"},
  316. {Opt_defrag, "autodefrag"},
  317. {Opt_nodefrag, "noautodefrag"},
  318. {Opt_inode_cache, "inode_cache"},
  319. {Opt_noinode_cache, "noinode_cache"},
  320. {Opt_no_space_cache, "nospace_cache"},
  321. {Opt_recovery, "recovery"}, /* deprecated */
  322. {Opt_usebackuproot, "usebackuproot"},
  323. {Opt_skip_balance, "skip_balance"},
  324. {Opt_check_integrity, "check_int"},
  325. {Opt_check_integrity_including_extent_data, "check_int_data"},
  326. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  327. {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
  328. {Opt_fatal_errors, "fatal_errors=%s"},
  329. {Opt_commit_interval, "commit=%d"},
  330. #ifdef CONFIG_BTRFS_DEBUG
  331. {Opt_fragment_data, "fragment=data"},
  332. {Opt_fragment_metadata, "fragment=metadata"},
  333. {Opt_fragment_all, "fragment=all"},
  334. #endif
  335. {Opt_err, NULL},
  336. };
  337. /*
  338. * Regular mount options parser. Everything that is needed only when
  339. * reading in a new superblock is parsed here.
  340. * XXX JDM: This needs to be cleaned up for remount.
  341. */
  342. int btrfs_parse_options(struct btrfs_root *root, char *options,
  343. unsigned long new_flags)
  344. {
  345. struct btrfs_fs_info *info = root->fs_info;
  346. substring_t args[MAX_OPT_ARGS];
  347. char *p, *num, *orig = NULL;
  348. u64 cache_gen;
  349. int intarg;
  350. int ret = 0;
  351. char *compress_type;
  352. bool compress_force = false;
  353. enum btrfs_compression_type saved_compress_type;
  354. bool saved_compress_force;
  355. int no_compress = 0;
  356. cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
  357. if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE))
  358. btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
  359. else if (cache_gen)
  360. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  361. /*
  362. * Even the options are empty, we still need to do extra check
  363. * against new flags
  364. */
  365. if (!options)
  366. goto check;
  367. /*
  368. * strsep changes the string, duplicate it because parse_options
  369. * gets called twice
  370. */
  371. options = kstrdup(options, GFP_NOFS);
  372. if (!options)
  373. return -ENOMEM;
  374. orig = options;
  375. while ((p = strsep(&options, ",")) != NULL) {
  376. int token;
  377. if (!*p)
  378. continue;
  379. token = match_token(p, tokens, args);
  380. switch (token) {
  381. case Opt_degraded:
  382. btrfs_info(root->fs_info, "allowing degraded mounts");
  383. btrfs_set_opt(info->mount_opt, DEGRADED);
  384. break;
  385. case Opt_subvol:
  386. case Opt_subvolid:
  387. case Opt_subvolrootid:
  388. case Opt_device:
  389. /*
  390. * These are parsed by btrfs_parse_early_options
  391. * and can be happily ignored here.
  392. */
  393. break;
  394. case Opt_nodatasum:
  395. btrfs_set_and_info(root, NODATASUM,
  396. "setting nodatasum");
  397. break;
  398. case Opt_datasum:
  399. if (btrfs_test_opt(root, NODATASUM)) {
  400. if (btrfs_test_opt(root, NODATACOW))
  401. btrfs_info(root->fs_info, "setting datasum, datacow enabled");
  402. else
  403. btrfs_info(root->fs_info, "setting datasum");
  404. }
  405. btrfs_clear_opt(info->mount_opt, NODATACOW);
  406. btrfs_clear_opt(info->mount_opt, NODATASUM);
  407. break;
  408. case Opt_nodatacow:
  409. if (!btrfs_test_opt(root, NODATACOW)) {
  410. if (!btrfs_test_opt(root, COMPRESS) ||
  411. !btrfs_test_opt(root, FORCE_COMPRESS)) {
  412. btrfs_info(root->fs_info,
  413. "setting nodatacow, compression disabled");
  414. } else {
  415. btrfs_info(root->fs_info, "setting nodatacow");
  416. }
  417. }
  418. btrfs_clear_opt(info->mount_opt, COMPRESS);
  419. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  420. btrfs_set_opt(info->mount_opt, NODATACOW);
  421. btrfs_set_opt(info->mount_opt, NODATASUM);
  422. break;
  423. case Opt_datacow:
  424. btrfs_clear_and_info(root, NODATACOW,
  425. "setting datacow");
  426. break;
  427. case Opt_compress_force:
  428. case Opt_compress_force_type:
  429. compress_force = true;
  430. /* Fallthrough */
  431. case Opt_compress:
  432. case Opt_compress_type:
  433. saved_compress_type = btrfs_test_opt(root, COMPRESS) ?
  434. info->compress_type : BTRFS_COMPRESS_NONE;
  435. saved_compress_force =
  436. btrfs_test_opt(root, FORCE_COMPRESS);
  437. if (token == Opt_compress ||
  438. token == Opt_compress_force ||
  439. strcmp(args[0].from, "zlib") == 0) {
  440. compress_type = "zlib";
  441. info->compress_type = BTRFS_COMPRESS_ZLIB;
  442. btrfs_set_opt(info->mount_opt, COMPRESS);
  443. btrfs_clear_opt(info->mount_opt, NODATACOW);
  444. btrfs_clear_opt(info->mount_opt, NODATASUM);
  445. no_compress = 0;
  446. } else if (strcmp(args[0].from, "lzo") == 0) {
  447. compress_type = "lzo";
  448. info->compress_type = BTRFS_COMPRESS_LZO;
  449. btrfs_set_opt(info->mount_opt, COMPRESS);
  450. btrfs_clear_opt(info->mount_opt, NODATACOW);
  451. btrfs_clear_opt(info->mount_opt, NODATASUM);
  452. btrfs_set_fs_incompat(info, COMPRESS_LZO);
  453. no_compress = 0;
  454. } else if (strncmp(args[0].from, "no", 2) == 0) {
  455. compress_type = "no";
  456. btrfs_clear_opt(info->mount_opt, COMPRESS);
  457. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  458. compress_force = false;
  459. no_compress++;
  460. } else {
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. if (compress_force) {
  465. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  466. } else {
  467. /*
  468. * If we remount from compress-force=xxx to
  469. * compress=xxx, we need clear FORCE_COMPRESS
  470. * flag, otherwise, there is no way for users
  471. * to disable forcible compression separately.
  472. */
  473. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  474. }
  475. if ((btrfs_test_opt(root, COMPRESS) &&
  476. (info->compress_type != saved_compress_type ||
  477. compress_force != saved_compress_force)) ||
  478. (!btrfs_test_opt(root, COMPRESS) &&
  479. no_compress == 1)) {
  480. btrfs_info(root->fs_info,
  481. "%s %s compression",
  482. (compress_force) ? "force" : "use",
  483. compress_type);
  484. }
  485. compress_force = false;
  486. break;
  487. case Opt_ssd:
  488. btrfs_set_and_info(root, SSD,
  489. "use ssd allocation scheme");
  490. break;
  491. case Opt_ssd_spread:
  492. btrfs_set_and_info(root, SSD_SPREAD,
  493. "use spread ssd allocation scheme");
  494. btrfs_set_opt(info->mount_opt, SSD);
  495. break;
  496. case Opt_nossd:
  497. btrfs_set_and_info(root, NOSSD,
  498. "not using ssd allocation scheme");
  499. btrfs_clear_opt(info->mount_opt, SSD);
  500. break;
  501. case Opt_barrier:
  502. btrfs_clear_and_info(root, NOBARRIER,
  503. "turning on barriers");
  504. break;
  505. case Opt_nobarrier:
  506. btrfs_set_and_info(root, NOBARRIER,
  507. "turning off barriers");
  508. break;
  509. case Opt_thread_pool:
  510. ret = match_int(&args[0], &intarg);
  511. if (ret) {
  512. goto out;
  513. } else if (intarg > 0) {
  514. info->thread_pool_size = intarg;
  515. } else {
  516. ret = -EINVAL;
  517. goto out;
  518. }
  519. break;
  520. case Opt_max_inline:
  521. num = match_strdup(&args[0]);
  522. if (num) {
  523. info->max_inline = memparse(num, NULL);
  524. kfree(num);
  525. if (info->max_inline) {
  526. info->max_inline = min_t(u64,
  527. info->max_inline,
  528. root->sectorsize);
  529. }
  530. btrfs_info(root->fs_info, "max_inline at %llu",
  531. info->max_inline);
  532. } else {
  533. ret = -ENOMEM;
  534. goto out;
  535. }
  536. break;
  537. case Opt_alloc_start:
  538. num = match_strdup(&args[0]);
  539. if (num) {
  540. mutex_lock(&info->chunk_mutex);
  541. info->alloc_start = memparse(num, NULL);
  542. mutex_unlock(&info->chunk_mutex);
  543. kfree(num);
  544. btrfs_info(root->fs_info, "allocations start at %llu",
  545. info->alloc_start);
  546. } else {
  547. ret = -ENOMEM;
  548. goto out;
  549. }
  550. break;
  551. case Opt_acl:
  552. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  553. root->fs_info->sb->s_flags |= MS_POSIXACL;
  554. break;
  555. #else
  556. btrfs_err(root->fs_info,
  557. "support for ACL not compiled in!");
  558. ret = -EINVAL;
  559. goto out;
  560. #endif
  561. case Opt_noacl:
  562. root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  563. break;
  564. case Opt_notreelog:
  565. btrfs_set_and_info(root, NOTREELOG,
  566. "disabling tree log");
  567. break;
  568. case Opt_treelog:
  569. btrfs_clear_and_info(root, NOTREELOG,
  570. "enabling tree log");
  571. break;
  572. case Opt_norecovery:
  573. case Opt_nologreplay:
  574. btrfs_set_and_info(root, NOLOGREPLAY,
  575. "disabling log replay at mount time");
  576. break;
  577. case Opt_flushoncommit:
  578. btrfs_set_and_info(root, FLUSHONCOMMIT,
  579. "turning on flush-on-commit");
  580. break;
  581. case Opt_noflushoncommit:
  582. btrfs_clear_and_info(root, FLUSHONCOMMIT,
  583. "turning off flush-on-commit");
  584. break;
  585. case Opt_ratio:
  586. ret = match_int(&args[0], &intarg);
  587. if (ret) {
  588. goto out;
  589. } else if (intarg >= 0) {
  590. info->metadata_ratio = intarg;
  591. btrfs_info(root->fs_info, "metadata ratio %d",
  592. info->metadata_ratio);
  593. } else {
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. break;
  598. case Opt_discard:
  599. btrfs_set_and_info(root, DISCARD,
  600. "turning on discard");
  601. break;
  602. case Opt_nodiscard:
  603. btrfs_clear_and_info(root, DISCARD,
  604. "turning off discard");
  605. break;
  606. case Opt_space_cache:
  607. case Opt_space_cache_version:
  608. if (token == Opt_space_cache ||
  609. strcmp(args[0].from, "v1") == 0) {
  610. btrfs_clear_opt(root->fs_info->mount_opt,
  611. FREE_SPACE_TREE);
  612. btrfs_set_and_info(root, SPACE_CACHE,
  613. "enabling disk space caching");
  614. } else if (strcmp(args[0].from, "v2") == 0) {
  615. btrfs_clear_opt(root->fs_info->mount_opt,
  616. SPACE_CACHE);
  617. btrfs_set_and_info(root, FREE_SPACE_TREE,
  618. "enabling free space tree");
  619. } else {
  620. ret = -EINVAL;
  621. goto out;
  622. }
  623. break;
  624. case Opt_rescan_uuid_tree:
  625. btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
  626. break;
  627. case Opt_no_space_cache:
  628. if (btrfs_test_opt(root, SPACE_CACHE)) {
  629. btrfs_clear_and_info(root, SPACE_CACHE,
  630. "disabling disk space caching");
  631. }
  632. if (btrfs_test_opt(root, FREE_SPACE_TREE)) {
  633. btrfs_clear_and_info(root, FREE_SPACE_TREE,
  634. "disabling free space tree");
  635. }
  636. break;
  637. case Opt_inode_cache:
  638. btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
  639. "enabling inode map caching");
  640. break;
  641. case Opt_noinode_cache:
  642. btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
  643. "disabling inode map caching");
  644. break;
  645. case Opt_clear_cache:
  646. btrfs_set_and_info(root, CLEAR_CACHE,
  647. "force clearing of disk cache");
  648. break;
  649. case Opt_user_subvol_rm_allowed:
  650. btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  651. break;
  652. case Opt_enospc_debug:
  653. btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  654. break;
  655. case Opt_noenospc_debug:
  656. btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
  657. break;
  658. case Opt_defrag:
  659. btrfs_set_and_info(root, AUTO_DEFRAG,
  660. "enabling auto defrag");
  661. break;
  662. case Opt_nodefrag:
  663. btrfs_clear_and_info(root, AUTO_DEFRAG,
  664. "disabling auto defrag");
  665. break;
  666. case Opt_recovery:
  667. btrfs_warn(root->fs_info,
  668. "'recovery' is deprecated, use 'usebackuproot' instead");
  669. case Opt_usebackuproot:
  670. btrfs_info(root->fs_info,
  671. "trying to use backup root at mount time");
  672. btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
  673. break;
  674. case Opt_skip_balance:
  675. btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  676. break;
  677. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  678. case Opt_check_integrity_including_extent_data:
  679. btrfs_info(root->fs_info,
  680. "enabling check integrity including extent data");
  681. btrfs_set_opt(info->mount_opt,
  682. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  683. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  684. break;
  685. case Opt_check_integrity:
  686. btrfs_info(root->fs_info, "enabling check integrity");
  687. btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  688. break;
  689. case Opt_check_integrity_print_mask:
  690. ret = match_int(&args[0], &intarg);
  691. if (ret) {
  692. goto out;
  693. } else if (intarg >= 0) {
  694. info->check_integrity_print_mask = intarg;
  695. btrfs_info(root->fs_info, "check_integrity_print_mask 0x%x",
  696. info->check_integrity_print_mask);
  697. } else {
  698. ret = -EINVAL;
  699. goto out;
  700. }
  701. break;
  702. #else
  703. case Opt_check_integrity_including_extent_data:
  704. case Opt_check_integrity:
  705. case Opt_check_integrity_print_mask:
  706. btrfs_err(root->fs_info,
  707. "support for check_integrity* not compiled in!");
  708. ret = -EINVAL;
  709. goto out;
  710. #endif
  711. case Opt_fatal_errors:
  712. if (strcmp(args[0].from, "panic") == 0)
  713. btrfs_set_opt(info->mount_opt,
  714. PANIC_ON_FATAL_ERROR);
  715. else if (strcmp(args[0].from, "bug") == 0)
  716. btrfs_clear_opt(info->mount_opt,
  717. PANIC_ON_FATAL_ERROR);
  718. else {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. break;
  723. case Opt_commit_interval:
  724. intarg = 0;
  725. ret = match_int(&args[0], &intarg);
  726. if (ret < 0) {
  727. btrfs_err(root->fs_info, "invalid commit interval");
  728. ret = -EINVAL;
  729. goto out;
  730. }
  731. if (intarg > 0) {
  732. if (intarg > 300) {
  733. btrfs_warn(root->fs_info, "excessive commit interval %d",
  734. intarg);
  735. }
  736. info->commit_interval = intarg;
  737. } else {
  738. btrfs_info(root->fs_info, "using default commit interval %ds",
  739. BTRFS_DEFAULT_COMMIT_INTERVAL);
  740. info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  741. }
  742. break;
  743. #ifdef CONFIG_BTRFS_DEBUG
  744. case Opt_fragment_all:
  745. btrfs_info(root->fs_info, "fragmenting all space");
  746. btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  747. btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
  748. break;
  749. case Opt_fragment_metadata:
  750. btrfs_info(root->fs_info, "fragmenting metadata");
  751. btrfs_set_opt(info->mount_opt,
  752. FRAGMENT_METADATA);
  753. break;
  754. case Opt_fragment_data:
  755. btrfs_info(root->fs_info, "fragmenting data");
  756. btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  757. break;
  758. #endif
  759. case Opt_err:
  760. btrfs_info(root->fs_info, "unrecognized mount option '%s'", p);
  761. ret = -EINVAL;
  762. goto out;
  763. default:
  764. break;
  765. }
  766. }
  767. check:
  768. /*
  769. * Extra check for current option against current flag
  770. */
  771. if (btrfs_test_opt(root, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) {
  772. btrfs_err(root->fs_info,
  773. "nologreplay must be used with ro mount option");
  774. ret = -EINVAL;
  775. }
  776. out:
  777. if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) &&
  778. !btrfs_test_opt(root, FREE_SPACE_TREE) &&
  779. !btrfs_test_opt(root, CLEAR_CACHE)) {
  780. btrfs_err(root->fs_info, "cannot disable free space tree");
  781. ret = -EINVAL;
  782. }
  783. if (!ret && btrfs_test_opt(root, SPACE_CACHE))
  784. btrfs_info(root->fs_info, "disk space caching is enabled");
  785. if (!ret && btrfs_test_opt(root, FREE_SPACE_TREE))
  786. btrfs_info(root->fs_info, "using free space tree");
  787. kfree(orig);
  788. return ret;
  789. }
  790. /*
  791. * Parse mount options that are required early in the mount process.
  792. *
  793. * All other options will be parsed on much later in the mount process and
  794. * only when we need to allocate a new super block.
  795. */
  796. static int btrfs_parse_early_options(const char *options, fmode_t flags,
  797. void *holder, char **subvol_name, u64 *subvol_objectid,
  798. struct btrfs_fs_devices **fs_devices)
  799. {
  800. substring_t args[MAX_OPT_ARGS];
  801. char *device_name, *opts, *orig, *p;
  802. char *num = NULL;
  803. int error = 0;
  804. if (!options)
  805. return 0;
  806. /*
  807. * strsep changes the string, duplicate it because parse_options
  808. * gets called twice
  809. */
  810. opts = kstrdup(options, GFP_KERNEL);
  811. if (!opts)
  812. return -ENOMEM;
  813. orig = opts;
  814. while ((p = strsep(&opts, ",")) != NULL) {
  815. int token;
  816. if (!*p)
  817. continue;
  818. token = match_token(p, tokens, args);
  819. switch (token) {
  820. case Opt_subvol:
  821. kfree(*subvol_name);
  822. *subvol_name = match_strdup(&args[0]);
  823. if (!*subvol_name) {
  824. error = -ENOMEM;
  825. goto out;
  826. }
  827. break;
  828. case Opt_subvolid:
  829. num = match_strdup(&args[0]);
  830. if (num) {
  831. *subvol_objectid = memparse(num, NULL);
  832. kfree(num);
  833. /* we want the original fs_tree */
  834. if (!*subvol_objectid)
  835. *subvol_objectid =
  836. BTRFS_FS_TREE_OBJECTID;
  837. } else {
  838. error = -EINVAL;
  839. goto out;
  840. }
  841. break;
  842. case Opt_subvolrootid:
  843. printk(KERN_WARNING
  844. "BTRFS: 'subvolrootid' mount option is deprecated and has "
  845. "no effect\n");
  846. break;
  847. case Opt_device:
  848. device_name = match_strdup(&args[0]);
  849. if (!device_name) {
  850. error = -ENOMEM;
  851. goto out;
  852. }
  853. error = btrfs_scan_one_device(device_name,
  854. flags, holder, fs_devices);
  855. kfree(device_name);
  856. if (error)
  857. goto out;
  858. break;
  859. default:
  860. break;
  861. }
  862. }
  863. out:
  864. kfree(orig);
  865. return error;
  866. }
  867. static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
  868. u64 subvol_objectid)
  869. {
  870. struct btrfs_root *root = fs_info->tree_root;
  871. struct btrfs_root *fs_root;
  872. struct btrfs_root_ref *root_ref;
  873. struct btrfs_inode_ref *inode_ref;
  874. struct btrfs_key key;
  875. struct btrfs_path *path = NULL;
  876. char *name = NULL, *ptr;
  877. u64 dirid;
  878. int len;
  879. int ret;
  880. path = btrfs_alloc_path();
  881. if (!path) {
  882. ret = -ENOMEM;
  883. goto err;
  884. }
  885. path->leave_spinning = 1;
  886. name = kmalloc(PATH_MAX, GFP_NOFS);
  887. if (!name) {
  888. ret = -ENOMEM;
  889. goto err;
  890. }
  891. ptr = name + PATH_MAX - 1;
  892. ptr[0] = '\0';
  893. /*
  894. * Walk up the subvolume trees in the tree of tree roots by root
  895. * backrefs until we hit the top-level subvolume.
  896. */
  897. while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  898. key.objectid = subvol_objectid;
  899. key.type = BTRFS_ROOT_BACKREF_KEY;
  900. key.offset = (u64)-1;
  901. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  902. if (ret < 0) {
  903. goto err;
  904. } else if (ret > 0) {
  905. ret = btrfs_previous_item(root, path, subvol_objectid,
  906. BTRFS_ROOT_BACKREF_KEY);
  907. if (ret < 0) {
  908. goto err;
  909. } else if (ret > 0) {
  910. ret = -ENOENT;
  911. goto err;
  912. }
  913. }
  914. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  915. subvol_objectid = key.offset;
  916. root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  917. struct btrfs_root_ref);
  918. len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
  919. ptr -= len + 1;
  920. if (ptr < name) {
  921. ret = -ENAMETOOLONG;
  922. goto err;
  923. }
  924. read_extent_buffer(path->nodes[0], ptr + 1,
  925. (unsigned long)(root_ref + 1), len);
  926. ptr[0] = '/';
  927. dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
  928. btrfs_release_path(path);
  929. key.objectid = subvol_objectid;
  930. key.type = BTRFS_ROOT_ITEM_KEY;
  931. key.offset = (u64)-1;
  932. fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
  933. if (IS_ERR(fs_root)) {
  934. ret = PTR_ERR(fs_root);
  935. goto err;
  936. }
  937. /*
  938. * Walk up the filesystem tree by inode refs until we hit the
  939. * root directory.
  940. */
  941. while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
  942. key.objectid = dirid;
  943. key.type = BTRFS_INODE_REF_KEY;
  944. key.offset = (u64)-1;
  945. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  946. if (ret < 0) {
  947. goto err;
  948. } else if (ret > 0) {
  949. ret = btrfs_previous_item(fs_root, path, dirid,
  950. BTRFS_INODE_REF_KEY);
  951. if (ret < 0) {
  952. goto err;
  953. } else if (ret > 0) {
  954. ret = -ENOENT;
  955. goto err;
  956. }
  957. }
  958. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  959. dirid = key.offset;
  960. inode_ref = btrfs_item_ptr(path->nodes[0],
  961. path->slots[0],
  962. struct btrfs_inode_ref);
  963. len = btrfs_inode_ref_name_len(path->nodes[0],
  964. inode_ref);
  965. ptr -= len + 1;
  966. if (ptr < name) {
  967. ret = -ENAMETOOLONG;
  968. goto err;
  969. }
  970. read_extent_buffer(path->nodes[0], ptr + 1,
  971. (unsigned long)(inode_ref + 1), len);
  972. ptr[0] = '/';
  973. btrfs_release_path(path);
  974. }
  975. }
  976. btrfs_free_path(path);
  977. if (ptr == name + PATH_MAX - 1) {
  978. name[0] = '/';
  979. name[1] = '\0';
  980. } else {
  981. memmove(name, ptr, name + PATH_MAX - ptr);
  982. }
  983. return name;
  984. err:
  985. btrfs_free_path(path);
  986. kfree(name);
  987. return ERR_PTR(ret);
  988. }
  989. static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
  990. {
  991. struct btrfs_root *root = fs_info->tree_root;
  992. struct btrfs_dir_item *di;
  993. struct btrfs_path *path;
  994. struct btrfs_key location;
  995. u64 dir_id;
  996. path = btrfs_alloc_path();
  997. if (!path)
  998. return -ENOMEM;
  999. path->leave_spinning = 1;
  1000. /*
  1001. * Find the "default" dir item which points to the root item that we
  1002. * will mount by default if we haven't been given a specific subvolume
  1003. * to mount.
  1004. */
  1005. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  1006. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
  1007. if (IS_ERR(di)) {
  1008. btrfs_free_path(path);
  1009. return PTR_ERR(di);
  1010. }
  1011. if (!di) {
  1012. /*
  1013. * Ok the default dir item isn't there. This is weird since
  1014. * it's always been there, but don't freak out, just try and
  1015. * mount the top-level subvolume.
  1016. */
  1017. btrfs_free_path(path);
  1018. *objectid = BTRFS_FS_TREE_OBJECTID;
  1019. return 0;
  1020. }
  1021. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  1022. btrfs_free_path(path);
  1023. *objectid = location.objectid;
  1024. return 0;
  1025. }
  1026. static int btrfs_fill_super(struct super_block *sb,
  1027. struct btrfs_fs_devices *fs_devices,
  1028. void *data, int silent)
  1029. {
  1030. struct inode *inode;
  1031. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1032. struct btrfs_key key;
  1033. int err;
  1034. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1035. sb->s_magic = BTRFS_SUPER_MAGIC;
  1036. sb->s_op = &btrfs_super_ops;
  1037. sb->s_d_op = &btrfs_dentry_operations;
  1038. sb->s_export_op = &btrfs_export_ops;
  1039. sb->s_xattr = btrfs_xattr_handlers;
  1040. sb->s_time_gran = 1;
  1041. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  1042. sb->s_flags |= MS_POSIXACL;
  1043. #endif
  1044. sb->s_flags |= MS_I_VERSION;
  1045. sb->s_iflags |= SB_I_CGROUPWB;
  1046. err = open_ctree(sb, fs_devices, (char *)data);
  1047. if (err) {
  1048. printk(KERN_ERR "BTRFS: open_ctree failed\n");
  1049. return err;
  1050. }
  1051. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  1052. key.type = BTRFS_INODE_ITEM_KEY;
  1053. key.offset = 0;
  1054. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  1055. if (IS_ERR(inode)) {
  1056. err = PTR_ERR(inode);
  1057. goto fail_close;
  1058. }
  1059. sb->s_root = d_make_root(inode);
  1060. if (!sb->s_root) {
  1061. err = -ENOMEM;
  1062. goto fail_close;
  1063. }
  1064. save_mount_options(sb, data);
  1065. cleancache_init_fs(sb);
  1066. sb->s_flags |= MS_ACTIVE;
  1067. return 0;
  1068. fail_close:
  1069. close_ctree(fs_info->tree_root);
  1070. return err;
  1071. }
  1072. int btrfs_sync_fs(struct super_block *sb, int wait)
  1073. {
  1074. struct btrfs_trans_handle *trans;
  1075. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1076. struct btrfs_root *root = fs_info->tree_root;
  1077. trace_btrfs_sync_fs(wait);
  1078. if (!wait) {
  1079. filemap_flush(fs_info->btree_inode->i_mapping);
  1080. return 0;
  1081. }
  1082. btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
  1083. trans = btrfs_attach_transaction_barrier(root);
  1084. if (IS_ERR(trans)) {
  1085. /* no transaction, don't bother */
  1086. if (PTR_ERR(trans) == -ENOENT) {
  1087. /*
  1088. * Exit unless we have some pending changes
  1089. * that need to go through commit
  1090. */
  1091. if (fs_info->pending_changes == 0)
  1092. return 0;
  1093. /*
  1094. * A non-blocking test if the fs is frozen. We must not
  1095. * start a new transaction here otherwise a deadlock
  1096. * happens. The pending operations are delayed to the
  1097. * next commit after thawing.
  1098. */
  1099. if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
  1100. __sb_end_write(sb, SB_FREEZE_WRITE);
  1101. else
  1102. return 0;
  1103. trans = btrfs_start_transaction(root, 0);
  1104. }
  1105. if (IS_ERR(trans))
  1106. return PTR_ERR(trans);
  1107. }
  1108. return btrfs_commit_transaction(trans, root);
  1109. }
  1110. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  1111. {
  1112. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  1113. struct btrfs_root *root = info->tree_root;
  1114. char *compress_type;
  1115. if (btrfs_test_opt(root, DEGRADED))
  1116. seq_puts(seq, ",degraded");
  1117. if (btrfs_test_opt(root, NODATASUM))
  1118. seq_puts(seq, ",nodatasum");
  1119. if (btrfs_test_opt(root, NODATACOW))
  1120. seq_puts(seq, ",nodatacow");
  1121. if (btrfs_test_opt(root, NOBARRIER))
  1122. seq_puts(seq, ",nobarrier");
  1123. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  1124. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  1125. if (info->alloc_start != 0)
  1126. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  1127. if (info->thread_pool_size != min_t(unsigned long,
  1128. num_online_cpus() + 2, 8))
  1129. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  1130. if (btrfs_test_opt(root, COMPRESS)) {
  1131. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  1132. compress_type = "zlib";
  1133. else
  1134. compress_type = "lzo";
  1135. if (btrfs_test_opt(root, FORCE_COMPRESS))
  1136. seq_printf(seq, ",compress-force=%s", compress_type);
  1137. else
  1138. seq_printf(seq, ",compress=%s", compress_type);
  1139. }
  1140. if (btrfs_test_opt(root, NOSSD))
  1141. seq_puts(seq, ",nossd");
  1142. if (btrfs_test_opt(root, SSD_SPREAD))
  1143. seq_puts(seq, ",ssd_spread");
  1144. else if (btrfs_test_opt(root, SSD))
  1145. seq_puts(seq, ",ssd");
  1146. if (btrfs_test_opt(root, NOTREELOG))
  1147. seq_puts(seq, ",notreelog");
  1148. if (btrfs_test_opt(root, NOLOGREPLAY))
  1149. seq_puts(seq, ",nologreplay");
  1150. if (btrfs_test_opt(root, FLUSHONCOMMIT))
  1151. seq_puts(seq, ",flushoncommit");
  1152. if (btrfs_test_opt(root, DISCARD))
  1153. seq_puts(seq, ",discard");
  1154. if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  1155. seq_puts(seq, ",noacl");
  1156. if (btrfs_test_opt(root, SPACE_CACHE))
  1157. seq_puts(seq, ",space_cache");
  1158. else if (btrfs_test_opt(root, FREE_SPACE_TREE))
  1159. seq_puts(seq, ",space_cache=v2");
  1160. else
  1161. seq_puts(seq, ",nospace_cache");
  1162. if (btrfs_test_opt(root, RESCAN_UUID_TREE))
  1163. seq_puts(seq, ",rescan_uuid_tree");
  1164. if (btrfs_test_opt(root, CLEAR_CACHE))
  1165. seq_puts(seq, ",clear_cache");
  1166. if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1167. seq_puts(seq, ",user_subvol_rm_allowed");
  1168. if (btrfs_test_opt(root, ENOSPC_DEBUG))
  1169. seq_puts(seq, ",enospc_debug");
  1170. if (btrfs_test_opt(root, AUTO_DEFRAG))
  1171. seq_puts(seq, ",autodefrag");
  1172. if (btrfs_test_opt(root, INODE_MAP_CACHE))
  1173. seq_puts(seq, ",inode_cache");
  1174. if (btrfs_test_opt(root, SKIP_BALANCE))
  1175. seq_puts(seq, ",skip_balance");
  1176. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1177. if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  1178. seq_puts(seq, ",check_int_data");
  1179. else if (btrfs_test_opt(root, CHECK_INTEGRITY))
  1180. seq_puts(seq, ",check_int");
  1181. if (info->check_integrity_print_mask)
  1182. seq_printf(seq, ",check_int_print_mask=%d",
  1183. info->check_integrity_print_mask);
  1184. #endif
  1185. if (info->metadata_ratio)
  1186. seq_printf(seq, ",metadata_ratio=%d",
  1187. info->metadata_ratio);
  1188. if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
  1189. seq_puts(seq, ",fatal_errors=panic");
  1190. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1191. seq_printf(seq, ",commit=%d", info->commit_interval);
  1192. #ifdef CONFIG_BTRFS_DEBUG
  1193. if (btrfs_test_opt(root, FRAGMENT_DATA))
  1194. seq_puts(seq, ",fragment=data");
  1195. if (btrfs_test_opt(root, FRAGMENT_METADATA))
  1196. seq_puts(seq, ",fragment=metadata");
  1197. #endif
  1198. seq_printf(seq, ",subvolid=%llu",
  1199. BTRFS_I(d_inode(dentry))->root->root_key.objectid);
  1200. seq_puts(seq, ",subvol=");
  1201. seq_dentry(seq, dentry, " \t\n\\");
  1202. return 0;
  1203. }
  1204. static int btrfs_test_super(struct super_block *s, void *data)
  1205. {
  1206. struct btrfs_fs_info *p = data;
  1207. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1208. return fs_info->fs_devices == p->fs_devices;
  1209. }
  1210. static int btrfs_set_super(struct super_block *s, void *data)
  1211. {
  1212. int err = set_anon_super(s, data);
  1213. if (!err)
  1214. s->s_fs_info = data;
  1215. return err;
  1216. }
  1217. /*
  1218. * subvolumes are identified by ino 256
  1219. */
  1220. static inline int is_subvolume_inode(struct inode *inode)
  1221. {
  1222. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1223. return 1;
  1224. return 0;
  1225. }
  1226. /*
  1227. * This will add subvolid=0 to the argument string while removing any subvol=
  1228. * and subvolid= arguments to make sure we get the top-level root for path
  1229. * walking to the subvol we want.
  1230. */
  1231. static char *setup_root_args(char *args)
  1232. {
  1233. char *buf, *dst, *sep;
  1234. if (!args)
  1235. return kstrdup("subvolid=0", GFP_NOFS);
  1236. /* The worst case is that we add ",subvolid=0" to the end. */
  1237. buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
  1238. if (!buf)
  1239. return NULL;
  1240. while (1) {
  1241. sep = strchrnul(args, ',');
  1242. if (!strstarts(args, "subvol=") &&
  1243. !strstarts(args, "subvolid=")) {
  1244. memcpy(dst, args, sep - args);
  1245. dst += sep - args;
  1246. *dst++ = ',';
  1247. }
  1248. if (*sep)
  1249. args = sep + 1;
  1250. else
  1251. break;
  1252. }
  1253. strcpy(dst, "subvolid=0");
  1254. return buf;
  1255. }
  1256. static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  1257. int flags, const char *device_name,
  1258. char *data)
  1259. {
  1260. struct dentry *root;
  1261. struct vfsmount *mnt = NULL;
  1262. char *newargs;
  1263. int ret;
  1264. newargs = setup_root_args(data);
  1265. if (!newargs) {
  1266. root = ERR_PTR(-ENOMEM);
  1267. goto out;
  1268. }
  1269. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
  1270. if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
  1271. if (flags & MS_RDONLY) {
  1272. mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
  1273. device_name, newargs);
  1274. } else {
  1275. mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
  1276. device_name, newargs);
  1277. if (IS_ERR(mnt)) {
  1278. root = ERR_CAST(mnt);
  1279. mnt = NULL;
  1280. goto out;
  1281. }
  1282. down_write(&mnt->mnt_sb->s_umount);
  1283. ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
  1284. up_write(&mnt->mnt_sb->s_umount);
  1285. if (ret < 0) {
  1286. root = ERR_PTR(ret);
  1287. goto out;
  1288. }
  1289. }
  1290. }
  1291. if (IS_ERR(mnt)) {
  1292. root = ERR_CAST(mnt);
  1293. mnt = NULL;
  1294. goto out;
  1295. }
  1296. if (!subvol_name) {
  1297. if (!subvol_objectid) {
  1298. ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  1299. &subvol_objectid);
  1300. if (ret) {
  1301. root = ERR_PTR(ret);
  1302. goto out;
  1303. }
  1304. }
  1305. subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
  1306. subvol_objectid);
  1307. if (IS_ERR(subvol_name)) {
  1308. root = ERR_CAST(subvol_name);
  1309. subvol_name = NULL;
  1310. goto out;
  1311. }
  1312. }
  1313. root = mount_subtree(mnt, subvol_name);
  1314. /* mount_subtree() drops our reference on the vfsmount. */
  1315. mnt = NULL;
  1316. if (!IS_ERR(root)) {
  1317. struct super_block *s = root->d_sb;
  1318. struct inode *root_inode = d_inode(root);
  1319. u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
  1320. ret = 0;
  1321. if (!is_subvolume_inode(root_inode)) {
  1322. pr_err("BTRFS: '%s' is not a valid subvolume\n",
  1323. subvol_name);
  1324. ret = -EINVAL;
  1325. }
  1326. if (subvol_objectid && root_objectid != subvol_objectid) {
  1327. /*
  1328. * This will also catch a race condition where a
  1329. * subvolume which was passed by ID is renamed and
  1330. * another subvolume is renamed over the old location.
  1331. */
  1332. pr_err("BTRFS: subvol '%s' does not match subvolid %llu\n",
  1333. subvol_name, subvol_objectid);
  1334. ret = -EINVAL;
  1335. }
  1336. if (ret) {
  1337. dput(root);
  1338. root = ERR_PTR(ret);
  1339. deactivate_locked_super(s);
  1340. }
  1341. }
  1342. out:
  1343. mntput(mnt);
  1344. kfree(newargs);
  1345. kfree(subvol_name);
  1346. return root;
  1347. }
  1348. static int parse_security_options(char *orig_opts,
  1349. struct security_mnt_opts *sec_opts)
  1350. {
  1351. char *secdata = NULL;
  1352. int ret = 0;
  1353. secdata = alloc_secdata();
  1354. if (!secdata)
  1355. return -ENOMEM;
  1356. ret = security_sb_copy_data(orig_opts, secdata);
  1357. if (ret) {
  1358. free_secdata(secdata);
  1359. return ret;
  1360. }
  1361. ret = security_sb_parse_opts_str(secdata, sec_opts);
  1362. free_secdata(secdata);
  1363. return ret;
  1364. }
  1365. static int setup_security_options(struct btrfs_fs_info *fs_info,
  1366. struct super_block *sb,
  1367. struct security_mnt_opts *sec_opts)
  1368. {
  1369. int ret = 0;
  1370. /*
  1371. * Call security_sb_set_mnt_opts() to check whether new sec_opts
  1372. * is valid.
  1373. */
  1374. ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
  1375. if (ret)
  1376. return ret;
  1377. #ifdef CONFIG_SECURITY
  1378. if (!fs_info->security_opts.num_mnt_opts) {
  1379. /* first time security setup, copy sec_opts to fs_info */
  1380. memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
  1381. } else {
  1382. /*
  1383. * Since SELinux (the only one supporting security_mnt_opts)
  1384. * does NOT support changing context during remount/mount of
  1385. * the same sb, this must be the same or part of the same
  1386. * security options, just free it.
  1387. */
  1388. security_free_mnt_opts(sec_opts);
  1389. }
  1390. #endif
  1391. return ret;
  1392. }
  1393. /*
  1394. * Find a superblock for the given device / mount point.
  1395. *
  1396. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1397. * for multiple device setup. Make sure to keep it in sync.
  1398. */
  1399. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1400. const char *device_name, void *data)
  1401. {
  1402. struct block_device *bdev = NULL;
  1403. struct super_block *s;
  1404. struct btrfs_fs_devices *fs_devices = NULL;
  1405. struct btrfs_fs_info *fs_info = NULL;
  1406. struct security_mnt_opts new_sec_opts;
  1407. fmode_t mode = FMODE_READ;
  1408. char *subvol_name = NULL;
  1409. u64 subvol_objectid = 0;
  1410. int error = 0;
  1411. if (!(flags & MS_RDONLY))
  1412. mode |= FMODE_WRITE;
  1413. error = btrfs_parse_early_options(data, mode, fs_type,
  1414. &subvol_name, &subvol_objectid,
  1415. &fs_devices);
  1416. if (error) {
  1417. kfree(subvol_name);
  1418. return ERR_PTR(error);
  1419. }
  1420. if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  1421. /* mount_subvol() will free subvol_name. */
  1422. return mount_subvol(subvol_name, subvol_objectid, flags,
  1423. device_name, data);
  1424. }
  1425. security_init_mnt_opts(&new_sec_opts);
  1426. if (data) {
  1427. error = parse_security_options(data, &new_sec_opts);
  1428. if (error)
  1429. return ERR_PTR(error);
  1430. }
  1431. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1432. if (error)
  1433. goto error_sec_opts;
  1434. /*
  1435. * Setup a dummy root and fs_info for test/set super. This is because
  1436. * we don't actually fill this stuff out until open_ctree, but we need
  1437. * it for searching for existing supers, so this lets us do that and
  1438. * then open_ctree will properly initialize everything later.
  1439. */
  1440. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1441. if (!fs_info) {
  1442. error = -ENOMEM;
  1443. goto error_sec_opts;
  1444. }
  1445. fs_info->fs_devices = fs_devices;
  1446. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1447. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1448. security_init_mnt_opts(&fs_info->security_opts);
  1449. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1450. error = -ENOMEM;
  1451. goto error_fs_info;
  1452. }
  1453. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1454. if (error)
  1455. goto error_fs_info;
  1456. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1457. error = -EACCES;
  1458. goto error_close_devices;
  1459. }
  1460. bdev = fs_devices->latest_bdev;
  1461. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1462. fs_info);
  1463. if (IS_ERR(s)) {
  1464. error = PTR_ERR(s);
  1465. goto error_close_devices;
  1466. }
  1467. if (s->s_root) {
  1468. btrfs_close_devices(fs_devices);
  1469. free_fs_info(fs_info);
  1470. if ((flags ^ s->s_flags) & MS_RDONLY)
  1471. error = -EBUSY;
  1472. } else {
  1473. snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
  1474. btrfs_sb(s)->bdev_holder = fs_type;
  1475. error = btrfs_fill_super(s, fs_devices, data,
  1476. flags & MS_SILENT ? 1 : 0);
  1477. }
  1478. if (error) {
  1479. deactivate_locked_super(s);
  1480. goto error_sec_opts;
  1481. }
  1482. fs_info = btrfs_sb(s);
  1483. error = setup_security_options(fs_info, s, &new_sec_opts);
  1484. if (error) {
  1485. deactivate_locked_super(s);
  1486. goto error_sec_opts;
  1487. }
  1488. return dget(s->s_root);
  1489. error_close_devices:
  1490. btrfs_close_devices(fs_devices);
  1491. error_fs_info:
  1492. free_fs_info(fs_info);
  1493. error_sec_opts:
  1494. security_free_mnt_opts(&new_sec_opts);
  1495. return ERR_PTR(error);
  1496. }
  1497. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1498. int new_pool_size, int old_pool_size)
  1499. {
  1500. if (new_pool_size == old_pool_size)
  1501. return;
  1502. fs_info->thread_pool_size = new_pool_size;
  1503. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1504. old_pool_size, new_pool_size);
  1505. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1506. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1507. btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
  1508. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1509. btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  1510. btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  1511. btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  1512. new_pool_size);
  1513. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1514. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1515. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1516. btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
  1517. btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  1518. new_pool_size);
  1519. }
  1520. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1521. {
  1522. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1523. }
  1524. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1525. unsigned long old_opts, int flags)
  1526. {
  1527. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1528. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1529. (flags & MS_RDONLY))) {
  1530. /* wait for any defraggers to finish */
  1531. wait_event(fs_info->transaction_wait,
  1532. (atomic_read(&fs_info->defrag_running) == 0));
  1533. if (flags & MS_RDONLY)
  1534. sync_filesystem(fs_info->sb);
  1535. }
  1536. }
  1537. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1538. unsigned long old_opts)
  1539. {
  1540. /*
  1541. * We need to cleanup all defragable inodes if the autodefragment is
  1542. * close or the filesystem is read only.
  1543. */
  1544. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1545. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1546. (fs_info->sb->s_flags & MS_RDONLY))) {
  1547. btrfs_cleanup_defrag_inodes(fs_info);
  1548. }
  1549. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1550. }
  1551. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1552. {
  1553. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1554. struct btrfs_root *root = fs_info->tree_root;
  1555. unsigned old_flags = sb->s_flags;
  1556. unsigned long old_opts = fs_info->mount_opt;
  1557. unsigned long old_compress_type = fs_info->compress_type;
  1558. u64 old_max_inline = fs_info->max_inline;
  1559. u64 old_alloc_start = fs_info->alloc_start;
  1560. int old_thread_pool_size = fs_info->thread_pool_size;
  1561. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1562. int ret;
  1563. sync_filesystem(sb);
  1564. btrfs_remount_prepare(fs_info);
  1565. if (data) {
  1566. struct security_mnt_opts new_sec_opts;
  1567. security_init_mnt_opts(&new_sec_opts);
  1568. ret = parse_security_options(data, &new_sec_opts);
  1569. if (ret)
  1570. goto restore;
  1571. ret = setup_security_options(fs_info, sb,
  1572. &new_sec_opts);
  1573. if (ret) {
  1574. security_free_mnt_opts(&new_sec_opts);
  1575. goto restore;
  1576. }
  1577. }
  1578. ret = btrfs_parse_options(root, data, *flags);
  1579. if (ret) {
  1580. ret = -EINVAL;
  1581. goto restore;
  1582. }
  1583. btrfs_remount_begin(fs_info, old_opts, *flags);
  1584. btrfs_resize_thread_pool(fs_info,
  1585. fs_info->thread_pool_size, old_thread_pool_size);
  1586. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1587. goto out;
  1588. if (*flags & MS_RDONLY) {
  1589. /*
  1590. * this also happens on 'umount -rf' or on shutdown, when
  1591. * the filesystem is busy.
  1592. */
  1593. cancel_work_sync(&fs_info->async_reclaim_work);
  1594. /* wait for the uuid_scan task to finish */
  1595. down(&fs_info->uuid_tree_rescan_sem);
  1596. /* avoid complains from lockdep et al. */
  1597. up(&fs_info->uuid_tree_rescan_sem);
  1598. sb->s_flags |= MS_RDONLY;
  1599. /*
  1600. * Setting MS_RDONLY will put the cleaner thread to
  1601. * sleep at the next loop if it's already active.
  1602. * If it's already asleep, we'll leave unused block
  1603. * groups on disk until we're mounted read-write again
  1604. * unless we clean them up here.
  1605. */
  1606. btrfs_delete_unused_bgs(fs_info);
  1607. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1608. btrfs_scrub_cancel(fs_info);
  1609. btrfs_pause_balance(fs_info);
  1610. ret = btrfs_commit_super(root);
  1611. if (ret)
  1612. goto restore;
  1613. } else {
  1614. if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  1615. btrfs_err(fs_info,
  1616. "Remounting read-write after error is not allowed");
  1617. ret = -EINVAL;
  1618. goto restore;
  1619. }
  1620. if (fs_info->fs_devices->rw_devices == 0) {
  1621. ret = -EACCES;
  1622. goto restore;
  1623. }
  1624. if (fs_info->fs_devices->missing_devices >
  1625. fs_info->num_tolerated_disk_barrier_failures &&
  1626. !(*flags & MS_RDONLY)) {
  1627. btrfs_warn(fs_info,
  1628. "too many missing devices, writeable remount is not allowed");
  1629. ret = -EACCES;
  1630. goto restore;
  1631. }
  1632. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1633. ret = -EINVAL;
  1634. goto restore;
  1635. }
  1636. ret = btrfs_cleanup_fs_roots(fs_info);
  1637. if (ret)
  1638. goto restore;
  1639. /* recover relocation */
  1640. mutex_lock(&fs_info->cleaner_mutex);
  1641. ret = btrfs_recover_relocation(root);
  1642. mutex_unlock(&fs_info->cleaner_mutex);
  1643. if (ret)
  1644. goto restore;
  1645. ret = btrfs_resume_balance_async(fs_info);
  1646. if (ret)
  1647. goto restore;
  1648. ret = btrfs_resume_dev_replace_async(fs_info);
  1649. if (ret) {
  1650. btrfs_warn(fs_info, "failed to resume dev_replace");
  1651. goto restore;
  1652. }
  1653. if (!fs_info->uuid_root) {
  1654. btrfs_info(fs_info, "creating UUID tree");
  1655. ret = btrfs_create_uuid_tree(fs_info);
  1656. if (ret) {
  1657. btrfs_warn(fs_info, "failed to create the UUID tree %d", ret);
  1658. goto restore;
  1659. }
  1660. }
  1661. sb->s_flags &= ~MS_RDONLY;
  1662. }
  1663. out:
  1664. wake_up_process(fs_info->transaction_kthread);
  1665. btrfs_remount_cleanup(fs_info, old_opts);
  1666. return 0;
  1667. restore:
  1668. /* We've hit an error - don't reset MS_RDONLY */
  1669. if (sb->s_flags & MS_RDONLY)
  1670. old_flags |= MS_RDONLY;
  1671. sb->s_flags = old_flags;
  1672. fs_info->mount_opt = old_opts;
  1673. fs_info->compress_type = old_compress_type;
  1674. fs_info->max_inline = old_max_inline;
  1675. mutex_lock(&fs_info->chunk_mutex);
  1676. fs_info->alloc_start = old_alloc_start;
  1677. mutex_unlock(&fs_info->chunk_mutex);
  1678. btrfs_resize_thread_pool(fs_info,
  1679. old_thread_pool_size, fs_info->thread_pool_size);
  1680. fs_info->metadata_ratio = old_metadata_ratio;
  1681. btrfs_remount_cleanup(fs_info, old_opts);
  1682. return ret;
  1683. }
  1684. /* Used to sort the devices by max_avail(descending sort) */
  1685. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1686. const void *dev_info2)
  1687. {
  1688. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1689. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1690. return -1;
  1691. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1692. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1693. return 1;
  1694. else
  1695. return 0;
  1696. }
  1697. /*
  1698. * sort the devices by max_avail, in which max free extent size of each device
  1699. * is stored.(Descending Sort)
  1700. */
  1701. static inline void btrfs_descending_sort_devices(
  1702. struct btrfs_device_info *devices,
  1703. size_t nr_devices)
  1704. {
  1705. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1706. btrfs_cmp_device_free_bytes, NULL);
  1707. }
  1708. /*
  1709. * The helper to calc the free space on the devices that can be used to store
  1710. * file data.
  1711. */
  1712. static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  1713. {
  1714. struct btrfs_fs_info *fs_info = root->fs_info;
  1715. struct btrfs_device_info *devices_info;
  1716. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1717. struct btrfs_device *device;
  1718. u64 skip_space;
  1719. u64 type;
  1720. u64 avail_space;
  1721. u64 used_space;
  1722. u64 min_stripe_size;
  1723. int min_stripes = 1, num_stripes = 1;
  1724. int i = 0, nr_devices;
  1725. int ret;
  1726. /*
  1727. * We aren't under the device list lock, so this is racy-ish, but good
  1728. * enough for our purposes.
  1729. */
  1730. nr_devices = fs_info->fs_devices->open_devices;
  1731. if (!nr_devices) {
  1732. smp_mb();
  1733. nr_devices = fs_info->fs_devices->open_devices;
  1734. ASSERT(nr_devices);
  1735. if (!nr_devices) {
  1736. *free_bytes = 0;
  1737. return 0;
  1738. }
  1739. }
  1740. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1741. GFP_NOFS);
  1742. if (!devices_info)
  1743. return -ENOMEM;
  1744. /* calc min stripe number for data space allocation */
  1745. type = btrfs_get_alloc_profile(root, 1);
  1746. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1747. min_stripes = 2;
  1748. num_stripes = nr_devices;
  1749. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1750. min_stripes = 2;
  1751. num_stripes = 2;
  1752. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1753. min_stripes = 4;
  1754. num_stripes = 4;
  1755. }
  1756. if (type & BTRFS_BLOCK_GROUP_DUP)
  1757. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1758. else
  1759. min_stripe_size = BTRFS_STRIPE_LEN;
  1760. if (fs_info->alloc_start)
  1761. mutex_lock(&fs_devices->device_list_mutex);
  1762. rcu_read_lock();
  1763. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  1764. if (!device->in_fs_metadata || !device->bdev ||
  1765. device->is_tgtdev_for_dev_replace)
  1766. continue;
  1767. if (i >= nr_devices)
  1768. break;
  1769. avail_space = device->total_bytes - device->bytes_used;
  1770. /* align with stripe_len */
  1771. avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
  1772. avail_space *= BTRFS_STRIPE_LEN;
  1773. /*
  1774. * In order to avoid overwriting the superblock on the drive,
  1775. * btrfs starts at an offset of at least 1MB when doing chunk
  1776. * allocation.
  1777. */
  1778. skip_space = SZ_1M;
  1779. /* user can set the offset in fs_info->alloc_start. */
  1780. if (fs_info->alloc_start &&
  1781. fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1782. device->total_bytes) {
  1783. rcu_read_unlock();
  1784. skip_space = max(fs_info->alloc_start, skip_space);
  1785. /*
  1786. * btrfs can not use the free space in
  1787. * [0, skip_space - 1], we must subtract it from the
  1788. * total. In order to implement it, we account the used
  1789. * space in this range first.
  1790. */
  1791. ret = btrfs_account_dev_extents_size(device, 0,
  1792. skip_space - 1,
  1793. &used_space);
  1794. if (ret) {
  1795. kfree(devices_info);
  1796. mutex_unlock(&fs_devices->device_list_mutex);
  1797. return ret;
  1798. }
  1799. rcu_read_lock();
  1800. /* calc the free space in [0, skip_space - 1] */
  1801. skip_space -= used_space;
  1802. }
  1803. /*
  1804. * we can use the free space in [0, skip_space - 1], subtract
  1805. * it from the total.
  1806. */
  1807. if (avail_space && avail_space >= skip_space)
  1808. avail_space -= skip_space;
  1809. else
  1810. avail_space = 0;
  1811. if (avail_space < min_stripe_size)
  1812. continue;
  1813. devices_info[i].dev = device;
  1814. devices_info[i].max_avail = avail_space;
  1815. i++;
  1816. }
  1817. rcu_read_unlock();
  1818. if (fs_info->alloc_start)
  1819. mutex_unlock(&fs_devices->device_list_mutex);
  1820. nr_devices = i;
  1821. btrfs_descending_sort_devices(devices_info, nr_devices);
  1822. i = nr_devices - 1;
  1823. avail_space = 0;
  1824. while (nr_devices >= min_stripes) {
  1825. if (num_stripes > nr_devices)
  1826. num_stripes = nr_devices;
  1827. if (devices_info[i].max_avail >= min_stripe_size) {
  1828. int j;
  1829. u64 alloc_size;
  1830. avail_space += devices_info[i].max_avail * num_stripes;
  1831. alloc_size = devices_info[i].max_avail;
  1832. for (j = i + 1 - num_stripes; j <= i; j++)
  1833. devices_info[j].max_avail -= alloc_size;
  1834. }
  1835. i--;
  1836. nr_devices--;
  1837. }
  1838. kfree(devices_info);
  1839. *free_bytes = avail_space;
  1840. return 0;
  1841. }
  1842. /*
  1843. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1844. *
  1845. * If there's a redundant raid level at DATA block groups, use the respective
  1846. * multiplier to scale the sizes.
  1847. *
  1848. * Unused device space usage is based on simulating the chunk allocator
  1849. * algorithm that respects the device sizes, order of allocations and the
  1850. * 'alloc_start' value, this is a close approximation of the actual use but
  1851. * there are other factors that may change the result (like a new metadata
  1852. * chunk).
  1853. *
  1854. * If metadata is exhausted, f_bavail will be 0.
  1855. *
  1856. * FIXME: not accurate for mixed block groups, total and free/used are ok,
  1857. * available appears slightly larger.
  1858. */
  1859. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1860. {
  1861. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1862. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1863. struct list_head *head = &fs_info->space_info;
  1864. struct btrfs_space_info *found;
  1865. u64 total_used = 0;
  1866. u64 total_free_data = 0;
  1867. u64 total_free_meta = 0;
  1868. int bits = dentry->d_sb->s_blocksize_bits;
  1869. __be32 *fsid = (__be32 *)fs_info->fsid;
  1870. unsigned factor = 1;
  1871. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1872. int ret;
  1873. u64 thresh = 0;
  1874. int mixed = 0;
  1875. /*
  1876. * holding chunk_mutex to avoid allocating new chunks, holding
  1877. * device_list_mutex to avoid the device being removed
  1878. */
  1879. rcu_read_lock();
  1880. list_for_each_entry_rcu(found, head, list) {
  1881. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1882. int i;
  1883. total_free_data += found->disk_total - found->disk_used;
  1884. total_free_data -=
  1885. btrfs_account_ro_block_groups_free_space(found);
  1886. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1887. if (!list_empty(&found->block_groups[i])) {
  1888. switch (i) {
  1889. case BTRFS_RAID_DUP:
  1890. case BTRFS_RAID_RAID1:
  1891. case BTRFS_RAID_RAID10:
  1892. factor = 2;
  1893. }
  1894. }
  1895. }
  1896. }
  1897. /*
  1898. * Metadata in mixed block goup profiles are accounted in data
  1899. */
  1900. if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1901. if (found->flags & BTRFS_BLOCK_GROUP_DATA)
  1902. mixed = 1;
  1903. else
  1904. total_free_meta += found->disk_total -
  1905. found->disk_used;
  1906. }
  1907. total_used += found->disk_used;
  1908. }
  1909. rcu_read_unlock();
  1910. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1911. buf->f_blocks >>= bits;
  1912. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1913. /* Account global block reserve as used, it's in logical size already */
  1914. spin_lock(&block_rsv->lock);
  1915. /* Mixed block groups accounting is not byte-accurate, avoid overflow */
  1916. if (buf->f_bfree >= block_rsv->size >> bits)
  1917. buf->f_bfree -= block_rsv->size >> bits;
  1918. else
  1919. buf->f_bfree = 0;
  1920. spin_unlock(&block_rsv->lock);
  1921. buf->f_bavail = div_u64(total_free_data, factor);
  1922. ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
  1923. if (ret)
  1924. return ret;
  1925. buf->f_bavail += div_u64(total_free_data, factor);
  1926. buf->f_bavail = buf->f_bavail >> bits;
  1927. /*
  1928. * We calculate the remaining metadata space minus global reserve. If
  1929. * this is (supposedly) smaller than zero, there's no space. But this
  1930. * does not hold in practice, the exhausted state happens where's still
  1931. * some positive delta. So we apply some guesswork and compare the
  1932. * delta to a 4M threshold. (Practically observed delta was ~2M.)
  1933. *
  1934. * We probably cannot calculate the exact threshold value because this
  1935. * depends on the internal reservations requested by various
  1936. * operations, so some operations that consume a few metadata will
  1937. * succeed even if the Avail is zero. But this is better than the other
  1938. * way around.
  1939. */
  1940. thresh = 4 * 1024 * 1024;
  1941. if (!mixed && total_free_meta - thresh < block_rsv->size)
  1942. buf->f_bavail = 0;
  1943. buf->f_type = BTRFS_SUPER_MAGIC;
  1944. buf->f_bsize = dentry->d_sb->s_blocksize;
  1945. buf->f_namelen = BTRFS_NAME_LEN;
  1946. /* We treat it as constant endianness (it doesn't matter _which_)
  1947. because we want the fsid to come out the same whether mounted
  1948. on a big-endian or little-endian host */
  1949. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1950. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1951. /* Mask in the root object ID too, to disambiguate subvols */
  1952. buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
  1953. buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
  1954. return 0;
  1955. }
  1956. static void btrfs_kill_super(struct super_block *sb)
  1957. {
  1958. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1959. kill_anon_super(sb);
  1960. free_fs_info(fs_info);
  1961. }
  1962. static struct file_system_type btrfs_fs_type = {
  1963. .owner = THIS_MODULE,
  1964. .name = "btrfs",
  1965. .mount = btrfs_mount,
  1966. .kill_sb = btrfs_kill_super,
  1967. .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
  1968. };
  1969. MODULE_ALIAS_FS("btrfs");
  1970. static int btrfs_control_open(struct inode *inode, struct file *file)
  1971. {
  1972. /*
  1973. * The control file's private_data is used to hold the
  1974. * transaction when it is started and is used to keep
  1975. * track of whether a transaction is already in progress.
  1976. */
  1977. file->private_data = NULL;
  1978. return 0;
  1979. }
  1980. /*
  1981. * used by btrfsctl to scan devices when no FS is mounted
  1982. */
  1983. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  1984. unsigned long arg)
  1985. {
  1986. struct btrfs_ioctl_vol_args *vol;
  1987. struct btrfs_fs_devices *fs_devices;
  1988. int ret = -ENOTTY;
  1989. if (!capable(CAP_SYS_ADMIN))
  1990. return -EPERM;
  1991. vol = memdup_user((void __user *)arg, sizeof(*vol));
  1992. if (IS_ERR(vol))
  1993. return PTR_ERR(vol);
  1994. switch (cmd) {
  1995. case BTRFS_IOC_SCAN_DEV:
  1996. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  1997. &btrfs_fs_type, &fs_devices);
  1998. break;
  1999. case BTRFS_IOC_DEVICES_READY:
  2000. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  2001. &btrfs_fs_type, &fs_devices);
  2002. if (ret)
  2003. break;
  2004. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  2005. break;
  2006. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  2007. ret = btrfs_ioctl_get_supported_features((void __user*)arg);
  2008. break;
  2009. }
  2010. kfree(vol);
  2011. return ret;
  2012. }
  2013. static int btrfs_freeze(struct super_block *sb)
  2014. {
  2015. struct btrfs_trans_handle *trans;
  2016. struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  2017. trans = btrfs_attach_transaction_barrier(root);
  2018. if (IS_ERR(trans)) {
  2019. /* no transaction, don't bother */
  2020. if (PTR_ERR(trans) == -ENOENT)
  2021. return 0;
  2022. return PTR_ERR(trans);
  2023. }
  2024. return btrfs_commit_transaction(trans, root);
  2025. }
  2026. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  2027. {
  2028. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  2029. struct btrfs_fs_devices *cur_devices;
  2030. struct btrfs_device *dev, *first_dev = NULL;
  2031. struct list_head *head;
  2032. struct rcu_string *name;
  2033. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2034. cur_devices = fs_info->fs_devices;
  2035. while (cur_devices) {
  2036. head = &cur_devices->devices;
  2037. list_for_each_entry(dev, head, dev_list) {
  2038. if (dev->missing)
  2039. continue;
  2040. if (!dev->name)
  2041. continue;
  2042. if (!first_dev || dev->devid < first_dev->devid)
  2043. first_dev = dev;
  2044. }
  2045. cur_devices = cur_devices->seed;
  2046. }
  2047. if (first_dev) {
  2048. rcu_read_lock();
  2049. name = rcu_dereference(first_dev->name);
  2050. seq_escape(m, name->str, " \t\n\\");
  2051. rcu_read_unlock();
  2052. } else {
  2053. WARN_ON(1);
  2054. }
  2055. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2056. return 0;
  2057. }
  2058. static const struct super_operations btrfs_super_ops = {
  2059. .drop_inode = btrfs_drop_inode,
  2060. .evict_inode = btrfs_evict_inode,
  2061. .put_super = btrfs_put_super,
  2062. .sync_fs = btrfs_sync_fs,
  2063. .show_options = btrfs_show_options,
  2064. .show_devname = btrfs_show_devname,
  2065. .write_inode = btrfs_write_inode,
  2066. .alloc_inode = btrfs_alloc_inode,
  2067. .destroy_inode = btrfs_destroy_inode,
  2068. .statfs = btrfs_statfs,
  2069. .remount_fs = btrfs_remount,
  2070. .freeze_fs = btrfs_freeze,
  2071. };
  2072. static const struct file_operations btrfs_ctl_fops = {
  2073. .open = btrfs_control_open,
  2074. .unlocked_ioctl = btrfs_control_ioctl,
  2075. .compat_ioctl = btrfs_control_ioctl,
  2076. .owner = THIS_MODULE,
  2077. .llseek = noop_llseek,
  2078. };
  2079. static struct miscdevice btrfs_misc = {
  2080. .minor = BTRFS_MINOR,
  2081. .name = "btrfs-control",
  2082. .fops = &btrfs_ctl_fops
  2083. };
  2084. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  2085. MODULE_ALIAS("devname:btrfs-control");
  2086. static int btrfs_interface_init(void)
  2087. {
  2088. return misc_register(&btrfs_misc);
  2089. }
  2090. static void btrfs_interface_exit(void)
  2091. {
  2092. misc_deregister(&btrfs_misc);
  2093. }
  2094. static void btrfs_print_mod_info(void)
  2095. {
  2096. printk(KERN_INFO "Btrfs loaded"
  2097. #ifdef CONFIG_BTRFS_DEBUG
  2098. ", debug=on"
  2099. #endif
  2100. #ifdef CONFIG_BTRFS_ASSERT
  2101. ", assert=on"
  2102. #endif
  2103. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2104. ", integrity-checker=on"
  2105. #endif
  2106. "\n");
  2107. }
  2108. static int btrfs_run_sanity_tests(void)
  2109. {
  2110. int ret;
  2111. ret = btrfs_init_test_fs();
  2112. if (ret)
  2113. return ret;
  2114. ret = btrfs_test_free_space_cache();
  2115. if (ret)
  2116. goto out;
  2117. ret = btrfs_test_extent_buffer_operations();
  2118. if (ret)
  2119. goto out;
  2120. ret = btrfs_test_extent_io();
  2121. if (ret)
  2122. goto out;
  2123. ret = btrfs_test_inodes();
  2124. if (ret)
  2125. goto out;
  2126. ret = btrfs_test_qgroups();
  2127. if (ret)
  2128. goto out;
  2129. ret = btrfs_test_free_space_tree();
  2130. out:
  2131. btrfs_destroy_test_fs();
  2132. return ret;
  2133. }
  2134. static int __init init_btrfs_fs(void)
  2135. {
  2136. int err;
  2137. err = btrfs_hash_init();
  2138. if (err)
  2139. return err;
  2140. btrfs_props_init();
  2141. err = btrfs_init_sysfs();
  2142. if (err)
  2143. goto free_hash;
  2144. btrfs_init_compress();
  2145. err = btrfs_init_cachep();
  2146. if (err)
  2147. goto free_compress;
  2148. err = extent_io_init();
  2149. if (err)
  2150. goto free_cachep;
  2151. err = extent_map_init();
  2152. if (err)
  2153. goto free_extent_io;
  2154. err = ordered_data_init();
  2155. if (err)
  2156. goto free_extent_map;
  2157. err = btrfs_delayed_inode_init();
  2158. if (err)
  2159. goto free_ordered_data;
  2160. err = btrfs_auto_defrag_init();
  2161. if (err)
  2162. goto free_delayed_inode;
  2163. err = btrfs_delayed_ref_init();
  2164. if (err)
  2165. goto free_auto_defrag;
  2166. err = btrfs_prelim_ref_init();
  2167. if (err)
  2168. goto free_delayed_ref;
  2169. err = btrfs_end_io_wq_init();
  2170. if (err)
  2171. goto free_prelim_ref;
  2172. err = btrfs_interface_init();
  2173. if (err)
  2174. goto free_end_io_wq;
  2175. btrfs_init_lockdep();
  2176. btrfs_print_mod_info();
  2177. err = btrfs_run_sanity_tests();
  2178. if (err)
  2179. goto unregister_ioctl;
  2180. err = register_filesystem(&btrfs_fs_type);
  2181. if (err)
  2182. goto unregister_ioctl;
  2183. return 0;
  2184. unregister_ioctl:
  2185. btrfs_interface_exit();
  2186. free_end_io_wq:
  2187. btrfs_end_io_wq_exit();
  2188. free_prelim_ref:
  2189. btrfs_prelim_ref_exit();
  2190. free_delayed_ref:
  2191. btrfs_delayed_ref_exit();
  2192. free_auto_defrag:
  2193. btrfs_auto_defrag_exit();
  2194. free_delayed_inode:
  2195. btrfs_delayed_inode_exit();
  2196. free_ordered_data:
  2197. ordered_data_exit();
  2198. free_extent_map:
  2199. extent_map_exit();
  2200. free_extent_io:
  2201. extent_io_exit();
  2202. free_cachep:
  2203. btrfs_destroy_cachep();
  2204. free_compress:
  2205. btrfs_exit_compress();
  2206. btrfs_exit_sysfs();
  2207. free_hash:
  2208. btrfs_hash_exit();
  2209. return err;
  2210. }
  2211. static void __exit exit_btrfs_fs(void)
  2212. {
  2213. btrfs_destroy_cachep();
  2214. btrfs_delayed_ref_exit();
  2215. btrfs_auto_defrag_exit();
  2216. btrfs_delayed_inode_exit();
  2217. btrfs_prelim_ref_exit();
  2218. ordered_data_exit();
  2219. extent_map_exit();
  2220. extent_io_exit();
  2221. btrfs_interface_exit();
  2222. btrfs_end_io_wq_exit();
  2223. unregister_filesystem(&btrfs_fs_type);
  2224. btrfs_exit_sysfs();
  2225. btrfs_cleanup_fs_uuids();
  2226. btrfs_exit_compress();
  2227. btrfs_hash_exit();
  2228. }
  2229. late_initcall(init_btrfs_fs);
  2230. module_exit(exit_btrfs_fs)
  2231. MODULE_LICENSE("GPL");