4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdecho/echo.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
43 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_ECHO
48 #include <obd_support.h>
49 #include <obd_class.h>
50 #include <lustre_debug.h>
51 #include <lustre_dlm.h>
52 #include <lprocfs_status.h>
54 #include "echo_internal.h"
56 /* The echo objid needs to be below 2^32, because regular FID numbers are
57 * limited to 2^32 objects in f_oid for the FID_SEQ_ECHO range. b=23335 */
58 #define ECHO_INIT_OID 0x10000000ULL
59 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
61 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
62 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
65 LPROC_ECHO_READ_BYTES = 1,
66 LPROC_ECHO_WRITE_BYTES = 2,
67 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
70 static int echo_connect(const struct lu_env *env,
71 struct obd_export **exp, struct obd_device *obd,
72 struct obd_uuid *cluuid, struct obd_connect_data *data,
75 struct lustre_handle conn = { 0 };
78 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
79 rc = class_connect(&conn, obd, cluuid);
81 CERROR("can't connect %d\n", rc);
84 *exp = class_conn2export(&conn);
89 static int echo_disconnect(struct obd_export *exp)
91 LASSERT (exp != NULL);
93 return server_disconnect_export(exp);
96 static int echo_init_export(struct obd_export *exp)
98 return ldlm_init_export(exp);
101 static int echo_destroy_export(struct obd_export *exp)
105 target_destroy_export(exp);
106 ldlm_destroy_export(exp);
111 static __u64 echo_next_id(struct obd_device *obddev)
115 cfs_spin_lock(&obddev->u.echo.eo_lock);
116 id = ++obddev->u.echo.eo_lastino;
117 cfs_spin_unlock(&obddev->u.echo.eo_lock);
122 static int echo_create(const struct lu_env *env, struct obd_export *exp,
123 struct obdo *oa, struct lov_stripe_md **ea,
124 struct obd_trans_info *oti)
126 struct obd_device *obd = class_exp2obd(exp);
129 CERROR("invalid client cookie "LPX64"\n",
130 exp->exp_handle.h_cookie);
134 if (!(oa->o_mode && S_IFMT)) {
135 CERROR("echo obd: no type!\n");
139 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
140 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
144 oa->o_id = echo_next_id(obd);
145 oa->o_valid = OBD_MD_FLID;
150 static int echo_destroy(const struct lu_env *env, struct obd_export *exp,
151 struct obdo *oa, struct lov_stripe_md *ea,
152 struct obd_trans_info *oti, struct obd_export *md_exp,
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(const struct lu_env *env, struct obd_export *exp,
178 struct obd_info *oinfo)
180 struct obd_device *obd = class_exp2obd(exp);
181 obd_id id = oinfo->oi_oa->o_id;
185 CERROR("invalid client cookie "LPX64"\n",
186 exp->exp_handle.h_cookie);
190 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
191 CERROR("obdo missing FLID valid flag: "LPX64"\n",
192 oinfo->oi_oa->o_valid);
196 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
197 oinfo->oi_oa->o_id = id;
202 static int echo_setattr(const struct lu_env *env, struct obd_export *exp,
203 struct obd_info *oinfo, struct obd_trans_info *oti)
205 struct obd_device *obd = class_exp2obd(exp);
209 CERROR("invalid client cookie "LPX64"\n",
210 exp->exp_handle.h_cookie);
214 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
215 CERROR("obdo missing FLID valid flag: "LPX64"\n",
216 oinfo->oi_oa->o_valid);
220 memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
222 if (oinfo->oi_oa->o_id & 4) {
223 /* Save lock to force ACKed reply */
224 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
225 oti->oti_ack_locks[0].mode = LCK_NL;
226 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
233 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
234 __u64 offset, int len)
236 int page_offset = offset & ~CFS_PAGE_MASK;
237 char *addr = ((char *)cfs_kmap(page)) + page_offset;
239 if (len % OBD_ECHO_BLOCK_SIZE != 0)
240 CERROR("Unexpected block size %d\n", len);
243 if (rw & OBD_BRW_READ)
244 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
247 block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
248 0xecc0ecc0ecc0ecc0ULL,
249 0xecc0ecc0ecc0ecc0ULL);
251 addr += OBD_ECHO_BLOCK_SIZE;
252 offset += OBD_ECHO_BLOCK_SIZE;
253 len -= OBD_ECHO_BLOCK_SIZE;
260 echo_page_debug_check(cfs_page_t *page, obd_id id,
261 __u64 offset, int len)
263 int page_offset = offset & ~CFS_PAGE_MASK;
264 char *addr = ((char *)cfs_kmap(page)) + page_offset;
268 if (len % OBD_ECHO_BLOCK_SIZE != 0)
269 CERROR("Unexpected block size %d\n", len);
272 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
275 if (rc2 != 0 && rc == 0)
278 addr += OBD_ECHO_BLOCK_SIZE;
279 offset += OBD_ECHO_BLOCK_SIZE;
280 len -= OBD_ECHO_BLOCK_SIZE;
288 /* This allows us to verify that desc_private is passed unmolested */
289 #define DESC_PRIV 0x10293847
291 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
292 struct niobuf_remote *nb, int *pages,
293 struct niobuf_local *lb, int cmd, int *left)
295 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
296 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
297 int debug_setup = (!ispersistent &&
298 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
299 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
300 struct niobuf_local *res = lb;
301 obd_off offset = nb->offset;
305 int plen = CFS_PAGE_SIZE - (offset & (CFS_PAGE_SIZE-1));
309 /* check for local buf overflow */
313 res->offset = offset;
315 LASSERT((res->offset & ~CFS_PAGE_MASK) + res->len <= CFS_PAGE_SIZE);
319 (res->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
320 res->page = echo_persistent_pages[res->offset >>
322 /* Take extra ref so __free_pages() can be called OK */
323 cfs_get_page (res->page);
325 OBD_PAGE_ALLOC(res->page, gfp_mask);
326 if (res->page == NULL) {
327 CERROR("can't get page for id " LPU64"\n",
333 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
334 res->page, res->offset, res->len);
336 if (cmd & OBD_BRW_READ)
340 echo_page_debug_setup(res->page, cmd, obj->ioo_id,
341 res->offset, res->len);
354 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
355 struct niobuf_remote *rb, int *pgs,
356 struct niobuf_local *lb, int verify)
358 struct niobuf_local *res = lb;
359 obd_off start = rb->offset >> CFS_PAGE_SHIFT;
360 obd_off end = (rb->offset + rb->len + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
361 int count = (int)(end - start);
365 for (i = 0; i < count; i++, (*pgs) ++, res++) {
366 cfs_page_t *page = res->page;
370 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
371 obj->ioo_id, page, i, obj->ioo_bufcnt);
375 addr = cfs_kmap(page);
377 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
378 res->page, addr, res->offset);
381 int vrc = echo_page_debug_check(page, obj->ioo_id,
382 res->offset, res->len);
383 /* check all the pages always */
384 if (vrc != 0 && rc == 0)
389 /* NB see comment above regarding persistent pages */
396 static int echo_preprw(const struct lu_env *env, int cmd,
397 struct obd_export *export, struct obdo *oa,
398 int objcount, struct obd_ioobj *obj,
399 struct niobuf_remote *nb, int *pages,
400 struct niobuf_local *res, struct obd_trans_info *oti,
401 struct lustre_capa *unused)
403 struct obd_device *obd;
409 obd = export->exp_obd;
413 /* Temp fix to stop falling foul of osc_announce_cached() */
414 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
416 memset(res, 0, sizeof(*res) * *pages);
418 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
419 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
422 oti->oti_handle = (void *)DESC_PRIV;
427 for (i = 0; i < objcount; i++, obj++) {
430 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
432 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
433 res + *pages, cmd, &left);
435 GOTO(preprw_cleanup, rc);
437 tot_bytes += nb->len;
441 cfs_atomic_add(*pages, &obd->u.echo.eo_prep);
443 if (cmd & OBD_BRW_READ)
444 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
447 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
450 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
451 cfs_atomic_read(&obd->u.echo.eo_prep));
456 /* It is possible that we would rather handle errors by allow
457 * any already-set-up pages to complete, rather than tearing them
458 * all down again. I believe that this is what the in-kernel
459 * prep/commit operations do.
461 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
462 for (i = 0; i < *pages; i++) {
463 cfs_kunmap(res[i].page);
464 /* NB if this is a persistent page, __free_pages will just
465 * lose the extra ref gained above */
466 OBD_PAGE_FREE(res[i].page);
468 cfs_atomic_dec(&obd->u.echo.eo_prep);
474 static int echo_commitrw(const struct lu_env *env, int cmd,
475 struct obd_export *export, struct obdo *oa,
476 int objcount, struct obd_ioobj *obj,
477 struct niobuf_remote *rb, int niocount,
478 struct niobuf_local *res, struct obd_trans_info *oti,
481 struct obd_device *obd;
486 obd = export->exp_obd;
491 GOTO(commitrw_cleanup, rc);
493 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
494 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
497 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
501 if (niocount && res == NULL) {
502 CERROR("NULL res niobuf with niocount %d\n", niocount);
506 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
508 for (i = 0; i < objcount; i++, obj++) {
509 int verify = (rc == 0 &&
510 obj->ioo_id != ECHO_PERSISTENT_OBJID &&
511 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
512 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
515 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
516 int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs], verify);
522 GOTO(commitrw_cleanup, rc = vrc);
530 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
532 CDEBUG(D_PAGE, "%d pages remain after commit\n",
533 cfs_atomic_read(&obd->u.echo.eo_prep));
537 cfs_atomic_sub(pgs, &obd->u.echo.eo_prep);
539 CERROR("cleaning up %d pages (%d obdos)\n",
540 niocount - pgs - 1, objcount);
542 while (pgs < niocount) {
543 cfs_page_t *page = res[pgs++].page;
548 /* NB see comment above regarding persistent pages */
550 cfs_atomic_dec(&obd->u.echo.eo_prep);
555 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
557 struct lprocfs_static_vars lvars;
560 struct ldlm_res_id res_id = {.name = {1}};
564 obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
565 cfs_spin_lock_init(&obd->u.echo.eo_lock);
566 obd->u.echo.eo_lastino = ECHO_INIT_OID;
568 sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
569 obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
570 LDLM_NAMESPACE_SERVER,
571 LDLM_NAMESPACE_MODEST,
573 if (obd->obd_namespace == NULL) {
578 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
579 NULL, LCK_NL, &lock_flags, NULL,
580 ldlm_completion_ast, NULL, NULL, 0, NULL,
581 &obd->u.echo.eo_nl_lock);
582 LASSERT (rc == ELDLM_OK);
584 lprocfs_echo_init_vars(&lvars);
585 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
586 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
587 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
588 LPROCFS_CNTR_AVGMINMAX,
589 "read_bytes", "bytes");
590 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
591 LPROCFS_CNTR_AVGMINMAX,
592 "write_bytes", "bytes");
595 ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
596 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
600 static int echo_cleanup(struct obd_device *obd)
605 lprocfs_obd_cleanup(obd);
606 lprocfs_free_obd_stats(obd);
608 ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
610 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
611 * happened before calling ldlm_namespace_free() */
612 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINT, cfs_time_seconds(1));
614 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
615 obd->obd_namespace = NULL;
617 leaked = cfs_atomic_read(&obd->u.echo.eo_prep);
619 CERROR("%d prep/commitrw pages leaked\n", leaked);
624 struct obd_ops echo_obd_ops = {
625 .o_owner = THIS_MODULE,
626 .o_connect = echo_connect,
627 .o_disconnect = echo_disconnect,
628 .o_init_export = echo_init_export,
629 .o_destroy_export = echo_destroy_export,
630 .o_create = echo_create,
631 .o_destroy = echo_destroy,
632 .o_getattr = echo_getattr,
633 .o_setattr = echo_setattr,
634 .o_preprw = echo_preprw,
635 .o_commitrw = echo_commitrw,
636 .o_setup = echo_setup,
637 .o_cleanup = echo_cleanup
640 void echo_persistent_pages_fini(void)
644 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
645 if (echo_persistent_pages[i] != NULL) {
646 OBD_PAGE_FREE(echo_persistent_pages[i]);
647 echo_persistent_pages[i] = NULL;
651 int echo_persistent_pages_init(void)
656 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
657 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
658 CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
660 OBD_PAGE_ALLOC(pg, gfp_mask);
662 echo_persistent_pages_fini ();
666 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
669 echo_persistent_pages[i] = pg;