Whamcloud - gitweb
- move the peter branch changes to the head
[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.19 2002/07/31 20:49:37 braam Exp $";
15 #define OBDECHO_VERSION "$Revision: 1.19 $"
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 {
97         int rc;
98
99         MOD_INC_USE_COUNT;
100         rc = class_connect(conn, obd);
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 int echo_getattr(struct lustre_handle *conn, struct obdo *oa, 
120                         struct lov_stripe_md *md)
121 {
122         memcpy(oa, &OA, sizeof(*oa));
123         oa->o_mode = ++GEN;
124
125         atomic_inc (&echo_getattrs);
126
127         return 0;
128 }
129
130 int echo_preprw(int cmd, struct lustre_handle *conn, int objcount,
131                 struct obd_ioobj *obj, int niocount, struct niobuf_remote *nb,
132                 struct niobuf_local *res, void **desc_private)
133 {
134         struct niobuf_local *r = res;
135         int rc = 0;
136         int i;
137
138         ENTRY;
139
140         memset(res, 0, sizeof(*res) * niocount);
141
142         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
143                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
144
145         for (i = 0; i < objcount; i++, obj++) {
146                 int j;
147
148                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++, r++) {
149                         unsigned long address;
150
151                         address = get_zeroed_page(GFP_KERNEL);
152                         if (!address) {
153                                 CERROR("can't get new page %d/%d for id %Ld\n",
154                                        j, obj->ioo_bufcnt,
155                                        (unsigned long long)obj->ioo_id);
156                                 GOTO(preprw_cleanup, rc = -ENOMEM);
157                         }
158                         echo_pages++;
159
160                         r->offset = nb->offset;
161                         r->page = virt_to_page(address);
162                         r->addr = kmap(r->page);
163                         r->len = nb->len;
164                 }
165         }
166         CDEBUG(D_PAGE, "%ld pages allocated after prep\n", echo_pages);
167
168         RETURN(0);
169
170 preprw_cleanup:
171         /* It is possible that we would rather handle errors by  allow
172          * any already-set-up pages to complete, rather than tearing them
173          * all down again.  I believe that this is what the in-kernel
174          * prep/commit operations do.
175          */
176         CERROR("cleaning up %ld pages (%d obdos)\n", (long)(r - res), objcount);
177         while (r-- > res) {
178                 kunmap(r->page);
179                 __free_pages(r->page, 0);
180                 echo_pages--;
181         }
182         memset(res, 0, sizeof(*res) * niocount);
183
184         return rc;
185 }
186
187 int echo_commitrw(int cmd, struct lustre_handle *conn, int objcount,
188                   struct obd_ioobj *obj, int niocount, struct niobuf_local *res,
189                   void *desc_private)
190 {
191         struct niobuf_local *r = res;
192         int rc = 0;
193         int i;
194         ENTRY;
195
196         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
197                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, niocount);
198
199         if (niocount && !r) {
200                 CERROR("NULL res niobuf with niocount %d\n", niocount);
201                 RETURN(-EINVAL);
202         }
203
204         for (i = 0; i < objcount; i++, obj++) {
205                 int j;
206
207                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, r++) {
208                         struct page *page = r->page;
209                         unsigned long addr;
210
211                         if (!page ||
212                             !(addr = (unsigned long)page_address(page)) ||
213                             !kern_addr_valid(addr)) {
214
215                                 CERROR("bad page %p, id %Ld (%d), buf %d/%d\n",
216                                        page, (unsigned long long)obj->ioo_id, i,
217                                        j, obj->ioo_bufcnt);
218                                 GOTO(commitrw_cleanup, rc = -EFAULT);
219                         }
220
221                         atomic_inc (&echo_page_rws);
222                         
223                         kunmap(page);
224                         __free_pages(page, 0);
225                         echo_pages--;
226                 }
227         }
228         CDEBUG(D_PAGE, "%ld pages remain after commit\n", echo_pages);
229         RETURN(0);
230
231 commitrw_cleanup:
232         CERROR("cleaning up %ld pages (%d obdos)\n",
233                niocount - (long)(r - res) - 1, objcount);
234         while (++r < res + niocount) {
235                 struct page *page = r->page;
236
237                 kunmap(page);
238                 __free_pages(page, 0);
239                 echo_pages--;
240         }
241         return rc;
242 }
243
244 struct obd_ops echo_obd_ops = {
245         o_connect:     echo_connect,
246         o_disconnect:  echo_disconnect,
247         o_getattr:     echo_getattr,
248         o_preprw:      echo_preprw,
249         o_commitrw:    echo_commitrw,
250 };
251
252 static int __init obdecho_init(void)
253 {
254         printk(KERN_INFO "Echo OBD driver " OBDECHO_VERSION " info@clusterfs.com\n");
255
256         echo_proc_init();
257         
258         return class_register_type(&echo_obd_ops, OBD_ECHO_DEVICENAME);
259 }
260
261 static void __exit obdecho_exit(void)
262 {
263         echo_proc_fini ();
264         
265         CERROR("%ld prep/commitrw pages leaked\n", echo_pages);
266         class_unregister_type(OBD_ECHO_DEVICENAME);
267 }
268
269 MODULE_AUTHOR("Cluster Filesystems Inc. <info@clusterfs.com>");
270 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver " OBDECHO_VERSION);
271 MODULE_LICENSE("GPL"); 
272
273 module_init(obdecho_init);
274 module_exit(obdecho_exit);