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