xfs_sb.c 33 KB

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