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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_MDC
35 #include <linux/init.h>
36 #include <linux/kthread.h>
37 #include <linux/miscdevice.h>
38 #include <linux/module.h>
39 #include <linux/pagemap.h>
40 #include <linux/user_namespace.h>
41 #include <linux/utsname.h>
42 #ifdef HAVE_UIDGID_HEADER
43 # include <linux/uidgid.h>
46 #include <cl_object.h>
47 #include <llog_swab.h>
48 #include <lprocfs_status.h>
49 #include <lustre_acl.h>
50 #include <lustre_fid.h>
51 #include <lustre_ioctl.h>
52 #include <lustre_kernelcomm.h>
53 #include <lustre_lmv.h>
54 #include <lustre_log.h>
55 #include <lustre_param.h>
56 #include <lustre_swab.h>
57 #include <obd_class.h>
59 #include "mdc_internal.h"
61 #define REQUEST_MINOR 244
63 static int mdc_cleanup(struct obd_device *obd);
65 static inline int mdc_queue_wait(struct ptlrpc_request *req)
67 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
70 /* obd_get_request_slot() ensures that this client has no more
71 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
73 rc = obd_get_request_slot(cli);
77 rc = ptlrpc_queue_wait(req);
78 obd_put_request_slot(cli);
84 * Send MDS_GET_ROOT RPC to fetch root FID.
86 * If \a fileset is not NULL it should contain a subdirectory off
87 * the ROOT/ directory to be mounted on the client. Return the FID
88 * of the subdirectory to the client to mount onto its mountpoint.
90 * \param[in] imp MDC import
91 * \param[in] fileset fileset name, which could be NULL
92 * \param[out] rootfid root FID of this mountpoint
93 * \param[out] pc root capa will be unpacked and saved in this pointer
95 * \retval 0 on success, negative errno on failure
97 static int mdc_get_root(struct obd_export *exp, const char *fileset,
98 struct lu_fid *rootfid)
100 struct ptlrpc_request *req;
101 struct mdt_body *body;
106 if (fileset && !(exp_connect_flags(exp) & OBD_CONNECT_SUBTREE))
109 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
115 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
116 strlen(fileset) + 1);
117 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_ROOT);
119 ptlrpc_request_free(req);
122 mdc_pack_body(req, NULL, 0, 0, -1, 0);
123 if (fileset != NULL) {
124 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
126 memcpy(name, fileset, strlen(fileset));
128 lustre_msg_add_flags(req->rq_reqmsg, LUSTRE_IMP_FULL);
129 req->rq_send_state = LUSTRE_IMP_FULL;
131 ptlrpc_request_set_replen(req);
133 rc = ptlrpc_queue_wait(req);
137 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
139 GOTO(out, rc = -EPROTO);
141 *rootfid = body->mbo_fid1;
142 CDEBUG(D_NET, "root fid="DFID", last_committed=%llu\n",
143 PFID(rootfid), lustre_msg_get_last_committed(req->rq_repmsg));
146 ptlrpc_req_finished(req);
152 * This function now is known to always saying that it will receive 4 buffers
153 * from server. Even for cases when acl_size and md_size is zero, RPC header
154 * will contain 4 fields and RPC itself will contain zero size fields. This is
155 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
156 * and thus zero, it shrinks it, making zero size. The same story about
157 * md_size. And this is course of problem when client waits for smaller number
158 * of fields. This issue will be fixed later when client gets aware of RPC
161 static int mdc_getattr_common(struct obd_export *exp,
162 struct ptlrpc_request *req)
164 struct req_capsule *pill = &req->rq_pill;
165 struct mdt_body *body;
170 /* Request message already built. */
171 rc = ptlrpc_queue_wait(req);
175 /* sanity check for the reply */
176 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
180 CDEBUG(D_NET, "mode: %o\n", body->mbo_mode);
182 mdc_update_max_ea_from_body(exp, body);
183 if (body->mbo_eadatasize != 0) {
184 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
185 body->mbo_eadatasize);
193 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
194 struct ptlrpc_request **request)
196 struct ptlrpc_request *req;
200 /* Single MDS without an LMV case */
201 if (op_data->op_flags & MF_GET_MDT_IDX) {
206 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
210 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
212 ptlrpc_request_free(req);
216 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
217 op_data->op_mode, -1, 0);
219 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
221 ptlrpc_request_set_replen(req);
223 rc = mdc_getattr_common(exp, req);
225 ptlrpc_req_finished(req);
231 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
232 struct ptlrpc_request **request)
234 struct ptlrpc_request *req;
239 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
240 &RQF_MDS_GETATTR_NAME);
244 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
245 op_data->op_namelen + 1);
247 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
249 ptlrpc_request_free(req);
253 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
254 op_data->op_mode, op_data->op_suppgids[0], 0);
256 if (op_data->op_name) {
257 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
258 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
259 op_data->op_namelen);
260 memcpy(name, op_data->op_name, op_data->op_namelen);
263 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
265 ptlrpc_request_set_replen(req);
267 rc = mdc_getattr_common(exp, req);
269 ptlrpc_req_finished(req);
275 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
276 const struct lu_fid *fid, int opcode, u64 valid,
277 const char *xattr_name, const char *input,
278 int input_size, int output_size, int flags,
279 __u32 suppgid, struct ptlrpc_request **request)
281 struct ptlrpc_request *req;
282 int xattr_namelen = 0;
288 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
293 xattr_namelen = strlen(xattr_name) + 1;
294 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
299 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
303 /* Flush local XATTR locks to get rid of a possible cancel RPC */
304 if (opcode == MDS_REINT && fid_is_sane(fid) &&
305 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
306 struct list_head cancels = LIST_HEAD_INIT(cancels);
309 /* Without that packing would fail */
311 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
314 count = mdc_resource_get_unused(exp, fid,
316 MDS_INODELOCK_XATTR);
318 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
320 ptlrpc_request_free(req);
324 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
326 ptlrpc_request_free(req);
331 if (opcode == MDS_REINT) {
332 struct mdt_rec_setxattr *rec;
334 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
335 sizeof(struct mdt_rec_reint));
336 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
337 rec->sx_opcode = REINT_SETXATTR;
338 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
339 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
340 rec->sx_cap = cfs_curproc_cap_pack();
341 rec->sx_suppgid1 = suppgid;
342 rec->sx_suppgid2 = -1;
344 rec->sx_valid = valid | OBD_MD_FLCTIME;
345 rec->sx_time = cfs_time_current_sec();
346 rec->sx_size = output_size;
347 rec->sx_flags = flags;
349 mdc_pack_body(req, fid, valid, output_size, suppgid, flags);
353 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
354 memcpy(tmp, xattr_name, xattr_namelen);
357 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
358 memcpy(tmp, input, input_size);
361 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
362 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
363 RCL_SERVER, output_size);
364 ptlrpc_request_set_replen(req);
367 if (opcode == MDS_REINT)
368 mdc_get_mod_rpc_slot(req, NULL);
370 rc = ptlrpc_queue_wait(req);
372 if (opcode == MDS_REINT)
373 mdc_put_mod_rpc_slot(req, NULL);
376 ptlrpc_req_finished(req);
382 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
383 u64 valid, const char *xattr_name,
384 const char *input, int input_size, int output_size,
385 int flags, __u32 suppgid,
386 struct ptlrpc_request **request)
388 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
389 fid, MDS_REINT, valid, xattr_name,
390 input, input_size, output_size, flags,
394 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
395 u64 valid, const char *xattr_name,
396 const char *input, int input_size, int output_size,
397 int flags, struct ptlrpc_request **request)
399 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
400 fid, MDS_GETXATTR, valid, xattr_name,
401 input, input_size, output_size, flags,
405 #ifdef CONFIG_FS_POSIX_ACL
406 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
408 struct req_capsule *pill = &req->rq_pill;
409 struct mdt_body *body = md->body;
410 struct posix_acl *acl;
415 if (!body->mbo_aclsize)
418 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->mbo_aclsize);
423 acl = posix_acl_from_xattr(&init_user_ns, buf, body->mbo_aclsize);
428 CERROR("convert xattr to acl: %d\n", rc);
432 rc = posix_acl_valid(acl);
434 CERROR("validate acl: %d\n", rc);
435 posix_acl_release(acl);
443 #define mdc_unpack_acl(req, md) 0
446 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
447 struct obd_export *dt_exp, struct obd_export *md_exp,
448 struct lustre_md *md)
450 struct req_capsule *pill = &req->rq_pill;
455 memset(md, 0, sizeof(*md));
457 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
458 LASSERT(md->body != NULL);
460 if (md->body->mbo_valid & OBD_MD_FLEASIZE) {
461 if (!S_ISREG(md->body->mbo_mode)) {
462 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
463 "regular file, but is not\n");
464 GOTO(out, rc = -EPROTO);
467 if (md->body->mbo_eadatasize == 0) {
468 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
469 "but eadatasize 0\n");
470 GOTO(out, rc = -EPROTO);
473 md->layout.lb_len = md->body->mbo_eadatasize;
474 md->layout.lb_buf = req_capsule_server_sized_get(pill,
477 if (md->layout.lb_buf == NULL)
478 GOTO(out, rc = -EPROTO);
479 } else if (md->body->mbo_valid & OBD_MD_FLDIREA) {
480 const union lmv_mds_md *lmv;
483 if (!S_ISDIR(md->body->mbo_mode)) {
484 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
485 "directory, but is not\n");
486 GOTO(out, rc = -EPROTO);
489 lmv_size = md->body->mbo_eadatasize;
491 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
492 "but eadatasize 0\n");
496 if (md->body->mbo_valid & OBD_MD_MEA) {
497 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
500 GOTO(out, rc = -EPROTO);
502 rc = md_unpackmd(md_exp, &md->lmv, lmv, lmv_size);
506 if (rc < (typeof(rc))sizeof(*md->lmv)) {
507 CDEBUG(D_INFO, "size too small: "
508 "rc < sizeof(*md->lmv) (%d < %d)\n",
509 rc, (int)sizeof(*md->lmv));
510 GOTO(out, rc = -EPROTO);
516 if (md->body->mbo_valid & OBD_MD_FLACL) {
517 /* for ACL, it's possible that FLACL is set but aclsize is zero.
518 * only when aclsize != 0 there's an actual segment for ACL
521 if (md->body->mbo_aclsize) {
522 rc = mdc_unpack_acl(req, md);
525 #ifdef CONFIG_FS_POSIX_ACL
527 md->posix_acl = NULL;
535 #ifdef CONFIG_FS_POSIX_ACL
536 posix_acl_release(md->posix_acl);
542 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
548 void mdc_replay_open(struct ptlrpc_request *req)
550 struct md_open_data *mod = req->rq_cb_data;
551 struct ptlrpc_request *close_req;
552 struct obd_client_handle *och;
553 struct lustre_handle old;
554 struct mdt_body *body;
558 DEBUG_REQ(D_ERROR, req,
559 "Can't properly replay without open data.");
564 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
565 LASSERT(body != NULL);
569 struct lustre_handle *file_fh;
571 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
573 file_fh = &och->och_fh;
574 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
575 file_fh->cookie, body->mbo_handle.cookie);
577 *file_fh = body->mbo_handle;
579 close_req = mod->mod_close_req;
580 if (close_req != NULL) {
581 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
582 struct mdt_ioepoch *epoch;
584 LASSERT(opc == MDS_CLOSE);
585 epoch = req_capsule_client_get(&close_req->rq_pill,
590 LASSERT(!memcmp(&old, &epoch->mio_handle, sizeof(old)));
592 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
593 epoch->mio_handle = body->mbo_handle;
598 void mdc_commit_open(struct ptlrpc_request *req)
600 struct md_open_data *mod = req->rq_cb_data;
605 * No need to touch md_open_data::mod_och, it holds a reference on
606 * \var mod and will zero references to each other, \var mod will be
607 * freed after that when md_open_data::mod_och will put the reference.
611 * Do not let open request to disappear as it still may be needed
612 * for close rpc to happen (it may happen on evict only, otherwise
613 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
614 * called), just mark this rpc as committed to distinguish these 2
615 * cases, see mdc_close() for details. The open request reference will
616 * be put along with freeing \var mod.
618 ptlrpc_request_addref(req);
619 spin_lock(&req->rq_lock);
620 req->rq_committed = 1;
621 spin_unlock(&req->rq_lock);
622 req->rq_cb_data = NULL;
626 int mdc_set_open_replay_data(struct obd_export *exp,
627 struct obd_client_handle *och,
628 struct lookup_intent *it)
630 struct md_open_data *mod;
631 struct mdt_rec_create *rec;
632 struct mdt_body *body;
633 struct ptlrpc_request *open_req = it->it_request;
634 struct obd_import *imp = open_req->rq_import;
637 if (!open_req->rq_replay)
640 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
641 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
642 LASSERT(rec != NULL);
643 /* Incoming message in my byte order (it's been swabbed). */
644 /* Outgoing messages always in my byte order. */
645 LASSERT(body != NULL);
647 /* Only if the import is replayable, we set replay_open data */
648 if (och && imp->imp_replayable) {
649 mod = obd_mod_alloc();
651 DEBUG_REQ(D_ERROR, open_req,
652 "Can't allocate md_open_data");
657 * Take a reference on \var mod, to be freed on mdc_close().
658 * It protects \var mod from being freed on eviction (commit
659 * callback is called despite rq_replay flag).
660 * Another reference for \var och.
665 spin_lock(&open_req->rq_lock);
668 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
669 it_disposition(it, DISP_OPEN_STRIPE);
670 mod->mod_open_req = open_req;
671 open_req->rq_cb_data = mod;
672 open_req->rq_commit_cb = mdc_commit_open;
673 spin_unlock(&open_req->rq_lock);
676 rec->cr_fid2 = body->mbo_fid1;
677 rec->cr_ioepoch = body->mbo_ioepoch;
678 rec->cr_old_handle.cookie = body->mbo_handle.cookie;
679 open_req->rq_replay_cb = mdc_replay_open;
680 if (!fid_is_sane(&body->mbo_fid1)) {
681 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
686 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
690 static void mdc_free_open(struct md_open_data *mod)
694 if (mod->mod_is_create == 0 &&
695 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
699 * No reason to asssert here if the open request has
700 * rq_replay == 1. It means that mdc_close failed, and
701 * close request wasn`t sent. It is not fatal to client.
702 * The worst thing is eviction if the client gets open lock
705 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request rq_replay"
706 "= %d\n", mod->mod_open_req->rq_replay);
708 ptlrpc_request_committed(mod->mod_open_req, committed);
709 if (mod->mod_close_req)
710 ptlrpc_request_committed(mod->mod_close_req, committed);
713 int mdc_clear_open_replay_data(struct obd_export *exp,
714 struct obd_client_handle *och)
716 struct md_open_data *mod = och->och_mod;
720 * It is possible to not have \var mod in a case of eviction between
721 * lookup and ll_file_open().
726 LASSERT(mod != LP_POISON);
727 LASSERT(mod->mod_open_req != NULL);
737 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
738 struct md_open_data *mod, struct ptlrpc_request **request)
740 struct obd_device *obd = class_exp2obd(exp);
741 struct ptlrpc_request *req;
742 struct req_format *req_fmt;
747 if (op_data->op_bias & MDS_HSM_RELEASE) {
748 req_fmt = &RQF_MDS_INTENT_CLOSE;
750 /* allocate a FID for volatile file */
751 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
753 CERROR("%s: "DFID" failed to allocate FID: %d\n",
754 obd->obd_name, PFID(&op_data->op_fid1), rc);
755 /* save the errcode and proceed to close */
758 } else if (op_data->op_bias & MDS_CLOSE_LAYOUT_SWAP) {
759 req_fmt = &RQF_MDS_INTENT_CLOSE;
761 req_fmt = &RQF_MDS_CLOSE;
765 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_CLOSE))
768 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
770 /* Ensure that this close's handle is fixed up during replay. */
771 if (likely(mod != NULL)) {
772 LASSERTF(mod->mod_open_req != NULL &&
773 mod->mod_open_req->rq_type != LI_POISON,
774 "POISONED open %p!\n", mod->mod_open_req);
776 mod->mod_close_req = req;
778 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
779 /* We no longer want to preserve this open for replay even
780 * though the open was committed. b=3632, b=3633 */
781 spin_lock(&mod->mod_open_req->rq_lock);
782 mod->mod_open_req->rq_replay = 0;
783 spin_unlock(&mod->mod_open_req->rq_lock);
785 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
789 * TODO: repeat close after errors
791 CWARN("%s: close of FID "DFID" failed, file reference will be "
792 "dropped when this client unmounts or is evicted\n",
793 obd->obd_name, PFID(&op_data->op_fid1));
794 GOTO(out, rc = -ENOMEM);
797 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
799 ptlrpc_request_free(req);
803 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
804 * portal whose threads are not taking any DLM locks and are therefore
805 * always progressing */
806 req->rq_request_portal = MDS_READPAGE_PORTAL;
807 ptlrpc_at_set_req_timeout(req);
810 mdc_close_pack(req, op_data);
812 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
813 obd->u.cli.cl_default_mds_easize);
815 ptlrpc_request_set_replen(req);
817 mdc_get_mod_rpc_slot(req, NULL);
818 rc = ptlrpc_queue_wait(req);
819 mdc_put_mod_rpc_slot(req, NULL);
821 if (req->rq_repmsg == NULL) {
822 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
825 rc = req->rq_status ?: -EIO;
826 } else if (rc == 0 || rc == -EAGAIN) {
827 struct mdt_body *body;
829 rc = lustre_msg_get_status(req->rq_repmsg);
830 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
831 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
836 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
839 } else if (rc == -ESTALE) {
841 * it can be allowed error after 3633 if open was committed and
842 * server failed before close was sent. Let's check if mod
843 * exists and return no error in that case
846 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
847 LASSERT(mod->mod_open_req != NULL);
848 if (mod->mod_open_req->rq_committed)
856 mod->mod_close_req = NULL;
857 /* Since now, mod is accessed through open_req only,
858 * thus close req does not keep a reference on mod anymore. */
863 RETURN(rc < 0 ? rc : saved_rc);
866 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
867 u64 offset, struct page **pages, int npages,
868 struct ptlrpc_request **request)
870 struct ptlrpc_request *req;
871 struct ptlrpc_bulk_desc *desc;
873 wait_queue_head_t waitq;
875 struct l_wait_info lwi;
880 init_waitqueue_head(&waitq);
883 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
887 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
889 ptlrpc_request_free(req);
893 req->rq_request_portal = MDS_READPAGE_PORTAL;
894 ptlrpc_at_set_req_timeout(req);
896 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
897 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
899 &ptlrpc_bulk_kiov_pin_ops);
901 ptlrpc_request_free(req);
905 /* NB req now owns desc and will free it when it gets freed */
906 for (i = 0; i < npages; i++)
907 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
910 mdc_readdir_pack(req, offset, PAGE_SIZE * npages, fid);
912 ptlrpc_request_set_replen(req);
913 rc = ptlrpc_queue_wait(req);
915 ptlrpc_req_finished(req);
916 if (rc != -ETIMEDOUT)
920 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
921 CERROR("%s: too many resend retries: rc = %d\n",
922 exp->exp_obd->obd_name, -EIO);
925 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL,
927 l_wait_event(waitq, 0, &lwi);
932 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
933 req->rq_bulk->bd_nob_transferred);
935 ptlrpc_req_finished(req);
939 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
940 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
941 exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
943 ptlrpc_req_finished(req);
951 static void mdc_release_page(struct page *page, int remove)
955 if (likely(page->mapping != NULL))
956 truncate_complete_page(page->mapping, page);
962 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
963 __u64 *start, __u64 *end, int hash64)
966 * Complement of hash is used as an index so that
967 * radix_tree_gang_lookup() can be used to find a page with starting
968 * hash _smaller_ than one we are looking for.
970 unsigned long offset = hash_x_index(*hash, hash64);
974 spin_lock_irq(&mapping->tree_lock);
975 found = radix_tree_gang_lookup(&mapping->page_tree,
976 (void **)&page, offset, 1);
977 if (found > 0 && !radix_tree_exceptional_entry(page)) {
978 struct lu_dirpage *dp;
981 spin_unlock_irq(&mapping->tree_lock);
983 * In contrast to find_lock_page() we are sure that directory
984 * page cannot be truncated (while DLM lock is held) and,
985 * hence, can avoid restart.
987 * In fact, page cannot be locked here at all, because
988 * mdc_read_page_remote does synchronous io.
990 wait_on_page_locked(page);
991 if (PageUptodate(page)) {
993 if (BITS_PER_LONG == 32 && hash64) {
994 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
995 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
998 *start = le64_to_cpu(dp->ldp_hash_start);
999 *end = le64_to_cpu(dp->ldp_hash_end);
1001 if (unlikely(*start == 1 && *hash == 0))
1004 LASSERTF(*start <= *hash, "start = %#llx"
1005 ",end = %#llx,hash = %#llx\n",
1006 *start, *end, *hash);
1007 CDEBUG(D_VFSTRACE, "offset %lx [%#llx %#llx],"
1008 " hash %#llx\n", offset, *start, *end, *hash);
1011 mdc_release_page(page, 0);
1013 } else if (*end != *start && *hash == *end) {
1015 * upon hash collision, remove this page,
1016 * otherwise put page reference, and
1017 * mdc_read_page_remote() will issue RPC to
1018 * fetch the page we want.
1021 mdc_release_page(page,
1022 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1027 page = ERR_PTR(-EIO);
1030 spin_unlock_irq(&mapping->tree_lock);
1037 * Adjust a set of pages, each page containing an array of lu_dirpages,
1038 * so that each page can be used as a single logical lu_dirpage.
1040 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1041 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1042 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
1043 * value is used as a cookie to request the next lu_dirpage in a
1044 * directory listing that spans multiple pages (two in this example):
1047 * .|--------v------- -----.
1048 * |s|e|f|p|ent|ent| ... |ent|
1049 * '--|-------------- -----' Each PAGE contains a single
1050 * '------. lu_dirpage.
1051 * .---------v------- -----.
1052 * |s|e|f|p|ent| 0 | ... | 0 |
1053 * '----------------- -----'
1055 * However, on hosts where the native VM page size (PAGE_SIZE) is
1056 * larger than LU_PAGE_SIZE, a single host page may contain multiple
1057 * lu_dirpages. After reading the lu_dirpages from the MDS, the
1058 * ldp_hash_end of the first lu_dirpage refers to the one immediately
1059 * after it in the same PAGE (arrows simplified for brevity, but
1060 * in general e0==s1, e1==s2, etc.):
1062 * .-------------------- -----.
1063 * |s0|e0|f0|p|ent|ent| ... |ent|
1064 * |---v---------------- -----|
1065 * |s1|e1|f1|p|ent|ent| ... |ent|
1066 * |---v---------------- -----| Here, each PAGE contains
1067 * ... multiple lu_dirpages.
1068 * |---v---------------- -----|
1069 * |s'|e'|f'|p|ent|ent| ... |ent|
1070 * '---|---------------- -----'
1072 * .----------------------------.
1075 * This structure is transformed into a single logical lu_dirpage as follows:
1077 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1078 * labeled 'next PAGE'.
1080 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1081 * a hash collision with the next page exists.
1083 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1084 * to the first entry of the next lu_dirpage.
1086 #if PAGE_SIZE > LU_PAGE_SIZE
1087 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1091 for (i = 0; i < cfs_pgs; i++) {
1092 struct lu_dirpage *dp = kmap(pages[i]);
1093 struct lu_dirpage *first = dp;
1094 struct lu_dirent *end_dirent = NULL;
1095 struct lu_dirent *ent;
1096 __u64 hash_end = le64_to_cpu(dp->ldp_hash_end);
1097 __u32 flags = le32_to_cpu(dp->ldp_flags);
1099 while (--lu_pgs > 0) {
1100 ent = lu_dirent_start(dp);
1101 for (end_dirent = ent; ent != NULL;
1102 end_dirent = ent, ent = lu_dirent_next(ent));
1104 /* Advance dp to next lu_dirpage. */
1105 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1107 /* Check if we've reached the end of the PAGE. */
1108 if (!((unsigned long)dp & ~PAGE_MASK))
1111 /* Save the hash and flags of this lu_dirpage. */
1112 hash_end = le64_to_cpu(dp->ldp_hash_end);
1113 flags = le32_to_cpu(dp->ldp_flags);
1115 /* Check if lu_dirpage contains no entries. */
1116 if (end_dirent == NULL)
1119 /* Enlarge the end entry lde_reclen from 0 to
1120 * first entry of next lu_dirpage. */
1121 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1122 end_dirent->lde_reclen =
1123 cpu_to_le16((char *)(dp->ldp_entries) -
1124 (char *)end_dirent);
1127 first->ldp_hash_end = hash_end;
1128 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1129 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1133 LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1136 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1137 #endif /* PAGE_SIZE > LU_PAGE_SIZE */
1139 /* parameters for readdir page */
1140 struct readpage_param {
1141 struct md_op_data *rp_mod;
1144 struct obd_export *rp_exp;
1145 struct md_callback *rp_cb;
1148 #ifndef HAVE_DELETE_FROM_PAGE_CACHE
1149 static inline void delete_from_page_cache(struct page *page)
1151 remove_from_page_cache(page);
1157 * Read pages from server.
1159 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1160 * a header lu_dirpage which describes the start/end hash, and whether this
1161 * page is empty (contains no dir entry) or hash collide with next page.
1162 * After client receives reply, several pages will be integrated into dir page
1163 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1164 * lu_dirpage for this integrated page will be adjusted.
1166 static int mdc_read_page_remote(void *data, struct page *page0)
1168 struct readpage_param *rp = data;
1169 struct page **page_pool;
1171 struct lu_dirpage *dp;
1172 int rd_pgs = 0; /* number of pages read actually */
1174 struct md_op_data *op_data = rp->rp_mod;
1175 struct ptlrpc_request *req;
1176 int max_pages = op_data->op_max_pages;
1177 struct inode *inode;
1183 LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
1184 inode = op_data->op_data;
1185 fid = &op_data->op_fid1;
1186 LASSERT(inode != NULL);
1188 OBD_ALLOC(page_pool, sizeof(page_pool[0]) * max_pages);
1189 if (page_pool != NULL) {
1190 page_pool[0] = page0;
1196 for (npages = 1; npages < max_pages; npages++) {
1197 page = page_cache_alloc_cold(inode->i_mapping);
1200 page_pool[npages] = page;
1203 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, page_pool, npages, &req);
1205 /* page0 is special, which was added into page cache early */
1206 delete_from_page_cache(page0);
1210 rd_pgs = (req->rq_bulk->bd_nob_transferred +
1211 PAGE_SIZE - 1) >> PAGE_SHIFT;
1212 lu_pgs = req->rq_bulk->bd_nob_transferred >>
1214 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1216 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1218 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1220 SetPageUptodate(page0);
1224 ptlrpc_req_finished(req);
1225 CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1226 for (i = 1; i < npages; i++) {
1227 unsigned long offset;
1231 page = page_pool[i];
1233 if (rc < 0 || i >= rd_pgs) {
1238 SetPageUptodate(page);
1241 hash = le64_to_cpu(dp->ldp_hash_start);
1244 offset = hash_x_index(hash, rp->rp_hash64);
1246 prefetchw(&page->flags);
1247 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1252 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1253 " rc = %d\n", offset, ret);
1257 if (page_pool != &page0)
1258 OBD_FREE(page_pool, sizeof(page_pool[0]) * max_pages);
1264 * Read dir page from cache first, if it can not find it, read it from
1265 * server and add into the cache.
1267 * \param[in] exp MDC export
1268 * \param[in] op_data client MD stack parameters, transfering parameters
1269 * between different layers on client MD stack.
1270 * \param[in] cb_op callback required for ldlm lock enqueue during
1272 * \param[in] hash_offset the hash offset of the page to be read
1273 * \param[in] ppage the page to be read
1275 * retval = 0 get the page successfully
1276 * errno(<0) get the page failed
1278 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1279 struct md_callback *cb_op, __u64 hash_offset,
1280 struct page **ppage)
1282 struct lookup_intent it = { .it_op = IT_READDIR };
1284 struct inode *dir = op_data->op_data;
1285 struct address_space *mapping;
1286 struct lu_dirpage *dp;
1289 struct lustre_handle lockh;
1290 struct ptlrpc_request *enq_req = NULL;
1291 struct readpage_param rp_param;
1298 LASSERT(dir != NULL);
1299 mapping = dir->i_mapping;
1301 rc = mdc_intent_lock(exp, op_data, &it, &enq_req,
1302 cb_op->md_blocking_ast, 0);
1303 if (enq_req != NULL)
1304 ptlrpc_req_finished(enq_req);
1307 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1308 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1313 lockh.cookie = it.it_lock_handle;
1314 mdc_set_lock_data(exp, &lockh, dir, NULL);
1316 rp_param.rp_off = hash_offset;
1317 rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1318 page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1319 rp_param.rp_hash64);
1321 CERROR("%s: dir page locate: "DFID" at %llu: rc %ld\n",
1322 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1323 rp_param.rp_off, PTR_ERR(page));
1324 GOTO(out_unlock, rc = PTR_ERR(page));
1325 } else if (page != NULL) {
1327 * XXX nikita: not entirely correct handling of a corner case:
1328 * suppose hash chain of entries with hash value HASH crosses
1329 * border between pages P0 and P1. First both P0 and P1 are
1330 * cached, seekdir() is called for some entry from the P0 part
1331 * of the chain. Later P0 goes out of cache. telldir(HASH)
1332 * happens and finds P1, as it starts with matching hash
1333 * value. Remaining entries from P0 part of the chain are
1334 * skipped. (Is that really a bug?)
1336 * Possible solutions: 0. don't cache P1 is such case, handle
1337 * it as an "overflow" page. 1. invalidate all pages at
1338 * once. 2. use HASH|1 as an index for P1.
1340 GOTO(hash_collision, page);
1343 rp_param.rp_exp = exp;
1344 rp_param.rp_mod = op_data;
1345 page = read_cache_page(mapping,
1346 hash_x_index(rp_param.rp_off,
1347 rp_param.rp_hash64),
1348 mdc_read_page_remote, &rp_param);
1350 CDEBUG(D_INFO, "%s: read cache page: "DFID" at %llu: %ld\n",
1351 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1352 rp_param.rp_off, PTR_ERR(page));
1353 GOTO(out_unlock, rc = PTR_ERR(page));
1356 wait_on_page_locked(page);
1358 if (!PageUptodate(page)) {
1359 CERROR("%s: page not updated: "DFID" at %llu: rc %d\n",
1360 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1361 rp_param.rp_off, -5);
1364 if (!PageChecked(page))
1365 SetPageChecked(page);
1366 if (PageError(page)) {
1367 CERROR("%s: page error: "DFID" at %llu: rc %d\n",
1368 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1369 rp_param.rp_off, -5);
1374 dp = page_address(page);
1375 if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1376 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1377 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1378 rp_param.rp_off = hash_offset >> 32;
1380 start = le64_to_cpu(dp->ldp_hash_start);
1381 end = le64_to_cpu(dp->ldp_hash_end);
1382 rp_param.rp_off = hash_offset;
1385 LASSERT(start == rp_param.rp_off);
1386 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1387 #if BITS_PER_LONG == 32
1388 CWARN("Real page-wide hash collision at [%llu %llu] with "
1389 "hash %llu\n", le64_to_cpu(dp->ldp_hash_start),
1390 le64_to_cpu(dp->ldp_hash_end), hash_offset);
1394 * Fetch whole overflow chain...
1402 ldlm_lock_decref(&lockh, it.it_lock_mode);
1406 mdc_release_page(page, 1);
1412 static int mdc_statfs(const struct lu_env *env,
1413 struct obd_export *exp, struct obd_statfs *osfs,
1414 __u64 max_age, __u32 flags)
1416 struct obd_device *obd = class_exp2obd(exp);
1417 struct ptlrpc_request *req;
1418 struct obd_statfs *msfs;
1419 struct obd_import *imp = NULL;
1424 * Since the request might also come from lprocfs, so we need
1425 * sync this with client_disconnect_export Bug15684
1427 down_read(&obd->u.cli.cl_sem);
1428 if (obd->u.cli.cl_import)
1429 imp = class_import_get(obd->u.cli.cl_import);
1430 up_read(&obd->u.cli.cl_sem);
1434 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1435 LUSTRE_MDS_VERSION, MDS_STATFS);
1437 GOTO(output, rc = -ENOMEM);
1439 ptlrpc_request_set_replen(req);
1441 if (flags & OBD_STATFS_NODELAY) {
1442 /* procfs requests not want stay in wait for avoid deadlock */
1443 req->rq_no_resend = 1;
1444 req->rq_no_delay = 1;
1447 rc = ptlrpc_queue_wait(req);
1449 /* check connection error first */
1450 if (imp->imp_connect_error)
1451 rc = imp->imp_connect_error;
1455 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1457 GOTO(out, rc = -EPROTO);
1462 ptlrpc_req_finished(req);
1464 class_import_put(imp);
1468 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1470 __u32 keylen, vallen;
1474 if (gf->gf_pathlen > PATH_MAX)
1475 RETURN(-ENAMETOOLONG);
1476 if (gf->gf_pathlen < 2)
1479 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1480 keylen = cfs_size_round(sizeof(KEY_FID2PATH) + sizeof(*gf) +
1481 sizeof(struct lu_fid));
1482 OBD_ALLOC(key, keylen);
1485 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1486 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1487 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf),
1488 gf->gf_u.gf_root_fid, sizeof(struct lu_fid));
1489 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1490 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1492 if (!fid_is_sane(&gf->gf_fid))
1493 GOTO(out, rc = -EINVAL);
1495 /* Val is struct getinfo_fid2path result plus path */
1496 vallen = sizeof(*gf) + gf->gf_pathlen;
1498 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf);
1499 if (rc != 0 && rc != -EREMOTE)
1502 if (vallen <= sizeof(*gf))
1503 GOTO(out, rc = -EPROTO);
1504 if (vallen > sizeof(*gf) + gf->gf_pathlen)
1505 GOTO(out, rc = -EOVERFLOW);
1507 CDEBUG(D_IOCTL, "path got "DFID" from %llu #%d: %s\n",
1508 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno,
1509 gf->gf_pathlen < 512 ? gf->gf_u.gf_path :
1510 /* only log the last 512 characters of the path */
1511 gf->gf_u.gf_path + gf->gf_pathlen - 512);
1514 OBD_FREE(key, keylen);
1518 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1519 struct hsm_progress_kernel *hpk)
1521 struct obd_import *imp = class_exp2cliimp(exp);
1522 struct hsm_progress_kernel *req_hpk;
1523 struct ptlrpc_request *req;
1527 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1528 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1530 GOTO(out, rc = -ENOMEM);
1532 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1534 /* Copy hsm_progress struct */
1535 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1536 if (req_hpk == NULL)
1537 GOTO(out, rc = -EPROTO);
1540 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1542 ptlrpc_request_set_replen(req);
1544 mdc_get_mod_rpc_slot(req, NULL);
1545 rc = ptlrpc_queue_wait(req);
1546 mdc_put_mod_rpc_slot(req, NULL);
1550 ptlrpc_req_finished(req);
1554 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1556 __u32 *archive_mask;
1557 struct ptlrpc_request *req;
1561 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1563 MDS_HSM_CT_REGISTER);
1565 GOTO(out, rc = -ENOMEM);
1567 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1569 /* Copy hsm_progress struct */
1570 archive_mask = req_capsule_client_get(&req->rq_pill,
1571 &RMF_MDS_HSM_ARCHIVE);
1572 if (archive_mask == NULL)
1573 GOTO(out, rc = -EPROTO);
1575 *archive_mask = archives;
1577 ptlrpc_request_set_replen(req);
1579 rc = mdc_queue_wait(req);
1582 ptlrpc_req_finished(req);
1586 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1587 struct md_op_data *op_data)
1589 struct hsm_current_action *hca = op_data->op_data;
1590 struct hsm_current_action *req_hca;
1591 struct ptlrpc_request *req;
1595 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1596 &RQF_MDS_HSM_ACTION);
1600 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1602 ptlrpc_request_free(req);
1606 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1607 op_data->op_suppgids[0], 0);
1609 ptlrpc_request_set_replen(req);
1611 rc = mdc_queue_wait(req);
1615 req_hca = req_capsule_server_get(&req->rq_pill,
1616 &RMF_MDS_HSM_CURRENT_ACTION);
1617 if (req_hca == NULL)
1618 GOTO(out, rc = -EPROTO);
1624 ptlrpc_req_finished(req);
1628 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1630 struct ptlrpc_request *req;
1634 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1636 MDS_HSM_CT_UNREGISTER);
1638 GOTO(out, rc = -ENOMEM);
1640 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1642 ptlrpc_request_set_replen(req);
1644 rc = mdc_queue_wait(req);
1647 ptlrpc_req_finished(req);
1651 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1652 struct md_op_data *op_data)
1654 struct hsm_user_state *hus = op_data->op_data;
1655 struct hsm_user_state *req_hus;
1656 struct ptlrpc_request *req;
1660 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1661 &RQF_MDS_HSM_STATE_GET);
1665 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1667 ptlrpc_request_free(req);
1671 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1672 op_data->op_suppgids[0], 0);
1674 ptlrpc_request_set_replen(req);
1676 rc = mdc_queue_wait(req);
1680 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1681 if (req_hus == NULL)
1682 GOTO(out, rc = -EPROTO);
1688 ptlrpc_req_finished(req);
1692 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1693 struct md_op_data *op_data)
1695 struct hsm_state_set *hss = op_data->op_data;
1696 struct hsm_state_set *req_hss;
1697 struct ptlrpc_request *req;
1701 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1702 &RQF_MDS_HSM_STATE_SET);
1706 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1708 ptlrpc_request_free(req);
1712 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1713 op_data->op_suppgids[0], 0);
1716 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1717 if (req_hss == NULL)
1718 GOTO(out, rc = -EPROTO);
1721 ptlrpc_request_set_replen(req);
1723 mdc_get_mod_rpc_slot(req, NULL);
1724 rc = ptlrpc_queue_wait(req);
1725 mdc_put_mod_rpc_slot(req, NULL);
1729 ptlrpc_req_finished(req);
1733 static int mdc_ioc_hsm_request(struct obd_export *exp,
1734 struct hsm_user_request *hur)
1736 struct obd_import *imp = class_exp2cliimp(exp);
1737 struct ptlrpc_request *req;
1738 struct hsm_request *req_hr;
1739 struct hsm_user_item *req_hui;
1744 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1746 GOTO(out, rc = -ENOMEM);
1748 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1749 hur->hur_request.hr_itemcount
1750 * sizeof(struct hsm_user_item));
1751 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1752 hur->hur_request.hr_data_len);
1754 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1756 ptlrpc_request_free(req);
1760 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1762 /* Copy hsm_request struct */
1763 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1765 GOTO(out, rc = -EPROTO);
1766 *req_hr = hur->hur_request;
1768 /* Copy hsm_user_item structs */
1769 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1770 if (req_hui == NULL)
1771 GOTO(out, rc = -EPROTO);
1772 memcpy(req_hui, hur->hur_user_item,
1773 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1775 /* Copy opaque field */
1776 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1777 if (req_opaque == NULL)
1778 GOTO(out, rc = -EPROTO);
1779 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1781 ptlrpc_request_set_replen(req);
1783 mdc_get_mod_rpc_slot(req, NULL);
1784 rc = ptlrpc_queue_wait(req);
1785 mdc_put_mod_rpc_slot(req, NULL);
1790 ptlrpc_req_finished(req);
1794 static struct kuc_hdr *changelog_kuc_hdr(char *buf, size_t len, __u32 flags)
1796 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1798 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1800 lh->kuc_magic = KUC_MAGIC;
1801 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1802 lh->kuc_flags = flags;
1803 lh->kuc_msgtype = CL_RECORD;
1804 lh->kuc_msglen = len;
1808 struct changelog_show {
1810 enum changelog_send_flag cs_flags;
1813 struct obd_device *cs_obd;
1816 static inline char *cs_obd_name(struct changelog_show *cs)
1818 return cs->cs_obd->obd_name;
1821 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1822 struct llog_rec_hdr *hdr, void *data)
1824 struct changelog_show *cs = data;
1825 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1831 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1833 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1834 cs_obd_name(cs), rec->cr_hdr.lrh_type,
1835 rec->cr.cr_type, rc);
1839 if (rec->cr.cr_index < cs->cs_startrec) {
1840 /* Skip entries earlier than what we are interested in */
1841 CDEBUG(D_HSM, "rec=%llu start=%llu\n",
1842 rec->cr.cr_index, cs->cs_startrec);
1846 CDEBUG(D_HSM, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID" %.*s\n",
1847 rec->cr.cr_index, rec->cr.cr_type,
1848 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1849 rec->cr.cr_flags & CLF_FLAGMASK,
1850 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1851 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1853 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1855 /* Set up the message */
1856 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1857 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1859 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1860 CDEBUG(D_HSM, "kucmsg fp %p len %zu rc %d\n", cs->cs_fp, len, rc);
1865 static int mdc_changelog_send_thread(void *csdata)
1867 struct changelog_show *cs = csdata;
1868 struct llog_ctxt *ctxt = NULL;
1869 struct llog_handle *llh = NULL;
1870 struct kuc_hdr *kuch;
1871 enum llog_flag flags = LLOG_F_IS_CAT;
1874 CDEBUG(D_HSM, "changelog to fp=%p start %llu\n",
1875 cs->cs_fp, cs->cs_startrec);
1877 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1878 if (cs->cs_buf == NULL)
1879 GOTO(out, rc = -ENOMEM);
1881 /* Set up the remote catalog handle */
1882 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1884 GOTO(out, rc = -ENOENT);
1885 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1888 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1889 cs_obd_name(cs), rc);
1893 if (cs->cs_flags & CHANGELOG_FLAG_JOBID)
1894 flags |= LLOG_F_EXT_JOBID;
1896 rc = llog_init_handle(NULL, llh, flags, NULL);
1898 CERROR("llog_init_handle failed %d\n", rc);
1902 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1904 /* Send EOF no matter what our result */
1905 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1906 kuch->kuc_msgtype = CL_EOF;
1907 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1912 llog_cat_close(NULL, llh);
1914 llog_ctxt_put(ctxt);
1916 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1921 static int mdc_ioc_changelog_send(struct obd_device *obd,
1922 struct ioc_changelog *icc)
1924 struct changelog_show *cs;
1925 struct task_struct *task;
1928 /* Freed in mdc_changelog_send_thread */
1934 cs->cs_startrec = icc->icc_recno;
1935 /* matching fput in mdc_changelog_send_thread */
1936 cs->cs_fp = fget(icc->icc_id);
1937 cs->cs_flags = icc->icc_flags;
1940 * New thread because we should return to user app before
1941 * writing into our pipe
1943 task = kthread_run(mdc_changelog_send_thread, cs,
1944 "mdc_clg_send_thread");
1947 CERROR("%s: cannot start changelog thread: rc = %d\n",
1948 cs_obd_name(cs), rc);
1952 CDEBUG(D_HSM, "%s: started changelog thread\n",
1959 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1960 struct lustre_kernelcomm *lk);
1962 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1963 struct obd_quotactl *oqctl)
1965 struct ptlrpc_request *req;
1966 struct obd_quotactl *oqc;
1970 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1971 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1976 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1979 ptlrpc_request_set_replen(req);
1980 ptlrpc_at_set_req_timeout(req);
1981 req->rq_no_resend = 1;
1983 rc = ptlrpc_queue_wait(req);
1985 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1987 if (req->rq_repmsg &&
1988 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1991 CERROR ("Can't unpack obd_quotactl\n");
1994 ptlrpc_req_finished(req);
1999 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2000 struct md_op_data *op_data)
2002 struct list_head cancels = LIST_HEAD_INIT(cancels);
2003 struct ptlrpc_request *req;
2005 struct mdc_swap_layouts *msl, *payload;
2008 msl = op_data->op_data;
2010 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2011 * first thing it will do is to cancel the 2 layout
2012 * locks held by this client.
2013 * So the client must cancel its layout locks on the 2 fids
2014 * with the request RPC to avoid extra RPC round trips.
2016 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2017 LCK_EX, MDS_INODELOCK_LAYOUT |
2018 MDS_INODELOCK_XATTR);
2019 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2020 LCK_EX, MDS_INODELOCK_LAYOUT |
2021 MDS_INODELOCK_XATTR);
2023 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2024 &RQF_MDS_SWAP_LAYOUTS);
2026 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2030 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2032 ptlrpc_request_free(req);
2036 mdc_swap_layouts_pack(req, op_data);
2038 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2043 ptlrpc_request_set_replen(req);
2045 rc = ptlrpc_queue_wait(req);
2051 ptlrpc_req_finished(req);
2055 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2056 void *karg, void __user *uarg)
2058 struct obd_device *obd = exp->exp_obd;
2059 struct obd_ioctl_data *data = karg;
2060 struct obd_import *imp = obd->u.cli.cl_import;
2064 if (!try_module_get(THIS_MODULE)) {
2065 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2066 module_name(THIS_MODULE));
2070 case OBD_IOC_CHANGELOG_SEND:
2071 rc = mdc_ioc_changelog_send(obd, karg);
2073 case OBD_IOC_CHANGELOG_CLEAR: {
2074 struct ioc_changelog *icc = karg;
2075 struct changelog_setinfo cs =
2076 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
2077 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2078 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2082 case OBD_IOC_FID2PATH:
2083 rc = mdc_ioc_fid2path(exp, karg);
2085 case LL_IOC_HSM_CT_START:
2086 rc = mdc_ioc_hsm_ct_start(exp, karg);
2087 /* ignore if it was already registered on this MDS. */
2091 case LL_IOC_HSM_PROGRESS:
2092 rc = mdc_ioc_hsm_progress(exp, karg);
2094 case LL_IOC_HSM_STATE_GET:
2095 rc = mdc_ioc_hsm_state_get(exp, karg);
2097 case LL_IOC_HSM_STATE_SET:
2098 rc = mdc_ioc_hsm_state_set(exp, karg);
2100 case LL_IOC_HSM_ACTION:
2101 rc = mdc_ioc_hsm_current_action(exp, karg);
2103 case LL_IOC_HSM_REQUEST:
2104 rc = mdc_ioc_hsm_request(exp, karg);
2106 case OBD_IOC_CLIENT_RECOVER:
2107 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2111 case IOC_OSC_SET_ACTIVE:
2112 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2114 case OBD_IOC_PING_TARGET:
2115 rc = ptlrpc_obd_ping(obd);
2118 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2119 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2120 * there'd be no LMV layer thus we might be called here. Eventually
2121 * this code should be removed.
2124 case IOC_OBD_STATFS: {
2125 struct obd_statfs stat_buf = {0};
2127 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2128 GOTO(out, rc = -ENODEV);
2131 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2132 min((int)data->ioc_plen2,
2133 (int)sizeof(struct obd_uuid))))
2134 GOTO(out, rc = -EFAULT);
2136 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2137 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2142 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2143 min((int) data->ioc_plen1,
2144 (int) sizeof(stat_buf))))
2145 GOTO(out, rc = -EFAULT);
2149 case OBD_IOC_QUOTACTL: {
2150 struct if_quotactl *qctl = karg;
2151 struct obd_quotactl *oqctl;
2153 OBD_ALLOC_PTR(oqctl);
2155 GOTO(out, rc = -ENOMEM);
2157 QCTL_COPY(oqctl, qctl);
2158 rc = obd_quotactl(exp, oqctl);
2160 QCTL_COPY(qctl, oqctl);
2161 qctl->qc_valid = QC_MDTIDX;
2162 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2165 OBD_FREE_PTR(oqctl);
2168 case LL_IOC_GET_CONNECT_FLAGS:
2169 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2170 sizeof(*exp_connect_flags_ptr(exp))))
2171 GOTO(out, rc = -EFAULT);
2174 case LL_IOC_LOV_SWAP_LAYOUTS:
2175 rc = mdc_ioc_swap_layouts(exp, karg);
2178 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2179 GOTO(out, rc = -ENOTTY);
2182 module_put(THIS_MODULE);
2187 static int mdc_get_info_rpc(struct obd_export *exp,
2188 u32 keylen, void *key,
2189 u32 vallen, void *val)
2191 struct obd_import *imp = class_exp2cliimp(exp);
2192 struct ptlrpc_request *req;
2197 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2201 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2202 RCL_CLIENT, keylen);
2203 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2204 RCL_CLIENT, sizeof(vallen));
2206 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2208 ptlrpc_request_free(req);
2212 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2213 memcpy(tmp, key, keylen);
2214 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2215 memcpy(tmp, &vallen, sizeof(vallen));
2217 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2218 RCL_SERVER, vallen);
2219 ptlrpc_request_set_replen(req);
2221 rc = ptlrpc_queue_wait(req);
2222 /* -EREMOTE means the get_info result is partial, and it needs to
2223 * continue on another MDT, see fid2path part in lmv_iocontrol */
2224 if (rc == 0 || rc == -EREMOTE) {
2225 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2226 memcpy(val, tmp, vallen);
2227 if (ptlrpc_rep_need_swab(req)) {
2228 if (KEY_IS(KEY_FID2PATH))
2229 lustre_swab_fid2path(val);
2232 ptlrpc_req_finished(req);
2237 static void lustre_swab_hai(struct hsm_action_item *h)
2239 __swab32s(&h->hai_len);
2240 __swab32s(&h->hai_action);
2241 lustre_swab_lu_fid(&h->hai_fid);
2242 lustre_swab_lu_fid(&h->hai_dfid);
2243 __swab64s(&h->hai_cookie);
2244 __swab64s(&h->hai_extent.offset);
2245 __swab64s(&h->hai_extent.length);
2246 __swab64s(&h->hai_gid);
2249 static void lustre_swab_hal(struct hsm_action_list *h)
2251 struct hsm_action_item *hai;
2254 __swab32s(&h->hal_version);
2255 __swab32s(&h->hal_count);
2256 __swab32s(&h->hal_archive_id);
2257 __swab64s(&h->hal_flags);
2259 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2260 lustre_swab_hai(hai);
2263 static void lustre_swab_kuch(struct kuc_hdr *l)
2265 __swab16s(&l->kuc_magic);
2266 /* __u8 l->kuc_transport */
2267 __swab16s(&l->kuc_msgtype);
2268 __swab16s(&l->kuc_msglen);
2271 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2272 struct lustre_kernelcomm *lk)
2274 struct obd_import *imp = class_exp2cliimp(exp);
2275 __u32 archive = lk->lk_data;
2278 if (lk->lk_group != KUC_GRP_HSM) {
2279 CERROR("Bad copytool group %d\n", lk->lk_group);
2283 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2284 lk->lk_uid, lk->lk_group, lk->lk_flags);
2286 if (lk->lk_flags & LK_FLG_STOP) {
2287 /* Unregister with the coordinator */
2288 rc = mdc_ioc_hsm_ct_unregister(imp);
2290 rc = mdc_ioc_hsm_ct_register(imp, archive);
2297 * Send a message to any listening copytools
2298 * @param val KUC message (kuc_hdr + hsm_action_list)
2299 * @param len total length of message
2301 static int mdc_hsm_copytool_send(size_t len, void *val)
2303 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2304 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2308 if (len < sizeof(*lh) + sizeof(*hal)) {
2309 CERROR("Short HSM message %zu < %zu\n", len,
2310 sizeof(*lh) + sizeof(*hal));
2313 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2314 lustre_swab_kuch(lh);
2315 lustre_swab_hal(hal);
2316 } else if (lh->kuc_magic != KUC_MAGIC) {
2317 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2321 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2323 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2324 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2326 /* Broadcast to HSM listeners */
2327 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2333 * callback function passed to kuc for re-registering each HSM copytool
2334 * running on MDC, after MDT shutdown/recovery.
2335 * @param data copytool registration data
2336 * @param cb_arg callback argument (obd_import)
2338 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2340 struct kkuc_ct_data *kcd = data;
2341 struct obd_import *imp = (struct obd_import *)cb_arg;
2344 if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2347 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2350 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2351 imp->imp_obd->obd_name, kcd->kcd_archive);
2352 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2354 /* ignore error if the copytool is already registered */
2355 return (rc == -EEXIST) ? 0 : rc;
2359 * Re-establish all kuc contexts with MDT
2360 * after MDT shutdown/recovery.
2362 static int mdc_kuc_reregister(struct obd_import *imp)
2364 /* re-register HSM agents */
2365 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2369 static int mdc_set_info_async(const struct lu_env *env,
2370 struct obd_export *exp,
2371 u32 keylen, void *key,
2372 u32 vallen, void *val,
2373 struct ptlrpc_request_set *set)
2375 struct obd_import *imp = class_exp2cliimp(exp);
2379 if (KEY_IS(KEY_READ_ONLY)) {
2380 if (vallen != sizeof(int))
2383 spin_lock(&imp->imp_lock);
2384 if (*((int *)val)) {
2385 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2386 imp->imp_connect_data.ocd_connect_flags |=
2389 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2390 imp->imp_connect_data.ocd_connect_flags &=
2391 ~OBD_CONNECT_RDONLY;
2393 spin_unlock(&imp->imp_lock);
2395 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2396 keylen, key, vallen, val, set);
2399 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2400 sptlrpc_conf_client_adapt(exp->exp_obd);
2403 if (KEY_IS(KEY_FLUSH_CTX)) {
2404 sptlrpc_import_flush_my_ctx(imp);
2407 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2408 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2409 keylen, key, vallen, val, set);
2412 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2413 rc = mdc_hsm_copytool_send(vallen, val);
2417 if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2418 __u32 *default_easize = val;
2420 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2424 CERROR("Unknown key %s\n", (char *)key);
2428 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2429 __u32 keylen, void *key, __u32 *vallen, void *val)
2433 if (KEY_IS(KEY_MAX_EASIZE)) {
2434 __u32 mdsize, *max_easize;
2436 if (*vallen != sizeof(int))
2438 mdsize = *(__u32 *)val;
2439 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2440 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2442 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2444 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2445 __u32 *default_easize;
2447 if (*vallen != sizeof(int))
2449 default_easize = val;
2450 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2452 } else if (KEY_IS(KEY_CONN_DATA)) {
2453 struct obd_import *imp = class_exp2cliimp(exp);
2454 struct obd_connect_data *data = val;
2456 if (*vallen != sizeof(*data))
2459 *data = imp->imp_connect_data;
2461 } else if (KEY_IS(KEY_TGT_COUNT)) {
2462 *((__u32 *)val) = 1;
2466 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2471 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2472 struct ptlrpc_request **request)
2474 struct ptlrpc_request *req;
2479 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2483 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2485 ptlrpc_request_free(req);
2489 mdc_pack_body(req, fid, 0, 0, -1, 0);
2491 ptlrpc_request_set_replen(req);
2493 rc = ptlrpc_queue_wait(req);
2495 ptlrpc_req_finished(req);
2501 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2502 enum obd_import_event event)
2506 LASSERT(imp->imp_obd == obd);
2510 case IMP_EVENT_INACTIVE: {
2511 struct client_obd *cli = &obd->u.cli;
2513 * Flush current sequence to make client obtain new one
2514 * from server in case of disconnect/reconnect.
2516 if (cli->cl_seq != NULL)
2517 seq_client_flush(cli->cl_seq);
2519 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2522 case IMP_EVENT_INVALIDATE: {
2523 struct ldlm_namespace *ns = obd->obd_namespace;
2525 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2529 case IMP_EVENT_ACTIVE:
2530 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2531 /* redo the kuc registration after reconnecting */
2533 rc = mdc_kuc_reregister(imp);
2536 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2538 case IMP_EVENT_DISCON:
2539 case IMP_EVENT_DEACTIVATE:
2540 case IMP_EVENT_ACTIVATE:
2543 CERROR("Unknown import event %x\n", event);
2549 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2550 struct lu_fid *fid, struct md_op_data *op_data)
2552 struct client_obd *cli = &exp->exp_obd->u.cli;
2553 struct lu_client_seq *seq = cli->cl_seq;
2555 RETURN(seq_client_alloc_fid(env, seq, fid));
2558 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2560 struct client_obd *cli = &exp->exp_obd->u.cli;
2561 return &cli->cl_target_uuid;
2565 * Determine whether the lock can be canceled before replaying it during
2566 * recovery, non zero value will be return if the lock can be canceled,
2567 * or zero returned for not
2569 static int mdc_cancel_weight(struct ldlm_lock *lock)
2571 if (lock->l_resource->lr_type != LDLM_IBITS)
2574 /* FIXME: if we ever get into a situation where there are too many
2575 * opened files with open locks on a single node, then we really
2576 * should replay these open locks to reget it */
2577 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2583 static int mdc_resource_inode_free(struct ldlm_resource *res)
2585 if (res->lr_lvb_inode)
2586 res->lr_lvb_inode = NULL;
2591 static struct ldlm_valblock_ops inode_lvbo = {
2592 .lvbo_free = mdc_resource_inode_free
2595 static int mdc_llog_init(struct obd_device *obd)
2597 struct obd_llog_group *olg = &obd->obd_olg;
2598 struct llog_ctxt *ctxt;
2603 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2608 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2609 llog_initiator_connect(ctxt);
2610 llog_ctxt_put(ctxt);
2615 static void mdc_llog_finish(struct obd_device *obd)
2617 struct llog_ctxt *ctxt;
2621 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2623 llog_cleanup(NULL, ctxt);
2628 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2633 rc = ptlrpcd_addref();
2637 rc = client_obd_setup(obd, cfg);
2639 GOTO(err_ptlrpcd_decref, rc);
2640 #ifdef CONFIG_PROC_FS
2641 obd->obd_vars = lprocfs_mdc_obd_vars;
2642 lprocfs_obd_setup(obd);
2643 lprocfs_alloc_md_stats(obd, 0);
2645 sptlrpc_lprocfs_cliobd_attach(obd);
2646 ptlrpc_lprocfs_register_obd(obd);
2648 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2650 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2652 rc = mdc_llog_init(obd);
2655 CERROR("failed to setup llogging subsystems\n");
2666 /* Initialize the default and maximum LOV EA sizes. This allows
2667 * us to make MDS RPCs with large enough reply buffers to hold a default
2668 * sized EA without having to calculate this (via a call into the
2669 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2670 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2671 * a large number of stripes is possible. If a larger reply buffer is
2672 * required it will be reallocated in the ptlrpc layer due to overflow.
2674 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2677 struct obd_device *obd = exp->exp_obd;
2678 struct client_obd *cli = &obd->u.cli;
2681 if (cli->cl_max_mds_easize < easize)
2682 cli->cl_max_mds_easize = easize;
2684 if (cli->cl_default_mds_easize < def_easize)
2685 cli->cl_default_mds_easize = def_easize;
2690 static int mdc_precleanup(struct obd_device *obd)
2694 /* Failsafe, ok if racy */
2695 if (obd->obd_type->typ_refcnt <= 1)
2696 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2698 obd_cleanup_client_import(obd);
2699 ptlrpc_lprocfs_unregister_obd(obd);
2700 lprocfs_obd_cleanup(obd);
2701 lprocfs_free_md_stats(obd);
2702 mdc_llog_finish(obd);
2706 static int mdc_cleanup(struct obd_device *obd)
2710 return client_obd_cleanup(obd);
2713 static int mdc_process_config(struct obd_device *obd, size_t len, void *buf)
2715 struct lustre_cfg *lcfg = buf;
2716 int rc = class_process_proc_param(PARAM_MDC, obd->obd_vars, lcfg, obd);
2717 return (rc > 0 ? 0: rc);
2720 static struct obd_ops mdc_obd_ops = {
2721 .o_owner = THIS_MODULE,
2722 .o_setup = mdc_setup,
2723 .o_precleanup = mdc_precleanup,
2724 .o_cleanup = mdc_cleanup,
2725 .o_add_conn = client_import_add_conn,
2726 .o_del_conn = client_import_del_conn,
2727 .o_connect = client_connect_import,
2728 .o_disconnect = client_disconnect_export,
2729 .o_iocontrol = mdc_iocontrol,
2730 .o_set_info_async = mdc_set_info_async,
2731 .o_statfs = mdc_statfs,
2732 .o_fid_init = client_fid_init,
2733 .o_fid_fini = client_fid_fini,
2734 .o_fid_alloc = mdc_fid_alloc,
2735 .o_import_event = mdc_import_event,
2736 .o_get_info = mdc_get_info,
2737 .o_process_config = mdc_process_config,
2738 .o_get_uuid = mdc_get_uuid,
2739 .o_quotactl = mdc_quotactl,
2742 static struct md_ops mdc_md_ops = {
2743 .m_get_root = mdc_get_root,
2744 .m_null_inode = mdc_null_inode,
2745 .m_close = mdc_close,
2746 .m_create = mdc_create,
2747 .m_enqueue = mdc_enqueue,
2748 .m_getattr = mdc_getattr,
2749 .m_getattr_name = mdc_getattr_name,
2750 .m_intent_lock = mdc_intent_lock,
2752 .m_rename = mdc_rename,
2753 .m_setattr = mdc_setattr,
2754 .m_setxattr = mdc_setxattr,
2755 .m_getxattr = mdc_getxattr,
2756 .m_fsync = mdc_fsync,
2757 .m_read_page = mdc_read_page,
2758 .m_unlink = mdc_unlink,
2759 .m_cancel_unused = mdc_cancel_unused,
2760 .m_init_ea_size = mdc_init_ea_size,
2761 .m_set_lock_data = mdc_set_lock_data,
2762 .m_lock_match = mdc_lock_match,
2763 .m_get_lustre_md = mdc_get_lustre_md,
2764 .m_free_lustre_md = mdc_free_lustre_md,
2765 .m_set_open_replay_data = mdc_set_open_replay_data,
2766 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2767 .m_intent_getattr_async = mdc_intent_getattr_async,
2768 .m_revalidate_lock = mdc_revalidate_lock
2771 static int __init mdc_init(void)
2773 return class_register_type(&mdc_obd_ops, &mdc_md_ops, true, NULL,
2774 LUSTRE_MDC_NAME, NULL);
2777 static void __exit mdc_exit(void)
2779 class_unregister_type(LUSTRE_MDC_NAME);
2782 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2783 MODULE_DESCRIPTION("Lustre Metadata Client");
2784 MODULE_VERSION(LUSTRE_VERSION_STRING);
2785 MODULE_LICENSE("GPL");
2787 module_init(mdc_init);
2788 module_exit(mdc_exit);