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