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.32 2002/09/04 16:30:40 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.32 $"
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 static atomic_t echo_page_rws;
42 static atomic_t echo_getattrs;
44 #define ECHO_PROC_STAT "sys/obdecho"
46 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
49 long long attrs = atomic_read(&echo_getattrs);
50 long long pages = atomic_read(&echo_page_rws);
57 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
63 int echo_proc_write(struct file *file, const char *ubuffer,
64 unsigned long count, void *data)
66 /* Ignore what we've been asked to write, and just zero the counters */
67 atomic_set (&echo_page_rws, 0);
68 atomic_set (&echo_getattrs, 0);
73 void echo_proc_init(void)
75 struct proc_dir_entry *entry;
77 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
80 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
85 entry->read_proc = echo_proc_read;
86 entry->write_proc = echo_proc_write;
89 void echo_proc_fini(void)
91 remove_proc_entry(ECHO_PROC_STAT, 0);
94 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
100 rc = class_connect(conn, obd, cluuid);
108 static int echo_disconnect(struct lustre_handle *conn)
112 rc = class_disconnect(conn);
119 static __u64 echo_next_id(struct obd_device *obddev)
123 spin_lock(&obddev->u.echo.eo_lock);
124 id = ++obddev->u.echo.eo_lastino;
125 spin_unlock(&obddev->u.echo.eo_lock);
130 int echo_create(struct lustre_handle *conn, struct obdo *oa,
131 struct lov_stripe_md **ea)
133 struct obd_device *obd = class_conn2obd(conn);
136 CERROR("invalid client %Lx\n", conn->addr);
140 if (!(oa->o_mode && S_IFMT)) {
141 CERROR("filter obd: no type!\n");
145 if (!(oa->o_valid & OBD_MD_FLMODE)) {
146 CERROR("invalid o_valid %08x\n", oa->o_valid);
150 oa->o_id = echo_next_id(obd);
151 oa->o_valid = OBD_MD_FLID;
152 atomic_inc(&obd->u.echo.eo_create);
157 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
158 struct lov_stripe_md *ea)
160 struct obd_device *obd = class_conn2obd(conn);
163 CERROR("invalid client "LPX64"\n", conn->addr);
167 if (!(oa->o_valid & OBD_MD_FLID)) {
168 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
172 if (oa->o_id > obd->u.echo.eo_lastino) {
173 CERROR("bad destroy objid: %Ld\n", (long long)oa->o_id);
177 atomic_inc(&obd->u.echo.eo_destroy);
182 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
183 struct lov_stripe_md *md)
185 struct obd_device *obd = class_conn2obd(conn);
186 obd_id id = oa->o_id;
189 CERROR("invalid client "LPX64"\n", conn->addr);
193 if (!(oa->o_valid & OBD_MD_FLID)) {
194 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
198 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
200 oa->o_valid |= OBD_MD_FLID;
202 atomic_inc(&echo_getattrs);
207 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
208 struct lov_stripe_md *md)
210 struct obd_device *obd = class_conn2obd(conn);
213 CERROR("invalid client "LPX64"\n", conn->addr);
217 if (!(oa->o_valid & OBD_MD_FLID)) {
218 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
222 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
224 atomic_inc(&obd->u.echo.eo_setattr);
229 /* This allows us to verify that desc_private is passed unmolested */
230 #define DESC_PRIV 0x10293847
232 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
233 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
234 struct niobuf_local *res, void **desc_private)
236 struct obd_device *obd;
237 struct niobuf_local *r = res;
243 obd = class_conn2obd(conn);
245 CERROR("invalid client "LPX64"\n", conn->addr);
249 memset(res, 0, sizeof(*res) * niocount);
251 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
252 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
254 *desc_private = (void *)DESC_PRIV;
256 for (i = 0; i < objcount; i++, obj++) {
257 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
258 int verify = obj->ioo_id != 0;
261 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
262 r->page = alloc_pages(gfp_mask, 0);
264 CERROR("can't get page %d/%d for id "LPU64"\n",
265 j, obj->ioo_bufcnt, obj->ioo_id);
266 GOTO(preprw_cleanup, rc = -ENOMEM);
268 atomic_inc(&obd->u.echo.eo_prep);
270 r->offset = nb->offset;
271 r->addr = kmap(r->page);
274 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
275 r->page, r->addr, r->offset);
277 if (verify && cmd == OBD_BRW_READ)
278 page_debug_setup(r->addr, r->len, r->offset,
281 page_debug_setup(r->addr, r->len,
286 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
287 atomic_read(&obd->u.echo.eo_prep));
292 /* It is possible that we would rather handle errors by allow
293 * any already-set-up pages to complete, rather than tearing them
294 * all down again. I believe that this is what the in-kernel
295 * prep/commit operations do.
297 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
300 __free_pages(r->page, 0);
301 atomic_dec(&obd->u.echo.eo_prep);
303 memset(res, 0, sizeof(*res) * niocount);
308 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
309 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
312 struct obd_device *obd;
313 struct niobuf_local *r = res;
318 obd = class_conn2obd(conn);
320 CERROR("invalid client "LPX64"\n", conn->addr);
324 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
325 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
328 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
332 if (niocount && !r) {
333 CERROR("NULL res niobuf with niocount %d\n", niocount);
337 LASSERT(desc_private == (void *)DESC_PRIV);
339 for (i = 0; i < objcount; i++, obj++) {
340 int verify = obj->ioo_id != 0;
343 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
344 struct page *page = r->page;
347 if (!page || !(addr = page_address(page)) ||
348 !kern_addr_valid(addr)) {
350 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
351 obj->ioo_id, page, j, obj->ioo_bufcnt);
352 GOTO(commitrw_cleanup, rc = -EFAULT);
355 atomic_inc(&echo_page_rws);
357 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
358 r->page, addr, r->offset);
361 page_debug_check("echo", addr, r->len,
362 r->offset, obj->ioo_id);
365 __free_pages(page, 0);
366 atomic_dec(&obd->u.echo.eo_prep);
369 CDEBUG(D_PAGE, "%d pages remain after commit\n",
370 atomic_read(&obd->u.echo.eo_prep));
374 CERROR("cleaning up %ld pages (%d obdos)\n",
375 niocount - (long)(r - res) - 1, objcount);
376 while (++r < res + niocount) {
377 struct page *page = r->page;
380 __free_pages(page, 0);
381 atomic_dec(&obd->u.echo.eo_prep);
386 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
390 obddev->obd_namespace =
391 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
392 if (obddev->obd_namespace == NULL) {
400 static int echo_cleanup(struct obd_device *obddev)
404 ldlm_namespace_free(obddev->obd_namespace);
405 CERROR("%d prep/commitrw pages leaked\n",
406 atomic_read(&obddev->u.echo.eo_prep));
411 struct obd_ops echo_obd_ops = {
412 o_connect: echo_connect,
413 o_disconnect: echo_disconnect,
414 o_create: echo_create,
415 o_destroy: echo_destroy,
416 o_getattr: echo_getattr,
417 o_setattr: echo_setattr,
418 o_preprw: echo_preprw,
419 o_commitrw: echo_commitrw,
421 o_cleanup: echo_cleanup
424 static int __init obdecho_init(void)
426 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
427 " info@clusterfs.com\n");
431 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
434 static void __exit obdecho_exit(void)
438 class_unregister_type(OBD_ECHO_DEVICENAME);
441 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
442 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
443 MODULE_LICENSE("GPL");
445 module_init(obdecho_init);
446 module_exit(obdecho_exit);