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