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 #include <linux/version.h>
32 #include <linux/module.h>
34 #include <linux/highmem.h>
36 #include <linux/stat.h>
37 #include <linux/sched.h>
38 #include <linux/smp_lock.h>
39 #include <linux/proc_fs.h>
40 #include <linux/init.h>
41 #include <asm/unistd.h>
43 #define DEBUG_SUBSYSTEM S_ECHO
45 #include <linux/obd_support.h>
46 #include <linux/obd_class.h>
47 #include <linux/obd_echo.h>
48 #include <linux/lustre_debug.h>
49 #include <linux/lustre_dlm.h>
50 #include <linux/lprocfs_status.h>
52 #define ECHO_INIT_OBJID 0x1000000000000000ULL
53 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
55 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE/PAGE_SIZE)
56 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
59 LPROC_ECHO_READ_BYTES = 1,
60 LPROC_ECHO_WRITE_BYTES = 2,
61 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
64 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
65 struct obd_uuid *cluuid, struct obd_connect_data *data)
67 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
68 return class_connect(conn, obd, cluuid);
71 static int echo_disconnect(struct obd_export *exp)
73 unsigned long irqflags;
75 LASSERT (exp != NULL);
77 ldlm_cancel_locks_for_export(exp);
79 /* complete all outstanding replies */
80 spin_lock_irqsave(&exp->exp_lock, irqflags);
81 while (!list_empty(&exp->exp_outstanding_replies)) {
82 struct ptlrpc_reply_state *rs =
83 list_entry(exp->exp_outstanding_replies.next,
84 struct ptlrpc_reply_state, rs_exp_list);
85 struct ptlrpc_service *svc = rs->rs_service;
87 spin_lock(&svc->srv_lock);
88 list_del_init(&rs->rs_exp_list);
89 ptlrpc_schedule_difficult_reply(rs);
90 spin_unlock(&svc->srv_lock);
92 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
94 return class_disconnect(exp);
97 static int echo_destroy_export(struct obd_export *exp)
101 target_destroy_export(exp);
106 static __u64 echo_next_id(struct obd_device *obddev)
110 spin_lock(&obddev->u.echo.eo_lock);
111 id = ++obddev->u.echo.eo_lastino;
112 spin_unlock(&obddev->u.echo.eo_lock);
117 int echo_create(struct obd_export *exp, struct obdo *oa,
118 struct lov_stripe_md **ea, struct obd_trans_info *oti)
120 struct obd_device *obd = class_exp2obd(exp);
123 CERROR("invalid client cookie "LPX64"\n",
124 exp->exp_handle.h_cookie);
128 if (!(oa->o_mode && S_IFMT)) {
129 CERROR("echo obd: no type!\n");
133 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
134 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
138 oa->o_id = echo_next_id(obd);
139 oa->o_valid = OBD_MD_FLID;
144 int echo_destroy(struct obd_export *exp, struct obdo *oa,
145 struct lov_stripe_md *ea, struct obd_trans_info *oti,
146 struct obd_export *md_exp)
148 struct obd_device *obd = class_exp2obd(exp);
151 CERROR("invalid client cookie "LPX64"\n",
152 exp->exp_handle.h_cookie);
156 if (!(oa->o_valid & OBD_MD_FLID)) {
157 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
161 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
162 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
169 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
170 struct lov_stripe_md *md)
172 struct obd_device *obd = class_exp2obd(exp);
173 obd_id id = oa->o_id;
176 CERROR("invalid client cookie "LPX64"\n",
177 exp->exp_handle.h_cookie);
181 if (!(oa->o_valid & OBD_MD_FLID)) {
182 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
186 obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
192 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
193 struct lov_stripe_md *md, struct obd_trans_info *oti)
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;
221 echo_page_debug_setup(struct page *page, int rw, obd_id id,
222 __u64 offset, int len)
224 int page_offset = offset & (PAGE_SIZE - 1);
225 char *addr = ((char *)kmap(page)) + page_offset;
227 if (len % OBD_ECHO_BLOCK_SIZE != 0)
228 CERROR("Unexpected block size %d\n", len);
231 if (rw & OBD_BRW_READ)
232 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
235 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
236 0xecc0ecc0ecc0ecc0ULL,
237 0xecc0ecc0ecc0ecc0ULL);
239 addr += OBD_ECHO_BLOCK_SIZE;
240 offset += OBD_ECHO_BLOCK_SIZE;
241 len -= OBD_ECHO_BLOCK_SIZE;
248 echo_page_debug_check(struct page *page, obd_id id,
249 __u64 offset, int len)
251 int page_offset = offset & (PAGE_SIZE - 1);
252 char *addr = ((char *)kmap(page)) + page_offset;
256 if (len % OBD_ECHO_BLOCK_SIZE != 0)
257 CERROR("Unexpected block size %d\n", len);
260 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
263 if (rc2 != 0 && rc == 0)
266 addr += OBD_ECHO_BLOCK_SIZE;
267 offset += OBD_ECHO_BLOCK_SIZE;
268 len -= OBD_ECHO_BLOCK_SIZE;
276 /* This allows us to verify that desc_private is passed unmolested */
277 #define DESC_PRIV 0x10293847
279 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
280 int objcount, struct obd_ioobj *obj, int niocount,
281 struct niobuf_remote *nb, struct niobuf_local *res,
282 struct obd_trans_info *oti)
284 struct obd_device *obd;
285 struct niobuf_local *r = res;
291 obd = export->exp_obd;
295 /* Temp fix to stop falling foul of osc_announce_cached() */
296 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
298 memset(res, 0, sizeof(*res) * niocount);
300 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
301 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
304 oti->oti_handle = (void *)DESC_PRIV;
306 for (i = 0; i < objcount; i++, obj++) {
307 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
308 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
309 int debug_setup = (!ispersistent &&
310 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
311 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
314 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
317 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
318 r->page = echo_persistent_pages[nb->offset >>
320 /* Take extra ref so __free_pages() can be called OK */
323 r->page = alloc_pages(gfp_mask, 0);
324 if (r->page == NULL) {
325 CERROR("can't get page %u/%u for id "
327 j, obj->ioo_bufcnt, obj->ioo_id);
328 GOTO(preprw_cleanup, rc = -ENOMEM);
332 tot_bytes += nb->len;
334 atomic_inc(&obd->u.echo.eo_prep);
336 r->offset = nb->offset;
338 LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
340 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
341 r->page, r->offset, r->len);
343 if (cmd & OBD_BRW_READ)
347 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
351 if (cmd & OBD_BRW_READ)
352 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
355 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
358 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
359 atomic_read(&obd->u.echo.eo_prep));
364 /* It is possible that we would rather handle errors by allow
365 * any already-set-up pages to complete, rather than tearing them
366 * all down again. I believe that this is what the in-kernel
367 * prep/commit operations do.
369 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
372 /* NB if this is a persistent page, __free_pages will just
373 * lose the extra ref gained above */
374 __free_pages(r->page, 0);
375 atomic_dec(&obd->u.echo.eo_prep);
377 memset(res, 0, sizeof(*res) * niocount);
382 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
383 int objcount, struct obd_ioobj *obj, int niocount,
384 struct niobuf_local *res, struct obd_trans_info *oti, int rc)
386 struct obd_device *obd;
387 struct niobuf_local *r = res;
391 obd = export->exp_obd;
396 GOTO(commitrw_cleanup, rc);
398 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
399 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
402 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
406 if (niocount && !r) {
407 CERROR("NULL res niobuf with niocount %d\n", niocount);
411 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
413 for (i = 0; i < objcount; i++, obj++) {
414 int verify = (rc == 0 &&
415 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
416 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
417 (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)
468 struct lprocfs_static_vars lvars;
471 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) {
484 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
485 LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
486 NULL, ldlm_completion_ast, NULL, NULL,
487 NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
488 LASSERT (rc == ELDLM_OK);
490 lprocfs_init_vars(echo, &lvars);
491 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
492 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
493 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
494 LPROCFS_CNTR_AVGMINMAX,
495 "read_bytes", "bytes");
496 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
497 LPROCFS_CNTR_AVGMINMAX,
498 "write_bytes", "bytes");
501 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
502 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
506 static int echo_cleanup(struct obd_device *obd)
511 lprocfs_free_obd_stats(obd);
512 lprocfs_obd_cleanup(obd);
514 ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
516 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
517 * happened before calling ldlm_namespace_free() */
518 set_current_state (TASK_UNINTERRUPTIBLE);
519 schedule_timeout (HZ);
521 ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
523 leaked = atomic_read(&obd->u.echo.eo_prep);
525 CERROR("%d prep/commitrw pages leaked\n", leaked);
530 static struct obd_ops echo_obd_ops = {
531 .o_owner = THIS_MODULE,
532 .o_connect = echo_connect,
533 .o_disconnect = echo_disconnect,
534 .o_destroy_export = echo_destroy_export,
535 .o_create = echo_create,
536 .o_destroy = echo_destroy,
537 .o_getattr = echo_getattr,
538 .o_setattr = echo_setattr,
539 .o_preprw = echo_preprw,
540 .o_commitrw = echo_commitrw,
541 .o_setup = echo_setup,
542 .o_cleanup = echo_cleanup
545 extern int echo_client_init(void);
546 extern void echo_client_exit(void);
549 echo_persistent_pages_fini (void)
553 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
554 if (echo_persistent_pages[i] != NULL) {
555 __free_pages (echo_persistent_pages[i], 0);
556 echo_persistent_pages[i] = NULL;
561 echo_persistent_pages_init (void)
566 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
567 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
568 GFP_KERNEL : GFP_HIGHUSER;
570 pg = alloc_pages (gfp_mask, 0);
572 echo_persistent_pages_fini ();
576 memset (kmap (pg), 0, PAGE_SIZE);
579 echo_persistent_pages[i] = pg;
585 static int __init obdecho_init(void)
587 struct lprocfs_static_vars lvars;
590 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
592 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
594 lprocfs_init_vars(echo, &lvars);
596 rc = echo_persistent_pages_init ();
600 rc = class_register_type(&echo_obd_ops, lvars.module_vars,
601 OBD_ECHO_DEVICENAME);
605 rc = echo_client_init();
609 class_unregister_type(OBD_ECHO_DEVICENAME);
611 echo_persistent_pages_fini ();
616 static void /*__exit*/ obdecho_exit(void)
619 class_unregister_type(OBD_ECHO_DEVICENAME);
620 echo_persistent_pages_fini ();
623 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
624 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
625 MODULE_LICENSE("GPL");
627 module_init(obdecho_init);
628 module_exit(obdecho_exit);