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.41 2002/10/18 22:32:45 pschwan Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.41 $"
20 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/highmem.h>
25 #include <linux/stat.h>
26 #include <linux/sched.h>
27 #include <linux/smp_lock.h>
28 #include <linux/ext2_fs.h>
29 #include <linux/quotaops.h>
30 #include <linux/proc_fs.h>
31 #include <linux/init.h>
32 #include <asm/unistd.h>
34 #define DEBUG_SUBSYSTEM S_ECHO
36 #include <linux/obd_support.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd_echo.h>
39 #include <linux/lustre_debug.h>
40 #include <linux/lustre_dlm.h>
42 static atomic_t echo_page_rws;
43 static atomic_t echo_getattrs;
45 #define ECHO_PROC_STAT "sys/obdecho"
46 #define ECHO_INIT_OBJID 0x1000000000000000ULL
48 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
51 long long attrs = atomic_read(&echo_getattrs);
52 long long pages = atomic_read(&echo_page_rws);
59 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
65 int echo_proc_write(struct file *file, const char *ubuffer,
66 unsigned long count, void *data)
68 /* Ignore what we've been asked to write, and just zero the counters */
69 atomic_set (&echo_page_rws, 0);
70 atomic_set (&echo_getattrs, 0);
75 void echo_proc_init(void)
77 struct proc_dir_entry *entry;
79 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
82 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
87 entry->read_proc = echo_proc_read;
88 entry->write_proc = echo_proc_write;
91 void echo_proc_fini(void)
93 remove_proc_entry(ECHO_PROC_STAT, 0);
96 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
97 obd_uuid_t cluuid, struct recovd_obd *recovd,
98 ptlrpc_recovery_cb_t recover)
103 rc = class_connect(conn, obd, cluuid);
111 static int echo_disconnect(struct lustre_handle *conn)
115 rc = class_disconnect(conn);
122 static __u64 echo_next_id(struct obd_device *obddev)
126 spin_lock(&obddev->u.echo.eo_lock);
127 id = ++obddev->u.echo.eo_lastino;
128 spin_unlock(&obddev->u.echo.eo_lock);
133 int echo_create(struct lustre_handle *conn, struct obdo *oa,
134 struct lov_stripe_md **ea)
136 struct obd_device *obd = class_conn2obd(conn);
139 CERROR("invalid client %Lx\n", conn->addr);
143 if (!(oa->o_mode && S_IFMT)) {
144 CERROR("filter obd: no type!\n");
148 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
149 CERROR("invalid o_valid %08x\n", oa->o_valid);
153 oa->o_id = echo_next_id(obd);
154 oa->o_valid = OBD_MD_FLID;
155 atomic_inc(&obd->u.echo.eo_create);
160 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
161 struct lov_stripe_md *ea)
163 struct obd_device *obd = class_conn2obd(conn);
166 CERROR("invalid client "LPX64"\n", conn->addr);
170 if (!(oa->o_valid & OBD_MD_FLID)) {
171 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
175 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
176 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
180 atomic_inc(&obd->u.echo.eo_destroy);
185 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
186 struct lov_stripe_md *md)
191 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
192 struct lov_stripe_md *md)
197 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
198 struct lov_stripe_md *md)
200 struct obd_device *obd = class_conn2obd(conn);
201 obd_id id = oa->o_id;
204 CERROR("invalid client "LPX64"\n", conn->addr);
208 if (!(oa->o_valid & OBD_MD_FLID)) {
209 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
213 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
215 oa->o_valid |= OBD_MD_FLID;
217 atomic_inc(&echo_getattrs);
222 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
223 struct lov_stripe_md *md)
225 struct obd_device *obd = class_conn2obd(conn);
228 CERROR("invalid client "LPX64"\n", conn->addr);
232 if (!(oa->o_valid & OBD_MD_FLID)) {
233 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
237 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
239 atomic_inc(&obd->u.echo.eo_setattr);
244 /* This allows us to verify that desc_private is passed unmolested */
245 #define DESC_PRIV 0x10293847
247 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
248 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
249 struct niobuf_local *res, void **desc_private)
251 struct obd_device *obd;
252 struct niobuf_local *r = res;
258 obd = class_conn2obd(conn);
260 CERROR("invalid client "LPX64"\n", conn->addr);
264 memset(res, 0, sizeof(*res) * niocount);
266 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
267 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
269 *desc_private = (void *)DESC_PRIV;
271 for (i = 0; i < objcount; i++, obj++) {
272 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
273 int verify = obj->ioo_id != 0;
276 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
277 r->page = alloc_pages(gfp_mask, 0);
279 CERROR("can't get page %d/%d for id "LPU64"\n",
280 j, obj->ioo_bufcnt, obj->ioo_id);
281 GOTO(preprw_cleanup, rc = -ENOMEM);
283 atomic_inc(&obd->u.echo.eo_prep);
285 r->offset = nb->offset;
286 r->addr = kmap(r->page);
289 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
290 r->page, r->addr, r->offset);
292 if (verify && cmd == OBD_BRW_READ)
293 page_debug_setup(r->addr, r->len, r->offset,
296 page_debug_setup(r->addr, r->len,
301 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
302 atomic_read(&obd->u.echo.eo_prep));
307 /* It is possible that we would rather handle errors by allow
308 * any already-set-up pages to complete, rather than tearing them
309 * all down again. I believe that this is what the in-kernel
310 * prep/commit operations do.
312 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
315 __free_pages(r->page, 0);
316 atomic_dec(&obd->u.echo.eo_prep);
318 memset(res, 0, sizeof(*res) * niocount);
323 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
324 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
327 struct obd_device *obd;
328 struct niobuf_local *r = res;
333 obd = class_conn2obd(conn);
335 CERROR("invalid client "LPX64"\n", conn->addr);
339 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
340 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
343 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
347 if (niocount && !r) {
348 CERROR("NULL res niobuf with niocount %d\n", niocount);
352 LASSERT(desc_private == (void *)DESC_PRIV);
354 for (i = 0; i < objcount; i++, obj++) {
355 int verify = obj->ioo_id != 0;
358 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
359 struct page *page = r->page;
362 if (!page || !(addr = page_address(page)) ||
363 !kern_addr_valid(addr)) {
365 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
366 obj->ioo_id, page, j, obj->ioo_bufcnt);
367 GOTO(commitrw_cleanup, rc = -EFAULT);
370 atomic_inc(&echo_page_rws);
372 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
373 r->page, addr, r->offset);
376 page_debug_check("echo", addr, r->len,
377 r->offset, obj->ioo_id);
380 __free_pages(page, 0);
381 atomic_dec(&obd->u.echo.eo_prep);
384 CDEBUG(D_PAGE, "%d pages remain after commit\n",
385 atomic_read(&obd->u.echo.eo_prep));
389 CERROR("cleaning up %ld pages (%d obdos)\n",
390 niocount - (long)(r - res) - 1, objcount);
391 while (++r < res + niocount) {
392 struct page *page = r->page;
395 __free_pages(page, 0);
396 atomic_dec(&obd->u.echo.eo_prep);
401 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
405 obddev->obd_namespace =
406 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
407 if (obddev->obd_namespace == NULL) {
411 spin_lock_init(&obddev->u.echo.eo_lock);
412 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
417 static int echo_cleanup(struct obd_device *obddev)
421 ldlm_namespace_free(obddev->obd_namespace);
422 CERROR("%d prep/commitrw pages leaked\n",
423 atomic_read(&obddev->u.echo.eo_prep));
428 struct obd_ops echo_obd_ops = {
429 o_connect: echo_connect,
430 o_disconnect: echo_disconnect,
431 o_create: echo_create,
432 o_destroy: echo_destroy,
435 o_getattr: echo_getattr,
436 o_setattr: echo_setattr,
437 o_preprw: echo_preprw,
438 o_commitrw: echo_commitrw,
440 o_cleanup: echo_cleanup
443 static int __init obdecho_init(void)
445 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
446 " info@clusterfs.com\n");
450 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
453 static void __exit obdecho_exit(void)
457 class_unregister_type(OBD_ECHO_DEVICENAME);
460 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
461 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
462 MODULE_LICENSE("GPL");
464 module_init(obdecho_init);
465 module_exit(obdecho_exit);