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 (c) 2001, 2010, Oracle and/or its affiliates. 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 struct mdc_renew_capa_args {
64 struct obd_capa *ra_oc;
65 renew_capa_cb_t ra_cb;
68 static quota_interface_t *quota_interface;
69 extern quota_interface_t mdc_quota_interface;
71 static int mdc_cleanup(struct obd_device *obd);
73 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
74 const struct req_msg_field *field, struct obd_capa **oc)
76 struct lustre_capa *capa;
80 /* swabbed already in mdc_enqueue */
81 capa = req_capsule_server_get(&req->rq_pill, field);
85 c = alloc_capa(CAPA_SITE_CLIENT);
87 CDEBUG(D_INFO, "alloc capa failed!\n");
96 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
97 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
98 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
99 struct obd_capa **pc, int level, int msg_flags)
101 struct ptlrpc_request *req;
102 struct mdt_body *body;
106 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
107 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
111 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
112 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
113 req->rq_send_state = level;
115 ptlrpc_request_set_replen(req);
117 rc = ptlrpc_queue_wait(req);
121 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
123 GOTO(out, rc = -EPROTO);
125 if (body->valid & OBD_MD_FLMDSCAPA) {
126 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
131 *rootfid = body->fid1;
133 "root fid="DFID", last_committed="LPU64"\n",
135 lustre_msg_get_last_committed(req->rq_repmsg));
138 ptlrpc_req_finished(req);
142 /* This should be mdc_get_info("rootfid") */
143 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
144 struct obd_capa **pc)
146 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
151 * This function now is known to always saying that it will receive 4 buffers
152 * from server. Even for cases when acl_size and md_size is zero, RPC header
153 * will contain 4 fields and RPC itself will contain zero size fields. This is
154 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
155 * and thus zero, it shrinks it, making zero size. The same story about
156 * md_size. And this is course of problem when client waits for smaller number
157 * of fields. This issue will be fixed later when client gets aware of RPC
160 static int mdc_getattr_common(struct obd_export *exp,
161 struct ptlrpc_request *req)
163 struct req_capsule *pill = &req->rq_pill;
164 struct mdt_body *body;
169 /* Request message already built. */
170 rc = ptlrpc_queue_wait(req);
174 /* sanity check for the reply */
175 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
179 CDEBUG(D_NET, "mode: %o\n", body->mode);
181 if (body->eadatasize != 0) {
182 mdc_update_max_ea_from_body(exp, body);
184 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
190 if (body->valid & OBD_MD_FLRMTPERM) {
191 struct mdt_remote_perm *perm;
193 LASSERT(client_is_remote(exp));
194 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
195 lustre_swab_mdt_remote_perm);
200 if (body->valid & OBD_MD_FLMDSCAPA) {
201 struct lustre_capa *capa;
202 capa = req_capsule_server_get(pill, &RMF_CAPA1);
210 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
211 struct ptlrpc_request **request)
213 struct ptlrpc_request *req;
218 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
222 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
224 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
226 ptlrpc_request_free(req);
230 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
231 op_data->op_valid, op_data->op_mode, -1, 0);
233 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
235 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
236 LASSERT(client_is_remote(exp));
237 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
238 sizeof(struct mdt_remote_perm));
240 ptlrpc_request_set_replen(req);
242 rc = mdc_getattr_common(exp, req);
244 ptlrpc_req_finished(req);
250 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
251 struct ptlrpc_request **request)
253 struct ptlrpc_request *req;
258 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
259 &RQF_MDS_GETATTR_NAME);
263 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
264 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
265 op_data->op_namelen + 1);
267 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
269 ptlrpc_request_free(req);
273 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
274 op_data->op_valid, op_data->op_mode,
275 op_data->op_suppgids[0], 0);
277 if (op_data->op_name) {
278 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
279 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
280 op_data->op_namelen);
281 memcpy(name, op_data->op_name, op_data->op_namelen);
284 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
286 ptlrpc_request_set_replen(req);
288 rc = mdc_getattr_common(exp, req);
290 ptlrpc_req_finished(req);
296 static int mdc_is_subdir(struct obd_export *exp,
297 const struct lu_fid *pfid,
298 const struct lu_fid *cfid,
299 struct ptlrpc_request **request)
301 struct ptlrpc_request *req;
307 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
308 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
313 mdc_is_subdir_pack(req, pfid, cfid, 0);
314 ptlrpc_request_set_replen(req);
316 rc = ptlrpc_queue_wait(req);
317 if (rc && rc != -EREMOTE)
318 ptlrpc_req_finished(req);
324 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
325 const struct lu_fid *fid,
326 struct obd_capa *oc, int opcode, obd_valid valid,
327 const char *xattr_name, const char *input,
328 int input_size, int output_size, int flags,
329 __u32 suppgid, struct ptlrpc_request **request)
331 struct ptlrpc_request *req;
332 int xattr_namelen = 0;
338 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
342 mdc_set_capa_size(req, &RMF_CAPA1, oc);
344 xattr_namelen = strlen(xattr_name) + 1;
345 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
350 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
354 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
356 ptlrpc_request_free(req);
360 if (opcode == MDS_REINT) {
361 struct mdt_rec_setxattr *rec;
363 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
364 sizeof(struct mdt_rec_reint));
365 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
366 rec->sx_opcode = REINT_SETXATTR;
368 * cfs_curproc_fs{u,g}id() should replace
369 * current->fs{u,g}id for portability.
371 rec->sx_fsuid = cfs_curproc_fsuid();
372 rec->sx_fsgid = cfs_curproc_fsgid();
373 rec->sx_cap = cfs_curproc_cap_pack();
374 rec->sx_suppgid1 = suppgid;
375 rec->sx_suppgid2 = -1;
377 rec->sx_valid = valid | OBD_MD_FLCTIME;
378 rec->sx_time = cfs_time_current_sec();
379 rec->sx_size = output_size;
380 rec->sx_flags = flags;
382 mdc_pack_capa(req, &RMF_CAPA1, oc);
384 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
388 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
389 memcpy(tmp, xattr_name, xattr_namelen);
392 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
393 memcpy(tmp, input, input_size);
396 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
397 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
398 RCL_SERVER, output_size);
399 ptlrpc_request_set_replen(req);
402 if (opcode == MDS_REINT)
403 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
405 rc = ptlrpc_queue_wait(req);
407 if (opcode == MDS_REINT)
408 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
411 ptlrpc_req_finished(req);
417 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
418 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
419 const char *input, int input_size, int output_size,
420 int flags, __u32 suppgid, struct ptlrpc_request **request)
422 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
423 fid, oc, MDS_REINT, valid, xattr_name,
424 input, input_size, output_size, flags,
428 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
429 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
430 const char *input, int input_size, int output_size,
431 int flags, struct ptlrpc_request **request)
433 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
434 fid, oc, MDS_GETXATTR, valid, xattr_name,
435 input, input_size, output_size, flags,
439 #ifdef CONFIG_FS_POSIX_ACL
440 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
442 struct req_capsule *pill = &req->rq_pill;
443 struct mdt_body *body = md->body;
444 struct posix_acl *acl;
452 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
457 acl = posix_acl_from_xattr(buf, body->aclsize);
460 CERROR("convert xattr to acl: %d\n", rc);
464 rc = posix_acl_valid(acl);
466 CERROR("validate acl: %d\n", rc);
467 posix_acl_release(acl);
475 #define mdc_unpack_acl(req, md) 0
478 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
479 struct obd_export *dt_exp, struct obd_export *md_exp,
480 struct lustre_md *md)
482 struct req_capsule *pill = &req->rq_pill;
487 memset(md, 0, sizeof(*md));
489 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
490 LASSERT(md->body != NULL);
492 if (md->body->valid & OBD_MD_FLEASIZE) {
494 struct lov_mds_md *lmm;
496 if (!S_ISREG(md->body->mode)) {
497 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
498 "regular file, but is not\n");
499 GOTO(out, rc = -EPROTO);
502 if (md->body->eadatasize == 0) {
503 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
504 "but eadatasize 0\n");
505 GOTO(out, rc = -EPROTO);
507 lmmsize = md->body->eadatasize;
508 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
510 GOTO(out, rc = -EPROTO);
512 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
516 if (rc < sizeof(*md->lsm)) {
517 CDEBUG(D_INFO, "lsm size too small: "
518 "rc < sizeof (*md->lsm) (%d < %d)\n",
519 rc, (int)sizeof(*md->lsm));
520 GOTO(out, rc = -EPROTO);
523 } else if (md->body->valid & OBD_MD_FLDIREA) {
525 struct lov_mds_md *lmv;
527 if(!S_ISDIR(md->body->mode)) {
528 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
529 "directory, but is not\n");
530 GOTO(out, rc = -EPROTO);
533 if (md->body->eadatasize == 0) {
534 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
535 "but eadatasize 0\n");
538 if (md->body->valid & OBD_MD_MEA) {
539 lmvsize = md->body->eadatasize;
540 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
543 GOTO(out, rc = -EPROTO);
545 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
550 if (rc < sizeof(*md->mea)) {
551 CDEBUG(D_INFO, "size too small: "
552 "rc < sizeof(*md->mea) (%d < %d)\n",
553 rc, (int)sizeof(*md->mea));
554 GOTO(out, rc = -EPROTO);
560 if (md->body->valid & OBD_MD_FLRMTPERM) {
561 /* remote permission */
562 LASSERT(client_is_remote(exp));
563 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
564 lustre_swab_mdt_remote_perm);
565 if (!md->remote_perm)
566 GOTO(out, rc = -EPROTO);
568 else if (md->body->valid & OBD_MD_FLACL) {
569 /* for ACL, it's possible that FLACL is set but aclsize is zero.
570 * only when aclsize != 0 there's an actual segment for ACL
573 if (md->body->aclsize) {
574 rc = mdc_unpack_acl(req, md);
577 #ifdef CONFIG_FS_POSIX_ACL
579 md->posix_acl = NULL;
583 if (md->body->valid & OBD_MD_FLMDSCAPA) {
584 struct obd_capa *oc = NULL;
586 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
592 if (md->body->valid & OBD_MD_FLOSSCAPA) {
593 struct obd_capa *oc = NULL;
595 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
605 capa_put(md->oss_capa);
609 capa_put(md->mds_capa);
612 #ifdef CONFIG_FS_POSIX_ACL
613 posix_acl_release(md->posix_acl);
616 obd_free_memmd(dt_exp, &md->lsm);
621 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
628 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
631 void mdc_replay_open(struct ptlrpc_request *req)
633 struct md_open_data *mod = req->rq_cb_data;
634 struct ptlrpc_request *close_req;
635 struct obd_client_handle *och;
636 struct lustre_handle old;
637 struct mdt_body *body;
641 DEBUG_REQ(D_ERROR, req,
642 "Can't properly replay without open data.");
647 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
648 LASSERT(body != NULL);
652 struct lustre_handle *file_fh;
654 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
656 file_fh = &och->och_fh;
657 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
658 file_fh->cookie, body->handle.cookie);
660 *file_fh = body->handle;
662 close_req = mod->mod_close_req;
663 if (close_req != NULL) {
664 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
665 struct mdt_ioepoch *epoch;
667 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
668 epoch = req_capsule_client_get(&close_req->rq_pill,
673 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
674 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
675 epoch->handle = body->handle;
680 void mdc_commit_open(struct ptlrpc_request *req)
682 struct md_open_data *mod = req->rq_cb_data;
687 * No need to touch md_open_data::mod_och, it holds a reference on
688 * \var mod and will zero references to each other, \var mod will be
689 * freed after that when md_open_data::mod_och will put the reference.
693 * Do not let open request to disappear as it still may be needed
694 * for close rpc to happen (it may happen on evict only, otherwise
695 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
696 * called), just mark this rpc as committed to distinguish these 2
697 * cases, see mdc_close() for details. The open request reference will
698 * be put along with freeing \var mod.
700 ptlrpc_request_addref(req);
701 cfs_spin_lock(&req->rq_lock);
702 req->rq_committed = 1;
703 cfs_spin_unlock(&req->rq_lock);
704 req->rq_cb_data = NULL;
708 int mdc_set_open_replay_data(struct obd_export *exp,
709 struct obd_client_handle *och,
710 struct ptlrpc_request *open_req)
712 struct md_open_data *mod;
713 struct mdt_rec_create *rec;
714 struct mdt_body *body;
715 struct obd_import *imp = open_req->rq_import;
718 if (!open_req->rq_replay)
721 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
722 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
723 LASSERT(rec != NULL);
724 /* Incoming message in my byte order (it's been swabbed). */
725 /* Outgoing messages always in my byte order. */
726 LASSERT(body != NULL);
728 /* Only if the import is replayable, we set replay_open data */
729 if (och && imp->imp_replayable) {
730 mod = obd_mod_alloc();
732 DEBUG_REQ(D_ERROR, open_req,
733 "Can't allocate md_open_data");
738 * Take a reference on \var mod, to be freed on mdc_close().
739 * It protects \var mod from being freed on eviction (commit
740 * callback is called despite rq_replay flag).
741 * Another reference for \var och.
746 cfs_spin_lock(&open_req->rq_lock);
749 mod->mod_open_req = open_req;
750 open_req->rq_cb_data = mod;
751 open_req->rq_commit_cb = mdc_commit_open;
752 cfs_spin_unlock(&open_req->rq_lock);
755 rec->cr_fid2 = body->fid1;
756 rec->cr_ioepoch = body->ioepoch;
757 rec->cr_old_handle.cookie = body->handle.cookie;
758 open_req->rq_replay_cb = mdc_replay_open;
759 if (!fid_is_sane(&body->fid1)) {
760 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
765 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
769 int mdc_clear_open_replay_data(struct obd_export *exp,
770 struct obd_client_handle *och)
772 struct md_open_data *mod = och->och_mod;
776 * It is possible to not have \var mod in a case of eviction between
777 * lookup and ll_file_open().
782 LASSERT(mod != LP_POISON);
791 /* Prepares the request for the replay by the given reply */
792 static void mdc_close_handle_reply(struct ptlrpc_request *req,
793 struct md_op_data *op_data, int rc) {
794 struct mdt_body *repbody;
795 struct mdt_ioepoch *epoch;
797 if (req && rc == -EAGAIN) {
798 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
799 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
801 epoch->flags |= MF_SOM_AU;
802 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
803 op_data->op_flags |= MF_GETATTR_LOCK;
807 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
808 struct md_open_data *mod, struct ptlrpc_request **request)
810 struct obd_device *obd = class_exp2obd(exp);
811 struct ptlrpc_request *req;
816 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
820 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
822 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
824 ptlrpc_request_free(req);
828 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
829 * portal whose threads are not taking any DLM locks and are therefore
830 * always progressing */
831 req->rq_request_portal = MDS_READPAGE_PORTAL;
832 ptlrpc_at_set_req_timeout(req);
834 /* Ensure that this close's handle is fixed up during replay. */
835 if (likely(mod != NULL)) {
836 LASSERTF(mod->mod_open_req != NULL &&
837 mod->mod_open_req->rq_type != LI_POISON,
838 "POISONED open %p!\n", mod->mod_open_req);
840 mod->mod_close_req = req;
842 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
843 /* We no longer want to preserve this open for replay even
844 * though the open was committed. b=3632, b=3633 */
845 cfs_spin_lock(&mod->mod_open_req->rq_lock);
846 mod->mod_open_req->rq_replay = 0;
847 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
849 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
852 mdc_close_pack(req, op_data);
854 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
855 obd->u.cli.cl_max_mds_easize);
856 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
857 obd->u.cli.cl_max_mds_cookiesize);
859 ptlrpc_request_set_replen(req);
861 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
862 rc = ptlrpc_queue_wait(req);
863 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
865 if (req->rq_repmsg == NULL) {
866 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
869 rc = req->rq_status ?: -EIO;
870 } else if (rc == 0 || rc == -EAGAIN) {
871 struct mdt_body *body;
873 rc = lustre_msg_get_status(req->rq_repmsg);
874 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
875 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
880 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
883 } else if (rc == -ESTALE) {
885 * it can be allowed error after 3633 if open was committed and
886 * server failed before close was sent. Let's check if mod
887 * exists and return no error in that case
890 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
891 LASSERT(mod->mod_open_req != NULL);
892 if (mod->mod_open_req->rq_committed)
899 mod->mod_close_req = NULL;
900 /* Since now, mod is accessed through open_req only,
901 * thus close req does not keep a reference on mod anymore. */
905 mdc_close_handle_reply(req, op_data, rc);
909 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
910 struct md_open_data *mod)
912 struct obd_device *obd = class_exp2obd(exp);
913 struct ptlrpc_request *req;
917 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
918 &RQF_MDS_DONE_WRITING);
922 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
923 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
925 ptlrpc_request_free(req);
930 LASSERTF(mod->mod_open_req != NULL &&
931 mod->mod_open_req->rq_type != LI_POISON,
932 "POISONED setattr %p!\n", mod->mod_open_req);
934 mod->mod_close_req = req;
935 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
936 /* We no longer want to preserve this setattr for replay even
937 * though the open was committed. b=3632, b=3633 */
938 cfs_spin_lock(&mod->mod_open_req->rq_lock);
939 mod->mod_open_req->rq_replay = 0;
940 cfs_spin_unlock(&mod->mod_open_req->rq_lock);
943 mdc_close_pack(req, op_data);
944 ptlrpc_request_set_replen(req);
946 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
947 rc = ptlrpc_queue_wait(req);
948 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
952 * it can be allowed error after 3633 if open or setattr were
953 * committed and server failed before close was sent.
954 * Let's check if mod exists and return no error in that case
957 LASSERT(mod->mod_open_req != NULL);
958 if (mod->mod_open_req->rq_committed)
965 mod->mod_close_req = NULL;
966 /* Since now, mod is accessed through setattr req only,
967 * thus DW req does not keep a reference on mod anymore. */
971 mdc_close_handle_reply(req, op_data, rc);
972 ptlrpc_req_finished(req);
976 #ifdef HAVE_SPLIT_SUPPORT
977 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
978 const struct page *page, int offset)
980 struct ptlrpc_request *req;
981 struct ptlrpc_bulk_desc *desc;
985 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
989 /* FIXME: capa doesn't support split yet */
990 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
992 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
994 ptlrpc_request_free(req);
998 req->rq_request_portal = MDS_READPAGE_PORTAL;
999 ptlrpc_at_set_req_timeout(req);
1001 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
1003 GOTO(out, rc = -ENOMEM);
1005 /* NB req now owns desc and will free it when it gets freed. */
1006 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1007 mdc_readdir_pack(req, 0, offset, fid, NULL);
1009 ptlrpc_request_set_replen(req);
1010 rc = ptlrpc_queue_wait(req);
1014 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1016 ptlrpc_req_finished(req);
1019 EXPORT_SYMBOL(mdc_sendpage);
1022 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
1023 struct obd_capa *oc, __u64 offset, struct page *page,
1024 struct ptlrpc_request **request)
1026 struct ptlrpc_request *req;
1027 struct ptlrpc_bulk_desc *desc;
1032 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1036 mdc_set_capa_size(req, &RMF_CAPA1, oc);
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, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1049 ptlrpc_request_free(req);
1053 /* NB req now owns desc and will free it when it gets freed */
1054 ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1055 mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
1057 ptlrpc_request_set_replen(req);
1058 rc = ptlrpc_queue_wait(req);
1060 ptlrpc_req_finished(req);
1064 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1065 req->rq_bulk->bd_nob_transferred);
1067 ptlrpc_req_finished(req);
1071 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1072 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1073 req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1074 ptlrpc_req_finished(req);
1082 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1083 __u64 max_age, __u32 flags)
1085 struct ptlrpc_request *req;
1086 struct obd_statfs *msfs;
1087 struct obd_import *imp = NULL;
1092 * Since the request might also come from lprocfs, so we need
1093 * sync this with client_disconnect_export Bug15684
1095 cfs_down_read(&obd->u.cli.cl_sem);
1096 if (obd->u.cli.cl_import)
1097 imp = class_import_get(obd->u.cli.cl_import);
1098 cfs_up_read(&obd->u.cli.cl_sem);
1102 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1103 LUSTRE_MDS_VERSION, MDS_STATFS);
1105 GOTO(output, rc = -ENOMEM);
1107 ptlrpc_request_set_replen(req);
1109 if (flags & OBD_STATFS_NODELAY) {
1110 /* procfs requests not want stay in wait for avoid deadlock */
1111 req->rq_no_resend = 1;
1112 req->rq_no_delay = 1;
1115 rc = ptlrpc_queue_wait(req);
1117 /* check connection error first */
1118 if (imp->imp_connect_error)
1119 rc = imp->imp_connect_error;
1123 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1125 GOTO(out, rc = -EPROTO);
1130 ptlrpc_req_finished(req);
1132 class_import_put(imp);
1136 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1138 __u32 keylen, vallen;
1142 if (gf->gf_pathlen > PATH_MAX)
1143 RETURN(-ENAMETOOLONG);
1144 if (gf->gf_pathlen < 2)
1147 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1148 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1149 OBD_ALLOC(key, keylen);
1152 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1153 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1155 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1156 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1158 if (!fid_is_sane(&gf->gf_fid))
1159 GOTO(out, rc = -EINVAL);
1161 /* Val is struct getinfo_fid2path result plus path */
1162 vallen = sizeof(*gf) + gf->gf_pathlen;
1164 rc = obd_get_info(exp, keylen, key, &vallen, gf, NULL);
1168 if (vallen <= sizeof(*gf))
1169 GOTO(out, rc = -EPROTO);
1170 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1171 GOTO(out, rc = -EOVERFLOW);
1173 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1174 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1177 OBD_FREE(key, keylen);
1181 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1183 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1185 LASSERT(len <= CR_MAXSIZE);
1187 lh->kuc_magic = KUC_MAGIC;
1188 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1189 lh->kuc_flags = flags;
1190 lh->kuc_msgtype = CL_RECORD;
1191 lh->kuc_msglen = len;
1195 #define D_CHANGELOG 0
1197 struct changelog_show {
1202 struct obd_device *cs_obd;
1205 static int changelog_show_cb(struct llog_handle *llh, struct llog_rec_hdr *hdr,
1208 struct changelog_show *cs = data;
1209 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1214 if ((rec->cr_hdr.lrh_type != CHANGELOG_REC) ||
1215 (rec->cr.cr_type >= CL_LAST)) {
1216 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
1221 if (rec->cr.cr_index < cs->cs_startrec) {
1222 /* Skip entries earlier than what we are interested in */
1223 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1224 rec->cr.cr_index, cs->cs_startrec);
1228 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1229 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1230 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1231 rec->cr.cr_flags & CLF_FLAGMASK,
1232 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1233 rec->cr.cr_namelen, rec->cr.cr_name);
1235 len = sizeof(*lh) + sizeof(rec->cr) + rec->cr.cr_namelen;
1237 /* Set up the message */
1238 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1239 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1241 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1242 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1247 static int mdc_changelog_send_thread(void *csdata)
1249 struct changelog_show *cs = csdata;
1250 struct llog_ctxt *ctxt = NULL;
1251 struct llog_handle *llh = NULL;
1252 struct kuc_hdr *kuch;
1255 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1256 cs->cs_fp, cs->cs_startrec);
1259 * It's important to daemonize here to close unused FDs.
1260 * The write fd from pipe is already opened by the caller,
1261 * so it's fine to clear all files here
1263 cfs_daemonize("mdc_clg_send_thread");
1265 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1266 if (cs->cs_buf == NULL)
1267 GOTO(out, rc = -ENOMEM);
1269 /* Set up the remote catalog handle */
1270 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1272 GOTO(out, rc = -ENOENT);
1273 rc = llog_create(ctxt, &llh, NULL, CHANGELOG_CATALOG);
1275 CERROR("llog_create() failed %d\n", rc);
1278 rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
1280 CERROR("llog_init_handle failed %d\n", rc);
1284 rc = llog_cat_process_flags(llh, changelog_show_cb, cs, 0, 0, 0);
1286 /* Send EOF no matter what our result */
1287 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1289 kuch->kuc_msgtype = CL_EOF;
1290 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1294 cfs_put_file(cs->cs_fp);
1298 llog_ctxt_put(ctxt);
1300 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1302 /* detach from parent process so we get cleaned up */
1303 cfs_daemonize("cl_send");
1307 static int mdc_ioc_changelog_send(struct obd_device *obd,
1308 struct ioc_changelog *icc)
1310 struct changelog_show *cs;
1313 /* Freed in mdc_changelog_send_thread */
1319 cs->cs_startrec = icc->icc_recno;
1320 /* matching cfs_put_file in mdc_changelog_send_thread */
1321 cs->cs_fp = cfs_get_fd(icc->icc_id);
1322 cs->cs_flags = icc->icc_flags;
1324 /* New thread because we should return to user app before
1325 writing into our pipe */
1326 rc = cfs_kernel_thread(mdc_changelog_send_thread, cs,
1327 CLONE_VM | CLONE_FILES);
1329 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1333 CERROR("Failed to start changelog thread: %d\n", rc);
1338 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1339 struct lustre_kernelcomm *lk);
1341 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1342 void *karg, void *uarg)
1344 struct obd_device *obd = exp->exp_obd;
1345 struct obd_ioctl_data *data = karg;
1346 struct obd_import *imp = obd->u.cli.cl_import;
1347 struct llog_ctxt *ctxt;
1351 if (!cfs_try_module_get(THIS_MODULE)) {
1352 CERROR("Can't get module. Is it alive?");
1356 case LL_IOC_HSM_CT_START:
1357 rc = mdc_ioc_hsm_ct_start(exp, karg);
1359 case OBD_IOC_CHANGELOG_SEND:
1360 rc = mdc_ioc_changelog_send(obd, karg);
1362 case OBD_IOC_CHANGELOG_CLEAR: {
1363 struct ioc_changelog *icc = karg;
1364 struct changelog_setinfo cs =
1365 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1366 rc = obd_set_info_async(exp, strlen(KEY_CHANGELOG_CLEAR),
1367 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1371 case OBD_IOC_FID2PATH: {
1372 rc = mdc_ioc_fid2path(exp, karg);
1375 case OBD_IOC_CLIENT_RECOVER:
1376 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1380 case IOC_OSC_SET_ACTIVE:
1381 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1383 case OBD_IOC_PARSE: {
1384 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1385 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1386 llog_ctxt_put(ctxt);
1390 case OBD_IOC_LLOG_INFO:
1391 case OBD_IOC_LLOG_PRINT: {
1392 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1393 rc = llog_ioctl(ctxt, cmd, data);
1394 llog_ctxt_put(ctxt);
1398 case OBD_IOC_POLL_QUOTACHECK:
1399 rc = lquota_poll_check(quota_interface, exp,
1400 (struct if_quotacheck *)karg);
1402 case OBD_IOC_PING_TARGET:
1403 rc = ptlrpc_obd_ping(obd);
1406 * Normally IOC_OBD_STATFS iocontrol is handled by LMV instead of MDC.
1407 * But when the cluster is upgraded from 1.8, there'd be no LMV layer
1408 * thus we might be called here. Eventually this code should be removed.
1411 case IOC_OBD_STATFS: {
1412 struct obd_statfs stat_buf = {0};
1414 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1415 GOTO(out, rc = -ENODEV);
1418 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1419 min((int) data->ioc_plen2,
1420 (int) sizeof(struct obd_uuid))))
1421 GOTO(out, rc = -EFAULT);
1423 rc = mdc_statfs(obd, &stat_buf,
1424 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1429 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1430 min((int) data->ioc_plen1,
1431 (int) sizeof(stat_buf))))
1432 GOTO(out, rc = -EFAULT);
1436 case LL_IOC_GET_CONNECT_FLAGS: {
1437 if (cfs_copy_to_user(uarg, &exp->exp_connect_flags,
1439 GOTO(out, rc = -EFAULT);
1444 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1445 GOTO(out, rc = -ENOTTY);
1448 cfs_module_put(THIS_MODULE);
1453 int mdc_get_info_rpc(struct obd_export *exp,
1454 obd_count keylen, void *key,
1455 int vallen, void *val)
1457 struct obd_import *imp = class_exp2cliimp(exp);
1458 struct ptlrpc_request *req;
1463 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1467 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1468 RCL_CLIENT, keylen);
1469 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1470 RCL_CLIENT, sizeof(__u32));
1472 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1474 ptlrpc_request_free(req);
1478 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1479 memcpy(tmp, key, keylen);
1480 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1481 memcpy(tmp, &vallen, sizeof(__u32));
1483 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1484 RCL_SERVER, vallen);
1485 ptlrpc_request_set_replen(req);
1487 rc = ptlrpc_queue_wait(req);
1489 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1490 memcpy(val, tmp, vallen);
1491 if (ptlrpc_rep_need_swab(req)) {
1492 if (KEY_IS(KEY_FID2PATH)) {
1493 lustre_swab_fid2path(val);
1497 ptlrpc_req_finished(req);
1502 static void lustre_swab_hai(struct hsm_action_item *h)
1504 __swab32s(&h->hai_len);
1505 __swab32s(&h->hai_action);
1506 lustre_swab_lu_fid(&h->hai_fid);
1507 __swab64s(&h->hai_cookie);
1508 __swab64s(&h->hai_extent.offset);
1509 __swab64s(&h->hai_extent.length);
1510 __swab64s(&h->hai_gid);
1513 static void lustre_swab_hal(struct hsm_action_list *h)
1515 struct hsm_action_item *hai;
1518 __swab32s(&h->hal_version);
1519 __swab32s(&h->hal_count);
1520 __swab32s(&h->hal_archive_num);
1522 for (i = 0; i < h->hal_count; i++) {
1523 lustre_swab_hai(hai);
1524 hai = hai_next(hai);
1528 static void lustre_swab_kuch(struct kuc_hdr *l)
1530 __swab16s(&l->kuc_magic);
1531 /* __u8 l->kuc_transport */
1532 __swab16s(&l->kuc_msgtype);
1533 __swab16s(&l->kuc_msglen);
1536 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1537 struct lustre_kernelcomm *lk)
1541 if (lk->lk_group != KUC_GRP_HSM) {
1542 CERROR("Bad copytool group %d\n", lk->lk_group);
1546 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1547 lk->lk_uid, lk->lk_group, lk->lk_flags);
1549 if (lk->lk_flags & LK_FLG_STOP)
1550 rc = libcfs_kkuc_group_rem(lk->lk_uid,lk->lk_group);
1552 cfs_file_t *fp = cfs_get_fd(lk->lk_wfd);
1553 rc = libcfs_kkuc_group_add(fp, lk->lk_uid,lk->lk_group,
1559 /* lk_data is archive number mask */
1560 /* TODO: register archive num with mdt so coordinator can choose
1567 * Send a message to any listening copytools
1568 * @param val KUC message (kuc_hdr + hsm_action_list)
1569 * @param len total length of message
1571 static int mdc_hsm_copytool_send(int len, void *val)
1573 struct kuc_hdr *lh = (struct kuc_hdr *)val;
1574 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1578 if (len < sizeof(*lh) + sizeof(*hal)) {
1579 CERROR("Short HSM message %d < %d\n", len,
1580 (int) (sizeof(*lh) + sizeof(*hal)));
1583 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1584 lustre_swab_kuch(lh);
1585 lustre_swab_hal(hal);
1586 } else if (lh->kuc_magic != KUC_MAGIC) {
1587 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
1591 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d\n",
1592 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
1593 lh->kuc_msglen, hal->hal_count);
1595 /* Broadcast to HSM listeners */
1596 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
1601 int mdc_set_info_async(struct obd_export *exp,
1602 obd_count keylen, void *key,
1603 obd_count vallen, void *val,
1604 struct ptlrpc_request_set *set)
1606 struct obd_import *imp = class_exp2cliimp(exp);
1610 if (KEY_IS(KEY_READ_ONLY)) {
1611 if (vallen != sizeof(int))
1614 cfs_spin_lock(&imp->imp_lock);
1615 if (*((int *)val)) {
1616 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1617 imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1619 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1620 imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1622 cfs_spin_unlock(&imp->imp_lock);
1624 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
1625 keylen, key, vallen, val, set);
1628 if (KEY_IS(KEY_SPTLRPC_CONF)) {
1629 sptlrpc_conf_client_adapt(exp->exp_obd);
1632 if (KEY_IS(KEY_FLUSH_CTX)) {
1633 sptlrpc_import_flush_my_ctx(imp);
1636 if (KEY_IS(KEY_MDS_CONN)) {
1637 /* mds-mds import */
1638 cfs_spin_lock(&imp->imp_lock);
1639 imp->imp_server_timeout = 1;
1640 cfs_spin_unlock(&imp->imp_lock);
1641 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1642 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1645 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1646 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
1647 keylen, key, vallen, val, set);
1650 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
1651 rc = mdc_hsm_copytool_send(vallen, val);
1658 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1659 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1663 if (KEY_IS(KEY_MAX_EASIZE)) {
1664 int mdsize, *max_easize;
1666 if (*vallen != sizeof(int))
1668 mdsize = *(int*)val;
1669 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1670 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1672 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1675 if (KEY_IS(KEY_CONN_DATA)) {
1676 struct obd_import *imp = class_exp2cliimp(exp);
1677 struct obd_connect_data *data = val;
1679 if (*vallen != sizeof(*data))
1682 *data = imp->imp_connect_data;
1686 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
1691 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1692 struct obd_capa *oc, struct obd_client_handle *handle,
1695 struct ptlrpc_request *req;
1696 struct mdt_body *body;
1700 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1704 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1706 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1708 ptlrpc_request_free(req);
1712 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1714 ptlrpc_request_set_replen(req);
1716 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1717 rc = ptlrpc_queue_wait(req);
1718 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1720 CERROR("Pin failed: %d\n", rc);
1724 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1726 GOTO(err_out, rc = -EPROTO);
1728 handle->och_fh = body->handle;
1729 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1731 handle->och_mod = obd_mod_alloc();
1732 if (handle->och_mod == NULL) {
1733 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1734 GOTO(err_out, rc = -ENOMEM);
1736 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1741 ptlrpc_req_finished(req);
1745 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1748 struct ptlrpc_request *req;
1749 struct mdt_body *body;
1753 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1754 LUSTRE_MDS_VERSION, MDS_UNPIN);
1758 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1759 body->handle = handle->och_fh;
1762 ptlrpc_request_set_replen(req);
1764 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1765 rc = ptlrpc_queue_wait(req);
1766 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1769 CERROR("Unpin failed: %d\n", rc);
1771 ptlrpc_req_finished(req);
1772 ptlrpc_req_finished(handle->och_mod->mod_open_req);
1774 obd_mod_put(handle->och_mod);
1778 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1779 struct obd_capa *oc, struct ptlrpc_request **request)
1781 struct ptlrpc_request *req;
1786 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1790 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1792 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1794 ptlrpc_request_free(req);
1798 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1800 ptlrpc_request_set_replen(req);
1802 rc = ptlrpc_queue_wait(req);
1804 ptlrpc_req_finished(req);
1810 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1811 enum obd_import_event event)
1815 LASSERT(imp->imp_obd == obd);
1818 case IMP_EVENT_DISCON: {
1820 /* XXX Pass event up to OBDs stack. used only for FLD now */
1821 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1825 case IMP_EVENT_INACTIVE: {
1826 struct client_obd *cli = &obd->u.cli;
1828 * Flush current sequence to make client obtain new one
1829 * from server in case of disconnect/reconnect.
1831 if (cli->cl_seq != NULL)
1832 seq_client_flush(cli->cl_seq);
1834 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1837 case IMP_EVENT_INVALIDATE: {
1838 struct ldlm_namespace *ns = obd->obd_namespace;
1840 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1844 case IMP_EVENT_ACTIVE: {
1845 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1849 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1851 case IMP_EVENT_DEACTIVATE:
1852 case IMP_EVENT_ACTIVATE:
1855 CERROR("Unknown import event %x\n", event);
1861 static int mdc_fid_init(struct obd_export *exp)
1863 struct client_obd *cli = &exp->exp_obd->u.cli;
1868 OBD_ALLOC_PTR(cli->cl_seq);
1869 if (cli->cl_seq == NULL)
1872 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1874 GOTO(out_free_seq, rc = -ENOMEM);
1876 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1877 exp->exp_obd->obd_name);
1879 /* Init client side sequence-manager */
1880 rc = seq_client_init(cli->cl_seq, exp,
1881 LUSTRE_SEQ_METADATA,
1883 OBD_FREE(prefix, MAX_OBD_NAME + 5);
1885 GOTO(out_free_seq, rc);
1889 OBD_FREE_PTR(cli->cl_seq);
1894 static int mdc_fid_fini(struct obd_export *exp)
1896 struct client_obd *cli = &exp->exp_obd->u.cli;
1899 if (cli->cl_seq != NULL) {
1900 seq_client_fini(cli->cl_seq);
1901 OBD_FREE_PTR(cli->cl_seq);
1908 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1909 struct md_op_data *op_data)
1911 struct client_obd *cli = &exp->exp_obd->u.cli;
1912 struct lu_client_seq *seq = cli->cl_seq;
1914 RETURN(seq_client_alloc_fid(seq, fid));
1917 /* XXX This method is used only to clear current fid seq
1918 * once fld/mds insert failed */
1919 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1921 struct client_obd *cli = &exp->exp_obd->u.cli;
1923 seq_client_flush(cli->cl_seq);
1927 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
1928 struct client_obd *cli = &exp->exp_obd->u.cli;
1929 return &cli->cl_target_uuid;
1933 * Determine whether the lock can be canceled before replaying it during
1934 * recovery, non zero value will be return if the lock can be canceled,
1935 * or zero returned for not
1937 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
1939 if (lock->l_resource->lr_type != LDLM_IBITS)
1942 /* FIXME: if we ever get into a situation where there are too many
1943 * opened files with open locks on a single node, then we really
1944 * should replay these open locks to reget it */
1945 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
1951 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1953 struct client_obd *cli = &obd->u.cli;
1954 struct lprocfs_static_vars lvars = { 0 };
1958 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1959 if (!cli->cl_rpc_lock)
1961 mdc_init_rpc_lock(cli->cl_rpc_lock);
1965 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1966 if (!cli->cl_setattr_lock)
1967 GOTO(err_rpc_lock, rc = -ENOMEM);
1968 mdc_init_rpc_lock(cli->cl_setattr_lock);
1970 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1971 if (!cli->cl_close_lock)
1972 GOTO(err_setattr_lock, rc = -ENOMEM);
1973 mdc_init_rpc_lock(cli->cl_close_lock);
1975 rc = client_obd_setup(obd, cfg);
1977 GOTO(err_close_lock, rc);
1978 lprocfs_mdc_init_vars(&lvars);
1979 lprocfs_obd_setup(obd, lvars.obd_vars);
1980 sptlrpc_lprocfs_cliobd_attach(obd);
1981 ptlrpc_lprocfs_register_obd(obd);
1983 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
1985 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
1988 CERROR("failed to setup llogging subsystems\n");
1994 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1996 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1998 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2003 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2004 * us to make MDS RPCs with large enough reply buffers to hold the
2005 * maximum-sized (= maximum striped) EA and cookie without having to
2006 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2007 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2008 int def_easize, int cookiesize)
2010 struct obd_device *obd = exp->exp_obd;
2011 struct client_obd *cli = &obd->u.cli;
2014 if (cli->cl_max_mds_easize < easize)
2015 cli->cl_max_mds_easize = easize;
2017 if (cli->cl_default_mds_easize < def_easize)
2018 cli->cl_default_mds_easize = def_easize;
2020 if (cli->cl_max_mds_cookiesize < cookiesize)
2021 cli->cl_max_mds_cookiesize = cookiesize;
2026 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2032 case OBD_CLEANUP_EARLY:
2034 case OBD_CLEANUP_EXPORTS:
2035 /* Failsafe, ok if racy */
2036 if (obd->obd_type->typ_refcnt <= 1)
2037 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2039 /* If we set up but never connected, the
2040 client import will not have been cleaned. */
2041 if (obd->u.cli.cl_import) {
2042 struct obd_import *imp;
2043 cfs_down_write(&obd->u.cli.cl_sem);
2044 imp = obd->u.cli.cl_import;
2045 CERROR("client import never connected\n");
2046 ptlrpc_invalidate_import(imp);
2047 class_destroy_import(imp);
2048 cfs_up_write(&obd->u.cli.cl_sem);
2049 obd->u.cli.cl_import = NULL;
2051 rc = obd_llog_finish(obd, 0);
2053 CERROR("failed to cleanup llogging subsystems\n");
2059 static int mdc_cleanup(struct obd_device *obd)
2061 struct client_obd *cli = &obd->u.cli;
2063 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2064 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
2065 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2067 ptlrpc_lprocfs_unregister_obd(obd);
2068 lprocfs_obd_cleanup(obd);
2071 return client_obd_cleanup(obd);
2075 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2076 struct obd_device *tgt, int *index)
2078 struct llog_ctxt *ctxt;
2082 LASSERT(olg == &obd->obd_olg);
2084 rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
2089 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
2090 llog_initiator_connect(ctxt);
2091 llog_ctxt_put(ctxt);
2093 rc = llog_setup(obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt, 0, NULL,
2096 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2097 llog_initiator_connect(ctxt);
2098 llog_ctxt_put(ctxt);
2104 static int mdc_llog_finish(struct obd_device *obd, int count)
2106 struct llog_ctxt *ctxt;
2110 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
2112 rc = llog_cleanup(ctxt);
2114 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2116 rc = llog_cleanup(ctxt);
2121 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2123 struct lustre_cfg *lcfg = buf;
2124 struct lprocfs_static_vars lvars = { 0 };
2127 lprocfs_mdc_init_vars(&lvars);
2128 switch (lcfg->lcfg_command) {
2130 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2140 /* get remote permission for current user on fid */
2141 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2142 struct obd_capa *oc, __u32 suppgid,
2143 struct ptlrpc_request **request)
2145 struct ptlrpc_request *req;
2149 LASSERT(client_is_remote(exp));
2152 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2156 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2158 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2160 ptlrpc_request_free(req);
2164 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2166 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2167 sizeof(struct mdt_remote_perm));
2169 ptlrpc_request_set_replen(req);
2171 rc = ptlrpc_queue_wait(req);
2173 ptlrpc_req_finished(req);
2179 static int mdc_interpret_renew_capa(const struct lu_env *env,
2180 struct ptlrpc_request *req, void *args,
2183 struct mdc_renew_capa_args *ra = args;
2184 struct mdt_body *body = NULL;
2185 struct lustre_capa *capa;
2189 GOTO(out, capa = ERR_PTR(status));
2191 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2193 GOTO(out, capa = ERR_PTR(-EFAULT));
2195 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2196 GOTO(out, capa = ERR_PTR(-ENOENT));
2198 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2200 GOTO(out, capa = ERR_PTR(-EFAULT));
2203 ra->ra_cb(ra->ra_oc, capa);
2207 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2210 struct ptlrpc_request *req;
2211 struct mdc_renew_capa_args *ra;
2214 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2215 LUSTRE_MDS_VERSION, MDS_GETATTR);
2219 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2220 * capa to renew is oss capa.
2222 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2223 ptlrpc_request_set_replen(req);
2225 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2226 ra = ptlrpc_req_async_args(req);
2229 req->rq_interpret_reply = mdc_interpret_renew_capa;
2230 ptlrpcd_add_req(req, PSCOPE_OTHER);
2234 static int mdc_connect(const struct lu_env *env,
2235 struct obd_export **exp,
2236 struct obd_device *obd, struct obd_uuid *cluuid,
2237 struct obd_connect_data *data,
2240 struct obd_import *imp = obd->u.cli.cl_import;
2242 /* mds-mds import features */
2243 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2244 cfs_spin_lock(&imp->imp_lock);
2245 imp->imp_server_timeout = 1;
2246 cfs_spin_unlock(&imp->imp_lock);
2247 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2248 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2252 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2255 struct obd_ops mdc_obd_ops = {
2256 .o_owner = THIS_MODULE,
2257 .o_setup = mdc_setup,
2258 .o_precleanup = mdc_precleanup,
2259 .o_cleanup = mdc_cleanup,
2260 .o_add_conn = client_import_add_conn,
2261 .o_del_conn = client_import_del_conn,
2262 .o_connect = mdc_connect,
2263 .o_disconnect = client_disconnect_export,
2264 .o_iocontrol = mdc_iocontrol,
2265 .o_set_info_async = mdc_set_info_async,
2266 .o_statfs = mdc_statfs,
2268 .o_unpin = mdc_unpin,
2269 .o_fid_init = mdc_fid_init,
2270 .o_fid_fini = mdc_fid_fini,
2271 .o_fid_alloc = mdc_fid_alloc,
2272 .o_fid_delete = mdc_fid_delete,
2273 .o_import_event = mdc_import_event,
2274 .o_llog_init = mdc_llog_init,
2275 .o_llog_finish = mdc_llog_finish,
2276 .o_get_info = mdc_get_info,
2277 .o_process_config = mdc_process_config,
2278 .o_get_uuid = mdc_get_uuid,
2281 struct md_ops mdc_md_ops = {
2282 .m_getstatus = mdc_getstatus,
2283 .m_change_cbdata = mdc_change_cbdata,
2284 .m_find_cbdata = mdc_find_cbdata,
2285 .m_close = mdc_close,
2286 .m_create = mdc_create,
2287 .m_done_writing = mdc_done_writing,
2288 .m_enqueue = mdc_enqueue,
2289 .m_getattr = mdc_getattr,
2290 .m_getattr_name = mdc_getattr_name,
2291 .m_intent_lock = mdc_intent_lock,
2293 .m_is_subdir = mdc_is_subdir,
2294 .m_rename = mdc_rename,
2295 .m_setattr = mdc_setattr,
2296 .m_setxattr = mdc_setxattr,
2297 .m_getxattr = mdc_getxattr,
2299 .m_readpage = mdc_readpage,
2300 .m_unlink = mdc_unlink,
2301 .m_cancel_unused = mdc_cancel_unused,
2302 .m_init_ea_size = mdc_init_ea_size,
2303 .m_set_lock_data = mdc_set_lock_data,
2304 .m_lock_match = mdc_lock_match,
2305 .m_get_lustre_md = mdc_get_lustre_md,
2306 .m_free_lustre_md = mdc_free_lustre_md,
2307 .m_set_open_replay_data = mdc_set_open_replay_data,
2308 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2309 .m_renew_capa = mdc_renew_capa,
2310 .m_unpack_capa = mdc_unpack_capa,
2311 .m_get_remote_perm = mdc_get_remote_perm,
2312 .m_intent_getattr_async = mdc_intent_getattr_async,
2313 .m_revalidate_lock = mdc_revalidate_lock
2316 int __init mdc_init(void)
2319 struct lprocfs_static_vars lvars = { 0 };
2320 lprocfs_mdc_init_vars(&lvars);
2322 cfs_request_module("lquota");
2323 quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
2324 init_obd_quota_ops(quota_interface, &mdc_obd_ops);
2326 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2327 LUSTRE_MDC_NAME, NULL);
2328 if (rc && quota_interface)
2329 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2335 static void /*__exit*/ mdc_exit(void)
2337 if (quota_interface)
2338 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2340 class_unregister_type(LUSTRE_MDC_NAME);
2343 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2344 MODULE_DESCRIPTION("Lustre Metadata Client");
2345 MODULE_LICENSE("GPL");
2347 module_init(mdc_init);
2348 module_exit(mdc_exit);