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, 2013, Intel Corporation.
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>
42 #define DEBUG_SUBSYSTEM S_ECHO
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_debug.h>
47 #include <lustre_dlm.h>
48 #include <lprocfs_status.h>
50 #include "echo_internal.h"
52 /* The echo objid needs to be below 2^32, because regular FID numbers are
53 * limited to 2^32 objects in f_oid for the FID_SEQ_ECHO range. b=23335 */
54 #define ECHO_INIT_OID 0x10000000ULL
55 #define ECHO_HANDLE_MAGIC 0xabcd0123fedc9876ULL
57 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> PAGE_CACHE_SHIFT)
58 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
61 LPROC_ECHO_READ_BYTES = 1,
62 LPROC_ECHO_WRITE_BYTES = 2,
63 LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
66 static int echo_connect(const struct lu_env *env,
67 struct obd_export **exp, struct obd_device *obd,
68 struct obd_uuid *cluuid, struct obd_connect_data *data,
71 struct lustre_handle conn = { 0 };
74 data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
75 rc = class_connect(&conn, obd, cluuid);
77 CERROR("can't connect %d\n", rc);
80 *exp = class_conn2export(&conn);
85 static int echo_disconnect(struct obd_export *exp)
87 LASSERT (exp != NULL);
89 return server_disconnect_export(exp);
92 static int echo_init_export(struct obd_export *exp)
94 return ldlm_init_export(exp);
97 static int echo_destroy_export(struct obd_export *exp)
101 target_destroy_export(exp);
102 ldlm_destroy_export(exp);
107 static __u64 echo_next_id(struct obd_device *obddev)
111 spin_lock(&obddev->u.echo.eo_lock);
112 id = ++obddev->u.echo.eo_lastino;
113 spin_unlock(&obddev->u.echo.eo_lock);
118 static int echo_create(const struct lu_env *env, struct obd_export *exp,
119 struct obdo *oa, struct lov_stripe_md **ea,
120 struct obd_trans_info *oti)
122 struct obd_device *obd = class_exp2obd(exp);
125 CERROR("invalid client cookie "LPX64"\n",
126 exp->exp_handle.h_cookie);
130 if (!(oa->o_mode & S_IFMT)) {
131 CERROR("echo obd: no type!\n");
135 if (!(oa->o_valid & OBD_MD_FLTYPE)) {
136 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
140 ostid_set_seq_echo(&oa->o_oi);
141 ostid_set_id(&oa->o_oi, echo_next_id(obd));
142 oa->o_valid = OBD_MD_FLID;
147 static int echo_destroy(const struct lu_env *env, struct obd_export *exp,
148 struct obdo *oa, struct lov_stripe_md *ea,
149 struct obd_trans_info *oti, struct obd_export *md_exp,
152 struct obd_device *obd = class_exp2obd(exp);
156 CERROR("invalid client cookie "LPX64"\n",
157 exp->exp_handle.h_cookie);
161 if (!(oa->o_valid & OBD_MD_FLID)) {
162 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
166 if (ostid_id(&oa->o_oi) > obd->u.echo.eo_lastino ||
167 ostid_id(&oa->o_oi) < ECHO_INIT_OID) {
168 CERROR("bad destroy objid: "DOSTID"\n", POSTID(&oa->o_oi));
175 static int echo_getattr(const struct lu_env *env, struct obd_export *exp,
176 struct obd_info *oinfo)
178 struct obd_device *obd = class_exp2obd(exp);
179 obd_id id = ostid_id(&oinfo->oi_oa->o_oi);
183 CERROR("invalid client cookie "LPX64"\n",
184 exp->exp_handle.h_cookie);
188 if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
189 CERROR("obdo missing FLID valid flag: "LPX64"\n",
190 oinfo->oi_oa->o_valid);
194 obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
195 ostid_set_seq_echo(&oinfo->oi_oa->o_oi);
196 ostid_set_id(&oinfo->oi_oa->o_oi, id);
201 static int echo_setattr(const struct lu_env *env, struct obd_export *exp,
202 struct obd_info *oinfo, 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 (ostid_id(&oinfo->oi_oa->o_oi) & 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(struct page *page, int rw, obd_id id,
233 __u64 offset, int len)
235 int page_offset = offset & ~CFS_PAGE_MASK;
236 char *addr = ((char *)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(struct page *page, obd_id id,
260 __u64 offset, int len)
262 int page_offset = offset & ~CFS_PAGE_MASK;
263 char *addr = ((char *)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 gfp_t gfp_mask = (ostid_id(&obj->ioo_oid) & 1) ?
295 GFP_HIGHUSER : GFP_IOFS;
296 int ispersistent = ostid_id(&obj->ioo_oid) == 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->rnb_offset;
302 int len = nb->rnb_len;
305 int plen = PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1));
309 /* check for local buf overflow */
313 res->lnb_file_offset = offset;
315 LASSERT((res->lnb_file_offset & ~CFS_PAGE_MASK) +
316 res->lnb_len <= PAGE_CACHE_SIZE);
319 ((res->lnb_file_offset >> PAGE_CACHE_SHIFT) <
320 ECHO_PERSISTENT_PAGES)) {
322 echo_persistent_pages[res->lnb_file_offset >>
324 /* Take extra ref so __free_pages() can be called OK */
325 get_page(res->lnb_page);
327 OBD_PAGE_ALLOC(res->lnb_page, gfp_mask);
328 if (res->lnb_page == NULL) {
329 CERROR("can't get page for id " DOSTID"\n",
330 POSTID(&obj->ioo_oid));
335 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
336 res->lnb_page, res->lnb_file_offset, res->lnb_len);
338 if (cmd & OBD_BRW_READ)
339 res->lnb_rc = res->lnb_len;
342 echo_page_debug_setup(res->lnb_page, cmd,
343 ostid_id(&obj->ioo_oid),
344 res->lnb_file_offset,
358 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
359 struct niobuf_remote *rb, int *pgs,
360 struct niobuf_local *lb, int verify)
362 struct niobuf_local *res = lb;
363 obd_off start = rb->rnb_offset >> PAGE_CACHE_SHIFT;
364 obd_off end = (rb->rnb_offset + rb->rnb_len + PAGE_CACHE_SIZE - 1) >>
366 int count = (int)(end - start);
370 for (i = 0; i < count; i++, (*pgs) ++, res++) {
371 struct page *page = res->lnb_page;
375 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
376 ostid_id(&obj->ioo_oid), page, i,
383 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
384 res->lnb_page, addr, res->lnb_file_offset);
387 int vrc = echo_page_debug_check(page,
388 ostid_id(&obj->ioo_oid),
389 res->lnb_file_offset,
391 /* check all the pages always */
392 if (vrc != 0 && rc == 0)
397 /* NB see comment above regarding persistent pages */
404 static int echo_preprw(const struct lu_env *env, int cmd,
405 struct obd_export *export, struct obdo *oa,
406 int objcount, struct obd_ioobj *obj,
407 struct niobuf_remote *nb, int *pages,
408 struct niobuf_local *res, struct obd_trans_info *oti,
409 struct lustre_capa *unused)
411 struct obd_device *obd;
417 obd = export->exp_obd;
421 /* Temp fix to stop falling foul of osc_announce_cached() */
422 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
424 memset(res, 0, sizeof(*res) * *pages);
426 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
427 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
430 oti->oti_handle = (void *)DESC_PRIV;
435 for (i = 0; i < objcount; i++, obj++) {
438 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
440 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
441 res + *pages, cmd, &left);
443 GOTO(preprw_cleanup, rc);
445 tot_bytes += nb->rnb_len;
449 atomic_add(*pages, &obd->u.echo.eo_prep);
451 if (cmd & OBD_BRW_READ)
452 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
455 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
458 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
459 atomic_read(&obd->u.echo.eo_prep));
464 /* It is possible that we would rather handle errors by allow
465 * any already-set-up pages to complete, rather than tearing them
466 * all down again. I believe that this is what the in-kernel
467 * prep/commit operations do.
469 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
470 for (i = 0; i < *pages; i++) {
471 kunmap(res[i].lnb_page);
472 /* NB if this is a persistent page, __free_pages will just
473 * lose the extra ref gained above */
474 OBD_PAGE_FREE(res[i].lnb_page);
475 res[i].lnb_page = NULL;
476 atomic_dec(&obd->u.echo.eo_prep);
482 static int echo_commitrw(const struct lu_env *env, int cmd,
483 struct obd_export *export, struct obdo *oa,
484 int objcount, struct obd_ioobj *obj,
485 struct niobuf_remote *rb, int niocount,
486 struct niobuf_local *res, struct obd_trans_info *oti,
489 struct obd_device *obd;
494 obd = export->exp_obd;
499 GOTO(commitrw_cleanup, rc);
501 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
502 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
505 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
509 if (niocount && res == NULL) {
510 CERROR("NULL res niobuf with niocount %d\n", niocount);
514 LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
516 for (i = 0; i < objcount; i++, obj++) {
517 int verify = (rc == 0 &&
518 ostid_id(&obj->ioo_oid) != ECHO_PERSISTENT_OBJID &&
519 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
520 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
523 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
524 int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs],
530 GOTO(commitrw_cleanup, rc = vrc);
538 atomic_sub(pgs, &obd->u.echo.eo_prep);
540 CDEBUG(D_PAGE, "%d pages remain after commit\n",
541 atomic_read(&obd->u.echo.eo_prep));
545 atomic_sub(pgs, &obd->u.echo.eo_prep);
547 CERROR("cleaning up %d pages (%d obdos)\n",
548 niocount - pgs - 1, objcount);
550 while (pgs < niocount) {
551 struct page *page = res[pgs++].lnb_page;
556 /* NB see comment above regarding persistent pages */
558 atomic_dec(&obd->u.echo.eo_prep);
563 LPROC_SEQ_FOPS_RO_TYPE(echo, uuid);
564 static struct lprocfs_seq_vars lprocfs_echo_obd_vars[] = {
566 .fops = &echo_uuid_fops },
570 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
573 __u64 lock_flags = 0;
574 struct ldlm_res_id res_id = {.name = {1}};
578 obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
579 spin_lock_init(&obd->u.echo.eo_lock);
580 obd->u.echo.eo_lastino = ECHO_INIT_OID;
582 sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
583 obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
584 LDLM_NAMESPACE_SERVER,
585 LDLM_NAMESPACE_MODEST,
587 if (obd->obd_namespace == NULL) {
592 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
593 NULL, LCK_NL, &lock_flags, NULL,
594 ldlm_completion_ast, NULL, NULL, 0,
595 LVB_T_NONE, NULL, &obd->u.echo.eo_nl_lock);
596 LASSERT (rc == ELDLM_OK);
598 obd->obd_vars = lprocfs_echo_obd_vars;
599 if (lprocfs_obd_setup(obd) == 0 &&
600 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
601 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
602 LPROCFS_CNTR_AVGMINMAX,
603 "read_bytes", "bytes");
604 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
605 LPROCFS_CNTR_AVGMINMAX,
606 "write_bytes", "bytes");
609 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
610 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
614 static int echo_cleanup(struct obd_device *obd)
619 lprocfs_obd_cleanup(obd);
620 lprocfs_free_obd_stats(obd);
622 ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
624 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
625 * happened before calling ldlm_namespace_free() */
626 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE, cfs_time_seconds(1));
628 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
629 obd->obd_namespace = NULL;
631 leaked = atomic_read(&obd->u.echo.eo_prep);
633 CERROR("%d prep/commitrw pages leaked\n", leaked);
638 struct obd_ops echo_obd_ops = {
639 .o_owner = THIS_MODULE,
640 .o_connect = echo_connect,
641 .o_disconnect = echo_disconnect,
642 .o_init_export = echo_init_export,
643 .o_destroy_export = echo_destroy_export,
644 .o_create = echo_create,
645 .o_destroy = echo_destroy,
646 .o_getattr = echo_getattr,
647 .o_setattr = echo_setattr,
648 .o_preprw = echo_preprw,
649 .o_commitrw = echo_commitrw,
650 .o_setup = echo_setup,
651 .o_cleanup = echo_cleanup
654 void echo_persistent_pages_fini(void)
658 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
659 if (echo_persistent_pages[i] != NULL) {
660 OBD_PAGE_FREE(echo_persistent_pages[i]);
661 echo_persistent_pages[i] = NULL;
665 int echo_persistent_pages_init(void)
670 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
671 gfp_t gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
672 GFP_IOFS : GFP_HIGHUSER;
674 OBD_PAGE_ALLOC(pg, gfp_mask);
676 echo_persistent_pages_fini();
680 memset (kmap (pg), 0, PAGE_CACHE_SIZE);
683 echo_persistent_pages[i] = pg;