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