Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[powerpc.git] / fs / nfs / inode.c
index f2781ca..432f41c 100644 (file)
@@ -54,7 +54,7 @@
 #define NFS_MAX_READAHEAD      (RPC_DEF_SLOT_TABLE - 1)
 
 static void nfs_invalidate_inode(struct inode *);
-static int nfs_update_inode(struct inode *, struct nfs_fattr *, unsigned long);
+static int nfs_update_inode(struct inode *, struct nfs_fattr *);
 
 static struct inode *nfs_alloc_inode(struct super_block *sb);
 static void nfs_destroy_inode(struct inode *);
@@ -640,17 +640,35 @@ static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
        return 0;
 }
 
+/**
+ * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
+ */
+int nfs_sync_mapping(struct address_space *mapping)
+{
+       int ret;
+
+       if (mapping->nrpages == 0)
+               return 0;
+       unmap_mapping_range(mapping, 0, 0, 0);
+       ret = filemap_fdatawrite(mapping);
+       if (ret != 0)
+               goto out;
+       ret = filemap_fdatawait(mapping);
+       if (ret != 0)
+               goto out;
+       ret = nfs_wb_all(mapping->host);
+out:
+       return ret;
+}
+
 /*
  * Invalidate the local caches
  */
-void
-nfs_zap_caches(struct inode *inode)
+static void nfs_zap_caches_locked(struct inode *inode)
 {
        struct nfs_inode *nfsi = NFS_I(inode);
        int mode = inode->i_mode;
 
-       spin_lock(&inode->i_lock);
-
        NFS_ATTRTIMEO(inode) = NFS_MINATTRTIMEO(inode);
        NFS_ATTRTIMEO_UPDATE(inode) = jiffies;
 
@@ -659,7 +677,12 @@ nfs_zap_caches(struct inode *inode)
                nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
        else
                nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
+}
 
+void nfs_zap_caches(struct inode *inode)
+{
+       spin_lock(&inode->i_lock);
+       nfs_zap_caches_locked(inode);
        spin_unlock(&inode->i_lock);
 }
 
@@ -676,16 +699,13 @@ static void nfs_zap_acl_cache(struct inode *inode)
 }
 
 /*
- * Invalidate, but do not unhash, the inode
+ * Invalidate, but do not unhash, the inode.
+ * NB: must be called with inode->i_lock held!
  */
-static void
-nfs_invalidate_inode(struct inode *inode)
+static void nfs_invalidate_inode(struct inode *inode)
 {
-       umode_t save_mode = inode->i_mode;
-
-       make_bad_inode(inode);
-       inode->i_mode = save_mode;
-       nfs_zap_caches(inode);
+       set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
+       nfs_zap_caches_locked(inode);
 }
 
 struct nfs_find_desc {
@@ -1009,13 +1029,18 @@ void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
        spin_unlock(&inode->i_lock);
 }
 
-struct nfs_open_context *nfs_find_open_context(struct inode *inode, int mode)
+/*
+ * Given an inode, search for an open context with the desired characteristics
+ */
+struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, int mode)
 {
        struct nfs_inode *nfsi = NFS_I(inode);
        struct nfs_open_context *pos, *ctx = NULL;
 
        spin_lock(&inode->i_lock);
        list_for_each_entry(pos, &nfsi->open_files, list) {
+               if (cred != NULL && pos->cred != cred)
+                       continue;
                if ((pos->mode & mode) == mode) {
                        ctx = get_nfs_open_context(pos);
                        break;
@@ -1076,8 +1101,6 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
        int              status = -ESTALE;
        struct nfs_fattr fattr;
        struct nfs_inode *nfsi = NFS_I(inode);
-       unsigned long verifier;
-       unsigned long cache_validity;
 
        dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
                inode->i_sb->s_id, (long long)NFS_FILEID(inode));
@@ -1102,8 +1125,6 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
                }
        }
 
-       /* Protect against RPC races by saving the change attribute */
-       verifier = nfs_save_change_attribute(inode);
        status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
        if (status != 0) {
                dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
@@ -1118,7 +1139,7 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
        }
 
        spin_lock(&inode->i_lock);
-       status = nfs_update_inode(inode, &fattr, verifier);
+       status = nfs_update_inode(inode, &fattr);
        if (status) {
                spin_unlock(&inode->i_lock);
                dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
@@ -1126,20 +1147,11 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
                         (long long)NFS_FILEID(inode), status);
                goto out;
        }
-       cache_validity = nfsi->cache_validity;
-       nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
-
-       /*
-        * We may need to keep the attributes marked as invalid if
-        * we raced with nfs_end_attr_update().
-        */
-       if (time_after_eq(verifier, nfsi->cache_change_attribute))
-               nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
        spin_unlock(&inode->i_lock);
 
        nfs_revalidate_mapping(inode, inode->i_mapping);
 
-       if (cache_validity & NFS_INO_INVALID_ACL)
+       if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
                nfs_zap_acl_cache(inode);
 
        dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
@@ -1188,11 +1200,8 @@ void nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
        struct nfs_inode *nfsi = NFS_I(inode);
 
        if (nfsi->cache_validity & NFS_INO_INVALID_DATA) {
-               if (S_ISREG(inode->i_mode)) {
-                       if (filemap_fdatawrite(mapping) == 0)
-                               filemap_fdatawait(mapping);
-                       nfs_wb_all(inode);
-               }
+               if (S_ISREG(inode->i_mode))
+                       nfs_sync_mapping(mapping);
                invalidate_inode_pages2(mapping);
 
                spin_lock(&inode->i_lock);
@@ -1274,14 +1283,12 @@ static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fat
        }
 
        if ((fattr->valid & NFS_ATTR_FATTR) == 0) {
-               spin_unlock(&inode->i_lock);
                return 0;
        }
 
        /* Has the inode gone and changed behind our back? */
        if (nfsi->fileid != fattr->fileid
                        || (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
-               spin_unlock(&inode->i_lock);
                return -EIO;
        }
 
@@ -1344,10 +1351,8 @@ int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
                return 0;
        spin_lock(&inode->i_lock);
        nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
-       if (nfs_verify_change_attribute(inode, fattr->time_start))
-               nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
        if (time_after(fattr->time_start, nfsi->last_updated))
-               status = nfs_update_inode(inode, fattr, fattr->time_start);
+               status = nfs_update_inode(inode, fattr);
        else
                status = nfs_check_inode_attributes(inode, fattr);
 
@@ -1373,10 +1378,7 @@ int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
                nfsi->cache_validity |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS;
                goto out;
        }
-       status = nfs_update_inode(inode, fattr, fattr->time_start);
-       if (time_after_eq(fattr->time_start, nfsi->cache_change_attribute))
-               nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
-       nfsi->cache_change_attribute = jiffies;
+       status = nfs_update_inode(inode, fattr);
 out:
        spin_unlock(&inode->i_lock);
        return status;
@@ -1394,12 +1396,12 @@ out:
  *
  * A very similar scenario holds for the dir cache.
  */
-static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsigned long verifier)
+static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
 {
        struct nfs_inode *nfsi = NFS_I(inode);
        loff_t cur_isize, new_isize;
        unsigned int    invalid = 0;
-       int data_unstable;
+       int data_stable;
 
        dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
                        __FUNCTION__, inode->i_sb->s_id, inode->i_ino,
@@ -1430,8 +1432,9 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
        nfsi->last_updated = jiffies;
 
        /* Are we racing with known updates of the metadata on the server? */
-       data_unstable = ! (nfs_verify_change_attribute(inode, verifier) ||
-               (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE));
+       data_stable = nfs_verify_change_attribute(inode, fattr->time_start);
+       if (data_stable)
+               nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME);
 
        /* Check if our cached file size is stale */
        new_isize = nfs_size_to_loff_t(fattr->size);
@@ -1440,7 +1443,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
                /* Do we perhaps have any outstanding writes? */
                if (nfsi->npages == 0) {
                        /* No, but did we race with nfs_end_data_update()? */
-                       if (time_after_eq(verifier,  nfsi->cache_change_attribute)) {
+                       if (data_stable) {
                                inode->i_size = new_isize;
                                invalid |= NFS_INO_INVALID_DATA;
                        }
@@ -1449,6 +1452,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
                        inode->i_size = new_isize;
                        invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
                }
+               nfsi->cache_change_attribute = jiffies;
                dprintk("NFS: isize change on server for file %s/%ld\n",
                                inode->i_sb->s_id, inode->i_ino);
        }
@@ -1458,8 +1462,8 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
                memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
                dprintk("NFS: mtime change on server for file %s/%ld\n",
                                inode->i_sb->s_id, inode->i_ino);
-               if (!data_unstable)
-                       invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
+               invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
+               nfsi->cache_change_attribute = jiffies;
        }
 
        if ((fattr->valid & NFS_ATTR_FATTR_V4)
@@ -1467,15 +1471,15 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
                dprintk("NFS: change_attr change on server for file %s/%ld\n",
                       inode->i_sb->s_id, inode->i_ino);
                nfsi->change_attr = fattr->change_attr;
-               if (!data_unstable)
-                       invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
+               invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
+               nfsi->cache_change_attribute = jiffies;
        }
 
        /* If ctime has changed we should definitely clear access+acl caches */
        if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
-               if (!data_unstable)
-                       invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
+               invalid |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
                memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
+               nfsi->cache_change_attribute = jiffies;
        }
        memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
 
@@ -1513,6 +1517,8 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
        if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
                                || S_ISLNK(inode->i_mode)))
                invalid &= ~NFS_INO_INVALID_DATA;
+       if (data_stable)
+               invalid &= ~(NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME|NFS_INO_REVAL_PAGECACHE);
        if (!nfs_have_delegation(inode, FMODE_READ))
                nfsi->cache_validity |= invalid;
 
@@ -1525,14 +1531,13 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr, unsign
        printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
                        __FUNCTION__, inode->i_ino, inode->i_mode, fattr->mode);
 #endif
+ out_err:
        /*
         * No need to worry about unhashing the dentry, as the
         * lookup validation will know that the inode is bad.
         * (But we fall through to invalidate the caches.)
         */
        nfs_invalidate_inode(inode);
- out_err:
-       set_bit(NFS_INO_STALE, &NFS_FLAGS(inode));
        return -ESTALE;
 }
 
@@ -1685,8 +1690,7 @@ static void nfs_kill_super(struct super_block *s)
 
        rpciod_down();          /* release rpciod */
 
-       if (server->hostname != NULL)
-               kfree(server->hostname);
+       kfree(server->hostname);
        kfree(server);
 }
 
@@ -1905,8 +1909,7 @@ nfs_copy_user_string(char *dst, struct nfs_string *src, int maxlen)
                        return ERR_PTR(-ENOMEM);
        }
        if (copy_from_user(dst, src->data, maxlen)) {
-               if (p != NULL)
-                       kfree(p);
+               kfree(p);
                return ERR_PTR(-EFAULT);
        }
        dst[maxlen] = '\0';
@@ -1997,10 +2000,8 @@ static struct super_block *nfs4_get_sb(struct file_system_type *fs_type,
 out_err:
        s = (struct super_block *)p;
 out_free:
-       if (server->mnt_path)
-               kfree(server->mnt_path);
-       if (server->hostname)
-               kfree(server->hostname);
+       kfree(server->mnt_path);
+       kfree(server->hostname);
        kfree(server);
        return s;
 }
@@ -2020,8 +2021,7 @@ static void nfs4_kill_super(struct super_block *sb)
 
        destroy_nfsv4_state(server);
 
-       if (server->hostname != NULL)
-               kfree(server->hostname);
+       kfree(server->hostname);
        kfree(server);
 }
 
@@ -2070,6 +2070,7 @@ static struct inode *nfs_alloc_inode(struct super_block *sb)
                return NULL;
        nfsi->flags = 0UL;
        nfsi->cache_validity = 0UL;
+       nfsi->cache_change_attribute = jiffies;
 #ifdef CONFIG_NFS_V3_ACL
        nfsi->acl_access = ERR_PTR(-EAGAIN);
        nfsi->acl_default = ERR_PTR(-EAGAIN);