Whamcloud - gitweb
LU-17015 gss: avoid request replay
[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         entry->ue_acquire_expire = 0;
56         entry->ue_expire = 0;
57         if (cache->uc_ops->init_entry)
58                 cache->uc_ops->init_entry(entry, args);
59         return entry;
60 }
61
62 /* protected by cache lock */
63 static void free_entry(struct upcall_cache *cache,
64                        struct upcall_cache_entry *entry)
65 {
66         if (cache->uc_ops->free_entry)
67                 cache->uc_ops->free_entry(cache, entry);
68
69         list_del(&entry->ue_hash);
70         CDEBUG(D_OTHER, "destroy cache entry %p for key %llu\n",
71                 entry, entry->ue_key);
72         LIBCFS_FREE(entry, sizeof(*entry));
73 }
74
75 static inline int upcall_compare(struct upcall_cache *cache,
76                                  struct upcall_cache_entry *entry,
77                                  __u64 key, void *args)
78 {
79         if (entry->ue_key != key)
80                 return -1;
81
82         if (cache->uc_ops->upcall_compare)
83                 return cache->uc_ops->upcall_compare(cache, entry, key, args);
84
85         return 0;
86 }
87
88 static inline int downcall_compare(struct upcall_cache *cache,
89                                    struct upcall_cache_entry *entry,
90                                    __u64 key, void *args)
91 {
92         if (entry->ue_key != key)
93                 return -1;
94
95         if (cache->uc_ops->downcall_compare)
96                 return cache->uc_ops->downcall_compare(cache, entry, key, args);
97
98         return 0;
99 }
100
101 static inline void get_entry(struct upcall_cache_entry *entry)
102 {
103         atomic_inc(&entry->ue_refcount);
104 }
105
106 static inline void put_entry(struct upcall_cache *cache,
107                              struct upcall_cache_entry *entry)
108 {
109         if (atomic_dec_and_test(&entry->ue_refcount) &&
110             (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
111                 free_entry(cache, entry);
112         }
113 }
114
115 static int check_unlink_entry(struct upcall_cache *cache,
116                               struct upcall_cache_entry *entry)
117 {
118         time64_t now = ktime_get_seconds();
119
120         if (UC_CACHE_IS_VALID(entry) && now < entry->ue_expire)
121                 return 0;
122
123         if (UC_CACHE_IS_ACQUIRING(entry)) {
124                 if (entry->ue_acquire_expire == 0 ||
125                     now < entry->ue_acquire_expire)
126                         return 0;
127
128                 UC_CACHE_SET_EXPIRED(entry);
129                 wake_up(&entry->ue_waitq);
130         } else if (!UC_CACHE_IS_INVALID(entry)) {
131                 UC_CACHE_SET_EXPIRED(entry);
132         }
133
134         list_del_init(&entry->ue_hash);
135         if (!atomic_read(&entry->ue_refcount))
136                 free_entry(cache, entry);
137         return 1;
138 }
139
140 static inline int refresh_entry(struct upcall_cache *cache,
141                          struct upcall_cache_entry *entry)
142 {
143         LASSERT(cache->uc_ops->do_upcall);
144         return cache->uc_ops->do_upcall(cache, entry);
145 }
146
147 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
148                                                   __u64 key, void *args)
149 {
150         struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
151         bool failedacquiring = false;
152         struct list_head *head;
153         wait_queue_entry_t wait;
154         int rc, found;
155         ENTRY;
156
157         LASSERT(cache);
158
159         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
160                                                         cache->uc_hashsize)];
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 = ktime_get_seconds() +
204                                            cache->uc_acquire_expire;
205                 if (rc < 0) {
206                         UC_CACHE_CLEAR_ACQUIRING(entry);
207                         UC_CACHE_SET_INVALID(entry);
208                         wake_up(&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_wait(&wait);
224                 add_wait_queue(&entry->ue_waitq, &wait);
225                 set_current_state(TASK_INTERRUPTIBLE);
226                 spin_unlock(&cache->uc_lock);
227
228                 left = schedule_timeout(expiry);
229
230                 spin_lock(&cache->uc_lock);
231                 remove_wait_queue(&entry->ue_waitq, &wait);
232                 if (UC_CACHE_IS_ACQUIRING(entry)) {
233                         /* we're interrupted or upcall failed in the middle */
234                         rc = left > 0 ? -EINTR : -ETIMEDOUT;
235                         /* if we waited uc_acquire_expire, we can try again
236                          * with same data, but only if acquire is replayable
237                          */
238                         if (left <= 0 && !cache->uc_acquire_replay)
239                                 failedacquiring = true;
240                         put_entry(cache, entry);
241                         if (!failedacquiring) {
242                                 spin_unlock(&cache->uc_lock);
243                                 failedacquiring = true;
244                                 new = NULL;
245                                 CDEBUG(D_OTHER,
246                                        "retry acquire for key %llu (got %d)\n",
247                                        entry->ue_key, rc);
248                                 goto find_again;
249                         }
250                         CERROR("acquire for key %llu: error %d\n",
251                                entry->ue_key, rc);
252                         GOTO(out, entry = ERR_PTR(rc));
253                 }
254         }
255
256         /* invalid means error, don't need to try again */
257         if (UC_CACHE_IS_INVALID(entry)) {
258                 put_entry(cache, entry);
259                 GOTO(out, entry = ERR_PTR(-EIDRM));
260         }
261
262         /* check expired
263          * We can't refresh the existing one because some
264          * memory might be shared by multiple processes.
265          */
266         if (check_unlink_entry(cache, entry)) {
267                 /* if expired, try again. but if this entry is
268                  * created by me but too quickly turn to expired
269                  * without any error, should at least give a
270                  * chance to use it once.
271                  */
272                 if (entry != new) {
273                         put_entry(cache, entry);
274                         spin_unlock(&cache->uc_lock);
275                         new = NULL;
276                         goto find_again;
277                 }
278         }
279
280         /* Now we know it's good */
281 out:
282         spin_unlock(&cache->uc_lock);
283         RETURN(entry);
284 }
285 EXPORT_SYMBOL(upcall_cache_get_entry);
286
287 void upcall_cache_get_entry_raw(struct upcall_cache_entry *entry)
288 {
289         get_entry(entry);
290 }
291 EXPORT_SYMBOL(upcall_cache_get_entry_raw);
292
293 void upcall_cache_update_entry(struct upcall_cache *cache,
294                                struct upcall_cache_entry *entry,
295                                time64_t expire, int state)
296 {
297         spin_lock(&cache->uc_lock);
298         entry->ue_expire = expire;
299         if (!state)
300                 UC_CACHE_SET_VALID(entry);
301         else
302                 entry->ue_flags |= state;
303         spin_unlock(&cache->uc_lock);
304 }
305 EXPORT_SYMBOL(upcall_cache_update_entry);
306
307 void upcall_cache_put_entry(struct upcall_cache *cache,
308                             struct upcall_cache_entry *entry)
309 {
310         ENTRY;
311
312         if (!entry) {
313                 EXIT;
314                 return;
315         }
316
317         LASSERT(atomic_read(&entry->ue_refcount) > 0);
318         spin_lock(&cache->uc_lock);
319         put_entry(cache, entry);
320         spin_unlock(&cache->uc_lock);
321         EXIT;
322 }
323 EXPORT_SYMBOL(upcall_cache_put_entry);
324
325 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
326                           void *args)
327 {
328         struct upcall_cache_entry *entry = NULL;
329         struct list_head *head;
330         int found = 0, rc = 0;
331         ENTRY;
332
333         LASSERT(cache);
334
335         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
336                                                         cache->uc_hashsize)];
337
338         spin_lock(&cache->uc_lock);
339         list_for_each_entry(entry, head, ue_hash) {
340                 if (downcall_compare(cache, entry, key, args) == 0) {
341                         found = 1;
342                         get_entry(entry);
343                         break;
344                 }
345         }
346
347         if (!found) {
348                 CDEBUG(D_OTHER, "%s: upcall for key %llu not expected\n",
349                        cache->uc_name, key);
350                 /* haven't found, it's possible */
351                 spin_unlock(&cache->uc_lock);
352                 RETURN(-EINVAL);
353         }
354
355         if (err) {
356                 CDEBUG(D_OTHER, "%s: upcall for key %llu returned %d\n",
357                        cache->uc_name, entry->ue_key, err);
358                 GOTO(out, rc = err);
359         }
360
361         if (!UC_CACHE_IS_ACQUIRING(entry)) {
362                 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key %llu)"
363                        "\n", cache->uc_name, entry, entry->ue_key);
364                 GOTO(out, rc = 0);
365         }
366
367         if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
368                 CERROR("%s: found a stale entry %p (key %llu) in ioctl\n",
369                        cache->uc_name, entry, entry->ue_key);
370                 GOTO(out, rc = -EINVAL);
371         }
372
373         spin_unlock(&cache->uc_lock);
374         if (cache->uc_ops->parse_downcall)
375                 rc = cache->uc_ops->parse_downcall(cache, entry, args);
376         spin_lock(&cache->uc_lock);
377         if (rc)
378                 GOTO(out, rc);
379
380         if (!entry->ue_expire)
381                 entry->ue_expire = ktime_get_seconds() + cache->uc_entry_expire;
382         UC_CACHE_SET_VALID(entry);
383         CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key %llu\n",
384                cache->uc_name, entry, entry->ue_key);
385 out:
386         if (rc) {
387                 UC_CACHE_SET_INVALID(entry);
388                 list_del_init(&entry->ue_hash);
389         }
390         UC_CACHE_CLEAR_ACQUIRING(entry);
391         spin_unlock(&cache->uc_lock);
392         wake_up(&entry->ue_waitq);
393         put_entry(cache, entry);
394
395         RETURN(rc);
396 }
397 EXPORT_SYMBOL(upcall_cache_downcall);
398
399 void upcall_cache_flush(struct upcall_cache *cache, int force)
400 {
401         struct upcall_cache_entry *entry, *next;
402         int i;
403         ENTRY;
404
405         spin_lock(&cache->uc_lock);
406         for (i = 0; i < cache->uc_hashsize; i++) {
407                 list_for_each_entry_safe(entry, next,
408                                          &cache->uc_hashtable[i], ue_hash) {
409                         if (!force && atomic_read(&entry->ue_refcount)) {
410                                 UC_CACHE_SET_EXPIRED(entry);
411                                 continue;
412                         }
413                         LASSERT(!atomic_read(&entry->ue_refcount));
414                         free_entry(cache, entry);
415                 }
416         }
417         spin_unlock(&cache->uc_lock);
418         EXIT;
419 }
420 EXPORT_SYMBOL(upcall_cache_flush);
421
422 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
423 {
424         struct list_head *head;
425         struct upcall_cache_entry *entry;
426         int found = 0;
427         ENTRY;
428
429         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
430                                                         cache->uc_hashsize)];
431
432         spin_lock(&cache->uc_lock);
433         list_for_each_entry(entry, head, ue_hash) {
434                 if (upcall_compare(cache, entry, key, args) == 0) {
435                         found = 1;
436                         break;
437                 }
438         }
439
440         if (found) {
441                 CWARN("%s: flush entry %p: key %llu, ref %d, fl %x, "
442                       "cur %lld, ex %lld/%lld\n",
443                       cache->uc_name, entry, entry->ue_key,
444                       atomic_read(&entry->ue_refcount), entry->ue_flags,
445                       ktime_get_real_seconds(), entry->ue_acquire_expire,
446                       entry->ue_expire);
447                 UC_CACHE_SET_EXPIRED(entry);
448                 if (!atomic_read(&entry->ue_refcount))
449                         free_entry(cache, entry);
450         }
451         spin_unlock(&cache->uc_lock);
452 }
453 EXPORT_SYMBOL(upcall_cache_flush_one);
454
455 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
456                                        int hashsz, time64_t entry_expire,
457                                        time64_t acquire_expire, bool replayable,
458                                        struct upcall_cache_ops *ops)
459 {
460         struct upcall_cache *cache;
461         int i;
462         ENTRY;
463
464         LIBCFS_ALLOC(cache, sizeof(*cache));
465         if (!cache)
466                 RETURN(ERR_PTR(-ENOMEM));
467
468         spin_lock_init(&cache->uc_lock);
469         init_rwsem(&cache->uc_upcall_rwsem);
470         cache->uc_hashsize = hashsz;
471         LIBCFS_ALLOC(cache->uc_hashtable,
472                      sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
473         if (!cache->uc_hashtable)
474                 RETURN(ERR_PTR(-ENOMEM));
475         for (i = 0; i < cache->uc_hashsize; i++)
476                 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
477         strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
478         /* upcall pathname proc tunable */
479         strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
480         cache->uc_entry_expire = entry_expire;
481         cache->uc_acquire_expire = acquire_expire;
482         cache->uc_acquire_replay = replayable;
483         cache->uc_ops = ops;
484
485         RETURN(cache);
486 }
487 EXPORT_SYMBOL(upcall_cache_init);
488
489 void upcall_cache_cleanup(struct upcall_cache *cache)
490 {
491         if (!cache)
492                 return;
493         upcall_cache_flush_all(cache);
494         LIBCFS_FREE(cache->uc_hashtable,
495                     sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
496         LIBCFS_FREE(cache, sizeof(*cache));
497 }
498 EXPORT_SYMBOL(upcall_cache_cleanup);