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