Whamcloud - gitweb
* Change mds_client_info to mds_export_data and embed it in obd_export.
[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_lock_callback(struct lustre_handle *lockh,
191                              struct ldlm_lock_desc *desc, void *data,
192                              int data_len, struct ptlrpc_request **req)
193 {
194         int rc;
195         struct inode *inode = data;
196         ENTRY;
197
198         if (desc == NULL) {
199                 /* Completion AST.  Do nothing. */
200                 RETURN(0);
201         }
202
203         if (data_len != sizeof(*inode)) {
204                 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
205                        data_len);
206                 LBUG();
207                 RETURN(-EINVAL);
208         }
209
210         /* FIXME: do something better than throwing away everything */
211         if (inode == NULL)
212                 LBUG();
213         if (S_ISDIR(inode->i_mode)) {
214                 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
215                 invalidate_inode_pages(inode);
216         }
217
218         rc = ldlm_cli_cancel(lockh);
219         if (rc < 0) {
220                 CERROR("ldlm_cli_cancel: %d\n", rc);
221                 LBUG();
222         }
223         RETURN(0);
224 }
225
226 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
227                 struct lookup_intent *it, int lock_mode, struct inode *dir,
228                 struct dentry *de, struct lustre_handle *lockh, __u64 id,
229                 char *tgt, int tgtlen, void *data, int datalen)
230 {
231         struct ptlrpc_request *req;
232         struct obd_device *obddev = class_conn2obd(conn);
233         __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
234         int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
235         int rc, flags;
236         int repsize[3] = {sizeof(struct ldlm_reply), 
237                           sizeof(struct mds_body),
238                           obddev->u.cli.cl_max_mdsize};
239         struct ldlm_reply *dlm_rep;
240         struct ldlm_intent *lit;
241         ENTRY;
242
243         LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op), dir->i_ino);
244
245         switch (it->it_op) { 
246         case IT_MKDIR:
247                 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask; 
248                 break;
249         case (IT_CREAT|IT_OPEN):
250         case IT_CREAT:
251                 it->it_mode |= S_IFREG; /* no break */
252         case IT_MKNOD:
253                 it->it_mode &= ~current->fs->umask;
254                 break;
255         case IT_SYMLINK:
256                 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask; 
257                 break;
258         }
259
260         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
261                 size[2] = sizeof(struct mds_rec_create);
262                 size[3] = de->d_name.len + 1;
263                 size[4] = tgtlen + 1;
264                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
265                 if (!req)
266                         RETURN(-ENOMEM);
267
268                 /* pack the intent */
269                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
270                 lit->opc = NTOH__u64((__u64)it->it_op);
271
272                 /* pack the intended request */
273                 mds_create_pack(req, 2, dir, it->it_mode, id, current->fsuid,
274                                 current->fsgid, CURRENT_TIME, de->d_name.name,
275                                 de->d_name.len, tgt, tgtlen);
276                 req->rq_replen = lustre_msg_size(3, repsize);
277         } else if (it->it_op == IT_RENAME2) {
278                 struct dentry *old_de = it->it_data;
279
280                 size[2] = sizeof(struct mds_rec_rename);
281                 size[3] = old_de->d_name.len + 1;
282                 size[4] = de->d_name.len + 1;
283                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
284                 if (!req)
285                         RETURN(-ENOMEM);
286
287                 /* pack the intent */
288                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
289                 lit->opc = NTOH__u64((__u64)it->it_op);
290
291                 /* pack the intended request */
292                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
293                                 old_de->d_name.name, old_de->d_name.len,
294                                 de->d_name.name, de->d_name.len);
295                 req->rq_replen = lustre_msg_size(1, repsize);
296         } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
297                 size[2] = sizeof(struct mds_rec_unlink);
298                 size[3] = de->d_name.len + 1;
299                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
300                 if (!req)
301                         RETURN(-ENOMEM);
302
303                 /* pack the intent */
304                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
305                 lit->opc = NTOH__u64((__u64)it->it_op);
306
307                 /* pack the intended request */
308                 mds_unlink_pack(req, 2, dir, NULL, de->d_name.name, 
309                                 de->d_name.len);
310
311                 req->rq_replen = lustre_msg_size(3, repsize);
312         } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
313                    it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
314                    it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
315                 size[2] = sizeof(struct mds_body);
316                 size[3] = de->d_name.len + 1;
317
318                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
319                 if (!req)
320                         RETURN(-ENOMEM);
321
322                 /* pack the intent */
323                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
324                 lit->opc = NTOH__u64((__u64)it->it_op);
325
326                 /* pack the intended request */
327                 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
328
329                 /* get ready for the reply */
330                 req->rq_replen = lustre_msg_size(3, repsize);
331         } else if (it->it_op == IT_READDIR) {
332                 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 1, size, NULL);
333                 if (!req)
334                         RETURN(-ENOMEM);
335
336                 /* get ready for the reply */
337                 req->rq_replen = lustre_msg_size(1, repsize);
338         } else {
339                 LBUG();
340                 RETURN(-EINVAL);
341         }
342 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
343         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id, lock_type,
344                               NULL, 0, lock_mode, &flags, ldlm_completion_ast,
345                               (void *)mdc_lock_callback, data, datalen, lockh);
346         if (rc == -ENOENT || rc == ELDLM_LOCK_ABORTED) {
347                 lock_mode = 0;
348                 memset(lockh, 0, sizeof(*lockh));
349                 /* rc = 0 */
350         } else if (rc != 0) {
351                 CERROR("ldlm_cli_enqueue: %d\n", rc);
352                 RETURN(rc);
353         }
354
355         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0); 
356         it->it_disposition = (int) dlm_rep->lock_policy_res1;
357         it->it_status = (int) dlm_rep->lock_policy_res2;
358         it->it_lock_mode = lock_mode;
359         it->it_data = req;
360
361         RETURN(0);
362 }
363
364 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
365              struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
366              struct ptlrpc_request **request)
367 {
368         struct ptlrpc_client *cl;
369         struct ptlrpc_connection *connection;
370         struct lustre_handle *rconn;
371         struct mds_body *body;
372         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
373         struct ptlrpc_request *req;
374         ENTRY;
375
376         if (smd != NULL) {
377                 bufcount = 2;
378                 size[1] = smd->lmd_easize;
379         }
380
381         mdc_con2cl(conn, &cl, &connection, &rconn);
382         req = ptlrpc_prep_req2(conn, MDS_OPEN, bufcount, size, NULL);
383         if (!req)
384                 GOTO(out, rc = -ENOMEM);
385
386         req->rq_flags |= PTL_RPC_FL_REPLAY;
387         body = lustre_msg_buf(req->rq_reqmsg, 0);
388
389         ll_ino2fid(&body->fid1, ino, 0, type);
390         body->flags = HTON__u32(flags);
391         body->extra = cookie;
392
393         if (smd != NULL)
394                 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), smd, smd->lmd_easize);
395
396         req->rq_replen = lustre_msg_size(1, size);
397
398         rc = ptlrpc_queue_wait(req);
399         rc = ptlrpc_check_status(req, rc);
400         if (!rc) {
401                 body = lustre_msg_buf(req->rq_repmsg, 0);
402                 mds_unpack_body(body);
403                 *fh = body->extra;
404         }
405
406         EXIT;
407  out:
408         *request = req;
409         return rc;
410 }
411
412 int mdc_close(struct lustre_handle *conn, 
413               obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
414 {
415         struct mds_body *body;
416         int rc, size = sizeof(*body);
417         struct ptlrpc_request *req;
418
419         req = ptlrpc_prep_req2(conn, MDS_CLOSE, 1, &size, NULL);
420         if (!req)
421                 GOTO(out, rc = -ENOMEM);
422
423         body = lustre_msg_buf(req->rq_reqmsg, 0);
424         ll_ino2fid(&body->fid1, ino, 0, type);
425         body->extra = fh;
426
427         req->rq_replen = lustre_msg_size(0, NULL);
428
429         rc = ptlrpc_queue_wait(req);
430         rc = ptlrpc_check_status(req, rc);
431
432         EXIT;
433  out:
434         *request = req;
435         return rc;
436 }
437
438 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
439                  char *addr, struct ptlrpc_request **request)
440 {
441         struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
442         struct ptlrpc_request *req = NULL;
443         struct ptlrpc_bulk_desc *desc = NULL;
444         struct ptlrpc_bulk_page *bulk = NULL;
445         struct mds_body *body;
446         int rc, size = sizeof(*body);
447         ENTRY;
448
449         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
450
451         desc = ptlrpc_prep_bulk(connection);
452         if (desc == NULL)
453                 GOTO(out, rc = -ENOMEM);
454
455         req = ptlrpc_prep_req2(conn, MDS_READPAGE, 1, &size, NULL);
456         if (!req)
457                 GOTO(out2, rc = -ENOMEM);
458
459         bulk = ptlrpc_prep_bulk_page(desc);
460         bulk->b_buflen = PAGE_SIZE;
461         bulk->b_buf = addr;
462         bulk->b_xid = req->rq_xid;
463         desc->b_portal = MDS_BULK_PORTAL;
464
465         rc = ptlrpc_register_bulk(desc);
466         if (rc) {
467                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
468                 GOTO(out2, rc);
469         }
470
471         body = lustre_msg_buf(req->rq_reqmsg, 0);
472         body->fid1.id = ino;
473         body->fid1.f_type = type;
474         body->size = offset;
475
476         req->rq_replen = lustre_msg_size(1, &size);
477         rc = ptlrpc_queue_wait(req);
478         rc = ptlrpc_check_status(req, rc);
479         if (rc) {
480                 ptlrpc_abort_bulk(desc);
481                 GOTO(out2, rc);
482         } else { 
483                 body = lustre_msg_buf(req->rq_repmsg, 0);
484                 mds_unpack_body(body);
485         }
486
487         EXIT;
488  out2:
489         ptlrpc_free_bulk(desc);
490  out:
491         *request = req;
492         return rc;
493 }
494
495 int mdc_statfs(struct lustre_handle *conn, struct statfs *sfs,
496                struct ptlrpc_request **request)
497 {
498         struct obd_statfs *osfs;
499         struct ptlrpc_request *req;
500         int rc, size = sizeof(*osfs);
501         ENTRY;
502
503         req = ptlrpc_prep_req2(conn, MDS_STATFS, 0, NULL, NULL);
504         if (!req)
505                 GOTO(out, rc = -ENOMEM);
506         req->rq_replen = lustre_msg_size(1, &size);
507
508         rc = ptlrpc_queue_wait(req);
509         rc = ptlrpc_check_status(req, rc);
510
511         if (rc)
512                 GOTO(out, rc);
513
514         osfs = lustre_msg_buf(req->rq_repmsg, 0);
515         obd_statfs_unpack(osfs, sfs);
516
517         EXIT;
518 out:
519         *request = req;
520
521         return rc;
522 }
523
524 struct obd_ops mdc_obd_ops = {
525         o_setup:   client_obd_setup,
526         o_cleanup: client_obd_cleanup,
527         o_connect: client_obd_connect,
528         o_disconnect: client_obd_disconnect,
529 };
530
531 static int __init ptlrpc_request_init(void)
532 {
533         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
534 }
535
536 static void __exit ptlrpc_request_exit(void)
537 {
538         class_unregister_type(LUSTRE_MDC_NAME);
539 }
540
541 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
542 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
543 MODULE_LICENSE("GPL");
544
545 EXPORT_SYMBOL(mdc_getstatus);
546 EXPORT_SYMBOL(mdc_getlovinfo);
547 EXPORT_SYMBOL(mdc_enqueue);
548 EXPORT_SYMBOL(mdc_getattr);
549 EXPORT_SYMBOL(mdc_statfs);
550 EXPORT_SYMBOL(mdc_create);
551 EXPORT_SYMBOL(mdc_unlink);
552 EXPORT_SYMBOL(mdc_rename);
553 EXPORT_SYMBOL(mdc_link);
554 EXPORT_SYMBOL(mdc_readpage);
555 EXPORT_SYMBOL(mdc_setattr);
556 EXPORT_SYMBOL(mdc_close);
557 EXPORT_SYMBOL(mdc_open);
558
559 module_init(ptlrpc_request_init);
560 module_exit(ptlrpc_request_exit);