Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cache operations for Coda. |
| 4 | * For Linux 2.1: (C) 1997 Carnegie Mellon University |
| 5 | * For Linux 2.3: (C) 2000 Carnegie Mellon University |
| 6 | * |
| 7 | * Carnegie Mellon encourages users of this code to contribute improvements |
| 8 | * to the Coda project http://www.coda.cs.cmu.edu/ <coda@cs.cmu.edu>. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/stat.h> |
| 16 | #include <linux/errno.h> |
Fabian Frederick | 834b46c | 2014-08-08 14:20:33 -0700 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/list.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <linux/coda.h> |
David Howells | 8fc8b9d | 2019-07-16 16:28:47 -0700 | [diff] [blame] | 24 | #include "coda_psdev.h" |
Al Viro | 31a203d | 2011-01-12 16:36:09 -0500 | [diff] [blame] | 25 | #include "coda_linux.h" |
| 26 | #include "coda_cache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | static atomic_t permission_epoch = ATOMIC_INIT(0); |
| 29 | |
| 30 | /* replace or extend an acl cache hit */ |
| 31 | void coda_cache_enter(struct inode *inode, int mask) |
| 32 | { |
| 33 | struct coda_inode_info *cii = ITOC(inode); |
| 34 | |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 35 | spin_lock(&cii->c_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | cii->c_cached_epoch = atomic_read(&permission_epoch); |
Eric W. Biederman | 17499e3 | 2013-01-30 19:36:06 -0800 | [diff] [blame] | 37 | if (!uid_eq(cii->c_uid, current_fsuid())) { |
David Howells | 97b7702 | 2008-11-14 10:38:48 +1100 | [diff] [blame] | 38 | cii->c_uid = current_fsuid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | cii->c_cached_perm = mask; |
| 40 | } else |
| 41 | cii->c_cached_perm |= mask; |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 42 | spin_unlock(&cii->c_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* remove cached acl from an inode */ |
| 46 | void coda_cache_clear_inode(struct inode *inode) |
| 47 | { |
| 48 | struct coda_inode_info *cii = ITOC(inode); |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 49 | spin_lock(&cii->c_lock); |
Jan Harkes | 56ee354 | 2007-07-19 01:48:42 -0700 | [diff] [blame] | 50 | cii->c_cached_epoch = atomic_read(&permission_epoch) - 1; |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 51 | spin_unlock(&cii->c_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* remove all acl caches */ |
| 55 | void coda_cache_clear_all(struct super_block *sb) |
| 56 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | atomic_inc(&permission_epoch); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* check if the mask has been matched against the acl already */ |
| 62 | int coda_cache_check(struct inode *inode, int mask) |
| 63 | { |
| 64 | struct coda_inode_info *cii = ITOC(inode); |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 65 | int hit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 67 | spin_lock(&cii->c_lock); |
| 68 | hit = (mask & cii->c_cached_perm) == mask && |
Eric W. Biederman | 17499e3 | 2013-01-30 19:36:06 -0800 | [diff] [blame] | 69 | uid_eq(cii->c_uid, current_fsuid()) && |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 70 | cii->c_cached_epoch == atomic_read(&permission_epoch); |
| 71 | spin_unlock(&cii->c_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 73 | return hit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 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 */ |
| 91 | static void coda_flag_children(struct dentry *parent, int flag) |
| 92 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct dentry *de; |
| 94 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 95 | spin_lock(&parent->d_lock); |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 96 | list_for_each_entry(de, &parent->d_subdirs, d_child) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* don't know what to do with negative dentries */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 98 | if (d_inode(de) ) |
| 99 | coda_flag_inode(d_inode(de), flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 101 | spin_unlock(&parent->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | void coda_flag_inode_children(struct inode *inode, int flag) |
| 106 | { |
| 107 | struct dentry *alias_de; |
| 108 | |
| 109 | if ( !inode || !S_ISDIR(inode->i_mode)) |
| 110 | return; |
| 111 | |
| 112 | alias_de = d_find_alias(inode); |
| 113 | if (!alias_de) |
| 114 | return; |
| 115 | coda_flag_children(alias_de, flag); |
| 116 | shrink_dcache_parent(alias_de); |
| 117 | dput(alias_de); |
| 118 | } |
| 119 | |