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