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