Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys move bi-endian disklabel support from the kernel and lib...



details:   https://anonhg.NetBSD.org/src/rev/f14b7e1038b3
branches:  trunk
changeset: 379176:f14b7e1038b3
user:      mrg <mrg%NetBSD.org@localhost>
date:      Mon May 17 08:50:36 2021 +0000

description:
move bi-endian disklabel support from the kernel and libsa into libkern.

- dkcksum() and dkcksum_sized() move from subr_disk.c and from
  libsa into libkern/dkcksum.c (which is missing _sized() version),
  using the version from usr.sbin/disklabel.

- swap_disklabel() moves from subr_disk_mbr.c into libkern, now called
  disklabel_swap().  (the sh3 version should be updated to use this.)

- DISKLABEL_EI becomes a first-class option with opt_disklabel.h.

- add libkern.h to libsa/disklabel.c.

this enables future work for bi-endian libsa/ufs.c (relevant for ffsv1,
ffsv2, lfsv1, and lfsv2), as well as making it possible for ports not
using subr_disk_mbr.c to include bi-endian disklabel support (which,
afaict, includes any disk on mbr-supporting platforms that do not have
an mbr as well as disklabel.)

builds successsfully on: alpha, i386, amd64, sun2, sun3, evbarm64,
evbarm64-eb, sparc, and sparc64.  tested in anita on i386 and sparc,
testing in hardware on evbarm64*.

diffstat:

 sys/kern/files.kern              |    3 +-
 sys/kern/subr_disk.c             |   27 +-------
 sys/kern/subr_disk_mbr.c         |  101 ++--------------------------
 sys/lib/libkern/Makefile.libkern |    5 +-
 sys/lib/libkern/disklabel_swap.c |  135 +++++++++++++++++++++++++++++++++++++++
 sys/lib/libkern/dkcksum.c        |   67 +++++++++++++++++++
 sys/lib/libkern/libkern.h        |    7 +-
 sys/lib/libsa/Makefile           |    4 +-
 sys/lib/libsa/disklabel.c        |    3 +-
 sys/lib/libsa/dkcksum.c          |   52 ---------------
 sys/lib/libsa/stand.h            |    3 +-
 sys/sys/disklabel.h              |    4 +-
 12 files changed, 231 insertions(+), 180 deletions(-)

diffs (truncated from 586 to 300 lines):

diff -r 1c41854be901 -r f14b7e1038b3 sys/kern/files.kern
--- a/sys/kern/files.kern       Mon May 17 06:31:30 2021 +0000
+++ b/sys/kern/files.kern       Mon May 17 08:50:36 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.kern,v 1.54 2020/11/01 18:51:02 pgoyette Exp $
+#      $NetBSD: files.kern,v 1.55 2021/05/17 08:50:36 mrg Exp $
 
 #
 # kernel sources
@@ -115,6 +115,7 @@ file        kern/subr_device.c              kern
 file   kern/subr_devsw.c               kern
 file   kern/subr_disk.c                kern
 file   kern/subr_disklabel.c           kern
+defopt opt_disklabel.h                 DISKLABEL_EI
 file   kern/subr_disk_open.c           kern
 file   kern/subr_emul.c                kern
 file   kern/subr_evcnt.c               kern
diff -r 1c41854be901 -r f14b7e1038b3 sys/kern/subr_disk.c
--- a/sys/kern/subr_disk.c      Mon May 17 06:31:30 2021 +0000
+++ b/sys/kern/subr_disk.c      Mon May 17 08:50:36 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_disk.c,v 1.132 2020/10/17 09:42:35 mlelstv Exp $  */
+/*     $NetBSD: subr_disk.c,v 1.133 2021/05/17 08:50:36 mrg Exp $      */
 
 /*-
  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.132 2020/10/17 09:42:35 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.133 2021/05/17 08:50:36 mrg Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -81,29 +81,6 @@
 #include <lib/libkern/libkern.h>
 
 /*
- * Compute checksum for disk label.
- */
-u_int
-dkcksum(struct disklabel *lp)
-{
-
-       return dkcksum_sized(lp, lp->d_npartitions);
-}
-
-u_int
-dkcksum_sized(struct disklabel *lp, size_t npartitions)
-{
-       uint16_t *start, *end;
-       uint16_t sum = 0;
-
-       start = (uint16_t *)lp;
-       end = (uint16_t *)&lp->d_partitions[npartitions];
-       while (start < end)
-               sum ^= *start++;
-       return sum;
-}
-
-/*
  * Disk error is the preface to plaintive error messages
  * about failing disk transfers.  It prints messages of the form
 
diff -r 1c41854be901 -r f14b7e1038b3 sys/kern/subr_disk_mbr.c
--- a/sys/kern/subr_disk_mbr.c  Mon May 17 06:31:30 2021 +0000
+++ b/sys/kern/subr_disk_mbr.c  Mon May 17 08:50:36 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_disk_mbr.c,v 1.56 2019/11/07 20:34:29 kamil Exp $ */
+/*     $NetBSD: subr_disk_mbr.c,v 1.57 2021/05/17 08:50:36 mrg Exp $   */
 
 /*
  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
@@ -54,7 +54,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.56 2019/11/07 20:34:29 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_disk_mbr.c,v 1.57 2021/05/17 08:50:36 mrg Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_mbr.h"
+#include "opt_disklabel.h"
+#endif /* _KERNEL_OPT */
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -72,10 +77,6 @@
 
 #include <sys/kauth.h>
 
-#ifdef _KERNEL_OPT
-#include "opt_mbr.h"
-#endif /* _KERNEL_OPT */
-
 typedef struct mbr_partition mbr_partition_t;
 
 /*
@@ -117,10 +118,6 @@ static int validate_label(mbr_args_t *, 
 static int look_netbsd_part(mbr_args_t *, mbr_partition_t *, int, uint);
 static int write_netbsd_label(mbr_args_t *, mbr_partition_t *, int, uint);
 
-#ifdef DISKLABEL_EI
-static void swap_disklabel(struct disklabel *, struct disklabel *);
-#endif
-
 static int
 read_sector(mbr_args_t *a, uint sector, int count)
 {
@@ -659,7 +656,7 @@ corrupted:
        case READ_LABEL:
 #ifdef DISKLABEL_EI
                if (swapped)
-                       swap_disklabel(a->lp, dlp);
+                       disklabel_swap(a->lp, dlp);
                else
                        *a->lp = *dlp;
 #else
@@ -675,7 +672,7 @@ corrupted:
 #ifdef DISKLABEL_EI
                /* DO NOT swap a->lp itself for later references. */
                if (swapped)
-                       swap_disklabel(dlp, a->lp);
+                       disklabel_swap(dlp, a->lp);
                else
                        *dlp = *a->lp;
 #else
@@ -744,83 +741,3 @@ write_netbsd_label(mbr_args_t *a, mbr_pa
 
        return validate_label(a, ptn_base);
 }
-
-#ifdef DISKLABEL_EI
-/*
- * from sh3/disksubr.c with modifications:
- *     - update d_checksum properly
- *     - replace memcpy(9) by memmove(9) as a precaution
- */
-static void
-swap_disklabel(struct disklabel *nlp, struct disklabel *olp)
-{
-       int i;
-       uint16_t npartitions;
-
-#define        SWAP16(x)       nlp->x = bswap16(olp->x)
-#define        SWAP32(x)       nlp->x = bswap32(olp->x)
-
-       SWAP32(d_magic);
-       SWAP16(d_type);
-       SWAP16(d_subtype);
-       /* Do not need to swap char strings. */
-       memmove(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
-
-       /* XXX What should we do for d_un (an union of char and pointers) ? */
-       memmove(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
-
-       SWAP32(d_secsize);
-       SWAP32(d_nsectors);
-       SWAP32(d_ntracks);
-       SWAP32(d_ncylinders);
-       SWAP32(d_secpercyl);
-       SWAP32(d_secperunit);
-
-       SWAP16(d_sparespertrack);
-       SWAP16(d_sparespercyl);
-
-       SWAP32(d_acylinders);
-
-       SWAP16(d_rpm);
-       SWAP16(d_interleave);
-       SWAP16(d_trackskew);
-       SWAP16(d_cylskew);
-       SWAP32(d_headswitch);
-       SWAP32(d_trkseek);
-       SWAP32(d_flags);
-       for (i = 0; i < NDDATA; i++)
-               SWAP32(d_drivedata[i]);
-       for (i = 0; i < NSPARE; i++)
-               SWAP32(d_spare[i]);
-       SWAP32(d_magic2);
-       /* d_checksum is updated later. */
-
-       SWAP16(d_npartitions);
-       SWAP32(d_bbsize);
-       SWAP32(d_sbsize);
-       for (i = 0; i < MAXPARTITIONS; i++) {
-               SWAP32(d_partitions[i].p_size);
-               SWAP32(d_partitions[i].p_offset);
-               SWAP32(d_partitions[i].p_fsize);
-               /* p_fstype and p_frag is uint8_t, so no need to swap. */
-               nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
-               nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
-               SWAP16(d_partitions[i].p_cpg);
-       }
-
-#undef SWAP16
-#undef SWAP32
-
-       /* Update checksum in the target endian. */
-       nlp->d_checksum = 0;
-       npartitions = nlp->d_magic == DISKMAGIC ?
-           nlp->d_npartitions : olp->d_npartitions;
-       /*
-        * npartitions can be larger than MAXPARTITIONS when the label was not
-        * validated by setdisklabel. If so, the label is intentionally(?)
-        * corrupted and checksum should be meaningless.
-        */
-       if (npartitions <= MAXPARTITIONS)
-               nlp->d_checksum = dkcksum_sized(nlp, npartitions);
-}
-#endif /* DISKLABEL_EI */
diff -r 1c41854be901 -r f14b7e1038b3 sys/lib/libkern/Makefile.libkern
--- a/sys/lib/libkern/Makefile.libkern  Mon May 17 06:31:30 2021 +0000
+++ b/sys/lib/libkern/Makefile.libkern  Mon May 17 08:50:36 2021 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.libkern,v 1.50 2021/01/25 12:45:49 thorpej Exp $
+#      $NetBSD: Makefile.libkern,v 1.51 2021/05/17 08:50:36 mrg Exp $
 
 #
 # Variable definitions for libkern.
@@ -99,6 +99,9 @@ SRCS+=        explicit_memset.c consttime_memeq
 
 SRCS+= entpool.c
 
+SRCS+= dkcksum.c
+SRCS+= disklabel_swap.c
+
 .PATH: ${NETBSDSRCDIR}/common/lib/libc/cdb
 SRCS+= cdbr.c
 SRCS+= mi_vector_hash.c
diff -r 1c41854be901 -r f14b7e1038b3 sys/lib/libkern/disklabel_swap.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libkern/disklabel_swap.c  Mon May 17 08:50:36 2021 +0000
@@ -0,0 +1,135 @@
+/*     $NetBSD: disklabel_swap.c,v 1.1 2021/05/17 08:50:36 mrg Exp $   */
+
+/*
+ * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)ufs_disksubr.c      7.16 (Berkeley) 5/4/91
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: disklabel_swap.c,v 1.1 2021/05/17 08:50:36 mrg Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_disklabel.h"
+#endif /* _KERNEL_OPT */
+
+#if defined(DISKLABEL_EI) || defined(LIBSA_DISKLABEL_EI)
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/disklabel.h>
+#include <sys/conf.h>
+#include <lib/libkern/libkern.h>
+
+/*
+ * from sh3/disksubr.c and kern/subr_disk_mbr.c with modifications:
+ *     - update d_checksum properly
+ *     - replace memcpy(9) by memmove(9) as a precaution
+ *     - avoid memmove(9) for libkern version, check if the labels
+ *       are the same and skip copying in-place.
+ */
+void
+disklabel_swap(struct disklabel *nlp, struct disklabel *olp)
+{
+       int i;
+       uint16_t npartitions;
+
+#define        SWAP16(x)       nlp->x = bswap16(olp->x)
+#define        SWAP32(x)       nlp->x = bswap32(olp->x)
+
+       SWAP32(d_magic);



Home | Main Index | Thread Index | Old Index