1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2010, 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/obdecho/echo.c
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
45 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_ECHO
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include <lustre_debug.h>
53 #include <lustre_dlm.h>
54 #include <lprocfs_status.h>
56 #include "echo_internal.h"
58 /* The echo objid needs to be below 2^32, because regular FID numbers are
59 * limited to 2^32 objects in f_oid for the FID_SEQ_ECHO range. b=23335 */
60 #define ECHO_INIT_OID 0x10000000ULL
61 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
63 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
64 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
67 LPROC_ECHO_READ_BYTES = 1,
68 LPROC_ECHO_WRITE_BYTES = 2,
69 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
72 static int echo_connect(const struct lu_env *env,
73 struct obd_export **exp, struct obd_device *obd,
74 struct obd_uuid *cluuid, struct obd_connect_data *data,
77 struct lustre_handle conn = { 0 };
80 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
81 rc = class_connect(&conn, obd, cluuid);
83 CERROR("can't connect %d\n", rc);
86 *exp = class_conn2export(&conn);
91 static int echo_disconnect(struct obd_export *exp)
93 LASSERT (exp != NULL);
95 return server_disconnect_export(exp);
98 static int echo_init_export(struct obd_export *exp)
100 return ldlm_init_export(exp);
103 static int echo_destroy_export(struct obd_export *exp)
107 target_destroy_export(exp);
108 ldlm_destroy_export(exp);
113 static __u64 echo_next_id(struct obd_device *obddev)
117 cfs_spin_lock(&obddev->u.echo.eo_lock);
118 id = ++obddev->u.echo.eo_lastino;
119 cfs_spin_unlock(&obddev->u.echo.eo_lock);
124 static int echo_create(struct obd_export *exp, struct obdo *oa,
125 struct lov_stripe_md **ea, struct obd_trans_info *oti)
127 struct obd_device *obd = class_exp2obd(exp);
130 CERROR("invalid client cookie "LPX64"\n",
131 exp->exp_handle.h_cookie);
135 if (!(oa->o_mode && S_IFMT)) {
136 CERROR("echo obd: no type!\n");
140 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
141 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
145 oa->o_id = echo_next_id(obd);
146 oa->o_valid = OBD_MD_FLID;
151 static int echo_destroy(struct obd_export *exp, struct obdo *oa,
152 struct lov_stripe_md *ea, struct obd_trans_info *oti,
153 struct obd_export *md_exp, void *capa)
155 struct obd_device *obd = class_exp2obd(exp);
159 CERROR("invalid client cookie "LPX64"\n",
160 exp->exp_handle.h_cookie);
164 if (!(oa->o_valid & OBD_MD_FLID)) {
165 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
169 if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OID) {
170 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
177 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
179 struct obd_device *obd = class_exp2obd(exp);
180 obd_id id = oinfo->oi_oa->o_id;
184 CERROR("invalid client cookie "LPX64"\n",
185 exp->exp_handle.h_cookie);
189 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
190 CERROR("obdo missing FLID valid flag: "LPX64"\n",
191 oinfo->oi_oa->o_valid);
195 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
196 oinfo->oi_oa->o_id = id;
201 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
202 struct obd_trans_info *oti)
204 struct obd_device *obd = class_exp2obd(exp);
208 CERROR("invalid client cookie "LPX64"\n",
209 exp->exp_handle.h_cookie);
213 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
214 CERROR("obdo missing FLID valid flag: "LPX64"\n",
215 oinfo->oi_oa->o_valid);
219 memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
221 if (oinfo->oi_oa->o_id & 4) {
222 /* Save lock to force ACKed reply */
223 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
224 oti->oti_ack_locks[0].mode = LCK_NL;
225 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
232 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
233 __u64 offset, int len)
235 int page_offset = offset & ~CFS_PAGE_MASK;
236 char *addr = ((char *)cfs_kmap(page)) + page_offset;
238 if (len % OBD_ECHO_BLOCK_SIZE != 0)
239 CERROR("Unexpected block size %d\n", len);
242 if (rw & OBD_BRW_READ)
243 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
246 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
247 0xecc0ecc0ecc0ecc0ULL,
248 0xecc0ecc0ecc0ecc0ULL);
250 addr += OBD_ECHO_BLOCK_SIZE;
251 offset += OBD_ECHO_BLOCK_SIZE;
252 len -= OBD_ECHO_BLOCK_SIZE;
259 echo_page_debug_check(cfs_page_t *page, obd_id id,
260 __u64 offset, int len)
262 int page_offset = offset & ~CFS_PAGE_MASK;
263 char *addr = ((char *)cfs_kmap(page)) + page_offset;
267 if (len % OBD_ECHO_BLOCK_SIZE != 0)
268 CERROR("Unexpected block size %d\n", len);
271 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
274 if (rc2 != 0 && rc == 0)
277 addr += OBD_ECHO_BLOCK_SIZE;
278 offset += OBD_ECHO_BLOCK_SIZE;
279 len -= OBD_ECHO_BLOCK_SIZE;
287 /* This allows us to verify that desc_private is passed unmolested */
288 #define DESC_PRIV 0x10293847
290 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
291 struct niobuf_remote *nb, int *pages,
292 struct niobuf_local *lb, int cmd, int *left)
294 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
295 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
296 int debug_setup = (!ispersistent &&
297 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
298 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
299 struct niobuf_local *res = lb;
300 obd_off offset = nb->offset;
304 int plen = CFS_PAGE_SIZE - (offset & (CFS_PAGE_SIZE-1));
308 /* check for local buf overflow */
312 res->offset = offset;
314 LASSERT((res->offset & ~CFS_PAGE_MASK) + res->len <= CFS_PAGE_SIZE);
318 (res->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
319 res->page = echo_persistent_pages[res->offset >>
321 /* Take extra ref so __free_pages() can be called OK */
322 cfs_get_page (res->page);
324 OBD_PAGE_ALLOC(res->page, gfp_mask);
325 if (res->page == NULL) {
326 CERROR("can't get page for id " LPU64"\n",
332 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
333 res->page, res->offset, res->len);
335 if (cmd & OBD_BRW_READ)
339 echo_page_debug_setup(res->page, cmd, obj->ioo_id,
340 res->offset, res->len);
353 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
354 struct niobuf_remote *rb, int *pgs,
355 struct niobuf_local *lb, int verify)
357 struct niobuf_local *res = lb;
358 obd_off start = rb->offset >> CFS_PAGE_SHIFT;
359 obd_off end = (rb->offset + rb->len + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
360 int count = (int)(end - start);
364 for (i = 0; i < count; i++, (*pgs) ++, res++) {
365 cfs_page_t *page = res->page;
369 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
370 obj->ioo_id, page, i, obj->ioo_bufcnt);
374 addr = cfs_kmap(page);
376 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
377 res->page, addr, res->offset);
380 int vrc = echo_page_debug_check(page, obj->ioo_id,
381 res->offset, res->len);
382 /* check all the pages always */
383 if (vrc != 0 && rc == 0)
388 /* NB see comment above regarding persistent pages */
395 static int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
396 int objcount, struct obd_ioobj *obj,
397 struct niobuf_remote *nb, int *pages,
398 struct niobuf_local *res, struct obd_trans_info *oti,
399 struct lustre_capa *unused)
401 struct obd_device *obd;
407 obd = export->exp_obd;
411 /* Temp fix to stop falling foul of osc_announce_cached() */
412 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
414 memset(res, 0, sizeof(*res) * *pages);
416 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
417 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
420 oti->oti_handle = (void *)DESC_PRIV;
425 for (i = 0; i < objcount; i++, obj++) {
428 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
430 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
431 res + *pages, cmd, &left);
433 GOTO(preprw_cleanup, rc);
435 tot_bytes += nb->len;
439 cfs_atomic_add(*pages, &obd->u.echo.eo_prep);
441 if (cmd & OBD_BRW_READ)
442 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
445 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
448 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
449 cfs_atomic_read(&obd->u.echo.eo_prep));
454 /* It is possible that we would rather handle errors by allow
455 * any already-set-up pages to complete, rather than tearing them
456 * all down again. I believe that this is what the in-kernel
457 * prep/commit operations do.
459 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
460 for (i = 0; i < *pages; i++) {
461 cfs_kunmap(res[i].page);
462 /* NB if this is a persistent page, __free_pages will just
463 * lose the extra ref gained above */
464 OBD_PAGE_FREE(res[i].page);
466 cfs_atomic_dec(&obd->u.echo.eo_prep);
472 static int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
473 int objcount, struct obd_ioobj *obj,
474 struct niobuf_remote *rb, int niocount,
475 struct niobuf_local *res, struct obd_trans_info *oti,
478 struct obd_device *obd;
483 obd = export->exp_obd;
488 GOTO(commitrw_cleanup, rc);
490 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
491 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
494 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
498 if (niocount && res == NULL) {
499 CERROR("NULL res niobuf with niocount %d\n", niocount);
503 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
505 for (i = 0; i < objcount; i++, obj++) {
506 int verify = (rc == 0 &&
507 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
508 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
509 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
512 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
513 int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs], verify);
519 GOTO(commitrw_cleanup, rc = vrc);
527 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
529 CDEBUG(D_PAGE, "%d pages remain after commit\n",
530 cfs_atomic_read(&obd->u.echo.eo_prep));
534 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
536 CERROR("cleaning up %d pages (%d obdos)\n",
537 niocount - pgs - 1, objcount);
539 while (pgs < niocount) {
540 cfs_page_t *page = res[pgs++].page;
545 /* NB see comment above regarding persistent pages */
547 cfs_atomic_dec(&obd->u.echo.eo_prep);
552 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
554 struct lprocfs_static_vars lvars;
557 struct ldlm_res_id res_id = {.name = {1}};
561 obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
562 cfs_spin_lock_init(&obd->u.echo.eo_lock);
563 obd->u.echo.eo_lastino = ECHO_INIT_OID;
565 sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
566 obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
567 LDLM_NAMESPACE_SERVER,
568 LDLM_NAMESPACE_MODEST,
570 if (obd->obd_namespace == NULL) {
575 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
576 NULL, LCK_NL, &lock_flags, NULL,
577 ldlm_completion_ast, NULL, NULL, 0, NULL,
578 &obd->u.echo.eo_nl_lock);
579 LASSERT (rc == ELDLM_OK);
581 lprocfs_echo_init_vars(&lvars);
582 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
583 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
584 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
585 LPROCFS_CNTR_AVGMINMAX,
586 "read_bytes", "bytes");
587 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
588 LPROCFS_CNTR_AVGMINMAX,
589 "write_bytes", "bytes");
592 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
593 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
597 static int echo_cleanup(struct obd_device *obd)
602 lprocfs_obd_cleanup(obd);
603 lprocfs_free_obd_stats(obd);
605 ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
607 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
608 * happened before calling ldlm_namespace_free() */
609 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT, cfs_time_seconds(1));
611 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
612 obd->obd_namespace = NULL;
614 leaked = cfs_atomic_read(&obd->u.echo.eo_prep);
616 CERROR("%d prep/commitrw pages leaked\n", leaked);
621 struct obd_ops echo_obd_ops = {
622 .o_owner = THIS_MODULE,
623 .o_connect = echo_connect,
624 .o_disconnect = echo_disconnect,
625 .o_init_export = echo_init_export,
626 .o_destroy_export = echo_destroy_export,
627 .o_create = echo_create,
628 .o_destroy = echo_destroy,
629 .o_getattr = echo_getattr,
630 .o_setattr = echo_setattr,
631 .o_preprw = echo_preprw,
632 .o_commitrw = echo_commitrw,
633 .o_setup = echo_setup,
634 .o_cleanup = echo_cleanup
637 void echo_persistent_pages_fini(void)
641 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
642 if (echo_persistent_pages[i] != NULL) {
643 OBD_PAGE_FREE(echo_persistent_pages[i]);
644 echo_persistent_pages[i] = NULL;
648 int echo_persistent_pages_init(void)
653 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
654 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
655 CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
657 OBD_PAGE_ALLOC(pg, gfp_mask);
659 echo_persistent_pages_fini ();
663 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
666 echo_persistent_pages[i] = pg;