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