4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_MDC
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>
45 #include <lustre_errno.h>
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>
61 #include "mdc_internal.h"
63 #define REQUEST_MINOR 244
65 static int mdc_cleanup(struct obd_device *obd);
67 static inline int mdc_queue_wait(struct ptlrpc_request *req)
69 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
72 /* obd_get_request_slot() ensures that this client has no more
73 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
75 rc = obd_get_request_slot(cli);
79 rc = ptlrpc_queue_wait(req);
80 obd_put_request_slot(cli);
86 * Send MDS_GET_ROOT RPC to fetch root FID.
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.
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
97 * \retval 0 on success, negative errno on failure
99 static int mdc_get_root(struct obd_export *exp, const char *fileset,
100 struct lu_fid *rootfid)
102 struct ptlrpc_request *req;
103 struct mdt_body *body;
108 if (fileset && !(exp_connect_flags(exp) & OBD_CONNECT_SUBTREE))
111 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
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);
121 ptlrpc_request_free(req);
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);
128 memcpy(name, fileset, strlen(fileset));
130 lustre_msg_add_flags(req->rq_reqmsg, LUSTRE_IMP_FULL);
131 req->rq_send_state = LUSTRE_IMP_FULL;
133 ptlrpc_request_set_replen(req);
135 rc = ptlrpc_queue_wait(req);
139 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
141 GOTO(out, rc = -EPROTO);
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));
148 ptlrpc_req_finished(req);
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
163 static int mdc_getattr_common(struct obd_export *exp,
164 struct ptlrpc_request *req)
166 struct req_capsule *pill = &req->rq_pill;
167 struct mdt_body *body;
172 /* Request message already built. */
173 rc = ptlrpc_queue_wait(req);
177 /* sanity check for the reply */
178 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
182 CDEBUG(D_NET, "mode: %o\n", body->mbo_mode);
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);
195 static void mdc_reset_acl_req(struct ptlrpc_request *req)
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);
206 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
207 struct ptlrpc_request **request)
209 struct ptlrpc_request *req;
210 struct obd_import *imp = class_exp2cliimp(exp);
211 __u32 acl_bufsize = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
215 /* Single MDS without an LMV case */
216 if (op_data->op_flags & MF_GET_MDT_IDX) {
222 req = ptlrpc_request_alloc(imp, &RQF_MDS_GETATTR);
226 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
228 ptlrpc_request_free(req);
232 /* LU-15245: avoid deadlock with modifying RPCs on MDS_REQUEST_PORTAL */
233 req->rq_request_portal = MDS_READPAGE_PORTAL;
236 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, op_data->op_valid,
237 op_data->op_mode, -1, 0);
238 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, acl_bufsize);
239 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
241 ptlrpc_request_set_replen(req);
243 rc = mdc_getattr_common(exp, req);
246 acl_bufsize = min_t(__u32,
247 imp->imp_connect_data.ocd_max_easize,
249 mdc_reset_acl_req(req);
253 ptlrpc_req_finished(req);
261 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
262 struct ptlrpc_request **request)
264 struct ptlrpc_request *req;
265 struct obd_import *imp = class_exp2cliimp(exp);
266 __u32 acl_bufsize = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
271 req = ptlrpc_request_alloc(imp, &RQF_MDS_GETATTR_NAME);
275 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
276 op_data->op_namelen + 1);
278 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
280 ptlrpc_request_free(req);
284 if (op_data->op_name) {
285 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
286 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
287 op_data->op_namelen);
288 memcpy(name, op_data->op_name, op_data->op_namelen);
292 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, op_data->op_valid,
293 op_data->op_mode, op_data->op_suppgids[0], 0);
294 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
296 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER, acl_bufsize);
297 ptlrpc_request_set_replen(req);
298 if (op_data->op_bias & MDS_FID_OP) {
299 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
303 b->mbo_valid |= OBD_MD_NAMEHASH;
304 b->mbo_fid2 = op_data->op_fid2;
308 rc = mdc_getattr_common(exp, req);
311 acl_bufsize = min_t(__u32,
312 imp->imp_connect_data.ocd_max_easize,
314 mdc_reset_acl_req(req);
318 ptlrpc_req_finished(req);
326 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
327 const struct lu_fid *fid, int opcode, u64 valid,
328 const char *xattr_name, const char *input,
329 int input_size, int output_size, int flags,
330 __u32 suppgid, struct ptlrpc_request **request)
332 struct ptlrpc_request *req;
333 int xattr_namelen = 0;
339 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
344 xattr_namelen = strlen(xattr_name) + 1;
345 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
350 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
353 /* get SELinux policy info if any */
354 rc = sptlrpc_get_sepol(req);
356 ptlrpc_request_free(req);
359 req_capsule_set_size(&req->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
360 strlen(req->rq_sepol) ?
361 strlen(req->rq_sepol) + 1 : 0);
363 /* Flush local XATTR locks to get rid of a possible cancel RPC */
364 if (opcode == MDS_REINT && fid_is_sane(fid) &&
365 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
369 /* Without that packing would fail */
371 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
374 count = mdc_resource_get_unused(exp, fid,
376 MDS_INODELOCK_XATTR);
378 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
380 ptlrpc_request_free(req);
384 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
386 ptlrpc_request_free(req);
391 if (opcode == MDS_REINT) {
392 struct mdt_rec_setxattr *rec;
394 BUILD_BUG_ON(sizeof(struct mdt_rec_setxattr) !=
395 sizeof(struct mdt_rec_reint));
396 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
397 rec->sx_opcode = REINT_SETXATTR;
398 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
399 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
400 rec->sx_cap = current_cap().cap[0];
401 rec->sx_suppgid1 = suppgid;
402 rec->sx_suppgid2 = -1;
404 rec->sx_valid = valid | OBD_MD_FLCTIME;
405 rec->sx_time = ktime_get_real_seconds();
406 rec->sx_size = output_size;
407 rec->sx_flags = flags;
409 mdc_pack_body(&req->rq_pill, fid, valid, output_size,
411 /* Avoid deadlock with modifying RPCs on MDS_REQUEST_PORTAL.
414 req->rq_request_portal = MDS_READPAGE_PORTAL;
418 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
419 memcpy(tmp, xattr_name, xattr_namelen);
422 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
423 memcpy(tmp, input, input_size);
426 mdc_file_sepol_pack(&req->rq_pill);
428 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
429 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
430 RCL_SERVER, output_size);
431 ptlrpc_request_set_replen(req);
434 if (opcode == MDS_REINT)
435 ptlrpc_get_mod_rpc_slot(req);
437 rc = ptlrpc_queue_wait(req);
439 if (opcode == MDS_REINT)
440 ptlrpc_put_mod_rpc_slot(req);
443 ptlrpc_req_finished(req);
449 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
450 u64 obd_md_valid, const char *name,
451 const void *value, size_t value_size,
452 unsigned int xattr_flags, u32 suppgid,
453 struct ptlrpc_request **req)
455 LASSERT(obd_md_valid == OBD_MD_FLXATTR ||
456 obd_md_valid == OBD_MD_FLXATTRRM);
458 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
459 fid, MDS_REINT, obd_md_valid, name,
460 value, value_size, 0, xattr_flags, suppgid,
464 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
465 u64 obd_md_valid, const char *name, size_t buf_size,
466 struct ptlrpc_request **req)
468 struct mdt_body *body;
471 LASSERT(obd_md_valid == OBD_MD_FLXATTR ||
472 obd_md_valid == OBD_MD_FLXATTRLS);
474 /* Message below is checked in sanity-selinux test_20d
475 * and sanity-sec test_49
477 CDEBUG(D_INFO, "%s: get xattr '%s' for "DFID"\n",
478 exp->exp_obd->obd_name, name, PFID(fid));
479 rc = mdc_xattr_common(exp, &RQF_MDS_GETXATTR, fid, MDS_GETXATTR,
480 obd_md_valid, name, NULL, 0, buf_size, 0, -1,
485 body = req_capsule_server_get(&(*req)->rq_pill, &RMF_MDT_BODY);
487 GOTO(out, rc = -EPROTO);
489 /* only detect the xattr size */
491 /* LU-11109: Older MDTs do not distinguish
492 * between nonexistent xattrs and zero length
493 * values in this case. Newer MDTs will return
494 * -ENODATA or set OBD_MD_FLXATTR. */
495 GOTO(out, rc = body->mbo_eadatasize);
498 if (body->mbo_eadatasize == 0) {
499 /* LU-11109: Newer MDTs set OBD_MD_FLXATTR on
500 * success so that we can distinguish between
501 * zero length value and nonexistent xattr.
503 * If OBD_MD_FLXATTR is not set then we keep
504 * the old behavior and return -ENODATA for
505 * getxattr() when mbo_eadatasize is 0. But
506 * -ENODATA only makes sense for getxattr()
507 * and not for listxattr(). */
508 if (body->mbo_valid & OBD_MD_FLXATTR)
510 else if (obd_md_valid == OBD_MD_FLXATTR)
511 GOTO(out, rc = -ENODATA);
516 GOTO(out, rc = body->mbo_eadatasize);
519 ptlrpc_req_finished(*req);
526 static int mdc_get_lustre_md(struct obd_export *exp, struct req_capsule *pill,
527 struct obd_export *dt_exp,
528 struct obd_export *md_exp,
529 struct lustre_md *md)
535 memset(md, 0, sizeof(*md));
537 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
538 LASSERT(md->body != NULL);
540 if (md->body->mbo_valid & OBD_MD_FLEASIZE) {
541 if (!S_ISREG(md->body->mbo_mode)) {
542 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
543 "regular file, but is not\n");
544 GOTO(out, rc = -EPROTO);
547 if (md->body->mbo_eadatasize == 0) {
548 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
549 "but eadatasize 0\n");
550 GOTO(out, rc = -EPROTO);
553 md->layout.lb_len = md->body->mbo_eadatasize;
554 md->layout.lb_buf = req_capsule_server_sized_get(pill,
557 if (md->layout.lb_buf == NULL)
558 GOTO(out, rc = -EPROTO);
559 } else if (md->body->mbo_valid & OBD_MD_FLDIREA) {
560 const union lmv_mds_md *lmv;
563 if (!S_ISDIR(md->body->mbo_mode)) {
564 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
565 "directory, but is not\n");
566 GOTO(out, rc = -EPROTO);
569 if (md_exp->exp_obd->obd_type->typ_lu == &mdc_device_type) {
570 CERROR("%s: no LMV, upgrading from old version?\n",
571 md_exp->exp_obd->obd_name);
573 GOTO(out_acl, rc = 0);
576 if (md->body->mbo_valid & OBD_MD_MEA) {
577 lmv_size = md->body->mbo_eadatasize;
579 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
580 "but eadatasize 0\n");
584 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
587 GOTO(out, rc = -EPROTO);
589 rc = md_unpackmd(md_exp, &md->lmv, lmv, lmv_size);
593 if (rc < (int)sizeof(*md->lmv)) {
594 struct lmv_foreign_md *lfm = md->lfm;
596 /* short (< sizeof(struct lmv_stripe_md))
599 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
601 "lmv size too small: %d < %d\n",
602 rc, (int)sizeof(*md->lmv));
603 GOTO(out, rc = -EPROTO);
608 /* since 2.12.58 intent_getattr fetches default LMV */
609 if (md->body->mbo_valid & OBD_MD_DEFAULT_MEA) {
610 lmv_size = sizeof(struct lmv_user_md);
611 lmv = req_capsule_server_sized_get(pill,
615 GOTO(out, rc = -EPROTO);
617 rc = md_unpackmd(md_exp, &md->default_lmv, lmv,
622 if (rc < (int)sizeof(*md->default_lmv)) {
624 "default lmv size too small: %d < %d\n",
625 rc, (int)sizeof(*md->default_lmv));
626 GOTO(out, rc = -EPROTO);
633 if (md->body->mbo_valid & OBD_MD_FLACL) {
634 /* for ACL, it's possible that FLACL is set but aclsize is zero.
635 * only when aclsize != 0 there's an actual segment for ACL
638 rc = mdc_unpack_acl(pill, md);
651 static int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
657 void mdc_replay_open(struct ptlrpc_request *req)
659 struct md_open_data *mod = req->rq_cb_data;
660 struct ptlrpc_request *close_req;
661 struct obd_client_handle *och;
662 struct lustre_handle old_open_handle = { };
663 struct mdt_body *body;
664 struct ldlm_reply *rep;
668 DEBUG_REQ(D_ERROR, req,
669 "cannot properly replay without open data");
674 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
675 LASSERT(body != NULL);
677 rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
678 if (rep != NULL && rep->lock_policy_res2 != 0)
679 DEBUG_REQ(D_ERROR, req, "Open request replay failed with %ld ",
680 (long int)rep->lock_policy_res2);
682 spin_lock(&req->rq_lock);
684 if (och && och->och_open_handle.cookie)
685 req->rq_early_free_repbuf = 1;
687 req->rq_early_free_repbuf = 0;
688 spin_unlock(&req->rq_lock);
690 if (req->rq_early_free_repbuf) {
691 struct lustre_handle *file_open_handle;
693 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
695 file_open_handle = &och->och_open_handle;
696 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
697 file_open_handle->cookie, body->mbo_open_handle.cookie);
698 old_open_handle = *file_open_handle;
699 *file_open_handle = body->mbo_open_handle;
702 close_req = mod->mod_close_req;
704 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
705 struct mdt_ioepoch *epoch;
707 LASSERT(opc == MDS_CLOSE);
708 epoch = req_capsule_client_get(&close_req->rq_pill,
712 if (req->rq_early_free_repbuf)
713 LASSERT(old_open_handle.cookie ==
714 epoch->mio_open_handle.cookie);
716 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
717 epoch->mio_open_handle = body->mbo_open_handle;
722 void mdc_commit_open(struct ptlrpc_request *req)
724 struct md_open_data *mod = req->rq_cb_data;
729 * No need to touch md_open_data::mod_och, it holds a reference on
730 * \var mod and will zero references to each other, \var mod will be
731 * freed after that when md_open_data::mod_och will put the reference.
735 * Do not let open request to disappear as it still may be needed
736 * for close rpc to happen (it may happen on evict only, otherwise
737 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
738 * called), just mark this rpc as committed to distinguish these 2
739 * cases, see mdc_close() for details. The open request reference will
740 * be put along with freeing \var mod.
742 ptlrpc_request_addref(req);
743 spin_lock(&req->rq_lock);
744 req->rq_committed = 1;
745 spin_unlock(&req->rq_lock);
746 req->rq_cb_data = NULL;
750 int mdc_set_open_replay_data(struct obd_export *exp,
751 struct obd_client_handle *och,
752 struct lookup_intent *it)
754 struct md_open_data *mod;
755 struct mdt_rec_create *rec;
756 struct mdt_body *body;
757 struct ptlrpc_request *open_req = it->it_request;
758 struct obd_import *imp = open_req->rq_import;
761 if (!open_req->rq_replay)
764 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
765 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
766 LASSERT(rec != NULL);
767 /* Incoming message in my byte order (it's been swabbed). */
768 /* Outgoing messages always in my byte order. */
769 LASSERT(body != NULL);
771 /* Only if the import is replayable, we set replay_open data */
772 if (och && imp->imp_replayable) {
773 mod = obd_mod_alloc();
775 DEBUG_REQ(D_ERROR, open_req,
776 "cannot allocate md_open_data");
781 * Take a reference on \var mod, to be freed on mdc_close().
782 * It protects \var mod from being freed on eviction (commit
783 * callback is called despite rq_replay flag).
784 * Another reference for \var och.
789 spin_lock(&open_req->rq_lock);
792 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
793 it_disposition(it, DISP_OPEN_STRIPE);
794 mod->mod_open_req = open_req;
795 open_req->rq_cb_data = mod;
796 open_req->rq_commit_cb = mdc_commit_open;
797 open_req->rq_early_free_repbuf = 1;
798 spin_unlock(&open_req->rq_lock);
801 rec->cr_fid2 = body->mbo_fid1;
802 rec->cr_open_handle_old = body->mbo_open_handle;
803 open_req->rq_replay_cb = mdc_replay_open;
804 if (!fid_is_sane(&body->mbo_fid1)) {
805 DEBUG_REQ(D_ERROR, open_req,
806 "saving replay request with insane FID " DFID,
807 PFID(&body->mbo_fid1));
811 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
815 static void mdc_free_open(struct md_open_data *mod)
819 if (mod->mod_is_create == 0 &&
820 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
824 * No reason to asssert here if the open request has
825 * rq_replay == 1. It means that mdc_close failed, and
826 * close request wasn`t sent. It is not fatal to client.
827 * The worst thing is eviction if the client gets open lock
830 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req,
831 "free open request, rq_replay=%d",
832 mod->mod_open_req->rq_replay);
834 ptlrpc_request_committed(mod->mod_open_req, committed);
835 if (mod->mod_close_req)
836 ptlrpc_request_committed(mod->mod_close_req, committed);
839 static int mdc_clear_open_replay_data(struct obd_export *exp,
840 struct obd_client_handle *och)
842 struct md_open_data *mod = och->och_mod;
846 * It is possible to not have \var mod in a case of eviction between
847 * lookup and ll_file_open().
852 LASSERT(mod != LP_POISON);
853 LASSERT(mod->mod_open_req != NULL);
855 spin_lock(&mod->mod_open_req->rq_lock);
857 mod->mod_och->och_open_handle.cookie = 0;
858 mod->mod_open_req->rq_early_free_repbuf = 0;
859 spin_unlock(&mod->mod_open_req->rq_lock);
869 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
870 struct md_open_data *mod, struct ptlrpc_request **request)
872 struct obd_device *obd = class_exp2obd(exp);
873 struct ptlrpc_request *req;
874 struct req_format *req_fmt;
875 size_t u32_count = 0;
880 CDEBUG(D_INODE, "%s: "DFID" file closed with intent: %x\n",
881 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
884 if (op_data->op_bias & MDS_CLOSE_INTENT) {
885 req_fmt = &RQF_MDS_CLOSE_INTENT;
886 if (op_data->op_bias & MDS_HSM_RELEASE) {
887 /* allocate a FID for volatile file */
888 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2,
891 CERROR("%s: "DFID" allocating FID: rc = %d\n",
892 obd->obd_name, PFID(&op_data->op_fid1),
894 /* save the errcode and proceed to close */
898 if (op_data->op_bias & MDS_CLOSE_RESYNC_DONE) {
899 size_t count = op_data->op_data_size / sizeof(__u32);
901 if (count > INLINE_RESYNC_ARRAY_SIZE)
905 req_fmt = &RQF_MDS_CLOSE;
909 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_CLOSE))
912 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
914 /* Ensure that this close's handle is fixed up during replay. */
915 if (likely(mod != NULL)) {
916 LASSERTF(mod->mod_open_req != NULL &&
917 mod->mod_open_req->rq_type != LI_POISON,
918 "POISONED open %p!\n", mod->mod_open_req);
920 mod->mod_close_req = req;
922 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "matched open");
923 /* We no longer want to preserve this open for replay even
924 * though the open was committed. b=3632, b=3633 */
925 spin_lock(&mod->mod_open_req->rq_lock);
926 mod->mod_open_req->rq_replay = 0;
927 spin_unlock(&mod->mod_open_req->rq_lock);
929 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
933 * TODO: repeat close after errors
935 CWARN("%s: close of FID "DFID" failed, file reference will be "
936 "dropped when this client unmounts or is evicted\n",
937 obd->obd_name, PFID(&op_data->op_fid1));
938 GOTO(out, rc = -ENOMEM);
942 req_capsule_set_size(&req->rq_pill, &RMF_U32, RCL_CLIENT,
943 u32_count * sizeof(__u32));
945 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
947 ptlrpc_request_free(req);
952 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
953 * portal whose threads are not taking any DLM locks and are therefore
954 * always progressing */
955 req->rq_request_portal = MDS_READPAGE_PORTAL;
956 ptlrpc_at_set_req_timeout(req);
958 if (!obd->u.cli.cl_lsom_update ||
959 !(exp_connect_flags2(exp) & OBD_CONNECT2_LSOM))
960 op_data->op_xvalid &= ~(OP_XVALID_LAZYSIZE |
961 OP_XVALID_LAZYBLOCKS);
963 mdc_close_pack(&req->rq_pill, op_data);
965 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
966 obd->u.cli.cl_default_mds_easize);
968 ptlrpc_request_set_replen(req);
970 ptlrpc_get_mod_rpc_slot(req);
971 rc = ptlrpc_queue_wait(req);
972 ptlrpc_put_mod_rpc_slot(req);
974 if (req->rq_repmsg == NULL) {
975 CDEBUG(D_RPCTRACE, "request %p failed to send: rc = %d\n", req,
978 rc = req->rq_status ?: -EIO;
979 } else if (rc == 0 || rc == -EAGAIN) {
980 struct mdt_body *body;
982 rc = lustre_msg_get_status(req->rq_repmsg);
983 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
984 DEBUG_REQ(D_ERROR, req,
985 "type = PTL_RPC_MSG_ERR: rc = %d", rc);
989 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
992 } else if (rc == -ESTALE) {
994 * it can be allowed error after 3633 if open was committed and
995 * server failed before close was sent. Let's check if mod
996 * exists and return no error in that case
999 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
1000 LASSERT(mod->mod_open_req != NULL);
1001 if (mod->mod_open_req->rq_committed)
1009 mod->mod_close_req = NULL;
1010 /* Since now, mod is accessed through open_req only,
1011 * thus close req does not keep a reference on mod anymore. */
1016 RETURN(rc < 0 ? rc : saved_rc);
1019 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
1020 u64 offset, struct page **pages, int npages,
1021 struct ptlrpc_request **request)
1023 struct ptlrpc_request *req;
1024 struct ptlrpc_bulk_desc *desc;
1033 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1037 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1039 ptlrpc_request_free(req);
1043 req->rq_request_portal = MDS_READPAGE_PORTAL;
1044 ptlrpc_at_set_req_timeout(req);
1046 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1047 PTLRPC_BULK_PUT_SINK,
1049 &ptlrpc_bulk_kiov_pin_ops);
1051 ptlrpc_req_finished(req);
1055 /* NB req now owns desc and will free it when it gets freed */
1056 for (i = 0; i < npages; i++)
1057 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1060 mdc_readdir_pack(&req->rq_pill, offset, PAGE_SIZE * npages, fid);
1062 ptlrpc_request_set_replen(req);
1063 rc = ptlrpc_queue_wait(req);
1065 ptlrpc_req_finished(req);
1066 if (rc != -ETIMEDOUT)
1070 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1071 CERROR("%s: too many resend retries: rc = %d\n",
1072 exp->exp_obd->obd_name, -EIO);
1076 /* If a signal interrupts then the timeout returned will
1077 * not be zero. In that case return -EINTR
1079 if (msleep_interruptible(resends * 1000))
1085 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1086 req->rq_bulk->bd_nob_transferred);
1088 ptlrpc_req_finished(req);
1092 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1093 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1094 exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1095 PAGE_SIZE * npages);
1096 ptlrpc_req_finished(req);
1104 static void mdc_release_page(struct page *page, int remove)
1108 if (likely(page->mapping != NULL))
1109 delete_from_page_cache(page);
1115 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1116 __u64 *start, __u64 *end, int hash64)
1119 * Complement of hash is used as an index so that
1120 * radix_tree_gang_lookup() can be used to find a page with starting
1121 * hash _smaller_ than one we are looking for.
1123 unsigned long offset = hash_x_index(*hash, hash64);
1125 unsigned long flags;
1128 ll_xa_lock_irqsave(&mapping->i_pages, flags);
1129 found = radix_tree_gang_lookup(&mapping->page_tree,
1130 (void **)&page, offset, 1);
1131 if (found > 0 && !ll_xa_is_value(page)) {
1132 struct lu_dirpage *dp;
1135 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1137 * In contrast to find_lock_page() we are sure that directory
1138 * page cannot be truncated (while DLM lock is held) and,
1139 * hence, can avoid restart.
1141 * In fact, page cannot be locked here at all, because
1142 * mdc_read_page_remote does synchronous io.
1144 wait_on_page_locked(page);
1145 if (PageUptodate(page)) {
1147 if (BITS_PER_LONG == 32 && hash64) {
1148 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1149 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1150 *hash = *hash >> 32;
1152 *start = le64_to_cpu(dp->ldp_hash_start);
1153 *end = le64_to_cpu(dp->ldp_hash_end);
1155 if (unlikely(*start == 1 && *hash == 0))
1158 LASSERTF(*start <= *hash, "start = %#llx"
1159 ",end = %#llx,hash = %#llx\n",
1160 *start, *end, *hash);
1161 CDEBUG(D_VFSTRACE, "offset %lx [%#llx %#llx],"
1162 " hash %#llx\n", offset, *start, *end, *hash);
1165 mdc_release_page(page, 0);
1167 } else if (*end != *start && *hash == *end) {
1169 * upon hash collision, remove this page,
1170 * otherwise put page reference, and
1171 * mdc_read_page_remote() will issue RPC to
1172 * fetch the page we want.
1175 mdc_release_page(page,
1176 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1181 page = ERR_PTR(-EIO);
1184 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1191 * Adjust a set of pages, each page containing an array of lu_dirpages,
1192 * so that each page can be used as a single logical lu_dirpage.
1194 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1195 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1196 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
1197 * value is used as a cookie to request the next lu_dirpage in a
1198 * directory listing that spans multiple pages (two in this example):
1201 * .|--------v------- -----.
1202 * |s|e|f|p|ent|ent| ... |ent|
1203 * '--|-------------- -----' Each PAGE contains a single
1204 * '------. lu_dirpage.
1205 * .---------v------- -----.
1206 * |s|e|f|p|ent| 0 | ... | 0 |
1207 * '----------------- -----'
1209 * However, on hosts where the native VM page size (PAGE_SIZE) is
1210 * larger than LU_PAGE_SIZE, a single host page may contain multiple
1211 * lu_dirpages. After reading the lu_dirpages from the MDS, the
1212 * ldp_hash_end of the first lu_dirpage refers to the one immediately
1213 * after it in the same PAGE (arrows simplified for brevity, but
1214 * in general e0==s1, e1==s2, etc.):
1216 * .-------------------- -----.
1217 * |s0|e0|f0|p|ent|ent| ... |ent|
1218 * |---v---------------- -----|
1219 * |s1|e1|f1|p|ent|ent| ... |ent|
1220 * |---v---------------- -----| Here, each PAGE contains
1221 * ... multiple lu_dirpages.
1222 * |---v---------------- -----|
1223 * |s'|e'|f'|p|ent|ent| ... |ent|
1224 * '---|---------------- -----'
1226 * .----------------------------.
1229 * This structure is transformed into a single logical lu_dirpage as follows:
1231 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1232 * labeled 'next PAGE'.
1234 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1235 * a hash collision with the next page exists.
1237 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1238 * to the first entry of the next lu_dirpage.
1240 #if PAGE_SIZE > LU_PAGE_SIZE
1241 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1245 for (i = 0; i < cfs_pgs; i++) {
1246 struct lu_dirpage *dp = kmap(pages[i]);
1247 struct lu_dirpage *first = dp;
1248 struct lu_dirent *end_dirent = NULL;
1249 struct lu_dirent *ent;
1250 __u64 hash_end = dp->ldp_hash_end;
1251 __u32 flags = dp->ldp_flags;
1253 while (--lu_pgs > 0) {
1254 ent = lu_dirent_start(dp);
1255 for (end_dirent = ent; ent != NULL;
1256 end_dirent = ent, ent = lu_dirent_next(ent));
1258 /* Advance dp to next lu_dirpage. */
1259 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1261 /* Check if we've reached the end of the PAGE. */
1262 if (!((unsigned long)dp & ~PAGE_MASK))
1265 /* Save the hash and flags of this lu_dirpage. */
1266 hash_end = dp->ldp_hash_end;
1267 flags = dp->ldp_flags;
1269 /* Check if lu_dirpage contains no entries. */
1270 if (end_dirent == NULL)
1273 /* Enlarge the end entry lde_reclen from 0 to
1274 * first entry of next lu_dirpage. */
1275 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1276 end_dirent->lde_reclen =
1277 cpu_to_le16((char *)(dp->ldp_entries) -
1278 (char *)end_dirent);
1281 first->ldp_hash_end = hash_end;
1282 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1283 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1287 LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1290 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1291 #endif /* PAGE_SIZE > LU_PAGE_SIZE */
1293 /* parameters for readdir page */
1294 struct readpage_param {
1295 struct md_op_data *rp_mod;
1298 struct obd_export *rp_exp;
1302 * Read pages from server.
1304 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1305 * a header lu_dirpage which describes the start/end hash, and whether this
1306 * page is empty (contains no dir entry) or hash collide with next page.
1307 * After client receives reply, several pages will be integrated into dir page
1308 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1309 * lu_dirpage for this integrated page will be adjusted.
1311 static int mdc_read_page_remote(void *data, struct page *page0)
1313 struct readpage_param *rp = data;
1314 struct page **page_pool;
1316 struct lu_dirpage *dp;
1317 struct md_op_data *op_data = rp->rp_mod;
1318 struct ptlrpc_request *req;
1320 struct inode *inode;
1322 int rd_pgs = 0; /* number of pages actually read */
1328 max_pages = rp->rp_exp->exp_obd->u.cli.cl_max_pages_per_rpc;
1329 inode = op_data->op_data;
1330 fid = &op_data->op_fid1;
1331 LASSERT(inode != NULL);
1333 OBD_ALLOC_PTR_ARRAY_LARGE(page_pool, max_pages);
1334 if (page_pool != NULL) {
1335 page_pool[0] = page0;
1341 for (npages = 1; npages < max_pages; npages++) {
1342 page = page_cache_alloc(inode->i_mapping);
1345 page_pool[npages] = page;
1348 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, page_pool, npages, &req);
1350 /* page0 is special, which was added into page cache early */
1351 delete_from_page_cache(page0);
1355 rd_pgs = (req->rq_bulk->bd_nob_transferred + PAGE_SIZE - 1) >>
1357 lu_pgs = req->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
1358 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1360 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1362 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1364 SetPageUptodate(page0);
1368 ptlrpc_req_finished(req);
1369 CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1370 for (i = 1; i < npages; i++) {
1371 unsigned long offset;
1375 page = page_pool[i];
1377 if (rc < 0 || i >= rd_pgs) {
1382 SetPageUptodate(page);
1385 hash = le64_to_cpu(dp->ldp_hash_start);
1388 offset = hash_x_index(hash, rp->rp_hash64);
1390 prefetchw(&page->flags);
1391 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1396 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1397 " rc = %d\n", offset, ret);
1401 if (page_pool != &page0)
1402 OBD_FREE_PTR_ARRAY_LARGE(page_pool, max_pages);
1408 * Read dir page from cache first, if it can not find it, read it from
1409 * server and add into the cache.
1411 * \param[in] exp MDC export
1412 * \param[in] op_data client MD stack parameters, transfering parameters
1413 * between different layers on client MD stack.
1414 * \param[in] mrinfo callback required for ldlm lock enqueue during
1416 * \param[in] hash_offset the hash offset of the page to be read
1417 * \param[in] ppage the page to be read
1419 * retval = 0 get the page successfully
1420 * errno(<0) get the page failed
1422 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1423 struct md_readdir_info *mrinfo, __u64 hash_offset,
1424 struct page **ppage)
1426 struct lookup_intent it = { .it_op = IT_READDIR };
1428 struct inode *dir = op_data->op_data;
1429 struct address_space *mapping;
1430 struct lu_dirpage *dp;
1433 struct lustre_handle lockh;
1434 struct ptlrpc_request *enq_req = NULL;
1435 struct readpage_param rp_param;
1442 LASSERT(dir != NULL);
1443 mapping = dir->i_mapping;
1445 rc = mdc_intent_lock(exp, op_data, &it, &enq_req,
1446 mrinfo->mr_blocking_ast, 0);
1447 if (enq_req != NULL)
1448 ptlrpc_req_finished(enq_req);
1451 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1452 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1457 lockh.cookie = it.it_lock_handle;
1458 mdc_set_lock_data(exp, &lockh, dir, NULL);
1460 rp_param.rp_off = hash_offset;
1461 rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1462 page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1463 rp_param.rp_hash64);
1465 CERROR("%s: dir page locate: "DFID" at %llu: rc %ld\n",
1466 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1467 rp_param.rp_off, PTR_ERR(page));
1468 GOTO(out_unlock, rc = PTR_ERR(page));
1469 } else if (page != NULL) {
1471 * XXX nikita: not entirely correct handling of a corner case:
1472 * suppose hash chain of entries with hash value HASH crosses
1473 * border between pages P0 and P1. First both P0 and P1 are
1474 * cached, seekdir() is called for some entry from the P0 part
1475 * of the chain. Later P0 goes out of cache. telldir(HASH)
1476 * happens and finds P1, as it starts with matching hash
1477 * value. Remaining entries from P0 part of the chain are
1478 * skipped. (Is that really a bug?)
1480 * Possible solutions: 0. don't cache P1 is such case, handle
1481 * it as an "overflow" page. 1. invalidate all pages at
1482 * once. 2. use HASH|1 as an index for P1.
1484 GOTO(hash_collision, page);
1487 rp_param.rp_exp = exp;
1488 rp_param.rp_mod = op_data;
1489 page = read_cache_page(mapping,
1490 hash_x_index(rp_param.rp_off,
1491 rp_param.rp_hash64),
1492 mdc_read_page_remote, &rp_param);
1494 CDEBUG(D_INFO, "%s: read cache page: "DFID" at %llu: %ld\n",
1495 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1496 rp_param.rp_off, PTR_ERR(page));
1497 GOTO(out_unlock, rc = PTR_ERR(page));
1500 wait_on_page_locked(page);
1502 if (!PageUptodate(page)) {
1503 CERROR("%s: page not updated: "DFID" at %llu: rc %d\n",
1504 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1505 rp_param.rp_off, -5);
1508 if (!PageChecked(page))
1509 SetPageChecked(page);
1510 if (PageError(page)) {
1511 CERROR("%s: page error: "DFID" at %llu: rc %d\n",
1512 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1513 rp_param.rp_off, -5);
1518 dp = page_address(page);
1519 if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1520 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1521 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1522 rp_param.rp_off = hash_offset >> 32;
1524 start = le64_to_cpu(dp->ldp_hash_start);
1525 end = le64_to_cpu(dp->ldp_hash_end);
1526 rp_param.rp_off = hash_offset;
1529 LASSERT(start == rp_param.rp_off);
1530 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1531 #if BITS_PER_LONG == 32
1532 CWARN("Real page-wide hash collision at [%llu %llu] with "
1533 "hash %llu\n", le64_to_cpu(dp->ldp_hash_start),
1534 le64_to_cpu(dp->ldp_hash_end), hash_offset);
1538 * Fetch whole overflow chain...
1546 ldlm_lock_decref(&lockh, it.it_lock_mode);
1550 mdc_release_page(page, 1);
1555 static int mdc_statfs_interpret(const struct lu_env *env,
1556 struct ptlrpc_request *req, void *args, int rc)
1558 struct obd_info *oinfo = args;
1559 struct obd_statfs *osfs;
1562 osfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1566 oinfo->oi_osfs = osfs;
1568 CDEBUG(D_CACHE, "blocks=%llu free=%llu avail=%llu "
1569 "objects=%llu free=%llu state=%x\n",
1570 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
1571 osfs->os_files, osfs->os_ffree, osfs->os_state);
1574 oinfo->oi_cb_up(oinfo, rc);
1579 static int mdc_statfs_async(struct obd_export *exp,
1580 struct obd_info *oinfo, time64_t max_age,
1581 struct ptlrpc_request_set *unused)
1583 struct ptlrpc_request *req;
1584 struct obd_info *aa;
1586 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_STATFS,
1587 LUSTRE_MDS_VERSION, MDS_STATFS);
1591 ptlrpc_request_set_replen(req);
1592 req->rq_interpret_reply = mdc_statfs_interpret;
1594 aa = ptlrpc_req_async_args(aa, req);
1597 ptlrpcd_add_req(req);
1602 static int mdc_statfs(const struct lu_env *env,
1603 struct obd_export *exp, struct obd_statfs *osfs,
1604 time64_t max_age, __u32 flags)
1606 struct obd_device *obd = class_exp2obd(exp);
1607 struct req_format *fmt;
1608 struct ptlrpc_request *req;
1609 struct obd_statfs *msfs;
1610 struct obd_import *imp, *imp0;
1615 * Since the request might also come from lprocfs, so we need
1616 * sync this with client_disconnect_export Bug15684
1618 with_imp_locked(obd, imp0, rc)
1619 imp = class_import_get(imp0);
1623 fmt = &RQF_MDS_STATFS;
1624 if ((exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS) &&
1625 (flags & OBD_STATFS_SUM))
1626 fmt = &RQF_MDS_STATFS_NEW;
1627 req = ptlrpc_request_alloc_pack(imp, fmt, LUSTRE_MDS_VERSION,
1630 GOTO(output, rc = -ENOMEM);
1631 req->rq_allow_intr = 1;
1633 if ((flags & OBD_STATFS_SUM) &&
1634 (exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS)) {
1635 /* request aggregated states */
1636 struct mdt_body *body;
1638 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1640 GOTO(out, rc = -EPROTO);
1641 body->mbo_valid = OBD_MD_FLAGSTATFS;
1644 ptlrpc_request_set_replen(req);
1646 if (flags & OBD_STATFS_NODELAY) {
1647 /* procfs requests not want stay in wait for avoid deadlock */
1648 req->rq_no_resend = 1;
1649 req->rq_no_delay = 1;
1652 rc = ptlrpc_queue_wait(req);
1654 /* check connection error first */
1655 if (imp->imp_connect_error)
1656 rc = imp->imp_connect_error;
1660 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1662 GOTO(out, rc = -EPROTO);
1667 ptlrpc_req_finished(req);
1669 class_import_put(imp);
1673 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1675 __u32 keylen, vallen;
1679 if (gf->gf_pathlen > PATH_MAX)
1680 RETURN(-ENAMETOOLONG);
1681 if (gf->gf_pathlen < 2)
1684 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1685 keylen = cfs_size_round(sizeof(KEY_FID2PATH) + sizeof(*gf) +
1686 sizeof(struct lu_fid));
1687 OBD_ALLOC(key, keylen);
1690 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1691 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1692 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf),
1693 gf->gf_u.gf_root_fid, sizeof(struct lu_fid));
1694 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1695 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1697 if (!fid_is_sane(&gf->gf_fid))
1698 GOTO(out, rc = -EINVAL);
1700 /* Val is struct getinfo_fid2path result plus path */
1701 vallen = sizeof(*gf) + gf->gf_pathlen;
1703 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf);
1704 if (rc != 0 && rc != -EREMOTE)
1707 if (vallen <= sizeof(*gf))
1708 GOTO(out, rc = -EPROTO);
1709 if (vallen > sizeof(*gf) + gf->gf_pathlen)
1710 GOTO(out, rc = -EOVERFLOW);
1712 CDEBUG(D_IOCTL, "path got "DFID" from %llu #%d: %s\n",
1713 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno,
1714 gf->gf_pathlen < 512 ? gf->gf_u.gf_path :
1715 /* only log the last 512 characters of the path */
1716 gf->gf_u.gf_path + gf->gf_pathlen - 512);
1719 OBD_FREE(key, keylen);
1723 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1724 struct hsm_progress_kernel *hpk)
1726 struct obd_import *imp = class_exp2cliimp(exp);
1727 struct hsm_progress_kernel *req_hpk;
1728 struct ptlrpc_request *req;
1732 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1733 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1735 GOTO(out, rc = -ENOMEM);
1737 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1739 /* Copy hsm_progress struct */
1740 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1741 if (req_hpk == NULL)
1742 GOTO(out, rc = -EPROTO);
1745 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1747 ptlrpc_request_set_replen(req);
1749 ptlrpc_get_mod_rpc_slot(req);
1750 rc = ptlrpc_queue_wait(req);
1751 ptlrpc_put_mod_rpc_slot(req);
1755 ptlrpc_req_finished(req);
1759 * Send hsm_ct_register to MDS
1761 * \param[in] imp import
1762 * \param[in] archive_count if in bitmap format, it is the bitmap,
1763 * else it is the count of archive_ids
1764 * \param[in] archives if in bitmap format, it is NULL,
1765 * else it is archive_id lists
1767 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archive_count,
1770 struct ptlrpc_request *req;
1771 __u32 *archive_array;
1772 size_t archives_size;
1776 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_CT_REGISTER);
1780 if (archives != NULL)
1781 archives_size = sizeof(*archive_array) * archive_count;
1783 archives_size = sizeof(archive_count);
1785 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_ARCHIVE,
1786 RCL_CLIENT, archives_size);
1788 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_CT_REGISTER);
1790 ptlrpc_request_free(req);
1794 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1796 archive_array = req_capsule_client_get(&req->rq_pill,
1797 &RMF_MDS_HSM_ARCHIVE);
1798 if (archive_array == NULL)
1799 GOTO(out, rc = -EPROTO);
1801 if (archives != NULL)
1802 memcpy(archive_array, archives, archives_size);
1804 *archive_array = archive_count;
1806 ptlrpc_request_set_replen(req);
1807 req->rq_no_resend = 1;
1809 rc = mdc_queue_wait(req);
1812 ptlrpc_req_finished(req);
1816 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1817 struct md_op_data *op_data)
1819 struct hsm_current_action *hca = op_data->op_data;
1820 struct hsm_current_action *req_hca;
1821 struct ptlrpc_request *req;
1825 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1826 &RQF_MDS_HSM_ACTION);
1830 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1832 ptlrpc_request_free(req);
1836 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1837 op_data->op_suppgids[0], 0);
1839 ptlrpc_request_set_replen(req);
1841 rc = mdc_queue_wait(req);
1845 req_hca = req_capsule_server_get(&req->rq_pill,
1846 &RMF_MDS_HSM_CURRENT_ACTION);
1847 if (req_hca == NULL)
1848 GOTO(out, rc = -EPROTO);
1854 ptlrpc_req_finished(req);
1858 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1860 struct ptlrpc_request *req;
1864 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1866 MDS_HSM_CT_UNREGISTER);
1868 GOTO(out, rc = -ENOMEM);
1870 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1872 ptlrpc_request_set_replen(req);
1874 rc = mdc_queue_wait(req);
1877 ptlrpc_req_finished(req);
1881 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1882 struct md_op_data *op_data)
1884 struct hsm_user_state *hus = op_data->op_data;
1885 struct hsm_user_state *req_hus;
1886 struct ptlrpc_request *req;
1890 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1891 &RQF_MDS_HSM_STATE_GET);
1895 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1897 ptlrpc_request_free(req);
1901 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1902 op_data->op_suppgids[0], 0);
1904 ptlrpc_request_set_replen(req);
1906 rc = mdc_queue_wait(req);
1910 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1911 if (req_hus == NULL)
1912 GOTO(out, rc = -EPROTO);
1918 ptlrpc_req_finished(req);
1922 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1923 struct md_op_data *op_data)
1925 struct hsm_state_set *hss = op_data->op_data;
1926 struct hsm_state_set *req_hss;
1927 struct ptlrpc_request *req;
1931 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1932 &RQF_MDS_HSM_STATE_SET);
1936 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1938 ptlrpc_request_free(req);
1942 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1943 op_data->op_suppgids[0], 0);
1946 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1947 if (req_hss == NULL)
1948 GOTO(out, rc = -EPROTO);
1951 ptlrpc_request_set_replen(req);
1953 ptlrpc_get_mod_rpc_slot(req);
1954 rc = ptlrpc_queue_wait(req);
1955 ptlrpc_put_mod_rpc_slot(req);
1959 ptlrpc_req_finished(req);
1963 static int mdc_ioc_hsm_request(struct obd_export *exp,
1964 struct hsm_user_request *hur)
1966 struct obd_import *imp = class_exp2cliimp(exp);
1967 struct ptlrpc_request *req;
1968 struct hsm_request *req_hr;
1969 struct hsm_user_item *req_hui;
1974 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1976 GOTO(out, rc = -ENOMEM);
1978 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1979 hur->hur_request.hr_itemcount
1980 * sizeof(struct hsm_user_item));
1981 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1982 hur->hur_request.hr_data_len);
1984 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1986 ptlrpc_request_free(req);
1990 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1992 /* Copy hsm_request struct */
1993 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1995 GOTO(out, rc = -EPROTO);
1996 *req_hr = hur->hur_request;
1998 /* Copy hsm_user_item structs */
1999 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
2000 if (req_hui == NULL)
2001 GOTO(out, rc = -EPROTO);
2002 memcpy(req_hui, hur->hur_user_item,
2003 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
2005 /* Copy opaque field */
2006 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
2007 if (req_opaque == NULL)
2008 GOTO(out, rc = -EPROTO);
2009 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
2011 ptlrpc_request_set_replen(req);
2013 ptlrpc_get_mod_rpc_slot(req);
2014 rc = ptlrpc_queue_wait(req);
2015 ptlrpc_put_mod_rpc_slot(req);
2020 ptlrpc_req_finished(req);
2024 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2025 struct lustre_kernelcomm *lk);
2027 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2028 struct obd_quotactl *oqctl)
2030 struct ptlrpc_request *req;
2031 struct obd_quotactl *oqc;
2035 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_QUOTACTL);
2040 if (LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd))
2041 req_capsule_set_size(&req->rq_pill,
2044 sizeof(*oqc) + LOV_MAXPOOLNAME + 1);
2046 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION,
2049 ptlrpc_request_free(req);
2053 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2054 QCTL_COPY(oqc, oqctl);
2056 ptlrpc_request_set_replen(req);
2057 ptlrpc_at_set_req_timeout(req);
2059 rc = ptlrpc_queue_wait(req);
2061 CERROR("%s: ptlrpc_queue_wait failed: rc = %d\n",
2062 exp->exp_obd->obd_name, rc);
2066 if (req->rq_repmsg &&
2067 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2068 QCTL_COPY(oqctl, oqc);
2071 CERROR("%s: cannot unpack obd_quotactl: rc = %d\n",
2072 exp->exp_obd->obd_name, rc);
2075 ptlrpc_req_finished(req);
2080 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2081 struct md_op_data *op_data)
2084 struct ptlrpc_request *req;
2086 struct mdc_swap_layouts *msl, *payload;
2089 msl = op_data->op_data;
2091 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2092 * first thing it will do is to cancel the 2 layout
2093 * locks held by this client.
2094 * So the client must cancel its layout locks on the 2 fids
2095 * with the request RPC to avoid extra RPC round trips.
2097 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2098 LCK_EX, MDS_INODELOCK_LAYOUT |
2099 MDS_INODELOCK_XATTR);
2100 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2101 LCK_EX, MDS_INODELOCK_LAYOUT |
2102 MDS_INODELOCK_XATTR);
2104 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2105 &RQF_MDS_SWAP_LAYOUTS);
2107 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2111 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2113 ptlrpc_request_free(req);
2117 mdc_swap_layouts_pack(&req->rq_pill, op_data);
2119 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2124 ptlrpc_request_set_replen(req);
2126 rc = ptlrpc_queue_wait(req);
2132 ptlrpc_req_finished(req);
2136 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2137 void *karg, void __user *uarg)
2139 struct obd_device *obd = exp->exp_obd;
2140 struct obd_ioctl_data *data = karg;
2141 struct obd_import *imp = obd->u.cli.cl_import;
2145 if (!try_module_get(THIS_MODULE)) {
2146 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2147 module_name(THIS_MODULE));
2151 case OBD_IOC_FID2PATH:
2152 rc = mdc_ioc_fid2path(exp, karg);
2154 case LL_IOC_HSM_CT_START:
2155 rc = mdc_ioc_hsm_ct_start(exp, karg);
2156 /* ignore if it was already registered on this MDS. */
2160 case LL_IOC_HSM_PROGRESS:
2161 rc = mdc_ioc_hsm_progress(exp, karg);
2163 case LL_IOC_HSM_STATE_GET:
2164 rc = mdc_ioc_hsm_state_get(exp, karg);
2166 case LL_IOC_HSM_STATE_SET:
2167 rc = mdc_ioc_hsm_state_set(exp, karg);
2169 case LL_IOC_HSM_ACTION:
2170 rc = mdc_ioc_hsm_current_action(exp, karg);
2172 case LL_IOC_HSM_REQUEST:
2173 rc = mdc_ioc_hsm_request(exp, karg);
2175 case OBD_IOC_CLIENT_RECOVER:
2176 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2180 case IOC_OSC_SET_ACTIVE:
2181 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2184 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2185 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2186 * there'd be no LMV layer thus we might be called here. Eventually
2187 * this code should be removed.
2190 case IOC_OBD_STATFS: {
2191 struct obd_statfs stat_buf = {0};
2193 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2194 GOTO(out, rc = -ENODEV);
2197 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2198 min((int)data->ioc_plen2,
2199 (int)sizeof(struct obd_uuid))))
2200 GOTO(out, rc = -EFAULT);
2202 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2203 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
2208 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2209 min((int) data->ioc_plen1,
2210 (int) sizeof(stat_buf))))
2211 GOTO(out, rc = -EFAULT);
2215 case OBD_IOC_QUOTACTL: {
2216 struct if_quotactl *qctl = karg;
2217 struct obd_quotactl *oqctl;
2219 OBD_ALLOC_PTR(oqctl);
2221 GOTO(out, rc = -ENOMEM);
2223 QCTL_COPY(oqctl, qctl);
2224 rc = obd_quotactl(exp, oqctl);
2226 QCTL_COPY(qctl, oqctl);
2227 qctl->qc_valid = QC_MDTIDX;
2228 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2231 OBD_FREE_PTR(oqctl);
2234 case LL_IOC_GET_CONNECT_FLAGS:
2235 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2236 sizeof(*exp_connect_flags_ptr(exp))))
2237 GOTO(out, rc = -EFAULT);
2240 case LL_IOC_LOV_SWAP_LAYOUTS:
2241 rc = mdc_ioc_swap_layouts(exp, karg);
2244 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2245 GOTO(out, rc = -ENOTTY);
2248 module_put(THIS_MODULE);
2253 static int mdc_get_info_rpc(struct obd_export *exp,
2254 u32 keylen, void *key,
2255 u32 vallen, void *val)
2257 struct obd_import *imp = class_exp2cliimp(exp);
2258 struct ptlrpc_request *req;
2263 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2267 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2268 RCL_CLIENT, keylen);
2269 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2270 RCL_CLIENT, sizeof(vallen));
2272 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2274 ptlrpc_request_free(req);
2278 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2279 memcpy(tmp, key, keylen);
2280 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2281 memcpy(tmp, &vallen, sizeof(vallen));
2283 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2284 RCL_SERVER, vallen);
2285 ptlrpc_request_set_replen(req);
2287 /* if server failed to resolve FID, and OI scrub not able to fix it, it
2288 * will return -EINPROGRESS, ptlrpc_queue_wait() will keep retrying,
2289 * set request interruptible to avoid deadlock.
2291 if (KEY_IS(KEY_FID2PATH))
2292 req->rq_allow_intr = 1;
2294 rc = ptlrpc_queue_wait(req);
2295 /* -EREMOTE means the get_info result is partial, and it needs to
2296 * continue on another MDT, see fid2path part in lmv_iocontrol */
2297 if (rc == 0 || rc == -EREMOTE) {
2298 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2299 memcpy(val, tmp, vallen);
2300 if (req_capsule_rep_need_swab(&req->rq_pill)) {
2301 if (KEY_IS(KEY_FID2PATH))
2302 lustre_swab_fid2path(val);
2305 ptlrpc_req_finished(req);
2310 static void lustre_swab_hai(struct hsm_action_item *h)
2312 __swab32s(&h->hai_len);
2313 __swab32s(&h->hai_action);
2314 lustre_swab_lu_fid(&h->hai_fid);
2315 lustre_swab_lu_fid(&h->hai_dfid);
2316 __swab64s(&h->hai_cookie);
2317 __swab64s(&h->hai_extent.offset);
2318 __swab64s(&h->hai_extent.length);
2319 __swab64s(&h->hai_gid);
2322 static void lustre_swab_hal(struct hsm_action_list *h)
2324 struct hsm_action_item *hai;
2327 __swab32s(&h->hal_version);
2328 __swab32s(&h->hal_count);
2329 __swab32s(&h->hal_archive_id);
2330 __swab64s(&h->hal_flags);
2332 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2333 lustre_swab_hai(hai);
2336 static void lustre_swab_kuch(struct kuc_hdr *l)
2338 __swab16s(&l->kuc_magic);
2339 /* __u8 l->kuc_transport */
2340 __swab16s(&l->kuc_msgtype);
2341 __swab16s(&l->kuc_msglen);
2344 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2345 struct lustre_kernelcomm *lk)
2347 struct obd_import *imp = class_exp2cliimp(exp);
2350 if (lk->lk_group != KUC_GRP_HSM) {
2351 CERROR("Bad copytool group %d\n", lk->lk_group);
2355 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2356 lk->lk_uid, lk->lk_group, lk->lk_flags);
2358 if (lk->lk_flags & LK_FLG_STOP) {
2359 /* Unregister with the coordinator */
2360 rc = mdc_ioc_hsm_ct_unregister(imp);
2362 __u32 *archives = NULL;
2364 if ((lk->lk_flags & LK_FLG_DATANR) && lk->lk_data_count > 0)
2365 archives = lk->lk_data;
2367 rc = mdc_ioc_hsm_ct_register(imp, lk->lk_data_count, archives);
2374 * Send a message to any listening copytools
2375 * @param val KUC message (kuc_hdr + hsm_action_list)
2376 * @param len total length of message
2378 static int mdc_hsm_copytool_send(const struct obd_uuid *uuid,
2379 size_t len, void *val)
2381 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2382 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2386 if (len < sizeof(*lh) + sizeof(*hal)) {
2387 CERROR("Short HSM message %zu < %zu\n", len,
2388 sizeof(*lh) + sizeof(*hal));
2391 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2392 lustre_swab_kuch(lh);
2393 lustre_swab_hal(hal);
2394 } else if (lh->kuc_magic != KUC_MAGIC) {
2395 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2399 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2401 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2402 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2404 /* Broadcast to HSM listeners */
2405 rc = libcfs_kkuc_group_put(uuid, KUC_GRP_HSM, lh);
2411 * callback function passed to kuc for re-registering each HSM copytool
2412 * running on MDC, after MDT shutdown/recovery.
2413 * @param data copytool registration data
2414 * @param cb_arg callback argument (obd_import)
2416 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2418 struct obd_import *imp = (struct obd_import *)cb_arg;
2419 struct kkuc_ct_data *kcd = data;
2420 __u32 *archives = NULL;
2424 (kcd->kcd_magic != KKUC_CT_DATA_ARRAY_MAGIC &&
2425 kcd->kcd_magic != KKUC_CT_DATA_BITMAP_MAGIC))
2428 if (kcd->kcd_magic == KKUC_CT_DATA_BITMAP_MAGIC) {
2429 CDEBUG(D_HA, "%s: recover copytool registration to MDT "
2430 "(archive=%#x)\n", imp->imp_obd->obd_name,
2431 kcd->kcd_nr_archives);
2433 CDEBUG(D_HA, "%s: recover copytool registration to MDT "
2434 "(archive nr = %u)\n",
2435 imp->imp_obd->obd_name, kcd->kcd_nr_archives);
2436 if (kcd->kcd_nr_archives != 0)
2437 archives = kcd->kcd_archives;
2440 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_nr_archives, archives);
2441 /* ignore error if the copytool is already registered */
2442 return (rc == -EEXIST) ? 0 : rc;
2446 * Re-establish all kuc contexts with MDT
2447 * after MDT shutdown/recovery.
2449 static int mdc_kuc_reregister(struct obd_import *imp)
2451 /* re-register HSM agents */
2452 return libcfs_kkuc_group_foreach(&imp->imp_obd->obd_uuid, KUC_GRP_HSM,
2453 mdc_hsm_ct_reregister, imp);
2456 static int mdc_set_info_async(const struct lu_env *env,
2457 struct obd_export *exp,
2458 u32 keylen, void *key,
2459 u32 vallen, void *val,
2460 struct ptlrpc_request_set *set)
2462 struct obd_import *imp = class_exp2cliimp(exp);
2466 if (KEY_IS(KEY_READ_ONLY)) {
2467 if (vallen != sizeof(int))
2470 spin_lock(&imp->imp_lock);
2471 if (*((int *)val)) {
2472 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2473 imp->imp_connect_data.ocd_connect_flags |=
2476 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2477 imp->imp_connect_data.ocd_connect_flags &=
2478 ~OBD_CONNECT_RDONLY;
2480 spin_unlock(&imp->imp_lock);
2482 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2483 keylen, key, vallen, val, set);
2486 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2487 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2488 keylen, key, vallen, val, set);
2491 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2492 rc = mdc_hsm_copytool_send(&imp->imp_obd->obd_uuid, vallen,
2497 if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2498 __u32 *default_easize = val;
2500 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2504 rc = osc_set_info_async(env, exp, keylen, key, vallen, val, set);
2508 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2509 __u32 keylen, void *key, __u32 *vallen, void *val)
2513 if (KEY_IS(KEY_MAX_EASIZE)) {
2514 __u32 mdsize, *max_easize;
2516 if (*vallen != sizeof(int))
2518 mdsize = *(__u32 *)val;
2519 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2520 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2522 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2524 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2525 __u32 *default_easize;
2527 if (*vallen != sizeof(int))
2529 default_easize = val;
2530 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2532 } else if (KEY_IS(KEY_CONN_DATA)) {
2533 struct obd_import *imp = class_exp2cliimp(exp);
2534 struct obd_connect_data *data = val;
2536 if (*vallen != sizeof(*data))
2539 *data = imp->imp_connect_data;
2541 } else if (KEY_IS(KEY_TGT_COUNT)) {
2542 *((__u32 *)val) = 1;
2546 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2551 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2552 struct ptlrpc_request **request)
2554 struct ptlrpc_request *req;
2559 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2563 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2565 ptlrpc_request_free(req);
2569 mdc_pack_body(&req->rq_pill, fid, 0, 0, -1, 0);
2571 ptlrpc_request_set_replen(req);
2573 rc = ptlrpc_queue_wait(req);
2575 ptlrpc_req_finished(req);
2581 struct mdc_rmfid_args {
2586 int mdc_rmfid_interpret(const struct lu_env *env, struct ptlrpc_request *req,
2589 struct mdc_rmfid_args *aa;
2594 aa = ptlrpc_req_async_args(aa, req);
2596 size = req_capsule_get_size(&req->rq_pill, &RMF_RCS,
2598 LASSERT(size == sizeof(int) * aa->mra_nr);
2599 rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
2601 LASSERT(aa->mra_rcs);
2602 LASSERT(aa->mra_nr);
2603 memcpy(aa->mra_rcs, rcs, size);
2609 static int mdc_rmfid(struct obd_export *exp, struct fid_array *fa,
2610 int *rcs, struct ptlrpc_request_set *set)
2612 struct ptlrpc_request *req;
2613 struct mdc_rmfid_args *aa;
2619 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_RMFID);
2623 flen = fa->fa_nr * sizeof(struct lu_fid);
2624 req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
2626 req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
2628 req_capsule_set_size(&req->rq_pill, &RMF_RCS,
2629 RCL_SERVER, fa->fa_nr * sizeof(__u32));
2630 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_RMFID);
2632 ptlrpc_request_free(req);
2635 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FID_ARRAY);
2636 memcpy(tmp, fa->fa_fids, flen);
2638 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
2639 b = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2640 b->mbo_ctime = ktime_get_real_seconds();
2642 ptlrpc_request_set_replen(req);
2645 aa = ptlrpc_req_async_args(aa, req);
2647 aa->mra_nr = fa->fa_nr;
2648 req->rq_interpret_reply = mdc_rmfid_interpret;
2650 ptlrpc_set_add_req(set, req);
2651 ptlrpc_check_set(NULL, set);
2656 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2657 enum obd_import_event event)
2659 struct client_obd *cli = &obd->u.cli;
2662 LASSERT(imp->imp_obd == obd);
2665 case IMP_EVENT_DISCON:
2666 spin_lock(&cli->cl_loi_list_lock);
2667 cli->cl_avail_grant = 0;
2668 cli->cl_lost_grant = 0;
2669 spin_unlock(&cli->cl_loi_list_lock);
2671 case IMP_EVENT_INACTIVE:
2673 * Flush current sequence to make client obtain new one
2674 * from server in case of disconnect/reconnect.
2676 down_read(&cli->cl_seq_rwsem);
2678 seq_client_flush(cli->cl_seq);
2679 up_read(&cli->cl_seq_rwsem);
2681 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE);
2683 case IMP_EVENT_INVALIDATE: {
2684 struct ldlm_namespace *ns = obd->obd_namespace;
2688 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2690 env = cl_env_get(&refcheck);
2692 /* Reset grants. All pages go to failing rpcs due to
2693 * the invalid import.
2695 osc_io_unplug(env, cli, NULL);
2697 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2698 osc_ldlm_resource_invalidate,
2700 cl_env_put(env, &refcheck);
2701 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2707 case IMP_EVENT_ACTIVE:
2708 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE);
2709 /* redo the kuc registration after reconnecting */
2711 rc = mdc_kuc_reregister(imp);
2713 case IMP_EVENT_OCD: {
2714 struct obd_connect_data *ocd = &imp->imp_connect_data;
2716 if (OCD_HAS_FLAG(ocd, GRANT))
2717 osc_init_grant(cli, ocd);
2719 md_init_ea_size(obd->obd_self_export, ocd->ocd_max_easize, 0);
2720 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD);
2723 case IMP_EVENT_DEACTIVATE:
2724 case IMP_EVENT_ACTIVATE:
2727 CERROR("Unknown import event %x\n", event);
2733 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2734 struct lu_fid *fid, struct md_op_data *op_data)
2736 struct client_obd *cli = &exp->exp_obd->u.cli;
2741 down_read(&cli->cl_seq_rwsem);
2743 rc = seq_client_alloc_fid(env, cli->cl_seq, fid);
2744 up_read(&cli->cl_seq_rwsem);
2749 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2751 struct client_obd *cli = &exp->exp_obd->u.cli;
2752 return &cli->cl_target_uuid;
2756 * Determine whether the lock can be canceled before replaying it during
2757 * recovery, non zero value will be return if the lock can be canceled,
2758 * or zero returned for not
2760 static int mdc_cancel_weight(struct ldlm_lock *lock)
2762 if (lock->l_resource->lr_type != LDLM_IBITS)
2765 /* FIXME: if we ever get into a situation where there are too many
2766 * opened files with open locks on a single node, then we really
2767 * should replay these open locks to reget it */
2768 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2771 /* Special case for DoM locks, cancel only unused and granted locks */
2772 if (ldlm_has_dom(lock) &&
2773 (lock->l_granted_mode != lock->l_req_mode ||
2774 osc_ldlm_weigh_ast(lock) != 0))
2780 static int mdc_resource_inode_free(struct ldlm_resource *res)
2782 if (res->lr_lvb_inode)
2783 res->lr_lvb_inode = NULL;
2788 static struct ldlm_valblock_ops inode_lvbo = {
2789 .lvbo_free = mdc_resource_inode_free
2792 static int mdc_llog_init(struct obd_device *obd)
2794 struct obd_llog_group *olg = &obd->obd_olg;
2795 struct llog_ctxt *ctxt;
2800 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2805 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2806 llog_initiator_connect(ctxt);
2807 llog_ctxt_put(ctxt);
2812 static void mdc_llog_finish(struct obd_device *obd)
2814 struct llog_ctxt *ctxt;
2818 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2820 llog_cleanup(NULL, ctxt);
2825 int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2831 rc = osc_setup_common(obd, cfg);
2835 rc = mdc_tunables_init(obd);
2837 GOTO(err_osc_cleanup, rc);
2839 obd->u.cli.cl_dom_min_inline_repsize = MDC_DOM_DEF_INLINE_REPSIZE;
2840 obd->u.cli.cl_lsom_update = true;
2842 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2844 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2846 rc = mdc_llog_init(obd);
2848 CERROR("%s: failed to setup llogging subsystems: rc = %d\n",
2850 GOTO(err_llog_cleanup, rc);
2853 rc = mdc_changelog_cdev_init(obd);
2855 CERROR("%s: failed to setup changelog char device: rc = %d\n",
2857 GOTO(err_changelog_cleanup, rc);
2862 err_changelog_cleanup:
2863 mdc_llog_finish(obd);
2865 lprocfs_free_md_stats(obd);
2866 ptlrpc_lprocfs_unregister_obd(obd);
2868 osc_cleanup_common(obd);
2872 /* Initialize the default and maximum LOV EA sizes. This allows
2873 * us to make MDS RPCs with large enough reply buffers to hold a default
2874 * sized EA without having to calculate this (via a call into the
2875 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2876 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2877 * a large number of stripes is possible. If a larger reply buffer is
2878 * required it will be reallocated in the ptlrpc layer due to overflow.
2880 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2883 struct obd_device *obd = exp->exp_obd;
2884 struct client_obd *cli = &obd->u.cli;
2887 if (cli->cl_max_mds_easize < easize)
2888 cli->cl_max_mds_easize = easize;
2890 if (cli->cl_default_mds_easize < def_easize)
2891 cli->cl_default_mds_easize = def_easize;
2896 static int mdc_precleanup(struct obd_device *obd)
2900 osc_precleanup_common(obd);
2901 mdc_changelog_cdev_finish(obd);
2903 obd_cleanup_client_import(obd);
2904 ptlrpc_lprocfs_unregister_obd(obd);
2905 lprocfs_free_md_stats(obd);
2906 mdc_llog_finish(obd);
2910 static int mdc_cleanup(struct obd_device *obd)
2912 return osc_cleanup_common(obd);
2915 static const struct obd_ops mdc_obd_ops = {
2916 .o_owner = THIS_MODULE,
2917 .o_setup = mdc_setup,
2918 .o_precleanup = mdc_precleanup,
2919 .o_cleanup = mdc_cleanup,
2920 .o_add_conn = client_import_add_conn,
2921 .o_del_conn = client_import_del_conn,
2922 .o_connect = client_connect_import,
2923 .o_reconnect = osc_reconnect,
2924 .o_disconnect = osc_disconnect,
2925 .o_iocontrol = mdc_iocontrol,
2926 .o_set_info_async = mdc_set_info_async,
2927 .o_statfs = mdc_statfs,
2928 .o_statfs_async = mdc_statfs_async,
2929 .o_fid_init = client_fid_init,
2930 .o_fid_fini = client_fid_fini,
2931 .o_fid_alloc = mdc_fid_alloc,
2932 .o_import_event = mdc_import_event,
2933 .o_get_info = mdc_get_info,
2934 .o_get_uuid = mdc_get_uuid,
2935 .o_quotactl = mdc_quotactl,
2938 static const struct md_ops mdc_md_ops = {
2939 .m_get_root = mdc_get_root,
2940 .m_null_inode = mdc_null_inode,
2941 .m_close = mdc_close,
2942 .m_create = mdc_create,
2943 .m_enqueue = mdc_enqueue,
2944 .m_getattr = mdc_getattr,
2945 .m_getattr_name = mdc_getattr_name,
2946 .m_intent_lock = mdc_intent_lock,
2948 .m_rename = mdc_rename,
2949 .m_setattr = mdc_setattr,
2950 .m_setxattr = mdc_setxattr,
2951 .m_getxattr = mdc_getxattr,
2952 .m_fsync = mdc_fsync,
2953 .m_file_resync = mdc_file_resync,
2954 .m_read_page = mdc_read_page,
2955 .m_unlink = mdc_unlink,
2956 .m_cancel_unused = mdc_cancel_unused,
2957 .m_init_ea_size = mdc_init_ea_size,
2958 .m_set_lock_data = mdc_set_lock_data,
2959 .m_lock_match = mdc_lock_match,
2960 .m_get_lustre_md = mdc_get_lustre_md,
2961 .m_free_lustre_md = mdc_free_lustre_md,
2962 .m_set_open_replay_data = mdc_set_open_replay_data,
2963 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2964 .m_intent_getattr_async = mdc_intent_getattr_async,
2965 .m_revalidate_lock = mdc_revalidate_lock,
2966 .m_rmfid = mdc_rmfid,
2969 dev_t mdc_changelog_dev;
2970 struct class *mdc_changelog_class;
2971 static int __init mdc_init(void)
2974 rc = alloc_chrdev_region(&mdc_changelog_dev, 0,
2975 MDC_CHANGELOG_DEV_COUNT,
2976 MDC_CHANGELOG_DEV_NAME);
2980 mdc_changelog_class = class_create(THIS_MODULE, MDC_CHANGELOG_DEV_NAME);
2981 if (IS_ERR(mdc_changelog_class)) {
2982 rc = PTR_ERR(mdc_changelog_class);
2986 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, true,
2987 LUSTRE_MDC_NAME, &mdc_device_type);
2994 class_destroy(mdc_changelog_class);
2996 unregister_chrdev_region(mdc_changelog_dev, MDC_CHANGELOG_DEV_COUNT);
3000 static void __exit mdc_exit(void)
3002 class_unregister_type(LUSTRE_MDC_NAME);
3003 class_destroy(mdc_changelog_class);
3004 unregister_chrdev_region(mdc_changelog_dev, MDC_CHANGELOG_DEV_COUNT);
3005 idr_destroy(&mdc_changelog_minor_idr);
3008 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3009 MODULE_DESCRIPTION("Lustre Metadata Client");
3010 MODULE_VERSION(LUSTRE_VERSION_STRING);
3011 MODULE_LICENSE("GPL");
3013 module_init(mdc_init);
3014 module_exit(mdc_exit);