1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
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.
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.
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.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.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>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/miscdevice.h>
45 #include <linux/obd_support.h>
46 #include <linux/lustre_lib.h>
47 #include <linux/lustre_idl.h>
48 #include <linux/lustre_mds.h>
50 #define REQUEST_MINOR 244
52 extern int mds_queue_req(struct ptlrpc_request *);
54 /* FIXME: this belongs in some sort of service struct */
55 static int mdc_xid = 0;
57 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name,
58 int tgtlen, char *tgt)
60 struct ptlrpc_request *request;
64 OBD_ALLOC(request, sizeof(*request));
66 printk("mds_prep_req: request allocation out of memory\n");
70 memset(request, 0, sizeof(*request));
71 request->rq_xid = mdc_xid++;
73 rc = mds_pack_req(name, namelen, tgt, tgtlen,
74 &request->rq_reqhdr, &(request->rq_req.mds),
75 &request->rq_reqlen, &request->rq_reqbuf);
77 printk("llight request: cannot pack request %d\n", rc);
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;
89 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
93 /* XXX fix the race here (wait_for_event?)*/
96 printk("--->> %s %d\n", __FUNCTION__, __LINE__);
97 rc = mds_queue_req(req);
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);
105 printk(__FUNCTION__ ": error %d, opcode %d\n", rc,
106 req->rq_reqhdr->opc);
110 init_waitqueue_head(&req->rq_wait_for_rep);
111 printk("-- sleeping\n");
112 interruptible_sleep_on(&req->rq_wait_for_rep);
115 rc = mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr,
118 printk(__FUNCTION__ ": mds_unpack_rep failed: %d\n", rc);
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);
131 void mdc_free_req(struct ptlrpc_request *request)
133 OBD_FREE(request, sizeof(*request));
136 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid,
137 struct mds_rep **rep, struct ptlrep_hdr **hdr)
139 struct ptlrpc_request *request;
142 request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL);
144 printk("llight request: cannot pack\n");
148 ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
150 request->rq_req.mds->valid = valid;
152 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
154 rc = mds_queue_wait(request, peer);
156 printk("llight request: error in handling %d\n", rc);
160 printk("mds_getattr: mode: %o\n", request->rq_rep.mds->mode);
163 *rep = request->rq_rep.mds;
166 *hdr = request->rq_rephdr;
170 mdc_free_req(request);
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)
177 struct ptlrpc_request *request;
178 struct niobuf niobuf;
181 niobuf.addr = (__u64) (long) addr;
183 printk("mdc_readpage: inode: %ld\n", ino);
185 request = mds_prep_req(MDS_READPAGE, 0, NULL,
186 sizeof(struct niobuf), (char *)&niobuf);
188 printk("mdc request: cannot pack\n");
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);
197 request->rq_bulk_portal = MDS_BULK_PORTAL;
199 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
201 rc = mds_queue_wait(request, peer);
203 printk("mdc request: error in handling %d\n", rc);
207 printk("mdc_readpage: mode: %o\n", request->rq_rep.mds->mode);
210 *rep = request->rq_rep.mds;
213 *hdr = request->rq_rephdr;
217 mdc_free_req(request);
221 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
225 rc = mds_queue_wait(request, peer);
227 printk("mdc request: error in handling %d\n", rc);
234 static int request_ioctl(struct inode *inode, struct file *file,
235 unsigned int cmd, unsigned long arg)
238 struct lustre_peer peer, *peer_ptr = NULL;
242 if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
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));
256 err = kportal_uuid_to_peer("mds", &peer);
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);
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));
270 printk("-- done err %d\n", err);
274 case IOC_REQUEST_READPAGE: {
275 struct ptlrep_hdr *hdr = NULL;
277 OBD_ALLOC(buf, PAGE_SIZE);
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);
286 printk("-- status: %d\n", hdr->status);
289 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
290 sizeof(struct mds_rep));
292 OBD_FREE(buf, PAGE_SIZE);
296 case IOC_REQUEST_SETATTR: {
298 struct ptlrep_hdr *hdr;
302 iattr.ia_mode = 040777;
304 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
306 err = mdc_setattr(peer_ptr, &inode, &iattr, NULL, &hdr);
307 printk("-- done err %d\n", err);
309 printk("-- status: %d\n", hdr->status);
312 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
313 sizeof(struct mds_rep));
318 case IOC_REQUEST_CREATE: {
320 struct ptlrep_hdr *hdr;
324 iattr.ia_mode = 040777;
326 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
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);
334 printk("-- status: %d\n", hdr->status);
337 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
338 sizeof(struct mds_rep));
352 static struct file_operations requestdev_fops = {
353 ioctl: request_ioctl,
357 static struct miscdevice request_dev = {
364 static int __init ptlrpc_request_init(void)
366 misc_register(&request_dev);
371 static void __exit ptlrpc_request_exit(void)
373 misc_deregister(&request_dev);
376 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
377 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
378 MODULE_LICENSE("GPL");
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);
388 module_init(ptlrpc_request_init);
389 module_exit(ptlrpc_request_exit);