1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ptlrpc/sec_gc.c
40 * Author: Eric Mei <ericm@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_SEC
49 #include <liblustre.h>
51 #include <libcfs/libcfs.h>
54 #include <obd_support.h>
55 #include <obd_class.h>
56 #include <lustre_net.h>
57 #include <lustre_sec.h>
59 #define SEC_GC_INTERVAL (30 * 60)
63 static cfs_mutex_t sec_gc_mutex;
64 static CFS_LIST_HEAD(sec_gc_list);
65 static cfs_spinlock_t sec_gc_list_lock;
67 static CFS_LIST_HEAD(sec_gc_ctx_list);
68 static cfs_spinlock_t sec_gc_ctx_list_lock;
70 static struct ptlrpc_thread sec_gc_thread;
71 static cfs_atomic_t sec_gc_wait_del = CFS_ATOMIC_INIT(0);
74 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
76 LASSERT(sec->ps_policy->sp_cops->gc_ctx);
77 LASSERT(sec->ps_gc_interval > 0);
78 LASSERT(cfs_list_empty(&sec->ps_gc_list));
80 sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
82 cfs_spin_lock(&sec_gc_list_lock);
83 cfs_list_add_tail(&sec_gc_list, &sec->ps_gc_list);
84 cfs_spin_unlock(&sec_gc_list_lock);
86 CDEBUG(D_SEC, "added sec %p(%s)\n", sec, sec->ps_policy->sp_name);
88 EXPORT_SYMBOL(sptlrpc_gc_add_sec);
90 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
92 if (cfs_list_empty(&sec->ps_gc_list))
97 /* signal before list_del to make iteration in gc thread safe */
98 cfs_atomic_inc(&sec_gc_wait_del);
100 cfs_spin_lock(&sec_gc_list_lock);
101 cfs_list_del_init(&sec->ps_gc_list);
102 cfs_spin_unlock(&sec_gc_list_lock);
105 cfs_mutex_lock(&sec_gc_mutex);
106 cfs_mutex_unlock(&sec_gc_mutex);
108 cfs_atomic_dec(&sec_gc_wait_del);
110 CDEBUG(D_SEC, "del sec %p(%s)\n", sec, sec->ps_policy->sp_name);
112 EXPORT_SYMBOL(sptlrpc_gc_del_sec);
114 void sptlrpc_gc_add_ctx(struct ptlrpc_cli_ctx *ctx)
116 LASSERT(cfs_list_empty(&ctx->cc_gc_chain));
118 CDEBUG(D_SEC, "hand over ctx %p(%u->%s)\n",
119 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
120 cfs_spin_lock(&sec_gc_ctx_list_lock);
121 cfs_list_add(&ctx->cc_gc_chain, &sec_gc_ctx_list);
122 cfs_spin_unlock(&sec_gc_ctx_list_lock);
124 thread_add_flags(&sec_gc_thread, SVC_SIGNAL);
125 cfs_waitq_signal(&sec_gc_thread.t_ctl_waitq);
127 EXPORT_SYMBOL(sptlrpc_gc_add_ctx);
129 static void sec_process_ctx_list(void)
131 struct ptlrpc_cli_ctx *ctx;
133 cfs_spin_lock(&sec_gc_ctx_list_lock);
135 while (!cfs_list_empty(&sec_gc_ctx_list)) {
136 ctx = cfs_list_entry(sec_gc_ctx_list.next,
137 struct ptlrpc_cli_ctx, cc_gc_chain);
138 cfs_list_del_init(&ctx->cc_gc_chain);
139 cfs_spin_unlock(&sec_gc_ctx_list_lock);
141 LASSERT(ctx->cc_sec);
142 LASSERT(cfs_atomic_read(&ctx->cc_refcount) == 1);
143 CDEBUG(D_SEC, "gc pick up ctx %p(%u->%s)\n",
144 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
145 sptlrpc_cli_ctx_put(ctx, 1);
147 cfs_spin_lock(&sec_gc_ctx_list_lock);
150 cfs_spin_unlock(&sec_gc_ctx_list_lock);
153 static void sec_do_gc(struct ptlrpc_sec *sec)
155 LASSERT(sec->ps_policy->sp_cops->gc_ctx);
157 if (unlikely(sec->ps_gc_next == 0)) {
158 CDEBUG(D_SEC, "sec %p(%s) has 0 gc time\n",
159 sec, sec->ps_policy->sp_name);
163 CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
165 if (cfs_time_after(sec->ps_gc_next, cfs_time_current_sec()))
168 sec->ps_policy->sp_cops->gc_ctx(sec);
169 sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
172 static int sec_gc_main(void *arg)
174 struct ptlrpc_thread *thread = (struct ptlrpc_thread *) arg;
175 struct l_wait_info lwi;
177 cfs_daemonize_ctxt("sptlrpc_gc");
179 /* Record that the thread is running */
180 thread_set_flags(thread, SVC_RUNNING);
181 cfs_waitq_signal(&thread->t_ctl_waitq);
184 struct ptlrpc_sec *sec;
186 thread_clear_flags(thread, SVC_SIGNAL);
187 sec_process_ctx_list();
189 /* go through sec list do gc.
190 * FIXME here we iterate through the whole list each time which
191 * is not optimal. we perhaps want to use balanced binary tree
192 * to trace each sec as order of expiry time.
193 * another issue here is we wakeup as fixed interval instead of
194 * according to each sec's expiry time */
195 cfs_mutex_lock(&sec_gc_mutex);
196 cfs_list_for_each_entry(sec, &sec_gc_list, ps_gc_list) {
197 /* if someone is waiting to be deleted, let it
198 * proceed as soon as possible. */
199 if (cfs_atomic_read(&sec_gc_wait_del)) {
200 CDEBUG(D_SEC, "deletion pending, start over\n");
201 cfs_mutex_unlock(&sec_gc_mutex);
207 cfs_mutex_unlock(&sec_gc_mutex);
209 /* check ctx list again before sleep */
210 sec_process_ctx_list();
212 lwi = LWI_TIMEOUT(SEC_GC_INTERVAL * CFS_HZ, NULL, NULL);
213 l_wait_event(thread->t_ctl_waitq,
214 thread_is_stopping(thread) ||
215 thread_is_signal(thread),
218 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
222 thread_set_flags(thread, SVC_STOPPED);
223 cfs_waitq_signal(&thread->t_ctl_waitq);
227 int sptlrpc_gc_init(void)
229 struct l_wait_info lwi = { 0 };
232 cfs_mutex_init(&sec_gc_mutex);
233 cfs_spin_lock_init(&sec_gc_list_lock);
234 cfs_spin_lock_init(&sec_gc_ctx_list_lock);
236 /* initialize thread control */
237 memset(&sec_gc_thread, 0, sizeof(sec_gc_thread));
238 cfs_waitq_init(&sec_gc_thread.t_ctl_waitq);
240 rc = cfs_create_thread(sec_gc_main, &sec_gc_thread, CFS_DAEMON_FLAGS);
242 CERROR("can't start gc thread: %d\n", rc);
246 l_wait_event(sec_gc_thread.t_ctl_waitq,
247 thread_is_running(&sec_gc_thread), &lwi);
251 void sptlrpc_gc_fini(void)
253 struct l_wait_info lwi = { 0 };
255 thread_set_flags(&sec_gc_thread, SVC_STOPPING);
256 cfs_waitq_signal(&sec_gc_thread.t_ctl_waitq);
258 l_wait_event(sec_gc_thread.t_ctl_waitq,
259 thread_is_stopped(&sec_gc_thread), &lwi);
262 #else /* !__KERNEL__ */
264 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
267 void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
270 int sptlrpc_gc_init(void)
274 void sptlrpc_gc_fini(void)
278 #endif /* __KERNEL__ */