super.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/module.h>
  7. #include <linux/fs.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/highmem.h>
  10. #include <linux/time.h>
  11. #include <linux/init.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/string.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/mount.h>
  16. #include <linux/writeback.h>
  17. #include <linux/statfs.h>
  18. #include <linux/compat.h>
  19. #include <linux/parser.h>
  20. #include <linux/ctype.h>
  21. #include <linux/namei.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/magic.h>
  24. #include <linux/slab.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/crc32c.h>
  27. #include <linux/btrfs.h>
  28. #include <linux/security.h>
  29. #include <linux/fs_parser.h>
  30. #include "messages.h"
  31. #include "delayed-inode.h"
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "btrfs_inode.h"
  36. #include "direct-io.h"
  37. #include "props.h"
  38. #include "xattr.h"
  39. #include "bio.h"
  40. #include "export.h"
  41. #include "compression.h"
  42. #include "dev-replace.h"
  43. #include "free-space-cache.h"
  44. #include "backref.h"
  45. #include "space-info.h"
  46. #include "sysfs.h"
  47. #include "zoned.h"
  48. #include "tests/btrfs-tests.h"
  49. #include "block-group.h"
  50. #include "discard.h"
  51. #include "qgroup.h"
  52. #include "raid56.h"
  53. #include "fs.h"
  54. #include "accessors.h"
  55. #include "defrag.h"
  56. #include "dir-item.h"
  57. #include "ioctl.h"
  58. #include "scrub.h"
  59. #include "verity.h"
  60. #include "super.h"
  61. #include "extent-tree.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 void btrfs_put_super(struct super_block *sb)
  67. {
  68. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  69. btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
  70. close_ctree(fs_info);
  71. }
  72. /* Store the mount options related information. */
  73. struct btrfs_fs_context {
  74. char *subvol_name;
  75. u64 subvol_objectid;
  76. u64 max_inline;
  77. u32 commit_interval;
  78. u32 metadata_ratio;
  79. u32 thread_pool_size;
  80. unsigned long long mount_opt;
  81. unsigned long compress_type:4;
  82. int compress_level;
  83. refcount_t refs;
  84. };
  85. static void btrfs_emit_options(struct btrfs_fs_info *info,
  86. struct btrfs_fs_context *old);
  87. enum {
  88. Opt_acl,
  89. Opt_clear_cache,
  90. Opt_commit_interval,
  91. Opt_compress,
  92. Opt_compress_force,
  93. Opt_compress_force_type,
  94. Opt_compress_type,
  95. Opt_degraded,
  96. Opt_device,
  97. Opt_fatal_errors,
  98. Opt_flushoncommit,
  99. Opt_max_inline,
  100. Opt_barrier,
  101. Opt_datacow,
  102. Opt_datasum,
  103. Opt_defrag,
  104. Opt_discard,
  105. Opt_discard_mode,
  106. Opt_ratio,
  107. Opt_rescan_uuid_tree,
  108. Opt_skip_balance,
  109. Opt_space_cache,
  110. Opt_space_cache_version,
  111. Opt_ssd,
  112. Opt_ssd_spread,
  113. Opt_subvol,
  114. Opt_subvol_empty,
  115. Opt_subvolid,
  116. Opt_thread_pool,
  117. Opt_treelog,
  118. Opt_user_subvol_rm_allowed,
  119. Opt_norecovery,
  120. /* Rescue options */
  121. Opt_rescue,
  122. Opt_usebackuproot,
  123. /* Debugging options */
  124. Opt_enospc_debug,
  125. #ifdef CONFIG_BTRFS_DEBUG
  126. Opt_fragment, Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
  127. Opt_ref_verify,
  128. Opt_ref_tracker,
  129. #endif
  130. Opt_err,
  131. };
  132. enum {
  133. Opt_fatal_errors_panic,
  134. Opt_fatal_errors_bug,
  135. };
  136. static const struct constant_table btrfs_parameter_fatal_errors[] = {
  137. { "panic", Opt_fatal_errors_panic },
  138. { "bug", Opt_fatal_errors_bug },
  139. {}
  140. };
  141. enum {
  142. Opt_discard_sync,
  143. Opt_discard_async,
  144. };
  145. static const struct constant_table btrfs_parameter_discard[] = {
  146. { "sync", Opt_discard_sync },
  147. { "async", Opt_discard_async },
  148. {}
  149. };
  150. enum {
  151. Opt_space_cache_v1,
  152. Opt_space_cache_v2,
  153. };
  154. static const struct constant_table btrfs_parameter_space_cache[] = {
  155. { "v1", Opt_space_cache_v1 },
  156. { "v2", Opt_space_cache_v2 },
  157. {}
  158. };
  159. enum {
  160. Opt_rescue_usebackuproot,
  161. Opt_rescue_nologreplay,
  162. Opt_rescue_ignorebadroots,
  163. Opt_rescue_ignoredatacsums,
  164. Opt_rescue_ignoremetacsums,
  165. Opt_rescue_ignoresuperflags,
  166. Opt_rescue_parameter_all,
  167. };
  168. static const struct constant_table btrfs_parameter_rescue[] = {
  169. { "usebackuproot", Opt_rescue_usebackuproot },
  170. { "nologreplay", Opt_rescue_nologreplay },
  171. { "ignorebadroots", Opt_rescue_ignorebadroots },
  172. { "ibadroots", Opt_rescue_ignorebadroots },
  173. { "ignoredatacsums", Opt_rescue_ignoredatacsums },
  174. { "ignoremetacsums", Opt_rescue_ignoremetacsums},
  175. { "ignoresuperflags", Opt_rescue_ignoresuperflags},
  176. { "idatacsums", Opt_rescue_ignoredatacsums },
  177. { "imetacsums", Opt_rescue_ignoremetacsums},
  178. { "isuperflags", Opt_rescue_ignoresuperflags},
  179. { "all", Opt_rescue_parameter_all },
  180. {}
  181. };
  182. #ifdef CONFIG_BTRFS_DEBUG
  183. enum {
  184. Opt_fragment_parameter_data,
  185. Opt_fragment_parameter_metadata,
  186. Opt_fragment_parameter_all,
  187. };
  188. static const struct constant_table btrfs_parameter_fragment[] = {
  189. { "data", Opt_fragment_parameter_data },
  190. { "metadata", Opt_fragment_parameter_metadata },
  191. { "all", Opt_fragment_parameter_all },
  192. {}
  193. };
  194. #endif
  195. static const struct fs_parameter_spec btrfs_fs_parameters[] = {
  196. fsparam_flag_no("acl", Opt_acl),
  197. fsparam_flag_no("autodefrag", Opt_defrag),
  198. fsparam_flag_no("barrier", Opt_barrier),
  199. fsparam_flag("clear_cache", Opt_clear_cache),
  200. fsparam_u32("commit", Opt_commit_interval),
  201. fsparam_flag("compress", Opt_compress),
  202. fsparam_string("compress", Opt_compress_type),
  203. fsparam_flag("compress-force", Opt_compress_force),
  204. fsparam_string("compress-force", Opt_compress_force_type),
  205. fsparam_flag_no("datacow", Opt_datacow),
  206. fsparam_flag_no("datasum", Opt_datasum),
  207. fsparam_flag("degraded", Opt_degraded),
  208. fsparam_string("device", Opt_device),
  209. fsparam_flag_no("discard", Opt_discard),
  210. fsparam_enum("discard", Opt_discard_mode, btrfs_parameter_discard),
  211. fsparam_enum("fatal_errors", Opt_fatal_errors, btrfs_parameter_fatal_errors),
  212. fsparam_flag_no("flushoncommit", Opt_flushoncommit),
  213. fsparam_string("max_inline", Opt_max_inline),
  214. fsparam_u32("metadata_ratio", Opt_ratio),
  215. fsparam_flag("rescan_uuid_tree", Opt_rescan_uuid_tree),
  216. fsparam_flag("skip_balance", Opt_skip_balance),
  217. fsparam_flag_no("space_cache", Opt_space_cache),
  218. fsparam_enum("space_cache", Opt_space_cache_version, btrfs_parameter_space_cache),
  219. fsparam_flag_no("ssd", Opt_ssd),
  220. fsparam_flag_no("ssd_spread", Opt_ssd_spread),
  221. fsparam_string("subvol", Opt_subvol),
  222. fsparam_flag("subvol=", Opt_subvol_empty),
  223. fsparam_u64("subvolid", Opt_subvolid),
  224. fsparam_u32("thread_pool", Opt_thread_pool),
  225. fsparam_flag_no("treelog", Opt_treelog),
  226. fsparam_flag("user_subvol_rm_allowed", Opt_user_subvol_rm_allowed),
  227. /* Rescue options. */
  228. fsparam_enum("rescue", Opt_rescue, btrfs_parameter_rescue),
  229. /* Deprecated, with alias rescue=usebackuproot */
  230. __fsparam(NULL, "usebackuproot", Opt_usebackuproot, fs_param_deprecated, NULL),
  231. /* For compatibility only, alias for "rescue=nologreplay". */
  232. fsparam_flag("norecovery", Opt_norecovery),
  233. /* Debugging options. */
  234. fsparam_flag_no("enospc_debug", Opt_enospc_debug),
  235. #ifdef CONFIG_BTRFS_DEBUG
  236. fsparam_enum("fragment", Opt_fragment, btrfs_parameter_fragment),
  237. fsparam_flag("ref_tracker", Opt_ref_tracker),
  238. fsparam_flag("ref_verify", Opt_ref_verify),
  239. #endif
  240. {}
  241. };
  242. static bool btrfs_match_compress_type(const char *string, const char *type, bool may_have_level)
  243. {
  244. const int len = strlen(type);
  245. return (strncmp(string, type, len) == 0) &&
  246. ((may_have_level && string[len] == ':') || string[len] == '\0');
  247. }
  248. static int btrfs_parse_compress(struct btrfs_fs_context *ctx,
  249. const struct fs_parameter *param, int opt)
  250. {
  251. const char *string = param->string;
  252. int ret;
  253. /*
  254. * Provide the same semantics as older kernels that don't use fs
  255. * context, specifying the "compress" option clears "force-compress"
  256. * without the need to pass "compress-force=[no|none]" before
  257. * specifying "compress".
  258. */
  259. if (opt != Opt_compress_force && opt != Opt_compress_force_type)
  260. btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
  261. if (opt == Opt_compress || opt == Opt_compress_force) {
  262. ctx->compress_type = BTRFS_COMPRESS_ZLIB;
  263. ctx->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
  264. btrfs_set_opt(ctx->mount_opt, COMPRESS);
  265. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  266. btrfs_clear_opt(ctx->mount_opt, NODATASUM);
  267. } else if (btrfs_match_compress_type(string, "zlib", true)) {
  268. ctx->compress_type = BTRFS_COMPRESS_ZLIB;
  269. ret = btrfs_compress_str2level(BTRFS_COMPRESS_ZLIB, string + 4,
  270. &ctx->compress_level);
  271. if (ret < 0)
  272. goto error;
  273. btrfs_set_opt(ctx->mount_opt, COMPRESS);
  274. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  275. btrfs_clear_opt(ctx->mount_opt, NODATASUM);
  276. } else if (btrfs_match_compress_type(string, "lzo", true)) {
  277. ctx->compress_type = BTRFS_COMPRESS_LZO;
  278. ret = btrfs_compress_str2level(BTRFS_COMPRESS_LZO, string + 3,
  279. &ctx->compress_level);
  280. if (ret < 0)
  281. goto error;
  282. if (string[3] == ':' && string[4])
  283. btrfs_warn(NULL, "Compression level ignored for LZO");
  284. btrfs_set_opt(ctx->mount_opt, COMPRESS);
  285. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  286. btrfs_clear_opt(ctx->mount_opt, NODATASUM);
  287. } else if (btrfs_match_compress_type(string, "zstd", true)) {
  288. ctx->compress_type = BTRFS_COMPRESS_ZSTD;
  289. ret = btrfs_compress_str2level(BTRFS_COMPRESS_ZSTD, string + 4,
  290. &ctx->compress_level);
  291. if (ret < 0)
  292. goto error;
  293. btrfs_set_opt(ctx->mount_opt, COMPRESS);
  294. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  295. btrfs_clear_opt(ctx->mount_opt, NODATASUM);
  296. } else if (btrfs_match_compress_type(string, "no", false) ||
  297. btrfs_match_compress_type(string, "none", false)) {
  298. ctx->compress_level = 0;
  299. ctx->compress_type = 0;
  300. btrfs_clear_opt(ctx->mount_opt, COMPRESS);
  301. btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
  302. } else {
  303. ret = -EINVAL;
  304. goto error;
  305. }
  306. return 0;
  307. error:
  308. btrfs_err(NULL, "failed to parse compression option '%s'", string);
  309. return ret;
  310. }
  311. static int btrfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  312. {
  313. struct btrfs_fs_context *ctx = fc->fs_private;
  314. struct fs_parse_result result;
  315. int opt;
  316. opt = fs_parse(fc, btrfs_fs_parameters, param, &result);
  317. if (opt < 0)
  318. return opt;
  319. switch (opt) {
  320. case Opt_degraded:
  321. btrfs_set_opt(ctx->mount_opt, DEGRADED);
  322. break;
  323. case Opt_subvol_empty:
  324. /*
  325. * This exists because we used to allow it on accident, so we're
  326. * keeping it to maintain ABI. See 37becec95ac3 ("Btrfs: allow
  327. * empty subvol= again").
  328. */
  329. break;
  330. case Opt_subvol:
  331. kfree(ctx->subvol_name);
  332. ctx->subvol_name = kstrdup(param->string, GFP_KERNEL);
  333. if (!ctx->subvol_name)
  334. return -ENOMEM;
  335. break;
  336. case Opt_subvolid:
  337. ctx->subvol_objectid = result.uint_64;
  338. /* subvolid=0 means give me the original fs_tree. */
  339. if (!ctx->subvol_objectid)
  340. ctx->subvol_objectid = BTRFS_FS_TREE_OBJECTID;
  341. break;
  342. case Opt_device: {
  343. struct btrfs_device *device;
  344. mutex_lock(&uuid_mutex);
  345. device = btrfs_scan_one_device(param->string, false);
  346. mutex_unlock(&uuid_mutex);
  347. if (IS_ERR(device))
  348. return PTR_ERR(device);
  349. break;
  350. }
  351. case Opt_datasum:
  352. if (result.negated) {
  353. btrfs_set_opt(ctx->mount_opt, NODATASUM);
  354. } else {
  355. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  356. btrfs_clear_opt(ctx->mount_opt, NODATASUM);
  357. }
  358. break;
  359. case Opt_datacow:
  360. if (result.negated) {
  361. btrfs_clear_opt(ctx->mount_opt, COMPRESS);
  362. btrfs_clear_opt(ctx->mount_opt, FORCE_COMPRESS);
  363. btrfs_set_opt(ctx->mount_opt, NODATACOW);
  364. btrfs_set_opt(ctx->mount_opt, NODATASUM);
  365. } else {
  366. btrfs_clear_opt(ctx->mount_opt, NODATACOW);
  367. }
  368. break;
  369. case Opt_compress_force:
  370. case Opt_compress_force_type:
  371. btrfs_set_opt(ctx->mount_opt, FORCE_COMPRESS);
  372. fallthrough;
  373. case Opt_compress:
  374. case Opt_compress_type:
  375. if (btrfs_parse_compress(ctx, param, opt))
  376. return -EINVAL;
  377. break;
  378. case Opt_ssd:
  379. if (result.negated) {
  380. btrfs_set_opt(ctx->mount_opt, NOSSD);
  381. btrfs_clear_opt(ctx->mount_opt, SSD);
  382. btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
  383. } else {
  384. btrfs_set_opt(ctx->mount_opt, SSD);
  385. btrfs_clear_opt(ctx->mount_opt, NOSSD);
  386. }
  387. break;
  388. case Opt_ssd_spread:
  389. if (result.negated) {
  390. btrfs_clear_opt(ctx->mount_opt, SSD_SPREAD);
  391. } else {
  392. btrfs_set_opt(ctx->mount_opt, SSD);
  393. btrfs_set_opt(ctx->mount_opt, SSD_SPREAD);
  394. btrfs_clear_opt(ctx->mount_opt, NOSSD);
  395. }
  396. break;
  397. case Opt_barrier:
  398. if (result.negated)
  399. btrfs_set_opt(ctx->mount_opt, NOBARRIER);
  400. else
  401. btrfs_clear_opt(ctx->mount_opt, NOBARRIER);
  402. break;
  403. case Opt_thread_pool:
  404. if (result.uint_32 == 0) {
  405. btrfs_err(NULL, "invalid value 0 for thread_pool");
  406. return -EINVAL;
  407. }
  408. ctx->thread_pool_size = result.uint_32;
  409. break;
  410. case Opt_max_inline:
  411. ctx->max_inline = memparse(param->string, NULL);
  412. break;
  413. case Opt_acl:
  414. if (result.negated) {
  415. fc->sb_flags &= ~SB_POSIXACL;
  416. } else {
  417. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  418. fc->sb_flags |= SB_POSIXACL;
  419. #else
  420. btrfs_err(NULL, "support for ACL not compiled in");
  421. return -EINVAL;
  422. #endif
  423. }
  424. /*
  425. * VFS limits the ability to toggle ACL on and off via remount,
  426. * despite every file system allowing this. This seems to be
  427. * an oversight since we all do, but it'll fail if we're
  428. * remounting. So don't set the mask here, we'll check it in
  429. * btrfs_reconfigure and do the toggling ourselves.
  430. */
  431. if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
  432. fc->sb_flags_mask |= SB_POSIXACL;
  433. break;
  434. case Opt_treelog:
  435. if (result.negated)
  436. btrfs_set_opt(ctx->mount_opt, NOTREELOG);
  437. else
  438. btrfs_clear_opt(ctx->mount_opt, NOTREELOG);
  439. break;
  440. case Opt_norecovery:
  441. btrfs_info(NULL,
  442. "'norecovery' is for compatibility only, recommended to use 'rescue=nologreplay'");
  443. btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
  444. break;
  445. case Opt_flushoncommit:
  446. if (result.negated)
  447. btrfs_clear_opt(ctx->mount_opt, FLUSHONCOMMIT);
  448. else
  449. btrfs_set_opt(ctx->mount_opt, FLUSHONCOMMIT);
  450. break;
  451. case Opt_ratio:
  452. ctx->metadata_ratio = result.uint_32;
  453. break;
  454. case Opt_discard:
  455. if (result.negated) {
  456. btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
  457. btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
  458. btrfs_set_opt(ctx->mount_opt, NODISCARD);
  459. } else {
  460. btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
  461. btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
  462. }
  463. break;
  464. case Opt_discard_mode:
  465. switch (result.uint_32) {
  466. case Opt_discard_sync:
  467. btrfs_clear_opt(ctx->mount_opt, DISCARD_ASYNC);
  468. btrfs_set_opt(ctx->mount_opt, DISCARD_SYNC);
  469. break;
  470. case Opt_discard_async:
  471. btrfs_clear_opt(ctx->mount_opt, DISCARD_SYNC);
  472. btrfs_set_opt(ctx->mount_opt, DISCARD_ASYNC);
  473. break;
  474. default:
  475. btrfs_err(NULL, "unrecognized discard mode value %s",
  476. param->key);
  477. return -EINVAL;
  478. }
  479. btrfs_clear_opt(ctx->mount_opt, NODISCARD);
  480. break;
  481. case Opt_space_cache:
  482. if (result.negated) {
  483. btrfs_set_opt(ctx->mount_opt, NOSPACECACHE);
  484. btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
  485. btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
  486. } else {
  487. btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
  488. btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
  489. }
  490. break;
  491. case Opt_space_cache_version:
  492. switch (result.uint_32) {
  493. case Opt_space_cache_v1:
  494. btrfs_set_opt(ctx->mount_opt, SPACE_CACHE);
  495. btrfs_clear_opt(ctx->mount_opt, FREE_SPACE_TREE);
  496. break;
  497. case Opt_space_cache_v2:
  498. btrfs_clear_opt(ctx->mount_opt, SPACE_CACHE);
  499. btrfs_set_opt(ctx->mount_opt, FREE_SPACE_TREE);
  500. break;
  501. default:
  502. btrfs_err(NULL, "unrecognized space_cache value %s",
  503. param->key);
  504. return -EINVAL;
  505. }
  506. break;
  507. case Opt_rescan_uuid_tree:
  508. btrfs_set_opt(ctx->mount_opt, RESCAN_UUID_TREE);
  509. break;
  510. case Opt_clear_cache:
  511. btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
  512. break;
  513. case Opt_user_subvol_rm_allowed:
  514. btrfs_set_opt(ctx->mount_opt, USER_SUBVOL_RM_ALLOWED);
  515. break;
  516. case Opt_enospc_debug:
  517. if (result.negated)
  518. btrfs_clear_opt(ctx->mount_opt, ENOSPC_DEBUG);
  519. else
  520. btrfs_set_opt(ctx->mount_opt, ENOSPC_DEBUG);
  521. break;
  522. case Opt_defrag:
  523. if (result.negated)
  524. btrfs_clear_opt(ctx->mount_opt, AUTO_DEFRAG);
  525. else
  526. btrfs_set_opt(ctx->mount_opt, AUTO_DEFRAG);
  527. break;
  528. case Opt_usebackuproot:
  529. btrfs_warn(NULL,
  530. "'usebackuproot' is deprecated, use 'rescue=usebackuproot' instead");
  531. btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
  532. /* If we're loading the backup roots we can't trust the space cache. */
  533. btrfs_set_opt(ctx->mount_opt, CLEAR_CACHE);
  534. break;
  535. case Opt_skip_balance:
  536. btrfs_set_opt(ctx->mount_opt, SKIP_BALANCE);
  537. break;
  538. case Opt_fatal_errors:
  539. switch (result.uint_32) {
  540. case Opt_fatal_errors_panic:
  541. btrfs_set_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
  542. break;
  543. case Opt_fatal_errors_bug:
  544. btrfs_clear_opt(ctx->mount_opt, PANIC_ON_FATAL_ERROR);
  545. break;
  546. default:
  547. btrfs_err(NULL, "unrecognized fatal_errors value %s",
  548. param->key);
  549. return -EINVAL;
  550. }
  551. break;
  552. case Opt_commit_interval:
  553. ctx->commit_interval = result.uint_32;
  554. if (ctx->commit_interval > BTRFS_WARNING_COMMIT_INTERVAL) {
  555. btrfs_warn(NULL, "excessive commit interval %u, use with care",
  556. ctx->commit_interval);
  557. }
  558. if (ctx->commit_interval == 0)
  559. ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  560. break;
  561. case Opt_rescue:
  562. switch (result.uint_32) {
  563. case Opt_rescue_usebackuproot:
  564. btrfs_set_opt(ctx->mount_opt, USEBACKUPROOT);
  565. break;
  566. case Opt_rescue_nologreplay:
  567. btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
  568. break;
  569. case Opt_rescue_ignorebadroots:
  570. btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
  571. break;
  572. case Opt_rescue_ignoredatacsums:
  573. btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
  574. break;
  575. case Opt_rescue_ignoremetacsums:
  576. btrfs_set_opt(ctx->mount_opt, IGNOREMETACSUMS);
  577. break;
  578. case Opt_rescue_ignoresuperflags:
  579. btrfs_set_opt(ctx->mount_opt, IGNORESUPERFLAGS);
  580. break;
  581. case Opt_rescue_parameter_all:
  582. btrfs_set_opt(ctx->mount_opt, IGNOREDATACSUMS);
  583. btrfs_set_opt(ctx->mount_opt, IGNOREMETACSUMS);
  584. btrfs_set_opt(ctx->mount_opt, IGNORESUPERFLAGS);
  585. btrfs_set_opt(ctx->mount_opt, IGNOREBADROOTS);
  586. btrfs_set_opt(ctx->mount_opt, NOLOGREPLAY);
  587. break;
  588. default:
  589. btrfs_info(NULL, "unrecognized rescue option '%s'",
  590. param->key);
  591. return -EINVAL;
  592. }
  593. break;
  594. #ifdef CONFIG_BTRFS_DEBUG
  595. case Opt_fragment:
  596. switch (result.uint_32) {
  597. case Opt_fragment_parameter_all:
  598. btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
  599. btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
  600. break;
  601. case Opt_fragment_parameter_metadata:
  602. btrfs_set_opt(ctx->mount_opt, FRAGMENT_METADATA);
  603. break;
  604. case Opt_fragment_parameter_data:
  605. btrfs_set_opt(ctx->mount_opt, FRAGMENT_DATA);
  606. break;
  607. default:
  608. btrfs_info(NULL, "unrecognized fragment option '%s'",
  609. param->key);
  610. return -EINVAL;
  611. }
  612. break;
  613. case Opt_ref_verify:
  614. btrfs_set_opt(ctx->mount_opt, REF_VERIFY);
  615. break;
  616. case Opt_ref_tracker:
  617. btrfs_set_opt(ctx->mount_opt, REF_TRACKER);
  618. break;
  619. #endif
  620. default:
  621. btrfs_err(NULL, "unrecognized mount option '%s'", param->key);
  622. return -EINVAL;
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Some options only have meaning at mount time and shouldn't persist across
  628. * remounts, or be displayed. Clear these at the end of mount and remount code
  629. * paths.
  630. */
  631. static void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info)
  632. {
  633. btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
  634. btrfs_clear_opt(fs_info->mount_opt, CLEAR_CACHE);
  635. btrfs_clear_opt(fs_info->mount_opt, NOSPACECACHE);
  636. }
  637. static bool check_ro_option(const struct btrfs_fs_info *fs_info,
  638. unsigned long long mount_opt, unsigned long long opt,
  639. const char *opt_name)
  640. {
  641. if (mount_opt & opt) {
  642. btrfs_err(fs_info, "%s must be used with ro mount option",
  643. opt_name);
  644. return true;
  645. }
  646. return false;
  647. }
  648. bool btrfs_check_options(const struct btrfs_fs_info *info,
  649. unsigned long long *mount_opt,
  650. unsigned long flags)
  651. {
  652. bool ret = true;
  653. if (!(flags & SB_RDONLY) &&
  654. (check_ro_option(info, *mount_opt, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
  655. check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
  656. check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums") ||
  657. check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNOREMETACSUMS, "ignoremetacsums") ||
  658. check_ro_option(info, *mount_opt, BTRFS_MOUNT_IGNORESUPERFLAGS, "ignoresuperflags")))
  659. ret = false;
  660. if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
  661. !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE) &&
  662. !btrfs_raw_test_opt(*mount_opt, CLEAR_CACHE)) {
  663. btrfs_err(info, "cannot disable free-space-tree");
  664. ret = false;
  665. }
  666. if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
  667. !btrfs_raw_test_opt(*mount_opt, FREE_SPACE_TREE)) {
  668. btrfs_err(info, "cannot disable free-space-tree with block-group-tree feature");
  669. ret = false;
  670. }
  671. if (btrfs_check_mountopts_zoned(info, mount_opt))
  672. ret = false;
  673. if (!test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state)) {
  674. if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) {
  675. btrfs_warn(info,
  676. "space cache v1 is being deprecated and will be removed in a future release, please use -o space_cache=v2");
  677. }
  678. }
  679. return ret;
  680. }
  681. /*
  682. * This is subtle, we only call this during open_ctree(). We need to pre-load
  683. * the mount options with the on-disk settings. Before the new mount API took
  684. * effect we would do this on mount and remount. With the new mount API we'll
  685. * only do this on the initial mount.
  686. *
  687. * This isn't a change in behavior, because we're using the current state of the
  688. * file system to set the current mount options. If you mounted with special
  689. * options to disable these features and then remounted we wouldn't revert the
  690. * settings, because mounting without these features cleared the on-disk
  691. * settings, so this being called on re-mount is not needed.
  692. */
  693. void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
  694. {
  695. if (fs_info->sectorsize != PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
  696. btrfs_info(fs_info,
  697. "forcing free space tree for sector size %u with page size %lu",
  698. fs_info->sectorsize, PAGE_SIZE);
  699. btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
  700. btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
  701. }
  702. /*
  703. * At this point our mount options are populated, so we only mess with
  704. * these settings if we don't have any settings already.
  705. */
  706. if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
  707. return;
  708. if (btrfs_is_zoned(fs_info) &&
  709. btrfs_free_space_cache_v1_active(fs_info)) {
  710. btrfs_info(fs_info, "zoned: clearing existing space cache");
  711. btrfs_set_super_cache_generation(fs_info->super_copy, 0);
  712. return;
  713. }
  714. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  715. return;
  716. if (btrfs_test_opt(fs_info, NOSPACECACHE))
  717. return;
  718. /*
  719. * At this point we don't have explicit options set by the user, set
  720. * them ourselves based on the state of the file system.
  721. */
  722. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
  723. btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
  724. else if (btrfs_free_space_cache_v1_active(fs_info))
  725. btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
  726. }
  727. static void set_device_specific_options(struct btrfs_fs_info *fs_info)
  728. {
  729. if (!btrfs_test_opt(fs_info, NOSSD) &&
  730. !fs_info->fs_devices->rotating)
  731. btrfs_set_opt(fs_info->mount_opt, SSD);
  732. /*
  733. * For devices supporting discard turn on discard=async automatically,
  734. * unless it's already set or disabled. This could be turned off by
  735. * nodiscard for the same mount.
  736. *
  737. * The zoned mode piggy backs on the discard functionality for
  738. * resetting a zone. There is no reason to delay the zone reset as it is
  739. * fast enough. So, do not enable async discard for zoned mode.
  740. */
  741. if (!(btrfs_test_opt(fs_info, DISCARD_SYNC) ||
  742. btrfs_test_opt(fs_info, DISCARD_ASYNC) ||
  743. btrfs_test_opt(fs_info, NODISCARD)) &&
  744. fs_info->fs_devices->discardable &&
  745. !btrfs_is_zoned(fs_info))
  746. btrfs_set_opt(fs_info->mount_opt, DISCARD_ASYNC);
  747. }
  748. char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
  749. u64 subvol_objectid)
  750. {
  751. struct btrfs_root *root = fs_info->tree_root;
  752. struct btrfs_root *fs_root = NULL;
  753. struct btrfs_root_ref *root_ref;
  754. struct btrfs_inode_ref *inode_ref;
  755. struct btrfs_key key;
  756. BTRFS_PATH_AUTO_FREE(path);
  757. char *name = NULL, *ptr;
  758. u64 dirid;
  759. int len;
  760. int ret;
  761. path = btrfs_alloc_path();
  762. if (!path)
  763. return ERR_PTR(-ENOMEM);
  764. name = kmalloc(PATH_MAX, GFP_KERNEL);
  765. if (!name) {
  766. ret = -ENOMEM;
  767. goto err;
  768. }
  769. ptr = name + PATH_MAX - 1;
  770. ptr[0] = '\0';
  771. /*
  772. * Walk up the subvolume trees in the tree of tree roots by root
  773. * backrefs until we hit the top-level subvolume.
  774. */
  775. while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  776. key.objectid = subvol_objectid;
  777. key.type = BTRFS_ROOT_BACKREF_KEY;
  778. key.offset = (u64)-1;
  779. ret = btrfs_search_backwards(root, &key, path);
  780. if (ret < 0) {
  781. goto err;
  782. } else if (ret > 0) {
  783. ret = -ENOENT;
  784. goto err;
  785. }
  786. subvol_objectid = key.offset;
  787. root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  788. struct btrfs_root_ref);
  789. len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
  790. ptr -= len + 1;
  791. if (ptr < name) {
  792. ret = -ENAMETOOLONG;
  793. goto err;
  794. }
  795. read_extent_buffer(path->nodes[0], ptr + 1,
  796. (unsigned long)(root_ref + 1), len);
  797. ptr[0] = '/';
  798. dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
  799. btrfs_release_path(path);
  800. fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
  801. if (IS_ERR(fs_root)) {
  802. ret = PTR_ERR(fs_root);
  803. fs_root = NULL;
  804. goto err;
  805. }
  806. /*
  807. * Walk up the filesystem tree by inode refs until we hit the
  808. * root directory.
  809. */
  810. while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
  811. key.objectid = dirid;
  812. key.type = BTRFS_INODE_REF_KEY;
  813. key.offset = (u64)-1;
  814. ret = btrfs_search_backwards(fs_root, &key, path);
  815. if (ret < 0) {
  816. goto err;
  817. } else if (ret > 0) {
  818. ret = -ENOENT;
  819. goto err;
  820. }
  821. dirid = key.offset;
  822. inode_ref = btrfs_item_ptr(path->nodes[0],
  823. path->slots[0],
  824. struct btrfs_inode_ref);
  825. len = btrfs_inode_ref_name_len(path->nodes[0],
  826. inode_ref);
  827. ptr -= len + 1;
  828. if (ptr < name) {
  829. ret = -ENAMETOOLONG;
  830. goto err;
  831. }
  832. read_extent_buffer(path->nodes[0], ptr + 1,
  833. (unsigned long)(inode_ref + 1), len);
  834. ptr[0] = '/';
  835. btrfs_release_path(path);
  836. }
  837. btrfs_put_root(fs_root);
  838. fs_root = NULL;
  839. }
  840. if (ptr == name + PATH_MAX - 1) {
  841. name[0] = '/';
  842. name[1] = '\0';
  843. } else {
  844. memmove(name, ptr, name + PATH_MAX - ptr);
  845. }
  846. return name;
  847. err:
  848. btrfs_put_root(fs_root);
  849. kfree(name);
  850. return ERR_PTR(ret);
  851. }
  852. static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
  853. {
  854. struct btrfs_root *root = fs_info->tree_root;
  855. struct btrfs_dir_item *di;
  856. BTRFS_PATH_AUTO_FREE(path);
  857. struct btrfs_key location;
  858. struct fscrypt_str name = FSTR_INIT("default", 7);
  859. u64 dir_id;
  860. path = btrfs_alloc_path();
  861. if (!path)
  862. return -ENOMEM;
  863. /*
  864. * Find the "default" dir item which points to the root item that we
  865. * will mount by default if we haven't been given a specific subvolume
  866. * to mount.
  867. */
  868. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  869. di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
  870. if (IS_ERR(di)) {
  871. return PTR_ERR(di);
  872. }
  873. if (!di) {
  874. /*
  875. * Ok the default dir item isn't there. This is weird since
  876. * it's always been there, but don't freak out, just try and
  877. * mount the top-level subvolume.
  878. */
  879. *objectid = BTRFS_FS_TREE_OBJECTID;
  880. return 0;
  881. }
  882. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  883. *objectid = location.objectid;
  884. return 0;
  885. }
  886. static int btrfs_fill_super(struct super_block *sb,
  887. struct btrfs_fs_devices *fs_devices)
  888. {
  889. struct btrfs_inode *inode;
  890. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  891. int ret;
  892. sb->s_maxbytes = MAX_LFS_FILESIZE;
  893. sb->s_magic = BTRFS_SUPER_MAGIC;
  894. sb->s_op = &btrfs_super_ops;
  895. set_default_d_op(sb, &btrfs_dentry_operations);
  896. sb->s_export_op = &btrfs_export_ops;
  897. #ifdef CONFIG_FS_VERITY
  898. sb->s_vop = &btrfs_verityops;
  899. #endif
  900. sb->s_xattr = btrfs_xattr_handlers;
  901. sb->s_time_gran = 1;
  902. sb->s_iflags |= SB_I_CGROUPWB | SB_I_ALLOW_HSM;
  903. ret = super_setup_bdi(sb);
  904. if (ret) {
  905. btrfs_err(fs_info, "super_setup_bdi failed");
  906. return ret;
  907. }
  908. ret = open_ctree(sb, fs_devices);
  909. if (ret) {
  910. btrfs_err(fs_info, "open_ctree failed: %d", ret);
  911. return ret;
  912. }
  913. btrfs_emit_options(fs_info, NULL);
  914. inode = btrfs_iget(BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
  915. if (IS_ERR(inode)) {
  916. ret = PTR_ERR(inode);
  917. btrfs_handle_fs_error(fs_info, ret, NULL);
  918. goto fail_close;
  919. }
  920. sb->s_root = d_make_root(&inode->vfs_inode);
  921. if (!sb->s_root) {
  922. ret = -ENOMEM;
  923. goto fail_close;
  924. }
  925. sb->s_flags |= SB_ACTIVE;
  926. return 0;
  927. fail_close:
  928. close_ctree(fs_info);
  929. return ret;
  930. }
  931. int btrfs_sync_fs(struct super_block *sb, int wait)
  932. {
  933. struct btrfs_trans_handle *trans;
  934. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  935. struct btrfs_root *root = fs_info->tree_root;
  936. trace_btrfs_sync_fs(fs_info, wait);
  937. if (!wait) {
  938. filemap_flush(fs_info->btree_inode->i_mapping);
  939. return 0;
  940. }
  941. btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
  942. trans = btrfs_attach_transaction_barrier(root);
  943. if (IS_ERR(trans)) {
  944. /* no transaction, don't bother */
  945. if (PTR_ERR(trans) == -ENOENT) {
  946. /*
  947. * Exit unless we have some pending changes
  948. * that need to go through commit
  949. */
  950. if (!test_bit(BTRFS_FS_NEED_TRANS_COMMIT,
  951. &fs_info->flags))
  952. return 0;
  953. /*
  954. * A non-blocking test if the fs is frozen. We must not
  955. * start a new transaction here otherwise a deadlock
  956. * happens. The pending operations are delayed to the
  957. * next commit after thawing.
  958. */
  959. if (sb_start_write_trylock(sb))
  960. sb_end_write(sb);
  961. else
  962. return 0;
  963. trans = btrfs_start_transaction(root, 0);
  964. }
  965. if (IS_ERR(trans))
  966. return PTR_ERR(trans);
  967. }
  968. return btrfs_commit_transaction(trans);
  969. }
  970. static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
  971. {
  972. seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
  973. *printed = true;
  974. }
  975. static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
  976. {
  977. struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  978. const char *compress_type;
  979. const char *subvol_name;
  980. bool printed = false;
  981. if (btrfs_test_opt(info, DEGRADED))
  982. seq_puts(seq, ",degraded");
  983. if (btrfs_test_opt(info, NODATASUM))
  984. seq_puts(seq, ",nodatasum");
  985. if (btrfs_test_opt(info, NODATACOW))
  986. seq_puts(seq, ",nodatacow");
  987. if (btrfs_test_opt(info, NOBARRIER))
  988. seq_puts(seq, ",nobarrier");
  989. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  990. seq_printf(seq, ",max_inline=%llu", info->max_inline);
  991. if (info->thread_pool_size != min_t(unsigned long,
  992. num_online_cpus() + 2, 8))
  993. seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
  994. if (btrfs_test_opt(info, COMPRESS)) {
  995. compress_type = btrfs_compress_type2str(info->compress_type);
  996. if (btrfs_test_opt(info, FORCE_COMPRESS))
  997. seq_printf(seq, ",compress-force=%s", compress_type);
  998. else
  999. seq_printf(seq, ",compress=%s", compress_type);
  1000. if (info->compress_level && info->compress_type != BTRFS_COMPRESS_LZO)
  1001. seq_printf(seq, ":%d", info->compress_level);
  1002. }
  1003. if (btrfs_test_opt(info, NOSSD))
  1004. seq_puts(seq, ",nossd");
  1005. if (btrfs_test_opt(info, SSD_SPREAD))
  1006. seq_puts(seq, ",ssd_spread");
  1007. else if (btrfs_test_opt(info, SSD))
  1008. seq_puts(seq, ",ssd");
  1009. if (btrfs_test_opt(info, NOTREELOG))
  1010. seq_puts(seq, ",notreelog");
  1011. if (btrfs_test_opt(info, NOLOGREPLAY))
  1012. print_rescue_option(seq, "nologreplay", &printed);
  1013. if (btrfs_test_opt(info, USEBACKUPROOT))
  1014. print_rescue_option(seq, "usebackuproot", &printed);
  1015. if (btrfs_test_opt(info, IGNOREBADROOTS))
  1016. print_rescue_option(seq, "ignorebadroots", &printed);
  1017. if (btrfs_test_opt(info, IGNOREDATACSUMS))
  1018. print_rescue_option(seq, "ignoredatacsums", &printed);
  1019. if (btrfs_test_opt(info, IGNOREMETACSUMS))
  1020. print_rescue_option(seq, "ignoremetacsums", &printed);
  1021. if (btrfs_test_opt(info, IGNORESUPERFLAGS))
  1022. print_rescue_option(seq, "ignoresuperflags", &printed);
  1023. if (btrfs_test_opt(info, FLUSHONCOMMIT))
  1024. seq_puts(seq, ",flushoncommit");
  1025. if (btrfs_test_opt(info, DISCARD_SYNC))
  1026. seq_puts(seq, ",discard");
  1027. if (btrfs_test_opt(info, DISCARD_ASYNC))
  1028. seq_puts(seq, ",discard=async");
  1029. if (!(info->sb->s_flags & SB_POSIXACL))
  1030. seq_puts(seq, ",noacl");
  1031. if (btrfs_free_space_cache_v1_active(info))
  1032. seq_puts(seq, ",space_cache");
  1033. else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
  1034. seq_puts(seq, ",space_cache=v2");
  1035. else
  1036. seq_puts(seq, ",nospace_cache");
  1037. if (btrfs_test_opt(info, RESCAN_UUID_TREE))
  1038. seq_puts(seq, ",rescan_uuid_tree");
  1039. if (btrfs_test_opt(info, CLEAR_CACHE))
  1040. seq_puts(seq, ",clear_cache");
  1041. if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
  1042. seq_puts(seq, ",user_subvol_rm_allowed");
  1043. if (btrfs_test_opt(info, ENOSPC_DEBUG))
  1044. seq_puts(seq, ",enospc_debug");
  1045. if (btrfs_test_opt(info, AUTO_DEFRAG))
  1046. seq_puts(seq, ",autodefrag");
  1047. if (btrfs_test_opt(info, SKIP_BALANCE))
  1048. seq_puts(seq, ",skip_balance");
  1049. if (info->metadata_ratio)
  1050. seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
  1051. if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
  1052. seq_puts(seq, ",fatal_errors=panic");
  1053. if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  1054. seq_printf(seq, ",commit=%u", info->commit_interval);
  1055. #ifdef CONFIG_BTRFS_DEBUG
  1056. if (btrfs_test_opt(info, FRAGMENT_DATA))
  1057. seq_puts(seq, ",fragment=data");
  1058. if (btrfs_test_opt(info, FRAGMENT_METADATA))
  1059. seq_puts(seq, ",fragment=metadata");
  1060. #endif
  1061. if (btrfs_test_opt(info, REF_VERIFY))
  1062. seq_puts(seq, ",ref_verify");
  1063. if (btrfs_test_opt(info, REF_TRACKER))
  1064. seq_puts(seq, ",ref_tracker");
  1065. seq_printf(seq, ",subvolid=%llu", btrfs_root_id(BTRFS_I(d_inode(dentry))->root));
  1066. subvol_name = btrfs_get_subvol_name_from_objectid(info,
  1067. btrfs_root_id(BTRFS_I(d_inode(dentry))->root));
  1068. if (!IS_ERR(subvol_name)) {
  1069. seq_show_option(seq, "subvol", subvol_name);
  1070. kfree(subvol_name);
  1071. }
  1072. return 0;
  1073. }
  1074. /*
  1075. * subvolumes are identified by ino 256
  1076. */
  1077. static inline bool is_subvolume_inode(struct inode *inode)
  1078. {
  1079. if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  1080. return true;
  1081. return false;
  1082. }
  1083. static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  1084. struct vfsmount *mnt)
  1085. {
  1086. struct dentry *root;
  1087. int ret;
  1088. if (!subvol_name) {
  1089. if (!subvol_objectid) {
  1090. ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  1091. &subvol_objectid);
  1092. if (ret) {
  1093. root = ERR_PTR(ret);
  1094. goto out;
  1095. }
  1096. }
  1097. subvol_name = btrfs_get_subvol_name_from_objectid(
  1098. btrfs_sb(mnt->mnt_sb), subvol_objectid);
  1099. if (IS_ERR(subvol_name)) {
  1100. root = ERR_CAST(subvol_name);
  1101. subvol_name = NULL;
  1102. goto out;
  1103. }
  1104. }
  1105. root = mount_subtree(mnt, subvol_name);
  1106. /* mount_subtree() drops our reference on the vfsmount. */
  1107. mnt = NULL;
  1108. if (!IS_ERR(root)) {
  1109. struct super_block *s = root->d_sb;
  1110. struct btrfs_fs_info *fs_info = btrfs_sb(s);
  1111. struct inode *root_inode = d_inode(root);
  1112. u64 root_objectid = btrfs_root_id(BTRFS_I(root_inode)->root);
  1113. ret = 0;
  1114. if (!is_subvolume_inode(root_inode)) {
  1115. btrfs_err(fs_info, "'%s' is not a valid subvolume",
  1116. subvol_name);
  1117. ret = -EINVAL;
  1118. }
  1119. if (subvol_objectid && root_objectid != subvol_objectid) {
  1120. /*
  1121. * This will also catch a race condition where a
  1122. * subvolume which was passed by ID is renamed and
  1123. * another subvolume is renamed over the old location.
  1124. */
  1125. btrfs_err(fs_info,
  1126. "subvol '%s' does not match subvolid %llu",
  1127. subvol_name, subvol_objectid);
  1128. ret = -EINVAL;
  1129. }
  1130. if (ret) {
  1131. dput(root);
  1132. root = ERR_PTR(ret);
  1133. deactivate_locked_super(s);
  1134. }
  1135. }
  1136. out:
  1137. mntput(mnt);
  1138. kfree(subvol_name);
  1139. return root;
  1140. }
  1141. static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  1142. u32 new_pool_size, u32 old_pool_size)
  1143. {
  1144. if (new_pool_size == old_pool_size)
  1145. return;
  1146. fs_info->thread_pool_size = new_pool_size;
  1147. btrfs_info(fs_info, "resize thread pool %d -> %d",
  1148. old_pool_size, new_pool_size);
  1149. btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
  1150. btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
  1151. btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
  1152. workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
  1153. workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
  1154. btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  1155. btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
  1156. btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
  1157. }
  1158. static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  1159. unsigned long long old_opts, int flags)
  1160. {
  1161. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1162. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  1163. (flags & SB_RDONLY))) {
  1164. /* wait for any defraggers to finish */
  1165. wait_event(fs_info->transaction_wait,
  1166. (atomic_read(&fs_info->defrag_running) == 0));
  1167. if (flags & SB_RDONLY)
  1168. sync_filesystem(fs_info->sb);
  1169. }
  1170. }
  1171. static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  1172. unsigned long long old_opts)
  1173. {
  1174. const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
  1175. /*
  1176. * We need to cleanup all defraggable inodes if the autodefragment is
  1177. * close or the filesystem is read only.
  1178. */
  1179. if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  1180. (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
  1181. btrfs_cleanup_defrag_inodes(fs_info);
  1182. }
  1183. /* If we toggled discard async */
  1184. if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
  1185. btrfs_test_opt(fs_info, DISCARD_ASYNC))
  1186. btrfs_discard_resume(fs_info);
  1187. else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
  1188. !btrfs_test_opt(fs_info, DISCARD_ASYNC))
  1189. btrfs_discard_cleanup(fs_info);
  1190. /* If we toggled space cache */
  1191. if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
  1192. btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
  1193. }
  1194. static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
  1195. {
  1196. int ret;
  1197. if (BTRFS_FS_ERROR(fs_info)) {
  1198. btrfs_err(fs_info,
  1199. "remounting read-write after error is not allowed");
  1200. return -EINVAL;
  1201. }
  1202. if (fs_info->fs_devices->rw_devices == 0)
  1203. return -EACCES;
  1204. if (!btrfs_check_rw_degradable(fs_info, NULL)) {
  1205. btrfs_warn(fs_info,
  1206. "too many missing devices, writable remount is not allowed");
  1207. return -EACCES;
  1208. }
  1209. if (btrfs_super_log_root(fs_info->super_copy) != 0) {
  1210. btrfs_warn(fs_info,
  1211. "mount required to replay tree-log, cannot remount read-write");
  1212. return -EINVAL;
  1213. }
  1214. /*
  1215. * NOTE: when remounting with a change that does writes, don't put it
  1216. * anywhere above this point, as we are not sure to be safe to write
  1217. * until we pass the above checks.
  1218. */
  1219. ret = btrfs_start_pre_rw_mount(fs_info);
  1220. if (ret)
  1221. return ret;
  1222. btrfs_clear_sb_rdonly(fs_info->sb);
  1223. set_bit(BTRFS_FS_OPEN, &fs_info->flags);
  1224. /*
  1225. * If we've gone from readonly -> read-write, we need to get our
  1226. * sync/async discard lists in the right state.
  1227. */
  1228. btrfs_discard_resume(fs_info);
  1229. return 0;
  1230. }
  1231. static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
  1232. {
  1233. /*
  1234. * This also happens on 'umount -rf' or on shutdown, when the
  1235. * filesystem is busy.
  1236. */
  1237. cancel_work_sync(&fs_info->async_reclaim_work);
  1238. cancel_work_sync(&fs_info->async_data_reclaim_work);
  1239. btrfs_discard_cleanup(fs_info);
  1240. /* Wait for the uuid_scan task to finish */
  1241. down(&fs_info->uuid_tree_rescan_sem);
  1242. /* Avoid complains from lockdep et al. */
  1243. up(&fs_info->uuid_tree_rescan_sem);
  1244. btrfs_set_sb_rdonly(fs_info->sb);
  1245. /*
  1246. * Setting SB_RDONLY will put the cleaner thread to sleep at the next
  1247. * loop if it's already active. If it's already asleep, we'll leave
  1248. * unused block groups on disk until we're mounted read-write again
  1249. * unless we clean them up here.
  1250. */
  1251. btrfs_delete_unused_bgs(fs_info);
  1252. /*
  1253. * The cleaner task could be already running before we set the flag
  1254. * BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). We must make
  1255. * sure that after we finish the remount, i.e. after we call
  1256. * btrfs_commit_super(), the cleaner can no longer start a transaction
  1257. * - either because it was dropping a dead root, running delayed iputs
  1258. * or deleting an unused block group (the cleaner picked a block
  1259. * group from the list of unused block groups before we were able to
  1260. * in the previous call to btrfs_delete_unused_bgs()).
  1261. */
  1262. wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
  1263. /*
  1264. * We've set the superblock to RO mode, so we might have made the
  1265. * cleaner task sleep without running all pending delayed iputs. Go
  1266. * through all the delayed iputs here, so that if an unmount happens
  1267. * without remounting RW we don't end up at finishing close_ctree()
  1268. * with a non-empty list of delayed iputs.
  1269. */
  1270. btrfs_run_delayed_iputs(fs_info);
  1271. btrfs_dev_replace_suspend_for_unmount(fs_info);
  1272. btrfs_scrub_cancel(fs_info);
  1273. btrfs_pause_balance(fs_info);
  1274. /*
  1275. * Pause the qgroup rescan worker if it is running. We don't want it to
  1276. * be still running after we are in RO mode, as after that, by the time
  1277. * we unmount, it might have left a transaction open, so we would leak
  1278. * the transaction and/or crash.
  1279. */
  1280. btrfs_qgroup_wait_for_completion(fs_info, false);
  1281. return btrfs_commit_super(fs_info);
  1282. }
  1283. static void btrfs_ctx_to_info(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
  1284. {
  1285. fs_info->max_inline = ctx->max_inline;
  1286. fs_info->commit_interval = ctx->commit_interval;
  1287. fs_info->metadata_ratio = ctx->metadata_ratio;
  1288. fs_info->thread_pool_size = ctx->thread_pool_size;
  1289. fs_info->mount_opt = ctx->mount_opt;
  1290. fs_info->compress_type = ctx->compress_type;
  1291. fs_info->compress_level = ctx->compress_level;
  1292. }
  1293. static void btrfs_info_to_ctx(struct btrfs_fs_info *fs_info, struct btrfs_fs_context *ctx)
  1294. {
  1295. ctx->max_inline = fs_info->max_inline;
  1296. ctx->commit_interval = fs_info->commit_interval;
  1297. ctx->metadata_ratio = fs_info->metadata_ratio;
  1298. ctx->thread_pool_size = fs_info->thread_pool_size;
  1299. ctx->mount_opt = fs_info->mount_opt;
  1300. ctx->compress_type = fs_info->compress_type;
  1301. ctx->compress_level = fs_info->compress_level;
  1302. }
  1303. #define btrfs_info_if_set(fs_info, old_ctx, opt, fmt, args...) \
  1304. do { \
  1305. if ((!old_ctx || !btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
  1306. btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
  1307. btrfs_info(fs_info, fmt, ##args); \
  1308. } while (0)
  1309. #define btrfs_info_if_unset(fs_info, old_ctx, opt, fmt, args...) \
  1310. do { \
  1311. if ((old_ctx && btrfs_raw_test_opt(old_ctx->mount_opt, opt)) && \
  1312. !btrfs_raw_test_opt(fs_info->mount_opt, opt)) \
  1313. btrfs_info(fs_info, fmt, ##args); \
  1314. } while (0)
  1315. static void btrfs_emit_options(struct btrfs_fs_info *info,
  1316. struct btrfs_fs_context *old)
  1317. {
  1318. btrfs_info_if_set(info, old, NODATASUM, "setting nodatasum");
  1319. btrfs_info_if_set(info, old, DEGRADED, "allowing degraded mounts");
  1320. btrfs_info_if_set(info, old, NODATACOW, "setting nodatacow");
  1321. btrfs_info_if_set(info, old, SSD, "enabling ssd optimizations");
  1322. btrfs_info_if_set(info, old, SSD_SPREAD, "using spread ssd allocation scheme");
  1323. btrfs_info_if_set(info, old, NOBARRIER, "turning off barriers");
  1324. btrfs_info_if_set(info, old, NOTREELOG, "disabling tree log");
  1325. btrfs_info_if_set(info, old, NOLOGREPLAY, "disabling log replay at mount time");
  1326. btrfs_info_if_set(info, old, FLUSHONCOMMIT, "turning on flush-on-commit");
  1327. btrfs_info_if_set(info, old, DISCARD_SYNC, "turning on sync discard");
  1328. btrfs_info_if_set(info, old, DISCARD_ASYNC, "turning on async discard");
  1329. btrfs_info_if_set(info, old, FREE_SPACE_TREE, "enabling free space tree");
  1330. btrfs_info_if_set(info, old, SPACE_CACHE, "enabling disk space caching");
  1331. btrfs_info_if_set(info, old, CLEAR_CACHE, "force clearing of disk cache");
  1332. btrfs_info_if_set(info, old, AUTO_DEFRAG, "enabling auto defrag");
  1333. btrfs_info_if_set(info, old, FRAGMENT_DATA, "fragmenting data");
  1334. btrfs_info_if_set(info, old, FRAGMENT_METADATA, "fragmenting metadata");
  1335. btrfs_info_if_set(info, old, REF_VERIFY, "doing ref verification");
  1336. btrfs_info_if_set(info, old, USEBACKUPROOT, "trying to use backup root at mount time");
  1337. btrfs_info_if_set(info, old, IGNOREBADROOTS, "ignoring bad roots");
  1338. btrfs_info_if_set(info, old, IGNOREDATACSUMS, "ignoring data csums");
  1339. btrfs_info_if_set(info, old, IGNOREMETACSUMS, "ignoring meta csums");
  1340. btrfs_info_if_set(info, old, IGNORESUPERFLAGS, "ignoring unknown super block flags");
  1341. btrfs_info_if_unset(info, old, NODATASUM, "setting datasum");
  1342. btrfs_info_if_unset(info, old, NODATACOW, "setting datacow");
  1343. btrfs_info_if_unset(info, old, SSD, "not using ssd optimizations");
  1344. btrfs_info_if_unset(info, old, SSD_SPREAD, "not using spread ssd allocation scheme");
  1345. btrfs_info_if_unset(info, old, NOBARRIER, "turning on barriers");
  1346. btrfs_info_if_unset(info, old, NOTREELOG, "enabling tree log");
  1347. btrfs_info_if_unset(info, old, SPACE_CACHE, "disabling disk space caching");
  1348. btrfs_info_if_unset(info, old, FREE_SPACE_TREE, "disabling free space tree");
  1349. btrfs_info_if_unset(info, old, AUTO_DEFRAG, "disabling auto defrag");
  1350. btrfs_info_if_unset(info, old, COMPRESS, "use no compression");
  1351. /* Did the compression settings change? */
  1352. if (btrfs_test_opt(info, COMPRESS) &&
  1353. (!old ||
  1354. old->compress_type != info->compress_type ||
  1355. old->compress_level != info->compress_level ||
  1356. (!btrfs_raw_test_opt(old->mount_opt, FORCE_COMPRESS) &&
  1357. btrfs_raw_test_opt(info->mount_opt, FORCE_COMPRESS)))) {
  1358. const char *compress_type = btrfs_compress_type2str(info->compress_type);
  1359. btrfs_info(info, "%s %s compression, level %d",
  1360. btrfs_test_opt(info, FORCE_COMPRESS) ? "force" : "use",
  1361. compress_type, info->compress_level);
  1362. }
  1363. if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
  1364. btrfs_info(info, "max_inline set to %llu", info->max_inline);
  1365. }
  1366. static int btrfs_reconfigure(struct fs_context *fc)
  1367. {
  1368. struct super_block *sb = fc->root->d_sb;
  1369. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1370. struct btrfs_fs_context *ctx = fc->fs_private;
  1371. struct btrfs_fs_context old_ctx;
  1372. int ret = 0;
  1373. bool mount_reconfigure = (fc->s_fs_info != NULL);
  1374. btrfs_info_to_ctx(fs_info, &old_ctx);
  1375. /*
  1376. * This is our "bind mount" trick, we don't want to allow the user to do
  1377. * anything other than mount a different ro/rw and a different subvol,
  1378. * all of the mount options should be maintained.
  1379. */
  1380. if (mount_reconfigure)
  1381. ctx->mount_opt = old_ctx.mount_opt;
  1382. sync_filesystem(sb);
  1383. set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1384. if (!btrfs_check_options(fs_info, &ctx->mount_opt, fc->sb_flags))
  1385. return -EINVAL;
  1386. ret = btrfs_check_features(fs_info, !(fc->sb_flags & SB_RDONLY));
  1387. if (ret < 0)
  1388. return ret;
  1389. btrfs_ctx_to_info(fs_info, ctx);
  1390. btrfs_remount_begin(fs_info, old_ctx.mount_opt, fc->sb_flags);
  1391. btrfs_resize_thread_pool(fs_info, fs_info->thread_pool_size,
  1392. old_ctx.thread_pool_size);
  1393. if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
  1394. (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
  1395. (!sb_rdonly(sb) || (fc->sb_flags & SB_RDONLY))) {
  1396. btrfs_warn(fs_info,
  1397. "remount supports changing free space tree only from RO to RW");
  1398. /* Make sure free space cache options match the state on disk. */
  1399. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  1400. btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
  1401. btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
  1402. }
  1403. if (btrfs_free_space_cache_v1_active(fs_info)) {
  1404. btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
  1405. btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
  1406. }
  1407. }
  1408. ret = 0;
  1409. if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY))
  1410. ret = btrfs_remount_ro(fs_info);
  1411. else if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY))
  1412. ret = btrfs_remount_rw(fs_info);
  1413. if (ret)
  1414. goto restore;
  1415. /*
  1416. * If we set the mask during the parameter parsing VFS would reject the
  1417. * remount. Here we can set the mask and the value will be updated
  1418. * appropriately.
  1419. */
  1420. if ((fc->sb_flags & SB_POSIXACL) != (sb->s_flags & SB_POSIXACL))
  1421. fc->sb_flags_mask |= SB_POSIXACL;
  1422. btrfs_emit_options(fs_info, &old_ctx);
  1423. wake_up_process(fs_info->transaction_kthread);
  1424. btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
  1425. btrfs_clear_oneshot_options(fs_info);
  1426. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1427. return 0;
  1428. restore:
  1429. btrfs_ctx_to_info(fs_info, &old_ctx);
  1430. btrfs_remount_cleanup(fs_info, old_ctx.mount_opt);
  1431. clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  1432. return ret;
  1433. }
  1434. /* Used to sort the devices by max_avail(descending sort) */
  1435. static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
  1436. {
  1437. const struct btrfs_device_info *dev_info1 = a;
  1438. const struct btrfs_device_info *dev_info2 = b;
  1439. if (dev_info1->max_avail > dev_info2->max_avail)
  1440. return -1;
  1441. else if (dev_info1->max_avail < dev_info2->max_avail)
  1442. return 1;
  1443. return 0;
  1444. }
  1445. /*
  1446. * sort the devices by max_avail, in which max free extent size of each device
  1447. * is stored.(Descending Sort)
  1448. */
  1449. static inline void btrfs_descending_sort_devices(
  1450. struct btrfs_device_info *devices,
  1451. size_t nr_devices)
  1452. {
  1453. sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  1454. btrfs_cmp_device_free_bytes, NULL);
  1455. }
  1456. /*
  1457. * The helper to calc the free space on the devices that can be used to store
  1458. * file data.
  1459. */
  1460. static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
  1461. u64 *free_bytes)
  1462. {
  1463. struct btrfs_device_info AUTO_KFREE(devices_info);
  1464. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  1465. struct btrfs_device *device;
  1466. u64 type;
  1467. u64 avail_space;
  1468. u64 min_stripe_size;
  1469. int num_stripes = 1;
  1470. int i = 0, nr_devices;
  1471. const struct btrfs_raid_attr *rattr;
  1472. /*
  1473. * We aren't under the device list lock, so this is racy-ish, but good
  1474. * enough for our purposes.
  1475. */
  1476. nr_devices = fs_info->fs_devices->open_devices;
  1477. if (!nr_devices) {
  1478. smp_mb();
  1479. nr_devices = fs_info->fs_devices->open_devices;
  1480. ASSERT(nr_devices);
  1481. if (!nr_devices) {
  1482. *free_bytes = 0;
  1483. return 0;
  1484. }
  1485. }
  1486. devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
  1487. GFP_KERNEL);
  1488. if (!devices_info)
  1489. return -ENOMEM;
  1490. /* calc min stripe number for data space allocation */
  1491. type = btrfs_data_alloc_profile(fs_info);
  1492. rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
  1493. if (type & BTRFS_BLOCK_GROUP_RAID0)
  1494. num_stripes = nr_devices;
  1495. else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
  1496. num_stripes = rattr->ncopies;
  1497. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1498. num_stripes = 4;
  1499. /* Adjust for more than 1 stripe per device */
  1500. min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
  1501. rcu_read_lock();
  1502. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  1503. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
  1504. &device->dev_state) ||
  1505. !device->bdev ||
  1506. test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
  1507. continue;
  1508. if (i >= nr_devices)
  1509. break;
  1510. avail_space = device->total_bytes - device->bytes_used;
  1511. /* align with stripe_len */
  1512. avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
  1513. /*
  1514. * Ensure we have at least min_stripe_size on top of the
  1515. * reserved space on the device.
  1516. */
  1517. if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
  1518. continue;
  1519. avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
  1520. devices_info[i].dev = device;
  1521. devices_info[i].max_avail = avail_space;
  1522. i++;
  1523. }
  1524. rcu_read_unlock();
  1525. nr_devices = i;
  1526. btrfs_descending_sort_devices(devices_info, nr_devices);
  1527. i = nr_devices - 1;
  1528. avail_space = 0;
  1529. while (nr_devices >= rattr->devs_min) {
  1530. num_stripes = min(num_stripes, nr_devices);
  1531. if (devices_info[i].max_avail >= min_stripe_size) {
  1532. int j;
  1533. u64 alloc_size;
  1534. avail_space += devices_info[i].max_avail * num_stripes;
  1535. alloc_size = devices_info[i].max_avail;
  1536. for (j = i + 1 - num_stripes; j <= i; j++)
  1537. devices_info[j].max_avail -= alloc_size;
  1538. }
  1539. i--;
  1540. nr_devices--;
  1541. }
  1542. *free_bytes = avail_space;
  1543. return 0;
  1544. }
  1545. /*
  1546. * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
  1547. *
  1548. * If there's a redundant raid level at DATA block groups, use the respective
  1549. * multiplier to scale the sizes.
  1550. *
  1551. * Unused device space usage is based on simulating the chunk allocator
  1552. * algorithm that respects the device sizes and order of allocations. This is
  1553. * a close approximation of the actual use but there are other factors that may
  1554. * change the result (like a new metadata chunk).
  1555. *
  1556. * If metadata is exhausted, f_bavail will be 0.
  1557. */
  1558. static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1559. {
  1560. struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  1561. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1562. struct btrfs_space_info *found;
  1563. u64 total_used = 0;
  1564. u64 total_free_data = 0;
  1565. u64 total_free_meta = 0;
  1566. u32 bits = fs_info->sectorsize_bits;
  1567. __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
  1568. unsigned factor = 1;
  1569. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  1570. int ret;
  1571. u64 thresh = 0;
  1572. int mixed = 0;
  1573. list_for_each_entry(found, &fs_info->space_info, list) {
  1574. if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
  1575. int i;
  1576. total_free_data += found->disk_total - found->disk_used;
  1577. total_free_data -=
  1578. btrfs_account_ro_block_groups_free_space(found);
  1579. for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  1580. if (!list_empty(&found->block_groups[i]))
  1581. factor = btrfs_bg_type_to_factor(
  1582. btrfs_raid_array[i].bg_flag);
  1583. }
  1584. }
  1585. /*
  1586. * Metadata in mixed block group profiles are accounted in data
  1587. */
  1588. if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
  1589. if (found->flags & BTRFS_BLOCK_GROUP_DATA)
  1590. mixed = 1;
  1591. else
  1592. total_free_meta += found->disk_total -
  1593. found->disk_used;
  1594. }
  1595. total_used += found->disk_used;
  1596. }
  1597. buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  1598. buf->f_blocks >>= bits;
  1599. buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  1600. /* Account global block reserve as used, it's in logical size already */
  1601. spin_lock(&block_rsv->lock);
  1602. /* Mixed block groups accounting is not byte-accurate, avoid overflow */
  1603. if (buf->f_bfree >= block_rsv->size >> bits)
  1604. buf->f_bfree -= block_rsv->size >> bits;
  1605. else
  1606. buf->f_bfree = 0;
  1607. spin_unlock(&block_rsv->lock);
  1608. buf->f_bavail = div_u64(total_free_data, factor);
  1609. ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
  1610. if (ret)
  1611. return ret;
  1612. buf->f_bavail += div_u64(total_free_data, factor);
  1613. buf->f_bavail = buf->f_bavail >> bits;
  1614. /*
  1615. * We calculate the remaining metadata space minus global reserve. If
  1616. * this is (supposedly) smaller than zero, there's no space. But this
  1617. * does not hold in practice, the exhausted state happens where's still
  1618. * some positive delta. So we apply some guesswork and compare the
  1619. * delta to a 4M threshold. (Practically observed delta was ~2M.)
  1620. *
  1621. * We probably cannot calculate the exact threshold value because this
  1622. * depends on the internal reservations requested by various
  1623. * operations, so some operations that consume a few metadata will
  1624. * succeed even if the Avail is zero. But this is better than the other
  1625. * way around.
  1626. */
  1627. thresh = SZ_4M;
  1628. /*
  1629. * We only want to claim there's no available space if we can no longer
  1630. * allocate chunks for our metadata profile and our global reserve will
  1631. * not fit in the free metadata space. If we aren't ->full then we
  1632. * still can allocate chunks and thus are fine using the currently
  1633. * calculated f_bavail.
  1634. */
  1635. if (!mixed && block_rsv->space_info->full &&
  1636. (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
  1637. buf->f_bavail = 0;
  1638. buf->f_type = BTRFS_SUPER_MAGIC;
  1639. buf->f_bsize = fs_info->sectorsize;
  1640. buf->f_namelen = BTRFS_NAME_LEN;
  1641. /* We treat it as constant endianness (it doesn't matter _which_)
  1642. because we want the fsid to come out the same whether mounted
  1643. on a big-endian or little-endian host */
  1644. buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  1645. buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
  1646. /* Mask in the root object ID too, to disambiguate subvols */
  1647. buf->f_fsid.val[0] ^= btrfs_root_id(BTRFS_I(d_inode(dentry))->root) >> 32;
  1648. buf->f_fsid.val[1] ^= btrfs_root_id(BTRFS_I(d_inode(dentry))->root);
  1649. return 0;
  1650. }
  1651. static int btrfs_fc_test_super(struct super_block *sb, struct fs_context *fc)
  1652. {
  1653. struct btrfs_fs_info *p = fc->s_fs_info;
  1654. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1655. return fs_info->fs_devices == p->fs_devices;
  1656. }
  1657. static int btrfs_get_tree_super(struct fs_context *fc)
  1658. {
  1659. struct btrfs_fs_info *fs_info = fc->s_fs_info;
  1660. struct btrfs_fs_context *ctx = fc->fs_private;
  1661. struct btrfs_fs_devices *fs_devices = NULL;
  1662. struct btrfs_device *device;
  1663. struct super_block *sb;
  1664. blk_mode_t mode = sb_open_mode(fc->sb_flags);
  1665. int ret;
  1666. btrfs_ctx_to_info(fs_info, ctx);
  1667. mutex_lock(&uuid_mutex);
  1668. /*
  1669. * With 'true' passed to btrfs_scan_one_device() (mount time) we expect
  1670. * either a valid device or an error.
  1671. */
  1672. device = btrfs_scan_one_device(fc->source, true);
  1673. ASSERT(device != NULL);
  1674. if (IS_ERR(device)) {
  1675. mutex_unlock(&uuid_mutex);
  1676. return PTR_ERR(device);
  1677. }
  1678. fs_devices = device->fs_devices;
  1679. /*
  1680. * We cannot hold uuid_mutex calling sget_fc(), it will lead to a
  1681. * locking order reversal with s_umount.
  1682. *
  1683. * So here we increase the holding number of fs_devices, this will ensure
  1684. * the fs_devices itself won't be freed.
  1685. */
  1686. btrfs_fs_devices_inc_holding(fs_devices);
  1687. fs_info->fs_devices = fs_devices;
  1688. mutex_unlock(&uuid_mutex);
  1689. sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
  1690. if (IS_ERR(sb)) {
  1691. mutex_lock(&uuid_mutex);
  1692. btrfs_fs_devices_dec_holding(fs_devices);
  1693. /*
  1694. * Since the fs_devices is not opened, it can be freed at any
  1695. * time after unlocking uuid_mutex. We need to avoid double
  1696. * free through put_fs_context()->btrfs_free_fs_info().
  1697. * So here we reset fs_info->fs_devices to NULL, and let the
  1698. * regular fs_devices reclaim path to handle it.
  1699. *
  1700. * This applies to all later branches where no fs_devices is
  1701. * opened.
  1702. */
  1703. fs_info->fs_devices = NULL;
  1704. mutex_unlock(&uuid_mutex);
  1705. return PTR_ERR(sb);
  1706. }
  1707. if (sb->s_root) {
  1708. /*
  1709. * Not the first mount of the fs thus got an existing super block.
  1710. * Will reuse the returned super block, fs_info and fs_devices.
  1711. *
  1712. * fc->s_fs_info is not touched and will be later freed by
  1713. * put_fs_context() through btrfs_free_fs_context().
  1714. */
  1715. ASSERT(fc->s_fs_info == fs_info);
  1716. mutex_lock(&uuid_mutex);
  1717. btrfs_fs_devices_dec_holding(fs_devices);
  1718. fs_info->fs_devices = NULL;
  1719. mutex_unlock(&uuid_mutex);
  1720. /*
  1721. * At this stage we may have RO flag mismatch between
  1722. * fc->sb_flags and sb->s_flags. Caller should detect such
  1723. * mismatch and reconfigure with sb->s_umount rwsem held if
  1724. * needed.
  1725. */
  1726. } else {
  1727. struct block_device *bdev;
  1728. /*
  1729. * The first mount of the fs thus a new superblock, fc->s_fs_info
  1730. * must be NULL, and the ownership of our fs_info and fs_devices is
  1731. * transferred to the super block.
  1732. */
  1733. ASSERT(fc->s_fs_info == NULL);
  1734. mutex_lock(&uuid_mutex);
  1735. btrfs_fs_devices_dec_holding(fs_devices);
  1736. ret = btrfs_open_devices(fs_devices, mode, sb);
  1737. if (ret < 0)
  1738. fs_info->fs_devices = NULL;
  1739. mutex_unlock(&uuid_mutex);
  1740. if (ret < 0) {
  1741. deactivate_locked_super(sb);
  1742. return ret;
  1743. }
  1744. if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
  1745. deactivate_locked_super(sb);
  1746. return -EACCES;
  1747. }
  1748. set_device_specific_options(fs_info);
  1749. bdev = fs_devices->latest_dev->bdev;
  1750. snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
  1751. shrinker_debugfs_rename(sb->s_shrink, "sb-btrfs:%s", sb->s_id);
  1752. ret = btrfs_fill_super(sb, fs_devices);
  1753. if (ret) {
  1754. deactivate_locked_super(sb);
  1755. return ret;
  1756. }
  1757. }
  1758. btrfs_clear_oneshot_options(fs_info);
  1759. fc->root = dget(sb->s_root);
  1760. return 0;
  1761. }
  1762. /*
  1763. * Ever since commit 0723a0473fb4 ("btrfs: allow mounting btrfs subvolumes
  1764. * with different ro/rw options") the following works:
  1765. *
  1766. * (i) mount /dev/sda3 -o subvol=foo,ro /mnt/foo
  1767. * (ii) mount /dev/sda3 -o subvol=bar,rw /mnt/bar
  1768. *
  1769. * which looks nice and innocent but is actually pretty intricate and deserves
  1770. * a long comment.
  1771. *
  1772. * On another filesystem a subvolume mount is close to something like:
  1773. *
  1774. * (iii) # create rw superblock + initial mount
  1775. * mount -t xfs /dev/sdb /opt/
  1776. *
  1777. * # create ro bind mount
  1778. * mount --bind -o ro /opt/foo /mnt/foo
  1779. *
  1780. * # unmount initial mount
  1781. * umount /opt
  1782. *
  1783. * Of course, there's some special subvolume sauce and there's the fact that the
  1784. * sb->s_root dentry is really swapped after mount_subtree(). But conceptually
  1785. * it's very close and will help us understand the issue.
  1786. *
  1787. * The old mount API didn't cleanly distinguish between a mount being made ro
  1788. * and a superblock being made ro. The only way to change the ro state of
  1789. * either object was by passing ms_rdonly. If a new mount was created via
  1790. * mount(2) such as:
  1791. *
  1792. * mount("/dev/sdb", "/mnt", "xfs", ms_rdonly, null);
  1793. *
  1794. * the MS_RDONLY flag being specified had two effects:
  1795. *
  1796. * (1) MNT_READONLY was raised -> the resulting mount got
  1797. * @mnt->mnt_flags |= MNT_READONLY raised.
  1798. *
  1799. * (2) MS_RDONLY was passed to the filesystem's mount method and the filesystems
  1800. * made the superblock ro. Note, how SB_RDONLY has the same value as
  1801. * ms_rdonly and is raised whenever MS_RDONLY is passed through mount(2).
  1802. *
  1803. * Creating a subtree mount via (iii) ends up leaving a rw superblock with a
  1804. * subtree mounted ro.
  1805. *
  1806. * But consider the effect on the old mount API on btrfs subvolume mounting
  1807. * which combines the distinct step in (iii) into a single step.
  1808. *
  1809. * By issuing (i) both the mount and the superblock are turned ro. Now when (ii)
  1810. * is issued the superblock is ro and thus even if the mount created for (ii) is
  1811. * rw it wouldn't help. Hence, btrfs needed to transition the superblock from ro
  1812. * to rw for (ii) which it did using an internal remount call.
  1813. *
  1814. * IOW, subvolume mounting was inherently complicated due to the ambiguity of
  1815. * MS_RDONLY in mount(2). Note, this ambiguity has mount(8) always translate
  1816. * "ro" to MS_RDONLY. IOW, in both (i) and (ii) "ro" becomes MS_RDONLY when
  1817. * passed by mount(8) to mount(2).
  1818. *
  1819. * Enter the new mount API. The new mount API disambiguates making a mount ro
  1820. * and making a superblock ro.
  1821. *
  1822. * (3) To turn a mount ro the MOUNT_ATTR_ONLY flag can be used with either
  1823. * fsmount() or mount_setattr() this is a pure VFS level change for a
  1824. * specific mount or mount tree that is never seen by the filesystem itself.
  1825. *
  1826. * (4) To turn a superblock ro the "ro" flag must be used with
  1827. * fsconfig(FSCONFIG_SET_FLAG, "ro"). This option is seen by the filesystem
  1828. * in fc->sb_flags.
  1829. *
  1830. * But, currently the util-linux mount command already utilizes the new mount
  1831. * API and is still setting fsconfig(FSCONFIG_SET_FLAG, "ro") no matter if it's
  1832. * btrfs or not, setting the whole super block RO. To make per-subvolume mounting
  1833. * work with different options work we need to keep backward compatibility.
  1834. */
  1835. static int btrfs_reconfigure_for_mount(struct fs_context *fc)
  1836. {
  1837. int ret = 0;
  1838. if (!(fc->sb_flags & SB_RDONLY) && (fc->root->d_sb->s_flags & SB_RDONLY))
  1839. ret = btrfs_reconfigure(fc);
  1840. return ret;
  1841. }
  1842. static int btrfs_get_tree_subvol(struct fs_context *fc)
  1843. {
  1844. struct btrfs_fs_info *fs_info = NULL;
  1845. struct btrfs_fs_context *ctx = fc->fs_private;
  1846. struct fs_context *dup_fc;
  1847. struct dentry *dentry;
  1848. struct vfsmount *mnt;
  1849. int ret = 0;
  1850. /*
  1851. * Setup a dummy root and fs_info for test/set super. This is because
  1852. * we don't actually fill this stuff out until open_ctree, but we need
  1853. * then open_ctree will properly initialize the file system specific
  1854. * settings later. btrfs_init_fs_info initializes the static elements
  1855. * of the fs_info (locks and such) to make cleanup easier if we find a
  1856. * superblock with our given fs_devices later on at sget() time.
  1857. */
  1858. fs_info = kvzalloc_obj(struct btrfs_fs_info);
  1859. if (!fs_info)
  1860. return -ENOMEM;
  1861. fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
  1862. fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
  1863. if (!fs_info->super_copy || !fs_info->super_for_commit) {
  1864. /*
  1865. * Dont call btrfs_free_fs_info() to free it as it's still
  1866. * initialized partially.
  1867. */
  1868. kfree(fs_info->super_copy);
  1869. kfree(fs_info->super_for_commit);
  1870. kvfree(fs_info);
  1871. return -ENOMEM;
  1872. }
  1873. btrfs_init_fs_info(fs_info);
  1874. dup_fc = vfs_dup_fs_context(fc);
  1875. if (IS_ERR(dup_fc)) {
  1876. btrfs_free_fs_info(fs_info);
  1877. return PTR_ERR(dup_fc);
  1878. }
  1879. /*
  1880. * When we do the sget_fc this gets transferred to the sb, so we only
  1881. * need to set it on the dup_fc as this is what creates the super block.
  1882. */
  1883. dup_fc->s_fs_info = fs_info;
  1884. ret = btrfs_get_tree_super(dup_fc);
  1885. if (ret)
  1886. goto error;
  1887. ret = btrfs_reconfigure_for_mount(dup_fc);
  1888. up_write(&dup_fc->root->d_sb->s_umount);
  1889. if (ret)
  1890. goto error;
  1891. mnt = vfs_create_mount(dup_fc);
  1892. put_fs_context(dup_fc);
  1893. if (IS_ERR(mnt))
  1894. return PTR_ERR(mnt);
  1895. /*
  1896. * This free's ->subvol_name, because if it isn't set we have to
  1897. * allocate a buffer to hold the subvol_name, so we just drop our
  1898. * reference to it here.
  1899. */
  1900. dentry = mount_subvol(ctx->subvol_name, ctx->subvol_objectid, mnt);
  1901. ctx->subvol_name = NULL;
  1902. if (IS_ERR(dentry))
  1903. return PTR_ERR(dentry);
  1904. fc->root = dentry;
  1905. return 0;
  1906. error:
  1907. put_fs_context(dup_fc);
  1908. return ret;
  1909. }
  1910. static int btrfs_get_tree(struct fs_context *fc)
  1911. {
  1912. ASSERT(fc->s_fs_info == NULL);
  1913. return btrfs_get_tree_subvol(fc);
  1914. }
  1915. static void btrfs_kill_super(struct super_block *sb)
  1916. {
  1917. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1918. kill_anon_super(sb);
  1919. btrfs_free_fs_info(fs_info);
  1920. }
  1921. static void btrfs_free_fs_context(struct fs_context *fc)
  1922. {
  1923. struct btrfs_fs_context *ctx = fc->fs_private;
  1924. struct btrfs_fs_info *fs_info = fc->s_fs_info;
  1925. if (fs_info)
  1926. btrfs_free_fs_info(fs_info);
  1927. if (ctx && refcount_dec_and_test(&ctx->refs)) {
  1928. kfree(ctx->subvol_name);
  1929. kfree(ctx);
  1930. }
  1931. }
  1932. static int btrfs_dup_fs_context(struct fs_context *fc, struct fs_context *src_fc)
  1933. {
  1934. struct btrfs_fs_context *ctx = src_fc->fs_private;
  1935. /*
  1936. * Give a ref to our ctx to this dup, as we want to keep it around for
  1937. * our original fc so we can have the subvolume name or objectid.
  1938. *
  1939. * We unset ->source in the original fc because the dup needs it for
  1940. * mounting, and then once we free the dup it'll free ->source, so we
  1941. * need to make sure we're only pointing to it in one fc.
  1942. */
  1943. refcount_inc(&ctx->refs);
  1944. fc->fs_private = ctx;
  1945. fc->source = src_fc->source;
  1946. src_fc->source = NULL;
  1947. return 0;
  1948. }
  1949. static const struct fs_context_operations btrfs_fs_context_ops = {
  1950. .parse_param = btrfs_parse_param,
  1951. .reconfigure = btrfs_reconfigure,
  1952. .get_tree = btrfs_get_tree,
  1953. .dup = btrfs_dup_fs_context,
  1954. .free = btrfs_free_fs_context,
  1955. };
  1956. static int btrfs_init_fs_context(struct fs_context *fc)
  1957. {
  1958. struct btrfs_fs_context *ctx;
  1959. ctx = kzalloc_obj(struct btrfs_fs_context);
  1960. if (!ctx)
  1961. return -ENOMEM;
  1962. refcount_set(&ctx->refs, 1);
  1963. fc->fs_private = ctx;
  1964. fc->ops = &btrfs_fs_context_ops;
  1965. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  1966. btrfs_info_to_ctx(btrfs_sb(fc->root->d_sb), ctx);
  1967. } else {
  1968. ctx->thread_pool_size =
  1969. min_t(unsigned long, num_online_cpus() + 2, 8);
  1970. ctx->max_inline = BTRFS_DEFAULT_MAX_INLINE;
  1971. ctx->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  1972. }
  1973. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  1974. fc->sb_flags |= SB_POSIXACL;
  1975. #endif
  1976. fc->sb_flags |= SB_I_VERSION;
  1977. return 0;
  1978. }
  1979. static struct file_system_type btrfs_fs_type = {
  1980. .owner = THIS_MODULE,
  1981. .name = "btrfs",
  1982. .init_fs_context = btrfs_init_fs_context,
  1983. .parameters = btrfs_fs_parameters,
  1984. .kill_sb = btrfs_kill_super,
  1985. .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA |
  1986. FS_ALLOW_IDMAP | FS_MGTIME,
  1987. };
  1988. MODULE_ALIAS_FS("btrfs");
  1989. static int btrfs_control_open(struct inode *inode, struct file *file)
  1990. {
  1991. /*
  1992. * The control file's private_data is used to hold the
  1993. * transaction when it is started and is used to keep
  1994. * track of whether a transaction is already in progress.
  1995. */
  1996. file->private_data = NULL;
  1997. return 0;
  1998. }
  1999. /*
  2000. * Used by /dev/btrfs-control for devices ioctls.
  2001. */
  2002. static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  2003. unsigned long arg)
  2004. {
  2005. struct btrfs_ioctl_vol_args *vol;
  2006. struct btrfs_device *device = NULL;
  2007. dev_t devt = 0;
  2008. int ret = -ENOTTY;
  2009. if (!capable(CAP_SYS_ADMIN))
  2010. return -EPERM;
  2011. vol = memdup_user((void __user *)arg, sizeof(*vol));
  2012. if (IS_ERR(vol))
  2013. return PTR_ERR(vol);
  2014. ret = btrfs_check_ioctl_vol_args_path(vol);
  2015. if (ret < 0)
  2016. goto out;
  2017. switch (cmd) {
  2018. case BTRFS_IOC_SCAN_DEV:
  2019. mutex_lock(&uuid_mutex);
  2020. /*
  2021. * Scanning outside of mount can return NULL which would turn
  2022. * into 0 error code.
  2023. */
  2024. device = btrfs_scan_one_device(vol->name, false);
  2025. ret = PTR_ERR_OR_ZERO(device);
  2026. mutex_unlock(&uuid_mutex);
  2027. break;
  2028. case BTRFS_IOC_FORGET_DEV:
  2029. if (vol->name[0] != 0) {
  2030. ret = lookup_bdev(vol->name, &devt);
  2031. if (ret)
  2032. break;
  2033. }
  2034. ret = btrfs_forget_devices(devt);
  2035. break;
  2036. case BTRFS_IOC_DEVICES_READY:
  2037. mutex_lock(&uuid_mutex);
  2038. /*
  2039. * Scanning outside of mount can return NULL which would turn
  2040. * into 0 error code.
  2041. */
  2042. device = btrfs_scan_one_device(vol->name, false);
  2043. if (IS_ERR_OR_NULL(device)) {
  2044. mutex_unlock(&uuid_mutex);
  2045. ret = PTR_ERR_OR_ZERO(device);
  2046. break;
  2047. }
  2048. ret = !(device->fs_devices->num_devices ==
  2049. device->fs_devices->total_devices);
  2050. mutex_unlock(&uuid_mutex);
  2051. break;
  2052. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  2053. ret = btrfs_ioctl_get_supported_features((void __user*)arg);
  2054. break;
  2055. }
  2056. out:
  2057. kfree(vol);
  2058. return ret;
  2059. }
  2060. static int btrfs_freeze(struct super_block *sb)
  2061. {
  2062. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2063. set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
  2064. /*
  2065. * We don't need a barrier here, we'll wait for any transaction that
  2066. * could be in progress on other threads (and do delayed iputs that
  2067. * we want to avoid on a frozen filesystem), or do the commit
  2068. * ourselves.
  2069. */
  2070. return btrfs_commit_current_transaction(fs_info->tree_root);
  2071. }
  2072. static int check_dev_super(struct btrfs_device *dev)
  2073. {
  2074. struct btrfs_fs_info *fs_info = dev->fs_info;
  2075. struct btrfs_super_block *sb;
  2076. u64 last_trans;
  2077. u16 csum_type;
  2078. int ret = 0;
  2079. /* This should be called with fs still frozen. */
  2080. ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
  2081. /* Missing dev, no need to check. */
  2082. if (!dev->bdev)
  2083. return 0;
  2084. /* Only need to check the primary super block. */
  2085. sb = btrfs_read_disk_super(dev->bdev, 0, true);
  2086. if (IS_ERR(sb))
  2087. return PTR_ERR(sb);
  2088. /* Verify the checksum. */
  2089. csum_type = btrfs_super_csum_type(sb);
  2090. if (unlikely(csum_type != btrfs_super_csum_type(fs_info->super_copy))) {
  2091. btrfs_err(fs_info, "csum type changed, has %u expect %u",
  2092. csum_type, btrfs_super_csum_type(fs_info->super_copy));
  2093. ret = -EUCLEAN;
  2094. goto out;
  2095. }
  2096. if (unlikely(btrfs_check_super_csum(fs_info, sb))) {
  2097. btrfs_err(fs_info, "csum for on-disk super block no longer matches");
  2098. ret = -EUCLEAN;
  2099. goto out;
  2100. }
  2101. /* Btrfs_validate_super() includes fsid check against super->fsid. */
  2102. ret = btrfs_validate_super(fs_info, sb, 0);
  2103. if (ret < 0)
  2104. goto out;
  2105. last_trans = btrfs_get_last_trans_committed(fs_info);
  2106. if (unlikely(btrfs_super_generation(sb) != last_trans)) {
  2107. btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
  2108. btrfs_super_generation(sb), last_trans);
  2109. ret = -EUCLEAN;
  2110. goto out;
  2111. }
  2112. out:
  2113. btrfs_release_disk_super(sb);
  2114. return ret;
  2115. }
  2116. static int btrfs_unfreeze(struct super_block *sb)
  2117. {
  2118. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2119. struct btrfs_device *device;
  2120. int ret = 0;
  2121. /*
  2122. * Make sure the fs is not changed by accident (like hibernation then
  2123. * modified by other OS).
  2124. * If we found anything wrong, we mark the fs error immediately.
  2125. *
  2126. * And since the fs is frozen, no one can modify the fs yet, thus
  2127. * we don't need to hold device_list_mutex.
  2128. */
  2129. list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
  2130. ret = check_dev_super(device);
  2131. if (ret < 0) {
  2132. btrfs_handle_fs_error(fs_info, ret,
  2133. "super block on devid %llu got modified unexpectedly",
  2134. device->devid);
  2135. break;
  2136. }
  2137. }
  2138. clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
  2139. /*
  2140. * We still return 0, to allow VFS layer to unfreeze the fs even the
  2141. * above checks failed. Since the fs is either fine or read-only, we're
  2142. * safe to continue, without causing further damage.
  2143. */
  2144. return 0;
  2145. }
  2146. static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  2147. {
  2148. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  2149. /*
  2150. * There should be always a valid pointer in latest_dev, it may be stale
  2151. * for a short moment in case it's being deleted but still valid until
  2152. * the end of RCU grace period.
  2153. */
  2154. rcu_read_lock();
  2155. seq_escape(m, btrfs_dev_name(fs_info->fs_devices->latest_dev), " \t\n\\");
  2156. rcu_read_unlock();
  2157. return 0;
  2158. }
  2159. static long btrfs_nr_cached_objects(struct super_block *sb, struct shrink_control *sc)
  2160. {
  2161. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2162. const s64 nr = percpu_counter_sum_positive(&fs_info->evictable_extent_maps);
  2163. trace_btrfs_extent_map_shrinker_count(fs_info, nr);
  2164. return nr;
  2165. }
  2166. static long btrfs_free_cached_objects(struct super_block *sb, struct shrink_control *sc)
  2167. {
  2168. const long nr_to_scan = min_t(unsigned long, LONG_MAX, sc->nr_to_scan);
  2169. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2170. btrfs_free_extent_maps(fs_info, nr_to_scan);
  2171. /* The extent map shrinker runs asynchronously, so always return 0. */
  2172. return 0;
  2173. }
  2174. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  2175. static int btrfs_remove_bdev(struct super_block *sb, struct block_device *bdev)
  2176. {
  2177. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2178. struct btrfs_device *device;
  2179. struct btrfs_dev_lookup_args lookup_args = { .devt = bdev->bd_dev };
  2180. bool can_rw;
  2181. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  2182. device = btrfs_find_device(fs_info->fs_devices, &lookup_args);
  2183. if (!device) {
  2184. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2185. /* Device not found, should not affect the running fs, just give a warning. */
  2186. btrfs_warn(fs_info, "unable to find btrfs device for block device '%pg'", bdev);
  2187. return 0;
  2188. }
  2189. /*
  2190. * The to-be-removed device is already missing?
  2191. *
  2192. * That's weird but no special handling needed and can exit right now.
  2193. */
  2194. if (unlikely(test_and_set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))) {
  2195. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2196. btrfs_warn(fs_info, "btrfs device id %llu is already missing", device->devid);
  2197. return 0;
  2198. }
  2199. device->fs_devices->missing_devices++;
  2200. if (test_and_clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  2201. list_del_init(&device->dev_alloc_list);
  2202. WARN_ON(device->fs_devices->rw_devices < 1);
  2203. device->fs_devices->rw_devices--;
  2204. }
  2205. can_rw = btrfs_check_rw_degradable(fs_info, device);
  2206. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  2207. /*
  2208. * Now device is considered missing, btrfs_device_name() won't give a
  2209. * meaningful result anymore, so only output the devid.
  2210. */
  2211. if (unlikely(!can_rw)) {
  2212. btrfs_crit(fs_info,
  2213. "btrfs device id %llu has gone missing, can not maintain read-write",
  2214. device->devid);
  2215. return -EIO;
  2216. }
  2217. btrfs_warn(fs_info,
  2218. "btrfs device id %llu has gone missing, continue as degraded",
  2219. device->devid);
  2220. btrfs_set_opt(fs_info->mount_opt, DEGRADED);
  2221. return 0;
  2222. }
  2223. static void btrfs_shutdown(struct super_block *sb)
  2224. {
  2225. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2226. btrfs_force_shutdown(fs_info);
  2227. }
  2228. #endif
  2229. static int btrfs_show_stats(struct seq_file *seq, struct dentry *root)
  2230. {
  2231. struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  2232. if (btrfs_is_zoned(fs_info)) {
  2233. btrfs_show_zoned_stats(fs_info, seq);
  2234. return 0;
  2235. }
  2236. return 0;
  2237. }
  2238. static const struct super_operations btrfs_super_ops = {
  2239. .drop_inode = btrfs_drop_inode,
  2240. .evict_inode = btrfs_evict_inode,
  2241. .put_super = btrfs_put_super,
  2242. .sync_fs = btrfs_sync_fs,
  2243. .show_options = btrfs_show_options,
  2244. .show_devname = btrfs_show_devname,
  2245. .alloc_inode = btrfs_alloc_inode,
  2246. .destroy_inode = btrfs_destroy_inode,
  2247. .free_inode = btrfs_free_inode,
  2248. .statfs = btrfs_statfs,
  2249. .freeze_fs = btrfs_freeze,
  2250. .unfreeze_fs = btrfs_unfreeze,
  2251. .nr_cached_objects = btrfs_nr_cached_objects,
  2252. .free_cached_objects = btrfs_free_cached_objects,
  2253. .show_stats = btrfs_show_stats,
  2254. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  2255. .remove_bdev = btrfs_remove_bdev,
  2256. .shutdown = btrfs_shutdown,
  2257. #endif
  2258. };
  2259. static const struct file_operations btrfs_ctl_fops = {
  2260. .open = btrfs_control_open,
  2261. .unlocked_ioctl = btrfs_control_ioctl,
  2262. .compat_ioctl = compat_ptr_ioctl,
  2263. .owner = THIS_MODULE,
  2264. .llseek = noop_llseek,
  2265. };
  2266. static struct miscdevice btrfs_misc = {
  2267. .minor = BTRFS_MINOR,
  2268. .name = "btrfs-control",
  2269. .fops = &btrfs_ctl_fops
  2270. };
  2271. MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  2272. MODULE_ALIAS("devname:btrfs-control");
  2273. static int __init btrfs_interface_init(void)
  2274. {
  2275. return misc_register(&btrfs_misc);
  2276. }
  2277. static __cold void btrfs_interface_exit(void)
  2278. {
  2279. misc_deregister(&btrfs_misc);
  2280. }
  2281. static int __init btrfs_print_mod_info(void)
  2282. {
  2283. static const char options[] = ""
  2284. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  2285. ", experimental=on"
  2286. #endif
  2287. #ifdef CONFIG_BTRFS_DEBUG
  2288. ", debug=on"
  2289. #endif
  2290. #ifdef CONFIG_BTRFS_ASSERT
  2291. ", assert=on"
  2292. #endif
  2293. #ifdef CONFIG_BLK_DEV_ZONED
  2294. ", zoned=yes"
  2295. #else
  2296. ", zoned=no"
  2297. #endif
  2298. #ifdef CONFIG_FS_VERITY
  2299. ", fsverity=yes"
  2300. #else
  2301. ", fsverity=no"
  2302. #endif
  2303. ;
  2304. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  2305. if (btrfs_get_mod_read_policy() == NULL)
  2306. pr_info("Btrfs loaded%s\n", options);
  2307. else
  2308. pr_info("Btrfs loaded%s, read_policy=%s\n",
  2309. options, btrfs_get_mod_read_policy());
  2310. #else
  2311. pr_info("Btrfs loaded%s\n", options);
  2312. #endif
  2313. return 0;
  2314. }
  2315. static int register_btrfs(void)
  2316. {
  2317. return register_filesystem(&btrfs_fs_type);
  2318. }
  2319. static void unregister_btrfs(void)
  2320. {
  2321. unregister_filesystem(&btrfs_fs_type);
  2322. }
  2323. /* Helper structure for long init/exit functions. */
  2324. struct init_sequence {
  2325. int (*init_func)(void);
  2326. /* Can be NULL if the init_func doesn't need cleanup. */
  2327. void (*exit_func)(void);
  2328. };
  2329. static const struct init_sequence mod_init_seq[] = {
  2330. {
  2331. .init_func = btrfs_props_init,
  2332. .exit_func = NULL,
  2333. }, {
  2334. .init_func = btrfs_init_sysfs,
  2335. .exit_func = btrfs_exit_sysfs,
  2336. }, {
  2337. .init_func = btrfs_init_compress,
  2338. .exit_func = btrfs_exit_compress,
  2339. }, {
  2340. .init_func = btrfs_init_cachep,
  2341. .exit_func = btrfs_destroy_cachep,
  2342. }, {
  2343. .init_func = btrfs_init_dio,
  2344. .exit_func = btrfs_destroy_dio,
  2345. }, {
  2346. .init_func = btrfs_transaction_init,
  2347. .exit_func = btrfs_transaction_exit,
  2348. }, {
  2349. .init_func = btrfs_ctree_init,
  2350. .exit_func = btrfs_ctree_exit,
  2351. }, {
  2352. .init_func = btrfs_free_space_init,
  2353. .exit_func = btrfs_free_space_exit,
  2354. }, {
  2355. .init_func = btrfs_extent_state_init_cachep,
  2356. .exit_func = btrfs_extent_state_free_cachep,
  2357. }, {
  2358. .init_func = extent_buffer_init_cachep,
  2359. .exit_func = extent_buffer_free_cachep,
  2360. }, {
  2361. .init_func = btrfs_bioset_init,
  2362. .exit_func = btrfs_bioset_exit,
  2363. }, {
  2364. .init_func = btrfs_extent_map_init,
  2365. .exit_func = btrfs_extent_map_exit,
  2366. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  2367. }, {
  2368. .init_func = btrfs_read_policy_init,
  2369. .exit_func = NULL,
  2370. #endif
  2371. }, {
  2372. .init_func = ordered_data_init,
  2373. .exit_func = ordered_data_exit,
  2374. }, {
  2375. .init_func = btrfs_delayed_inode_init,
  2376. .exit_func = btrfs_delayed_inode_exit,
  2377. }, {
  2378. .init_func = btrfs_auto_defrag_init,
  2379. .exit_func = btrfs_auto_defrag_exit,
  2380. }, {
  2381. .init_func = btrfs_delayed_ref_init,
  2382. .exit_func = btrfs_delayed_ref_exit,
  2383. }, {
  2384. .init_func = btrfs_prelim_ref_init,
  2385. .exit_func = btrfs_prelim_ref_exit,
  2386. }, {
  2387. .init_func = btrfs_interface_init,
  2388. .exit_func = btrfs_interface_exit,
  2389. }, {
  2390. .init_func = btrfs_print_mod_info,
  2391. .exit_func = NULL,
  2392. }, {
  2393. .init_func = btrfs_run_sanity_tests,
  2394. .exit_func = NULL,
  2395. }, {
  2396. .init_func = register_btrfs,
  2397. .exit_func = unregister_btrfs,
  2398. }
  2399. };
  2400. static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
  2401. static __always_inline void btrfs_exit_btrfs_fs(void)
  2402. {
  2403. int i;
  2404. for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
  2405. if (!mod_init_result[i])
  2406. continue;
  2407. if (mod_init_seq[i].exit_func)
  2408. mod_init_seq[i].exit_func();
  2409. mod_init_result[i] = false;
  2410. }
  2411. }
  2412. static void __exit exit_btrfs_fs(void)
  2413. {
  2414. btrfs_exit_btrfs_fs();
  2415. btrfs_cleanup_fs_uuids();
  2416. }
  2417. static int __init init_btrfs_fs(void)
  2418. {
  2419. int ret;
  2420. int i;
  2421. for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
  2422. ASSERT(!mod_init_result[i]);
  2423. ret = mod_init_seq[i].init_func();
  2424. if (ret < 0) {
  2425. btrfs_exit_btrfs_fs();
  2426. return ret;
  2427. }
  2428. mod_init_result[i] = true;
  2429. }
  2430. return 0;
  2431. }
  2432. late_initcall(init_btrfs_fs);
  2433. module_exit(exit_btrfs_fs)
  2434. MODULE_DESCRIPTION("B-Tree File System (BTRFS)");
  2435. MODULE_LICENSE("GPL");