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 the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
28 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_ECHO
33 #include <obd_support.h>
34 #include <obd_class.h>
36 #include <lustre_debug.h>
37 #include <lustre_dlm.h>
38 #include <lprocfs_status.h>
40 #define ECHO_INIT_OBJID 0x1000000000000000ULL
41 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
43 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE/CFS_PAGE_SIZE)
44 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
47 LPROC_ECHO_READ_BYTES = 1,
48 LPROC_ECHO_WRITE_BYTES = 2,
49 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
52 static int echo_connect(const struct lu_context *ctx,
53 struct lustre_handle *conn, struct obd_device *obd,
54 struct obd_uuid *cluuid, struct obd_connect_data *data)
56 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
57 return class_connect(conn, obd, cluuid);
60 static int echo_disconnect(struct obd_export *exp)
62 unsigned long irqflags;
64 LASSERT (exp != NULL);
66 ldlm_cancel_locks_for_export(exp);
68 /* complete all outstanding replies */
69 spin_lock_irqsave(&exp->exp_lock, irqflags);
70 while (!list_empty(&exp->exp_outstanding_replies)) {
71 struct ptlrpc_reply_state *rs =
72 list_entry(exp->exp_outstanding_replies.next,
73 struct ptlrpc_reply_state, rs_exp_list);
74 struct ptlrpc_service *svc = rs->rs_service;
76 spin_lock(&svc->srv_lock);
77 list_del_init(&rs->rs_exp_list);
78 ptlrpc_schedule_difficult_reply(rs);
79 spin_unlock(&svc->srv_lock);
81 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
83 return class_disconnect(exp);
86 static int echo_destroy_export(struct obd_export *exp)
90 target_destroy_export(exp);
95 static __u64 echo_next_id(struct obd_device *obddev)
99 spin_lock(&obddev->u.echo.eo_lock);
100 id = ++obddev->u.echo.eo_lastino;
101 spin_unlock(&obddev->u.echo.eo_lock);
106 int echo_create(struct obd_export *exp, struct obdo *oa,
107 struct lov_stripe_md **ea, struct obd_trans_info *oti)
109 struct obd_device *obd = class_exp2obd(exp);
112 CERROR("invalid client cookie "LPX64"\n",
113 exp->exp_handle.h_cookie);
117 if (!(oa->o_mode && S_IFMT)) {
118 CERROR("echo obd: no type!\n");
122 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
123 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
127 oa->o_id = echo_next_id(obd);
128 oa->o_valid = OBD_MD_FLID;
133 int echo_destroy(struct obd_export *exp, struct obdo *oa,
134 struct lov_stripe_md *ea, struct obd_trans_info *oti,
135 struct obd_export *md_exp)
137 struct obd_device *obd = class_exp2obd(exp);
141 CERROR("invalid client cookie "LPX64"\n",
142 exp->exp_handle.h_cookie);
146 if (!(oa->o_valid & OBD_MD_FLID)) {
147 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
151 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
152 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
159 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
160 struct lov_stripe_md *md)
162 struct obd_device *obd = class_exp2obd(exp);
163 obd_id id = oa->o_id;
167 CERROR("invalid client cookie "LPX64"\n",
168 exp->exp_handle.h_cookie);
172 if (!(oa->o_valid & OBD_MD_FLID)) {
173 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
177 obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
183 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
184 struct lov_stripe_md *md, struct obd_trans_info *oti)
186 struct obd_device *obd = class_exp2obd(exp);
190 CERROR("invalid client cookie "LPX64"\n",
191 exp->exp_handle.h_cookie);
195 if (!(oa->o_valid & OBD_MD_FLID)) {
196 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
200 memcpy(&obd->u.echo.eo_oa, oa, sizeof(*oa));
203 /* Save lock to force ACKed reply */
204 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
205 oti->oti_ack_locks[0].mode = LCK_NL;
206 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
213 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
214 __u64 offset, int len)
216 int page_offset = offset & (PAGE_SIZE - 1);
217 char *addr = ((char *)cfs_kmap(page)) + page_offset;
219 if (len % OBD_ECHO_BLOCK_SIZE != 0)
220 CERROR("Unexpected block size %d\n", len);
223 if (rw & OBD_BRW_READ)
224 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
227 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
228 0xecc0ecc0ecc0ecc0ULL,
229 0xecc0ecc0ecc0ecc0ULL);
231 addr += OBD_ECHO_BLOCK_SIZE;
232 offset += OBD_ECHO_BLOCK_SIZE;
233 len -= OBD_ECHO_BLOCK_SIZE;
240 echo_page_debug_check(cfs_page_t *page, obd_id id,
241 __u64 offset, int len)
243 int page_offset = offset & (PAGE_SIZE - 1);
244 char *addr = ((char *)cfs_kmap(page)) + page_offset;
248 if (len % OBD_ECHO_BLOCK_SIZE != 0)
249 CERROR("Unexpected block size %d\n", len);
252 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
255 if (rc2 != 0 && rc == 0)
258 addr += OBD_ECHO_BLOCK_SIZE;
259 offset += OBD_ECHO_BLOCK_SIZE;
260 len -= OBD_ECHO_BLOCK_SIZE;
268 /* This allows us to verify that desc_private is passed unmolested */
269 #define DESC_PRIV 0x10293847
271 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
272 int objcount, struct obd_ioobj *obj, int niocount,
273 struct niobuf_remote *nb, struct niobuf_local *res,
274 struct obd_trans_info *oti)
276 struct obd_device *obd;
277 struct niobuf_local *r = res;
283 obd = export->exp_obd;
287 /* Temp fix to stop falling foul of osc_announce_cached() */
288 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
290 memset(res, 0, sizeof(*res) * niocount);
292 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
293 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
296 oti->oti_handle = (void *)DESC_PRIV;
298 for (i = 0; i < objcount; i++, obj++) {
299 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
300 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
301 int debug_setup = (!ispersistent &&
302 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
303 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
306 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
309 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
310 r->page = echo_persistent_pages[nb->offset >>
312 /* Take extra ref so __free_pages() can be called OK */
313 cfs_get_page (r->page);
315 r->page = cfs_alloc_page(gfp_mask);
316 if (r->page == NULL) {
317 CERROR("can't get page %u/%u for id "
319 j, obj->ioo_bufcnt, obj->ioo_id);
320 GOTO(preprw_cleanup, rc = -ENOMEM);
324 tot_bytes += nb->len;
326 atomic_inc(&obd->u.echo.eo_prep);
328 r->offset = nb->offset;
330 LASSERT((r->offset & ~CFS_PAGE_MASK) + r->len <= CFS_PAGE_SIZE);
332 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
333 r->page, r->offset, r->len);
335 if (cmd & OBD_BRW_READ)
339 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
343 if (cmd & OBD_BRW_READ)
344 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
347 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
350 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
351 atomic_read(&obd->u.echo.eo_prep));
356 /* It is possible that we would rather handle errors by allow
357 * any already-set-up pages to complete, rather than tearing them
358 * all down again. I believe that this is what the in-kernel
359 * prep/commit operations do.
361 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
364 /* NB if this is a persistent page, __free_pages will just
365 * lose the extra ref gained above */
366 cfs_free_page(r->page);
367 atomic_dec(&obd->u.echo.eo_prep);
369 memset(res, 0, sizeof(*res) * niocount);
374 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
375 int objcount, struct obd_ioobj *obj, int niocount,
376 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
378 struct obd_device *obd;
379 struct niobuf_local *r = res;
383 obd = export->exp_obd;
388 GOTO(commitrw_cleanup, rc);
390 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
391 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
394 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
398 if (niocount && !r) {
399 CERROR("NULL res niobuf with niocount %d\n", niocount);
403 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
405 for (i = 0; i < objcount; i++, obj++) {
406 int verify = (rc == 0 &&
407 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
408 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
409 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
412 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
413 cfs_page_t *page = r->page;
417 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
418 obj->ioo_id, page, j, obj->ioo_bufcnt);
419 GOTO(commitrw_cleanup, rc = -EFAULT);
422 addr = cfs_kmap(page);
424 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
425 r->page, addr, r->offset);
428 vrc = echo_page_debug_check(page, obj->ioo_id,
430 /* check all the pages always */
431 if (vrc != 0 && rc == 0)
436 /* NB see comment above regarding persistent pages */
438 atomic_dec(&obd->u.echo.eo_prep);
441 CDEBUG(D_PAGE, "%d pages remain after commit\n",
442 atomic_read(&obd->u.echo.eo_prep));
446 CERROR("cleaning up %ld pages (%d obdos)\n",
447 niocount - (long)(r - res) - 1, objcount);
448 while (++r < res + niocount) {
449 cfs_page_t *page = r->page;
451 /* NB see comment above regarding persistent pages */
453 atomic_dec(&obd->u.echo.eo_prep);
458 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
460 struct lprocfs_static_vars lvars;
463 struct ldlm_res_id res_id = {.name = {1}};
466 spin_lock_init(&obd->u.echo.eo_lock);
467 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
469 obd->obd_namespace = ldlm_namespace_new("echo-tgt",
470 LDLM_NAMESPACE_SERVER);
471 if (obd->obd_namespace == NULL) {
476 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
477 LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
478 NULL, ldlm_completion_ast, NULL, NULL,
479 NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
480 LASSERT (rc == ELDLM_OK);
482 lprocfs_init_vars(echo, &lvars);
483 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
484 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
485 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
486 LPROCFS_CNTR_AVGMINMAX,
487 "read_bytes", "bytes");
488 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
489 LPROCFS_CNTR_AVGMINMAX,
490 "write_bytes", "bytes");
493 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
494 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
498 static int echo_cleanup(struct obd_device *obd)
503 lprocfs_free_obd_stats(obd);
504 lprocfs_obd_cleanup(obd);
506 ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
508 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
509 * happened before calling ldlm_namespace_free() */
510 set_current_state (TASK_UNINTERRUPTIBLE);
511 cfs_schedule_timeout (CFS_TASK_UNINT, cfs_time_seconds(1));
513 ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
515 leaked = atomic_read(&obd->u.echo.eo_prep);
517 CERROR("%d prep/commitrw pages leaked\n", leaked);
522 static struct obd_ops echo_obd_ops = {
523 .o_owner = THIS_MODULE,
524 .o_connect = echo_connect,
525 .o_disconnect = echo_disconnect,
526 .o_destroy_export = echo_destroy_export,
527 .o_create = echo_create,
528 .o_destroy = echo_destroy,
529 .o_getattr = echo_getattr,
530 .o_setattr = echo_setattr,
531 .o_preprw = echo_preprw,
532 .o_commitrw = echo_commitrw,
533 .o_setup = echo_setup,
534 .o_cleanup = echo_cleanup
537 extern int echo_client_init(void);
538 extern void echo_client_exit(void);
541 echo_persistent_pages_fini (void)
545 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
546 if (echo_persistent_pages[i] != NULL) {
547 cfs_free_page (echo_persistent_pages[i]);
548 echo_persistent_pages[i] = NULL;
553 echo_persistent_pages_init (void)
558 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
559 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
560 CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
562 pg = cfs_alloc_page (gfp_mask);
564 echo_persistent_pages_fini ();
568 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
571 echo_persistent_pages[i] = pg;
577 static int __init obdecho_init(void)
579 struct lprocfs_static_vars lvars;
583 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
585 LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
587 lprocfs_init_vars(echo, &lvars);
589 rc = echo_persistent_pages_init ();
593 rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
594 LUSTRE_ECHO_NAME, NULL);
598 rc = echo_client_init();
602 class_unregister_type(LUSTRE_ECHO_NAME);
604 echo_persistent_pages_fini ();
609 static void /*__exit*/ obdecho_exit(void)
612 class_unregister_type(LUSTRE_ECHO_NAME);
613 echo_persistent_pages_fini ();
616 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
617 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
618 MODULE_LICENSE("GPL");
620 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);