linux-changes-needed-to-compile-with-suse-included-patches-20010623
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
index 2bc8287..c6497e5 100644 (file)
 #include "../h/smp_lock.h"
 #endif
 
+#ifdef pgoff2loff
+#define pageoff(pp) pgoff2loff((pp)->index)
+#else
+#define pageoff(pp) pp->offset
+#endif
+
 extern struct vcache *afs_globalVp;
 
 extern struct dentry_operations *afs_dops;
@@ -224,8 +230,40 @@ tagain:
        len = strlen(de->name);
 
        /* filldir returns -EINVAL when the buffer is full. */
-#ifdef AFS_LINUX24_ENV
-       code = (*filldir)(dirbuf, de->name, len, offset, ino, DT_DIR);
+#if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
+        {
+             unsigned int type=DT_UNKNOWN;
+             struct VenusFid afid;
+             struct vcache *tvc;
+             int vtype;
+             afid.Cell=avc->fid.Cell;
+             afid.Fid.Volume=avc->fid.Fid.Volume;
+             afid.Fid.Vnode=ntohl(de->fid.vnode);
+             afid.Fid.Unique=ntohl(de->fid.vunique);
+             if ((avc->states & CForeign) == 0 &&
+                 (ntohl(de->fid.vnode) & 1)) {
+                  type=DT_DIR;
+             } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
+                  if (tvc->mvstat) {
+                       type=DT_DIR;
+                  } else if (((tvc->states) & (CStatd|CTruth))) {
+                       /* CTruth will be set if the object has 
+                        *ever* been statd */
+                       vtype=vType(tvc);
+                       if (vtype == VDIR)
+                            type=DT_DIR;
+                       else if (vtype == VREG)
+                            type=DT_REG;
+                       /* Don't do this until we're sure it can't be a mtpt */
+                       /* else if (vtype == VLNK)
+                          type=DT_LNK; */
+                       /* what other types does AFS support? */
+                  }
+                 /* clean up from afs_FindVCache */
+                 afs_PutVCache(tvc, WRITE_LOCK);
+             }
+             code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
+        }
 #else
        code = (*filldir)(dirbuf, de->name, len, offset, ino); 
 #endif
@@ -470,7 +508,11 @@ static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
     int code = 0;
     struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
     cred_t *credp = crref();
+#ifdef AFS_LINUX24_ENV
+    struct flock64 flock;
+#else
     struct flock flock;
+#endif
     
     /* Convert to a lock format afs_lockctl understands. */
     memset((char*)&flock, 0, sizeof(flock));
@@ -613,15 +655,6 @@ static int afs_linux_revalidate(struct dentry *dp)
     lock_kernel();
 #endif
 
-    /* If it's a negative dentry, then there's nothing to do. */
-    if (!vcp) {
-#ifdef AFS_LINUX24_ENV
-        unlock_kernel();
-#endif
-       AFS_GUNLOCK();
-       return 0;
-    }
-
     /* Make this a fast path (no crref), since it's called so often. */
     if (vcp->states & CStatd) {
         if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
@@ -648,13 +681,6 @@ static int afs_linux_revalidate(struct dentry *dp)
     return -code ;
 }
 
-/* Validate a dentry. Return 0 if unchanged, 1 if VFS layer should re-evaluate.
- * In kernels 2.2.10 and above, we are passed an additional flags var which
- * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
- * we are advised to follow the entry if it is a link or to make sure that 
- * it is a directory. But since the kernel itself checks these possibilities
- * later on, we shouldn't have to do it until later. Perhaps in the future..
- */
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
 #else
@@ -664,72 +690,31 @@ static int afs_linux_dentry_revalidate(struct dentry *dp)
     int code;
     cred_t *credp;
     struct vrequest treq;
-    struct vcache *vcp = (struct vcache*)dp->d_inode;
-
-    AFS_GLOCK();
-#ifdef AFS_LINUX24_ENV
-    lock_kernel();
-#endif
-
-    /* If it's a negative dentry, then there's nothing to do. */
-    if (!vcp) {
-#ifdef AFS_LINUX24_ENV
-       unlock_kernel();
-#endif
-       AFS_GUNLOCK();
-       return 0;
-    }
-
-    /* Make this a fast path (no crref), since it's called so often. */
-    if (vcp->states & CStatd) {
-        if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
-           check_bad_parent(dp); /* check and correct mvid */
-       vcache2inode(vcp);
-#ifdef AFS_LINUX24_ENV
-       unlock_kernel();
-#endif
-       AFS_GUNLOCK();
-       return 0;
+    struct inode *ip = (struct inode *)dp->d_inode;
+    
+    unsigned long timeout = 3*HZ; /* 3 seconds */
+    
+    if (!(flags & LOOKUP_CONTINUE)) {
+       long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
+       
+       if (diff < 15*60)
+           timeout = 0;
     }
-
-    credp = crref();
-    code = afs_InitReq(&treq, credp);
-    if (!code)
-       code = afs_VerifyVCache(vcp, &treq);
-
-#ifdef AFS_LINUX24_ENV
-    unlock_kernel();
-#endif
-    AFS_GUNLOCK();
-    crfree(credp);
-
+    
+    if (time_after(jiffies, dp->d_time + timeout))
+       goto out_bad;
+    
+ out_valid:
     return 1;
+    
+ out_bad:
+    return 0;
 }
 
 /* afs_dentry_iput */
 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
 {
-#if defined(AFS_LINUX24_ENV)
-    if (atomic_read(&ip->i_count) == 0 || atomic_read(&ip->i_count) & 0xffff0000) {
-#else
-    if (ip->i_count == 0 || ip->i_count & 0xffff0000) {
-#endif
-       osi_Panic("Bad refCount %d on inode 0x%x\n",
-#if defined(AFS_LINUX24_ENV)
-                 atomic_read(&ip->i_count), ip);
-#else
-                 ip->i_count, ip);
-#endif
-    }
-#if defined(AFS_LINUX24_ENV)
-    atomic_dec(&ip->i_count);
-    if (!atomic_read(&ip->i_count)) {
-#else
-    ip->i_count --;
-    if (!ip->i_count) {
-#endif
-       afs_delete_inode(ip);
-    }
+    osi_iput(ip);
 }
 
 #if defined(AFS_LINUX24_ENV)
@@ -803,6 +788,7 @@ int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
 #endif
 
        dp->d_op = afs_dops;
+       dp->d_time = jiffies;
        d_instantiate(dp, ip);
     }
 
@@ -849,6 +835,7 @@ int afs_linux_lookup(struct inode *dip, struct dentry *dp)
            ip->i_op = &afs_symlink_iops;
 #endif
     }
+    dp->d_time = jiffies;
     dp->d_op = afs_dops;
     d_add(dp, (struct inode*)vcp);
 
@@ -914,9 +901,11 @@ int afs_linux_unlink(struct inode *dip, struct dentry *dp)
     AFS_GUNLOCK();
     if (!code) {
        d_delete(dp);
-       if (putback)
+       if (putback) {
+           dp->d_time = jiffies;
            d_add(dp, NULL); /* means definitely does _not_ exist */
     }
+    }
     crfree(credp);
     return -code;
 }
@@ -963,6 +952,7 @@ int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
        tvcp->v.v_fop = &afs_dir_fops;
 #endif
        dp->d_op = afs_dops;
+       dp->d_time = jiffies;
        d_instantiate(dp, (struct inode*)tvcp);
     }
     AFS_GUNLOCK();
@@ -979,12 +969,12 @@ int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
     AFS_GLOCK();
     code = afs_rmdir((struct vcache*)dip, name, credp);
 
-    /* Linux likes to see ENOTDIR returned from an rmdir() syscall
+    /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
      * that failed because a directory is not empty. So, we map
-     * EEXIST to ENOTDIR on linux.
+     * EEXIST to ENOTEMPTY on linux.
      */
     if (code == EEXIST) {
-       code = ENOTDIR;
+       code = ENOTEMPTY;
     }
     
     if (!code) {
@@ -1022,8 +1012,11 @@ int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
                      newname, credp);
     AFS_GUNLOCK();
 
-    if (!code)
+    if (!code) {
+        /* update time so it doesn't expire immediately */
+       newdp->d_time = jiffies;
        d_move(olddp, newdp);
+    }
 
     crfree(credp);
     return -code;
@@ -1113,7 +1106,13 @@ int afs_linux_readpage(struct file *fp, struct page *pp)
 {
     int code;
     cred_t *credp = crref();
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
+    char *address;
+    loff_t offset = pp->index << PAGE_CACHE_SHIFT;
+#else
     ulong address = afs_linux_page_address(pp);
+    loff_t offset = pageoff(pp);
+#endif
     uio_t tuio;
     struct iovec iovec;
     struct inode *ip = FILE_INODE(fp);
@@ -1125,22 +1124,22 @@ int afs_linux_readpage(struct file *fp, struct page *pp)
               ICL_TYPE_POINTER, pp,
               ICL_TYPE_INT32, cnt,
               ICL_TYPE_INT32, 99999); /* not a possible code value */
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
+    address = kmap(pp);
+    ClearPageError(pp);
+
+    lock_kernel();
+#else
     atomic_add(1, &pp->count);
     set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
     clear_bit(PG_error, &pp->flags);
+#endif
 
-#if defined(AFS_LINUX24_ENV)
-    setup_uio(&tuio, &iovec, (char*)address, pp->index << PAGE_CACHE_SHIFT,
-             PAGESIZE, UIO_READ, AFS_UIOSYS);
-#else
-    setup_uio(&tuio, &iovec, (char*)address, pp->offset, PAGESIZE,
+    setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
              UIO_READ, AFS_UIOSYS);
-#endif
-#ifdef AFS_LINUX24_ENV
-    lock_kernel();
-#endif
     code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
-#ifdef AFS_LINUX24_ENV
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
     unlock_kernel();
 #endif
 
@@ -1148,12 +1147,22 @@ int afs_linux_readpage(struct file *fp, struct page *pp)
        if (tuio.uio_resid) /* zero remainder of page */
            memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
                   tuio.uio_resid);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
+       flush_dcache_page(pp);
+       SetPageUptodate(pp);
+#else
        set_bit(PG_uptodate, &pp->flags);
+#endif
     }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
+    kunmap(pp);
+    UnlockPage(pp);
+#else
     clear_bit(PG_locked, &pp->flags);
     wake_up(&pp->wait);
     free_page(address);
+#endif
 
     crfree(credp);
     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
@@ -1166,25 +1175,31 @@ int afs_linux_readpage(struct file *fp, struct page *pp)
 }
 
 #if defined(AFS_LINUX24_ENV)
-int afs_linux_writepage(struct file *file, struct page *page)
+int afs_linux_writepage(struct page *pp)
 {
-    struct dentry *dentry = file->f_dentry;
-    struct inode *inode = dentry->d_inode;
-    unsigned long end_index = inode->i_size >> PAGE_CACHE_SHIFT;
+    struct address_space *mapping = pp->mapping;
+    struct inode *inode;
+    unsigned long end_index;
     unsigned offset = PAGE_CACHE_SIZE;
     long status;
  
+    inode = (struct inode *) mapping->host;
+    end_index = inode->i_size >> PAGE_CACHE_SHIFT;
+
     /* easy case */
-    if (page->index < end_index)
+    if (pp->index < end_index)
        goto do_it;
     /* things got complicated... */
     offset = inode->i_size & (PAGE_CACHE_SIZE-1);
     /* OK, are we completely out? */
-    if (page->index >= end_index+1 || !offset)
+    if (pp->index >= end_index+1 || !offset)
        return -EIO;
 do_it:
-    status = afs_linux_updatepage(file, page, 0, offset, 1);
-    kunmap(page);
+    AFS_GLOCK();
+    status = afs_linux_writepage_sync(inode, pp, 0, offset);
+    AFS_GUNLOCK();
+    SetPageUptodate(pp);
+    UnlockPage(pp);
     if (status == offset)
        return 0;
     else
@@ -1231,6 +1246,55 @@ int afs_linux_permission(struct inode *ip, int mode)
 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
 #endif
 
+#if defined(AFS_LINUX24_ENV)
+int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
+                        unsigned long offset,
+                        unsigned int count)
+{
+    struct vcache *vcp = (struct vcache *) ip;
+    char *buffer;
+    loff_t base;
+    int code = 0;
+    cred_t *credp;
+    uio_t tuio;
+    struct iovec iovec;
+    int f_flags = 0;
+
+    buffer = kmap(pp) + offset;
+    base = (pp->index << PAGE_CACHE_SHIFT) + offset;
+
+    credp = crref();
+    afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
+              ICL_TYPE_POINTER, pp,
+              ICL_TYPE_INT32, atomic_read(&pp->count),
+              ICL_TYPE_INT32, 99999);
+    setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
+
+    code = afs_write(vcp, &tuio, f_flags, credp, 0);
+
+    vcache2inode(vcp);
+
+    code = code ? -code : count - tuio.uio_resid;
+    afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
+              ICL_TYPE_POINTER, pp,
+              ICL_TYPE_INT32, atomic_read(&pp->count),
+              ICL_TYPE_INT32, code);
+
+    crfree(credp);
+    kunmap(pp);
+
+    return code;
+}
+
+static int
+afs_linux_updatepage(struct file *file, struct page *page, 
+                    unsigned long offset, unsigned int count)
+{
+    struct dentry *dentry = file->f_dentry;
+
+    return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
+}
+#else
 /* afs_linux_updatepage
  * What one would have thought was writepage - write dirty page to file.
  * Called from generic_file_write. buffer is still in user space. pagep
@@ -1251,21 +1315,12 @@ int afs_linux_updatepage(struct file *fp, struct page *pp,
 
     credp = crref();
     AFS_GLOCK();
-#ifdef AFS_LINUX24_ENV
-    lock_kernel();
-#endif
     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
               ICL_TYPE_POINTER, pp,
               ICL_TYPE_INT32, atomic_read(&pp->count),
               ICL_TYPE_INT32, 99999);
-#if defined(AFS_LINUX24_ENV)
-    setup_uio(&tuio, &iovec, page_addr + offset,
-             (pp->index << PAGE_CACHE_SHIFT) + offset, count,
+    setup_uio(&tuio, &iovec, page_addr + offset, pageoff(pp) + offset, count,
              UIO_WRITE, AFS_UIOSYS);
-#else
-    setup_uio(&tuio, &iovec, page_addr + offset, pp->offset + offset, count,
-             UIO_WRITE, AFS_UIOSYS);
-#endif
 
     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
 
@@ -1277,26 +1332,27 @@ int afs_linux_updatepage(struct file *fp, struct page *pp,
               ICL_TYPE_INT32, atomic_read(&pp->count),
               ICL_TYPE_INT32, code);
 
-#ifdef AFS_LINUX24_ENV
-    unlock_kernel();
-#endif
     AFS_GUNLOCK();
     crfree(credp);
 
     clear_bit(PG_locked, &pp->flags);
     return code;
 }
+#endif
 
 #if defined(AFS_LINUX24_ENV)
 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
 {
-    long status;
-    loff_t pos = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + to;
+    int code;
 
-    status = afs_linux_updatepage(file, page, offset, to-offset, 1);
+    AFS_GLOCK();
+    lock_kernel();
+    code = afs_linux_updatepage(file, page, offset, to-offset);
+    unlock_kernel();
+    AFS_GUNLOCK();
     kunmap(page);
 
-    return status;
+    return code;
 }
 
 static int afs_linux_prepare_write(struct file *file, struct page *page,
@@ -1405,18 +1461,22 @@ static int afs_symlink_filler(struct file *file, struct page *page)
     AFS_GLOCK();
     lock_kernel();
     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
-    unlock_kernel();
-    AFS_GUNLOCK();
 
     if (code<0)
            goto fail;
     p[code] = '\0';            /* null terminate? */
+    unlock_kernel();
+    AFS_GUNLOCK();
+
     SetPageUptodate(page);
     kunmap(page);
     UnlockPage(page);
     return 0;
 
 fail:
+    unlock_kernel();
+    AFS_GUNLOCK();
+
     SetPageError(page);
     kunmap(page);
     UnlockPage(page);