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