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