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