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