]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_sb.c
xfs: fix transaction leak on remote attr set/remove failure
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_sb.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "libxfs_priv.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
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"
25 #include "xfs_rmap_btree.h"
26 #include "xfs_bmap.h"
27 #include "xfs_refcount_btree.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
30
31 /*
32 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
33 */
34
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 */
40 struct xfs_perag *
41 xfs_perag_get(
42 struct xfs_mount *mp,
43 xfs_agnumber_t agno)
44 {
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);
53 }
54 rcu_read_unlock();
55 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
56 return pag;
57 }
58
59 /*
60 * search from @first to find the next perag with the given tag set.
61 */
62 struct xfs_perag *
63 xfs_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
85 void
86 xfs_perag_put(
87 struct xfs_perag *pag)
88 {
89 int ref;
90
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_);
94 }
95
96 /*
97 * Check the validity of the SB found.
98 */
99 STATIC int
100 xfs_mount_validate_sb(
101 xfs_mount_t *mp,
102 xfs_sb_t *sbp,
103 bool check_inprogress,
104 bool check_version)
105 {
106 uint32_t agcount = 0;
107 uint32_t rem;
108
109 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
110 xfs_warn(mp, "bad magic number");
111 return -EWRONGFS;
112 }
113
114
115 if (!xfs_sb_good_version(sbp)) {
116 xfs_warn(mp, "bad version");
117 return -EWRONGFS;
118 }
119
120 /*
121 * Version 5 superblock feature mask validation. Reject combinations the
122 * kernel cannot support up front before checking anything else. For
123 * write validation, we don't need to check feature masks.
124 */
125 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
126 if (xfs_sb_has_compat_feature(sbp,
127 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
128 xfs_warn(mp,
129 "Superblock has unknown compatible features (0x%x) enabled.",
130 (sbp->sb_features_compat &
131 XFS_SB_FEAT_COMPAT_UNKNOWN));
132 xfs_warn(mp,
133 "Using a more recent kernel is recommended.");
134 }
135
136 if (xfs_sb_has_ro_compat_feature(sbp,
137 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
138 xfs_alert(mp,
139 "Superblock has unknown read-only compatible features (0x%x) enabled.",
140 (sbp->sb_features_ro_compat &
141 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
142 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
143 xfs_warn(mp,
144 "Attempted to mount read-only compatible filesystem read-write.");
145 xfs_warn(mp,
146 "Filesystem can only be safely mounted read only.");
147
148 return -EINVAL;
149 }
150 }
151 if (xfs_sb_has_incompat_feature(sbp,
152 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
153 xfs_warn(mp,
154 "Superblock has unknown incompatible features (0x%x) enabled.",
155 (sbp->sb_features_incompat &
156 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
157 xfs_warn(mp,
158 "Filesystem can not be safely mounted by this kernel.");
159 return -EINVAL;
160 }
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;
170 }
171
172 if (xfs_sb_version_has_pquotino(sbp)) {
173 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
174 xfs_notice(mp,
175 "Version 5 of Super block has XFS_OQUOTA bits.");
176 return -EFSCORRUPTED;
177 }
178 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
179 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
180 xfs_notice(mp,
181 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
182 return -EFSCORRUPTED;
183 }
184
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
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
203 if (unlikely(
204 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
205 xfs_warn(mp,
206 "filesystem is marked as having an external log; "
207 "specify logdev on the mount command line.");
208 return -EINVAL;
209 }
210
211 if (unlikely(
212 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
213 xfs_warn(mp,
214 "filesystem is marked as having an internal log; "
215 "do not specify logdev on the mount command line.");
216 return -EINVAL;
217 }
218
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
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) ||
242 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
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) ||
248 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
249 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
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 ||
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) ||
260 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
261 sbp->sb_shared_vn != 0)) {
262 xfs_notice(mp, "SB sanity check failed");
263 return -EFSCORRUPTED;
264 }
265
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
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
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);
300 return -ENOSYS;
301 }
302
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.");
307 return -EFBIG;
308 }
309
310 return 0;
311 }
312
313 void
314 xfs_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
349 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
350 sbp->sb_gquotino != NULLFSINO) {
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,
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!
359 */
360 sbp->sb_pquotino = sbp->sb_gquotino;
361 sbp->sb_gquotino = NULLFSINO;
362 }
363 }
364
365 static void
366 __xfs_sb_from_disk(
367 struct xfs_sb *to,
368 xfs_dsb_t *from,
369 bool convert_xquota)
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);
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);
420 to->sb_features_log_incompat =
421 be32_to_cpu(from->sb_features_log_incompat);
422 /* crc is only used on disk, not in memory; just init to 0 here. */
423 to->sb_crc = 0;
424 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
425 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
426 to->sb_lsn = be64_to_cpu(from->sb_lsn);
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);
435 /* Convert on-disk flags to in-memory flags? */
436 if (convert_xquota)
437 xfs_sb_quota_from_disk(to);
438 }
439
440 void
441 xfs_sb_from_disk(
442 struct xfs_sb *to,
443 xfs_dsb_t *from)
444 {
445 __xfs_sb_from_disk(to, from, true);
446 }
447
448 static void
449 xfs_sb_quota_to_disk(
450 struct xfs_dsb *to,
451 struct xfs_sb *from)
452 {
453 uint16_t qflags = from->sb_qflags;
454
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
463 /*
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.
467 */
468 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
469 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
470
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);
478
479 /*
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.
484 *
485 * In all cases, the separate pquotino must remain 0 because it
486 * it beyond the "end" of the valid non-pquotino superblock.
487 */
488 if (from->sb_qflags & XFS_GQUOTA_ACCT)
489 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
490 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
491 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
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 }
503
504 to->sb_pquotino = 0;
505 }
506
507 void
508 xfs_sb_to_disk(
509 struct xfs_dsb *to,
510 struct xfs_sb *from)
511 {
512 xfs_sb_quota_to_disk(to, from);
513
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);
556
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);
574 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
575 to->sb_lsn = cpu_to_be64(from->sb_lsn);
576 if (xfs_sb_version_hasmetauuid(from))
577 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
578 }
579 }
580
581 static int
582 xfs_sb_verify(
583 struct xfs_buf *bp,
584 bool check_version)
585 {
586 struct xfs_mount *mp = bp->b_target->bt_mount;
587 struct xfs_sb sb;
588
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);
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 */
599 return xfs_mount_validate_sb(mp, &sb,
600 bp->b_maps[0].bm_bn == XFS_SB_DADDR,
601 check_version);
602 }
603
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.
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.
615 */
616 static void
617 xfs_sb_read_verify(
618 struct xfs_buf *bp)
619 {
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
633 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
634 /* Only fail bad secondaries on a known V5 filesystem */
635 if (bp->b_bn == XFS_SB_DADDR ||
636 xfs_sb_version_hascrc(&mp->m_sb)) {
637 error = -EFSBADCRC;
638 goto out_error;
639 }
640 }
641 }
642 error = xfs_sb_verify(bp, true);
643
644 out_error:
645 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
646 xfs_verifier_error(bp, error, __this_address);
647 else if (error)
648 xfs_buf_ioerror(bp, error);
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.
654 * If we find an XFS superblock, then run a normal, noisy mount because we are
655 * really going to mount it and want to know about errors.
656 */
657 static void
658 xfs_sb_quiet_read_verify(
659 struct xfs_buf *bp)
660 {
661 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
662
663 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
664 /* XFS filesystem, verify noisily! */
665 xfs_sb_read_verify(bp);
666 return;
667 }
668 /* quietly fail */
669 xfs_buf_ioerror(bp, -EWRONGFS);
670 }
671
672 static void
673 xfs_sb_write_verify(
674 struct xfs_buf *bp)
675 {
676 struct xfs_mount *mp = bp->b_target->bt_mount;
677 struct xfs_buf_log_item *bip = bp->b_log_item;
678 int error;
679
680 error = xfs_sb_verify(bp, false);
681 if (error) {
682 xfs_verifier_error(bp, error, __this_address);
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
692 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
693 }
694
695 const struct xfs_buf_ops xfs_sb_buf_ops = {
696 .name = "xfs_sb",
697 .verify_read = xfs_sb_read_verify,
698 .verify_write = xfs_sb_write_verify,
699 };
700
701 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
702 .name = "xfs_sb_quiet",
703 .verify_read = xfs_sb_quiet_read_verify,
704 .verify_write = xfs_sb_write_verify,
705 };
706
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
713 */
714 void
715 xfs_sb_mount_common(
716 struct xfs_mount *mp,
717 struct xfs_sb *sbp)
718 {
719 mp->m_agfrotor = mp->m_agirotor = 0;
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;
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;
729
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;
744
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);
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
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);
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
755 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
756 mp->m_ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
757 sbp->sb_inopblock);
758 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
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;
764 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
765 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
766 }
767
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 */
776 int
777 xfs_initialize_perag_data(
778 struct xfs_mount *mp,
779 xfs_agnumber_t agcount)
780 {
781 xfs_agnumber_t index;
782 xfs_perag_t *pag;
783 xfs_sb_t *sbp = &mp->m_sb;
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;
789 uint64_t fdblocks;
790 int error;
791
792 for (index = 0; index < agcount; index++) {
793 /*
794 * read the agf, then the agi. This gets us
795 * all the information we need and populates the
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;
805 pag = xfs_perag_get(mp, index);
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;
811 xfs_perag_put(pag);
812 }
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 }
826
827 /* Overwrite incore superblock counters with just-read data */
828 spin_lock(&mp->m_sb_lock);
829 sbp->sb_ifree = ifree;
830 sbp->sb_icount = ialloc;
831 sbp->sb_fdblocks = fdblocks;
832 spin_unlock(&mp->m_sb_lock);
833
834 xfs_reinit_percpu_counters(mp);
835 out:
836 mp->m_flags &= ~XFS_MOUNT_BAD_SUMMARY;
837 return error;
838 }
839
840 /*
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.
845 */
846 void
847 xfs_log_sb(
848 struct xfs_trans *tp)
849 {
850 struct xfs_mount *mp = tp->t_mountp;
851 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
852
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);
856
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 }
861
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 */
873 int
874 xfs_sync_sb(
875 struct xfs_mount *mp,
876 bool wait)
877 {
878 struct xfs_trans *tp;
879 int error;
880
881 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
882 XFS_TRANS_NO_WRITECOUNT, &tp);
883 if (error)
884 return error;
885
886 xfs_log_sb(tp);
887 if (wait)
888 xfs_trans_set_sync(tp);
889 return xfs_trans_commit(tp);
890 }
891
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 */
902 int
903 xfs_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
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 */
969 int
970 xfs_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);
992 out:
993 xfs_buf_relse(bp);
994 return error;
995 }
996
997 int
998 xfs_fs_geometry(
999 struct xfs_sb *sbp,
1000 struct xfs_fsop_geom *geo,
1001 int struct_version)
1002 {
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
1071 if (struct_version < 4)
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;
1078
1079 return 0;
1080 }
1081
1082 /* Read a secondary superblock. */
1083 int
1084 xfs_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 }
1103
1104 /* Get an uninitialised secondary superblock buffer. */
1105 int
1106 xfs_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 }