Quick fix for readpages when using memcache
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
index 553e070..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
@@ -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);
@@ -1915,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();
@@ -1956,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,
@@ -2127,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 )
@@ -2171,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)
@@ -2216,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);
+
+    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(file->f_dentry->d_inode, page,
-                                    offset, to - offset);
+    code = afs_linux_writepage_sync(inode, page, offset, to - offset);
 
     return code;
 }
@@ -2233,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;
@@ -2259,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