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