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.33 2002/09/06 22:23:49 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.33 $"
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"
45 #define ECHO_INIT_OBJID 0x1000000000000000ULL
47 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
50 long long attrs = atomic_read(&echo_getattrs);
51 long long pages = atomic_read(&echo_page_rws);
58 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
64 int echo_proc_write(struct file *file, const char *ubuffer,
65 unsigned long count, void *data)
67 /* Ignore what we've been asked to write, and just zero the counters */
68 atomic_set (&echo_page_rws, 0);
69 atomic_set (&echo_getattrs, 0);
74 void echo_proc_init(void)
76 struct proc_dir_entry *entry;
78 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
81 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
86 entry->read_proc = echo_proc_read;
87 entry->write_proc = echo_proc_write;
90 void echo_proc_fini(void)
92 remove_proc_entry(ECHO_PROC_STAT, 0);
95 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
101 rc = class_connect(conn, obd, cluuid);
109 static int echo_disconnect(struct lustre_handle *conn)
113 rc = class_disconnect(conn);
120 static __u64 echo_next_id(struct obd_device *obddev)
124 spin_lock(&obddev->u.echo.eo_lock);
125 id = ++obddev->u.echo.eo_lastino;
126 spin_unlock(&obddev->u.echo.eo_lock);
131 int echo_create(struct lustre_handle *conn, struct obdo *oa,
132 struct lov_stripe_md **ea)
134 struct obd_device *obd = class_conn2obd(conn);
137 CERROR("invalid client %Lx\n", conn->addr);
141 if (!(oa->o_mode && S_IFMT)) {
142 CERROR("filter obd: no type!\n");
146 if (!(oa->o_valid & OBD_MD_FLMODE)) {
147 CERROR("invalid o_valid %08x\n", oa->o_valid);
151 oa->o_id = echo_next_id(obd);
152 oa->o_valid = OBD_MD_FLID;
153 atomic_inc(&obd->u.echo.eo_create);
158 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
159 struct lov_stripe_md *ea)
161 struct obd_device *obd = class_conn2obd(conn);
164 CERROR("invalid client "LPX64"\n", conn->addr);
168 if (!(oa->o_valid & OBD_MD_FLID)) {
169 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
173 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
174 CERROR("bad destroy objid: 0x"LPX64"\n", oa->o_id);
178 atomic_inc(&obd->u.echo.eo_destroy);
183 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
184 struct lov_stripe_md *md)
186 struct obd_device *obd = class_conn2obd(conn);
187 obd_id id = oa->o_id;
190 CERROR("invalid client "LPX64"\n", conn->addr);
194 if (!(oa->o_valid & OBD_MD_FLID)) {
195 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
199 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
201 oa->o_valid |= OBD_MD_FLID;
203 atomic_inc(&echo_getattrs);
208 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
209 struct lov_stripe_md *md)
211 struct obd_device *obd = class_conn2obd(conn);
214 CERROR("invalid client "LPX64"\n", conn->addr);
218 if (!(oa->o_valid & OBD_MD_FLID)) {
219 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
223 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
225 atomic_inc(&obd->u.echo.eo_setattr);
230 /* This allows us to verify that desc_private is passed unmolested */
231 #define DESC_PRIV 0x10293847
233 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
234 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
235 struct niobuf_local *res, void **desc_private)
237 struct obd_device *obd;
238 struct niobuf_local *r = res;
244 obd = class_conn2obd(conn);
246 CERROR("invalid client "LPX64"\n", conn->addr);
250 memset(res, 0, sizeof(*res) * niocount);
252 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
253 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
255 *desc_private = (void *)DESC_PRIV;
257 for (i = 0; i < objcount; i++, obj++) {
258 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
259 int verify = obj->ioo_id != 0;
262 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
263 r->page = alloc_pages(gfp_mask, 0);
265 CERROR("can't get page %d/%d for id "LPU64"\n",
266 j, obj->ioo_bufcnt, obj->ioo_id);
267 GOTO(preprw_cleanup, rc = -ENOMEM);
269 atomic_inc(&obd->u.echo.eo_prep);
271 r->offset = nb->offset;
272 r->addr = kmap(r->page);
275 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
276 r->page, r->addr, r->offset);
278 if (verify && cmd == OBD_BRW_READ)
279 page_debug_setup(r->addr, r->len, r->offset,
282 page_debug_setup(r->addr, r->len,
287 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
288 atomic_read(&obd->u.echo.eo_prep));
293 /* It is possible that we would rather handle errors by allow
294 * any already-set-up pages to complete, rather than tearing them
295 * all down again. I believe that this is what the in-kernel
296 * prep/commit operations do.
298 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
301 __free_pages(r->page, 0);
302 atomic_dec(&obd->u.echo.eo_prep);
304 memset(res, 0, sizeof(*res) * niocount);
309 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
310 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
313 struct obd_device *obd;
314 struct niobuf_local *r = res;
319 obd = class_conn2obd(conn);
321 CERROR("invalid client "LPX64"\n", conn->addr);
325 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
326 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
329 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
333 if (niocount && !r) {
334 CERROR("NULL res niobuf with niocount %d\n", niocount);
338 LASSERT(desc_private == (void *)DESC_PRIV);
340 for (i = 0; i < objcount; i++, obj++) {
341 int verify = obj->ioo_id != 0;
344 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
345 struct page *page = r->page;
348 if (!page || !(addr = page_address(page)) ||
349 !kern_addr_valid(addr)) {
351 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
352 obj->ioo_id, page, j, obj->ioo_bufcnt);
353 GOTO(commitrw_cleanup, rc = -EFAULT);
356 atomic_inc(&echo_page_rws);
358 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
359 r->page, addr, r->offset);
362 page_debug_check("echo", addr, r->len,
363 r->offset, obj->ioo_id);
366 __free_pages(page, 0);
367 atomic_dec(&obd->u.echo.eo_prep);
370 CDEBUG(D_PAGE, "%d pages remain after commit\n",
371 atomic_read(&obd->u.echo.eo_prep));
375 CERROR("cleaning up %ld pages (%d obdos)\n",
376 niocount - (long)(r - res) - 1, objcount);
377 while (++r < res + niocount) {
378 struct page *page = r->page;
381 __free_pages(page, 0);
382 atomic_dec(&obd->u.echo.eo_prep);
387 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
391 obddev->obd_namespace =
392 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
393 if (obddev->obd_namespace == NULL) {
397 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
402 static int echo_cleanup(struct obd_device *obddev)
406 ldlm_namespace_free(obddev->obd_namespace);
407 CERROR("%d prep/commitrw pages leaked\n",
408 atomic_read(&obddev->u.echo.eo_prep));
413 struct obd_ops echo_obd_ops = {
414 o_connect: echo_connect,
415 o_disconnect: echo_disconnect,
416 o_create: echo_create,
417 o_destroy: echo_destroy,
418 o_getattr: echo_getattr,
419 o_setattr: echo_setattr,
420 o_preprw: echo_preprw,
421 o_commitrw: echo_commitrw,
423 o_cleanup: echo_cleanup
426 static int __init obdecho_init(void)
428 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
429 " info@clusterfs.com\n");
433 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
436 static void __exit obdecho_exit(void)
440 class_unregister_type(OBD_ECHO_DEVICENAME);
443 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
444 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
445 MODULE_LICENSE("GPL");
447 module_init(obdecho_init);
448 module_exit(obdecho_exit);