Whamcloud - gitweb
LU-8602 gss: get rid of cfs_crypto_hash_desc
[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/workqueue.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 static struct mutex sec_gc_mutex;
52 static spinlock_t sec_gc_list_lock;
53 static struct list_head sec_gc_list;
54
55 static spinlock_t sec_gc_ctx_list_lock;
56 static struct list_head sec_gc_ctx_list;
57
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->ps_gc_list, &sec_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_gc_main(struct work_struct *ws);
99 static DECLARE_DELAYED_WORK(sec_gc_work, sec_gc_main);
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         mod_delayed_work(system_wq, &sec_gc_work, 0);
112 }
113 EXPORT_SYMBOL(sptlrpc_gc_add_ctx);
114
115 static void sec_process_ctx_list(void)
116 {
117         struct ptlrpc_cli_ctx *ctx;
118
119         spin_lock(&sec_gc_ctx_list_lock);
120
121         while (!list_empty(&sec_gc_ctx_list)) {
122                 ctx = list_entry(sec_gc_ctx_list.next,
123                                      struct ptlrpc_cli_ctx, cc_gc_chain);
124                 list_del_init(&ctx->cc_gc_chain);
125                 spin_unlock(&sec_gc_ctx_list_lock);
126
127                 LASSERT(ctx->cc_sec);
128                 LASSERT(atomic_read(&ctx->cc_refcount) == 1);
129                 CDEBUG(D_SEC, "gc pick up ctx %p(%u->%s)\n",
130                        ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
131                 sptlrpc_cli_ctx_put(ctx, 1);
132
133                 spin_lock(&sec_gc_ctx_list_lock);
134         }
135
136         spin_unlock(&sec_gc_ctx_list_lock);
137 }
138
139 static void sec_do_gc(struct ptlrpc_sec *sec)
140 {
141         LASSERT(sec->ps_policy->sp_cops->gc_ctx);
142
143         if (unlikely(sec->ps_gc_next == 0)) {
144                 CDEBUG(D_SEC, "sec %p(%s) has 0 gc time\n",
145                       sec, sec->ps_policy->sp_name);
146                 return;
147         }
148
149         CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
150
151         if (sec->ps_gc_next > ktime_get_real_seconds())
152                 return;
153
154         sec->ps_policy->sp_cops->gc_ctx(sec);
155         sec->ps_gc_next = ktime_get_real_seconds() + sec->ps_gc_interval;
156 }
157
158 static void sec_gc_main(struct work_struct *ws)
159 {
160         struct ptlrpc_sec *sec;
161
162         sec_process_ctx_list();
163 again:
164         /* go through sec list do gc.
165          * FIXME here we iterate through the whole list each time which
166          * is not optimal. we perhaps want to use balanced binary tree
167          * to trace each sec as order of expiry time.
168          * another issue here is we wakeup as fixed interval instead of
169          * according to each sec's expiry time
170          */
171         mutex_lock(&sec_gc_mutex);
172         list_for_each_entry(sec, &sec_gc_list, ps_gc_list) {
173                 /* if someone is waiting to be deleted, let it
174                  * proceed as soon as possible.
175                  */
176                 if (atomic_read(&sec_gc_wait_del)) {
177                         CDEBUG(D_SEC, "deletion pending, start over\n");
178                         mutex_unlock(&sec_gc_mutex);
179                         goto again;
180                 }
181
182                 sec_do_gc(sec);
183         }
184         mutex_unlock(&sec_gc_mutex);
185
186         /* check ctx list again before sleep */
187         sec_process_ctx_list();
188         schedule_delayed_work(&sec_gc_work, cfs_time_seconds(SEC_GC_INTERVAL));
189 }
190
191 int sptlrpc_gc_init(void)
192 {
193         mutex_init(&sec_gc_mutex);
194         spin_lock_init(&sec_gc_list_lock);
195         spin_lock_init(&sec_gc_ctx_list_lock);
196
197         INIT_LIST_HEAD(&sec_gc_list);
198         INIT_LIST_HEAD(&sec_gc_ctx_list);
199
200         schedule_delayed_work(&sec_gc_work, cfs_time_seconds(SEC_GC_INTERVAL));
201         return 0;
202 }
203
204 void sptlrpc_gc_fini(void)
205 {
206         cancel_delayed_work_sync(&sec_gc_work);
207 }