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