Whamcloud - gitweb
74afc27760081d0b88d65d81cdfa84aabfa0bf65
[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  2008 Sun Microsystems, Inc. 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         LASSERT(mod != LP_POISON && mod != NULL);
771
772         mod->mod_och = NULL;
773         och->och_mod = NULL;
774         obd_mod_put(mod);
775
776         RETURN(0);
777 }
778
779 /* Prepares the request for the replay by the given reply */
780 static void mdc_close_handle_reply(struct ptlrpc_request *req,
781                                    struct md_op_data *op_data, int rc) {
782         struct mdt_body  *repbody;
783         struct mdt_ioepoch *epoch;
784
785         if (req && rc == -EAGAIN) {
786                 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
787                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
788
789                 epoch->flags |= MF_SOM_AU;
790                 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
791                         op_data->op_flags |= MF_GETATTR_LOCK;
792         }
793 }
794
795 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
796               struct md_open_data *mod, struct ptlrpc_request **request)
797 {
798         struct obd_device     *obd = class_exp2obd(exp);
799         struct ptlrpc_request *req;
800         int                    rc;
801         ENTRY;
802
803         *request = NULL;
804         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
805         if (req == NULL)
806                 RETURN(-ENOMEM);
807
808         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
809
810         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
811         if (rc) {
812                 ptlrpc_request_free(req);
813                 RETURN(rc);
814         }
815
816         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
817          * portal whose threads are not taking any DLM locks and are therefore
818          * always progressing */
819         req->rq_request_portal = MDS_READPAGE_PORTAL;
820         ptlrpc_at_set_req_timeout(req);
821
822         /* Ensure that this close's handle is fixed up during replay. */
823         if (likely(mod != NULL)) {
824                 LASSERTF(mod->mod_open_req != NULL &&
825                          mod->mod_open_req->rq_type != LI_POISON,
826                          "POISONED open %p!\n", mod->mod_open_req);
827
828                 mod->mod_close_req = req;
829
830                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
831                 /* We no longer want to preserve this open for replay even
832                  * though the open was committed. b=3632, b=3633 */
833                 cfs_spin_lock(&mod->mod_open_req->rq_lock);
834                 mod->mod_open_req->rq_replay = 0;
835                 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
836         } else {
837                  CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
838         }
839
840         mdc_close_pack(req, op_data);
841
842         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
843                              obd->u.cli.cl_max_mds_easize);
844         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
845                              obd->u.cli.cl_max_mds_cookiesize);
846
847         ptlrpc_request_set_replen(req);
848
849         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
850         rc = ptlrpc_queue_wait(req);
851         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
852
853         if (req->rq_repmsg == NULL) {
854                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
855                        req->rq_status);
856                 if (rc == 0)
857                         rc = req->rq_status ?: -EIO;
858         } else if (rc == 0 || rc == -EAGAIN) {
859                 struct mdt_body *body;
860
861                 rc = lustre_msg_get_status(req->rq_repmsg);
862                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
863                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
864                                   "= %d", rc);
865                         if (rc > 0)
866                                 rc = -rc;
867                 }
868                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
869                 if (body == NULL)
870                         rc = -EPROTO;
871         } else if (rc == -ESTALE) {
872                 /**
873                  * it can be allowed error after 3633 if open was committed and
874                  * server failed before close was sent. Let's check if mod
875                  * exists and return no error in that case
876                  */
877                 if (mod) {
878                         LASSERT(mod->mod_open_req != NULL);
879                         if (mod->mod_open_req->rq_committed)
880                                 rc = 0;
881                 }
882         }
883
884         if (mod) {
885                 if (rc != 0)
886                         mod->mod_close_req = NULL;
887                 /* Since now, mod is accessed through open_req only,
888                  * thus close req does not keep a reference on mod anymore. */
889                 obd_mod_put(mod);
890         }
891         *request = req;
892         mdc_close_handle_reply(req, op_data, rc);
893         RETURN(rc);
894 }
895
896 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
897                      struct md_open_data *mod)
898 {
899         struct obd_device     *obd = class_exp2obd(exp);
900         struct ptlrpc_request *req;
901         int                    rc;
902         ENTRY;
903
904         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
905                                    &RQF_MDS_DONE_WRITING);
906         if (req == NULL)
907                 RETURN(-ENOMEM);
908
909         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
910         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
911         if (rc) {
912                 ptlrpc_request_free(req);
913                 RETURN(rc);
914         }
915
916         if (mod != NULL) {
917                 LASSERTF(mod->mod_open_req != NULL &&
918                          mod->mod_open_req->rq_type != LI_POISON,
919                          "POISONED setattr %p!\n", mod->mod_open_req);
920
921                 mod->mod_close_req = req;
922                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
923                 /* We no longer want to preserve this setattr for replay even
924                  * though the open was committed. b=3632, b=3633 */
925                 cfs_spin_lock(&mod->mod_open_req->rq_lock);
926                 mod->mod_open_req->rq_replay = 0;
927                 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
928         }
929
930         mdc_close_pack(req, op_data);
931         ptlrpc_request_set_replen(req);
932
933         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
934         rc = ptlrpc_queue_wait(req);
935         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
936
937         if (rc == -ESTALE) {
938                 /**
939                  * it can be allowed error after 3633 if open or setattr were
940                  * committed and server failed before close was sent.
941                  * Let's check if mod exists and return no error in that case
942                  */
943                 if (mod) {
944                         LASSERT(mod->mod_open_req != NULL);
945                         if (mod->mod_open_req->rq_committed)
946                                 rc = 0;
947                 }
948         }
949
950         if (mod) {
951                 if (rc != 0)
952                         mod->mod_close_req = NULL;
953                 /* Since now, mod is accessed through setattr req only,
954                  * thus DW req does not keep a reference on mod anymore. */
955                 obd_mod_put(mod);
956         }
957
958         mdc_close_handle_reply(req, op_data, rc);
959         ptlrpc_req_finished(req);
960         RETURN(rc);
961 }
962
963 #ifdef HAVE_SPLIT_SUPPORT
964 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
965                  const struct page *page, int offset)
966 {
967         struct ptlrpc_request   *req;
968         struct ptlrpc_bulk_desc *desc;
969         int                      rc;
970         ENTRY;
971
972         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
973         if (req == NULL)
974                 RETURN(-ENOMEM);
975
976         /* FIXME: capa doesn't support split yet */
977         mdc_set_capa_size(req, &RMF_CAPA1, NULL);
978
979         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
980         if (rc) {
981                 ptlrpc_request_free(req);
982                 RETURN(rc);
983         }
984
985         req->rq_request_portal = MDS_READPAGE_PORTAL;
986         ptlrpc_at_set_req_timeout(req);
987
988         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
989         if (desc == NULL)
990                 GOTO(out, rc = -ENOMEM);
991
992         /* NB req now owns desc and will free it when it gets freed. */
993         ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
994         mdc_readdir_pack(req, 0, offset, fid, NULL);
995
996         ptlrpc_request_set_replen(req);
997         rc = ptlrpc_queue_wait(req);
998         if (rc)
999                 GOTO(out, rc);
1000
1001         rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1002 out:
1003         ptlrpc_req_finished(req);
1004         return rc;
1005 }
1006 EXPORT_SYMBOL(mdc_sendpage);
1007 #endif
1008
1009 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
1010                  struct obd_capa *oc, __u64 offset, struct page *page,
1011                  struct ptlrpc_request **request)
1012 {
1013         struct ptlrpc_request   *req;
1014         struct ptlrpc_bulk_desc *desc;
1015         int                      rc;
1016         ENTRY;
1017
1018         *request = NULL;
1019         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1020         if (req == NULL)
1021                 RETURN(-ENOMEM);
1022
1023         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1024
1025         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1026         if (rc) {
1027                 ptlrpc_request_free(req);
1028                 RETURN(rc);
1029         }
1030
1031         req->rq_request_portal = MDS_READPAGE_PORTAL;
1032         ptlrpc_at_set_req_timeout(req);
1033
1034         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1035         if (desc == NULL) {
1036                 ptlrpc_request_free(req);
1037                 RETURN(-ENOMEM);
1038         }
1039
1040         /* NB req now owns desc and will free it when it gets freed */
1041         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1042         mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
1043
1044         ptlrpc_request_set_replen(req);
1045         rc = ptlrpc_queue_wait(req);
1046         if (rc) {
1047                 ptlrpc_req_finished(req);
1048                 RETURN(rc);
1049         }
1050
1051         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1052                                           req->rq_bulk->bd_nob_transferred);
1053         if (rc < 0) {
1054                 ptlrpc_req_finished(req);
1055                 RETURN(rc);
1056         }
1057
1058         if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1059                 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1060                         req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1061                 ptlrpc_req_finished(req);
1062                 RETURN(-EPROTO);
1063         }
1064
1065         *request = req;
1066         RETURN(0);
1067 }
1068
1069 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1070                       __u64 max_age, __u32 flags)
1071 {
1072         struct ptlrpc_request *req;
1073         struct obd_statfs     *msfs;
1074         struct obd_import     *imp = NULL;
1075         int                    rc;
1076         ENTRY;
1077
1078         /*
1079          * Since the request might also come from lprocfs, so we need
1080          * sync this with client_disconnect_export Bug15684
1081          */
1082         cfs_down_read(&obd->u.cli.cl_sem);
1083         if (obd->u.cli.cl_import)
1084                 imp = class_import_get(obd->u.cli.cl_import);
1085         cfs_up_read(&obd->u.cli.cl_sem);
1086         if (!imp)
1087                 RETURN(-ENODEV);
1088
1089         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1090                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1091         if (req == NULL)
1092                 GOTO(output, rc = -ENOMEM);
1093
1094         ptlrpc_request_set_replen(req);
1095
1096         if (flags & OBD_STATFS_NODELAY) {
1097                 /* procfs requests not want stay in wait for avoid deadlock */
1098                 req->rq_no_resend = 1;
1099                 req->rq_no_delay = 1;
1100         }
1101
1102         rc = ptlrpc_queue_wait(req);
1103         if (rc) {
1104                 /* check connection error first */
1105                 if (imp->imp_connect_error)
1106                         rc = imp->imp_connect_error;
1107                 GOTO(out, rc);
1108         }
1109
1110         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1111         if (msfs == NULL)
1112                 GOTO(out, rc = -EPROTO);
1113
1114         *osfs = *msfs;
1115         EXIT;
1116 out:
1117         ptlrpc_req_finished(req);
1118 output:
1119         class_import_put(imp);
1120         return rc;
1121 }
1122
1123 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1124 {
1125         __u32 keylen, vallen;
1126         void *key;
1127         int rc;
1128
1129         if (gf->gf_pathlen > PATH_MAX)
1130                 RETURN(-ENAMETOOLONG);
1131         if (gf->gf_pathlen < 2)
1132                 RETURN(-EOVERFLOW);
1133
1134         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1135         keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1136         OBD_ALLOC(key, keylen);
1137         if (key == NULL)
1138                 RETURN(-ENOMEM);
1139         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1140         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1141
1142         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1143                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1144
1145         if (!fid_is_sane(&gf->gf_fid))
1146                 GOTO(out, rc = -EINVAL);
1147
1148         /* Val is struct getinfo_fid2path result plus path */
1149         vallen = sizeof(*gf) + gf->gf_pathlen;
1150
1151         rc = obd_get_info(exp, keylen, key, &vallen, gf, NULL);
1152         if (rc)
1153                 GOTO(out, rc);
1154
1155         if (vallen <= sizeof(*gf))
1156                 GOTO(out, rc = -EPROTO);
1157         else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1158                 GOTO(out, rc = -EOVERFLOW);
1159
1160         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1161                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1162
1163 out:
1164         OBD_FREE(key, keylen);
1165         return rc;
1166 }
1167
1168 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1169                          void *karg, void *uarg)
1170 {
1171         struct obd_device *obd = exp->exp_obd;
1172         struct obd_ioctl_data *data = karg;
1173         struct obd_import *imp = obd->u.cli.cl_import;
1174         struct llog_ctxt *ctxt;
1175         int rc;
1176         ENTRY;
1177
1178         if (!cfs_try_module_get(THIS_MODULE)) {
1179                 CERROR("Can't get module. Is it alive?");
1180                 return -EINVAL;
1181         }
1182         switch (cmd) {
1183         case OBD_IOC_CHANGELOG_CLEAR: {
1184                 struct ioc_changelog_clear *icc = karg;
1185                 struct changelog_setinfo cs =
1186                         {icc->icc_recno, icc->icc_id};
1187                 rc = obd_set_info_async(exp, strlen(KEY_CHANGELOG_CLEAR),
1188                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1189                                         NULL);
1190                 GOTO(out, rc);
1191         }
1192         case OBD_IOC_FID2PATH: {
1193                 rc = mdc_ioc_fid2path(exp, karg);
1194                 GOTO(out, rc);
1195         }
1196         case OBD_IOC_CLIENT_RECOVER:
1197                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1198                 if (rc < 0)
1199                         GOTO(out, rc);
1200                 GOTO(out, rc = 0);
1201         case IOC_OSC_SET_ACTIVE:
1202                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1203                 GOTO(out, rc);
1204         case OBD_IOC_PARSE: {
1205                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1206                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1207                 llog_ctxt_put(ctxt);
1208                 GOTO(out, rc);
1209         }
1210 #ifdef __KERNEL__
1211         case OBD_IOC_LLOG_INFO:
1212         case OBD_IOC_LLOG_PRINT: {
1213                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1214                 rc = llog_ioctl(ctxt, cmd, data);
1215                 llog_ctxt_put(ctxt);
1216                 GOTO(out, rc);
1217         }
1218 #endif
1219         case OBD_IOC_POLL_QUOTACHECK:
1220                 rc = lquota_poll_check(quota_interface, exp,
1221                                        (struct if_quotacheck *)karg);
1222                 GOTO(out, rc);
1223         case OBD_IOC_PING_TARGET:
1224                 rc = ptlrpc_obd_ping(obd);
1225                 GOTO(out, rc);
1226         /*
1227          * Normally IOC_OBD_STATFS iocontrol is handled by LMV instead of MDC.
1228          * But when the cluster is upgraded from 1.8, there'd be no LMV layer
1229          * thus we might be called here. Eventually this code should be removed.
1230          * bz20731.
1231          */
1232         case IOC_OBD_STATFS: {
1233                 struct obd_statfs stat_buf = {0};
1234
1235                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1236                         GOTO(out, rc = -ENODEV);
1237
1238                 rc = mdc_statfs(obd, &stat_buf,
1239                                 cfs_time_current_64() - CFS_HZ, 0);
1240                 if (rc != 0)
1241                         GOTO(out, rc);
1242
1243                 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1244                                      data->ioc_plen1))
1245                         GOTO(out, rc = -EFAULT);
1246                 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1247                                      data->ioc_plen2))
1248                         GOTO(out, rc = -EFAULT);
1249
1250                 GOTO(out, rc = 0);
1251         }
1252         default:
1253                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1254                 GOTO(out, rc = -ENOTTY);
1255         }
1256 out:
1257         cfs_module_put(THIS_MODULE);
1258
1259         return rc;
1260 }
1261
1262 int mdc_get_info_rpc(struct obd_export *exp,
1263                      obd_count keylen, void *key,
1264                      int vallen, void *val)
1265 {
1266         struct obd_import      *imp = class_exp2cliimp(exp);
1267         struct ptlrpc_request  *req;
1268         char                   *tmp;
1269         int                     rc = -EINVAL;
1270         ENTRY;
1271
1272         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1273         if (req == NULL)
1274                 RETURN(-ENOMEM);
1275
1276         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1277                              RCL_CLIENT, keylen);
1278         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1279                              RCL_CLIENT, sizeof(__u32));
1280
1281         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1282         if (rc) {
1283                 ptlrpc_request_free(req);
1284                 RETURN(rc);
1285         }
1286
1287         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1288         memcpy(tmp, key, keylen);
1289         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1290         memcpy(tmp, &vallen, sizeof(__u32));
1291
1292         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1293                              RCL_SERVER, vallen);
1294         ptlrpc_request_set_replen(req);
1295
1296         rc = ptlrpc_queue_wait(req);
1297         if (rc == 0) {
1298                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1299                 memcpy(val, tmp, vallen);
1300                 if (ptlrpc_rep_need_swab(req)) {
1301                         if (KEY_IS(KEY_FID2PATH)) {
1302                                 lustre_swab_fid2path(val);
1303                         }
1304                 }
1305         }
1306         ptlrpc_req_finished(req);
1307
1308         RETURN(rc);
1309 }
1310
1311 static void lustre_swab_hai(struct hsm_action_item *h)
1312 {
1313         __swab32s(&h->hai_len);
1314         __swab32s(&h->hai_action);
1315         lustre_swab_lu_fid(&h->hai_fid);
1316         __swab64s(&h->hai_cookie);
1317         __swab64s(&h->hai_extent_start);
1318         __swab64s(&h->hai_extent_end);
1319         __swab64s(&h->hai_gid);
1320 }
1321
1322 static void lustre_swab_hal(struct hsm_action_list *h)
1323 {
1324         struct hsm_action_item *hai;
1325         int i;
1326
1327         __swab32s(&h->hal_version);
1328         __swab32s(&h->hal_count);
1329         __swab32s(&h->hal_archive_num);
1330         hai = hai_zero(h);
1331         for (i = 0; i < h->hal_count; i++) {
1332                 lustre_swab_hai(hai);
1333                 hai = hai_next(hai);
1334         }
1335 }
1336
1337 /**
1338  * Send a message to any listening copytools, nonblocking
1339  * @param val LNL message (lnl_hdr + hsm_action_list)
1340  * @param len total length of message
1341  */
1342 static int mdc_hsm_copytool_send(int len, void *val)
1343 {
1344         struct lnl_hdr *lh = (struct lnl_hdr *)val;
1345         struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1346         int rc;
1347         ENTRY;
1348
1349         if (len < sizeof(*lh) + sizeof(*hal)) {
1350                 CERROR("Short HSM message %d < %d\n", len,
1351                       (int) (sizeof(*lh) + sizeof(*hal)));
1352                 RETURN(-EPROTO);
1353         }
1354         if (lh->lnl_magic == __swab16(LNL_MAGIC)) {
1355                 lustre_swab_lnlh(lh);
1356                 lustre_swab_hal(hal);
1357         } else if (lh->lnl_magic != LNL_MAGIC) {
1358                 CERROR("Bad magic %x!=%x\n", lh->lnl_magic, LNL_MAGIC);
1359                 RETURN(-EPROTO);
1360         }
1361
1362         CDEBUG(D_IOCTL, " Received message mg=%x t=%d m=%d l=%d actions=%d\n",
1363                lh->lnl_magic, lh->lnl_transport, lh->lnl_msgtype,
1364                lh->lnl_msglen, hal->hal_count);
1365
1366         /* Broadcast to HSM listeners */
1367         rc = libcfs_klnl_msg_put(0, LNL_GRP_HSM, lh);
1368
1369         RETURN(rc);
1370 }
1371
1372 int mdc_set_info_async(struct obd_export *exp,
1373                        obd_count keylen, void *key,
1374                        obd_count vallen, void *val,
1375                        struct ptlrpc_request_set *set)
1376 {
1377         struct obd_import *imp = class_exp2cliimp(exp);
1378         int                rc = -EINVAL;
1379         ENTRY;
1380
1381         if (KEY_IS(KEY_INIT_RECOV)) {
1382                 if (vallen != sizeof(int))
1383                         RETURN(-EINVAL);
1384                 cfs_spin_lock(&imp->imp_lock);
1385                 imp->imp_initial_recov = *(int *)val;
1386                 cfs_spin_unlock(&imp->imp_lock);
1387                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1388                        exp->exp_obd->obd_name, imp->imp_initial_recov);
1389                 RETURN(0);
1390         }
1391         /* Turn off initial_recov after we try all backup servers once */
1392         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1393                 if (vallen != sizeof(int))
1394                         RETURN(-EINVAL);
1395                 cfs_spin_lock(&imp->imp_lock);
1396                 imp->imp_initial_recov_bk = *(int *)val;
1397                 if (imp->imp_initial_recov_bk)
1398                         imp->imp_initial_recov = 1;
1399                 cfs_spin_unlock(&imp->imp_lock);
1400                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1401                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1402                 RETURN(0);
1403         }
1404         if (KEY_IS(KEY_READ_ONLY)) {
1405                 if (vallen != sizeof(int))
1406                         RETURN(-EINVAL);
1407
1408                 cfs_spin_lock(&imp->imp_lock);
1409                 if (*((int *)val)) {
1410                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1411                         imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1412                 } else {
1413                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1414                         imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1415                 }
1416                 cfs_spin_unlock(&imp->imp_lock);
1417
1418                 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1419                                          keylen, key, vallen, val, set);
1420                 RETURN(rc);
1421         }
1422         if (KEY_IS(KEY_SPTLRPC_CONF)) {
1423                 sptlrpc_conf_client_adapt(exp->exp_obd);
1424                 RETURN(0);
1425         }
1426         if (KEY_IS(KEY_FLUSH_CTX)) {
1427                 sptlrpc_import_flush_my_ctx(imp);
1428                 RETURN(0);
1429         }
1430         if (KEY_IS(KEY_MDS_CONN)) {
1431                 /* mds-mds import */
1432                 cfs_spin_lock(&imp->imp_lock);
1433                 imp->imp_server_timeout = 1;
1434                 cfs_spin_unlock(&imp->imp_lock);
1435                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1436                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1437                 RETURN(0);
1438         }
1439         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1440                 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1441                                          keylen, key, vallen, val, set);
1442                 RETURN(rc);
1443         }
1444         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
1445                 rc = mdc_hsm_copytool_send(vallen, val);
1446                 RETURN(rc);
1447         }
1448
1449         RETURN(rc);
1450 }
1451
1452 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1453                  __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1454 {
1455         int rc = -EINVAL;
1456
1457         if (KEY_IS(KEY_MAX_EASIZE)) {
1458                 int mdsize, *max_easize;
1459
1460                 if (*vallen != sizeof(int))
1461                         RETURN(-EINVAL);
1462                 mdsize = *(int*)val;
1463                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1464                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1465                 max_easize = val;
1466                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1467                 RETURN(0);
1468         }
1469         if (KEY_IS(KEY_CONN_DATA)) {
1470                 struct obd_import *imp = class_exp2cliimp(exp);
1471                 struct obd_connect_data *data = val;
1472
1473                 if (*vallen != sizeof(*data))
1474                         RETURN(-EINVAL);
1475
1476                 *data = imp->imp_connect_data;
1477                 RETURN(0);
1478         }
1479
1480         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
1481
1482         RETURN(rc);
1483 }
1484
1485 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1486                    struct obd_capa *oc, struct obd_client_handle *handle,
1487                    int flags)
1488 {
1489         struct ptlrpc_request *req;
1490         struct mdt_body       *body;
1491         int                    rc;
1492         ENTRY;
1493
1494         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1495         if (req == NULL)
1496                 RETURN(-ENOMEM);
1497
1498         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1499
1500         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1501         if (rc) {
1502                 ptlrpc_request_free(req);
1503                 RETURN(rc);
1504         }
1505
1506         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1507
1508         ptlrpc_request_set_replen(req);
1509
1510         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1511         rc = ptlrpc_queue_wait(req);
1512         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1513         if (rc) {
1514                 CERROR("Pin failed: %d\n", rc);
1515                 GOTO(err_out, rc);
1516         }
1517
1518         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1519         if (body == NULL)
1520                 GOTO(err_out, rc = -EPROTO);
1521
1522         handle->och_fh = body->handle;
1523         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1524
1525         handle->och_mod = obd_mod_alloc();
1526         if (handle->och_mod == NULL) {
1527                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1528                 GOTO(err_out, rc = -ENOMEM);
1529         }
1530         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1531
1532         RETURN(0);
1533
1534 err_out:
1535         ptlrpc_req_finished(req);
1536         RETURN(rc);
1537 }
1538
1539 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1540                      int flag)
1541 {
1542         struct ptlrpc_request *req;
1543         struct mdt_body       *body;
1544         int                    rc;
1545         ENTRY;
1546
1547         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1548                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
1549         if (req == NULL)
1550                 RETURN(-ENOMEM);
1551
1552         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1553         body->handle = handle->och_fh;
1554         body->flags = flag;
1555
1556         ptlrpc_request_set_replen(req);
1557
1558         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1559         rc = ptlrpc_queue_wait(req);
1560         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1561
1562         if (rc != 0)
1563                 CERROR("Unpin failed: %d\n", rc);
1564
1565         ptlrpc_req_finished(req);
1566         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1567
1568         obd_mod_put(handle->och_mod);
1569         RETURN(rc);
1570 }
1571
1572 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1573              struct obd_capa *oc, struct ptlrpc_request **request)
1574 {
1575         struct ptlrpc_request *req;
1576         int                    rc;
1577         ENTRY;
1578
1579         *request = NULL;
1580         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1581         if (req == NULL)
1582                 RETURN(-ENOMEM);
1583
1584         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1585
1586         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1587         if (rc) {
1588                 ptlrpc_request_free(req);
1589                 RETURN(rc);
1590         }
1591
1592         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1593
1594         ptlrpc_request_set_replen(req);
1595
1596         rc = ptlrpc_queue_wait(req);
1597         if (rc)
1598                 ptlrpc_req_finished(req);
1599         else
1600                 *request = req;
1601         RETURN(rc);
1602 }
1603
1604 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1605                             enum obd_import_event event)
1606 {
1607         int rc = 0;
1608
1609         LASSERT(imp->imp_obd == obd);
1610
1611         switch (event) {
1612         case IMP_EVENT_DISCON: {
1613 #if 0
1614                 /* XXX Pass event up to OBDs stack. used only for FLD now */
1615                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1616 #endif
1617                 break;
1618         }
1619         case IMP_EVENT_INACTIVE: {
1620                 struct client_obd *cli = &obd->u.cli;
1621                 /*
1622                  * Flush current sequence to make client obtain new one
1623                  * from server in case of disconnect/reconnect.
1624                  * If range is already empty then no need to flush it.
1625                  */
1626                 if (cli->cl_seq != NULL &&
1627                     !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1628                         seq_client_flush(cli->cl_seq);
1629                 }
1630
1631                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1632                 break;
1633         }
1634         case IMP_EVENT_INVALIDATE: {
1635                 struct ldlm_namespace *ns = obd->obd_namespace;
1636
1637                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1638
1639                 break;
1640         }
1641         case IMP_EVENT_ACTIVE: {
1642                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1643                 break;
1644         }
1645         case IMP_EVENT_OCD:
1646                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1647                 break;
1648
1649         default:
1650                 CERROR("Unknown import event %x\n", event);
1651                 LBUG();
1652         }
1653         RETURN(rc);
1654 }
1655
1656 static int mdc_fid_init(struct obd_export *exp)
1657 {
1658         struct client_obd *cli = &exp->exp_obd->u.cli;
1659         char *prefix;
1660         int rc;
1661         ENTRY;
1662
1663         OBD_ALLOC_PTR(cli->cl_seq);
1664         if (cli->cl_seq == NULL)
1665                 RETURN(-ENOMEM);
1666
1667         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1668         if (prefix == NULL)
1669                 GOTO(out_free_seq, rc = -ENOMEM);
1670
1671         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1672                  exp->exp_obd->obd_name);
1673
1674         /* Init client side sequence-manager */
1675         rc = seq_client_init(cli->cl_seq, exp,
1676                              LUSTRE_SEQ_METADATA,
1677                              prefix, NULL);
1678         OBD_FREE(prefix, MAX_OBD_NAME + 5);
1679         if (rc)
1680                 GOTO(out_free_seq, rc);
1681
1682         RETURN(rc);
1683 out_free_seq:
1684         OBD_FREE_PTR(cli->cl_seq);
1685         cli->cl_seq = NULL;
1686         return rc;
1687 }
1688
1689 static int mdc_fid_fini(struct obd_export *exp)
1690 {
1691         struct client_obd *cli = &exp->exp_obd->u.cli;
1692         ENTRY;
1693
1694         if (cli->cl_seq != NULL) {
1695                 seq_client_fini(cli->cl_seq);
1696                 OBD_FREE_PTR(cli->cl_seq);
1697                 cli->cl_seq = NULL;
1698         }
1699
1700         RETURN(0);
1701 }
1702
1703 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1704                   struct md_op_data *op_data)
1705 {
1706         struct client_obd *cli = &exp->exp_obd->u.cli;
1707         struct lu_client_seq *seq = cli->cl_seq;
1708         ENTRY;
1709         RETURN(seq_client_alloc_fid(seq, fid));
1710 }
1711
1712 /* XXX This method is used only to clear current fid seq
1713  * once fld/mds insert failed */
1714 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1715 {
1716         struct client_obd *cli = &exp->exp_obd->u.cli;
1717
1718         seq_client_flush(cli->cl_seq);
1719         return 0;
1720 }
1721
1722 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
1723         struct client_obd *cli = &exp->exp_obd->u.cli;
1724         return &cli->cl_target_uuid;
1725 }
1726
1727 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1728 {
1729         struct client_obd *cli = &obd->u.cli;
1730         struct lprocfs_static_vars lvars = { 0 };
1731         int rc;
1732         ENTRY;
1733
1734         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1735         if (!cli->cl_rpc_lock)
1736                 RETURN(-ENOMEM);
1737         mdc_init_rpc_lock(cli->cl_rpc_lock);
1738
1739         ptlrpcd_addref();
1740
1741         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1742         if (!cli->cl_setattr_lock)
1743                 GOTO(err_rpc_lock, rc = -ENOMEM);
1744         mdc_init_rpc_lock(cli->cl_setattr_lock);
1745
1746         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1747         if (!cli->cl_close_lock)
1748                 GOTO(err_setattr_lock, rc = -ENOMEM);
1749         mdc_init_rpc_lock(cli->cl_close_lock);
1750
1751         rc = client_obd_setup(obd, cfg);
1752         if (rc)
1753                 GOTO(err_close_lock, rc);
1754         lprocfs_mdc_init_vars(&lvars);
1755         lprocfs_obd_setup(obd, lvars.obd_vars);
1756         sptlrpc_lprocfs_cliobd_attach(obd);
1757         ptlrpc_lprocfs_register_obd(obd);
1758
1759         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
1760         if (rc) {
1761                 mdc_cleanup(obd);
1762                 CERROR("failed to setup llogging subsystems\n");
1763         }
1764
1765         /* ignore errors */
1766         libcfs_klnl_start(LNL_TRANSPORT_HSM);
1767         libcfs_klnl_start(LNL_TRANSPORT_CHANGELOG);
1768
1769         RETURN(rc);
1770
1771 err_close_lock:
1772         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1773 err_setattr_lock:
1774         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1775 err_rpc_lock:
1776         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1777         ptlrpcd_decref();
1778         RETURN(rc);
1779 }
1780
1781 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1782  * us to make MDS RPCs with large enough reply buffers to hold the
1783  * maximum-sized (= maximum striped) EA and cookie without having to
1784  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1785 static int mdc_init_ea_size(struct obd_export *exp, int easize,
1786                      int def_easize, int cookiesize)
1787 {
1788         struct obd_device *obd = exp->exp_obd;
1789         struct client_obd *cli = &obd->u.cli;
1790         ENTRY;
1791
1792         if (cli->cl_max_mds_easize < easize)
1793                 cli->cl_max_mds_easize = easize;
1794
1795         if (cli->cl_default_mds_easize < def_easize)
1796                 cli->cl_default_mds_easize = def_easize;
1797
1798         if (cli->cl_max_mds_cookiesize < cookiesize)
1799                 cli->cl_max_mds_cookiesize = cookiesize;
1800
1801         RETURN(0);
1802 }
1803
1804 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1805 {
1806         int rc = 0;
1807         ENTRY;
1808
1809         switch (stage) {
1810         case OBD_CLEANUP_EARLY:
1811         case OBD_CLEANUP_EXPORTS:
1812                 /* If we set up but never connected, the
1813                    client import will not have been cleaned. */
1814                 if (obd->u.cli.cl_import) {
1815                         struct obd_import *imp;
1816                         cfs_down_write(&obd->u.cli.cl_sem);
1817                         imp = obd->u.cli.cl_import;
1818                         CERROR("client import never connected\n");
1819                         ptlrpc_invalidate_import(imp);
1820                         class_destroy_import(imp);
1821                         cfs_up_write(&obd->u.cli.cl_sem);
1822                         obd->u.cli.cl_import = NULL;
1823                 }
1824                 rc = obd_llog_finish(obd, 0);
1825                 if (rc != 0)
1826                         CERROR("failed to cleanup llogging subsystems\n");
1827                 break;
1828         }
1829         RETURN(rc);
1830 }
1831
1832 static int mdc_cleanup(struct obd_device *obd)
1833 {
1834         struct client_obd *cli = &obd->u.cli;
1835
1836         libcfs_klnl_stop(LNL_TRANSPORT_HSM, LNL_GRP_HSM);
1837         libcfs_klnl_stop(LNL_TRANSPORT_CHANGELOG, 0);
1838
1839         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1840         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1841         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1842
1843         ptlrpc_lprocfs_unregister_obd(obd);
1844         lprocfs_obd_cleanup(obd);
1845         ptlrpcd_decref();
1846
1847         return client_obd_cleanup(obd);
1848 }
1849
1850
1851 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1852                          struct obd_device *tgt, int *index)
1853 {
1854         struct llog_ctxt *ctxt;
1855         int rc;
1856         ENTRY;
1857
1858         LASSERT(olg == &obd->obd_olg);
1859
1860         rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
1861                         &llog_client_ops);
1862         if (rc)
1863                 RETURN(rc);
1864
1865         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1866         llog_initiator_connect(ctxt);
1867         llog_ctxt_put(ctxt);
1868
1869         rc = llog_setup(obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt, 0, NULL,
1870                         &llog_client_ops);
1871         if (rc == 0) {
1872                 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
1873                 llog_initiator_connect(ctxt);
1874                 llog_ctxt_put(ctxt);
1875         }
1876
1877         RETURN(rc);
1878 }
1879
1880 static int mdc_llog_finish(struct obd_device *obd, int count)
1881 {
1882         struct llog_ctxt *ctxt;
1883         int rc = 0;
1884         ENTRY;
1885
1886         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1887         if (ctxt)
1888                 rc = llog_cleanup(ctxt);
1889
1890         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
1891         if (ctxt)
1892                 rc = llog_cleanup(ctxt);
1893
1894         RETURN(rc);
1895 }
1896
1897 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1898 {
1899         struct lustre_cfg *lcfg = buf;
1900         struct lprocfs_static_vars lvars = { 0 };
1901         int rc = 0;
1902
1903         lprocfs_mdc_init_vars(&lvars);
1904         switch (lcfg->lcfg_command) {
1905         default:
1906                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
1907                                               lcfg, obd);
1908                 if (rc > 0)
1909                         rc = 0;
1910                 break;
1911         }
1912         return(rc);
1913 }
1914
1915
1916 /* get remote permission for current user on fid */
1917 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
1918                         struct obd_capa *oc, __u32 suppgid,
1919                         struct ptlrpc_request **request)
1920 {
1921         struct ptlrpc_request  *req;
1922         int                    rc;
1923         ENTRY;
1924
1925         LASSERT(client_is_remote(exp));
1926
1927         *request = NULL;
1928         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
1929         if (req == NULL)
1930                 RETURN(-ENOMEM);
1931
1932         mdc_set_capa_size(req, &RMF_CAPA1, oc);
1933
1934         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
1935         if (rc) {
1936                 ptlrpc_request_free(req);
1937                 RETURN(rc);
1938         }
1939
1940         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
1941
1942         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
1943                              sizeof(struct mdt_remote_perm));
1944
1945         ptlrpc_request_set_replen(req);
1946
1947         rc = ptlrpc_queue_wait(req);
1948         if (rc)
1949                 ptlrpc_req_finished(req);
1950         else
1951                 *request = req;
1952         RETURN(rc);
1953 }
1954
1955 static int mdc_interpret_renew_capa(const struct lu_env *env,
1956                                     struct ptlrpc_request *req, void *unused,
1957                                     int status)
1958 {
1959         struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
1960         renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
1961         struct mdt_body *body = NULL;
1962         struct lustre_capa *capa;
1963         ENTRY;
1964
1965         if (status)
1966                 GOTO(out, capa = ERR_PTR(status));
1967
1968         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1969         if (body == NULL)
1970                 GOTO(out, capa = ERR_PTR(-EFAULT));
1971
1972         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
1973                 GOTO(out, capa = ERR_PTR(-ENOENT));
1974
1975         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
1976         if (!capa)
1977                 GOTO(out, capa = ERR_PTR(-EFAULT));
1978         EXIT;
1979 out:
1980         cb(oc, capa);
1981         return 0;
1982 }
1983
1984 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
1985                           renew_capa_cb_t cb)
1986 {
1987         struct ptlrpc_request *req;
1988         ENTRY;
1989
1990         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
1991                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
1992         if (req == NULL)
1993                 RETURN(-ENOMEM);
1994
1995         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
1996          * capa to renew is oss capa.
1997          */
1998         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
1999         ptlrpc_request_set_replen(req);
2000
2001         req->rq_async_args.pointer_arg[0] = oc;
2002         req->rq_async_args.pointer_arg[1] = cb;
2003         req->rq_interpret_reply = mdc_interpret_renew_capa;
2004         ptlrpcd_add_req(req, PSCOPE_OTHER);
2005         RETURN(0);
2006 }
2007
2008 static int mdc_connect(const struct lu_env *env,
2009                        struct obd_export **exp,
2010                        struct obd_device *obd, struct obd_uuid *cluuid,
2011                        struct obd_connect_data *data,
2012                        void *localdata)
2013 {
2014         struct obd_import *imp = obd->u.cli.cl_import;
2015
2016         /* mds-mds import features */
2017         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2018                 cfs_spin_lock(&imp->imp_lock);
2019                 imp->imp_server_timeout = 1;
2020                 cfs_spin_unlock(&imp->imp_lock);
2021                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2022                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2023                        obd->obd_name);
2024         }
2025
2026         return client_connect_import(env, exp, obd, cluuid, data, NULL);
2027 }
2028
2029 struct obd_ops mdc_obd_ops = {
2030         .o_owner            = THIS_MODULE,
2031         .o_setup            = mdc_setup,
2032         .o_precleanup       = mdc_precleanup,
2033         .o_cleanup          = mdc_cleanup,
2034         .o_add_conn         = client_import_add_conn,
2035         .o_del_conn         = client_import_del_conn,
2036         .o_connect          = mdc_connect,
2037         .o_disconnect       = client_disconnect_export,
2038         .o_iocontrol        = mdc_iocontrol,
2039         .o_set_info_async   = mdc_set_info_async,
2040         .o_statfs           = mdc_statfs,
2041         .o_pin              = mdc_pin,
2042         .o_unpin            = mdc_unpin,
2043         .o_fid_init         = mdc_fid_init,
2044         .o_fid_fini         = mdc_fid_fini,
2045         .o_fid_alloc        = mdc_fid_alloc,
2046         .o_fid_delete       = mdc_fid_delete,
2047         .o_import_event     = mdc_import_event,
2048         .o_llog_init        = mdc_llog_init,
2049         .o_llog_finish      = mdc_llog_finish,
2050         .o_get_info         = mdc_get_info,
2051         .o_process_config   = mdc_process_config,
2052         .o_get_uuid         = mdc_get_uuid,
2053 };
2054
2055 struct md_ops mdc_md_ops = {
2056         .m_getstatus        = mdc_getstatus,
2057         .m_change_cbdata    = mdc_change_cbdata,
2058         .m_close            = mdc_close,
2059         .m_create           = mdc_create,
2060         .m_done_writing     = mdc_done_writing,
2061         .m_enqueue          = mdc_enqueue,
2062         .m_getattr          = mdc_getattr,
2063         .m_getattr_name     = mdc_getattr_name,
2064         .m_intent_lock      = mdc_intent_lock,
2065         .m_link             = mdc_link,
2066         .m_is_subdir        = mdc_is_subdir,
2067         .m_rename           = mdc_rename,
2068         .m_setattr          = mdc_setattr,
2069         .m_setxattr         = mdc_setxattr,
2070         .m_getxattr         = mdc_getxattr,
2071         .m_sync             = mdc_sync,
2072         .m_readpage         = mdc_readpage,
2073         .m_unlink           = mdc_unlink,
2074         .m_cancel_unused    = mdc_cancel_unused,
2075         .m_init_ea_size     = mdc_init_ea_size,
2076         .m_set_lock_data    = mdc_set_lock_data,
2077         .m_lock_match       = mdc_lock_match,
2078         .m_get_lustre_md    = mdc_get_lustre_md,
2079         .m_free_lustre_md   = mdc_free_lustre_md,
2080         .m_set_open_replay_data = mdc_set_open_replay_data,
2081         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2082         .m_renew_capa       = mdc_renew_capa,
2083         .m_unpack_capa      = mdc_unpack_capa,
2084         .m_get_remote_perm  = mdc_get_remote_perm,
2085         .m_intent_getattr_async = mdc_intent_getattr_async,
2086         .m_revalidate_lock      = mdc_revalidate_lock
2087 };
2088
2089 int __init mdc_init(void)
2090 {
2091         int rc;
2092         struct lprocfs_static_vars lvars = { 0 };
2093         lprocfs_mdc_init_vars(&lvars);
2094
2095         cfs_request_module("lquota");
2096         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
2097         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
2098
2099         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2100                                  LUSTRE_MDC_NAME, NULL);
2101         if (rc && quota_interface)
2102                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2103
2104         RETURN(rc);
2105 }
2106
2107 #ifdef __KERNEL__
2108 static void /*__exit*/ mdc_exit(void)
2109 {
2110         if (quota_interface)
2111                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2112
2113         class_unregister_type(LUSTRE_MDC_NAME);
2114 }
2115
2116 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2117 MODULE_DESCRIPTION("Lustre Metadata Client");
2118 MODULE_LICENSE("GPL");
2119
2120 module_init(mdc_init);
2121 module_exit(mdc_exit);
2122 #endif