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, 2012, 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
39 # include <linux/module.h>
40 # include <linux/pagemap.h>
41 # include <linux/miscdevice.h>
42 # include <linux/init.h>
43 # include <linux/utsname.h>
45 #include <lustre_acl.h>
46 #include <obd_class.h>
47 #include <lustre_fid.h>
48 #include <lprocfs_status.h>
49 #include <lustre_param.h>
50 #include <lustre_log.h>
52 #include "mdc_internal.h"
54 #define REQUEST_MINOR 244
56 struct mdc_renew_capa_args {
57 struct obd_capa *ra_oc;
58 renew_capa_cb_t ra_cb;
61 static int mdc_cleanup(struct obd_device *obd);
63 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
64 const struct req_msg_field *field, struct obd_capa **oc)
66 struct lustre_capa *capa;
69 /* swabbed already in mdc_enqueue */
70 capa = req_capsule_server_get(&req->rq_pill, field);
74 c = alloc_capa(CAPA_SITE_CLIENT);
76 CDEBUG(D_INFO, "alloc capa failed!\n");
85 static inline int mdc_queue_wait(struct ptlrpc_request *req)
87 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
90 /* mdc_enter_request() ensures that this client has no more
91 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
93 rc = mdc_enter_request(cli);
97 rc = ptlrpc_queue_wait(req);
98 mdc_exit_request(cli);
103 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
104 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
105 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
106 struct obd_capa **pc, int level, int msg_flags)
108 struct ptlrpc_request *req;
109 struct mdt_body *body;
112 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
113 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
117 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
118 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
119 req->rq_send_state = level;
121 ptlrpc_request_set_replen(req);
123 rc = ptlrpc_queue_wait(req);
127 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
129 GOTO(out, rc = -EPROTO);
131 if (body->valid & OBD_MD_FLMDSCAPA) {
132 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
137 *rootfid = body->fid1;
139 "root fid="DFID", last_committed="LPU64"\n",
141 lustre_msg_get_last_committed(req->rq_repmsg));
143 ptlrpc_req_finished(req);
147 /* This should be mdc_get_info("rootfid") */
148 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
149 struct obd_capa **pc)
151 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
156 * This function now is known to always saying that it will receive 4 buffers
157 * from server. Even for cases when acl_size and md_size is zero, RPC header
158 * will contain 4 fields and RPC itself will contain zero size fields. This is
159 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
160 * and thus zero, it shrinks it, making zero size. The same story about
161 * md_size. And this is course of problem when client waits for smaller number
162 * of fields. This issue will be fixed later when client gets aware of RPC
165 static int mdc_getattr_common(struct obd_export *exp,
166 struct ptlrpc_request *req)
168 struct req_capsule *pill = &req->rq_pill;
169 struct mdt_body *body;
173 /* Request message already built. */
174 rc = ptlrpc_queue_wait(req);
178 /* sanity check for the reply */
179 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
183 CDEBUG(D_NET, "mode: %o\n", body->mode);
185 if (body->eadatasize != 0) {
186 mdc_update_max_ea_from_body(exp, body);
188 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
194 if (body->valid & OBD_MD_FLRMTPERM) {
195 struct mdt_remote_perm *perm;
197 LASSERT(client_is_remote(exp));
198 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
199 lustre_swab_mdt_remote_perm);
204 if (body->valid & OBD_MD_FLMDSCAPA) {
205 struct lustre_capa *capa;
206 capa = req_capsule_server_get(pill, &RMF_CAPA1);
214 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
215 struct ptlrpc_request **request)
217 struct ptlrpc_request *req;
220 /* Single MDS without an LMV case */
221 if (op_data->op_flags & MF_GET_MDT_IDX) {
226 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
230 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
232 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
234 ptlrpc_request_free(req);
238 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
239 op_data->op_valid, op_data->op_mode, -1, 0);
241 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
243 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
244 LASSERT(client_is_remote(exp));
245 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
246 sizeof(struct mdt_remote_perm));
248 ptlrpc_request_set_replen(req);
250 rc = mdc_getattr_common(exp, req);
252 ptlrpc_req_finished(req);
258 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
259 struct ptlrpc_request **request)
261 struct ptlrpc_request *req;
265 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
266 &RQF_MDS_GETATTR_NAME);
270 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
271 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
272 op_data->op_namelen + 1);
274 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
276 ptlrpc_request_free(req);
280 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
281 op_data->op_valid, op_data->op_mode,
282 op_data->op_suppgids[0], 0);
284 if (op_data->op_name) {
285 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
286 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
287 op_data->op_namelen);
288 memcpy(name, op_data->op_name, op_data->op_namelen);
291 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
293 ptlrpc_request_set_replen(req);
295 rc = mdc_getattr_common(exp, req);
297 ptlrpc_req_finished(req);
303 static int mdc_is_subdir(struct obd_export *exp,
304 const struct lu_fid *pfid,
305 const struct lu_fid *cfid,
306 struct ptlrpc_request **request)
308 struct ptlrpc_request *req;
312 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
313 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
318 mdc_is_subdir_pack(req, pfid, cfid, 0);
319 ptlrpc_request_set_replen(req);
321 rc = ptlrpc_queue_wait(req);
322 if (rc && rc != -EREMOTE)
323 ptlrpc_req_finished(req);
329 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
330 const struct lu_fid *fid,
331 struct obd_capa *oc, int opcode, obd_valid valid,
332 const char *xattr_name, const char *input,
333 int input_size, int output_size, int flags,
334 __u32 suppgid, struct ptlrpc_request **request)
336 struct ptlrpc_request *req;
337 int xattr_namelen = 0;
342 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
346 mdc_set_capa_size(req, &RMF_CAPA1, oc);
348 xattr_namelen = strlen(xattr_name) + 1;
349 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
354 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
358 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
360 ptlrpc_request_free(req);
364 if (opcode == MDS_REINT) {
365 struct mdt_rec_setxattr *rec;
367 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
368 sizeof(struct mdt_rec_reint));
369 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
370 rec->sx_opcode = REINT_SETXATTR;
371 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
372 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
373 rec->sx_cap = cfs_curproc_cap_pack();
374 rec->sx_suppgid1 = suppgid;
375 rec->sx_suppgid2 = -1;
377 rec->sx_valid = valid | OBD_MD_FLCTIME;
378 rec->sx_time = cfs_time_current_sec();
379 rec->sx_size = output_size;
380 rec->sx_flags = flags;
382 mdc_pack_capa(req, &RMF_CAPA1, oc);
384 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
388 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
389 memcpy(tmp, xattr_name, xattr_namelen);
392 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
393 memcpy(tmp, input, input_size);
396 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
397 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
398 RCL_SERVER, output_size);
399 ptlrpc_request_set_replen(req);
402 if (opcode == MDS_REINT)
403 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
405 rc = ptlrpc_queue_wait(req);
407 if (opcode == MDS_REINT)
408 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
411 ptlrpc_req_finished(req);
417 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
418 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
419 const char *input, int input_size, int output_size,
420 int flags, __u32 suppgid, struct ptlrpc_request **request)
422 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
423 fid, oc, MDS_REINT, valid, xattr_name,
424 input, input_size, output_size, flags,
428 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
429 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
430 const char *input, int input_size, int output_size,
431 int flags, struct ptlrpc_request **request)
433 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
434 fid, oc, MDS_GETXATTR, valid, xattr_name,
435 input, input_size, output_size, flags,
439 #ifdef CONFIG_FS_POSIX_ACL
440 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
442 struct req_capsule *pill = &req->rq_pill;
443 struct mdt_body *body = md->body;
444 struct posix_acl *acl;
451 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
456 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
459 CERROR("convert xattr to acl: %d\n", rc);
463 rc = posix_acl_valid(acl);
465 CERROR("validate acl: %d\n", rc);
466 posix_acl_release(acl);
474 #define mdc_unpack_acl(req, md) 0
477 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
478 struct obd_export *dt_exp, struct obd_export *md_exp,
479 struct lustre_md *md)
481 struct req_capsule *pill = &req->rq_pill;
485 memset(md, 0, sizeof(*md));
487 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
488 LASSERT(md->body != NULL);
490 if (md->body->valid & OBD_MD_FLEASIZE) {
492 struct lov_mds_md *lmm;
494 if (!S_ISREG(md->body->mode)) {
495 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
496 "regular file, but is not\n");
497 GOTO(out, rc = -EPROTO);
500 if (md->body->eadatasize == 0) {
501 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
502 "but eadatasize 0\n");
503 GOTO(out, rc = -EPROTO);
505 lmmsize = md->body->eadatasize;
506 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
508 GOTO(out, rc = -EPROTO);
510 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
514 if (rc < sizeof(*md->lsm)) {
515 CDEBUG(D_INFO, "lsm size too small: "
516 "rc < sizeof (*md->lsm) (%d < %d)\n",
517 rc, (int)sizeof(*md->lsm));
518 GOTO(out, rc = -EPROTO);
521 } else if (md->body->valid & OBD_MD_FLDIREA) {
523 struct lov_mds_md *lmv;
525 if(!S_ISDIR(md->body->mode)) {
526 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
527 "directory, but is not\n");
528 GOTO(out, rc = -EPROTO);
531 if (md->body->eadatasize == 0) {
532 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
533 "but eadatasize 0\n");
536 if (md->body->valid & OBD_MD_MEA) {
537 lmvsize = md->body->eadatasize;
538 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
541 GOTO(out, rc = -EPROTO);
543 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
548 if (rc < sizeof(*md->mea)) {
549 CDEBUG(D_INFO, "size too small: "
550 "rc < sizeof(*md->mea) (%d < %d)\n",
551 rc, (int)sizeof(*md->mea));
552 GOTO(out, rc = -EPROTO);
558 if (md->body->valid & OBD_MD_FLRMTPERM) {
559 /* remote permission */
560 LASSERT(client_is_remote(exp));
561 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
562 lustre_swab_mdt_remote_perm);
563 if (!md->remote_perm)
564 GOTO(out, rc = -EPROTO);
566 else if (md->body->valid & OBD_MD_FLACL) {
567 /* for ACL, it's possible that FLACL is set but aclsize is zero.
568 * only when aclsize != 0 there's an actual segment for ACL
571 if (md->body->aclsize) {
572 rc = mdc_unpack_acl(req, md);
575 #ifdef CONFIG_FS_POSIX_ACL
577 md->posix_acl = NULL;
581 if (md->body->valid & OBD_MD_FLMDSCAPA) {
582 struct obd_capa *oc = NULL;
584 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
590 if (md->body->valid & OBD_MD_FLOSSCAPA) {
591 struct obd_capa *oc = NULL;
593 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
602 capa_put(md->oss_capa);
606 capa_put(md->mds_capa);
609 #ifdef CONFIG_FS_POSIX_ACL
610 posix_acl_release(md->posix_acl);
613 obd_free_memmd(dt_exp, &md->lsm);
618 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
624 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
627 void mdc_replay_open(struct ptlrpc_request *req)
629 struct md_open_data *mod = req->rq_cb_data;
630 struct ptlrpc_request *close_req;
631 struct obd_client_handle *och;
632 struct lustre_handle old;
633 struct mdt_body *body;
636 DEBUG_REQ(D_ERROR, req,
637 "Can't properly replay without open data.");
641 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
642 LASSERT(body != NULL);
646 struct lustre_handle *file_fh;
648 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
650 file_fh = &och->och_fh;
651 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
652 file_fh->cookie, body->handle.cookie);
654 *file_fh = body->handle;
656 close_req = mod->mod_close_req;
657 if (close_req != NULL) {
658 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
659 struct mdt_ioepoch *epoch;
661 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
662 epoch = req_capsule_client_get(&close_req->rq_pill,
667 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
668 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
669 epoch->handle = body->handle;
673 void mdc_commit_open(struct ptlrpc_request *req)
675 struct md_open_data *mod = req->rq_cb_data;
680 * No need to touch md_open_data::mod_och, it holds a reference on
681 * \var mod and will zero references to each other, \var mod will be
682 * freed after that when md_open_data::mod_och will put the reference.
686 * Do not let open request to disappear as it still may be needed
687 * for close rpc to happen (it may happen on evict only, otherwise
688 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
689 * called), just mark this rpc as committed to distinguish these 2
690 * cases, see mdc_close() for details. The open request reference will
691 * be put along with freeing \var mod.
693 ptlrpc_request_addref(req);
694 spin_lock(&req->rq_lock);
695 req->rq_committed = 1;
696 spin_unlock(&req->rq_lock);
697 req->rq_cb_data = NULL;
701 int mdc_set_open_replay_data(struct obd_export *exp,
702 struct obd_client_handle *och,
703 struct ptlrpc_request *open_req)
705 struct md_open_data *mod;
706 struct mdt_rec_create *rec;
707 struct mdt_body *body;
708 struct obd_import *imp = open_req->rq_import;
710 if (!open_req->rq_replay)
713 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
714 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
715 LASSERT(rec != NULL);
716 /* Incoming message in my byte order (it's been swabbed). */
717 /* Outgoing messages always in my byte order. */
718 LASSERT(body != NULL);
720 /* Only if the import is replayable, we set replay_open data */
721 if (och && imp->imp_replayable) {
722 mod = obd_mod_alloc();
724 DEBUG_REQ(D_ERROR, open_req,
725 "Can't allocate md_open_data");
730 * Take a reference on \var mod, to be freed on mdc_close().
731 * It protects \var mod from being freed on eviction (commit
732 * callback is called despite rq_replay flag).
733 * Another reference for \var och.
738 spin_lock(&open_req->rq_lock);
741 mod->mod_open_req = open_req;
742 open_req->rq_cb_data = mod;
743 open_req->rq_commit_cb = mdc_commit_open;
744 spin_unlock(&open_req->rq_lock);
747 rec->cr_fid2 = body->fid1;
748 rec->cr_ioepoch = body->ioepoch;
749 rec->cr_old_handle.cookie = body->handle.cookie;
750 open_req->rq_replay_cb = mdc_replay_open;
751 if (!fid_is_sane(&body->fid1)) {
752 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
757 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
761 int mdc_clear_open_replay_data(struct obd_export *exp,
762 struct obd_client_handle *och)
764 struct md_open_data *mod = och->och_mod;
767 * It is possible to not have \var mod in a case of eviction between
768 * lookup and ll_file_open().
773 LASSERT(mod != LP_POISON);
782 /* Prepares the request for the replay by the given reply */
783 static void mdc_close_handle_reply(struct ptlrpc_request *req,
784 struct md_op_data *op_data, int rc) {
785 struct mdt_body *repbody;
786 struct mdt_ioepoch *epoch;
788 if (req && rc == -EAGAIN) {
789 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
790 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
792 epoch->flags |= MF_SOM_AU;
793 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
794 op_data->op_flags |= MF_GETATTR_LOCK;
798 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
799 struct md_open_data *mod, struct ptlrpc_request **request)
801 struct obd_device *obd = class_exp2obd(exp);
802 struct ptlrpc_request *req;
803 struct req_format *req_fmt;
808 req_fmt = &RQF_MDS_CLOSE;
809 if (op_data->op_bias & MDS_HSM_RELEASE) {
810 req_fmt = &RQF_MDS_RELEASE_CLOSE;
812 /* allocate a FID for volatile file */
813 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
815 CERROR("%s: "DFID" failed to allocate FID: %d\n",
816 obd->obd_name, PFID(&op_data->op_fid1), rc);
817 /* save the errcode and proceed to close */
823 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
827 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
829 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
831 ptlrpc_request_free(req);
835 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
836 * portal whose threads are not taking any DLM locks and are therefore
837 * always progressing */
838 req->rq_request_portal = MDS_READPAGE_PORTAL;
839 ptlrpc_at_set_req_timeout(req);
841 /* Ensure that this close's handle is fixed up during replay. */
842 if (likely(mod != NULL)) {
843 LASSERTF(mod->mod_open_req != NULL &&
844 mod->mod_open_req->rq_type != LI_POISON,
845 "POISONED open %p!\n", mod->mod_open_req);
847 mod->mod_close_req = req;
849 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
850 /* We no longer want to preserve this open for replay even
851 * though the open was committed. b=3632, b=3633 */
852 spin_lock(&mod->mod_open_req->rq_lock);
853 mod->mod_open_req->rq_replay = 0;
854 spin_unlock(&mod->mod_open_req->rq_lock);
856 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
859 mdc_close_pack(req, op_data);
861 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
862 obd->u.cli.cl_max_mds_easize);
863 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
864 obd->u.cli.cl_max_mds_cookiesize);
866 ptlrpc_request_set_replen(req);
868 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
869 rc = ptlrpc_queue_wait(req);
870 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
872 if (req->rq_repmsg == NULL) {
873 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
876 rc = req->rq_status ?: -EIO;
877 } else if (rc == 0 || rc == -EAGAIN) {
878 struct mdt_body *body;
880 rc = lustre_msg_get_status(req->rq_repmsg);
881 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
882 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
887 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
890 } else if (rc == -ESTALE) {
892 * it can be allowed error after 3633 if open was committed and
893 * server failed before close was sent. Let's check if mod
894 * exists and return no error in that case
897 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
898 LASSERT(mod->mod_open_req != NULL);
899 if (mod->mod_open_req->rq_committed)
906 mod->mod_close_req = NULL;
907 /* Since now, mod is accessed through open_req only,
908 * thus close req does not keep a reference on mod anymore. */
912 mdc_close_handle_reply(req, op_data, rc);
913 return rc < 0 ? rc : saved_rc;
916 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
917 struct md_open_data *mod)
919 struct obd_device *obd = class_exp2obd(exp);
920 struct ptlrpc_request *req;
923 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
924 &RQF_MDS_DONE_WRITING);
928 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
929 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
931 ptlrpc_request_free(req);
936 LASSERTF(mod->mod_open_req != NULL &&
937 mod->mod_open_req->rq_type != LI_POISON,
938 "POISONED setattr %p!\n", mod->mod_open_req);
940 mod->mod_close_req = req;
941 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
942 /* We no longer want to preserve this setattr for replay even
943 * though the open was committed. b=3632, b=3633 */
944 spin_lock(&mod->mod_open_req->rq_lock);
945 mod->mod_open_req->rq_replay = 0;
946 spin_unlock(&mod->mod_open_req->rq_lock);
949 mdc_close_pack(req, op_data);
950 ptlrpc_request_set_replen(req);
952 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
953 rc = ptlrpc_queue_wait(req);
954 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
958 * it can be allowed error after 3633 if open or setattr were
959 * committed and server failed before close was sent.
960 * Let's check if mod exists and return no error in that case
963 LASSERT(mod->mod_open_req != NULL);
964 if (mod->mod_open_req->rq_committed)
971 mod->mod_close_req = NULL;
972 /* Since now, mod is accessed through setattr req only,
973 * thus DW req does not keep a reference on mod anymore. */
977 mdc_close_handle_reply(req, op_data, rc);
978 ptlrpc_req_finished(req);
983 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
984 struct page **pages, struct ptlrpc_request **request)
986 struct ptlrpc_request *req;
987 struct ptlrpc_bulk_desc *desc;
989 wait_queue_head_t waitq;
991 struct l_wait_info lwi;
995 init_waitqueue_head(&waitq);
998 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1002 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1004 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1006 ptlrpc_request_free(req);
1010 req->rq_request_portal = MDS_READPAGE_PORTAL;
1011 ptlrpc_at_set_req_timeout(req);
1013 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1016 ptlrpc_request_free(req);
1020 /* NB req now owns desc and will free it when it gets freed */
1021 for (i = 0; i < op_data->op_npages; i++)
1022 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1024 mdc_readdir_pack(req, op_data->op_offset,
1025 PAGE_CACHE_SIZE * op_data->op_npages,
1026 &op_data->op_fid1, op_data->op_capa1);
1028 ptlrpc_request_set_replen(req);
1029 rc = ptlrpc_queue_wait(req);
1031 ptlrpc_req_finished(req);
1032 if (rc != -ETIMEDOUT)
1036 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1037 CERROR("too many resend retries, returning error\n");
1040 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1041 l_wait_event(waitq, 0, &lwi);
1046 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1047 req->rq_bulk->bd_nob_transferred);
1049 ptlrpc_req_finished(req);
1053 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1054 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1055 req->rq_bulk->bd_nob_transferred,
1056 PAGE_CACHE_SIZE * op_data->op_npages);
1057 ptlrpc_req_finished(req);
1065 static int mdc_statfs(const struct lu_env *env,
1066 struct obd_export *exp, struct obd_statfs *osfs,
1067 __u64 max_age, __u32 flags)
1069 struct obd_device *obd = class_exp2obd(exp);
1070 struct ptlrpc_request *req;
1071 struct obd_statfs *msfs;
1072 struct obd_import *imp = NULL;
1076 * Since the request might also come from lprocfs, so we need
1077 * sync this with client_disconnect_export Bug15684
1079 down_read(&obd->u.cli.cl_sem);
1080 if (obd->u.cli.cl_import)
1081 imp = class_import_get(obd->u.cli.cl_import);
1082 up_read(&obd->u.cli.cl_sem);
1086 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1087 LUSTRE_MDS_VERSION, MDS_STATFS);
1089 GOTO(output, rc = -ENOMEM);
1091 ptlrpc_request_set_replen(req);
1093 if (flags & OBD_STATFS_NODELAY) {
1094 /* procfs requests not want stay in wait for avoid deadlock */
1095 req->rq_no_resend = 1;
1096 req->rq_no_delay = 1;
1099 rc = ptlrpc_queue_wait(req);
1101 /* check connection error first */
1102 if (imp->imp_connect_error)
1103 rc = imp->imp_connect_error;
1107 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1109 GOTO(out, rc = -EPROTO);
1113 ptlrpc_req_finished(req);
1115 class_import_put(imp);
1119 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1121 __u32 keylen, vallen;
1125 if (gf->gf_pathlen > PATH_MAX)
1126 return -ENAMETOOLONG;
1127 if (gf->gf_pathlen < 2)
1130 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1131 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1132 OBD_ALLOC(key, keylen);
1135 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1136 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1138 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1139 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1141 if (!fid_is_sane(&gf->gf_fid))
1142 GOTO(out, rc = -EINVAL);
1144 /* Val is struct getinfo_fid2path result plus path */
1145 vallen = sizeof(*gf) + gf->gf_pathlen;
1147 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1148 if (rc != 0 && rc != -EREMOTE)
1151 if (vallen <= sizeof(*gf))
1152 GOTO(out, rc = -EPROTO);
1153 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1154 GOTO(out, rc = -EOVERFLOW);
1156 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1157 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1160 OBD_FREE(key, keylen);
1164 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1165 struct hsm_progress_kernel *hpk)
1167 struct obd_import *imp = class_exp2cliimp(exp);
1168 struct hsm_progress_kernel *req_hpk;
1169 struct ptlrpc_request *req;
1172 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1173 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1175 GOTO(out, rc = -ENOMEM);
1177 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1179 /* Copy hsm_progress struct */
1180 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1181 if (req_hpk == NULL)
1182 GOTO(out, rc = -EPROTO);
1185 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1187 ptlrpc_request_set_replen(req);
1189 rc = mdc_queue_wait(req);
1192 ptlrpc_req_finished(req);
1196 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1198 __u32 *archive_mask;
1199 struct ptlrpc_request *req;
1202 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1204 MDS_HSM_CT_REGISTER);
1206 GOTO(out, rc = -ENOMEM);
1208 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1210 /* Copy hsm_progress struct */
1211 archive_mask = req_capsule_client_get(&req->rq_pill,
1212 &RMF_MDS_HSM_ARCHIVE);
1213 if (archive_mask == NULL)
1214 GOTO(out, rc = -EPROTO);
1216 *archive_mask = archives;
1218 ptlrpc_request_set_replen(req);
1220 rc = mdc_queue_wait(req);
1223 ptlrpc_req_finished(req);
1227 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1228 struct md_op_data *op_data)
1230 struct hsm_current_action *hca = op_data->op_data;
1231 struct hsm_current_action *req_hca;
1232 struct ptlrpc_request *req;
1235 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1236 &RQF_MDS_HSM_ACTION);
1240 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1242 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1244 ptlrpc_request_free(req);
1248 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1249 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1251 ptlrpc_request_set_replen(req);
1253 rc = mdc_queue_wait(req);
1257 req_hca = req_capsule_server_get(&req->rq_pill,
1258 &RMF_MDS_HSM_CURRENT_ACTION);
1259 if (req_hca == NULL)
1260 GOTO(out, rc = -EPROTO);
1265 ptlrpc_req_finished(req);
1269 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1271 struct ptlrpc_request *req;
1274 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1276 MDS_HSM_CT_UNREGISTER);
1278 GOTO(out, rc = -ENOMEM);
1280 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1282 ptlrpc_request_set_replen(req);
1284 rc = mdc_queue_wait(req);
1287 ptlrpc_req_finished(req);
1291 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1292 struct md_op_data *op_data)
1294 struct hsm_user_state *hus = op_data->op_data;
1295 struct hsm_user_state *req_hus;
1296 struct ptlrpc_request *req;
1299 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1300 &RQF_MDS_HSM_STATE_GET);
1304 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1306 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1308 ptlrpc_request_free(req);
1312 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1313 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1315 ptlrpc_request_set_replen(req);
1317 rc = mdc_queue_wait(req);
1321 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1322 if (req_hus == NULL)
1323 GOTO(out, rc = -EPROTO);
1328 ptlrpc_req_finished(req);
1332 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1333 struct md_op_data *op_data)
1335 struct hsm_state_set *hss = op_data->op_data;
1336 struct hsm_state_set *req_hss;
1337 struct ptlrpc_request *req;
1340 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1341 &RQF_MDS_HSM_STATE_SET);
1345 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1347 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1349 ptlrpc_request_free(req);
1353 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1354 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1357 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1358 if (req_hss == NULL)
1359 GOTO(out, rc = -EPROTO);
1362 ptlrpc_request_set_replen(req);
1364 rc = mdc_queue_wait(req);
1368 ptlrpc_req_finished(req);
1372 static int mdc_ioc_hsm_request(struct obd_export *exp,
1373 struct hsm_user_request *hur)
1375 struct obd_import *imp = class_exp2cliimp(exp);
1376 struct ptlrpc_request *req;
1377 struct hsm_request *req_hr;
1378 struct hsm_user_item *req_hui;
1382 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1384 GOTO(out, rc = -ENOMEM);
1386 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1387 hur->hur_request.hr_itemcount
1388 * sizeof(struct hsm_user_item));
1389 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1390 hur->hur_request.hr_data_len);
1392 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1394 ptlrpc_request_free(req);
1398 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1400 /* Copy hsm_request struct */
1401 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1403 GOTO(out, rc = -EPROTO);
1404 *req_hr = hur->hur_request;
1406 /* Copy hsm_user_item structs */
1407 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1408 if (req_hui == NULL)
1409 GOTO(out, rc = -EPROTO);
1410 memcpy(req_hui, hur->hur_user_item,
1411 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1413 /* Copy opaque field */
1414 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1415 if (req_opaque == NULL)
1416 GOTO(out, rc = -EPROTO);
1417 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1419 ptlrpc_request_set_replen(req);
1421 rc = mdc_queue_wait(req);
1425 ptlrpc_req_finished(req);
1429 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1431 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1433 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1435 lh->kuc_magic = KUC_MAGIC;
1436 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1437 lh->kuc_flags = flags;
1438 lh->kuc_msgtype = CL_RECORD;
1439 lh->kuc_msglen = len;
1443 #define D_CHANGELOG 0
1445 struct changelog_show {
1450 struct obd_device *cs_obd;
1453 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1454 struct llog_rec_hdr *hdr, void *data)
1456 struct changelog_show *cs = data;
1457 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1461 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1463 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1464 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1465 rec->cr.cr_type, rc);
1469 if (rec->cr.cr_index < cs->cs_startrec) {
1470 /* Skip entries earlier than what we are interested in */
1471 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1472 rec->cr.cr_index, cs->cs_startrec);
1476 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1477 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1478 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1479 rec->cr.cr_flags & CLF_FLAGMASK,
1480 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1481 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1483 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1485 /* Set up the message */
1486 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1487 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1489 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1490 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1495 static int mdc_changelog_send_thread(void *csdata)
1497 struct changelog_show *cs = csdata;
1498 struct llog_ctxt *ctxt = NULL;
1499 struct llog_handle *llh = NULL;
1500 struct kuc_hdr *kuch;
1503 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1504 cs->cs_fp, cs->cs_startrec);
1506 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1507 if (cs->cs_buf == NULL)
1508 GOTO(out, rc = -ENOMEM);
1510 /* Set up the remote catalog handle */
1511 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1513 GOTO(out, rc = -ENOENT);
1514 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1517 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1518 cs->cs_obd->obd_name, rc);
1521 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1523 CERROR("llog_init_handle failed %d\n", rc);
1527 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1529 /* Send EOF no matter what our result */
1530 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1532 kuch->kuc_msgtype = CL_EOF;
1533 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1539 llog_cat_close(NULL, llh);
1541 llog_ctxt_put(ctxt);
1543 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1548 static int mdc_ioc_changelog_send(struct obd_device *obd,
1549 struct ioc_changelog *icc)
1551 struct changelog_show *cs;
1554 /* Freed in mdc_changelog_send_thread */
1560 cs->cs_startrec = icc->icc_recno;
1561 /* matching fput in mdc_changelog_send_thread */
1562 cs->cs_fp = fget(icc->icc_id);
1563 cs->cs_flags = icc->icc_flags;
1566 * New thread because we should return to user app before
1567 * writing into our pipe
1569 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1570 "mdc_clg_send_thread"));
1571 if (!IS_ERR_VALUE(rc)) {
1572 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1576 CERROR("Failed to start changelog thread: %d\n", rc);
1581 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1582 struct lustre_kernelcomm *lk);
1584 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1585 struct obd_quotactl *oqctl)
1587 struct client_obd *cli = &exp->exp_obd->u.cli;
1588 struct ptlrpc_request *req;
1589 struct obd_quotactl *body;
1592 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1593 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1598 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1601 ptlrpc_request_set_replen(req);
1603 /* the next poll will find -ENODATA, that means quotacheck is
1605 cli->cl_qchk_stat = -ENODATA;
1606 rc = ptlrpc_queue_wait(req);
1608 cli->cl_qchk_stat = rc;
1609 ptlrpc_req_finished(req);
1613 static int mdc_quota_poll_check(struct obd_export *exp,
1614 struct if_quotacheck *qchk)
1616 struct client_obd *cli = &exp->exp_obd->u.cli;
1619 qchk->obd_uuid = cli->cl_target_uuid;
1620 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1622 rc = cli->cl_qchk_stat;
1623 /* the client is not the previous one */
1624 if (rc == CL_NOT_QUOTACHECKED)
1629 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1630 struct obd_quotactl *oqctl)
1632 struct ptlrpc_request *req;
1633 struct obd_quotactl *oqc;
1636 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1637 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1642 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1645 ptlrpc_request_set_replen(req);
1646 ptlrpc_at_set_req_timeout(req);
1647 req->rq_no_resend = 1;
1649 rc = ptlrpc_queue_wait(req);
1651 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1653 if (req->rq_repmsg &&
1654 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1657 CERROR ("Can't unpack obd_quotactl\n");
1660 ptlrpc_req_finished(req);
1665 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1666 struct md_op_data *op_data)
1669 struct ptlrpc_request *req;
1671 struct mdc_swap_layouts *msl, *payload;
1673 msl = op_data->op_data;
1675 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1676 * first thing it will do is to cancel the 2 layout
1677 * locks hold by this client.
1678 * So the client must cancel its layout locks on the 2 fids
1679 * with the request RPC to avoid extra RPC round trips
1681 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1682 LCK_CR, MDS_INODELOCK_LAYOUT);
1683 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1684 LCK_CR, MDS_INODELOCK_LAYOUT);
1686 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1687 &RQF_MDS_SWAP_LAYOUTS);
1689 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1693 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1694 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1696 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1698 ptlrpc_request_free(req);
1702 mdc_swap_layouts_pack(req, op_data);
1704 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1709 ptlrpc_request_set_replen(req);
1711 rc = ptlrpc_queue_wait(req);
1716 ptlrpc_req_finished(req);
1720 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1721 void *karg, void *uarg)
1723 struct obd_device *obd = exp->exp_obd;
1724 struct obd_ioctl_data *data = karg;
1725 struct obd_import *imp = obd->u.cli.cl_import;
1726 struct llog_ctxt *ctxt;
1729 if (!try_module_get(THIS_MODULE)) {
1730 CERROR("Can't get module. Is it alive?");
1734 case OBD_IOC_CHANGELOG_SEND:
1735 rc = mdc_ioc_changelog_send(obd, karg);
1737 case OBD_IOC_CHANGELOG_CLEAR: {
1738 struct ioc_changelog *icc = karg;
1739 struct changelog_setinfo cs =
1740 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1741 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1742 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1746 case OBD_IOC_FID2PATH:
1747 rc = mdc_ioc_fid2path(exp, karg);
1749 case LL_IOC_HSM_CT_START:
1750 rc = mdc_ioc_hsm_ct_start(exp, karg);
1751 /* ignore if it was already registered on this MDS. */
1755 case LL_IOC_HSM_PROGRESS:
1756 rc = mdc_ioc_hsm_progress(exp, karg);
1758 case LL_IOC_HSM_STATE_GET:
1759 rc = mdc_ioc_hsm_state_get(exp, karg);
1761 case LL_IOC_HSM_STATE_SET:
1762 rc = mdc_ioc_hsm_state_set(exp, karg);
1764 case LL_IOC_HSM_ACTION:
1765 rc = mdc_ioc_hsm_current_action(exp, karg);
1767 case LL_IOC_HSM_REQUEST:
1768 rc = mdc_ioc_hsm_request(exp, karg);
1770 case OBD_IOC_CLIENT_RECOVER:
1771 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1775 case IOC_OSC_SET_ACTIVE:
1776 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1778 case OBD_IOC_PARSE: {
1779 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1780 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1782 llog_ctxt_put(ctxt);
1785 case OBD_IOC_LLOG_INFO:
1786 case OBD_IOC_LLOG_PRINT: {
1787 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1788 rc = llog_ioctl(NULL, ctxt, cmd, data);
1789 llog_ctxt_put(ctxt);
1792 case OBD_IOC_POLL_QUOTACHECK:
1793 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1795 case OBD_IOC_PING_TARGET:
1796 rc = ptlrpc_obd_ping(obd);
1799 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1800 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1801 * there'd be no LMV layer thus we might be called here. Eventually
1802 * this code should be removed.
1805 case IOC_OBD_STATFS: {
1806 struct obd_statfs stat_buf = {0};
1808 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1809 GOTO(out, rc = -ENODEV);
1812 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1813 min((int) data->ioc_plen2,
1814 (int) sizeof(struct obd_uuid))))
1815 GOTO(out, rc = -EFAULT);
1817 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1818 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1823 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1824 min((int) data->ioc_plen1,
1825 (int) sizeof(stat_buf))))
1826 GOTO(out, rc = -EFAULT);
1830 case OBD_IOC_QUOTACTL: {
1831 struct if_quotactl *qctl = karg;
1832 struct obd_quotactl *oqctl;
1834 OBD_ALLOC_PTR(oqctl);
1836 GOTO(out, rc = -ENOMEM);
1838 QCTL_COPY(oqctl, qctl);
1839 rc = obd_quotactl(exp, oqctl);
1841 QCTL_COPY(qctl, oqctl);
1842 qctl->qc_valid = QC_MDTIDX;
1843 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1846 OBD_FREE_PTR(oqctl);
1849 case LL_IOC_GET_CONNECT_FLAGS:
1850 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1851 sizeof(*exp_connect_flags_ptr(exp))))
1852 GOTO(out, rc = -EFAULT);
1855 case LL_IOC_LOV_SWAP_LAYOUTS:
1856 rc = mdc_ioc_swap_layouts(exp, karg);
1859 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1860 GOTO(out, rc = -ENOTTY);
1863 module_put(THIS_MODULE);
1868 int mdc_get_info_rpc(struct obd_export *exp,
1869 obd_count keylen, void *key,
1870 int vallen, void *val)
1872 struct obd_import *imp = class_exp2cliimp(exp);
1873 struct ptlrpc_request *req;
1877 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1881 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1882 RCL_CLIENT, keylen);
1883 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1884 RCL_CLIENT, sizeof(__u32));
1886 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1888 ptlrpc_request_free(req);
1892 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1893 memcpy(tmp, key, keylen);
1894 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1895 memcpy(tmp, &vallen, sizeof(__u32));
1897 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1898 RCL_SERVER, vallen);
1899 ptlrpc_request_set_replen(req);
1901 rc = ptlrpc_queue_wait(req);
1902 /* -EREMOTE means the get_info result is partial, and it needs to
1903 * continue on another MDT, see fid2path part in lmv_iocontrol */
1904 if (rc == 0 || rc == -EREMOTE) {
1905 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1906 memcpy(val, tmp, vallen);
1907 if (ptlrpc_rep_need_swab(req)) {
1908 if (KEY_IS(KEY_FID2PATH))
1909 lustre_swab_fid2path(val);
1912 ptlrpc_req_finished(req);
1917 static void lustre_swab_hai(struct hsm_action_item *h)
1919 __swab32s(&h->hai_len);
1920 __swab32s(&h->hai_action);
1921 lustre_swab_lu_fid(&h->hai_fid);
1922 lustre_swab_lu_fid(&h->hai_dfid);
1923 __swab64s(&h->hai_cookie);
1924 __swab64s(&h->hai_extent.offset);
1925 __swab64s(&h->hai_extent.length);
1926 __swab64s(&h->hai_gid);
1929 static void lustre_swab_hal(struct hsm_action_list *h)
1931 struct hsm_action_item *hai;
1934 __swab32s(&h->hal_version);
1935 __swab32s(&h->hal_count);
1936 __swab32s(&h->hal_archive_id);
1937 __swab64s(&h->hal_flags);
1939 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
1940 lustre_swab_hai(hai);
1943 static void lustre_swab_kuch(struct kuc_hdr *l)
1945 __swab16s(&l->kuc_magic);
1946 /* __u8 l->kuc_transport */
1947 __swab16s(&l->kuc_msgtype);
1948 __swab16s(&l->kuc_msglen);
1951 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1952 struct lustre_kernelcomm *lk)
1954 struct obd_import *imp = class_exp2cliimp(exp);
1955 __u32 archive = lk->lk_data;
1958 if (lk->lk_group != KUC_GRP_HSM) {
1959 CERROR("Bad copytool group %d\n", lk->lk_group);
1963 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
1964 lk->lk_uid, lk->lk_group, lk->lk_flags);
1966 if (lk->lk_flags & LK_FLG_STOP) {
1967 /* Unregister with the coordinator */
1968 rc = mdc_ioc_hsm_ct_unregister(imp);
1970 rc = mdc_ioc_hsm_ct_register(imp, archive);
1977 * Send a message to any listening copytools
1978 * @param val KUC message (kuc_hdr + hsm_action_list)
1979 * @param len total length of message
1981 static int mdc_hsm_copytool_send(int len, void *val)
1983 struct kuc_hdr *lh = (struct kuc_hdr *)val;
1984 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
1987 if (len < sizeof(*lh) + sizeof(*hal)) {
1988 CERROR("Short HSM message %d < %d\n", len,
1989 (int) (sizeof(*lh) + sizeof(*hal)));
1992 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
1993 lustre_swab_kuch(lh);
1994 lustre_swab_hal(hal);
1995 } else if (lh->kuc_magic != KUC_MAGIC) {
1996 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2000 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2002 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2003 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2005 /* Broadcast to HSM listeners */
2006 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2012 * callback function passed to kuc for re-registering each HSM copytool
2013 * running on MDC, after MDT shutdown/recovery.
2014 * @param data archive id served by the copytool
2015 * @param cb_arg callback argument (obd_import)
2017 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2019 struct obd_import *imp = (struct obd_import *)cb_arg;
2020 __u32 archive = data;
2023 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2025 rc = mdc_ioc_hsm_ct_register(imp, archive);
2027 /* ignore error if the copytool is already registered */
2028 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2032 * Re-establish all kuc contexts with MDT
2033 * after MDT shutdown/recovery.
2035 static int mdc_kuc_reregister(struct obd_import *imp)
2037 /* re-register HSM agents */
2038 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2042 int mdc_set_info_async(const struct lu_env *env,
2043 struct obd_export *exp,
2044 obd_count keylen, void *key,
2045 obd_count vallen, void *val,
2046 struct ptlrpc_request_set *set)
2048 struct obd_import *imp = class_exp2cliimp(exp);
2051 if (KEY_IS(KEY_READ_ONLY)) {
2052 if (vallen != sizeof(int))
2055 spin_lock(&imp->imp_lock);
2056 if (*((int *)val)) {
2057 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2058 imp->imp_connect_data.ocd_connect_flags |=
2061 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2062 imp->imp_connect_data.ocd_connect_flags &=
2063 ~OBD_CONNECT_RDONLY;
2065 spin_unlock(&imp->imp_lock);
2067 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2068 keylen, key, vallen, val, set);
2071 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2072 sptlrpc_conf_client_adapt(exp->exp_obd);
2075 if (KEY_IS(KEY_FLUSH_CTX)) {
2076 sptlrpc_import_flush_my_ctx(imp);
2079 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2080 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2081 keylen, key, vallen, val, set);
2084 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2085 rc = mdc_hsm_copytool_send(vallen, val);
2089 CERROR("Unknown key %s\n", (char *)key);
2093 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2094 __u32 keylen, void *key, __u32 *vallen, void *val,
2095 struct lov_stripe_md *lsm)
2099 if (KEY_IS(KEY_MAX_EASIZE)) {
2100 int mdsize, *max_easize;
2102 if (*vallen != sizeof(int))
2104 mdsize = *(int*)val;
2105 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2106 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2108 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2110 } else if (KEY_IS(KEY_CONN_DATA)) {
2111 struct obd_import *imp = class_exp2cliimp(exp);
2112 struct obd_connect_data *data = val;
2114 if (*vallen != sizeof(*data))
2117 *data = imp->imp_connect_data;
2119 } else if (KEY_IS(KEY_TGT_COUNT)) {
2124 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2129 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2130 struct obd_capa *oc, struct obd_client_handle *handle,
2133 struct ptlrpc_request *req;
2134 struct mdt_body *body;
2137 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2141 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2143 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2145 ptlrpc_request_free(req);
2149 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2151 ptlrpc_request_set_replen(req);
2153 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2154 rc = ptlrpc_queue_wait(req);
2155 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2157 CERROR("Pin failed: %d\n", rc);
2161 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2163 GOTO(err_out, rc = -EPROTO);
2165 handle->och_fh = body->handle;
2166 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2168 handle->och_mod = obd_mod_alloc();
2169 if (handle->och_mod == NULL) {
2170 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2171 GOTO(err_out, rc = -ENOMEM);
2173 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2178 ptlrpc_req_finished(req);
2182 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2185 struct ptlrpc_request *req;
2186 struct mdt_body *body;
2189 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2190 LUSTRE_MDS_VERSION, MDS_UNPIN);
2194 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2195 body->handle = handle->och_fh;
2198 ptlrpc_request_set_replen(req);
2200 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2201 rc = ptlrpc_queue_wait(req);
2202 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2205 CERROR("Unpin failed: %d\n", rc);
2207 ptlrpc_req_finished(req);
2208 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2210 obd_mod_put(handle->och_mod);
2214 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2215 struct obd_capa *oc, struct ptlrpc_request **request)
2217 struct ptlrpc_request *req;
2221 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2225 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2227 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2229 ptlrpc_request_free(req);
2233 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2235 ptlrpc_request_set_replen(req);
2237 rc = ptlrpc_queue_wait(req);
2239 ptlrpc_req_finished(req);
2245 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2246 enum obd_import_event event)
2250 LASSERT(imp->imp_obd == obd);
2253 case IMP_EVENT_DISCON: {
2255 /* XXX Pass event up to OBDs stack. used only for FLD now */
2256 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2260 case IMP_EVENT_INACTIVE: {
2261 struct client_obd *cli = &obd->u.cli;
2263 * Flush current sequence to make client obtain new one
2264 * from server in case of disconnect/reconnect.
2266 if (cli->cl_seq != NULL)
2267 seq_client_flush(cli->cl_seq);
2269 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2272 case IMP_EVENT_INVALIDATE: {
2273 struct ldlm_namespace *ns = obd->obd_namespace;
2275 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2279 case IMP_EVENT_ACTIVE:
2280 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2281 /* redo the kuc registration after reconnecting */
2283 rc = mdc_kuc_reregister(imp);
2286 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2288 case IMP_EVENT_DEACTIVATE:
2289 case IMP_EVENT_ACTIVATE:
2292 CERROR("Unknown import event %x\n", event);
2298 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2299 struct md_op_data *op_data)
2301 struct client_obd *cli = &exp->exp_obd->u.cli;
2302 struct lu_client_seq *seq = cli->cl_seq;
2304 return seq_client_alloc_fid(NULL, seq, fid);
2307 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2308 struct client_obd *cli = &exp->exp_obd->u.cli;
2309 return &cli->cl_target_uuid;
2313 * Determine whether the lock can be canceled before replaying it during
2314 * recovery, non zero value will be return if the lock can be canceled,
2315 * or zero returned for not
2317 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2319 if (lock->l_resource->lr_type != LDLM_IBITS)
2322 /* FIXME: if we ever get into a situation where there are too many
2323 * opened files with open locks on a single node, then we really
2324 * should replay these open locks to reget it */
2325 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2331 static int mdc_resource_inode_free(struct ldlm_resource *res)
2333 if (res->lr_lvb_inode)
2334 res->lr_lvb_inode = NULL;
2339 struct ldlm_valblock_ops inode_lvbo = {
2340 .lvbo_free = mdc_resource_inode_free,
2343 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2345 struct client_obd *cli = &obd->u.cli;
2346 struct lprocfs_static_vars lvars = { 0 };
2349 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2350 if (!cli->cl_rpc_lock)
2352 mdc_init_rpc_lock(cli->cl_rpc_lock);
2356 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2357 if (!cli->cl_close_lock)
2358 GOTO(err_rpc_lock, rc = -ENOMEM);
2359 mdc_init_rpc_lock(cli->cl_close_lock);
2361 rc = client_obd_setup(obd, cfg);
2363 GOTO(err_close_lock, rc);
2364 lprocfs_mdc_init_vars(&lvars);
2365 lprocfs_obd_setup(obd, lvars.obd_vars);
2366 sptlrpc_lprocfs_cliobd_attach(obd);
2367 ptlrpc_lprocfs_register_obd(obd);
2369 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2371 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2373 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2376 CERROR("failed to setup llogging subsystems\n");
2382 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2384 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2389 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2390 * us to make MDS RPCs with large enough reply buffers to hold the
2391 * maximum-sized (= maximum striped) EA and cookie without having to
2392 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2393 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2394 int def_easize, int cookiesize)
2396 struct obd_device *obd = exp->exp_obd;
2397 struct client_obd *cli = &obd->u.cli;
2399 if (cli->cl_max_mds_easize < easize)
2400 cli->cl_max_mds_easize = easize;
2402 if (cli->cl_default_mds_easize < def_easize)
2403 cli->cl_default_mds_easize = def_easize;
2405 if (cli->cl_max_mds_cookiesize < cookiesize)
2406 cli->cl_max_mds_cookiesize = cookiesize;
2411 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2416 case OBD_CLEANUP_EARLY:
2418 case OBD_CLEANUP_EXPORTS:
2419 /* Failsafe, ok if racy */
2420 if (obd->obd_type->typ_refcnt <= 1)
2421 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2423 obd_cleanup_client_import(obd);
2424 ptlrpc_lprocfs_unregister_obd(obd);
2425 lprocfs_obd_cleanup(obd);
2427 rc = obd_llog_finish(obd, 0);
2429 CERROR("failed to cleanup llogging subsystems\n");
2435 static int mdc_cleanup(struct obd_device *obd)
2437 struct client_obd *cli = &obd->u.cli;
2439 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2440 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2444 return client_obd_cleanup(obd);
2448 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2449 struct obd_device *tgt, int *index)
2451 struct llog_ctxt *ctxt;
2454 LASSERT(olg == &obd->obd_olg);
2456 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2461 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2462 llog_initiator_connect(ctxt);
2463 llog_ctxt_put(ctxt);
2468 static int mdc_llog_finish(struct obd_device *obd, int count)
2470 struct llog_ctxt *ctxt;
2472 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2474 llog_cleanup(NULL, ctxt);
2479 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2481 struct lustre_cfg *lcfg = buf;
2482 struct lprocfs_static_vars lvars = { 0 };
2485 lprocfs_mdc_init_vars(&lvars);
2486 switch (lcfg->lcfg_command) {
2488 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2498 /* get remote permission for current user on fid */
2499 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2500 struct obd_capa *oc, __u32 suppgid,
2501 struct ptlrpc_request **request)
2503 struct ptlrpc_request *req;
2506 LASSERT(client_is_remote(exp));
2509 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2513 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2515 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2517 ptlrpc_request_free(req);
2521 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2523 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2524 sizeof(struct mdt_remote_perm));
2526 ptlrpc_request_set_replen(req);
2528 rc = ptlrpc_queue_wait(req);
2530 ptlrpc_req_finished(req);
2536 static int mdc_interpret_renew_capa(const struct lu_env *env,
2537 struct ptlrpc_request *req, void *args,
2540 struct mdc_renew_capa_args *ra = args;
2541 struct mdt_body *body = NULL;
2542 struct lustre_capa *capa;
2545 GOTO(out, capa = ERR_PTR(status));
2547 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2549 GOTO(out, capa = ERR_PTR(-EFAULT));
2551 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2552 GOTO(out, capa = ERR_PTR(-ENOENT));
2554 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2556 GOTO(out, capa = ERR_PTR(-EFAULT));
2558 ra->ra_cb(ra->ra_oc, capa);
2562 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2565 struct ptlrpc_request *req;
2566 struct mdc_renew_capa_args *ra;
2568 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2569 LUSTRE_MDS_VERSION, MDS_GETATTR);
2573 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2574 * capa to renew is oss capa.
2576 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2577 ptlrpc_request_set_replen(req);
2579 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2580 ra = ptlrpc_req_async_args(req);
2583 req->rq_interpret_reply = mdc_interpret_renew_capa;
2584 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2588 struct obd_ops mdc_obd_ops = {
2589 .o_owner = THIS_MODULE,
2590 .o_setup = mdc_setup,
2591 .o_precleanup = mdc_precleanup,
2592 .o_cleanup = mdc_cleanup,
2593 .o_add_conn = client_import_add_conn,
2594 .o_del_conn = client_import_del_conn,
2595 .o_connect = client_connect_import,
2596 .o_disconnect = client_disconnect_export,
2597 .o_iocontrol = mdc_iocontrol,
2598 .o_set_info_async = mdc_set_info_async,
2599 .o_statfs = mdc_statfs,
2601 .o_unpin = mdc_unpin,
2602 .o_fid_init = client_fid_init,
2603 .o_fid_fini = client_fid_fini,
2604 .o_fid_alloc = mdc_fid_alloc,
2605 .o_import_event = mdc_import_event,
2606 .o_llog_init = mdc_llog_init,
2607 .o_llog_finish = mdc_llog_finish,
2608 .o_get_info = mdc_get_info,
2609 .o_process_config = mdc_process_config,
2610 .o_get_uuid = mdc_get_uuid,
2611 .o_quotactl = mdc_quotactl,
2612 .o_quotacheck = mdc_quotacheck
2615 struct md_ops mdc_md_ops = {
2616 .m_getstatus = mdc_getstatus,
2617 .m_null_inode = mdc_null_inode,
2618 .m_find_cbdata = mdc_find_cbdata,
2619 .m_close = mdc_close,
2620 .m_create = mdc_create,
2621 .m_done_writing = mdc_done_writing,
2622 .m_enqueue = mdc_enqueue,
2623 .m_getattr = mdc_getattr,
2624 .m_getattr_name = mdc_getattr_name,
2625 .m_intent_lock = mdc_intent_lock,
2627 .m_is_subdir = mdc_is_subdir,
2628 .m_rename = mdc_rename,
2629 .m_setattr = mdc_setattr,
2630 .m_setxattr = mdc_setxattr,
2631 .m_getxattr = mdc_getxattr,
2633 .m_readpage = mdc_readpage,
2634 .m_unlink = mdc_unlink,
2635 .m_cancel_unused = mdc_cancel_unused,
2636 .m_init_ea_size = mdc_init_ea_size,
2637 .m_set_lock_data = mdc_set_lock_data,
2638 .m_lock_match = mdc_lock_match,
2639 .m_get_lustre_md = mdc_get_lustre_md,
2640 .m_free_lustre_md = mdc_free_lustre_md,
2641 .m_set_open_replay_data = mdc_set_open_replay_data,
2642 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2643 .m_renew_capa = mdc_renew_capa,
2644 .m_unpack_capa = mdc_unpack_capa,
2645 .m_get_remote_perm = mdc_get_remote_perm,
2646 .m_intent_getattr_async = mdc_intent_getattr_async,
2647 .m_revalidate_lock = mdc_revalidate_lock
2650 int __init mdc_init(void)
2653 struct lprocfs_static_vars lvars = { 0 };
2654 lprocfs_mdc_init_vars(&lvars);
2656 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2657 LUSTRE_MDC_NAME, NULL);
2661 static void /*__exit*/ mdc_exit(void)
2663 class_unregister_type(LUSTRE_MDC_NAME);
2666 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2667 MODULE_DESCRIPTION("Lustre Metadata Client");
2668 MODULE_LICENSE("GPL");
2670 module_init(mdc_init);
2671 module_exit(mdc_exit);