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