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