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