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