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