aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorDave Chinner <dchinner@redhat.com>2013-08-12 06:49:41 -0400
committerBen Myers <bpm@sgi.com>2013-08-12 17:44:11 -0400
commitff55068c2010f39bcd899ca14b0a3d401d14da54 (patch)
treee207720e153c782682f833d0c00441fda88fadb2 /fs
parent1fb7e48db6810093012873a652062648ace6b5bb (diff)
xfs: introduce xfs_sb.c for sharing with libxfs
xfs_mount.c is shared with userspace, but the only functions that are shared are to do with physical superblock manipulations. This means that less than 25% of the xfs_mount.c code is actually shared with userspace. Move all the superblock functions to xfs_sb.c and share that instead with libxfs. Note that this will leave all the in-core transaction related superblock counter modifications in xfs_mount.c as none of that is shared with userspace. With a few more small changes, xfs_mount.h won't need to be shared with userspace anymore, either. Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Mark Tinguely <tinguely@sgi.com> Signed-off-by: Ben Myers <bpm@sgi.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/xfs/Makefile3
-rw-r--r--fs/xfs/xfs_mount.c784
-rw-r--r--fs/xfs/xfs_mount.h19
-rw-r--r--fs/xfs/xfs_sb.c834
-rw-r--r--fs/xfs/xfs_sb.h20
5 files changed, 863 insertions, 797 deletions
diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile
index 20b1d949b090..fb14461d414e 100644
--- a/fs/xfs/Makefile
+++ b/fs/xfs/Makefile
@@ -47,6 +47,7 @@ xfs-y += xfs_aops.o \
47 xfs_iops.o \ 47 xfs_iops.o \
48 xfs_itable.o \ 48 xfs_itable.o \
49 xfs_message.o \ 49 xfs_message.o \
50 xfs_mount.o \
50 xfs_mru_cache.o \ 51 xfs_mru_cache.o \
51 xfs_rename.o \ 52 xfs_rename.o \
52 xfs_super.o \ 53 xfs_super.o \
@@ -81,7 +82,7 @@ xfs-y += xfs_alloc.o \
81 xfs_inode_fork.o \ 82 xfs_inode_fork.o \
82 xfs_inode_buf.o \ 83 xfs_inode_buf.o \
83 xfs_log_recover.o \ 84 xfs_log_recover.o \
84 xfs_mount.o \ 85 xfs_sb.o \
85 xfs_symlink_remote.o \ 86 xfs_symlink_remote.o \
86 xfs_trans_resv.o 87 xfs_trans_resv.o
87 88
diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index b07ca4be3941..3b6fe4ffd724 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -61,69 +61,6 @@ STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
61#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0) 61#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
62#endif 62#endif
63 63
64static const struct {
65 short offset;
66 short type; /* 0 = integer
67 * 1 = binary / string (no translation)
68 */
69} xfs_sb_info[] = {
70 { offsetof(xfs_sb_t, sb_magicnum), 0 },
71 { offsetof(xfs_sb_t, sb_blocksize), 0 },
72 { offsetof(xfs_sb_t, sb_dblocks), 0 },
73 { offsetof(xfs_sb_t, sb_rblocks), 0 },
74 { offsetof(xfs_sb_t, sb_rextents), 0 },
75 { offsetof(xfs_sb_t, sb_uuid), 1 },
76 { offsetof(xfs_sb_t, sb_logstart), 0 },
77 { offsetof(xfs_sb_t, sb_rootino), 0 },
78 { offsetof(xfs_sb_t, sb_rbmino), 0 },
79 { offsetof(xfs_sb_t, sb_rsumino), 0 },
80 { offsetof(xfs_sb_t, sb_rextsize), 0 },
81 { offsetof(xfs_sb_t, sb_agblocks), 0 },
82 { offsetof(xfs_sb_t, sb_agcount), 0 },
83 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
84 { offsetof(xfs_sb_t, sb_logblocks), 0 },
85 { offsetof(xfs_sb_t, sb_versionnum), 0 },
86 { offsetof(xfs_sb_t, sb_sectsize), 0 },
87 { offsetof(xfs_sb_t, sb_inodesize), 0 },
88 { offsetof(xfs_sb_t, sb_inopblock), 0 },
89 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
90 { offsetof(xfs_sb_t, sb_blocklog), 0 },
91 { offsetof(xfs_sb_t, sb_sectlog), 0 },
92 { offsetof(xfs_sb_t, sb_inodelog), 0 },
93 { offsetof(xfs_sb_t, sb_inopblog), 0 },
94 { offsetof(xfs_sb_t, sb_agblklog), 0 },
95 { offsetof(xfs_sb_t, sb_rextslog), 0 },
96 { offsetof(xfs_sb_t, sb_inprogress), 0 },
97 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
98 { offsetof(xfs_sb_t, sb_icount), 0 },
99 { offsetof(xfs_sb_t, sb_ifree), 0 },
100 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
101 { offsetof(xfs_sb_t, sb_frextents), 0 },
102 { offsetof(xfs_sb_t, sb_uquotino), 0 },
103 { offsetof(xfs_sb_t, sb_gquotino), 0 },
104 { offsetof(xfs_sb_t, sb_qflags), 0 },
105 { offsetof(xfs_sb_t, sb_flags), 0 },
106 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
107 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
108 { offsetof(xfs_sb_t, sb_unit), 0 },
109 { offsetof(xfs_sb_t, sb_width), 0 },
110 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
111 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
112 { offsetof(xfs_sb_t, sb_logsectsize),0 },
113 { offsetof(xfs_sb_t, sb_logsunit), 0 },
114 { offsetof(xfs_sb_t, sb_features2), 0 },
115 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
116 { offsetof(xfs_sb_t, sb_features_compat), 0 },
117 { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
118 { offsetof(xfs_sb_t, sb_features_incompat), 0 },
119 { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
120 { offsetof(xfs_sb_t, sb_crc), 0 },
121 { offsetof(xfs_sb_t, sb_pad), 0 },
122 { offsetof(xfs_sb_t, sb_pquotino), 0 },
123 { offsetof(xfs_sb_t, sb_lsn), 0 },
124 { sizeof(xfs_sb_t), 0 }
125};
126
127static DEFINE_MUTEX(xfs_uuid_table_mutex); 64static DEFINE_MUTEX(xfs_uuid_table_mutex);
128static int xfs_uuid_table_size; 65static int xfs_uuid_table_size;
129static uuid_t *xfs_uuid_table; 66static uuid_t *xfs_uuid_table;
@@ -199,64 +136,6 @@ xfs_uuid_unmount(
199} 136}
200 137
201 138
202/*
203 * Reference counting access wrappers to the perag structures.
204 * Because we never free per-ag structures, the only thing we
205 * have to protect against changes is the tree structure itself.
206 */
207struct xfs_perag *
208xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
209{
210 struct xfs_perag *pag;
211 int ref = 0;
212
213 rcu_read_lock();
214 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
215 if (pag) {
216 ASSERT(atomic_read(&pag->pag_ref) >= 0);
217 ref = atomic_inc_return(&pag->pag_ref);
218 }
219 rcu_read_unlock();
220 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
221 return pag;
222}
223
224/*
225 * search from @first to find the next perag with the given tag set.
226 */
227struct xfs_perag *
228xfs_perag_get_tag(
229 struct xfs_mount *mp,
230 xfs_agnumber_t first,
231 int tag)
232{
233 struct xfs_perag *pag;
234 int found;
235 int ref;
236
237 rcu_read_lock();
238 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
239 (void **)&pag, first, 1, tag);
240 if (found <= 0) {
241 rcu_read_unlock();
242 return NULL;
243 }
244 ref = atomic_inc_return(&pag->pag_ref);
245 rcu_read_unlock();
246 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
247 return pag;
248}
249
250void
251xfs_perag_put(struct xfs_perag *pag)
252{
253 int ref;
254
255 ASSERT(atomic_read(&pag->pag_ref) > 0);
256 ref = atomic_dec_return(&pag->pag_ref);
257 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
258}
259
260STATIC void 139STATIC void
261__xfs_free_perag( 140__xfs_free_perag(
262 struct rcu_head *head) 141 struct rcu_head *head)
@@ -309,189 +188,6 @@ xfs_sb_validate_fsb_count(
309 return 0; 188 return 0;
310} 189}
311 190
312/*
313 * Check the validity of the SB found.
314 */
315STATIC int
316xfs_mount_validate_sb(
317 xfs_mount_t *mp,
318 xfs_sb_t *sbp,
319 bool check_inprogress,
320 bool check_version)
321{
322
323 /*
324 * If the log device and data device have the
325 * same device number, the log is internal.
326 * Consequently, the sb_logstart should be non-zero. If
327 * we have a zero sb_logstart in this case, we may be trying to mount
328 * a volume filesystem in a non-volume manner.
329 */
330 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
331 xfs_warn(mp, "bad magic number");
332 return XFS_ERROR(EWRONGFS);
333 }
334
335
336 if (!xfs_sb_good_version(sbp)) {
337 xfs_warn(mp, "bad version");
338 return XFS_ERROR(EWRONGFS);
339 }
340
341 /*
342 * Version 5 superblock feature mask validation. Reject combinations the
343 * kernel cannot support up front before checking anything else. For
344 * write validation, we don't need to check feature masks.
345 */
346 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
347 xfs_alert(mp,
348"Version 5 superblock detected. This kernel has EXPERIMENTAL support enabled!\n"
349"Use of these features in this kernel is at your own risk!");
350
351 if (xfs_sb_has_compat_feature(sbp,
352 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
353 xfs_warn(mp,
354"Superblock has unknown compatible features (0x%x) enabled.\n"
355"Using a more recent kernel is recommended.",
356 (sbp->sb_features_compat &
357 XFS_SB_FEAT_COMPAT_UNKNOWN));
358 }
359
360 if (xfs_sb_has_ro_compat_feature(sbp,
361 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
362 xfs_alert(mp,
363"Superblock has unknown read-only compatible features (0x%x) enabled.",
364 (sbp->sb_features_ro_compat &
365 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
366 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
367 xfs_warn(mp,
368"Attempted to mount read-only compatible filesystem read-write.\n"
369"Filesystem can only be safely mounted read only.");
370 return XFS_ERROR(EINVAL);
371 }
372 }
373 if (xfs_sb_has_incompat_feature(sbp,
374 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
375 xfs_warn(mp,
376"Superblock has unknown incompatible features (0x%x) enabled.\n"
377"Filesystem can not be safely mounted by this kernel.",
378 (sbp->sb_features_incompat &
379 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
380 return XFS_ERROR(EINVAL);
381 }
382 }
383
384 if (xfs_sb_version_has_pquotino(sbp)) {
385 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
386 xfs_notice(mp,
387 "Version 5 of Super block has XFS_OQUOTA bits.\n");
388 return XFS_ERROR(EFSCORRUPTED);
389 }
390 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
391 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
392 xfs_notice(mp,
393"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.\n");
394 return XFS_ERROR(EFSCORRUPTED);
395 }
396
397 if (unlikely(
398 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
399 xfs_warn(mp,
400 "filesystem is marked as having an external log; "
401 "specify logdev on the mount command line.");
402 return XFS_ERROR(EINVAL);
403 }
404
405 if (unlikely(
406 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
407 xfs_warn(mp,
408 "filesystem is marked as having an internal log; "
409 "do not specify logdev on the mount command line.");
410 return XFS_ERROR(EINVAL);
411 }
412
413 /*
414 * More sanity checking. Most of these were stolen directly from
415 * xfs_repair.
416 */
417 if (unlikely(
418 sbp->sb_agcount <= 0 ||
419 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
420 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
421 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
422 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
423 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
424 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
425 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
426 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
427 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
428 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
429 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
430 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
431 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
432 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
433 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
434 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
435 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
436 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
437 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
438 sbp->sb_dblocks == 0 ||
439 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
440 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
441 XFS_CORRUPTION_ERROR("SB sanity check failed",
442 XFS_ERRLEVEL_LOW, mp, sbp);
443 return XFS_ERROR(EFSCORRUPTED);
444 }
445
446 /*
447 * Until this is fixed only page-sized or smaller data blocks work.
448 */
449 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
450 xfs_warn(mp,
451 "File system with blocksize %d bytes. "
452 "Only pagesize (%ld) or less will currently work.",
453 sbp->sb_blocksize, PAGE_SIZE);
454 return XFS_ERROR(ENOSYS);
455 }
456
457 /*
458 * Currently only very few inode sizes are supported.
459 */
460 switch (sbp->sb_inodesize) {
461 case 256:
462 case 512:
463 case 1024:
464 case 2048:
465 break;
466 default:
467 xfs_warn(mp, "inode size of %d bytes not supported",
468 sbp->sb_inodesize);
469 return XFS_ERROR(ENOSYS);
470 }
471
472 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
473 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
474 xfs_warn(mp,
475 "file system too large to be mounted on this system.");
476 return XFS_ERROR(EFBIG);
477 }
478
479 if (check_inprogress && sbp->sb_inprogress) {
480 xfs_warn(mp, "Offline file system operation in progress!");
481 return XFS_ERROR(EFSCORRUPTED);
482 }
483
484 /*
485 * Version 1 directory format has never worked on Linux.
486 */
487 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
488 xfs_warn(mp, "file system using version 1 directory format");
489 return XFS_ERROR(ENOSYS);
490 }
491
492 return 0;
493}
494
495int 191int
496xfs_initialize_perag( 192xfs_initialize_perag(
497 xfs_mount_t *mp, 193 xfs_mount_t *mp,
@@ -576,342 +272,15 @@ out_unwind:
576 return error; 272 return error;
577} 273}
578 274
579static void
580xfs_sb_quota_from_disk(struct xfs_sb *sbp)
581{
582 /*
583 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
584 * leads to in-core values having two different values for a quota
585 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
586 * NULLFSINO.
587 *
588 * Note that this change affect only the in-core values. These
589 * values are not written back to disk unless any quota information
590 * is written to the disk. Even in that case, sb_pquotino field is
591 * not written to disk unless the superblock supports pquotino.
592 */
593 if (sbp->sb_uquotino == 0)
594 sbp->sb_uquotino = NULLFSINO;
595 if (sbp->sb_gquotino == 0)
596 sbp->sb_gquotino = NULLFSINO;
597 if (sbp->sb_pquotino == 0)
598 sbp->sb_pquotino = NULLFSINO;
599
600 /*
601 * We need to do these manipilations only if we are working
602 * with an older version of on-disk superblock.
603 */
604 if (xfs_sb_version_has_pquotino(sbp))
605 return;
606
607 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
608 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
609 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
610 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
611 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
612 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
613 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
614
615 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
616 /*
617 * In older version of superblock, on-disk superblock only
618 * has sb_gquotino, and in-core superblock has both sb_gquotino
619 * and sb_pquotino. But, only one of them is supported at any
620 * point of time. So, if PQUOTA is set in disk superblock,
621 * copy over sb_gquotino to sb_pquotino.
622 */
623 sbp->sb_pquotino = sbp->sb_gquotino;
624 sbp->sb_gquotino = NULLFSINO;
625 }
626}
627
628void
629xfs_sb_from_disk(
630 struct xfs_sb *to,
631 xfs_dsb_t *from)
632{
633 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
634 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
635 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
636 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
637 to->sb_rextents = be64_to_cpu(from->sb_rextents);
638 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
639 to->sb_logstart = be64_to_cpu(from->sb_logstart);
640 to->sb_rootino = be64_to_cpu(from->sb_rootino);
641 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
642 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
643 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
644 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
645 to->sb_agcount = be32_to_cpu(from->sb_agcount);
646 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
647 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
648 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
649 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
650 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
651 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
652 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
653 to->sb_blocklog = from->sb_blocklog;
654 to->sb_sectlog = from->sb_sectlog;
655 to->sb_inodelog = from->sb_inodelog;
656 to->sb_inopblog = from->sb_inopblog;
657 to->sb_agblklog = from->sb_agblklog;
658 to->sb_rextslog = from->sb_rextslog;
659 to->sb_inprogress = from->sb_inprogress;
660 to->sb_imax_pct = from->sb_imax_pct;
661 to->sb_icount = be64_to_cpu(from->sb_icount);
662 to->sb_ifree = be64_to_cpu(from->sb_ifree);
663 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
664 to->sb_frextents = be64_to_cpu(from->sb_frextents);
665 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
666 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
667 to->sb_qflags = be16_to_cpu(from->sb_qflags);
668 to->sb_flags = from->sb_flags;
669 to->sb_shared_vn = from->sb_shared_vn;
670 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
671 to->sb_unit = be32_to_cpu(from->sb_unit);
672 to->sb_width = be32_to_cpu(from->sb_width);
673 to->sb_dirblklog = from->sb_dirblklog;
674 to->sb_logsectlog = from->sb_logsectlog;
675 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
676 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
677 to->sb_features2 = be32_to_cpu(from->sb_features2);
678 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
679 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
680 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
681 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
682 to->sb_features_log_incompat =
683 be32_to_cpu(from->sb_features_log_incompat);
684 to->sb_pad = 0;
685 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
686 to->sb_lsn = be64_to_cpu(from->sb_lsn);
687}
688
689static inline void
690xfs_sb_quota_to_disk(
691 xfs_dsb_t *to,
692 xfs_sb_t *from,
693 __int64_t *fields)
694{
695 __uint16_t qflags = from->sb_qflags;
696
697 /*
698 * We need to do these manipilations only if we are working
699 * with an older version of on-disk superblock.
700 */
701 if (xfs_sb_version_has_pquotino(from))
702 return;
703
704 if (*fields & XFS_SB_QFLAGS) {
705 /*
706 * The in-core version of sb_qflags do not have
707 * XFS_OQUOTA_* flags, whereas the on-disk version
708 * does. So, convert incore XFS_{PG}QUOTA_* flags
709 * to on-disk XFS_OQUOTA_* flags.
710 */
711 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
712 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
713
714 if (from->sb_qflags &
715 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
716 qflags |= XFS_OQUOTA_ENFD;
717 if (from->sb_qflags &
718 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
719 qflags |= XFS_OQUOTA_CHKD;
720 to->sb_qflags = cpu_to_be16(qflags);
721 *fields &= ~XFS_SB_QFLAGS;
722 }
723
724 /*
725 * GQUOTINO and PQUOTINO cannot be used together in versions
726 * of superblock that do not have pquotino. from->sb_flags
727 * tells us which quota is active and should be copied to
728 * disk.
729 */
730 if ((*fields & XFS_SB_GQUOTINO) &&
731 (from->sb_qflags & XFS_GQUOTA_ACCT))
732 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
733 else if ((*fields & XFS_SB_PQUOTINO) &&
734 (from->sb_qflags & XFS_PQUOTA_ACCT))
735 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
736
737 *fields &= ~(XFS_SB_PQUOTINO | XFS_SB_GQUOTINO);
738}
739
740/*
741 * Copy in core superblock to ondisk one.
742 *
743 * The fields argument is mask of superblock fields to copy.
744 */
745void
746xfs_sb_to_disk(
747 xfs_dsb_t *to,
748 xfs_sb_t *from,
749 __int64_t fields)
750{
751 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
752 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
753 xfs_sb_field_t f;
754 int first;
755 int size;
756
757 ASSERT(fields);
758 if (!fields)
759 return;
760
761 xfs_sb_quota_to_disk(to, from, &fields);
762 while (fields) {
763 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
764 first = xfs_sb_info[f].offset;
765 size = xfs_sb_info[f + 1].offset - first;
766
767 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
768
769 if (size == 1 || xfs_sb_info[f].type == 1) {
770 memcpy(to_ptr + first, from_ptr + first, size);
771 } else {
772 switch (size) {
773 case 2:
774 *(__be16 *)(to_ptr + first) =
775 cpu_to_be16(*(__u16 *)(from_ptr + first));
776 break;
777 case 4:
778 *(__be32 *)(to_ptr + first) =
779 cpu_to_be32(*(__u32 *)(from_ptr + first));
780 break;
781 case 8:
782 *(__be64 *)(to_ptr + first) =
783 cpu_to_be64(*(__u64 *)(from_ptr + first));
784 break;
785 default:
786 ASSERT(0);
787 }
788 }
789
790 fields &= ~(1LL << f);
791 }
792}
793
794static int
795xfs_sb_verify(
796 struct xfs_buf *bp,
797 bool check_version)
798{
799 struct xfs_mount *mp = bp->b_target->bt_mount;
800 struct xfs_sb sb;
801
802 xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
803
804 /*
805 * Only check the in progress field for the primary superblock as
806 * mkfs.xfs doesn't clear it from secondary superblocks.
807 */
808 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
809 check_version);
810}
811
812/*
813 * If the superblock has the CRC feature bit set or the CRC field is non-null,
814 * check that the CRC is valid. We check the CRC field is non-null because a
815 * single bit error could clear the feature bit and unused parts of the
816 * superblock are supposed to be zero. Hence a non-null crc field indicates that
817 * we've potentially lost a feature bit and we should check it anyway.
818 */
819static void
820xfs_sb_read_verify(
821 struct xfs_buf *bp)
822{
823 struct xfs_mount *mp = bp->b_target->bt_mount;
824 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
825 int error;
826
827 /*
828 * open code the version check to avoid needing to convert the entire
829 * superblock from disk order just to check the version number
830 */
831 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
832 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
833 XFS_SB_VERSION_5) ||
834 dsb->sb_crc != 0)) {
835
836 if (!xfs_verify_cksum(bp->b_addr, be16_to_cpu(dsb->sb_sectsize),
837 offsetof(struct xfs_sb, sb_crc))) {
838 error = EFSCORRUPTED;
839 goto out_error;
840 }
841 }
842 error = xfs_sb_verify(bp, true);
843
844out_error:
845 if (error) {
846 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
847 xfs_buf_ioerror(bp, error);
848 }
849}
850
851/*
852 * We may be probed for a filesystem match, so we may not want to emit
853 * messages when the superblock buffer is not actually an XFS superblock.
854 * If we find an XFS superblock, the run a normal, noisy mount because we are
855 * really going to mount it and want to know about errors.
856 */
857static void
858xfs_sb_quiet_read_verify(
859 struct xfs_buf *bp)
860{
861 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
862
863
864 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
865 /* XFS filesystem, verify noisily! */
866 xfs_sb_read_verify(bp);
867 return;
868 }
869 /* quietly fail */
870 xfs_buf_ioerror(bp, EWRONGFS);
871}
872
873static void
874xfs_sb_write_verify(
875 struct xfs_buf *bp)
876{
877 struct xfs_mount *mp = bp->b_target->bt_mount;
878 struct xfs_buf_log_item *bip = bp->b_fspriv;
879 int error;
880
881 error = xfs_sb_verify(bp, false);
882 if (error) {
883 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
884 xfs_buf_ioerror(bp, error);
885 return;
886 }
887
888 if (!xfs_sb_version_hascrc(&mp->m_sb))
889 return;
890
891 if (bip)
892 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
893
894 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
895 offsetof(struct xfs_sb, sb_crc));
896}
897
898const struct xfs_buf_ops xfs_sb_buf_ops = {
899 .verify_read = xfs_sb_read_verify,
900 .verify_write = xfs_sb_write_verify,
901};
902
903static const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
904 .verify_read = xfs_sb_quiet_read_verify,
905 .verify_write = xfs_sb_write_verify,
906};
907
908/* 275/*
909 * xfs_readsb 276 * xfs_readsb
910 * 277 *
911 * Does the initial read of the superblock. 278 * Does the initial read of the superblock.
912 */ 279 */
913int 280int
914xfs_readsb(xfs_mount_t *mp, int flags) 281xfs_readsb(
282 struct xfs_mount *mp,
283 int flags)
915{ 284{
916 unsigned int sector_size; 285 unsigned int sector_size;
917 struct xfs_buf *bp; 286 struct xfs_buf *bp;
@@ -950,8 +319,8 @@ reread:
950 * Initialize the mount structure from the superblock. 319 * Initialize the mount structure from the superblock.
951 */ 320 */
952 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp)); 321 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
953
954 xfs_sb_quota_from_disk(&mp->m_sb); 322 xfs_sb_quota_from_disk(&mp->m_sb);
323
955 /* 324 /*
956 * We must be able to do sector-sized and sector-aligned IO. 325 * We must be able to do sector-sized and sector-aligned IO.
957 */ 326 */
@@ -988,107 +357,6 @@ release_buf:
988 return error; 357 return error;
989} 358}
990 359
991
992/*
993 * xfs_mount_common
994 *
995 * Mount initialization code establishing various mount
996 * fields from the superblock associated with the given
997 * mount structure
998 */
999STATIC void
1000xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
1001{
1002 mp->m_agfrotor = mp->m_agirotor = 0;
1003 spin_lock_init(&mp->m_agirotor_lock);
1004 mp->m_maxagi = mp->m_sb.sb_agcount;
1005 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
1006 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
1007 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
1008 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
1009 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
1010 mp->m_blockmask = sbp->sb_blocksize - 1;
1011 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
1012 mp->m_blockwmask = mp->m_blockwsize - 1;
1013
1014 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
1015 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
1016 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
1017 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
1018
1019 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
1020 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
1021 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
1022 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
1023
1024 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
1025 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
1026 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
1027 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
1028
1029 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
1030 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
1031 sbp->sb_inopblock);
1032 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
1033}
1034
1035/*
1036 * xfs_initialize_perag_data
1037 *
1038 * Read in each per-ag structure so we can count up the number of
1039 * allocated inodes, free inodes and used filesystem blocks as this
1040 * information is no longer persistent in the superblock. Once we have
1041 * this information, write it into the in-core superblock structure.
1042 */
1043STATIC int
1044xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
1045{
1046 xfs_agnumber_t index;
1047 xfs_perag_t *pag;
1048 xfs_sb_t *sbp = &mp->m_sb;
1049 uint64_t ifree = 0;
1050 uint64_t ialloc = 0;
1051 uint64_t bfree = 0;
1052 uint64_t bfreelst = 0;
1053 uint64_t btree = 0;
1054 int error;
1055
1056 for (index = 0; index < agcount; index++) {
1057 /*
1058 * read the agf, then the agi. This gets us
1059 * all the information we need and populates the
1060 * per-ag structures for us.
1061 */
1062 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
1063 if (error)
1064 return error;
1065
1066 error = xfs_ialloc_pagi_init(mp, NULL, index);
1067 if (error)
1068 return error;
1069 pag = xfs_perag_get(mp, index);
1070 ifree += pag->pagi_freecount;
1071 ialloc += pag->pagi_count;
1072 bfree += pag->pagf_freeblks;
1073 bfreelst += pag->pagf_flcount;
1074 btree += pag->pagf_btreeblks;
1075 xfs_perag_put(pag);
1076 }
1077 /*
1078 * Overwrite incore superblock counters with just-read data
1079 */
1080 spin_lock(&mp->m_sb_lock);
1081 sbp->sb_ifree = ifree;
1082 sbp->sb_icount = ialloc;
1083 sbp->sb_fdblocks = bfree + bfreelst + btree;
1084 spin_unlock(&mp->m_sb_lock);
1085
1086 /* Fixup the per-cpu counters as well. */
1087 xfs_icsb_reinit_counters(mp);
1088
1089 return 0;
1090}
1091
1092/* 360/*
1093 * Update alignment values based on mount options and sb values 361 * Update alignment values based on mount options and sb values
1094 */ 362 */
@@ -1381,7 +649,7 @@ xfs_mountfs(
1381 uint quotaflags = 0; 649 uint quotaflags = 0;
1382 int error = 0; 650 int error = 0;
1383 651
1384 xfs_mount_common(mp, sbp); 652 xfs_sb_mount_common(mp, sbp);
1385 653
1386 /* 654 /*
1387 * Check for a mismatched features2 values. Older kernels 655 * Check for a mismatched features2 values. Older kernels
@@ -1818,48 +1086,6 @@ xfs_log_sbcount(xfs_mount_t *mp)
1818} 1086}
1819 1087
1820/* 1088/*
1821 * xfs_mod_sb() can be used to copy arbitrary changes to the
1822 * in-core superblock into the superblock buffer to be logged.
1823 * It does not provide the higher level of locking that is
1824 * needed to protect the in-core superblock from concurrent
1825 * access.
1826 */
1827void
1828xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1829{
1830 xfs_buf_t *bp;
1831 int first;
1832 int last;
1833 xfs_mount_t *mp;
1834 xfs_sb_field_t f;
1835
1836 ASSERT(fields);
1837 if (!fields)
1838 return;
1839 mp = tp->t_mountp;
1840 bp = xfs_trans_getsb(tp, mp, 0);
1841 first = sizeof(xfs_sb_t);
1842 last = 0;
1843
1844 /* translate/copy */
1845
1846 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
1847
1848 /* find modified range */
1849 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1850 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1851 last = xfs_sb_info[f + 1].offset - 1;
1852
1853 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1854 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1855 first = xfs_sb_info[f].offset;
1856
1857 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
1858 xfs_trans_log_buf(tp, bp, first, last);
1859}
1860
1861
1862/*
1863 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply 1089 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1864 * a delta to a specified field in the in-core superblock. Simply 1090 * a delta to a specified field in the in-core superblock. Simply
1865 * switch on the field indicated and apply the delta to that field. 1091 * switch on the field indicated and apply the delta to that field.
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index 27d0d5c72f4f..5488df18c200 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -292,14 +292,6 @@ xfs_daddr_to_agbno(struct xfs_mount *mp, xfs_daddr_t d)
292} 292}
293 293
294/* 294/*
295 * perag get/put wrappers for ref counting
296 */
297struct xfs_perag *xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno);
298struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *mp, xfs_agnumber_t agno,
299 int tag);
300void xfs_perag_put(struct xfs_perag *pag);
301
302/*
303 * Per-cpu superblock locking functions 295 * Per-cpu superblock locking functions
304 */ 296 */
305#ifdef HAVE_PERCPU_SB 297#ifdef HAVE_PERCPU_SB
@@ -331,6 +323,8 @@ typedef struct xfs_mod_sb {
331extern int xfs_log_sbcount(xfs_mount_t *); 323extern int xfs_log_sbcount(xfs_mount_t *);
332extern __uint64_t xfs_default_resblks(xfs_mount_t *mp); 324extern __uint64_t xfs_default_resblks(xfs_mount_t *mp);
333extern int xfs_mountfs(xfs_mount_t *mp); 325extern int xfs_mountfs(xfs_mount_t *mp);
326extern int xfs_initialize_perag(xfs_mount_t *mp, xfs_agnumber_t agcount,
327 xfs_agnumber_t *maxagi);
334 328
335extern void xfs_unmountfs(xfs_mount_t *); 329extern void xfs_unmountfs(xfs_mount_t *);
336extern int xfs_mod_incore_sb(xfs_mount_t *, xfs_sb_field_t, int64_t, int); 330extern int xfs_mod_incore_sb(xfs_mount_t *, xfs_sb_field_t, int64_t, int);
@@ -349,13 +343,4 @@ extern void xfs_set_low_space_thresholds(struct xfs_mount *);
349 343
350#endif /* __KERNEL__ */ 344#endif /* __KERNEL__ */
351 345
352extern void xfs_sb_calc_crc(struct xfs_buf *);
353extern void xfs_mod_sb(struct xfs_trans *, __int64_t);
354extern int xfs_initialize_perag(struct xfs_mount *, xfs_agnumber_t,
355 xfs_agnumber_t *);
356extern void xfs_sb_from_disk(struct xfs_sb *, struct xfs_dsb *);
357extern void xfs_sb_to_disk(struct xfs_dsb *, struct xfs_sb *, __int64_t);
358
359extern const struct xfs_buf_ops xfs_sb_buf_ops;
360
361#endif /* __XFS_MOUNT_H__ */ 346#endif /* __XFS_MOUNT_H__ */
diff --git a/fs/xfs/xfs_sb.c b/fs/xfs/xfs_sb.c
new file mode 100644
index 000000000000..98d896f94280
--- /dev/null
+++ b/fs/xfs/xfs_sb.c
@@ -0,0 +1,834 @@
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"
20#include "xfs_format.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_trans_priv.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
28#include "xfs_mount.h"
29#include "xfs_da_btree.h"
30#include "xfs_dir2_format.h"
31#include "xfs_dir2.h"
32#include "xfs_bmap_btree.h"
33#include "xfs_alloc_btree.h"
34#include "xfs_ialloc_btree.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
39#include "xfs_alloc.h"
40#include "xfs_rtalloc.h"
41#include "xfs_bmap.h"
42#include "xfs_error.h"
43#include "xfs_quota.h"
44#include "xfs_fsops.h"
45#include "xfs_trace.h"
46#include "xfs_cksum.h"
47#include "xfs_buf_item.h"
48
49/*
50 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
51 */
52
53static const struct {
54 short offset;
55 short type; /* 0 = integer
56 * 1 = binary / string (no translation)
57 */
58} xfs_sb_info[] = {
59 { offsetof(xfs_sb_t, sb_magicnum), 0 },
60 { offsetof(xfs_sb_t, sb_blocksize), 0 },
61 { offsetof(xfs_sb_t, sb_dblocks), 0 },
62 { offsetof(xfs_sb_t, sb_rblocks), 0 },
63 { offsetof(xfs_sb_t, sb_rextents), 0 },
64 { offsetof(xfs_sb_t, sb_uuid), 1 },
65 { offsetof(xfs_sb_t, sb_logstart), 0 },
66 { offsetof(xfs_sb_t, sb_rootino), 0 },
67 { offsetof(xfs_sb_t, sb_rbmino), 0 },
68 { offsetof(xfs_sb_t, sb_rsumino), 0 },
69 { offsetof(xfs_sb_t, sb_rextsize), 0 },
70 { offsetof(xfs_sb_t, sb_agblocks), 0 },
71 { offsetof(xfs_sb_t, sb_agcount), 0 },
72 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
73 { offsetof(xfs_sb_t, sb_logblocks), 0 },
74 { offsetof(xfs_sb_t, sb_versionnum), 0 },
75 { offsetof(xfs_sb_t, sb_sectsize), 0 },
76 { offsetof(xfs_sb_t, sb_inodesize), 0 },
77 { offsetof(xfs_sb_t, sb_inopblock), 0 },
78 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
79 { offsetof(xfs_sb_t, sb_blocklog), 0 },
80 { offsetof(xfs_sb_t, sb_sectlog), 0 },
81 { offsetof(xfs_sb_t, sb_inodelog), 0 },
82 { offsetof(xfs_sb_t, sb_inopblog), 0 },
83 { offsetof(xfs_sb_t, sb_agblklog), 0 },
84 { offsetof(xfs_sb_t, sb_rextslog), 0 },
85 { offsetof(xfs_sb_t, sb_inprogress), 0 },
86 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
87 { offsetof(xfs_sb_t, sb_icount), 0 },
88 { offsetof(xfs_sb_t, sb_ifree), 0 },
89 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
90 { offsetof(xfs_sb_t, sb_frextents), 0 },
91 { offsetof(xfs_sb_t, sb_uquotino), 0 },
92 { offsetof(xfs_sb_t, sb_gquotino), 0 },
93 { offsetof(xfs_sb_t, sb_qflags), 0 },
94 { offsetof(xfs_sb_t, sb_flags), 0 },
95 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
96 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
97 { offsetof(xfs_sb_t, sb_unit), 0 },
98 { offsetof(xfs_sb_t, sb_width), 0 },
99 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
100 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
101 { offsetof(xfs_sb_t, sb_logsectsize), 0 },
102 { offsetof(xfs_sb_t, sb_logsunit), 0 },
103 { offsetof(xfs_sb_t, sb_features2), 0 },
104 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
105 { offsetof(xfs_sb_t, sb_features_compat), 0 },
106 { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
107 { offsetof(xfs_sb_t, sb_features_incompat), 0 },
108 { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
109 { offsetof(xfs_sb_t, sb_crc), 0 },
110 { offsetof(xfs_sb_t, sb_pad), 0 },
111 { offsetof(xfs_sb_t, sb_pquotino), 0 },
112 { offsetof(xfs_sb_t, sb_lsn), 0 },
113 { sizeof(xfs_sb_t), 0 }
114};
115
116/*
117 * Reference counting access wrappers to the perag structures.
118 * Because we never free per-ag structures, the only thing we
119 * have to protect against changes is the tree structure itself.
120 */
121struct xfs_perag *
122xfs_perag_get(
123 struct xfs_mount *mp,
124 xfs_agnumber_t agno)
125{
126 struct xfs_perag *pag;
127 int ref = 0;
128
129 rcu_read_lock();
130 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
131 if (pag) {
132 ASSERT(atomic_read(&pag->pag_ref) >= 0);
133 ref = atomic_inc_return(&pag->pag_ref);
134 }
135 rcu_read_unlock();
136 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
137 return pag;
138}
139
140/*
141 * search from @first to find the next perag with the given tag set.
142 */
143struct xfs_perag *
144xfs_perag_get_tag(
145 struct xfs_mount *mp,
146 xfs_agnumber_t first,
147 int tag)
148{
149 struct xfs_perag *pag;
150 int found;
151 int ref;
152
153 rcu_read_lock();
154 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
155 (void **)&pag, first, 1, tag);
156 if (found <= 0) {
157 rcu_read_unlock();
158 return NULL;
159 }
160 ref = atomic_inc_return(&pag->pag_ref);
161 rcu_read_unlock();
162 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
163 return pag;
164}
165
166void
167xfs_perag_put(
168 struct xfs_perag *pag)
169{
170 int ref;
171
172 ASSERT(atomic_read(&pag->pag_ref) > 0);
173 ref = atomic_dec_return(&pag->pag_ref);
174 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
175}
176
177/*
178 * Check the validity of the SB found.
179 */
180STATIC int
181xfs_mount_validate_sb(
182 xfs_mount_t *mp,
183 xfs_sb_t *sbp,
184 bool check_inprogress,
185 bool check_version)
186{
187
188 /*
189 * If the log device and data device have the
190 * same device number, the log is internal.
191 * Consequently, the sb_logstart should be non-zero. If
192 * we have a zero sb_logstart in this case, we may be trying to mount
193 * a volume filesystem in a non-volume manner.
194 */
195 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
196 xfs_warn(mp, "bad magic number");
197 return XFS_ERROR(EWRONGFS);
198 }
199
200
201 if (!xfs_sb_good_version(sbp)) {
202 xfs_warn(mp, "bad version");
203 return XFS_ERROR(EWRONGFS);
204 }
205
206 /*
207 * Version 5 superblock feature mask validation. Reject combinations the
208 * kernel cannot support up front before checking anything else. For
209 * write validation, we don't need to check feature masks.
210 */
211 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
212 xfs_alert(mp,
213"Version 5 superblock detected. This kernel has EXPERIMENTAL support enabled!\n"
214"Use of these features in this kernel is at your own risk!");
215
216 if (xfs_sb_has_compat_feature(sbp,
217 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
218 xfs_warn(mp,
219"Superblock has unknown compatible features (0x%x) enabled.\n"
220"Using a more recent kernel is recommended.",
221 (sbp->sb_features_compat &
222 XFS_SB_FEAT_COMPAT_UNKNOWN));
223 }
224
225 if (xfs_sb_has_ro_compat_feature(sbp,
226 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
227 xfs_alert(mp,
228"Superblock has unknown read-only compatible features (0x%x) enabled.",
229 (sbp->sb_features_ro_compat &
230 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
231 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
232 xfs_warn(mp,
233"Attempted to mount read-only compatible filesystem read-write.\n"
234"Filesystem can only be safely mounted read only.");
235 return XFS_ERROR(EINVAL);
236 }
237 }
238 if (xfs_sb_has_incompat_feature(sbp,
239 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
240 xfs_warn(mp,
241"Superblock has unknown incompatible features (0x%x) enabled.\n"
242"Filesystem can not be safely mounted by this kernel.",
243 (sbp->sb_features_incompat &
244 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
245 return XFS_ERROR(EINVAL);
246 }
247 }
248
249 if (xfs_sb_version_has_pquotino(sbp)) {
250 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
251 xfs_notice(mp,
252 "Version 5 of Super block has XFS_OQUOTA bits.\n");
253 return XFS_ERROR(EFSCORRUPTED);
254 }
255 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
256 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
257 xfs_notice(mp,
258"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.\n");
259 return XFS_ERROR(EFSCORRUPTED);
260 }
261
262 if (unlikely(
263 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
264 xfs_warn(mp,
265 "filesystem is marked as having an external log; "
266 "specify logdev on the mount command line.");
267 return XFS_ERROR(EINVAL);
268 }
269
270 if (unlikely(
271 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
272 xfs_warn(mp,
273 "filesystem is marked as having an internal log; "
274 "do not specify logdev on the mount command line.");
275 return XFS_ERROR(EINVAL);
276 }
277
278 /*
279 * More sanity checking. Most of these were stolen directly from
280 * xfs_repair.
281 */
282 if (unlikely(
283 sbp->sb_agcount <= 0 ||
284 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
285 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
286 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
287 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
288 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
289 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
290 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
291 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
292 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
293 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
294 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
295 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
296 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
297 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
298 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
299 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
300 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
301 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
302 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
303 sbp->sb_dblocks == 0 ||
304 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
305 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
306 XFS_CORRUPTION_ERROR("SB sanity check failed",
307 XFS_ERRLEVEL_LOW, mp, sbp);
308 return XFS_ERROR(EFSCORRUPTED);
309 }
310
311 /*
312 * Until this is fixed only page-sized or smaller data blocks work.
313 */
314 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
315 xfs_warn(mp,
316 "File system with blocksize %d bytes. "
317 "Only pagesize (%ld) or less will currently work.",
318 sbp->sb_blocksize, PAGE_SIZE);
319 return XFS_ERROR(ENOSYS);
320 }
321
322 /*
323 * Currently only very few inode sizes are supported.
324 */
325 switch (sbp->sb_inodesize) {
326 case 256:
327 case 512:
328 case 1024:
329 case 2048:
330 break;
331 default:
332 xfs_warn(mp, "inode size of %d bytes not supported",
333 sbp->sb_inodesize);
334 return XFS_ERROR(ENOSYS);
335 }
336
337 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
338 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
339 xfs_warn(mp,
340 "file system too large to be mounted on this system.");
341 return XFS_ERROR(EFBIG);
342 }
343
344 if (check_inprogress && sbp->sb_inprogress) {
345 xfs_warn(mp, "Offline file system operation in progress!");
346 return XFS_ERROR(EFSCORRUPTED);
347 }
348
349 /*
350 * Version 1 directory format has never worked on Linux.
351 */
352 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
353 xfs_warn(mp, "file system using version 1 directory format");
354 return XFS_ERROR(ENOSYS);
355 }
356
357 return 0;
358}
359
360void
361xfs_sb_quota_from_disk(struct xfs_sb *sbp)
362{
363 /*
364 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
365 * leads to in-core values having two different values for a quota
366 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
367 * NULLFSINO.
368 *
369 * Note that this change affect only the in-core values. These
370 * values are not written back to disk unless any quota information
371 * is written to the disk. Even in that case, sb_pquotino field is
372 * not written to disk unless the superblock supports pquotino.
373 */
374 if (sbp->sb_uquotino == 0)
375 sbp->sb_uquotino = NULLFSINO;
376 if (sbp->sb_gquotino == 0)
377 sbp->sb_gquotino = NULLFSINO;
378 if (sbp->sb_pquotino == 0)
379 sbp->sb_pquotino = NULLFSINO;
380
381 /*
382 * We need to do these manipilations only if we are working
383 * with an older version of on-disk superblock.
384 */
385 if (xfs_sb_version_has_pquotino(sbp))
386 return;
387
388 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
389 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
390 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
391 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
392 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
393 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
394 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
395
396 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
397 /*
398 * In older version of superblock, on-disk superblock only
399 * has sb_gquotino, and in-core superblock has both sb_gquotino
400 * and sb_pquotino. But, only one of them is supported at any
401 * point of time. So, if PQUOTA is set in disk superblock,
402 * copy over sb_gquotino to sb_pquotino.
403 */
404 sbp->sb_pquotino = sbp->sb_gquotino;
405 sbp->sb_gquotino = NULLFSINO;
406 }
407}
408
409void
410xfs_sb_from_disk(
411 struct xfs_sb *to,
412 xfs_dsb_t *from)
413{
414 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
415 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
416 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
417 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
418 to->sb_rextents = be64_to_cpu(from->sb_rextents);
419 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
420 to->sb_logstart = be64_to_cpu(from->sb_logstart);
421 to->sb_rootino = be64_to_cpu(from->sb_rootino);
422 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
423 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
424 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
425 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
426 to->sb_agcount = be32_to_cpu(from->sb_agcount);
427 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
428 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
429 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
430 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
431 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
432 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
433 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
434 to->sb_blocklog = from->sb_blocklog;
435 to->sb_sectlog = from->sb_sectlog;
436 to->sb_inodelog = from->sb_inodelog;
437 to->sb_inopblog = from->sb_inopblog;
438 to->sb_agblklog = from->sb_agblklog;
439 to->sb_rextslog = from->sb_rextslog;
440 to->sb_inprogress = from->sb_inprogress;
441 to->sb_imax_pct = from->sb_imax_pct;
442 to->sb_icount = be64_to_cpu(from->sb_icount);
443 to->sb_ifree = be64_to_cpu(from->sb_ifree);
444 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
445 to->sb_frextents = be64_to_cpu(from->sb_frextents);
446 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
447 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
448 to->sb_qflags = be16_to_cpu(from->sb_qflags);
449 to->sb_flags = from->sb_flags;
450 to->sb_shared_vn = from->sb_shared_vn;
451 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
452 to->sb_unit = be32_to_cpu(from->sb_unit);
453 to->sb_width = be32_to_cpu(from->sb_width);
454 to->sb_dirblklog = from->sb_dirblklog;
455 to->sb_logsectlog = from->sb_logsectlog;
456 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
457 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
458 to->sb_features2 = be32_to_cpu(from->sb_features2);
459 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
460 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
461 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
462 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
463 to->sb_features_log_incompat =
464 be32_to_cpu(from->sb_features_log_incompat);
465 to->sb_pad = 0;
466 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
467 to->sb_lsn = be64_to_cpu(from->sb_lsn);
468}
469
470static inline void
471xfs_sb_quota_to_disk(
472 xfs_dsb_t *to,
473 xfs_sb_t *from,
474 __int64_t *fields)
475{
476 __uint16_t qflags = from->sb_qflags;
477
478 /*
479 * We need to do these manipilations only if we are working
480 * with an older version of on-disk superblock.
481 */
482 if (xfs_sb_version_has_pquotino(from))
483 return;
484
485 if (*fields & XFS_SB_QFLAGS) {
486 /*
487 * The in-core version of sb_qflags do not have
488 * XFS_OQUOTA_* flags, whereas the on-disk version
489 * does. So, convert incore XFS_{PG}QUOTA_* flags
490 * to on-disk XFS_OQUOTA_* flags.
491 */
492 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
493 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
494
495 if (from->sb_qflags &
496 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
497 qflags |= XFS_OQUOTA_ENFD;
498 if (from->sb_qflags &
499 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
500 qflags |= XFS_OQUOTA_CHKD;
501 to->sb_qflags = cpu_to_be16(qflags);
502 *fields &= ~XFS_SB_QFLAGS;
503 }
504
505 /*
506 * GQUOTINO and PQUOTINO cannot be used together in versions
507 * of superblock that do not have pquotino. from->sb_flags
508 * tells us which quota is active and should be copied to
509 * disk.
510 */
511 if ((*fields & XFS_SB_GQUOTINO) &&
512 (from->sb_qflags & XFS_GQUOTA_ACCT))
513 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
514 else if ((*fields & XFS_SB_PQUOTINO) &&
515 (from->sb_qflags & XFS_PQUOTA_ACCT))
516 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
517
518 *fields &= ~(XFS_SB_PQUOTINO | XFS_SB_GQUOTINO);
519}
520
521/*
522 * Copy in core superblock to ondisk one.
523 *
524 * The fields argument is mask of superblock fields to copy.
525 */
526void
527xfs_sb_to_disk(
528 xfs_dsb_t *to,
529 xfs_sb_t *from,
530 __int64_t fields)
531{
532 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
533 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
534 xfs_sb_field_t f;
535 int first;
536 int size;
537
538 ASSERT(fields);
539 if (!fields)
540 return;
541
542 xfs_sb_quota_to_disk(to, from, &fields);
543 while (fields) {
544 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
545 first = xfs_sb_info[f].offset;
546 size = xfs_sb_info[f + 1].offset - first;
547
548 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
549
550 if (size == 1 || xfs_sb_info[f].type == 1) {
551 memcpy(to_ptr + first, from_ptr + first, size);
552 } else {
553 switch (size) {
554 case 2:
555 *(__be16 *)(to_ptr + first) =
556 cpu_to_be16(*(__u16 *)(from_ptr + first));
557 break;
558 case 4:
559 *(__be32 *)(to_ptr + first) =
560 cpu_to_be32(*(__u32 *)(from_ptr + first));
561 break;
562 case 8:
563 *(__be64 *)(to_ptr + first) =
564 cpu_to_be64(*(__u64 *)(from_ptr + first));
565 break;
566 default:
567 ASSERT(0);
568 }
569 }
570
571 fields &= ~(1LL << f);
572 }
573}
574
575static int
576xfs_sb_verify(
577 struct xfs_buf *bp,
578 bool check_version)
579{
580 struct xfs_mount *mp = bp->b_target->bt_mount;
581 struct xfs_sb sb;
582
583 xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
584
585 /*
586 * Only check the in progress field for the primary superblock as
587 * mkfs.xfs doesn't clear it from secondary superblocks.
588 */
589 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
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.
599 */
600static void
601xfs_sb_read_verify(
602 struct xfs_buf *bp)
603{
604 struct xfs_mount *mp = bp->b_target->bt_mount;
605 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
606 int error;
607
608 /*
609 * open code the version check to avoid needing to convert the entire
610 * superblock from disk order just to check the version number
611 */
612 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
613 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
614 XFS_SB_VERSION_5) ||
615 dsb->sb_crc != 0)) {
616
617 if (!xfs_verify_cksum(bp->b_addr, be16_to_cpu(dsb->sb_sectsize),
618 offsetof(struct xfs_sb, sb_crc))) {
619 error = EFSCORRUPTED;
620 goto out_error;
621 }
622 }
623 error = xfs_sb_verify(bp, true);
624
625out_error:
626 if (error) {
627 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
628 mp, bp->b_addr);
629 xfs_buf_ioerror(bp, error);
630 }
631}
632
633/*
634 * We may be probed for a filesystem match, so we may not want to emit
635 * messages when the superblock buffer is not actually an XFS superblock.
636 * If we find an XFS superblock, the run a normal, noisy mount because we are
637 * really going to mount it and want to know about errors.
638 */
639static void
640xfs_sb_quiet_read_verify(
641 struct xfs_buf *bp)
642{
643 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
644
645
646 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
647 /* XFS filesystem, verify noisily! */
648 xfs_sb_read_verify(bp);
649 return;
650 }
651 /* quietly fail */
652 xfs_buf_ioerror(bp, EWRONGFS);
653}
654
655static void
656xfs_sb_write_verify(
657 struct xfs_buf *bp)
658{
659 struct xfs_mount *mp = bp->b_target->bt_mount;
660 struct xfs_buf_log_item *bip = bp->b_fspriv;
661 int error;
662
663 error = xfs_sb_verify(bp, false);
664 if (error) {
665 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
666 mp, bp->b_addr);
667 xfs_buf_ioerror(bp, error);
668 return;
669 }
670
671 if (!xfs_sb_version_hascrc(&mp->m_sb))
672 return;
673
674 if (bip)
675 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
676
677 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
678 offsetof(struct xfs_sb, sb_crc));
679}
680
681const struct xfs_buf_ops xfs_sb_buf_ops = {
682 .verify_read = xfs_sb_read_verify,
683 .verify_write = xfs_sb_write_verify,
684};
685
686const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
687 .verify_read = xfs_sb_quiet_read_verify,
688 .verify_write = xfs_sb_write_verify,
689};
690
691/*
692 * xfs_mount_common
693 *
694 * Mount initialization code establishing various mount
695 * fields from the superblock associated with the given
696 * mount structure
697 */
698void
699xfs_sb_mount_common(
700 struct xfs_mount *mp,
701 struct xfs_sb *sbp)
702{
703 mp->m_agfrotor = mp->m_agirotor = 0;
704 spin_lock_init(&mp->m_agirotor_lock);
705 mp->m_maxagi = mp->m_sb.sb_agcount;
706 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
707 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
708 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
709 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
710 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
711 mp->m_blockmask = sbp->sb_blocksize - 1;
712 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
713 mp->m_blockwmask = mp->m_blockwsize - 1;
714
715 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
716 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
717 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
718 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
719
720 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
721 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
722 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
723 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
724
725 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
726 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
727 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
728 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
729
730 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
731 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
732 sbp->sb_inopblock);
733 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
734}
735
736/*
737 * xfs_initialize_perag_data
738 *
739 * Read in each per-ag structure so we can count up the number of
740 * allocated inodes, free inodes and used filesystem blocks as this
741 * information is no longer persistent in the superblock. Once we have
742 * this information, write it into the in-core superblock structure.
743 */
744int
745xfs_initialize_perag_data(
746 struct xfs_mount *mp,
747 xfs_agnumber_t agcount)
748{
749 xfs_agnumber_t index;
750 xfs_perag_t *pag;
751 xfs_sb_t *sbp = &mp->m_sb;
752 uint64_t ifree = 0;
753 uint64_t ialloc = 0;
754 uint64_t bfree = 0;
755 uint64_t bfreelst = 0;
756 uint64_t btree = 0;
757 int error;
758
759 for (index = 0; index < agcount; index++) {
760 /*
761 * read the agf, then the agi. This gets us
762 * all the information we need and populates the
763 * per-ag structures for us.
764 */
765 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
766 if (error)
767 return error;
768
769 error = xfs_ialloc_pagi_init(mp, NULL, index);
770 if (error)
771 return error;
772 pag = xfs_perag_get(mp, index);
773 ifree += pag->pagi_freecount;
774 ialloc += pag->pagi_count;
775 bfree += pag->pagf_freeblks;
776 bfreelst += pag->pagf_flcount;
777 btree += pag->pagf_btreeblks;
778 xfs_perag_put(pag);
779 }
780 /*
781 * Overwrite incore superblock counters with just-read data
782 */
783 spin_lock(&mp->m_sb_lock);
784 sbp->sb_ifree = ifree;
785 sbp->sb_icount = ialloc;
786 sbp->sb_fdblocks = bfree + bfreelst + btree;
787 spin_unlock(&mp->m_sb_lock);
788
789 /* Fixup the per-cpu counters as well. */
790 xfs_icsb_reinit_counters(mp);
791
792 return 0;
793}
794
795/*
796 * xfs_mod_sb() can be used to copy arbitrary changes to the
797 * in-core superblock into the superblock buffer to be logged.
798 * It does not provide the higher level of locking that is
799 * needed to protect the in-core superblock from concurrent
800 * access.
801 */
802void
803xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
804{
805 xfs_buf_t *bp;
806 int first;
807 int last;
808 xfs_mount_t *mp;
809 xfs_sb_field_t f;
810
811 ASSERT(fields);
812 if (!fields)
813 return;
814 mp = tp->t_mountp;
815 bp = xfs_trans_getsb(tp, mp, 0);
816 first = sizeof(xfs_sb_t);
817 last = 0;
818
819 /* translate/copy */
820
821 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
822
823 /* find modified range */
824 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
825 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
826 last = xfs_sb_info[f + 1].offset - 1;
827
828 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
829 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
830 first = xfs_sb_info[f].offset;
831
832 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
833 xfs_trans_log_buf(tp, bp, first, last);
834}
diff --git a/fs/xfs/xfs_sb.h b/fs/xfs/xfs_sb.h
index 40b003e47845..91e163a3794d 100644
--- a/fs/xfs/xfs_sb.h
+++ b/fs/xfs/xfs_sb.h
@@ -26,6 +26,7 @@
26 26
27struct xfs_buf; 27struct xfs_buf;
28struct xfs_mount; 28struct xfs_mount;
29struct xfs_trans;
29 30
30#define XFS_SB_MAGIC 0x58465342 /* 'XFSB' */ 31#define XFS_SB_MAGIC 0x58465342 /* 'XFSB' */
31#define XFS_SB_VERSION_1 1 /* 5.3, 6.0.1, 6.1 */ 32#define XFS_SB_VERSION_1 1 /* 5.3, 6.0.1, 6.1 */
@@ -674,4 +675,23 @@ xfs_is_quota_inode(struct xfs_sb *sbp, xfs_ino_t ino)
674#define XFS_B_TO_FSBT(mp,b) (((__uint64_t)(b)) >> (mp)->m_sb.sb_blocklog) 675#define XFS_B_TO_FSBT(mp,b) (((__uint64_t)(b)) >> (mp)->m_sb.sb_blocklog)
675#define XFS_B_FSB_OFFSET(mp,b) ((b) & (mp)->m_blockmask) 676#define XFS_B_FSB_OFFSET(mp,b) ((b) & (mp)->m_blockmask)
676 677
678/*
679 * perag get/put wrappers for ref counting
680 */
681extern struct xfs_perag *xfs_perag_get(struct xfs_mount *, xfs_agnumber_t);
682extern struct xfs_perag *xfs_perag_get_tag(struct xfs_mount *, xfs_agnumber_t,
683 int tag);
684extern void xfs_perag_put(struct xfs_perag *pag);
685extern int xfs_initialize_perag_data(struct xfs_mount *, xfs_agnumber_t);
686
687extern void xfs_sb_calc_crc(struct xfs_buf *);
688extern void xfs_mod_sb(struct xfs_trans *, __int64_t);
689extern void xfs_sb_mount_common(struct xfs_mount *, struct xfs_sb *);
690extern void xfs_sb_from_disk(struct xfs_sb *, struct xfs_dsb *);
691extern void xfs_sb_to_disk(struct xfs_dsb *, struct xfs_sb *, __int64_t);
692extern void xfs_sb_quota_from_disk(struct xfs_sb *sbp);
693
694extern const struct xfs_buf_ops xfs_sb_buf_ops;
695extern const struct xfs_buf_ops xfs_sb_quiet_buf_ops;
696
677#endif /* __XFS_SB_H__ */ 697#endif /* __XFS_SB_H__ */