]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/libxfs/xfs_sb.c
xfs: validate transaction header length on log recovery
[mirror_ubuntu-artful-kernel.git] / fs / xfs / libxfs / xfs_sb.c
CommitLineData
ff55068c
DC
1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
632b89e8 20#include "xfs_shared.h"
ff55068c 21#include "xfs_format.h"
239880ef
DC
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
ff55068c 24#include "xfs_bit.h"
ff55068c 25#include "xfs_sb.h"
ff55068c 26#include "xfs_mount.h"
ff55068c 27#include "xfs_inode.h"
ff55068c
DC
28#include "xfs_ialloc.h"
29#include "xfs_alloc.h"
ff55068c 30#include "xfs_error.h"
ff55068c
DC
31#include "xfs_trace.h"
32#include "xfs_cksum.h"
239880ef 33#include "xfs_trans.h"
ff55068c 34#include "xfs_buf_item.h"
a4fbe6ab
DC
35#include "xfs_bmap_btree.h"
36#include "xfs_alloc_btree.h"
37#include "xfs_ialloc_btree.h"
ff55068c
DC
38
39/*
40 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
41 */
42
ff55068c
DC
43/*
44 * Reference counting access wrappers to the perag structures.
45 * Because we never free per-ag structures, the only thing we
46 * have to protect against changes is the tree structure itself.
47 */
48struct xfs_perag *
49xfs_perag_get(
50 struct xfs_mount *mp,
51 xfs_agnumber_t agno)
52{
53 struct xfs_perag *pag;
54 int ref = 0;
55
56 rcu_read_lock();
57 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
58 if (pag) {
59 ASSERT(atomic_read(&pag->pag_ref) >= 0);
60 ref = atomic_inc_return(&pag->pag_ref);
61 }
62 rcu_read_unlock();
63 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
64 return pag;
65}
66
67/*
68 * search from @first to find the next perag with the given tag set.
69 */
70struct xfs_perag *
71xfs_perag_get_tag(
72 struct xfs_mount *mp,
73 xfs_agnumber_t first,
74 int tag)
75{
76 struct xfs_perag *pag;
77 int found;
78 int ref;
79
80 rcu_read_lock();
81 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
82 (void **)&pag, first, 1, tag);
83 if (found <= 0) {
84 rcu_read_unlock();
85 return NULL;
86 }
87 ref = atomic_inc_return(&pag->pag_ref);
88 rcu_read_unlock();
89 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
90 return pag;
91}
92
93void
94xfs_perag_put(
95 struct xfs_perag *pag)
96{
97 int ref;
98
99 ASSERT(atomic_read(&pag->pag_ref) > 0);
100 ref = atomic_dec_return(&pag->pag_ref);
101 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
102}
103
104/*
105 * Check the validity of the SB found.
106 */
107STATIC int
108xfs_mount_validate_sb(
109 xfs_mount_t *mp,
110 xfs_sb_t *sbp,
111 bool check_inprogress,
112 bool check_version)
113{
ff55068c
DC
114 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
115 xfs_warn(mp, "bad magic number");
2451337d 116 return -EWRONGFS;
ff55068c
DC
117 }
118
119
120 if (!xfs_sb_good_version(sbp)) {
121 xfs_warn(mp, "bad version");
2451337d 122 return -EWRONGFS;
ff55068c
DC
123 }
124
125 /*
126 * Version 5 superblock feature mask validation. Reject combinations the
127 * kernel cannot support up front before checking anything else. For
128 * write validation, we don't need to check feature masks.
129 */
130 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
ff55068c
DC
131 if (xfs_sb_has_compat_feature(sbp,
132 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
133 xfs_warn(mp,
134"Superblock has unknown compatible features (0x%x) enabled.\n"
135"Using a more recent kernel is recommended.",
136 (sbp->sb_features_compat &
137 XFS_SB_FEAT_COMPAT_UNKNOWN));
138 }
139
140 if (xfs_sb_has_ro_compat_feature(sbp,
141 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
142 xfs_alert(mp,
143"Superblock has unknown read-only compatible features (0x%x) enabled.",
144 (sbp->sb_features_ro_compat &
145 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
146 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
147 xfs_warn(mp,
148"Attempted to mount read-only compatible filesystem read-write.\n"
149"Filesystem can only be safely mounted read only.");
2451337d 150 return -EINVAL;
ff55068c
DC
151 }
152 }
153 if (xfs_sb_has_incompat_feature(sbp,
154 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
155 xfs_warn(mp,
156"Superblock has unknown incompatible features (0x%x) enabled.\n"
157"Filesystem can not be safely mounted by this kernel.",
158 (sbp->sb_features_incompat &
159 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
2451337d 160 return -EINVAL;
ff55068c
DC
161 }
162 }
163
164 if (xfs_sb_version_has_pquotino(sbp)) {
165 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
166 xfs_notice(mp,
08e96e1a 167 "Version 5 of Super block has XFS_OQUOTA bits.");
2451337d 168 return -EFSCORRUPTED;
ff55068c
DC
169 }
170 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
171 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
172 xfs_notice(mp,
08e96e1a 173"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
2451337d 174 return -EFSCORRUPTED;
ff55068c
DC
175 }
176
e5376fc1
BF
177 /*
178 * Full inode chunks must be aligned to inode chunk size when
179 * sparse inodes are enabled to support the sparse chunk
180 * allocation algorithm and prevent overlapping inode records.
181 */
182 if (xfs_sb_version_hassparseinodes(sbp)) {
183 uint32_t align;
184
185 xfs_alert(mp,
186 "EXPERIMENTAL sparse inode feature enabled. Use at your own risk!");
187
188 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
189 >> sbp->sb_blocklog;
190 if (sbp->sb_inoalignmt != align) {
191 xfs_warn(mp,
192"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
193 sbp->sb_inoalignmt, align);
194 return -EINVAL;
195 }
196 }
197
ff55068c
DC
198 if (unlikely(
199 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
200 xfs_warn(mp,
201 "filesystem is marked as having an external log; "
202 "specify logdev on the mount command line.");
2451337d 203 return -EINVAL;
ff55068c
DC
204 }
205
206 if (unlikely(
207 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
208 xfs_warn(mp,
209 "filesystem is marked as having an internal log; "
210 "do not specify logdev on the mount command line.");
2451337d 211 return -EINVAL;
ff55068c
DC
212 }
213
214 /*
215 * More sanity checking. Most of these were stolen directly from
216 * xfs_repair.
217 */
218 if (unlikely(
219 sbp->sb_agcount <= 0 ||
220 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
221 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
222 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
223 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
224 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
225 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
226 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
227 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
228 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
229 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
e1b05723 230 sbp->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG ||
ff55068c
DC
231 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
232 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
233 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
234 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
235 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
e1b05723 236 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
392c6de9 237 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
ff55068c
DC
238 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
239 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
240 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
241 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
242 sbp->sb_dblocks == 0 ||
243 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
ab3e57b5
DC
244 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
245 sbp->sb_shared_vn != 0)) {
5ef11eb0 246 xfs_notice(mp, "SB sanity check failed");
2451337d 247 return -EFSCORRUPTED;
ff55068c
DC
248 }
249
250 /*
251 * Until this is fixed only page-sized or smaller data blocks work.
252 */
253 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
254 xfs_warn(mp,
255 "File system with blocksize %d bytes. "
256 "Only pagesize (%ld) or less will currently work.",
257 sbp->sb_blocksize, PAGE_SIZE);
2451337d 258 return -ENOSYS;
ff55068c
DC
259 }
260
261 /*
262 * Currently only very few inode sizes are supported.
263 */
264 switch (sbp->sb_inodesize) {
265 case 256:
266 case 512:
267 case 1024:
268 case 2048:
269 break;
270 default:
271 xfs_warn(mp, "inode size of %d bytes not supported",
272 sbp->sb_inodesize);
2451337d 273 return -ENOSYS;
ff55068c
DC
274 }
275
276 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
277 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
278 xfs_warn(mp,
279 "file system too large to be mounted on this system.");
2451337d 280 return -EFBIG;
ff55068c
DC
281 }
282
283 if (check_inprogress && sbp->sb_inprogress) {
284 xfs_warn(mp, "Offline file system operation in progress!");
2451337d 285 return -EFSCORRUPTED;
ff55068c 286 }
ff55068c
DC
287 return 0;
288}
289
290void
291xfs_sb_quota_from_disk(struct xfs_sb *sbp)
292{
293 /*
294 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
295 * leads to in-core values having two different values for a quota
296 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
297 * NULLFSINO.
298 *
299 * Note that this change affect only the in-core values. These
300 * values are not written back to disk unless any quota information
301 * is written to the disk. Even in that case, sb_pquotino field is
302 * not written to disk unless the superblock supports pquotino.
303 */
304 if (sbp->sb_uquotino == 0)
305 sbp->sb_uquotino = NULLFSINO;
306 if (sbp->sb_gquotino == 0)
307 sbp->sb_gquotino = NULLFSINO;
308 if (sbp->sb_pquotino == 0)
309 sbp->sb_pquotino = NULLFSINO;
310
311 /*
312 * We need to do these manipilations only if we are working
313 * with an older version of on-disk superblock.
314 */
315 if (xfs_sb_version_has_pquotino(sbp))
316 return;
317
318 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
319 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
320 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
321 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
322 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
323 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
324 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
325
326 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
327 /*
328 * In older version of superblock, on-disk superblock only
329 * has sb_gquotino, and in-core superblock has both sb_gquotino
330 * and sb_pquotino. But, only one of them is supported at any
331 * point of time. So, if PQUOTA is set in disk superblock,
332 * copy over sb_gquotino to sb_pquotino.
333 */
334 sbp->sb_pquotino = sbp->sb_gquotino;
335 sbp->sb_gquotino = NULLFSINO;
336 }
337}
338
5ef828c4
ES
339static void
340__xfs_sb_from_disk(
ff55068c 341 struct xfs_sb *to,
5ef828c4
ES
342 xfs_dsb_t *from,
343 bool convert_xquota)
ff55068c
DC
344{
345 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
346 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
347 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
348 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
349 to->sb_rextents = be64_to_cpu(from->sb_rextents);
350 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
351 to->sb_logstart = be64_to_cpu(from->sb_logstart);
352 to->sb_rootino = be64_to_cpu(from->sb_rootino);
353 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
354 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
355 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
356 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
357 to->sb_agcount = be32_to_cpu(from->sb_agcount);
358 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
359 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
360 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
361 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
362 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
363 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
364 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
365 to->sb_blocklog = from->sb_blocklog;
366 to->sb_sectlog = from->sb_sectlog;
367 to->sb_inodelog = from->sb_inodelog;
368 to->sb_inopblog = from->sb_inopblog;
369 to->sb_agblklog = from->sb_agblklog;
370 to->sb_rextslog = from->sb_rextslog;
371 to->sb_inprogress = from->sb_inprogress;
372 to->sb_imax_pct = from->sb_imax_pct;
373 to->sb_icount = be64_to_cpu(from->sb_icount);
374 to->sb_ifree = be64_to_cpu(from->sb_ifree);
375 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
376 to->sb_frextents = be64_to_cpu(from->sb_frextents);
377 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
378 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
379 to->sb_qflags = be16_to_cpu(from->sb_qflags);
380 to->sb_flags = from->sb_flags;
381 to->sb_shared_vn = from->sb_shared_vn;
382 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
383 to->sb_unit = be32_to_cpu(from->sb_unit);
384 to->sb_width = be32_to_cpu(from->sb_width);
385 to->sb_dirblklog = from->sb_dirblklog;
386 to->sb_logsectlog = from->sb_logsectlog;
387 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
388 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
389 to->sb_features2 = be32_to_cpu(from->sb_features2);
390 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
391 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
392 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
393 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
394 to->sb_features_log_incompat =
395 be32_to_cpu(from->sb_features_log_incompat);
04dd1a0d
ES
396 /* crc is only used on disk, not in memory; just init to 0 here. */
397 to->sb_crc = 0;
fb4f2b4e 398 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
ff55068c
DC
399 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
400 to->sb_lsn = be64_to_cpu(from->sb_lsn);
5ef828c4
ES
401 /* Convert on-disk flags to in-memory flags? */
402 if (convert_xquota)
403 xfs_sb_quota_from_disk(to);
404}
405
406void
407xfs_sb_from_disk(
408 struct xfs_sb *to,
409 xfs_dsb_t *from)
410{
411 __xfs_sb_from_disk(to, from, true);
ff55068c
DC
412}
413
4d11a402 414static void
ff55068c 415xfs_sb_quota_to_disk(
4d11a402
DC
416 struct xfs_dsb *to,
417 struct xfs_sb *from)
ff55068c
DC
418{
419 __uint16_t qflags = from->sb_qflags;
420
4d11a402
DC
421 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
422 if (xfs_sb_version_has_pquotino(from)) {
423 to->sb_qflags = cpu_to_be16(from->sb_qflags);
424 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
425 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
426 return;
427 }
428
ff55068c 429 /*
4d11a402
DC
430 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
431 * flags, whereas the on-disk version does. So, convert incore
432 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
ff55068c 433 */
4d11a402
DC
434 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
435 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
ff55068c 436
4d11a402
DC
437 if (from->sb_qflags &
438 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
439 qflags |= XFS_OQUOTA_ENFD;
440 if (from->sb_qflags &
441 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
442 qflags |= XFS_OQUOTA_CHKD;
443 to->sb_qflags = cpu_to_be16(qflags);
ff55068c
DC
444
445 /*
4d11a402
DC
446 * GQUOTINO and PQUOTINO cannot be used together in versions
447 * of superblock that do not have pquotino. from->sb_flags
448 * tells us which quota is active and should be copied to
449 * disk. If neither are active, we should NULL the inode.
03e01349 450 *
4d11a402
DC
451 * In all cases, the separate pquotino must remain 0 because it
452 * it beyond the "end" of the valid non-pquotino superblock.
ff55068c 453 */
4d11a402 454 if (from->sb_qflags & XFS_GQUOTA_ACCT)
ff55068c 455 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
4d11a402 456 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
ff55068c 457 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
03e01349
DC
458 else {
459 /*
460 * We can't rely on just the fields being logged to tell us
461 * that it is safe to write NULLFSINO - we should only do that
462 * if quotas are not actually enabled. Hence only write
463 * NULLFSINO if both in-core quota inodes are NULL.
464 */
465 if (from->sb_gquotino == NULLFSINO &&
466 from->sb_pquotino == NULLFSINO)
467 to->sb_gquotino = cpu_to_be64(NULLFSINO);
468 }
ff55068c 469
4d11a402 470 to->sb_pquotino = 0;
ff55068c
DC
471}
472
ff55068c
DC
473void
474xfs_sb_to_disk(
4d11a402
DC
475 struct xfs_dsb *to,
476 struct xfs_sb *from)
ff55068c 477{
4d11a402
DC
478 xfs_sb_quota_to_disk(to, from);
479
480 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
481 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
482 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
483 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
484 to->sb_rextents = cpu_to_be64(from->sb_rextents);
485 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
486 to->sb_logstart = cpu_to_be64(from->sb_logstart);
487 to->sb_rootino = cpu_to_be64(from->sb_rootino);
488 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
489 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
490 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
491 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
492 to->sb_agcount = cpu_to_be32(from->sb_agcount);
493 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
494 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
495 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
496 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
497 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
498 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
499 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
500 to->sb_blocklog = from->sb_blocklog;
501 to->sb_sectlog = from->sb_sectlog;
502 to->sb_inodelog = from->sb_inodelog;
503 to->sb_inopblog = from->sb_inopblog;
504 to->sb_agblklog = from->sb_agblklog;
505 to->sb_rextslog = from->sb_rextslog;
506 to->sb_inprogress = from->sb_inprogress;
507 to->sb_imax_pct = from->sb_imax_pct;
508 to->sb_icount = cpu_to_be64(from->sb_icount);
509 to->sb_ifree = cpu_to_be64(from->sb_ifree);
510 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
511 to->sb_frextents = cpu_to_be64(from->sb_frextents);
ff55068c 512
4d11a402
DC
513 to->sb_flags = from->sb_flags;
514 to->sb_shared_vn = from->sb_shared_vn;
515 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
516 to->sb_unit = cpu_to_be32(from->sb_unit);
517 to->sb_width = cpu_to_be32(from->sb_width);
518 to->sb_dirblklog = from->sb_dirblklog;
519 to->sb_logsectlog = from->sb_logsectlog;
520 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
521 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
074e427b
DC
522
523 /*
524 * We need to ensure that bad_features2 always matches features2.
525 * Hence we enforce that here rather than having to remember to do it
526 * everywhere else that updates features2.
527 */
528 from->sb_bad_features2 = from->sb_features2;
4d11a402
DC
529 to->sb_features2 = cpu_to_be32(from->sb_features2);
530 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
531
532 if (xfs_sb_version_hascrc(from)) {
533 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
534 to->sb_features_ro_compat =
535 cpu_to_be32(from->sb_features_ro_compat);
536 to->sb_features_incompat =
537 cpu_to_be32(from->sb_features_incompat);
538 to->sb_features_log_incompat =
539 cpu_to_be32(from->sb_features_log_incompat);
fb4f2b4e 540 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
4d11a402 541 to->sb_lsn = cpu_to_be64(from->sb_lsn);
ff55068c
DC
542 }
543}
544
545static int
546xfs_sb_verify(
547 struct xfs_buf *bp,
548 bool check_version)
549{
550 struct xfs_mount *mp = bp->b_target->bt_mount;
551 struct xfs_sb sb;
552
5ef828c4
ES
553 /*
554 * Use call variant which doesn't convert quota flags from disk
555 * format, because xfs_mount_validate_sb checks the on-disk flags.
556 */
557 __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
ff55068c
DC
558
559 /*
560 * Only check the in progress field for the primary superblock as
561 * mkfs.xfs doesn't clear it from secondary superblocks.
562 */
563 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
564 check_version);
565}
566
567/*
568 * If the superblock has the CRC feature bit set or the CRC field is non-null,
569 * check that the CRC is valid. We check the CRC field is non-null because a
570 * single bit error could clear the feature bit and unused parts of the
571 * superblock are supposed to be zero. Hence a non-null crc field indicates that
572 * we've potentially lost a feature bit and we should check it anyway.
10e6e65d
ES
573 *
574 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
575 * last field in V4 secondary superblocks. So for secondary superblocks,
576 * we are more forgiving, and ignore CRC failures if the primary doesn't
577 * indicate that the fs version is V5.
ff55068c
DC
578 */
579static void
580xfs_sb_read_verify(
581 struct xfs_buf *bp)
582{
583 struct xfs_mount *mp = bp->b_target->bt_mount;
584 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
585 int error;
586
587 /*
588 * open code the version check to avoid needing to convert the entire
589 * superblock from disk order just to check the version number
590 */
591 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
592 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
593 XFS_SB_VERSION_5) ||
594 dsb->sb_crc != 0)) {
595
51582170 596 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
10e6e65d 597 /* Only fail bad secondaries on a known V5 filesystem */
7a01e707 598 if (bp->b_bn == XFS_SB_DADDR ||
10e6e65d 599 xfs_sb_version_hascrc(&mp->m_sb)) {
2451337d 600 error = -EFSBADCRC;
10e6e65d
ES
601 goto out_error;
602 }
ff55068c
DC
603 }
604 }
605 error = xfs_sb_verify(bp, true);
606
607out_error:
608 if (error) {
ff55068c 609 xfs_buf_ioerror(bp, error);
2451337d 610 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
ce5028cf 611 xfs_verifier_error(bp);
ff55068c
DC
612 }
613}
614
615/*
616 * We may be probed for a filesystem match, so we may not want to emit
617 * messages when the superblock buffer is not actually an XFS superblock.
2533787a 618 * If we find an XFS superblock, then run a normal, noisy mount because we are
ff55068c
DC
619 * really going to mount it and want to know about errors.
620 */
621static void
622xfs_sb_quiet_read_verify(
623 struct xfs_buf *bp)
624{
625 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
626
ff55068c
DC
627 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
628 /* XFS filesystem, verify noisily! */
629 xfs_sb_read_verify(bp);
630 return;
631 }
632 /* quietly fail */
2451337d 633 xfs_buf_ioerror(bp, -EWRONGFS);
ff55068c
DC
634}
635
636static void
637xfs_sb_write_verify(
638 struct xfs_buf *bp)
639{
640 struct xfs_mount *mp = bp->b_target->bt_mount;
641 struct xfs_buf_log_item *bip = bp->b_fspriv;
642 int error;
643
644 error = xfs_sb_verify(bp, false);
645 if (error) {
ff55068c 646 xfs_buf_ioerror(bp, error);
ce5028cf 647 xfs_verifier_error(bp);
ff55068c
DC
648 return;
649 }
650
651 if (!xfs_sb_version_hascrc(&mp->m_sb))
652 return;
653
654 if (bip)
655 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
656
f1dbcd7e 657 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
ff55068c
DC
658}
659
660const struct xfs_buf_ops xfs_sb_buf_ops = {
661 .verify_read = xfs_sb_read_verify,
662 .verify_write = xfs_sb_write_verify,
663};
664
665const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
666 .verify_read = xfs_sb_quiet_read_verify,
667 .verify_write = xfs_sb_write_verify,
668};
669
670/*
671 * xfs_mount_common
672 *
673 * Mount initialization code establishing various mount
674 * fields from the superblock associated with the given
675 * mount structure
676 */
677void
678xfs_sb_mount_common(
679 struct xfs_mount *mp,
680 struct xfs_sb *sbp)
681{
682 mp->m_agfrotor = mp->m_agirotor = 0;
683 spin_lock_init(&mp->m_agirotor_lock);
684 mp->m_maxagi = mp->m_sb.sb_agcount;
685 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
686 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
687 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
688 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
689 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
690 mp->m_blockmask = sbp->sb_blocksize - 1;
691 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
692 mp->m_blockwmask = mp->m_blockwsize - 1;
693
694 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
695 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
696 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
697 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
698
699 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
700 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
701 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
702 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
703
704 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
705 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
706 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
707 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
708
709 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
710 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
711 sbp->sb_inopblock);
712 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
066a1884
BF
713
714 if (sbp->sb_spino_align)
715 mp->m_ialloc_min_blks = sbp->sb_spino_align;
716 else
717 mp->m_ialloc_min_blks = mp->m_ialloc_blks;
ff55068c
DC
718}
719
720/*
721 * xfs_initialize_perag_data
722 *
723 * Read in each per-ag structure so we can count up the number of
724 * allocated inodes, free inodes and used filesystem blocks as this
725 * information is no longer persistent in the superblock. Once we have
726 * this information, write it into the in-core superblock structure.
727 */
728int
729xfs_initialize_perag_data(
730 struct xfs_mount *mp,
731 xfs_agnumber_t agcount)
732{
733 xfs_agnumber_t index;
734 xfs_perag_t *pag;
735 xfs_sb_t *sbp = &mp->m_sb;
736 uint64_t ifree = 0;
737 uint64_t ialloc = 0;
738 uint64_t bfree = 0;
739 uint64_t bfreelst = 0;
740 uint64_t btree = 0;
741 int error;
742
743 for (index = 0; index < agcount; index++) {
744 /*
745 * read the agf, then the agi. This gets us
746 * all the information we need and populates the
747 * per-ag structures for us.
748 */
749 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
750 if (error)
751 return error;
752
753 error = xfs_ialloc_pagi_init(mp, NULL, index);
754 if (error)
755 return error;
756 pag = xfs_perag_get(mp, index);
757 ifree += pag->pagi_freecount;
758 ialloc += pag->pagi_count;
759 bfree += pag->pagf_freeblks;
760 bfreelst += pag->pagf_flcount;
761 btree += pag->pagf_btreeblks;
762 xfs_perag_put(pag);
763 }
5681ca40
DC
764
765 /* Overwrite incore superblock counters with just-read data */
ff55068c
DC
766 spin_lock(&mp->m_sb_lock);
767 sbp->sb_ifree = ifree;
768 sbp->sb_icount = ialloc;
769 sbp->sb_fdblocks = bfree + bfreelst + btree;
770 spin_unlock(&mp->m_sb_lock);
771
5681ca40 772 xfs_reinit_percpu_counters(mp);
ff55068c
DC
773
774 return 0;
775}
776
777/*
61e63ecb
DC
778 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
779 * into the superblock buffer to be logged. It does not provide the higher
780 * level of locking that is needed to protect the in-core superblock from
781 * concurrent access.
ff55068c
DC
782 */
783void
61e63ecb 784xfs_log_sb(
4d11a402 785 struct xfs_trans *tp)
ff55068c 786{
4d11a402
DC
787 struct xfs_mount *mp = tp->t_mountp;
788 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
ff55068c 789
501ab323 790 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
e88b64ea 791 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
0d485ada 792 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
501ab323 793
4d11a402 794 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
ff55068c 795 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
4d11a402 796 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
ff55068c 797}
61e63ecb
DC
798
799/*
800 * xfs_sync_sb
801 *
802 * Sync the superblock to disk.
803 *
804 * Note that the caller is responsible for checking the frozen state of the
805 * filesystem. This procedure uses the non-blocking transaction allocator and
806 * thus will allow modifications to a frozen fs. This is required because this
807 * code can be called during the process of freezing where use of the high-level
808 * allocator would deadlock.
809 */
810int
811xfs_sync_sb(
812 struct xfs_mount *mp,
813 bool wait)
814{
815 struct xfs_trans *tp;
816 int error;
817
818 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_CHANGE, KM_SLEEP);
819 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
820 if (error) {
4906e215 821 xfs_trans_cancel(tp);
61e63ecb
DC
822 return error;
823 }
824
825 xfs_log_sb(tp);
826 if (wait)
827 xfs_trans_set_sync(tp);
70393313 828 return xfs_trans_commit(tp);
61e63ecb 829}