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/ext2_fs.h>
37 #include <linux/quotaops.h>
38 #include <linux/proc_fs.h>
39 #include <linux/init.h>
40 #include <asm/unistd.h>
42 #define DEBUG_SUBSYSTEM S_ECHO
44 #include <linux/obd_support.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_echo.h>
47 #include <linux/lustre_debug.h>
48 #include <linux/lustre_dlm.h>
49 #include <linux/lprocfs_status.h>
51 #define ECHO_INIT_OBJID 0x1000000000000000ULL
52 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
54 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE/PAGE_SIZE)
55 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
58 LPROC_ECHO_READ_BYTES = 1,
59 LPROC_ECHO_WRITE_BYTES = 2,
60 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
63 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
64 struct obd_uuid *cluuid)
66 return class_connect(conn, obd, cluuid);
69 static int echo_disconnect(struct obd_export *exp, int flags)
71 unsigned long irqflags;
73 LASSERT (exp != NULL);
75 ldlm_cancel_locks_for_export(exp);
77 spin_lock_irqsave(&exp->exp_lock, irqflags);
78 exp->exp_flags = flags;
79 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
81 return class_disconnect(exp, flags);
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 %08x\n", oa->o_valid);
125 oa->o_id = echo_next_id(obd);
126 oa->o_valid = OBD_MD_FLID;
127 atomic_inc(&obd->u.echo.eo_create);
132 int echo_destroy(struct obd_export *exp, struct obdo *oa,
133 struct lov_stripe_md *ea, struct obd_trans_info *oti)
135 struct obd_device *obd = class_exp2obd(exp);
138 CERROR("invalid client cookie "LPX64"\n",
139 exp->exp_handle.h_cookie);
143 if (!(oa->o_valid & OBD_MD_FLID)) {
144 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
148 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
149 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
153 atomic_inc(&obd->u.echo.eo_destroy);
158 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
159 struct lov_stripe_md *md)
161 struct obd_device *obd = class_exp2obd(exp);
162 obd_id id = oa->o_id;
165 CERROR("invalid client cookie "LPX64"\n",
166 exp->exp_handle.h_cookie);
170 if (!(oa->o_valid & OBD_MD_FLID)) {
171 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
175 obdo_cpy_md(oa, &obd->u.echo.oa, oa->o_valid);
181 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
182 struct lov_stripe_md *md, struct obd_trans_info *oti)
184 struct obd_device *obd = class_exp2obd(exp);
187 CERROR("invalid client cookie "LPX64"\n",
188 exp->exp_handle.h_cookie);
192 if (!(oa->o_valid & OBD_MD_FLID)) {
193 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
197 memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
199 atomic_inc(&obd->u.echo.eo_setattr);
205 echo_page_debug_setup(struct page *page, int rw, obd_id id,
206 __u64 offset, int len)
208 int page_offset = offset & (PAGE_SIZE - 1);
209 char *addr = ((char *)kmap(page)) + page_offset;
211 if (len % OBD_ECHO_BLOCK_SIZE != 0)
212 CERROR("Unexpected block size %d\n", len);
215 if (rw & OBD_BRW_READ)
216 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
219 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
220 0xecc0ecc0ecc0ecc0ULL,
221 0xecc0ecc0ecc0ecc0ULL);
223 addr += OBD_ECHO_BLOCK_SIZE;
224 offset += OBD_ECHO_BLOCK_SIZE;
225 len -= OBD_ECHO_BLOCK_SIZE;
232 echo_page_debug_check(struct page *page, obd_id id,
233 __u64 offset, int len)
235 int page_offset = offset & (PAGE_SIZE - 1);
236 char *addr = ((char *)kmap(page)) + page_offset;
240 if (len % OBD_ECHO_BLOCK_SIZE != 0)
241 CERROR("Unexpected block size %d\n", len);
244 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
247 if (rc2 != 0 && rc == 0)
250 addr += OBD_ECHO_BLOCK_SIZE;
251 offset += OBD_ECHO_BLOCK_SIZE;
252 len -= OBD_ECHO_BLOCK_SIZE;
260 /* This allows us to verify that desc_private is passed unmolested */
261 #define DESC_PRIV 0x10293847
263 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
264 int objcount, struct obd_ioobj *obj, int niocount,
265 struct niobuf_remote *nb, struct niobuf_local *res,
266 struct obd_trans_info *oti)
268 struct obd_device *obd;
269 struct niobuf_local *r = res;
275 obd = export->exp_obd;
279 /* Temp fix to stop falling foul of osc_announce_cached() */
280 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
282 memset(res, 0, sizeof(*res) * niocount);
284 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
285 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
288 oti->oti_handle = (void *)DESC_PRIV;
290 for (i = 0; i < objcount; i++, obj++) {
291 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
292 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
295 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
298 (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
299 r->page = echo_persistent_pages[nb->offset >>
301 /* Take extra ref so __free_pages() can be called OK */
304 r->page = alloc_pages(gfp_mask, 0);
305 if (r->page == NULL) {
306 CERROR("can't get page %u/%u for id "
308 j, obj->ioo_bufcnt, obj->ioo_id);
309 GOTO(preprw_cleanup, rc = -ENOMEM);
313 tot_bytes += nb->len;
315 atomic_inc(&obd->u.echo.eo_prep);
317 r->offset = nb->offset;
319 LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
321 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
322 r->page, r->offset, r->len);
324 if (cmd & OBD_BRW_READ)
328 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
332 if (cmd & OBD_BRW_READ)
333 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
336 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
339 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
340 atomic_read(&obd->u.echo.eo_prep));
345 /* It is possible that we would rather handle errors by allow
346 * any already-set-up pages to complete, rather than tearing them
347 * all down again. I believe that this is what the in-kernel
348 * prep/commit operations do.
350 CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
353 /* NB if this is a persistent page, __free_pages will just
354 * lose the extra ref gained above */
355 __free_pages(r->page, 0);
356 atomic_dec(&obd->u.echo.eo_prep);
358 memset(res, 0, sizeof(*res) * niocount);
363 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
364 int objcount, struct obd_ioobj *obj, int niocount,
365 struct niobuf_local *res, struct obd_trans_info *oti)
367 struct obd_device *obd;
368 struct niobuf_local *r = res;
369 int i, vrc = 0, rc = 0;
372 obd = export->exp_obd;
376 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
377 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
380 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
384 if (niocount && !r) {
385 CERROR("NULL res niobuf with niocount %d\n", niocount);
389 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
391 for (i = 0; i < objcount; i++, obj++) {
392 int verify = obj->ioo_id != ECHO_PERSISTENT_OBJID;
395 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
396 struct page *page = r->page;
400 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
401 obj->ioo_id, page, j, obj->ioo_bufcnt);
402 GOTO(commitrw_cleanup, rc = -EFAULT);
407 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
408 r->page, addr, r->offset);
411 vrc = echo_page_debug_check(page, obj->ioo_id,
413 /* check all the pages always */
414 if (vrc != 0 && rc == 0)
419 /* NB see comment above regarding persistent pages */
420 __free_pages(page, 0);
421 atomic_dec(&obd->u.echo.eo_prep);
424 CDEBUG(D_PAGE, "%d pages remain after commit\n",
425 atomic_read(&obd->u.echo.eo_prep));
429 CERROR("cleaning up %ld pages (%d obdos)\n",
430 niocount - (long)(r - res) - 1, objcount);
431 while (++r < res + niocount) {
432 struct page *page = r->page;
434 /* NB see comment above regarding persistent pages */
435 __free_pages(page, 0);
436 atomic_dec(&obd->u.echo.eo_prep);
441 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
445 spin_lock_init(&obddev->u.echo.eo_lock);
446 obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
448 obddev->obd_namespace =
449 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
450 if (obddev->obd_namespace == NULL) {
455 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
456 "echo_ldlm_cb_client", &obddev->obd_ldlm_client);
460 static int echo_cleanup(struct obd_device *obddev, int flags)
465 ldlm_namespace_free(obddev->obd_namespace, flags & OBD_OPT_FORCE);
467 leaked = atomic_read(&obddev->u.echo.eo_prep);
469 CERROR("%d prep/commitrw pages leaked\n", leaked);
474 int echo_attach(struct obd_device *obd, obd_count len, void *data)
476 struct lprocfs_static_vars lvars;
479 lprocfs_init_vars(echo, &lvars);
480 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
483 rc = lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST);
487 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
488 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
489 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
490 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
494 int echo_detach(struct obd_device *dev)
496 lprocfs_free_obd_stats(dev);
497 return lprocfs_obd_detach(dev);
500 static struct obd_ops echo_obd_ops = {
501 o_owner: THIS_MODULE,
502 o_attach: echo_attach,
503 o_detach: echo_detach,
504 o_connect: echo_connect,
505 o_disconnect: echo_disconnect,
506 o_destroy_export: echo_destroy_export,
507 o_create: echo_create,
508 o_destroy: echo_destroy,
509 o_getattr: echo_getattr,
510 o_setattr: echo_setattr,
511 o_preprw: echo_preprw,
512 o_commitrw: echo_commitrw,
514 o_cleanup: echo_cleanup
517 extern int echo_client_init(void);
518 extern void echo_client_exit(void);
521 echo_persistent_pages_fini (void)
525 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
526 if (echo_persistent_pages[i] != NULL) {
527 __free_pages (echo_persistent_pages[i], 0);
528 echo_persistent_pages[i] = NULL;
533 echo_persistent_pages_init (void)
538 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
539 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
540 GFP_KERNEL : GFP_HIGHUSER;
542 pg = alloc_pages (gfp_mask, 0);
544 echo_persistent_pages_fini ();
548 memset (kmap (pg), 0, PAGE_SIZE);
551 echo_persistent_pages[i] = pg;
557 static int __init obdecho_init(void)
559 struct lprocfs_static_vars lvars;
562 printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
564 LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
566 lprocfs_init_vars(echo, &lvars);
568 rc = echo_persistent_pages_init ();
572 rc = class_register_type(&echo_obd_ops, lvars.module_vars,
573 OBD_ECHO_DEVICENAME);
577 rc = echo_client_init();
581 class_unregister_type(OBD_ECHO_DEVICENAME);
583 echo_persistent_pages_fini ();
588 static void /*__exit*/ obdecho_exit(void)
591 class_unregister_type(OBD_ECHO_DEVICENAME);
592 echo_persistent_pages_fini ();
595 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
596 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
597 MODULE_LICENSE("GPL");
599 module_init(obdecho_init);
600 module_exit(obdecho_exit);