1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.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_ialloc.h"
16 #include "xfs_alloc.h"
17 #include "xfs_error.h"
18 #include "xfs_trans.h"
19 #include "xfs_buf_item.h"
20 #include "xfs_bmap_btree.h"
21 #include "xfs_alloc_btree.h"
22 #include "xfs_log.h"
23 #include "xfs_rmap_btree.h"
24 #include "xfs_refcount_btree.h"
25 #include "xfs_da_format.h"
26 #include "xfs_health.h"
27 #include "xfs_ag.h"
28 #include "xfs_rtbitmap.h"
29 #include "xfs_exchrange.h"
30 #include "xfs_rtgroup.h"
31 #include "xfs_rtrmap_btree.h"
32 #include "xfs_rtrefcount_btree.h"
33
34 /*
35 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
36 */
37
38 /*
39 * Check that all the V4 feature bits that the V5 filesystem format requires are
40 * correctly set.
41 */
42 static bool
xfs_sb_validate_v5_features(struct xfs_sb * sbp)43 xfs_sb_validate_v5_features(
44 struct xfs_sb *sbp)
45 {
46 /* We must not have any unknown V4 feature bits set */
47 if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
48 return false;
49
50 /*
51 * The CRC bit is considered an invalid V4 flag, so we have to add it
52 * manually to the OKBITS mask.
53 */
54 if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
55 XFS_SB_VERSION2_CRCBIT))
56 return false;
57
58 /* Now check all the required V4 feature flags are set. */
59
60 #define V5_VERS_FLAGS (XFS_SB_VERSION_NLINKBIT | \
61 XFS_SB_VERSION_ALIGNBIT | \
62 XFS_SB_VERSION_LOGV2BIT | \
63 XFS_SB_VERSION_EXTFLGBIT | \
64 XFS_SB_VERSION_DIRV2BIT | \
65 XFS_SB_VERSION_MOREBITSBIT)
66
67 #define V5_FEAT_FLAGS (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
68 XFS_SB_VERSION2_ATTR2BIT | \
69 XFS_SB_VERSION2_PROJID32BIT | \
70 XFS_SB_VERSION2_CRCBIT)
71
72 if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
73 return false;
74 if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
75 return false;
76 return true;
77 }
78
79 /*
80 * We current support XFS v5 formats with known features and v4 superblocks with
81 * at least V2 directories.
82 */
83 bool
xfs_sb_good_version(struct xfs_sb * sbp)84 xfs_sb_good_version(
85 struct xfs_sb *sbp)
86 {
87 /*
88 * All v5 filesystems are supported, but we must check that all the
89 * required v4 feature flags are enabled correctly as the code checks
90 * those flags and not for v5 support.
91 */
92 if (xfs_sb_is_v5(sbp))
93 return xfs_sb_validate_v5_features(sbp);
94
95 /* versions prior to v4 are not supported */
96 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
97 return false;
98
99 /* We must not have any unknown v4 feature bits set */
100 if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
101 ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
102 (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
103 return false;
104
105 /* V4 filesystems need v2 directories and unwritten extents */
106 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
107 return false;
108 if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
109 return false;
110
111 /* It's a supported v4 filesystem */
112 return true;
113 }
114
115 uint64_t
xfs_sb_version_to_features(struct xfs_sb * sbp)116 xfs_sb_version_to_features(
117 struct xfs_sb *sbp)
118 {
119 uint64_t features = 0;
120
121 /* optional V4 features */
122 if (sbp->sb_rblocks > 0)
123 features |= XFS_FEAT_REALTIME;
124 if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
125 features |= XFS_FEAT_NLINK;
126 if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
127 features |= XFS_FEAT_ATTR;
128 if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
129 features |= XFS_FEAT_QUOTA;
130 if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
131 features |= XFS_FEAT_ALIGN;
132 if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
133 features |= XFS_FEAT_LOGV2;
134 if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
135 features |= XFS_FEAT_DALIGN;
136 if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
137 features |= XFS_FEAT_EXTFLG;
138 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
139 features |= XFS_FEAT_SECTOR;
140 if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
141 features |= XFS_FEAT_ASCIICI;
142 if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
143 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
144 features |= XFS_FEAT_LAZYSBCOUNT;
145 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
146 features |= XFS_FEAT_ATTR2;
147 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
148 features |= XFS_FEAT_PROJID32;
149 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
150 features |= XFS_FEAT_FTYPE;
151 }
152
153 if (!xfs_sb_is_v5(sbp))
154 return features;
155
156 /* Always on V5 features */
157 features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
158 XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
159 XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
160
161 /* Optional V5 features */
162 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
163 features |= XFS_FEAT_FINOBT;
164 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
165 features |= XFS_FEAT_RMAPBT;
166 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
167 features |= XFS_FEAT_REFLINK;
168 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
169 features |= XFS_FEAT_INOBTCNT;
170 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
171 features |= XFS_FEAT_FTYPE;
172 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
173 features |= XFS_FEAT_SPINODES;
174 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
175 features |= XFS_FEAT_META_UUID;
176 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
177 features |= XFS_FEAT_BIGTIME;
178 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
179 features |= XFS_FEAT_NEEDSREPAIR;
180 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
181 features |= XFS_FEAT_NREXT64;
182 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE)
183 features |= XFS_FEAT_EXCHANGE_RANGE;
184 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_PARENT)
185 features |= XFS_FEAT_PARENT;
186 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)
187 features |= XFS_FEAT_METADIR;
188
189 return features;
190 }
191
192 /* Check all the superblock fields we care about when reading one in. */
193 STATIC int
xfs_validate_sb_read(struct xfs_mount * mp,struct xfs_sb * sbp)194 xfs_validate_sb_read(
195 struct xfs_mount *mp,
196 struct xfs_sb *sbp)
197 {
198 if (!xfs_sb_is_v5(sbp))
199 return 0;
200
201 /*
202 * Version 5 superblock feature mask validation. Reject combinations
203 * the kernel cannot support up front before checking anything else.
204 */
205 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
206 xfs_warn(mp,
207 "Superblock has unknown compatible features (0x%x) enabled.",
208 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
209 xfs_warn(mp,
210 "Using a more recent kernel is recommended.");
211 }
212
213 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
214 xfs_alert(mp,
215 "Superblock has unknown read-only compatible features (0x%x) enabled.",
216 (sbp->sb_features_ro_compat &
217 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
218 if (!xfs_is_readonly(mp)) {
219 xfs_warn(mp,
220 "Attempted to mount read-only compatible filesystem read-write.");
221 xfs_warn(mp,
222 "Filesystem can only be safely mounted read only.");
223
224 return -EINVAL;
225 }
226 }
227 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
228 xfs_warn(mp,
229 "Superblock has unknown incompatible features (0x%x) enabled.",
230 (sbp->sb_features_incompat &
231 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
232 xfs_warn(mp,
233 "Filesystem cannot be safely mounted by this kernel.");
234 return -EINVAL;
235 }
236
237 return 0;
238 }
239
240 /* Return the number of extents covered by a single rt bitmap file */
241 static xfs_rtbxlen_t
xfs_extents_per_rbm(struct xfs_sb * sbp)242 xfs_extents_per_rbm(
243 struct xfs_sb *sbp)
244 {
245 if (xfs_sb_is_v5(sbp) &&
246 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
247 return sbp->sb_rgextents;
248 return sbp->sb_rextents;
249 }
250
251 /*
252 * Return the payload size of a single rt bitmap block (without the metadata
253 * header if any).
254 */
255 static inline unsigned int
xfs_rtbmblock_size(struct xfs_sb * sbp)256 xfs_rtbmblock_size(
257 struct xfs_sb *sbp)
258 {
259 if (xfs_sb_is_v5(sbp) &&
260 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
261 return sbp->sb_blocksize - sizeof(struct xfs_rtbuf_blkinfo);
262 return sbp->sb_blocksize;
263 }
264
265 static uint64_t
xfs_expected_rbmblocks(struct xfs_sb * sbp)266 xfs_expected_rbmblocks(
267 struct xfs_sb *sbp)
268 {
269 return howmany_64(xfs_extents_per_rbm(sbp),
270 NBBY * xfs_rtbmblock_size(sbp));
271 }
272
273 /* Validate the realtime geometry */
274 bool
xfs_validate_rt_geometry(struct xfs_sb * sbp)275 xfs_validate_rt_geometry(
276 struct xfs_sb *sbp)
277 {
278 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
279 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)
280 return false;
281
282 if (sbp->sb_rblocks == 0) {
283 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
284 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0)
285 return false;
286 return true;
287 }
288
289 if (sbp->sb_rextents == 0 ||
290 sbp->sb_rextents != div_u64(sbp->sb_rblocks, sbp->sb_rextsize) ||
291 sbp->sb_rextslog != xfs_compute_rextslog(sbp->sb_rextents) ||
292 sbp->sb_rbmblocks != xfs_expected_rbmblocks(sbp))
293 return false;
294
295 return true;
296 }
297
298 /* Check all the superblock fields we care about when writing one out. */
299 STATIC int
xfs_validate_sb_write(struct xfs_mount * mp,struct xfs_buf * bp,struct xfs_sb * sbp)300 xfs_validate_sb_write(
301 struct xfs_mount *mp,
302 struct xfs_buf *bp,
303 struct xfs_sb *sbp)
304 {
305 /*
306 * Carry out additional sb summary counter sanity checks when we write
307 * the superblock. We skip this in the read validator because there
308 * could be newer superblocks in the log and if the values are garbage
309 * even after replay we'll recalculate them at the end of log mount.
310 *
311 * mkfs has traditionally written zeroed counters to inprogress and
312 * secondary superblocks, so allow this usage to continue because
313 * we never read counters from such superblocks.
314 */
315 if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
316 (sbp->sb_fdblocks > sbp->sb_dblocks ||
317 !xfs_verify_icount(mp, sbp->sb_icount) ||
318 sbp->sb_ifree > sbp->sb_icount)) {
319 xfs_warn(mp, "SB summary counter sanity check failed");
320 return -EFSCORRUPTED;
321 }
322
323 if (!xfs_sb_is_v5(sbp))
324 return 0;
325
326 /*
327 * Version 5 superblock feature mask validation. Reject combinations
328 * the kernel cannot support since we checked for unsupported bits in
329 * the read verifier, which means that memory is corrupt.
330 */
331 if (!xfs_is_readonly(mp) &&
332 xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
333 xfs_alert(mp,
334 "Corruption detected in superblock read-only compatible features (0x%x)!",
335 (sbp->sb_features_ro_compat &
336 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
337 return -EFSCORRUPTED;
338 }
339 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
340 xfs_warn(mp,
341 "Corruption detected in superblock incompatible features (0x%x)!",
342 (sbp->sb_features_incompat &
343 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
344 return -EFSCORRUPTED;
345 }
346 if (xfs_sb_has_incompat_log_feature(sbp,
347 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
348 xfs_warn(mp,
349 "Corruption detected in superblock incompatible log features (0x%x)!",
350 (sbp->sb_features_log_incompat &
351 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
352 return -EFSCORRUPTED;
353 }
354
355 /*
356 * We can't read verify the sb LSN because the read verifier is called
357 * before the log is allocated and processed. We know the log is set up
358 * before write verifier calls, so check it here.
359 */
360 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
361 return -EFSCORRUPTED;
362
363 return 0;
364 }
365
366 int
xfs_compute_rgblklog(xfs_rtxlen_t rgextents,xfs_rgblock_t rextsize)367 xfs_compute_rgblklog(
368 xfs_rtxlen_t rgextents,
369 xfs_rgblock_t rextsize)
370 {
371 uint64_t rgblocks = (uint64_t)rgextents * rextsize;
372
373 return xfs_highbit64(rgblocks - 1) + 1;
374 }
375
376 static int
xfs_validate_sb_rtgroups(struct xfs_mount * mp,struct xfs_sb * sbp)377 xfs_validate_sb_rtgroups(
378 struct xfs_mount *mp,
379 struct xfs_sb *sbp)
380 {
381 uint64_t groups;
382 int rgblklog;
383
384 if (sbp->sb_rextsize == 0) {
385 xfs_warn(mp,
386 "Realtime extent size must not be zero.");
387 return -EINVAL;
388 }
389
390 if (sbp->sb_rgextents > XFS_MAX_RGBLOCKS / sbp->sb_rextsize) {
391 xfs_warn(mp,
392 "Realtime group size (%u) must be less than %u rt extents.",
393 sbp->sb_rgextents,
394 XFS_MAX_RGBLOCKS / sbp->sb_rextsize);
395 return -EINVAL;
396 }
397
398 if (sbp->sb_rgextents < XFS_MIN_RGEXTENTS) {
399 xfs_warn(mp,
400 "Realtime group size (%u) must be at least %u rt extents.",
401 sbp->sb_rgextents, XFS_MIN_RGEXTENTS);
402 return -EINVAL;
403 }
404
405 if (sbp->sb_rgcount > XFS_MAX_RGNUMBER) {
406 xfs_warn(mp,
407 "Realtime groups (%u) must be less than %u.",
408 sbp->sb_rgcount, XFS_MAX_RGNUMBER);
409 return -EINVAL;
410 }
411
412 groups = howmany_64(sbp->sb_rextents, sbp->sb_rgextents);
413 if (groups != sbp->sb_rgcount) {
414 xfs_warn(mp,
415 "Realtime groups (%u) do not cover the entire rt section; need (%llu) groups.",
416 sbp->sb_rgcount, groups);
417 return -EINVAL;
418 }
419
420 /* Exchange-range is required for fsr to work on realtime files */
421 if (!(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE)) {
422 xfs_warn(mp,
423 "Realtime groups feature requires exchange-range support.");
424 return -EINVAL;
425 }
426
427 rgblklog = xfs_compute_rgblklog(sbp->sb_rgextents, sbp->sb_rextsize);
428 if (sbp->sb_rgblklog != rgblklog) {
429 xfs_warn(mp,
430 "Realtime group log (%d) does not match expected value (%d).",
431 sbp->sb_rgblklog, rgblklog);
432 return -EINVAL;
433 }
434
435 return 0;
436 }
437
438 /* Check the validity of the SB. */
439 STATIC int
xfs_validate_sb_common(struct xfs_mount * mp,struct xfs_buf * bp,struct xfs_sb * sbp)440 xfs_validate_sb_common(
441 struct xfs_mount *mp,
442 struct xfs_buf *bp,
443 struct xfs_sb *sbp)
444 {
445 struct xfs_dsb *dsb = bp->b_addr;
446 uint32_t agcount = 0;
447 uint32_t rem;
448 bool has_dalign;
449 int error;
450
451 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
452 xfs_warn(mp,
453 "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
454 be32_to_cpu(dsb->sb_magicnum));
455 return -EWRONGFS;
456 }
457
458 if (!xfs_sb_good_version(sbp)) {
459 xfs_warn(mp,
460 "Superblock has unknown features enabled or corrupted feature masks.");
461 return -EWRONGFS;
462 }
463
464 /*
465 * Validate feature flags and state
466 */
467 if (xfs_sb_is_v5(sbp)) {
468 if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
469 xfs_notice(mp,
470 "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
471 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
472 return -EFSCORRUPTED;
473 }
474
475 /* V5 has a separate project quota inode */
476 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
477 xfs_notice(mp,
478 "Version 5 of Super block has XFS_OQUOTA bits.");
479 return -EFSCORRUPTED;
480 }
481
482 /*
483 * Full inode chunks must be aligned to inode chunk size when
484 * sparse inodes are enabled to support the sparse chunk
485 * allocation algorithm and prevent overlapping inode records.
486 */
487 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
488 uint32_t align;
489
490 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
491 >> sbp->sb_blocklog;
492 if (sbp->sb_inoalignmt != align) {
493 xfs_warn(mp,
494 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
495 sbp->sb_inoalignmt, align);
496 return -EINVAL;
497 }
498
499 if (sbp->sb_spino_align &&
500 (sbp->sb_spino_align > sbp->sb_inoalignmt ||
501 (sbp->sb_inoalignmt % sbp->sb_spino_align) != 0)) {
502 xfs_warn(mp,
503 "Sparse inode alignment (%u) is invalid, must be integer factor of (%u).",
504 sbp->sb_spino_align,
505 sbp->sb_inoalignmt);
506 return -EINVAL;
507 }
508 } else if (sbp->sb_spino_align) {
509 xfs_warn(mp,
510 "Sparse inode alignment (%u) should be zero.",
511 sbp->sb_spino_align);
512 return -EINVAL;
513 }
514
515 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
516 if (memchr_inv(sbp->sb_pad, 0, sizeof(sbp->sb_pad))) {
517 xfs_warn(mp,
518 "Metadir superblock padding fields must be zero.");
519 return -EINVAL;
520 }
521
522 error = xfs_validate_sb_rtgroups(mp, sbp);
523 if (error)
524 return error;
525 }
526 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
527 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
528 xfs_notice(mp,
529 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
530 return -EFSCORRUPTED;
531 }
532
533 if (unlikely(
534 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
535 xfs_warn(mp,
536 "filesystem is marked as having an external log; "
537 "specify logdev on the mount command line.");
538 return -EINVAL;
539 }
540
541 if (unlikely(
542 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
543 xfs_warn(mp,
544 "filesystem is marked as having an internal log; "
545 "do not specify logdev on the mount command line.");
546 return -EINVAL;
547 }
548
549 /* Compute agcount for this number of dblocks and agblocks */
550 if (sbp->sb_agblocks) {
551 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
552 if (rem)
553 agcount++;
554 }
555
556 /*
557 * More sanity checking. Most of these were stolen directly from
558 * xfs_repair.
559 */
560 if (unlikely(
561 sbp->sb_agcount <= 0 ||
562 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
563 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
564 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
565 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
566 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
567 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
568 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
569 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
570 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
571 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
572 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
573 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
574 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
575 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
576 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
577 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
578 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
579 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
580 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
581 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
582 agcount == 0 || agcount != sbp->sb_agcount ||
583 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
584 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
585 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
586 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
587 sbp->sb_dblocks == 0 ||
588 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
589 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
590 sbp->sb_shared_vn != 0)) {
591 xfs_notice(mp, "SB sanity check failed");
592 return -EFSCORRUPTED;
593 }
594
595 /*
596 * Logs that are too large are not supported at all. Reject them
597 * outright. Logs that are too small are tolerated on v4 filesystems,
598 * but we can only check that when mounting the log. Hence we skip
599 * those checks here.
600 */
601 if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) {
602 xfs_notice(mp,
603 "Log size 0x%x blocks too large, maximum size is 0x%llx blocks",
604 sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS);
605 return -EFSCORRUPTED;
606 }
607
608 if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) {
609 xfs_warn(mp,
610 "log size 0x%llx bytes too large, maximum size is 0x%llx bytes",
611 XFS_FSB_TO_B(mp, sbp->sb_logblocks),
612 XFS_MAX_LOG_BYTES);
613 return -EFSCORRUPTED;
614 }
615
616 /*
617 * Do not allow filesystems with corrupted log sector or stripe units to
618 * be mounted. We cannot safely size the iclogs or write to the log if
619 * the log stripe unit is not valid.
620 */
621 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
622 if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) {
623 xfs_notice(mp,
624 "log sector size in bytes/log2 (0x%x/0x%x) must match",
625 sbp->sb_logsectsize, 1U << sbp->sb_logsectlog);
626 return -EFSCORRUPTED;
627 }
628 } else if (sbp->sb_logsectsize || sbp->sb_logsectlog) {
629 xfs_notice(mp,
630 "log sector size in bytes/log2 (0x%x/0x%x) are not zero",
631 sbp->sb_logsectsize, sbp->sb_logsectlog);
632 return -EFSCORRUPTED;
633 }
634
635 if (sbp->sb_logsunit > 1) {
636 if (sbp->sb_logsunit % sbp->sb_blocksize) {
637 xfs_notice(mp,
638 "log stripe unit 0x%x bytes must be a multiple of block size",
639 sbp->sb_logsunit);
640 return -EFSCORRUPTED;
641 }
642 if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) {
643 xfs_notice(mp,
644 "log stripe unit 0x%x bytes over maximum size (0x%x bytes)",
645 sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE);
646 return -EFSCORRUPTED;
647 }
648 }
649
650 if (!xfs_validate_rt_geometry(sbp)) {
651 xfs_notice(mp,
652 "realtime %sgeometry check failed",
653 sbp->sb_rblocks ? "" : "zeroed ");
654 return -EFSCORRUPTED;
655 }
656
657 /*
658 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
659 * would imply the image is corrupted.
660 */
661 has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
662 if (!!sbp->sb_unit ^ has_dalign) {
663 xfs_notice(mp, "SB stripe alignment sanity check failed");
664 return -EFSCORRUPTED;
665 }
666
667 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
668 XFS_FSB_TO_B(mp, sbp->sb_width), 0,
669 xfs_buf_daddr(bp) == XFS_SB_DADDR, false))
670 return -EFSCORRUPTED;
671
672 /*
673 * Currently only very few inode sizes are supported.
674 */
675 switch (sbp->sb_inodesize) {
676 case 256:
677 case 512:
678 case 1024:
679 case 2048:
680 break;
681 default:
682 xfs_warn(mp, "inode size of %d bytes not supported",
683 sbp->sb_inodesize);
684 return -ENOSYS;
685 }
686
687 return 0;
688 }
689
690 void
xfs_sb_quota_from_disk(struct xfs_sb * sbp)691 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
692 {
693 if (xfs_sb_is_v5(sbp) &&
694 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
695 sbp->sb_uquotino = NULLFSINO;
696 sbp->sb_gquotino = NULLFSINO;
697 sbp->sb_pquotino = NULLFSINO;
698 return;
699 }
700
701 /*
702 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
703 * leads to in-core values having two different values for a quota
704 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
705 * NULLFSINO.
706 *
707 * Note that this change affect only the in-core values. These
708 * values are not written back to disk unless any quota information
709 * is written to the disk. Even in that case, sb_pquotino field is
710 * not written to disk unless the superblock supports pquotino.
711 */
712 if (sbp->sb_uquotino == 0)
713 sbp->sb_uquotino = NULLFSINO;
714 if (sbp->sb_gquotino == 0)
715 sbp->sb_gquotino = NULLFSINO;
716 if (sbp->sb_pquotino == 0)
717 sbp->sb_pquotino = NULLFSINO;
718
719 /*
720 * We need to do these manipilations only if we are working
721 * with an older version of on-disk superblock.
722 */
723 if (xfs_sb_is_v5(sbp))
724 return;
725
726 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
727 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
728 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
729 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
730 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
731 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
732 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
733
734 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
735 sbp->sb_gquotino != NULLFSINO) {
736 /*
737 * In older version of superblock, on-disk superblock only
738 * has sb_gquotino, and in-core superblock has both sb_gquotino
739 * and sb_pquotino. But, only one of them is supported at any
740 * point of time. So, if PQUOTA is set in disk superblock,
741 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
742 * above is to make sure we don't do this twice and wipe them
743 * both out!
744 */
745 sbp->sb_pquotino = sbp->sb_gquotino;
746 sbp->sb_gquotino = NULLFSINO;
747 }
748 }
749
750 static void
__xfs_sb_from_disk(struct xfs_sb * to,struct xfs_dsb * from,bool convert_xquota)751 __xfs_sb_from_disk(
752 struct xfs_sb *to,
753 struct xfs_dsb *from,
754 bool convert_xquota)
755 {
756 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
757 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
758 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
759 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
760 to->sb_rextents = be64_to_cpu(from->sb_rextents);
761 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
762 to->sb_logstart = be64_to_cpu(from->sb_logstart);
763 to->sb_rootino = be64_to_cpu(from->sb_rootino);
764 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
765 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
766 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
767 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
768 to->sb_agcount = be32_to_cpu(from->sb_agcount);
769 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
770 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
771 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
772 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
773 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
774 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
775 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
776 to->sb_blocklog = from->sb_blocklog;
777 to->sb_sectlog = from->sb_sectlog;
778 to->sb_inodelog = from->sb_inodelog;
779 to->sb_inopblog = from->sb_inopblog;
780 to->sb_agblklog = from->sb_agblklog;
781 to->sb_rextslog = from->sb_rextslog;
782 to->sb_inprogress = from->sb_inprogress;
783 to->sb_imax_pct = from->sb_imax_pct;
784 to->sb_icount = be64_to_cpu(from->sb_icount);
785 to->sb_ifree = be64_to_cpu(from->sb_ifree);
786 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
787 to->sb_frextents = be64_to_cpu(from->sb_frextents);
788 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
789 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
790 to->sb_qflags = be16_to_cpu(from->sb_qflags);
791 to->sb_flags = from->sb_flags;
792 to->sb_shared_vn = from->sb_shared_vn;
793 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
794 to->sb_unit = be32_to_cpu(from->sb_unit);
795 to->sb_width = be32_to_cpu(from->sb_width);
796 to->sb_dirblklog = from->sb_dirblklog;
797 to->sb_logsectlog = from->sb_logsectlog;
798 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
799 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
800 to->sb_features2 = be32_to_cpu(from->sb_features2);
801 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
802 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
803 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
804 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
805 to->sb_features_log_incompat =
806 be32_to_cpu(from->sb_features_log_incompat);
807 /* crc is only used on disk, not in memory; just init to 0 here. */
808 to->sb_crc = 0;
809 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
810 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
811 to->sb_lsn = be64_to_cpu(from->sb_lsn);
812 /*
813 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
814 * feature flag is set; if not set we keep it only in memory.
815 */
816 if (xfs_sb_is_v5(to) &&
817 (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
818 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
819 else
820 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
821 /* Convert on-disk flags to in-memory flags? */
822 if (convert_xquota)
823 xfs_sb_quota_from_disk(to);
824
825 if (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
826 to->sb_metadirino = be64_to_cpu(from->sb_metadirino);
827 to->sb_rgblklog = from->sb_rgblklog;
828 memcpy(to->sb_pad, from->sb_pad, sizeof(to->sb_pad));
829 to->sb_rgcount = be32_to_cpu(from->sb_rgcount);
830 to->sb_rgextents = be32_to_cpu(from->sb_rgextents);
831 to->sb_rbmino = NULLFSINO;
832 to->sb_rsumino = NULLFSINO;
833 } else {
834 to->sb_metadirino = NULLFSINO;
835 to->sb_rgcount = 1;
836 to->sb_rgextents = 0;
837 }
838 }
839
840 void
xfs_sb_from_disk(struct xfs_sb * to,struct xfs_dsb * from)841 xfs_sb_from_disk(
842 struct xfs_sb *to,
843 struct xfs_dsb *from)
844 {
845 __xfs_sb_from_disk(to, from, true);
846 }
847
848 static void
xfs_sb_quota_to_disk(struct xfs_dsb * to,struct xfs_sb * from)849 xfs_sb_quota_to_disk(
850 struct xfs_dsb *to,
851 struct xfs_sb *from)
852 {
853 uint16_t qflags = from->sb_qflags;
854
855 if (xfs_sb_is_v5(from) &&
856 (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
857 to->sb_qflags = cpu_to_be16(from->sb_qflags);
858 to->sb_uquotino = cpu_to_be64(0);
859 to->sb_gquotino = cpu_to_be64(0);
860 to->sb_pquotino = cpu_to_be64(0);
861 return;
862 }
863
864 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
865
866 /*
867 * The in-memory superblock quota state matches the v5 on-disk format so
868 * just write them out and return
869 */
870 if (xfs_sb_is_v5(from)) {
871 to->sb_qflags = cpu_to_be16(from->sb_qflags);
872 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
873 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
874 return;
875 }
876
877 /*
878 * For older superblocks (v4), the in-core version of sb_qflags do not
879 * have XFS_OQUOTA_* flags, whereas the on-disk version does. So,
880 * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
881 */
882 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
883 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
884
885 if (from->sb_qflags &
886 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
887 qflags |= XFS_OQUOTA_ENFD;
888 if (from->sb_qflags &
889 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
890 qflags |= XFS_OQUOTA_CHKD;
891 to->sb_qflags = cpu_to_be16(qflags);
892
893 /*
894 * GQUOTINO and PQUOTINO cannot be used together in versions
895 * of superblock that do not have pquotino. from->sb_flags
896 * tells us which quota is active and should be copied to
897 * disk. If neither are active, we should NULL the inode.
898 *
899 * In all cases, the separate pquotino must remain 0 because it
900 * is beyond the "end" of the valid non-pquotino superblock.
901 */
902 if (from->sb_qflags & XFS_GQUOTA_ACCT)
903 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
904 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
905 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
906 else {
907 /*
908 * We can't rely on just the fields being logged to tell us
909 * that it is safe to write NULLFSINO - we should only do that
910 * if quotas are not actually enabled. Hence only write
911 * NULLFSINO if both in-core quota inodes are NULL.
912 */
913 if (from->sb_gquotino == NULLFSINO &&
914 from->sb_pquotino == NULLFSINO)
915 to->sb_gquotino = cpu_to_be64(NULLFSINO);
916 }
917
918 to->sb_pquotino = 0;
919 }
920
921 void
xfs_sb_to_disk(struct xfs_dsb * to,struct xfs_sb * from)922 xfs_sb_to_disk(
923 struct xfs_dsb *to,
924 struct xfs_sb *from)
925 {
926 xfs_sb_quota_to_disk(to, from);
927
928 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
929 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
930 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
931 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
932 to->sb_rextents = cpu_to_be64(from->sb_rextents);
933 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
934 to->sb_logstart = cpu_to_be64(from->sb_logstart);
935 to->sb_rootino = cpu_to_be64(from->sb_rootino);
936 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
937 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
938 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
939 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
940 to->sb_agcount = cpu_to_be32(from->sb_agcount);
941 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
942 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
943 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
944 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
945 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
946 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
947 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
948 to->sb_blocklog = from->sb_blocklog;
949 to->sb_sectlog = from->sb_sectlog;
950 to->sb_inodelog = from->sb_inodelog;
951 to->sb_inopblog = from->sb_inopblog;
952 to->sb_agblklog = from->sb_agblklog;
953 to->sb_rextslog = from->sb_rextslog;
954 to->sb_inprogress = from->sb_inprogress;
955 to->sb_imax_pct = from->sb_imax_pct;
956 to->sb_icount = cpu_to_be64(from->sb_icount);
957 to->sb_ifree = cpu_to_be64(from->sb_ifree);
958 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
959 to->sb_frextents = cpu_to_be64(from->sb_frextents);
960
961 to->sb_flags = from->sb_flags;
962 to->sb_shared_vn = from->sb_shared_vn;
963 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
964 to->sb_unit = cpu_to_be32(from->sb_unit);
965 to->sb_width = cpu_to_be32(from->sb_width);
966 to->sb_dirblklog = from->sb_dirblklog;
967 to->sb_logsectlog = from->sb_logsectlog;
968 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
969 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
970
971 /*
972 * We need to ensure that bad_features2 always matches features2.
973 * Hence we enforce that here rather than having to remember to do it
974 * everywhere else that updates features2.
975 */
976 from->sb_bad_features2 = from->sb_features2;
977 to->sb_features2 = cpu_to_be32(from->sb_features2);
978 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
979
980 if (!xfs_sb_is_v5(from))
981 return;
982
983 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
984 to->sb_features_ro_compat =
985 cpu_to_be32(from->sb_features_ro_compat);
986 to->sb_features_incompat =
987 cpu_to_be32(from->sb_features_incompat);
988 to->sb_features_log_incompat =
989 cpu_to_be32(from->sb_features_log_incompat);
990 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
991 to->sb_lsn = cpu_to_be64(from->sb_lsn);
992 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
993 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
994
995 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
996 to->sb_metadirino = cpu_to_be64(from->sb_metadirino);
997 to->sb_rgblklog = from->sb_rgblklog;
998 memset(to->sb_pad, 0, sizeof(to->sb_pad));
999 to->sb_rgcount = cpu_to_be32(from->sb_rgcount);
1000 to->sb_rgextents = cpu_to_be32(from->sb_rgextents);
1001 to->sb_rbmino = cpu_to_be64(0);
1002 to->sb_rsumino = cpu_to_be64(0);
1003 }
1004 }
1005
1006 /*
1007 * If the superblock has the CRC feature bit set or the CRC field is non-null,
1008 * check that the CRC is valid. We check the CRC field is non-null because a
1009 * single bit error could clear the feature bit and unused parts of the
1010 * superblock are supposed to be zero. Hence a non-null crc field indicates that
1011 * we've potentially lost a feature bit and we should check it anyway.
1012 *
1013 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
1014 * last field in V4 secondary superblocks. So for secondary superblocks,
1015 * we are more forgiving, and ignore CRC failures if the primary doesn't
1016 * indicate that the fs version is V5.
1017 */
1018 static void
xfs_sb_read_verify(struct xfs_buf * bp)1019 xfs_sb_read_verify(
1020 struct xfs_buf *bp)
1021 {
1022 struct xfs_sb sb;
1023 struct xfs_mount *mp = bp->b_mount;
1024 struct xfs_dsb *dsb = bp->b_addr;
1025 int error;
1026
1027 /*
1028 * open code the version check to avoid needing to convert the entire
1029 * superblock from disk order just to check the version number
1030 */
1031 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
1032 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
1033 XFS_SB_VERSION_5) ||
1034 dsb->sb_crc != 0)) {
1035
1036 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
1037 /* Only fail bad secondaries on a known V5 filesystem */
1038 if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
1039 xfs_has_crc(mp)) {
1040 error = -EFSBADCRC;
1041 goto out_error;
1042 }
1043 }
1044 }
1045
1046 /*
1047 * Check all the superblock fields. Don't byteswap the xquota flags
1048 * because _verify_common checks the on-disk values.
1049 */
1050 __xfs_sb_from_disk(&sb, dsb, false);
1051 error = xfs_validate_sb_common(mp, bp, &sb);
1052 if (error)
1053 goto out_error;
1054 error = xfs_validate_sb_read(mp, &sb);
1055
1056 out_error:
1057 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
1058 xfs_verifier_error(bp, error, __this_address);
1059 else if (error)
1060 xfs_buf_ioerror(bp, error);
1061 }
1062
1063 /*
1064 * We may be probed for a filesystem match, so we may not want to emit
1065 * messages when the superblock buffer is not actually an XFS superblock.
1066 * If we find an XFS superblock, then run a normal, noisy mount because we are
1067 * really going to mount it and want to know about errors.
1068 */
1069 static void
xfs_sb_quiet_read_verify(struct xfs_buf * bp)1070 xfs_sb_quiet_read_verify(
1071 struct xfs_buf *bp)
1072 {
1073 struct xfs_dsb *dsb = bp->b_addr;
1074
1075 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
1076 /* XFS filesystem, verify noisily! */
1077 xfs_sb_read_verify(bp);
1078 return;
1079 }
1080 /* quietly fail */
1081 xfs_buf_ioerror(bp, -EWRONGFS);
1082 }
1083
1084 static void
xfs_sb_write_verify(struct xfs_buf * bp)1085 xfs_sb_write_verify(
1086 struct xfs_buf *bp)
1087 {
1088 struct xfs_sb sb;
1089 struct xfs_mount *mp = bp->b_mount;
1090 struct xfs_buf_log_item *bip = bp->b_log_item;
1091 struct xfs_dsb *dsb = bp->b_addr;
1092 int error;
1093
1094 /*
1095 * Check all the superblock fields. Don't byteswap the xquota flags
1096 * because _verify_common checks the on-disk values.
1097 */
1098 __xfs_sb_from_disk(&sb, dsb, false);
1099 error = xfs_validate_sb_common(mp, bp, &sb);
1100 if (error)
1101 goto out_error;
1102 error = xfs_validate_sb_write(mp, bp, &sb);
1103 if (error)
1104 goto out_error;
1105
1106 if (!xfs_sb_is_v5(&sb))
1107 return;
1108
1109 if (bip)
1110 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1111
1112 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
1113 return;
1114
1115 out_error:
1116 xfs_verifier_error(bp, error, __this_address);
1117 }
1118
1119 const struct xfs_buf_ops xfs_sb_buf_ops = {
1120 .name = "xfs_sb",
1121 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
1122 .verify_read = xfs_sb_read_verify,
1123 .verify_write = xfs_sb_write_verify,
1124 };
1125
1126 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
1127 .name = "xfs_sb_quiet",
1128 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
1129 .verify_read = xfs_sb_quiet_read_verify,
1130 .verify_write = xfs_sb_write_verify,
1131 };
1132
1133 /* Compute cached rt geometry from the incore sb. */
1134 void
xfs_sb_mount_rextsize(struct xfs_mount * mp,struct xfs_sb * sbp)1135 xfs_sb_mount_rextsize(
1136 struct xfs_mount *mp,
1137 struct xfs_sb *sbp)
1138 {
1139 struct xfs_groups *rgs = &mp->m_groups[XG_TYPE_RTG];
1140
1141 mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize);
1142 mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize);
1143
1144 if (xfs_sb_is_v5(sbp) &&
1145 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
1146 rgs->blocks = sbp->sb_rgextents * sbp->sb_rextsize;
1147 rgs->blklog = mp->m_sb.sb_rgblklog;
1148 rgs->blkmask = xfs_mask32lo(mp->m_sb.sb_rgblklog);
1149 } else {
1150 rgs->blocks = 0;
1151 rgs->blklog = 0;
1152 rgs->blkmask = (uint64_t)-1;
1153 }
1154 }
1155
1156 /* Update incore sb rt extent size, then recompute the cached rt geometry. */
1157 void
xfs_mount_sb_set_rextsize(struct xfs_mount * mp,struct xfs_sb * sbp,xfs_agblock_t rextsize)1158 xfs_mount_sb_set_rextsize(
1159 struct xfs_mount *mp,
1160 struct xfs_sb *sbp,
1161 xfs_agblock_t rextsize)
1162 {
1163 sbp->sb_rextsize = rextsize;
1164 if (xfs_sb_is_v5(sbp) &&
1165 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
1166 sbp->sb_rgblklog = xfs_compute_rgblklog(sbp->sb_rgextents,
1167 rextsize);
1168
1169 xfs_sb_mount_rextsize(mp, sbp);
1170 }
1171
1172 /*
1173 * xfs_mount_common
1174 *
1175 * Mount initialization code establishing various mount
1176 * fields from the superblock associated with the given
1177 * mount structure.
1178 *
1179 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
1180 */
1181 void
xfs_sb_mount_common(struct xfs_mount * mp,struct xfs_sb * sbp)1182 xfs_sb_mount_common(
1183 struct xfs_mount *mp,
1184 struct xfs_sb *sbp)
1185 {
1186 struct xfs_groups *ags = &mp->m_groups[XG_TYPE_AG];
1187
1188 mp->m_agfrotor = 0;
1189 atomic_set(&mp->m_agirotor, 0);
1190 mp->m_maxagi = mp->m_sb.sb_agcount;
1191 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
1192 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
1193 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
1194 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
1195 mp->m_blockmask = sbp->sb_blocksize - 1;
1196 mp->m_blockwsize = xfs_rtbmblock_size(sbp) >> XFS_WORDLOG;
1197 mp->m_rtx_per_rbmblock = mp->m_blockwsize << XFS_NBWORDLOG;
1198
1199 ags->blocks = mp->m_sb.sb_agblocks;
1200 ags->blklog = mp->m_sb.sb_agblklog;
1201 ags->blkmask = xfs_mask32lo(mp->m_sb.sb_agblklog);
1202
1203 xfs_sb_mount_rextsize(mp, sbp);
1204
1205 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, true);
1206 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, false);
1207 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
1208 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
1209
1210 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, true);
1211 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, false);
1212 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
1213 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
1214
1215 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, true);
1216 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, false);
1217 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
1218 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
1219
1220 mp->m_rtrmap_mxr[0] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, true);
1221 mp->m_rtrmap_mxr[1] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, false);
1222 mp->m_rtrmap_mnr[0] = mp->m_rtrmap_mxr[0] / 2;
1223 mp->m_rtrmap_mnr[1] = mp->m_rtrmap_mxr[1] / 2;
1224
1225 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, true);
1226 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, false);
1227 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
1228 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
1229
1230 mp->m_rtrefc_mxr[0] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
1231 true);
1232 mp->m_rtrefc_mxr[1] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
1233 false);
1234 mp->m_rtrefc_mnr[0] = mp->m_rtrefc_mxr[0] / 2;
1235 mp->m_rtrefc_mnr[1] = mp->m_rtrefc_mxr[1] / 2;
1236
1237 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
1238 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
1239 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
1240 }
1241
1242 /*
1243 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
1244 * into the superblock buffer to be logged. It does not provide the higher
1245 * level of locking that is needed to protect the in-core superblock from
1246 * concurrent access.
1247 */
1248 void
xfs_log_sb(struct xfs_trans * tp)1249 xfs_log_sb(
1250 struct xfs_trans *tp)
1251 {
1252 struct xfs_mount *mp = tp->t_mountp;
1253 struct xfs_buf *bp = xfs_trans_getsb(tp);
1254
1255 /*
1256 * Lazy sb counters don't update the in-core superblock so do that now.
1257 * If this is at unmount, the counters will be exactly correct, but at
1258 * any other time they will only be ballpark correct because of
1259 * reservations that have been taken out percpu counters. If we have an
1260 * unclean shutdown, this will be corrected by log recovery rebuilding
1261 * the counters from the AGF block counts.
1262 */
1263 if (xfs_has_lazysbcount(mp)) {
1264 mp->m_sb.sb_icount = percpu_counter_sum_positive(&mp->m_icount);
1265 mp->m_sb.sb_ifree = min_t(uint64_t,
1266 percpu_counter_sum_positive(&mp->m_ifree),
1267 mp->m_sb.sb_icount);
1268 mp->m_sb.sb_fdblocks =
1269 percpu_counter_sum_positive(&mp->m_fdblocks);
1270 }
1271
1272 /*
1273 * sb_frextents was added to the lazy sb counters when the rt groups
1274 * feature was introduced. This counter can go negative due to the way
1275 * we handle nearly-lockless reservations, so we must use the _positive
1276 * variant here to avoid writing out nonsense frextents.
1277 */
1278 if (xfs_has_rtgroups(mp))
1279 mp->m_sb.sb_frextents =
1280 percpu_counter_sum_positive(&mp->m_frextents);
1281
1282 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1283 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
1284 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
1285 }
1286
1287 /*
1288 * xfs_sync_sb
1289 *
1290 * Sync the superblock to disk.
1291 *
1292 * Note that the caller is responsible for checking the frozen state of the
1293 * filesystem. This procedure uses the non-blocking transaction allocator and
1294 * thus will allow modifications to a frozen fs. This is required because this
1295 * code can be called during the process of freezing where use of the high-level
1296 * allocator would deadlock.
1297 */
1298 int
xfs_sync_sb(struct xfs_mount * mp,bool wait)1299 xfs_sync_sb(
1300 struct xfs_mount *mp,
1301 bool wait)
1302 {
1303 struct xfs_trans *tp;
1304 int error;
1305
1306 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
1307 XFS_TRANS_NO_WRITECOUNT, &tp);
1308 if (error)
1309 return error;
1310
1311 xfs_log_sb(tp);
1312 if (wait)
1313 xfs_trans_set_sync(tp);
1314 return xfs_trans_commit(tp);
1315 }
1316
1317 /*
1318 * Update all the secondary superblocks to match the new state of the primary.
1319 * Because we are completely overwriting all the existing fields in the
1320 * secondary superblock buffers, there is no need to read them in from disk.
1321 * Just get a new buffer, stamp it and write it.
1322 *
1323 * The sb buffers need to be cached here so that we serialise against other
1324 * operations that access the secondary superblocks, but we don't want to keep
1325 * them in memory once it is written so we mark it as a one-shot buffer.
1326 */
1327 int
xfs_update_secondary_sbs(struct xfs_mount * mp)1328 xfs_update_secondary_sbs(
1329 struct xfs_mount *mp)
1330 {
1331 struct xfs_perag *pag = NULL;
1332 int saved_error = 0;
1333 int error = 0;
1334 LIST_HEAD (buffer_list);
1335
1336 /* update secondary superblocks. */
1337 while ((pag = xfs_perag_next_from(mp, pag, 1))) {
1338 struct xfs_buf *bp;
1339
1340 error = xfs_buf_get(mp->m_ddev_targp,
1341 XFS_AG_DADDR(mp, pag_agno(pag), XFS_SB_DADDR),
1342 XFS_FSS_TO_BB(mp, 1), &bp);
1343 /*
1344 * If we get an error reading or writing alternate superblocks,
1345 * continue. xfs_repair chooses the "best" superblock based
1346 * on most matches; if we break early, we'll leave more
1347 * superblocks un-updated than updated, and xfs_repair may
1348 * pick them over the properly-updated primary.
1349 */
1350 if (error) {
1351 xfs_warn(mp,
1352 "error allocating secondary superblock for ag %d",
1353 pag_agno(pag));
1354 if (!saved_error)
1355 saved_error = error;
1356 continue;
1357 }
1358
1359 bp->b_ops = &xfs_sb_buf_ops;
1360 xfs_buf_oneshot(bp);
1361 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1362 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1363 xfs_buf_delwri_queue(bp, &buffer_list);
1364 xfs_buf_relse(bp);
1365
1366 /* don't hold too many buffers at once */
1367 if (pag_agno(pag) % 16)
1368 continue;
1369
1370 error = xfs_buf_delwri_submit(&buffer_list);
1371 if (error) {
1372 xfs_warn(mp,
1373 "write error %d updating a secondary superblock near ag %d",
1374 error, pag_agno(pag));
1375 if (!saved_error)
1376 saved_error = error;
1377 continue;
1378 }
1379 }
1380 error = xfs_buf_delwri_submit(&buffer_list);
1381 if (error)
1382 xfs_warn(mp, "error %d writing secondary superblocks", error);
1383 return saved_error ? saved_error : error;
1384 }
1385
1386 /*
1387 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1388 * also writes the superblock buffer to disk sector 0 immediately.
1389 */
1390 int
xfs_sync_sb_buf(struct xfs_mount * mp,bool update_rtsb)1391 xfs_sync_sb_buf(
1392 struct xfs_mount *mp,
1393 bool update_rtsb)
1394 {
1395 struct xfs_trans *tp;
1396 struct xfs_buf *bp;
1397 struct xfs_buf *rtsb_bp = NULL;
1398 int error;
1399
1400 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1401 if (error)
1402 return error;
1403
1404 bp = xfs_trans_getsb(tp);
1405 xfs_log_sb(tp);
1406 xfs_trans_bhold(tp, bp);
1407 if (update_rtsb) {
1408 rtsb_bp = xfs_log_rtsb(tp, bp);
1409 if (rtsb_bp)
1410 xfs_trans_bhold(tp, rtsb_bp);
1411 }
1412 xfs_trans_set_sync(tp);
1413 error = xfs_trans_commit(tp);
1414 if (error)
1415 goto out;
1416 /*
1417 * write out the sb buffer to get the changes to disk
1418 */
1419 error = xfs_bwrite(bp);
1420 if (!error && rtsb_bp)
1421 error = xfs_bwrite(rtsb_bp);
1422 out:
1423 if (rtsb_bp)
1424 xfs_buf_relse(rtsb_bp);
1425 xfs_buf_relse(bp);
1426 return error;
1427 }
1428
1429 void
xfs_fs_geometry(struct xfs_mount * mp,struct xfs_fsop_geom * geo,int struct_version)1430 xfs_fs_geometry(
1431 struct xfs_mount *mp,
1432 struct xfs_fsop_geom *geo,
1433 int struct_version)
1434 {
1435 struct xfs_sb *sbp = &mp->m_sb;
1436
1437 memset(geo, 0, sizeof(struct xfs_fsop_geom));
1438
1439 geo->blocksize = sbp->sb_blocksize;
1440 geo->rtextsize = sbp->sb_rextsize;
1441 geo->agblocks = sbp->sb_agblocks;
1442 geo->agcount = sbp->sb_agcount;
1443 geo->logblocks = sbp->sb_logblocks;
1444 geo->sectsize = sbp->sb_sectsize;
1445 geo->inodesize = sbp->sb_inodesize;
1446 geo->imaxpct = sbp->sb_imax_pct;
1447 geo->datablocks = sbp->sb_dblocks;
1448 geo->rtblocks = sbp->sb_rblocks;
1449 geo->rtextents = sbp->sb_rextents;
1450 geo->logstart = sbp->sb_logstart;
1451 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1452 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1453
1454 if (struct_version < 2)
1455 return;
1456
1457 geo->sunit = sbp->sb_unit;
1458 geo->swidth = sbp->sb_width;
1459
1460 if (struct_version < 3)
1461 return;
1462
1463 geo->version = XFS_FSOP_GEOM_VERSION;
1464 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1465 XFS_FSOP_GEOM_FLAGS_DIRV2 |
1466 XFS_FSOP_GEOM_FLAGS_EXTFLG;
1467 if (xfs_has_attr(mp))
1468 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1469 if (xfs_has_quota(mp))
1470 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1471 if (xfs_has_align(mp))
1472 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1473 if (xfs_has_dalign(mp))
1474 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1475 if (xfs_has_asciici(mp))
1476 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1477 if (xfs_has_lazysbcount(mp))
1478 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1479 if (xfs_has_attr2(mp))
1480 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1481 if (xfs_has_projid32(mp))
1482 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1483 if (xfs_has_crc(mp))
1484 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1485 if (xfs_has_ftype(mp))
1486 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1487 if (xfs_has_finobt(mp))
1488 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1489 if (xfs_has_sparseinodes(mp))
1490 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1491 if (xfs_has_rmapbt(mp))
1492 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1493 if (xfs_has_reflink(mp))
1494 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1495 if (xfs_has_bigtime(mp))
1496 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
1497 if (xfs_has_inobtcounts(mp))
1498 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
1499 if (xfs_has_parent(mp))
1500 geo->flags |= XFS_FSOP_GEOM_FLAGS_PARENT;
1501 if (xfs_has_sector(mp)) {
1502 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1503 geo->logsectsize = sbp->sb_logsectsize;
1504 } else {
1505 geo->logsectsize = BBSIZE;
1506 }
1507 if (xfs_has_large_extent_counts(mp))
1508 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
1509 if (xfs_has_exchange_range(mp))
1510 geo->flags |= XFS_FSOP_GEOM_FLAGS_EXCHANGE_RANGE;
1511 if (xfs_has_metadir(mp))
1512 geo->flags |= XFS_FSOP_GEOM_FLAGS_METADIR;
1513 geo->rtsectsize = sbp->sb_blocksize;
1514 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1515
1516 if (struct_version < 4)
1517 return;
1518
1519 if (xfs_has_logv2(mp))
1520 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1521
1522 geo->logsunit = sbp->sb_logsunit;
1523
1524 if (struct_version < 5)
1525 return;
1526
1527 geo->version = XFS_FSOP_GEOM_VERSION_V5;
1528
1529 if (xfs_has_rtgroups(mp)) {
1530 geo->rgcount = sbp->sb_rgcount;
1531 geo->rgextents = sbp->sb_rgextents;
1532 }
1533 }
1534
1535 /* Read a secondary superblock. */
1536 int
xfs_sb_read_secondary(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_buf ** bpp)1537 xfs_sb_read_secondary(
1538 struct xfs_mount *mp,
1539 struct xfs_trans *tp,
1540 xfs_agnumber_t agno,
1541 struct xfs_buf **bpp)
1542 {
1543 struct xfs_buf *bp;
1544 int error;
1545
1546 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1547 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1548 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1549 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1550 if (xfs_metadata_is_sick(error))
1551 xfs_agno_mark_sick(mp, agno, XFS_SICK_AG_SB);
1552 if (error)
1553 return error;
1554 xfs_buf_set_ref(bp, XFS_SSB_REF);
1555 *bpp = bp;
1556 return 0;
1557 }
1558
1559 /* Get an uninitialised secondary superblock buffer. */
1560 int
xfs_sb_get_secondary(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_buf ** bpp)1561 xfs_sb_get_secondary(
1562 struct xfs_mount *mp,
1563 struct xfs_trans *tp,
1564 xfs_agnumber_t agno,
1565 struct xfs_buf **bpp)
1566 {
1567 struct xfs_buf *bp;
1568 int error;
1569
1570 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1571 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1572 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1573 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1574 if (error)
1575 return error;
1576 bp->b_ops = &xfs_sb_buf_ops;
1577 xfs_buf_oneshot(bp);
1578 *bpp = bp;
1579 return 0;
1580 }
1581
1582 /*
1583 * sunit, swidth, sectorsize(optional with 0) should be all in bytes, so users
1584 * won't be confused by values in error messages. This function returns false
1585 * if the stripe geometry is invalid and the caller is unable to repair the
1586 * stripe configuration later in the mount process.
1587 */
1588 bool
xfs_validate_stripe_geometry(struct xfs_mount * mp,__s64 sunit,__s64 swidth,int sectorsize,bool may_repair,bool silent)1589 xfs_validate_stripe_geometry(
1590 struct xfs_mount *mp,
1591 __s64 sunit,
1592 __s64 swidth,
1593 int sectorsize,
1594 bool may_repair,
1595 bool silent)
1596 {
1597 if (swidth > INT_MAX) {
1598 if (!silent)
1599 xfs_notice(mp,
1600 "stripe width (%lld) is too large", swidth);
1601 goto check_override;
1602 }
1603
1604 if (sunit > swidth) {
1605 if (!silent)
1606 xfs_notice(mp,
1607 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1608 goto check_override;
1609 }
1610
1611 if (sectorsize && (int)sunit % sectorsize) {
1612 if (!silent)
1613 xfs_notice(mp,
1614 "stripe unit (%lld) must be a multiple of the sector size (%d)",
1615 sunit, sectorsize);
1616 goto check_override;
1617 }
1618
1619 if (sunit && !swidth) {
1620 if (!silent)
1621 xfs_notice(mp,
1622 "invalid stripe unit (%lld) and stripe width of 0", sunit);
1623 goto check_override;
1624 }
1625
1626 if (!sunit && swidth) {
1627 if (!silent)
1628 xfs_notice(mp,
1629 "invalid stripe width (%lld) and stripe unit of 0", swidth);
1630 goto check_override;
1631 }
1632
1633 if (sunit && (int)swidth % (int)sunit) {
1634 if (!silent)
1635 xfs_notice(mp,
1636 "stripe width (%lld) must be a multiple of the stripe unit (%lld)",
1637 swidth, sunit);
1638 goto check_override;
1639 }
1640 return true;
1641
1642 check_override:
1643 if (!may_repair)
1644 return false;
1645 /*
1646 * During mount, mp->m_dalign will not be set unless the sunit mount
1647 * option was set. If it was set, ignore the bad stripe alignment values
1648 * and allow the validation and overwrite later in the mount process to
1649 * attempt to overwrite the bad stripe alignment values with the values
1650 * supplied by mount options.
1651 */
1652 if (!mp->m_dalign)
1653 return false;
1654 if (!silent)
1655 xfs_notice(mp,
1656 "Will try to correct with specified mount options sunit (%d) and swidth (%d)",
1657 BBTOB(mp->m_dalign), BBTOB(mp->m_swidth));
1658 return true;
1659 }
1660
1661 /*
1662 * Compute the maximum level number of the realtime summary file, as defined by
1663 * mkfs. The historic use of highbit32 on a 64-bit quantity prohibited correct
1664 * use of rt volumes with more than 2^32 extents.
1665 */
1666 uint8_t
xfs_compute_rextslog(xfs_rtbxlen_t rtextents)1667 xfs_compute_rextslog(
1668 xfs_rtbxlen_t rtextents)
1669 {
1670 if (!rtextents)
1671 return 0;
1672 return xfs_highbit64(rtextents);
1673 }
1674