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