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.40 2002/10/18 21:19:57 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.40 $"
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/highmem.h>
24 #include <linux/stat.h>
25 #include <linux/smp_lock.h>
26 #include <linux/ext2_fs.h>
27 #include <linux/quotaops.h>
28 #include <linux/proc_fs.h>
29 #include <linux/init.h>
30 #include <asm/unistd.h>
32 #define DEBUG_SUBSYSTEM S_ECHO
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd_echo.h>
37 #include <linux/lustre_debug.h>
38 #include <linux/lustre_dlm.h>
40 static atomic_t echo_page_rws;
41 static atomic_t echo_getattrs;
43 #define ECHO_PROC_STAT "sys/obdecho"
44 #define ECHO_INIT_OBJID 0x1000000000000000ULL
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,
95 obd_uuid_t cluuid, struct recovd_obd *recovd,
96 ptlrpc_recovery_cb_t recover)
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_FLTYPE)) {
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: "LPX64"\n", oa->o_id);
178 atomic_inc(&obd->u.echo.eo_destroy);
183 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
184 struct lov_stripe_md *md)
189 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
190 struct lov_stripe_md *md)
195 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
196 struct lov_stripe_md *md)
198 struct obd_device *obd = class_conn2obd(conn);
199 obd_id id = oa->o_id;
202 CERROR("invalid client "LPX64"\n", conn->addr);
206 if (!(oa->o_valid & OBD_MD_FLID)) {
207 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
211 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
213 oa->o_valid |= OBD_MD_FLID;
215 atomic_inc(&echo_getattrs);
220 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
221 struct lov_stripe_md *md)
223 struct obd_device *obd = class_conn2obd(conn);
226 CERROR("invalid client "LPX64"\n", conn->addr);
230 if (!(oa->o_valid & OBD_MD_FLID)) {
231 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
235 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
237 atomic_inc(&obd->u.echo.eo_setattr);
242 /* This allows us to verify that desc_private is passed unmolested */
243 #define DESC_PRIV 0x10293847
245 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
246 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
247 struct niobuf_local *res, void **desc_private)
249 struct obd_device *obd;
250 struct niobuf_local *r = res;
256 obd = class_conn2obd(conn);
258 CERROR("invalid client "LPX64"\n", conn->addr);
262 memset(res, 0, sizeof(*res) * niocount);
264 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
265 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
267 *desc_private = (void *)DESC_PRIV;
269 for (i = 0; i < objcount; i++, obj++) {
270 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
271 int verify = obj->ioo_id != 0;
274 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
275 r->page = alloc_pages(gfp_mask, 0);
277 CERROR("can't get page %d/%d for id "LPU64"\n",
278 j, obj->ioo_bufcnt, obj->ioo_id);
279 GOTO(preprw_cleanup, rc = -ENOMEM);
281 atomic_inc(&obd->u.echo.eo_prep);
283 r->offset = nb->offset;
284 r->addr = kmap(r->page);
287 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
288 r->page, r->addr, r->offset);
290 if (verify && cmd == OBD_BRW_READ)
291 page_debug_setup(r->addr, r->len, r->offset,
294 page_debug_setup(r->addr, r->len,
299 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
300 atomic_read(&obd->u.echo.eo_prep));
305 /* It is possible that we would rather handle errors by allow
306 * any already-set-up pages to complete, rather than tearing them
307 * all down again. I believe that this is what the in-kernel
308 * prep/commit operations do.
310 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
313 __free_pages(r->page, 0);
314 atomic_dec(&obd->u.echo.eo_prep);
316 memset(res, 0, sizeof(*res) * niocount);
321 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
322 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
325 struct obd_device *obd;
326 struct niobuf_local *r = res;
331 obd = class_conn2obd(conn);
333 CERROR("invalid client "LPX64"\n", conn->addr);
337 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
338 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
341 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
345 if (niocount && !r) {
346 CERROR("NULL res niobuf with niocount %d\n", niocount);
350 LASSERT(desc_private == (void *)DESC_PRIV);
352 for (i = 0; i < objcount; i++, obj++) {
353 int verify = obj->ioo_id != 0;
356 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
357 struct page *page = r->page;
360 if (!page || !(addr = page_address(page)) ||
361 !kern_addr_valid(addr)) {
363 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
364 obj->ioo_id, page, j, obj->ioo_bufcnt);
365 GOTO(commitrw_cleanup, rc = -EFAULT);
368 atomic_inc(&echo_page_rws);
370 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
371 r->page, addr, r->offset);
374 page_debug_check("echo", addr, r->len,
375 r->offset, obj->ioo_id);
378 __free_pages(page, 0);
379 atomic_dec(&obd->u.echo.eo_prep);
382 CDEBUG(D_PAGE, "%d pages remain after commit\n",
383 atomic_read(&obd->u.echo.eo_prep));
387 CERROR("cleaning up %ld pages (%d obdos)\n",
388 niocount - (long)(r - res) - 1, objcount);
389 while (++r < res + niocount) {
390 struct page *page = r->page;
393 __free_pages(page, 0);
394 atomic_dec(&obd->u.echo.eo_prep);
399 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
403 obddev->obd_namespace =
404 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
405 if (obddev->obd_namespace == NULL) {
409 spin_lock_init(&obddev->u.echo.eo_lock);
410 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
415 static int echo_cleanup(struct obd_device *obddev)
419 ldlm_namespace_free(obddev->obd_namespace);
420 CERROR("%d prep/commitrw pages leaked\n",
421 atomic_read(&obddev->u.echo.eo_prep));
426 struct obd_ops echo_obd_ops = {
427 o_connect: echo_connect,
428 o_disconnect: echo_disconnect,
429 o_create: echo_create,
430 o_destroy: echo_destroy,
433 o_getattr: echo_getattr,
434 o_setattr: echo_setattr,
435 o_preprw: echo_preprw,
436 o_commitrw: echo_commitrw,
438 o_cleanup: echo_cleanup
441 static int __init obdecho_init(void)
443 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
444 " info@clusterfs.com\n");
448 return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
451 static void __exit obdecho_exit(void)
455 class_unregister_type(OBD_ECHO_DEVICENAME);
458 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
459 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
460 MODULE_LICENSE("GPL");
462 module_init(obdecho_init);
463 module_exit(obdecho_exit);