Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / vfs / specfs / spec_vnops.c
index f19ba42..878fd73 100644 (file)
@@ -32,7 +32,7 @@
  *
  *     @(#)spec_vnops.c        8.14 (Berkeley) 5/21/95
  * $FreeBSD: src/sys/miscfs/specfs/spec_vnops.c,v 1.131.2.4 2001/02/26 04:23:20 jlemon Exp $
- * $DragonFly: src/sys/vfs/specfs/spec_vnops.c,v 1.33 2006/03/29 18:45:03 dillon Exp $
+ * $DragonFly: src/sys/vfs/specfs/spec_vnops.c,v 1.49 2006/09/10 01:26:41 dillon Exp $
  */
 
 #include <sys/param.h>
@@ -54,6 +54,8 @@
 #include <vm/vm_page.h>
 #include <vm/vm_pager.h>
 
+#include <machine/limits.h>
+
 #include <sys/buf2.h>
 
 #include <sys/thread2.h>
@@ -74,54 +76,53 @@ static int  spec_read (struct vop_read_args *);
 static int     spec_strategy (struct vop_strategy_args *);
 static int     spec_write (struct vop_write_args *);
 
-struct vop_ops *spec_vnode_vops;
-struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
-       { &vop_default_desc,            vop_defaultop },
-       { &vop_access_desc,             vop_ebadf },
-       { &vop_advlock_desc,            (vnodeopv_entry_t) spec_advlock },
-       { &vop_bmap_desc,               (vnodeopv_entry_t) spec_bmap },
-       { &vop_close_desc,              (vnodeopv_entry_t) spec_close },
-       { &vop_old_create_desc,         vop_panic },
-       { &vop_freeblks_desc,           (vnodeopv_entry_t) spec_freeblks },
-       { &vop_fsync_desc,              (vnodeopv_entry_t) spec_fsync },
-       { &vop_getpages_desc,           (vnodeopv_entry_t) spec_getpages },
-       { &vop_inactive_desc,           (vnodeopv_entry_t) spec_inactive },
-       { &vop_ioctl_desc,              (vnodeopv_entry_t) spec_ioctl },
-       { &vop_old_link_desc,           vop_panic },
-       { &vop_old_mkdir_desc,          vop_panic },
-       { &vop_old_mknod_desc,          vop_panic },
-       { &vop_open_desc,               (vnodeopv_entry_t) spec_open },
-       { &vop_pathconf_desc,           (vnodeopv_entry_t) vop_stdpathconf },
-       { &vop_poll_desc,               (vnodeopv_entry_t) spec_poll },
-       { &vop_kqfilter_desc,           (vnodeopv_entry_t) spec_kqfilter },
-       { &vop_print_desc,              (vnodeopv_entry_t) spec_print },
-       { &vop_read_desc,               (vnodeopv_entry_t) spec_read },
-       { &vop_readdir_desc,            vop_panic },
-       { &vop_readlink_desc,           vop_panic },
-       { &vop_reallocblks_desc,        vop_panic },
-       { &vop_reclaim_desc,            vop_null },
-       { &vop_old_remove_desc,         vop_panic },
-       { &vop_old_rename_desc,         vop_panic },
-       { &vop_old_rmdir_desc,          vop_panic },
-       { &vop_setattr_desc,            vop_ebadf },
-       { &vop_strategy_desc,           (vnodeopv_entry_t) spec_strategy },
-       { &vop_old_symlink_desc,        vop_panic },
-       { &vop_write_desc,              (vnodeopv_entry_t) spec_write },
-       { NULL, NULL }
+struct vop_ops spec_vnode_vops = {
+       .vop_default =          vop_defaultop,
+       .vop_access =           (void *)vop_ebadf,
+       .vop_advlock =          spec_advlock,
+       .vop_bmap =             spec_bmap,
+       .vop_close =            spec_close,
+       .vop_old_create =       (void *)vop_panic,
+       .vop_freeblks =         spec_freeblks,
+       .vop_fsync =            spec_fsync,
+       .vop_getpages =         spec_getpages,
+       .vop_inactive =         spec_inactive,
+       .vop_ioctl =            spec_ioctl,
+       .vop_old_link =         (void *)vop_panic,
+       .vop_old_mkdir =        (void *)vop_panic,
+       .vop_old_mknod =        (void *)vop_panic,
+       .vop_open =             spec_open,
+       .vop_pathconf =         vop_stdpathconf,
+       .vop_poll =             spec_poll,
+       .vop_kqfilter =         spec_kqfilter,
+       .vop_print =            spec_print,
+       .vop_read =             spec_read,
+       .vop_readdir =          (void *)vop_panic,
+       .vop_readlink =         (void *)vop_panic,
+       .vop_reallocblks =      (void *)vop_panic,
+       .vop_reclaim =          (void *)vop_null,
+       .vop_old_remove =       (void *)vop_panic,
+       .vop_old_rename =       (void *)vop_panic,
+       .vop_old_rmdir =        (void *)vop_panic,
+       .vop_setattr =          (void *)vop_ebadf,
+       .vop_strategy =         spec_strategy,
+       .vop_old_symlink =      (void *)vop_panic,
+       .vop_write =            spec_write
 };
-static struct vnodeopv_desc spec_vnodeop_opv_desc =
-       { &spec_vnode_vops, spec_vnodeop_entries, 0 };
-VNODEOP_SET(spec_vnodeop_opv_desc);
+
+struct vop_ops *spec_vnode_vops_p = &spec_vnode_vops;
+
+VNODEOP_SET(spec_vnode_vops);
 
 extern int dev_ref_debug;
 
 /*
- * spec_vnoperate(struct vnodeop_desc *a_desc, ...)
+ * spec_vnoperate()
  */
 int
 spec_vnoperate(struct vop_generic_args *ap)
 {
-       return (VOCALL(spec_vnode_vops, ap));
+       return (VOCALL(&spec_vnode_vops, ap));
 }
 
 static void spec_getpages_iodone (struct bio *bio);
@@ -130,14 +131,14 @@ static void spec_getpages_iodone (struct bio *bio);
  * Open a special file.
  *
  * spec_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
- *          struct thread *a_td)
+ *          struct file *a_fp)
  */
 /* ARGSUSED */
 static int
 spec_open(struct vop_open_args *ap)
 {
        struct vnode *vp = ap->a_vp;
-       dev_t dev;
+       cdev_t dev;
        int error;
        int isblk = (vp->v_type == VBLK) ? 1 : 0;
        const char *cp;
@@ -183,7 +184,6 @@ spec_open(struct vop_open_args *ap)
        /*
         * Prevent degenerate open/close sequences from nulling out rdev.
         */
-       ++vp->v_opencount;
        dev = vp->v_rdev;
        KKASSERT(dev != NULL);
 
@@ -239,9 +239,9 @@ spec_open(struct vop_open_args *ap)
         * dev_dopen() is always called for each open.  dev_dclose() is
         * only called for the last close unless D_TRACKCLOSE is set.
         */
-       VOP_UNLOCK(vp, 0, ap->a_td);
-       error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_td);
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
+       vn_unlock(vp);
+       error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
+       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 
        if (error)
                goto done;
@@ -264,12 +264,12 @@ spec_open(struct vop_open_args *ap)
        if (vn_isdisk(vp, NULL)) {
                if (!dev->si_bsize_phys)
                        dev->si_bsize_phys = DEV_BSIZE;
-               vinitvmio(vp);
+               vinitvmio(vp, IDX_TO_OFF(INT_MAX));
        }
        if ((dev_dflags(dev) & D_DISK) == 0) {
                cp = devtoname(dev);
                if (*cp == '#') {
-                       printf("WARNING: driver %s should register devices with make_dev() (dev_t = \"%s\")\n",
+                       printf("WARNING: driver %s should register devices with make_dev() (cdev_t = \"%s\")\n",
                            dev_dname(dev), cp);
                }
        }
@@ -287,9 +287,10 @@ spec_open(struct vop_open_args *ap)
                printf("spec_open: %s %d\n", dev->si_name, vp->v_opencount);
 done:
        if (error) {
-               KKASSERT(vp->v_opencount > 0);
-               if (--vp->v_opencount == 0)
+               if (vp->v_opencount == 0)
                        v_release_rdev(vp);
+       } else {
+               vop_stdopen(ap);
        }
        return (error);
 }
@@ -307,7 +308,7 @@ spec_read(struct vop_read_args *ap)
        struct vnode *vp;
        struct thread *td;
        struct uio *uio;
-       dev_t dev;
+       cdev_t dev;
        int error;
 
        vp = ap->a_vp;
@@ -320,9 +321,9 @@ spec_read(struct vop_read_args *ap)
        if (uio->uio_resid == 0)
                return (0);
 
-       VOP_UNLOCK(vp, 0, td);
+       vn_unlock(vp);
        error = dev_dread(dev, uio, ap->a_ioflag);
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
+       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
        return (error);
 }
 
@@ -339,7 +340,7 @@ spec_write(struct vop_write_args *ap)
        struct vnode *vp;
        struct thread *td;
        struct uio *uio;
-       dev_t dev;
+       cdev_t dev;
        int error;
 
        vp = ap->a_vp;
@@ -350,9 +351,9 @@ spec_write(struct vop_write_args *ap)
        if (dev == NULL)                /* device was revoked */
                return (EBADF);
 
-       VOP_UNLOCK(vp, 0, td);
+       vn_unlock(vp);
        error = dev_dwrite(dev, uio, ap->a_ioflag);
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
+       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
        return (error);
 }
 
@@ -360,34 +361,33 @@ spec_write(struct vop_write_args *ap)
  * Device ioctl operation.
  *
  * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
- *           int a_fflag, struct ucred *a_cred, struct thread *a_td)
+ *           int a_fflag, struct ucred *a_cred)
  */
 /* ARGSUSED */
 static int
 spec_ioctl(struct vop_ioctl_args *ap)
 {
-       dev_t dev;
+       cdev_t dev;
 
        if ((dev = ap->a_vp->v_rdev) == NULL)
                return (EBADF);         /* device was revoked */
 
        return (dev_dioctl(dev, ap->a_command, ap->a_data,
-                   ap->a_fflag, ap->a_td));
+                   ap->a_fflag, ap->a_cred));
 }
 
 /*
- * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
- *          struct thread *a_td)
+ * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred)
  */
 /* ARGSUSED */
 static int
 spec_poll(struct vop_poll_args *ap)
 {
-       dev_t dev;
+       cdev_t dev;
 
        if ((dev = ap->a_vp->v_rdev) == NULL)
                return (EBADF);         /* device was revoked */
-       return (dev_dpoll(dev, ap->a_events, ap->a_td));
+       return (dev_dpoll(dev, ap->a_events));
 }
 
 /*
@@ -397,7 +397,7 @@ spec_poll(struct vop_poll_args *ap)
 static int
 spec_kqfilter(struct vop_kqfilter_args *ap)
 {
-       dev_t dev;
+       cdev_t dev;
 
        if ((dev = ap->a_vp->v_rdev) == NULL)
                return (EBADF);         /* device was revoked */
@@ -423,7 +423,7 @@ spec_fsync(struct vop_fsync_args *ap)
        /*
         * Flush all dirty buffers associated with a block device.
         */
-       error = vfsync(vp, ap->a_waitfor, 10000, NOOFFSET, NULL, NULL);
+       error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
        return (error);
 }
 
@@ -449,9 +449,10 @@ spec_strategy(struct vop_strategy_args *ap)
        struct vnode *vp;
        struct mount *mp;
 
-       if (((bp->b_flags & B_READ) == 0) &&
-               (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
+       if (bp->b_cmd != BUF_CMD_READ &&
+           (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start) {
                (*bioops.io_start)(bp);
+       }
 
        /*
         * Collect statistics on synchronous and asynchronous read
@@ -460,16 +461,16 @@ spec_strategy(struct vop_strategy_args *ap)
        vp = ap->a_vp;
        KKASSERT(vp->v_rdev != NULL);   /* XXX */
        if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
-               if ((bp->b_flags & B_READ) == 0) {
-                       if (bp->b_lock.lk_lockholder == LK_KERNTHREAD)
-                               mp->mnt_stat.f_asyncwrites++;
-                       else
-                               mp->mnt_stat.f_syncwrites++;
-               } else {
-                       if (bp->b_lock.lk_lockholder == LK_KERNTHREAD)
+               if (bp->b_cmd == BUF_CMD_READ) {
+                       if (bp->b_flags & B_ASYNC)
                                mp->mnt_stat.f_asyncreads++;
                        else
                                mp->mnt_stat.f_syncreads++;
+               } else {
+                       if (bp->b_flags & B_ASYNC)
+                               mp->mnt_stat.f_asyncwrites++;
+                       else
+                               mp->mnt_stat.f_syncwrites++;
                }
        }
        dev_dstrategy_chain(vp->v_rdev, bio);
@@ -492,7 +493,7 @@ spec_freeblks(struct vop_freeblks_args *ap)
        if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
                return (0);
        bp = geteblk(ap->a_length);
-       bp->b_flags |= B_FREEBUF;
+       bp->b_cmd = BUF_CMD_FREEBLKS;
        bp->b_bio1.bio_offset = ap->a_offset;
        bp->b_bcount = ap->a_length;
        dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
@@ -539,9 +540,9 @@ spec_bmap(struct vop_bmap_args *ap)
 static int
 spec_close(struct vop_close_args *ap)
 {
-       struct proc *p = ap->a_td->td_proc;
+       struct proc *p = curproc;
        struct vnode *vp = ap->a_vp;
-       dev_t dev = vp->v_rdev;
+       cdev_t dev = vp->v_rdev;
        int error;
        int needrelock;
 
@@ -578,13 +579,13 @@ spec_close(struct vop_close_args *ap)
            (dev_dflags(dev) & D_TRACKCLOSE) ||
            (vcount(vp) <= 1 && vp->v_opencount == 1))) {
                needrelock = 0;
-               if (VOP_ISLOCKED(vp, ap->a_td)) {
+               if (vn_islocked(vp)) {
                        needrelock = 1;
-                       VOP_UNLOCK(vp, 0, ap->a_td);
+                       vn_unlock(vp);
                }
-               error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_td);
+               error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
                if (needrelock)
-                       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
+                       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
        } else {
                error = 0;
        }
@@ -596,15 +597,16 @@ spec_close(struct vop_close_args *ap)
         * tracking occurs.
         */
        if (dev) {
-               KKASSERT(vp->v_opencount > 0);
+               /*KKASSERT(vp->v_opencount > 0);*/
                if (dev_ref_debug) {
                        printf("spec_close: %s %d\n",
                                dev->si_name, vp->v_opencount - 1);
                }
-               if (--vp->v_opencount == 0)
+               if (vp->v_opencount == 1)
                        v_release_rdev(vp);
                release_dev(dev);
        }
+       vop_stdclose(ap);
        return(error);
 }
 
@@ -630,13 +632,13 @@ spec_print(struct vop_print_args *ap)
 static int
 spec_advlock(struct vop_advlock_args *ap)
 {
-       return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
+       return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
 }
 
 static void
 spec_getpages_iodone(struct bio *bio)
 {
-       bio->bio_buf->b_flags |= B_DONE;
+       bio->bio_buf->b_cmd = BUF_CMD_DONE;
        wakeup(bio->bio_buf);
 }
 
@@ -688,14 +690,10 @@ spec_getpages(struct vop_getpages_args *ap)
        pmap_qenter(kva, ap->a_m, pcount);
 
        /* Build a minimal buffer header. */
-       bp->b_flags = B_READ;
-
-       /* B_PHYS is not set, but it is nice to fill this in. */
-       pbgetvp(ap->a_vp, bp);
+       bp->b_cmd = BUF_CMD_READ;
        bp->b_bcount = size;
-       bp->b_bufsize = size;
        bp->b_resid = 0;
-       bp->b_runningbufspace = bp->b_bufsize;
+       bp->b_runningbufspace = size;
        runningbufspace += bp->b_runningbufspace;
 
        bp->b_bio1.bio_offset = offset;
@@ -710,28 +708,30 @@ spec_getpages(struct vop_getpages_args *ap)
        crit_enter();
 
        /* We definitely need to be at splbio here. */
-       while ((bp->b_flags & B_DONE) == 0) {
+       while (bp->b_cmd != BUF_CMD_DONE)
                tsleep(bp, 0, "spread", 0);
-       }
 
        crit_exit();
 
-       if ((bp->b_flags & B_ERROR) != 0) {
+       if (bp->b_flags & B_ERROR) {
                if (bp->b_error)
                        error = bp->b_error;
                else
                        error = EIO;
        }
 
-       nread = size - bp->b_resid;
-
-       if (nread < ap->a_count) {
-               bzero((caddr_t)kva + nread,
-                       ap->a_count - nread);
-       }
+       /*
+        * If EOF is encountered we must zero-extend the result in order
+        * to ensure that the page does not contain garabge.  When no
+        * error occurs, an early EOF is indicated if b_bcount got truncated.
+        * b_resid is relative to b_bcount and should be 0, but some devices
+        * might indicate an EOF with b_resid instead of truncating b_bcount.
+        */
+       nread = bp->b_bcount - bp->b_resid;
+       if (nread < ap->a_count)
+               bzero((caddr_t)kva + nread, ap->a_count - nread);
        pmap_qremove(kva, pcount);
 
-
        gotreqpage = 0;
        for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
                nextoff = toff + PAGE_SIZE;