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