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_connect(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);
91 int mdc_getattr(struct ptlrpc_client *cl, ino_t ino, int type, int valid,
92 struct ptlrpc_request **request)
94 struct ptlrpc_request *req;
95 struct mds_body *body;
96 int rc, size = sizeof(*body);
99 req = ptlrpc_prep_req(cl, MDS_GETATTR, 1, &size, NULL);
101 GOTO(out, rc = -ENOMEM);
103 body = lustre_msg_buf(req->rq_reqmsg, 0);
104 ll_ino2fid(&body->fid1, ino, 0, type);
107 req->rq_replen = lustre_msg_size(1, &size);
109 rc = ptlrpc_queue_wait(cl, req);
110 rc = ptlrpc_check_status(req, rc);
113 mds_unpack_body(req);
114 body = lustre_msg_buf(req->rq_repmsg, 0);
115 CDEBUG(D_NET, "mode: %o\n", body->mode);
124 int mdc_open(struct ptlrpc_client *cl, ino_t ino, int type, int flags,
125 __u64 *fh, struct ptlrpc_request **request)
127 struct mds_body *body;
128 int rc, size = sizeof(*body);
129 struct ptlrpc_request *req;
131 req = ptlrpc_prep_req(cl, MDS_OPEN, 1, &size, NULL);
133 GOTO(out, rc = -ENOMEM);
135 body = lustre_msg_buf(req->rq_reqmsg, 0);
136 ll_ino2fid(&body->fid1, ino, 0, type);
137 body->flags = HTON__u32(flags);
139 req->rq_replen = lustre_msg_size(1, &size);
141 rc = ptlrpc_queue_wait(cl, req);
142 rc = ptlrpc_check_status(req, rc);
145 mds_unpack_body(req);
146 body = lustre_msg_buf(req->rq_repmsg, 0);
156 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh,
157 struct ptlrpc_request **request)
159 struct mds_body *body;
160 int rc, size = sizeof(*body);
161 struct ptlrpc_request *req;
163 req = ptlrpc_prep_req(cl, MDS_CLOSE, 1, &size, NULL);
165 GOTO(out, rc = -ENOMEM);
167 body = lustre_msg_buf(req->rq_reqmsg, 0);
168 ll_ino2fid(&body->fid1, ino, 0, type);
171 req->rq_replen = lustre_msg_size(1, &size);
173 rc = ptlrpc_queue_wait(cl, req);
174 rc = ptlrpc_check_status(req, rc);
182 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
183 char *addr, struct ptlrpc_request **request)
185 struct ptlrpc_request *req = NULL;
186 struct ptlrpc_bulk_desc *bulk = NULL;
187 struct niobuf niobuf;
188 struct mds_body *body;
189 int rc, size[2] = {sizeof(*body), sizeof(struct niobuf)};
190 char *bufs[2] = {NULL, (char *)&niobuf};
192 niobuf.addr = (__u64) (long) addr;
194 CDEBUG(D_INODE, "inode: %ld\n", ino);
196 bulk = ptlrpc_prep_bulk(&cl->cli_server);
198 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
203 req = ptlrpc_prep_req(cl, MDS_READPAGE, 2, size, bufs);
205 GOTO(out, rc = -ENOMEM);
207 bulk->b_buflen = PAGE_SIZE;
208 bulk->b_buf = (void *)(long)niobuf.addr;
209 bulk->b_portal = MDS_BULK_PORTAL;
210 bulk->b_xid = req->rq_xid;
212 rc = ptlrpc_register_bulk(bulk);
214 CERROR("couldn't setup bulk sink: error %d.\n", rc);
218 body = lustre_msg_buf(req->rq_reqmsg, 0);
220 body->fid1.f_type = type;
223 req->rq_replen = lustre_msg_size(1, size);
225 rc = ptlrpc_queue_wait(cl, req);
227 CERROR("error in handling %d\n", rc);
228 ptlrpc_abort_bulk(bulk);
232 mds_unpack_body(req);
238 OBD_FREE(bulk, sizeof(*bulk));
242 static int request_ioctl(struct inode *inode, struct file *file,
243 unsigned int cmd, unsigned long arg)
246 struct ptlrpc_client cl;
247 struct ptlrpc_request *request;
251 if (MINOR(inode->i_rdev) != REQUEST_MINOR)
254 if (_IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
255 _IOC_NR(cmd) < IOC_REQUEST_MIN_NR ||
256 _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
257 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
258 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
262 ptlrpc_init_client(-1, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
263 err = ptlrpc_connect_client(-1, "mds", &cl);
265 CERROR("cannot create client\n");
270 case IOC_REQUEST_GETATTR: {
271 CERROR("-- getting attr for ino %lu\n", arg);
272 err = mdc_getattr(&cl, arg, S_IFDIR, ~0, &request);
273 CERROR("-- done err %d\n", err);
278 case IOC_REQUEST_READPAGE: {
280 OBD_ALLOC(buf, PAGE_SIZE);
285 CERROR("-- readpage 0 for ino %lu\n", arg);
286 err = mdc_readpage(&cl, arg, S_IFDIR, 0, buf, &request);
287 CERROR("-- done err %d\n", err);
288 OBD_FREE(buf, PAGE_SIZE);
293 case IOC_REQUEST_SETATTR: {
298 inode.i_generation = 0;
299 iattr.ia_mode = 040777;
301 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
303 err = mdc_setattr(&cl, &inode, &iattr, &request);
304 CERROR("-- done err %d\n", err);
309 case IOC_REQUEST_CREATE: {
314 inode.i_generation = 0;
315 iattr.ia_mode = 040777;
317 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
319 err = mdc_create(&cl, &inode,
320 "foofile", strlen("foofile"),
321 NULL, 0, 0100707, 47114711,
322 11, 47, 0, &request);
323 CERROR("-- done err %d\n", err);
328 case IOC_REQUEST_OPEN: {
330 copy_from_user(&ino, (__u64 *)arg, sizeof(ino));
331 CERROR("-- opening ino %llu\n", ino);
332 err = mdc_open(&cl, ino, S_IFDIR, O_RDONLY, &fh, &request);
333 copy_to_user((__u64 *)arg, &fh, sizeof(fh));
334 CERROR("-- done err %d (fh=%Lu)\n", err, fh);
339 case IOC_REQUEST_CLOSE: {
340 CERROR("-- closing ino 2, filehandle %lu\n", arg);
341 err = mdc_close(&cl, 2, S_IFDIR, arg, &request);
342 CERROR("-- done err %d\n", err);
352 ptlrpc_free_req(request);
358 static struct file_operations requestdev_fops = {
359 ioctl: request_ioctl,
362 static struct miscdevice request_dev = {
368 static int __init ptlrpc_request_init(void)
370 misc_register(&request_dev);
374 static void __exit ptlrpc_request_exit(void)
376 misc_deregister(&request_dev);
379 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
380 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
381 MODULE_LICENSE("GPL");
383 EXPORT_SYMBOL(mdc_create);
384 EXPORT_SYMBOL(mdc_unlink);
385 EXPORT_SYMBOL(mdc_rename);
386 EXPORT_SYMBOL(mdc_link);
387 EXPORT_SYMBOL(mdc_getattr);
388 EXPORT_SYMBOL(mdc_readpage);
389 EXPORT_SYMBOL(mdc_setattr);
390 EXPORT_SYMBOL(mdc_close);
391 EXPORT_SYMBOL(mdc_open);
393 module_init(ptlrpc_request_init);
394 module_exit(ptlrpc_request_exit);