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.39 2002/10/15 23:25:10 shaver Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.39 $"
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 static atomic_t echo_page_rws;
40 static atomic_t echo_getattrs;
42 #define ECHO_PROC_STAT "sys/obdecho"
43 #define ECHO_INIT_OBJID 0x1000000000000000ULL
45 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
48 long long attrs = atomic_read(&echo_getattrs);
49 long long pages = atomic_read(&echo_page_rws);
56 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
62 int echo_proc_write(struct file *file, const char *ubuffer,
63 unsigned long count, void *data)
65 /* Ignore what we've been asked to write, and just zero the counters */
66 atomic_set (&echo_page_rws, 0);
67 atomic_set (&echo_getattrs, 0);
72 void echo_proc_init(void)
74 struct proc_dir_entry *entry;
76 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
79 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
84 entry->read_proc = echo_proc_read;
85 entry->write_proc = echo_proc_write;
88 void echo_proc_fini(void)
90 remove_proc_entry(ECHO_PROC_STAT, 0);
93 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
94 obd_uuid_t cluuid, struct recovd_obd *recovd,
95 ptlrpc_recovery_cb_t recover)
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_FLTYPE)) {
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 || oa->o_id < ECHO_INIT_OBJID) {
173 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
177 atomic_inc(&obd->u.echo.eo_destroy);
182 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
183 struct lov_stripe_md *md)
188 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
189 struct lov_stripe_md *md)
194 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
195 struct lov_stripe_md *md)
197 struct obd_device *obd = class_conn2obd(conn);
198 obd_id id = oa->o_id;
201 CERROR("invalid client "LPX64"\n", conn->addr);
205 if (!(oa->o_valid & OBD_MD_FLID)) {
206 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
210 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
212 oa->o_valid |= OBD_MD_FLID;
214 atomic_inc(&echo_getattrs);
219 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
220 struct lov_stripe_md *md)
222 struct obd_device *obd = class_conn2obd(conn);
225 CERROR("invalid client "LPX64"\n", conn->addr);
229 if (!(oa->o_valid & OBD_MD_FLID)) {
230 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
234 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
236 atomic_inc(&obd->u.echo.eo_setattr);
241 /* This allows us to verify that desc_private is passed unmolested */
242 #define DESC_PRIV 0x10293847
244 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
245 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
246 struct niobuf_local *res, void **desc_private)
248 struct obd_device *obd;
249 struct niobuf_local *r = res;
255 obd = class_conn2obd(conn);
257 CERROR("invalid client "LPX64"\n", conn->addr);
261 memset(res, 0, sizeof(*res) * niocount);
263 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
264 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
266 *desc_private = (void *)DESC_PRIV;
268 for (i = 0; i < objcount; i++, obj++) {
269 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
270 int verify = obj->ioo_id != 0;
273 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
274 r->page = alloc_pages(gfp_mask, 0);
276 CERROR("can't get page %d/%d for id "LPU64"\n",
277 j, obj->ioo_bufcnt, obj->ioo_id);
278 GOTO(preprw_cleanup, rc = -ENOMEM);
280 atomic_inc(&obd->u.echo.eo_prep);
282 r->offset = nb->offset;
283 r->addr = kmap(r->page);
286 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
287 r->page, r->addr, r->offset);
289 if (verify && cmd == OBD_BRW_READ)
290 page_debug_setup(r->addr, r->len, r->offset,
293 page_debug_setup(r->addr, r->len,
298 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
299 atomic_read(&obd->u.echo.eo_prep));
304 /* It is possible that we would rather handle errors by allow
305 * any already-set-up pages to complete, rather than tearing them
306 * all down again. I believe that this is what the in-kernel
307 * prep/commit operations do.
309 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
312 __free_pages(r->page, 0);
313 atomic_dec(&obd->u.echo.eo_prep);
315 memset(res, 0, sizeof(*res) * niocount);
320 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
321 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
324 struct obd_device *obd;
325 struct niobuf_local *r = res;
330 obd = class_conn2obd(conn);
332 CERROR("invalid client "LPX64"\n", conn->addr);
336 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
337 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
340 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
344 if (niocount && !r) {
345 CERROR("NULL res niobuf with niocount %d\n", niocount);
349 LASSERT(desc_private == (void *)DESC_PRIV);
351 for (i = 0; i < objcount; i++, obj++) {
352 int verify = obj->ioo_id != 0;
355 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
356 struct page *page = r->page;
359 if (!page || !(addr = page_address(page)) ||
360 !kern_addr_valid(addr)) {
362 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
363 obj->ioo_id, page, j, obj->ioo_bufcnt);
364 GOTO(commitrw_cleanup, rc = -EFAULT);
367 atomic_inc(&echo_page_rws);
369 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
370 r->page, addr, r->offset);
373 page_debug_check("echo", addr, r->len,
374 r->offset, obj->ioo_id);
377 __free_pages(page, 0);
378 atomic_dec(&obd->u.echo.eo_prep);
381 CDEBUG(D_PAGE, "%d pages remain after commit\n",
382 atomic_read(&obd->u.echo.eo_prep));
386 CERROR("cleaning up %ld pages (%d obdos)\n",
387 niocount - (long)(r - res) - 1, objcount);
388 while (++r < res + niocount) {
389 struct page *page = r->page;
392 __free_pages(page, 0);
393 atomic_dec(&obd->u.echo.eo_prep);
398 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
402 obddev->obd_namespace =
403 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
404 if (obddev->obd_namespace == NULL) {
408 spin_lock_init(&obddev->u.echo.eo_lock);
409 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
414 static int echo_cleanup(struct obd_device *obddev)
418 ldlm_namespace_free(obddev->obd_namespace);
419 CERROR("%d prep/commitrw pages leaked\n",
420 atomic_read(&obddev->u.echo.eo_prep));
425 struct obd_ops echo_obd_ops = {
426 o_connect: echo_connect,
427 o_disconnect: echo_disconnect,
428 o_create: echo_create,
429 o_destroy: echo_destroy,
432 o_getattr: echo_getattr,
433 o_setattr: echo_setattr,
434 o_preprw: echo_preprw,
435 o_commitrw: echo_commitrw,
437 o_cleanup: echo_cleanup
440 static int __init obdecho_init(void)
442 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
443 " info@clusterfs.com\n");
447 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
450 static void __exit obdecho_exit(void)
454 class_unregister_type(OBD_ECHO_DEVICENAME);
457 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
458 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
459 MODULE_LICENSE("GPL");
461 module_init(obdecho_init);
462 module_exit(obdecho_exit);