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