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