xfs_sb.c 36 KB

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