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