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_SHIFT)
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_env *env,
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 LASSERT (exp != NULL);
64 ldlm_cancel_locks_for_export(exp);
66 /* complete all outstanding replies */
67 spin_lock(&exp->exp_lock);
68 while (!list_empty(&exp->exp_outstanding_replies)) {
69 struct ptlrpc_reply_state *rs =
70 list_entry(exp->exp_outstanding_replies.next,
71 struct ptlrpc_reply_state, rs_exp_list);
72 struct ptlrpc_service *svc = rs->rs_service;
74 spin_lock(&svc->srv_lock);
75 list_del_init(&rs->rs_exp_list);
76 ptlrpc_schedule_difficult_reply(rs);
77 spin_unlock(&svc->srv_lock);
79 spin_unlock(&exp->exp_lock);
81 return class_disconnect(exp);
84 static int echo_destroy_export(struct obd_export *exp)
88 target_destroy_export(exp);
93 static __u64 echo_next_id(struct obd_device *obddev)
97 spin_lock(&obddev->u.echo.eo_lock);
98 id = ++obddev->u.echo.eo_lastino;
99 spin_unlock(&obddev->u.echo.eo_lock);
104 int echo_create(struct obd_export *exp, struct obdo *oa,
105 struct lov_stripe_md **ea, struct obd_trans_info *oti)
107 struct obd_device *obd = class_exp2obd(exp);
110 CERROR("invalid client cookie "LPX64"\n",
111 exp->exp_handle.h_cookie);
115 if (!(oa->o_mode && S_IFMT)) {
116 CERROR("echo obd: no type!\n");
120 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
121 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
125 oa->o_id = echo_next_id(obd);
126 oa->o_valid = OBD_MD_FLID;
131 int echo_destroy(struct obd_export *exp, struct obdo *oa,
132 struct lov_stripe_md *ea, struct obd_trans_info *oti,
133 struct obd_export *md_exp)
135 struct obd_device *obd = class_exp2obd(exp);
139 CERROR("invalid client cookie "LPX64"\n",
140 exp->exp_handle.h_cookie);
144 if (!(oa->o_valid & OBD_MD_FLID)) {
145 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
149 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
150 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
157 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
159 struct obd_device *obd = class_exp2obd(exp);
160 obd_id id = oinfo->oi_oa->o_id;
164 CERROR("invalid client cookie "LPX64"\n",
165 exp->exp_handle.h_cookie);
169 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
170 CERROR("obdo missing FLID valid flag: "LPX64"\n",
171 oinfo->oi_oa->o_valid);
175 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
176 oinfo->oi_oa->o_id = id;
181 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
182 struct obd_trans_info *oti)
184 struct obd_device *obd = class_exp2obd(exp);
188 CERROR("invalid client cookie "LPX64"\n",
189 exp->exp_handle.h_cookie);
193 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
194 CERROR("obdo missing FLID valid flag: "LPX64"\n",
195 oinfo->oi_oa->o_valid);
199 memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
201 if (oinfo->oi_oa->o_id & 4) {
202 /* Save lock to force ACKed reply */
203 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
204 oti->oti_ack_locks[0].mode = LCK_NL;
205 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
212 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
213 __u64 offset, int len)
215 int page_offset = offset & ~CFS_PAGE_MASK;
216 char *addr = ((char *)cfs_kmap(page)) + page_offset;
218 if (len % OBD_ECHO_BLOCK_SIZE != 0)
219 CERROR("Unexpected block size %d\n", len);
222 if (rw & OBD_BRW_READ)
223 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
226 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
227 0xecc0ecc0ecc0ecc0ULL,
228 0xecc0ecc0ecc0ecc0ULL);
230 addr += OBD_ECHO_BLOCK_SIZE;
231 offset += OBD_ECHO_BLOCK_SIZE;
232 len -= OBD_ECHO_BLOCK_SIZE;
239 echo_page_debug_check(cfs_page_t *page, obd_id id,
240 __u64 offset, int len)
242 int page_offset = offset & ~CFS_PAGE_MASK;
243 char *addr = ((char *)cfs_kmap(page)) + page_offset;
247 if (len % OBD_ECHO_BLOCK_SIZE != 0)
248 CERROR("Unexpected block size %d\n", len);
251 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
254 if (rc2 != 0 && rc == 0)
257 addr += OBD_ECHO_BLOCK_SIZE;
258 offset += OBD_ECHO_BLOCK_SIZE;
259 len -= OBD_ECHO_BLOCK_SIZE;
267 /* This allows us to verify that desc_private is passed unmolested */
268 #define DESC_PRIV 0x10293847
270 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
271 int objcount, struct obd_ioobj *obj, int niocount,
272 struct niobuf_remote *nb, struct niobuf_local *res,
273 struct obd_trans_info *oti, struct lustre_capa *unused)
275 struct obd_device *obd;
276 struct niobuf_local *r = res;
282 obd = export->exp_obd;
286 /* Temp fix to stop falling foul of osc_announce_cached() */
287 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
289 memset(res, 0, sizeof(*res) * niocount);
291 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
292 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
295 oti->oti_handle = (void *)DESC_PRIV;
297 for (i = 0; i < objcount; i++, obj++) {
298 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
299 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
300 int debug_setup = (!ispersistent &&
301 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
302 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
305 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
308 (nb->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
309 r->page = echo_persistent_pages[nb->offset >>
311 /* Take extra ref so __free_pages() can be called OK */
312 cfs_get_page (r->page);
314 r->page = cfs_alloc_page(gfp_mask);
315 if (r->page == NULL) {
316 CERROR("can't get page %u/%u for id "
318 j, obj->ioo_bufcnt, obj->ioo_id);
319 GOTO(preprw_cleanup, rc = -ENOMEM);
323 tot_bytes += nb->len;
325 atomic_inc(&obd->u.echo.eo_prep);
327 r->offset = nb->offset;
329 LASSERT((r->offset & ~CFS_PAGE_MASK) + r->len <= CFS_PAGE_SIZE);
331 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
332 r->page, r->offset, r->len);
334 if (cmd & OBD_BRW_READ)
338 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
342 if (cmd & OBD_BRW_READ)
343 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
346 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
349 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
350 atomic_read(&obd->u.echo.eo_prep));
355 /* It is possible that we would rather handle errors by allow
356 * any already-set-up pages to complete, rather than tearing them
357 * all down again. I believe that this is what the in-kernel
358 * prep/commit operations do.
360 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
363 /* NB if this is a persistent page, __free_pages will just
364 * lose the extra ref gained above */
365 cfs_free_page(r->page);
366 atomic_dec(&obd->u.echo.eo_prep);
368 memset(res, 0, sizeof(*res) * niocount);
373 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
374 int objcount, struct obd_ioobj *obj, int niocount,
375 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
377 struct obd_device *obd;
378 struct niobuf_local *r = res;
382 obd = export->exp_obd;
387 GOTO(commitrw_cleanup, rc);
389 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
390 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
393 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
397 if (niocount && !r) {
398 CERROR("NULL res niobuf with niocount %d\n", niocount);
402 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
404 for (i = 0; i < objcount; i++, obj++) {
405 int verify = (rc == 0 &&
406 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
407 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
408 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
411 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
412 cfs_page_t *page = r->page;
416 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
417 obj->ioo_id, page, j, obj->ioo_bufcnt);
418 GOTO(commitrw_cleanup, rc = -EFAULT);
421 addr = cfs_kmap(page);
423 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
424 r->page, addr, r->offset);
427 vrc = echo_page_debug_check(page, obj->ioo_id,
429 /* check all the pages always */
430 if (vrc != 0 && rc == 0)
435 /* NB see comment above regarding persistent pages */
437 atomic_dec(&obd->u.echo.eo_prep);
440 CDEBUG(D_PAGE, "%d pages remain after commit\n",
441 atomic_read(&obd->u.echo.eo_prep));
445 CERROR("cleaning up %ld pages (%d obdos)\n",
446 niocount - (long)(r - res) - 1, objcount);
447 while (++r < res + niocount) {
448 cfs_page_t *page = r->page;
450 /* NB see comment above regarding persistent pages */
452 atomic_dec(&obd->u.echo.eo_prep);
457 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
459 struct lprocfs_static_vars lvars;
462 struct ldlm_res_id res_id = {.name = {1}};
465 spin_lock_init(&obd->u.echo.eo_lock);
466 obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
468 obd->obd_namespace = ldlm_namespace_new("echo-tgt",
469 LDLM_NAMESPACE_SERVER,
470 LDLM_NAMESPACE_MODEST);
471 if (obd->obd_namespace == NULL) {
476 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
477 NULL, LCK_NL, &lock_flags, NULL,
478 ldlm_completion_ast, NULL, NULL,
479 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_obd_cleanup(obd);
504 lprocfs_free_obd_stats(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);