Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdecho / echo.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdecho/echo.c
32  *
33  * Author: Peter Braam <braam@clusterfs.com>
34  * Author: Andreas Dilger <adilger@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_ECHO
38
39 #include <obd_support.h>
40 #include <obd_class.h>
41 #include <lustre_dlm.h>
42 #include <lprocfs_status.h>
43
44 #include "echo_internal.h"
45
46 /*
47  * The echo objid needs to be below 2^32, because regular FID numbers are
48  * limited to 2^32 objects in f_oid for the FID_SEQ_ECHO range. b=23335
49  */
50 #define ECHO_INIT_OID        0x10000000ULL
51 #define ECHO_HANDLE_MAGIC    0xabcd0123fedc9876ULL
52
53 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> PAGE_SHIFT)
54 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
55
56 enum {
57         LPROC_ECHO_READ_BYTES = 1,
58         LPROC_ECHO_WRITE_BYTES = 2,
59         LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES + 1
60 };
61
62 struct echo_srv_device {
63         struct lu_device esd_dev;
64         struct lu_target esd_lut;
65 };
66
67 static inline struct echo_srv_device *echo_srv_dev(struct lu_device *d)
68 {
69         return container_of_safe(d, struct echo_srv_device, esd_dev);
70 }
71
72 static inline struct obd_device *echo_srv_obd(struct echo_srv_device *esd)
73 {
74         return esd->esd_dev.ld_obd;
75 }
76
77 static int echo_connect(const struct lu_env *env,
78                         struct obd_export **exp, struct obd_device *obd,
79                         struct obd_uuid *cluuid, struct obd_connect_data *data,
80                         void *localdata)
81 {
82         struct lustre_handle conn = { 0 };
83         int rc;
84
85         data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
86
87         if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
88                 data->ocd_connect_flags2 &= ECHO_CONNECT_SUPPORTED2;
89
90         rc = class_connect(&conn, obd, cluuid);
91         if (rc) {
92                 CERROR("can't connect %d\n", rc);
93                 return rc;
94         }
95         *exp = class_conn2export(&conn);
96
97         return 0;
98 }
99
100 static int echo_disconnect(struct obd_export *exp)
101 {
102         LASSERT(exp != NULL);
103
104         return server_disconnect_export(exp);
105 }
106
107 static int echo_init_export(struct obd_export *exp)
108 {
109         return ldlm_init_export(exp);
110 }
111
112 static int echo_destroy_export(struct obd_export *exp)
113 {
114         ENTRY;
115
116         target_destroy_export(exp);
117         ldlm_destroy_export(exp);
118
119         RETURN(0);
120 }
121
122 static u64 echo_next_id(struct obd_device *obd)
123 {
124         u64 id;
125
126         spin_lock(&obd->u.echo.eo_lock);
127         id = ++obd->u.echo.eo_lastino;
128         spin_unlock(&obd->u.echo.eo_lock);
129
130         return id;
131 }
132
133 static void
134 echo_page_debug_setup(struct page *page, int rw, u64 id,
135                       __u64 offset, int len)
136 {
137         int   page_offset = offset & ~PAGE_MASK;
138         char *addr        = ((char *)kmap(page)) + page_offset;
139
140         if (len % OBD_ECHO_BLOCK_SIZE != 0)
141                 CERROR("Unexpected block size %d\n", len);
142
143         while (len > 0) {
144                 if (rw & OBD_BRW_READ)
145                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
146                                           offset, id);
147                 else
148                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
149                                           0xecc0ecc0ecc0ecc0ULL,
150                                           0xecc0ecc0ecc0ecc0ULL);
151
152                 addr   += OBD_ECHO_BLOCK_SIZE;
153                 offset += OBD_ECHO_BLOCK_SIZE;
154                 len    -= OBD_ECHO_BLOCK_SIZE;
155         }
156
157         kunmap(page);
158 }
159
160 static int
161 echo_page_debug_check(struct page *page, u64 id,
162                       __u64 offset, int len)
163 {
164         int   page_offset = offset & ~PAGE_MASK;
165         char *addr        = ((char *)kmap(page)) + page_offset;
166         int   rc          = 0;
167         int   rc2;
168
169         if (len % OBD_ECHO_BLOCK_SIZE != 0)
170                 CERROR("Unexpected block size %d\n", len);
171
172         while (len > 0) {
173                 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
174                                         offset, id);
175
176                 if (rc2 != 0 && rc == 0)
177                         rc = rc2;
178
179                 addr   += OBD_ECHO_BLOCK_SIZE;
180                 offset += OBD_ECHO_BLOCK_SIZE;
181                 len    -= OBD_ECHO_BLOCK_SIZE;
182         }
183
184         kunmap(page);
185
186         return rc;
187 }
188
189 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
190                              struct niobuf_remote *nb, int *pages,
191                              struct niobuf_local *lb, int cmd, int *left)
192 {
193         gfp_t gfp_mask = (ostid_id(&obj->ioo_oid) & 1) ?
194                         GFP_HIGHUSER : GFP_KERNEL;
195         int ispersistent = ostid_id(&obj->ioo_oid) == ECHO_PERSISTENT_OBJID;
196         int debug_setup = (!ispersistent &&
197                            (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
198                            (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
199         struct niobuf_local *res = lb;
200         u64 offset = nb->rnb_offset;
201         int len = nb->rnb_len;
202
203         while (len > 0) {
204                 int plen = PAGE_SIZE - (offset & (PAGE_SIZE - 1));
205
206                 if (len < plen)
207                         plen = len;
208
209                 /* check for local buf overflow */
210                 if (*left == 0)
211                         return -EINVAL;
212
213                 res->lnb_file_offset = offset;
214                 res->lnb_len = plen;
215                 LASSERT((res->lnb_file_offset & ~PAGE_MASK) +
216                         res->lnb_len <= PAGE_SIZE);
217
218                 if (ispersistent &&
219                     ((res->lnb_file_offset >> PAGE_SHIFT) <
220                       ECHO_PERSISTENT_PAGES)) {
221                         res->lnb_page =
222                                 echo_persistent_pages[res->lnb_file_offset >>
223                                                       PAGE_SHIFT];
224                         /* Take extra ref so __free_pages() can be called OK */
225                         get_page(res->lnb_page);
226                 } else {
227                         res->lnb_page = alloc_page(gfp_mask);
228                         if (!res->lnb_page) {
229                                 CERROR("can't get page for id " DOSTID"\n",
230                                        POSTID(&obj->ioo_oid));
231                                 return -ENOMEM;
232                         }
233                         /* set mapping so page is not considered encrypted */
234                         res->lnb_page->mapping = ECHO_MAPPING_UNENCRYPTED;
235                 }
236
237                 CDEBUG(D_PAGE, "$$$$ get page %p @ %llu for %d\n",
238                        res->lnb_page, res->lnb_file_offset, res->lnb_len);
239
240                 if (cmd & OBD_BRW_READ)
241                         res->lnb_rc = res->lnb_len;
242
243                 if (debug_setup)
244                         echo_page_debug_setup(res->lnb_page, cmd,
245                                               ostid_id(&obj->ioo_oid),
246                                               res->lnb_file_offset,
247                                               res->lnb_len);
248
249                 offset += plen;
250                 len -= plen;
251                 res++;
252
253                 (*left)--;
254                 (*pages)++;
255         }
256
257         return 0;
258 }
259
260 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
261                             struct niobuf_remote *rb, int *pgs,
262                             struct niobuf_local *lb, int verify)
263 {
264         struct niobuf_local *res = lb;
265         u64 start = rb->rnb_offset >> PAGE_SHIFT;
266         u64 end   = (rb->rnb_offset + rb->rnb_len + PAGE_SIZE - 1) >>
267                     PAGE_SHIFT;
268         int     count  = (int)(end - start);
269         int     rc     = 0;
270         int     i;
271
272         for (i = 0; i < count; i++, (*pgs) ++, res++) {
273                 struct page *page = res->lnb_page;
274                 void       *addr;
275
276                 if (!page) {
277                         CERROR("null page objid %llu:%p, buf %d/%d\n",
278                                ostid_id(&obj->ioo_oid), page, i,
279                                obj->ioo_bufcnt);
280                         return -EFAULT;
281                 }
282
283                 addr = kmap(page);
284
285                 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@%llu\n",
286                        res->lnb_page, addr, res->lnb_file_offset);
287
288                 if (verify) {
289                         int vrc = echo_page_debug_check(page,
290                                                         ostid_id(&obj->ioo_oid),
291                                                         res->lnb_file_offset,
292                                                         res->lnb_len);
293                         /* check all the pages always */
294                         if (vrc != 0 && rc == 0)
295                                 rc = vrc;
296                 }
297
298                 kunmap(page);
299                 /* NB see comment above regarding persistent pages */
300                 __free_page(page);
301         }
302
303         return rc;
304 }
305
306 static int echo_preprw(const struct lu_env *env, int cmd,
307                        struct obd_export *export, struct obdo *oa,
308                        int objcount, struct obd_ioobj *obj,
309                        struct niobuf_remote *nb, int *pages,
310                        struct niobuf_local *res)
311 {
312         struct obd_device *obd;
313         int tot_bytes = 0;
314         int rc = 0;
315         int i, left;
316
317         ENTRY;
318
319         obd = export->exp_obd;
320         if (!obd)
321                 RETURN(-EINVAL);
322
323         /* Temp fix to stop falling foul of osc_announce_cached() */
324         oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
325
326         memset(res, 0, sizeof(*res) * *pages);
327
328         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
329                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
330
331         left = *pages;
332         *pages = 0;
333
334         for (i = 0; i < objcount; i++, obj++) {
335                 int j;
336
337                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
338                         rc = echo_map_nb_to_lb(oa, obj, nb, pages,
339                                                res + *pages, cmd, &left);
340                         if (rc)
341                                 GOTO(preprw_cleanup, rc);
342
343                         tot_bytes += nb->rnb_len;
344                 }
345         }
346
347         atomic_add(*pages, &obd->u.echo.eo_prep);
348
349         if (cmd & OBD_BRW_READ)
350                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
351                                     tot_bytes);
352         else
353                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
354                                     tot_bytes);
355
356         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
357                atomic_read(&obd->u.echo.eo_prep));
358
359         RETURN(0);
360
361 preprw_cleanup:
362         /*
363          * It is possible that we would rather handle errors by  allow
364          * any already-set-up pages to complete, rather than tearing them
365          * all down again.  I believe that this is what the in-kernel
366          * prep/commit operations do.
367          */
368         CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
369         for (i = 0; i < *pages; i++) {
370                 kunmap(res[i].lnb_page);
371                 /*
372                  * NB if this is a persistent page, __free_page() will just
373                  * lose the extra ref gained above
374                  */
375                 __free_page(res[i].lnb_page);
376                 res[i].lnb_page = NULL;
377                 atomic_dec(&obd->u.echo.eo_prep);
378         }
379
380         return rc;
381 }
382
383 static int echo_commitrw(const struct lu_env *env, int cmd,
384                          struct obd_export *export, struct obdo *oa,
385                          int objcount, struct obd_ioobj *obj,
386                          struct niobuf_remote *rb, int niocount,
387                          struct niobuf_local *res, int rc, int nob,
388                          ktime_t kstart)
389 {
390         struct obd_device *obd;
391         int pgs = 0;
392         int i;
393
394         ENTRY;
395
396         obd = export->exp_obd;
397         if (!obd)
398                 RETURN(-EINVAL);
399
400         if (rc)
401                 GOTO(commitrw_cleanup, rc);
402
403         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
404                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
405                        objcount, niocount);
406         } else {
407                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
408                        objcount, niocount);
409         }
410
411         if (niocount && !res) {
412                 CERROR("NULL res niobuf with niocount %d\n", niocount);
413                 RETURN(-EINVAL);
414         }
415
416         for (i = 0; i < objcount; i++, obj++) {
417                 int verify = (rc == 0 &&
418                              ostid_id(&obj->ioo_oid) != ECHO_PERSISTENT_OBJID &&
419                               (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
420                               (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
421                 int j;
422
423                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
424                         int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs],
425                                                    verify);
426                         if (vrc == 0)
427                                 continue;
428
429                         if (vrc == -EFAULT)
430                                 GOTO(commitrw_cleanup, rc = vrc);
431
432                         if (rc == 0)
433                                 rc = vrc;
434                 }
435         }
436
437         atomic_sub(pgs, &obd->u.echo.eo_prep);
438
439         CDEBUG(D_PAGE, "%d pages remain after commit\n",
440                atomic_read(&obd->u.echo.eo_prep));
441         RETURN(rc);
442
443 commitrw_cleanup:
444         atomic_sub(pgs, &obd->u.echo.eo_prep);
445
446         CERROR("cleaning up %d pages (%d obdos)\n",
447                niocount - pgs - 1, objcount);
448
449         while (pgs < niocount) {
450                 struct page *page = res[pgs++].lnb_page;
451
452                 if (!page)
453                         continue;
454
455                 /* NB see comment above regarding persistent pages */
456                 __free_page(page);
457                 atomic_dec(&obd->u.echo.eo_prep);
458         }
459         return rc;
460 }
461
462 LPROC_SEQ_FOPS_RO_TYPE(echo, uuid);
463 static struct lprocfs_vars lprocfs_echo_obd_vars[] = {
464         { .name =       "uuid",
465           .fops =       &echo_uuid_fops         },
466         { NULL }
467 };
468
469 const struct obd_ops echo_obd_ops = {
470         .o_owner           = THIS_MODULE,
471         .o_connect         = echo_connect,
472         .o_disconnect      = echo_disconnect,
473         .o_init_export     = echo_init_export,
474         .o_destroy_export  = echo_destroy_export,
475         .o_preprw          = echo_preprw,
476         .o_commitrw        = echo_commitrw,
477 };
478
479 /**
480  * Echo Server request handler for OST_CREATE RPC.
481  *
482  * This is part of request processing. Its simulates the object
483  * creation on OST.
484  *
485  * \param[in] tsi       target session environment for this request
486  *
487  * \retval              0 if successful
488  * \retval              negative value on error
489  */
490 static int esd_create_hdl(struct tgt_session_info *tsi)
491 {
492         const struct obdo *oa = &tsi->tsi_ost_body->oa;
493         struct obd_device *obd = tsi->tsi_exp->exp_obd;
494         struct ost_body *repbody;
495         struct obdo *rep_oa;
496
497         ENTRY;
498
499         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
500         if (!repbody)
501                 RETURN(-ENOMEM);
502
503         if (!(oa->o_mode & S_IFMT)) {
504                 CERROR("%s: no type is set in obdo!\n",
505                        tsi->tsi_exp->exp_obd->obd_name);
506                 RETURN(-ENOENT);
507         }
508
509         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
510                 CERROR("%s: invalid o_valid in obdo: %#llx\n",
511                        tsi->tsi_exp->exp_obd->obd_name, oa->o_valid);
512                 RETURN(-EINVAL);
513         }
514
515         rep_oa = &repbody->oa;
516
517         if (!fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
518                 CERROR("%s: invalid seq %#llx\n",
519                        tsi->tsi_exp->exp_obd->obd_name, ostid_seq(&oa->o_oi));
520                 return -EINVAL;
521         }
522
523         ostid_set_seq_echo(&rep_oa->o_oi);
524         ostid_set_id(&rep_oa->o_oi, echo_next_id(obd));
525
526         CDEBUG(D_INFO, "%s: Create object "DOSTID"\n",
527                tsi->tsi_exp->exp_obd->obd_name, POSTID(&rep_oa->o_oi));
528
529         rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
530
531         RETURN(0);
532 }
533
534 /**
535  * Echo Server request handler for OST_DESTROY RPC.
536  *
537  * This is Echo Server part of request handling. It simulates the objects
538  * destroy on OST.
539  *
540  * \param[in] tsi       target session environment for this request
541  *
542  * \retval              0 if successful
543  * \retval              negative value on error
544  */
545 static int esd_destroy_hdl(struct tgt_session_info *tsi)
546 {
547         const struct obdo *oa = &tsi->tsi_ost_body->oa;
548         struct obd_device *obd = tsi->tsi_exp->exp_obd;
549         struct ost_body *repbody;
550         u64 oid;
551
552         ENTRY;
553
554         oid = ostid_id(&oa->o_oi);
555         LASSERT(oid != 0);
556
557         if (!(oa->o_valid & OBD_MD_FLID)) {
558                 CERROR("%s: obdo missing FLID valid flag: %#llx\n",
559                        tsi->tsi_exp->exp_obd->obd_name, oa->o_valid);
560                 RETURN(-EINVAL);
561         }
562
563         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
564
565         if (ostid_id(&oa->o_oi) > obd->u.echo.eo_lastino ||
566             ostid_id(&oa->o_oi) < ECHO_INIT_OID) {
567                 CERROR("%s: bad objid to destroy: "DOSTID"\n",
568                        tsi->tsi_exp->exp_obd->obd_name, POSTID(&oa->o_oi));
569                 RETURN(-EINVAL);
570         }
571
572         CDEBUG(D_INFO, "%s: Destroy object "DOSTID"\n",
573                tsi->tsi_exp->exp_obd->obd_name, POSTID(&oa->o_oi));
574
575         repbody->oa.o_oi = oa->o_oi;
576         RETURN(0);
577 }
578
579 /**
580  * Echo Server request handler for OST_GETATTR RPC.
581  *
582  * This is Echo Server part of request handling. It returns an object
583  * attributes to the client. All objects have the same attributes in
584  * Echo Server.
585  *
586  * \param[in] tsi       target session environment for this request
587  *
588  * \retval              0 if successful
589  * \retval              negative value on error
590  */
591 static int esd_getattr_hdl(struct tgt_session_info *tsi)
592 {
593         const struct obdo *oa = &tsi->tsi_ost_body->oa;
594         struct obd_device *obd = tsi->tsi_exp->exp_obd;
595         struct ost_body *repbody;
596
597         ENTRY;
598
599         if (!(oa->o_valid & OBD_MD_FLID)) {
600                 CERROR("%s: obdo missing FLID valid flag: %#llx\n",
601                        tsi->tsi_exp->exp_obd->obd_name, oa->o_valid);
602                 RETURN(-EINVAL);
603         }
604
605         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
606         if (!repbody)
607                 RETURN(-ENOMEM);
608
609         repbody->oa.o_oi = oa->o_oi;
610         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
611
612         obdo_cpy_md(&repbody->oa, &obd->u.echo.eo_oa, oa->o_valid);
613
614         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
615         repbody->oa.o_flags = OBD_FL_FLUSH;
616
617         RETURN(0);
618 }
619
620 /**
621  * Echo Server request handler for OST_SETATTR RPC.
622  *
623  * This is Echo Server part of request handling. It sets common
624  * attributes from request to the Echo Server objects.
625  *
626  * \param[in] tsi       target session environment for this request
627  *
628  * \retval              0 if successful
629  * \retval              negative value on error
630  */
631 static int esd_setattr_hdl(struct tgt_session_info *tsi)
632 {
633         struct ost_body *body = tsi->tsi_ost_body;
634         struct obd_device *obd = tsi->tsi_exp->exp_obd;
635         struct ost_body *repbody;
636
637         ENTRY;
638
639         if (!(body->oa.o_valid & OBD_MD_FLID)) {
640                 CERROR("%s: obdo missing FLID valid flag: %#llx\n",
641                        tsi->tsi_exp->exp_obd->obd_name,
642                        body->oa.o_valid);
643                 RETURN(-EINVAL);
644         }
645
646         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
647         if (!repbody)
648                 RETURN(-ENOMEM);
649
650         repbody->oa.o_oi = body->oa.o_oi;
651         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
652
653         obd->u.echo.eo_oa = body->oa;
654
655         RETURN(0);
656 }
657
658 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
659 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
660 #define OST_BRW_READ    OST_READ
661 #define OST_BRW_WRITE   OST_WRITE
662
663 /**
664  * Table of Echo Server specific request handlers
665  *
666  * This table contains all opcodes accepted by Echo Server and
667  * specifies handlers for them. The tgt_request_handler()
668  * uses such table from each target to process incoming
669  * requests.
670  */
671 static struct tgt_handler esd_tgt_handlers[] = {
672 TGT_RPC_HANDLER(OST_FIRST_OPC, 0, OST_CONNECT, tgt_connect,
673                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
674 TGT_RPC_HANDLER(OST_FIRST_OPC, 0, OST_DISCONNECT, tgt_disconnect,
675                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
676 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_GETATTR, esd_getattr_hdl),
677 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_SETATTR,
678             esd_setattr_hdl),
679 TGT_OST_HDL(HAS_REPLY | IS_MUTABLE, OST_CREATE, esd_create_hdl),
680 TGT_OST_HDL(HAS_REPLY | IS_MUTABLE, OST_DESTROY, esd_destroy_hdl),
681 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read),
682 TGT_OST_HDL(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write),
683 };
684
685 static struct tgt_opc_slice esd_common_slice[] = {
686         {
687                 .tos_opc_start  = OST_FIRST_OPC,
688                 .tos_opc_end    = OST_LAST_OPC,
689                 .tos_hs         = esd_tgt_handlers
690         },
691         {
692                 .tos_opc_start  = OBD_FIRST_OPC,
693                 .tos_opc_end    = OBD_LAST_OPC,
694                 .tos_hs         = tgt_obd_handlers
695         },
696         {
697                 .tos_opc_start  = LDLM_FIRST_OPC,
698                 .tos_opc_end    = LDLM_LAST_OPC,
699                 .tos_hs         = tgt_dlm_handlers
700         },
701         {
702                 .tos_opc_start  = SEC_FIRST_OPC,
703                 .tos_opc_end    = SEC_LAST_OPC,
704                 .tos_hs         = tgt_sec_ctx_handlers
705         },
706         {
707                 .tos_hs         = NULL
708         }
709 };
710
711 /**
712  * lu_device_operations matrix for ECHO SRV device is NULL,
713  * this device is just serving incoming requests immediately
714  * without building a stack of lu_devices.
715  */
716 static const struct lu_device_operations echo_srv_lu_ops = { 0 };
717
718 /**
719  * Initialize Echo Server device with parameters in the config log \a cfg.
720  *
721  * This is the main starting point of Echo Server initialization. It fills all
722  * parameters with their initial values and starts Echo Server.
723  *
724  * \param[in] env       execution environment
725  * \param[in] m         Echo Server device
726  * \param[in] ldt       LU device type of Echo Server
727  * \param[in] cfg       configuration log
728  *
729  * \retval              0 if successful
730  * \retval              negative value on error
731  */
732 static int echo_srv_init0(const struct lu_env *env,
733                           struct echo_srv_device *esd,
734                           struct lu_device_type *ldt, struct lustre_cfg *cfg)
735 {
736         const char *dev = lustre_cfg_string(cfg, 0);
737         struct obd_device *obd;
738         char ns_name[48];
739         int rc;
740
741         ENTRY;
742
743         obd = class_name2obd(dev);
744         if (!obd) {
745                 CERROR("Cannot find obd with name %s\n", dev);
746                 RETURN(-ENODEV);
747         }
748
749         spin_lock_init(&obd->u.echo.eo_lock);
750         obd->u.echo.eo_lastino = ECHO_INIT_OID;
751
752         esd->esd_dev.ld_ops = &echo_srv_lu_ops;
753         esd->esd_dev.ld_obd = obd;
754         /* set this lu_device to obd, because error handling need it */
755         obd->obd_lu_dev = &esd->esd_dev;
756
757         /* No connection accepted until configurations will finish */
758         spin_lock(&obd->obd_dev_lock);
759         obd->obd_no_conn = 1;
760         spin_unlock(&obd->obd_dev_lock);
761
762         /* non-replayable target */
763         obd->obd_replayable = 0;
764
765         snprintf(ns_name, sizeof(ns_name), "echotgt-%s", obd->obd_uuid.uuid);
766         obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
767                                                 LDLM_NAMESPACE_SERVER,
768                                                 LDLM_NAMESPACE_MODEST,
769                                                 LDLM_NS_TYPE_OST);
770         if (IS_ERR(obd->obd_namespace)) {
771                 rc = PTR_ERR(obd->obd_namespace);
772                 CERROR("%s: unable to create server namespace: rc = %d\n",
773                        obd->obd_name, rc);
774                 obd->obd_namespace = NULL;
775                 RETURN(rc);
776         }
777
778         obd->obd_vars = lprocfs_echo_obd_vars;
779         if (!lprocfs_obd_setup(obd, true) &&
780             lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
781                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
782                                      LPROCFS_CNTR_AVGMINMAX,
783                                      "read_bytes", "bytes");
784                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
785                                      LPROCFS_CNTR_AVGMINMAX,
786                                      "write_bytes", "bytes");
787         }
788
789         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
790                            "echo_ldlm_cb_client", &obd->obd_ldlm_client);
791
792         rc = tgt_init(env, &esd->esd_lut, obd, NULL, esd_common_slice,
793                       OBD_FAIL_OST_ALL_REQUEST_NET,
794                       OBD_FAIL_OST_ALL_REPLY_NET);
795         if (rc)
796                 GOTO(err_out, rc);
797
798         spin_lock(&obd->obd_dev_lock);
799         obd->obd_no_conn = 0;
800         spin_unlock(&obd->obd_dev_lock);
801
802         RETURN(0);
803
804 err_out:
805         ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
806         obd->obd_namespace = NULL;
807
808         lprocfs_obd_cleanup(obd);
809         lprocfs_free_obd_stats(obd);
810         RETURN(rc);
811 }
812
813 /**
814  * Stop the Echo Server device.
815  *
816  * This function stops the Echo Server device and all its subsystems.
817  * This is the end of Echo Server lifecycle.
818  *
819  * \param[in] env       execution environment
820  * \param[in] esd               ESD device
821  */
822 static void echo_srv_fini(const struct lu_env *env,
823                           struct echo_srv_device *esd)
824 {
825         struct obd_device *obd = echo_srv_obd(esd);
826         struct lu_device *d = &esd->esd_dev;
827         int leaked;
828
829         ENTRY;
830
831         class_disconnect_exports(obd);
832         if (obd->obd_namespace)
833                 ldlm_namespace_free_prior(obd->obd_namespace, NULL,
834                                           obd->obd_force);
835
836         obd_exports_barrier(obd);
837         obd_zombie_barrier();
838
839         tgt_fini(env, &esd->esd_lut);
840
841         if (obd->obd_namespace) {
842                 ldlm_namespace_free_post(obd->obd_namespace);
843                 obd->obd_namespace = NULL;
844         }
845
846         lprocfs_obd_cleanup(obd);
847         lprocfs_free_obd_stats(obd);
848
849         leaked = atomic_read(&obd->u.echo.eo_prep);
850         if (leaked != 0)
851                 CERROR("%d prep/commitrw pages leaked\n", leaked);
852
853         LASSERT(atomic_read(&d->ld_ref) == 0);
854         EXIT;
855 }
856
857 /**
858  * Implementation of lu_device_type_operations::ldto_device_fini.
859  *
860  * Finalize device. Dual to echo_srv_device_init(). It is called from
861  * obd_precleanup() and stops the current device.
862  *
863  * \param[in] env       execution environment
864  * \param[in] d         LU device of ESD
865  *
866  * \retval              NULL
867  */
868 static struct lu_device *echo_srv_device_fini(const struct lu_env *env,
869                                               struct lu_device *d)
870 {
871         ENTRY;
872         echo_srv_fini(env, echo_srv_dev(d));
873         RETURN(NULL);
874 }
875
876 /**
877  * Implementation of lu_device_type_operations::ldto_device_free.
878  *
879  * Free Echo Server device. Dual to echo_srv_device_alloc().
880  *
881  * \param[in] env       execution environment
882  * \param[in] d         LU device of ESD
883  *
884  * \retval              NULL
885  */
886 static struct lu_device *echo_srv_device_free(const struct lu_env *env,
887                                               struct lu_device *d)
888 {
889         struct echo_srv_device *esd = echo_srv_dev(d);
890
891         lu_device_fini(&esd->esd_dev);
892         OBD_FREE_PTR(esd);
893         RETURN(NULL);
894 }
895
896 /**
897  * Implementation of lu_device_type_operations::ldto_device_alloc.
898  *
899  * This function allocates the new Echo Server device. It is called from
900  * obd_setup() if OBD device had lu_device_type defined.
901  *
902  * \param[in] env       execution environment
903  * \param[in] t         lu_device_type of ESD device
904  * \param[in] cfg       configuration log
905  *
906  * \retval              pointer to the lu_device of just allocated OFD
907  * \retval              ERR_PTR of return value on error
908  */
909 static struct lu_device *echo_srv_device_alloc(const struct lu_env *env,
910                                                struct lu_device_type *t,
911                                                struct lustre_cfg *cfg)
912 {
913         struct echo_srv_device *esd;
914         struct lu_device *l;
915         int rc;
916
917         OBD_ALLOC_PTR(esd);
918         if (!esd)
919                 return ERR_PTR(-ENOMEM);
920
921         l = &esd->esd_dev;
922         lu_device_init(l, t);
923         rc = echo_srv_init0(env, esd, t, cfg);
924         if (rc != 0) {
925                 echo_srv_device_free(env, l);
926                 l = ERR_PTR(rc);
927         }
928
929         return l;
930 }
931
932 static const struct lu_device_type_operations echo_srv_type_ops = {
933         .ldto_device_alloc = echo_srv_device_alloc,
934         .ldto_device_free = echo_srv_device_free,
935         .ldto_device_fini = echo_srv_device_fini
936 };
937
938 struct lu_device_type echo_srv_type = {
939         .ldt_tags = LU_DEVICE_DT,
940         .ldt_name = LUSTRE_ECHO_NAME,
941         .ldt_ops = &echo_srv_type_ops,
942         .ldt_ctx_tags = LCT_DT_THREAD,
943 };
944
945 void echo_persistent_pages_fini(void)
946 {
947         int i;
948
949         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
950                 if (echo_persistent_pages[i]) {
951                         __free_page(echo_persistent_pages[i]);
952                         echo_persistent_pages[i] = NULL;
953                 }
954 }
955
956 int echo_persistent_pages_init(void)
957 {
958         struct page *pg;
959         int          i;
960
961         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
962                 gfp_t gfp_mask = (i < ECHO_PERSISTENT_PAGES / 2) ?
963                         GFP_KERNEL : GFP_HIGHUSER;
964
965                 pg = alloc_page(gfp_mask);
966                 if (!pg) {
967                         echo_persistent_pages_fini();
968                         return -ENOMEM;
969                 }
970
971                 memset(kmap(pg), 0, PAGE_SIZE);
972                 kunmap(pg);
973                 /* set mapping so page is not considered encrypted */
974                 pg->mapping = ECHO_MAPPING_UNENCRYPTED;
975
976                 echo_persistent_pages[i] = pg;
977         }
978
979         return 0;
980 }