Whamcloud - gitweb
215c0af2013d75184ea2531698a50f1391e7995b
[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_ioepoch *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         /**
681          * No need to touch md_open_data::mod_och, it holds a reference on
682          * \var mod and will zero references to each other, \var mod will be
683          * freed after that when md_open_data::mod_och will put the reference.
684          */
685
686         /**
687          * Do not let open request to disappear as it still may be needed
688          * for close rpc to happen (it may happen on evict only, otherwise
689          * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
690          * called), just mark this rpc as committed to distinguish these 2
691          * cases, see mdc_close() for details. The open request reference will
692          * be put along with freeing \var mod.
693          */
694         ptlrpc_request_addref(req);
695         spin_lock(&req->rq_lock);
696         req->rq_committed = 1;
697         spin_unlock(&req->rq_lock);
698         req->rq_cb_data = NULL;
699         obd_mod_put(mod);
700 }
701
702 int mdc_set_open_replay_data(struct obd_export *exp,
703                              struct obd_client_handle *och,
704                              struct ptlrpc_request *open_req)
705 {
706         struct md_open_data   *mod;
707         struct mdt_rec_create *rec;
708         struct mdt_body       *body;
709         struct obd_import     *imp = open_req->rq_import;
710         ENTRY;
711
712         if (!open_req->rq_replay)
713                 RETURN(0);
714
715         rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
716         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
717         LASSERT(rec != NULL);
718         /* Incoming message in my byte order (it's been swabbed). */
719         /* Outgoing messages always in my byte order. */
720         LASSERT(body != NULL);
721
722         /* Only if the import is replayable, we set replay_open data */
723         if (och && imp->imp_replayable) {
724                 mod = obd_mod_alloc();
725                 if (mod == NULL) {
726                         DEBUG_REQ(D_ERROR, open_req,
727                                   "Can't allocate md_open_data");
728                         RETURN(0);
729                 }
730
731                 /**
732                  * Take a reference on \var mod, to be freed on mdc_close().
733                  * It protects \var mod from being freed on eviction (commit
734                  * callback is called despite rq_replay flag).
735                  * Another reference for \var och.
736                  */
737                 obd_mod_get(mod);
738                 obd_mod_get(mod);
739
740                 spin_lock(&open_req->rq_lock);
741                 och->och_mod = mod;
742                 mod->mod_och = och;
743                 mod->mod_open_req = open_req;
744                 open_req->rq_cb_data = mod;
745                 open_req->rq_commit_cb = mdc_commit_open;
746                 spin_unlock(&open_req->rq_lock);
747         }
748
749         rec->cr_fid2 = body->fid1;
750         rec->cr_ioepoch = body->ioepoch;
751         rec->cr_old_handle.cookie = body->handle.cookie;
752         open_req->rq_replay_cb = mdc_replay_open;
753         if (!fid_is_sane(&body->fid1)) {
754                 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
755                           "insane fid");
756                 LBUG();
757         }
758
759         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
760         RETURN(0);
761 }
762
763 int mdc_clear_open_replay_data(struct obd_export *exp,
764                                struct obd_client_handle *och)
765 {
766         struct md_open_data *mod = och->och_mod;
767         ENTRY;
768
769         LASSERT(mod != LP_POISON && mod != NULL);
770
771         mod->mod_och = NULL;
772         och->och_mod = NULL;
773         obd_mod_put(mod);
774
775         RETURN(0);
776 }
777
778 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
779               struct md_open_data *mod, struct ptlrpc_request **request)
780 {
781         struct obd_device     *obd = class_exp2obd(exp);
782         struct ptlrpc_request *req;
783         int                    rc;
784         ENTRY;
785
786         *request = NULL;
787         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
788         if (req == NULL)
789                 RETURN(-ENOMEM);
790
791         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
792
793         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
794         if (rc) {
795                 ptlrpc_request_free(req);
796                 RETURN(rc);
797         }
798
799         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
800          * portal whose threads are not taking any DLM locks and are therefore
801          * always progressing */
802         req->rq_request_portal = MDS_READPAGE_PORTAL;
803         ptlrpc_at_set_req_timeout(req);
804
805         /* Ensure that this close's handle is fixed up during replay. */
806         if (likely(mod != NULL)) {
807                 LASSERTF(mod->mod_open_req != NULL &&
808                          mod->mod_open_req->rq_type != LI_POISON,
809                          "POISONED open %p!\n", mod->mod_open_req);
810
811                 mod->mod_close_req = req;
812
813                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
814                 /* We no longer want to preserve this open for replay even
815                  * though the open was committed. b=3632, b=3633 */
816                 spin_lock(&mod->mod_open_req->rq_lock);
817                 mod->mod_open_req->rq_replay = 0;
818                 spin_unlock(&mod->mod_open_req->rq_lock);
819         } else {
820                  CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
821         }
822
823         mdc_close_pack(req, op_data);
824
825         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
826                              obd->u.cli.cl_max_mds_easize);
827         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
828                              obd->u.cli.cl_max_mds_cookiesize);
829
830         ptlrpc_request_set_replen(req);
831
832         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
833         rc = ptlrpc_queue_wait(req);
834         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
835
836         if (req->rq_repmsg == NULL) {
837                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
838                        req->rq_status);
839                 if (rc == 0)
840                         rc = req->rq_status ?: -EIO;
841         } else if (rc == 0 || rc == -EAGAIN) {
842                 struct mdt_body *body;
843
844                 rc = lustre_msg_get_status(req->rq_repmsg);
845                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
846                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
847                                   "= %d", rc);
848                         if (rc > 0)
849                                 rc = -rc;
850                 }
851                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
852                 if (body == NULL)
853                         rc = -EPROTO;
854         } else if (rc == -ESTALE) {
855                 /**
856                  * it can be allowed error after 3633 if open was committed and
857                  * server failed before close was sent. Let's check if mod
858                  * exists and return no error in that case
859                  */
860                 if (mod) {
861                         LASSERT(mod->mod_open_req != NULL);
862                         if (mod->mod_open_req->rq_committed)
863                                 rc = 0;
864                 }
865         }
866
867         if (mod) {
868                 if (rc != 0)
869                         mod->mod_close_req = NULL;
870                 /* Since now, mod is accessed through open_req only,
871                  * thus close req does not keep a reference on mod anymore. */
872                 obd_mod_put(mod);
873         }
874         *request = req;
875         RETURN(rc);
876 }
877
878 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
879                      struct md_open_data *mod)
880 {
881         struct obd_device     *obd = class_exp2obd(exp);
882         struct ptlrpc_request *req;
883         int                    rc;
884         ENTRY;
885
886         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
887                                    &RQF_MDS_DONE_WRITING);
888         if (req == NULL)
889                 RETURN(-ENOMEM);
890
891         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
892         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
893         if (rc) {
894                 ptlrpc_request_free(req);
895                 RETURN(rc);
896         }
897
898         if (mod != NULL) {
899                 LASSERTF(mod->mod_open_req != NULL &&
900                          mod->mod_open_req->rq_type != LI_POISON,
901                          "POISONED setattr %p!\n", mod->mod_open_req);
902
903                 mod->mod_close_req = req;
904                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
905                 /* We no longer want to preserve this setattr for replay even
906                  * though the open was committed. b=3632, b=3633 */
907                 spin_lock(&mod->mod_open_req->rq_lock);
908                 mod->mod_open_req->rq_replay = 0;
909                 spin_unlock(&mod->mod_open_req->rq_lock);
910         }
911
912         mdc_close_pack(req, op_data);
913         ptlrpc_request_set_replen(req);
914
915         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
916         rc = ptlrpc_queue_wait(req);
917         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
918
919         if (rc == -ESTALE) {
920                 /**
921                  * it can be allowed error after 3633 if open or setattr were
922                  * committed and server failed before close was sent.
923                  * Let's check if mod exists and return no error in that case
924                  */
925                 if (mod) {
926                         LASSERT(mod->mod_open_req != NULL);
927                         if (mod->mod_open_req->rq_committed)
928                                 rc = 0;
929                 }
930         }
931
932         if (mod) {
933                 if (rc != 0)
934                         mod->mod_close_req = NULL;
935                 /* Since now, mod is accessed through setattr req only,
936                  * thus DW req does not keep a reference on mod anymore. */
937                 obd_mod_put(mod);
938         }
939         ptlrpc_req_finished(req);
940         RETURN(rc);
941 }
942
943 #ifdef HAVE_SPLIT_SUPPORT
944 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
945                  const struct page *page, int offset)
946 {
947         struct ptlrpc_request   *req;
948         struct ptlrpc_bulk_desc *desc;
949         int                      rc;
950         ENTRY;
951
952         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
953         if (req == NULL)
954                 RETURN(-ENOMEM);
955
956         /* FIXME: capa doesn't support split yet */
957         mdc_set_capa_size(req, &RMF_CAPA1, NULL);
958
959         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
960         if (rc) {
961                 ptlrpc_request_free(req);
962                 RETURN(rc);
963         }
964
965         req->rq_request_portal = MDS_READPAGE_PORTAL;
966         ptlrpc_at_set_req_timeout(req);
967
968         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
969         if (desc == NULL)
970                 GOTO(out, rc = -ENOMEM);
971
972         /* NB req now owns desc and will free it when it gets freed. */
973         ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
974         mdc_readdir_pack(req, 0, offset, fid, NULL);
975
976         ptlrpc_request_set_replen(req);
977         rc = ptlrpc_queue_wait(req);
978         if (rc)
979                 GOTO(out, rc);
980
981         rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
982 out:
983         ptlrpc_req_finished(req);
984         return rc;
985 }
986 EXPORT_SYMBOL(mdc_sendpage);
987 #endif
988
989 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
990                  struct obd_capa *oc, __u64 offset, struct page *page,
991                  struct ptlrpc_request **request)
992 {
993         struct ptlrpc_request   *req;
994         struct ptlrpc_bulk_desc *desc;
995         int                      rc;
996         ENTRY;
997
998         *request = NULL;
999         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1000         if (req == NULL)
1001                 RETURN(-ENOMEM);
1002
1003         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1004
1005         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1006         if (rc) {
1007                 ptlrpc_request_free(req);
1008                 RETURN(rc);
1009         }
1010
1011         req->rq_request_portal = MDS_READPAGE_PORTAL;
1012         ptlrpc_at_set_req_timeout(req);
1013
1014         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1015         if (desc == NULL) {
1016                 ptlrpc_request_free(req);
1017                 RETURN(-ENOMEM);
1018         }
1019
1020         /* NB req now owns desc and will free it when it gets freed */
1021         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1022         mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
1023
1024         ptlrpc_request_set_replen(req);
1025         rc = ptlrpc_queue_wait(req);
1026         if (rc) {
1027                 ptlrpc_req_finished(req);
1028                 RETURN(rc);
1029         }
1030
1031         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1032                                           req->rq_bulk->bd_nob_transferred);
1033         if (rc < 0) {
1034                 ptlrpc_req_finished(req);
1035                 RETURN(rc);
1036         }
1037
1038         if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1039                 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1040                         req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1041                 ptlrpc_req_finished(req);
1042                 RETURN(-EPROTO);
1043         }
1044
1045         *request = req;
1046         RETURN(0);
1047 }
1048
1049 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1050 {
1051         __u32 keylen, vallen;
1052         void *key;
1053         int rc;
1054
1055         if (gf->gf_pathlen > PATH_MAX)
1056                 RETURN(-ENAMETOOLONG);
1057         if (gf->gf_pathlen < 2)
1058                 RETURN(-EOVERFLOW);
1059
1060         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1061         keylen = size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1062         OBD_ALLOC(key, keylen);
1063         if (key == NULL)
1064                 RETURN(-ENOMEM);
1065         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1066         memcpy(key + size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1067
1068         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1069                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1070
1071         if (!fid_is_sane(&gf->gf_fid))
1072                 GOTO(out, rc = -EINVAL);
1073
1074         /* Val is struct getinfo_fid2path result plus path */
1075         vallen = sizeof(*gf) + gf->gf_pathlen;
1076
1077         rc = obd_get_info(exp, keylen, key, &vallen, gf, NULL);
1078         if (rc)
1079                 GOTO(out, rc);
1080
1081         if (vallen <= sizeof(*gf))
1082                 GOTO(out, rc = -EPROTO);
1083         else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1084                 GOTO(out, rc = -EOVERFLOW);
1085
1086         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1087                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1088
1089 out:
1090         OBD_FREE(key, keylen);
1091         return rc;
1092 }
1093
1094 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1095                          void *karg, void *uarg)
1096 {
1097         struct obd_device *obd = exp->exp_obd;
1098         struct obd_ioctl_data *data = karg;
1099         struct obd_import *imp = obd->u.cli.cl_import;
1100         struct llog_ctxt *ctxt;
1101         int rc;
1102         ENTRY;
1103
1104         if (!try_module_get(THIS_MODULE)) {
1105                 CERROR("Can't get module. Is it alive?");
1106                 return -EINVAL;
1107         }
1108         switch (cmd) {
1109         case OBD_IOC_CHANGELOG_CLEAR: {
1110                 struct ioc_changelog_clear *icc = karg;
1111                 struct changelog_setinfo cs =
1112                         {icc->icc_recno, icc->icc_id};
1113                 rc = obd_set_info_async(exp, strlen(KEY_CHANGELOG_CLEAR),
1114                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1115                                         NULL);
1116                 GOTO(out, rc);
1117         }
1118         case OBD_IOC_FID2PATH: {
1119                 rc = mdc_ioc_fid2path(exp, karg);
1120                 GOTO(out, rc);
1121         }
1122         case OBD_IOC_CLIENT_RECOVER:
1123                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1124                 if (rc < 0)
1125                         GOTO(out, rc);
1126                 GOTO(out, rc = 0);
1127         case IOC_OSC_SET_ACTIVE:
1128                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1129                 GOTO(out, rc);
1130         case OBD_IOC_PARSE: {
1131                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1132                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1133                 llog_ctxt_put(ctxt);
1134                 GOTO(out, rc);
1135         }
1136 #ifdef __KERNEL__
1137         case OBD_IOC_LLOG_INFO:
1138         case OBD_IOC_LLOG_PRINT: {
1139                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1140                 rc = llog_ioctl(ctxt, cmd, data);
1141                 llog_ctxt_put(ctxt);
1142                 GOTO(out, rc);
1143         }
1144 #endif
1145         case OBD_IOC_POLL_QUOTACHECK:
1146                 rc = lquota_poll_check(quota_interface, exp,
1147                                        (struct if_quotacheck *)karg);
1148                 GOTO(out, rc);
1149         case OBD_IOC_PING_TARGET:
1150                 rc = ptlrpc_obd_ping(obd);
1151                 GOTO(out, rc);
1152         default:
1153                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1154                 GOTO(out, rc = -ENOTTY);
1155         }
1156 out:
1157         module_put(THIS_MODULE);
1158
1159         return rc;
1160 }
1161
1162 int mdc_get_info_rpc(struct obd_export *exp,
1163                      obd_count keylen, void *key,
1164                      int vallen, void *val)
1165 {
1166         struct obd_import      *imp = class_exp2cliimp(exp);
1167         struct ptlrpc_request  *req;
1168         char                   *tmp;
1169         int                     rc = -EINVAL;
1170         ENTRY;
1171
1172         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1173         if (req == NULL)
1174                 RETURN(-ENOMEM);
1175
1176         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1177                              RCL_CLIENT, keylen);
1178         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1179                              RCL_CLIENT, sizeof(__u32));
1180
1181         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1182         if (rc) {
1183                 ptlrpc_request_free(req);
1184                 RETURN(rc);
1185         }
1186
1187         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1188         memcpy(tmp, key, keylen);
1189         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1190         memcpy(tmp, &vallen, sizeof(__u32));
1191
1192         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1193                              RCL_SERVER, vallen);
1194         ptlrpc_request_set_replen(req);
1195
1196         rc = ptlrpc_queue_wait(req);
1197         if (rc == 0) {
1198                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1199                 memcpy(val, tmp, vallen);
1200                 if (ptlrpc_rep_need_swab(req)) {
1201                         if (KEY_IS(KEY_FID2PATH)) {
1202                                 lustre_swab_fid2path(val);
1203                         }
1204                 }
1205         }
1206         ptlrpc_req_finished(req);
1207
1208         RETURN(rc);
1209 }
1210
1211 static void lustre_swab_hai(struct hsm_action_item *h)
1212 {
1213         __swab32s(&h->hai_len);
1214         __swab32s(&h->hai_action);
1215         lustre_swab_lu_fid(&h->hai_fid);
1216         __swab64s(&h->hai_cookie);
1217         __swab64s(&h->hai_extent_start);
1218         __swab64s(&h->hai_extent_end);
1219         __swab64s(&h->hai_gid);
1220 }
1221
1222 static void lustre_swab_hal(struct hsm_action_list *h)
1223 {
1224         struct hsm_action_item *hai;
1225         int i;
1226
1227         __swab32s(&h->hal_version);
1228         __swab32s(&h->hal_count);
1229         __swab32s(&h->hal_archive_num);
1230         hai = hai_zero(h);
1231         for (i = 0; i < h->hal_count; i++) {
1232                 lustre_swab_hai(hai);
1233                 hai = hai_next(hai);
1234         }
1235 }
1236
1237 /**
1238  * Send a message to any listening copytools, nonblocking
1239  * @param val LNL message (lnl_hdr + hsm_action_list)
1240  * @param len total length of message
1241  */
1242 static int mdc_hsm_copytool_send(int len, void *val)
1243 {
1244         struct lnl_hdr *lh = (struct lnl_hdr *)val;
1245         struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1246         int rc;
1247         ENTRY;
1248
1249         if (len < sizeof(*lh) + sizeof(*hal)) {
1250                 CERROR("Short HSM message %d < %d\n", len,
1251                       (int) (sizeof(*lh) + sizeof(*hal)));
1252                 RETURN(-EPROTO);
1253         }
1254         if (lh->lnl_magic == __swab16(LNL_MAGIC)) {
1255                 lustre_swab_lnlh(lh);
1256                 lustre_swab_hal(hal);
1257         } else if (lh->lnl_magic != LNL_MAGIC) {
1258                 CERROR("Bad magic %x!=%x\n", lh->lnl_magic, LNL_MAGIC);
1259                 RETURN(-EPROTO);
1260         }
1261
1262         CDEBUG(D_IOCTL, " Received message mg=%x t=%d m=%d l=%d actions=%d\n",
1263                lh->lnl_magic, lh->lnl_transport, lh->lnl_msgtype,
1264                lh->lnl_msglen, hal->hal_count);
1265
1266         /* Broadcast to HSM listeners */
1267         rc = libcfs_klnl_msg_put(0, LNL_GRP_HSM, lh);
1268
1269         RETURN(rc);
1270 }
1271
1272 int mdc_set_info_async(struct obd_export *exp,
1273                        obd_count keylen, void *key,
1274                        obd_count vallen, void *val,
1275                        struct ptlrpc_request_set *set)
1276 {
1277         struct obd_import *imp = class_exp2cliimp(exp);
1278         int                rc = -EINVAL;
1279         ENTRY;
1280
1281         if (KEY_IS(KEY_INIT_RECOV)) {
1282                 if (vallen != sizeof(int))
1283                         RETURN(-EINVAL);
1284                 spin_lock(&imp->imp_lock);
1285                 imp->imp_initial_recov = *(int *)val;
1286                 spin_unlock(&imp->imp_lock);
1287                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1288                        exp->exp_obd->obd_name, imp->imp_initial_recov);
1289                 RETURN(0);
1290         }
1291         /* Turn off initial_recov after we try all backup servers once */
1292         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1293                 if (vallen != sizeof(int))
1294                         RETURN(-EINVAL);
1295                 spin_lock(&imp->imp_lock);
1296                 imp->imp_initial_recov_bk = *(int *)val;
1297                 if (imp->imp_initial_recov_bk)
1298                         imp->imp_initial_recov = 1;
1299                 spin_unlock(&imp->imp_lock);
1300                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1301                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1302                 RETURN(0);
1303         }
1304         if (KEY_IS(KEY_READ_ONLY)) {
1305                 if (vallen != sizeof(int))
1306                         RETURN(-EINVAL);
1307
1308                 spin_lock(&imp->imp_lock);
1309                 if (*((int *)val)) {
1310                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1311                         imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1312                 } else {
1313                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1314                         imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1315                 }
1316                 spin_unlock(&imp->imp_lock);
1317
1318                 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1319                                          keylen, key, vallen, val, set);
1320                 RETURN(rc);
1321         }
1322         if (KEY_IS(KEY_SPTLRPC_CONF)) {
1323                 sptlrpc_conf_client_adapt(exp->exp_obd);
1324                 RETURN(0);
1325         }
1326         if (KEY_IS(KEY_FLUSH_CTX)) {
1327                 sptlrpc_import_flush_my_ctx(imp);
1328                 RETURN(0);
1329         }
1330         if (KEY_IS(KEY_MDS_CONN)) {
1331                 /* mds-mds import */
1332                 spin_lock(&imp->imp_lock);
1333                 imp->imp_server_timeout = 1;
1334                 spin_unlock(&imp->imp_lock);
1335                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1336                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1337                 RETURN(0);
1338         }
1339         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1340                 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1341                                          keylen, key, vallen, val, set);
1342                 RETURN(rc);
1343         }
1344         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
1345                 rc = mdc_hsm_copytool_send(vallen, val);
1346                 RETURN(rc);
1347         }
1348
1349         RETURN(rc);
1350 }
1351
1352 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1353                  __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1354 {
1355         int rc = -EINVAL;
1356
1357         if (KEY_IS(KEY_MAX_EASIZE)) {
1358                 int mdsize, *max_easize;
1359
1360                 if (*vallen != sizeof(int))
1361                         RETURN(-EINVAL);
1362                 mdsize = *(int*)val;
1363                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1364                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1365                 max_easize = val;
1366                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1367                 RETURN(0);
1368         }
1369         if (KEY_IS(KEY_CONN_DATA)) {
1370                 struct obd_import *imp = class_exp2cliimp(exp);
1371                 struct obd_connect_data *data = val;
1372
1373                 if (*vallen != sizeof(*data))
1374                         RETURN(-EINVAL);
1375
1376                 *data = imp->imp_connect_data;
1377                 RETURN(0);
1378         }
1379
1380         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
1381
1382         RETURN(rc);
1383 }
1384
1385 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1386                       __u64 max_age, __u32 flags)
1387 {
1388         struct ptlrpc_request *req;
1389         struct obd_statfs     *msfs;
1390         struct obd_import     *imp = NULL;
1391         int                    rc;
1392         ENTRY;
1393
1394
1395         /*Since the request might also come from lprocfs, so we need
1396          *sync this with client_disconnect_export Bug15684*/
1397         down_read(&obd->u.cli.cl_sem);
1398         if (obd->u.cli.cl_import)
1399                 imp = class_import_get(obd->u.cli.cl_import);
1400         up_read(&obd->u.cli.cl_sem);
1401         if (!imp)
1402                 RETURN(-ENODEV);
1403
1404         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1405                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1406         if (req == NULL)
1407                 GOTO(output, rc = -ENOMEM);
1408
1409         ptlrpc_request_set_replen(req);
1410
1411         if (flags & OBD_STATFS_NODELAY) {
1412                 /* procfs requests not want stay in wait for avoid deadlock */
1413                 req->rq_no_resend = 1;
1414                 req->rq_no_delay = 1;
1415         }
1416
1417         rc = ptlrpc_queue_wait(req);
1418         if (rc) {
1419                 /* check connection error first */
1420                 if (imp->imp_connect_error)
1421                         rc = imp->imp_connect_error;
1422                 GOTO(out, rc);
1423         }
1424
1425         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1426         if (msfs == NULL)
1427                 GOTO(out, rc = -EPROTO);
1428
1429         *osfs = *msfs;
1430         EXIT;
1431 out:
1432         ptlrpc_req_finished(req);
1433 output:
1434         class_import_put(imp);
1435         return rc;
1436 }
1437
1438 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1439                    struct obd_capa *oc, struct obd_client_handle *handle,
1440                    int flags)
1441 {
1442         struct ptlrpc_request *req;
1443         struct mdt_body       *body;
1444         int                    rc;
1445         ENTRY;
1446
1447         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1448         if (req == NULL)
1449                 RETURN(-ENOMEM);
1450
1451         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1452
1453         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1454         if (rc) {
1455                 ptlrpc_request_free(req);
1456                 RETURN(rc);
1457         }
1458
1459         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1460
1461         ptlrpc_request_set_replen(req);
1462
1463         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1464         rc = ptlrpc_queue_wait(req);
1465         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1466         if (rc) {
1467                 CERROR("Pin failed: %d\n", rc);
1468                 GOTO(err_out, rc);
1469         }
1470
1471         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1472         if (body == NULL)
1473                 GOTO(err_out, rc = -EPROTO);
1474
1475         handle->och_fh = body->handle;
1476         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1477
1478         handle->och_mod = obd_mod_alloc();
1479         if (handle->och_mod == NULL) {
1480                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1481                 GOTO(err_out, rc = -ENOMEM);
1482         }
1483         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1484
1485         RETURN(0);
1486
1487 err_out:
1488         ptlrpc_req_finished(req);
1489         RETURN(rc);
1490 }
1491
1492 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1493                      int flag)
1494 {
1495         struct ptlrpc_request *req;
1496         struct mdt_body       *body;
1497         int                    rc;
1498         ENTRY;
1499
1500         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1501                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
1502         if (req == NULL)
1503                 RETURN(-ENOMEM);
1504
1505         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1506         body->handle = handle->och_fh;
1507         body->flags = flag;
1508
1509         ptlrpc_request_set_replen(req);
1510
1511         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1512         rc = ptlrpc_queue_wait(req);
1513         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1514
1515         if (rc != 0)
1516                 CERROR("Unpin failed: %d\n", rc);
1517
1518         ptlrpc_req_finished(req);
1519         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1520
1521         obd_mod_put(handle->och_mod);
1522         RETURN(rc);
1523 }
1524
1525 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1526              struct obd_capa *oc, struct ptlrpc_request **request)
1527 {
1528         struct ptlrpc_request *req;
1529         int                    rc;
1530         ENTRY;
1531
1532         *request = NULL;
1533         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1534         if (req == NULL)
1535                 RETURN(-ENOMEM);
1536
1537         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1538
1539         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1540         if (rc) {
1541                 ptlrpc_request_free(req);
1542                 RETURN(rc);
1543         }
1544
1545         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1546
1547         ptlrpc_request_set_replen(req);
1548
1549         rc = ptlrpc_queue_wait(req);
1550         if (rc)
1551                 ptlrpc_req_finished(req);
1552         else
1553                 *request = req;
1554         RETURN(rc);
1555 }
1556
1557 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1558                             enum obd_import_event event)
1559 {
1560         int rc = 0;
1561
1562         LASSERT(imp->imp_obd == obd);
1563
1564         switch (event) {
1565         case IMP_EVENT_DISCON: {
1566 #if 0
1567                 /* XXX Pass event up to OBDs stack. used only for FLD now */
1568                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1569 #endif
1570                 break;
1571         }
1572         case IMP_EVENT_INACTIVE: {
1573                 struct client_obd *cli = &obd->u.cli;
1574                 /*
1575                  * Flush current sequence to make client obtain new one
1576                  * from server in case of disconnect/reconnect.
1577                  * If range is already empty then no need to flush it.
1578                  */
1579                 if (cli->cl_seq != NULL &&
1580                     !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1581                         seq_client_flush(cli->cl_seq);
1582                 }
1583
1584                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1585                 break;
1586         }
1587         case IMP_EVENT_INVALIDATE: {
1588                 struct ldlm_namespace *ns = obd->obd_namespace;
1589
1590                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1591
1592                 break;
1593         }
1594         case IMP_EVENT_ACTIVE: {
1595                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1596                 break;
1597         }
1598         case IMP_EVENT_OCD:
1599                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1600                 break;
1601
1602         default:
1603                 CERROR("Unknown import event %x\n", event);
1604                 LBUG();
1605         }
1606         RETURN(rc);
1607 }
1608
1609 static int mdc_fid_init(struct obd_export *exp)
1610 {
1611         struct client_obd *cli = &exp->exp_obd->u.cli;
1612         char *prefix;
1613         int rc;
1614         ENTRY;
1615
1616         OBD_ALLOC_PTR(cli->cl_seq);
1617         if (cli->cl_seq == NULL)
1618                 RETURN(-ENOMEM);
1619
1620         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1621         if (prefix == NULL)
1622                 GOTO(out_free_seq, rc = -ENOMEM);
1623
1624         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1625                  exp->exp_obd->obd_name);
1626
1627         /* Init client side sequence-manager */
1628         rc = seq_client_init(cli->cl_seq, exp,
1629                              LUSTRE_SEQ_METADATA,
1630                              prefix, NULL);
1631         OBD_FREE(prefix, MAX_OBD_NAME + 5);
1632         if (rc)
1633                 GOTO(out_free_seq, rc);
1634
1635         RETURN(rc);
1636 out_free_seq:
1637         OBD_FREE_PTR(cli->cl_seq);
1638         cli->cl_seq = NULL;
1639         return rc;
1640 }
1641
1642 static int mdc_fid_fini(struct obd_export *exp)
1643 {
1644         struct client_obd *cli = &exp->exp_obd->u.cli;
1645         ENTRY;
1646
1647         if (cli->cl_seq != NULL) {
1648                 seq_client_fini(cli->cl_seq);
1649                 OBD_FREE_PTR(cli->cl_seq);
1650                 cli->cl_seq = NULL;
1651         }
1652
1653         RETURN(0);
1654 }
1655
1656 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1657                   struct md_op_data *op_data)
1658 {
1659         struct client_obd *cli = &exp->exp_obd->u.cli;
1660         struct lu_client_seq *seq = cli->cl_seq;
1661         ENTRY;
1662         RETURN(seq_client_alloc_fid(seq, fid));
1663 }
1664
1665 /* XXX This method is used only to clear current fid seq
1666  * once fld/mds insert failed */
1667 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1668 {
1669         struct client_obd *cli = &exp->exp_obd->u.cli;
1670
1671         seq_client_flush(cli->cl_seq);
1672         return 0;
1673 }
1674
1675 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
1676         struct client_obd *cli = &exp->exp_obd->u.cli;
1677         return &cli->cl_target_uuid;
1678 }
1679
1680 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1681 {
1682         struct client_obd *cli = &obd->u.cli;
1683         struct lprocfs_static_vars lvars = { 0 };
1684         int rc;
1685         ENTRY;
1686
1687         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1688         if (!cli->cl_rpc_lock)
1689                 RETURN(-ENOMEM);
1690         mdc_init_rpc_lock(cli->cl_rpc_lock);
1691
1692         ptlrpcd_addref();
1693
1694         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1695         if (!cli->cl_setattr_lock)
1696                 GOTO(err_rpc_lock, rc = -ENOMEM);
1697         mdc_init_rpc_lock(cli->cl_setattr_lock);
1698
1699         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1700         if (!cli->cl_close_lock)
1701                 GOTO(err_setattr_lock, rc = -ENOMEM);
1702         mdc_init_rpc_lock(cli->cl_close_lock);
1703
1704         rc = client_obd_setup(obd, cfg);
1705         if (rc)
1706                 GOTO(err_close_lock, rc);
1707         lprocfs_mdc_init_vars(&lvars);
1708         lprocfs_obd_setup(obd, lvars.obd_vars);
1709         sptlrpc_lprocfs_cliobd_attach(obd);
1710         ptlrpc_lprocfs_register_obd(obd);
1711
1712         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
1713         if (rc) {
1714                 mdc_cleanup(obd);
1715                 CERROR("failed to setup llogging subsystems\n");
1716         }
1717
1718         /* ignore errors */
1719         libcfs_klnl_start(LNL_TRANSPORT_HSM);
1720         libcfs_klnl_start(LNL_TRANSPORT_CHANGELOG);
1721
1722         RETURN(rc);
1723
1724 err_close_lock:
1725         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1726 err_setattr_lock:
1727         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1728 err_rpc_lock:
1729         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1730         ptlrpcd_decref();
1731         RETURN(rc);
1732 }
1733
1734 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1735  * us to make MDS RPCs with large enough reply buffers to hold the
1736  * maximum-sized (= maximum striped) EA and cookie without having to
1737  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1738 static int mdc_init_ea_size(struct obd_export *exp, int easize,
1739                      int def_easize, int cookiesize)
1740 {
1741         struct obd_device *obd = exp->exp_obd;
1742         struct client_obd *cli = &obd->u.cli;
1743         ENTRY;
1744
1745         if (cli->cl_max_mds_easize < easize)
1746                 cli->cl_max_mds_easize = easize;
1747
1748         if (cli->cl_default_mds_easize < def_easize)
1749                 cli->cl_default_mds_easize = def_easize;
1750
1751         if (cli->cl_max_mds_cookiesize < cookiesize)
1752                 cli->cl_max_mds_cookiesize = cookiesize;
1753
1754         RETURN(0);
1755 }
1756
1757 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1758 {
1759         int rc = 0;
1760         ENTRY;
1761
1762         switch (stage) {
1763         case OBD_CLEANUP_EARLY:
1764         case OBD_CLEANUP_EXPORTS:
1765                 /* If we set up but never connected, the
1766                    client import will not have been cleaned. */
1767                 if (obd->u.cli.cl_import) {
1768                         struct obd_import *imp;
1769                         down_write(&obd->u.cli.cl_sem);
1770                         imp = obd->u.cli.cl_import;
1771                         CERROR("client import never connected\n");
1772                         ptlrpc_invalidate_import(imp);
1773                         class_destroy_import(imp);
1774                         up_write(&obd->u.cli.cl_sem);
1775                         obd->u.cli.cl_import = NULL;
1776                 }
1777                 rc = obd_llog_finish(obd, 0);
1778                 if (rc != 0)
1779                         CERROR("failed to cleanup llogging subsystems\n");
1780                 break;
1781         }
1782         RETURN(rc);
1783 }
1784
1785 static int mdc_cleanup(struct obd_device *obd)
1786 {
1787         struct client_obd *cli = &obd->u.cli;
1788
1789         libcfs_klnl_stop(LNL_TRANSPORT_HSM, LNL_GRP_HSM);
1790         libcfs_klnl_stop(LNL_TRANSPORT_CHANGELOG, 0);
1791
1792         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1793         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1794         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1795
1796         ptlrpc_lprocfs_unregister_obd(obd);
1797         lprocfs_obd_cleanup(obd);
1798         ptlrpcd_decref();
1799
1800         return client_obd_cleanup(obd);
1801 }
1802
1803
1804 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1805                          struct obd_device *tgt, int *index)
1806 {
1807         struct llog_ctxt *ctxt;
1808         int rc;
1809         ENTRY;
1810
1811         LASSERT(olg == &obd->obd_olg);
1812
1813         rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
1814                         &llog_client_ops);
1815         if (rc)
1816                 RETURN(rc);
1817
1818         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1819         llog_initiator_connect(ctxt);
1820         llog_ctxt_put(ctxt);
1821
1822         rc = llog_setup(obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt, 0, NULL,
1823                         &llog_client_ops);
1824         if (rc == 0) {
1825                 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
1826                 llog_initiator_connect(ctxt);
1827                 llog_ctxt_put(ctxt);
1828         }
1829
1830         RETURN(rc);
1831 }
1832
1833 static int mdc_llog_finish(struct obd_device *obd, int count)
1834 {
1835         struct llog_ctxt *ctxt;
1836         int rc = 0;
1837         ENTRY;
1838
1839         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1840         if (ctxt)
1841                 rc = llog_cleanup(ctxt);
1842
1843         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
1844         if (ctxt)
1845                 rc = llog_cleanup(ctxt);
1846
1847         RETURN(rc);
1848 }
1849
1850 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1851 {
1852         struct lustre_cfg *lcfg = buf;
1853         struct lprocfs_static_vars lvars = { 0 };
1854         int rc = 0;
1855
1856         lprocfs_mdc_init_vars(&lvars);
1857         switch (lcfg->lcfg_command) {
1858         default:
1859                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
1860                                               lcfg, obd);
1861                 if (rc > 0)
1862                         rc = 0;
1863                 break;
1864         }
1865         return(rc);
1866 }
1867
1868
1869 /* get remote permission for current user on fid */
1870 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
1871                         struct obd_capa *oc, __u32 suppgid,
1872                         struct ptlrpc_request **request)
1873 {
1874         struct ptlrpc_request  *req;
1875         int                    rc;
1876         ENTRY;
1877
1878         LASSERT(client_is_remote(exp));
1879
1880         *request = NULL;
1881         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
1882         if (req == NULL)
1883                 RETURN(-ENOMEM);
1884
1885         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1886
1887         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
1888         if (rc) {
1889                 ptlrpc_request_free(req);
1890                 RETURN(rc);
1891         }
1892
1893         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
1894
1895         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
1896                              sizeof(struct mdt_remote_perm));
1897
1898         ptlrpc_request_set_replen(req);
1899
1900         rc = ptlrpc_queue_wait(req);
1901         if (rc)
1902                 ptlrpc_req_finished(req);
1903         else
1904                 *request = req;
1905         RETURN(rc);
1906 }
1907
1908 static int mdc_interpret_renew_capa(const struct lu_env *env,
1909                                     struct ptlrpc_request *req, void *unused,
1910                                     int status)
1911 {
1912         struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
1913         renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
1914         struct mdt_body *body = NULL;
1915         struct lustre_capa *capa;
1916         ENTRY;
1917
1918         if (status)
1919                 GOTO(out, capa = ERR_PTR(status));
1920
1921         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1922         if (body == NULL)
1923                 GOTO(out, capa = ERR_PTR(-EFAULT));
1924
1925         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
1926                 GOTO(out, capa = ERR_PTR(-ENOENT));
1927
1928         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
1929         if (!capa)
1930                 GOTO(out, capa = ERR_PTR(-EFAULT));
1931         EXIT;
1932 out:
1933         cb(oc, capa);
1934         return 0;
1935 }
1936
1937 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
1938                           renew_capa_cb_t cb)
1939 {
1940         struct ptlrpc_request *req;
1941         ENTRY;
1942
1943         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
1944                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
1945         if (req == NULL)
1946                 RETURN(-ENOMEM);
1947
1948         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
1949          * capa to renew is oss capa.
1950          */
1951         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
1952         ptlrpc_request_set_replen(req);
1953
1954         req->rq_async_args.pointer_arg[0] = oc;
1955         req->rq_async_args.pointer_arg[1] = cb;
1956         req->rq_interpret_reply = mdc_interpret_renew_capa;
1957         ptlrpcd_add_req(req, PSCOPE_OTHER);
1958         RETURN(0);
1959 }
1960
1961 static int mdc_connect(const struct lu_env *env,
1962                        struct obd_export **exp,
1963                        struct obd_device *obd, struct obd_uuid *cluuid,
1964                        struct obd_connect_data *data,
1965                        void *localdata)
1966 {
1967         struct obd_import *imp = obd->u.cli.cl_import;
1968
1969         /* mds-mds import features */
1970         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
1971                 spin_lock(&imp->imp_lock);
1972                 imp->imp_server_timeout = 1;
1973                 spin_unlock(&imp->imp_lock);
1974                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1975                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
1976                        obd->obd_name);
1977         }
1978
1979         return client_connect_import(env, exp, obd, cluuid, data, NULL);
1980 }
1981
1982 struct obd_ops mdc_obd_ops = {
1983         .o_owner            = THIS_MODULE,
1984         .o_setup            = mdc_setup,
1985         .o_precleanup       = mdc_precleanup,
1986         .o_cleanup          = mdc_cleanup,
1987         .o_add_conn         = client_import_add_conn,
1988         .o_del_conn         = client_import_del_conn,
1989         .o_connect          = mdc_connect,
1990         .o_disconnect       = client_disconnect_export,
1991         .o_iocontrol        = mdc_iocontrol,
1992         .o_set_info_async   = mdc_set_info_async,
1993         .o_statfs           = mdc_statfs,
1994         .o_pin              = mdc_pin,
1995         .o_unpin            = mdc_unpin,
1996         .o_fid_init         = mdc_fid_init,
1997         .o_fid_fini         = mdc_fid_fini,
1998         .o_fid_alloc        = mdc_fid_alloc,
1999         .o_fid_delete       = mdc_fid_delete,
2000         .o_import_event     = mdc_import_event,
2001         .o_llog_init        = mdc_llog_init,
2002         .o_llog_finish      = mdc_llog_finish,
2003         .o_get_info         = mdc_get_info,
2004         .o_process_config   = mdc_process_config,
2005         .o_get_uuid         = mdc_get_uuid,
2006 };
2007
2008 struct md_ops mdc_md_ops = {
2009         .m_getstatus        = mdc_getstatus,
2010         .m_change_cbdata    = mdc_change_cbdata,
2011         .m_close            = mdc_close,
2012         .m_create           = mdc_create,
2013         .m_done_writing     = mdc_done_writing,
2014         .m_enqueue          = mdc_enqueue,
2015         .m_getattr          = mdc_getattr,
2016         .m_getattr_name     = mdc_getattr_name,
2017         .m_intent_lock      = mdc_intent_lock,
2018         .m_link             = mdc_link,
2019         .m_is_subdir        = mdc_is_subdir,
2020         .m_rename           = mdc_rename,
2021         .m_setattr          = mdc_setattr,
2022         .m_setxattr         = mdc_setxattr,
2023         .m_getxattr         = mdc_getxattr,
2024         .m_sync             = mdc_sync,
2025         .m_readpage         = mdc_readpage,
2026         .m_unlink           = mdc_unlink,
2027         .m_cancel_unused    = mdc_cancel_unused,
2028         .m_init_ea_size     = mdc_init_ea_size,
2029         .m_set_lock_data    = mdc_set_lock_data,
2030         .m_lock_match       = mdc_lock_match,
2031         .m_get_lustre_md    = mdc_get_lustre_md,
2032         .m_free_lustre_md   = mdc_free_lustre_md,
2033         .m_set_open_replay_data = mdc_set_open_replay_data,
2034         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2035         .m_renew_capa       = mdc_renew_capa,
2036         .m_unpack_capa      = mdc_unpack_capa,
2037         .m_get_remote_perm  = mdc_get_remote_perm,
2038         .m_intent_getattr_async = mdc_intent_getattr_async,
2039         .m_revalidate_lock      = mdc_revalidate_lock
2040 };
2041
2042 int __init mdc_init(void)
2043 {
2044         int rc;
2045         struct lprocfs_static_vars lvars = { 0 };
2046         lprocfs_mdc_init_vars(&lvars);
2047
2048         request_module("lquota");
2049         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
2050         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
2051
2052         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2053                                  LUSTRE_MDC_NAME, NULL);
2054         if (rc && quota_interface)
2055                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2056
2057         RETURN(rc);
2058 }
2059
2060 #ifdef __KERNEL__
2061 static void /*__exit*/ mdc_exit(void)
2062 {
2063         if (quota_interface)
2064                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2065
2066         class_unregister_type(LUSTRE_MDC_NAME);
2067 }
2068
2069 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2070 MODULE_DESCRIPTION("Lustre Metadata Client");
2071 MODULE_LICENSE("GPL");
2072
2073 module_init(mdc_init);
2074 module_exit(mdc_exit);
2075 #endif