| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 19 | #include <linux/sched.h> | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 20 | #include <linux/spinlock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | #include <linux/coda.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/coda_psdev.h> | 
| Al Viro | 31a203d | 2011-01-12 16:36:09 -0500 | [diff] [blame] | 24 | #include "coda_linux.h" | 
|  | 25 | #include "coda_cache.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | static atomic_t permission_epoch = ATOMIC_INIT(0); | 
|  | 28 |  | 
|  | 29 | /* replace or extend an acl cache hit */ | 
|  | 30 | void coda_cache_enter(struct inode *inode, int mask) | 
|  | 31 | { | 
|  | 32 | struct coda_inode_info *cii = ITOC(inode); | 
|  | 33 |  | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 34 | spin_lock(&cii->c_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | cii->c_cached_epoch = atomic_read(&permission_epoch); | 
| David Howells | 97b7702 | 2008-11-14 10:38:48 +1100 | [diff] [blame] | 36 | if (cii->c_uid != current_fsuid()) { | 
|  | 37 | cii->c_uid = current_fsuid(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | cii->c_cached_perm = mask; | 
|  | 39 | } else | 
|  | 40 | cii->c_cached_perm |= mask; | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 41 | spin_unlock(&cii->c_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
|  | 44 | /* remove cached acl from an inode */ | 
|  | 45 | void coda_cache_clear_inode(struct inode *inode) | 
|  | 46 | { | 
|  | 47 | struct coda_inode_info *cii = ITOC(inode); | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 48 | spin_lock(&cii->c_lock); | 
| Jan Harkes | 56ee354 | 2007-07-19 01:48:42 -0700 | [diff] [blame] | 49 | cii->c_cached_epoch = atomic_read(&permission_epoch) - 1; | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 50 | spin_unlock(&cii->c_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | /* remove all acl caches */ | 
|  | 54 | void coda_cache_clear_all(struct super_block *sb) | 
|  | 55 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | atomic_inc(&permission_epoch); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 |  | 
|  | 60 | /* check if the mask has been matched against the acl already */ | 
|  | 61 | int coda_cache_check(struct inode *inode, int mask) | 
|  | 62 | { | 
|  | 63 | struct coda_inode_info *cii = ITOC(inode); | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 64 | int hit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 66 | spin_lock(&cii->c_lock); | 
|  | 67 | hit = (mask & cii->c_cached_perm) == mask && | 
|  | 68 | cii->c_uid == current_fsuid() && | 
|  | 69 | cii->c_cached_epoch == atomic_read(&permission_epoch); | 
|  | 70 | spin_unlock(&cii->c_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 |  | 
| Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 72 | return hit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } | 
|  | 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 */ | 
|  | 90 | static void coda_flag_children(struct dentry *parent, int flag) | 
|  | 91 | { | 
|  | 92 | struct list_head *child; | 
|  | 93 | struct dentry *de; | 
|  | 94 |  | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 95 | spin_lock(&parent->d_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | list_for_each(child, &parent->d_subdirs) | 
|  | 97 | { | 
| Eric Dumazet | 5160ee6 | 2006-01-08 01:03:32 -0800 | [diff] [blame] | 98 | de = list_entry(child, struct dentry, d_u.d_child); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* don't know what to do with negative dentries */ | 
|  | 100 | if ( ! de->d_inode ) | 
|  | 101 | continue; | 
|  | 102 | coda_flag_inode(de->d_inode, flag); | 
|  | 103 | } | 
| Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 104 | spin_unlock(&parent->d_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | void coda_flag_inode_children(struct inode *inode, int flag) | 
|  | 109 | { | 
|  | 110 | struct dentry *alias_de; | 
|  | 111 |  | 
|  | 112 | if ( !inode || !S_ISDIR(inode->i_mode)) | 
|  | 113 | return; | 
|  | 114 |  | 
|  | 115 | alias_de = d_find_alias(inode); | 
|  | 116 | if (!alias_de) | 
|  | 117 | return; | 
|  | 118 | coda_flag_children(alias_de, flag); | 
|  | 119 | shrink_dcache_parent(alias_de); | 
|  | 120 | dput(alias_de); | 
|  | 121 | } | 
|  | 122 |  |