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