Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Add a bounds_check_with_mediasize function, which i...



details:   https://anonhg.NetBSD.org/src/rev/93d22bcd6ddc
branches:  trunk
changeset: 545172:93d22bcd6ddc
user:      fvdl <fvdl%NetBSD.org@localhost>
date:      Thu Apr 03 22:20:24 2003 +0000

description:
Add a bounds_check_with_mediasize function, which is intended
for checking RAW_PART transfers (and later raw disk devices).

diffstat:

 sys/kern/subr_disk.c |  40 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 38 insertions(+), 2 deletions(-)

diffs (58 lines):

diff -r 410ce61884ff -r 93d22bcd6ddc sys/kern/subr_disk.c
--- a/sys/kern/subr_disk.c      Thu Apr 03 22:19:16 2003 +0000
+++ b/sys/kern/subr_disk.c      Thu Apr 03 22:20:24 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_disk.c,v 1.49 2002/11/06 02:31:34 enami Exp $     */
+/*     $NetBSD: subr_disk.c,v 1.50 2003/04/03 22:20:24 fvdl Exp $      */
 
 /*-
  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.49 2002/11/06 02:31:34 enami Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.50 2003/04/03 22:20:24 fvdl Exp $");
 
 #include "opt_compat_netbsd.h"
 
@@ -817,3 +817,39 @@
        bufq->bq_get = NULL;
        bufq->bq_put = NULL;
 }
+
+/*
+ * Bounds checking against the media size, used for the raw partition.
+ * The sector size passed in should currently always be DEV_BSIZE,
+ * and the media size the size of the device in DEV_BSIZE sectors.
+ */
+int
+bounds_check_with_mediasize(struct buf *bp, int secsize, u_int64_t mediasize)
+{
+       int sz;
+
+       sz = howmany(bp->b_bcount, secsize);
+
+       if (bp->b_blkno + sz > mediasize) {
+               sz = mediasize - bp->b_blkno;
+               if (sz == 0) {
+                       /* If exactly at end of disk, return EOF. */
+                       bp->b_resid = bp->b_bcount;
+                       goto done;
+               }
+               if (sz < 0) {
+                       /* If past end of disk, return EINVAL. */
+                       bp->b_error = EINVAL;
+                       goto bad;
+               }
+               /* Otherwise, truncate request. */
+               bp->b_bcount = sz << DEV_BSHIFT;
+       }
+
+       return 1;
+
+bad:
+       bp->b_flags |= B_ERROR;
+done:
+       return 0;
+}



Home | Main Index | Thread Index | Old Index