Whamcloud - gitweb
New tag 2.15.63
[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         struct list_head *head;
150         wait_queue_entry_t wait;
151         int rc, found;
152         ENTRY;
153
154         LASSERT(cache);
155
156         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
157 find_again:
158         found = 0;
159         spin_lock(&cache->uc_lock);
160         list_for_each_entry_safe(entry, next, head, ue_hash) {
161                 /* check invalid & expired items */
162                 if (check_unlink_entry(cache, entry))
163                         continue;
164                 if (upcall_compare(cache, entry, key, args) == 0) {
165                         found = 1;
166                         break;
167                 }
168         }
169
170         if (!found) {
171                 if (!new) {
172                         spin_unlock(&cache->uc_lock);
173                         new = alloc_entry(cache, key, args);
174                         if (!new) {
175                                 CERROR("fail to alloc entry\n");
176                                 RETURN(ERR_PTR(-ENOMEM));
177                         }
178                         goto find_again;
179                 } else {
180                         list_add(&new->ue_hash, head);
181                         entry = new;
182                 }
183         } else {
184                 if (new) {
185                         free_entry(cache, new);
186                         new = NULL;
187                 }
188                 list_move(&entry->ue_hash, head);
189         }
190         get_entry(entry);
191
192         /* acquire for new one */
193         if (UC_CACHE_IS_NEW(entry)) {
194                 UC_CACHE_SET_ACQUIRING(entry);
195                 UC_CACHE_CLEAR_NEW(entry);
196                 spin_unlock(&cache->uc_lock);
197                 rc = refresh_entry(cache, entry);
198                 spin_lock(&cache->uc_lock);
199                 entry->ue_acquire_expire = ktime_get_seconds() +
200                                            cache->uc_acquire_expire;
201                 if (rc < 0) {
202                         UC_CACHE_CLEAR_ACQUIRING(entry);
203                         UC_CACHE_SET_INVALID(entry);
204                         wake_up(&entry->ue_waitq);
205                         if (unlikely(rc == -EREMCHG)) {
206                                 put_entry(cache, entry);
207                                 GOTO(out, entry = ERR_PTR(rc));
208                         }
209                 }
210         }
211         /* someone (and only one) is doing upcall upon this item,
212          * wait it to complete */
213         if (UC_CACHE_IS_ACQUIRING(entry)) {
214                 long expiry = (entry == new) ?
215                               cfs_time_seconds(cache->uc_acquire_expire) :
216                               MAX_SCHEDULE_TIMEOUT;
217                 long left;
218
219                 init_wait(&wait);
220                 add_wait_queue(&entry->ue_waitq, &wait);
221                 set_current_state(TASK_INTERRUPTIBLE);
222                 spin_unlock(&cache->uc_lock);
223
224                 left = schedule_timeout(expiry);
225
226                 spin_lock(&cache->uc_lock);
227                 remove_wait_queue(&entry->ue_waitq, &wait);
228                 if (UC_CACHE_IS_ACQUIRING(entry)) {
229                         /* we're interrupted or upcall failed in the middle */
230                         rc = left > 0 ? -EINTR : -ETIMEDOUT;
231                         CERROR("acquire for key %llu: error %d\n",
232                                entry->ue_key, rc);
233                         put_entry(cache, entry);
234                         GOTO(out, entry = ERR_PTR(rc));
235                 }
236         }
237
238         /* invalid means error, don't need to try again */
239         if (UC_CACHE_IS_INVALID(entry)) {
240                 put_entry(cache, entry);
241                 GOTO(out, entry = ERR_PTR(-EIDRM));
242         }
243
244         /* check expired
245          * We can't refresh the existing one because some
246          * memory might be shared by multiple processes.
247          */
248         if (check_unlink_entry(cache, entry)) {
249                 /* if expired, try again. but if this entry is
250                  * created by me but too quickly turn to expired
251                  * without any error, should at least give a
252                  * chance to use it once.
253                  */
254                 if (entry != new) {
255                         put_entry(cache, entry);
256                         spin_unlock(&cache->uc_lock);
257                         new = NULL;
258                         goto find_again;
259                 }
260         }
261
262         /* Now we know it's good */
263 out:
264         spin_unlock(&cache->uc_lock);
265         RETURN(entry);
266 }
267 EXPORT_SYMBOL(upcall_cache_get_entry);
268
269 void upcall_cache_put_entry(struct upcall_cache *cache,
270                             struct upcall_cache_entry *entry)
271 {
272         ENTRY;
273
274         if (!entry) {
275                 EXIT;
276                 return;
277         }
278
279         LASSERT(atomic_read(&entry->ue_refcount) > 0);
280         spin_lock(&cache->uc_lock);
281         put_entry(cache, entry);
282         spin_unlock(&cache->uc_lock);
283         EXIT;
284 }
285 EXPORT_SYMBOL(upcall_cache_put_entry);
286
287 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
288                           void *args)
289 {
290         struct upcall_cache_entry *entry = NULL;
291         struct list_head *head;
292         int found = 0, rc = 0;
293         ENTRY;
294
295         LASSERT(cache);
296
297         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
298
299         spin_lock(&cache->uc_lock);
300         list_for_each_entry(entry, head, ue_hash) {
301                 if (downcall_compare(cache, entry, key, args) == 0) {
302                         found = 1;
303                         get_entry(entry);
304                         break;
305                 }
306         }
307
308         if (!found) {
309                 CDEBUG(D_OTHER, "%s: upcall for key %llu not expected\n",
310                        cache->uc_name, key);
311                 /* haven't found, it's possible */
312                 spin_unlock(&cache->uc_lock);
313                 RETURN(-EINVAL);
314         }
315
316         if (err) {
317                 CDEBUG(D_OTHER, "%s: upcall for key %llu returned %d\n",
318                        cache->uc_name, entry->ue_key, err);
319                 GOTO(out, rc = -EINVAL);
320         }
321
322         if (!UC_CACHE_IS_ACQUIRING(entry)) {
323                 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key %llu)"
324                        "\n", cache->uc_name, entry, entry->ue_key);
325                 GOTO(out, rc = 0);
326         }
327
328         if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
329                 CERROR("%s: found a stale entry %p (key %llu) in ioctl\n",
330                        cache->uc_name, entry, entry->ue_key);
331                 GOTO(out, rc = -EINVAL);
332         }
333
334         spin_unlock(&cache->uc_lock);
335         if (cache->uc_ops->parse_downcall)
336                 rc = cache->uc_ops->parse_downcall(cache, entry, args);
337         spin_lock(&cache->uc_lock);
338         if (rc)
339                 GOTO(out, rc);
340
341         entry->ue_expire = ktime_get_seconds() + cache->uc_entry_expire;
342         UC_CACHE_SET_VALID(entry);
343         CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key %llu\n",
344                cache->uc_name, entry, entry->ue_key);
345 out:
346         if (rc) {
347                 UC_CACHE_SET_INVALID(entry);
348                 list_del_init(&entry->ue_hash);
349         }
350         UC_CACHE_CLEAR_ACQUIRING(entry);
351         spin_unlock(&cache->uc_lock);
352         wake_up(&entry->ue_waitq);
353         put_entry(cache, entry);
354
355         RETURN(rc);
356 }
357 EXPORT_SYMBOL(upcall_cache_downcall);
358
359 void upcall_cache_flush(struct upcall_cache *cache, int force)
360 {
361         struct upcall_cache_entry *entry, *next;
362         int i;
363         ENTRY;
364
365         spin_lock(&cache->uc_lock);
366         for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
367                 list_for_each_entry_safe(entry, next,
368                                          &cache->uc_hashtable[i], ue_hash) {
369                         if (!force && atomic_read(&entry->ue_refcount)) {
370                                 UC_CACHE_SET_EXPIRED(entry);
371                                 continue;
372                         }
373                         LASSERT(!atomic_read(&entry->ue_refcount));
374                         free_entry(cache, entry);
375                 }
376         }
377         spin_unlock(&cache->uc_lock);
378         EXIT;
379 }
380 EXPORT_SYMBOL(upcall_cache_flush);
381
382 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
383 {
384         struct list_head *head;
385         struct upcall_cache_entry *entry;
386         int found = 0;
387         ENTRY;
388
389         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
390
391         spin_lock(&cache->uc_lock);
392         list_for_each_entry(entry, head, ue_hash) {
393                 if (upcall_compare(cache, entry, key, args) == 0) {
394                         found = 1;
395                         break;
396                 }
397         }
398
399         if (found) {
400                 CWARN("%s: flush entry %p: key %llu, ref %d, fl %x, "
401                       "cur %lld, ex %lld/%lld\n",
402                       cache->uc_name, entry, entry->ue_key,
403                       atomic_read(&entry->ue_refcount), entry->ue_flags,
404                       ktime_get_real_seconds(), entry->ue_acquire_expire,
405                       entry->ue_expire);
406                 UC_CACHE_SET_EXPIRED(entry);
407                 if (!atomic_read(&entry->ue_refcount))
408                         free_entry(cache, entry);
409         }
410         spin_unlock(&cache->uc_lock);
411 }
412 EXPORT_SYMBOL(upcall_cache_flush_one);
413
414 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
415                                        struct upcall_cache_ops *ops)
416 {
417         struct upcall_cache *cache;
418         int i;
419         ENTRY;
420
421         LIBCFS_ALLOC(cache, sizeof(*cache));
422         if (!cache)
423                 RETURN(ERR_PTR(-ENOMEM));
424
425         spin_lock_init(&cache->uc_lock);
426         init_rwsem(&cache->uc_upcall_rwsem);
427         for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
428                 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
429         strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
430         /* upcall pathname proc tunable */
431         strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
432         cache->uc_entry_expire = 20 * 60;
433         cache->uc_acquire_expire = 30;
434         cache->uc_ops = ops;
435
436         RETURN(cache);
437 }
438 EXPORT_SYMBOL(upcall_cache_init);
439
440 void upcall_cache_cleanup(struct upcall_cache *cache)
441 {
442         if (!cache)
443                 return;
444         upcall_cache_flush_all(cache);
445         LIBCFS_FREE(cache, sizeof(*cache));
446 }
447 EXPORT_SYMBOL(upcall_cache_cleanup);