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