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