Whamcloud - gitweb
197d3cdda5f1262d0a9af4317141b0d2f05c4a8c
[fs/lustre-release.git] / lustre / obdecho / echo.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Andreas Dilger <adilger@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30
31 #define DEBUG_SUBSYSTEM S_ECHO
32
33 #include <obd_support.h>
34 #include <obd_class.h>
35 #include <obd_echo.h>
36 #include <lustre_debug.h>
37 #include <lustre_dlm.h>
38 #include <lprocfs_status.h>
39
40 #define ECHO_INIT_OBJID      0x1000000000000000ULL
41 #define ECHO_HANDLE_MAGIC    0xabcd0123fedc9876ULL
42
43 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
44 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
45
46 enum {
47         LPROC_ECHO_READ_BYTES = 1,
48         LPROC_ECHO_WRITE_BYTES = 2,
49         LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
50 };
51
52 static int echo_connect(const struct lu_env *env,
53                         struct lustre_handle *conn, struct obd_device *obd,
54                         struct obd_uuid *cluuid, struct obd_connect_data *data,
55                         void *localdata)
56 {
57         data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
58         return class_connect(conn, obd, cluuid);
59 }
60
61 static int echo_disconnect(struct obd_export *exp)
62 {
63         LASSERT (exp != NULL);
64
65         ldlm_cancel_locks_for_export(exp);
66
67         /* complete all outstanding replies */
68         spin_lock(&exp->exp_lock);
69         while (!list_empty(&exp->exp_outstanding_replies)) {
70                 struct ptlrpc_reply_state *rs =
71                         list_entry(exp->exp_outstanding_replies.next,
72                                    struct ptlrpc_reply_state, rs_exp_list);
73                 struct ptlrpc_service *svc = rs->rs_service;
74
75                 spin_lock(&svc->srv_lock);
76                 list_del_init(&rs->rs_exp_list);
77                 ptlrpc_schedule_difficult_reply(rs);
78                 spin_unlock(&svc->srv_lock);
79         }
80         spin_unlock(&exp->exp_lock);
81
82         return class_disconnect(exp);
83 }
84
85 static int echo_destroy_export(struct obd_export *exp)
86 {
87         ENTRY;
88
89         target_destroy_export(exp);
90
91         RETURN(0);
92 }
93
94  static __u64 echo_next_id(struct obd_device *obddev)
95 {
96         obd_id id;
97
98         spin_lock(&obddev->u.echo.eo_lock);
99         id = ++obddev->u.echo.eo_lastino;
100         spin_unlock(&obddev->u.echo.eo_lock);
101
102         return id;
103 }
104
105 int echo_create(struct obd_export *exp, struct obdo *oa,
106                 struct lov_stripe_md **ea, struct obd_trans_info *oti)
107 {
108         struct obd_device *obd = class_exp2obd(exp);
109
110         if (!obd) {
111                 CERROR("invalid client cookie "LPX64"\n",
112                        exp->exp_handle.h_cookie);
113                 return -EINVAL;
114         }
115
116         if (!(oa->o_mode && S_IFMT)) {
117                 CERROR("echo obd: no type!\n");
118                 return -ENOENT;
119         }
120
121         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
122                 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
123                 return -EINVAL;
124         }
125
126         oa->o_id = echo_next_id(obd);
127         oa->o_valid = OBD_MD_FLID;
128
129         return 0;
130 }
131
132 int echo_destroy(struct obd_export *exp, struct obdo *oa,
133                  struct lov_stripe_md *ea, struct obd_trans_info *oti,
134                  struct obd_export *md_exp)
135 {
136         struct obd_device *obd = class_exp2obd(exp);
137
138         ENTRY;
139         if (!obd) {
140                 CERROR("invalid client cookie "LPX64"\n",
141                        exp->exp_handle.h_cookie);
142                 RETURN(-EINVAL);
143         }
144
145         if (!(oa->o_valid & OBD_MD_FLID)) {
146                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
147                 RETURN(-EINVAL);
148         }
149
150         if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
151                 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
152                 RETURN(-EINVAL);
153         }
154
155         RETURN(0);
156 }
157
158 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
159 {
160         struct obd_device *obd = class_exp2obd(exp);
161         obd_id id = oinfo->oi_oa->o_id;
162
163         ENTRY;
164         if (!obd) {
165                 CERROR("invalid client cookie "LPX64"\n",
166                        exp->exp_handle.h_cookie);
167                 RETURN(-EINVAL);
168         }
169
170         if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
171                 CERROR("obdo missing FLID valid flag: "LPX64"\n",
172                        oinfo->oi_oa->o_valid);
173                 RETURN(-EINVAL);
174         }
175
176         obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
177         oinfo->oi_oa->o_id = id;
178
179         RETURN(0);
180 }
181
182 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
183                         struct obd_trans_info *oti)
184 {
185         struct obd_device *obd = class_exp2obd(exp);
186
187         ENTRY;
188         if (!obd) {
189                 CERROR("invalid client cookie "LPX64"\n",
190                        exp->exp_handle.h_cookie);
191                 RETURN(-EINVAL);
192         }
193
194         if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
195                 CERROR("obdo missing FLID valid flag: "LPX64"\n",
196                        oinfo->oi_oa->o_valid);
197                 RETURN(-EINVAL);
198         }
199
200         memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
201
202         if (oinfo->oi_oa->o_id & 4) {
203                 /* Save lock to force ACKed reply */
204                 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
205                 oti->oti_ack_locks[0].mode = LCK_NL;
206                 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
207         }
208
209         RETURN(0);
210 }
211
212 static void
213 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
214                       __u64 offset, int len)
215 {
216         int   page_offset = offset & ~CFS_PAGE_MASK;
217         char *addr        = ((char *)cfs_kmap(page)) + page_offset;
218
219         if (len % OBD_ECHO_BLOCK_SIZE != 0)
220                 CERROR("Unexpected block size %d\n", len);
221
222         while (len > 0) {
223                 if (rw & OBD_BRW_READ)
224                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
225                                           offset, id);
226                 else
227                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
228                                           0xecc0ecc0ecc0ecc0ULL,
229                                           0xecc0ecc0ecc0ecc0ULL);
230
231                 addr   += OBD_ECHO_BLOCK_SIZE;
232                 offset += OBD_ECHO_BLOCK_SIZE;
233                 len    -= OBD_ECHO_BLOCK_SIZE;
234         }
235
236         cfs_kunmap(page);
237 }
238
239 static int
240 echo_page_debug_check(cfs_page_t *page, obd_id id,
241                       __u64 offset, int len)
242 {
243         int   page_offset = offset & ~CFS_PAGE_MASK;
244         char *addr        = ((char *)cfs_kmap(page)) + page_offset;
245         int   rc          = 0;
246         int   rc2;
247
248         if (len % OBD_ECHO_BLOCK_SIZE != 0)
249                 CERROR("Unexpected block size %d\n", len);
250
251         while (len > 0) {
252                 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
253                                         offset, id);
254
255                 if (rc2 != 0 && rc == 0)
256                         rc = rc2;
257
258                 addr   += OBD_ECHO_BLOCK_SIZE;
259                 offset += OBD_ECHO_BLOCK_SIZE;
260                 len    -= OBD_ECHO_BLOCK_SIZE;
261         }
262
263         cfs_kunmap(page);
264
265         return (rc);
266 }
267
268 /* This allows us to verify that desc_private is passed unmolested */
269 #define DESC_PRIV 0x10293847
270
271 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
272                 int objcount, struct obd_ioobj *obj, int niocount,
273                 struct niobuf_remote *nb, struct niobuf_local *res,
274                 struct obd_trans_info *oti, struct lustre_capa *unused)
275 {
276         struct obd_device *obd;
277         struct niobuf_local *r = res;
278         int tot_bytes = 0;
279         int rc = 0;
280         int i;
281         ENTRY;
282
283         obd = export->exp_obd;
284         if (obd == NULL)
285                 RETURN(-EINVAL);
286
287         /* Temp fix to stop falling foul of osc_announce_cached() */
288         oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
289
290         memset(res, 0, sizeof(*res) * niocount);
291
292         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
293                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
294
295         if (oti)
296                 oti->oti_handle = (void *)DESC_PRIV;
297
298         for (i = 0; i < objcount; i++, obj++) {
299                 int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
300                 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
301                 int debug_setup = (!ispersistent &&
302                                    (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
303                                    (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
304                 int j;
305
306                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
307
308                         if (ispersistent &&
309                             (nb->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
310                                 r->page = echo_persistent_pages[nb->offset >>
311                                                                 CFS_PAGE_SHIFT];
312                                 /* Take extra ref so __free_pages() can be called OK */
313                                 cfs_get_page (r->page);
314                         } else {
315                                 OBD_PAGE_ALLOC(r->page, gfp_mask);
316                                 if (r->page == NULL) {
317                                         CERROR("can't get page %u/%u for id "
318                                                LPU64"\n",
319                                                j, obj->ioo_bufcnt, obj->ioo_id);
320                                         GOTO(preprw_cleanup, rc = -ENOMEM);
321                                 }
322                         }
323
324                         tot_bytes += nb->len;
325
326                         atomic_inc(&obd->u.echo.eo_prep);
327
328                         r->offset = nb->offset;
329                         r->len = nb->len;
330                         LASSERT((r->offset & ~CFS_PAGE_MASK) + r->len <= CFS_PAGE_SIZE);
331
332                         CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
333                                r->page, r->offset, r->len);
334
335                         if (cmd & OBD_BRW_READ)
336                                 r->rc = r->len;
337
338                         if (debug_setup)
339                                 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
340                                                       r->offset, r->len);
341                 }
342         }
343         if (cmd & OBD_BRW_READ)
344                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
345                                     tot_bytes);
346         else
347                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
348                                     tot_bytes);
349
350         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
351                atomic_read(&obd->u.echo.eo_prep));
352
353         RETURN(0);
354
355 preprw_cleanup:
356         /* It is possible that we would rather handle errors by  allow
357          * any already-set-up pages to complete, rather than tearing them
358          * all down again.  I believe that this is what the in-kernel
359          * prep/commit operations do.
360          */
361         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
362         while (r-- > res) {
363                 cfs_kunmap(r->page);
364                 /* NB if this is a persistent page, __free_pages will just
365                  * lose the extra ref gained above */
366                 OBD_PAGE_FREE(r->page);
367                 atomic_dec(&obd->u.echo.eo_prep);
368         }
369         memset(res, 0, sizeof(*res) * niocount);
370
371         return rc;
372 }
373
374 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
375                   int objcount, struct obd_ioobj *obj, int niocount,
376                   struct niobuf_local *res, struct obd_trans_info *oti, int rc)
377 {
378         struct obd_device *obd;
379         struct niobuf_local *r = res;
380         int i, vrc = 0;
381         ENTRY;
382
383         obd = export->exp_obd;
384         if (obd == NULL)
385                 RETURN(-EINVAL);
386
387         if (rc)
388                 GOTO(commitrw_cleanup, rc);
389
390         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
391                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
392                        objcount, niocount);
393         } else {
394                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
395                        objcount, niocount);
396         }
397
398         if (niocount && !r) {
399                 CERROR("NULL res niobuf with niocount %d\n", niocount);
400                 RETURN(-EINVAL);
401         }
402
403         LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
404
405         for (i = 0; i < objcount; i++, obj++) {
406                 int verify = (rc == 0 &&
407                               obj->ioo_id != ECHO_PERSISTENT_OBJID &&
408                               (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
409                               (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
410                 int j;
411
412                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
413                         cfs_page_t *page = r->page;
414                         void *addr;
415
416                         if (page == NULL) {
417                                 CERROR("null page objid "LPU64":%p, buf %d/%d\n",
418                                        obj->ioo_id, page, j, obj->ioo_bufcnt);
419                                 GOTO(commitrw_cleanup, rc = -EFAULT);
420                         }
421
422                         addr = cfs_kmap(page);
423
424                         CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
425                                r->page, addr, r->offset);
426
427                         if (verify) {
428                                 vrc = echo_page_debug_check(page, obj->ioo_id,
429                                                             r->offset, r->len);
430                                 /* check all the pages always */
431                                 if (vrc != 0 && rc == 0)
432                                         rc = vrc;
433                         }
434
435                         cfs_kunmap(page);
436                         /* NB see comment above regarding persistent pages */
437                         OBD_PAGE_FREE(page);
438                         atomic_dec(&obd->u.echo.eo_prep);
439                 }
440         }
441         CDEBUG(D_PAGE, "%d pages remain after commit\n",
442                atomic_read(&obd->u.echo.eo_prep));
443         RETURN(rc);
444
445 commitrw_cleanup:
446         CERROR("cleaning up %ld pages (%d obdos)\n",
447                niocount - (long)(r - res) - 1, objcount);
448         while (++r < res + niocount) {
449                 cfs_page_t *page = r->page;
450
451                 /* NB see comment above regarding persistent pages */
452                 OBD_PAGE_FREE(page);
453                 atomic_dec(&obd->u.echo.eo_prep);
454         }
455         return rc;
456 }
457
458 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
459 {
460         struct lprocfs_static_vars lvars;
461         int                        rc;
462         int                        lock_flags = 0;
463         struct ldlm_res_id         res_id = {.name = {1}};
464         ENTRY;
465
466         spin_lock_init(&obd->u.echo.eo_lock);
467         obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
468
469         obd->obd_namespace = ldlm_namespace_new("echo-tgt",
470                                                 LDLM_NAMESPACE_SERVER,
471                                                 LDLM_NAMESPACE_MODEST);
472         if (obd->obd_namespace == NULL) {
473                 LBUG();
474                 RETURN(-ENOMEM);
475         }
476
477         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
478                                     NULL, LCK_NL, &lock_flags, NULL,
479                                     ldlm_completion_ast, NULL, NULL,
480                                     0, NULL, &obd->u.echo.eo_nl_lock);
481         LASSERT (rc == ELDLM_OK);
482
483         lprocfs_echo_init_vars(&lvars);
484         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
485             lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
486                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
487                                      LPROCFS_CNTR_AVGMINMAX,
488                                      "read_bytes", "bytes");
489                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
490                                      LPROCFS_CNTR_AVGMINMAX,
491                                      "write_bytes", "bytes");
492         }
493
494         ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
495                             "echo_ldlm_cb_client", &obd->obd_ldlm_client);
496         RETURN(0);
497 }
498
499 static int echo_cleanup(struct obd_device *obd)
500 {
501         int leaked;
502         ENTRY;
503
504         lprocfs_obd_cleanup(obd);
505         lprocfs_free_obd_stats(obd);
506
507         ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
508
509         /* XXX Bug 3413; wait for a bit to ensure the BL callback has
510          * happened before calling ldlm_namespace_free() */
511         set_current_state (TASK_UNINTERRUPTIBLE);
512         cfs_schedule_timeout (CFS_TASK_UNINT, cfs_time_seconds(1));
513
514         ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
515
516         leaked = atomic_read(&obd->u.echo.eo_prep);
517         if (leaked != 0)
518                 CERROR("%d prep/commitrw pages leaked\n", leaked);
519
520         RETURN(0);
521 }
522
523 static struct obd_ops echo_obd_ops = {
524         .o_owner           = THIS_MODULE,
525         .o_connect         = echo_connect,
526         .o_disconnect      = echo_disconnect,
527         .o_destroy_export  = echo_destroy_export,
528         .o_create          = echo_create,
529         .o_destroy         = echo_destroy,
530         .o_getattr         = echo_getattr,
531         .o_setattr         = echo_setattr,
532         .o_preprw          = echo_preprw,
533         .o_commitrw        = echo_commitrw,
534         .o_setup           = echo_setup,
535         .o_cleanup         = echo_cleanup
536 };
537
538 extern int echo_client_init(void);
539 extern void echo_client_exit(void);
540
541 static void
542 echo_persistent_pages_fini (void)
543 {
544         int     i;
545
546         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
547                 if (echo_persistent_pages[i] != NULL) {
548                         OBD_PAGE_FREE(echo_persistent_pages[i]);
549                         echo_persistent_pages[i] = NULL;
550                 }
551 }
552
553 static int
554 echo_persistent_pages_init (void)
555 {
556         cfs_page_t *pg;
557         int          i;
558
559         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
560                 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
561                         CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
562
563                 OBD_PAGE_ALLOC(pg, gfp_mask);
564                 if (pg == NULL) {
565                         echo_persistent_pages_fini ();
566                         return (-ENOMEM);
567                 }
568
569                 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
570                 cfs_kunmap (pg);
571
572                 echo_persistent_pages[i] = pg;
573         }
574
575         return (0);
576 }
577
578 static int __init obdecho_init(void)
579 {
580         struct lprocfs_static_vars lvars;
581         int rc;
582
583         ENTRY;
584         printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
585
586         LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
587
588         lprocfs_echo_init_vars(&lvars);
589
590         rc = echo_persistent_pages_init ();
591         if (rc != 0)
592                 goto failed_0;
593
594         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
595                                  LUSTRE_ECHO_NAME, NULL);
596         if (rc != 0)
597                 goto failed_1;
598
599         rc = echo_client_init();
600         if (rc == 0)
601                 RETURN (0);
602
603         class_unregister_type(LUSTRE_ECHO_NAME);
604  failed_1:
605         echo_persistent_pages_fini ();
606  failed_0:
607         RETURN(rc);
608 }
609
610 static void /*__exit*/ obdecho_exit(void)
611 {
612         echo_client_exit();
613         class_unregister_type(LUSTRE_ECHO_NAME);
614         echo_persistent_pages_fini ();
615 }
616
617 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
618 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
619 MODULE_LICENSE("GPL");
620
621 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);