Whamcloud - gitweb
b=3048
[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 Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #define DEBUG_SUBSYSTEM S_CLASS
23
24 #include <linux/obd_support.h>
25 #include <linux/obd.h>
26 #include <linux/lprocfs_status.h>
27 #include <linux/lustre_idl.h>
28 #include <linux/lustre_net.h>
29 #include <linux/obd_class.h>
30 #include <linux/seq_file.h>
31 #include "ptlrpc_internal.h"
32
33
34 struct ll_rpc_opcode {
35      __u32       opcode;
36      const char *opname;
37 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
38         { OST_REPLY,        "ost_reply" },
39         { OST_GETATTR,      "ost_getattr" },
40         { OST_SETATTR,      "ost_setattr" },
41         { OST_READ,         "ost_read" },
42         { OST_WRITE,        "ost_write" },
43         { OST_CREATE ,      "ost_create" },
44         { OST_DESTROY,      "ost_destroy" },
45         { OST_GET_INFO,     "ost_get_info" },
46         { OST_CONNECT,      "ost_connect" },
47         { OST_DISCONNECT,   "ost_disconnect" },
48         { OST_PUNCH,        "ost_punch" },
49         { OST_OPEN,         "ost_open" },
50         { OST_CLOSE,        "ost_close" },
51         { OST_STATFS,       "ost_statfs" },
52         { OST_SAN_READ,     "ost_san_read" },
53         { OST_SAN_WRITE,    "ost_san_write" },
54         { OST_SYNC,         "ost_sync" },
55         { OST_SET_INFO,     "ost_set_info" },
56         { MDS_GETATTR,      "mds_getattr" },
57         { MDS_GETATTR_NAME, "mds_getattr_name" },
58         { MDS_CLOSE,        "mds_close" },
59         { MDS_REINT,        "mds_reint" },
60         { MDS_READPAGE,     "mds_readpage" },
61         { MDS_CONNECT,      "mds_connect" },
62         { MDS_DISCONNECT,   "mds_disconnect" },
63         { MDS_GETSTATUS,    "mds_getstatus" },
64         { MDS_STATFS,       "mds_statfs" },
65         { MDS_PIN,          "mds_pin" },
66         { MDS_UNPIN,        "mds_unpin" },
67         { MDS_SYNC,         "mds_sync" },
68         { MDS_DONE_WRITING, "mds_done_writing" },
69         { MDS_SET_INFO,     "mds_set_info" },
70         { LDLM_ENQUEUE,     "ldlm_enqueue" },
71         { LDLM_CONVERT,     "ldlm_convert" },
72         { LDLM_CANCEL,      "ldlm_cancel" },
73         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
74         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
75         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
76         { PTLBD_QUERY,      "ptlbd_query" },
77         { PTLBD_READ,       "ptlbd_read" },
78         { PTLBD_WRITE,      "ptlbd_write" },
79         { PTLBD_FLUSH,      "ptlbd_flush" },
80         { PTLBD_CONNECT,    "ptlbd_connect" },
81         { PTLBD_DISCONNECT, "ptlbd_disconnect" },
82         { OBD_PING,         "obd_ping" },
83         { OBD_LOG_CANCEL,   "llog_origin_handle_cancel"},
84 };
85
86 const char* ll_opcode2str(__u32 opcode)
87 {
88         /* When one of the assertions below fail, chances are that:
89          *     1) A new opcode was added in lustre_idl.h, but was
90          *        is missing from the table above.
91          * or  2) The opcode space was renumbered or rearranged,
92          *        and the opcode_offset() function in
93          *        ptlrpc_internals.h needs to be modified.
94          */
95         __u32 offset = opcode_offset(opcode);
96         LASSERT(offset < LUSTRE_MAX_OPCODES);
97         LASSERT(ll_rpc_opcode_table[offset].opcode == opcode);
98         return ll_rpc_opcode_table[offset].opname;
99 }
100
101 #ifndef LPROCFS
102 void ptlrpc_lprocfs_register_service(struct obd_device *obddev,
103                                      struct ptlrpc_service *svc) { return ; }
104 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc) { return; }
105 #else
106
107 void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
108                              char *name, struct proc_dir_entry **procroot_ret,
109                              struct lprocfs_stats **stats_ret)
110 {
111         struct proc_dir_entry *svc_procroot;
112         struct lprocfs_stats *svc_stats;
113         int i, rc;
114         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
115                                           LPROCFS_CNTR_STDDEV;
116
117         LASSERT(*procroot_ret == NULL);
118         LASSERT(*stats_ret == NULL);
119
120         svc_stats = lprocfs_alloc_stats(PTLRPC_LAST_CNTR + LUSTRE_MAX_OPCODES);
121         if (svc_stats == NULL)
122                 return;
123
124         if (dir) {
125                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
126                 if (IS_ERR(svc_procroot)) {
127                         lprocfs_free_stats(svc_stats);
128                         return;
129                 }
130         } else {
131                 svc_procroot = root;
132         }
133
134         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
135                              svc_counter_config, "req_waittime", "usec");
136         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
137                              svc_counter_config, "req_qdepth", "reqs");
138         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
139                              svc_counter_config, "req_active", "reqs");
140         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
141                              svc_counter_config, "reqbuf_avail", "bufs");
142         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
143                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
144                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
145                                      svc_counter_config, ll_opcode2str(opcode),
146                                      "usec");
147         }
148
149         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
150         if (rc < 0) {
151                 if (dir)
152                         lprocfs_remove(svc_procroot);
153                 lprocfs_free_stats(svc_stats);
154         } else {
155                 if (dir)
156                         *procroot_ret = svc_procroot;
157                 *stats_ret = svc_stats;
158         }
159 }
160
161 static int
162 ptlrpc_lprocfs_read_req_history_len(char *page, char **start, off_t off,
163                                     int count, int *eof, void *data)
164 {
165         struct ptlrpc_service *svc = data;
166         
167         *eof = 1;
168         return snprintf(page, count, "%d\n", svc->srv_n_history_rqbds);
169 }
170
171 static int
172 ptlrpc_lprocfs_read_req_history_max(char *page, char **start, off_t off,
173                                     int count, int *eof, void *data)
174 {
175         struct ptlrpc_service *svc = data;
176         
177         *eof = 1;
178         return snprintf(page, count, "%d\n", svc->srv_max_history_rqbds);
179 }
180
181 static int
182 ptlrpc_lprocfs_write_req_history_max(struct file *file, const char *buffer,
183                                      unsigned long count, void *data)
184 {
185         struct ptlrpc_service *svc = data;
186         int                    bufpages;
187         unsigned long          flags;
188         int                    val;
189         int                    rc = lprocfs_write_helper(buffer, count, &val);
190         
191         if (rc < 0)
192                 return rc;
193
194         if (val < 0)
195                 return -ERANGE;
196
197         /* This sanity check is more of an insanity check; we can still
198          * hose a kernel by allowing the request history to grow too
199          * far. */
200         bufpages = (svc->srv_buf_size + PAGE_SIZE - 1)/PAGE_SIZE;
201         if (val > num_physpages/(2*bufpages))
202                 return -ERANGE;
203         
204         spin_lock_irqsave(&svc->srv_lock, flags);
205         svc->srv_max_history_rqbds = val;
206         spin_unlock_irqrestore(&svc->srv_lock, flags);
207  
208         return count;
209 }
210
211 struct ptlrpc_srh_iterator {
212         __u64                  srhi_seq;
213         struct ptlrpc_request *srhi_req;
214 };
215
216 int
217 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service *svc,
218                                     struct ptlrpc_srh_iterator *srhi,
219                                     __u64 seq)
220 {
221         struct list_head      *e;
222         struct ptlrpc_request *req;
223
224         if (srhi->srhi_req != NULL &&
225             srhi->srhi_seq > svc->srv_request_max_cull_seq &&
226             srhi->srhi_seq <= seq) {
227                 /* If srhi_req was set previously, hasn't been culled and
228                  * we're searching for a seq on or after it (i.e. more
229                  * recent), search from it onwards. 
230                  * Since the service history is LRU (i.e. culled reqs will
231                  * be near the head), we shouldn't have to do long
232                  * re-scans */
233                 LASSERT (srhi->srhi_seq == srhi->srhi_req->rq_history_seq);
234                 LASSERT (!list_empty(&svc->srv_request_history));
235                 e = &srhi->srhi_req->rq_history_list;
236         } else {
237                 /* search from start */
238                 e = svc->srv_request_history.next;
239         }
240
241         while (e != &svc->srv_request_history) {
242                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
243                 
244                 if (req->rq_history_seq >= seq) {
245                         srhi->srhi_seq = req->rq_history_seq;
246                         srhi->srhi_req = req;
247                         return 0;
248                 }
249                 e = e->next;
250         }
251         
252         return -ENOENT;
253 }
254
255 static void *
256 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
257 {
258         struct ptlrpc_service       *svc = s->private;
259         struct ptlrpc_srh_iterator  *srhi;
260         unsigned long                flags;
261         int                          rc;
262
263         OBD_ALLOC(srhi, sizeof(*srhi));
264         if (srhi == NULL)
265                 return NULL;
266
267         srhi->srhi_seq = 0;
268         srhi->srhi_req = NULL;
269         
270         spin_lock_irqsave(&svc->srv_lock, flags);
271         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos);
272         spin_unlock_irqrestore(&svc->srv_lock, flags);
273         
274         if (rc == 0) {
275                 *pos = srhi->srhi_seq;
276                 return srhi;
277         }
278         
279         OBD_FREE(srhi, sizeof(*srhi));
280         return NULL;
281 }
282
283 static void
284 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
285 {
286         struct ptlrpc_srh_iterator *srhi = iter;
287
288         if (srhi != NULL)
289                 OBD_FREE(srhi, sizeof(*srhi));
290 }
291
292 static void *
293 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s, 
294                                     void *iter, loff_t *pos)
295 {
296         struct ptlrpc_service       *svc = s->private;
297         struct ptlrpc_srh_iterator  *srhi = iter;
298         unsigned long                flags;
299         int                          rc;
300
301         spin_lock_irqsave(&svc->srv_lock, flags);
302         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos + 1);
303         spin_unlock_irqrestore(&svc->srv_lock, flags);
304
305         if (rc != 0) {
306                 OBD_FREE(srhi, sizeof(*srhi));
307                 return NULL;
308         }
309
310         *pos = srhi->srhi_seq;
311         return srhi;
312 }
313
314 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
315 {
316         struct ptlrpc_service      *svc = s->private;
317         struct ptlrpc_srh_iterator *srhi = iter;
318         struct ptlrpc_request      *req;
319         unsigned long               flags;
320         int                         rc;
321
322         spin_lock_irqsave(&svc->srv_lock, flags);
323
324         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, srhi->srhi_seq);
325
326         if (rc == 0) {
327                 req = srhi->srhi_req;
328
329                 /* Print common req fields.
330                  * CAVEAT EMPTOR: we're racing with the service handler
331                  * here.  The request could contain any old crap, so you
332                  * must be just as careful as the service's request
333                  * parser. Currently I only print stuff here I know is OK
334                  * to look at coz it was set up in request_in_callback()!!! */
335                 seq_printf(s, LPD64":%s:%s:"LPD64":%d:%s ",
336                            req->rq_history_seq,
337                            req->rq_peer.peer_ni->pni_name, req->rq_peerstr,
338                            req->rq_xid, req->rq_reqlen,ptlrpc_rqphase2str(req));
339
340                 if (svc->srv_request_history_print_fn == NULL)
341                         seq_printf(s, "\n");
342                 else
343                         svc->srv_request_history_print_fn(s, srhi->srhi_req);
344         }
345
346         spin_unlock_irqrestore(&svc->srv_lock, flags);
347
348         return rc;
349 }
350
351 static int
352 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
353 {
354         static struct seq_operations sops = {
355                 .start = ptlrpc_lprocfs_svc_req_history_start,
356                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
357                 .next  = ptlrpc_lprocfs_svc_req_history_next,
358                 .show  = ptlrpc_lprocfs_svc_req_history_show,
359         };
360         struct proc_dir_entry *dp = PDE(inode);
361         struct seq_file       *seqf;
362         int                    rc;
363
364         rc = seq_open(file, &sops);
365
366         if (rc == 0) {
367                 seqf = file->private_data;
368                 seqf->private = dp->data;
369         }
370
371         return rc;
372 }
373
374 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
375                                      struct ptlrpc_service *svc)
376 {
377         struct lprocfs_vars lproc_vars[] = {
378                 {.name       = "req_buffer_history_len", 
379                  .write_fptr = NULL,
380                  .read_fptr  = ptlrpc_lprocfs_read_req_history_len, 
381                  .data       = svc},
382                 {.name       = "req_buffer_history_max",
383                  .write_fptr = ptlrpc_lprocfs_write_req_history_max,
384                  .read_fptr  = ptlrpc_lprocfs_read_req_history_max, 
385                  .data       = svc},
386                 {NULL}
387         };
388         static struct file_operations req_history_fops = {
389                 .owner       = THIS_MODULE,
390                 .open        = ptlrpc_lprocfs_svc_req_history_open,
391                 .read        = seq_read,
392                 .llseek      = seq_lseek,
393                 .release     = seq_release,
394         };
395         struct proc_dir_entry *req_history;
396
397         ptlrpc_lprocfs_register(entry, svc->srv_name,
398                                 "stats", &svc->srv_procroot,
399                                 &svc->srv_stats);
400         
401         if (svc->srv_procroot == NULL)
402                 return;
403
404         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
405
406         req_history = create_proc_entry("req_history", 0400, 
407                                         svc->srv_procroot);
408         if (req_history != NULL) {
409                 req_history->data = svc;
410                 req_history->proc_fops = &req_history_fops;
411         }
412 }
413
414 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
415 {
416         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
417                                 &obddev->obd_svc_procroot,
418                                 &obddev->obd_svc_stats);
419 }
420
421 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req)
422 {
423         struct lprocfs_stats *svc_stats;
424         int opc =  opcode_offset(req->rq_reqmsg->opc);
425
426         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
427         if (svc_stats == NULL || opc <= 0)
428                 return;
429         LASSERT(opc < LUSTRE_MAX_OPCODES);
430         lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, 0);
431 }
432
433 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
434 {
435         if (svc->srv_procroot != NULL) {
436                 lprocfs_remove(svc->srv_procroot);
437                 svc->srv_procroot = NULL;
438         }
439         if (svc->srv_stats) {
440                 lprocfs_free_stats(svc->srv_stats);
441                 svc->srv_stats = NULL;
442         }
443 }
444
445 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
446 {
447         if (obd->obd_svc_procroot) {
448                 lprocfs_remove(obd->obd_svc_procroot);
449                 obd->obd_svc_procroot = NULL;
450         }
451         if (obd->obd_svc_stats) {
452                 lprocfs_free_stats(obd->obd_svc_stats);
453                 obd->obd_svc_stats = NULL;
454         }
455 }
456
457 int lprocfs_wr_evict_client(struct file *file, const char *buffer,
458                             unsigned long count, void *data)
459 {
460         struct obd_device *obd = data;
461         struct obd_export *doomed_exp = NULL;
462         struct obd_uuid doomed;
463         struct list_head *p;
464         char tmpbuf[sizeof(doomed)];
465
466         sscanf(buffer, "%40s", tmpbuf);
467         obd_str2uuid(&doomed, tmpbuf);
468
469         spin_lock(&obd->obd_dev_lock);
470         list_for_each(p, &obd->obd_exports) {
471                 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
472                 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
473                         class_export_get(doomed_exp);
474                         break;
475                 }
476                 doomed_exp = NULL;
477         }
478         spin_unlock(&obd->obd_dev_lock);
479
480         if (doomed_exp == NULL) {
481                 CERROR("can't disconnect %s: no export found\n", doomed.uuid);
482         } else {
483                 CERROR("evicting %s at adminstrative request\n", doomed.uuid);
484                 ptlrpc_fail_export(doomed_exp);
485                 class_export_put(doomed_exp);
486         }
487         return count;
488 }
489 EXPORT_SYMBOL(lprocfs_wr_evict_client);
490
491 int lprocfs_wr_ping(struct file *file, const char *buffer,
492                     unsigned long count, void *data)
493 {
494         struct obd_device *obd = data;
495         struct ptlrpc_request *req;
496         int rc;
497         ENTRY;
498
499         req = ptlrpc_prep_req(obd->u.cli.cl_import, OBD_PING, 0, NULL, NULL);
500         if (req == NULL)
501                 RETURN(-ENOMEM);
502
503         req->rq_replen = lustre_msg_size(0, NULL);
504         req->rq_send_state = LUSTRE_IMP_FULL;
505         req->rq_no_resend = 1;
506
507         rc = ptlrpc_queue_wait(req);
508
509         ptlrpc_req_finished(req);
510         if (rc >= 0)
511                 RETURN(count);
512         RETURN(rc);
513 }
514 EXPORT_SYMBOL(lprocfs_wr_ping);
515
516 #endif /* LPROCFS */