Whamcloud - gitweb
Remove fancy but broken statistics from obdecho.
[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/obdecho/echo.c
5  *
6  * Copyright (C) 2001, 2002 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  * and Andreas Dilger <adilger@clusterfs.com>
13  */
14
15 static char rcsid[] __attribute ((unused)) = "$Id: echo.c,v 1.31 2002/09/04 16:29:14 adilger Exp $";
16 #define OBDECHO_VERSION "$Revision: 1.31 $"
17
18 #define EXPORT_SYMTAB
19
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/quotaops.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <asm/unistd.h>
30
31 #define DEBUG_SUBSYSTEM S_ECHO
32
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_echo.h>
36 #include <linux/lustre_debug.h>
37 #include <linux/lustre_dlm.h>
38
39 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
40
41 static atomic_t echo_page_rws;
42 static atomic_t echo_getattrs;
43
44 #define ECHO_PROC_STAT "sys/obdecho"
45
46 int echo_proc_read(char *page, char **start, off_t off, int count, int *eof,
47                    void *data)
48 {
49         struct obd_device *obd;
50         long long attrs = atomic_read(&echo_getattrs);
51         long long pages = atomic_read(&echo_page_rws);
52         int len;
53         int i;
54
55         *eof = 1;
56         if (off != 0)
57                 return (0);
58
59         len = sprintf(page, "%Ld %Ld\n", attrs, pages);
60
61         *start = page;
62         return (len);
63 }
64
65 int echo_proc_write(struct file *file, const char *ubuffer,
66                     unsigned long count, void *data)
67 {
68         /* Ignore what we've been asked to write, and just zero the counters */
69         atomic_set (&echo_page_rws, 0);
70         atomic_set (&echo_getattrs, 0);
71
72         return (count);
73 }
74
75 void echo_proc_init(void)
76 {
77         struct proc_dir_entry *entry;
78
79         entry = create_proc_entry(ECHO_PROC_STAT, S_IFREG|S_IRUGO|S_IWUSR,NULL);
80
81         if (entry == NULL) {
82                 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
83                 return;
84         }
85
86         entry->data = NULL;
87         entry->read_proc = echo_proc_read;
88         entry->write_proc = echo_proc_write;
89 }
90
91 void echo_proc_fini(void)
92 {
93         remove_proc_entry(ECHO_PROC_STAT, 0);
94 }
95
96 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd,
97                         char *cluuid)
98 {
99         int rc;
100
101         MOD_INC_USE_COUNT;
102         rc = class_connect(conn, obd, cluuid);
103
104         if (rc)
105                 MOD_DEC_USE_COUNT;
106
107         return rc;
108 }
109
110 static int echo_disconnect(struct lustre_handle *conn)
111 {
112         int rc;
113
114         rc = class_disconnect(conn);
115         if (!rc)
116                 MOD_DEC_USE_COUNT;
117
118         return rc;
119 }
120
121 static __u64 echo_next_id(struct obd_device *obddev)
122 {
123         obd_id id;
124
125         spin_lock(&obddev->u.echo.eo_lock);
126         id = ++obddev->u.echo.eo_lastino;
127         spin_unlock(&obddev->u.echo.eo_lock);
128
129         return id;
130 }
131
132 int echo_create(struct lustre_handle *conn, struct obdo *oa,
133                 struct lov_stripe_md **ea)
134 {
135         struct obd_device *obd = class_conn2obd(conn);
136
137         if (!obd) {
138                 CERROR("invalid client %Lx\n", conn->addr);
139                 return -EINVAL;
140         }
141
142         if (!(oa->o_mode && S_IFMT)) {
143                 CERROR("filter obd: no type!\n");
144                 return -ENOENT;
145         }
146
147         if (!(oa->o_valid & OBD_MD_FLMODE)) {
148                 CERROR("invalid o_valid %08x\n", oa->o_valid);
149                 return -EINVAL;
150         }
151
152         oa->o_id = echo_next_id(obd);
153         oa->o_valid = OBD_MD_FLID;
154         atomic_inc(&obd->u.echo.eo_create);
155
156         return 0;
157 }
158
159 int echo_destroy(struct lustre_handle *conn, struct obdo *oa,
160                  struct lov_stripe_md *ea)
161 {
162         struct obd_device *obd = class_conn2obd(conn);
163
164         if (!obd) {
165                 CERROR("invalid client "LPX64"\n", conn->addr);
166                 RETURN(-EINVAL);
167         }
168
169         if (!(oa->o_valid & OBD_MD_FLID)) {
170                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
171                 RETURN(-EINVAL);
172         }
173
174         if (oa->o_id > obd->u.echo.eo_lastino) {
175                 CERROR("bad destroy objid: %Ld\n", (long long)oa->o_id);
176                 RETURN(-EINVAL);
177         }
178
179         atomic_inc(&obd->u.echo.eo_destroy);
180
181         return 0;
182 }
183
184 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa,
185                         struct lov_stripe_md *md)
186 {
187         struct obd_device *obd = class_conn2obd(conn);
188         obd_id id = oa->o_id;
189
190         if (!obd) {
191                 CERROR("invalid client "LPX64"\n", conn->addr);
192                 RETURN(-EINVAL);
193         }
194
195         if (!(oa->o_valid & OBD_MD_FLID)) {
196                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
197                 RETURN(-EINVAL);
198         }
199
200         memcpy(oa, &obd->u.echo.oa, sizeof(*oa));
201         oa->o_id = id;
202         oa->o_valid |= OBD_MD_FLID;
203
204         atomic_inc(&echo_getattrs);
205
206         return 0;
207 }
208
209 static int echo_setattr(struct lustre_handle *conn, struct obdo *oa,
210                         struct lov_stripe_md *md)
211 {
212         struct obd_device *obd = class_conn2obd(conn);
213
214         if (!obd) {
215                 CERROR("invalid client "LPX64"\n", conn->addr);
216                 RETURN(-EINVAL);
217         }
218
219         if (!(oa->o_valid & OBD_MD_FLID)) {
220                 CERROR("obdo missing FLID valid flag: %08x\n", oa->o_valid);
221                 RETURN(-EINVAL);
222         }
223
224         memcpy(&obd->u.echo.oa, oa, sizeof(*oa));
225
226         atomic_inc(&obd->u.echo.eo_setattr);
227
228         return 0;
229 }
230
231 /* This allows us to verify that desc_private is passed unmolested */
232 #define DESC_PRIV 0x10293847
233
234 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
235                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
236                 struct niobuf_local *res, void **desc_private)
237 {
238         struct obd_device *obd;
239         struct niobuf_local *r = res;
240         int rc = 0;
241         int i;
242
243         ENTRY;
244
245         obd = class_conn2obd(conn);
246         if (!obd) {
247                 CERROR("invalid client "LPX64"\n", conn->addr);
248                 RETURN(-EINVAL);
249         }
250
251         memset(res, 0, sizeof(*res) * niocount);
252
253         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
254                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
255
256         *desc_private = (void *)DESC_PRIV;
257
258         for (i = 0; i < objcount; i++, obj++) {
259                 int gfp_mask = (obj->ioo_id & 1) ? GFP_HIGHUSER : GFP_KERNEL;
260                 int verify = obj->ioo_id != 0;
261                 int j;
262
263                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
264                         r->page = alloc_pages(gfp_mask, 0);
265                         if (!r->page) {
266                                 CERROR("can't get page %d/%d for id "LPU64"\n",
267                                        j, obj->ioo_bufcnt, obj->ioo_id);
268                                 GOTO(preprw_cleanup, rc = -ENOMEM);
269                         }
270                         atomic_inc(&obd->u.echo.eo_prep);
271
272                         r->offset = nb->offset;
273                         r->addr = kmap(r->page);
274                         r->len = nb->len;
275
276                         CDEBUG(D_PAGE, "$$$$ get page %p, addr %p@"LPU64"\n",
277                                r->page, r->addr, r->offset);
278
279                         if (verify && cmd == OBD_BRW_READ)
280                                 page_debug_setup(r->addr, r->len, r->offset,
281                                                  obj->ioo_id);
282                         else if (verify)
283                                 page_debug_setup(r->addr, r->len,
284                                                  0xecc0ecc0ecc0ecc0,
285                                                  0xecc0ecc0ecc0ecc0);
286                 }
287         }
288         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
289                atomic_read(&obd->u.echo.eo_prep));
290
291         RETURN(0);
292
293 preprw_cleanup:
294         /* It is possible that we would rather handle errors by  allow
295          * any already-set-up pages to complete, rather than tearing them
296          * all down again.  I believe that this is what the in-kernel
297          * prep/commit operations do.
298          */
299         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
300         while (r-- > res) {
301                 kunmap(r->page);
302                 __free_pages(r->page, 0);
303                 atomic_dec(&obd->u.echo.eo_prep);
304         }
305         memset(res, 0, sizeof(*res) * niocount);
306
307         return rc;
308 }
309
310 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
311                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
312                   void *desc_private)
313 {
314         struct obd_device *obd;
315         struct niobuf_local *r = res;
316         int rc = 0;
317         int i;
318         ENTRY;
319
320         obd = class_conn2obd(conn);
321         if (!obd) {
322                 CERROR("invalid client "LPX64"\n", conn->addr);
323                 RETURN(-EINVAL);
324         }
325
326         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
327                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
328                        objcount, niocount);
329         } else {
330                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
331                        objcount, niocount);
332         }
333
334         if (niocount && !r) {
335                 CERROR("NULL res niobuf with niocount %d\n", niocount);
336                 RETURN(-EINVAL);
337         }
338
339         LASSERT(desc_private == (void *)DESC_PRIV);
340
341         for (i = 0; i < objcount; i++, obj++) {
342                 int verify = obj->ioo_id != 0;
343                 int j;
344
345                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
346                         struct page *page = r->page;
347                         void *addr;
348
349                         if (!page || !(addr = page_address(page)) ||
350                             !kern_addr_valid(addr)) {
351
352                                 CERROR("bad page objid "LPU64":%p, buf %d/%d\n",
353                                        obj->ioo_id, page, j, obj->ioo_bufcnt);
354                                 GOTO(commitrw_cleanup, rc = -EFAULT);
355                         }
356
357                         atomic_inc(&echo_page_rws);
358
359                         CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
360                                r->page, addr, r->offset);
361
362                         if (verify)
363                                 page_debug_check("echo", addr, r->len,
364                                                  r->offset, obj->ioo_id);
365
366                         kunmap(page);
367                         __free_pages(page, 0);
368                         atomic_dec(&obd->u.echo.eo_prep);
369                 }
370         }
371         CDEBUG(D_PAGE, "%d pages remain after commit\n",
372                atomic_read(&obd->u.echo.eo_prep));
373         RETURN(0);
374
375 commitrw_cleanup:
376         CERROR("cleaning up %ld pages (%d obdos)\n",
377                niocount - (long)(r - res) - 1, objcount);
378         while (++r < res + niocount) {
379                 struct page *page = r->page;
380
381                 kunmap(page);
382                 __free_pages(page, 0);
383                 atomic_dec(&obd->u.echo.eo_prep);
384         }
385         return rc;
386 }
387
388 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
389 {
390         ENTRY;
391
392         obddev->obd_namespace =
393                 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
394         if (obddev->obd_namespace == NULL) {
395                 LBUG();
396                 RETURN(-ENOMEM);
397         }
398
399         RETURN(0);
400 }
401
402 static int echo_cleanup(struct obd_device *obddev)
403 {
404         ENTRY;
405
406         ldlm_namespace_free(obddev->obd_namespace);
407         CERROR("%d prep/commitrw pages leaked\n",
408                atomic_read(&obddev->u.echo.eo_prep));
409
410         RETURN(0);
411 }
412
413 struct obd_ops echo_obd_ops = {
414         o_connect:      echo_connect,
415         o_disconnect:   echo_disconnect,
416         o_create:       echo_create,
417         o_destroy:      echo_destroy,
418         o_getattr:      echo_getattr,
419         o_setattr:      echo_setattr,
420         o_preprw:       echo_preprw,
421         o_commitrw:     echo_commitrw,
422         o_setup:        echo_setup,
423         o_cleanup:      echo_cleanup
424 };
425
426 static int __init obdecho_init(void)
427 {
428         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION
429                " info@clusterfs.com\n");
430
431         echo_proc_init();
432
433         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
434 }
435
436 static void __exit obdecho_exit(void)
437 {
438         echo_proc_fini();
439
440         class_unregister_type(OBD_ECHO_DEVICENAME);
441 }
442
443 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
444 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
445 MODULE_LICENSE("GPL");
446
447 module_init(obdecho_init);
448 module_exit(obdecho_exit);