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>
44 # include <linux/utsname.h>
46 # include <liblustre.h>
49 #include <lustre_acl.h>
50 #include <obd_class.h>
51 #include <lustre_fid.h>
52 #include <lprocfs_status.h>
53 #include <lustre_param.h>
54 #include <lustre_log.h>
56 #include "mdc_internal.h"
58 #define REQUEST_MINOR 244
60 struct mdc_renew_capa_args {
61 struct obd_capa *ra_oc;
62 renew_capa_cb_t ra_cb;
65 static int mdc_cleanup(struct obd_device *obd);
67 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
68 const struct req_msg_field *field, struct obd_capa **oc)
70 struct lustre_capa *capa;
74 /* swabbed already in mdc_enqueue */
75 capa = req_capsule_server_get(&req->rq_pill, field);
79 c = alloc_capa(CAPA_SITE_CLIENT);
81 CDEBUG(D_INFO, "alloc capa failed!\n");
90 static inline int mdc_queue_wait(struct ptlrpc_request *req)
92 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
95 /* mdc_enter_request() ensures that this client has no more
96 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
98 rc = mdc_enter_request(cli);
102 rc = ptlrpc_queue_wait(req);
103 mdc_exit_request(cli);
108 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
109 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
110 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
111 struct obd_capa **pc, int level, int msg_flags)
113 struct ptlrpc_request *req;
114 struct mdt_body *body;
118 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
119 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
123 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
124 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
125 req->rq_send_state = level;
127 ptlrpc_request_set_replen(req);
129 rc = ptlrpc_queue_wait(req);
133 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
135 GOTO(out, rc = -EPROTO);
137 if (body->valid & OBD_MD_FLMDSCAPA) {
138 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
143 *rootfid = body->fid1;
145 "root fid="DFID", last_committed="LPU64"\n",
147 lustre_msg_get_last_committed(req->rq_repmsg));
150 ptlrpc_req_finished(req);
154 /* This should be mdc_get_info("rootfid") */
155 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
156 struct obd_capa **pc)
158 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
163 * This function now is known to always saying that it will receive 4 buffers
164 * from server. Even for cases when acl_size and md_size is zero, RPC header
165 * will contain 4 fields and RPC itself will contain zero size fields. This is
166 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
167 * and thus zero, it shrinks it, making zero size. The same story about
168 * md_size. And this is course of problem when client waits for smaller number
169 * of fields. This issue will be fixed later when client gets aware of RPC
172 static int mdc_getattr_common(struct obd_export *exp,
173 struct ptlrpc_request *req)
175 struct req_capsule *pill = &req->rq_pill;
176 struct mdt_body *body;
181 /* Request message already built. */
182 rc = ptlrpc_queue_wait(req);
186 /* sanity check for the reply */
187 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
191 CDEBUG(D_NET, "mode: %o\n", body->mode);
193 if (body->eadatasize != 0) {
194 mdc_update_max_ea_from_body(exp, body);
196 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
202 if (body->valid & OBD_MD_FLRMTPERM) {
203 struct mdt_remote_perm *perm;
205 LASSERT(client_is_remote(exp));
206 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
207 lustre_swab_mdt_remote_perm);
212 if (body->valid & OBD_MD_FLMDSCAPA) {
213 struct lustre_capa *capa;
214 capa = req_capsule_server_get(pill, &RMF_CAPA1);
222 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
223 struct ptlrpc_request **request)
225 struct ptlrpc_request *req;
229 /* Single MDS without an LMV case */
230 if (op_data->op_flags & MF_GET_MDT_IDX) {
235 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
239 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
241 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
243 ptlrpc_request_free(req);
247 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
248 op_data->op_valid, op_data->op_mode, -1, 0);
250 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
252 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
253 LASSERT(client_is_remote(exp));
254 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
255 sizeof(struct mdt_remote_perm));
257 ptlrpc_request_set_replen(req);
259 rc = mdc_getattr_common(exp, req);
261 ptlrpc_req_finished(req);
267 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
268 struct ptlrpc_request **request)
270 struct ptlrpc_request *req;
275 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
276 &RQF_MDS_GETATTR_NAME);
280 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
281 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
282 op_data->op_namelen + 1);
284 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
286 ptlrpc_request_free(req);
290 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
291 op_data->op_valid, op_data->op_mode,
292 op_data->op_suppgids[0], 0);
294 if (op_data->op_name) {
295 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
296 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
297 op_data->op_namelen);
298 memcpy(name, op_data->op_name, op_data->op_namelen);
301 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
303 ptlrpc_request_set_replen(req);
305 rc = mdc_getattr_common(exp, req);
307 ptlrpc_req_finished(req);
313 static int mdc_is_subdir(struct obd_export *exp,
314 const struct lu_fid *pfid,
315 const struct lu_fid *cfid,
316 struct ptlrpc_request **request)
318 struct ptlrpc_request *req;
324 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
325 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
330 mdc_is_subdir_pack(req, pfid, cfid, 0);
331 ptlrpc_request_set_replen(req);
333 rc = ptlrpc_queue_wait(req);
334 if (rc && rc != -EREMOTE)
335 ptlrpc_req_finished(req);
341 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
342 const struct lu_fid *fid,
343 struct obd_capa *oc, int opcode, obd_valid valid,
344 const char *xattr_name, const char *input,
345 int input_size, int output_size, int flags,
346 __u32 suppgid, struct ptlrpc_request **request)
348 struct ptlrpc_request *req;
349 int xattr_namelen = 0;
355 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
359 mdc_set_capa_size(req, &RMF_CAPA1, oc);
361 xattr_namelen = strlen(xattr_name) + 1;
362 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
367 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
371 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
373 ptlrpc_request_free(req);
377 if (opcode == MDS_REINT) {
378 struct mdt_rec_setxattr *rec;
380 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
381 sizeof(struct mdt_rec_reint));
382 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
383 rec->sx_opcode = REINT_SETXATTR;
385 * cfs_curproc_fs{u,g}id() should replace
386 * current->fs{u,g}id for portability.
388 rec->sx_fsuid = cfs_curproc_fsuid();
389 rec->sx_fsgid = cfs_curproc_fsgid();
390 rec->sx_cap = cfs_curproc_cap_pack();
391 rec->sx_suppgid1 = suppgid;
392 rec->sx_suppgid2 = -1;
394 rec->sx_valid = valid | OBD_MD_FLCTIME;
395 rec->sx_time = cfs_time_current_sec();
396 rec->sx_size = output_size;
397 rec->sx_flags = flags;
399 mdc_pack_capa(req, &RMF_CAPA1, oc);
401 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
405 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
406 memcpy(tmp, xattr_name, xattr_namelen);
409 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
410 memcpy(tmp, input, input_size);
413 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
414 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
415 RCL_SERVER, output_size);
416 ptlrpc_request_set_replen(req);
419 if (opcode == MDS_REINT)
420 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
422 rc = ptlrpc_queue_wait(req);
424 if (opcode == MDS_REINT)
425 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
428 ptlrpc_req_finished(req);
434 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
435 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
436 const char *input, int input_size, int output_size,
437 int flags, __u32 suppgid, struct ptlrpc_request **request)
439 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
440 fid, oc, MDS_REINT, valid, xattr_name,
441 input, input_size, output_size, flags,
445 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
446 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
447 const char *input, int input_size, int output_size,
448 int flags, struct ptlrpc_request **request)
450 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
451 fid, oc, MDS_GETXATTR, valid, xattr_name,
452 input, input_size, output_size, flags,
456 #ifdef CONFIG_FS_POSIX_ACL
457 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
459 struct req_capsule *pill = &req->rq_pill;
460 struct mdt_body *body = md->body;
461 struct posix_acl *acl;
469 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
474 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
477 CERROR("convert xattr to acl: %d\n", rc);
481 rc = posix_acl_valid(acl);
483 CERROR("validate acl: %d\n", rc);
484 posix_acl_release(acl);
492 #define mdc_unpack_acl(req, md) 0
495 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
496 struct obd_export *dt_exp, struct obd_export *md_exp,
497 struct lustre_md *md)
499 struct req_capsule *pill = &req->rq_pill;
504 memset(md, 0, sizeof(*md));
506 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
507 LASSERT(md->body != NULL);
509 if (md->body->valid & OBD_MD_FLEASIZE) {
511 struct lov_mds_md *lmm;
513 if (!S_ISREG(md->body->mode)) {
514 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
515 "regular file, but is not\n");
516 GOTO(out, rc = -EPROTO);
519 if (md->body->eadatasize == 0) {
520 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
521 "but eadatasize 0\n");
522 GOTO(out, rc = -EPROTO);
524 lmmsize = md->body->eadatasize;
525 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
527 GOTO(out, rc = -EPROTO);
529 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
533 if (rc < sizeof(*md->lsm)) {
534 CDEBUG(D_INFO, "lsm size too small: "
535 "rc < sizeof (*md->lsm) (%d < %d)\n",
536 rc, (int)sizeof(*md->lsm));
537 GOTO(out, rc = -EPROTO);
540 } else if (md->body->valid & OBD_MD_FLDIREA) {
542 struct lov_mds_md *lmv;
544 if(!S_ISDIR(md->body->mode)) {
545 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
546 "directory, but is not\n");
547 GOTO(out, rc = -EPROTO);
550 if (md->body->eadatasize == 0) {
551 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
552 "but eadatasize 0\n");
555 if (md->body->valid & OBD_MD_MEA) {
556 lmvsize = md->body->eadatasize;
557 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
560 GOTO(out, rc = -EPROTO);
562 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
567 if (rc < sizeof(*md->mea)) {
568 CDEBUG(D_INFO, "size too small: "
569 "rc < sizeof(*md->mea) (%d < %d)\n",
570 rc, (int)sizeof(*md->mea));
571 GOTO(out, rc = -EPROTO);
577 if (md->body->valid & OBD_MD_FLRMTPERM) {
578 /* remote permission */
579 LASSERT(client_is_remote(exp));
580 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
581 lustre_swab_mdt_remote_perm);
582 if (!md->remote_perm)
583 GOTO(out, rc = -EPROTO);
585 else if (md->body->valid & OBD_MD_FLACL) {
586 /* for ACL, it's possible that FLACL is set but aclsize is zero.
587 * only when aclsize != 0 there's an actual segment for ACL
590 if (md->body->aclsize) {
591 rc = mdc_unpack_acl(req, md);
594 #ifdef CONFIG_FS_POSIX_ACL
596 md->posix_acl = NULL;
600 if (md->body->valid & OBD_MD_FLMDSCAPA) {
601 struct obd_capa *oc = NULL;
603 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
609 if (md->body->valid & OBD_MD_FLOSSCAPA) {
610 struct obd_capa *oc = NULL;
612 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
622 capa_put(md->oss_capa);
626 capa_put(md->mds_capa);
629 #ifdef CONFIG_FS_POSIX_ACL
630 posix_acl_release(md->posix_acl);
633 obd_free_memmd(dt_exp, &md->lsm);
638 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
645 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
648 void mdc_replay_open(struct ptlrpc_request *req)
650 struct md_open_data *mod = req->rq_cb_data;
651 struct ptlrpc_request *close_req;
652 struct obd_client_handle *och;
653 struct lustre_handle old;
654 struct mdt_body *body;
658 DEBUG_REQ(D_ERROR, req,
659 "Can't properly replay without open data.");
664 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
665 LASSERT(body != NULL);
669 struct lustre_handle *file_fh;
671 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
673 file_fh = &och->och_fh;
674 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
675 file_fh->cookie, body->handle.cookie);
677 *file_fh = body->handle;
679 close_req = mod->mod_close_req;
680 if (close_req != NULL) {
681 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
682 struct mdt_ioepoch *epoch;
684 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
685 epoch = req_capsule_client_get(&close_req->rq_pill,
690 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
691 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
692 epoch->handle = body->handle;
697 void mdc_commit_open(struct ptlrpc_request *req)
699 struct md_open_data *mod = req->rq_cb_data;
704 * No need to touch md_open_data::mod_och, it holds a reference on
705 * \var mod and will zero references to each other, \var mod will be
706 * freed after that when md_open_data::mod_och will put the reference.
710 * Do not let open request to disappear as it still may be needed
711 * for close rpc to happen (it may happen on evict only, otherwise
712 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
713 * called), just mark this rpc as committed to distinguish these 2
714 * cases, see mdc_close() for details. The open request reference will
715 * be put along with freeing \var mod.
717 ptlrpc_request_addref(req);
718 spin_lock(&req->rq_lock);
719 req->rq_committed = 1;
720 spin_unlock(&req->rq_lock);
721 req->rq_cb_data = NULL;
725 int mdc_set_open_replay_data(struct obd_export *exp,
726 struct obd_client_handle *och,
727 struct ptlrpc_request *open_req)
729 struct md_open_data *mod;
730 struct mdt_rec_create *rec;
731 struct mdt_body *body;
732 struct obd_import *imp = open_req->rq_import;
735 if (!open_req->rq_replay)
738 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
739 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
740 LASSERT(rec != NULL);
741 /* Incoming message in my byte order (it's been swabbed). */
742 /* Outgoing messages always in my byte order. */
743 LASSERT(body != NULL);
745 /* Only if the import is replayable, we set replay_open data */
746 if (och && imp->imp_replayable) {
747 mod = obd_mod_alloc();
749 DEBUG_REQ(D_ERROR, open_req,
750 "Can't allocate md_open_data");
755 * Take a reference on \var mod, to be freed on mdc_close().
756 * It protects \var mod from being freed on eviction (commit
757 * callback is called despite rq_replay flag).
758 * Another reference for \var och.
763 spin_lock(&open_req->rq_lock);
766 mod->mod_open_req = open_req;
767 open_req->rq_cb_data = mod;
768 open_req->rq_commit_cb = mdc_commit_open;
769 spin_unlock(&open_req->rq_lock);
772 rec->cr_fid2 = body->fid1;
773 rec->cr_ioepoch = body->ioepoch;
774 rec->cr_old_handle.cookie = body->handle.cookie;
775 open_req->rq_replay_cb = mdc_replay_open;
776 if (!fid_is_sane(&body->fid1)) {
777 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
782 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
786 int mdc_clear_open_replay_data(struct obd_export *exp,
787 struct obd_client_handle *och)
789 struct md_open_data *mod = och->och_mod;
793 * It is possible to not have \var mod in a case of eviction between
794 * lookup and ll_file_open().
799 LASSERT(mod != LP_POISON);
808 /* Prepares the request for the replay by the given reply */
809 static void mdc_close_handle_reply(struct ptlrpc_request *req,
810 struct md_op_data *op_data, int rc) {
811 struct mdt_body *repbody;
812 struct mdt_ioepoch *epoch;
814 if (req && rc == -EAGAIN) {
815 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
816 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
818 epoch->flags |= MF_SOM_AU;
819 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
820 op_data->op_flags |= MF_GETATTR_LOCK;
824 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
825 struct md_open_data *mod, struct ptlrpc_request **request)
827 struct obd_device *obd = class_exp2obd(exp);
828 struct ptlrpc_request *req;
833 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_CLOSE);
837 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
839 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
841 ptlrpc_request_free(req);
845 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
846 * portal whose threads are not taking any DLM locks and are therefore
847 * always progressing */
848 req->rq_request_portal = MDS_READPAGE_PORTAL;
849 ptlrpc_at_set_req_timeout(req);
851 /* Ensure that this close's handle is fixed up during replay. */
852 if (likely(mod != NULL)) {
853 LASSERTF(mod->mod_open_req != NULL &&
854 mod->mod_open_req->rq_type != LI_POISON,
855 "POISONED open %p!\n", mod->mod_open_req);
857 mod->mod_close_req = req;
859 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
860 /* We no longer want to preserve this open for replay even
861 * though the open was committed. b=3632, b=3633 */
862 spin_lock(&mod->mod_open_req->rq_lock);
863 mod->mod_open_req->rq_replay = 0;
864 spin_unlock(&mod->mod_open_req->rq_lock);
866 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
869 mdc_close_pack(req, op_data);
871 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
872 obd->u.cli.cl_max_mds_easize);
873 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
874 obd->u.cli.cl_max_mds_cookiesize);
876 ptlrpc_request_set_replen(req);
878 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
879 rc = ptlrpc_queue_wait(req);
880 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
882 if (req->rq_repmsg == NULL) {
883 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
886 rc = req->rq_status ?: -EIO;
887 } else if (rc == 0 || rc == -EAGAIN) {
888 struct mdt_body *body;
890 rc = lustre_msg_get_status(req->rq_repmsg);
891 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
892 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
897 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
900 } else if (rc == -ESTALE) {
902 * it can be allowed error after 3633 if open was committed and
903 * server failed before close was sent. Let's check if mod
904 * exists and return no error in that case
907 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
908 LASSERT(mod->mod_open_req != NULL);
909 if (mod->mod_open_req->rq_committed)
916 mod->mod_close_req = NULL;
917 /* Since now, mod is accessed through open_req only,
918 * thus close req does not keep a reference on mod anymore. */
922 mdc_close_handle_reply(req, op_data, rc);
926 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
927 struct md_open_data *mod)
929 struct obd_device *obd = class_exp2obd(exp);
930 struct ptlrpc_request *req;
934 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
935 &RQF_MDS_DONE_WRITING);
939 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
940 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
942 ptlrpc_request_free(req);
947 LASSERTF(mod->mod_open_req != NULL &&
948 mod->mod_open_req->rq_type != LI_POISON,
949 "POISONED setattr %p!\n", mod->mod_open_req);
951 mod->mod_close_req = req;
952 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
953 /* We no longer want to preserve this setattr for replay even
954 * though the open was committed. b=3632, b=3633 */
955 spin_lock(&mod->mod_open_req->rq_lock);
956 mod->mod_open_req->rq_replay = 0;
957 spin_unlock(&mod->mod_open_req->rq_lock);
960 mdc_close_pack(req, op_data);
961 ptlrpc_request_set_replen(req);
963 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
964 rc = ptlrpc_queue_wait(req);
965 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
969 * it can be allowed error after 3633 if open or setattr were
970 * committed and server failed before close was sent.
971 * Let's check if mod exists and return no error in that case
974 LASSERT(mod->mod_open_req != NULL);
975 if (mod->mod_open_req->rq_committed)
982 mod->mod_close_req = NULL;
983 /* Since now, mod is accessed through setattr req only,
984 * thus DW req does not keep a reference on mod anymore. */
988 mdc_close_handle_reply(req, op_data, rc);
989 ptlrpc_req_finished(req);
993 #ifdef HAVE_SPLIT_SUPPORT
994 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
995 const struct page *page, int offset)
997 struct ptlrpc_request *req;
998 struct ptlrpc_bulk_desc *desc;
1002 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1006 /* FIXME: capa doesn't support split yet */
1007 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1009 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1011 ptlrpc_request_free(req);
1015 req->rq_request_portal = MDS_READPAGE_PORTAL;
1016 ptlrpc_at_set_req_timeout(req);
1018 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1020 GOTO(out, rc = -ENOMEM);
1022 /* NB req now owns desc and will free it when it gets freed. */
1023 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1024 mdc_readdir_pack(req, 0, offset, fid, NULL);
1026 ptlrpc_request_set_replen(req);
1027 rc = ptlrpc_queue_wait(req);
1031 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1033 ptlrpc_req_finished(req);
1036 EXPORT_SYMBOL(mdc_sendpage);
1039 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1040 struct page **pages, struct ptlrpc_request **request)
1042 struct ptlrpc_request *req;
1043 struct ptlrpc_bulk_desc *desc;
1047 struct l_wait_info lwi;
1052 cfs_waitq_init(&waitq);
1055 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1059 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1061 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1063 ptlrpc_request_free(req);
1067 req->rq_request_portal = MDS_READPAGE_PORTAL;
1068 ptlrpc_at_set_req_timeout(req);
1070 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1073 ptlrpc_request_free(req);
1077 /* NB req now owns desc and will free it when it gets freed */
1078 for (i = 0; i < op_data->op_npages; i++)
1079 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1081 mdc_readdir_pack(req, op_data->op_offset,
1082 PAGE_CACHE_SIZE * op_data->op_npages,
1083 &op_data->op_fid1, op_data->op_capa1);
1085 ptlrpc_request_set_replen(req);
1086 rc = ptlrpc_queue_wait(req);
1088 ptlrpc_req_finished(req);
1089 if (rc != -ETIMEDOUT)
1093 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1094 CERROR("too many resend retries, returning error\n");
1097 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1098 l_wait_event(waitq, 0, &lwi);
1103 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1104 req->rq_bulk->bd_nob_transferred);
1106 ptlrpc_req_finished(req);
1110 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1111 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1112 req->rq_bulk->bd_nob_transferred,
1113 PAGE_CACHE_SIZE * op_data->op_npages);
1114 ptlrpc_req_finished(req);
1122 static int mdc_statfs(const struct lu_env *env,
1123 struct obd_export *exp, struct obd_statfs *osfs,
1124 __u64 max_age, __u32 flags)
1126 struct obd_device *obd = class_exp2obd(exp);
1127 struct ptlrpc_request *req;
1128 struct obd_statfs *msfs;
1129 struct obd_import *imp = NULL;
1134 * Since the request might also come from lprocfs, so we need
1135 * sync this with client_disconnect_export Bug15684
1137 down_read(&obd->u.cli.cl_sem);
1138 if (obd->u.cli.cl_import)
1139 imp = class_import_get(obd->u.cli.cl_import);
1140 up_read(&obd->u.cli.cl_sem);
1144 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1145 LUSTRE_MDS_VERSION, MDS_STATFS);
1147 GOTO(output, rc = -ENOMEM);
1149 ptlrpc_request_set_replen(req);
1151 if (flags & OBD_STATFS_NODELAY) {
1152 /* procfs requests not want stay in wait for avoid deadlock */
1153 req->rq_no_resend = 1;
1154 req->rq_no_delay = 1;
1157 rc = ptlrpc_queue_wait(req);
1159 /* check connection error first */
1160 if (imp->imp_connect_error)
1161 rc = imp->imp_connect_error;
1165 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1167 GOTO(out, rc = -EPROTO);
1172 ptlrpc_req_finished(req);
1174 class_import_put(imp);
1178 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1180 __u32 keylen, vallen;
1184 if (gf->gf_pathlen > PATH_MAX)
1185 RETURN(-ENAMETOOLONG);
1186 if (gf->gf_pathlen < 2)
1189 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1190 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1191 OBD_ALLOC(key, keylen);
1194 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1195 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1197 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1198 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1200 if (!fid_is_sane(&gf->gf_fid))
1201 GOTO(out, rc = -EINVAL);
1203 /* Val is struct getinfo_fid2path result plus path */
1204 vallen = sizeof(*gf) + gf->gf_pathlen;
1206 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1207 if (rc != 0 && rc != -EREMOTE)
1210 if (vallen <= sizeof(*gf))
1211 GOTO(out, rc = -EPROTO);
1212 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1213 GOTO(out, rc = -EOVERFLOW);
1215 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1216 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1219 OBD_FREE(key, keylen);
1223 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1224 struct hsm_progress_kernel *hpk)
1226 struct obd_import *imp = class_exp2cliimp(exp);
1227 struct hsm_progress_kernel *req_hpk;
1228 struct ptlrpc_request *req;
1232 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1233 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1235 GOTO(out, rc = -ENOMEM);
1237 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1239 /* Copy hsm_progress struct */
1240 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1241 if (req_hpk == NULL)
1242 GOTO(out, rc = -EPROTO);
1245 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1247 ptlrpc_request_set_replen(req);
1249 rc = mdc_queue_wait(req);
1252 ptlrpc_req_finished(req);
1256 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1258 __u32 *archive_mask;
1259 struct ptlrpc_request *req;
1263 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1265 MDS_HSM_CT_REGISTER);
1267 GOTO(out, rc = -ENOMEM);
1269 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1271 /* Copy hsm_progress struct */
1272 archive_mask = req_capsule_client_get(&req->rq_pill,
1273 &RMF_MDS_HSM_ARCHIVE);
1274 if (archive_mask == NULL)
1275 GOTO(out, rc = -EPROTO);
1277 *archive_mask = archives;
1279 ptlrpc_request_set_replen(req);
1281 rc = mdc_queue_wait(req);
1284 ptlrpc_req_finished(req);
1288 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1289 struct md_op_data *op_data)
1291 struct hsm_current_action *hca = op_data->op_data;
1292 struct hsm_current_action *req_hca;
1293 struct ptlrpc_request *req;
1297 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1298 &RQF_MDS_HSM_ACTION);
1302 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1304 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1306 ptlrpc_request_free(req);
1310 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1311 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1313 ptlrpc_request_set_replen(req);
1315 rc = mdc_queue_wait(req);
1319 req_hca = req_capsule_server_get(&req->rq_pill,
1320 &RMF_MDS_HSM_CURRENT_ACTION);
1321 if (req_hca == NULL)
1322 GOTO(out, rc = -EPROTO);
1328 ptlrpc_req_finished(req);
1332 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1334 struct ptlrpc_request *req;
1338 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1340 MDS_HSM_CT_UNREGISTER);
1342 GOTO(out, rc = -ENOMEM);
1344 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1346 ptlrpc_request_set_replen(req);
1348 rc = mdc_queue_wait(req);
1351 ptlrpc_req_finished(req);
1355 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1356 struct md_op_data *op_data)
1358 struct hsm_user_state *hus = op_data->op_data;
1359 struct hsm_user_state *req_hus;
1360 struct ptlrpc_request *req;
1364 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1365 &RQF_MDS_HSM_STATE_GET);
1369 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1371 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1373 ptlrpc_request_free(req);
1377 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1378 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1380 ptlrpc_request_set_replen(req);
1382 rc = mdc_queue_wait(req);
1386 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1387 if (req_hus == NULL)
1388 GOTO(out, rc = -EPROTO);
1394 ptlrpc_req_finished(req);
1398 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1399 struct md_op_data *op_data)
1401 struct hsm_state_set *hss = op_data->op_data;
1402 struct hsm_state_set *req_hss;
1403 struct ptlrpc_request *req;
1407 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1408 &RQF_MDS_HSM_STATE_SET);
1412 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1414 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1416 ptlrpc_request_free(req);
1420 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1421 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1424 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1425 if (req_hss == NULL)
1426 GOTO(out, rc = -EPROTO);
1429 ptlrpc_request_set_replen(req);
1431 rc = mdc_queue_wait(req);
1436 ptlrpc_req_finished(req);
1440 static int mdc_ioc_hsm_request(struct obd_export *exp,
1441 struct hsm_user_request *hur)
1443 struct obd_import *imp = class_exp2cliimp(exp);
1444 struct ptlrpc_request *req;
1445 struct hsm_request *req_hr;
1446 struct hsm_user_item *req_hui;
1451 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1453 GOTO(out, rc = -ENOMEM);
1455 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1456 hur->hur_request.hr_itemcount
1457 * sizeof(struct hsm_user_item));
1458 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1459 hur->hur_request.hr_data_len);
1461 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1463 ptlrpc_request_free(req);
1467 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1469 /* Copy hsm_request struct */
1470 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1472 GOTO(out, rc = -EPROTO);
1473 *req_hr = hur->hur_request;
1475 /* Copy hsm_user_item structs */
1476 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1477 if (req_hui == NULL)
1478 GOTO(out, rc = -EPROTO);
1479 memcpy(req_hui, hur->hur_user_item,
1480 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1482 /* Copy opaque field */
1483 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1484 if (req_opaque == NULL)
1485 GOTO(out, rc = -EPROTO);
1486 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1488 ptlrpc_request_set_replen(req);
1490 rc = mdc_queue_wait(req);
1494 ptlrpc_req_finished(req);
1498 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1500 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1502 LASSERT(len <= CR_MAXSIZE);
1504 lh->kuc_magic = KUC_MAGIC;
1505 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1506 lh->kuc_flags = flags;
1507 lh->kuc_msgtype = CL_RECORD;
1508 lh->kuc_msglen = len;
1512 #define D_CHANGELOG 0
1514 struct changelog_show {
1519 struct obd_device *cs_obd;
1522 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1523 struct llog_rec_hdr *hdr, void *data)
1525 struct changelog_show *cs = data;
1526 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1531 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1533 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1534 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1535 rec->cr.cr_type, rc);
1539 if (rec->cr.cr_index < cs->cs_startrec) {
1540 /* Skip entries earlier than what we are interested in */
1541 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1542 rec->cr.cr_index, cs->cs_startrec);
1546 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1547 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1548 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1549 rec->cr.cr_flags & CLF_FLAGMASK,
1550 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1551 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1553 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1555 /* Set up the message */
1556 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1557 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1559 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1560 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1565 static int mdc_changelog_send_thread(void *csdata)
1567 struct changelog_show *cs = csdata;
1568 struct llog_ctxt *ctxt = NULL;
1569 struct llog_handle *llh = NULL;
1570 struct kuc_hdr *kuch;
1573 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1574 cs->cs_fp, cs->cs_startrec);
1576 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1577 if (cs->cs_buf == NULL)
1578 GOTO(out, rc = -ENOMEM);
1580 /* Set up the remote catalog handle */
1581 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1583 GOTO(out, rc = -ENOENT);
1584 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1587 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1588 cs->cs_obd->obd_name, rc);
1591 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1593 CERROR("llog_init_handle failed %d\n", rc);
1597 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1599 /* Send EOF no matter what our result */
1600 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1602 kuch->kuc_msgtype = CL_EOF;
1603 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1609 llog_cat_close(NULL, llh);
1611 llog_ctxt_put(ctxt);
1613 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1618 static int mdc_ioc_changelog_send(struct obd_device *obd,
1619 struct ioc_changelog *icc)
1621 struct changelog_show *cs;
1624 /* Freed in mdc_changelog_send_thread */
1630 cs->cs_startrec = icc->icc_recno;
1631 /* matching fput in mdc_changelog_send_thread */
1632 cs->cs_fp = fget(icc->icc_id);
1633 cs->cs_flags = icc->icc_flags;
1636 * New thread because we should return to user app before
1637 * writing into our pipe
1639 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1640 "mdc_clg_send_thread"));
1641 if (!IS_ERR_VALUE(rc)) {
1642 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1646 CERROR("Failed to start changelog thread: %d\n", rc);
1651 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1652 struct lustre_kernelcomm *lk);
1654 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1655 struct obd_quotactl *oqctl)
1657 struct client_obd *cli = &exp->exp_obd->u.cli;
1658 struct ptlrpc_request *req;
1659 struct obd_quotactl *body;
1663 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1664 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1669 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1672 ptlrpc_request_set_replen(req);
1674 /* the next poll will find -ENODATA, that means quotacheck is
1676 cli->cl_qchk_stat = -ENODATA;
1677 rc = ptlrpc_queue_wait(req);
1679 cli->cl_qchk_stat = rc;
1680 ptlrpc_req_finished(req);
1684 static int mdc_quota_poll_check(struct obd_export *exp,
1685 struct if_quotacheck *qchk)
1687 struct client_obd *cli = &exp->exp_obd->u.cli;
1691 qchk->obd_uuid = cli->cl_target_uuid;
1692 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1694 rc = cli->cl_qchk_stat;
1695 /* the client is not the previous one */
1696 if (rc == CL_NOT_QUOTACHECKED)
1701 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1702 struct obd_quotactl *oqctl)
1704 struct ptlrpc_request *req;
1705 struct obd_quotactl *oqc;
1709 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1710 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1715 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1718 ptlrpc_request_set_replen(req);
1719 ptlrpc_at_set_req_timeout(req);
1720 req->rq_no_resend = 1;
1722 rc = ptlrpc_queue_wait(req);
1724 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1726 if (req->rq_repmsg &&
1727 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1730 CERROR ("Can't unpack obd_quotactl\n");
1733 ptlrpc_req_finished(req);
1738 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1739 struct md_op_data *op_data)
1741 CFS_LIST_HEAD(cancels);
1742 struct ptlrpc_request *req;
1744 struct mdc_swap_layouts *msl, *payload;
1747 msl = op_data->op_data;
1749 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1750 * first thing it will do is to cancel the 2 layout
1751 * locks hold by this client.
1752 * So the client must cancel its layout locks on the 2 fids
1753 * with the request RPC to avoid extra RPC round trips
1755 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1756 LCK_CR, MDS_INODELOCK_LAYOUT);
1757 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1758 LCK_CR, MDS_INODELOCK_LAYOUT);
1760 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1761 &RQF_MDS_SWAP_LAYOUTS);
1763 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1767 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1768 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1770 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1772 ptlrpc_request_free(req);
1776 mdc_swap_layouts_pack(req, op_data);
1778 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1783 ptlrpc_request_set_replen(req);
1785 rc = ptlrpc_queue_wait(req);
1791 ptlrpc_req_finished(req);
1795 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1796 void *karg, void *uarg)
1798 struct obd_device *obd = exp->exp_obd;
1799 struct obd_ioctl_data *data = karg;
1800 struct obd_import *imp = obd->u.cli.cl_import;
1801 struct llog_ctxt *ctxt;
1805 if (!cfs_try_module_get(THIS_MODULE)) {
1806 CERROR("Can't get module. Is it alive?");
1810 case OBD_IOC_CHANGELOG_SEND:
1811 rc = mdc_ioc_changelog_send(obd, karg);
1813 case OBD_IOC_CHANGELOG_CLEAR: {
1814 struct ioc_changelog *icc = karg;
1815 struct changelog_setinfo cs =
1816 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1817 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1818 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1822 case OBD_IOC_FID2PATH:
1823 rc = mdc_ioc_fid2path(exp, karg);
1825 case LL_IOC_HSM_CT_START:
1826 rc = mdc_ioc_hsm_ct_start(exp, karg);
1827 /* ignore if it was already registered on this MDS. */
1831 case LL_IOC_HSM_PROGRESS:
1832 rc = mdc_ioc_hsm_progress(exp, karg);
1834 case LL_IOC_HSM_STATE_GET:
1835 rc = mdc_ioc_hsm_state_get(exp, karg);
1837 case LL_IOC_HSM_STATE_SET:
1838 rc = mdc_ioc_hsm_state_set(exp, karg);
1839 case LL_IOC_HSM_ACTION:
1840 rc = mdc_ioc_hsm_current_action(exp, karg);
1842 case LL_IOC_HSM_REQUEST:
1843 rc = mdc_ioc_hsm_request(exp, karg);
1845 case OBD_IOC_CLIENT_RECOVER:
1846 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1850 case IOC_OSC_SET_ACTIVE:
1851 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1853 case OBD_IOC_PARSE: {
1854 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1855 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1857 llog_ctxt_put(ctxt);
1861 case OBD_IOC_LLOG_INFO:
1862 case OBD_IOC_LLOG_PRINT: {
1863 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1864 rc = llog_ioctl(NULL, ctxt, cmd, data);
1865 llog_ctxt_put(ctxt);
1869 case OBD_IOC_POLL_QUOTACHECK:
1870 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1872 case OBD_IOC_PING_TARGET:
1873 rc = ptlrpc_obd_ping(obd);
1876 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1877 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1878 * there'd be no LMV layer thus we might be called here. Eventually
1879 * this code should be removed.
1882 case IOC_OBD_STATFS: {
1883 struct obd_statfs stat_buf = {0};
1885 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1886 GOTO(out, rc = -ENODEV);
1889 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1890 min((int)data->ioc_plen2,
1891 (int)sizeof(struct obd_uuid))))
1892 GOTO(out, rc = -EFAULT);
1894 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1895 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1900 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1901 min((int) data->ioc_plen1,
1902 (int) sizeof(stat_buf))))
1903 GOTO(out, rc = -EFAULT);
1907 case OBD_IOC_QUOTACTL: {
1908 struct if_quotactl *qctl = karg;
1909 struct obd_quotactl *oqctl;
1911 OBD_ALLOC_PTR(oqctl);
1915 QCTL_COPY(oqctl, qctl);
1916 rc = obd_quotactl(exp, oqctl);
1918 QCTL_COPY(qctl, oqctl);
1919 qctl->qc_valid = QC_MDTIDX;
1920 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1922 OBD_FREE_PTR(oqctl);
1925 case LL_IOC_GET_CONNECT_FLAGS: {
1926 if (copy_to_user(uarg,
1927 exp_connect_flags_ptr(exp),
1929 GOTO(out, rc = -EFAULT);
1933 case LL_IOC_LOV_SWAP_LAYOUTS: {
1934 rc = mdc_ioc_swap_layouts(exp, karg);
1938 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1939 GOTO(out, rc = -ENOTTY);
1942 cfs_module_put(THIS_MODULE);
1947 int mdc_get_info_rpc(struct obd_export *exp,
1948 obd_count keylen, void *key,
1949 int vallen, void *val)
1951 struct obd_import *imp = class_exp2cliimp(exp);
1952 struct ptlrpc_request *req;
1957 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1961 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1962 RCL_CLIENT, keylen);
1963 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1964 RCL_CLIENT, sizeof(__u32));
1966 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1968 ptlrpc_request_free(req);
1972 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1973 memcpy(tmp, key, keylen);
1974 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1975 memcpy(tmp, &vallen, sizeof(__u32));
1977 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1978 RCL_SERVER, vallen);
1979 ptlrpc_request_set_replen(req);
1981 rc = ptlrpc_queue_wait(req);
1982 /* -EREMOTE means the get_info result is partial, and it needs to
1983 * continue on another MDT, see fid2path part in lmv_iocontrol */
1984 if (rc == 0 || rc == -EREMOTE) {
1985 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1986 memcpy(val, tmp, vallen);
1987 if (ptlrpc_rep_need_swab(req)) {
1988 if (KEY_IS(KEY_FID2PATH))
1989 lustre_swab_fid2path(val);
1992 ptlrpc_req_finished(req);
1997 static void lustre_swab_hai(struct hsm_action_item *h)
1999 __swab32s(&h->hai_len);
2000 __swab32s(&h->hai_action);
2001 lustre_swab_lu_fid(&h->hai_fid);
2002 lustre_swab_lu_fid(&h->hai_dfid);
2003 __swab64s(&h->hai_cookie);
2004 __swab64s(&h->hai_extent.offset);
2005 __swab64s(&h->hai_extent.length);
2006 __swab64s(&h->hai_gid);
2009 static void lustre_swab_hal(struct hsm_action_list *h)
2011 struct hsm_action_item *hai;
2014 __swab32s(&h->hal_version);
2015 __swab32s(&h->hal_count);
2016 __swab32s(&h->hal_archive_id);
2017 __swab64s(&h->hal_flags);
2019 for (i = 0; i < h->hal_count; i++) {
2020 lustre_swab_hai(hai);
2021 hai = hai_next(hai);
2025 static void lustre_swab_kuch(struct kuc_hdr *l)
2027 __swab16s(&l->kuc_magic);
2028 /* __u8 l->kuc_transport */
2029 __swab16s(&l->kuc_msgtype);
2030 __swab16s(&l->kuc_msglen);
2033 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2034 struct lustre_kernelcomm *lk)
2036 struct obd_import *imp = class_exp2cliimp(exp);
2037 __u32 archive = lk->lk_data;
2040 if (lk->lk_group != KUC_GRP_HSM) {
2041 CERROR("Bad copytool group %d\n", lk->lk_group);
2045 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2046 lk->lk_uid, lk->lk_group, lk->lk_flags);
2048 if (lk->lk_flags & LK_FLG_STOP) {
2049 /* Unregister with the coordinator */
2050 rc = mdc_ioc_hsm_ct_unregister(imp);
2052 rc = mdc_ioc_hsm_ct_register(imp, archive);
2059 * Send a message to any listening copytools
2060 * @param val KUC message (kuc_hdr + hsm_action_list)
2061 * @param len total length of message
2063 static int mdc_hsm_copytool_send(int len, void *val)
2065 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2066 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2070 if (len < sizeof(*lh) + sizeof(*hal)) {
2071 CERROR("Short HSM message %d < %d\n", len,
2072 (int) (sizeof(*lh) + sizeof(*hal)));
2075 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2076 lustre_swab_kuch(lh);
2077 lustre_swab_hal(hal);
2078 } else if (lh->kuc_magic != KUC_MAGIC) {
2079 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2083 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2085 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2086 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2088 /* Broadcast to HSM listeners */
2089 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2095 * callback function passed to kuc for re-registering each HSM copytool
2096 * running on MDC, after MDT shutdown/recovery.
2097 * @param data archive id served by the copytool
2098 * @param cb_arg callback argument (obd_import)
2100 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2102 struct obd_import *imp = (struct obd_import *)cb_arg;
2103 __u32 archive = data;
2106 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2108 rc = mdc_ioc_hsm_ct_register(imp, archive);
2110 /* ignore error if the copytool is already registered */
2111 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2115 * Re-establish all kuc contexts with MDT
2116 * after MDT shutdown/recovery.
2118 static int mdc_kuc_reregister(struct obd_import *imp)
2120 /* re-register HSM agents */
2121 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2125 int mdc_set_info_async(const struct lu_env *env,
2126 struct obd_export *exp,
2127 obd_count keylen, void *key,
2128 obd_count vallen, void *val,
2129 struct ptlrpc_request_set *set)
2131 struct obd_import *imp = class_exp2cliimp(exp);
2135 if (KEY_IS(KEY_READ_ONLY)) {
2136 if (vallen != sizeof(int))
2139 spin_lock(&imp->imp_lock);
2140 if (*((int *)val)) {
2141 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2142 imp->imp_connect_data.ocd_connect_flags |=
2145 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2146 imp->imp_connect_data.ocd_connect_flags &=
2147 ~OBD_CONNECT_RDONLY;
2149 spin_unlock(&imp->imp_lock);
2151 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2152 keylen, key, vallen, val, set);
2155 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2156 sptlrpc_conf_client_adapt(exp->exp_obd);
2159 if (KEY_IS(KEY_FLUSH_CTX)) {
2160 sptlrpc_import_flush_my_ctx(imp);
2163 if (KEY_IS(KEY_MDS_CONN)) {
2164 /* mds-mds import */
2165 spin_lock(&imp->imp_lock);
2166 imp->imp_server_timeout = 1;
2167 spin_unlock(&imp->imp_lock);
2168 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2169 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2172 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2173 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2174 keylen, key, vallen, val, set);
2177 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2178 rc = mdc_hsm_copytool_send(vallen, val);
2182 CERROR("Unknown key %s\n", (char *)key);
2186 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2187 __u32 keylen, void *key, __u32 *vallen, void *val,
2188 struct lov_stripe_md *lsm)
2192 if (KEY_IS(KEY_MAX_EASIZE)) {
2193 int mdsize, *max_easize;
2195 if (*vallen != sizeof(int))
2197 mdsize = *(int*)val;
2198 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2199 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2201 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2203 } else if (KEY_IS(KEY_CONN_DATA)) {
2204 struct obd_import *imp = class_exp2cliimp(exp);
2205 struct obd_connect_data *data = val;
2207 if (*vallen != sizeof(*data))
2210 *data = imp->imp_connect_data;
2212 } else if (KEY_IS(KEY_TGT_COUNT)) {
2217 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2222 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2223 struct obd_capa *oc, struct obd_client_handle *handle,
2226 struct ptlrpc_request *req;
2227 struct mdt_body *body;
2231 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2235 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2237 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2239 ptlrpc_request_free(req);
2243 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2245 ptlrpc_request_set_replen(req);
2247 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2248 rc = ptlrpc_queue_wait(req);
2249 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2251 CERROR("Pin failed: %d\n", rc);
2255 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2257 GOTO(err_out, rc = -EPROTO);
2259 handle->och_fh = body->handle;
2260 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2262 handle->och_mod = obd_mod_alloc();
2263 if (handle->och_mod == NULL) {
2264 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2265 GOTO(err_out, rc = -ENOMEM);
2267 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2272 ptlrpc_req_finished(req);
2276 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2279 struct ptlrpc_request *req;
2280 struct mdt_body *body;
2284 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2285 LUSTRE_MDS_VERSION, MDS_UNPIN);
2289 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2290 body->handle = handle->och_fh;
2293 ptlrpc_request_set_replen(req);
2295 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2296 rc = ptlrpc_queue_wait(req);
2297 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2300 CERROR("Unpin failed: %d\n", rc);
2302 ptlrpc_req_finished(req);
2303 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2305 obd_mod_put(handle->och_mod);
2309 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2310 struct obd_capa *oc, struct ptlrpc_request **request)
2312 struct ptlrpc_request *req;
2317 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2321 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2323 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2325 ptlrpc_request_free(req);
2329 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2331 ptlrpc_request_set_replen(req);
2333 rc = ptlrpc_queue_wait(req);
2335 ptlrpc_req_finished(req);
2341 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2342 enum obd_import_event event)
2346 LASSERT(imp->imp_obd == obd);
2349 case IMP_EVENT_DISCON: {
2351 /* XXX Pass event up to OBDs stack. used only for FLD now */
2352 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2356 case IMP_EVENT_INACTIVE: {
2357 struct client_obd *cli = &obd->u.cli;
2359 * Flush current sequence to make client obtain new one
2360 * from server in case of disconnect/reconnect.
2362 if (cli->cl_seq != NULL)
2363 seq_client_flush(cli->cl_seq);
2365 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2368 case IMP_EVENT_INVALIDATE: {
2369 struct ldlm_namespace *ns = obd->obd_namespace;
2371 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2375 case IMP_EVENT_ACTIVE:
2376 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2377 /* redo the kuc registration after reconnecting */
2379 rc = mdc_kuc_reregister(imp);
2382 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2384 case IMP_EVENT_DEACTIVATE:
2385 case IMP_EVENT_ACTIVATE:
2388 CERROR("Unknown import event %x\n", event);
2394 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2395 struct md_op_data *op_data)
2397 struct client_obd *cli = &exp->exp_obd->u.cli;
2398 struct lu_client_seq *seq = cli->cl_seq;
2400 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2403 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2404 struct client_obd *cli = &exp->exp_obd->u.cli;
2405 return &cli->cl_target_uuid;
2409 * Determine whether the lock can be canceled before replaying it during
2410 * recovery, non zero value will be return if the lock can be canceled,
2411 * or zero returned for not
2413 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2415 if (lock->l_resource->lr_type != LDLM_IBITS)
2418 /* FIXME: if we ever get into a situation where there are too many
2419 * opened files with open locks on a single node, then we really
2420 * should replay these open locks to reget it */
2421 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2427 static int mdc_resource_inode_free(struct ldlm_resource *res)
2429 if (res->lr_lvb_inode)
2430 res->lr_lvb_inode = NULL;
2435 struct ldlm_valblock_ops inode_lvbo = {
2436 lvbo_free: mdc_resource_inode_free
2439 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2441 struct client_obd *cli = &obd->u.cli;
2442 struct lprocfs_static_vars lvars = { 0 };
2446 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2447 if (!cli->cl_rpc_lock)
2449 mdc_init_rpc_lock(cli->cl_rpc_lock);
2453 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2454 if (!cli->cl_close_lock)
2455 GOTO(err_rpc_lock, rc = -ENOMEM);
2456 mdc_init_rpc_lock(cli->cl_close_lock);
2458 rc = client_obd_setup(obd, cfg);
2460 GOTO(err_close_lock, rc);
2461 lprocfs_mdc_init_vars(&lvars);
2462 lprocfs_obd_setup(obd, lvars.obd_vars);
2463 sptlrpc_lprocfs_cliobd_attach(obd);
2464 ptlrpc_lprocfs_register_obd(obd);
2466 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2468 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2470 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2473 CERROR("failed to setup llogging subsystems\n");
2479 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2481 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2486 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2487 * us to make MDS RPCs with large enough reply buffers to hold the
2488 * maximum-sized (= maximum striped) EA and cookie without having to
2489 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2490 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2491 int def_easize, int cookiesize)
2493 struct obd_device *obd = exp->exp_obd;
2494 struct client_obd *cli = &obd->u.cli;
2497 if (cli->cl_max_mds_easize < easize)
2498 cli->cl_max_mds_easize = easize;
2500 if (cli->cl_default_mds_easize < def_easize)
2501 cli->cl_default_mds_easize = def_easize;
2503 if (cli->cl_max_mds_cookiesize < cookiesize)
2504 cli->cl_max_mds_cookiesize = cookiesize;
2509 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2515 case OBD_CLEANUP_EARLY:
2517 case OBD_CLEANUP_EXPORTS:
2518 /* Failsafe, ok if racy */
2519 if (obd->obd_type->typ_refcnt <= 1)
2520 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2522 obd_cleanup_client_import(obd);
2523 ptlrpc_lprocfs_unregister_obd(obd);
2524 lprocfs_obd_cleanup(obd);
2526 rc = obd_llog_finish(obd, 0);
2528 CERROR("failed to cleanup llogging subsystems\n");
2534 static int mdc_cleanup(struct obd_device *obd)
2536 struct client_obd *cli = &obd->u.cli;
2538 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2539 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2543 return client_obd_cleanup(obd);
2547 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2548 struct obd_device *tgt, int *index)
2550 struct llog_ctxt *ctxt;
2555 LASSERT(olg == &obd->obd_olg);
2557 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2562 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2563 llog_initiator_connect(ctxt);
2564 llog_ctxt_put(ctxt);
2569 static int mdc_llog_finish(struct obd_device *obd, int count)
2571 struct llog_ctxt *ctxt;
2575 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2577 llog_cleanup(NULL, ctxt);
2582 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2584 struct lustre_cfg *lcfg = buf;
2585 struct lprocfs_static_vars lvars = { 0 };
2588 lprocfs_mdc_init_vars(&lvars);
2589 switch (lcfg->lcfg_command) {
2591 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2601 /* get remote permission for current user on fid */
2602 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2603 struct obd_capa *oc, __u32 suppgid,
2604 struct ptlrpc_request **request)
2606 struct ptlrpc_request *req;
2610 LASSERT(client_is_remote(exp));
2613 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2617 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2619 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2621 ptlrpc_request_free(req);
2625 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2627 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2628 sizeof(struct mdt_remote_perm));
2630 ptlrpc_request_set_replen(req);
2632 rc = ptlrpc_queue_wait(req);
2634 ptlrpc_req_finished(req);
2640 static int mdc_interpret_renew_capa(const struct lu_env *env,
2641 struct ptlrpc_request *req, void *args,
2644 struct mdc_renew_capa_args *ra = args;
2645 struct mdt_body *body = NULL;
2646 struct lustre_capa *capa;
2650 GOTO(out, capa = ERR_PTR(status));
2652 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2654 GOTO(out, capa = ERR_PTR(-EFAULT));
2656 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2657 GOTO(out, capa = ERR_PTR(-ENOENT));
2659 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2661 GOTO(out, capa = ERR_PTR(-EFAULT));
2664 ra->ra_cb(ra->ra_oc, capa);
2668 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2671 struct ptlrpc_request *req;
2672 struct mdc_renew_capa_args *ra;
2675 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2676 LUSTRE_MDS_VERSION, MDS_GETATTR);
2680 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2681 * capa to renew is oss capa.
2683 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2684 ptlrpc_request_set_replen(req);
2686 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2687 ra = ptlrpc_req_async_args(req);
2690 req->rq_interpret_reply = mdc_interpret_renew_capa;
2691 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2695 static int mdc_connect(const struct lu_env *env,
2696 struct obd_export **exp,
2697 struct obd_device *obd, struct obd_uuid *cluuid,
2698 struct obd_connect_data *data,
2701 struct obd_import *imp = obd->u.cli.cl_import;
2703 /* mds-mds import features */
2704 if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2705 spin_lock(&imp->imp_lock);
2706 imp->imp_server_timeout = 1;
2707 spin_unlock(&imp->imp_lock);
2708 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2709 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2713 return client_connect_import(env, exp, obd, cluuid, data, NULL);
2716 struct obd_ops mdc_obd_ops = {
2717 .o_owner = THIS_MODULE,
2718 .o_setup = mdc_setup,
2719 .o_precleanup = mdc_precleanup,
2720 .o_cleanup = mdc_cleanup,
2721 .o_add_conn = client_import_add_conn,
2722 .o_del_conn = client_import_del_conn,
2723 .o_connect = mdc_connect,
2724 .o_disconnect = client_disconnect_export,
2725 .o_iocontrol = mdc_iocontrol,
2726 .o_set_info_async = mdc_set_info_async,
2727 .o_statfs = mdc_statfs,
2729 .o_unpin = mdc_unpin,
2730 .o_fid_init = client_fid_init,
2731 .o_fid_fini = client_fid_fini,
2732 .o_fid_alloc = mdc_fid_alloc,
2733 .o_import_event = mdc_import_event,
2734 .o_llog_init = mdc_llog_init,
2735 .o_llog_finish = mdc_llog_finish,
2736 .o_get_info = mdc_get_info,
2737 .o_process_config = mdc_process_config,
2738 .o_get_uuid = mdc_get_uuid,
2739 .o_quotactl = mdc_quotactl,
2740 .o_quotacheck = mdc_quotacheck
2743 struct md_ops mdc_md_ops = {
2744 .m_getstatus = mdc_getstatus,
2745 .m_null_inode = mdc_null_inode,
2746 .m_find_cbdata = mdc_find_cbdata,
2747 .m_close = mdc_close,
2748 .m_create = mdc_create,
2749 .m_done_writing = mdc_done_writing,
2750 .m_enqueue = mdc_enqueue,
2751 .m_getattr = mdc_getattr,
2752 .m_getattr_name = mdc_getattr_name,
2753 .m_intent_lock = mdc_intent_lock,
2755 .m_is_subdir = mdc_is_subdir,
2756 .m_rename = mdc_rename,
2757 .m_setattr = mdc_setattr,
2758 .m_setxattr = mdc_setxattr,
2759 .m_getxattr = mdc_getxattr,
2761 .m_readpage = mdc_readpage,
2762 .m_unlink = mdc_unlink,
2763 .m_cancel_unused = mdc_cancel_unused,
2764 .m_init_ea_size = mdc_init_ea_size,
2765 .m_set_lock_data = mdc_set_lock_data,
2766 .m_lock_match = mdc_lock_match,
2767 .m_get_lustre_md = mdc_get_lustre_md,
2768 .m_free_lustre_md = mdc_free_lustre_md,
2769 .m_set_open_replay_data = mdc_set_open_replay_data,
2770 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2771 .m_renew_capa = mdc_renew_capa,
2772 .m_unpack_capa = mdc_unpack_capa,
2773 .m_get_remote_perm = mdc_get_remote_perm,
2774 .m_intent_getattr_async = mdc_intent_getattr_async,
2775 .m_revalidate_lock = mdc_revalidate_lock
2778 int __init mdc_init(void)
2781 struct lprocfs_static_vars lvars = { 0 };
2782 lprocfs_mdc_init_vars(&lvars);
2784 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2785 LUSTRE_MDC_NAME, NULL);
2790 static void /*__exit*/ mdc_exit(void)
2792 class_unregister_type(LUSTRE_MDC_NAME);
2795 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2796 MODULE_DESCRIPTION("Lustre Metadata Client");
2797 MODULE_LICENSE("GPL");
2799 module_init(mdc_init);
2800 module_exit(mdc_exit);