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