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