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