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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_MDC
40 # include <linux/module.h>
41 # include <linux/pagemap.h>
42 # include <linux/miscdevice.h>
43 # include <linux/init.h>
45 # include <liblustre.h>
48 #include <lustre_acl.h>
49 #include <obd_class.h>
50 #include <lustre_fid.h>
51 #include <lprocfs_status.h>
52 #include <lustre_param.h>
53 #include <lustre_log.h>
55 #include "mdc_internal.h"
57 #define REQUEST_MINOR 244
59 struct mdc_renew_capa_args {
60 struct obd_capa *ra_oc;
61 renew_capa_cb_t ra_cb;
64 static int mdc_cleanup(struct obd_device *obd);
66 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
67 const struct req_msg_field *field, struct obd_capa **oc)
69 struct lustre_capa *capa;
73 /* swabbed already in mdc_enqueue */
74 capa = req_capsule_server_get(&req->rq_pill, field);
78 c = alloc_capa(CAPA_SITE_CLIENT);
80 CDEBUG(D_INFO, "alloc capa failed!\n");
89 static inline int mdc_queue_wait(struct ptlrpc_request *req)
91 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
94 /* mdc_enter_request() ensures that this client has no more
95 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
97 rc = mdc_enter_request(cli);
101 rc = ptlrpc_queue_wait(req);
102 mdc_exit_request(cli);
107 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
108 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
109 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
110 struct obd_capa **pc, int level, int msg_flags)
112 struct ptlrpc_request *req;
113 struct mdt_body *body;
117 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
118 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
122 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
123 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
124 req->rq_send_state = level;
126 ptlrpc_request_set_replen(req);
128 rc = ptlrpc_queue_wait(req);
132 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
134 GOTO(out, rc = -EPROTO);
136 if (body->valid & OBD_MD_FLMDSCAPA) {
137 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
142 *rootfid = body->fid1;
144 "root fid="DFID", last_committed="LPU64"\n",
146 lustre_msg_get_last_committed(req->rq_repmsg));
149 ptlrpc_req_finished(req);
153 /* This should be mdc_get_info("rootfid") */
154 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
155 struct obd_capa **pc)
157 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
162 * This function now is known to always saying that it will receive 4 buffers
163 * from server. Even for cases when acl_size and md_size is zero, RPC header
164 * will contain 4 fields and RPC itself will contain zero size fields. This is
165 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
166 * and thus zero, it shrinks it, making zero size. The same story about
167 * md_size. And this is course of problem when client waits for smaller number
168 * of fields. This issue will be fixed later when client gets aware of RPC
171 static int mdc_getattr_common(struct obd_export *exp,
172 struct ptlrpc_request *req)
174 struct req_capsule *pill = &req->rq_pill;
175 struct mdt_body *body;
180 /* Request message already built. */
181 rc = ptlrpc_queue_wait(req);
185 /* sanity check for the reply */
186 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
190 CDEBUG(D_NET, "mode: %o\n", body->mode);
192 if (body->eadatasize != 0) {
193 mdc_update_max_ea_from_body(exp, body);
195 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
201 if (body->valid & OBD_MD_FLRMTPERM) {
202 struct mdt_remote_perm *perm;
204 LASSERT(client_is_remote(exp));
205 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
206 lustre_swab_mdt_remote_perm);
211 if (body->valid & OBD_MD_FLMDSCAPA) {
212 struct lustre_capa *capa;
213 capa = req_capsule_server_get(pill, &RMF_CAPA1);
221 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
222 struct ptlrpc_request **request)
224 struct ptlrpc_request *req;
228 /* Single MDS without an LMV case */
229 if (op_data->op_flags & MF_GET_MDT_IDX) {
234 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
238 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
240 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
242 ptlrpc_request_free(req);
246 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
247 op_data->op_valid, op_data->op_mode, -1, 0);
249 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
251 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
252 LASSERT(client_is_remote(exp));
253 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
254 sizeof(struct mdt_remote_perm));
256 ptlrpc_request_set_replen(req);
258 rc = mdc_getattr_common(exp, req);
260 ptlrpc_req_finished(req);
266 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
267 struct ptlrpc_request **request)
269 struct ptlrpc_request *req;
274 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
275 &RQF_MDS_GETATTR_NAME);
279 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
280 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
281 op_data->op_namelen + 1);
283 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
285 ptlrpc_request_free(req);
289 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
290 op_data->op_valid, op_data->op_mode,
291 op_data->op_suppgids[0], 0);
293 if (op_data->op_name) {
294 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
295 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
296 op_data->op_namelen);
297 memcpy(name, op_data->op_name, op_data->op_namelen);
300 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
302 ptlrpc_request_set_replen(req);
304 rc = mdc_getattr_common(exp, req);
306 ptlrpc_req_finished(req);
312 static int mdc_is_subdir(struct obd_export *exp,
313 const struct lu_fid *pfid,
314 const struct lu_fid *cfid,
315 struct ptlrpc_request **request)
317 struct ptlrpc_request *req;
323 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
324 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
329 mdc_is_subdir_pack(req, pfid, cfid, 0);
330 ptlrpc_request_set_replen(req);
332 rc = ptlrpc_queue_wait(req);
333 if (rc && rc != -EREMOTE)
334 ptlrpc_req_finished(req);
340 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
341 const struct lu_fid *fid,
342 struct obd_capa *oc, int opcode, obd_valid valid,
343 const char *xattr_name, const char *input,
344 int input_size, int output_size, int flags,
345 __u32 suppgid, struct ptlrpc_request **request)
347 struct ptlrpc_request *req;
348 int xattr_namelen = 0;
354 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
358 mdc_set_capa_size(req, &RMF_CAPA1, oc);
360 xattr_namelen = strlen(xattr_name) + 1;
361 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
366 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
370 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
372 ptlrpc_request_free(req);
376 if (opcode == MDS_REINT) {
377 struct mdt_rec_setxattr *rec;
379 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
380 sizeof(struct mdt_rec_reint));
381 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
382 rec->sx_opcode = REINT_SETXATTR;
384 * cfs_curproc_fs{u,g}id() should replace
385 * current->fs{u,g}id for portability.
387 rec->sx_fsuid = cfs_curproc_fsuid();
388 rec->sx_fsgid = cfs_curproc_fsgid();
389 rec->sx_cap = cfs_curproc_cap_pack();
390 rec->sx_suppgid1 = suppgid;
391 rec->sx_suppgid2 = -1;
393 rec->sx_valid = valid | OBD_MD_FLCTIME;
394 rec->sx_time = cfs_time_current_sec();
395 rec->sx_size = output_size;
396 rec->sx_flags = flags;
398 mdc_pack_capa(req, &RMF_CAPA1, oc);
400 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
404 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
405 memcpy(tmp, xattr_name, xattr_namelen);
408 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
409 memcpy(tmp, input, input_size);
412 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
413 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
414 RCL_SERVER, output_size);
415 ptlrpc_request_set_replen(req);
418 if (opcode == MDS_REINT)
419 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
421 rc = ptlrpc_queue_wait(req);
423 if (opcode == MDS_REINT)
424 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
427 ptlrpc_req_finished(req);
433 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
434 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
435 const char *input, int input_size, int output_size,
436 int flags, __u32 suppgid, struct ptlrpc_request **request)
438 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
439 fid, oc, MDS_REINT, valid, xattr_name,
440 input, input_size, output_size, flags,
444 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
445 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
446 const char *input, int input_size, int output_size,
447 int flags, struct ptlrpc_request **request)
449 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
450 fid, oc, MDS_GETXATTR, valid, xattr_name,
451 input, input_size, output_size, flags,
455 #ifdef CONFIG_FS_POSIX_ACL
456 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
458 struct req_capsule *pill = &req->rq_pill;
459 struct mdt_body *body = md->body;
460 struct posix_acl *acl;
468 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
473 acl = posix_acl_from_xattr(buf, body->aclsize);
476 CERROR("convert xattr to acl: %d\n", rc);
480 rc = posix_acl_valid(acl);
482 CERROR("validate acl: %d\n", rc);
483 posix_acl_release(acl);
491 #define mdc_unpack_acl(req, md) 0
494 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
495 struct obd_export *dt_exp, struct obd_export *md_exp,
496 struct lustre_md *md)
498 struct req_capsule *pill = &req->rq_pill;
503 memset(md, 0, sizeof(*md));
505 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
506 LASSERT(md->body != NULL);
508 if (md->body->valid & OBD_MD_FLEASIZE) {
510 struct lov_mds_md *lmm;
512 if (!S_ISREG(md->body->mode)) {
513 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
514 "regular file, but is not\n");
515 GOTO(out, rc = -EPROTO);
518 if (md->body->eadatasize == 0) {
519 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
520 "but eadatasize 0\n");
521 GOTO(out, rc = -EPROTO);
523 lmmsize = md->body->eadatasize;
524 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
526 GOTO(out, rc = -EPROTO);
528 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
532 if (rc < sizeof(*md->lsm)) {
533 CDEBUG(D_INFO, "lsm size too small: "
534 "rc < sizeof (*md->lsm) (%d < %d)\n",
535 rc, (int)sizeof(*md->lsm));
536 GOTO(out, rc = -EPROTO);
539 } else if (md->body->valid & OBD_MD_FLDIREA) {
541 struct lov_mds_md *lmv;
543 if(!S_ISDIR(md->body->mode)) {
544 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
545 "directory, but is not\n");
546 GOTO(out, rc = -EPROTO);
549 if (md->body->eadatasize == 0) {
550 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
551 "but eadatasize 0\n");
554 if (md->body->valid & OBD_MD_MEA) {
555 lmvsize = md->body->eadatasize;
556 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
559 GOTO(out, rc = -EPROTO);
561 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
566 if (rc < sizeof(*md->mea)) {
567 CDEBUG(D_INFO, "size too small: "
568 "rc < sizeof(*md->mea) (%d < %d)\n",
569 rc, (int)sizeof(*md->mea));
570 GOTO(out, rc = -EPROTO);
576 if (md->body->valid & OBD_MD_FLRMTPERM) {
577 /* remote permission */
578 LASSERT(client_is_remote(exp));
579 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
580 lustre_swab_mdt_remote_perm);
581 if (!md->remote_perm)
582 GOTO(out, rc = -EPROTO);
584 else if (md->body->valid & OBD_MD_FLACL) {
585 /* for ACL, it's possible that FLACL is set but aclsize is zero.
586 * only when aclsize != 0 there's an actual segment for ACL
589 if (md->body->aclsize) {
590 rc = mdc_unpack_acl(req, md);
593 #ifdef CONFIG_FS_POSIX_ACL
595 md->posix_acl = NULL;
599 if (md->body->valid & OBD_MD_FLMDSCAPA) {
600 struct obd_capa *oc = NULL;
602 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
608 if (md->body->valid & OBD_MD_FLOSSCAPA) {
609 struct obd_capa *oc = NULL;
611 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
621 capa_put(md->oss_capa);
625 capa_put(md->mds_capa);
628 #ifdef CONFIG_FS_POSIX_ACL
629 posix_acl_release(md->posix_acl);
632 obd_free_memmd(dt_exp, &md->lsm);
637 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
644 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
647 void mdc_replay_open(struct ptlrpc_request *req)
649 struct md_open_data *mod = req->rq_cb_data;
650 struct ptlrpc_request *close_req;
651 struct obd_client_handle *och;
652 struct lustre_handle old;
653 struct mdt_body *body;
657 DEBUG_REQ(D_ERROR, req,
658 "Can't properly replay without open data.");
663 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
664 LASSERT(body != NULL);
668 struct lustre_handle *file_fh;
670 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
672 file_fh = &och->och_fh;
673 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
674 file_fh->cookie, body->handle.cookie);
676 *file_fh = body->handle;
678 close_req = mod->mod_close_req;
679 if (close_req != NULL) {
680 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
681 struct mdt_ioepoch *epoch;
683 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
684 epoch = req_capsule_client_get(&close_req->rq_pill,
689 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
690 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
691 epoch->handle = body->handle;
696 void mdc_commit_open(struct ptlrpc_request *req)
698 struct md_open_data *mod = req->rq_cb_data;
703 * No need to touch md_open_data::mod_och, it holds a reference on
704 * \var mod and will zero references to each other, \var mod will be
705 * freed after that when md_open_data::mod_och will put the reference.
709 * Do not let open request to disappear as it still may be needed
710 * for close rpc to happen (it may happen on evict only, otherwise
711 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
712 * called), just mark this rpc as committed to distinguish these 2
713 * cases, see mdc_close() for details. The open request reference will
714 * be put along with freeing \var mod.
716 ptlrpc_request_addref(req);
717 spin_lock(&req->rq_lock);
718 req->rq_committed = 1;
719 spin_unlock(&req->rq_lock);
720 req->rq_cb_data = NULL;
724 int mdc_set_open_replay_data(struct obd_export *exp,
725 struct obd_client_handle *och,
726 struct ptlrpc_request *open_req)
728 struct md_open_data *mod;
729 struct mdt_rec_create *rec;
730 struct mdt_body *body;
731 struct obd_import *imp = open_req->rq_import;
734 if (!open_req->rq_replay)
737 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
738 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
739 LASSERT(rec != NULL);
740 /* Incoming message in my byte order (it's been swabbed). */
741 /* Outgoing messages always in my byte order. */
742 LASSERT(body != NULL);
744 /* Only if the import is replayable, we set replay_open data */
745 if (och && imp->imp_replayable) {
746 mod = obd_mod_alloc();
748 DEBUG_REQ(D_ERROR, open_req,
749 "Can't allocate md_open_data");
754 * Take a reference on \var mod, to be freed on mdc_close().
755 * It protects \var mod from being freed on eviction (commit
756 * callback is called despite rq_replay flag).
757 * Another reference for \var och.
762 spin_lock(&open_req->rq_lock);
765 mod->mod_open_req = open_req;
766 open_req->rq_cb_data = mod;
767 open_req->rq_commit_cb = mdc_commit_open;
768 spin_unlock(&open_req->rq_lock);
771 rec->cr_fid2 = body->fid1;
772 rec->cr_ioepoch = body->ioepoch;
773 rec->cr_old_handle.cookie = body->handle.cookie;
774 open_req->rq_replay_cb = mdc_replay_open;
775 if (!fid_is_sane(&body->fid1)) {
776 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
781 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
785 int mdc_clear_open_replay_data(struct obd_export *exp,
786 struct obd_client_handle *och)
788 struct md_open_data *mod = och->och_mod;
792 * It is possible to not have \var mod in a case of eviction between
793 * lookup and ll_file_open().
798 LASSERT(mod != LP_POISON);
807 /* Prepares the request for the replay by the given reply */
808 static void mdc_close_handle_reply(struct ptlrpc_request *req,
809 struct md_op_data *op_data, int rc) {
810 struct mdt_body *repbody;
811 struct mdt_ioepoch *epoch;
813 if (req && rc == -EAGAIN) {
814 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
815 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
817 epoch->flags |= MF_SOM_AU;
818 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
819 op_data->op_flags |= MF_GETATTR_LOCK;
823 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
824 struct md_open_data *mod, struct ptlrpc_request **request)
826 struct obd_device *obd = class_exp2obd(exp);
827 struct ptlrpc_request *req;
832 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
836 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
838 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
840 ptlrpc_request_free(req);
844 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
845 * portal whose threads are not taking any DLM locks and are therefore
846 * always progressing */
847 req->rq_request_portal = MDS_READPAGE_PORTAL;
848 ptlrpc_at_set_req_timeout(req);
850 /* Ensure that this close's handle is fixed up during replay. */
851 if (likely(mod != NULL)) {
852 LASSERTF(mod->mod_open_req != NULL &&
853 mod->mod_open_req->rq_type != LI_POISON,
854 "POISONED open %p!\n", mod->mod_open_req);
856 mod->mod_close_req = req;
858 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
859 /* We no longer want to preserve this open for replay even
860 * though the open was committed. b=3632, b=3633 */
861 spin_lock(&mod->mod_open_req->rq_lock);
862 mod->mod_open_req->rq_replay = 0;
863 spin_unlock(&mod->mod_open_req->rq_lock);
865 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
868 mdc_close_pack(req, op_data);
870 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
871 obd->u.cli.cl_max_mds_easize);
872 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
873 obd->u.cli.cl_max_mds_cookiesize);
875 ptlrpc_request_set_replen(req);
877 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
878 rc = ptlrpc_queue_wait(req);
879 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
881 if (req->rq_repmsg == NULL) {
882 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
885 rc = req->rq_status ?: -EIO;
886 } else if (rc == 0 || rc == -EAGAIN) {
887 struct mdt_body *body;
889 rc = lustre_msg_get_status(req->rq_repmsg);
890 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
891 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
896 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
899 } else if (rc == -ESTALE) {
901 * it can be allowed error after 3633 if open was committed and
902 * server failed before close was sent. Let's check if mod
903 * exists and return no error in that case
906 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
907 LASSERT(mod->mod_open_req != NULL);
908 if (mod->mod_open_req->rq_committed)
915 mod->mod_close_req = NULL;
916 /* Since now, mod is accessed through open_req only,
917 * thus close req does not keep a reference on mod anymore. */
921 mdc_close_handle_reply(req, op_data, rc);
925 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
926 struct md_open_data *mod)
928 struct obd_device *obd = class_exp2obd(exp);
929 struct ptlrpc_request *req;
933 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
934 &RQF_MDS_DONE_WRITING);
938 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
939 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
941 ptlrpc_request_free(req);
946 LASSERTF(mod->mod_open_req != NULL &&
947 mod->mod_open_req->rq_type != LI_POISON,
948 "POISONED setattr %p!\n", mod->mod_open_req);
950 mod->mod_close_req = req;
951 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
952 /* We no longer want to preserve this setattr for replay even
953 * though the open was committed. b=3632, b=3633 */
954 spin_lock(&mod->mod_open_req->rq_lock);
955 mod->mod_open_req->rq_replay = 0;
956 spin_unlock(&mod->mod_open_req->rq_lock);
959 mdc_close_pack(req, op_data);
960 ptlrpc_request_set_replen(req);
962 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
963 rc = ptlrpc_queue_wait(req);
964 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
968 * it can be allowed error after 3633 if open or setattr were
969 * committed and server failed before close was sent.
970 * Let's check if mod exists and return no error in that case
973 LASSERT(mod->mod_open_req != NULL);
974 if (mod->mod_open_req->rq_committed)
981 mod->mod_close_req = NULL;
982 /* Since now, mod is accessed through setattr req only,
983 * thus DW req does not keep a reference on mod anymore. */
987 mdc_close_handle_reply(req, op_data, rc);
988 ptlrpc_req_finished(req);
992 #ifdef HAVE_SPLIT_SUPPORT
993 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
994 const struct page *page, int offset)
996 struct ptlrpc_request *req;
997 struct ptlrpc_bulk_desc *desc;
1001 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1005 /* FIXME: capa doesn't support split yet */
1006 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1008 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1010 ptlrpc_request_free(req);
1014 req->rq_request_portal = MDS_READPAGE_PORTAL;
1015 ptlrpc_at_set_req_timeout(req);
1017 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1019 GOTO(out, rc = -ENOMEM);
1021 /* NB req now owns desc and will free it when it gets freed. */
1022 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1023 mdc_readdir_pack(req, 0, offset, fid, NULL);
1025 ptlrpc_request_set_replen(req);
1026 rc = ptlrpc_queue_wait(req);
1030 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1032 ptlrpc_req_finished(req);
1035 EXPORT_SYMBOL(mdc_sendpage);
1038 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1039 struct page **pages, struct ptlrpc_request **request)
1041 struct ptlrpc_request *req;
1042 struct ptlrpc_bulk_desc *desc;
1046 struct l_wait_info lwi;
1051 cfs_waitq_init(&waitq);
1054 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1058 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1060 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1062 ptlrpc_request_free(req);
1066 req->rq_request_portal = MDS_READPAGE_PORTAL;
1067 ptlrpc_at_set_req_timeout(req);
1069 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1072 ptlrpc_request_free(req);
1076 /* NB req now owns desc and will free it when it gets freed */
1077 for (i = 0; i < op_data->op_npages; i++)
1078 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, CFS_PAGE_SIZE);
1080 mdc_readdir_pack(req, op_data->op_offset,
1081 CFS_PAGE_SIZE * op_data->op_npages,
1082 &op_data->op_fid1, op_data->op_capa1);
1084 ptlrpc_request_set_replen(req);
1085 rc = ptlrpc_queue_wait(req);
1087 ptlrpc_req_finished(req);
1088 if (rc != -ETIMEDOUT)
1092 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1093 CERROR("too many resend retries, returning error\n");
1096 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1097 l_wait_event(waitq, 0, &lwi);
1102 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1103 req->rq_bulk->bd_nob_transferred);
1105 ptlrpc_req_finished(req);
1109 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1110 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1111 req->rq_bulk->bd_nob_transferred,
1112 CFS_PAGE_SIZE * op_data->op_npages);
1113 ptlrpc_req_finished(req);
1121 static int mdc_statfs(const struct lu_env *env,
1122 struct obd_export *exp, struct obd_statfs *osfs,
1123 __u64 max_age, __u32 flags)
1125 struct obd_device *obd = class_exp2obd(exp);
1126 struct ptlrpc_request *req;
1127 struct obd_statfs *msfs;
1128 struct obd_import *imp = NULL;
1133 * Since the request might also come from lprocfs, so we need
1134 * sync this with client_disconnect_export Bug15684
1136 down_read(&obd->u.cli.cl_sem);
1137 if (obd->u.cli.cl_import)
1138 imp = class_import_get(obd->u.cli.cl_import);
1139 up_read(&obd->u.cli.cl_sem);
1143 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1144 LUSTRE_MDS_VERSION, MDS_STATFS);
1146 GOTO(output, rc = -ENOMEM);
1148 ptlrpc_request_set_replen(req);
1150 if (flags & OBD_STATFS_NODELAY) {
1151 /* procfs requests not want stay in wait for avoid deadlock */
1152 req->rq_no_resend = 1;
1153 req->rq_no_delay = 1;
1156 rc = ptlrpc_queue_wait(req);
1158 /* check connection error first */
1159 if (imp->imp_connect_error)
1160 rc = imp->imp_connect_error;
1164 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1166 GOTO(out, rc = -EPROTO);
1171 ptlrpc_req_finished(req);
1173 class_import_put(imp);
1177 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1179 __u32 keylen, vallen;
1183 if (gf->gf_pathlen > PATH_MAX)
1184 RETURN(-ENAMETOOLONG);
1185 if (gf->gf_pathlen < 2)
1188 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1189 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1190 OBD_ALLOC(key, keylen);
1193 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1194 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1196 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1197 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1199 if (!fid_is_sane(&gf->gf_fid))
1200 GOTO(out, rc = -EINVAL);
1202 /* Val is struct getinfo_fid2path result plus path */
1203 vallen = sizeof(*gf) + gf->gf_pathlen;
1205 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1206 if (rc != 0 && rc != -EREMOTE)
1209 if (vallen <= sizeof(*gf))
1210 GOTO(out, rc = -EPROTO);
1211 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1212 GOTO(out, rc = -EOVERFLOW);
1214 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1215 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1218 OBD_FREE(key, keylen);
1222 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1223 struct hsm_progress_kernel *hpk)
1225 struct obd_import *imp = class_exp2cliimp(exp);
1226 struct hsm_progress_kernel *req_hpk;
1227 struct ptlrpc_request *req;
1231 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1232 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1234 GOTO(out, rc = -ENOMEM);
1236 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1238 /* Copy hsm_progress struct */
1239 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1240 if (req_hpk == NULL)
1241 GOTO(out, rc = -EPROTO);
1245 ptlrpc_request_set_replen(req);
1247 rc = mdc_queue_wait(req);
1250 ptlrpc_req_finished(req);
1254 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1256 __u32 *archive_mask;
1257 struct ptlrpc_request *req;
1261 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1263 MDS_HSM_CT_REGISTER);
1265 GOTO(out, rc = -ENOMEM);
1267 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1269 /* Copy hsm_progress struct */
1270 archive_mask = req_capsule_client_get(&req->rq_pill,
1271 &RMF_MDS_HSM_ARCHIVE);
1272 if (archive_mask == NULL)
1273 GOTO(out, rc = -EPROTO);
1275 *archive_mask = archives;
1277 ptlrpc_request_set_replen(req);
1279 rc = mdc_queue_wait(req);
1282 ptlrpc_req_finished(req);
1286 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1287 struct md_op_data *op_data)
1289 struct hsm_current_action *hca = op_data->op_data;
1290 struct hsm_current_action *req_hca;
1291 struct ptlrpc_request *req;
1295 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1296 &RQF_MDS_HSM_ACTION);
1300 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1302 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1304 ptlrpc_request_free(req);
1308 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1309 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1311 ptlrpc_request_set_replen(req);
1313 rc = mdc_queue_wait(req);
1317 req_hca = req_capsule_server_get(&req->rq_pill,
1318 &RMF_MDS_HSM_CURRENT_ACTION);
1319 if (req_hca == NULL)
1320 GOTO(out, rc = -EPROTO);
1326 ptlrpc_req_finished(req);
1330 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1332 struct ptlrpc_request *req;
1336 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1338 MDS_HSM_CT_UNREGISTER);
1340 GOTO(out, rc = -ENOMEM);
1342 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1344 ptlrpc_request_set_replen(req);
1346 rc = mdc_queue_wait(req);
1349 ptlrpc_req_finished(req);
1353 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1354 struct md_op_data *op_data)
1356 struct hsm_user_state *hus = op_data->op_data;
1357 struct hsm_user_state *req_hus;
1358 struct ptlrpc_request *req;
1362 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1363 &RQF_MDS_HSM_STATE_GET);
1367 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1369 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1371 ptlrpc_request_free(req);
1375 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1376 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1378 ptlrpc_request_set_replen(req);
1380 rc = mdc_queue_wait(req);
1384 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1385 if (req_hus == NULL)
1386 GOTO(out, rc = -EPROTO);
1392 ptlrpc_req_finished(req);
1396 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1397 struct md_op_data *op_data)
1399 struct hsm_state_set *hss = op_data->op_data;
1400 struct hsm_state_set *req_hss;
1401 struct ptlrpc_request *req;
1405 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1406 &RQF_MDS_HSM_STATE_SET);
1410 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1412 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1414 ptlrpc_request_free(req);
1418 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1419 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1422 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1423 if (req_hss == NULL)
1424 GOTO(out, rc = -EPROTO);
1427 ptlrpc_request_set_replen(req);
1429 rc = mdc_queue_wait(req);
1434 ptlrpc_req_finished(req);
1438 static int mdc_ioc_hsm_request(struct obd_export *exp,
1439 struct hsm_user_request *hur)
1441 struct obd_import *imp = class_exp2cliimp(exp);
1442 struct ptlrpc_request *req;
1443 struct hsm_request *req_hr;
1444 struct hsm_user_item *req_hui;
1449 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1451 GOTO(out, rc = -ENOMEM);
1453 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1454 hur->hur_request.hr_itemcount
1455 * sizeof(struct hsm_user_item));
1456 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1457 hur->hur_request.hr_data_len);
1459 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1461 ptlrpc_request_free(req);
1465 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1467 /* Copy hsm_request struct */
1468 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1470 GOTO(out, rc = -EPROTO);
1471 *req_hr = hur->hur_request;
1473 /* Copy hsm_user_item structs */
1474 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1475 if (req_hui == NULL)
1476 GOTO(out, rc = -EPROTO);
1477 memcpy(req_hui, hur->hur_user_item,
1478 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1480 /* Copy opaque field */
1481 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1482 if (req_opaque == NULL)
1483 GOTO(out, rc = -EPROTO);
1484 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1486 ptlrpc_request_set_replen(req);
1488 rc = mdc_queue_wait(req);
1492 ptlrpc_req_finished(req);
1496 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1498 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1500 LASSERT(len <= CR_MAXSIZE);
1502 lh->kuc_magic = KUC_MAGIC;
1503 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1504 lh->kuc_flags = flags;
1505 lh->kuc_msgtype = CL_RECORD;
1506 lh->kuc_msglen = len;
1510 #define D_CHANGELOG 0
1512 struct changelog_show {
1517 struct obd_device *cs_obd;
1520 static int changelog_show_cb(const struct lu_env *env, struct llog_handle *llh,
1521 struct llog_rec_hdr *hdr, void *data)
1523 struct changelog_show *cs = data;
1524 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1529 if ((rec->cr_hdr.lrh_type != CHANGELOG_REC) ||
1530 (rec->cr.cr_type >= CL_LAST)) {
1531 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
1536 if (rec->cr.cr_index < cs->cs_startrec) {
1537 /* Skip entries earlier than what we are interested in */
1538 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1539 rec->cr.cr_index, cs->cs_startrec);
1543 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1544 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1545 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1546 rec->cr.cr_flags & CLF_FLAGMASK,
1547 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1548 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1550 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1552 /* Set up the message */
1553 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1554 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1556 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1557 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1562 static int mdc_changelog_send_thread(void *csdata)
1564 struct changelog_show *cs = csdata;
1565 struct llog_ctxt *ctxt = NULL;
1566 struct llog_handle *llh = NULL;
1567 struct kuc_hdr *kuch;
1570 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1571 cs->cs_fp, cs->cs_startrec);
1574 * It's important to daemonize here to close unused FDs.
1575 * The write fd from pipe is already opened by the caller,
1576 * so it's fine to clear all files here
1578 cfs_daemonize("mdc_clg_send_thread");
1580 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1581 if (cs->cs_buf == NULL)
1582 GOTO(out, rc = -ENOMEM);
1584 /* Set up the remote catalog handle */
1585 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1587 GOTO(out, rc = -ENOENT);
1588 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1591 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1592 cs->cs_obd->obd_name, rc);
1595 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1597 CERROR("llog_init_handle failed %d\n", rc);
1601 rc = llog_cat_process(NULL, llh, changelog_show_cb, cs, 0, 0);
1603 /* Send EOF no matter what our result */
1604 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1606 kuch->kuc_msgtype = CL_EOF;
1607 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1613 llog_cat_close(NULL, llh);
1615 llog_ctxt_put(ctxt);
1617 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1619 /* detach from parent process so we get cleaned up */
1620 cfs_daemonize("cl_send");
1624 static int mdc_ioc_changelog_send(struct obd_device *obd,
1625 struct ioc_changelog *icc)
1627 struct changelog_show *cs;
1630 /* Freed in mdc_changelog_send_thread */
1636 cs->cs_startrec = icc->icc_recno;
1637 /* matching fput in mdc_changelog_send_thread */
1638 cs->cs_fp = fget(icc->icc_id);
1639 cs->cs_flags = icc->icc_flags;
1641 /* New thread because we should return to user app before
1642 writing into our pipe */
1643 rc = cfs_create_thread(mdc_changelog_send_thread, cs, CFS_DAEMON_FLAGS);
1645 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1649 CERROR("Failed to start changelog thread: %d\n", rc);
1654 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1655 struct lustre_kernelcomm *lk);
1657 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1658 struct obd_quotactl *oqctl)
1660 struct client_obd *cli = &exp->exp_obd->u.cli;
1661 struct ptlrpc_request *req;
1662 struct obd_quotactl *body;
1666 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1667 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1672 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1675 ptlrpc_request_set_replen(req);
1677 /* the next poll will find -ENODATA, that means quotacheck is
1679 cli->cl_qchk_stat = -ENODATA;
1680 rc = ptlrpc_queue_wait(req);
1682 cli->cl_qchk_stat = rc;
1683 ptlrpc_req_finished(req);
1687 static int mdc_quota_poll_check(struct obd_export *exp,
1688 struct if_quotacheck *qchk)
1690 struct client_obd *cli = &exp->exp_obd->u.cli;
1694 qchk->obd_uuid = cli->cl_target_uuid;
1695 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1697 rc = cli->cl_qchk_stat;
1698 /* the client is not the previous one */
1699 if (rc == CL_NOT_QUOTACHECKED)
1704 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1705 struct obd_quotactl *oqctl)
1707 struct ptlrpc_request *req;
1708 struct obd_quotactl *oqc;
1712 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1713 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1718 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1721 ptlrpc_request_set_replen(req);
1722 ptlrpc_at_set_req_timeout(req);
1723 req->rq_no_resend = 1;
1725 rc = ptlrpc_queue_wait(req);
1727 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1729 if (req->rq_repmsg &&
1730 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1733 CERROR ("Can't unpack obd_quotactl\n");
1736 ptlrpc_req_finished(req);
1741 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1742 struct md_op_data *op_data)
1744 CFS_LIST_HEAD(cancels);
1745 struct ptlrpc_request *req;
1747 struct mdc_swap_layouts *msl, *payload;
1750 msl = op_data->op_data;
1752 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1753 * first thing it will do is to cancel the 2 layout
1754 * locks hold by this client.
1755 * So the client must cancel its layout locks on the 2 fids
1756 * with the request RPC to avoid extra RPC round trips
1758 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1759 LCK_CR, MDS_INODELOCK_LAYOUT);
1760 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1761 LCK_CR, MDS_INODELOCK_LAYOUT);
1763 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1764 &RQF_MDS_SWAP_LAYOUTS);
1766 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1770 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1771 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1773 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1775 ptlrpc_request_free(req);
1779 mdc_swap_layouts_pack(req, op_data);
1781 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1786 ptlrpc_request_set_replen(req);
1788 rc = ptlrpc_queue_wait(req);
1794 ptlrpc_req_finished(req);
1798 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1799 void *karg, void *uarg)
1801 struct obd_device *obd = exp->exp_obd;
1802 struct obd_ioctl_data *data = karg;
1803 struct obd_import *imp = obd->u.cli.cl_import;
1804 struct llog_ctxt *ctxt;
1808 if (!cfs_try_module_get(THIS_MODULE)) {
1809 CERROR("Can't get module. Is it alive?");
1813 case OBD_IOC_CHANGELOG_SEND:
1814 rc = mdc_ioc_changelog_send(obd, karg);
1816 case OBD_IOC_CHANGELOG_CLEAR: {
1817 struct ioc_changelog *icc = karg;
1818 struct changelog_setinfo cs =
1819 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1820 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1821 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1825 case OBD_IOC_FID2PATH:
1826 rc = mdc_ioc_fid2path(exp, karg);
1828 case LL_IOC_HSM_CT_START:
1829 rc = mdc_ioc_hsm_ct_start(exp, karg);
1831 case LL_IOC_HSM_PROGRESS:
1832 rc = mdc_ioc_hsm_progress(exp, karg);
1834 case LL_IOC_HSM_STATE_GET:
1835 rc = mdc_ioc_hsm_state_get(exp, karg);
1837 case LL_IOC_HSM_STATE_SET:
1838 rc = mdc_ioc_hsm_state_set(exp, karg);
1839 case LL_IOC_HSM_ACTION:
1840 rc = mdc_ioc_hsm_current_action(exp, karg);
1842 case LL_IOC_HSM_REQUEST:
1843 rc = mdc_ioc_hsm_request(exp, karg);
1845 case OBD_IOC_CLIENT_RECOVER:
1846 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1850 case IOC_OSC_SET_ACTIVE:
1851 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1853 case OBD_IOC_PARSE: {
1854 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1855 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1857 llog_ctxt_put(ctxt);
1861 case OBD_IOC_LLOG_INFO:
1862 case OBD_IOC_LLOG_PRINT: {
1863 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1864 rc = llog_ioctl(NULL, ctxt, cmd, data);
1865 llog_ctxt_put(ctxt);
1869 case OBD_IOC_POLL_QUOTACHECK:
1870 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1872 case OBD_IOC_PING_TARGET:
1873 rc = ptlrpc_obd_ping(obd);
1876 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1877 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1878 * there'd be no LMV layer thus we might be called here. Eventually
1879 * this code should be removed.
1882 case IOC_OBD_STATFS: {
1883 struct obd_statfs stat_buf = {0};
1885 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1886 GOTO(out, rc = -ENODEV);
1889 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1890 min((int) data->ioc_plen2,
1891 (int) sizeof(struct obd_uuid))))
1892 GOTO(out, rc = -EFAULT);
1894 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1895 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1900 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1901 min((int) data->ioc_plen1,
1902 (int) sizeof(stat_buf))))
1903 GOTO(out, rc = -EFAULT);
1907 case OBD_IOC_QUOTACTL: {
1908 struct if_quotactl *qctl = karg;
1909 struct obd_quotactl *oqctl;
1911 OBD_ALLOC_PTR(oqctl);
1915 QCTL_COPY(oqctl, qctl);
1916 rc = obd_quotactl(exp, oqctl);
1918 QCTL_COPY(qctl, oqctl);
1919 qctl->qc_valid = QC_MDTIDX;
1920 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1922 OBD_FREE_PTR(oqctl);
1925 case LL_IOC_GET_CONNECT_FLAGS: {
1926 if (cfs_copy_to_user(uarg,
1927 exp_connect_flags_ptr(exp),
1929 GOTO(out, rc = -EFAULT);
1933 case LL_IOC_LOV_SWAP_LAYOUTS: {
1934 rc = mdc_ioc_swap_layouts(exp, karg);
1938 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1939 GOTO(out, rc = -ENOTTY);
1942 cfs_module_put(THIS_MODULE);
1947 int mdc_get_info_rpc(struct obd_export *exp,
1948 obd_count keylen, void *key,
1949 int vallen, void *val)
1951 struct obd_import *imp = class_exp2cliimp(exp);
1952 struct ptlrpc_request *req;
1957 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1961 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1962 RCL_CLIENT, keylen);
1963 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1964 RCL_CLIENT, sizeof(__u32));
1966 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1968 ptlrpc_request_free(req);
1972 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1973 memcpy(tmp, key, keylen);
1974 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1975 memcpy(tmp, &vallen, sizeof(__u32));
1977 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1978 RCL_SERVER, vallen);
1979 ptlrpc_request_set_replen(req);
1981 rc = ptlrpc_queue_wait(req);
1982 /* -EREMOTE means the get_info result is partial, and it needs to
1983 * continue on another MDT, see fid2path part in lmv_iocontrol */
1984 if (rc == 0 || rc == -EREMOTE) {
1985 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1986 memcpy(val, tmp, vallen);
1987 if (ptlrpc_rep_need_swab(req)) {
1988 if (KEY_IS(KEY_FID2PATH))
1989 lustre_swab_fid2path(val);
1992 ptlrpc_req_finished(req);
1997 static void lustre_swab_hai(struct hsm_action_item *h)
1999 __swab32s(&h->hai_len);
2000 __swab32s(&h->hai_action);
2001 lustre_swab_lu_fid(&h->hai_fid);
2002 lustre_swab_lu_fid(&h->hai_dfid);
2003 __swab64s(&h->hai_cookie);
2004 __swab64s(&h->hai_extent.offset);
2005 __swab64s(&h->hai_extent.length);
2006 __swab64s(&h->hai_gid);
2009 static void lustre_swab_hal(struct hsm_action_list *h)
2011 struct hsm_action_item *hai;
2014 __swab32s(&h->hal_version);
2015 __swab32s(&h->hal_count);
2016 __swab32s(&h->hal_archive_id);
2017 __swab64s(&h->hal_flags);
2019 for (i = 0; i < h->hal_count; i++) {
2020 lustre_swab_hai(hai);
2021 hai = hai_next(hai);
2025 static void lustre_swab_kuch(struct kuc_hdr *l)
2027 __swab16s(&l->kuc_magic);
2028 /* __u8 l->kuc_transport */
2029 __swab16s(&l->kuc_msgtype);
2030 __swab16s(&l->kuc_msglen);
2033 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2034 struct lustre_kernelcomm *lk)
2036 struct obd_import *imp = class_exp2cliimp(exp);
2037 __u32 archive = lk->lk_data;
2040 if (lk->lk_group != KUC_GRP_HSM) {
2041 CERROR("Bad copytool group %d\n", lk->lk_group);
2045 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2046 lk->lk_uid, lk->lk_group, lk->lk_flags);
2048 if (lk->lk_flags & LK_FLG_STOP) {
2049 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
2050 /* Unregister with the coordinator */
2052 rc = mdc_ioc_hsm_ct_unregister(imp);
2054 struct file *fp = fget(lk->lk_wfd);
2056 rc = libcfs_kkuc_group_add(fp, lk->lk_uid, lk->lk_group,
2061 rc = mdc_ioc_hsm_ct_register(imp, archive);
2068 * Send a message to any listening copytools
2069 * @param val KUC message (kuc_hdr + hsm_action_list)
2070 * @param len total length of message
2072 static int mdc_hsm_copytool_send(int len, void *val)
2074 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2075 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2079 if (len < sizeof(*lh) + sizeof(*hal)) {
2080 CERROR("Short HSM message %d < %d\n", len,
2081 (int) (sizeof(*lh) + sizeof(*hal)));
2084 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2085 lustre_swab_kuch(lh);
2086 lustre_swab_hal(hal);
2087 } else if (lh->kuc_magic != KUC_MAGIC) {
2088 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2092 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2094 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2095 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2097 /* Broadcast to HSM listeners */
2098 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2104 * callback function passed to kuc for re-registering each HSM copytool
2105 * running on MDC, after MDT shutdown/recovery.
2106 * @param data archive id served by the copytool
2107 * @param cb_arg callback argument (obd_import)
2109 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2111 struct obd_import *imp = (struct obd_import *)cb_arg;
2112 __u32 archive = data;
2115 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2117 rc = mdc_ioc_hsm_ct_register(imp, archive);
2119 /* ignore error if the copytool is already registered */
2120 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2124 * Re-establish all kuc contexts with MDT
2125 * after MDT shutdown/recovery.
2127 static int mdc_kuc_reregister(struct obd_import *imp)
2129 /* re-register HSM agents */
2130 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2134 int mdc_set_info_async(const struct lu_env *env,
2135 struct obd_export *exp,
2136 obd_count keylen, void *key,
2137 obd_count vallen, void *val,
2138 struct ptlrpc_request_set *set)
2140 struct obd_import *imp = class_exp2cliimp(exp);
2144 if (KEY_IS(KEY_READ_ONLY)) {
2145 if (vallen != sizeof(int))
2148 spin_lock(&imp->imp_lock);
2149 if (*((int *)val)) {
2150 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2151 imp->imp_connect_data.ocd_connect_flags |=
2154 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2155 imp->imp_connect_data.ocd_connect_flags &=
2156 ~OBD_CONNECT_RDONLY;
2158 spin_unlock(&imp->imp_lock);
2160 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2161 keylen, key, vallen, val, set);
2164 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2165 sptlrpc_conf_client_adapt(exp->exp_obd);
2168 if (KEY_IS(KEY_FLUSH_CTX)) {
2169 sptlrpc_import_flush_my_ctx(imp);
2172 if (KEY_IS(KEY_MDS_CONN)) {
2173 /* mds-mds import */
2174 spin_lock(&imp->imp_lock);
2175 imp->imp_server_timeout = 1;
2176 spin_unlock(&imp->imp_lock);
2177 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2178 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2181 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2182 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2183 keylen, key, vallen, val, set);
2186 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2187 rc = mdc_hsm_copytool_send(vallen, val);
2191 CERROR("Unknown key %s\n", (char *)key);
2195 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2196 __u32 keylen, void *key, __u32 *vallen, void *val,
2197 struct lov_stripe_md *lsm)
2201 if (KEY_IS(KEY_MAX_EASIZE)) {
2202 int mdsize, *max_easize;
2204 if (*vallen != sizeof(int))
2206 mdsize = *(int*)val;
2207 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2208 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2210 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2212 } else if (KEY_IS(KEY_CONN_DATA)) {
2213 struct obd_import *imp = class_exp2cliimp(exp);
2214 struct obd_connect_data *data = val;
2216 if (*vallen != sizeof(*data))
2219 *data = imp->imp_connect_data;
2221 } else if (KEY_IS(KEY_TGT_COUNT)) {
2226 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2231 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2232 struct obd_capa *oc, struct obd_client_handle *handle,
2235 struct ptlrpc_request *req;
2236 struct mdt_body *body;
2240 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2244 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2246 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2248 ptlrpc_request_free(req);
2252 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2254 ptlrpc_request_set_replen(req);
2256 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2257 rc = ptlrpc_queue_wait(req);
2258 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2260 CERROR("Pin failed: %d\n", rc);
2264 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2266 GOTO(err_out, rc = -EPROTO);
2268 handle->och_fh = body->handle;
2269 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2271 handle->och_mod = obd_mod_alloc();
2272 if (handle->och_mod == NULL) {
2273 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2274 GOTO(err_out, rc = -ENOMEM);
2276 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2281 ptlrpc_req_finished(req);
2285 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2288 struct ptlrpc_request *req;
2289 struct mdt_body *body;
2293 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2294 LUSTRE_MDS_VERSION, MDS_UNPIN);
2298 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2299 body->handle = handle->och_fh;
2302 ptlrpc_request_set_replen(req);
2304 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2305 rc = ptlrpc_queue_wait(req);
2306 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2309 CERROR("Unpin failed: %d\n", rc);
2311 ptlrpc_req_finished(req);
2312 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2314 obd_mod_put(handle->och_mod);
2318 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2319 struct obd_capa *oc, struct ptlrpc_request **request)
2321 struct ptlrpc_request *req;
2326 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2330 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2332 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2334 ptlrpc_request_free(req);
2338 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2340 ptlrpc_request_set_replen(req);
2342 rc = ptlrpc_queue_wait(req);
2344 ptlrpc_req_finished(req);
2350 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2351 enum obd_import_event event)
2355 LASSERT(imp->imp_obd == obd);
2358 case IMP_EVENT_DISCON: {
2360 /* XXX Pass event up to OBDs stack. used only for FLD now */
2361 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2365 case IMP_EVENT_INACTIVE: {
2366 struct client_obd *cli = &obd->u.cli;
2368 * Flush current sequence to make client obtain new one
2369 * from server in case of disconnect/reconnect.
2371 if (cli->cl_seq != NULL)
2372 seq_client_flush(cli->cl_seq);
2374 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2377 case IMP_EVENT_INVALIDATE: {
2378 struct ldlm_namespace *ns = obd->obd_namespace;
2380 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2384 case IMP_EVENT_ACTIVE:
2385 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2386 /* restore re-establish kuc registration after reconnecting */
2388 rc = mdc_kuc_reregister(imp);
2391 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2393 case IMP_EVENT_DEACTIVATE:
2394 case IMP_EVENT_ACTIVATE:
2397 CERROR("Unknown import event %x\n", event);
2403 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2404 struct md_op_data *op_data)
2406 struct client_obd *cli = &exp->exp_obd->u.cli;
2407 struct lu_client_seq *seq = cli->cl_seq;
2409 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2412 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2413 struct client_obd *cli = &exp->exp_obd->u.cli;
2414 return &cli->cl_target_uuid;
2418 * Determine whether the lock can be canceled before replaying it during
2419 * recovery, non zero value will be return if the lock can be canceled,
2420 * or zero returned for not
2422 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2424 if (lock->l_resource->lr_type != LDLM_IBITS)
2427 /* FIXME: if we ever get into a situation where there are too many
2428 * opened files with open locks on a single node, then we really
2429 * should replay these open locks to reget it */
2430 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2436 static int mdc_resource_inode_free(struct ldlm_resource *res)
2438 if (res->lr_lvb_inode)
2439 res->lr_lvb_inode = NULL;
2444 struct ldlm_valblock_ops inode_lvbo = {
2445 lvbo_free: mdc_resource_inode_free
2448 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2450 struct client_obd *cli = &obd->u.cli;
2451 struct lprocfs_static_vars lvars = { 0 };
2455 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2456 if (!cli->cl_rpc_lock)
2458 mdc_init_rpc_lock(cli->cl_rpc_lock);
2462 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2463 if (!cli->cl_close_lock)
2464 GOTO(err_rpc_lock, rc = -ENOMEM);
2465 mdc_init_rpc_lock(cli->cl_close_lock);
2467 rc = client_obd_setup(obd, cfg);
2469 GOTO(err_close_lock, rc);
2470 lprocfs_mdc_init_vars(&lvars);
2471 lprocfs_obd_setup(obd, lvars.obd_vars);
2472 sptlrpc_lprocfs_cliobd_attach(obd);
2473 ptlrpc_lprocfs_register_obd(obd);
2475 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2477 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2479 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2482 CERROR("failed to setup llogging subsystems\n");
2488 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2490 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2495 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2496 * us to make MDS RPCs with large enough reply buffers to hold the
2497 * maximum-sized (= maximum striped) EA and cookie without having to
2498 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2499 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2500 int def_easize, int cookiesize)
2502 struct obd_device *obd = exp->exp_obd;
2503 struct client_obd *cli = &obd->u.cli;
2506 if (cli->cl_max_mds_easize < easize)
2507 cli->cl_max_mds_easize = easize;
2509 if (cli->cl_default_mds_easize < def_easize)
2510 cli->cl_default_mds_easize = def_easize;
2512 if (cli->cl_max_mds_cookiesize < cookiesize)
2513 cli->cl_max_mds_cookiesize = cookiesize;
2518 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2524 case OBD_CLEANUP_EARLY:
2526 case OBD_CLEANUP_EXPORTS:
2527 /* Failsafe, ok if racy */
2528 if (obd->obd_type->typ_refcnt <= 1)
2529 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2531 obd_cleanup_client_import(obd);
2532 ptlrpc_lprocfs_unregister_obd(obd);
2533 lprocfs_obd_cleanup(obd);
2535 rc = obd_llog_finish(obd, 0);
2537 CERROR("failed to cleanup llogging subsystems\n");
2543 static int mdc_cleanup(struct obd_device *obd)
2545 struct client_obd *cli = &obd->u.cli;
2547 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2548 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2552 return client_obd_cleanup(obd);
2556 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2557 struct obd_device *tgt, int *index)
2559 struct llog_ctxt *ctxt;
2564 LASSERT(olg == &obd->obd_olg);
2566 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2571 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2572 llog_initiator_connect(ctxt);
2573 llog_ctxt_put(ctxt);
2578 static int mdc_llog_finish(struct obd_device *obd, int count)
2580 struct llog_ctxt *ctxt;
2584 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2586 llog_cleanup(NULL, ctxt);
2591 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2593 struct lustre_cfg *lcfg = buf;
2594 struct lprocfs_static_vars lvars = { 0 };
2597 lprocfs_mdc_init_vars(&lvars);
2598 switch (lcfg->lcfg_command) {
2600 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2610 /* get remote permission for current user on fid */
2611 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2612 struct obd_capa *oc, __u32 suppgid,
2613 struct ptlrpc_request **request)
2615 struct ptlrpc_request *req;
2619 LASSERT(client_is_remote(exp));
2622 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2626 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2628 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2630 ptlrpc_request_free(req);
2634 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2636 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2637 sizeof(struct mdt_remote_perm));
2639 ptlrpc_request_set_replen(req);
2641 rc = ptlrpc_queue_wait(req);
2643 ptlrpc_req_finished(req);
2649 static int mdc_interpret_renew_capa(const struct lu_env *env,
2650 struct ptlrpc_request *req, void *args,
2653 struct mdc_renew_capa_args *ra = args;
2654 struct mdt_body *body = NULL;
2655 struct lustre_capa *capa;
2659 GOTO(out, capa = ERR_PTR(status));
2661 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2663 GOTO(out, capa = ERR_PTR(-EFAULT));
2665 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2666 GOTO(out, capa = ERR_PTR(-ENOENT));
2668 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2670 GOTO(out, capa = ERR_PTR(-EFAULT));
2673 ra->ra_cb(ra->ra_oc, capa);
2677 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2680 struct ptlrpc_request *req;
2681 struct mdc_renew_capa_args *ra;
2684 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2685 LUSTRE_MDS_VERSION, MDS_GETATTR);
2689 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2690 * capa to renew is oss capa.
2692 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2693 ptlrpc_request_set_replen(req);
2695 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2696 ra = ptlrpc_req_async_args(req);
2699 req->rq_interpret_reply = mdc_interpret_renew_capa;
2700 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2704 static int mdc_connect(const struct lu_env *env,
2705 struct obd_export **exp,
2706 struct obd_device *obd, struct obd_uuid *cluuid,
2707 struct obd_connect_data *data,
2710 struct obd_import *imp = obd->u.cli.cl_import;
2712 /* mds-mds import features */
2713 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2714 spin_lock(&imp->imp_lock);
2715 imp->imp_server_timeout = 1;
2716 spin_unlock(&imp->imp_lock);
2717 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2718 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2722 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2725 struct obd_ops mdc_obd_ops = {
2726 .o_owner = THIS_MODULE,
2727 .o_setup = mdc_setup,
2728 .o_precleanup = mdc_precleanup,
2729 .o_cleanup = mdc_cleanup,
2730 .o_add_conn = client_import_add_conn,
2731 .o_del_conn = client_import_del_conn,
2732 .o_connect = mdc_connect,
2733 .o_disconnect = client_disconnect_export,
2734 .o_iocontrol = mdc_iocontrol,
2735 .o_set_info_async = mdc_set_info_async,
2736 .o_statfs = mdc_statfs,
2738 .o_unpin = mdc_unpin,
2739 .o_fid_init = client_fid_init,
2740 .o_fid_fini = client_fid_fini,
2741 .o_fid_alloc = mdc_fid_alloc,
2742 .o_import_event = mdc_import_event,
2743 .o_llog_init = mdc_llog_init,
2744 .o_llog_finish = mdc_llog_finish,
2745 .o_get_info = mdc_get_info,
2746 .o_process_config = mdc_process_config,
2747 .o_get_uuid = mdc_get_uuid,
2748 .o_quotactl = mdc_quotactl,
2749 .o_quotacheck = mdc_quotacheck
2752 struct md_ops mdc_md_ops = {
2753 .m_getstatus = mdc_getstatus,
2754 .m_null_inode = mdc_null_inode,
2755 .m_find_cbdata = mdc_find_cbdata,
2756 .m_close = mdc_close,
2757 .m_create = mdc_create,
2758 .m_done_writing = mdc_done_writing,
2759 .m_enqueue = mdc_enqueue,
2760 .m_getattr = mdc_getattr,
2761 .m_getattr_name = mdc_getattr_name,
2762 .m_intent_lock = mdc_intent_lock,
2764 .m_is_subdir = mdc_is_subdir,
2765 .m_rename = mdc_rename,
2766 .m_setattr = mdc_setattr,
2767 .m_setxattr = mdc_setxattr,
2768 .m_getxattr = mdc_getxattr,
2770 .m_readpage = mdc_readpage,
2771 .m_unlink = mdc_unlink,
2772 .m_cancel_unused = mdc_cancel_unused,
2773 .m_init_ea_size = mdc_init_ea_size,
2774 .m_set_lock_data = mdc_set_lock_data,
2775 .m_lock_match = mdc_lock_match,
2776 .m_get_lustre_md = mdc_get_lustre_md,
2777 .m_free_lustre_md = mdc_free_lustre_md,
2778 .m_set_open_replay_data = mdc_set_open_replay_data,
2779 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2780 .m_renew_capa = mdc_renew_capa,
2781 .m_unpack_capa = mdc_unpack_capa,
2782 .m_get_remote_perm = mdc_get_remote_perm,
2783 .m_intent_getattr_async = mdc_intent_getattr_async,
2784 .m_revalidate_lock = mdc_revalidate_lock
2787 int __init mdc_init(void)
2790 struct lprocfs_static_vars lvars = { 0 };
2791 lprocfs_mdc_init_vars(&lvars);
2793 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2794 LUSTRE_MDC_NAME, NULL);
2799 static void /*__exit*/ mdc_exit(void)
2801 class_unregister_type(LUSTRE_MDC_NAME);
2804 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2805 MODULE_DESCRIPTION("Lustre Metadata Client");
2806 MODULE_LICENSE("GPL");
2808 module_init(mdc_init);
2809 module_exit(mdc_exit);