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