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