Whamcloud - gitweb
LU-17497 obdclass: check upcall incorrect values
[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 inline void write_lock_from_read(rwlock_t *lock, bool *writelock)
116 {
117         if (!*writelock) {
118                 read_unlock(lock);
119                 write_lock(lock);
120                 *writelock = true;
121         }
122 }
123
124 static int check_unlink_entry(struct upcall_cache *cache,
125                               struct upcall_cache_entry *entry,
126                               bool writelock)
127 {
128         time64_t now = ktime_get_seconds();
129
130         if (UC_CACHE_IS_VALID(entry) && now < entry->ue_expire)
131                 return 0;
132
133         if (UC_CACHE_IS_ACQUIRING(entry)) {
134                 if (entry->ue_acquire_expire == 0 ||
135                     now < entry->ue_acquire_expire)
136                         return 0;
137
138                 if (writelock) {
139                         UC_CACHE_SET_EXPIRED(entry);
140                         wake_up(&entry->ue_waitq);
141                 }
142         } else if (!UC_CACHE_IS_INVALID(entry) && writelock) {
143                 UC_CACHE_SET_EXPIRED(entry);
144         }
145
146         if (writelock) {
147                 list_del_init(&entry->ue_hash);
148                 if (!atomic_read(&entry->ue_refcount))
149                         free_entry(cache, entry);
150         }
151         return 1;
152 }
153
154 int upcall_cache_set_upcall(struct upcall_cache *cache, const char *buffer,
155                             size_t count, bool path_only)
156 {
157         char *upcall;
158
159         if (count >= UC_CACHE_UPCALL_MAXPATH)
160                 return -E2BIG;
161
162         OBD_ALLOC(upcall, count + 1);
163         if (upcall == NULL)
164                 return -ENOMEM;
165
166         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
167         if (sscanf(buffer, "%s", upcall) != 1)
168                 goto invalid;
169
170         if (upcall[0] == '/')
171                 goto valid;
172
173         if (path_only)
174                 goto invalid;
175
176         if (strcasecmp(upcall, "NONE") == 0) {
177                 snprintf(upcall, count + 1, "NONE");
178                 goto valid;
179         }
180
181 invalid:
182         OBD_FREE(upcall, count + 1);
183         return -EINVAL;
184
185 valid:
186         down_write(&cache->uc_upcall_rwsem);
187         strcpy(cache->uc_upcall, upcall);
188         up_write(&cache->uc_upcall_rwsem);
189
190         OBD_FREE(upcall, count + 1);
191         return 0;
192 }
193 EXPORT_SYMBOL(upcall_cache_set_upcall);
194
195 static inline int refresh_entry(struct upcall_cache *cache,
196                          struct upcall_cache_entry *entry)
197 {
198         LASSERT(cache->uc_ops->do_upcall);
199         return cache->uc_ops->do_upcall(cache, entry);
200 }
201
202 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
203                                                   __u64 key, void *args)
204 {
205         struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
206         bool failedacquiring = false;
207         struct list_head *head;
208         wait_queue_entry_t wait;
209         bool writelock;
210         int rc, found;
211
212         ENTRY;
213
214         LASSERT(cache);
215
216         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
217                                                         cache->uc_hashsize)];
218 find_again:
219         found = 0;
220         if (new) {
221                 write_lock(&cache->uc_lock);
222                 writelock = true;
223         } else {
224                 read_lock(&cache->uc_lock);
225                 writelock = false;
226         }
227 find_with_lock:
228         list_for_each_entry_safe(entry, next, head, ue_hash) {
229                 /* check invalid & expired items */
230                 if (check_unlink_entry(cache, entry, writelock))
231                         continue;
232                 if (upcall_compare(cache, entry, key, args) == 0) {
233                         found = 1;
234                         break;
235                 }
236         }
237
238         if (!found) {
239                 if (!new) {
240                         if (writelock)
241                                 write_unlock(&cache->uc_lock);
242                         else
243                                 read_unlock(&cache->uc_lock);
244                         new = alloc_entry(cache, key, args);
245                         if (!new) {
246                                 CERROR("%s: fail to alloc entry: rc = %d\n",
247                                        cache->uc_name, -ENOMEM);
248                                 RETURN(ERR_PTR(-ENOMEM));
249                         }
250                         goto find_again;
251                 } else {
252                         list_add(&new->ue_hash, head);
253                         entry = new;
254                 }
255         } else {
256                 if (new) {
257                         free_entry(cache, new);
258                         new = NULL;
259                 } else if (!writelock) {
260                         /* We found an entry while holding the read lock, so
261                          * convert it to a write lock and find again, to check
262                          * that entry was not modified/freed in between.
263                          */
264                         write_lock_from_read(&cache->uc_lock, &writelock);
265                         found = 0;
266                         goto find_with_lock;
267                 }
268                 list_move(&entry->ue_hash, head);
269         }
270         /* now we hold a write lock */
271         get_entry(entry);
272
273         /* acquire for new one */
274         if (UC_CACHE_IS_NEW(entry)) {
275                 UC_CACHE_SET_ACQUIRING(entry);
276                 UC_CACHE_CLEAR_NEW(entry);
277                 write_unlock(&cache->uc_lock);
278                 rc = refresh_entry(cache, entry);
279                 write_lock(&cache->uc_lock);
280                 entry->ue_acquire_expire = ktime_get_seconds() +
281                                            cache->uc_acquire_expire;
282                 if (rc < 0) {
283                         UC_CACHE_CLEAR_ACQUIRING(entry);
284                         UC_CACHE_SET_INVALID(entry);
285                         wake_up(&entry->ue_waitq);
286                         if (unlikely(rc == -EREMCHG)) {
287                                 put_entry(cache, entry);
288                                 GOTO(out, entry = ERR_PTR(rc));
289                         }
290                 }
291         }
292         /* someone (and only one) is doing upcall upon this item,
293          * wait it to complete */
294         if (UC_CACHE_IS_ACQUIRING(entry)) {
295                 long expiry = (entry == new) ?
296                               cfs_time_seconds(cache->uc_acquire_expire) :
297                               MAX_SCHEDULE_TIMEOUT;
298                 long left;
299
300                 init_wait(&wait);
301                 add_wait_queue(&entry->ue_waitq, &wait);
302                 set_current_state(TASK_INTERRUPTIBLE);
303                 write_unlock(&cache->uc_lock);
304
305                 left = schedule_timeout(expiry);
306
307                 write_lock(&cache->uc_lock);
308                 remove_wait_queue(&entry->ue_waitq, &wait);
309                 if (UC_CACHE_IS_ACQUIRING(entry)) {
310                         /* we're interrupted or upcall failed in the middle */
311                         rc = left > 0 ? -EINTR : -ETIMEDOUT;
312                         /* if we waited uc_acquire_expire, we can try again
313                          * with same data, but only if acquire is replayable
314                          */
315                         if (left <= 0 && !cache->uc_acquire_replay)
316                                 failedacquiring = true;
317                         put_entry(cache, entry);
318                         if (!failedacquiring) {
319                                 write_unlock(&cache->uc_lock);
320                                 failedacquiring = true;
321                                 new = NULL;
322                                 CDEBUG(D_OTHER,
323                                        "retry acquire for key %llu (got %d)\n",
324                                        entry->ue_key, rc);
325                                 goto find_again;
326                         }
327                         wake_up_all(&entry->ue_waitq);
328                         CERROR("%s: acquire for key %lld after %llu: rc = %d\n",
329                                cache->uc_name, entry->ue_key,
330                                cache->uc_acquire_expire, rc);
331                         GOTO(out, entry = ERR_PTR(rc));
332                 }
333         }
334
335         /* invalid means error, don't need to try again */
336         if (UC_CACHE_IS_INVALID(entry)) {
337                 put_entry(cache, entry);
338                 GOTO(out, entry = ERR_PTR(-EIDRM));
339         }
340
341         /* check expired
342          * We can't refresh the existing one because some
343          * memory might be shared by multiple processes.
344          */
345         if (check_unlink_entry(cache, entry, writelock)) {
346                 /* if expired, try again. but if this entry is
347                  * created by me but too quickly turn to expired
348                  * without any error, should at least give a
349                  * chance to use it once.
350                  */
351                 if (entry != new) {
352                         /* as stated above, we already hold a write lock */
353                         put_entry(cache, entry);
354                         write_unlock(&cache->uc_lock);
355                         new = NULL;
356                         goto find_again;
357                 }
358         }
359
360         /* Now we know it's good */
361 out:
362         if (writelock)
363                 write_unlock(&cache->uc_lock);
364         else
365                 read_unlock(&cache->uc_lock);
366         RETURN(entry);
367 }
368 EXPORT_SYMBOL(upcall_cache_get_entry);
369
370 void upcall_cache_get_entry_raw(struct upcall_cache_entry *entry)
371 {
372         get_entry(entry);
373 }
374 EXPORT_SYMBOL(upcall_cache_get_entry_raw);
375
376 void upcall_cache_update_entry(struct upcall_cache *cache,
377                                struct upcall_cache_entry *entry,
378                                time64_t expire, int state)
379 {
380         write_lock(&cache->uc_lock);
381         entry->ue_expire = expire;
382         if (!state)
383                 UC_CACHE_SET_VALID(entry);
384         else
385                 entry->ue_flags |= state;
386         write_unlock(&cache->uc_lock);
387 }
388 EXPORT_SYMBOL(upcall_cache_update_entry);
389
390 void upcall_cache_put_entry(struct upcall_cache *cache,
391                             struct upcall_cache_entry *entry)
392 {
393         ENTRY;
394
395         if (!entry) {
396                 EXIT;
397                 return;
398         }
399
400         LASSERT(atomic_read(&entry->ue_refcount) > 0);
401         write_lock(&cache->uc_lock);
402         put_entry(cache, entry);
403         write_unlock(&cache->uc_lock);
404         EXIT;
405 }
406 EXPORT_SYMBOL(upcall_cache_put_entry);
407
408 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
409                           void *args)
410 {
411         struct upcall_cache_entry *entry = NULL;
412         struct list_head *head;
413         int found = 0, rc = 0;
414         bool writelock = false;
415         ENTRY;
416
417         LASSERT(cache);
418
419         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
420                                                         cache->uc_hashsize)];
421
422         read_lock(&cache->uc_lock);
423         list_for_each_entry(entry, head, ue_hash) {
424                 if (downcall_compare(cache, entry, key, args) == 0) {
425                         found = 1;
426                         get_entry(entry);
427                         break;
428                 }
429         }
430
431         if (!found) {
432                 CDEBUG(D_OTHER, "%s: upcall for key %llu not expected\n",
433                        cache->uc_name, key);
434                 /* haven't found, it's possible */
435                 read_unlock(&cache->uc_lock);
436                 RETURN(-EINVAL);
437         }
438
439         if (err) {
440                 CDEBUG(D_OTHER, "%s: upcall for key %llu returned %d\n",
441                        cache->uc_name, entry->ue_key, err);
442                 write_lock_from_read(&cache->uc_lock, &writelock);
443                 GOTO(out, rc = err);
444         }
445
446         if (!UC_CACHE_IS_ACQUIRING(entry)) {
447                 CDEBUG(D_RPCTRACE, "%s: found uptodate entry %p (key %llu)"
448                        "\n", cache->uc_name, entry, entry->ue_key);
449                 write_lock_from_read(&cache->uc_lock, &writelock);
450                 GOTO(out, rc = 0);
451         }
452
453         if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
454                 CERROR("%s: found a stale entry %p (key %llu) in ioctl\n",
455                        cache->uc_name, entry, entry->ue_key);
456                 write_lock_from_read(&cache->uc_lock, &writelock);
457                 GOTO(out, rc = -EINVAL);
458         }
459
460         read_unlock(&cache->uc_lock);
461         if (cache->uc_ops->parse_downcall)
462                 rc = cache->uc_ops->parse_downcall(cache, entry, args);
463         write_lock(&cache->uc_lock);
464         if (rc)
465                 GOTO(out, rc);
466
467         if (!entry->ue_expire)
468                 entry->ue_expire = ktime_get_seconds() + cache->uc_entry_expire;
469         UC_CACHE_SET_VALID(entry);
470         CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key %llu\n",
471                cache->uc_name, entry, entry->ue_key);
472 out:
473         /* 'goto out' needs to make sure to take a write lock first */
474         if (rc) {
475                 UC_CACHE_SET_INVALID(entry);
476                 list_del_init(&entry->ue_hash);
477         }
478         UC_CACHE_CLEAR_ACQUIRING(entry);
479         wake_up(&entry->ue_waitq);
480         put_entry(cache, entry);
481         write_unlock(&cache->uc_lock);
482
483         RETURN(rc);
484 }
485 EXPORT_SYMBOL(upcall_cache_downcall);
486
487 void upcall_cache_flush(struct upcall_cache *cache, int force)
488 {
489         struct upcall_cache_entry *entry, *next;
490         int i;
491         ENTRY;
492
493         write_lock(&cache->uc_lock);
494         for (i = 0; i < cache->uc_hashsize; i++) {
495                 list_for_each_entry_safe(entry, next,
496                                          &cache->uc_hashtable[i], ue_hash) {
497                         if (!force && atomic_read(&entry->ue_refcount)) {
498                                 UC_CACHE_SET_EXPIRED(entry);
499                                 continue;
500                         }
501                         LASSERT(!atomic_read(&entry->ue_refcount));
502                         free_entry(cache, entry);
503                 }
504         }
505         write_unlock(&cache->uc_lock);
506         EXIT;
507 }
508 EXPORT_SYMBOL(upcall_cache_flush);
509
510 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
511 {
512         struct list_head *head;
513         struct upcall_cache_entry *entry;
514         int found = 0;
515         ENTRY;
516
517         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key,
518                                                         cache->uc_hashsize)];
519
520         write_lock(&cache->uc_lock);
521         list_for_each_entry(entry, head, ue_hash) {
522                 if (upcall_compare(cache, entry, key, args) == 0) {
523                         found = 1;
524                         break;
525                 }
526         }
527
528         if (found) {
529                 CWARN("%s: flush entry %p: key %llu, ref %d, fl %x, "
530                       "cur %lld, ex %lld/%lld\n",
531                       cache->uc_name, entry, entry->ue_key,
532                       atomic_read(&entry->ue_refcount), entry->ue_flags,
533                       ktime_get_real_seconds(), entry->ue_acquire_expire,
534                       entry->ue_expire);
535                 get_entry(entry);
536                 UC_CACHE_SET_EXPIRED(entry);
537                 put_entry(cache, entry);
538         }
539         write_unlock(&cache->uc_lock);
540 }
541 EXPORT_SYMBOL(upcall_cache_flush_one);
542
543 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
544                                        int hashsz, time64_t entry_expire,
545                                        time64_t acquire_expire, bool replayable,
546                                        struct upcall_cache_ops *ops)
547 {
548         struct upcall_cache *cache;
549         int i;
550         ENTRY;
551
552         LIBCFS_ALLOC(cache, sizeof(*cache));
553         if (!cache)
554                 RETURN(ERR_PTR(-ENOMEM));
555
556         rwlock_init(&cache->uc_lock);
557         init_rwsem(&cache->uc_upcall_rwsem);
558         cache->uc_hashsize = hashsz;
559         LIBCFS_ALLOC(cache->uc_hashtable,
560                      sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
561         if (!cache->uc_hashtable)
562                 RETURN(ERR_PTR(-ENOMEM));
563         for (i = 0; i < cache->uc_hashsize; i++)
564                 INIT_LIST_HEAD(&cache->uc_hashtable[i]);
565         strlcpy(cache->uc_name, name, sizeof(cache->uc_name));
566         /* upcall pathname proc tunable */
567         strlcpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall));
568         cache->uc_entry_expire = entry_expire;
569         cache->uc_acquire_expire = acquire_expire;
570         cache->uc_acquire_replay = replayable;
571         cache->uc_ops = ops;
572
573         RETURN(cache);
574 }
575 EXPORT_SYMBOL(upcall_cache_init);
576
577 void upcall_cache_cleanup(struct upcall_cache *cache)
578 {
579         if (!cache)
580                 return;
581         upcall_cache_flush_all(cache);
582         LIBCFS_FREE(cache->uc_hashtable,
583                     sizeof(*cache->uc_hashtable) * cache->uc_hashsize);
584         LIBCFS_FREE(cache, sizeof(*cache));
585 }
586 EXPORT_SYMBOL(upcall_cache_cleanup);