Whamcloud - gitweb
LU-8560 libcfs: handle PAGE_CACHE_* removal in newer kernels
[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         __s64 val;
301         int rc;
302
303         rc = lprocfs_str_to_s64(buffer, count, &val);
304         if (rc < 0)
305                 return rc;
306
307         if (val < 0 || val > INT_MAX)
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_SIZE - 1) >>
314                                                         PAGE_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 =
324                         max(1, ((int)val / svc->srv_ncpts));
325
326         spin_unlock(&svc->srv_lock);
327
328         return count;
329 }
330 LPROC_SEQ_FOPS(ptlrpc_lprocfs_req_history_max);
331
332 static int
333 ptlrpc_lprocfs_threads_min_seq_show(struct seq_file *m, void *n)
334 {
335         struct ptlrpc_service *svc = m->private;
336
337         seq_printf(m, "%d\n",
338                    svc->srv_nthrs_cpt_init * svc->srv_ncpts);
339         return 0;
340 }
341
342 static ssize_t
343 ptlrpc_lprocfs_threads_min_seq_write(struct file *file,
344                                      const char __user *buffer,
345                                      size_t count, loff_t *off)
346 {
347         struct seq_file *m = file->private_data;
348         struct ptlrpc_service *svc = m->private;
349         __s64 val;
350         int rc = lprocfs_str_to_s64(buffer, count, &val);
351
352         if (rc < 0)
353                 return rc;
354
355         if (val / svc->srv_ncpts < PTLRPC_NTHRS_INIT)
356                 return -ERANGE;
357
358         spin_lock(&svc->srv_lock);
359         if (val > svc->srv_nthrs_cpt_limit * svc->srv_ncpts) {
360                 spin_unlock(&svc->srv_lock);
361                 return -ERANGE;
362         }
363
364         svc->srv_nthrs_cpt_init = (int)val / svc->srv_ncpts;
365
366         spin_unlock(&svc->srv_lock);
367
368         return count;
369 }
370 LPROC_SEQ_FOPS(ptlrpc_lprocfs_threads_min);
371
372 static int
373 ptlrpc_lprocfs_threads_started_seq_show(struct seq_file *m, void *n)
374 {
375         struct ptlrpc_service           *svc = m->private;
376         struct ptlrpc_service_part      *svcpt;
377         int     total = 0;
378         int     i;
379
380         ptlrpc_service_for_each_part(svcpt, i, svc)
381                 total += svcpt->scp_nthrs_running;
382
383         seq_printf(m, "%d\n", total);
384         return 0;
385 }
386 LPROC_SEQ_FOPS_RO(ptlrpc_lprocfs_threads_started);
387
388 static int
389 ptlrpc_lprocfs_threads_max_seq_show(struct seq_file *m, void *n)
390 {
391         struct ptlrpc_service *svc = m->private;
392
393         seq_printf(m, "%d\n",
394                    svc->srv_nthrs_cpt_limit * svc->srv_ncpts);
395         return 0;
396 }
397
398 static ssize_t
399 ptlrpc_lprocfs_threads_max_seq_write(struct file *file,
400                                      const char __user *buffer,
401                                      size_t count, loff_t *off)
402 {
403         struct seq_file *m = file->private_data;
404         struct ptlrpc_service *svc = m->private;
405         __s64 val;
406         int rc = lprocfs_str_to_s64(buffer, count, &val);
407
408         if (rc < 0)
409                 return rc;
410
411         if (val / svc->srv_ncpts < PTLRPC_NTHRS_INIT)
412                 return -ERANGE;
413
414         spin_lock(&svc->srv_lock);
415         if (val < svc->srv_nthrs_cpt_init * svc->srv_ncpts) {
416                 spin_unlock(&svc->srv_lock);
417                 return -ERANGE;
418         }
419
420         svc->srv_nthrs_cpt_limit = (int)val / svc->srv_ncpts;
421
422         spin_unlock(&svc->srv_lock);
423
424         return count;
425 }
426 LPROC_SEQ_FOPS(ptlrpc_lprocfs_threads_max);
427
428 /**
429  * Translates \e ptlrpc_nrs_pol_state values to human-readable strings.
430  *
431  * \param[in] state The policy state
432  */
433 static const char *nrs_state2str(enum ptlrpc_nrs_pol_state state)
434 {
435         switch (state) {
436         default:
437                 LBUG();
438         case NRS_POL_STATE_INVALID:
439                 return "invalid";
440         case NRS_POL_STATE_STOPPED:
441                 return "stopped";
442         case NRS_POL_STATE_STOPPING:
443                 return "stopping";
444         case NRS_POL_STATE_STARTING:
445                 return "starting";
446         case NRS_POL_STATE_STARTED:
447                 return "started";
448         }
449 }
450
451 /**
452  * Obtains status information for \a policy.
453  *
454  * Information is copied in \a info.
455  *
456  * \param[in] policy The policy
457  * \param[out] info  Holds returned status information
458  */
459 void nrs_policy_get_info_locked(struct ptlrpc_nrs_policy *policy,
460                                 struct ptlrpc_nrs_pol_info *info)
461 {
462         LASSERT(policy != NULL);
463         LASSERT(info != NULL);
464         assert_spin_locked(&policy->pol_nrs->nrs_lock);
465
466         LASSERT(sizeof(info->pi_arg) == sizeof(policy->pol_arg));
467         memcpy(info->pi_name, policy->pol_desc->pd_name, NRS_POL_NAME_MAX);
468         memcpy(info->pi_arg, policy->pol_arg, sizeof(policy->pol_arg));
469
470         info->pi_fallback    = !!(policy->pol_flags & PTLRPC_NRS_FL_FALLBACK);
471         info->pi_state       = policy->pol_state;
472         /**
473          * XXX: These are accessed without holding
474          * ptlrpc_service_part::scp_req_lock.
475          */
476         info->pi_req_queued  = policy->pol_req_queued;
477         info->pi_req_started = policy->pol_req_started;
478 }
479
480 /**
481  * Reads and prints policy status information for all policies of a PTLRPC
482  * service.
483  */
484 static int ptlrpc_lprocfs_nrs_seq_show(struct seq_file *m, void *n)
485 {
486         struct ptlrpc_service          *svc = m->private;
487         struct ptlrpc_service_part     *svcpt;
488         struct ptlrpc_nrs              *nrs;
489         struct ptlrpc_nrs_policy       *policy;
490         struct ptlrpc_nrs_pol_info     *infos;
491         struct ptlrpc_nrs_pol_info      tmp;
492         unsigned                        num_pols;
493         unsigned                        pol_idx = 0;
494         bool                            hp = false;
495         int                             i;
496         int                             rc = 0;
497         ENTRY;
498
499         /**
500          * Serialize NRS core lprocfs operations with policy registration/
501          * unregistration.
502          */
503         mutex_lock(&nrs_core.nrs_mutex);
504
505         /**
506          * Use the first service partition's regular NRS head in order to obtain
507          * the number of policies registered with NRS heads of this service. All
508          * service partitions will have the same number of policies.
509          */
510         nrs = nrs_svcpt2nrs(svc->srv_parts[0], false);
511
512         spin_lock(&nrs->nrs_lock);
513         num_pols = svc->srv_parts[0]->scp_nrs_reg.nrs_num_pols;
514         spin_unlock(&nrs->nrs_lock);
515
516         OBD_ALLOC(infos, num_pols * sizeof(*infos));
517         if (infos == NULL)
518                 GOTO(out, rc = -ENOMEM);
519 again:
520
521         ptlrpc_service_for_each_part(svcpt, i, svc) {
522                 nrs = nrs_svcpt2nrs(svcpt, hp);
523                 spin_lock(&nrs->nrs_lock);
524
525                 pol_idx = 0;
526
527                 list_for_each_entry(policy, &nrs->nrs_policy_list,
528                                     pol_list) {
529                         LASSERT(pol_idx < num_pols);
530
531                         nrs_policy_get_info_locked(policy, &tmp);
532                         /**
533                          * Copy values when handling the first service
534                          * partition.
535                          */
536                         if (i == 0) {
537                                 memcpy(infos[pol_idx].pi_name, tmp.pi_name,
538                                        NRS_POL_NAME_MAX);
539                                 memcpy(infos[pol_idx].pi_arg, tmp.pi_arg,
540                                        sizeof(tmp.pi_arg));
541                                 memcpy(&infos[pol_idx].pi_state, &tmp.pi_state,
542                                        sizeof(tmp.pi_state));
543                                 infos[pol_idx].pi_fallback = tmp.pi_fallback;
544                                 /**
545                                  * For the rest of the service partitions
546                                  * sanity-check the values we get.
547                                  */
548                         } else {
549                                 LASSERT(strncmp(infos[pol_idx].pi_name,
550                                                 tmp.pi_name,
551                                                 NRS_POL_NAME_MAX) == 0);
552                                 LASSERT(strncmp(infos[pol_idx].pi_arg,
553                                                 tmp.pi_arg,
554                                                 sizeof(tmp.pi_arg)) == 0);
555                                 /**
556                                  * Not asserting ptlrpc_nrs_pol_info::pi_state,
557                                  * because it may be different between
558                                  * instances of the same policy in different
559                                  * service partitions.
560                                  */
561                                 LASSERT(infos[pol_idx].pi_fallback ==
562                                         tmp.pi_fallback);
563                         }
564
565                         infos[pol_idx].pi_req_queued += tmp.pi_req_queued;
566                         infos[pol_idx].pi_req_started += tmp.pi_req_started;
567
568                         pol_idx++;
569                 }
570                 spin_unlock(&nrs->nrs_lock);
571         }
572
573         /**
574          * Policy status information output is in YAML format.
575          * For example:
576          *
577          *      regular_requests:
578          *        - name: fifo
579          *          state: started
580          *          fallback: yes
581          *          queued: 0
582          *          active: 0
583          *
584          *        - name: crrn
585          *          state: started
586          *          fallback: no
587          *          queued: 2015
588          *          active: 384
589          *
590          *      high_priority_requests:
591          *        - name: fifo
592          *          state: started
593          *          fallback: yes
594          *          queued: 0
595          *          active: 2
596          *
597          *        - name: crrn
598          *          state: stopped
599          *          fallback: no
600          *          queued: 0
601          *          active: 0
602          */
603         seq_printf(m, "%s\n", !hp ? "\nregular_requests:" :
604                    "high_priority_requests:");
605
606         for (pol_idx = 0; pol_idx < num_pols; pol_idx++) {
607                 if (strlen(infos[pol_idx].pi_arg) > 0)
608                         seq_printf(m, "  - name: %s %s\n",
609                                    infos[pol_idx].pi_name,
610                                    infos[pol_idx].pi_arg);
611                 else
612                         seq_printf(m, "  - name: %s\n",
613                                    infos[pol_idx].pi_name);
614
615
616                 seq_printf(m, "    state: %s\n"
617                            "    fallback: %s\n"
618                            "    queued: %-20d\n"
619                            "    active: %-20d\n\n",
620                            nrs_state2str(infos[pol_idx].pi_state),
621                            infos[pol_idx].pi_fallback ? "yes" : "no",
622                            (int)infos[pol_idx].pi_req_queued,
623                            (int)infos[pol_idx].pi_req_started);
624         }
625
626         if (!hp && nrs_svc_has_hp(svc)) {
627                 memset(infos, 0, num_pols * sizeof(*infos));
628
629                 /**
630                  * Redo the processing for the service's HP NRS heads' policies.
631                  */
632                 hp = true;
633                 goto again;
634         }
635
636 out:
637         if (infos)
638                 OBD_FREE(infos, num_pols * sizeof(*infos));
639
640         mutex_unlock(&nrs_core.nrs_mutex);
641
642         RETURN(rc);
643 }
644
645
646 #define LPROCFS_NRS_WR_MAX_ARG (1024)
647 /**
648  * The longest valid command string is the maxium policy name size, plus the
649  * length of the " reg" substring, plus the lenght of argument
650  */
651 #define LPROCFS_NRS_WR_MAX_CMD  (NRS_POL_NAME_MAX + sizeof(" reg") - 1 \
652                                  + LPROCFS_NRS_WR_MAX_ARG)
653
654 /**
655  * Starts and stops a given policy on a PTLRPC service.
656  *
657  * Commands consist of the policy name, followed by an optional [reg|hp] token;
658  * if the optional token is omitted, the operation is performed on both the
659  * regular and high-priority (if the service has one) NRS head.
660  */
661 static ssize_t
662 ptlrpc_lprocfs_nrs_seq_write(struct file *file, const char __user *buffer,
663                              size_t count, loff_t *off)
664 {
665         struct seq_file                *m = file->private_data;
666         struct ptlrpc_service          *svc = m->private;
667         enum ptlrpc_nrs_queue_type      queue = PTLRPC_NRS_QUEUE_BOTH;
668         char                           *cmd;
669         char                           *cmd_copy = NULL;
670         char                           *policy_name;
671         char                           *queue_name;
672         int                             rc = 0;
673         ENTRY;
674
675         if (count >= LPROCFS_NRS_WR_MAX_CMD)
676                 GOTO(out, rc = -EINVAL);
677
678         OBD_ALLOC(cmd, LPROCFS_NRS_WR_MAX_CMD);
679         if (cmd == NULL)
680                 GOTO(out, rc = -ENOMEM);
681         /**
682          * strsep() modifies its argument, so keep a copy
683          */
684         cmd_copy = cmd;
685
686         if (copy_from_user(cmd, buffer, count))
687                 GOTO(out, rc = -EFAULT);
688
689         cmd[count] = '\0';
690
691         policy_name = strsep(&cmd, " ");
692
693         if (strlen(policy_name) > NRS_POL_NAME_MAX - 1)
694                 GOTO(out, rc = -EINVAL);
695
696         /**
697          * No [reg|hp] token has been specified
698          */
699         if (cmd == NULL)
700                 goto default_queue;
701
702         queue_name = strsep(&cmd, " ");
703         /**
704          * The second token is either an optional [reg|hp] string,
705          * or arguments
706          */
707         if (strcmp(queue_name, "reg") == 0)
708                 queue = PTLRPC_NRS_QUEUE_REG;
709         else if (strcmp(queue_name, "hp") == 0)
710                 queue = PTLRPC_NRS_QUEUE_HP;
711         else {
712                 if (cmd != NULL)
713                         *(cmd - 1) = ' ';
714                 cmd = queue_name;
715         }
716
717 default_queue:
718
719         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
720                 GOTO(out, rc = -ENODEV);
721         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
722                 queue = PTLRPC_NRS_QUEUE_REG;
723
724         /**
725          * Serialize NRS core lprocfs operations with policy registration/
726          * unregistration.
727          */
728         mutex_lock(&nrs_core.nrs_mutex);
729
730         rc = ptlrpc_nrs_policy_control(svc, queue, policy_name,
731                                        PTLRPC_NRS_CTL_START,
732                                        false, cmd);
733
734         mutex_unlock(&nrs_core.nrs_mutex);
735 out:
736         if (cmd_copy)
737                 OBD_FREE(cmd_copy, LPROCFS_NRS_WR_MAX_CMD);
738
739         RETURN(rc < 0 ? rc : count);
740 }
741 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs);
742
743 /** @} nrs */
744
745 struct ptlrpc_srh_iterator {
746         int                     srhi_idx;
747         __u64                   srhi_seq;
748         struct ptlrpc_request   *srhi_req;
749 };
750
751 static int
752 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service_part *svcpt,
753                                     struct ptlrpc_srh_iterator *srhi,
754                                     __u64 seq)
755 {
756         struct list_head        *e;
757         struct ptlrpc_request   *req;
758
759         if (srhi->srhi_req != NULL &&
760             srhi->srhi_seq > svcpt->scp_hist_seq_culled &&
761             srhi->srhi_seq <= seq) {
762                 /* If srhi_req was set previously, hasn't been culled and
763                  * we're searching for a seq on or after it (i.e. more
764                  * recent), search from it onwards.
765                  * Since the service history is LRU (i.e. culled reqs will
766                  * be near the head), we shouldn't have to do long
767                  * re-scans */
768                 LASSERTF(srhi->srhi_seq == srhi->srhi_req->rq_history_seq,
769                          "%s:%d: seek seq %llu, request seq %llu\n",
770                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
771                          srhi->srhi_seq, srhi->srhi_req->rq_history_seq);
772                 LASSERTF(!list_empty(&svcpt->scp_hist_reqs),
773                          "%s:%d: seek offset %llu, request seq %llu, "
774                          "last culled %llu\n",
775                          svcpt->scp_service->srv_name, svcpt->scp_cpt,
776                          seq, srhi->srhi_seq, svcpt->scp_hist_seq_culled);
777                 e = &srhi->srhi_req->rq_history_list;
778         } else {
779                 /* search from start */
780                 e = svcpt->scp_hist_reqs.next;
781         }
782
783         while (e != &svcpt->scp_hist_reqs) {
784                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
785
786                 if (req->rq_history_seq >= seq) {
787                         srhi->srhi_seq = req->rq_history_seq;
788                         srhi->srhi_req = req;
789                         return 0;
790                 }
791                 e = e->next;
792         }
793
794         return -ENOENT;
795 }
796
797 /*
798  * ptlrpc history sequence is used as "position" of seq_file, in some case,
799  * seq_read() will increase "position" to indicate reading the next
800  * element, however, low bits of history sequence are reserved for CPT id
801  * (check the details from comments before ptlrpc_req_add_history), which
802  * means seq_read() might change CPT id of history sequence and never
803  * finish reading of requests on a CPT. To make it work, we have to shift
804  * CPT id to high bits and timestamp to low bits, so seq_read() will only
805  * increase timestamp which can correctly indicate the next position.
806  */
807
808 /* convert seq_file pos to cpt */
809 #define PTLRPC_REQ_POS2CPT(svc, pos)                    \
810         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
811          (__u64)(pos) >> (64 - (svc)->srv_cpt_bits))
812
813 /* make up seq_file pos from cpt */
814 #define PTLRPC_REQ_CPT2POS(svc, cpt)                    \
815         ((svc)->srv_cpt_bits == 0 ? 0 :                 \
816          (cpt) << (64 - (svc)->srv_cpt_bits))
817
818 /* convert sequence to position */
819 #define PTLRPC_REQ_SEQ2POS(svc, seq)                    \
820         ((svc)->srv_cpt_bits == 0 ? (seq) :             \
821          ((seq) >> (svc)->srv_cpt_bits) |               \
822          ((seq) << (64 - (svc)->srv_cpt_bits)))
823
824 /* convert position to sequence */
825 #define PTLRPC_REQ_POS2SEQ(svc, pos)                    \
826         ((svc)->srv_cpt_bits == 0 ? (pos) :             \
827          ((__u64)(pos) << (svc)->srv_cpt_bits) |        \
828          ((__u64)(pos) >> (64 - (svc)->srv_cpt_bits)))
829
830 static void *
831 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
832 {
833         struct ptlrpc_service           *svc = s->private;
834         struct ptlrpc_service_part      *svcpt;
835         struct ptlrpc_srh_iterator      *srhi;
836         unsigned int                    cpt;
837         int                             rc;
838         int                             i;
839
840         if (sizeof(loff_t) != sizeof(__u64)) { /* can't support */
841                 CWARN("Failed to read request history because size of loff_t "
842                       "%d can't match size of u64\n", (int)sizeof(loff_t));
843                 return NULL;
844         }
845
846         OBD_ALLOC(srhi, sizeof(*srhi));
847         if (srhi == NULL)
848                 return NULL;
849
850         srhi->srhi_seq = 0;
851         srhi->srhi_req = NULL;
852
853         cpt = PTLRPC_REQ_POS2CPT(svc, *pos);
854
855         ptlrpc_service_for_each_part(svcpt, i, svc) {
856                 if (i < cpt) /* skip */
857                         continue;
858                 if (i > cpt) /* make up the lowest position for this CPT */
859                         *pos = PTLRPC_REQ_CPT2POS(svc, i);
860
861                 spin_lock(&svcpt->scp_lock);
862                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi,
863                                 PTLRPC_REQ_POS2SEQ(svc, *pos));
864                 spin_unlock(&svcpt->scp_lock);
865                 if (rc == 0) {
866                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
867                         srhi->srhi_idx = i;
868                         return srhi;
869                 }
870         }
871
872         OBD_FREE(srhi, sizeof(*srhi));
873         return NULL;
874 }
875
876 static void
877 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
878 {
879         struct ptlrpc_srh_iterator *srhi = iter;
880
881         if (srhi != NULL)
882                 OBD_FREE(srhi, sizeof(*srhi));
883 }
884
885 static void *
886 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
887                                     void *iter, loff_t *pos)
888 {
889         struct ptlrpc_service           *svc = s->private;
890         struct ptlrpc_srh_iterator      *srhi = iter;
891         struct ptlrpc_service_part      *svcpt;
892         __u64                           seq;
893         int                             rc;
894         int                             i;
895
896         for (i = srhi->srhi_idx; i < svc->srv_ncpts; i++) {
897                 svcpt = svc->srv_parts[i];
898
899                 if (i > srhi->srhi_idx) { /* reset iterator for a new CPT */
900                         srhi->srhi_req = NULL;
901                         seq = srhi->srhi_seq = 0;
902                 } else { /* the next sequence */
903                         seq = srhi->srhi_seq + (1 << svc->srv_cpt_bits);
904                 }
905
906                 spin_lock(&svcpt->scp_lock);
907                 rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, seq);
908                 spin_unlock(&svcpt->scp_lock);
909                 if (rc == 0) {
910                         *pos = PTLRPC_REQ_SEQ2POS(svc, srhi->srhi_seq);
911                         srhi->srhi_idx = i;
912                         return srhi;
913                 }
914         }
915
916         OBD_FREE(srhi, sizeof(*srhi));
917         return NULL;
918 }
919
920 /* common ost/mdt so_req_printer */
921 void target_print_req(void *seq_file, struct ptlrpc_request *req)
922 {
923         /* Called holding srv_lock with irqs disabled.
924          * Print specific req contents and a newline.
925          * CAVEAT EMPTOR: check request message length before printing!!!
926          * You might have received any old crap so you must be just as
927          * careful here as the service's request parser!!! */
928         struct seq_file *sf = seq_file;
929
930         switch (req->rq_phase) {
931         case RQ_PHASE_NEW:
932                 /* still awaiting a service thread's attention, or rejected
933                  * because the generic request message didn't unpack */
934                 seq_printf(sf, "<not swabbed>\n");
935                 break;
936         case RQ_PHASE_INTERPRET:
937                 /* being handled, so basic msg swabbed, and opc is valid
938                  * but racing with mds_handle() */
939         case RQ_PHASE_COMPLETE:
940                 /* been handled by mds_handle() reply state possibly still
941                  * volatile */
942                 seq_printf(sf, "opc %d\n", lustre_msg_get_opc(req->rq_reqmsg));
943                 break;
944         default:
945                 DEBUG_REQ(D_ERROR, req, "bad phase %d", req->rq_phase);
946         }
947 }
948 EXPORT_SYMBOL(target_print_req);
949
950 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
951 {
952         struct ptlrpc_service           *svc = s->private;
953         struct ptlrpc_srh_iterator      *srhi = iter;
954         struct ptlrpc_service_part      *svcpt;
955         struct ptlrpc_request           *req;
956         int                             rc;
957
958         LASSERT(srhi->srhi_idx < svc->srv_ncpts);
959
960         svcpt = svc->srv_parts[srhi->srhi_idx];
961
962         spin_lock(&svcpt->scp_lock);
963
964         rc = ptlrpc_lprocfs_svc_req_history_seek(svcpt, srhi, srhi->srhi_seq);
965
966         if (rc == 0) {
967                 struct timespec arrival, sent, arrivaldiff;
968                 char nidstr[LNET_NIDSTR_SIZE];
969
970                 req = srhi->srhi_req;
971
972                 libcfs_nid2str_r(req->rq_self, nidstr, sizeof(nidstr));
973                 arrival.tv_sec = req->rq_arrival_time.tv_sec;
974                 arrival.tv_nsec =
975                         req->rq_arrival_time.tv_usec * NSEC_PER_USEC;
976                 sent.tv_sec = req->rq_sent;
977                 sent.tv_nsec = 0;
978                 arrivaldiff = timespec_sub(sent, arrival);
979
980                 /* Print common req fields.
981                  * CAVEAT EMPTOR: we're racing with the service handler
982                  * here.  The request could contain any old crap, so you
983                  * must be just as careful as the service's request
984                  * parser. Currently I only print stuff here I know is OK
985                  * to look at coz it was set up in request_in_callback()!!! */
986                 seq_printf(s, "%lld:%s:%s:x%llu:%d:%s:"
987                            "%ld.%06ld:%ld.%06lds(%+ld.0s) ",
988                            req->rq_history_seq, nidstr,
989                            libcfs_id2str(req->rq_peer), req->rq_xid,
990                            req->rq_reqlen, ptlrpc_rqphase2str(req),
991                            req->rq_arrival_time.tv_sec,
992                            req->rq_arrival_time.tv_usec,
993                            arrivaldiff.tv_sec,
994                            arrivaldiff.tv_nsec / NSEC_PER_USEC,
995                            req->rq_sent - req->rq_deadline);
996                 if (svc->srv_ops.so_req_printer == NULL)
997                         seq_printf(s, "\n");
998                 else
999                         svc->srv_ops.so_req_printer(s, srhi->srhi_req);
1000         }
1001
1002         spin_unlock(&svcpt->scp_lock);
1003         return rc;
1004 }
1005
1006 static int
1007 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
1008 {
1009         static struct seq_operations sops = {
1010                 .start = ptlrpc_lprocfs_svc_req_history_start,
1011                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
1012                 .next  = ptlrpc_lprocfs_svc_req_history_next,
1013                 .show  = ptlrpc_lprocfs_svc_req_history_show,
1014         };
1015         struct seq_file *seqf;
1016         int             rc;
1017
1018         rc = LPROCFS_ENTRY_CHECK(inode);
1019         if (rc < 0)
1020                 return rc;
1021
1022         rc = seq_open(file, &sops);
1023         if (rc)
1024                 return rc;
1025
1026         seqf = file->private_data;
1027         seqf->private = PDE_DATA(inode);
1028         return 0;
1029 }
1030
1031 /* See also lprocfs_rd_timeouts */
1032 static int ptlrpc_lprocfs_timeouts_seq_show(struct seq_file *m, void *n)
1033 {
1034         struct ptlrpc_service           *svc = m->private;
1035         struct ptlrpc_service_part      *svcpt;
1036         struct dhms                     ts;
1037         time_t                          worstt;
1038         unsigned int                    cur;
1039         unsigned int                    worst;
1040         int                             i;
1041
1042         if (AT_OFF) {
1043                 seq_printf(m, "adaptive timeouts off, using obd_timeout %u\n",
1044                            obd_timeout);
1045                 return 0;
1046         }
1047
1048         ptlrpc_service_for_each_part(svcpt, i, svc) {
1049                 cur     = at_get(&svcpt->scp_at_estimate);
1050                 worst   = svcpt->scp_at_estimate.at_worst_ever;
1051                 worstt  = svcpt->scp_at_estimate.at_worst_time;
1052                 s2dhms(&ts, cfs_time_current_sec() - worstt);
1053
1054                 seq_printf(m, "%10s : cur %3u  worst %3u (at %ld, "
1055                            DHMS_FMT" ago) ", "service",
1056                            cur, worst, worstt, DHMS_VARS(&ts));
1057
1058                 lprocfs_at_hist_helper(m, &svcpt->scp_at_estimate);
1059         }
1060
1061         return 0;
1062 }
1063 LPROC_SEQ_FOPS_RO(ptlrpc_lprocfs_timeouts);
1064
1065 static int ptlrpc_lprocfs_hp_ratio_seq_show(struct seq_file *m, void *v)
1066 {
1067         struct ptlrpc_service *svc = m->private;
1068         seq_printf(m, "%d\n", svc->srv_hpreq_ratio);
1069         return 0;
1070 }
1071
1072 static ssize_t
1073 ptlrpc_lprocfs_hp_ratio_seq_write(struct file *file, const char __user *buffer,
1074                                   size_t count, loff_t *off)
1075 {
1076         struct seq_file *m = file->private_data;
1077         struct ptlrpc_service *svc = m->private;
1078         int rc;
1079         __s64 val;
1080
1081         rc = lprocfs_str_to_s64(buffer, count, &val);
1082         if (rc < 0)
1083                 return rc;
1084
1085         if (val < 0 || val > INT_MAX)
1086                 return -ERANGE;
1087
1088         spin_lock(&svc->srv_lock);
1089         svc->srv_hpreq_ratio = val;
1090         spin_unlock(&svc->srv_lock);
1091
1092         return count;
1093 }
1094 LPROC_SEQ_FOPS(ptlrpc_lprocfs_hp_ratio);
1095
1096 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
1097                                      struct ptlrpc_service *svc)
1098 {
1099         struct lprocfs_vars lproc_vars[] = {
1100                 { .name = "high_priority_ratio",
1101                   .fops = &ptlrpc_lprocfs_hp_ratio_fops,
1102                   .data = svc },
1103                 { .name = "req_buffer_history_len",
1104                   .fops = &ptlrpc_lprocfs_req_history_len_fops,
1105                   .data = svc },
1106                 { .name = "req_buffer_history_max",
1107                   .fops = &ptlrpc_lprocfs_req_history_max_fops,
1108                   .data = svc },
1109                 { .name = "threads_min",
1110                   .fops = &ptlrpc_lprocfs_threads_min_fops,
1111                   .data = svc },
1112                 { .name = "threads_max",
1113                   .fops = &ptlrpc_lprocfs_threads_max_fops,
1114                   .data = svc },
1115                 { .name = "threads_started",
1116                   .fops = &ptlrpc_lprocfs_threads_started_fops,
1117                   .data = svc },
1118                 { .name = "timeouts",
1119                   .fops = &ptlrpc_lprocfs_timeouts_fops,
1120                   .data = svc },
1121                 { .name = "nrs_policies",
1122                   .fops = &ptlrpc_lprocfs_nrs_fops,
1123                   .data = svc },
1124                 { NULL }
1125         };
1126         static struct file_operations req_history_fops = {
1127                 .owner       = THIS_MODULE,
1128                 .open        = ptlrpc_lprocfs_svc_req_history_open,
1129                 .read        = seq_read,
1130                 .llseek      = seq_lseek,
1131                 .release     = lprocfs_seq_release,
1132         };
1133
1134         int rc;
1135
1136         ptlrpc_lprocfs_register(entry, svc->srv_name,
1137                                 "stats", &svc->srv_procroot,
1138                                 &svc->srv_stats);
1139         if (svc->srv_procroot == NULL)
1140                 return;
1141
1142         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
1143
1144         rc = lprocfs_seq_create(svc->srv_procroot, "req_history",
1145                                 0400, &req_history_fops, svc);
1146         if (rc)
1147                 CWARN("Error adding the req_history file\n");
1148 }
1149
1150 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
1151 {
1152         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
1153                                 &obddev->obd_svc_procroot,
1154                                 &obddev->obd_svc_stats);
1155 }
1156 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
1157
1158 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req, long amount)
1159 {
1160         struct lprocfs_stats *svc_stats;
1161         __u32 op = lustre_msg_get_opc(req->rq_reqmsg);
1162         int opc = opcode_offset(op);
1163
1164         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1165         if (svc_stats == NULL || opc <= 0)
1166                 return;
1167         LASSERT(opc < LUSTRE_MAX_OPCODES);
1168         if (!(op == LDLM_ENQUEUE || op == MDS_REINT))
1169                 lprocfs_counter_add(svc_stats, opc + EXTRA_MAX_OPCODES, amount);
1170 }
1171
1172 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes)
1173 {
1174         struct lprocfs_stats *svc_stats;
1175         int idx;
1176
1177         if (!req->rq_import)
1178                 return;
1179         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
1180         if (!svc_stats)
1181                 return;
1182         idx = lustre_msg_get_opc(req->rq_reqmsg);
1183         switch (idx) {
1184         case OST_READ:
1185                 idx = BRW_READ_BYTES + PTLRPC_LAST_CNTR;
1186                 break;
1187         case OST_WRITE:
1188                 idx = BRW_WRITE_BYTES + PTLRPC_LAST_CNTR;
1189                 break;
1190         default:
1191                 LASSERTF(0, "unsupported opcode %u\n", idx);
1192                 break;
1193         }
1194
1195         lprocfs_counter_add(svc_stats, idx, bytes);
1196 }
1197
1198 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
1199
1200 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
1201 {
1202         if (svc->srv_procroot != NULL)
1203                 lprocfs_remove(&svc->srv_procroot);
1204
1205         if (svc->srv_stats)
1206                 lprocfs_free_stats(&svc->srv_stats);
1207 }
1208
1209 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
1210 {
1211         if (obd->obd_svc_procroot)
1212                 lprocfs_remove(&obd->obd_svc_procroot);
1213
1214         if (obd->obd_svc_stats)
1215                 lprocfs_free_stats(&obd->obd_svc_stats);
1216 }
1217 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
1218
1219 ssize_t
1220 lprocfs_ping_seq_write(struct file *file, const char __user *buffer,
1221                        size_t count, loff_t *off)
1222 {
1223         struct seq_file         *m = file->private_data;
1224         struct obd_device       *obd = m->private;
1225         struct ptlrpc_request   *req;
1226         int                     rc;
1227         ENTRY;
1228
1229         LPROCFS_CLIMP_CHECK(obd);
1230         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
1231         LPROCFS_CLIMP_EXIT(obd);
1232         if (req == NULL)
1233                 RETURN(-ENOMEM);
1234
1235         req->rq_send_state = LUSTRE_IMP_FULL;
1236
1237         rc = ptlrpc_queue_wait(req);
1238
1239         ptlrpc_req_finished(req);
1240         if (rc >= 0)
1241                 RETURN(count);
1242         RETURN(rc);
1243 }
1244 EXPORT_SYMBOL(lprocfs_ping_seq_write);
1245
1246 /* Write the connection UUID to this file to attempt to connect to that node.
1247  * The connection UUID is a node's primary NID. For example,
1248  * "echo connection=192.168.0.1@tcp0::instance > .../import".
1249  */
1250 ssize_t
1251 lprocfs_import_seq_write(struct file *file, const char __user *buffer,
1252                          size_t count, loff_t *off)
1253 {
1254         struct seq_file   *m    = file->private_data;
1255         struct obd_device *obd  = m->private;
1256         struct obd_import *imp  = obd->u.cli.cl_import;
1257         char *kbuf = NULL;
1258         char *uuid;
1259         char *ptr;
1260         int do_reconn = 1;
1261         const char prefix[] = "connection=";
1262         const int prefix_len = sizeof(prefix) - 1;
1263
1264         if (count > PAGE_SIZE - 1 || count <= prefix_len)
1265                 return -EINVAL;
1266
1267         OBD_ALLOC(kbuf, count + 1);
1268         if (kbuf == NULL)
1269                 return -ENOMEM;
1270
1271         if (copy_from_user(kbuf, buffer, count))
1272                 GOTO(out, count = -EFAULT);
1273
1274         kbuf[count] = 0;
1275
1276         /* only support connection=uuid::instance now */
1277         if (strncmp(prefix, kbuf, prefix_len) != 0)
1278                 GOTO(out, count = -EINVAL);
1279
1280         uuid = kbuf + prefix_len;
1281         ptr = strstr(uuid, "::");
1282         if (ptr) {
1283                 __u32 inst;
1284                 char *endptr;
1285
1286                 *ptr = 0;
1287                 do_reconn = 0;
1288                 ptr += 2; /* Skip :: */
1289                 inst = simple_strtol(ptr, &endptr, 10);
1290                 if (*endptr) {
1291                         CERROR("config: wrong instance # %s\n", ptr);
1292                 } else if (inst != imp->imp_connect_data.ocd_instance) {
1293                         CDEBUG(D_INFO, "IR: %s is connecting to an obsoleted "
1294                                "target(%u/%u), reconnecting...\n",
1295                                imp->imp_obd->obd_name,
1296                                imp->imp_connect_data.ocd_instance, inst);
1297                         do_reconn = 1;
1298                 } else {
1299                         CDEBUG(D_INFO, "IR: %s has already been connecting to "
1300                                "new target(%u)\n",
1301                                imp->imp_obd->obd_name, inst);
1302                 }
1303         }
1304
1305         if (do_reconn)
1306                 ptlrpc_recover_import(imp, uuid, 1);
1307
1308 out:
1309         OBD_FREE(kbuf, count + 1);
1310         return count;
1311 }
1312 EXPORT_SYMBOL(lprocfs_import_seq_write);
1313
1314 int lprocfs_pinger_recov_seq_show(struct seq_file *m, void *n)
1315 {
1316         struct obd_device *obd = m->private;
1317         struct obd_import *imp = obd->u.cli.cl_import;
1318
1319         LPROCFS_CLIMP_CHECK(obd);
1320         seq_printf(m, "%d\n", !imp->imp_no_pinger_recover);
1321         LPROCFS_CLIMP_EXIT(obd);
1322         return 0;
1323 }
1324 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_show);
1325
1326 ssize_t
1327 lprocfs_pinger_recov_seq_write(struct file *file, const char __user *buffer,
1328                                size_t count, loff_t *off)
1329 {
1330         struct seq_file *m = file->private_data;
1331         struct obd_device *obd = m->private;
1332         struct client_obd *cli = &obd->u.cli;
1333         struct obd_import *imp = cli->cl_import;
1334         int rc;
1335         __s64 val;
1336
1337         rc = lprocfs_str_to_s64(buffer, count, &val);
1338         if (rc < 0)
1339                 return rc;
1340
1341         if (val != 0 && val != 1)
1342                 return -ERANGE;
1343
1344         LPROCFS_CLIMP_CHECK(obd);
1345         spin_lock(&imp->imp_lock);
1346         imp->imp_no_pinger_recover = !val;
1347         spin_unlock(&imp->imp_lock);
1348         LPROCFS_CLIMP_EXIT(obd);
1349         return count;
1350 }
1351 EXPORT_SYMBOL(lprocfs_pinger_recov_seq_write);
1352
1353 #endif /* CONFIG_PROC_FS */