Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/dkwedge allocate buffer for disk I/O via geteblk() i...



details:   https://anonhg.NetBSD.org/src/rev/c74c17dab618
branches:  trunk
changeset: 930664:c74c17dab618
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Sat Apr 11 16:00:34 2020 +0000

description:
allocate buffer for disk I/O via geteblk() instead of malloc(), so they
are properly aligned; e.g. readdisklabel() does the same

also removed the DKW_MALLOC()/DKW_FREE()/DKW_REALLOC() macros as apparently
unnecessary, these files don't seem to be compiled into any userland tools

dkwedge_gpt.c confirmed working, others compile-tested only

diffstat:

 sys/dev/dkwedge/dkwedge_apple.c    |  30 +++++++++++-------------------
 sys/dev/dkwedge/dkwedge_bsdlabel.c |  35 ++++++++++++++---------------------
 sys/dev/dkwedge/dkwedge_gpt.c      |  33 ++++++++++++++++++---------------
 sys/dev/dkwedge/dkwedge_mbr.c      |  17 +++++++++--------
 sys/dev/dkwedge/dkwedge_rdb.c      |  27 ++++++++-------------------
 5 files changed, 60 insertions(+), 82 deletions(-)

diffs (truncated from 481 to 300 lines):

diff -r 07a541fc9edb -r c74c17dab618 sys/dev/dkwedge/dkwedge_apple.c
--- a/sys/dev/dkwedge/dkwedge_apple.c   Sat Apr 11 14:48:19 2020 +0000
+++ b/sys/dev/dkwedge/dkwedge_apple.c   Sat Apr 11 16:00:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dkwedge_apple.c,v 1.5 2019/07/09 17:06:46 maxv Exp $   */
+/*     $NetBSD: dkwedge_apple.c,v 1.6 2020/04/11 16:00:34 jdolecek Exp $       */
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dkwedge_apple.c,v 1.5 2019/07/09 17:06:46 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dkwedge_apple.c,v 1.6 2020/04/11 16:00:34 jdolecek Exp $");
 
 #include <sys/param.h>
 #ifdef _KERNEL
@@ -44,8 +44,8 @@
 #include <sys/errno.h>
 #include <sys/disk.h>
 #include <sys/vnode.h>
-#include <sys/malloc.h>
 #include <sys/bitops.h>
+#include <sys/buf.h>
 
 #include <sys/bootblock.h>
 
@@ -123,14 +123,6 @@
 
 #define ASIZE  16384
  
-#ifdef _KERNEL
-#define        DKW_MALLOC(SZ)  malloc((SZ), M_DEVBUF, M_WAITOK)
-#define        DKW_FREE(PTR)   free((PTR), M_DEVBUF)
-#else
-#define        DKW_MALLOC(SZ)  malloc((SZ))
-#define        DKW_FREE(PTR)   free((PTR))
-#endif
-
 static struct {
        const char *name;
        const char *type;
@@ -145,20 +137,20 @@
 {
        size_t i, n;
        int error;
-       void *buf;
+       struct buf *bp;
        uint32_t blocksize, blockcount, offset, rsize;
        struct apple_drvr_map *am;
        struct apple_part_map_entry *ae;
        struct apple_blockzeroblock ab;
        const char *ptype;
 
-       buf = DKW_MALLOC(ASIZE);
-       if ((error = dkwedge_read(pdk, vp, 0, buf, ASIZE)) != 0) {
+       bp = geteblk(ASIZE);
+       if ((error = dkwedge_read(pdk, vp, 0, bp->b_data, ASIZE)) != 0) {
                DPRINTF("%s: read @%u %d\n", __func__, 0, error);
                goto out;
        }
 
-       am = buf;
+       am = bp->b_data;
        swap_apple_drvr_map(am);
 
        error = ESRCH;
@@ -186,12 +178,12 @@
                blockcount = 512;
        }
 
-       ae = buf;
+       ae = bp->b_data;
        offset = blocksize;
        for (n = 0; n < blockcount; n++, offset += rsize) {
                DPRINTF("%s: offset %x rsize %x\n", __func__, offset, rsize);
-               if ((error = dkwedge_read(pdk, vp, offset / DEV_BSIZE, buf,
-                   rsize)) != 0) {
+               if ((error = dkwedge_read(pdk, vp, offset / DEV_BSIZE,
+                   bp->b_data, rsize)) != 0) {
                        DPRINTF("%s: read @%u %d\n", __func__, offset,
                            error);
                        goto out;
@@ -244,7 +236,7 @@
        }
 
 out:
-       DKW_FREE(buf);
+       brelse(bp, 0);
        DPRINTF("%s: return %d\n", __func__, error);
        return error;
 }
diff -r 07a541fc9edb -r c74c17dab618 sys/dev/dkwedge/dkwedge_bsdlabel.c
--- a/sys/dev/dkwedge/dkwedge_bsdlabel.c        Sat Apr 11 14:48:19 2020 +0000
+++ b/sys/dev/dkwedge/dkwedge_bsdlabel.c        Sat Apr 11 16:00:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dkwedge_bsdlabel.c,v 1.24 2019/07/09 17:06:46 maxv Exp $       */
+/*     $NetBSD: dkwedge_bsdlabel.c,v 1.25 2020/04/11 16:00:34 jdolecek Exp $   */
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -79,7 +79,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dkwedge_bsdlabel.c,v 1.24 2019/07/09 17:06:46 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dkwedge_bsdlabel.c,v 1.25 2020/04/11 16:00:34 jdolecek Exp $");
 
 #include <sys/param.h>
 #ifdef _KERNEL
@@ -89,7 +89,7 @@
 #include <sys/errno.h>
 #include <sys/disk.h>
 #include <sys/vnode.h>
-#include <sys/malloc.h>
+#include <sys/buf.h>
 
 #include <sys/bootblock.h>
 #include <sys/disklabel.h>
@@ -135,7 +135,7 @@
 typedef struct mbr_args {
        struct disk     *pdk;
        struct vnode    *vp;
-       void            *buf;
+       struct buf      *bp;
        int             error;
        uint32_t        secsize;
 } mbr_args_t;
@@ -278,7 +278,8 @@
        int error, swapped;
        uint16_t npartitions;
 
-       error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, a->secsize);
+       error = dkwedge_read(a->pdk, a->vp, label_sector, a->bp->b_data,
+           a->secsize);
        if (error) {
                aprint_error("%s: unable to read BSD disklabel @ %" PRId64
                    ", error = %d\n", a->pdk->dk_name, label_sector, error);
@@ -291,12 +292,12 @@
         * consistently in the old code, requiring us to do the search
         * in the sector.
         */
-       lp = a->buf;
-       lp_lim = (char *)a->buf + a->secsize - DISKLABEL_MINSIZE;
+       lp = a->bp->b_data;
+       lp_lim = (char *)a->bp->b_data + a->secsize - DISKLABEL_MINSIZE;
        for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
                if ((char *)lp > (char *)lp_lim)
                        return (SCAN_CONTINUE);
-               label_offset = (size_t)((char *)lp - (char *)a->buf);
+               label_offset = (size_t)((char *)lp - (char *)a->bp->b_data);
                if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
                        if (lp->d_magic != bswap32(DISKMAGIC) ||
                            lp->d_magic2 != bswap32(DISKMAGIC))
@@ -311,7 +312,7 @@
 
                /* Validate label length. */
                if ((char *)lp + DISKLABEL_SIZE(npartitions) >
-                   (char *)a->buf + a->secsize) {
+                   (char *)a->bp->b_data + a->secsize) {
                        aprint_error("%s: BSD disklabel @ "
                            "%" PRId64 "+%zd has bogus partition count (%u)\n",
                            a->pdk->dk_name, label_sector, label_offset,
@@ -354,7 +355,7 @@
        ext_base = 0;
        this_ext = 0;
        for (;;) {
-               a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
+               a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->bp->b_data,
                                        a->secsize);
                if (a->error) {
                        aprint_error("%s: unable to read MBR @ %u, "
@@ -363,7 +364,7 @@
                        return (SCAN_ERROR);
                }
 
-               mbr = a->buf;
+               mbr = a->bp->b_data;
                if (mbr->mbr_magic != htole16(MBR_MAGIC))
                        return (SCAN_CONTINUE);
 
@@ -435,14 +436,6 @@
        return (SCAN_CONTINUE);
 }
  
-#ifdef _KERNEL
-#define        DKW_MALLOC(SZ)  malloc((SZ), M_DEVBUF, M_WAITOK)
-#define        DKW_FREE(PTR)   free((PTR), M_DEVBUF)
-#else
-#define        DKW_MALLOC(SZ)  malloc((SZ))
-#define        DKW_FREE(PTR)   free((PTR))
-#endif
-
 static int
 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
 {
@@ -453,7 +446,7 @@
        a.pdk = pdk;
        a.secsize = DEV_BSIZE << pdk->dk_blkshift;
        a.vp = vp;
-       a.buf = DKW_MALLOC(a.secsize);
+       a.bp = geteblk(a.secsize);
        a.error = 0;
 
        /* MBR search. */
@@ -477,7 +470,7 @@
        /* No NetBSD disklabel found. */
        a.error = ESRCH;
  out:
-       DKW_FREE(a.buf);
+       brelse(a.bp, 0);
        return (a.error);
 }
 
diff -r 07a541fc9edb -r c74c17dab618 sys/dev/dkwedge/dkwedge_gpt.c
--- a/sys/dev/dkwedge/dkwedge_gpt.c     Sat Apr 11 14:48:19 2020 +0000
+++ b/sys/dev/dkwedge/dkwedge_gpt.c     Sat Apr 11 16:00:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dkwedge_gpt.c,v 1.25 2020/03/30 08:36:09 wiz Exp $     */
+/*     $NetBSD: dkwedge_gpt.c,v 1.26 2020/04/11 16:00:34 jdolecek Exp $        */
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.25 2020/03/30 08:36:09 wiz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dkwedge_gpt.c,v 1.26 2020/04/11 16:00:34 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -42,7 +42,7 @@
 #include <sys/errno.h>
 #include <sys/disk.h>
 #include <sys/vnode.h>
-#include <sys/malloc.h>
+#include <sys/buf.h>
 
 #include <sys/disklabel_gpt.h>
 #include <sys/uuid.h>
@@ -115,7 +115,7 @@
        static const struct uuid ent_type_unused = GPT_ENT_TYPE_UNUSED;
        static const char gpt_hdr_sig[] = GPT_HDR_SIG;
        struct dkwedge_info dkw;
-       void *buf;
+       struct buf *bp;
        uint32_t secsize;
        struct gpt_hdr *hdr;
        struct gpt_ent *ent;
@@ -124,11 +124,11 @@
        uint32_t gpe_crc;
        int error;
        u_int i;
-       size_t r, n;
+       size_t r, n, sz;
        uint8_t *c;
 
        secsize = DEV_BSIZE << pdk->dk_blkshift;
-       buf = malloc(secsize, M_DEVBUF, M_WAITOK);
+       bp = geteblk(secsize);
 
        /*
         * Note: We don't bother with a Legacy or Protective MBR
@@ -137,10 +137,11 @@
         */
 
        /* Read in the GPT Header. */
-       error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO << pdk->dk_blkshift, buf, secsize);
+       error = dkwedge_read(pdk, vp, GPT_HDR_BLKNO << pdk->dk_blkshift,
+           bp->b_data, secsize);
        if (error)
                goto out;
-       hdr = buf;
+       hdr = bp->b_data;
 
        /* Validate it. */
        if (memcmp(gpt_hdr_sig, hdr->hdr_sig, sizeof(hdr->hdr_sig)) != 0) {
@@ -201,10 +202,12 @@
                goto out;
        }
 
-       free(buf, M_DEVBUF);
-       buf = malloc(roundup(entries * entsz, secsize), M_DEVBUF, M_WAITOK);
-       error = dkwedge_read(pdk, vp, lba_table << pdk->dk_blkshift, buf,
-                            roundup(entries * entsz, secsize));
+       brelse(bp, 0);
+
+       sz = roundup(entries * entsz, secsize);
+       bp = geteblk(sz);
+       error = dkwedge_read(pdk, vp, lba_table << pdk->dk_blkshift,
+           bp->b_data, sz);
        if (error) {
                /* XXX Should check alternate location. */
                aprint_error("%s: unable to read GPT partition array, "
@@ -212,7 +215,7 @@
                goto out;
        }
 



Home | Main Index | Thread Index | Old Index