super.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/blkdev.h>
  19. #include <linux/module.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/fs.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/highmem.h>
  24. #include <linux/time.h>
  25. #include <linux/init.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mount.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/parser.h>
  36. #include <linux/ctype.h>
  37. #include <linux/namei.h>
  38. #include <linux/miscdevice.h>
  39. #include <linux/magic.h>
  40. #include <linux/slab.h>
  41. #include <linux/cleancache.h>
  42. #include <linux/ratelimit.h>
  43. #include <linux/btrfs.h>
  44. #include "delayed-inode.h"
  45. #include "ctree.h"
  46. #include "disk-io.h"
  47. #include "transaction.h"
  48. #include "btrfs_inode.h"
  49. #include "print-tree.h"
  50. #include "hash.h"
  51. #include "props.h"
  52. #include "xattr.h"
  53. #include "volumes.h"
  54. #include "export.h"
  55. #include "compression.h"
  56. #include "rcu-string.h"
  57. #include "dev-replace.h"
  58. #include "free-space-cache.h"
  59. #include "backref.h"
  60. #include "tests/btrfs-tests.h"
  61. #include "qgroup.h"
  62. #define CREATE_TRACE_POINTS
  63. #include <trace/events/btrfs.h>
  64. static const struct super_operations btrfs_super_ops;
  65. static struct file_system_type btrfs_fs_type;
  66. static int btrfs_remount(struct super_block *sb, int *flags, char *data);
  67. const char *btrfs_decode_error(int errno)
  68. {
  69. char *errstr = "unknown";
  70. switch (errno) {
  71. case -EIO:
  72. errstr = "IO failure";
  73. break;
  74. case -ENOMEM:
  75. errstr = "Out of memory";
  76. break;
  77. case -EROFS:
  78. errstr = "Readonly filesystem";
  79. break;
  80. case -EEXIST:
  81. errstr = "Object already exists";
  82. break;
  83. case -ENOSPC:
  84. errstr = "No space left";
  85. break;
  86. case -ENOENT:
  87. errstr = "No such entry";
  88. break;
  89. }
  90. return errstr;
  91. }
  92. /* btrfs handle error by forcing the filesystem readonly */
  93. static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  94. {
  95. struct super_block *sb = fs_info->sb;
  96. if (sb->s_flags & MS_RDONLY)
  97. return;
  98. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  99. sb->s_flags |= MS_RDONLY;
  100. btrfs_info(fs_info, "forced readonly");
  101. /*
  102. * Note that a running device replace operation is not
  103. * canceled here although there is no way to update
  104. * the progress. It would add the risk of a deadlock,
  105. * therefore the canceling is omitted. The only penalty
  106. * is that some I/O remains active until the procedure
  107. * completes. The next time when the filesystem is
  108. * mounted writeable again, the device replace
  109. * operation continues.
  110. */
  111. }
  112. }
  113. /*
  114. * __btrfs_handle_fs_error decodes expected errors from the caller and
  115. * invokes the approciate error response.
  116. */
  117. __cold
  118. void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
  119. unsigned int line, int errno, const char *fmt, ...)
  120. {
  121. struct super_block *sb = fs_info->sb;
  122. #ifdef CONFIG_PRINTK
  123. const char *errstr;
  124. #endif
  125. /*
  126. * Special case: if the error is EROFS, and we're already
  127. * under MS_RDONLY, then it is safe here.
  128. */
  129. if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
  130. return;
  131. #ifdef CONFIG_PRINTK
  132. errstr = btrfs_decode_error(errno);
  133. if (fmt) {
  134. struct va_format vaf;
  135. va_list args;
  136. va_start(args, fmt);
  137. vaf.fmt = fmt;
  138. vaf.va = &args;
  139. pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
  140. sb->s_id, function, line, errno, errstr, &vaf);
  141. va_end(args);
  142. } else {
  143. pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
  144. sb->s_id, function, line, errno, errstr);
  145. }
  146. #endif
  147. /*
  148. * Today we only save the error info to memory. Long term we'll
  149. * also send it down to the disk
  150. */
  151. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  152. /* Don't go through full error handling during mount */
  153. if (sb->s_flags & MS_BORN)
  154. btrfs_handle_error(fs_info);
  155. }
  156. #ifdef CONFIG_PRINTK
  157. static const char * const logtypes[] = {
  158. "emergency",
  159. "alert",
  160. "critical",
  161. "error",
  162. "warning",
  163. "notice",
  164. "info",
  165. "debug",
  166. };
  167. /*
  168. * Use one ratelimit state per log level so that a flood of less important
  169. * messages doesn't cause more important ones to be dropped.
  170. */
  171. static struct ratelimit_state printk_limits[] = {
  172. RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
  173. RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
  174. RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
  175. RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
  176. RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
  177. RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
  178. RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
  179. RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
  180. };
  181. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  182. {
  183. struct super_block *sb = fs_info->sb;
  184. char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
  185. struct va_format vaf;
  186. va_list args;
  187. int kern_level;
  188. const char *type = logtypes[4];
  189. struct ratelimit_state *ratelimit = &printk_limits[4];
  190. va_start(args, fmt);
  191. while ((kern_level = printk_get_level(fmt)) != 0) {
  192. size_t size = printk_skip_level(fmt) - fmt;
  193. if (kern_level >= '0' && kern_level <= '7') {
  194. memcpy(lvl, fmt, size);
  195. lvl[size] = '\0';
  196. type = logtypes[kern_level - '0'];
  197. ratelimit = &printk_limits[kern_level - '0'];
  198. }
  199. fmt += size;
  200. }
  201. vaf.fmt = fmt;
  202. vaf.va = &args;
  203. if (__ratelimit(ratelimit))
  204. printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
  205. va_end(args);
  206. }
  207. #endif
  208. /*
  209. * We only mark the transaction aborted and then set the file system read-only.
  210. * This will prevent new transactions from starting or trying to join this
  211. * one.
  212. *
  213. * This means that error recovery at the call site is limited to freeing
  214. * any local memory allocations and passing the error code up without
  215. * further cleanup. The transaction should complete as it normally would
  216. * in the call path but will return -EIO.
  217. *
  218. * We'll complete the cleanup in btrfs_end_transaction and
  219. * btrfs_commit_transaction.
  220. */
  221. __cold
  222. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  223. const char *function,
  224. unsigned int line, int errno)
  225. {
  226. struct btrfs_fs_info *fs_info = trans->fs_info;
  227. trans->aborted = errno;
  228. /* Nothing used. The other threads that have joined this
  229. * transaction may be able to continue. */
  230. if (!trans->dirty && list_empty(&trans->new_bgs)) {
  231. const char *errstr;
  232. errstr = btrfs_decode_error(errno);
  233. btrfs_warn(fs_info,
  234. "%s:%d: Aborting unused transaction(%s).",
  235. function, line, errstr);
  236. return;
  237. }
  238. WRITE_ONCE(trans->transaction->aborted, errno);
  239. /* Wake up anybody who may be waiting on this transaction */
  240. wake_up(&fs_info->transaction_wait);
  241. wake_up(&fs_info->transaction_blocked_wait);
  242. __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
  243. }
  244. /*
  245. * __btrfs_panic decodes unexpected, fatal errors from the caller,
  246. * issues an alert, and either panics or BUGs, depending on mount options.
  247. */
  248. __cold
  249. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  250. unsigned int line, int errno, const char *fmt, ...)
  251. {
  252. char *s_id = "<unknown>";
  253. const char *errstr;
  254. struct va_format vaf = { .fmt = fmt };
  255. va_list args;
  256. if (fs_info)
  257. s_id = fs_info->sb->s_id;
  258. va_start(args, fmt);
  259. vaf.va = &args;
  260. errstr = btrfs_decode_error(errno);
  261. if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
  262. panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
  263. s_id, function, line, &vaf, errno, errstr);
  264. btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  265. function, line, &vaf, errno, errstr);
  266. va_end(args);
  267. /* Caller calls BUG() */
  268. }
  269. static void btrfs_put_super(struct super_block *sb)
  270. {
  271. close_ctree(btrfs_sb(sb));
  272. }
  273. enum {
  274. Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
  275. Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  276. Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
  277. Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  278. Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
  279. Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
  280. Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
  281. Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
  282. Opt_skip_balance, Opt_check_integrity,
  283. Opt_check_integrity_including_extent_data,
  284. Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
  285. Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
  286. Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
  287. Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
  288. Opt_nologreplay, Opt_norecovery,
  289. #ifdef CONFIG_BTRFS_DEBUG
  290. Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
  291. #endif
  292. Opt_err,
  293. };
  294. static const match_table_t tokens = {
  295. {Opt_degraded, "degraded"},
  296. {Opt_subvol, "subvol=%s"},
  297. {Opt_subvolid, "subvolid=%s"},
  298. {Opt_device, "device=%s"},
  299. {Opt_nodatasum, "nodatasum"},
  300. {Opt_datasum, "datasum"},
  301. {Opt_nodatacow, "nodatacow"},
  302. {Opt_datacow, "datacow"},
  303. {Opt_nobarrier, "nobarrier"},
  304. {Opt_barrier, "barrier"},
  305. {Opt_max_inline, "max_inline=%s"},
  306. {Opt_alloc_start, "alloc_start=%s"},
  307. {Opt_thread_pool, "thread_pool=%d"},
  308. {Opt_compress, "compress"},
  309. {Opt_compress_type, "compress=%s"},
  310. {Opt_compress_force, "compress-force"},
  311. {Opt_compress_force_type, "compress-force=%s"},
  312. {Opt_ssd, "ssd"},
  313. {Opt_ssd_spread, "ssd_spread"},
  314. {Opt_nossd, "nossd"},
  315. {Opt_acl, "acl"},
  316. {Opt_noacl, "noacl"},
  317. {Opt_notreelog, "notreelog"},
  318. {Opt_treelog, "treelog"},
  319. {Opt_nologreplay, "nologreplay"},
  320. {Opt_norecovery, "norecovery"},
  321. {Opt_flushoncommit, "flushoncommit"},
  322. {Opt_noflushoncommit, "noflushoncommit"},
  323. {Opt_ratio, "metadata_ratio=%d"},
  324. {Opt_discard, "discard"},
  325. {Opt_nodiscard, "nodiscard"},
  326. {Opt_space_cache, "space_cache"},
  327. {Opt_space_cache_version, "space_cache=%s"},
  328. {Opt_clear_cache, "clear_cache"},
  329. {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
  330. {Opt_enospc_debug, "enospc_debug"},
  331. {Opt_noenospc_debug, "noenospc_debug"},
  332. {Opt_subvolrootid, "subvolrootid=%d"},
  333. {Opt_defrag, "autodefrag"},
  334. {Opt_nodefrag, "noautodefrag"},
  335. {Opt_inode_cache, "inode_cache"},
  336. {Opt_noinode_cache, "noinode_cache"},
  337. {Opt_no_space_cache, "nospace_cache"},
  338. {Opt_recovery, "recovery"}, /* deprecated */
  339. {Opt_usebackuproot, "usebackuproot"},
  340. {Opt_skip_balance, "skip_balance"},
  341. {Opt_check_integrity, "check_int"},
  342. {Opt_check_integrity_including_extent_data, "check_int_data"},
  343. {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
  344. {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
  345. {Opt_fatal_errors, "fatal_errors=%s"},
  346. {Opt_commit_interval, "commit=%d"},
  347. #ifdef CONFIG_BTRFS_DEBUG
  348. {Opt_fragment_data, "fragment=data"},
  349. {Opt_fragment_metadata, "fragment=metadata"},
  350. {Opt_fragment_all, "fragment=all"},
  351. #endif
  352. {Opt_err, NULL},
  353. };
  354. /*
  355. * Regular mount options parser. Everything that is needed only when
  356. * reading in a new superblock is parsed here.
  357. * XXX JDM: This needs to be cleaned up for remount.
  358. */
  359. int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
  360. unsigned long new_flags)
  361. {
  362. substring_t args[MAX_OPT_ARGS];
  363. char *p, *num, *orig = NULL;
  364. u64 cache_gen;
  365. int intarg;
  366. int ret = 0;
  367. char *compress_type;
  368. bool compress_force = false;
  369. enum btrfs_compression_type saved_compress_type;
  370. bool saved_compress_force;
  371. int no_compress = 0;
  372. cache_gen = btrfs_super_cache_generation(info->super_copy);
  373. if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
  374. btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
  375. else if (cache_gen)
  376. btrfs_set_opt(info->mount_opt, SPACE_CACHE);
  377. /*
  378. * Even the options are empty, we still need to do extra check
  379. * against new flags
  380. */
  381. if (!options)
  382. goto check;
  383. /*
  384. * strsep changes the string, duplicate it because parse_options
  385. * gets called twice
  386. */
  387. options = kstrdup(options, GFP_NOFS);
  388. if (!options)
  389. return -ENOMEM;
  390. orig = options;
  391. while ((p = strsep(&options, ",")) != NULL) {
  392. int token;
  393. if (!*p)
  394. continue;
  395. token = match_token(p, tokens, args);
  396. switch (token) {
  397. case Opt_degraded:
  398. btrfs_info(info, "allowing degraded mounts");
  399. btrfs_set_opt(info->mount_opt, DEGRADED);
  400. break;
  401. case Opt_subvol:
  402. case Opt_subvolid:
  403. case Opt_subvolrootid:
  404. case Opt_device:
  405. /*
  406. * These are parsed by btrfs_parse_early_options
  407. * and can be happily ignored here.
  408. */
  409. break;
  410. case Opt_nodatasum:
  411. btrfs_set_and_info(info, NODATASUM,
  412. "setting nodatasum");
  413. break;
  414. case Opt_datasum:
  415. if (btrfs_test_opt(info, NODATASUM)) {
  416. if (btrfs_test_opt(info, NODATACOW))
  417. btrfs_info(info,
  418. "setting datasum, datacow enabled");
  419. else
  420. btrfs_info(info, "setting datasum");
  421. }
  422. btrfs_clear_opt(info->mount_opt, NODATACOW);
  423. btrfs_clear_opt(info->mount_opt, NODATASUM);
  424. break;
  425. case Opt_nodatacow:
  426. if (!btrfs_test_opt(info, NODATACOW)) {
  427. if (!btrfs_test_opt(info, COMPRESS) ||
  428. !btrfs_test_opt(info, FORCE_COMPRESS)) {
  429. btrfs_info(info,
  430. "setting nodatacow, compression disabled");
  431. } else {
  432. btrfs_info(info, "setting nodatacow");
  433. }
  434. }
  435. btrfs_clear_opt(info->mount_opt, COMPRESS);
  436. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  437. btrfs_set_opt(info->mount_opt, NODATACOW);
  438. btrfs_set_opt(info->mount_opt, NODATASUM);
  439. break;
  440. case Opt_datacow:
  441. btrfs_clear_and_info(info, NODATACOW,
  442. "setting datacow");
  443. break;
  444. case Opt_compress_force:
  445. case Opt_compress_force_type:
  446. compress_force = true;
  447. /* Fallthrough */
  448. case Opt_compress:
  449. case Opt_compress_type:
  450. saved_compress_type = btrfs_test_opt(info,
  451. COMPRESS) ?
  452. info->compress_type : BTRFS_COMPRESS_NONE;
  453. saved_compress_force =
  454. btrfs_test_opt(info, FORCE_COMPRESS);
  455. if (token == Opt_compress ||
  456. token == Opt_compress_force ||
  457. strcmp(args[0].from, "zlib") == 0) {
  458. compress_type = "zlib";
  459. info->compress_type = BTRFS_COMPRESS_ZLIB;
  460. btrfs_set_opt(info->mount_opt, COMPRESS);
  461. btrfs_clear_opt(info->mount_opt, NODATACOW);
  462. btrfs_clear_opt(info->mount_opt, NODATASUM);
  463. no_compress = 0;
  464. } else if (strcmp(args[0].from, "lzo") == 0) {
  465. compress_type = "lzo";
  466. info->compress_type = BTRFS_COMPRESS_LZO;
  467. btrfs_set_opt(info->mount_opt, COMPRESS);
  468. btrfs_clear_opt(info->mount_opt, NODATACOW);
  469. btrfs_clear_opt(info->mount_opt, NODATASUM);
  470. btrfs_set_fs_incompat(info, COMPRESS_LZO);
  471. no_compress = 0;
  472. } else if (strncmp(args[0].from, "no", 2) == 0) {
  473. compress_type = "no";
  474. btrfs_clear_opt(info->mount_opt, COMPRESS);
  475. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  476. compress_force = false;
  477. no_compress++;
  478. } else {
  479. ret = -EINVAL;
  480. goto out;
  481. }
  482. if (compress_force) {
  483. btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
  484. } else {
  485. /*
  486. * If we remount from compress-force=xxx to
  487. * compress=xxx, we need clear FORCE_COMPRESS
  488. * flag, otherwise, there is no way for users
  489. * to disable forcible compression separately.
  490. */
  491. btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  492. }
  493. if ((btrfs_test_opt(info, COMPRESS) &&
  494. (info->compress_type != saved_compress_type ||
  495. compress_force != saved_compress_force)) ||
  496. (!btrfs_test_opt(info, COMPRESS) &&
  497. no_compress == 1)) {
  498. btrfs_info(info, "%s %s compression",
  499. (compress_force) ? "force" : "use",
  500. compress_type);
  501. }
  502. compress_force = false;
  503. break;
  504. case Opt_ssd:
  505. btrfs_set_and_info(info, SSD,
  506. "use ssd allocation scheme");
  507. btrfs_clear_opt(info->mount_opt, NOSSD);
  508. break;
  509. case Opt_ssd_spread:
  510. btrfs_set_and_info(info, SSD_SPREAD,
  511. "use spread ssd allocation scheme");
  512. btrfs_set_opt(info->mount_opt, SSD);
  513. btrfs_clear_opt(info->mount_opt, NOSSD);
  514. break;
  515. case Opt_nossd:
  516. btrfs_set_and_info(info, NOSSD,
  517. "not using ssd allocation scheme");
  518. btrfs_clear_opt(info->mount_opt, SSD);
  519. btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
  520. break;
  521. case Opt_barrier:
  522. btrfs_clear_and_info(info, NOBARRIER,
  523. "turning on barriers");
  524. break;
  525. case Opt_nobarrier:
  526. btrfs_set_and_info(info, NOBARRIER,
  527. "turning off barriers");
  528. break;
  529. case Opt_thread_pool:
  530. ret = match_int(&args[0], &intarg);
  531. if (ret) {
  532. goto out;
  533. } else if (intarg > 0) {
  534. info->thread_pool_size = intarg;
  535. } else {
  536. ret = -EINVAL;
  537. goto out;
  538. }
  539. break;
  540. case Opt_max_inline:
  541. num = match_strdup(&args[0]);
  542. if (num) {
  543. info->max_inline = memparse(num, NULL);
  544. kfree(num);
  545. if (info->max_inline) {
  546. info->max_inline = min_t(u64,
  547. info->max_inline,
  548. info->sectorsize);
  549. }
  550. btrfs_info(info, "max_inline at %llu",
  551. info->max_inline);
  552. } else {
  553. ret = -ENOMEM;
  554. goto out;
  555. }
  556. break;
  557. case Opt_alloc_start:
  558. 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)
  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 = super_setup_bdi(sb);
  1067. if (err) {
  1068. btrfs_err(fs_info, "super_setup_bdi failed");
  1069. return err;
  1070. }
  1071. err = open_ctree(sb, fs_devices, (char *)data);
  1072. if (err) {
  1073. btrfs_err(fs_info, "open_ctree failed");
  1074. return err;
  1075. }
  1076. key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  1077. key.type = BTRFS_INODE_ITEM_KEY;
  1078. key.offset = 0;
  1079. inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
  1080. if (IS_ERR(inode)) {
  1081. err = PTR_ERR(inode);
  1082. goto fail_close;
  1083. }
  1084. sb->s_root = d_make_root(inode);
  1085. if (!sb->s_root) {
  1086. err = -ENOMEM;
  1087. goto fail_close;
  1088. }
  1089. save_mount_options(sb, data);
  1090. cleancache_init_fs(sb);
  1091. sb->s_flags |= MS_ACTIVE;
  1092. return 0;
  1093. fail_close:
  1094. close_ctree(fs_info);
  1095. return err;
  1096. }
  1097. int btrfs_sync_fs(struct super_block *sb, int wait)
  1098. {
  1099. struct btrfs_trans_handle *trans;
  1100. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1101. struct btrfs_root *root = fs_info->tree_root;
  1102. trace_btrfs_sync_fs(fs_info, wait);
  1103. if (!wait) {
  1104. filemap_flush(fs_info->btree_inode->i_mapping);
  1105. return 0;
  1106. }
  1107. btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
  1108. trans = btrfs_attach_transaction_barrier(root);
  1109. if (IS_ERR(trans)) {
  1110. /* no transaction, don't bother */
  1111. if (PTR_ERR(trans) == -ENOENT) {
  1112. /*
  1113. * Exit unless we have some pending changes
  1114. * that need to go through commit
  1115. */
  1116. if (fs_info->pending_changes == 0)
  1117. return 0;
  1118. /*
  1119. * A non-blocking test if the fs is frozen. We must not
  1120. * start a new transaction here otherwise a deadlock
  1121. * happens. The pending operations are delayed to the
  1122. * next commit after thawing.
  1123. */
  1124. if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
  1125. __sb_end_write(sb, SB_FREEZE_WRITE);
  1126. else
  1127. return 0;
  1128. trans = btrfs_start_transaction(root, 0);
  1129. }
  1130. if (IS_ERR(trans))
  1131. return PTR_ERR(trans);
  1132. }
  1133. return btrfs_commit_transaction(trans);
  1134. }
  1135. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  1136. {
  1137. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  1138. char *compress_type;
  1139. if (btrfs_test_opt(info, DEGRADED))
  1140. seq_puts(seq, ",degraded");
  1141. if (btrfs_test_opt(info, NODATASUM))
  1142. seq_puts(seq, ",nodatasum");
  1143. if (btrfs_test_opt(info, NODATACOW))
  1144. seq_puts(seq, ",nodatacow");
  1145. if (btrfs_test_opt(info, NOBARRIER))
  1146. seq_puts(seq, ",nobarrier");
  1147. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  1148. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  1149. if (info->alloc_start != 0)
  1150. seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
  1151. if (info->thread_pool_size != min_t(unsigned long,
  1152. num_online_cpus() + 2, 8))
  1153. seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
  1154. if (btrfs_test_opt(info, COMPRESS)) {
  1155. if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  1156. compress_type = "zlib";
  1157. else
  1158. compress_type = "lzo";
  1159. if (btrfs_test_opt(info, FORCE_COMPRESS))
  1160. seq_printf(seq, ",compress-force=%s", compress_type);
  1161. else
  1162. seq_printf(seq, ",compress=%s", compress_type);
  1163. }
  1164. if (btrfs_test_opt(info, NOSSD))
  1165. seq_puts(seq, ",nossd");
  1166. if (btrfs_test_opt(info, SSD_SPREAD))
  1167. seq_puts(seq, ",ssd_spread");
  1168. else if (btrfs_test_opt(info, SSD))
  1169. seq_puts(seq, ",ssd");
  1170. if (btrfs_test_opt(info, NOTREELOG))
  1171. seq_puts(seq, ",notreelog");
  1172. if (btrfs_test_opt(info, NOLOGREPLAY))
  1173. seq_puts(seq, ",nologreplay");
  1174. if (btrfs_test_opt(info, FLUSHONCOMMIT))
  1175. seq_puts(seq, ",flushoncommit");
  1176. if (btrfs_test_opt(info, DISCARD))
  1177. seq_puts(seq, ",discard");
  1178. if (!(info->sb->s_flags & MS_POSIXACL))
  1179. seq_puts(seq, ",noacl");
  1180. if (btrfs_test_opt(info, SPACE_CACHE))
  1181. seq_puts(seq, ",space_cache");
  1182. else if (btrfs_test_opt(info, FREE_SPACE_TREE))
  1183. seq_puts(seq, ",space_cache=v2");
  1184. else
  1185. seq_puts(seq, ",nospace_cache");
  1186. if (btrfs_test_opt(info, RESCAN_UUID_TREE))
  1187. seq_puts(seq, ",rescan_uuid_tree");
  1188. if (btrfs_test_opt(info, CLEAR_CACHE))
  1189. seq_puts(seq, ",clear_cache");
  1190. if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
  1191. seq_puts(seq, ",user_subvol_rm_allowed");
  1192. if (btrfs_test_opt(info, ENOSPC_DEBUG))
  1193. seq_puts(seq, ",enospc_debug");
  1194. if (btrfs_test_opt(info, AUTO_DEFRAG))
  1195. seq_puts(seq, ",autodefrag");
  1196. if (btrfs_test_opt(info, INODE_MAP_CACHE))
  1197. seq_puts(seq, ",inode_cache");
  1198. if (btrfs_test_opt(info, SKIP_BALANCE))
  1199. seq_puts(seq, ",skip_balance");
  1200. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  1201. if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
  1202. seq_puts(seq, ",check_int_data");
  1203. else if (btrfs_test_opt(info, CHECK_INTEGRITY))
  1204. seq_puts(seq, ",check_int");
  1205. if (info->check_integrity_print_mask)
  1206. seq_printf(seq, ",check_int_print_mask=%d",
  1207. info->check_integrity_print_mask);
  1208. #endif
  1209. if (info->metadata_ratio)
  1210. seq_printf(seq, ",metadata_ratio=%d",
  1211. info->metadata_ratio);
  1212. if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
  1213. seq_puts(seq, ",fatal_errors=panic");
  1214. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1215. seq_printf(seq, ",commit=%d", info->commit_interval);
  1216. #ifdef CONFIG_BTRFS_DEBUG
  1217. if (btrfs_test_opt(info, FRAGMENT_DATA))
  1218. seq_puts(seq, ",fragment=data");
  1219. if (btrfs_test_opt(info, FRAGMENT_METADATA))
  1220. seq_puts(seq, ",fragment=metadata");
  1221. #endif
  1222. seq_printf(seq, ",subvolid=%llu",
  1223. BTRFS_I(d_inode(dentry))->root->root_key.objectid);
  1224. seq_puts(seq, ",subvol=");
  1225. seq_dentry(seq, dentry, " \t\n\\");
  1226. return 0;
  1227. }
  1228. static int btrfs_test_super(struct super_block *s, void *data)
  1229. {
  1230. struct btrfs_fs_info *p = data;
  1231. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1232. return fs_info->fs_devices == p->fs_devices;
  1233. }
  1234. static int btrfs_set_super(struct super_block *s, void *data)
  1235. {
  1236. int err = set_anon_super(s, data);
  1237. if (!err)
  1238. s->s_fs_info = data;
  1239. return err;
  1240. }
  1241. /*
  1242. * subvolumes are identified by ino 256
  1243. */
  1244. static inline int is_subvolume_inode(struct inode *inode)
  1245. {
  1246. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1247. return 1;
  1248. return 0;
  1249. }
  1250. /*
  1251. * This will add subvolid=0 to the argument string while removing any subvol=
  1252. * and subvolid= arguments to make sure we get the top-level root for path
  1253. * walking to the subvol we want.
  1254. */
  1255. static char *setup_root_args(char *args)
  1256. {
  1257. char *buf, *dst, *sep;
  1258. if (!args)
  1259. return kstrdup("subvolid=0", GFP_NOFS);
  1260. /* The worst case is that we add ",subvolid=0" to the end. */
  1261. buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
  1262. if (!buf)
  1263. return NULL;
  1264. while (1) {
  1265. sep = strchrnul(args, ',');
  1266. if (!strstarts(args, "subvol=") &&
  1267. !strstarts(args, "subvolid=")) {
  1268. memcpy(dst, args, sep - args);
  1269. dst += sep - args;
  1270. *dst++ = ',';
  1271. }
  1272. if (*sep)
  1273. args = sep + 1;
  1274. else
  1275. break;
  1276. }
  1277. strcpy(dst, "subvolid=0");
  1278. return buf;
  1279. }
  1280. static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  1281. int flags, const char *device_name,
  1282. char *data)
  1283. {
  1284. struct dentry *root;
  1285. struct vfsmount *mnt = NULL;
  1286. char *newargs;
  1287. int ret;
  1288. newargs = setup_root_args(data);
  1289. if (!newargs) {
  1290. root = ERR_PTR(-ENOMEM);
  1291. goto out;
  1292. }
  1293. mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
  1294. if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
  1295. if (flags & MS_RDONLY) {
  1296. mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
  1297. device_name, newargs);
  1298. } else {
  1299. mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
  1300. device_name, newargs);
  1301. if (IS_ERR(mnt)) {
  1302. root = ERR_CAST(mnt);
  1303. mnt = NULL;
  1304. goto out;
  1305. }
  1306. down_write(&mnt->mnt_sb->s_umount);
  1307. ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
  1308. up_write(&mnt->mnt_sb->s_umount);
  1309. if (ret < 0) {
  1310. root = ERR_PTR(ret);
  1311. goto out;
  1312. }
  1313. }
  1314. }
  1315. if (IS_ERR(mnt)) {
  1316. root = ERR_CAST(mnt);
  1317. mnt = NULL;
  1318. goto out;
  1319. }
  1320. if (!subvol_name) {
  1321. if (!subvol_objectid) {
  1322. ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  1323. &subvol_objectid);
  1324. if (ret) {
  1325. root = ERR_PTR(ret);
  1326. goto out;
  1327. }
  1328. }
  1329. subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
  1330. subvol_objectid);
  1331. if (IS_ERR(subvol_name)) {
  1332. root = ERR_CAST(subvol_name);
  1333. subvol_name = NULL;
  1334. goto out;
  1335. }
  1336. }
  1337. root = mount_subtree(mnt, subvol_name);
  1338. /* mount_subtree() drops our reference on the vfsmount. */
  1339. mnt = NULL;
  1340. if (!IS_ERR(root)) {
  1341. struct super_block *s = root->d_sb;
  1342. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1343. struct inode *root_inode = d_inode(root);
  1344. u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
  1345. ret = 0;
  1346. if (!is_subvolume_inode(root_inode)) {
  1347. btrfs_err(fs_info, "'%s' is not a valid subvolume",
  1348. subvol_name);
  1349. ret = -EINVAL;
  1350. }
  1351. if (subvol_objectid && root_objectid != subvol_objectid) {
  1352. /*
  1353. * This will also catch a race condition where a
  1354. * subvolume which was passed by ID is renamed and
  1355. * another subvolume is renamed over the old location.
  1356. */
  1357. btrfs_err(fs_info,
  1358. "subvol '%s' does not match subvolid %llu",
  1359. subvol_name, subvol_objectid);
  1360. ret = -EINVAL;
  1361. }
  1362. if (ret) {
  1363. dput(root);
  1364. root = ERR_PTR(ret);
  1365. deactivate_locked_super(s);
  1366. }
  1367. }
  1368. out:
  1369. mntput(mnt);
  1370. kfree(newargs);
  1371. kfree(subvol_name);
  1372. return root;
  1373. }
  1374. static int parse_security_options(char *orig_opts,
  1375. struct security_mnt_opts *sec_opts)
  1376. {
  1377. char *secdata = NULL;
  1378. int ret = 0;
  1379. secdata = alloc_secdata();
  1380. if (!secdata)
  1381. return -ENOMEM;
  1382. ret = security_sb_copy_data(orig_opts, secdata);
  1383. if (ret) {
  1384. free_secdata(secdata);
  1385. return ret;
  1386. }
  1387. ret = security_sb_parse_opts_str(secdata, sec_opts);
  1388. free_secdata(secdata);
  1389. return ret;
  1390. }
  1391. static int setup_security_options(struct btrfs_fs_info *fs_info,
  1392. struct super_block *sb,
  1393. struct security_mnt_opts *sec_opts)
  1394. {
  1395. int ret = 0;
  1396. /*
  1397. * Call security_sb_set_mnt_opts() to check whether new sec_opts
  1398. * is valid.
  1399. */
  1400. ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
  1401. if (ret)
  1402. return ret;
  1403. #ifdef CONFIG_SECURITY
  1404. if (!fs_info->security_opts.num_mnt_opts) {
  1405. /* first time security setup, copy sec_opts to fs_info */
  1406. memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
  1407. } else {
  1408. /*
  1409. * Since SELinux (the only one supporting security_mnt_opts)
  1410. * does NOT support changing context during remount/mount of
  1411. * the same sb, this must be the same or part of the same
  1412. * security options, just free it.
  1413. */
  1414. security_free_mnt_opts(sec_opts);
  1415. }
  1416. #endif
  1417. return ret;
  1418. }
  1419. /*
  1420. * Find a superblock for the given device / mount point.
  1421. *
  1422. * Note: This is based on get_sb_bdev from fs/super.c with a few additions
  1423. * for multiple device setup. Make sure to keep it in sync.
  1424. */
  1425. static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
  1426. const char *device_name, void *data)
  1427. {
  1428. struct block_device *bdev = NULL;
  1429. struct super_block *s;
  1430. struct btrfs_fs_devices *fs_devices = NULL;
  1431. struct btrfs_fs_info *fs_info = NULL;
  1432. struct security_mnt_opts new_sec_opts;
  1433. fmode_t mode = FMODE_READ;
  1434. char *subvol_name = NULL;
  1435. u64 subvol_objectid = 0;
  1436. int error = 0;
  1437. if (!(flags & MS_RDONLY))
  1438. mode |= FMODE_WRITE;
  1439. error = btrfs_parse_early_options(data, mode, fs_type,
  1440. &subvol_name, &subvol_objectid,
  1441. &fs_devices);
  1442. if (error) {
  1443. kfree(subvol_name);
  1444. return ERR_PTR(error);
  1445. }
  1446. if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  1447. /* mount_subvol() will free subvol_name. */
  1448. return mount_subvol(subvol_name, subvol_objectid, flags,
  1449. device_name, data);
  1450. }
  1451. security_init_mnt_opts(&new_sec_opts);
  1452. if (data) {
  1453. error = parse_security_options(data, &new_sec_opts);
  1454. if (error)
  1455. return ERR_PTR(error);
  1456. }
  1457. error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
  1458. if (error)
  1459. goto error_sec_opts;
  1460. /*
  1461. * Setup a dummy root and fs_info for test/set super. This is because
  1462. * we don't actually fill this stuff out until open_ctree, but we need
  1463. * it for searching for existing supers, so this lets us do that and
  1464. * then open_ctree will properly initialize everything later.
  1465. */
  1466. fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
  1467. if (!fs_info) {
  1468. error = -ENOMEM;
  1469. goto error_sec_opts;
  1470. }
  1471. fs_info->fs_devices = fs_devices;
  1472. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1473. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  1474. security_init_mnt_opts(&fs_info->security_opts);
  1475. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1476. error = -ENOMEM;
  1477. goto error_fs_info;
  1478. }
  1479. error = btrfs_open_devices(fs_devices, mode, fs_type);
  1480. if (error)
  1481. goto error_fs_info;
  1482. if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  1483. error = -EACCES;
  1484. goto error_close_devices;
  1485. }
  1486. bdev = fs_devices->latest_bdev;
  1487. s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  1488. fs_info);
  1489. if (IS_ERR(s)) {
  1490. error = PTR_ERR(s);
  1491. goto error_close_devices;
  1492. }
  1493. if (s->s_root) {
  1494. btrfs_close_devices(fs_devices);
  1495. free_fs_info(fs_info);
  1496. if ((flags ^ s->s_flags) & MS_RDONLY)
  1497. error = -EBUSY;
  1498. } else {
  1499. snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
  1500. btrfs_sb(s)->bdev_holder = fs_type;
  1501. error = btrfs_fill_super(s, fs_devices, data);
  1502. }
  1503. if (error) {
  1504. deactivate_locked_super(s);
  1505. goto error_sec_opts;
  1506. }
  1507. fs_info = btrfs_sb(s);
  1508. error = setup_security_options(fs_info, s, &new_sec_opts);
  1509. if (error) {
  1510. deactivate_locked_super(s);
  1511. goto error_sec_opts;
  1512. }
  1513. return dget(s->s_root);
  1514. error_close_devices:
  1515. btrfs_close_devices(fs_devices);
  1516. error_fs_info:
  1517. free_fs_info(fs_info);
  1518. error_sec_opts:
  1519. security_free_mnt_opts(&new_sec_opts);
  1520. return ERR_PTR(error);
  1521. }
  1522. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1523. int new_pool_size, int old_pool_size)
  1524. {
  1525. if (new_pool_size == old_pool_size)
  1526. return;
  1527. fs_info->thread_pool_size = new_pool_size;
  1528. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1529. old_pool_size, new_pool_size);
  1530. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1531. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1532. btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
  1533. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1534. btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  1535. btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  1536. btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  1537. new_pool_size);
  1538. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1539. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1540. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1541. btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
  1542. btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  1543. new_pool_size);
  1544. }
  1545. static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
  1546. {
  1547. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1548. }
  1549. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1550. unsigned long old_opts, int flags)
  1551. {
  1552. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1553. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1554. (flags & MS_RDONLY))) {
  1555. /* wait for any defraggers to finish */
  1556. wait_event(fs_info->transaction_wait,
  1557. (atomic_read(&fs_info->defrag_running) == 0));
  1558. if (flags & MS_RDONLY)
  1559. sync_filesystem(fs_info->sb);
  1560. }
  1561. }
  1562. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1563. unsigned long old_opts)
  1564. {
  1565. /*
  1566. * We need to cleanup all defragable inodes if the autodefragment is
  1567. * close or the filesystem is read only.
  1568. */
  1569. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1570. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1571. (fs_info->sb->s_flags & MS_RDONLY))) {
  1572. btrfs_cleanup_defrag_inodes(fs_info);
  1573. }
  1574. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1575. }
  1576. static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  1577. {
  1578. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1579. struct btrfs_root *root = fs_info->tree_root;
  1580. unsigned old_flags = sb->s_flags;
  1581. unsigned long old_opts = fs_info->mount_opt;
  1582. unsigned long old_compress_type = fs_info->compress_type;
  1583. u64 old_max_inline = fs_info->max_inline;
  1584. u64 old_alloc_start = fs_info->alloc_start;
  1585. int old_thread_pool_size = fs_info->thread_pool_size;
  1586. unsigned int old_metadata_ratio = fs_info->metadata_ratio;
  1587. int ret;
  1588. sync_filesystem(sb);
  1589. btrfs_remount_prepare(fs_info);
  1590. if (data) {
  1591. struct security_mnt_opts new_sec_opts;
  1592. security_init_mnt_opts(&new_sec_opts);
  1593. ret = parse_security_options(data, &new_sec_opts);
  1594. if (ret)
  1595. goto restore;
  1596. ret = setup_security_options(fs_info, sb,
  1597. &new_sec_opts);
  1598. if (ret) {
  1599. security_free_mnt_opts(&new_sec_opts);
  1600. goto restore;
  1601. }
  1602. }
  1603. ret = btrfs_parse_options(fs_info, data, *flags);
  1604. if (ret) {
  1605. ret = -EINVAL;
  1606. goto restore;
  1607. }
  1608. btrfs_remount_begin(fs_info, old_opts, *flags);
  1609. btrfs_resize_thread_pool(fs_info,
  1610. fs_info->thread_pool_size, old_thread_pool_size);
  1611. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  1612. goto out;
  1613. if (*flags & MS_RDONLY) {
  1614. /*
  1615. * this also happens on 'umount -rf' or on shutdown, when
  1616. * the filesystem is busy.
  1617. */
  1618. cancel_work_sync(&fs_info->async_reclaim_work);
  1619. /* wait for the uuid_scan task to finish */
  1620. down(&fs_info->uuid_tree_rescan_sem);
  1621. /* avoid complains from lockdep et al. */
  1622. up(&fs_info->uuid_tree_rescan_sem);
  1623. sb->s_flags |= MS_RDONLY;
  1624. /*
  1625. * Setting MS_RDONLY will put the cleaner thread to
  1626. * sleep at the next loop if it's already active.
  1627. * If it's already asleep, we'll leave unused block
  1628. * groups on disk until we're mounted read-write again
  1629. * unless we clean them up here.
  1630. */
  1631. btrfs_delete_unused_bgs(fs_info);
  1632. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1633. btrfs_scrub_cancel(fs_info);
  1634. btrfs_pause_balance(fs_info);
  1635. ret = btrfs_commit_super(fs_info);
  1636. if (ret)
  1637. goto restore;
  1638. } else {
  1639. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  1640. btrfs_err(fs_info,
  1641. "Remounting read-write after error is not allowed");
  1642. ret = -EINVAL;
  1643. goto restore;
  1644. }
  1645. if (fs_info->fs_devices->rw_devices == 0) {
  1646. ret = -EACCES;
  1647. goto restore;
  1648. }
  1649. if (fs_info->fs_devices->missing_devices >
  1650. fs_info->num_tolerated_disk_barrier_failures) {
  1651. btrfs_warn(fs_info,
  1652. "too many missing devices, writeable remount is not allowed");
  1653. ret = -EACCES;
  1654. goto restore;
  1655. }
  1656. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1657. ret = -EINVAL;
  1658. goto restore;
  1659. }
  1660. ret = btrfs_cleanup_fs_roots(fs_info);
  1661. if (ret)
  1662. goto restore;
  1663. /* recover relocation */
  1664. mutex_lock(&fs_info->cleaner_mutex);
  1665. ret = btrfs_recover_relocation(root);
  1666. mutex_unlock(&fs_info->cleaner_mutex);
  1667. if (ret)
  1668. goto restore;
  1669. ret = btrfs_resume_balance_async(fs_info);
  1670. if (ret)
  1671. goto restore;
  1672. ret = btrfs_resume_dev_replace_async(fs_info);
  1673. if (ret) {
  1674. btrfs_warn(fs_info, "failed to resume dev_replace");
  1675. goto restore;
  1676. }
  1677. if (!fs_info->uuid_root) {
  1678. btrfs_info(fs_info, "creating UUID tree");
  1679. ret = btrfs_create_uuid_tree(fs_info);
  1680. if (ret) {
  1681. btrfs_warn(fs_info,
  1682. "failed to create the UUID tree %d",
  1683. ret);
  1684. goto restore;
  1685. }
  1686. }
  1687. sb->s_flags &= ~MS_RDONLY;
  1688. set_bit(BTRFS_FS_OPEN, &fs_info->flags);
  1689. }
  1690. out:
  1691. wake_up_process(fs_info->transaction_kthread);
  1692. btrfs_remount_cleanup(fs_info, old_opts);
  1693. return 0;
  1694. restore:
  1695. /* We've hit an error - don't reset MS_RDONLY */
  1696. if (sb->s_flags & MS_RDONLY)
  1697. old_flags |= MS_RDONLY;
  1698. sb->s_flags = old_flags;
  1699. fs_info->mount_opt = old_opts;
  1700. fs_info->compress_type = old_compress_type;
  1701. fs_info->max_inline = old_max_inline;
  1702. mutex_lock(&fs_info->chunk_mutex);
  1703. fs_info->alloc_start = old_alloc_start;
  1704. mutex_unlock(&fs_info->chunk_mutex);
  1705. btrfs_resize_thread_pool(fs_info,
  1706. old_thread_pool_size, fs_info->thread_pool_size);
  1707. fs_info->metadata_ratio = old_metadata_ratio;
  1708. btrfs_remount_cleanup(fs_info, old_opts);
  1709. return ret;
  1710. }
  1711. /* Used to sort the devices by max_avail(descending sort) */
  1712. static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  1713. const void *dev_info2)
  1714. {
  1715. if (((struct btrfs_device_info *)dev_info1)->max_avail >
  1716. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1717. return -1;
  1718. else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  1719. ((struct btrfs_device_info *)dev_info2)->max_avail)
  1720. return 1;
  1721. else
  1722. return 0;
  1723. }
  1724. /*
  1725. * sort the devices by max_avail, in which max free extent size of each device
  1726. * is stored.(Descending Sort)
  1727. */
  1728. static inline void btrfs_descending_sort_devices(
  1729. struct btrfs_device_info *devices,
  1730. size_t nr_devices)
  1731. {
  1732. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1733. btrfs_cmp_device_free_bytes, NULL);
  1734. }
  1735. /*
  1736. * The helper to calc the free space on the devices that can be used to store
  1737. * file data.
  1738. */
  1739. static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
  1740. u64 *free_bytes)
  1741. {
  1742. struct btrfs_root *root = fs_info->tree_root;
  1743. struct btrfs_device_info *devices_info;
  1744. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1745. struct btrfs_device *device;
  1746. u64 skip_space;
  1747. u64 type;
  1748. u64 avail_space;
  1749. u64 used_space;
  1750. u64 min_stripe_size;
  1751. int min_stripes = 1, num_stripes = 1;
  1752. int i = 0, nr_devices;
  1753. int ret;
  1754. /*
  1755. * We aren't under the device list lock, so this is racy-ish, but good
  1756. * enough for our purposes.
  1757. */
  1758. nr_devices = fs_info->fs_devices->open_devices;
  1759. if (!nr_devices) {
  1760. smp_mb();
  1761. nr_devices = fs_info->fs_devices->open_devices;
  1762. ASSERT(nr_devices);
  1763. if (!nr_devices) {
  1764. *free_bytes = 0;
  1765. return 0;
  1766. }
  1767. }
  1768. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1769. GFP_NOFS);
  1770. if (!devices_info)
  1771. return -ENOMEM;
  1772. /* calc min stripe number for data space allocation */
  1773. type = btrfs_get_alloc_profile(root, 1);
  1774. if (type & BTRFS_BLOCK_GROUP_RAID0) {
  1775. min_stripes = 2;
  1776. num_stripes = nr_devices;
  1777. } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
  1778. min_stripes = 2;
  1779. num_stripes = 2;
  1780. } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  1781. min_stripes = 4;
  1782. num_stripes = 4;
  1783. }
  1784. if (type & BTRFS_BLOCK_GROUP_DUP)
  1785. min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  1786. else
  1787. min_stripe_size = BTRFS_STRIPE_LEN;
  1788. if (fs_info->alloc_start)
  1789. mutex_lock(&fs_devices->device_list_mutex);
  1790. rcu_read_lock();
  1791. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  1792. if (!device->in_fs_metadata || !device->bdev ||
  1793. device->is_tgtdev_for_dev_replace)
  1794. continue;
  1795. if (i >= nr_devices)
  1796. break;
  1797. avail_space = device->total_bytes - device->bytes_used;
  1798. /* align with stripe_len */
  1799. avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
  1800. avail_space *= BTRFS_STRIPE_LEN;
  1801. /*
  1802. * In order to avoid overwriting the superblock on the drive,
  1803. * btrfs starts at an offset of at least 1MB when doing chunk
  1804. * allocation.
  1805. */
  1806. skip_space = SZ_1M;
  1807. /* user can set the offset in fs_info->alloc_start. */
  1808. if (fs_info->alloc_start &&
  1809. fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  1810. device->total_bytes) {
  1811. rcu_read_unlock();
  1812. skip_space = max(fs_info->alloc_start, skip_space);
  1813. /*
  1814. * btrfs can not use the free space in
  1815. * [0, skip_space - 1], we must subtract it from the
  1816. * total. In order to implement it, we account the used
  1817. * space in this range first.
  1818. */
  1819. ret = btrfs_account_dev_extents_size(device, 0,
  1820. skip_space - 1,
  1821. &used_space);
  1822. if (ret) {
  1823. kfree(devices_info);
  1824. mutex_unlock(&fs_devices->device_list_mutex);
  1825. return ret;
  1826. }
  1827. rcu_read_lock();
  1828. /* calc the free space in [0, skip_space - 1] */
  1829. skip_space -= used_space;
  1830. }
  1831. /*
  1832. * we can use the free space in [0, skip_space - 1], subtract
  1833. * it from the total.
  1834. */
  1835. if (avail_space && avail_space >= skip_space)
  1836. avail_space -= skip_space;
  1837. else
  1838. avail_space = 0;
  1839. if (avail_space < min_stripe_size)
  1840. continue;
  1841. devices_info[i].dev = device;
  1842. devices_info[i].max_avail = avail_space;
  1843. i++;
  1844. }
  1845. rcu_read_unlock();
  1846. if (fs_info->alloc_start)
  1847. mutex_unlock(&fs_devices->device_list_mutex);
  1848. nr_devices = i;
  1849. btrfs_descending_sort_devices(devices_info, nr_devices);
  1850. i = nr_devices - 1;
  1851. avail_space = 0;
  1852. while (nr_devices >= min_stripes) {
  1853. if (num_stripes > nr_devices)
  1854. num_stripes = nr_devices;
  1855. if (devices_info[i].max_avail >= min_stripe_size) {
  1856. int j;
  1857. u64 alloc_size;
  1858. avail_space += devices_info[i].max_avail * num_stripes;
  1859. alloc_size = devices_info[i].max_avail;
  1860. for (j = i + 1 - num_stripes; j <= i; j++)
  1861. devices_info[j].max_avail -= alloc_size;
  1862. }
  1863. i--;
  1864. nr_devices--;
  1865. }
  1866. kfree(devices_info);
  1867. *free_bytes = avail_space;
  1868. return 0;
  1869. }
  1870. /*
  1871. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1872. *
  1873. * If there's a redundant raid level at DATA block groups, use the respective
  1874. * multiplier to scale the sizes.
  1875. *
  1876. * Unused device space usage is based on simulating the chunk allocator
  1877. * algorithm that respects the device sizes, order of allocations and the
  1878. * 'alloc_start' value, this is a close approximation of the actual use but
  1879. * there are other factors that may change the result (like a new metadata
  1880. * chunk).
  1881. *
  1882. * If metadata is exhausted, f_bavail will be 0.
  1883. */
  1884. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1885. {
  1886. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1887. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1888. struct list_head *head = &fs_info->space_info;
  1889. struct btrfs_space_info *found;
  1890. u64 total_used = 0;
  1891. u64 total_free_data = 0;
  1892. u64 total_free_meta = 0;
  1893. int bits = dentry->d_sb->s_blocksize_bits;
  1894. __be32 *fsid = (__be32 *)fs_info->fsid;
  1895. unsigned factor = 1;
  1896. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1897. int ret;
  1898. u64 thresh = 0;
  1899. int mixed = 0;
  1900. rcu_read_lock();
  1901. list_for_each_entry_rcu(found, head, list) {
  1902. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1903. int i;
  1904. total_free_data += found->disk_total - found->disk_used;
  1905. total_free_data -=
  1906. btrfs_account_ro_block_groups_free_space(found);
  1907. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1908. if (!list_empty(&found->block_groups[i])) {
  1909. switch (i) {
  1910. case BTRFS_RAID_DUP:
  1911. case BTRFS_RAID_RAID1:
  1912. case BTRFS_RAID_RAID10:
  1913. factor = 2;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. /*
  1919. * Metadata in mixed block goup profiles are accounted in data
  1920. */
  1921. if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1922. if (found->flags & BTRFS_BLOCK_GROUP_DATA)
  1923. mixed = 1;
  1924. else
  1925. total_free_meta += found->disk_total -
  1926. found->disk_used;
  1927. }
  1928. total_used += found->disk_used;
  1929. }
  1930. rcu_read_unlock();
  1931. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1932. buf->f_blocks >>= bits;
  1933. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1934. /* Account global block reserve as used, it's in logical size already */
  1935. spin_lock(&block_rsv->lock);
  1936. /* Mixed block groups accounting is not byte-accurate, avoid overflow */
  1937. if (buf->f_bfree >= block_rsv->size >> bits)
  1938. buf->f_bfree -= block_rsv->size >> bits;
  1939. else
  1940. buf->f_bfree = 0;
  1941. spin_unlock(&block_rsv->lock);
  1942. buf->f_bavail = div_u64(total_free_data, factor);
  1943. ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
  1944. if (ret)
  1945. return ret;
  1946. buf->f_bavail += div_u64(total_free_data, factor);
  1947. buf->f_bavail = buf->f_bavail >> bits;
  1948. /*
  1949. * We calculate the remaining metadata space minus global reserve. If
  1950. * this is (supposedly) smaller than zero, there's no space. But this
  1951. * does not hold in practice, the exhausted state happens where's still
  1952. * some positive delta. So we apply some guesswork and compare the
  1953. * delta to a 4M threshold. (Practically observed delta was ~2M.)
  1954. *
  1955. * We probably cannot calculate the exact threshold value because this
  1956. * depends on the internal reservations requested by various
  1957. * operations, so some operations that consume a few metadata will
  1958. * succeed even if the Avail is zero. But this is better than the other
  1959. * way around.
  1960. */
  1961. thresh = 4 * 1024 * 1024;
  1962. if (!mixed && total_free_meta - thresh < block_rsv->size)
  1963. buf->f_bavail = 0;
  1964. buf->f_type = BTRFS_SUPER_MAGIC;
  1965. buf->f_bsize = dentry->d_sb->s_blocksize;
  1966. buf->f_namelen = BTRFS_NAME_LEN;
  1967. /* We treat it as constant endianness (it doesn't matter _which_)
  1968. because we want the fsid to come out the same whether mounted
  1969. on a big-endian or little-endian host */
  1970. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1971. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1972. /* Mask in the root object ID too, to disambiguate subvols */
  1973. buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
  1974. buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
  1975. return 0;
  1976. }
  1977. static void btrfs_kill_super(struct super_block *sb)
  1978. {
  1979. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1980. kill_anon_super(sb);
  1981. free_fs_info(fs_info);
  1982. }
  1983. static struct file_system_type btrfs_fs_type = {
  1984. .owner = THIS_MODULE,
  1985. .name = "btrfs",
  1986. .mount = btrfs_mount,
  1987. .kill_sb = btrfs_kill_super,
  1988. .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
  1989. };
  1990. MODULE_ALIAS_FS("btrfs");
  1991. static int btrfs_control_open(struct inode *inode, struct file *file)
  1992. {
  1993. /*
  1994. * The control file's private_data is used to hold the
  1995. * transaction when it is started and is used to keep
  1996. * track of whether a transaction is already in progress.
  1997. */
  1998. file->private_data = NULL;
  1999. return 0;
  2000. }
  2001. /*
  2002. * used by btrfsctl to scan devices when no FS is mounted
  2003. */
  2004. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  2005. unsigned long arg)
  2006. {
  2007. struct btrfs_ioctl_vol_args *vol;
  2008. struct btrfs_fs_devices *fs_devices;
  2009. int ret = -ENOTTY;
  2010. if (!capable(CAP_SYS_ADMIN))
  2011. return -EPERM;
  2012. vol = memdup_user((void __user *)arg, sizeof(*vol));
  2013. if (IS_ERR(vol))
  2014. return PTR_ERR(vol);
  2015. switch (cmd) {
  2016. case BTRFS_IOC_SCAN_DEV:
  2017. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  2018. &btrfs_fs_type, &fs_devices);
  2019. break;
  2020. case BTRFS_IOC_DEVICES_READY:
  2021. ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  2022. &btrfs_fs_type, &fs_devices);
  2023. if (ret)
  2024. break;
  2025. ret = !(fs_devices->num_devices == fs_devices->total_devices);
  2026. break;
  2027. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  2028. ret = btrfs_ioctl_get_supported_features((void __user*)arg);
  2029. break;
  2030. }
  2031. kfree(vol);
  2032. return ret;
  2033. }
  2034. static int btrfs_freeze(struct super_block *sb)
  2035. {
  2036. struct btrfs_trans_handle *trans;
  2037. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2038. struct btrfs_root *root = fs_info->tree_root;
  2039. fs_info->fs_frozen = 1;
  2040. /*
  2041. * We don't need a barrier here, we'll wait for any transaction that
  2042. * could be in progress on other threads (and do delayed iputs that
  2043. * we want to avoid on a frozen filesystem), or do the commit
  2044. * ourselves.
  2045. */
  2046. trans = btrfs_attach_transaction_barrier(root);
  2047. if (IS_ERR(trans)) {
  2048. /* no transaction, don't bother */
  2049. if (PTR_ERR(trans) == -ENOENT)
  2050. return 0;
  2051. return PTR_ERR(trans);
  2052. }
  2053. return btrfs_commit_transaction(trans);
  2054. }
  2055. static int btrfs_unfreeze(struct super_block *sb)
  2056. {
  2057. btrfs_sb(sb)->fs_frozen = 0;
  2058. return 0;
  2059. }
  2060. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  2061. {
  2062. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  2063. struct btrfs_fs_devices *cur_devices;
  2064. struct btrfs_device *dev, *first_dev = NULL;
  2065. struct list_head *head;
  2066. struct rcu_string *name;
  2067. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2068. cur_devices = fs_info->fs_devices;
  2069. while (cur_devices) {
  2070. head = &cur_devices->devices;
  2071. list_for_each_entry(dev, head, dev_list) {
  2072. if (dev->missing)
  2073. continue;
  2074. if (!dev->name)
  2075. continue;
  2076. if (!first_dev || dev->devid < first_dev->devid)
  2077. first_dev = dev;
  2078. }
  2079. cur_devices = cur_devices->seed;
  2080. }
  2081. if (first_dev) {
  2082. rcu_read_lock();
  2083. name = rcu_dereference(first_dev->name);
  2084. seq_escape(m, name->str, " \t\n\\");
  2085. rcu_read_unlock();
  2086. } else {
  2087. WARN_ON(1);
  2088. }
  2089. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2090. return 0;
  2091. }
  2092. static const struct super_operations btrfs_super_ops = {
  2093. .drop_inode = btrfs_drop_inode,
  2094. .evict_inode = btrfs_evict_inode,
  2095. .put_super = btrfs_put_super,
  2096. .sync_fs = btrfs_sync_fs,
  2097. .show_options = btrfs_show_options,
  2098. .show_devname = btrfs_show_devname,
  2099. .write_inode = btrfs_write_inode,
  2100. .alloc_inode = btrfs_alloc_inode,
  2101. .destroy_inode = btrfs_destroy_inode,
  2102. .statfs = btrfs_statfs,
  2103. .remount_fs = btrfs_remount,
  2104. .freeze_fs = btrfs_freeze,
  2105. .unfreeze_fs = btrfs_unfreeze,
  2106. };
  2107. static const struct file_operations btrfs_ctl_fops = {
  2108. .open = btrfs_control_open,
  2109. .unlocked_ioctl = btrfs_control_ioctl,
  2110. .compat_ioctl = btrfs_control_ioctl,
  2111. .owner = THIS_MODULE,
  2112. .llseek = noop_llseek,
  2113. };
  2114. static struct miscdevice btrfs_misc = {
  2115. .minor = BTRFS_MINOR,
  2116. .name = "btrfs-control",
  2117. .fops = &btrfs_ctl_fops
  2118. };
  2119. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  2120. MODULE_ALIAS("devname:btrfs-control");
  2121. static int btrfs_interface_init(void)
  2122. {
  2123. return misc_register(&btrfs_misc);
  2124. }
  2125. static void btrfs_interface_exit(void)
  2126. {
  2127. misc_deregister(&btrfs_misc);
  2128. }
  2129. static void btrfs_print_mod_info(void)
  2130. {
  2131. pr_info("Btrfs loaded, crc32c=%s"
  2132. #ifdef CONFIG_BTRFS_DEBUG
  2133. ", debug=on"
  2134. #endif
  2135. #ifdef CONFIG_BTRFS_ASSERT
  2136. ", assert=on"
  2137. #endif
  2138. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2139. ", integrity-checker=on"
  2140. #endif
  2141. "\n",
  2142. btrfs_crc32c_impl());
  2143. }
  2144. static int __init init_btrfs_fs(void)
  2145. {
  2146. int err;
  2147. err = btrfs_hash_init();
  2148. if (err)
  2149. return err;
  2150. btrfs_props_init();
  2151. err = btrfs_init_sysfs();
  2152. if (err)
  2153. goto free_hash;
  2154. btrfs_init_compress();
  2155. err = btrfs_init_cachep();
  2156. if (err)
  2157. goto free_compress;
  2158. err = extent_io_init();
  2159. if (err)
  2160. goto free_cachep;
  2161. err = extent_map_init();
  2162. if (err)
  2163. goto free_extent_io;
  2164. err = ordered_data_init();
  2165. if (err)
  2166. goto free_extent_map;
  2167. err = btrfs_delayed_inode_init();
  2168. if (err)
  2169. goto free_ordered_data;
  2170. err = btrfs_auto_defrag_init();
  2171. if (err)
  2172. goto free_delayed_inode;
  2173. err = btrfs_delayed_ref_init();
  2174. if (err)
  2175. goto free_auto_defrag;
  2176. err = btrfs_prelim_ref_init();
  2177. if (err)
  2178. goto free_delayed_ref;
  2179. err = btrfs_end_io_wq_init();
  2180. if (err)
  2181. goto free_prelim_ref;
  2182. err = btrfs_interface_init();
  2183. if (err)
  2184. goto free_end_io_wq;
  2185. btrfs_init_lockdep();
  2186. btrfs_print_mod_info();
  2187. err = btrfs_run_sanity_tests();
  2188. if (err)
  2189. goto unregister_ioctl;
  2190. err = register_filesystem(&btrfs_fs_type);
  2191. if (err)
  2192. goto unregister_ioctl;
  2193. return 0;
  2194. unregister_ioctl:
  2195. btrfs_interface_exit();
  2196. free_end_io_wq:
  2197. btrfs_end_io_wq_exit();
  2198. free_prelim_ref:
  2199. btrfs_prelim_ref_exit();
  2200. free_delayed_ref:
  2201. btrfs_delayed_ref_exit();
  2202. free_auto_defrag:
  2203. btrfs_auto_defrag_exit();
  2204. free_delayed_inode:
  2205. btrfs_delayed_inode_exit();
  2206. free_ordered_data:
  2207. ordered_data_exit();
  2208. free_extent_map:
  2209. extent_map_exit();
  2210. free_extent_io:
  2211. extent_io_exit();
  2212. free_cachep:
  2213. btrfs_destroy_cachep();
  2214. free_compress:
  2215. btrfs_exit_compress();
  2216. btrfs_exit_sysfs();
  2217. free_hash:
  2218. btrfs_hash_exit();
  2219. return err;
  2220. }
  2221. static void __exit exit_btrfs_fs(void)
  2222. {
  2223. btrfs_destroy_cachep();
  2224. btrfs_delayed_ref_exit();
  2225. btrfs_auto_defrag_exit();
  2226. btrfs_delayed_inode_exit();
  2227. btrfs_prelim_ref_exit();
  2228. ordered_data_exit();
  2229. extent_map_exit();
  2230. extent_io_exit();
  2231. btrfs_interface_exit();
  2232. btrfs_end_io_wq_exit();
  2233. unregister_filesystem(&btrfs_fs_type);
  2234. btrfs_exit_sysfs();
  2235. btrfs_cleanup_fs_uuids();
  2236. btrfs_exit_compress();
  2237. btrfs_hash_exit();
  2238. }
  2239. late_initcall(init_btrfs_fs);
  2240. module_exit(exit_btrfs_fs)
  2241. MODULE_LICENSE("GPL");