]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/mdc/mdc_request.c
Merge tag 'squashfs-updates' of git://git.kernel.org/pub/scm/linux/kernel/git/pkl...
[~andy/linux] / drivers / staging / lustre / lustre / mdc / mdc_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_MDC
38
39 # include <linux/module.h>
40 # include <linux/pagemap.h>
41 # include <linux/miscdevice.h>
42 # include <linux/init.h>
43 # include <linux/utsname.h>
44
45 #include <lustre_acl.h>
46 #include <obd_class.h>
47 #include <lustre_fid.h>
48 #include <lprocfs_status.h>
49 #include <lustre_param.h>
50 #include <lustre_log.h>
51
52 #include "mdc_internal.h"
53
54 #define REQUEST_MINOR 244
55
56 struct mdc_renew_capa_args {
57         struct obd_capa *ra_oc;
58         renew_capa_cb_t  ra_cb;
59 };
60
61 static int mdc_cleanup(struct obd_device *obd);
62
63 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
64                     const struct req_msg_field *field, struct obd_capa **oc)
65 {
66         struct lustre_capa *capa;
67         struct obd_capa *c;
68
69         /* swabbed already in mdc_enqueue */
70         capa = req_capsule_server_get(&req->rq_pill, field);
71         if (capa == NULL)
72                 return -EPROTO;
73
74         c = alloc_capa(CAPA_SITE_CLIENT);
75         if (IS_ERR(c)) {
76                 CDEBUG(D_INFO, "alloc capa failed!\n");
77                 return PTR_ERR(c);
78         } else {
79                 c->c_capa = *capa;
80                 *oc = c;
81                 return 0;
82         }
83 }
84
85 static inline int mdc_queue_wait(struct ptlrpc_request *req)
86 {
87         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
88         int rc;
89
90         /* mdc_enter_request() ensures that this client has no more
91          * than cl_max_rpcs_in_flight RPCs simultaneously inf light
92          * against an MDT. */
93         rc = mdc_enter_request(cli);
94         if (rc != 0)
95                 return rc;
96
97         rc = ptlrpc_queue_wait(req);
98         mdc_exit_request(cli);
99
100         return rc;
101 }
102
103 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
104 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
105 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
106                           struct obd_capa **pc, int level, int msg_flags)
107 {
108         struct ptlrpc_request *req;
109         struct mdt_body       *body;
110         int                 rc;
111
112         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
113                                         LUSTRE_MDS_VERSION, MDS_GETSTATUS);
114         if (req == NULL)
115                 return -ENOMEM;
116
117         mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
118         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
119         req->rq_send_state = level;
120
121         ptlrpc_request_set_replen(req);
122
123         rc = ptlrpc_queue_wait(req);
124         if (rc)
125                 GOTO(out, rc);
126
127         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
128         if (body == NULL)
129                 GOTO(out, rc = -EPROTO);
130
131         if (body->valid & OBD_MD_FLMDSCAPA) {
132                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
133                 if (rc)
134                         GOTO(out, rc);
135         }
136
137         *rootfid = body->fid1;
138         CDEBUG(D_NET,
139                "root fid="DFID", last_committed="LPU64"\n",
140                PFID(rootfid),
141                lustre_msg_get_last_committed(req->rq_repmsg));
142 out:
143         ptlrpc_req_finished(req);
144         return rc;
145 }
146
147 /* This should be mdc_get_info("rootfid") */
148 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
149                   struct obd_capa **pc)
150 {
151         return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
152                               LUSTRE_IMP_FULL, 0);
153 }
154
155 /*
156  * This function now is known to always saying that it will receive 4 buffers
157  * from server. Even for cases when acl_size and md_size is zero, RPC header
158  * will contain 4 fields and RPC itself will contain zero size fields. This is
159  * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
160  * and thus zero, it shrinks it, making zero size. The same story about
161  * md_size. And this is course of problem when client waits for smaller number
162  * of fields. This issue will be fixed later when client gets aware of RPC
163  * layouts.  --umka
164  */
165 static int mdc_getattr_common(struct obd_export *exp,
166                               struct ptlrpc_request *req)
167 {
168         struct req_capsule *pill = &req->rq_pill;
169         struct mdt_body    *body;
170         void           *eadata;
171         int              rc;
172
173         /* Request message already built. */
174         rc = ptlrpc_queue_wait(req);
175         if (rc != 0)
176                 return rc;
177
178         /* sanity check for the reply */
179         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
180         if (body == NULL)
181                 return -EPROTO;
182
183         CDEBUG(D_NET, "mode: %o\n", body->mode);
184
185         if (body->eadatasize != 0) {
186                 mdc_update_max_ea_from_body(exp, body);
187
188                 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
189                                                       body->eadatasize);
190                 if (eadata == NULL)
191                         return -EPROTO;
192         }
193
194         if (body->valid & OBD_MD_FLRMTPERM) {
195                 struct mdt_remote_perm *perm;
196
197                 LASSERT(client_is_remote(exp));
198                 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
199                                                 lustre_swab_mdt_remote_perm);
200                 if (perm == NULL)
201                         return -EPROTO;
202         }
203
204         if (body->valid & OBD_MD_FLMDSCAPA) {
205                 struct lustre_capa *capa;
206                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
207                 if (capa == NULL)
208                         return -EPROTO;
209         }
210
211         return 0;
212 }
213
214 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
215                 struct ptlrpc_request **request)
216 {
217         struct ptlrpc_request *req;
218         int                 rc;
219
220         /* Single MDS without an LMV case */
221         if (op_data->op_flags & MF_GET_MDT_IDX) {
222                 op_data->op_mds = 0;
223                 return 0;
224         }
225         *request = NULL;
226         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
227         if (req == NULL)
228                 return -ENOMEM;
229
230         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
231
232         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
233         if (rc) {
234                 ptlrpc_request_free(req);
235                 return rc;
236         }
237
238         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
239                       op_data->op_valid, op_data->op_mode, -1, 0);
240
241         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
242                              op_data->op_mode);
243         if (op_data->op_valid & OBD_MD_FLRMTPERM) {
244                 LASSERT(client_is_remote(exp));
245                 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
246                                      sizeof(struct mdt_remote_perm));
247         }
248         ptlrpc_request_set_replen(req);
249
250         rc = mdc_getattr_common(exp, req);
251         if (rc)
252                 ptlrpc_req_finished(req);
253         else
254                 *request = req;
255         return rc;
256 }
257
258 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
259                      struct ptlrpc_request **request)
260 {
261         struct ptlrpc_request *req;
262         int                 rc;
263
264         *request = NULL;
265         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
266                                    &RQF_MDS_GETATTR_NAME);
267         if (req == NULL)
268                 return -ENOMEM;
269
270         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
271         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
272                              op_data->op_namelen + 1);
273
274         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
275         if (rc) {
276                 ptlrpc_request_free(req);
277                 return rc;
278         }
279
280         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
281                       op_data->op_valid, op_data->op_mode,
282                       op_data->op_suppgids[0], 0);
283
284         if (op_data->op_name) {
285                 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
286                 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
287                                 op_data->op_namelen);
288                 memcpy(name, op_data->op_name, op_data->op_namelen);
289         }
290
291         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
292                              op_data->op_mode);
293         ptlrpc_request_set_replen(req);
294
295         rc = mdc_getattr_common(exp, req);
296         if (rc)
297                 ptlrpc_req_finished(req);
298         else
299                 *request = req;
300         return rc;
301 }
302
303 static int mdc_is_subdir(struct obd_export *exp,
304                          const struct lu_fid *pfid,
305                          const struct lu_fid *cfid,
306                          struct ptlrpc_request **request)
307 {
308         struct ptlrpc_request  *req;
309         int                  rc;
310
311         *request = NULL;
312         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
313                                         &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
314                                         MDS_IS_SUBDIR);
315         if (req == NULL)
316                 return -ENOMEM;
317
318         mdc_is_subdir_pack(req, pfid, cfid, 0);
319         ptlrpc_request_set_replen(req);
320
321         rc = ptlrpc_queue_wait(req);
322         if (rc && rc != -EREMOTE)
323                 ptlrpc_req_finished(req);
324         else
325                 *request = req;
326         return rc;
327 }
328
329 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
330                             const struct lu_fid *fid,
331                             struct obd_capa *oc, int opcode, obd_valid valid,
332                             const char *xattr_name, const char *input,
333                             int input_size, int output_size, int flags,
334                             __u32 suppgid, struct ptlrpc_request **request)
335 {
336         struct ptlrpc_request *req;
337         int   xattr_namelen = 0;
338         char *tmp;
339         int   rc;
340
341         *request = NULL;
342         req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
343         if (req == NULL)
344                 return -ENOMEM;
345
346         mdc_set_capa_size(req, &RMF_CAPA1, oc);
347         if (xattr_name) {
348                 xattr_namelen = strlen(xattr_name) + 1;
349                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
350                                      xattr_namelen);
351         }
352         if (input_size) {
353                 LASSERT(input);
354                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
355                                      input_size);
356         }
357
358         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
359         if (rc) {
360                 ptlrpc_request_free(req);
361                 return rc;
362         }
363
364         if (opcode == MDS_REINT) {
365                 struct mdt_rec_setxattr *rec;
366
367                 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
368                          sizeof(struct mdt_rec_reint));
369                 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
370                 rec->sx_opcode = REINT_SETXATTR;
371                 rec->sx_fsuid  = from_kuid(&init_user_ns, current_fsuid());
372                 rec->sx_fsgid  = from_kgid(&init_user_ns, current_fsgid());
373                 rec->sx_cap    = cfs_curproc_cap_pack();
374                 rec->sx_suppgid1 = suppgid;
375                 rec->sx_suppgid2 = -1;
376                 rec->sx_fid    = *fid;
377                 rec->sx_valid  = valid | OBD_MD_FLCTIME;
378                 rec->sx_time   = cfs_time_current_sec();
379                 rec->sx_size   = output_size;
380                 rec->sx_flags  = flags;
381
382                 mdc_pack_capa(req, &RMF_CAPA1, oc);
383         } else {
384                 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
385         }
386
387         if (xattr_name) {
388                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
389                 memcpy(tmp, xattr_name, xattr_namelen);
390         }
391         if (input_size) {
392                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
393                 memcpy(tmp, input, input_size);
394         }
395
396         if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
397                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
398                                      RCL_SERVER, output_size);
399         ptlrpc_request_set_replen(req);
400
401         /* make rpc */
402         if (opcode == MDS_REINT)
403                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
404
405         rc = ptlrpc_queue_wait(req);
406
407         if (opcode == MDS_REINT)
408                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
409
410         if (rc)
411                 ptlrpc_req_finished(req);
412         else
413                 *request = req;
414         return rc;
415 }
416
417 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
418                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
419                  const char *input, int input_size, int output_size,
420                  int flags, __u32 suppgid, struct ptlrpc_request **request)
421 {
422         return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
423                                 fid, oc, MDS_REINT, valid, xattr_name,
424                                 input, input_size, output_size, flags,
425                                 suppgid, request);
426 }
427
428 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
429                  struct obd_capa *oc, obd_valid valid, const char *xattr_name,
430                  const char *input, int input_size, int output_size,
431                  int flags, struct ptlrpc_request **request)
432 {
433         return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
434                                 fid, oc, MDS_GETXATTR, valid, xattr_name,
435                                 input, input_size, output_size, flags,
436                                 -1, request);
437 }
438
439 #ifdef CONFIG_FS_POSIX_ACL
440 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
441 {
442         struct req_capsule     *pill = &req->rq_pill;
443         struct mdt_body *body = md->body;
444         struct posix_acl       *acl;
445         void               *buf;
446         int                  rc;
447
448         if (!body->aclsize)
449                 return 0;
450
451         buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
452
453         if (!buf)
454                 return -EPROTO;
455
456         acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
457         if (IS_ERR(acl)) {
458                 rc = PTR_ERR(acl);
459                 CERROR("convert xattr to acl: %d\n", rc);
460                 return rc;
461         }
462
463         rc = posix_acl_valid(acl);
464         if (rc) {
465                 CERROR("validate acl: %d\n", rc);
466                 posix_acl_release(acl);
467                 return rc;
468         }
469
470         md->posix_acl = acl;
471         return 0;
472 }
473 #else
474 #define mdc_unpack_acl(req, md) 0
475 #endif
476
477 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
478                       struct obd_export *dt_exp, struct obd_export *md_exp,
479                       struct lustre_md *md)
480 {
481         struct req_capsule *pill = &req->rq_pill;
482         int rc;
483
484         LASSERT(md);
485         memset(md, 0, sizeof(*md));
486
487         md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
488         LASSERT(md->body != NULL);
489
490         if (md->body->valid & OBD_MD_FLEASIZE) {
491                 int lmmsize;
492                 struct lov_mds_md *lmm;
493
494                 if (!S_ISREG(md->body->mode)) {
495                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
496                                "regular file, but is not\n");
497                         GOTO(out, rc = -EPROTO);
498                 }
499
500                 if (md->body->eadatasize == 0) {
501                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
502                                "but eadatasize 0\n");
503                         GOTO(out, rc = -EPROTO);
504                 }
505                 lmmsize = md->body->eadatasize;
506                 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
507                 if (!lmm)
508                         GOTO(out, rc = -EPROTO);
509
510                 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
511                 if (rc < 0)
512                         GOTO(out, rc);
513
514                 if (rc < sizeof(*md->lsm)) {
515                         CDEBUG(D_INFO, "lsm size too small: "
516                                "rc < sizeof (*md->lsm) (%d < %d)\n",
517                                rc, (int)sizeof(*md->lsm));
518                         GOTO(out, rc = -EPROTO);
519                 }
520
521         } else if (md->body->valid & OBD_MD_FLDIREA) {
522                 int lmvsize;
523                 struct lov_mds_md *lmv;
524
525                 if(!S_ISDIR(md->body->mode)) {
526                         CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
527                                "directory, but is not\n");
528                         GOTO(out, rc = -EPROTO);
529                 }
530
531                 if (md->body->eadatasize == 0) {
532                         CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
533                                "but eadatasize 0\n");
534                         return -EPROTO;
535                 }
536                 if (md->body->valid & OBD_MD_MEA) {
537                         lmvsize = md->body->eadatasize;
538                         lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
539                                                            lmvsize);
540                         if (!lmv)
541                                 GOTO(out, rc = -EPROTO);
542
543                         rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
544                                           lmvsize);
545                         if (rc < 0)
546                                 GOTO(out, rc);
547
548                         if (rc < sizeof(*md->mea)) {
549                                 CDEBUG(D_INFO, "size too small:  "
550                                        "rc < sizeof(*md->mea) (%d < %d)\n",
551                                         rc, (int)sizeof(*md->mea));
552                                 GOTO(out, rc = -EPROTO);
553                         }
554                 }
555         }
556         rc = 0;
557
558         if (md->body->valid & OBD_MD_FLRMTPERM) {
559                 /* remote permission */
560                 LASSERT(client_is_remote(exp));
561                 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
562                                                 lustre_swab_mdt_remote_perm);
563                 if (!md->remote_perm)
564                         GOTO(out, rc = -EPROTO);
565         }
566         else if (md->body->valid & OBD_MD_FLACL) {
567                 /* for ACL, it's possible that FLACL is set but aclsize is zero.
568                  * only when aclsize != 0 there's an actual segment for ACL
569                  * in reply buffer.
570                  */
571                 if (md->body->aclsize) {
572                         rc = mdc_unpack_acl(req, md);
573                         if (rc)
574                                 GOTO(out, rc);
575 #ifdef CONFIG_FS_POSIX_ACL
576                 } else {
577                         md->posix_acl = NULL;
578 #endif
579                 }
580         }
581         if (md->body->valid & OBD_MD_FLMDSCAPA) {
582                 struct obd_capa *oc = NULL;
583
584                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
585                 if (rc)
586                         GOTO(out, rc);
587                 md->mds_capa = oc;
588         }
589
590         if (md->body->valid & OBD_MD_FLOSSCAPA) {
591                 struct obd_capa *oc = NULL;
592
593                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
594                 if (rc)
595                         GOTO(out, rc);
596                 md->oss_capa = oc;
597         }
598
599 out:
600         if (rc) {
601                 if (md->oss_capa) {
602                         capa_put(md->oss_capa);
603                         md->oss_capa = NULL;
604                 }
605                 if (md->mds_capa) {
606                         capa_put(md->mds_capa);
607                         md->mds_capa = NULL;
608                 }
609 #ifdef CONFIG_FS_POSIX_ACL
610                 posix_acl_release(md->posix_acl);
611 #endif
612                 if (md->lsm)
613                         obd_free_memmd(dt_exp, &md->lsm);
614         }
615         return rc;
616 }
617
618 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
619 {
620         return 0;
621 }
622
623 /**
624  * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
625  * RPC chains.
626  */
627 void mdc_replay_open(struct ptlrpc_request *req)
628 {
629         struct md_open_data *mod = req->rq_cb_data;
630         struct ptlrpc_request *close_req;
631         struct obd_client_handle *och;
632         struct lustre_handle old;
633         struct mdt_body *body;
634
635         if (mod == NULL) {
636                 DEBUG_REQ(D_ERROR, req,
637                           "Can't properly replay without open data.");
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 }
672
673 void mdc_commit_open(struct ptlrpc_request *req)
674 {
675         struct md_open_data *mod = req->rq_cb_data;
676         if (mod == NULL)
677                 return;
678
679         /**
680          * No need to touch md_open_data::mod_och, it holds a reference on
681          * \var mod and will zero references to each other, \var mod will be
682          * freed after that when md_open_data::mod_och will put the reference.
683          */
684
685         /**
686          * Do not let open request to disappear as it still may be needed
687          * for close rpc to happen (it may happen on evict only, otherwise
688          * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
689          * called), just mark this rpc as committed to distinguish these 2
690          * cases, see mdc_close() for details. The open request reference will
691          * be put along with freeing \var mod.
692          */
693         ptlrpc_request_addref(req);
694         spin_lock(&req->rq_lock);
695         req->rq_committed = 1;
696         spin_unlock(&req->rq_lock);
697         req->rq_cb_data = NULL;
698         obd_mod_put(mod);
699 }
700
701 int mdc_set_open_replay_data(struct obd_export *exp,
702                              struct obd_client_handle *och,
703                              struct ptlrpc_request *open_req)
704 {
705         struct md_open_data   *mod;
706         struct mdt_rec_create *rec;
707         struct mdt_body       *body;
708         struct obd_import     *imp = open_req->rq_import;
709
710         if (!open_req->rq_replay)
711                 return 0;
712
713         rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
714         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
715         LASSERT(rec != NULL);
716         /* Incoming message in my byte order (it's been swabbed). */
717         /* Outgoing messages always in my byte order. */
718         LASSERT(body != NULL);
719
720         /* Only if the import is replayable, we set replay_open data */
721         if (och && imp->imp_replayable) {
722                 mod = obd_mod_alloc();
723                 if (mod == NULL) {
724                         DEBUG_REQ(D_ERROR, open_req,
725                                   "Can't allocate md_open_data");
726                         return 0;
727                 }
728
729                 /**
730                  * Take a reference on \var mod, to be freed on mdc_close().
731                  * It protects \var mod from being freed on eviction (commit
732                  * callback is called despite rq_replay flag).
733                  * Another reference for \var och.
734                  */
735                 obd_mod_get(mod);
736                 obd_mod_get(mod);
737
738                 spin_lock(&open_req->rq_lock);
739                 och->och_mod = mod;
740                 mod->mod_och = och;
741                 mod->mod_open_req = open_req;
742                 open_req->rq_cb_data = mod;
743                 open_req->rq_commit_cb = mdc_commit_open;
744                 spin_unlock(&open_req->rq_lock);
745         }
746
747         rec->cr_fid2 = body->fid1;
748         rec->cr_ioepoch = body->ioepoch;
749         rec->cr_old_handle.cookie = body->handle.cookie;
750         open_req->rq_replay_cb = mdc_replay_open;
751         if (!fid_is_sane(&body->fid1)) {
752                 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
753                           "insane fid");
754                 LBUG();
755         }
756
757         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
758         return 0;
759 }
760
761 int mdc_clear_open_replay_data(struct obd_export *exp,
762                                struct obd_client_handle *och)
763 {
764         struct md_open_data *mod = och->och_mod;
765
766         /**
767          * It is possible to not have \var mod in a case of eviction between
768          * lookup and ll_file_open().
769          **/
770         if (mod == NULL)
771                 return 0;
772
773         LASSERT(mod != LP_POISON);
774
775         mod->mod_och = NULL;
776         och->och_mod = NULL;
777         obd_mod_put(mod);
778
779         return 0;
780 }
781
782 /* Prepares the request for the replay by the given reply */
783 static void mdc_close_handle_reply(struct ptlrpc_request *req,
784                                    struct md_op_data *op_data, int rc) {
785         struct mdt_body  *repbody;
786         struct mdt_ioepoch *epoch;
787
788         if (req && rc == -EAGAIN) {
789                 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
790                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
791
792                 epoch->flags |= MF_SOM_AU;
793                 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
794                         op_data->op_flags |= MF_GETATTR_LOCK;
795         }
796 }
797
798 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
799               struct md_open_data *mod, struct ptlrpc_request **request)
800 {
801         struct obd_device     *obd = class_exp2obd(exp);
802         struct ptlrpc_request *req;
803         int                 rc;
804
805         *request = NULL;
806         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
807         if (req == NULL)
808                 return -ENOMEM;
809
810         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
811
812         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
813         if (rc) {
814                 ptlrpc_request_free(req);
815                 return rc;
816         }
817
818         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
819          * portal whose threads are not taking any DLM locks and are therefore
820          * always progressing */
821         req->rq_request_portal = MDS_READPAGE_PORTAL;
822         ptlrpc_at_set_req_timeout(req);
823
824         /* Ensure that this close's handle is fixed up during replay. */
825         if (likely(mod != NULL)) {
826                 LASSERTF(mod->mod_open_req != NULL &&
827                          mod->mod_open_req->rq_type != LI_POISON,
828                          "POISONED open %p!\n", mod->mod_open_req);
829
830                 mod->mod_close_req = req;
831
832                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
833                 /* We no longer want to preserve this open for replay even
834                  * though the open was committed. b=3632, b=3633 */
835                 spin_lock(&mod->mod_open_req->rq_lock);
836                 mod->mod_open_req->rq_replay = 0;
837                 spin_unlock(&mod->mod_open_req->rq_lock);
838         } else {
839                  CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
840         }
841
842         mdc_close_pack(req, op_data);
843
844         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
845                              obd->u.cli.cl_max_mds_easize);
846         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
847                              obd->u.cli.cl_max_mds_cookiesize);
848
849         ptlrpc_request_set_replen(req);
850
851         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
852         rc = ptlrpc_queue_wait(req);
853         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
854
855         if (req->rq_repmsg == NULL) {
856                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
857                        req->rq_status);
858                 if (rc == 0)
859                         rc = req->rq_status ?: -EIO;
860         } else if (rc == 0 || rc == -EAGAIN) {
861                 struct mdt_body *body;
862
863                 rc = lustre_msg_get_status(req->rq_repmsg);
864                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
865                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
866                                   "= %d", rc);
867                         if (rc > 0)
868                                 rc = -rc;
869                 }
870                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
871                 if (body == NULL)
872                         rc = -EPROTO;
873         } else if (rc == -ESTALE) {
874                 /**
875                  * it can be allowed error after 3633 if open was committed and
876                  * server failed before close was sent. Let's check if mod
877                  * exists and return no error in that case
878                  */
879                 if (mod) {
880                         DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
881                         LASSERT(mod->mod_open_req != NULL);
882                         if (mod->mod_open_req->rq_committed)
883                                 rc = 0;
884                 }
885         }
886
887         if (mod) {
888                 if (rc != 0)
889                         mod->mod_close_req = NULL;
890                 /* Since now, mod is accessed through open_req only,
891                  * thus close req does not keep a reference on mod anymore. */
892                 obd_mod_put(mod);
893         }
894         *request = req;
895         mdc_close_handle_reply(req, op_data, rc);
896         return rc;
897 }
898
899 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
900                      struct md_open_data *mod)
901 {
902         struct obd_device     *obd = class_exp2obd(exp);
903         struct ptlrpc_request *req;
904         int                 rc;
905
906         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
907                                    &RQF_MDS_DONE_WRITING);
908         if (req == NULL)
909                 return -ENOMEM;
910
911         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
912         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
913         if (rc) {
914                 ptlrpc_request_free(req);
915                 return rc;
916         }
917
918         if (mod != NULL) {
919                 LASSERTF(mod->mod_open_req != NULL &&
920                          mod->mod_open_req->rq_type != LI_POISON,
921                          "POISONED setattr %p!\n", mod->mod_open_req);
922
923                 mod->mod_close_req = req;
924                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
925                 /* We no longer want to preserve this setattr for replay even
926                  * though the open was committed. b=3632, b=3633 */
927                 spin_lock(&mod->mod_open_req->rq_lock);
928                 mod->mod_open_req->rq_replay = 0;
929                 spin_unlock(&mod->mod_open_req->rq_lock);
930         }
931
932         mdc_close_pack(req, op_data);
933         ptlrpc_request_set_replen(req);
934
935         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
936         rc = ptlrpc_queue_wait(req);
937         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
938
939         if (rc == -ESTALE) {
940                 /**
941                  * it can be allowed error after 3633 if open or setattr were
942                  * committed and server failed before close was sent.
943                  * Let's check if mod exists and return no error in that case
944                  */
945                 if (mod) {
946                         LASSERT(mod->mod_open_req != NULL);
947                         if (mod->mod_open_req->rq_committed)
948                                 rc = 0;
949                 }
950         }
951
952         if (mod) {
953                 if (rc != 0)
954                         mod->mod_close_req = NULL;
955                 /* Since now, mod is accessed through setattr req only,
956                  * thus DW req does not keep a reference on mod anymore. */
957                 obd_mod_put(mod);
958         }
959
960         mdc_close_handle_reply(req, op_data, rc);
961         ptlrpc_req_finished(req);
962         return rc;
963 }
964
965
966 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
967                  struct page **pages, struct ptlrpc_request **request)
968 {
969         struct ptlrpc_request   *req;
970         struct ptlrpc_bulk_desc *desc;
971         int                   i;
972         wait_queue_head_t             waitq;
973         int                   resends = 0;
974         struct l_wait_info       lwi;
975         int                   rc;
976
977         *request = NULL;
978         init_waitqueue_head(&waitq);
979
980 restart_bulk:
981         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
982         if (req == NULL)
983                 return -ENOMEM;
984
985         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
986
987         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
988         if (rc) {
989                 ptlrpc_request_free(req);
990                 return rc;
991         }
992
993         req->rq_request_portal = MDS_READPAGE_PORTAL;
994         ptlrpc_at_set_req_timeout(req);
995
996         desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
997                                     MDS_BULK_PORTAL);
998         if (desc == NULL) {
999                 ptlrpc_request_free(req);
1000                 return -ENOMEM;
1001         }
1002
1003         /* NB req now owns desc and will free it when it gets freed */
1004         for (i = 0; i < op_data->op_npages; i++)
1005                 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1006
1007         mdc_readdir_pack(req, op_data->op_offset,
1008                          PAGE_CACHE_SIZE * op_data->op_npages,
1009                          &op_data->op_fid1, op_data->op_capa1);
1010
1011         ptlrpc_request_set_replen(req);
1012         rc = ptlrpc_queue_wait(req);
1013         if (rc) {
1014                 ptlrpc_req_finished(req);
1015                 if (rc != -ETIMEDOUT)
1016                         return rc;
1017
1018                 resends++;
1019                 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1020                         CERROR("too many resend retries, returning error\n");
1021                         return -EIO;
1022                 }
1023                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1024                 l_wait_event(waitq, 0, &lwi);
1025
1026                 goto restart_bulk;
1027         }
1028
1029         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1030                                           req->rq_bulk->bd_nob_transferred);
1031         if (rc < 0) {
1032                 ptlrpc_req_finished(req);
1033                 return rc;
1034         }
1035
1036         if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1037                 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1038                         req->rq_bulk->bd_nob_transferred,
1039                         PAGE_CACHE_SIZE * op_data->op_npages);
1040                 ptlrpc_req_finished(req);
1041                 return -EPROTO;
1042         }
1043
1044         *request = req;
1045         return 0;
1046 }
1047
1048 static int mdc_statfs(const struct lu_env *env,
1049                       struct obd_export *exp, struct obd_statfs *osfs,
1050                       __u64 max_age, __u32 flags)
1051 {
1052         struct obd_device     *obd = class_exp2obd(exp);
1053         struct ptlrpc_request *req;
1054         struct obd_statfs     *msfs;
1055         struct obd_import     *imp = NULL;
1056         int                 rc;
1057
1058         /*
1059          * Since the request might also come from lprocfs, so we need
1060          * sync this with client_disconnect_export Bug15684
1061          */
1062         down_read(&obd->u.cli.cl_sem);
1063         if (obd->u.cli.cl_import)
1064                 imp = class_import_get(obd->u.cli.cl_import);
1065         up_read(&obd->u.cli.cl_sem);
1066         if (!imp)
1067                 return -ENODEV;
1068
1069         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1070                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1071         if (req == NULL)
1072                 GOTO(output, rc = -ENOMEM);
1073
1074         ptlrpc_request_set_replen(req);
1075
1076         if (flags & OBD_STATFS_NODELAY) {
1077                 /* procfs requests not want stay in wait for avoid deadlock */
1078                 req->rq_no_resend = 1;
1079                 req->rq_no_delay = 1;
1080         }
1081
1082         rc = ptlrpc_queue_wait(req);
1083         if (rc) {
1084                 /* check connection error first */
1085                 if (imp->imp_connect_error)
1086                         rc = imp->imp_connect_error;
1087                 GOTO(out, rc);
1088         }
1089
1090         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1091         if (msfs == NULL)
1092                 GOTO(out, rc = -EPROTO);
1093
1094         *osfs = *msfs;
1095 out:
1096         ptlrpc_req_finished(req);
1097 output:
1098         class_import_put(imp);
1099         return rc;
1100 }
1101
1102 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1103 {
1104         __u32 keylen, vallen;
1105         void *key;
1106         int rc;
1107
1108         if (gf->gf_pathlen > PATH_MAX)
1109                 return -ENAMETOOLONG;
1110         if (gf->gf_pathlen < 2)
1111                 return -EOVERFLOW;
1112
1113         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1114         keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1115         OBD_ALLOC(key, keylen);
1116         if (key == NULL)
1117                 return -ENOMEM;
1118         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1119         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1120
1121         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1122                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1123
1124         if (!fid_is_sane(&gf->gf_fid))
1125                 GOTO(out, rc = -EINVAL);
1126
1127         /* Val is struct getinfo_fid2path result plus path */
1128         vallen = sizeof(*gf) + gf->gf_pathlen;
1129
1130         rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1131         if (rc != 0 && rc != -EREMOTE)
1132                 GOTO(out, rc);
1133
1134         if (vallen <= sizeof(*gf))
1135                 GOTO(out, rc = -EPROTO);
1136         else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1137                 GOTO(out, rc = -EOVERFLOW);
1138
1139         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1140                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1141
1142 out:
1143         OBD_FREE(key, keylen);
1144         return rc;
1145 }
1146
1147 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1148                                 struct hsm_progress_kernel *hpk)
1149 {
1150         struct obd_import               *imp = class_exp2cliimp(exp);
1151         struct hsm_progress_kernel      *req_hpk;
1152         struct ptlrpc_request           *req;
1153         int                              rc;
1154
1155         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1156                                         LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1157         if (req == NULL)
1158                 GOTO(out, rc = -ENOMEM);
1159
1160         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1161
1162         /* Copy hsm_progress struct */
1163         req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1164         if (req_hpk == NULL)
1165                 GOTO(out, rc = -EPROTO);
1166
1167         *req_hpk = *hpk;
1168         req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1169
1170         ptlrpc_request_set_replen(req);
1171
1172         rc = mdc_queue_wait(req);
1173         GOTO(out, rc);
1174 out:
1175         ptlrpc_req_finished(req);
1176         return rc;
1177 }
1178
1179 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1180 {
1181         __u32                   *archive_mask;
1182         struct ptlrpc_request   *req;
1183         int                      rc;
1184
1185         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1186                                         LUSTRE_MDS_VERSION,
1187                                         MDS_HSM_CT_REGISTER);
1188         if (req == NULL)
1189                 GOTO(out, rc = -ENOMEM);
1190
1191         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1192
1193         /* Copy hsm_progress struct */
1194         archive_mask = req_capsule_client_get(&req->rq_pill,
1195                                               &RMF_MDS_HSM_ARCHIVE);
1196         if (archive_mask == NULL)
1197                 GOTO(out, rc = -EPROTO);
1198
1199         *archive_mask = archives;
1200
1201         ptlrpc_request_set_replen(req);
1202
1203         rc = mdc_queue_wait(req);
1204         GOTO(out, rc);
1205 out:
1206         ptlrpc_req_finished(req);
1207         return rc;
1208 }
1209
1210 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1211                                       struct md_op_data *op_data)
1212 {
1213         struct hsm_current_action       *hca = op_data->op_data;
1214         struct hsm_current_action       *req_hca;
1215         struct ptlrpc_request           *req;
1216         int                              rc;
1217
1218         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1219                                    &RQF_MDS_HSM_ACTION);
1220         if (req == NULL)
1221                 return -ENOMEM;
1222
1223         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1224
1225         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1226         if (rc) {
1227                 ptlrpc_request_free(req);
1228                 return rc;
1229         }
1230
1231         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1232                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1233
1234         ptlrpc_request_set_replen(req);
1235
1236         rc = mdc_queue_wait(req);
1237         if (rc)
1238                 GOTO(out, rc);
1239
1240         req_hca = req_capsule_server_get(&req->rq_pill,
1241                                          &RMF_MDS_HSM_CURRENT_ACTION);
1242         if (req_hca == NULL)
1243                 GOTO(out, rc = -EPROTO);
1244
1245         *hca = *req_hca;
1246
1247 out:
1248         ptlrpc_req_finished(req);
1249         return rc;
1250 }
1251
1252 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1253 {
1254         struct ptlrpc_request   *req;
1255         int                      rc;
1256
1257         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1258                                         LUSTRE_MDS_VERSION,
1259                                         MDS_HSM_CT_UNREGISTER);
1260         if (req == NULL)
1261                 GOTO(out, rc = -ENOMEM);
1262
1263         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1264
1265         ptlrpc_request_set_replen(req);
1266
1267         rc = mdc_queue_wait(req);
1268         GOTO(out, rc);
1269 out:
1270         ptlrpc_req_finished(req);
1271         return rc;
1272 }
1273
1274 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1275                                  struct md_op_data *op_data)
1276 {
1277         struct hsm_user_state   *hus = op_data->op_data;
1278         struct hsm_user_state   *req_hus;
1279         struct ptlrpc_request   *req;
1280         int                      rc;
1281
1282         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1283                                    &RQF_MDS_HSM_STATE_GET);
1284         if (req == NULL)
1285                 return -ENOMEM;
1286
1287         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1288
1289         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1290         if (rc != 0) {
1291                 ptlrpc_request_free(req);
1292                 return rc;
1293         }
1294
1295         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1296                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1297
1298         ptlrpc_request_set_replen(req);
1299
1300         rc = mdc_queue_wait(req);
1301         if (rc)
1302                 GOTO(out, rc);
1303
1304         req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1305         if (req_hus == NULL)
1306                 GOTO(out, rc = -EPROTO);
1307
1308         *hus = *req_hus;
1309
1310 out:
1311         ptlrpc_req_finished(req);
1312         return rc;
1313 }
1314
1315 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1316                                  struct md_op_data *op_data)
1317 {
1318         struct hsm_state_set    *hss = op_data->op_data;
1319         struct hsm_state_set    *req_hss;
1320         struct ptlrpc_request   *req;
1321         int                      rc;
1322
1323         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1324                                    &RQF_MDS_HSM_STATE_SET);
1325         if (req == NULL)
1326                 return -ENOMEM;
1327
1328         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1329
1330         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1331         if (rc) {
1332                 ptlrpc_request_free(req);
1333                 return rc;
1334         }
1335
1336         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1337                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1338
1339         /* Copy states */
1340         req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1341         if (req_hss == NULL)
1342                 GOTO(out, rc = -EPROTO);
1343         *req_hss = *hss;
1344
1345         ptlrpc_request_set_replen(req);
1346
1347         rc = mdc_queue_wait(req);
1348         GOTO(out, rc);
1349
1350 out:
1351         ptlrpc_req_finished(req);
1352         return rc;
1353 }
1354
1355 static int mdc_ioc_hsm_request(struct obd_export *exp,
1356                                struct hsm_user_request *hur)
1357 {
1358         struct obd_import       *imp = class_exp2cliimp(exp);
1359         struct ptlrpc_request   *req;
1360         struct hsm_request      *req_hr;
1361         struct hsm_user_item    *req_hui;
1362         char                    *req_opaque;
1363         int                      rc;
1364
1365         req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1366         if (req == NULL)
1367                 GOTO(out, rc = -ENOMEM);
1368
1369         req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1370                              hur->hur_request.hr_itemcount
1371                              * sizeof(struct hsm_user_item));
1372         req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1373                              hur->hur_request.hr_data_len);
1374
1375         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1376         if (rc) {
1377                 ptlrpc_request_free(req);
1378                 return rc;
1379         }
1380
1381         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1382
1383         /* Copy hsm_request struct */
1384         req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1385         if (req_hr == NULL)
1386                 GOTO(out, rc = -EPROTO);
1387         *req_hr = hur->hur_request;
1388
1389         /* Copy hsm_user_item structs */
1390         req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1391         if (req_hui == NULL)
1392                 GOTO(out, rc = -EPROTO);
1393         memcpy(req_hui, hur->hur_user_item,
1394                hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1395
1396         /* Copy opaque field */
1397         req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1398         if (req_opaque == NULL)
1399                 GOTO(out, rc = -EPROTO);
1400         memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1401
1402         ptlrpc_request_set_replen(req);
1403
1404         rc = mdc_queue_wait(req);
1405         GOTO(out, rc);
1406
1407 out:
1408         ptlrpc_req_finished(req);
1409         return rc;
1410 }
1411
1412 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1413 {
1414         struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1415
1416         LASSERT(len <= CR_MAXSIZE);
1417
1418         lh->kuc_magic = KUC_MAGIC;
1419         lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1420         lh->kuc_flags = flags;
1421         lh->kuc_msgtype = CL_RECORD;
1422         lh->kuc_msglen = len;
1423         return lh;
1424 }
1425
1426 #define D_CHANGELOG 0
1427
1428 struct changelog_show {
1429         __u64           cs_startrec;
1430         __u32           cs_flags;
1431         struct file     *cs_fp;
1432         char            *cs_buf;
1433         struct obd_device *cs_obd;
1434 };
1435
1436 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1437                              struct llog_rec_hdr *hdr, void *data)
1438 {
1439         struct changelog_show *cs = data;
1440         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1441         struct kuc_hdr *lh;
1442         int len, rc;
1443
1444         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1445                 rc = -EINVAL;
1446                 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1447                        cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1448                        rec->cr.cr_type, rc);
1449                 return rc;
1450         }
1451
1452         if (rec->cr.cr_index < cs->cs_startrec) {
1453                 /* Skip entries earlier than what we are interested in */
1454                 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1455                        rec->cr.cr_index, cs->cs_startrec);
1456                 return 0;
1457         }
1458
1459         CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1460                 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1461                 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1462                 rec->cr.cr_flags & CLF_FLAGMASK,
1463                 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1464                 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1465
1466         len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1467
1468         /* Set up the message */
1469         lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1470         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1471
1472         rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1473         CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1474
1475         return rc;
1476 }
1477
1478 static int mdc_changelog_send_thread(void *csdata)
1479 {
1480         struct changelog_show *cs = csdata;
1481         struct llog_ctxt *ctxt = NULL;
1482         struct llog_handle *llh = NULL;
1483         struct kuc_hdr *kuch;
1484         int rc;
1485
1486         CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1487                cs->cs_fp, cs->cs_startrec);
1488
1489         OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1490         if (cs->cs_buf == NULL)
1491                 GOTO(out, rc = -ENOMEM);
1492
1493         /* Set up the remote catalog handle */
1494         ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1495         if (ctxt == NULL)
1496                 GOTO(out, rc = -ENOENT);
1497         rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1498                        LLOG_OPEN_EXISTS);
1499         if (rc) {
1500                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1501                        cs->cs_obd->obd_name, rc);
1502                 GOTO(out, rc);
1503         }
1504         rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1505         if (rc) {
1506                 CERROR("llog_init_handle failed %d\n", rc);
1507                 GOTO(out, rc);
1508         }
1509
1510         rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1511
1512         /* Send EOF no matter what our result */
1513         if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1514                                       cs->cs_flags))) {
1515                 kuch->kuc_msgtype = CL_EOF;
1516                 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1517         }
1518
1519 out:
1520         fput(cs->cs_fp);
1521         if (llh)
1522                 llog_cat_close(NULL, llh);
1523         if (ctxt)
1524                 llog_ctxt_put(ctxt);
1525         if (cs->cs_buf)
1526                 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1527         OBD_FREE_PTR(cs);
1528         return rc;
1529 }
1530
1531 static int mdc_ioc_changelog_send(struct obd_device *obd,
1532                                   struct ioc_changelog *icc)
1533 {
1534         struct changelog_show *cs;
1535         int rc;
1536
1537         /* Freed in mdc_changelog_send_thread */
1538         OBD_ALLOC_PTR(cs);
1539         if (!cs)
1540                 return -ENOMEM;
1541
1542         cs->cs_obd = obd;
1543         cs->cs_startrec = icc->icc_recno;
1544         /* matching fput in mdc_changelog_send_thread */
1545         cs->cs_fp = fget(icc->icc_id);
1546         cs->cs_flags = icc->icc_flags;
1547
1548         /*
1549          * New thread because we should return to user app before
1550          * writing into our pipe
1551          */
1552         rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1553                                  "mdc_clg_send_thread"));
1554         if (!IS_ERR_VALUE(rc)) {
1555                 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1556                 return 0;
1557         }
1558
1559         CERROR("Failed to start changelog thread: %d\n", rc);
1560         OBD_FREE_PTR(cs);
1561         return rc;
1562 }
1563
1564 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1565                                 struct lustre_kernelcomm *lk);
1566
1567 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1568                           struct obd_quotactl *oqctl)
1569 {
1570         struct client_obd       *cli = &exp->exp_obd->u.cli;
1571         struct ptlrpc_request   *req;
1572         struct obd_quotactl     *body;
1573         int                   rc;
1574
1575         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1576                                         &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1577                                         MDS_QUOTACHECK);
1578         if (req == NULL)
1579                 return -ENOMEM;
1580
1581         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1582         *body = *oqctl;
1583
1584         ptlrpc_request_set_replen(req);
1585
1586         /* the next poll will find -ENODATA, that means quotacheck is
1587          * going on */
1588         cli->cl_qchk_stat = -ENODATA;
1589         rc = ptlrpc_queue_wait(req);
1590         if (rc)
1591                 cli->cl_qchk_stat = rc;
1592         ptlrpc_req_finished(req);
1593         return rc;
1594 }
1595
1596 static int mdc_quota_poll_check(struct obd_export *exp,
1597                                 struct if_quotacheck *qchk)
1598 {
1599         struct client_obd *cli = &exp->exp_obd->u.cli;
1600         int rc;
1601
1602         qchk->obd_uuid = cli->cl_target_uuid;
1603         memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1604
1605         rc = cli->cl_qchk_stat;
1606         /* the client is not the previous one */
1607         if (rc == CL_NOT_QUOTACHECKED)
1608                 rc = -EINTR;
1609         return rc;
1610 }
1611
1612 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1613                         struct obd_quotactl *oqctl)
1614 {
1615         struct ptlrpc_request   *req;
1616         struct obd_quotactl     *oqc;
1617         int                   rc;
1618
1619         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1620                                         &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1621                                         MDS_QUOTACTL);
1622         if (req == NULL)
1623                 return -ENOMEM;
1624
1625         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1626         *oqc = *oqctl;
1627
1628         ptlrpc_request_set_replen(req);
1629         ptlrpc_at_set_req_timeout(req);
1630         req->rq_no_resend = 1;
1631
1632         rc = ptlrpc_queue_wait(req);
1633         if (rc)
1634                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1635
1636         if (req->rq_repmsg &&
1637             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1638                 *oqctl = *oqc;
1639         } else if (!rc) {
1640                 CERROR ("Can't unpack obd_quotactl\n");
1641                 rc = -EPROTO;
1642         }
1643         ptlrpc_req_finished(req);
1644
1645         return rc;
1646 }
1647
1648 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1649                                 struct md_op_data *op_data)
1650 {
1651         LIST_HEAD(cancels);
1652         struct ptlrpc_request   *req;
1653         int                      rc, count;
1654         struct mdc_swap_layouts *msl, *payload;
1655
1656         msl = op_data->op_data;
1657
1658         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1659          * first thing it will do is to cancel the 2 layout
1660          * locks hold by this client.
1661          * So the client must cancel its layout locks on the 2 fids
1662          * with the request RPC to avoid extra RPC round trips
1663          */
1664         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1665                                         LCK_CR, MDS_INODELOCK_LAYOUT);
1666         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1667                                          LCK_CR, MDS_INODELOCK_LAYOUT);
1668
1669         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1670                                    &RQF_MDS_SWAP_LAYOUTS);
1671         if (req == NULL) {
1672                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1673                 return -ENOMEM;
1674         }
1675
1676         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1677         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1678
1679         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1680         if (rc) {
1681                 ptlrpc_request_free(req);
1682                 return rc;
1683         }
1684
1685         mdc_swap_layouts_pack(req, op_data);
1686
1687         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1688         LASSERT(payload);
1689
1690         *payload = *msl;
1691
1692         ptlrpc_request_set_replen(req);
1693
1694         rc = ptlrpc_queue_wait(req);
1695         if (rc)
1696                 GOTO(out, rc);
1697
1698 out:
1699         ptlrpc_req_finished(req);
1700         return rc;
1701 }
1702
1703 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1704                          void *karg, void *uarg)
1705 {
1706         struct obd_device *obd = exp->exp_obd;
1707         struct obd_ioctl_data *data = karg;
1708         struct obd_import *imp = obd->u.cli.cl_import;
1709         struct llog_ctxt *ctxt;
1710         int rc;
1711
1712         if (!try_module_get(THIS_MODULE)) {
1713                 CERROR("Can't get module. Is it alive?");
1714                 return -EINVAL;
1715         }
1716         switch (cmd) {
1717         case OBD_IOC_CHANGELOG_SEND:
1718                 rc = mdc_ioc_changelog_send(obd, karg);
1719                 GOTO(out, rc);
1720         case OBD_IOC_CHANGELOG_CLEAR: {
1721                 struct ioc_changelog *icc = karg;
1722                 struct changelog_setinfo cs =
1723                         {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1724                 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1725                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1726                                         NULL);
1727                 GOTO(out, rc);
1728         }
1729         case OBD_IOC_FID2PATH:
1730                 rc = mdc_ioc_fid2path(exp, karg);
1731                 GOTO(out, rc);
1732         case LL_IOC_HSM_CT_START:
1733                 rc = mdc_ioc_hsm_ct_start(exp, karg);
1734                 /* ignore if it was already registered on this MDS. */
1735                 if (rc == -EEXIST)
1736                         rc = 0;
1737                 GOTO(out, rc);
1738         case LL_IOC_HSM_PROGRESS:
1739                 rc = mdc_ioc_hsm_progress(exp, karg);
1740                 GOTO(out, rc);
1741         case LL_IOC_HSM_STATE_GET:
1742                 rc = mdc_ioc_hsm_state_get(exp, karg);
1743                 GOTO(out, rc);
1744         case LL_IOC_HSM_STATE_SET:
1745                 rc = mdc_ioc_hsm_state_set(exp, karg);
1746         case LL_IOC_HSM_ACTION:
1747                 rc = mdc_ioc_hsm_current_action(exp, karg);
1748                 GOTO(out, rc);
1749         case LL_IOC_HSM_REQUEST:
1750                 rc = mdc_ioc_hsm_request(exp, karg);
1751                 GOTO(out, rc);
1752         case OBD_IOC_CLIENT_RECOVER:
1753                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1754                 if (rc < 0)
1755                         GOTO(out, rc);
1756                 GOTO(out, rc = 0);
1757         case IOC_OSC_SET_ACTIVE:
1758                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1759                 GOTO(out, rc);
1760         case OBD_IOC_PARSE: {
1761                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1762                 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1763                                              NULL);
1764                 llog_ctxt_put(ctxt);
1765                 GOTO(out, rc);
1766         }
1767         case OBD_IOC_LLOG_INFO:
1768         case OBD_IOC_LLOG_PRINT: {
1769                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1770                 rc = llog_ioctl(NULL, ctxt, cmd, data);
1771                 llog_ctxt_put(ctxt);
1772                 GOTO(out, rc);
1773         }
1774         case OBD_IOC_POLL_QUOTACHECK:
1775                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1776                 GOTO(out, rc);
1777         case OBD_IOC_PING_TARGET:
1778                 rc = ptlrpc_obd_ping(obd);
1779                 GOTO(out, rc);
1780         /*
1781          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1782          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1783          * there'd be no LMV layer thus we might be called here. Eventually
1784          * this code should be removed.
1785          * bz20731, LU-592.
1786          */
1787         case IOC_OBD_STATFS: {
1788                 struct obd_statfs stat_buf = {0};
1789
1790                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1791                         GOTO(out, rc = -ENODEV);
1792
1793                 /* copy UUID */
1794                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1795                                      min((int) data->ioc_plen2,
1796                                          (int) sizeof(struct obd_uuid))))
1797                         GOTO(out, rc = -EFAULT);
1798
1799                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1800                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1801                                 0);
1802                 if (rc != 0)
1803                         GOTO(out, rc);
1804
1805                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1806                                      min((int) data->ioc_plen1,
1807                                          (int) sizeof(stat_buf))))
1808                         GOTO(out, rc = -EFAULT);
1809
1810                 GOTO(out, rc = 0);
1811         }
1812         case OBD_IOC_QUOTACTL: {
1813                 struct if_quotactl *qctl = karg;
1814                 struct obd_quotactl *oqctl;
1815
1816                 OBD_ALLOC_PTR(oqctl);
1817                 if (!oqctl)
1818                         return -ENOMEM;
1819
1820                 QCTL_COPY(oqctl, qctl);
1821                 rc = obd_quotactl(exp, oqctl);
1822                 if (rc == 0) {
1823                         QCTL_COPY(qctl, oqctl);
1824                         qctl->qc_valid = QC_MDTIDX;
1825                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1826                 }
1827                 OBD_FREE_PTR(oqctl);
1828                 break;
1829         }
1830         case LL_IOC_GET_CONNECT_FLAGS: {
1831                 if (copy_to_user(uarg,
1832                                      exp_connect_flags_ptr(exp),
1833                                      sizeof(__u64)))
1834                         GOTO(out, rc = -EFAULT);
1835                 else
1836                         GOTO(out, rc = 0);
1837         }
1838         case LL_IOC_LOV_SWAP_LAYOUTS: {
1839                 rc = mdc_ioc_swap_layouts(exp, karg);
1840                 break;
1841         }
1842         default:
1843                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1844                 GOTO(out, rc = -ENOTTY);
1845         }
1846 out:
1847         module_put(THIS_MODULE);
1848
1849         return rc;
1850 }
1851
1852 int mdc_get_info_rpc(struct obd_export *exp,
1853                      obd_count keylen, void *key,
1854                      int vallen, void *val)
1855 {
1856         struct obd_import      *imp = class_exp2cliimp(exp);
1857         struct ptlrpc_request  *req;
1858         char               *tmp;
1859         int                  rc = -EINVAL;
1860
1861         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1862         if (req == NULL)
1863                 return -ENOMEM;
1864
1865         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1866                              RCL_CLIENT, keylen);
1867         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1868                              RCL_CLIENT, sizeof(__u32));
1869
1870         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1871         if (rc) {
1872                 ptlrpc_request_free(req);
1873                 return rc;
1874         }
1875
1876         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1877         memcpy(tmp, key, keylen);
1878         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1879         memcpy(tmp, &vallen, sizeof(__u32));
1880
1881         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1882                              RCL_SERVER, vallen);
1883         ptlrpc_request_set_replen(req);
1884
1885         rc = ptlrpc_queue_wait(req);
1886         /* -EREMOTE means the get_info result is partial, and it needs to
1887          * continue on another MDT, see fid2path part in lmv_iocontrol */
1888         if (rc == 0 || rc == -EREMOTE) {
1889                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1890                 memcpy(val, tmp, vallen);
1891                 if (ptlrpc_rep_need_swab(req)) {
1892                         if (KEY_IS(KEY_FID2PATH))
1893                                 lustre_swab_fid2path(val);
1894                 }
1895         }
1896         ptlrpc_req_finished(req);
1897
1898         return rc;
1899 }
1900
1901 static void lustre_swab_hai(struct hsm_action_item *h)
1902 {
1903         __swab32s(&h->hai_len);
1904         __swab32s(&h->hai_action);
1905         lustre_swab_lu_fid(&h->hai_fid);
1906         lustre_swab_lu_fid(&h->hai_dfid);
1907         __swab64s(&h->hai_cookie);
1908         __swab64s(&h->hai_extent.offset);
1909         __swab64s(&h->hai_extent.length);
1910         __swab64s(&h->hai_gid);
1911 }
1912
1913 static void lustre_swab_hal(struct hsm_action_list *h)
1914 {
1915         struct hsm_action_item  *hai;
1916         int                      i;
1917
1918         __swab32s(&h->hal_version);
1919         __swab32s(&h->hal_count);
1920         __swab32s(&h->hal_archive_id);
1921         __swab64s(&h->hal_flags);
1922         hai = hai_zero(h);
1923         for (i = 0; i < h->hal_count; i++) {
1924                 lustre_swab_hai(hai);
1925                 hai = hai_next(hai);
1926         }
1927 }
1928
1929 static void lustre_swab_kuch(struct kuc_hdr *l)
1930 {
1931         __swab16s(&l->kuc_magic);
1932         /* __u8 l->kuc_transport */
1933         __swab16s(&l->kuc_msgtype);
1934         __swab16s(&l->kuc_msglen);
1935 }
1936
1937 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1938                                 struct lustre_kernelcomm *lk)
1939 {
1940         struct obd_import  *imp = class_exp2cliimp(exp);
1941         __u32               archive = lk->lk_data;
1942         int                 rc = 0;
1943
1944         if (lk->lk_group != KUC_GRP_HSM) {
1945                 CERROR("Bad copytool group %d\n", lk->lk_group);
1946                 return -EINVAL;
1947         }
1948
1949         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1950                lk->lk_uid, lk->lk_group, lk->lk_flags);
1951
1952         if (lk->lk_flags & LK_FLG_STOP) {
1953                 /* Unregister with the coordinator */
1954                 rc = mdc_ioc_hsm_ct_unregister(imp);
1955         } else {
1956                 rc = mdc_ioc_hsm_ct_register(imp, archive);
1957         }
1958
1959         return rc;
1960 }
1961
1962 /**
1963  * Send a message to any listening copytools
1964  * @param val KUC message (kuc_hdr + hsm_action_list)
1965  * @param len total length of message
1966  */
1967 static int mdc_hsm_copytool_send(int len, void *val)
1968 {
1969         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
1970         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
1971         int                      rc;
1972
1973         if (len < sizeof(*lh) + sizeof(*hal)) {
1974                 CERROR("Short HSM message %d < %d\n", len,
1975                        (int) (sizeof(*lh) + sizeof(*hal)));
1976                 return -EPROTO;
1977         }
1978         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1979                 lustre_swab_kuch(lh);
1980                 lustre_swab_hal(hal);
1981         } else if (lh->kuc_magic != KUC_MAGIC) {
1982                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
1983                 return -EPROTO;
1984         }
1985
1986         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
1987                "on %s\n",
1988                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
1989                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
1990
1991         /* Broadcast to HSM listeners */
1992         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
1993
1994         return rc;
1995 }
1996
1997 /**
1998  * callback function passed to kuc for re-registering each HSM copytool
1999  * running on MDC, after MDT shutdown/recovery.
2000  * @param data archive id served by the copytool
2001  * @param cb_arg callback argument (obd_import)
2002  */
2003 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2004 {
2005         struct obd_import       *imp = (struct obd_import *)cb_arg;
2006         __u32                    archive = data;
2007         int                      rc;
2008
2009         CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2010                archive);
2011         rc = mdc_ioc_hsm_ct_register(imp, archive);
2012
2013         /* ignore error if the copytool is already registered */
2014         return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2015 }
2016
2017 /**
2018  * Re-establish all kuc contexts with MDT
2019  * after MDT shutdown/recovery.
2020  */
2021 static int mdc_kuc_reregister(struct obd_import *imp)
2022 {
2023         /* re-register HSM agents */
2024         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2025                                          (void *)imp);
2026 }
2027
2028 int mdc_set_info_async(const struct lu_env *env,
2029                        struct obd_export *exp,
2030                        obd_count keylen, void *key,
2031                        obd_count vallen, void *val,
2032                        struct ptlrpc_request_set *set)
2033 {
2034         struct obd_import       *imp = class_exp2cliimp(exp);
2035         int                      rc;
2036
2037         if (KEY_IS(KEY_READ_ONLY)) {
2038                 if (vallen != sizeof(int))
2039                         return -EINVAL;
2040
2041                 spin_lock(&imp->imp_lock);
2042                 if (*((int *)val)) {
2043                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2044                         imp->imp_connect_data.ocd_connect_flags |=
2045                                                         OBD_CONNECT_RDONLY;
2046                 } else {
2047                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2048                         imp->imp_connect_data.ocd_connect_flags &=
2049                                                         ~OBD_CONNECT_RDONLY;
2050                 }
2051                 spin_unlock(&imp->imp_lock);
2052
2053                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2054                                        keylen, key, vallen, val, set);
2055                 return rc;
2056         }
2057         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2058                 sptlrpc_conf_client_adapt(exp->exp_obd);
2059                 return 0;
2060         }
2061         if (KEY_IS(KEY_FLUSH_CTX)) {
2062                 sptlrpc_import_flush_my_ctx(imp);
2063                 return 0;
2064         }
2065         if (KEY_IS(KEY_MDS_CONN)) {
2066                 /* mds-mds import */
2067                 spin_lock(&imp->imp_lock);
2068                 imp->imp_server_timeout = 1;
2069                 spin_unlock(&imp->imp_lock);
2070                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2071                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2072                 return 0;
2073         }
2074         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2075                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2076                                        keylen, key, vallen, val, set);
2077                 return rc;
2078         }
2079         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2080                 rc = mdc_hsm_copytool_send(vallen, val);
2081                 return rc;
2082         }
2083
2084         CERROR("Unknown key %s\n", (char *)key);
2085         return -EINVAL;
2086 }
2087
2088 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2089                  __u32 keylen, void *key, __u32 *vallen, void *val,
2090                  struct lov_stripe_md *lsm)
2091 {
2092         int rc = -EINVAL;
2093
2094         if (KEY_IS(KEY_MAX_EASIZE)) {
2095                 int mdsize, *max_easize;
2096
2097                 if (*vallen != sizeof(int))
2098                         return -EINVAL;
2099                 mdsize = *(int*)val;
2100                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2101                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2102                 max_easize = val;
2103                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2104                 return 0;
2105         } else if (KEY_IS(KEY_CONN_DATA)) {
2106                 struct obd_import *imp = class_exp2cliimp(exp);
2107                 struct obd_connect_data *data = val;
2108
2109                 if (*vallen != sizeof(*data))
2110                         return -EINVAL;
2111
2112                 *data = imp->imp_connect_data;
2113                 return 0;
2114         } else if (KEY_IS(KEY_TGT_COUNT)) {
2115                 *((int *)val) = 1;
2116                 return 0;
2117         }
2118
2119         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2120
2121         return rc;
2122 }
2123
2124 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2125                    struct obd_capa *oc, struct obd_client_handle *handle,
2126                    int flags)
2127 {
2128         struct ptlrpc_request *req;
2129         struct mdt_body       *body;
2130         int                 rc;
2131
2132         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2133         if (req == NULL)
2134                 return -ENOMEM;
2135
2136         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2137
2138         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2139         if (rc) {
2140                 ptlrpc_request_free(req);
2141                 return rc;
2142         }
2143
2144         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2145
2146         ptlrpc_request_set_replen(req);
2147
2148         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2149         rc = ptlrpc_queue_wait(req);
2150         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2151         if (rc) {
2152                 CERROR("Pin failed: %d\n", rc);
2153                 GOTO(err_out, rc);
2154         }
2155
2156         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2157         if (body == NULL)
2158                 GOTO(err_out, rc = -EPROTO);
2159
2160         handle->och_fh = body->handle;
2161         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2162
2163         handle->och_mod = obd_mod_alloc();
2164         if (handle->och_mod == NULL) {
2165                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2166                 GOTO(err_out, rc = -ENOMEM);
2167         }
2168         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2169
2170         return 0;
2171
2172 err_out:
2173         ptlrpc_req_finished(req);
2174         return rc;
2175 }
2176
2177 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2178                      int flag)
2179 {
2180         struct ptlrpc_request *req;
2181         struct mdt_body       *body;
2182         int                 rc;
2183
2184         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2185                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
2186         if (req == NULL)
2187                 return -ENOMEM;
2188
2189         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2190         body->handle = handle->och_fh;
2191         body->flags = flag;
2192
2193         ptlrpc_request_set_replen(req);
2194
2195         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2196         rc = ptlrpc_queue_wait(req);
2197         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2198
2199         if (rc != 0)
2200                 CERROR("Unpin failed: %d\n", rc);
2201
2202         ptlrpc_req_finished(req);
2203         ptlrpc_req_finished(handle->och_mod->mod_open_req);
2204
2205         obd_mod_put(handle->och_mod);
2206         return rc;
2207 }
2208
2209 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2210              struct obd_capa *oc, struct ptlrpc_request **request)
2211 {
2212         struct ptlrpc_request *req;
2213         int                 rc;
2214
2215         *request = NULL;
2216         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2217         if (req == NULL)
2218                 return -ENOMEM;
2219
2220         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2221
2222         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2223         if (rc) {
2224                 ptlrpc_request_free(req);
2225                 return rc;
2226         }
2227
2228         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2229
2230         ptlrpc_request_set_replen(req);
2231
2232         rc = ptlrpc_queue_wait(req);
2233         if (rc)
2234                 ptlrpc_req_finished(req);
2235         else
2236                 *request = req;
2237         return rc;
2238 }
2239
2240 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2241                             enum obd_import_event event)
2242 {
2243         int rc = 0;
2244
2245         LASSERT(imp->imp_obd == obd);
2246
2247         switch (event) {
2248         case IMP_EVENT_DISCON: {
2249 #if 0
2250                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2251                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2252 #endif
2253                 break;
2254         }
2255         case IMP_EVENT_INACTIVE: {
2256                 struct client_obd *cli = &obd->u.cli;
2257                 /*
2258                  * Flush current sequence to make client obtain new one
2259                  * from server in case of disconnect/reconnect.
2260                  */
2261                 if (cli->cl_seq != NULL)
2262                         seq_client_flush(cli->cl_seq);
2263
2264                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2265                 break;
2266         }
2267         case IMP_EVENT_INVALIDATE: {
2268                 struct ldlm_namespace *ns = obd->obd_namespace;
2269
2270                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2271
2272                 break;
2273         }
2274         case IMP_EVENT_ACTIVE:
2275                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2276                 /* redo the kuc registration after reconnecting */
2277                 if (rc == 0)
2278                         rc = mdc_kuc_reregister(imp);
2279                 break;
2280         case IMP_EVENT_OCD:
2281                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2282                 break;
2283         case IMP_EVENT_DEACTIVATE:
2284         case IMP_EVENT_ACTIVATE:
2285                 break;
2286         default:
2287                 CERROR("Unknown import event %x\n", event);
2288                 LBUG();
2289         }
2290         return rc;
2291 }
2292
2293 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2294                   struct md_op_data *op_data)
2295 {
2296         struct client_obd *cli = &exp->exp_obd->u.cli;
2297         struct lu_client_seq *seq = cli->cl_seq;
2298
2299         return seq_client_alloc_fid(NULL, seq, fid);
2300 }
2301
2302 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2303         struct client_obd *cli = &exp->exp_obd->u.cli;
2304         return &cli->cl_target_uuid;
2305 }
2306
2307 /**
2308  * Determine whether the lock can be canceled before replaying it during
2309  * recovery, non zero value will be return if the lock can be canceled,
2310  * or zero returned for not
2311  */
2312 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2313 {
2314         if (lock->l_resource->lr_type != LDLM_IBITS)
2315                 return 0;
2316
2317         /* FIXME: if we ever get into a situation where there are too many
2318          * opened files with open locks on a single node, then we really
2319          * should replay these open locks to reget it */
2320         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2321                 return 0;
2322
2323         return 1;
2324 }
2325
2326 static int mdc_resource_inode_free(struct ldlm_resource *res)
2327 {
2328         if (res->lr_lvb_inode)
2329                 res->lr_lvb_inode = NULL;
2330
2331         return 0;
2332 }
2333
2334 struct ldlm_valblock_ops inode_lvbo = {
2335         .lvbo_free = mdc_resource_inode_free,
2336 };
2337
2338 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2339 {
2340         struct client_obd *cli = &obd->u.cli;
2341         struct lprocfs_static_vars lvars = { 0 };
2342         int rc;
2343
2344         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2345         if (!cli->cl_rpc_lock)
2346                 return -ENOMEM;
2347         mdc_init_rpc_lock(cli->cl_rpc_lock);
2348
2349         ptlrpcd_addref();
2350
2351         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2352         if (!cli->cl_close_lock)
2353                 GOTO(err_rpc_lock, rc = -ENOMEM);
2354         mdc_init_rpc_lock(cli->cl_close_lock);
2355
2356         rc = client_obd_setup(obd, cfg);
2357         if (rc)
2358                 GOTO(err_close_lock, rc);
2359         lprocfs_mdc_init_vars(&lvars);
2360         lprocfs_obd_setup(obd, lvars.obd_vars);
2361         sptlrpc_lprocfs_cliobd_attach(obd);
2362         ptlrpc_lprocfs_register_obd(obd);
2363
2364         ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2365
2366         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2367
2368         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2369         if (rc) {
2370                 mdc_cleanup(obd);
2371                 CERROR("failed to setup llogging subsystems\n");
2372         }
2373
2374         return rc;
2375
2376 err_close_lock:
2377         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2378 err_rpc_lock:
2379         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2380         ptlrpcd_decref();
2381         return rc;
2382 }
2383
2384 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2385  * us to make MDS RPCs with large enough reply buffers to hold the
2386  * maximum-sized (= maximum striped) EA and cookie without having to
2387  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2388 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2389                      int def_easize, int cookiesize)
2390 {
2391         struct obd_device *obd = exp->exp_obd;
2392         struct client_obd *cli = &obd->u.cli;
2393
2394         if (cli->cl_max_mds_easize < easize)
2395                 cli->cl_max_mds_easize = easize;
2396
2397         if (cli->cl_default_mds_easize < def_easize)
2398                 cli->cl_default_mds_easize = def_easize;
2399
2400         if (cli->cl_max_mds_cookiesize < cookiesize)
2401                 cli->cl_max_mds_cookiesize = cookiesize;
2402
2403         return 0;
2404 }
2405
2406 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2407 {
2408         int rc = 0;
2409
2410         switch (stage) {
2411         case OBD_CLEANUP_EARLY:
2412                 break;
2413         case OBD_CLEANUP_EXPORTS:
2414                 /* Failsafe, ok if racy */
2415                 if (obd->obd_type->typ_refcnt <= 1)
2416                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2417
2418                 obd_cleanup_client_import(obd);
2419                 ptlrpc_lprocfs_unregister_obd(obd);
2420                 lprocfs_obd_cleanup(obd);
2421
2422                 rc = obd_llog_finish(obd, 0);
2423                 if (rc != 0)
2424                         CERROR("failed to cleanup llogging subsystems\n");
2425                 break;
2426         }
2427         return rc;
2428 }
2429
2430 static int mdc_cleanup(struct obd_device *obd)
2431 {
2432         struct client_obd *cli = &obd->u.cli;
2433
2434         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2435         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2436
2437         ptlrpcd_decref();
2438
2439         return client_obd_cleanup(obd);
2440 }
2441
2442
2443 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2444                          struct obd_device *tgt, int *index)
2445 {
2446         struct llog_ctxt        *ctxt;
2447         int                      rc;
2448
2449         LASSERT(olg == &obd->obd_olg);
2450
2451         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2452                         &llog_client_ops);
2453         if (rc)
2454                 return rc;
2455
2456         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2457         llog_initiator_connect(ctxt);
2458         llog_ctxt_put(ctxt);
2459
2460         return 0;
2461 }
2462
2463 static int mdc_llog_finish(struct obd_device *obd, int count)
2464 {
2465         struct llog_ctxt *ctxt;
2466
2467         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2468         if (ctxt)
2469                 llog_cleanup(NULL, ctxt);
2470
2471         return 0;
2472 }
2473
2474 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2475 {
2476         struct lustre_cfg *lcfg = buf;
2477         struct lprocfs_static_vars lvars = { 0 };
2478         int rc = 0;
2479
2480         lprocfs_mdc_init_vars(&lvars);
2481         switch (lcfg->lcfg_command) {
2482         default:
2483                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2484                                               lcfg, obd);
2485                 if (rc > 0)
2486                         rc = 0;
2487                 break;
2488         }
2489         return(rc);
2490 }
2491
2492
2493 /* get remote permission for current user on fid */
2494 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2495                         struct obd_capa *oc, __u32 suppgid,
2496                         struct ptlrpc_request **request)
2497 {
2498         struct ptlrpc_request  *req;
2499         int                 rc;
2500
2501         LASSERT(client_is_remote(exp));
2502
2503         *request = NULL;
2504         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2505         if (req == NULL)
2506                 return -ENOMEM;
2507
2508         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2509
2510         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2511         if (rc) {
2512                 ptlrpc_request_free(req);
2513                 return rc;
2514         }
2515
2516         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2517
2518         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2519                              sizeof(struct mdt_remote_perm));
2520
2521         ptlrpc_request_set_replen(req);
2522
2523         rc = ptlrpc_queue_wait(req);
2524         if (rc)
2525                 ptlrpc_req_finished(req);
2526         else
2527                 *request = req;
2528         return rc;
2529 }
2530
2531 static int mdc_interpret_renew_capa(const struct lu_env *env,
2532                                     struct ptlrpc_request *req, void *args,
2533                                     int status)
2534 {
2535         struct mdc_renew_capa_args *ra = args;
2536         struct mdt_body *body = NULL;
2537         struct lustre_capa *capa;
2538
2539         if (status)
2540                 GOTO(out, capa = ERR_PTR(status));
2541
2542         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2543         if (body == NULL)
2544                 GOTO(out, capa = ERR_PTR(-EFAULT));
2545
2546         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2547                 GOTO(out, capa = ERR_PTR(-ENOENT));
2548
2549         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2550         if (!capa)
2551                 GOTO(out, capa = ERR_PTR(-EFAULT));
2552 out:
2553         ra->ra_cb(ra->ra_oc, capa);
2554         return 0;
2555 }
2556
2557 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2558                           renew_capa_cb_t cb)
2559 {
2560         struct ptlrpc_request *req;
2561         struct mdc_renew_capa_args *ra;
2562
2563         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2564                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
2565         if (req == NULL)
2566                 return -ENOMEM;
2567
2568         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2569          * capa to renew is oss capa.
2570          */
2571         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2572         ptlrpc_request_set_replen(req);
2573
2574         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2575         ra = ptlrpc_req_async_args(req);
2576         ra->ra_oc = oc;
2577         ra->ra_cb = cb;
2578         req->rq_interpret_reply = mdc_interpret_renew_capa;
2579         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2580         return 0;
2581 }
2582
2583 static int mdc_connect(const struct lu_env *env,
2584                        struct obd_export **exp,
2585                        struct obd_device *obd, struct obd_uuid *cluuid,
2586                        struct obd_connect_data *data,
2587                        void *localdata)
2588 {
2589         struct obd_import *imp = obd->u.cli.cl_import;
2590
2591         /* mds-mds import features */
2592         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2593                 spin_lock(&imp->imp_lock);
2594                 imp->imp_server_timeout = 1;
2595                 spin_unlock(&imp->imp_lock);
2596                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2597                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2598                        obd->obd_name);
2599         }
2600
2601         return client_connect_import(env, exp, obd, cluuid, data, NULL);
2602 }
2603
2604 struct obd_ops mdc_obd_ops = {
2605         .o_owner            = THIS_MODULE,
2606         .o_setup            = mdc_setup,
2607         .o_precleanup       = mdc_precleanup,
2608         .o_cleanup        = mdc_cleanup,
2609         .o_add_conn      = client_import_add_conn,
2610         .o_del_conn      = client_import_del_conn,
2611         .o_connect        = mdc_connect,
2612         .o_disconnect       = client_disconnect_export,
2613         .o_iocontrol    = mdc_iocontrol,
2614         .o_set_info_async   = mdc_set_info_async,
2615         .o_statfs          = mdc_statfs,
2616         .o_pin        = mdc_pin,
2617         .o_unpin            = mdc_unpin,
2618         .o_fid_init         = client_fid_init,
2619         .o_fid_fini         = client_fid_fini,
2620         .o_fid_alloc    = mdc_fid_alloc,
2621         .o_import_event     = mdc_import_event,
2622         .o_llog_init    = mdc_llog_init,
2623         .o_llog_finish      = mdc_llog_finish,
2624         .o_get_info      = mdc_get_info,
2625         .o_process_config   = mdc_process_config,
2626         .o_get_uuid      = mdc_get_uuid,
2627         .o_quotactl      = mdc_quotactl,
2628         .o_quotacheck       = mdc_quotacheck
2629 };
2630
2631 struct md_ops mdc_md_ops = {
2632         .m_getstatus    = mdc_getstatus,
2633         .m_null_inode       = mdc_null_inode,
2634         .m_find_cbdata      = mdc_find_cbdata,
2635         .m_close            = mdc_close,
2636         .m_create          = mdc_create,
2637         .m_done_writing     = mdc_done_writing,
2638         .m_enqueue        = mdc_enqueue,
2639         .m_getattr        = mdc_getattr,
2640         .m_getattr_name     = mdc_getattr_name,
2641         .m_intent_lock      = mdc_intent_lock,
2642         .m_link      = mdc_link,
2643         .m_is_subdir    = mdc_is_subdir,
2644         .m_rename          = mdc_rename,
2645         .m_setattr        = mdc_setattr,
2646         .m_setxattr      = mdc_setxattr,
2647         .m_getxattr      = mdc_getxattr,
2648         .m_sync      = mdc_sync,
2649         .m_readpage      = mdc_readpage,
2650         .m_unlink          = mdc_unlink,
2651         .m_cancel_unused    = mdc_cancel_unused,
2652         .m_init_ea_size     = mdc_init_ea_size,
2653         .m_set_lock_data    = mdc_set_lock_data,
2654         .m_lock_match       = mdc_lock_match,
2655         .m_get_lustre_md    = mdc_get_lustre_md,
2656         .m_free_lustre_md   = mdc_free_lustre_md,
2657         .m_set_open_replay_data = mdc_set_open_replay_data,
2658         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2659         .m_renew_capa       = mdc_renew_capa,
2660         .m_unpack_capa      = mdc_unpack_capa,
2661         .m_get_remote_perm  = mdc_get_remote_perm,
2662         .m_intent_getattr_async = mdc_intent_getattr_async,
2663         .m_revalidate_lock      = mdc_revalidate_lock
2664 };
2665
2666 int __init mdc_init(void)
2667 {
2668         int rc;
2669         struct lprocfs_static_vars lvars = { 0 };
2670         lprocfs_mdc_init_vars(&lvars);
2671
2672         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2673                                  LUSTRE_MDC_NAME, NULL);
2674         return rc;
2675 }
2676
2677 static void /*__exit*/ mdc_exit(void)
2678 {
2679         class_unregister_type(LUSTRE_MDC_NAME);
2680 }
2681
2682 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2683 MODULE_DESCRIPTION("Lustre Metadata Client");
2684 MODULE_LICENSE("GPL");
2685
2686 module_init(mdc_init);
2687 module_exit(mdc_exit);