blob: 46ee6f238985a86186493cd22a245f4baa7638e0 [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>
Fabian Frederick834b46c2014-08-08 14:20:33 -070016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/coda_psdev.h>
Al Viro31a203d2011-01-12 16:36:09 -050024#include "coda_linux.h"
25#include "coda_cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static atomic_t permission_epoch = ATOMIC_INIT(0);
28
29/* replace or extend an acl cache hit */
30void coda_cache_enter(struct inode *inode, int mask)
31{
32 struct coda_inode_info *cii = ITOC(inode);
33
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040034 spin_lock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 cii->c_cached_epoch = atomic_read(&permission_epoch);
Eric W. Biederman17499e32013-01-30 19:36:06 -080036 if (!uid_eq(cii->c_uid, current_fsuid())) {
David Howells97b77022008-11-14 10:38:48 +110037 cii->c_uid = current_fsuid();
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 cii->c_cached_perm = mask;
39 } else
40 cii->c_cached_perm |= mask;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040041 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44/* remove cached acl from an inode */
45void coda_cache_clear_inode(struct inode *inode)
46{
47 struct coda_inode_info *cii = ITOC(inode);
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040048 spin_lock(&cii->c_lock);
Jan Harkes56ee3542007-07-19 01:48:42 -070049 cii->c_cached_epoch = atomic_read(&permission_epoch) - 1;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040050 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53/* remove all acl caches */
54void coda_cache_clear_all(struct super_block *sb)
55{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 atomic_inc(&permission_epoch);
57}
58
59
60/* check if the mask has been matched against the acl already */
61int coda_cache_check(struct inode *inode, int mask)
62{
63 struct coda_inode_info *cii = ITOC(inode);
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040064 int hit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040066 spin_lock(&cii->c_lock);
67 hit = (mask & cii->c_cached_perm) == mask &&
Eric W. Biederman17499e32013-01-30 19:36:06 -080068 uid_eq(cii->c_uid, current_fsuid()) &&
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040069 cii->c_cached_epoch == atomic_read(&permission_epoch);
70 spin_unlock(&cii->c_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040072 return hit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75
76/* Purging dentries and children */
77/* The following routines drop dentries which are not
78 in use and flag dentries which are in use to be
79 zapped later.
80
81 The flags are detected by:
82 - coda_dentry_revalidate (for lookups) if the flag is C_PURGE
83 - coda_dentry_delete: to remove dentry from the cache when d_count
84 falls to zero
85 - an inode method coda_revalidate (for attributes) if the
86 flag is C_VATTR
87*/
88
89/* this won't do any harm: just flag all children */
90static void coda_flag_children(struct dentry *parent, int flag)
91{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct dentry *de;
93
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110094 spin_lock(&parent->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -040095 list_for_each_entry(de, &parent->d_subdirs, d_child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* don't know what to do with negative dentries */
Al Viro1d674102012-06-09 19:52:19 -040097 if (de->d_inode )
98 coda_flag_inode(de->d_inode, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100100 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return;
102}
103
104void coda_flag_inode_children(struct inode *inode, int flag)
105{
106 struct dentry *alias_de;
107
108 if ( !inode || !S_ISDIR(inode->i_mode))
109 return;
110
111 alias_de = d_find_alias(inode);
112 if (!alias_de)
113 return;
114 coda_flag_children(alias_de, flag);
115 shrink_dcache_parent(alias_de);
116 dput(alias_de);
117}
118