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