1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001, 2002 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Andreas Dilger <adilger@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define OBDECHO_VERSION "1.0"
28 #include <linux/version.h>
29 #include <linux/module.h>
31 #include <linux/highmem.h>
33 #include <linux/stat.h>
34 #include <linux/sched.h>
35 #include <linux/smp_lock.h>
36 #include <linux/ext2_fs.h>
37 #include <linux/quotaops.h>
38 #include <linux/proc_fs.h>
39 #include <linux/init.h>
40 #include <asm/unistd.h>
42 #define DEBUG_SUBSYSTEM S_ECHO
44 #include <linux/obd_support.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_echo.h>
47 #include <linux/lustre_debug.h>
48 #include <linux/lustre_dlm.h>
49 #include <linux/lprocfs_status.h>
51 static atomic_t echo_page_rws;
52 static atomic_t echo_getattrs;
54 #define ECHO_PROC_STAT "sys/obdecho"
55 #define ECHO_INIT_OBJID 0x1000000000000000ULL
57 extern struct lprocfs_vars status_var_nm_1[];
58 extern struct lprocfs_vars status_class_var[];
60 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
63 long long attrs = atomic_read(&echo_getattrs);
64 long long pages = atomic_read(&echo_page_rws);
71 len = sprintf(page, "%Ld %Ld\n", attrs, pages);
77 int echo_proc_write(struct file *file, const char *ubuffer,
78 unsigned long count, void *data)
80 /* Ignore what we've been asked to write, and just zero the counters */
81 atomic_set (&echo_page_rws, 0);
82 atomic_set (&echo_getattrs, 0);
87 void echo_proc_init(void)
89 struct proc_dir_entry *entry;
91 entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
94 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
99 entry->read_proc = echo_proc_read;
100 entry->write_proc = echo_proc_write;
103 void echo_proc_fini(void)
105 remove_proc_entry(ECHO_PROC_STAT, 0);
108 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
109 obd_uuid_t cluuid, struct recovd_obd *recovd,
110 ptlrpc_recovery_cb_t recover)
115 rc = class_connect(conn, obd, cluuid);
123 static int echo_disconnect(struct lustre_handle *conn)
127 rc = class_disconnect(conn);
134 static __u64 echo_next_id(struct obd_device *obddev)
138 spin_lock(&obddev->u.echo.eo_lock);
139 id = ++obddev->u.echo.eo_lastino;
140 spin_unlock(&obddev->u.echo.eo_lock);
145 int echo_create(struct lustre_handle *conn, struct obdo *oa,
146 struct lov_stripe_md **ea)
148 struct obd_device *obd = class_conn2obd(conn);
151 CERROR("invalid client %Lx\n", conn->addr);
155 if (!(oa->o_mode && S_IFMT)) {
156 CERROR("filter obd: no type!\n");
160 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
161 CERROR("invalid o_valid %08x\n", oa->o_valid);
165 oa->o_id = echo_next_id(obd);
166 oa->o_valid = OBD_MD_FLID;
167 atomic_inc(&obd->u.echo.eo_create);
172 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
173 struct lov_stripe_md *ea)
175 struct obd_device *obd = class_conn2obd(conn);
178 CERROR("invalid client "LPX64"\n", conn->addr);
182 if (!(oa->o_valid & OBD_MD_FLID)) {
183 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
187 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
188 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
192 atomic_inc(&obd->u.echo.eo_destroy);
197 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
198 struct lov_stripe_md *md)
203 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
204 struct lov_stripe_md *md)
209 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
210 struct lov_stripe_md *md)
212 struct obd_device *obd = class_conn2obd(conn);
213 obd_id id = oa->o_id;
216 CERROR("invalid client "LPX64"\n", conn->addr);
220 if (!(oa->o_valid & OBD_MD_FLID)) {
221 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
225 memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
227 oa->o_valid |= OBD_MD_FLID;
229 atomic_inc(&echo_getattrs);
234 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
235 struct lov_stripe_md *md)
237 struct obd_device *obd = class_conn2obd(conn);
240 CERROR("invalid client "LPX64"\n", conn->addr);
244 if (!(oa->o_valid & OBD_MD_FLID)) {
245 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
249 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
251 atomic_inc(&obd->u.echo.eo_setattr);
256 /* This allows us to verify that desc_private is passed unmolested */
257 #define DESC_PRIV 0x10293847
259 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
260 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
261 struct niobuf_local *res, void **desc_private)
263 struct obd_device *obd;
264 struct niobuf_local *r = res;
270 obd = class_conn2obd(conn);
272 CERROR("invalid client "LPX64"\n", conn->addr);
276 memset(res, 0, sizeof(*res) * niocount);
278 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
279 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
281 *desc_private = (void *)DESC_PRIV;
283 obd_kmap_get(niocount, 1);
285 for (i = 0; i < objcount; i++, obj++) {
286 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
287 int verify = obj->ioo_id != 0;
290 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
291 r->page = alloc_pages(gfp_mask, 0);
293 CERROR("can't get page %d/%d for id "LPU64"\n",
294 j, obj->ioo_bufcnt, obj->ioo_id);
295 GOTO(preprw_cleanup, rc = -ENOMEM);
297 atomic_inc(&obd->u.echo.eo_prep);
299 r->offset = nb->offset;
300 r->addr = kmap(r->page);
303 CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
304 r->page, r->addr, r->offset);
306 if (verify && cmd == OBD_BRW_READ)
307 page_debug_setup(r->addr, r->len, r->offset,
310 page_debug_setup(r->addr, r->len,
315 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
316 atomic_read(&obd->u.echo.eo_prep));
321 /* It is possible that we would rather handle errors by allow
322 * any already-set-up pages to complete, rather than tearing them
323 * all down again. I believe that this is what the in-kernel
324 * prep/commit operations do.
326 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
329 __free_pages(r->page, 0);
330 atomic_dec(&obd->u.echo.eo_prep);
332 obd_kmap_put(niocount);
333 memset(res, 0, sizeof(*res) * niocount);
338 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
339 struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
342 struct obd_device *obd;
343 struct niobuf_local *r = res;
348 obd = class_conn2obd(conn);
350 CERROR("invalid client "LPX64"\n", conn->addr);
354 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
355 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
358 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
362 if (niocount && !r) {
363 CERROR("NULL res niobuf with niocount %d\n", niocount);
367 LASSERT(desc_private == (void *)DESC_PRIV);
369 for (i = 0; i < objcount; i++, obj++) {
370 int verify = obj->ioo_id != 0;
373 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
374 struct page *page = r->page;
377 if (!page || !(addr = page_address(page)) ||
378 !kern_addr_valid(addr)) {
380 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
381 obj->ioo_id, page, j, obj->ioo_bufcnt);
382 GOTO(commitrw_cleanup, rc = -EFAULT);
385 atomic_inc(&echo_page_rws);
387 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
388 r->page, addr, r->offset);
391 page_debug_check("echo", addr, r->len,
392 r->offset, obj->ioo_id);
396 __free_pages(page, 0);
397 atomic_dec(&obd->u.echo.eo_prep);
400 CDEBUG(D_PAGE, "%d pages remain after commit\n",
401 atomic_read(&obd->u.echo.eo_prep));
405 CERROR("cleaning up %ld pages (%d obdos)\n",
406 niocount - (long)(r - res) - 1, objcount);
407 while (++r < res + niocount) {
408 struct page *page = r->page;
412 __free_pages(page, 0);
413 atomic_dec(&obd->u.echo.eo_prep);
418 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
422 obddev->obd_namespace =
423 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
424 if (obddev->obd_namespace == NULL) {
428 spin_lock_init(&obddev->u.echo.eo_lock);
429 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
434 static int echo_cleanup(struct obd_device *obddev)
438 ldlm_namespace_free(obddev->obd_namespace);
439 CERROR("%d prep/commitrw pages leaked\n",
440 atomic_read(&obddev->u.echo.eo_prep));
445 int echo_attach(struct obd_device *dev, obd_count len, void *data)
447 return lprocfs_reg_obd(dev, status_var_nm_1, dev);
450 int echo_detach(struct obd_device *dev)
452 return lprocfs_dereg_obd(dev);
455 static struct obd_ops echo_obd_ops = {
456 o_attach: echo_attach,
457 o_detach: echo_detach,
458 o_connect: echo_connect,
459 o_disconnect: echo_disconnect,
460 o_create: echo_create,
461 o_destroy: echo_destroy,
464 o_getattr: echo_getattr,
465 o_setattr: echo_setattr,
466 o_preprw: echo_preprw,
467 o_commitrw: echo_commitrw,
469 o_cleanup: echo_cleanup
472 extern int echo_client_init(void);
473 extern void echo_client_cleanup(void);
475 static int __init obdecho_init(void)
479 printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
480 " info@clusterfs.com\n");
483 rc = class_register_type(&echo_obd_ops, status_class_var,
484 OBD_ECHO_DEVICENAME);
488 rc = echo_client_init();
490 class_unregister_type(OBD_ECHO_DEVICENAME);
495 static void __exit obdecho_exit(void)
498 echo_client_cleanup();
499 class_unregister_type(OBD_ECHO_DEVICENAME);
502 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
503 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
504 MODULE_LICENSE("GPL");
506 module_init(obdecho_init);
507 module_exit(obdecho_exit);