proc: protect mm start_code/end_code in /proc/pid/stat
[firefly-linux-kernel-4.4.55.git] / fs / 9p / vfs_inode.c
index fdc086d07447201c90f48872d85099e646a9cedf..7f6c6770319538c3094f7b26eb40fc18a398475d 100644 (file)
@@ -220,6 +220,8 @@ struct inode *v9fs_alloc_inode(struct super_block *sb)
        spin_lock_init(&v9inode->fscache_lock);
 #endif
        v9inode->writeback_fid = NULL;
+       v9inode->cache_validity = 0;
+       mutex_init(&v9inode->v_mutex);
        return &v9inode->vfs_inode;
 }
 
@@ -498,8 +500,8 @@ v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
 {
        int retval;
-       struct inode *file_inode;
        struct p9_fid *v9fid;
+       struct inode *file_inode;
 
        P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
                rmdir);
@@ -510,8 +512,20 @@ static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
                return PTR_ERR(v9fid);
 
        retval = p9_client_remove(v9fid);
-       if (!retval)
-               drop_nlink(file_inode);
+       if (!retval) {
+               /*
+                * directories on unlink should have zero
+                * link count
+                */
+               if (rmdir) {
+                       clear_nlink(file_inode);
+                       drop_nlink(dir);
+               } else
+                       drop_nlink(file_inode);
+
+               v9fs_invalidate_inode_attr(file_inode);
+               v9fs_invalidate_inode_attr(dir);
+       }
        return retval;
 }
 
@@ -633,10 +647,13 @@ v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
                goto error;
        }
 
+       v9fs_invalidate_inode_attr(dir);
        /* if we are opening a file, assign the open fid to the file */
        if (nd && nd->flags & LOOKUP_OPEN) {
                v9inode = V9FS_I(dentry->d_inode);
-               if (v9ses->cache && !v9inode->writeback_fid) {
+               mutex_lock(&v9inode->v_mutex);
+               if (v9ses->cache && !v9inode->writeback_fid &&
+                   ((flags & O_ACCMODE) != O_RDONLY)) {
                        /*
                         * clone a fid and add it to writeback_fid
                         * we do it during open time instead of
@@ -647,10 +664,12 @@ v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
                        inode_fid = v9fs_writeback_fid(dentry);
                        if (IS_ERR(inode_fid)) {
                                err = PTR_ERR(inode_fid);
+                               mutex_unlock(&v9inode->v_mutex);
                                goto error;
                        }
                        v9inode->writeback_fid = (void *) inode_fid;
                }
+               mutex_unlock(&v9inode->v_mutex);
                filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
                if (IS_ERR(filp)) {
                        err = PTR_ERR(filp);
@@ -686,8 +705,8 @@ static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 {
        int err;
        u32 perm;
-       struct v9fs_session_info *v9ses;
        struct p9_fid *fid;
+       struct v9fs_session_info *v9ses;
 
        P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
        err = 0;
@@ -697,6 +716,9 @@ static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        if (IS_ERR(fid)) {
                err = PTR_ERR(fid);
                fid = NULL;
+       } else {
+               inc_nlink(dir);
+               v9fs_invalidate_inode_attr(dir);
        }
 
        if (fid)
@@ -808,17 +830,19 @@ int
 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
                struct inode *new_dir, struct dentry *new_dentry)
 {
+       int retval;
        struct inode *old_inode;
+       struct inode *new_inode;
        struct v9fs_session_info *v9ses;
        struct p9_fid *oldfid;
        struct p9_fid *olddirfid;
        struct p9_fid *newdirfid;
        struct p9_wstat wstat;
-       int retval;
 
        P9_DPRINTK(P9_DEBUG_VFS, "\n");
        retval = 0;
        old_inode = old_dentry->d_inode;
+       new_inode = new_dentry->d_inode;
        v9ses = v9fs_inode2v9ses(old_inode);
        oldfid = v9fs_fid_lookup(old_dentry);
        if (IS_ERR(oldfid))
@@ -859,9 +883,30 @@ v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
        retval = p9_client_wstat(oldfid, &wstat);
 
 clunk_newdir:
-       if (!retval)
+       if (!retval) {
+               if (new_inode) {
+                       if (S_ISDIR(new_inode->i_mode))
+                               clear_nlink(new_inode);
+                       else
+                               drop_nlink(new_inode);
+                       /*
+                        * Work around vfs rename rehash bug with
+                        * FS_RENAME_DOES_D_MOVE
+                        */
+                       v9fs_invalidate_inode_attr(new_inode);
+               }
+               if (S_ISDIR(old_inode->i_mode)) {
+                       if (!new_inode)
+                               inc_nlink(new_dir);
+                       drop_nlink(old_dir);
+               }
+               v9fs_invalidate_inode_attr(old_inode);
+               v9fs_invalidate_inode_attr(old_dir);
+               v9fs_invalidate_inode_attr(new_dir);
+
                /* successful rename */
                d_move(old_dentry, new_dentry);
+       }
        up_write(&v9ses->rename_sem);
        p9_client_clunk(newdirfid);
 
@@ -891,7 +936,7 @@ v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
 
        P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
        err = -EPERM;
-       v9ses = v9fs_inode2v9ses(dentry->d_inode);
+       v9ses = v9fs_dentry2v9ses(dentry);
        if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
                generic_fillattr(dentry->d_inode, stat);
                return 0;
@@ -927,8 +972,12 @@ static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
        struct p9_wstat wstat;
 
        P9_DPRINTK(P9_DEBUG_VFS, "\n");
+       retval = inode_change_ok(dentry->d_inode, iattr);
+       if (retval)
+               return retval;
+
        retval = -EPERM;
-       v9ses = v9fs_inode2v9ses(dentry->d_inode);
+       v9ses = v9fs_dentry2v9ses(dentry);
        fid = v9fs_fid_lookup(dentry);
        if(IS_ERR(fid))
                return PTR_ERR(fid);
@@ -954,16 +1003,19 @@ static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
                        wstat.n_gid = iattr->ia_gid;
        }
 
+       /* Write all dirty data */
+       if (S_ISREG(dentry->d_inode->i_mode))
+               filemap_write_and_wait(dentry->d_inode->i_mapping);
+
        retval = p9_client_wstat(fid, &wstat);
        if (retval < 0)
                return retval;
 
        if ((iattr->ia_valid & ATTR_SIZE) &&
-           iattr->ia_size != i_size_read(dentry->d_inode)) {
-               retval = vmtruncate(dentry->d_inode, iattr->ia_size);
-               if (retval)
-                       return retval;
-       }
+           iattr->ia_size != i_size_read(dentry->d_inode))
+               truncate_setsize(dentry->d_inode, iattr->ia_size);
+
+       v9fs_invalidate_inode_attr(dentry->d_inode);
 
        setattr_copy(dentry->d_inode, iattr);
        mark_inode_dirty(dentry->d_inode);
@@ -986,6 +1038,7 @@ v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
        char tag_name[14];
        unsigned int i_nlink;
        struct v9fs_session_info *v9ses = sb->s_fs_info;
+       struct v9fs_inode *v9inode = V9FS_I(inode);
 
        inode->i_nlink = 1;
 
@@ -1045,6 +1098,7 @@ v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
 
        /* not real number of blocks, but 512 byte ones ... */
        inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
+       v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
 }
 
 /**
@@ -1085,7 +1139,7 @@ static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
 
        P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
        retval = -EPERM;
-       v9ses = v9fs_inode2v9ses(dentry->d_inode);
+       v9ses = v9fs_dentry2v9ses(dentry);
        fid = v9fs_fid_lookup(dentry);
        if (IS_ERR(fid))
                return PTR_ERR(fid);
@@ -1177,8 +1231,8 @@ static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
        int mode, const char *extension)
 {
        u32 perm;
-       struct v9fs_session_info *v9ses;
        struct p9_fid *fid;
+       struct v9fs_session_info *v9ses;
 
        v9ses = v9fs_inode2v9ses(dir);
        if (!v9fs_proto_dotu(v9ses)) {
@@ -1192,6 +1246,7 @@ static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
        if (IS_ERR(fid))
                return PTR_ERR(fid);
 
+       v9fs_invalidate_inode_attr(dir);
        p9_client_clunk(fid);
        return 0;
 }
@@ -1228,8 +1283,8 @@ v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
              struct dentry *dentry)
 {
        int retval;
-       struct p9_fid *oldfid;
        char *name;
+       struct p9_fid *oldfid;
 
        P9_DPRINTK(P9_DEBUG_VFS,
                " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
@@ -1248,7 +1303,10 @@ v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
        sprintf(name, "%d\n", oldfid->fid);
        retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
        __putname(name);
-
+       if (!retval) {
+               v9fs_refresh_inode(oldfid, old_dentry->d_inode);
+               v9fs_invalidate_inode_attr(dir);
+       }
 clunk_fid:
        p9_client_clunk(oldfid);
        return retval;
@@ -1299,6 +1357,32 @@ v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
        return retval;
 }
 
+int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
+{
+       loff_t i_size;
+       struct p9_wstat *st;
+       struct v9fs_session_info *v9ses;
+
+       v9ses = v9fs_inode2v9ses(inode);
+       st = p9_client_stat(fid);
+       if (IS_ERR(st))
+               return PTR_ERR(st);
+
+       spin_lock(&inode->i_lock);
+       /*
+        * We don't want to refresh inode->i_size,
+        * because we may have cached data
+        */
+       i_size = inode->i_size;
+       v9fs_stat2inode(st, inode, inode->i_sb);
+       if (v9ses->cache)
+               inode->i_size = i_size;
+       spin_unlock(&inode->i_lock);
+       p9stat_free(st);
+       kfree(st);
+       return 0;
+}
+
 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
        .create = v9fs_vfs_create,
        .lookup = v9fs_vfs_lookup,