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