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