Linux 3.11: Convert from readdir to iterate file operation
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
index 7d5e166..25cbac2 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/pagemap.h>
 #include <linux/writeback.h>
 #include <linux/pagevec.h>
+#include <linux/aio.h>
 #include "afs/lock.h"
 #include "afs/afs_bypasscache.h"
 
@@ -46,6 +47,8 @@
 #define MAX_ERRNO 1000L
 #endif
 
+int cachefs_noreadpage = 0;
+
 extern struct backing_dev_info *afs_backing_dev_info;
 
 extern struct vcache *afs_globalVp;
@@ -97,8 +100,15 @@ afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
 }
 
 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
+# ifdef LINUX_HAS_NONVECTOR_AIO
+static ssize_t
+afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
+                   loff_t pos)
+# else
 static ssize_t
-afs_linux_aio_read(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
+afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
+                   unsigned long bufsize, loff_t pos)
+# endif
 {
     struct file *fp = iocb->ki_filp;
     ssize_t code = 0;
@@ -106,8 +116,8 @@ afs_linux_aio_read(struct kiocb *iocb, const struct iovec *iov, unsigned long se
 
     AFS_GLOCK();
     afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
-              99999);
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
+               (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
     code = afs_linux_VerifyVCache(vcp, NULL);
 
     if (code == 0) {
@@ -115,13 +125,13 @@ afs_linux_aio_read(struct kiocb *iocb, const struct iovec *iov, unsigned long se
         * so we optimise by not using it */
        osi_FlushPages(vcp, NULL);      /* ensure stale pages are gone */
        AFS_GUNLOCK();
-       code = generic_file_aio_read(iocb, iov, segs, pos);
+       code = generic_file_aio_read(iocb, buf, bufsize, pos);
        AFS_GLOCK();
     }
 
     afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
-              code);
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
+               (afs_int32)bufsize, ICL_TYPE_INT32, code);
     AFS_GUNLOCK();
     return code;
 }
@@ -161,8 +171,15 @@ afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
  * mode. Call fake open/close to ensure we do writes of core dumps.
  */
 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
+# ifdef LINUX_HAS_NONVECTOR_AIO
+static ssize_t
+afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
+                    loff_t pos)
+# else
 static ssize_t
-afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
+afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
+                    unsigned long bufsize, loff_t pos)
+# endif
 {
     ssize_t code = 0;
     struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
@@ -171,7 +188,8 @@ afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long s
     AFS_GLOCK();
 
     afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
+               (afs_int32)bufsize, ICL_TYPE_INT32,
               (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
 
     code = afs_linux_VerifyVCache(vcp, &credp);
@@ -181,7 +199,7 @@ afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long s
     ReleaseWriteLock(&vcp->lock);
     if (code == 0) {
            AFS_GUNLOCK();
-           code = generic_file_aio_write(iocb, iov, segs, pos);
+           code = generic_file_aio_write(iocb, buf, bufsize, pos);
            AFS_GLOCK();
     }
 
@@ -194,8 +212,8 @@ afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long s
     ReleaseWriteLock(&vcp->lock);
 
     afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
-              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
-              code);
+              ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
+               (afs_int32)bufsize, ICL_TYPE_INT32, code);
 
     if (credp)
       crfree(credp);
@@ -253,7 +271,11 @@ extern int BlobScan(struct dcache * afile, afs_int32 ablob);
  * handling and use of bulkstats will need to be reflected here as well.
  */
 static int
+#if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
+afs_linux_readdir(struct file *fp, struct dir_context *ctx)
+#else
 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
+#endif
 {
     struct vcache *avc = VTOAFS(FILE_INODE(fp));
     struct vrequest treq;
@@ -332,7 +354,11 @@ afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
      * takes an offset in units of blobs, rather than bytes.
      */
     code = 0;
+#if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
+    offset = ctx->pos;
+#else
     offset = (int) fp->f_pos;
+#endif
     while (1) {
        dirpos = BlobScan(tdc, offset);
        if (!dirpos)
@@ -390,7 +416,13 @@ afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
             * holding the GLOCK.
             */
            AFS_GUNLOCK();
+#if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
+           /* dir_emit returns a bool - true when it succeeds.
+            * Inverse the result to fit with how we check "code" */
+           code = !dir_emit(ctx, de->name, len, ino, type);
+#else
            code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
+#endif
            AFS_GLOCK();
        }
        DRelease(&entry, 0);
@@ -401,7 +433,11 @@ afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
     /* If filldir didn't fill in the last one this is still pointing to that
      * last attempt.
      */
+#if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
+    ctx->pos = (loff_t) offset;
+#else
     fp->f_pos = (loff_t) offset;
+#endif
 
     ReleaseReadLock(&tdc->lock);
     afs_PutDCache(tdc);
@@ -556,6 +592,16 @@ afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
 
     AFS_GLOCK();
+    if ((vcp->f.states & CRO)) {
+       if (flp->fl_type == F_WRLCK) {
+           code = EBADF;
+       } else {
+           code = 0;
+       }
+       AFS_GUNLOCK();
+       crfree(credp);
+       return code;
+    }
     code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
     AFS_GUNLOCK();
 
@@ -720,7 +766,11 @@ out:
 
 struct file_operations afs_dir_fops = {
   .read =      generic_read_dir,
+#if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
+  .iterate =   afs_linux_readdir,
+#else
   .readdir =   afs_linux_readdir,
+#endif
 #ifdef HAVE_UNLOCKED_IOCTL
   .unlocked_ioctl = afs_unlocked_xioctl,
 #else
@@ -779,6 +829,9 @@ canonical_dentry(struct inode *ip)
 {
     struct vcache *vcp = VTOAFS(ip);
     struct dentry *first = NULL, *ret = NULL, *cur;
+#if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
+    struct hlist_node *p;
+#endif
 
     /* general strategy:
      * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
@@ -798,7 +851,15 @@ canonical_dentry(struct inode *ip)
     spin_lock(&ip->i_lock);
 # endif
 
+#if defined(D_ALIAS_IS_HLIST)
+# if defined(HLIST_ITERATOR_NO_NODE)
+    hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
+# else
+    hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
+# endif
+#else
     list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
+#endif
 
        if (!vcp->target_link || cur == vcp->target_link) {
            ret = cur;
@@ -1038,7 +1099,9 @@ afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *sta
  * The code here assumes that on entry the global lock is not held
  */
 static int
-#ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
+#if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
+afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
+#elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
 #else
 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
@@ -1054,7 +1117,11 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
 
 #ifdef LOOKUP_RCU
     /* We don't support RCU path walking */
+# if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
+    if (flags & LOOKUP_RCU)
+# else
     if (nd->flags & LOOKUP_RCU)
+# endif
        return -ECHILD;
 #endif
 
@@ -1092,6 +1159,7 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
                    code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
                if ((tryEvalOnly && vcp->mvstat == 1) || code) {
                    /* a mount point, not yet replaced by its directory */
+                   dput(parent);
                    goto bad_dentry;
                }
            }
@@ -1108,8 +1176,10 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
         * always require a crref() which would be "slow".
         */
        if (vcp->last_looker != treq.uid) {
-           if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
+           if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
+               dput(parent);
                goto bad_dentry;
+           }
 
            vcp->last_looker = treq.uid;
        }
@@ -1225,10 +1295,14 @@ afs_dentry_delete(struct dentry *dp)
 
 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
 static struct vfsmount *
-afs_dentry_automount(struct path *path)
+afs_dentry_automount(afs_linux_path_t *path)
 {
     struct dentry *target;
 
+    /* avoid symlink resolution limits when resolving; we cannot contribute to
+     * an infinite symlink loop */
+    current->total_link_count--;
+
     target = canonical_dentry(path->dentry->d_inode);
 
     if (target == path->dentry) {
@@ -1272,17 +1346,18 @@ struct dentry_operations afs_dentry_operations = {
  * name is in kernel space at this point.
  */
 static int
-#if defined(IOP_MKDIR_TAKES_UMODE_T)
+#if defined(IOP_CREATE_TAKES_BOOL)
+afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
+                bool excl)
+#elif defined(IOP_CREATE_TAKES_UMODE_T)
 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
                 struct nameidata *nd)
-#else
-#ifdef IOP_CREATE_TAKES_NAMEIDATA
+#elif defined(IOP_CREATE_TAKES_NAMEIDATA)
 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
                 struct nameidata *nd)
 #else
 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
 #endif
-#endif
 {
     struct vattr vattr;
     cred_t *credp = crref();
@@ -1318,7 +1393,10 @@ afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
 
 /* afs_linux_lookup */
 static struct dentry *
-#ifdef IOP_LOOKUP_TAKES_NAMEIDATA
+#if defined(IOP_LOOKUP_TAKES_UNSIGNED)
+afs_linux_lookup(struct inode *dip, struct dentry *dp,
+                unsigned flags)
+#elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
 afs_linux_lookup(struct inode *dip, struct dentry *dp,
                 struct nameidata *nd)
 #else
@@ -1363,24 +1441,7 @@ afs_linux_lookup(struct inode *dip, struct dentry *dp)
     AFS_GUNLOCK();
 
     if (ip && S_ISDIR(ip->i_mode)) {
-       int retry = 1;
-       struct dentry *alias;
-
-       while (retry) {
-           retry = 0;
-
-           /* Try to invalidate an existing alias in favor of our new one */
-           alias = d_find_alias(ip);
-           /* But not if it's disconnected; then we want d_splice_alias below */
-           if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
-               if (d_invalidate(alias) == 0) {
-                   /* there may be more aliases; try again until we run out */
-                   retry = 1;
-               }
-           }
-
-           dput(alias);
-       }
+       d_prune_aliases(ip);
 
 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
        ip->i_flags |= S_AUTOMOUNT;
@@ -1836,6 +1897,10 @@ afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
        return 0;
 
+    /* No readpage (ex: tmpfs) , skip */
+    if (cachefs_noreadpage)
+       return 0;
+
     /* Can't do anything if the vcache isn't statd , or if the read
      * crosses a chunk boundary.
      */
@@ -1895,12 +1960,8 @@ afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
 
     /* Is the dcache we've been given currently up to date */
     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
-       (tdc->dflags & DFFetching)) {
-       ReleaseWriteLock(&avc->lock);
-       ReleaseReadLock(&tdc->lock);
-       afs_PutDCache(tdc);
-       return 0;
-    }
+       (tdc->dflags & DFFetching))
+       goto out;
 
     /* Update our hint for future abuse */
     avc->dchint = tdc;
@@ -1910,6 +1971,11 @@ afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
     /* XXX - I suspect we should be locking the inodes before we use them! */
     AFS_GUNLOCK();
     cacheFp = afs_linux_raw_open(&tdc->f.inode);
+    if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
+       cachefs_noreadpage = 1;
+       AFS_GLOCK();
+       goto out;
+    }
     pagevec_init(&lrupv, 0);
 
     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
@@ -1926,6 +1992,12 @@ afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
 
     *codep = code;
     return 1;
+
+out:
+    ReleaseWriteLock(&avc->lock);
+    ReleaseReadLock(&tdc->lock);
+    afs_PutDCache(tdc);
+    return 0;
 }
 
 /* afs_linux_readpage
@@ -2083,7 +2155,7 @@ afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
 
        if(page_ix == 0) {
            offset = page_offset(pp);
-           auio->uio_offset = offset;
+           ancr->offset = auio->uio_offset = offset;
            base_index = pp->index;
        }
         iovecp[page_ix].iov_len = PAGE_SIZE;
@@ -2195,13 +2267,14 @@ afs_linux_bypass_readpage(struct file *fp, struct page *pp)
 
 static inline int
 afs_linux_can_bypass(struct inode *ip) {
+
     switch(cache_bypass_strategy) {
        case NEVER_BYPASS_CACHE:
            return 0;
        case ALWAYS_BYPASS_CACHE:
            return 1;
        case LARGE_FILES_BYPASS_CACHE:
-           if(i_size_read(ip) > cache_bypass_threshold)
+           if (i_size_read(ip) > cache_bypass_threshold)
                return 1;
        default:
            return 0;
@@ -2267,6 +2340,10 @@ afs_linux_readpages(struct file *fp, struct address_space *mapping,
     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
        return 0;
 
+    /* No readpage (ex: tmpfs) , skip */
+    if (cachefs_noreadpage)
+       return 0;
+
     AFS_GLOCK();
     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
        AFS_GUNLOCK();
@@ -2301,14 +2378,20 @@ afs_linux_readpages(struct file *fp, struct address_space *mapping,
                ObtainReadLock(&tdc->lock);
                if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
                    (tdc->dflags & DFFetching)) {
+                   goto out;
                    ReleaseReadLock(&tdc->lock);
                    afs_PutDCache(tdc);
                    tdc = NULL;
                }
            }
            AFS_GUNLOCK();
-           if (tdc)
+           if (tdc) {
                cacheFp = afs_linux_raw_open(&tdc->f.inode);
+               if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
+                   cachefs_noreadpage = 1;
+                   goto out;
+               }
+           }
        }
 
        if (tdc && !add_to_page_cache(page, mapping, page->index,
@@ -2324,6 +2407,7 @@ afs_linux_readpages(struct file *fp, struct address_space *mapping,
     if (pagevec_count(&lrupv))
        __pagevec_lru_add_file(&lrupv);
 
+out:
     if (tdc)
        filp_close(cacheFp, NULL);
 
@@ -2689,6 +2773,17 @@ afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
     struct dentry **dpp;
     struct dentry *target;
 
+    if (current->total_link_count > 0) {
+       /* avoid symlink resolution limits when resolving; we cannot contribute to
+        * an infinite symlink loop */
+       /* only do this for follow_link when total_link_count is positive to be
+        * on the safe side; there is at least one code path in the Linux
+        * kernel where it seems like it may be possible to get here without
+        * total_link_count getting incremented. it is not clear on how that
+        * path is actually reached, but guard against it just to be safe */
+       current->total_link_count--;
+    }
+
     target = canonical_dentry(dentry->d_inode);
 
 # ifdef STRUCT_NAMEIDATA_HAS_PATH
@@ -2705,6 +2800,8 @@ afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
        *dpp = dget(dentry);
     }
 
+    nd->last_type = LAST_BIND;
+
     return NULL;
 }
 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */