super.c 63 KB

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