xfs_sb.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_ialloc.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_error.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_buf_item.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_alloc_btree.h"
  22. #include "xfs_log.h"
  23. #include "xfs_rmap_btree.h"
  24. #include "xfs_refcount_btree.h"
  25. #include "xfs_da_format.h"
  26. #include "xfs_health.h"
  27. #include "xfs_ag.h"
  28. /*
  29. * Physical superblock buffer manipulations. Shared with libxfs in userspace.
  30. */
  31. /*
  32. * Check that all the V4 feature bits that the V5 filesystem format requires are
  33. * correctly set.
  34. */
  35. static bool
  36. xfs_sb_validate_v5_features(
  37. struct xfs_sb *sbp)
  38. {
  39. /* We must not have any unknown V4 feature bits set */
  40. if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
  41. return false;
  42. /*
  43. * The CRC bit is considered an invalid V4 flag, so we have to add it
  44. * manually to the OKBITS mask.
  45. */
  46. if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
  47. XFS_SB_VERSION2_CRCBIT))
  48. return false;
  49. /* Now check all the required V4 feature flags are set. */
  50. #define V5_VERS_FLAGS (XFS_SB_VERSION_NLINKBIT | \
  51. XFS_SB_VERSION_ALIGNBIT | \
  52. XFS_SB_VERSION_LOGV2BIT | \
  53. XFS_SB_VERSION_EXTFLGBIT | \
  54. XFS_SB_VERSION_DIRV2BIT | \
  55. XFS_SB_VERSION_MOREBITSBIT)
  56. #define V5_FEAT_FLAGS (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
  57. XFS_SB_VERSION2_ATTR2BIT | \
  58. XFS_SB_VERSION2_PROJID32BIT | \
  59. XFS_SB_VERSION2_CRCBIT)
  60. if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
  61. return false;
  62. if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
  63. return false;
  64. return true;
  65. }
  66. /*
  67. * We current support XFS v5 formats with known features and v4 superblocks with
  68. * at least V2 directories.
  69. */
  70. bool
  71. xfs_sb_good_version(
  72. struct xfs_sb *sbp)
  73. {
  74. /*
  75. * All v5 filesystems are supported, but we must check that all the
  76. * required v4 feature flags are enabled correctly as the code checks
  77. * those flags and not for v5 support.
  78. */
  79. if (xfs_sb_is_v5(sbp))
  80. return xfs_sb_validate_v5_features(sbp);
  81. /* versions prior to v4 are not supported */
  82. if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
  83. return false;
  84. /* We must not have any unknown v4 feature bits set */
  85. if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
  86. ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
  87. (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
  88. return false;
  89. /* V4 filesystems need v2 directories and unwritten extents */
  90. if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
  91. return false;
  92. if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
  93. return false;
  94. /* It's a supported v4 filesystem */
  95. return true;
  96. }
  97. uint64_t
  98. xfs_sb_version_to_features(
  99. struct xfs_sb *sbp)
  100. {
  101. uint64_t features = 0;
  102. /* optional V4 features */
  103. if (sbp->sb_rblocks > 0)
  104. features |= XFS_FEAT_REALTIME;
  105. if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
  106. features |= XFS_FEAT_NLINK;
  107. if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
  108. features |= XFS_FEAT_ATTR;
  109. if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
  110. features |= XFS_FEAT_QUOTA;
  111. if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
  112. features |= XFS_FEAT_ALIGN;
  113. if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
  114. features |= XFS_FEAT_LOGV2;
  115. if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
  116. features |= XFS_FEAT_DALIGN;
  117. if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
  118. features |= XFS_FEAT_EXTFLG;
  119. if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
  120. features |= XFS_FEAT_SECTOR;
  121. if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
  122. features |= XFS_FEAT_ASCIICI;
  123. if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
  124. if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
  125. features |= XFS_FEAT_LAZYSBCOUNT;
  126. if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
  127. features |= XFS_FEAT_ATTR2;
  128. if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
  129. features |= XFS_FEAT_PROJID32;
  130. if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
  131. features |= XFS_FEAT_FTYPE;
  132. }
  133. if (!xfs_sb_is_v5(sbp))
  134. return features;
  135. /* Always on V5 features */
  136. features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
  137. XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
  138. XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
  139. /* Optional V5 features */
  140. if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
  141. features |= XFS_FEAT_FINOBT;
  142. if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
  143. features |= XFS_FEAT_RMAPBT;
  144. if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
  145. features |= XFS_FEAT_REFLINK;
  146. if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
  147. features |= XFS_FEAT_INOBTCNT;
  148. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
  149. features |= XFS_FEAT_FTYPE;
  150. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
  151. features |= XFS_FEAT_SPINODES;
  152. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
  153. features |= XFS_FEAT_META_UUID;
  154. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
  155. features |= XFS_FEAT_BIGTIME;
  156. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
  157. features |= XFS_FEAT_NEEDSREPAIR;
  158. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
  159. features |= XFS_FEAT_NREXT64;
  160. return features;
  161. }
  162. /* Check all the superblock fields we care about when reading one in. */
  163. STATIC int
  164. xfs_validate_sb_read(
  165. struct xfs_mount *mp,
  166. struct xfs_sb *sbp)
  167. {
  168. if (!xfs_sb_is_v5(sbp))
  169. return 0;
  170. /*
  171. * Version 5 superblock feature mask validation. Reject combinations
  172. * the kernel cannot support up front before checking anything else.
  173. */
  174. if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
  175. xfs_warn(mp,
  176. "Superblock has unknown compatible features (0x%x) enabled.",
  177. (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
  178. xfs_warn(mp,
  179. "Using a more recent kernel is recommended.");
  180. }
  181. if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
  182. xfs_alert(mp,
  183. "Superblock has unknown read-only compatible features (0x%x) enabled.",
  184. (sbp->sb_features_ro_compat &
  185. XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
  186. if (!xfs_is_readonly(mp)) {
  187. xfs_warn(mp,
  188. "Attempted to mount read-only compatible filesystem read-write.");
  189. xfs_warn(mp,
  190. "Filesystem can only be safely mounted read only.");
  191. return -EINVAL;
  192. }
  193. }
  194. if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
  195. xfs_warn(mp,
  196. "Superblock has unknown incompatible features (0x%x) enabled.",
  197. (sbp->sb_features_incompat &
  198. XFS_SB_FEAT_INCOMPAT_UNKNOWN));
  199. xfs_warn(mp,
  200. "Filesystem cannot be safely mounted by this kernel.");
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. /* Check all the superblock fields we care about when writing one out. */
  206. STATIC int
  207. xfs_validate_sb_write(
  208. struct xfs_mount *mp,
  209. struct xfs_buf *bp,
  210. struct xfs_sb *sbp)
  211. {
  212. /*
  213. * Carry out additional sb summary counter sanity checks when we write
  214. * the superblock. We skip this in the read validator because there
  215. * could be newer superblocks in the log and if the values are garbage
  216. * even after replay we'll recalculate them at the end of log mount.
  217. *
  218. * mkfs has traditionally written zeroed counters to inprogress and
  219. * secondary superblocks, so allow this usage to continue because
  220. * we never read counters from such superblocks.
  221. */
  222. if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
  223. (sbp->sb_fdblocks > sbp->sb_dblocks ||
  224. !xfs_verify_icount(mp, sbp->sb_icount) ||
  225. sbp->sb_ifree > sbp->sb_icount)) {
  226. xfs_warn(mp, "SB summary counter sanity check failed");
  227. return -EFSCORRUPTED;
  228. }
  229. if (!xfs_sb_is_v5(sbp))
  230. return 0;
  231. /*
  232. * Version 5 superblock feature mask validation. Reject combinations
  233. * the kernel cannot support since we checked for unsupported bits in
  234. * the read verifier, which means that memory is corrupt.
  235. */
  236. if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
  237. xfs_warn(mp,
  238. "Corruption detected in superblock compatible features (0x%x)!",
  239. (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
  240. return -EFSCORRUPTED;
  241. }
  242. if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
  243. xfs_alert(mp,
  244. "Corruption detected in superblock read-only compatible features (0x%x)!",
  245. (sbp->sb_features_ro_compat &
  246. XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
  247. return -EFSCORRUPTED;
  248. }
  249. if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
  250. xfs_warn(mp,
  251. "Corruption detected in superblock incompatible features (0x%x)!",
  252. (sbp->sb_features_incompat &
  253. XFS_SB_FEAT_INCOMPAT_UNKNOWN));
  254. return -EFSCORRUPTED;
  255. }
  256. if (xfs_sb_has_incompat_log_feature(sbp,
  257. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  258. xfs_warn(mp,
  259. "Corruption detected in superblock incompatible log features (0x%x)!",
  260. (sbp->sb_features_log_incompat &
  261. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  262. return -EFSCORRUPTED;
  263. }
  264. /*
  265. * We can't read verify the sb LSN because the read verifier is called
  266. * before the log is allocated and processed. We know the log is set up
  267. * before write verifier calls, so check it here.
  268. */
  269. if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
  270. return -EFSCORRUPTED;
  271. return 0;
  272. }
  273. /* Check the validity of the SB. */
  274. STATIC int
  275. xfs_validate_sb_common(
  276. struct xfs_mount *mp,
  277. struct xfs_buf *bp,
  278. struct xfs_sb *sbp)
  279. {
  280. struct xfs_dsb *dsb = bp->b_addr;
  281. uint32_t agcount = 0;
  282. uint32_t rem;
  283. bool has_dalign;
  284. if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
  285. xfs_warn(mp,
  286. "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
  287. be32_to_cpu(dsb->sb_magicnum));
  288. return -EWRONGFS;
  289. }
  290. if (!xfs_sb_good_version(sbp)) {
  291. xfs_warn(mp,
  292. "Superblock has unknown features enabled or corrupted feature masks.");
  293. return -EWRONGFS;
  294. }
  295. /*
  296. * Validate feature flags and state
  297. */
  298. if (xfs_sb_is_v5(sbp)) {
  299. if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
  300. xfs_notice(mp,
  301. "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
  302. sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
  303. return -EFSCORRUPTED;
  304. }
  305. /* V5 has a separate project quota inode */
  306. if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
  307. xfs_notice(mp,
  308. "Version 5 of Super block has XFS_OQUOTA bits.");
  309. return -EFSCORRUPTED;
  310. }
  311. /*
  312. * Full inode chunks must be aligned to inode chunk size when
  313. * sparse inodes are enabled to support the sparse chunk
  314. * allocation algorithm and prevent overlapping inode records.
  315. */
  316. if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
  317. uint32_t align;
  318. align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
  319. >> sbp->sb_blocklog;
  320. if (sbp->sb_inoalignmt != align) {
  321. xfs_warn(mp,
  322. "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
  323. sbp->sb_inoalignmt, align);
  324. return -EINVAL;
  325. }
  326. }
  327. } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
  328. XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
  329. xfs_notice(mp,
  330. "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
  331. return -EFSCORRUPTED;
  332. }
  333. if (unlikely(
  334. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  335. xfs_warn(mp,
  336. "filesystem is marked as having an external log; "
  337. "specify logdev on the mount command line.");
  338. return -EINVAL;
  339. }
  340. if (unlikely(
  341. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  342. xfs_warn(mp,
  343. "filesystem is marked as having an internal log; "
  344. "do not specify logdev on the mount command line.");
  345. return -EINVAL;
  346. }
  347. /* Compute agcount for this number of dblocks and agblocks */
  348. if (sbp->sb_agblocks) {
  349. agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
  350. if (rem)
  351. agcount++;
  352. }
  353. /*
  354. * More sanity checking. Most of these were stolen directly from
  355. * xfs_repair.
  356. */
  357. if (unlikely(
  358. sbp->sb_agcount <= 0 ||
  359. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  360. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  361. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  362. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  363. sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
  364. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  365. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  366. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  367. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  368. sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
  369. sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  370. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  371. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  372. sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
  373. sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
  374. sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
  375. sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
  376. sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
  377. XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
  378. XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
  379. sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
  380. agcount == 0 || agcount != sbp->sb_agcount ||
  381. (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
  382. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  383. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  384. (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
  385. sbp->sb_dblocks == 0 ||
  386. sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
  387. sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
  388. sbp->sb_shared_vn != 0)) {
  389. xfs_notice(mp, "SB sanity check failed");
  390. return -EFSCORRUPTED;
  391. }
  392. /* Validate the realtime geometry; stolen from xfs_repair */
  393. if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
  394. sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
  395. xfs_notice(mp,
  396. "realtime extent sanity check failed");
  397. return -EFSCORRUPTED;
  398. }
  399. if (sbp->sb_rblocks == 0) {
  400. if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
  401. sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
  402. xfs_notice(mp,
  403. "realtime zeroed geometry check failed");
  404. return -EFSCORRUPTED;
  405. }
  406. } else {
  407. uint64_t rexts;
  408. uint64_t rbmblocks;
  409. rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
  410. rbmblocks = howmany_64(sbp->sb_rextents,
  411. NBBY * sbp->sb_blocksize);
  412. if (sbp->sb_rextents != rexts ||
  413. sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
  414. sbp->sb_rbmblocks != rbmblocks) {
  415. xfs_notice(mp,
  416. "realtime geometry sanity check failed");
  417. return -EFSCORRUPTED;
  418. }
  419. }
  420. /*
  421. * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
  422. * would imply the image is corrupted.
  423. */
  424. has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
  425. if (!!sbp->sb_unit ^ has_dalign) {
  426. xfs_notice(mp, "SB stripe alignment sanity check failed");
  427. return -EFSCORRUPTED;
  428. }
  429. if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
  430. XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
  431. return -EFSCORRUPTED;
  432. /*
  433. * Currently only very few inode sizes are supported.
  434. */
  435. switch (sbp->sb_inodesize) {
  436. case 256:
  437. case 512:
  438. case 1024:
  439. case 2048:
  440. break;
  441. default:
  442. xfs_warn(mp, "inode size of %d bytes not supported",
  443. sbp->sb_inodesize);
  444. return -ENOSYS;
  445. }
  446. return 0;
  447. }
  448. void
  449. xfs_sb_quota_from_disk(struct xfs_sb *sbp)
  450. {
  451. /*
  452. * older mkfs doesn't initialize quota inodes to NULLFSINO. This
  453. * leads to in-core values having two different values for a quota
  454. * inode to be invalid: 0 and NULLFSINO. Change it to a single value
  455. * NULLFSINO.
  456. *
  457. * Note that this change affect only the in-core values. These
  458. * values are not written back to disk unless any quota information
  459. * is written to the disk. Even in that case, sb_pquotino field is
  460. * not written to disk unless the superblock supports pquotino.
  461. */
  462. if (sbp->sb_uquotino == 0)
  463. sbp->sb_uquotino = NULLFSINO;
  464. if (sbp->sb_gquotino == 0)
  465. sbp->sb_gquotino = NULLFSINO;
  466. if (sbp->sb_pquotino == 0)
  467. sbp->sb_pquotino = NULLFSINO;
  468. /*
  469. * We need to do these manipilations only if we are working
  470. * with an older version of on-disk superblock.
  471. */
  472. if (xfs_sb_is_v5(sbp))
  473. return;
  474. if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
  475. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  476. XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
  477. if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
  478. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  479. XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
  480. sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
  481. if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
  482. sbp->sb_gquotino != NULLFSINO) {
  483. /*
  484. * In older version of superblock, on-disk superblock only
  485. * has sb_gquotino, and in-core superblock has both sb_gquotino
  486. * and sb_pquotino. But, only one of them is supported at any
  487. * point of time. So, if PQUOTA is set in disk superblock,
  488. * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
  489. * above is to make sure we don't do this twice and wipe them
  490. * both out!
  491. */
  492. sbp->sb_pquotino = sbp->sb_gquotino;
  493. sbp->sb_gquotino = NULLFSINO;
  494. }
  495. }
  496. static void
  497. __xfs_sb_from_disk(
  498. struct xfs_sb *to,
  499. struct xfs_dsb *from,
  500. bool convert_xquota)
  501. {
  502. to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
  503. to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
  504. to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
  505. to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
  506. to->sb_rextents = be64_to_cpu(from->sb_rextents);
  507. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  508. to->sb_logstart = be64_to_cpu(from->sb_logstart);
  509. to->sb_rootino = be64_to_cpu(from->sb_rootino);
  510. to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
  511. to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
  512. to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
  513. to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
  514. to->sb_agcount = be32_to_cpu(from->sb_agcount);
  515. to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
  516. to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
  517. to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
  518. to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
  519. to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
  520. to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
  521. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  522. to->sb_blocklog = from->sb_blocklog;
  523. to->sb_sectlog = from->sb_sectlog;
  524. to->sb_inodelog = from->sb_inodelog;
  525. to->sb_inopblog = from->sb_inopblog;
  526. to->sb_agblklog = from->sb_agblklog;
  527. to->sb_rextslog = from->sb_rextslog;
  528. to->sb_inprogress = from->sb_inprogress;
  529. to->sb_imax_pct = from->sb_imax_pct;
  530. to->sb_icount = be64_to_cpu(from->sb_icount);
  531. to->sb_ifree = be64_to_cpu(from->sb_ifree);
  532. to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
  533. to->sb_frextents = be64_to_cpu(from->sb_frextents);
  534. to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
  535. to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
  536. to->sb_qflags = be16_to_cpu(from->sb_qflags);
  537. to->sb_flags = from->sb_flags;
  538. to->sb_shared_vn = from->sb_shared_vn;
  539. to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
  540. to->sb_unit = be32_to_cpu(from->sb_unit);
  541. to->sb_width = be32_to_cpu(from->sb_width);
  542. to->sb_dirblklog = from->sb_dirblklog;
  543. to->sb_logsectlog = from->sb_logsectlog;
  544. to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
  545. to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
  546. to->sb_features2 = be32_to_cpu(from->sb_features2);
  547. to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
  548. to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
  549. to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
  550. to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
  551. to->sb_features_log_incompat =
  552. be32_to_cpu(from->sb_features_log_incompat);
  553. /* crc is only used on disk, not in memory; just init to 0 here. */
  554. to->sb_crc = 0;
  555. to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
  556. to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
  557. to->sb_lsn = be64_to_cpu(from->sb_lsn);
  558. /*
  559. * sb_meta_uuid is only on disk if it differs from sb_uuid and the
  560. * feature flag is set; if not set we keep it only in memory.
  561. */
  562. if (xfs_sb_is_v5(to) &&
  563. (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
  564. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  565. else
  566. uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
  567. /* Convert on-disk flags to in-memory flags? */
  568. if (convert_xquota)
  569. xfs_sb_quota_from_disk(to);
  570. }
  571. void
  572. xfs_sb_from_disk(
  573. struct xfs_sb *to,
  574. struct xfs_dsb *from)
  575. {
  576. __xfs_sb_from_disk(to, from, true);
  577. }
  578. static void
  579. xfs_sb_quota_to_disk(
  580. struct xfs_dsb *to,
  581. struct xfs_sb *from)
  582. {
  583. uint16_t qflags = from->sb_qflags;
  584. to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
  585. /*
  586. * The in-memory superblock quota state matches the v5 on-disk format so
  587. * just write them out and return
  588. */
  589. if (xfs_sb_is_v5(from)) {
  590. to->sb_qflags = cpu_to_be16(from->sb_qflags);
  591. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  592. to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
  593. return;
  594. }
  595. /*
  596. * For older superblocks (v4), the in-core version of sb_qflags do not
  597. * have XFS_OQUOTA_* flags, whereas the on-disk version does. So,
  598. * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
  599. */
  600. qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
  601. XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
  602. if (from->sb_qflags &
  603. (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
  604. qflags |= XFS_OQUOTA_ENFD;
  605. if (from->sb_qflags &
  606. (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
  607. qflags |= XFS_OQUOTA_CHKD;
  608. to->sb_qflags = cpu_to_be16(qflags);
  609. /*
  610. * GQUOTINO and PQUOTINO cannot be used together in versions
  611. * of superblock that do not have pquotino. from->sb_flags
  612. * tells us which quota is active and should be copied to
  613. * disk. If neither are active, we should NULL the inode.
  614. *
  615. * In all cases, the separate pquotino must remain 0 because it
  616. * is beyond the "end" of the valid non-pquotino superblock.
  617. */
  618. if (from->sb_qflags & XFS_GQUOTA_ACCT)
  619. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  620. else if (from->sb_qflags & XFS_PQUOTA_ACCT)
  621. to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
  622. else {
  623. /*
  624. * We can't rely on just the fields being logged to tell us
  625. * that it is safe to write NULLFSINO - we should only do that
  626. * if quotas are not actually enabled. Hence only write
  627. * NULLFSINO if both in-core quota inodes are NULL.
  628. */
  629. if (from->sb_gquotino == NULLFSINO &&
  630. from->sb_pquotino == NULLFSINO)
  631. to->sb_gquotino = cpu_to_be64(NULLFSINO);
  632. }
  633. to->sb_pquotino = 0;
  634. }
  635. void
  636. xfs_sb_to_disk(
  637. struct xfs_dsb *to,
  638. struct xfs_sb *from)
  639. {
  640. xfs_sb_quota_to_disk(to, from);
  641. to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
  642. to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
  643. to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
  644. to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
  645. to->sb_rextents = cpu_to_be64(from->sb_rextents);
  646. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  647. to->sb_logstart = cpu_to_be64(from->sb_logstart);
  648. to->sb_rootino = cpu_to_be64(from->sb_rootino);
  649. to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
  650. to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
  651. to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
  652. to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
  653. to->sb_agcount = cpu_to_be32(from->sb_agcount);
  654. to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
  655. to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
  656. to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
  657. to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
  658. to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
  659. to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
  660. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  661. to->sb_blocklog = from->sb_blocklog;
  662. to->sb_sectlog = from->sb_sectlog;
  663. to->sb_inodelog = from->sb_inodelog;
  664. to->sb_inopblog = from->sb_inopblog;
  665. to->sb_agblklog = from->sb_agblklog;
  666. to->sb_rextslog = from->sb_rextslog;
  667. to->sb_inprogress = from->sb_inprogress;
  668. to->sb_imax_pct = from->sb_imax_pct;
  669. to->sb_icount = cpu_to_be64(from->sb_icount);
  670. to->sb_ifree = cpu_to_be64(from->sb_ifree);
  671. to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
  672. to->sb_frextents = cpu_to_be64(from->sb_frextents);
  673. to->sb_flags = from->sb_flags;
  674. to->sb_shared_vn = from->sb_shared_vn;
  675. to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
  676. to->sb_unit = cpu_to_be32(from->sb_unit);
  677. to->sb_width = cpu_to_be32(from->sb_width);
  678. to->sb_dirblklog = from->sb_dirblklog;
  679. to->sb_logsectlog = from->sb_logsectlog;
  680. to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
  681. to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
  682. /*
  683. * We need to ensure that bad_features2 always matches features2.
  684. * Hence we enforce that here rather than having to remember to do it
  685. * everywhere else that updates features2.
  686. */
  687. from->sb_bad_features2 = from->sb_features2;
  688. to->sb_features2 = cpu_to_be32(from->sb_features2);
  689. to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
  690. if (!xfs_sb_is_v5(from))
  691. return;
  692. to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
  693. to->sb_features_ro_compat =
  694. cpu_to_be32(from->sb_features_ro_compat);
  695. to->sb_features_incompat =
  696. cpu_to_be32(from->sb_features_incompat);
  697. to->sb_features_log_incompat =
  698. cpu_to_be32(from->sb_features_log_incompat);
  699. to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
  700. to->sb_lsn = cpu_to_be64(from->sb_lsn);
  701. if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
  702. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  703. }
  704. /*
  705. * If the superblock has the CRC feature bit set or the CRC field is non-null,
  706. * check that the CRC is valid. We check the CRC field is non-null because a
  707. * single bit error could clear the feature bit and unused parts of the
  708. * superblock are supposed to be zero. Hence a non-null crc field indicates that
  709. * we've potentially lost a feature bit and we should check it anyway.
  710. *
  711. * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
  712. * last field in V4 secondary superblocks. So for secondary superblocks,
  713. * we are more forgiving, and ignore CRC failures if the primary doesn't
  714. * indicate that the fs version is V5.
  715. */
  716. static void
  717. xfs_sb_read_verify(
  718. struct xfs_buf *bp)
  719. {
  720. struct xfs_sb sb;
  721. struct xfs_mount *mp = bp->b_mount;
  722. struct xfs_dsb *dsb = bp->b_addr;
  723. int error;
  724. /*
  725. * open code the version check to avoid needing to convert the entire
  726. * superblock from disk order just to check the version number
  727. */
  728. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
  729. (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
  730. XFS_SB_VERSION_5) ||
  731. dsb->sb_crc != 0)) {
  732. if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
  733. /* Only fail bad secondaries on a known V5 filesystem */
  734. if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
  735. xfs_has_crc(mp)) {
  736. error = -EFSBADCRC;
  737. goto out_error;
  738. }
  739. }
  740. }
  741. /*
  742. * Check all the superblock fields. Don't byteswap the xquota flags
  743. * because _verify_common checks the on-disk values.
  744. */
  745. __xfs_sb_from_disk(&sb, dsb, false);
  746. error = xfs_validate_sb_common(mp, bp, &sb);
  747. if (error)
  748. goto out_error;
  749. error = xfs_validate_sb_read(mp, &sb);
  750. out_error:
  751. if (error == -EFSCORRUPTED || error == -EFSBADCRC)
  752. xfs_verifier_error(bp, error, __this_address);
  753. else if (error)
  754. xfs_buf_ioerror(bp, error);
  755. }
  756. /*
  757. * We may be probed for a filesystem match, so we may not want to emit
  758. * messages when the superblock buffer is not actually an XFS superblock.
  759. * If we find an XFS superblock, then run a normal, noisy mount because we are
  760. * really going to mount it and want to know about errors.
  761. */
  762. static void
  763. xfs_sb_quiet_read_verify(
  764. struct xfs_buf *bp)
  765. {
  766. struct xfs_dsb *dsb = bp->b_addr;
  767. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
  768. /* XFS filesystem, verify noisily! */
  769. xfs_sb_read_verify(bp);
  770. return;
  771. }
  772. /* quietly fail */
  773. xfs_buf_ioerror(bp, -EWRONGFS);
  774. }
  775. static void
  776. xfs_sb_write_verify(
  777. struct xfs_buf *bp)
  778. {
  779. struct xfs_sb sb;
  780. struct xfs_mount *mp = bp->b_mount;
  781. struct xfs_buf_log_item *bip = bp->b_log_item;
  782. struct xfs_dsb *dsb = bp->b_addr;
  783. int error;
  784. /*
  785. * Check all the superblock fields. Don't byteswap the xquota flags
  786. * because _verify_common checks the on-disk values.
  787. */
  788. __xfs_sb_from_disk(&sb, dsb, false);
  789. error = xfs_validate_sb_common(mp, bp, &sb);
  790. if (error)
  791. goto out_error;
  792. error = xfs_validate_sb_write(mp, bp, &sb);
  793. if (error)
  794. goto out_error;
  795. if (!xfs_sb_is_v5(&sb))
  796. return;
  797. if (bip)
  798. dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  799. xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
  800. return;
  801. out_error:
  802. xfs_verifier_error(bp, error, __this_address);
  803. }
  804. const struct xfs_buf_ops xfs_sb_buf_ops = {
  805. .name = "xfs_sb",
  806. .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
  807. .verify_read = xfs_sb_read_verify,
  808. .verify_write = xfs_sb_write_verify,
  809. };
  810. const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
  811. .name = "xfs_sb_quiet",
  812. .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
  813. .verify_read = xfs_sb_quiet_read_verify,
  814. .verify_write = xfs_sb_write_verify,
  815. };
  816. /*
  817. * xfs_mount_common
  818. *
  819. * Mount initialization code establishing various mount
  820. * fields from the superblock associated with the given
  821. * mount structure.
  822. *
  823. * Inode geometry are calculated in xfs_ialloc_setup_geometry.
  824. */
  825. void
  826. xfs_sb_mount_common(
  827. struct xfs_mount *mp,
  828. struct xfs_sb *sbp)
  829. {
  830. mp->m_agfrotor = mp->m_agirotor = 0;
  831. mp->m_maxagi = mp->m_sb.sb_agcount;
  832. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  833. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  834. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  835. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  836. mp->m_blockmask = sbp->sb_blocksize - 1;
  837. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  838. mp->m_blockwmask = mp->m_blockwsize - 1;
  839. mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
  840. mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
  841. mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
  842. mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
  843. mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
  844. mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
  845. mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
  846. mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
  847. mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
  848. mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
  849. mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
  850. mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
  851. mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
  852. mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
  853. mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
  854. mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
  855. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  856. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  857. mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
  858. }
  859. /*
  860. * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
  861. * into the superblock buffer to be logged. It does not provide the higher
  862. * level of locking that is needed to protect the in-core superblock from
  863. * concurrent access.
  864. */
  865. void
  866. xfs_log_sb(
  867. struct xfs_trans *tp)
  868. {
  869. struct xfs_mount *mp = tp->t_mountp;
  870. struct xfs_buf *bp = xfs_trans_getsb(tp);
  871. /*
  872. * Lazy sb counters don't update the in-core superblock so do that now.
  873. * If this is at unmount, the counters will be exactly correct, but at
  874. * any other time they will only be ballpark correct because of
  875. * reservations that have been taken out percpu counters. If we have an
  876. * unclean shutdown, this will be corrected by log recovery rebuilding
  877. * the counters from the AGF block counts.
  878. *
  879. * Do not update sb_frextents here because it is not part of the lazy
  880. * sb counters, despite having a percpu counter. It is always kept
  881. * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas()
  882. * and hence we don't need have to update it here.
  883. */
  884. if (xfs_has_lazysbcount(mp)) {
  885. mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
  886. mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
  887. mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
  888. }
  889. xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
  890. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  891. xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
  892. }
  893. /*
  894. * xfs_sync_sb
  895. *
  896. * Sync the superblock to disk.
  897. *
  898. * Note that the caller is responsible for checking the frozen state of the
  899. * filesystem. This procedure uses the non-blocking transaction allocator and
  900. * thus will allow modifications to a frozen fs. This is required because this
  901. * code can be called during the process of freezing where use of the high-level
  902. * allocator would deadlock.
  903. */
  904. int
  905. xfs_sync_sb(
  906. struct xfs_mount *mp,
  907. bool wait)
  908. {
  909. struct xfs_trans *tp;
  910. int error;
  911. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
  912. XFS_TRANS_NO_WRITECOUNT, &tp);
  913. if (error)
  914. return error;
  915. xfs_log_sb(tp);
  916. if (wait)
  917. xfs_trans_set_sync(tp);
  918. return xfs_trans_commit(tp);
  919. }
  920. /*
  921. * Update all the secondary superblocks to match the new state of the primary.
  922. * Because we are completely overwriting all the existing fields in the
  923. * secondary superblock buffers, there is no need to read them in from disk.
  924. * Just get a new buffer, stamp it and write it.
  925. *
  926. * The sb buffers need to be cached here so that we serialise against other
  927. * operations that access the secondary superblocks, but we don't want to keep
  928. * them in memory once it is written so we mark it as a one-shot buffer.
  929. */
  930. int
  931. xfs_update_secondary_sbs(
  932. struct xfs_mount *mp)
  933. {
  934. struct xfs_perag *pag;
  935. xfs_agnumber_t agno = 1;
  936. int saved_error = 0;
  937. int error = 0;
  938. LIST_HEAD (buffer_list);
  939. /* update secondary superblocks. */
  940. for_each_perag_from(mp, agno, pag) {
  941. struct xfs_buf *bp;
  942. error = xfs_buf_get(mp->m_ddev_targp,
  943. XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
  944. XFS_FSS_TO_BB(mp, 1), &bp);
  945. /*
  946. * If we get an error reading or writing alternate superblocks,
  947. * continue. xfs_repair chooses the "best" superblock based
  948. * on most matches; if we break early, we'll leave more
  949. * superblocks un-updated than updated, and xfs_repair may
  950. * pick them over the properly-updated primary.
  951. */
  952. if (error) {
  953. xfs_warn(mp,
  954. "error allocating secondary superblock for ag %d",
  955. pag->pag_agno);
  956. if (!saved_error)
  957. saved_error = error;
  958. continue;
  959. }
  960. bp->b_ops = &xfs_sb_buf_ops;
  961. xfs_buf_oneshot(bp);
  962. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  963. xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
  964. xfs_buf_delwri_queue(bp, &buffer_list);
  965. xfs_buf_relse(bp);
  966. /* don't hold too many buffers at once */
  967. if (agno % 16)
  968. continue;
  969. error = xfs_buf_delwri_submit(&buffer_list);
  970. if (error) {
  971. xfs_warn(mp,
  972. "write error %d updating a secondary superblock near ag %d",
  973. error, pag->pag_agno);
  974. if (!saved_error)
  975. saved_error = error;
  976. continue;
  977. }
  978. }
  979. error = xfs_buf_delwri_submit(&buffer_list);
  980. if (error) {
  981. xfs_warn(mp,
  982. "write error %d updating a secondary superblock near ag %d",
  983. error, agno);
  984. }
  985. return saved_error ? saved_error : error;
  986. }
  987. /*
  988. * Same behavior as xfs_sync_sb, except that it is always synchronous and it
  989. * also writes the superblock buffer to disk sector 0 immediately.
  990. */
  991. int
  992. xfs_sync_sb_buf(
  993. struct xfs_mount *mp)
  994. {
  995. struct xfs_trans *tp;
  996. struct xfs_buf *bp;
  997. int error;
  998. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
  999. if (error)
  1000. return error;
  1001. bp = xfs_trans_getsb(tp);
  1002. xfs_log_sb(tp);
  1003. xfs_trans_bhold(tp, bp);
  1004. xfs_trans_set_sync(tp);
  1005. error = xfs_trans_commit(tp);
  1006. if (error)
  1007. goto out;
  1008. /*
  1009. * write out the sb buffer to get the changes to disk
  1010. */
  1011. error = xfs_bwrite(bp);
  1012. out:
  1013. xfs_buf_relse(bp);
  1014. return error;
  1015. }
  1016. void
  1017. xfs_fs_geometry(
  1018. struct xfs_mount *mp,
  1019. struct xfs_fsop_geom *geo,
  1020. int struct_version)
  1021. {
  1022. struct xfs_sb *sbp = &mp->m_sb;
  1023. memset(geo, 0, sizeof(struct xfs_fsop_geom));
  1024. geo->blocksize = sbp->sb_blocksize;
  1025. geo->rtextsize = sbp->sb_rextsize;
  1026. geo->agblocks = sbp->sb_agblocks;
  1027. geo->agcount = sbp->sb_agcount;
  1028. geo->logblocks = sbp->sb_logblocks;
  1029. geo->sectsize = sbp->sb_sectsize;
  1030. geo->inodesize = sbp->sb_inodesize;
  1031. geo->imaxpct = sbp->sb_imax_pct;
  1032. geo->datablocks = sbp->sb_dblocks;
  1033. geo->rtblocks = sbp->sb_rblocks;
  1034. geo->rtextents = sbp->sb_rextents;
  1035. geo->logstart = sbp->sb_logstart;
  1036. BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
  1037. memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
  1038. if (struct_version < 2)
  1039. return;
  1040. geo->sunit = sbp->sb_unit;
  1041. geo->swidth = sbp->sb_width;
  1042. if (struct_version < 3)
  1043. return;
  1044. geo->version = XFS_FSOP_GEOM_VERSION;
  1045. geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
  1046. XFS_FSOP_GEOM_FLAGS_DIRV2 |
  1047. XFS_FSOP_GEOM_FLAGS_EXTFLG;
  1048. if (xfs_has_attr(mp))
  1049. geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
  1050. if (xfs_has_quota(mp))
  1051. geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
  1052. if (xfs_has_align(mp))
  1053. geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
  1054. if (xfs_has_dalign(mp))
  1055. geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
  1056. if (xfs_has_asciici(mp))
  1057. geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
  1058. if (xfs_has_lazysbcount(mp))
  1059. geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
  1060. if (xfs_has_attr2(mp))
  1061. geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
  1062. if (xfs_has_projid32(mp))
  1063. geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
  1064. if (xfs_has_crc(mp))
  1065. geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
  1066. if (xfs_has_ftype(mp))
  1067. geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
  1068. if (xfs_has_finobt(mp))
  1069. geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
  1070. if (xfs_has_sparseinodes(mp))
  1071. geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
  1072. if (xfs_has_rmapbt(mp))
  1073. geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
  1074. if (xfs_has_reflink(mp))
  1075. geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
  1076. if (xfs_has_bigtime(mp))
  1077. geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
  1078. if (xfs_has_inobtcounts(mp))
  1079. geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
  1080. if (xfs_has_sector(mp)) {
  1081. geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
  1082. geo->logsectsize = sbp->sb_logsectsize;
  1083. } else {
  1084. geo->logsectsize = BBSIZE;
  1085. }
  1086. if (xfs_has_large_extent_counts(mp))
  1087. geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
  1088. geo->rtsectsize = sbp->sb_blocksize;
  1089. geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
  1090. if (struct_version < 4)
  1091. return;
  1092. if (xfs_has_logv2(mp))
  1093. geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
  1094. geo->logsunit = sbp->sb_logsunit;
  1095. if (struct_version < 5)
  1096. return;
  1097. geo->version = XFS_FSOP_GEOM_VERSION_V5;
  1098. }
  1099. /* Read a secondary superblock. */
  1100. int
  1101. xfs_sb_read_secondary(
  1102. struct xfs_mount *mp,
  1103. struct xfs_trans *tp,
  1104. xfs_agnumber_t agno,
  1105. struct xfs_buf **bpp)
  1106. {
  1107. struct xfs_buf *bp;
  1108. int error;
  1109. ASSERT(agno != 0 && agno != NULLAGNUMBER);
  1110. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  1111. XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  1112. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
  1113. if (error)
  1114. return error;
  1115. xfs_buf_set_ref(bp, XFS_SSB_REF);
  1116. *bpp = bp;
  1117. return 0;
  1118. }
  1119. /* Get an uninitialised secondary superblock buffer. */
  1120. int
  1121. xfs_sb_get_secondary(
  1122. struct xfs_mount *mp,
  1123. struct xfs_trans *tp,
  1124. xfs_agnumber_t agno,
  1125. struct xfs_buf **bpp)
  1126. {
  1127. struct xfs_buf *bp;
  1128. int error;
  1129. ASSERT(agno != 0 && agno != NULLAGNUMBER);
  1130. error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1131. XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  1132. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  1133. if (error)
  1134. return error;
  1135. bp->b_ops = &xfs_sb_buf_ops;
  1136. xfs_buf_oneshot(bp);
  1137. *bpp = bp;
  1138. return 0;
  1139. }
  1140. /*
  1141. * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
  1142. * so users won't be confused by values in error messages.
  1143. */
  1144. bool
  1145. xfs_validate_stripe_geometry(
  1146. struct xfs_mount *mp,
  1147. __s64 sunit,
  1148. __s64 swidth,
  1149. int sectorsize,
  1150. bool silent)
  1151. {
  1152. if (swidth > INT_MAX) {
  1153. if (!silent)
  1154. xfs_notice(mp,
  1155. "stripe width (%lld) is too large", swidth);
  1156. return false;
  1157. }
  1158. if (sunit > swidth) {
  1159. if (!silent)
  1160. xfs_notice(mp,
  1161. "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
  1162. return false;
  1163. }
  1164. if (sectorsize && (int)sunit % sectorsize) {
  1165. if (!silent)
  1166. xfs_notice(mp,
  1167. "stripe unit (%lld) must be a multiple of the sector size (%d)",
  1168. sunit, sectorsize);
  1169. return false;
  1170. }
  1171. if (sunit && !swidth) {
  1172. if (!silent)
  1173. xfs_notice(mp,
  1174. "invalid stripe unit (%lld) and stripe width of 0", sunit);
  1175. return false;
  1176. }
  1177. if (!sunit && swidth) {
  1178. if (!silent)
  1179. xfs_notice(mp,
  1180. "invalid stripe width (%lld) and stripe unit of 0", swidth);
  1181. return false;
  1182. }
  1183. if (sunit && (int)swidth % (int)sunit) {
  1184. if (!silent)
  1185. xfs_notice(mp,
  1186. "stripe width (%lld) must be a multiple of the stripe unit (%lld)",
  1187. swidth, sunit);
  1188. return false;
  1189. }
  1190. return true;
  1191. }