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