Whamcloud - gitweb
LU-1346 libcfs: cleanup libcfs primitive (linux-prim.h)
[fs/lustre-release.git] / lustre / ptlrpc / sec_gc.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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/ptlrpc/sec_gc.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #else
46 #include <libcfs/libcfs.h>
47 #endif
48
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include <lustre_net.h>
52 #include <lustre_sec.h>
53
54 #define SEC_GC_INTERVAL (30 * 60)
55
56 #ifdef __KERNEL__
57
58 static struct mutex sec_gc_mutex;
59 static CFS_LIST_HEAD(sec_gc_list);
60 static spinlock_t sec_gc_list_lock;
61
62 static CFS_LIST_HEAD(sec_gc_ctx_list);
63 static spinlock_t sec_gc_ctx_list_lock;
64
65 static struct ptlrpc_thread sec_gc_thread;
66 static cfs_atomic_t sec_gc_wait_del = CFS_ATOMIC_INIT(0);
67
68
69 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
70 {
71         LASSERT(sec->ps_policy->sp_cops->gc_ctx);
72         LASSERT(sec->ps_gc_interval > 0);
73         LASSERT(cfs_list_empty(&sec->ps_gc_list));
74
75         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
76
77         spin_lock(&sec_gc_list_lock);
78         cfs_list_add_tail(&sec_gc_list, &sec->ps_gc_list);
79         spin_unlock(&sec_gc_list_lock);
80
81         CDEBUG(D_SEC, "added sec %p(%s)\n", sec, sec->ps_policy->sp_name);
82 }
83 EXPORT_SYMBOL(sptlrpc_gc_add_sec);
84
85 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
86 {
87         if (cfs_list_empty(&sec->ps_gc_list))
88                 return;
89
90         cfs_might_sleep();
91
92         /* signal before list_del to make iteration in gc thread safe */
93         cfs_atomic_inc(&sec_gc_wait_del);
94
95         spin_lock(&sec_gc_list_lock);
96         cfs_list_del_init(&sec->ps_gc_list);
97         spin_unlock(&sec_gc_list_lock);
98
99         /* barrier */
100         mutex_lock(&sec_gc_mutex);
101         mutex_unlock(&sec_gc_mutex);
102
103         cfs_atomic_dec(&sec_gc_wait_del);
104
105         CDEBUG(D_SEC, "del sec %p(%s)\n", sec, sec->ps_policy->sp_name);
106 }
107 EXPORT_SYMBOL(sptlrpc_gc_del_sec);
108
109 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx)
110 {
111         LASSERT(cfs_list_empty(&ctx->cc_gc_chain));
112
113         CDEBUG(D_SEC, "hand over ctx %p(%u->%s)\n",
114                ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
115         spin_lock(&sec_gc_ctx_list_lock);
116         cfs_list_add(&ctx->cc_gc_chain, &sec_gc_ctx_list);
117         spin_unlock(&sec_gc_ctx_list_lock);
118
119         thread_add_flags(&sec_gc_thread, SVC_SIGNAL);
120         wake_up(&sec_gc_thread.t_ctl_waitq);
121 }
122 EXPORT_SYMBOL(sptlrpc_gc_add_ctx);
123
124 static void sec_process_ctx_list(void)
125 {
126         struct ptlrpc_cli_ctx *ctx;
127
128         spin_lock(&sec_gc_ctx_list_lock);
129
130         while (!cfs_list_empty(&sec_gc_ctx_list)) {
131                 ctx = cfs_list_entry(sec_gc_ctx_list.next,
132                                      struct ptlrpc_cli_ctx, cc_gc_chain);
133                 cfs_list_del_init(&ctx->cc_gc_chain);
134                 spin_unlock(&sec_gc_ctx_list_lock);
135
136                 LASSERT(ctx->cc_sec);
137                 LASSERT(cfs_atomic_read(&ctx->cc_refcount) == 1);
138                 CDEBUG(D_SEC, "gc pick up ctx %p(%u->%s)\n",
139                        ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
140                 sptlrpc_cli_ctx_put(ctx, 1);
141
142                 spin_lock(&sec_gc_ctx_list_lock);
143         }
144
145         spin_unlock(&sec_gc_ctx_list_lock);
146 }
147
148 static void sec_do_gc(struct ptlrpc_sec *sec)
149 {
150         LASSERT(sec->ps_policy->sp_cops->gc_ctx);
151
152         if (unlikely(sec->ps_gc_next == 0)) {
153                 CDEBUG(D_SEC, "sec %p(%s) has 0 gc time\n",
154                       sec, sec->ps_policy->sp_name);
155                 return;
156         }
157
158         CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
159
160         if (cfs_time_after(sec->ps_gc_next, cfs_time_current_sec()))
161                 return;
162
163         sec->ps_policy->sp_cops->gc_ctx(sec);
164         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
165 }
166
167 static int sec_gc_main(void *arg)
168 {
169         struct ptlrpc_thread *thread = (struct ptlrpc_thread *) arg;
170         struct l_wait_info    lwi;
171
172         unshare_fs_struct();
173
174         /* Record that the thread is running */
175         thread_set_flags(thread, SVC_RUNNING);
176         wake_up(&thread->t_ctl_waitq);
177
178         while (1) {
179                 struct ptlrpc_sec *sec;
180
181                 thread_clear_flags(thread, SVC_SIGNAL);
182                 sec_process_ctx_list();
183 again:
184                 /* go through sec list do gc.
185                  * FIXME here we iterate through the whole list each time which
186                  * is not optimal. we perhaps want to use balanced binary tree
187                  * to trace each sec as order of expiry time.
188                  * another issue here is we wakeup as fixed interval instead of
189                  * according to each sec's expiry time */
190                 mutex_lock(&sec_gc_mutex);
191                 cfs_list_for_each_entry(sec, &sec_gc_list, ps_gc_list) {
192                         /* if someone is waiting to be deleted, let it
193                          * proceed as soon as possible. */
194                         if (cfs_atomic_read(&sec_gc_wait_del)) {
195                                 CDEBUG(D_SEC, "deletion pending, start over\n");
196                                 mutex_unlock(&sec_gc_mutex);
197                                 goto again;
198                         }
199
200                         sec_do_gc(sec);
201                 }
202                 mutex_unlock(&sec_gc_mutex);
203
204                 /* check ctx list again before sleep */
205                 sec_process_ctx_list();
206
207                 lwi = LWI_TIMEOUT(SEC_GC_INTERVAL * HZ, NULL, NULL);
208                 l_wait_event(thread->t_ctl_waitq,
209                              thread_is_stopping(thread) ||
210                              thread_is_signal(thread),
211                              &lwi);
212
213                 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
214                         break;
215         }
216
217         thread_set_flags(thread, SVC_STOPPED);
218         wake_up(&thread->t_ctl_waitq);
219         return 0;
220 }
221
222 int sptlrpc_gc_init(void)
223 {
224         struct l_wait_info lwi = { 0 };
225         struct task_struct *task;
226
227         mutex_init(&sec_gc_mutex);
228         spin_lock_init(&sec_gc_list_lock);
229         spin_lock_init(&sec_gc_ctx_list_lock);
230
231         /* initialize thread control */
232         memset(&sec_gc_thread, 0, sizeof(sec_gc_thread));
233         init_waitqueue_head(&sec_gc_thread.t_ctl_waitq);
234
235         task = kthread_run(sec_gc_main, &sec_gc_thread, "sptlrpc_gc");
236         if (IS_ERR(task)) {
237                 CERROR("can't start gc thread: %ld\n", PTR_ERR(task));
238                 return PTR_ERR(task);
239         }
240
241         l_wait_event(sec_gc_thread.t_ctl_waitq,
242                      thread_is_running(&sec_gc_thread), &lwi);
243         return 0;
244 }
245
246 void sptlrpc_gc_fini(void)
247 {
248         struct l_wait_info lwi = { 0 };
249
250         thread_set_flags(&sec_gc_thread, SVC_STOPPING);
251         wake_up(&sec_gc_thread.t_ctl_waitq);
252
253         l_wait_event(sec_gc_thread.t_ctl_waitq,
254                      thread_is_stopped(&sec_gc_thread), &lwi);
255 }
256
257 #else /* !__KERNEL__ */
258
259 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
260 {
261 }
262 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
263 {
264 }
265 int sptlrpc_gc_init(void)
266 {
267         return 0;
268 }
269 void sptlrpc_gc_fini(void)
270 {
271 }
272
273 #endif /* __KERNEL__ */