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