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