Whamcloud - gitweb
LU-6245 libcfs: remove mem wrappers for libcfs
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, 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 <lustre_acl.h>
46 #include <lustre_ioctl.h>
47 #include <obd_class.h>
48 #include <lustre_lmv.h>
49 #include <lustre_fid.h>
50 #include <lprocfs_status.h>
51 #include <lustre_param.h>
52 #include <lustre_log.h>
53 #include <lustre_kernelcomm.h>
54 #include <cl_object.h>
55
56 #include "mdc_internal.h"
57
58 #define REQUEST_MINOR 244
59
60 struct mdc_renew_capa_args {
61         struct obd_capa        *ra_oc;
62         renew_capa_cb_t         ra_cb;
63 };
64
65 static int mdc_cleanup(struct obd_device *obd);
66
67 static int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
68                            const struct req_msg_field *field,
69                            struct obd_capa **oc)
70 {
71         struct lustre_capa *capa;
72         struct obd_capa *c;
73         ENTRY;
74
75         /* swabbed already in mdc_enqueue */
76         capa = req_capsule_server_get(&req->rq_pill, field);
77         if (capa == NULL)
78                 RETURN(-EPROTO);
79
80         c = alloc_capa(CAPA_SITE_CLIENT);
81         if (IS_ERR(c)) {
82                 CDEBUG(D_INFO, "alloc capa failed!\n");
83                 RETURN(PTR_ERR(c));
84         } else {
85                 c->c_capa = *capa;
86                 *oc = c;
87                 RETURN(0);
88         }
89 }
90
91 static inline int mdc_queue_wait(struct ptlrpc_request *req)
92 {
93         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
94         int rc;
95
96         /* obd_get_request_slot() ensures that this client has no more
97          * than cl_max_rpcs_in_flight RPCs simultaneously inf light
98          * against an MDT. */
99         rc = obd_get_request_slot(cli);
100         if (rc != 0)
101                 return rc;
102
103         rc = ptlrpc_queue_wait(req);
104         obd_put_request_slot(cli);
105
106         return rc;
107 }
108
109 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
110 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
111 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
112                           struct obd_capa **pc, int level, int msg_flags)
113 {
114         struct ptlrpc_request *req;
115         struct mdt_body       *body;
116         int                    rc;
117         ENTRY;
118
119         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
120                                         LUSTRE_MDS_VERSION, MDS_GETSTATUS);
121         if (req == NULL)
122                 RETURN(-ENOMEM);
123
124         mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
125         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
126         req->rq_send_state = level;
127
128         ptlrpc_request_set_replen(req);
129
130         rc = ptlrpc_queue_wait(req);
131         if (rc)
132                 GOTO(out, rc);
133
134         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
135         if (body == NULL)
136                 GOTO(out, rc = -EPROTO);
137
138         if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
139                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
140                 if (rc)
141                         GOTO(out, rc);
142         }
143
144         *rootfid = body->mbo_fid1;
145         CDEBUG(D_NET, "root fid="DFID", last_committed="LPU64"\n",
146                PFID(rootfid), lustre_msg_get_last_committed(req->rq_repmsg));
147         EXIT;
148 out:
149         ptlrpc_req_finished(req);
150         return rc;
151 }
152
153 /* This should be mdc_get_info("rootfid") */
154 static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
155                          struct obd_capa **pc)
156 {
157         return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
158                               LUSTRE_IMP_FULL, 0);
159 }
160
161 /*
162  * This function now is known to always saying that it will receive 4 buffers
163  * from server. Even for cases when acl_size and md_size is zero, RPC header
164  * will contain 4 fields and RPC itself will contain zero size fields. This is
165  * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
166  * and thus zero, it shrinks it, making zero size. The same story about
167  * md_size. And this is course of problem when client waits for smaller number
168  * of fields. This issue will be fixed later when client gets aware of RPC
169  * layouts.  --umka
170  */
171 static int mdc_getattr_common(struct obd_export *exp,
172                               struct ptlrpc_request *req)
173 {
174         struct req_capsule *pill = &req->rq_pill;
175         struct mdt_body    *body;
176         void               *eadata;
177         int                 rc;
178         ENTRY;
179
180         /* Request message already built. */
181         rc = ptlrpc_queue_wait(req);
182         if (rc != 0)
183                 RETURN(rc);
184
185         /* sanity check for the reply */
186         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
187         if (body == NULL)
188                 RETURN(-EPROTO);
189
190         CDEBUG(D_NET, "mode: %o\n", body->mbo_mode);
191
192         mdc_update_max_ea_from_body(exp, body);
193         if (body->mbo_eadatasize != 0) {
194                 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
195                                                       body->mbo_eadatasize);
196                 if (eadata == NULL)
197                         RETURN(-EPROTO);
198         }
199
200         if (body->mbo_valid & OBD_MD_FLRMTPERM) {
201                 struct mdt_remote_perm *perm;
202
203                 LASSERT(client_is_remote(exp));
204                 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
205                                                 lustre_swab_mdt_remote_perm);
206                 if (perm == NULL)
207                         RETURN(-EPROTO);
208         }
209
210         if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
211                 struct lustre_capa *capa;
212                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
213                 if (capa == NULL)
214                         RETURN(-EPROTO);
215         }
216
217         RETURN(0);
218 }
219
220 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
221                        struct ptlrpc_request **request)
222 {
223         struct ptlrpc_request *req;
224         int                    rc;
225         ENTRY;
226
227         /* Single MDS without an LMV case */
228         if (op_data->op_flags & MF_GET_MDT_IDX) {
229                 op_data->op_mds = 0;
230                 RETURN(0);
231         }
232         *request = NULL;
233         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
234         if (req == NULL)
235                 RETURN(-ENOMEM);
236
237         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
238
239         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
240         if (rc) {
241                 ptlrpc_request_free(req);
242                 RETURN(rc);
243         }
244
245         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
246                       op_data->op_valid, op_data->op_mode, -1, 0);
247
248         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
249                              op_data->op_mode);
250         if (op_data->op_valid & OBD_MD_FLRMTPERM) {
251                 LASSERT(client_is_remote(exp));
252                 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
253                                      sizeof(struct mdt_remote_perm));
254         }
255         ptlrpc_request_set_replen(req);
256
257         rc = mdc_getattr_common(exp, req);
258         if (rc)
259                 ptlrpc_req_finished(req);
260         else
261                 *request = req;
262         RETURN(rc);
263 }
264
265 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
266                             struct ptlrpc_request **request)
267 {
268         struct ptlrpc_request *req;
269         int                    rc;
270         ENTRY;
271
272         *request = NULL;
273         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
274                                    &RQF_MDS_GETATTR_NAME);
275         if (req == NULL)
276                 RETURN(-ENOMEM);
277
278         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
279         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
280                              op_data->op_namelen + 1);
281
282         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
283         if (rc) {
284                 ptlrpc_request_free(req);
285                 RETURN(rc);
286         }
287
288         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
289                       op_data->op_valid, op_data->op_mode,
290                       op_data->op_suppgids[0], 0);
291
292         if (op_data->op_name) {
293                 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
294                 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
295                                 op_data->op_namelen);
296                 memcpy(name, op_data->op_name, op_data->op_namelen);
297         }
298
299         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
300                              op_data->op_mode);
301         ptlrpc_request_set_replen(req);
302
303         rc = mdc_getattr_common(exp, req);
304         if (rc)
305                 ptlrpc_req_finished(req);
306         else
307                 *request = req;
308         RETURN(rc);
309 }
310
311 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
312                             const struct lu_fid *fid,
313                             struct obd_capa *oc, int opcode, u64 valid,
314                             const char *xattr_name, const char *input,
315                             int input_size, int output_size, int flags,
316                             __u32 suppgid, struct ptlrpc_request **request)
317 {
318         struct ptlrpc_request *req;
319         int   xattr_namelen = 0;
320         char *tmp;
321         int   rc;
322         ENTRY;
323
324         *request = NULL;
325         req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
326         if (req == NULL)
327                 RETURN(-ENOMEM);
328
329         mdc_set_capa_size(req, &RMF_CAPA1, oc);
330         if (xattr_name) {
331                 xattr_namelen = strlen(xattr_name) + 1;
332                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
333                                      xattr_namelen);
334         }
335         if (input_size) {
336                 LASSERT(input);
337                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
338                                      input_size);
339         }
340
341         /* Flush local XATTR locks to get rid of a possible cancel RPC */
342         if (opcode == MDS_REINT && fid_is_sane(fid) &&
343             exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
344                 struct list_head cancels = LIST_HEAD_INIT(cancels);
345                 int count;
346
347                 /* Without that packing would fail */
348                 if (input_size == 0)
349                         req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
350                                              RCL_CLIENT, 0);
351
352                 count = mdc_resource_get_unused(exp, fid,
353                                                 &cancels, LCK_EX,
354                                                 MDS_INODELOCK_XATTR);
355
356                 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
357                 if (rc) {
358                         ptlrpc_request_free(req);
359                         RETURN(rc);
360                 }
361         } else {
362                 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
363                 if (rc) {
364                         ptlrpc_request_free(req);
365                         RETURN(rc);
366                 }
367         }
368
369         if (opcode == MDS_REINT) {
370                 struct mdt_rec_setxattr *rec;
371
372                 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
373                          sizeof(struct mdt_rec_reint));
374                 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
375                 rec->sx_opcode = REINT_SETXATTR;
376                 rec->sx_fsuid  = from_kuid(&init_user_ns, current_fsuid());
377                 rec->sx_fsgid  = from_kgid(&init_user_ns, current_fsgid());
378                 rec->sx_cap    = cfs_curproc_cap_pack();
379                 rec->sx_suppgid1 = suppgid;
380                 rec->sx_suppgid2 = -1;
381                 rec->sx_fid    = *fid;
382                 rec->sx_valid  = valid | OBD_MD_FLCTIME;
383                 rec->sx_time   = cfs_time_current_sec();
384                 rec->sx_size   = output_size;
385                 rec->sx_flags  = flags;
386
387                 mdc_pack_capa(req, &RMF_CAPA1, oc);
388         } else {
389                 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
390         }
391
392         if (xattr_name) {
393                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
394                 memcpy(tmp, xattr_name, xattr_namelen);
395         }
396         if (input_size) {
397                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
398                 memcpy(tmp, input, input_size);
399         }
400
401         if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
402                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
403                                      RCL_SERVER, output_size);
404         ptlrpc_request_set_replen(req);
405
406         /* make rpc */
407         if (opcode == MDS_REINT)
408                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
409
410         rc = ptlrpc_queue_wait(req);
411
412         if (opcode == MDS_REINT)
413                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
414
415         if (rc)
416                 ptlrpc_req_finished(req);
417         else
418                 *request = req;
419         RETURN(rc);
420 }
421
422 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
423                         struct obd_capa *oc, u64 valid,
424                         const char *xattr_name,
425                         const char *input, int input_size, int output_size,
426                         int flags, __u32 suppgid,
427                         struct ptlrpc_request **request)
428 {
429         return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
430                                 fid, oc, MDS_REINT, valid, xattr_name,
431                                 input, input_size, output_size, flags,
432                                 suppgid, request);
433 }
434
435 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
436                         struct obd_capa *oc, u64 valid,
437                         const char *xattr_name,
438                         const char *input, int input_size, int output_size,
439                         int flags, struct ptlrpc_request **request)
440 {
441         return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
442                                 fid, oc, MDS_GETXATTR, valid, xattr_name,
443                                 input, input_size, output_size, flags,
444                                 -1, request);
445 }
446
447 #ifdef CONFIG_FS_POSIX_ACL
448 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
449 {
450         struct req_capsule     *pill = &req->rq_pill;
451         struct mdt_body        *body = md->body;
452         struct posix_acl       *acl;
453         void                   *buf;
454         int                     rc;
455         ENTRY;
456
457         if (!body->mbo_aclsize)
458                 RETURN(0);
459
460         buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->mbo_aclsize);
461
462         if (!buf)
463                 RETURN(-EPROTO);
464
465         acl = posix_acl_from_xattr(&init_user_ns, buf, body->mbo_aclsize);
466         if (acl == NULL)
467                 RETURN(0);
468         if (IS_ERR(acl)) {
469                 rc = PTR_ERR(acl);
470                 CERROR("convert xattr to acl: %d\n", rc);
471                 RETURN(rc);
472         }
473
474         rc = posix_acl_valid(acl);
475         if (rc) {
476                 CERROR("validate acl: %d\n", rc);
477                 posix_acl_release(acl);
478                 RETURN(rc);
479         }
480
481         md->posix_acl = acl;
482         RETURN(0);
483 }
484 #else
485 #define mdc_unpack_acl(req, md) 0
486 #endif
487
488 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
489                       struct obd_export *dt_exp, struct obd_export *md_exp,
490                       struct lustre_md *md)
491 {
492         struct req_capsule *pill = &req->rq_pill;
493         int rc;
494         ENTRY;
495
496         LASSERT(md);
497         memset(md, 0, sizeof(*md));
498
499         md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
500         LASSERT(md->body != NULL);
501
502         if (md->body->mbo_valid & OBD_MD_FLEASIZE) {
503                 int lmmsize;
504                 struct lov_mds_md *lmm;
505
506                 if (!S_ISREG(md->body->mbo_mode)) {
507                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
508                                "regular file, but is not\n");
509                         GOTO(out, rc = -EPROTO);
510                 }
511
512                 if (md->body->mbo_eadatasize == 0) {
513                         CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
514                                "but eadatasize 0\n");
515                         GOTO(out, rc = -EPROTO);
516                 }
517
518                 lmmsize = md->body->mbo_eadatasize;
519                 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
520                 if (!lmm)
521                         GOTO(out, rc = -EPROTO);
522
523                 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
524                 if (rc < 0)
525                         GOTO(out, rc);
526
527                 if (rc < (typeof(rc))sizeof(*md->lsm)) {
528                         CDEBUG(D_INFO, "lsm size too small: "
529                                "rc < sizeof (*md->lsm) (%d < %d)\n",
530                                rc, (int)sizeof(*md->lsm));
531                         GOTO(out, rc = -EPROTO);
532                 }
533
534         } else if (md->body->mbo_valid & OBD_MD_FLDIREA) {
535                 int lmvsize;
536                 struct lov_mds_md *lmv;
537
538                 if (!S_ISDIR(md->body->mbo_mode)) {
539                         CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
540                                "directory, but is not\n");
541                         GOTO(out, rc = -EPROTO);
542                 }
543
544                 if (md->body->mbo_eadatasize == 0) {
545                         CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
546                                "but eadatasize 0\n");
547                         RETURN(-EPROTO);
548                 }
549
550                 if (md->body->mbo_valid & OBD_MD_MEA) {
551                         lmvsize = md->body->mbo_eadatasize;
552                         lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
553                                                            lmvsize);
554                         if (!lmv)
555                                 GOTO(out, rc = -EPROTO);
556
557                         rc = obd_unpackmd(md_exp, (void *)&md->lmv, lmv,
558                                           lmvsize);
559                         if (rc < 0)
560                                 GOTO(out, rc);
561
562                         if (rc < (typeof(rc))sizeof(*md->lmv)) {
563                                 CDEBUG(D_INFO, "size too small:  "
564                                        "rc < sizeof(*md->lmv) (%d < %d)\n",
565                                         rc, (int)sizeof(*md->lmv));
566                                 GOTO(out, rc = -EPROTO);
567                         }
568                 }
569         }
570         rc = 0;
571
572         if (md->body->mbo_valid & OBD_MD_FLRMTPERM) {
573                 /* remote permission */
574                 LASSERT(client_is_remote(exp));
575                 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
576                                                 lustre_swab_mdt_remote_perm);
577                 if (!md->remote_perm)
578                         GOTO(out, rc = -EPROTO);
579         } else if (md->body->mbo_valid & OBD_MD_FLACL) {
580                 /* for ACL, it's possible that FLACL is set but aclsize is zero.
581                  * only when aclsize != 0 there's an actual segment for ACL
582                  * in reply buffer.
583                  */
584                 if (md->body->mbo_aclsize) {
585                         rc = mdc_unpack_acl(req, md);
586                         if (rc)
587                                 GOTO(out, rc);
588 #ifdef CONFIG_FS_POSIX_ACL
589                 } else {
590                         md->posix_acl = NULL;
591 #endif
592                 }
593         }
594         if (md->body->mbo_valid & OBD_MD_FLMDSCAPA) {
595                 struct obd_capa *oc = NULL;
596
597                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
598                 if (rc)
599                         GOTO(out, rc);
600                 md->mds_capa = oc;
601         }
602
603         if (md->body->mbo_valid & OBD_MD_FLOSSCAPA) {
604                 struct obd_capa *oc = NULL;
605
606                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
607                 if (rc)
608                         GOTO(out, rc);
609                 md->oss_capa = oc;
610         }
611
612         EXIT;
613 out:
614         if (rc) {
615                 if (md->oss_capa) {
616                         capa_put(md->oss_capa);
617                         md->oss_capa = NULL;
618                 }
619                 if (md->mds_capa) {
620                         capa_put(md->mds_capa);
621                         md->mds_capa = NULL;
622                 }
623 #ifdef CONFIG_FS_POSIX_ACL
624                 posix_acl_release(md->posix_acl);
625 #endif
626                 if (md->lsm)
627                         obd_free_memmd(dt_exp, &md->lsm);
628         }
629         return rc;
630 }
631
632 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
633 {
634         ENTRY;
635         RETURN(0);
636 }
637
638 void mdc_replay_open(struct ptlrpc_request *req)
639 {
640         struct md_open_data *mod = req->rq_cb_data;
641         struct ptlrpc_request *close_req;
642         struct obd_client_handle *och;
643         struct lustre_handle old;
644         struct mdt_body *body;
645         ENTRY;
646
647         if (mod == NULL) {
648                 DEBUG_REQ(D_ERROR, req,
649                           "Can't properly replay without open data.");
650                 EXIT;
651                 return;
652         }
653
654         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
655         LASSERT(body != NULL);
656
657         och = mod->mod_och;
658         if (och != NULL) {
659                 struct lustre_handle *file_fh;
660
661                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
662
663                 file_fh = &och->och_fh;
664                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
665                        file_fh->cookie, body->mbo_handle.cookie);
666                 old = *file_fh;
667                 *file_fh = body->mbo_handle;
668         }
669         close_req = mod->mod_close_req;
670         if (close_req != NULL) {
671                 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
672                 struct mdt_ioepoch *epoch;
673
674                 LASSERT(opc == MDS_CLOSE);
675                 epoch = req_capsule_client_get(&close_req->rq_pill,
676                                                &RMF_MDT_EPOCH);
677                 LASSERT(epoch);
678
679                 if (och != NULL)
680                         LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
681                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
682                 epoch->handle = body->mbo_handle;
683         }
684         EXIT;
685 }
686
687 void mdc_commit_open(struct ptlrpc_request *req)
688 {
689         struct md_open_data *mod = req->rq_cb_data;
690         if (mod == NULL)
691                 return;
692
693         /**
694          * No need to touch md_open_data::mod_och, it holds a reference on
695          * \var mod and will zero references to each other, \var mod will be
696          * freed after that when md_open_data::mod_och will put the reference.
697          */
698
699         /**
700          * Do not let open request to disappear as it still may be needed
701          * for close rpc to happen (it may happen on evict only, otherwise
702          * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
703          * called), just mark this rpc as committed to distinguish these 2
704          * cases, see mdc_close() for details. The open request reference will
705          * be put along with freeing \var mod.
706          */
707         ptlrpc_request_addref(req);
708         spin_lock(&req->rq_lock);
709         req->rq_committed = 1;
710         spin_unlock(&req->rq_lock);
711         req->rq_cb_data = NULL;
712         obd_mod_put(mod);
713 }
714
715 int mdc_set_open_replay_data(struct obd_export *exp,
716                              struct obd_client_handle *och,
717                              struct lookup_intent *it)
718 {
719         struct md_open_data     *mod;
720         struct mdt_rec_create   *rec;
721         struct mdt_body         *body;
722         struct ptlrpc_request   *open_req = it->d.lustre.it_data;
723         struct obd_import       *imp = open_req->rq_import;
724         ENTRY;
725
726         if (!open_req->rq_replay)
727                 RETURN(0);
728
729         rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
730         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
731         LASSERT(rec != NULL);
732         /* Incoming message in my byte order (it's been swabbed). */
733         /* Outgoing messages always in my byte order. */
734         LASSERT(body != NULL);
735
736         /* Only if the import is replayable, we set replay_open data */
737         if (och && imp->imp_replayable) {
738                 mod = obd_mod_alloc();
739                 if (mod == NULL) {
740                         DEBUG_REQ(D_ERROR, open_req,
741                                   "Can't allocate md_open_data");
742                         RETURN(0);
743                 }
744
745                 /**
746                  * Take a reference on \var mod, to be freed on mdc_close().
747                  * It protects \var mod from being freed on eviction (commit
748                  * callback is called despite rq_replay flag).
749                  * Another reference for \var och.
750                  */
751                 obd_mod_get(mod);
752                 obd_mod_get(mod);
753
754                 spin_lock(&open_req->rq_lock);
755                 och->och_mod = mod;
756                 mod->mod_och = och;
757                 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
758                                      it_disposition(it, DISP_OPEN_STRIPE);
759                 mod->mod_open_req = open_req;
760                 open_req->rq_cb_data = mod;
761                 open_req->rq_commit_cb = mdc_commit_open;
762                 spin_unlock(&open_req->rq_lock);
763         }
764
765         rec->cr_fid2 = body->mbo_fid1;
766         rec->cr_ioepoch = body->mbo_ioepoch;
767         rec->cr_old_handle.cookie = body->mbo_handle.cookie;
768         open_req->rq_replay_cb = mdc_replay_open;
769         if (!fid_is_sane(&body->mbo_fid1)) {
770                 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
771                           "insane fid");
772                 LBUG();
773         }
774
775         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
776         RETURN(0);
777 }
778
779 static void mdc_free_open(struct md_open_data *mod)
780 {
781         int committed = 0;
782
783         if (mod->mod_is_create == 0 &&
784             imp_connect_disp_stripe(mod->mod_open_req->rq_import))
785                 committed = 1;
786
787         LASSERT(mod->mod_open_req->rq_replay == 0);
788
789         DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
790
791         ptlrpc_request_committed(mod->mod_open_req, committed);
792         if (mod->mod_close_req)
793                 ptlrpc_request_committed(mod->mod_close_req, committed);
794 }
795
796 int mdc_clear_open_replay_data(struct obd_export *exp,
797                                struct obd_client_handle *och)
798 {
799         struct md_open_data *mod = och->och_mod;
800         ENTRY;
801
802         /**
803          * It is possible to not have \var mod in a case of eviction between
804          * lookup and ll_file_open().
805          **/
806         if (mod == NULL)
807                 RETURN(0);
808
809         LASSERT(mod != LP_POISON);
810         LASSERT(mod->mod_open_req != NULL);
811         mdc_free_open(mod);
812
813         mod->mod_och = NULL;
814         och->och_mod = NULL;
815         obd_mod_put(mod);
816
817         RETURN(0);
818 }
819
820 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
821                      struct md_open_data *mod, struct ptlrpc_request **request)
822 {
823         struct obd_device     *obd = class_exp2obd(exp);
824         struct ptlrpc_request *req;
825         struct req_format     *req_fmt;
826         int                    rc;
827         int                    saved_rc = 0;
828         ENTRY;
829
830         req_fmt = &RQF_MDS_CLOSE;
831         if (op_data->op_bias & MDS_HSM_RELEASE) {
832                 req_fmt = &RQF_MDS_RELEASE_CLOSE;
833
834                 /* allocate a FID for volatile file */
835                 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
836                 if (rc < 0) {
837                         CERROR("%s: "DFID" failed to allocate FID: %d\n",
838                                obd->obd_name, PFID(&op_data->op_fid1), rc);
839                         /* save the errcode and proceed to close */
840                         saved_rc = rc;
841                 }
842         }
843
844         *request = NULL;
845         req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
846         if (req == NULL)
847                 RETURN(-ENOMEM);
848
849         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
850
851         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
852         if (rc) {
853                 ptlrpc_request_free(req);
854                 RETURN(rc);
855         }
856
857         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
858          * portal whose threads are not taking any DLM locks and are therefore
859          * always progressing */
860         req->rq_request_portal = MDS_READPAGE_PORTAL;
861         ptlrpc_at_set_req_timeout(req);
862
863         /* Ensure that this close's handle is fixed up during replay. */
864         if (likely(mod != NULL)) {
865                 LASSERTF(mod->mod_open_req != NULL &&
866                          mod->mod_open_req->rq_type != LI_POISON,
867                          "POISONED open %p!\n", mod->mod_open_req);
868
869                 mod->mod_close_req = req;
870
871                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
872                 /* We no longer want to preserve this open for replay even
873                  * though the open was committed. b=3632, b=3633 */
874                 spin_lock(&mod->mod_open_req->rq_lock);
875                 mod->mod_open_req->rq_replay = 0;
876                 spin_unlock(&mod->mod_open_req->rq_lock);
877         } else {
878                  CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
879         }
880
881         mdc_close_pack(req, op_data);
882
883         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
884                              obd->u.cli.cl_default_mds_easize);
885         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
886                              obd->u.cli.cl_default_mds_cookiesize);
887
888         ptlrpc_request_set_replen(req);
889
890         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
891         rc = ptlrpc_queue_wait(req);
892         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
893
894         if (req->rq_repmsg == NULL) {
895                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
896                        req->rq_status);
897                 if (rc == 0)
898                         rc = req->rq_status ?: -EIO;
899         } else if (rc == 0 || rc == -EAGAIN) {
900                 struct mdt_body *body;
901
902                 rc = lustre_msg_get_status(req->rq_repmsg);
903                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
904                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
905                                   "= %d", rc);
906                         if (rc > 0)
907                                 rc = -rc;
908                 }
909                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
910                 if (body == NULL)
911                         rc = -EPROTO;
912         } else if (rc == -ESTALE) {
913                 /**
914                  * it can be allowed error after 3633 if open was committed and
915                  * server failed before close was sent. Let's check if mod
916                  * exists and return no error in that case
917                  */
918                 if (mod) {
919                         DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
920                         LASSERT(mod->mod_open_req != NULL);
921                         if (mod->mod_open_req->rq_committed)
922                                 rc = 0;
923                 }
924         }
925
926         if (mod) {
927                 if (rc != 0)
928                         mod->mod_close_req = NULL;
929                 /* Since now, mod is accessed through open_req only,
930                  * thus close req does not keep a reference on mod anymore. */
931                 obd_mod_put(mod);
932         }
933         *request = req;
934
935         RETURN(rc < 0 ? rc : saved_rc);
936 }
937
938 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
939                        __u64 offset, struct obd_capa *oc,
940                        struct page **pages, int npages,
941                        struct ptlrpc_request **request)
942 {
943         struct ptlrpc_request   *req;
944         struct ptlrpc_bulk_desc *desc;
945         int                      i;
946         wait_queue_head_t        waitq;
947         int                      resends = 0;
948         struct l_wait_info       lwi;
949         int                      rc;
950         ENTRY;
951
952         *request = NULL;
953         init_waitqueue_head(&waitq);
954
955 restart_bulk:
956         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
957         if (req == NULL)
958                 RETURN(-ENOMEM);
959
960         mdc_set_capa_size(req, &RMF_CAPA1, oc);
961
962         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
963         if (rc) {
964                 ptlrpc_request_free(req);
965                 RETURN(rc);
966         }
967
968         req->rq_request_portal = MDS_READPAGE_PORTAL;
969         ptlrpc_at_set_req_timeout(req);
970
971         desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
972                                     MDS_BULK_PORTAL);
973         if (desc == NULL) {
974                 ptlrpc_request_free(req);
975                 RETURN(-ENOMEM);
976         }
977
978         /* NB req now owns desc and will free it when it gets freed */
979         for (i = 0; i < npages; i++)
980                 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
981
982         mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE * npages, fid, oc);
983
984         ptlrpc_request_set_replen(req);
985         rc = ptlrpc_queue_wait(req);
986         if (rc) {
987                 ptlrpc_req_finished(req);
988                 if (rc != -ETIMEDOUT)
989                         RETURN(rc);
990
991                 resends++;
992                 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
993                         CERROR("%s: too many resend retries: rc = %d\n",
994                                exp->exp_obd->obd_name, -EIO);
995                         RETURN(-EIO);
996                 }
997                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL,
998                                        NULL);
999                 l_wait_event(waitq, 0, &lwi);
1000
1001                 goto restart_bulk;
1002         }
1003
1004         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1005                                           req->rq_bulk->bd_nob_transferred);
1006         if (rc < 0) {
1007                 ptlrpc_req_finished(req);
1008                 RETURN(rc);
1009         }
1010
1011         if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1012                 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1013                        exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1014                        PAGE_CACHE_SIZE * npages);
1015                 ptlrpc_req_finished(req);
1016                 RETURN(-EPROTO);
1017         }
1018
1019         *request = req;
1020         RETURN(0);
1021 }
1022
1023 static void mdc_release_page(struct page *page, int remove)
1024 {
1025         if (remove) {
1026                 lock_page(page);
1027                 if (likely(page->mapping != NULL))
1028                         truncate_complete_page(page->mapping, page);
1029                 unlock_page(page);
1030         }
1031         page_cache_release(page);
1032 }
1033
1034 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1035                                     __u64 *start, __u64 *end, int hash64)
1036 {
1037         /*
1038          * Complement of hash is used as an index so that
1039          * radix_tree_gang_lookup() can be used to find a page with starting
1040          * hash _smaller_ than one we are looking for.
1041          */
1042         unsigned long offset = hash_x_index(*hash, hash64);
1043         struct page *page;
1044         int found;
1045
1046         spin_lock_irq(&mapping->tree_lock);
1047         found = radix_tree_gang_lookup(&mapping->page_tree,
1048                                        (void **)&page, offset, 1);
1049         if (found > 0 && !radix_tree_exceptional_entry(page)) {
1050                 struct lu_dirpage *dp;
1051
1052                 page_cache_get(page);
1053                 spin_unlock_irq(&mapping->tree_lock);
1054                 /*
1055                  * In contrast to find_lock_page() we are sure that directory
1056                  * page cannot be truncated (while DLM lock is held) and,
1057                  * hence, can avoid restart.
1058                  *
1059                  * In fact, page cannot be locked here at all, because
1060                  * mdc_read_page_remote does synchronous io.
1061                  */
1062                 wait_on_page_locked(page);
1063                 if (PageUptodate(page)) {
1064                         dp = kmap(page);
1065                         if (BITS_PER_LONG == 32 && hash64) {
1066                                 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1067                                 *end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
1068                                 *hash  = *hash >> 32;
1069                         } else {
1070                                 *start = le64_to_cpu(dp->ldp_hash_start);
1071                                 *end   = le64_to_cpu(dp->ldp_hash_end);
1072                         }
1073                         if (unlikely(*start == 1 && *hash == 0))
1074                                 *hash = *start;
1075                         else
1076                                 LASSERTF(*start <= *hash, "start = "LPX64
1077                                          ",end = "LPX64",hash = "LPX64"\n",
1078                                          *start, *end, *hash);
1079                         CDEBUG(D_VFSTRACE, "offset %lx ["LPX64" "LPX64"],"
1080                               " hash "LPX64"\n", offset, *start, *end, *hash);
1081                         if (*hash > *end) {
1082                                 kunmap(page);
1083                                 mdc_release_page(page, 0);
1084                                 page = NULL;
1085                         } else if (*end != *start && *hash == *end) {
1086                                 /*
1087                                  * upon hash collision, remove this page,
1088                                  * otherwise put page reference, and
1089                                  * mdc_read_page_remote() will issue RPC to
1090                                  * fetch the page we want.
1091                                  */
1092                                 kunmap(page);
1093                                 mdc_release_page(page,
1094                                     le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1095                                 page = NULL;
1096                         }
1097                 } else {
1098                         page_cache_release(page);
1099                         page = ERR_PTR(-EIO);
1100                 }
1101         } else {
1102                 spin_unlock_irq(&mapping->tree_lock);
1103                 page = NULL;
1104         }
1105         return page;
1106 }
1107
1108 /*
1109  * Adjust a set of pages, each page containing an array of lu_dirpages,
1110  * so that each page can be used as a single logical lu_dirpage.
1111  *
1112  * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1113  * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1114  * struct lu_dirent.  It has size up to LU_PAGE_SIZE. The ldp_hash_end
1115  * value is used as a cookie to request the next lu_dirpage in a
1116  * directory listing that spans multiple pages (two in this example):
1117  *   ________
1118  *  |        |
1119  * .|--------v-------   -----.
1120  * |s|e|f|p|ent|ent| ... |ent|
1121  * '--|--------------   -----'   Each PAGE contains a single
1122  *    '------.                   lu_dirpage.
1123  * .---------v-------   -----.
1124  * |s|e|f|p|ent| 0 | ... | 0 |
1125  * '-----------------   -----'
1126  *
1127  * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
1128  * larger than LU_PAGE_SIZE, a single host page may contain multiple
1129  * lu_dirpages. After reading the lu_dirpages from the MDS, the
1130  * ldp_hash_end of the first lu_dirpage refers to the one immediately
1131  * after it in the same PAGE (arrows simplified for brevity, but
1132  * in general e0==s1, e1==s2, etc.):
1133  *
1134  * .--------------------   -----.
1135  * |s0|e0|f0|p|ent|ent| ... |ent|
1136  * |---v----------------   -----|
1137  * |s1|e1|f1|p|ent|ent| ... |ent|
1138  * |---v----------------   -----|  Here, each PAGE contains
1139  *             ...                 multiple lu_dirpages.
1140  * |---v----------------   -----|
1141  * |s'|e'|f'|p|ent|ent| ... |ent|
1142  * '---|----------------   -----'
1143  *     v
1144  * .----------------------------.
1145  * |        next PAGE           |
1146  *
1147  * This structure is transformed into a single logical lu_dirpage as follows:
1148  *
1149  * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1150  *   labeled 'next PAGE'.
1151  *
1152  * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1153  *   a hash collision with the next page exists.
1154  *
1155  * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1156  *   to the first entry of the next lu_dirpage.
1157  */
1158 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
1159 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1160 {
1161         int i;
1162
1163         for (i = 0; i < cfs_pgs; i++) {
1164                 struct lu_dirpage       *dp = kmap(pages[i]);
1165                 struct lu_dirpage       *first = dp;
1166                 struct lu_dirent        *end_dirent = NULL;
1167                 struct lu_dirent        *ent;
1168                 __u64           hash_end = le64_to_cpu(dp->ldp_hash_end);
1169                 __u32           flags = le32_to_cpu(dp->ldp_flags);
1170
1171                 while (--lu_pgs > 0) {
1172                         ent = lu_dirent_start(dp);
1173                         for (end_dirent = ent; ent != NULL;
1174                              end_dirent = ent, ent = lu_dirent_next(ent));
1175
1176                         /* Advance dp to next lu_dirpage. */
1177                         dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1178
1179                         /* Check if we've reached the end of the PAGE. */
1180                         if (!((unsigned long)dp & ~PAGE_MASK))
1181                                 break;
1182
1183                         /* Save the hash and flags of this lu_dirpage. */
1184                         hash_end = le64_to_cpu(dp->ldp_hash_end);
1185                         flags = le32_to_cpu(dp->ldp_flags);
1186
1187                         /* Check if lu_dirpage contains no entries. */
1188                         if (end_dirent == NULL)
1189                                 break;
1190
1191                         /* Enlarge the end entry lde_reclen from 0 to
1192                          * first entry of next lu_dirpage. */
1193                         LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1194                         end_dirent->lde_reclen =
1195                                 cpu_to_le16((char *)(dp->ldp_entries) -
1196                                             (char *)end_dirent);
1197                 }
1198
1199                 first->ldp_hash_end = hash_end;
1200                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1201                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1202
1203                 kunmap(pages[i]);
1204         }
1205         LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1206 }
1207 #else
1208 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1209 #endif  /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
1210
1211 /* parameters for readdir page */
1212 struct readpage_param {
1213         struct md_op_data       *rp_mod;
1214         __u64                   rp_off;
1215         int                     rp_hash64;
1216         struct obd_export       *rp_exp;
1217         struct md_callback      *rp_cb;
1218 };
1219
1220 #ifndef HAVE_DELETE_FROM_PAGE_CACHE
1221 static inline void delete_from_page_cache(struct page *page)
1222 {
1223         remove_from_page_cache(page);
1224         page_cache_release(page);
1225 }
1226 #endif
1227
1228 /**
1229  * Read pages from server.
1230  *
1231  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1232  * a header lu_dirpage which describes the start/end hash, and whether this
1233  * page is empty (contains no dir entry) or hash collide with next page.
1234  * After client receives reply, several pages will be integrated into dir page
1235  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1236  * lu_dirpage for this integrated page will be adjusted.
1237  **/
1238 static int mdc_read_page_remote(void *data, struct page *page0)
1239 {
1240         struct readpage_param   *rp = data;
1241         struct page             **page_pool;
1242         struct page             *page;
1243         struct lu_dirpage       *dp;
1244         int                     rd_pgs = 0; /* number of pages read actually */
1245         int                     npages;
1246         struct md_op_data       *op_data = rp->rp_mod;
1247         struct ptlrpc_request   *req;
1248         int                     max_pages = op_data->op_max_pages;
1249         struct inode            *inode;
1250         struct lu_fid           *fid;
1251         int                     i;
1252         int                     rc;
1253         ENTRY;
1254
1255         LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
1256         inode = op_data->op_data;
1257         fid = &op_data->op_fid1;
1258         LASSERT(inode != NULL);
1259
1260         OBD_ALLOC(page_pool, sizeof(page_pool[0]) * max_pages);
1261         if (page_pool != NULL) {
1262                 page_pool[0] = page0;
1263         } else {
1264                 page_pool = &page0;
1265                 max_pages = 1;
1266         }
1267
1268         for (npages = 1; npages < max_pages; npages++) {
1269                 page = page_cache_alloc_cold(inode->i_mapping);
1270                 if (page == NULL)
1271                         break;
1272                 page_pool[npages] = page;
1273         }
1274
1275         rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, op_data->op_capa1,
1276                          page_pool, npages, &req);
1277         if (rc < 0) {
1278                 /* page0 is special, which was added into page cache early */
1279                 delete_from_page_cache(page0);
1280         } else {
1281                 int lu_pgs;
1282
1283                 rd_pgs = (req->rq_bulk->bd_nob_transferred +
1284                             PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1285                 lu_pgs = req->rq_bulk->bd_nob_transferred >>
1286                                                         LU_PAGE_SHIFT;
1287                 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1288
1289                 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1290
1291                 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1292
1293                 SetPageUptodate(page0);
1294         }
1295         unlock_page(page0);
1296
1297         ptlrpc_req_finished(req);
1298         CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1299         for (i = 1; i < npages; i++) {
1300                 unsigned long   offset;
1301                 __u64           hash;
1302                 int ret;
1303
1304                 page = page_pool[i];
1305
1306                 if (rc < 0 || i >= rd_pgs) {
1307                         page_cache_release(page);
1308                         continue;
1309                 }
1310
1311                 SetPageUptodate(page);
1312
1313                 dp = kmap(page);
1314                 hash = le64_to_cpu(dp->ldp_hash_start);
1315                 kunmap(page);
1316
1317                 offset = hash_x_index(hash, rp->rp_hash64);
1318
1319                 prefetchw(&page->flags);
1320                 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1321                                             GFP_KERNEL);
1322                 if (ret == 0)
1323                         unlock_page(page);
1324                 else
1325                         CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1326                                " rc = %d\n", offset, ret);
1327                 page_cache_release(page);
1328         }
1329
1330         if (page_pool != &page0)
1331                 OBD_FREE(page_pool, sizeof(page_pool[0]) * max_pages);
1332
1333         RETURN(rc);
1334 }
1335
1336 /**
1337  * Read dir page from cache first, if it can not find it, read it from
1338  * server and add into the cache.
1339  *
1340  * \param[in] exp       MDC export
1341  * \param[in] op_data   client MD stack parameters, transfering parameters
1342  *                      between different layers on client MD stack.
1343  * \param[in] cb_op     callback required for ldlm lock enqueue during
1344  *                      read page
1345  * \param[in] hash_offset the hash offset of the page to be read
1346  * \param[in] ppage     the page to be read
1347  *
1348  * retval               = 0 get the page successfully
1349  *                      errno(<0) get the page failed
1350  */
1351 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1352                          struct md_callback *cb_op, __u64 hash_offset,
1353                          struct page **ppage)
1354 {
1355         struct lookup_intent    it = { .it_op = IT_READDIR };
1356         struct page             *page;
1357         struct inode            *dir = op_data->op_data;
1358         struct address_space    *mapping;
1359         struct lu_dirpage       *dp;
1360         __u64                   start = 0;
1361         __u64                   end = 0;
1362         struct lustre_handle    lockh;
1363         struct ptlrpc_request   *enq_req = NULL;
1364         struct readpage_param   rp_param;
1365         int rc;
1366
1367         ENTRY;
1368
1369         *ppage = NULL;
1370
1371         LASSERT(dir != NULL);
1372         mapping = dir->i_mapping;
1373
1374         rc = mdc_intent_lock(exp, op_data, &it, &enq_req,
1375                              cb_op->md_blocking_ast, 0);
1376         if (enq_req != NULL)
1377                 ptlrpc_req_finished(enq_req);
1378
1379         if (rc < 0) {
1380                 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1381                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1382                 RETURN(rc);
1383         }
1384
1385         rc = 0;
1386         mdc_set_lock_data(exp, &it.d.lustre.it_lock_handle, dir, NULL);
1387
1388         rp_param.rp_off = hash_offset;
1389         rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1390         page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1391                                rp_param.rp_hash64);
1392         if (IS_ERR(page)) {
1393                 CERROR("%s: dir page locate: "DFID" at "LPU64": rc %ld\n",
1394                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1395                        rp_param.rp_off, PTR_ERR(page));
1396                 GOTO(out_unlock, rc = PTR_ERR(page));
1397         } else if (page != NULL) {
1398                 /*
1399                  * XXX nikita: not entirely correct handling of a corner case:
1400                  * suppose hash chain of entries with hash value HASH crosses
1401                  * border between pages P0 and P1. First both P0 and P1 are
1402                  * cached, seekdir() is called for some entry from the P0 part
1403                  * of the chain. Later P0 goes out of cache. telldir(HASH)
1404                  * happens and finds P1, as it starts with matching hash
1405                  * value. Remaining entries from P0 part of the chain are
1406                  * skipped. (Is that really a bug?)
1407                  *
1408                  * Possible solutions: 0. don't cache P1 is such case, handle
1409                  * it as an "overflow" page. 1. invalidate all pages at
1410                  * once. 2. use HASH|1 as an index for P1.
1411                  */
1412                 GOTO(hash_collision, page);
1413         }
1414
1415         rp_param.rp_exp = exp;
1416         rp_param.rp_mod = op_data;
1417         page = read_cache_page(mapping,
1418                                hash_x_index(rp_param.rp_off,
1419                                             rp_param.rp_hash64),
1420                                mdc_read_page_remote, &rp_param);
1421         if (IS_ERR(page)) {
1422                 CDEBUG(D_INFO, "%s: read cache page: "DFID" at "LPU64": %ld\n",
1423                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1424                        rp_param.rp_off, PTR_ERR(page));
1425                 GOTO(out_unlock, rc = PTR_ERR(page));
1426         }
1427
1428         wait_on_page_locked(page);
1429         (void)kmap(page);
1430         if (!PageUptodate(page)) {
1431                 CERROR("%s: page not updated: "DFID" at "LPU64": rc %d\n",
1432                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1433                        rp_param.rp_off, -5);
1434                 goto fail;
1435         }
1436         if (!PageChecked(page))
1437                 SetPageChecked(page);
1438         if (PageError(page)) {
1439                 CERROR("%s: page error: "DFID" at "LPU64": rc %d\n",
1440                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1441                        rp_param.rp_off, -5);
1442                 goto fail;
1443         }
1444
1445 hash_collision:
1446         dp = page_address(page);
1447         if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1448                 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1449                 end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
1450                 rp_param.rp_off = hash_offset >> 32;
1451         } else {
1452                 start = le64_to_cpu(dp->ldp_hash_start);
1453                 end   = le64_to_cpu(dp->ldp_hash_end);
1454                 rp_param.rp_off = hash_offset;
1455         }
1456         if (end == start) {
1457                 LASSERT(start == rp_param.rp_off);
1458                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1459 #if BITS_PER_LONG == 32
1460                 CWARN("Real page-wide hash collision at ["LPU64" "LPU64"] with "
1461                       "hash "LPU64"\n", le64_to_cpu(dp->ldp_hash_start),
1462                       le64_to_cpu(dp->ldp_hash_end), hash_offset);
1463 #endif
1464
1465                 /*
1466                  * Fetch whole overflow chain...
1467                  *
1468                  * XXX not yet.
1469                  */
1470                 goto fail;
1471         }
1472         *ppage = page;
1473 out_unlock:
1474         lockh.cookie = it.d.lustre.it_lock_handle;
1475         ldlm_lock_decref(&lockh, it.d.lustre.it_lock_mode);
1476         it.d.lustre.it_lock_handle = 0;
1477         return rc;
1478 fail:
1479         kunmap(page);
1480         mdc_release_page(page, 1);
1481         rc = -EIO;
1482         goto out_unlock;
1483 }
1484
1485
1486 static int mdc_statfs(const struct lu_env *env,
1487                       struct obd_export *exp, struct obd_statfs *osfs,
1488                       __u64 max_age, __u32 flags)
1489 {
1490         struct obd_device     *obd = class_exp2obd(exp);
1491         struct ptlrpc_request *req;
1492         struct obd_statfs     *msfs;
1493         struct obd_import     *imp = NULL;
1494         int                    rc;
1495         ENTRY;
1496
1497         /*
1498          * Since the request might also come from lprocfs, so we need
1499          * sync this with client_disconnect_export Bug15684
1500          */
1501         down_read(&obd->u.cli.cl_sem);
1502         if (obd->u.cli.cl_import)
1503                 imp = class_import_get(obd->u.cli.cl_import);
1504         up_read(&obd->u.cli.cl_sem);
1505         if (!imp)
1506                 RETURN(-ENODEV);
1507
1508         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1509                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1510         if (req == NULL)
1511                 GOTO(output, rc = -ENOMEM);
1512
1513         ptlrpc_request_set_replen(req);
1514
1515         if (flags & OBD_STATFS_NODELAY) {
1516                 /* procfs requests not want stay in wait for avoid deadlock */
1517                 req->rq_no_resend = 1;
1518                 req->rq_no_delay = 1;
1519         }
1520
1521         rc = ptlrpc_queue_wait(req);
1522         if (rc) {
1523                 /* check connection error first */
1524                 if (imp->imp_connect_error)
1525                         rc = imp->imp_connect_error;
1526                 GOTO(out, rc);
1527         }
1528
1529         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1530         if (msfs == NULL)
1531                 GOTO(out, rc = -EPROTO);
1532
1533         *osfs = *msfs;
1534         EXIT;
1535 out:
1536         ptlrpc_req_finished(req);
1537 output:
1538         class_import_put(imp);
1539         return rc;
1540 }
1541
1542 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1543 {
1544         __u32 keylen, vallen;
1545         void *key;
1546         int rc;
1547
1548         if (gf->gf_pathlen > PATH_MAX)
1549                 RETURN(-ENAMETOOLONG);
1550         if (gf->gf_pathlen < 2)
1551                 RETURN(-EOVERFLOW);
1552
1553         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1554         keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1555         OBD_ALLOC(key, keylen);
1556         if (key == NULL)
1557                 RETURN(-ENOMEM);
1558         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1559         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1560
1561         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1562                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1563
1564         if (!fid_is_sane(&gf->gf_fid))
1565                 GOTO(out, rc = -EINVAL);
1566
1567         /* Val is struct getinfo_fid2path result plus path */
1568         vallen = sizeof(*gf) + gf->gf_pathlen;
1569
1570         rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1571         if (rc != 0 && rc != -EREMOTE)
1572                 GOTO(out, rc);
1573
1574         if (vallen <= sizeof(*gf))
1575                 GOTO(out, rc = -EPROTO);
1576         else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1577                 GOTO(out, rc = -EOVERFLOW);
1578
1579         CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1580                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1581
1582 out:
1583         OBD_FREE(key, keylen);
1584         return rc;
1585 }
1586
1587 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1588                                 struct hsm_progress_kernel *hpk)
1589 {
1590         struct obd_import               *imp = class_exp2cliimp(exp);
1591         struct hsm_progress_kernel      *req_hpk;
1592         struct ptlrpc_request           *req;
1593         int                              rc;
1594         ENTRY;
1595
1596         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1597                                         LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1598         if (req == NULL)
1599                 GOTO(out, rc = -ENOMEM);
1600
1601         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1602
1603         /* Copy hsm_progress struct */
1604         req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1605         if (req_hpk == NULL)
1606                 GOTO(out, rc = -EPROTO);
1607
1608         *req_hpk = *hpk;
1609         req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1610
1611         ptlrpc_request_set_replen(req);
1612
1613         rc = mdc_queue_wait(req);
1614         GOTO(out, rc);
1615 out:
1616         ptlrpc_req_finished(req);
1617         return rc;
1618 }
1619
1620 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1621 {
1622         __u32                   *archive_mask;
1623         struct ptlrpc_request   *req;
1624         int                      rc;
1625         ENTRY;
1626
1627         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1628                                         LUSTRE_MDS_VERSION,
1629                                         MDS_HSM_CT_REGISTER);
1630         if (req == NULL)
1631                 GOTO(out, rc = -ENOMEM);
1632
1633         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1634
1635         /* Copy hsm_progress struct */
1636         archive_mask = req_capsule_client_get(&req->rq_pill,
1637                                               &RMF_MDS_HSM_ARCHIVE);
1638         if (archive_mask == NULL)
1639                 GOTO(out, rc = -EPROTO);
1640
1641         *archive_mask = archives;
1642
1643         ptlrpc_request_set_replen(req);
1644
1645         rc = mdc_queue_wait(req);
1646         GOTO(out, rc);
1647 out:
1648         ptlrpc_req_finished(req);
1649         return rc;
1650 }
1651
1652 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1653                                       struct md_op_data *op_data)
1654 {
1655         struct hsm_current_action       *hca = op_data->op_data;
1656         struct hsm_current_action       *req_hca;
1657         struct ptlrpc_request           *req;
1658         int                              rc;
1659         ENTRY;
1660
1661         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1662                                    &RQF_MDS_HSM_ACTION);
1663         if (req == NULL)
1664                 RETURN(-ENOMEM);
1665
1666         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1667
1668         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1669         if (rc) {
1670                 ptlrpc_request_free(req);
1671                 RETURN(rc);
1672         }
1673
1674         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1675                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1676
1677         ptlrpc_request_set_replen(req);
1678
1679         rc = mdc_queue_wait(req);
1680         if (rc)
1681                 GOTO(out, rc);
1682
1683         req_hca = req_capsule_server_get(&req->rq_pill,
1684                                          &RMF_MDS_HSM_CURRENT_ACTION);
1685         if (req_hca == NULL)
1686                 GOTO(out, rc = -EPROTO);
1687
1688         *hca = *req_hca;
1689
1690         EXIT;
1691 out:
1692         ptlrpc_req_finished(req);
1693         return rc;
1694 }
1695
1696 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1697 {
1698         struct ptlrpc_request   *req;
1699         int                      rc;
1700         ENTRY;
1701
1702         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1703                                         LUSTRE_MDS_VERSION,
1704                                         MDS_HSM_CT_UNREGISTER);
1705         if (req == NULL)
1706                 GOTO(out, rc = -ENOMEM);
1707
1708         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1709
1710         ptlrpc_request_set_replen(req);
1711
1712         rc = mdc_queue_wait(req);
1713         GOTO(out, rc);
1714 out:
1715         ptlrpc_req_finished(req);
1716         return rc;
1717 }
1718
1719 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1720                                  struct md_op_data *op_data)
1721 {
1722         struct hsm_user_state   *hus = op_data->op_data;
1723         struct hsm_user_state   *req_hus;
1724         struct ptlrpc_request   *req;
1725         int                      rc;
1726         ENTRY;
1727
1728         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1729                                    &RQF_MDS_HSM_STATE_GET);
1730         if (req == NULL)
1731                 RETURN(-ENOMEM);
1732
1733         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1734
1735         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1736         if (rc != 0) {
1737                 ptlrpc_request_free(req);
1738                 RETURN(rc);
1739         }
1740
1741         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1742                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1743
1744         ptlrpc_request_set_replen(req);
1745
1746         rc = mdc_queue_wait(req);
1747         if (rc)
1748                 GOTO(out, rc);
1749
1750         req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1751         if (req_hus == NULL)
1752                 GOTO(out, rc = -EPROTO);
1753
1754         *hus = *req_hus;
1755
1756         EXIT;
1757 out:
1758         ptlrpc_req_finished(req);
1759         return rc;
1760 }
1761
1762 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1763                                  struct md_op_data *op_data)
1764 {
1765         struct hsm_state_set    *hss = op_data->op_data;
1766         struct hsm_state_set    *req_hss;
1767         struct ptlrpc_request   *req;
1768         int                      rc;
1769         ENTRY;
1770
1771         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1772                                    &RQF_MDS_HSM_STATE_SET);
1773         if (req == NULL)
1774                 RETURN(-ENOMEM);
1775
1776         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1777
1778         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1779         if (rc) {
1780                 ptlrpc_request_free(req);
1781                 RETURN(rc);
1782         }
1783
1784         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1785                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1786
1787         /* Copy states */
1788         req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1789         if (req_hss == NULL)
1790                 GOTO(out, rc = -EPROTO);
1791         *req_hss = *hss;
1792
1793         ptlrpc_request_set_replen(req);
1794
1795         rc = mdc_queue_wait(req);
1796         GOTO(out, rc);
1797
1798         EXIT;
1799 out:
1800         ptlrpc_req_finished(req);
1801         return rc;
1802 }
1803
1804 static int mdc_ioc_hsm_request(struct obd_export *exp,
1805                                struct hsm_user_request *hur)
1806 {
1807         struct obd_import       *imp = class_exp2cliimp(exp);
1808         struct ptlrpc_request   *req;
1809         struct hsm_request      *req_hr;
1810         struct hsm_user_item    *req_hui;
1811         char                    *req_opaque;
1812         int                      rc;
1813         ENTRY;
1814
1815         req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1816         if (req == NULL)
1817                 GOTO(out, rc = -ENOMEM);
1818
1819         req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1820                              hur->hur_request.hr_itemcount
1821                              * sizeof(struct hsm_user_item));
1822         req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1823                              hur->hur_request.hr_data_len);
1824
1825         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1826         if (rc) {
1827                 ptlrpc_request_free(req);
1828                 RETURN(rc);
1829         }
1830
1831         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1832
1833         /* Copy hsm_request struct */
1834         req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1835         if (req_hr == NULL)
1836                 GOTO(out, rc = -EPROTO);
1837         *req_hr = hur->hur_request;
1838
1839         /* Copy hsm_user_item structs */
1840         req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1841         if (req_hui == NULL)
1842                 GOTO(out, rc = -EPROTO);
1843         memcpy(req_hui, hur->hur_user_item,
1844                hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1845
1846         /* Copy opaque field */
1847         req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1848         if (req_opaque == NULL)
1849                 GOTO(out, rc = -EPROTO);
1850         memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1851
1852         ptlrpc_request_set_replen(req);
1853
1854         rc = mdc_queue_wait(req);
1855         GOTO(out, rc);
1856
1857 out:
1858         ptlrpc_req_finished(req);
1859         return rc;
1860 }
1861
1862 static struct kuc_hdr *changelog_kuc_hdr(char *buf, size_t len, __u32 flags)
1863 {
1864         struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1865
1866         LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1867
1868         lh->kuc_magic = KUC_MAGIC;
1869         lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1870         lh->kuc_flags = flags;
1871         lh->kuc_msgtype = CL_RECORD;
1872         lh->kuc_msglen = len;
1873         return lh;
1874 }
1875
1876 struct changelog_show {
1877         __u64                            cs_startrec;
1878         enum changelog_send_flag         cs_flags;
1879         struct file                     *cs_fp;
1880         char                            *cs_buf;
1881         struct obd_device               *cs_obd;
1882 };
1883
1884 static inline char *cs_obd_name(struct changelog_show *cs)
1885 {
1886         return cs->cs_obd->obd_name;
1887 }
1888
1889 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1890                              struct llog_rec_hdr *hdr, void *data)
1891 {
1892         struct changelog_show           *cs = data;
1893         struct llog_changelog_rec       *rec = (struct llog_changelog_rec *)hdr;
1894         struct kuc_hdr                  *lh;
1895         size_t                           len;
1896         int                              rc;
1897         ENTRY;
1898
1899         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1900                 rc = -EINVAL;
1901                 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1902                        cs_obd_name(cs), rec->cr_hdr.lrh_type,
1903                        rec->cr.cr_type, rc);
1904                 RETURN(rc);
1905         }
1906
1907         if (rec->cr.cr_index < cs->cs_startrec) {
1908                 /* Skip entries earlier than what we are interested in */
1909                 CDEBUG(D_HSM, "rec="LPU64" start="LPU64"\n",
1910                        rec->cr.cr_index, cs->cs_startrec);
1911                 RETURN(0);
1912         }
1913
1914         CDEBUG(D_HSM, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID" %.*s\n",
1915                rec->cr.cr_index, rec->cr.cr_type,
1916                changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1917                rec->cr.cr_flags & CLF_FLAGMASK,
1918                PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1919                rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1920
1921         len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1922
1923         /* Set up the message */
1924         lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1925         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1926
1927         rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1928         CDEBUG(D_HSM, "kucmsg fp %p len %zu rc %d\n", cs->cs_fp, len, rc);
1929
1930         RETURN(rc);
1931 }
1932
1933 static int mdc_changelog_send_thread(void *csdata)
1934 {
1935         struct changelog_show   *cs = csdata;
1936         struct llog_ctxt        *ctxt = NULL;
1937         struct llog_handle      *llh = NULL;
1938         struct kuc_hdr          *kuch;
1939         enum llog_flag           flags = LLOG_F_IS_CAT;
1940         int                      rc;
1941
1942         CDEBUG(D_HSM, "changelog to fp=%p start "LPU64"\n",
1943                cs->cs_fp, cs->cs_startrec);
1944
1945         OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1946         if (cs->cs_buf == NULL)
1947                 GOTO(out, rc = -ENOMEM);
1948
1949         /* Set up the remote catalog handle */
1950         ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1951         if (ctxt == NULL)
1952                 GOTO(out, rc = -ENOENT);
1953         rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1954                        LLOG_OPEN_EXISTS);
1955         if (rc) {
1956                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1957                        cs_obd_name(cs), rc);
1958                 GOTO(out, rc);
1959         }
1960
1961         if (cs->cs_flags & CHANGELOG_FLAG_JOBID)
1962                 flags |= LLOG_F_EXT_JOBID;
1963
1964         rc = llog_init_handle(NULL, llh, flags, NULL);
1965         if (rc) {
1966                 CERROR("llog_init_handle failed %d\n", rc);
1967                 GOTO(out, rc);
1968         }
1969
1970         rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1971
1972         /* Send EOF no matter what our result */
1973         if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1974                                       cs->cs_flags))) {
1975                 kuch->kuc_msgtype = CL_EOF;
1976                 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1977         }
1978
1979 out:
1980         fput(cs->cs_fp);
1981         if (llh)
1982                 llog_cat_close(NULL, llh);
1983         if (ctxt)
1984                 llog_ctxt_put(ctxt);
1985         if (cs->cs_buf)
1986                 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1987         OBD_FREE_PTR(cs);
1988         return rc;
1989 }
1990
1991 static int mdc_ioc_changelog_send(struct obd_device *obd,
1992                                   struct ioc_changelog *icc)
1993 {
1994         struct changelog_show *cs;
1995         struct task_struct *task;
1996         int rc;
1997
1998         /* Freed in mdc_changelog_send_thread */
1999         OBD_ALLOC_PTR(cs);
2000         if (!cs)
2001                 return -ENOMEM;
2002
2003         cs->cs_obd = obd;
2004         cs->cs_startrec = icc->icc_recno;
2005         /* matching fput in mdc_changelog_send_thread */
2006         cs->cs_fp = fget(icc->icc_id);
2007         cs->cs_flags = icc->icc_flags;
2008
2009         /*
2010          * New thread because we should return to user app before
2011          * writing into our pipe
2012          */
2013         task = kthread_run(mdc_changelog_send_thread, cs,
2014                            "mdc_clg_send_thread");
2015         if (IS_ERR(task)) {
2016                 rc = PTR_ERR(task);
2017                 CERROR("%s: cannot start changelog thread: rc = %d\n",
2018                        cs_obd_name(cs), rc);
2019                 OBD_FREE_PTR(cs);
2020         } else {
2021                 rc = 0;
2022                 CDEBUG(D_HSM, "%s: started changelog thread\n",
2023                        cs_obd_name(cs));
2024         }
2025
2026         return rc;
2027 }
2028
2029 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2030                                 struct lustre_kernelcomm *lk);
2031
2032 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
2033                           struct obd_quotactl *oqctl)
2034 {
2035         struct client_obd       *cli = &exp->exp_obd->u.cli;
2036         struct ptlrpc_request   *req;
2037         struct obd_quotactl     *body;
2038         int                      rc;
2039         ENTRY;
2040
2041         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2042                                         &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
2043                                         MDS_QUOTACHECK);
2044         if (req == NULL)
2045                 RETURN(-ENOMEM);
2046
2047         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2048         *body = *oqctl;
2049
2050         ptlrpc_request_set_replen(req);
2051
2052         /* the next poll will find -ENODATA, that means quotacheck is
2053          * going on */
2054         cli->cl_qchk_stat = -ENODATA;
2055         rc = ptlrpc_queue_wait(req);
2056         if (rc)
2057                 cli->cl_qchk_stat = rc;
2058         ptlrpc_req_finished(req);
2059         RETURN(rc);
2060 }
2061
2062 static int mdc_quota_poll_check(struct obd_export *exp,
2063                                 struct if_quotacheck *qchk)
2064 {
2065         struct client_obd *cli = &exp->exp_obd->u.cli;
2066         int rc;
2067         ENTRY;
2068
2069         qchk->obd_uuid = cli->cl_target_uuid;
2070         memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
2071
2072         rc = cli->cl_qchk_stat;
2073         /* the client is not the previous one */
2074         if (rc == CL_NOT_QUOTACHECKED)
2075                 rc = -EINTR;
2076         RETURN(rc);
2077 }
2078
2079 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2080                         struct obd_quotactl *oqctl)
2081 {
2082         struct ptlrpc_request   *req;
2083         struct obd_quotactl     *oqc;
2084         int                      rc;
2085         ENTRY;
2086
2087         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2088                                         &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
2089                                         MDS_QUOTACTL);
2090         if (req == NULL)
2091                 RETURN(-ENOMEM);
2092
2093         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2094         *oqc = *oqctl;
2095
2096         ptlrpc_request_set_replen(req);
2097         ptlrpc_at_set_req_timeout(req);
2098         req->rq_no_resend = 1;
2099
2100         rc = ptlrpc_queue_wait(req);
2101         if (rc)
2102                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
2103
2104         if (req->rq_repmsg &&
2105             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2106                 *oqctl = *oqc;
2107         } else if (!rc) {
2108                 CERROR ("Can't unpack obd_quotactl\n");
2109                 rc = -EPROTO;
2110         }
2111         ptlrpc_req_finished(req);
2112
2113         RETURN(rc);
2114 }
2115
2116 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2117                                 struct md_op_data *op_data)
2118 {
2119         struct list_head cancels = LIST_HEAD_INIT(cancels);
2120         struct ptlrpc_request   *req;
2121         int                      rc, count;
2122         struct mdc_swap_layouts *msl, *payload;
2123         ENTRY;
2124
2125         msl = op_data->op_data;
2126
2127         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2128          * first thing it will do is to cancel the 2 layout
2129          * locks hold by this client.
2130          * So the client must cancel its layout locks on the 2 fids
2131          * with the request RPC to avoid extra RPC round trips
2132          */
2133         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2134                                         LCK_EX, MDS_INODELOCK_LAYOUT |
2135                                         MDS_INODELOCK_XATTR);
2136         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2137                                          LCK_EX, MDS_INODELOCK_LAYOUT |
2138                                          MDS_INODELOCK_XATTR);
2139
2140         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2141                                    &RQF_MDS_SWAP_LAYOUTS);
2142         if (req == NULL) {
2143                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2144                 RETURN(-ENOMEM);
2145         }
2146
2147         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2148         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
2149
2150         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2151         if (rc) {
2152                 ptlrpc_request_free(req);
2153                 RETURN(rc);
2154         }
2155
2156         mdc_swap_layouts_pack(req, op_data);
2157
2158         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2159         LASSERT(payload);
2160
2161         *payload = *msl;
2162
2163         ptlrpc_request_set_replen(req);
2164
2165         rc = ptlrpc_queue_wait(req);
2166         if (rc)
2167                 GOTO(out, rc);
2168         EXIT;
2169
2170 out:
2171         ptlrpc_req_finished(req);
2172         return rc;
2173 }
2174
2175 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2176                          void *karg, void __user *uarg)
2177 {
2178         struct obd_device *obd = exp->exp_obd;
2179         struct obd_ioctl_data *data = karg;
2180         struct obd_import *imp = obd->u.cli.cl_import;
2181         int rc;
2182         ENTRY;
2183
2184         if (!try_module_get(THIS_MODULE)) {
2185                 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2186                        module_name(THIS_MODULE));
2187                 return -EINVAL;
2188         }
2189         switch (cmd) {
2190         case OBD_IOC_CHANGELOG_SEND:
2191                 rc = mdc_ioc_changelog_send(obd, karg);
2192                 GOTO(out, rc);
2193         case OBD_IOC_CHANGELOG_CLEAR: {
2194                 struct ioc_changelog *icc = karg;
2195                 struct changelog_setinfo cs =
2196                         {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
2197                 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2198                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2199                                         NULL);
2200                 GOTO(out, rc);
2201         }
2202         case OBD_IOC_FID2PATH:
2203                 rc = mdc_ioc_fid2path(exp, karg);
2204                 GOTO(out, rc);
2205         case LL_IOC_HSM_CT_START:
2206                 rc = mdc_ioc_hsm_ct_start(exp, karg);
2207                 /* ignore if it was already registered on this MDS. */
2208                 if (rc == -EEXIST)
2209                         rc = 0;
2210                 GOTO(out, rc);
2211         case LL_IOC_HSM_PROGRESS:
2212                 rc = mdc_ioc_hsm_progress(exp, karg);
2213                 GOTO(out, rc);
2214         case LL_IOC_HSM_STATE_GET:
2215                 rc = mdc_ioc_hsm_state_get(exp, karg);
2216                 GOTO(out, rc);
2217         case LL_IOC_HSM_STATE_SET:
2218                 rc = mdc_ioc_hsm_state_set(exp, karg);
2219                 GOTO(out, rc);
2220         case LL_IOC_HSM_ACTION:
2221                 rc = mdc_ioc_hsm_current_action(exp, karg);
2222                 GOTO(out, rc);
2223         case LL_IOC_HSM_REQUEST:
2224                 rc = mdc_ioc_hsm_request(exp, karg);
2225                 GOTO(out, rc);
2226         case OBD_IOC_CLIENT_RECOVER:
2227                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2228                 if (rc < 0)
2229                         GOTO(out, rc);
2230                 GOTO(out, rc = 0);
2231         case IOC_OSC_SET_ACTIVE:
2232                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2233                 GOTO(out, rc);
2234         case OBD_IOC_POLL_QUOTACHECK:
2235                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
2236                 GOTO(out, rc);
2237         case OBD_IOC_PING_TARGET:
2238                 rc = ptlrpc_obd_ping(obd);
2239                 GOTO(out, rc);
2240         /*
2241          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2242          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2243          * there'd be no LMV layer thus we might be called here. Eventually
2244          * this code should be removed.
2245          * bz20731, LU-592.
2246          */
2247         case IOC_OBD_STATFS: {
2248                 struct obd_statfs stat_buf = {0};
2249
2250                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2251                         GOTO(out, rc = -ENODEV);
2252
2253                 /* copy UUID */
2254                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2255                                  min((int)data->ioc_plen2,
2256                                      (int)sizeof(struct obd_uuid))))
2257                         GOTO(out, rc = -EFAULT);
2258
2259                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2260                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2261                                 0);
2262                 if (rc != 0)
2263                         GOTO(out, rc);
2264
2265                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2266                                      min((int) data->ioc_plen1,
2267                                          (int) sizeof(stat_buf))))
2268                         GOTO(out, rc = -EFAULT);
2269
2270                 GOTO(out, rc = 0);
2271         }
2272         case OBD_IOC_QUOTACTL: {
2273                 struct if_quotactl *qctl = karg;
2274                 struct obd_quotactl *oqctl;
2275
2276                 OBD_ALLOC_PTR(oqctl);
2277                 if (oqctl == NULL)
2278                         GOTO(out, rc = -ENOMEM);
2279
2280                 QCTL_COPY(oqctl, qctl);
2281                 rc = obd_quotactl(exp, oqctl);
2282                 if (rc == 0) {
2283                         QCTL_COPY(qctl, oqctl);
2284                         qctl->qc_valid = QC_MDTIDX;
2285                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2286                 }
2287
2288                 OBD_FREE_PTR(oqctl);
2289                 GOTO(out, rc);
2290         }
2291         case LL_IOC_GET_CONNECT_FLAGS:
2292                 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2293                                  sizeof(*exp_connect_flags_ptr(exp))))
2294                         GOTO(out, rc = -EFAULT);
2295
2296                 GOTO(out, rc = 0);
2297         case LL_IOC_LOV_SWAP_LAYOUTS:
2298                 rc = mdc_ioc_swap_layouts(exp, karg);
2299                 GOTO(out, rc);
2300         default:
2301                 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2302                 GOTO(out, rc = -ENOTTY);
2303         }
2304 out:
2305         module_put(THIS_MODULE);
2306
2307         return rc;
2308 }
2309
2310 static int mdc_get_info_rpc(struct obd_export *exp,
2311                             u32 keylen, void *key,
2312                             u32 vallen, void *val)
2313 {
2314         struct obd_import      *imp = class_exp2cliimp(exp);
2315         struct ptlrpc_request  *req;
2316         char                   *tmp;
2317         int                     rc = -EINVAL;
2318         ENTRY;
2319
2320         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2321         if (req == NULL)
2322                 RETURN(-ENOMEM);
2323
2324         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2325                              RCL_CLIENT, keylen);
2326         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2327                              RCL_CLIENT, sizeof(vallen));
2328
2329         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2330         if (rc) {
2331                 ptlrpc_request_free(req);
2332                 RETURN(rc);
2333         }
2334
2335         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2336         memcpy(tmp, key, keylen);
2337         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2338         memcpy(tmp, &vallen, sizeof(vallen));
2339
2340         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2341                              RCL_SERVER, vallen);
2342         ptlrpc_request_set_replen(req);
2343
2344         rc = ptlrpc_queue_wait(req);
2345         /* -EREMOTE means the get_info result is partial, and it needs to
2346          * continue on another MDT, see fid2path part in lmv_iocontrol */
2347         if (rc == 0 || rc == -EREMOTE) {
2348                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2349                 memcpy(val, tmp, vallen);
2350                 if (ptlrpc_rep_need_swab(req)) {
2351                         if (KEY_IS(KEY_FID2PATH))
2352                                 lustre_swab_fid2path(val);
2353                 }
2354         }
2355         ptlrpc_req_finished(req);
2356
2357         RETURN(rc);
2358 }
2359
2360 static void lustre_swab_hai(struct hsm_action_item *h)
2361 {
2362         __swab32s(&h->hai_len);
2363         __swab32s(&h->hai_action);
2364         lustre_swab_lu_fid(&h->hai_fid);
2365         lustre_swab_lu_fid(&h->hai_dfid);
2366         __swab64s(&h->hai_cookie);
2367         __swab64s(&h->hai_extent.offset);
2368         __swab64s(&h->hai_extent.length);
2369         __swab64s(&h->hai_gid);
2370 }
2371
2372 static void lustre_swab_hal(struct hsm_action_list *h)
2373 {
2374         struct hsm_action_item  *hai;
2375         __u32                    i;
2376
2377         __swab32s(&h->hal_version);
2378         __swab32s(&h->hal_count);
2379         __swab32s(&h->hal_archive_id);
2380         __swab64s(&h->hal_flags);
2381         hai = hai_first(h);
2382         for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2383                 lustre_swab_hai(hai);
2384 }
2385
2386 static void lustre_swab_kuch(struct kuc_hdr *l)
2387 {
2388         __swab16s(&l->kuc_magic);
2389         /* __u8 l->kuc_transport */
2390         __swab16s(&l->kuc_msgtype);
2391         __swab16s(&l->kuc_msglen);
2392 }
2393
2394 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2395                                 struct lustre_kernelcomm *lk)
2396 {
2397         struct obd_import  *imp = class_exp2cliimp(exp);
2398         __u32               archive = lk->lk_data;
2399         int                 rc = 0;
2400
2401         if (lk->lk_group != KUC_GRP_HSM) {
2402                 CERROR("Bad copytool group %d\n", lk->lk_group);
2403                 return -EINVAL;
2404         }
2405
2406         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2407                lk->lk_uid, lk->lk_group, lk->lk_flags);
2408
2409         if (lk->lk_flags & LK_FLG_STOP) {
2410                 /* Unregister with the coordinator */
2411                 rc = mdc_ioc_hsm_ct_unregister(imp);
2412         } else {
2413                 rc = mdc_ioc_hsm_ct_register(imp, archive);
2414         }
2415
2416         return rc;
2417 }
2418
2419 /**
2420  * Send a message to any listening copytools
2421  * @param val KUC message (kuc_hdr + hsm_action_list)
2422  * @param len total length of message
2423  */
2424 static int mdc_hsm_copytool_send(size_t len, void *val)
2425 {
2426         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
2427         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
2428         int                      rc;
2429         ENTRY;
2430
2431         if (len < sizeof(*lh) + sizeof(*hal)) {
2432                 CERROR("Short HSM message %zu < %zu\n", len,
2433                        sizeof(*lh) + sizeof(*hal));
2434                 RETURN(-EPROTO);
2435         }
2436         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2437                 lustre_swab_kuch(lh);
2438                 lustre_swab_hal(hal);
2439         } else if (lh->kuc_magic != KUC_MAGIC) {
2440                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2441                 RETURN(-EPROTO);
2442         }
2443
2444         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2445                "on %s\n",
2446                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2447                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2448
2449         /* Broadcast to HSM listeners */
2450         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2451
2452         RETURN(rc);
2453 }
2454
2455 /**
2456  * callback function passed to kuc for re-registering each HSM copytool
2457  * running on MDC, after MDT shutdown/recovery.
2458  * @param data copytool registration data
2459  * @param cb_arg callback argument (obd_import)
2460  */
2461 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2462 {
2463         struct kkuc_ct_data     *kcd = data;
2464         struct obd_import       *imp = (struct obd_import *)cb_arg;
2465         int                      rc;
2466
2467         if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2468                 return -EPROTO;
2469
2470         if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2471                 return 0;
2472
2473         CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2474                imp->imp_obd->obd_name, kcd->kcd_archive);
2475         rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2476
2477         /* ignore error if the copytool is already registered */
2478         return (rc == -EEXIST) ? 0 : rc;
2479 }
2480
2481 /**
2482  * Re-establish all kuc contexts with MDT
2483  * after MDT shutdown/recovery.
2484  */
2485 static int mdc_kuc_reregister(struct obd_import *imp)
2486 {
2487         /* re-register HSM agents */
2488         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2489                                          (void *)imp);
2490 }
2491
2492 static int mdc_set_info_async(const struct lu_env *env,
2493                               struct obd_export *exp,
2494                               u32 keylen, void *key,
2495                               u32 vallen, void *val,
2496                               struct ptlrpc_request_set *set)
2497 {
2498         struct obd_import       *imp = class_exp2cliimp(exp);
2499         int                      rc;
2500         ENTRY;
2501
2502         if (KEY_IS(KEY_READ_ONLY)) {
2503                 if (vallen != sizeof(int))
2504                         RETURN(-EINVAL);
2505
2506                 spin_lock(&imp->imp_lock);
2507                 if (*((int *)val)) {
2508                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2509                         imp->imp_connect_data.ocd_connect_flags |=
2510                                                         OBD_CONNECT_RDONLY;
2511                 } else {
2512                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2513                         imp->imp_connect_data.ocd_connect_flags &=
2514                                                         ~OBD_CONNECT_RDONLY;
2515                 }
2516                 spin_unlock(&imp->imp_lock);
2517
2518                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2519                                        keylen, key, vallen, val, set);
2520                 RETURN(rc);
2521         }
2522         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2523                 sptlrpc_conf_client_adapt(exp->exp_obd);
2524                 RETURN(0);
2525         }
2526         if (KEY_IS(KEY_FLUSH_CTX)) {
2527                 sptlrpc_import_flush_my_ctx(imp);
2528                 RETURN(0);
2529         }
2530         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2531                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2532                                        keylen, key, vallen, val, set);
2533                 RETURN(rc);
2534         }
2535         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2536                 rc = mdc_hsm_copytool_send(vallen, val);
2537                 RETURN(rc);
2538         }
2539
2540         if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2541                 __u32 *default_easize = val;
2542
2543                 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2544                 RETURN(0);
2545         }
2546
2547         CERROR("Unknown key %s\n", (char *)key);
2548         RETURN(-EINVAL);
2549 }
2550
2551 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2552                         __u32 keylen, void *key,
2553                         __u32 *vallen, void *val,
2554                         struct lov_stripe_md *lsm)
2555 {
2556         int rc = -EINVAL;
2557
2558         if (KEY_IS(KEY_MAX_EASIZE)) {
2559                 __u32 mdsize, *max_easize;
2560
2561                 if (*vallen != sizeof(int))
2562                         RETURN(-EINVAL);
2563                 mdsize = *(__u32 *)val;
2564                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2565                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2566                 max_easize = val;
2567                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2568                 RETURN(0);
2569         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2570                 __u32 *default_easize;
2571
2572                 if (*vallen != sizeof(int))
2573                         RETURN(-EINVAL);
2574                 default_easize = val;
2575                 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2576                 RETURN(0);
2577         } else if (KEY_IS(KEY_MAX_COOKIESIZE)) {
2578                 __u32 *max_cookiesize;
2579
2580                 if (*vallen != sizeof(int))
2581                         RETURN(-EINVAL);
2582                 max_cookiesize = val;
2583                 *max_cookiesize = exp->exp_obd->u.cli.cl_max_mds_cookiesize;
2584                 RETURN(0);
2585         } else if (KEY_IS(KEY_DEFAULT_COOKIESIZE)) {
2586                 __u32 *default_cookiesize;
2587
2588                 if (*vallen != sizeof(int))
2589                         RETURN(-EINVAL);
2590                 default_cookiesize = val;
2591                 *default_cookiesize =
2592                         exp->exp_obd->u.cli.cl_default_mds_cookiesize;
2593                 RETURN(0);
2594         } else if (KEY_IS(KEY_CONN_DATA)) {
2595                 struct obd_import *imp = class_exp2cliimp(exp);
2596                 struct obd_connect_data *data = val;
2597
2598                 if (*vallen != sizeof(*data))
2599                         RETURN(-EINVAL);
2600
2601                 *data = imp->imp_connect_data;
2602                 RETURN(0);
2603         } else if (KEY_IS(KEY_TGT_COUNT)) {
2604                 *((__u32 *)val) = 1;
2605                 RETURN(0);
2606         }
2607
2608         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2609
2610         RETURN(rc);
2611 }
2612
2613 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2614                      struct obd_capa *oc, struct ptlrpc_request **request)
2615 {
2616         struct ptlrpc_request *req;
2617         int                    rc;
2618         ENTRY;
2619
2620         *request = NULL;
2621         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2622         if (req == NULL)
2623                 RETURN(-ENOMEM);
2624
2625         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2626
2627         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2628         if (rc) {
2629                 ptlrpc_request_free(req);
2630                 RETURN(rc);
2631         }
2632
2633         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2634
2635         ptlrpc_request_set_replen(req);
2636
2637         rc = ptlrpc_queue_wait(req);
2638         if (rc)
2639                 ptlrpc_req_finished(req);
2640         else
2641                 *request = req;
2642         RETURN(rc);
2643 }
2644
2645 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2646                             enum obd_import_event event)
2647 {
2648         int rc = 0;
2649
2650         LASSERT(imp->imp_obd == obd);
2651
2652         switch (event) {
2653         case IMP_EVENT_DISCON: {
2654 #if 0
2655                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2656                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2657 #endif
2658                 break;
2659         }
2660         case IMP_EVENT_INACTIVE: {
2661                 struct client_obd *cli = &obd->u.cli;
2662                 /*
2663                  * Flush current sequence to make client obtain new one
2664                  * from server in case of disconnect/reconnect.
2665                  */
2666                 if (cli->cl_seq != NULL)
2667                         seq_client_flush(cli->cl_seq);
2668
2669                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2670                 break;
2671         }
2672         case IMP_EVENT_INVALIDATE: {
2673                 struct ldlm_namespace *ns = obd->obd_namespace;
2674
2675                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2676
2677                 break;
2678         }
2679         case IMP_EVENT_ACTIVE:
2680                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2681                 /* redo the kuc registration after reconnecting */
2682                 if (rc == 0)
2683                         rc = mdc_kuc_reregister(imp);
2684                 break;
2685         case IMP_EVENT_OCD:
2686                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2687                 break;
2688         case IMP_EVENT_DEACTIVATE:
2689         case IMP_EVENT_ACTIVATE:
2690                 break;
2691         default:
2692                 CERROR("Unknown import event %x\n", event);
2693                 LBUG();
2694         }
2695         RETURN(rc);
2696 }
2697
2698 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2699                   struct lu_fid *fid, struct md_op_data *op_data)
2700 {
2701         struct client_obd *cli = &exp->exp_obd->u.cli;
2702         struct lu_client_seq *seq = cli->cl_seq;
2703         ENTRY;
2704         RETURN(seq_client_alloc_fid(env, seq, fid));
2705 }
2706
2707 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2708 {
2709         struct client_obd *cli = &exp->exp_obd->u.cli;
2710         return &cli->cl_target_uuid;
2711 }
2712
2713 /**
2714  * Determine whether the lock can be canceled before replaying it during
2715  * recovery, non zero value will be return if the lock can be canceled,
2716  * or zero returned for not
2717  */
2718 static int mdc_cancel_weight(struct ldlm_lock *lock)
2719 {
2720         if (lock->l_resource->lr_type != LDLM_IBITS)
2721                 RETURN(0);
2722
2723         /* FIXME: if we ever get into a situation where there are too many
2724          * opened files with open locks on a single node, then we really
2725          * should replay these open locks to reget it */
2726         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2727                 RETURN(0);
2728
2729         RETURN(1);
2730 }
2731
2732 static int mdc_resource_inode_free(struct ldlm_resource *res)
2733 {
2734         if (res->lr_lvb_inode)
2735                 res->lr_lvb_inode = NULL;
2736
2737         return 0;
2738 }
2739
2740 static struct ldlm_valblock_ops inode_lvbo = {
2741         .lvbo_free = mdc_resource_inode_free
2742 };
2743
2744 static int mdc_llog_init(struct obd_device *obd)
2745 {
2746         struct obd_llog_group   *olg = &obd->obd_olg;
2747         struct llog_ctxt        *ctxt;
2748         int                      rc;
2749
2750         ENTRY;
2751
2752         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2753                         &llog_client_ops);
2754         if (rc < 0)
2755                 RETURN(rc);
2756
2757         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2758         llog_initiator_connect(ctxt);
2759         llog_ctxt_put(ctxt);
2760
2761         RETURN(0);
2762 }
2763
2764 static void mdc_llog_finish(struct obd_device *obd)
2765 {
2766         struct llog_ctxt *ctxt;
2767
2768         ENTRY;
2769
2770         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2771         if (ctxt != NULL)
2772                 llog_cleanup(NULL, ctxt);
2773
2774         EXIT;
2775 }
2776
2777 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2778 {
2779         struct client_obd               *cli = &obd->u.cli;
2780         int                             rc;
2781         ENTRY;
2782
2783         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2784         if (!cli->cl_rpc_lock)
2785                 RETURN(-ENOMEM);
2786         mdc_init_rpc_lock(cli->cl_rpc_lock);
2787
2788         rc = ptlrpcd_addref();
2789         if (rc < 0)
2790                 GOTO(err_rpc_lock, rc);
2791
2792         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2793         if (!cli->cl_close_lock)
2794                 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2795         mdc_init_rpc_lock(cli->cl_close_lock);
2796
2797         rc = client_obd_setup(obd, cfg);
2798         if (rc)
2799                 GOTO(err_close_lock, rc);
2800 #ifdef CONFIG_PROC_FS
2801         obd->obd_vars = lprocfs_mdc_obd_vars;
2802         lprocfs_obd_setup(obd);
2803         lprocfs_alloc_md_stats(obd, 0);
2804 #endif
2805         sptlrpc_lprocfs_cliobd_attach(obd);
2806         ptlrpc_lprocfs_register_obd(obd);
2807
2808         ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2809
2810         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2811
2812         rc = mdc_llog_init(obd);
2813         if (rc) {
2814                 mdc_cleanup(obd);
2815                 CERROR("failed to setup llogging subsystems\n");
2816         }
2817
2818         RETURN(rc);
2819
2820 err_close_lock:
2821         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2822 err_ptlrpcd_decref:
2823         ptlrpcd_decref();
2824 err_rpc_lock:
2825         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2826         RETURN(rc);
2827 }
2828
2829 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2830  * us to make MDS RPCs with large enough reply buffers to hold a default
2831  * sized EA and cookie without having to calculate this (via a call into the
2832  * LOV + OSCs) each time we make an RPC.  The maximum size is also tracked
2833  * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2834  * a large number of stripes is possible.  If a larger reply buffer is
2835  * required it will be reallocated in the ptlrpc layer due to overflow.
2836  */
2837 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2838                             __u32 def_easize, __u32 cookiesize,
2839                             __u32 def_cookiesize)
2840 {
2841         struct obd_device *obd = exp->exp_obd;
2842         struct client_obd *cli = &obd->u.cli;
2843         ENTRY;
2844
2845         if (cli->cl_max_mds_easize < easize)
2846                 cli->cl_max_mds_easize = easize;
2847
2848         if (cli->cl_default_mds_easize < def_easize)
2849                 cli->cl_default_mds_easize = def_easize;
2850
2851         if (cli->cl_max_mds_cookiesize < cookiesize)
2852                 cli->cl_max_mds_cookiesize = cookiesize;
2853
2854         if (cli->cl_default_mds_cookiesize < def_cookiesize)
2855                 cli->cl_default_mds_cookiesize = def_cookiesize;
2856
2857         RETURN(0);
2858 }
2859
2860 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2861 {
2862         int rc = 0;
2863         ENTRY;
2864
2865         switch (stage) {
2866         case OBD_CLEANUP_EARLY:
2867                 break;
2868         case OBD_CLEANUP_EXPORTS:
2869                 /* Failsafe, ok if racy */
2870                 if (obd->obd_type->typ_refcnt <= 1)
2871                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
2872
2873                 obd_cleanup_client_import(obd);
2874                 ptlrpc_lprocfs_unregister_obd(obd);
2875                 lprocfs_obd_cleanup(obd);
2876                 lprocfs_free_md_stats(obd);
2877                 mdc_llog_finish(obd);
2878                 break;
2879         }
2880         RETURN(rc);
2881 }
2882
2883 static int mdc_cleanup(struct obd_device *obd)
2884 {
2885         struct client_obd *cli = &obd->u.cli;
2886
2887         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2888         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2889
2890         ptlrpcd_decref();
2891
2892         return client_obd_cleanup(obd);
2893 }
2894
2895 static int mdc_process_config(struct obd_device *obd, size_t len, void *buf)
2896 {
2897         struct lustre_cfg *lcfg = buf;
2898         int rc = class_process_proc_param(PARAM_MDC, obd->obd_vars, lcfg, obd);
2899         return (rc > 0 ? 0: rc);
2900 }
2901
2902
2903 /* get remote permission for current user on fid */
2904 static int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2905                                struct obd_capa *oc, __u32 suppgid,
2906                                struct ptlrpc_request **request)
2907 {
2908         struct ptlrpc_request  *req;
2909         int                    rc;
2910         ENTRY;
2911
2912         LASSERT(client_is_remote(exp));
2913
2914         *request = NULL;
2915         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2916         if (req == NULL)
2917                 RETURN(-ENOMEM);
2918
2919         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2920
2921         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2922         if (rc) {
2923                 ptlrpc_request_free(req);
2924                 RETURN(rc);
2925         }
2926
2927         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2928
2929         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2930                              sizeof(struct mdt_remote_perm));
2931
2932         ptlrpc_request_set_replen(req);
2933
2934         rc = ptlrpc_queue_wait(req);
2935         if (rc)
2936                 ptlrpc_req_finished(req);
2937         else
2938                 *request = req;
2939         RETURN(rc);
2940 }
2941
2942 static int mdc_interpret_renew_capa(const struct lu_env *env,
2943                                     struct ptlrpc_request *req, void *args,
2944                                     int status)
2945 {
2946         struct mdc_renew_capa_args *ra = args;
2947         struct mdt_body *body = NULL;
2948         struct lustre_capa *capa;
2949         ENTRY;
2950
2951         if (status)
2952                 GOTO(out, capa = ERR_PTR(status));
2953
2954         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2955         if (body == NULL)
2956                 GOTO(out, capa = ERR_PTR(-EFAULT));
2957
2958         if ((body->mbo_valid & OBD_MD_FLOSSCAPA) == 0)
2959                 GOTO(out, capa = ERR_PTR(-ENOENT));
2960
2961         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2962         if (!capa)
2963                 GOTO(out, capa = ERR_PTR(-EFAULT));
2964         EXIT;
2965 out:
2966         ra->ra_cb(ra->ra_oc, capa);
2967         return 0;
2968 }
2969
2970 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2971                           renew_capa_cb_t cb)
2972 {
2973         struct ptlrpc_request *req;
2974         struct mdc_renew_capa_args *ra;
2975         ENTRY;
2976
2977         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2978                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
2979         if (req == NULL)
2980                 RETURN(-ENOMEM);
2981
2982         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2983          * capa to renew is oss capa.
2984          */
2985         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2986         ptlrpc_request_set_replen(req);
2987
2988         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2989         ra = ptlrpc_req_async_args(req);
2990         ra->ra_oc = oc;
2991         ra->ra_cb = cb;
2992         req->rq_interpret_reply = mdc_interpret_renew_capa;
2993         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2994         RETURN(0);
2995 }
2996
2997 static struct obd_ops mdc_obd_ops = {
2998         .o_owner            = THIS_MODULE,
2999         .o_setup            = mdc_setup,
3000         .o_precleanup       = mdc_precleanup,
3001         .o_cleanup          = mdc_cleanup,
3002         .o_add_conn         = client_import_add_conn,
3003         .o_del_conn         = client_import_del_conn,
3004         .o_connect          = client_connect_import,
3005         .o_disconnect       = client_disconnect_export,
3006         .o_iocontrol        = mdc_iocontrol,
3007         .o_set_info_async   = mdc_set_info_async,
3008         .o_statfs           = mdc_statfs,
3009         .o_fid_init         = client_fid_init,
3010         .o_fid_fini         = client_fid_fini,
3011         .o_fid_alloc        = mdc_fid_alloc,
3012         .o_import_event     = mdc_import_event,
3013         .o_get_info         = mdc_get_info,
3014         .o_process_config   = mdc_process_config,
3015         .o_get_uuid         = mdc_get_uuid,
3016         .o_quotactl         = mdc_quotactl,
3017         .o_quotacheck       = mdc_quotacheck
3018 };
3019
3020 static struct md_ops mdc_md_ops = {
3021         .m_getstatus        = mdc_getstatus,
3022         .m_null_inode       = mdc_null_inode,
3023         .m_find_cbdata      = mdc_find_cbdata,
3024         .m_close            = mdc_close,
3025         .m_create           = mdc_create,
3026         .m_enqueue          = mdc_enqueue,
3027         .m_getattr          = mdc_getattr,
3028         .m_getattr_name     = mdc_getattr_name,
3029         .m_intent_lock      = mdc_intent_lock,
3030         .m_link             = mdc_link,
3031         .m_rename           = mdc_rename,
3032         .m_setattr          = mdc_setattr,
3033         .m_setxattr         = mdc_setxattr,
3034         .m_getxattr         = mdc_getxattr,
3035         .m_fsync                = mdc_fsync,
3036         .m_read_page            = mdc_read_page,
3037         .m_unlink           = mdc_unlink,
3038         .m_cancel_unused    = mdc_cancel_unused,
3039         .m_init_ea_size     = mdc_init_ea_size,
3040         .m_set_lock_data    = mdc_set_lock_data,
3041         .m_lock_match       = mdc_lock_match,
3042         .m_get_lustre_md    = mdc_get_lustre_md,
3043         .m_free_lustre_md   = mdc_free_lustre_md,
3044         .m_set_open_replay_data = mdc_set_open_replay_data,
3045         .m_clear_open_replay_data = mdc_clear_open_replay_data,
3046         .m_renew_capa       = mdc_renew_capa,
3047         .m_unpack_capa      = mdc_unpack_capa,
3048         .m_get_remote_perm  = mdc_get_remote_perm,
3049         .m_intent_getattr_async = mdc_intent_getattr_async,
3050         .m_revalidate_lock      = mdc_revalidate_lock
3051 };
3052
3053 static int __init mdc_init(void)
3054 {
3055         return class_register_type(&mdc_obd_ops, &mdc_md_ops, true, NULL,
3056                                    LUSTRE_MDC_NAME, NULL);
3057 }
3058
3059 static void /*__exit*/ mdc_exit(void)
3060 {
3061         class_unregister_type(LUSTRE_MDC_NAME);
3062 }
3063
3064 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3065 MODULE_DESCRIPTION("Lustre Metadata Client");
3066 MODULE_LICENSE("GPL");
3067
3068 module_init(mdc_init);
3069 module_exit(mdc_exit);