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