super.c 65 KB

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