]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_sb.c
libxfs: add helper for verifying checksums on xfs_bufs
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_sb.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
da23017d 3 * All Rights Reserved.
2bd0ea18 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18
NS
7 * published by the Free Software Foundation.
8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
2bd0ea18 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18 17 */
2bd0ea18
NS
18#include <xfs.h>
19
4896e6c8
DC
20/*
21 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
22 */
23
5e656dbb
BN
24static const struct {
25 short offset;
26 short type; /* 0 = integer
27 * 1 = binary / string (no translation)
28 */
2bd0ea18 29} xfs_sb_info[] = {
4896e6c8
DC
30 { offsetof(xfs_sb_t, sb_magicnum), 0 },
31 { offsetof(xfs_sb_t, sb_blocksize), 0 },
32 { offsetof(xfs_sb_t, sb_dblocks), 0 },
33 { offsetof(xfs_sb_t, sb_rblocks), 0 },
34 { offsetof(xfs_sb_t, sb_rextents), 0 },
35 { offsetof(xfs_sb_t, sb_uuid), 1 },
36 { offsetof(xfs_sb_t, sb_logstart), 0 },
37 { offsetof(xfs_sb_t, sb_rootino), 0 },
38 { offsetof(xfs_sb_t, sb_rbmino), 0 },
39 { offsetof(xfs_sb_t, sb_rsumino), 0 },
40 { offsetof(xfs_sb_t, sb_rextsize), 0 },
41 { offsetof(xfs_sb_t, sb_agblocks), 0 },
42 { offsetof(xfs_sb_t, sb_agcount), 0 },
43 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
44 { offsetof(xfs_sb_t, sb_logblocks), 0 },
45 { offsetof(xfs_sb_t, sb_versionnum), 0 },
46 { offsetof(xfs_sb_t, sb_sectsize), 0 },
47 { offsetof(xfs_sb_t, sb_inodesize), 0 },
48 { offsetof(xfs_sb_t, sb_inopblock), 0 },
49 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
50 { offsetof(xfs_sb_t, sb_blocklog), 0 },
51 { offsetof(xfs_sb_t, sb_sectlog), 0 },
52 { offsetof(xfs_sb_t, sb_inodelog), 0 },
53 { offsetof(xfs_sb_t, sb_inopblog), 0 },
54 { offsetof(xfs_sb_t, sb_agblklog), 0 },
55 { offsetof(xfs_sb_t, sb_rextslog), 0 },
56 { offsetof(xfs_sb_t, sb_inprogress), 0 },
57 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
58 { offsetof(xfs_sb_t, sb_icount), 0 },
59 { offsetof(xfs_sb_t, sb_ifree), 0 },
60 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
61 { offsetof(xfs_sb_t, sb_frextents), 0 },
62 { offsetof(xfs_sb_t, sb_uquotino), 0 },
63 { offsetof(xfs_sb_t, sb_gquotino), 0 },
64 { offsetof(xfs_sb_t, sb_qflags), 0 },
65 { offsetof(xfs_sb_t, sb_flags), 0 },
66 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
67 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
68 { offsetof(xfs_sb_t, sb_unit), 0 },
69 { offsetof(xfs_sb_t, sb_width), 0 },
70 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
71 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
72 { offsetof(xfs_sb_t, sb_logsectsize), 0 },
73 { offsetof(xfs_sb_t, sb_logsunit), 0 },
74 { offsetof(xfs_sb_t, sb_features2), 0 },
75 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
76 { offsetof(xfs_sb_t, sb_features_compat), 0 },
77 { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
78 { offsetof(xfs_sb_t, sb_features_incompat), 0 },
79 { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
80 { offsetof(xfs_sb_t, sb_crc), 0 },
81 { offsetof(xfs_sb_t, sb_pad), 0 },
82 { offsetof(xfs_sb_t, sb_pquotino), 0 },
83 { offsetof(xfs_sb_t, sb_lsn), 0 },
84 { sizeof(xfs_sb_t), 0 }
2bd0ea18
NS
85};
86
56b2de80
DC
87/*
88 * Reference counting access wrappers to the perag structures.
89 * Because we never free per-ag structures, the only thing we
90 * have to protect against changes is the tree structure itself.
91 */
92struct xfs_perag *
4896e6c8
DC
93xfs_perag_get(
94 struct xfs_mount *mp,
95 xfs_agnumber_t agno)
a33a9e62 96{
56b2de80
DC
97 struct xfs_perag *pag;
98 int ref = 0;
99
100 rcu_read_lock();
101 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
102 if (pag) {
103 ASSERT(atomic_read(&pag->pag_ref) >= 0);
104 ref = atomic_inc_return(&pag->pag_ref);
a33a9e62 105 }
56b2de80 106 rcu_read_unlock();
a2ceac1f 107 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
56b2de80
DC
108 return pag;
109}
a33a9e62 110
4896e6c8
DC
111/*
112 * search from @first to find the next perag with the given tag set.
113 */
114struct xfs_perag *
115xfs_perag_get_tag(
116 struct xfs_mount *mp,
117 xfs_agnumber_t first,
118 int tag)
119{
120 struct xfs_perag *pag;
121 int found;
122 int ref;
123
124 rcu_read_lock();
125 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
126 (void **)&pag, first, 1, tag);
127 if (found <= 0) {
128 rcu_read_unlock();
129 return NULL;
130 }
131 ref = atomic_inc_return(&pag->pag_ref);
132 rcu_read_unlock();
133 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
134 return pag;
135}
136
56b2de80 137void
4896e6c8
DC
138xfs_perag_put(
139 struct xfs_perag *pag)
56b2de80
DC
140{
141 int ref;
a33a9e62 142
56b2de80
DC
143 ASSERT(atomic_read(&pag->pag_ref) > 0);
144 ref = atomic_dec_return(&pag->pag_ref);
145 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
a33a9e62 146}
cdded3d8 147
a2ceac1f
DC
148/*
149 * Check the validity of the SB found.
150 */
151STATIC int
152xfs_mount_validate_sb(
153 xfs_mount_t *mp,
154 xfs_sb_t *sbp,
5629f836
DC
155 bool check_inprogress,
156 bool check_version)
a2ceac1f
DC
157{
158
159 /*
160 * If the log device and data device have the
161 * same device number, the log is internal.
162 * Consequently, the sb_logstart should be non-zero. If
163 * we have a zero sb_logstart in this case, we may be trying to mount
164 * a volume filesystem in a non-volume manner.
165 */
166 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
167 xfs_warn(mp, "bad magic number");
168 return XFS_ERROR(EWRONGFS);
169 }
170
5565d79a 171
a2ceac1f
DC
172 if (!xfs_sb_good_version(sbp)) {
173 xfs_warn(mp, "bad version");
174 return XFS_ERROR(EWRONGFS);
175 }
176
5565d79a 177 /*
6567f1f9 178 * Version 5 superblock feature mask validation. Reject combinations the
4896e6c8
DC
179 * kernel cannot support up front before checking anything else. For
180 * write validation, we don't need to check feature masks.
5565d79a 181 */
5629f836 182 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
6567f1f9
DC
183 if (xfs_sb_has_compat_feature(sbp,
184 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
185 xfs_warn(mp,
186"Superblock has unknown compatible features (0x%x) enabled.\n"
187"Using a more recent xfsprogs is recommended.",
188 (sbp->sb_features_compat &
189 XFS_SB_FEAT_COMPAT_UNKNOWN));
190 }
191
192 if (xfs_sb_has_ro_compat_feature(sbp,
193 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
194 xfs_warn(mp,
195"Superblock has unknown read-only compatible features (0x%x) enabled.\n"
196"Using a more recent xfsprogs is recommended.",
197 (sbp->sb_features_ro_compat &
198 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
199 }
200 if (xfs_sb_has_incompat_feature(sbp,
201 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
202 xfs_warn(mp,
203"Superblock has unknown incompatible features (0x%x) enabled.\n"
204"Filesystem can not be safely operated on by this xfsprogs installation",
205 (sbp->sb_features_incompat &
206 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
207 return XFS_ERROR(EINVAL);
208 }
5565d79a
DC
209 }
210
4896e6c8
DC
211 if (xfs_sb_version_has_pquotino(sbp)) {
212 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
213 xfs_notice(mp,
12864fd9 214 "Version 5 of Super block has XFS_OQUOTA bits.");
4896e6c8
DC
215 return XFS_ERROR(EFSCORRUPTED);
216 }
217 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
218 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
219 xfs_notice(mp,
12864fd9 220"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
4896e6c8 221 return XFS_ERROR(EFSCORRUPTED);
342aef1e
CS
222 }
223
a2ceac1f 224 if (unlikely(
6567f1f9 225 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
a2ceac1f
DC
226 xfs_warn(mp,
227 "filesystem is marked as having an external log; "
228 "specify logdev on the mount command line.");
229 return XFS_ERROR(EINVAL);
230 }
231
232 if (unlikely(
6567f1f9 233 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
a2ceac1f
DC
234 xfs_warn(mp,
235 "filesystem is marked as having an internal log; "
236 "do not specify logdev on the mount command line.");
237 return XFS_ERROR(EINVAL);
238 }
239
240 /*
241 * More sanity checking. Most of these were stolen directly from
242 * xfs_repair.
243 */
244 if (unlikely(
245 sbp->sb_agcount <= 0 ||
246 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
247 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
248 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
249 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
250 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
251 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
252 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
253 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
254 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
255 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
256 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
257 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
258 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
259 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
260 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
4b0c789d 261 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
a2ceac1f
DC
262 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
263 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
264 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
265 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
266 sbp->sb_dblocks == 0 ||
267 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
268 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
ba5027e0 269 xfs_notice(mp, "SB sanity check failed");
a2ceac1f
DC
270 return XFS_ERROR(EFSCORRUPTED);
271 }
272
273 /*
274 * Currently only very few inode sizes are supported.
275 */
276 switch (sbp->sb_inodesize) {
277 case 256:
278 case 512:
279 case 1024:
280 case 2048:
281 break;
282 default:
283 xfs_warn(mp, "inode size of %d bytes not supported",
284 sbp->sb_inodesize);
285 return XFS_ERROR(ENOSYS);
286 }
287
4896e6c8
DC
288 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
289 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
290 xfs_warn(mp,
291 "file system too large to be mounted on this system.");
292 return XFS_ERROR(EFBIG);
293 }
294
a2ceac1f
DC
295 /*
296 * Version 1 directory format has never worked on Linux.
297 */
298 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
299 xfs_warn(mp, "file system using version 1 directory format");
300 return XFS_ERROR(ENOSYS);
301 }
302
303 return 0;
304}
305
4896e6c8
DC
306void
307xfs_sb_quota_from_disk(struct xfs_sb *sbp)
308{
309 /*
310 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
311 * leads to in-core values having two different values for a quota
312 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
313 * NULLFSINO.
314 *
315 * Note that this change affect only the in-core values. These
316 * values are not written back to disk unless any quota information
317 * is written to the disk. Even in that case, sb_pquotino field is
318 * not written to disk unless the superblock supports pquotino.
319 */
320 if (sbp->sb_uquotino == 0)
321 sbp->sb_uquotino = NULLFSINO;
322 if (sbp->sb_gquotino == 0)
323 sbp->sb_gquotino = NULLFSINO;
324 if (sbp->sb_pquotino == 0)
325 sbp->sb_pquotino = NULLFSINO;
326
327 /*
328 * We need to do these manipilations only if we are working
329 * with an older version of on-disk superblock.
330 */
331 if (xfs_sb_version_has_pquotino(sbp))
332 return;
333
334 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
335 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
336 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
337 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
338 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
339 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
340 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
341
342 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
343 /*
344 * In older version of superblock, on-disk superblock only
345 * has sb_gquotino, and in-core superblock has both sb_gquotino
346 * and sb_pquotino. But, only one of them is supported at any
347 * point of time. So, if PQUOTA is set in disk superblock,
348 * copy over sb_gquotino to sb_pquotino.
349 */
350 sbp->sb_pquotino = sbp->sb_gquotino;
351 sbp->sb_gquotino = NULLFSINO;
352 }
353}
354
5e656dbb
BN
355void
356xfs_sb_from_disk(
4896e6c8 357 struct xfs_sb *to,
5e656dbb
BN
358 xfs_dsb_t *from)
359{
360 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
361 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
362 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
363 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
364 to->sb_rextents = be64_to_cpu(from->sb_rextents);
365 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
366 to->sb_logstart = be64_to_cpu(from->sb_logstart);
367 to->sb_rootino = be64_to_cpu(from->sb_rootino);
368 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
369 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
370 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
371 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
372 to->sb_agcount = be32_to_cpu(from->sb_agcount);
373 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
374 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
375 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
376 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
377 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
378 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
379 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
380 to->sb_blocklog = from->sb_blocklog;
381 to->sb_sectlog = from->sb_sectlog;
382 to->sb_inodelog = from->sb_inodelog;
383 to->sb_inopblog = from->sb_inopblog;
384 to->sb_agblklog = from->sb_agblklog;
385 to->sb_rextslog = from->sb_rextslog;
386 to->sb_inprogress = from->sb_inprogress;
387 to->sb_imax_pct = from->sb_imax_pct;
388 to->sb_icount = be64_to_cpu(from->sb_icount);
389 to->sb_ifree = be64_to_cpu(from->sb_ifree);
390 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
391 to->sb_frextents = be64_to_cpu(from->sb_frextents);
392 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
393 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
394 to->sb_qflags = be16_to_cpu(from->sb_qflags);
395 to->sb_flags = from->sb_flags;
396 to->sb_shared_vn = from->sb_shared_vn;
397 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
398 to->sb_unit = be32_to_cpu(from->sb_unit);
399 to->sb_width = be32_to_cpu(from->sb_width);
400 to->sb_dirblklog = from->sb_dirblklog;
401 to->sb_logsectlog = from->sb_logsectlog;
402 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
403 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
404 to->sb_features2 = be32_to_cpu(from->sb_features2);
405 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
5565d79a
DC
406 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
407 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
408 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
6567f1f9
DC
409 to->sb_features_log_incompat =
410 be32_to_cpu(from->sb_features_log_incompat);
411 to->sb_pad = 0;
5565d79a
DC
412 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
413 to->sb_lsn = be64_to_cpu(from->sb_lsn);
5e656dbb
BN
414}
415
342aef1e
CS
416static inline void
417xfs_sb_quota_to_disk(
418 xfs_dsb_t *to,
419 xfs_sb_t *from,
420 __int64_t *fields)
421{
422 __uint16_t qflags = from->sb_qflags;
423
0340d706
CS
424 /*
425 * We need to do these manipilations only if we are working
426 * with an older version of on-disk superblock.
427 */
428 if (xfs_sb_version_has_pquotino(from))
429 return;
430
342aef1e
CS
431 if (*fields & XFS_SB_QFLAGS) {
432 /*
433 * The in-core version of sb_qflags do not have
434 * XFS_OQUOTA_* flags, whereas the on-disk version
435 * does. So, convert incore XFS_{PG}QUOTA_* flags
436 * to on-disk XFS_OQUOTA_* flags.
437 */
438 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
439 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
440
441 if (from->sb_qflags &
442 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
443 qflags |= XFS_OQUOTA_ENFD;
444 if (from->sb_qflags &
445 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
446 qflags |= XFS_OQUOTA_CHKD;
447 to->sb_qflags = cpu_to_be16(qflags);
448 *fields &= ~XFS_SB_QFLAGS;
449 }
0340d706
CS
450
451 /*
452 * GQUOTINO and PQUOTINO cannot be used together in versions
453 * of superblock that do not have pquotino. from->sb_flags
454 * tells us which quota is active and should be copied to
455 * disk.
456 */
457 if ((*fields & XFS_SB_GQUOTINO) &&
458 (from->sb_qflags & XFS_GQUOTA_ACCT))
459 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
460 else if ((*fields & XFS_SB_PQUOTINO) &&
461 (from->sb_qflags & XFS_PQUOTA_ACCT))
462 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
463
464 *fields &= ~(XFS_SB_PQUOTINO | XFS_SB_GQUOTINO);
342aef1e
CS
465}
466
5e656dbb
BN
467/*
468 * Copy in core superblock to ondisk one.
469 *
470 * The fields argument is mask of superblock fields to copy.
471 */
472void
473xfs_sb_to_disk(
474 xfs_dsb_t *to,
475 xfs_sb_t *from,
476 __int64_t fields)
477{
478 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
479 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
480 xfs_sb_field_t f;
481 int first;
482 int size;
483
484 ASSERT(fields);
485 if (!fields)
486 return;
487
342aef1e 488 xfs_sb_quota_to_disk(to, from, &fields);
5e656dbb
BN
489 while (fields) {
490 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
491 first = xfs_sb_info[f].offset;
492 size = xfs_sb_info[f + 1].offset - first;
493
494 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
495
496 if (size == 1 || xfs_sb_info[f].type == 1) {
497 memcpy(to_ptr + first, from_ptr + first, size);
498 } else {
499 switch (size) {
500 case 2:
501 *(__be16 *)(to_ptr + first) =
4896e6c8 502 cpu_to_be16(*(__u16 *)(from_ptr + first));
5e656dbb
BN
503 break;
504 case 4:
505 *(__be32 *)(to_ptr + first) =
4896e6c8 506 cpu_to_be32(*(__u32 *)(from_ptr + first));
5e656dbb
BN
507 break;
508 case 8:
509 *(__be64 *)(to_ptr + first) =
4896e6c8 510 cpu_to_be64(*(__u64 *)(from_ptr + first));
5e656dbb
BN
511 break;
512 default:
513 ASSERT(0);
514 }
515 }
516
517 fields &= ~(1LL << f);
518 }
519}
520
5565d79a 521static int
a2ceac1f 522xfs_sb_verify(
f7b80291 523 struct xfs_buf *bp,
5629f836 524 bool check_version)
a2ceac1f
DC
525{
526 struct xfs_mount *mp = bp->b_target->bt_mount;
527 struct xfs_sb sb;
a2ceac1f
DC
528
529 xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
530
531 /*
532 * Only check the in progress field for the primary superblock as
533 * mkfs.xfs doesn't clear it from secondary superblocks.
534 */
5629f836
DC
535 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
536 check_version);
a2ceac1f
DC
537}
538
5565d79a
DC
539/*
540 * If the superblock has the CRC feature bit set or the CRC field is non-null,
541 * check that the CRC is valid. We check the CRC field is non-null because a
542 * single bit error could clear the feature bit and unused parts of the
543 * superblock are supposed to be zero. Hence a non-null crc field indicates that
544 * we've potentially lost a feature bit and we should check it anyway.
46ea3a62
DC
545 *
546 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
547 * last field in V4 secondary superblocks. So for secondary superblocks,
548 * we are more forgiving, and ignore CRC failures if the primary doesn't
549 * indicate that the fs version is V5.
5565d79a 550 */
a2ceac1f
DC
551static void
552xfs_sb_read_verify(
553 struct xfs_buf *bp)
554{
5565d79a
DC
555 struct xfs_mount *mp = bp->b_target->bt_mount;
556 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
557 int error;
558
559 /*
560 * open code the version check to avoid needing to convert the entire
561 * superblock from disk order just to check the version number
562 */
563 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
564 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
565 XFS_SB_VERSION_5) ||
566 dsb->sb_crc != 0)) {
567
d21ca64d 568 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
46ea3a62 569 /* Only fail bad secondaries on a known V5 filesystem */
f3cddd91 570 if (bp->b_bn == XFS_SB_DADDR ||
46ea3a62
DC
571 xfs_sb_version_hascrc(&mp->m_sb)) {
572 error = EFSCORRUPTED;
573 goto out_error;
574 }
5565d79a
DC
575 }
576 }
f7b80291 577 error = xfs_sb_verify(bp, true);
5565d79a
DC
578
579out_error:
580 if (error) {
ba5027e0 581 if (error == EFSCORRUPTED)
6aa3d87b
DC
582 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
583 mp, bp->b_addr);
5565d79a
DC
584 xfs_buf_ioerror(bp, error);
585 }
a2ceac1f
DC
586}
587
588/*
589 * We may be probed for a filesystem match, so we may not want to emit
590 * messages when the superblock buffer is not actually an XFS superblock.
4896e6c8 591 * If we find an XFS superblock, then run a normal, noisy mount because we are
a2ceac1f
DC
592 * really going to mount it and want to know about errors.
593 */
594static void
595xfs_sb_quiet_read_verify(
596 struct xfs_buf *bp)
597{
5565d79a 598 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
a2ceac1f 599
5565d79a 600 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
a2ceac1f
DC
601 /* XFS filesystem, verify noisily! */
602 xfs_sb_read_verify(bp);
603 return;
604 }
605 /* quietly fail */
49f693fa 606 xfs_buf_ioerror(bp, EWRONGFS);
a2ceac1f
DC
607}
608
609static void
610xfs_sb_write_verify(
5565d79a 611 struct xfs_buf *bp)
a2ceac1f 612{
5565d79a
DC
613 struct xfs_mount *mp = bp->b_target->bt_mount;
614 struct xfs_buf_log_item *bip = bp->b_fspriv;
615 int error;
616
f7b80291 617 error = xfs_sb_verify(bp, false);
5565d79a 618 if (error) {
4896e6c8
DC
619 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
620 mp, bp->b_addr);
5565d79a
DC
621 xfs_buf_ioerror(bp, error);
622 return;
623 }
624
625 if (!xfs_sb_version_hascrc(&mp->m_sb))
626 return;
627
628 if (bip)
629 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
630
3faddc54 631 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_SB_CRC_OFF);
a2ceac1f
DC
632}
633
634const struct xfs_buf_ops xfs_sb_buf_ops = {
635 .verify_read = xfs_sb_read_verify,
636 .verify_write = xfs_sb_write_verify,
637};
638
4896e6c8 639const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
a2ceac1f
DC
640 .verify_read = xfs_sb_quiet_read_verify,
641 .verify_write = xfs_sb_write_verify,
642};
643
5e656dbb
BN
644/*
645 * xfs_mount_common
646 *
647 * Mount initialization code establishing various mount
648 * fields from the superblock associated with the given
649 * mount structure
5e656dbb
BN
650 */
651void
4896e6c8
DC
652xfs_sb_mount_common(
653 struct xfs_mount *mp,
654 struct xfs_sb *sbp)
5e656dbb 655{
5e656dbb
BN
656 mp->m_agfrotor = mp->m_agirotor = 0;
657 spin_lock_init(&mp->m_agirotor_lock);
658 mp->m_maxagi = mp->m_sb.sb_agcount;
659 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
660 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
661 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
662 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
663 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
5e656dbb
BN
664 mp->m_blockmask = sbp->sb_blocksize - 1;
665 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
666 mp->m_blockwmask = mp->m_blockwsize - 1;
5e656dbb 667
b3563c19
BN
668 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
669 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
670 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
671 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
672
673 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
674 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
675 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
676 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
677
678 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
679 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
680 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
681 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
5e656dbb
BN
682
683 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
684 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
685 sbp->sb_inopblock);
686 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
687}
688
cdded3d8
DC
689/*
690 * xfs_initialize_perag_data
691 *
692 * Read in each per-ag structure so we can count up the number of
693 * allocated inodes, free inodes and used filesystem blocks as this
694 * information is no longer persistent in the superblock. Once we have
695 * this information, write it into the in-core superblock structure.
696 */
5e656dbb 697int
4896e6c8
DC
698xfs_initialize_perag_data(
699 struct xfs_mount *mp,
700 xfs_agnumber_t agcount)
cdded3d8
DC
701{
702 xfs_agnumber_t index;
703 xfs_perag_t *pag;
704 xfs_sb_t *sbp = &mp->m_sb;
5e656dbb
BN
705 uint64_t ifree = 0;
706 uint64_t ialloc = 0;
707 uint64_t bfree = 0;
708 uint64_t bfreelst = 0;
709 uint64_t btree = 0;
cdded3d8 710 int error;
cdded3d8
DC
711
712 for (index = 0; index < agcount; index++) {
713 /*
714 * read the agf, then the agi. This gets us
56b2de80 715 * all the information we need and populates the
cdded3d8
DC
716 * per-ag structures for us.
717 */
718 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
719 if (error)
720 return error;
721
722 error = xfs_ialloc_pagi_init(mp, NULL, index);
723 if (error)
724 return error;
56b2de80 725 pag = xfs_perag_get(mp, index);
cdded3d8
DC
726 ifree += pag->pagi_freecount;
727 ialloc += pag->pagi_count;
728 bfree += pag->pagf_freeblks;
729 bfreelst += pag->pagf_flcount;
730 btree += pag->pagf_btreeblks;
56b2de80 731 xfs_perag_put(pag);
cdded3d8
DC
732 }
733 /*
734 * Overwrite incore superblock counters with just-read data
735 */
5e656dbb 736 spin_lock(&mp->m_sb_lock);
cdded3d8
DC
737 sbp->sb_ifree = ifree;
738 sbp->sb_icount = ialloc;
739 sbp->sb_fdblocks = bfree + bfreelst + btree;
5e656dbb
BN
740 spin_unlock(&mp->m_sb_lock);
741
742 /* Fixup the per-cpu counters as well. */
743 xfs_icsb_reinit_counters(mp);
744
cdded3d8
DC
745 return 0;
746}
5e656dbb
BN
747
748/*
749 * xfs_mod_sb() can be used to copy arbitrary changes to the
750 * in-core superblock into the superblock buffer to be logged.
751 * It does not provide the higher level of locking that is
752 * needed to protect the in-core superblock from concurrent
753 * access.
754 */
755void
756xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
757{
758 xfs_buf_t *bp;
759 int first;
760 int last;
761 xfs_mount_t *mp;
762 xfs_sb_field_t f;
763
764 ASSERT(fields);
765 if (!fields)
766 return;
767 mp = tp->t_mountp;
768 bp = xfs_trans_getsb(tp, mp, 0);
769 first = sizeof(xfs_sb_t);
770 last = 0;
771
772 /* translate/copy */
4896e6c8 773
5e656dbb
BN
774 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
775
776 /* find modified range */
56b2de80
DC
777 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
778 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
779 last = xfs_sb_info[f + 1].offset - 1;
5e656dbb
BN
780
781 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
782 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
783 first = xfs_sb_info[f].offset;
784
5565d79a 785 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
5e656dbb
BN
786 xfs_trans_log_buf(tp, bp, first, last);
787}