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.48 2002/11/13 04:45:21 thantry Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.48 $"
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>
41 #include <linux/lprocfs_status.h>
43 static atomic_t echo_page_rws;
44 static atomic_t echo_getattrs;
46 #define ECHO_PROC_STAT "sys/obdecho"
47 #define ECHO_INIT_OBJID 0x1000000000000000ULL
49 extern struct lprocfs_vars status_var_nm_1[];
50 extern struct lprocfs_vars status_class_var[];
52 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
55 long long attrs = atomic_read(&echo_getattrs);
56 long long pages = atomic_read(&echo_page_rws);
63 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
69 int echo_proc_write(struct file *file, const char *ubuffer,
70 unsigned long count, void *data)
72 /* Ignore what we've been asked to write, and just zero the counters */
73 atomic_set (&echo_page_rws, 0);
74 atomic_set (&echo_getattrs, 0);
79 void echo_proc_init(void)
81 struct proc_dir_entry *entry;
83 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
86 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
91 entry->read_proc = echo_proc_read;
92 entry->write_proc = echo_proc_write;
95 void echo_proc_fini(void)
97 remove_proc_entry(ECHO_PROC_STAT, 0);
100 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
101 obd_uuid_t cluuid, struct recovd_obd *recovd,
102 ptlrpc_recovery_cb_t recover)
107 rc = class_connect(conn, obd, cluuid);
115 static int echo_disconnect(struct lustre_handle *conn)
119 rc = class_disconnect(conn);
126 static __u64 echo_next_id(struct obd_device *obddev)
130 spin_lock(&obddev->u.echo.eo_lock);
131 id = ++obddev->u.echo.eo_lastino;
132 spin_unlock(&obddev->u.echo.eo_lock);
137 int echo_create(struct lustre_handle *conn, struct obdo *oa,
138 struct lov_stripe_md **ea)
140 struct obd_device *obd = class_conn2obd(conn);
143 CERROR("invalid client %Lx\n", conn->addr);
147 if (!(oa->o_mode && S_IFMT)) {
148 CERROR("filter obd: no type!\n");
152 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
153 CERROR("invalid o_valid %08x\n", oa->o_valid);
157 oa->o_id = echo_next_id(obd);
158 oa->o_valid = OBD_MD_FLID;
159 atomic_inc(&obd->u.echo.eo_create);
164 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
165 struct lov_stripe_md *ea)
167 struct obd_device *obd = class_conn2obd(conn);
170 CERROR("invalid client "LPX64"\n", conn->addr);
174 if (!(oa->o_valid & OBD_MD_FLID)) {
175 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
179 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
180 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
184 atomic_inc(&obd->u.echo.eo_destroy);
189 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
190 struct lov_stripe_md *md)
195 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
196 struct lov_stripe_md *md)
201 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
202 struct lov_stripe_md *md)
204 struct obd_device *obd = class_conn2obd(conn);
205 obd_id id = oa->o_id;
208 CERROR("invalid client "LPX64"\n", conn->addr);
212 if (!(oa->o_valid & OBD_MD_FLID)) {
213 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
217 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
219 oa->o_valid |= OBD_MD_FLID;
221 atomic_inc(&echo_getattrs);
226 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
227 struct lov_stripe_md *md)
229 struct obd_device *obd = class_conn2obd(conn);
232 CERROR("invalid client "LPX64"\n", conn->addr);
236 if (!(oa->o_valid & OBD_MD_FLID)) {
237 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
241 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
243 atomic_inc(&obd->u.echo.eo_setattr);
248 /* This allows us to verify that desc_private is passed unmolested */
249 #define DESC_PRIV 0x10293847
251 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
252 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
253 struct niobuf_local *res, void **desc_private)
255 struct obd_device *obd;
256 struct niobuf_local *r = res;
262 obd = class_conn2obd(conn);
264 CERROR("invalid client "LPX64"\n", conn->addr);
268 memset(res, 0, sizeof(*res) * niocount);
270 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
271 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
273 *desc_private = (void *)DESC_PRIV;
275 obd_kmap_get(niocount, 1);
277 for (i = 0; i < objcount; i++, obj++) {
278 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
279 int verify = obj->ioo_id != 0;
282 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
283 r->page = alloc_pages(gfp_mask, 0);
285 CERROR("can't get page %d/%d for id "LPU64"\n",
286 j, obj->ioo_bufcnt, obj->ioo_id);
287 GOTO(preprw_cleanup, rc = -ENOMEM);
289 atomic_inc(&obd->u.echo.eo_prep);
291 r->offset = nb->offset;
292 r->addr = kmap(r->page);
295 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
296 r->page, r->addr, r->offset);
298 if (verify && cmd == OBD_BRW_READ)
299 page_debug_setup(r->addr, r->len, r->offset,
302 page_debug_setup(r->addr, r->len,
307 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
308 atomic_read(&obd->u.echo.eo_prep));
313 /* It is possible that we would rather handle errors by allow
314 * any already-set-up pages to complete, rather than tearing them
315 * all down again. I believe that this is what the in-kernel
316 * prep/commit operations do.
318 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
321 __free_pages(r->page, 0);
322 atomic_dec(&obd->u.echo.eo_prep);
324 obd_kmap_put(niocount);
325 memset(res, 0, sizeof(*res) * niocount);
330 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
331 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
334 struct obd_device *obd;
335 struct niobuf_local *r = res;
340 obd = class_conn2obd(conn);
342 CERROR("invalid client "LPX64"\n", conn->addr);
346 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
347 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
350 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
354 if (niocount && !r) {
355 CERROR("NULL res niobuf with niocount %d\n", niocount);
359 LASSERT(desc_private == (void *)DESC_PRIV);
361 for (i = 0; i < objcount; i++, obj++) {
362 int verify = obj->ioo_id != 0;
365 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
366 struct page *page = r->page;
369 if (!page || !(addr = page_address(page)) ||
370 !kern_addr_valid(addr)) {
372 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
373 obj->ioo_id, page, j, obj->ioo_bufcnt);
374 GOTO(commitrw_cleanup, rc = -EFAULT);
377 atomic_inc(&echo_page_rws);
379 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
380 r->page, addr, r->offset);
383 page_debug_check("echo", addr, r->len,
384 r->offset, obj->ioo_id);
388 __free_pages(page, 0);
389 atomic_dec(&obd->u.echo.eo_prep);
392 CDEBUG(D_PAGE, "%d pages remain after commit\n",
393 atomic_read(&obd->u.echo.eo_prep));
397 CERROR("cleaning up %ld pages (%d obdos)\n",
398 niocount - (long)(r - res) - 1, objcount);
399 while (++r < res + niocount) {
400 struct page *page = r->page;
404 __free_pages(page, 0);
405 atomic_dec(&obd->u.echo.eo_prep);
410 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
414 obddev->obd_namespace =
415 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
416 if (obddev->obd_namespace == NULL) {
420 spin_lock_init(&obddev->u.echo.eo_lock);
421 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
426 static int echo_cleanup(struct obd_device *obddev)
430 ldlm_namespace_free(obddev->obd_namespace);
431 CERROR("%d prep/commitrw pages leaked\n",
432 atomic_read(&obddev->u.echo.eo_prep));
437 int echo_attach(struct obd_device *dev, obd_count len, void *data)
439 return lprocfs_reg_obd(dev, status_var_nm_1, dev);
442 int echo_detach(struct obd_device *dev)
444 return lprocfs_dereg_obd(dev);
448 struct obd_ops echo_obd_ops = {
449 o_attach: echo_attach,
450 o_detach: echo_detach,
451 o_connect: echo_connect,
452 o_disconnect: echo_disconnect,
453 o_create: echo_create,
454 o_destroy: echo_destroy,
457 o_getattr: echo_getattr,
458 o_setattr: echo_setattr,
459 o_preprw: echo_preprw,
460 o_commitrw: echo_commitrw,
462 o_cleanup: echo_cleanup
465 static int __init obdecho_init(void)
470 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
471 " info@clusterfs.com\n");
474 rc = class_register_type(&echo_obd_ops, status_class_var,
475 OBD_ECHO_DEVICENAME);
481 static void __exit obdecho_exit(void)
485 class_unregister_type(OBD_ECHO_DEVICENAME);
488 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
489 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
490 MODULE_LICENSE("GPL");
492 module_init(obdecho_init);
493 module_exit(obdecho_exit);