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