Whamcloud - gitweb
- change I/O to use a pagearray
[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 #define DEBUG_SUBSYSTEM S_MDC
25
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/lustre_mds.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/init.h>
32 #include <linux/obd_lov.h>
33
34 #define REQUEST_MINOR 244
35
36 extern int mds_queue_req(struct ptlrpc_request *);
37
38 int mdc_con2cl(struct lustre_handle *conn, struct ptlrpc_client **cl,
39                struct ptlrpc_connection **connection,
40                struct lustre_handle **rconn)
41 {
42         struct obd_export *export;
43         struct client_obd *mdc;
44
45         export = class_conn2export(conn);
46         if (!export)
47                 return -ENOTCONN;
48
49         mdc = &export->exp_obd->u.cli;
50
51         *cl = mdc->cl_client;
52         *connection = mdc->cl_conn;
53         *rconn = &export->exp_rconnh;
54
55         return 0;
56 }
57
58 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
59                   __u64 *last_committed, __u32 *last_xid, 
60                   struct ptlrpc_request **request)
61 {
62         struct ptlrpc_request *req;
63         struct mds_body *body;
64         int rc, size = sizeof(*body);
65         ENTRY;
66
67         req = ptlrpc_prep_req2(conn, MDS_GETSTATUS, 1, &size, NULL);
68         if (!req)
69                 GOTO(out, rc = -ENOMEM);
70
71         body = lustre_msg_buf(req->rq_reqmsg, 0);
72         req->rq_level = LUSTRE_CONN_CON;
73         req->rq_replen = lustre_msg_size(1, &size);
74
75         mds_pack_req_body(req);
76         rc = ptlrpc_queue_wait(req);
77         rc = ptlrpc_check_status(req, rc);
78
79         if (!rc) {
80                 body = lustre_msg_buf(req->rq_repmsg, 0);
81                 mds_unpack_body(body);
82                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
83                 *last_committed = req->rq_repmsg->last_committed;
84                 *last_xid = req->rq_repmsg->last_xid;
85
86                 CDEBUG(D_NET, "root ino=%ld, last_committed=%Lu, last_xid=%d\n",
87                        (unsigned long)rootfid->id,
88                        (unsigned long long)*last_committed, last_xid);
89         }
90
91         EXIT;
92  out:
93         ptlrpc_free_req(req); 
94         return rc;
95 }
96
97 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
98                    uuid_t **uuids, struct ptlrpc_request **request)
99 {
100         struct ptlrpc_request *req;
101         struct mds_status_req *streq;
102         struct lov_obd *lov = &obd->u.lov;
103         struct client_obd *mdc = &lov->mdcobd->u.cli;
104         struct lov_desc *desc = &lov->desc;
105         int rc, size[2] = {sizeof(*streq)};
106         ENTRY;
107
108         req = ptlrpc_prep_req2(mdc_connh, MDS_GETLOVINFO, 1, size, NULL);
109         if (!req)
110                 GOTO(out, rc = -ENOMEM);
111
112         *request = req;
113         streq = lustre_msg_buf(req->rq_reqmsg, 0);
114         streq->flags = HTON__u32(MDS_STATUS_LOV);
115         streq->repbuf = HTON__u32(8000);
116
117         /* prepare for reply */
118         req->rq_level = LUSTRE_CONN_CON;
119         size[0] = sizeof(*desc);
120         size[1] = 8000;
121         req->rq_replen = lustre_msg_size(2, size);
122
123         rc = ptlrpc_queue_wait(req);
124         rc = ptlrpc_check_status(req, rc);
125
126         if (!rc) {
127                 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
128                 *uuids = lustre_msg_buf(req->rq_repmsg, 1);
129                 lov_unpackdesc(desc);
130         }
131         mdc->cl_max_mdsize = sizeof(*desc) +
132                 desc->ld_tgt_count * sizeof(uuid_t);
133
134         EXIT;
135  out:
136         return rc;
137 }
138
139
140 int mdc_getattr(struct lustre_handle *conn,
141                 obd_id ino, int type, unsigned long valid, size_t ea_size,
142                 struct ptlrpc_request **request)
143 {
144         struct ptlrpc_request *req;
145         struct mds_body *body;
146         int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
147         ENTRY;
148
149         req = ptlrpc_prep_req2(conn, MDS_GETATTR, 1, size, NULL);
150         if (!req)
151                 GOTO(out, rc = -ENOMEM);
152
153         body = lustre_msg_buf(req->rq_reqmsg, 0);
154         ll_ino2fid(&body->fid1, ino, 0, type);
155         body->valid = valid;
156
157         if (S_ISREG(type)) {
158                 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
159                 bufcount = 2;
160                 size[1] = mdc->cl_max_mdsize;
161         } else if (valid & OBD_MD_LINKNAME) {
162                 bufcount = 2;
163                 size[1] = ea_size;
164         }
165         req->rq_replen = lustre_msg_size(bufcount, size);
166
167         rc = ptlrpc_queue_wait(req);
168         rc = ptlrpc_check_status(req, rc);
169
170         if (!rc) {
171                 body = lustre_msg_buf(req->rq_repmsg, 0);
172                 mds_unpack_body(body);
173                 CDEBUG(D_NET, "mode: %o\n", body->mode);
174         }
175
176         EXIT;
177  out:
178         *request = req;
179         return rc;
180 }
181
182 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
183                             void *data, __u32 data_len)
184 {
185         int rc;
186         struct inode *inode = data;
187         struct lustre_handle lockh;
188         ENTRY;
189
190         if (data_len != sizeof(*inode)) {
191                 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
192                        data_len);
193                 LBUG();
194                 RETURN(-EINVAL);
195         }
196
197         /* FIXME: do something better than throwing away everything */
198         if (inode == NULL)
199                 LBUG();
200         if (S_ISDIR(inode->i_mode)) {
201                 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
202                 invalidate_inode_pages(inode);
203         }
204
205         ldlm_lock2handle(lock, &lockh);
206         rc = ldlm_cli_cancel(&lockh);
207         if (rc < 0) {
208                 CERROR("ldlm_cli_cancel: %d\n", rc);
209                 LBUG();
210         }
211         RETURN(0);
212 }
213
214 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
215                 struct lookup_intent *it, int lock_mode, struct inode *dir,
216                 struct dentry *de, struct lustre_handle *lockh, __u64 id,
217                 char *tgt, int tgtlen, void *data, int datalen)
218 {
219         struct ptlrpc_request *req;
220         struct obd_device *obddev = class_conn2obd(conn);
221         __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
222         int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
223         int rc, flags;
224         int repsize[3] = {sizeof(struct ldlm_reply), 
225                           sizeof(struct mds_body),
226                           obddev->u.cli.cl_max_mdsize};
227         struct ldlm_reply *dlm_rep;
228         struct ldlm_intent *lit;
229         ENTRY;
230
231         LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op), dir->i_ino);
232
233         switch (it->it_op) { 
234         case IT_MKDIR:
235                 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask; 
236                 break;
237         case (IT_CREAT|IT_OPEN):
238         case IT_CREAT:
239                 it->it_mode |= S_IFREG; /* no break */
240         case IT_MKNOD:
241                 it->it_mode &= ~current->fs->umask;
242                 break;
243         case IT_SYMLINK:
244                 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask; 
245                 break;
246         }
247
248         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
249                 size[2] = sizeof(struct mds_rec_create);
250                 size[3] = de->d_name.len + 1;
251                 size[4] = tgtlen + 1;
252                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
253                 if (!req)
254                         RETURN(-ENOMEM);
255
256                 /* pack the intent */
257                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
258                 lit->opc = NTOH__u64((__u64)it->it_op);
259
260                 /* pack the intended request */
261                 mds_create_pack(req, 2, dir, it->it_mode, id, current->fsuid,
262                                 current->fsgid, CURRENT_TIME, de->d_name.name,
263                                 de->d_name.len, tgt, tgtlen);
264                 req->rq_replen = lustre_msg_size(3, repsize);
265         } else if (it->it_op == IT_RENAME2) {
266                 struct dentry *old_de = it->it_data;
267
268                 size[2] = sizeof(struct mds_rec_rename);
269                 size[3] = old_de->d_name.len + 1;
270                 size[4] = de->d_name.len + 1;
271                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
272                 if (!req)
273                         RETURN(-ENOMEM);
274
275                 /* pack the intent */
276                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
277                 lit->opc = NTOH__u64((__u64)it->it_op);
278
279                 /* pack the intended request */
280                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
281                                 old_de->d_name.name, old_de->d_name.len,
282                                 de->d_name.name, de->d_name.len);
283                 req->rq_replen = lustre_msg_size(1, repsize);
284         } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
285                 size[2] = sizeof(struct mds_rec_unlink);
286                 size[3] = de->d_name.len + 1;
287                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
288                 if (!req)
289                         RETURN(-ENOMEM);
290
291                 /* pack the intent */
292                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
293                 lit->opc = NTOH__u64((__u64)it->it_op);
294
295                 /* pack the intended request */
296                 mds_unlink_pack(req, 2, dir, NULL, de->d_name.name, 
297                                 de->d_name.len);
298
299                 req->rq_replen = lustre_msg_size(3, repsize);
300         } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
301                    it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
302                    it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
303                 size[2] = sizeof(struct mds_body);
304                 size[3] = de->d_name.len + 1;
305
306                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
307                 if (!req)
308                         RETURN(-ENOMEM);
309
310                 /* pack the intent */
311                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
312                 lit->opc = NTOH__u64((__u64)it->it_op);
313
314                 /* pack the intended request */
315                 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
316
317                 /* get ready for the reply */
318                 req->rq_replen = lustre_msg_size(3, repsize);
319         } else if (it->it_op == IT_READDIR) {
320                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 1, size, NULL);
321                 if (!req)
322                         RETURN(-ENOMEM);
323
324                 /* get ready for the reply */
325                 req->rq_replen = lustre_msg_size(1, repsize);
326         } else {
327                 LBUG();
328                 RETURN(-EINVAL);
329         }
330 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
331         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
332                               lock_type, NULL, 0, lock_mode, &flags,
333                               ldlm_completion_ast, mdc_blocking_ast, data,
334                               datalen, lockh);
335         if (rc == -ENOENT) {
336                 /* This can go when we're sure that this can never happen */
337                 LBUG();
338         }
339         if (rc == ELDLM_LOCK_ABORTED) {
340                 lock_mode = 0;
341                 memset(lockh, 0, sizeof(*lockh));
342                 /* rc = 0 */
343         } else if (rc != 0) {
344                 CERROR("ldlm_cli_enqueue: %d\n", rc);
345                 RETURN(rc);
346         }
347
348         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0); 
349         it->it_disposition = (int) dlm_rep->lock_policy_res1;
350         it->it_status = (int) dlm_rep->lock_policy_res2;
351         it->it_lock_mode = lock_mode;
352         it->it_data = req;
353
354         RETURN(0);
355 }
356
357 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
358              struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
359              struct ptlrpc_request **request)
360 {
361         struct ptlrpc_client *cl;
362         struct ptlrpc_connection *connection;
363         struct lustre_handle *rconn;
364         struct mds_body *body;
365         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
366         struct ptlrpc_request *req;
367         ENTRY;
368
369         if (smd != NULL) {
370                 bufcount = 2;
371                 size[1] = smd->lmd_easize;
372         }
373
374         mdc_con2cl(conn, &cl, &connection, &rconn);
375         req = ptlrpc_prep_req2(conn, MDS_OPEN, bufcount, size, NULL);
376         if (!req)
377                 GOTO(out, rc = -ENOMEM);
378
379         req->rq_flags |= PTL_RPC_FL_REPLAY;
380         body = lustre_msg_buf(req->rq_reqmsg, 0);
381
382         ll_ino2fid(&body->fid1, ino, 0, type);
383         body->flags = HTON__u32(flags);
384         body->extra = cookie;
385
386         if (smd != NULL)
387                 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), smd, smd->lmd_easize);
388
389         req->rq_replen = lustre_msg_size(1, size);
390
391         rc = ptlrpc_queue_wait(req);
392         rc = ptlrpc_check_status(req, rc);
393         if (!rc) {
394                 body = lustre_msg_buf(req->rq_repmsg, 0);
395                 mds_unpack_body(body);
396                 *fh = body->extra;
397         }
398
399         EXIT;
400  out:
401         *request = req;
402         return rc;
403 }
404
405 int mdc_close(struct lustre_handle *conn, 
406               obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
407 {
408         struct mds_body *body;
409         int rc, size = sizeof(*body);
410         struct ptlrpc_request *req;
411
412         req = ptlrpc_prep_req2(conn, MDS_CLOSE, 1, &size, NULL);
413         if (!req)
414                 GOTO(out, rc = -ENOMEM);
415
416         body = lustre_msg_buf(req->rq_reqmsg, 0);
417         ll_ino2fid(&body->fid1, ino, 0, type);
418         body->extra = fh;
419
420         req->rq_replen = lustre_msg_size(0, NULL);
421
422         rc = ptlrpc_queue_wait(req);
423         rc = ptlrpc_check_status(req, rc);
424
425         EXIT;
426  out:
427         *request = req;
428         return rc;
429 }
430
431 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
432                  char *addr, struct ptlrpc_request **request)
433 {
434         struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
435         struct ptlrpc_request *req = NULL;
436         struct ptlrpc_bulk_desc *desc = NULL;
437         struct ptlrpc_bulk_page *bulk = NULL;
438         struct mds_body *body;
439         int rc, size = sizeof(*body);
440         ENTRY;
441
442         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
443
444         desc = ptlrpc_prep_bulk(connection);
445         if (desc == NULL)
446                 GOTO(out, rc = -ENOMEM);
447
448         req = ptlrpc_prep_req2(conn, MDS_READPAGE, 1, &size, NULL);
449         if (!req)
450                 GOTO(out2, rc = -ENOMEM);
451
452         bulk = ptlrpc_prep_bulk_page(desc);
453         bulk->b_buflen = PAGE_SIZE;
454         bulk->b_buf = addr;
455         bulk->b_xid = req->rq_xid;
456         desc->b_portal = MDS_BULK_PORTAL;
457
458         rc = ptlrpc_register_bulk(desc);
459         if (rc) {
460                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
461                 GOTO(out2, rc);
462         }
463
464         body = lustre_msg_buf(req->rq_reqmsg, 0);
465         body->fid1.id = ino;
466         body->fid1.f_type = type;
467         body->size = offset;
468
469         req->rq_replen = lustre_msg_size(1, &size);
470         rc = ptlrpc_queue_wait(req);
471         rc = ptlrpc_check_status(req, rc);
472         if (rc) {
473                 ptlrpc_abort_bulk(desc);
474                 GOTO(out2, rc);
475         } else { 
476                 body = lustre_msg_buf(req->rq_repmsg, 0);
477                 mds_unpack_body(body);
478         }
479
480         EXIT;
481  out2:
482         ptlrpc_free_bulk(desc);
483  out:
484         *request = req;
485         return rc;
486 }
487
488 int mdc_statfs(struct lustre_handle *conn, struct statfs *sfs,
489                struct ptlrpc_request **request)
490 {
491         struct obd_statfs *osfs;
492         struct ptlrpc_request *req;
493         int rc, size = sizeof(*osfs);
494         ENTRY;
495
496         req = ptlrpc_prep_req2(conn, MDS_STATFS, 0, NULL, NULL);
497         if (!req)
498                 GOTO(out, rc = -ENOMEM);
499         req->rq_replen = lustre_msg_size(1, &size);
500
501         rc = ptlrpc_queue_wait(req);
502         rc = ptlrpc_check_status(req, rc);
503
504         if (rc)
505                 GOTO(out, rc);
506
507         osfs = lustre_msg_buf(req->rq_repmsg, 0);
508         obd_statfs_unpack(osfs, sfs);
509
510         EXIT;
511 out:
512         *request = req;
513
514         return rc;
515 }
516
517 struct obd_ops mdc_obd_ops = {
518         o_setup:   client_obd_setup,
519         o_cleanup: client_obd_cleanup,
520         o_connect: client_obd_connect,
521         o_disconnect: client_obd_disconnect,
522 };
523
524 static int __init ptlrpc_request_init(void)
525 {
526         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
527 }
528
529 static void __exit ptlrpc_request_exit(void)
530 {
531         class_unregister_type(LUSTRE_MDC_NAME);
532 }
533
534 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
535 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
536 MODULE_LICENSE("GPL");
537
538 EXPORT_SYMBOL(mdc_getstatus);
539 EXPORT_SYMBOL(mdc_getlovinfo);
540 EXPORT_SYMBOL(mdc_enqueue);
541 EXPORT_SYMBOL(mdc_getattr);
542 EXPORT_SYMBOL(mdc_statfs);
543 EXPORT_SYMBOL(mdc_create);
544 EXPORT_SYMBOL(mdc_unlink);
545 EXPORT_SYMBOL(mdc_rename);
546 EXPORT_SYMBOL(mdc_link);
547 EXPORT_SYMBOL(mdc_readpage);
548 EXPORT_SYMBOL(mdc_setattr);
549 EXPORT_SYMBOL(mdc_close);
550 EXPORT_SYMBOL(mdc_open);
551
552 module_init(ptlrpc_request_init);
553 module_exit(ptlrpc_request_exit);