Whamcloud - gitweb
Branch: HEAD
[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 Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #include <linux/version.h>
29 #include <linux/module.h>
30 #include <linux/mm.h>
31 #include <linux/highmem.h>
32 #include <linux/fs.h>
33 #include <linux/stat.h>
34 #include <linux/sched.h>
35 #include <linux/smp_lock.h>
36 #include <linux/proc_fs.h>
37 #include <linux/init.h>
38 #include <asm/unistd.h>
39
40 #define DEBUG_SUBSYSTEM S_ECHO
41
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/obd_echo.h>
45 #include <linux/lustre_debug.h>
46 #include <linux/lustre_dlm.h>
47 #include <linux/lprocfs_status.h>
48
49 #define ECHO_INIT_OBJID      0x1000000000000000ULL
50 #define ECHO_HANDLE_MAGIC    0xabcd0123fedc9876ULL
51
52 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE/PAGE_SIZE)
53 static struct page *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
54
55 enum {
56         LPROC_ECHO_READ_BYTES = 1,
57         LPROC_ECHO_WRITE_BYTES = 2,
58         LPROC_ECHO_LAST
59 };
60
61 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
62                         struct obd_uuid *cluuid, struct obd_connect_data *data,
63                         unsigned long connect_flags)
64 {
65         return class_connect(conn, obd, cluuid);
66 }
67
68 static int echo_disconnect(struct obd_export *exp, unsigned long flags)
69 {
70         unsigned long irqflags;
71
72         LASSERT (exp != NULL);
73
74         ldlm_cancel_locks_for_export(exp);
75
76         spin_lock_irqsave(&exp->exp_lock, irqflags);
77         exp->exp_flags = flags;
78         /* complete all outstanding replies */
79         while (!list_empty(&exp->exp_outstanding_replies)) {
80                 struct ptlrpc_reply_state *rs =
81                         list_entry(exp->exp_outstanding_replies.next,
82                                    struct ptlrpc_reply_state, rs_exp_list);
83                 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
84
85                 spin_lock(&svc->srv_lock);
86                 list_del_init(&rs->rs_exp_list);
87                 ptlrpc_schedule_difficult_reply(rs);
88                 spin_unlock(&svc->srv_lock);
89         }
90         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
91
92         return class_disconnect(exp, flags);
93 }
94
95 static int echo_destroy_export(struct obd_export *exp)
96 {
97         ENTRY;
98         
99         target_destroy_export(exp);
100
101         RETURN(0);
102 }
103
104  static __u64 echo_next_id(struct obd_device *obddev)
105 {
106         obd_id id;
107
108         spin_lock(&obddev->u.echo.eo_lock);
109         id = ++obddev->u.echo.eo_lastino;
110         spin_unlock(&obddev->u.echo.eo_lock);
111
112         return id;
113 }
114
115 int echo_create(struct obd_export *exp, struct obdo *oa,
116                 void *acl, int acl_size,
117                 struct lov_stripe_md **ea, struct obd_trans_info *oti)
118 {
119         struct obd_device *obd = class_exp2obd(exp);
120
121         if (!obd) {
122                 CERROR("invalid client cookie "LPX64"\n", 
123                        exp->exp_handle.h_cookie);
124                 return -EINVAL;
125         }
126
127         if (!(oa->o_mode && S_IFMT)) {
128                 CERROR("echo obd: no type!\n");
129                 return -ENOENT;
130         }
131
132         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
133                 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
134                 return -EINVAL;
135         }
136
137         oa->o_id = echo_next_id(obd);
138         oa->o_valid = OBD_MD_FLID;
139
140         return 0;
141 }
142
143 int echo_destroy(struct obd_export *exp, struct obdo *oa,
144                  struct lov_stripe_md *ea, struct obd_trans_info *oti)
145 {
146         struct obd_device *obd = class_exp2obd(exp);
147
148         if (!obd) {
149                 CERROR("invalid client cookie "LPX64"\n", 
150                        exp->exp_handle.h_cookie);
151                 RETURN(-EINVAL);
152         }
153
154         if (!(oa->o_valid & OBD_MD_FLID)) {
155                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
156                 RETURN(-EINVAL);
157         }
158
159         if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
160                 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
161                 RETURN(-EINVAL);
162         }
163
164
165         return 0;
166 }
167
168 static int echo_getattr(struct obd_export *exp, struct obdo *oa,
169                         struct lov_stripe_md *md)
170 {
171         struct obd_device *obd = class_exp2obd(exp);
172         obd_id id = oa->o_id;
173
174         if (!obd) {
175                 CERROR("invalid client cookie "LPX64"\n", 
176                        exp->exp_handle.h_cookie);
177                 RETURN(-EINVAL);
178         }
179
180         if (!(oa->o_valid & OBD_MD_FLID)) {
181                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
182                 RETURN(-EINVAL);
183         }
184
185         obdo_cpy_md(oa, &obd->u.echo.eo_oa, oa->o_valid);
186         oa->o_id = id;
187
188         return 0;
189 }
190
191 static int echo_setattr(struct obd_export *exp, struct obdo *oa,
192                         struct lov_stripe_md *md, struct obd_trans_info *oti,
193                         struct lustre_capa *capa)
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         return 0;
217 }
218
219 static void
220 echo_page_debug_setup(struct page *page, int rw, obd_id id,
221                       __u64 offset, int len)
222 {
223         int   page_offset = offset & (PAGE_SIZE - 1);
224         char *addr        = ((char *)kmap(page)) + page_offset;
225
226         if (len % OBD_ECHO_BLOCK_SIZE != 0)
227                 CERROR("Unexpected block size %d\n", len);
228
229         while (len > 0) {
230                 if (rw & OBD_BRW_READ)
231                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
232                                           offset, id);
233                 else
234                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
235                                           0xecc0ecc0ecc0ecc0ULL,
236                                           0xecc0ecc0ecc0ecc0ULL);
237                 
238                 addr   += OBD_ECHO_BLOCK_SIZE;
239                 offset += OBD_ECHO_BLOCK_SIZE;
240                 len    -= OBD_ECHO_BLOCK_SIZE;
241         }
242
243         kunmap(page);
244 }
245
246 static int
247 echo_page_debug_check(struct page *page, obd_id id,
248                       __u64 offset, int len)
249 {
250         int   page_offset = offset & (PAGE_SIZE - 1);
251         char *addr        = ((char *)kmap(page)) + page_offset;
252         int   rc          = 0;
253         int   rc2;
254
255         if (len % OBD_ECHO_BLOCK_SIZE != 0)
256                 CERROR("Unexpected block size %d\n", len);
257
258         while (len > 0) {
259                 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
260                                         offset, id);
261
262                 if (rc2 != 0 && rc == 0)
263                         rc = rc2;
264                 
265                 addr   += OBD_ECHO_BLOCK_SIZE;
266                 offset += OBD_ECHO_BLOCK_SIZE;
267                 len    -= OBD_ECHO_BLOCK_SIZE;
268         }
269
270         kunmap(page);
271
272         return (rc);
273 }
274
275 /* This allows us to verify that desc_private is passed unmolested */
276 #define DESC_PRIV 0x10293847
277
278 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
279                 int objcount, struct obd_ioobj *obj, int niocount,
280                 struct niobuf_remote *nb, struct niobuf_local *res,
281                 struct obd_trans_info *oti, struct lustre_capa *capa)
282 {
283         struct obd_device *obd;
284         struct niobuf_local *r = res;
285         int tot_bytes = 0;
286         int rc = 0;
287         int i;
288         ENTRY;
289
290         obd = export->exp_obd;
291         if (obd == NULL)
292                 RETURN(-EINVAL);
293
294         /* Temp fix to stop falling foul of osc_announce_cached() */
295         oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
296
297         memset(res, 0, sizeof(*res) * niocount);
298
299         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
300                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
301
302         if (oti)
303                 oti->oti_handle = (void *)DESC_PRIV;
304
305         for (i = 0; i < objcount; i++, obj++) {
306                 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
307                 int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
308                 int debug_setup = (!ispersistent &&
309                                    (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
310                                    (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
311                 int j;
312
313                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
314
315                         if (ispersistent &&
316                             (nb->offset >> PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
317                                 r->page = echo_persistent_pages[nb->offset >>
318                                                                 PAGE_SHIFT];
319                                 /* Take extra ref so __free_pages() can be called OK */
320                                 get_page (r->page);
321                         } else {
322                                 r->page = alloc_pages(gfp_mask, 0);
323                                 if (r->page == NULL) {
324                                         CERROR("can't get page %u/%u for id "
325                                                LPU64"\n",
326                                                j, obj->ioo_bufcnt, obj->ioo_id);
327                                         GOTO(preprw_cleanup, rc = -ENOMEM);
328                                 }
329                         }
330
331                         tot_bytes += nb->len;
332
333                         atomic_inc(&obd->u.echo.eo_prep);
334
335                         r->offset = nb->offset;
336                         r->len = nb->len;
337                         LASSERT((r->offset & ~PAGE_MASK) + r->len <= PAGE_SIZE);
338
339                         CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
340                                r->page, r->offset, r->len);
341
342                         if (cmd & OBD_BRW_READ)
343                                 r->rc = r->len;
344
345                         if (debug_setup)
346                                 echo_page_debug_setup(r->page, cmd, obj->ioo_id,
347                                                       r->offset, r->len);
348                 }
349         }
350         if (cmd & OBD_BRW_READ)
351                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
352                                     tot_bytes);
353         else
354                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
355                                     tot_bytes);
356
357         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
358                atomic_read(&obd->u.echo.eo_prep));
359
360         RETURN(0);
361
362 preprw_cleanup:
363         /* It is possible that we would rather handle errors by  allow
364          * any already-set-up pages to complete, rather than tearing them
365          * all down again.  I believe that this is what the in-kernel
366          * prep/commit operations do.
367          */
368         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
369         while (r-- > res) {
370                 kunmap(r->page);
371                 /* NB if this is a persistent page, __free_pages will just
372                  * lose the extra ref gained above */
373                 __free_pages(r->page, 0);
374                 atomic_dec(&obd->u.echo.eo_prep);
375         }
376         memset(res, 0, sizeof(*res) * niocount);
377
378         return rc;
379 }
380
381 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
382                   int objcount, struct obd_ioobj *obj, int niocount,
383                   struct niobuf_local *res, struct obd_trans_info *oti, int rc)
384 {
385         struct obd_device *obd;
386         struct niobuf_local *r = res;
387         int i, vrc = 0;
388         ENTRY;
389
390         obd = export->exp_obd;
391         if (obd == NULL)
392                 RETURN(-EINVAL);
393
394         if (rc)
395                 GOTO(commitrw_cleanup, rc);
396
397         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
398                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
399                        objcount, niocount);
400         } else {
401                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
402                        objcount, niocount);
403         }
404
405         if (niocount && !r) {
406                 CERROR("NULL res niobuf with niocount %d\n", niocount);
407                 RETURN(-EINVAL);
408         }
409
410         LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
411
412         for (i = 0; i < objcount; i++, obj++) {
413                 int verify = (rc == 0 &&
414                               obj->ioo_id != ECHO_PERSISTENT_OBJID &&
415                               (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
416                               (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
417  
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         int                        rc;
469         int                        lock_flags = 0;
470         struct ldlm_res_id         res_id = {.name = {1}};
471
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         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
484                               LDLM_PLAIN, NULL, LCK_NL, &lock_flags,
485                               NULL, ldlm_completion_ast, NULL, NULL,
486                               NULL, 0, NULL, &obd->u.echo.eo_nl_lock);
487         LASSERT (rc == ELDLM_OK);
488
489
490
491         ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
492                             "echo_ldlm_cb_client", &obd->obd_ldlm_client);
493         RETURN(0);
494 }
495
496 static int echo_cleanup(struct obd_device *obd, int flags)
497 {
498         int leaked;
499         ENTRY;
500
501         ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
502
503         /* XXX Bug 3413; wait for a bit to ensure the BL callback has
504          * happened before calling ldlm_namespace_free() */
505         set_current_state (TASK_UNINTERRUPTIBLE);
506         schedule_timeout (HZ);
507
508         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
509
510         leaked = atomic_read(&obd->u.echo.eo_prep);
511         if (leaked != 0)
512                 CERROR("%d prep/commitrw pages leaked\n", leaked);
513
514         RETURN(0);
515 }
516
517 int echo_attach(struct obd_device *obd, obd_count len, void *data)
518 {
519         struct lprocfs_static_vars lvars;
520         int rc;
521
522         lprocfs_init_vars(echo, &lvars);
523         rc = lprocfs_obd_attach(obd, lvars.obd_vars);
524         if (rc != 0)
525                 return rc;
526         rc = lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST);
527         if (rc != 0)
528                 return rc;
529
530         lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
531                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
532         lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
533                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
534         return rc;
535 }
536
537 int echo_detach(struct obd_device *dev)
538 {
539         lprocfs_free_obd_stats(dev);
540         return lprocfs_obd_detach(dev);
541 }
542
543 static struct obd_ops echo_obd_ops = {
544         .o_owner           = THIS_MODULE,
545         .o_attach          = echo_attach,
546         .o_detach          = echo_detach,
547         .o_connect         = echo_connect,
548         .o_disconnect      = echo_disconnect,
549         .o_destroy_export  = echo_destroy_export,
550         .o_create          = echo_create,
551         .o_destroy         = echo_destroy,
552         .o_getattr         = echo_getattr,
553         .o_setattr         = echo_setattr,
554         .o_preprw          = echo_preprw,
555         .o_commitrw        = echo_commitrw,
556         .o_setup           = echo_setup,
557         .o_cleanup         = echo_cleanup
558 };
559
560 extern int echo_client_init(void);
561 extern void echo_client_exit(void);
562
563 static void
564 echo_persistent_pages_fini (void)
565 {
566         int     i;
567
568         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
569                 if (echo_persistent_pages[i] != NULL) {
570                         __free_pages (echo_persistent_pages[i], 0);
571                         echo_persistent_pages[i] = NULL;
572                 }
573 }
574
575 static int
576 echo_persistent_pages_init (void)
577 {
578         struct page *pg;
579         int          i;
580
581         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
582                 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
583                         GFP_KERNEL : GFP_HIGHUSER;
584
585                 pg = alloc_pages (gfp_mask, 0);
586                 if (pg == NULL) {
587                         echo_persistent_pages_fini ();
588                         return (-ENOMEM);
589                 }
590
591                 memset (kmap (pg), 0, PAGE_SIZE);
592                 kunmap (pg);
593
594                 echo_persistent_pages[i] = pg;
595         }
596
597         return (0);
598 }
599
600 static int __init obdecho_init(void)
601 {
602         struct lprocfs_static_vars lvars;
603         int rc;
604
605         printk(KERN_INFO "Lustre: Echo OBD driver; info@clusterfs.com\n");
606
607         LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
608
609         lprocfs_init_vars(echo, &lvars);
610
611         rc = echo_persistent_pages_init ();
612         if (rc != 0)
613                 goto failed_0;
614
615         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
616                                  OBD_ECHO_DEVICENAME);
617         if (rc != 0)
618                 goto failed_1;
619
620         rc = echo_client_init();
621         if (rc == 0)
622                 RETURN (0);
623
624         class_unregister_type(OBD_ECHO_DEVICENAME);
625  failed_1:
626         echo_persistent_pages_fini ();
627  failed_0:
628         RETURN(rc);
629 }
630
631 static void /*__exit*/ obdecho_exit(void)
632 {
633         echo_client_exit();
634         class_unregister_type(OBD_ECHO_DEVICENAME);
635         echo_persistent_pages_fini ();
636 }
637
638 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
639 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
640 MODULE_LICENSE("GPL");
641
642 module_init(obdecho_init);
643 module_exit(obdecho_exit);