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