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