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