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