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