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