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