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 Lustre, http://www.sf.net/projects/lustre/
8 * Lustre 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 * Lustre 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 Lustre; 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/obd_class.h>
49 #include <linux/lustre_lib.h>
50 #include <linux/lustre_idl.h>
51 #include <linux/lustre_mds.h>
53 #define REQUEST_MINOR 244
55 extern int mds_queue_req(struct ptlrpc_request *);
57 int mdc_getattr(struct ptlrpc_client *cl, ino_t ino, int type, int valid,
58 struct ptlrpc_request **request)
60 struct ptlrpc_request *req;
61 struct mds_body *body;
62 int rc, size = sizeof(*body);
65 req = ptlrpc_prep_req(cl, MDS_GETATTR, 1, &size, NULL);
67 GOTO(out, rc = -ENOMEM);
69 body = lustre_msg_buf(req->rq_reqmsg, 0);
70 ll_ino2fid(&body->fid1, ino, 0, type);
73 req->rq_replen = lustre_msg_size(1, &size);
75 rc = ptlrpc_queue_wait(cl, req);
76 rc = ptlrpc_check_status(req, rc);
80 body = lustre_msg_buf(req->rq_repmsg, 0);
81 CDEBUG(D_NET, "mode: %o\n", body->mode);
90 int mdc_open(struct ptlrpc_client *cl, ino_t ino, int type, int flags,
91 __u64 *fh, struct ptlrpc_request **request)
93 struct mds_body *body;
94 int rc, size = sizeof(*body);
95 struct ptlrpc_request *req;
97 req = ptlrpc_prep_req(cl, MDS_OPEN, 1, &size, NULL);
99 GOTO(out, rc = -ENOMEM);
101 body = lustre_msg_buf(req->rq_reqmsg, 0);
102 ll_ino2fid(&body->fid1, ino, 0, type);
103 body->flags = HTON__u32(flags);
105 req->rq_replen = lustre_msg_size(1, &size);
107 rc = ptlrpc_queue_wait(cl, req);
108 rc = ptlrpc_check_status(req, rc);
111 mds_unpack_body(req);
112 body = lustre_msg_buf(req->rq_repmsg, 0);
122 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh,
123 struct ptlrpc_request **request)
125 struct mds_body *body;
126 int rc, size = sizeof(*body);
127 struct ptlrpc_request *req;
129 req = ptlrpc_prep_req(cl, MDS_CLOSE, 1, &size, NULL);
131 GOTO(out, rc = -ENOMEM);
133 body = lustre_msg_buf(req->rq_reqmsg, 0);
134 ll_ino2fid(&body->fid1, ino, 0, type);
137 req->rq_replen = lustre_msg_size(1, &size);
139 rc = ptlrpc_queue_wait(cl, req);
140 rc = ptlrpc_check_status(req, rc);
148 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
149 char *addr, struct ptlrpc_request **request)
151 struct ptlrpc_request *req = NULL;
152 struct ptlrpc_bulk_desc *bulk = NULL;
153 struct niobuf niobuf;
154 struct mds_body *body;
155 int rc, size[2] = {sizeof(*body), sizeof(struct niobuf)};
156 char *bufs[2] = {NULL, (char *)&niobuf};
158 niobuf.addr = (__u64) (long) addr;
160 CDEBUG(D_INODE, "inode: %ld\n", ino);
162 bulk = ptlrpc_prep_bulk(&cl->cli_server);
164 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
169 req = ptlrpc_prep_req(cl, MDS_READPAGE, 2, size, bufs);
171 GOTO(out, rc = -ENOMEM);
173 bulk->b_buflen = PAGE_SIZE;
174 bulk->b_buf = (void *)(long)niobuf.addr;
175 bulk->b_portal = MDS_BULK_PORTAL;
176 bulk->b_xid = req->rq_xid;
178 rc = ptlrpc_register_bulk(bulk);
180 CERROR("couldn't setup bulk sink: error %d.\n", rc);
184 body = lustre_msg_buf(req->rq_reqmsg, 0);
186 body->fid1.f_type = type;
189 req->rq_replen = lustre_msg_size(1, size);
191 rc = ptlrpc_queue_wait(cl, req);
193 CERROR("error in handling %d\n", rc);
194 ptlrpc_abort_bulk(bulk);
198 mds_unpack_body(req);
204 OBD_FREE(bulk, sizeof(*bulk));
208 static int request_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, unsigned long arg)
212 struct ptlrpc_client cl;
213 struct ptlrpc_request *request;
217 if (MINOR(inode->i_rdev) != REQUEST_MINOR)
220 if (_IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
221 _IOC_NR(cmd) < IOC_REQUEST_MIN_NR ||
222 _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
223 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
224 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
228 err = ptlrpc_connect_client(-1, "mds",
229 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
231 CERROR("cannot create client\n");
236 case IOC_REQUEST_GETATTR: {
237 CERROR("-- getting attr for ino %lu\n", arg);
238 err = mdc_getattr(&cl, arg, S_IFDIR, ~0, &request);
239 CERROR("-- done err %d\n", err);
244 case IOC_REQUEST_READPAGE: {
246 OBD_ALLOC(buf, PAGE_SIZE);
251 CERROR("-- readpage 0 for ino %lu\n", arg);
252 err = mdc_readpage(&cl, arg, S_IFDIR, 0, buf, &request);
253 CERROR("-- done err %d\n", err);
254 OBD_FREE(buf, PAGE_SIZE);
259 case IOC_REQUEST_SETATTR: {
264 inode.i_generation = 0;
265 iattr.ia_mode = 040777;
267 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
269 err = mdc_setattr(&cl, &inode, &iattr, &request);
270 CERROR("-- done err %d\n", err);
275 case IOC_REQUEST_CREATE: {
280 inode.i_generation = 0;
281 iattr.ia_mode = 040777;
283 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
285 err = mdc_create(&cl, &inode,
286 "foofile", strlen("foofile"),
287 NULL, 0, 0100707, 47114711,
288 11, 47, 0, &request);
289 CERROR("-- done err %d\n", err);
294 case IOC_REQUEST_OPEN: {
296 copy_from_user(&ino, (__u64 *)arg, sizeof(ino));
297 CERROR("-- opening ino %llu\n", ino);
298 err = mdc_open(&cl, ino, S_IFDIR, O_RDONLY, &fh, &request);
299 copy_to_user((__u64 *)arg, &fh, sizeof(fh));
300 CERROR("-- done err %d (fh=%Lu)\n", err, fh);
305 case IOC_REQUEST_CLOSE: {
306 CERROR("-- closing ino 2, filehandle %lu\n", arg);
307 err = mdc_close(&cl, 2, S_IFDIR, arg, &request);
308 CERROR("-- done err %d\n", err);
318 ptlrpc_free_req(request);
324 static struct file_operations requestdev_fops = {
325 ioctl: request_ioctl,
328 static struct miscdevice request_dev = {
334 static int __init ptlrpc_request_init(void)
336 misc_register(&request_dev);
340 static void __exit ptlrpc_request_exit(void)
342 misc_deregister(&request_dev);
345 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
346 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
347 MODULE_LICENSE("GPL");
349 EXPORT_SYMBOL(mdc_create);
350 EXPORT_SYMBOL(mdc_unlink);
351 EXPORT_SYMBOL(mdc_rename);
352 EXPORT_SYMBOL(mdc_link);
353 EXPORT_SYMBOL(mdc_getattr);
354 EXPORT_SYMBOL(mdc_readpage);
355 EXPORT_SYMBOL(mdc_setattr);
356 EXPORT_SYMBOL(mdc_close);
357 EXPORT_SYMBOL(mdc_open);
359 module_init(ptlrpc_request_init);
360 module_exit(ptlrpc_request_exit);