Whamcloud - gitweb
b=22766 cascading_rw: take lmm_stripe_count returned by ioctl(LL_IOC_LOV_SETSTRIPE)
[fs/lustre-release.git] / lustre / lvfs / upcall_cache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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/lvfs/upcall_cache.c
37  *
38  * Supplementary groups cache.
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifndef AUTOCONF_INCLUDED
44 #include <linux/config.h>
45 #endif
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/mm.h>
49 #include <linux/kmod.h>
50 #include <linux/string.h>
51 #include <linux/stat.h>
52 #include <linux/errno.h>
53 #include <linux/version.h>
54 #include <linux/unistd.h>
55
56 #include <asm/system.h>
57 #include <asm/uaccess.h>
58
59 #include <linux/fs.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/slab.h>
63
64 #include <obd_support.h>
65 #include <lustre_lib.h>
66
67 static struct upcall_cache_entry *alloc_entry(struct upcall_cache *cache,
68                                               __u64 key, void *args)
69 {
70         struct upcall_cache_entry *entry;
71
72         OBD_ALLOC_PTR(entry);
73         if (!entry)
74                 return NULL;
75
76         UC_CACHE_SET_NEW(entry);
77         CFS_INIT_LIST_HEAD(&entry->ue_hash);
78         entry->ue_key = key;
79         cfs_atomic_set(&entry->ue_refcount, 0);
80         cfs_waitq_init(&entry->ue_waitq);
81         if (cache->uc_ops->init_entry)
82                 cache->uc_ops->init_entry(entry, args);
83         return entry;
84 }
85
86 /* protected by cache lock */
87 static void free_entry(struct upcall_cache *cache,
88                        struct upcall_cache_entry *entry)
89 {
90         if (cache->uc_ops->free_entry)
91                 cache->uc_ops->free_entry(cache, entry);
92
93         cfs_list_del(&entry->ue_hash);
94         CDEBUG(D_OTHER, "destroy cache entry %p for key "LPU64"\n",
95                entry, entry->ue_key);
96         OBD_FREE_PTR(entry);
97 }
98
99 static inline int upcall_compare(struct upcall_cache *cache,
100                                  struct upcall_cache_entry *entry,
101                                  __u64 key, void *args)
102 {
103         if (entry->ue_key != key)
104                 return -1;
105
106         if (cache->uc_ops->upcall_compare)
107                 return cache->uc_ops->upcall_compare(cache, entry, key, args);
108
109         return 0;
110 }
111
112 static inline int downcall_compare(struct upcall_cache *cache,
113                                    struct upcall_cache_entry *entry,
114                                    __u64 key, void *args)
115 {
116         if (entry->ue_key != key)
117                 return -1;
118
119         if (cache->uc_ops->downcall_compare)
120                 return cache->uc_ops->downcall_compare(cache, entry, key, args);
121
122         return 0;
123 }
124
125 static inline void get_entry(struct upcall_cache_entry *entry)
126 {
127         cfs_atomic_inc(&entry->ue_refcount);
128 }
129
130 static inline void put_entry(struct upcall_cache *cache,
131                              struct upcall_cache_entry *entry)
132 {
133         if (cfs_atomic_dec_and_test(&entry->ue_refcount) &&
134             (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry))) {
135                 free_entry(cache, entry);
136         }
137 }
138
139 static int check_unlink_entry(struct upcall_cache *cache,
140                               struct upcall_cache_entry *entry)
141 {
142         if (UC_CACHE_IS_VALID(entry) &&
143             cfs_time_before(jiffies, entry->ue_expire))
144                 return 0;
145
146         if (UC_CACHE_IS_ACQUIRING(entry)) {
147                 if (entry->ue_acquire_expire == 0 ||
148                     cfs_time_before(jiffies, entry->ue_acquire_expire))
149                         return 0;
150
151                 UC_CACHE_SET_EXPIRED(entry);
152                 cfs_waitq_broadcast(&entry->ue_waitq);
153         } else if (!UC_CACHE_IS_INVALID(entry)) {
154                 UC_CACHE_SET_EXPIRED(entry);
155         }
156
157         cfs_list_del_init(&entry->ue_hash);
158         if (!cfs_atomic_read(&entry->ue_refcount))
159                 free_entry(cache, entry);
160         return 1;
161 }
162
163 static inline int refresh_entry(struct upcall_cache *cache,
164                          struct upcall_cache_entry *entry)
165 {
166         LASSERT(cache->uc_ops->do_upcall);
167         return cache->uc_ops->do_upcall(cache, entry);
168 }
169
170 struct upcall_cache_entry *upcall_cache_get_entry(struct upcall_cache *cache,
171                                                   __u64 key, void *args)
172 {
173         struct upcall_cache_entry *entry = NULL, *new = NULL, *next;
174         cfs_list_t *head;
175         cfs_waitlink_t wait;
176         int rc, found;
177         ENTRY;
178
179         LASSERT(cache);
180
181         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
182 find_again:
183         found = 0;
184         cfs_spin_lock(&cache->uc_lock);
185         cfs_list_for_each_entry_safe(entry, next, head, ue_hash) {
186                 /* check invalid & expired items */
187                 if (check_unlink_entry(cache, entry))
188                         continue;
189                 if (upcall_compare(cache, entry, key, args) == 0) {
190                         found = 1;
191                         break;
192                 }
193         }
194
195         if (!found) {
196                 if (!new) {
197                         cfs_spin_unlock(&cache->uc_lock);
198                         new = alloc_entry(cache, key, args);
199                         if (!new) {
200                                 CERROR("fail to alloc entry\n");
201                                 RETURN(ERR_PTR(-ENOMEM));
202                         }
203                         goto find_again;
204                 } else {
205                         cfs_list_add(&new->ue_hash, head);
206                         entry = new;
207                 }
208         } else {
209                 if (new) {
210                         free_entry(cache, new);
211                         new = NULL;
212                 }
213                 cfs_list_move(&entry->ue_hash, head);
214         }
215         get_entry(entry);
216
217         /* acquire for new one */
218         if (UC_CACHE_IS_NEW(entry)) {
219                 UC_CACHE_SET_ACQUIRING(entry);
220                 UC_CACHE_CLEAR_NEW(entry);
221                 cfs_spin_unlock(&cache->uc_lock);
222                 rc = refresh_entry(cache, entry);
223                 cfs_spin_lock(&cache->uc_lock);
224                 entry->ue_acquire_expire = jiffies + cache->uc_acquire_expire;
225                 if (rc < 0) {
226                         UC_CACHE_CLEAR_ACQUIRING(entry);
227                         UC_CACHE_SET_INVALID(entry);
228                         cfs_waitq_broadcast(&entry->ue_waitq);
229                         if (unlikely(rc == -EREMCHG)) {
230                                 put_entry(cache, entry);
231                                 GOTO(out, entry = ERR_PTR(rc));
232                         }
233                 }
234         }
235
236         /* someone (and only one) is doing upcall upon this item,
237          * wait it to complete */
238         if (UC_CACHE_IS_ACQUIRING(entry)) {
239                 long expiry = (entry == new) ? cache->uc_acquire_expire :
240                                                CFS_MAX_SCHEDULE_TIMEOUT;
241                 long left;
242
243                 cfs_waitlink_init(&wait);
244                 cfs_waitq_add(&entry->ue_waitq, &wait);
245                 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
246                 cfs_spin_unlock(&cache->uc_lock);
247
248                 left = cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE,
249                                            expiry);
250
251                 cfs_spin_lock(&cache->uc_lock);
252                 cfs_waitq_del(&entry->ue_waitq, &wait);
253                 if (UC_CACHE_IS_ACQUIRING(entry)) {
254                         /* we're interrupted or upcall failed in the middle */
255                         rc = left > 0 ? -EINTR : -ETIMEDOUT;
256                         CERROR("acquire for key "LPU64": error %d\n",
257                                entry->ue_key, rc);
258                         put_entry(cache, entry);
259                         GOTO(out, entry = ERR_PTR(rc));
260                 }
261         }
262
263         /* invalid means error, don't need to try again */
264         if (UC_CACHE_IS_INVALID(entry)) {
265                 put_entry(cache, entry);
266                 GOTO(out, entry = ERR_PTR(-EIDRM));
267         }
268
269         /* check expired
270          * We can't refresh the existing one because some
271          * memory might be shared by multiple processes.
272          */
273         if (check_unlink_entry(cache, entry)) {
274                 /* if expired, try again. but if this entry is
275                  * created by me but too quickly turn to expired
276                  * without any error, should at least give a
277                  * chance to use it once.
278                  */
279                 if (entry != new) {
280                         put_entry(cache, entry);
281                         cfs_spin_unlock(&cache->uc_lock);
282                         new = NULL;
283                         goto find_again;
284                 }
285         }
286
287         /* Now we know it's good */
288 out:
289         cfs_spin_unlock(&cache->uc_lock);
290         RETURN(entry);
291 }
292 EXPORT_SYMBOL(upcall_cache_get_entry);
293
294 void upcall_cache_put_entry(struct upcall_cache *cache,
295                             struct upcall_cache_entry *entry)
296 {
297         ENTRY;
298
299         if (!entry) {
300                 EXIT;
301                 return;
302         }
303
304         LASSERT(cfs_atomic_read(&entry->ue_refcount) > 0);
305         cfs_spin_lock(&cache->uc_lock);
306         put_entry(cache, entry);
307         cfs_spin_unlock(&cache->uc_lock);
308         EXIT;
309 }
310 EXPORT_SYMBOL(upcall_cache_put_entry);
311
312 int upcall_cache_downcall(struct upcall_cache *cache, __u32 err, __u64 key,
313                           void *args)
314 {
315         struct upcall_cache_entry *entry = NULL;
316         cfs_list_t *head;
317         int found = 0, rc = 0;
318         ENTRY;
319
320         LASSERT(cache);
321
322         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
323
324         cfs_spin_lock(&cache->uc_lock);
325         cfs_list_for_each_entry(entry, head, ue_hash) {
326                 if (downcall_compare(cache, entry, key, args) == 0) {
327                         found = 1;
328                         get_entry(entry);
329                         break;
330                 }
331         }
332
333         if (!found) {
334                 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" not expected\n",
335                        cache->uc_name, key);
336                 /* haven't found, it's possible */
337                 cfs_spin_unlock(&cache->uc_lock);
338                 RETURN(-EINVAL);
339         }
340
341         if (err) {
342                 CDEBUG(D_OTHER, "%s: upcall for key "LPU64" returned %d\n",
343                        cache->uc_name, entry->ue_key, err);
344                 GOTO(out, rc = -EINVAL);
345         }
346
347         if (!UC_CACHE_IS_ACQUIRING(entry)) {
348                 CDEBUG(D_RPCTRACE,"%s: found uptodate entry %p (key "LPU64")\n",
349                        cache->uc_name, entry, entry->ue_key);
350                 GOTO(out, rc = 0);
351         }
352
353         if (UC_CACHE_IS_INVALID(entry) || UC_CACHE_IS_EXPIRED(entry)) {
354                 CERROR("%s: found a stale entry %p (key "LPU64") in ioctl\n",
355                        cache->uc_name, entry, entry->ue_key);
356                 GOTO(out, rc = -EINVAL);
357         }
358
359         cfs_spin_unlock(&cache->uc_lock);
360         if (cache->uc_ops->parse_downcall)
361                 rc = cache->uc_ops->parse_downcall(cache, entry, args);
362         cfs_spin_lock(&cache->uc_lock);
363         if (rc)
364                 GOTO(out, rc);
365
366         entry->ue_expire = jiffies + cache->uc_entry_expire;
367         UC_CACHE_SET_VALID(entry);
368         CDEBUG(D_OTHER, "%s: created upcall cache entry %p for key "LPU64"\n",
369                cache->uc_name, entry, entry->ue_key);
370 out:
371         if (rc) {
372                 UC_CACHE_SET_INVALID(entry);
373                 cfs_list_del_init(&entry->ue_hash);
374         }
375         UC_CACHE_CLEAR_ACQUIRING(entry);
376         cfs_spin_unlock(&cache->uc_lock);
377         cfs_waitq_broadcast(&entry->ue_waitq);
378         put_entry(cache, entry);
379
380         RETURN(rc);
381 }
382 EXPORT_SYMBOL(upcall_cache_downcall);
383
384 static void cache_flush(struct upcall_cache *cache, int force)
385 {
386         struct upcall_cache_entry *entry, *next;
387         int i;
388         ENTRY;
389
390         cfs_spin_lock(&cache->uc_lock);
391         for (i = 0; i < UC_CACHE_HASH_SIZE; i++) {
392                 cfs_list_for_each_entry_safe(entry, next,
393                                          &cache->uc_hashtable[i], ue_hash) {
394                         if (!force && cfs_atomic_read(&entry->ue_refcount)) {
395                                 UC_CACHE_SET_EXPIRED(entry);
396                                 continue;
397                         }
398                         LASSERT(!cfs_atomic_read(&entry->ue_refcount));
399                         free_entry(cache, entry);
400                 }
401         }
402         cfs_spin_unlock(&cache->uc_lock);
403         EXIT;
404 }
405
406 void upcall_cache_flush_idle(struct upcall_cache *cache)
407 {
408         cache_flush(cache, 0);
409 }
410 EXPORT_SYMBOL(upcall_cache_flush_idle);
411
412 void upcall_cache_flush_all(struct upcall_cache *cache)
413 {
414         cache_flush(cache, 1);
415 }
416 EXPORT_SYMBOL(upcall_cache_flush_all);
417
418 void upcall_cache_flush_one(struct upcall_cache *cache, __u64 key, void *args)
419 {
420         cfs_list_t *head;
421         struct upcall_cache_entry *entry;
422         int found = 0;
423         ENTRY;
424
425         head = &cache->uc_hashtable[UC_CACHE_HASH_INDEX(key)];
426
427         cfs_spin_lock(&cache->uc_lock);
428         cfs_list_for_each_entry(entry, head, ue_hash) {
429                 if (upcall_compare(cache, entry, key, args) == 0) {
430                         found = 1;
431                         break;
432                 }
433         }
434
435         if (found) {
436                 CWARN("%s: flush entry %p: key "LPU64", ref %d, fl %x, "
437                       "cur %lu, ex %ld/%ld\n",
438                       cache->uc_name, entry, entry->ue_key,
439                       cfs_atomic_read(&entry->ue_refcount), entry->ue_flags,
440                       get_seconds(), entry->ue_acquire_expire,
441                       entry->ue_expire);
442                 UC_CACHE_SET_EXPIRED(entry);
443                 if (!cfs_atomic_read(&entry->ue_refcount))
444                         free_entry(cache, entry);
445         }
446         cfs_spin_unlock(&cache->uc_lock);
447 }
448 EXPORT_SYMBOL(upcall_cache_flush_one);
449
450 struct upcall_cache *upcall_cache_init(const char *name, const char *upcall,
451                                        struct upcall_cache_ops *ops)
452 {
453         struct upcall_cache *cache;
454         int i;
455         ENTRY;
456
457         OBD_ALLOC(cache, sizeof(*cache));
458         if (!cache)
459                 RETURN(ERR_PTR(-ENOMEM));
460
461         cfs_spin_lock_init(&cache->uc_lock);
462         cfs_rwlock_init(&cache->uc_upcall_rwlock);
463         for (i = 0; i < UC_CACHE_HASH_SIZE; i++)
464                 CFS_INIT_LIST_HEAD(&cache->uc_hashtable[i]);
465         strncpy(cache->uc_name, name, sizeof(cache->uc_name) - 1);
466         /* upcall pathname proc tunable */
467         strncpy(cache->uc_upcall, upcall, sizeof(cache->uc_upcall) - 1);
468         cache->uc_entry_expire = 20 * 60 * CFS_HZ;
469         cache->uc_acquire_expire = 30 * CFS_HZ;
470         cache->uc_ops = ops;
471
472         RETURN(cache);
473 }
474 EXPORT_SYMBOL(upcall_cache_init);
475
476 void upcall_cache_cleanup(struct upcall_cache *cache)
477 {
478         if (!cache)
479                 return;
480         upcall_cache_flush_all(cache);
481         OBD_FREE(cache, sizeof(*cache));
482 }
483 EXPORT_SYMBOL(upcall_cache_cleanup);