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