Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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 /* should be generic per-obd stats... */
56 struct xprocfs_io_stat {
57         __u64    st_read_bytes;
58         __u64    st_read_reqs;
59         __u64    st_write_bytes;
60         __u64    st_write_reqs;
61         __u64    st_getattr_reqs;
62         __u64    st_setattr_reqs;
63         __u64    st_create_reqs;
64         __u64    st_destroy_reqs;
65         __u64    st_statfs_reqs;
66         __u64    st_sync_reqs;
67         __u64    st_open_reqs;
68         __u64    st_close_reqs;
69         __u64    st_punch_reqs;
70 };
71
72 static struct xprocfs_io_stat xprocfs_iostats[NR_CPUS];
73 static struct proc_dir_entry *xprocfs_dir;
74
75 #define XPROCFS_BUMP_MYCPU_IOSTAT(field, count)                 \
76 do {                                                            \
77         xprocfs_iostats[smp_processor_id()].field += (count);   \
78 } while (0)
79
80 #define DECLARE_XPROCFS_SUM_STAT(field)                 \
81 static long long                                        \
82 xprocfs_sum_##field (void)                              \
83 {                                                       \
84         long long stat = 0;                             \
85         int       i;                                    \
86                                                         \
87         for (i = 0; i < smp_num_cpus; i++)              \
88                 stat += xprocfs_iostats[i].field;       \
89         return (stat);                                  \
90 }
91 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
92 DECLARE_XPROCFS_SUM_STAT (st_read_bytes)
93 DECLARE_XPROCFS_SUM_STAT (st_read_reqs)
94 DECLARE_XPROCFS_SUM_STAT (st_write_bytes)
95 DECLARE_XPROCFS_SUM_STAT (st_write_reqs)
96 DECLARE_XPROCFS_SUM_STAT (st_getattr_reqs)
97 DECLARE_XPROCFS_SUM_STAT (st_setattr_reqs)
98 DECLARE_XPROCFS_SUM_STAT (st_create_reqs)
99 DECLARE_XPROCFS_SUM_STAT (st_destroy_reqs)
100 DECLARE_XPROCFS_SUM_STAT (st_statfs_reqs)
101 DECLARE_XPROCFS_SUM_STAT (st_sync_reqs)
102 DECLARE_XPROCFS_SUM_STAT (st_open_reqs)
103 DECLARE_XPROCFS_SUM_STAT (st_close_reqs)
104 DECLARE_XPROCFS_SUM_STAT (st_punch_reqs)
105 #endif
106
107 static int
108 xprocfs_rd_stat (char *page, char **start, off_t off, int count,
109                  int  *eof, void *data)
110 {
111         long long (*fn)(void) = (long long(*)(void))data;
112         int         len;
113
114         *eof = 1;
115         if (off != 0)
116                 return (0);
117
118         len = snprintf (page, count, "%Ld\n", fn());
119         *start = page;
120         return (len);
121 }
122
123
124 static void
125 xprocfs_add_stat(char *name, long long (*fn)(void))
126 {
127         struct proc_dir_entry *entry;
128
129         entry = create_proc_entry (name, S_IFREG|S_IRUGO, xprocfs_dir);
130         if (entry == NULL) {
131                 CERROR ("Can't add procfs stat %s\n", name);
132                 return;
133         }
134
135         entry->data = fn;
136         entry->read_proc = xprocfs_rd_stat;
137         entry->write_proc = NULL;
138 }
139
140 static void
141 xprocfs_init (char *name)
142 {
143         char  dirname[64];
144
145         snprintf (dirname, sizeof (dirname), "sys/%s", name);
146
147         xprocfs_dir = proc_mkdir (dirname, NULL);
148         if (xprocfs_dir == NULL) {
149                 CERROR ("Can't make dir\n");
150                 return;
151         }
152
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
154         xprocfs_add_stat ("read_bytes",   xprocfs_sum_st_read_bytes);
155         xprocfs_add_stat ("read_reqs",    xprocfs_sum_st_read_reqs);
156         xprocfs_add_stat ("write_bytes",  xprocfs_sum_st_write_bytes);
157         xprocfs_add_stat ("write_reqs",   xprocfs_sum_st_write_reqs);
158         xprocfs_add_stat ("getattr_reqs", xprocfs_sum_st_getattr_reqs);
159         xprocfs_add_stat ("setattr_reqs", xprocfs_sum_st_setattr_reqs);
160         xprocfs_add_stat ("create_reqs",  xprocfs_sum_st_create_reqs);
161         xprocfs_add_stat ("destroy_reqs", xprocfs_sum_st_destroy_reqs);
162         xprocfs_add_stat ("statfs_reqs",  xprocfs_sum_st_statfs_reqs);
163         xprocfs_add_stat ("sync_reqs",    xprocfs_sum_st_sync_reqs);
164         xprocfs_add_stat ("open_reqs",    xprocfs_sum_st_open_reqs);
165         xprocfs_add_stat ("close_reqs",   xprocfs_sum_st_close_reqs);
166         xprocfs_add_stat ("punch_reqs",   xprocfs_sum_st_punch_reqs);
167 #endif
168 }
169
170 void xprocfs_fini (void)
171 {
172         if (xprocfs_dir == NULL)
173                 return;
174
175         remove_proc_entry ("read_bytes",   xprocfs_dir);
176         remove_proc_entry ("read_reqs",    xprocfs_dir);
177         remove_proc_entry ("write_bytes",  xprocfs_dir);
178         remove_proc_entry ("write_reqs",   xprocfs_dir);
179         remove_proc_entry ("getattr_reqs", xprocfs_dir);
180         remove_proc_entry ("setattr_reqs", xprocfs_dir);
181         remove_proc_entry ("create_reqs",  xprocfs_dir);
182         remove_proc_entry ("destroy_reqs", xprocfs_dir);
183         remove_proc_entry ("statfs_reqs",  xprocfs_dir);
184         remove_proc_entry ("sync_reqs",    xprocfs_dir);
185         remove_proc_entry ("open_reqs",    xprocfs_dir);
186         remove_proc_entry ("close_reqs",   xprocfs_dir);
187         remove_proc_entry ("punch_reqs",   xprocfs_dir);
188
189         remove_proc_entry (xprocfs_dir->name, xprocfs_dir->parent);
190         xprocfs_dir = NULL;
191 }
192
193 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
194                         struct obd_uuid *cluuid, struct recovd_obd *recovd,
195                         ptlrpc_recovery_cb_t recover)
196 {
197         return class_connect(conn, obd, cluuid);
198 }
199
200 static int echo_disconnect(struct lustre_handle *conn)
201 {
202         struct obd_export *exp = class_conn2export(conn);
203
204         LASSERT (exp != NULL);
205
206         ldlm_cancel_locks_for_export (exp);
207         return (class_disconnect (conn));
208 }
209
210 static __u64 echo_next_id(struct obd_device *obddev)
211 {
212         obd_id id;
213
214         spin_lock(&obddev->u.echo.eo_lock);
215         id = ++obddev->u.echo.eo_lastino;
216         spin_unlock(&obddev->u.echo.eo_lock);
217
218         return id;
219 }
220
221 int echo_create(struct lustre_handle *conn, struct obdo *oa,
222                 struct lov_stripe_md **ea, struct obd_trans_info *oti)
223 {
224         struct obd_device *obd = class_conn2obd(conn);
225
226         XPROCFS_BUMP_MYCPU_IOSTAT (st_create_reqs, 1);
227
228         if (!obd) {
229                 CERROR("invalid client "LPX64"\n", conn->addr);
230                 return -EINVAL;
231         }
232
233         if (!(oa->o_mode && S_IFMT)) {
234                 CERROR("echo obd: no type!\n");
235                 return -ENOENT;
236         }
237
238         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
239                 CERROR("invalid o_valid %08x\n", oa->o_valid);
240                 return -EINVAL;
241         }
242
243         oa->o_id = echo_next_id(obd);
244         oa->o_valid = OBD_MD_FLID;
245         atomic_inc(&obd->u.echo.eo_create);
246
247         return 0;
248 }
249
250 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
251                  struct lov_stripe_md *ea, struct obd_trans_info *oti)
252 {
253         struct obd_device *obd = class_conn2obd(conn);
254
255         XPROCFS_BUMP_MYCPU_IOSTAT (st_destroy_reqs, 1);
256
257         if (!obd) {
258                 CERROR("invalid client "LPX64"\n", conn->addr);
259                 RETURN(-EINVAL);
260         }
261
262         if (!(oa->o_valid & OBD_MD_FLID)) {
263                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
264                 RETURN(-EINVAL);
265         }
266
267         if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
268                 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
269                 RETURN(-EINVAL);
270         }
271
272         atomic_inc(&obd->u.echo.eo_destroy);
273
274         return 0;
275 }
276
277 static int echo_open(struct lustre_handle *conn, struct obdo *oa,
278                      struct lov_stripe_md *md, struct obd_trans_info *oti)
279 {
280         struct lustre_handle *fh = obdo_handle (oa);
281         struct obd_device    *obd = class_conn2obd (conn);
282
283         XPROCFS_BUMP_MYCPU_IOSTAT (st_open_reqs, 1);
284
285         if (!obd) {
286                 CERROR ("invalid client "LPX64"\n", conn->addr);
287                 return (-EINVAL);
288         }
289
290         if (!(oa->o_valid & OBD_MD_FLID)) {
291                 CERROR ("obdo missing FLID valid flag: %08x\n", oa->o_valid);
292                 return (-EINVAL);
293         }
294
295         fh->addr = oa->o_id;
296         fh->cookie = ECHO_HANDLE_MAGIC;
297
298         oa->o_valid |= OBD_MD_FLHANDLE;
299         return 0;
300 }
301
302 static int echo_close(struct lustre_handle *conn, struct obdo *oa,
303                       struct lov_stripe_md *md, struct obd_trans_info *oti)
304 {
305         struct lustre_handle *fh = obdo_handle (oa);
306         struct obd_device    *obd = class_conn2obd(conn);
307
308         XPROCFS_BUMP_MYCPU_IOSTAT (st_close_reqs, 1);
309
310         if (!obd) {
311                 CERROR("invalid client "LPX64"\n", conn->addr);
312                 return (-EINVAL);
313         }
314
315         if (!(oa->o_valid & OBD_MD_FLHANDLE)) {
316                 CERROR("obdo missing FLHANDLE valid flag: %08x\n", oa->o_valid);
317                 return (-EINVAL);
318         }
319
320         if (fh->cookie != ECHO_HANDLE_MAGIC) {
321                 CERROR ("invalid file handle on close: "LPX64"\n", fh->cookie);
322                 return (-EINVAL);
323         }
324
325         return 0;
326 }
327
328 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
329                         struct lov_stripe_md *md)
330 {
331         struct obd_device *obd = class_conn2obd(conn);
332         obd_id id = oa->o_id;
333
334         XPROCFS_BUMP_MYCPU_IOSTAT (st_getattr_reqs, 1);
335
336         if (!obd) {
337                 CERROR("invalid client "LPX64"\n", conn->addr);
338                 RETURN(-EINVAL);
339         }
340
341         if (!(oa->o_valid & OBD_MD_FLID)) {
342                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
343                 RETURN(-EINVAL);
344         }
345
346         obdo_cpy_md(oa, &obd->u.echo.oa, oa->o_valid);
347         oa->o_id = id;
348
349         return 0;
350 }
351
352 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
353                         struct lov_stripe_md *md, struct obd_trans_info *oti)
354 {
355         struct obd_device *obd = class_conn2obd(conn);
356
357         XPROCFS_BUMP_MYCPU_IOSTAT (st_setattr_reqs, 1);
358
359         if (!obd) {
360                 CERROR("invalid client "LPX64"\n", conn->addr);
361                 RETURN(-EINVAL);
362         }
363
364         if (!(oa->o_valid & OBD_MD_FLID)) {
365                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
366                 RETURN(-EINVAL);
367         }
368
369         memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
370
371         atomic_inc(&obd->u.echo.eo_setattr);
372
373         return 0;
374 }
375
376 /* This allows us to verify that desc_private is passed unmolested */
377 #define DESC_PRIV 0x10293847
378
379 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
380                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
381                 struct niobuf_local *res, void **desc_private,
382                 struct obd_trans_info *oti)
383 {
384         struct obd_device *obd;
385         struct niobuf_local *r = res;
386         int rc = 0;
387         int i;
388         ENTRY;
389
390         if ((cmd & OBD_BRW_WRITE) != 0)
391                 XPROCFS_BUMP_MYCPU_IOSTAT (st_write_reqs, 1);
392         else
393                 XPROCFS_BUMP_MYCPU_IOSTAT (st_read_reqs, 1);
394
395         obd = class_conn2obd(conn);
396         if (!obd) {
397                 CERROR("invalid client "LPX64"\n", conn->addr);
398                 RETURN(-EINVAL);
399         }
400
401         memset(res, 0, sizeof(*res) * niocount);
402
403         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
404                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
405
406         *desc_private = (void *)DESC_PRIV;
407
408         obd_kmap_get(niocount, 1);
409
410         for (i = 0; i < objcount; i++, obj++) {
411                 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
412                 int isobj0 = obj->ioo_id == 0;
413                 int verify = !isobj0;
414                 int j;
415
416                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
417
418                         if (isobj0 &&
419                             (nb->offset >> PAGE_SHIFT) < ECHO_OBJECT0_NPAGES) {
420                                 r->page = echo_object0_pages[nb->offset >>
421                                                              PAGE_SHIFT];
422                                 /* Take extra ref so __free_pages() can be called OK */
423                                 get_page (r->page);
424                         } else {
425                                 r->page = alloc_pages(gfp_mask, 0);
426                                 if (r->page == NULL) {
427                                         CERROR("can't get page %u/%u for id "
428                                                LPU64"\n",
429                                                j, obj->ioo_bufcnt, obj->ioo_id);
430                                         GOTO(preprw_cleanup, rc = -ENOMEM);
431                                 }
432                         }
433
434                         atomic_inc(&obd->u.echo.eo_prep);
435
436                         r->offset = nb->offset;
437                         r->addr = kmap(r->page);
438                         r->len = nb->len;
439
440                         CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
441                                r->page, r->addr, r->offset);
442
443                         if (cmd == OBD_BRW_READ) {
444                                 XPROCFS_BUMP_MYCPU_IOSTAT(st_read_bytes,r->len);
445                                 if (verify)
446                                         page_debug_setup(r->addr, r->len,
447                                                          r->offset,obj->ioo_id);
448                         } else {
449                                 XPROCFS_BUMP_MYCPU_IOSTAT(st_write_bytes,
450                                                           r->len);
451                                 if (verify)
452                                         page_debug_setup(r->addr, r->len,
453                                                          0xecc0ecc0ecc0ecc0,
454                                                          0xecc0ecc0ecc0ecc0);
455                         }
456                 }
457         }
458         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
459                atomic_read(&obd->u.echo.eo_prep));
460
461         RETURN(0);
462
463 preprw_cleanup:
464         /* It is possible that we would rather handle errors by  allow
465          * any already-set-up pages to complete, rather than tearing them
466          * all down again.  I believe that this is what the in-kernel
467          * prep/commit operations do.
468          */
469         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
470         while (r-- > res) {
471                 kunmap(r->page);
472                 /* NB if this is an 'object0' page, __free_pages will just
473                  * lose the extra ref gained above */
474                 __free_pages(r->page, 0);
475                 atomic_dec(&obd->u.echo.eo_prep);
476         }
477         obd_kmap_put(niocount);
478         memset(res, 0, sizeof(*res) * niocount);
479
480         return rc;
481 }
482
483 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
484                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
485                   void *desc_private, struct obd_trans_info *oti)
486 {
487         struct obd_device *obd;
488         struct niobuf_local *r = res;
489         int rc = 0;
490         int vrc = 0;
491         int i;
492         ENTRY;
493
494         obd = class_conn2obd(conn);
495         if (!obd) {
496                 CERROR("invalid client "LPX64"\n", conn->addr);
497                 RETURN(-EINVAL);
498         }
499
500         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
501                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
502                        objcount, niocount);
503         } else {
504                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
505                        objcount, niocount);
506         }
507
508         if (niocount && !r) {
509                 CERROR("NULL res niobuf with niocount %d\n", niocount);
510                 RETURN(-EINVAL);
511         }
512
513         LASSERT(desc_private == (void *)DESC_PRIV);
514
515         for (i = 0; i < objcount; i++, obj++) {
516                 int verify = obj->ioo_id != 0;
517                 int j;
518
519                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
520                         struct page *page = r->page;
521                         void *addr;
522
523                         if (!page || !(addr = page_address(page)) ||
524                             !kern_addr_valid(addr)) {
525
526                                 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
527                                        obj->ioo_id, page, j, obj->ioo_bufcnt);
528                                 GOTO(commitrw_cleanup, rc = -EFAULT);
529                         }
530
531                         CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
532                                r->page, addr, r->offset);
533
534                         if (verify) {
535                                 vrc = page_debug_check("echo", addr, r->len,
536                                                        r->offset, obj->ioo_id);
537                                 /* check all the pages always */
538                                 if (vrc != 0 && rc == 0)
539                                         rc = vrc;
540                         }
541
542                         kunmap(page);
543                         /* NB see comment above regarding object0 pages */
544                         obd_kmap_put(1);
545                         __free_pages(page, 0);
546                         atomic_dec(&obd->u.echo.eo_prep);
547                 }
548         }
549         CDEBUG(D_PAGE, "%d pages remain after commit\n",
550                atomic_read(&obd->u.echo.eo_prep));
551         RETURN(rc);
552
553 commitrw_cleanup:
554         CERROR("cleaning up %ld pages (%d obdos)\n",
555                niocount - (long)(r - res) - 1, objcount);
556         while (++r < res + niocount) {
557                 struct page *page = r->page;
558
559                 kunmap(page);
560                 obd_kmap_put(1);
561                 /* NB see comment above regarding object0 pages */
562                 __free_pages(page, 0);
563                 atomic_dec(&obd->u.echo.eo_prep);
564         }
565         return rc;
566 }
567
568 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
569 {
570         ENTRY;
571
572         spin_lock_init(&obddev->u.echo.eo_lock);
573         obddev->u.echo.eo_lastino = ECHO_INIT_OBJID;
574
575         obddev->obd_namespace =
576                 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
577         if (obddev->obd_namespace == NULL) {
578                 LBUG();
579                 RETURN(-ENOMEM);
580         }
581
582         ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
583                             "echo_ldlm_cb_client", &obddev->obd_ldlm_client);
584         RETURN(0);
585 }
586
587 static int echo_cleanup(struct obd_device *obddev)
588 {
589         ENTRY;
590
591         ldlm_namespace_free(obddev->obd_namespace);
592         CERROR("%d prep/commitrw pages leaked\n",
593                atomic_read(&obddev->u.echo.eo_prep));
594
595         RETURN(0);
596 }
597
598 int echo_attach(struct obd_device *dev, obd_count len, void *data)
599 {
600         struct lprocfs_static_vars lvars;
601
602         lprocfs_init_vars(&lvars);
603         return lprocfs_obd_attach(dev, lvars.obd_vars);
604 }
605
606 int echo_detach(struct obd_device *dev)
607 {
608         return lprocfs_obd_detach(dev);
609 }
610
611 static struct obd_ops echo_obd_ops = {
612         o_owner:       THIS_MODULE,
613         o_attach:      echo_attach,
614         o_detach:      echo_detach,
615         o_connect:     echo_connect,
616         o_disconnect:  echo_disconnect,
617         o_create:      echo_create,
618         o_destroy:     echo_destroy,
619         o_open:        echo_open,
620         o_close:       echo_close,
621         o_getattr:     echo_getattr,
622         o_setattr:     echo_setattr,
623         o_preprw:      echo_preprw,
624         o_commitrw:    echo_commitrw,
625         o_setup:       echo_setup,
626         o_cleanup:     echo_cleanup
627 };
628
629 extern int echo_client_init(void);
630 extern void echo_client_cleanup(void);
631
632 static void
633 echo_object0_pages_fini (void)
634 {
635         int     i;
636
637         for (i = 0; i < ECHO_OBJECT0_NPAGES; i++)
638                 if (echo_object0_pages[i] != NULL) {
639                         __free_pages (echo_object0_pages[i], 0);
640                         echo_object0_pages[i] = NULL;
641                 }
642 }
643
644 static int
645 echo_object0_pages_init (void)
646 {
647         struct page *pg;
648         int          i;
649
650         for (i = 0; i < ECHO_OBJECT0_NPAGES; i++) {
651                 int gfp_mask = (i < ECHO_OBJECT0_NPAGES/2) ?
652                         GFP_KERNEL : GFP_HIGHUSER;
653
654                 pg = alloc_pages (gfp_mask, 0);
655                 if (pg == NULL) {
656                         echo_object0_pages_fini ();
657                         return (-ENOMEM);
658                 }
659
660                 memset (kmap (pg), 0, PAGE_SIZE);
661                 kunmap (pg);
662
663                 echo_object0_pages[i] = pg;
664         }
665
666         return (0);
667 }
668
669 static int __init obdecho_init(void)
670 {
671         struct lprocfs_static_vars lvars;
672         int rc;
673
674         printk(KERN_INFO "Lustre Echo OBD driver; info@clusterfs.com\n");
675
676         lprocfs_init_vars(&lvars);
677
678         xprocfs_init ("echo");
679
680         rc = echo_object0_pages_init ();
681         if (rc != 0)
682                 goto failed_0;
683
684         rc = class_register_type(&echo_obd_ops, lvars.module_vars,
685                                  OBD_ECHO_DEVICENAME);
686         if (rc != 0)
687                 goto failed_1;
688
689         rc = echo_client_init();
690         if (rc == 0)
691                 RETURN (0);
692
693         class_unregister_type(OBD_ECHO_DEVICENAME);
694  failed_1:
695         echo_object0_pages_fini ();
696  failed_0:
697         xprocfs_fini ();
698
699         RETURN(rc);
700 }
701
702 static void __exit obdecho_exit(void)
703 {
704         echo_client_cleanup();
705         class_unregister_type(OBD_ECHO_DEVICENAME);
706         echo_object0_pages_fini ();
707         xprocfs_fini ();
708 }
709
710 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
711 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
712 MODULE_LICENSE("GPL");
713
714 module_init(obdecho_init);
715 module_exit(obdecho_exit);