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