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