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