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