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