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