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