4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_MDC
40 # include <linux/module.h>
41 # include <linux/pagemap.h>
42 # include <linux/miscdevice.h>
43 # include <linux/init.h>
45 # include <liblustre.h>
48 #include <lustre_acl.h>
49 #include <obd_class.h>
50 #include <lustre_fid.h>
51 #include <lprocfs_status.h>
52 #include <lustre_param.h>
53 #include <lustre_log.h>
55 #include "mdc_internal.h"
57 #define REQUEST_MINOR 244
59 struct mdc_renew_capa_args {
60 struct obd_capa *ra_oc;
61 renew_capa_cb_t ra_cb;
64 static int mdc_cleanup(struct obd_device *obd);
66 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
67 const struct req_msg_field *field, struct obd_capa **oc)
69 struct lustre_capa *capa;
73 /* swabbed already in mdc_enqueue */
74 capa = req_capsule_server_get(&req->rq_pill, field);
78 c = alloc_capa(CAPA_SITE_CLIENT);
80 CDEBUG(D_INFO, "alloc capa failed!\n");
89 static inline int mdc_queue_wait(struct ptlrpc_request *req)
91 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
94 /* mdc_enter_request() ensures that this client has no more
95 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
97 rc = mdc_enter_request(cli);
101 rc = ptlrpc_queue_wait(req);
102 mdc_exit_request(cli);
107 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
108 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
109 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
110 struct obd_capa **pc, int level, int msg_flags)
112 struct ptlrpc_request *req;
113 struct mdt_body *body;
117 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
118 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
122 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
123 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
124 req->rq_send_state = level;
126 ptlrpc_request_set_replen(req);
128 rc = ptlrpc_queue_wait(req);
132 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
134 GOTO(out, rc = -EPROTO);
136 if (body->valid & OBD_MD_FLMDSCAPA) {
137 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
142 *rootfid = body->fid1;
144 "root fid="DFID", last_committed="LPU64"\n",
146 lustre_msg_get_last_committed(req->rq_repmsg));
149 ptlrpc_req_finished(req);
153 /* This should be mdc_get_info("rootfid") */
154 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
155 struct obd_capa **pc)
157 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
162 * This function now is known to always saying that it will receive 4 buffers
163 * from server. Even for cases when acl_size and md_size is zero, RPC header
164 * will contain 4 fields and RPC itself will contain zero size fields. This is
165 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
166 * and thus zero, it shrinks it, making zero size. The same story about
167 * md_size. And this is course of problem when client waits for smaller number
168 * of fields. This issue will be fixed later when client gets aware of RPC
171 static int mdc_getattr_common(struct obd_export *exp,
172 struct ptlrpc_request *req)
174 struct req_capsule *pill = &req->rq_pill;
175 struct mdt_body *body;
180 /* Request message already built. */
181 rc = ptlrpc_queue_wait(req);
185 /* sanity check for the reply */
186 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
190 CDEBUG(D_NET, "mode: %o\n", body->mode);
192 if (body->eadatasize != 0) {
193 mdc_update_max_ea_from_body(exp, body);
195 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
201 if (body->valid & OBD_MD_FLRMTPERM) {
202 struct mdt_remote_perm *perm;
204 LASSERT(client_is_remote(exp));
205 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
206 lustre_swab_mdt_remote_perm);
211 if (body->valid & OBD_MD_FLMDSCAPA) {
212 struct lustre_capa *capa;
213 capa = req_capsule_server_get(pill, &RMF_CAPA1);
221 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
222 struct ptlrpc_request **request)
224 struct ptlrpc_request *req;
228 /* Single MDS without an LMV case */
229 if (op_data->op_flags & MF_GET_MDT_IDX) {
234 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
238 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
240 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
242 ptlrpc_request_free(req);
246 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
247 op_data->op_valid, op_data->op_mode, -1, 0);
249 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
251 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
252 LASSERT(client_is_remote(exp));
253 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
254 sizeof(struct mdt_remote_perm));
256 ptlrpc_request_set_replen(req);
258 rc = mdc_getattr_common(exp, req);
260 ptlrpc_req_finished(req);
266 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
267 struct ptlrpc_request **request)
269 struct ptlrpc_request *req;
274 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
275 &RQF_MDS_GETATTR_NAME);
279 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
280 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
281 op_data->op_namelen + 1);
283 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
285 ptlrpc_request_free(req);
289 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
290 op_data->op_valid, op_data->op_mode,
291 op_data->op_suppgids[0], 0);
293 if (op_data->op_name) {
294 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
295 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
296 op_data->op_namelen);
297 memcpy(name, op_data->op_name, op_data->op_namelen);
300 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
302 ptlrpc_request_set_replen(req);
304 rc = mdc_getattr_common(exp, req);
306 ptlrpc_req_finished(req);
312 static int mdc_is_subdir(struct obd_export *exp,
313 const struct lu_fid *pfid,
314 const struct lu_fid *cfid,
315 struct ptlrpc_request **request)
317 struct ptlrpc_request *req;
323 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
324 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
329 mdc_is_subdir_pack(req, pfid, cfid, 0);
330 ptlrpc_request_set_replen(req);
332 rc = ptlrpc_queue_wait(req);
333 if (rc && rc != -EREMOTE)
334 ptlrpc_req_finished(req);
340 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
341 const struct lu_fid *fid,
342 struct obd_capa *oc, int opcode, obd_valid valid,
343 const char *xattr_name, const char *input,
344 int input_size, int output_size, int flags,
345 __u32 suppgid, struct ptlrpc_request **request)
347 struct ptlrpc_request *req;
348 int xattr_namelen = 0;
354 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
358 mdc_set_capa_size(req, &RMF_CAPA1, oc);
360 xattr_namelen = strlen(xattr_name) + 1;
361 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
366 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
370 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
372 ptlrpc_request_free(req);
376 if (opcode == MDS_REINT) {
377 struct mdt_rec_setxattr *rec;
379 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
380 sizeof(struct mdt_rec_reint));
381 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
382 rec->sx_opcode = REINT_SETXATTR;
384 * cfs_curproc_fs{u,g}id() should replace
385 * current->fs{u,g}id for portability.
387 rec->sx_fsuid = cfs_curproc_fsuid();
388 rec->sx_fsgid = cfs_curproc_fsgid();
389 rec->sx_cap = cfs_curproc_cap_pack();
390 rec->sx_suppgid1 = suppgid;
391 rec->sx_suppgid2 = -1;
393 rec->sx_valid = valid | OBD_MD_FLCTIME;
394 rec->sx_time = cfs_time_current_sec();
395 rec->sx_size = output_size;
396 rec->sx_flags = flags;
398 mdc_pack_capa(req, &RMF_CAPA1, oc);
400 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
404 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
405 memcpy(tmp, xattr_name, xattr_namelen);
408 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
409 memcpy(tmp, input, input_size);
412 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
413 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
414 RCL_SERVER, output_size);
415 ptlrpc_request_set_replen(req);
418 if (opcode == MDS_REINT)
419 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
421 rc = ptlrpc_queue_wait(req);
423 if (opcode == MDS_REINT)
424 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
427 ptlrpc_req_finished(req);
433 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
434 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
435 const char *input, int input_size, int output_size,
436 int flags, __u32 suppgid, struct ptlrpc_request **request)
438 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
439 fid, oc, MDS_REINT, valid, xattr_name,
440 input, input_size, output_size, flags,
444 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
445 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
446 const char *input, int input_size, int output_size,
447 int flags, struct ptlrpc_request **request)
449 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
450 fid, oc, MDS_GETXATTR, valid, xattr_name,
451 input, input_size, output_size, flags,
455 #ifdef CONFIG_FS_POSIX_ACL
456 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
458 struct req_capsule *pill = &req->rq_pill;
459 struct mdt_body *body = md->body;
460 struct posix_acl *acl;
468 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
473 acl = posix_acl_from_xattr(buf, body->aclsize);
476 CERROR("convert xattr to acl: %d\n", rc);
480 rc = posix_acl_valid(acl);
482 CERROR("validate acl: %d\n", rc);
483 posix_acl_release(acl);
491 #define mdc_unpack_acl(req, md) 0
494 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
495 struct obd_export *dt_exp, struct obd_export *md_exp,
496 struct lustre_md *md)
498 struct req_capsule *pill = &req->rq_pill;
503 memset(md, 0, sizeof(*md));
505 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
506 LASSERT(md->body != NULL);
508 if (md->body->valid & OBD_MD_FLEASIZE) {
510 struct lov_mds_md *lmm;
512 if (!S_ISREG(md->body->mode)) {
513 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
514 "regular file, but is not\n");
515 GOTO(out, rc = -EPROTO);
518 if (md->body->eadatasize == 0) {
519 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
520 "but eadatasize 0\n");
521 GOTO(out, rc = -EPROTO);
523 lmmsize = md->body->eadatasize;
524 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
526 GOTO(out, rc = -EPROTO);
528 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
532 if (rc < sizeof(*md->lsm)) {
533 CDEBUG(D_INFO, "lsm size too small: "
534 "rc < sizeof (*md->lsm) (%d < %d)\n",
535 rc, (int)sizeof(*md->lsm));
536 GOTO(out, rc = -EPROTO);
539 } else if (md->body->valid & OBD_MD_FLDIREA) {
541 struct lov_mds_md *lmv;
543 if(!S_ISDIR(md->body->mode)) {
544 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
545 "directory, but is not\n");
546 GOTO(out, rc = -EPROTO);
549 if (md->body->eadatasize == 0) {
550 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
551 "but eadatasize 0\n");
554 if (md->body->valid & OBD_MD_MEA) {
555 lmvsize = md->body->eadatasize;
556 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
559 GOTO(out, rc = -EPROTO);
561 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
566 if (rc < sizeof(*md->mea)) {
567 CDEBUG(D_INFO, "size too small: "
568 "rc < sizeof(*md->mea) (%d < %d)\n",
569 rc, (int)sizeof(*md->mea));
570 GOTO(out, rc = -EPROTO);
576 if (md->body->valid & OBD_MD_FLRMTPERM) {
577 /* remote permission */
578 LASSERT(client_is_remote(exp));
579 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
580 lustre_swab_mdt_remote_perm);
581 if (!md->remote_perm)
582 GOTO(out, rc = -EPROTO);
584 else if (md->body->valid & OBD_MD_FLACL) {
585 /* for ACL, it's possible that FLACL is set but aclsize is zero.
586 * only when aclsize != 0 there's an actual segment for ACL
589 if (md->body->aclsize) {
590 rc = mdc_unpack_acl(req, md);
593 #ifdef CONFIG_FS_POSIX_ACL
595 md->posix_acl = NULL;
599 if (md->body->valid & OBD_MD_FLMDSCAPA) {
600 struct obd_capa *oc = NULL;
602 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
608 if (md->body->valid & OBD_MD_FLOSSCAPA) {
609 struct obd_capa *oc = NULL;
611 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
621 capa_put(md->oss_capa);
625 capa_put(md->mds_capa);
628 #ifdef CONFIG_FS_POSIX_ACL
629 posix_acl_release(md->posix_acl);
632 obd_free_memmd(dt_exp, &md->lsm);
637 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
644 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
647 void mdc_replay_open(struct ptlrpc_request *req)
649 struct md_open_data *mod = req->rq_cb_data;
650 struct ptlrpc_request *close_req;
651 struct obd_client_handle *och;
652 struct lustre_handle old;
653 struct mdt_body *body;
657 DEBUG_REQ(D_ERROR, req,
658 "Can't properly replay without open data.");
663 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
664 LASSERT(body != NULL);
668 struct lustre_handle *file_fh;
670 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
672 file_fh = &och->och_fh;
673 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
674 file_fh->cookie, body->handle.cookie);
676 *file_fh = body->handle;
678 close_req = mod->mod_close_req;
679 if (close_req != NULL) {
680 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
681 struct mdt_ioepoch *epoch;
683 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
684 epoch = req_capsule_client_get(&close_req->rq_pill,
689 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
690 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
691 epoch->handle = body->handle;
696 void mdc_commit_open(struct ptlrpc_request *req)
698 struct md_open_data *mod = req->rq_cb_data;
703 * No need to touch md_open_data::mod_och, it holds a reference on
704 * \var mod and will zero references to each other, \var mod will be
705 * freed after that when md_open_data::mod_och will put the reference.
709 * Do not let open request to disappear as it still may be needed
710 * for close rpc to happen (it may happen on evict only, otherwise
711 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
712 * called), just mark this rpc as committed to distinguish these 2
713 * cases, see mdc_close() for details. The open request reference will
714 * be put along with freeing \var mod.
716 ptlrpc_request_addref(req);
717 spin_lock(&req->rq_lock);
718 req->rq_committed = 1;
719 spin_unlock(&req->rq_lock);
720 req->rq_cb_data = NULL;
724 int mdc_set_open_replay_data(struct obd_export *exp,
725 struct obd_client_handle *och,
726 struct ptlrpc_request *open_req)
728 struct md_open_data *mod;
729 struct mdt_rec_create *rec;
730 struct mdt_body *body;
731 struct obd_import *imp = open_req->rq_import;
734 if (!open_req->rq_replay)
737 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
738 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
739 LASSERT(rec != NULL);
740 /* Incoming message in my byte order (it's been swabbed). */
741 /* Outgoing messages always in my byte order. */
742 LASSERT(body != NULL);
744 /* Only if the import is replayable, we set replay_open data */
745 if (och && imp->imp_replayable) {
746 mod = obd_mod_alloc();
748 DEBUG_REQ(D_ERROR, open_req,
749 "Can't allocate md_open_data");
754 * Take a reference on \var mod, to be freed on mdc_close().
755 * It protects \var mod from being freed on eviction (commit
756 * callback is called despite rq_replay flag).
757 * Another reference for \var och.
762 spin_lock(&open_req->rq_lock);
765 mod->mod_open_req = open_req;
766 open_req->rq_cb_data = mod;
767 open_req->rq_commit_cb = mdc_commit_open;
768 spin_unlock(&open_req->rq_lock);
771 rec->cr_fid2 = body->fid1;
772 rec->cr_ioepoch = body->ioepoch;
773 rec->cr_old_handle.cookie = body->handle.cookie;
774 open_req->rq_replay_cb = mdc_replay_open;
775 if (!fid_is_sane(&body->fid1)) {
776 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
781 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
785 int mdc_clear_open_replay_data(struct obd_export *exp,
786 struct obd_client_handle *och)
788 struct md_open_data *mod = och->och_mod;
792 * It is possible to not have \var mod in a case of eviction between
793 * lookup and ll_file_open().
798 LASSERT(mod != LP_POISON);
807 /* Prepares the request for the replay by the given reply */
808 static void mdc_close_handle_reply(struct ptlrpc_request *req,
809 struct md_op_data *op_data, int rc) {
810 struct mdt_body *repbody;
811 struct mdt_ioepoch *epoch;
813 if (req && rc == -EAGAIN) {
814 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
815 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
817 epoch->flags |= MF_SOM_AU;
818 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
819 op_data->op_flags |= MF_GETATTR_LOCK;
823 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
824 struct md_open_data *mod, struct ptlrpc_request **request)
826 struct obd_device *obd = class_exp2obd(exp);
827 struct ptlrpc_request *req;
832 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
836 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
838 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
840 ptlrpc_request_free(req);
844 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
845 * portal whose threads are not taking any DLM locks and are therefore
846 * always progressing */
847 req->rq_request_portal = MDS_READPAGE_PORTAL;
848 ptlrpc_at_set_req_timeout(req);
850 /* Ensure that this close's handle is fixed up during replay. */
851 if (likely(mod != NULL)) {
852 LASSERTF(mod->mod_open_req != NULL &&
853 mod->mod_open_req->rq_type != LI_POISON,
854 "POISONED open %p!\n", mod->mod_open_req);
856 mod->mod_close_req = req;
858 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
859 /* We no longer want to preserve this open for replay even
860 * though the open was committed. b=3632, b=3633 */
861 spin_lock(&mod->mod_open_req->rq_lock);
862 mod->mod_open_req->rq_replay = 0;
863 spin_unlock(&mod->mod_open_req->rq_lock);
865 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
868 mdc_close_pack(req, op_data);
870 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
871 obd->u.cli.cl_max_mds_easize);
872 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
873 obd->u.cli.cl_max_mds_cookiesize);
875 ptlrpc_request_set_replen(req);
877 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
878 rc = ptlrpc_queue_wait(req);
879 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
881 if (req->rq_repmsg == NULL) {
882 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
885 rc = req->rq_status ?: -EIO;
886 } else if (rc == 0 || rc == -EAGAIN) {
887 struct mdt_body *body;
889 rc = lustre_msg_get_status(req->rq_repmsg);
890 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
891 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
896 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
899 } else if (rc == -ESTALE) {
901 * it can be allowed error after 3633 if open was committed and
902 * server failed before close was sent. Let's check if mod
903 * exists and return no error in that case
906 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
907 LASSERT(mod->mod_open_req != NULL);
908 if (mod->mod_open_req->rq_committed)
915 mod->mod_close_req = NULL;
916 /* Since now, mod is accessed through open_req only,
917 * thus close req does not keep a reference on mod anymore. */
921 mdc_close_handle_reply(req, op_data, rc);
925 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
926 struct md_open_data *mod)
928 struct obd_device *obd = class_exp2obd(exp);
929 struct ptlrpc_request *req;
933 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
934 &RQF_MDS_DONE_WRITING);
938 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
939 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
941 ptlrpc_request_free(req);
946 LASSERTF(mod->mod_open_req != NULL &&
947 mod->mod_open_req->rq_type != LI_POISON,
948 "POISONED setattr %p!\n", mod->mod_open_req);
950 mod->mod_close_req = req;
951 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
952 /* We no longer want to preserve this setattr for replay even
953 * though the open was committed. b=3632, b=3633 */
954 spin_lock(&mod->mod_open_req->rq_lock);
955 mod->mod_open_req->rq_replay = 0;
956 spin_unlock(&mod->mod_open_req->rq_lock);
959 mdc_close_pack(req, op_data);
960 ptlrpc_request_set_replen(req);
962 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
963 rc = ptlrpc_queue_wait(req);
964 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
968 * it can be allowed error after 3633 if open or setattr were
969 * committed and server failed before close was sent.
970 * Let's check if mod exists and return no error in that case
973 LASSERT(mod->mod_open_req != NULL);
974 if (mod->mod_open_req->rq_committed)
981 mod->mod_close_req = NULL;
982 /* Since now, mod is accessed through setattr req only,
983 * thus DW req does not keep a reference on mod anymore. */
987 mdc_close_handle_reply(req, op_data, rc);
988 ptlrpc_req_finished(req);
992 #ifdef HAVE_SPLIT_SUPPORT
993 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
994 const struct page *page, int offset)
996 struct ptlrpc_request *req;
997 struct ptlrpc_bulk_desc *desc;
1001 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1005 /* FIXME: capa doesn't support split yet */
1006 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1008 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1010 ptlrpc_request_free(req);
1014 req->rq_request_portal = MDS_READPAGE_PORTAL;
1015 ptlrpc_at_set_req_timeout(req);
1017 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1019 GOTO(out, rc = -ENOMEM);
1021 /* NB req now owns desc and will free it when it gets freed. */
1022 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1023 mdc_readdir_pack(req, 0, offset, fid, NULL);
1025 ptlrpc_request_set_replen(req);
1026 rc = ptlrpc_queue_wait(req);
1030 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1032 ptlrpc_req_finished(req);
1035 EXPORT_SYMBOL(mdc_sendpage);
1038 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1039 struct page **pages, struct ptlrpc_request **request)
1041 struct ptlrpc_request *req;
1042 struct ptlrpc_bulk_desc *desc;
1046 struct l_wait_info lwi;
1051 cfs_waitq_init(&waitq);
1054 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1058 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1060 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1062 ptlrpc_request_free(req);
1066 req->rq_request_portal = MDS_READPAGE_PORTAL;
1067 ptlrpc_at_set_req_timeout(req);
1069 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1072 ptlrpc_request_free(req);
1076 /* NB req now owns desc and will free it when it gets freed */
1077 for (i = 0; i < op_data->op_npages; i++)
1078 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, CFS_PAGE_SIZE);
1080 mdc_readdir_pack(req, op_data->op_offset,
1081 CFS_PAGE_SIZE * op_data->op_npages,
1082 &op_data->op_fid1, op_data->op_capa1);
1084 ptlrpc_request_set_replen(req);
1085 rc = ptlrpc_queue_wait(req);
1087 ptlrpc_req_finished(req);
1088 if (rc != -ETIMEDOUT)
1092 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1093 CERROR("too many resend retries, returning error\n");
1096 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1097 l_wait_event(waitq, 0, &lwi);
1102 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1103 req->rq_bulk->bd_nob_transferred);
1105 ptlrpc_req_finished(req);
1109 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1110 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1111 req->rq_bulk->bd_nob_transferred,
1112 CFS_PAGE_SIZE * op_data->op_npages);
1113 ptlrpc_req_finished(req);
1121 static int mdc_statfs(const struct lu_env *env,
1122 struct obd_export *exp, struct obd_statfs *osfs,
1123 __u64 max_age, __u32 flags)
1125 struct obd_device *obd = class_exp2obd(exp);
1126 struct ptlrpc_request *req;
1127 struct obd_statfs *msfs;
1128 struct obd_import *imp = NULL;
1133 * Since the request might also come from lprocfs, so we need
1134 * sync this with client_disconnect_export Bug15684
1136 down_read(&obd->u.cli.cl_sem);
1137 if (obd->u.cli.cl_import)
1138 imp = class_import_get(obd->u.cli.cl_import);
1139 up_read(&obd->u.cli.cl_sem);
1143 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1144 LUSTRE_MDS_VERSION, MDS_STATFS);
1146 GOTO(output, rc = -ENOMEM);
1148 ptlrpc_request_set_replen(req);
1150 if (flags & OBD_STATFS_NODELAY) {
1151 /* procfs requests not want stay in wait for avoid deadlock */
1152 req->rq_no_resend = 1;
1153 req->rq_no_delay = 1;
1156 rc = ptlrpc_queue_wait(req);
1158 /* check connection error first */
1159 if (imp->imp_connect_error)
1160 rc = imp->imp_connect_error;
1164 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1166 GOTO(out, rc = -EPROTO);
1171 ptlrpc_req_finished(req);
1173 class_import_put(imp);
1177 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1179 __u32 keylen, vallen;
1183 if (gf->gf_pathlen > PATH_MAX)
1184 RETURN(-ENAMETOOLONG);
1185 if (gf->gf_pathlen < 2)
1188 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1189 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1190 OBD_ALLOC(key, keylen);
1193 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1194 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1196 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1197 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1199 if (!fid_is_sane(&gf->gf_fid))
1200 GOTO(out, rc = -EINVAL);
1202 /* Val is struct getinfo_fid2path result plus path */
1203 vallen = sizeof(*gf) + gf->gf_pathlen;
1205 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1206 if (rc != 0 && rc != -EREMOTE)
1209 if (vallen <= sizeof(*gf))
1210 GOTO(out, rc = -EPROTO);
1211 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1212 GOTO(out, rc = -EOVERFLOW);
1214 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1215 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1218 OBD_FREE(key, keylen);
1222 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1223 struct hsm_progress_kernel *hpk)
1225 struct obd_import *imp = class_exp2cliimp(exp);
1226 struct hsm_progress_kernel *req_hpk;
1227 struct ptlrpc_request *req;
1231 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1232 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1234 GOTO(out, rc = -ENOMEM);
1236 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1238 /* Copy hsm_progress struct */
1239 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1240 if (req_hpk == NULL)
1241 GOTO(out, rc = -EPROTO);
1245 ptlrpc_request_set_replen(req);
1247 rc = mdc_queue_wait(req);
1250 ptlrpc_req_finished(req);
1254 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1256 __u32 *archive_mask;
1257 struct ptlrpc_request *req;
1261 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1263 MDS_HSM_CT_REGISTER);
1265 GOTO(out, rc = -ENOMEM);
1267 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1269 /* Copy hsm_progress struct */
1270 archive_mask = req_capsule_client_get(&req->rq_pill,
1271 &RMF_MDS_HSM_ARCHIVE);
1272 if (archive_mask == NULL)
1273 GOTO(out, rc = -EPROTO);
1275 *archive_mask = archives;
1277 ptlrpc_request_set_replen(req);
1279 rc = mdc_queue_wait(req);
1282 ptlrpc_req_finished(req);
1286 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1287 struct md_op_data *op_data)
1289 struct hsm_current_action *hca = op_data->op_data;
1290 struct hsm_current_action *req_hca;
1291 struct ptlrpc_request *req;
1295 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1296 &RQF_MDS_HSM_ACTION);
1300 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1302 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1304 ptlrpc_request_free(req);
1308 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1309 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1311 ptlrpc_request_set_replen(req);
1313 rc = mdc_queue_wait(req);
1317 req_hca = req_capsule_server_get(&req->rq_pill,
1318 &RMF_MDS_HSM_CURRENT_ACTION);
1319 if (req_hca == NULL)
1320 GOTO(out, rc = -EPROTO);
1326 ptlrpc_req_finished(req);
1330 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1332 struct ptlrpc_request *req;
1336 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1338 MDS_HSM_CT_UNREGISTER);
1340 GOTO(out, rc = -ENOMEM);
1342 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1344 ptlrpc_request_set_replen(req);
1346 rc = mdc_queue_wait(req);
1349 ptlrpc_req_finished(req);
1353 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1354 struct md_op_data *op_data)
1356 struct hsm_user_state *hus = op_data->op_data;
1357 struct hsm_user_state *req_hus;
1358 struct ptlrpc_request *req;
1362 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1363 &RQF_MDS_HSM_STATE_GET);
1367 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1369 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1371 ptlrpc_request_free(req);
1375 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1376 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1378 ptlrpc_request_set_replen(req);
1380 rc = mdc_queue_wait(req);
1384 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1385 if (req_hus == NULL)
1386 GOTO(out, rc = -EPROTO);
1392 ptlrpc_req_finished(req);
1396 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1397 struct md_op_data *op_data)
1399 struct hsm_state_set *hss = op_data->op_data;
1400 struct hsm_state_set *req_hss;
1401 struct ptlrpc_request *req;
1405 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1406 &RQF_MDS_HSM_STATE_SET);
1410 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1412 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1414 ptlrpc_request_free(req);
1418 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1419 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1422 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1423 if (req_hss == NULL)
1424 GOTO(out, rc = -EPROTO);
1427 ptlrpc_request_set_replen(req);
1429 rc = mdc_queue_wait(req);
1434 ptlrpc_req_finished(req);
1438 static int mdc_ioc_hsm_request(struct obd_export *exp,
1439 struct hsm_user_request *hur)
1441 struct obd_import *imp = class_exp2cliimp(exp);
1442 struct ptlrpc_request *req;
1443 struct hsm_request *req_hr;
1444 struct hsm_user_item *req_hui;
1449 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1451 GOTO(out, rc = -ENOMEM);
1453 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1454 hur->hur_request.hr_itemcount
1455 * sizeof(struct hsm_user_item));
1456 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1457 hur->hur_request.hr_data_len);
1459 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1461 ptlrpc_request_free(req);
1465 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1467 /* Copy hsm_request struct */
1468 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1470 GOTO(out, rc = -EPROTO);
1471 *req_hr = hur->hur_request;
1473 /* Copy hsm_user_item structs */
1474 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1475 if (req_hui == NULL)
1476 GOTO(out, rc = -EPROTO);
1477 memcpy(req_hui, hur->hur_user_item,
1478 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1480 /* Copy opaque field */
1481 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1482 if (req_opaque == NULL)
1483 GOTO(out, rc = -EPROTO);
1484 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1486 ptlrpc_request_set_replen(req);
1488 rc = mdc_queue_wait(req);
1492 ptlrpc_req_finished(req);
1496 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1498 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1500 LASSERT(len <= CR_MAXSIZE);
1502 lh->kuc_magic = KUC_MAGIC;
1503 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1504 lh->kuc_flags = flags;
1505 lh->kuc_msgtype = CL_RECORD;
1506 lh->kuc_msglen = len;
1510 #define D_CHANGELOG 0
1512 struct changelog_show {
1517 struct obd_device *cs_obd;
1520 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1521 struct llog_rec_hdr *hdr, void *data)
1523 struct changelog_show *cs = data;
1524 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1529 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1531 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1532 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1533 rec->cr.cr_type, rc);
1537 if (rec->cr.cr_index < cs->cs_startrec) {
1538 /* Skip entries earlier than what we are interested in */
1539 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1540 rec->cr.cr_index, cs->cs_startrec);
1544 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1545 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1546 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1547 rec->cr.cr_flags & CLF_FLAGMASK,
1548 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1549 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1551 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1553 /* Set up the message */
1554 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1555 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1557 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1558 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1563 static int mdc_changelog_send_thread(void *csdata)
1565 struct changelog_show *cs = csdata;
1566 struct llog_ctxt *ctxt = NULL;
1567 struct llog_handle *llh = NULL;
1568 struct kuc_hdr *kuch;
1571 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1572 cs->cs_fp, cs->cs_startrec);
1575 * It's important to daemonize here to close unused FDs.
1576 * The write fd from pipe is already opened by the caller,
1577 * so it's fine to clear all files here
1579 cfs_daemonize("mdc_clg_send_thread");
1581 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1582 if (cs->cs_buf == NULL)
1583 GOTO(out, rc = -ENOMEM);
1585 /* Set up the remote catalog handle */
1586 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1588 GOTO(out, rc = -ENOENT);
1589 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1592 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1593 cs->cs_obd->obd_name, rc);
1596 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1598 CERROR("llog_init_handle failed %d\n", rc);
1602 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1604 /* Send EOF no matter what our result */
1605 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1607 kuch->kuc_msgtype = CL_EOF;
1608 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1614 llog_cat_close(NULL, llh);
1616 llog_ctxt_put(ctxt);
1618 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1620 /* detach from parent process so we get cleaned up */
1621 cfs_daemonize("cl_send");
1625 static int mdc_ioc_changelog_send(struct obd_device *obd,
1626 struct ioc_changelog *icc)
1628 struct changelog_show *cs;
1631 /* Freed in mdc_changelog_send_thread */
1637 cs->cs_startrec = icc->icc_recno;
1638 /* matching fput in mdc_changelog_send_thread */
1639 cs->cs_fp = fget(icc->icc_id);
1640 cs->cs_flags = icc->icc_flags;
1642 /* New thread because we should return to user app before
1643 writing into our pipe */
1644 rc = cfs_create_thread(mdc_changelog_send_thread, cs, CFS_DAEMON_FLAGS);
1646 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1650 CERROR("Failed to start changelog thread: %d\n", rc);
1655 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1656 struct lustre_kernelcomm *lk);
1658 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1659 struct obd_quotactl *oqctl)
1661 struct client_obd *cli = &exp->exp_obd->u.cli;
1662 struct ptlrpc_request *req;
1663 struct obd_quotactl *body;
1667 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1668 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1673 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1676 ptlrpc_request_set_replen(req);
1678 /* the next poll will find -ENODATA, that means quotacheck is
1680 cli->cl_qchk_stat = -ENODATA;
1681 rc = ptlrpc_queue_wait(req);
1683 cli->cl_qchk_stat = rc;
1684 ptlrpc_req_finished(req);
1688 static int mdc_quota_poll_check(struct obd_export *exp,
1689 struct if_quotacheck *qchk)
1691 struct client_obd *cli = &exp->exp_obd->u.cli;
1695 qchk->obd_uuid = cli->cl_target_uuid;
1696 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1698 rc = cli->cl_qchk_stat;
1699 /* the client is not the previous one */
1700 if (rc == CL_NOT_QUOTACHECKED)
1705 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1706 struct obd_quotactl *oqctl)
1708 struct ptlrpc_request *req;
1709 struct obd_quotactl *oqc;
1713 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1714 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1719 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1722 ptlrpc_request_set_replen(req);
1723 ptlrpc_at_set_req_timeout(req);
1724 req->rq_no_resend = 1;
1726 rc = ptlrpc_queue_wait(req);
1728 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1730 if (req->rq_repmsg &&
1731 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1734 CERROR ("Can't unpack obd_quotactl\n");
1737 ptlrpc_req_finished(req);
1742 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1743 struct md_op_data *op_data)
1745 CFS_LIST_HEAD(cancels);
1746 struct ptlrpc_request *req;
1748 struct mdc_swap_layouts *msl, *payload;
1751 msl = op_data->op_data;
1753 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1754 * first thing it will do is to cancel the 2 layout
1755 * locks hold by this client.
1756 * So the client must cancel its layout locks on the 2 fids
1757 * with the request RPC to avoid extra RPC round trips
1759 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1760 LCK_CR, MDS_INODELOCK_LAYOUT);
1761 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1762 LCK_CR, MDS_INODELOCK_LAYOUT);
1764 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1765 &RQF_MDS_SWAP_LAYOUTS);
1767 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1771 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1772 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1774 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1776 ptlrpc_request_free(req);
1780 mdc_swap_layouts_pack(req, op_data);
1782 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1787 ptlrpc_request_set_replen(req);
1789 rc = ptlrpc_queue_wait(req);
1795 ptlrpc_req_finished(req);
1799 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1800 void *karg, void *uarg)
1802 struct obd_device *obd = exp->exp_obd;
1803 struct obd_ioctl_data *data = karg;
1804 struct obd_import *imp = obd->u.cli.cl_import;
1805 struct llog_ctxt *ctxt;
1809 if (!cfs_try_module_get(THIS_MODULE)) {
1810 CERROR("Can't get module. Is it alive?");
1814 case OBD_IOC_CHANGELOG_SEND:
1815 rc = mdc_ioc_changelog_send(obd, karg);
1817 case OBD_IOC_CHANGELOG_CLEAR: {
1818 struct ioc_changelog *icc = karg;
1819 struct changelog_setinfo cs =
1820 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1821 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1822 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1826 case OBD_IOC_FID2PATH:
1827 rc = mdc_ioc_fid2path(exp, karg);
1829 case LL_IOC_HSM_CT_START:
1830 rc = mdc_ioc_hsm_ct_start(exp, karg);
1832 case LL_IOC_HSM_PROGRESS:
1833 rc = mdc_ioc_hsm_progress(exp, karg);
1835 case LL_IOC_HSM_STATE_GET:
1836 rc = mdc_ioc_hsm_state_get(exp, karg);
1838 case LL_IOC_HSM_STATE_SET:
1839 rc = mdc_ioc_hsm_state_set(exp, karg);
1840 case LL_IOC_HSM_ACTION:
1841 rc = mdc_ioc_hsm_current_action(exp, karg);
1843 case LL_IOC_HSM_REQUEST:
1844 rc = mdc_ioc_hsm_request(exp, karg);
1846 case OBD_IOC_CLIENT_RECOVER:
1847 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1851 case IOC_OSC_SET_ACTIVE:
1852 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1854 case OBD_IOC_PARSE: {
1855 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1856 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1858 llog_ctxt_put(ctxt);
1862 case OBD_IOC_LLOG_INFO:
1863 case OBD_IOC_LLOG_PRINT: {
1864 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1865 rc = llog_ioctl(NULL, ctxt, cmd, data);
1866 llog_ctxt_put(ctxt);
1870 case OBD_IOC_POLL_QUOTACHECK:
1871 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1873 case OBD_IOC_PING_TARGET:
1874 rc = ptlrpc_obd_ping(obd);
1877 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1878 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1879 * there'd be no LMV layer thus we might be called here. Eventually
1880 * this code should be removed.
1883 case IOC_OBD_STATFS: {
1884 struct obd_statfs stat_buf = {0};
1886 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1887 GOTO(out, rc = -ENODEV);
1890 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1891 min((int) data->ioc_plen2,
1892 (int) sizeof(struct obd_uuid))))
1893 GOTO(out, rc = -EFAULT);
1895 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1896 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1901 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1902 min((int) data->ioc_plen1,
1903 (int) sizeof(stat_buf))))
1904 GOTO(out, rc = -EFAULT);
1908 case OBD_IOC_QUOTACTL: {
1909 struct if_quotactl *qctl = karg;
1910 struct obd_quotactl *oqctl;
1912 OBD_ALLOC_PTR(oqctl);
1916 QCTL_COPY(oqctl, qctl);
1917 rc = obd_quotactl(exp, oqctl);
1919 QCTL_COPY(qctl, oqctl);
1920 qctl->qc_valid = QC_MDTIDX;
1921 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1923 OBD_FREE_PTR(oqctl);
1926 case LL_IOC_GET_CONNECT_FLAGS: {
1927 if (cfs_copy_to_user(uarg,
1928 exp_connect_flags_ptr(exp),
1930 GOTO(out, rc = -EFAULT);
1934 case LL_IOC_LOV_SWAP_LAYOUTS: {
1935 rc = mdc_ioc_swap_layouts(exp, karg);
1939 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1940 GOTO(out, rc = -ENOTTY);
1943 cfs_module_put(THIS_MODULE);
1948 int mdc_get_info_rpc(struct obd_export *exp,
1949 obd_count keylen, void *key,
1950 int vallen, void *val)
1952 struct obd_import *imp = class_exp2cliimp(exp);
1953 struct ptlrpc_request *req;
1958 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1962 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1963 RCL_CLIENT, keylen);
1964 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1965 RCL_CLIENT, sizeof(__u32));
1967 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1969 ptlrpc_request_free(req);
1973 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1974 memcpy(tmp, key, keylen);
1975 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1976 memcpy(tmp, &vallen, sizeof(__u32));
1978 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1979 RCL_SERVER, vallen);
1980 ptlrpc_request_set_replen(req);
1982 rc = ptlrpc_queue_wait(req);
1983 /* -EREMOTE means the get_info result is partial, and it needs to
1984 * continue on another MDT, see fid2path part in lmv_iocontrol */
1985 if (rc == 0 || rc == -EREMOTE) {
1986 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1987 memcpy(val, tmp, vallen);
1988 if (ptlrpc_rep_need_swab(req)) {
1989 if (KEY_IS(KEY_FID2PATH))
1990 lustre_swab_fid2path(val);
1993 ptlrpc_req_finished(req);
1998 static void lustre_swab_hai(struct hsm_action_item *h)
2000 __swab32s(&h->hai_len);
2001 __swab32s(&h->hai_action);
2002 lustre_swab_lu_fid(&h->hai_fid);
2003 lustre_swab_lu_fid(&h->hai_dfid);
2004 __swab64s(&h->hai_cookie);
2005 __swab64s(&h->hai_extent.offset);
2006 __swab64s(&h->hai_extent.length);
2007 __swab64s(&h->hai_gid);
2010 static void lustre_swab_hal(struct hsm_action_list *h)
2012 struct hsm_action_item *hai;
2015 __swab32s(&h->hal_version);
2016 __swab32s(&h->hal_count);
2017 __swab32s(&h->hal_archive_id);
2018 __swab64s(&h->hal_flags);
2020 for (i = 0; i < h->hal_count; i++) {
2021 lustre_swab_hai(hai);
2022 hai = hai_next(hai);
2026 static void lustre_swab_kuch(struct kuc_hdr *l)
2028 __swab16s(&l->kuc_magic);
2029 /* __u8 l->kuc_transport */
2030 __swab16s(&l->kuc_msgtype);
2031 __swab16s(&l->kuc_msglen);
2034 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2035 struct lustre_kernelcomm *lk)
2037 struct obd_import *imp = class_exp2cliimp(exp);
2038 __u32 archive = lk->lk_data;
2041 if (lk->lk_group != KUC_GRP_HSM) {
2042 CERROR("Bad copytool group %d\n", lk->lk_group);
2046 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2047 lk->lk_uid, lk->lk_group, lk->lk_flags);
2049 if (lk->lk_flags & LK_FLG_STOP) {
2050 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
2051 /* Unregister with the coordinator */
2053 rc = mdc_ioc_hsm_ct_unregister(imp);
2055 struct file *fp = fget(lk->lk_wfd);
2057 rc = libcfs_kkuc_group_add(fp, lk->lk_uid, lk->lk_group,
2062 rc = mdc_ioc_hsm_ct_register(imp, archive);
2069 * Send a message to any listening copytools
2070 * @param val KUC message (kuc_hdr + hsm_action_list)
2071 * @param len total length of message
2073 static int mdc_hsm_copytool_send(int len, void *val)
2075 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2076 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2080 if (len < sizeof(*lh) + sizeof(*hal)) {
2081 CERROR("Short HSM message %d < %d\n", len,
2082 (int) (sizeof(*lh) + sizeof(*hal)));
2085 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2086 lustre_swab_kuch(lh);
2087 lustre_swab_hal(hal);
2088 } else if (lh->kuc_magic != KUC_MAGIC) {
2089 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2093 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2095 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2096 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2098 /* Broadcast to HSM listeners */
2099 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2105 * callback function passed to kuc for re-registering each HSM copytool
2106 * running on MDC, after MDT shutdown/recovery.
2107 * @param data archive id served by the copytool
2108 * @param cb_arg callback argument (obd_import)
2110 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2112 struct obd_import *imp = (struct obd_import *)cb_arg;
2113 __u32 archive = data;
2116 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2118 rc = mdc_ioc_hsm_ct_register(imp, archive);
2120 /* ignore error if the copytool is already registered */
2121 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2125 * Re-establish all kuc contexts with MDT
2126 * after MDT shutdown/recovery.
2128 static int mdc_kuc_reregister(struct obd_import *imp)
2130 /* re-register HSM agents */
2131 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2135 int mdc_set_info_async(const struct lu_env *env,
2136 struct obd_export *exp,
2137 obd_count keylen, void *key,
2138 obd_count vallen, void *val,
2139 struct ptlrpc_request_set *set)
2141 struct obd_import *imp = class_exp2cliimp(exp);
2145 if (KEY_IS(KEY_READ_ONLY)) {
2146 if (vallen != sizeof(int))
2149 spin_lock(&imp->imp_lock);
2150 if (*((int *)val)) {
2151 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2152 imp->imp_connect_data.ocd_connect_flags |=
2155 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2156 imp->imp_connect_data.ocd_connect_flags &=
2157 ~OBD_CONNECT_RDONLY;
2159 spin_unlock(&imp->imp_lock);
2161 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2162 keylen, key, vallen, val, set);
2165 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2166 sptlrpc_conf_client_adapt(exp->exp_obd);
2169 if (KEY_IS(KEY_FLUSH_CTX)) {
2170 sptlrpc_import_flush_my_ctx(imp);
2173 if (KEY_IS(KEY_MDS_CONN)) {
2174 /* mds-mds import */
2175 spin_lock(&imp->imp_lock);
2176 imp->imp_server_timeout = 1;
2177 spin_unlock(&imp->imp_lock);
2178 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2179 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2182 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2183 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2184 keylen, key, vallen, val, set);
2187 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2188 rc = mdc_hsm_copytool_send(vallen, val);
2192 CERROR("Unknown key %s\n", (char *)key);
2196 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2197 __u32 keylen, void *key, __u32 *vallen, void *val,
2198 struct lov_stripe_md *lsm)
2202 if (KEY_IS(KEY_MAX_EASIZE)) {
2203 int mdsize, *max_easize;
2205 if (*vallen != sizeof(int))
2207 mdsize = *(int*)val;
2208 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2209 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2211 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2213 } else if (KEY_IS(KEY_CONN_DATA)) {
2214 struct obd_import *imp = class_exp2cliimp(exp);
2215 struct obd_connect_data *data = val;
2217 if (*vallen != sizeof(*data))
2220 *data = imp->imp_connect_data;
2222 } else if (KEY_IS(KEY_TGT_COUNT)) {
2227 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2232 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2233 struct obd_capa *oc, struct obd_client_handle *handle,
2236 struct ptlrpc_request *req;
2237 struct mdt_body *body;
2241 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2245 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2247 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2249 ptlrpc_request_free(req);
2253 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2255 ptlrpc_request_set_replen(req);
2257 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2258 rc = ptlrpc_queue_wait(req);
2259 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2261 CERROR("Pin failed: %d\n", rc);
2265 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2267 GOTO(err_out, rc = -EPROTO);
2269 handle->och_fh = body->handle;
2270 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2272 handle->och_mod = obd_mod_alloc();
2273 if (handle->och_mod == NULL) {
2274 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2275 GOTO(err_out, rc = -ENOMEM);
2277 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2282 ptlrpc_req_finished(req);
2286 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2289 struct ptlrpc_request *req;
2290 struct mdt_body *body;
2294 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2295 LUSTRE_MDS_VERSION, MDS_UNPIN);
2299 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2300 body->handle = handle->och_fh;
2303 ptlrpc_request_set_replen(req);
2305 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2306 rc = ptlrpc_queue_wait(req);
2307 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2310 CERROR("Unpin failed: %d\n", rc);
2312 ptlrpc_req_finished(req);
2313 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2315 obd_mod_put(handle->och_mod);
2319 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2320 struct obd_capa *oc, struct ptlrpc_request **request)
2322 struct ptlrpc_request *req;
2327 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2331 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2333 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2335 ptlrpc_request_free(req);
2339 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2341 ptlrpc_request_set_replen(req);
2343 rc = ptlrpc_queue_wait(req);
2345 ptlrpc_req_finished(req);
2351 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2352 enum obd_import_event event)
2356 LASSERT(imp->imp_obd == obd);
2359 case IMP_EVENT_DISCON: {
2361 /* XXX Pass event up to OBDs stack. used only for FLD now */
2362 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2366 case IMP_EVENT_INACTIVE: {
2367 struct client_obd *cli = &obd->u.cli;
2369 * Flush current sequence to make client obtain new one
2370 * from server in case of disconnect/reconnect.
2372 if (cli->cl_seq != NULL)
2373 seq_client_flush(cli->cl_seq);
2375 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2378 case IMP_EVENT_INVALIDATE: {
2379 struct ldlm_namespace *ns = obd->obd_namespace;
2381 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2385 case IMP_EVENT_ACTIVE:
2386 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2387 /* restore re-establish kuc registration after reconnecting */
2389 rc = mdc_kuc_reregister(imp);
2392 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2394 case IMP_EVENT_DEACTIVATE:
2395 case IMP_EVENT_ACTIVATE:
2398 CERROR("Unknown import event %x\n", event);
2404 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2405 struct md_op_data *op_data)
2407 struct client_obd *cli = &exp->exp_obd->u.cli;
2408 struct lu_client_seq *seq = cli->cl_seq;
2410 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2413 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2414 struct client_obd *cli = &exp->exp_obd->u.cli;
2415 return &cli->cl_target_uuid;
2419 * Determine whether the lock can be canceled before replaying it during
2420 * recovery, non zero value will be return if the lock can be canceled,
2421 * or zero returned for not
2423 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2425 if (lock->l_resource->lr_type != LDLM_IBITS)
2428 /* FIXME: if we ever get into a situation where there are too many
2429 * opened files with open locks on a single node, then we really
2430 * should replay these open locks to reget it */
2431 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2437 static int mdc_resource_inode_free(struct ldlm_resource *res)
2439 if (res->lr_lvb_inode)
2440 res->lr_lvb_inode = NULL;
2445 struct ldlm_valblock_ops inode_lvbo = {
2446 lvbo_free: mdc_resource_inode_free
2449 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2451 struct client_obd *cli = &obd->u.cli;
2452 struct lprocfs_static_vars lvars = { 0 };
2456 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2457 if (!cli->cl_rpc_lock)
2459 mdc_init_rpc_lock(cli->cl_rpc_lock);
2463 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2464 if (!cli->cl_close_lock)
2465 GOTO(err_rpc_lock, rc = -ENOMEM);
2466 mdc_init_rpc_lock(cli->cl_close_lock);
2468 rc = client_obd_setup(obd, cfg);
2470 GOTO(err_close_lock, rc);
2471 lprocfs_mdc_init_vars(&lvars);
2472 lprocfs_obd_setup(obd, lvars.obd_vars);
2473 sptlrpc_lprocfs_cliobd_attach(obd);
2474 ptlrpc_lprocfs_register_obd(obd);
2476 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2478 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2480 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2483 CERROR("failed to setup llogging subsystems\n");
2489 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2491 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2496 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2497 * us to make MDS RPCs with large enough reply buffers to hold the
2498 * maximum-sized (= maximum striped) EA and cookie without having to
2499 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2500 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2501 int def_easize, int cookiesize)
2503 struct obd_device *obd = exp->exp_obd;
2504 struct client_obd *cli = &obd->u.cli;
2507 if (cli->cl_max_mds_easize < easize)
2508 cli->cl_max_mds_easize = easize;
2510 if (cli->cl_default_mds_easize < def_easize)
2511 cli->cl_default_mds_easize = def_easize;
2513 if (cli->cl_max_mds_cookiesize < cookiesize)
2514 cli->cl_max_mds_cookiesize = cookiesize;
2519 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2525 case OBD_CLEANUP_EARLY:
2527 case OBD_CLEANUP_EXPORTS:
2528 /* Failsafe, ok if racy */
2529 if (obd->obd_type->typ_refcnt <= 1)
2530 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2532 obd_cleanup_client_import(obd);
2533 ptlrpc_lprocfs_unregister_obd(obd);
2534 lprocfs_obd_cleanup(obd);
2536 rc = obd_llog_finish(obd, 0);
2538 CERROR("failed to cleanup llogging subsystems\n");
2544 static int mdc_cleanup(struct obd_device *obd)
2546 struct client_obd *cli = &obd->u.cli;
2548 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2549 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2553 return client_obd_cleanup(obd);
2557 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2558 struct obd_device *tgt, int *index)
2560 struct llog_ctxt *ctxt;
2565 LASSERT(olg == &obd->obd_olg);
2567 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2572 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2573 llog_initiator_connect(ctxt);
2574 llog_ctxt_put(ctxt);
2579 static int mdc_llog_finish(struct obd_device *obd, int count)
2581 struct llog_ctxt *ctxt;
2585 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2587 llog_cleanup(NULL, ctxt);
2592 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2594 struct lustre_cfg *lcfg = buf;
2595 struct lprocfs_static_vars lvars = { 0 };
2598 lprocfs_mdc_init_vars(&lvars);
2599 switch (lcfg->lcfg_command) {
2601 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2611 /* get remote permission for current user on fid */
2612 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2613 struct obd_capa *oc, __u32 suppgid,
2614 struct ptlrpc_request **request)
2616 struct ptlrpc_request *req;
2620 LASSERT(client_is_remote(exp));
2623 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2627 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2629 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2631 ptlrpc_request_free(req);
2635 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2637 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2638 sizeof(struct mdt_remote_perm));
2640 ptlrpc_request_set_replen(req);
2642 rc = ptlrpc_queue_wait(req);
2644 ptlrpc_req_finished(req);
2650 static int mdc_interpret_renew_capa(const struct lu_env *env,
2651 struct ptlrpc_request *req, void *args,
2654 struct mdc_renew_capa_args *ra = args;
2655 struct mdt_body *body = NULL;
2656 struct lustre_capa *capa;
2660 GOTO(out, capa = ERR_PTR(status));
2662 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2664 GOTO(out, capa = ERR_PTR(-EFAULT));
2666 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2667 GOTO(out, capa = ERR_PTR(-ENOENT));
2669 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2671 GOTO(out, capa = ERR_PTR(-EFAULT));
2674 ra->ra_cb(ra->ra_oc, capa);
2678 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2681 struct ptlrpc_request *req;
2682 struct mdc_renew_capa_args *ra;
2685 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2686 LUSTRE_MDS_VERSION, MDS_GETATTR);
2690 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2691 * capa to renew is oss capa.
2693 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2694 ptlrpc_request_set_replen(req);
2696 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2697 ra = ptlrpc_req_async_args(req);
2700 req->rq_interpret_reply = mdc_interpret_renew_capa;
2701 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2705 static int mdc_connect(const struct lu_env *env,
2706 struct obd_export **exp,
2707 struct obd_device *obd, struct obd_uuid *cluuid,
2708 struct obd_connect_data *data,
2711 struct obd_import *imp = obd->u.cli.cl_import;
2713 /* mds-mds import features */
2714 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2715 spin_lock(&imp->imp_lock);
2716 imp->imp_server_timeout = 1;
2717 spin_unlock(&imp->imp_lock);
2718 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2719 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2723 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2726 struct obd_ops mdc_obd_ops = {
2727 .o_owner = THIS_MODULE,
2728 .o_setup = mdc_setup,
2729 .o_precleanup = mdc_precleanup,
2730 .o_cleanup = mdc_cleanup,
2731 .o_add_conn = client_import_add_conn,
2732 .o_del_conn = client_import_del_conn,
2733 .o_connect = mdc_connect,
2734 .o_disconnect = client_disconnect_export,
2735 .o_iocontrol = mdc_iocontrol,
2736 .o_set_info_async = mdc_set_info_async,
2737 .o_statfs = mdc_statfs,
2739 .o_unpin = mdc_unpin,
2740 .o_fid_init = client_fid_init,
2741 .o_fid_fini = client_fid_fini,
2742 .o_fid_alloc = mdc_fid_alloc,
2743 .o_import_event = mdc_import_event,
2744 .o_llog_init = mdc_llog_init,
2745 .o_llog_finish = mdc_llog_finish,
2746 .o_get_info = mdc_get_info,
2747 .o_process_config = mdc_process_config,
2748 .o_get_uuid = mdc_get_uuid,
2749 .o_quotactl = mdc_quotactl,
2750 .o_quotacheck = mdc_quotacheck
2753 struct md_ops mdc_md_ops = {
2754 .m_getstatus = mdc_getstatus,
2755 .m_null_inode = mdc_null_inode,
2756 .m_find_cbdata = mdc_find_cbdata,
2757 .m_close = mdc_close,
2758 .m_create = mdc_create,
2759 .m_done_writing = mdc_done_writing,
2760 .m_enqueue = mdc_enqueue,
2761 .m_getattr = mdc_getattr,
2762 .m_getattr_name = mdc_getattr_name,
2763 .m_intent_lock = mdc_intent_lock,
2765 .m_is_subdir = mdc_is_subdir,
2766 .m_rename = mdc_rename,
2767 .m_setattr = mdc_setattr,
2768 .m_setxattr = mdc_setxattr,
2769 .m_getxattr = mdc_getxattr,
2771 .m_readpage = mdc_readpage,
2772 .m_unlink = mdc_unlink,
2773 .m_cancel_unused = mdc_cancel_unused,
2774 .m_init_ea_size = mdc_init_ea_size,
2775 .m_set_lock_data = mdc_set_lock_data,
2776 .m_lock_match = mdc_lock_match,
2777 .m_get_lustre_md = mdc_get_lustre_md,
2778 .m_free_lustre_md = mdc_free_lustre_md,
2779 .m_set_open_replay_data = mdc_set_open_replay_data,
2780 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2781 .m_renew_capa = mdc_renew_capa,
2782 .m_unpack_capa = mdc_unpack_capa,
2783 .m_get_remote_perm = mdc_get_remote_perm,
2784 .m_intent_getattr_async = mdc_intent_getattr_async,
2785 .m_revalidate_lock = mdc_revalidate_lock
2788 int __init mdc_init(void)
2791 struct lprocfs_static_vars lvars = { 0 };
2792 lprocfs_mdc_init_vars(&lvars);
2794 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2795 LUSTRE_MDC_NAME, NULL);
2800 static void /*__exit*/ mdc_exit(void)
2802 class_unregister_type(LUSTRE_MDC_NAME);
2805 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2806 MODULE_DESCRIPTION("Lustre Metadata Client");
2807 MODULE_LICENSE("GPL");
2809 module_init(mdc_init);
2810 module_exit(mdc_exit);