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, 2013, 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>
44 # include <linux/utsname.h>
46 # include <liblustre.h>
49 #include <lustre_acl.h>
50 #include <obd_class.h>
51 #include <lustre_fid.h>
52 #include <lprocfs_status.h>
53 #include <lustre_param.h>
54 #include <lustre_log.h>
56 #include "mdc_internal.h"
58 #define REQUEST_MINOR 244
60 struct mdc_renew_capa_args {
61 struct obd_capa *ra_oc;
62 renew_capa_cb_t ra_cb;
65 static int mdc_cleanup(struct obd_device *obd);
67 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
68 const struct req_msg_field *field, struct obd_capa **oc)
70 struct lustre_capa *capa;
74 /* swabbed already in mdc_enqueue */
75 capa = req_capsule_server_get(&req->rq_pill, field);
79 c = alloc_capa(CAPA_SITE_CLIENT);
81 CDEBUG(D_INFO, "alloc capa failed!\n");
90 static inline int mdc_queue_wait(struct ptlrpc_request *req)
92 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
95 /* mdc_enter_request() ensures that this client has no more
96 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
98 rc = mdc_enter_request(cli);
102 rc = ptlrpc_queue_wait(req);
103 mdc_exit_request(cli);
108 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
109 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
110 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
111 struct obd_capa **pc, int level, int msg_flags)
113 struct ptlrpc_request *req;
114 struct mdt_body *body;
118 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
119 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
123 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
124 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
125 req->rq_send_state = level;
127 ptlrpc_request_set_replen(req);
129 rc = ptlrpc_queue_wait(req);
133 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
135 GOTO(out, rc = -EPROTO);
137 if (body->valid & OBD_MD_FLMDSCAPA) {
138 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
143 *rootfid = body->fid1;
145 "root fid="DFID", last_committed="LPU64"\n",
147 lustre_msg_get_last_committed(req->rq_repmsg));
150 ptlrpc_req_finished(req);
154 /* This should be mdc_get_info("rootfid") */
155 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
156 struct obd_capa **pc)
158 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
163 * This function now is known to always saying that it will receive 4 buffers
164 * from server. Even for cases when acl_size and md_size is zero, RPC header
165 * will contain 4 fields and RPC itself will contain zero size fields. This is
166 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
167 * and thus zero, it shrinks it, making zero size. The same story about
168 * md_size. And this is course of problem when client waits for smaller number
169 * of fields. This issue will be fixed later when client gets aware of RPC
172 static int mdc_getattr_common(struct obd_export *exp,
173 struct ptlrpc_request *req)
175 struct req_capsule *pill = &req->rq_pill;
176 struct mdt_body *body;
181 /* Request message already built. */
182 rc = ptlrpc_queue_wait(req);
186 /* sanity check for the reply */
187 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
191 CDEBUG(D_NET, "mode: %o\n", body->mode);
193 mdc_update_max_ea_from_body(exp, body);
194 if (body->eadatasize != 0) {
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 /* Flush local XATTR locks to get rid of a possible cancel RPC */
371 if (opcode == MDS_REINT && fid_is_sane(fid) &&
372 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
373 CFS_LIST_HEAD(cancels);
376 /* Without that packing would fail */
378 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
381 count = mdc_resource_get_unused(exp, fid,
383 MDS_INODELOCK_XATTR);
385 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
387 ptlrpc_request_free(req);
391 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
393 ptlrpc_request_free(req);
398 if (opcode == MDS_REINT) {
399 struct mdt_rec_setxattr *rec;
401 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
402 sizeof(struct mdt_rec_reint));
403 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
404 rec->sx_opcode = REINT_SETXATTR;
405 rec->sx_fsuid = current_fsuid();
406 rec->sx_fsgid = current_fsgid();
407 rec->sx_cap = cfs_curproc_cap_pack();
408 rec->sx_suppgid1 = suppgid;
409 rec->sx_suppgid2 = -1;
411 rec->sx_valid = valid | OBD_MD_FLCTIME;
412 rec->sx_time = cfs_time_current_sec();
413 rec->sx_size = output_size;
414 rec->sx_flags = flags;
416 mdc_pack_capa(req, &RMF_CAPA1, oc);
418 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
422 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
423 memcpy(tmp, xattr_name, xattr_namelen);
426 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
427 memcpy(tmp, input, input_size);
430 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
431 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
432 RCL_SERVER, output_size);
433 ptlrpc_request_set_replen(req);
436 if (opcode == MDS_REINT)
437 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
439 rc = ptlrpc_queue_wait(req);
441 if (opcode == MDS_REINT)
442 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
445 ptlrpc_req_finished(req);
451 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
452 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
453 const char *input, int input_size, int output_size,
454 int flags, __u32 suppgid, struct ptlrpc_request **request)
456 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
457 fid, oc, MDS_REINT, valid, xattr_name,
458 input, input_size, output_size, flags,
462 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
463 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
464 const char *input, int input_size, int output_size,
465 int flags, struct ptlrpc_request **request)
467 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
468 fid, oc, MDS_GETXATTR, valid, xattr_name,
469 input, input_size, output_size, flags,
473 #ifdef CONFIG_FS_POSIX_ACL
474 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
476 struct req_capsule *pill = &req->rq_pill;
477 struct mdt_body *body = md->body;
478 struct posix_acl *acl;
486 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
491 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
494 CERROR("convert xattr to acl: %d\n", rc);
498 rc = posix_acl_valid(acl);
500 CERROR("validate acl: %d\n", rc);
501 posix_acl_release(acl);
509 #define mdc_unpack_acl(req, md) 0
512 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
513 struct obd_export *dt_exp, struct obd_export *md_exp,
514 struct lustre_md *md)
516 struct req_capsule *pill = &req->rq_pill;
521 memset(md, 0, sizeof(*md));
523 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
524 LASSERT(md->body != NULL);
526 if (md->body->valid & OBD_MD_FLEASIZE) {
528 struct lov_mds_md *lmm;
530 if (!S_ISREG(md->body->mode)) {
531 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
532 "regular file, but is not\n");
533 GOTO(out, rc = -EPROTO);
536 if (md->body->eadatasize == 0) {
537 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
538 "but eadatasize 0\n");
539 GOTO(out, rc = -EPROTO);
541 lmmsize = md->body->eadatasize;
542 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
544 GOTO(out, rc = -EPROTO);
546 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
550 if (rc < sizeof(*md->lsm)) {
551 CDEBUG(D_INFO, "lsm size too small: "
552 "rc < sizeof (*md->lsm) (%d < %d)\n",
553 rc, (int)sizeof(*md->lsm));
554 GOTO(out, rc = -EPROTO);
557 } else if (md->body->valid & OBD_MD_FLDIREA) {
559 struct lov_mds_md *lmv;
561 if(!S_ISDIR(md->body->mode)) {
562 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
563 "directory, but is not\n");
564 GOTO(out, rc = -EPROTO);
567 if (md->body->eadatasize == 0) {
568 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
569 "but eadatasize 0\n");
572 if (md->body->valid & OBD_MD_MEA) {
573 lmvsize = md->body->eadatasize;
574 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
577 GOTO(out, rc = -EPROTO);
579 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
584 if (rc < sizeof(*md->mea)) {
585 CDEBUG(D_INFO, "size too small: "
586 "rc < sizeof(*md->mea) (%d < %d)\n",
587 rc, (int)sizeof(*md->mea));
588 GOTO(out, rc = -EPROTO);
594 if (md->body->valid & OBD_MD_FLRMTPERM) {
595 /* remote permission */
596 LASSERT(client_is_remote(exp));
597 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
598 lustre_swab_mdt_remote_perm);
599 if (!md->remote_perm)
600 GOTO(out, rc = -EPROTO);
602 else if (md->body->valid & OBD_MD_FLACL) {
603 /* for ACL, it's possible that FLACL is set but aclsize is zero.
604 * only when aclsize != 0 there's an actual segment for ACL
607 if (md->body->aclsize) {
608 rc = mdc_unpack_acl(req, md);
611 #ifdef CONFIG_FS_POSIX_ACL
613 md->posix_acl = NULL;
617 if (md->body->valid & OBD_MD_FLMDSCAPA) {
618 struct obd_capa *oc = NULL;
620 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
626 if (md->body->valid & OBD_MD_FLOSSCAPA) {
627 struct obd_capa *oc = NULL;
629 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
639 capa_put(md->oss_capa);
643 capa_put(md->mds_capa);
646 #ifdef CONFIG_FS_POSIX_ACL
647 posix_acl_release(md->posix_acl);
650 obd_free_memmd(dt_exp, &md->lsm);
655 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
662 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
665 void mdc_replay_open(struct ptlrpc_request *req)
667 struct md_open_data *mod = req->rq_cb_data;
668 struct ptlrpc_request *close_req;
669 struct obd_client_handle *och;
670 struct lustre_handle old;
671 struct mdt_body *body;
675 DEBUG_REQ(D_ERROR, req,
676 "Can't properly replay without open data.");
681 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
682 LASSERT(body != NULL);
686 struct lustre_handle *file_fh;
688 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
690 file_fh = &och->och_fh;
691 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
692 file_fh->cookie, body->handle.cookie);
694 *file_fh = body->handle;
696 close_req = mod->mod_close_req;
697 if (close_req != NULL) {
698 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
699 struct mdt_ioepoch *epoch;
701 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
702 epoch = req_capsule_client_get(&close_req->rq_pill,
707 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
708 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
709 epoch->handle = body->handle;
714 void mdc_commit_open(struct ptlrpc_request *req)
716 struct md_open_data *mod = req->rq_cb_data;
721 * No need to touch md_open_data::mod_och, it holds a reference on
722 * \var mod and will zero references to each other, \var mod will be
723 * freed after that when md_open_data::mod_och will put the reference.
727 * Do not let open request to disappear as it still may be needed
728 * for close rpc to happen (it may happen on evict only, otherwise
729 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
730 * called), just mark this rpc as committed to distinguish these 2
731 * cases, see mdc_close() for details. The open request reference will
732 * be put along with freeing \var mod.
734 ptlrpc_request_addref(req);
735 spin_lock(&req->rq_lock);
736 req->rq_committed = 1;
737 spin_unlock(&req->rq_lock);
738 req->rq_cb_data = NULL;
742 int mdc_set_open_replay_data(struct obd_export *exp,
743 struct obd_client_handle *och,
744 struct lookup_intent *it)
746 struct md_open_data *mod;
747 struct mdt_rec_create *rec;
748 struct mdt_body *body;
749 struct ptlrpc_request *open_req = it->d.lustre.it_data;
750 struct obd_import *imp = open_req->rq_import;
753 if (!open_req->rq_replay)
756 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
757 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
758 LASSERT(rec != NULL);
759 /* Incoming message in my byte order (it's been swabbed). */
760 /* Outgoing messages always in my byte order. */
761 LASSERT(body != NULL);
763 /* Only if the import is replayable, we set replay_open data */
764 if (och && imp->imp_replayable) {
765 mod = obd_mod_alloc();
767 DEBUG_REQ(D_ERROR, open_req,
768 "Can't allocate md_open_data");
773 * Take a reference on \var mod, to be freed on mdc_close().
774 * It protects \var mod from being freed on eviction (commit
775 * callback is called despite rq_replay flag).
776 * Another reference for \var och.
781 spin_lock(&open_req->rq_lock);
784 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
785 it_disposition(it, DISP_OPEN_STRIPE);
786 mod->mod_open_req = open_req;
787 open_req->rq_cb_data = mod;
788 open_req->rq_commit_cb = mdc_commit_open;
789 spin_unlock(&open_req->rq_lock);
792 rec->cr_fid2 = body->fid1;
793 rec->cr_ioepoch = body->ioepoch;
794 rec->cr_old_handle.cookie = body->handle.cookie;
795 open_req->rq_replay_cb = mdc_replay_open;
796 if (!fid_is_sane(&body->fid1)) {
797 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
802 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
806 static void mdc_free_open(struct md_open_data *mod)
810 if (mod->mod_is_create == 0 &&
811 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
814 LASSERT(mod->mod_open_req->rq_replay == 0);
816 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
818 ptlrpc_request_committed(mod->mod_open_req, committed);
819 if (mod->mod_close_req)
820 ptlrpc_request_committed(mod->mod_close_req, committed);
823 int mdc_clear_open_replay_data(struct obd_export *exp,
824 struct obd_client_handle *och)
826 struct md_open_data *mod = och->och_mod;
830 * It is possible to not have \var mod in a case of eviction between
831 * lookup and ll_file_open().
836 LASSERT(mod != LP_POISON);
837 LASSERT(mod->mod_open_req != NULL);
847 /* Prepares the request for the replay by the given reply */
848 static void mdc_close_handle_reply(struct ptlrpc_request *req,
849 struct md_op_data *op_data, int rc) {
850 struct mdt_body *repbody;
851 struct mdt_ioepoch *epoch;
853 if (req && rc == -EAGAIN) {
854 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
855 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
857 epoch->flags |= MF_SOM_AU;
858 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
859 op_data->op_flags |= MF_GETATTR_LOCK;
863 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
864 struct md_open_data *mod, struct ptlrpc_request **request)
866 struct obd_device *obd = class_exp2obd(exp);
867 struct ptlrpc_request *req;
868 struct req_format *req_fmt;
873 req_fmt = &RQF_MDS_CLOSE;
874 if (op_data->op_bias & MDS_HSM_RELEASE) {
875 req_fmt = &RQF_MDS_RELEASE_CLOSE;
877 /* allocate a FID for volatile file */
878 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
880 CERROR("%s: "DFID" failed to allocate FID: %d\n",
881 obd->obd_name, PFID(&op_data->op_fid1), rc);
882 /* save the errcode and proceed to close */
888 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
892 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
894 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
896 ptlrpc_request_free(req);
900 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
901 * portal whose threads are not taking any DLM locks and are therefore
902 * always progressing */
903 req->rq_request_portal = MDS_READPAGE_PORTAL;
904 ptlrpc_at_set_req_timeout(req);
906 /* Ensure that this close's handle is fixed up during replay. */
907 if (likely(mod != NULL)) {
908 LASSERTF(mod->mod_open_req != NULL &&
909 mod->mod_open_req->rq_type != LI_POISON,
910 "POISONED open %p!\n", mod->mod_open_req);
912 mod->mod_close_req = req;
914 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
915 /* We no longer want to preserve this open for replay even
916 * though the open was committed. b=3632, b=3633 */
917 spin_lock(&mod->mod_open_req->rq_lock);
918 mod->mod_open_req->rq_replay = 0;
919 spin_unlock(&mod->mod_open_req->rq_lock);
921 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
924 mdc_close_pack(req, op_data);
926 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
927 obd->u.cli.cl_max_mds_easize);
928 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
929 obd->u.cli.cl_max_mds_cookiesize);
931 ptlrpc_request_set_replen(req);
933 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
934 rc = ptlrpc_queue_wait(req);
935 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
937 if (req->rq_repmsg == NULL) {
938 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
941 rc = req->rq_status ?: -EIO;
942 } else if (rc == 0 || rc == -EAGAIN) {
943 struct mdt_body *body;
945 rc = lustre_msg_get_status(req->rq_repmsg);
946 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
947 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
952 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
955 } else if (rc == -ESTALE) {
957 * it can be allowed error after 3633 if open was committed and
958 * server failed before close was sent. Let's check if mod
959 * exists and return no error in that case
962 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
963 LASSERT(mod->mod_open_req != NULL);
964 if (mod->mod_open_req->rq_committed)
971 mod->mod_close_req = NULL;
972 /* Since now, mod is accessed through open_req only,
973 * thus close req does not keep a reference on mod anymore. */
977 mdc_close_handle_reply(req, op_data, rc);
978 RETURN(rc < 0 ? rc : saved_rc);
981 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
982 struct md_open_data *mod)
984 struct obd_device *obd = class_exp2obd(exp);
985 struct ptlrpc_request *req;
989 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
990 &RQF_MDS_DONE_WRITING);
994 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
995 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
997 ptlrpc_request_free(req);
1002 LASSERTF(mod->mod_open_req != NULL &&
1003 mod->mod_open_req->rq_type != LI_POISON,
1004 "POISONED setattr %p!\n", mod->mod_open_req);
1006 mod->mod_close_req = req;
1007 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
1008 /* We no longer want to preserve this setattr for replay even
1009 * though the open was committed. b=3632, b=3633 */
1010 spin_lock(&mod->mod_open_req->rq_lock);
1011 mod->mod_open_req->rq_replay = 0;
1012 spin_unlock(&mod->mod_open_req->rq_lock);
1015 mdc_close_pack(req, op_data);
1016 ptlrpc_request_set_replen(req);
1018 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1019 rc = ptlrpc_queue_wait(req);
1020 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1022 if (rc == -ESTALE) {
1024 * it can be allowed error after 3633 if open or setattr were
1025 * committed and server failed before close was sent.
1026 * Let's check if mod exists and return no error in that case
1029 LASSERT(mod->mod_open_req != NULL);
1030 if (mod->mod_open_req->rq_committed)
1037 mod->mod_close_req = NULL;
1038 LASSERT(mod->mod_open_req != NULL);
1041 /* Since now, mod is accessed through setattr req only,
1042 * thus DW req does not keep a reference on mod anymore. */
1046 mdc_close_handle_reply(req, op_data, rc);
1047 ptlrpc_req_finished(req);
1051 #ifdef HAVE_SPLIT_SUPPORT
1052 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
1053 const struct page *page, int offset)
1055 struct ptlrpc_request *req;
1056 struct ptlrpc_bulk_desc *desc;
1060 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1064 /* FIXME: capa doesn't support split yet */
1065 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1067 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1069 ptlrpc_request_free(req);
1073 req->rq_request_portal = MDS_READPAGE_PORTAL;
1074 ptlrpc_at_set_req_timeout(req);
1076 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1078 GOTO(out, rc = -ENOMEM);
1080 /* NB req now owns desc and will free it when it gets freed. */
1081 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1082 mdc_readdir_pack(req, 0, offset, fid, NULL);
1084 ptlrpc_request_set_replen(req);
1085 rc = ptlrpc_queue_wait(req);
1089 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1091 ptlrpc_req_finished(req);
1094 EXPORT_SYMBOL(mdc_sendpage);
1097 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1098 struct page **pages, struct ptlrpc_request **request)
1100 struct ptlrpc_request *req;
1101 struct ptlrpc_bulk_desc *desc;
1103 wait_queue_head_t waitq;
1105 struct l_wait_info lwi;
1110 init_waitqueue_head(&waitq);
1113 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1117 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1119 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1121 ptlrpc_request_free(req);
1125 req->rq_request_portal = MDS_READPAGE_PORTAL;
1126 ptlrpc_at_set_req_timeout(req);
1128 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1131 ptlrpc_request_free(req);
1135 /* NB req now owns desc and will free it when it gets freed */
1136 for (i = 0; i < op_data->op_npages; i++)
1137 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1139 mdc_readdir_pack(req, op_data->op_offset,
1140 PAGE_CACHE_SIZE * op_data->op_npages,
1141 &op_data->op_fid1, op_data->op_capa1);
1143 ptlrpc_request_set_replen(req);
1144 rc = ptlrpc_queue_wait(req);
1146 ptlrpc_req_finished(req);
1147 if (rc != -ETIMEDOUT)
1151 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1152 CERROR("too many resend retries, returning error\n");
1155 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1156 l_wait_event(waitq, 0, &lwi);
1161 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1162 req->rq_bulk->bd_nob_transferred);
1164 ptlrpc_req_finished(req);
1168 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1169 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1170 req->rq_bulk->bd_nob_transferred,
1171 PAGE_CACHE_SIZE * op_data->op_npages);
1172 ptlrpc_req_finished(req);
1180 static int mdc_statfs(const struct lu_env *env,
1181 struct obd_export *exp, struct obd_statfs *osfs,
1182 __u64 max_age, __u32 flags)
1184 struct obd_device *obd = class_exp2obd(exp);
1185 struct ptlrpc_request *req;
1186 struct obd_statfs *msfs;
1187 struct obd_import *imp = NULL;
1192 * Since the request might also come from lprocfs, so we need
1193 * sync this with client_disconnect_export Bug15684
1195 down_read(&obd->u.cli.cl_sem);
1196 if (obd->u.cli.cl_import)
1197 imp = class_import_get(obd->u.cli.cl_import);
1198 up_read(&obd->u.cli.cl_sem);
1202 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1203 LUSTRE_MDS_VERSION, MDS_STATFS);
1205 GOTO(output, rc = -ENOMEM);
1207 ptlrpc_request_set_replen(req);
1209 if (flags & OBD_STATFS_NODELAY) {
1210 /* procfs requests not want stay in wait for avoid deadlock */
1211 req->rq_no_resend = 1;
1212 req->rq_no_delay = 1;
1215 rc = ptlrpc_queue_wait(req);
1217 /* check connection error first */
1218 if (imp->imp_connect_error)
1219 rc = imp->imp_connect_error;
1223 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1225 GOTO(out, rc = -EPROTO);
1230 ptlrpc_req_finished(req);
1232 class_import_put(imp);
1236 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1238 __u32 keylen, vallen;
1242 if (gf->gf_pathlen > PATH_MAX)
1243 RETURN(-ENAMETOOLONG);
1244 if (gf->gf_pathlen < 2)
1247 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1248 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1249 OBD_ALLOC(key, keylen);
1252 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1253 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1255 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1256 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1258 if (!fid_is_sane(&gf->gf_fid))
1259 GOTO(out, rc = -EINVAL);
1261 /* Val is struct getinfo_fid2path result plus path */
1262 vallen = sizeof(*gf) + gf->gf_pathlen;
1264 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1265 if (rc != 0 && rc != -EREMOTE)
1268 if (vallen <= sizeof(*gf))
1269 GOTO(out, rc = -EPROTO);
1270 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1271 GOTO(out, rc = -EOVERFLOW);
1273 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1274 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1277 OBD_FREE(key, keylen);
1281 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1282 struct hsm_progress_kernel *hpk)
1284 struct obd_import *imp = class_exp2cliimp(exp);
1285 struct hsm_progress_kernel *req_hpk;
1286 struct ptlrpc_request *req;
1290 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1291 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1293 GOTO(out, rc = -ENOMEM);
1295 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1297 /* Copy hsm_progress struct */
1298 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1299 if (req_hpk == NULL)
1300 GOTO(out, rc = -EPROTO);
1303 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1305 ptlrpc_request_set_replen(req);
1307 rc = mdc_queue_wait(req);
1310 ptlrpc_req_finished(req);
1314 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1316 __u32 *archive_mask;
1317 struct ptlrpc_request *req;
1321 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1323 MDS_HSM_CT_REGISTER);
1325 GOTO(out, rc = -ENOMEM);
1327 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1329 /* Copy hsm_progress struct */
1330 archive_mask = req_capsule_client_get(&req->rq_pill,
1331 &RMF_MDS_HSM_ARCHIVE);
1332 if (archive_mask == NULL)
1333 GOTO(out, rc = -EPROTO);
1335 *archive_mask = archives;
1337 ptlrpc_request_set_replen(req);
1339 rc = mdc_queue_wait(req);
1342 ptlrpc_req_finished(req);
1346 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1347 struct md_op_data *op_data)
1349 struct hsm_current_action *hca = op_data->op_data;
1350 struct hsm_current_action *req_hca;
1351 struct ptlrpc_request *req;
1355 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1356 &RQF_MDS_HSM_ACTION);
1360 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1362 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1364 ptlrpc_request_free(req);
1368 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1369 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1371 ptlrpc_request_set_replen(req);
1373 rc = mdc_queue_wait(req);
1377 req_hca = req_capsule_server_get(&req->rq_pill,
1378 &RMF_MDS_HSM_CURRENT_ACTION);
1379 if (req_hca == NULL)
1380 GOTO(out, rc = -EPROTO);
1386 ptlrpc_req_finished(req);
1390 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1392 struct ptlrpc_request *req;
1396 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1398 MDS_HSM_CT_UNREGISTER);
1400 GOTO(out, rc = -ENOMEM);
1402 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1404 ptlrpc_request_set_replen(req);
1406 rc = mdc_queue_wait(req);
1409 ptlrpc_req_finished(req);
1413 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1414 struct md_op_data *op_data)
1416 struct hsm_user_state *hus = op_data->op_data;
1417 struct hsm_user_state *req_hus;
1418 struct ptlrpc_request *req;
1422 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1423 &RQF_MDS_HSM_STATE_GET);
1427 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1429 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1431 ptlrpc_request_free(req);
1435 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1436 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1438 ptlrpc_request_set_replen(req);
1440 rc = mdc_queue_wait(req);
1444 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1445 if (req_hus == NULL)
1446 GOTO(out, rc = -EPROTO);
1452 ptlrpc_req_finished(req);
1456 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1457 struct md_op_data *op_data)
1459 struct hsm_state_set *hss = op_data->op_data;
1460 struct hsm_state_set *req_hss;
1461 struct ptlrpc_request *req;
1465 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1466 &RQF_MDS_HSM_STATE_SET);
1470 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1472 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1474 ptlrpc_request_free(req);
1478 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1479 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1482 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1483 if (req_hss == NULL)
1484 GOTO(out, rc = -EPROTO);
1487 ptlrpc_request_set_replen(req);
1489 rc = mdc_queue_wait(req);
1494 ptlrpc_req_finished(req);
1498 static int mdc_ioc_hsm_request(struct obd_export *exp,
1499 struct hsm_user_request *hur)
1501 struct obd_import *imp = class_exp2cliimp(exp);
1502 struct ptlrpc_request *req;
1503 struct hsm_request *req_hr;
1504 struct hsm_user_item *req_hui;
1509 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1511 GOTO(out, rc = -ENOMEM);
1513 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1514 hur->hur_request.hr_itemcount
1515 * sizeof(struct hsm_user_item));
1516 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1517 hur->hur_request.hr_data_len);
1519 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1521 ptlrpc_request_free(req);
1525 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1527 /* Copy hsm_request struct */
1528 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1530 GOTO(out, rc = -EPROTO);
1531 *req_hr = hur->hur_request;
1533 /* Copy hsm_user_item structs */
1534 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1535 if (req_hui == NULL)
1536 GOTO(out, rc = -EPROTO);
1537 memcpy(req_hui, hur->hur_user_item,
1538 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1540 /* Copy opaque field */
1541 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1542 if (req_opaque == NULL)
1543 GOTO(out, rc = -EPROTO);
1544 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1546 ptlrpc_request_set_replen(req);
1548 rc = mdc_queue_wait(req);
1552 ptlrpc_req_finished(req);
1556 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1558 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1560 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1562 lh->kuc_magic = KUC_MAGIC;
1563 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1564 lh->kuc_flags = flags;
1565 lh->kuc_msgtype = CL_RECORD;
1566 lh->kuc_msglen = len;
1570 #define D_CHANGELOG 0
1572 struct changelog_show {
1577 struct obd_device *cs_obd;
1580 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1581 struct llog_rec_hdr *hdr, void *data)
1583 struct changelog_show *cs = data;
1584 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1589 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1591 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1592 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1593 rec->cr.cr_type, rc);
1597 if (rec->cr.cr_index < cs->cs_startrec) {
1598 /* Skip entries earlier than what we are interested in */
1599 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1600 rec->cr.cr_index, cs->cs_startrec);
1604 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1605 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1606 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1607 rec->cr.cr_flags & CLF_FLAGMASK,
1608 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1609 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1611 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1613 /* Set up the message */
1614 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1615 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1617 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1618 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1623 static int mdc_changelog_send_thread(void *csdata)
1625 struct changelog_show *cs = csdata;
1626 struct llog_ctxt *ctxt = NULL;
1627 struct llog_handle *llh = NULL;
1628 struct kuc_hdr *kuch;
1631 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1632 cs->cs_fp, cs->cs_startrec);
1634 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1635 if (cs->cs_buf == NULL)
1636 GOTO(out, rc = -ENOMEM);
1638 /* Set up the remote catalog handle */
1639 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1641 GOTO(out, rc = -ENOENT);
1642 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1645 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1646 cs->cs_obd->obd_name, rc);
1649 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1651 CERROR("llog_init_handle failed %d\n", rc);
1655 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1657 /* Send EOF no matter what our result */
1658 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1660 kuch->kuc_msgtype = CL_EOF;
1661 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1667 llog_cat_close(NULL, llh);
1669 llog_ctxt_put(ctxt);
1671 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1676 static int mdc_ioc_changelog_send(struct obd_device *obd,
1677 struct ioc_changelog *icc)
1679 struct changelog_show *cs;
1682 /* Freed in mdc_changelog_send_thread */
1688 cs->cs_startrec = icc->icc_recno;
1689 /* matching fput in mdc_changelog_send_thread */
1690 cs->cs_fp = fget(icc->icc_id);
1691 cs->cs_flags = icc->icc_flags;
1694 * New thread because we should return to user app before
1695 * writing into our pipe
1697 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1698 "mdc_clg_send_thread"));
1699 if (!IS_ERR_VALUE(rc)) {
1700 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1704 CERROR("Failed to start changelog thread: %d\n", rc);
1709 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1710 struct lustre_kernelcomm *lk);
1712 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1713 struct obd_quotactl *oqctl)
1715 struct client_obd *cli = &exp->exp_obd->u.cli;
1716 struct ptlrpc_request *req;
1717 struct obd_quotactl *body;
1721 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1722 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1727 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1730 ptlrpc_request_set_replen(req);
1732 /* the next poll will find -ENODATA, that means quotacheck is
1734 cli->cl_qchk_stat = -ENODATA;
1735 rc = ptlrpc_queue_wait(req);
1737 cli->cl_qchk_stat = rc;
1738 ptlrpc_req_finished(req);
1742 static int mdc_quota_poll_check(struct obd_export *exp,
1743 struct if_quotacheck *qchk)
1745 struct client_obd *cli = &exp->exp_obd->u.cli;
1749 qchk->obd_uuid = cli->cl_target_uuid;
1750 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1752 rc = cli->cl_qchk_stat;
1753 /* the client is not the previous one */
1754 if (rc == CL_NOT_QUOTACHECKED)
1759 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1760 struct obd_quotactl *oqctl)
1762 struct ptlrpc_request *req;
1763 struct obd_quotactl *oqc;
1767 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1768 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1773 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1776 ptlrpc_request_set_replen(req);
1777 ptlrpc_at_set_req_timeout(req);
1778 req->rq_no_resend = 1;
1780 rc = ptlrpc_queue_wait(req);
1782 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1784 if (req->rq_repmsg &&
1785 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1788 CERROR ("Can't unpack obd_quotactl\n");
1791 ptlrpc_req_finished(req);
1796 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1797 struct md_op_data *op_data)
1799 CFS_LIST_HEAD(cancels);
1800 struct ptlrpc_request *req;
1802 struct mdc_swap_layouts *msl, *payload;
1805 msl = op_data->op_data;
1807 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1808 * first thing it will do is to cancel the 2 layout
1809 * locks hold by this client.
1810 * So the client must cancel its layout locks on the 2 fids
1811 * with the request RPC to avoid extra RPC round trips
1813 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1814 LCK_CR, MDS_INODELOCK_LAYOUT);
1815 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1816 LCK_CR, MDS_INODELOCK_LAYOUT);
1818 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1819 &RQF_MDS_SWAP_LAYOUTS);
1821 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1825 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1826 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1828 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1830 ptlrpc_request_free(req);
1834 mdc_swap_layouts_pack(req, op_data);
1836 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1841 ptlrpc_request_set_replen(req);
1843 rc = ptlrpc_queue_wait(req);
1849 ptlrpc_req_finished(req);
1853 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1854 void *karg, void *uarg)
1856 struct obd_device *obd = exp->exp_obd;
1857 struct obd_ioctl_data *data = karg;
1858 struct obd_import *imp = obd->u.cli.cl_import;
1862 if (!try_module_get(THIS_MODULE)) {
1863 CERROR("Can't get module. Is it alive?");
1867 case OBD_IOC_CHANGELOG_SEND:
1868 rc = mdc_ioc_changelog_send(obd, karg);
1870 case OBD_IOC_CHANGELOG_CLEAR: {
1871 struct ioc_changelog *icc = karg;
1872 struct changelog_setinfo cs =
1873 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1874 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1875 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1879 case OBD_IOC_FID2PATH:
1880 rc = mdc_ioc_fid2path(exp, karg);
1882 case LL_IOC_HSM_CT_START:
1883 rc = mdc_ioc_hsm_ct_start(exp, karg);
1884 /* ignore if it was already registered on this MDS. */
1888 case LL_IOC_HSM_PROGRESS:
1889 rc = mdc_ioc_hsm_progress(exp, karg);
1891 case LL_IOC_HSM_STATE_GET:
1892 rc = mdc_ioc_hsm_state_get(exp, karg);
1894 case LL_IOC_HSM_STATE_SET:
1895 rc = mdc_ioc_hsm_state_set(exp, karg);
1897 case LL_IOC_HSM_ACTION:
1898 rc = mdc_ioc_hsm_current_action(exp, karg);
1900 case LL_IOC_HSM_REQUEST:
1901 rc = mdc_ioc_hsm_request(exp, karg);
1903 case OBD_IOC_CLIENT_RECOVER:
1904 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1908 case IOC_OSC_SET_ACTIVE:
1909 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1911 case OBD_IOC_POLL_QUOTACHECK:
1912 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1914 case OBD_IOC_PING_TARGET:
1915 rc = ptlrpc_obd_ping(obd);
1918 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1919 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1920 * there'd be no LMV layer thus we might be called here. Eventually
1921 * this code should be removed.
1924 case IOC_OBD_STATFS: {
1925 struct obd_statfs stat_buf = {0};
1927 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1928 GOTO(out, rc = -ENODEV);
1931 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1932 min((int)data->ioc_plen2,
1933 (int)sizeof(struct obd_uuid))))
1934 GOTO(out, rc = -EFAULT);
1936 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1937 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1942 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1943 min((int) data->ioc_plen1,
1944 (int) sizeof(stat_buf))))
1945 GOTO(out, rc = -EFAULT);
1949 case OBD_IOC_QUOTACTL: {
1950 struct if_quotactl *qctl = karg;
1951 struct obd_quotactl *oqctl;
1953 OBD_ALLOC_PTR(oqctl);
1955 GOTO(out, rc = -ENOMEM);
1957 QCTL_COPY(oqctl, qctl);
1958 rc = obd_quotactl(exp, oqctl);
1960 QCTL_COPY(qctl, oqctl);
1961 qctl->qc_valid = QC_MDTIDX;
1962 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1965 OBD_FREE_PTR(oqctl);
1968 case LL_IOC_GET_CONNECT_FLAGS:
1969 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1970 sizeof(*exp_connect_flags_ptr(exp))))
1971 GOTO(out, rc = -EFAULT);
1974 case LL_IOC_LOV_SWAP_LAYOUTS:
1975 rc = mdc_ioc_swap_layouts(exp, karg);
1978 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1979 GOTO(out, rc = -ENOTTY);
1982 module_put(THIS_MODULE);
1987 int mdc_get_info_rpc(struct obd_export *exp,
1988 obd_count keylen, void *key,
1989 int vallen, void *val)
1991 struct obd_import *imp = class_exp2cliimp(exp);
1992 struct ptlrpc_request *req;
1997 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2001 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2002 RCL_CLIENT, keylen);
2003 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2004 RCL_CLIENT, sizeof(__u32));
2006 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2008 ptlrpc_request_free(req);
2012 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2013 memcpy(tmp, key, keylen);
2014 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2015 memcpy(tmp, &vallen, sizeof(__u32));
2017 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2018 RCL_SERVER, vallen);
2019 ptlrpc_request_set_replen(req);
2021 rc = ptlrpc_queue_wait(req);
2022 /* -EREMOTE means the get_info result is partial, and it needs to
2023 * continue on another MDT, see fid2path part in lmv_iocontrol */
2024 if (rc == 0 || rc == -EREMOTE) {
2025 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2026 memcpy(val, tmp, vallen);
2027 if (ptlrpc_rep_need_swab(req)) {
2028 if (KEY_IS(KEY_FID2PATH))
2029 lustre_swab_fid2path(val);
2032 ptlrpc_req_finished(req);
2037 static void lustre_swab_hai(struct hsm_action_item *h)
2039 __swab32s(&h->hai_len);
2040 __swab32s(&h->hai_action);
2041 lustre_swab_lu_fid(&h->hai_fid);
2042 lustre_swab_lu_fid(&h->hai_dfid);
2043 __swab64s(&h->hai_cookie);
2044 __swab64s(&h->hai_extent.offset);
2045 __swab64s(&h->hai_extent.length);
2046 __swab64s(&h->hai_gid);
2049 static void lustre_swab_hal(struct hsm_action_list *h)
2051 struct hsm_action_item *hai;
2054 __swab32s(&h->hal_version);
2055 __swab32s(&h->hal_count);
2056 __swab32s(&h->hal_archive_id);
2057 __swab64s(&h->hal_flags);
2059 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2060 lustre_swab_hai(hai);
2063 static void lustre_swab_kuch(struct kuc_hdr *l)
2065 __swab16s(&l->kuc_magic);
2066 /* __u8 l->kuc_transport */
2067 __swab16s(&l->kuc_msgtype);
2068 __swab16s(&l->kuc_msglen);
2071 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2072 struct lustre_kernelcomm *lk)
2074 struct obd_import *imp = class_exp2cliimp(exp);
2075 __u32 archive = lk->lk_data;
2078 if (lk->lk_group != KUC_GRP_HSM) {
2079 CERROR("Bad copytool group %d\n", lk->lk_group);
2083 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2084 lk->lk_uid, lk->lk_group, lk->lk_flags);
2086 if (lk->lk_flags & LK_FLG_STOP) {
2087 /* Unregister with the coordinator */
2088 rc = mdc_ioc_hsm_ct_unregister(imp);
2090 rc = mdc_ioc_hsm_ct_register(imp, archive);
2097 * Send a message to any listening copytools
2098 * @param val KUC message (kuc_hdr + hsm_action_list)
2099 * @param len total length of message
2101 static int mdc_hsm_copytool_send(int len, void *val)
2103 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2104 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2108 if (len < sizeof(*lh) + sizeof(*hal)) {
2109 CERROR("Short HSM message %d < %d\n", len,
2110 (int) (sizeof(*lh) + sizeof(*hal)));
2113 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2114 lustre_swab_kuch(lh);
2115 lustre_swab_hal(hal);
2116 } else if (lh->kuc_magic != KUC_MAGIC) {
2117 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2121 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2123 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2124 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2126 /* Broadcast to HSM listeners */
2127 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2133 * callback function passed to kuc for re-registering each HSM copytool
2134 * running on MDC, after MDT shutdown/recovery.
2135 * @param data copytool registration data
2136 * @param cb_arg callback argument (obd_import)
2138 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2140 struct kkuc_ct_data *kcd = data;
2141 struct obd_import *imp = (struct obd_import *)cb_arg;
2144 if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2147 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2150 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2151 imp->imp_obd->obd_name, kcd->kcd_archive);
2152 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2154 /* ignore error if the copytool is already registered */
2155 return (rc == -EEXIST) ? 0 : rc;
2159 * Re-establish all kuc contexts with MDT
2160 * after MDT shutdown/recovery.
2162 static int mdc_kuc_reregister(struct obd_import *imp)
2164 /* re-register HSM agents */
2165 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2169 int mdc_set_info_async(const struct lu_env *env,
2170 struct obd_export *exp,
2171 obd_count keylen, void *key,
2172 obd_count vallen, void *val,
2173 struct ptlrpc_request_set *set)
2175 struct obd_import *imp = class_exp2cliimp(exp);
2179 if (KEY_IS(KEY_READ_ONLY)) {
2180 if (vallen != sizeof(int))
2183 spin_lock(&imp->imp_lock);
2184 if (*((int *)val)) {
2185 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2186 imp->imp_connect_data.ocd_connect_flags |=
2189 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2190 imp->imp_connect_data.ocd_connect_flags &=
2191 ~OBD_CONNECT_RDONLY;
2193 spin_unlock(&imp->imp_lock);
2195 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2196 keylen, key, vallen, val, set);
2199 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2200 sptlrpc_conf_client_adapt(exp->exp_obd);
2203 if (KEY_IS(KEY_FLUSH_CTX)) {
2204 sptlrpc_import_flush_my_ctx(imp);
2207 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2208 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2209 keylen, key, vallen, val, set);
2212 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2213 rc = mdc_hsm_copytool_send(vallen, val);
2217 CERROR("Unknown key %s\n", (char *)key);
2221 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2222 __u32 keylen, void *key, __u32 *vallen, void *val,
2223 struct lov_stripe_md *lsm)
2227 if (KEY_IS(KEY_MAX_EASIZE)) {
2228 int mdsize, *max_easize;
2230 if (*vallen != sizeof(int))
2232 mdsize = *(int*)val;
2233 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2234 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2236 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2238 } else if (KEY_IS(KEY_CONN_DATA)) {
2239 struct obd_import *imp = class_exp2cliimp(exp);
2240 struct obd_connect_data *data = val;
2242 if (*vallen != sizeof(*data))
2245 *data = imp->imp_connect_data;
2247 } else if (KEY_IS(KEY_TGT_COUNT)) {
2252 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2257 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2258 struct obd_capa *oc, struct obd_client_handle *handle,
2261 struct ptlrpc_request *req;
2262 struct mdt_body *body;
2266 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2270 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2272 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2274 ptlrpc_request_free(req);
2278 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2280 ptlrpc_request_set_replen(req);
2282 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2283 rc = ptlrpc_queue_wait(req);
2284 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2286 CERROR("Pin failed: %d\n", rc);
2290 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2292 GOTO(err_out, rc = -EPROTO);
2294 handle->och_fh = body->handle;
2295 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2297 handle->och_mod = obd_mod_alloc();
2298 if (handle->och_mod == NULL) {
2299 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2300 GOTO(err_out, rc = -ENOMEM);
2302 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2307 ptlrpc_req_finished(req);
2311 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2314 struct ptlrpc_request *req;
2315 struct mdt_body *body;
2319 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2320 LUSTRE_MDS_VERSION, MDS_UNPIN);
2324 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2325 body->handle = handle->och_fh;
2328 ptlrpc_request_set_replen(req);
2330 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2331 rc = ptlrpc_queue_wait(req);
2332 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2335 CERROR("Unpin failed: %d\n", rc);
2337 ptlrpc_req_finished(req);
2338 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2340 obd_mod_put(handle->och_mod);
2344 int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2345 struct obd_capa *oc, struct ptlrpc_request **request)
2347 struct ptlrpc_request *req;
2352 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2356 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2358 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2360 ptlrpc_request_free(req);
2364 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2366 ptlrpc_request_set_replen(req);
2368 rc = ptlrpc_queue_wait(req);
2370 ptlrpc_req_finished(req);
2376 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2377 enum obd_import_event event)
2381 LASSERT(imp->imp_obd == obd);
2384 case IMP_EVENT_DISCON: {
2386 /* XXX Pass event up to OBDs stack. used only for FLD now */
2387 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2391 case IMP_EVENT_INACTIVE: {
2392 struct client_obd *cli = &obd->u.cli;
2394 * Flush current sequence to make client obtain new one
2395 * from server in case of disconnect/reconnect.
2397 if (cli->cl_seq != NULL)
2398 seq_client_flush(cli->cl_seq);
2400 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2403 case IMP_EVENT_INVALIDATE: {
2404 struct ldlm_namespace *ns = obd->obd_namespace;
2406 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2410 case IMP_EVENT_ACTIVE:
2411 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2412 /* redo the kuc registration after reconnecting */
2414 rc = mdc_kuc_reregister(imp);
2417 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2419 case IMP_EVENT_DEACTIVATE:
2420 case IMP_EVENT_ACTIVATE:
2423 CERROR("Unknown import event %x\n", event);
2429 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2430 struct md_op_data *op_data)
2432 struct client_obd *cli = &exp->exp_obd->u.cli;
2433 struct lu_client_seq *seq = cli->cl_seq;
2435 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2438 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2439 struct client_obd *cli = &exp->exp_obd->u.cli;
2440 return &cli->cl_target_uuid;
2444 * Determine whether the lock can be canceled before replaying it during
2445 * recovery, non zero value will be return if the lock can be canceled,
2446 * or zero returned for not
2448 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2450 if (lock->l_resource->lr_type != LDLM_IBITS)
2453 /* FIXME: if we ever get into a situation where there are too many
2454 * opened files with open locks on a single node, then we really
2455 * should replay these open locks to reget it */
2456 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2462 static int mdc_resource_inode_free(struct ldlm_resource *res)
2464 if (res->lr_lvb_inode)
2465 res->lr_lvb_inode = NULL;
2470 struct ldlm_valblock_ops inode_lvbo = {
2471 lvbo_free: mdc_resource_inode_free
2474 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2476 struct client_obd *cli = &obd->u.cli;
2480 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2481 if (!cli->cl_rpc_lock)
2483 mdc_init_rpc_lock(cli->cl_rpc_lock);
2485 rc = ptlrpcd_addref();
2487 GOTO(err_rpc_lock, rc);
2489 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2490 if (!cli->cl_close_lock)
2491 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2492 mdc_init_rpc_lock(cli->cl_close_lock);
2494 rc = client_obd_setup(obd, cfg);
2496 GOTO(err_close_lock, rc);
2498 obd->obd_vars = lprocfs_mdc_obd_vars;
2499 lprocfs_seq_obd_setup(obd);
2500 lprocfs_alloc_md_stats(obd, 0);
2502 sptlrpc_lprocfs_cliobd_attach(obd);
2503 ptlrpc_lprocfs_register_obd(obd);
2505 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2507 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2509 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2512 CERROR("failed to setup llogging subsystems\n");
2518 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2522 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2526 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2527 * us to make MDS RPCs with large enough reply buffers to hold the
2528 * maximum-sized (= maximum striped) EA and cookie without having to
2529 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2530 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2531 int def_easize, int cookiesize)
2533 struct obd_device *obd = exp->exp_obd;
2534 struct client_obd *cli = &obd->u.cli;
2537 if (cli->cl_max_mds_easize < easize)
2538 cli->cl_max_mds_easize = easize;
2540 if (cli->cl_default_mds_easize < def_easize)
2541 cli->cl_default_mds_easize = def_easize;
2543 if (cli->cl_max_mds_cookiesize < cookiesize)
2544 cli->cl_max_mds_cookiesize = cookiesize;
2549 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2555 case OBD_CLEANUP_EARLY:
2557 case OBD_CLEANUP_EXPORTS:
2558 /* Failsafe, ok if racy */
2559 if (obd->obd_type->typ_refcnt <= 1)
2560 libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
2562 obd_cleanup_client_import(obd);
2563 ptlrpc_lprocfs_unregister_obd(obd);
2564 lprocfs_obd_cleanup(obd);
2565 lprocfs_free_md_stats(obd);
2567 rc = obd_llog_finish(obd, 0);
2569 CERROR("failed to cleanup llogging subsystems\n");
2575 static int mdc_cleanup(struct obd_device *obd)
2577 struct client_obd *cli = &obd->u.cli;
2579 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2580 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2584 return client_obd_cleanup(obd);
2588 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2589 struct obd_device *tgt, int *index)
2591 struct llog_ctxt *ctxt;
2596 LASSERT(olg == &obd->obd_olg);
2598 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2603 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2604 llog_initiator_connect(ctxt);
2605 llog_ctxt_put(ctxt);
2610 static int mdc_llog_finish(struct obd_device *obd, int count)
2612 struct llog_ctxt *ctxt;
2616 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2618 llog_cleanup(NULL, ctxt);
2623 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2625 struct lustre_cfg *lcfg = buf;
2626 int rc = class_process_proc_seq_param(PARAM_MDC, obd->obd_vars,
2628 return (rc > 0 ? 0: rc);
2632 /* get remote permission for current user on fid */
2633 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2634 struct obd_capa *oc, __u32 suppgid,
2635 struct ptlrpc_request **request)
2637 struct ptlrpc_request *req;
2641 LASSERT(client_is_remote(exp));
2644 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2648 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2650 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2652 ptlrpc_request_free(req);
2656 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2658 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2659 sizeof(struct mdt_remote_perm));
2661 ptlrpc_request_set_replen(req);
2663 rc = ptlrpc_queue_wait(req);
2665 ptlrpc_req_finished(req);
2671 static int mdc_interpret_renew_capa(const struct lu_env *env,
2672 struct ptlrpc_request *req, void *args,
2675 struct mdc_renew_capa_args *ra = args;
2676 struct mdt_body *body = NULL;
2677 struct lustre_capa *capa;
2681 GOTO(out, capa = ERR_PTR(status));
2683 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2685 GOTO(out, capa = ERR_PTR(-EFAULT));
2687 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2688 GOTO(out, capa = ERR_PTR(-ENOENT));
2690 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2692 GOTO(out, capa = ERR_PTR(-EFAULT));
2695 ra->ra_cb(ra->ra_oc, capa);
2699 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2702 struct ptlrpc_request *req;
2703 struct mdc_renew_capa_args *ra;
2706 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2707 LUSTRE_MDS_VERSION, MDS_GETATTR);
2711 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2712 * capa to renew is oss capa.
2714 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2715 ptlrpc_request_set_replen(req);
2717 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2718 ra = ptlrpc_req_async_args(req);
2721 req->rq_interpret_reply = mdc_interpret_renew_capa;
2722 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2726 struct obd_ops mdc_obd_ops = {
2727 .o_owner = THIS_MODULE,
2728 .o_setup = mdc_setup,
2729 .o_precleanup = mdc_precleanup,
2730 .o_cleanup = mdc_cleanup,
2731 .o_add_conn = client_import_add_conn,
2732 .o_del_conn = client_import_del_conn,
2733 .o_connect = client_connect_import,
2734 .o_disconnect = client_disconnect_export,
2735 .o_iocontrol = mdc_iocontrol,
2736 .o_set_info_async = mdc_set_info_async,
2737 .o_statfs = mdc_statfs,
2739 .o_unpin = mdc_unpin,
2740 .o_fid_init = client_fid_init,
2741 .o_fid_fini = client_fid_fini,
2742 .o_fid_alloc = mdc_fid_alloc,
2743 .o_import_event = mdc_import_event,
2744 .o_llog_init = mdc_llog_init,
2745 .o_llog_finish = mdc_llog_finish,
2746 .o_get_info = mdc_get_info,
2747 .o_process_config = mdc_process_config,
2748 .o_get_uuid = mdc_get_uuid,
2749 .o_quotactl = mdc_quotactl,
2750 .o_quotacheck = mdc_quotacheck
2753 struct md_ops mdc_md_ops = {
2754 .m_getstatus = mdc_getstatus,
2755 .m_null_inode = mdc_null_inode,
2756 .m_find_cbdata = mdc_find_cbdata,
2757 .m_close = mdc_close,
2758 .m_create = mdc_create,
2759 .m_done_writing = mdc_done_writing,
2760 .m_enqueue = mdc_enqueue,
2761 .m_getattr = mdc_getattr,
2762 .m_getattr_name = mdc_getattr_name,
2763 .m_intent_lock = mdc_intent_lock,
2765 .m_is_subdir = mdc_is_subdir,
2766 .m_rename = mdc_rename,
2767 .m_setattr = mdc_setattr,
2768 .m_setxattr = mdc_setxattr,
2769 .m_getxattr = mdc_getxattr,
2770 .m_fsync = mdc_fsync,
2771 .m_readpage = mdc_readpage,
2772 .m_unlink = mdc_unlink,
2773 .m_cancel_unused = mdc_cancel_unused,
2774 .m_init_ea_size = mdc_init_ea_size,
2775 .m_set_lock_data = mdc_set_lock_data,
2776 .m_lock_match = mdc_lock_match,
2777 .m_get_lustre_md = mdc_get_lustre_md,
2778 .m_free_lustre_md = mdc_free_lustre_md,
2779 .m_set_open_replay_data = mdc_set_open_replay_data,
2780 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2781 .m_renew_capa = mdc_renew_capa,
2782 .m_unpack_capa = mdc_unpack_capa,
2783 .m_get_remote_perm = mdc_get_remote_perm,
2784 .m_intent_getattr_async = mdc_intent_getattr_async,
2785 .m_revalidate_lock = mdc_revalidate_lock
2788 int __init mdc_init(void)
2790 return class_register_type(&mdc_obd_ops, &mdc_md_ops, NULL,
2791 #ifndef HAVE_ONLY_PROCFS_SEQ
2794 LUSTRE_MDC_NAME, NULL);
2798 static void /*__exit*/ mdc_exit(void)
2800 class_unregister_type(LUSTRE_MDC_NAME);
2803 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2804 MODULE_DESCRIPTION("Lustre Metadata Client");
2805 MODULE_LICENSE("GPL");
2807 module_init(mdc_init);
2808 module_exit(mdc_exit);