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