blob: 859393fca2b7a28681de298652d6bac5f5a994c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cache operations for Coda.
3 * For Linux 2.1: (C) 1997 Carnegie Mellon University
4 * For Linux 2.3: (C) 2000 Carnegie Mellon University
5 *
6 * Carnegie Mellon encourages users of this code to contribute improvements
7 * to the Coda project http://www.coda.cs.cmu.edu/ <coda@cs.cmu.edu>.
8 */
9
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/time.h>
13#include <linux/fs.h>
14#include <linux/stat.h>
15#include <linux/errno.h>
16#include <asm/uaccess.h>
17#include <linux/string.h>
18#include <linux/list.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <linux/sched.h>
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040020#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <linux/coda.h>
23#include <linux/coda_linux.h>
24#include <linux/coda_psdev.h>
25#include <linux/coda_fs_i.h>
26#include <linux/coda_cache.h>
27
28static atomic_t permission_epoch = ATOMIC_INIT(0);
29
30/* replace or extend an acl cache hit */
31void coda_cache_enter(struct inode *inode, int mask)
32{
33 struct coda_inode_info *cii = ITOC(inode);
34
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040035 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 cii->c_cached_epoch = atomic_read(&permission_epoch);
David Howells97b77022008-11-14 10:38:48 +110037 if (cii->c_uid != current_fsuid()) {
38 cii->c_uid = current_fsuid();
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 cii->c_cached_perm = mask;
40 } else
41 cii->c_cached_perm |= mask;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040042 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45/* remove cached acl from an inode */
46void coda_cache_clear_inode(struct inode *inode)
47{
48 struct coda_inode_info *cii = ITOC(inode);
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040049 spin_lock(&cii->c_lock);
Jan Harkes56ee3542007-07-19 01:48:42 -070050 cii->c_cached_epoch = atomic_read(&permission_epoch) - 1;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040051 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54/* remove all acl caches */
55void coda_cache_clear_all(struct super_block *sb)
56{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 atomic_inc(&permission_epoch);
58}
59
60
61/* check if the mask has been matched against the acl already */
62int coda_cache_check(struct inode *inode, int mask)
63{
64 struct coda_inode_info *cii = ITOC(inode);
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040065 int hit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040067 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040073 return hit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76
77/* Purging dentries and children */
78/* The following routines drop dentries which are not
79 in use and flag dentries which are in use to be
80 zapped later.
81
82 The flags are detected by:
83 - coda_dentry_revalidate (for lookups) if the flag is C_PURGE
84 - coda_dentry_delete: to remove dentry from the cache when d_count
85 falls to zero
86 - an inode method coda_revalidate (for attributes) if the
87 flag is C_VATTR
88*/
89
90/* this won't do any harm: just flag all children */
91static void coda_flag_children(struct dentry *parent, int flag)
92{
93 struct list_head *child;
94 struct dentry *de;
95
96 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110097 spin_lock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 list_for_each(child, &parent->d_subdirs)
99 {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800100 de = list_entry(child, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* don't know what to do with negative dentries */
102 if ( ! de->d_inode )
103 continue;
104 coda_flag_inode(de->d_inode, flag);
105 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100106 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 spin_unlock(&dcache_lock);
108 return;
109}
110
111void coda_flag_inode_children(struct inode *inode, int flag)
112{
113 struct dentry *alias_de;
114
115 if ( !inode || !S_ISDIR(inode->i_mode))
116 return;
117
118 alias_de = d_find_alias(inode);
119 if (!alias_de)
120 return;
121 coda_flag_children(alias_de, flag);
122 shrink_dcache_parent(alias_de);
123 dput(alias_de);
124}
125