Whamcloud - gitweb
Fix problem with multiple connections to the same local device.
[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.17 2002/07/05 20:05:27 rread Exp $";
15 #define OBDECHO_VERSION "$Revision: 1.17 $"
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 <asm/unistd.h>
27
28 #define DEBUG_SUBSYSTEM S_ECHO
29
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
32 #include <linux/obd_echo.h>
33
34 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
35 static struct obdo OA;
36 static obd_count GEN;
37 static long echo_pages = 0;
38
39 static int echo_connect(struct lustre_handle *conn, struct obd_device *obd)
40 {
41         int rc;
42
43         MOD_INC_USE_COUNT;
44         rc = class_connect(conn, obd);
45
46         if (rc)
47                 MOD_DEC_USE_COUNT;
48
49         return rc;
50 }
51
52 static int echo_disconnect(struct lustre_handle *conn)
53 {
54         int rc;
55
56         rc = class_disconnect(conn);
57         if (!rc)
58                 MOD_DEC_USE_COUNT;
59
60         return rc;
61 }
62
63 static int echo_getattr(struct lustre_handle *conn, struct obdo *oa)
64 {
65         memcpy(oa, &OA, sizeof(*oa));
66         oa->o_mode = ++GEN;
67
68         return 0;
69 }
70
71 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
72                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
73                 struct niobuf_local *res, void **desc_private)
74 {
75         struct niobuf_local *r = res;
76         int rc = 0;
77         int i;
78
79         ENTRY;
80
81         memset(res, 0, sizeof(*res) * niocount);
82
83         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
84                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
85
86         for (i = 0; i < objcount; i++, obj++) {
87                 int j;
88
89                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
90                         unsigned long address;
91
92                         address = get_zeroed_page(GFP_KERNEL);
93                         if (!address) {
94                                 CERROR("can't get new page %d/%d for id %Ld\n",
95                                        j, obj->ioo_bufcnt,
96                                        (unsigned long long)obj->ioo_id);
97                                 GOTO(preprw_cleanup, rc = -ENOMEM);
98                         }
99                         echo_pages++;
100
101                         r->offset = nb->offset;
102                         r->page = virt_to_page(address);
103                         r->addr = kmap(r->page);
104                         r->len = nb->len;
105                 }
106         }
107         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
108
109         RETURN(0);
110
111 preprw_cleanup:
112         /* It is possible that we would rather handle errors by  allow
113          * any already-set-up pages to complete, rather than tearing them
114          * all down again.  I believe that this is what the in-kernel
115          * prep/commit operations do.
116          */
117         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
118         while (r-- > res) {
119                 kunmap(r->page);
120                 __free_pages(r->page, 0);
121                 echo_pages--;
122         }
123         memset(res, 0, sizeof(*res) * niocount);
124
125         return rc;
126 }
127
128 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
129                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
130                   void *desc_private)
131 {
132         struct niobuf_local *r = res;
133         int rc = 0;
134         int i;
135         ENTRY;
136
137         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
138                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
139
140         if (niocount && !r) {
141                 CERROR("NULL res niobuf with niocount %d\n", niocount);
142                 RETURN(-EINVAL);
143         }
144
145         for (i = 0; i < objcount; i++, obj++) {
146                 int j;
147
148                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
149                         struct page *page = r->page;
150                         unsigned long addr;
151
152                         if (!page ||
153                             !(addr = (unsigned long)page_address(page)) ||
154                             !kern_addr_valid(addr)) {
155
156                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
157                                        page, (unsigned long long)obj->ioo_id, i,
158                                        j, obj->ioo_bufcnt);
159                                 GOTO(commitrw_cleanup, rc = -EFAULT);
160                         }
161
162                         kunmap(page);
163                         __free_pages(page, 0);
164                         echo_pages--;
165                 }
166         }
167         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
168         RETURN(0);
169
170 commitrw_cleanup:
171         CERROR("cleaning up %ld pages (%d obdos)\n",
172                niocount - (long)(r - res) - 1, objcount);
173         while (++r < res + niocount) {
174                 struct page *page = r->page;
175
176                 kunmap(page);
177                 __free_pages(page, 0);
178                 echo_pages--;
179         }
180         return rc;
181 }
182
183 struct obd_ops echo_obd_ops = {
184         o_connect:     echo_connect,
185         o_disconnect:  echo_disconnect,
186         o_getattr:     echo_getattr,
187         o_preprw:      echo_preprw,
188         o_commitrw:    echo_commitrw,
189 };
190
191 static int __init obdecho_init(void)
192 {
193         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION " info@clusterfs.com\n");
194
195         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
196 }
197
198 static void __exit obdecho_exit(void)
199 {
200         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
201         class_unregister_type(OBD_ECHO_DEVICENAME);
202 }
203
204 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
205 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
206 MODULE_LICENSE("GPL"); 
207
208 module_init(obdecho_init);
209 module_exit(obdecho_exit);