Whamcloud - gitweb
LU-9019 obd: use 64-bit timestamps for rpc stats
[fs/lustre-release.git] / lustre / obdclass / genops.c
index 257cf03..9857a57 100644 (file)
  *
  * 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.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2014, Intel Corporation.
+ * Copyright (c) 2011, 2016, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #define DEBUG_SUBSYSTEM S_CLASS
 
+#include <linux/pid_namespace.h>
 #include <linux/kthread.h>
 #include <obd_class.h>
 #include <lprocfs_status.h>
+#include <lustre_disk.h>
 #include <lustre_kernelcomm.h>
 
 spinlock_t obd_types_lock;
@@ -61,7 +59,11 @@ static void obd_zombie_impexp_notify(void);
 static void obd_zombie_export_add(struct obd_export *exp);
 static void obd_zombie_import_add(struct obd_import *imp);
 static void print_export_data(struct obd_export *exp,
-                              const char *status, int locks);
+                              const char *status, int locks, int debug_level);
+
+struct list_head obd_stale_exports;
+spinlock_t       obd_stale_export_lock;
+atomic_t         obd_stale_export_num;
 
 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
 EXPORT_SYMBOL(ptlrpc_put_connection_superhack);
@@ -615,19 +617,22 @@ int class_notify_sptlrpc_conf(const char *fsname, int namelen)
         LASSERT(namelen > 0);
 
        read_lock(&obd_dev_lock);
-        for (i = 0; i < class_devno_max(); i++) {
-                obd = class_num2obd(i);
+       for (i = 0; i < class_devno_max(); i++) {
+               obd = class_num2obd(i);
 
-                if (obd == NULL || obd->obd_set_up == 0 || obd->obd_stopping)
-                        continue;
+               if (obd == NULL || obd->obd_set_up == 0 || obd->obd_stopping)
+                       continue;
 
-                /* only notify mdc, osc, mdt, ost */
-                type = obd->obd_type->typ_name;
-                if (strcmp(type, LUSTRE_MDC_NAME) != 0 &&
-                    strcmp(type, LUSTRE_OSC_NAME) != 0 &&
-                    strcmp(type, LUSTRE_MDT_NAME) != 0 &&
-                    strcmp(type, LUSTRE_OST_NAME) != 0)
-                        continue;
+               /* only notify mdc, osc, osp, lwp, mdt, ost
+                * because only these have a -sptlrpc llog */
+               type = obd->obd_type->typ_name;
+               if (strcmp(type, LUSTRE_MDC_NAME) != 0 &&
+                   strcmp(type, LUSTRE_OSC_NAME) != 0 &&
+                   strcmp(type, LUSTRE_OSP_NAME) != 0 &&
+                   strcmp(type, LUSTRE_LWP_NAME) != 0 &&
+                   strcmp(type, LUSTRE_MDT_NAME) != 0 &&
+                   strcmp(type, LUSTRE_OST_NAME) != 0)
+                       continue;
 
                 if (strncmp(obd->obd_name, fsname, namelen))
                         continue;
@@ -712,7 +717,7 @@ struct obd_export *class_conn2export(struct lustre_handle *conn)
                 RETURN(NULL);
         }
 
-        CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
+       CDEBUG(D_INFO, "looking for export cookie %#llx\n", conn->cookie);
        export = class_handle2object(conn->cookie, NULL);
        RETURN(export);
 }
@@ -810,14 +815,15 @@ void class_export_put(struct obd_export *exp)
 
        if (atomic_dec_and_test(&exp->exp_refcount)) {
                LASSERT(!list_empty(&exp->exp_obd_chain));
-                CDEBUG(D_IOCTL, "final put %p/%s\n",
-                       exp, exp->exp_client_uuid.uuid);
+               LASSERT(list_empty(&exp->exp_stale_list));
+               CDEBUG(D_IOCTL, "final put %p/%s\n",
+                      exp, exp->exp_client_uuid.uuid);
 
-                /* release nid stat refererence */
-                lprocfs_exp_cleanup(exp);
+               /* release nid stat refererence */
+               lprocfs_exp_cleanup(exp);
 
-                obd_zombie_export_add(exp);
-        }
+               obd_zombie_export_add(exp);
+       }
 }
 EXPORT_SYMBOL(class_export_put);
 
@@ -865,6 +871,7 @@ struct obd_export *class_new_export(struct obd_device *obd,
        INIT_HLIST_NODE(&export->exp_gen_hash);
        spin_lock_init(&export->exp_bl_list_lock);
        INIT_LIST_HEAD(&export->exp_bl_list);
+       INIT_LIST_HEAD(&export->exp_stale_list);
 
        export->exp_sp_peer = LUSTRE_SP_ANY;
        export->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
@@ -930,12 +937,29 @@ void class_unlink_export(struct obd_export *exp)
                             &exp->exp_client_uuid,
                             &exp->exp_uuid_hash);
 
+       if (!hlist_unhashed(&exp->exp_gen_hash)) {
+               struct tg_export_data   *ted = &exp->exp_target_data;
+               struct cfs_hash         *hash;
+
+               /* Because obd_gen_hash will not be released until
+                * class_cleanup(), so hash should never be NULL here */
+               hash = cfs_hash_getref(exp->exp_obd->obd_gen_hash);
+               LASSERT(hash != NULL);
+               cfs_hash_del(hash, &ted->ted_lcd->lcd_generation,
+                            &exp->exp_gen_hash);
+               cfs_hash_putref(hash);
+       }
+
        list_move(&exp->exp_obd_chain, &exp->exp_obd->obd_unlinked_exports);
        list_del_init(&exp->exp_obd_chain_timed);
        exp->exp_obd->obd_num_exports--;
        spin_unlock(&exp->exp_obd->obd_dev_lock);
-       class_export_put(exp);
+       atomic_inc(&obd_stale_export_num);
+
+       /* A reference is kept by obd_stale_exports list */
+       obd_stale_export_put(exp);
 }
+EXPORT_SYMBOL(class_unlink_export);
 
 /* Import management functions */
 static void class_import_destroy(struct obd_import *imp)
@@ -1022,6 +1046,7 @@ static void init_imp_at(struct imp_at *at) {
 struct obd_import *class_new_import(struct obd_device *obd)
 {
        struct obd_import *imp;
+       struct pid_namespace *curr_pid_ns = ll_task_pid_ns(current);
 
        OBD_ALLOC(imp, sizeof(*imp));
        if (imp == NULL)
@@ -1033,6 +1058,8 @@ struct obd_import *class_new_import(struct obd_device *obd)
        INIT_LIST_HEAD(&imp->imp_sending_list);
        INIT_LIST_HEAD(&imp->imp_delayed_list);
        INIT_LIST_HEAD(&imp->imp_committed_list);
+       INIT_LIST_HEAD(&imp->imp_unreplied_list);
+       imp->imp_known_replied_xid = 0;
        imp->imp_replay_cursor = &imp->imp_committed_list;
        spin_lock_init(&imp->imp_lock);
        imp->imp_last_success_conn = 0;
@@ -1041,6 +1068,11 @@ struct obd_import *class_new_import(struct obd_device *obd)
        mutex_init(&imp->imp_sec_mutex);
        init_waitqueue_head(&imp->imp_recovery_waitq);
 
+       if (curr_pid_ns->child_reaper)
+               imp->imp_sec_refpid = curr_pid_ns->child_reaper->pid;
+       else
+               imp->imp_sec_refpid = 1;
+
        atomic_set(&imp->imp_refcount, 2);
        atomic_set(&imp->imp_unregistering, 0);
        atomic_set(&imp->imp_inflight, 0);
@@ -1094,6 +1126,7 @@ void __class_export_add_lock_ref(struct obd_export *exp, struct ldlm_lock *lock)
                lock, exp, lock->l_exp_refs_nr);
        spin_unlock(&exp->exp_locks_list_guard);
 }
+EXPORT_SYMBOL(__class_export_add_lock_ref);
 
 void __class_export_del_lock_ref(struct obd_export *exp, struct ldlm_lock *lock)
 {
@@ -1112,6 +1145,7 @@ void __class_export_del_lock_ref(struct obd_export *exp, struct ldlm_lock *lock)
                lock, exp, lock->l_exp_refs_nr);
        spin_unlock(&exp->exp_locks_list_guard);
 }
+EXPORT_SYMBOL(__class_export_del_lock_ref);
 #endif
 
 /* A connection defines an export context in which preallocation can
@@ -1134,7 +1168,7 @@ int class_connect(struct lustre_handle *conn, struct obd_device *obd,
         conn->cookie = export->exp_handle.h_cookie;
         class_export_put(export);
 
-        CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
+       CDEBUG(D_IOCTL, "connect: client %s, cookie %#llx\n",
                cluuid->uuid, conn->cookie);
         RETURN(0);
 }
@@ -1201,6 +1235,13 @@ int class_disconnect(struct obd_export *export)
        spin_lock(&export->exp_lock);
        already_disconnected = export->exp_disconnected;
        export->exp_disconnected = 1;
+       /*  We hold references of export for uuid hash
+        *  and nid_hash and export link at least. So
+        *  it is safe to call cfs_hash_del in there.  */
+       if (!hlist_unhashed(&export->exp_nid_hash))
+               cfs_hash_del(export->exp_obd->obd_nid_hash,
+                            &export->exp_connection->c_peer.nid,
+                            &export->exp_nid_hash);
        spin_unlock(&export->exp_lock);
 
         /* class_cleanup(), abort_recovery(), and class_fail_export()
@@ -1211,14 +1252,9 @@ int class_disconnect(struct obd_export *export)
                 GOTO(no_disconn, already_disconnected);
         }
 
-        CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
+       CDEBUG(D_IOCTL, "disconnect: cookie %#llx\n",
                export->exp_handle.h_cookie);
 
-       if (!hlist_unhashed(&export->exp_nid_hash))
-                cfs_hash_del(export->exp_obd->obd_nid_hash,
-                             &export->exp_connection->c_peer.nid,
-                             &export->exp_nid_hash);
-
         class_export_recovery_cleanup(export);
         class_unlink_export(export);
 no_disconn:
@@ -1349,7 +1385,7 @@ void class_disconnect_stale_exports(struct obd_device *obd,
                        obd->obd_name, exp->exp_client_uuid.uuid,
                        exp->exp_connection == NULL ? "<unknown>" :
                        libcfs_nid2str(exp->exp_connection->c_peer.nid));
-                print_export_data(exp, "EVICTING", 0);
+                print_export_data(exp, "EVICTING", 0, D_HA);
         }
        spin_unlock(&obd->obd_dev_lock);
 
@@ -1500,10 +1536,11 @@ int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
 
 #if LUSTRE_TRACKS_LOCK_EXP_REFS
 void (*class_export_dump_hook)(struct obd_export*) = NULL;
+EXPORT_SYMBOL(class_export_dump_hook);
 #endif
 
 static void print_export_data(struct obd_export *exp, const char *status,
-                             int locks)
+                             int locks, int debug_level)
 {
        struct ptlrpc_reply_state *rs;
        struct ptlrpc_reply_state *first_reply = NULL;
@@ -1518,36 +1555,37 @@ static void print_export_data(struct obd_export *exp, const char *status,
        }
        spin_unlock(&exp->exp_lock);
 
-        CDEBUG(D_HA, "%s: %s %p %s %s %d (%d %d %d) %d %d %d %d: %p %s "LPU64"\n",
-               exp->exp_obd->obd_name, status, exp, exp->exp_client_uuid.uuid,
+       CDEBUG(debug_level, "%s: %s %p %s %s %d (%d %d %d) %d %d %d %d: "
+              "%p %s %llu stale:%d\n",
+              exp->exp_obd->obd_name, status, exp, exp->exp_client_uuid.uuid,
               obd_export_nid2str(exp), atomic_read(&exp->exp_refcount),
               atomic_read(&exp->exp_rpc_count),
               atomic_read(&exp->exp_cb_count),
               atomic_read(&exp->exp_locks_count),
-               exp->exp_disconnected, exp->exp_delayed, exp->exp_failed,
-               nreplies, first_reply, nreplies > 3 ? "..." : "",
-               exp->exp_last_committed);
+              exp->exp_disconnected, exp->exp_delayed, exp->exp_failed,
+              nreplies, first_reply, nreplies > 3 ? "..." : "",
+              exp->exp_last_committed, !list_empty(&exp->exp_stale_list));
 #if LUSTRE_TRACKS_LOCK_EXP_REFS
-        if (locks && class_export_dump_hook != NULL)
-                class_export_dump_hook(exp);
+       if (locks && class_export_dump_hook != NULL)
+               class_export_dump_hook(exp);
 #endif
 }
 
-void dump_exports(struct obd_device *obd, int locks)
+void dump_exports(struct obd_device *obd, int locks, int debug_level)
 {
         struct obd_export *exp;
 
        spin_lock(&obd->obd_dev_lock);
        list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain)
-               print_export_data(exp, "ACTIVE", locks);
+               print_export_data(exp, "ACTIVE", locks, debug_level);
        list_for_each_entry(exp, &obd->obd_unlinked_exports, exp_obd_chain)
-               print_export_data(exp, "UNLINKED", locks);
+               print_export_data(exp, "UNLINKED", locks, debug_level);
        list_for_each_entry(exp, &obd->obd_delayed_exports, exp_obd_chain)
-               print_export_data(exp, "DELAYED", locks);
+               print_export_data(exp, "DELAYED", locks, debug_level);
        spin_unlock(&obd->obd_dev_lock);
        spin_lock(&obd_zombie_impexp_lock);
        list_for_each_entry(exp, &obd_zombie_exports, exp_obd_chain)
-               print_export_data(exp, "ZOMBIE", locks);
+               print_export_data(exp, "ZOMBIE", locks, debug_level);
        spin_unlock(&obd_zombie_impexp_lock);
 }
 
@@ -1560,13 +1598,13 @@ void obd_exports_barrier(struct obd_device *obd)
                spin_unlock(&obd->obd_dev_lock);
                set_current_state(TASK_UNINTERRUPTIBLE);
                schedule_timeout(cfs_time_seconds(waited));
-               if (waited > 5 && IS_PO2(waited)) {
+               if (waited > 5 && is_power_of_2(waited)) {
                        LCONSOLE_WARN("%s is waiting for obd_unlinked_exports "
                                      "more than %d seconds. "
                                      "The obd refcount = %d. Is it stuck?\n",
                                      obd->obd_name, waited,
                                      atomic_read(&obd->obd_refcount));
-                       dump_exports(obd, 1);
+                       dump_exports(obd, 1, D_CONSOLE | D_WARNING);
                }
                waited *= 2;
                spin_lock(&obd->obd_dev_lock);
@@ -1656,6 +1694,7 @@ static int obd_zombie_impexp_check(void *arg)
  * Add export to the obd_zombe thread and notify it.
  */
 static void obd_zombie_export_add(struct obd_export *exp) {
+       atomic_dec(&obd_stale_export_num);
        spin_lock(&exp->exp_obd->obd_dev_lock);
        LASSERT(!list_empty(&exp->exp_obd_chain));
        list_del_init(&exp->exp_obd_chain);
@@ -1673,7 +1712,6 @@ static void obd_zombie_export_add(struct obd_export *exp) {
  */
 static void obd_zombie_import_add(struct obd_import *imp) {
        LASSERT(imp->imp_sec == NULL);
-       LASSERT(imp->imp_rq_pool == NULL);
        spin_lock(&obd_zombie_impexp_lock);
        LASSERT(list_empty(&imp->imp_zombie_chain));
        zombies_count++;
@@ -1725,6 +1763,76 @@ void obd_zombie_barrier(void)
 EXPORT_SYMBOL(obd_zombie_barrier);
 
 
+struct obd_export *obd_stale_export_get(void)
+{
+       struct obd_export *exp = NULL;
+       ENTRY;
+
+       spin_lock(&obd_stale_export_lock);
+       if (!list_empty(&obd_stale_exports)) {
+               exp = list_entry(obd_stale_exports.next,
+                                struct obd_export, exp_stale_list);
+               list_del_init(&exp->exp_stale_list);
+       }
+       spin_unlock(&obd_stale_export_lock);
+
+       if (exp) {
+               CDEBUG(D_DLMTRACE, "Get export %p: total %d\n", exp,
+                      atomic_read(&obd_stale_export_num));
+       }
+       RETURN(exp);
+}
+EXPORT_SYMBOL(obd_stale_export_get);
+
+void obd_stale_export_put(struct obd_export *exp)
+{
+       ENTRY;
+
+       LASSERT(list_empty(&exp->exp_stale_list));
+       if (exp->exp_lock_hash &&
+           atomic_read(&exp->exp_lock_hash->hs_count)) {
+               CDEBUG(D_DLMTRACE, "Put export %p: total %d\n", exp,
+                      atomic_read(&obd_stale_export_num));
+
+               spin_lock_bh(&exp->exp_bl_list_lock);
+               spin_lock(&obd_stale_export_lock);
+               /* Add to the tail if there is no blocked locks,
+                * to the head otherwise. */
+               if (list_empty(&exp->exp_bl_list))
+                       list_add_tail(&exp->exp_stale_list,
+                                     &obd_stale_exports);
+               else
+                       list_add(&exp->exp_stale_list,
+                                &obd_stale_exports);
+
+               spin_unlock(&obd_stale_export_lock);
+               spin_unlock_bh(&exp->exp_bl_list_lock);
+       } else {
+               class_export_put(exp);
+       }
+       EXIT;
+}
+EXPORT_SYMBOL(obd_stale_export_put);
+
+/**
+ * Adjust the position of the export in the stale list,
+ * i.e. move to the head of the list if is needed.
+ **/
+void obd_stale_export_adjust(struct obd_export *exp)
+{
+       LASSERT(exp != NULL);
+       spin_lock_bh(&exp->exp_bl_list_lock);
+       spin_lock(&obd_stale_export_lock);
+
+       if (!list_empty(&exp->exp_stale_list) &&
+           !list_empty(&exp->exp_bl_list))
+               list_move(&exp->exp_stale_list, &obd_stale_exports);
+
+       spin_unlock(&obd_stale_export_lock);
+       spin_unlock_bh(&exp->exp_bl_list_lock);
+}
+EXPORT_SYMBOL(obd_stale_export_adjust);
+
 /**
  * destroy zombie export/import thread.
  */
@@ -1809,21 +1917,6 @@ struct kuc_hdr * kuc_ptr(void *p)
 }
 EXPORT_SYMBOL(kuc_ptr);
 
-/* Test if payload is part of kuc message
- * @param p Pointer to payload area
- * @returns boolean
- */
-int kuc_ispayload(void *p)
-{
-        struct kuc_hdr *kh = ((struct kuc_hdr *)p) - 1;
-
-        if (kh->kuc_magic == KUC_MAGIC)
-                return 1;
-        else
-                return 0;
-}
-EXPORT_SYMBOL(kuc_ispayload);
-
 /* Alloc space for a message, and fill in header
  * @return Pointer to payload area
  */
@@ -1846,7 +1939,7 @@ void *kuc_alloc(int payload_len, int transport, int type)
 EXPORT_SYMBOL(kuc_alloc);
 
 /* Takes pointer to payload area */
-inline void kuc_free(void *p, int payload_len)
+void kuc_free(void *p, int payload_len)
 {
         struct kuc_hdr *lh = kuc_ptr(p);
         OBD_FREE(lh, kuc_len(payload_len));
@@ -2062,16 +2155,16 @@ EXPORT_SYMBOL(obd_set_max_mod_rpcs_in_flight);
 int obd_mod_rpc_stats_seq_show(struct client_obd *cli,
                               struct seq_file *seq)
 {
-       struct timeval now;
        unsigned long mod_tot = 0, mod_cum;
+       struct timespec64 now;
        int i;
 
-       do_gettimeofday(&now);
+       ktime_get_real_ts64(&now);
 
        spin_lock(&cli->cl_mod_rpcs_lock);
 
-       seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
-                  now.tv_sec, now.tv_usec);
+       seq_printf(seq, "snapshot_time:         %llu.%9lu (secs.nsecs)\n",
+                  (s64)now.tv_sec, now.tv_nsec);
        seq_printf(seq, "modify_RPCs_in_flight:  %hu\n",
                   cli->cl_mod_rpcs_in_flight);
 
@@ -2085,8 +2178,8 @@ int obd_mod_rpc_stats_seq_show(struct client_obd *cli,
                unsigned long mod = cli->cl_mod_rpcs_hist.oh_buckets[i];
                mod_cum += mod;
                seq_printf(seq, "%d:\t\t%10lu %3lu %3lu\n",
-                                i, mod, pct(mod, mod_tot),
-                                pct(mod_cum, mod_tot));
+                          i, mod, pct(mod, mod_tot),
+                          pct(mod_cum, mod_tot));
                if (mod_cum == mod_tot)
                        break;
        }