Whamcloud - gitweb
Use req->page instead of req->addr inside echo_commitrw, as the latter is
[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 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 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, obj->ioo_id);
93                                 rc = -ENOMEM;
94                                 EXIT;
95                                 goto preprw_cleanup;
96                         }
97                         echo_pages++;
98
99                         /*
100                         if (cmd == OBD_BRW_READ) {
101                                 __u64 *data = address;
102
103                                 data[0] = obj->ioo_id;
104                                 data[1] = j;
105                                 data[2] = nb->offset;
106                                 data[3] = nb->len;
107                         }
108                         */
109
110                         r->addr = address;
111                         r->offset = nb->offset;
112                         r->page = virt_to_page(address);
113                         r->len = nb->len;
114                         // r->flags
115                 }
116         }
117         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
118
119         EXIT;
120         return 0;
121
122 preprw_cleanup:
123         /* It is possible that we would rather handle errors by  allow
124          * any already-set-up pages to complete, rather than tearing them
125          * all down again.  I believe that this is what the in-kernel
126          * prep/commit operations do.
127          */
128         CERROR("cleaning up %d pages (%d obdos)\n", r - res, objcount);
129         while (r-- > res) {
130                 unsigned long addr = r->addr;
131
132                 free_pages(addr, 0);
133                 echo_pages--;
134         }
135         memset(res, 0, sizeof(*res) * niocount);
136
137         return rc;
138 }
139
140 int echo_commitrw(int cmd, struct obd_conn *conn, int objcount,
141                   struct obd_ioobj *obj, int niocount, struct niobuf *res)
142 {
143         struct niobuf *r = res;
144         int rc = 0;
145         int i;
146
147         ENTRY;
148
149         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
150                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
151
152         if (niocount && !r) {
153                 CERROR("NULL res niobuf with niocount %d\n", niocount);
154                 EXIT;
155                 return -EINVAL;
156         }
157
158         for (i = 0; i < objcount; i++, obj++) {
159                 int j;
160
161                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
162                         struct page *page = r->page;
163                         unsigned long addr = (unsigned long)page_address(page);
164
165                         if (!addr || !kern_addr_valid(addr)) {
166                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
167                                        page, obj->ioo_id, i, j,obj->ioo_bufcnt);
168                                 rc = -EFAULT;
169                                 EXIT;
170                                 goto commitrw_cleanup;
171                         }
172
173                         free_pages(addr, 0);
174                         echo_pages--;
175                 }
176         }
177         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
178         EXIT;
179         return 0;
180
181 commitrw_cleanup:
182         CERROR("cleaning up %d pages (%d obdos)\n", niocount - (r - res) - 1,
183                objcount);
184         while (++r < res + niocount) {
185                 struct page *page = r->page;
186                 unsigned long addr = (unsigned long)page_address(page);
187
188                 free_pages(addr, 0);
189                 echo_pages--;
190         }
191         return rc;
192 }
193
194 struct obd_ops echo_obd_ops = {
195         o_connect:     echo_connect,
196         o_disconnect:  echo_disconnect,
197         o_getattr:     echo_getattr,
198         o_preprw:      echo_preprw,
199         o_commitrw:    echo_commitrw,
200 };
201
202
203 static int __init obdecho_init(void)
204 {
205         printk(KERN_INFO "Echo OBD driver  v0.001, braam@clusterfs.com\n");
206
207         return obd_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
208 }
209
210 static void __exit obdecho_exit(void)
211 {
212         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
213         obd_unregister_type(OBD_ECHO_DEVICENAME);
214 }
215
216 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
217 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver v1.0");
218 MODULE_LICENSE("GPL"); 
219
220 module_init(obdecho_init);
221 module_exit(obdecho_exit);