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