Whamcloud - gitweb
Land b_hd_capa onto HEAD (20050809_1942)
[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-2004 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 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lustre_sec.h>
40 #include <linux/lprocfs_status.h>
41 #include <linux/lustre_acl.h>
42 #include <linux/lustre_lite.h>
43 #include <linux/lustre_gs.h>
44 #include "mdc_internal.h"
45
46 #define REQUEST_MINOR 244
47
48 static int mdc_cleanup(struct obd_device *obd, int flags);
49
50 extern int mds_queue_req(struct ptlrpc_request *);
51 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
52 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
53 static int send_getstatus(struct obd_import *imp, struct lustre_id *rootid,
54                           int level, int msg_flags)
55 {
56         struct ptlrpc_request *req;
57         struct mds_body *body;
58         int rc, size[2] = {0, sizeof(*body)};
59         ENTRY;
60
61         //size[0] = lustre_secdesc_size();
62
63         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS,
64                               2, size, NULL);
65         if (!req)
66                 GOTO(out, rc = -ENOMEM);
67
68         //lustre_pack_secdesc(req, size[0]);
69
70         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
71         req->rq_send_state = level;
72         req->rq_replen = lustre_msg_size(1, &size[1]);
73
74         req->rq_reqmsg->flags |= msg_flags;
75         rc = ptlrpc_queue_wait(req);
76
77         if (!rc) {
78                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
79                                            lustre_swab_mds_body);
80                 if (body == NULL) {
81                         CERROR ("Can't extract mds_body\n");
82                         GOTO (out, rc = -EPROTO);
83                 }
84
85                 memcpy(rootid, &body->id1, sizeof(*rootid));
86
87                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
88                        ", last_xid="LPU64"\n", rootid->li_stc.u.e3s.l3s_ino,
89                        req->rq_repmsg->last_committed, req->rq_repmsg->last_xid);
90         }
91
92         EXIT;
93  out:
94         ptlrpc_req_finished(req);
95         return rc;
96 }
97
98 /* This should be mdc_get_info("rootid") */
99 int mdc_getstatus(struct obd_export *exp, struct lustre_id *rootid)
100 {
101         return send_getstatus(class_exp2cliimp(exp), rootid,
102                               LUSTRE_IMP_FULL, 0);
103 }
104
105 int
106 mdc_interpret_getattr(struct ptlrpc_request *req, void *unused, int rc)
107 {
108         struct mds_body *body = NULL;
109         struct lustre_capa *capa = NULL;
110         unsigned long expiry;
111         ENTRY;
112
113         if (rc) {
114                 DEBUG_REQ(D_ERROR, req,
115                           "async getattr failed: rc = %d", rc);
116                 RETURN(rc);
117         }
118
119         body = lustre_swab_repbuf(req, 0, sizeof (*body), lustre_swab_mds_body);
120         if (body == NULL) {
121                 CERROR ("Can't unpack mds_body\n");
122                 RETURN(-EPROTO);
123         }
124
125         if (!(body->valid & OBD_MD_CAPA)) {
126                 CDEBUG(D_INFO, "MDS has disabled capability\n");
127                 RETURN(0);
128         }
129
130         capa = lustre_swab_repbuf(req, 1, sizeof(*capa),
131                                   lustre_swab_lustre_capa);
132         if (capa == NULL && rc != 0) {
133                 CERROR ("Can't unpack lustre_capa\n");
134                 RETURN(-EPROTO);
135         }
136
137         rc = capa_renew(capa, CLIENT_CAPA);
138         if (rc)
139                 RETURN(rc);
140
141         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
142         if (time_before(expiry, ll_capa_timer.expires) ||
143             !timer_pending(&ll_capa_timer))
144                 mod_timer(&ll_capa_timer, expiry);
145
146         RETURN(rc);
147 }
148
149 int mdc_getattr_async(struct obd_export *exp, struct ptlrpc_request *req)
150 {
151         int repsize[2] = {sizeof(struct mds_body), sizeof(struct lustre_capa)};
152         ENTRY;
153
154         req->rq_replen = lustre_msg_size(1, repsize);
155         req->rq_interpret_reply = mdc_interpret_getattr;
156         ptlrpcd_add_req(req);
157
158         RETURN (0);
159 }
160
161 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
162                        struct ptlrpc_request *req)
163 {
164         struct mds_body *body, *reqbody;
165         void            *eadata;
166         int              rc;
167         int              repsize[2] = {sizeof(*body)};
168         int              bufcount = 1;
169         ENTRY;
170
171         /* request message already built */
172
173         if (ea_size != 0) {
174                 repsize[bufcount++] = ea_size;
175                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
176                        ea_size);
177         }
178
179         reqbody = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*reqbody));
180         LASSERT(!(reqbody->valid & OBD_MD_FLACL));
181
182         if (reqbody->valid & OBD_MD_FLKEY) {
183                 repsize[bufcount++] = 5;
184                 repsize[bufcount++] = sizeof(struct lustre_key);
185         } else if (reqbody->valid & OBD_MD_CAPA) {
186                 LASSERT(ea_size == 0);
187                 repsize[bufcount++] = sizeof(struct lustre_capa);
188         }
189
190         req->rq_replen = lustre_msg_size(bufcount, repsize);
191
192         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
193         rc = ptlrpc_queue_wait(req);
194         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
195         if (rc != 0)
196                 RETURN (rc);
197
198         body = lustre_swab_repbuf (req, 0, sizeof (*body),
199                                    lustre_swab_mds_body);
200         if (body == NULL) {
201                 CERROR ("Can't unpack mds_body\n");
202                 RETURN (-EPROTO);
203         }
204
205         CDEBUG(D_NET, "mode: %o\n", body->mode);
206
207         LASSERT_REPSWAB (req, 1);
208
209         /* Skip the check if getxattr/listxattr are called with no buffers */
210         if ((reqbody->eadatasize != 0) &&
211             !(reqbody->valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLIST))) {
212                 /* reply indicates presence of eadata; check it's there... */
213                 eadata = lustre_msg_buf (req->rq_repmsg, 1,
214                                          body->eadatasize);
215                 if (eadata == NULL) {
216                         CERROR ("Missing/short eadata\n");
217                         RETURN (-EPROTO);
218                 }
219         }
220
221         RETURN (0);
222 }
223
224 static int mdc_cancel_unused(struct obd_export *exp,
225                              struct lov_stripe_md *lsm, 
226                              int flags, void *opaque)
227 {
228         struct obd_device *obd = class_exp2obd(exp);
229
230         ENTRY;
231         RETURN(ldlm_cli_cancel_unused(obd->obd_namespace,
232                                       NULL, flags, opaque));
233 }
234
235 int mdc_getattr(struct obd_export *exp, struct lustre_id *id,
236                 __u64 valid, const char *xattr_name,
237                 const void *xattr_data, unsigned int xattr_datalen,
238                 unsigned int ea_size, struct obd_capa *ocapa,
239                 struct ptlrpc_request **request)
240 {
241         struct ptlrpc_request *req;
242         struct mds_body *body;
243         int xattr_namelen = xattr_name ? strlen(xattr_name) + 1 : 0;
244         int size[4] = {0, sizeof(*body)};
245         int bufcount = 2;
246         int rc;
247         ENTRY;
248
249         size[0] = lustre_secdesc_size();
250
251         if (valid & OBD_MD_FLXATTR) {
252                 size[bufcount++] = xattr_namelen;
253
254                 if (xattr_datalen > 0) {
255                         LASSERT(xattr_data);
256                         size[bufcount++] = xattr_datalen;
257                 }
258         } else if (valid & OBD_MD_CAPA) {
259                 LASSERT(valid  == OBD_MD_CAPA);
260                 LASSERT(ocapa);
261                 size[bufcount++] = sizeof(*ocapa);
262         } else {
263                 LASSERT(!xattr_data && !xattr_datalen);
264         }
265
266         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
267                               MDS_GETATTR, bufcount, size, NULL);
268         if (!req)
269                 GOTO(out, rc = -ENOMEM);
270
271         lustre_pack_secdesc(req, size[0]);
272
273         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
274         memcpy(&body->id1, id, sizeof(*id));
275         body->valid = valid;
276         body->eadatasize = ea_size;
277
278         if (valid & OBD_MD_FLXATTR) {
279                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, xattr_namelen),
280                        xattr_name, xattr_namelen);
281                 if (xattr_datalen)
282                         memcpy(lustre_msg_buf(req->rq_reqmsg, 3, xattr_datalen),
283                                xattr_data, xattr_datalen);
284         }
285
286         if (valid & OBD_MD_CAPA) {
287                 /* renew capability */
288                 memcpy(&body->handle, &ocapa->c_handle, sizeof(body->handle));
289                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, sizeof(ocapa->c_capa)),
290                        &ocapa->c_capa, sizeof(ocapa->c_capa));
291
292                 rc = mdc_getattr_async(exp, req);
293                 req = NULL;     /* ptlrpcd will finish request */
294         } else {
295                 rc = mdc_getattr_common(exp, ea_size, req);
296                 if (rc != 0) {
297                         ptlrpc_req_finished (req);
298                         req = NULL;
299                 }
300         }
301  out:
302         *request = req;
303         RETURN (rc);
304 }
305
306 int mdc_access_check(struct obd_export *exp, struct lustre_id *id,
307                      struct ptlrpc_request **request)
308
309 {
310         struct ptlrpc_request *req;
311         struct mds_body *body;
312         int size[2] = {0, sizeof(*body)};
313         int rc;
314         ENTRY;
315
316         size[0] = lustre_secdesc_size();
317         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
318                               MDS_ACCESS_CHECK, 2, size, NULL);
319         if (!req)
320                 GOTO(out, rc = -ENOMEM);
321
322         lustre_pack_secdesc(req, size[0]);
323         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
324         memcpy(&body->id1, id, sizeof(*id));
325
326         req->rq_replen = lustre_msg_size(2, size);
327         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
328         rc = ptlrpc_queue_wait(req);
329         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
330         if (rc != 0) {
331                 ptlrpc_req_finished (req);
332                 req = NULL;
333         } else {
334                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
335                                            lustre_swab_mds_body);
336                 if (body == NULL) {
337                         CERROR ("Can't unpack mds_body\n");
338                         RETURN (-EPROTO);
339                 }
340         }
341
342  out:
343         *request = req;
344         RETURN (rc);
345 }
346
347 int mdc_getattr_lock(struct obd_export *exp, struct lustre_id *id,
348                      char *filename, int namelen, __u64 valid,
349                      unsigned int ea_size, struct ptlrpc_request **request)
350 {
351         struct ptlrpc_request *req;
352         struct mds_body *body;
353         int rc, size[3] = {0, sizeof(*body), namelen};
354         ENTRY;
355
356         size[0] = lustre_secdesc_size();
357
358         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
359                               MDS_GETATTR_LOCK, 3, size, NULL);
360         if (!req)
361                 GOTO(out, rc = -ENOMEM);
362
363         lustre_pack_secdesc(req, size[0]);
364
365         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
366         memcpy(&body->id1, id, sizeof(*id));
367         body->valid = valid;
368         body->eadatasize = ea_size;
369
370         if (filename != NULL) {
371                 LASSERT (strnlen (filename, namelen) == namelen - 1);
372                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, namelen),
373                        filename, namelen);
374         } else {
375                 LASSERT(namelen == 1);
376         }
377
378         rc = mdc_getattr_common(exp, ea_size, req);
379         if (rc != 0) {
380                 ptlrpc_req_finished (req);
381                 req = NULL;
382         }
383  out:
384         *request = req;
385         RETURN(rc);
386 }
387
388 /* This should be called with both the request and the reply still packed. */
389 int mdc_store_inode_generation(struct obd_export *exp,
390                                struct ptlrpc_request *req,
391                                int reqoff, int repoff)
392 {
393         struct mds_rec_create *rec =
394                 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
395         struct mds_body *body =
396                 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
397
398         LASSERT (rec != NULL);
399         LASSERT (body != NULL);
400
401         memcpy(&rec->cr_replayid, &body->id1, sizeof(rec->cr_replayid));
402         DEBUG_REQ(D_HA, req, "storing generation for ino "DLID4,
403                   OLID4(&rec->cr_replayid));
404         return 0;
405 }
406
407 int mdc_req2lustre_capa(struct ptlrpc_request *req, unsigned int offset,
408                         struct lustre_capa **capa)
409 {
410         struct mds_body *body;
411         struct lustre_capa *lcapa;
412
413         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
414         if (!body)
415                 RETURN(-ENOMEM);
416
417         if (!(body->valid & OBD_MD_CAPA)) {
418                 *capa = NULL;
419                 RETURN(0);
420         }
421
422         lcapa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
423                                         sizeof(*capa), lustre_swab_lustre_capa);
424         if (!lcapa)
425                 RETURN(-ENOMEM);
426                 
427         OBD_ALLOC(*capa, sizeof(**capa));
428         if (!*capa)
429                 RETURN(-ENOMEM);
430         memcpy(*capa, lcapa, sizeof(**capa));
431
432         RETURN(0);
433 }
434
435 static int mdc_unpack_acl(struct obd_export *exp_lmv, struct ptlrpc_request *req, 
436                           unsigned int *offset, struct lustre_md *md)
437 {
438         struct posix_acl *acl;
439         struct mds_remote_perm *perm;
440         int    size, acl_off = 0, rc = 0;       
441         void   *buf;
442  
443         /* if anything wrong when unpacking md, we don't check acl
444          * stuff, for simplicity
445          */
446         if (md->body->valid & OBD_MD_FLACL) {
447                 acl_off = *offset;
448                 if (md->body->valid & OBD_MD_FLRMTACL) {
449                         acl_off++; /* XXX: pass place where acl size is stored */
450                         buf = lustre_swab_repbuf(req, acl_off++, sizeof(*perm),
451                                                  lustre_swab_remote_perm);
452                         if (buf == NULL) {
453                                 CERROR("Can't unpack remote perm\n");
454                                 RETURN(0);
455                         }
456                         OBD_ALLOC(perm, sizeof(*perm));
457                         if (!perm)
458                                 RETURN(-ENOMEM);
459                         memcpy(perm, buf, sizeof(*perm));
460                         md->remote_perm = perm;
461                         *offset = acl_off; 
462                 } else {
463                         size = le32_to_cpu(*(__u32 *) lustre_msg_buf(
464                                            req->rq_repmsg, acl_off++, 4));
465                         buf = lustre_msg_buf(req->rq_repmsg, acl_off++, size);
466
467                         acl = posix_acl_from_xattr(buf, size);
468                         *offset = acl_off;
469                         if (IS_ERR(acl)) {
470                                 rc = PTR_ERR(acl);
471                                 CERROR("convert xattr to acl failed: %d\n", rc);
472                                 RETURN(0);
473                         } else if (acl) {
474                                 rc = posix_acl_valid(acl);
475                                 if (rc) {
476                                         CERROR("acl valid error: %d\n", rc);
477                                         posix_acl_release(acl);
478                                         RETURN(0);
479                                 }
480                         }
481                         md->posix_acl = acl;
482                 }
483         }
484         RETURN(rc);
485 }
486
487 static int mdc_unpack_gskey(struct obd_export *exp_lmv, struct ptlrpc_request *req, 
488                             unsigned int *offset, struct lustre_md *md)
489
490         int key_off = 0, rc = 0, size = 0;
491         void *buf;
492         
493         key_off = *offset;
494         if (md->body->valid & OBD_MD_FLKEY) {
495                 size = le32_to_cpu(*(__u32 *) lustre_msg_buf(req->rq_repmsg, 
496                                    key_off++, 4));
497                 buf = lustre_msg_buf(req->rq_repmsg, key_off++, size);
498                 
499                 CDEBUG(D_INFO, "buf %p key_off %d size %d \n", 
500                        buf, key_off, size);
501                 md->key = (struct lustre_key *)buf; 
502                 *offset = key_off; 
503         }  
504         RETURN(rc);
505 }
506
507 int mdc_req2lustre_md(struct obd_export *exp_lmv, struct ptlrpc_request *req, 
508                       unsigned int offset, struct obd_export *exp_lov, 
509                       struct lustre_md *md)
510 {
511         struct lov_mds_md *lmm;
512         int rc = 0, reply_off;
513         ENTRY;
514
515         LASSERT(md != NULL);
516         memset(md, 0, sizeof(*md));
517
518         md->body = lustre_msg_buf(req->rq_repmsg, offset,
519                                   sizeof(*md->body));
520         if (!md->body)
521                 RETURN(-ENOMEM);
522
523         LASSERT_REPSWABBED(req, offset);
524
525         if (!(md->body->valid & OBD_MD_FLEASIZE) &&
526             !(md->body->valid & OBD_MD_FLDIREA))
527                 RETURN(0);
528
529         if (S_ISREG(md->body->mode)) {
530                 if (md->body->eadatasize == 0) {
531                         CERROR("invalid EA size (0) is detected\n");
532                         RETURN(-EPROTO);
533                 }
534
535                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1,
536                                      md->body->eadatasize);
537                 if (!lmm)
538                         RETURN(-EINVAL);
539
540                 LASSERT(exp_lov != NULL);
541                 
542                 rc = obd_unpackmd(exp_lov, &md->lsm, lmm,
543                                   md->body->eadatasize);
544                 if (rc > 0) {
545                         LASSERT(rc >= sizeof(*md->lsm));
546                         rc = 0;
547                 }
548         } else if (S_ISDIR(md->body->mode)) {
549                 /* dir can be non-splitted */
550                 if (md->body->eadatasize == 0)
551                         RETURN(0);
552
553                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1,
554                                      md->body->eadatasize);
555                 if (!lmm)
556                         RETURN(-EINVAL);
557
558                 if (md->body->valid & OBD_MD_MEA) {
559                         LASSERT(exp_lmv != NULL);
560                 
561                         rc = obd_unpackmd(exp_lmv, (void *)&md->mea,
562                                           lmm, md->body->eadatasize);
563                         if (rc > 0) {
564                                 LASSERT(rc >= sizeof(*md->mea));
565                                 rc = 0;
566                         }
567                 }
568         } else {
569                 LASSERT(S_ISCHR(md->body->mode) ||
570                         S_ISBLK(md->body->mode) ||
571                         S_ISFIFO(md->body->mode)||
572                         S_ISLNK(md->body->mode) ||
573                         S_ISSOCK(md->body->mode));
574         }
575
576         reply_off = (md->body->valid & OBD_MD_FLEASIZE) ?
577                                 (offset + 2) : (offset + 1);
578         rc = mdc_unpack_acl(exp_lmv, req, &reply_off, md);
579         if (rc) {
580                 CERROR("upack acl error %d \n", rc);
581                 RETURN(rc);
582         }
583         
584         rc = mdc_unpack_gskey(exp_lmv, req, &reply_off, md);
585         if (rc)
586                 RETURN(rc);
587
588         RETURN(rc);
589 }
590
591 static void mdc_commit_open(struct ptlrpc_request *req)
592 {
593         struct mdc_open_data *mod = req->rq_cb_data;
594         if (mod == NULL)
595                 return;
596
597         if (mod->mod_close_req != NULL)
598                 mod->mod_close_req->rq_cb_data = NULL;
599
600         if (mod->mod_och != NULL)
601                 mod->mod_och->och_mod = NULL;
602
603         OBD_FREE(mod, sizeof(*mod));
604         req->rq_cb_data = NULL;
605         LASSERT(atomic_read(&req->rq_refcount) > 1);
606         ptlrpc_req_finished(req);
607 }
608
609 static void mdc_replay_open(struct ptlrpc_request *req)
610 {
611         struct mdc_open_data *mod = req->rq_cb_data;
612         struct obd_client_handle *och;
613         struct ptlrpc_request *close_req;
614         struct lustre_handle old;
615         struct mds_body *body;
616         ENTRY;
617
618         body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
619         LASSERT (body != NULL);
620
621         if (mod == NULL) {
622                 DEBUG_REQ(D_ERROR, req,
623                           "can't properly replay without open data");
624                 EXIT;
625                 return;
626         }
627
628         och = mod->mod_och;
629         if (och != NULL) {
630                 struct lustre_handle *file_fh;
631                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
632                 file_fh = &och->och_fh;
633                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
634                        file_fh->cookie, body->handle.cookie);
635                 memcpy(&old, file_fh, sizeof(old));
636                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
637         }
638
639         close_req = mod->mod_close_req;
640         if (close_req != NULL) {
641                 struct mds_body *close_body;
642                 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
643                 close_body = lustre_msg_buf(close_req->rq_reqmsg,
644                                             MDS_REQ_REC_OFF,
645                                             sizeof(*close_body));
646                 if (och != NULL)
647                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
648                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
649                 memcpy(&close_body->handle, &body->handle,
650                        sizeof(close_body->handle));
651         }
652
653         EXIT;
654 }
655
656 int mdc_set_open_replay_data(struct obd_export *exp,
657                              struct obd_client_handle *och,
658                              struct ptlrpc_request *open_req)
659 {
660         struct mdc_open_data *mod;
661         struct mds_rec_create *rec;
662         struct mds_body *body;
663
664         rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
665                              sizeof(*rec));
666         body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
667
668         LASSERT(rec != NULL);
669         /* outgoing messages always in my byte order */
670         LASSERT(body != NULL);
671         /* incoming message in my byte order (it's been swabbed) */
672         LASSERT_REPSWABBED(open_req, 1);
673
674         OBD_ALLOC(mod, sizeof(*mod));
675         if (mod == NULL) {
676                 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
677                 return 0;
678         }
679
680         och->och_mod = mod;
681         mod->mod_och = och;
682         mod->mod_open_req = ptlrpc_request_addref(open_req);
683
684         memcpy(&rec->cr_replayid, &body->id1, sizeof rec->cr_replayid);
685         open_req->rq_replay_cb = mdc_replay_open;
686         open_req->rq_commit_cb = mdc_commit_open;
687         open_req->rq_cb_data = mod;
688         DEBUG_REQ(D_HA, open_req, "set up replay data");
689         return 0;
690 }
691
692 int mdc_clear_open_replay_data(struct obd_export *exp,
693                                struct obd_client_handle *och)
694 {
695         struct mdc_open_data *mod = och->och_mod;
696
697         /* Don't free the structure now (it happens in mdc_commit_open, after
698          * we're sure we won't need to fix up the close request in the future),
699          * but make sure that replay doesn't poke at the och, which is about to
700          * be freed. */
701         LASSERT(mod != LP_POISON);
702         if (mod != NULL)
703                 mod->mod_och = NULL;
704         och->och_mod = NULL;
705         return 0;
706 }
707
708 static void mdc_commit_close(struct ptlrpc_request *req)
709 {
710         struct mdc_open_data *mod = req->rq_cb_data;
711         struct obd_import *imp = req->rq_import;
712         struct ptlrpc_request *open_req;
713
714         DEBUG_REQ(D_HA, req, "close req committed");
715         if (mod == NULL)
716                 return;
717
718         mod->mod_close_req = NULL;
719         req->rq_cb_data = NULL;
720         req->rq_commit_cb = NULL;
721
722         open_req = mod->mod_open_req;
723         LASSERT(open_req != NULL);
724         LASSERT(open_req != LP_POISON);
725         LASSERT(open_req->rq_type != LI_POISON);
726
727         DEBUG_REQ(D_HA, open_req, "open req balanced");
728         if (open_req->rq_transno == 0) {
729                 DEBUG_REQ(D_ERROR, open_req, "BUG 3892  open");
730                 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
731                 LASSERTF(open_req->rq_transno != 0, "BUG 3892\n");
732         }
733         LASSERT(open_req->rq_import == imp);
734
735         /* We no longer want to preserve this for transno-unconditional
736          * replay. */
737         spin_lock(&open_req->rq_lock);
738         open_req->rq_replay = 0;
739         spin_unlock(&open_req->rq_lock);
740 }
741
742 int mdc_close(struct obd_export *exp, struct obdo *oa,
743               struct obd_client_handle *och,
744               struct ptlrpc_request **request)
745 {
746         struct obd_device *obd = class_exp2obd(exp);
747         struct obd_import *imp = class_exp2cliimp(exp);
748         int reqsize[3] = {0, sizeof(struct mds_body),
749                           obd->u.cli.cl_max_mds_cookiesize};
750         int rc, repsize[3] = {sizeof(struct mds_body),
751                               obd->u.cli.cl_max_mds_easize,
752                               obd->u.cli.cl_max_mds_cookiesize};
753         struct ptlrpc_request *req;
754         struct mdc_open_data *mod;
755         ENTRY;
756
757         if (imp->imp_connection == NULL) {
758                 CERROR("request on not connected import %s\n",
759                         imp->imp_obd->obd_name);
760                 RETURN(-EIO);
761         }
762
763         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
764                               MDS_CLOSE, 3, reqsize, NULL);
765         if (req == NULL)
766                 GOTO(out, rc = -ENOMEM);
767         req->rq_request_portal = MDS_CLOSE_PORTAL;
768
769         //reqsize[0] = lustre_secdesc_size();
770         //lustre_pack_secdesc(req, reqsize[0]);
771
772         /* Ensure that this close's handle is fixed up during replay. */
773         LASSERT(och != NULL);
774         mod = och->och_mod;
775         if (likely(mod != NULL)) {
776                 mod->mod_close_req = req;
777                 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
778                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
779         } else {
780                 CDEBUG(D_HA, "couldn't find open req; "
781                        "expecting close error\n");
782         }
783
784         mdc_close_pack(req, 1, oa, oa->o_valid, och);
785
786         req->rq_replen = lustre_msg_size(3, repsize);
787         req->rq_commit_cb = mdc_commit_close;
788         LASSERT(req->rq_cb_data == NULL);
789         req->rq_cb_data = mod;
790
791         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
792         rc = ptlrpc_queue_wait(req);
793         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
794
795         if (req->rq_repmsg == NULL) {
796                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
797                        req->rq_status);
798                 if (rc == 0)
799                         rc = req->rq_status ? req->rq_status : -EIO;
800         } else if (rc == 0) {
801                 rc = req->rq_repmsg->status;
802                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
803                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, "
804                                   "err = %d", rc);
805                         if (rc > 0)
806                                 rc = -rc;
807                 } else {
808                         if (mod == NULL)
809                                 CERROR("Unexpected: can't find mdc_open_data, but "
810                                        "close succeeded. Please tell CFS.\n");
811                         if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
812                                                 lustre_swab_mds_body))
813                         {
814                                 CERROR("Error unpacking mds_body\n");
815                                 rc = -EPROTO;
816                         }
817                 }
818         }
819
820         EXIT;
821  out:
822         *request = req;
823         return rc;
824 }
825
826 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
827 {
828         struct ptlrpc_request *req;
829         struct mds_body *body;
830         int rc, size[2] = {0, sizeof(*body)};
831         ENTRY;
832
833         size[0] = lustre_secdesc_size();
834
835         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
836                               MDS_DONE_WRITING, 2, size, NULL);
837         if (req == NULL)
838                 RETURN(-ENOMEM);
839
840         lustre_pack_secdesc(req, size[0]);
841
842         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, 
843                               sizeof(*body));
844         
845         mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode, 
846                     obdo->o_mds, obdo->o_fid);
847         
848         body->size = obdo->o_size;
849         body->blocks = obdo->o_blocks;
850         body->flags = obdo->o_flags;
851         body->valid = obdo->o_valid;
852
853         req->rq_replen = lustre_msg_size(1, &size[1]);
854
855         rc = ptlrpc_queue_wait(req);
856         ptlrpc_req_finished(req);
857         RETURN(rc);
858 }
859
860 int mdc_readpage(struct obd_export *exp,
861                  struct lustre_id *id,
862                  __u64 offset, struct page *page,
863                  struct ptlrpc_request **request)
864 {
865         struct obd_import *imp = class_exp2cliimp(exp);
866         struct ptlrpc_request *req = NULL;
867         struct ptlrpc_bulk_desc *desc = NULL;
868         struct mds_body *body;
869         int rc, size[2] = {0, sizeof(*body)};
870         ENTRY;
871
872         CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
873
874         size[0] = lustre_secdesc_size();
875
876         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
877                               2, size, NULL);
878         if (req == NULL)
879                 GOTO(out, rc = -ENOMEM);
880         /* XXX FIXME bug 249 */
881         req->rq_request_portal = MDS_READPAGE_PORTAL;
882
883         lustre_pack_secdesc(req, size[0]);
884
885         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
886         if (desc == NULL)
887                 GOTO(out, rc = -ENOMEM);
888         /* NB req now owns desc and will free it when it gets freed */
889
890         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
891         mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
892
893         req->rq_replen = lustre_msg_size(1, &size[1]);
894         rc = ptlrpc_queue_wait(req);
895
896         if (rc == 0) {
897                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
898                                           lustre_swab_mds_body);
899                 if (body == NULL) {
900                         CERROR("Can't unpack mds_body\n");
901                         GOTO(out, rc = -EPROTO);
902                 }
903
904                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
905                         CERROR ("Unexpected # bytes transferred: %d"
906                                 " (%ld expected)\n",
907                                 req->rq_bulk->bd_nob_transferred,
908                                 PAGE_CACHE_SIZE);
909                         GOTO (out, rc = -EPROTO);
910                 }
911         }
912
913         EXIT;
914  out:
915         *request = req;
916         return rc;
917 }
918
919 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
920                          void *karg, void *uarg)
921 {
922         struct obd_device *obd = exp->exp_obd;
923         struct obd_ioctl_data *data = karg;
924         struct obd_import *imp = obd->u.cli.cl_import;
925         struct llog_ctxt *ctxt;
926         int rc;
927         ENTRY;
928
929 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
930         MOD_INC_USE_COUNT;
931 #else
932         if (!try_module_get(THIS_MODULE)) {
933                 CERROR("Can't get module. Is it alive?");
934                 return -EINVAL;
935         }
936 #endif
937         switch (cmd) {
938         case OBD_IOC_CLIENT_RECOVER:
939                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
940                 if (rc < 0)
941                         GOTO(out, rc);
942                 GOTO(out, rc = 0);
943         case IOC_OSC_SET_ACTIVE:
944                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
945                 GOTO(out, rc);
946         case IOC_OSC_CTL_RECOVERY:
947                 rc = ptlrpc_import_control_recovery(imp, data->ioc_offset);
948                 GOTO(out, rc);
949         case OBD_IOC_PARSE: {
950                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
951                                         LLOG_CONFIG_REPL_CTXT);
952                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
953                 GOTO(out, rc);
954         }
955 #ifdef __KERNEL__
956         case OBD_IOC_LLOG_INFO:
957         case OBD_IOC_LLOG_PRINT: {
958                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
959                 rc = llog_ioctl(ctxt, cmd, data);
960
961                 GOTO(out, rc);
962         }
963 #endif
964         default:
965                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
966                 GOTO(out, rc = -ENOTTY);
967         }
968 out:
969 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
970         MOD_DEC_USE_COUNT;
971 #else
972         module_put(THIS_MODULE);
973 #endif
974
975         return rc;
976 }
977
978 int mdc_set_info(struct obd_export *exp, obd_count keylen,
979                  void *key, obd_count vallen, void *val)
980 {
981         int rc = -EINVAL;
982
983         if (keylen == strlen("initial_recov") &&
984             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
985                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
986                 if (vallen != sizeof(int))
987                         RETURN(-EINVAL);
988                 imp->imp_initial_recov = *(int *)val;
989                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
990                        exp->exp_obd->obd_name,
991                        imp->imp_initial_recov);
992                 RETURN(0);
993         } else if ((keylen >= strlen("crypto_type")) && 
994                     strcmp(key, "crypto_type") == 0) {
995                 struct ptlrpc_request *req;
996                 char *bufs[2] = {key, val};
997                 int rc, size[2] = {keylen, vallen};
998
999                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1000                                       OST_SET_INFO, 2, size, bufs);
1001                 if (req == NULL)
1002                         RETURN(-ENOMEM);
1003
1004                 req->rq_replen = lustre_msg_size(0, NULL);
1005                 rc = ptlrpc_queue_wait(req);
1006                 ptlrpc_req_finished(req);
1007                 RETURN(rc);
1008         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1009                 struct obd_import *imp = class_exp2cliimp(exp);
1010                 imp->imp_server_timeout = 1;
1011                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1012                 RETURN(0);
1013         } else if (keylen == strlen("sec") &&
1014                    memcmp(key, "sec", keylen) == 0) {
1015                 struct client_obd *cli = &exp->exp_obd->u.cli;
1016
1017                 cli->cl_sec_flavor = ptlrpcs_name2flavor(val);
1018                 if (cli->cl_sec_flavor == PTLRPCS_FLVR_INVALID) {
1019                         CERROR("unrecognized security type %s\n", (char*) val);
1020                         RETURN(-EINVAL);
1021                 }
1022
1023                 RETURN(0);
1024         } else if (keylen == strlen("sec_flags") &&
1025                    memcmp(key, "sec_flags", keylen) == 0) {
1026                 struct client_obd *cli = &exp->exp_obd->u.cli;
1027
1028                 cli->cl_sec_flags = *((unsigned long *) val);
1029                 RETURN(0);
1030         } else if (keylen == strlen("flush_cred") &&
1031                    memcmp(key, "flush_cred", keylen) == 0) {
1032                 struct client_obd *cli = &exp->exp_obd->u.cli;
1033
1034                 if (cli->cl_import)
1035                         ptlrpcs_import_flush_current_creds(cli->cl_import);
1036                 RETURN(0);
1037         } else if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
1038                 struct client_obd *cl = &exp->exp_obd->u.cli;
1039                 if (vallen != sizeof(int))
1040                         RETURN(-EINVAL);
1041                 cl->cl_async = *(int *)val;
1042                 CDEBUG(D_HA, "%s: set async = %d\n",
1043                        exp->exp_obd->obd_name, cl->cl_async);
1044                 RETURN(0);
1045         } else if (keylen == strlen("setext") && memcmp(key, "setext", keylen) == 0) {
1046                 struct ptlrpc_request *req;
1047                 char *bufs[2] = {key, val};
1048                 int rc, size[2] = {keylen, vallen};
1049
1050                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1051                                       OST_SET_INFO, 2, size, bufs);
1052                 if (req == NULL)
1053                         RETURN(-ENOMEM);
1054
1055                 req->rq_replen = lustre_msg_size(0, NULL);
1056                 rc = ptlrpc_queue_wait(req);
1057                 ptlrpc_req_finished(req);
1058                 RETURN(rc);
1059         } else if (keylen == 5 && strcmp(key, "audit") == 0) {
1060                 struct ptlrpc_request *req;
1061                 char *bufs[2] = {key, val};
1062                 int rc, size[2] = {keylen, vallen};
1063
1064                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1065                                       OST_SET_INFO, 2, size, bufs);
1066                 if (req == NULL)
1067                         RETURN(-ENOMEM);
1068
1069                 req->rq_replen = lustre_msg_size(0, NULL);
1070                 lustre_swab_reqbuf(req, 1, sizeof(struct audit_attr_msg),
1071                                    lustre_swab_audit_attr);
1072                 rc = ptlrpc_queue_wait(req);
1073                 ptlrpc_req_finished(req);
1074
1075                 RETURN(rc);
1076         } else if (keylen == strlen("ids") && memcmp(key, "ids", keylen) == 0) {
1077                 struct ptlrpc_request *req;
1078                 struct lustre_id *ids = (struct lustre_id *)val;
1079                 char *bufs[3] = {key, (char *)ids, (char *)(ids + 1)};
1080                 int rc, size[3] = {keylen, sizeof(struct lustre_id), 
1081                                    sizeof(struct lustre_id)};
1082
1083                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1084                                       OST_SET_INFO, 3, size, bufs);
1085                 if (req == NULL)
1086                         RETURN(-ENOMEM);
1087
1088                 req->rq_replen = lustre_msg_size(0, NULL);
1089                 rc = ptlrpc_queue_wait(req);
1090                 ptlrpc_req_finished(req);
1091                 RETURN(rc);
1092         }
1093         RETURN(rc);
1094 }
1095
1096 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1097                       unsigned long max_age)
1098 {
1099         struct obd_statfs *msfs;
1100         struct ptlrpc_request *req;
1101         int rc, size = sizeof(*msfs);
1102         ENTRY;
1103
1104         /* We could possibly pass max_age in the request (as an absolute
1105          * timestamp or a "seconds.usec ago") so the target can avoid doing
1106          * extra calls into the filesystem if that isn't necessary (e.g.
1107          * during mount that would help a bit).  Having relative timestamps
1108          * is not so great if request processing is slow, while absolute
1109          * timestamps are not ideal because they need time synchronization. */
1110         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
1111                               MDS_STATFS, 0, NULL, NULL);
1112         if (!req)
1113                 RETURN(-ENOMEM);
1114
1115         req->rq_replen = lustre_msg_size(1, &size);
1116
1117         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
1118         rc = ptlrpc_queue_wait(req);
1119         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
1120
1121         if (rc) {
1122                 /* this can be LMV fake import, whcih is not connected. */
1123                 if (!req->rq_import->imp_connection)
1124                         memset(osfs, 0, sizeof(*osfs));
1125                 GOTO(out, rc);
1126         }
1127
1128         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
1129                                   lustre_swab_obd_statfs);
1130         if (msfs == NULL) {
1131                 CERROR("Can't unpack obd_statfs\n");
1132                 GOTO(out, rc = -EPROTO);
1133         }
1134
1135         memcpy(osfs, msfs, sizeof (*msfs));
1136         EXIT;
1137 out:
1138         ptlrpc_req_finished(req);
1139         return rc;
1140 }
1141
1142 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
1143                    struct obd_client_handle *handle, int flag)
1144 {
1145         struct ptlrpc_request *req;
1146         struct mds_body *body;
1147         int rc, size[2] = {0, sizeof(*body)};
1148         ENTRY;
1149
1150         //size[0] = lustre_secdesc_size();
1151
1152         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1153                               MDS_PIN, 2, size, NULL);
1154         if (req == NULL)
1155                 RETURN(-ENOMEM);
1156
1157         //lustre_pack_secdesc(req, size[0]);
1158
1159         body = lustre_msg_buf(req->rq_reqmsg, 
1160                               MDS_REQ_REC_OFF, sizeof(*body));
1161
1162         /* FIXME-UMKA: here should be also mdsnum and fid. */
1163         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
1164         body->flags = flag;
1165
1166         req->rq_replen = lustre_msg_size(1, &size[1]);
1167
1168         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1169         rc = ptlrpc_queue_wait(req);
1170         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1171         if (rc) {
1172                 CERROR("pin failed: %d\n", rc);
1173                 ptlrpc_req_finished(req);
1174                 RETURN(rc);
1175         }
1176
1177         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1178         if (body == NULL) {
1179                 ptlrpc_req_finished(req);
1180                 RETURN(rc);
1181         }
1182
1183         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1184         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1185
1186         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1187         if (handle->och_mod == NULL) {
1188                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1189                 RETURN(-ENOMEM);
1190         }
1191         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1192
1193         RETURN(rc);
1194 }
1195
1196 static int mdc_unpin(struct obd_export *exp,
1197                      struct obd_client_handle *handle, int flag)
1198 {
1199         struct ptlrpc_request *req;
1200         struct mds_body *body;
1201         int rc, size[2] = {0, sizeof(*body)};
1202         ENTRY;
1203
1204         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1205                 RETURN(0);
1206
1207         //size[0] = lustre_secdesc_size();
1208
1209         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1210                               MDS_CLOSE, 2, size, NULL);
1211         if (req == NULL)
1212                 RETURN(-ENOMEM);
1213
1214         //lustre_pack_secdesc(req, size[0]);
1215
1216         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
1217         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1218         body->flags = flag;
1219
1220         req->rq_replen = lustre_msg_size(0, NULL);
1221         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1222         rc = ptlrpc_queue_wait(req);
1223         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1224
1225         if (rc != 0)
1226                 CERROR("unpin failed: %d\n", rc);
1227
1228         ptlrpc_req_finished(req);
1229         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1230         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1231         RETURN(rc);
1232 }
1233
1234 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
1235              struct ptlrpc_request **request)
1236 {
1237         struct ptlrpc_request *req;
1238         struct mds_body *body;
1239         int size[2] = {0, sizeof(*body)};
1240         int rc;
1241         ENTRY;
1242
1243         //size[0] = lustre_secdesc_size();
1244
1245         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1246                               MDS_SYNC, 2, size, NULL);
1247         if (!req)
1248                 RETURN(rc = -ENOMEM);
1249
1250         //lustre_pack_secdesc(req, size[0]);
1251
1252         if (id) {
1253                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1254                                       sizeof (*body));
1255                 memcpy(&body->id1, id, sizeof(*id));
1256         }
1257
1258         req->rq_replen = lustre_msg_size(1, &size[1]);
1259
1260         rc = ptlrpc_queue_wait(req);
1261         if (rc || request == NULL)
1262                 ptlrpc_req_finished(req);
1263         else
1264                 *request = req;
1265
1266         RETURN(rc);
1267 }
1268
1269 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1270                             enum obd_import_event event)
1271 {
1272         int rc = 0;
1273
1274         LASSERT(imp->imp_obd == obd);
1275
1276         switch (event) {
1277         case IMP_EVENT_DISCON: {
1278                 break;
1279         }
1280         case IMP_EVENT_INACTIVE: {
1281                 if (obd->obd_observer)
1282                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1283                 break;
1284         }
1285         case IMP_EVENT_INVALIDATE: {
1286                 struct ldlm_namespace *ns = obd->obd_namespace;
1287
1288                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1289
1290                 break;
1291         }
1292         case IMP_EVENT_ACTIVE: {
1293                 if (obd->obd_observer)
1294                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1295                 break;
1296         }
1297         default:
1298                 CERROR("Unknown import event %d\n", event);
1299                 LBUG();
1300         }
1301         RETURN(rc);
1302 }
1303
1304 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1305 {
1306         struct lprocfs_static_vars lvars;
1307
1308         lprocfs_init_vars(mdc, &lvars);
1309         return lprocfs_obd_attach(dev, lvars.obd_vars);
1310 }
1311
1312 static int mdc_detach(struct obd_device *dev)
1313 {
1314         return lprocfs_obd_detach(dev);
1315 }
1316
1317 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1318 {
1319         struct client_obd *cli = &obd->u.cli;
1320         int rc;
1321         ENTRY;
1322
1323         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1324         if (!cli->cl_rpc_lock)
1325                 RETURN(-ENOMEM);
1326         mdc_init_rpc_lock(cli->cl_rpc_lock);
1327
1328         ptlrpcd_addref();
1329
1330         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1331         if (!cli->cl_setattr_lock)
1332                 GOTO(err_rpc_lock, rc = -ENOMEM);
1333         mdc_init_rpc_lock(cli->cl_setattr_lock);
1334
1335         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1336         if (!cli->cl_close_lock)
1337                 GOTO(err_setattr_lock, rc = -ENOMEM);
1338         mdc_init_rpc_lock(cli->cl_close_lock);
1339
1340         rc = client_obd_setup(obd, len, buf);
1341         if (rc)
1342                 GOTO(err_close_lock, rc);
1343
1344         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1345         if (rc) {
1346                 mdc_cleanup(obd, 0);
1347                 CERROR("failed to setup llogging subsystems\n");
1348         }
1349
1350         RETURN(rc);
1351
1352 err_close_lock:
1353         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1354 err_setattr_lock:
1355         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1356 err_rpc_lock:
1357         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1358         ptlrpcd_decref();
1359         RETURN(rc);
1360 }
1361
1362 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1363 {
1364         struct obd_device *obd = exp->exp_obd;
1365         struct client_obd *cli = &obd->u.cli;
1366         ENTRY;
1367
1368         if (cli->cl_max_mds_easize < easize)
1369                 cli->cl_max_mds_easize = easize;
1370         if (cli->cl_max_mds_cookiesize < cookiesize)
1371                 cli->cl_max_mds_cookiesize = cookiesize;
1372         RETURN(0);
1373 }
1374
1375 static int mdc_precleanup(struct obd_device *obd, int flags)
1376 {
1377         int rc = 0;
1378         
1379         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1380         if (rc != 0)
1381                 CERROR("failed to cleanup llogging subsystems\n");
1382
1383         RETURN(rc);
1384 }
1385
1386 static int mdc_cleanup(struct obd_device *obd, int flags)
1387 {
1388         struct client_obd *cli = &obd->u.cli;
1389
1390         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1391         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1392         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1393
1394         ptlrpcd_decref();
1395
1396         return client_obd_cleanup(obd, flags);
1397 }
1398
1399
1400 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1401                          struct obd_device *tgt, int count,
1402                          struct llog_catid *logid)
1403 {
1404         struct llog_ctxt *ctxt;
1405         int rc;
1406         ENTRY;
1407
1408         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1409                             &llog_client_ops);
1410         if (rc == 0) {
1411                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1412                 ctxt->loc_imp = obd->u.cli.cl_import;
1413         }
1414
1415         RETURN(rc);
1416 }
1417
1418 static int mdc_llog_finish(struct obd_device *obd,
1419                            struct obd_llogs *llogs, int count)
1420 {
1421         int rc;
1422         ENTRY;
1423
1424         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1425         RETURN(rc);
1426 }
1427
1428 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1429                                            struct lustre_id *id)
1430 {
1431        ENTRY;
1432        RETURN(exp->exp_obd);
1433 }
1434
1435 static int mdc_get_info(struct obd_export *exp, __u32 keylen,
1436                         void *key, __u32 *valsize, void *val)
1437 {
1438         struct ptlrpc_request *req;
1439         char *bufs[1] = {key};
1440         int rc = 0;
1441         ENTRY;
1442         
1443         if (!valsize || !val)
1444                 RETURN(-EFAULT);
1445
1446         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
1447                 struct obd_import *imp;
1448                 struct obd_connect_data *data;
1449
1450                 imp = class_exp2cliimp(exp);
1451                 if (!imp) {
1452                         LBUG();
1453                         RETURN(-EINVAL);
1454                 }
1455
1456                 if (imp->imp_state != LUSTRE_IMP_FULL) {
1457                         CERROR("import state not full\n");
1458                         RETURN(-EINVAL);
1459                 }
1460
1461                 data = &imp->imp_connect_data;
1462                 if (data->ocd_connect_flags & OBD_CONNECT_REMOTE) {
1463                         *((int *)val) = 1;
1464                         RETURN(0);
1465                 } else if (data->ocd_connect_flags & OBD_CONNECT_LOCAL) {
1466                         *((int *)val) = 0;
1467                         RETURN(0);
1468                 }
1469                 CERROR("no remote flag set?\n");
1470                 RETURN(-EINVAL);
1471         }
1472
1473         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1474             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1475             (keylen < strlen("lovdesc") || strcmp(key, "lovdesc") != 0) &&
1476             (keylen < strlen("getext") || strcmp(key, "getext") != 0) &&
1477             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0) &&
1478             (keylen < strlen("auditid") || strcmp(key, "auditid") != 0))
1479                 RETURN(-EPROTO);
1480                 
1481         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1482                               OST_GET_INFO, 1, (int *)&keylen, bufs);
1483         if (req == NULL)
1484                 RETURN(-ENOMEM);
1485
1486         req->rq_replen = lustre_msg_size(1, (int *)valsize);
1487         rc = ptlrpc_queue_wait(req);
1488         if (rc)
1489                 GOTO(out_req, rc);
1490
1491         if ((keylen >= strlen("rootid") && !strcmp(key, "rootid")) ||
1492             (keylen >= strlen("auditid") && !strcmp(key, "auditid"))) {
1493                 struct lustre_id *reply;
1494                 
1495                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1496                                            lustre_swab_lustre_id);
1497                 if (reply == NULL) {
1498                         CERROR("Can't unpack %s\n", (char *)key);
1499                         GOTO(out_req, rc = -EPROTO);
1500                 }
1501
1502                 *(struct lustre_id *)val = *reply;
1503         } else if (keylen >= strlen("lovdesc") && !strcmp(key, "lovdesc")) {
1504                 struct lov_desc *reply;
1505                 
1506                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1507                                            lustre_swab_lov_desc);
1508                 if (reply == NULL) {
1509                         CERROR("Can't unpack %s\n", (char *)key);
1510                         GOTO(out_req, rc = -EPROTO);
1511                 }
1512
1513                 *(struct lov_desc *)val = *reply;
1514                 RETURN(0);
1515         } else if (keylen >= strlen("getext") && !strcmp(key, "getext")) {
1516                 struct fid_extent *reply;
1517                 
1518                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1519                                            lustre_swab_fid_extent);
1520                 if (reply == NULL) {
1521                         CERROR("Can't unpack %s\n", (char *)key);
1522                         GOTO(out_req, rc = -EPROTO);
1523                 }
1524
1525                 *(struct fid_extent *)val = *reply;
1526                 RETURN(0);
1527         } else {
1528                 __u32 *reply;
1529                 
1530                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1531                                            lustre_swab_generic_32s);
1532                 if (reply == NULL) {
1533                         CERROR("Can't unpack %s\n", (char *)key);
1534                         GOTO(out_req, rc = -EPROTO);
1535                 }
1536                 *((__u32 *)val) = *reply;
1537         }
1538 out_req:
1539         ptlrpc_req_finished(req);
1540         RETURN(rc);
1541 }
1542
1543 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1544                    void *acl, int acl_size,
1545                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1546 {
1547         struct ptlrpc_request *request;
1548         struct ost_body *body;
1549         char *acl_buf;
1550         int rc, size[2] = { sizeof(*body), acl_size };
1551         ENTRY;
1552
1553         LASSERT(oa);
1554
1555         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1556                                   OST_CREATE, 2, size, NULL);
1557         if (!request)
1558                 GOTO(out_req, rc = -ENOMEM);
1559
1560         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1561         memcpy(&body->oa, oa, sizeof(body->oa));
1562
1563         if (acl_size) {
1564                 acl_buf = lustre_msg_buf(request->rq_reqmsg, 1, acl_size);
1565                 memcpy(acl_buf, acl, acl_size);
1566         }
1567
1568         request->rq_replen = lustre_msg_size(1, size);
1569         rc = ptlrpc_queue_wait(request);
1570         if (rc)
1571                 GOTO(out_req, rc);
1572
1573         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1574                                   lustre_swab_ost_body);
1575         if (body == NULL) {
1576                 CERROR ("can't unpack ost_body\n");
1577                 GOTO (out_req, rc = -EPROTO);
1578         }
1579
1580         memcpy(oa, &body->oa, sizeof(*oa));
1581
1582         /* store ino/generation for recovery */
1583         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1584         body->oa.o_id = oa->o_id;
1585         body->oa.o_generation = oa->o_generation;
1586         body->oa.o_fid = oa->o_fid;
1587         body->oa.o_mds = oa->o_mds;
1588
1589         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1590         EXIT;
1591 out_req:
1592         ptlrpc_req_finished(request);
1593         return rc;
1594 }
1595
1596 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1597             struct lov_stripe_md *ea, obd_count oa_bufs,
1598             struct brw_page *pgarr, struct obd_trans_info *oti)
1599 {
1600         struct ptlrpc_bulk_desc *desc;
1601         struct niobuf_remote *niobuf;
1602         struct ptlrpc_request *req;
1603         struct obd_ioobj *ioobj;
1604         struct ost_body *body;
1605         int err, opc, i;
1606         int size[3];
1607
1608         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1609         
1610         size[0] = sizeof(*body);
1611         size[1] = sizeof(*ioobj);
1612         size[2] = oa_bufs * sizeof(*niobuf);
1613
1614         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1615                               3, size, NULL);
1616         LASSERT(req != NULL);
1617
1618         if (opc == OST_WRITE)
1619                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1620                                             OST_BULK_PORTAL);
1621         else
1622                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1623                                             OST_BULK_PORTAL);
1624         LASSERT(desc != NULL);
1625
1626         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1627         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1628         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1629
1630         memcpy(&body->oa, oa, sizeof(*oa));
1631         obdo_to_ioobj(oa, ioobj);
1632         ioobj->ioo_bufcnt = oa_bufs;
1633
1634         for (i = 0; i < oa_bufs; i++, niobuf++) {
1635                 struct brw_page *pg = &pgarr[i];
1636
1637                 LASSERT(pg->count > 0);
1638                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1639
1640                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1641                                       pg->count);
1642
1643                 niobuf->offset = pg->disk_offset;
1644                 niobuf->len = pg->count;
1645                 niobuf->flags = pg->flag;
1646         }
1647
1648         /* size[0] still sizeof (*body) */
1649         if (opc == OST_WRITE) {
1650                 /* 1 RC per niobuf */
1651                 size[1] = sizeof(__u32) * oa_bufs;
1652                 req->rq_replen = lustre_msg_size(2, size);
1653         } else {
1654                 /* 1 RC for the whole I/O */
1655                 req->rq_replen = lustre_msg_size(1, size);
1656         }
1657         err = ptlrpc_queue_wait(req);
1658         LASSERT(err == 0);
1659
1660         ptlrpc_req_finished(req);
1661         return 0;
1662 }
1663
1664 static int mdc_valid_attrs(struct obd_export *exp,
1665                            struct lustre_id *id)
1666 {
1667         struct ldlm_res_id res_id = { .name = {0} };
1668         struct obd_device *obd = exp->exp_obd;
1669         struct lustre_handle lockh;
1670         ldlm_policy_data_t policy;
1671         int flags;
1672         ENTRY;
1673
1674         res_id.name[0] = id_fid(id);
1675         res_id.name[1] = id_group(id);
1676         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1677
1678         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1679                res_id.name[0]);
1680
1681         /* FIXME use LDLM_FL_TEST_LOCK instead */
1682         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1683         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1684                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1685                 ldlm_lock_decref(&lockh, LCK_PR);
1686                 RETURN(1);
1687         }
1688
1689         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1690                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1691                 ldlm_lock_decref(&lockh, LCK_PW);
1692                 RETURN(1);
1693         }
1694         RETURN(0);
1695 }
1696
1697 static int mdc_change_cbdata_name(struct obd_export *exp,
1698                                   struct lustre_id *pid,
1699                                   char *name, int len,
1700                                   struct lustre_id *cid,
1701                                   ldlm_iterator_t it, void *data)
1702 {
1703         int rc;
1704         rc = mdc_change_cbdata(exp, cid, it, data);
1705         RETURN(rc);
1706 }
1707
1708 struct obd_ops mdc_obd_ops = {
1709         .o_owner         = THIS_MODULE,
1710         .o_attach        = mdc_attach,
1711         .o_detach        = mdc_detach,
1712         .o_setup         = mdc_setup,
1713         .o_precleanup    = mdc_precleanup,
1714         .o_cleanup       = mdc_cleanup,
1715         .o_add_conn      = client_import_add_conn,
1716         .o_del_conn      = client_import_del_conn,
1717         .o_connect       = client_connect_import,
1718         .o_disconnect    = client_disconnect_export,
1719         .o_iocontrol     = mdc_iocontrol,
1720         .o_packmd        = mdc_packmd,
1721         .o_unpackmd      = mdc_unpackmd,
1722         .o_statfs        = mdc_statfs,
1723         .o_pin           = mdc_pin,
1724         .o_unpin         = mdc_unpin,
1725         .o_import_event  = mdc_import_event,
1726         .o_llog_init     = mdc_llog_init,
1727         .o_llog_finish   = mdc_llog_finish,
1728         .o_create        = mdc_obj_create,
1729         .o_set_info      = mdc_set_info,
1730         .o_get_info      = mdc_get_info,
1731         .o_brw           = mdc_brw,
1732         .o_cancel_unused = mdc_cancel_unused,
1733         .o_init_ea_size  = mdc_init_ea_size,
1734 };
1735
1736 struct md_ops mdc_md_ops = {
1737         .m_getstatus     = mdc_getstatus,
1738         .m_getattr       = mdc_getattr,
1739         .m_close         = mdc_close,
1740         .m_create        = mdc_create,
1741         .m_done_writing  = mdc_done_writing,
1742         .m_enqueue       = mdc_enqueue,
1743         .m_getattr_lock  = mdc_getattr_lock,
1744         .m_intent_lock   = mdc_intent_lock,
1745         .m_link          = mdc_link,
1746         .m_rename        = mdc_rename,
1747         .m_setattr       = mdc_setattr,
1748         .m_sync          = mdc_sync,
1749         .m_readpage      = mdc_readpage,
1750         .m_unlink        = mdc_unlink,
1751         .m_valid_attrs   = mdc_valid_attrs,
1752         .m_req2lustre_md = mdc_req2lustre_md,
1753         .m_set_open_replay_data   = mdc_set_open_replay_data,
1754         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1755         .m_store_inode_generation = mdc_store_inode_generation,
1756         .m_set_lock_data = mdc_set_lock_data,
1757         .m_get_real_obd  = mdc_get_real_obd,
1758         .m_change_cbdata_name = mdc_change_cbdata_name,
1759         .m_change_cbdata = mdc_change_cbdata,
1760         .m_access_check  = mdc_access_check,
1761 };
1762
1763 int __init mdc_init(void)
1764 {
1765         struct lprocfs_static_vars lvars;
1766         
1767         lprocfs_init_vars(mdc, &lvars);
1768         return class_register_type(&mdc_obd_ops, &mdc_md_ops,
1769                                    lvars.module_vars, OBD_MDC_DEVICENAME);
1770 }
1771
1772 #ifdef __KERNEL__
1773 static void /*__exit*/ mdc_exit(void)
1774 {
1775         class_unregister_type(OBD_MDC_DEVICENAME);
1776 }
1777
1778 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1779 MODULE_DESCRIPTION("Lustre Metadata Client");
1780 MODULE_LICENSE("GPL");
1781
1782 EXPORT_SYMBOL(mdc_req2lustre_md);
1783 EXPORT_SYMBOL(mdc_req2lustre_capa);
1784 EXPORT_SYMBOL(mdc_change_cbdata);
1785 EXPORT_SYMBOL(mdc_getstatus);
1786 EXPORT_SYMBOL(mdc_getattr);
1787 EXPORT_SYMBOL(mdc_getattr_lock);
1788 EXPORT_SYMBOL(mdc_create);
1789 EXPORT_SYMBOL(mdc_unlink);
1790 EXPORT_SYMBOL(mdc_rename);
1791 EXPORT_SYMBOL(mdc_link);
1792 EXPORT_SYMBOL(mdc_readpage);
1793 EXPORT_SYMBOL(mdc_setattr);
1794 EXPORT_SYMBOL(mdc_close);
1795 EXPORT_SYMBOL(mdc_done_writing);
1796 EXPORT_SYMBOL(mdc_sync);
1797 EXPORT_SYMBOL(mdc_set_open_replay_data);
1798 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1799 EXPORT_SYMBOL(mdc_store_inode_generation);
1800
1801 module_init(mdc_init);
1802 module_exit(mdc_exit);
1803 #endif