1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Andreas Dilger <adilger@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/version.h>
27 #include <linux/module.h>
29 #include <linux/highmem.h>
31 #include <linux/stat.h>
32 #include <linux/sched.h>
33 #include <linux/smp_lock.h>
34 #include <linux/ext2_fs.h>
35 #include <linux/quotaops.h>
36 #include <linux/proc_fs.h>
37 #include <linux/init.h>
38 #include <asm/unistd.h>
40 #define DEBUG_SUBSYSTEM S_ECHO
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/obd_echo.h>
45 #include <linux/lustre_debug.h>
46 #include <linux/lustre_dlm.h>
47 #include <linux/lprocfs_status.h>
49 #define ECHO_INIT_OBJID 0x1000000000000000ULL
50 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
52 #define ECHO_OBJECT0_NPAGES 16
53 static struct page *echo_object0_pages[ECHO_OBJECT0_NPAGES];
55 /* should be generic per-obd stats... */
56 struct xprocfs_io_stat {
61 __u64 st_getattr_reqs;
62 __u64 st_setattr_reqs;
64 __u64 st_destroy_reqs;
72 static struct xprocfs_io_stat xprocfs_iostats[NR_CPUS];
73 static struct proc_dir_entry *xprocfs_dir;
75 #define XPROCFS_BUMP_MYCPU_IOSTAT(field, count) \
77 xprocfs_iostats[smp_processor_id()].field += (count); \
80 #define DECLARE_XPROCFS_SUM_STAT(field) \
82 xprocfs_sum_##field (void) \
87 for (i = 0; i < smp_num_cpus; i++) \
88 stat += xprocfs_iostats[i].field; \
91 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
92 DECLARE_XPROCFS_SUM_STAT (st_read_bytes)
93 DECLARE_XPROCFS_SUM_STAT (st_read_reqs)
94 DECLARE_XPROCFS_SUM_STAT (st_write_bytes)
95 DECLARE_XPROCFS_SUM_STAT (st_write_reqs)
96 DECLARE_XPROCFS_SUM_STAT (st_getattr_reqs)
97 DECLARE_XPROCFS_SUM_STAT (st_setattr_reqs)
98 DECLARE_XPROCFS_SUM_STAT (st_create_reqs)
99 DECLARE_XPROCFS_SUM_STAT (st_destroy_reqs)
100 DECLARE_XPROCFS_SUM_STAT (st_statfs_reqs)
101 DECLARE_XPROCFS_SUM_STAT (st_sync_reqs)
102 DECLARE_XPROCFS_SUM_STAT (st_open_reqs)
103 DECLARE_XPROCFS_SUM_STAT (st_close_reqs)
104 DECLARE_XPROCFS_SUM_STAT (st_punch_reqs)
108 xprocfs_rd_stat (char *page, char **start, off_t off, int count,
109 int *eof, void *data)
111 long long (*fn)(void) = (long long(*)(void))data;
118 len = snprintf (page, count, "%Ld\n", fn());
125 xprocfs_add_stat(char *name, long long (*fn)(void))
127 struct proc_dir_entry *entry;
129 entry = create_proc_entry (name, S_IFREG|S_IRUGO, xprocfs_dir);
131 CERROR ("Can't add procfs stat %s\n", name);
136 entry->read_proc = xprocfs_rd_stat;
137 entry->write_proc = NULL;
141 xprocfs_init (char *name)
145 snprintf (dirname, sizeof (dirname), "sys/%s", name);
147 xprocfs_dir = proc_mkdir (dirname, NULL);
148 if (xprocfs_dir == NULL) {
149 CERROR ("Can't make dir\n");
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
154 xprocfs_add_stat ("read_bytes", xprocfs_sum_st_read_bytes);
155 xprocfs_add_stat ("read_reqs", xprocfs_sum_st_read_reqs);
156 xprocfs_add_stat ("write_bytes", xprocfs_sum_st_write_bytes);
157 xprocfs_add_stat ("write_reqs", xprocfs_sum_st_write_reqs);
158 xprocfs_add_stat ("getattr_reqs", xprocfs_sum_st_getattr_reqs);
159 xprocfs_add_stat ("setattr_reqs", xprocfs_sum_st_setattr_reqs);
160 xprocfs_add_stat ("create_reqs", xprocfs_sum_st_create_reqs);
161 xprocfs_add_stat ("destroy_reqs", xprocfs_sum_st_destroy_reqs);
162 xprocfs_add_stat ("statfs_reqs", xprocfs_sum_st_statfs_reqs);
163 xprocfs_add_stat ("sync_reqs", xprocfs_sum_st_sync_reqs);
164 xprocfs_add_stat ("open_reqs", xprocfs_sum_st_open_reqs);
165 xprocfs_add_stat ("close_reqs", xprocfs_sum_st_close_reqs);
166 xprocfs_add_stat ("punch_reqs", xprocfs_sum_st_punch_reqs);
170 void xprocfs_fini (void)
172 if (xprocfs_dir == NULL)
175 remove_proc_entry ("read_bytes", xprocfs_dir);
176 remove_proc_entry ("read_reqs", xprocfs_dir);
177 remove_proc_entry ("write_bytes", xprocfs_dir);
178 remove_proc_entry ("write_reqs", xprocfs_dir);
179 remove_proc_entry ("getattr_reqs", xprocfs_dir);
180 remove_proc_entry ("setattr_reqs", xprocfs_dir);
181 remove_proc_entry ("create_reqs", xprocfs_dir);
182 remove_proc_entry ("destroy_reqs", xprocfs_dir);
183 remove_proc_entry ("statfs_reqs", xprocfs_dir);
184 remove_proc_entry ("sync_reqs", xprocfs_dir);
185 remove_proc_entry ("open_reqs", xprocfs_dir);
186 remove_proc_entry ("close_reqs", xprocfs_dir);
187 remove_proc_entry ("punch_reqs", xprocfs_dir);
189 remove_proc_entry (xprocfs_dir->name, xprocfs_dir->parent);
193 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
194 struct obd_uuid *cluuid, struct recovd_obd *recovd,
195 ptlrpc_recovery_cb_t recover)
197 return class_connect(conn, obd, cluuid);
200 static int echo_disconnect(struct lustre_handle *conn)
202 struct obd_export *exp = class_conn2export(conn);
204 LASSERT (exp != NULL);
206 ldlm_cancel_locks_for_export (exp);
207 return (class_disconnect (conn));
210 static __u64 echo_next_id(struct obd_device *obddev)
214 spin_lock(&obddev->u.echo.eo_lock);
215 id = ++obddev->u.echo.eo_lastino;
216 spin_unlock(&obddev->u.echo.eo_lock);
221 int echo_create(struct lustre_handle *conn, struct obdo *oa,
222 struct lov_stripe_md **ea, struct obd_trans_info *oti)
224 struct obd_device *obd = class_conn2obd(conn);
226 XPROCFS_BUMP_MYCPU_IOSTAT (st_create_reqs, 1);
229 CERROR("invalid client "LPX64"\n", conn->addr);
233 if (!(oa->o_mode && S_IFMT)) {
234 CERROR("echo obd: no type!\n");
238 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
239 CERROR("invalid o_valid %08x\n", oa->o_valid);
243 oa->o_id = echo_next_id(obd);
244 oa->o_valid = OBD_MD_FLID;
245 atomic_inc(&obd->u.echo.eo_create);
250 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
251 struct lov_stripe_md *ea, struct obd_trans_info *oti)
253 struct obd_device *obd = class_conn2obd(conn);
255 XPROCFS_BUMP_MYCPU_IOSTAT (st_destroy_reqs, 1);
258 CERROR("invalid client "LPX64"\n", conn->addr);
262 if (!(oa->o_valid & OBD_MD_FLID)) {
263 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
267 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
268 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
272 atomic_inc(&obd->u.echo.eo_destroy);
277 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
278 struct lov_stripe_md *md, struct obd_trans_info *oti)
280 struct lustre_handle *fh = obdo_handle (oa);
281 struct obd_device *obd = class_conn2obd (conn);
283 XPROCFS_BUMP_MYCPU_IOSTAT (st_open_reqs, 1);
286 CERROR ("invalid client "LPX64"\n", conn->addr);
290 if (!(oa->o_valid & OBD_MD_FLID)) {
291 CERROR ("obdo missing FLID valid flag: %08x\n", oa->o_valid);
296 fh->cookie = ECHO_HANDLE_MAGIC;
298 oa->o_valid |= OBD_MD_FLHANDLE;
302 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
303 struct lov_stripe_md *md, struct obd_trans_info *oti)
305 struct lustre_handle *fh = obdo_handle (oa);
306 struct obd_device *obd = class_conn2obd(conn);
308 XPROCFS_BUMP_MYCPU_IOSTAT (st_close_reqs, 1);
311 CERROR("invalid client "LPX64"\n", conn->addr);
315 if (!(oa->o_valid & OBD_MD_FLHANDLE)) {
316 CERROR("obdo missing FLHANDLE valid flag: %08x\n", oa->o_valid);
320 if (fh->cookie != ECHO_HANDLE_MAGIC) {
321 CERROR ("invalid file handle on close: "LPX64"\n", fh->cookie);
328 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
329 struct lov_stripe_md *md)
331 struct obd_device *obd = class_conn2obd(conn);
332 obd_id id = oa->o_id;
334 XPROCFS_BUMP_MYCPU_IOSTAT (st_getattr_reqs, 1);
337 CERROR("invalid client "LPX64"\n", conn->addr);
341 if (!(oa->o_valid & OBD_MD_FLID)) {
342 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
346 obdo_cpy_md(oa, &obd->u.echo.oa, oa->o_valid);
352 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
353 struct lov_stripe_md *md, struct obd_trans_info *oti)
355 struct obd_device *obd = class_conn2obd(conn);
357 XPROCFS_BUMP_MYCPU_IOSTAT (st_setattr_reqs, 1);
360 CERROR("invalid client "LPX64"\n", conn->addr);
364 if (!(oa->o_valid & OBD_MD_FLID)) {
365 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
369 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
371 atomic_inc(&obd->u.echo.eo_setattr);
376 /* This allows us to verify that desc_private is passed unmolested */
377 #define DESC_PRIV 0x10293847
379 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
380 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
381 struct niobuf_local *res, void **desc_private,
382 struct obd_trans_info *oti)
384 struct obd_device *obd;
385 struct niobuf_local *r = res;
390 if ((cmd & OBD_BRW_WRITE) != 0)
391 XPROCFS_BUMP_MYCPU_IOSTAT (st_write_reqs, 1);
393 XPROCFS_BUMP_MYCPU_IOSTAT (st_read_reqs, 1);
395 obd = class_conn2obd(conn);
397 CERROR("invalid client "LPX64"\n", conn->addr);
401 memset(res, 0, sizeof(*res) * niocount);
403 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
404 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
406 *desc_private = (void *)DESC_PRIV;
408 obd_kmap_get(niocount, 1);
410 for (i = 0; i < objcount; i++, obj++) {
411 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
412 int isobj0 = obj->ioo_id == 0;
413 int verify = !isobj0;
416 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
419 (nb->offset >> PAGE_SHIFT) < ECHO_OBJECT0_NPAGES) {
420 r->page = echo_object0_pages[nb->offset >>
422 /* Take extra ref so __free_pages() can be called OK */
425 r->page = alloc_pages(gfp_mask, 0);
426 if (r->page == NULL) {
427 CERROR("can't get page %u/%u for id "
429 j, obj->ioo_bufcnt, obj->ioo_id);
430 GOTO(preprw_cleanup, rc = -ENOMEM);
434 atomic_inc(&obd->u.echo.eo_prep);
436 r->offset = nb->offset;
437 r->addr = kmap(r->page);
440 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
441 r->page, r->addr, r->offset);
443 if (cmd == OBD_BRW_READ) {
444 XPROCFS_BUMP_MYCPU_IOSTAT(st_read_bytes,r->len);
446 page_debug_setup(r->addr, r->len,
447 r->offset,obj->ioo_id);
449 XPROCFS_BUMP_MYCPU_IOSTAT(st_write_bytes,
452 page_debug_setup(r->addr, r->len,
458 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
459 atomic_read(&obd->u.echo.eo_prep));
464 /* It is possible that we would rather handle errors by allow
465 * any already-set-up pages to complete, rather than tearing them
466 * all down again. I believe that this is what the in-kernel
467 * prep/commit operations do.
469 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
472 /* NB if this is an 'object0' page, __free_pages will just
473 * lose the extra ref gained above */
474 __free_pages(r->page, 0);
475 atomic_dec(&obd->u.echo.eo_prep);
477 obd_kmap_put(niocount);
478 memset(res, 0, sizeof(*res) * niocount);
483 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
484 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
485 void *desc_private, struct obd_trans_info *oti)
487 struct obd_device *obd;
488 struct niobuf_local *r = res;
494 obd = class_conn2obd(conn);
496 CERROR("invalid client "LPX64"\n", conn->addr);
500 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
501 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
504 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
508 if (niocount && !r) {
509 CERROR("NULL res niobuf with niocount %d\n", niocount);
513 LASSERT(desc_private == (void *)DESC_PRIV);
515 for (i = 0; i < objcount; i++, obj++) {
516 int verify = obj->ioo_id != 0;
519 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
520 struct page *page = r->page;
523 if (!page || !(addr = page_address(page)) ||
524 !kern_addr_valid(addr)) {
526 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
527 obj->ioo_id, page, j, obj->ioo_bufcnt);
528 GOTO(commitrw_cleanup, rc = -EFAULT);
531 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
532 r->page, addr, r->offset);
535 vrc = page_debug_check("echo", addr, r->len,
536 r->offset, obj->ioo_id);
537 /* check all the pages always */
538 if (vrc != 0 && rc == 0)
543 /* NB see comment above regarding object0 pages */
545 __free_pages(page, 0);
546 atomic_dec(&obd->u.echo.eo_prep);
549 CDEBUG(D_PAGE, "%d pages remain after commit\n",
550 atomic_read(&obd->u.echo.eo_prep));
554 CERROR("cleaning up %ld pages (%d obdos)\n",
555 niocount - (long)(r - res) - 1, objcount);
556 while (++r < res + niocount) {
557 struct page *page = r->page;
561 /* NB see comment above regarding object0 pages */
562 __free_pages(page, 0);
563 atomic_dec(&obd->u.echo.eo_prep);
568 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
572 spin_lock_init(&obddev->u.echo.eo_lock);
573 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
575 obddev->obd_namespace =
576 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
577 if (obddev->obd_namespace == NULL) {
582 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
583 "echo_ldlm_cb_client", &obddev->obd_ldlm_client);
587 static int echo_cleanup(struct obd_device *obddev)
591 ldlm_namespace_free(obddev->obd_namespace);
592 CERROR("%d prep/commitrw pages leaked\n",
593 atomic_read(&obddev->u.echo.eo_prep));
598 int echo_attach(struct obd_device *dev, obd_count len, void *data)
600 struct lprocfs_static_vars lvars;
602 lprocfs_init_vars(&lvars);
603 return lprocfs_obd_attach(dev, lvars.obd_vars);
606 int echo_detach(struct obd_device *dev)
608 return lprocfs_obd_detach(dev);
611 static struct obd_ops echo_obd_ops = {
612 o_owner: THIS_MODULE,
613 o_attach: echo_attach,
614 o_detach: echo_detach,
615 o_connect: echo_connect,
616 o_disconnect: echo_disconnect,
617 o_create: echo_create,
618 o_destroy: echo_destroy,
621 o_getattr: echo_getattr,
622 o_setattr: echo_setattr,
623 o_preprw: echo_preprw,
624 o_commitrw: echo_commitrw,
626 o_cleanup: echo_cleanup
629 extern int echo_client_init(void);
630 extern void echo_client_cleanup(void);
633 echo_object0_pages_fini (void)
637 for (i = 0; i < ECHO_OBJECT0_NPAGES; i++)
638 if (echo_object0_pages[i] != NULL) {
639 __free_pages (echo_object0_pages[i], 0);
640 echo_object0_pages[i] = NULL;
645 echo_object0_pages_init (void)
650 for (i = 0; i < ECHO_OBJECT0_NPAGES; i++) {
651 int gfp_mask = (i < ECHO_OBJECT0_NPAGES/2) ?
652 GFP_KERNEL : GFP_HIGHUSER;
654 pg = alloc_pages (gfp_mask, 0);
656 echo_object0_pages_fini ();
660 memset (kmap (pg), 0, PAGE_SIZE);
663 echo_object0_pages[i] = pg;
669 static int __init obdecho_init(void)
671 struct lprocfs_static_vars lvars;
674 printk(KERN_INFO "Lustre Echo OBD driver; info@clusterfs.com\n");
676 lprocfs_init_vars(&lvars);
678 xprocfs_init ("echo");
680 rc = echo_object0_pages_init ();
684 rc = class_register_type(&echo_obd_ops, lvars.module_vars,
685 OBD_ECHO_DEVICENAME);
689 rc = echo_client_init();
693 class_unregister_type(OBD_ECHO_DEVICENAME);
695 echo_object0_pages_fini ();
702 static void __exit obdecho_exit(void)
704 echo_client_cleanup();
705 class_unregister_type(OBD_ECHO_DEVICENAME);
706 echo_object0_pages_fini ();
710 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
711 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
712 MODULE_LICENSE("GPL");
714 module_init(obdecho_init);
715 module_exit(obdecho_exit);