Whamcloud - gitweb
Pass 1 of debugging and leak cleanup:
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/locks.h>
33 #include <linux/unistd.h>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
38
39 #include <linux/fs.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/miscdevice.h>
44
45 #include <linux/obd_support.h>
46 #include <linux/lustre_lib.h>
47 #include <linux/lustre_idl.h>
48 #include <linux/lustre_mds.h>
49
50 #define REQUEST_MINOR 244
51
52 extern int mds_queue_req(struct ptlrpc_request *);
53
54 /* FIXME: this belongs in some sort of service struct */
55 static int mdc_xid = 0;
56
57 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name,
58                                     int tgtlen, char *tgt)
59 {
60         struct ptlrpc_request *request;
61         int rc;
62         ENTRY; 
63
64         OBD_ALLOC(request, sizeof(*request));
65         if (!request) { 
66                 printk("mds_prep_req: request allocation out of memory\n");
67                 return NULL;
68         }
69
70         memset(request, 0, sizeof(*request));
71         request->rq_xid = mdc_xid++;
72
73         rc = mds_pack_req(name, namelen, tgt, tgtlen,
74                           &request->rq_reqhdr, &(request->rq_req.mds),
75                           &request->rq_reqlen, &request->rq_reqbuf);
76         if (rc) { 
77                 printk("llight request: cannot pack request %d\n", rc); 
78                 return NULL;
79         }
80         printk("--> mds_prep_req: len %d, req %p, tgtlen %d\n", 
81                request->rq_reqlen, request->rq_req.mds, 
82                request->rq_req.mds->tgtlen);
83         request->rq_reqhdr->opc = opcode;
84
85         EXIT;
86         return request;
87 }
88
89 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
90 {
91         int rc;
92
93         /* XXX fix the race here (wait_for_event?)*/
94         if (peer == NULL) {
95                 /* Local delivery */
96                 printk("--->> %s %d\n", __FUNCTION__, __LINE__);
97                 rc = mds_queue_req(req); 
98         } else {
99                 /* Remote delivery via portals. */
100                 req->rq_req_portal = MDS_REQUEST_PORTAL;
101                 req->rq_reply_portal = MDS_REPLY_PORTAL;
102                 rc = ptl_send_rpc(req, peer);
103         }
104         if (rc) { 
105                 printk(__FUNCTION__ ": error %d, opcode %d\n", rc, 
106                        req->rq_reqhdr->opc); 
107                 return -rc;
108         }
109
110         init_waitqueue_head(&req->rq_wait_for_rep);
111         printk("-- sleeping\n");
112         interruptible_sleep_on(&req->rq_wait_for_rep);
113         printk("-- done\n");
114
115         rc = mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr, 
116                             &req->rq_rep.mds);
117         if (rc) {
118                 printk(__FUNCTION__ ": mds_unpack_rep failed: %d\n", rc);
119                 return rc;
120         }
121
122         if ( req->rq_rephdr->status == 0 )
123                 printk("-->mdc_queue_wait: buf %p len %d status %d\n", 
124                        req->rq_repbuf, req->rq_replen, 
125                        req->rq_rephdr->status); 
126
127         EXIT;
128         return 0;
129 }
130
131 void mdc_free_req(struct ptlrpc_request *request)
132 {
133         OBD_FREE(request, sizeof(*request));
134 }
135
136 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid, 
137                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
138 {
139         struct ptlrpc_request *request;
140         int rc; 
141
142         request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL); 
143         if (!request) { 
144                 printk("llight request: cannot pack\n");
145                 return -ENOMEM;
146         }
147
148         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
149
150         request->rq_req.mds->valid = valid;
151         request->rq_replen = 
152                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
153
154         rc = mds_queue_wait(request, peer);
155         if (rc) { 
156                 printk("llight request: error in handling %d\n", rc); 
157                 goto out;
158         }
159
160         printk("mds_getattr: mode: %o\n", request->rq_rep.mds->mode); 
161
162         if (rep) { 
163                 *rep = request->rq_rep.mds;
164         }
165         if (hdr) { 
166                 *hdr = request->rq_rephdr;
167         }
168
169  out: 
170         mdc_free_req(request);
171         return rc;
172 }
173
174 int mdc_readpage(struct lustre_peer *peer, ino_t ino, int type, __u64 offset,
175                  char *addr, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
176 {
177         struct ptlrpc_request *request;
178         struct niobuf niobuf;
179         int rc; 
180
181         niobuf.addr = (__u64) (long) addr;
182
183         printk("mdc_readpage: inode: %ld\n", ino); 
184
185         request = mds_prep_req(MDS_READPAGE, 0, NULL,
186                                sizeof(struct niobuf), (char *)&niobuf);
187         if (!request) { 
188                 printk("mdc request: cannot pack\n");
189                 return -ENOMEM;
190         }
191
192         request->rq_req.mds->fid1.id = ino;
193         request->rq_req.mds->fid1.f_type = type;
194         request->rq_req.mds->size = offset;
195         request->rq_req.mds->tgtlen = sizeof(niobuf); 
196
197         request->rq_bulk_portal = MDS_BULK_PORTAL;
198         request->rq_replen = 
199                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
200
201         rc = mds_queue_wait(request, peer);
202         if (rc) { 
203                 printk("mdc request: error in handling %d\n", rc); 
204                 goto out;
205         }
206
207         printk("mdc_readpage: mode: %o\n", request->rq_rep.mds->mode); 
208
209         if (rep) { 
210                 *rep = request->rq_rep.mds;
211         }
212         if (hdr) { 
213                 *hdr = request->rq_rephdr;
214         }
215
216  out: 
217         mdc_free_req(request);
218         return rc;
219 }
220
221 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
222 {
223         int rc; 
224
225         rc = mds_queue_wait(request, peer);
226         if (rc) { 
227                 printk("mdc request: error in handling %d\n", rc); 
228         }
229
230         return rc;
231 }
232
233
234 static int request_ioctl(struct inode *inode, struct file *file, 
235                          unsigned int cmd, unsigned long arg)
236 {
237         int err;
238         struct lustre_peer peer, *peer_ptr = NULL;
239
240         ENTRY;
241
242         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
243                 EXIT;
244                 return -EINVAL;
245         }
246
247         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
248              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
249              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
250                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
251                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
252                 EXIT;
253                 return -EINVAL;
254         }
255
256         err = kportal_uuid_to_peer("mds", &peer);
257         if (err == 0)
258                 peer_ptr = &peer;
259         
260         switch (cmd) {
261         case IOC_REQUEST_GETATTR: { 
262                 struct ptlrep_hdr *hdr = NULL;
263                 printk("-- getting attr for ino 2\n"); 
264                 err = mdc_getattr(peer_ptr, 2, S_IFDIR, ~0, NULL, &hdr);
265                 if (hdr) {
266                         /* FIXME: there must be a better way to get the size */
267                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
268                                  sizeof(struct mds_rep));
269                 }
270                 printk("-- done err %d\n", err);
271                 break;
272         }
273
274         case IOC_REQUEST_READPAGE: { 
275                 struct ptlrep_hdr *hdr = NULL;
276                 char *buf;
277                 OBD_ALLOC(buf, PAGE_SIZE);
278                 if (!buf) { 
279                         err = -ENOMEM;
280                         break;
281                 }
282                 printk("-- readpage 0 for ino 2\n"); 
283                 err = mdc_readpage(peer_ptr, 2, S_IFDIR, 0, buf, NULL, &hdr);
284                 printk("-- done err %d\n", err);
285                 if (!err) { 
286                         printk("-- status: %d\n", hdr->status); 
287                         err = hdr->status;
288                         if (hdr)
289                                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
290                                          sizeof(struct mds_rep));
291                 }
292                 OBD_FREE(buf, PAGE_SIZE);
293                 break;
294         }
295
296         case IOC_REQUEST_SETATTR: { 
297                 struct inode inode;
298                 struct ptlrep_hdr *hdr;
299                 struct iattr iattr; 
300
301                 inode.i_ino = 2;
302                 iattr.ia_mode = 040777;
303                 iattr.ia_atime = 0;
304                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
305
306                 err = mdc_setattr(peer_ptr, &inode, &iattr, NULL, &hdr);
307                 printk("-- done err %d\n", err);
308                 if (!err) { 
309                         printk("-- status: %d\n", hdr->status); 
310                         err = hdr->status;
311                 } else {
312                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
313                                  sizeof(struct mds_rep));
314                 }
315                 break;
316         }
317
318         case IOC_REQUEST_CREATE: { 
319                 struct inode inode;
320                 struct ptlrep_hdr *hdr;
321                 struct iattr iattr; 
322
323                 inode.i_ino = 2;
324                 iattr.ia_mode = 040777;
325                 iattr.ia_atime = 0;
326                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
327
328                 err = mdc_create(peer_ptr, &inode, 
329                                  "foofile", strlen("foofile"), 
330                                  NULL, 0, 0100707, 47114711, 
331                                  11, 47, 0, NULL, &hdr);
332                 printk("-- done err %d\n", err);
333                 if (!err) { 
334                         printk("-- status: %d\n", hdr->status); 
335                         err = hdr->status;
336                 }
337                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
338                          sizeof(struct mds_rep));
339                 break;
340         }
341
342         default:                
343                 err = -EINVAL;
344                 EXIT;
345                 break;
346         }
347         EXIT;
348         return err;
349 }
350
351
352 static struct file_operations requestdev_fops = {
353         ioctl: request_ioctl,
354 };
355
356
357 static struct miscdevice request_dev = {
358         REQUEST_MINOR,
359         "request",
360         &requestdev_fops
361 };
362
363
364 static int __init ptlrpc_request_init(void)
365 {
366         misc_register(&request_dev);
367         return 0 ;
368 }
369
370
371 static void __exit ptlrpc_request_exit(void)
372 {
373         misc_deregister(&request_dev);
374 }
375
376 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
377 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
378 MODULE_LICENSE("GPL");
379
380 EXPORT_SYMBOL(mdc_create); 
381 EXPORT_SYMBOL(mdc_unlink); 
382 EXPORT_SYMBOL(mdc_rename); 
383 EXPORT_SYMBOL(mdc_link); 
384 EXPORT_SYMBOL(mdc_getattr); 
385 EXPORT_SYMBOL(mdc_readpage); 
386 EXPORT_SYMBOL(mdc_setattr); 
387
388 module_init(ptlrpc_request_init);
389 module_exit(ptlrpc_request_exit);