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