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