Whamcloud - gitweb
LU-8066 obd: migrate to ksets
[fs/lustre-release.git] / lustre / ptlrpc / lproc_ptlrpc.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 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 #define DEBUG_SUBSYSTEM S_CLASS
33
34
35 #include <obd_support.h>
36 #include <obd.h>
37 #include <lprocfs_status.h>
38 #include <lustre_net.h>
39 #include <obd_class.h>
40 #include "ptlrpc_internal.h"
41
42
43 static struct ll_rpc_opcode {
44      __u32       opcode;
45      const char *opname;
46 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
47         { OST_REPLY,        "ost_reply" },
48         { OST_GETATTR,      "ost_getattr" },
49         { OST_SETATTR,      "ost_setattr" },
50         { OST_READ,         "ost_read" },
51         { OST_WRITE,        "ost_write" },
52         { OST_CREATE ,      "ost_create" },
53         { OST_DESTROY,      "ost_destroy" },
54         { OST_GET_INFO,     "ost_get_info" },
55         { OST_CONNECT,      "ost_connect" },
56         { OST_DISCONNECT,   "ost_disconnect" },
57         { OST_PUNCH,        "ost_punch" },
58         { OST_OPEN,         "ost_open" },
59         { OST_CLOSE,        "ost_close" },
60         { OST_STATFS,       "ost_statfs" },
61         { 14,                NULL },    /* formerly OST_SAN_READ */
62         { 15,                NULL },    /* formerly OST_SAN_WRITE */
63         { OST_SYNC,         "ost_sync" },
64         { OST_SET_INFO,     "ost_set_info" },
65         { OST_QUOTACHECK,   "ost_quotacheck" },
66         { OST_QUOTACTL,     "ost_quotactl" },
67         { OST_QUOTA_ADJUST_QUNIT, "ost_quota_adjust_qunit" },
68         { OST_LADVISE,      "ost_ladvise" },
69         { MDS_GETATTR,      "mds_getattr" },
70         { MDS_GETATTR_NAME, "mds_getattr_lock" },
71         { MDS_CLOSE,        "mds_close" },
72         { MDS_REINT,        "mds_reint" },
73         { MDS_READPAGE,     "mds_readpage" },
74         { MDS_CONNECT,      "mds_connect" },
75         { MDS_DISCONNECT,   "mds_disconnect" },
76         { MDS_GET_ROOT,     "mds_get_root" },
77         { MDS_STATFS,       "mds_statfs" },
78         { MDS_PIN,          "mds_pin" },
79         { MDS_UNPIN,        "mds_unpin" },
80         { MDS_SYNC,         "mds_sync" },
81         { MDS_DONE_WRITING, "mds_done_writing" },
82         { MDS_SET_INFO,     "mds_set_info" },
83         { MDS_QUOTACHECK,   "mds_quotacheck" },
84         { MDS_QUOTACTL,     "mds_quotactl" },
85         { MDS_GETXATTR,     "mds_getxattr" },
86         { MDS_SETXATTR,     "mds_setxattr" },
87         { MDS_WRITEPAGE,    "mds_writepage" },
88         { MDS_IS_SUBDIR,    "mds_is_subdir" },
89         { MDS_GET_INFO,     "mds_get_info" },
90         { MDS_HSM_STATE_GET, "mds_hsm_state_get" },
91         { MDS_HSM_STATE_SET, "mds_hsm_state_set" },
92         { MDS_HSM_ACTION,   "mds_hsm_action" },
93         { MDS_HSM_PROGRESS, "mds_hsm_progress" },
94         { MDS_HSM_REQUEST,  "mds_hsm_request" },
95         { MDS_HSM_CT_REGISTER, "mds_hsm_ct_register" },
96         { MDS_HSM_CT_UNREGISTER, "mds_hsm_ct_unregister" },
97         { MDS_SWAP_LAYOUTS,     "mds_swap_layouts" },
98         { LDLM_ENQUEUE,     "ldlm_enqueue" },
99         { LDLM_CONVERT,     "ldlm_convert" },
100         { LDLM_CANCEL,      "ldlm_cancel" },
101         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
102         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
103         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
104         { LDLM_SET_INFO,    "ldlm_set_info" },
105         { MGS_CONNECT,      "mgs_connect" },
106         { MGS_DISCONNECT,   "mgs_disconnect" },
107         { MGS_EXCEPTION,    "mgs_exception" },
108         { MGS_TARGET_REG,   "mgs_target_reg" },
109         { MGS_TARGET_DEL,   "mgs_target_del" },
110         { MGS_SET_INFO,     "mgs_set_info" },
111         { MGS_CONFIG_READ,  "mgs_config_read" },
112         { OBD_PING,         "obd_ping" },
113         { OBD_LOG_CANCEL,       "llog_cancel" },
114         { OBD_QC_CALLBACK,  "obd_quota_callback" },
115         { OBD_IDX_READ,     "dt_index_read" },
116         { LLOG_ORIGIN_HANDLE_CREATE,     "llog_origin_handle_open" },
117         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block" },
118         { LLOG_ORIGIN_HANDLE_READ_HEADER,"llog_origin_handle_read_header" },
119         { LLOG_ORIGIN_HANDLE_WRITE_REC,  "llog_origin_handle_write_rec" },
120         { LLOG_ORIGIN_HANDLE_CLOSE,      "llog_origin_handle_close" },
121         { LLOG_ORIGIN_CONNECT,           "llog_origin_connect" },
122         { LLOG_CATINFO,                  "llog_catinfo" },
123         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
124         { LLOG_ORIGIN_HANDLE_DESTROY,    "llog_origin_handle_destroy" },
125         { QUOTA_DQACQ,      "quota_acquire" },
126         { QUOTA_DQREL,      "quota_release" },
127         { SEQ_QUERY,        "seq_query" },
128         { SEC_CTX_INIT,     "sec_ctx_init" },
129         { SEC_CTX_INIT_CONT,"sec_ctx_init_cont" },
130         { SEC_CTX_FINI,     "sec_ctx_fini" },
131         { FLD_QUERY,        "fld_query" },
132         { FLD_READ,         "fld_read" },
133         { OUT_UPDATE,       "out_update" },
134         { LFSCK_NOTIFY,     "lfsck_notify" },
135         { LFSCK_QUERY,      "lfsck_query" },
136 };
137
138 static struct ll_eopcode {
139      __u32       opcode;
140      const char *opname;
141 } ll_eopcode_table[EXTRA_LAST_OPC] = {
142         { LDLM_GLIMPSE_ENQUEUE, "ldlm_glimpse_enqueue" },
143         { LDLM_PLAIN_ENQUEUE,   "ldlm_plain_enqueue" },
144         { LDLM_EXTENT_ENQUEUE,  "ldlm_extent_enqueue" },
145         { LDLM_FLOCK_ENQUEUE,   "ldlm_flock_enqueue" },
146         { LDLM_IBITS_ENQUEUE,   "ldlm_ibits_enqueue" },
147         { MDS_REINT_SETATTR,    "mds_reint_setattr" },
148         { MDS_REINT_CREATE,     "mds_reint_create" },
149         { MDS_REINT_LINK,       "mds_reint_link" },
150         { MDS_REINT_UNLINK,     "mds_reint_unlink" },
151         { MDS_REINT_RENAME,     "mds_reint_rename" },
152         { MDS_REINT_OPEN,       "mds_reint_open" },
153         { MDS_REINT_SETXATTR,   "mds_reint_setxattr" },
154         { BRW_READ_BYTES,       "read_bytes" },
155         { BRW_WRITE_BYTES,      "write_bytes" },
156 };
157
158 const char *ll_opcode2str(__u32 opcode)
159 {
160         /* When one of the assertions below fail, chances are that:
161          *     1) A new opcode was added in include/lustre/lustre_idl.h,
162          *        but is missing from the table above.
163          * or  2) The opcode space was renumbered or rearranged,
164          *        and the opcode_offset() function in
165          *        ptlrpc_internal.h needs to be modified.
166          */
167         __u32 offset = opcode_offset(opcode);
168         LASSERTF(offset < LUSTRE_MAX_OPCODES,
169                  "offset %u >= LUSTRE_MAX_OPCODES %u\n",
170                  offset, LUSTRE_MAX_OPCODES);
171         LASSERTF(ll_rpc_opcode_table[offset].opcode == opcode,
172                  "ll_rpc_opcode_table[%u].opcode %u != opcode %u\n",
173                  offset, ll_rpc_opcode_table[offset].opcode, opcode);
174         return ll_rpc_opcode_table[offset].opname;
175 }
176
177 const int ll_str2opcode(const char *ops)
178 {
179         int i;
180
181         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
182                 if (ll_rpc_opcode_table[i].opname != NULL &&
183                     strcmp(ll_rpc_opcode_table[i].opname, ops) == 0)
184                         return ll_rpc_opcode_table[i].opcode;
185         }
186
187         return -EINVAL;
188 }
189
190 static const char *ll_eopcode2str(__u32 opcode)
191 {
192         LASSERT(ll_eopcode_table[opcode].opcode == opcode);
193         return ll_eopcode_table[opcode].opname;
194 }
195
196 #ifdef CONFIG_PROC_FS
197 static void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
198                              char *name, struct proc_dir_entry **procroot_ret,
199                              struct lprocfs_stats **stats_ret)
200 {
201         struct proc_dir_entry *svc_procroot;
202         struct lprocfs_stats *svc_stats;
203         int i, rc;
204         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
205                                           LPROCFS_CNTR_STDDEV;
206
207         LASSERT(*procroot_ret == NULL);
208         LASSERT(*stats_ret == NULL);
209
210         svc_stats = lprocfs_alloc_stats(EXTRA_MAX_OPCODES+LUSTRE_MAX_OPCODES,0);
211         if (svc_stats == NULL)
212                 return;
213
214         if (dir) {
215                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
216                 if (IS_ERR(svc_procroot)) {
217                         lprocfs_free_stats(&svc_stats);
218                         return;
219                 }
220         } else {
221                 svc_procroot = root;
222         }
223
224         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
225                              svc_counter_config, "req_waittime", "usec");
226         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
227                              svc_counter_config, "req_qdepth", "reqs");
228         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
229                              svc_counter_config, "req_active", "reqs");
230         lprocfs_counter_init(svc_stats, PTLRPC_TIMEOUT,
231                              svc_counter_config, "req_timeout", "sec");
232         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
233                              svc_counter_config, "reqbuf_avail", "bufs");
234         for (i = 0; i < EXTRA_LAST_OPC; i++) {
235                 char *units;
236
237                 switch(i) {
238                 case BRW_WRITE_BYTES:
239                 case BRW_READ_BYTES:
240                         units = "bytes";
241                         break;
242                 default:
243                         units = "reqs";
244                         break;
245                 }
246                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
247                                      svc_counter_config,
248                                      ll_eopcode2str(i), units);
249         }
250         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
251                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
252                 lprocfs_counter_init(svc_stats,
253                                      EXTRA_MAX_OPCODES + i, svc_counter_config,
254                                      ll_opcode2str(opcode), "usec");
255         }
256
257         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
258         if (rc < 0) {
259                 if (dir)
260                         lprocfs_remove(&svc_procroot);
261                 lprocfs_free_stats(&svc_stats);
262         } else {
263                 if (dir)
264                         *procroot_ret = svc_procroot;
265                 *stats_ret = svc_stats;
266         }
267 }
268
269 static int
270 ptlrpc_lprocfs_req_history_len_seq_show(struct seq_file *m, void *v)
271 {
272         struct ptlrpc_service *svc = m->private;
273         struct ptlrpc_service_part *svcpt;
274         int     total = 0;
275         int     i;
276
277         ptlrpc_service_for_each_part(svcpt, i, svc)
278                 total += svcpt->scp_hist_nrqbds;
279
280         seq_printf(m, "%d\n", total);
281         return 0;
282 }
283 LPROC_SEQ_FOPS_RO(ptlrpc_lprocfs_req_history_len);
284
285 static int
286 ptlrpc_lprocfs_req_history_max_seq_show(struct seq_file *m, void *n)
287 {
288         struct ptlrpc_service *svc = m->private;
289         struct ptlrpc_service_part *svcpt;
290         int     total = 0;
291         int     i;
292
293         ptlrpc_service_for_each_part(svcpt, i, svc)
294                 total += svc->srv_hist_nrqbds_cpt_max;
295
296         seq_printf(m, "%d\n", total);
297         return 0;
298 }
299
300 static ssize_t
301 ptlrpc_lprocfs_req_history_max_seq_write(struct file *file,
302                                          const char __user *buffer,
303                                          size_t count, loff_t *off)
304 {
305         struct seq_file *m = file->private_data;
306         struct ptlrpc_service *svc = m->private;
307         int bufpages;
308         __s64 val;
309         int rc;
310
311         rc = lprocfs_str_to_s64(buffer, count, &val);
312         if (rc < 0)
313                 return rc;
314
315         if (val < 0 || val > INT_MAX)
316                 return -ERANGE;
317
318         /* This sanity check is more of an insanity check; we can still
319          * hose a kernel by allowing the request history to grow too
320          * far. */
321         bufpages = (svc->srv_buf_size + PAGE_SIZE - 1) >>
322                                                         PAGE_SHIFT;
323         if (val > totalram_pages/(2 * bufpages))
324                 return -ERANGE;
325
326         spin_lock(&svc->srv_lock);
327
328         if (val == 0)
329                 svc->srv_hist_nrqbds_cpt_max = 0;
330         else
331                 svc->srv_hist_nrqbds_cpt_max =
332                         max(1, ((int)val / svc->srv_ncpts));
333
334         spin_unlock(&svc->srv_lock);
335
336         return count;
337 }
338 LPROC_SEQ_FOPS(ptlrpc_lprocfs_req_history_max);
339
340 static ssize_t threads_min_show(struct kobject *kobj, struct attribute *attr,
341                                 char *buf)
342 {
343         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
344                                                   srv_kobj);
345
346         return sprintf(buf, "%d\n", svc->srv_nthrs_cpt_init * svc->srv_ncpts);
347 }
348
349 static ssize_t threads_min_store(struct kobject *kobj, struct attribute *attr,
350                                  const char *buffer, size_t count)
351 {
352         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
353                                                   srv_kobj);
354         unsigned long val;
355         int rc;
356
357         rc = kstrtoul(buffer, 10, &val);
358         if (rc < 0)
359                 return rc;
360
361         if (val / svc->srv_ncpts < PTLRPC_NTHRS_INIT)
362                 return -ERANGE;
363
364         spin_lock(&svc->srv_lock);
365         if (val > svc->srv_nthrs_cpt_limit * svc->srv_ncpts) {
366                 spin_unlock(&svc->srv_lock);
367                 return -ERANGE;
368         }
369
370         svc->srv_nthrs_cpt_init = (int)val / svc->srv_ncpts;
371
372         spin_unlock(&svc->srv_lock);
373
374         return count;
375 }
376 LUSTRE_RW_ATTR(threads_min);
377
378 static ssize_t threads_started_show(struct kobject *kobj,
379                                     struct attribute *attr,
380                                     char *buf)
381 {
382         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
383                                                   srv_kobj);
384         struct ptlrpc_service_part *svcpt;
385         int total = 0;
386         int i;
387
388         ptlrpc_service_for_each_part(svcpt, i, svc)
389                 total += svcpt->scp_nthrs_running;
390
391         return sprintf(buf, "%d\n", total);
392 }
393 LUSTRE_RO_ATTR(threads_started);
394
395 static ssize_t threads_max_show(struct kobject *kobj, struct attribute *attr,
396                                 char *buf)
397 {
398         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
399                                                   srv_kobj);
400
401         return sprintf(buf, "%d\n", svc->srv_nthrs_cpt_limit * svc->srv_ncpts);
402 }
403
404 static ssize_t threads_max_store(struct kobject *kobj, struct attribute *attr,
405                                  const char *buffer, size_t count)
406 {
407         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
408                                                   srv_kobj);
409         unsigned long val;
410         int rc;
411
412         rc = kstrtoul(buffer, 10, &val);
413         if (rc < 0)
414                 return rc;
415
416         if (val / svc->srv_ncpts < PTLRPC_NTHRS_INIT)
417                 return -ERANGE;
418
419         spin_lock(&svc->srv_lock);
420         if (val < svc->srv_nthrs_cpt_init * svc->srv_ncpts) {
421                 spin_unlock(&svc->srv_lock);
422                 return -ERANGE;
423         }
424
425         svc->srv_nthrs_cpt_limit = (int)val / svc->srv_ncpts;
426
427         spin_unlock(&svc->srv_lock);
428
429         return count;
430 }
431 LUSTRE_RW_ATTR(threads_max);
432
433 /**
434  * Translates \e ptlrpc_nrs_pol_state values to human-readable strings.
435  *
436  * \param[in] state The policy state
437  */
438 static const char *nrs_state2str(enum ptlrpc_nrs_pol_state state)
439 {
440         switch (state) {
441         default:
442                 LBUG();
443         case NRS_POL_STATE_INVALID:
444                 return "invalid";
445         case NRS_POL_STATE_STOPPED:
446                 return "stopped";
447         case NRS_POL_STATE_STOPPING:
448                 return "stopping";
449         case NRS_POL_STATE_STARTING:
450                 return "starting";
451         case NRS_POL_STATE_STARTED:
452                 return "started";
453         }
454 }
455
456 /**
457  * Obtains status information for \a policy.
458  *
459  * Information is copied in \a info.
460  *
461  * \param[in] policy The policy
462  * \param[out] info  Holds returned status information
463  */
464 void nrs_policy_get_info_locked(struct ptlrpc_nrs_policy *policy,
465                                 struct ptlrpc_nrs_pol_info *info)
466 {
467         LASSERT(policy != NULL);
468         LASSERT(info != NULL);
469         assert_spin_locked(&policy->pol_nrs->nrs_lock);
470
471         CLASSERT(sizeof(info->pi_arg) == sizeof(policy->pol_arg));
472         memcpy(info->pi_name, policy->pol_desc->pd_name, NRS_POL_NAME_MAX);
473         memcpy(info->pi_arg, policy->pol_arg, sizeof(policy->pol_arg));
474
475         info->pi_fallback    = !!(policy->pol_flags & PTLRPC_NRS_FL_FALLBACK);
476         info->pi_state       = policy->pol_state;
477         /**
478          * XXX: These are accessed without holding
479          * ptlrpc_service_part::scp_req_lock.
480          */
481         info->pi_req_queued  = policy->pol_req_queued;
482         info->pi_req_started = policy->pol_req_started;
483 }
484
485 /**
486  * Reads and prints policy status information for all policies of a PTLRPC
487  * service.
488  */
489 static int ptlrpc_lprocfs_nrs_seq_show(struct seq_file *m, void *n)
490 {
491         struct ptlrpc_service          *svc = m->private;
492         struct ptlrpc_service_part     *svcpt;
493         struct ptlrpc_nrs              *nrs;
494         struct ptlrpc_nrs_policy       *policy;
495         struct ptlrpc_nrs_pol_info     *infos;
496         struct ptlrpc_nrs_pol_info      tmp;
497         unsigned                        num_pols;
498         unsigned                        pol_idx = 0;
499         bool                            hp = false;
500         int                             i;
501         int                             rc = 0;
502         ENTRY;
503
504         /**
505          * Serialize NRS core lprocfs operations with policy registration/
506          * unregistration.
507          */
508         mutex_lock(&nrs_core.nrs_mutex);
509
510         /**
511          * Use the first service partition's regular NRS head in order to obtain
512          * the number of policies registered with NRS heads of this service. All
513          * service partitions will have the same number of policies.
514          */
515         nrs = nrs_svcpt2nrs(svc->srv_parts[0], false);
516
517         spin_lock(&nrs->nrs_lock);
518         num_pols = svc->srv_parts[0]->scp_nrs_reg.nrs_num_pols;
519         spin_unlock(&nrs->nrs_lock);
520
521         OBD_ALLOC(infos, num_pols * sizeof(*infos));
522         if (infos == NULL)
523                 GOTO(out, rc = -ENOMEM);
524 again:
525
526         ptlrpc_service_for_each_part(svcpt, i, svc) {
527                 nrs = nrs_svcpt2nrs(svcpt, hp);
528                 spin_lock(&nrs->nrs_lock);
529
530                 pol_idx = 0;
531
532                 list_for_each_entry(policy, &nrs->nrs_policy_list,
533                                     pol_list) {
534                         LASSERT(pol_idx < num_pols);
535
536                         nrs_policy_get_info_locked(policy, &tmp);
537                         /**
538                          * Copy values when handling the first service
539                          * partition.
540                          */
541                         if (i == 0) {
542                                 memcpy(infos[pol_idx].pi_name, tmp.pi_name,
543                                        NRS_POL_NAME_MAX);
544                                 memcpy(infos[pol_idx].pi_arg, tmp.pi_arg,
545                                        sizeof(tmp.pi_arg));
546                                 memcpy(&infos[pol_idx].pi_state, &tmp.pi_state,
547                                        sizeof(tmp.pi_state));
548                                 infos[pol_idx].pi_fallback = tmp.pi_fallback;
549                                 /**
550                                  * For the rest of the service partitions
551                                  * sanity-check the values we get.
552                                  */
553                         } else {
554                                 LASSERT(strncmp(infos[pol_idx].pi_name,
555                                                 tmp.pi_name,
556                                                 NRS_POL_NAME_MAX) == 0);
557                                 LASSERT(strncmp(infos[pol_idx].pi_arg,
558                                                 tmp.pi_arg,
559                                                 sizeof(tmp.pi_arg)) == 0);
560                                 /**
561                                  * Not asserting ptlrpc_nrs_pol_info::pi_state,
562                                  * because it may be different between
563                                  * instances of the same policy in different
564                                  * service partitions.
565                                  */
566                                 LASSERT(infos[pol_idx].pi_fallback ==
567                                         tmp.pi_fallback);
568                         }
569
570                         infos[pol_idx].pi_req_queued += tmp.pi_req_queued;
571                         infos[pol_idx].pi_req_started += tmp.pi_req_started;
572
573                         pol_idx++;
574                 }
575                 spin_unlock(&nrs->nrs_lock);
576         }
577
578         /**
579          * Policy status information output is in YAML format.
580          * For example:
581          *
582          *      regular_requests:
583          *        - name: fifo
584          *          state: started
585          *          fallback: yes
586          *          queued: 0
587          *          active: 0
588          *
589          *        - name: crrn
590          *          state: started
591          *          fallback: no
592          *          queued: 2015
593          *          active: 384
594          *
595          *      high_priority_requests:
596          *        - name: fifo
597          *          state: started
598          *          fallback: yes
599          *          queued: 0
600          *          active: 2
601          *
602          *        - name: crrn
603          *          state: stopped
604          *          fallback: no
605          *          queued: 0
606          *          active: 0
607          */
608         seq_printf(m, "%s\n", !hp ? "\nregular_requests:" :
609                    "high_priority_requests:");
610
611         for (pol_idx = 0; pol_idx < num_pols; pol_idx++) {
612                 if (strlen(infos[pol_idx].pi_arg) > 0)
613                         seq_printf(m, "  - name: %s %s\n",
614                                    infos[pol_idx].pi_name,
615                                    infos[pol_idx].pi_arg);
616                 else
617                         seq_printf(m, "  - name: %s\n",
618                                    infos[pol_idx].pi_name);
619
620
621                 seq_printf(m, "    state: %s\n"
622                            "    fallback: %s\n"
623                            "    queued: %-20d\n"
624                            "    active: %-20d\n\n",
625                            nrs_state2str(infos[pol_idx].pi_state),
626                            infos[pol_idx].pi_fallback ? "yes" : "no",
627                            (int)infos[pol_idx].pi_req_queued,
628                            (int)infos[pol_idx].pi_req_started);
629         }
630
631         if (!hp && nrs_svc_has_hp(svc)) {
632                 memset(infos, 0, num_pols * sizeof(*infos));
633
634                 /**
635                  * Redo the processing for the service's HP NRS heads' policies.
636                  */
637                 hp = true;
638                 goto again;
639         }
640
641 out:
642         if (infos)
643                 OBD_FREE(infos, num_pols * sizeof(*infos));
644
645         mutex_unlock(&nrs_core.nrs_mutex);
646
647         RETURN(rc);
648 }
649
650
651 #define LPROCFS_NRS_WR_MAX_ARG (1024)
652 /**
653  * The longest valid command string is the maxium policy name size, plus the
654  * length of the " reg" substring, plus the lenght of argument
655  */
656 #define LPROCFS_NRS_WR_MAX_CMD  (NRS_POL_NAME_MAX + sizeof(" reg") - 1 \
657                                  + LPROCFS_NRS_WR_MAX_ARG)
658
659 /**
660  * Starts and stops a given policy on a PTLRPC service.
661  *
662  * Commands consist of the policy name, followed by an optional [reg|hp] token;
663  * if the optional token is omitted, the operation is performed on both the
664  * regular and high-priority (if the service has one) NRS head.
665  */
666 static ssize_t
667 ptlrpc_lprocfs_nrs_seq_write(struct file *file, const char __user *buffer,
668                              size_t count, loff_t *off)
669 {
670         struct seq_file                *m = file->private_data;
671         struct ptlrpc_service          *svc = m->private;
672         enum ptlrpc_nrs_queue_type      queue = PTLRPC_NRS_QUEUE_BOTH;
673         char                           *cmd;
674         char                           *cmd_copy = NULL;
675         char                           *policy_name;
676         char                           *queue_name;
677         int                             rc = 0;
678         ENTRY;
679
680         if (count >= LPROCFS_NRS_WR_MAX_CMD)
681                 GOTO(out, rc = -EINVAL);
682
683         OBD_ALLOC(cmd, LPROCFS_NRS_WR_MAX_CMD);
684         if (cmd == NULL)
685                 GOTO(out, rc = -ENOMEM);
686         /**
687          * strsep() modifies its argument, so keep a copy
688          */
689         cmd_copy = cmd;
690
691         if (copy_from_user(cmd, buffer, count))
692                 GOTO(out, rc = -EFAULT);
693
694         cmd[count] = '\0';
695
696         policy_name = strsep(&cmd, " ");
697
698         if (strlen(policy_name) > NRS_POL_NAME_MAX - 1)
699                 GOTO(out, rc = -EINVAL);
700
701         /**
702          * No [reg|hp] token has been specified
703          */
704         if (cmd == NULL)
705                 goto default_queue;
706
707         queue_name = strsep(&cmd, " ");
708         /**
709          * The second token is either an optional [reg|hp] string,
710          * or arguments
711          */
712         if (strcmp(queue_name, "reg") == 0)
713                 queue = PTLRPC_NRS_QUEUE_REG;
714         else if (strcmp(queue_name, "hp") == 0)
715                 queue = PTLRPC_NRS_QUEUE_HP;
716         else {
717                 if (cmd != NULL)
718                         *(cmd - 1) = ' ';
719                 cmd = queue_name;
720         }
721
722 default_queue:
723
724         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
725                 GOTO(out, rc = -ENODEV);
726         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
727                 queue = PTLRPC_NRS_QUEUE_REG;
728
729         /**
730          * Serialize NRS core lprocfs operations with policy registration/
731          * unregistration.
732          */
733         mutex_lock(&nrs_core.nrs_mutex);
734
735         rc = ptlrpc_nrs_policy_control(svc, queue, policy_name,
736                                        PTLRPC_NRS_CTL_START,
737                                        false, cmd);
738
739         mutex_unlock(&nrs_core.nrs_mutex);
740 out:
741         if (cmd_copy)
742                 OBD_FREE(cmd_copy, LPROCFS_NRS_WR_MAX_CMD);
743
744         RETURN(rc < 0 ? rc : count);
745 }
746 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs);
747
748 /** @} nrs */
749
750 struct ptlrpc_srh_iterator {
751         int                     srhi_idx;
752         __u64                   srhi_seq;
753         struct ptlrpc_request   *srhi_req;
754 };
755
756 static int
757 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service_part *svcpt,
758                                     struct ptlrpc_srh_iterator *srhi,
759                                     __u64 seq)
760 {
761         struct list_head        *e;
762         struct ptlrpc_request   *req;
763
764         if (srhi->srhi_req != NULL &&
765             srhi->srhi_seq > svcpt->scp_hist_seq_culled &&
766             srhi->srhi_seq <= seq) {
767                 /* If srhi_req was set previously, hasn't been culled and
768                  * we're searching for a seq on or after it (i.e. more
769                  * recent), search from it onwards.
770                  * Since the service history is LRU (i.e. culled reqs will
771                  * be near the head), we shouldn't have to do long
772                  * re-scans */
773                 LASSERTF(srhi->srhi_seq == srhi->srhi_req->rq_history_seq,
774                          "%s:%d: seek seq %llu, request seq %llu\n",
775                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
776                          srhi->srhi_seq, srhi->srhi_req->rq_history_seq);
777                 LASSERTF(!list_empty(&svcpt->scp_hist_reqs),
778                          "%s:%d: seek offset %llu, request seq %llu, "
779                          "last culled %llu\n",
780                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
781                          seq, srhi->srhi_seq, svcpt->scp_hist_seq_culled);
782                 e = &srhi->srhi_req->rq_history_list;
783         } else {
784                 /* search from start */
785                 e = svcpt->scp_hist_reqs.next;
786         }
787
788         while (e != &svcpt->scp_hist_reqs) {
789                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
790
791                 if (req->rq_history_seq >= seq) {
792                         srhi->srhi_seq = req->rq_history_seq;
793                         srhi->srhi_req = req;
794                         return 0;
795                 }
796                 e = e->next;
797         }
798
799         return -ENOENT;
800 }
801
802 /*
803  * ptlrpc history sequence is used as "position" of seq_file, in some case,
804  * seq_read() will increase "position" to indicate reading the next
805  * element, however, low bits of history sequence are reserved for CPT id
806  * (check the details from comments before ptlrpc_req_add_history), which
807  * means seq_read() might change CPT id of history sequence and never
808  * finish reading of requests on a CPT. To make it work, we have to shift
809  * CPT id to high bits and timestamp to low bits, so seq_read() will only
810  * increase timestamp which can correctly indicate the next position.
811  */
812
813 /* convert seq_file pos to cpt */
814 #define PTLRPC_REQ_POS2CPT(svc, pos)                    \
815         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
816          (__u64)(pos) >> (64 - (svc)->srv_cpt_bits))
817
818 /* make up seq_file pos from cpt */
819 #define PTLRPC_REQ_CPT2POS(svc, cpt)                    \
820         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
821          (cpt) << (64 - (svc)->srv_cpt_bits))
822
823 /* convert sequence to position */
824 #define PTLRPC_REQ_SEQ2POS(svc, seq)                    \
825         ((svc)->srv_cpt_bits == 0 ? (seq) :             \
826          ((seq) >> (svc)->srv_cpt_bits) |               \
827          ((seq) << (64 - (svc)->srv_cpt_bits)))
828
829 /* convert position to sequence */
830 #define PTLRPC_REQ_POS2SEQ(svc, pos)                    \
831         ((svc)->srv_cpt_bits == 0 ? (pos) :             \
832          ((__u64)(pos) << (svc)->srv_cpt_bits) |        \
833          ((__u64)(pos) >> (64 - (svc)->srv_cpt_bits)))
834
835 static void *
836 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
837 {
838         struct ptlrpc_service           *svc = s->private;
839         struct ptlrpc_service_part      *svcpt;
840         struct ptlrpc_srh_iterator      *srhi;
841         unsigned int                    cpt;
842         int                             rc;
843         int                             i;
844
845         if (sizeof(loff_t) != sizeof(__u64)) { /* can't support */
846                 CWARN("Failed to read request history because size of loff_t "
847                       "%d can't match size of u64\n", (int)sizeof(loff_t));
848                 return NULL;
849         }
850
851         OBD_ALLOC(srhi, sizeof(*srhi));
852         if (srhi == NULL)
853                 return NULL;
854
855         srhi->srhi_seq = 0;
856         srhi->srhi_req = NULL;
857
858         cpt = PTLRPC_REQ_POS2CPT(svc, *pos);
859
860         ptlrpc_service_for_each_part(svcpt, i, svc) {
861                 if (i < cpt) /* skip */
862                         continue;
863                 if (i > cpt) /* make up the lowest position for this CPT */
864                         *pos = PTLRPC_REQ_CPT2POS(svc, i);
865
866                 spin_lock(&svcpt->scp_lock);
867                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi,
868                                 PTLRPC_REQ_POS2SEQ(svc, *pos));
869                 spin_unlock(&svcpt->scp_lock);
870                 if (rc == 0) {
871                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
872                         srhi->srhi_idx = i;
873                         return srhi;
874                 }
875         }
876
877         OBD_FREE(srhi, sizeof(*srhi));
878         return NULL;
879 }
880
881 static void
882 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
883 {
884         struct ptlrpc_srh_iterator *srhi = iter;
885
886         if (srhi != NULL)
887                 OBD_FREE(srhi, sizeof(*srhi));
888 }
889
890 static void *
891 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
892                                     void *iter, loff_t *pos)
893 {
894         struct ptlrpc_service           *svc = s->private;
895         struct ptlrpc_srh_iterator      *srhi = iter;
896         struct ptlrpc_service_part      *svcpt;
897         __u64                           seq;
898         int                             rc;
899         int                             i;
900
901         for (i = srhi->srhi_idx; i < svc->srv_ncpts; i++) {
902                 svcpt = svc->srv_parts[i];
903
904                 if (i > srhi->srhi_idx) { /* reset iterator for a new CPT */
905                         srhi->srhi_req = NULL;
906                         seq = srhi->srhi_seq = 0;
907                 } else { /* the next sequence */
908                         seq = srhi->srhi_seq + (1 << svc->srv_cpt_bits);
909                 }
910
911                 spin_lock(&svcpt->scp_lock);
912                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, seq);
913                 spin_unlock(&svcpt->scp_lock);
914                 if (rc == 0) {
915                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
916                         srhi->srhi_idx = i;
917                         return srhi;
918                 }
919         }
920
921         OBD_FREE(srhi, sizeof(*srhi));
922         return NULL;
923 }
924
925 /* common ost/mdt so_req_printer */
926 void target_print_req(void *seq_file, struct ptlrpc_request *req)
927 {
928         /* Called holding srv_lock with irqs disabled.
929          * Print specific req contents and a newline.
930          * CAVEAT EMPTOR: check request message length before printing!!!
931          * You might have received any old crap so you must be just as
932          * careful here as the service's request parser!!! */
933         struct seq_file *sf = seq_file;
934
935         switch (req->rq_phase) {
936         case RQ_PHASE_NEW:
937                 /* still awaiting a service thread's attention, or rejected
938                  * because the generic request message didn't unpack */
939                 seq_printf(sf, "<not swabbed>\n");
940                 break;
941         case RQ_PHASE_INTERPRET:
942                 /* being handled, so basic msg swabbed, and opc is valid
943                  * but racing with mds_handle() */
944         case RQ_PHASE_COMPLETE:
945                 /* been handled by mds_handle() reply state possibly still
946                  * volatile */
947                 seq_printf(sf, "opc %d\n", lustre_msg_get_opc(req->rq_reqmsg));
948                 break;
949         default:
950                 DEBUG_REQ(D_ERROR, req, "bad phase %d", req->rq_phase);
951         }
952 }
953 EXPORT_SYMBOL(target_print_req);
954
955 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
956 {
957         struct ptlrpc_service           *svc = s->private;
958         struct ptlrpc_srh_iterator      *srhi = iter;
959         struct ptlrpc_service_part      *svcpt;
960         struct ptlrpc_request           *req;
961         int                             rc;
962
963         LASSERT(srhi->srhi_idx < svc->srv_ncpts);
964
965         svcpt = svc->srv_parts[srhi->srhi_idx];
966
967         spin_lock(&svcpt->scp_lock);
968
969         rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, srhi->srhi_seq);
970
971         if (rc == 0) {
972                 struct timespec64 arrival, sent, arrivaldiff;
973                 char nidstr[LNET_NIDSTR_SIZE];
974
975                 req = srhi->srhi_req;
976
977                 libcfs_nid2str_r(req->rq_self, nidstr, sizeof(nidstr));
978                 arrival.tv_sec = req->rq_arrival_time.tv_sec;
979                 arrival.tv_nsec = req->rq_arrival_time.tv_nsec;
980                 sent.tv_sec = req->rq_sent;
981                 sent.tv_nsec = 0;
982                 arrivaldiff = timespec64_sub(sent, arrival);
983
984                 /* Print common req fields.
985                  * CAVEAT EMPTOR: we're racing with the service handler
986                  * here.  The request could contain any old crap, so you
987                  * must be just as careful as the service's request
988                  * parser. Currently I only print stuff here I know is OK
989                  * to look at coz it was set up in request_in_callback()!!!
990                  */
991                 seq_printf(s, "%lld:%s:%s:x%llu:%d:%s:%lld.%06lld:%lld.%06llds(%+lld.0s) ",
992                            req->rq_history_seq, nidstr,
993                            libcfs_id2str(req->rq_peer), req->rq_xid,
994                            req->rq_reqlen, ptlrpc_rqphase2str(req),
995                            (s64)req->rq_arrival_time.tv_sec,
996                            (s64)(req->rq_arrival_time.tv_nsec / NSEC_PER_USEC),
997                            (s64)arrivaldiff.tv_sec,
998                            (s64)(arrivaldiff.tv_nsec / NSEC_PER_USEC),
999                            (s64)(req->rq_sent - req->rq_deadline));
1000                 if (svc->srv_ops.so_req_printer == NULL)
1001                         seq_printf(s, "\n");
1002                 else
1003                         svc->srv_ops.so_req_printer(s, srhi->srhi_req);
1004         }
1005
1006         spin_unlock(&svcpt->scp_lock);
1007         return rc;
1008 }
1009
1010 static int
1011 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
1012 {
1013         static struct seq_operations sops = {
1014                 .start = ptlrpc_lprocfs_svc_req_history_start,
1015                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
1016                 .next  = ptlrpc_lprocfs_svc_req_history_next,
1017                 .show  = ptlrpc_lprocfs_svc_req_history_show,
1018         };
1019         struct seq_file *seqf;
1020         int             rc;
1021
1022         rc = LPROCFS_ENTRY_CHECK(inode);
1023         if (rc < 0)
1024                 return rc;
1025
1026         rc = seq_open(file, &sops);
1027         if (rc)
1028                 return rc;
1029
1030         seqf = file->private_data;
1031         seqf->private = PDE_DATA(inode);
1032         return 0;
1033 }
1034
1035 /* See also lprocfs_rd_timeouts */
1036 static int ptlrpc_lprocfs_timeouts_seq_show(struct seq_file *m, void *n)
1037 {
1038         struct ptlrpc_service           *svc = m->private;
1039         struct ptlrpc_service_part      *svcpt;
1040         time64_t worstt;
1041         unsigned int                    cur;
1042         unsigned int                    worst;
1043         int                             i;
1044
1045         if (AT_OFF) {
1046                 seq_printf(m, "adaptive timeouts off, using obd_timeout %u\n",
1047                            obd_timeout);
1048                 return 0;
1049         }
1050
1051         ptlrpc_service_for_each_part(svcpt, i, svc) {
1052                 cur     = at_get(&svcpt->scp_at_estimate);
1053                 worst   = svcpt->scp_at_estimate.at_worst_ever;
1054                 worstt  = svcpt->scp_at_estimate.at_worst_time;
1055
1056                 seq_printf(m, "%10s : cur %3u  worst %3u (at %lld, %llds ago) ",
1057                            "service", cur, worst, (s64)worstt,
1058                            (s64)(ktime_get_real_seconds() - worstt));
1059
1060                 lprocfs_at_hist_helper(m, &svcpt->scp_at_estimate);
1061         }
1062
1063         return 0;
1064 }
1065 LPROC_SEQ_FOPS_RO(ptlrpc_lprocfs_timeouts);
1066
1067 static ssize_t high_priority_ratio_show(struct kobject *kobj,
1068                                         struct attribute *attr,
1069                                         char *buf)
1070 {
1071         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1072                                                   srv_kobj);
1073
1074         return sprintf(buf, "%d\n", svc->srv_hpreq_ratio);
1075 }
1076
1077 static ssize_t high_priority_ratio_store(struct kobject *kobj,
1078                                          struct attribute *attr,
1079                                          const char *buffer,
1080                                          size_t count)
1081 {
1082         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1083                                                   srv_kobj);
1084         int rc;
1085         unsigned long val;
1086
1087         rc = kstrtoul(buffer, 10, &val);
1088         if (rc < 0)
1089                 return rc;
1090
1091         spin_lock(&svc->srv_lock);
1092         svc->srv_hpreq_ratio = val;
1093         spin_unlock(&svc->srv_lock);
1094
1095         return count;
1096 }
1097 LUSTRE_RW_ATTR(high_priority_ratio);
1098
1099 static struct attribute *ptlrpc_svc_attrs[] = {
1100         &lustre_attr_threads_min.attr,
1101         &lustre_attr_threads_started.attr,
1102         &lustre_attr_threads_max.attr,
1103         &lustre_attr_high_priority_ratio.attr,
1104         NULL,
1105 };
1106
1107 static void ptlrpc_sysfs_svc_release(struct kobject *kobj)
1108 {
1109         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1110                                                   srv_kobj);
1111
1112         complete(&svc->srv_kobj_unregister);
1113 }
1114
1115 static struct kobj_type ptlrpc_svc_ktype = {
1116         .default_attrs  = ptlrpc_svc_attrs,
1117         .sysfs_ops      = &lustre_sysfs_ops,
1118         .release        = ptlrpc_sysfs_svc_release,
1119 };
1120
1121 void ptlrpc_sysfs_unregister_service(struct ptlrpc_service *svc)
1122 {
1123         /* Let's see if we had a chance at initialization first */
1124         if (svc->srv_kobj.kset) {
1125                 kobject_put(&svc->srv_kobj);
1126                 wait_for_completion(&svc->srv_kobj_unregister);
1127         }
1128 }
1129
1130 int ptlrpc_sysfs_register_service(struct kset *parent,
1131                                   struct ptlrpc_service *svc)
1132 {
1133         svc->srv_kobj.kset = parent;
1134         init_completion(&svc->srv_kobj_unregister);
1135         return kobject_init_and_add(&svc->srv_kobj, &ptlrpc_svc_ktype,
1136                                     &parent->kobj, "%s", svc->srv_name);
1137 }
1138
1139 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
1140                                      struct ptlrpc_service *svc)
1141 {
1142         struct lprocfs_vars lproc_vars[] = {
1143                 { .name = "req_buffer_history_len",
1144                   .fops = &ptlrpc_lprocfs_req_history_len_fops,
1145                   .data = svc },
1146                 { .name = "req_buffer_history_max",
1147                   .fops = &ptlrpc_lprocfs_req_history_max_fops,
1148                   .data = svc },
1149                 { .name = "timeouts",
1150                   .fops = &ptlrpc_lprocfs_timeouts_fops,
1151                   .data = svc },
1152                 { .name = "nrs_policies",
1153                   .fops = &ptlrpc_lprocfs_nrs_fops,
1154                   .data = svc },
1155                 { NULL }
1156         };
1157         static struct file_operations req_history_fops = {
1158                 .owner       = THIS_MODULE,
1159                 .open        = ptlrpc_lprocfs_svc_req_history_open,
1160                 .read        = seq_read,
1161                 .llseek      = seq_lseek,
1162                 .release     = lprocfs_seq_release,
1163         };
1164
1165         int rc;
1166
1167         ptlrpc_lprocfs_register(entry, svc->srv_name,
1168                                 "stats", &svc->srv_procroot,
1169                                 &svc->srv_stats);
1170         if (svc->srv_procroot == NULL)
1171                 return;
1172
1173         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
1174
1175         rc = lprocfs_seq_create(svc->srv_procroot, "req_history",
1176                                 0400, &req_history_fops, svc);
1177         if (rc)
1178                 CWARN("Error adding the req_history file\n");
1179 }
1180
1181 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
1182 {
1183         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
1184                                 &obddev->obd_svc_procroot,
1185                                 &obddev->obd_svc_stats);
1186 }
1187 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
1188
1189 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req, long amount)
1190 {
1191         struct lprocfs_stats *svc_stats;
1192         __u32 op = lustre_msg_get_opc(req->rq_reqmsg);
1193         int opc = opcode_offset(op);
1194
1195         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1196         if (svc_stats == NULL || opc <= 0)
1197                 return;
1198         LASSERT(opc < LUSTRE_MAX_OPCODES);
1199         if (!(op == LDLM_ENQUEUE || op == MDS_REINT))
1200                 lprocfs_counter_add(svc_stats, opc + EXTRA_MAX_OPCODES, amount);
1201 }
1202
1203 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes)
1204 {
1205         struct lprocfs_stats *svc_stats;
1206         int idx;
1207
1208         if (!req->rq_import)
1209                 return;
1210         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1211         if (!svc_stats)
1212                 return;
1213         idx = lustre_msg_get_opc(req->rq_reqmsg);
1214         switch (idx) {
1215         case OST_READ:
1216                 idx = BRW_READ_BYTES + PTLRPC_LAST_CNTR;
1217                 break;
1218         case OST_WRITE:
1219                 idx = BRW_WRITE_BYTES + PTLRPC_LAST_CNTR;
1220                 break;
1221         default:
1222                 LASSERTF(0, "unsupported opcode %u\n", idx);
1223                 break;
1224         }
1225
1226         lprocfs_counter_add(svc_stats, idx, bytes);
1227 }
1228
1229 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
1230
1231 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
1232 {
1233         if (svc->srv_procroot != NULL)
1234                 lprocfs_remove(&svc->srv_procroot);
1235
1236         if (svc->srv_stats)
1237                 lprocfs_free_stats(&svc->srv_stats);
1238 }
1239
1240 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
1241 {
1242         if (obd->obd_svc_procroot)
1243                 lprocfs_remove(&obd->obd_svc_procroot);
1244
1245         if (obd->obd_svc_stats)
1246                 lprocfs_free_stats(&obd->obd_svc_stats);
1247 }
1248 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
1249
1250 ssize_t
1251 lprocfs_ping_seq_write(struct file *file, const char __user *buffer,
1252                        size_t count, loff_t *off)
1253 {
1254         struct seq_file         *m = file->private_data;
1255         struct obd_device       *obd = m->private;
1256         struct ptlrpc_request   *req;
1257         int                     rc;
1258         ENTRY;
1259
1260         LPROCFS_CLIMP_CHECK(obd);
1261         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
1262         LPROCFS_CLIMP_EXIT(obd);
1263         if (req == NULL)
1264                 RETURN(-ENOMEM);
1265
1266         req->rq_send_state = LUSTRE_IMP_FULL;
1267
1268         rc = ptlrpc_queue_wait(req);
1269
1270         ptlrpc_req_finished(req);
1271         if (rc >= 0)
1272                 RETURN(count);
1273         RETURN(rc);
1274 }
1275 EXPORT_SYMBOL(lprocfs_ping_seq_write);
1276
1277 /* Write the connection UUID to this file to attempt to connect to that node.
1278  * The connection UUID is a node's primary NID. For example,
1279  * "echo connection=192.168.0.1@tcp0::instance > .../import".
1280  */
1281 ssize_t
1282 lprocfs_import_seq_write(struct file *file, const char __user *buffer,
1283                          size_t count, loff_t *off)
1284 {
1285         struct seq_file   *m    = file->private_data;
1286         struct obd_device *obd  = m->private;
1287         struct obd_import *imp  = obd->u.cli.cl_import;
1288         char *kbuf = NULL;
1289         char *uuid;
1290         char *ptr;
1291         int do_reconn = 1;
1292         const char prefix[] = "connection=";
1293         const int prefix_len = sizeof(prefix) - 1;
1294
1295         if (count > PAGE_SIZE - 1 || count <= prefix_len)
1296                 return -EINVAL;
1297
1298         OBD_ALLOC(kbuf, count + 1);
1299         if (kbuf == NULL)
1300                 return -ENOMEM;
1301
1302         if (copy_from_user(kbuf, buffer, count))
1303                 GOTO(out, count = -EFAULT);
1304
1305         kbuf[count] = 0;
1306
1307         /* only support connection=uuid::instance now */
1308         if (strncmp(prefix, kbuf, prefix_len) != 0)
1309                 GOTO(out, count = -EINVAL);
1310
1311         uuid = kbuf + prefix_len;
1312         ptr = strstr(uuid, "::");
1313         if (ptr) {
1314                 __u32 inst;
1315                 char *endptr;
1316
1317                 *ptr = 0;
1318                 do_reconn = 0;
1319                 ptr += 2; /* Skip :: */
1320                 inst = simple_strtol(ptr, &endptr, 10);
1321                 if (*endptr) {
1322                         CERROR("config: wrong instance # %s\n", ptr);
1323                 } else if (inst != imp->imp_connect_data.ocd_instance) {
1324                         CDEBUG(D_INFO, "IR: %s is connecting to an obsoleted "
1325                                "target(%u/%u), reconnecting...\n",
1326                                imp->imp_obd->obd_name,
1327                                imp->imp_connect_data.ocd_instance, inst);
1328                         do_reconn = 1;
1329                 } else {
1330                         CDEBUG(D_INFO, "IR: %s has already been connecting to "
1331                                "new target(%u)\n",
1332                                imp->imp_obd->obd_name, inst);
1333                 }
1334         }
1335
1336         if (do_reconn)
1337                 ptlrpc_recover_import(imp, uuid, 1);
1338
1339 out:
1340         OBD_FREE(kbuf, count + 1);
1341         return count;
1342 }
1343 EXPORT_SYMBOL(lprocfs_import_seq_write);
1344
1345 int lprocfs_pinger_recov_seq_show(struct seq_file *m, void *n)
1346 {
1347         struct obd_device *obd = m->private;
1348         struct obd_import *imp = obd->u.cli.cl_import;
1349
1350         LPROCFS_CLIMP_CHECK(obd);
1351         seq_printf(m, "%d\n", !imp->imp_no_pinger_recover);
1352         LPROCFS_CLIMP_EXIT(obd);
1353         return 0;
1354 }
1355 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_show);
1356
1357 ssize_t
1358 lprocfs_pinger_recov_seq_write(struct file *file, const char __user *buffer,
1359                                size_t count, loff_t *off)
1360 {
1361         struct seq_file *m = file->private_data;
1362         struct obd_device *obd = m->private;
1363         struct client_obd *cli = &obd->u.cli;
1364         struct obd_import *imp = cli->cl_import;
1365         int rc;
1366         __s64 val;
1367
1368         rc = lprocfs_str_to_s64(buffer, count, &val);
1369         if (rc < 0)
1370                 return rc;
1371
1372         if (val != 0 && val != 1)
1373                 return -ERANGE;
1374
1375         LPROCFS_CLIMP_CHECK(obd);
1376         spin_lock(&imp->imp_lock);
1377         imp->imp_no_pinger_recover = !val;
1378         spin_unlock(&imp->imp_lock);
1379         LPROCFS_CLIMP_EXIT(obd);
1380         return count;
1381 }
1382 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_write);
1383
1384 #endif /* CONFIG_PROC_FS */