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 /* Flush local XATTR locks to get rid of a possible cancel RPC */
372 if (opcode == MDS_REINT && fid_is_sane(fid) &&
373 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
374 CFS_LIST_HEAD(cancels);
377 /* Without that packing would fail */
379 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
382 count = mdc_resource_get_unused(exp, fid,
384 MDS_INODELOCK_XATTR);
386 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
388 ptlrpc_request_free(req);
392 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
394 ptlrpc_request_free(req);
399 if (opcode == MDS_REINT) {
400 struct mdt_rec_setxattr *rec;
402 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
403 sizeof(struct mdt_rec_reint));
404 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
405 rec->sx_opcode = REINT_SETXATTR;
406 rec->sx_fsuid = current_fsuid();
407 rec->sx_fsgid = current_fsgid();
408 rec->sx_cap = cfs_curproc_cap_pack();
409 rec->sx_suppgid1 = suppgid;
410 rec->sx_suppgid2 = -1;
412 rec->sx_valid = valid | OBD_MD_FLCTIME;
413 rec->sx_time = cfs_time_current_sec();
414 rec->sx_size = output_size;
415 rec->sx_flags = flags;
417 mdc_pack_capa(req, &RMF_CAPA1, oc);
419 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
423 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
424 memcpy(tmp, xattr_name, xattr_namelen);
427 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
428 memcpy(tmp, input, input_size);
431 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
432 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
433 RCL_SERVER, output_size);
434 ptlrpc_request_set_replen(req);
437 if (opcode == MDS_REINT)
438 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
440 rc = ptlrpc_queue_wait(req);
442 if (opcode == MDS_REINT)
443 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
446 ptlrpc_req_finished(req);
452 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
453 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
454 const char *input, int input_size, int output_size,
455 int flags, __u32 suppgid, struct ptlrpc_request **request)
457 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
458 fid, oc, MDS_REINT, valid, xattr_name,
459 input, input_size, output_size, flags,
463 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
464 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
465 const char *input, int input_size, int output_size,
466 int flags, struct ptlrpc_request **request)
468 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
469 fid, oc, MDS_GETXATTR, valid, xattr_name,
470 input, input_size, output_size, flags,
474 #ifdef CONFIG_FS_POSIX_ACL
475 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
477 struct req_capsule *pill = &req->rq_pill;
478 struct mdt_body *body = md->body;
479 struct posix_acl *acl;
487 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
492 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
495 CERROR("convert xattr to acl: %d\n", rc);
499 rc = posix_acl_valid(acl);
501 CERROR("validate acl: %d\n", rc);
502 posix_acl_release(acl);
510 #define mdc_unpack_acl(req, md) 0
513 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
514 struct obd_export *dt_exp, struct obd_export *md_exp,
515 struct lustre_md *md)
517 struct req_capsule *pill = &req->rq_pill;
522 memset(md, 0, sizeof(*md));
524 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
525 LASSERT(md->body != NULL);
527 if (md->body->valid & OBD_MD_FLEASIZE) {
529 struct lov_mds_md *lmm;
531 if (!S_ISREG(md->body->mode)) {
532 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
533 "regular file, but is not\n");
534 GOTO(out, rc = -EPROTO);
537 if (md->body->eadatasize == 0) {
538 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
539 "but eadatasize 0\n");
540 GOTO(out, rc = -EPROTO);
542 lmmsize = md->body->eadatasize;
543 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
545 GOTO(out, rc = -EPROTO);
547 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
551 if (rc < sizeof(*md->lsm)) {
552 CDEBUG(D_INFO, "lsm size too small: "
553 "rc < sizeof (*md->lsm) (%d < %d)\n",
554 rc, (int)sizeof(*md->lsm));
555 GOTO(out, rc = -EPROTO);
558 } else if (md->body->valid & OBD_MD_FLDIREA) {
560 struct lov_mds_md *lmv;
562 if(!S_ISDIR(md->body->mode)) {
563 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
564 "directory, but is not\n");
565 GOTO(out, rc = -EPROTO);
568 if (md->body->eadatasize == 0) {
569 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
570 "but eadatasize 0\n");
573 if (md->body->valid & OBD_MD_MEA) {
574 lmvsize = md->body->eadatasize;
575 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
578 GOTO(out, rc = -EPROTO);
580 rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
585 if (rc < sizeof(*md->mea)) {
586 CDEBUG(D_INFO, "size too small: "
587 "rc < sizeof(*md->mea) (%d < %d)\n",
588 rc, (int)sizeof(*md->mea));
589 GOTO(out, rc = -EPROTO);
595 if (md->body->valid & OBD_MD_FLRMTPERM) {
596 /* remote permission */
597 LASSERT(client_is_remote(exp));
598 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
599 lustre_swab_mdt_remote_perm);
600 if (!md->remote_perm)
601 GOTO(out, rc = -EPROTO);
603 else if (md->body->valid & OBD_MD_FLACL) {
604 /* for ACL, it's possible that FLACL is set but aclsize is zero.
605 * only when aclsize != 0 there's an actual segment for ACL
608 if (md->body->aclsize) {
609 rc = mdc_unpack_acl(req, md);
612 #ifdef CONFIG_FS_POSIX_ACL
614 md->posix_acl = NULL;
618 if (md->body->valid & OBD_MD_FLMDSCAPA) {
619 struct obd_capa *oc = NULL;
621 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
627 if (md->body->valid & OBD_MD_FLOSSCAPA) {
628 struct obd_capa *oc = NULL;
630 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
640 capa_put(md->oss_capa);
644 capa_put(md->mds_capa);
647 #ifdef CONFIG_FS_POSIX_ACL
648 posix_acl_release(md->posix_acl);
651 obd_free_memmd(dt_exp, &md->lsm);
656 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
663 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
666 void mdc_replay_open(struct ptlrpc_request *req)
668 struct md_open_data *mod = req->rq_cb_data;
669 struct ptlrpc_request *close_req;
670 struct obd_client_handle *och;
671 struct lustre_handle old;
672 struct mdt_body *body;
676 DEBUG_REQ(D_ERROR, req,
677 "Can't properly replay without open data.");
682 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
683 LASSERT(body != NULL);
687 struct lustre_handle *file_fh;
689 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
691 file_fh = &och->och_fh;
692 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
693 file_fh->cookie, body->handle.cookie);
695 *file_fh = body->handle;
697 close_req = mod->mod_close_req;
698 if (close_req != NULL) {
699 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
700 struct mdt_ioepoch *epoch;
702 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
703 epoch = req_capsule_client_get(&close_req->rq_pill,
708 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
709 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
710 epoch->handle = body->handle;
715 void mdc_commit_open(struct ptlrpc_request *req)
717 struct md_open_data *mod = req->rq_cb_data;
722 * No need to touch md_open_data::mod_och, it holds a reference on
723 * \var mod and will zero references to each other, \var mod will be
724 * freed after that when md_open_data::mod_och will put the reference.
728 * Do not let open request to disappear as it still may be needed
729 * for close rpc to happen (it may happen on evict only, otherwise
730 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
731 * called), just mark this rpc as committed to distinguish these 2
732 * cases, see mdc_close() for details. The open request reference will
733 * be put along with freeing \var mod.
735 ptlrpc_request_addref(req);
736 spin_lock(&req->rq_lock);
737 req->rq_committed = 1;
738 spin_unlock(&req->rq_lock);
739 req->rq_cb_data = NULL;
743 int mdc_set_open_replay_data(struct obd_export *exp,
744 struct obd_client_handle *och,
745 struct ptlrpc_request *open_req)
747 struct md_open_data *mod;
748 struct mdt_rec_create *rec;
749 struct mdt_body *body;
750 struct obd_import *imp = open_req->rq_import;
753 if (!open_req->rq_replay)
756 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
757 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
758 LASSERT(rec != NULL);
759 /* Incoming message in my byte order (it's been swabbed). */
760 /* Outgoing messages always in my byte order. */
761 LASSERT(body != NULL);
763 /* Only if the import is replayable, we set replay_open data */
764 if (och && imp->imp_replayable) {
765 mod = obd_mod_alloc();
767 DEBUG_REQ(D_ERROR, open_req,
768 "Can't allocate md_open_data");
773 * Take a reference on \var mod, to be freed on mdc_close().
774 * It protects \var mod from being freed on eviction (commit
775 * callback is called despite rq_replay flag).
776 * Another reference for \var och.
781 spin_lock(&open_req->rq_lock);
784 mod->mod_open_req = open_req;
785 open_req->rq_cb_data = mod;
786 open_req->rq_commit_cb = mdc_commit_open;
787 spin_unlock(&open_req->rq_lock);
790 rec->cr_fid2 = body->fid1;
791 rec->cr_ioepoch = body->ioepoch;
792 rec->cr_old_handle.cookie = body->handle.cookie;
793 open_req->rq_replay_cb = mdc_replay_open;
794 if (!fid_is_sane(&body->fid1)) {
795 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
800 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
804 int mdc_clear_open_replay_data(struct obd_export *exp,
805 struct obd_client_handle *och)
807 struct md_open_data *mod = och->och_mod;
811 * It is possible to not have \var mod in a case of eviction between
812 * lookup and ll_file_open().
817 LASSERT(mod != LP_POISON);
826 /* Prepares the request for the replay by the given reply */
827 static void mdc_close_handle_reply(struct ptlrpc_request *req,
828 struct md_op_data *op_data, int rc) {
829 struct mdt_body *repbody;
830 struct mdt_ioepoch *epoch;
832 if (req && rc == -EAGAIN) {
833 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
834 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
836 epoch->flags |= MF_SOM_AU;
837 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
838 op_data->op_flags |= MF_GETATTR_LOCK;
842 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
843 struct md_open_data *mod, struct ptlrpc_request **request)
845 struct obd_device *obd = class_exp2obd(exp);
846 struct ptlrpc_request *req;
847 struct req_format *req_fmt;
852 req_fmt = &RQF_MDS_CLOSE;
853 if (op_data->op_bias & MDS_HSM_RELEASE) {
854 req_fmt = &RQF_MDS_RELEASE_CLOSE;
856 /* allocate a FID for volatile file */
857 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
859 CERROR("%s: "DFID" failed to allocate FID: %d\n",
860 obd->obd_name, PFID(&op_data->op_fid1), rc);
861 /* save the errcode and proceed to close */
867 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
871 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
873 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
875 ptlrpc_request_free(req);
879 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
880 * portal whose threads are not taking any DLM locks and are therefore
881 * always progressing */
882 req->rq_request_portal = MDS_READPAGE_PORTAL;
883 ptlrpc_at_set_req_timeout(req);
885 /* Ensure that this close's handle is fixed up during replay. */
886 if (likely(mod != NULL)) {
887 LASSERTF(mod->mod_open_req != NULL &&
888 mod->mod_open_req->rq_type != LI_POISON,
889 "POISONED open %p!\n", mod->mod_open_req);
891 mod->mod_close_req = req;
893 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
894 /* We no longer want to preserve this open for replay even
895 * though the open was committed. b=3632, b=3633 */
896 spin_lock(&mod->mod_open_req->rq_lock);
897 mod->mod_open_req->rq_replay = 0;
898 spin_unlock(&mod->mod_open_req->rq_lock);
900 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
903 mdc_close_pack(req, op_data);
905 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
906 obd->u.cli.cl_max_mds_easize);
907 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
908 obd->u.cli.cl_max_mds_cookiesize);
910 ptlrpc_request_set_replen(req);
912 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
913 rc = ptlrpc_queue_wait(req);
914 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
916 if (req->rq_repmsg == NULL) {
917 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
920 rc = req->rq_status ?: -EIO;
921 } else if (rc == 0 || rc == -EAGAIN) {
922 struct mdt_body *body;
924 rc = lustre_msg_get_status(req->rq_repmsg);
925 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
926 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
931 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
934 } else if (rc == -ESTALE) {
936 * it can be allowed error after 3633 if open was committed and
937 * server failed before close was sent. Let's check if mod
938 * exists and return no error in that case
941 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
942 LASSERT(mod->mod_open_req != NULL);
943 if (mod->mod_open_req->rq_committed)
950 mod->mod_close_req = NULL;
951 /* Since now, mod is accessed through open_req only,
952 * thus close req does not keep a reference on mod anymore. */
956 mdc_close_handle_reply(req, op_data, rc);
957 RETURN(rc < 0 ? rc : saved_rc);
960 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
961 struct md_open_data *mod)
963 struct obd_device *obd = class_exp2obd(exp);
964 struct ptlrpc_request *req;
968 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
969 &RQF_MDS_DONE_WRITING);
973 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
974 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
976 ptlrpc_request_free(req);
981 LASSERTF(mod->mod_open_req != NULL &&
982 mod->mod_open_req->rq_type != LI_POISON,
983 "POISONED setattr %p!\n", mod->mod_open_req);
985 mod->mod_close_req = req;
986 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
987 /* We no longer want to preserve this setattr for replay even
988 * though the open was committed. b=3632, b=3633 */
989 spin_lock(&mod->mod_open_req->rq_lock);
990 mod->mod_open_req->rq_replay = 0;
991 spin_unlock(&mod->mod_open_req->rq_lock);
994 mdc_close_pack(req, op_data);
995 ptlrpc_request_set_replen(req);
997 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
998 rc = ptlrpc_queue_wait(req);
999 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1001 if (rc == -ESTALE) {
1003 * it can be allowed error after 3633 if open or setattr were
1004 * committed and server failed before close was sent.
1005 * Let's check if mod exists and return no error in that case
1008 LASSERT(mod->mod_open_req != NULL);
1009 if (mod->mod_open_req->rq_committed)
1016 mod->mod_close_req = NULL;
1017 /* Since now, mod is accessed through setattr req only,
1018 * thus DW req does not keep a reference on mod anymore. */
1022 mdc_close_handle_reply(req, op_data, rc);
1023 ptlrpc_req_finished(req);
1027 #ifdef HAVE_SPLIT_SUPPORT
1028 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
1029 const struct page *page, int offset)
1031 struct ptlrpc_request *req;
1032 struct ptlrpc_bulk_desc *desc;
1036 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1040 /* FIXME: capa doesn't support split yet */
1041 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1043 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1045 ptlrpc_request_free(req);
1049 req->rq_request_portal = MDS_READPAGE_PORTAL;
1050 ptlrpc_at_set_req_timeout(req);
1052 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1054 GOTO(out, rc = -ENOMEM);
1056 /* NB req now owns desc and will free it when it gets freed. */
1057 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1058 mdc_readdir_pack(req, 0, offset, fid, NULL);
1060 ptlrpc_request_set_replen(req);
1061 rc = ptlrpc_queue_wait(req);
1065 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1067 ptlrpc_req_finished(req);
1070 EXPORT_SYMBOL(mdc_sendpage);
1073 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1074 struct page **pages, struct ptlrpc_request **request)
1076 struct ptlrpc_request *req;
1077 struct ptlrpc_bulk_desc *desc;
1079 wait_queue_head_t waitq;
1081 struct l_wait_info lwi;
1086 init_waitqueue_head(&waitq);
1089 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1093 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1095 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1097 ptlrpc_request_free(req);
1101 req->rq_request_portal = MDS_READPAGE_PORTAL;
1102 ptlrpc_at_set_req_timeout(req);
1104 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1107 ptlrpc_request_free(req);
1111 /* NB req now owns desc and will free it when it gets freed */
1112 for (i = 0; i < op_data->op_npages; i++)
1113 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1115 mdc_readdir_pack(req, op_data->op_offset,
1116 PAGE_CACHE_SIZE * op_data->op_npages,
1117 &op_data->op_fid1, op_data->op_capa1);
1119 ptlrpc_request_set_replen(req);
1120 rc = ptlrpc_queue_wait(req);
1122 ptlrpc_req_finished(req);
1123 if (rc != -ETIMEDOUT)
1127 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1128 CERROR("too many resend retries, returning error\n");
1131 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1132 l_wait_event(waitq, 0, &lwi);
1137 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1138 req->rq_bulk->bd_nob_transferred);
1140 ptlrpc_req_finished(req);
1144 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1145 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1146 req->rq_bulk->bd_nob_transferred,
1147 PAGE_CACHE_SIZE * op_data->op_npages);
1148 ptlrpc_req_finished(req);
1156 static int mdc_statfs(const struct lu_env *env,
1157 struct obd_export *exp, struct obd_statfs *osfs,
1158 __u64 max_age, __u32 flags)
1160 struct obd_device *obd = class_exp2obd(exp);
1161 struct ptlrpc_request *req;
1162 struct obd_statfs *msfs;
1163 struct obd_import *imp = NULL;
1168 * Since the request might also come from lprocfs, so we need
1169 * sync this with client_disconnect_export Bug15684
1171 down_read(&obd->u.cli.cl_sem);
1172 if (obd->u.cli.cl_import)
1173 imp = class_import_get(obd->u.cli.cl_import);
1174 up_read(&obd->u.cli.cl_sem);
1178 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1179 LUSTRE_MDS_VERSION, MDS_STATFS);
1181 GOTO(output, rc = -ENOMEM);
1183 ptlrpc_request_set_replen(req);
1185 if (flags & OBD_STATFS_NODELAY) {
1186 /* procfs requests not want stay in wait for avoid deadlock */
1187 req->rq_no_resend = 1;
1188 req->rq_no_delay = 1;
1191 rc = ptlrpc_queue_wait(req);
1193 /* check connection error first */
1194 if (imp->imp_connect_error)
1195 rc = imp->imp_connect_error;
1199 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1201 GOTO(out, rc = -EPROTO);
1206 ptlrpc_req_finished(req);
1208 class_import_put(imp);
1212 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1214 __u32 keylen, vallen;
1218 if (gf->gf_pathlen > PATH_MAX)
1219 RETURN(-ENAMETOOLONG);
1220 if (gf->gf_pathlen < 2)
1223 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1224 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1225 OBD_ALLOC(key, keylen);
1228 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1229 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1231 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1232 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1234 if (!fid_is_sane(&gf->gf_fid))
1235 GOTO(out, rc = -EINVAL);
1237 /* Val is struct getinfo_fid2path result plus path */
1238 vallen = sizeof(*gf) + gf->gf_pathlen;
1240 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1241 if (rc != 0 && rc != -EREMOTE)
1244 if (vallen <= sizeof(*gf))
1245 GOTO(out, rc = -EPROTO);
1246 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1247 GOTO(out, rc = -EOVERFLOW);
1249 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1250 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1253 OBD_FREE(key, keylen);
1257 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1258 struct hsm_progress_kernel *hpk)
1260 struct obd_import *imp = class_exp2cliimp(exp);
1261 struct hsm_progress_kernel *req_hpk;
1262 struct ptlrpc_request *req;
1266 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1267 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1269 GOTO(out, rc = -ENOMEM);
1271 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1273 /* Copy hsm_progress struct */
1274 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1275 if (req_hpk == NULL)
1276 GOTO(out, rc = -EPROTO);
1279 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1281 ptlrpc_request_set_replen(req);
1283 rc = mdc_queue_wait(req);
1286 ptlrpc_req_finished(req);
1290 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1292 __u32 *archive_mask;
1293 struct ptlrpc_request *req;
1297 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1299 MDS_HSM_CT_REGISTER);
1301 GOTO(out, rc = -ENOMEM);
1303 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1305 /* Copy hsm_progress struct */
1306 archive_mask = req_capsule_client_get(&req->rq_pill,
1307 &RMF_MDS_HSM_ARCHIVE);
1308 if (archive_mask == NULL)
1309 GOTO(out, rc = -EPROTO);
1311 *archive_mask = archives;
1313 ptlrpc_request_set_replen(req);
1315 rc = mdc_queue_wait(req);
1318 ptlrpc_req_finished(req);
1322 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1323 struct md_op_data *op_data)
1325 struct hsm_current_action *hca = op_data->op_data;
1326 struct hsm_current_action *req_hca;
1327 struct ptlrpc_request *req;
1331 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1332 &RQF_MDS_HSM_ACTION);
1336 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1338 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1340 ptlrpc_request_free(req);
1344 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1345 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1347 ptlrpc_request_set_replen(req);
1349 rc = mdc_queue_wait(req);
1353 req_hca = req_capsule_server_get(&req->rq_pill,
1354 &RMF_MDS_HSM_CURRENT_ACTION);
1355 if (req_hca == NULL)
1356 GOTO(out, rc = -EPROTO);
1362 ptlrpc_req_finished(req);
1366 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1368 struct ptlrpc_request *req;
1372 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1374 MDS_HSM_CT_UNREGISTER);
1376 GOTO(out, rc = -ENOMEM);
1378 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1380 ptlrpc_request_set_replen(req);
1382 rc = mdc_queue_wait(req);
1385 ptlrpc_req_finished(req);
1389 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1390 struct md_op_data *op_data)
1392 struct hsm_user_state *hus = op_data->op_data;
1393 struct hsm_user_state *req_hus;
1394 struct ptlrpc_request *req;
1398 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1399 &RQF_MDS_HSM_STATE_GET);
1403 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1405 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1407 ptlrpc_request_free(req);
1411 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1412 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1414 ptlrpc_request_set_replen(req);
1416 rc = mdc_queue_wait(req);
1420 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1421 if (req_hus == NULL)
1422 GOTO(out, rc = -EPROTO);
1428 ptlrpc_req_finished(req);
1432 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1433 struct md_op_data *op_data)
1435 struct hsm_state_set *hss = op_data->op_data;
1436 struct hsm_state_set *req_hss;
1437 struct ptlrpc_request *req;
1441 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1442 &RQF_MDS_HSM_STATE_SET);
1446 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1448 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1450 ptlrpc_request_free(req);
1454 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1455 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1458 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1459 if (req_hss == NULL)
1460 GOTO(out, rc = -EPROTO);
1463 ptlrpc_request_set_replen(req);
1465 rc = mdc_queue_wait(req);
1470 ptlrpc_req_finished(req);
1474 static int mdc_ioc_hsm_request(struct obd_export *exp,
1475 struct hsm_user_request *hur)
1477 struct obd_import *imp = class_exp2cliimp(exp);
1478 struct ptlrpc_request *req;
1479 struct hsm_request *req_hr;
1480 struct hsm_user_item *req_hui;
1485 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1487 GOTO(out, rc = -ENOMEM);
1489 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1490 hur->hur_request.hr_itemcount
1491 * sizeof(struct hsm_user_item));
1492 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1493 hur->hur_request.hr_data_len);
1495 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1497 ptlrpc_request_free(req);
1501 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1503 /* Copy hsm_request struct */
1504 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1506 GOTO(out, rc = -EPROTO);
1507 *req_hr = hur->hur_request;
1509 /* Copy hsm_user_item structs */
1510 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1511 if (req_hui == NULL)
1512 GOTO(out, rc = -EPROTO);
1513 memcpy(req_hui, hur->hur_user_item,
1514 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1516 /* Copy opaque field */
1517 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1518 if (req_opaque == NULL)
1519 GOTO(out, rc = -EPROTO);
1520 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1522 ptlrpc_request_set_replen(req);
1524 rc = mdc_queue_wait(req);
1528 ptlrpc_req_finished(req);
1532 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1534 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1536 LASSERT(len <= CR_MAXSIZE);
1538 lh->kuc_magic = KUC_MAGIC;
1539 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1540 lh->kuc_flags = flags;
1541 lh->kuc_msgtype = CL_RECORD;
1542 lh->kuc_msglen = len;
1546 #define D_CHANGELOG 0
1548 struct changelog_show {
1553 struct obd_device *cs_obd;
1556 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1557 struct llog_rec_hdr *hdr, void *data)
1559 struct changelog_show *cs = data;
1560 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1565 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1567 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1568 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1569 rec->cr.cr_type, rc);
1573 if (rec->cr.cr_index < cs->cs_startrec) {
1574 /* Skip entries earlier than what we are interested in */
1575 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1576 rec->cr.cr_index, cs->cs_startrec);
1580 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1581 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1582 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1583 rec->cr.cr_flags & CLF_FLAGMASK,
1584 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1585 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1587 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1589 /* Set up the message */
1590 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1591 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1593 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1594 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1599 static int mdc_changelog_send_thread(void *csdata)
1601 struct changelog_show *cs = csdata;
1602 struct llog_ctxt *ctxt = NULL;
1603 struct llog_handle *llh = NULL;
1604 struct kuc_hdr *kuch;
1607 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1608 cs->cs_fp, cs->cs_startrec);
1610 OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1611 if (cs->cs_buf == NULL)
1612 GOTO(out, rc = -ENOMEM);
1614 /* Set up the remote catalog handle */
1615 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1617 GOTO(out, rc = -ENOENT);
1618 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1621 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1622 cs->cs_obd->obd_name, rc);
1625 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1627 CERROR("llog_init_handle failed %d\n", rc);
1631 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1633 /* Send EOF no matter what our result */
1634 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1636 kuch->kuc_msgtype = CL_EOF;
1637 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1643 llog_cat_close(NULL, llh);
1645 llog_ctxt_put(ctxt);
1647 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1652 static int mdc_ioc_changelog_send(struct obd_device *obd,
1653 struct ioc_changelog *icc)
1655 struct changelog_show *cs;
1658 /* Freed in mdc_changelog_send_thread */
1664 cs->cs_startrec = icc->icc_recno;
1665 /* matching fput in mdc_changelog_send_thread */
1666 cs->cs_fp = fget(icc->icc_id);
1667 cs->cs_flags = icc->icc_flags;
1670 * New thread because we should return to user app before
1671 * writing into our pipe
1673 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1674 "mdc_clg_send_thread"));
1675 if (!IS_ERR_VALUE(rc)) {
1676 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1680 CERROR("Failed to start changelog thread: %d\n", rc);
1685 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1686 struct lustre_kernelcomm *lk);
1688 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1689 struct obd_quotactl *oqctl)
1691 struct client_obd *cli = &exp->exp_obd->u.cli;
1692 struct ptlrpc_request *req;
1693 struct obd_quotactl *body;
1697 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1698 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1703 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1706 ptlrpc_request_set_replen(req);
1708 /* the next poll will find -ENODATA, that means quotacheck is
1710 cli->cl_qchk_stat = -ENODATA;
1711 rc = ptlrpc_queue_wait(req);
1713 cli->cl_qchk_stat = rc;
1714 ptlrpc_req_finished(req);
1718 static int mdc_quota_poll_check(struct obd_export *exp,
1719 struct if_quotacheck *qchk)
1721 struct client_obd *cli = &exp->exp_obd->u.cli;
1725 qchk->obd_uuid = cli->cl_target_uuid;
1726 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1728 rc = cli->cl_qchk_stat;
1729 /* the client is not the previous one */
1730 if (rc == CL_NOT_QUOTACHECKED)
1735 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1736 struct obd_quotactl *oqctl)
1738 struct ptlrpc_request *req;
1739 struct obd_quotactl *oqc;
1743 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1744 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1749 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1752 ptlrpc_request_set_replen(req);
1753 ptlrpc_at_set_req_timeout(req);
1754 req->rq_no_resend = 1;
1756 rc = ptlrpc_queue_wait(req);
1758 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1760 if (req->rq_repmsg &&
1761 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1764 CERROR ("Can't unpack obd_quotactl\n");
1767 ptlrpc_req_finished(req);
1772 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1773 struct md_op_data *op_data)
1775 CFS_LIST_HEAD(cancels);
1776 struct ptlrpc_request *req;
1778 struct mdc_swap_layouts *msl, *payload;
1781 msl = op_data->op_data;
1783 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1784 * first thing it will do is to cancel the 2 layout
1785 * locks hold by this client.
1786 * So the client must cancel its layout locks on the 2 fids
1787 * with the request RPC to avoid extra RPC round trips
1789 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1790 LCK_CR, MDS_INODELOCK_LAYOUT);
1791 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1792 LCK_CR, MDS_INODELOCK_LAYOUT);
1794 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1795 &RQF_MDS_SWAP_LAYOUTS);
1797 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1801 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1802 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1804 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1806 ptlrpc_request_free(req);
1810 mdc_swap_layouts_pack(req, op_data);
1812 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1817 ptlrpc_request_set_replen(req);
1819 rc = ptlrpc_queue_wait(req);
1825 ptlrpc_req_finished(req);
1829 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1830 void *karg, void *uarg)
1832 struct obd_device *obd = exp->exp_obd;
1833 struct obd_ioctl_data *data = karg;
1834 struct obd_import *imp = obd->u.cli.cl_import;
1835 struct llog_ctxt *ctxt;
1839 if (!try_module_get(THIS_MODULE)) {
1840 CERROR("Can't get module. Is it alive?");
1844 case OBD_IOC_CHANGELOG_SEND:
1845 rc = mdc_ioc_changelog_send(obd, karg);
1847 case OBD_IOC_CHANGELOG_CLEAR: {
1848 struct ioc_changelog *icc = karg;
1849 struct changelog_setinfo cs =
1850 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1851 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1852 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1856 case OBD_IOC_FID2PATH:
1857 rc = mdc_ioc_fid2path(exp, karg);
1859 case LL_IOC_HSM_CT_START:
1860 rc = mdc_ioc_hsm_ct_start(exp, karg);
1861 /* ignore if it was already registered on this MDS. */
1865 case LL_IOC_HSM_PROGRESS:
1866 rc = mdc_ioc_hsm_progress(exp, karg);
1868 case LL_IOC_HSM_STATE_GET:
1869 rc = mdc_ioc_hsm_state_get(exp, karg);
1871 case LL_IOC_HSM_STATE_SET:
1872 rc = mdc_ioc_hsm_state_set(exp, karg);
1874 case LL_IOC_HSM_ACTION:
1875 rc = mdc_ioc_hsm_current_action(exp, karg);
1877 case LL_IOC_HSM_REQUEST:
1878 rc = mdc_ioc_hsm_request(exp, karg);
1880 case OBD_IOC_CLIENT_RECOVER:
1881 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1885 case IOC_OSC_SET_ACTIVE:
1886 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1888 case OBD_IOC_PARSE: {
1889 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1890 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1892 llog_ctxt_put(ctxt);
1896 case OBD_IOC_LLOG_INFO:
1897 case OBD_IOC_LLOG_PRINT: {
1898 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1899 rc = llog_ioctl(NULL, ctxt, cmd, data);
1900 llog_ctxt_put(ctxt);
1904 case OBD_IOC_POLL_QUOTACHECK:
1905 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1907 case OBD_IOC_PING_TARGET:
1908 rc = ptlrpc_obd_ping(obd);
1911 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1912 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1913 * there'd be no LMV layer thus we might be called here. Eventually
1914 * this code should be removed.
1917 case IOC_OBD_STATFS: {
1918 struct obd_statfs stat_buf = {0};
1920 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1921 GOTO(out, rc = -ENODEV);
1924 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1925 min((int)data->ioc_plen2,
1926 (int)sizeof(struct obd_uuid))))
1927 GOTO(out, rc = -EFAULT);
1929 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1930 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1935 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1936 min((int) data->ioc_plen1,
1937 (int) sizeof(stat_buf))))
1938 GOTO(out, rc = -EFAULT);
1942 case OBD_IOC_QUOTACTL: {
1943 struct if_quotactl *qctl = karg;
1944 struct obd_quotactl *oqctl;
1946 OBD_ALLOC_PTR(oqctl);
1948 GOTO(out, rc = -ENOMEM);
1950 QCTL_COPY(oqctl, qctl);
1951 rc = obd_quotactl(exp, oqctl);
1953 QCTL_COPY(qctl, oqctl);
1954 qctl->qc_valid = QC_MDTIDX;
1955 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1958 OBD_FREE_PTR(oqctl);
1961 case LL_IOC_GET_CONNECT_FLAGS:
1962 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1963 sizeof(*exp_connect_flags_ptr(exp))))
1964 GOTO(out, rc = -EFAULT);
1967 case LL_IOC_LOV_SWAP_LAYOUTS:
1968 rc = mdc_ioc_swap_layouts(exp, karg);
1971 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1972 GOTO(out, rc = -ENOTTY);
1975 module_put(THIS_MODULE);
1980 int mdc_get_info_rpc(struct obd_export *exp,
1981 obd_count keylen, void *key,
1982 int vallen, void *val)
1984 struct obd_import *imp = class_exp2cliimp(exp);
1985 struct ptlrpc_request *req;
1990 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1994 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1995 RCL_CLIENT, keylen);
1996 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1997 RCL_CLIENT, sizeof(__u32));
1999 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2001 ptlrpc_request_free(req);
2005 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2006 memcpy(tmp, key, keylen);
2007 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2008 memcpy(tmp, &vallen, sizeof(__u32));
2010 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2011 RCL_SERVER, vallen);
2012 ptlrpc_request_set_replen(req);
2014 rc = ptlrpc_queue_wait(req);
2015 /* -EREMOTE means the get_info result is partial, and it needs to
2016 * continue on another MDT, see fid2path part in lmv_iocontrol */
2017 if (rc == 0 || rc == -EREMOTE) {
2018 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2019 memcpy(val, tmp, vallen);
2020 if (ptlrpc_rep_need_swab(req)) {
2021 if (KEY_IS(KEY_FID2PATH))
2022 lustre_swab_fid2path(val);
2025 ptlrpc_req_finished(req);
2030 static void lustre_swab_hai(struct hsm_action_item *h)
2032 __swab32s(&h->hai_len);
2033 __swab32s(&h->hai_action);
2034 lustre_swab_lu_fid(&h->hai_fid);
2035 lustre_swab_lu_fid(&h->hai_dfid);
2036 __swab64s(&h->hai_cookie);
2037 __swab64s(&h->hai_extent.offset);
2038 __swab64s(&h->hai_extent.length);
2039 __swab64s(&h->hai_gid);
2042 static void lustre_swab_hal(struct hsm_action_list *h)
2044 struct hsm_action_item *hai;
2047 __swab32s(&h->hal_version);
2048 __swab32s(&h->hal_count);
2049 __swab32s(&h->hal_archive_id);
2050 __swab64s(&h->hal_flags);
2052 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2053 lustre_swab_hai(hai);
2056 static void lustre_swab_kuch(struct kuc_hdr *l)
2058 __swab16s(&l->kuc_magic);
2059 /* __u8 l->kuc_transport */
2060 __swab16s(&l->kuc_msgtype);
2061 __swab16s(&l->kuc_msglen);
2064 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2065 struct lustre_kernelcomm *lk)
2067 struct obd_import *imp = class_exp2cliimp(exp);
2068 __u32 archive = lk->lk_data;
2071 if (lk->lk_group != KUC_GRP_HSM) {
2072 CERROR("Bad copytool group %d\n", lk->lk_group);
2076 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2077 lk->lk_uid, lk->lk_group, lk->lk_flags);
2079 if (lk->lk_flags & LK_FLG_STOP) {
2080 /* Unregister with the coordinator */
2081 rc = mdc_ioc_hsm_ct_unregister(imp);
2083 rc = mdc_ioc_hsm_ct_register(imp, archive);
2090 * Send a message to any listening copytools
2091 * @param val KUC message (kuc_hdr + hsm_action_list)
2092 * @param len total length of message
2094 static int mdc_hsm_copytool_send(int len, void *val)
2096 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2097 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2101 if (len < sizeof(*lh) + sizeof(*hal)) {
2102 CERROR("Short HSM message %d < %d\n", len,
2103 (int) (sizeof(*lh) + sizeof(*hal)));
2106 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2107 lustre_swab_kuch(lh);
2108 lustre_swab_hal(hal);
2109 } else if (lh->kuc_magic != KUC_MAGIC) {
2110 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2114 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2116 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2117 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2119 /* Broadcast to HSM listeners */
2120 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2126 * callback function passed to kuc for re-registering each HSM copytool
2127 * running on MDC, after MDT shutdown/recovery.
2128 * @param data archive id served by the copytool
2129 * @param cb_arg callback argument (obd_import)
2131 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2133 struct obd_import *imp = (struct obd_import *)cb_arg;
2134 __u32 archive = data;
2137 CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2139 rc = mdc_ioc_hsm_ct_register(imp, archive);
2141 /* ignore error if the copytool is already registered */
2142 return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2146 * Re-establish all kuc contexts with MDT
2147 * after MDT shutdown/recovery.
2149 static int mdc_kuc_reregister(struct obd_import *imp)
2151 /* re-register HSM agents */
2152 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2156 int mdc_set_info_async(const struct lu_env *env,
2157 struct obd_export *exp,
2158 obd_count keylen, void *key,
2159 obd_count vallen, void *val,
2160 struct ptlrpc_request_set *set)
2162 struct obd_import *imp = class_exp2cliimp(exp);
2166 if (KEY_IS(KEY_READ_ONLY)) {
2167 if (vallen != sizeof(int))
2170 spin_lock(&imp->imp_lock);
2171 if (*((int *)val)) {
2172 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2173 imp->imp_connect_data.ocd_connect_flags |=
2176 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2177 imp->imp_connect_data.ocd_connect_flags &=
2178 ~OBD_CONNECT_RDONLY;
2180 spin_unlock(&imp->imp_lock);
2182 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2183 keylen, key, vallen, val, set);
2186 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2187 sptlrpc_conf_client_adapt(exp->exp_obd);
2190 if (KEY_IS(KEY_FLUSH_CTX)) {
2191 sptlrpc_import_flush_my_ctx(imp);
2194 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2195 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2196 keylen, key, vallen, val, set);
2199 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2200 rc = mdc_hsm_copytool_send(vallen, val);
2204 CERROR("Unknown key %s\n", (char *)key);
2208 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2209 __u32 keylen, void *key, __u32 *vallen, void *val,
2210 struct lov_stripe_md *lsm)
2214 if (KEY_IS(KEY_MAX_EASIZE)) {
2215 int mdsize, *max_easize;
2217 if (*vallen != sizeof(int))
2219 mdsize = *(int*)val;
2220 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2221 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2223 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2225 } else if (KEY_IS(KEY_CONN_DATA)) {
2226 struct obd_import *imp = class_exp2cliimp(exp);
2227 struct obd_connect_data *data = val;
2229 if (*vallen != sizeof(*data))
2232 *data = imp->imp_connect_data;
2234 } else if (KEY_IS(KEY_TGT_COUNT)) {
2239 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2244 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2245 struct obd_capa *oc, struct obd_client_handle *handle,
2248 struct ptlrpc_request *req;
2249 struct mdt_body *body;
2253 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2257 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2259 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2261 ptlrpc_request_free(req);
2265 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2267 ptlrpc_request_set_replen(req);
2269 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2270 rc = ptlrpc_queue_wait(req);
2271 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2273 CERROR("Pin failed: %d\n", rc);
2277 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2279 GOTO(err_out, rc = -EPROTO);
2281 handle->och_fh = body->handle;
2282 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2284 handle->och_mod = obd_mod_alloc();
2285 if (handle->och_mod == NULL) {
2286 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2287 GOTO(err_out, rc = -ENOMEM);
2289 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2294 ptlrpc_req_finished(req);
2298 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2301 struct ptlrpc_request *req;
2302 struct mdt_body *body;
2306 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2307 LUSTRE_MDS_VERSION, MDS_UNPIN);
2311 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2312 body->handle = handle->och_fh;
2315 ptlrpc_request_set_replen(req);
2317 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2318 rc = ptlrpc_queue_wait(req);
2319 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2322 CERROR("Unpin failed: %d\n", rc);
2324 ptlrpc_req_finished(req);
2325 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2327 obd_mod_put(handle->och_mod);
2331 int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2332 struct obd_capa *oc, struct ptlrpc_request **request)
2334 struct ptlrpc_request *req;
2339 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2343 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2345 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2347 ptlrpc_request_free(req);
2351 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2353 ptlrpc_request_set_replen(req);
2355 rc = ptlrpc_queue_wait(req);
2357 ptlrpc_req_finished(req);
2363 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2364 enum obd_import_event event)
2368 LASSERT(imp->imp_obd == obd);
2371 case IMP_EVENT_DISCON: {
2373 /* XXX Pass event up to OBDs stack. used only for FLD now */
2374 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2378 case IMP_EVENT_INACTIVE: {
2379 struct client_obd *cli = &obd->u.cli;
2381 * Flush current sequence to make client obtain new one
2382 * from server in case of disconnect/reconnect.
2384 if (cli->cl_seq != NULL)
2385 seq_client_flush(cli->cl_seq);
2387 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2390 case IMP_EVENT_INVALIDATE: {
2391 struct ldlm_namespace *ns = obd->obd_namespace;
2393 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2397 case IMP_EVENT_ACTIVE:
2398 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2399 /* redo the kuc registration after reconnecting */
2401 rc = mdc_kuc_reregister(imp);
2404 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2406 case IMP_EVENT_DEACTIVATE:
2407 case IMP_EVENT_ACTIVATE:
2410 CERROR("Unknown import event %x\n", event);
2416 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2417 struct md_op_data *op_data)
2419 struct client_obd *cli = &exp->exp_obd->u.cli;
2420 struct lu_client_seq *seq = cli->cl_seq;
2422 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2425 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2426 struct client_obd *cli = &exp->exp_obd->u.cli;
2427 return &cli->cl_target_uuid;
2431 * Determine whether the lock can be canceled before replaying it during
2432 * recovery, non zero value will be return if the lock can be canceled,
2433 * or zero returned for not
2435 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2437 if (lock->l_resource->lr_type != LDLM_IBITS)
2440 /* FIXME: if we ever get into a situation where there are too many
2441 * opened files with open locks on a single node, then we really
2442 * should replay these open locks to reget it */
2443 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2449 static int mdc_resource_inode_free(struct ldlm_resource *res)
2451 if (res->lr_lvb_inode)
2452 res->lr_lvb_inode = NULL;
2457 struct ldlm_valblock_ops inode_lvbo = {
2458 lvbo_free: mdc_resource_inode_free
2461 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2463 struct client_obd *cli = &obd->u.cli;
2464 struct lprocfs_static_vars lvars = { 0 };
2468 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2469 if (!cli->cl_rpc_lock)
2471 mdc_init_rpc_lock(cli->cl_rpc_lock);
2473 rc = ptlrpcd_addref();
2475 GOTO(err_rpc_lock, rc);
2477 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2478 if (!cli->cl_close_lock)
2479 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2480 mdc_init_rpc_lock(cli->cl_close_lock);
2482 rc = client_obd_setup(obd, cfg);
2484 GOTO(err_close_lock, rc);
2485 lprocfs_mdc_init_vars(&lvars);
2486 lprocfs_obd_setup(obd, lvars.obd_vars);
2487 lprocfs_alloc_md_stats(obd, 0);
2488 sptlrpc_lprocfs_cliobd_attach(obd);
2489 ptlrpc_lprocfs_register_obd(obd);
2491 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2493 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2495 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2498 CERROR("failed to setup llogging subsystems\n");
2504 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2508 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2512 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2513 * us to make MDS RPCs with large enough reply buffers to hold the
2514 * maximum-sized (= maximum striped) EA and cookie without having to
2515 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2516 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2517 int def_easize, int cookiesize)
2519 struct obd_device *obd = exp->exp_obd;
2520 struct client_obd *cli = &obd->u.cli;
2523 if (cli->cl_max_mds_easize < easize)
2524 cli->cl_max_mds_easize = easize;
2526 if (cli->cl_default_mds_easize < def_easize)
2527 cli->cl_default_mds_easize = def_easize;
2529 if (cli->cl_max_mds_cookiesize < cookiesize)
2530 cli->cl_max_mds_cookiesize = cookiesize;
2535 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2541 case OBD_CLEANUP_EARLY:
2543 case OBD_CLEANUP_EXPORTS:
2544 /* Failsafe, ok if racy */
2545 if (obd->obd_type->typ_refcnt <= 1)
2546 libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2548 obd_cleanup_client_import(obd);
2549 ptlrpc_lprocfs_unregister_obd(obd);
2550 lprocfs_obd_cleanup(obd);
2551 lprocfs_free_md_stats(obd);
2553 rc = obd_llog_finish(obd, 0);
2555 CERROR("failed to cleanup llogging subsystems\n");
2561 static int mdc_cleanup(struct obd_device *obd)
2563 struct client_obd *cli = &obd->u.cli;
2565 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2566 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2570 return client_obd_cleanup(obd);
2574 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2575 struct obd_device *tgt, int *index)
2577 struct llog_ctxt *ctxt;
2582 LASSERT(olg == &obd->obd_olg);
2584 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2589 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2590 llog_initiator_connect(ctxt);
2591 llog_ctxt_put(ctxt);
2596 static int mdc_llog_finish(struct obd_device *obd, int count)
2598 struct llog_ctxt *ctxt;
2602 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2604 llog_cleanup(NULL, ctxt);
2609 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2611 struct lustre_cfg *lcfg = buf;
2612 struct lprocfs_static_vars lvars = { 0 };
2615 lprocfs_mdc_init_vars(&lvars);
2616 switch (lcfg->lcfg_command) {
2618 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2628 /* get remote permission for current user on fid */
2629 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2630 struct obd_capa *oc, __u32 suppgid,
2631 struct ptlrpc_request **request)
2633 struct ptlrpc_request *req;
2637 LASSERT(client_is_remote(exp));
2640 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2644 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2646 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2648 ptlrpc_request_free(req);
2652 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2654 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2655 sizeof(struct mdt_remote_perm));
2657 ptlrpc_request_set_replen(req);
2659 rc = ptlrpc_queue_wait(req);
2661 ptlrpc_req_finished(req);
2667 static int mdc_interpret_renew_capa(const struct lu_env *env,
2668 struct ptlrpc_request *req, void *args,
2671 struct mdc_renew_capa_args *ra = args;
2672 struct mdt_body *body = NULL;
2673 struct lustre_capa *capa;
2677 GOTO(out, capa = ERR_PTR(status));
2679 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2681 GOTO(out, capa = ERR_PTR(-EFAULT));
2683 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2684 GOTO(out, capa = ERR_PTR(-ENOENT));
2686 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2688 GOTO(out, capa = ERR_PTR(-EFAULT));
2691 ra->ra_cb(ra->ra_oc, capa);
2695 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2698 struct ptlrpc_request *req;
2699 struct mdc_renew_capa_args *ra;
2702 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2703 LUSTRE_MDS_VERSION, MDS_GETATTR);
2707 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2708 * capa to renew is oss capa.
2710 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2711 ptlrpc_request_set_replen(req);
2713 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2714 ra = ptlrpc_req_async_args(req);
2717 req->rq_interpret_reply = mdc_interpret_renew_capa;
2718 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2722 struct obd_ops mdc_obd_ops = {
2723 .o_owner = THIS_MODULE,
2724 .o_setup = mdc_setup,
2725 .o_precleanup = mdc_precleanup,
2726 .o_cleanup = mdc_cleanup,
2727 .o_add_conn = client_import_add_conn,
2728 .o_del_conn = client_import_del_conn,
2729 .o_connect = client_connect_import,
2730 .o_disconnect = client_disconnect_export,
2731 .o_iocontrol = mdc_iocontrol,
2732 .o_set_info_async = mdc_set_info_async,
2733 .o_statfs = mdc_statfs,
2735 .o_unpin = mdc_unpin,
2736 .o_fid_init = client_fid_init,
2737 .o_fid_fini = client_fid_fini,
2738 .o_fid_alloc = mdc_fid_alloc,
2739 .o_import_event = mdc_import_event,
2740 .o_llog_init = mdc_llog_init,
2741 .o_llog_finish = mdc_llog_finish,
2742 .o_get_info = mdc_get_info,
2743 .o_process_config = mdc_process_config,
2744 .o_get_uuid = mdc_get_uuid,
2745 .o_quotactl = mdc_quotactl,
2746 .o_quotacheck = mdc_quotacheck
2749 struct md_ops mdc_md_ops = {
2750 .m_getstatus = mdc_getstatus,
2751 .m_null_inode = mdc_null_inode,
2752 .m_find_cbdata = mdc_find_cbdata,
2753 .m_close = mdc_close,
2754 .m_create = mdc_create,
2755 .m_done_writing = mdc_done_writing,
2756 .m_enqueue = mdc_enqueue,
2757 .m_getattr = mdc_getattr,
2758 .m_getattr_name = mdc_getattr_name,
2759 .m_intent_lock = mdc_intent_lock,
2761 .m_is_subdir = mdc_is_subdir,
2762 .m_rename = mdc_rename,
2763 .m_setattr = mdc_setattr,
2764 .m_setxattr = mdc_setxattr,
2765 .m_getxattr = mdc_getxattr,
2766 .m_fsync = mdc_fsync,
2767 .m_readpage = mdc_readpage,
2768 .m_unlink = mdc_unlink,
2769 .m_cancel_unused = mdc_cancel_unused,
2770 .m_init_ea_size = mdc_init_ea_size,
2771 .m_set_lock_data = mdc_set_lock_data,
2772 .m_lock_match = mdc_lock_match,
2773 .m_get_lustre_md = mdc_get_lustre_md,
2774 .m_free_lustre_md = mdc_free_lustre_md,
2775 .m_set_open_replay_data = mdc_set_open_replay_data,
2776 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2777 .m_renew_capa = mdc_renew_capa,
2778 .m_unpack_capa = mdc_unpack_capa,
2779 .m_get_remote_perm = mdc_get_remote_perm,
2780 .m_intent_getattr_async = mdc_intent_getattr_async,
2781 .m_revalidate_lock = mdc_revalidate_lock
2784 int __init mdc_init(void)
2787 struct lprocfs_static_vars lvars = { 0 };
2788 lprocfs_mdc_init_vars(&lvars);
2790 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2791 LUSTRE_MDC_NAME, NULL);
2796 static void /*__exit*/ mdc_exit(void)
2798 class_unregister_type(LUSTRE_MDC_NAME);
2801 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2802 MODULE_DESCRIPTION("Lustre Metadata Client");
2803 MODULE_LICENSE("GPL");
2805 module_init(mdc_init);
2806 module_exit(mdc_exit);