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