Whamcloud - gitweb
optional highmem buffers in obdecho/test_brw
[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.25 2002/08/21 22:17:39 eeb Exp $";
15 #define OBDECHO_VERSION "$Revision: 1.25 $"
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/proc_fs.h>
27 #include <linux/init.h>
28 #include <asm/unistd.h>
29
30 #define DEBUG_SUBSYSTEM S_ECHO
31
32 #include <linux/obd_support.h>
33 #include <linux/obd_class.h>
34 #include <linux/obd_echo.h>
35 #include <linux/lustre_debug.h>
36 #include <linux/lustre_dlm.h>
37
38 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
39 static struct obdo OA;
40 static obd_count GEN;
41 static long echo_pages = 0;
42
43 static atomic_t echo_page_rws;
44 static atomic_t echo_getattrs;
45
46 int obdecho_highmem = 0;
47
48 #define ECHO_PROC_STAT "sys/obdecho"
49
50 int
51 echo_proc_read (char *page, char **start, off_t off, int count, int *eof, void *data)
52 {
53         int                len;
54         int                attrs = atomic_read (&echo_getattrs);
55         int                pages = atomic_read (&echo_page_rws);
56
57         *eof = 1;
58         if (off != 0)
59                 return (0);
60
61         len = sprintf (page, "%d %d\n", pages, attrs);
62
63         *start = page;
64         return (len);
65 }
66
67 int
68 echo_proc_write (struct file *file, const char *ubuffer, unsigned long count, void *data)
69 {
70         /* Ignore what we've been asked to write, and just zero the stats counters */
71         atomic_set (&echo_page_rws, 0);
72         atomic_set (&echo_getattrs, 0);
73
74         return (count);
75 }
76
77 void
78 echo_proc_init(void)
79 {
80         struct proc_dir_entry *entry;
81
82         entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
83
84         if (entry == NULL) {
85                 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
86                 return;
87         }
88
89         entry->data = NULL;
90         entry->read_proc = echo_proc_read;
91         entry->write_proc = echo_proc_write;
92 }
93
94 void echo_proc_fini(void)
95 {
96         remove_proc_entry(ECHO_PROC_STAT, 0);
97 }
98
99 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
100                         char *cluuid)
101 {
102         int rc;
103
104         MOD_INC_USE_COUNT;
105         rc = class_connect(conn, obd, NULL);
106
107         if (rc)
108                 MOD_DEC_USE_COUNT;
109
110         return rc;
111 }
112
113 static int echo_disconnect(struct lustre_handle *conn)
114 {
115         int rc;
116
117         rc = class_disconnect(conn);
118         if (!rc)
119                 MOD_DEC_USE_COUNT;
120
121         return rc;
122 }
123
124 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
125                         struct lov_stripe_md *md)
126 {
127         memcpy(oa, &OA, sizeof(*oa));
128         oa->o_mode = ++GEN;
129
130         atomic_inc (&echo_getattrs);
131
132         return 0;
133 }
134
135 #define DESC_PRIV 0x10293847
136
137 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
138                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
139                 struct niobuf_local *res, void **desc_private)
140 {
141         struct niobuf_local *r = res;
142         int rc = 0;
143         int i;
144
145         ENTRY;
146
147         memset(res, 0, sizeof(*res) * niocount);
148
149         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
150                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
151
152         *desc_private = (void *)DESC_PRIV;
153
154         for (i = 0; i < objcount; i++, obj++) {
155                 int j;
156
157                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
158                         r->page = alloc_pages(obdecho_highmem ? GFP_HIGHUSER : GFP_KERNEL, 0);
159                         if (!r->page) {
160                                 CERROR("can't get page %d/%d for id "LPU64"\n",
161                                        j, obj->ioo_bufcnt, obj->ioo_id);
162                                 GOTO(preprw_cleanup, rc = -ENOMEM);
163                         }
164                         echo_pages++;
165
166                         r->offset = nb->offset;
167                         r->addr = kmap(r->page);
168                         r->len = nb->len;
169
170                         CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
171                                r->page, r->addr, r->offset);
172                         if (cmd & OBD_BRW_READ)
173                                 page_debug_setup(r->addr, r->len, r->offset,
174                                                  obj->ioo_id);
175                 }
176         }
177         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
178
179         RETURN(0);
180
181 preprw_cleanup:
182         /* It is possible that we would rather handle errors by  allow
183          * any already-set-up pages to complete, rather than tearing them
184          * all down again.  I believe that this is what the in-kernel
185          * prep/commit operations do.
186          */
187         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
188         while (r-- > res) {
189                 kunmap(r->page);
190                 __free_pages(r->page, 0);
191                 echo_pages--;
192         }
193         memset(res, 0, sizeof(*res) * niocount);
194
195         return rc;
196 }
197
198 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
199                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
200                   void *desc_private)
201 {
202         struct niobuf_local *r = res;
203         int rc = 0;
204         int i;
205         ENTRY;
206
207         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
208                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
209
210         if (niocount && !r) {
211                 CERROR("NULL res niobuf with niocount %d\n", niocount);
212                 RETURN(-EINVAL);
213         }
214
215         LASSERT(desc_private == (void *)DESC_PRIV);
216
217         for (i = 0; i < objcount; i++, obj++) {
218                 int j;
219
220                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
221                         struct page *page = r->page;
222                         void *addr;
223
224                         if (!page || !(addr = page_address(page)) ||
225                             !kern_addr_valid(addr)) {
226
227                                 CERROR("bad page "LPU64":%p, buf %d/%d\n",
228                                        obj->ioo_id, page, j, obj->ioo_bufcnt);
229                                 GOTO(commitrw_cleanup, rc = -EFAULT);
230                         }
231
232                         atomic_inc (&echo_page_rws);
233
234                         CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
235                                r->page, addr, r->offset);
236                         if (cmd & OBD_BRW_WRITE)
237                                 page_debug_check("echo", addr, r->len,
238                                                  r->offset, obj->ioo_id);
239
240                         kunmap(page);
241                         __free_pages(page, 0);
242                         echo_pages--;
243                 }
244         }
245         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
246         RETURN(0);
247
248 commitrw_cleanup:
249         CERROR("cleaning up %ld pages (%d obdos)\n",
250                niocount - (long)(r - res) - 1, objcount);
251         while (++r < res + niocount) {
252                 struct page *page = r->page;
253
254                 kunmap(page);
255                 __free_pages(page, 0);
256                 echo_pages--;
257         }
258         return rc;
259 }
260
261 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
262 {
263         ENTRY;
264
265         obddev->obd_namespace =
266                 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
267         if (obddev->obd_namespace == NULL) {
268                 LBUG();
269                 RETURN(-ENOMEM);
270         }
271
272         RETURN(0);
273 }
274
275 static int echo_cleanup(struct obd_device *obddev)
276 {
277         ENTRY;
278
279         ldlm_namespace_free(obddev->obd_namespace);
280
281         RETURN(0);
282 }
283
284 struct obd_ops echo_obd_ops = {
285         o_connect:     echo_connect,
286         o_disconnect:  echo_disconnect,
287         o_getattr:     echo_getattr,
288         o_preprw:      echo_preprw,
289         o_commitrw:    echo_commitrw,
290         o_setup:       echo_setup,
291         o_cleanup:     echo_cleanup
292 };
293
294 static int __init obdecho_init(void)
295 {
296         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION " info@clusterfs.com\n");
297
298         echo_proc_init();
299
300         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
301 }
302
303 static void __exit obdecho_exit(void)
304 {
305         echo_proc_fini ();
306
307         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
308         class_unregister_type(OBD_ECHO_DEVICENAME);
309 }
310
311 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
312 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
313 MODULE_LICENSE("GPL");
314
315 module_init(obdecho_init);
316 module_exit(obdecho_exit);