1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdecho/echo.c
6 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
8 * This code is issued under the GNU General Public License.
9 * See the file COPYING in this distribution
11 * by Peter Braam <braam@clusterfs.com>
12 * and Andreas Dilger <adilger@clusterfs.com>
15 static char rcsid[] __attribute ((unused)) = "$Id: echo.c,v 1.30 2002/09/03 20:19:20 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.30 $"
20 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/quotaops.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <asm/unistd.h>
31 #define DEBUG_SUBSYSTEM S_ECHO
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_echo.h>
36 #include <linux/lustre_debug.h>
37 #include <linux/lustre_dlm.h>
39 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
41 #define ECHO_PROC_STAT "sys/obdecho"
43 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
46 struct obd_device *obd;
56 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++) {
57 if (strcmp(obd->obd_type->typ_name, OBD_ECHO_DEVICENAME))
59 attrs += atomic_read(&obd->u.echo.eo_getattr) +
60 atomic_read(&obd->u.echo.eo_setattr);
61 pages += atomic_read(&obd->u.echo.eo_read) +
62 atomic_read(&obd->u.echo.eo_write);
65 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
71 int echo_proc_write(struct file *file, const char *ubuffer,
72 unsigned long count, void *data)
74 struct obd_device *obd;
77 for (i = 0, obd = obd_dev; i < MAX_OBD_DEVICES; i++, obd++) {
78 if (strcmp(obd->obd_type->typ_name, OBD_ECHO_DEVICENAME))
81 atomic_set(&obd->u.echo.eo_getattr, 0);
82 atomic_set(&obd->u.echo.eo_setattr, 0);
83 atomic_set(&obd->u.echo.eo_read, 0);
84 atomic_set(&obd->u.echo.eo_write, 0);
87 /* Ignore what we've been asked to write, and just zero the counters */
92 void echo_proc_init(void)
94 struct proc_dir_entry *entry;
96 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
99 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
104 entry->read_proc = echo_proc_read;
105 entry->write_proc = echo_proc_write;
108 void echo_proc_fini(void)
110 remove_proc_entry(ECHO_PROC_STAT, 0);
113 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
119 rc = class_connect(conn, obd, cluuid);
127 static int echo_disconnect(struct lustre_handle *conn)
131 rc = class_disconnect(conn);
138 static __u64 echo_next_id(struct obd_device *obddev)
142 spin_lock(&obddev->u.echo.eo_lock);
143 id = ++obddev->u.echo.eo_lastino;
144 spin_unlock(&obddev->u.echo.eo_lock);
149 int echo_create(struct lustre_handle *conn, struct obdo *oa,
150 struct lov_stripe_md **ea)
152 struct obd_device *obd = class_conn2obd(conn);
155 CERROR("invalid client %Lx\n", conn->addr);
159 if (!(oa->o_mode && S_IFMT)) {
160 CERROR("filter obd: no type!\n");
164 if (!(oa->o_valid & OBD_MD_FLMODE)) {
165 CERROR("invalid o_valid %08x\n", oa->o_valid);
169 oa->o_id = echo_next_id(obd);
170 oa->o_valid = OBD_MD_FLID;
171 atomic_inc(&obd->u.echo.eo_create);
176 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
177 struct lov_stripe_md *ea)
179 struct obd_device *obd = class_conn2obd(conn);
182 CERROR("invalid client "LPX64"\n", conn->addr);
186 if (!(oa->o_valid & OBD_MD_FLID)) {
187 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
191 if (oa->o_id > obd->u.echo.eo_lastino) {
192 CERROR("bad destroy objid: %Ld\n", (long long)oa->o_id);
196 atomic_inc(&obd->u.echo.eo_destroy);
201 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
202 struct lov_stripe_md *md)
204 struct obd_device *obd = class_conn2obd(conn);
205 obd_id id = oa->o_id;
208 CERROR("invalid client "LPX64"\n", conn->addr);
212 if (!(oa->o_valid & OBD_MD_FLID)) {
213 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
217 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
219 oa->o_valid |= OBD_MD_FLID;
221 atomic_inc(&obd->u.echo.eo_getattr);
226 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
227 struct lov_stripe_md *md)
229 struct obd_device *obd = class_conn2obd(conn);
232 CERROR("invalid client "LPX64"\n", conn->addr);
236 if (!(oa->o_valid & OBD_MD_FLID)) {
237 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
241 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
243 atomic_inc(&obd->u.echo.eo_setattr);
248 /* This allows us to verify that desc_private is passed unmolested */
249 #define DESC_PRIV 0x10293847
251 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
252 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
253 struct niobuf_local *res, void **desc_private)
255 struct obd_device *obd;
256 struct niobuf_local *r = res;
262 obd = class_conn2obd(conn);
264 CERROR("invalid client "LPX64"\n", conn->addr);
268 memset(res, 0, sizeof(*res) * niocount);
270 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
271 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
273 *desc_private = (void *)DESC_PRIV;
275 for (i = 0; i < objcount; i++, obj++) {
276 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
277 int verify = obj->ioo_id != 0;
280 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
281 r->page = alloc_pages(gfp_mask, 0);
283 CERROR("can't get page %d/%d for id "LPU64"\n",
284 j, obj->ioo_bufcnt, obj->ioo_id);
285 GOTO(preprw_cleanup, rc = -ENOMEM);
287 atomic_inc(&obd->u.echo.eo_prep);
289 r->offset = nb->offset;
290 r->addr = kmap(r->page);
293 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
294 r->page, r->addr, r->offset);
296 if (verify && cmd == OBD_BRW_READ)
297 page_debug_setup(r->addr, r->len, r->offset,
300 page_debug_setup(r->addr, r->len,
305 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
306 atomic_read(&obd->u.echo.eo_prep));
311 /* It is possible that we would rather handle errors by allow
312 * any already-set-up pages to complete, rather than tearing them
313 * all down again. I believe that this is what the in-kernel
314 * prep/commit operations do.
316 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
319 __free_pages(r->page, 0);
320 atomic_dec(&obd->u.echo.eo_prep);
322 memset(res, 0, sizeof(*res) * niocount);
327 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
328 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
331 struct obd_device *obd;
332 struct niobuf_local *r = res;
337 obd = class_conn2obd(conn);
339 CERROR("invalid client "LPX64"\n", conn->addr);
343 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
344 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
346 atomic_inc(&obd->u.echo.eo_read);
348 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
350 atomic_inc(&obd->u.echo.eo_write);
353 if (niocount && !r) {
354 CERROR("NULL res niobuf with niocount %d\n", niocount);
358 LASSERT(desc_private == (void *)DESC_PRIV);
360 for (i = 0; i < objcount; i++, obj++) {
361 int verify = obj->ioo_id != 0;
364 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
365 struct page *page = r->page;
368 if (!page || !(addr = page_address(page)) ||
369 !kern_addr_valid(addr)) {
371 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
372 obj->ioo_id, page, j, obj->ioo_bufcnt);
373 GOTO(commitrw_cleanup, rc = -EFAULT);
376 atomic_inc(&obd->u.echo.eo_read);
378 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
379 r->page, addr, r->offset);
382 page_debug_check("echo", addr, r->len,
383 r->offset, obj->ioo_id);
386 __free_pages(page, 0);
387 atomic_dec(&obd->u.echo.eo_prep);
390 CDEBUG(D_PAGE, "%d pages remain after commit\n",
391 atomic_read(&obd->u.echo.eo_prep));
395 CERROR("cleaning up %ld pages (%d obdos)\n",
396 niocount - (long)(r - res) - 1, objcount);
397 while (++r < res + niocount) {
398 struct page *page = r->page;
401 __free_pages(page, 0);
402 atomic_dec(&obd->u.echo.eo_prep);
407 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
411 obddev->obd_namespace =
412 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
413 if (obddev->obd_namespace == NULL) {
421 static int echo_cleanup(struct obd_device *obddev)
425 ldlm_namespace_free(obddev->obd_namespace);
426 printk(KERN_INFO "%s: %u getattrs, %u setattrs\n", OBD_ECHO_DEVICENAME,
427 atomic_read(&obddev->u.echo.eo_getattr),
428 atomic_read(&obddev->u.echo.eo_setattr));
429 printk(KERN_INFO "%s: %u reads, %u writes (%d leaked)\n",
431 atomic_read(&obddev->u.echo.eo_read),
432 atomic_read(&obddev->u.echo.eo_write),
433 atomic_read(&obddev->u.echo.eo_prep));
434 printk(KERN_INFO "%s: %u creates, %u destroys\n", OBD_ECHO_DEVICENAME,
435 atomic_read(&obddev->u.echo.eo_create),
436 atomic_read(&obddev->u.echo.eo_destroy));
441 struct obd_ops echo_obd_ops = {
442 o_connect: echo_connect,
443 o_disconnect: echo_disconnect,
444 o_create: echo_create,
445 o_destroy: echo_destroy,
446 o_getattr: echo_getattr,
447 o_setattr: echo_setattr,
448 o_preprw: echo_preprw,
449 o_commitrw: echo_commitrw,
451 o_cleanup: echo_cleanup
454 static int __init obdecho_init(void)
456 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
457 " info@clusterfs.com\n");
461 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
464 static void __exit obdecho_exit(void)
468 class_unregister_type(OBD_ECHO_DEVICENAME);
471 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
472 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
473 MODULE_LICENSE("GPL");
475 module_init(obdecho_init);
476 module_exit(obdecho_exit);