Whamcloud - gitweb
4ca7ad41ef52cd7d781e345cb810971672cbe2fd
[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 Lustre, http://www.sf.net/projects/lustre/
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24
25 #include <linux/module.h>
26 #include <linux/miscdevice.h>
27
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #include <linux/lustre_mds.h>
31
32 #define REQUEST_MINOR 244
33
34 extern int mds_queue_req(struct ptlrpc_request *);
35
36 int mdc_connect(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
37                 ino_t ino, int type, int valid, struct ptlrpc_request **request)
38 {
39         struct ptlrpc_request *req;
40         struct mds_body *body;
41         int rc, size = sizeof(*body);
42         ENTRY;
43
44         req = ptlrpc_prep_req(cl, conn, MDS_CONNECT, 1, &size, NULL);
45         if (!req)
46                 GOTO(out, rc = -ENOMEM);
47
48         body = lustre_msg_buf(req->rq_reqmsg, 0);
49         ll_ino2fid(&body->fid1, ino, 0, type);
50         body->valid = valid;
51
52         req->rq_replen = lustre_msg_size(1, &size);
53
54         rc = ptlrpc_queue_wait(req);
55         rc = ptlrpc_check_status(req, rc);
56
57         if (!rc) {
58                 mds_unpack_body(req);
59                 body = lustre_msg_buf(req->rq_repmsg, 0);
60                 CDEBUG(D_NET, "mode: %o\n", body->mode);
61         }
62
63         EXIT;
64  out:
65         *request = req;
66         return rc;
67 }
68
69
70 int mdc_getattr(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
71                 ino_t ino, int type, int valid, struct ptlrpc_request **request)
72 {
73         struct ptlrpc_request *req;
74         struct mds_body *body;
75         int rc, size = sizeof(*body);
76         ENTRY;
77
78         req = ptlrpc_prep_req(cl, conn, MDS_GETATTR, 1, &size, NULL);
79         if (!req)
80                 GOTO(out, rc = -ENOMEM);
81
82         body = lustre_msg_buf(req->rq_reqmsg, 0);
83         ll_ino2fid(&body->fid1, ino, 0, type);
84         body->valid = valid;
85
86         req->rq_replen = lustre_msg_size(1, &size);
87
88         rc = ptlrpc_queue_wait(req);
89         rc = ptlrpc_check_status(req, rc);
90
91         if (!rc) {
92                 mds_unpack_body(req);
93                 body = lustre_msg_buf(req->rq_repmsg, 0);
94                 CDEBUG(D_NET, "mode: %o\n", body->mode);
95         }
96
97         EXIT;
98  out:
99         *request = req;
100         return rc;
101 }
102
103 int mdc_open(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
104              ino_t ino, int type, int flags, __u64 *fh,
105              struct ptlrpc_request **request)
106 {
107         struct mds_body *body;
108         int rc, size = sizeof(*body);
109         struct ptlrpc_request *req;
110
111         req = ptlrpc_prep_req(cl, conn, MDS_OPEN, 1, &size, NULL);
112         if (!req)
113                 GOTO(out, rc = -ENOMEM);
114
115         body = lustre_msg_buf(req->rq_reqmsg, 0);
116         ll_ino2fid(&body->fid1, ino, 0, type);
117         body->flags = HTON__u32(flags);
118
119         req->rq_replen = lustre_msg_size(1, &size);
120
121         rc = ptlrpc_queue_wait(req);
122         rc = ptlrpc_check_status(req, rc);
123
124         if (!rc) {
125                 mds_unpack_body(req);
126                 body = lustre_msg_buf(req->rq_repmsg, 0);
127                 *fh = body->objid;
128         }
129
130         EXIT;
131  out:
132         *request = req;
133         return rc;
134 }
135
136 int mdc_close(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
137               ino_t ino, int type, __u64 fh, struct ptlrpc_request **request)
138 {
139         struct mds_body *body;
140         int rc, size = sizeof(*body);
141         struct ptlrpc_request *req;
142
143         req = ptlrpc_prep_req(cl, conn, MDS_CLOSE, 1, &size, NULL);
144         if (!req)
145                 GOTO(out, rc = -ENOMEM);
146
147         body = lustre_msg_buf(req->rq_reqmsg, 0);
148         ll_ino2fid(&body->fid1, ino, 0, type);
149         body->objid = fh;
150
151         req->rq_replen = lustre_msg_size(1, &size);
152
153         rc = ptlrpc_queue_wait(req);
154         rc = ptlrpc_check_status(req, rc);
155
156         EXIT;
157  out:
158         *request = req;
159         return rc;
160 }
161
162 int mdc_readpage(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
163                  ino_t ino, int type, __u64 offset, char *addr,
164                  struct ptlrpc_request **request)
165 {
166         struct ptlrpc_request *req = NULL;
167         struct ptlrpc_bulk_desc *bulk = NULL;
168         struct niobuf niobuf;
169         struct mds_body *body;
170         int rc, size[2] = {sizeof(*body), sizeof(struct niobuf)};
171         char *bufs[2] = {NULL, (char *)&niobuf};
172
173         niobuf.addr = (__u64) (long) addr;
174
175         CDEBUG(D_INODE, "inode: %ld\n", ino);
176
177         bulk = ptlrpc_prep_bulk(conn);
178         if (bulk == NULL) {
179                 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
180                 rc = -ENOMEM;
181                 goto out;
182         }
183
184         req = ptlrpc_prep_req(cl, conn, MDS_READPAGE, 2, size, bufs);
185         if (!req)
186                 GOTO(out, rc = -ENOMEM);
187
188         bulk->b_buflen = PAGE_SIZE;
189         bulk->b_buf = (void *)(long)niobuf.addr;
190         bulk->b_portal = MDS_BULK_PORTAL;
191         bulk->b_xid = req->rq_reqmsg->xid;
192
193         rc = ptlrpc_register_bulk(bulk);
194         if (rc) {
195                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
196                 GOTO(out, rc);
197         }
198
199         body = lustre_msg_buf(req->rq_reqmsg, 0);
200         body->fid1.id = ino;
201         body->fid1.f_type = type;
202         body->size = offset;
203
204         req->rq_replen = lustre_msg_size(1, size);
205
206         rc = ptlrpc_queue_wait(req);
207         if (rc) {
208                 CERROR("error in handling %d\n", rc);
209                 ptlrpc_abort_bulk(bulk);
210                 GOTO(out, rc);
211         }
212
213         mds_unpack_body(req);
214         EXIT;
215
216  out:
217         *request = req;
218         if (bulk != NULL)
219                 OBD_FREE(bulk, sizeof(*bulk));
220         return rc;
221 }
222
223 static int request_ioctl(struct inode *inode, struct file *file,
224                          unsigned int cmd, unsigned long arg)
225 {
226         int err;
227         struct ptlrpc_client cl;
228         struct ptlrpc_connection *conn;
229         struct ptlrpc_request *request;
230
231         ENTRY;
232
233         if (MINOR(inode->i_rdev) != REQUEST_MINOR)
234                 RETURN(-EINVAL);
235
236         if (_IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
237             _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  ||
238             _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
239                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
240                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
241                 RETURN(-EINVAL);
242         }
243
244         ptlrpc_init_client(NULL, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
245         conn = ptlrpc_uuid_to_connection("mds");
246         if (err) {
247                 CERROR("cannot create client\n");
248                 RETURN(-EINVAL);
249         }
250
251         switch (cmd) {
252         case IOC_REQUEST_GETATTR: {
253                 CERROR("-- getting attr for ino %lu\n", arg);
254                 err = mdc_getattr(&cl, conn, arg, S_IFDIR, ~0, &request);
255                 CERROR("-- done err %d\n", err);
256
257                 GOTO(out, err);
258         }
259
260         case IOC_REQUEST_READPAGE: {
261                 char *buf;
262                 OBD_ALLOC(buf, PAGE_SIZE);
263                 if (!buf) {
264                         err = -ENOMEM;
265                         break;
266                 }
267                 CERROR("-- readpage 0 for ino %lu\n", arg);
268                 err = mdc_readpage(&cl, conn, arg, S_IFDIR, 0, buf, &request);
269                 CERROR("-- done err %d\n", err);
270                 OBD_FREE(buf, PAGE_SIZE);
271
272                 GOTO(out, err);
273         }
274
275         case IOC_REQUEST_SETATTR: {
276                 struct inode inode;
277                 struct iattr iattr;
278
279                 inode.i_ino = arg;
280                 inode.i_generation = 0;
281                 iattr.ia_mode = 040777;
282                 iattr.ia_atime = 0;
283                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
284
285                 err = mdc_setattr(&cl, conn, &inode, &iattr, &request);
286                 CERROR("-- done err %d\n", err);
287
288                 GOTO(out, err);
289         }
290
291         case IOC_REQUEST_CREATE: {
292                 struct inode inode;
293                 struct iattr iattr;
294
295                 inode.i_ino = arg;
296                 inode.i_generation = 0;
297                 iattr.ia_mode = 040777;
298                 iattr.ia_atime = 0;
299                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
300
301                 err = mdc_create(&cl, conn, &inode,
302                                  "foofile", strlen("foofile"),
303                                  NULL, 0, 0100707, 47114711,
304                                  11, 47, 0, &request);
305                 CERROR("-- done err %d\n", err);
306
307                 GOTO(out, err);
308         }
309
310         case IOC_REQUEST_OPEN: {
311                 __u64 fh, ino;
312                 copy_from_user(&ino, (__u64 *)arg, sizeof(ino));
313                 CERROR("-- opening ino %llu\n", ino);
314                 err = mdc_open(&cl, conn, ino, S_IFDIR, O_RDONLY, &fh,
315                                &request);
316                 copy_to_user((__u64 *)arg, &fh, sizeof(fh));
317                 CERROR("-- done err %d (fh=%Lu)\n", err, fh);
318
319                 GOTO(out, err);
320         }
321
322         case IOC_REQUEST_CLOSE: {
323                 CERROR("-- closing ino 2, filehandle %lu\n", arg);
324                 err = mdc_close(&cl, conn, 2, S_IFDIR, arg, &request);
325                 CERROR("-- done err %d\n", err);
326
327                 GOTO(out, err);
328         }
329
330         default:
331                 RETURN(-EINVAL);
332         }
333
334  out:
335         ptlrpc_free_req(request);
336         ptlrpc_put_connection(conn);
337
338         RETURN(err);
339 }
340
341
342 static struct file_operations requestdev_fops = {
343         ioctl: request_ioctl,
344 };
345
346 static struct miscdevice request_dev = {
347         REQUEST_MINOR,
348         "request",
349         &requestdev_fops
350 };
351
352 static int __init ptlrpc_request_init(void)
353 {
354         misc_register(&request_dev);
355         return 0;
356 }
357
358 static void __exit ptlrpc_request_exit(void)
359 {
360         misc_deregister(&request_dev);
361 }
362
363 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
364 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
365 MODULE_LICENSE("GPL");
366
367 EXPORT_SYMBOL(mdc_create);
368 EXPORT_SYMBOL(mdc_unlink);
369 EXPORT_SYMBOL(mdc_rename);
370 EXPORT_SYMBOL(mdc_link);
371 EXPORT_SYMBOL(mdc_getattr);
372 EXPORT_SYMBOL(mdc_readpage);
373 EXPORT_SYMBOL(mdc_setattr);
374 EXPORT_SYMBOL(mdc_close);
375 EXPORT_SYMBOL(mdc_open);
376
377 module_init(ptlrpc_request_init);
378 module_exit(ptlrpc_request_exit);