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, const struct lu_fid *fid,
206 struct obd_capa *oc, obd_valid valid, int ea_size,
207 struct ptlrpc_request **request)
209 struct ptlrpc_request *req;
214 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
218 mdc_set_capa_size(req, &RMF_CAPA1, oc);
220 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
222 ptlrpc_request_free(req);
226 /* MDS_BFLAG_EXT_FLAGS: request "new" flags(bug 9486) */
227 mdc_pack_body(req, fid, oc, valid, ea_size, -1, MDS_BFLAG_EXT_FLAGS);
229 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER, ea_size);
230 if (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, const struct lu_fid *fid,
246 struct obd_capa *oc, const char *filename, int namelen,
247 obd_valid valid, int ea_size, __u32 suppgid,
248 struct ptlrpc_request **request)
250 struct ptlrpc_request *req;
255 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
256 &RQF_MDS_GETATTR_NAME);
260 mdc_set_capa_size(req, &RMF_CAPA1, oc);
261 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT, namelen);
263 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
265 ptlrpc_request_free(req);
269 /* MDS_BFLAG_EXT_FLAGS: request "new" flags(bug 9486) */
270 mdc_pack_body(req, fid, oc, valid, ea_size, suppgid,
271 MDS_BFLAG_EXT_FLAGS);
274 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
275 LASSERT(strnlen(filename, namelen) == namelen - 1);
276 memcpy(name, filename, namelen);
279 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER, ea_size);
280 ptlrpc_request_set_replen(req);
282 rc = mdc_getattr_common(exp, req);
284 ptlrpc_req_finished(req);
290 static int mdc_is_subdir(struct obd_export *exp,
291 const struct lu_fid *pfid,
292 const struct lu_fid *cfid,
293 struct ptlrpc_request **request)
295 struct ptlrpc_request *req;
301 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
302 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
307 mdc_is_subdir_pack(req, pfid, cfid, 0);
308 ptlrpc_request_set_replen(req);
310 rc = ptlrpc_queue_wait(req);
311 if (rc && rc != -EREMOTE)
312 ptlrpc_req_finished(req);
318 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
319 const struct lu_fid *fid,
320 struct obd_capa *oc, int opcode, obd_valid valid,
321 const char *xattr_name, const char *input,
322 int input_size, int output_size, int flags,
323 __u32 suppgid, struct ptlrpc_request **request)
325 struct ptlrpc_request *req;
326 int xattr_namelen = 0;
332 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
336 mdc_set_capa_size(req, &RMF_CAPA1, oc);
338 xattr_namelen = strlen(xattr_name) + 1;
339 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
344 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
348 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
350 ptlrpc_request_free(req);
354 if (opcode == MDS_REINT) {
355 struct mdt_rec_setxattr *rec;
357 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
358 sizeof(struct mdt_rec_reint));
359 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
360 rec->sx_opcode = REINT_SETXATTR;
362 * cfs_curproc_fs{u,g}id() should replace
363 * current->fs{u,g}id for portability.
365 rec->sx_fsuid = current->fsuid;
366 rec->sx_fsgid = current->fsgid;
367 rec->sx_cap = cfs_curproc_cap_pack();
368 rec->sx_suppgid1 = suppgid;
369 rec->sx_suppgid2 = -1;
371 rec->sx_valid = valid | OBD_MD_FLCTIME;
372 rec->sx_time = cfs_time_current_sec();
373 rec->sx_size = output_size;
374 rec->sx_flags = flags;
376 mdc_pack_capa(req, &RMF_CAPA1, oc);
378 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
382 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
383 memcpy(tmp, xattr_name, xattr_namelen);
386 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
387 memcpy(tmp, input, input_size);
390 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
391 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
392 RCL_SERVER, output_size);
393 ptlrpc_request_set_replen(req);
396 if (opcode == MDS_REINT)
397 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
399 rc = ptlrpc_queue_wait(req);
401 if (opcode == MDS_REINT)
402 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
405 ptlrpc_req_finished(req);
411 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
412 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
413 const char *input, int input_size, int output_size,
414 int flags, __u32 suppgid, struct ptlrpc_request **request)
416 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
417 fid, oc, MDS_REINT, valid, xattr_name,
418 input, input_size, output_size, flags,
422 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
423 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
424 const char *input, int input_size, int output_size,
425 int flags, struct ptlrpc_request **request)
427 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
428 fid, oc, MDS_GETXATTR, valid, xattr_name,
429 input, input_size, output_size, flags,
433 #ifdef CONFIG_FS_POSIX_ACL
434 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
436 struct req_capsule *pill = &req->rq_pill;
437 struct mdt_body *body = md->body;
438 struct posix_acl *acl;
446 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
451 acl = posix_acl_from_xattr(buf, body->aclsize);
454 CERROR("convert xattr to acl: %d\n", rc);
458 rc = posix_acl_valid(acl);
460 CERROR("validate acl: %d\n", rc);
461 posix_acl_release(acl);
469 #define mdc_unpack_acl(req, md) 0
472 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
473 struct obd_export *dt_exp, struct obd_export *md_exp,
474 struct lustre_md *md)
476 struct req_capsule *pill = &req->rq_pill;
481 memset(md, 0, sizeof(*md));
483 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
484 LASSERT(md->body != NULL);
486 if (md->body->valid & OBD_MD_FLEASIZE) {
488 struct lov_mds_md *lmm;
490 if (!S_ISREG(md->body->mode)) {
491 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
492 "regular file, but is not\n");
493 GOTO(out, rc = -EPROTO);
496 if (md->body->eadatasize == 0) {
497 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
498 "but eadatasize 0\n");
499 GOTO(out, rc = -EPROTO);
501 lmmsize = md->body->eadatasize;
502 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
504 GOTO(out, rc = -EPROTO);
506 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
510 if (rc < sizeof(*md->lsm)) {
511 CDEBUG(D_INFO, "lsm size too small: "
512 "rc < sizeof (*md->lsm) (%d < %d)\n",
513 rc, (int)sizeof(*md->lsm));
514 GOTO(out, rc = -EPROTO);
517 } else if (md->body->valid & OBD_MD_FLDIREA) {
519 struct lov_mds_md *lmv;
521 if(!S_ISDIR(md->body->mode)) {
522 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
523 "directory, but is not\n");
524 GOTO(out, rc = -EPROTO);
527 if (md->body->eadatasize == 0) {
528 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
529 "but eadatasize 0\n");
532 if (md->body->valid & OBD_MD_MEA) {
533 lmvsize = md->body->eadatasize;
534 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
537 GOTO(out, rc = -EPROTO);
539 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
544 if (rc < sizeof(*md->mea)) {
545 CDEBUG(D_INFO, "size too small: "
546 "rc < sizeof(*md->mea) (%d < %d)\n",
547 rc, (int)sizeof(*md->mea));
548 GOTO(out, rc = -EPROTO);
554 if (md->body->valid & OBD_MD_FLRMTPERM) {
555 /* remote permission */
556 LASSERT(client_is_remote(exp));
557 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
558 lustre_swab_mdt_remote_perm);
559 if (!md->remote_perm)
560 GOTO(out, rc = -EPROTO);
562 else if (md->body->valid & OBD_MD_FLACL) {
563 /* for ACL, it's possible that FLACL is set but aclsize is zero.
564 * only when aclsize != 0 there's an actual segment for ACL
567 if (md->body->aclsize) {
568 rc = mdc_unpack_acl(req, md);
571 #ifdef CONFIG_FS_POSIX_ACL
573 md->posix_acl = NULL;
577 if (md->body->valid & OBD_MD_FLMDSCAPA) {
578 struct obd_capa *oc = NULL;
580 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
586 if (md->body->valid & OBD_MD_FLOSSCAPA) {
587 struct obd_capa *oc = NULL;
589 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
599 capa_put(md->oss_capa);
603 capa_put(md->mds_capa);
606 #ifdef CONFIG_FS_POSIX_ACL
607 posix_acl_release(md->posix_acl);
610 obd_free_memmd(dt_exp, &md->lsm);
615 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
622 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
625 void mdc_replay_open(struct ptlrpc_request *req)
627 struct md_open_data *mod = req->rq_cb_data;
628 struct ptlrpc_request *close_req;
629 struct obd_client_handle *och;
630 struct lustre_handle old;
631 struct mdt_body *body;
635 DEBUG_REQ(D_ERROR, req,
636 "Can't properly replay without open data.");
641 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
642 LASSERT(body != NULL);
646 struct lustre_handle *file_fh;
648 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
650 file_fh = &och->och_fh;
651 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
652 file_fh->cookie, body->handle.cookie);
654 *file_fh = body->handle;
656 close_req = mod->mod_close_req;
657 if (close_req != NULL) {
658 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
659 struct mdt_ioepoch *epoch;
661 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
662 epoch = req_capsule_client_get(&close_req->rq_pill,
667 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
668 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
669 epoch->handle = body->handle;
674 void mdc_commit_open(struct ptlrpc_request *req)
676 struct md_open_data *mod = req->rq_cb_data;
681 * No need to touch md_open_data::mod_och, it holds a reference on
682 * \var mod and will zero references to each other, \var mod will be
683 * freed after that when md_open_data::mod_och will put the reference.
687 * Do not let open request to disappear as it still may be needed
688 * for close rpc to happen (it may happen on evict only, otherwise
689 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
690 * called), just mark this rpc as committed to distinguish these 2
691 * cases, see mdc_close() for details. The open request reference will
692 * be put along with freeing \var mod.
694 ptlrpc_request_addref(req);
695 spin_lock(&req->rq_lock);
696 req->rq_committed = 1;
697 spin_unlock(&req->rq_lock);
698 req->rq_cb_data = NULL;
702 int mdc_set_open_replay_data(struct obd_export *exp,
703 struct obd_client_handle *och,
704 struct ptlrpc_request *open_req)
706 struct md_open_data *mod;
707 struct mdt_rec_create *rec;
708 struct mdt_body *body;
709 struct obd_import *imp = open_req->rq_import;
712 if (!open_req->rq_replay)
715 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
716 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
717 LASSERT(rec != NULL);
718 /* Incoming message in my byte order (it's been swabbed). */
719 /* Outgoing messages always in my byte order. */
720 LASSERT(body != NULL);
722 /* Only if the import is replayable, we set replay_open data */
723 if (och && imp->imp_replayable) {
724 mod = obd_mod_alloc();
726 DEBUG_REQ(D_ERROR, open_req,
727 "Can't allocate md_open_data");
732 * Take a reference on \var mod, to be freed on mdc_close().
733 * It protects \var mod from being freed on eviction (commit
734 * callback is called despite rq_replay flag).
735 * Another reference for \var och.
740 spin_lock(&open_req->rq_lock);
743 mod->mod_open_req = open_req;
744 open_req->rq_cb_data = mod;
745 open_req->rq_commit_cb = mdc_commit_open;
746 spin_unlock(&open_req->rq_lock);
749 rec->cr_fid2 = body->fid1;
750 rec->cr_ioepoch = body->ioepoch;
751 rec->cr_old_handle.cookie = body->handle.cookie;
752 open_req->rq_replay_cb = mdc_replay_open;
753 if (!fid_is_sane(&body->fid1)) {
754 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
759 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
763 int mdc_clear_open_replay_data(struct obd_export *exp,
764 struct obd_client_handle *och)
766 struct md_open_data *mod = och->och_mod;
769 LASSERT(mod != LP_POISON && mod != NULL);
778 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
779 struct md_open_data *mod, struct ptlrpc_request **request)
781 struct obd_device *obd = class_exp2obd(exp);
782 struct ptlrpc_request *req;
787 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
791 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
793 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
795 ptlrpc_request_free(req);
799 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
800 * portal whose threads are not taking any DLM locks and are therefore
801 * always progressing */
802 req->rq_request_portal = MDS_READPAGE_PORTAL;
803 ptlrpc_at_set_req_timeout(req);
805 /* Ensure that this close's handle is fixed up during replay. */
806 if (likely(mod != NULL)) {
807 LASSERTF(mod->mod_open_req != NULL &&
808 mod->mod_open_req->rq_type != LI_POISON,
809 "POISONED open %p!\n", mod->mod_open_req);
811 mod->mod_close_req = req;
813 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
814 /* We no longer want to preserve this open for replay even
815 * though the open was committed. b=3632, b=3633 */
816 spin_lock(&mod->mod_open_req->rq_lock);
817 mod->mod_open_req->rq_replay = 0;
818 spin_unlock(&mod->mod_open_req->rq_lock);
820 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
823 mdc_close_pack(req, op_data);
825 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
826 obd->u.cli.cl_max_mds_easize);
827 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
828 obd->u.cli.cl_max_mds_cookiesize);
830 ptlrpc_request_set_replen(req);
832 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
833 rc = ptlrpc_queue_wait(req);
834 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
836 if (req->rq_repmsg == NULL) {
837 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
840 rc = req->rq_status ?: -EIO;
841 } else if (rc == 0 || rc == -EAGAIN) {
842 struct mdt_body *body;
844 rc = lustre_msg_get_status(req->rq_repmsg);
845 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
846 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
851 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
854 } else if (rc == -ESTALE) {
856 * it can be allowed error after 3633 if open was committed and
857 * server failed before close was sent. Let's check if mod
858 * exists and return no error in that case
861 LASSERT(mod->mod_open_req != NULL);
862 if (mod->mod_open_req->rq_committed)
869 mod->mod_close_req = NULL;
870 /* Since now, mod is accessed through open_req only,
871 * thus close req does not keep a reference on mod anymore. */
878 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
879 struct md_open_data *mod)
881 struct obd_device *obd = class_exp2obd(exp);
882 struct ptlrpc_request *req;
886 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
887 &RQF_MDS_DONE_WRITING);
891 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
892 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
894 ptlrpc_request_free(req);
899 LASSERTF(mod->mod_open_req != NULL &&
900 mod->mod_open_req->rq_type != LI_POISON,
901 "POISONED setattr %p!\n", mod->mod_open_req);
903 mod->mod_close_req = req;
904 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
905 /* We no longer want to preserve this setattr for replay even
906 * though the open was committed. b=3632, b=3633 */
907 spin_lock(&mod->mod_open_req->rq_lock);
908 mod->mod_open_req->rq_replay = 0;
909 spin_unlock(&mod->mod_open_req->rq_lock);
912 mdc_close_pack(req, op_data);
913 ptlrpc_request_set_replen(req);
915 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
916 rc = ptlrpc_queue_wait(req);
917 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
921 * it can be allowed error after 3633 if open or setattr were
922 * committed and server failed before close was sent.
923 * Let's check if mod exists and return no error in that case
926 LASSERT(mod->mod_open_req != NULL);
927 if (mod->mod_open_req->rq_committed)
934 mod->mod_close_req = NULL;
935 /* Since now, mod is accessed through setattr req only,
936 * thus DW req does not keep a reference on mod anymore. */
939 ptlrpc_req_finished(req);
943 #ifdef HAVE_SPLIT_SUPPORT
944 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
945 const struct page *page, int offset)
947 struct ptlrpc_request *req;
948 struct ptlrpc_bulk_desc *desc;
952 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
956 /* FIXME: capa doesn't support split yet */
957 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
959 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
961 ptlrpc_request_free(req);
965 req->rq_request_portal = MDS_READPAGE_PORTAL;
966 ptlrpc_at_set_req_timeout(req);
968 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_GET_SOURCE, MDS_BULK_PORTAL);
970 GOTO(out, rc = -ENOMEM);
972 /* NB req now owns desc and will free it when it gets freed. */
973 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
974 mdc_readdir_pack(req, 0, offset, fid, NULL);
976 ptlrpc_request_set_replen(req);
977 rc = ptlrpc_queue_wait(req);
981 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
983 ptlrpc_req_finished(req);
986 EXPORT_SYMBOL(mdc_sendpage);
989 int mdc_readpage(struct obd_export *exp, const struct lu_fid *fid,
990 struct obd_capa *oc, __u64 offset, struct page *page,
991 struct ptlrpc_request **request)
993 struct ptlrpc_request *req;
994 struct ptlrpc_bulk_desc *desc;
999 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1003 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1005 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1007 ptlrpc_request_free(req);
1011 req->rq_request_portal = MDS_READPAGE_PORTAL;
1012 ptlrpc_at_set_req_timeout(req);
1014 desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
1016 ptlrpc_request_free(req);
1020 /* NB req now owns desc and will free it when it gets freed */
1021 ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
1022 mdc_readdir_pack(req, offset, CFS_PAGE_SIZE, fid, oc);
1024 ptlrpc_request_set_replen(req);
1025 rc = ptlrpc_queue_wait(req);
1027 ptlrpc_req_finished(req);
1031 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1032 req->rq_bulk->bd_nob_transferred);
1034 ptlrpc_req_finished(req);
1038 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
1039 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1040 req->rq_bulk->bd_nob_transferred, CFS_PAGE_SIZE);
1041 ptlrpc_req_finished(req);
1049 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1051 __u32 keylen, vallen;
1055 if (gf->gf_pathlen > PATH_MAX)
1056 RETURN(-ENAMETOOLONG);
1057 if (gf->gf_pathlen < 2)
1060 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1061 keylen = size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1062 OBD_ALLOC(key, keylen);
1065 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1066 memcpy(key + size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1068 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1069 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1071 if (!fid_is_sane(&gf->gf_fid))
1072 GOTO(out, rc = -EINVAL);
1074 /* Val is struct getinfo_fid2path result plus path */
1075 vallen = sizeof(*gf) + gf->gf_pathlen;
1077 rc = obd_get_info(exp, keylen, key, &vallen, gf, NULL);
1081 if (vallen <= sizeof(*gf))
1082 GOTO(out, rc = -EPROTO);
1083 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1084 GOTO(out, rc = -EOVERFLOW);
1086 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1087 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1090 OBD_FREE(key, keylen);
1094 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1095 void *karg, void *uarg)
1097 struct obd_device *obd = exp->exp_obd;
1098 struct obd_ioctl_data *data = karg;
1099 struct obd_import *imp = obd->u.cli.cl_import;
1100 struct llog_ctxt *ctxt;
1104 if (!try_module_get(THIS_MODULE)) {
1105 CERROR("Can't get module. Is it alive?");
1109 case OBD_IOC_CHANGELOG_CLEAR: {
1110 struct ioc_changelog_clear *icc = karg;
1111 struct changelog_setinfo cs =
1112 {icc->icc_recno, icc->icc_id};
1113 rc = obd_set_info_async(exp, strlen(KEY_CHANGELOG_CLEAR),
1114 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1118 case OBD_IOC_FID2PATH: {
1119 rc = mdc_ioc_fid2path(exp, karg);
1122 case OBD_IOC_CLIENT_RECOVER:
1123 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1127 case IOC_OSC_SET_ACTIVE:
1128 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1130 case OBD_IOC_PARSE: {
1131 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1132 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1133 llog_ctxt_put(ctxt);
1137 case OBD_IOC_LLOG_INFO:
1138 case OBD_IOC_LLOG_PRINT: {
1139 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1140 rc = llog_ioctl(ctxt, cmd, data);
1141 llog_ctxt_put(ctxt);
1145 case OBD_IOC_POLL_QUOTACHECK:
1146 rc = lquota_poll_check(quota_interface, exp,
1147 (struct if_quotacheck *)karg);
1149 case OBD_IOC_PING_TARGET:
1150 rc = ptlrpc_obd_ping(obd);
1153 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1154 GOTO(out, rc = -ENOTTY);
1157 module_put(THIS_MODULE);
1162 int mdc_get_info_rpc(struct obd_export *exp,
1163 obd_count keylen, void *key,
1164 int vallen, void *val)
1166 struct obd_import *imp = class_exp2cliimp(exp);
1167 struct ptlrpc_request *req;
1172 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1176 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1177 RCL_CLIENT, keylen);
1178 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1179 RCL_CLIENT, sizeof(__u32));
1181 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1183 ptlrpc_request_free(req);
1187 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1188 memcpy(tmp, key, keylen);
1189 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1190 memcpy(tmp, &vallen, sizeof(__u32));
1192 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1193 RCL_SERVER, vallen);
1194 ptlrpc_request_set_replen(req);
1196 rc = ptlrpc_queue_wait(req);
1198 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1199 memcpy(val, tmp, vallen);
1200 if (ptlrpc_rep_need_swab(req)) {
1201 if (KEY_IS(KEY_FID2PATH)) {
1202 lustre_swab_fid2path(val);
1206 ptlrpc_req_finished(req);
1211 static void lustre_swab_hai(struct hsm_action_item *h)
1213 __swab32s(&h->hai_len);
1214 __swab32s(&h->hai_action);
1215 lustre_swab_lu_fid(&h->hai_fid);
1216 __swab64s(&h->hai_cookie);
1217 __swab64s(&h->hai_extent_start);
1218 __swab64s(&h->hai_extent_end);
1219 __swab64s(&h->hai_gid);
1222 static void lustre_swab_hal(struct hsm_action_list *h)
1224 struct hsm_action_item *hai;
1227 __swab32s(&h->hal_version);
1228 __swab32s(&h->hal_count);
1229 __swab32s(&h->hal_archive_num);
1231 for (i = 0; i < h->hal_count; i++) {
1232 lustre_swab_hai(hai);
1233 hai = hai_next(hai);
1238 * Send a message to any listening copytools, nonblocking
1239 * @param val LNL message (lnl_hdr + hsm_action_list)
1240 * @param len total length of message
1242 static int mdc_hsm_copytool_send(int len, void *val)
1244 struct lnl_hdr *lh = (struct lnl_hdr *)val;
1245 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1249 if (len < sizeof(*lh) + sizeof(*hal)) {
1250 CERROR("Short HSM message %d < %d\n", len,
1251 (int) (sizeof(*lh) + sizeof(*hal)));
1254 if (lh->lnl_magic == __swab16(LNL_MAGIC)) {
1255 lustre_swab_lnlh(lh);
1256 lustre_swab_hal(hal);
1257 } else if (lh->lnl_magic != LNL_MAGIC) {
1258 CERROR("Bad magic %x!=%x\n", lh->lnl_magic, LNL_MAGIC);
1262 CDEBUG(D_IOCTL, " Received message mg=%x t=%d m=%d l=%d actions=%d\n",
1263 lh->lnl_magic, lh->lnl_transport, lh->lnl_msgtype,
1264 lh->lnl_msglen, hal->hal_count);
1266 /* Broadcast to HSM listeners */
1267 rc = libcfs_klnl_msg_put(0, LNL_GRP_HSM, lh);
1272 int mdc_set_info_async(struct obd_export *exp,
1273 obd_count keylen, void *key,
1274 obd_count vallen, void *val,
1275 struct ptlrpc_request_set *set)
1277 struct obd_import *imp = class_exp2cliimp(exp);
1281 if (KEY_IS(KEY_INIT_RECOV)) {
1282 if (vallen != sizeof(int))
1284 spin_lock(&imp->imp_lock);
1285 imp->imp_initial_recov = *(int *)val;
1286 spin_unlock(&imp->imp_lock);
1287 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1288 exp->exp_obd->obd_name, imp->imp_initial_recov);
1291 /* Turn off initial_recov after we try all backup servers once */
1292 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1293 if (vallen != sizeof(int))
1295 spin_lock(&imp->imp_lock);
1296 imp->imp_initial_recov_bk = *(int *)val;
1297 if (imp->imp_initial_recov_bk)
1298 imp->imp_initial_recov = 1;
1299 spin_unlock(&imp->imp_lock);
1300 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1301 exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1304 if (KEY_IS(KEY_READ_ONLY)) {
1305 if (vallen != sizeof(int))
1308 spin_lock(&imp->imp_lock);
1309 if (*((int *)val)) {
1310 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1311 imp->imp_connect_data.ocd_connect_flags |= OBD_CONNECT_RDONLY;
1313 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1314 imp->imp_connect_data.ocd_connect_flags &= ~OBD_CONNECT_RDONLY;
1316 spin_unlock(&imp->imp_lock);
1318 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1319 keylen, key, vallen, val, set);
1322 if (KEY_IS(KEY_SPTLRPC_CONF)) {
1323 sptlrpc_conf_client_adapt(exp->exp_obd);
1326 if (KEY_IS(KEY_FLUSH_CTX)) {
1327 sptlrpc_import_flush_my_ctx(imp);
1330 if (KEY_IS(KEY_MDS_CONN)) {
1331 /* mds-mds import */
1332 spin_lock(&imp->imp_lock);
1333 imp->imp_server_timeout = 1;
1334 spin_unlock(&imp->imp_lock);
1335 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1336 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
1339 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1340 rc = target_set_info_rpc(imp, MDS_SET_INFO,
1341 keylen, key, vallen, val, set);
1344 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
1345 rc = mdc_hsm_copytool_send(vallen, val);
1352 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1353 __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1357 if (KEY_IS(KEY_MAX_EASIZE)) {
1358 int mdsize, *max_easize;
1360 if (*vallen != sizeof(int))
1362 mdsize = *(int*)val;
1363 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1364 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1366 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1369 if (KEY_IS(KEY_CONN_DATA)) {
1370 struct obd_import *imp = class_exp2cliimp(exp);
1371 struct obd_connect_data *data = val;
1373 if (*vallen != sizeof(*data))
1376 *data = imp->imp_connect_data;
1380 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
1385 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1386 __u64 max_age, __u32 flags)
1388 struct ptlrpc_request *req;
1389 struct obd_statfs *msfs;
1390 struct obd_import *imp = NULL;
1395 /*Since the request might also come from lprocfs, so we need
1396 *sync this with client_disconnect_export Bug15684*/
1397 down_read(&obd->u.cli.cl_sem);
1398 if (obd->u.cli.cl_import)
1399 imp = class_import_get(obd->u.cli.cl_import);
1400 up_read(&obd->u.cli.cl_sem);
1404 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1405 LUSTRE_MDS_VERSION, MDS_STATFS);
1407 GOTO(output, rc = -ENOMEM);
1409 ptlrpc_request_set_replen(req);
1411 if (flags & OBD_STATFS_NODELAY) {
1412 /* procfs requests not want stay in wait for avoid deadlock */
1413 req->rq_no_resend = 1;
1414 req->rq_no_delay = 1;
1417 rc = ptlrpc_queue_wait(req);
1419 /* check connection error first */
1420 if (imp->imp_connect_error)
1421 rc = imp->imp_connect_error;
1425 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1427 GOTO(out, rc = -EPROTO);
1432 ptlrpc_req_finished(req);
1434 class_import_put(imp);
1438 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
1439 struct obd_capa *oc, struct obd_client_handle *handle,
1442 struct ptlrpc_request *req;
1443 struct mdt_body *body;
1447 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
1451 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1453 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
1455 ptlrpc_request_free(req);
1459 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
1461 ptlrpc_request_set_replen(req);
1463 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1464 rc = ptlrpc_queue_wait(req);
1465 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1467 CERROR("Pin failed: %d\n", rc);
1471 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1473 GOTO(err_out, rc = -EPROTO);
1475 handle->och_fh = body->handle;
1476 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1478 handle->och_mod = obd_mod_alloc();
1479 if (handle->och_mod == NULL) {
1480 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
1481 GOTO(err_out, rc = -ENOMEM);
1483 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1488 ptlrpc_req_finished(req);
1492 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
1495 struct ptlrpc_request *req;
1496 struct mdt_body *body;
1500 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
1501 LUSTRE_MDS_VERSION, MDS_UNPIN);
1505 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
1506 body->handle = handle->och_fh;
1509 ptlrpc_request_set_replen(req);
1511 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1512 rc = ptlrpc_queue_wait(req);
1513 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1516 CERROR("Unpin failed: %d\n", rc);
1518 ptlrpc_req_finished(req);
1519 ptlrpc_req_finished(handle->och_mod->mod_open_req);
1521 obd_mod_put(handle->och_mod);
1525 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
1526 struct obd_capa *oc, struct ptlrpc_request **request)
1528 struct ptlrpc_request *req;
1533 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
1537 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1539 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
1541 ptlrpc_request_free(req);
1545 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
1547 ptlrpc_request_set_replen(req);
1549 rc = ptlrpc_queue_wait(req);
1551 ptlrpc_req_finished(req);
1557 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1558 enum obd_import_event event)
1562 LASSERT(imp->imp_obd == obd);
1565 case IMP_EVENT_DISCON: {
1567 /* XXX Pass event up to OBDs stack. used only for FLD now */
1568 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
1572 case IMP_EVENT_INACTIVE: {
1573 struct client_obd *cli = &obd->u.cli;
1575 * Flush current sequence to make client obtain new one
1576 * from server in case of disconnect/reconnect.
1577 * If range is already empty then no need to flush it.
1579 if (cli->cl_seq != NULL &&
1580 !range_is_exhausted(&cli->cl_seq->lcs_space)) {
1581 seq_client_flush(cli->cl_seq);
1584 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1587 case IMP_EVENT_INVALIDATE: {
1588 struct ldlm_namespace *ns = obd->obd_namespace;
1590 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1594 case IMP_EVENT_ACTIVE: {
1595 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1599 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
1603 CERROR("Unknown import event %x\n", event);
1609 static int mdc_fid_init(struct obd_export *exp)
1611 struct client_obd *cli = &exp->exp_obd->u.cli;
1616 OBD_ALLOC_PTR(cli->cl_seq);
1617 if (cli->cl_seq == NULL)
1620 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1622 GOTO(out_free_seq, rc = -ENOMEM);
1624 snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s",
1625 exp->exp_obd->obd_name);
1627 /* Init client side sequence-manager */
1628 rc = seq_client_init(cli->cl_seq, exp,
1629 LUSTRE_SEQ_METADATA,
1631 OBD_FREE(prefix, MAX_OBD_NAME + 5);
1633 GOTO(out_free_seq, rc);
1637 OBD_FREE_PTR(cli->cl_seq);
1642 static int mdc_fid_fini(struct obd_export *exp)
1644 struct client_obd *cli = &exp->exp_obd->u.cli;
1647 if (cli->cl_seq != NULL) {
1648 seq_client_fini(cli->cl_seq);
1649 OBD_FREE_PTR(cli->cl_seq);
1656 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1657 struct md_op_data *op_data)
1659 struct client_obd *cli = &exp->exp_obd->u.cli;
1660 struct lu_client_seq *seq = cli->cl_seq;
1662 RETURN(seq_client_alloc_fid(seq, fid));
1665 /* XXX This method is used only to clear current fid seq
1666 * once fld/mds insert failed */
1667 static int mdc_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1669 struct client_obd *cli = &exp->exp_obd->u.cli;
1671 seq_client_flush(cli->cl_seq);
1675 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
1676 struct client_obd *cli = &exp->exp_obd->u.cli;
1677 return &cli->cl_target_uuid;
1680 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
1682 struct client_obd *cli = &obd->u.cli;
1683 struct lprocfs_static_vars lvars = { 0 };
1687 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1688 if (!cli->cl_rpc_lock)
1690 mdc_init_rpc_lock(cli->cl_rpc_lock);
1694 OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1695 if (!cli->cl_setattr_lock)
1696 GOTO(err_rpc_lock, rc = -ENOMEM);
1697 mdc_init_rpc_lock(cli->cl_setattr_lock);
1699 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1700 if (!cli->cl_close_lock)
1701 GOTO(err_setattr_lock, rc = -ENOMEM);
1702 mdc_init_rpc_lock(cli->cl_close_lock);
1704 rc = client_obd_setup(obd, cfg);
1706 GOTO(err_close_lock, rc);
1707 lprocfs_mdc_init_vars(&lvars);
1708 lprocfs_obd_setup(obd, lvars.obd_vars);
1709 sptlrpc_lprocfs_cliobd_attach(obd);
1710 ptlrpc_lprocfs_register_obd(obd);
1712 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
1715 CERROR("failed to setup llogging subsystems\n");
1719 libcfs_klnl_start(LNL_TRANSPORT_HSM);
1720 libcfs_klnl_start(LNL_TRANSPORT_CHANGELOG);
1725 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1727 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1729 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1734 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
1735 * us to make MDS RPCs with large enough reply buffers to hold the
1736 * maximum-sized (= maximum striped) EA and cookie without having to
1737 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1738 static int mdc_init_ea_size(struct obd_export *exp, int easize,
1739 int def_easize, int cookiesize)
1741 struct obd_device *obd = exp->exp_obd;
1742 struct client_obd *cli = &obd->u.cli;
1745 if (cli->cl_max_mds_easize < easize)
1746 cli->cl_max_mds_easize = easize;
1748 if (cli->cl_default_mds_easize < def_easize)
1749 cli->cl_default_mds_easize = def_easize;
1751 if (cli->cl_max_mds_cookiesize < cookiesize)
1752 cli->cl_max_mds_cookiesize = cookiesize;
1757 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1763 case OBD_CLEANUP_EARLY:
1764 case OBD_CLEANUP_EXPORTS:
1765 /* If we set up but never connected, the
1766 client import will not have been cleaned. */
1767 if (obd->u.cli.cl_import) {
1768 struct obd_import *imp;
1769 down_write(&obd->u.cli.cl_sem);
1770 imp = obd->u.cli.cl_import;
1771 CERROR("client import never connected\n");
1772 ptlrpc_invalidate_import(imp);
1773 class_destroy_import(imp);
1774 up_write(&obd->u.cli.cl_sem);
1775 obd->u.cli.cl_import = NULL;
1777 rc = obd_llog_finish(obd, 0);
1779 CERROR("failed to cleanup llogging subsystems\n");
1785 static int mdc_cleanup(struct obd_device *obd)
1787 struct client_obd *cli = &obd->u.cli;
1789 libcfs_klnl_stop(LNL_TRANSPORT_HSM, LNL_GRP_HSM);
1790 libcfs_klnl_stop(LNL_TRANSPORT_CHANGELOG, 0);
1792 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1793 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1794 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1796 ptlrpc_lprocfs_unregister_obd(obd);
1797 lprocfs_obd_cleanup(obd);
1800 return client_obd_cleanup(obd);
1804 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1805 struct obd_device *tgt, int *index)
1807 struct llog_ctxt *ctxt;
1811 LASSERT(olg == &obd->obd_olg);
1813 rc = llog_setup(obd, olg, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
1818 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1819 llog_initiator_connect(ctxt);
1820 llog_ctxt_put(ctxt);
1822 rc = llog_setup(obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt, 0, NULL,
1825 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
1826 llog_initiator_connect(ctxt);
1827 llog_ctxt_put(ctxt);
1833 static int mdc_llog_finish(struct obd_device *obd, int count)
1835 struct llog_ctxt *ctxt;
1839 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1841 rc = llog_cleanup(ctxt);
1843 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
1845 rc = llog_cleanup(ctxt);
1850 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1852 struct lustre_cfg *lcfg = buf;
1853 struct lprocfs_static_vars lvars = { 0 };
1856 lprocfs_mdc_init_vars(&lvars);
1857 switch (lcfg->lcfg_command) {
1859 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
1869 /* get remote permission for current user on fid */
1870 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
1871 struct obd_capa *oc, __u32 suppgid,
1872 struct ptlrpc_request **request)
1874 struct ptlrpc_request *req;
1878 LASSERT(client_is_remote(exp));
1881 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
1885 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1887 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
1889 ptlrpc_request_free(req);
1893 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
1895 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
1896 sizeof(struct mdt_remote_perm));
1898 ptlrpc_request_set_replen(req);
1900 rc = ptlrpc_queue_wait(req);
1902 ptlrpc_req_finished(req);
1908 static int mdc_interpret_renew_capa(const struct lu_env *env,
1909 struct ptlrpc_request *req, void *unused,
1912 struct obd_capa *oc = req->rq_async_args.pointer_arg[0];
1913 renew_capa_cb_t cb = req->rq_async_args.pointer_arg[1];
1914 struct mdt_body *body = NULL;
1915 struct lustre_capa *capa;
1919 GOTO(out, capa = ERR_PTR(status));
1921 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1923 GOTO(out, capa = ERR_PTR(-EFAULT));
1925 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
1926 GOTO(out, capa = ERR_PTR(-ENOENT));
1928 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
1930 GOTO(out, capa = ERR_PTR(-EFAULT));
1937 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
1940 struct ptlrpc_request *req;
1943 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
1944 LUSTRE_MDS_VERSION, MDS_GETATTR);
1948 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
1949 * capa to renew is oss capa.
1951 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
1952 ptlrpc_request_set_replen(req);
1954 req->rq_async_args.pointer_arg[0] = oc;
1955 req->rq_async_args.pointer_arg[1] = cb;
1956 req->rq_interpret_reply = mdc_interpret_renew_capa;
1957 ptlrpcd_add_req(req, PSCOPE_OTHER);
1961 static int mdc_connect(const struct lu_env *env,
1962 struct obd_export **exp,
1963 struct obd_device *obd, struct obd_uuid *cluuid,
1964 struct obd_connect_data *data,
1967 struct obd_import *imp = obd->u.cli.cl_import;
1969 /* mds-mds import features */
1970 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
1971 spin_lock(&imp->imp_lock);
1972 imp->imp_server_timeout = 1;
1973 spin_unlock(&imp->imp_lock);
1974 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
1975 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
1979 return client_connect_import(env, exp, obd, cluuid, data, NULL);
1982 struct obd_ops mdc_obd_ops = {
1983 .o_owner = THIS_MODULE,
1984 .o_setup = mdc_setup,
1985 .o_precleanup = mdc_precleanup,
1986 .o_cleanup = mdc_cleanup,
1987 .o_add_conn = client_import_add_conn,
1988 .o_del_conn = client_import_del_conn,
1989 .o_connect = mdc_connect,
1990 .o_disconnect = client_disconnect_export,
1991 .o_iocontrol = mdc_iocontrol,
1992 .o_set_info_async = mdc_set_info_async,
1993 .o_statfs = mdc_statfs,
1995 .o_unpin = mdc_unpin,
1996 .o_fid_init = mdc_fid_init,
1997 .o_fid_fini = mdc_fid_fini,
1998 .o_fid_alloc = mdc_fid_alloc,
1999 .o_fid_delete = mdc_fid_delete,
2000 .o_import_event = mdc_import_event,
2001 .o_llog_init = mdc_llog_init,
2002 .o_llog_finish = mdc_llog_finish,
2003 .o_get_info = mdc_get_info,
2004 .o_process_config = mdc_process_config,
2005 .o_get_uuid = mdc_get_uuid,
2008 struct md_ops mdc_md_ops = {
2009 .m_getstatus = mdc_getstatus,
2010 .m_change_cbdata = mdc_change_cbdata,
2011 .m_close = mdc_close,
2012 .m_create = mdc_create,
2013 .m_done_writing = mdc_done_writing,
2014 .m_enqueue = mdc_enqueue,
2015 .m_getattr = mdc_getattr,
2016 .m_getattr_name = mdc_getattr_name,
2017 .m_intent_lock = mdc_intent_lock,
2019 .m_is_subdir = mdc_is_subdir,
2020 .m_rename = mdc_rename,
2021 .m_setattr = mdc_setattr,
2022 .m_setxattr = mdc_setxattr,
2023 .m_getxattr = mdc_getxattr,
2025 .m_readpage = mdc_readpage,
2026 .m_unlink = mdc_unlink,
2027 .m_cancel_unused = mdc_cancel_unused,
2028 .m_init_ea_size = mdc_init_ea_size,
2029 .m_set_lock_data = mdc_set_lock_data,
2030 .m_lock_match = mdc_lock_match,
2031 .m_get_lustre_md = mdc_get_lustre_md,
2032 .m_free_lustre_md = mdc_free_lustre_md,
2033 .m_set_open_replay_data = mdc_set_open_replay_data,
2034 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2035 .m_renew_capa = mdc_renew_capa,
2036 .m_unpack_capa = mdc_unpack_capa,
2037 .m_get_remote_perm = mdc_get_remote_perm,
2038 .m_intent_getattr_async = mdc_intent_getattr_async,
2039 .m_revalidate_lock = mdc_revalidate_lock
2042 int __init mdc_init(void)
2045 struct lprocfs_static_vars lvars = { 0 };
2046 lprocfs_mdc_init_vars(&lvars);
2048 request_module("lquota");
2049 quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
2050 init_obd_quota_ops(quota_interface, &mdc_obd_ops);
2052 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2053 LUSTRE_MDC_NAME, NULL);
2054 if (rc && quota_interface)
2055 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2061 static void /*__exit*/ mdc_exit(void)
2063 if (quota_interface)
2064 PORTAL_SYMBOL_PUT(mdc_quota_interface);
2066 class_unregister_type(LUSTRE_MDC_NAME);
2069 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2070 MODULE_DESCRIPTION("Lustre Metadata Client");
2071 MODULE_LICENSE("GPL");
2073 module_init(mdc_init);
2074 module_exit(mdc_exit);