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/module.h>
36 # include <linux/pagemap.h>
37 # include <linux/miscdevice.h>
38 # include <linux/init.h>
39 # include <linux/utsname.h>
41 #include "../include/lustre_acl.h"
42 #include "../include/obd_class.h"
43 #include "../include/lustre_fid.h"
44 #include "../include/lprocfs_status.h"
45 #include "../include/lustre_param.h"
46 #include "../include/lustre_log.h"
47 #include "../include/lustre_kernelcomm.h"
49 #include "mdc_internal.h"
51 #define REQUEST_MINOR 244
53 static int mdc_cleanup(struct obd_device *obd);
55 static inline int mdc_queue_wait(struct ptlrpc_request *req)
57 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
60 /* mdc_enter_request() ensures that this client has no more
61 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
64 rc = mdc_enter_request(cli);
68 rc = ptlrpc_queue_wait(req);
69 mdc_exit_request(cli);
74 static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid)
76 struct ptlrpc_request *req;
77 struct mdt_body *body;
80 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
82 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
86 mdc_pack_body(req, NULL, 0, 0, -1, 0);
87 req->rq_send_state = LUSTRE_IMP_FULL;
89 ptlrpc_request_set_replen(req);
91 rc = ptlrpc_queue_wait(req);
95 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
101 *rootfid = body->fid1;
103 "root fid="DFID", last_committed=%llu\n",
105 lustre_msg_get_last_committed(req->rq_repmsg));
107 ptlrpc_req_finished(req);
112 * This function now is known to always saying that it will receive 4 buffers
113 * from server. Even for cases when acl_size and md_size is zero, RPC header
114 * will contain 4 fields and RPC itself will contain zero size fields. This is
115 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
116 * and thus zero, it shrinks it, making zero size. The same story about
117 * md_size. And this is course of problem when client waits for smaller number
118 * of fields. This issue will be fixed later when client gets aware of RPC
121 static int mdc_getattr_common(struct obd_export *exp,
122 struct ptlrpc_request *req)
124 struct req_capsule *pill = &req->rq_pill;
125 struct mdt_body *body;
129 /* Request message already built. */
130 rc = ptlrpc_queue_wait(req);
134 /* sanity check for the reply */
135 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
139 CDEBUG(D_NET, "mode: %o\n", body->mode);
141 mdc_update_max_ea_from_body(exp, body);
142 if (body->eadatasize != 0) {
143 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
152 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
153 struct ptlrpc_request **request)
155 struct ptlrpc_request *req;
158 /* Single MDS without an LMV case */
159 if (op_data->op_flags & MF_GET_MDT_IDX) {
164 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
168 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
170 ptlrpc_request_free(req);
174 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
175 op_data->op_mode, -1, 0);
177 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
179 ptlrpc_request_set_replen(req);
181 rc = mdc_getattr_common(exp, req);
183 ptlrpc_req_finished(req);
189 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
190 struct ptlrpc_request **request)
192 struct ptlrpc_request *req;
196 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
197 &RQF_MDS_GETATTR_NAME);
201 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
202 op_data->op_namelen + 1);
204 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
206 ptlrpc_request_free(req);
210 mdc_pack_body(req, &op_data->op_fid1, op_data->op_valid,
211 op_data->op_mode, op_data->op_suppgids[0], 0);
213 if (op_data->op_name) {
214 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
216 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
217 op_data->op_namelen);
218 memcpy(name, op_data->op_name, op_data->op_namelen);
221 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
223 ptlrpc_request_set_replen(req);
225 rc = mdc_getattr_common(exp, req);
227 ptlrpc_req_finished(req);
233 static int mdc_is_subdir(struct obd_export *exp,
234 const struct lu_fid *pfid,
235 const struct lu_fid *cfid,
236 struct ptlrpc_request **request)
238 struct ptlrpc_request *req;
242 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
243 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
248 mdc_is_subdir_pack(req, pfid, cfid, 0);
249 ptlrpc_request_set_replen(req);
251 rc = ptlrpc_queue_wait(req);
252 if (rc && rc != -EREMOTE)
253 ptlrpc_req_finished(req);
259 static int mdc_xattr_common(struct obd_export *exp,
260 const struct req_format *fmt,
261 const struct lu_fid *fid,
262 int opcode, u64 valid,
263 const char *xattr_name, const char *input,
264 int input_size, int output_size, int flags,
265 __u32 suppgid, struct ptlrpc_request **request)
267 struct ptlrpc_request *req;
268 int xattr_namelen = 0;
273 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
278 xattr_namelen = strlen(xattr_name) + 1;
279 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
284 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
288 /* Flush local XATTR locks to get rid of a possible cancel RPC */
289 if (opcode == MDS_REINT && fid_is_sane(fid) &&
290 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
294 /* Without that packing would fail */
296 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
299 count = mdc_resource_get_unused(exp, fid,
301 MDS_INODELOCK_XATTR);
303 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
305 ptlrpc_request_free(req);
309 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
311 ptlrpc_request_free(req);
316 if (opcode == MDS_REINT) {
317 struct mdt_rec_setxattr *rec;
319 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
320 sizeof(struct mdt_rec_reint));
321 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
322 rec->sx_opcode = REINT_SETXATTR;
323 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
324 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
325 rec->sx_cap = cfs_curproc_cap_pack();
326 rec->sx_suppgid1 = suppgid;
327 rec->sx_suppgid2 = -1;
329 rec->sx_valid = valid | OBD_MD_FLCTIME;
330 rec->sx_time = ktime_get_real_seconds();
331 rec->sx_size = output_size;
332 rec->sx_flags = flags;
335 mdc_pack_body(req, fid, valid, output_size, suppgid, flags);
339 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
340 memcpy(tmp, xattr_name, xattr_namelen);
343 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
344 memcpy(tmp, input, input_size);
347 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
348 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
349 RCL_SERVER, output_size);
350 ptlrpc_request_set_replen(req);
353 if (opcode == MDS_REINT)
354 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
356 rc = ptlrpc_queue_wait(req);
358 if (opcode == MDS_REINT)
359 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
362 ptlrpc_req_finished(req);
368 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
369 u64 valid, const char *xattr_name,
370 const char *input, int input_size, int output_size,
371 int flags, __u32 suppgid,
372 struct ptlrpc_request **request)
374 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
375 fid, MDS_REINT, valid, xattr_name,
376 input, input_size, output_size, flags,
380 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
381 u64 valid, const char *xattr_name,
382 const char *input, int input_size, int output_size,
383 int flags, struct ptlrpc_request **request)
385 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
386 fid, MDS_GETXATTR, valid, xattr_name,
387 input, input_size, output_size, flags,
391 #ifdef CONFIG_FS_POSIX_ACL
392 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
394 struct req_capsule *pill = &req->rq_pill;
395 struct mdt_body *body = md->body;
396 struct posix_acl *acl;
403 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
408 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
414 CERROR("convert xattr to acl: %d\n", rc);
418 rc = posix_acl_valid(acl);
420 CERROR("validate acl: %d\n", rc);
421 posix_acl_release(acl);
429 #define mdc_unpack_acl(req, md) 0
432 static int mdc_get_lustre_md(struct obd_export *exp,
433 struct ptlrpc_request *req,
434 struct obd_export *dt_exp,
435 struct obd_export *md_exp,
436 struct lustre_md *md)
438 struct req_capsule *pill = &req->rq_pill;
442 memset(md, 0, sizeof(*md));
444 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
446 if (md->body->valid & OBD_MD_FLEASIZE) {
448 struct lov_mds_md *lmm;
450 if (!S_ISREG(md->body->mode)) {
452 "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
457 if (md->body->eadatasize == 0) {
459 "OBD_MD_FLEASIZE set, but eadatasize 0\n");
463 lmmsize = md->body->eadatasize;
464 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
470 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
474 if (rc < sizeof(*md->lsm)) {
476 "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
477 rc, (int)sizeof(*md->lsm));
482 } else if (md->body->valid & OBD_MD_FLDIREA) {
484 struct lov_mds_md *lmv;
486 if (!S_ISDIR(md->body->mode)) {
488 "OBD_MD_FLDIREA set, should be a directory, but is not\n");
493 if (md->body->eadatasize == 0) {
495 "OBD_MD_FLDIREA is set, but eadatasize 0\n");
498 if (md->body->valid & OBD_MD_MEA) {
499 lmvsize = md->body->eadatasize;
500 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
507 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
512 if (rc < sizeof(*md->mea)) {
514 "size too small: rc < sizeof(*md->mea) (%d < %d)\n",
515 rc, (int)sizeof(*md->mea));
523 if (md->body->valid & OBD_MD_FLACL) {
524 /* for ACL, it's possible that FLACL is set but aclsize is zero.
525 * only when aclsize != 0 there's an actual segment for ACL
528 if (md->body->aclsize) {
529 rc = mdc_unpack_acl(req, md);
532 #ifdef CONFIG_FS_POSIX_ACL
534 md->posix_acl = NULL;
541 #ifdef CONFIG_FS_POSIX_ACL
542 posix_acl_release(md->posix_acl);
545 obd_free_memmd(dt_exp, &md->lsm);
550 static int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
556 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
559 void mdc_replay_open(struct ptlrpc_request *req)
561 struct md_open_data *mod = req->rq_cb_data;
562 struct ptlrpc_request *close_req;
563 struct obd_client_handle *och;
564 struct lustre_handle old;
565 struct mdt_body *body;
568 DEBUG_REQ(D_ERROR, req,
569 "Can't properly replay without open data.");
573 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
577 struct lustre_handle *file_fh;
579 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
581 file_fh = &och->och_fh;
582 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
583 file_fh->cookie, body->handle.cookie);
585 *file_fh = body->handle;
587 close_req = mod->mod_close_req;
589 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
590 struct mdt_ioepoch *epoch;
592 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
593 epoch = req_capsule_client_get(&close_req->rq_pill,
598 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
599 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
600 epoch->handle = body->handle;
604 void mdc_commit_open(struct ptlrpc_request *req)
606 struct md_open_data *mod = req->rq_cb_data;
612 * No need to touch md_open_data::mod_och, it holds a reference on
613 * \var mod and will zero references to each other, \var mod will be
614 * freed after that when md_open_data::mod_och will put the reference.
618 * Do not let open request to disappear as it still may be needed
619 * for close rpc to happen (it may happen on evict only, otherwise
620 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
621 * called), just mark this rpc as committed to distinguish these 2
622 * cases, see mdc_close() for details. The open request reference will
623 * be put along with freeing \var mod.
625 ptlrpc_request_addref(req);
626 spin_lock(&req->rq_lock);
627 req->rq_committed = 1;
628 spin_unlock(&req->rq_lock);
629 req->rq_cb_data = NULL;
633 int mdc_set_open_replay_data(struct obd_export *exp,
634 struct obd_client_handle *och,
635 struct lookup_intent *it)
637 struct md_open_data *mod;
638 struct mdt_rec_create *rec;
639 struct mdt_body *body;
640 struct ptlrpc_request *open_req = it->d.lustre.it_data;
641 struct obd_import *imp = open_req->rq_import;
643 if (!open_req->rq_replay)
646 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
647 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
649 /* Incoming message in my byte order (it's been swabbed). */
650 /* Outgoing messages always in my byte order. */
653 /* Only if the import is replayable, we set replay_open data */
654 if (och && imp->imp_replayable) {
655 mod = obd_mod_alloc();
657 DEBUG_REQ(D_ERROR, open_req,
658 "Can't allocate md_open_data");
663 * Take a reference on \var mod, to be freed on mdc_close().
664 * It protects \var mod from being freed on eviction (commit
665 * callback is called despite rq_replay flag).
666 * Another reference for \var och.
671 spin_lock(&open_req->rq_lock);
674 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
675 it_disposition(it, DISP_OPEN_STRIPE);
676 mod->mod_open_req = open_req;
677 open_req->rq_cb_data = mod;
678 open_req->rq_commit_cb = mdc_commit_open;
679 spin_unlock(&open_req->rq_lock);
682 rec->cr_fid2 = body->fid1;
683 rec->cr_ioepoch = body->ioepoch;
684 rec->cr_old_handle.cookie = body->handle.cookie;
685 open_req->rq_replay_cb = mdc_replay_open;
686 if (!fid_is_sane(&body->fid1)) {
687 DEBUG_REQ(D_ERROR, open_req,
688 "Saving replay request with insane fid");
692 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
696 static void mdc_free_open(struct md_open_data *mod)
700 if (mod->mod_is_create == 0 &&
701 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
704 LASSERT(mod->mod_open_req->rq_replay == 0);
706 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
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 static 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;
719 * It is possible to not have \var mod in a case of eviction between
720 * lookup and ll_file_open().
725 LASSERT(mod != LP_POISON);
726 LASSERT(mod->mod_open_req);
736 /* Prepares the request for the replay by the given reply */
737 static void mdc_close_handle_reply(struct ptlrpc_request *req,
738 struct md_op_data *op_data, int rc) {
739 struct mdt_body *repbody;
740 struct mdt_ioepoch *epoch;
742 if (req && rc == -EAGAIN) {
743 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
744 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
746 epoch->flags |= MF_SOM_AU;
747 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
748 op_data->op_flags |= MF_GETATTR_LOCK;
752 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
753 struct md_open_data *mod, struct ptlrpc_request **request)
755 struct obd_device *obd = class_exp2obd(exp);
756 struct ptlrpc_request *req;
757 struct req_format *req_fmt;
761 req_fmt = &RQF_MDS_CLOSE;
762 if (op_data->op_bias & MDS_HSM_RELEASE) {
763 req_fmt = &RQF_MDS_RELEASE_CLOSE;
765 /* allocate a FID for volatile file */
766 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
768 CERROR("%s: "DFID" failed to allocate FID: %d\n",
769 obd->obd_name, PFID(&op_data->op_fid1), rc);
770 /* save the errcode and proceed to close */
776 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
780 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
782 ptlrpc_request_free(req);
786 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
787 * portal whose threads are not taking any DLM locks and are therefore
790 req->rq_request_portal = MDS_READPAGE_PORTAL;
791 ptlrpc_at_set_req_timeout(req);
793 /* Ensure that this close's handle is fixed up during replay. */
795 LASSERTF(mod->mod_open_req &&
796 mod->mod_open_req->rq_type != LI_POISON,
797 "POISONED open %p!\n", mod->mod_open_req);
799 mod->mod_close_req = req;
801 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
802 /* We no longer want to preserve this open for replay even
803 * though the open was committed. b=3632, b=3633
805 spin_lock(&mod->mod_open_req->rq_lock);
806 mod->mod_open_req->rq_replay = 0;
807 spin_unlock(&mod->mod_open_req->rq_lock);
810 "couldn't find open req; expecting close error\n");
813 mdc_close_pack(req, op_data);
815 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
816 obd->u.cli.cl_default_mds_easize);
817 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
818 obd->u.cli.cl_default_mds_cookiesize);
820 ptlrpc_request_set_replen(req);
822 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
823 rc = ptlrpc_queue_wait(req);
824 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
826 if (!req->rq_repmsg) {
827 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
830 rc = req->rq_status ?: -EIO;
831 } else if (rc == 0 || rc == -EAGAIN) {
832 struct mdt_body *body;
834 rc = lustre_msg_get_status(req->rq_repmsg);
835 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
836 DEBUG_REQ(D_ERROR, req,
837 "type == PTL_RPC_MSG_ERR, err = %d", rc);
841 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
844 } else if (rc == -ESTALE) {
846 * it can be allowed error after 3633 if open was committed and
847 * server failed before close was sent. Let's check if mod
848 * exists and return no error in that case
851 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
852 if (mod->mod_open_req->rq_committed)
859 mod->mod_close_req = NULL;
860 /* Since now, mod is accessed through open_req only,
861 * thus close req does not keep a reference on mod anymore.
866 mdc_close_handle_reply(req, op_data, rc);
867 return rc < 0 ? rc : saved_rc;
870 static int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
871 struct md_open_data *mod)
873 struct obd_device *obd = class_exp2obd(exp);
874 struct ptlrpc_request *req;
877 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
878 &RQF_MDS_DONE_WRITING);
882 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
884 ptlrpc_request_free(req);
889 LASSERTF(mod->mod_open_req &&
890 mod->mod_open_req->rq_type != LI_POISON,
891 "POISONED setattr %p!\n", mod->mod_open_req);
893 mod->mod_close_req = req;
894 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
895 /* We no longer want to preserve this setattr for replay even
896 * though the open was committed. b=3632, b=3633
898 spin_lock(&mod->mod_open_req->rq_lock);
899 mod->mod_open_req->rq_replay = 0;
900 spin_unlock(&mod->mod_open_req->rq_lock);
903 mdc_close_pack(req, op_data);
904 ptlrpc_request_set_replen(req);
906 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
907 rc = ptlrpc_queue_wait(req);
908 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
912 * it can be allowed error after 3633 if open or setattr were
913 * committed and server failed before close was sent.
914 * Let's check if mod exists and return no error in that case
917 if (mod->mod_open_req->rq_committed)
924 mod->mod_close_req = NULL;
925 LASSERT(mod->mod_open_req);
928 /* Since now, mod is accessed through setattr req only,
929 * thus DW req does not keep a reference on mod anymore.
934 mdc_close_handle_reply(req, op_data, rc);
935 ptlrpc_req_finished(req);
939 static int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
940 struct page **pages, struct ptlrpc_request **request)
942 struct ptlrpc_request *req;
943 struct ptlrpc_bulk_desc *desc;
945 wait_queue_head_t waitq;
947 struct l_wait_info lwi;
951 init_waitqueue_head(&waitq);
954 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
958 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
960 ptlrpc_request_free(req);
964 req->rq_request_portal = MDS_READPAGE_PORTAL;
965 ptlrpc_at_set_req_timeout(req);
967 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
970 ptlrpc_request_free(req);
974 /* NB req now owns desc and will free it when it gets freed */
975 for (i = 0; i < op_data->op_npages; i++)
976 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_SIZE);
978 mdc_readdir_pack(req, op_data->op_offset,
979 PAGE_SIZE * op_data->op_npages,
982 ptlrpc_request_set_replen(req);
983 rc = ptlrpc_queue_wait(req);
985 ptlrpc_req_finished(req);
986 if (rc != -ETIMEDOUT)
990 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
991 CERROR("too many resend retries, returning error\n");
994 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
996 l_wait_event(waitq, 0, &lwi);
1001 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1002 req->rq_bulk->bd_nob_transferred);
1004 ptlrpc_req_finished(req);
1008 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1009 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1010 req->rq_bulk->bd_nob_transferred,
1011 PAGE_SIZE * op_data->op_npages);
1012 ptlrpc_req_finished(req);
1020 static int mdc_statfs(const struct lu_env *env,
1021 struct obd_export *exp, struct obd_statfs *osfs,
1022 __u64 max_age, __u32 flags)
1024 struct obd_device *obd = class_exp2obd(exp);
1025 struct ptlrpc_request *req;
1026 struct obd_statfs *msfs;
1027 struct obd_import *imp = NULL;
1031 * Since the request might also come from lprocfs, so we need
1032 * sync this with client_disconnect_export Bug15684
1034 down_read(&obd->u.cli.cl_sem);
1035 if (obd->u.cli.cl_import)
1036 imp = class_import_get(obd->u.cli.cl_import);
1037 up_read(&obd->u.cli.cl_sem);
1041 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1042 LUSTRE_MDS_VERSION, MDS_STATFS);
1048 ptlrpc_request_set_replen(req);
1050 if (flags & OBD_STATFS_NODELAY) {
1051 /* procfs requests not want stay in wait for avoid deadlock */
1052 req->rq_no_resend = 1;
1053 req->rq_no_delay = 1;
1056 rc = ptlrpc_queue_wait(req);
1058 /* check connection error first */
1059 if (imp->imp_connect_error)
1060 rc = imp->imp_connect_error;
1064 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1072 ptlrpc_req_finished(req);
1074 class_import_put(imp);
1078 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1080 __u32 keylen, vallen;
1084 if (gf->gf_pathlen > PATH_MAX)
1085 return -ENAMETOOLONG;
1086 if (gf->gf_pathlen < 2)
1089 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1090 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1091 key = kzalloc(keylen, GFP_NOFS);
1094 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1095 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1097 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1098 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1100 if (!fid_is_sane(&gf->gf_fid)) {
1105 /* Val is struct getinfo_fid2path result plus path */
1106 vallen = sizeof(*gf) + gf->gf_pathlen;
1108 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1109 if (rc != 0 && rc != -EREMOTE)
1112 if (vallen <= sizeof(*gf)) {
1115 } else if (vallen > sizeof(*gf) + gf->gf_pathlen) {
1120 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
1121 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1128 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1129 struct hsm_progress_kernel *hpk)
1131 struct obd_import *imp = class_exp2cliimp(exp);
1132 struct hsm_progress_kernel *req_hpk;
1133 struct ptlrpc_request *req;
1136 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1137 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1143 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1145 /* Copy hsm_progress struct */
1146 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1153 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1155 ptlrpc_request_set_replen(req);
1157 rc = mdc_queue_wait(req);
1159 ptlrpc_req_finished(req);
1163 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1165 __u32 *archive_mask;
1166 struct ptlrpc_request *req;
1169 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1171 MDS_HSM_CT_REGISTER);
1177 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1179 /* Copy hsm_progress struct */
1180 archive_mask = req_capsule_client_get(&req->rq_pill,
1181 &RMF_MDS_HSM_ARCHIVE);
1182 if (!archive_mask) {
1187 *archive_mask = archives;
1189 ptlrpc_request_set_replen(req);
1191 rc = mdc_queue_wait(req);
1193 ptlrpc_req_finished(req);
1197 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1198 struct md_op_data *op_data)
1200 struct hsm_current_action *hca = op_data->op_data;
1201 struct hsm_current_action *req_hca;
1202 struct ptlrpc_request *req;
1205 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1206 &RQF_MDS_HSM_ACTION);
1210 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1212 ptlrpc_request_free(req);
1216 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1217 op_data->op_suppgids[0], 0);
1219 ptlrpc_request_set_replen(req);
1221 rc = mdc_queue_wait(req);
1225 req_hca = req_capsule_server_get(&req->rq_pill,
1226 &RMF_MDS_HSM_CURRENT_ACTION);
1235 ptlrpc_req_finished(req);
1239 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1241 struct ptlrpc_request *req;
1244 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1246 MDS_HSM_CT_UNREGISTER);
1252 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1254 ptlrpc_request_set_replen(req);
1256 rc = mdc_queue_wait(req);
1258 ptlrpc_req_finished(req);
1262 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1263 struct md_op_data *op_data)
1265 struct hsm_user_state *hus = op_data->op_data;
1266 struct hsm_user_state *req_hus;
1267 struct ptlrpc_request *req;
1270 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1271 &RQF_MDS_HSM_STATE_GET);
1275 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1277 ptlrpc_request_free(req);
1281 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1282 op_data->op_suppgids[0], 0);
1284 ptlrpc_request_set_replen(req);
1286 rc = mdc_queue_wait(req);
1290 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1299 ptlrpc_req_finished(req);
1303 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1304 struct md_op_data *op_data)
1306 struct hsm_state_set *hss = op_data->op_data;
1307 struct hsm_state_set *req_hss;
1308 struct ptlrpc_request *req;
1311 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1312 &RQF_MDS_HSM_STATE_SET);
1316 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1318 ptlrpc_request_free(req);
1322 mdc_pack_body(req, &op_data->op_fid1, 0, 0,
1323 op_data->op_suppgids[0], 0);
1326 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1333 ptlrpc_request_set_replen(req);
1335 rc = mdc_queue_wait(req);
1337 ptlrpc_req_finished(req);
1341 static int mdc_ioc_hsm_request(struct obd_export *exp,
1342 struct hsm_user_request *hur)
1344 struct obd_import *imp = class_exp2cliimp(exp);
1345 struct ptlrpc_request *req;
1346 struct hsm_request *req_hr;
1347 struct hsm_user_item *req_hui;
1351 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1357 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1358 hur->hur_request.hr_itemcount
1359 * sizeof(struct hsm_user_item));
1360 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1361 hur->hur_request.hr_data_len);
1363 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1365 ptlrpc_request_free(req);
1369 mdc_pack_body(req, NULL, 0, 0, -1, 0);
1371 /* Copy hsm_request struct */
1372 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1377 *req_hr = hur->hur_request;
1379 /* Copy hsm_user_item structs */
1380 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1385 memcpy(req_hui, hur->hur_user_item,
1386 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1388 /* Copy opaque field */
1389 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1394 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1396 ptlrpc_request_set_replen(req);
1398 rc = mdc_queue_wait(req);
1400 ptlrpc_req_finished(req);
1404 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1406 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1408 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1410 lh->kuc_magic = KUC_MAGIC;
1411 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1412 lh->kuc_flags = flags;
1413 lh->kuc_msgtype = CL_RECORD;
1414 lh->kuc_msglen = len;
1418 #define D_CHANGELOG 0
1420 struct changelog_show {
1425 struct obd_device *cs_obd;
1428 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1429 struct llog_rec_hdr *hdr, void *data)
1431 struct changelog_show *cs = data;
1432 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1436 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1438 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1439 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1440 rec->cr.cr_type, rc);
1444 if (rec->cr.cr_index < cs->cs_startrec) {
1445 /* Skip entries earlier than what we are interested in */
1446 CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
1447 rec->cr.cr_index, cs->cs_startrec);
1451 CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
1452 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1453 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1454 rec->cr.cr_flags & CLF_FLAGMASK,
1455 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1456 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1458 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1460 /* Set up the message */
1461 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1462 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1464 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1465 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
1470 static int mdc_changelog_send_thread(void *csdata)
1472 struct changelog_show *cs = csdata;
1473 struct llog_ctxt *ctxt = NULL;
1474 struct llog_handle *llh = NULL;
1475 struct kuc_hdr *kuch;
1478 CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
1479 cs->cs_fp, cs->cs_startrec);
1481 cs->cs_buf = kzalloc(KUC_CHANGELOG_MSG_MAXSIZE, GFP_NOFS);
1487 /* Set up the remote catalog handle */
1488 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1493 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1496 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1497 cs->cs_obd->obd_name, rc);
1500 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1502 CERROR("llog_init_handle failed %d\n", rc);
1506 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1508 /* Send EOF no matter what our result */
1509 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1511 kuch->kuc_msgtype = CL_EOF;
1512 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1518 llog_cat_close(NULL, llh);
1520 llog_ctxt_put(ctxt);
1526 static int mdc_ioc_changelog_send(struct obd_device *obd,
1527 struct ioc_changelog *icc)
1529 struct changelog_show *cs;
1530 struct task_struct *task;
1533 /* Freed in mdc_changelog_send_thread */
1534 cs = kzalloc(sizeof(*cs), GFP_NOFS);
1539 cs->cs_startrec = icc->icc_recno;
1540 /* matching fput in mdc_changelog_send_thread */
1541 cs->cs_fp = fget(icc->icc_id);
1542 cs->cs_flags = icc->icc_flags;
1545 * New thread because we should return to user app before
1546 * writing into our pipe
1548 task = kthread_run(mdc_changelog_send_thread, cs,
1549 "mdc_clg_send_thread");
1552 CERROR("%s: can't start changelog thread: rc = %d\n",
1557 CDEBUG(D_CHANGELOG, "%s: started changelog thread\n",
1561 CERROR("Failed to start changelog thread: %d\n", rc);
1565 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1566 struct lustre_kernelcomm *lk);
1568 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1569 struct obd_quotactl *oqctl)
1571 struct client_obd *cli = &exp->exp_obd->u.cli;
1572 struct ptlrpc_request *req;
1573 struct obd_quotactl *body;
1576 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1577 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1582 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1585 ptlrpc_request_set_replen(req);
1587 /* the next poll will find -ENODATA, that means quotacheck is
1590 cli->cl_qchk_stat = -ENODATA;
1591 rc = ptlrpc_queue_wait(req);
1593 cli->cl_qchk_stat = rc;
1594 ptlrpc_req_finished(req);
1598 static int mdc_quota_poll_check(struct obd_export *exp,
1599 struct if_quotacheck *qchk)
1601 struct client_obd *cli = &exp->exp_obd->u.cli;
1604 qchk->obd_uuid = cli->cl_target_uuid;
1605 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1607 rc = cli->cl_qchk_stat;
1608 /* the client is not the previous one */
1609 if (rc == CL_NOT_QUOTACHECKED)
1614 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1615 struct obd_quotactl *oqctl)
1617 struct ptlrpc_request *req;
1618 struct obd_quotactl *oqc;
1621 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1622 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1627 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1630 ptlrpc_request_set_replen(req);
1631 ptlrpc_at_set_req_timeout(req);
1632 req->rq_no_resend = 1;
1634 rc = ptlrpc_queue_wait(req);
1636 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1638 if (req->rq_repmsg) {
1639 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1643 CERROR("Can't unpack obd_quotactl\n");
1647 CERROR("Can't unpack obd_quotactl\n");
1650 ptlrpc_req_finished(req);
1655 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1656 struct md_op_data *op_data)
1659 struct ptlrpc_request *req;
1661 struct mdc_swap_layouts *msl, *payload;
1663 msl = op_data->op_data;
1665 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1666 * first thing it will do is to cancel the 2 layout
1667 * locks hold by this client.
1668 * So the client must cancel its layout locks on the 2 fids
1669 * with the request RPC to avoid extra RPC round trips
1671 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1672 LCK_CR, MDS_INODELOCK_LAYOUT);
1673 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1674 LCK_CR, MDS_INODELOCK_LAYOUT);
1676 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1677 &RQF_MDS_SWAP_LAYOUTS);
1679 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1683 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1685 ptlrpc_request_free(req);
1689 mdc_swap_layouts_pack(req, op_data);
1691 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1696 ptlrpc_request_set_replen(req);
1698 rc = ptlrpc_queue_wait(req);
1700 ptlrpc_req_finished(req);
1704 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1705 void *karg, void __user *uarg)
1707 struct obd_device *obd = exp->exp_obd;
1708 struct obd_ioctl_data *data = karg;
1709 struct obd_import *imp = obd->u.cli.cl_import;
1712 if (!try_module_get(THIS_MODULE)) {
1713 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
1714 module_name(THIS_MODULE));
1718 case OBD_IOC_CHANGELOG_SEND:
1719 rc = mdc_ioc_changelog_send(obd, karg);
1721 case OBD_IOC_CHANGELOG_CLEAR: {
1722 struct ioc_changelog *icc = karg;
1723 struct changelog_setinfo cs = {
1724 .cs_recno = icc->icc_recno,
1725 .cs_id = icc->icc_id
1728 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1729 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1733 case OBD_IOC_FID2PATH:
1734 rc = mdc_ioc_fid2path(exp, karg);
1736 case LL_IOC_HSM_CT_START:
1737 rc = mdc_ioc_hsm_ct_start(exp, karg);
1738 /* ignore if it was already registered on this MDS. */
1742 case LL_IOC_HSM_PROGRESS:
1743 rc = mdc_ioc_hsm_progress(exp, karg);
1745 case LL_IOC_HSM_STATE_GET:
1746 rc = mdc_ioc_hsm_state_get(exp, karg);
1748 case LL_IOC_HSM_STATE_SET:
1749 rc = mdc_ioc_hsm_state_set(exp, karg);
1751 case LL_IOC_HSM_ACTION:
1752 rc = mdc_ioc_hsm_current_action(exp, karg);
1754 case LL_IOC_HSM_REQUEST:
1755 rc = mdc_ioc_hsm_request(exp, karg);
1757 case OBD_IOC_CLIENT_RECOVER:
1758 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1763 case IOC_OSC_SET_ACTIVE:
1764 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1766 case OBD_IOC_POLL_QUOTACHECK:
1767 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1769 case OBD_IOC_PING_TARGET:
1770 rc = ptlrpc_obd_ping(obd);
1773 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1774 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1775 * there'd be no LMV layer thus we might be called here. Eventually
1776 * this code should be removed.
1779 case IOC_OBD_STATFS: {
1780 struct obd_statfs stat_buf = {0};
1782 if (*((__u32 *)data->ioc_inlbuf2) != 0) {
1788 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1789 min_t(size_t, data->ioc_plen2,
1790 sizeof(struct obd_uuid)))) {
1795 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1796 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1801 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1802 min_t(size_t, data->ioc_plen1,
1803 sizeof(stat_buf)))) {
1811 case OBD_IOC_QUOTACTL: {
1812 struct if_quotactl *qctl = karg;
1813 struct obd_quotactl *oqctl;
1815 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
1821 QCTL_COPY(oqctl, qctl);
1822 rc = obd_quotactl(exp, oqctl);
1824 QCTL_COPY(qctl, oqctl);
1825 qctl->qc_valid = QC_MDTIDX;
1826 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1832 case LL_IOC_GET_CONNECT_FLAGS:
1833 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1834 sizeof(*exp_connect_flags_ptr(exp)))) {
1841 case LL_IOC_LOV_SWAP_LAYOUTS:
1842 rc = mdc_ioc_swap_layouts(exp, karg);
1845 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1850 module_put(THIS_MODULE);
1855 static int mdc_get_info_rpc(struct obd_export *exp,
1856 u32 keylen, void *key,
1857 int vallen, void *val)
1859 struct obd_import *imp = class_exp2cliimp(exp);
1860 struct ptlrpc_request *req;
1864 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1868 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1869 RCL_CLIENT, keylen);
1870 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1871 RCL_CLIENT, sizeof(__u32));
1873 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1875 ptlrpc_request_free(req);
1879 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1880 memcpy(tmp, key, keylen);
1881 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1882 memcpy(tmp, &vallen, sizeof(__u32));
1884 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1885 RCL_SERVER, vallen);
1886 ptlrpc_request_set_replen(req);
1888 rc = ptlrpc_queue_wait(req);
1889 /* -EREMOTE means the get_info result is partial, and it needs to
1890 * continue on another MDT, see fid2path part in lmv_iocontrol
1892 if (rc == 0 || rc == -EREMOTE) {
1893 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1894 memcpy(val, tmp, vallen);
1895 if (ptlrpc_rep_need_swab(req)) {
1896 if (KEY_IS(KEY_FID2PATH))
1897 lustre_swab_fid2path(val);
1900 ptlrpc_req_finished(req);
1905 static void lustre_swab_hai(struct hsm_action_item *h)
1907 __swab32s(&h->hai_len);
1908 __swab32s(&h->hai_action);
1909 lustre_swab_lu_fid(&h->hai_fid);
1910 lustre_swab_lu_fid(&h->hai_dfid);
1911 __swab64s(&h->hai_cookie);
1912 __swab64s(&h->hai_extent.offset);
1913 __swab64s(&h->hai_extent.length);
1914 __swab64s(&h->hai_gid);
1917 static void lustre_swab_hal(struct hsm_action_list *h)
1919 struct hsm_action_item *hai;
1922 __swab32s(&h->hal_version);
1923 __swab32s(&h->hal_count);
1924 __swab32s(&h->hal_archive_id);
1925 __swab64s(&h->hal_flags);
1927 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
1928 lustre_swab_hai(hai);
1931 static void lustre_swab_kuch(struct kuc_hdr *l)
1933 __swab16s(&l->kuc_magic);
1934 /* __u8 l->kuc_transport */
1935 __swab16s(&l->kuc_msgtype);
1936 __swab16s(&l->kuc_msglen);
1939 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1940 struct lustre_kernelcomm *lk)
1942 struct obd_import *imp = class_exp2cliimp(exp);
1943 __u32 archive = lk->lk_data;
1946 if (lk->lk_group != KUC_GRP_HSM) {
1947 CERROR("Bad copytool group %d\n", lk->lk_group);
1951 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1952 lk->lk_uid, lk->lk_group, lk->lk_flags);
1954 if (lk->lk_flags & LK_FLG_STOP) {
1955 /* Unregister with the coordinator */
1956 rc = mdc_ioc_hsm_ct_unregister(imp);
1958 rc = mdc_ioc_hsm_ct_register(imp, archive);
1965 * Send a message to any listening copytools
1966 * @param val KUC message (kuc_hdr + hsm_action_list)
1967 * @param len total length of message
1969 static int mdc_hsm_copytool_send(int len, void *val)
1971 struct kuc_hdr *lh = (struct kuc_hdr *)val;
1972 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1974 if (len < sizeof(*lh) + sizeof(*hal)) {
1975 CERROR("Short HSM message %d < %d\n", len,
1976 (int)(sizeof(*lh) + sizeof(*hal)));
1979 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1980 lustre_swab_kuch(lh);
1981 lustre_swab_hal(hal);
1982 } else if (lh->kuc_magic != KUC_MAGIC) {
1983 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
1988 "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
1989 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
1990 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
1992 /* Broadcast to HSM listeners */
1993 return libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
1997 * callback function passed to kuc for re-registering each HSM copytool
1998 * running on MDC, after MDT shutdown/recovery.
1999 * @param data copytool registration data
2000 * @param cb_arg callback argument (obd_import)
2002 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2004 struct kkuc_ct_data *kcd = data;
2005 struct obd_import *imp = (struct obd_import *)cb_arg;
2008 if (!kcd || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2011 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2014 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2015 imp->imp_obd->obd_name, kcd->kcd_archive);
2016 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2018 /* ignore error if the copytool is already registered */
2019 return (rc == -EEXIST) ? 0 : rc;
2022 static int mdc_set_info_async(const struct lu_env *env,
2023 struct obd_export *exp,
2024 u32 keylen, void *key,
2025 u32 vallen, void *val,
2026 struct ptlrpc_request_set *set)
2028 struct obd_import *imp = class_exp2cliimp(exp);
2031 if (KEY_IS(KEY_READ_ONLY)) {
2032 if (vallen != sizeof(int))
2035 spin_lock(&imp->imp_lock);
2036 if (*((int *)val)) {
2037 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2038 imp->imp_connect_data.ocd_connect_flags |=
2041 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2042 imp->imp_connect_data.ocd_connect_flags &=
2043 ~OBD_CONNECT_RDONLY;
2045 spin_unlock(&imp->imp_lock);
2047 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2048 keylen, key, vallen, val, set);
2051 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2052 sptlrpc_conf_client_adapt(exp->exp_obd);
2055 if (KEY_IS(KEY_FLUSH_CTX)) {
2056 sptlrpc_import_flush_my_ctx(imp);
2059 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2060 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2061 keylen, key, vallen, val, set);
2064 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2065 rc = mdc_hsm_copytool_send(vallen, val);
2069 CERROR("Unknown key %s\n", (char *)key);
2073 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2074 __u32 keylen, void *key, __u32 *vallen, void *val,
2075 struct lov_stripe_md *lsm)
2079 if (KEY_IS(KEY_MAX_EASIZE)) {
2080 int mdsize, *max_easize;
2082 if (*vallen != sizeof(int))
2084 mdsize = *(int *)val;
2085 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2086 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2088 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2090 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2091 int *default_easize;
2093 if (*vallen != sizeof(int))
2095 default_easize = val;
2096 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2098 } else if (KEY_IS(KEY_CONN_DATA)) {
2099 struct obd_import *imp = class_exp2cliimp(exp);
2100 struct obd_connect_data *data = val;
2102 if (*vallen != sizeof(*data))
2105 *data = imp->imp_connect_data;
2107 } else if (KEY_IS(KEY_TGT_COUNT)) {
2112 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2117 static int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2118 struct ptlrpc_request **request)
2120 struct ptlrpc_request *req;
2124 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2128 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2130 ptlrpc_request_free(req);
2134 mdc_pack_body(req, fid, 0, 0, -1, 0);
2136 ptlrpc_request_set_replen(req);
2138 rc = ptlrpc_queue_wait(req);
2140 ptlrpc_req_finished(req);
2146 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2147 enum obd_import_event event)
2151 LASSERT(imp->imp_obd == obd);
2154 case IMP_EVENT_DISCON: {
2156 /* XXX Pass event up to OBDs stack. used only for FLD now */
2157 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2161 case IMP_EVENT_INACTIVE: {
2162 struct client_obd *cli = &obd->u.cli;
2164 * Flush current sequence to make client obtain new one
2165 * from server in case of disconnect/reconnect.
2168 seq_client_flush(cli->cl_seq);
2170 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2173 case IMP_EVENT_INVALIDATE: {
2174 struct ldlm_namespace *ns = obd->obd_namespace;
2176 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2180 case IMP_EVENT_ACTIVE:
2181 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2182 /* redo the kuc registration after reconnecting */
2184 /* re-register HSM agents */
2185 rc = libcfs_kkuc_group_foreach(KUC_GRP_HSM,
2186 mdc_hsm_ct_reregister,
2190 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2192 case IMP_EVENT_DEACTIVATE:
2193 case IMP_EVENT_ACTIVATE:
2196 CERROR("Unknown import event %x\n", event);
2202 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2203 struct md_op_data *op_data)
2205 struct client_obd *cli = &exp->exp_obd->u.cli;
2206 struct lu_client_seq *seq = cli->cl_seq;
2208 return seq_client_alloc_fid(NULL, seq, fid);
2211 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2213 struct client_obd *cli = &exp->exp_obd->u.cli;
2215 return &cli->cl_target_uuid;
2219 * Determine whether the lock can be canceled before replaying it during
2220 * recovery, non zero value will be return if the lock can be canceled,
2221 * or zero returned for not
2223 static int mdc_cancel_weight(struct ldlm_lock *lock)
2225 if (lock->l_resource->lr_type != LDLM_IBITS)
2228 /* FIXME: if we ever get into a situation where there are too many
2229 * opened files with open locks on a single node, then we really
2230 * should replay these open locks to reget it
2232 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2238 static int mdc_resource_inode_free(struct ldlm_resource *res)
2240 if (res->lr_lvb_inode)
2241 res->lr_lvb_inode = NULL;
2246 static struct ldlm_valblock_ops inode_lvbo = {
2247 .lvbo_free = mdc_resource_inode_free,
2250 static int mdc_llog_init(struct obd_device *obd)
2252 struct obd_llog_group *olg = &obd->obd_olg;
2253 struct llog_ctxt *ctxt;
2256 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2261 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2262 llog_initiator_connect(ctxt);
2263 llog_ctxt_put(ctxt);
2268 static void mdc_llog_finish(struct obd_device *obd)
2270 struct llog_ctxt *ctxt;
2272 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2274 llog_cleanup(NULL, ctxt);
2277 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2279 struct client_obd *cli = &obd->u.cli;
2280 struct lprocfs_static_vars lvars = { NULL };
2283 cli->cl_rpc_lock = kzalloc(sizeof(*cli->cl_rpc_lock), GFP_NOFS);
2284 if (!cli->cl_rpc_lock)
2286 mdc_init_rpc_lock(cli->cl_rpc_lock);
2288 rc = ptlrpcd_addref();
2292 cli->cl_close_lock = kzalloc(sizeof(*cli->cl_close_lock), GFP_NOFS);
2293 if (!cli->cl_close_lock) {
2295 goto err_ptlrpcd_decref;
2297 mdc_init_rpc_lock(cli->cl_close_lock);
2299 rc = client_obd_setup(obd, cfg);
2301 goto err_close_lock;
2302 lprocfs_mdc_init_vars(&lvars);
2303 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
2304 sptlrpc_lprocfs_cliobd_attach(obd);
2305 ptlrpc_lprocfs_register_obd(obd);
2307 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2309 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2311 rc = mdc_llog_init(obd);
2314 CERROR("failed to setup llogging subsystems\n");
2320 kfree(cli->cl_close_lock);
2324 kfree(cli->cl_rpc_lock);
2328 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2329 * us to make MDS RPCs with large enough reply buffers to hold a default
2330 * sized EA and cookie without having to calculate this (via a call into the
2331 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2332 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2333 * a large number of stripes is possible. If a larger reply buffer is
2334 * required it will be reallocated in the ptlrpc layer due to overflow.
2336 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2337 int def_easize, int cookiesize, int def_cookiesize)
2339 struct obd_device *obd = exp->exp_obd;
2340 struct client_obd *cli = &obd->u.cli;
2342 if (cli->cl_max_mds_easize < easize)
2343 cli->cl_max_mds_easize = easize;
2345 if (cli->cl_default_mds_easize < def_easize)
2346 cli->cl_default_mds_easize = def_easize;
2348 if (cli->cl_max_mds_cookiesize < cookiesize)
2349 cli->cl_max_mds_cookiesize = cookiesize;
2351 if (cli->cl_default_mds_cookiesize < def_cookiesize)
2352 cli->cl_default_mds_cookiesize = def_cookiesize;
2357 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2360 case OBD_CLEANUP_EARLY:
2362 case OBD_CLEANUP_EXPORTS:
2363 /* Failsafe, ok if racy */
2364 if (obd->obd_type->typ_refcnt <= 1)
2365 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2367 obd_cleanup_client_import(obd);
2368 ptlrpc_lprocfs_unregister_obd(obd);
2369 lprocfs_obd_cleanup(obd);
2371 mdc_llog_finish(obd);
2377 static int mdc_cleanup(struct obd_device *obd)
2379 struct client_obd *cli = &obd->u.cli;
2381 kfree(cli->cl_rpc_lock);
2382 kfree(cli->cl_close_lock);
2386 return client_obd_cleanup(obd);
2389 static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
2391 struct lustre_cfg *lcfg = buf;
2392 struct lprocfs_static_vars lvars = { NULL };
2395 lprocfs_mdc_init_vars(&lvars);
2396 switch (lcfg->lcfg_command) {
2398 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2407 static struct obd_ops mdc_obd_ops = {
2408 .owner = THIS_MODULE,
2410 .precleanup = mdc_precleanup,
2411 .cleanup = mdc_cleanup,
2412 .add_conn = client_import_add_conn,
2413 .del_conn = client_import_del_conn,
2414 .connect = client_connect_import,
2415 .disconnect = client_disconnect_export,
2416 .iocontrol = mdc_iocontrol,
2417 .set_info_async = mdc_set_info_async,
2418 .statfs = mdc_statfs,
2419 .fid_init = client_fid_init,
2420 .fid_fini = client_fid_fini,
2421 .fid_alloc = mdc_fid_alloc,
2422 .import_event = mdc_import_event,
2423 .get_info = mdc_get_info,
2424 .process_config = mdc_process_config,
2425 .get_uuid = mdc_get_uuid,
2426 .quotactl = mdc_quotactl,
2427 .quotacheck = mdc_quotacheck
2430 static struct md_ops mdc_md_ops = {
2431 .getstatus = mdc_getstatus,
2432 .null_inode = mdc_null_inode,
2433 .find_cbdata = mdc_find_cbdata,
2435 .create = mdc_create,
2436 .done_writing = mdc_done_writing,
2437 .enqueue = mdc_enqueue,
2438 .getattr = mdc_getattr,
2439 .getattr_name = mdc_getattr_name,
2440 .intent_lock = mdc_intent_lock,
2442 .is_subdir = mdc_is_subdir,
2443 .rename = mdc_rename,
2444 .setattr = mdc_setattr,
2445 .setxattr = mdc_setxattr,
2446 .getxattr = mdc_getxattr,
2448 .readpage = mdc_readpage,
2449 .unlink = mdc_unlink,
2450 .cancel_unused = mdc_cancel_unused,
2451 .init_ea_size = mdc_init_ea_size,
2452 .set_lock_data = mdc_set_lock_data,
2453 .lock_match = mdc_lock_match,
2454 .get_lustre_md = mdc_get_lustre_md,
2455 .free_lustre_md = mdc_free_lustre_md,
2456 .set_open_replay_data = mdc_set_open_replay_data,
2457 .clear_open_replay_data = mdc_clear_open_replay_data,
2458 .intent_getattr_async = mdc_intent_getattr_async,
2459 .revalidate_lock = mdc_revalidate_lock
2462 static int __init mdc_init(void)
2464 struct lprocfs_static_vars lvars = { NULL };
2466 lprocfs_mdc_init_vars(&lvars);
2468 return class_register_type(&mdc_obd_ops, &mdc_md_ops,
2469 LUSTRE_MDC_NAME, NULL);
2472 static void /*__exit*/ mdc_exit(void)
2474 class_unregister_type(LUSTRE_MDC_NAME);
2477 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2478 MODULE_DESCRIPTION("Lustre Metadata Client");
2479 MODULE_VERSION(LUSTRE_VERSION_STRING);
2480 MODULE_LICENSE("GPL");
2482 module_init(mdc_init);
2483 module_exit(mdc_exit);