Whamcloud - gitweb
Allow llrd to evict clients directly on OSTs.
[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         { OBD_PING,         "obd_ping" },
96         { OBD_LOG_CANCEL,   "llog_origin_handle_cancel" },
97         { OBD_QC_CALLBACK,  "obd_qc_callback" },
98         { LLOG_ORIGIN_HANDLE_CREATE, "llog_origin_handle_create" },
99         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block" },
100         { LLOG_ORIGIN_HANDLE_READ_HEADER, "llog_origin_handle_read_header" },
101         { LLOG_ORIGIN_HANDLE_WRITE_REC, "llog_origin_handle_write_rec" },
102         { LLOG_ORIGIN_HANDLE_CLOSE, "llog_origin_handle_close" },
103         { LLOG_ORIGIN_CONNECT, "llog_origin_connect" },
104         { LLOG_CATINFO,     "llog_catinfo" },
105         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
106         { LLOG_ORIGIN_HANDLE_DESTROY, "llog_origin_handle_destroy" },
107         { FLD_QUERY,        "fld_query" },
108         { SEQ_QUERY,        "seq_query" },
109         { SEC_CTX_INIT,     "sec_ctx_init" },
110         { SEC_CTX_INIT_CONT,"sec_ctx_init_cont" },
111         { SEC_CTX_FINI,     "sec_ctx_fini" }
112 };
113
114 const char* ll_opcode2str(__u32 opcode)
115 {
116         /* When one of the assertions below fail, chances are that:
117          *     1) A new opcode was added in lustre_idl.h, but was
118          *        is missing from the table above.
119          * or  2) The opcode space was renumbered or rearranged,
120          *        and the opcode_offset() function in
121          *        ptlrpc_internal.h needs to be modified.
122          */
123         __u32 offset = opcode_offset(opcode);
124         LASSERT(offset < LUSTRE_MAX_OPCODES);
125         LASSERT(ll_rpc_opcode_table[offset].opcode == opcode);
126         return ll_rpc_opcode_table[offset].opname;
127 }
128
129 #ifdef LPROCFS
130 void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
131                              char *name, struct proc_dir_entry **procroot_ret,
132                              struct lprocfs_stats **stats_ret)
133 {
134         struct proc_dir_entry *svc_procroot;
135         struct lprocfs_stats *svc_stats;
136         int i, rc;
137         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
138                                           LPROCFS_CNTR_STDDEV;
139
140         LASSERT(*procroot_ret == NULL);
141         LASSERT(*stats_ret == NULL);
142
143         svc_stats = lprocfs_alloc_stats(PTLRPC_LAST_CNTR + LUSTRE_MAX_OPCODES);
144         if (svc_stats == NULL)
145                 return;
146
147         if (dir) {
148                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
149                 if (IS_ERR(svc_procroot)) {
150                         lprocfs_free_stats(&svc_stats);
151                         return;
152                 }
153         } else {
154                 svc_procroot = root;
155         }
156
157         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
158                              svc_counter_config, "req_waittime", "usec");
159         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
160                              svc_counter_config, "req_qdepth", "reqs");
161         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
162                              svc_counter_config, "req_active", "reqs");
163         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
164                              svc_counter_config, "reqbuf_avail", "bufs");
165         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
166                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
167                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
168                                      svc_counter_config, ll_opcode2str(opcode),
169                                      "usec");
170         }
171
172         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
173         if (rc < 0) {
174                 if (dir)
175                         lprocfs_remove(&svc_procroot);
176                 lprocfs_free_stats(&svc_stats);
177         } else {
178                 if (dir)
179                         *procroot_ret = svc_procroot;
180                 *stats_ret = svc_stats;
181         }
182 }
183
184 static int
185 ptlrpc_lprocfs_read_req_history_len(char *page, char **start, off_t off,
186                                     int count, int *eof, void *data)
187 {
188         struct ptlrpc_service *svc = data;
189
190         *eof = 1;
191         return snprintf(page, count, "%d\n", svc->srv_n_history_rqbds);
192 }
193
194 static int
195 ptlrpc_lprocfs_read_req_history_max(char *page, char **start, off_t off,
196                                     int count, int *eof, void *data)
197 {
198         struct ptlrpc_service *svc = data;
199
200         *eof = 1;
201         return snprintf(page, count, "%d\n", svc->srv_max_history_rqbds);
202 }
203
204 static int
205 ptlrpc_lprocfs_write_req_history_max(struct file *file, const char *buffer,
206                                      unsigned long count, void *data)
207 {
208         struct ptlrpc_service *svc = data;
209         int                    bufpages;
210         int                    val;
211         int                    rc = lprocfs_write_helper(buffer, count, &val);
212
213         if (rc < 0)
214                 return rc;
215
216         if (val < 0)
217                 return -ERANGE;
218
219         /* This sanity check is more of an insanity check; we can still
220          * hose a kernel by allowing the request history to grow too
221          * far. */
222         bufpages = (svc->srv_buf_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
223         if (val > num_physpages/(2 * bufpages))
224                 return -ERANGE;
225
226         spin_lock(&svc->srv_lock);
227         svc->srv_max_history_rqbds = val;
228         spin_unlock(&svc->srv_lock);
229
230         return count;
231 }
232
233 struct ptlrpc_srh_iterator {
234         __u64                  srhi_seq;
235         struct ptlrpc_request *srhi_req;
236 };
237
238 int
239 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service *svc,
240                                     struct ptlrpc_srh_iterator *srhi,
241                                     __u64 seq)
242 {
243         struct list_head      *e;
244         struct ptlrpc_request *req;
245
246         if (srhi->srhi_req != NULL &&
247             srhi->srhi_seq > svc->srv_request_max_cull_seq &&
248             srhi->srhi_seq <= seq) {
249                 /* If srhi_req was set previously, hasn't been culled and
250                  * we're searching for a seq on or after it (i.e. more
251                  * recent), search from it onwards.
252                  * Since the service history is LRU (i.e. culled reqs will
253                  * be near the head), we shouldn't have to do long
254                  * re-scans */
255                 LASSERT (srhi->srhi_seq == srhi->srhi_req->rq_history_seq);
256                 LASSERT (!list_empty(&svc->srv_request_history));
257                 e = &srhi->srhi_req->rq_history_list;
258         } else {
259                 /* search from start */
260                 e = svc->srv_request_history.next;
261         }
262
263         while (e != &svc->srv_request_history) {
264                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
265
266                 if (req->rq_history_seq >= seq) {
267                         srhi->srhi_seq = req->rq_history_seq;
268                         srhi->srhi_req = req;
269                         return 0;
270                 }
271                 e = e->next;
272         }
273
274         return -ENOENT;
275 }
276
277 static void *
278 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
279 {
280         struct ptlrpc_service       *svc = s->private;
281         struct ptlrpc_srh_iterator  *srhi;
282         int                          rc;
283
284         OBD_ALLOC(srhi, sizeof(*srhi));
285         if (srhi == NULL)
286                 return NULL;
287
288         srhi->srhi_seq = 0;
289         srhi->srhi_req = NULL;
290
291         spin_lock(&svc->srv_lock);
292         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos);
293         spin_unlock(&svc->srv_lock);
294
295         if (rc == 0) {
296                 *pos = srhi->srhi_seq;
297                 return srhi;
298         }
299
300         OBD_FREE(srhi, sizeof(*srhi));
301         return NULL;
302 }
303
304 static void
305 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
306 {
307         struct ptlrpc_srh_iterator *srhi = iter;
308
309         if (srhi != NULL)
310                 OBD_FREE(srhi, sizeof(*srhi));
311 }
312
313 static void *
314 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
315                                     void *iter, loff_t *pos)
316 {
317         struct ptlrpc_service       *svc = s->private;
318         struct ptlrpc_srh_iterator  *srhi = iter;
319         int                          rc;
320
321         spin_lock(&svc->srv_lock);
322         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos + 1);
323         spin_unlock(&svc->srv_lock);
324
325         if (rc != 0) {
326                 OBD_FREE(srhi, sizeof(*srhi));
327                 return NULL;
328         }
329
330         *pos = srhi->srhi_seq;
331         return srhi;
332 }
333
334 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
335 {
336         struct ptlrpc_service      *svc = s->private;
337         struct ptlrpc_srh_iterator *srhi = iter;
338         struct ptlrpc_request      *req;
339         int                         rc;
340
341         spin_lock(&svc->srv_lock);
342
343         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, srhi->srhi_seq);
344
345         if (rc == 0) {
346                 req = srhi->srhi_req;
347
348                 /* Print common req fields.
349                  * CAVEAT EMPTOR: we're racing with the service handler
350                  * here.  The request could contain any old crap, so you
351                  * must be just as careful as the service's request
352                  * parser. Currently I only print stuff here I know is OK
353                  * to look at coz it was set up in request_in_callback()!!! */
354                 seq_printf(s, LPD64":%s:%s:"LPD64":%d:%s ",
355                            req->rq_history_seq, libcfs_nid2str(req->rq_self),
356                            libcfs_id2str(req->rq_peer), req->rq_xid,
357                            req->rq_reqlen,ptlrpc_rqphase2str(req));
358
359                 if (svc->srv_request_history_print_fn == NULL)
360                         seq_printf(s, "\n");
361                 else
362                         svc->srv_request_history_print_fn(s, srhi->srhi_req);
363         }
364
365         spin_unlock(&svc->srv_lock);
366
367         return rc;
368 }
369
370 static int
371 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
372 {
373         static struct seq_operations sops = {
374                 .start = ptlrpc_lprocfs_svc_req_history_start,
375                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
376                 .next  = ptlrpc_lprocfs_svc_req_history_next,
377                 .show  = ptlrpc_lprocfs_svc_req_history_show,
378         };
379         struct proc_dir_entry *dp = PDE(inode);
380         struct seq_file       *seqf;
381         int                    rc;
382
383         LPROCFS_ENTRY_AND_CHECK(dp);
384         rc = seq_open(file, &sops);
385         if (rc) {
386                 LPROCFS_EXIT();
387                 return rc;
388         }
389
390         seqf = file->private_data;
391         seqf->private = dp->data;
392         return 0;
393 }
394
395 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
396                                      struct ptlrpc_service *svc)
397 {
398         struct lprocfs_vars lproc_vars[] = {
399                 {.name       = "req_buffer_history_len",
400                  .write_fptr = NULL,
401                  .read_fptr  = ptlrpc_lprocfs_read_req_history_len,
402                  .data       = svc},
403                 {.name       = "req_buffer_history_max",
404                  .write_fptr = ptlrpc_lprocfs_write_req_history_max,
405                  .read_fptr  = ptlrpc_lprocfs_read_req_history_max,
406                  .data       = svc},
407                 {NULL}
408         };
409         static struct file_operations req_history_fops = {
410                 .owner       = THIS_MODULE,
411                 .open        = ptlrpc_lprocfs_svc_req_history_open,
412                 .read        = seq_read,
413                 .llseek      = seq_lseek,
414                 .release     = lprocfs_seq_release,
415         };
416         struct proc_dir_entry *req_history;
417
418         ptlrpc_lprocfs_register(entry, svc->srv_name,
419                                 "stats", &svc->srv_procroot,
420                                 &svc->srv_stats);
421
422         if (svc->srv_procroot == NULL)
423                 return;
424
425         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
426
427         req_history = create_proc_entry("req_history", 0400,
428                                         svc->srv_procroot);
429         if (req_history != NULL) {
430                 req_history->data = svc;
431                 req_history->proc_fops = &req_history_fops;
432         }
433 }
434
435 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
436 {
437         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
438                                 &obddev->obd_svc_procroot,
439                                 &obddev->obd_svc_stats);
440 }
441 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
442
443 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req)
444 {
445         struct lprocfs_stats *svc_stats;
446         int opc = opcode_offset(lustre_msg_get_opc(req->rq_reqmsg));
447
448         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
449         if (svc_stats == NULL || opc <= 0)
450                 return;
451         LASSERT(opc < LUSTRE_MAX_OPCODES);
452         /* These two use the ptlrpc_lprocfs_brw below */
453         if (!(opc == OST_WRITE || opc == OST_READ))
454                 lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, 0);
455 }
456
457 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes)
458 {
459         struct lprocfs_stats *svc_stats;
460         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
461         if (!svc_stats) 
462                 return;
463         lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, bytes);
464 }
465 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
466
467 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
468 {
469         if (svc->srv_procroot != NULL)
470                 lprocfs_remove(&svc->srv_procroot);
471
472         if (svc->srv_stats)
473                 lprocfs_free_stats(&svc->srv_stats);
474 }
475
476 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
477 {
478         if (obd->obd_svc_procroot)
479                 lprocfs_remove(&obd->obd_svc_procroot);
480
481         if (obd->obd_svc_stats)
482                 lprocfs_free_stats(&obd->obd_svc_stats);
483 }
484 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
485
486
487 int lprocfs_wr_evict_client(struct file *file, const char *buffer,
488                             unsigned long count, void *data)
489 {
490         struct obd_device *obd = data;
491         char tmpbuf[sizeof(struct obd_uuid)];
492
493         /* Kludge code(deadlock situation): the lprocfs lock has been held 
494          * since the client is evicted by writting client's
495          * uuid/nid to procfs "evict_client" entry. However, 
496          * obd_export_evict_by_uuid() will call lprocfs_remove() to destroy
497          * the proc entries under the being destroyed export{}, so I have
498          * to drop the lock at first here. 
499          * - jay, jxiong@clusterfs.com */
500         class_incref(obd);
501         LPROCFS_EXIT();
502  
503         sscanf(buffer, "%40s", tmpbuf);
504         if (strncmp(tmpbuf, "nid:", 4) == 0)
505                 obd_export_evict_by_nid(obd, tmpbuf + 4);
506         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
507                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
508         else
509                 obd_export_evict_by_uuid(obd, tmpbuf);
510
511         LPROCFS_ENTRY();
512         class_decref(obd);
513
514         return count;
515 }
516 EXPORT_SYMBOL(lprocfs_wr_evict_client);
517
518 int lprocfs_wr_ping(struct file *file, const char *buffer,
519                     unsigned long count, void *data)
520 {
521         struct obd_device *obd = data;
522         struct ptlrpc_request *req;
523         int rc;
524         ENTRY;
525
526         LPROCFS_CLIMP_CHECK(obd);
527         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_OBD_VERSION,
528                               OBD_PING, 1, NULL, NULL);
529         LPROCFS_CLIMP_EXIT(obd);
530         if (req == NULL)
531                 RETURN(-ENOMEM);
532
533         ptlrpc_req_set_repsize(req, 1, NULL);
534         req->rq_send_state = LUSTRE_IMP_FULL;
535         req->rq_no_resend = 1;
536
537         rc = ptlrpc_queue_wait(req);
538
539         ptlrpc_req_finished(req);
540         if (rc >= 0)
541                 RETURN(count);
542         RETURN(rc);
543 }
544 EXPORT_SYMBOL(lprocfs_wr_ping);
545
546 #endif /* LPROCFS */