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