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