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