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