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