super.c 64 KB

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