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