Whamcloud - gitweb
LU-6142 lustre: change super/file/inode operations to const
[fs/lustre-release.git] / lustre / ptlrpc / lproc_ptlrpc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34
35 #include <obd_support.h>
36 #include <obd.h>
37 #include <lprocfs_status.h>
38 #include <lustre_net.h>
39 #include <obd_class.h>
40 #include "ptlrpc_internal.h"
41
42
43 static struct ll_rpc_opcode {
44         __u32       opcode;
45         const char *opname;
46 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
47         { OST_REPLY,        "ost_reply" },
48         { OST_GETATTR,      "ost_getattr" },
49         { OST_SETATTR,      "ost_setattr" },
50         { OST_READ,         "ost_read" },
51         { OST_WRITE,        "ost_write" },
52         { OST_CREATE ,      "ost_create" },
53         { OST_DESTROY,      "ost_destroy" },
54         { OST_GET_INFO,     "ost_get_info" },
55         { OST_CONNECT,      "ost_connect" },
56         { OST_DISCONNECT,   "ost_disconnect" },
57         { OST_PUNCH,        "ost_punch" },
58         { OST_OPEN,         "ost_open" },
59         { OST_CLOSE,        "ost_close" },
60         { OST_STATFS,       "ost_statfs" },
61         { 14,                NULL },    /* formerly OST_SAN_READ */
62         { 15,                NULL },    /* formerly OST_SAN_WRITE */
63         { OST_SYNC,         "ost_sync" },
64         { OST_SET_INFO,     "ost_set_info" },
65         { OST_QUOTACHECK,   "ost_quotacheck" },
66         { OST_QUOTACTL,     "ost_quotactl" },
67         { OST_QUOTA_ADJUST_QUNIT, "ost_quota_adjust_qunit" },
68         { OST_LADVISE,      "ost_ladvise" },
69         { OST_FALLOCATE,    "ost_fallocate" },
70         { OST_SEEK,         "ost_seek" },
71         { MDS_GETATTR,      "mds_getattr" },
72         { MDS_GETATTR_NAME, "mds_getattr_lock" },
73         { MDS_CLOSE,        "mds_close" },
74         { MDS_REINT,        "mds_reint" },
75         { MDS_READPAGE,     "mds_readpage" },
76         { MDS_CONNECT,      "mds_connect" },
77         { MDS_DISCONNECT,   "mds_disconnect" },
78         { MDS_GET_ROOT,     "mds_get_root" },
79         { MDS_STATFS,       "mds_statfs" },
80         { MDS_PIN,          "mds_pin" },
81         { MDS_UNPIN,        "mds_unpin" },
82         { MDS_SYNC,         "mds_sync" },
83         { MDS_DONE_WRITING, "mds_done_writing" },
84         { MDS_SET_INFO,     "mds_set_info" },
85         { MDS_QUOTACHECK,   "mds_quotacheck" },
86         { MDS_QUOTACTL,     "mds_quotactl" },
87         { MDS_GETXATTR,     "mds_getxattr" },
88         { MDS_SETXATTR,     "mds_setxattr" },
89         { MDS_WRITEPAGE,    "mds_writepage" },
90         { MDS_IS_SUBDIR,    "mds_is_subdir" },
91         { MDS_GET_INFO,     "mds_get_info" },
92         { MDS_HSM_STATE_GET, "mds_hsm_state_get" },
93         { MDS_HSM_STATE_SET, "mds_hsm_state_set" },
94         { MDS_HSM_ACTION,   "mds_hsm_action" },
95         { MDS_HSM_PROGRESS, "mds_hsm_progress" },
96         { MDS_HSM_REQUEST,  "mds_hsm_request" },
97         { MDS_HSM_CT_REGISTER, "mds_hsm_ct_register" },
98         { MDS_HSM_CT_UNREGISTER, "mds_hsm_ct_unregister" },
99         { MDS_SWAP_LAYOUTS,     "mds_swap_layouts" },
100         { MDS_RMFID,        "mds_rmfid" },
101         { LDLM_ENQUEUE,     "ldlm_enqueue" },
102         { LDLM_CONVERT,     "ldlm_convert" },
103         { LDLM_CANCEL,      "ldlm_cancel" },
104         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
105         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
106         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
107         { LDLM_SET_INFO,    "ldlm_set_info" },
108         { MGS_CONNECT,      "mgs_connect" },
109         { MGS_DISCONNECT,   "mgs_disconnect" },
110         { MGS_EXCEPTION,    "mgs_exception" },
111         { MGS_TARGET_REG,   "mgs_target_reg" },
112         { MGS_TARGET_DEL,   "mgs_target_del" },
113         { MGS_SET_INFO,     "mgs_set_info" },
114         { MGS_CONFIG_READ,  "mgs_config_read" },
115         { OBD_PING,                      "obd_ping" },
116         { 401, /* was OBD_LOG_CANCEL */ "llog_cancel" },
117         { 402, /* was OBD_QC_CALLBACK */ "obd_quota_callback" },
118         { OBD_IDX_READ, "dt_index_read" },
119         { LLOG_ORIGIN_HANDLE_CREATE, "llog_origin_handle_open" },
120         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block" },
121         { LLOG_ORIGIN_HANDLE_READ_HEADER, "llog_origin_handle_read_header" },
122         { 504, /*LLOG_ORIGIN_HANDLE_WRITE_REC*/"llog_origin_handle_write_rec" },
123         { 505, /* was LLOG_ORIGIN_HANDLE_CLOSE */ "llog_origin_handle_close" },
124         { 506, /* was LLOG_ORIGIN_CONNECT */ "llog_origin_connect" },
125         { 507, /* was LLOG_CATINFO */ "llog_catinfo" },
126         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
127         { LLOG_ORIGIN_HANDLE_DESTROY,    "llog_origin_handle_destroy" },
128         { QUOTA_DQACQ,      "quota_acquire" },
129         { QUOTA_DQREL,      "quota_release" },
130         { SEQ_QUERY,        "seq_query" },
131         { SEC_CTX_INIT,     "sec_ctx_init" },
132         { SEC_CTX_INIT_CONT, "sec_ctx_init_cont" },
133         { SEC_CTX_FINI,     "sec_ctx_fini" },
134         { FLD_QUERY,        "fld_query" },
135         { FLD_READ,         "fld_read" },
136         { OUT_UPDATE,       "out_update" },
137         { LFSCK_NOTIFY,     "lfsck_notify" },
138         { LFSCK_QUERY,      "lfsck_query" },
139 };
140
141 static struct ll_eopcode {
142         __u32       opcode;
143         const char *opname;
144 } ll_eopcode_table[EXTRA_LAST_OPC] = {
145         { LDLM_GLIMPSE_ENQUEUE, "ldlm_glimpse_enqueue" },
146         { LDLM_PLAIN_ENQUEUE,   "ldlm_plain_enqueue" },
147         { LDLM_EXTENT_ENQUEUE,  "ldlm_extent_enqueue" },
148         { LDLM_FLOCK_ENQUEUE,   "ldlm_flock_enqueue" },
149         { LDLM_IBITS_ENQUEUE,   "ldlm_ibits_enqueue" },
150         { MDS_REINT_SETATTR,    "mds_reint_setattr" },
151         { MDS_REINT_CREATE,     "mds_reint_create" },
152         { MDS_REINT_LINK,       "mds_reint_link" },
153         { MDS_REINT_UNLINK,     "mds_reint_unlink" },
154         { MDS_REINT_RENAME,     "mds_reint_rename" },
155         { MDS_REINT_OPEN,       "mds_reint_open" },
156         { MDS_REINT_SETXATTR,   "mds_reint_setxattr" },
157         { MDS_REINT_RESYNC,     "mds_reint_resync" },
158         { BRW_READ_BYTES,       "read_bytes" },
159         { BRW_WRITE_BYTES,      "write_bytes" },
160 };
161
162 const char *ll_opcode2str(__u32 opcode)
163 {
164         /* When one of the assertions below fail, chances are that:
165          *     1) A new opcode was added in include/lustre/lustre_idl.h,
166          *        but is missing from the table above.
167          * or  2) The opcode space was renumbered or rearranged,
168          *        and the opcode_offset() function in
169          *        ptlrpc_internal.h needs to be modified.
170          */
171         __u32 offset = opcode_offset(opcode);
172         LASSERTF(offset < LUSTRE_MAX_OPCODES,
173                  "offset %u >= LUSTRE_MAX_OPCODES %u\n",
174                  offset, LUSTRE_MAX_OPCODES);
175         LASSERTF(ll_rpc_opcode_table[offset].opcode == opcode,
176                  "ll_rpc_opcode_table[%u].opcode %u != opcode %u\n",
177                  offset, ll_rpc_opcode_table[offset].opcode, opcode);
178         return ll_rpc_opcode_table[offset].opname;
179 }
180
181 const int ll_str2opcode(const char *ops)
182 {
183         int i;
184
185         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
186                 if (ll_rpc_opcode_table[i].opname != NULL &&
187                     strcmp(ll_rpc_opcode_table[i].opname, ops) == 0)
188                         return ll_rpc_opcode_table[i].opcode;
189         }
190
191         return -EINVAL;
192 }
193
194 static const char *ll_eopcode2str(__u32 opcode)
195 {
196         LASSERT(ll_eopcode_table[opcode].opcode == opcode);
197         return ll_eopcode_table[opcode].opname;
198 }
199
200 static void
201 ptlrpc_ldebugfs_register(struct dentry *root, char *dir, char *name,
202                          struct dentry **debugfs_root_ret,
203                          struct lprocfs_stats **stats_ret)
204 {
205         struct dentry *svc_debugfs_entry;
206         struct lprocfs_stats *svc_stats;
207         int i;
208         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
209                                           LPROCFS_CNTR_STDDEV;
210
211         LASSERT(!*debugfs_root_ret);
212         LASSERT(!*stats_ret);
213
214         svc_stats = lprocfs_alloc_stats(EXTRA_MAX_OPCODES + LUSTRE_MAX_OPCODES,
215                                         0);
216         if (!svc_stats)
217                 return;
218
219         if (dir)
220                 svc_debugfs_entry = debugfs_create_dir(dir, root);
221         else
222                 svc_debugfs_entry = root;
223
224         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
225                              svc_counter_config, "req_waittime", "usec");
226         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
227                              svc_counter_config, "req_qdepth", "reqs");
228         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
229                              svc_counter_config, "req_active", "reqs");
230         lprocfs_counter_init(svc_stats, PTLRPC_TIMEOUT,
231                              svc_counter_config, "req_timeout", "sec");
232         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
233                              svc_counter_config, "reqbuf_avail", "bufs");
234         for (i = 0; i < EXTRA_LAST_OPC; i++) {
235                 char *units;
236
237                 switch (i) {
238                 case BRW_WRITE_BYTES:
239                 case BRW_READ_BYTES:
240                         units = "bytes";
241                         break;
242                 default:
243                         units = "reqs";
244                         break;
245                 }
246                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
247                                      svc_counter_config,
248                                      ll_eopcode2str(i), units);
249         }
250         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
251                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
252                 lprocfs_counter_init(svc_stats,
253                                      EXTRA_MAX_OPCODES + i, svc_counter_config,
254                                      ll_opcode2str(opcode), "usec");
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_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         return 0;
278 }
279
280
281 LDEBUGFS_SEQ_FOPS_RO(ptlrpc_lprocfs_req_history_len);
282
283 static int
284 ptlrpc_lprocfs_req_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_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_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 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_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                        num_pols;
540         unsigned                        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                                 LASSERT(strncmp(infos[pol_idx].pi_name,
597                                                 tmp.pi_name,
598                                                 NRS_POL_NAME_MAX) == 0);
599                                 LASSERT(strncmp(infos[pol_idx].pi_arg,
600                                                 tmp.pi_arg,
601                                                 sizeof(tmp.pi_arg)) == 0);
602                                 /**
603                                  * Not asserting ptlrpc_nrs_pol_info::pi_state,
604                                  * because it may be different between
605                                  * instances of the same policy in different
606                                  * service partitions.
607                                  */
608                                 LASSERT(infos[pol_idx].pi_fallback ==
609                                         tmp.pi_fallback);
610                         }
611
612                         infos[pol_idx].pi_req_queued += tmp.pi_req_queued;
613                         infos[pol_idx].pi_req_started += tmp.pi_req_started;
614
615                         pol_idx++;
616                 }
617                 spin_unlock(&nrs->nrs_lock);
618         }
619
620         /**
621          * Policy status information output is in YAML format.
622          * For example:
623          *
624          *      regular_requests:
625          *        - name: fifo
626          *          state: started
627          *          fallback: yes
628          *          queued: 0
629          *          active: 0
630          *
631          *        - name: crrn
632          *          state: started
633          *          fallback: no
634          *          queued: 2015
635          *          active: 384
636          *
637          *      high_priority_requests:
638          *        - name: fifo
639          *          state: started
640          *          fallback: yes
641          *          queued: 0
642          *          active: 2
643          *
644          *        - name: crrn
645          *          state: stopped
646          *          fallback: no
647          *          queued: 0
648          *          active: 0
649          */
650         seq_printf(m, "%s\n", !hp ? "\nregular_requests:" :
651                    "high_priority_requests:");
652
653         for (pol_idx = 0; pol_idx < num_pols; pol_idx++) {
654                 if (strlen(infos[pol_idx].pi_arg) > 0)
655                         seq_printf(m, "  - name: %s %s\n",
656                                    infos[pol_idx].pi_name,
657                                    infos[pol_idx].pi_arg);
658                 else
659                         seq_printf(m, "  - name: %s\n",
660                                    infos[pol_idx].pi_name);
661
662
663                 seq_printf(m, "    state: %s\n"
664                            "    fallback: %s\n"
665                            "    queued: %-20d\n"
666                            "    active: %-20d\n\n",
667                            nrs_state2str(infos[pol_idx].pi_state),
668                            infos[pol_idx].pi_fallback ? "yes" : "no",
669                            (int)infos[pol_idx].pi_req_queued,
670                            (int)infos[pol_idx].pi_req_started);
671         }
672
673         if (!hp && nrs_svc_has_hp(svc)) {
674                 memset(infos, 0, num_pols * sizeof(*infos));
675
676                 /**
677                  * Redo the processing for the service's HP NRS heads' policies.
678                  */
679                 hp = true;
680                 goto again;
681         }
682
683 out:
684         if (infos)
685                 OBD_FREE_PTR_ARRAY(infos, num_pols);
686
687         mutex_unlock(&nrs_core.nrs_mutex);
688
689         RETURN(rc);
690 }
691
692
693 #define LPROCFS_NRS_WR_MAX_ARG (1024)
694 /**
695  * The longest valid command string is the maxium policy name size, plus the
696  * length of the " reg" substring, plus the lenght of argument
697  */
698 #define LPROCFS_NRS_WR_MAX_CMD  (NRS_POL_NAME_MAX + sizeof(" reg") - 1 \
699                                  + LPROCFS_NRS_WR_MAX_ARG)
700
701 /**
702  * Starts and stops a given policy on a PTLRPC service.
703  *
704  * Commands consist of the policy name, followed by an optional [reg|hp] token;
705  * if the optional token is omitted, the operation is performed on both the
706  * regular and high-priority (if the service has one) NRS head.
707  */
708 static ssize_t
709 ptlrpc_lprocfs_nrs_seq_write(struct file *file, const char __user *buffer,
710                              size_t count, loff_t *off)
711 {
712         struct seq_file                *m = file->private_data;
713         struct ptlrpc_service          *svc = m->private;
714         enum ptlrpc_nrs_queue_type      queue = PTLRPC_NRS_QUEUE_BOTH;
715         char                           *cmd;
716         char                           *cmd_copy = NULL;
717         char                           *policy_name;
718         char                           *queue_name;
719         int                             rc = 0;
720         ENTRY;
721
722         if (count >= LPROCFS_NRS_WR_MAX_CMD)
723                 GOTO(out, rc = -EINVAL);
724
725         OBD_ALLOC(cmd, LPROCFS_NRS_WR_MAX_CMD);
726         if (cmd == NULL)
727                 GOTO(out, rc = -ENOMEM);
728         /**
729          * strsep() modifies its argument, so keep a copy
730          */
731         cmd_copy = cmd;
732
733         if (copy_from_user(cmd, buffer, count))
734                 GOTO(out, rc = -EFAULT);
735
736         cmd[count] = '\0';
737
738         policy_name = strsep(&cmd, " ");
739
740         if (strlen(policy_name) > NRS_POL_NAME_MAX - 1)
741                 GOTO(out, rc = -EINVAL);
742
743         /**
744          * No [reg|hp] token has been specified
745          */
746         if (cmd == NULL)
747                 goto default_queue;
748
749         queue_name = strsep(&cmd, " ");
750         /**
751          * The second token is either an optional [reg|hp] string,
752          * or arguments
753          */
754         if (strcmp(queue_name, "reg") == 0)
755                 queue = PTLRPC_NRS_QUEUE_REG;
756         else if (strcmp(queue_name, "hp") == 0)
757                 queue = PTLRPC_NRS_QUEUE_HP;
758         else {
759                 if (cmd != NULL)
760                         *(cmd - 1) = ' ';
761                 cmd = queue_name;
762         }
763
764 default_queue:
765
766         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
767                 GOTO(out, rc = -ENODEV);
768         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
769                 queue = PTLRPC_NRS_QUEUE_REG;
770
771         /**
772          * Serialize NRS core lprocfs operations with policy registration/
773          * unregistration.
774          */
775         mutex_lock(&nrs_core.nrs_mutex);
776
777         rc = ptlrpc_nrs_policy_control(svc, queue, policy_name,
778                                        PTLRPC_NRS_CTL_START,
779                                        false, cmd);
780
781         mutex_unlock(&nrs_core.nrs_mutex);
782 out:
783         if (cmd_copy)
784                 OBD_FREE(cmd_copy, LPROCFS_NRS_WR_MAX_CMD);
785
786         RETURN(rc < 0 ? rc : count);
787 }
788
789 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs);
790
791 /** @} nrs */
792
793 struct ptlrpc_srh_iterator {
794         int                     srhi_idx;
795         __u64                   srhi_seq;
796         struct ptlrpc_request   *srhi_req;
797 };
798
799 static int
800 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service_part *svcpt,
801                                     struct ptlrpc_srh_iterator *srhi,
802                                     __u64 seq)
803 {
804         struct list_head        *e;
805         struct ptlrpc_request   *req;
806
807         if (srhi->srhi_req != NULL &&
808             srhi->srhi_seq > svcpt->scp_hist_seq_culled &&
809             srhi->srhi_seq <= seq) {
810                 /* If srhi_req was set previously, hasn't been culled and
811                  * we're searching for a seq on or after it (i.e. more
812                  * recent), search from it onwards.
813                  * Since the service history is LRU (i.e. culled reqs will
814                  * be near the head), we shouldn't have to do long
815                  * re-scans */
816                 LASSERTF(srhi->srhi_seq == srhi->srhi_req->rq_history_seq,
817                          "%s:%d: seek seq %llu, request seq %llu\n",
818                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
819                          srhi->srhi_seq, srhi->srhi_req->rq_history_seq);
820                 LASSERTF(!list_empty(&svcpt->scp_hist_reqs),
821                          "%s:%d: seek offset %llu, request seq %llu, "
822                          "last culled %llu\n",
823                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
824                          seq, srhi->srhi_seq, svcpt->scp_hist_seq_culled);
825                 e = &srhi->srhi_req->rq_history_list;
826         } else {
827                 /* search from start */
828                 e = svcpt->scp_hist_reqs.next;
829         }
830
831         while (e != &svcpt->scp_hist_reqs) {
832                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
833
834                 if (req->rq_history_seq >= seq) {
835                         srhi->srhi_seq = req->rq_history_seq;
836                         srhi->srhi_req = req;
837                         return 0;
838                 }
839                 e = e->next;
840         }
841
842         return -ENOENT;
843 }
844
845 /*
846  * ptlrpc history sequence is used as "position" of seq_file, in some case,
847  * seq_read() will increase "position" to indicate reading the next
848  * element, however, low bits of history sequence are reserved for CPT id
849  * (check the details from comments before ptlrpc_req_add_history), which
850  * means seq_read() might change CPT id of history sequence and never
851  * finish reading of requests on a CPT. To make it work, we have to shift
852  * CPT id to high bits and timestamp to low bits, so seq_read() will only
853  * increase timestamp which can correctly indicate the next position.
854  */
855
856 /* convert seq_file pos to cpt */
857 #define PTLRPC_REQ_POS2CPT(svc, pos)                    \
858         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
859          (__u64)(pos) >> (64 - (svc)->srv_cpt_bits))
860
861 /* make up seq_file pos from cpt */
862 #define PTLRPC_REQ_CPT2POS(svc, cpt)                    \
863         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
864          (cpt) << (64 - (svc)->srv_cpt_bits))
865
866 /* convert sequence to position */
867 #define PTLRPC_REQ_SEQ2POS(svc, seq)                    \
868         ((svc)->srv_cpt_bits == 0 ? (seq) :             \
869          ((seq) >> (svc)->srv_cpt_bits) |               \
870          ((seq) << (64 - (svc)->srv_cpt_bits)))
871
872 /* convert position to sequence */
873 #define PTLRPC_REQ_POS2SEQ(svc, pos)                    \
874         ((svc)->srv_cpt_bits == 0 ? (pos) :             \
875          ((__u64)(pos) << (svc)->srv_cpt_bits) |        \
876          ((__u64)(pos) >> (64 - (svc)->srv_cpt_bits)))
877
878 static void *
879 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
880 {
881         struct ptlrpc_service           *svc = s->private;
882         struct ptlrpc_service_part      *svcpt;
883         struct ptlrpc_srh_iterator      *srhi;
884         unsigned int                    cpt;
885         int                             rc;
886         int                             i;
887
888         if (sizeof(loff_t) != sizeof(__u64)) { /* can't support */
889                 CWARN("Failed to read request history because size of loff_t "
890                       "%d can't match size of u64\n", (int)sizeof(loff_t));
891                 return NULL;
892         }
893
894         OBD_ALLOC(srhi, sizeof(*srhi));
895         if (srhi == NULL)
896                 return NULL;
897
898         srhi->srhi_seq = 0;
899         srhi->srhi_req = NULL;
900
901         cpt = PTLRPC_REQ_POS2CPT(svc, *pos);
902
903         ptlrpc_service_for_each_part(svcpt, i, svc) {
904                 if (i < cpt) /* skip */
905                         continue;
906                 if (i > cpt) /* make up the lowest position for this CPT */
907                         *pos = PTLRPC_REQ_CPT2POS(svc, i);
908
909                 mutex_lock(&svcpt->scp_mutex);
910                 spin_lock(&svcpt->scp_lock);
911                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi,
912                                 PTLRPC_REQ_POS2SEQ(svc, *pos));
913                 spin_unlock(&svcpt->scp_lock);
914                 mutex_unlock(&svcpt->scp_mutex);
915                 if (rc == 0) {
916                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
917                         srhi->srhi_idx = i;
918                         return srhi;
919                 }
920         }
921
922         OBD_FREE(srhi, sizeof(*srhi));
923         return NULL;
924 }
925
926 static void
927 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
928 {
929         struct ptlrpc_srh_iterator *srhi = iter;
930
931         if (srhi != NULL)
932                 OBD_FREE(srhi, sizeof(*srhi));
933 }
934
935 static void *
936 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
937                                     void *iter, loff_t *pos)
938 {
939         struct ptlrpc_service           *svc = s->private;
940         struct ptlrpc_srh_iterator      *srhi = iter;
941         struct ptlrpc_service_part      *svcpt;
942         __u64                           seq;
943         int                             rc;
944         int                             i;
945
946         for (i = srhi->srhi_idx; i < svc->srv_ncpts; i++) {
947                 svcpt = svc->srv_parts[i];
948
949                 if (i > srhi->srhi_idx) { /* reset iterator for a new CPT */
950                         srhi->srhi_req = NULL;
951                         seq = srhi->srhi_seq = 0;
952                 } else { /* the next sequence */
953                         seq = srhi->srhi_seq + (1 << svc->srv_cpt_bits);
954                 }
955
956                 mutex_lock(&svcpt->scp_mutex);
957                 spin_lock(&svcpt->scp_lock);
958                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, seq);
959                 spin_unlock(&svcpt->scp_lock);
960                 mutex_unlock(&svcpt->scp_mutex);
961                 if (rc == 0) {
962                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
963                         srhi->srhi_idx = i;
964                         return srhi;
965                 }
966         }
967
968         OBD_FREE(srhi, sizeof(*srhi));
969         ++*pos;
970         return NULL;
971 }
972
973 /* common ost/mdt so_req_printer */
974 void target_print_req(void *seq_file, struct ptlrpc_request *req)
975 {
976         /* Called holding srv_lock with irqs disabled.
977          * Print specific req contents and a newline.
978          * CAVEAT EMPTOR: check request message length before printing!!!
979          * You might have received any old crap so you must be just as
980          * careful here as the service's request parser!!! */
981         struct seq_file *sf = seq_file;
982
983         switch (req->rq_phase) {
984         case RQ_PHASE_NEW:
985                 /* still awaiting a service thread's attention, or rejected
986                  * because the generic request message didn't unpack */
987                 seq_printf(sf, "<not swabbed>\n");
988                 break;
989         case RQ_PHASE_INTERPRET:
990                 /* being handled, so basic msg swabbed, and opc is valid
991                  * but racing with mds_handle() */
992         case RQ_PHASE_COMPLETE:
993                 /* been handled by mds_handle() reply state possibly still
994                  * volatile */
995                 seq_printf(sf, "opc %d\n", lustre_msg_get_opc(req->rq_reqmsg));
996                 break;
997         default:
998                 DEBUG_REQ(D_ERROR, req, "bad phase %d", req->rq_phase);
999         }
1000 }
1001 EXPORT_SYMBOL(target_print_req);
1002
1003 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
1004 {
1005         struct ptlrpc_service           *svc = s->private;
1006         struct ptlrpc_srh_iterator      *srhi = iter;
1007         struct ptlrpc_service_part      *svcpt;
1008         struct ptlrpc_request           *req;
1009         int                             rc;
1010
1011         LASSERT(srhi->srhi_idx < svc->srv_ncpts);
1012
1013         svcpt = svc->srv_parts[srhi->srhi_idx];
1014
1015         mutex_lock(&svcpt->scp_mutex);
1016         spin_lock(&svcpt->scp_lock);
1017
1018         rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, srhi->srhi_seq);
1019
1020         if (rc == 0) {
1021                 struct timespec64 arrival, sent, arrivaldiff;
1022                 char nidstr[LNET_NIDSTR_SIZE];
1023
1024                 req = srhi->srhi_req;
1025
1026                 arrival.tv_sec = req->rq_arrival_time.tv_sec;
1027                 arrival.tv_nsec = req->rq_arrival_time.tv_nsec;
1028                 sent.tv_sec = req->rq_sent;
1029                 sent.tv_nsec = 0;
1030                 arrivaldiff = timespec64_sub(sent, arrival);
1031
1032                 /* Print common req fields.
1033                  * CAVEAT EMPTOR: we're racing with the service handler
1034                  * here.  The request could contain any old crap, so you
1035                  * must be just as careful as the service's request
1036                  * parser. Currently I only print stuff here I know is OK
1037                  * to look at coz it was set up in request_in_callback()!!!
1038                  */
1039                 seq_printf(s,
1040                            "%lld:%s:%s:x%llu:%d:%s:%lld.%06lld:%lld.%06llds(%+lld.0s) ",
1041                            req->rq_history_seq,
1042                            req->rq_export && req->rq_export->exp_obd ?
1043                                 req->rq_export->exp_obd->obd_name :
1044                                 libcfs_nid2str_r(req->rq_self, nidstr,
1045                                                  sizeof(nidstr)),
1046                            libcfs_id2str(req->rq_peer), req->rq_xid,
1047                            req->rq_reqlen, ptlrpc_rqphase2str(req),
1048                            (s64)req->rq_arrival_time.tv_sec,
1049                            (s64)(req->rq_arrival_time.tv_nsec / NSEC_PER_USEC),
1050                            (s64)arrivaldiff.tv_sec,
1051                            (s64)(arrivaldiff.tv_nsec / NSEC_PER_USEC),
1052                            (s64)(req->rq_sent - req->rq_deadline));
1053                 if (svc->srv_ops.so_req_printer == NULL)
1054                         seq_printf(s, "\n");
1055                 else
1056                         svc->srv_ops.so_req_printer(s, srhi->srhi_req);
1057         }
1058
1059         spin_unlock(&svcpt->scp_lock);
1060         mutex_unlock(&svcpt->scp_mutex);
1061
1062         return rc;
1063 }
1064
1065 static int
1066 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
1067 {
1068         static struct seq_operations sops = {
1069                 .start = ptlrpc_lprocfs_svc_req_history_start,
1070                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
1071                 .next  = ptlrpc_lprocfs_svc_req_history_next,
1072                 .show  = ptlrpc_lprocfs_svc_req_history_show,
1073         };
1074         struct seq_file *seqf;
1075         int             rc;
1076
1077         rc = seq_open(file, &sops);
1078         if (rc)
1079                 return rc;
1080
1081         seqf = file->private_data;
1082         seqf->private = inode->i_private;
1083         return 0;
1084 }
1085
1086 /* See also lprocfs_rd_timeouts */
1087 static int ptlrpc_lprocfs_timeouts_seq_show(struct seq_file *m, void *n)
1088 {
1089         struct ptlrpc_service *svc = m->private;
1090         struct ptlrpc_service_part *svcpt;
1091         time64_t worst_timestamp;
1092         timeout_t cur_timeout;
1093         timeout_t worst_timeout;
1094         int i;
1095
1096         if (AT_OFF) {
1097                 seq_printf(m, "adaptive timeouts off, using obd_timeout %u\n",
1098                            obd_timeout);
1099                 return 0;
1100         }
1101
1102         ptlrpc_service_for_each_part(svcpt, i, svc) {
1103                 cur_timeout = at_get(&svcpt->scp_at_estimate);
1104                 worst_timeout = svcpt->scp_at_estimate.at_worst_timeout_ever;
1105                 worst_timestamp = svcpt->scp_at_estimate.at_worst_timestamp;
1106
1107                 seq_printf(m, "%10s : cur %3u  worst %3u (at %lld, %llds ago) ",
1108                            "service", cur_timeout, worst_timeout,
1109                            worst_timestamp,
1110                            ktime_get_real_seconds() - worst_timestamp);
1111
1112                 lprocfs_at_hist_helper(m, &svcpt->scp_at_estimate);
1113         }
1114
1115         return 0;
1116 }
1117
1118 LDEBUGFS_SEQ_FOPS_RO(ptlrpc_lprocfs_timeouts);
1119
1120 static ssize_t high_priority_ratio_show(struct kobject *kobj,
1121                                         struct attribute *attr,
1122                                         char *buf)
1123 {
1124         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1125                                                   srv_kobj);
1126
1127         return sprintf(buf, "%d\n", svc->srv_hpreq_ratio);
1128 }
1129
1130 static ssize_t high_priority_ratio_store(struct kobject *kobj,
1131                                          struct attribute *attr,
1132                                          const char *buffer,
1133                                          size_t count)
1134 {
1135         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1136                                                   srv_kobj);
1137         int rc;
1138         unsigned long val;
1139
1140         rc = kstrtoul(buffer, 10, &val);
1141         if (rc < 0)
1142                 return rc;
1143
1144         spin_lock(&svc->srv_lock);
1145         svc->srv_hpreq_ratio = val;
1146         spin_unlock(&svc->srv_lock);
1147
1148         return count;
1149 }
1150 LUSTRE_RW_ATTR(high_priority_ratio);
1151
1152 static struct attribute *ptlrpc_svc_attrs[] = {
1153         &lustre_attr_threads_min.attr,
1154         &lustre_attr_threads_started.attr,
1155         &lustre_attr_threads_max.attr,
1156         &lustre_attr_high_priority_ratio.attr,
1157         NULL,
1158 };
1159
1160 static void ptlrpc_sysfs_svc_release(struct kobject *kobj)
1161 {
1162         struct ptlrpc_service *svc = container_of(kobj, struct ptlrpc_service,
1163                                                   srv_kobj);
1164
1165         complete(&svc->srv_kobj_unregister);
1166 }
1167
1168 static struct kobj_type ptlrpc_svc_ktype = {
1169         .default_attrs  = ptlrpc_svc_attrs,
1170         .sysfs_ops      = &lustre_sysfs_ops,
1171         .release        = ptlrpc_sysfs_svc_release,
1172 };
1173
1174 void ptlrpc_sysfs_unregister_service(struct ptlrpc_service *svc)
1175 {
1176         /* Let's see if we had a chance at initialization first */
1177         if (svc->srv_kobj.kset) {
1178                 kobject_put(&svc->srv_kobj);
1179                 wait_for_completion(&svc->srv_kobj_unregister);
1180         }
1181 }
1182
1183 int ptlrpc_sysfs_register_service(struct kset *parent,
1184                                   struct ptlrpc_service *svc)
1185 {
1186         svc->srv_kobj.kset = parent;
1187         init_completion(&svc->srv_kobj_unregister);
1188         return kobject_init_and_add(&svc->srv_kobj, &ptlrpc_svc_ktype,
1189                                     &parent->kobj, "%s", svc->srv_name);
1190 }
1191
1192 void ptlrpc_ldebugfs_register_service(struct dentry *entry,
1193                                       struct ptlrpc_service *svc)
1194 {
1195         struct ldebugfs_vars ldebugfs_vars[] = {
1196                 { .name = "req_buffer_history_len",
1197                   .fops = &ptlrpc_lprocfs_req_history_len_fops,
1198                   .data = svc },
1199                 { .name = "req_buffer_history_max",
1200                   .fops = &ptlrpc_lprocfs_req_history_max_fops,
1201                   .data = svc },
1202                 { .name = "timeouts",
1203                   .fops = &ptlrpc_lprocfs_timeouts_fops,
1204                   .data = svc },
1205                 { .name = "nrs_policies",
1206                   .fops = &ptlrpc_lprocfs_nrs_fops,
1207                   .data = svc },
1208                 { .name = "req_buffers_max",
1209                   .fops = &ptlrpc_lprocfs_req_buffers_max_fops,
1210                   .data = svc },
1211                 { NULL }
1212         };
1213         static const struct file_operations req_history_fops = {
1214                 .owner          = THIS_MODULE,
1215                 .open           = ptlrpc_lprocfs_svc_req_history_open,
1216                 .read           = seq_read,
1217                 .llseek         = seq_lseek,
1218                 .release        = lprocfs_seq_release,
1219         };
1220
1221         ptlrpc_ldebugfs_register(entry, svc->srv_name, "stats",
1222                                  &svc->srv_debugfs_entry, &svc->srv_stats);
1223         if (!svc->srv_debugfs_entry)
1224                 return;
1225
1226         ldebugfs_add_vars(svc->srv_debugfs_entry, ldebugfs_vars, NULL);
1227
1228         debugfs_create_file("req_history", 0400, svc->srv_debugfs_entry, svc,
1229                             &req_history_fops);
1230 }
1231
1232 void ptlrpc_lprocfs_register_obd(struct obd_device *obd)
1233 {
1234         ptlrpc_ldebugfs_register(obd->obd_debugfs_entry, NULL, "stats",
1235                                  &obd->obd_svc_debugfs_entry,
1236                                  &obd->obd_svc_stats);
1237 }
1238 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
1239
1240 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req, long amount)
1241 {
1242         struct lprocfs_stats *svc_stats;
1243         __u32 op = lustre_msg_get_opc(req->rq_reqmsg);
1244         int opc = opcode_offset(op);
1245
1246         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1247         if (svc_stats == NULL || opc <= 0)
1248                 return;
1249         LASSERT(opc < LUSTRE_MAX_OPCODES);
1250         if (!(op == LDLM_ENQUEUE || op == MDS_REINT))
1251                 lprocfs_counter_add(svc_stats, opc + EXTRA_MAX_OPCODES, amount);
1252 }
1253
1254 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes)
1255 {
1256         struct lprocfs_stats *svc_stats;
1257         int idx;
1258
1259         if (!req->rq_import)
1260                 return;
1261         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1262         if (!svc_stats)
1263                 return;
1264         idx = lustre_msg_get_opc(req->rq_reqmsg);
1265         switch (idx) {
1266         case OST_READ:
1267                 idx = BRW_READ_BYTES + PTLRPC_LAST_CNTR;
1268                 break;
1269         case OST_WRITE:
1270                 idx = BRW_WRITE_BYTES + PTLRPC_LAST_CNTR;
1271                 break;
1272         default:
1273                 LASSERTF(0, "unsupported opcode %u\n", idx);
1274                 break;
1275         }
1276
1277         lprocfs_counter_add(svc_stats, idx, bytes);
1278 }
1279
1280 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
1281
1282 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
1283 {
1284         debugfs_remove_recursive(svc->srv_debugfs_entry);
1285
1286         if (svc->srv_stats)
1287                 lprocfs_free_stats(&svc->srv_stats);
1288 }
1289
1290 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
1291 {
1292         /* cleanup first to allow concurrent access to device's
1293          * stats via debugfs to complete safely
1294          */
1295         lprocfs_obd_cleanup(obd);
1296
1297         debugfs_remove_recursive(obd->obd_svc_debugfs_entry);
1298
1299         if (obd->obd_svc_stats)
1300                 lprocfs_free_stats(&obd->obd_svc_stats);
1301 }
1302 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
1303
1304 ssize_t ping_show(struct kobject *kobj, struct attribute *attr,
1305                   char *buffer)
1306 {
1307         struct obd_device *obd = container_of(kobj, struct obd_device,
1308                                               obd_kset.kobj);
1309         struct obd_import *imp;
1310         struct ptlrpc_request *req;
1311         int rc;
1312
1313         ENTRY;
1314         with_imp_locked(obd, imp, rc)
1315                 req = ptlrpc_prep_ping(imp);
1316
1317         if (rc)
1318                 RETURN(rc);
1319         if (!req)
1320                 RETURN(-ENOMEM);
1321
1322         req->rq_send_state = LUSTRE_IMP_FULL;
1323
1324         rc = ptlrpc_queue_wait(req);
1325         ptlrpc_req_finished(req);
1326
1327         RETURN(rc);
1328 }
1329 EXPORT_SYMBOL(ping_show);
1330
1331 /* kept for older verison of tools. */
1332 ssize_t ping_store(struct kobject *kobj, struct attribute *attr,
1333                    const char *buffer, size_t count)
1334 {
1335         int rc = ping_show(kobj, attr, (char *)buffer);
1336
1337         return (rc < 0) ? rc : count;
1338 }
1339 EXPORT_SYMBOL(ping_store);
1340
1341 /* Write the connection UUID to this file to attempt to connect to that node.
1342  * The connection UUID is a node's primary NID. For example,
1343  * "echo connection=192.168.0.1@tcp0::instance > .../import".
1344  */
1345 ssize_t
1346 ldebugfs_import_seq_write(struct file *file, const char __user *buffer,
1347                           size_t count, loff_t *off)
1348 {
1349         struct seq_file   *m    = file->private_data;
1350         struct obd_device *obd  = m->private;
1351         struct obd_import *imp;
1352         char *kbuf = NULL;
1353         char *uuid;
1354         char *ptr;
1355         int do_reconn = 1;
1356         const char prefix[] = "connection=";
1357         const int prefix_len = sizeof(prefix) - 1;
1358         int rc = 0;
1359
1360         if (count > PAGE_SIZE - 1 || count <= prefix_len)
1361                 return -EINVAL;
1362
1363         OBD_ALLOC(kbuf, count + 1);
1364         if (kbuf == NULL)
1365                 return -ENOMEM;
1366
1367         if (copy_from_user(kbuf, buffer, count))
1368                 GOTO(out, rc = -EFAULT);
1369
1370         kbuf[count] = 0;
1371
1372         /* only support connection=uuid::instance now */
1373         if (strncmp(prefix, kbuf, prefix_len) != 0)
1374                 GOTO(out, rc = -EINVAL);
1375
1376         with_imp_locked(obd, imp, rc) {
1377                 uuid = kbuf + prefix_len;
1378                 ptr = strstr(uuid, "::");
1379                 if (ptr) {
1380                         u32 inst;
1381                         int rc;
1382
1383                         *ptr = 0;
1384                         do_reconn = 0;
1385                         ptr += 2; /* Skip :: */
1386                         rc = kstrtouint(ptr, 10, &inst);
1387                         if (rc) {
1388                                 CERROR("config: wrong instance # %s\n", ptr);
1389                         } else if (inst != imp->imp_connect_data.ocd_instance) {
1390                                 CDEBUG(D_INFO,
1391                                        "IR: %s is connecting to an obsoleted target(%u/%u), reconnecting...\n",
1392                                        imp->imp_obd->obd_name,
1393                                        imp->imp_connect_data.ocd_instance,
1394                                        inst);
1395                                 do_reconn = 1;
1396                         } else {
1397                                 CDEBUG(D_INFO,
1398                                        "IR: %s has already been connecting to "
1399                                        "new target(%u)\n",
1400                                        imp->imp_obd->obd_name, inst);
1401                         }
1402                 }
1403
1404                 if (do_reconn)
1405                         ptlrpc_recover_import(imp, uuid, 1);
1406         }
1407
1408 out:
1409         OBD_FREE(kbuf, count + 1);
1410         return rc ?: count;
1411 }
1412 EXPORT_SYMBOL(ldebugfs_import_seq_write);
1413
1414 int lprocfs_pinger_recov_seq_show(struct seq_file *m, void *n)
1415 {
1416         struct obd_device *obd = m->private;
1417         struct obd_import *imp;
1418         int rc;
1419
1420         with_imp_locked(obd, imp, rc)
1421                 seq_printf(m, "%d\n", !imp->imp_no_pinger_recover);
1422
1423         return rc;
1424 }
1425 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_show);
1426
1427 ssize_t
1428 lprocfs_pinger_recov_seq_write(struct file *file, const char __user *buffer,
1429                                size_t count, loff_t *off)
1430 {
1431         struct seq_file *m = file->private_data;
1432         struct obd_device *obd = m->private;
1433         struct obd_import *imp;
1434         bool val;
1435         int rc;
1436
1437         rc = kstrtobool_from_user(buffer, count, &val);
1438         if (rc < 0)
1439                 return rc;
1440
1441         with_imp_locked(obd, imp, rc) {
1442                 spin_lock(&imp->imp_lock);
1443                 imp->imp_no_pinger_recover = !val;
1444                 spin_unlock(&imp->imp_lock);
1445         }
1446
1447         return rc ?: count;
1448 }
1449 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_write);