1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_MDC
43 # include <linux/module.h>
44 # include <linux/pagemap.h>
45 # include <linux/miscdevice.h>
46 # include <linux/init.h>
48 # include <liblustre.h>
51 #include <lustre_acl.h>
52 #include <obd_class.h>
53 #include <lustre_dlm.h>
54 #include <lustre_fid.h>
55 #include <md_object.h>
56 #include <lprocfs_status.h>
57 #include <lustre_param.h>
58 #include "mdc_internal.h"
59 #include <lustre/lustre_idl.h>
61 #define REQUEST_MINOR 244
63 static quota_interface_t *quota_interface;
64 extern quota_interface_t mdc_quota_interface;
66 static int mdc_cleanup(struct obd_device *obd);
68 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
69 const struct req_msg_field *field, struct obd_capa **oc)
71 struct lustre_capa *capa;
75 /* swabbed already in mdc_enqueue */
76 capa = req_capsule_server_get(&req->rq_pill, field);
80 c = alloc_capa(CAPA_SITE_CLIENT);
82 CDEBUG(D_INFO, "alloc capa failed!\n");
91 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
92 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
93 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
94 struct obd_capa **pc, int level, int msg_flags)
96 struct ptlrpc_request *req;
97 struct mdt_body *body;
101 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
102 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
106 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
107 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
108 req->rq_send_state = level;
110 ptlrpc_request_set_replen(req);
112 rc = ptlrpc_queue_wait(req);
116 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
118 GOTO(out, rc = -EPROTO);
120 if (body->valid & OBD_MD_FLMDSCAPA) {
121 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
126 *rootfid = body->fid1;
128 "root fid="DFID", last_committed="LPU64"\n",
130 lustre_msg_get_last_committed(req->rq_repmsg));
133 ptlrpc_req_finished(req);
137 /* This should be mdc_get_info("rootfid") */
138 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
139 struct obd_capa **pc)
141 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
146 * This function now is known to always saying that it will receive 4 buffers
147 * from server. Even for cases when acl_size and md_size is zero, RPC header
148 * will contain 4 fields and RPC itself will contain zero size fields. This is
149 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
150 * and thus zero, it shrinks it, making zero size. The same story about
151 * md_size. And this is course of problem when client waits for smaller number
152 * of fields. This issue will be fixed later when client gets aware of RPC
155 static int mdc_getattr_common(struct obd_export *exp,
156 struct ptlrpc_request *req)
158 struct req_capsule *pill = &req->rq_pill;
159 struct mdt_body *body;
164 /* Request message already built. */
165 rc = ptlrpc_queue_wait(req);
169 /* sanity check for the reply */
170 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
174 CDEBUG(D_NET, "mode: %o\n", body->mode);
176 if (body->eadatasize != 0) {
177 mdc_update_max_ea_from_body(exp, body);
179 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
185 if (body->valid & OBD_MD_FLRMTPERM) {
186 struct mdt_remote_perm *perm;
188 LASSERT(client_is_remote(exp));
189 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
190 lustre_swab_mdt_remote_perm);
195 if (body->valid & OBD_MD_FLMDSCAPA) {
196 struct lustre_capa *capa;
197 capa = req_capsule_server_get(pill, &RMF_CAPA1);
205 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
206 struct ptlrpc_request **request)
208 struct ptlrpc_request *req;
213 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
217 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
219 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
221 ptlrpc_request_free(req);
225 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
226 op_data->op_valid, op_data->op_mode, -1, 0);
228 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
230 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
231 LASSERT(client_is_remote(exp));
232 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
233 sizeof(struct mdt_remote_perm));
235 ptlrpc_request_set_replen(req);
237 rc = mdc_getattr_common(exp, req);
239 ptlrpc_req_finished(req);
245 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
246 struct ptlrpc_request **request)
248 struct ptlrpc_request *req;
253 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
254 &RQF_MDS_GETATTR_NAME);
258 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
259 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
260 op_data->op_namelen + 1);
262 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
264 ptlrpc_request_free(req);
268 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
269 op_data->op_valid, op_data->op_mode,
270 op_data->op_suppgids[0], 0);
272 if (op_data->op_name) {
273 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
274 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
275 op_data->op_namelen);
276 memcpy(name, op_data->op_name, op_data->op_namelen);
279 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
281 ptlrpc_request_set_replen(req);
283 rc = mdc_getattr_common(exp, req);
285 ptlrpc_req_finished(req);
291 static int mdc_is_subdir(struct obd_export *exp,
292 const struct lu_fid *pfid,
293 const struct lu_fid *cfid,
294 struct ptlrpc_request **request)
296 struct ptlrpc_request *req;
302 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
303 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
308 mdc_is_subdir_pack(req, pfid, cfid, 0);
309 ptlrpc_request_set_replen(req);
311 rc = ptlrpc_queue_wait(req);
312 if (rc && rc != -EREMOTE)
313 ptlrpc_req_finished(req);
319 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
320 const struct lu_fid *fid,
321 struct obd_capa *oc, int opcode, obd_valid valid,
322 const char *xattr_name, const char *input,
323 int input_size, int output_size, int flags,
324 __u32 suppgid, struct ptlrpc_request **request)
326 struct ptlrpc_request *req;
327 int xattr_namelen = 0;
333 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
337 mdc_set_capa_size(req, &RMF_CAPA1, oc);
339 xattr_namelen = strlen(xattr_name) + 1;
340 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
345 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
349 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
351 ptlrpc_request_free(req);
355 if (opcode == MDS_REINT) {
356 struct mdt_rec_setxattr *rec;
358 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
359 sizeof(struct mdt_rec_reint));
360 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
361 rec->sx_opcode = REINT_SETXATTR;
363 * cfs_curproc_fs{u,g}id() should replace
364 * current->fs{u,g}id for portability.
366 rec->sx_fsuid = cfs_curproc_fsuid();
367 rec->sx_fsgid = cfs_curproc_fsgid();
368 rec->sx_cap = cfs_curproc_cap_pack();
369 rec->sx_suppgid1 = suppgid;
370 rec->sx_suppgid2 = -1;
372 rec->sx_valid = valid | OBD_MD_FLCTIME;
373 rec->sx_time = cfs_time_current_sec();
374 rec->sx_size = output_size;
375 rec->sx_flags = flags;
377 mdc_pack_capa(req, &RMF_CAPA1, oc);
379 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
383 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
384 memcpy(tmp, xattr_name, xattr_namelen);
387 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
388 memcpy(tmp, input, input_size);
391 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
392 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
393 RCL_SERVER, output_size);
394 ptlrpc_request_set_replen(req);
397 if (opcode == MDS_REINT)
398 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
400 rc = ptlrpc_queue_wait(req);
402 if (opcode == MDS_REINT)
403 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
406 ptlrpc_req_finished(req);
412 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
413 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
414 const char *input, int input_size, int output_size,
415 int flags, __u32 suppgid, struct ptlrpc_request **request)
417 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
418 fid, oc, MDS_REINT, valid, xattr_name,
419 input, input_size, output_size, flags,
423 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
424 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
425 const char *input, int input_size, int output_size,
426 int flags, struct ptlrpc_request **request)
428 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
429 fid, oc, MDS_GETXATTR, valid, xattr_name,
430 input, input_size, output_size, flags,
434 #ifdef CONFIG_FS_POSIX_ACL
435 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
437 struct req_capsule *pill = &req->rq_pill;
438 struct mdt_body *body = md->body;
439 struct posix_acl *acl;
447 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
452 acl = posix_acl_from_xattr(buf, body->aclsize);
455 CERROR("convert xattr to acl: %d\n", rc);
459 rc = posix_acl_valid(acl);
461 CERROR("validate acl: %d\n", rc);
462 posix_acl_release(acl);
470 #define mdc_unpack_acl(req, md) 0
473 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
474 struct obd_export *dt_exp, struct obd_export *md_exp,
475 struct lustre_md *md)
477 struct req_capsule *pill = &req->rq_pill;
482 memset(md, 0, sizeof(*md));
484 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
485 LASSERT(md->body != NULL);
487 if (md->body->valid & OBD_MD_FLEASIZE) {
489 struct lov_mds_md *lmm;
491 if (!S_ISREG(md->body->mode)) {
492 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
493 "regular file, but is not\n");
494 GOTO(out, rc = -EPROTO);
497 if (md->body->eadatasize == 0) {
498 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
499 "but eadatasize 0\n");
500 GOTO(out, rc = -EPROTO);
502 lmmsize = md->body->eadatasize;
503 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
505 GOTO(out, rc = -EPROTO);
507 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
511 if (rc < sizeof(*md->lsm)) {
512 CDEBUG(D_INFO, "lsm size too small: "
513 "rc < sizeof (*md->lsm) (%d < %d)\n",
514 rc, (int)sizeof(*md->lsm));
515 GOTO(out, rc = -EPROTO);
518 } else if (md->body->valid & OBD_MD_FLDIREA) {
520 struct lov_mds_md *lmv;
522 if(!S_ISDIR(md->body->mode)) {
523 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
524 "directory, but is not\n");
525 GOTO(out, rc = -EPROTO);
528 if (md->body->eadatasize == 0) {
529 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
530 "but eadatasize 0\n");
533 if (md->body->valid & OBD_MD_MEA) {
534 lmvsize = md->body->eadatasize;
535 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
538 GOTO(out, rc = -EPROTO);
540 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
545 if (rc < sizeof(*md->mea)) {
546 CDEBUG(D_INFO, "size too small: "
547 "rc < sizeof(*md->mea) (%d < %d)\n",
548 rc, (int)sizeof(*md->mea));
549 GOTO(out, rc = -EPROTO);
555 if (md->body->valid & OBD_MD_FLRMTPERM) {
556 /* remote permission */
557 LASSERT(client_is_remote(exp));
558 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
559 lustre_swab_mdt_remote_perm);
560 if (!md->remote_perm)
561 GOTO(out, rc = -EPROTO);
563 else if (md->body->valid & OBD_MD_FLACL) {
564 /* for ACL, it's possible that FLACL is set but aclsize is zero.
565 * only when aclsize != 0 there's an actual segment for ACL
568 if (md->body->aclsize) {
569 rc = mdc_unpack_acl(req, md);
572 #ifdef CONFIG_FS_POSIX_ACL
574 md->posix_acl = NULL;
578 if (md->body->valid & OBD_MD_FLMDSCAPA) {
579 struct obd_capa *oc = NULL;
581 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
587 if (md->body->valid & OBD_MD_FLOSSCAPA) {
588 struct obd_capa *oc = NULL;
590 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
600 capa_put(md->oss_capa);
604 capa_put(md->mds_capa);
607 #ifdef CONFIG_FS_POSIX_ACL
608 posix_acl_release(md->posix_acl);
611 obd_free_memmd(dt_exp, &md->lsm);
616 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
623 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
626 void mdc_replay_open(struct ptlrpc_request *req)
628 struct md_open_data *mod = req->rq_cb_data;
629 struct ptlrpc_request *close_req;
630 struct obd_client_handle *och;
631 struct lustre_handle old;
632 struct mdt_body *body;
636 DEBUG_REQ(D_ERROR, req,
637 "Can't properly replay without open data.");
642 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
643 LASSERT(body != NULL);
647 struct lustre_handle *file_fh;
649 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
651 file_fh = &och->och_fh;
652 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
653 file_fh->cookie, body->handle.cookie);
655 *file_fh = body->handle;
657 close_req = mod->mod_close_req;
658 if (close_req != NULL) {
659 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
660 struct mdt_ioepoch *epoch;
662 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
663 epoch = req_capsule_client_get(&close_req->rq_pill,
668 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
669 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
670 epoch->handle = body->handle;
675 void mdc_commit_open(struct ptlrpc_request *req)
677 struct md_open_data *mod = req->rq_cb_data;
682 * No need to touch md_open_data::mod_och, it holds a reference on
683 * \var mod and will zero references to each other, \var mod will be
684 * freed after that when md_open_data::mod_och will put the reference.
688 * Do not let open request to disappear as it still may be needed
689 * for close rpc to happen (it may happen on evict only, otherwise
690 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
691 * called), just mark this rpc as committed to distinguish these 2
692 * cases, see mdc_close() for details. The open request reference will
693 * be put along with freeing \var mod.
695 ptlrpc_request_addref(req);
696 cfs_spin_lock(&req->rq_lock);
697 req->rq_committed = 1;
698 cfs_spin_unlock(&req->rq_lock);
699 req->rq_cb_data = NULL;
703 int mdc_set_open_replay_data(struct obd_export *exp,
704 struct obd_client_handle *och,
705 struct ptlrpc_request *open_req)
707 struct md_open_data *mod;
708 struct mdt_rec_create *rec;
709 struct mdt_body *body;
710 struct obd_import *imp = open_req->rq_import;
713 if (!open_req->rq_replay)
716 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
717 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
718 LASSERT(rec != NULL);
719 /* Incoming message in my byte order (it's been swabbed). */
720 /* Outgoing messages always in my byte order. */
721 LASSERT(body != NULL);
723 /* Only if the import is replayable, we set replay_open data */
724 if (och && imp->imp_replayable) {
725 mod = obd_mod_alloc();
727 DEBUG_REQ(D_ERROR, open_req,
728 "Can't allocate md_open_data");
733 * Take a reference on \var mod, to be freed on mdc_close().
734 * It protects \var mod from being freed on eviction (commit
735 * callback is called despite rq_replay flag).
736 * Another reference for \var och.
741 cfs_spin_lock(&open_req->rq_lock);
744 mod->mod_open_req = open_req;
745 open_req->rq_cb_data = mod;
746 open_req->rq_commit_cb = mdc_commit_open;
747 cfs_spin_unlock(&open_req->rq_lock);
750 rec->cr_fid2 = body->fid1;
751 rec->cr_ioepoch = body->ioepoch;
752 rec->cr_old_handle.cookie = body->handle.cookie;
753 open_req->rq_replay_cb = mdc_replay_open;
754 if (!fid_is_sane(&body->fid1)) {
755 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
760 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
764 int mdc_clear_open_replay_data(struct obd_export *exp,
765 struct obd_client_handle *och)
767 struct md_open_data *mod = och->och_mod;
770 LASSERT(mod != LP_POISON && mod != NULL);
779 /* Prepares the request for the replay by the given reply */
780 static void mdc_close_handle_reply(struct ptlrpc_request *req,
781 struct md_op_data *op_data, int rc) {
782 struct mdt_body *repbody;
783 struct mdt_ioepoch *epoch;
785 if (req && rc == -EAGAIN) {
786 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
787 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
789 epoch->flags |= MF_SOM_AU;
790 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
791 op_data->op_flags |= MF_GETATTR_LOCK;
795 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
796 struct md_open_data *mod, struct ptlrpc_request **request)
798 struct obd_device *obd = class_exp2obd(exp);
799 struct ptlrpc_request *req;
804 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
808 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
810 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
812 ptlrpc_request_free(req);
816 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
817 * portal whose threads are not taking any DLM locks and are therefore
818 * always progressing */
819 req->rq_request_portal = MDS_READPAGE_PORTAL;
820 ptlrpc_at_set_req_timeout(req);
822 /* Ensure that this close's handle is fixed up during replay. */
823 if (likely(mod != NULL)) {
824 LASSERTF(mod->mod_open_req != NULL &&
825 mod->mod_open_req->rq_type != LI_POISON,
826 "POISONED open %p!\n", mod->mod_open_req);
828 mod->mod_close_req = req;
830 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
831 /* We no longer want to preserve this open for replay even
832 * though the open was committed. b=3632, b=3633 */
833 cfs_spin_lock(&mod->mod_open_req->rq_lock);
834 mod->mod_open_req->rq_replay = 0;
835 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
837 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
840 mdc_close_pack(req, op_data);
842 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
843 obd->u.cli.cl_max_mds_easize);
844 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
845 obd->u.cli.cl_max_mds_cookiesize);
847 ptlrpc_request_set_replen(req);
849 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
850 rc = ptlrpc_queue_wait(req);
851 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
853 if (req->rq_repmsg == NULL) {
854 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
857 rc = req->rq_status ?: -EIO;
858 } else if (rc == 0 || rc == -EAGAIN) {
859 struct mdt_body *body;
861 rc = lustre_msg_get_status(req->rq_repmsg);
862 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
863 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
868 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
871 } else if (rc == -ESTALE) {
873 * it can be allowed error after 3633 if open was committed and
874 * server failed before close was sent. Let's check if mod
875 * exists and return no error in that case
878 LASSERT(mod->mod_open_req != NULL);
879 if (mod->mod_open_req->rq_committed)
886 mod->mod_close_req = NULL;
887 /* Since now, mod is accessed through open_req only,
888 * thus close req does not keep a reference on mod anymore. */
892 mdc_close_handle_reply(req, op_data, rc);
896 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
897 struct md_open_data *mod)
899 struct obd_device *obd = class_exp2obd(exp);
900 struct ptlrpc_request *req;
904 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
905 &RQF_MDS_DONE_WRITING);
909 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
910 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
912 ptlrpc_request_free(req);
917 LASSERTF(mod->mod_open_req != NULL &&
918 mod->mod_open_req->rq_type != LI_POISON,
919 "POISONED setattr %p!\n", mod->mod_open_req);
921 mod->mod_close_req = req;
922 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
923 /* We no longer want to preserve this setattr for replay even
924 * though the open was committed. b=3632, b=3633 */
925 cfs_spin_lock(&mod->mod_open_req->rq_lock);
926 mod->mod_open_req->rq_replay = 0;
927 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
930 mdc_close_pack(req, op_data);
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);
939 * it can be allowed error after 3633 if open or setattr were
940 * committed and server failed before close was sent.
941 * Let's check if mod exists and return no error in that case
944 LASSERT(mod->mod_open_req != NULL);
945 if (mod->mod_open_req->rq_committed)
952 mod->mod_close_req = NULL;
953 /* Since now, mod is accessed through setattr req only,
954 * thus DW req does not keep a reference on mod anymore. */
958 mdc_close_handle_reply(req, op_data, rc);
959 ptlrpc_req_finished(req);
963 #ifdef HAVE_SPLIT_SUPPORT
964 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
965 const struct page *page, int offset)
967 struct ptlrpc_request *req;
968 struct ptlrpc_bulk_desc *desc;
972 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
976 /* FIXME: capa doesn't support split yet */
977 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
979 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
981 ptlrpc_request_free(req);
985 req->rq_request_portal = MDS_READPAGE_PORTAL;
986 ptlrpc_at_set_req_timeout(req);
988 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
990 GOTO(out, rc = -ENOMEM);
992 /* NB req now owns desc and will free it when it gets freed. */
993 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
994 mdc_readdir_pack(req, 0, offset, fid, NULL);
996 ptlrpc_request_set_replen(req);
997 rc = ptlrpc_queue_wait(req);
1001 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1003 ptlrpc_req_finished(req);
1006 EXPORT_SYMBOL(mdc_sendpage);
1009 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
1010 struct obd_capa *oc, __u64 offset, struct page *page,
1011 struct ptlrpc_request **request)
1013 struct ptlrpc_request *req;
1014 struct ptlrpc_bulk_desc *desc;
1019 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1023 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1025 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1027 ptlrpc_request_free(req);
1031 req->rq_request_portal = MDS_READPAGE_PORTAL;
1032 ptlrpc_at_set_req_timeout(req);
1034 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1036 ptlrpc_request_free(req);
1040 /* NB req now owns desc and will free it when it gets freed */
1041 ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1042 mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
1044 ptlrpc_request_set_replen(req);
1045 rc = ptlrpc_queue_wait(req);
1047 ptlrpc_req_finished(req);
1051 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1052 req->rq_bulk->bd_nob_transferred);
1054 ptlrpc_req_finished(req);
1058 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1059 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1060 req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1061 ptlrpc_req_finished(req);
1069 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1070 __u64 max_age, __u32 flags)
1072 struct ptlrpc_request *req;
1073 struct obd_statfs *msfs;
1074 struct obd_import *imp = NULL;
1079 * Since the request might also come from lprocfs, so we need
1080 * sync this with client_disconnect_export Bug15684
1082 cfs_down_read(&obd->u.cli.cl_sem);
1083 if (obd->u.cli.cl_import)
1084 imp = class_import_get(obd->u.cli.cl_import);
1085 cfs_up_read(&obd->u.cli.cl_sem);
1089 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1090 LUSTRE_MDS_VERSION, MDS_STATFS);
1092 GOTO(output, rc = -ENOMEM);
1094 ptlrpc_request_set_replen(req);
1096 if (flags & OBD_STATFS_NODELAY) {
1097 /* procfs requests not want stay in wait for avoid deadlock */
1098 req->rq_no_resend = 1;
1099 req->rq_no_delay = 1;
1102 rc = ptlrpc_queue_wait(req);
1104 /* check connection error first */
1105 if (imp->imp_connect_error)
1106 rc = imp->imp_connect_error;
1110 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1112 GOTO(out, rc = -EPROTO);
1117 ptlrpc_req_finished(req);
1119 class_import_put(imp);
1123 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1125 __u32 keylen, vallen;
1129 if (gf->gf_pathlen > PATH_MAX)
1130 RETURN(-ENAMETOOLONG);
1131 if (gf->gf_pathlen < 2)
1134 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1135 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1136 OBD_ALLOC(key, keylen);
1139 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1140 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1142 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1143 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1145 if (!fid_is_sane(&gf->gf_fid))
1146 GOTO(out, rc = -EINVAL);
1148 /* Val is struct getinfo_fid2path result plus path */
1149 vallen = sizeof(*gf) + gf->gf_pathlen;
1151 rc = obd_get_info(exp, keylen, key, &vallen, gf, NULL);
1155 if (vallen <= sizeof(*gf))
1156 GOTO(out, rc = -EPROTO);
1157 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1158 GOTO(out, rc = -EOVERFLOW);
1160 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1161 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1164 OBD_FREE(key, keylen);
1168 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1170 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1172 LASSERT(len <= CR_MAXSIZE);
1174 lh->kuc_magic = KUC_MAGIC;
1175 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1176 lh->kuc_flags = flags;
1177 lh->kuc_msgtype = CL_RECORD;
1178 lh->kuc_msglen = len;
1182 #define D_CHANGELOG 0
1184 struct changelog_show {
1189 struct obd_device *cs_obd;
1192 static int changelog_show_cb(struct llog_handle *llh, struct llog_rec_hdr *hdr,
1195 struct changelog_show *cs = data;
1196 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1201 if ((rec->cr_hdr.lrh_type != CHANGELOG_REC) ||
1202 (rec->cr.cr_type >= CL_LAST)) {
1203 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
1208 if (rec->cr.cr_index < cs->cs_startrec) {
1209 /* Skip entries earlier than what we are interested in */
1210 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1211 rec->cr.cr_index, cs->cs_startrec);
1215 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1216 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1217 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1218 rec->cr.cr_flags & CLF_FLAGMASK,
1219 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1220 rec->cr.cr_namelen, rec->cr.cr_name);
1222 len = sizeof(*lh) + sizeof(rec->cr) + rec->cr.cr_namelen;
1224 /* Set up the message */
1225 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1226 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1228 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1229 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1234 static int mdc_changelog_send_thread(void *csdata)
1236 struct changelog_show *cs = csdata;
1237 struct llog_ctxt *ctxt = NULL;
1238 struct llog_handle *llh = NULL;
1239 struct kuc_hdr *kuch;
1242 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1243 cs->cs_fp, cs->cs_startrec);
1245 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1246 if (cs->cs_buf == NULL)
1247 GOTO(out, rc = -ENOMEM);
1249 /* Set up the remote catalog handle */
1250 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1252 GOTO(out, rc = -ENOENT);
1253 rc = llog_create(ctxt, &llh, NULL, CHANGELOG_CATALOG);
1255 CERROR("llog_create() failed %d\n", rc);
1258 rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
1260 CERROR("llog_init_handle failed %d\n", rc);
1264 /* We need the pipe fd open, so llog_process can't daemonize */
1265 rc = llog_cat_process_flags(llh, changelog_show_cb, cs,
1266 LLOG_FLAG_NODEAMON, 0, 0);
1268 /* Send EOF no matter what our result */
1269 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1271 kuch->kuc_msgtype = CL_EOF;
1272 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1276 cfs_put_file(cs->cs_fp);
1280 llog_ctxt_put(ctxt);
1282 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1287 static int mdc_ioc_changelog_send(struct obd_device *obd,
1288 struct ioc_changelog *icc)
1290 struct changelog_show *cs;
1293 /* Freed in mdc_changelog_send_thread */
1299 cs->cs_startrec = icc->icc_recno;
1300 /* matching cfs_put_file in mdc_changelog_send_thread */
1301 cs->cs_fp = cfs_get_fd(icc->icc_id);
1302 cs->cs_flags = icc->icc_flags;
1304 /* New thread because we should return to user app before
1305 writing into our pipe */
1306 rc = cfs_kernel_thread(mdc_changelog_send_thread, cs,
1307 CLONE_VM | CLONE_FILES);
1309 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1313 CERROR("Failed to start changelog thread: %d\n", rc);
1318 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1319 struct lustre_kernelcomm *lk);
1321 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1322 void *karg, void *uarg)
1324 struct obd_device *obd = exp->exp_obd;
1325 struct obd_ioctl_data *data = karg;
1326 struct obd_import *imp = obd->u.cli.cl_import;
1327 struct llog_ctxt *ctxt;
1331 if (!cfs_try_module_get(THIS_MODULE)) {
1332 CERROR("Can't get module. Is it alive?");
1336 case LL_IOC_HSM_CT_START:
1337 rc = mdc_ioc_hsm_ct_start(exp, karg);
1339 case OBD_IOC_CHANGELOG_SEND:
1340 rc = mdc_ioc_changelog_send(obd, karg);
1342 case OBD_IOC_CHANGELOG_CLEAR: {
1343 struct ioc_changelog *icc = karg;
1344 struct changelog_setinfo cs =
1345 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1346 rc = obd_set_info_async(exp, strlen(KEY_CHANGELOG_CLEAR),
1347 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1351 case OBD_IOC_FID2PATH: {
1352 rc = mdc_ioc_fid2path(exp, karg);
1355 case OBD_IOC_CLIENT_RECOVER:
1356 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1360 case IOC_OSC_SET_ACTIVE:
1361 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1363 case OBD_IOC_PARSE: {
1364 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1365 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1366 llog_ctxt_put(ctxt);
1370 case OBD_IOC_LLOG_INFO:
1371 case OBD_IOC_LLOG_PRINT: {
1372 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1373 rc = llog_ioctl(ctxt, cmd, data);
1374 llog_ctxt_put(ctxt);
1378 case OBD_IOC_POLL_QUOTACHECK:
1379 rc = lquota_poll_check(quota_interface, exp,
1380 (struct if_quotacheck *)karg);
1382 case OBD_IOC_PING_TARGET:
1383 rc = ptlrpc_obd_ping(obd);
1386 * Normally IOC_OBD_STATFS iocontrol is handled by LMV instead of MDC.
1387 * But when the cluster is upgraded from 1.8, there'd be no LMV layer
1388 * thus we might be called here. Eventually this code should be removed.
1391 case IOC_OBD_STATFS: {
1392 struct obd_statfs stat_buf = {0};
1394 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1395 GOTO(out, rc = -ENODEV);
1397 rc = mdc_statfs(obd, &stat_buf,
1398 cfs_time_current_64() - CFS_HZ, 0);
1402 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1404 GOTO(out, rc = -EFAULT);
1405 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1407 GOTO(out, rc = -EFAULT);
1412 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1413 GOTO(out, rc = -ENOTTY);
1416 cfs_module_put(THIS_MODULE);
1421 int mdc_get_info_rpc(struct obd_export *exp,
1422 obd_count keylen, void *key,
1423 int vallen, void *val)
1425 struct obd_import *imp = class_exp2cliimp(exp);
1426 struct ptlrpc_request *req;
1431 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1435 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1436 RCL_CLIENT, keylen);
1437 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1438 RCL_CLIENT, sizeof(__u32));
1440 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1442 ptlrpc_request_free(req);
1446 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1447 memcpy(tmp, key, keylen);
1448 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1449 memcpy(tmp, &vallen, sizeof(__u32));
1451 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1452 RCL_SERVER, vallen);
1453 ptlrpc_request_set_replen(req);
1455 rc = ptlrpc_queue_wait(req);
1457 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1458 memcpy(val, tmp, vallen);
1459 if (ptlrpc_rep_need_swab(req)) {
1460 if (KEY_IS(KEY_FID2PATH)) {
1461 lustre_swab_fid2path(val);
1465 ptlrpc_req_finished(req);
1470 static void lustre_swab_hai(struct hsm_action_item *h)
1472 __swab32s(&h->hai_len);
1473 __swab32s(&h->hai_action);
1474 lustre_swab_lu_fid(&h->hai_fid);
1475 __swab64s(&h->hai_cookie);
1476 __swab64s(&h->hai_extent_start);
1477 __swab64s(&h->hai_extent_end);
1478 __swab64s(&h->hai_gid);
1481 static void lustre_swab_hal(struct hsm_action_list *h)
1483 struct hsm_action_item *hai;
1486 __swab32s(&h->hal_version);
1487 __swab32s(&h->hal_count);
1488 __swab32s(&h->hal_archive_num);
1490 for (i = 0; i < h->hal_count; i++) {
1491 lustre_swab_hai(hai);
1492 hai = hai_next(hai);
1496 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1497 struct lustre_kernelcomm *lk)
1501 if (lk->lk_group != KUC_GRP_HSM) {
1502 CERROR("Bad copytool group %d\n", lk->lk_group);
1506 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1507 lk->lk_uid, lk->lk_group, lk->lk_flags);
1509 if (lk->lk_flags & LK_FLG_STOP)
1510 rc = libcfs_kkuc_group_rem(lk->lk_uid,lk->lk_group);
1512 cfs_file_t *fp = cfs_get_fd(lk->lk_wfd);
1513 rc = libcfs_kkuc_group_add(fp, lk->lk_uid,lk->lk_group);
1518 /* lk_data is archive number mask */
1519 /* TODO: register archive num with mdt so coordinator can choose
1526 * Send a message to any listening copytools
1527 * @param val KUC message (kuc_hdr + hsm_action_list)
1528 * @param len total length of message
1530 static int mdc_hsm_copytool_send(int len, void *val)
1532 struct kuc_hdr *lh = (struct kuc_hdr *)val;
1533 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1537 if (len < sizeof(*lh) + sizeof(*hal)) {
1538 CERROR("Short HSM message %d < %d\n", len,
1539 (int) (sizeof(*lh) + sizeof(*hal)));
1542 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1543 lustre_swab_kuch(lh);
1544 lustre_swab_hal(hal);
1545 } else if (lh->kuc_magic != KUC_MAGIC) {
1546 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
1550 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d\n",
1551 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
1552 lh->kuc_msglen, hal->hal_count);
1554 /* Broadcast to HSM listeners */
1555 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
1560 int mdc_set_info_async(struct obd_export *exp,
1561 obd_count keylen, void *key,
1562 obd_count vallen, void *val,
1563 struct ptlrpc_request_set *set)
1565 struct obd_import *imp = class_exp2cliimp(exp);
1569 if (KEY_IS(KEY_INIT_RECOV)) {
1570 if (vallen != sizeof(int))
1572 cfs_spin_lock(&imp->imp_lock);
1573 imp->imp_initial_recov = *(int *)val;
1574 cfs_spin_unlock(&imp->imp_lock);
1575 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1576 exp->exp_obd->obd_name, imp->imp_initial_recov);
1579 /* Turn off initial_recov after we try all backup servers once */
1580 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1581 if (vallen != sizeof(int))
1583 cfs_spin_lock(&imp->imp_lock);
1584 imp->imp_initial_recov_bk = *(int *)val;
1585 if (imp->imp_initial_recov_bk)
1586 imp->imp_initial_recov = 1;
1587 cfs_spin_unlock(&imp->imp_lock);
1588 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1589 exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1592 if (KEY_IS(KEY_READ_ONLY)) {
1593 if (vallen != sizeof(int))
1596 cfs_spin_lock(&imp->imp_lock);
1597 if (*((int *)val)) {
1598 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1599 imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1601 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1602 imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1604 cfs_spin_unlock(&imp->imp_lock);
1606 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
1607 keylen, key, vallen, val, set);
1610 if (KEY_IS(KEY_SPTLRPC_CONF)) {
1611 sptlrpc_conf_client_adapt(exp->exp_obd);
1614 if (KEY_IS(KEY_FLUSH_CTX)) {
1615 sptlrpc_import_flush_my_ctx(imp);
1618 if (KEY_IS(KEY_MDS_CONN)) {
1619 /* mds-mds import */
1620 cfs_spin_lock(&imp->imp_lock);
1621 imp->imp_server_timeout = 1;
1622 cfs_spin_unlock(&imp->imp_lock);
1623 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1624 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1627 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1628 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
1629 keylen, key, vallen, val, set);
1632 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
1633 rc = mdc_hsm_copytool_send(vallen, val);
1640 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1641 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1645 if (KEY_IS(KEY_MAX_EASIZE)) {
1646 int mdsize, *max_easize;
1648 if (*vallen != sizeof(int))
1650 mdsize = *(int*)val;
1651 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1652 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1654 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1657 if (KEY_IS(KEY_CONN_DATA)) {
1658 struct obd_import *imp = class_exp2cliimp(exp);
1659 struct obd_connect_data *data = val;
1661 if (*vallen != sizeof(*data))
1664 *data = imp->imp_connect_data;
1668 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
1673 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1674 struct obd_capa *oc, struct obd_client_handle *handle,
1677 struct ptlrpc_request *req;
1678 struct mdt_body *body;
1682 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1686 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1688 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1690 ptlrpc_request_free(req);
1694 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1696 ptlrpc_request_set_replen(req);
1698 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1699 rc = ptlrpc_queue_wait(req);
1700 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1702 CERROR("Pin failed: %d\n", rc);
1706 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1708 GOTO(err_out, rc = -EPROTO);
1710 handle->och_fh = body->handle;
1711 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1713 handle->och_mod = obd_mod_alloc();
1714 if (handle->och_mod == NULL) {
1715 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1716 GOTO(err_out, rc = -ENOMEM);
1718 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1723 ptlrpc_req_finished(req);
1727 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1730 struct ptlrpc_request *req;
1731 struct mdt_body *body;
1735 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1736 LUSTRE_MDS_VERSION, MDS_UNPIN);
1740 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1741 body->handle = handle->och_fh;
1744 ptlrpc_request_set_replen(req);
1746 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1747 rc = ptlrpc_queue_wait(req);
1748 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1751 CERROR("Unpin failed: %d\n", rc);
1753 ptlrpc_req_finished(req);
1754 ptlrpc_req_finished(handle->och_mod->mod_open_req);
1756 obd_mod_put(handle->och_mod);
1760 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1761 struct obd_capa *oc, struct ptlrpc_request **request)
1763 struct ptlrpc_request *req;
1768 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1772 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1774 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1776 ptlrpc_request_free(req);
1780 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1782 ptlrpc_request_set_replen(req);
1784 rc = ptlrpc_queue_wait(req);
1786 ptlrpc_req_finished(req);
1792 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1793 enum obd_import_event event)
1797 LASSERT(imp->imp_obd == obd);
1800 case IMP_EVENT_DISCON: {
1802 /* XXX Pass event up to OBDs stack. used only for FLD now */
1803 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1807 case IMP_EVENT_INACTIVE: {
1808 struct client_obd *cli = &obd->u.cli;
1810 * Flush current sequence to make client obtain new one
1811 * from server in case of disconnect/reconnect.
1812 * If range is already empty then no need to flush it.
1814 if (cli->cl_seq != NULL &&
1815 !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1816 seq_client_flush(cli->cl_seq);
1819 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1822 case IMP_EVENT_INVALIDATE: {
1823 struct ldlm_namespace *ns = obd->obd_namespace;
1825 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1829 case IMP_EVENT_ACTIVE: {
1830 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1834 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1838 CERROR("Unknown import event %x\n", event);
1844 static int mdc_fid_init(struct obd_export *exp)
1846 struct client_obd *cli = &exp->exp_obd->u.cli;
1851 OBD_ALLOC_PTR(cli->cl_seq);
1852 if (cli->cl_seq == NULL)
1855 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1857 GOTO(out_free_seq, rc = -ENOMEM);
1859 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1860 exp->exp_obd->obd_name);
1862 /* Init client side sequence-manager */
1863 rc = seq_client_init(cli->cl_seq, exp,
1864 LUSTRE_SEQ_METADATA,
1866 OBD_FREE(prefix, MAX_OBD_NAME + 5);
1868 GOTO(out_free_seq, rc);
1872 OBD_FREE_PTR(cli->cl_seq);
1877 static int mdc_fid_fini(struct obd_export *exp)
1879 struct client_obd *cli = &exp->exp_obd->u.cli;
1882 if (cli->cl_seq != NULL) {
1883 seq_client_fini(cli->cl_seq);
1884 OBD_FREE_PTR(cli->cl_seq);
1891 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1892 struct md_op_data *op_data)
1894 struct client_obd *cli = &exp->exp_obd->u.cli;
1895 struct lu_client_seq *seq = cli->cl_seq;
1897 RETURN(seq_client_alloc_fid(seq, fid));
1900 /* XXX This method is used only to clear current fid seq
1901 * once fld/mds insert failed */
1902 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1904 struct client_obd *cli = &exp->exp_obd->u.cli;
1906 seq_client_flush(cli->cl_seq);
1910 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
1911 struct client_obd *cli = &exp->exp_obd->u.cli;
1912 return &cli->cl_target_uuid;
1915 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1917 struct client_obd *cli = &obd->u.cli;
1918 struct lprocfs_static_vars lvars = { 0 };
1922 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1923 if (!cli->cl_rpc_lock)
1925 mdc_init_rpc_lock(cli->cl_rpc_lock);
1929 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1930 if (!cli->cl_setattr_lock)
1931 GOTO(err_rpc_lock, rc = -ENOMEM);
1932 mdc_init_rpc_lock(cli->cl_setattr_lock);
1934 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1935 if (!cli->cl_close_lock)
1936 GOTO(err_setattr_lock, rc = -ENOMEM);
1937 mdc_init_rpc_lock(cli->cl_close_lock);
1939 rc = client_obd_setup(obd, cfg);
1941 GOTO(err_close_lock, rc);
1942 lprocfs_mdc_init_vars(&lvars);
1943 lprocfs_obd_setup(obd, lvars.obd_vars);
1944 sptlrpc_lprocfs_cliobd_attach(obd);
1945 ptlrpc_lprocfs_register_obd(obd);
1947 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
1950 CERROR("failed to setup llogging subsystems\n");
1956 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1958 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1960 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1965 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
1966 * us to make MDS RPCs with large enough reply buffers to hold the
1967 * maximum-sized (= maximum striped) EA and cookie without having to
1968 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1969 static int mdc_init_ea_size(struct obd_export *exp, int easize,
1970 int def_easize, int cookiesize)
1972 struct obd_device *obd = exp->exp_obd;
1973 struct client_obd *cli = &obd->u.cli;
1976 if (cli->cl_max_mds_easize < easize)
1977 cli->cl_max_mds_easize = easize;
1979 if (cli->cl_default_mds_easize < def_easize)
1980 cli->cl_default_mds_easize = def_easize;
1982 if (cli->cl_max_mds_cookiesize < cookiesize)
1983 cli->cl_max_mds_cookiesize = cookiesize;
1988 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1994 case OBD_CLEANUP_EARLY:
1996 case OBD_CLEANUP_EXPORTS:
1997 /* Failsafe, ok if racy */
1998 if (obd->obd_type->typ_refcnt <= 1)
1999 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2001 /* If we set up but never connected, the
2002 client import will not have been cleaned. */
2003 if (obd->u.cli.cl_import) {
2004 struct obd_import *imp;
2005 cfs_down_write(&obd->u.cli.cl_sem);
2006 imp = obd->u.cli.cl_import;
2007 CERROR("client import never connected\n");
2008 ptlrpc_invalidate_import(imp);
2009 class_destroy_import(imp);
2010 cfs_up_write(&obd->u.cli.cl_sem);
2011 obd->u.cli.cl_import = NULL;
2013 rc = obd_llog_finish(obd, 0);
2015 CERROR("failed to cleanup llogging subsystems\n");
2021 static int mdc_cleanup(struct obd_device *obd)
2023 struct client_obd *cli = &obd->u.cli;
2025 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2026 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
2027 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2029 ptlrpc_lprocfs_unregister_obd(obd);
2030 lprocfs_obd_cleanup(obd);
2033 return client_obd_cleanup(obd);
2037 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2038 struct obd_device *tgt, int *index)
2040 struct llog_ctxt *ctxt;
2044 LASSERT(olg == &obd->obd_olg);
2046 rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
2051 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
2052 llog_initiator_connect(ctxt);
2053 llog_ctxt_put(ctxt);
2055 rc = llog_setup(obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt, 0, NULL,
2058 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2059 llog_initiator_connect(ctxt);
2060 llog_ctxt_put(ctxt);
2066 static int mdc_llog_finish(struct obd_device *obd, int count)
2068 struct llog_ctxt *ctxt;
2072 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
2074 rc = llog_cleanup(ctxt);
2076 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2078 rc = llog_cleanup(ctxt);
2083 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2085 struct lustre_cfg *lcfg = buf;
2086 struct lprocfs_static_vars lvars = { 0 };
2089 lprocfs_mdc_init_vars(&lvars);
2090 switch (lcfg->lcfg_command) {
2092 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2102 /* get remote permission for current user on fid */
2103 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2104 struct obd_capa *oc, __u32 suppgid,
2105 struct ptlrpc_request **request)
2107 struct ptlrpc_request *req;
2111 LASSERT(client_is_remote(exp));
2114 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2118 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2120 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2122 ptlrpc_request_free(req);
2126 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2128 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2129 sizeof(struct mdt_remote_perm));
2131 ptlrpc_request_set_replen(req);
2133 rc = ptlrpc_queue_wait(req);
2135 ptlrpc_req_finished(req);
2141 static int mdc_interpret_renew_capa(const struct lu_env *env,
2142 struct ptlrpc_request *req, void *unused,
2145 struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
2146 renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
2147 struct mdt_body *body = NULL;
2148 struct lustre_capa *capa;
2152 GOTO(out, capa = ERR_PTR(status));
2154 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2156 GOTO(out, capa = ERR_PTR(-EFAULT));
2158 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2159 GOTO(out, capa = ERR_PTR(-ENOENT));
2161 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2163 GOTO(out, capa = ERR_PTR(-EFAULT));
2170 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2173 struct ptlrpc_request *req;
2176 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2177 LUSTRE_MDS_VERSION, MDS_GETATTR);
2181 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2182 * capa to renew is oss capa.
2184 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2185 ptlrpc_request_set_replen(req);
2187 req->rq_async_args.pointer_arg[0] = oc;
2188 req->rq_async_args.pointer_arg[1] = cb;
2189 req->rq_interpret_reply = mdc_interpret_renew_capa;
2190 ptlrpcd_add_req(req, PSCOPE_OTHER);
2194 static int mdc_connect(const struct lu_env *env,
2195 struct obd_export **exp,
2196 struct obd_device *obd, struct obd_uuid *cluuid,
2197 struct obd_connect_data *data,
2200 struct obd_import *imp = obd->u.cli.cl_import;
2202 /* mds-mds import features */
2203 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2204 cfs_spin_lock(&imp->imp_lock);
2205 imp->imp_server_timeout = 1;
2206 cfs_spin_unlock(&imp->imp_lock);
2207 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2208 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2212 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2215 struct obd_ops mdc_obd_ops = {
2216 .o_owner = THIS_MODULE,
2217 .o_setup = mdc_setup,
2218 .o_precleanup = mdc_precleanup,
2219 .o_cleanup = mdc_cleanup,
2220 .o_add_conn = client_import_add_conn,
2221 .o_del_conn = client_import_del_conn,
2222 .o_connect = mdc_connect,
2223 .o_disconnect = client_disconnect_export,
2224 .o_iocontrol = mdc_iocontrol,
2225 .o_set_info_async = mdc_set_info_async,
2226 .o_statfs = mdc_statfs,
2228 .o_unpin = mdc_unpin,
2229 .o_fid_init = mdc_fid_init,
2230 .o_fid_fini = mdc_fid_fini,
2231 .o_fid_alloc = mdc_fid_alloc,
2232 .o_fid_delete = mdc_fid_delete,
2233 .o_import_event = mdc_import_event,
2234 .o_llog_init = mdc_llog_init,
2235 .o_llog_finish = mdc_llog_finish,
2236 .o_get_info = mdc_get_info,
2237 .o_process_config = mdc_process_config,
2238 .o_get_uuid = mdc_get_uuid,
2241 struct md_ops mdc_md_ops = {
2242 .m_getstatus = mdc_getstatus,
2243 .m_change_cbdata = mdc_change_cbdata,
2244 .m_find_cbdata = mdc_find_cbdata,
2245 .m_close = mdc_close,
2246 .m_create = mdc_create,
2247 .m_done_writing = mdc_done_writing,
2248 .m_enqueue = mdc_enqueue,
2249 .m_getattr = mdc_getattr,
2250 .m_getattr_name = mdc_getattr_name,
2251 .m_intent_lock = mdc_intent_lock,
2253 .m_is_subdir = mdc_is_subdir,
2254 .m_rename = mdc_rename,
2255 .m_setattr = mdc_setattr,
2256 .m_setxattr = mdc_setxattr,
2257 .m_getxattr = mdc_getxattr,
2259 .m_readpage = mdc_readpage,
2260 .m_unlink = mdc_unlink,
2261 .m_cancel_unused = mdc_cancel_unused,
2262 .m_init_ea_size = mdc_init_ea_size,
2263 .m_set_lock_data = mdc_set_lock_data,
2264 .m_lock_match = mdc_lock_match,
2265 .m_get_lustre_md = mdc_get_lustre_md,
2266 .m_free_lustre_md = mdc_free_lustre_md,
2267 .m_set_open_replay_data = mdc_set_open_replay_data,
2268 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2269 .m_renew_capa = mdc_renew_capa,
2270 .m_unpack_capa = mdc_unpack_capa,
2271 .m_get_remote_perm = mdc_get_remote_perm,
2272 .m_intent_getattr_async = mdc_intent_getattr_async,
2273 .m_revalidate_lock = mdc_revalidate_lock
2276 int __init mdc_init(void)
2279 struct lprocfs_static_vars lvars = { 0 };
2280 lprocfs_mdc_init_vars(&lvars);
2282 cfs_request_module("lquota");
2283 quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
2284 init_obd_quota_ops(quota_interface, &mdc_obd_ops);
2286 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2287 LUSTRE_MDC_NAME, NULL);
2288 if (rc && quota_interface)
2289 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2295 static void /*__exit*/ mdc_exit(void)
2297 if (quota_interface)
2298 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2300 class_unregister_type(LUSTRE_MDC_NAME);
2303 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2304 MODULE_DESCRIPTION("Lustre Metadata Client");
2305 MODULE_LICENSE("GPL");
2307 module_init(mdc_init);
2308 module_exit(mdc_exit);