Whamcloud - gitweb
b72e7000090d317b132ad5b65df17bd7767886a3
[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, -1, 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         lustre_set_rep_swabbed(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, -1,
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, __u32 suppgid,
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, suppgid,
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, __u32 suppgid, 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;
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,
371                           suppgid, flags);
372
373
374         if (xattr_name) {
375                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, xattr_namelen);
376                 memcpy(tmp, xattr_name, xattr_namelen);
377         }
378         if (input_size) {
379                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, input_size);
380                 memcpy(tmp, input, input_size);
381         }
382
383         /* reply buffers */
384         if (opcode == MDS_GETXATTR) {
385                 size[REPLY_REC_OFF] = sizeof(struct mdt_body);
386                 bufcnt = 2;
387         } else {
388                 bufcnt = 1;
389         }
390
391         /* we do this even output_size is 0, because server is doing that */
392         size[bufcnt++] = output_size;
393         ptlrpc_req_set_repsize(req, bufcnt, size);
394
395         /* make rpc */
396         if (opcode == MDS_SETXATTR)
397                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
398
399         rc = ptlrpc_queue_wait(req);
400
401         if (opcode == MDS_SETXATTR)
402                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
403
404         if (rc != 0)
405                 GOTO(err_out, rc);
406
407         if (opcode == MDS_GETXATTR) {
408                 struct mdt_body * body = lustre_swab_repbuf(req, REPLY_REC_OFF,
409                                           sizeof(*body), lustre_swab_mdt_body);
410                 if (body == NULL) {
411                         CERROR ("Can't unpack mdt_body\n");
412                         GOTO(err_out, rc = -EPROTO);
413                 }
414         }
415 out:
416         *request = req;
417         RETURN (rc);
418 err_out:
419         ptlrpc_req_finished(req);
420         req = NULL;
421         goto out;
422 }
423
424 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
425                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
426                  const char *input, int input_size, int output_size, int flags,
427                  __u32 suppgid, struct ptlrpc_request **request)
428 {
429         return mdc_xattr_common(exp, fid, oc, MDS_SETXATTR, valid, xattr_name,
430                                 input, input_size, output_size, flags, suppgid,
431                                 request);
432 }
433
434 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
435                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
436                  const char *input, int input_size, int output_size, int flags,
437                  struct ptlrpc_request **request)
438 {
439         return mdc_xattr_common(exp, fid, oc, MDS_GETXATTR, valid, xattr_name,
440                                 input, input_size, output_size, flags, -1,
441                                 request);
442 }
443
444 #ifdef CONFIG_FS_POSIX_ACL
445 static
446 int mdc_unpack_acl(struct obd_export *exp, struct ptlrpc_request *req,
447                    struct lustre_md *md, unsigned int offset)
448 {
449         struct mdt_body  *body = md->body;
450         struct posix_acl *acl;
451         void             *buf;
452         int               rc;
453
454         if (!body->aclsize)
455                 return 0;
456
457         buf = lustre_msg_buf(req->rq_repmsg, offset, body->aclsize);
458         if (!buf) {
459                 CERROR("aclsize %u, bufcount %u, bufsize %u\n",
460                        body->aclsize, lustre_msg_bufcount(req->rq_repmsg),
461                        (lustre_msg_bufcount(req->rq_repmsg) <= offset) ?
462                                 -1 : lustre_msg_buflen(req->rq_repmsg, offset));
463                 return -EPROTO;
464         }
465
466         acl = posix_acl_from_xattr(buf, body->aclsize);
467         if (IS_ERR(acl)) {
468                 rc = PTR_ERR(acl);
469                 CERROR("convert xattr to acl: %d\n", rc);
470                 return rc;
471         }
472
473         rc = posix_acl_valid(acl);
474         if (rc) {
475                 CERROR("validate acl: %d\n", rc);
476                 posix_acl_release(acl);
477                 return rc;
478         }
479
480         md->posix_acl = acl;
481         return 0;
482 }
483 #else
484 #define mdc_unpack_acl(exp, req, md, offset) 0
485 #endif
486
487 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
488                       int offset, struct obd_export *dt_exp,
489                       struct obd_export *md_exp,
490                       struct lustre_md *md)
491 {
492         int rc;
493         ENTRY;
494
495         LASSERT(md);
496         memset(md, 0, sizeof(*md));
497
498         md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
499         LASSERT (md->body != NULL);
500         LASSERT(lustre_rep_swabbed(req, offset));
501         offset++;
502
503         if (md->body->valid & OBD_MD_FLEASIZE) {
504                 int lmmsize;
505                 struct lov_mds_md *lmm;
506
507                 if (!S_ISREG(md->body->mode)) {
508                         CERROR("OBD_MD_FLEASIZE set, should be a regular file, "
509                                "but is not\n");
510                         GOTO(out, rc = -EPROTO);
511                 }
512
513                 if (md->body->eadatasize == 0) {
514                         CERROR("OBD_MD_FLEASIZE set, but eadatasize 0\n");
515                         GOTO(out, rc = -EPROTO);
516                 }
517                 lmmsize = md->body->eadatasize;
518                 lmm = lustre_msg_buf(req->rq_repmsg, offset, lmmsize);
519                 if (!lmm) {
520                         CERROR ("incorrect message: lmm == 0\n");
521                         GOTO(out, rc = -EPROTO);
522                 }
523                 LASSERT(lustre_rep_swabbed(req, offset));
524
525                 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
526                 if (rc < 0)
527                         GOTO(out, rc);
528
529                 if (rc < sizeof(*md->lsm)) {
530                         CERROR ("lsm size too small:  rc < sizeof (*md->lsm) "
531                                 "(%d < "LPSZ")\n", rc, sizeof(*md->lsm));
532                         GOTO(out, rc = -EPROTO);
533                 }
534
535                 offset++;
536         } else if (md->body->valid & OBD_MD_FLDIREA) {
537                 int lmvsize;
538                 struct lov_mds_md *lmv;
539
540                 if(!S_ISDIR(md->body->mode)) {
541                         CERROR("OBD_MD_FLDIREA set, should be a directory, but "
542                                "is not\n");
543                         GOTO(out, rc = -EPROTO);
544                 }
545
546                 if (md->body->eadatasize == 0) {
547                         CERROR("OBD_MD_FLDIREA is set, but eadatasize 0\n");
548                         RETURN(-EPROTO);
549                 }
550                 if (md->body->valid & OBD_MD_MEA) {
551                         lmvsize = md->body->eadatasize;
552                         lmv = lustre_msg_buf(req->rq_repmsg, offset, lmvsize);
553                         if (!lmv) {
554                                 CERROR ("incorrect message: lmv == 0\n");
555                                 GOTO(out, rc = -EPROTO);
556                         }
557
558                         LASSERT(lustre_rep_swabbed(req, offset));
559
560                         rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
561                                           lmvsize);
562                         if (rc < 0)
563                                 GOTO(out, rc);
564
565                         if (rc < sizeof(*md->mea)) {
566                                 CERROR ("size too small:  rc < sizeof(*md->mea) "
567                                         "(%d < %d)\n", rc, sizeof(*md->mea));
568                                 GOTO(out, rc = -EPROTO);
569                         }
570                 }
571                 offset++;
572         }
573         rc = 0;
574
575         /* remote permission */
576         if (md->body->valid & OBD_MD_FLRMTPERM) {
577                 md->remote_perm = lustre_msg_buf(req->rq_repmsg, offset++,
578                                                 sizeof(struct mdt_remote_perm));
579                 if (!md->remote_perm) {
580                         CERROR ("incorrect message: remote_perm == 0\n");
581                         GOTO(out, rc = -EPROTO);
582                 }
583         }
584
585         /* for ACL, it's possible that FLACL is set but aclsize is zero.  only
586          * when aclsize != 0 there's an actual segment for ACL in reply
587          * buffer. */
588         else if (md->body->valid & OBD_MD_FLACL) {
589                 if (md->body->aclsize) {
590                         rc = mdc_unpack_acl(dt_exp, req, md, offset++);
591                         if (rc)
592                                 GOTO(out, rc);
593 #ifdef CONFIG_FS_POSIX_ACL
594                 } else {
595                         md->posix_acl = NULL;
596 #endif
597                 }
598         }
599
600         if (md->body->valid & OBD_MD_FLMDSCAPA) {
601                 struct obd_capa *oc = mdc_unpack_capa(req, offset++);
602
603                 if (IS_ERR(oc))
604                         GOTO(out, rc = PTR_ERR(oc));
605                 md->mds_capa = oc;
606         }
607
608         if (md->body->valid & OBD_MD_FLOSSCAPA) {
609                 struct obd_capa *oc = mdc_unpack_capa(req, offset++);
610
611                 if (IS_ERR(oc))
612                         GOTO(out, rc = PTR_ERR(oc));
613                 md->oss_capa = oc;
614         }
615
616         EXIT;
617 out:
618         if (rc) {
619                 if (md->oss_capa)
620                         free_capa(md->oss_capa);
621                 if (md->mds_capa)
622                         free_capa(md->mds_capa);
623 #ifdef CONFIG_FS_POSIX_ACL
624                 posix_acl_release(md->posix_acl);
625 #endif
626                 if (md->lsm)
627                         obd_free_memmd(dt_exp, &md->lsm);
628         }
629         return rc;
630 }
631
632 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
633 {
634         ENTRY;
635         RETURN(0);
636 }
637
638 static void mdc_replay_open(struct ptlrpc_request *req)
639 {
640         struct md_open_data *mod = req->rq_cb_data;
641         struct ptlrpc_request *cur, *tmp;
642         struct obd_client_handle *och;
643         struct lustre_handle old;
644         struct mdt_body *body;
645         ENTRY;
646
647         if (mod == NULL) {
648                 DEBUG_REQ(D_ERROR, req,
649                           "Can't properly replay without open data.");
650                 EXIT;
651                 return;
652         }
653
654         body = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, sizeof(*body),
655                                   lustre_swab_mdt_body);
656         LASSERT(body != NULL);
657
658         och = mod->mod_och;
659         if (och != NULL) {
660                 struct lustre_handle *file_fh;
661
662                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
663
664                 file_fh = &och->och_fh;
665                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
666                        file_fh->cookie, body->handle.cookie);
667                 old = *file_fh;
668                 *file_fh = body->handle;
669         }
670
671         list_for_each_entry_safe(cur, tmp, &mod->mod_replay_list, rq_mod_list) {
672                 int opc = lustre_msg_get_opc(cur->rq_reqmsg);
673                 struct mdt_epoch *epoch = NULL;
674
675                 if (opc == MDS_CLOSE || opc == MDS_DONE_WRITING) {
676                         epoch = lustre_msg_buf(cur->rq_reqmsg,
677                                                REQ_REC_OFF, sizeof(*epoch));
678                         LASSERT(epoch);
679                         DEBUG_REQ(D_HA, cur, "updating %s body with new fh",
680                                   opc == MDS_CLOSE ? "CLOSE" : "DONE_WRITING");
681                 } else if (opc == MDS_REINT) {
682                         struct mdt_rec_setattr *rec;
683                         
684                         /* Check this is REINT_SETATTR. */
685                         rec = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
686                                              sizeof (*rec));
687                         LASSERT(rec && rec->sa_opcode == REINT_SETATTR);
688
689                         epoch = lustre_msg_buf(cur->rq_reqmsg,
690                                                REQ_REC_OFF + 2, sizeof(*epoch));
691                         LASSERT(epoch);
692                         DEBUG_REQ(D_HA, cur, "updating REINT_SETATTR body "
693                                   "with new fh");
694                 }
695                 if (epoch) {
696                         if (och != NULL)
697                                 LASSERT(!memcmp(&old, &epoch->handle,
698                                                 sizeof(old)));
699                         epoch->handle = body->handle;
700                 }
701         }
702         EXIT;
703 }
704
705 void mdc_commit_delayed(struct ptlrpc_request *req)
706 {
707         struct md_open_data *mod = req->rq_cb_data;
708         struct ptlrpc_request *cur, *tmp;
709         
710         DEBUG_REQ(D_HA, req, "req committed");
711
712         if (mod == NULL)
713                 return;
714
715         req->rq_cb_data = NULL;
716         req->rq_commit_cb = NULL;
717         list_del_init(&req->rq_mod_list);
718         if (req->rq_sequence) {
719                 list_for_each_entry_safe(cur, tmp, &mod->mod_replay_list,
720                                          rq_mod_list) {
721                         LASSERT(cur != LP_POISON);
722                         LASSERT(cur->rq_type != LI_POISON);
723                         DEBUG_REQ(D_HA, cur, "req balanced");
724                         LASSERT(cur->rq_transno != 0);
725                         LASSERT(cur->rq_import == req->rq_import);
726
727                         /* We no longer want to preserve this for transno-
728                          * unconditional replay. */
729                         spin_lock(&cur->rq_lock);
730                         cur->rq_replay = 0;
731                         spin_unlock(&cur->rq_lock);
732                 }
733         }
734
735         if (list_empty(&mod->mod_replay_list)) {
736                 if (mod->mod_och != NULL)
737                         mod->mod_och->och_mod = NULL;
738
739                 OBD_FREE_PTR(mod);
740         }
741 }
742
743 int mdc_set_open_replay_data(struct obd_export *exp,
744                              struct obd_client_handle *och,
745                              struct ptlrpc_request *open_req)
746 {
747         struct md_open_data *mod;
748         struct mdt_rec_create *rec = lustre_msg_buf(open_req->rq_reqmsg,
749                                                     DLM_INTENT_REC_OFF,
750                                                     sizeof(*rec));
751         struct mdt_body *body = lustre_msg_buf(open_req->rq_repmsg,
752                                                DLM_REPLY_REC_OFF,
753                                                sizeof(*body));
754         struct obd_import *imp = open_req->rq_import;
755         ENTRY;
756
757         LASSERT(rec != NULL);
758
759         /* Incoming message in my byte order (it's been swabbed). */
760         LASSERT(lustre_rep_swabbed(open_req, DLM_REPLY_REC_OFF));
761
762         /* Outgoing messages always in my byte order. */
763         LASSERT(body != NULL);
764
765         /*Only the import is replayable, we set replay_open data */
766         if (och && imp->imp_replayable) {
767                 OBD_ALLOC(mod, sizeof(*mod));
768                 if (mod == NULL) {
769                         DEBUG_REQ(D_ERROR, open_req,
770                                   "Can't allocate md_open_data");
771                         RETURN(0);
772                 }
773                 CFS_INIT_LIST_HEAD(&mod->mod_replay_list);
774
775                 spin_lock(&open_req->rq_lock);
776                 if (!open_req->rq_replay) {
777                         OBD_FREE(mod, sizeof(*mod));
778                         spin_unlock(&open_req->rq_lock);
779                         RETURN(0);
780                 }
781
782                 och->och_mod = mod;
783                 mod->mod_och = och;
784                 open_req->rq_cb_data = mod;
785                 list_add_tail(&open_req->rq_mod_list, &mod->mod_replay_list);
786                 open_req->rq_commit_cb = mdc_commit_delayed;
787                 spin_unlock(&open_req->rq_lock);
788         }
789
790         rec->cr_fid2 = body->fid1;
791         rec->cr_ioepoch = body->ioepoch;
792         rec->cr_old_handle.cookie = body->handle.cookie;
793         open_req->rq_replay_cb = mdc_replay_open;
794         if (!fid_is_sane(&body->fid1)) {
795                 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
796                           "insane fid");
797                 LBUG();
798         }
799
800         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
801         RETURN(0);
802 }
803
804 int mdc_clear_open_replay_data(struct obd_export *exp,
805                                struct obd_client_handle *och)
806 {
807         struct md_open_data *mod = och->och_mod;
808         ENTRY;
809
810         /*
811          * Don't free the structure now (it happens in mdc_commit_delayed(),
812          * after the last request is removed from its replay list),
813          * but make sure that replay doesn't poke at the och, which is about to
814          * be freed.
815          */
816         LASSERT(mod != LP_POISON);
817         if (mod != NULL)
818                 mod->mod_och = NULL;
819
820         och->och_mod = NULL;
821         RETURN(0);
822 }
823
824 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
825               struct md_open_data *mod, struct ptlrpc_request **request)
826 {
827         struct obd_device *obd = class_exp2obd(exp);
828         int reqsize[4] = { sizeof(struct ptlrpc_body),
829                            sizeof(struct mdt_epoch),
830                            sizeof(struct mdt_rec_setattr)};
831         int repsize[4] = { sizeof(struct ptlrpc_body),
832                            sizeof(struct mdt_body),
833                            obd->u.cli.cl_max_mds_easize,
834                            obd->u.cli.cl_max_mds_cookiesize };
835         struct ptlrpc_request *req;
836         int rc;
837         ENTRY;
838
839         reqsize[REQ_REC_OFF + 2] = op_data->op_capa1 ?
840                                         sizeof(struct lustre_capa) : 0;
841         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
842                               MDS_CLOSE, 4, reqsize, NULL);
843         if (req == NULL)
844                 GOTO(out, rc = -ENOMEM);
845
846         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
847          * portal whose threads are not taking any DLM locks and are therefore
848          * always progressing */
849         /* XXX FIXME bug 249 */
850         req->rq_request_portal = MDS_READPAGE_PORTAL;
851
852         /* Ensure that this close's handle is fixed up during replay. */
853         if (likely(mod != NULL))
854                 list_add_tail(&req->rq_mod_list, &mod->mod_replay_list);
855         else
856                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
857
858         mdc_close_pack(req, REQ_REC_OFF, op_data);
859         ptlrpc_req_set_repsize(req, 4, repsize);
860         req->rq_commit_cb = mdc_commit_delayed;
861         req->rq_replay = 1;
862         LASSERT(req->rq_cb_data == NULL);
863         req->rq_cb_data = mod;
864
865         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
866         rc = ptlrpc_queue_wait(req);
867         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
868
869         if (req->rq_repmsg == NULL) {
870                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
871                        req->rq_status);
872                 if (rc == 0)
873                         rc = req->rq_status ? req->rq_status : -EIO;
874         } else if (rc == 0 || rc == -EAGAIN) {
875                 rc = lustre_msg_get_status(req->rq_repmsg);
876                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
877                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
878                                   "= %d", rc);
879                         if (rc > 0)
880                                 rc = -rc;
881                 } else if (mod == NULL) {
882                         if (req->rq_import->imp_replayable) 
883                                 CERROR("Unexpected: can't find md_open_data," 
884                                        "but close succeeded with replayable imp"
885                                        "Please tell CFS.\n");
886                 }
887                 if (!lustre_swab_repbuf(req, REPLY_REC_OFF,
888                                         sizeof(struct mdt_body),
889                                         lustre_swab_mdt_body)) {
890                         CERROR("Error unpacking mdt_body\n");
891                         rc = -EPROTO;
892                 }
893         }
894
895         EXIT;
896         *request = req;
897  out:
898         if (rc != 0 && rc != -EAGAIN && req && req->rq_commit_cb)
899                 req->rq_commit_cb(req);
900
901         return rc;
902 }
903
904 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
905                      struct md_open_data *mod)
906 {
907         struct obd_device *obd = class_exp2obd(exp);
908         struct ptlrpc_request *req;
909         int size[4] = { sizeof(struct ptlrpc_body),
910                         sizeof(struct mdt_epoch),
911                         sizeof(struct mdt_rec_setattr)};
912         int repsize[2] = { sizeof(struct ptlrpc_body),
913                            sizeof(struct mdt_body)};
914         int rc;
915         ENTRY;
916
917         if (op_data->op_capa1)
918                 size[REQ_REC_OFF + 2] = sizeof(struct lustre_capa);
919         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
920                               MDS_DONE_WRITING, 4, size, NULL);
921         if (req == NULL)
922                 RETURN(-ENOMEM);
923
924         mdc_close_pack(req, REQ_REC_OFF, op_data);
925         
926         req->rq_replay = 1;
927         req->rq_cb_data = mod;
928         req->rq_commit_cb = mdc_commit_delayed;
929         if (likely(mod != NULL))
930                 list_add_tail(&req->rq_mod_list, &mod->mod_replay_list);
931         else
932                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
933
934         ptlrpc_req_set_repsize(req, 2, repsize);
935         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
936         rc = ptlrpc_queue_wait(req);
937         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
938
939         /* Close the open replay sequence if an error occured or no SOM
940          * attribute update is needed. */
941         if (rc != -EAGAIN)
942                 ptlrpc_close_replay_seq(req);
943                 
944         if (rc && rc != -EAGAIN && req->rq_commit_cb)
945                 req->rq_commit_cb(req);
946
947         ptlrpc_req_finished(req);
948         RETURN(rc);
949 }
950
951 #ifdef HAVE_SPLIT_SUPPORT
952 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
953                  const struct page *page, int offset)
954 {
955         int rc, size[3] = { sizeof(struct ptlrpc_body), sizeof(struct mdt_body) };
956         struct obd_import *imp = class_exp2cliimp(exp);
957         struct ptlrpc_bulk_desc *desc = NULL;
958         struct ptlrpc_request *req = NULL;
959         ENTRY;
960
961         CDEBUG(D_INODE, "object: "DFID"\n", PFID(fid));
962
963         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_WRITEPAGE, 3,
964                               size, NULL);
965         if (req == NULL)
966                 GOTO(out, rc = -ENOMEM);
967
968         req->rq_request_portal = MDS_READPAGE_PORTAL;
969
970         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
971         if (desc == NULL)
972                 GOTO(out, rc = -ENOMEM);
973
974         /* NB req now owns desc and will free it when it gets freed. */
975         ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
976         mdc_readdir_pack(req, REQ_REC_OFF, 0, offset, fid, NULL);
977
978         ptlrpc_req_set_repsize(req, 2, size);
979         rc = ptlrpc_queue_wait(req);
980         EXIT;
981 out:
982         if (req != NULL)
983                 ptlrpc_req_finished(req);
984         return rc;
985 }
986 EXPORT_SYMBOL(mdc_sendpage);
987 #endif
988
989 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
990                  struct obd_capa *oc, __u64 offset, struct page *page,
991                  struct ptlrpc_request **request)
992 {
993         int rc, size[3] = { sizeof(struct ptlrpc_body), sizeof(struct mdt_body) };
994         struct obd_import *imp = class_exp2cliimp(exp);
995         struct ptlrpc_bulk_desc *desc = NULL;
996         struct ptlrpc_request *req = NULL;
997         struct mdt_body *body;
998         ENTRY;
999
1000         CDEBUG(D_INODE, "object: "DFID"\n", PFID(fid));
1001
1002         size[REQ_REC_OFF + 1] = oc ? sizeof(struct lustre_capa) : 0;
1003         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE, 3, size,
1004                               NULL);
1005         if (req == NULL)
1006                 GOTO(out, rc = -ENOMEM);
1007
1008         /* XXX FIXME bug 249 */
1009         req->rq_request_portal = MDS_READPAGE_PORTAL;
1010
1011         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1012         if (desc == NULL)
1013                 GOTO(out, rc = -ENOMEM);
1014
1015         /* NB req now owns desc and will free it when it gets freed */
1016         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1017         mdc_readdir_pack(req, REQ_REC_OFF, offset, CFS_PAGE_SIZE, fid, oc);
1018
1019         ptlrpc_req_set_repsize(req, 2, size);
1020         rc = ptlrpc_queue_wait(req);
1021
1022         if (rc == 0) {
1023                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1024                                           lustre_swab_mdt_body);
1025                 if (body == NULL) {
1026                         CERROR("Can't unpack mdt_body\n");
1027                         GOTO(out, rc = -EPROTO);
1028                 }
1029
1030                 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1031                         CERROR ("Unexpected # bytes transferred: %d"
1032                                 " (%ld expected)\n",
1033                                 req->rq_bulk->bd_nob_transferred,
1034                                 CFS_PAGE_SIZE);
1035                         GOTO(out, rc = -EPROTO);
1036                 }
1037         }
1038
1039         EXIT;
1040  out:
1041         *request = req;
1042         return rc;
1043 }
1044
1045 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1046                          void *karg, void *uarg)
1047 {
1048         struct obd_device *obd = exp->exp_obd;
1049         struct obd_ioctl_data *data = karg;
1050         struct obd_import *imp = obd->u.cli.cl_import;
1051         struct llog_ctxt *ctxt;
1052         int rc;
1053         ENTRY;
1054
1055         if (!try_module_get(THIS_MODULE)) {
1056                 CERROR("Can't get module. Is it alive?");
1057                 return -EINVAL;
1058         }
1059         switch (cmd) {
1060         case OBD_IOC_CLIENT_RECOVER:
1061                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1062                 if (rc < 0)
1063                         GOTO(out, rc);
1064                 GOTO(out, rc = 0);
1065         case IOC_OSC_SET_ACTIVE:
1066                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1067                 GOTO(out, rc);
1068         case OBD_IOC_PARSE: {
1069                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1070                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1071                 GOTO(out, rc);
1072         }
1073 #ifdef __KERNEL__
1074         case OBD_IOC_LLOG_INFO:
1075         case OBD_IOC_LLOG_PRINT: {
1076                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1077                 rc = llog_ioctl(ctxt, cmd, data);
1078
1079                 GOTO(out, rc);
1080         }
1081 #endif
1082         case OBD_IOC_POLL_QUOTACHECK:
1083                 rc = lquota_poll_check(quota_interface, exp,
1084                                        (struct if_quotacheck *)karg);
1085                 GOTO(out, rc);
1086         default:
1087                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1088                 GOTO(out, rc = -ENOTTY);
1089         }
1090 out:
1091         module_put(THIS_MODULE);
1092
1093         return rc;
1094 }
1095
1096 int mdc_set_info_async(struct obd_export *exp, obd_count keylen,
1097                        void *key, obd_count vallen, void *val,
1098                        struct ptlrpc_request_set *set)
1099 {
1100         struct obd_import *imp = class_exp2cliimp(exp);
1101         int rc = -EINVAL;
1102
1103         if (KEY_IS(KEY_INIT_RECOV)) {
1104                 if (vallen != sizeof(int))
1105                         RETURN(-EINVAL);
1106                 spin_lock(&imp->imp_lock);
1107                 imp->imp_initial_recov = *(int *)val;
1108                 spin_unlock(&imp->imp_lock);
1109                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1110                        exp->exp_obd->obd_name, imp->imp_initial_recov);
1111                 RETURN(0);
1112         }
1113         /* Turn off initial_recov after we try all backup servers once */
1114         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1115                 if (vallen != sizeof(int))
1116                         RETURN(-EINVAL);
1117                 spin_lock(&imp->imp_lock);
1118                 imp->imp_initial_recov_bk = *(int *)val;
1119                 if (imp->imp_initial_recov_bk)
1120                         imp->imp_initial_recov = 1;
1121                 spin_unlock(&imp->imp_lock);
1122                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1123                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1124                 RETURN(0);
1125         }
1126         if (KEY_IS(KEY_READ_ONLY)) {
1127                 struct ptlrpc_request *req;
1128                 int size[3] = { sizeof(struct ptlrpc_body), keylen, vallen };
1129                 char *bufs[3] = { NULL, key, val };
1130
1131                 if (vallen != sizeof(int))
1132                         RETURN(-EINVAL);
1133
1134                 spin_lock(&imp->imp_lock);
1135                 if (*((int *)val)) {
1136                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1137                         imp->imp_connect_data.ocd_connect_flags |=
1138                                 OBD_CONNECT_RDONLY;
1139                 } else {
1140                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1141                         imp->imp_connect_data.ocd_connect_flags &=
1142                                 ~OBD_CONNECT_RDONLY;
1143                 }
1144                 spin_unlock(&imp->imp_lock);
1145
1146                 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_SET_INFO,
1147                                       3, size, bufs);
1148                 if (req == NULL)
1149                         RETURN(-ENOMEM);
1150
1151                 ptlrpc_req_set_repsize(req, 1, NULL);
1152                 if (set) {
1153                         rc = 0;
1154                         ptlrpc_set_add_req(set, req);
1155                         ptlrpc_check_set(set);
1156                 } else {
1157                         rc = ptlrpc_queue_wait(req);
1158                         ptlrpc_req_finished(req);
1159                 }
1160
1161                 RETURN(rc);
1162         }
1163         if (KEY_IS(KEY_FLUSH_CTX)) {
1164                 sptlrpc_import_flush_my_ctx(imp);
1165                 RETURN(0);
1166         }
1167         if (KEY_IS(KEY_MDS_CONN)) {
1168                 struct obd_import *imp = class_exp2cliimp(exp);
1169                 
1170                 /* mds-mds import */
1171                 spin_lock(&imp->imp_lock);
1172                 imp->imp_server_timeout = 1;
1173                 spin_unlock(&imp->imp_lock);
1174                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1175                 CDEBUG(D_OTHER|D_WARNING, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1176                 RETURN(0);
1177         }
1178
1179         RETURN(rc);
1180 }
1181
1182 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1183                  __u32 *vallen, void *val)
1184 {
1185         int rc = -EINVAL;
1186
1187         if (KEY_IS(KEY_MAX_EASIZE)) {
1188                 int mdsize, *max_easize;
1189
1190                 if (*vallen != sizeof(int))
1191                         RETURN(-EINVAL);
1192                 mdsize = *(int*)val;
1193                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1194                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1195                 max_easize = val;
1196                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1197                 RETURN(0);
1198         }
1199         if (KEY_IS(KEY_CONN_DATA)) {
1200                 struct obd_import *imp = class_exp2cliimp(exp);
1201                 struct obd_connect_data *data = val;
1202
1203                 if (*vallen != sizeof(*data))
1204                         RETURN(-EINVAL);
1205
1206                 *data = imp->imp_connect_data;
1207                 RETURN(0);
1208         }
1209                 
1210         RETURN(rc);
1211 }
1212
1213 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1214                       __u64 max_age)
1215 {
1216         struct ptlrpc_request *req;
1217         struct obd_statfs *msfs;
1218         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*msfs) };
1219         ENTRY;
1220
1221         /* We could possibly pass max_age in the request (as an absolute
1222          * timestamp or a "seconds.usec ago") so the target can avoid doing
1223          * extra calls into the filesystem if that isn't necessary (e.g.
1224          * during mount that would help a bit).  Having relative timestamps
1225          * is not so great if request processing is slow, while absolute
1226          * timestamps are not ideal because they need time synchronization. */
1227         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
1228                               MDS_STATFS, 1, NULL, NULL);
1229         if (!req)
1230                 RETURN(-ENOMEM);
1231
1232         ptlrpc_req_set_repsize(req, 2, size);
1233
1234         rc = ptlrpc_queue_wait(req);
1235
1236         if (rc) {
1237                 /* check connection error first */
1238                 if (obd->u.cli.cl_import->imp_connect_error)
1239                         rc = obd->u.cli.cl_import->imp_connect_error;
1240
1241                 GOTO(out, rc);
1242         }
1243
1244         msfs = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*msfs),
1245                                   lustre_swab_obd_statfs);
1246         if (msfs == NULL) {
1247                 CERROR("Can't unpack obd_statfs\n");
1248                 GOTO(out, rc = -EPROTO);
1249         }
1250
1251         memcpy(osfs, msfs, sizeof(*msfs));
1252         EXIT;
1253 out:
1254         ptlrpc_req_finished(req);
1255
1256         return rc;
1257 }
1258
1259 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1260                    struct obd_capa *oc,
1261                    struct obd_client_handle *handle, int flag)
1262 {
1263         struct ptlrpc_request *req;
1264         struct mdt_body *body;
1265         int rc, size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1266         ENTRY;
1267
1268         size[REQ_REC_OFF + 1] = oc ? sizeof(struct lustre_capa) : 0;
1269         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1270                               MDS_PIN, 3, size, NULL);
1271         if (req == NULL)
1272                 RETURN(-ENOMEM);
1273
1274         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof (*body));
1275         body->fid1 = *fid;
1276         body->flags = flag;
1277         mdc_pack_capa(req, REQ_REC_OFF + 1, oc);
1278
1279         ptlrpc_req_set_repsize(req, 2, size);
1280
1281         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1282         rc = ptlrpc_queue_wait(req);
1283         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1284         if (rc) {
1285                 CERROR("pin failed: %d\n", rc);
1286                 ptlrpc_req_finished(req);
1287                 RETURN(rc);
1288         }
1289
1290         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1291                                   lustre_swab_mdt_body);
1292         if (body == NULL) {
1293                 ptlrpc_req_finished(req);
1294                 RETURN(rc);
1295         }
1296
1297         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1298         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1299
1300         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1301         if (handle->och_mod == NULL) {
1302                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1303                 RETURN(-ENOMEM);
1304         }
1305
1306         /* will be dropped by unpin */
1307         CFS_INIT_LIST_HEAD(&handle->och_mod->mod_replay_list);
1308         list_add_tail(&req->rq_mod_list, &handle->och_mod->mod_replay_list);
1309
1310         RETURN(rc);
1311 }
1312
1313 static int mdc_unpin(struct obd_export *exp,
1314                      struct obd_client_handle *handle, int flag)
1315 {
1316         struct ptlrpc_request *req;
1317         struct mdt_body *body;
1318         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1319         ENTRY;
1320
1321         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1322                 RETURN(0);
1323
1324         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1325                               MDS_CLOSE, 2, size, NULL);
1326         if (req == NULL)
1327                 RETURN(-ENOMEM);
1328
1329         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1330         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1331         body->flags = flag;
1332
1333         ptlrpc_req_set_repsize(req, 1, NULL);
1334         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1335         rc = ptlrpc_queue_wait(req);
1336         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1337
1338         if (rc != 0)
1339                 CERROR("unpin failed: %d\n", rc);
1340
1341         ptlrpc_req_finished(req);
1342
1343         LASSERT(!list_empty(&handle->och_mod->mod_replay_list));
1344         req = list_entry(handle->och_mod->mod_replay_list.next,
1345                          typeof(*req), rq_mod_list);
1346         list_del_init(&req->rq_mod_list);
1347         ptlrpc_req_finished(req);
1348         LASSERT(list_empty(&handle->och_mod->mod_replay_list));
1349
1350         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1351         RETURN(rc);
1352 }
1353
1354 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1355              struct obd_capa *oc, struct ptlrpc_request **request)
1356 {
1357         struct ptlrpc_request *req;
1358         int size[3] = { sizeof(struct ptlrpc_body), sizeof(struct mdt_body) };
1359         int rc;
1360         ENTRY;
1361
1362         size[REQ_REC_OFF + 1] = oc ? sizeof(struct lustre_capa) : 0;
1363         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1364                               MDS_SYNC, 3, size, NULL);
1365         if (!req)
1366                 RETURN(rc = -ENOMEM);
1367
1368         mdc_pack_req_body(req, REQ_REC_OFF, 0, fid, oc, 0, -1, 0);
1369
1370         ptlrpc_req_set_repsize(req, 2, size);
1371
1372         rc = ptlrpc_queue_wait(req);
1373         if (rc || request == NULL)
1374                 ptlrpc_req_finished(req);
1375         else
1376                 *request = req;
1377
1378         RETURN(rc);
1379 }
1380
1381 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1382                             enum obd_import_event event)
1383 {
1384         int rc = 0;
1385
1386         LASSERT(imp->imp_obd == obd);
1387
1388         switch (event) {
1389         case IMP_EVENT_DISCON: {
1390 #if 0
1391                 /* XXX Pass event up to OBDs stack. used only for FLD now */
1392                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1393 #endif
1394                 break;
1395         }
1396         case IMP_EVENT_INACTIVE: {
1397                 struct client_obd *cli = &obd->u.cli;
1398                 /* 
1399                  * Flush current sequence to make client obtain new one
1400                  * from server in case of disconnect/reconnect.
1401                  * If range is already empty then no need to flush it.
1402                  */
1403                 if (cli->cl_seq != NULL && 
1404                     !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1405                         seq_client_flush(cli->cl_seq);
1406                 }
1407
1408                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1409                 break;
1410         }
1411         case IMP_EVENT_INVALIDATE: {
1412                 struct ldlm_namespace *ns = obd->obd_namespace;
1413
1414                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1415
1416                 break;
1417         }
1418         case IMP_EVENT_ACTIVE: {
1419                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1420                 break;
1421         }
1422         case IMP_EVENT_OCD:
1423                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1424                 break;
1425
1426         default:
1427                 CERROR("Unknown import event %x\n", event);
1428                 LBUG();
1429         }
1430         RETURN(rc);
1431 }
1432
1433 static int mdc_fid_init(struct obd_export *exp)
1434 {
1435         struct client_obd *cli = &exp->exp_obd->u.cli;
1436         char *prefix;
1437         int rc;
1438         ENTRY;
1439
1440         OBD_ALLOC_PTR(cli->cl_seq);
1441         if (cli->cl_seq == NULL)
1442                 RETURN(-ENOMEM);
1443
1444         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1445         if (prefix == NULL)
1446                 GOTO(out_free_seq, rc = -ENOMEM);
1447
1448         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1449                  exp->exp_obd->obd_name);
1450
1451         /* Init client side sequence-manager */
1452         rc = seq_client_init(cli->cl_seq, exp, 
1453                              LUSTRE_SEQ_METADATA,
1454                              prefix, NULL);
1455         OBD_FREE(prefix, MAX_OBD_NAME + 5);
1456         if (rc)
1457                 GOTO(out_free_seq, rc);
1458
1459         RETURN(rc);
1460 out_free_seq:
1461         OBD_FREE_PTR(cli->cl_seq);
1462         cli->cl_seq = NULL;
1463         return rc;
1464 }
1465
1466 static int mdc_fid_fini(struct obd_export *exp)
1467 {
1468         struct client_obd *cli = &exp->exp_obd->u.cli;
1469         ENTRY;
1470
1471         if (cli->cl_seq != NULL) {
1472                 seq_client_fini(cli->cl_seq);
1473                 OBD_FREE_PTR(cli->cl_seq);
1474                 cli->cl_seq = NULL;
1475         }
1476         
1477         RETURN(0);
1478 }
1479
1480 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1481                   struct md_op_data *op_data)
1482 {
1483         struct client_obd *cli = &exp->exp_obd->u.cli;
1484         struct lu_client_seq *seq = cli->cl_seq;
1485         ENTRY;
1486         RETURN(seq_client_alloc_fid(seq, fid));
1487 }
1488
1489 /* XXX This method is used only to clear current fid seq
1490  * once fld/mds insert failed */
1491 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1492 {
1493         struct client_obd *cli = &exp->exp_obd->u.cli;
1494         
1495         seq_client_flush(cli->cl_seq);
1496         return 0;
1497 }
1498
1499 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1500 {
1501         struct client_obd *cli = &obd->u.cli;
1502         struct lprocfs_static_vars lvars = { 0 };
1503         int rc;
1504         ENTRY;
1505
1506         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1507         if (!cli->cl_rpc_lock)
1508                 RETURN(-ENOMEM);
1509         mdc_init_rpc_lock(cli->cl_rpc_lock);
1510
1511         ptlrpcd_addref();
1512
1513         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1514         if (!cli->cl_setattr_lock)
1515                 GOTO(err_rpc_lock, rc = -ENOMEM);
1516         mdc_init_rpc_lock(cli->cl_setattr_lock);
1517
1518         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1519         if (!cli->cl_close_lock)
1520                 GOTO(err_setattr_lock, rc = -ENOMEM);
1521         mdc_init_rpc_lock(cli->cl_close_lock);
1522
1523         rc = client_obd_setup(obd, cfg);
1524         if (rc)
1525                 GOTO(err_close_lock, rc);
1526         lprocfs_mdc_init_vars(&lvars);
1527         lprocfs_obd_setup(obd, lvars.obd_vars);
1528         sptlrpc_lprocfs_cliobd_attach(obd);
1529         ptlrpc_lprocfs_register_obd(obd);
1530
1531         rc = obd_llog_init(obd, NULL, obd, 0, NULL, NULL);
1532         if (rc) {
1533                 mdc_cleanup(obd);
1534                 CERROR("failed to setup llogging subsystems\n");
1535         }
1536
1537         RETURN(rc);
1538
1539 err_close_lock:
1540         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1541 err_setattr_lock:
1542         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1543 err_rpc_lock:
1544         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1545         ptlrpcd_decref();
1546         RETURN(rc);
1547 }
1548
1549 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1550  * us to make MDS RPCs with large enough reply buffers to hold the
1551  * maximum-sized (= maximum striped) EA and cookie without having to
1552  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1553 int mdc_init_ea_size(struct obd_export *exp, int easize,
1554                      int def_easize, int cookiesize)
1555 {
1556         struct obd_device *obd = exp->exp_obd;
1557         struct client_obd *cli = &obd->u.cli;
1558         ENTRY;
1559
1560         if (cli->cl_max_mds_easize < easize)
1561                 cli->cl_max_mds_easize = easize;
1562
1563         if (cli->cl_default_mds_easize < def_easize)
1564                 cli->cl_default_mds_easize = def_easize;
1565
1566         if (cli->cl_max_mds_cookiesize < cookiesize)
1567                 cli->cl_max_mds_cookiesize = cookiesize;
1568
1569         RETURN(0);
1570 }
1571
1572 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1573 {
1574         int rc = 0;
1575         ENTRY;
1576
1577         switch (stage) {
1578         case OBD_CLEANUP_EARLY:
1579         case OBD_CLEANUP_EXPORTS:
1580                 /* If we set up but never connected, the
1581                    client import will not have been cleaned. */
1582                 if (obd->u.cli.cl_import) {
1583                         struct obd_import *imp;
1584                         imp = obd->u.cli.cl_import;
1585                         CERROR("client import never connected\n");
1586                         ptlrpc_invalidate_import(imp);
1587                         ptlrpc_free_rq_pool(imp->imp_rq_pool);
1588                         class_destroy_import(imp);
1589                         obd->u.cli.cl_import = NULL;
1590                 }
1591                 break;
1592         case OBD_CLEANUP_SELF_EXP:
1593                 rc = obd_llog_finish(obd, 0);
1594                 if (rc != 0)
1595                         CERROR("failed to cleanup llogging subsystems\n");
1596         case OBD_CLEANUP_OBD:
1597                 break;
1598         }
1599         RETURN(rc);
1600 }
1601
1602 static int mdc_cleanup(struct obd_device *obd)
1603 {
1604         struct client_obd *cli = &obd->u.cli;
1605
1606         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1607         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1608         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1609
1610         ptlrpc_lprocfs_unregister_obd(obd);
1611         lprocfs_obd_cleanup(obd);
1612         ptlrpcd_decref();
1613
1614         return client_obd_cleanup(obd);
1615 }
1616
1617
1618 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
1619                          struct obd_device *tgt,
1620                          int count, struct llog_catid *logid, 
1621                          struct obd_uuid *uuid)
1622 {
1623         struct llog_ctxt *ctxt;
1624         int rc;
1625         ENTRY;
1626
1627         rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1628                         &llog_client_ops);
1629         if (rc == 0) {
1630                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1631                 ctxt->loc_imp = obd->u.cli.cl_import;
1632         }
1633
1634         rc = llog_setup(obd, llogs, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
1635                        &llog_client_ops);
1636         if (rc == 0) {
1637                 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1638                 ctxt->loc_imp = obd->u.cli.cl_import;
1639         }
1640
1641         RETURN(rc);
1642 }
1643
1644 static int mdc_llog_finish(struct obd_device *obd, int count)
1645 {
1646         int rc;
1647         ENTRY;
1648
1649         rc = llog_cleanup(llog_get_context(obd, LLOG_LOVEA_REPL_CTXT));
1650         if (rc) {
1651                 CERROR("can not cleanup LLOG_CONFIG_REPL_CTXT rc %d\n", rc);
1652         }
1653         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
1654         RETURN(rc);
1655 }
1656
1657 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1658 {
1659         struct lustre_cfg *lcfg = buf;
1660         struct lprocfs_static_vars lvars = { 0 };
1661         int rc = 0;
1662
1663         lprocfs_mdc_init_vars(&lvars);
1664
1665         switch (lcfg->lcfg_command) {
1666         case LCFG_SPTLRPC_CONF:
1667                 rc = sptlrpc_cliobd_process_config(obd, lcfg);
1668                 break;
1669         default:
1670                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
1671                                               lcfg, obd);
1672                 break;
1673         }
1674         return(rc);
1675 }
1676
1677 /* get remote permission for current user on fid */
1678 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
1679                         struct obd_capa *oc, __u32 suppgid,
1680                         struct ptlrpc_request **request)
1681 {
1682         struct ptlrpc_request *req;
1683         struct mdt_body *body;
1684         struct mdt_remote_perm *perm;
1685         int size[5] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1686         int rc;
1687         ENTRY;
1688
1689         size[REQ_REC_OFF + 1] = oc ? sizeof(struct lustre_capa) : 0;
1690
1691         *request = NULL;
1692         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1693                               MDS_GETATTR, 3, size, NULL);
1694         if (!req)
1695                 RETURN(-ENOMEM);
1696
1697         mdc_pack_req_body(req, REQ_REC_OFF, OBD_MD_FLRMTPERM, fid, oc, 0,
1698                           suppgid, 0);
1699
1700         size[REPLY_REC_OFF + 1] = sizeof(*perm);
1701         ptlrpc_req_set_repsize(req, 5, size);
1702         rc = ptlrpc_queue_wait(req);
1703         if (rc) {
1704                 ptlrpc_req_finished(req);
1705                 RETURN(rc);
1706         }
1707
1708         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1709                                   lustre_swab_mdt_body);
1710         LASSERT(body);
1711         LASSERT(body->valid & OBD_MD_FLRMTPERM);
1712
1713         perm = lustre_swab_repbuf(req, REPLY_REC_OFF + 1, sizeof(*perm),
1714                                   lustre_swab_mdt_remote_perm);
1715         LASSERT(perm);
1716
1717         *request = req;
1718         RETURN(0);
1719 }
1720
1721 static int mdc_interpret_renew_capa(struct ptlrpc_request *req, void *unused,
1722                                     int status)
1723 {
1724         struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
1725         renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
1726         struct mdt_body *body = NULL;
1727         struct lustre_capa *capa;
1728         ENTRY;
1729
1730         if (status)
1731                 GOTO(out, capa = ERR_PTR(status));
1732
1733         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1734                                   lustre_swab_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 = lustre_unpack_capa(req->rq_repmsg, REPLY_REC_OFF + 1);
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         int size[5] = { sizeof(struct ptlrpc_body),
1755                         sizeof(struct mdt_body),
1756                         sizeof(struct lustre_capa) };
1757         ENTRY;
1758
1759         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1760                               MDS_GETATTR, 3, size, NULL);
1761         if (!req)
1762                 RETURN(-ENOMEM);
1763
1764         mdc_pack_req_body(req, REQ_REC_OFF, OBD_MD_FLOSSCAPA,
1765                           &oc->c_capa.lc_fid, oc, 0, -1, 0);
1766
1767         ptlrpc_req_set_repsize(req, 5, size);
1768         req->rq_async_args.pointer_arg[0] = oc;
1769         req->rq_async_args.pointer_arg[1] = cb;
1770         req->rq_interpret_reply = mdc_interpret_renew_capa;
1771         ptlrpcd_add_req(req);
1772
1773         RETURN(0);
1774 }
1775
1776 static int mdc_connect(const struct lu_env *env,
1777                        struct lustre_handle *dlm_handle,
1778                        struct obd_device *obd, struct obd_uuid *cluuid,
1779                        struct obd_connect_data *data) {
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);
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_get_remote_perm  = mdc_get_remote_perm,
1848         .m_renew_capa       = mdc_renew_capa
1849 };
1850
1851 extern quota_interface_t mdc_quota_interface;
1852
1853 int __init mdc_init(void)
1854 {
1855         int rc;
1856         struct lprocfs_static_vars lvars = { 0 };
1857         lprocfs_mdc_init_vars(&lvars);
1858         
1859         request_module("lquota");
1860         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
1861         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
1862
1863         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1864                                  LUSTRE_MDC_NAME, NULL);
1865         if (rc && quota_interface)
1866                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1867
1868         RETURN(rc);
1869 }
1870
1871 #ifdef __KERNEL__
1872 static void /*__exit*/ mdc_exit(void)
1873 {
1874         if (quota_interface)
1875                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1876
1877         class_unregister_type(LUSTRE_MDC_NAME);
1878 }
1879
1880 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1881 MODULE_DESCRIPTION("Lustre Metadata Client");
1882 MODULE_LICENSE("GPL");
1883
1884 module_init(mdc_init);
1885 module_exit(mdc_exit);
1886 #endif