]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/mdc/mdc_request.c
8373778c2e780fbb914fc450b80f3aab594ee5de
[~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                 GOTO(out, rc);
1747         case LL_IOC_HSM_ACTION:
1748                 rc = mdc_ioc_hsm_current_action(exp, karg);
1749                 GOTO(out, rc);
1750         case LL_IOC_HSM_REQUEST:
1751                 rc = mdc_ioc_hsm_request(exp, karg);
1752                 GOTO(out, rc);
1753         case OBD_IOC_CLIENT_RECOVER:
1754                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1755                 if (rc < 0)
1756                         GOTO(out, rc);
1757                 GOTO(out, rc = 0);
1758         case IOC_OSC_SET_ACTIVE:
1759                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1760                 GOTO(out, rc);
1761         case OBD_IOC_PARSE: {
1762                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1763                 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1764                                              NULL);
1765                 llog_ctxt_put(ctxt);
1766                 GOTO(out, rc);
1767         }
1768         case OBD_IOC_LLOG_INFO:
1769         case OBD_IOC_LLOG_PRINT: {
1770                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1771                 rc = llog_ioctl(NULL, ctxt, cmd, data);
1772                 llog_ctxt_put(ctxt);
1773                 GOTO(out, rc);
1774         }
1775         case OBD_IOC_POLL_QUOTACHECK:
1776                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1777                 GOTO(out, rc);
1778         case OBD_IOC_PING_TARGET:
1779                 rc = ptlrpc_obd_ping(obd);
1780                 GOTO(out, rc);
1781         /*
1782          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1783          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1784          * there'd be no LMV layer thus we might be called here. Eventually
1785          * this code should be removed.
1786          * bz20731, LU-592.
1787          */
1788         case IOC_OBD_STATFS: {
1789                 struct obd_statfs stat_buf = {0};
1790
1791                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1792                         GOTO(out, rc = -ENODEV);
1793
1794                 /* copy UUID */
1795                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1796                                      min((int) data->ioc_plen2,
1797                                          (int) sizeof(struct obd_uuid))))
1798                         GOTO(out, rc = -EFAULT);
1799
1800                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1801                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1802                                 0);
1803                 if (rc != 0)
1804                         GOTO(out, rc);
1805
1806                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1807                                      min((int) data->ioc_plen1,
1808                                          (int) sizeof(stat_buf))))
1809                         GOTO(out, rc = -EFAULT);
1810
1811                 GOTO(out, rc = 0);
1812         }
1813         case OBD_IOC_QUOTACTL: {
1814                 struct if_quotactl *qctl = karg;
1815                 struct obd_quotactl *oqctl;
1816
1817                 OBD_ALLOC_PTR(oqctl);
1818                 if (oqctl == NULL)
1819                         GOTO(out, rc = -ENOMEM);
1820
1821                 QCTL_COPY(oqctl, qctl);
1822                 rc = obd_quotactl(exp, oqctl);
1823                 if (rc == 0) {
1824                         QCTL_COPY(qctl, oqctl);
1825                         qctl->qc_valid = QC_MDTIDX;
1826                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1827                 }
1828
1829                 OBD_FREE_PTR(oqctl);
1830                 GOTO(out, rc);
1831         }
1832         case LL_IOC_GET_CONNECT_FLAGS:
1833                 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1834                                  sizeof(*exp_connect_flags_ptr(exp))))
1835                         GOTO(out, rc = -EFAULT);
1836
1837                 GOTO(out, rc = 0);
1838         case LL_IOC_LOV_SWAP_LAYOUTS:
1839                 rc = mdc_ioc_swap_layouts(exp, karg);
1840                 GOTO(out, rc);
1841         default:
1842                 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1843                 GOTO(out, rc = -ENOTTY);
1844         }
1845 out:
1846         module_put(THIS_MODULE);
1847
1848         return rc;
1849 }
1850
1851 int mdc_get_info_rpc(struct obd_export *exp,
1852                      obd_count keylen, void *key,
1853                      int vallen, void *val)
1854 {
1855         struct obd_import      *imp = class_exp2cliimp(exp);
1856         struct ptlrpc_request  *req;
1857         char               *tmp;
1858         int                  rc = -EINVAL;
1859
1860         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1861         if (req == NULL)
1862                 return -ENOMEM;
1863
1864         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1865                              RCL_CLIENT, keylen);
1866         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1867                              RCL_CLIENT, sizeof(__u32));
1868
1869         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1870         if (rc) {
1871                 ptlrpc_request_free(req);
1872                 return rc;
1873         }
1874
1875         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1876         memcpy(tmp, key, keylen);
1877         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1878         memcpy(tmp, &vallen, sizeof(__u32));
1879
1880         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1881                              RCL_SERVER, vallen);
1882         ptlrpc_request_set_replen(req);
1883
1884         rc = ptlrpc_queue_wait(req);
1885         /* -EREMOTE means the get_info result is partial, and it needs to
1886          * continue on another MDT, see fid2path part in lmv_iocontrol */
1887         if (rc == 0 || rc == -EREMOTE) {
1888                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1889                 memcpy(val, tmp, vallen);
1890                 if (ptlrpc_rep_need_swab(req)) {
1891                         if (KEY_IS(KEY_FID2PATH))
1892                                 lustre_swab_fid2path(val);
1893                 }
1894         }
1895         ptlrpc_req_finished(req);
1896
1897         return rc;
1898 }
1899
1900 static void lustre_swab_hai(struct hsm_action_item *h)
1901 {
1902         __swab32s(&h->hai_len);
1903         __swab32s(&h->hai_action);
1904         lustre_swab_lu_fid(&h->hai_fid);
1905         lustre_swab_lu_fid(&h->hai_dfid);
1906         __swab64s(&h->hai_cookie);
1907         __swab64s(&h->hai_extent.offset);
1908         __swab64s(&h->hai_extent.length);
1909         __swab64s(&h->hai_gid);
1910 }
1911
1912 static void lustre_swab_hal(struct hsm_action_list *h)
1913 {
1914         struct hsm_action_item  *hai;
1915         int                      i;
1916
1917         __swab32s(&h->hal_version);
1918         __swab32s(&h->hal_count);
1919         __swab32s(&h->hal_archive_id);
1920         __swab64s(&h->hal_flags);
1921         hai = hai_zero(h);
1922         for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
1923                 lustre_swab_hai(hai);
1924 }
1925
1926 static void lustre_swab_kuch(struct kuc_hdr *l)
1927 {
1928         __swab16s(&l->kuc_magic);
1929         /* __u8 l->kuc_transport */
1930         __swab16s(&l->kuc_msgtype);
1931         __swab16s(&l->kuc_msglen);
1932 }
1933
1934 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1935                                 struct lustre_kernelcomm *lk)
1936 {
1937         struct obd_import  *imp = class_exp2cliimp(exp);
1938         __u32               archive = lk->lk_data;
1939         int                 rc = 0;
1940
1941         if (lk->lk_group != KUC_GRP_HSM) {
1942                 CERROR("Bad copytool group %d\n", lk->lk_group);
1943                 return -EINVAL;
1944         }
1945
1946         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1947                lk->lk_uid, lk->lk_group, lk->lk_flags);
1948
1949         if (lk->lk_flags & LK_FLG_STOP) {
1950                 /* Unregister with the coordinator */
1951                 rc = mdc_ioc_hsm_ct_unregister(imp);
1952         } else {
1953                 rc = mdc_ioc_hsm_ct_register(imp, archive);
1954         }
1955
1956         return rc;
1957 }
1958
1959 /**
1960  * Send a message to any listening copytools
1961  * @param val KUC message (kuc_hdr + hsm_action_list)
1962  * @param len total length of message
1963  */
1964 static int mdc_hsm_copytool_send(int len, void *val)
1965 {
1966         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
1967         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
1968         int                      rc;
1969
1970         if (len < sizeof(*lh) + sizeof(*hal)) {
1971                 CERROR("Short HSM message %d < %d\n", len,
1972                        (int) (sizeof(*lh) + sizeof(*hal)));
1973                 return -EPROTO;
1974         }
1975         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1976                 lustre_swab_kuch(lh);
1977                 lustre_swab_hal(hal);
1978         } else if (lh->kuc_magic != KUC_MAGIC) {
1979                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
1980                 return -EPROTO;
1981         }
1982
1983         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
1984                "on %s\n",
1985                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
1986                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
1987
1988         /* Broadcast to HSM listeners */
1989         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
1990
1991         return rc;
1992 }
1993
1994 /**
1995  * callback function passed to kuc for re-registering each HSM copytool
1996  * running on MDC, after MDT shutdown/recovery.
1997  * @param data archive id served by the copytool
1998  * @param cb_arg callback argument (obd_import)
1999  */
2000 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2001 {
2002         struct obd_import       *imp = (struct obd_import *)cb_arg;
2003         __u32                    archive = data;
2004         int                      rc;
2005
2006         CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2007                archive);
2008         rc = mdc_ioc_hsm_ct_register(imp, archive);
2009
2010         /* ignore error if the copytool is already registered */
2011         return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2012 }
2013
2014 /**
2015  * Re-establish all kuc contexts with MDT
2016  * after MDT shutdown/recovery.
2017  */
2018 static int mdc_kuc_reregister(struct obd_import *imp)
2019 {
2020         /* re-register HSM agents */
2021         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2022                                          (void *)imp);
2023 }
2024
2025 int mdc_set_info_async(const struct lu_env *env,
2026                        struct obd_export *exp,
2027                        obd_count keylen, void *key,
2028                        obd_count vallen, void *val,
2029                        struct ptlrpc_request_set *set)
2030 {
2031         struct obd_import       *imp = class_exp2cliimp(exp);
2032         int                      rc;
2033
2034         if (KEY_IS(KEY_READ_ONLY)) {
2035                 if (vallen != sizeof(int))
2036                         return -EINVAL;
2037
2038                 spin_lock(&imp->imp_lock);
2039                 if (*((int *)val)) {
2040                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2041                         imp->imp_connect_data.ocd_connect_flags |=
2042                                                         OBD_CONNECT_RDONLY;
2043                 } else {
2044                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2045                         imp->imp_connect_data.ocd_connect_flags &=
2046                                                         ~OBD_CONNECT_RDONLY;
2047                 }
2048                 spin_unlock(&imp->imp_lock);
2049
2050                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2051                                        keylen, key, vallen, val, set);
2052                 return rc;
2053         }
2054         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2055                 sptlrpc_conf_client_adapt(exp->exp_obd);
2056                 return 0;
2057         }
2058         if (KEY_IS(KEY_FLUSH_CTX)) {
2059                 sptlrpc_import_flush_my_ctx(imp);
2060                 return 0;
2061         }
2062         if (KEY_IS(KEY_MDS_CONN)) {
2063                 /* mds-mds import */
2064                 spin_lock(&imp->imp_lock);
2065                 imp->imp_server_timeout = 1;
2066                 spin_unlock(&imp->imp_lock);
2067                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2068                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2069                 return 0;
2070         }
2071         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2072                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2073                                        keylen, key, vallen, val, set);
2074                 return rc;
2075         }
2076         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2077                 rc = mdc_hsm_copytool_send(vallen, val);
2078                 return rc;
2079         }
2080
2081         CERROR("Unknown key %s\n", (char *)key);
2082         return -EINVAL;
2083 }
2084
2085 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2086                  __u32 keylen, void *key, __u32 *vallen, void *val,
2087                  struct lov_stripe_md *lsm)
2088 {
2089         int rc = -EINVAL;
2090
2091         if (KEY_IS(KEY_MAX_EASIZE)) {
2092                 int mdsize, *max_easize;
2093
2094                 if (*vallen != sizeof(int))
2095                         return -EINVAL;
2096                 mdsize = *(int*)val;
2097                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2098                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2099                 max_easize = val;
2100                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2101                 return 0;
2102         } else if (KEY_IS(KEY_CONN_DATA)) {
2103                 struct obd_import *imp = class_exp2cliimp(exp);
2104                 struct obd_connect_data *data = val;
2105
2106                 if (*vallen != sizeof(*data))
2107                         return -EINVAL;
2108
2109                 *data = imp->imp_connect_data;
2110                 return 0;
2111         } else if (KEY_IS(KEY_TGT_COUNT)) {
2112                 *((int *)val) = 1;
2113                 return 0;
2114         }
2115
2116         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2117
2118         return rc;
2119 }
2120
2121 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2122                    struct obd_capa *oc, struct obd_client_handle *handle,
2123                    int flags)
2124 {
2125         struct ptlrpc_request *req;
2126         struct mdt_body       *body;
2127         int                 rc;
2128
2129         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2130         if (req == NULL)
2131                 return -ENOMEM;
2132
2133         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2134
2135         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2136         if (rc) {
2137                 ptlrpc_request_free(req);
2138                 return rc;
2139         }
2140
2141         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2142
2143         ptlrpc_request_set_replen(req);
2144
2145         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2146         rc = ptlrpc_queue_wait(req);
2147         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2148         if (rc) {
2149                 CERROR("Pin failed: %d\n", rc);
2150                 GOTO(err_out, rc);
2151         }
2152
2153         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2154         if (body == NULL)
2155                 GOTO(err_out, rc = -EPROTO);
2156
2157         handle->och_fh = body->handle;
2158         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2159
2160         handle->och_mod = obd_mod_alloc();
2161         if (handle->och_mod == NULL) {
2162                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2163                 GOTO(err_out, rc = -ENOMEM);
2164         }
2165         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2166
2167         return 0;
2168
2169 err_out:
2170         ptlrpc_req_finished(req);
2171         return rc;
2172 }
2173
2174 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2175                      int flag)
2176 {
2177         struct ptlrpc_request *req;
2178         struct mdt_body       *body;
2179         int                 rc;
2180
2181         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2182                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
2183         if (req == NULL)
2184                 return -ENOMEM;
2185
2186         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2187         body->handle = handle->och_fh;
2188         body->flags = flag;
2189
2190         ptlrpc_request_set_replen(req);
2191
2192         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2193         rc = ptlrpc_queue_wait(req);
2194         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2195
2196         if (rc != 0)
2197                 CERROR("Unpin failed: %d\n", rc);
2198
2199         ptlrpc_req_finished(req);
2200         ptlrpc_req_finished(handle->och_mod->mod_open_req);
2201
2202         obd_mod_put(handle->och_mod);
2203         return rc;
2204 }
2205
2206 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2207              struct obd_capa *oc, struct ptlrpc_request **request)
2208 {
2209         struct ptlrpc_request *req;
2210         int                 rc;
2211
2212         *request = NULL;
2213         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2214         if (req == NULL)
2215                 return -ENOMEM;
2216
2217         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2218
2219         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2220         if (rc) {
2221                 ptlrpc_request_free(req);
2222                 return rc;
2223         }
2224
2225         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2226
2227         ptlrpc_request_set_replen(req);
2228
2229         rc = ptlrpc_queue_wait(req);
2230         if (rc)
2231                 ptlrpc_req_finished(req);
2232         else
2233                 *request = req;
2234         return rc;
2235 }
2236
2237 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2238                             enum obd_import_event event)
2239 {
2240         int rc = 0;
2241
2242         LASSERT(imp->imp_obd == obd);
2243
2244         switch (event) {
2245         case IMP_EVENT_DISCON: {
2246 #if 0
2247                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2248                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2249 #endif
2250                 break;
2251         }
2252         case IMP_EVENT_INACTIVE: {
2253                 struct client_obd *cli = &obd->u.cli;
2254                 /*
2255                  * Flush current sequence to make client obtain new one
2256                  * from server in case of disconnect/reconnect.
2257                  */
2258                 if (cli->cl_seq != NULL)
2259                         seq_client_flush(cli->cl_seq);
2260
2261                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2262                 break;
2263         }
2264         case IMP_EVENT_INVALIDATE: {
2265                 struct ldlm_namespace *ns = obd->obd_namespace;
2266
2267                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2268
2269                 break;
2270         }
2271         case IMP_EVENT_ACTIVE:
2272                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2273                 /* redo the kuc registration after reconnecting */
2274                 if (rc == 0)
2275                         rc = mdc_kuc_reregister(imp);
2276                 break;
2277         case IMP_EVENT_OCD:
2278                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2279                 break;
2280         case IMP_EVENT_DEACTIVATE:
2281         case IMP_EVENT_ACTIVATE:
2282                 break;
2283         default:
2284                 CERROR("Unknown import event %x\n", event);
2285                 LBUG();
2286         }
2287         return rc;
2288 }
2289
2290 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2291                   struct md_op_data *op_data)
2292 {
2293         struct client_obd *cli = &exp->exp_obd->u.cli;
2294         struct lu_client_seq *seq = cli->cl_seq;
2295
2296         return seq_client_alloc_fid(NULL, seq, fid);
2297 }
2298
2299 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2300         struct client_obd *cli = &exp->exp_obd->u.cli;
2301         return &cli->cl_target_uuid;
2302 }
2303
2304 /**
2305  * Determine whether the lock can be canceled before replaying it during
2306  * recovery, non zero value will be return if the lock can be canceled,
2307  * or zero returned for not
2308  */
2309 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2310 {
2311         if (lock->l_resource->lr_type != LDLM_IBITS)
2312                 return 0;
2313
2314         /* FIXME: if we ever get into a situation where there are too many
2315          * opened files with open locks on a single node, then we really
2316          * should replay these open locks to reget it */
2317         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2318                 return 0;
2319
2320         return 1;
2321 }
2322
2323 static int mdc_resource_inode_free(struct ldlm_resource *res)
2324 {
2325         if (res->lr_lvb_inode)
2326                 res->lr_lvb_inode = NULL;
2327
2328         return 0;
2329 }
2330
2331 struct ldlm_valblock_ops inode_lvbo = {
2332         .lvbo_free = mdc_resource_inode_free,
2333 };
2334
2335 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2336 {
2337         struct client_obd *cli = &obd->u.cli;
2338         struct lprocfs_static_vars lvars = { 0 };
2339         int rc;
2340
2341         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2342         if (!cli->cl_rpc_lock)
2343                 return -ENOMEM;
2344         mdc_init_rpc_lock(cli->cl_rpc_lock);
2345
2346         ptlrpcd_addref();
2347
2348         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2349         if (!cli->cl_close_lock)
2350                 GOTO(err_rpc_lock, rc = -ENOMEM);
2351         mdc_init_rpc_lock(cli->cl_close_lock);
2352
2353         rc = client_obd_setup(obd, cfg);
2354         if (rc)
2355                 GOTO(err_close_lock, rc);
2356         lprocfs_mdc_init_vars(&lvars);
2357         lprocfs_obd_setup(obd, lvars.obd_vars);
2358         sptlrpc_lprocfs_cliobd_attach(obd);
2359         ptlrpc_lprocfs_register_obd(obd);
2360
2361         ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2362
2363         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2364
2365         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2366         if (rc) {
2367                 mdc_cleanup(obd);
2368                 CERROR("failed to setup llogging subsystems\n");
2369         }
2370
2371         return rc;
2372
2373 err_close_lock:
2374         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2375 err_rpc_lock:
2376         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2377         ptlrpcd_decref();
2378         return rc;
2379 }
2380
2381 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2382  * us to make MDS RPCs with large enough reply buffers to hold the
2383  * maximum-sized (= maximum striped) EA and cookie without having to
2384  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2385 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2386                      int def_easize, int cookiesize)
2387 {
2388         struct obd_device *obd = exp->exp_obd;
2389         struct client_obd *cli = &obd->u.cli;
2390
2391         if (cli->cl_max_mds_easize < easize)
2392                 cli->cl_max_mds_easize = easize;
2393
2394         if (cli->cl_default_mds_easize < def_easize)
2395                 cli->cl_default_mds_easize = def_easize;
2396
2397         if (cli->cl_max_mds_cookiesize < cookiesize)
2398                 cli->cl_max_mds_cookiesize = cookiesize;
2399
2400         return 0;
2401 }
2402
2403 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2404 {
2405         int rc = 0;
2406
2407         switch (stage) {
2408         case OBD_CLEANUP_EARLY:
2409                 break;
2410         case OBD_CLEANUP_EXPORTS:
2411                 /* Failsafe, ok if racy */
2412                 if (obd->obd_type->typ_refcnt <= 1)
2413                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2414
2415                 obd_cleanup_client_import(obd);
2416                 ptlrpc_lprocfs_unregister_obd(obd);
2417                 lprocfs_obd_cleanup(obd);
2418
2419                 rc = obd_llog_finish(obd, 0);
2420                 if (rc != 0)
2421                         CERROR("failed to cleanup llogging subsystems\n");
2422                 break;
2423         }
2424         return rc;
2425 }
2426
2427 static int mdc_cleanup(struct obd_device *obd)
2428 {
2429         struct client_obd *cli = &obd->u.cli;
2430
2431         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2432         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2433
2434         ptlrpcd_decref();
2435
2436         return client_obd_cleanup(obd);
2437 }
2438
2439
2440 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2441                          struct obd_device *tgt, int *index)
2442 {
2443         struct llog_ctxt        *ctxt;
2444         int                      rc;
2445
2446         LASSERT(olg == &obd->obd_olg);
2447
2448         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2449                         &llog_client_ops);
2450         if (rc)
2451                 return rc;
2452
2453         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2454         llog_initiator_connect(ctxt);
2455         llog_ctxt_put(ctxt);
2456
2457         return 0;
2458 }
2459
2460 static int mdc_llog_finish(struct obd_device *obd, int count)
2461 {
2462         struct llog_ctxt *ctxt;
2463
2464         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2465         if (ctxt)
2466                 llog_cleanup(NULL, ctxt);
2467
2468         return 0;
2469 }
2470
2471 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2472 {
2473         struct lustre_cfg *lcfg = buf;
2474         struct lprocfs_static_vars lvars = { 0 };
2475         int rc = 0;
2476
2477         lprocfs_mdc_init_vars(&lvars);
2478         switch (lcfg->lcfg_command) {
2479         default:
2480                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2481                                               lcfg, obd);
2482                 if (rc > 0)
2483                         rc = 0;
2484                 break;
2485         }
2486         return(rc);
2487 }
2488
2489
2490 /* get remote permission for current user on fid */
2491 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2492                         struct obd_capa *oc, __u32 suppgid,
2493                         struct ptlrpc_request **request)
2494 {
2495         struct ptlrpc_request  *req;
2496         int                 rc;
2497
2498         LASSERT(client_is_remote(exp));
2499
2500         *request = NULL;
2501         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2502         if (req == NULL)
2503                 return -ENOMEM;
2504
2505         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2506
2507         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2508         if (rc) {
2509                 ptlrpc_request_free(req);
2510                 return rc;
2511         }
2512
2513         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2514
2515         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2516                              sizeof(struct mdt_remote_perm));
2517
2518         ptlrpc_request_set_replen(req);
2519
2520         rc = ptlrpc_queue_wait(req);
2521         if (rc)
2522                 ptlrpc_req_finished(req);
2523         else
2524                 *request = req;
2525         return rc;
2526 }
2527
2528 static int mdc_interpret_renew_capa(const struct lu_env *env,
2529                                     struct ptlrpc_request *req, void *args,
2530                                     int status)
2531 {
2532         struct mdc_renew_capa_args *ra = args;
2533         struct mdt_body *body = NULL;
2534         struct lustre_capa *capa;
2535
2536         if (status)
2537                 GOTO(out, capa = ERR_PTR(status));
2538
2539         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2540         if (body == NULL)
2541                 GOTO(out, capa = ERR_PTR(-EFAULT));
2542
2543         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2544                 GOTO(out, capa = ERR_PTR(-ENOENT));
2545
2546         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2547         if (!capa)
2548                 GOTO(out, capa = ERR_PTR(-EFAULT));
2549 out:
2550         ra->ra_cb(ra->ra_oc, capa);
2551         return 0;
2552 }
2553
2554 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2555                           renew_capa_cb_t cb)
2556 {
2557         struct ptlrpc_request *req;
2558         struct mdc_renew_capa_args *ra;
2559
2560         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2561                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
2562         if (req == NULL)
2563                 return -ENOMEM;
2564
2565         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2566          * capa to renew is oss capa.
2567          */
2568         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2569         ptlrpc_request_set_replen(req);
2570
2571         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2572         ra = ptlrpc_req_async_args(req);
2573         ra->ra_oc = oc;
2574         ra->ra_cb = cb;
2575         req->rq_interpret_reply = mdc_interpret_renew_capa;
2576         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2577         return 0;
2578 }
2579
2580 static int mdc_connect(const struct lu_env *env,
2581                        struct obd_export **exp,
2582                        struct obd_device *obd, struct obd_uuid *cluuid,
2583                        struct obd_connect_data *data,
2584                        void *localdata)
2585 {
2586         struct obd_import *imp = obd->u.cli.cl_import;
2587
2588         /* mds-mds import features */
2589         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2590                 spin_lock(&imp->imp_lock);
2591                 imp->imp_server_timeout = 1;
2592                 spin_unlock(&imp->imp_lock);
2593                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2594                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2595                        obd->obd_name);
2596         }
2597
2598         return client_connect_import(env, exp, obd, cluuid, data, NULL);
2599 }
2600
2601 struct obd_ops mdc_obd_ops = {
2602         .o_owner            = THIS_MODULE,
2603         .o_setup            = mdc_setup,
2604         .o_precleanup       = mdc_precleanup,
2605         .o_cleanup        = mdc_cleanup,
2606         .o_add_conn      = client_import_add_conn,
2607         .o_del_conn      = client_import_del_conn,
2608         .o_connect        = mdc_connect,
2609         .o_disconnect       = client_disconnect_export,
2610         .o_iocontrol    = mdc_iocontrol,
2611         .o_set_info_async   = mdc_set_info_async,
2612         .o_statfs          = mdc_statfs,
2613         .o_pin        = mdc_pin,
2614         .o_unpin            = mdc_unpin,
2615         .o_fid_init         = client_fid_init,
2616         .o_fid_fini         = client_fid_fini,
2617         .o_fid_alloc    = mdc_fid_alloc,
2618         .o_import_event     = mdc_import_event,
2619         .o_llog_init    = mdc_llog_init,
2620         .o_llog_finish      = mdc_llog_finish,
2621         .o_get_info      = mdc_get_info,
2622         .o_process_config   = mdc_process_config,
2623         .o_get_uuid      = mdc_get_uuid,
2624         .o_quotactl      = mdc_quotactl,
2625         .o_quotacheck       = mdc_quotacheck
2626 };
2627
2628 struct md_ops mdc_md_ops = {
2629         .m_getstatus    = mdc_getstatus,
2630         .m_null_inode       = mdc_null_inode,
2631         .m_find_cbdata      = mdc_find_cbdata,
2632         .m_close            = mdc_close,
2633         .m_create          = mdc_create,
2634         .m_done_writing     = mdc_done_writing,
2635         .m_enqueue        = mdc_enqueue,
2636         .m_getattr        = mdc_getattr,
2637         .m_getattr_name     = mdc_getattr_name,
2638         .m_intent_lock      = mdc_intent_lock,
2639         .m_link      = mdc_link,
2640         .m_is_subdir    = mdc_is_subdir,
2641         .m_rename          = mdc_rename,
2642         .m_setattr        = mdc_setattr,
2643         .m_setxattr      = mdc_setxattr,
2644         .m_getxattr      = mdc_getxattr,
2645         .m_sync      = mdc_sync,
2646         .m_readpage      = mdc_readpage,
2647         .m_unlink          = mdc_unlink,
2648         .m_cancel_unused    = mdc_cancel_unused,
2649         .m_init_ea_size     = mdc_init_ea_size,
2650         .m_set_lock_data    = mdc_set_lock_data,
2651         .m_lock_match       = mdc_lock_match,
2652         .m_get_lustre_md    = mdc_get_lustre_md,
2653         .m_free_lustre_md   = mdc_free_lustre_md,
2654         .m_set_open_replay_data = mdc_set_open_replay_data,
2655         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2656         .m_renew_capa       = mdc_renew_capa,
2657         .m_unpack_capa      = mdc_unpack_capa,
2658         .m_get_remote_perm  = mdc_get_remote_perm,
2659         .m_intent_getattr_async = mdc_intent_getattr_async,
2660         .m_revalidate_lock      = mdc_revalidate_lock
2661 };
2662
2663 int __init mdc_init(void)
2664 {
2665         int rc;
2666         struct lprocfs_static_vars lvars = { 0 };
2667         lprocfs_mdc_init_vars(&lvars);
2668
2669         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2670                                  LUSTRE_MDC_NAME, NULL);
2671         return rc;
2672 }
2673
2674 static void /*__exit*/ mdc_exit(void)
2675 {
2676         class_unregister_type(LUSTRE_MDC_NAME);
2677 }
2678
2679 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2680 MODULE_DESCRIPTION("Lustre Metadata Client");
2681 MODULE_LICENSE("GPL");
2682
2683 module_init(mdc_init);
2684 module_exit(mdc_exit);