Whamcloud - gitweb
LU-8654 obd: access ocd_connect_flags2 only when present
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdecho/echo.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_ECHO
43
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>
49
50 #include "echo_internal.h"
51
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
56
57 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> PAGE_SHIFT)
58 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
59
60 enum {
61         LPROC_ECHO_READ_BYTES = 1,
62         LPROC_ECHO_WRITE_BYTES = 2,
63         LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
64 };
65
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,
69                         void *localdata)
70 {
71         struct lustre_handle conn = { 0 };
72         int rc;
73
74         data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
75
76         if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
77                 data->ocd_connect_flags2 &= ECHO_CONNECT_SUPPORTED2;
78
79         rc = class_connect(&conn, obd, cluuid);
80         if (rc) {
81                 CERROR("can't connect %d\n", rc);
82                 return rc;
83         }
84         *exp = class_conn2export(&conn);
85
86         return 0;
87 }
88
89 static int echo_disconnect(struct obd_export *exp)
90 {
91         LASSERT (exp != NULL);
92
93         return server_disconnect_export(exp);
94 }
95
96 static int echo_init_export(struct obd_export *exp)
97 {
98         return ldlm_init_export(exp);
99 }
100
101 static int echo_destroy_export(struct obd_export *exp)
102 {
103         ENTRY;
104
105         target_destroy_export(exp);
106         ldlm_destroy_export(exp);
107
108         RETURN(0);
109 }
110
111 static u64 echo_next_id(struct obd_device *obddev)
112 {
113         u64 id;
114
115         spin_lock(&obddev->u.echo.eo_lock);
116         id = ++obddev->u.echo.eo_lastino;
117         spin_unlock(&obddev->u.echo.eo_lock);
118
119         return id;
120 }
121
122 static int echo_create(const struct lu_env *env, struct obd_export *exp,
123                        struct obdo *oa)
124 {
125         struct obd_device *obd = class_exp2obd(exp);
126
127         if (!obd) {
128                 CERROR("invalid client cookie %#llx\n",
129                        exp->exp_handle.h_cookie);
130                 return -EINVAL;
131         }
132
133         if (!(oa->o_mode & S_IFMT)) {
134                 CERROR("echo obd: no type!\n");
135                 return -ENOENT;
136         }
137
138         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
139                 CERROR("invalid o_valid %#llx\n", oa->o_valid);
140                 return -EINVAL;
141         }
142
143         ostid_set_seq_echo(&oa->o_oi);
144         ostid_set_id(&oa->o_oi, echo_next_id(obd));
145         oa->o_valid = OBD_MD_FLID;
146
147         return 0;
148 }
149
150 static int echo_destroy(const struct lu_env *env, struct obd_export *exp,
151                         struct obdo *oa)
152 {
153         struct obd_device *obd = class_exp2obd(exp);
154
155         ENTRY;
156         if (!obd) {
157                 CERROR("invalid client cookie %#llx\n",
158                        exp->exp_handle.h_cookie);
159                 RETURN(-EINVAL);
160         }
161
162         if (!(oa->o_valid & OBD_MD_FLID)) {
163                 CERROR("obdo missing FLID valid flag: %#llx\n", oa->o_valid);
164                 RETURN(-EINVAL);
165         }
166
167         if (ostid_id(&oa->o_oi) > obd->u.echo.eo_lastino ||
168             ostid_id(&oa->o_oi) < ECHO_INIT_OID) {
169                 CERROR("bad destroy objid: "DOSTID"\n", POSTID(&oa->o_oi));
170                 RETURN(-EINVAL);
171         }
172
173         RETURN(0);
174 }
175
176 static int echo_getattr(const struct lu_env *env, struct obd_export *exp,
177                         struct obdo *oa)
178 {
179         struct obd_device *obd = class_exp2obd(exp);
180         u64 id = ostid_id(&oa->o_oi);
181
182         ENTRY;
183         if (!obd) {
184                 CERROR("invalid client cookie %#llx\n",
185                        exp->exp_handle.h_cookie);
186                 RETURN(-EINVAL);
187         }
188
189         if (!(oa->o_valid & OBD_MD_FLID)) {
190                 CERROR("obdo missing FLID valid flag: %#llx\n", oa->o_valid);
191                 RETURN(-EINVAL);
192         }
193
194         obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
195         ostid_set_seq_echo(&oa->o_oi);
196         ostid_set_id(&oa->o_oi, id);
197
198         RETURN(0);
199 }
200
201 static int echo_setattr(const struct lu_env *env, struct obd_export *exp,
202                         struct obdo *oa)
203 {
204         struct obd_device *obd = class_exp2obd(exp);
205
206         ENTRY;
207         if (!obd) {
208                 CERROR("invalid client cookie %#llx\n",
209                        exp->exp_handle.h_cookie);
210                 RETURN(-EINVAL);
211         }
212
213         if (!(oa->o_valid & OBD_MD_FLID)) {
214                 CERROR("obdo missing FLID valid flag: %#llx\n", oa->o_valid);
215                 RETURN(-EINVAL);
216         }
217
218         obd->u.echo.eo_oa = *oa;
219
220         RETURN(0);
221 }
222
223 static void
224 echo_page_debug_setup(struct page *page, int rw, u64 id,
225                       __u64 offset, int len)
226 {
227         int   page_offset = offset & ~PAGE_MASK;
228         char *addr        = ((char *)kmap(page)) + page_offset;
229
230         if (len % OBD_ECHO_BLOCK_SIZE != 0)
231                 CERROR("Unexpected block size %d\n", len);
232
233         while (len > 0) {
234                 if (rw & OBD_BRW_READ)
235                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
236                                           offset, id);
237                 else
238                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
239                                           0xecc0ecc0ecc0ecc0ULL,
240                                           0xecc0ecc0ecc0ecc0ULL);
241
242                 addr   += OBD_ECHO_BLOCK_SIZE;
243                 offset += OBD_ECHO_BLOCK_SIZE;
244                 len    -= OBD_ECHO_BLOCK_SIZE;
245         }
246
247         kunmap(page);
248 }
249
250 static int
251 echo_page_debug_check(struct page *page, u64 id,
252                       __u64 offset, int len)
253 {
254         int   page_offset = offset & ~PAGE_MASK;
255         char *addr        = ((char *)kmap(page)) + page_offset;
256         int   rc          = 0;
257         int   rc2;
258
259         if (len % OBD_ECHO_BLOCK_SIZE != 0)
260                 CERROR("Unexpected block size %d\n", len);
261
262         while (len > 0) {
263                 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
264                                         offset, id);
265
266                 if (rc2 != 0 && rc == 0)
267                         rc = rc2;
268
269                 addr   += OBD_ECHO_BLOCK_SIZE;
270                 offset += OBD_ECHO_BLOCK_SIZE;
271                 len    -= OBD_ECHO_BLOCK_SIZE;
272         }
273
274         kunmap(page);
275
276         return rc;
277 }
278
279 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
280                              struct niobuf_remote *nb, int *pages,
281                              struct niobuf_local *lb, int cmd, int *left)
282 {
283         gfp_t gfp_mask = (ostid_id(&obj->ioo_oid) & 1) ?
284                         GFP_HIGHUSER : GFP_KERNEL;
285         int ispersistent = ostid_id(&obj->ioo_oid) == ECHO_PERSISTENT_OBJID;
286         int debug_setup = (!ispersistent &&
287                            (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
288                            (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
289         struct niobuf_local *res = lb;
290         u64 offset = nb->rnb_offset;
291         int len = nb->rnb_len;
292
293         while (len > 0) {
294                 int plen = PAGE_SIZE - (offset & (PAGE_SIZE-1));
295                 if (len < plen)
296                         plen = len;
297
298                 /* check for local buf overflow */
299                 if (*left == 0)
300                         return -EINVAL;
301
302                 res->lnb_file_offset = offset;
303                 res->lnb_len = plen;
304                 LASSERT((res->lnb_file_offset & ~PAGE_MASK) +
305                         res->lnb_len <= PAGE_SIZE);
306
307                 if (ispersistent &&
308                     ((res->lnb_file_offset >> PAGE_SHIFT) <
309                       ECHO_PERSISTENT_PAGES)) {
310                         res->lnb_page =
311                                 echo_persistent_pages[res->lnb_file_offset >>
312                                                       PAGE_SHIFT];
313                         /* Take extra ref so __free_pages() can be called OK */
314                         get_page(res->lnb_page);
315                 } else {
316                         res->lnb_page = alloc_page(gfp_mask);
317                         if (res->lnb_page == NULL) {
318                                 CERROR("can't get page for id " DOSTID"\n",
319                                        POSTID(&obj->ioo_oid));
320                                 return -ENOMEM;
321                         }
322                 }
323
324                 CDEBUG(D_PAGE, "$$$$ get page %p @ %llu for %d\n",
325                        res->lnb_page, res->lnb_file_offset, res->lnb_len);
326
327                 if (cmd & OBD_BRW_READ)
328                         res->lnb_rc = res->lnb_len;
329
330                 if (debug_setup)
331                         echo_page_debug_setup(res->lnb_page, cmd,
332                                               ostid_id(&obj->ioo_oid),
333                                               res->lnb_file_offset,
334                                               res->lnb_len);
335
336                 offset += plen;
337                 len -= plen;
338                 res++;
339
340                 (*left)--;
341                 (*pages)++;
342         }
343
344         return 0;
345 }
346
347 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
348                             struct niobuf_remote *rb, int *pgs,
349                             struct niobuf_local *lb, int verify)
350 {
351         struct niobuf_local *res = lb;
352         u64 start = rb->rnb_offset >> PAGE_SHIFT;
353         u64 end   = (rb->rnb_offset + rb->rnb_len + PAGE_SIZE - 1) >>
354                     PAGE_SHIFT;
355         int     count  = (int)(end - start);
356         int     rc     = 0;
357         int     i;
358
359         for (i = 0; i < count; i++, (*pgs) ++, res++) {
360                 struct page *page = res->lnb_page;
361                 void       *addr;
362
363                 if (page == NULL) {
364                         CERROR("null page objid %llu:%p, buf %d/%d\n",
365                                ostid_id(&obj->ioo_oid), page, i,
366                                obj->ioo_bufcnt);
367                         return -EFAULT;
368                 }
369
370                 addr = kmap(page);
371
372                 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@%llu\n",
373                        res->lnb_page, addr, res->lnb_file_offset);
374
375                 if (verify) {
376                         int vrc = echo_page_debug_check(page,
377                                                         ostid_id(&obj->ioo_oid),
378                                                         res->lnb_file_offset,
379                                                         res->lnb_len);
380                         /* check all the pages always */
381                         if (vrc != 0 && rc == 0)
382                                 rc = vrc;
383                 }
384
385                 kunmap(page);
386                 /* NB see comment above regarding persistent pages */
387                 __free_page(page);
388         }
389
390         return rc;
391 }
392
393 static int echo_preprw(const struct lu_env *env, int cmd,
394                        struct obd_export *export, struct obdo *oa,
395                        int objcount, struct obd_ioobj *obj,
396                        struct niobuf_remote *nb, int *pages,
397                        struct niobuf_local *res)
398 {
399         struct obd_device *obd;
400         int tot_bytes = 0;
401         int rc = 0;
402         int i, left;
403         ENTRY;
404
405         obd = export->exp_obd;
406         if (obd == NULL)
407                 RETURN(-EINVAL);
408
409         /* Temp fix to stop falling foul of osc_announce_cached() */
410         oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
411
412         memset(res, 0, sizeof(*res) * *pages);
413
414         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
415                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
416
417         left = *pages;
418         *pages = 0;
419
420         for (i = 0; i < objcount; i++, obj++) {
421                 int j;
422
423                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
424
425                         rc = echo_map_nb_to_lb(oa, obj, nb, pages,
426                                                res + *pages, cmd, &left);
427                         if (rc)
428                                 GOTO(preprw_cleanup, rc);
429
430                         tot_bytes += nb->rnb_len;
431                 }
432         }
433
434         atomic_add(*pages, &obd->u.echo.eo_prep);
435
436         if (cmd & OBD_BRW_READ)
437                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
438                                     tot_bytes);
439         else
440                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
441                                     tot_bytes);
442
443         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
444                atomic_read(&obd->u.echo.eo_prep));
445
446         RETURN(0);
447
448 preprw_cleanup:
449         /* It is possible that we would rather handle errors by  allow
450          * any already-set-up pages to complete, rather than tearing them
451          * all down again.  I believe that this is what the in-kernel
452          * prep/commit operations do.
453          */
454         CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
455         for (i = 0; i < *pages; i++) {
456                 kunmap(res[i].lnb_page);
457                 /* NB if this is a persistent page, __free_page() will just
458                  * lose the extra ref gained above */
459                 __free_page(res[i].lnb_page);
460                 res[i].lnb_page = NULL;
461                 atomic_dec(&obd->u.echo.eo_prep);
462         }
463
464         return rc;
465 }
466
467 static int echo_commitrw(const struct lu_env *env, int cmd,
468                          struct obd_export *export, struct obdo *oa,
469                          int objcount, struct obd_ioobj *obj,
470                          struct niobuf_remote *rb, int niocount,
471                          struct niobuf_local *res, int rc)
472 {
473         struct obd_device *obd;
474         int pgs = 0;
475         int i;
476         ENTRY;
477
478         obd = export->exp_obd;
479         if (obd == NULL)
480                 RETURN(-EINVAL);
481
482         if (rc)
483                 GOTO(commitrw_cleanup, rc);
484
485         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
486                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
487                        objcount, niocount);
488         } else {
489                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
490                        objcount, niocount);
491         }
492
493         if (niocount && res == NULL) {
494                 CERROR("NULL res niobuf with niocount %d\n", niocount);
495                 RETURN(-EINVAL);
496         }
497
498         for (i = 0; i < objcount; i++, obj++) {
499                 int verify = (rc == 0 &&
500                              ostid_id(&obj->ioo_oid) != ECHO_PERSISTENT_OBJID &&
501                               (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
502                               (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
503                 int j;
504
505                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
506                         int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs],
507                                                    verify);
508                         if (vrc == 0)
509                                 continue;
510
511                         if (vrc == -EFAULT)
512                                 GOTO(commitrw_cleanup, rc = vrc);
513
514                         if (rc == 0)
515                                 rc = vrc;
516                 }
517
518         }
519
520         atomic_sub(pgs, &obd->u.echo.eo_prep);
521
522         CDEBUG(D_PAGE, "%d pages remain after commit\n",
523                atomic_read(&obd->u.echo.eo_prep));
524         RETURN(rc);
525
526 commitrw_cleanup:
527         atomic_sub(pgs, &obd->u.echo.eo_prep);
528
529         CERROR("cleaning up %d pages (%d obdos)\n",
530                niocount - pgs - 1, objcount);
531
532         while (pgs < niocount) {
533                 struct page *page = res[pgs++].lnb_page;
534
535                 if (page == NULL)
536                         continue;
537
538                 /* NB see comment above regarding persistent pages */
539                 __free_page(page);
540                 atomic_dec(&obd->u.echo.eo_prep);
541         }
542         return rc;
543 }
544
545 LPROC_SEQ_FOPS_RO_TYPE(echo, uuid);
546 static struct lprocfs_vars lprocfs_echo_obd_vars[] = {
547         { .name =       "uuid",
548           .fops =       &echo_uuid_fops         },
549         { NULL }
550 };
551
552 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
553 {
554         int                     rc;
555         __u64                   lock_flags = 0;
556         struct ldlm_res_id      res_id = {.name = {1}};
557         char                    ns_name[48];
558         ENTRY;
559
560         obd->u.echo.eo_obt.obt_magic = OBT_MAGIC;
561         spin_lock_init(&obd->u.echo.eo_lock);
562         obd->u.echo.eo_lastino = ECHO_INIT_OID;
563
564         sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
565         obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
566                                                 LDLM_NAMESPACE_SERVER,
567                                                 LDLM_NAMESPACE_MODEST,
568                                                 LDLM_NS_TYPE_OST);
569         if (obd->obd_namespace == NULL) {
570                 LBUG();
571                 RETURN(-ENOMEM);
572         }
573
574         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
575                                     NULL, LCK_NL, &lock_flags, NULL,
576                                     ldlm_completion_ast, NULL, NULL, 0,
577                                     LVB_T_NONE, NULL, &obd->u.echo.eo_nl_lock);
578         LASSERT (rc == ELDLM_OK);
579
580         obd->obd_vars = lprocfs_echo_obd_vars;
581         if (lprocfs_obd_setup(obd) == 0 &&
582             lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
583                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
584                                      LPROCFS_CNTR_AVGMINMAX,
585                                      "read_bytes", "bytes");
586                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
587                                      LPROCFS_CNTR_AVGMINMAX,
588                                      "write_bytes", "bytes");
589         }
590
591         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
592                            "echo_ldlm_cb_client", &obd->obd_ldlm_client);
593         RETURN(0);
594 }
595
596 static int echo_cleanup(struct obd_device *obd)
597 {
598         int leaked;
599         ENTRY;
600
601         lprocfs_obd_cleanup(obd);
602         lprocfs_free_obd_stats(obd);
603
604         ldlm_lock_decref(&obd->u.echo.eo_nl_lock, LCK_NL);
605
606         /* XXX Bug 3413; wait for a bit to ensure the BL callback has
607          * happened before calling ldlm_namespace_free() */
608         set_current_state(TASK_UNINTERRUPTIBLE);
609         schedule_timeout(cfs_time_seconds(1));
610
611         ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
612         obd->obd_namespace = NULL;
613
614         leaked = atomic_read(&obd->u.echo.eo_prep);
615         if (leaked != 0)
616                 CERROR("%d prep/commitrw pages leaked\n", leaked);
617
618         RETURN(0);
619 }
620
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
635 };
636
637 void echo_persistent_pages_fini(void)
638 {
639         int i;
640
641         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
642                 if (echo_persistent_pages[i] != NULL) {
643                         __free_page(echo_persistent_pages[i]);
644                         echo_persistent_pages[i] = NULL;
645                 }
646 }
647
648 int echo_persistent_pages_init(void)
649 {
650         struct page *pg;
651         int          i;
652
653         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
654                 gfp_t gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
655                         GFP_KERNEL : GFP_HIGHUSER;
656
657                 pg = alloc_page(gfp_mask);
658                 if (pg == NULL) {
659                         echo_persistent_pages_fini();
660                         return -ENOMEM;
661                 }
662
663                 memset(kmap(pg), 0, PAGE_SIZE);
664                 kunmap(pg);
665
666                 echo_persistent_pages[i] = pg;
667         }
668
669         return 0;
670 }