Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / ptlrpc / lproc_ptlrpc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #ifndef __KERNEL__
41 # include <liblustre.h>
42 #endif
43
44 #include <obd_support.h>
45 #include <obd.h>
46 #include <lprocfs_status.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_net.h>
49 #include <obd_class.h>
50 #include "ptlrpc_internal.h"
51
52
53 struct ll_rpc_opcode {
54      __u32       opcode;
55      const char *opname;
56 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
57         { OST_REPLY,        "ost_reply" },
58         { OST_GETATTR,      "ost_getattr" },
59         { OST_SETATTR,      "ost_setattr" },
60         { OST_READ,         "ost_read" },
61         { OST_WRITE,        "ost_write" },
62         { OST_CREATE ,      "ost_create" },
63         { OST_DESTROY,      "ost_destroy" },
64         { OST_GET_INFO,     "ost_get_info" },
65         { OST_CONNECT,      "ost_connect" },
66         { OST_DISCONNECT,   "ost_disconnect" },
67         { OST_PUNCH,        "ost_punch" },
68         { OST_OPEN,         "ost_open" },
69         { OST_CLOSE,        "ost_close" },
70         { OST_STATFS,       "ost_statfs" },
71         { 14,                NULL },    /* formerly OST_SAN_READ */
72         { 15,                NULL },    /* formerly OST_SAN_WRITE */
73         { OST_SYNC,         "ost_sync" },
74         { OST_SET_INFO,     "ost_set_info" },
75         { OST_QUOTACHECK,   "ost_quotacheck" },
76         { OST_QUOTACTL,     "ost_quotactl" },
77         { OST_QUOTA_ADJUST_QUNIT, "ost_quota_adjust_qunit" },
78         { MDS_GETATTR,      "mds_getattr" },
79         { MDS_GETATTR_NAME, "mds_getattr_lock" },
80         { MDS_CLOSE,        "mds_close" },
81         { MDS_REINT,        "mds_reint" },
82         { MDS_READPAGE,     "mds_readpage" },
83         { MDS_CONNECT,      "mds_connect" },
84         { MDS_DISCONNECT,   "mds_disconnect" },
85         { MDS_GETSTATUS,    "mds_getstatus" },
86         { MDS_STATFS,       "mds_statfs" },
87         { MDS_PIN,          "mds_pin" },
88         { MDS_UNPIN,        "mds_unpin" },
89         { MDS_SYNC,         "mds_sync" },
90         { MDS_DONE_WRITING, "mds_done_writing" },
91         { MDS_SET_INFO,     "mds_set_info" },
92         { MDS_QUOTACHECK,   "mds_quotacheck" },
93         { MDS_QUOTACTL,     "mds_quotactl" },
94         { MDS_GETXATTR,     "mds_getxattr" },
95         { MDS_SETXATTR,     "mds_setxattr" },
96         { MDS_WRITEPAGE,    "mds_writepage" },
97         { MDS_IS_SUBDIR,    "mds_is_subdir" },
98         { MDS_GET_INFO,     "mds_get_info" },
99         { LDLM_ENQUEUE,     "ldlm_enqueue" },
100         { LDLM_CONVERT,     "ldlm_convert" },
101         { LDLM_CANCEL,      "ldlm_cancel" },
102         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
103         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
104         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
105         { LDLM_SET_INFO,    "ldlm_set_info" },
106         { MGS_CONNECT,      "mgs_connect" },
107         { MGS_DISCONNECT,   "mgs_disconnect" },
108         { MGS_EXCEPTION,    "mgs_exception" },
109         { MGS_TARGET_REG,   "mgs_target_reg" },
110         { MGS_TARGET_DEL,   "mgs_target_del" },
111         { MGS_SET_INFO,     "mgs_set_info" },
112         { MGS_CONFIG_READ,  "mgs_config_read" },
113         { OBD_PING,         "obd_ping" },
114         { OBD_LOG_CANCEL,   "llog_origin_handle_cancel" },
115         { OBD_QC_CALLBACK,  "obd_quota_callback" },
116         { LLOG_ORIGIN_HANDLE_CREATE,     "llog_origin_handle_create" },
117         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block" },
118         { LLOG_ORIGIN_HANDLE_READ_HEADER,"llog_origin_handle_read_header" },
119         { LLOG_ORIGIN_HANDLE_WRITE_REC,  "llog_origin_handle_write_rec" },
120         { LLOG_ORIGIN_HANDLE_CLOSE,      "llog_origin_handle_close" },
121         { LLOG_ORIGIN_CONNECT,           "llog_origin_connect" },
122         { LLOG_CATINFO,                  "llog_catinfo" },
123         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
124         { LLOG_ORIGIN_HANDLE_DESTROY,    "llog_origin_handle_destroy" },
125         { QUOTA_DQACQ,      "quota_acquire" },
126         { QUOTA_DQREL,      "quota_release" },
127         { SEQ_QUERY,        "seq_query" },
128         { SEC_CTX_INIT,     "sec_ctx_init" },
129         { SEC_CTX_INIT_CONT,"sec_ctx_init_cont" },
130         { SEC_CTX_FINI,     "sec_ctx_fini" },
131         { FLD_QUERY,        "fld_query" }
132 };
133
134 struct ll_eopcode {
135      __u32       opcode;
136      const char *opname;
137 } ll_eopcode_table[EXTRA_LAST_OPC] = {
138         { LDLM_GLIMPSE_ENQUEUE, "ldlm_glimpse_enqueue" },
139         { LDLM_PLAIN_ENQUEUE,   "ldlm_plain_enqueue" },
140         { LDLM_EXTENT_ENQUEUE,  "ldlm_extent_enqueue" },
141         { LDLM_FLOCK_ENQUEUE,   "ldlm_flock_enqueue" },
142         { LDLM_IBITS_ENQUEUE,   "ldlm_ibits_enqueue" },
143         { MDS_REINT_SETATTR,    "mds_reint_setattr" },
144         { MDS_REINT_CREATE,     "mds_reint_create" },
145         { MDS_REINT_LINK,       "mds_reint_link" },
146         { MDS_REINT_UNLINK,     "mds_reint_unlink" },
147         { MDS_REINT_RENAME,     "mds_reint_rename" },
148         { MDS_REINT_OPEN,       "mds_reint_open" },
149         { MDS_REINT_SETXATTR,   "mds_reint_setxattr" },
150         { BRW_READ_BYTES,       "read_bytes" },
151         { BRW_WRITE_BYTES,      "write_bytes" },
152 };
153
154 const char *ll_opcode2str(__u32 opcode)
155 {
156         /* When one of the assertions below fail, chances are that:
157          *     1) A new opcode was added in include/lustre/lustre_idl.h,
158          *        but is missing from the table above.
159          * or  2) The opcode space was renumbered or rearranged,
160          *        and the opcode_offset() function in
161          *        ptlrpc_internal.h needs to be modified.
162          */
163         __u32 offset = opcode_offset(opcode);
164         LASSERTF(offset < LUSTRE_MAX_OPCODES,
165                  "offset %u >= LUSTRE_MAX_OPCODES %u\n",
166                  offset, LUSTRE_MAX_OPCODES);
167         LASSERTF(ll_rpc_opcode_table[offset].opcode == opcode,
168                  "ll_rpc_opcode_table[%u].opcode %u != opcode %u\n",
169                  offset, ll_rpc_opcode_table[offset].opcode, opcode);
170         return ll_rpc_opcode_table[offset].opname;
171 }
172
173 const char* ll_eopcode2str(__u32 opcode)
174 {
175         LASSERT(ll_eopcode_table[opcode].opcode == opcode);
176         return ll_eopcode_table[opcode].opname;
177 }
178 #ifdef LPROCFS
179 void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
180                              char *name, struct proc_dir_entry **procroot_ret,
181                              struct lprocfs_stats **stats_ret)
182 {
183         struct proc_dir_entry *svc_procroot;
184         struct lprocfs_stats *svc_stats;
185         int i, rc;
186         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
187                                           LPROCFS_CNTR_STDDEV;
188
189         LASSERT(*procroot_ret == NULL);
190         LASSERT(*stats_ret == NULL);
191
192         svc_stats = lprocfs_alloc_stats(EXTRA_MAX_OPCODES+LUSTRE_MAX_OPCODES,0);
193         if (svc_stats == NULL)
194                 return;
195
196         if (dir) {
197                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
198                 if (IS_ERR(svc_procroot)) {
199                         lprocfs_free_stats(&svc_stats);
200                         return;
201                 }
202         } else {
203                 svc_procroot = root;
204         }
205
206         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
207                              svc_counter_config, "req_waittime", "usec");
208         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
209                              svc_counter_config, "req_qdepth", "reqs");
210         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
211                              svc_counter_config, "req_active", "reqs");
212         lprocfs_counter_init(svc_stats, PTLRPC_TIMEOUT,
213                              svc_counter_config, "req_timeout", "sec");
214         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
215                              svc_counter_config, "reqbuf_avail", "bufs");
216         for (i = 0; i < EXTRA_LAST_OPC; i++) {
217                 char *units;
218
219                 switch(i) {
220                 case BRW_WRITE_BYTES:
221                 case BRW_READ_BYTES:
222                         units = "bytes";
223                         break;
224                 default:
225                         units = "reqs";
226                         break;
227                 }
228                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
229                                      svc_counter_config,
230                                      ll_eopcode2str(i), units);
231         }
232         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
233                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
234                 lprocfs_counter_init(svc_stats,
235                                      EXTRA_MAX_OPCODES + i, svc_counter_config,
236                                      ll_opcode2str(opcode), "usec");
237         }
238
239         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
240         if (rc < 0) {
241                 if (dir)
242                         lprocfs_remove(&svc_procroot);
243                 lprocfs_free_stats(&svc_stats);
244         } else {
245                 if (dir)
246                         *procroot_ret = svc_procroot;
247                 *stats_ret = svc_stats;
248         }
249 }
250
251 static int
252 ptlrpc_lprocfs_read_req_history_len(char *page, char **start, off_t off,
253                                     int count, int *eof, void *data)
254 {
255         struct ptlrpc_service *svc = data;
256
257         *eof = 1;
258         return snprintf(page, count, "%d\n", svc->srv_n_history_rqbds);
259 }
260
261 static int
262 ptlrpc_lprocfs_read_req_history_max(char *page, char **start, off_t off,
263                                     int count, int *eof, void *data)
264 {
265         struct ptlrpc_service *svc = data;
266
267         *eof = 1;
268         return snprintf(page, count, "%d\n", svc->srv_max_history_rqbds);
269 }
270
271 static int
272 ptlrpc_lprocfs_write_req_history_max(struct file *file, const char *buffer,
273                                      unsigned long count, void *data)
274 {
275         struct ptlrpc_service *svc = data;
276         int                    bufpages;
277         int                    val;
278         int                    rc = lprocfs_write_helper(buffer, count, &val);
279
280         if (rc < 0)
281                 return rc;
282
283         if (val < 0)
284                 return -ERANGE;
285
286         /* This sanity check is more of an insanity check; we can still
287          * hose a kernel by allowing the request history to grow too
288          * far. */
289         bufpages = (svc->srv_buf_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
290         if (val > cfs_num_physpages/(2 * bufpages))
291                 return -ERANGE;
292
293         cfs_spin_lock(&svc->srv_lock);
294         svc->srv_max_history_rqbds = val;
295         cfs_spin_unlock(&svc->srv_lock);
296
297         return count;
298 }
299
300 static int
301 ptlrpc_lprocfs_rd_threads_min(char *page, char **start, off_t off,
302                               int count, int *eof, void *data)
303 {
304         struct ptlrpc_service *svc = data;
305
306         return snprintf(page, count, "%d\n", svc->srv_threads_min);
307 }
308
309 static int
310 ptlrpc_lprocfs_wr_threads_min(struct file *file, const char *buffer,
311                               unsigned long count, void *data)
312 {
313         struct ptlrpc_service *svc = data;
314         int                    val;
315         int                    rc = lprocfs_write_helper(buffer, count, &val);
316
317         if (rc < 0)
318                 return rc;
319
320         if (val < 2)
321                 return -ERANGE;
322
323         if (val > svc->srv_threads_max)
324                 return -ERANGE;
325
326         cfs_spin_lock(&svc->srv_lock);
327         svc->srv_threads_min = val;
328         cfs_spin_unlock(&svc->srv_lock);
329
330         return count;
331 }
332
333 static int
334 ptlrpc_lprocfs_rd_threads_started(char *page, char **start, off_t off,
335                                   int count, int *eof, void *data)
336 {
337         struct ptlrpc_service *svc = data;
338
339         return snprintf(page, count, "%d\n", svc->srv_threads_running);
340 }
341
342 static int
343 ptlrpc_lprocfs_rd_threads_max(char *page, char **start, off_t off,
344                               int count, int *eof, void *data)
345 {
346         struct ptlrpc_service *svc = data;
347
348         return snprintf(page, count, "%d\n", svc->srv_threads_max);
349 }
350
351 static int
352 ptlrpc_lprocfs_wr_threads_max(struct file *file, const char *buffer,
353                               unsigned long count, void *data)
354 {
355         struct ptlrpc_service *svc = data;
356         int                    val;
357         int                    rc = lprocfs_write_helper(buffer, count, &val);
358
359         if (rc < 0)
360                 return rc;
361
362         if (val < 2)
363                 return -ERANGE;
364
365         if (val < svc->srv_threads_min)
366                 return -ERANGE;
367
368         cfs_spin_lock(&svc->srv_lock);
369         svc->srv_threads_max = val;
370         cfs_spin_unlock(&svc->srv_lock);
371
372         return count;
373 }
374
375 struct ptlrpc_srh_iterator {
376         __u64                  srhi_seq;
377         struct ptlrpc_request *srhi_req;
378 };
379
380 int
381 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service *svc,
382                                     struct ptlrpc_srh_iterator *srhi,
383                                     __u64 seq)
384 {
385         cfs_list_t            *e;
386         struct ptlrpc_request *req;
387
388         if (srhi->srhi_req != NULL &&
389             srhi->srhi_seq > svc->srv_request_max_cull_seq &&
390             srhi->srhi_seq <= seq) {
391                 /* If srhi_req was set previously, hasn't been culled and
392                  * we're searching for a seq on or after it (i.e. more
393                  * recent), search from it onwards.
394                  * Since the service history is LRU (i.e. culled reqs will
395                  * be near the head), we shouldn't have to do long
396                  * re-scans */
397                 LASSERT (srhi->srhi_seq == srhi->srhi_req->rq_history_seq);
398                 LASSERT (!cfs_list_empty(&svc->srv_request_history));
399                 e = &srhi->srhi_req->rq_history_list;
400         } else {
401                 /* search from start */
402                 e = svc->srv_request_history.next;
403         }
404
405         while (e != &svc->srv_request_history) {
406                 req = cfs_list_entry(e, struct ptlrpc_request, rq_history_list);
407
408                 if (req->rq_history_seq >= seq) {
409                         srhi->srhi_seq = req->rq_history_seq;
410                         srhi->srhi_req = req;
411                         return 0;
412                 }
413                 e = e->next;
414         }
415
416         return -ENOENT;
417 }
418
419 static void *
420 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
421 {
422         struct ptlrpc_service       *svc = s->private;
423         struct ptlrpc_srh_iterator  *srhi;
424         int                          rc;
425
426         OBD_ALLOC(srhi, sizeof(*srhi));
427         if (srhi == NULL)
428                 return NULL;
429
430         srhi->srhi_seq = 0;
431         srhi->srhi_req = NULL;
432
433         cfs_spin_lock(&svc->srv_lock);
434         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos);
435         cfs_spin_unlock(&svc->srv_lock);
436
437         if (rc == 0) {
438                 *pos = srhi->srhi_seq;
439                 return srhi;
440         }
441
442         OBD_FREE(srhi, sizeof(*srhi));
443         return NULL;
444 }
445
446 static void
447 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
448 {
449         struct ptlrpc_srh_iterator *srhi = iter;
450
451         if (srhi != NULL)
452                 OBD_FREE(srhi, sizeof(*srhi));
453 }
454
455 static void *
456 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
457                                     void *iter, loff_t *pos)
458 {
459         struct ptlrpc_service       *svc = s->private;
460         struct ptlrpc_srh_iterator  *srhi = iter;
461         int                          rc;
462
463         cfs_spin_lock(&svc->srv_lock);
464         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos + 1);
465         cfs_spin_unlock(&svc->srv_lock);
466
467         if (rc != 0) {
468                 OBD_FREE(srhi, sizeof(*srhi));
469                 return NULL;
470         }
471
472         *pos = srhi->srhi_seq;
473         return srhi;
474 }
475
476 /* common ost/mdt srv_req_printfn */
477 void target_print_req(void *seq_file, struct ptlrpc_request *req)
478 {
479         /* Called holding srv_lock with irqs disabled.
480          * Print specific req contents and a newline.
481          * CAVEAT EMPTOR: check request message length before printing!!!
482          * You might have received any old crap so you must be just as
483          * careful here as the service's request parser!!! */
484         struct seq_file *sf = seq_file;
485
486         switch (req->rq_phase) {
487         case RQ_PHASE_NEW:
488                 /* still awaiting a service thread's attention, or rejected
489                  * because the generic request message didn't unpack */
490                 seq_printf(sf, "<not swabbed>\n");
491                 break;
492         case RQ_PHASE_INTERPRET:
493                 /* being handled, so basic msg swabbed, and opc is valid
494                  * but racing with mds_handle() */
495         case RQ_PHASE_COMPLETE:
496                 /* been handled by mds_handle() reply state possibly still
497                  * volatile */
498                 seq_printf(sf, "opc %d\n", lustre_msg_get_opc(req->rq_reqmsg));
499                 break;
500         default:
501                 DEBUG_REQ(D_ERROR, req, "bad phase %d", req->rq_phase);
502         }
503 }
504 EXPORT_SYMBOL(target_print_req);
505
506 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
507 {
508         struct ptlrpc_service      *svc = s->private;
509         struct ptlrpc_srh_iterator *srhi = iter;
510         struct ptlrpc_request      *req;
511         int                         rc;
512
513         cfs_spin_lock(&svc->srv_lock);
514
515         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, srhi->srhi_seq);
516
517         if (rc == 0) {
518                 req = srhi->srhi_req;
519
520                 /* Print common req fields.
521                  * CAVEAT EMPTOR: we're racing with the service handler
522                  * here.  The request could contain any old crap, so you
523                  * must be just as careful as the service's request
524                  * parser. Currently I only print stuff here I know is OK
525                  * to look at coz it was set up in request_in_callback()!!! */
526                 seq_printf(s, LPD64":%s:%s:x"LPU64":%d:%s:%ld:%lds(%+lds) ",
527                            req->rq_history_seq, libcfs_nid2str(req->rq_self),
528                            libcfs_id2str(req->rq_peer), req->rq_xid,
529                            req->rq_reqlen, ptlrpc_rqphase2str(req),
530                            req->rq_arrival_time.tv_sec,
531                            req->rq_sent - req->rq_arrival_time.tv_sec,
532                            req->rq_sent - req->rq_deadline);
533                 if (svc->srv_req_printfn == NULL)
534                         seq_printf(s, "\n");
535                 else
536                         svc->srv_req_printfn(s, srhi->srhi_req);
537         }
538
539         cfs_spin_unlock(&svc->srv_lock);
540
541         return rc;
542 }
543
544 static int
545 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
546 {
547         static struct seq_operations sops = {
548                 .start = ptlrpc_lprocfs_svc_req_history_start,
549                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
550                 .next  = ptlrpc_lprocfs_svc_req_history_next,
551                 .show  = ptlrpc_lprocfs_svc_req_history_show,
552         };
553         struct proc_dir_entry *dp = PDE(inode);
554         struct seq_file       *seqf;
555         int                    rc;
556
557         LPROCFS_ENTRY_AND_CHECK(dp);
558         rc = seq_open(file, &sops);
559         if (rc) {
560                 LPROCFS_EXIT();
561                 return rc;
562         }
563
564         seqf = file->private_data;
565         seqf->private = dp->data;
566         return 0;
567 }
568
569 /* See also lprocfs_rd_timeouts */
570 static int ptlrpc_lprocfs_rd_timeouts(char *page, char **start, off_t off,
571                                       int count, int *eof, void *data)
572 {
573         struct ptlrpc_service *svc = data;
574         unsigned int cur, worst;
575         time_t worstt;
576         struct dhms ts;
577         int rc = 0;
578
579         *eof = 1;
580         cur = at_get(&svc->srv_at_estimate);
581         worst = svc->srv_at_estimate.at_worst_ever;
582         worstt = svc->srv_at_estimate.at_worst_time;
583         s2dhms(&ts, cfs_time_current_sec() - worstt);
584         if (AT_OFF)
585                 rc += snprintf(page + rc, count - rc,
586                               "adaptive timeouts off, using obd_timeout %u\n",
587                               obd_timeout);
588         rc += snprintf(page + rc, count - rc,
589                        "%10s : cur %3u  worst %3u (at %ld, "DHMS_FMT" ago) ",
590                        "service", cur, worst, worstt,
591                        DHMS_VARS(&ts));
592         rc = lprocfs_at_hist_helper(page, count, rc,
593                                     &svc->srv_at_estimate);
594         return rc;
595 }
596
597 static int ptlrpc_lprocfs_rd_hp_ratio(char *page, char **start, off_t off,
598                                       int count, int *eof, void *data)
599 {
600         struct ptlrpc_service *svc = data;
601         int rc = snprintf(page, count, "%d", svc->srv_hpreq_ratio);
602         return rc;
603 }
604
605 static int ptlrpc_lprocfs_wr_hp_ratio(struct file *file, const char *buffer,
606                                       unsigned long count, void *data)
607 {
608         struct ptlrpc_service *svc = data;
609         int rc, val;
610
611         rc = lprocfs_write_helper(buffer, count, &val);
612         if (rc < 0)
613                 return rc;
614         if (val < 0)
615                 return -ERANGE;
616
617         cfs_spin_lock(&svc->srv_lock);
618         svc->srv_hpreq_ratio = val;
619         cfs_spin_unlock(&svc->srv_lock);
620         return count;
621 }
622
623 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
624                                      struct ptlrpc_service *svc)
625 {
626         struct lprocfs_vars lproc_vars[] = {
627                 {.name       = "high_priority_ratio",
628                  .read_fptr  = ptlrpc_lprocfs_rd_hp_ratio,
629                  .write_fptr = ptlrpc_lprocfs_wr_hp_ratio,
630                  .data       = svc},
631                 {.name       = "req_buffer_history_len",
632                  .read_fptr  = ptlrpc_lprocfs_read_req_history_len,
633                  .data       = svc},
634                 {.name       = "req_buffer_history_max",
635                  .write_fptr = ptlrpc_lprocfs_write_req_history_max,
636                  .read_fptr  = ptlrpc_lprocfs_read_req_history_max,
637                  .data       = svc},
638                 {.name       = "threads_min",
639                  .read_fptr  = ptlrpc_lprocfs_rd_threads_min,
640                  .write_fptr = ptlrpc_lprocfs_wr_threads_min,
641                  .data       = svc},
642                 {.name       = "threads_max",
643                  .read_fptr  = ptlrpc_lprocfs_rd_threads_max,
644                  .write_fptr = ptlrpc_lprocfs_wr_threads_max,
645                  .data       = svc},
646                 {.name       = "threads_started",
647                  .read_fptr  = ptlrpc_lprocfs_rd_threads_started,
648                  .data       = svc},
649                 {.name       = "timeouts",
650                  .read_fptr  = ptlrpc_lprocfs_rd_timeouts,
651                  .data       = svc},
652                 {NULL}
653         };
654         static struct file_operations req_history_fops = {
655                 .owner       = THIS_MODULE,
656                 .open        = ptlrpc_lprocfs_svc_req_history_open,
657                 .read        = seq_read,
658                 .llseek      = seq_lseek,
659                 .release     = lprocfs_seq_release,
660         };
661
662         int rc;
663
664         ptlrpc_lprocfs_register(entry, svc->srv_name,
665                                 "stats", &svc->srv_procroot,
666                                 &svc->srv_stats);
667
668         if (svc->srv_procroot == NULL)
669                 return;
670
671         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
672
673         rc = lprocfs_seq_create(svc->srv_procroot, "req_history",
674                                 0400, &req_history_fops, svc);
675         if (rc)
676                 CWARN("Error adding the req_history file\n");
677 }
678
679 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
680 {
681         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
682                                 &obddev->obd_svc_procroot,
683                                 &obddev->obd_svc_stats);
684 }
685 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
686
687 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req, long amount)
688 {
689         struct lprocfs_stats *svc_stats;
690         __u32 op = lustre_msg_get_opc(req->rq_reqmsg);
691         int opc = opcode_offset(op);
692
693         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
694         if (svc_stats == NULL || opc <= 0)
695                 return;
696         LASSERT(opc < LUSTRE_MAX_OPCODES);
697         if (!(op == LDLM_ENQUEUE || op == MDS_REINT))
698                 lprocfs_counter_add(svc_stats, opc + EXTRA_MAX_OPCODES, amount);
699 }
700
701 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int bytes)
702 {
703         struct lprocfs_stats *svc_stats;
704         int idx;
705
706         if (!req->rq_import)
707                 return;
708         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
709         if (!svc_stats)
710                 return;
711         idx = lustre_msg_get_opc(req->rq_reqmsg);
712         switch (idx) {
713         case OST_READ:
714                 idx = BRW_READ_BYTES + PTLRPC_LAST_CNTR;
715                 break;
716         case OST_WRITE:
717                 idx = BRW_WRITE_BYTES + PTLRPC_LAST_CNTR;
718                 break;
719         default:
720                 LASSERTF(0, "unsupported opcode %u\n", idx);
721                 break;
722         }
723
724         lprocfs_counter_add(svc_stats, idx, bytes);
725 }
726
727 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
728
729 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
730 {
731         if (svc->srv_procroot != NULL)
732                 lprocfs_remove(&svc->srv_procroot);
733
734         if (svc->srv_stats)
735                 lprocfs_free_stats(&svc->srv_stats);
736 }
737
738 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
739 {
740         if (obd->obd_svc_procroot)
741                 lprocfs_remove(&obd->obd_svc_procroot);
742
743         if (obd->obd_svc_stats)
744                 lprocfs_free_stats(&obd->obd_svc_stats);
745 }
746 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
747
748
749 #define BUFLEN (UUID_MAX + 5)
750
751 int lprocfs_wr_evict_client(struct file *file, const char *buffer,
752                             unsigned long count, void *data)
753 {
754         struct obd_device *obd = data;
755         char              *kbuf;
756         char              *tmpbuf;
757
758         OBD_ALLOC(kbuf, BUFLEN);
759         if (kbuf == NULL)
760                 return -ENOMEM;
761
762         /*
763          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
764          * bytes into kbuf, to ensure that the string is NUL-terminated.
765          * UUID_MAX should include a trailing NUL already.
766          */
767         if (cfs_copy_from_user(kbuf, buffer,
768                                min_t(unsigned long, BUFLEN - 1, count))) {
769                 count = -EFAULT;
770                 goto out;
771         }
772         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
773         /* Kludge code(deadlock situation): the lprocfs lock has been held
774          * since the client is evicted by writting client's
775          * uuid/nid to procfs "evict_client" entry. However,
776          * obd_export_evict_by_uuid() will call lprocfs_remove() to destroy
777          * the proc entries under the being destroyed export{}, so I have
778          * to drop the lock at first here.
779          * - jay, jxiong@clusterfs.com */
780         class_incref(obd, __FUNCTION__, cfs_current());
781         LPROCFS_EXIT();
782
783         if (strncmp(tmpbuf, "nid:", 4) == 0)
784                 obd_export_evict_by_nid(obd, tmpbuf + 4);
785         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
786                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
787         else
788                 obd_export_evict_by_uuid(obd, tmpbuf);
789
790         LPROCFS_ENTRY();
791         class_decref(obd, __FUNCTION__, cfs_current());
792
793 out:
794         OBD_FREE(kbuf, BUFLEN);
795         return count;
796 }
797 EXPORT_SYMBOL(lprocfs_wr_evict_client);
798
799 #undef BUFLEN
800
801 int lprocfs_wr_ping(struct file *file, const char *buffer,
802                     unsigned long count, void *data)
803 {
804         struct obd_device     *obd = data;
805         struct ptlrpc_request *req;
806         int                    rc;
807         ENTRY;
808
809         LPROCFS_CLIMP_CHECK(obd);
810         req = ptlrpc_prep_ping(obd->u.cli.cl_import);
811         LPROCFS_CLIMP_EXIT(obd);
812         if (req == NULL)
813                 RETURN(-ENOMEM);
814
815         req->rq_send_state = LUSTRE_IMP_FULL;
816
817         rc = ptlrpc_queue_wait(req);
818
819         ptlrpc_req_finished(req);
820         if (rc >= 0)
821                 RETURN(count);
822         RETURN(rc);
823 }
824 EXPORT_SYMBOL(lprocfs_wr_ping);
825
826 /* Write the connection UUID to this file to attempt to connect to that node.
827  * The connection UUID is a node's primary NID. For example,
828  * "echo connection=192.168.0.1@tcp0::instance > .../import".
829  */
830 int lprocfs_wr_import(struct file *file, const char *buffer,
831                       unsigned long count, void *data)
832 {
833         struct obd_device *obd = data;
834         struct obd_import *imp = obd->u.cli.cl_import;
835         char *kbuf = NULL;
836         char *uuid;
837         char *ptr;
838         int do_reconn = 1;
839         const char prefix[] = "connection=";
840         const int prefix_len = sizeof(prefix) - 1;
841
842         if (count > CFS_PAGE_SIZE - 1 || count <= prefix_len)
843                 return -EINVAL;
844
845         OBD_ALLOC(kbuf, count + 1);
846         if (kbuf == NULL)
847                 return -ENOMEM;
848
849         if (cfs_copy_from_user(kbuf, buffer, count))
850                 GOTO(out, count = -EFAULT);
851
852         kbuf[count] = 0;
853
854         /* only support connection=uuid::instance now */
855         if (strncmp(prefix, kbuf, prefix_len) != 0)
856                 GOTO(out, count = -EINVAL);
857
858         uuid = kbuf + prefix_len;
859         ptr = strstr(uuid, "::");
860         if (ptr) {
861                 __u32 inst;
862                 char *endptr;
863
864                 *ptr = 0;
865                 do_reconn = 0;
866                 ptr += strlen("::");
867                 inst = simple_strtol(ptr, &endptr, 10);
868                 if (*endptr) {
869                         CERROR("config: wrong instance # %s\n", ptr);
870                 } else if (inst != imp->imp_connect_data.ocd_instance) {
871                         CDEBUG(D_INFO, "IR: %s is connecting to an obsoleted "
872                                "target(%u/%u), reconnecting...\n",
873                                imp->imp_obd->obd_name,
874                                imp->imp_connect_data.ocd_instance, inst);
875                         do_reconn = 1;
876                 } else {
877                         CDEBUG(D_INFO, "IR: %s has already been connecting to "
878                                "new target(%u)\n",
879                                imp->imp_obd->obd_name, inst);
880                 }
881         }
882
883         if (do_reconn)
884                 ptlrpc_recover_import(imp, uuid, 1);
885
886 out:
887         OBD_FREE(kbuf, count + 1);
888         return count;
889 }
890 EXPORT_SYMBOL(lprocfs_wr_import);
891
892 int lprocfs_rd_pinger_recov(char *page, char **start, off_t off,
893                             int count, int *eof, void *data)
894 {
895         struct obd_device *obd = data;
896         struct obd_import *imp = obd->u.cli.cl_import;
897         int rc;
898
899         LPROCFS_CLIMP_CHECK(obd);
900         rc = snprintf(page, count, "%d\n", !imp->imp_no_pinger_recover);
901         LPROCFS_CLIMP_EXIT(obd);
902
903         return rc;
904 }
905 EXPORT_SYMBOL(lprocfs_rd_pinger_recov);
906
907 int lprocfs_wr_pinger_recov(struct file *file, const char *buffer,
908                       unsigned long count, void *data)
909 {
910         struct obd_device *obd = data;
911         struct client_obd *cli = &obd->u.cli;
912         struct obd_import *imp = cli->cl_import;
913         int rc, val;
914
915         rc = lprocfs_write_helper(buffer, count, &val);
916         if (rc < 0)
917                 return rc;
918
919         if (val != 0 && val != 1)
920                 return -ERANGE;
921
922         LPROCFS_CLIMP_CHECK(obd);
923         cfs_spin_lock(&imp->imp_lock);
924         imp->imp_no_pinger_recover = !val;
925         cfs_spin_unlock(&imp->imp_lock);
926         LPROCFS_CLIMP_EXIT(obd);
927
928         return count;
929
930 }
931 EXPORT_SYMBOL(lprocfs_wr_pinger_recov);
932
933 #endif /* LPROCFS */