Whamcloud - gitweb
10ee5fc07d4f40b0a28c56eed8b5f01ee3163b0a
[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  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25 #define DEBUG_SUBSYSTEM S_CLASS
26
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <obd.h>
33 #include <lprocfs_status.h>
34 #include <lustre/lustre_idl.h>
35 #include <lustre_net.h>
36 #include <obd_class.h>
37 #include "ptlrpc_internal.h"
38
39
40 struct ll_rpc_opcode {
41      __u32       opcode;
42      const char *opname;
43 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
44         { OST_REPLY,        "ost_reply" },
45         { OST_GETATTR,      "ost_getattr" },
46         { OST_SETATTR,      "ost_setattr" },
47         { OST_READ,         "ost_read" },
48         { OST_WRITE,        "ost_write" },
49         { OST_CREATE ,      "ost_create" },
50         { OST_DESTROY,      "ost_destroy" },
51         { OST_GET_INFO,     "ost_get_info" },
52         { OST_CONNECT,      "ost_connect" },
53         { OST_DISCONNECT,   "ost_disconnect" },
54         { OST_PUNCH,        "ost_punch" },
55         { OST_OPEN,         "ost_open" },
56         { OST_CLOSE,        "ost_close" },
57         { OST_STATFS,       "ost_statfs" },
58         { 14,               "ost_san_read" },
59         { 15,               "ost_san_write" },
60         { OST_SYNC,         "ost_sync" },
61         { OST_SET_INFO,     "ost_set_info" },
62         { OST_QUOTACHECK,   "ost_quotacheck" },
63         { OST_QUOTACTL,     "ost_quotactl" },
64         { MDS_GETATTR,      "mds_getattr" },
65         { MDS_GETATTR_NAME, "mds_getattr_lock" },
66         { MDS_CLOSE,        "mds_close" },
67         { MDS_REINT,        "mds_reint" },
68         { MDS_READPAGE,     "mds_readpage" },
69         { MDS_CONNECT,      "mds_connect" },
70         { MDS_DISCONNECT,   "mds_disconnect" },
71         { MDS_GETSTATUS,    "mds_getstatus" },
72         { MDS_STATFS,       "mds_statfs" },
73         { MDS_PIN,          "mds_pin" },
74         { MDS_UNPIN,        "mds_unpin" },
75         { MDS_SYNC,         "mds_sync" },
76         { MDS_DONE_WRITING, "mds_done_writing" },
77         { MDS_SET_INFO,     "mds_set_info" },
78         { MDS_QUOTACHECK,   "mds_quotacheck" },
79         { MDS_QUOTACTL,     "mds_quotactl" },
80         { MDS_GETXATTR,     "mds_getxattr" },
81         { MDS_SETXATTR,     "mds_setxattr" },
82         { MDS_WRITEPAGE,    "mds_writepage" },
83         { MDS_IS_SUBDIR,    "mds_is_subdir" },
84         { LDLM_ENQUEUE,     "ldlm_enqueue" },
85         { LDLM_CONVERT,     "ldlm_convert" },
86         { LDLM_CANCEL,      "ldlm_cancel" },
87         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
88         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
89         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
90         { MGS_CONNECT,      "mgs_connect" },
91         { MGS_DISCONNECT,   "mgs_disconnect" },
92         { MGS_EXCEPTION,    "mgs_exception" },
93         { MGS_TARGET_REG,   "mgs_target_reg" },
94         { MGS_TARGET_DEL,   "mgs_target_del" },
95         { MGS_SET_INFO,     "mgs_set_info" },
96         { OBD_PING,         "obd_ping" },
97         { OBD_LOG_CANCEL,   "llog_origin_handle_cancel" },
98         { OBD_QC_CALLBACK,  "obd_qc_callback" },
99         { LLOG_ORIGIN_HANDLE_CREATE, "llog_origin_handle_create" },
100         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block" },
101         { LLOG_ORIGIN_HANDLE_READ_HEADER, "llog_origin_handle_read_header" },
102         { LLOG_ORIGIN_HANDLE_WRITE_REC, "llog_origin_handle_write_rec" },
103         { LLOG_ORIGIN_HANDLE_CLOSE, "llog_origin_handle_close" },
104         { LLOG_ORIGIN_CONNECT, "llog_origin_connect" },
105         { LLOG_CATINFO,     "llog_catinfo" },
106         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
107         { LLOG_ORIGIN_HANDLE_DESTROY, "llog_origin_handle_destroy" },
108         { FLD_QUERY,        "fld_query" },
109         { SEQ_QUERY,        "seq_query" },
110         { SEC_CTX_INIT,     "sec_ctx_init" },
111         { SEC_CTX_INIT_CONT,"sec_ctx_init_cont" },
112         { SEC_CTX_FINI,     "sec_ctx_fini" }
113 };
114
115 const char* ll_opcode2str(__u32 opcode)
116 {
117         /* When one of the assertions below fail, chances are that:
118          *     1) A new opcode was added in lustre_idl.h, but was
119          *        is missing from the table above.
120          * or  2) The opcode space was renumbered or rearranged,
121          *        and the opcode_offset() function in
122          *        ptlrpc_internal.h needs to be modified.
123          */
124         __u32 offset = opcode_offset(opcode);
125         LASSERT(offset < LUSTRE_MAX_OPCODES);
126         LASSERT(ll_rpc_opcode_table[offset].opcode == opcode);
127         return ll_rpc_opcode_table[offset].opname;
128 }
129
130 #ifdef LPROCFS
131 void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
132                              char *name, struct proc_dir_entry **procroot_ret,
133                              struct lprocfs_stats **stats_ret)
134 {
135         struct proc_dir_entry *svc_procroot;
136         struct lprocfs_stats *svc_stats;
137         int i, rc;
138         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
139                                           LPROCFS_CNTR_STDDEV;
140
141         LASSERT(*procroot_ret == NULL);
142         LASSERT(*stats_ret == NULL);
143
144         svc_stats = lprocfs_alloc_stats(PTLRPC_LAST_CNTR + LUSTRE_MAX_OPCODES);
145         if (svc_stats == NULL)
146                 return;
147
148         if (dir) {
149                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
150                 if (IS_ERR(svc_procroot)) {
151                         lprocfs_free_stats(&svc_stats);
152                         return;
153                 }
154         } else {
155                 svc_procroot = root;
156         }
157
158         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
159                              svc_counter_config, "req_waittime", "usec");
160         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
161                              svc_counter_config, "req_qdepth", "reqs");
162         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
163                              svc_counter_config, "req_active", "reqs");
164         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
165                              svc_counter_config, "reqbuf_avail", "bufs");
166         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
167                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
168                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
169                                      svc_counter_config, ll_opcode2str(opcode),
170                                      "usec");
171         }
172
173         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
174         if (rc < 0) {
175                 if (dir)
176                         lprocfs_remove(&svc_procroot);
177                 lprocfs_free_stats(&svc_stats);
178         } else {
179                 if (dir)
180                         *procroot_ret = svc_procroot;
181                 *stats_ret = svc_stats;
182         }
183 }
184
185 static int
186 ptlrpc_lprocfs_read_req_history_len(char *page, char **start, off_t off,
187                                     int count, int *eof, void *data)
188 {
189         struct ptlrpc_service *svc = data;
190
191         *eof = 1;
192         return snprintf(page, count, "%d\n", svc->srv_n_history_rqbds);
193 }
194
195 static int
196 ptlrpc_lprocfs_read_req_history_max(char *page, char **start, off_t off,
197                                     int count, int *eof, void *data)
198 {
199         struct ptlrpc_service *svc = data;
200
201         *eof = 1;
202         return snprintf(page, count, "%d\n", svc->srv_max_history_rqbds);
203 }
204
205 static int
206 ptlrpc_lprocfs_write_req_history_max(struct file *file, const char *buffer,
207                                      unsigned long count, void *data)
208 {
209         struct ptlrpc_service *svc = data;
210         int                    bufpages;
211         int                    val;
212         int                    rc = lprocfs_write_helper(buffer, count, &val);
213
214         if (rc < 0)
215                 return rc;
216
217         if (val < 0)
218                 return -ERANGE;
219
220         /* This sanity check is more of an insanity check; we can still
221          * hose a kernel by allowing the request history to grow too
222          * far. */
223         bufpages = (svc->srv_buf_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
224         if (val > num_physpages/(2 * bufpages))
225                 return -ERANGE;
226
227         spin_lock(&svc->srv_lock);
228         svc->srv_max_history_rqbds = val;
229         spin_unlock(&svc->srv_lock);
230
231         return count;
232 }
233
234 struct ptlrpc_srh_iterator {
235         __u64                  srhi_seq;
236         struct ptlrpc_request *srhi_req;
237 };
238
239 int
240 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service *svc,
241                                     struct ptlrpc_srh_iterator *srhi,
242                                     __u64 seq)
243 {
244         struct list_head      *e;
245         struct ptlrpc_request *req;
246
247         if (srhi->srhi_req != NULL &&
248             srhi->srhi_seq > svc->srv_request_max_cull_seq &&
249             srhi->srhi_seq <= seq) {
250                 /* If srhi_req was set previously, hasn't been culled and
251                  * we're searching for a seq on or after it (i.e. more
252                  * recent), search from it onwards.
253                  * Since the service history is LRU (i.e. culled reqs will
254                  * be near the head), we shouldn't have to do long
255                  * re-scans */
256                 LASSERT (srhi->srhi_seq == srhi->srhi_req->rq_history_seq);
257                 LASSERT (!list_empty(&svc->srv_request_history));
258                 e = &srhi->srhi_req->rq_history_list;
259         } else {
260                 /* search from start */
261                 e = svc->srv_request_history.next;
262         }
263
264         while (e != &svc->srv_request_history) {
265                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
266
267                 if (req->rq_history_seq >= seq) {
268                         srhi->srhi_seq = req->rq_history_seq;
269                         srhi->srhi_req = req;
270                         return 0;
271                 }
272                 e = e->next;
273         }
274
275         return -ENOENT;
276 }
277
278 static void *
279 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
280 {
281         struct ptlrpc_service       *svc = s->private;
282         struct ptlrpc_srh_iterator  *srhi;
283         int                          rc;
284
285         OBD_ALLOC(srhi, sizeof(*srhi));
286         if (srhi == NULL)
287                 return NULL;
288
289         srhi->srhi_seq = 0;
290         srhi->srhi_req = NULL;
291
292         spin_lock(&svc->srv_lock);
293         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos);
294         spin_unlock(&svc->srv_lock);
295
296         if (rc == 0) {
297                 *pos = srhi->srhi_seq;
298                 return srhi;
299         }
300
301         OBD_FREE(srhi, sizeof(*srhi));
302         return NULL;
303 }
304
305 static void
306 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
307 {
308         struct ptlrpc_srh_iterator *srhi = iter;
309
310         if (srhi != NULL)
311                 OBD_FREE(srhi, sizeof(*srhi));
312 }
313
314 static void *
315 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
316                                     void *iter, loff_t *pos)
317 {
318         struct ptlrpc_service       *svc = s->private;
319         struct ptlrpc_srh_iterator  *srhi = iter;
320         int                          rc;
321
322         spin_lock(&svc->srv_lock);
323         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos + 1);
324         spin_unlock(&svc->srv_lock);
325
326         if (rc != 0) {
327                 OBD_FREE(srhi, sizeof(*srhi));
328                 return NULL;
329         }
330
331         *pos = srhi->srhi_seq;
332         return srhi;
333 }
334
335 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
336 {
337         struct ptlrpc_service      *svc = s->private;
338         struct ptlrpc_srh_iterator *srhi = iter;
339         struct ptlrpc_request      *req;
340         int                         rc;
341
342         spin_lock(&svc->srv_lock);
343
344         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, srhi->srhi_seq);
345
346         if (rc == 0) {
347                 req = srhi->srhi_req;
348
349                 /* Print common req fields.
350                  * CAVEAT EMPTOR: we're racing with the service handler
351                  * here.  The request could contain any old crap, so you
352                  * must be just as careful as the service's request
353                  * parser. Currently I only print stuff here I know is OK
354                  * to look at coz it was set up in request_in_callback()!!! */
355                 seq_printf(s, LPD64":%s:%s:"LPD64":%d:%s ",
356                            req->rq_history_seq, libcfs_nid2str(req->rq_self),
357                            libcfs_id2str(req->rq_peer), req->rq_xid,
358                            req->rq_reqlen,ptlrpc_rqphase2str(req));
359
360                 if (svc->srv_request_history_print_fn == NULL)
361                         seq_printf(s, "\n");
362                 else
363                         svc->srv_request_history_print_fn(s, srhi->srhi_req);
364         }
365
366         spin_unlock(&svc->srv_lock);
367
368         return rc;
369 }
370
371 static int
372 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
373 {
374         static struct seq_operations sops = {
375                 .start = ptlrpc_lprocfs_svc_req_history_start,
376                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
377                 .next  = ptlrpc_lprocfs_svc_req_history_next,
378                 .show  = ptlrpc_lprocfs_svc_req_history_show,
379         };
380         struct proc_dir_entry *dp = PDE(inode);
381         struct seq_file       *seqf;
382         int                    rc;
383
384         LPROCFS_ENTRY_AND_CHECK(dp);
385         rc = seq_open(file, &sops);
386         if (rc) {
387                 LPROCFS_EXIT();
388                 return rc;
389         }
390
391         seqf = file->private_data;
392         seqf->private = dp->data;
393         return 0;
394 }
395
396 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
397                                      struct ptlrpc_service *svc)
398 {
399         struct lprocfs_vars lproc_vars[] = {
400                 {.name       = "req_buffer_history_len",
401                  .write_fptr = NULL,
402                  .read_fptr  = ptlrpc_lprocfs_read_req_history_len,
403                  .data       = svc},
404                 {.name       = "req_buffer_history_max",
405                  .write_fptr = ptlrpc_lprocfs_write_req_history_max,
406                  .read_fptr  = ptlrpc_lprocfs_read_req_history_max,
407                  .data       = svc},
408                 {NULL}
409         };
410         static struct file_operations req_history_fops = {
411                 .owner       = THIS_MODULE,
412                 .open        = ptlrpc_lprocfs_svc_req_history_open,
413                 .read        = seq_read,
414                 .llseek      = seq_lseek,
415                 .release     = lprocfs_seq_release,
416         };
417         struct proc_dir_entry *req_history;
418
419         ptlrpc_lprocfs_register(entry, svc->srv_name,
420                                 "stats", &svc->srv_procroot,
421                                 &svc->srv_stats);
422
423         if (svc->srv_procroot == NULL)
424                 return;
425
426         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
427
428         req_history = create_proc_entry("req_history", 0400,
429                                         svc->srv_procroot);
430         if (req_history != NULL) {
431                 req_history->data = svc;
432                 req_history->proc_fops = &req_history_fops;
433         }
434 }
435
436 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
437 {
438         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
439                                 &obddev->obd_svc_procroot,
440                                 &obddev->obd_svc_stats);
441 }
442 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
443
444 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req)
445 {
446         struct lprocfs_stats *svc_stats;
447         int opc = opcode_offset(lustre_msg_get_opc(req->rq_reqmsg));
448
449         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
450         if (svc_stats == NULL || opc <= 0)
451                 return;
452         LASSERT(opc < LUSTRE_MAX_OPCODES);
453         /* These two use the ptlrpc_lprocfs_brw below */
454         if (!(opc == OST_WRITE || opc == OST_READ))
455                 lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, 0);
456 }
457
458 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes)
459 {
460         struct lprocfs_stats *svc_stats;
461         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
462         if (!svc_stats) 
463                 return;
464         lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, bytes);
465 }
466 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
467
468 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
469 {
470         if (svc->srv_procroot != NULL)
471                 lprocfs_remove(&svc->srv_procroot);
472
473         if (svc->srv_stats)
474                 lprocfs_free_stats(&svc->srv_stats);
475 }
476
477 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
478 {
479         if (obd->obd_svc_procroot)
480                 lprocfs_remove(&obd->obd_svc_procroot);
481
482         if (obd->obd_svc_stats)
483                 lprocfs_free_stats(&obd->obd_svc_stats);
484 }
485 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
486
487
488 int lprocfs_wr_evict_client(struct file *file, const char *buffer,
489                             unsigned long count, void *data)
490 {
491         struct obd_device *obd = data;
492         char tmpbuf[sizeof(struct obd_uuid)];
493
494         /* Kludge code(deadlock situation): the lprocfs lock has been held 
495          * since the client is evicted by writting client's
496          * uuid/nid to procfs "evict_client" entry. However, 
497          * obd_export_evict_by_uuid() will call lprocfs_remove() to destroy
498          * the proc entries under the being destroyed export{}, so I have
499          * to drop the lock at first here. 
500          * - jay, jxiong@clusterfs.com */
501         class_incref(obd);
502         LPROCFS_EXIT();
503  
504         sscanf(buffer, "%40s", tmpbuf);
505         if (strncmp(tmpbuf, "nid:", 4) == 0)
506                 obd_export_evict_by_nid(obd, tmpbuf + 4);
507         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
508                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
509         else
510                 obd_export_evict_by_uuid(obd, tmpbuf);
511
512         LPROCFS_ENTRY();
513         class_decref(obd);
514
515         return count;
516 }
517 EXPORT_SYMBOL(lprocfs_wr_evict_client);
518
519 int lprocfs_wr_ping(struct file *file, const char *buffer,
520                     unsigned long count, void *data)
521 {
522         struct obd_device *obd = data;
523         struct ptlrpc_request *req;
524         int rc;
525         ENTRY;
526
527         LPROCFS_CLIMP_CHECK(obd);
528         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_OBD_VERSION,
529                               OBD_PING, 1, NULL, NULL);
530         LPROCFS_CLIMP_EXIT(obd);
531         if (req == NULL)
532                 RETURN(-ENOMEM);
533
534         ptlrpc_req_set_repsize(req, 1, NULL);
535         req->rq_send_state = LUSTRE_IMP_FULL;
536         req->rq_no_resend = 1;
537
538         rc = ptlrpc_queue_wait(req);
539
540         ptlrpc_req_finished(req);
541         if (rc >= 0)
542                 RETURN(count);
543         RETURN(rc);
544 }
545 EXPORT_SYMBOL(lprocfs_wr_ping);
546
547 #endif /* LPROCFS */