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,
61 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
62 struct obd_uuid *cluuid, struct obd_connect_data *data,
63 unsigned long connect_flags)
65 return class_connect(conn, obd, cluuid);
68 static int echo_disconnect(struct obd_export *exp, unsigned long flags)
70 unsigned long irqflags;
72 LASSERT (exp != NULL);
74 ldlm_cancel_locks_for_export(exp);
76 spin_lock_irqsave(&exp->exp_lock, irqflags);
77 exp->exp_flags = flags;
78 /* complete all outstanding replies */
79 while (!list_empty(&exp->exp_outstanding_replies)) {
80 struct ptlrpc_reply_state *rs =
81 list_entry(exp->exp_outstanding_replies.next,
82 struct ptlrpc_reply_state, rs_exp_list);
83 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
85 spin_lock(&svc->srv_lock);
86 list_del_init(&rs->rs_exp_list);
87 ptlrpc_schedule_difficult_reply(rs);
88 spin_unlock(&svc->srv_lock);
90 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
92 return class_disconnect(exp, flags);
95 static int echo_destroy_export(struct obd_export *exp)
99 target_destroy_export(exp);
104 static __u64 echo_next_id(struct obd_device *obddev)
108 spin_lock(&obddev->u.echo.eo_lock);
109 id = ++obddev->u.echo.eo_lastino;
110 spin_unlock(&obddev->u.echo.eo_lock);
115 int echo_create(struct obd_export *exp, struct obdo *oa,
116 void *acl, int acl_size,
117 struct lov_stripe_md **ea, struct obd_trans_info *oti)
119 struct obd_device *obd = class_exp2obd(exp);
122 CERROR("invalid client cookie "LPX64"\n",
123 exp->exp_handle.h_cookie);
127 if (!(oa->o_mode && S_IFMT)) {
128 CERROR("echo obd: no type!\n");
132 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
133 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
137 oa->o_id = echo_next_id(obd);
138 oa->o_valid = OBD_MD_FLID;
143 int echo_destroy(struct obd_export *exp, struct obdo *oa,
144 struct lov_stripe_md *ea, struct obd_trans_info *oti)
146 struct obd_device *obd = class_exp2obd(exp);
149 CERROR("invalid client cookie "LPX64"\n",
150 exp->exp_handle.h_cookie);
154 if (!(oa->o_valid & OBD_MD_FLID)) {
155 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
159 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
160 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
168 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
169 struct lov_stripe_md *md)
171 struct obd_device *obd = class_exp2obd(exp);
172 obd_id id = oa->o_id;
175 CERROR("invalid client cookie "LPX64"\n",
176 exp->exp_handle.h_cookie);
180 if (!(oa->o_valid & OBD_MD_FLID)) {
181 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
185 obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
191 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
192 struct lov_stripe_md *md, struct obd_trans_info *oti,
193 struct lustre_capa *capa)
195 struct obd_device *obd = class_exp2obd(exp);
198 CERROR("invalid client cookie "LPX64"\n",
199 exp->exp_handle.h_cookie);
203 if (!(oa->o_valid & OBD_MD_FLID)) {
204 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
208 memcpy(&obd->u.echo.eo_oa, oa, sizeof(*oa));
211 /* Save lock to force ACKed reply */
212 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
213 oti->oti_ack_locks[0].mode = LCK_NL;
214 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
220 echo_page_debug_setup(struct page *page, int rw, obd_id id,
221 __u64 offset, int len)
223 int page_offset = offset & (PAGE_SIZE - 1);
224 char *addr = ((char *)kmap(page)) + page_offset;
226 if (len % OBD_ECHO_BLOCK_SIZE != 0)
227 CERROR("Unexpected block size %d\n", len);
230 if (rw & OBD_BRW_READ)
231 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
234 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
235 0xecc0ecc0ecc0ecc0ULL,
236 0xecc0ecc0ecc0ecc0ULL);
238 addr += OBD_ECHO_BLOCK_SIZE;
239 offset += OBD_ECHO_BLOCK_SIZE;
240 len -= OBD_ECHO_BLOCK_SIZE;
247 echo_page_debug_check(struct page *page, obd_id id,
248 __u64 offset, int len)
250 int page_offset = offset & (PAGE_SIZE - 1);
251 char *addr = ((char *)kmap(page)) + page_offset;
255 if (len % OBD_ECHO_BLOCK_SIZE != 0)
256 CERROR("Unexpected block size %d\n", len);
259 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
262 if (rc2 != 0 && rc == 0)
265 addr += OBD_ECHO_BLOCK_SIZE;
266 offset += OBD_ECHO_BLOCK_SIZE;
267 len -= OBD_ECHO_BLOCK_SIZE;
275 /* This allows us to verify that desc_private is passed unmolested */
276 #define DESC_PRIV 0x10293847
278 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
279 int objcount, struct obd_ioobj *obj, int niocount,
280 struct niobuf_remote *nb, struct niobuf_local *res,
281 struct obd_trans_info *oti, struct lustre_capa *capa)
283 struct obd_device *obd;
284 struct niobuf_local *r = res;
290 obd = export->exp_obd;
294 /* Temp fix to stop falling foul of osc_announce_cached() */
295 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
297 memset(res, 0, sizeof(*res) * niocount);
299 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
300 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
303 oti->oti_handle = (void *)DESC_PRIV;
305 for (i = 0; i < objcount; i++, obj++) {
306 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
307 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
308 int debug_setup = (!ispersistent &&
309 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
310 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
313 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
316 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
317 r->page = echo_persistent_pages[nb->offset >>
319 /* Take extra ref so __free_pages() can be called OK */
322 r->page = alloc_pages(gfp_mask, 0);
323 if (r->page == NULL) {
324 CERROR("can't get page %u/%u for id "
326 j, obj->ioo_bufcnt, obj->ioo_id);
327 GOTO(preprw_cleanup, rc = -ENOMEM);
331 tot_bytes += nb->len;
333 atomic_inc(&obd->u.echo.eo_prep);
335 r->offset = nb->offset;
337 LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
339 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
340 r->page, r->offset, r->len);
342 if (cmd & OBD_BRW_READ)
346 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
350 if (cmd & OBD_BRW_READ)
351 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
354 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
357 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
358 atomic_read(&obd->u.echo.eo_prep));
363 /* It is possible that we would rather handle errors by allow
364 * any already-set-up pages to complete, rather than tearing them
365 * all down again. I believe that this is what the in-kernel
366 * prep/commit operations do.
368 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
371 /* NB if this is a persistent page, __free_pages will just
372 * lose the extra ref gained above */
373 __free_pages(r->page, 0);
374 atomic_dec(&obd->u.echo.eo_prep);
376 memset(res, 0, sizeof(*res) * niocount);
381 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
382 int objcount, struct obd_ioobj *obj, int niocount,
383 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
385 struct obd_device *obd;
386 struct niobuf_local *r = res;
390 obd = export->exp_obd;
395 GOTO(commitrw_cleanup, rc);
397 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
398 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
401 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
405 if (niocount && !r) {
406 CERROR("NULL res niobuf with niocount %d\n", niocount);
410 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
412 for (i = 0; i < objcount; i++, obj++) {
413 int verify = (rc == 0 &&
414 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
415 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
416 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
420 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
421 struct page *page = r->page;
425 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
426 obj->ioo_id, page, j, obj->ioo_bufcnt);
427 GOTO(commitrw_cleanup, rc = -EFAULT);
432 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
433 r->page, addr, r->offset);
436 vrc = echo_page_debug_check(page, obj->ioo_id,
438 /* check all the pages always */
439 if (vrc != 0 && rc == 0)
444 /* NB see comment above regarding persistent pages */
445 __free_pages(page, 0);
446 atomic_dec(&obd->u.echo.eo_prep);
449 CDEBUG(D_PAGE, "%d pages remain after commit\n",
450 atomic_read(&obd->u.echo.eo_prep));
454 CERROR("cleaning up %ld pages (%d obdos)\n",
455 niocount - (long)(r - res) - 1, objcount);
456 while (++r < res + niocount) {
457 struct page *page = r->page;
459 /* NB see comment above regarding persistent pages */
460 __free_pages(page, 0);
461 atomic_dec(&obd->u.echo.eo_prep);
466 static int echo_setup(struct obd_device *obd, obd_count len, void *buf)
470 struct ldlm_res_id res_id = {.name = {1}};
474 spin_lock_init(&obd->u.echo.eo_lock);
475 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
477 obd->obd_namespace = ldlm_namespace_new("echo-tgt",
478 LDLM_NAMESPACE_SERVER);
479 if (obd->obd_namespace == NULL) {
483 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
484 LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
485 NULL, ldlm_completion_ast, NULL, NULL,
486 NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
487 LASSERT (rc == ELDLM_OK);
491 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
492 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
496 static int echo_cleanup(struct obd_device *obd, int flags)
501 ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
503 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
504 * happened before calling ldlm_namespace_free() */
505 set_current_state (TASK_UNINTERRUPTIBLE);
506 schedule_timeout (HZ);
508 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
510 leaked = atomic_read(&obd->u.echo.eo_prep);
512 CERROR("%d prep/commitrw pages leaked\n", leaked);
517 int echo_attach(struct obd_device *obd, obd_count len, void *data)
519 struct lprocfs_static_vars lvars;
522 lprocfs_init_vars(echo, &lvars);
523 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
526 rc = lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST);
530 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
531 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
532 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
533 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
537 int echo_detach(struct obd_device *dev)
539 lprocfs_free_obd_stats(dev);
540 return lprocfs_obd_detach(dev);
543 static struct obd_ops echo_obd_ops = {
544 .o_owner = THIS_MODULE,
545 .o_attach = echo_attach,
546 .o_detach = echo_detach,
547 .o_connect = echo_connect,
548 .o_disconnect = echo_disconnect,
549 .o_destroy_export = echo_destroy_export,
550 .o_create = echo_create,
551 .o_destroy = echo_destroy,
552 .o_getattr = echo_getattr,
553 .o_setattr = echo_setattr,
554 .o_preprw = echo_preprw,
555 .o_commitrw = echo_commitrw,
556 .o_setup = echo_setup,
557 .o_cleanup = echo_cleanup
560 extern int echo_client_init(void);
561 extern void echo_client_exit(void);
564 echo_persistent_pages_fini (void)
568 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
569 if (echo_persistent_pages[i] != NULL) {
570 __free_pages (echo_persistent_pages[i], 0);
571 echo_persistent_pages[i] = NULL;
576 echo_persistent_pages_init (void)
581 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
582 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
583 GFP_KERNEL : GFP_HIGHUSER;
585 pg = alloc_pages (gfp_mask, 0);
587 echo_persistent_pages_fini ();
591 memset (kmap (pg), 0, PAGE_SIZE);
594 echo_persistent_pages[i] = pg;
600 static int __init obdecho_init(void)
602 struct lprocfs_static_vars lvars;
605 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
607 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
609 lprocfs_init_vars(echo, &lvars);
611 rc = echo_persistent_pages_init ();
615 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
616 OBD_ECHO_DEVICENAME);
620 rc = echo_client_init();
624 class_unregister_type(OBD_ECHO_DEVICENAME);
626 echo_persistent_pages_fini ();
631 static void /*__exit*/ obdecho_exit(void)
634 class_unregister_type(OBD_ECHO_DEVICENAME);
635 echo_persistent_pages_fini ();
638 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
639 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
640 MODULE_LICENSE("GPL");
642 module_init(obdecho_init);
643 module_exit(obdecho_exit);