1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 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.
25 # define EXPORT_SYMTAB
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/proc_fs.h>
37 #include <linux/init.h>
38 #include <asm/unistd.h>
40 #define DEBUG_SUBSYSTEM S_ECHO
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/obd_echo.h>
45 #include <linux/lustre_debug.h>
46 #include <linux/lustre_dlm.h>
47 #include <linux/lprocfs_status.h>
49 #define ECHO_INIT_OBJID 0x1000000000000000ULL
50 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
52 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE/PAGE_SIZE)
53 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
56 LPROC_ECHO_READ_BYTES = 1,
57 LPROC_ECHO_WRITE_BYTES = 2,
58 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
61 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
62 struct obd_uuid *cluuid, unsigned long connect_flags)
64 return class_connect(conn, obd, cluuid);
67 static int echo_disconnect(struct obd_export *exp, int flags)
69 unsigned long irqflags;
71 LASSERT (exp != NULL);
73 ldlm_cancel_locks_for_export(exp);
75 spin_lock_irqsave(&exp->exp_lock, irqflags);
76 exp->exp_flags = flags;
77 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
79 return class_disconnect(exp, flags);
82 static int echo_destroy_export(struct obd_export *exp)
86 target_destroy_export(exp);
91 static __u64 echo_next_id(struct obd_device *obddev)
95 spin_lock(&obddev->u.echo.eo_lock);
96 id = ++obddev->u.echo.eo_lastino;
97 spin_unlock(&obddev->u.echo.eo_lock);
102 int echo_create(struct obd_export *exp, struct obdo *oa,
103 struct lov_stripe_md **ea, struct obd_trans_info *oti)
105 struct obd_device *obd = class_exp2obd(exp);
108 CERROR("invalid client cookie "LPX64"\n",
109 exp->exp_handle.h_cookie);
113 if (!(oa->o_mode && S_IFMT)) {
114 CERROR("echo obd: no type!\n");
118 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
119 CERROR("invalid o_valid %08x\n", oa->o_valid);
123 oa->o_id = echo_next_id(obd);
124 oa->o_valid = OBD_MD_FLID;
125 atomic_inc(&obd->u.echo.eo_create);
130 int echo_destroy(struct obd_export *exp, struct obdo *oa,
131 struct lov_stripe_md *ea, struct obd_trans_info *oti)
133 struct obd_device *obd = class_exp2obd(exp);
136 CERROR("invalid client cookie "LPX64"\n",
137 exp->exp_handle.h_cookie);
141 if (!(oa->o_valid & OBD_MD_FLID)) {
142 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
146 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
147 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
151 atomic_inc(&obd->u.echo.eo_destroy);
156 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
157 struct lov_stripe_md *md)
159 struct obd_device *obd = class_exp2obd(exp);
160 obd_id id = oa->o_id;
163 CERROR("invalid client cookie "LPX64"\n",
164 exp->exp_handle.h_cookie);
168 if (!(oa->o_valid & OBD_MD_FLID)) {
169 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
173 obdo_cpy_md(oa, &obd->u.echo.oa, oa->o_valid);
179 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
180 struct lov_stripe_md *md, struct obd_trans_info *oti)
182 struct obd_device *obd = class_exp2obd(exp);
185 CERROR("invalid client cookie "LPX64"\n",
186 exp->exp_handle.h_cookie);
190 if (!(oa->o_valid & OBD_MD_FLID)) {
191 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
195 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
197 atomic_inc(&obd->u.echo.eo_setattr);
203 echo_page_debug_setup(struct page *page, int rw, obd_id id,
204 __u64 offset, int len)
206 int page_offset = offset & (PAGE_SIZE - 1);
207 char *addr = ((char *)kmap(page)) + page_offset;
209 if (len % OBD_ECHO_BLOCK_SIZE != 0)
210 CERROR("Unexpected block size %d\n", len);
213 if (rw & OBD_BRW_READ)
214 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
217 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
218 0xecc0ecc0ecc0ecc0ULL,
219 0xecc0ecc0ecc0ecc0ULL);
221 addr += OBD_ECHO_BLOCK_SIZE;
222 offset += OBD_ECHO_BLOCK_SIZE;
223 len -= OBD_ECHO_BLOCK_SIZE;
230 echo_page_debug_check(struct page *page, obd_id id,
231 __u64 offset, int len)
233 int page_offset = offset & (PAGE_SIZE - 1);
234 char *addr = ((char *)kmap(page)) + page_offset;
238 if (len % OBD_ECHO_BLOCK_SIZE != 0)
239 CERROR("Unexpected block size %d\n", len);
242 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
245 if (rc2 != 0 && rc == 0)
248 addr += OBD_ECHO_BLOCK_SIZE;
249 offset += OBD_ECHO_BLOCK_SIZE;
250 len -= OBD_ECHO_BLOCK_SIZE;
258 /* This allows us to verify that desc_private is passed unmolested */
259 #define DESC_PRIV 0x10293847
261 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
262 int objcount, struct obd_ioobj *obj, int niocount,
263 struct niobuf_remote *nb, struct niobuf_local *res,
264 struct obd_trans_info *oti)
266 struct obd_device *obd;
267 struct niobuf_local *r = res;
273 obd = export->exp_obd;
277 /* Temp fix to stop falling foul of osc_announce_cached() */
278 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
280 memset(res, 0, sizeof(*res) * niocount);
282 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
283 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
286 oti->oti_handle = (void *)DESC_PRIV;
288 for (i = 0; i < objcount; i++, obj++) {
289 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
290 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
293 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
296 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
297 r->page = echo_persistent_pages[nb->offset >>
299 /* Take extra ref so __free_pages() can be called OK */
302 r->page = alloc_pages(gfp_mask, 0);
303 if (r->page == NULL) {
304 CERROR("can't get page %u/%u for id "
306 j, obj->ioo_bufcnt, obj->ioo_id);
307 GOTO(preprw_cleanup, rc = -ENOMEM);
311 tot_bytes += nb->len;
313 atomic_inc(&obd->u.echo.eo_prep);
315 r->offset = nb->offset;
317 LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
319 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
320 r->page, r->offset, r->len);
322 if (cmd & OBD_BRW_READ)
326 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
330 if (cmd & OBD_BRW_READ)
331 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
334 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
337 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
338 atomic_read(&obd->u.echo.eo_prep));
343 /* It is possible that we would rather handle errors by allow
344 * any already-set-up pages to complete, rather than tearing them
345 * all down again. I believe that this is what the in-kernel
346 * prep/commit operations do.
348 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
351 /* NB if this is a persistent page, __free_pages will just
352 * lose the extra ref gained above */
353 __free_pages(r->page, 0);
354 atomic_dec(&obd->u.echo.eo_prep);
356 memset(res, 0, sizeof(*res) * niocount);
361 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
362 int objcount, struct obd_ioobj *obj, int niocount,
363 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
365 struct obd_device *obd;
366 struct niobuf_local *r = res;
370 obd = export->exp_obd;
375 GOTO(commitrw_cleanup, rc);
377 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
378 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
381 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
385 if (niocount && !r) {
386 CERROR("NULL res niobuf with niocount %d\n", niocount);
390 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
392 for (i = 0; i < objcount; i++, obj++) {
393 int verify = (rc == 0 &&
394 obj->ioo_id != ECHO_PERSISTENT_OBJID);
397 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
398 struct page *page = r->page;
402 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
403 obj->ioo_id, page, j, obj->ioo_bufcnt);
404 GOTO(commitrw_cleanup, rc = -EFAULT);
409 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
410 r->page, addr, r->offset);
413 vrc = echo_page_debug_check(page, obj->ioo_id,
415 /* check all the pages always */
416 if (vrc != 0 && rc == 0)
421 /* NB see comment above regarding persistent pages */
422 __free_pages(page, 0);
423 atomic_dec(&obd->u.echo.eo_prep);
426 CDEBUG(D_PAGE, "%d pages remain after commit\n",
427 atomic_read(&obd->u.echo.eo_prep));
431 CERROR("cleaning up %ld pages (%d obdos)\n",
432 niocount - (long)(r - res) - 1, objcount);
433 while (++r < res + niocount) {
434 struct page *page = r->page;
436 /* NB see comment above regarding persistent pages */
437 __free_pages(page, 0);
438 atomic_dec(&obd->u.echo.eo_prep);
443 static int echo_setup(struct obd_device *obd, obd_count len, void *buf)
447 spin_lock_init(&obd->u.echo.eo_lock);
448 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
450 obd->obd_namespace = ldlm_namespace_new("echo-tgt",
451 LDLM_NAMESPACE_SERVER);
452 if (obd->obd_namespace == NULL) {
457 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
458 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
462 static int echo_cleanup(struct obd_device *obd, int flags)
467 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
469 leaked = atomic_read(&obd->u.echo.eo_prep);
471 CERROR("%d prep/commitrw pages leaked\n", leaked);
476 int echo_attach(struct obd_device *obd, obd_count len, void *data)
478 struct lprocfs_static_vars lvars;
481 lprocfs_init_vars(echo, &lvars);
482 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
485 rc = lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST);
489 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
490 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
491 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
492 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
496 int echo_detach(struct obd_device *dev)
498 lprocfs_free_obd_stats(dev);
499 return lprocfs_obd_detach(dev);
502 static struct obd_ops echo_obd_ops = {
503 .o_owner = THIS_MODULE,
504 .o_attach = echo_attach,
505 .o_detach = echo_detach,
506 .o_connect = echo_connect,
507 .o_disconnect = echo_disconnect,
508 .o_destroy_export = echo_destroy_export,
509 .o_create = echo_create,
510 .o_destroy = echo_destroy,
511 .o_getattr = echo_getattr,
512 .o_setattr = echo_setattr,
513 .o_preprw = echo_preprw,
514 .o_commitrw = echo_commitrw,
515 .o_setup = echo_setup,
516 .o_cleanup = echo_cleanup
519 extern int echo_client_init(void);
520 extern void echo_client_exit(void);
523 echo_persistent_pages_fini (void)
527 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
528 if (echo_persistent_pages[i] != NULL) {
529 __free_pages (echo_persistent_pages[i], 0);
530 echo_persistent_pages[i] = NULL;
535 echo_persistent_pages_init (void)
540 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
541 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
542 GFP_KERNEL : GFP_HIGHUSER;
544 pg = alloc_pages (gfp_mask, 0);
546 echo_persistent_pages_fini ();
550 memset (kmap (pg), 0, PAGE_SIZE);
553 echo_persistent_pages[i] = pg;
559 static int __init obdecho_init(void)
561 struct lprocfs_static_vars lvars;
564 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
566 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
568 lprocfs_init_vars(echo, &lvars);
570 rc = echo_persistent_pages_init ();
574 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
575 OBD_ECHO_DEVICENAME);
579 rc = echo_client_init();
583 class_unregister_type(OBD_ECHO_DEVICENAME);
585 echo_persistent_pages_fini ();
590 static void /*__exit*/ obdecho_exit(void)
593 class_unregister_type(OBD_ECHO_DEVICENAME);
594 echo_persistent_pages_fini ();
597 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
598 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
599 MODULE_LICENSE("GPL");
601 module_init(obdecho_init);
602 module_exit(obdecho_exit);