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