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