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