Whamcloud - gitweb
- Fix the mdc_replay_open fixup so that it updates the handle in the Lustre
[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                         CERROR("ldlm_cli_cancel: %d\n", rc);
179                         LBUG();
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                         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         switch (it->it_op) {
220         case IT_MKDIR:
221                 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
222                 break;
223         case (IT_CREAT|IT_OPEN):
224         case IT_CREAT:
225                 it->it_mode |= S_IFREG; /* no break */
226         case IT_MKNOD:
227                 it->it_mode &= ~current->fs->umask;
228                 break;
229         case IT_SYMLINK:
230                 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
231                 break;
232         }
233
234         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
235                 size[2] = sizeof(struct mds_rec_create);
236                 size[3] = de->d_name.len + 1;
237                 size[4] = tgtlen + 1;
238                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
239                                       size, NULL);
240                 if (!req)
241                         RETURN(-ENOMEM);
242
243                 /* pack the intent */
244                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
245                 lit->opc = NTOH__u64((__u64)it->it_op);
246
247                 /* pack the intended request */
248                 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
249                                 current->fsgid, CURRENT_TIME, de->d_name.name,
250                                 de->d_name.len, tgt, tgtlen);
251                 req->rq_replen = lustre_msg_size(3, repsize);
252         } else if (it->it_op == IT_RENAME2) {
253                 struct dentry *old_de = it->it_data;
254
255                 size[2] = sizeof(struct mds_rec_rename);
256                 size[3] = old_de->d_name.len + 1;
257                 size[4] = de->d_name.len + 1;
258                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
259                                       size, NULL);
260                 if (!req)
261                         RETURN(-ENOMEM);
262
263                 /* pack the intent */
264                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
265                 lit->opc = NTOH__u64((__u64)it->it_op);
266
267                 /* pack the intended request */
268                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
269                                 old_de->d_name.name, old_de->d_name.len,
270                                 de->d_name.name, de->d_name.len);
271                 req->rq_replen = lustre_msg_size(3, repsize);
272         } else if (it->it_op == IT_LINK2) {
273                 struct dentry *old_de = it->it_data;
274
275                 size[2] = sizeof(struct mds_rec_link);
276                 size[3] = de->d_name.len + 1;
277                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
278                                       size, NULL);
279                 if (!req)
280                         RETURN(-ENOMEM);
281
282                 /* pack the intent */
283                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
284                 lit->opc = NTOH__u64((__u64)it->it_op);
285
286                 /* pack the intended request */
287                 mds_link_pack(req, 2, old_de->d_inode, dir,
288                               de->d_name.name, de->d_name.len);
289                 req->rq_replen = lustre_msg_size(3, repsize);
290         } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
291                 size[2] = sizeof(struct mds_rec_unlink);
292                 size[3] = de->d_name.len + 1;
293                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
294                                       size, NULL);
295                 if (!req)
296                         RETURN(-ENOMEM);
297
298                 /* pack the intent */
299                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
300                 lit->opc = NTOH__u64((__u64)it->it_op);
301
302                 /* pack the intended request */
303                 mds_unlink_pack(req, 2, dir, NULL,
304                                 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
305                                 de->d_name.name, de->d_name.len);
306
307                 req->rq_replen = lustre_msg_size(3, repsize);
308         } else if (it->it_op  & (IT_GETATTR | IT_RENAME | IT_LINK | 
309                    IT_OPEN |  IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
310                 size[2] = sizeof(struct mds_body);
311                 size[3] = de->d_name.len + 1;
312
313                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
314                                       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                 /* we need to replay opens */
326                 if (it->it_op == IT_OPEN)
327                         req->rq_flags |= PTL_RPC_FL_REPLAY;
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_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
333                                       size, NULL);
334                 if (!req)
335                         RETURN(-ENOMEM);
336
337                 /* get ready for the reply */
338                 req->rq_replen = lustre_msg_size(1, repsize);
339         } else {
340                 LBUG();
341                 RETURN(-EINVAL);
342         }
343 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
344         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
345                               lock_type, NULL, 0, lock_mode, &flags,
346                               ldlm_completion_ast, mdc_blocking_ast, data,
347                               datalen, lockh);
348         if (rc == -ENOENT) {
349                 /* This can go when we're sure that this can never happen */
350                 LBUG();
351         }
352         if (rc == ELDLM_LOCK_ABORTED) {
353                 lock_mode = 0;
354                 memset(lockh, 0, sizeof(*lockh));
355                 /* rc = 0 */
356         } else if (rc != 0) {
357                 CERROR("ldlm_cli_enqueue: %d\n", rc);
358                 RETURN(rc);
359         }
360
361         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
362         it->it_disposition = (int) dlm_rep->lock_policy_res1;
363         it->it_status = (int) dlm_rep->lock_policy_res2;
364         it->it_lock_mode = lock_mode;
365         it->it_data = req;
366
367         RETURN(0);
368 }
369
370 static void mdc_replay_open(struct ptlrpc_request *req, void *data)
371 {
372         struct lustre_handle *fh = data;
373         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
374
375         mds_unpack_body(body);
376         CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
377                fh->addr, fh->cookie, body->handle.addr, body->handle.cookie);
378         memcpy(fh, &body->handle, sizeof(*fh));
379 }
380
381 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
382              struct lov_stripe_md *lsm, struct lustre_handle *fh,
383              struct ptlrpc_request **request)
384 {
385         struct mds_body *body;
386         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
387         struct ptlrpc_request *req;
388         ENTRY;
389
390         if (lsm) {
391                 bufcount = 2;
392                 // size[1] = mdc->cl_max_mds_easize; soon...
393                 size[1] = lsm->lsm_mds_easize;
394         }
395
396         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
397                               NULL);
398         if (!req)
399                 GOTO(out, rc = -ENOMEM);
400
401         req->rq_flags |= PTL_RPC_FL_REPLAY;
402         body = lustre_msg_buf(req->rq_reqmsg, 0);
403
404         ll_ino2fid(&body->fid1, ino, 0, type);
405         body->flags = HTON__u32(flags);
406         memcpy(&body->handle, fh, sizeof(body->handle));
407
408         if (lsm)
409                 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), lsm);
410
411         req->rq_replen = lustre_msg_size(1, size);
412
413         rc = ptlrpc_queue_wait(req);
414         rc = ptlrpc_check_status(req, rc);
415         if (!rc) {
416                 body = lustre_msg_buf(req->rq_repmsg, 0);
417                 mds_unpack_body(body);
418                 memcpy(fh, &body->handle, sizeof(*fh));
419         }
420
421         /* If open is replayed, we need to fix up the fh. */
422         req->rq_replay_cb = mdc_replay_open;
423         req->rq_replay_cb_data = fh;
424
425         EXIT;
426  out:
427         *request = req;
428         return rc;
429 }
430
431 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
432               struct lustre_handle *fh, struct ptlrpc_request **request)
433 {
434         struct mds_body *body;
435         int rc, size = sizeof(*body);
436         struct ptlrpc_request *req;
437
438         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
439                               NULL);
440         if (!req)
441                 GOTO(out, rc = -ENOMEM);
442
443         body = lustre_msg_buf(req->rq_reqmsg, 0);
444         ll_ino2fid(&body->fid1, ino, 0, type);
445         memcpy(&body->handle, fh, sizeof(body->handle));
446
447         req->rq_replen = lustre_msg_size(0, NULL);
448
449         rc = ptlrpc_queue_wait(req);
450         rc = ptlrpc_check_status(req, rc);
451
452         EXIT;
453  out:
454         *request = req;
455         return rc;
456 }
457
458 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
459                  char *addr, struct ptlrpc_request **request)
460 {
461         struct ptlrpc_connection *connection = 
462                 client_conn2cli(conn)->cl_import.imp_connection;
463         struct ptlrpc_request *req = NULL;
464         struct ptlrpc_bulk_desc *desc = NULL;
465         struct ptlrpc_bulk_page *bulk = NULL;
466         struct mds_body *body;
467         int rc, size = sizeof(*body);
468         ENTRY;
469
470         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
471
472         desc = ptlrpc_prep_bulk(connection);
473         if (desc == NULL)
474                 GOTO(out, rc = -ENOMEM);
475
476         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
477                               NULL);
478         if (!req)
479                 GOTO(out2, rc = -ENOMEM);
480
481         bulk = ptlrpc_prep_bulk_page(desc);
482         bulk->bp_buflen = PAGE_SIZE;
483         bulk->bp_buf = addr;
484         bulk->bp_xid = req->rq_xid;
485         desc->bd_portal = MDS_BULK_PORTAL;
486
487         rc = ptlrpc_register_bulk(desc);
488         if (rc) {
489                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
490                 GOTO(out2, rc);
491         }
492
493         body = lustre_msg_buf(req->rq_reqmsg, 0);
494         body->fid1.id = ino;
495         body->fid1.f_type = type;
496         body->size = offset;
497
498         req->rq_replen = lustre_msg_size(1, &size);
499         rc = ptlrpc_queue_wait(req);
500         rc = ptlrpc_check_status(req, rc);
501         if (rc) {
502                 ptlrpc_abort_bulk(desc);
503                 GOTO(out2, rc);
504         } else {
505                 body = lustre_msg_buf(req->rq_repmsg, 0);
506                 mds_unpack_body(body);
507         }
508
509         EXIT;
510  out2:
511         ptlrpc_free_bulk(desc);
512  out:
513         *request = req;
514         return rc;
515 }
516
517 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
518                struct ptlrpc_request **request)
519 {
520         struct ptlrpc_request *req;
521         int rc, size = sizeof(*osfs);
522         ENTRY;
523
524         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
525                               NULL);
526         if (!req)
527                 GOTO(out, rc = -ENOMEM);
528         req->rq_replen = lustre_msg_size(1, &size);
529
530         rc = ptlrpc_queue_wait(req);
531         rc = ptlrpc_check_status(req, rc);
532
533         if (rc)
534                 GOTO(out, rc);
535
536         obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
537
538         EXIT;
539 out:
540         *request = req;
541
542         return rc;
543 }
544
545 struct obd_ops mdc_obd_ops = {
546         o_setup:   client_obd_setup,
547         o_cleanup: client_obd_cleanup,
548         o_connect: client_obd_connect,
549         o_disconnect: client_obd_disconnect,
550 };
551
552 static int __init ptlrpc_request_init(void)
553 {
554         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
555 }
556
557 static void __exit ptlrpc_request_exit(void)
558 {
559         class_unregister_type(LUSTRE_MDC_NAME);
560 }
561
562 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
563 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
564 MODULE_LICENSE("GPL");
565
566 EXPORT_SYMBOL(mdc_getstatus);
567 EXPORT_SYMBOL(mdc_getlovinfo);
568 EXPORT_SYMBOL(mdc_enqueue);
569 EXPORT_SYMBOL(mdc_getattr);
570 EXPORT_SYMBOL(mdc_statfs);
571 EXPORT_SYMBOL(mdc_create);
572 EXPORT_SYMBOL(mdc_unlink);
573 EXPORT_SYMBOL(mdc_rename);
574 EXPORT_SYMBOL(mdc_link);
575 EXPORT_SYMBOL(mdc_readpage);
576 EXPORT_SYMBOL(mdc_setattr);
577 EXPORT_SYMBOL(mdc_close);
578 EXPORT_SYMBOL(mdc_open);
579
580 module_init(ptlrpc_request_init);
581 module_exit(ptlrpc_request_exit);