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