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