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