xfs_sb.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_ialloc.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_error.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_cksum.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_buf_item.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_alloc_btree.h"
  26. #include "xfs_ialloc_btree.h"
  27. #include "xfs_log.h"
  28. #include "xfs_rmap_btree.h"
  29. #include "xfs_bmap.h"
  30. #include "xfs_refcount_btree.h"
  31. #include "xfs_da_format.h"
  32. #include "xfs_da_btree.h"
  33. /*
  34. * Physical superblock buffer manipulations. Shared with libxfs in userspace.
  35. */
  36. /*
  37. * Reference counting access wrappers to the perag structures.
  38. * Because we never free per-ag structures, the only thing we
  39. * have to protect against changes is the tree structure itself.
  40. */
  41. struct xfs_perag *
  42. xfs_perag_get(
  43. struct xfs_mount *mp,
  44. xfs_agnumber_t agno)
  45. {
  46. struct xfs_perag *pag;
  47. int ref = 0;
  48. rcu_read_lock();
  49. pag = radix_tree_lookup(&mp->m_perag_tree, agno);
  50. if (pag) {
  51. ASSERT(atomic_read(&pag->pag_ref) >= 0);
  52. ref = atomic_inc_return(&pag->pag_ref);
  53. }
  54. rcu_read_unlock();
  55. trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
  56. return pag;
  57. }
  58. /*
  59. * search from @first to find the next perag with the given tag set.
  60. */
  61. struct xfs_perag *
  62. xfs_perag_get_tag(
  63. struct xfs_mount *mp,
  64. xfs_agnumber_t first,
  65. int tag)
  66. {
  67. struct xfs_perag *pag;
  68. int found;
  69. int ref;
  70. rcu_read_lock();
  71. found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
  72. (void **)&pag, first, 1, tag);
  73. if (found <= 0) {
  74. rcu_read_unlock();
  75. return NULL;
  76. }
  77. ref = atomic_inc_return(&pag->pag_ref);
  78. rcu_read_unlock();
  79. trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
  80. return pag;
  81. }
  82. void
  83. xfs_perag_put(
  84. struct xfs_perag *pag)
  85. {
  86. int ref;
  87. ASSERT(atomic_read(&pag->pag_ref) > 0);
  88. ref = atomic_dec_return(&pag->pag_ref);
  89. trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
  90. }
  91. /*
  92. * Check the validity of the SB found.
  93. */
  94. STATIC int
  95. xfs_mount_validate_sb(
  96. xfs_mount_t *mp,
  97. xfs_sb_t *sbp,
  98. bool check_inprogress,
  99. bool check_version)
  100. {
  101. uint32_t agcount = 0;
  102. uint32_t rem;
  103. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  104. xfs_warn(mp, "bad magic number");
  105. return -EWRONGFS;
  106. }
  107. if (!xfs_sb_good_version(sbp)) {
  108. xfs_warn(mp, "bad version");
  109. return -EWRONGFS;
  110. }
  111. /*
  112. * Version 5 superblock feature mask validation. Reject combinations the
  113. * kernel cannot support up front before checking anything else. For
  114. * write validation, we don't need to check feature masks.
  115. */
  116. if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
  117. if (xfs_sb_has_compat_feature(sbp,
  118. XFS_SB_FEAT_COMPAT_UNKNOWN)) {
  119. xfs_warn(mp,
  120. "Superblock has unknown compatible features (0x%x) enabled.",
  121. (sbp->sb_features_compat &
  122. XFS_SB_FEAT_COMPAT_UNKNOWN));
  123. xfs_warn(mp,
  124. "Using a more recent kernel is recommended.");
  125. }
  126. if (xfs_sb_has_ro_compat_feature(sbp,
  127. XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
  128. xfs_alert(mp,
  129. "Superblock has unknown read-only compatible features (0x%x) enabled.",
  130. (sbp->sb_features_ro_compat &
  131. XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
  132. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  133. xfs_warn(mp,
  134. "Attempted to mount read-only compatible filesystem read-write.");
  135. xfs_warn(mp,
  136. "Filesystem can only be safely mounted read only.");
  137. return -EINVAL;
  138. }
  139. }
  140. if (xfs_sb_has_incompat_feature(sbp,
  141. XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
  142. xfs_warn(mp,
  143. "Superblock has unknown incompatible features (0x%x) enabled.",
  144. (sbp->sb_features_incompat &
  145. XFS_SB_FEAT_INCOMPAT_UNKNOWN));
  146. xfs_warn(mp,
  147. "Filesystem can not be safely mounted by this kernel.");
  148. return -EINVAL;
  149. }
  150. } else if (xfs_sb_version_hascrc(sbp)) {
  151. /*
  152. * We can't read verify the sb LSN because the read verifier is
  153. * called before the log is allocated and processed. We know the
  154. * log is set up before write verifier (!check_version) calls,
  155. * so just check it here.
  156. */
  157. if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
  158. return -EFSCORRUPTED;
  159. }
  160. if (xfs_sb_version_has_pquotino(sbp)) {
  161. if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
  162. xfs_notice(mp,
  163. "Version 5 of Super block has XFS_OQUOTA bits.");
  164. return -EFSCORRUPTED;
  165. }
  166. } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
  167. XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
  168. xfs_notice(mp,
  169. "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
  170. return -EFSCORRUPTED;
  171. }
  172. /*
  173. * Full inode chunks must be aligned to inode chunk size when
  174. * sparse inodes are enabled to support the sparse chunk
  175. * allocation algorithm and prevent overlapping inode records.
  176. */
  177. if (xfs_sb_version_hassparseinodes(sbp)) {
  178. uint32_t align;
  179. align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
  180. >> sbp->sb_blocklog;
  181. if (sbp->sb_inoalignmt != align) {
  182. xfs_warn(mp,
  183. "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
  184. sbp->sb_inoalignmt, align);
  185. return -EINVAL;
  186. }
  187. }
  188. if (unlikely(
  189. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  190. xfs_warn(mp,
  191. "filesystem is marked as having an external log; "
  192. "specify logdev on the mount command line.");
  193. return -EINVAL;
  194. }
  195. if (unlikely(
  196. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  197. xfs_warn(mp,
  198. "filesystem is marked as having an internal log; "
  199. "do not specify logdev on the mount command line.");
  200. return -EINVAL;
  201. }
  202. /* Compute agcount for this number of dblocks and agblocks */
  203. if (sbp->sb_agblocks) {
  204. agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
  205. if (rem)
  206. agcount++;
  207. }
  208. /*
  209. * More sanity checking. Most of these were stolen directly from
  210. * xfs_repair.
  211. */
  212. if (unlikely(
  213. sbp->sb_agcount <= 0 ||
  214. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  215. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  216. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  217. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  218. sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
  219. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  220. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  221. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  222. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  223. sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
  224. sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  225. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  226. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  227. sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
  228. sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
  229. sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
  230. sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
  231. sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
  232. XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
  233. XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
  234. sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
  235. agcount == 0 || agcount != sbp->sb_agcount ||
  236. (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
  237. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  238. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  239. (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
  240. sbp->sb_dblocks == 0 ||
  241. sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
  242. sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
  243. sbp->sb_shared_vn != 0)) {
  244. xfs_notice(mp, "SB sanity check failed");
  245. return -EFSCORRUPTED;
  246. }
  247. if (sbp->sb_unit) {
  248. if (!xfs_sb_version_hasdalign(sbp) ||
  249. sbp->sb_unit > sbp->sb_width ||
  250. (sbp->sb_width % sbp->sb_unit) != 0) {
  251. xfs_notice(mp, "SB stripe unit sanity check failed");
  252. return -EFSCORRUPTED;
  253. }
  254. } else if (xfs_sb_version_hasdalign(sbp)) {
  255. xfs_notice(mp, "SB stripe alignment sanity check failed");
  256. return -EFSCORRUPTED;
  257. } else if (sbp->sb_width) {
  258. xfs_notice(mp, "SB stripe width sanity check failed");
  259. return -EFSCORRUPTED;
  260. }
  261. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  262. sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
  263. xfs_notice(mp, "v5 SB sanity check failed");
  264. return -EFSCORRUPTED;
  265. }
  266. /*
  267. * Until this is fixed only page-sized or smaller data blocks work.
  268. */
  269. if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
  270. xfs_warn(mp,
  271. "File system with blocksize %d bytes. "
  272. "Only pagesize (%ld) or less will currently work.",
  273. sbp->sb_blocksize, PAGE_SIZE);
  274. return -ENOSYS;
  275. }
  276. /*
  277. * Currently only very few inode sizes are supported.
  278. */
  279. switch (sbp->sb_inodesize) {
  280. case 256:
  281. case 512:
  282. case 1024:
  283. case 2048:
  284. break;
  285. default:
  286. xfs_warn(mp, "inode size of %d bytes not supported",
  287. sbp->sb_inodesize);
  288. return -ENOSYS;
  289. }
  290. if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
  291. xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
  292. xfs_warn(mp,
  293. "file system too large to be mounted on this system.");
  294. return -EFBIG;
  295. }
  296. if (check_inprogress && sbp->sb_inprogress) {
  297. xfs_warn(mp, "Offline file system operation in progress!");
  298. return -EFSCORRUPTED;
  299. }
  300. return 0;
  301. }
  302. void
  303. xfs_sb_quota_from_disk(struct xfs_sb *sbp)
  304. {
  305. /*
  306. * older mkfs doesn't initialize quota inodes to NULLFSINO. This
  307. * leads to in-core values having two different values for a quota
  308. * inode to be invalid: 0 and NULLFSINO. Change it to a single value
  309. * NULLFSINO.
  310. *
  311. * Note that this change affect only the in-core values. These
  312. * values are not written back to disk unless any quota information
  313. * is written to the disk. Even in that case, sb_pquotino field is
  314. * not written to disk unless the superblock supports pquotino.
  315. */
  316. if (sbp->sb_uquotino == 0)
  317. sbp->sb_uquotino = NULLFSINO;
  318. if (sbp->sb_gquotino == 0)
  319. sbp->sb_gquotino = NULLFSINO;
  320. if (sbp->sb_pquotino == 0)
  321. sbp->sb_pquotino = NULLFSINO;
  322. /*
  323. * We need to do these manipilations only if we are working
  324. * with an older version of on-disk superblock.
  325. */
  326. if (xfs_sb_version_has_pquotino(sbp))
  327. return;
  328. if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
  329. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  330. XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
  331. if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
  332. sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
  333. XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
  334. sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
  335. if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
  336. sbp->sb_gquotino != NULLFSINO) {
  337. /*
  338. * In older version of superblock, on-disk superblock only
  339. * has sb_gquotino, and in-core superblock has both sb_gquotino
  340. * and sb_pquotino. But, only one of them is supported at any
  341. * point of time. So, if PQUOTA is set in disk superblock,
  342. * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
  343. * above is to make sure we don't do this twice and wipe them
  344. * both out!
  345. */
  346. sbp->sb_pquotino = sbp->sb_gquotino;
  347. sbp->sb_gquotino = NULLFSINO;
  348. }
  349. }
  350. static void
  351. __xfs_sb_from_disk(
  352. struct xfs_sb *to,
  353. xfs_dsb_t *from,
  354. bool convert_xquota)
  355. {
  356. to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
  357. to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
  358. to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
  359. to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
  360. to->sb_rextents = be64_to_cpu(from->sb_rextents);
  361. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  362. to->sb_logstart = be64_to_cpu(from->sb_logstart);
  363. to->sb_rootino = be64_to_cpu(from->sb_rootino);
  364. to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
  365. to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
  366. to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
  367. to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
  368. to->sb_agcount = be32_to_cpu(from->sb_agcount);
  369. to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
  370. to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
  371. to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
  372. to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
  373. to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
  374. to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
  375. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  376. to->sb_blocklog = from->sb_blocklog;
  377. to->sb_sectlog = from->sb_sectlog;
  378. to->sb_inodelog = from->sb_inodelog;
  379. to->sb_inopblog = from->sb_inopblog;
  380. to->sb_agblklog = from->sb_agblklog;
  381. to->sb_rextslog = from->sb_rextslog;
  382. to->sb_inprogress = from->sb_inprogress;
  383. to->sb_imax_pct = from->sb_imax_pct;
  384. to->sb_icount = be64_to_cpu(from->sb_icount);
  385. to->sb_ifree = be64_to_cpu(from->sb_ifree);
  386. to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
  387. to->sb_frextents = be64_to_cpu(from->sb_frextents);
  388. to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
  389. to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
  390. to->sb_qflags = be16_to_cpu(from->sb_qflags);
  391. to->sb_flags = from->sb_flags;
  392. to->sb_shared_vn = from->sb_shared_vn;
  393. to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
  394. to->sb_unit = be32_to_cpu(from->sb_unit);
  395. to->sb_width = be32_to_cpu(from->sb_width);
  396. to->sb_dirblklog = from->sb_dirblklog;
  397. to->sb_logsectlog = from->sb_logsectlog;
  398. to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
  399. to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
  400. to->sb_features2 = be32_to_cpu(from->sb_features2);
  401. to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
  402. to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
  403. to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
  404. to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
  405. to->sb_features_log_incompat =
  406. be32_to_cpu(from->sb_features_log_incompat);
  407. /* crc is only used on disk, not in memory; just init to 0 here. */
  408. to->sb_crc = 0;
  409. to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
  410. to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
  411. to->sb_lsn = be64_to_cpu(from->sb_lsn);
  412. /*
  413. * sb_meta_uuid is only on disk if it differs from sb_uuid and the
  414. * feature flag is set; if not set we keep it only in memory.
  415. */
  416. if (xfs_sb_version_hasmetauuid(to))
  417. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  418. else
  419. uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
  420. /* Convert on-disk flags to in-memory flags? */
  421. if (convert_xquota)
  422. xfs_sb_quota_from_disk(to);
  423. }
  424. void
  425. xfs_sb_from_disk(
  426. struct xfs_sb *to,
  427. xfs_dsb_t *from)
  428. {
  429. __xfs_sb_from_disk(to, from, true);
  430. }
  431. static void
  432. xfs_sb_quota_to_disk(
  433. struct xfs_dsb *to,
  434. struct xfs_sb *from)
  435. {
  436. uint16_t qflags = from->sb_qflags;
  437. to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
  438. if (xfs_sb_version_has_pquotino(from)) {
  439. to->sb_qflags = cpu_to_be16(from->sb_qflags);
  440. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  441. to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
  442. return;
  443. }
  444. /*
  445. * The in-core version of sb_qflags do not have XFS_OQUOTA_*
  446. * flags, whereas the on-disk version does. So, convert incore
  447. * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
  448. */
  449. qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
  450. XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
  451. if (from->sb_qflags &
  452. (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
  453. qflags |= XFS_OQUOTA_ENFD;
  454. if (from->sb_qflags &
  455. (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
  456. qflags |= XFS_OQUOTA_CHKD;
  457. to->sb_qflags = cpu_to_be16(qflags);
  458. /*
  459. * GQUOTINO and PQUOTINO cannot be used together in versions
  460. * of superblock that do not have pquotino. from->sb_flags
  461. * tells us which quota is active and should be copied to
  462. * disk. If neither are active, we should NULL the inode.
  463. *
  464. * In all cases, the separate pquotino must remain 0 because it
  465. * it beyond the "end" of the valid non-pquotino superblock.
  466. */
  467. if (from->sb_qflags & XFS_GQUOTA_ACCT)
  468. to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
  469. else if (from->sb_qflags & XFS_PQUOTA_ACCT)
  470. to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
  471. else {
  472. /*
  473. * We can't rely on just the fields being logged to tell us
  474. * that it is safe to write NULLFSINO - we should only do that
  475. * if quotas are not actually enabled. Hence only write
  476. * NULLFSINO if both in-core quota inodes are NULL.
  477. */
  478. if (from->sb_gquotino == NULLFSINO &&
  479. from->sb_pquotino == NULLFSINO)
  480. to->sb_gquotino = cpu_to_be64(NULLFSINO);
  481. }
  482. to->sb_pquotino = 0;
  483. }
  484. void
  485. xfs_sb_to_disk(
  486. struct xfs_dsb *to,
  487. struct xfs_sb *from)
  488. {
  489. xfs_sb_quota_to_disk(to, from);
  490. to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
  491. to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
  492. to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
  493. to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
  494. to->sb_rextents = cpu_to_be64(from->sb_rextents);
  495. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  496. to->sb_logstart = cpu_to_be64(from->sb_logstart);
  497. to->sb_rootino = cpu_to_be64(from->sb_rootino);
  498. to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
  499. to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
  500. to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
  501. to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
  502. to->sb_agcount = cpu_to_be32(from->sb_agcount);
  503. to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
  504. to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
  505. to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
  506. to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
  507. to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
  508. to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
  509. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  510. to->sb_blocklog = from->sb_blocklog;
  511. to->sb_sectlog = from->sb_sectlog;
  512. to->sb_inodelog = from->sb_inodelog;
  513. to->sb_inopblog = from->sb_inopblog;
  514. to->sb_agblklog = from->sb_agblklog;
  515. to->sb_rextslog = from->sb_rextslog;
  516. to->sb_inprogress = from->sb_inprogress;
  517. to->sb_imax_pct = from->sb_imax_pct;
  518. to->sb_icount = cpu_to_be64(from->sb_icount);
  519. to->sb_ifree = cpu_to_be64(from->sb_ifree);
  520. to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
  521. to->sb_frextents = cpu_to_be64(from->sb_frextents);
  522. to->sb_flags = from->sb_flags;
  523. to->sb_shared_vn = from->sb_shared_vn;
  524. to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
  525. to->sb_unit = cpu_to_be32(from->sb_unit);
  526. to->sb_width = cpu_to_be32(from->sb_width);
  527. to->sb_dirblklog = from->sb_dirblklog;
  528. to->sb_logsectlog = from->sb_logsectlog;
  529. to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
  530. to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
  531. /*
  532. * We need to ensure that bad_features2 always matches features2.
  533. * Hence we enforce that here rather than having to remember to do it
  534. * everywhere else that updates features2.
  535. */
  536. from->sb_bad_features2 = from->sb_features2;
  537. to->sb_features2 = cpu_to_be32(from->sb_features2);
  538. to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
  539. if (xfs_sb_version_hascrc(from)) {
  540. to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
  541. to->sb_features_ro_compat =
  542. cpu_to_be32(from->sb_features_ro_compat);
  543. to->sb_features_incompat =
  544. cpu_to_be32(from->sb_features_incompat);
  545. to->sb_features_log_incompat =
  546. cpu_to_be32(from->sb_features_log_incompat);
  547. to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
  548. to->sb_lsn = cpu_to_be64(from->sb_lsn);
  549. if (xfs_sb_version_hasmetauuid(from))
  550. uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
  551. }
  552. }
  553. static int
  554. xfs_sb_verify(
  555. struct xfs_buf *bp,
  556. bool check_version)
  557. {
  558. struct xfs_mount *mp = bp->b_target->bt_mount;
  559. struct xfs_sb sb;
  560. /*
  561. * Use call variant which doesn't convert quota flags from disk
  562. * format, because xfs_mount_validate_sb checks the on-disk flags.
  563. */
  564. __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
  565. /*
  566. * Only check the in progress field for the primary superblock as
  567. * mkfs.xfs doesn't clear it from secondary superblocks.
  568. */
  569. return xfs_mount_validate_sb(mp, &sb,
  570. bp->b_maps[0].bm_bn == XFS_SB_DADDR,
  571. check_version);
  572. }
  573. /*
  574. * If the superblock has the CRC feature bit set or the CRC field is non-null,
  575. * check that the CRC is valid. We check the CRC field is non-null because a
  576. * single bit error could clear the feature bit and unused parts of the
  577. * superblock are supposed to be zero. Hence a non-null crc field indicates that
  578. * we've potentially lost a feature bit and we should check it anyway.
  579. *
  580. * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
  581. * last field in V4 secondary superblocks. So for secondary superblocks,
  582. * we are more forgiving, and ignore CRC failures if the primary doesn't
  583. * indicate that the fs version is V5.
  584. */
  585. static void
  586. xfs_sb_read_verify(
  587. struct xfs_buf *bp)
  588. {
  589. struct xfs_mount *mp = bp->b_target->bt_mount;
  590. struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
  591. int error;
  592. /*
  593. * open code the version check to avoid needing to convert the entire
  594. * superblock from disk order just to check the version number
  595. */
  596. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
  597. (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
  598. XFS_SB_VERSION_5) ||
  599. dsb->sb_crc != 0)) {
  600. if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
  601. /* Only fail bad secondaries on a known V5 filesystem */
  602. if (bp->b_bn == XFS_SB_DADDR ||
  603. xfs_sb_version_hascrc(&mp->m_sb)) {
  604. error = -EFSBADCRC;
  605. goto out_error;
  606. }
  607. }
  608. }
  609. error = xfs_sb_verify(bp, true);
  610. out_error:
  611. if (error == -EFSCORRUPTED || error == -EFSBADCRC)
  612. xfs_verifier_error(bp, error, __this_address);
  613. else if (error)
  614. xfs_buf_ioerror(bp, error);
  615. }
  616. /*
  617. * We may be probed for a filesystem match, so we may not want to emit
  618. * messages when the superblock buffer is not actually an XFS superblock.
  619. * If we find an XFS superblock, then run a normal, noisy mount because we are
  620. * really going to mount it and want to know about errors.
  621. */
  622. static void
  623. xfs_sb_quiet_read_verify(
  624. struct xfs_buf *bp)
  625. {
  626. struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
  627. if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
  628. /* XFS filesystem, verify noisily! */
  629. xfs_sb_read_verify(bp);
  630. return;
  631. }
  632. /* quietly fail */
  633. xfs_buf_ioerror(bp, -EWRONGFS);
  634. }
  635. static void
  636. xfs_sb_write_verify(
  637. struct xfs_buf *bp)
  638. {
  639. struct xfs_mount *mp = bp->b_target->bt_mount;
  640. struct xfs_buf_log_item *bip = bp->b_log_item;
  641. int error;
  642. error = xfs_sb_verify(bp, false);
  643. if (error) {
  644. xfs_verifier_error(bp, error, __this_address);
  645. return;
  646. }
  647. if (!xfs_sb_version_hascrc(&mp->m_sb))
  648. return;
  649. if (bip)
  650. XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  651. xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
  652. }
  653. const struct xfs_buf_ops xfs_sb_buf_ops = {
  654. .name = "xfs_sb",
  655. .verify_read = xfs_sb_read_verify,
  656. .verify_write = xfs_sb_write_verify,
  657. };
  658. const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
  659. .name = "xfs_sb_quiet",
  660. .verify_read = xfs_sb_quiet_read_verify,
  661. .verify_write = xfs_sb_write_verify,
  662. };
  663. /*
  664. * xfs_mount_common
  665. *
  666. * Mount initialization code establishing various mount
  667. * fields from the superblock associated with the given
  668. * mount structure
  669. */
  670. void
  671. xfs_sb_mount_common(
  672. struct xfs_mount *mp,
  673. struct xfs_sb *sbp)
  674. {
  675. mp->m_agfrotor = mp->m_agirotor = 0;
  676. mp->m_maxagi = mp->m_sb.sb_agcount;
  677. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  678. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  679. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  680. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  681. mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
  682. mp->m_blockmask = sbp->sb_blocksize - 1;
  683. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  684. mp->m_blockwmask = mp->m_blockwsize - 1;
  685. mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
  686. mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
  687. mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
  688. mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
  689. mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
  690. mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
  691. mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
  692. mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
  693. mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
  694. mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
  695. mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
  696. mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
  697. mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
  698. mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
  699. mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
  700. mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
  701. mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
  702. mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
  703. mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
  704. mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
  705. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  706. mp->m_ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
  707. sbp->sb_inopblock);
  708. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  709. if (sbp->sb_spino_align)
  710. mp->m_ialloc_min_blks = sbp->sb_spino_align;
  711. else
  712. mp->m_ialloc_min_blks = mp->m_ialloc_blks;
  713. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  714. mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
  715. }
  716. /*
  717. * xfs_initialize_perag_data
  718. *
  719. * Read in each per-ag structure so we can count up the number of
  720. * allocated inodes, free inodes and used filesystem blocks as this
  721. * information is no longer persistent in the superblock. Once we have
  722. * this information, write it into the in-core superblock structure.
  723. */
  724. int
  725. xfs_initialize_perag_data(
  726. struct xfs_mount *mp,
  727. xfs_agnumber_t agcount)
  728. {
  729. xfs_agnumber_t index;
  730. xfs_perag_t *pag;
  731. xfs_sb_t *sbp = &mp->m_sb;
  732. uint64_t ifree = 0;
  733. uint64_t ialloc = 0;
  734. uint64_t bfree = 0;
  735. uint64_t bfreelst = 0;
  736. uint64_t btree = 0;
  737. int error;
  738. for (index = 0; index < agcount; index++) {
  739. /*
  740. * read the agf, then the agi. This gets us
  741. * all the information we need and populates the
  742. * per-ag structures for us.
  743. */
  744. error = xfs_alloc_pagf_init(mp, NULL, index, 0);
  745. if (error)
  746. return error;
  747. error = xfs_ialloc_pagi_init(mp, NULL, index);
  748. if (error)
  749. return error;
  750. pag = xfs_perag_get(mp, index);
  751. ifree += pag->pagi_freecount;
  752. ialloc += pag->pagi_count;
  753. bfree += pag->pagf_freeblks;
  754. bfreelst += pag->pagf_flcount;
  755. btree += pag->pagf_btreeblks;
  756. xfs_perag_put(pag);
  757. }
  758. /* Overwrite incore superblock counters with just-read data */
  759. spin_lock(&mp->m_sb_lock);
  760. sbp->sb_ifree = ifree;
  761. sbp->sb_icount = ialloc;
  762. sbp->sb_fdblocks = bfree + bfreelst + btree;
  763. spin_unlock(&mp->m_sb_lock);
  764. xfs_reinit_percpu_counters(mp);
  765. return 0;
  766. }
  767. /*
  768. * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
  769. * into the superblock buffer to be logged. It does not provide the higher
  770. * level of locking that is needed to protect the in-core superblock from
  771. * concurrent access.
  772. */
  773. void
  774. xfs_log_sb(
  775. struct xfs_trans *tp)
  776. {
  777. struct xfs_mount *mp = tp->t_mountp;
  778. struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
  779. mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
  780. mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
  781. mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
  782. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
  783. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
  784. xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
  785. }
  786. /*
  787. * xfs_sync_sb
  788. *
  789. * Sync the superblock to disk.
  790. *
  791. * Note that the caller is responsible for checking the frozen state of the
  792. * filesystem. This procedure uses the non-blocking transaction allocator and
  793. * thus will allow modifications to a frozen fs. This is required because this
  794. * code can be called during the process of freezing where use of the high-level
  795. * allocator would deadlock.
  796. */
  797. int
  798. xfs_sync_sb(
  799. struct xfs_mount *mp,
  800. bool wait)
  801. {
  802. struct xfs_trans *tp;
  803. int error;
  804. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
  805. XFS_TRANS_NO_WRITECOUNT, &tp);
  806. if (error)
  807. return error;
  808. xfs_log_sb(tp);
  809. if (wait)
  810. xfs_trans_set_sync(tp);
  811. return xfs_trans_commit(tp);
  812. }
  813. /*
  814. * Update all the secondary superblocks to match the new state of the primary.
  815. * Because we are completely overwriting all the existing fields in the
  816. * secondary superblock buffers, there is no need to read them in from disk.
  817. * Just get a new buffer, stamp it and write it.
  818. *
  819. * The sb buffers need to be cached here so that we serialise against other
  820. * operations that access the secondary superblocks, but we don't want to keep
  821. * them in memory once it is written so we mark it as a one-shot buffer.
  822. */
  823. int
  824. xfs_update_secondary_sbs(
  825. struct xfs_mount *mp)
  826. {
  827. xfs_agnumber_t agno;
  828. int saved_error = 0;
  829. int error = 0;
  830. LIST_HEAD (buffer_list);
  831. /* update secondary superblocks. */
  832. for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
  833. struct xfs_buf *bp;
  834. bp = xfs_buf_get(mp->m_ddev_targp,
  835. XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
  836. XFS_FSS_TO_BB(mp, 1), 0);
  837. /*
  838. * If we get an error reading or writing alternate superblocks,
  839. * continue. xfs_repair chooses the "best" superblock based
  840. * on most matches; if we break early, we'll leave more
  841. * superblocks un-updated than updated, and xfs_repair may
  842. * pick them over the properly-updated primary.
  843. */
  844. if (!bp) {
  845. xfs_warn(mp,
  846. "error allocating secondary superblock for ag %d",
  847. agno);
  848. if (!saved_error)
  849. saved_error = -ENOMEM;
  850. continue;
  851. }
  852. bp->b_ops = &xfs_sb_buf_ops;
  853. xfs_buf_oneshot(bp);
  854. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  855. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
  856. xfs_buf_delwri_queue(bp, &buffer_list);
  857. xfs_buf_relse(bp);
  858. /* don't hold too many buffers at once */
  859. if (agno % 16)
  860. continue;
  861. error = xfs_buf_delwri_submit(&buffer_list);
  862. if (error) {
  863. xfs_warn(mp,
  864. "write error %d updating a secondary superblock near ag %d",
  865. error, agno);
  866. if (!saved_error)
  867. saved_error = error;
  868. continue;
  869. }
  870. }
  871. error = xfs_buf_delwri_submit(&buffer_list);
  872. if (error) {
  873. xfs_warn(mp,
  874. "write error %d updating a secondary superblock near ag %d",
  875. error, agno);
  876. }
  877. return saved_error ? saved_error : error;
  878. }
  879. /*
  880. * Same behavior as xfs_sync_sb, except that it is always synchronous and it
  881. * also writes the superblock buffer to disk sector 0 immediately.
  882. */
  883. int
  884. xfs_sync_sb_buf(
  885. struct xfs_mount *mp)
  886. {
  887. struct xfs_trans *tp;
  888. struct xfs_buf *bp;
  889. int error;
  890. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
  891. if (error)
  892. return error;
  893. bp = xfs_trans_getsb(tp, mp, 0);
  894. xfs_log_sb(tp);
  895. xfs_trans_bhold(tp, bp);
  896. xfs_trans_set_sync(tp);
  897. error = xfs_trans_commit(tp);
  898. if (error)
  899. goto out;
  900. /*
  901. * write out the sb buffer to get the changes to disk
  902. */
  903. error = xfs_bwrite(bp);
  904. out:
  905. xfs_buf_relse(bp);
  906. return error;
  907. }
  908. int
  909. xfs_fs_geometry(
  910. struct xfs_sb *sbp,
  911. struct xfs_fsop_geom *geo,
  912. int struct_version)
  913. {
  914. memset(geo, 0, sizeof(struct xfs_fsop_geom));
  915. geo->blocksize = sbp->sb_blocksize;
  916. geo->rtextsize = sbp->sb_rextsize;
  917. geo->agblocks = sbp->sb_agblocks;
  918. geo->agcount = sbp->sb_agcount;
  919. geo->logblocks = sbp->sb_logblocks;
  920. geo->sectsize = sbp->sb_sectsize;
  921. geo->inodesize = sbp->sb_inodesize;
  922. geo->imaxpct = sbp->sb_imax_pct;
  923. geo->datablocks = sbp->sb_dblocks;
  924. geo->rtblocks = sbp->sb_rblocks;
  925. geo->rtextents = sbp->sb_rextents;
  926. geo->logstart = sbp->sb_logstart;
  927. BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
  928. memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
  929. if (struct_version < 2)
  930. return 0;
  931. geo->sunit = sbp->sb_unit;
  932. geo->swidth = sbp->sb_width;
  933. if (struct_version < 3)
  934. return 0;
  935. geo->version = XFS_FSOP_GEOM_VERSION;
  936. geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
  937. XFS_FSOP_GEOM_FLAGS_DIRV2;
  938. if (xfs_sb_version_hasattr(sbp))
  939. geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
  940. if (xfs_sb_version_hasquota(sbp))
  941. geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
  942. if (xfs_sb_version_hasalign(sbp))
  943. geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
  944. if (xfs_sb_version_hasdalign(sbp))
  945. geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
  946. if (xfs_sb_version_hasextflgbit(sbp))
  947. geo->flags |= XFS_FSOP_GEOM_FLAGS_EXTFLG;
  948. if (xfs_sb_version_hassector(sbp))
  949. geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
  950. if (xfs_sb_version_hasasciici(sbp))
  951. geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
  952. if (xfs_sb_version_haslazysbcount(sbp))
  953. geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
  954. if (xfs_sb_version_hasattr2(sbp))
  955. geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
  956. if (xfs_sb_version_hasprojid32bit(sbp))
  957. geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
  958. if (xfs_sb_version_hascrc(sbp))
  959. geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
  960. if (xfs_sb_version_hasftype(sbp))
  961. geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
  962. if (xfs_sb_version_hasfinobt(sbp))
  963. geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
  964. if (xfs_sb_version_hassparseinodes(sbp))
  965. geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
  966. if (xfs_sb_version_hasrmapbt(sbp))
  967. geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
  968. if (xfs_sb_version_hasreflink(sbp))
  969. geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
  970. if (xfs_sb_version_hassector(sbp))
  971. geo->logsectsize = sbp->sb_logsectsize;
  972. else
  973. geo->logsectsize = BBSIZE;
  974. geo->rtsectsize = sbp->sb_blocksize;
  975. geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
  976. if (struct_version < 4)
  977. return 0;
  978. if (xfs_sb_version_haslogv2(sbp))
  979. geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
  980. geo->logsunit = sbp->sb_logsunit;
  981. return 0;
  982. }
  983. /* Read a secondary superblock. */
  984. int
  985. xfs_sb_read_secondary(
  986. struct xfs_mount *mp,
  987. struct xfs_trans *tp,
  988. xfs_agnumber_t agno,
  989. struct xfs_buf **bpp)
  990. {
  991. struct xfs_buf *bp;
  992. int error;
  993. ASSERT(agno != 0 && agno != NULLAGNUMBER);
  994. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  995. XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  996. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
  997. if (error)
  998. return error;
  999. xfs_buf_set_ref(bp, XFS_SSB_REF);
  1000. *bpp = bp;
  1001. return 0;
  1002. }
  1003. /* Get an uninitialised secondary superblock buffer. */
  1004. int
  1005. xfs_sb_get_secondary(
  1006. struct xfs_mount *mp,
  1007. struct xfs_trans *tp,
  1008. xfs_agnumber_t agno,
  1009. struct xfs_buf **bpp)
  1010. {
  1011. struct xfs_buf *bp;
  1012. ASSERT(agno != 0 && agno != NULLAGNUMBER);
  1013. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1014. XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  1015. XFS_FSS_TO_BB(mp, 1), 0);
  1016. if (!bp)
  1017. return -ENOMEM;
  1018. bp->b_ops = &xfs_sb_buf_ops;
  1019. xfs_buf_oneshot(bp);
  1020. *bpp = bp;
  1021. return 0;
  1022. }