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 <md_object.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 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
91 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
92 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
93 struct obd_capa **pc, int level, int msg_flags)
95 struct ptlrpc_request *req;
96 struct mdt_body *body;
100 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
101 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
105 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
106 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
107 req->rq_send_state = level;
109 ptlrpc_request_set_replen(req);
111 rc = ptlrpc_queue_wait(req);
115 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
117 GOTO(out, rc = -EPROTO);
119 if (body->valid & OBD_MD_FLMDSCAPA) {
120 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
125 *rootfid = body->fid1;
127 "root fid="DFID", last_committed="LPU64"\n",
129 lustre_msg_get_last_committed(req->rq_repmsg));
132 ptlrpc_req_finished(req);
136 /* This should be mdc_get_info("rootfid") */
137 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
138 struct obd_capa **pc)
140 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
145 * This function now is known to always saying that it will receive 4 buffers
146 * from server. Even for cases when acl_size and md_size is zero, RPC header
147 * will contain 4 fields and RPC itself will contain zero size fields. This is
148 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
149 * and thus zero, it shrinks it, making zero size. The same story about
150 * md_size. And this is course of problem when client waits for smaller number
151 * of fields. This issue will be fixed later when client gets aware of RPC
154 static int mdc_getattr_common(struct obd_export *exp,
155 struct ptlrpc_request *req)
157 struct req_capsule *pill = &req->rq_pill;
158 struct mdt_body *body;
163 /* Request message already built. */
164 rc = ptlrpc_queue_wait(req);
168 /* sanity check for the reply */
169 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
173 CDEBUG(D_NET, "mode: %o\n", body->mode);
175 if (body->eadatasize != 0) {
176 mdc_update_max_ea_from_body(exp, body);
178 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
184 if (body->valid & OBD_MD_FLRMTPERM) {
185 struct mdt_remote_perm *perm;
187 LASSERT(client_is_remote(exp));
188 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
189 lustre_swab_mdt_remote_perm);
194 if (body->valid & OBD_MD_FLMDSCAPA) {
195 struct lustre_capa *capa;
196 capa = req_capsule_server_get(pill, &RMF_CAPA1);
204 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
205 struct ptlrpc_request **request)
207 struct ptlrpc_request *req;
212 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
216 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
218 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
220 ptlrpc_request_free(req);
224 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
225 op_data->op_valid, op_data->op_mode, -1, 0);
227 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
229 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
230 LASSERT(client_is_remote(exp));
231 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
232 sizeof(struct mdt_remote_perm));
234 ptlrpc_request_set_replen(req);
236 rc = mdc_getattr_common(exp, req);
238 ptlrpc_req_finished(req);
244 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
245 struct ptlrpc_request **request)
247 struct ptlrpc_request *req;
252 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
253 &RQF_MDS_GETATTR_NAME);
257 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
258 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
259 op_data->op_namelen + 1);
261 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
263 ptlrpc_request_free(req);
267 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
268 op_data->op_valid, op_data->op_mode,
269 op_data->op_suppgids[0], 0);
271 if (op_data->op_name) {
272 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
273 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
274 op_data->op_namelen);
275 memcpy(name, op_data->op_name, op_data->op_namelen);
278 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
280 ptlrpc_request_set_replen(req);
282 rc = mdc_getattr_common(exp, req);
284 ptlrpc_req_finished(req);
290 static int mdc_is_subdir(struct obd_export *exp,
291 const struct lu_fid *pfid,
292 const struct lu_fid *cfid,
293 struct ptlrpc_request **request)
295 struct ptlrpc_request *req;
301 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
302 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
307 mdc_is_subdir_pack(req, pfid, cfid, 0);
308 ptlrpc_request_set_replen(req);
310 rc = ptlrpc_queue_wait(req);
311 if (rc && rc != -EREMOTE)
312 ptlrpc_req_finished(req);
318 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
319 const struct lu_fid *fid,
320 struct obd_capa *oc, int opcode, obd_valid valid,
321 const char *xattr_name, const char *input,
322 int input_size, int output_size, int flags,
323 __u32 suppgid, struct ptlrpc_request **request)
325 struct ptlrpc_request *req;
326 int xattr_namelen = 0;
332 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
336 mdc_set_capa_size(req, &RMF_CAPA1, oc);
338 xattr_namelen = strlen(xattr_name) + 1;
339 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
344 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
348 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
350 ptlrpc_request_free(req);
354 if (opcode == MDS_REINT) {
355 struct mdt_rec_setxattr *rec;
357 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
358 sizeof(struct mdt_rec_reint));
359 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
360 rec->sx_opcode = REINT_SETXATTR;
362 * cfs_curproc_fs{u,g}id() should replace
363 * current->fs{u,g}id for portability.
365 rec->sx_fsuid = cfs_curproc_fsuid();
366 rec->sx_fsgid = cfs_curproc_fsgid();
367 rec->sx_cap = cfs_curproc_cap_pack();
368 rec->sx_suppgid1 = suppgid;
369 rec->sx_suppgid2 = -1;
371 rec->sx_valid = valid | OBD_MD_FLCTIME;
372 rec->sx_time = cfs_time_current_sec();
373 rec->sx_size = output_size;
374 rec->sx_flags = flags;
376 mdc_pack_capa(req, &RMF_CAPA1, oc);
378 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
382 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
383 memcpy(tmp, xattr_name, xattr_namelen);
386 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
387 memcpy(tmp, input, input_size);
390 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
391 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
392 RCL_SERVER, output_size);
393 ptlrpc_request_set_replen(req);
396 if (opcode == MDS_REINT)
397 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
399 rc = ptlrpc_queue_wait(req);
401 if (opcode == MDS_REINT)
402 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
405 ptlrpc_req_finished(req);
411 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
412 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
413 const char *input, int input_size, int output_size,
414 int flags, __u32 suppgid, struct ptlrpc_request **request)
416 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
417 fid, oc, MDS_REINT, valid, xattr_name,
418 input, input_size, output_size, flags,
422 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
423 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
424 const char *input, int input_size, int output_size,
425 int flags, struct ptlrpc_request **request)
427 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
428 fid, oc, MDS_GETXATTR, valid, xattr_name,
429 input, input_size, output_size, flags,
433 #ifdef CONFIG_FS_POSIX_ACL
434 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
436 struct req_capsule *pill = &req->rq_pill;
437 struct mdt_body *body = md->body;
438 struct posix_acl *acl;
446 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
451 acl = posix_acl_from_xattr(buf, body->aclsize);
454 CERROR("convert xattr to acl: %d\n", rc);
458 rc = posix_acl_valid(acl);
460 CERROR("validate acl: %d\n", rc);
461 posix_acl_release(acl);
469 #define mdc_unpack_acl(req, md) 0
472 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
473 struct obd_export *dt_exp, struct obd_export *md_exp,
474 struct lustre_md *md)
476 struct req_capsule *pill = &req->rq_pill;
481 memset(md, 0, sizeof(*md));
483 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
484 LASSERT(md->body != NULL);
486 if (md->body->valid & OBD_MD_FLEASIZE) {
488 struct lov_mds_md *lmm;
490 if (!S_ISREG(md->body->mode)) {
491 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
492 "regular file, but is not\n");
493 GOTO(out, rc = -EPROTO);
496 if (md->body->eadatasize == 0) {
497 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
498 "but eadatasize 0\n");
499 GOTO(out, rc = -EPROTO);
501 lmmsize = md->body->eadatasize;
502 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
504 GOTO(out, rc = -EPROTO);
506 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
510 if (rc < sizeof(*md->lsm)) {
511 CDEBUG(D_INFO, "lsm size too small: "
512 "rc < sizeof (*md->lsm) (%d < %d)\n",
513 rc, (int)sizeof(*md->lsm));
514 GOTO(out, rc = -EPROTO);
517 } else if (md->body->valid & OBD_MD_FLDIREA) {
519 struct lov_mds_md *lmv;
521 if(!S_ISDIR(md->body->mode)) {
522 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
523 "directory, but is not\n");
524 GOTO(out, rc = -EPROTO);
527 if (md->body->eadatasize == 0) {
528 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
529 "but eadatasize 0\n");
532 if (md->body->valid & OBD_MD_MEA) {
533 lmvsize = md->body->eadatasize;
534 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
537 GOTO(out, rc = -EPROTO);
539 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
544 if (rc < sizeof(*md->mea)) {
545 CDEBUG(D_INFO, "size too small: "
546 "rc < sizeof(*md->mea) (%d < %d)\n",
547 rc, (int)sizeof(*md->mea));
548 GOTO(out, rc = -EPROTO);
554 if (md->body->valid & OBD_MD_FLRMTPERM) {
555 /* remote permission */
556 LASSERT(client_is_remote(exp));
557 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
558 lustre_swab_mdt_remote_perm);
559 if (!md->remote_perm)
560 GOTO(out, rc = -EPROTO);
562 else if (md->body->valid & OBD_MD_FLACL) {
563 /* for ACL, it's possible that FLACL is set but aclsize is zero.
564 * only when aclsize != 0 there's an actual segment for ACL
567 if (md->body->aclsize) {
568 rc = mdc_unpack_acl(req, md);
571 #ifdef CONFIG_FS_POSIX_ACL
573 md->posix_acl = NULL;
577 if (md->body->valid & OBD_MD_FLMDSCAPA) {
578 struct obd_capa *oc = NULL;
580 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
586 if (md->body->valid & OBD_MD_FLOSSCAPA) {
587 struct obd_capa *oc = NULL;
589 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
599 capa_put(md->oss_capa);
603 capa_put(md->mds_capa);
606 #ifdef CONFIG_FS_POSIX_ACL
607 posix_acl_release(md->posix_acl);
610 obd_free_memmd(dt_exp, &md->lsm);
615 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
622 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
625 void mdc_replay_open(struct ptlrpc_request *req)
627 struct md_open_data *mod = req->rq_cb_data;
628 struct ptlrpc_request *close_req;
629 struct obd_client_handle *och;
630 struct lustre_handle old;
631 struct mdt_body *body;
635 DEBUG_REQ(D_ERROR, req,
636 "Can't properly replay without open data.");
641 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
642 LASSERT(body != NULL);
646 struct lustre_handle *file_fh;
648 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
650 file_fh = &och->och_fh;
651 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
652 file_fh->cookie, body->handle.cookie);
654 *file_fh = body->handle;
656 close_req = mod->mod_close_req;
657 if (close_req != NULL) {
658 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
659 struct mdt_ioepoch *epoch;
661 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
662 epoch = req_capsule_client_get(&close_req->rq_pill,
667 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
668 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
669 epoch->handle = body->handle;
674 void mdc_commit_open(struct ptlrpc_request *req)
676 struct md_open_data *mod = req->rq_cb_data;
681 * No need to touch md_open_data::mod_och, it holds a reference on
682 * \var mod and will zero references to each other, \var mod will be
683 * freed after that when md_open_data::mod_och will put the reference.
687 * Do not let open request to disappear as it still may be needed
688 * for close rpc to happen (it may happen on evict only, otherwise
689 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
690 * called), just mark this rpc as committed to distinguish these 2
691 * cases, see mdc_close() for details. The open request reference will
692 * be put along with freeing \var mod.
694 ptlrpc_request_addref(req);
695 spin_lock(&req->rq_lock);
696 req->rq_committed = 1;
697 spin_unlock(&req->rq_lock);
698 req->rq_cb_data = NULL;
702 int mdc_set_open_replay_data(struct obd_export *exp,
703 struct obd_client_handle *och,
704 struct ptlrpc_request *open_req)
706 struct md_open_data *mod;
707 struct mdt_rec_create *rec;
708 struct mdt_body *body;
709 struct obd_import *imp = open_req->rq_import;
712 if (!open_req->rq_replay)
715 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
716 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
717 LASSERT(rec != NULL);
718 /* Incoming message in my byte order (it's been swabbed). */
719 /* Outgoing messages always in my byte order. */
720 LASSERT(body != NULL);
722 /* Only if the import is replayable, we set replay_open data */
723 if (och && imp->imp_replayable) {
724 mod = obd_mod_alloc();
726 DEBUG_REQ(D_ERROR, open_req,
727 "Can't allocate md_open_data");
732 * Take a reference on \var mod, to be freed on mdc_close().
733 * It protects \var mod from being freed on eviction (commit
734 * callback is called despite rq_replay flag).
735 * Another reference for \var och.
740 spin_lock(&open_req->rq_lock);
743 mod->mod_open_req = open_req;
744 open_req->rq_cb_data = mod;
745 open_req->rq_commit_cb = mdc_commit_open;
746 spin_unlock(&open_req->rq_lock);
749 rec->cr_fid2 = body->fid1;
750 rec->cr_ioepoch = body->ioepoch;
751 rec->cr_old_handle.cookie = body->handle.cookie;
752 open_req->rq_replay_cb = mdc_replay_open;
753 if (!fid_is_sane(&body->fid1)) {
754 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
759 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
763 int mdc_clear_open_replay_data(struct obd_export *exp,
764 struct obd_client_handle *och)
766 struct md_open_data *mod = och->och_mod;
770 * It is possible to not have \var mod in a case of eviction between
771 * lookup and ll_file_open().
776 LASSERT(mod != LP_POISON);
785 /* Prepares the request for the replay by the given reply */
786 static void mdc_close_handle_reply(struct ptlrpc_request *req,
787 struct md_op_data *op_data, int rc) {
788 struct mdt_body *repbody;
789 struct mdt_ioepoch *epoch;
791 if (req && rc == -EAGAIN) {
792 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
793 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
795 epoch->flags |= MF_SOM_AU;
796 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
797 op_data->op_flags |= MF_GETATTR_LOCK;
801 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
802 struct md_open_data *mod, struct ptlrpc_request **request)
804 struct obd_device *obd = class_exp2obd(exp);
805 struct ptlrpc_request *req;
810 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
814 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
816 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
818 ptlrpc_request_free(req);
822 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
823 * portal whose threads are not taking any DLM locks and are therefore
824 * always progressing */
825 req->rq_request_portal = MDS_READPAGE_PORTAL;
826 ptlrpc_at_set_req_timeout(req);
828 /* Ensure that this close's handle is fixed up during replay. */
829 if (likely(mod != NULL)) {
830 LASSERTF(mod->mod_open_req != NULL &&
831 mod->mod_open_req->rq_type != LI_POISON,
832 "POISONED open %p!\n", mod->mod_open_req);
834 mod->mod_close_req = req;
836 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
837 /* We no longer want to preserve this open for replay even
838 * though the open was committed. b=3632, b=3633 */
839 spin_lock(&mod->mod_open_req->rq_lock);
840 mod->mod_open_req->rq_replay = 0;
841 spin_unlock(&mod->mod_open_req->rq_lock);
843 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
846 mdc_close_pack(req, op_data);
848 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
849 obd->u.cli.cl_max_mds_easize);
850 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
851 obd->u.cli.cl_max_mds_cookiesize);
853 ptlrpc_request_set_replen(req);
855 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
856 rc = ptlrpc_queue_wait(req);
857 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
859 if (req->rq_repmsg == NULL) {
860 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
863 rc = req->rq_status ?: -EIO;
864 } else if (rc == 0 || rc == -EAGAIN) {
865 struct mdt_body *body;
867 rc = lustre_msg_get_status(req->rq_repmsg);
868 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
869 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
874 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
877 } else if (rc == -ESTALE) {
879 * it can be allowed error after 3633 if open was committed and
880 * server failed before close was sent. Let's check if mod
881 * exists and return no error in that case
884 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
885 LASSERT(mod->mod_open_req != NULL);
886 if (mod->mod_open_req->rq_committed)
893 mod->mod_close_req = NULL;
894 /* Since now, mod is accessed through open_req only,
895 * thus close req does not keep a reference on mod anymore. */
899 mdc_close_handle_reply(req, op_data, rc);
903 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
904 struct md_open_data *mod)
906 struct obd_device *obd = class_exp2obd(exp);
907 struct ptlrpc_request *req;
911 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
912 &RQF_MDS_DONE_WRITING);
916 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
917 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
919 ptlrpc_request_free(req);
924 LASSERTF(mod->mod_open_req != NULL &&
925 mod->mod_open_req->rq_type != LI_POISON,
926 "POISONED setattr %p!\n", mod->mod_open_req);
928 mod->mod_close_req = req;
929 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
930 /* We no longer want to preserve this setattr for replay even
931 * though the open was committed. b=3632, b=3633 */
932 spin_lock(&mod->mod_open_req->rq_lock);
933 mod->mod_open_req->rq_replay = 0;
934 spin_unlock(&mod->mod_open_req->rq_lock);
937 mdc_close_pack(req, op_data);
938 ptlrpc_request_set_replen(req);
940 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
941 rc = ptlrpc_queue_wait(req);
942 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
946 * it can be allowed error after 3633 if open or setattr were
947 * committed and server failed before close was sent.
948 * Let's check if mod exists and return no error in that case
951 LASSERT(mod->mod_open_req != NULL);
952 if (mod->mod_open_req->rq_committed)
959 mod->mod_close_req = NULL;
960 /* Since now, mod is accessed through setattr req only,
961 * thus DW req does not keep a reference on mod anymore. */
965 mdc_close_handle_reply(req, op_data, rc);
966 ptlrpc_req_finished(req);
970 #ifdef HAVE_SPLIT_SUPPORT
971 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
972 const struct page *page, int offset)
974 struct ptlrpc_request *req;
975 struct ptlrpc_bulk_desc *desc;
979 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
983 /* FIXME: capa doesn't support split yet */
984 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
986 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
988 ptlrpc_request_free(req);
992 req->rq_request_portal = MDS_READPAGE_PORTAL;
993 ptlrpc_at_set_req_timeout(req);
995 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
997 GOTO(out, rc = -ENOMEM);
999 /* NB req now owns desc and will free it when it gets freed. */
1000 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1001 mdc_readdir_pack(req, 0, offset, fid, NULL);
1003 ptlrpc_request_set_replen(req);
1004 rc = ptlrpc_queue_wait(req);
1008 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1010 ptlrpc_req_finished(req);
1013 EXPORT_SYMBOL(mdc_sendpage);
1016 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1017 struct page **pages, struct ptlrpc_request **request)
1019 struct ptlrpc_request *req;
1020 struct ptlrpc_bulk_desc *desc;
1024 struct l_wait_info lwi;
1029 cfs_waitq_init(&waitq);
1032 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1036 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1038 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1040 ptlrpc_request_free(req);
1044 req->rq_request_portal = MDS_READPAGE_PORTAL;
1045 ptlrpc_at_set_req_timeout(req);
1047 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1050 ptlrpc_request_free(req);
1054 /* NB req now owns desc and will free it when it gets freed */
1055 for (i = 0; i < op_data->op_npages; i++)
1056 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, CFS_PAGE_SIZE);
1058 mdc_readdir_pack(req, op_data->op_offset,
1059 CFS_PAGE_SIZE * op_data->op_npages,
1060 &op_data->op_fid1, op_data->op_capa1);
1062 ptlrpc_request_set_replen(req);
1063 rc = ptlrpc_queue_wait(req);
1065 ptlrpc_req_finished(req);
1066 if (rc != -ETIMEDOUT)
1070 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1071 CERROR("too many resend retries, returning error\n");
1074 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1075 l_wait_event(waitq, 0, &lwi);
1080 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1081 req->rq_bulk->bd_nob_transferred);
1083 ptlrpc_req_finished(req);
1087 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1088 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1089 req->rq_bulk->bd_nob_transferred,
1090 CFS_PAGE_SIZE * op_data->op_npages);
1091 ptlrpc_req_finished(req);
1099 static int mdc_statfs(const struct lu_env *env,
1100 struct obd_export *exp, struct obd_statfs *osfs,
1101 __u64 max_age, __u32 flags)
1103 struct obd_device *obd = class_exp2obd(exp);
1104 struct ptlrpc_request *req;
1105 struct obd_statfs *msfs;
1106 struct obd_import *imp = NULL;
1111 * Since the request might also come from lprocfs, so we need
1112 * sync this with client_disconnect_export Bug15684
1114 down_read(&obd->u.cli.cl_sem);
1115 if (obd->u.cli.cl_import)
1116 imp = class_import_get(obd->u.cli.cl_import);
1117 up_read(&obd->u.cli.cl_sem);
1121 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1122 LUSTRE_MDS_VERSION, MDS_STATFS);
1124 GOTO(output, rc = -ENOMEM);
1126 ptlrpc_request_set_replen(req);
1128 if (flags & OBD_STATFS_NODELAY) {
1129 /* procfs requests not want stay in wait for avoid deadlock */
1130 req->rq_no_resend = 1;
1131 req->rq_no_delay = 1;
1134 rc = ptlrpc_queue_wait(req);
1136 /* check connection error first */
1137 if (imp->imp_connect_error)
1138 rc = imp->imp_connect_error;
1142 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1144 GOTO(out, rc = -EPROTO);
1149 ptlrpc_req_finished(req);
1151 class_import_put(imp);
1155 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1157 __u32 keylen, vallen;
1161 if (gf->gf_pathlen > PATH_MAX)
1162 RETURN(-ENAMETOOLONG);
1163 if (gf->gf_pathlen < 2)
1166 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1167 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1168 OBD_ALLOC(key, keylen);
1171 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1172 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1174 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1175 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1177 if (!fid_is_sane(&gf->gf_fid))
1178 GOTO(out, rc = -EINVAL);
1180 /* Val is struct getinfo_fid2path result plus path */
1181 vallen = sizeof(*gf) + gf->gf_pathlen;
1183 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1187 if (vallen <= sizeof(*gf))
1188 GOTO(out, rc = -EPROTO);
1189 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1190 GOTO(out, rc = -EOVERFLOW);
1192 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1193 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1196 OBD_FREE(key, keylen);
1200 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1201 struct hsm_progress_kernel *hpk)
1203 struct obd_import *imp = class_exp2cliimp(exp);
1204 struct hsm_progress_kernel *req_hpk;
1205 struct ptlrpc_request *req;
1209 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1210 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1212 GOTO(out, rc = -ENOMEM);
1214 /* Copy hsm_progress struct */
1215 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1219 ptlrpc_request_set_replen(req);
1221 rc = ptlrpc_queue_wait(req);
1224 ptlrpc_req_finished(req);
1228 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1230 __u32 *archive_mask;
1231 struct ptlrpc_request *req;
1235 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1237 MDS_HSM_CT_REGISTER);
1239 GOTO(out, rc = -ENOMEM);
1241 /* Copy hsm_progress struct */
1242 archive_mask = req_capsule_client_get(&req->rq_pill,
1243 &RMF_MDS_HSM_ARCHIVE);
1244 LASSERT(archive_mask);
1245 *archive_mask = archives;
1247 ptlrpc_request_set_replen(req);
1249 rc = ptlrpc_queue_wait(req);
1252 ptlrpc_req_finished(req);
1256 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1257 struct md_op_data *op_data)
1259 struct hsm_current_action *hca = op_data->op_data;
1260 struct hsm_current_action *req_hca;
1261 struct ptlrpc_request *req;
1265 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1266 &RQF_MDS_HSM_ACTION);
1270 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1272 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1274 ptlrpc_request_free(req);
1278 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1279 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1281 ptlrpc_request_set_replen(req);
1283 rc = ptlrpc_queue_wait(req);
1287 req_hca = req_capsule_server_get(&req->rq_pill,
1288 &RMF_MDS_HSM_CURRENT_ACTION);
1289 if (req_hca == NULL)
1290 GOTO(out, rc = -EPROTO);
1296 ptlrpc_req_finished(req);
1300 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1302 struct ptlrpc_request *req;
1306 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1308 MDS_HSM_CT_UNREGISTER);
1310 GOTO(out, rc = -ENOMEM);
1312 ptlrpc_request_set_replen(req);
1314 rc = ptlrpc_queue_wait(req);
1317 ptlrpc_req_finished(req);
1321 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1322 struct md_op_data *op_data)
1324 struct hsm_user_state *hus = op_data->op_data;
1325 struct hsm_user_state *req_hus;
1326 struct ptlrpc_request *req;
1330 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1331 &RQF_MDS_HSM_STATE_GET);
1335 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1337 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1339 ptlrpc_request_free(req);
1343 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1344 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1346 ptlrpc_request_set_replen(req);
1348 rc = ptlrpc_queue_wait(req);
1352 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1353 if (req_hus == NULL)
1354 GOTO(out, rc = -EPROTO);
1360 ptlrpc_req_finished(req);
1364 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1365 struct md_op_data *op_data)
1367 struct hsm_state_set *hss = op_data->op_data;
1368 struct hsm_state_set *req_hss;
1369 struct ptlrpc_request *req;
1373 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1374 &RQF_MDS_HSM_STATE_SET);
1378 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1380 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1382 ptlrpc_request_free(req);
1386 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1387 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1390 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1394 ptlrpc_request_set_replen(req);
1396 rc = ptlrpc_queue_wait(req);
1401 ptlrpc_req_finished(req);
1405 static int mdc_ioc_hsm_request(struct obd_export *exp,
1406 struct hsm_user_request *hur)
1408 struct obd_import *imp = class_exp2cliimp(exp);
1409 struct ptlrpc_request *req;
1410 struct hsm_request *req_hr;
1411 struct hsm_user_item *req_hui;
1416 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1418 GOTO(out, rc = -ENOMEM);
1420 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1421 hur->hur_request.hr_itemcount
1422 * sizeof(struct hsm_user_item));
1423 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1424 hur->hur_request.hr_data_len);
1426 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1428 ptlrpc_request_free(req);
1432 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1434 /* Copy hsm_request struct */
1435 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1437 *req_hr = hur->hur_request;
1439 /* Copy hsm_user_item structs */
1440 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1442 memcpy(req_hui, hur->hur_user_item,
1443 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1445 /* Copy opaque field */
1446 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1447 LASSERT(req_opaque);
1448 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1450 ptlrpc_request_set_replen(req);
1452 rc = ptlrpc_queue_wait(req);
1456 ptlrpc_req_finished(req);
1460 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1462 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1464 LASSERT(len <= CR_MAXSIZE);
1466 lh->kuc_magic = KUC_MAGIC;
1467 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1468 lh->kuc_flags = flags;
1469 lh->kuc_msgtype = CL_RECORD;
1470 lh->kuc_msglen = len;
1474 #define D_CHANGELOG 0
1476 struct changelog_show {
1481 struct obd_device *cs_obd;
1484 static int changelog_show_cb(const struct lu_env *env, struct llog_handle *llh,
1485 struct llog_rec_hdr *hdr, void *data)
1487 struct changelog_show *cs = data;
1488 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1493 if ((rec->cr_hdr.lrh_type != CHANGELOG_REC) ||
1494 (rec->cr.cr_type >= CL_LAST)) {
1495 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
1500 if (rec->cr.cr_index < cs->cs_startrec) {
1501 /* Skip entries earlier than what we are interested in */
1502 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1503 rec->cr.cr_index, cs->cs_startrec);
1507 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1508 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1509 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1510 rec->cr.cr_flags & CLF_FLAGMASK,
1511 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1512 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1514 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1516 /* Set up the message */
1517 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1518 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1520 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1521 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1526 static int mdc_changelog_send_thread(void *csdata)
1528 struct changelog_show *cs = csdata;
1529 struct llog_ctxt *ctxt = NULL;
1530 struct llog_handle *llh = NULL;
1531 struct kuc_hdr *kuch;
1534 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1535 cs->cs_fp, cs->cs_startrec);
1538 * It's important to daemonize here to close unused FDs.
1539 * The write fd from pipe is already opened by the caller,
1540 * so it's fine to clear all files here
1542 cfs_daemonize("mdc_clg_send_thread");
1544 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1545 if (cs->cs_buf == NULL)
1546 GOTO(out, rc = -ENOMEM);
1548 /* Set up the remote catalog handle */
1549 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1551 GOTO(out, rc = -ENOENT);
1552 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1555 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1556 cs->cs_obd->obd_name, rc);
1559 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1561 CERROR("llog_init_handle failed %d\n", rc);
1565 rc = llog_cat_process(NULL, llh, changelog_show_cb, cs, 0, 0);
1567 /* Send EOF no matter what our result */
1568 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1570 kuch->kuc_msgtype = CL_EOF;
1571 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1575 cfs_put_file(cs->cs_fp);
1577 llog_cat_close(NULL, llh);
1579 llog_ctxt_put(ctxt);
1581 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1583 /* detach from parent process so we get cleaned up */
1584 cfs_daemonize("cl_send");
1588 static int mdc_ioc_changelog_send(struct obd_device *obd,
1589 struct ioc_changelog *icc)
1591 struct changelog_show *cs;
1594 /* Freed in mdc_changelog_send_thread */
1600 cs->cs_startrec = icc->icc_recno;
1601 /* matching cfs_put_file in mdc_changelog_send_thread */
1602 cs->cs_fp = cfs_get_fd(icc->icc_id);
1603 cs->cs_flags = icc->icc_flags;
1605 /* New thread because we should return to user app before
1606 writing into our pipe */
1607 rc = cfs_create_thread(mdc_changelog_send_thread, cs, CFS_DAEMON_FLAGS);
1609 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1613 CERROR("Failed to start changelog thread: %d\n", rc);
1618 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1619 struct lustre_kernelcomm *lk);
1621 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1622 struct obd_quotactl *oqctl)
1624 struct client_obd *cli = &exp->exp_obd->u.cli;
1625 struct ptlrpc_request *req;
1626 struct obd_quotactl *body;
1630 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1631 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1636 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1639 ptlrpc_request_set_replen(req);
1641 /* the next poll will find -ENODATA, that means quotacheck is
1643 cli->cl_qchk_stat = -ENODATA;
1644 rc = ptlrpc_queue_wait(req);
1646 cli->cl_qchk_stat = rc;
1647 ptlrpc_req_finished(req);
1651 static int mdc_quota_poll_check(struct obd_export *exp,
1652 struct if_quotacheck *qchk)
1654 struct client_obd *cli = &exp->exp_obd->u.cli;
1658 qchk->obd_uuid = cli->cl_target_uuid;
1659 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1661 rc = cli->cl_qchk_stat;
1662 /* the client is not the previous one */
1663 if (rc == CL_NOT_QUOTACHECKED)
1668 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1669 struct obd_quotactl *oqctl)
1671 struct ptlrpc_request *req;
1672 struct obd_quotactl *oqc;
1676 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1677 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1682 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1685 ptlrpc_request_set_replen(req);
1686 ptlrpc_at_set_req_timeout(req);
1687 req->rq_no_resend = 1;
1689 rc = ptlrpc_queue_wait(req);
1691 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1693 if (req->rq_repmsg &&
1694 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1697 CERROR ("Can't unpack obd_quotactl\n");
1700 ptlrpc_req_finished(req);
1705 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1706 struct md_op_data *op_data)
1708 CFS_LIST_HEAD(cancels);
1709 struct ptlrpc_request *req;
1711 struct mdc_swap_layouts *msl, *payload;
1714 msl = op_data->op_data;
1716 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1717 * first thing it will do is to cancel the 2 layout
1718 * locks hold by this client.
1719 * So the client must cancel its layout locks on the 2 fids
1720 * with the request RPC to avoid extra RPC round trips
1722 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1723 LCK_CR, MDS_INODELOCK_LAYOUT);
1724 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1725 LCK_CR, MDS_INODELOCK_LAYOUT);
1727 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1728 &RQF_MDS_SWAP_LAYOUTS);
1730 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1734 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1735 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1737 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1739 ptlrpc_request_free(req);
1743 mdc_swap_layouts_pack(req, op_data);
1745 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1750 ptlrpc_request_set_replen(req);
1752 rc = ptlrpc_queue_wait(req);
1758 ptlrpc_req_finished(req);
1762 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1763 void *karg, void *uarg)
1765 struct obd_device *obd = exp->exp_obd;
1766 struct obd_ioctl_data *data = karg;
1767 struct obd_import *imp = obd->u.cli.cl_import;
1768 struct llog_ctxt *ctxt;
1772 if (!cfs_try_module_get(THIS_MODULE)) {
1773 CERROR("Can't get module. Is it alive?");
1777 case OBD_IOC_CHANGELOG_SEND:
1778 rc = mdc_ioc_changelog_send(obd, karg);
1780 case OBD_IOC_CHANGELOG_CLEAR: {
1781 struct ioc_changelog *icc = karg;
1782 struct changelog_setinfo cs =
1783 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1784 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1785 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1789 case OBD_IOC_FID2PATH:
1790 rc = mdc_ioc_fid2path(exp, karg);
1792 case LL_IOC_HSM_CT_START:
1793 rc = mdc_ioc_hsm_ct_start(exp, karg);
1795 case LL_IOC_HSM_PROGRESS:
1796 rc = mdc_ioc_hsm_progress(exp, karg);
1798 case LL_IOC_HSM_STATE_GET:
1799 rc = mdc_ioc_hsm_state_get(exp, karg);
1801 case LL_IOC_HSM_STATE_SET:
1802 rc = mdc_ioc_hsm_state_set(exp, karg);
1803 case LL_IOC_HSM_ACTION:
1804 rc = mdc_ioc_hsm_current_action(exp, karg);
1806 case LL_IOC_HSM_REQUEST:
1807 rc = mdc_ioc_hsm_request(exp, karg);
1809 case OBD_IOC_CLIENT_RECOVER:
1810 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1814 case IOC_OSC_SET_ACTIVE:
1815 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1817 case OBD_IOC_PARSE: {
1818 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1819 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1821 llog_ctxt_put(ctxt);
1825 case OBD_IOC_LLOG_INFO:
1826 case OBD_IOC_LLOG_PRINT: {
1827 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1828 rc = llog_ioctl(NULL, ctxt, cmd, data);
1829 llog_ctxt_put(ctxt);
1833 case OBD_IOC_POLL_QUOTACHECK:
1834 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1836 case OBD_IOC_PING_TARGET:
1837 rc = ptlrpc_obd_ping(obd);
1840 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1841 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1842 * there'd be no LMV layer thus we might be called here. Eventually
1843 * this code should be removed.
1846 case IOC_OBD_STATFS: {
1847 struct obd_statfs stat_buf = {0};
1849 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1850 GOTO(out, rc = -ENODEV);
1853 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1854 min((int) data->ioc_plen2,
1855 (int) sizeof(struct obd_uuid))))
1856 GOTO(out, rc = -EFAULT);
1858 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1859 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1864 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1865 min((int) data->ioc_plen1,
1866 (int) sizeof(stat_buf))))
1867 GOTO(out, rc = -EFAULT);
1871 case OBD_IOC_QUOTACTL: {
1872 struct if_quotactl *qctl = karg;
1873 struct obd_quotactl *oqctl;
1875 OBD_ALLOC_PTR(oqctl);
1879 QCTL_COPY(oqctl, qctl);
1880 rc = obd_quotactl(exp, oqctl);
1882 QCTL_COPY(qctl, oqctl);
1883 qctl->qc_valid = QC_MDTIDX;
1884 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1886 OBD_FREE_PTR(oqctl);
1889 case LL_IOC_GET_CONNECT_FLAGS: {
1890 if (cfs_copy_to_user(uarg,
1891 exp_connect_flags_ptr(exp),
1893 GOTO(out, rc = -EFAULT);
1897 case LL_IOC_LOV_SWAP_LAYOUTS: {
1898 rc = mdc_ioc_swap_layouts(exp, karg);
1902 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1903 GOTO(out, rc = -ENOTTY);
1906 cfs_module_put(THIS_MODULE);
1911 int mdc_get_info_rpc(struct obd_export *exp,
1912 obd_count keylen, void *key,
1913 int vallen, void *val)
1915 struct obd_import *imp = class_exp2cliimp(exp);
1916 struct ptlrpc_request *req;
1921 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1925 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1926 RCL_CLIENT, keylen);
1927 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1928 RCL_CLIENT, sizeof(__u32));
1930 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1932 ptlrpc_request_free(req);
1936 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1937 memcpy(tmp, key, keylen);
1938 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1939 memcpy(tmp, &vallen, sizeof(__u32));
1941 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1942 RCL_SERVER, vallen);
1943 ptlrpc_request_set_replen(req);
1945 rc = ptlrpc_queue_wait(req);
1947 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1948 memcpy(val, tmp, vallen);
1949 if (ptlrpc_rep_need_swab(req)) {
1950 if (KEY_IS(KEY_FID2PATH)) {
1951 lustre_swab_fid2path(val);
1955 ptlrpc_req_finished(req);
1960 static void lustre_swab_hai(struct hsm_action_item *h)
1962 __swab32s(&h->hai_len);
1963 __swab32s(&h->hai_action);
1964 lustre_swab_lu_fid(&h->hai_fid);
1965 lustre_swab_lu_fid(&h->hai_dfid);
1966 __swab64s(&h->hai_cookie);
1967 __swab64s(&h->hai_extent.offset);
1968 __swab64s(&h->hai_extent.length);
1969 __swab64s(&h->hai_gid);
1972 static void lustre_swab_hal(struct hsm_action_list *h)
1974 struct hsm_action_item *hai;
1977 __swab32s(&h->hal_version);
1978 __swab32s(&h->hal_count);
1979 __swab32s(&h->hal_archive_id);
1980 __swab64s(&h->hal_flags);
1982 for (i = 0; i < h->hal_count; i++) {
1983 lustre_swab_hai(hai);
1984 hai = hai_next(hai);
1988 static void lustre_swab_kuch(struct kuc_hdr *l)
1990 __swab16s(&l->kuc_magic);
1991 /* __u8 l->kuc_transport */
1992 __swab16s(&l->kuc_msgtype);
1993 __swab16s(&l->kuc_msglen);
1996 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1997 struct lustre_kernelcomm *lk)
1999 struct obd_import *imp = class_exp2cliimp(exp);
2000 __u32 archive = lk->lk_data;
2003 if (lk->lk_group != KUC_GRP_HSM) {
2004 CERROR("Bad copytool group %d\n", lk->lk_group);
2008 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2009 lk->lk_uid, lk->lk_group, lk->lk_flags);
2011 if (lk->lk_flags & LK_FLG_STOP) {
2012 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
2013 /* Unregister with the coordinator */
2015 rc = mdc_ioc_hsm_ct_unregister(imp);
2017 cfs_file_t *fp = cfs_get_fd(lk->lk_wfd);
2018 rc = libcfs_kkuc_group_add(fp, lk->lk_uid, lk->lk_group,
2023 rc = mdc_ioc_hsm_ct_register(imp, archive);
2030 * Send a message to any listening copytools
2031 * @param val KUC message (kuc_hdr + hsm_action_list)
2032 * @param len total length of message
2034 static int mdc_hsm_copytool_send(int len, void *val)
2036 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2037 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2041 if (len < sizeof(*lh) + sizeof(*hal)) {
2042 CERROR("Short HSM message %d < %d\n", len,
2043 (int) (sizeof(*lh) + sizeof(*hal)));
2046 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2047 lustre_swab_kuch(lh);
2048 lustre_swab_hal(hal);
2049 } else if (lh->kuc_magic != KUC_MAGIC) {
2050 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2054 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2056 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2057 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2059 /* Broadcast to HSM listeners */
2060 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2066 * callback function passed to kuc for re-registering each HSM copytool
2067 * running on MDC, after MDT shutdown/recovery.
2068 * @param data archive id served by the copytool
2069 * @param cb_arg callback argument (obd_import)
2071 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2073 struct obd_import *imp = (struct obd_import *)cb_arg;
2074 __u32 archive = data;
2077 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2079 rc = mdc_ioc_hsm_ct_register(imp, archive);
2081 /* ignore error if the copytool is already registered */
2082 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2086 * Re-establish all kuc contexts with MDT
2087 * after MDT shutdown/recovery.
2089 static int mdc_kuc_reregister(struct obd_import *imp)
2091 /* re-register HSM agents */
2092 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2096 int mdc_set_info_async(const struct lu_env *env,
2097 struct obd_export *exp,
2098 obd_count keylen, void *key,
2099 obd_count vallen, void *val,
2100 struct ptlrpc_request_set *set)
2102 struct obd_import *imp = class_exp2cliimp(exp);
2106 if (KEY_IS(KEY_READ_ONLY)) {
2107 if (vallen != sizeof(int))
2110 spin_lock(&imp->imp_lock);
2111 if (*((int *)val)) {
2112 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2113 imp->imp_connect_data.ocd_connect_flags |=
2116 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2117 imp->imp_connect_data.ocd_connect_flags &=
2118 ~OBD_CONNECT_RDONLY;
2120 spin_unlock(&imp->imp_lock);
2122 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2123 keylen, key, vallen, val, set);
2126 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2127 sptlrpc_conf_client_adapt(exp->exp_obd);
2130 if (KEY_IS(KEY_FLUSH_CTX)) {
2131 sptlrpc_import_flush_my_ctx(imp);
2134 if (KEY_IS(KEY_MDS_CONN)) {
2135 /* mds-mds import */
2136 spin_lock(&imp->imp_lock);
2137 imp->imp_server_timeout = 1;
2138 spin_unlock(&imp->imp_lock);
2139 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2140 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2143 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2144 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2145 keylen, key, vallen, val, set);
2148 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2149 rc = mdc_hsm_copytool_send(vallen, val);
2153 CERROR("Unknown key %s\n", (char *)key);
2157 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2158 __u32 keylen, void *key, __u32 *vallen, void *val,
2159 struct lov_stripe_md *lsm)
2163 if (KEY_IS(KEY_MAX_EASIZE)) {
2164 int mdsize, *max_easize;
2166 if (*vallen != sizeof(int))
2168 mdsize = *(int*)val;
2169 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2170 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2172 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2174 } else if (KEY_IS(KEY_CONN_DATA)) {
2175 struct obd_import *imp = class_exp2cliimp(exp);
2176 struct obd_connect_data *data = val;
2178 if (*vallen != sizeof(*data))
2181 *data = imp->imp_connect_data;
2183 } else if (KEY_IS(KEY_TGT_COUNT)) {
2188 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2193 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2194 struct obd_capa *oc, struct obd_client_handle *handle,
2197 struct ptlrpc_request *req;
2198 struct mdt_body *body;
2202 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2206 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2208 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2210 ptlrpc_request_free(req);
2214 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2216 ptlrpc_request_set_replen(req);
2218 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2219 rc = ptlrpc_queue_wait(req);
2220 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2222 CERROR("Pin failed: %d\n", rc);
2226 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2228 GOTO(err_out, rc = -EPROTO);
2230 handle->och_fh = body->handle;
2231 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2233 handle->och_mod = obd_mod_alloc();
2234 if (handle->och_mod == NULL) {
2235 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2236 GOTO(err_out, rc = -ENOMEM);
2238 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2243 ptlrpc_req_finished(req);
2247 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2250 struct ptlrpc_request *req;
2251 struct mdt_body *body;
2255 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2256 LUSTRE_MDS_VERSION, MDS_UNPIN);
2260 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2261 body->handle = handle->och_fh;
2264 ptlrpc_request_set_replen(req);
2266 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2267 rc = ptlrpc_queue_wait(req);
2268 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2271 CERROR("Unpin failed: %d\n", rc);
2273 ptlrpc_req_finished(req);
2274 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2276 obd_mod_put(handle->och_mod);
2280 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2281 struct obd_capa *oc, struct ptlrpc_request **request)
2283 struct ptlrpc_request *req;
2288 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2292 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2294 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2296 ptlrpc_request_free(req);
2300 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2302 ptlrpc_request_set_replen(req);
2304 rc = ptlrpc_queue_wait(req);
2306 ptlrpc_req_finished(req);
2312 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2313 enum obd_import_event event)
2317 LASSERT(imp->imp_obd == obd);
2320 case IMP_EVENT_DISCON: {
2322 /* XXX Pass event up to OBDs stack. used only for FLD now */
2323 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2327 case IMP_EVENT_INACTIVE: {
2328 struct client_obd *cli = &obd->u.cli;
2330 * Flush current sequence to make client obtain new one
2331 * from server in case of disconnect/reconnect.
2333 if (cli->cl_seq != NULL)
2334 seq_client_flush(cli->cl_seq);
2336 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2339 case IMP_EVENT_INVALIDATE: {
2340 struct ldlm_namespace *ns = obd->obd_namespace;
2342 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2346 case IMP_EVENT_ACTIVE:
2347 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2348 /* restore re-establish kuc registration after reconnecting */
2350 rc = mdc_kuc_reregister(imp);
2353 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2355 case IMP_EVENT_DEACTIVATE:
2356 case IMP_EVENT_ACTIVATE:
2359 CERROR("Unknown import event %x\n", event);
2365 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2366 struct md_op_data *op_data)
2368 struct client_obd *cli = &exp->exp_obd->u.cli;
2369 struct lu_client_seq *seq = cli->cl_seq;
2371 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2374 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2375 struct client_obd *cli = &exp->exp_obd->u.cli;
2376 return &cli->cl_target_uuid;
2380 * Determine whether the lock can be canceled before replaying it during
2381 * recovery, non zero value will be return if the lock can be canceled,
2382 * or zero returned for not
2384 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2386 if (lock->l_resource->lr_type != LDLM_IBITS)
2389 /* FIXME: if we ever get into a situation where there are too many
2390 * opened files with open locks on a single node, then we really
2391 * should replay these open locks to reget it */
2392 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2398 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2400 struct client_obd *cli = &obd->u.cli;
2401 struct lprocfs_static_vars lvars = { 0 };
2405 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2406 if (!cli->cl_rpc_lock)
2408 mdc_init_rpc_lock(cli->cl_rpc_lock);
2412 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2413 if (!cli->cl_close_lock)
2414 GOTO(err_rpc_lock, rc = -ENOMEM);
2415 mdc_init_rpc_lock(cli->cl_close_lock);
2417 rc = client_obd_setup(obd, cfg);
2419 GOTO(err_close_lock, rc);
2420 lprocfs_mdc_init_vars(&lvars);
2421 lprocfs_obd_setup(obd, lvars.obd_vars);
2422 sptlrpc_lprocfs_cliobd_attach(obd);
2423 ptlrpc_lprocfs_register_obd(obd);
2425 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2427 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2430 CERROR("failed to setup llogging subsystems\n");
2436 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2438 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2443 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2444 * us to make MDS RPCs with large enough reply buffers to hold the
2445 * maximum-sized (= maximum striped) EA and cookie without having to
2446 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2447 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2448 int def_easize, int cookiesize)
2450 struct obd_device *obd = exp->exp_obd;
2451 struct client_obd *cli = &obd->u.cli;
2454 if (cli->cl_max_mds_easize < easize)
2455 cli->cl_max_mds_easize = easize;
2457 if (cli->cl_default_mds_easize < def_easize)
2458 cli->cl_default_mds_easize = def_easize;
2460 if (cli->cl_max_mds_cookiesize < cookiesize)
2461 cli->cl_max_mds_cookiesize = cookiesize;
2466 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2472 case OBD_CLEANUP_EARLY:
2474 case OBD_CLEANUP_EXPORTS:
2475 /* Failsafe, ok if racy */
2476 if (obd->obd_type->typ_refcnt <= 1)
2477 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2479 obd_cleanup_client_import(obd);
2480 ptlrpc_lprocfs_unregister_obd(obd);
2481 lprocfs_obd_cleanup(obd);
2483 rc = obd_llog_finish(obd, 0);
2485 CERROR("failed to cleanup llogging subsystems\n");
2491 static int mdc_cleanup(struct obd_device *obd)
2493 struct client_obd *cli = &obd->u.cli;
2495 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2496 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2500 return client_obd_cleanup(obd);
2504 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2505 struct obd_device *tgt, int *index)
2507 struct llog_ctxt *ctxt;
2512 LASSERT(olg == &obd->obd_olg);
2514 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2519 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2520 llog_initiator_connect(ctxt);
2521 llog_ctxt_put(ctxt);
2526 static int mdc_llog_finish(struct obd_device *obd, int count)
2528 struct llog_ctxt *ctxt;
2532 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2534 llog_cleanup(NULL, ctxt);
2539 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2541 struct lustre_cfg *lcfg = buf;
2542 struct lprocfs_static_vars lvars = { 0 };
2545 lprocfs_mdc_init_vars(&lvars);
2546 switch (lcfg->lcfg_command) {
2548 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2558 /* get remote permission for current user on fid */
2559 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2560 struct obd_capa *oc, __u32 suppgid,
2561 struct ptlrpc_request **request)
2563 struct ptlrpc_request *req;
2567 LASSERT(client_is_remote(exp));
2570 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2574 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2576 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2578 ptlrpc_request_free(req);
2582 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2584 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2585 sizeof(struct mdt_remote_perm));
2587 ptlrpc_request_set_replen(req);
2589 rc = ptlrpc_queue_wait(req);
2591 ptlrpc_req_finished(req);
2597 static int mdc_interpret_renew_capa(const struct lu_env *env,
2598 struct ptlrpc_request *req, void *args,
2601 struct mdc_renew_capa_args *ra = args;
2602 struct mdt_body *body = NULL;
2603 struct lustre_capa *capa;
2607 GOTO(out, capa = ERR_PTR(status));
2609 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2611 GOTO(out, capa = ERR_PTR(-EFAULT));
2613 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2614 GOTO(out, capa = ERR_PTR(-ENOENT));
2616 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2618 GOTO(out, capa = ERR_PTR(-EFAULT));
2621 ra->ra_cb(ra->ra_oc, capa);
2625 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2628 struct ptlrpc_request *req;
2629 struct mdc_renew_capa_args *ra;
2632 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2633 LUSTRE_MDS_VERSION, MDS_GETATTR);
2637 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2638 * capa to renew is oss capa.
2640 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2641 ptlrpc_request_set_replen(req);
2643 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2644 ra = ptlrpc_req_async_args(req);
2647 req->rq_interpret_reply = mdc_interpret_renew_capa;
2648 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2652 static int mdc_connect(const struct lu_env *env,
2653 struct obd_export **exp,
2654 struct obd_device *obd, struct obd_uuid *cluuid,
2655 struct obd_connect_data *data,
2658 struct obd_import *imp = obd->u.cli.cl_import;
2660 /* mds-mds import features */
2661 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2662 spin_lock(&imp->imp_lock);
2663 imp->imp_server_timeout = 1;
2664 spin_unlock(&imp->imp_lock);
2665 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2666 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2670 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2673 struct obd_ops mdc_obd_ops = {
2674 .o_owner = THIS_MODULE,
2675 .o_setup = mdc_setup,
2676 .o_precleanup = mdc_precleanup,
2677 .o_cleanup = mdc_cleanup,
2678 .o_add_conn = client_import_add_conn,
2679 .o_del_conn = client_import_del_conn,
2680 .o_connect = mdc_connect,
2681 .o_disconnect = client_disconnect_export,
2682 .o_iocontrol = mdc_iocontrol,
2683 .o_set_info_async = mdc_set_info_async,
2684 .o_statfs = mdc_statfs,
2686 .o_unpin = mdc_unpin,
2687 .o_fid_init = client_fid_init,
2688 .o_fid_fini = client_fid_fini,
2689 .o_fid_alloc = mdc_fid_alloc,
2690 .o_import_event = mdc_import_event,
2691 .o_llog_init = mdc_llog_init,
2692 .o_llog_finish = mdc_llog_finish,
2693 .o_get_info = mdc_get_info,
2694 .o_process_config = mdc_process_config,
2695 .o_get_uuid = mdc_get_uuid,
2696 .o_quotactl = mdc_quotactl,
2697 .o_quotacheck = mdc_quotacheck
2700 struct md_ops mdc_md_ops = {
2701 .m_getstatus = mdc_getstatus,
2702 .m_change_cbdata = mdc_change_cbdata,
2703 .m_find_cbdata = mdc_find_cbdata,
2704 .m_close = mdc_close,
2705 .m_create = mdc_create,
2706 .m_done_writing = mdc_done_writing,
2707 .m_enqueue = mdc_enqueue,
2708 .m_getattr = mdc_getattr,
2709 .m_getattr_name = mdc_getattr_name,
2710 .m_intent_lock = mdc_intent_lock,
2712 .m_is_subdir = mdc_is_subdir,
2713 .m_rename = mdc_rename,
2714 .m_setattr = mdc_setattr,
2715 .m_setxattr = mdc_setxattr,
2716 .m_getxattr = mdc_getxattr,
2718 .m_readpage = mdc_readpage,
2719 .m_unlink = mdc_unlink,
2720 .m_cancel_unused = mdc_cancel_unused,
2721 .m_init_ea_size = mdc_init_ea_size,
2722 .m_set_lock_data = mdc_set_lock_data,
2723 .m_lock_match = mdc_lock_match,
2724 .m_get_lustre_md = mdc_get_lustre_md,
2725 .m_free_lustre_md = mdc_free_lustre_md,
2726 .m_set_open_replay_data = mdc_set_open_replay_data,
2727 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2728 .m_renew_capa = mdc_renew_capa,
2729 .m_unpack_capa = mdc_unpack_capa,
2730 .m_get_remote_perm = mdc_get_remote_perm,
2731 .m_intent_getattr_async = mdc_intent_getattr_async,
2732 .m_revalidate_lock = mdc_revalidate_lock
2735 int __init mdc_init(void)
2738 struct lprocfs_static_vars lvars = { 0 };
2739 lprocfs_mdc_init_vars(&lvars);
2741 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2742 LUSTRE_MDC_NAME, NULL);
2747 static void /*__exit*/ mdc_exit(void)
2749 class_unregister_type(LUSTRE_MDC_NAME);
2752 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2753 MODULE_DESCRIPTION("Lustre Metadata Client");
2754 MODULE_LICENSE("GPL");
2756 module_init(mdc_init);
2757 module_exit(mdc_exit);