Whamcloud - gitweb
- compile fixes for 2.5.44
[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 static void mdc_replay_open(struct ptlrpc_request *req, void *data)
370 {
371         struct lustre_handle *fh = data;
372         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
373
374         mds_unpack_body(body);
375         CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
376                fh->addr, fh->cookie, body->handle.addr, body->handle.cookie);
377         memcpy(fh, &body->handle, sizeof(*fh));
378 }
379
380 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
381              struct lov_stripe_md *lsm, struct lustre_handle *fh,
382              struct ptlrpc_request **request)
383 {
384         struct mds_body *body;
385         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
386         struct ptlrpc_request *req;
387         ENTRY;
388
389         if (lsm) {
390                 bufcount = 2;
391                 // size[1] = mdc->cl_max_mds_easize; soon...
392                 size[1] = lsm->lsm_mds_easize;
393         }
394
395         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
396                               NULL);
397         if (!req)
398                 GOTO(out, rc = -ENOMEM);
399
400         req->rq_flags |= PTL_RPC_FL_REPLAY;
401         body = lustre_msg_buf(req->rq_reqmsg, 0);
402
403         ll_ino2fid(&body->fid1, ino, 0, type);
404         body->flags = HTON__u32(flags);
405         memcpy(&body->handle, fh, sizeof(body->handle));
406
407         if (lsm)
408                 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), lsm);
409
410         req->rq_replen = lustre_msg_size(1, size);
411
412         rc = ptlrpc_queue_wait(req);
413         rc = ptlrpc_check_status(req, rc);
414         if (!rc) {
415                 body = lustre_msg_buf(req->rq_repmsg, 0);
416                 mds_unpack_body(body);
417                 memcpy(fh, &body->handle, sizeof(*fh));
418         }
419
420         /* If open is replayed, we need to fix up the fh. */
421         req->rq_replay_cb = mdc_replay_open;
422         req->rq_replay_cb_data = fh;
423
424         EXIT;
425  out:
426         *request = req;
427         return rc;
428 }
429
430 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
431               struct lustre_handle *fh, struct ptlrpc_request **request)
432 {
433         struct mds_body *body;
434         int rc, size = sizeof(*body);
435         struct ptlrpc_request *req;
436
437         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
438                               NULL);
439         if (!req)
440                 GOTO(out, rc = -ENOMEM);
441
442         body = lustre_msg_buf(req->rq_reqmsg, 0);
443         ll_ino2fid(&body->fid1, ino, 0, type);
444         memcpy(&body->handle, fh, sizeof(body->handle));
445
446         req->rq_replen = lustre_msg_size(0, NULL);
447
448         rc = ptlrpc_queue_wait(req);
449         rc = ptlrpc_check_status(req, rc);
450
451         EXIT;
452  out:
453         *request = req;
454         return rc;
455 }
456
457 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
458                  char *addr, struct ptlrpc_request **request)
459 {
460         struct ptlrpc_connection *connection = 
461                 client_conn2cli(conn)->cl_import.imp_connection;
462         struct ptlrpc_request *req = NULL;
463         struct ptlrpc_bulk_desc *desc = NULL;
464         struct ptlrpc_bulk_page *bulk = NULL;
465         struct mds_body *body;
466         int rc, size = sizeof(*body);
467         ENTRY;
468
469         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
470
471         desc = ptlrpc_prep_bulk(connection);
472         if (desc == NULL)
473                 GOTO(out, rc = -ENOMEM);
474
475         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
476                               NULL);
477         if (!req)
478                 GOTO(out2, rc = -ENOMEM);
479
480         bulk = ptlrpc_prep_bulk_page(desc);
481         bulk->bp_buflen = PAGE_SIZE;
482         bulk->bp_buf = addr;
483         bulk->bp_xid = req->rq_xid;
484         desc->bd_portal = MDS_BULK_PORTAL;
485
486         rc = ptlrpc_register_bulk(desc);
487         if (rc) {
488                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
489                 GOTO(out2, rc);
490         }
491
492         body = lustre_msg_buf(req->rq_reqmsg, 0);
493         body->fid1.id = ino;
494         body->fid1.f_type = type;
495         body->size = offset;
496
497         req->rq_replen = lustre_msg_size(1, &size);
498         rc = ptlrpc_queue_wait(req);
499         rc = ptlrpc_check_status(req, rc);
500         if (rc) {
501                 ptlrpc_abort_bulk(desc);
502                 GOTO(out2, rc);
503         } else {
504                 body = lustre_msg_buf(req->rq_repmsg, 0);
505                 mds_unpack_body(body);
506         }
507
508         EXIT;
509  out2:
510         ptlrpc_free_bulk(desc);
511  out:
512         *request = req;
513         return rc;
514 }
515
516 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
517                struct ptlrpc_request **request)
518 {
519         struct ptlrpc_request *req;
520         int rc, size = sizeof(*osfs);
521         ENTRY;
522
523         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
524                               NULL);
525         if (!req)
526                 GOTO(out, rc = -ENOMEM);
527         req->rq_replen = lustre_msg_size(1, &size);
528
529         rc = ptlrpc_queue_wait(req);
530         rc = ptlrpc_check_status(req, rc);
531
532         if (rc)
533                 GOTO(out, rc);
534
535         obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
536
537         EXIT;
538 out:
539         *request = req;
540
541         return rc;
542 }
543
544 struct obd_ops mdc_obd_ops = {
545         o_setup:   client_obd_setup,
546         o_cleanup: client_obd_cleanup,
547         o_connect: client_obd_connect,
548         o_disconnect: client_obd_disconnect,
549 };
550
551 static int __init ptlrpc_request_init(void)
552 {
553         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
554 }
555
556 static void __exit ptlrpc_request_exit(void)
557 {
558         class_unregister_type(LUSTRE_MDC_NAME);
559 }
560
561 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
562 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
563 MODULE_LICENSE("GPL");
564
565 EXPORT_SYMBOL(mdc_getstatus);
566 EXPORT_SYMBOL(mdc_getlovinfo);
567 EXPORT_SYMBOL(mdc_enqueue);
568 EXPORT_SYMBOL(mdc_getattr);
569 EXPORT_SYMBOL(mdc_statfs);
570 EXPORT_SYMBOL(mdc_create);
571 EXPORT_SYMBOL(mdc_unlink);
572 EXPORT_SYMBOL(mdc_rename);
573 EXPORT_SYMBOL(mdc_link);
574 EXPORT_SYMBOL(mdc_readpage);
575 EXPORT_SYMBOL(mdc_setattr);
576 EXPORT_SYMBOL(mdc_close);
577 EXPORT_SYMBOL(mdc_open);
578
579 module_init(ptlrpc_request_init);
580 module_exit(ptlrpc_request_exit);