Whamcloud - gitweb
- name switch: "light" --> "lite"
[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_connect(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
91 int mdc_getattr(struct ptlrpc_client *cl, ino_t ino, int type, int valid,
92                 struct ptlrpc_request **request)
93 {
94         struct ptlrpc_request *req;
95         struct mds_body *body;
96         int rc, size = sizeof(*body);
97         ENTRY;
98
99         req = ptlrpc_prep_req(cl, MDS_GETATTR, 1, &size, NULL);
100         if (!req)
101                 GOTO(out, rc = -ENOMEM);
102
103         body = lustre_msg_buf(req->rq_reqmsg, 0);
104         ll_ino2fid(&body->fid1, ino, 0, type);
105         body->valid = valid;
106
107         req->rq_replen = lustre_msg_size(1, &size);
108
109         rc = ptlrpc_queue_wait(cl, req);
110         rc = ptlrpc_check_status(req, rc);
111
112         if (!rc) {
113                 mds_unpack_body(req);
114                 body = lustre_msg_buf(req->rq_repmsg, 0);
115                 CDEBUG(D_NET, "mode: %o\n", body->mode);
116         }
117
118         EXIT;
119  out:
120         *request = req;
121         return rc;
122 }
123
124 int mdc_open(struct ptlrpc_client *cl, ino_t ino, int type, int flags,
125              __u64 *fh, struct ptlrpc_request **request)
126 {
127         struct mds_body *body;
128         int rc, size = sizeof(*body);
129         struct ptlrpc_request *req;
130
131         req = ptlrpc_prep_req(cl, MDS_OPEN, 1, &size, NULL);
132         if (!req)
133                 GOTO(out, rc = -ENOMEM);
134
135         body = lustre_msg_buf(req->rq_reqmsg, 0);
136         ll_ino2fid(&body->fid1, ino, 0, type);
137         body->flags = HTON__u32(flags);
138
139         req->rq_replen = lustre_msg_size(1, &size);
140
141         rc = ptlrpc_queue_wait(cl, req);
142         rc = ptlrpc_check_status(req, rc);
143
144         if (!rc) {
145                 mds_unpack_body(req);
146                 body = lustre_msg_buf(req->rq_repmsg, 0);
147                 *fh = body->objid;
148         }
149
150         EXIT;
151  out:
152         *request = req;
153         return rc;
154 }
155
156 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh,
157               struct ptlrpc_request **request)
158 {
159         struct mds_body *body;
160         int rc, size = sizeof(*body);
161         struct ptlrpc_request *req;
162
163         req = ptlrpc_prep_req(cl, MDS_CLOSE, 1, &size, NULL);
164         if (!req)
165                 GOTO(out, rc = -ENOMEM);
166
167         body = lustre_msg_buf(req->rq_reqmsg, 0);
168         ll_ino2fid(&body->fid1, ino, 0, type);
169         body->objid = fh;
170
171         req->rq_replen = lustre_msg_size(1, &size);
172
173         rc = ptlrpc_queue_wait(cl, req);
174         rc = ptlrpc_check_status(req, rc);
175
176         EXIT;
177  out:
178         *request = req;
179         return rc;
180 }
181
182 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
183                  char *addr, struct ptlrpc_request **request)
184 {
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};
191
192         niobuf.addr = (__u64) (long) addr;
193
194         CDEBUG(D_INODE, "inode: %ld\n", ino);
195
196         bulk = ptlrpc_prep_bulk(&cl->cli_server);
197         if (bulk == NULL) {
198                 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
199                 rc = -ENOMEM;
200                 goto out;
201         }
202
203         req = ptlrpc_prep_req(cl, MDS_READPAGE, 2, size, bufs);
204         if (!req)
205                 GOTO(out, rc = -ENOMEM);
206
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;
211
212         rc = ptlrpc_register_bulk(bulk);
213         if (rc) {
214                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
215                 GOTO(out, rc);
216         }
217
218         body = lustre_msg_buf(req->rq_reqmsg, 0);
219         body->fid1.id = ino;
220         body->fid1.f_type = type;
221         body->size = offset;
222
223         req->rq_replen = lustre_msg_size(1, size);
224
225         rc = ptlrpc_queue_wait(cl, req);
226         if (rc) {
227                 CERROR("error in handling %d\n", rc);
228                 ptlrpc_abort_bulk(bulk);
229                 GOTO(out, rc);
230         }
231
232         mds_unpack_body(req);
233         EXIT;
234
235  out:
236         *request = req;
237         if (bulk != NULL)
238                 OBD_FREE(bulk, sizeof(*bulk));
239         return rc;
240 }
241
242 static int request_ioctl(struct inode *inode, struct file *file,
243                          unsigned int cmd, unsigned long arg)
244 {
245         int err;
246         struct ptlrpc_client cl;
247         struct ptlrpc_request *request;
248
249         ENTRY;
250
251         if (MINOR(inode->i_rdev) != REQUEST_MINOR)
252                 RETURN(-EINVAL);
253
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));
259                 RETURN(-EINVAL);
260         }
261
262         ptlrpc_init_client(-1, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
263         err = ptlrpc_connect_client(-1, "mds", &cl);
264         if (err) {
265                 CERROR("cannot create client\n");
266                 RETURN(-EINVAL);
267         }
268
269         switch (cmd) {
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);
274
275                 GOTO(out, err);
276         }
277
278         case IOC_REQUEST_READPAGE: {
279                 char *buf;
280                 OBD_ALLOC(buf, PAGE_SIZE);
281                 if (!buf) {
282                         err = -ENOMEM;
283                         break;
284                 }
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);
289
290                 GOTO(out, err);
291         }
292
293         case IOC_REQUEST_SETATTR: {
294                 struct inode inode;
295                 struct iattr iattr;
296
297                 inode.i_ino = arg;
298                 inode.i_generation = 0;
299                 iattr.ia_mode = 040777;
300                 iattr.ia_atime = 0;
301                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
302
303                 err = mdc_setattr(&cl, &inode, &iattr, &request);
304                 CERROR("-- done err %d\n", err);
305
306                 GOTO(out, err);
307         }
308
309         case IOC_REQUEST_CREATE: {
310                 struct inode inode;
311                 struct iattr iattr;
312
313                 inode.i_ino = arg;
314                 inode.i_generation = 0;
315                 iattr.ia_mode = 040777;
316                 iattr.ia_atime = 0;
317                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
318
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);
324
325                 GOTO(out, err);
326         }
327
328         case IOC_REQUEST_OPEN: {
329                 __u64 fh, ino;
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);
335
336                 GOTO(out, err);
337         }
338
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);
343
344                 GOTO(out, err);
345         }
346
347         default:
348                 RETURN(-EINVAL);
349         }
350
351  out:
352         ptlrpc_free_req(request);
353
354         RETURN(err);
355 }
356
357
358 static struct file_operations requestdev_fops = {
359         ioctl: request_ioctl,
360 };
361
362 static struct miscdevice request_dev = {
363         REQUEST_MINOR,
364         "request",
365         &requestdev_fops
366 };
367
368 static int __init ptlrpc_request_init(void)
369 {
370         misc_register(&request_dev);
371         return 0;
372 }
373
374 static void __exit ptlrpc_request_exit(void)
375 {
376         misc_deregister(&request_dev);
377 }
378
379 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
380 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
381 MODULE_LICENSE("GPL");
382
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);
392
393 module_init(ptlrpc_request_init);
394 module_exit(ptlrpc_request_exit);