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);
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,
238 ptlrpc_request_set_replen(req);
240 rc = mdc_getattr_common(exp, req);
243 acl_bufsize = min_t(__u32,
244 imp->imp_connect_data.ocd_max_easize,
246 mdc_reset_acl_req(req);
250 ptlrpc_req_finished(req);
258 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
259 struct ptlrpc_request **request)
261 struct ptlrpc_request *req;
262 struct obd_import *imp = class_exp2cliimp(exp);
263 __u32 acl_bufsize = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
268 req = ptlrpc_request_alloc(imp, &RQF_MDS_GETATTR_NAME);
272 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
273 op_data->op_namelen + 1);
275 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
277 ptlrpc_request_free(req);
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);
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,
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,
300 b->mbo_valid |= OBD_MD_NAMEHASH;
301 b->mbo_fid2 = op_data->op_fid2;
305 rc = mdc_getattr_common(exp, req);
308 acl_bufsize = min_t(__u32,
309 imp->imp_connect_data.ocd_max_easize,
311 mdc_reset_acl_req(req);
315 ptlrpc_req_finished(req);
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)
329 struct ptlrpc_request *req;
330 int xattr_namelen = 0;
336 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
341 xattr_namelen = strlen(xattr_name) + 1;
342 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
347 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
350 /* get SELinux policy info if any */
351 rc = sptlrpc_get_sepol(req);
353 ptlrpc_request_free(req);
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);
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) {
366 /* Without that packing would fail */
368 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
371 count = mdc_resource_get_unused(exp, fid,
373 MDS_INODELOCK_XATTR);
375 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
377 ptlrpc_request_free(req);
381 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
383 ptlrpc_request_free(req);
388 if (opcode == MDS_REINT) {
389 struct mdt_rec_setxattr *rec;
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;
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;
406 mdc_pack_body(&req->rq_pill, fid, valid, output_size,
411 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
412 memcpy(tmp, xattr_name, xattr_namelen);
415 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
416 memcpy(tmp, input, input_size);
419 mdc_file_sepol_pack(&req->rq_pill);
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);
427 if (opcode == MDS_REINT)
428 ptlrpc_get_mod_rpc_slot(req);
430 rc = ptlrpc_queue_wait(req);
432 if (opcode == MDS_REINT)
433 ptlrpc_put_mod_rpc_slot(req);
436 ptlrpc_req_finished(req);
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)
448 LASSERT(obd_md_valid == OBD_MD_FLXATTR ||
449 obd_md_valid == OBD_MD_FLXATTRRM);
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,
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)
461 struct mdt_body *body;
464 LASSERT(obd_md_valid == OBD_MD_FLXATTR ||
465 obd_md_valid == OBD_MD_FLXATTRLS);
467 /* Message below is checked in sanity-selinux test_20d
468 * and sanity-sec test_49
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,
478 body = req_capsule_server_get(&(*req)->rq_pill, &RMF_MDT_BODY);
480 GOTO(out, rc = -EPROTO);
482 /* only detect the xattr size */
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);
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.
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)
503 else if (obd_md_valid == OBD_MD_FLXATTR)
504 GOTO(out, rc = -ENODATA);
509 GOTO(out, rc = body->mbo_eadatasize);
512 ptlrpc_req_finished(*req);
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)
528 memset(md, 0, sizeof(*md));
530 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
531 LASSERT(md->body != NULL);
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);
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);
546 md->layout.lb_len = md->body->mbo_eadatasize;
547 md->layout.lb_buf = req_capsule_server_sized_get(pill,
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;
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);
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);
566 GOTO(out_acl, rc = 0);
569 if (md->body->mbo_valid & OBD_MD_MEA) {
570 lmv_size = md->body->mbo_eadatasize;
572 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
573 "but eadatasize 0\n");
577 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
580 GOTO(out, rc = -EPROTO);
582 rc = md_unpackmd(md_exp, &md->lmv, lmv, lmv_size);
586 if (rc < (int)sizeof(*md->lmv)) {
587 struct lmv_foreign_md *lfm = md->lfm;
589 /* short (< sizeof(struct lmv_stripe_md))
592 if (lfm->lfm_magic != LMV_MAGIC_FOREIGN) {
594 "lmv size too small: %d < %d\n",
595 rc, (int)sizeof(*md->lmv));
596 GOTO(out, rc = -EPROTO);
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,
608 GOTO(out, rc = -EPROTO);
610 rc = md_unpackmd(md_exp, &md->default_lmv, lmv,
615 if (rc < (int)sizeof(*md->default_lmv)) {
617 "default lmv size too small: %d < %d\n",
618 rc, (int)sizeof(*md->default_lmv));
619 GOTO(out, rc = -EPROTO);
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
631 rc = mdc_unpack_acl(pill, md);
644 static int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
650 void mdc_replay_open(struct ptlrpc_request *req)
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;
661 DEBUG_REQ(D_ERROR, req,
662 "cannot properly replay without open data");
667 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
668 LASSERT(body != NULL);
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);
675 spin_lock(&req->rq_lock);
677 if (och && och->och_open_handle.cookie)
678 req->rq_early_free_repbuf = 1;
680 req->rq_early_free_repbuf = 0;
681 spin_unlock(&req->rq_lock);
683 if (req->rq_early_free_repbuf) {
684 struct lustre_handle *file_open_handle;
686 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
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;
695 close_req = mod->mod_close_req;
697 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
698 struct mdt_ioepoch *epoch;
700 LASSERT(opc == MDS_CLOSE);
701 epoch = req_capsule_client_get(&close_req->rq_pill,
705 if (req->rq_early_free_repbuf)
706 LASSERT(old_open_handle.cookie ==
707 epoch->mio_open_handle.cookie);
709 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
710 epoch->mio_open_handle = body->mbo_open_handle;
715 void mdc_commit_open(struct ptlrpc_request *req)
717 struct md_open_data *mod = req->rq_cb_data;
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.
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.
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;
743 int mdc_set_open_replay_data(struct obd_export *exp,
744 struct obd_client_handle *och,
745 struct lookup_intent *it)
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;
754 if (!open_req->rq_replay)
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);
764 /* Only if the import is replayable, we set replay_open data */
765 if (och && imp->imp_replayable) {
766 mod = obd_mod_alloc();
768 DEBUG_REQ(D_ERROR, open_req,
769 "cannot allocate md_open_data");
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.
782 spin_lock(&open_req->rq_lock);
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);
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));
804 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
808 static void mdc_free_open(struct md_open_data *mod)
812 if (mod->mod_is_create == 0 &&
813 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
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
823 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req,
824 "free open request, rq_replay=%d",
825 mod->mod_open_req->rq_replay);
827 ptlrpc_request_committed(mod->mod_open_req, committed);
828 if (mod->mod_close_req)
829 ptlrpc_request_committed(mod->mod_close_req, committed);
832 static int mdc_clear_open_replay_data(struct obd_export *exp,
833 struct obd_client_handle *och)
835 struct md_open_data *mod = och->och_mod;
839 * It is possible to not have \var mod in a case of eviction between
840 * lookup and ll_file_open().
845 LASSERT(mod != LP_POISON);
846 LASSERT(mod->mod_open_req != NULL);
848 spin_lock(&mod->mod_open_req->rq_lock);
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);
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)
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;
873 CDEBUG(D_INODE, "%s: "DFID" file closed with intent: %x\n",
874 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
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,
884 CERROR("%s: "DFID" allocating FID: rc = %d\n",
885 obd->obd_name, PFID(&op_data->op_fid1),
887 /* save the errcode and proceed to close */
891 if (op_data->op_bias & MDS_CLOSE_RESYNC_DONE) {
892 size_t count = op_data->op_data_size / sizeof(__u32);
894 if (count > INLINE_RESYNC_ARRAY_SIZE)
898 req_fmt = &RQF_MDS_CLOSE;
902 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_CLOSE))
905 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
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);
913 mod->mod_close_req = req;
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);
922 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
926 * TODO: repeat close after errors
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);
935 req_capsule_set_size(&req->rq_pill, &RMF_U32, RCL_CLIENT,
936 u32_count * sizeof(__u32));
938 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
940 ptlrpc_request_free(req);
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);
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);
956 mdc_close_pack(&req->rq_pill, op_data);
958 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
959 obd->u.cli.cl_default_mds_easize);
961 ptlrpc_request_set_replen(req);
963 ptlrpc_get_mod_rpc_slot(req);
964 rc = ptlrpc_queue_wait(req);
965 ptlrpc_put_mod_rpc_slot(req);
967 if (req->rq_repmsg == NULL) {
968 CDEBUG(D_RPCTRACE, "request %p failed to send: rc = %d\n", req,
971 rc = req->rq_status ?: -EIO;
972 } else if (rc == 0 || rc == -EAGAIN) {
973 struct mdt_body *body;
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);
982 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
985 } else if (rc == -ESTALE) {
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
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)
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. */
1009 RETURN(rc < 0 ? rc : saved_rc);
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)
1016 struct ptlrpc_request *req;
1017 struct ptlrpc_bulk_desc *desc;
1026 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1030 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1032 ptlrpc_request_free(req);
1036 req->rq_request_portal = MDS_READPAGE_PORTAL;
1037 ptlrpc_at_set_req_timeout(req);
1039 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1040 PTLRPC_BULK_PUT_SINK,
1042 &ptlrpc_bulk_kiov_pin_ops);
1044 ptlrpc_req_finished(req);
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,
1053 mdc_readdir_pack(&req->rq_pill, offset, PAGE_SIZE * npages, fid);
1055 ptlrpc_request_set_replen(req);
1056 rc = ptlrpc_queue_wait(req);
1058 ptlrpc_req_finished(req);
1059 if (rc != -ETIMEDOUT)
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);
1069 /* If a signal interrupts then the timeout returned will
1070 * not be zero. In that case return -EINTR
1072 if (msleep_interruptible(resends * 1000))
1078 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1079 req->rq_bulk->bd_nob_transferred);
1081 ptlrpc_req_finished(req);
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);
1097 static void mdc_release_page(struct page *page, int remove)
1101 if (likely(page->mapping != NULL))
1102 delete_from_page_cache(page);
1108 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1109 __u64 *start, __u64 *end, int hash64)
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.
1116 unsigned long offset = hash_x_index(*hash, hash64);
1118 unsigned long flags;
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;
1128 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
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.
1134 * In fact, page cannot be locked here at all, because
1135 * mdc_read_page_remote does synchronous io.
1137 wait_on_page_locked(page);
1138 if (PageUptodate(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;
1145 *start = le64_to_cpu(dp->ldp_hash_start);
1146 *end = le64_to_cpu(dp->ldp_hash_end);
1148 if (unlikely(*start == 1 && *hash == 0))
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);
1158 mdc_release_page(page, 0);
1160 } else if (*end != *start && *hash == *end) {
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.
1168 mdc_release_page(page,
1169 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1174 page = ERR_PTR(-EIO);
1177 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
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.
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):
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 * '----------------- -----'
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.):
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 * '---|---------------- -----'
1219 * .----------------------------.
1222 * This structure is transformed into a single logical lu_dirpage as follows:
1224 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1225 * labeled 'next PAGE'.
1227 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1228 * a hash collision with the next page exists.
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.
1233 #if PAGE_SIZE > LU_PAGE_SIZE
1234 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
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;
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));
1251 /* Advance dp to next lu_dirpage. */
1252 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1254 /* Check if we've reached the end of the PAGE. */
1255 if (!((unsigned long)dp & ~PAGE_MASK))
1258 /* Save the hash and flags of this lu_dirpage. */
1259 hash_end = dp->ldp_hash_end;
1260 flags = dp->ldp_flags;
1262 /* Check if lu_dirpage contains no entries. */
1263 if (end_dirent == NULL)
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);
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);
1280 LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1283 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1284 #endif /* PAGE_SIZE > LU_PAGE_SIZE */
1286 /* parameters for readdir page */
1287 struct readpage_param {
1288 struct md_op_data *rp_mod;
1291 struct obd_export *rp_exp;
1292 struct md_callback *rp_cb;
1296 * Read pages from server.
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.
1305 static int mdc_read_page_remote(void *data, struct page *page0)
1307 struct readpage_param *rp = data;
1308 struct page **page_pool;
1310 struct lu_dirpage *dp;
1311 struct md_op_data *op_data = rp->rp_mod;
1312 struct ptlrpc_request *req;
1314 struct inode *inode;
1316 int rd_pgs = 0; /* number of pages actually read */
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);
1327 OBD_ALLOC_PTR_ARRAY_LARGE(page_pool, max_pages);
1328 if (page_pool != NULL) {
1329 page_pool[0] = page0;
1335 for (npages = 1; npages < max_pages; npages++) {
1336 page = page_cache_alloc(inode->i_mapping);
1339 page_pool[npages] = page;
1342 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, page_pool, npages, &req);
1344 /* page0 is special, which was added into page cache early */
1345 delete_from_page_cache(page0);
1349 rd_pgs = (req->rq_bulk->bd_nob_transferred + PAGE_SIZE - 1) >>
1351 lu_pgs = req->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
1352 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1354 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1356 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1358 SetPageUptodate(page0);
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;
1369 page = page_pool[i];
1371 if (rc < 0 || i >= rd_pgs) {
1376 SetPageUptodate(page);
1379 hash = le64_to_cpu(dp->ldp_hash_start);
1382 offset = hash_x_index(hash, rp->rp_hash64);
1384 prefetchw(&page->flags);
1385 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1390 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1391 " rc = %d\n", offset, ret);
1395 if (page_pool != &page0)
1396 OBD_FREE_PTR_ARRAY_LARGE(page_pool, max_pages);
1402 * Read dir page from cache first, if it can not find it, read it from
1403 * server and add into the cache.
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
1410 * \param[in] hash_offset the hash offset of the page to be read
1411 * \param[in] ppage the page to be read
1413 * retval = 0 get the page successfully
1414 * errno(<0) get the page failed
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)
1420 struct lookup_intent it = { .it_op = IT_READDIR };
1422 struct inode *dir = op_data->op_data;
1423 struct address_space *mapping;
1424 struct lu_dirpage *dp;
1427 struct lustre_handle lockh;
1428 struct ptlrpc_request *enq_req = NULL;
1429 struct readpage_param rp_param;
1436 LASSERT(dir != NULL);
1437 mapping = dir->i_mapping;
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);
1445 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1446 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1451 lockh.cookie = it.it_lock_handle;
1452 mdc_set_lock_data(exp, &lockh, dir, NULL);
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);
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) {
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?)
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.
1478 GOTO(hash_collision, page);
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);
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));
1494 wait_on_page_locked(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);
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);
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;
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;
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);
1532 * Fetch whole overflow chain...
1540 ldlm_lock_decref(&lockh, it.it_lock_mode);
1544 mdc_release_page(page, 1);
1549 static int mdc_statfs_interpret(const struct lu_env *env,
1550 struct ptlrpc_request *req, void *args, int rc)
1552 struct obd_info *oinfo = args;
1553 struct obd_statfs *osfs;
1556 osfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1560 oinfo->oi_osfs = osfs;
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);
1568 oinfo->oi_cb_up(oinfo, rc);
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)
1577 struct ptlrpc_request *req;
1578 struct obd_info *aa;
1580 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_STATFS,
1581 LUSTRE_MDS_VERSION, MDS_STATFS);
1585 ptlrpc_request_set_replen(req);
1586 req->rq_interpret_reply = mdc_statfs_interpret;
1588 aa = ptlrpc_req_async_args(aa, req);
1591 ptlrpcd_add_req(req);
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)
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;
1609 * Since the request might also come from lprocfs, so we need
1610 * sync this with client_disconnect_export Bug15684
1612 with_imp_locked(obd, imp0, rc)
1613 imp = class_import_get(imp0);
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,
1624 GOTO(output, rc = -ENOMEM);
1625 req->rq_allow_intr = 1;
1627 if ((flags & OBD_STATFS_SUM) &&
1628 (exp_connect_flags2(exp) & OBD_CONNECT2_SUM_STATFS)) {
1629 /* request aggregated states */
1630 struct mdt_body *body;
1632 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1634 GOTO(out, rc = -EPROTO);
1635 body->mbo_valid = OBD_MD_FLAGSTATFS;
1638 ptlrpc_request_set_replen(req);
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;
1646 rc = ptlrpc_queue_wait(req);
1648 /* check connection error first */
1649 if (imp->imp_connect_error)
1650 rc = imp->imp_connect_error;
1654 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1656 GOTO(out, rc = -EPROTO);
1661 ptlrpc_req_finished(req);
1663 class_import_put(imp);
1667 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1669 __u32 keylen, vallen;
1673 if (gf->gf_pathlen > PATH_MAX)
1674 RETURN(-ENAMETOOLONG);
1675 if (gf->gf_pathlen < 2)
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);
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);
1691 if (!fid_is_sane(&gf->gf_fid))
1692 GOTO(out, rc = -EINVAL);
1694 /* Val is struct getinfo_fid2path result plus path */
1695 vallen = sizeof(*gf) + gf->gf_pathlen;
1697 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf);
1698 if (rc != 0 && rc != -EREMOTE)
1701 if (vallen <= sizeof(*gf))
1702 GOTO(out, rc = -EPROTO);
1703 if (vallen > sizeof(*gf) + gf->gf_pathlen)
1704 GOTO(out, rc = -EOVERFLOW);
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);
1713 OBD_FREE(key, keylen);
1717 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1718 struct hsm_progress_kernel *hpk)
1720 struct obd_import *imp = class_exp2cliimp(exp);
1721 struct hsm_progress_kernel *req_hpk;
1722 struct ptlrpc_request *req;
1726 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1727 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1729 GOTO(out, rc = -ENOMEM);
1731 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
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);
1739 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1741 ptlrpc_request_set_replen(req);
1743 ptlrpc_get_mod_rpc_slot(req);
1744 rc = ptlrpc_queue_wait(req);
1745 ptlrpc_put_mod_rpc_slot(req);
1749 ptlrpc_req_finished(req);
1753 * Send hsm_ct_register to MDS
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
1761 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archive_count,
1764 struct ptlrpc_request *req;
1765 __u32 *archive_array;
1766 size_t archives_size;
1770 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_CT_REGISTER);
1774 if (archives != NULL)
1775 archives_size = sizeof(*archive_array) * archive_count;
1777 archives_size = sizeof(archive_count);
1779 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_ARCHIVE,
1780 RCL_CLIENT, archives_size);
1782 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_CT_REGISTER);
1784 ptlrpc_request_free(req);
1788 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
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);
1795 if (archives != NULL)
1796 memcpy(archive_array, archives, archives_size);
1798 *archive_array = archive_count;
1800 ptlrpc_request_set_replen(req);
1801 req->rq_no_resend = 1;
1803 rc = mdc_queue_wait(req);
1806 ptlrpc_req_finished(req);
1810 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1811 struct md_op_data *op_data)
1813 struct hsm_current_action *hca = op_data->op_data;
1814 struct hsm_current_action *req_hca;
1815 struct ptlrpc_request *req;
1819 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1820 &RQF_MDS_HSM_ACTION);
1824 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1826 ptlrpc_request_free(req);
1830 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1831 op_data->op_suppgids[0], 0);
1833 ptlrpc_request_set_replen(req);
1835 rc = mdc_queue_wait(req);
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);
1848 ptlrpc_req_finished(req);
1852 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1854 struct ptlrpc_request *req;
1858 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1860 MDS_HSM_CT_UNREGISTER);
1862 GOTO(out, rc = -ENOMEM);
1864 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1866 ptlrpc_request_set_replen(req);
1868 rc = mdc_queue_wait(req);
1871 ptlrpc_req_finished(req);
1875 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1876 struct md_op_data *op_data)
1878 struct hsm_user_state *hus = op_data->op_data;
1879 struct hsm_user_state *req_hus;
1880 struct ptlrpc_request *req;
1884 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1885 &RQF_MDS_HSM_STATE_GET);
1889 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1891 ptlrpc_request_free(req);
1895 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1896 op_data->op_suppgids[0], 0);
1898 ptlrpc_request_set_replen(req);
1900 rc = mdc_queue_wait(req);
1904 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1905 if (req_hus == NULL)
1906 GOTO(out, rc = -EPROTO);
1912 ptlrpc_req_finished(req);
1916 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1917 struct md_op_data *op_data)
1919 struct hsm_state_set *hss = op_data->op_data;
1920 struct hsm_state_set *req_hss;
1921 struct ptlrpc_request *req;
1925 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1926 &RQF_MDS_HSM_STATE_SET);
1930 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1932 ptlrpc_request_free(req);
1936 mdc_pack_body(&req->rq_pill, &op_data->op_fid1, 0, 0,
1937 op_data->op_suppgids[0], 0);
1940 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1941 if (req_hss == NULL)
1942 GOTO(out, rc = -EPROTO);
1945 ptlrpc_request_set_replen(req);
1947 ptlrpc_get_mod_rpc_slot(req);
1948 rc = ptlrpc_queue_wait(req);
1949 ptlrpc_put_mod_rpc_slot(req);
1953 ptlrpc_req_finished(req);
1957 static int mdc_ioc_hsm_request(struct obd_export *exp,
1958 struct hsm_user_request *hur)
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;
1968 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1970 GOTO(out, rc = -ENOMEM);
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);
1978 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1980 ptlrpc_request_free(req);
1984 mdc_pack_body(&req->rq_pill, NULL, 0, 0, -1, 0);
1986 /* Copy hsm_request struct */
1987 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1989 GOTO(out, rc = -EPROTO);
1990 *req_hr = hur->hur_request;
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));
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);
2005 ptlrpc_request_set_replen(req);
2007 ptlrpc_get_mod_rpc_slot(req);
2008 rc = ptlrpc_queue_wait(req);
2009 ptlrpc_put_mod_rpc_slot(req);
2014 ptlrpc_req_finished(req);
2018 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2019 struct lustre_kernelcomm *lk);
2021 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2022 struct obd_quotactl *oqctl)
2024 struct ptlrpc_request *req;
2025 struct obd_quotactl *oqc;
2029 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_QUOTACTL);
2034 if (LUSTRE_Q_CMD_IS_POOL(oqctl->qc_cmd))
2035 req_capsule_set_size(&req->rq_pill,
2038 sizeof(*oqc) + LOV_MAXPOOLNAME + 1);
2040 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION,
2043 ptlrpc_request_free(req);
2047 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2048 QCTL_COPY(oqc, oqctl);
2050 ptlrpc_request_set_replen(req);
2051 ptlrpc_at_set_req_timeout(req);
2053 rc = ptlrpc_queue_wait(req);
2055 CERROR("%s: ptlrpc_queue_wait failed: rc = %d\n",
2056 exp->exp_obd->obd_name, rc);
2060 if (req->rq_repmsg &&
2061 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2062 QCTL_COPY(oqctl, oqc);
2065 CERROR("%s: cannot unpack obd_quotactl: rc = %d\n",
2066 exp->exp_obd->obd_name, rc);
2069 ptlrpc_req_finished(req);
2074 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2075 struct md_op_data *op_data)
2078 struct ptlrpc_request *req;
2080 struct mdc_swap_layouts *msl, *payload;
2083 msl = op_data->op_data;
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.
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);
2098 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2099 &RQF_MDS_SWAP_LAYOUTS);
2101 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2105 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2107 ptlrpc_request_free(req);
2111 mdc_swap_layouts_pack(&req->rq_pill, op_data);
2113 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2118 ptlrpc_request_set_replen(req);
2120 rc = ptlrpc_queue_wait(req);
2126 ptlrpc_req_finished(req);
2130 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2131 void *karg, void __user *uarg)
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;
2139 if (!try_module_get(THIS_MODULE)) {
2140 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2141 module_name(THIS_MODULE));
2145 case OBD_IOC_FID2PATH:
2146 rc = mdc_ioc_fid2path(exp, karg);
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. */
2154 case LL_IOC_HSM_PROGRESS:
2155 rc = mdc_ioc_hsm_progress(exp, karg);
2157 case LL_IOC_HSM_STATE_GET:
2158 rc = mdc_ioc_hsm_state_get(exp, karg);
2160 case LL_IOC_HSM_STATE_SET:
2161 rc = mdc_ioc_hsm_state_set(exp, karg);
2163 case LL_IOC_HSM_ACTION:
2164 rc = mdc_ioc_hsm_current_action(exp, karg);
2166 case LL_IOC_HSM_REQUEST:
2167 rc = mdc_ioc_hsm_request(exp, karg);
2169 case OBD_IOC_CLIENT_RECOVER:
2170 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2174 case IOC_OSC_SET_ACTIVE:
2175 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
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.
2184 case IOC_OBD_STATFS: {
2185 struct obd_statfs stat_buf = {0};
2187 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2188 GOTO(out, rc = -ENODEV);
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);
2196 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2197 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
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);
2209 case OBD_IOC_QUOTACTL: {
2210 struct if_quotactl *qctl = karg;
2211 struct obd_quotactl *oqctl;
2213 OBD_ALLOC_PTR(oqctl);
2215 GOTO(out, rc = -ENOMEM);
2217 QCTL_COPY(oqctl, qctl);
2218 rc = obd_quotactl(exp, oqctl);
2220 QCTL_COPY(qctl, oqctl);
2221 qctl->qc_valid = QC_MDTIDX;
2222 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2225 OBD_FREE_PTR(oqctl);
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);
2234 case LL_IOC_LOV_SWAP_LAYOUTS:
2235 rc = mdc_ioc_swap_layouts(exp, karg);
2238 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2239 GOTO(out, rc = -ENOTTY);
2242 module_put(THIS_MODULE);
2247 static int mdc_get_info_rpc(struct obd_export *exp,
2248 u32 keylen, void *key,
2249 u32 vallen, void *val)
2251 struct obd_import *imp = class_exp2cliimp(exp);
2252 struct ptlrpc_request *req;
2257 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
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));
2266 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2268 ptlrpc_request_free(req);
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));
2277 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2278 RCL_SERVER, vallen);
2279 ptlrpc_request_set_replen(req);
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.
2285 if (KEY_IS(KEY_FID2PATH))
2286 req->rq_allow_intr = 1;
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);
2299 ptlrpc_req_finished(req);
2304 static void lustre_swab_hai(struct hsm_action_item *h)
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);
2316 static void lustre_swab_hal(struct hsm_action_list *h)
2318 struct hsm_action_item *hai;
2321 __swab32s(&h->hal_version);
2322 __swab32s(&h->hal_count);
2323 __swab32s(&h->hal_archive_id);
2324 __swab64s(&h->hal_flags);
2326 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2327 lustre_swab_hai(hai);
2330 static void lustre_swab_kuch(struct kuc_hdr *l)
2332 __swab16s(&l->kuc_magic);
2333 /* __u8 l->kuc_transport */
2334 __swab16s(&l->kuc_msgtype);
2335 __swab16s(&l->kuc_msglen);
2338 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2339 struct lustre_kernelcomm *lk)
2341 struct obd_import *imp = class_exp2cliimp(exp);
2344 if (lk->lk_group != KUC_GRP_HSM) {
2345 CERROR("Bad copytool group %d\n", lk->lk_group);
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);
2352 if (lk->lk_flags & LK_FLG_STOP) {
2353 /* Unregister with the coordinator */
2354 rc = mdc_ioc_hsm_ct_unregister(imp);
2356 __u32 *archives = NULL;
2358 if ((lk->lk_flags & LK_FLG_DATANR) && lk->lk_data_count > 0)
2359 archives = lk->lk_data;
2361 rc = mdc_ioc_hsm_ct_register(imp, lk->lk_data_count, archives);
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
2372 static int mdc_hsm_copytool_send(const struct obd_uuid *uuid,
2373 size_t len, void *val)
2375 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2376 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2380 if (len < sizeof(*lh) + sizeof(*hal)) {
2381 CERROR("Short HSM message %zu < %zu\n", len,
2382 sizeof(*lh) + sizeof(*hal));
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);
2393 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2395 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2396 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2398 /* Broadcast to HSM listeners */
2399 rc = libcfs_kkuc_group_put(uuid, KUC_GRP_HSM, lh);
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)
2410 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2412 struct obd_import *imp = (struct obd_import *)cb_arg;
2413 struct kkuc_ct_data *kcd = data;
2414 __u32 *archives = NULL;
2418 (kcd->kcd_magic != KKUC_CT_DATA_ARRAY_MAGIC &&
2419 kcd->kcd_magic != KKUC_CT_DATA_BITMAP_MAGIC))
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);
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;
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;
2440 * Re-establish all kuc contexts with MDT
2441 * after MDT shutdown/recovery.
2443 static int mdc_kuc_reregister(struct obd_import *imp)
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);
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)
2456 struct obd_import *imp = class_exp2cliimp(exp);
2460 if (KEY_IS(KEY_READ_ONLY)) {
2461 if (vallen != sizeof(int))
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 |=
2470 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2471 imp->imp_connect_data.ocd_connect_flags &=
2472 ~OBD_CONNECT_RDONLY;
2474 spin_unlock(&imp->imp_lock);
2476 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2477 keylen, key, vallen, val, set);
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);
2485 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2486 rc = mdc_hsm_copytool_send(&imp->imp_obd->obd_uuid, vallen,
2491 if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2492 __u32 *default_easize = val;
2494 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2498 rc = osc_set_info_async(env, exp, keylen, key, vallen, val, set);
2502 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2503 __u32 keylen, void *key, __u32 *vallen, void *val)
2507 if (KEY_IS(KEY_MAX_EASIZE)) {
2508 __u32 mdsize, *max_easize;
2510 if (*vallen != sizeof(int))
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;
2516 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2518 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2519 __u32 *default_easize;
2521 if (*vallen != sizeof(int))
2523 default_easize = val;
2524 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2526 } else if (KEY_IS(KEY_CONN_DATA)) {
2527 struct obd_import *imp = class_exp2cliimp(exp);
2528 struct obd_connect_data *data = val;
2530 if (*vallen != sizeof(*data))
2533 *data = imp->imp_connect_data;
2535 } else if (KEY_IS(KEY_TGT_COUNT)) {
2536 *((__u32 *)val) = 1;
2540 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2545 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2546 struct ptlrpc_request **request)
2548 struct ptlrpc_request *req;
2553 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2557 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2559 ptlrpc_request_free(req);
2563 mdc_pack_body(&req->rq_pill, fid, 0, 0, -1, 0);
2565 ptlrpc_request_set_replen(req);
2567 rc = ptlrpc_queue_wait(req);
2569 ptlrpc_req_finished(req);
2575 struct mdc_rmfid_args {
2580 int mdc_rmfid_interpret(const struct lu_env *env, struct ptlrpc_request *req,
2583 struct mdc_rmfid_args *aa;
2588 aa = ptlrpc_req_async_args(aa, req);
2590 size = req_capsule_get_size(&req->rq_pill, &RMF_RCS,
2592 LASSERT(size == sizeof(int) * aa->mra_nr);
2593 rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
2595 LASSERT(aa->mra_rcs);
2596 LASSERT(aa->mra_nr);
2597 memcpy(aa->mra_rcs, rcs, size);
2603 static int mdc_rmfid(struct obd_export *exp, struct fid_array *fa,
2604 int *rcs, struct ptlrpc_request_set *set)
2606 struct ptlrpc_request *req;
2607 struct mdc_rmfid_args *aa;
2613 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_RMFID);
2617 flen = fa->fa_nr * sizeof(struct lu_fid);
2618 req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
2620 req_capsule_set_size(&req->rq_pill, &RMF_FID_ARRAY,
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);
2626 ptlrpc_request_free(req);
2629 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FID_ARRAY);
2630 memcpy(tmp, fa->fa_fids, flen);
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();
2636 ptlrpc_request_set_replen(req);
2639 aa = ptlrpc_req_async_args(aa, req);
2641 aa->mra_nr = fa->fa_nr;
2642 req->rq_interpret_reply = mdc_rmfid_interpret;
2644 ptlrpc_set_add_req(set, req);
2645 ptlrpc_check_set(NULL, set);
2650 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2651 enum obd_import_event event)
2653 struct client_obd *cli = &obd->u.cli;
2656 LASSERT(imp->imp_obd == obd);
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);
2665 case IMP_EVENT_INACTIVE:
2667 * Flush current sequence to make client obtain new one
2668 * from server in case of disconnect/reconnect.
2670 down_read(&cli->cl_seq_rwsem);
2672 seq_client_flush(cli->cl_seq);
2673 up_read(&cli->cl_seq_rwsem);
2675 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE);
2677 case IMP_EVENT_INVALIDATE: {
2678 struct ldlm_namespace *ns = obd->obd_namespace;
2682 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2684 env = cl_env_get(&refcheck);
2686 /* Reset grants. All pages go to failing rpcs due to
2687 * the invalid import.
2689 osc_io_unplug(env, cli, NULL);
2691 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2692 osc_ldlm_resource_invalidate,
2694 cl_env_put(env, &refcheck);
2695 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2701 case IMP_EVENT_ACTIVE:
2702 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE);
2703 /* redo the kuc registration after reconnecting */
2705 rc = mdc_kuc_reregister(imp);
2707 case IMP_EVENT_OCD: {
2708 struct obd_connect_data *ocd = &imp->imp_connect_data;
2710 if (OCD_HAS_FLAG(ocd, GRANT))
2711 osc_init_grant(cli, ocd);
2713 md_init_ea_size(obd->obd_self_export, ocd->ocd_max_easize, 0);
2714 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD);
2717 case IMP_EVENT_DEACTIVATE:
2718 case IMP_EVENT_ACTIVATE:
2721 CERROR("Unknown import event %x\n", event);
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)
2730 struct client_obd *cli = &exp->exp_obd->u.cli;
2735 down_read(&cli->cl_seq_rwsem);
2737 rc = seq_client_alloc_fid(env, cli->cl_seq, fid);
2738 up_read(&cli->cl_seq_rwsem);
2743 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2745 struct client_obd *cli = &exp->exp_obd->u.cli;
2746 return &cli->cl_target_uuid;
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
2754 static int mdc_cancel_weight(struct ldlm_lock *lock)
2756 if (lock->l_resource->lr_type != LDLM_IBITS)
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)
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))
2774 static int mdc_resource_inode_free(struct ldlm_resource *res)
2776 if (res->lr_lvb_inode)
2777 res->lr_lvb_inode = NULL;
2782 static struct ldlm_valblock_ops inode_lvbo = {
2783 .lvbo_free = mdc_resource_inode_free
2786 static int mdc_llog_init(struct obd_device *obd)
2788 struct obd_llog_group *olg = &obd->obd_olg;
2789 struct llog_ctxt *ctxt;
2794 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2799 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2800 llog_initiator_connect(ctxt);
2801 llog_ctxt_put(ctxt);
2806 static void mdc_llog_finish(struct obd_device *obd)
2808 struct llog_ctxt *ctxt;
2812 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2814 llog_cleanup(NULL, ctxt);
2819 int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2825 rc = osc_setup_common(obd, cfg);
2829 rc = mdc_tunables_init(obd);
2831 GOTO(err_osc_cleanup, rc);
2833 obd->u.cli.cl_dom_min_inline_repsize = MDC_DOM_DEF_INLINE_REPSIZE;
2834 obd->u.cli.cl_lsom_update = true;
2836 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2838 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2840 rc = mdc_llog_init(obd);
2842 CERROR("%s: failed to setup llogging subsystems: rc = %d\n",
2844 GOTO(err_llog_cleanup, rc);
2847 rc = mdc_changelog_cdev_init(obd);
2849 CERROR("%s: failed to setup changelog char device: rc = %d\n",
2851 GOTO(err_changelog_cleanup, rc);
2856 err_changelog_cleanup:
2857 mdc_llog_finish(obd);
2859 lprocfs_free_md_stats(obd);
2860 ptlrpc_lprocfs_unregister_obd(obd);
2862 osc_cleanup_common(obd);
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.
2874 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2877 struct obd_device *obd = exp->exp_obd;
2878 struct client_obd *cli = &obd->u.cli;
2881 if (cli->cl_max_mds_easize < easize)
2882 cli->cl_max_mds_easize = easize;
2884 if (cli->cl_default_mds_easize < def_easize)
2885 cli->cl_default_mds_easize = def_easize;
2890 static int mdc_precleanup(struct obd_device *obd)
2894 osc_precleanup_common(obd);
2895 mdc_changelog_cdev_finish(obd);
2897 obd_cleanup_client_import(obd);
2898 ptlrpc_lprocfs_unregister_obd(obd);
2899 lprocfs_free_md_stats(obd);
2900 mdc_llog_finish(obd);
2904 static int mdc_cleanup(struct obd_device *obd)
2906 return osc_cleanup_common(obd);
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,
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,
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,
2963 dev_t mdc_changelog_dev;
2964 struct class *mdc_changelog_class;
2965 static int __init mdc_init(void)
2968 rc = alloc_chrdev_region(&mdc_changelog_dev, 0,
2969 MDC_CHANGELOG_DEV_COUNT,
2970 MDC_CHANGELOG_DEV_NAME);
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);
2980 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, true,
2981 LUSTRE_MDC_NAME, &mdc_device_type);
2988 class_destroy(mdc_changelog_class);
2990 unregister_chrdev_region(mdc_changelog_dev, MDC_CHANGELOG_DEV_COUNT);
2994 static void __exit mdc_exit(void)
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);
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");
3007 module_init(mdc_init);
3008 module_exit(mdc_exit);