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