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);
72 ldlm_cancel_locks_for_export(exp);
74 spin_lock_irqsave(&exp->exp_lock, irqflags);
75 exp->exp_flags = flags;
76 /* complete all outstanding replies */
77 spin_lock_irqsave(&exp->exp_lock, irqflags);
78 while (!list_empty(&exp->exp_outstanding_replies)) {
79 struct ptlrpc_reply_state *rs =
80 list_entry(exp->exp_outstanding_replies.next,
81 struct ptlrpc_reply_state, rs_exp_list);
82 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
84 spin_lock(&svc->srv_lock);
85 list_del_init(&rs->rs_exp_list);
86 ptlrpc_schedule_difficult_reply(rs);
87 spin_unlock(&svc->srv_lock);
89 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
91 return class_disconnect(exp, flags);
94 static int echo_destroy_export(struct obd_export *exp)
98 target_destroy_export(exp);
103 static __u64 echo_next_id(struct obd_device *obddev)
107 spin_lock(&obddev->u.echo.eo_lock);
108 id = ++obddev->u.echo.eo_lastino;
109 spin_unlock(&obddev->u.echo.eo_lock);
114 int echo_create(struct obd_export *exp, struct obdo *oa,
115 struct lov_stripe_md **ea, struct obd_trans_info *oti)
117 struct obd_device *obd = class_exp2obd(exp);
120 CERROR("invalid client cookie "LPX64"\n",
121 exp->exp_handle.h_cookie);
125 if (!(oa->o_mode && S_IFMT)) {
126 CERROR("echo obd: no type!\n");
130 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
131 CERROR("invalid o_valid %08x\n", oa->o_valid);
135 oa->o_id = echo_next_id(obd);
136 oa->o_valid = OBD_MD_FLID;
141 int echo_destroy(struct obd_export *exp, struct obdo *oa,
142 struct lov_stripe_md *ea, struct obd_trans_info *oti)
144 struct obd_device *obd = class_exp2obd(exp);
147 CERROR("invalid client cookie "LPX64"\n",
148 exp->exp_handle.h_cookie);
152 if (!(oa->o_valid & OBD_MD_FLID)) {
153 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
157 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
158 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
166 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
167 struct lov_stripe_md *md)
169 struct obd_device *obd = class_exp2obd(exp);
170 obd_id id = oa->o_id;
173 CERROR("invalid client cookie "LPX64"\n",
174 exp->exp_handle.h_cookie);
178 if (!(oa->o_valid & OBD_MD_FLID)) {
179 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
183 obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
189 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
190 struct lov_stripe_md *md, struct obd_trans_info *oti)
192 struct obd_device *obd = class_exp2obd(exp);
195 CERROR("invalid client cookie "LPX64"\n",
196 exp->exp_handle.h_cookie);
200 if (!(oa->o_valid & OBD_MD_FLID)) {
201 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
205 memcpy(&obd->u.echo.eo_oa, oa, sizeof(*oa));
208 /* Save lock to force ACKed reply */
209 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
210 oti->oti_ack_locks[0].mode = LCK_NL;
211 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
217 echo_page_debug_setup(struct page *page, int rw, obd_id id,
218 __u64 offset, int len)
220 int page_offset = offset & (PAGE_SIZE - 1);
221 char *addr = ((char *)kmap(page)) + page_offset;
223 if (len % OBD_ECHO_BLOCK_SIZE != 0)
224 CERROR("Unexpected block size %d\n", len);
227 if (rw & OBD_BRW_READ)
228 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
231 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
232 0xecc0ecc0ecc0ecc0ULL,
233 0xecc0ecc0ecc0ecc0ULL);
235 addr += OBD_ECHO_BLOCK_SIZE;
236 offset += OBD_ECHO_BLOCK_SIZE;
237 len -= OBD_ECHO_BLOCK_SIZE;
244 echo_page_debug_check(struct page *page, obd_id id,
245 __u64 offset, int len)
247 int page_offset = offset & (PAGE_SIZE - 1);
248 char *addr = ((char *)kmap(page)) + page_offset;
252 if (len % OBD_ECHO_BLOCK_SIZE != 0)
253 CERROR("Unexpected block size %d\n", len);
256 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
259 if (rc2 != 0 && rc == 0)
262 addr += OBD_ECHO_BLOCK_SIZE;
263 offset += OBD_ECHO_BLOCK_SIZE;
264 len -= OBD_ECHO_BLOCK_SIZE;
272 /* This allows us to verify that desc_private is passed unmolested */
273 #define DESC_PRIV 0x10293847
275 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
276 int objcount, struct obd_ioobj *obj, int niocount,
277 struct niobuf_remote *nb, struct niobuf_local *res,
278 struct obd_trans_info *oti)
280 struct obd_device *obd;
281 struct niobuf_local *r = res;
287 obd = export->exp_obd;
291 /* Temp fix to stop falling foul of osc_announce_cached() */
292 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
294 memset(res, 0, sizeof(*res) * niocount);
296 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
297 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
300 oti->oti_handle = (void *)DESC_PRIV;
302 for (i = 0; i < objcount; i++, obj++) {
303 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
304 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
305 int debug_setup = (!ispersistent &&
306 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
307 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
310 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
313 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
314 r->page = echo_persistent_pages[nb->offset >>
316 /* Take extra ref so __free_pages() can be called OK */
319 r->page = alloc_pages(gfp_mask, 0);
320 if (r->page == NULL) {
321 CERROR("can't get page %u/%u for id "
323 j, obj->ioo_bufcnt, obj->ioo_id);
324 GOTO(preprw_cleanup, rc = -ENOMEM);
328 tot_bytes += nb->len;
330 atomic_inc(&obd->u.echo.eo_prep);
332 r->offset = nb->offset;
334 LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
336 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
337 r->page, r->offset, r->len);
339 if (cmd & OBD_BRW_READ)
343 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
347 if (cmd & OBD_BRW_READ)
348 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
351 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
354 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
355 atomic_read(&obd->u.echo.eo_prep));
360 /* It is possible that we would rather handle errors by allow
361 * any already-set-up pages to complete, rather than tearing them
362 * all down again. I believe that this is what the in-kernel
363 * prep/commit operations do.
365 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
368 /* NB if this is a persistent page, __free_pages will just
369 * lose the extra ref gained above */
370 __free_pages(r->page, 0);
371 atomic_dec(&obd->u.echo.eo_prep);
373 memset(res, 0, sizeof(*res) * niocount);
378 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
379 int objcount, struct obd_ioobj *obj, int niocount,
380 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
382 struct obd_device *obd;
383 struct niobuf_local *r = res;
387 obd = export->exp_obd;
392 GOTO(commitrw_cleanup, rc);
394 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
395 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
398 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
402 if (niocount && !r) {
403 CERROR("NULL res niobuf with niocount %d\n", niocount);
407 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
409 for (i = 0; i < objcount; i++, obj++) {
410 int verify = (rc == 0 &&
411 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
412 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
413 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
417 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
418 struct page *page = r->page;
422 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
423 obj->ioo_id, page, j, obj->ioo_bufcnt);
424 GOTO(commitrw_cleanup, rc = -EFAULT);
429 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
430 r->page, addr, r->offset);
433 vrc = echo_page_debug_check(page, obj->ioo_id,
435 /* check all the pages always */
436 if (vrc != 0 && rc == 0)
441 /* NB see comment above regarding persistent pages */
442 __free_pages(page, 0);
443 atomic_dec(&obd->u.echo.eo_prep);
446 CDEBUG(D_PAGE, "%d pages remain after commit\n",
447 atomic_read(&obd->u.echo.eo_prep));
451 CERROR("cleaning up %ld pages (%d obdos)\n",
452 niocount - (long)(r - res) - 1, objcount);
453 while (++r < res + niocount) {
454 struct page *page = r->page;
456 /* NB see comment above regarding persistent pages */
457 __free_pages(page, 0);
458 atomic_dec(&obd->u.echo.eo_prep);
463 static int echo_setup(struct obd_device *obd, obd_count len, void *buf)
467 struct ldlm_res_id res_id = {.name = {1}};
471 spin_lock_init(&obd->u.echo.eo_lock);
472 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
474 obd->obd_namespace = ldlm_namespace_new("echo-tgt",
475 LDLM_NAMESPACE_SERVER);
476 if (obd->obd_namespace == NULL) {
480 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
481 LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
482 NULL, ldlm_completion_ast, NULL, NULL,
483 NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
484 LASSERT (rc == ELDLM_OK);
488 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
489 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
493 static int echo_cleanup(struct obd_device *obd, int flags)
498 ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
500 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
501 * happened before calling ldlm_namespace_free() */
502 set_current_state (TASK_UNINTERRUPTIBLE);
503 schedule_timeout (HZ);
505 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
507 leaked = atomic_read(&obd->u.echo.eo_prep);
509 CERROR("%d prep/commitrw pages leaked\n", leaked);
514 int echo_attach(struct obd_device *obd, obd_count len, void *data)
516 struct lprocfs_static_vars lvars;
519 lprocfs_init_vars(echo, &lvars);
520 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
523 rc = lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST);
527 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
528 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
529 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
530 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
534 int echo_detach(struct obd_device *dev)
536 lprocfs_free_obd_stats(dev);
537 return lprocfs_obd_detach(dev);
540 static struct obd_ops echo_obd_ops = {
541 .o_owner = THIS_MODULE,
542 .o_attach = echo_attach,
543 .o_detach = echo_detach,
544 .o_connect = echo_connect,
545 .o_disconnect = echo_disconnect,
546 .o_destroy_export = echo_destroy_export,
547 .o_create = echo_create,
548 .o_destroy = echo_destroy,
549 .o_getattr = echo_getattr,
550 .o_setattr = echo_setattr,
551 .o_preprw = echo_preprw,
552 .o_commitrw = echo_commitrw,
553 .o_setup = echo_setup,
554 .o_cleanup = echo_cleanup
557 extern int echo_client_init(void);
558 extern void echo_client_exit(void);
561 echo_persistent_pages_fini (void)
565 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
566 if (echo_persistent_pages[i] != NULL) {
567 __free_pages (echo_persistent_pages[i], 0);
568 echo_persistent_pages[i] = NULL;
573 echo_persistent_pages_init (void)
578 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
579 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
580 GFP_KERNEL : GFP_HIGHUSER;
582 pg = alloc_pages (gfp_mask, 0);
584 echo_persistent_pages_fini ();
588 memset (kmap (pg), 0, PAGE_SIZE);
591 echo_persistent_pages[i] = pg;
597 static int __init obdecho_init(void)
599 struct lprocfs_static_vars lvars;
602 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
604 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
606 lprocfs_init_vars(echo, &lvars);
608 rc = echo_persistent_pages_init ();
612 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
613 OBD_ECHO_DEVICENAME);
617 rc = echo_client_init();
621 class_unregister_type(OBD_ECHO_DEVICENAME);
623 echo_persistent_pages_fini ();
628 static void /*__exit*/ obdecho_exit(void)
631 class_unregister_type(OBD_ECHO_DEVICENAME);
632 echo_persistent_pages_fini ();
635 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
636 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
637 MODULE_LICENSE("GPL");
639 module_init(obdecho_init);
640 module_exit(obdecho_exit);