Whamcloud - gitweb
Removes all traces of mds_req, mds_rep, ost_req, and ost_rep. All subsystems
[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/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.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>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
38
39 #include <linux/fs.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/miscdevice.h>
44
45 #define DEBUG_SUBSYSTEM S_MDC
46
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>
52
53 #define REQUEST_MINOR 244
54
55 extern int mds_queue_req(struct ptlrpc_request *);
56
57 int mdc_getattr(struct ptlrpc_client *cl, ino_t ino, int type, int valid,
58                 struct ptlrpc_request **request)
59 {
60         struct ptlrpc_request *req;
61         struct mds_body *body;
62         int rc, size = sizeof(*body);
63         ENTRY;
64
65         req = ptlrpc_prep_req(cl, MDS_GETATTR, 1, &size, NULL);
66         if (!req)
67                 GOTO(out, rc = -ENOMEM);
68
69         body = lustre_msg_buf(req->rq_reqmsg, 0);
70         ll_ino2fid(&body->fid1, ino, 0, type);
71         body->valid = valid;
72
73         req->rq_replen = lustre_msg_size(1, &size);
74
75         rc = ptlrpc_queue_wait(cl, req);
76         rc = ptlrpc_check_status(req, rc);
77
78         if (!rc) {
79                 mds_unpack_body(req);
80                 body = lustre_msg_buf(req->rq_repmsg, 0);
81                 CDEBUG(D_NET, "mode: %o\n", body->mode);
82         }
83
84         EXIT;
85  out:
86         *request = req;
87         return rc;
88 }
89
90 int mdc_open(struct ptlrpc_client *cl, ino_t ino, int type, int flags,
91              __u64 *fh, struct ptlrpc_request **request)
92 {
93         struct mds_body *body;
94         int rc, size = sizeof(*body);
95         struct ptlrpc_request *req;
96
97         req = ptlrpc_prep_req(cl, MDS_OPEN, 1, &size, NULL);
98         if (!req)
99                 GOTO(out, rc = -ENOMEM);
100
101         body = lustre_msg_buf(req->rq_reqmsg, 0);
102         ll_ino2fid(&body->fid1, ino, 0, type);
103         body->flags = HTON__u32(flags);
104
105         req->rq_replen = lustre_msg_size(1, &size);
106
107         rc = ptlrpc_queue_wait(cl, req);
108         rc = ptlrpc_check_status(req, rc);
109
110         if (!rc) {
111                 mds_unpack_body(req);
112                 body = lustre_msg_buf(req->rq_repmsg, 0);
113                 *fh = body->objid;
114         }
115
116         EXIT;
117  out:
118         *request = req;
119         return rc;
120 }
121
122 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh,
123               struct ptlrpc_request **request)
124 {
125         struct mds_body *body;
126         int rc, size = sizeof(*body);
127         struct ptlrpc_request *req;
128
129         req = ptlrpc_prep_req(cl, MDS_CLOSE, 1, &size, NULL);
130         if (!req)
131                 GOTO(out, rc = -ENOMEM);
132
133         body = lustre_msg_buf(req->rq_reqmsg, 0);
134         ll_ino2fid(&body->fid1, ino, 0, type);
135         body->objid = fh;
136
137         req->rq_replen = lustre_msg_size(1, &size);
138
139         rc = ptlrpc_queue_wait(cl, req);
140         rc = ptlrpc_check_status(req, rc);
141
142         EXIT;
143  out:
144         *request = req;
145         return rc;
146 }
147
148 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
149                  char *addr, struct ptlrpc_request **request)
150 {
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};
157
158         niobuf.addr = (__u64) (long) addr;
159
160         CDEBUG(D_INODE, "inode: %ld\n", ino);
161
162         bulk = ptlrpc_prep_bulk(&cl->cli_server);
163         if (bulk == NULL) {
164                 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
165                 rc = -ENOMEM;
166                 goto out;
167         }
168
169         req = ptlrpc_prep_req(cl, MDS_READPAGE, 2, size, bufs);
170         if (!req)
171                 GOTO(out, rc = -ENOMEM);
172
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;
177
178         rc = ptlrpc_register_bulk(bulk);
179         if (rc) {
180                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
181                 GOTO(out, rc);
182         }
183
184         body = lustre_msg_buf(req->rq_reqmsg, 0);
185         body->fid1.id = ino;
186         body->fid1.f_type = type;
187         body->size = offset;
188
189         req->rq_replen = lustre_msg_size(1, size);
190
191         rc = ptlrpc_queue_wait(cl, req);
192         if (rc) {
193                 CERROR("error in handling %d\n", rc);
194                 ptlrpc_abort_bulk(bulk);
195                 GOTO(out, rc);
196         }
197
198         mds_unpack_body(req);
199         EXIT;
200
201  out:
202         *request = req;
203         if (bulk != NULL)
204                 OBD_FREE(bulk, sizeof(*bulk));
205         return rc;
206 }
207
208 static int request_ioctl(struct inode *inode, struct file *file,
209                          unsigned int cmd, unsigned long arg)
210 {
211         int err;
212         struct ptlrpc_client cl;
213         struct ptlrpc_request *request;
214
215         ENTRY;
216
217         if (MINOR(inode->i_rdev) != REQUEST_MINOR)
218                 RETURN(-EINVAL);
219
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));
225                 RETURN(-EINVAL);
226         }
227
228         err = ptlrpc_connect_client(-1, "mds",
229                                     MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
230         if (err) {
231                 CERROR("cannot create client\n");
232                 RETURN(-EINVAL);
233         }
234
235         switch (cmd) {
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);
240
241                 GOTO(out, err);
242         }
243
244         case IOC_REQUEST_READPAGE: {
245                 char *buf;
246                 OBD_ALLOC(buf, PAGE_SIZE);
247                 if (!buf) {
248                         err = -ENOMEM;
249                         break;
250                 }
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);
255
256                 GOTO(out, err);
257         }
258
259         case IOC_REQUEST_SETATTR: {
260                 struct inode inode;
261                 struct iattr iattr;
262
263                 inode.i_ino = arg;
264                 inode.i_generation = 0;
265                 iattr.ia_mode = 040777;
266                 iattr.ia_atime = 0;
267                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
268
269                 err = mdc_setattr(&cl, &inode, &iattr, &request);
270                 CERROR("-- done err %d\n", err);
271
272                 GOTO(out, err);
273         }
274
275         case IOC_REQUEST_CREATE: {
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_create(&cl, &inode,
286                                  "foofile", strlen("foofile"),
287                                  NULL, 0, 0100707, 47114711,
288                                  11, 47, 0, &request);
289                 CERROR("-- done err %d\n", err);
290
291                 GOTO(out, err);
292         }
293
294         case IOC_REQUEST_OPEN: {
295                 __u64 fh, ino;
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);
301
302                 GOTO(out, err);
303         }
304
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);
309
310                 GOTO(out, err);
311         }
312
313         default:
314                 RETURN(-EINVAL);
315         }
316
317  out:
318         ptlrpc_free_req(request);
319
320         RETURN(err);
321 }
322
323
324 static struct file_operations requestdev_fops = {
325         ioctl: request_ioctl,
326 };
327
328 static struct miscdevice request_dev = {
329         REQUEST_MINOR,
330         "request",
331         &requestdev_fops
332 };
333
334 static int __init ptlrpc_request_init(void)
335 {
336         misc_register(&request_dev);
337         return 0;
338 }
339
340 static void __exit ptlrpc_request_exit(void)
341 {
342         misc_deregister(&request_dev);
343 }
344
345 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
346 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
347 MODULE_LICENSE("GPL");
348
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);
358
359 module_init(ptlrpc_request_init);
360 module_exit(ptlrpc_request_exit);