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 #define DEBUG_SUBSYSTEM S_MDC
47 #include <linux/obd_support.h>
48 #include <linux/lustre_lib.h>
49 #include <linux/lustre_idl.h>
50 #include <linux/lustre_mds.h>
52 #define REQUEST_MINOR 244
54 extern int mds_queue_req(struct ptlrpc_request *);
56 /* FIXME: this belongs in some sort of service struct */
57 static int mdc_xid = 0;
59 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name,
60 int tgtlen, char *tgt)
62 struct ptlrpc_request *request;
66 OBD_ALLOC(request, sizeof(*request));
68 CERROR("request allocation out of memory\n");
72 memset(request, 0, sizeof(*request));
73 request->rq_xid = mdc_xid++;
75 rc = mds_pack_req(name, namelen, tgt, tgtlen,
76 &request->rq_reqhdr, &(request->rq_req.mds),
77 &request->rq_reqlen, &request->rq_reqbuf);
79 CERROR("llight request: cannot pack request %d\n", rc);
82 CDEBUG(0, "--> len %d, req %p, tgtlen %d\n",
83 request->rq_reqlen, request->rq_req.mds,
84 request->rq_req.mds->tgtlen);
85 request->rq_reqhdr->opc = opcode;
91 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
95 /* XXX fix the race here (wait_for_event?)*/
99 rc = mds_queue_req(req);
101 /* Remote delivery via portals. */
102 req->rq_req_portal = MDS_REQUEST_PORTAL;
103 req->rq_reply_portal = MDS_REPLY_PORTAL;
104 rc = ptl_send_rpc(req, peer);
107 CERROR("error %d, opcode %d\n", rc,
108 req->rq_reqhdr->opc);
112 init_waitqueue_head(&req->rq_wait_for_rep);
113 CDEBUG(0, "-- sleeping\n");
114 interruptible_sleep_on(&req->rq_wait_for_rep);
115 CDEBUG(0, "-- done\n");
117 rc = mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr,
120 CERROR("mds_unpack_rep failed: %d\n", rc);
124 if ( req->rq_rephdr->status == 0 )
125 CDEBUG(0, "--> buf %p len %d status %d\n",
126 req->rq_repbuf, req->rq_replen,
127 req->rq_rephdr->status);
133 void mdc_free_req(struct ptlrpc_request *request)
135 OBD_FREE(request, sizeof(*request));
138 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid,
139 struct mds_rep **rep, struct ptlrep_hdr **hdr)
141 struct ptlrpc_request *request;
144 request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL);
146 CERROR("llight request: cannot pack\n");
150 ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
152 request->rq_req.mds->valid = valid;
154 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
156 rc = mds_queue_wait(request, peer);
158 CERROR("llight request: error in handling %d\n", rc);
162 CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
165 *rep = request->rq_rep.mds;
168 *hdr = request->rq_rephdr;
172 mdc_free_req(request);
176 int mdc_readpage(struct lustre_peer *peer, ino_t ino, int type, __u64 offset,
177 char *addr, struct mds_rep **rep, struct ptlrep_hdr **hdr)
179 struct ptlrpc_request *request;
180 struct niobuf niobuf;
183 niobuf.addr = (__u64) (long) addr;
185 CDEBUG(D_INODE, "inode: %ld\n", ino);
187 request = mds_prep_req(MDS_READPAGE, 0, NULL,
188 sizeof(struct niobuf), (char *)&niobuf);
190 CERROR("mdc request: cannot pack\n");
194 request->rq_req.mds->fid1.id = ino;
195 request->rq_req.mds->fid1.f_type = type;
196 request->rq_req.mds->size = offset;
197 request->rq_req.mds->tgtlen = sizeof(niobuf);
199 request->rq_bulk_portal = MDS_BULK_PORTAL;
201 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
203 rc = mds_queue_wait(request, peer);
205 CERROR("mdc request: error in handling %d\n", rc);
209 CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
212 *rep = request->rq_rep.mds;
215 *hdr = request->rq_rephdr;
219 mdc_free_req(request);
223 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
227 rc = mds_queue_wait(request, peer);
229 CERROR("mdc request: error in handling %d\n", rc);
236 static int request_ioctl(struct inode *inode, struct file *file,
237 unsigned int cmd, unsigned long arg)
240 struct lustre_peer peer, *peer_ptr = NULL;
244 if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
249 if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
250 _IOC_NR(cmd) < IOC_REQUEST_MIN_NR ||
251 _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
252 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
253 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
258 err = kportal_uuid_to_peer("mds", &peer);
263 case IOC_REQUEST_GETATTR: {
264 struct ptlrep_hdr *hdr = NULL;
265 CERROR("-- getting attr for ino 2\n");
266 err = mdc_getattr(peer_ptr, 2, S_IFDIR, ~0, NULL, &hdr);
268 /* FIXME: there must be a better way to get the size */
269 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
270 sizeof(struct mds_rep));
272 CERROR("-- done err %d\n", err);
276 case IOC_REQUEST_READPAGE: {
277 struct ptlrep_hdr *hdr = NULL;
279 OBD_ALLOC(buf, PAGE_SIZE);
284 CERROR("-- readpage 0 for ino 2\n");
285 err = mdc_readpage(peer_ptr, 2, S_IFDIR, 0, buf, NULL, &hdr);
286 CERROR("-- done err %d\n", err);
288 CERROR("-- status: %d\n", hdr->status);
291 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
292 sizeof(struct mds_rep));
294 OBD_FREE(buf, PAGE_SIZE);
298 case IOC_REQUEST_SETATTR: {
300 struct ptlrep_hdr *hdr;
304 iattr.ia_mode = 040777;
306 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
308 err = mdc_setattr(peer_ptr, &inode, &iattr, NULL, &hdr);
309 CERROR("-- done err %d\n", err);
311 CERROR("-- status: %d\n", hdr->status);
314 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
315 sizeof(struct mds_rep));
320 case IOC_REQUEST_CREATE: {
322 struct ptlrep_hdr *hdr;
326 iattr.ia_mode = 040777;
328 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
330 err = mdc_create(peer_ptr, &inode,
331 "foofile", strlen("foofile"),
332 NULL, 0, 0100707, 47114711,
333 11, 47, 0, NULL, &hdr);
334 CERROR("-- done err %d\n", err);
336 CERROR("-- status: %d\n", hdr->status);
339 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
340 sizeof(struct mds_rep));
354 static struct file_operations requestdev_fops = {
355 ioctl: request_ioctl,
359 static struct miscdevice request_dev = {
366 static int __init ptlrpc_request_init(void)
368 misc_register(&request_dev);
373 static void __exit ptlrpc_request_exit(void)
375 misc_deregister(&request_dev);
378 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
379 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
380 MODULE_LICENSE("GPL");
382 EXPORT_SYMBOL(mdc_create);
383 EXPORT_SYMBOL(mdc_unlink);
384 EXPORT_SYMBOL(mdc_rename);
385 EXPORT_SYMBOL(mdc_link);
386 EXPORT_SYMBOL(mdc_getattr);
387 EXPORT_SYMBOL(mdc_readpage);
388 EXPORT_SYMBOL(mdc_setattr);
390 module_init(ptlrpc_request_init);
391 module_exit(ptlrpc_request_exit);