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