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.31 2002/09/04 16:29:14 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.31 $"
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 struct obd_device *obd;
50 long long attrs = atomic_read(&echo_getattrs);
51 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,
102 rc = class_connect(conn, obd, cluuid);
110 static int echo_disconnect(struct lustre_handle *conn)
114 rc = class_disconnect(conn);
121 static __u64 echo_next_id(struct obd_device *obddev)
125 spin_lock(&obddev->u.echo.eo_lock);
126 id = ++obddev->u.echo.eo_lastino;
127 spin_unlock(&obddev->u.echo.eo_lock);
132 int echo_create(struct lustre_handle *conn, struct obdo *oa,
133 struct lov_stripe_md **ea)
135 struct obd_device *obd = class_conn2obd(conn);
138 CERROR("invalid client %Lx\n", conn->addr);
142 if (!(oa->o_mode && S_IFMT)) {
143 CERROR("filter obd: no type!\n");
147 if (!(oa->o_valid & OBD_MD_FLMODE)) {
148 CERROR("invalid o_valid %08x\n", oa->o_valid);
152 oa->o_id = echo_next_id(obd);
153 oa->o_valid = OBD_MD_FLID;
154 atomic_inc(&obd->u.echo.eo_create);
159 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
160 struct lov_stripe_md *ea)
162 struct obd_device *obd = class_conn2obd(conn);
165 CERROR("invalid client "LPX64"\n", conn->addr);
169 if (!(oa->o_valid & OBD_MD_FLID)) {
170 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
174 if (oa->o_id > obd->u.echo.eo_lastino) {
175 CERROR("bad destroy objid: %Ld\n", (long long)oa->o_id);
179 atomic_inc(&obd->u.echo.eo_destroy);
184 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
185 struct lov_stripe_md *md)
187 struct obd_device *obd = class_conn2obd(conn);
188 obd_id id = oa->o_id;
191 CERROR("invalid client "LPX64"\n", conn->addr);
195 if (!(oa->o_valid & OBD_MD_FLID)) {
196 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
200 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
202 oa->o_valid |= OBD_MD_FLID;
204 atomic_inc(&echo_getattrs);
209 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
210 struct lov_stripe_md *md)
212 struct obd_device *obd = class_conn2obd(conn);
215 CERROR("invalid client "LPX64"\n", conn->addr);
219 if (!(oa->o_valid & OBD_MD_FLID)) {
220 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
224 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
226 atomic_inc(&obd->u.echo.eo_setattr);
231 /* This allows us to verify that desc_private is passed unmolested */
232 #define DESC_PRIV 0x10293847
234 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
235 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
236 struct niobuf_local *res, void **desc_private)
238 struct obd_device *obd;
239 struct niobuf_local *r = res;
245 obd = class_conn2obd(conn);
247 CERROR("invalid client "LPX64"\n", conn->addr);
251 memset(res, 0, sizeof(*res) * niocount);
253 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
254 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
256 *desc_private = (void *)DESC_PRIV;
258 for (i = 0; i < objcount; i++, obj++) {
259 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
260 int verify = obj->ioo_id != 0;
263 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
264 r->page = alloc_pages(gfp_mask, 0);
266 CERROR("can't get page %d/%d for id "LPU64"\n",
267 j, obj->ioo_bufcnt, obj->ioo_id);
268 GOTO(preprw_cleanup, rc = -ENOMEM);
270 atomic_inc(&obd->u.echo.eo_prep);
272 r->offset = nb->offset;
273 r->addr = kmap(r->page);
276 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
277 r->page, r->addr, r->offset);
279 if (verify && cmd == OBD_BRW_READ)
280 page_debug_setup(r->addr, r->len, r->offset,
283 page_debug_setup(r->addr, r->len,
288 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
289 atomic_read(&obd->u.echo.eo_prep));
294 /* It is possible that we would rather handle errors by allow
295 * any already-set-up pages to complete, rather than tearing them
296 * all down again. I believe that this is what the in-kernel
297 * prep/commit operations do.
299 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
302 __free_pages(r->page, 0);
303 atomic_dec(&obd->u.echo.eo_prep);
305 memset(res, 0, sizeof(*res) * niocount);
310 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
311 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
314 struct obd_device *obd;
315 struct niobuf_local *r = res;
320 obd = class_conn2obd(conn);
322 CERROR("invalid client "LPX64"\n", conn->addr);
326 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
327 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
330 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
334 if (niocount && !r) {
335 CERROR("NULL res niobuf with niocount %d\n", niocount);
339 LASSERT(desc_private == (void *)DESC_PRIV);
341 for (i = 0; i < objcount; i++, obj++) {
342 int verify = obj->ioo_id != 0;
345 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
346 struct page *page = r->page;
349 if (!page || !(addr = page_address(page)) ||
350 !kern_addr_valid(addr)) {
352 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
353 obj->ioo_id, page, j, obj->ioo_bufcnt);
354 GOTO(commitrw_cleanup, rc = -EFAULT);
357 atomic_inc(&echo_page_rws);
359 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
360 r->page, addr, r->offset);
363 page_debug_check("echo", addr, r->len,
364 r->offset, obj->ioo_id);
367 __free_pages(page, 0);
368 atomic_dec(&obd->u.echo.eo_prep);
371 CDEBUG(D_PAGE, "%d pages remain after commit\n",
372 atomic_read(&obd->u.echo.eo_prep));
376 CERROR("cleaning up %ld pages (%d obdos)\n",
377 niocount - (long)(r - res) - 1, objcount);
378 while (++r < res + niocount) {
379 struct page *page = r->page;
382 __free_pages(page, 0);
383 atomic_dec(&obd->u.echo.eo_prep);
388 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
392 obddev->obd_namespace =
393 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
394 if (obddev->obd_namespace == NULL) {
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);