Whamcloud - gitweb
Reverting the changes
[fs/lustre-release.git] / lustre / ptlrpc / sec_gc.c
index 96ce6ef..bc1fe0d 100644 (file)
@@ -1,23 +1,41 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2007 Cluster File Systems, Inc.
- *   Author: Eric Mei <ericm@clusterfs.com>
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/ptlrpc/sec_gc.c
+ *
+ * Author: Eric Mei <ericm@clusterfs.com>
  */
 
 #ifndef EXPORT_SYMTAB
@@ -27,6 +45,8 @@
 
 #ifndef __KERNEL__
 #include <liblustre.h>
+#else
+#include <libcfs/libcfs.h>
 #endif
 
 #include <obd_support.h>
@@ -51,11 +71,11 @@ static atomic_t sec_gc_wait_del = ATOMIC_INIT(0);
 
 void sptlrpc_gc_add_sec(struct ptlrpc_sec *sec)
 {
-        if (!list_empty(&sec->ps_gc_list)) {
-                CERROR("sec %p(%s) already in gc list\n",
-                       sec, sec->ps_policy->sp_name);
-                return;
-        }
+        LASSERT(sec->ps_policy->sp_cops->gc_ctx);
+        LASSERT(sec->ps_gc_interval > 0);
+        LASSERT(list_empty(&sec->ps_gc_list));
+
+        sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
 
         spin_lock(&sec_gc_list_lock);
         list_add_tail(&sec_gc_list, &sec->ps_gc_list);
@@ -72,14 +92,17 @@ void sptlrpc_gc_del_sec(struct ptlrpc_sec *sec)
 
         might_sleep();
 
+        /* signal before list_del to make iteration in gc thread safe */
+        atomic_inc(&sec_gc_wait_del);
+
         spin_lock(&sec_gc_list_lock);
         list_del_init(&sec->ps_gc_list);
         spin_unlock(&sec_gc_list_lock);
 
         /* barrier */
-        atomic_inc(&sec_gc_wait_del);
         mutex_down(&sec_gc_mutex);
         mutex_up(&sec_gc_mutex);
+
         atomic_dec(&sec_gc_wait_del);
 
         CDEBUG(D_SEC, "del sec %p(%s)\n", sec, sec->ps_policy->sp_name);
@@ -127,7 +150,7 @@ static void sec_process_ctx_list(void)
 
 static void sec_do_gc(struct ptlrpc_sec *sec)
 {
-        cfs_time_t      now = cfs_time_current_sec();
+        LASSERT(sec->ps_policy->sp_cops->gc_ctx);
 
         if (unlikely(sec->ps_gc_next == 0)) {
                 CWARN("sec %p(%s) has 0 gc time\n",
@@ -135,19 +158,13 @@ static void sec_do_gc(struct ptlrpc_sec *sec)
                 return;
         }
 
-        if (unlikely(sec->ps_policy->sp_cops->gc_ctx == NULL)) {
-                CWARN("sec %p(%s) is not prepared for gc\n",
-                      sec, sec->ps_policy->sp_name);
-                return;
-        }
-
         CDEBUG(D_SEC, "check on sec %p(%s)\n", sec, sec->ps_policy->sp_name);
 
-        if (time_after(sec->ps_gc_next, now))
+        if (cfs_time_after(sec->ps_gc_next, cfs_time_current_sec()))
                 return;
 
         sec->ps_policy->sp_cops->gc_ctx(sec);
-        sec->ps_gc_next = now + sec->ps_gc_interval;
+        sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
 }
 
 static int sec_gc_main(void *arg)
@@ -155,24 +172,30 @@ static int sec_gc_main(void *arg)
         struct ptlrpc_thread *thread = (struct ptlrpc_thread *) arg;
         struct l_wait_info    lwi;
 
-        cfs_daemonize("sptlrpc_ctx_gc");
+        cfs_daemonize("sptlrpc_gc");
 
         /* Record that the thread is running */
         thread->t_flags = SVC_RUNNING;
         cfs_waitq_signal(&thread->t_ctl_waitq);
 
         while (1) {
-                struct ptlrpc_sec *sec, *next;
+                struct ptlrpc_sec *sec;
 
                 thread->t_flags &= ~SVC_SIGNAL;
                 sec_process_ctx_list();
 again:
+                /* go through sec list do gc.
+                 * FIXME here we iterate through the whole list each time which
+                 * is not optimal. we perhaps want to use balanced binary tree
+                 * to trace each sec as order of expiry time.
+                 * another issue here is we wakeup as fixed interval instead of
+                 * according to each sec's expiry time */
                 mutex_down(&sec_gc_mutex);
-                list_for_each_entry_safe(sec, next, &sec_gc_list, ps_gc_list) {
+                list_for_each_entry(sec, &sec_gc_list, ps_gc_list) {
                         /* if someone is waiting to be deleted, let it
                          * proceed as soon as possible. */
                         if (atomic_read(&sec_gc_wait_del)) {
-                                CWARN("deletion pending, retry\n");
+                                CWARN("deletion pending, start over\n");
                                 mutex_up(&sec_gc_mutex);
                                 goto again;
                         }
@@ -181,6 +204,9 @@ again:
                 }
                 mutex_up(&sec_gc_mutex);
 
+                /* check ctx list again before sleep */
+                sec_process_ctx_list();
+
                 lwi = LWI_TIMEOUT(SEC_GC_INTERVAL * HZ, NULL, NULL);
                 l_wait_event(thread->t_ctl_waitq,
                              thread->t_flags & (SVC_STOPPING | SVC_SIGNAL),