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