Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / lustre / lvfs / upcall_cache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/upcall_cache.c
37  *
38  * Supplementary groups cache.
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifndef AUTOCONF_INCLUDED
44 #include <linux/config.h>
45 #endif
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/mm.h>
49 #include <linux/kmod.h>
50 #include <linux/string.h>
51 #include <linux/stat.h>
52 #include <linux/errno.h>
53 #include <linux/version.h>
54 #include <linux/unistd.h>
55
56 #include <asm/system.h>
57 #include <asm/uaccess.h>
58
59 #include <linux/fs.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/slab.h>
63
64 #include <obd_support.h>
65 #include <lustre_lib.h>
66
67 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
68                                               __u64 key, void *args)
69 {
70         struct upcall_cache_entry *entry;
71
72         OBD_ALLOC_PTR(entry);
73         if (!entry)
74                 return NULL;
75
76         UC_CACHE_SET_NEW(entry);
77         CFS_INIT_LIST_HEAD(&entry->ue_hash);
78         entry->ue_key = key;
79         cfs_atomic_set(&entry->ue_refcount, 0);
80         cfs_waitq_init(&entry->ue_waitq);
81         if (cache->uc_ops->init_entry)
82                 cache->uc_ops->init_entry(entry, args);
83         return entry;
84 }
85
86 /* protected by cache lock */
87 static void free_entry(struct upcall_cache *cache,
88                        struct upcall_cache_entry *entry)
89 {
90         if (cache->uc_ops->free_entry)
91                 cache->uc_ops->free_entry(cache, entry);
92
93         cfs_list_del(&entry->ue_hash);
94         CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
95                entry, entry->ue_key);
96         OBD_FREE_PTR(entry);
97 }
98
99 static inline int upcall_compare(struct upcall_cache *cache,
100                                  struct upcall_cache_entry *entry,
101                                  __u64 key, void *args)
102 {
103         if (entry->ue_key != key)
104                 return -1;
105
106         if (cache->uc_ops->upcall_compare)
107                 return cache->uc_ops->upcall_compare(cache, entry, key, args);
108
109         return 0;
110 }
111
112 static inline int downcall_compare(struct upcall_cache *cache,
113                                    struct upcall_cache_entry *entry,
114                                    __u64 key, void *args)
115 {
116         if (entry->ue_key != key)
117                 return -1;
118
119         if (cache->uc_ops->downcall_compare)
120                 return cache->uc_ops->downcall_compare(cache, entry, key, args);
121
122         return 0;
123 }
124
125 static inline void get_entry(struct upcall_cache_entry *entry)
126 {
127         cfs_atomic_inc(&entry->ue_refcount);
128 }
129
130 static inline void put_entry(struct upcall_cache *cache,
131                              struct upcall_cache_entry *entry)
132 {
133         if (cfs_atomic_dec_and_test(&entry->ue_refcount) &&
134             (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
135                 free_entry(cache, entry);
136         }
137 }
138
139 static int check_unlink_entry(struct upcall_cache *cache,
140                               struct upcall_cache_entry *entry)
141 {
142         if (UC_CACHE_IS_VALID(entry) &&
143             cfs_time_before(jiffies, entry->ue_expire))
144                 return 0;
145
146         if (UC_CACHE_IS_ACQUIRING(entry)) {
147                 if (cfs_time_before(jiffies, entry->ue_acquire_expire))
148                         return 0;
149
150                 UC_CACHE_SET_EXPIRED(entry);
151                 cfs_waitq_broadcast(&entry->ue_waitq);
152         } else if (!UC_CACHE_IS_INVALID(entry)) {
153                 UC_CACHE_SET_EXPIRED(entry);
154         }
155
156         cfs_list_del_init(&entry->ue_hash);
157         if (!cfs_atomic_read(&entry->ue_refcount))
158                 free_entry(cache, entry);
159         return 1;
160 }
161
162 static inline int refresh_entry(struct upcall_cache *cache,
163                          struct upcall_cache_entry *entry)
164 {
165         LASSERT(cache->uc_ops->do_upcall);
166         return cache->uc_ops->do_upcall(cache, entry);
167 }
168
169 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
170                                                   __u64 key, void *args)
171 {
172         struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
173         cfs_list_t *head;
174         cfs_waitlink_t wait;
175         int rc, found;
176         ENTRY;
177
178         LASSERT(cache);
179
180         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
181 find_again:
182         found = 0;
183         cfs_spin_lock(&cache->uc_lock);
184         cfs_list_for_each_entry_safe(entry, next, head, ue_hash) {
185                 /* check invalid & expired items */
186                 if (check_unlink_entry(cache, entry))
187                         continue;
188                 if (upcall_compare(cache, entry, key, args) == 0) {
189                         found = 1;
190                         break;
191                 }
192         }
193
194         if (!found) { /* didn't find it */
195                 if (!new) {
196                         cfs_spin_unlock(&cache->uc_lock);
197                         new = alloc_entry(cache, key, args);
198                         if (!new) {
199                                 CERROR("fail to alloc entry\n");
200                                 RETURN(ERR_PTR(-ENOMEM));
201                         }
202                         goto find_again;
203                 } else {
204                         cfs_list_add(&new->ue_hash, head);
205                         entry = new;
206                 }
207         } else {
208                 if (new) {
209                         free_entry(cache, new);
210                         new = NULL;
211                 }
212                 cfs_list_move(&entry->ue_hash, head);
213         }
214         get_entry(entry);
215
216         /* acquire for new one */
217         if (UC_CACHE_IS_NEW(entry)) {
218                 UC_CACHE_SET_ACQUIRING(entry);
219                 UC_CACHE_CLEAR_NEW(entry);
220                 entry->ue_acquire_expire = jiffies + cache->uc_acquire_expire;
221                 cfs_spin_unlock(&cache->uc_lock);
222                 rc = refresh_entry(cache, entry);
223                 cfs_spin_lock(&cache->uc_lock);
224                 if (rc < 0) {
225                         UC_CACHE_CLEAR_ACQUIRING(entry);
226                         UC_CACHE_SET_INVALID(entry);
227                         if (unlikely(rc == -EREMCHG)) {
228                                 put_entry(cache, entry);
229                                 GOTO(out, entry = ERR_PTR(rc));
230                         }
231                 }
232                 /* fall through */
233         }
234         /* someone (and only one) is doing upcall upon
235          * this item, just wait it complete
236          */
237         if (UC_CACHE_IS_ACQUIRING(entry)) {
238                 unsigned long expiry = jiffies + cache->uc_acquire_expire;
239
240                 cfs_waitlink_init(&wait);
241                 cfs_waitq_add(&entry->ue_waitq, &wait);
242                 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
243                 cfs_spin_unlock(&cache->uc_lock);
244
245                 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, 
246                                     cache->uc_acquire_expire);
247
248                 cfs_spin_lock(&cache->uc_lock);
249                 cfs_waitq_del(&entry->ue_waitq, &wait);
250                 if (UC_CACHE_IS_ACQUIRING(entry)) {
251                         /* we're interrupted or upcall failed in the middle */
252                         rc = cfs_time_before(jiffies, expiry) ? \
253                                 -EINTR : -ETIMEDOUT;
254                         put_entry(cache, entry);
255                         CERROR("acquire timeout exceeded for key "LPU64
256                                "\n", entry->ue_key);
257                         GOTO(out, entry = ERR_PTR(rc));
258                 }
259                 /* fall through */
260         }
261
262         /* invalid means error, don't need to try again */
263         if (UC_CACHE_IS_INVALID(entry)) {
264                 put_entry(cache, entry);
265                 GOTO(out, entry = ERR_PTR(-EIDRM));
266         }
267
268         /* check expired
269          * We can't refresh the existing one because some
270          * memory might be shared by multiple processes.
271          */
272         if (check_unlink_entry(cache, entry)) {
273                 /* if expired, try again. but if this entry is
274                  * created by me but too quickly turn to expired
275                  * without any error, should at least give a
276                  * chance to use it once.
277                  */
278                 if (entry != new) {
279                         put_entry(cache, entry);
280                         cfs_spin_unlock(&cache->uc_lock);
281                         new = NULL;
282                         goto find_again;
283                 }
284         }
285
286         /* Now we know it's good */
287 out:
288         cfs_spin_unlock(&cache->uc_lock);
289         RETURN(entry);
290 }
291 EXPORT_SYMBOL(upcall_cache_get_entry);
292
293 void upcall_cache_put_entry(struct upcall_cache *cache,
294                             struct upcall_cache_entry *entry)
295 {
296         ENTRY;
297
298         if (!entry) {
299                 EXIT;
300                 return;
301         }
302
303         LASSERT(cfs_atomic_read(&entry->ue_refcount) > 0);
304         cfs_spin_lock(&cache->uc_lock);
305         put_entry(cache, entry);
306         cfs_spin_unlock(&cache->uc_lock);
307         EXIT;
308 }
309 EXPORT_SYMBOL(upcall_cache_put_entry);
310
311 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
312                           void *args)
313 {
314         struct upcall_cache_entry *entry = NULL;
315         cfs_list_t *head;
316         int found = 0, rc = 0;
317         ENTRY;
318
319         LASSERT(cache);
320
321         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
322
323         cfs_spin_lock(&cache->uc_lock);
324         cfs_list_for_each_entry(entry, head, ue_hash) {
325                 if (downcall_compare(cache, entry, key, args) == 0) {
326                         found = 1;
327                         get_entry(entry);
328                         break;
329                 }
330         }
331
332         if (!found) {
333                 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
334                        cache->uc_name, key);
335                 /* haven't found, it's possible */
336                 cfs_spin_unlock(&cache->uc_lock);
337                 RETURN(-EINVAL);
338         }
339
340         if (err) {
341                 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
342                        cache->uc_name, entry->ue_key, err);
343                 GOTO(out, rc = -EINVAL);
344         }
345
346         if (!UC_CACHE_IS_ACQUIRING(entry)) {
347                 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
348                        cache->uc_name, entry, entry->ue_key);
349                 GOTO(out, rc = 0);
350         }
351
352         if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
353                 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
354                        cache->uc_name, entry, entry->ue_key);
355                 GOTO(out, rc = -EINVAL);
356         }
357
358         cfs_spin_unlock(&cache->uc_lock);
359         if (cache->uc_ops->parse_downcall)
360                 rc = cache->uc_ops->parse_downcall(cache, entry, args);
361         cfs_spin_lock(&cache->uc_lock);
362         if (rc)
363                 GOTO(out, rc);
364
365         entry->ue_expire = jiffies + cache->uc_entry_expire;
366         UC_CACHE_SET_VALID(entry);
367         CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
368                cache->uc_name, entry, entry->ue_key);
369 out:
370         if (rc) {
371                 UC_CACHE_SET_INVALID(entry);
372                 cfs_list_del_init(&entry->ue_hash);
373         }
374         UC_CACHE_CLEAR_ACQUIRING(entry);
375         cfs_spin_unlock(&cache->uc_lock);
376         cfs_waitq_broadcast(&entry->ue_waitq);
377         put_entry(cache, entry);
378
379         RETURN(rc);
380 }
381 EXPORT_SYMBOL(upcall_cache_downcall);
382
383 static void cache_flush(struct upcall_cache *cache, int force)
384 {
385         struct upcall_cache_entry *entry, *next;
386         int i;
387         ENTRY;
388
389         cfs_spin_lock(&cache->uc_lock);
390         for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
391                 cfs_list_for_each_entry_safe(entry, next,
392                                          &cache->uc_hashtable[i], ue_hash) {
393                         if (!force && cfs_atomic_read(&entry->ue_refcount)) {
394                                 UC_CACHE_SET_EXPIRED(entry);
395                                 continue;
396                         }
397                         LASSERT(!cfs_atomic_read(&entry->ue_refcount));
398                         free_entry(cache, entry);
399                 }
400         }
401         cfs_spin_unlock(&cache->uc_lock);
402         EXIT;
403 }
404
405 void upcall_cache_flush_idle(struct upcall_cache *cache)
406 {
407         cache_flush(cache, 0);
408 }
409 EXPORT_SYMBOL(upcall_cache_flush_idle);
410
411 void upcall_cache_flush_all(struct upcall_cache *cache)
412 {
413         cache_flush(cache, 1);
414 }
415 EXPORT_SYMBOL(upcall_cache_flush_all);
416
417 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
418 {
419         cfs_list_t *head;
420         struct upcall_cache_entry *entry;
421         int found = 0;
422         ENTRY;
423
424         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
425
426         cfs_spin_lock(&cache->uc_lock);
427         cfs_list_for_each_entry(entry, head, ue_hash) {
428                 if (upcall_compare(cache, entry, key, args) == 0) {
429                         found = 1;
430                         break;
431                 }
432         }
433
434         if (found) {
435                 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
436                       "cur %lu, ex %ld/%ld\n",
437                       cache->uc_name, entry, entry->ue_key,
438                       cfs_atomic_read(&entry->ue_refcount), entry->ue_flags,
439                       get_seconds(), entry->ue_acquire_expire,
440                       entry->ue_expire);
441                 UC_CACHE_SET_EXPIRED(entry);
442                 if (!cfs_atomic_read(&entry->ue_refcount))
443                         free_entry(cache, entry);
444         }
445         cfs_spin_unlock(&cache->uc_lock);
446 }
447 EXPORT_SYMBOL(upcall_cache_flush_one);
448
449 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
450                                        struct upcall_cache_ops *ops)
451 {
452         struct upcall_cache *cache;
453         int i;
454         ENTRY;
455
456         OBD_ALLOC(cache, sizeof(*cache));
457         if (!cache)
458                 RETURN(ERR_PTR(-ENOMEM));
459
460         cfs_spin_lock_init(&cache->uc_lock);
461         cfs_rwlock_init(&cache->uc_upcall_rwlock);
462         for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
463                 CFS_INIT_LIST_HEAD(&cache->uc_hashtable[i]);
464         strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
465         /* upcall pathname proc tunable */
466         strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
467         cache->uc_entry_expire = 10 * 60 * CFS_HZ;
468         cache->uc_acquire_expire = 15 * CFS_HZ;
469         cache->uc_ops = ops;
470
471         RETURN(cache);
472 }
473 EXPORT_SYMBOL(upcall_cache_init);
474
475 void upcall_cache_cleanup(struct upcall_cache *cache)
476 {
477         if (!cache)
478                 return;
479         upcall_cache_flush_all(cache);
480         OBD_FREE(cache, sizeof(*cache));
481 }
482 EXPORT_SYMBOL(upcall_cache_cleanup);