Whamcloud - gitweb
- small include changes needed compile on powerpc
[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  *  linux/fs/ext2_obd/ext2_obd.c
5  *
6  * Copyright (C) 2001  Cluster File Systems, Inc.
7  *
8  * This code is issued under the GNU General Public License.
9  * See the file COPYING in this distribution
10  *
11  * by Peter Braam <braam@clusterfs.com>
12  */
13
14 static char rcsid[] __attribute ((unused)) = "$Id: echo.c,v 1.18 2002/07/26 16:54:55 rread Exp $";
15 #define OBDECHO_VERSION "$Revision: 1.18 $"
16
17 #define EXPORT_SYMTAB
18
19 #include <linux/version.h>
20 #include <linux/module.h>
21 #include <linux/fs.h>
22 #include <linux/stat.h>
23 #include <linux/locks.h>
24 #include <linux/ext2_fs.h>
25 #include <linux/quotaops.h>
26 #include <linux/init.h>
27 #include <asm/unistd.h>
28
29 #define DEBUG_SUBSYSTEM S_ECHO
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_echo.h>
34
35 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
36 static struct obdo OA;
37 static obd_count GEN;
38 static long echo_pages = 0;
39
40 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd)
41 {
42         int rc;
43
44         MOD_INC_USE_COUNT;
45         rc = class_connect(conn, obd);
46
47         if (rc)
48                 MOD_DEC_USE_COUNT;
49
50         return rc;
51 }
52
53 static int echo_disconnect(struct lustre_handle *conn)
54 {
55         int rc;
56
57         rc = class_disconnect(conn);
58         if (!rc)
59                 MOD_DEC_USE_COUNT;
60
61         return rc;
62 }
63
64 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa)
65 {
66         memcpy(oa, &OA, sizeof(*oa));
67         oa->o_mode = ++GEN;
68
69         return 0;
70 }
71
72 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
73                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
74                 struct niobuf_local *res, void **desc_private)
75 {
76         struct niobuf_local *r = res;
77         int rc = 0;
78         int i;
79
80         ENTRY;
81
82         memset(res, 0, sizeof(*res) * niocount);
83
84         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
85                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
86
87         for (i = 0; i < objcount; i++, obj++) {
88                 int j;
89
90                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
91                         unsigned long address;
92
93                         address = get_zeroed_page(GFP_KERNEL);
94                         if (!address) {
95                                 CERROR("can't get new page %d/%d for id %Ld\n",
96                                        j, obj->ioo_bufcnt,
97                                        (unsigned long long)obj->ioo_id);
98                                 GOTO(preprw_cleanup, rc = -ENOMEM);
99                         }
100                         echo_pages++;
101
102                         r->offset = nb->offset;
103                         r->page = virt_to_page(address);
104                         r->addr = kmap(r->page);
105                         r->len = nb->len;
106                 }
107         }
108         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
109
110         RETURN(0);
111
112 preprw_cleanup:
113         /* It is possible that we would rather handle errors by  allow
114          * any already-set-up pages to complete, rather than tearing them
115          * all down again.  I believe that this is what the in-kernel
116          * prep/commit operations do.
117          */
118         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
119         while (r-- > res) {
120                 kunmap(r->page);
121                 __free_pages(r->page, 0);
122                 echo_pages--;
123         }
124         memset(res, 0, sizeof(*res) * niocount);
125
126         return rc;
127 }
128
129 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
130                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
131                   void *desc_private)
132 {
133         struct niobuf_local *r = res;
134         int rc = 0;
135         int i;
136         ENTRY;
137
138         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
139                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
140
141         if (niocount && !r) {
142                 CERROR("NULL res niobuf with niocount %d\n", niocount);
143                 RETURN(-EINVAL);
144         }
145
146         for (i = 0; i < objcount; i++, obj++) {
147                 int j;
148
149                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
150                         struct page *page = r->page;
151                         unsigned long addr;
152
153                         if (!page ||
154                             !(addr = (unsigned long)page_address(page)) ||
155                             !kern_addr_valid(addr)) {
156
157                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
158                                        page, (unsigned long long)obj->ioo_id, i,
159                                        j, obj->ioo_bufcnt);
160                                 GOTO(commitrw_cleanup, rc = -EFAULT);
161                         }
162
163                         kunmap(page);
164                         __free_pages(page, 0);
165                         echo_pages--;
166                 }
167         }
168         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
169         RETURN(0);
170
171 commitrw_cleanup:
172         CERROR("cleaning up %ld pages (%d obdos)\n",
173                niocount - (long)(r - res) - 1, objcount);
174         while (++r < res + niocount) {
175                 struct page *page = r->page;
176
177                 kunmap(page);
178                 __free_pages(page, 0);
179                 echo_pages--;
180         }
181         return rc;
182 }
183
184 struct obd_ops echo_obd_ops = {
185         o_connect:     echo_connect,
186         o_disconnect:  echo_disconnect,
187         o_getattr:     echo_getattr,
188         o_preprw:      echo_preprw,
189         o_commitrw:    echo_commitrw,
190 };
191
192 static int __init obdecho_init(void)
193 {
194         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION " info@clusterfs.com\n");
195
196         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
197 }
198
199 static void __exit obdecho_exit(void)
200 {
201         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
202         class_unregister_type(OBD_ECHO_DEVICENAME);
203 }
204
205 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
206 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
207 MODULE_LICENSE("GPL"); 
208
209 module_init(obdecho_init);
210 module_exit(obdecho_exit);