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, 2015, 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 data->ocd_connect_flags2 &= ECHO_CONNECT_SUPPORTED2;
76 rc = class_connect(&conn, obd, cluuid);
78 CERROR("can't connect %d\n", rc);
81 *exp = class_conn2export(&conn);
86 static int echo_disconnect(struct obd_export *exp)
88 LASSERT (exp != NULL);
90 return server_disconnect_export(exp);
93 static int echo_init_export(struct obd_export *exp)
95 return ldlm_init_export(exp);
98 static int echo_destroy_export(struct obd_export *exp)
102 target_destroy_export(exp);
103 ldlm_destroy_export(exp);
108 static u64 echo_next_id(struct obd_device *obddev)
112 spin_lock(&obddev->u.echo.eo_lock);
113 id = ++obddev->u.echo.eo_lastino;
114 spin_unlock(&obddev->u.echo.eo_lock);
119 static int echo_create(const struct lu_env *env, struct obd_export *exp,
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,
150 struct obd_device *obd = class_exp2obd(exp);
154 CERROR("invalid client cookie "LPX64"\n",
155 exp->exp_handle.h_cookie);
159 if (!(oa->o_valid & OBD_MD_FLID)) {
160 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
164 if (ostid_id(&oa->o_oi) > obd->u.echo.eo_lastino ||
165 ostid_id(&oa->o_oi) < ECHO_INIT_OID) {
166 CERROR("bad destroy objid: "DOSTID"\n", POSTID(&oa->o_oi));
173 static int echo_getattr(const struct lu_env *env, struct obd_export *exp,
176 struct obd_device *obd = class_exp2obd(exp);
177 u64 id = ostid_id(&oa->o_oi);
181 CERROR("invalid client cookie "LPX64"\n",
182 exp->exp_handle.h_cookie);
186 if (!(oa->o_valid & OBD_MD_FLID)) {
187 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
191 obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
192 ostid_set_seq_echo(&oa->o_oi);
193 ostid_set_id(&oa->o_oi, id);
198 static int echo_setattr(const struct lu_env *env, struct obd_export *exp,
201 struct obd_device *obd = class_exp2obd(exp);
205 CERROR("invalid client cookie "LPX64"\n",
206 exp->exp_handle.h_cookie);
210 if (!(oa->o_valid & OBD_MD_FLID)) {
211 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
215 obd->u.echo.eo_oa = *oa;
221 echo_page_debug_setup(struct page *page, int rw, u64 id,
222 __u64 offset, int len)
224 int page_offset = offset & ~PAGE_MASK;
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, u64 id,
249 __u64 offset, int len)
251 int page_offset = offset & ~PAGE_MASK;
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 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
277 struct niobuf_remote *nb, int *pages,
278 struct niobuf_local *lb, int cmd, int *left)
280 gfp_t gfp_mask = (ostid_id(&obj->ioo_oid) & 1) ?
281 GFP_HIGHUSER : GFP_KERNEL;
282 int ispersistent = ostid_id(&obj->ioo_oid) == ECHO_PERSISTENT_OBJID;
283 int debug_setup = (!ispersistent &&
284 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
285 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
286 struct niobuf_local *res = lb;
287 u64 offset = nb->rnb_offset;
288 int len = nb->rnb_len;
291 int plen = PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1));
295 /* check for local buf overflow */
299 res->lnb_file_offset = offset;
301 LASSERT((res->lnb_file_offset & ~PAGE_MASK) +
302 res->lnb_len <= PAGE_CACHE_SIZE);
305 ((res->lnb_file_offset >> PAGE_CACHE_SHIFT) <
306 ECHO_PERSISTENT_PAGES)) {
308 echo_persistent_pages[res->lnb_file_offset >>
310 /* Take extra ref so __free_pages() can be called OK */
311 get_page(res->lnb_page);
313 res->lnb_page = alloc_page(gfp_mask);
314 if (res->lnb_page == NULL) {
315 CERROR("can't get page for id " DOSTID"\n",
316 POSTID(&obj->ioo_oid));
321 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
322 res->lnb_page, res->lnb_file_offset, res->lnb_len);
324 if (cmd & OBD_BRW_READ)
325 res->lnb_rc = res->lnb_len;
328 echo_page_debug_setup(res->lnb_page, cmd,
329 ostid_id(&obj->ioo_oid),
330 res->lnb_file_offset,
344 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
345 struct niobuf_remote *rb, int *pgs,
346 struct niobuf_local *lb, int verify)
348 struct niobuf_local *res = lb;
349 u64 start = rb->rnb_offset >> PAGE_CACHE_SHIFT;
350 u64 end = (rb->rnb_offset + rb->rnb_len + PAGE_CACHE_SIZE - 1) >>
352 int count = (int)(end - start);
356 for (i = 0; i < count; i++, (*pgs) ++, res++) {
357 struct page *page = res->lnb_page;
361 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
362 ostid_id(&obj->ioo_oid), page, i,
369 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
370 res->lnb_page, addr, res->lnb_file_offset);
373 int vrc = echo_page_debug_check(page,
374 ostid_id(&obj->ioo_oid),
375 res->lnb_file_offset,
377 /* check all the pages always */
378 if (vrc != 0 && rc == 0)
383 /* NB see comment above regarding persistent pages */
390 static int echo_preprw(const struct lu_env *env, int cmd,
391 struct obd_export *export, struct obdo *oa,
392 int objcount, struct obd_ioobj *obj,
393 struct niobuf_remote *nb, int *pages,
394 struct niobuf_local *res)
396 struct obd_device *obd;
402 obd = export->exp_obd;
406 /* Temp fix to stop falling foul of osc_announce_cached() */
407 oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
409 memset(res, 0, sizeof(*res) * *pages);
411 CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
412 cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
417 for (i = 0; i < objcount; i++, obj++) {
420 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
422 rc = echo_map_nb_to_lb(oa, obj, nb, pages,
423 res + *pages, cmd, &left);
425 GOTO(preprw_cleanup, rc);
427 tot_bytes += nb->rnb_len;
431 atomic_add(*pages, &obd->u.echo.eo_prep);
433 if (cmd & OBD_BRW_READ)
434 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
437 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
440 CDEBUG(D_PAGE, "%d pages allocated after prep\n",
441 atomic_read(&obd->u.echo.eo_prep));
446 /* It is possible that we would rather handle errors by allow
447 * any already-set-up pages to complete, rather than tearing them
448 * all down again. I believe that this is what the in-kernel
449 * prep/commit operations do.
451 CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
452 for (i = 0; i < *pages; i++) {
453 kunmap(res[i].lnb_page);
454 /* NB if this is a persistent page, __free_page() will just
455 * lose the extra ref gained above */
456 __free_page(res[i].lnb_page);
457 res[i].lnb_page = NULL;
458 atomic_dec(&obd->u.echo.eo_prep);
464 static int echo_commitrw(const struct lu_env *env, int cmd,
465 struct obd_export *export, struct obdo *oa,
466 int objcount, struct obd_ioobj *obj,
467 struct niobuf_remote *rb, int niocount,
468 struct niobuf_local *res, int rc)
470 struct obd_device *obd;
475 obd = export->exp_obd;
480 GOTO(commitrw_cleanup, rc);
482 if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
483 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
486 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
490 if (niocount && res == NULL) {
491 CERROR("NULL res niobuf with niocount %d\n", niocount);
495 for (i = 0; i < objcount; i++, obj++) {
496 int verify = (rc == 0 &&
497 ostid_id(&obj->ioo_oid) != ECHO_PERSISTENT_OBJID &&
498 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
499 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
502 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
503 int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs],
509 GOTO(commitrw_cleanup, rc = vrc);
517 atomic_sub(pgs, &obd->u.echo.eo_prep);
519 CDEBUG(D_PAGE, "%d pages remain after commit\n",
520 atomic_read(&obd->u.echo.eo_prep));
524 atomic_sub(pgs, &obd->u.echo.eo_prep);
526 CERROR("cleaning up %d pages (%d obdos)\n",
527 niocount - pgs - 1, objcount);
529 while (pgs < niocount) {
530 struct page *page = res[pgs++].lnb_page;
535 /* NB see comment above regarding persistent pages */
537 atomic_dec(&obd->u.echo.eo_prep);
542 LPROC_SEQ_FOPS_RO_TYPE(echo, uuid);
543 static struct lprocfs_vars lprocfs_echo_obd_vars[] = {
545 .fops = &echo_uuid_fops },
549 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
552 __u64 lock_flags = 0;
553 struct ldlm_res_id res_id = {.name = {1}};
557 obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
558 spin_lock_init(&obd->u.echo.eo_lock);
559 obd->u.echo.eo_lastino = ECHO_INIT_OID;
561 sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
562 obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
563 LDLM_NAMESPACE_SERVER,
564 LDLM_NAMESPACE_MODEST,
566 if (obd->obd_namespace == NULL) {
571 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
572 NULL, LCK_NL, &lock_flags, NULL,
573 ldlm_completion_ast, NULL, NULL, 0,
574 LVB_T_NONE, NULL, &obd->u.echo.eo_nl_lock);
575 LASSERT (rc == ELDLM_OK);
577 obd->obd_vars = lprocfs_echo_obd_vars;
578 if (lprocfs_obd_setup(obd) == 0 &&
579 lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
580 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
581 LPROCFS_CNTR_AVGMINMAX,
582 "read_bytes", "bytes");
583 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
584 LPROCFS_CNTR_AVGMINMAX,
585 "write_bytes", "bytes");
588 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
589 "echo_ldlm_cb_client", &obd->obd_ldlm_client);
593 static int echo_cleanup(struct obd_device *obd)
598 lprocfs_obd_cleanup(obd);
599 lprocfs_free_obd_stats(obd);
601 ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
603 /* XXX Bug 3413; wait for a bit to ensure the BL callback has
604 * happened before calling ldlm_namespace_free() */
605 set_current_state(TASK_UNINTERRUPTIBLE);
606 schedule_timeout(cfs_time_seconds(1));
608 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
609 obd->obd_namespace = NULL;
611 leaked = atomic_read(&obd->u.echo.eo_prep);
613 CERROR("%d prep/commitrw pages leaked\n", leaked);
618 struct obd_ops echo_obd_ops = {
619 .o_owner = THIS_MODULE,
620 .o_connect = echo_connect,
621 .o_disconnect = echo_disconnect,
622 .o_init_export = echo_init_export,
623 .o_destroy_export = echo_destroy_export,
624 .o_create = echo_create,
625 .o_destroy = echo_destroy,
626 .o_getattr = echo_getattr,
627 .o_setattr = echo_setattr,
628 .o_preprw = echo_preprw,
629 .o_commitrw = echo_commitrw,
630 .o_setup = echo_setup,
631 .o_cleanup = echo_cleanup
634 void echo_persistent_pages_fini(void)
638 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
639 if (echo_persistent_pages[i] != NULL) {
640 __free_page(echo_persistent_pages[i]);
641 echo_persistent_pages[i] = NULL;
645 int echo_persistent_pages_init(void)
650 for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
651 gfp_t gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
652 GFP_KERNEL : GFP_HIGHUSER;
654 pg = alloc_page(gfp_mask);
656 echo_persistent_pages_fini();
660 memset (kmap (pg), 0, PAGE_CACHE_SIZE);
663 echo_persistent_pages[i] = pg;