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;
71 static struct xprocfs_io_stat xprocfs_iostats[NR_CPUS];
72 static struct proc_dir_entry *xprocfs_dir;
74 #define XPROCFS_BUMP_MYCPU_IOSTAT(field, count) \
76 xprocfs_iostats[smp_processor_id()].field += (count); \
79 #define DECLARE_XPROCFS_SUM_STAT(field) \
81 xprocfs_sum_##field (void) \
86 for (i = 0; i < smp_num_cpus; i++) \
87 stat += xprocfs_iostats[i].field; \
91 DECLARE_XPROCFS_SUM_STAT (st_read_bytes)
92 DECLARE_XPROCFS_SUM_STAT (st_read_reqs)
93 DECLARE_XPROCFS_SUM_STAT (st_write_bytes)
94 DECLARE_XPROCFS_SUM_STAT (st_write_reqs)
95 DECLARE_XPROCFS_SUM_STAT (st_getattr_reqs)
96 DECLARE_XPROCFS_SUM_STAT (st_setattr_reqs)
97 DECLARE_XPROCFS_SUM_STAT (st_create_reqs)
98 DECLARE_XPROCFS_SUM_STAT (st_destroy_reqs)
99 DECLARE_XPROCFS_SUM_STAT (st_statfs_reqs)
100 DECLARE_XPROCFS_SUM_STAT (st_open_reqs)
101 DECLARE_XPROCFS_SUM_STAT (st_close_reqs)
102 DECLARE_XPROCFS_SUM_STAT (st_punch_reqs)
105 xprocfs_rd_stat (char *page, char **start, off_t off, int count,
106 int *eof, void *data)
108 long long (*fn)(void) = (long long(*)(void))data;
115 len = snprintf (page, count, "%Ld\n", fn());
122 xprocfs_add_stat(char *name, long long (*fn)(void))
124 struct proc_dir_entry *entry;
126 entry = create_proc_entry (name, S_IFREG|S_IRUGO, xprocfs_dir);
128 CERROR ("Can't add procfs stat %s\n", name);
133 entry->read_proc = xprocfs_rd_stat;
134 entry->write_proc = NULL;
138 xprocfs_init (char *name)
142 snprintf (dirname, sizeof (dirname), "sys/%s", name);
144 xprocfs_dir = proc_mkdir (dirname, NULL);
145 if (xprocfs_dir == NULL) {
146 CERROR ("Can't make dir\n");
150 xprocfs_add_stat ("read_bytes", xprocfs_sum_st_read_bytes);
151 xprocfs_add_stat ("read_reqs", xprocfs_sum_st_read_reqs);
152 xprocfs_add_stat ("write_bytes", xprocfs_sum_st_write_bytes);
153 xprocfs_add_stat ("write_reqs", xprocfs_sum_st_write_reqs);
154 xprocfs_add_stat ("getattr_reqs", xprocfs_sum_st_getattr_reqs);
155 xprocfs_add_stat ("setattr_reqs", xprocfs_sum_st_setattr_reqs);
156 xprocfs_add_stat ("create_reqs", xprocfs_sum_st_create_reqs);
157 xprocfs_add_stat ("destroy_reqs", xprocfs_sum_st_destroy_reqs);
158 xprocfs_add_stat ("statfs_reqs", xprocfs_sum_st_statfs_reqs);
159 xprocfs_add_stat ("open_reqs", xprocfs_sum_st_open_reqs);
160 xprocfs_add_stat ("close_reqs", xprocfs_sum_st_close_reqs);
161 xprocfs_add_stat ("punch_reqs", xprocfs_sum_st_punch_reqs);
164 void xprocfs_fini (void)
166 if (xprocfs_dir == NULL)
169 remove_proc_entry ("read_bytes", xprocfs_dir);
170 remove_proc_entry ("read_reqs", xprocfs_dir);
171 remove_proc_entry ("write_bytes", xprocfs_dir);
172 remove_proc_entry ("write_reqs", xprocfs_dir);
173 remove_proc_entry ("getattr_reqs", xprocfs_dir);
174 remove_proc_entry ("setattr_reqs", xprocfs_dir);
175 remove_proc_entry ("create_reqs", xprocfs_dir);
176 remove_proc_entry ("destroy_reqs", xprocfs_dir);
177 remove_proc_entry ("statfs_reqs", xprocfs_dir);
178 remove_proc_entry ("open_reqs", xprocfs_dir);
179 remove_proc_entry ("close_reqs", xprocfs_dir);
180 remove_proc_entry ("punch_reqs", xprocfs_dir);
182 remove_proc_entry (xprocfs_dir->name, xprocfs_dir->parent);
186 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
187 struct obd_uuid *cluuid, struct recovd_obd *recovd,
188 ptlrpc_recovery_cb_t recover)
190 return class_connect(conn, obd, cluuid);
193 static int echo_disconnect(struct lustre_handle *conn)
195 struct obd_export *exp = class_conn2export(conn);
197 LASSERT (exp != NULL);
199 ldlm_cancel_locks_for_export (exp);
200 return (class_disconnect (conn));
203 static __u64 echo_next_id(struct obd_device *obddev)
207 spin_lock(&obddev->u.echo.eo_lock);
208 id = ++obddev->u.echo.eo_lastino;
209 spin_unlock(&obddev->u.echo.eo_lock);
214 int echo_create(struct lustre_handle *conn, struct obdo *oa,
215 struct lov_stripe_md **ea, struct obd_trans_info *oti)
217 struct obd_device *obd = class_conn2obd(conn);
219 XPROCFS_BUMP_MYCPU_IOSTAT (st_create_reqs, 1);
222 CERROR("invalid client "LPX64"\n", conn->addr);
226 if (!(oa->o_mode && S_IFMT)) {
227 CERROR("echo obd: no type!\n");
231 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
232 CERROR("invalid o_valid %08x\n", oa->o_valid);
236 oa->o_id = echo_next_id(obd);
237 oa->o_valid = OBD_MD_FLID;
238 atomic_inc(&obd->u.echo.eo_create);
243 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
244 struct lov_stripe_md *ea, struct obd_trans_info *oti)
246 struct obd_device *obd = class_conn2obd(conn);
248 XPROCFS_BUMP_MYCPU_IOSTAT (st_destroy_reqs, 1);
251 CERROR("invalid client "LPX64"\n", conn->addr);
255 if (!(oa->o_valid & OBD_MD_FLID)) {
256 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
260 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
261 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
265 atomic_inc(&obd->u.echo.eo_destroy);
270 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
271 struct lov_stripe_md *md, struct obd_trans_info *oti)
273 struct lustre_handle *fh = obdo_handle (oa);
274 struct obd_device *obd = class_conn2obd (conn);
276 XPROCFS_BUMP_MYCPU_IOSTAT (st_open_reqs, 1);
279 CERROR ("invalid client "LPX64"\n", conn->addr);
283 if (!(oa->o_valid & OBD_MD_FLID)) {
284 CERROR ("obdo missing FLID valid flag: %08x\n", oa->o_valid);
289 fh->cookie = ECHO_HANDLE_MAGIC;
291 oa->o_valid |= OBD_MD_FLHANDLE;
295 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
296 struct lov_stripe_md *md, struct obd_trans_info *oti)
298 struct lustre_handle *fh = obdo_handle (oa);
299 struct obd_device *obd = class_conn2obd(conn);
301 XPROCFS_BUMP_MYCPU_IOSTAT (st_close_reqs, 1);
304 CERROR("invalid client "LPX64"\n", conn->addr);
308 if (!(oa->o_valid & OBD_MD_FLHANDLE)) {
309 CERROR("obdo missing FLHANDLE valid flag: %08x\n", oa->o_valid);
313 if (fh->cookie != ECHO_HANDLE_MAGIC) {
314 CERROR ("invalid file handle on close: "LPX64"\n", fh->cookie);
321 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
322 struct lov_stripe_md *md)
324 struct obd_device *obd = class_conn2obd(conn);
325 obd_id id = oa->o_id;
327 XPROCFS_BUMP_MYCPU_IOSTAT (st_getattr_reqs, 1);
330 CERROR("invalid client "LPX64"\n", conn->addr);
334 if (!(oa->o_valid & OBD_MD_FLID)) {
335 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
339 obdo_cpy_md(oa, &obd->u.echo.oa, oa->o_valid);
345 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
346 struct lov_stripe_md *md, struct obd_trans_info *oti)
348 struct obd_device *obd = class_conn2obd(conn);
350 XPROCFS_BUMP_MYCPU_IOSTAT (st_setattr_reqs, 1);
353 CERROR("invalid client "LPX64"\n", conn->addr);
357 if (!(oa->o_valid & OBD_MD_FLID)) {
358 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
362 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
364 atomic_inc(&obd->u.echo.eo_setattr);
369 /* This allows us to verify that desc_private is passed unmolested */
370 #define DESC_PRIV 0x10293847
372 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
373 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
374 struct niobuf_local *res, void **desc_private, struct obd_trans_info *oti)
376 struct obd_device *obd;
377 struct niobuf_local *r = res;
382 if ((cmd & OBD_BRW_WRITE) != 0)
383 XPROCFS_BUMP_MYCPU_IOSTAT (st_write_reqs, 1);
385 XPROCFS_BUMP_MYCPU_IOSTAT (st_read_reqs, 1);
387 obd = class_conn2obd(conn);
389 CERROR("invalid client "LPX64"\n", conn->addr);
393 memset(res, 0, sizeof(*res) * niocount);
395 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
396 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
398 *desc_private = (void *)DESC_PRIV;
400 obd_kmap_get(niocount, 1);
402 for (i = 0; i < objcount; i++, obj++) {
403 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
404 int isobj0 = obj->ioo_id == 0;
405 int verify = !isobj0;
408 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
411 (nb->offset >> PAGE_SHIFT) < ECHO_OBJECT0_NPAGES) {
412 r->page = echo_object0_pages[nb->offset >> PAGE_SHIFT];
413 /* Take extra ref so __free_pages() can be called OK */
416 r->page = alloc_pages(gfp_mask, 0);
417 if (r->page == NULL) {
418 CERROR("can't get page %d/%d for id "LPU64"\n",
419 j, obj->ioo_bufcnt, obj->ioo_id);
420 GOTO(preprw_cleanup, rc = -ENOMEM);
424 atomic_inc(&obd->u.echo.eo_prep);
426 r->offset = nb->offset;
427 r->addr = kmap(r->page);
430 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
431 r->page, r->addr, r->offset);
433 if (cmd == OBD_BRW_READ) {
434 XPROCFS_BUMP_MYCPU_IOSTAT (st_read_bytes, r->len);
436 page_debug_setup(r->addr, r->len, r->offset,
439 XPROCFS_BUMP_MYCPU_IOSTAT (st_write_bytes, r->len);
441 page_debug_setup(r->addr, r->len,
447 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
448 atomic_read(&obd->u.echo.eo_prep));
453 /* It is possible that we would rather handle errors by allow
454 * any already-set-up pages to complete, rather than tearing them
455 * all down again. I believe that this is what the in-kernel
456 * prep/commit operations do.
458 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
461 /* NB if this is an 'object0' page, __free_pages will just
462 * lose the extra ref gained above */
463 __free_pages(r->page, 0);
464 atomic_dec(&obd->u.echo.eo_prep);
466 obd_kmap_put(niocount);
467 memset(res, 0, sizeof(*res) * niocount);
472 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
473 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
474 void *desc_private, struct obd_trans_info *oti)
476 struct obd_device *obd;
477 struct niobuf_local *r = res;
483 obd = class_conn2obd(conn);
485 CERROR("invalid client "LPX64"\n", conn->addr);
489 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
490 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
493 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
497 if (niocount && !r) {
498 CERROR("NULL res niobuf with niocount %d\n", niocount);
502 LASSERT(desc_private == (void *)DESC_PRIV);
504 for (i = 0; i < objcount; i++, obj++) {
505 int verify = obj->ioo_id != 0;
508 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
509 struct page *page = r->page;
512 if (!page || !(addr = page_address(page)) ||
513 !kern_addr_valid(addr)) {
515 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
516 obj->ioo_id, page, j, obj->ioo_bufcnt);
517 GOTO(commitrw_cleanup, rc = -EFAULT);
520 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
521 r->page, addr, r->offset);
524 vrc = page_debug_check("echo", addr, r->len,
525 r->offset, obj->ioo_id);
526 /* check all the pages always */
527 if (vrc != 0 && rc == 0)
532 /* NB see comment above regarding object0 pages */
534 __free_pages(page, 0);
535 atomic_dec(&obd->u.echo.eo_prep);
538 CDEBUG(D_PAGE, "%d pages remain after commit\n",
539 atomic_read(&obd->u.echo.eo_prep));
543 CERROR("cleaning up %ld pages (%d obdos)\n",
544 niocount - (long)(r - res) - 1, objcount);
545 while (++r < res + niocount) {
546 struct page *page = r->page;
550 /* NB see comment above regarding object0 pages */
551 __free_pages(page, 0);
552 atomic_dec(&obd->u.echo.eo_prep);
557 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
561 spin_lock_init(&obddev->u.echo.eo_lock);
562 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
564 obddev->obd_namespace =
565 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
566 if (obddev->obd_namespace == NULL) {
571 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
572 "echo_ldlm_cb_client", &obddev->obd_ldlm_client);
576 static int echo_cleanup(struct obd_device *obddev)
580 ldlm_namespace_free(obddev->obd_namespace);
581 CERROR("%d prep/commitrw pages leaked\n",
582 atomic_read(&obddev->u.echo.eo_prep));
587 int echo_attach(struct obd_device *dev, obd_count len, void *data)
589 struct lprocfs_static_vars lvars;
591 lprocfs_init_vars(&lvars);
592 return lprocfs_obd_attach(dev, lvars.obd_vars);
595 int echo_detach(struct obd_device *dev)
597 return lprocfs_obd_detach(dev);
600 static struct obd_ops echo_obd_ops = {
601 o_owner: THIS_MODULE,
602 o_attach: echo_attach,
603 o_detach: echo_detach,
604 o_connect: echo_connect,
605 o_disconnect: echo_disconnect,
606 o_create: echo_create,
607 o_destroy: echo_destroy,
610 o_getattr: echo_getattr,
611 o_setattr: echo_setattr,
612 o_preprw: echo_preprw,
613 o_commitrw: echo_commitrw,
615 o_cleanup: echo_cleanup
618 extern int echo_client_init(void);
619 extern void echo_client_cleanup(void);
622 echo_object0_pages_fini (void)
626 for (i = 0; i < ECHO_OBJECT0_NPAGES; i++)
627 if (echo_object0_pages[i] != NULL) {
628 __free_pages (echo_object0_pages[i], 0);
629 echo_object0_pages[i] = NULL;
634 echo_object0_pages_init (void)
639 for (i = 0; i < ECHO_OBJECT0_NPAGES; i++) {
640 int gfp_mask = (i < ECHO_OBJECT0_NPAGES/2) ? GFP_KERNEL : GFP_HIGHUSER;
642 pg = alloc_pages (gfp_mask, 0);
644 echo_object0_pages_fini ();
648 memset (kmap (pg), 0, PAGE_SIZE);
651 echo_object0_pages[i] = pg;
657 static int __init obdecho_init(void)
659 struct lprocfs_static_vars lvars;
662 printk(KERN_INFO "Lustre Echo OBD driver; info@clusterfs.com\n");
664 lprocfs_init_vars(&lvars);
666 xprocfs_init ("echo");
668 rc = echo_object0_pages_init ();
672 rc = class_register_type(&echo_obd_ops, lvars.module_vars,
673 OBD_ECHO_DEVICENAME);
677 rc = echo_client_init();
681 class_unregister_type(OBD_ECHO_DEVICENAME);
683 echo_object0_pages_fini ();
690 static void __exit obdecho_exit(void)
692 echo_client_cleanup();
693 class_unregister_type(OBD_ECHO_DEVICENAME);
694 echo_object0_pages_fini ();
698 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
699 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
700 MODULE_LICENSE("GPL");
702 module_init(obdecho_init);
703 module_exit(obdecho_exit);