Fix build for FreeBSD 10.0
[openafs.git] / src / afs / FBSD / osi_file.c
index 4ce371f..096e472 100644 (file)
@@ -10,8 +10,6 @@
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include "afs/sysincludes.h"   /* Standard vendor system headers */
 #include "afsincludes.h"       /* Afs-based standard headers */
@@ -19,79 +17,76 @@ RCSID
 
 
 int afs_osicred_initialized = 0;
-struct AFS_UCRED afs_osi_cred;
+#ifndef AFS_FBSD80_ENV /* cr_groups is now malloc()'d */
+afs_ucred_t afs_osi_cred;
+#endif
 afs_lock_t afs_xosi;           /* lock is for tvattr */
 extern struct osi_dev cacheDev;
 extern struct mount *afs_cacheVfsp;
 
 
 void *
-osi_UFSOpen(afs_int32 ainode)
+osi_UFSOpen(afs_dcache_id_t *ainode)
 {
-    struct inode *ip;
-    register struct osi_file *afile = NULL;
+    struct osi_file *afile;
+    struct vnode *vp;
     extern int cacheDiskType;
-    afs_int32 code = 0;
-    int dummy;
+    afs_int32 code;
+
     AFS_STATCNT(osi_UFSOpen);
-    if (cacheDiskType != AFS_FCACHE_TYPE_UFS) {
+    if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
        osi_Panic("UFSOpen called for non-UFS cache\n");
-    }
-    if (!afs_osicred_initialized) {
-       /* valid for alpha_osf, SunOS, Ultrix */
-       memset((char *)&afs_osi_cred, 0, sizeof(struct AFS_UCRED));
-       afs_osi_cred.cr_ref++;
-       afs_osicred_initialized = 1;
-    }
-    afile = (struct osi_file *)osi_AllocSmallSpace(sizeof(struct osi_file));
+    afile = osi_AllocSmallSpace(sizeof(struct osi_file));
     AFS_GUNLOCK();
-    code =
-       igetinode(afs_cacheVfsp, (dev_t) cacheDev.dev, (ino_t) ainode, &ip,
-                 &dummy);
+    code = VFS_VGET(afs_cacheVfsp, (ino_t) ainode->ufs, LK_EXCLUSIVE, &vp);
     AFS_GLOCK();
+    if (code == 0 && vp->v_type == VNON)
+       code = ENOENT;
     if (code) {
        osi_FreeSmallSpace(afile);
        osi_Panic("UFSOpen: igetinode failed");
     }
-    afile->vnode = ITOV(ip);
-#if defined(AFS_FBSD50_ENV)
-    VOP_UNLOCK(afile->vnode, 0, curthread);
+#if defined(AFS_FBSD80_ENV)
+    VOP_UNLOCK(vp, 0);
 #else
-    VOP_UNLOCK(afile->vnode, 0, curproc);
+    VOP_UNLOCK(vp, 0, curthread);
 #endif
-    afile->size = VTOI(afile->vnode)->i_size;
+    afile->vnode = vp;
+    afile->size = VTOI(vp)->i_size;
     afile->offset = 0;
-    afile->proc = (int (*)())0;
-    afile->inum = ainode;      /* for hint validity checking */
+    afile->proc = NULL;
     return (void *)afile;
 }
 
 int
-afs_osi_Stat(register struct osi_file *afile, register struct osi_stat *astat)
+afs_osi_Stat(struct osi_file *afile, struct osi_stat *astat)
 {
-    register afs_int32 code;
+    afs_int32 code;
     struct vattr tvattr;
     AFS_STATCNT(osi_Stat);
-    MObtainWriteLock(&afs_xosi, 320);
+    ObtainWriteLock(&afs_xosi, 320);
     AFS_GUNLOCK();
-#if defined(AFS_FBSD50_ENV)
-    code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, curthread);
+#if defined(AFS_FBSD80_ENV)
+    vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY);
+    code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
+    VOP_UNLOCK(afile->vnode, 0);
 #else
-    code = VOP_GETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
+    vn_lock(afile->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
+    code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
+    VOP_UNLOCK(afile->vnode, LK_EXCLUSIVE, curthread);
 #endif
     AFS_GLOCK();
     if (code == 0) {
        astat->size = tvattr.va_size;
-       astat->blksize = tvattr.va_blocksize;
        astat->mtime = tvattr.va_mtime.tv_sec;
        astat->atime = tvattr.va_atime.tv_sec;
     }
-    MReleaseWriteLock(&afs_xosi);
+    ReleaseWriteLock(&afs_xosi);
     return code;
 }
 
 int
-osi_UFSClose(register struct osi_file *afile)
+osi_UFSClose(struct osi_file *afile)
 {
     AFS_STATCNT(osi_Close);
     if (afile->vnode) {
@@ -103,31 +98,50 @@ osi_UFSClose(register struct osi_file *afile)
 }
 
 int
-osi_UFSTruncate(register struct osi_file *afile, afs_int32 asize)
+osi_UFSTruncate(struct osi_file *afile, afs_int32 asize)
 {
     struct vattr tvattr;
-    register afs_int32 code;
-    struct osi_stat tstat;
+    struct vnode *vp;
+    afs_int32 code, glocked;
     AFS_STATCNT(osi_Truncate);
 
-    /* This routine only shrinks files, and most systems
+    ObtainWriteLock(&afs_xosi, 321);
+    vp = afile->vnode;
+    /*
+     * This routine only shrinks files, and most systems
      * have very slow truncates, even when the file is already
      * small enough.  Check now and save some time.
      */
-    code = afs_osi_Stat(afile, &tstat);
-    if (code || tstat.size <= asize)
-       return code;
-    MObtainWriteLock(&afs_xosi, 321);
+    glocked = ISAFS_GLOCK();
+    if (glocked)
+      AFS_GUNLOCK();
+#if defined(AFS_FBSD80_ENV)
+    vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+    code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp);
+#else
+    vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
+    code = VOP_GETATTR(afile->vnode, &tvattr, afs_osi_credp, curthread);
+#endif
+    if (code != 0 || tvattr.va_size <= asize)
+       goto out;
+
     VATTR_NULL(&tvattr);
     tvattr.va_size = asize;
-    AFS_GUNLOCK();
-#if defined(AFS_FBSD50_ENV)
-    code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, curthread);
+#if defined(AFS_FBSD80_ENV)
+    code = VOP_SETATTR(vp, &tvattr, afs_osi_credp);
 #else
-    code = VOP_SETATTR(afile->vnode, &tvattr, &afs_osi_cred, curproc);
+    code = VOP_SETATTR(vp, &tvattr, afs_osi_credp, curthread);
 #endif
-    AFS_GLOCK();
-    MReleaseWriteLock(&afs_xosi);
+
+out:
+#if defined(AFS_FBSD80_ENV)
+    VOP_UNLOCK(vp, 0);
+#else
+    VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
+#endif
+    if (glocked)
+      AFS_GLOCK();
+    ReleaseWriteLock(&afs_xosi);
     return code;
 }
 
@@ -141,11 +155,15 @@ osi_DisableAtimes(struct vnode *avp)
 
 /* Generic read interface */
 int
-afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
+afs_osi_Read(struct osi_file *afile, int offset, void *aptr,
             afs_int32 asize)
 {
-    unsigned int resid;
-    register afs_int32 code;
+#if (__FreeBSD_version >= 900505 && __FreeBSD_Version < 1000000) ||__FreeBSD_version >= 1000009
+    ssize_t resid;
+#else
+    int resid;
+#endif
+    afs_int32 code;
     AFS_STATCNT(osi_Read);
 
     /**
@@ -156,7 +174,7 @@ afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
        if (!afs_shuttingdown)
            osi_Panic("osi_Read called with null param");
        else
-           return EIO;
+           return -EIO;
     }
 
     if (offset != -1)
@@ -164,27 +182,33 @@ afs_osi_Read(register struct osi_file *afile, int offset, void *aptr,
     AFS_GUNLOCK();
     code =
        gop_rdwr(UIO_READ, afile->vnode, (caddr_t) aptr, asize, afile->offset,
-                AFS_UIOSYS, IO_UNIT, &afs_osi_cred, &resid);
+                AFS_UIOSYS, IO_UNIT, afs_osi_credp, &resid);
     AFS_GLOCK();
     if (code == 0) {
        code = asize - resid;
        afile->offset += code;
        osi_DisableAtimes(afile->vnode);
     } else {
-       afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, resid,
+       afs_Trace2(afs_iclSetp, CM_TRACE_READFAILED, ICL_TYPE_INT32, (int)resid,
                   ICL_TYPE_INT32, code);
-       code = -1;
+       if (code > 0) {
+           code = -code;
+       }
     }
     return code;
 }
 
 /* Generic write interface */
 int
-afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
+afs_osi_Write(struct osi_file *afile, afs_int32 offset, void *aptr,
              afs_int32 asize)
 {
-    unsigned int resid;
-    register afs_int32 code;
+#if (__FreeBSD_version >= 900505 && __FreeBSD_Version < 1000000) ||__FreeBSD_version >= 1000009
+    ssize_t resid;
+#else
+    int resid;
+#endif
+    afs_int32 code;
     AFS_STATCNT(osi_Write);
     if (!afile)
        osi_Panic("afs_osi_Write called with null param");
@@ -194,7 +218,7 @@ afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
        AFS_GUNLOCK();
        code =
            gop_rdwr(UIO_WRITE, afile->vnode, (caddr_t) aptr, asize,
-                    afile->offset, AFS_UIOSYS, IO_UNIT, &afs_osi_cred,
+                    afile->offset, AFS_UIOSYS, IO_UNIT, afs_osi_credp,
                     &resid);
        AFS_GLOCK();
     }
@@ -202,7 +226,9 @@ afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
        code = asize - resid;
        afile->offset += code;
     } else {
-       code = -1;
+       if (code > 0) {
+           code = -code;
+       }
     }
     if (afile->proc) {
        (*afile->proc) (afile, code);
@@ -215,7 +241,7 @@ afs_osi_Write(register struct osi_file *afile, afs_int32 offset, void *aptr,
     This routine written from the RT NFS port strategy routine.
     It has been generalized a bit, but should still be pretty clear. */
 int
-afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
+afs_osi_MapStrategy(int (*aproc) (), struct buf *bp)
 {
     afs_int32 returnCode;
 
@@ -230,8 +256,6 @@ afs_osi_MapStrategy(int (*aproc) (), register struct buf *bp)
 void
 shutdown_osifile(void)
 {
-    extern int afs_cold_shutdown;
-
     AFS_STATCNT(shutdown_osifile);
     if (afs_cold_shutdown) {
        afs_osicred_initialized = 0;