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