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