super.c 76 KB

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