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