Whamcloud - gitweb
14ddcc91c190ee3e8f036acbe96d8468fce4db6f
[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-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 # include <linux/module.h>
32 # include <linux/pagemap.h>
33 # include <linux/miscdevice.h>
34 # include <linux/init.h>
35 #else
36 # include <liblustre.h>
37 #endif
38
39 #include <linux/lustre_acl.h>
40 #include <obd_class.h>
41 #include <lustre_dlm.h>
42 #include <lustre_fid.h>
43 #include <md_object.h>
44 #include <lprocfs_status.h>
45 #include <lustre_param.h>
46 #include "mdc_internal.h"
47
48 static quota_interface_t *quota_interface;
49
50 #define REQUEST_MINOR 244
51
52 static quota_interface_t *quota_interface;
53 extern quota_interface_t mdc_quota_interface;
54
55 static int mdc_cleanup(struct obd_device *obd);
56
57 static struct obd_capa *mdc_unpack_capa(struct ptlrpc_request *req,
58                                         const struct req_msg_field *field)
59 {
60         struct lustre_capa *capa;
61         struct obd_capa *oc;
62
63         /* swabbed already in mdc_enqueue */
64         capa = req_capsule_server_get(&req->rq_pill, field);
65         if (capa == NULL)
66                 return ERR_PTR(-EPROTO);
67
68         oc = alloc_capa(CAPA_SITE_CLIENT);
69         if (!oc) {
70                 CDEBUG(D_INFO, "alloc capa failed!\n");
71                 return ERR_PTR(-ENOMEM);
72         }
73         oc->c_capa = *capa;
74
75         return oc;
76 }
77
78 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
79 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
80 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
81                           struct obd_capa **pc, int level, int msg_flags)
82 {
83         struct ptlrpc_request *req;
84         struct mdt_body       *body;
85         int                    rc;
86         ENTRY;
87
88         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
89                                         LUSTRE_MDS_VERSION, MDS_GETSTATUS);
90         if (req == NULL)
91                 RETURN(-ENOMEM);
92
93         mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
94         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
95         req->rq_send_state = level;
96
97         ptlrpc_request_set_replen(req);
98
99         rc = ptlrpc_queue_wait(req);
100         if (rc)
101                 GOTO(out, rc);
102
103         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
104         if (body == NULL)
105                 GOTO(out, rc = -EPROTO);
106
107         if (body->valid & OBD_MD_FLMDSCAPA) {
108                 struct obd_capa *oc;
109
110                 oc = mdc_unpack_capa(req, &RMF_CAPA1);
111                 if (IS_ERR(oc))
112                         GOTO(out, rc = PTR_ERR(oc));
113                 *pc = oc;
114         }
115
116         *rootfid = body->fid1;
117         CDEBUG(D_NET,
118                "root fid="DFID", last_committed="LPU64", last_xid="LPU64"\n",
119                PFID(rootfid),
120                lustre_msg_get_last_committed(req->rq_repmsg),
121                lustre_msg_get_last_xid(req->rq_repmsg));
122         EXIT;
123 out:
124         ptlrpc_req_finished(req);
125         return rc;
126 }
127
128 /* This should be mdc_get_info("rootfid") */
129 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
130                   struct obd_capa **pc)
131 {
132         return send_getstatus(class_exp2cliimp(exp), rootfid, pc, 
133                               LUSTRE_IMP_FULL, 0);
134 }
135
136 /*
137  * This function now is known to always saying that it will receive 4 buffers
138  * from server. Even for cases when acl_size and md_size is zero, RPC header
139  * will contain 4 fields and RPC itself will contain zero size fields. This is
140  * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
141  * and thus zero, it shirinks it, making zero size. The same story about
142  * md_size. And this is course of problem when client waits for smaller number
143  * of fields. This issue will be fixed later when client gets aware of RPC
144  * layouts.  --umka
145  */
146 static int mdc_getattr_common(struct obd_export *exp,
147                               struct ptlrpc_request *req)
148 {
149         struct req_capsule *pill = &req->rq_pill;
150         struct mdt_body    *body;
151         void               *eadata;
152         int                 rc;
153         ENTRY;
154
155         /* Request message already built. */
156         rc = ptlrpc_queue_wait(req);
157         if (rc != 0)
158                 RETURN(rc);
159
160         /* sanity check for the reply */
161         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
162         if (body == NULL)
163                 RETURN(-EPROTO);
164
165         CDEBUG(D_NET, "mode: %o\n", body->mode);
166
167         if (body->eadatasize != 0) {
168                 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
169                                                       body->eadatasize);
170                 if (eadata == NULL)
171                         RETURN(-EPROTO);
172         }
173
174         if (body->valid & OBD_MD_FLMODEASIZE) {
175                 struct client_obd *cli = &exp->exp_obd->u.cli;
176
177                 if (cli->cl_max_mds_easize < body->max_mdsize)
178                         cli->cl_max_mds_easize = body->max_mdsize;
179                 if (cli->cl_max_mds_cookiesize < body->max_cookiesize)
180                         cli->cl_max_mds_cookiesize = body->max_cookiesize;
181         }
182
183         if (body->valid & OBD_MD_FLRMTPERM) {
184                 struct mdt_remote_perm *perm;
185
186                 LASSERT(client_is_remote(exp));
187                 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
188                                                 lustre_swab_mdt_remote_perm);
189                 if (perm == NULL)
190                         RETURN(-EPROTO);
191         }
192
193         if (body->valid & OBD_MD_FLMDSCAPA) {
194                 struct lustre_capa *capa;
195                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
196                 if (capa == NULL)
197                         RETURN(-EPROTO);
198         }
199
200         RETURN(0);
201 }
202
203 int mdc_getattr(struct obd_export *exp, const struct lu_fid *fid,
204                 struct obd_capa *oc, obd_valid valid, int ea_size,
205                 struct ptlrpc_request **request)
206 {
207         struct ptlrpc_request *req;
208         int                    rc;
209         ENTRY;
210
211         *request = NULL;
212         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
213         if (req == NULL)
214                 RETURN(-ENOMEM);
215
216         mdc_set_capa_size(req, &RMF_CAPA1, oc);
217
218         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
219         if (rc) {
220                 ptlrpc_request_free(req);
221                 RETURN(rc);
222         }
223
224         /* MDS_BFLAG_EXT_FLAGS: request "new" flags(bug 9486) */
225         mdc_pack_body(req, fid, oc, valid, ea_size, -1, MDS_BFLAG_EXT_FLAGS);
226
227         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER, ea_size);
228         if (valid & OBD_MD_FLRMTPERM) {
229                 LASSERT(client_is_remote(exp));
230                 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
231                                      sizeof(struct mdt_remote_perm));
232         }
233         ptlrpc_request_set_replen(req);
234
235         rc = mdc_getattr_common(exp, req);
236         if (rc)
237                 ptlrpc_req_finished(req);
238         else
239                 *request = req;
240         RETURN(rc);
241 }
242
243 int mdc_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
244                      struct obd_capa *oc, const char *filename, int namelen,
245                      obd_valid valid, int ea_size, __u32 suppgid,
246                      struct ptlrpc_request **request)
247 {
248         struct ptlrpc_request *req;
249         int                    rc;
250         ENTRY;
251
252         *request = NULL;
253         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
254                                    &RQF_MDS_GETATTR_NAME);
255         if (req == NULL)
256                 RETURN(-ENOMEM);
257
258         mdc_set_capa_size(req, &RMF_CAPA1, oc);
259         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, namelen);
260
261         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
262         if (rc) {
263                 ptlrpc_request_free(req);
264                 RETURN(rc);
265         }
266
267         /* MDS_BFLAG_EXT_FLAGS: request "new" flags(bug 9486) */
268         mdc_pack_body(req, fid, oc, valid, ea_size, suppgid,
269                       MDS_BFLAG_EXT_FLAGS);
270
271         if (filename) {
272                 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
273                 LASSERT(strnlen(filename, namelen) == namelen - 1);
274                 memcpy(name, filename, namelen);
275         }
276
277         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER, ea_size);
278         ptlrpc_request_set_replen(req);
279
280         rc = mdc_getattr_common(exp, req);
281         if (rc)
282                 ptlrpc_req_finished(req);
283         else
284                 *request = req;
285         RETURN(rc);
286 }
287
288 static int mdc_is_subdir(struct obd_export *exp,
289                          const struct lu_fid *pfid,
290                          const struct lu_fid *cfid,
291                          struct ptlrpc_request **request)
292 {
293         struct ptlrpc_request  *req;
294         int                     rc;
295
296         ENTRY;
297
298         *request = NULL;
299         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
300                                         &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
301                                         MDS_IS_SUBDIR);
302         if (req == NULL)
303                 RETURN(-ENOMEM);
304
305         mdc_is_subdir_pack(req, pfid, cfid, 0);
306         ptlrpc_request_set_replen(req);
307
308         rc = ptlrpc_queue_wait(req);
309         if (rc && rc != -EREMOTE)
310                 ptlrpc_req_finished(req);
311         else
312                 *request = req;
313         RETURN(rc);
314 }
315
316 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
317                             const struct lu_fid *fid,
318                             struct obd_capa *oc, int opcode, obd_valid valid,
319                             const char *xattr_name, const char *input,
320                             int input_size, int output_size, int flags,
321                             __u32 suppgid, struct ptlrpc_request **request)
322 {
323         struct ptlrpc_request *req;
324         int   xattr_namelen = 0;
325         char *tmp;
326         int   rc;
327         ENTRY;
328
329         *request = NULL;
330         req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
331         if (req == NULL)
332                 RETURN(-ENOMEM);
333
334         mdc_set_capa_size(req, &RMF_CAPA1, oc);
335         if (xattr_name) {
336                 xattr_namelen = strlen(xattr_name) + 1;
337                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
338                                      xattr_namelen);
339         }
340         if (input_size) {
341                 LASSERT(input);
342                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
343                                      input_size);
344         }
345
346         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
347         if (rc) {
348                 ptlrpc_request_free(req);
349                 RETURN(rc);
350         }
351
352         if (opcode == MDS_REINT) {
353                 struct mdt_rec_setxattr *rec;
354
355                 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
356                          sizeof(struct mdt_rec_reint));
357                 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
358                 rec->sx_opcode = REINT_SETXATTR;
359                 /* TODO: 
360                  *  cfs_curproc_fs{u,g}id() should replace 
361                  *  current->fs{u,g}id for portability.
362                  */
363                 rec->sx_fsuid  = current->fsuid;
364                 rec->sx_fsgid  = current->fsgid;
365                 rec->sx_cap    = current->cap_effective;
366                 rec->sx_suppgid1 = suppgid;
367                 rec->sx_suppgid2 = -1;
368                 rec->sx_fid    = *fid;
369                 rec->sx_valid  = valid | OBD_MD_FLCTIME;
370                 rec->sx_time   = cfs_time_current_sec();
371                 rec->sx_size   = output_size;
372                 rec->sx_flags  = flags;
373
374                 mdc_pack_capa(req, &RMF_CAPA1, oc);
375         } else {
376                 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
377         }
378
379         if (xattr_name) {
380                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
381                 memcpy(tmp, xattr_name, xattr_namelen);
382         }
383         if (input_size) {
384                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
385                 memcpy(tmp, input, input_size);
386         }
387
388         if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
389                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
390                                      RCL_SERVER, output_size);
391         ptlrpc_request_set_replen(req);
392
393         /* make rpc */
394         if (opcode == MDS_REINT)
395                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
396
397         rc = ptlrpc_queue_wait(req);
398
399         if (opcode == MDS_REINT)
400                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
401
402         if (rc)
403                 ptlrpc_req_finished(req);
404         else
405                 *request = req;
406         RETURN(rc);
407 }
408
409 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
410                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
411                  const char *input, int input_size, int output_size,
412                  int flags, __u32 suppgid, struct ptlrpc_request **request)
413 {
414         return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR, 
415                                 fid, oc, MDS_REINT, valid, xattr_name,
416                                 input, input_size, output_size, flags,
417                                 suppgid, request);
418 }
419
420 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
421                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
422                  const char *input, int input_size, int output_size,
423                  int flags, struct ptlrpc_request **request)
424 {
425         return mdc_xattr_common(exp, &RQF_MDS_GETXATTR, 
426                                 fid, oc, MDS_GETXATTR, valid, xattr_name,
427                                 input, input_size, output_size, flags,
428                                 -1, request);
429 }
430
431 #ifdef CONFIG_FS_POSIX_ACL
432 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
433 {
434         struct req_capsule     *pill = &req->rq_pill;
435         struct mdt_body        *body = md->body;
436         struct posix_acl       *acl;
437         void                   *buf;
438         int                     rc;
439         ENTRY;
440
441         if (!body->aclsize)
442                 RETURN(0);
443
444         buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
445
446         if (!buf)
447                 RETURN(-EPROTO);
448
449         acl = posix_acl_from_xattr(buf, body->aclsize);
450         if (IS_ERR(acl)) {
451                 rc = PTR_ERR(acl);
452                 CERROR("convert xattr to acl: %d\n", rc);
453                 RETURN(rc);
454         }
455
456         rc = posix_acl_valid(acl);
457         if (rc) {
458                 CERROR("validate acl: %d\n", rc);
459                 posix_acl_release(acl);
460                 RETURN(rc);
461         }
462
463         md->posix_acl = acl;
464         RETURN(0);
465 }
466 #else
467 #define mdc_unpack_acl(req, md) 0
468 #endif
469
470 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
471                       struct obd_export *dt_exp, struct obd_export *md_exp,
472                       struct lustre_md *md)
473 {
474         struct req_capsule *pill = &req->rq_pill;
475         int rc;
476         ENTRY;
477
478         LASSERT(md);
479         memset(md, 0, sizeof(*md));
480
481         md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
482         LASSERT(md->body != NULL);
483
484         if (md->body->valid & OBD_MD_FLEASIZE) {
485                 int lmmsize;
486                 struct lov_mds_md *lmm;
487
488                 if (!S_ISREG(md->body->mode)) {
489                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
490                                "regular file, but is not\n");
491                         GOTO(out, rc = -EPROTO);
492                 }
493
494                 if (md->body->eadatasize == 0) {
495                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
496                                "but eadatasize 0\n");
497                         GOTO(out, rc = -EPROTO);
498                 }
499                 lmmsize = md->body->eadatasize;
500                 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
501                 if (!lmm)
502                         GOTO(out, rc = -EPROTO);
503
504                 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
505                 if (rc < 0)
506                         GOTO(out, rc);
507
508                 if (rc < sizeof(*md->lsm)) {
509                         CDEBUG(D_INFO, "lsm size too small: "
510                                "rc < sizeof (*md->lsm) (%d < %d)\n",
511                                rc, (int)sizeof(*md->lsm));
512                         GOTO(out, rc = -EPROTO);
513                 }
514
515         } else if (md->body->valid & OBD_MD_FLDIREA) {
516                 int lmvsize;
517                 struct lov_mds_md *lmv;
518
519                 if(!S_ISDIR(md->body->mode)) {
520                         CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
521                                "directory, but is not\n");
522                         GOTO(out, rc = -EPROTO);
523                 }
524
525                 if (md->body->eadatasize == 0) {
526                         CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
527                                "but eadatasize 0\n");
528                         RETURN(-EPROTO);
529                 }
530                 if (md->body->valid & OBD_MD_MEA) {
531                         lmvsize = md->body->eadatasize;
532                         lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
533                                                            lmvsize);
534                         if (!lmv)
535                                 GOTO(out, rc = -EPROTO);
536
537                         rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
538                                           lmvsize);
539                         if (rc < 0)
540                                 GOTO(out, rc);
541
542                         if (rc < sizeof(*md->mea)) {
543                                 CDEBUG(D_INFO, "size too small:  "
544                                        "rc < sizeof(*md->mea) (%d < %d)\n",
545                                         rc, sizeof(*md->mea));
546                                 GOTO(out, rc = -EPROTO);
547                         }
548                 }
549         }
550         rc = 0;
551
552         if (md->body->valid & OBD_MD_FLRMTPERM) {
553                 /* remote permission */
554                 LASSERT(client_is_remote(exp));
555                 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
556                                                 lustre_swab_mdt_remote_perm);
557                 if (!md->remote_perm)
558                         GOTO(out, rc = -EPROTO);
559         }
560         else if (md->body->valid & OBD_MD_FLACL) {
561                 /* for ACL, it's possible that FLACL is set but aclsize is zero.
562                  * only when aclsize != 0 there's an actual segment for ACL 
563                  * in reply buffer. 
564                  */
565                 if (md->body->aclsize) {
566                         rc = mdc_unpack_acl(req, md);
567                         if (rc)
568                                 GOTO(out, rc);
569 #ifdef CONFIG_FS_POSIX_ACL
570                 } else {
571                         md->posix_acl = NULL;
572 #endif
573                 }
574         }
575         if (md->body->valid & OBD_MD_FLMDSCAPA) {
576                 struct obd_capa *oc = mdc_unpack_capa(req, &RMF_CAPA1);
577
578                 if (IS_ERR(oc))
579                         GOTO(out, rc = PTR_ERR(oc));
580                 md->mds_capa = oc;
581         }
582
583         if (md->body->valid & OBD_MD_FLOSSCAPA) {
584                 struct obd_capa *oc = mdc_unpack_capa(req, &RMF_CAPA2);
585
586                 if (IS_ERR(oc))
587                         GOTO(out, rc = PTR_ERR(oc));
588                 md->oss_capa = oc;
589         }
590
591         EXIT;
592 out:
593         if (rc) {
594                 if (md->oss_capa)
595                         free_capa(md->oss_capa);
596                 if (md->mds_capa)
597                         free_capa(md->mds_capa);
598 #ifdef CONFIG_FS_POSIX_ACL
599                 posix_acl_release(md->posix_acl);
600 #endif
601                 if (md->lsm)
602                         obd_free_memmd(dt_exp, &md->lsm);
603         }
604         return rc;
605 }
606
607 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
608 {
609         ENTRY;
610         RETURN(0);
611 }
612
613 static void mdc_replay_open(struct ptlrpc_request *req)
614 {
615         struct md_open_data *mod = req->rq_cb_data;
616         struct ptlrpc_request *cur, *tmp;
617         struct obd_client_handle *och;
618         struct lustre_handle old;
619         struct mdt_body *body;
620         ENTRY;
621
622         if (mod == NULL) {
623                 DEBUG_REQ(D_ERROR, req,
624                           "Can't properly replay without open data.");
625                 EXIT;
626                 return;
627         }
628
629         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
630         LASSERT(body != NULL);
631
632         och = mod->mod_och;
633         if (och != NULL) {
634                 struct lustre_handle *file_fh;
635
636                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
637
638                 file_fh = &och->och_fh;
639                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
640                        file_fh->cookie, body->handle.cookie);
641                 old = *file_fh;
642                 *file_fh = body->handle;
643         }
644         list_for_each_entry_safe(cur, tmp, &mod->mod_replay_list, rq_mod_list) {
645                 int opc = lustre_msg_get_opc(cur->rq_reqmsg);
646                 struct mdt_epoch *epoch = NULL;
647
648                 if (opc == MDS_CLOSE || opc == MDS_DONE_WRITING) {
649                         epoch = req_capsule_client_get(&cur->rq_pill,
650                                                &RMF_MDT_EPOCH);
651                         LASSERT(epoch);
652                         DEBUG_REQ(D_HA, cur, "updating %s body with new fh",
653                                   opc == MDS_CLOSE ? "CLOSE" : "DONE_WRITING");
654                 } else if (opc == MDS_REINT) {
655                         struct mdt_rec_setattr *rec;
656                         
657                         /* Check this is REINT_SETATTR. */
658                         rec = req_capsule_client_get(&cur->rq_pill,
659                                                &RMF_REC_REINT);
660                         LASSERT(rec && rec->sa_opcode == REINT_SETATTR);
661
662                         epoch = req_capsule_client_get(&cur->rq_pill,
663                                                &RMF_MDT_EPOCH);
664                         LASSERT(epoch);
665                         DEBUG_REQ(D_HA, cur, "updating REINT_SETATTR body "
666                                   "with new fh");
667                 }
668                 if (epoch) {
669                         if (och != NULL)
670                                 LASSERT(!memcmp(&old, &epoch->handle,
671                                                 sizeof(old)));
672                         epoch->handle = body->handle;
673                 }
674         }
675         EXIT;
676 }
677
678 void mdc_commit_delayed(struct ptlrpc_request *req)
679 {
680         struct md_open_data *mod = req->rq_cb_data;
681         struct ptlrpc_request *cur, *tmp;
682         
683         DEBUG_REQ(D_HA, req, "req committed");
684
685         if (mod == NULL)
686                 return;
687
688         req->rq_cb_data = NULL;
689         req->rq_commit_cb = NULL;
690         list_del_init(&req->rq_mod_list);
691         if (req->rq_sequence) {
692                 list_for_each_entry_safe(cur, tmp, &mod->mod_replay_list,
693                                          rq_mod_list) {
694                         LASSERT(cur != LP_POISON);
695                         LASSERT(cur->rq_type != LI_POISON);
696                         DEBUG_REQ(D_HA, cur, "req balanced");
697                         LASSERT(cur->rq_transno != 0);
698                         LASSERT(cur->rq_import == req->rq_import);
699
700                         /* We no longer want to preserve this for transno-
701                          * unconditional replay. */
702                         spin_lock(&cur->rq_lock);
703                         cur->rq_replay = 0;
704                         spin_unlock(&cur->rq_lock);
705                 }
706         }
707
708         if (list_empty(&mod->mod_replay_list)) {
709                 if (mod->mod_och != NULL)
710                         mod->mod_och->och_mod = NULL;
711
712                 OBD_FREE_PTR(mod);
713         }
714 }
715
716 int mdc_set_open_replay_data(struct obd_export *exp,
717                              struct obd_client_handle *och,
718                              struct ptlrpc_request *open_req)
719 {
720         struct md_open_data   *mod;
721         struct mdt_rec_create *rec;
722         struct mdt_body       *body;
723         struct obd_import     *imp = open_req->rq_import;
724         ENTRY;
725
726         if (!open_req->rq_replay)
727                 RETURN(0);
728
729         rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
730         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
731         LASSERT(rec != NULL);
732         /* Incoming message in my byte order (it's been swabbed). */
733         /* Outgoing messages always in my byte order. */
734         LASSERT(body != NULL);
735
736         /* Only if the import is replayable, we set replay_open data */
737         if (och && imp->imp_replayable) {
738                 OBD_ALLOC_PTR(mod);
739                 if (mod == NULL) {
740                         DEBUG_REQ(D_ERROR, open_req,
741                                   "Can't allocate md_open_data");
742                         RETURN(0);
743                 }
744                 CFS_INIT_LIST_HEAD(&mod->mod_replay_list);
745
746                 spin_lock(&open_req->rq_lock);
747                 och->och_mod = mod;
748                 mod->mod_och = och;
749                 open_req->rq_cb_data = mod;
750                 list_add_tail(&open_req->rq_mod_list, &mod->mod_replay_list);
751                 open_req->rq_commit_cb = mdc_commit_delayed;
752                 spin_unlock(&open_req->rq_lock);
753         }
754
755         rec->cr_fid2 = body->fid1;
756         rec->cr_ioepoch = body->ioepoch;
757         rec->cr_old_handle.cookie = body->handle.cookie;
758         open_req->rq_replay_cb = mdc_replay_open;
759         if (!fid_is_sane(&body->fid1)) {
760                 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
761                           "insane fid");
762                 LBUG();
763         }
764
765         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
766         RETURN(0);
767 }
768
769 int mdc_clear_open_replay_data(struct obd_export *exp,
770                                struct obd_client_handle *och)
771 {
772         struct md_open_data *mod = och->och_mod;
773         ENTRY;
774
775         /*
776          * Don't free the structure now (it happens in mdc_commit_delayed(),
777          * after the last request is removed from its replay list),
778          * but make sure that replay doesn't poke at the och, which is about to
779          * be freed.
780          */
781         LASSERT(mod != LP_POISON);
782         if (mod != NULL)
783                 mod->mod_och = NULL;
784
785         och->och_mod = NULL;
786         RETURN(0);
787 }
788
789 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
790               struct md_open_data *mod, struct ptlrpc_request **request)
791 {
792         struct obd_device     *obd = class_exp2obd(exp);
793         struct ptlrpc_request *req;
794         int                    rc;
795         ENTRY;
796
797         *request = NULL;
798         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
799         if (req == NULL)
800                 RETURN(-ENOMEM);
801
802         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
803
804         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
805         if (rc) {
806                 ptlrpc_request_free(req);
807                 RETURN(rc);
808         }
809
810         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
811          * portal whose threads are not taking any DLM locks and are therefore
812          * always progressing */
813         /* XXX FIXME bug 249 */
814         req->rq_request_portal = MDS_READPAGE_PORTAL;
815
816         /* Ensure that this close's handle is fixed up during replay. */
817         if (likely(mod != NULL))
818                 list_add_tail(&req->rq_mod_list, &mod->mod_replay_list);
819         else
820                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
821
822         mdc_close_pack(req, op_data);
823
824         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
825                              obd->u.cli.cl_max_mds_easize);
826         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
827                              obd->u.cli.cl_max_mds_cookiesize);
828
829         ptlrpc_request_set_replen(req);
830
831         req->rq_commit_cb = mdc_commit_delayed;
832         req->rq_replay = 1;
833         LASSERT(req->rq_cb_data == NULL);
834         req->rq_cb_data = mod;
835
836         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
837         rc = ptlrpc_queue_wait(req);
838         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
839
840         if (req->rq_repmsg == NULL) {
841                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
842                        req->rq_status);
843                 if (rc == 0)
844                         rc = req->rq_status ?: -EIO;
845         } else if (rc == 0 || rc == -EAGAIN) {
846                 struct mdt_body *body;
847
848                 rc = lustre_msg_get_status(req->rq_repmsg);
849                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
850                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
851                                   "= %d", rc);
852                         if (rc > 0)
853                                 rc = -rc;
854                 } else if (mod == NULL) {
855                         if (req->rq_import->imp_replayable) 
856                                 CERROR("Unexpected: can't find md_open_data," 
857                                        "but close succeeded with replayable imp"
858                                        "Please tell CFS.\n");
859                 }
860
861                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
862                 if (body == NULL)
863                         rc = -EPROTO;
864         }
865
866         EXIT;
867         if (rc != 0 && rc != -EAGAIN && req && req->rq_commit_cb)
868                 req->rq_commit_cb(req);
869
870         *request = req;
871         return rc;
872 }
873
874 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
875                      struct md_open_data *mod)
876 {
877         struct obd_device     *obd = class_exp2obd(exp);
878         struct ptlrpc_request *req;
879         int                    rc;
880         ENTRY;
881
882         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
883                                    &RQF_MDS_DONE_WRITING);
884         if (req == NULL)
885                 RETURN(-ENOMEM);
886
887         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
888         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
889         if (rc) {
890                 ptlrpc_request_free(req);
891                 RETURN(rc);
892         }
893
894         /* XXX: add DONE_WRITING request to och -- when Size-on-MDS
895          * recovery will be ready. */
896         mdc_close_pack(req, op_data);
897         ptlrpc_request_set_replen(req);
898         req->rq_replay = 1;
899         req->rq_cb_data = mod;
900         req->rq_commit_cb = mdc_commit_delayed;
901         if (likely(mod != NULL))
902                 list_add_tail(&req->rq_mod_list, &mod->mod_replay_list);
903         else
904                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
905
906         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
907         rc = ptlrpc_queue_wait(req);
908         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
909
910         /* Close the open replay sequence if an error occured or no SOM
911          * attribute update is needed. */
912         if (rc != -EAGAIN)
913                 ptlrpc_close_replay_seq(req);
914                 
915         if (rc && rc != -EAGAIN && req->rq_commit_cb)
916                 req->rq_commit_cb(req);
917
918         ptlrpc_req_finished(req);
919         RETURN(rc);
920 }
921
922 #ifdef HAVE_SPLIT_SUPPORT
923 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
924                  const struct page *page, int offset)
925 {
926         struct ptlrpc_request   *req;
927         struct ptlrpc_bulk_desc *desc;
928         int                      rc;
929         ENTRY;
930
931         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
932         if (req == NULL)
933                 RETURN(-ENOMEM);
934
935         /* FIXME: capa doesn't support split yet */
936         mdc_set_capa_size(req, &RMF_CAPA1, NULL);
937
938         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
939         if (rc) {
940                 ptlrpc_request_free(req);
941                 RETURN(rc);
942         }
943
944         req->rq_request_portal = MDS_READPAGE_PORTAL;
945
946         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
947         if (desc == NULL)
948                 GOTO(out, rc = -ENOMEM);
949
950         /* NB req now owns desc and will free it when it gets freed. */
951         ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
952         mdc_readdir_pack(req, 0, offset, fid, NULL);
953
954         ptlrpc_request_set_replen(req);
955         rc = ptlrpc_queue_wait(req);
956         GOTO(out, rc);
957 out:
958         ptlrpc_req_finished(req);
959         return rc;
960 }
961 EXPORT_SYMBOL(mdc_sendpage);
962 #endif
963
964 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
965                  struct obd_capa *oc, __u64 offset, struct page *page,
966                  struct ptlrpc_request **request)
967 {
968         struct ptlrpc_request   *req;
969         struct ptlrpc_bulk_desc *desc;
970         int                      rc;
971         ENTRY;
972
973         *request = NULL;
974         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
975         if (req == NULL)
976                 RETURN(-ENOMEM);
977
978         mdc_set_capa_size(req, &RMF_CAPA1, oc);
979
980         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
981         if (rc) {
982                 ptlrpc_request_free(req);
983                 RETURN(rc);
984         }
985
986         /* XXX FIXME bug 249 */
987         req->rq_request_portal = MDS_READPAGE_PORTAL;
988         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
989         if (desc == NULL) {
990                 ptlrpc_request_free(req);
991                 RETURN(-ENOMEM);
992         }
993
994         /* NB req now owns desc and will free it when it gets freed */
995         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
996         mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
997
998         ptlrpc_request_set_replen(req);
999         rc = ptlrpc_queue_wait(req);
1000         if (rc) {
1001                 ptlrpc_req_finished(req);
1002                 RETURN(rc);
1003         }
1004
1005         if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1006                 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1007                         req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1008                 ptlrpc_req_finished(req);
1009                 RETURN(-EPROTO);
1010         }
1011
1012         *request = req;
1013         RETURN(0);
1014 }
1015
1016 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1017                          void *karg, void *uarg)
1018 {
1019         struct obd_device *obd = exp->exp_obd;
1020         struct obd_ioctl_data *data = karg;
1021         struct obd_import *imp = obd->u.cli.cl_import;
1022         struct llog_ctxt *ctxt;
1023         int rc;
1024         ENTRY;
1025
1026         if (!try_module_get(THIS_MODULE)) {
1027                 CERROR("Can't get module. Is it alive?");
1028                 return -EINVAL;
1029         }
1030         switch (cmd) {
1031         case OBD_IOC_CLIENT_RECOVER:
1032                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1033                 if (rc < 0)
1034                         GOTO(out, rc);
1035                 GOTO(out, rc = 0);
1036         case IOC_OSC_SET_ACTIVE:
1037                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1038                 GOTO(out, rc);
1039         case OBD_IOC_PARSE: {
1040                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1041                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1042                 llog_ctxt_put(ctxt);
1043                 GOTO(out, rc);
1044         }
1045 #ifdef __KERNEL__
1046         case OBD_IOC_LLOG_INFO:
1047         case OBD_IOC_LLOG_PRINT: {
1048                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1049                 rc = llog_ioctl(ctxt, cmd, data);
1050                 llog_ctxt_put(ctxt);
1051                 GOTO(out, rc);
1052         }
1053 #endif
1054         case OBD_IOC_POLL_QUOTACHECK:
1055                 rc = lquota_poll_check(quota_interface, exp,
1056                                        (struct if_quotacheck *)karg);
1057                 GOTO(out, rc);
1058         default:
1059                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1060                 GOTO(out, rc = -ENOTTY);
1061         }
1062 out:
1063         module_put(THIS_MODULE);
1064
1065         return rc;
1066 }
1067
1068 static int do_set_info_async(struct obd_export *exp,
1069                              obd_count keylen, void *key,
1070                              obd_count vallen, void *val,
1071                              struct ptlrpc_request_set *set)
1072 {
1073         struct obd_import     *imp = class_exp2cliimp(exp);
1074         struct ptlrpc_request *req;
1075         char                  *tmp;
1076         int                    rc;
1077         ENTRY;
1078
1079         if (vallen != sizeof(int))
1080                 RETURN(-EINVAL);
1081
1082         spin_lock(&imp->imp_lock);
1083         if (*((int *)val)) {
1084                 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1085                 imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1086         } else {
1087                 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1088                 imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1089         }
1090         spin_unlock(&imp->imp_lock);
1091
1092         req = ptlrpc_request_alloc(imp, &RQF_MDS_SET_INFO);
1093         if (req == NULL)
1094                 RETURN(-ENOMEM);
1095
1096         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
1097                              RCL_CLIENT, keylen);
1098         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL,
1099                              RCL_CLIENT, vallen);
1100         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SET_INFO);
1101         if (rc) {
1102                 ptlrpc_request_free(req);
1103                 RETURN(rc);
1104         }
1105
1106         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1107         memcpy(tmp, key, keylen);
1108         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1109         memcpy(tmp, val, vallen);
1110
1111         ptlrpc_request_set_replen(req);
1112
1113         if (set) {
1114                 ptlrpc_set_add_req(set, req);
1115                 ptlrpc_check_set(set);
1116         } else {
1117                 rc = ptlrpc_queue_wait(req);
1118                 ptlrpc_req_finished(req);
1119         }
1120
1121         RETURN(rc);
1122 }
1123
1124 int mdc_set_info_async(struct obd_export *exp,
1125                        obd_count keylen, void *key,
1126                        obd_count vallen, void *val,
1127                        struct ptlrpc_request_set *set)
1128 {
1129         struct obd_import *imp = class_exp2cliimp(exp);
1130         int                rc = -EINVAL;
1131         ENTRY;
1132
1133         if (KEY_IS(KEY_INIT_RECOV)) {
1134                 if (vallen != sizeof(int))
1135                         RETURN(-EINVAL);
1136                 spin_lock(&imp->imp_lock);
1137                 imp->imp_initial_recov = *(int *)val;
1138                 spin_unlock(&imp->imp_lock);
1139                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1140                        exp->exp_obd->obd_name, imp->imp_initial_recov);
1141                 RETURN(0);
1142         }
1143         /* Turn off initial_recov after we try all backup servers once */
1144         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1145                 if (vallen != sizeof(int))
1146                         RETURN(-EINVAL);
1147                 spin_lock(&imp->imp_lock);
1148                 imp->imp_initial_recov_bk = *(int *)val;
1149                 if (imp->imp_initial_recov_bk)
1150                         imp->imp_initial_recov = 1;
1151                 spin_unlock(&imp->imp_lock);
1152                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1153                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1154                 RETURN(0);
1155         }
1156         if (KEY_IS(KEY_READ_ONLY)) {
1157                 rc = do_set_info_async(exp, keylen, key, vallen, val, set);
1158                 RETURN(rc);
1159         }
1160         if (KEY_IS(KEY_FLUSH_CTX)) {
1161                 sptlrpc_import_flush_my_ctx(imp);
1162                 RETURN(0);
1163         }
1164         if (KEY_IS(KEY_MDS_CONN)) {
1165                 struct obd_import *imp = class_exp2cliimp(exp);
1166                 
1167                 /* mds-mds import */
1168                 spin_lock(&imp->imp_lock);
1169                 imp->imp_server_timeout = 1;
1170                 spin_unlock(&imp->imp_lock);
1171                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1172                 CDEBUG(D_OTHER|D_WARNING, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1173                 RETURN(0);
1174         }
1175
1176         RETURN(rc);
1177 }
1178
1179 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1180                  __u32 *vallen, void *val)
1181 {
1182         int rc = -EINVAL;
1183
1184         if (KEY_IS(KEY_MAX_EASIZE)) {
1185                 int mdsize, *max_easize;
1186
1187                 if (*vallen != sizeof(int))
1188                         RETURN(-EINVAL);
1189                 mdsize = *(int*)val;
1190                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1191                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1192                 max_easize = val;
1193                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1194                 RETURN(0);
1195         }
1196         if (KEY_IS(KEY_CONN_DATA)) {
1197                 struct obd_import *imp = class_exp2cliimp(exp);
1198                 struct obd_connect_data *data = val;
1199
1200                 if (*vallen != sizeof(*data))
1201                         RETURN(-EINVAL);
1202
1203                 *data = imp->imp_connect_data;
1204                 RETURN(0);
1205         }
1206                 
1207         RETURN(rc);
1208 }
1209
1210 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1211                       __u64 max_age, __u32 flags)
1212 {
1213         struct ptlrpc_request *req;
1214         struct obd_statfs     *msfs;
1215         struct obd_import     *imp = NULL;
1216         int                    rc;
1217         ENTRY;
1218
1219                         
1220         /*Since the request might also come from lprocfs, so we need 
1221          *sync this with client_disconnect_export Bug15684*/
1222         down_read(&obd->u.cli.cl_sem);
1223         if (obd->u.cli.cl_import)
1224                 imp = class_import_get(obd->u.cli.cl_import);
1225         up_read(&obd->u.cli.cl_sem);
1226         if (!imp)
1227                 RETURN(-ENODEV);
1228         
1229         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1230                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1231         if (req == NULL)
1232                 GOTO(output, rc = -ENOMEM);
1233
1234         ptlrpc_request_set_replen(req);
1235
1236         if (flags & OBD_STATFS_NODELAY) {
1237                 /* procfs requests not want stay in wait for avoid deadlock */
1238                 req->rq_no_resend = 1;
1239                 req->rq_no_delay = 1;
1240         }
1241
1242         rc = ptlrpc_queue_wait(req);
1243         if (rc) {
1244                 /* check connection error first */
1245                 if (imp->imp_connect_error)
1246                         rc = imp->imp_connect_error;
1247                 GOTO(out, rc);
1248         }
1249
1250         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1251         if (msfs == NULL)
1252                 GOTO(out, rc = -EPROTO);
1253
1254         *osfs = *msfs;
1255         EXIT;
1256 out:
1257         ptlrpc_req_finished(req);
1258 output:
1259         class_import_put(imp);
1260         return rc;
1261 }
1262
1263 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1264                    struct obd_capa *oc, struct obd_client_handle *handle,
1265                    int flags)
1266 {
1267         struct ptlrpc_request *req;
1268         struct mdt_body       *body;
1269         int                    rc;
1270         ENTRY;
1271
1272         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1273         if (req == NULL)
1274                 RETURN(-ENOMEM);
1275
1276         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1277
1278         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1279         if (rc) {
1280                 ptlrpc_request_free(req);
1281                 RETURN(rc);
1282         }
1283
1284         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1285
1286         ptlrpc_request_set_replen(req);
1287
1288         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1289         rc = ptlrpc_queue_wait(req);
1290         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1291         if (rc) {
1292                 CERROR("Pin failed: %d\n", rc);
1293                 GOTO(err_out, rc);
1294         }
1295
1296         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1297         if (body == NULL)
1298                 GOTO(err_out, rc = -EPROTO);
1299
1300         handle->och_fh = body->handle;
1301         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1302
1303         OBD_ALLOC_PTR(handle->och_mod);
1304         if (handle->och_mod == NULL) {
1305                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1306                 GOTO(err_out, rc = -ENOMEM);
1307         }
1308         /* will be dropped by unpin */
1309         CFS_INIT_LIST_HEAD(&handle->och_mod->mod_replay_list);
1310         list_add_tail(&req->rq_mod_list, &handle->och_mod->mod_replay_list);
1311
1312         RETURN(0);
1313
1314 err_out:
1315         ptlrpc_req_finished(req);
1316         RETURN(rc);
1317 }
1318
1319 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1320                      int flag)
1321 {
1322         struct ptlrpc_request *req;
1323         struct mdt_body       *body;
1324         int                    rc;
1325         ENTRY;
1326
1327         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1328                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
1329         if (req == NULL)
1330                 RETURN(-ENOMEM);
1331
1332         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1333         body->handle = handle->och_fh;
1334         body->flags = flag;
1335
1336         ptlrpc_request_set_replen(req);
1337
1338         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1339         rc = ptlrpc_queue_wait(req);
1340         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1341
1342         if (rc != 0)
1343                 CERROR("Unpin failed: %d\n", rc);
1344
1345         ptlrpc_req_finished(req);
1346
1347         LASSERT(!list_empty(&handle->och_mod->mod_replay_list));
1348         req = list_entry(handle->och_mod->mod_replay_list.next,
1349                          typeof(*req), rq_mod_list);
1350         list_del_init(&req->rq_mod_list);
1351         ptlrpc_req_finished(req);
1352         LASSERT(list_empty(&handle->och_mod->mod_replay_list));
1353
1354         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1355         RETURN(rc);
1356 }
1357
1358 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1359              struct obd_capa *oc, struct ptlrpc_request **request)
1360 {
1361         struct ptlrpc_request *req;
1362         int                    rc;
1363         ENTRY;
1364
1365         *request = NULL;
1366         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1367         if (req == NULL)
1368                 RETURN(-ENOMEM);
1369
1370         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1371
1372         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1373         if (rc) {
1374                 ptlrpc_request_free(req);
1375                 RETURN(rc);
1376         }
1377
1378         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1379
1380         ptlrpc_request_set_replen(req);
1381
1382         rc = ptlrpc_queue_wait(req);
1383         if (rc)
1384                 ptlrpc_req_finished(req);
1385         else
1386                 *request = req;
1387         RETURN(rc);
1388 }
1389
1390 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1391                             enum obd_import_event event)
1392 {
1393         int rc = 0;
1394
1395         LASSERT(imp->imp_obd == obd);
1396
1397         switch (event) {
1398         case IMP_EVENT_DISCON: {
1399 #if 0
1400                 /* XXX Pass event up to OBDs stack. used only for FLD now */
1401                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1402 #endif
1403                 break;
1404         }
1405         case IMP_EVENT_INACTIVE: {
1406                 struct client_obd *cli = &obd->u.cli;
1407                 /* 
1408                  * Flush current sequence to make client obtain new one
1409                  * from server in case of disconnect/reconnect.
1410                  * If range is already empty then no need to flush it.
1411                  */
1412                 if (cli->cl_seq != NULL && 
1413                     !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1414                         seq_client_flush(cli->cl_seq);
1415                 }
1416
1417                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1418                 break;
1419         }
1420         case IMP_EVENT_INVALIDATE: {
1421                 struct ldlm_namespace *ns = obd->obd_namespace;
1422
1423                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1424
1425                 break;
1426         }
1427         case IMP_EVENT_ACTIVE: {
1428                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1429                 break;
1430         }
1431         case IMP_EVENT_OCD:
1432                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1433                 break;
1434
1435         default:
1436                 CERROR("Unknown import event %x\n", event);
1437                 LBUG();
1438         }
1439         RETURN(rc);
1440 }
1441
1442 static int mdc_fid_init(struct obd_export *exp)
1443 {
1444         struct client_obd *cli = &exp->exp_obd->u.cli;
1445         char *prefix;
1446         int rc;
1447         ENTRY;
1448
1449         OBD_ALLOC_PTR(cli->cl_seq);
1450         if (cli->cl_seq == NULL)
1451                 RETURN(-ENOMEM);
1452
1453         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1454         if (prefix == NULL)
1455                 GOTO(out_free_seq, rc = -ENOMEM);
1456
1457         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1458                  exp->exp_obd->obd_name);
1459
1460         /* Init client side sequence-manager */
1461         rc = seq_client_init(cli->cl_seq, exp, 
1462                              LUSTRE_SEQ_METADATA,
1463                              prefix, NULL);
1464         OBD_FREE(prefix, MAX_OBD_NAME + 5);
1465         if (rc)
1466                 GOTO(out_free_seq, rc);
1467
1468         RETURN(rc);
1469 out_free_seq:
1470         OBD_FREE_PTR(cli->cl_seq);
1471         cli->cl_seq = NULL;
1472         return rc;
1473 }
1474
1475 static int mdc_fid_fini(struct obd_export *exp)
1476 {
1477         struct client_obd *cli = &exp->exp_obd->u.cli;
1478         ENTRY;
1479
1480         if (cli->cl_seq != NULL) {
1481                 seq_client_fini(cli->cl_seq);
1482                 OBD_FREE_PTR(cli->cl_seq);
1483                 cli->cl_seq = NULL;
1484         }
1485         
1486         RETURN(0);
1487 }
1488
1489 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1490                   struct md_op_data *op_data)
1491 {
1492         struct client_obd *cli = &exp->exp_obd->u.cli;
1493         struct lu_client_seq *seq = cli->cl_seq;
1494         ENTRY;
1495         RETURN(seq_client_alloc_fid(seq, fid));
1496 }
1497
1498 /* XXX This method is used only to clear current fid seq
1499  * once fld/mds insert failed */
1500 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1501 {
1502         struct client_obd *cli = &exp->exp_obd->u.cli;
1503         
1504         seq_client_flush(cli->cl_seq);
1505         return 0;
1506 }
1507
1508 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1509 {
1510         struct client_obd *cli = &obd->u.cli;
1511         struct lprocfs_static_vars lvars = { 0 };
1512         int rc;
1513         ENTRY;
1514
1515         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1516         if (!cli->cl_rpc_lock)
1517                 RETURN(-ENOMEM);
1518         mdc_init_rpc_lock(cli->cl_rpc_lock);
1519
1520         ptlrpcd_addref();
1521
1522         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1523         if (!cli->cl_setattr_lock)
1524                 GOTO(err_rpc_lock, rc = -ENOMEM);
1525         mdc_init_rpc_lock(cli->cl_setattr_lock);
1526
1527         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1528         if (!cli->cl_close_lock)
1529                 GOTO(err_setattr_lock, rc = -ENOMEM);
1530         mdc_init_rpc_lock(cli->cl_close_lock);
1531
1532         rc = client_obd_setup(obd, cfg);
1533         if (rc)
1534                 GOTO(err_close_lock, rc);
1535         lprocfs_mdc_init_vars(&lvars);
1536         lprocfs_obd_setup(obd, lvars.obd_vars);
1537         sptlrpc_lprocfs_cliobd_attach(obd);
1538         ptlrpc_lprocfs_register_obd(obd);
1539
1540         rc = obd_llog_init(obd, &obd->obd_olg, obd, 0, NULL, NULL);
1541         if (rc) {
1542                 mdc_cleanup(obd);
1543                 CERROR("failed to setup llogging subsystems\n");
1544         }
1545
1546         RETURN(rc);
1547
1548 err_close_lock:
1549         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1550 err_setattr_lock:
1551         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1552 err_rpc_lock:
1553         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1554         ptlrpcd_decref();
1555         RETURN(rc);
1556 }
1557
1558 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1559  * us to make MDS RPCs with large enough reply buffers to hold the
1560  * maximum-sized (= maximum striped) EA and cookie without having to
1561  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1562 static int mdc_init_ea_size(struct obd_export *exp, int easize,
1563                      int def_easize, int cookiesize)
1564 {
1565         struct obd_device *obd = exp->exp_obd;
1566         struct client_obd *cli = &obd->u.cli;
1567         ENTRY;
1568
1569         if (cli->cl_max_mds_easize < easize)
1570                 cli->cl_max_mds_easize = easize;
1571
1572         if (cli->cl_default_mds_easize < def_easize)
1573                 cli->cl_default_mds_easize = def_easize;
1574
1575         if (cli->cl_max_mds_cookiesize < cookiesize)
1576                 cli->cl_max_mds_cookiesize = cookiesize;
1577
1578         RETURN(0);
1579 }
1580
1581 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1582 {
1583         int rc = 0;
1584         ENTRY;
1585
1586         switch (stage) {
1587         case OBD_CLEANUP_EARLY:
1588         case OBD_CLEANUP_EXPORTS:
1589                 /* If we set up but never connected, the
1590                    client import will not have been cleaned. */
1591                 if (obd->u.cli.cl_import) {
1592                         struct obd_import *imp;
1593                         imp = obd->u.cli.cl_import;
1594                         CERROR("client import never connected\n");
1595                         ptlrpc_invalidate_import(imp);
1596                         ptlrpc_free_rq_pool(imp->imp_rq_pool);
1597                         class_destroy_import(imp);
1598                         obd->u.cli.cl_import = NULL;
1599                 }
1600                 rc = obd_llog_finish(obd, 0);
1601                 if (rc != 0)
1602                         CERROR("failed to cleanup llogging subsystems\n");
1603                 break;
1604         case OBD_CLEANUP_SELF_EXP:
1605                 break;
1606         case OBD_CLEANUP_OBD:
1607                 break;
1608         }
1609         RETURN(rc);
1610 }
1611
1612 static int mdc_cleanup(struct obd_device *obd)
1613 {
1614         struct client_obd *cli = &obd->u.cli;
1615
1616         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1617         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1618         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1619
1620         ptlrpc_lprocfs_unregister_obd(obd);
1621         lprocfs_obd_cleanup(obd);
1622         ptlrpcd_decref();
1623
1624         return client_obd_cleanup(obd);
1625 }
1626
1627
1628 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1629                          struct obd_device *tgt, int count,
1630                          struct llog_catid *logid, struct obd_uuid *uuid)
1631 {
1632         struct llog_ctxt *ctxt;
1633         int rc;
1634         ENTRY;
1635
1636         LASSERT(olg == &obd->obd_olg);
1637
1638         rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0,
1639                         NULL, &llog_client_ops);
1640         if (rc == 0) {
1641                 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1642                 llog_initiator_connect(ctxt);
1643                 llog_ctxt_put(ctxt);
1644         }
1645
1646         RETURN(rc);
1647 }
1648
1649 static int mdc_llog_finish(struct obd_device *obd, int count)
1650 {
1651         struct llog_ctxt *ctxt;
1652         int rc = 0;
1653         ENTRY;
1654
1655         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1656         if (ctxt)
1657                 rc = llog_cleanup(ctxt);
1658
1659         RETURN(rc);
1660 }
1661
1662 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1663 {
1664         struct lustre_cfg *lcfg = buf;
1665         struct lprocfs_static_vars lvars = { 0 };
1666         int rc = 0;
1667
1668         lprocfs_mdc_init_vars(&lvars);
1669
1670         switch (lcfg->lcfg_command) {
1671         case LCFG_SPTLRPC_CONF:
1672                 rc = sptlrpc_cliobd_process_config(obd, lcfg);
1673                 break;
1674         default:
1675                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
1676                                               lcfg, obd);
1677                 break;
1678         }
1679         return(rc);
1680 }
1681
1682
1683 /* get remote permission for current user on fid */
1684 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
1685                         struct obd_capa *oc, __u32 suppgid,
1686                         struct ptlrpc_request **request)
1687 {
1688         struct ptlrpc_request  *req;
1689         int                    rc;
1690         ENTRY;
1691
1692         LASSERT(client_is_remote(exp));
1693
1694         *request = NULL;
1695         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
1696         if (req == NULL)
1697                 RETURN(-ENOMEM);
1698
1699         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1700
1701         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
1702         if (rc) {
1703                 ptlrpc_request_free(req);
1704                 RETURN(rc);
1705         }
1706
1707         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
1708
1709         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
1710                              sizeof(struct mdt_remote_perm));
1711
1712         ptlrpc_request_set_replen(req);
1713
1714         rc = ptlrpc_queue_wait(req);
1715         if (rc)
1716                 ptlrpc_req_finished(req);
1717         else
1718                 *request = req;
1719         RETURN(rc);
1720 }
1721
1722 static int mdc_interpret_renew_capa(struct ptlrpc_request *req, void *unused,
1723                                     int status)
1724 {
1725         struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
1726         renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
1727         struct mdt_body *body = NULL;
1728         struct lustre_capa *capa;
1729         ENTRY;
1730
1731         if (status)
1732                 GOTO(out, capa = ERR_PTR(status));
1733
1734         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1735         if (body == NULL)
1736                 GOTO(out, capa = ERR_PTR(-EFAULT));
1737
1738         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
1739                 GOTO(out, capa = ERR_PTR(-ENOENT));
1740
1741         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
1742         if (!capa)
1743                 GOTO(out, capa = ERR_PTR(-EFAULT));
1744         EXIT;
1745 out:
1746         cb(oc, capa);
1747         return 0;
1748 }
1749
1750 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
1751                           renew_capa_cb_t cb)
1752 {
1753         struct ptlrpc_request *req;
1754         ENTRY;
1755
1756         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
1757                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
1758         if (req == NULL)
1759                 RETURN(-ENOMEM);
1760
1761         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
1762          * capa to renew is oss capa.
1763          */
1764         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
1765         ptlrpc_request_set_replen(req);
1766
1767         req->rq_async_args.pointer_arg[0] = oc;
1768         req->rq_async_args.pointer_arg[1] = cb;
1769         req->rq_interpret_reply = mdc_interpret_renew_capa;
1770         ptlrpcd_add_req(req);
1771         RETURN(0);
1772 }
1773
1774 static int mdc_connect(const struct lu_env *env,
1775                        struct lustre_handle *dlm_handle,
1776                        struct obd_device *obd, struct obd_uuid *cluuid,
1777                        struct obd_connect_data *data,
1778                        void *localdata)
1779 {
1780         struct obd_import *imp = obd->u.cli.cl_import;
1781
1782         /* mds-mds import features */
1783         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
1784                 spin_lock(&imp->imp_lock);
1785                 imp->imp_server_timeout = 1;
1786                 spin_unlock(&imp->imp_lock);
1787                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1788                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
1789                        obd->obd_name);
1790         }
1791
1792         return client_connect_import(env, dlm_handle, obd, cluuid, data, NULL);
1793 }
1794
1795 struct obd_ops mdc_obd_ops = {
1796         .o_owner            = THIS_MODULE,
1797         .o_setup            = mdc_setup,
1798         .o_precleanup       = mdc_precleanup,
1799         .o_cleanup          = mdc_cleanup,
1800         .o_add_conn         = client_import_add_conn,
1801         .o_del_conn         = client_import_del_conn,
1802         .o_connect          = mdc_connect,
1803         .o_disconnect       = client_disconnect_export,
1804         .o_iocontrol        = mdc_iocontrol,
1805         .o_set_info_async   = mdc_set_info_async,
1806         .o_statfs           = mdc_statfs,
1807         .o_pin              = mdc_pin,
1808         .o_unpin            = mdc_unpin,
1809         .o_fid_init         = mdc_fid_init,
1810         .o_fid_fini         = mdc_fid_fini,
1811         .o_fid_alloc        = mdc_fid_alloc,
1812         .o_fid_delete       = mdc_fid_delete,
1813         .o_import_event     = mdc_import_event,
1814         .o_llog_init        = mdc_llog_init,
1815         .o_llog_finish      = mdc_llog_finish,
1816         .o_get_info         = mdc_get_info,
1817         .o_process_config  = mdc_process_config,
1818 };
1819
1820 struct md_ops mdc_md_ops = {
1821         .m_getstatus        = mdc_getstatus,
1822         .m_change_cbdata    = mdc_change_cbdata,
1823         .m_close            = mdc_close,
1824         .m_create           = mdc_create,
1825         .m_done_writing     = mdc_done_writing,
1826         .m_enqueue          = mdc_enqueue,
1827         .m_getattr          = mdc_getattr,
1828         .m_getattr_name     = mdc_getattr_name,
1829         .m_intent_lock      = mdc_intent_lock,
1830         .m_link             = mdc_link,
1831         .m_is_subdir        = mdc_is_subdir,
1832         .m_rename           = mdc_rename,
1833         .m_setattr          = mdc_setattr,
1834         .m_setxattr         = mdc_setxattr,
1835         .m_getxattr         = mdc_getxattr,
1836         .m_sync             = mdc_sync,
1837         .m_readpage         = mdc_readpage,
1838         .m_unlink           = mdc_unlink,
1839         .m_cancel_unused    = mdc_cancel_unused,
1840         .m_init_ea_size     = mdc_init_ea_size,
1841         .m_set_lock_data    = mdc_set_lock_data,
1842         .m_lock_match       = mdc_lock_match,
1843         .m_get_lustre_md    = mdc_get_lustre_md,
1844         .m_free_lustre_md   = mdc_free_lustre_md,
1845         .m_set_open_replay_data = mdc_set_open_replay_data,
1846         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1847         .m_renew_capa       = mdc_renew_capa,
1848         .m_get_remote_perm  = mdc_get_remote_perm,
1849         .m_intent_getattr_async = mdc_intent_getattr_async,
1850         .m_revalidate_lock      = mdc_revalidate_lock
1851 };
1852
1853 extern quota_interface_t mdc_quota_interface;
1854
1855 int __init mdc_init(void)
1856 {
1857         int rc;
1858         struct lprocfs_static_vars lvars = { 0 };
1859         lprocfs_mdc_init_vars(&lvars);
1860         
1861         request_module("lquota");
1862         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
1863         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
1864
1865         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1866                                  LUSTRE_MDC_NAME, NULL);
1867         if (rc && quota_interface)
1868                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1869
1870         RETURN(rc);
1871 }
1872
1873 #ifdef __KERNEL__
1874 static void /*__exit*/ mdc_exit(void)
1875 {
1876         if (quota_interface)
1877                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1878
1879         class_unregister_type(LUSTRE_MDC_NAME);
1880 }
1881
1882 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1883 MODULE_DESCRIPTION("Lustre Metadata Client");
1884 MODULE_LICENSE("GPL");
1885
1886 module_init(mdc_init);
1887 module_exit(mdc_exit);
1888 #endif