linux/drivers/staging/lustre/lustre/ptlrpc/sec_gc.c
<<
>>
Prefs
   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) 2011, 2012, 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 "../../include/linux/libcfs/libcfs.h"
  44
  45#include "../include/obd_support.h"
  46#include "../include/obd_class.h"
  47#include "../include/lustre_net.h"
  48#include "../include/lustre_sec.h"
  49
  50#include "ptlrpc_internal.h"
  51
  52#define SEC_GC_INTERVAL (30 * 60)
  53
  54static struct mutex sec_gc_mutex;
  55static LIST_HEAD(sec_gc_list);
  56static spinlock_t sec_gc_list_lock;
  57
  58static LIST_HEAD(sec_gc_ctx_list);
  59static spinlock_t sec_gc_ctx_list_lock;
  60
  61static struct ptlrpc_thread sec_gc_thread;
  62static atomic_t sec_gc_wait_del = ATOMIC_INIT(0);
  63
  64void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
  65{
  66        LASSERT(sec->ps_policy->sp_cops->gc_ctx);
  67        LASSERT(sec->ps_gc_interval > 0);
  68        LASSERT(list_empty(&sec->ps_gc_list));
  69
  70        sec->ps_gc_next = ktime_get_real_seconds() + sec->ps_gc_interval;
  71
  72        spin_lock(&sec_gc_list_lock);
  73        list_add_tail(&sec_gc_list, &sec->ps_gc_list);
  74        spin_unlock(&sec_gc_list_lock);
  75
  76        CDEBUG(D_SEC, "added sec %p(%s)\n", sec, sec->ps_policy->sp_name);
  77}
  78EXPORT_SYMBOL(sptlrpc_gc_add_sec);
  79
  80void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
  81{
  82        if (list_empty(&sec->ps_gc_list))
  83                return;
  84
  85        might_sleep();
  86
  87        /* signal before list_del to make iteration in gc thread safe */
  88        atomic_inc(&sec_gc_wait_del);
  89
  90        spin_lock(&sec_gc_list_lock);
  91        list_del_init(&sec->ps_gc_list);
  92        spin_unlock(&sec_gc_list_lock);
  93
  94        /* barrier */
  95        mutex_lock(&sec_gc_mutex);
  96        mutex_unlock(&sec_gc_mutex);
  97
  98        atomic_dec(&sec_gc_wait_del);
  99
 100        CDEBUG(D_SEC, "del sec %p(%s)\n", sec, sec->ps_policy->sp_name);
 101}
 102EXPORT_SYMBOL(sptlrpc_gc_del_sec);
 103
 104static void sec_process_ctx_list(void)
 105{
 106        struct ptlrpc_cli_ctx *ctx;
 107
 108        spin_lock(&sec_gc_ctx_list_lock);
 109
 110        while (!list_empty(&sec_gc_ctx_list)) {
 111                ctx = list_entry(sec_gc_ctx_list.next,
 112                                 struct ptlrpc_cli_ctx, cc_gc_chain);
 113                list_del_init(&ctx->cc_gc_chain);
 114                spin_unlock(&sec_gc_ctx_list_lock);
 115
 116                LASSERT(ctx->cc_sec);
 117                LASSERT(atomic_read(&ctx->cc_refcount) == 1);
 118                CDEBUG(D_SEC, "gc pick up ctx %p(%u->%s)\n",
 119                       ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
 120                sptlrpc_cli_ctx_put(ctx, 1);
 121
 122                spin_lock(&sec_gc_ctx_list_lock);
 123        }
 124
 125        spin_unlock(&sec_gc_ctx_list_lock);
 126}
 127
 128static void sec_do_gc(struct ptlrpc_sec *sec)
 129{
 130        LASSERT(sec->ps_policy->sp_cops->gc_ctx);
 131
 132        if (unlikely(sec->ps_gc_next == 0)) {
 133                CDEBUG(D_SEC, "sec %p(%s) has 0 gc time\n",
 134                       sec, sec->ps_policy->sp_name);
 135                return;
 136        }
 137
 138        CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
 139
 140        if (sec->ps_gc_next > ktime_get_real_seconds())
 141                return;
 142
 143        sec->ps_policy->sp_cops->gc_ctx(sec);
 144        sec->ps_gc_next = ktime_get_real_seconds() + sec->ps_gc_interval;
 145}
 146
 147static int sec_gc_main(void *arg)
 148{
 149        struct ptlrpc_thread *thread = arg;
 150        struct l_wait_info lwi;
 151
 152        unshare_fs_struct();
 153
 154        /* Record that the thread is running */
 155        thread_set_flags(thread, SVC_RUNNING);
 156        wake_up(&thread->t_ctl_waitq);
 157
 158        while (1) {
 159                struct ptlrpc_sec *sec;
 160
 161                thread_clear_flags(thread, SVC_SIGNAL);
 162                sec_process_ctx_list();
 163again:
 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
 189                lwi = LWI_TIMEOUT(SEC_GC_INTERVAL * HZ, NULL, NULL);
 190                l_wait_event(thread->t_ctl_waitq,
 191                             thread_is_stopping(thread) ||
 192                             thread_is_signal(thread),
 193                             &lwi);
 194
 195                if (thread_test_and_clear_flags(thread, SVC_STOPPING))
 196                        break;
 197        }
 198
 199        thread_set_flags(thread, SVC_STOPPED);
 200        wake_up(&thread->t_ctl_waitq);
 201        return 0;
 202}
 203
 204int sptlrpc_gc_init(void)
 205{
 206        struct l_wait_info lwi = { 0 };
 207        struct task_struct *task;
 208
 209        mutex_init(&sec_gc_mutex);
 210        spin_lock_init(&sec_gc_list_lock);
 211        spin_lock_init(&sec_gc_ctx_list_lock);
 212
 213        /* initialize thread control */
 214        memset(&sec_gc_thread, 0, sizeof(sec_gc_thread));
 215        init_waitqueue_head(&sec_gc_thread.t_ctl_waitq);
 216
 217        task = kthread_run(sec_gc_main, &sec_gc_thread, "sptlrpc_gc");
 218        if (IS_ERR(task)) {
 219                CERROR("can't start gc thread: %ld\n", PTR_ERR(task));
 220                return PTR_ERR(task);
 221        }
 222
 223        l_wait_event(sec_gc_thread.t_ctl_waitq,
 224                     thread_is_running(&sec_gc_thread), &lwi);
 225        return 0;
 226}
 227
 228void sptlrpc_gc_fini(void)
 229{
 230        struct l_wait_info lwi = { 0 };
 231
 232        thread_set_flags(&sec_gc_thread, SVC_STOPPING);
 233        wake_up(&sec_gc_thread.t_ctl_waitq);
 234
 235        l_wait_event(sec_gc_thread.t_ctl_waitq,
 236                     thread_is_stopped(&sec_gc_thread), &lwi);
 237}
 238