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