Whamcloud - gitweb
Default to an rq_level of LUSTRE_CONN_FULL.
[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
32 #define REQUEST_MINOR 244
33
34 extern int mds_queue_req(struct ptlrpc_request *);
35
36
37 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
38                   __u64 *last_committed, __u64 *last_rcvd,
39                   __u32 *last_xid, struct ptlrpc_request **request)
40 {
41         struct ptlrpc_request *req;
42         struct mds_body *body;
43         struct mdc_obd *mdc = mdc_conn2mdc(conn);
44         int rc, size = sizeof(*body);
45         ENTRY;
46
47         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
48                               MDS_GETSTATUS, 1, &size, 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_rcvd = req->rq_repmsg->last_rcvd;
66                 *last_xid = body->last_xid;
67
68                 CDEBUG(D_NET, "root ino=%ld, last_committed=%Lu, last_rcvd=%Lu,"
69                        " last_xid=%d\n",
70                        (unsigned long)rootfid->id,
71                        (unsigned long long)*last_committed,
72                        (unsigned long long)*last_rcvd,
73                        body->last_xid);
74         }
75
76         EXIT;
77  out:
78         ptlrpc_free_req(req); 
79         return rc;
80 }
81
82
83 int mdc_getattr(struct lustre_handle *conn,
84                 obd_id ino, int type, unsigned long valid, size_t ea_size,
85                 struct ptlrpc_request **request)
86 {
87         struct mdc_obd *mdc = mdc_conn2mdc(conn);
88         struct ptlrpc_request *req;
89         struct mds_body *body;
90         int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
91         ENTRY;
92
93         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
94                                MDS_GETATTR, 1, size, NULL);
95         if (!req)
96                 GOTO(out, rc = -ENOMEM);
97
98         body = lustre_msg_buf(req->rq_reqmsg, 0);
99         ll_ino2fid(&body->fid1, ino, 0, type);
100         body->valid = valid;
101
102         if (S_ISREG(type)) {
103                 bufcount = 2;
104                 size[1] = sizeof(struct obdo);
105         } else if (valid & OBD_MD_LINKNAME) {
106                 bufcount = 2;
107                 size[1] = ea_size;
108         }
109         req->rq_replen = lustre_msg_size(bufcount, size);
110
111         rc = ptlrpc_queue_wait(req);
112         rc = ptlrpc_check_status(req, rc);
113
114         if (!rc) {
115                 body = lustre_msg_buf(req->rq_repmsg, 0);
116                 mds_unpack_body(body);
117                 CDEBUG(D_NET, "mode: %o\n", body->mode);
118         }
119
120         EXIT;
121  out:
122         *request = req;
123         return rc;
124 }
125
126 static int mdc_lock_callback(struct lustre_handle *lockh,
127                              struct ldlm_lock_desc *desc, void *data,
128                              int data_len, struct ptlrpc_request **req)
129 {
130         int rc;
131         struct inode *inode = data;
132         ENTRY;
133
134         if (desc == NULL) {
135                 /* Completion AST.  Do nothing. */
136                 RETURN(0);
137         }
138
139         if (data_len != sizeof(*inode)) {
140                 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
141                        data_len);
142                 LBUG();
143         }
144
145         /* FIXME: do something better than throwing away everything */
146         if (inode == NULL)
147                 LBUG();
148         if (S_ISDIR(inode->i_mode)) {
149                 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
150                 invalidate_inode_pages(inode);
151         }
152
153         rc = ldlm_cli_cancel(lockh, NULL);
154         if (rc < 0) {
155                 CERROR("ldlm_cli_cancel: %d\n", rc);
156                 LBUG();
157         }
158         RETURN(0);
159 }
160
161 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
162                 struct lookup_intent *it, int lock_mode, struct inode *dir,
163                 struct dentry *de, struct lustre_handle *lockh, __u64 id,
164                 char *tgt, int tgtlen, void *data, int datalen)
165 {
166         struct ptlrpc_request *req;
167         struct obd_device *obddev = class_conn2obd(conn);
168         struct mdc_obd *mdc = mdc_conn2mdc(conn);
169         __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
170         int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
171         int rc, flags;
172         struct ldlm_reply *dlm_rep;
173         struct ldlm_intent *lit;
174         ENTRY;
175
176 #warning FIXME: Andreas, the sgid directory stuff also goes here, but check again on mds
177
178         LDLM_DEBUG_NOLOCK("mdsintent %d dir %ld", it->it_op, dir->i_ino);
179
180         switch (it->it_op) { 
181         case IT_MKDIR:
182                 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask; 
183                 break;
184         case IT_SETATTR:
185                 it->it_op = IT_GETATTR;
186                 break;
187         case (IT_CREAT|IT_OPEN):
188         case IT_CREAT:
189         case IT_MKNOD:
190                 it->it_mode = (it->it_mode | S_IFREG) & ~current->fs->umask; 
191                 break;
192         case IT_SYMLINK:
193                 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask; 
194                 break;
195         }
196
197         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
198                 size[2] = sizeof(struct mds_rec_create);
199                 size[3] = de->d_name.len + 1;
200                 size[4] = tgtlen + 1;
201                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
202                                        &mdc->mdc_connh, LDLM_ENQUEUE, 5, size,
203                                        NULL);
204                 if (!req)
205                         RETURN(-ENOMEM);
206
207                 /* pack the intent */
208                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
209                 lit->opc = NTOH__u64((__u64)it->it_op);
210
211                 /* pack the intended request */
212                 mds_create_pack(req, 2, dir, it->it_mode, id, current->fsuid,
213                                 current->fsgid, CURRENT_TIME, de->d_name.name,
214                                 de->d_name.len, tgt, tgtlen);
215
216                 size[0] = sizeof(struct ldlm_reply);
217                 size[1] = sizeof(struct mds_body);
218                 size[2] = sizeof(struct obdo);
219                 req->rq_replen = lustre_msg_size(3, size);
220         } else if (it->it_op == IT_RENAME2) {
221                 struct dentry *old_de = it->it_data;
222
223                 size[2] = sizeof(struct mds_rec_rename);
224                 size[3] = old_de->d_name.len + 1;
225                 size[4] = de->d_name.len + 1;
226                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
227                                        &mdc->mdc_connh, LDLM_ENQUEUE, 5, size,
228                                        NULL);
229                 if (!req)
230                         RETURN(-ENOMEM);
231
232                 /* pack the intent */
233                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
234                 lit->opc = NTOH__u64((__u64)it->it_op);
235
236                 /* pack the intended request */
237                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
238                                 old_de->d_name.name, old_de->d_name.len,
239                                 de->d_name.name, de->d_name.len);
240
241                 size[0] = sizeof(struct ldlm_reply);
242                 req->rq_replen = lustre_msg_size(1, size);
243         } else if (it->it_op == IT_UNLINK) {
244                 size[2] = sizeof(struct mds_rec_unlink);
245                 size[3] = de->d_name.len + 1;
246                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
247                                 &mdc->mdc_connh, LDLM_ENQUEUE, 4, size, NULL);
248                 if (!req)
249                         RETURN(-ENOMEM);
250
251                 /* pack the intent */
252                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
253                 lit->opc = NTOH__u64((__u64)it->it_op);
254
255                 /* pack the intended request */
256                 mds_unlink_pack(req, 2, dir, NULL, de->d_name.name, 
257                                 de->d_name.len);
258                 size[0] = sizeof(struct ldlm_reply);
259                 size[1] = sizeof(struct obdo);
260                 req->rq_replen = lustre_msg_size(2, size);
261         } else if (it->it_op == IT_RMDIR) {
262                 size[2] = sizeof(struct mds_rec_unlink);
263                 size[3] = de->d_name.len + 1;
264                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
265                                        &mdc->mdc_connh, LDLM_ENQUEUE, 4, size,
266                                        NULL);
267                 if (!req)
268                         RETURN(-ENOMEM);
269
270                 /* pack the intent */
271                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
272                 lit->opc = NTOH__u64((__u64)it->it_op);
273
274                 /* pack the intended request */
275                 mds_unlink_pack(req, 2, dir, NULL, de->d_name.name, 
276                                 de->d_name.len);
277                 size[0] = sizeof(struct ldlm_reply);
278                 req->rq_replen = lustre_msg_size(1, size);
279         } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
280                    it->it_op == IT_OPEN) {
281                 size[2] = sizeof(struct mds_body);
282                 size[3] = de->d_name.len + 1;
283
284                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
285                                        &mdc->mdc_connh, LDLM_ENQUEUE, 4, size,
286                                        NULL);
287                 if (!req)
288                         RETURN(-ENOMEM);
289
290                 /* pack the intent */
291                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
292                 lit->opc = NTOH__u64((__u64)it->it_op);
293
294                 /* pack the intended request */
295                 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
296
297                 /* get ready for the reply */
298                 size[0] = sizeof(struct ldlm_reply);
299                 size[1] = sizeof(struct mds_body);
300                 size[2] = sizeof(struct obdo);
301                 req->rq_replen = lustre_msg_size(3, size);
302         } else if (it->it_op == IT_SETATTR) {
303                 size[2] = sizeof(struct mds_rec_setattr);
304                 size[3] = de->d_name.len + 1;
305                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
306                                        &mdc->mdc_connh, LDLM_ENQUEUE, 5, size,
307                                        NULL);
308                 if (!req)
309                         RETURN(-ENOMEM);
310
311                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
312                 lit->opc = NTOH__u64((__u64)it->it_op);
313                 
314                 if (!it->it_iattr) 
315                         LBUG();
316
317                 mds_setattr_pack(req, 2, dir, it->it_iattr, 
318                                 de->d_name.name, de->d_name.len);
319                 size[0] = sizeof(struct ldlm_reply);
320                 size[1] = sizeof(struct mds_body);
321                 req->rq_replen = lustre_msg_size(2, size);
322         } else if (it->it_op == IT_READDIR) {
323                 req = ptlrpc_prep_req2(mdc->mdc_ldlm_client, mdc->mdc_conn,
324                                 &mdc->mdc_connh, LDLM_ENQUEUE, 1, size, NULL);
325                 if (!req)
326                         RETURN(-ENOMEM);
327
328                 /* get ready for the reply */
329                 size[0] = sizeof(struct ldlm_reply);
330                 req->rq_replen = lustre_msg_size(1, size);
331         } else {
332                 LBUG();
333                 RETURN(-1);
334         }
335 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
336         rc = ldlm_cli_enqueue(mdc->mdc_ldlm_client, mdc->mdc_conn, NULL, req,
337                               obddev->obd_namespace, NULL, res_id, lock_type,
338                               NULL, 0, lock_mode, &flags,
339                               (void *)mdc_lock_callback, data, datalen, lockh);
340         if (rc == -ENOENT || rc == ELDLM_LOCK_ABORTED) {
341                 lock_mode = 0;
342                 memset(lockh, 0, sizeof(*lockh));
343         } else if (rc != 0) {
344                 CERROR("ldlm_cli_enqueue: %d\n", rc);
345                 RETURN(rc);
346         }
347
348         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0); 
349         it->it_disposition = (int) dlm_rep->lock_policy_res1;
350         it->it_status = (int) dlm_rep->lock_policy_res2;
351         it->it_lock_mode = lock_mode;
352         it->it_data = req;
353
354         RETURN(0);
355 }
356
357 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
358              struct obdo *obdo,
359              __u64 cookie, __u64 *fh, struct ptlrpc_request **request)
360 {
361         struct mdc_obd *mdc = mdc_conn2mdc(conn);
362         struct mds_body *body;
363         int rc, size[2] = {sizeof(*body)}, bufcount = 1;
364         struct ptlrpc_request *req;
365         ENTRY;
366
367         if (obdo != NULL) {
368                 bufcount = 2;
369                 size[1] = sizeof(*obdo);
370         }
371
372         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
373                                MDS_OPEN, bufcount, size, NULL);
374         if (!req)
375                 GOTO(out, rc = -ENOMEM);
376
377         req->rq_flags |= PTL_RPC_FL_REPLAY;
378         body = lustre_msg_buf(req->rq_reqmsg, 0);
379
380         ll_ino2fid(&body->fid1, ino, 0, type);
381         body->flags = HTON__u32(flags);
382         body->extra = cookie;
383
384         if (obdo != NULL)
385                 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), obdo, sizeof(*obdo));
386
387         req->rq_replen = lustre_msg_size(1, size);
388
389         rc = ptlrpc_queue_wait(req);
390         rc = ptlrpc_check_status(req, rc);
391
392         if (!rc) {
393                 body = lustre_msg_buf(req->rq_repmsg, 0);
394                 mds_unpack_body(body);
395                 *fh = body->extra;
396         }
397
398         EXIT;
399  out:
400         *request = req;
401         return rc;
402 }
403
404 int mdc_close(struct lustre_handle *conn, 
405               obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
406 {
407         struct mdc_obd *mdc = mdc_conn2mdc(conn);
408         struct mds_body *body;
409         int rc, size = sizeof(*body);
410         struct ptlrpc_request *req;
411
412         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
413                                MDS_CLOSE, 1, &size, NULL);
414         if (!req)
415                 GOTO(out, rc = -ENOMEM);
416
417         body = lustre_msg_buf(req->rq_reqmsg, 0);
418         ll_ino2fid(&body->fid1, ino, 0, type);
419         body->extra = fh;
420
421         req->rq_replen = lustre_msg_size(0, NULL);
422
423         rc = ptlrpc_queue_wait(req);
424         rc = ptlrpc_check_status(req, rc);
425
426         EXIT;
427  out:
428         *request = req;
429         return rc;
430 }
431
432 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
433                  char *addr, struct ptlrpc_request **request)
434 {
435         struct mdc_obd *mdc = mdc_conn2mdc(conn);
436         struct ptlrpc_request *req = NULL;
437         struct ptlrpc_bulk_desc *desc = NULL;
438         struct ptlrpc_bulk_page *bulk = NULL;
439         struct mds_body *body;
440         int rc, size = sizeof(*body);
441         ENTRY;
442
443         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
444
445         desc = ptlrpc_prep_bulk(mdc->mdc_conn);
446         if (desc == NULL)
447                 GOTO(out, rc = -ENOMEM);
448
449         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
450                                MDS_READPAGE, 1, &size, NULL);
451         if (!req)
452                 GOTO(out2, rc = -ENOMEM);
453
454         bulk = ptlrpc_prep_bulk_page(desc);
455         bulk->b_buflen = PAGE_SIZE;
456         bulk->b_buf = addr;
457         bulk->b_xid = req->rq_xid;
458         desc->b_portal = MDS_BULK_PORTAL;
459
460         rc = ptlrpc_register_bulk(desc);
461         if (rc) {
462                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
463                 GOTO(out2, rc);
464         }
465
466         body = lustre_msg_buf(req->rq_reqmsg, 0);
467         body->fid1.id = ino;
468         body->fid1.f_type = type;
469         body->size = offset;
470
471         req->rq_replen = lustre_msg_size(1, &size);
472         rc = ptlrpc_queue_wait(req);
473         rc = ptlrpc_check_status(req, rc);
474         if (rc) {
475                 ptlrpc_abort_bulk(desc);
476                 GOTO(out2, rc);
477         } else { 
478                 body = lustre_msg_buf(req->rq_repmsg, 0);
479                 mds_unpack_body(body);
480         }
481
482         EXIT;
483  out2:
484         ptlrpc_free_bulk(desc);
485  out:
486         *request = req;
487         return rc;
488 }
489
490 int mdc_statfs(struct lustre_handle *conn, struct statfs *sfs,
491                struct ptlrpc_request **request)
492 {
493         struct mdc_obd *mdc = mdc_conn2mdc(conn);
494         struct obd_statfs *osfs;
495         struct ptlrpc_request *req;
496         int rc, size = sizeof(*osfs);
497         ENTRY;
498
499         req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, &mdc->mdc_connh,
500                                MDS_STATFS, 0, NULL, NULL);
501         if (!req)
502                 GOTO(out, rc = -ENOMEM);
503         req->rq_replen = lustre_msg_size(1, &size);
504
505         rc = ptlrpc_queue_wait(req);
506         rc = ptlrpc_check_status(req, rc);
507
508         if (rc)
509                 GOTO(out, rc);
510
511         osfs = lustre_msg_buf(req->rq_repmsg, 0);
512         obd_statfs_unpack(osfs, sfs);
513
514         EXIT;
515 out:
516         *request = req;
517
518         return rc;
519 }
520
521 static int mdc_ioctl(long cmd, struct lustre_handle *conn, int len, void *karg,
522                      void *uarg)
523 {
524 #if 0
525         /* FIXME XXX : This should use the new ioc_data to pass args in */
526         int err = 0;
527         struct ptlrpc_client cl;
528         struct ptlrpc_connection *conn;
529         struct ptlrpc_request *request;
530
531         ENTRY;
532
533         if (_IOC_TYPE(cmd) != IOC_REQUEST_TYPE ||
534             _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  ||
535             _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
536                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
537                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
538                 RETURN(-EINVAL);
539         }
540
541         ptlrpc_init_client(NULL, NULL, 
542                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL, &cl);
543         connection = ptlrpc_uuid_to_connection("mds");
544         if (!connection) {
545                 CERROR("cannot create client\n");
546                 RETURN(-EINVAL);
547         }
548
549         switch (cmd) {
550         case IOC_REQUEST_GETATTR: {
551                 CERROR("-- getting attr for ino %lu\n", arg);
552                 err = mdc_getattr(&cl, connection, (obd_id)arg, S_IFDIR, ~0, 0,
553                                   &request);
554                 CERROR("-- done err %d\n", err);
555
556                 GOTO(out, err);
557         }
558
559         case IOC_REQUEST_READPAGE: {
560                 char *buf;
561                 OBD_ALLOC(buf, PAGE_SIZE);
562                 if (!buf) {
563                         err = -ENOMEM;
564                         GOTO(out, err);
565                 }
566                 CERROR("-- readpage 0 for ino %lu\n", arg);
567                 err = mdc_readpage(&cl, connection, arg, S_IFDIR, 0, buf,
568                                    &request);
569                 CERROR("-- done err %d\n", err);
570                 OBD_FREE(buf, PAGE_SIZE);
571
572                 GOTO(out, err);
573         }
574
575         case IOC_REQUEST_SETATTR: {
576                 struct inode inode;
577                 struct iattr iattr;
578
579                 inode.i_ino = arg;
580                 inode.i_generation = 0;
581                 iattr.ia_mode = 040777;
582                 iattr.ia_atime = 0;
583                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
584
585                 err = mdc_setattr(&cl, connection, &inode, &iattr, &request);
586                 CERROR("-- done err %d\n", err);
587
588                 GOTO(out, err);
589         }
590
591         case IOC_REQUEST_CREATE: {
592                 struct inode inode;
593                 struct iattr iattr;
594
595                 inode.i_ino = arg;
596                 inode.i_generation = 0;
597                 iattr.ia_mode = 040777;
598                 iattr.ia_atime = 0;
599                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
600
601                 err = mdc_create(&cl, connection, &inode,
602                                  "foofile", strlen("foofile"),
603                                  NULL, 0, 0100707, 47114711,
604                                  11, 47, 0, NULL, &request);
605                 CERROR("-- done err %d\n", err);
606
607                 GOTO(out, err);
608         }
609
610         case IOC_REQUEST_OPEN: {
611                 __u64 fh, ino;
612                 copy_from_user(&ino, (__u64 *)arg, sizeof(ino));
613                 CERROR("-- opening ino %llu\n", (unsigned long long)ino);
614                 err = mdc_open(&cl, connection, ino, S_IFDIR, O_RDONLY, 4711,
615                                &fh, &request);
616                 copy_to_user((__u64 *)arg, &fh, sizeof(fh));
617                 CERROR("-- done err %d (fh=%Lu)\n", err,
618                        (unsigned long long)fh);
619
620                 GOTO(out, err);
621         }
622
623         case IOC_REQUEST_CLOSE: {
624                 CERROR("-- closing ino 2, filehandle %lu\n", arg);
625                 err = mdc_close(&cl, connection, 2, S_IFDIR, arg, &request);
626                 CERROR("-- done err %d\n", err);
627
628                 GOTO(out, err);
629         }
630
631         default:
632                 GOTO(out, err = -EINVAL);
633         }
634
635  out:
636         ptlrpc_free_req(request);
637         ptlrpc_put_connection(connection);
638         ptlrpc_cleanup_client(&cl);
639
640         RETURN(err);
641 #endif
642         return 0;
643 }
644
645 static int mdc_setup(struct obd_device *obddev, obd_count len, void *buf)
646 {
647         struct obd_ioctl_data* data = buf;
648         struct mdc_obd *mdc = &obddev->u.mdc;
649         char server_uuid[37];
650         int rc;
651         ENTRY;
652
653         if (data->ioc_inllen1 < 1) {
654                 CERROR("osc setup requires a TARGET UUID\n");
655                 RETURN(-EINVAL);
656         }
657
658         if (data->ioc_inllen1 > 37) {
659                 CERROR("mdc UUID must be less than 38 characters\n");
660                 RETURN(-EINVAL);
661         }
662
663         if (data->ioc_inllen2 < 1) {
664                 CERROR("mdc setup requires a SERVER UUID\n");
665                RETURN(-EINVAL);
666         }
667
668         if (data->ioc_inllen2 > 37) {
669                 CERROR("mdc UUID must be less than 38 characters\n");
670                 RETURN(-EINVAL);
671         }
672
673         memcpy(mdc->mdc_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
674         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
675                                                    sizeof(server_uuid)));
676
677         mdc->mdc_conn = ptlrpc_uuid_to_connection(server_uuid);
678         if (!mdc->mdc_conn)
679                 RETURN(-ENOENT); 
680
681         OBD_ALLOC(mdc->mdc_client, sizeof(*mdc->mdc_client));
682         if (mdc->mdc_client == NULL)
683                 GOTO(out_conn, rc = -ENOMEM);
684
685         OBD_ALLOC(mdc->mdc_ldlm_client, sizeof(*mdc->mdc_ldlm_client));
686         if (mdc->mdc_ldlm_client == NULL)
687                 GOTO(out_client, rc = -ENOMEM);
688
689         ptlrpc_init_client(NULL, NULL, MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
690                            mdc->mdc_client);
691         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
692                            mdc->mdc_ldlm_client);
693         mdc->mdc_client->cli_name = "mdc";
694         mdc->mdc_ldlm_client->cli_name = "ldlm";
695         /* XXX get recovery hooked in here again */
696         //ptlrpc_init_client(ptlrpc_connmgr, ll_recover,...
697
698         ptlrpc_init_client(ptlrpc_connmgr, NULL,
699                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
700                            mdc->mdc_client);
701
702         MOD_INC_USE_COUNT;
703         RETURN(0);
704
705  out_client:
706         OBD_FREE(mdc->mdc_client, sizeof(*mdc->mdc_client));
707  out_conn:
708         ptlrpc_put_connection(mdc->mdc_conn);
709         return rc;
710 }
711
712 static int mdc_cleanup(struct obd_device * obddev)
713 {
714         struct mdc_obd *mdc = &obddev->u.mdc;
715
716         ptlrpc_cleanup_client(mdc->mdc_client);
717         OBD_FREE(mdc->mdc_client, sizeof(*mdc->mdc_client));
718         ptlrpc_cleanup_client(mdc->mdc_ldlm_client);
719         OBD_FREE(mdc->mdc_ldlm_client, sizeof(*mdc->mdc_ldlm_client));
720         ptlrpc_put_connection(mdc->mdc_conn);
721
722         MOD_DEC_USE_COUNT;
723         return 0;
724 }
725
726 static int mdc_connect(struct lustre_handle *conn, struct obd_device *obd)
727 {
728         struct mdc_obd *mdc = &obd->u.mdc;
729         struct ptlrpc_request *request;
730         int rc, size = sizeof(mdc->mdc_target_uuid);
731         char *tmp = mdc->mdc_target_uuid;
732
733         ENTRY;
734
735         obd->obd_namespace =
736                 ldlm_namespace_new("mdc", LDLM_NAMESPACE_CLIENT);
737         if (obd->obd_namespace == NULL)
738                 RETURN(-ENOMEM);
739
740         MOD_INC_USE_COUNT;
741         rc = class_connect(conn, obd);
742         if (rc) 
743                 RETURN(rc); 
744
745         request = ptlrpc_prep_req(mdc->mdc_client, mdc->mdc_conn,
746                                   MDS_CONNECT, 1, &size, &tmp);
747         if (!request)
748                 GOTO(out_disco, -ENOMEM);
749
750         request->rq_replen = lustre_msg_size(0, NULL);
751
752         rc = ptlrpc_queue_wait(request);
753         rc = ptlrpc_check_status(request, rc);
754         if (rc)
755                 GOTO(out, rc);
756
757         mdc->mdc_connh.addr = request->rq_repmsg->addr;
758         mdc->mdc_connh.cookie = request->rq_repmsg->cookie;
759
760         EXIT;
761  out:
762         ptlrpc_free_req(request);
763  out_disco:
764         if (rc) {
765                 class_disconnect(conn);
766                 MOD_DEC_USE_COUNT;
767         }
768         return rc;
769 }
770
771 static int mdc_disconnect(struct lustre_handle *conn)
772 {
773         struct mdc_obd *mdc = mdc_conn2mdc(conn);
774         struct obd_device *obd = class_conn2obd(conn);
775         struct ptlrpc_request *request;
776         int rc;
777         ENTRY;
778
779         ldlm_namespace_free(obd->obd_namespace);
780         request = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, 
781                                    &mdc->mdc_connh, MDS_DISCONNECT, 0, NULL, NULL);
782         if (!request)
783                 RETURN(-ENOMEM);
784
785         request->rq_replen = lustre_msg_size(0, NULL);
786
787         rc = ptlrpc_queue_wait(request);
788         if (rc) 
789                 GOTO(out, rc);
790         rc = class_disconnect(conn);
791         if (!rc)
792                 MOD_DEC_USE_COUNT;
793  out:
794         ptlrpc_free_req(request);
795         return rc;
796 }
797
798 struct obd_ops mdc_obd_ops = {
799         o_setup:   mdc_setup,
800         o_cleanup: mdc_cleanup,
801         o_connect: mdc_connect,
802         o_disconnect: mdc_disconnect,
803         o_iocontrol: mdc_ioctl
804 };
805
806 static int __init ptlrpc_request_init(void)
807 {
808         return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
809 }
810
811 static void __exit ptlrpc_request_exit(void)
812 {
813         class_unregister_type(LUSTRE_MDC_NAME);
814 }
815
816 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
817 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
818 MODULE_LICENSE("GPL");
819
820 EXPORT_SYMBOL(mdc_getstatus);
821 EXPORT_SYMBOL(mdc_enqueue);
822 EXPORT_SYMBOL(mdc_getattr);
823 EXPORT_SYMBOL(mdc_statfs);
824 EXPORT_SYMBOL(mdc_create);
825 EXPORT_SYMBOL(mdc_unlink);
826 EXPORT_SYMBOL(mdc_rename);
827 EXPORT_SYMBOL(mdc_link);
828 EXPORT_SYMBOL(mdc_readpage);
829 EXPORT_SYMBOL(mdc_setattr);
830 EXPORT_SYMBOL(mdc_close);
831 EXPORT_SYMBOL(mdc_open);
832
833 module_init(ptlrpc_request_init);
834 module_exit(ptlrpc_request_exit);