summaryrefslogtreecommitdiffstats
path: root/fs/coda/cache.c
blob: fa8a66e0ca865fd90916ce660ff0769512b92d7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Cache operations for Coda.
 * For Linux 2.1: (C) 1997 Carnegie Mellon University
 *
 * Carnegie Mellon encourages users of this code to contribute improvements
 * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/locks.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include <linux/list.h>

#include <linux/coda.h>
#include <linux/coda_linux.h>
#include <linux/coda_psdev.h>
#include <linux/coda_fs_i.h>
#include <linux/coda_cache.h>

static void coda_ccinsert(struct coda_cache *el, struct super_block *sb);
static void coda_cninsert(struct coda_cache *el, struct coda_inode_info *cii);
static void coda_ccremove(struct coda_cache *el);
static void coda_cnremove(struct coda_cache *el);
static void coda_cache_create(struct inode *inode, int mask);
static struct coda_cache * coda_cache_find(struct inode *inode);


/* insert a acl-cache entry in sb list */
static void coda_ccinsert(struct coda_cache *el, struct super_block *sb)
{
	struct coda_sb_info *sbi = coda_sbp(sb);
	ENTRY;
	/* third test verifies cc was initialized before adding it 
	   to the sblist. Probably superfluous */
	if ( !sbi || !el || !list_empty(&el->cc_cclist) ) {
		printk("coda_ccinsert: NULL sbi or el->cc_cclist not empty!\n");
		return ;
	}

	list_add(&el->cc_cclist, &sbi->sbi_cchead);
}

/* insert a acl-cache entry in the inode list */
static void coda_cninsert(struct coda_cache *el, struct coda_inode_info *cii)
{
	ENTRY;
	if ( !cii ||  !el || ! list_empty(&el->cc_cnlist)) {
		printk("coda_cninsert: NULL cii or el->cc_cnlist not empty!\n");
		return ;
	}
	list_add(&el->cc_cnlist, &cii->c_cnhead);
}

/* remove a cache entry from the superblock list */
static void coda_ccremove(struct coda_cache *el)
{
	ENTRY;
        if ( ! list_empty(&el->cc_cclist) )
	        list_del(&el->cc_cclist);
	else
		printk("coda_ccremove: loose cc entry!");
}

/* remove a cache entry from the inode's list */
static void coda_cnremove(struct coda_cache *el)
{
	ENTRY;
	if ( ! list_empty(&el->cc_cnlist) )
		list_del(&el->cc_cnlist);
	else
		printk("coda_cnremove: loose cn entry!");
}

/* create a new cache entry and enlist it */
static void coda_cache_create(struct inode *inode, int mask)
{
	struct coda_inode_info *cii = ITOC(inode);
	struct super_block *sb = inode->i_sb;
	struct coda_cache *cc = NULL;
	ENTRY;

	CODA_ALLOC(cc, struct coda_cache *, sizeof(*cc));

	if ( !cc ) {
		printk("Out of memory in coda_cache_enter!\n");
		return;
	}

	INIT_LIST_HEAD(&cc->cc_cclist);
	INIT_LIST_HEAD(&cc->cc_cnlist);

	coda_load_creds(&cc->cc_cred);
	cc->cc_mask = mask;
	coda_cninsert(cc, cii);
	coda_ccinsert(cc, sb);
}

/* see if there is a match for the current 
   credentials already */
static struct coda_cache * coda_cache_find(struct inode *inode)
{
	struct coda_inode_info *cii = ITOC(inode);
	struct list_head *lh, *le;
	struct coda_cache *cc = NULL;
	
	le = lh = &cii->c_cnhead;
	while( (le = le->next ) != lh )  {
		/* compare name and creds */
		cc = list_entry(le, struct coda_cache, cc_cnlist);
		if ( !coda_cred_ok(&cc->cc_cred) )
			continue;
		CDEBUG(D_CACHE, "HIT for ino %ld\n", inode->i_ino );
		return cc; /* cache hit */
	}
		return NULL;
}

/* create or extend an acl cache hit */
void coda_cache_enter(struct inode *inode, int mask)
{
	struct coda_cache *cc;

	cc = coda_cache_find(inode);

	if ( cc ) {
		cc->cc_mask |= mask;
	} else {
		coda_cache_create(inode, mask);
	}
}

/* remove all cached acl matches from an inode */
void coda_cache_clear_inode(struct inode *inode)
{
	struct list_head *le;
	struct coda_inode_info *cii;
	struct coda_cache *cc;
	ENTRY;

	if ( !inode ) {
		CDEBUG(D_CACHE, "coda_cache_clear_inode: NULL inode\n");
		return;
	}
	cii = ITOC(inode);
	
	le = cii->c_cnhead.next;
	while ( le != &cii->c_cnhead ) {
		cc = list_entry(le, struct coda_cache, cc_cnlist);
		le = le->next;
		coda_cnremove(cc);
		coda_ccremove(cc);
		CODA_FREE(cc, sizeof(*cc));
	}
}

/* remove all acl caches */
void coda_cache_clear_all(struct super_block *sb)
{
	struct list_head *le;
	struct coda_cache *cc;
	struct coda_sb_info *sbi = coda_sbp(sb);

	if ( !sbi ) {
		printk("coda_cache_clear_all: NULL sbi\n");
		return;
	}
	
	if ( list_empty(&sbi->sbi_cchead) )
		return;

	le = sbi->sbi_cchead.next;
	while ( le != &sbi->sbi_cchead ) {
		cc = list_entry(le, struct coda_cache, cc_cclist);
		le = le->next;
		coda_cnremove(cc);
		coda_ccremove(cc);
		CODA_FREE(cc, sizeof(*cc));
	}
}

/* remove all acl caches for a principal */
void coda_cache_clear_cred(struct super_block *sb, struct coda_cred *cred)
{
	struct list_head *le;
	struct coda_cache *cc;
	struct coda_sb_info *sbi = coda_sbp(sb);

	if ( !sbi ) {
		printk("coda_cache_clear_all: NULL sbi\n");
		return;
	}
	
	if (list_empty(&sbi->sbi_cchead))
		return;

	le = sbi->sbi_cchead.next;
	while ( le != &sbi->sbi_cchead ) {
		cc = list_entry(le, struct coda_cache, cc_cclist);
		le = le->next;
		if ( coda_cred_eq(&cc->cc_cred, cred)) {
			coda_cnremove(cc);
			coda_ccremove(cc);
			CODA_FREE(cc, sizeof(*cc));
		}
	}
}


/* check if the mask has been matched against the acl
   already */
int coda_cache_check(struct inode *inode, int mask)
{
	struct coda_inode_info *cii = ITOC(inode);
	struct list_head *lh, *le;
	struct coda_cache *cc = NULL;
	
	le = lh = &cii->c_cnhead;
	while( (le = le->next ) != lh )  {
		/* compare name and creds */
		cc = list_entry(le, struct coda_cache, cc_cnlist);
		if ( (cc->cc_mask & mask) != mask ) 
			continue; 
		if ( !coda_cred_ok(&cc->cc_cred) )
			continue;
		CDEBUG(D_CACHE, "HIT for ino %ld\n", inode->i_ino );
		return 1; /* cache hit */
	}
		CDEBUG(D_CACHE, "MISS for ino %ld\n", inode->i_ino );
		return 0;
}


/* Purging dentries and children */
/* The following routines drop dentries which are not
   in use and flag dentries which are in use to be 
   zapped later.

   The flags are detected by:
   - coda_dentry_revalidate (for lookups) if the flag is C_PURGE
   - coda_dentry_delete: to remove dentry from the cache when d_count
     falls to zero
   - an inode method coda_revalidate (for attributes) if the 
     flag is C_VATTR
*/

/* 
   Some of this is pretty scary: what can disappear underneath us?
   - shrink_dcache_parent calls on purge_one_dentry which is safe:
     it only purges children.
   - dput is evil since it  may recurse up the dentry tree
 */

void coda_purge_dentries(struct inode *inode)
{
	struct list_head *tmp, *head = &inode->i_dentry;

	if (!inode)
		return ;

	/* better safe than sorry: dput could kill us */
	iget(inode->i_sb, inode->i_ino);
	/* catch the dentries later if some are still busy */
	coda_flag_inode(inode, C_PURGE);

restart:
	tmp = head;
	while ((tmp = tmp->next) != head) {
		struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
		if (!dentry->d_count) {
			CDEBUG(D_DOWNCALL, 
			       "coda_free_dentries: freeing %s/%s, i_count=%d\n",
			       dentry->d_parent->d_name.name, dentry->d_name.name, 
			       inode->i_count);
			dget(dentry);
			d_drop(dentry);
			dput(dentry);
			goto restart;
		}
			
	}
	iput(inode);
}

/* this won't do any harm: just flag all children */
static void coda_flag_children(struct dentry *parent, int flag)
{
	struct list_head *child;
	struct dentry *de;

	child = parent->d_subdirs.next;
	while ( child != &parent->d_subdirs ) {
		de = list_entry(child, struct dentry, d_child);
		child = child->next;
		/* don't know what to do with negative dentries */
		if ( ! de->d_inode ) 
			continue;
		CDEBUG(D_DOWNCALL, "%d for %*s/%*s\n", flag, 
		       de->d_name.len, de->d_name.name, 
		       de->d_parent->d_name.len, de->d_parent->d_name.name);
		coda_flag_inode(de->d_inode, flag);
	}
	return; 
}

void coda_flag_inode_children(struct inode *inode, int flag)
{
	struct list_head *alias;
	struct dentry *alias_de;

	ENTRY;
	if ( !inode ) 
		return; 

	if (list_empty(&inode->i_dentry))
	        return; 

	/* I believe that shrink_dcache_parent will not
	   remove dentries from the alias list. If it 
	   does we are toast. 
	*/
	alias = inode->i_dentry.next; 
	while ( alias != &inode->i_dentry ) {
		alias_de = list_entry(alias, struct dentry, d_alias);
		coda_flag_children(alias_de, flag);
		alias = alias->next;
		shrink_dcache_parent(alias_de);
	}

}

/* this will not zap the inode away */
void coda_flag_inode(struct inode *inode, int flag)
{
	struct coda_inode_info *cii;

	if ( !inode ) {
		CDEBUG(D_CACHE, " no inode!\n");
		return;
	}

	cii = ITOC(inode);
	cii->c_flags |= flag;
}