Whamcloud - gitweb
LU-5092 nodemap: save nodemaps to targets for caching
[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, 2014, 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 #include <linux/kthread.h>
44 #include <libcfs/libcfs.h>
45
46 #include <obd_support.h>
47 #include <obd_class.h>
48 #include <lustre_net.h>
49 #include <lustre_sec.h>
50
51 #include "ptlrpc_internal.h"
52
53 #define SEC_GC_INTERVAL (30 * 60)
54
55
56 static struct mutex sec_gc_mutex;
57 static spinlock_t sec_gc_list_lock;
58 static struct list_head sec_gc_list;
59
60 static spinlock_t sec_gc_ctx_list_lock;
61 static struct list_head sec_gc_ctx_list;
62
63 static struct ptlrpc_thread sec_gc_thread;
64 static atomic_t sec_gc_wait_del = ATOMIC_INIT(0);
65
66
67 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
68 {
69         LASSERT(sec->ps_policy->sp_cops->gc_ctx);
70         LASSERT(sec->ps_gc_interval > 0);
71         LASSERT(list_empty(&sec->ps_gc_list));
72
73         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
74
75         spin_lock(&sec_gc_list_lock);
76         list_add_tail(&sec->ps_gc_list, &sec_gc_list);
77         spin_unlock(&sec_gc_list_lock);
78
79         CDEBUG(D_SEC, "added sec %p(%s)\n", sec, sec->ps_policy->sp_name);
80 }
81
82 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
83 {
84         if (list_empty(&sec->ps_gc_list))
85                 return;
86
87         might_sleep();
88
89         /* signal before list_del to make iteration in gc thread safe */
90         atomic_inc(&sec_gc_wait_del);
91
92         spin_lock(&sec_gc_list_lock);
93         list_del_init(&sec->ps_gc_list);
94         spin_unlock(&sec_gc_list_lock);
95
96         /* barrier */
97         mutex_lock(&sec_gc_mutex);
98         mutex_unlock(&sec_gc_mutex);
99
100         atomic_dec(&sec_gc_wait_del);
101
102         CDEBUG(D_SEC, "del sec %p(%s)\n", sec, sec->ps_policy->sp_name);
103 }
104
105 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx)
106 {
107         LASSERT(list_empty(&ctx->cc_gc_chain));
108
109         CDEBUG(D_SEC, "hand over ctx %p(%u->%s)\n",
110                ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
111         spin_lock(&sec_gc_ctx_list_lock);
112         list_add(&ctx->cc_gc_chain, &sec_gc_ctx_list);
113         spin_unlock(&sec_gc_ctx_list_lock);
114
115         thread_add_flags(&sec_gc_thread, SVC_SIGNAL);
116         wake_up(&sec_gc_thread.t_ctl_waitq);
117 }
118 EXPORT_SYMBOL(sptlrpc_gc_add_ctx);
119
120 static void sec_process_ctx_list(void)
121 {
122         struct ptlrpc_cli_ctx *ctx;
123
124         spin_lock(&sec_gc_ctx_list_lock);
125
126         while (!list_empty(&sec_gc_ctx_list)) {
127                 ctx = list_entry(sec_gc_ctx_list.next,
128                                      struct ptlrpc_cli_ctx, cc_gc_chain);
129                 list_del_init(&ctx->cc_gc_chain);
130                 spin_unlock(&sec_gc_ctx_list_lock);
131
132                 LASSERT(ctx->cc_sec);
133                 LASSERT(atomic_read(&ctx->cc_refcount) == 1);
134                 CDEBUG(D_SEC, "gc pick up ctx %p(%u->%s)\n",
135                        ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
136                 sptlrpc_cli_ctx_put(ctx, 1);
137
138                 spin_lock(&sec_gc_ctx_list_lock);
139         }
140
141         spin_unlock(&sec_gc_ctx_list_lock);
142 }
143
144 static void sec_do_gc(struct ptlrpc_sec *sec)
145 {
146         LASSERT(sec->ps_policy->sp_cops->gc_ctx);
147
148         if (unlikely(sec->ps_gc_next == 0)) {
149                 CDEBUG(D_SEC, "sec %p(%s) has 0 gc time\n",
150                       sec, sec->ps_policy->sp_name);
151                 return;
152         }
153
154         CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
155
156         if (cfs_time_after(sec->ps_gc_next, cfs_time_current_sec()))
157                 return;
158
159         sec->ps_policy->sp_cops->gc_ctx(sec);
160         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
161 }
162
163 static int sec_gc_main(void *arg)
164 {
165         struct ptlrpc_thread *thread = (struct ptlrpc_thread *) arg;
166         struct l_wait_info    lwi;
167
168         unshare_fs_struct();
169
170         /* Record that the thread is running */
171         thread_set_flags(thread, SVC_RUNNING);
172         wake_up(&thread->t_ctl_waitq);
173
174         while (1) {
175                 struct ptlrpc_sec *sec;
176
177                 thread_clear_flags(thread, SVC_SIGNAL);
178                 sec_process_ctx_list();
179 again:
180                 /* go through sec list do gc.
181                  * FIXME here we iterate through the whole list each time which
182                  * is not optimal. we perhaps want to use balanced binary tree
183                  * to trace each sec as order of expiry time.
184                  * another issue here is we wakeup as fixed interval instead of
185                  * according to each sec's expiry time */
186                 mutex_lock(&sec_gc_mutex);
187                 list_for_each_entry(sec, &sec_gc_list, ps_gc_list) {
188                         /* if someone is waiting to be deleted, let it
189                          * proceed as soon as possible. */
190                         if (atomic_read(&sec_gc_wait_del)) {
191                                 CDEBUG(D_SEC, "deletion pending, start over\n");
192                                 mutex_unlock(&sec_gc_mutex);
193                                 goto again;
194                         }
195
196                         sec_do_gc(sec);
197                 }
198                 mutex_unlock(&sec_gc_mutex);
199
200                 /* check ctx list again before sleep */
201                 sec_process_ctx_list();
202
203                 lwi = LWI_TIMEOUT(msecs_to_jiffies(SEC_GC_INTERVAL *
204                                                    MSEC_PER_SEC),
205                                   NULL, NULL);
206                 l_wait_event(thread->t_ctl_waitq,
207                              thread_is_stopping(thread) ||
208                              thread_is_signal(thread),
209                              &lwi);
210
211                 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
212                         break;
213         }
214
215         thread_set_flags(thread, SVC_STOPPED);
216         wake_up(&thread->t_ctl_waitq);
217         return 0;
218 }
219
220 int sptlrpc_gc_init(void)
221 {
222         struct l_wait_info lwi = { 0 };
223         struct task_struct *task;
224
225         mutex_init(&sec_gc_mutex);
226         spin_lock_init(&sec_gc_list_lock);
227         spin_lock_init(&sec_gc_ctx_list_lock);
228
229         INIT_LIST_HEAD(&sec_gc_list);
230         INIT_LIST_HEAD(&sec_gc_ctx_list);
231
232         /* initialize thread control */
233         memset(&sec_gc_thread, 0, sizeof(sec_gc_thread));
234         init_waitqueue_head(&sec_gc_thread.t_ctl_waitq);
235
236         task = kthread_run(sec_gc_main, &sec_gc_thread, "sptlrpc_gc");
237         if (IS_ERR(task)) {
238                 CERROR("can't start gc thread: %ld\n", PTR_ERR(task));
239                 return PTR_ERR(task);
240         }
241
242         l_wait_event(sec_gc_thread.t_ctl_waitq,
243                      thread_is_running(&sec_gc_thread), &lwi);
244         return 0;
245 }
246
247 void sptlrpc_gc_fini(void)
248 {
249         struct l_wait_info lwi = { 0 };
250
251         thread_set_flags(&sec_gc_thread, SVC_STOPPING);
252         wake_up(&sec_gc_thread.t_ctl_waitq);
253
254         l_wait_event(sec_gc_thread.t_ctl_waitq,
255                      thread_is_stopped(&sec_gc_thread), &lwi);
256 }