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