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