Whamcloud - gitweb
64-bit warning fixes. Someone should take a closer look at ext2_obd.c
[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 #define EXPORT_SYMTAB
15
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/fs.h>
19 #include <linux/stat.h>
20 #include <linux/locks.h>
21 #include <linux/ext2_fs.h>
22 #include <linux/quotaops.h>
23 #include <asm/unistd.h>
24
25 #define DEBUG_SUBSYSTEM S_ECHO
26
27 #include <linux/obd_support.h>
28 #include <linux/obd_class.h>
29 #include <linux/obd_echo.h>
30
31 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
32 static struct obdo OA;
33 static obd_count GEN;
34 static long echo_pages = 0;
35
36 static int echo_connect(struct obd_conn *conn)
37 {
38         int rc;
39
40         MOD_INC_USE_COUNT;
41         rc = gen_connect(conn);
42
43         if (rc)
44                 MOD_DEC_USE_COUNT;
45
46         return rc;
47 }
48
49 static int echo_disconnect(struct obd_conn *conn)
50 {
51         int rc;
52
53         rc = gen_disconnect(conn);
54         if (!rc)
55                 MOD_DEC_USE_COUNT;
56
57         return rc;
58 }
59
60 static int echo_getattr(struct obd_conn *conn, struct obdo *oa)
61 {
62         memcpy(oa, &OA, sizeof(*oa));
63         oa->o_mode = ++GEN;
64
65         return 0;
66 }
67
68 int echo_preprw(int cmd, struct obd_conn *conn, int objcount,
69                 struct obd_ioobj *obj, int niocount, struct niobuf *nb,
70                 struct niobuf *res)
71 {
72         struct niobuf *r = res;
73         int rc = 0;
74         int i;
75
76         ENTRY;
77
78         memset(res, 0, sizeof(*res) * niocount);
79
80         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
81                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
82
83         for (i = 0; i < objcount; i++, obj++) {
84                 int j;
85
86                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
87                         unsigned long address;
88
89                         address = get_zeroed_page(GFP_KERNEL);
90                         if (!address) {
91                                 CERROR("can't get new page %d/%d for id %Ld\n",
92                                        j, obj->ioo_bufcnt,
93                                        (unsigned long long)obj->ioo_id);
94                                 rc = -ENOMEM;
95                                 EXIT;
96                                 goto preprw_cleanup;
97                         }
98                         echo_pages++;
99
100                         /*
101                         if (cmd == OBD_BRW_READ) {
102                                 __u64 *data = address;
103
104                                 data[0] = obj->ioo_id;
105                                 data[1] = j;
106                                 data[2] = nb->offset;
107                                 data[3] = nb->len;
108                         }
109                         */
110
111                         r->addr = address;
112                         r->offset = nb->offset;
113                         r->page = virt_to_page(address);
114                         r->len = nb->len;
115                         // r->flags
116                 }
117         }
118         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
119
120         EXIT;
121         return 0;
122
123 preprw_cleanup:
124         /* It is possible that we would rather handle errors by  allow
125          * any already-set-up pages to complete, rather than tearing them
126          * all down again.  I believe that this is what the in-kernel
127          * prep/commit operations do.
128          */
129         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
130         while (r-- > res) {
131                 unsigned long addr = r->addr;
132
133                 free_pages(addr, 0);
134                 echo_pages--;
135         }
136         memset(res, 0, sizeof(*res) * niocount);
137
138         return rc;
139 }
140
141 int echo_commitrw(int cmd, struct obd_conn *conn, int objcount,
142                   struct obd_ioobj *obj, int niocount, struct niobuf *res)
143 {
144         struct niobuf *r = res;
145         int rc = 0;
146         int i;
147
148         ENTRY;
149
150         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
151                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
152
153         if (niocount && !r) {
154                 CERROR("NULL res niobuf with niocount %d\n", niocount);
155                 EXIT;
156                 return -EINVAL;
157         }
158
159         for (i = 0; i < objcount; i++, obj++) {
160                 int j;
161
162                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
163                         struct page *page = r->page;
164                         unsigned long addr = (unsigned long)page_address(page);
165
166                         if (!addr || !kern_addr_valid(addr)) {
167                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
168                                        page, (unsigned long long)obj->ioo_id, i,
169                                        j, obj->ioo_bufcnt);
170                                 rc = -EFAULT;
171                                 EXIT;
172                                 goto commitrw_cleanup;
173                         }
174
175                         free_pages(addr, 0);
176                         echo_pages--;
177                 }
178         }
179         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
180         EXIT;
181         return 0;
182
183 commitrw_cleanup:
184         CERROR("cleaning up %ld pages (%d obdos)\n",
185                niocount - (long)(r - res) - 1, objcount);
186         while (++r < res + niocount) {
187                 struct page *page = r->page;
188                 unsigned long addr = (unsigned long)page_address(page);
189
190                 free_pages(addr, 0);
191                 echo_pages--;
192         }
193         return rc;
194 }
195
196 struct obd_ops echo_obd_ops = {
197         o_connect:     echo_connect,
198         o_disconnect:  echo_disconnect,
199         o_getattr:     echo_getattr,
200         o_preprw:      echo_preprw,
201         o_commitrw:    echo_commitrw,
202 };
203
204
205 static int __init obdecho_init(void)
206 {
207         printk(KERN_INFO "Echo OBD driver  v0.001, braam@clusterfs.com\n");
208
209         return obd_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
210 }
211
212 static void __exit obdecho_exit(void)
213 {
214         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
215         obd_unregister_type(OBD_ECHO_DEVICENAME);
216 }
217
218 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
219 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver v1.0");
220 MODULE_LICENSE("GPL"); 
221
222 module_init(obdecho_init);
223 module_exit(obdecho_exit);