Whamcloud - gitweb
dc4b3e7251391ee295a648741f1dd06d1b1d39f7
[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.22 2002/08/12 22:25:53 pschwan Exp $";
15 #define OBDECHO_VERSION "$Revision: 1.22 $"
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_dlm.h>
36
37 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
38 static struct obdo OA;
39 static obd_count GEN;
40 static long echo_pages = 0;
41
42 static atomic_t echo_page_rws;
43 static atomic_t echo_getattrs;
44
45 #define ECHO_PROC_STAT "sys/obdecho"
46
47 int
48 echo_proc_read (char *page, char **start, off_t off, int count, int *eof, void *data)
49 {
50         int                len;
51         int                attrs = atomic_read (&echo_getattrs);
52         int                pages = atomic_read (&echo_page_rws);
53         
54         *eof = 1;
55         if (off != 0)
56                 return (0);
57         
58         len = sprintf (page, "%d %d\n", pages, attrs);
59         
60         *start = page;
61         return (len);
62 }
63
64 int
65 echo_proc_write (struct file *file, const char *ubuffer, unsigned long count, void *data)
66 {
67         /* Ignore what we've been asked to write, and just zero the stats counters */
68         atomic_set (&echo_page_rws, 0);
69         atomic_set (&echo_getattrs, 0);
70         
71         return (count);
72 }
73
74 void
75 echo_proc_init(void)
76 {
77         struct proc_dir_entry *entry = create_proc_entry (ECHO_PROC_STAT, S_IFREG | S_IRUGO | S_IWUSR, NULL);
78
79         if (entry == NULL) 
80         {
81                 CERROR("couldn't create proc entry %s\n", ECHO_PROC_STAT);
82                 return;
83         }
84
85         entry->data = NULL;
86         entry->read_proc = echo_proc_read;
87         entry->write_proc = echo_proc_write;
88 }
89
90 void 
91 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, NULL);
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 int echo_getattr(struct lustre_handle *conn, struct obdo *oa, 
122                         struct lov_stripe_md *md)
123 {
124         memcpy(oa, &OA, sizeof(*oa));
125         oa->o_mode = ++GEN;
126
127         atomic_inc (&echo_getattrs);
128
129         return 0;
130 }
131
132 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
133                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
134                 struct niobuf_local *res, void **desc_private)
135 {
136         struct niobuf_local *r = res;
137         int rc = 0;
138         int i;
139
140         ENTRY;
141
142         memset(res, 0, sizeof(*res) * niocount);
143
144         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
145                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
146
147         for (i = 0; i < objcount; i++, obj++) {
148                 int j;
149
150                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
151                         unsigned long address;
152
153                         address = get_zeroed_page(GFP_KERNEL);
154                         if (!address) {
155                                 CERROR("can't get new page %d/%d for id %Ld\n",
156                                        j, obj->ioo_bufcnt,
157                                        (unsigned long long)obj->ioo_id);
158                                 GOTO(preprw_cleanup, rc = -ENOMEM);
159                         }
160                         echo_pages++;
161
162                         r->offset = nb->offset;
163                         r->page = virt_to_page(address);
164                         r->addr = kmap(r->page);
165                         r->len = nb->len;
166                 }
167         }
168         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
169
170         RETURN(0);
171
172 preprw_cleanup:
173         /* It is possible that we would rather handle errors by  allow
174          * any already-set-up pages to complete, rather than tearing them
175          * all down again.  I believe that this is what the in-kernel
176          * prep/commit operations do.
177          */
178         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
179         while (r-- > res) {
180                 kunmap(r->page);
181                 __free_pages(r->page, 0);
182                 echo_pages--;
183         }
184         memset(res, 0, sizeof(*res) * niocount);
185
186         return rc;
187 }
188
189 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
190                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
191                   void *desc_private)
192 {
193         struct niobuf_local *r = res;
194         int rc = 0;
195         int i;
196         ENTRY;
197
198         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
199                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
200
201         if (niocount && !r) {
202                 CERROR("NULL res niobuf with niocount %d\n", niocount);
203                 RETURN(-EINVAL);
204         }
205
206         for (i = 0; i < objcount; i++, obj++) {
207                 int j;
208
209                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
210                         struct page *page = r->page;
211                         unsigned long addr;
212
213                         if (!page ||
214                             !(addr = (unsigned long)page_address(page)) ||
215                             !kern_addr_valid(addr)) {
216
217                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
218                                        page, (unsigned long long)obj->ioo_id, i,
219                                        j, obj->ioo_bufcnt);
220                                 GOTO(commitrw_cleanup, rc = -EFAULT);
221                         }
222
223                         atomic_inc (&echo_page_rws);
224                         
225                         kunmap(page);
226                         __free_pages(page, 0);
227                         echo_pages--;
228                 }
229         }
230         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
231         RETURN(0);
232
233 commitrw_cleanup:
234         CERROR("cleaning up %ld pages (%d obdos)\n",
235                niocount - (long)(r - res) - 1, objcount);
236         while (++r < res + niocount) {
237                 struct page *page = r->page;
238
239                 kunmap(page);
240                 __free_pages(page, 0);
241                 echo_pages--;
242         }
243         return rc;
244 }
245
246 static int echo_setup(struct obd_device *obddev, obd_count len, void *buf)
247 {
248         ENTRY;
249
250         obddev->obd_namespace =
251                 ldlm_namespace_new("echo-tgt", LDLM_NAMESPACE_SERVER);
252         if (obddev->obd_namespace == NULL) {
253                 LBUG();
254                 RETURN(-ENOMEM);
255         }
256
257         RETURN(0);
258 }
259
260 static int echo_cleanup(struct obd_device *obddev)
261 {
262         ENTRY;
263
264         ldlm_namespace_free(obddev->obd_namespace);
265
266         RETURN(0);
267 }
268
269 struct obd_ops echo_obd_ops = {
270         o_connect:     echo_connect,
271         o_disconnect:  echo_disconnect,
272         o_getattr:     echo_getattr,
273         o_preprw:      echo_preprw,
274         o_commitrw:    echo_commitrw,
275         o_setup:       echo_setup,
276         o_cleanup:     echo_cleanup
277 };
278
279 static int __init obdecho_init(void)
280 {
281         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION " info@clusterfs.com\n");
282
283         echo_proc_init();
284         
285         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
286 }
287
288 static void __exit obdecho_exit(void)
289 {
290         echo_proc_fini ();
291         
292         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
293         class_unregister_type(OBD_ECHO_DEVICENAME);
294 }
295
296 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
297 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
298 MODULE_LICENSE("GPL"); 
299
300 module_init(obdecho_init);
301 module_exit(obdecho_exit);