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