Quick fix for readpages when using memcache
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
index c851adf..a72b752 100644 (file)
@@ -786,7 +786,8 @@ afs_linux_revalidate(struct dentry *dp)
      */
     if (vcp->f.states & CStatd &&
         (!afs_fakestat_enable || vcp->mvstat != 1) &&
-       !afs_nfsexporter) {
+       !afs_nfsexporter &&
+       (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
        code = afs_CopyOutAttrs(vcp, &vattr);
     } else {
         credp = crref();
@@ -829,6 +830,7 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
     struct vattr vattr;
     cred_t *credp = NULL;
     struct vcache *vcp, *pvcp, *tvc = NULL;
+    struct dentry *parent;
     int valid;
     struct afs_fakestat_state fakestate;
 
@@ -837,9 +839,7 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
     afs_InitFakeStat(&fakestate);
 
     if (dp->d_inode) {
-
        vcp = VTOAFS(dp->d_inode);
-       pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
 
        if (vcp == afs_globalVp)
            goto good_dentry;
@@ -882,6 +882,9 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
        }
 #endif
 
+       parent = dget_parent(dp);
+       pvcp = VTOAFS(parent->d_inode);
+
        /* If the parent's DataVersion has changed or the vnode
         * is longer valid, we need to do a full lookup.  VerifyVCache
         * isn't enough since the vnode may have been renamed.
@@ -891,11 +894,15 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
 
            credp = crref();
            afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
-           if (!tvc || tvc != vcp)
+           if (!tvc || tvc != vcp) {
+               dput(parent);
                goto bad_dentry;
+           }
 
-           if (afs_getattr(vcp, &vattr, credp))
+           if (afs_getattr(vcp, &vattr, credp)) {
+               dput(parent);
                goto bad_dentry;
+           }
 
            vattr2inode(AFSTOV(vcp), &vattr);
            dp->d_time = hgetlo(pvcp->f.m.DataVersion);
@@ -904,9 +911,13 @@ afs_linux_dentry_revalidate(struct dentry *dp, int flags)
        /* should we always update the attributes at this point? */
        /* unlikely--the vcache entry hasn't changed */
 
+       dput(parent);
     } else {
 #ifdef notyet
-       pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
+       /* If this code is ever enabled, we should use dget_parent to handle
+        * getting the parent, and dput() to dispose of it. See above for an
+        * example ... */
+       pvcp = VTOAFS(dp->d_parent->d_inode);
        if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
            goto bad_dentry;
 #endif
@@ -1073,7 +1084,7 @@ afs_linux_lookup(struct inode *dip, struct dentry *dp)
                dput(alias);
            } else {
                iput(ip);
-               unlock_kernel();
+               afs_maybe_unlock_kernel();
                crfree(credp);
                return alias;
            }
@@ -1114,6 +1125,69 @@ afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
     return afs_convert_code(code);
 }
 
+/* We have to have a Linux specific sillyrename function, because we
+ * also have to keep the dcache up to date when we're doing a silly
+ * rename - so we don't want the generic vnodeops doing this behind our
+ * back.
+ */
+
+static int
+afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
+                     cred_t *credp)
+{
+    struct vcache *tvc = VTOAFS(dentry->d_inode);
+    struct dentry *__dp = NULL;
+    char *__name = NULL;
+    int code;
+
+    if (afs_linux_nfsfs_renamed(dentry))
+       return EBUSY;
+
+    do {
+       dput(__dp);
+
+       AFS_GLOCK();
+       if (__name)
+           osi_FreeSmallSpace(__name);
+       __name = afs_newname();
+       AFS_GUNLOCK();
+
+       __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
+
+       if (IS_ERR(__dp)) {
+           osi_FreeSmallSpace(__name);
+           return EBUSY;
+       }
+    } while (__dp->d_inode != NULL);
+
+    AFS_GLOCK();
+    code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
+                     VTOAFS(dir), (char *)__dp->d_name.name,
+                     credp);
+    if (!code) {
+       tvc->mvid = (void *) __name;
+       crhold(credp);
+       if (tvc->uncred) {
+           crfree(tvc->uncred);
+       }
+       tvc->uncred = credp;
+       tvc->f.states |= CUnlinked;
+       afs_linux_set_nfsfs_renamed(dentry);
+    } else {
+       osi_FreeSmallSpace(__name);
+    }
+    AFS_GUNLOCK();
+
+    if (!code) {
+       __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
+       d_move(dentry, __dp);
+    }
+    dput(__dp);
+
+    return code;
+}
+
+
 static int
 afs_linux_unlink(struct inode *dip, struct dentry *dp)
 {
@@ -1123,59 +1197,19 @@ afs_linux_unlink(struct inode *dip, struct dentry *dp)
     struct vcache *tvc = VTOAFS(dp->d_inode);
 
     afs_maybe_lock_kernel();
+
     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
                                && !(tvc->f.states & CUnlinked)) {
-       struct dentry *__dp;
-       char *__name;
-
-       __dp = NULL;
-       __name = NULL;
-       do {
-           dput(__dp);
-
-           AFS_GLOCK();
-           if (__name)
-               osi_FreeSmallSpace(__name);
-           __name = afs_newname();
-           AFS_GUNLOCK();
-
-           __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
-               
-           if (IS_ERR(__dp))
-               goto out;
-       } while (__dp->d_inode != NULL);
 
+       code = afs_linux_sillyrename(dip, dp, credp);
+    } else {
        AFS_GLOCK();
-       code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
-       if (!code) {
-            tvc->mvid = (void *) __name;
-            crhold(credp);
-            if (tvc->uncred) {
-                crfree(tvc->uncred);
-            }
-            tvc->uncred = credp;
-           tvc->f.states |= CUnlinked;
-           afs_linux_set_nfsfs_renamed(dp);
-       } else {
-           osi_FreeSmallSpace(__name); 
-       }
+       code = afs_remove(VTOAFS(dip), (char *)name, credp);
        AFS_GUNLOCK();
-
-       if (!code) {
-           __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
-           d_move(dp, __dp);
-       }
-       dput(__dp);
-
-       goto out;
+       if (!code)
+           d_drop(dp);
     }
 
-    AFS_GLOCK();
-    code = afs_remove(VTOAFS(dip), (char *)name, credp);
-    AFS_GUNLOCK();
-    if (!code)
-       d_drop(dp);
-out:
     afs_maybe_unlock_kernel();
     crfree(credp);
     return afs_convert_code(code);
@@ -1559,7 +1593,7 @@ 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, NULL);
+    cacheFp = afs_linux_raw_open(&tdc->f.inode);
     pagevec_init(&lrupv, 0);
 
     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
@@ -1691,7 +1725,7 @@ afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
     afs_offs_t offset;
     struct iovec* iovecp;
     struct nocache_read_request *ancr;
-    struct page *pp, *ppt;
+    struct page *pp;
     struct pagevec lrupv;
     afs_int32 code = 0;
 
@@ -1788,7 +1822,6 @@ afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
     /* we do not flush, release, or unmap pages--that will be
      * done for us by the background thread as each page comes in
      * from the fileserver */
-out:
     return afs_convert_code(code);
 }
 
@@ -1800,7 +1833,7 @@ afs_linux_bypass_readpage(struct file *fp, struct page *pp)
     uio_t *auio;
     struct iovec *iovecp;
     struct nocache_read_request *ancr;
-    afs_int32 isize;
+    int code;
 
     ClearPageError(pp);
 
@@ -1818,7 +1851,7 @@ afs_linux_bypass_readpage(struct file *fp, struct page *pp)
     /* the background thread will free this */
     ancr = osi_Alloc(sizeof(struct nocache_read_request));
     ancr->auio = auio;
-    ancr->offset = offset;
+    ancr->offset = page_offset(pp);
     ancr->length = PAGE_SIZE;
 
     credp = crref();
@@ -1841,8 +1874,8 @@ afs_linux_can_bypass(struct inode *ip) {
            if(i_size_read(ip) > cache_bypass_threshold)
                return 1;
        default:
+           return 0;
      }
-     return 0;
 }
 
 /* Check if a file is permitted to bypass the cache by policy, and modify
@@ -1850,12 +1883,12 @@ afs_linux_can_bypass(struct inode *ip) {
 
 static inline int
 afs_linux_bypass_check(struct inode *ip) {
-    struct cred* credp;
+    cred_t* credp;
 
     int bypass = afs_linux_can_bypass(ip);
 
     credp = crref();
-    trydo_cache_transition(VTOAFS(ip)), credp, bypass);
+    trydo_cache_transition(VTOAFS(ip), credp, bypass);
     crfree(credp);
 
     return bypass;
@@ -1916,6 +1949,9 @@ afs_linux_readpages(struct file *fp, struct address_space *mapping,
     if (afs_linux_bypass_check(inode))
        return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
 
+    if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
+       return 0;
+
     AFS_GLOCK();
     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
        AFS_GUNLOCK();
@@ -1957,7 +1993,7 @@ afs_linux_readpages(struct file *fp, struct address_space *mapping,
            }
            AFS_GUNLOCK();
            if (tdc)
-               cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
+               cacheFp = afs_linux_raw_open(&tdc->f.inode);
        }
 
        if (tdc && !add_to_page_cache(page, mapping, page->index,
@@ -2070,15 +2106,13 @@ afs_linux_writepage_sync(struct inode *ip, struct page *pp,
 
     /* Catch recursive writeback. This occurs if the kernel decides
      * writeback is required whilst we are writing to the cache, or
-     * flushing to the server. */
+     * flushing to the server. When we're running syncronously (as
+     * opposed to from writepage) we can't actually do anything about
+     * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
+     */
     AFS_GLOCK();
     ObtainWriteLock(&vcp->lock, 532);
-    code = afs_linux_prepare_writeback(vcp);
-    if (code) {
-       ReleaseWriteLock(&vcp->lock);
-       AFS_GUNLOCK();
-       return code;
-    }
+    afs_linux_prepare_writeback(vcp);
     ReleaseWriteLock(&vcp->lock);
     AFS_GUNLOCK();
 
@@ -2130,24 +2164,43 @@ afs_linux_writepage(struct page *pp)
     isize = i_size_read(inode);
 
     /* Don't defeat an earlier truncate */
-    if (page_offset(pp) > isize)
+    if (page_offset(pp) > isize) {
+       set_page_writeback(pp);
+       unlock_page(pp);
        goto done;
+    }
 
     AFS_GLOCK();
     ObtainWriteLock(&vcp->lock, 537);
     code = afs_linux_prepare_writeback(vcp);
-    if (code) {
+    if (code == AOP_WRITEPAGE_ACTIVATE) {
+       /* WRITEPAGE_ACTIVATE is the only return value that permits us
+        * to return with the page still locked */
        ReleaseWriteLock(&vcp->lock);
        AFS_GUNLOCK();
        return code;
     }
+
     /* Grab the creds structure currently held in the vnode, and
      * get a reference to it, in case it goes away ... */
     credp = vcp->cred;
-    crhold(credp);
+    if (credp)
+       crhold(credp);
+    else
+       credp = crref();
     ReleaseWriteLock(&vcp->lock);
     AFS_GUNLOCK();
 
+    set_page_writeback(pp);
+
+    SetPageUptodate(pp);
+
+    /* We can unlock the page here, because it's protected by the
+     * page_writeback flag. This should make us less vulnerable to
+     * deadlocking in afs_write and afs_DoPartialWrite
+     */
+    unlock_page(pp);
+
     /* If this is the final page, then just write the number of bytes that
      * are actually in it */
     if ((isize - page_offset(pp)) < to )
@@ -2174,12 +2227,7 @@ afs_linux_writepage(struct page *pp)
     afs_maybe_unlock_kernel();
 
 done:
-    SetPageUptodate(pp);
-    if ( code != AOP_WRITEPAGE_ACTIVATE ) {
-       /* XXX - do we need to redirty the page here? */
-       unlock_page(pp);
-    }
-
+    end_page_writeback(pp);
     page_cache_release(pp);
 
     if (code1)
@@ -2219,15 +2267,23 @@ afs_linux_permission(struct inode *ip, int mode)
     return afs_convert_code(code);
 }
 
-#if !defined(HAVE_WRITE_BEGIN)
 static int
 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
                       unsigned to)
 {
     int code;
+    struct inode *inode = FILE_INODE(file);
+    loff_t pagebase = page_offset(page);
 
-    code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
-                                    offset, to - offset);
+    if (i_size_read(inode) < (pagebase + offset))
+       i_size_write(inode, pagebase + offset);
+
+    if (PageChecked(page)) {
+       SetPageUptodate(page);
+       ClearPageChecked(page);
+    }
+
+    code = afs_linux_writepage_sync(inode, page, offset, to - offset);
 
     return code;
 }
@@ -2236,20 +2292,47 @@ static int
 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
                        unsigned to)
 {
+
+    /* http://kerneltrap.org/node/4941 details the expected behaviour of
+     * prepare_write. Essentially, if the page exists within the file,
+     * and is not being fully written, then we should populate it.
+     */
+
+    if (!PageUptodate(page)) {
+       loff_t pagebase = page_offset(page);
+       loff_t isize = i_size_read(page->mapping->host);
+
+       /* Is the location we are writing to beyond the end of the file? */
+       if (pagebase >= isize ||
+           ((from == 0) && (pagebase + to) >= isize)) {
+           zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
+           SetPageChecked(page);
+       /* Are we we writing a full page */
+       } else if (from == 0 && to == PAGE_CACHE_SIZE) {
+           SetPageChecked(page);
+       /* Is the page readable, if it's wronly, we don't care, because we're
+        * not actually going to read from it ... */
+       } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
+           /* We don't care if fillpage fails, because if it does the page
+            * won't be marked as up to date
+            */
+           afs_linux_fillpage(file, page);
+       }
+    }
     return 0;
 }
-#else
 
+#if defined(HAVE_WRITE_BEGIN)
 static int
 afs_linux_write_end(struct file *file, struct address_space *mapping,
                                 loff_t pos, unsigned len, unsigned copied,
                                 struct page *page, void *fsdata)
 {
     int code;
-    unsigned from = pos & (PAGE_CACHE_SIZE - 1);
+    unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
+
+    code = afs_linux_commit_write(file, page, from, from + len);
 
-    code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
-                                    from, copied);
     unlock_page(page);
     page_cache_release(page);
     return code;
@@ -2262,10 +2345,19 @@ afs_linux_write_begin(struct file *file, struct address_space *mapping,
 {
     struct page *page;
     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
+    unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
+    int code;
+
     page = grab_cache_page_write_begin(mapping, index, flags);
     *pagep = page;
 
-    return 0;
+    code = afs_linux_prepare_write(file, page, from, from + len);
+    if (code) {
+       unlock_page(page);
+       page_cache_release(page);
+    }
+
+    return code;
 }
 #endif