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