Whamcloud - gitweb
LU-15252 mdc: add client tunable to disable LSOM update
[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 (!obd->u.cli.cl_lsom_update ||
952             !(exp_connect_flags2(exp) & OBD_CONNECT2_LSOM))
953                 op_data->op_xvalid &= ~(OP_XVALID_LAZYSIZE |
954                                         OP_XVALID_LAZYBLOCKS);
955
956         mdc_close_pack(&req->rq_pill, op_data);
957
958         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
959                              obd->u.cli.cl_default_mds_easize);
960
961         ptlrpc_request_set_replen(req);
962
963         ptlrpc_get_mod_rpc_slot(req);
964         rc = ptlrpc_queue_wait(req);
965         ptlrpc_put_mod_rpc_slot(req);
966
967         if (req->rq_repmsg == NULL) {
968                 CDEBUG(D_RPCTRACE, "request %p failed to send: rc = %d\n", req,
969                        req->rq_status);
970                 if (rc == 0)
971                         rc = req->rq_status ?: -EIO;
972         } else if (rc == 0 || rc == -EAGAIN) {
973                 struct mdt_body *body;
974
975                 rc = lustre_msg_get_status(req->rq_repmsg);
976                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
977                         DEBUG_REQ(D_ERROR, req,
978                                   "type = PTL_RPC_MSG_ERR: rc = %d", rc);
979                         if (rc > 0)
980                                 rc = -rc;
981                 }
982                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
983                 if (body == NULL)
984                         rc = -EPROTO;
985         } else if (rc == -ESTALE) {
986                 /**
987                  * it can be allowed error after 3633 if open was committed and
988                  * server failed before close was sent. Let's check if mod
989                  * exists and return no error in that case
990                  */
991                 if (mod) {
992                         DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
993                         LASSERT(mod->mod_open_req != NULL);
994                         if (mod->mod_open_req->rq_committed)
995                                 rc = 0;
996                 }
997         }
998
999 out:
1000         if (mod) {
1001                 if (rc != 0)
1002                         mod->mod_close_req = NULL;
1003                 /* Since now, mod is accessed through open_req only,
1004                  * thus close req does not keep a reference on mod anymore. */
1005                 obd_mod_put(mod);
1006         }
1007         *request = req;
1008
1009         RETURN(rc < 0 ? rc : saved_rc);
1010 }
1011
1012 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
1013                        u64 offset, struct page **pages, int npages,
1014                        struct ptlrpc_request **request)
1015 {
1016         struct ptlrpc_request   *req;
1017         struct ptlrpc_bulk_desc *desc;
1018         int                      i;
1019         int                      resends = 0;
1020         int                      rc;
1021         ENTRY;
1022
1023         *request = NULL;
1024
1025 restart_bulk:
1026         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1027         if (req == NULL)
1028                 RETURN(-ENOMEM);
1029
1030         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1031         if (rc) {
1032                 ptlrpc_request_free(req);
1033                 RETURN(rc);
1034         }
1035
1036         req->rq_request_portal = MDS_READPAGE_PORTAL;
1037         ptlrpc_at_set_req_timeout(req);
1038
1039         desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1040                                     PTLRPC_BULK_PUT_SINK,
1041                                     MDS_BULK_PORTAL,
1042                                     &ptlrpc_bulk_kiov_pin_ops);
1043         if (desc == NULL) {
1044                 ptlrpc_req_finished(req);
1045                 RETURN(-ENOMEM);
1046         }
1047
1048         /* NB req now owns desc and will free it when it gets freed */
1049         for (i = 0; i < npages; i++)
1050                 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1051                                                  PAGE_SIZE);
1052
1053         mdc_readdir_pack(&req->rq_pill, offset, PAGE_SIZE * npages, fid);
1054
1055         ptlrpc_request_set_replen(req);
1056         rc = ptlrpc_queue_wait(req);
1057         if (rc) {
1058                 ptlrpc_req_finished(req);
1059                 if (rc != -ETIMEDOUT)
1060                         RETURN(rc);
1061
1062                 resends++;
1063                 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1064                         CERROR("%s: too many resend retries: rc = %d\n",
1065                                exp->exp_obd->obd_name, -EIO);
1066                         RETURN(-EIO);
1067                 }
1068
1069                 /* If a signal interrupts then the timeout returned will
1070                  * not be zero. In that case return -EINTR
1071                  */
1072                 if (msleep_interruptible(resends * 1000))
1073                         RETURN(-EINTR);
1074
1075                 goto restart_bulk;
1076         }
1077
1078         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1079                                           req->rq_bulk->bd_nob_transferred);
1080         if (rc < 0) {
1081                 ptlrpc_req_finished(req);
1082                 RETURN(rc);
1083         }
1084
1085         if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1086                 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1087                        exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1088                        PAGE_SIZE * npages);
1089                 ptlrpc_req_finished(req);
1090                 RETURN(-EPROTO);
1091         }
1092
1093         *request = req;
1094         RETURN(0);
1095 }
1096
1097 static void mdc_release_page(struct page *page, int remove)
1098 {
1099         if (remove) {
1100                 lock_page(page);
1101                 if (likely(page->mapping != NULL))
1102                         delete_from_page_cache(page);
1103                 unlock_page(page);
1104         }
1105         put_page(page);
1106 }
1107
1108 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1109                                     __u64 *start, __u64 *end, int hash64)
1110 {
1111         /*
1112          * Complement of hash is used as an index so that
1113          * radix_tree_gang_lookup() can be used to find a page with starting
1114          * hash _smaller_ than one we are looking for.
1115          */
1116         unsigned long offset = hash_x_index(*hash, hash64);
1117         struct page *page;
1118         unsigned long flags;
1119         int found;
1120
1121         ll_xa_lock_irqsave(&mapping->i_pages, flags);
1122         found = radix_tree_gang_lookup(&mapping->page_tree,
1123                                        (void **)&page, offset, 1);
1124         if (found > 0 && !ll_xa_is_value(page)) {
1125                 struct lu_dirpage *dp;
1126
1127                 get_page(page);
1128                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1129                 /*
1130                  * In contrast to find_lock_page() we are sure that directory
1131                  * page cannot be truncated (while DLM lock is held) and,
1132                  * hence, can avoid restart.
1133                  *
1134                  * In fact, page cannot be locked here at all, because
1135                  * mdc_read_page_remote does synchronous io.
1136                  */
1137                 wait_on_page_locked(page);
1138                 if (PageUptodate(page)) {
1139                         dp = kmap(page);
1140                         if (BITS_PER_LONG == 32 && hash64) {
1141                                 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1142                                 *end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
1143                                 *hash  = *hash >> 32;
1144                         } else {
1145                                 *start = le64_to_cpu(dp->ldp_hash_start);
1146                                 *end   = le64_to_cpu(dp->ldp_hash_end);
1147                         }
1148                         if (unlikely(*start == 1 && *hash == 0))
1149                                 *hash = *start;
1150                         else
1151                                 LASSERTF(*start <= *hash, "start = %#llx"
1152                                          ",end = %#llx,hash = %#llx\n",
1153                                          *start, *end, *hash);
1154                         CDEBUG(D_VFSTRACE, "offset %lx [%#llx %#llx],"
1155                               " hash %#llx\n", offset, *start, *end, *hash);
1156                         if (*hash > *end) {
1157                                 kunmap(page);
1158                                 mdc_release_page(page, 0);
1159                                 page = NULL;
1160                         } else if (*end != *start && *hash == *end) {
1161                                 /*
1162                                  * upon hash collision, remove this page,
1163                                  * otherwise put page reference, and
1164                                  * mdc_read_page_remote() will issue RPC to
1165                                  * fetch the page we want.
1166                                  */
1167                                 kunmap(page);
1168                                 mdc_release_page(page,
1169                                     le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1170                                 page = NULL;
1171                         }
1172                 } else {
1173                         put_page(page);
1174                         page = ERR_PTR(-EIO);
1175                 }
1176         } else {
1177                 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1178                 page = NULL;
1179         }
1180         return page;
1181 }
1182
1183 /*
1184  * Adjust a set of pages, each page containing an array of lu_dirpages,
1185  * so that each page can be used as a single logical lu_dirpage.
1186  *
1187  * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1188  * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1189  * struct lu_dirent.  It has size up to LU_PAGE_SIZE. The ldp_hash_end
1190  * value is used as a cookie to request the next lu_dirpage in a
1191  * directory listing that spans multiple pages (two in this example):
1192  *   ________
1193  *  |        |
1194  * .|--------v-------   -----.
1195  * |s|e|f|p|ent|ent| ... |ent|
1196  * '--|--------------   -----'   Each PAGE contains a single
1197  *    '------.                   lu_dirpage.
1198  * .---------v-------   -----.
1199  * |s|e|f|p|ent| 0 | ... | 0 |
1200  * '-----------------   -----'
1201  *
1202  * However, on hosts where the native VM page size (PAGE_SIZE) is
1203  * larger than LU_PAGE_SIZE, a single host page may contain multiple
1204  * lu_dirpages. After reading the lu_dirpages from the MDS, the
1205  * ldp_hash_end of the first lu_dirpage refers to the one immediately
1206  * after it in the same PAGE (arrows simplified for brevity, but
1207  * in general e0==s1, e1==s2, etc.):
1208  *
1209  * .--------------------   -----.
1210  * |s0|e0|f0|p|ent|ent| ... |ent|
1211  * |---v----------------   -----|
1212  * |s1|e1|f1|p|ent|ent| ... |ent|
1213  * |---v----------------   -----|  Here, each PAGE contains
1214  *             ...                 multiple lu_dirpages.
1215  * |---v----------------   -----|
1216  * |s'|e'|f'|p|ent|ent| ... |ent|
1217  * '---|----------------   -----'
1218  *     v
1219  * .----------------------------.
1220  * |        next PAGE           |
1221  *
1222  * This structure is transformed into a single logical lu_dirpage as follows:
1223  *
1224  * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1225  *   labeled 'next PAGE'.
1226  *
1227  * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1228  *   a hash collision with the next page exists.
1229  *
1230  * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1231  *   to the first entry of the next lu_dirpage.
1232  */
1233 #if PAGE_SIZE > LU_PAGE_SIZE
1234 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1235 {
1236         int i;
1237
1238         for (i = 0; i < cfs_pgs; i++) {
1239                 struct lu_dirpage *dp = kmap(pages[i]);
1240                 struct lu_dirpage *first = dp;
1241                 struct lu_dirent *end_dirent = NULL;
1242                 struct lu_dirent *ent;
1243                 __u64 hash_end = dp->ldp_hash_end;
1244                 __u32 flags = dp->ldp_flags;
1245
1246                 while (--lu_pgs > 0) {
1247                         ent = lu_dirent_start(dp);
1248                         for (end_dirent = ent; ent != NULL;
1249                              end_dirent = ent, ent = lu_dirent_next(ent));
1250
1251                         /* Advance dp to next lu_dirpage. */
1252                         dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1253
1254                         /* Check if we've reached the end of the PAGE. */
1255                         if (!((unsigned long)dp & ~PAGE_MASK))
1256                                 break;
1257
1258                         /* Save the hash and flags of this lu_dirpage. */
1259                         hash_end = dp->ldp_hash_end;
1260                         flags = dp->ldp_flags;
1261
1262                         /* Check if lu_dirpage contains no entries. */
1263                         if (end_dirent == NULL)
1264                                 break;
1265
1266                         /* Enlarge the end entry lde_reclen from 0 to
1267                          * first entry of next lu_dirpage. */
1268                         LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1269                         end_dirent->lde_reclen =
1270                                 cpu_to_le16((char *)(dp->ldp_entries) -
1271                                             (char *)end_dirent);
1272                 }
1273
1274                 first->ldp_hash_end = hash_end;
1275                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1276                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1277
1278                 kunmap(pages[i]);
1279         }
1280         LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1281 }
1282 #else
1283 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1284 #endif  /* PAGE_SIZE > LU_PAGE_SIZE */
1285
1286 /* parameters for readdir page */
1287 struct readpage_param {
1288         struct md_op_data       *rp_mod;
1289         __u64                   rp_off;
1290         int                     rp_hash64;
1291         struct obd_export       *rp_exp;
1292         struct md_callback      *rp_cb;
1293 };
1294
1295 /**
1296  * Read pages from server.
1297  *
1298  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1299  * a header lu_dirpage which describes the start/end hash, and whether this
1300  * page is empty (contains no dir entry) or hash collide with next page.
1301  * After client receives reply, several pages will be integrated into dir page
1302  * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1303  * lu_dirpage for this integrated page will be adjusted.
1304  **/
1305 static int mdc_read_page_remote(void *data, struct page *page0)
1306 {
1307         struct readpage_param *rp = data;
1308         struct page **page_pool;
1309         struct page *page;
1310         struct lu_dirpage *dp;
1311         struct md_op_data *op_data = rp->rp_mod;
1312         struct ptlrpc_request *req;
1313         int max_pages;
1314         struct inode *inode;
1315         struct lu_fid *fid;
1316         int rd_pgs = 0; /* number of pages actually read */
1317         int npages;
1318         int i;
1319         int rc;
1320         ENTRY;
1321
1322         max_pages = rp->rp_exp->exp_obd->u.cli.cl_max_pages_per_rpc;
1323         inode = op_data->op_data;
1324         fid = &op_data->op_fid1;
1325         LASSERT(inode != NULL);
1326
1327         OBD_ALLOC_PTR_ARRAY_LARGE(page_pool, max_pages);
1328         if (page_pool != NULL) {
1329                 page_pool[0] = page0;
1330         } else {
1331                 page_pool = &page0;
1332                 max_pages = 1;
1333         }
1334
1335         for (npages = 1; npages < max_pages; npages++) {
1336                 page = page_cache_alloc(inode->i_mapping);
1337                 if (page == NULL)
1338                         break;
1339                 page_pool[npages] = page;
1340         }
1341
1342         rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, page_pool, npages, &req);
1343         if (rc < 0) {
1344                 /* page0 is special, which was added into page cache early */
1345                 delete_from_page_cache(page0);
1346         } else {
1347                 int lu_pgs;
1348
1349                 rd_pgs = (req->rq_bulk->bd_nob_transferred + PAGE_SIZE - 1) >>
1350                         PAGE_SHIFT;
1351                 lu_pgs = req->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
1352                 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1353
1354                 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1355
1356                 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1357
1358                 SetPageUptodate(page0);
1359         }
1360         unlock_page(page0);
1361
1362         ptlrpc_req_finished(req);
1363         CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1364         for (i = 1; i < npages; i++) {
1365                 unsigned long   offset;
1366                 __u64           hash;
1367                 int ret;
1368
1369                 page = page_pool[i];
1370
1371                 if (rc < 0 || i >= rd_pgs) {
1372                         put_page(page);
1373                         continue;
1374                 }
1375
1376                 SetPageUptodate(page);
1377
1378                 dp = kmap(page);
1379                 hash = le64_to_cpu(dp->ldp_hash_start);
1380                 kunmap(page);
1381
1382                 offset = hash_x_index(hash, rp->rp_hash64);
1383
1384                 prefetchw(&page->flags);
1385                 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1386                                             GFP_KERNEL);
1387                 if (ret == 0)
1388                         unlock_page(page);
1389                 else
1390                         CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1391                                " rc = %d\n", offset, ret);
1392                 put_page(page);
1393         }
1394
1395         if (page_pool != &page0)
1396                 OBD_FREE_PTR_ARRAY_LARGE(page_pool, max_pages);
1397
1398         RETURN(rc);
1399 }
1400
1401 /**
1402  * Read dir page from cache first, if it can not find it, read it from
1403  * server and add into the cache.
1404  *
1405  * \param[in] exp       MDC export
1406  * \param[in] op_data   client MD stack parameters, transfering parameters
1407  *                      between different layers on client MD stack.
1408  * \param[in] cb_op     callback required for ldlm lock enqueue during
1409  *                      read page
1410  * \param[in] hash_offset the hash offset of the page to be read
1411  * \param[in] ppage     the page to be read
1412  *
1413  * retval               = 0 get the page successfully
1414  *                      errno(<0) get the page failed
1415  */
1416 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1417                          struct md_callback *cb_op, __u64 hash_offset,
1418                          struct page **ppage)
1419 {
1420         struct lookup_intent    it = { .it_op = IT_READDIR };
1421         struct page             *page;
1422         struct inode            *dir = op_data->op_data;
1423         struct address_space    *mapping;
1424         struct lu_dirpage       *dp;
1425         __u64                   start = 0;
1426         __u64                   end = 0;
1427         struct lustre_handle    lockh;
1428         struct ptlrpc_request   *enq_req = NULL;
1429         struct readpage_param   rp_param;
1430         int rc;
1431
1432         ENTRY;
1433
1434         *ppage = NULL;
1435
1436         LASSERT(dir != NULL);
1437         mapping = dir->i_mapping;
1438
1439         rc = mdc_intent_lock(exp, op_data, &it, &enq_req,
1440                              cb_op->md_blocking_ast, 0);
1441         if (enq_req != NULL)
1442                 ptlrpc_req_finished(enq_req);
1443
1444         if (rc < 0) {
1445                 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1446                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1447                 RETURN(rc);
1448         }
1449
1450         rc = 0;
1451         lockh.cookie = it.it_lock_handle;
1452         mdc_set_lock_data(exp, &lockh, dir, NULL);
1453
1454         rp_param.rp_off = hash_offset;
1455         rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1456         page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1457                                rp_param.rp_hash64);
1458         if (IS_ERR(page)) {
1459                 CERROR("%s: dir page locate: "DFID" at %llu: rc %ld\n",
1460                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1461                        rp_param.rp_off, PTR_ERR(page));
1462                 GOTO(out_unlock, rc = PTR_ERR(page));
1463         } else if (page != NULL) {
1464                 /*
1465                  * XXX nikita: not entirely correct handling of a corner case:
1466                  * suppose hash chain of entries with hash value HASH crosses
1467                  * border between pages P0 and P1. First both P0 and P1 are
1468                  * cached, seekdir() is called for some entry from the P0 part
1469                  * of the chain. Later P0 goes out of cache. telldir(HASH)
1470                  * happens and finds P1, as it starts with matching hash
1471                  * value. Remaining entries from P0 part of the chain are
1472                  * skipped. (Is that really a bug?)
1473                  *
1474                  * Possible solutions: 0. don't cache P1 is such case, handle
1475                  * it as an "overflow" page. 1. invalidate all pages at
1476                  * once. 2. use HASH|1 as an index for P1.
1477                  */
1478                 GOTO(hash_collision, page);
1479         }
1480
1481         rp_param.rp_exp = exp;
1482         rp_param.rp_mod = op_data;
1483         page = read_cache_page(mapping,
1484                                hash_x_index(rp_param.rp_off,
1485                                             rp_param.rp_hash64),
1486                                mdc_read_page_remote, &rp_param);
1487         if (IS_ERR(page)) {
1488                 CDEBUG(D_INFO, "%s: read cache page: "DFID" at %llu: %ld\n",
1489                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1490                        rp_param.rp_off, PTR_ERR(page));
1491                 GOTO(out_unlock, rc = PTR_ERR(page));
1492         }
1493
1494         wait_on_page_locked(page);
1495         (void)kmap(page);
1496         if (!PageUptodate(page)) {
1497                 CERROR("%s: page not updated: "DFID" at %llu: rc %d\n",
1498                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1499                        rp_param.rp_off, -5);
1500                 goto fail;
1501         }
1502         if (!PageChecked(page))
1503                 SetPageChecked(page);
1504         if (PageError(page)) {
1505                 CERROR("%s: page error: "DFID" at %llu: rc %d\n",
1506                        exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1507                        rp_param.rp_off, -5);
1508                 goto fail;
1509         }
1510
1511 hash_collision:
1512         dp = page_address(page);
1513         if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1514                 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1515                 end   = le64_to_cpu(dp->ldp_hash_end) >> 32;
1516                 rp_param.rp_off = hash_offset >> 32;
1517         } else {
1518                 start = le64_to_cpu(dp->ldp_hash_start);
1519                 end   = le64_to_cpu(dp->ldp_hash_end);
1520                 rp_param.rp_off = hash_offset;
1521         }
1522         if (end == start) {
1523                 LASSERT(start == rp_param.rp_off);
1524                 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1525 #if BITS_PER_LONG == 32
1526                 CWARN("Real page-wide hash collision at [%llu %llu] with "
1527                       "hash %llu\n", le64_to_cpu(dp->ldp_hash_start),
1528                       le64_to_cpu(dp->ldp_hash_end), hash_offset);
1529 #endif
1530
1531                 /*
1532                  * Fetch whole overflow chain...
1533                  *
1534                  * XXX not yet.
1535                  */
1536                 goto fail;
1537         }
1538         *ppage = page;
1539 out_unlock:
1540         ldlm_lock_decref(&lockh, it.it_lock_mode);
1541         return rc;
1542 fail:
1543         kunmap(page);
1544         mdc_release_page(page, 1);
1545         rc = -EIO;
1546         goto out_unlock;
1547 }
1548
1549 static int mdc_statfs_interpret(const struct lu_env *env,
1550                                 struct ptlrpc_request *req, void *args, int rc)
1551 {
1552         struct obd_info *oinfo = args;
1553         struct obd_statfs *osfs;
1554
1555         if (!rc) {
1556                 osfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1557                 if (!osfs)
1558                         return -EPROTO;
1559
1560                 oinfo->oi_osfs = osfs;
1561
1562                 CDEBUG(D_CACHE, "blocks=%llu free=%llu avail=%llu "
1563                        "objects=%llu free=%llu state=%x\n",
1564                         osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
1565                         osfs->os_files, osfs->os_ffree, osfs->os_state);
1566         }
1567
1568         oinfo->oi_cb_up(oinfo, rc);
1569
1570         return rc;
1571 }
1572
1573 static int mdc_statfs_async(struct obd_export *exp,
1574                             struct obd_info *oinfo, time64_t max_age,
1575                             struct ptlrpc_request_set *unused)
1576 {
1577         struct ptlrpc_request *req;
1578         struct obd_info *aa;
1579
1580         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_STATFS,
1581                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1582         if (req == NULL)
1583                 return -ENOMEM;
1584
1585         ptlrpc_request_set_replen(req);
1586         req->rq_interpret_reply = mdc_statfs_interpret;
1587
1588         aa = ptlrpc_req_async_args(aa, req);
1589         *aa = *oinfo;
1590
1591         ptlrpcd_add_req(req);
1592
1593         return 0;
1594 }
1595
1596 static int mdc_statfs(const struct lu_env *env,
1597                       struct obd_export *exp, struct obd_statfs *osfs,
1598                       time64_t max_age, __u32 flags)
1599 {
1600         struct obd_device *obd = class_exp2obd(exp);
1601         struct req_format *fmt;
1602         struct ptlrpc_request *req;
1603         struct obd_statfs *msfs;
1604         struct obd_import *imp, *imp0;
1605         int rc;
1606         ENTRY;
1607
1608         /*
1609          * Since the request might also come from lprocfs, so we need
1610          * sync this with client_disconnect_export Bug15684
1611          */
1612         with_imp_locked(obd, imp0, rc)
1613                 imp = class_import_get(imp0);
1614         if (rc)
1615                 RETURN(rc);
1616
1617         fmt = &RQF_MDS_STATFS;
1618         if ((exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS) &&
1619             (flags & OBD_STATFS_SUM))
1620                 fmt = &RQF_MDS_STATFS_NEW;
1621         req = ptlrpc_request_alloc_pack(imp, fmt, LUSTRE_MDS_VERSION,
1622                                         MDS_STATFS);
1623         if (req == NULL)
1624                 GOTO(output, rc = -ENOMEM);
1625         req->rq_allow_intr = 1;
1626
1627         if ((flags & OBD_STATFS_SUM) &&
1628             (exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS)) {
1629                 /* request aggregated states */
1630                 struct mdt_body *body;
1631
1632                 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1633                 if (body == NULL)
1634                         GOTO(out, rc = -EPROTO);
1635                 body->mbo_valid = OBD_MD_FLAGSTATFS;
1636         }
1637
1638         ptlrpc_request_set_replen(req);
1639
1640         if (flags & OBD_STATFS_NODELAY) {
1641                 /* procfs requests not want stay in wait for avoid deadlock */
1642                 req->rq_no_resend = 1;
1643                 req->rq_no_delay = 1;
1644         }
1645
1646         rc = ptlrpc_queue_wait(req);
1647         if (rc) {
1648                 /* check connection error first */
1649                 if (imp->imp_connect_error)
1650                         rc = imp->imp_connect_error;
1651                 GOTO(out, rc);
1652         }
1653
1654         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1655         if (msfs == NULL)
1656                 GOTO(out, rc = -EPROTO);
1657
1658         *osfs = *msfs;
1659         EXIT;
1660 out:
1661         ptlrpc_req_finished(req);
1662 output:
1663         class_import_put(imp);
1664         return rc;
1665 }
1666
1667 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1668 {
1669         __u32 keylen, vallen;
1670         void *key;
1671         int rc;
1672
1673         if (gf->gf_pathlen > PATH_MAX)
1674                 RETURN(-ENAMETOOLONG);
1675         if (gf->gf_pathlen < 2)
1676                 RETURN(-EOVERFLOW);
1677
1678         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1679         keylen = cfs_size_round(sizeof(KEY_FID2PATH) + sizeof(*gf) +
1680                                 sizeof(struct lu_fid));
1681         OBD_ALLOC(key, keylen);
1682         if (key == NULL)
1683                 RETURN(-ENOMEM);
1684         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1685         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1686         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf),
1687                gf->gf_u.gf_root_fid, sizeof(struct lu_fid));
1688         CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1689                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1690
1691         if (!fid_is_sane(&gf->gf_fid))
1692                 GOTO(out, rc = -EINVAL);
1693
1694         /* Val is struct getinfo_fid2path result plus path */
1695         vallen = sizeof(*gf) + gf->gf_pathlen;
1696
1697         rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf);
1698         if (rc != 0 && rc != -EREMOTE)
1699                 GOTO(out, rc);
1700
1701         if (vallen <= sizeof(*gf))
1702                 GOTO(out, rc = -EPROTO);
1703         if (vallen > sizeof(*gf) + gf->gf_pathlen)
1704                 GOTO(out, rc = -EOVERFLOW);
1705
1706         CDEBUG(D_IOCTL, "path got "DFID" from %llu #%d: %s\n",
1707                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno,
1708                gf->gf_pathlen < 512 ? gf->gf_u.gf_path :
1709                /* only log the last 512 characters of the path */
1710                gf->gf_u.gf_path + gf->gf_pathlen - 512);
1711
1712 out:
1713         OBD_FREE(key, keylen);
1714         return rc;
1715 }
1716
1717 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1718                                 struct hsm_progress_kernel *hpk)
1719 {
1720         struct obd_import               *imp = class_exp2cliimp(exp);
1721         struct hsm_progress_kernel      *req_hpk;
1722         struct ptlrpc_request           *req;
1723         int                              rc;
1724         ENTRY;
1725
1726         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1727                                         LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1728         if (req == NULL)
1729                 GOTO(out, rc = -ENOMEM);
1730
1731         mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1732
1733         /* Copy hsm_progress struct */
1734         req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1735         if (req_hpk == NULL)
1736                 GOTO(out, rc = -EPROTO);
1737
1738         *req_hpk = *hpk;
1739         req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1740
1741         ptlrpc_request_set_replen(req);
1742
1743         ptlrpc_get_mod_rpc_slot(req);
1744         rc = ptlrpc_queue_wait(req);
1745         ptlrpc_put_mod_rpc_slot(req);
1746
1747         GOTO(out, rc);
1748 out:
1749         ptlrpc_req_finished(req);
1750         return rc;
1751 }
1752 /**
1753  * Send hsm_ct_register to MDS
1754  *
1755  * \param[in]   imp             import
1756  * \param[in]   archive_count   if in bitmap format, it is the bitmap,
1757  *                              else it is the count of archive_ids
1758  * \param[in]   archives        if in bitmap format, it is NULL,
1759  *                              else it is archive_id lists
1760  */
1761 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archive_count,
1762                                    __u32 *archives)
1763 {
1764         struct ptlrpc_request *req;
1765         __u32 *archive_array;
1766         size_t archives_size;
1767         int rc;
1768         ENTRY;
1769
1770         req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_CT_REGISTER);
1771         if (req == NULL)
1772                 RETURN(-ENOMEM);
1773
1774         if (archives != NULL)
1775                 archives_size = sizeof(*archive_array) * archive_count;
1776         else
1777                 archives_size = sizeof(archive_count);
1778
1779         req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_ARCHIVE,
1780                              RCL_CLIENT, archives_size);
1781
1782         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_CT_REGISTER);
1783         if (rc) {
1784                 ptlrpc_request_free(req);
1785                 RETURN(-ENOMEM);
1786         }
1787
1788         mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1789
1790         archive_array = req_capsule_client_get(&req->rq_pill,
1791                                                &RMF_MDS_HSM_ARCHIVE);
1792         if (archive_array == NULL)
1793                 GOTO(out, rc = -EPROTO);
1794
1795         if (archives != NULL)
1796                 memcpy(archive_array, archives, archives_size);
1797         else
1798                 *archive_array = archive_count;
1799
1800         ptlrpc_request_set_replen(req);
1801         req->rq_no_resend = 1;
1802
1803         rc = mdc_queue_wait(req);
1804         GOTO(out, rc);
1805 out:
1806         ptlrpc_req_finished(req);
1807         return rc;
1808 }
1809
1810 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1811                                       struct md_op_data *op_data)
1812 {
1813         struct hsm_current_action       *hca = op_data->op_data;
1814         struct hsm_current_action       *req_hca;
1815         struct ptlrpc_request           *req;
1816         int                              rc;
1817         ENTRY;
1818
1819         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1820                                    &RQF_MDS_HSM_ACTION);
1821         if (req == NULL)
1822                 RETURN(-ENOMEM);
1823
1824         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1825         if (rc) {
1826                 ptlrpc_request_free(req);
1827                 RETURN(rc);
1828         }
1829
1830         mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1831                       op_data->op_suppgids[0], 0);
1832
1833         ptlrpc_request_set_replen(req);
1834
1835         rc = mdc_queue_wait(req);
1836         if (rc)
1837                 GOTO(out, rc);
1838
1839         req_hca = req_capsule_server_get(&req->rq_pill,
1840                                          &RMF_MDS_HSM_CURRENT_ACTION);
1841         if (req_hca == NULL)
1842                 GOTO(out, rc = -EPROTO);
1843
1844         *hca = *req_hca;
1845
1846         EXIT;
1847 out:
1848         ptlrpc_req_finished(req);
1849         return rc;
1850 }
1851
1852 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1853 {
1854         struct ptlrpc_request   *req;
1855         int                      rc;
1856         ENTRY;
1857
1858         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1859                                         LUSTRE_MDS_VERSION,
1860                                         MDS_HSM_CT_UNREGISTER);
1861         if (req == NULL)
1862                 GOTO(out, rc = -ENOMEM);
1863
1864         mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1865
1866         ptlrpc_request_set_replen(req);
1867
1868         rc = mdc_queue_wait(req);
1869         GOTO(out, rc);
1870 out:
1871         ptlrpc_req_finished(req);
1872         return rc;
1873 }
1874
1875 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1876                                  struct md_op_data *op_data)
1877 {
1878         struct hsm_user_state   *hus = op_data->op_data;
1879         struct hsm_user_state   *req_hus;
1880         struct ptlrpc_request   *req;
1881         int                      rc;
1882         ENTRY;
1883
1884         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1885                                    &RQF_MDS_HSM_STATE_GET);
1886         if (req == NULL)
1887                 RETURN(-ENOMEM);
1888
1889         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1890         if (rc != 0) {
1891                 ptlrpc_request_free(req);
1892                 RETURN(rc);
1893         }
1894
1895         mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1896                       op_data->op_suppgids[0], 0);
1897
1898         ptlrpc_request_set_replen(req);
1899
1900         rc = mdc_queue_wait(req);
1901         if (rc)
1902                 GOTO(out, rc);
1903
1904         req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1905         if (req_hus == NULL)
1906                 GOTO(out, rc = -EPROTO);
1907
1908         *hus = *req_hus;
1909
1910         EXIT;
1911 out:
1912         ptlrpc_req_finished(req);
1913         return rc;
1914 }
1915
1916 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1917                                  struct md_op_data *op_data)
1918 {
1919         struct hsm_state_set    *hss = op_data->op_data;
1920         struct hsm_state_set    *req_hss;
1921         struct ptlrpc_request   *req;
1922         int                      rc;
1923         ENTRY;
1924
1925         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1926                                    &RQF_MDS_HSM_STATE_SET);
1927         if (req == NULL)
1928                 RETURN(-ENOMEM);
1929
1930         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1931         if (rc) {
1932                 ptlrpc_request_free(req);
1933                 RETURN(rc);
1934         }
1935
1936         mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1937                       op_data->op_suppgids[0], 0);
1938
1939         /* Copy states */
1940         req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1941         if (req_hss == NULL)
1942                 GOTO(out, rc = -EPROTO);
1943         *req_hss = *hss;
1944
1945         ptlrpc_request_set_replen(req);
1946
1947         ptlrpc_get_mod_rpc_slot(req);
1948         rc = ptlrpc_queue_wait(req);
1949         ptlrpc_put_mod_rpc_slot(req);
1950
1951         GOTO(out, rc);
1952 out:
1953         ptlrpc_req_finished(req);
1954         return rc;
1955 }
1956
1957 static int mdc_ioc_hsm_request(struct obd_export *exp,
1958                                struct hsm_user_request *hur)
1959 {
1960         struct obd_import       *imp = class_exp2cliimp(exp);
1961         struct ptlrpc_request   *req;
1962         struct hsm_request      *req_hr;
1963         struct hsm_user_item    *req_hui;
1964         char                    *req_opaque;
1965         int                      rc;
1966         ENTRY;
1967
1968         req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1969         if (req == NULL)
1970                 GOTO(out, rc = -ENOMEM);
1971
1972         req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1973                              hur->hur_request.hr_itemcount
1974                              * sizeof(struct hsm_user_item));
1975         req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1976                              hur->hur_request.hr_data_len);
1977
1978         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1979         if (rc) {
1980                 ptlrpc_request_free(req);
1981                 RETURN(rc);
1982         }
1983
1984         mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1985
1986         /* Copy hsm_request struct */
1987         req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1988         if (req_hr == NULL)
1989                 GOTO(out, rc = -EPROTO);
1990         *req_hr = hur->hur_request;
1991
1992         /* Copy hsm_user_item structs */
1993         req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1994         if (req_hui == NULL)
1995                 GOTO(out, rc = -EPROTO);
1996         memcpy(req_hui, hur->hur_user_item,
1997                hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1998
1999         /* Copy opaque field */
2000         req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
2001         if (req_opaque == NULL)
2002                 GOTO(out, rc = -EPROTO);
2003         memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
2004
2005         ptlrpc_request_set_replen(req);
2006
2007         ptlrpc_get_mod_rpc_slot(req);
2008         rc = ptlrpc_queue_wait(req);
2009         ptlrpc_put_mod_rpc_slot(req);
2010
2011         GOTO(out, rc);
2012
2013 out:
2014         ptlrpc_req_finished(req);
2015         return rc;
2016 }
2017
2018 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2019                                 struct lustre_kernelcomm *lk);
2020
2021 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2022                         struct obd_quotactl *oqctl)
2023 {
2024         struct ptlrpc_request *req;
2025         struct obd_quotactl *oqc;
2026         int rc;
2027         ENTRY;
2028
2029         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_QUOTACTL);
2030         if (req == NULL)
2031                 RETURN(-ENOMEM);
2032
2033
2034         if (LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd))
2035                 req_capsule_set_size(&req->rq_pill,
2036                                      &RMF_OBD_QUOTACTL,
2037                                      RCL_CLIENT,
2038                                      sizeof(*oqc) + LOV_MAXPOOLNAME + 1);
2039
2040         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION,
2041                                  MDS_QUOTACTL);
2042         if (rc) {
2043                 ptlrpc_request_free(req);
2044                 RETURN(rc);
2045         }
2046
2047         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2048         QCTL_COPY(oqc, oqctl);
2049
2050         ptlrpc_request_set_replen(req);
2051         ptlrpc_at_set_req_timeout(req);
2052
2053         rc = ptlrpc_queue_wait(req);
2054         if (rc) {
2055                 CERROR("%s: ptlrpc_queue_wait failed: rc = %d\n",
2056                        exp->exp_obd->obd_name, rc);
2057                 GOTO(out, rc);
2058         }
2059
2060         if (req->rq_repmsg &&
2061             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2062                 QCTL_COPY(oqctl, oqc);
2063         } else if (!rc) {
2064                 rc = -EPROTO;
2065                 CERROR("%s: cannot unpack obd_quotactl: rc = %d\n",
2066                         exp->exp_obd->obd_name, rc);
2067         }
2068 out:
2069         ptlrpc_req_finished(req);
2070
2071         RETURN(rc);
2072 }
2073
2074 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2075                                 struct md_op_data *op_data)
2076 {
2077         LIST_HEAD(cancels);
2078         struct ptlrpc_request   *req;
2079         int                      rc, count;
2080         struct mdc_swap_layouts *msl, *payload;
2081         ENTRY;
2082
2083         msl = op_data->op_data;
2084
2085         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2086          * first thing it will do is to cancel the 2 layout
2087          * locks held by this client.
2088          * So the client must cancel its layout locks on the 2 fids
2089          * with the request RPC to avoid extra RPC round trips.
2090          */
2091         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2092                                         LCK_EX, MDS_INODELOCK_LAYOUT |
2093                                         MDS_INODELOCK_XATTR);
2094         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2095                                          LCK_EX, MDS_INODELOCK_LAYOUT |
2096                                          MDS_INODELOCK_XATTR);
2097
2098         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2099                                    &RQF_MDS_SWAP_LAYOUTS);
2100         if (req == NULL) {
2101                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2102                 RETURN(-ENOMEM);
2103         }
2104
2105         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2106         if (rc) {
2107                 ptlrpc_request_free(req);
2108                 RETURN(rc);
2109         }
2110
2111         mdc_swap_layouts_pack(&req->rq_pill, op_data);
2112
2113         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2114         LASSERT(payload);
2115
2116         *payload = *msl;
2117
2118         ptlrpc_request_set_replen(req);
2119
2120         rc = ptlrpc_queue_wait(req);
2121         if (rc)
2122                 GOTO(out, rc);
2123         EXIT;
2124
2125 out:
2126         ptlrpc_req_finished(req);
2127         return rc;
2128 }
2129
2130 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2131                          void *karg, void __user *uarg)
2132 {
2133         struct obd_device *obd = exp->exp_obd;
2134         struct obd_ioctl_data *data = karg;
2135         struct obd_import *imp = obd->u.cli.cl_import;
2136         int rc;
2137         ENTRY;
2138
2139         if (!try_module_get(THIS_MODULE)) {
2140                 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2141                        module_name(THIS_MODULE));
2142                 return -EINVAL;
2143         }
2144         switch (cmd) {
2145         case OBD_IOC_FID2PATH:
2146                 rc = mdc_ioc_fid2path(exp, karg);
2147                 GOTO(out, rc);
2148         case LL_IOC_HSM_CT_START:
2149                 rc = mdc_ioc_hsm_ct_start(exp, karg);
2150                 /* ignore if it was already registered on this MDS. */
2151                 if (rc == -EEXIST)
2152                         rc = 0;
2153                 GOTO(out, rc);
2154         case LL_IOC_HSM_PROGRESS:
2155                 rc = mdc_ioc_hsm_progress(exp, karg);
2156                 GOTO(out, rc);
2157         case LL_IOC_HSM_STATE_GET:
2158                 rc = mdc_ioc_hsm_state_get(exp, karg);
2159                 GOTO(out, rc);
2160         case LL_IOC_HSM_STATE_SET:
2161                 rc = mdc_ioc_hsm_state_set(exp, karg);
2162                 GOTO(out, rc);
2163         case LL_IOC_HSM_ACTION:
2164                 rc = mdc_ioc_hsm_current_action(exp, karg);
2165                 GOTO(out, rc);
2166         case LL_IOC_HSM_REQUEST:
2167                 rc = mdc_ioc_hsm_request(exp, karg);
2168                 GOTO(out, rc);
2169         case OBD_IOC_CLIENT_RECOVER:
2170                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2171                 if (rc < 0)
2172                         GOTO(out, rc);
2173                 GOTO(out, rc = 0);
2174         case IOC_OSC_SET_ACTIVE:
2175                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2176                 GOTO(out, rc);
2177         /*
2178          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2179          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2180          * there'd be no LMV layer thus we might be called here. Eventually
2181          * this code should be removed.
2182          * bz20731, LU-592.
2183          */
2184         case IOC_OBD_STATFS: {
2185                 struct obd_statfs stat_buf = {0};
2186
2187                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2188                         GOTO(out, rc = -ENODEV);
2189
2190                 /* copy UUID */
2191                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2192                                  min((int)data->ioc_plen2,
2193                                      (int)sizeof(struct obd_uuid))))
2194                         GOTO(out, rc = -EFAULT);
2195
2196                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2197                                 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
2198                                 0);
2199                 if (rc != 0)
2200                         GOTO(out, rc);
2201
2202                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2203                                      min((int) data->ioc_plen1,
2204                                          (int) sizeof(stat_buf))))
2205                         GOTO(out, rc = -EFAULT);
2206
2207                 GOTO(out, rc = 0);
2208         }
2209         case OBD_IOC_QUOTACTL: {
2210                 struct if_quotactl *qctl = karg;
2211                 struct obd_quotactl *oqctl;
2212
2213                 OBD_ALLOC_PTR(oqctl);
2214                 if (oqctl == NULL)
2215                         GOTO(out, rc = -ENOMEM);
2216
2217                 QCTL_COPY(oqctl, qctl);
2218                 rc = obd_quotactl(exp, oqctl);
2219                 if (rc == 0) {
2220                         QCTL_COPY(qctl, oqctl);
2221                         qctl->qc_valid = QC_MDTIDX;
2222                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2223                 }
2224
2225                 OBD_FREE_PTR(oqctl);
2226                 GOTO(out, rc);
2227         }
2228         case LL_IOC_GET_CONNECT_FLAGS:
2229                 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2230                                  sizeof(*exp_connect_flags_ptr(exp))))
2231                         GOTO(out, rc = -EFAULT);
2232
2233                 GOTO(out, rc = 0);
2234         case LL_IOC_LOV_SWAP_LAYOUTS:
2235                 rc = mdc_ioc_swap_layouts(exp, karg);
2236                 GOTO(out, rc);
2237         default:
2238                 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2239                 GOTO(out, rc = -ENOTTY);
2240         }
2241 out:
2242         module_put(THIS_MODULE);
2243
2244         return rc;
2245 }
2246
2247 static int mdc_get_info_rpc(struct obd_export *exp,
2248                             u32 keylen, void *key,
2249                             u32 vallen, void *val)
2250 {
2251         struct obd_import      *imp = class_exp2cliimp(exp);
2252         struct ptlrpc_request  *req;
2253         char                   *tmp;
2254         int                     rc = -EINVAL;
2255         ENTRY;
2256
2257         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2258         if (req == NULL)
2259                 RETURN(-ENOMEM);
2260
2261         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2262                              RCL_CLIENT, keylen);
2263         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2264                              RCL_CLIENT, sizeof(vallen));
2265
2266         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2267         if (rc) {
2268                 ptlrpc_request_free(req);
2269                 RETURN(rc);
2270         }
2271
2272         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2273         memcpy(tmp, key, keylen);
2274         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2275         memcpy(tmp, &vallen, sizeof(vallen));
2276
2277         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2278                              RCL_SERVER, vallen);
2279         ptlrpc_request_set_replen(req);
2280
2281         /* if server failed to resolve FID, and OI scrub not able to fix it, it
2282          * will return -EINPROGRESS, ptlrpc_queue_wait() will keep retrying,
2283          * set request interruptible to avoid deadlock.
2284          */
2285         if (KEY_IS(KEY_FID2PATH))
2286                 req->rq_allow_intr = 1;
2287
2288         rc = ptlrpc_queue_wait(req);
2289         /* -EREMOTE means the get_info result is partial, and it needs to
2290          * continue on another MDT, see fid2path part in lmv_iocontrol */
2291         if (rc == 0 || rc == -EREMOTE) {
2292                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2293                 memcpy(val, tmp, vallen);
2294                 if (req_capsule_rep_need_swab(&req->rq_pill)) {
2295                         if (KEY_IS(KEY_FID2PATH))
2296                                 lustre_swab_fid2path(val);
2297                 }
2298         }
2299         ptlrpc_req_finished(req);
2300
2301         RETURN(rc);
2302 }
2303
2304 static void lustre_swab_hai(struct hsm_action_item *h)
2305 {
2306         __swab32s(&h->hai_len);
2307         __swab32s(&h->hai_action);
2308         lustre_swab_lu_fid(&h->hai_fid);
2309         lustre_swab_lu_fid(&h->hai_dfid);
2310         __swab64s(&h->hai_cookie);
2311         __swab64s(&h->hai_extent.offset);
2312         __swab64s(&h->hai_extent.length);
2313         __swab64s(&h->hai_gid);
2314 }
2315
2316 static void lustre_swab_hal(struct hsm_action_list *h)
2317 {
2318         struct hsm_action_item  *hai;
2319         __u32                    i;
2320
2321         __swab32s(&h->hal_version);
2322         __swab32s(&h->hal_count);
2323         __swab32s(&h->hal_archive_id);
2324         __swab64s(&h->hal_flags);
2325         hai = hai_first(h);
2326         for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2327                 lustre_swab_hai(hai);
2328 }
2329
2330 static void lustre_swab_kuch(struct kuc_hdr *l)
2331 {
2332         __swab16s(&l->kuc_magic);
2333         /* __u8 l->kuc_transport */
2334         __swab16s(&l->kuc_msgtype);
2335         __swab16s(&l->kuc_msglen);
2336 }
2337
2338 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2339                                 struct lustre_kernelcomm *lk)
2340 {
2341         struct obd_import *imp = class_exp2cliimp(exp);
2342         int rc = 0;
2343
2344         if (lk->lk_group != KUC_GRP_HSM) {
2345                 CERROR("Bad copytool group %d\n", lk->lk_group);
2346                 return -EINVAL;
2347         }
2348
2349         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2350                lk->lk_uid, lk->lk_group, lk->lk_flags);
2351
2352         if (lk->lk_flags & LK_FLG_STOP) {
2353                 /* Unregister with the coordinator */
2354                 rc = mdc_ioc_hsm_ct_unregister(imp);
2355         } else {
2356                 __u32 *archives = NULL;
2357
2358                 if ((lk->lk_flags & LK_FLG_DATANR) && lk->lk_data_count > 0)
2359                         archives = lk->lk_data;
2360
2361                 rc = mdc_ioc_hsm_ct_register(imp, lk->lk_data_count, archives);
2362         }
2363
2364         return rc;
2365 }
2366
2367 /**
2368  * Send a message to any listening copytools
2369  * @param val KUC message (kuc_hdr + hsm_action_list)
2370  * @param len total length of message
2371  */
2372 static int mdc_hsm_copytool_send(const struct obd_uuid *uuid,
2373                                  size_t len, void *val)
2374 {
2375         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
2376         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
2377         int                      rc;
2378         ENTRY;
2379
2380         if (len < sizeof(*lh) + sizeof(*hal)) {
2381                 CERROR("Short HSM message %zu < %zu\n", len,
2382                        sizeof(*lh) + sizeof(*hal));
2383                 RETURN(-EPROTO);
2384         }
2385         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2386                 lustre_swab_kuch(lh);
2387                 lustre_swab_hal(hal);
2388         } else if (lh->kuc_magic != KUC_MAGIC) {
2389                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2390                 RETURN(-EPROTO);
2391         }
2392
2393         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2394                "on %s\n",
2395                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2396                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2397
2398         /* Broadcast to HSM listeners */
2399         rc = libcfs_kkuc_group_put(uuid, KUC_GRP_HSM, lh);
2400
2401         RETURN(rc);
2402 }
2403
2404 /**
2405  * callback function passed to kuc for re-registering each HSM copytool
2406  * running on MDC, after MDT shutdown/recovery.
2407  * @param data copytool registration data
2408  * @param cb_arg callback argument (obd_import)
2409  */
2410 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2411 {
2412         struct obd_import *imp = (struct obd_import *)cb_arg;
2413         struct kkuc_ct_data *kcd = data;
2414         __u32 *archives = NULL;
2415         int rc;
2416
2417         if (kcd == NULL ||
2418             (kcd->kcd_magic != KKUC_CT_DATA_ARRAY_MAGIC &&
2419              kcd->kcd_magic != KKUC_CT_DATA_BITMAP_MAGIC))
2420                 return -EPROTO;
2421
2422         if (kcd->kcd_magic == KKUC_CT_DATA_BITMAP_MAGIC) {
2423                 CDEBUG(D_HA, "%s: recover copytool registration to MDT "
2424                        "(archive=%#x)\n", imp->imp_obd->obd_name,
2425                        kcd->kcd_nr_archives);
2426         } else {
2427                 CDEBUG(D_HA, "%s: recover copytool registration to MDT "
2428                        "(archive nr = %u)\n",
2429                        imp->imp_obd->obd_name, kcd->kcd_nr_archives);
2430                 if (kcd->kcd_nr_archives != 0)
2431                         archives = kcd->kcd_archives;
2432         }
2433
2434         rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_nr_archives, archives);
2435         /* ignore error if the copytool is already registered */
2436         return (rc == -EEXIST) ? 0 : rc;
2437 }
2438
2439 /**
2440  * Re-establish all kuc contexts with MDT
2441  * after MDT shutdown/recovery.
2442  */
2443 static int mdc_kuc_reregister(struct obd_import *imp)
2444 {
2445         /* re-register HSM agents */
2446         return libcfs_kkuc_group_foreach(&imp->imp_obd->obd_uuid, KUC_GRP_HSM,
2447                                          mdc_hsm_ct_reregister, imp);
2448 }
2449
2450 static int mdc_set_info_async(const struct lu_env *env,
2451                               struct obd_export *exp,
2452                               u32 keylen, void *key,
2453                               u32 vallen, void *val,
2454                               struct ptlrpc_request_set *set)
2455 {
2456         struct obd_import       *imp = class_exp2cliimp(exp);
2457         int                      rc;
2458         ENTRY;
2459
2460         if (KEY_IS(KEY_READ_ONLY)) {
2461                 if (vallen != sizeof(int))
2462                         RETURN(-EINVAL);
2463
2464                 spin_lock(&imp->imp_lock);
2465                 if (*((int *)val)) {
2466                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2467                         imp->imp_connect_data.ocd_connect_flags |=
2468                                                         OBD_CONNECT_RDONLY;
2469                 } else {
2470                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2471                         imp->imp_connect_data.ocd_connect_flags &=
2472                                                         ~OBD_CONNECT_RDONLY;
2473                 }
2474                 spin_unlock(&imp->imp_lock);
2475
2476                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2477                                        keylen, key, vallen, val, set);
2478                 RETURN(rc);
2479         }
2480         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2481                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2482                                        keylen, key, vallen, val, set);
2483                 RETURN(rc);
2484         }
2485         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2486                 rc = mdc_hsm_copytool_send(&imp->imp_obd->obd_uuid, vallen,
2487                                            val);
2488                 RETURN(rc);
2489         }
2490
2491         if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2492                 __u32 *default_easize = val;
2493
2494                 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2495                 RETURN(0);
2496         }
2497
2498         rc = osc_set_info_async(env, exp, keylen, key, vallen, val, set);
2499         RETURN(rc);
2500 }
2501
2502 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2503                         __u32 keylen, void *key, __u32 *vallen, void *val)
2504 {
2505         int rc = -EINVAL;
2506
2507         if (KEY_IS(KEY_MAX_EASIZE)) {
2508                 __u32 mdsize, *max_easize;
2509
2510                 if (*vallen != sizeof(int))
2511                         RETURN(-EINVAL);
2512                 mdsize = *(__u32 *)val;
2513                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2514                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2515                 max_easize = val;
2516                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2517                 RETURN(0);
2518         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2519                 __u32 *default_easize;
2520
2521                 if (*vallen != sizeof(int))
2522                         RETURN(-EINVAL);
2523                 default_easize = val;
2524                 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2525                 RETURN(0);
2526         } else if (KEY_IS(KEY_CONN_DATA)) {
2527                 struct obd_import *imp = class_exp2cliimp(exp);
2528                 struct obd_connect_data *data = val;
2529
2530                 if (*vallen != sizeof(*data))
2531                         RETURN(-EINVAL);
2532
2533                 *data = imp->imp_connect_data;
2534                 RETURN(0);
2535         } else if (KEY_IS(KEY_TGT_COUNT)) {
2536                 *((__u32 *)val) = 1;
2537                 RETURN(0);
2538         }
2539
2540         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2541
2542         RETURN(rc);
2543 }
2544
2545 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2546                      struct ptlrpc_request **request)
2547 {
2548         struct ptlrpc_request *req;
2549         int                    rc;
2550         ENTRY;
2551
2552         *request = NULL;
2553         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2554         if (req == NULL)
2555                 RETURN(-ENOMEM);
2556
2557         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2558         if (rc) {
2559                 ptlrpc_request_free(req);
2560                 RETURN(rc);
2561         }
2562
2563         mdc_pack_body(&req->rq_pill, fid, 0, 0, -1, 0);
2564
2565         ptlrpc_request_set_replen(req);
2566
2567         rc = ptlrpc_queue_wait(req);
2568         if (rc)
2569                 ptlrpc_req_finished(req);
2570         else
2571                 *request = req;
2572         RETURN(rc);
2573 }
2574
2575 struct mdc_rmfid_args {
2576         int *mra_rcs;
2577         int mra_nr;
2578 };
2579
2580 int mdc_rmfid_interpret(const struct lu_env *env, struct ptlrpc_request *req,
2581                           void *args, int rc)
2582 {
2583         struct mdc_rmfid_args *aa;
2584         int *rcs, size;
2585         ENTRY;
2586
2587         if (!rc) {
2588                 aa = ptlrpc_req_async_args(aa, req);
2589
2590                 size = req_capsule_get_size(&req->rq_pill, &RMF_RCS,
2591                                             RCL_SERVER);
2592                 LASSERT(size == sizeof(int) * aa->mra_nr);
2593                 rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
2594                 LASSERT(rcs);
2595                 LASSERT(aa->mra_rcs);
2596                 LASSERT(aa->mra_nr);
2597                 memcpy(aa->mra_rcs, rcs, size);
2598         }
2599
2600         RETURN(rc);
2601 }
2602
2603 static int mdc_rmfid(struct obd_export *exp, struct fid_array *fa,
2604                      int *rcs, struct ptlrpc_request_set *set)
2605 {
2606         struct ptlrpc_request *req;
2607         struct mdc_rmfid_args *aa;
2608         struct mdt_body *b;
2609         struct lu_fid *tmp;
2610         int rc, flen;
2611         ENTRY;
2612
2613         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_RMFID);
2614         if (req == NULL)
2615                 RETURN(-ENOMEM);
2616
2617         flen = fa->fa_nr * sizeof(struct lu_fid);
2618         req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
2619                              RCL_CLIENT, flen);
2620         req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
2621                              RCL_SERVER, flen);
2622         req_capsule_set_size(&req->rq_pill, &RMF_RCS,
2623                              RCL_SERVER, fa->fa_nr * sizeof(__u32));
2624         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_RMFID);
2625         if (rc) {
2626                 ptlrpc_request_free(req);
2627                 RETURN(rc);
2628         }
2629         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FID_ARRAY);
2630         memcpy(tmp, fa->fa_fids, flen);
2631
2632         mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
2633         b = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2634         b->mbo_ctime = ktime_get_real_seconds();
2635
2636         ptlrpc_request_set_replen(req);
2637
2638         LASSERT(rcs);
2639         aa = ptlrpc_req_async_args(aa, req);
2640         aa->mra_rcs = rcs;
2641         aa->mra_nr = fa->fa_nr;
2642         req->rq_interpret_reply = mdc_rmfid_interpret;
2643
2644         ptlrpc_set_add_req(set, req);
2645         ptlrpc_check_set(NULL, set);
2646
2647         RETURN(rc);
2648 }
2649
2650 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2651                             enum obd_import_event event)
2652 {
2653         struct client_obd *cli = &obd->u.cli;
2654         int rc = 0;
2655
2656         LASSERT(imp->imp_obd == obd);
2657
2658         switch (event) {
2659         case IMP_EVENT_DISCON:
2660                 spin_lock(&cli->cl_loi_list_lock);
2661                 cli->cl_avail_grant = 0;
2662                 cli->cl_lost_grant = 0;
2663                 spin_unlock(&cli->cl_loi_list_lock);
2664                 break;
2665         case IMP_EVENT_INACTIVE:
2666                 /*
2667                  * Flush current sequence to make client obtain new one
2668                  * from server in case of disconnect/reconnect.
2669                  */
2670                 down_read(&cli->cl_seq_rwsem);
2671                 if (cli->cl_seq)
2672                         seq_client_flush(cli->cl_seq);
2673                 up_read(&cli->cl_seq_rwsem);
2674
2675                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE);
2676                 break;
2677         case IMP_EVENT_INVALIDATE: {
2678                 struct ldlm_namespace *ns = obd->obd_namespace;
2679                 struct lu_env *env;
2680                 __u16 refcheck;
2681
2682                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2683
2684                 env = cl_env_get(&refcheck);
2685                 if (!IS_ERR(env)) {
2686                         /* Reset grants. All pages go to failing rpcs due to
2687                          * the invalid import.
2688                          */
2689                         osc_io_unplug(env, cli, NULL);
2690
2691                         cfs_hash_for_each_nolock(ns->ns_rs_hash,
2692                                                  osc_ldlm_resource_invalidate,
2693                                                  env, 0);
2694                         cl_env_put(env, &refcheck);
2695                         ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2696                 } else {
2697                         rc = PTR_ERR(env);
2698                 }
2699                 break;
2700         }
2701         case IMP_EVENT_ACTIVE:
2702                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE);
2703                 /* redo the kuc registration after reconnecting */
2704                 if (rc == 0)
2705                         rc = mdc_kuc_reregister(imp);
2706                 break;
2707         case IMP_EVENT_OCD: {
2708                 struct obd_connect_data *ocd = &imp->imp_connect_data;
2709
2710                 if (OCD_HAS_FLAG(ocd, GRANT))
2711                         osc_init_grant(cli, ocd);
2712
2713                 md_init_ea_size(obd->obd_self_export, ocd->ocd_max_easize, 0);
2714                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD);
2715                 break;
2716         }
2717         case IMP_EVENT_DEACTIVATE:
2718         case IMP_EVENT_ACTIVATE:
2719                 break;
2720         default:
2721                 CERROR("Unknown import event %x\n", event);
2722                 LBUG();
2723         }
2724         RETURN(rc);
2725 }
2726
2727 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2728                   struct lu_fid *fid, struct md_op_data *op_data)
2729 {
2730         struct client_obd *cli = &exp->exp_obd->u.cli;
2731         int rc = -EIO;
2732
2733         ENTRY;
2734
2735         down_read(&cli->cl_seq_rwsem);
2736         if (cli->cl_seq)
2737                 rc = seq_client_alloc_fid(env, cli->cl_seq, fid);
2738         up_read(&cli->cl_seq_rwsem);
2739
2740         RETURN(rc);
2741 }
2742
2743 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2744 {
2745         struct client_obd *cli = &exp->exp_obd->u.cli;
2746         return &cli->cl_target_uuid;
2747 }
2748
2749 /**
2750  * Determine whether the lock can be canceled before replaying it during
2751  * recovery, non zero value will be return if the lock can be canceled,
2752  * or zero returned for not
2753  */
2754 static int mdc_cancel_weight(struct ldlm_lock *lock)
2755 {
2756         if (lock->l_resource->lr_type != LDLM_IBITS)
2757                 RETURN(0);
2758
2759         /* FIXME: if we ever get into a situation where there are too many
2760          * opened files with open locks on a single node, then we really
2761          * should replay these open locks to reget it */
2762         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2763                 RETURN(0);
2764
2765         /* Special case for DoM locks, cancel only unused and granted locks */
2766         if (ldlm_has_dom(lock) &&
2767             (lock->l_granted_mode != lock->l_req_mode ||
2768              osc_ldlm_weigh_ast(lock) != 0))
2769                 RETURN(0);
2770
2771         RETURN(1);
2772 }
2773
2774 static int mdc_resource_inode_free(struct ldlm_resource *res)
2775 {
2776         if (res->lr_lvb_inode)
2777                 res->lr_lvb_inode = NULL;
2778
2779         return 0;
2780 }
2781
2782 static struct ldlm_valblock_ops inode_lvbo = {
2783         .lvbo_free = mdc_resource_inode_free
2784 };
2785
2786 static int mdc_llog_init(struct obd_device *obd)
2787 {
2788         struct obd_llog_group   *olg = &obd->obd_olg;
2789         struct llog_ctxt        *ctxt;
2790         int                      rc;
2791
2792         ENTRY;
2793
2794         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2795                         &llog_client_ops);
2796         if (rc < 0)
2797                 RETURN(rc);
2798
2799         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2800         llog_initiator_connect(ctxt);
2801         llog_ctxt_put(ctxt);
2802
2803         RETURN(0);
2804 }
2805
2806 static void mdc_llog_finish(struct obd_device *obd)
2807 {
2808         struct llog_ctxt *ctxt;
2809
2810         ENTRY;
2811
2812         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2813         if (ctxt != NULL)
2814                 llog_cleanup(NULL, ctxt);
2815
2816         EXIT;
2817 }
2818
2819 int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2820 {
2821         int rc;
2822
2823         ENTRY;
2824
2825         rc = osc_setup_common(obd, cfg);
2826         if (rc < 0)
2827                 RETURN(rc);
2828
2829         rc = mdc_tunables_init(obd);
2830         if (rc)
2831                 GOTO(err_osc_cleanup, rc);
2832
2833         obd->u.cli.cl_dom_min_inline_repsize = MDC_DOM_DEF_INLINE_REPSIZE;
2834         obd->u.cli.cl_lsom_update = true;
2835
2836         ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2837
2838         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2839
2840         rc = mdc_llog_init(obd);
2841         if (rc) {
2842                 CERROR("%s: failed to setup llogging subsystems: rc = %d\n",
2843                        obd->obd_name, rc);
2844                 GOTO(err_llog_cleanup, rc);
2845         }
2846
2847         rc = mdc_changelog_cdev_init(obd);
2848         if (rc) {
2849                 CERROR("%s: failed to setup changelog char device: rc = %d\n",
2850                        obd->obd_name, rc);
2851                 GOTO(err_changelog_cleanup, rc);
2852         }
2853
2854         RETURN(rc);
2855
2856 err_changelog_cleanup:
2857         mdc_llog_finish(obd);
2858 err_llog_cleanup:
2859         lprocfs_free_md_stats(obd);
2860         ptlrpc_lprocfs_unregister_obd(obd);
2861 err_osc_cleanup:
2862         osc_cleanup_common(obd);
2863         return rc;
2864 }
2865
2866 /* Initialize the default and maximum LOV EA sizes.  This allows
2867  * us to make MDS RPCs with large enough reply buffers to hold a default
2868  * sized EA without having to calculate this (via a call into the
2869  * LOV + OSCs) each time we make an RPC.  The maximum size is also tracked
2870  * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2871  * a large number of stripes is possible.  If a larger reply buffer is
2872  * required it will be reallocated in the ptlrpc layer due to overflow.
2873  */
2874 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2875                             __u32 def_easize)
2876 {
2877         struct obd_device *obd = exp->exp_obd;
2878         struct client_obd *cli = &obd->u.cli;
2879         ENTRY;
2880
2881         if (cli->cl_max_mds_easize < easize)
2882                 cli->cl_max_mds_easize = easize;
2883
2884         if (cli->cl_default_mds_easize < def_easize)
2885                 cli->cl_default_mds_easize = def_easize;
2886
2887         RETURN(0);
2888 }
2889
2890 static int mdc_precleanup(struct obd_device *obd)
2891 {
2892         ENTRY;
2893
2894         osc_precleanup_common(obd);
2895         mdc_changelog_cdev_finish(obd);
2896
2897         obd_cleanup_client_import(obd);
2898         ptlrpc_lprocfs_unregister_obd(obd);
2899         lprocfs_free_md_stats(obd);
2900         mdc_llog_finish(obd);
2901         RETURN(0);
2902 }
2903
2904 static int mdc_cleanup(struct obd_device *obd)
2905 {
2906         return osc_cleanup_common(obd);
2907 }
2908
2909 static const struct obd_ops mdc_obd_ops = {
2910         .o_owner            = THIS_MODULE,
2911         .o_setup            = mdc_setup,
2912         .o_precleanup       = mdc_precleanup,
2913         .o_cleanup          = mdc_cleanup,
2914         .o_add_conn         = client_import_add_conn,
2915         .o_del_conn         = client_import_del_conn,
2916         .o_connect          = client_connect_import,
2917         .o_reconnect        = osc_reconnect,
2918         .o_disconnect       = osc_disconnect,
2919         .o_iocontrol        = mdc_iocontrol,
2920         .o_set_info_async   = mdc_set_info_async,
2921         .o_statfs           = mdc_statfs,
2922         .o_statfs_async     = mdc_statfs_async,
2923         .o_fid_init         = client_fid_init,
2924         .o_fid_fini         = client_fid_fini,
2925         .o_fid_alloc        = mdc_fid_alloc,
2926         .o_import_event     = mdc_import_event,
2927         .o_get_info         = mdc_get_info,
2928         .o_get_uuid         = mdc_get_uuid,
2929         .o_quotactl         = mdc_quotactl,
2930 };
2931
2932 static const struct md_ops mdc_md_ops = {
2933         .m_get_root         = mdc_get_root,
2934         .m_null_inode       = mdc_null_inode,
2935         .m_close            = mdc_close,
2936         .m_create           = mdc_create,
2937         .m_enqueue          = mdc_enqueue,
2938         .m_getattr          = mdc_getattr,
2939         .m_getattr_name     = mdc_getattr_name,
2940         .m_intent_lock      = mdc_intent_lock,
2941         .m_link             = mdc_link,
2942         .m_rename           = mdc_rename,
2943         .m_setattr          = mdc_setattr,
2944         .m_setxattr         = mdc_setxattr,
2945         .m_getxattr         = mdc_getxattr,
2946         .m_fsync                = mdc_fsync,
2947         .m_file_resync          = mdc_file_resync,
2948         .m_read_page            = mdc_read_page,
2949         .m_unlink           = mdc_unlink,
2950         .m_cancel_unused    = mdc_cancel_unused,
2951         .m_init_ea_size     = mdc_init_ea_size,
2952         .m_set_lock_data    = mdc_set_lock_data,
2953         .m_lock_match       = mdc_lock_match,
2954         .m_get_lustre_md    = mdc_get_lustre_md,
2955         .m_free_lustre_md   = mdc_free_lustre_md,
2956         .m_set_open_replay_data = mdc_set_open_replay_data,
2957         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2958         .m_intent_getattr_async = mdc_intent_getattr_async,
2959         .m_revalidate_lock      = mdc_revalidate_lock,
2960         .m_rmfid                = mdc_rmfid,
2961 };
2962
2963 dev_t mdc_changelog_dev;
2964 struct class *mdc_changelog_class;
2965 static int __init mdc_init(void)
2966 {
2967         int rc = 0;
2968         rc = alloc_chrdev_region(&mdc_changelog_dev, 0,
2969                                  MDC_CHANGELOG_DEV_COUNT,
2970                                  MDC_CHANGELOG_DEV_NAME);
2971         if (rc)
2972                 return rc;
2973
2974         mdc_changelog_class = class_create(THIS_MODULE, MDC_CHANGELOG_DEV_NAME);
2975         if (IS_ERR(mdc_changelog_class)) {
2976                 rc = PTR_ERR(mdc_changelog_class);
2977                 goto out_dev;
2978         }
2979
2980         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, true,
2981                                  LUSTRE_MDC_NAME, &mdc_device_type);
2982         if (rc)
2983                 goto out_class;
2984
2985         return 0;
2986
2987 out_class:
2988         class_destroy(mdc_changelog_class);
2989 out_dev:
2990         unregister_chrdev_region(mdc_changelog_dev, MDC_CHANGELOG_DEV_COUNT);
2991         return rc;
2992 }
2993
2994 static void __exit mdc_exit(void)
2995 {
2996         class_unregister_type(LUSTRE_MDC_NAME);
2997         class_destroy(mdc_changelog_class);
2998         unregister_chrdev_region(mdc_changelog_dev, MDC_CHANGELOG_DEV_COUNT);
2999         idr_destroy(&mdc_changelog_minor_idr);
3000 }
3001
3002 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3003 MODULE_DESCRIPTION("Lustre Metadata Client");
3004 MODULE_VERSION(LUSTRE_VERSION_STRING);
3005 MODULE_LICENSE("GPL");
3006
3007 module_init(mdc_init);
3008 module_exit(mdc_exit);