Whamcloud - gitweb
pass lu_context to ->o_connect() method
[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_SIZE)
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_context *ctx,
53                         struct lustre_handle *conn, struct obd_device *obd,
54                         struct obd_uuid *cluuid, struct obd_connect_data *data)
55 {
56         data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
57         return class_connect(conn, obd, cluuid);
58 }
59
60 static int echo_disconnect(struct obd_export *exp)
61 {
62         unsigned long irqflags;
63
64         LASSERT (exp != NULL);
65
66         ldlm_cancel_locks_for_export(exp);
67
68         /* complete all outstanding replies */
69         spin_lock_irqsave(&exp->exp_lock, irqflags);
70         while (!list_empty(&exp->exp_outstanding_replies)) {
71                 struct ptlrpc_reply_state *rs =
72                         list_entry(exp->exp_outstanding_replies.next,
73                                    struct ptlrpc_reply_state, rs_exp_list);
74                 struct ptlrpc_service *svc = rs->rs_service;
75
76                 spin_lock(&svc->srv_lock);
77                 list_del_init(&rs->rs_exp_list);
78                 ptlrpc_schedule_difficult_reply(rs);
79                 spin_unlock(&svc->srv_lock);
80         }
81         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
82
83         return class_disconnect(exp);
84 }
85
86 static int echo_destroy_export(struct obd_export *exp)
87 {
88         ENTRY;
89
90         target_destroy_export(exp);
91
92         RETURN(0);
93 }
94
95  static __u64 echo_next_id(struct obd_device *obddev)
96 {
97         obd_id id;
98
99         spin_lock(&obddev->u.echo.eo_lock);
100         id = ++obddev->u.echo.eo_lastino;
101         spin_unlock(&obddev->u.echo.eo_lock);
102
103         return id;
104 }
105
106 int echo_create(struct obd_export *exp, struct obdo *oa,
107                 struct lov_stripe_md **ea, struct obd_trans_info *oti)
108 {
109         struct obd_device *obd = class_exp2obd(exp);
110
111         if (!obd) {
112                 CERROR("invalid client cookie "LPX64"\n",
113                        exp->exp_handle.h_cookie);
114                 return -EINVAL;
115         }
116
117         if (!(oa->o_mode && S_IFMT)) {
118                 CERROR("echo obd: no type!\n");
119                 return -ENOENT;
120         }
121
122         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
123                 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
124                 return -EINVAL;
125         }
126
127         oa->o_id = echo_next_id(obd);
128         oa->o_valid = OBD_MD_FLID;
129
130         return 0;
131 }
132
133 int echo_destroy(struct obd_export *exp, struct obdo *oa,
134                  struct lov_stripe_md *ea, struct obd_trans_info *oti,
135                  struct obd_export *md_exp)
136 {
137         struct obd_device *obd = class_exp2obd(exp);
138
139         ENTRY;
140         if (!obd) {
141                 CERROR("invalid client cookie "LPX64"\n",
142                        exp->exp_handle.h_cookie);
143                 RETURN(-EINVAL);
144         }
145
146         if (!(oa->o_valid & OBD_MD_FLID)) {
147                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
148                 RETURN(-EINVAL);
149         }
150
151         if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
152                 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
153                 RETURN(-EINVAL);
154         }
155
156         RETURN(0);
157 }
158
159 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
160                         struct lov_stripe_md *md)
161 {
162         struct obd_device *obd = class_exp2obd(exp);
163         obd_id id = oa->o_id;
164
165         ENTRY;
166         if (!obd) {
167                 CERROR("invalid client cookie "LPX64"\n",
168                        exp->exp_handle.h_cookie);
169                 RETURN(-EINVAL);
170         }
171
172         if (!(oa->o_valid & OBD_MD_FLID)) {
173                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
174                 RETURN(-EINVAL);
175         }
176
177         obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
178         oa->o_id = id;
179
180         RETURN(0);
181 }
182
183 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
184                         struct lov_stripe_md *md, struct obd_trans_info *oti)
185 {
186         struct obd_device *obd = class_exp2obd(exp);
187
188         ENTRY;
189         if (!obd) {
190                 CERROR("invalid client cookie "LPX64"\n",
191                        exp->exp_handle.h_cookie);
192                 RETURN(-EINVAL);
193         }
194
195         if (!(oa->o_valid & OBD_MD_FLID)) {
196                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
197                 RETURN(-EINVAL);
198         }
199
200         memcpy(&obd->u.echo.eo_oa, oa, sizeof(*oa));
201
202         if (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 & (PAGE_SIZE - 1);
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 & (PAGE_SIZE - 1);
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)
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 >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
310                                 r->page = echo_persistent_pages[nb->offset >>
311                                                                 PAGE_SHIFT];
312                                 /* Take extra ref so __free_pages() can be called OK */
313                                 cfs_get_page (r->page);
314                         } else {
315                                 r->page = cfs_alloc_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                 cfs_free_page(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                         cfs_free_page(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                 cfs_free_page(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         if (obd->obd_namespace == NULL) {
472                 LBUG();
473                 RETURN(-ENOMEM);
474         }
475
476         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
477                               LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
478                               NULL, ldlm_completion_ast, NULL, NULL,
479                               NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
480         LASSERT (rc == ELDLM_OK);
481
482         lprocfs_init_vars(echo, &lvars);
483         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
484             lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
485                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
486                                      LPROCFS_CNTR_AVGMINMAX,
487                                      "read_bytes", "bytes");
488                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
489                                      LPROCFS_CNTR_AVGMINMAX,
490                                      "write_bytes", "bytes");
491         }
492
493         ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
494                             "echo_ldlm_cb_client", &obd->obd_ldlm_client);
495         RETURN(0);
496 }
497
498 static int echo_cleanup(struct obd_device *obd)
499 {
500         int leaked;
501         ENTRY;
502
503         lprocfs_free_obd_stats(obd);
504         lprocfs_obd_cleanup(obd);
505
506         ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
507
508         /* XXX Bug 3413; wait for a bit to ensure the BL callback has
509          * happened before calling ldlm_namespace_free() */
510         set_current_state (TASK_UNINTERRUPTIBLE);
511         cfs_schedule_timeout (CFS_TASK_UNINT, cfs_time_seconds(1));
512
513         ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
514
515         leaked = atomic_read(&obd->u.echo.eo_prep);
516         if (leaked != 0)
517                 CERROR("%d prep/commitrw pages leaked\n", leaked);
518
519         RETURN(0);
520 }
521
522 static struct obd_ops echo_obd_ops = {
523         .o_owner           = THIS_MODULE,
524         .o_connect         = echo_connect,
525         .o_disconnect      = echo_disconnect,
526         .o_destroy_export  = echo_destroy_export,
527         .o_create          = echo_create,
528         .o_destroy         = echo_destroy,
529         .o_getattr         = echo_getattr,
530         .o_setattr         = echo_setattr,
531         .o_preprw          = echo_preprw,
532         .o_commitrw        = echo_commitrw,
533         .o_setup           = echo_setup,
534         .o_cleanup         = echo_cleanup
535 };
536
537 extern int echo_client_init(void);
538 extern void echo_client_exit(void);
539
540 static void
541 echo_persistent_pages_fini (void)
542 {
543         int     i;
544
545         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
546                 if (echo_persistent_pages[i] != NULL) {
547                         cfs_free_page (echo_persistent_pages[i]);
548                         echo_persistent_pages[i] = NULL;
549                 }
550 }
551
552 static int
553 echo_persistent_pages_init (void)
554 {
555         cfs_page_t *pg;
556         int          i;
557
558         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
559                 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
560                         CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
561
562                 pg = cfs_alloc_page (gfp_mask);
563                 if (pg == NULL) {
564                         echo_persistent_pages_fini ();
565                         return (-ENOMEM);
566                 }
567
568                 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
569                 cfs_kunmap (pg);
570
571                 echo_persistent_pages[i] = pg;
572         }
573
574         return (0);
575 }
576
577 static int __init obdecho_init(void)
578 {
579         struct lprocfs_static_vars lvars;
580         int rc;
581
582         ENTRY;
583         printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
584
585         LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
586
587         lprocfs_init_vars(echo, &lvars);
588
589         rc = echo_persistent_pages_init ();
590         if (rc != 0)
591                 goto failed_0;
592
593         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
594                                  LUSTRE_ECHO_NAME, NULL);
595         if (rc != 0)
596                 goto failed_1;
597
598         rc = echo_client_init();
599         if (rc == 0)
600                 RETURN (0);
601
602         class_unregister_type(LUSTRE_ECHO_NAME);
603  failed_1:
604         echo_persistent_pages_fini ();
605  failed_0:
606         RETURN(rc);
607 }
608
609 static void /*__exit*/ obdecho_exit(void)
610 {
611         echo_client_exit();
612         class_unregister_type(LUSTRE_ECHO_NAME);
613         echo_persistent_pages_fini ();
614 }
615
616 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
617 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
618 MODULE_LICENSE("GPL");
619
620 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);