Whamcloud - gitweb
Commented-out mdc_statfs method. Not sure of how to set up all the portals
[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, size_t ea_size,
83                 struct ptlrpc_request **request)
84 {
85         struct ptlrpc_request *req;
86         struct mds_body *body;
87         int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
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         if (valid & OBD_MD_LINKNAME) {
99                 bufcount = 2;
100                 size[1] = ea_size;
101         }
102         req->rq_replen = lustre_msg_size(bufcount, size);
103         req->rq_level = LUSTRE_CONN_FULL;
104
105         rc = ptlrpc_queue_wait(req);
106         rc = ptlrpc_check_status(req, rc);
107
108         if (!rc) {
109                 mds_unpack_rep_body(req);
110                 body = lustre_msg_buf(req->rq_repmsg, 0);
111                 CDEBUG(D_NET, "mode: %o\n", body->mode);
112         }
113
114         EXIT;
115  out:
116         *request = req;
117         return rc;
118 }
119
120 int mdc_open(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
121              ino_t ino, int type, int flags, __u64 cookie, __u64 *fh,
122              struct ptlrpc_request **request)
123 {
124         struct mds_body *body;
125         int rc, size = sizeof(*body);
126         struct ptlrpc_request *req;
127
128         req = ptlrpc_prep_req(cl, conn, MDS_OPEN, 1, &size, NULL);
129         if (!req)
130                 GOTO(out, rc = -ENOMEM);
131
132         req->rq_flags |= PTL_RPC_FL_REPLAY;
133         req->rq_level = LUSTRE_CONN_FULL;
134         body = lustre_msg_buf(req->rq_reqmsg, 0);
135         ll_ino2fid(&body->fid1, ino, 0, type);
136         body->flags = HTON__u32(flags);
137         body->objid = cookie; 
138
139         req->rq_replen = lustre_msg_size(1, &size);
140
141         rc = ptlrpc_queue_wait(req);
142         rc = ptlrpc_check_status(req, rc);
143
144         if (!rc) {
145                 mds_unpack_rep_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, struct ptlrpc_connection *conn,
157               ino_t ino, int type, __u64 fh, 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, conn, 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_level = LUSTRE_CONN_FULL;
172         req->rq_replen = lustre_msg_size(0, NULL);
173
174         rc = ptlrpc_queue_wait(req);
175         rc = ptlrpc_check_status(req, rc);
176
177         EXIT;
178  out:
179         *request = req;
180         return rc;
181 }
182
183 int mdc_readpage(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
184                  ino_t ino, int type, __u64 offset, char *addr,
185                  struct ptlrpc_request **request)
186 {
187         struct ptlrpc_request *req = NULL;
188         struct ptlrpc_bulk_desc *desc = NULL;
189         struct ptlrpc_bulk_page *bulk = NULL;
190         struct mds_body *body;
191         int rc, size = sizeof(*body);
192         ENTRY;
193
194         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
195
196         desc = ptlrpc_prep_bulk(conn);
197         if (desc == NULL)
198                 GOTO(out, rc = -ENOMEM);
199
200         req = ptlrpc_prep_req(cl, conn, MDS_READPAGE, 1, &size, NULL);
201         if (!req)
202                 GOTO(out2, rc = -ENOMEM);
203
204         bulk = ptlrpc_prep_bulk_page(desc);
205         bulk->b_buflen = PAGE_SIZE;
206         bulk->b_buf = addr;
207         bulk->b_xid = req->rq_reqmsg->xid;
208         desc->b_portal = MDS_BULK_PORTAL;
209
210         rc = ptlrpc_register_bulk(desc);
211         if (rc) {
212                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
213                 GOTO(out2, 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         rc = ptlrpc_check_status(req, rc);
225         if (rc) {
226                 ptlrpc_abort_bulk(desc);
227                 GOTO(out2, rc);
228         } else
229                 mds_unpack_rep_body(req);
230
231         EXIT;
232  out2:
233         ptlrpc_free_bulk(desc);
234  out:
235         *request = req;
236         return rc;
237 }
238
239 #if 0
240 int mdc_statfs(struct ptlrpc_client *cl, struct ptlrpc_connection *conn,
241                struct statfs *statfs,
242                struct ptlrpc_request **request)
243 {
244         struct mds_rec_setattr *rec;
245         struct ptlrpc_request *req;
246         int rc, size = sizeof(*rec);
247         ENTRY;
248
249         req = ptlrpc_prep_req(cl, conn, MDS_STATFS, 1, &size, NULL);
250         if (!req)
251                 RETURN(-ENOMEM);
252
253         rec = lustre_msg_buf(req->rq_reqmsg, 0);
254         mds_setattr_pack(rec, inode, iattr);
255
256         size = sizeof(struct mds_body);
257         req->rq_replen = lustre_msg_size(1, &size);
258
259         rc = mdc_reint(cl, req, LUSTRE_CONN_FULL);
260         *request = req;
261         if (rc == -ERESTARTSYS )
262                 rc = 0;
263
264         RETURN(rc);
265 }
266 #endif
267
268 static int request_ioctl(struct inode *inode, struct file *file,
269                          unsigned int cmd, unsigned long arg)
270 {
271         int err = 0;
272         struct ptlrpc_client cl;
273         struct ptlrpc_connection *conn;
274         struct ptlrpc_request *request;
275
276         ENTRY;
277
278         if (MINOR(inode->i_rdev) != REQUEST_MINOR)
279                 RETURN(-EINVAL);
280
281         if (_IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
282             _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  ||
283             _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
284                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
285                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
286                 RETURN(-EINVAL);
287         }
288
289         ptlrpc_init_client(NULL, NULL, 
290                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
291         conn = ptlrpc_uuid_to_connection("mds");
292         if (!conn) {
293                 CERROR("cannot create client\n");
294                 RETURN(-EINVAL);
295         }
296
297         switch (cmd) {
298         case IOC_REQUEST_GETATTR: {
299                 CERROR("-- getting attr for ino %lu\n", arg);
300                 err = mdc_getattr(&cl, conn, arg, S_IFDIR, ~0, 0, &request);
301                 CERROR("-- done err %d\n", err);
302
303                 GOTO(out, err);
304         }
305
306         case IOC_REQUEST_READPAGE: {
307                 char *buf;
308                 OBD_ALLOC(buf, PAGE_SIZE);
309                 if (!buf) {
310                         err = -ENOMEM;
311                         GOTO(out, err);
312                 }
313                 CERROR("-- readpage 0 for ino %lu\n", arg);
314                 err = mdc_readpage(&cl, conn, arg, S_IFDIR, 0, buf, &request);
315                 CERROR("-- done err %d\n", err);
316                 OBD_FREE(buf, PAGE_SIZE);
317
318                 GOTO(out, err);
319         }
320
321         case IOC_REQUEST_SETATTR: {
322                 struct inode inode;
323                 struct iattr iattr;
324
325                 inode.i_ino = arg;
326                 inode.i_generation = 0;
327                 iattr.ia_mode = 040777;
328                 iattr.ia_atime = 0;
329                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
330
331                 err = mdc_setattr(&cl, conn, &inode, &iattr, &request);
332                 CERROR("-- done err %d\n", err);
333
334                 GOTO(out, err);
335         }
336
337         case IOC_REQUEST_CREATE: {
338                 struct inode inode;
339                 struct iattr iattr;
340
341                 inode.i_ino = arg;
342                 inode.i_generation = 0;
343                 iattr.ia_mode = 040777;
344                 iattr.ia_atime = 0;
345                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
346
347                 err = mdc_create(&cl, conn, &inode,
348                                  "foofile", strlen("foofile"),
349                                  NULL, 0, 0100707, 47114711,
350                                  11, 47, 0, &request);
351                 CERROR("-- done err %d\n", err);
352
353                 GOTO(out, err);
354         }
355
356         case IOC_REQUEST_OPEN: {
357                 __u64 fh, ino;
358                 copy_from_user(&ino, (__u64 *)arg, sizeof(ino));
359                 CERROR("-- opening ino %llu\n", (unsigned long long)ino);
360                 err = mdc_open(&cl, conn, ino, S_IFDIR, O_RDONLY, 4711, &fh, 
361                                &request);
362                 copy_to_user((__u64 *)arg, &fh, sizeof(fh));
363                 CERROR("-- done err %d (fh=%Lu)\n", err,
364                        (unsigned long long)fh);
365
366                 GOTO(out, err);
367         }
368
369         case IOC_REQUEST_CLOSE: {
370                 CERROR("-- closing ino 2, filehandle %lu\n", arg);
371                 err = mdc_close(&cl, conn, 2, S_IFDIR, arg, &request);
372                 CERROR("-- done err %d\n", err);
373
374                 GOTO(out, err);
375         }
376
377         default:
378                 GOTO(out, err = -EINVAL);
379         }
380
381  out:
382         ptlrpc_free_req(request);
383         ptlrpc_put_connection(conn);
384         ptlrpc_cleanup_client(&cl);
385
386         RETURN(err);
387 }
388
389
390 static struct file_operations requestdev_fops = {
391         ioctl: request_ioctl,
392 };
393
394 static struct miscdevice request_dev = {
395         REQUEST_MINOR,
396         "request",
397         &requestdev_fops
398 };
399
400 static int __init ptlrpc_request_init(void)
401 {
402         misc_register(&request_dev);
403         return 0;
404 }
405
406 static void __exit ptlrpc_request_exit(void)
407 {
408         misc_deregister(&request_dev);
409 }
410
411 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
412 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
413 MODULE_LICENSE("GPL");
414
415 EXPORT_SYMBOL(mdc_connect);
416 EXPORT_SYMBOL(mdc_getattr);
417 EXPORT_SYMBOL(mdc_create);
418 EXPORT_SYMBOL(mdc_unlink);
419 EXPORT_SYMBOL(mdc_rename);
420 EXPORT_SYMBOL(mdc_link);
421 EXPORT_SYMBOL(mdc_readpage);
422 EXPORT_SYMBOL(mdc_setattr);
423 EXPORT_SYMBOL(mdc_close);
424 EXPORT_SYMBOL(mdc_open);
425
426 module_init(ptlrpc_request_init);
427 module_exit(ptlrpc_request_exit);