Skip to content

Commit b5ce1d8

Browse files
Yoshihisa Abetorvalds
authored andcommitted
Coda: add spin lock to protect accesses to struct coda_inode_info.
We mostly need it to protect cached user permissions. The c_flags field is advisory, reading the wrong value is harmless and in the worst case we hit a slow path where we have to make an extra upcall to the userspace cache manager when revalidating a dentry or inode. Signed-off-by: Yoshihisa Abe <yoshiabe@cs.cmu.edu> Signed-off-by: Jan Harkes <jaharkes@cs.cmu.edu> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3a99c63 commit b5ce1d8

7 files changed

Lines changed: 58 additions & 17 deletions

File tree

‎fs/coda/cache.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/string.h>
1818
#include <linux/list.h>
1919
#include <linux/sched.h>
20+
#include <linux/spinlock.h>
2021

2122
#include <linux/coda.h>
2223
#include <linux/coda_linux.h>
@@ -31,19 +32,23 @@ void coda_cache_enter(struct inode *inode, int mask)
3132
{
3233
struct coda_inode_info *cii = ITOC(inode);
3334

35+
spin_lock(&cii->c_lock);
3436
cii->c_cached_epoch = atomic_read(&permission_epoch);
3537
if (cii->c_uid != current_fsuid()) {
3638
cii->c_uid = current_fsuid();
3739
cii->c_cached_perm = mask;
3840
} else
3941
cii->c_cached_perm |= mask;
42+
spin_unlock(&cii->c_lock);
4043
}
4144

4245
/* remove cached acl from an inode */
4346
void coda_cache_clear_inode(struct inode *inode)
4447
{
4548
struct coda_inode_info *cii = ITOC(inode);
49+
spin_lock(&cii->c_lock);
4650
cii->c_cached_epoch = atomic_read(&permission_epoch) - 1;
51+
spin_unlock(&cii->c_lock);
4752
}
4853

4954
/* remove all acl caches */
@@ -57,13 +62,15 @@ void coda_cache_clear_all(struct super_block *sb)
5762
int coda_cache_check(struct inode *inode, int mask)
5863
{
5964
struct coda_inode_info *cii = ITOC(inode);
60-
int hit;
65+
int hit;
6166

62-
hit = (mask & cii->c_cached_perm) == mask &&
63-
cii->c_uid == current_fsuid() &&
64-
cii->c_cached_epoch == atomic_read(&permission_epoch);
67+
spin_lock(&cii->c_lock);
68+
hit = (mask & cii->c_cached_perm) == mask &&
69+
cii->c_uid == current_fsuid() &&
70+
cii->c_cached_epoch == atomic_read(&permission_epoch);
71+
spin_unlock(&cii->c_lock);
6572

66-
return hit;
73+
return hit;
6774
}
6875

6976

‎fs/coda/cnode.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
4545
static int coda_test_inode(struct inode *inode, void *data)
4646
{
4747
struct CodaFid *fid = (struct CodaFid *)data;
48-
return coda_fideq(&(ITOC(inode)->c_fid), fid);
48+
struct coda_inode_info *cii = ITOC(inode);
49+
return coda_fideq(&cii->c_fid, fid);
4950
}
5051

5152
static int coda_set_inode(struct inode *inode, void *data)
5253
{
5354
struct CodaFid *fid = (struct CodaFid *)data;
54-
ITOC(inode)->c_fid = *fid;
55+
struct coda_inode_info *cii = ITOC(inode);
56+
cii->c_fid = *fid;
5557
return 0;
5658
}
5759

@@ -71,6 +73,7 @@ struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
7173
cii = ITOC(inode);
7274
/* we still need to set i_ino for things like stat(2) */
7375
inode->i_ino = hash;
76+
/* inode is locked and unique, no need to grab cii->c_lock */
7477
cii->c_mapcount = 0;
7578
unlock_new_inode(inode);
7679
}
@@ -107,14 +110,20 @@ int coda_cnode_make(struct inode **inode, struct CodaFid *fid, struct super_bloc
107110
}
108111

109112

113+
/* Although we treat Coda file identifiers as immutable, there is one
114+
* special case for files created during a disconnection where they may
115+
* not be globally unique. When an identifier collision is detected we
116+
* first try to flush the cached inode from the kernel and finally
117+
* resort to renaming/rehashing in-place. Userspace remembers both old
118+
* and new values of the identifier to handle any in-flight upcalls.
119+
* The real solution is to use globally unique UUIDs as identifiers, but
120+
* retrofitting the existing userspace code for this is non-trivial. */
110121
void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
111122
struct CodaFid *newfid)
112123
{
113-
struct coda_inode_info *cii;
124+
struct coda_inode_info *cii = ITOC(inode);
114125
unsigned long hash = coda_f2i(newfid);
115126

116-
cii = ITOC(inode);
117-
118127
BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
119128

120129
/* replace fid and rehash inode */

‎fs/coda/dir.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/errno.h>
1919
#include <linux/string.h>
2020
#include <linux/smp_lock.h>
21+
#include <linux/spinlock.h>
2122

2223
#include <asm/uaccess.h>
2324

@@ -617,7 +618,9 @@ static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
617618
goto out;
618619

619620
/* clear the flags. */
621+
spin_lock(&cii->c_lock);
620622
cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
623+
spin_unlock(&cii->c_lock);
621624

622625
bad:
623626
unlock_kernel();
@@ -691,7 +694,10 @@ int coda_revalidate_inode(struct dentry *dentry)
691694
goto return_bad;
692695

693696
coda_flag_inode_children(inode, C_FLUSH);
697+
698+
spin_lock(&cii->c_lock);
694699
cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
700+
spin_unlock(&cii->c_lock);
695701
}
696702

697703
ok:

‎fs/coda/file.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/cred.h>
1717
#include <linux/errno.h>
1818
#include <linux/smp_lock.h>
19+
#include <linux/spinlock.h>
1920
#include <linux/string.h>
2021
#include <linux/slab.h>
2122
#include <asm/uaccess.h>
@@ -109,19 +110,24 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
109110

110111
coda_inode = coda_file->f_path.dentry->d_inode;
111112
host_inode = host_file->f_path.dentry->d_inode;
113+
114+
cii = ITOC(coda_inode);
115+
spin_lock(&cii->c_lock);
112116
coda_file->f_mapping = host_file->f_mapping;
113117
if (coda_inode->i_mapping == &coda_inode->i_data)
114118
coda_inode->i_mapping = host_inode->i_mapping;
115119

116120
/* only allow additional mmaps as long as userspace isn't changing
117121
* the container file on us! */
118-
else if (coda_inode->i_mapping != host_inode->i_mapping)
122+
else if (coda_inode->i_mapping != host_inode->i_mapping) {
123+
spin_unlock(&cii->c_lock);
119124
return -EBUSY;
125+
}
120126

121127
/* keep track of how often the coda_inode/host_file has been mmapped */
122-
cii = ITOC(coda_inode);
123128
cii->c_mapcount++;
124129
cfi->cfi_mapcount++;
130+
spin_unlock(&cii->c_lock);
125131

126132
return host_file->f_op->mmap(host_file, vma);
127133
}
@@ -185,11 +191,13 @@ int coda_release(struct inode *coda_inode, struct file *coda_file)
185191
cii = ITOC(coda_inode);
186192

187193
/* did we mmap this file? */
194+
spin_lock(&cii->c_lock);
188195
if (coda_inode->i_mapping == &host_inode->i_data) {
189196
cii->c_mapcount -= cfi->cfi_mapcount;
190197
if (!cii->c_mapcount)
191198
coda_inode->i_mapping = &coda_inode->i_data;
192199
}
200+
spin_unlock(&cii->c_lock);
193201

194202
fput(cfi->cfi_container);
195203
kfree(coda_file->private_data);

‎fs/coda/inode.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/errno.h>
1717
#include <linux/unistd.h>
1818
#include <linux/smp_lock.h>
19+
#include <linux/spinlock.h>
1920
#include <linux/file.h>
2021
#include <linux/vfs.h>
2122
#include <linux/slab.h>
@@ -51,6 +52,7 @@ static struct inode *coda_alloc_inode(struct super_block *sb)
5152
ei->c_flags = 0;
5253
ei->c_uid = 0;
5354
ei->c_cached_perm = 0;
55+
spin_lock_init(&ei->c_lock);
5456
return &ei->vfs_inode;
5557
}
5658

‎include/linux/coda_fs_i.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@
1010

1111
#include <linux/types.h>
1212
#include <linux/list.h>
13+
#include <linux/spinlock.h>
1314
#include <linux/coda.h>
1415

1516
/*
1617
* coda fs inode data
18+
* c_lock protects accesses to c_flags, c_mapcount, c_cached_epoch, c_uid and
19+
* c_cached_perm.
20+
* vfs_inode is set only when the inode is created and never changes.
21+
* c_fid is set when the inode is created and should be considered immutable.
1722
*/
1823
struct coda_inode_info {
19-
struct CodaFid c_fid; /* Coda identifier */
20-
u_short c_flags; /* flags (see below) */
21-
struct list_head c_cilist; /* list of all coda inodes */
24+
struct CodaFid c_fid; /* Coda identifier */
25+
u_short c_flags; /* flags (see below) */
2226
unsigned int c_mapcount; /* nr of times this inode is mapped */
2327
unsigned int c_cached_epoch; /* epoch for cached permissions */
2428
vuid_t c_uid; /* fsuid for cached permissions */
25-
unsigned int c_cached_perm; /* cached access permissions */
29+
unsigned int c_cached_perm; /* cached access permissions */
30+
spinlock_t c_lock;
2631
struct inode vfs_inode;
2732
};
2833

‎include/linux/coda_linux.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ static __inline__ char *coda_i2s(struct inode *inode)
8989
/* this will not zap the inode away */
9090
static __inline__ void coda_flag_inode(struct inode *inode, int flag)
9191
{
92-
ITOC(inode)->c_flags |= flag;
92+
struct coda_inode_info *cii = ITOC(inode);
93+
94+
spin_lock(&cii->c_lock);
95+
cii->c_flags |= flag;
96+
spin_unlock(&cii->c_lock);
9397
}
9498

9599
#endif

0 commit comments

Comments
 (0)