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 lookup_intent *it)
747 struct md_open_data *mod;
748 struct mdt_rec_create *rec;
749 struct mdt_body *body;
750 struct ptlrpc_request *open_req = it->d.lustre.it_data;
751 struct obd_import *imp = open_req->rq_import;
754 if (!open_req->rq_replay)
757 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
758 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
759 LASSERT(rec != NULL);
760 /* Incoming message in my byte order (it's been swabbed). */
761 /* Outgoing messages always in my byte order. */
762 LASSERT(body != NULL);
764 /* Only if the import is replayable, we set replay_open data */
765 if (och && imp->imp_replayable) {
766 mod = obd_mod_alloc();
768 DEBUG_REQ(D_ERROR, open_req,
769 "Can't allocate md_open_data");
774 * Take a reference on \var mod, to be freed on mdc_close().
775 * It protects \var mod from being freed on eviction (commit
776 * callback is called despite rq_replay flag).
777 * Another reference for \var och.
782 spin_lock(&open_req->rq_lock);
785 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
786 it_disposition(it, DISP_OPEN_STRIPE);
787 mod->mod_open_req = open_req;
788 open_req->rq_cb_data = mod;
789 open_req->rq_commit_cb = mdc_commit_open;
790 spin_unlock(&open_req->rq_lock);
793 rec->cr_fid2 = body->fid1;
794 rec->cr_ioepoch = body->ioepoch;
795 rec->cr_old_handle.cookie = body->handle.cookie;
796 open_req->rq_replay_cb = mdc_replay_open;
797 if (!fid_is_sane(&body->fid1)) {
798 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
803 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
807 static void mdc_free_open(struct md_open_data *mod)
811 if (mod->mod_is_create == 0 &&
812 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
815 LASSERT(mod->mod_open_req->rq_replay == 0);
817 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
819 ptlrpc_request_committed(mod->mod_open_req, committed);
820 if (mod->mod_close_req)
821 ptlrpc_request_committed(mod->mod_close_req, committed);
824 int mdc_clear_open_replay_data(struct obd_export *exp,
825 struct obd_client_handle *och)
827 struct md_open_data *mod = och->och_mod;
831 * It is possible to not have \var mod in a case of eviction between
832 * lookup and ll_file_open().
837 LASSERT(mod != LP_POISON);
838 LASSERT(mod->mod_open_req != NULL);
848 /* Prepares the request for the replay by the given reply */
849 static void mdc_close_handle_reply(struct ptlrpc_request *req,
850 struct md_op_data *op_data, int rc) {
851 struct mdt_body *repbody;
852 struct mdt_ioepoch *epoch;
854 if (req && rc == -EAGAIN) {
855 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
856 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
858 epoch->flags |= MF_SOM_AU;
859 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
860 op_data->op_flags |= MF_GETATTR_LOCK;
864 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
865 struct md_open_data *mod, struct ptlrpc_request **request)
867 struct obd_device *obd = class_exp2obd(exp);
868 struct ptlrpc_request *req;
869 struct req_format *req_fmt;
874 req_fmt = &RQF_MDS_CLOSE;
875 if (op_data->op_bias & MDS_HSM_RELEASE) {
876 req_fmt = &RQF_MDS_RELEASE_CLOSE;
878 /* allocate a FID for volatile file */
879 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
881 CERROR("%s: "DFID" failed to allocate FID: %d\n",
882 obd->obd_name, PFID(&op_data->op_fid1), rc);
883 /* save the errcode and proceed to close */
889 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
893 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
895 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
897 ptlrpc_request_free(req);
901 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
902 * portal whose threads are not taking any DLM locks and are therefore
903 * always progressing */
904 req->rq_request_portal = MDS_READPAGE_PORTAL;
905 ptlrpc_at_set_req_timeout(req);
907 /* Ensure that this close's handle is fixed up during replay. */
908 if (likely(mod != NULL)) {
909 LASSERTF(mod->mod_open_req != NULL &&
910 mod->mod_open_req->rq_type != LI_POISON,
911 "POISONED open %p!\n", mod->mod_open_req);
913 mod->mod_close_req = req;
915 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
916 /* We no longer want to preserve this open for replay even
917 * though the open was committed. b=3632, b=3633 */
918 spin_lock(&mod->mod_open_req->rq_lock);
919 mod->mod_open_req->rq_replay = 0;
920 spin_unlock(&mod->mod_open_req->rq_lock);
922 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
925 mdc_close_pack(req, op_data);
927 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
928 obd->u.cli.cl_max_mds_easize);
929 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
930 obd->u.cli.cl_max_mds_cookiesize);
932 ptlrpc_request_set_replen(req);
934 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
935 rc = ptlrpc_queue_wait(req);
936 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
938 if (req->rq_repmsg == NULL) {
939 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
942 rc = req->rq_status ?: -EIO;
943 } else if (rc == 0 || rc == -EAGAIN) {
944 struct mdt_body *body;
946 rc = lustre_msg_get_status(req->rq_repmsg);
947 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
948 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
953 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
956 } else if (rc == -ESTALE) {
958 * it can be allowed error after 3633 if open was committed and
959 * server failed before close was sent. Let's check if mod
960 * exists and return no error in that case
963 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
964 LASSERT(mod->mod_open_req != NULL);
965 if (mod->mod_open_req->rq_committed)
972 mod->mod_close_req = NULL;
973 /* Since now, mod is accessed through open_req only,
974 * thus close req does not keep a reference on mod anymore. */
978 mdc_close_handle_reply(req, op_data, rc);
979 RETURN(rc < 0 ? rc : saved_rc);
982 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
983 struct md_open_data *mod)
985 struct obd_device *obd = class_exp2obd(exp);
986 struct ptlrpc_request *req;
990 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
991 &RQF_MDS_DONE_WRITING);
995 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
996 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
998 ptlrpc_request_free(req);
1003 LASSERTF(mod->mod_open_req != NULL &&
1004 mod->mod_open_req->rq_type != LI_POISON,
1005 "POISONED setattr %p!\n", mod->mod_open_req);
1007 mod->mod_close_req = req;
1008 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
1009 /* We no longer want to preserve this setattr for replay even
1010 * though the open was committed. b=3632, b=3633 */
1011 spin_lock(&mod->mod_open_req->rq_lock);
1012 mod->mod_open_req->rq_replay = 0;
1013 spin_unlock(&mod->mod_open_req->rq_lock);
1016 mdc_close_pack(req, op_data);
1017 ptlrpc_request_set_replen(req);
1019 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1020 rc = ptlrpc_queue_wait(req);
1021 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1023 if (rc == -ESTALE) {
1025 * it can be allowed error after 3633 if open or setattr were
1026 * committed and server failed before close was sent.
1027 * Let's check if mod exists and return no error in that case
1030 LASSERT(mod->mod_open_req != NULL);
1031 if (mod->mod_open_req->rq_committed)
1038 mod->mod_close_req = NULL;
1039 LASSERT(mod->mod_open_req != NULL);
1042 /* Since now, mod is accessed through setattr req only,
1043 * thus DW req does not keep a reference on mod anymore. */
1047 mdc_close_handle_reply(req, op_data, rc);
1048 ptlrpc_req_finished(req);
1052 #ifdef HAVE_SPLIT_SUPPORT
1053 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
1054 const struct page *page, int offset)
1056 struct ptlrpc_request *req;
1057 struct ptlrpc_bulk_desc *desc;
1061 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1065 /* FIXME: capa doesn't support split yet */
1066 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1068 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1070 ptlrpc_request_free(req);
1074 req->rq_request_portal = MDS_READPAGE_PORTAL;
1075 ptlrpc_at_set_req_timeout(req);
1077 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1079 GOTO(out, rc = -ENOMEM);
1081 /* NB req now owns desc and will free it when it gets freed. */
1082 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1083 mdc_readdir_pack(req, 0, offset, fid, NULL);
1085 ptlrpc_request_set_replen(req);
1086 rc = ptlrpc_queue_wait(req);
1090 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1092 ptlrpc_req_finished(req);
1095 EXPORT_SYMBOL(mdc_sendpage);
1098 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1099 struct page **pages, struct ptlrpc_request **request)
1101 struct ptlrpc_request *req;
1102 struct ptlrpc_bulk_desc *desc;
1104 wait_queue_head_t waitq;
1106 struct l_wait_info lwi;
1111 init_waitqueue_head(&waitq);
1114 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1118 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1120 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1122 ptlrpc_request_free(req);
1126 req->rq_request_portal = MDS_READPAGE_PORTAL;
1127 ptlrpc_at_set_req_timeout(req);
1129 desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1132 ptlrpc_request_free(req);
1136 /* NB req now owns desc and will free it when it gets freed */
1137 for (i = 0; i < op_data->op_npages; i++)
1138 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1140 mdc_readdir_pack(req, op_data->op_offset,
1141 PAGE_CACHE_SIZE * op_data->op_npages,
1142 &op_data->op_fid1, op_data->op_capa1);
1144 ptlrpc_request_set_replen(req);
1145 rc = ptlrpc_queue_wait(req);
1147 ptlrpc_req_finished(req);
1148 if (rc != -ETIMEDOUT)
1152 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1153 CERROR("too many resend retries, returning error\n");
1156 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL, NULL);
1157 l_wait_event(waitq, 0, &lwi);
1162 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1163 req->rq_bulk->bd_nob_transferred);
1165 ptlrpc_req_finished(req);
1169 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1170 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1171 req->rq_bulk->bd_nob_transferred,
1172 PAGE_CACHE_SIZE * op_data->op_npages);
1173 ptlrpc_req_finished(req);
1181 static int mdc_statfs(const struct lu_env *env,
1182 struct obd_export *exp, struct obd_statfs *osfs,
1183 __u64 max_age, __u32 flags)
1185 struct obd_device *obd = class_exp2obd(exp);
1186 struct ptlrpc_request *req;
1187 struct obd_statfs *msfs;
1188 struct obd_import *imp = NULL;
1193 * Since the request might also come from lprocfs, so we need
1194 * sync this with client_disconnect_export Bug15684
1196 down_read(&obd->u.cli.cl_sem);
1197 if (obd->u.cli.cl_import)
1198 imp = class_import_get(obd->u.cli.cl_import);
1199 up_read(&obd->u.cli.cl_sem);
1203 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1204 LUSTRE_MDS_VERSION, MDS_STATFS);
1206 GOTO(output, rc = -ENOMEM);
1208 ptlrpc_request_set_replen(req);
1210 if (flags & OBD_STATFS_NODELAY) {
1211 /* procfs requests not want stay in wait for avoid deadlock */
1212 req->rq_no_resend = 1;
1213 req->rq_no_delay = 1;
1216 rc = ptlrpc_queue_wait(req);
1218 /* check connection error first */
1219 if (imp->imp_connect_error)
1220 rc = imp->imp_connect_error;
1224 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1226 GOTO(out, rc = -EPROTO);
1231 ptlrpc_req_finished(req);
1233 class_import_put(imp);
1237 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1239 __u32 keylen, vallen;
1243 if (gf->gf_pathlen > PATH_MAX)
1244 RETURN(-ENAMETOOLONG);
1245 if (gf->gf_pathlen < 2)
1248 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1249 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1250 OBD_ALLOC(key, keylen);
1253 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1254 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1256 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1257 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1259 if (!fid_is_sane(&gf->gf_fid))
1260 GOTO(out, rc = -EINVAL);
1262 /* Val is struct getinfo_fid2path result plus path */
1263 vallen = sizeof(*gf) + gf->gf_pathlen;
1265 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1266 if (rc != 0 && rc != -EREMOTE)
1269 if (vallen <= sizeof(*gf))
1270 GOTO(out, rc = -EPROTO);
1271 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1272 GOTO(out, rc = -EOVERFLOW);
1274 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1275 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1278 OBD_FREE(key, keylen);
1282 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1283 struct hsm_progress_kernel *hpk)
1285 struct obd_import *imp = class_exp2cliimp(exp);
1286 struct hsm_progress_kernel *req_hpk;
1287 struct ptlrpc_request *req;
1291 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1292 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1294 GOTO(out, rc = -ENOMEM);
1296 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1298 /* Copy hsm_progress struct */
1299 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1300 if (req_hpk == NULL)
1301 GOTO(out, rc = -EPROTO);
1304 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1306 ptlrpc_request_set_replen(req);
1308 rc = mdc_queue_wait(req);
1311 ptlrpc_req_finished(req);
1315 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1317 __u32 *archive_mask;
1318 struct ptlrpc_request *req;
1322 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1324 MDS_HSM_CT_REGISTER);
1326 GOTO(out, rc = -ENOMEM);
1328 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1330 /* Copy hsm_progress struct */
1331 archive_mask = req_capsule_client_get(&req->rq_pill,
1332 &RMF_MDS_HSM_ARCHIVE);
1333 if (archive_mask == NULL)
1334 GOTO(out, rc = -EPROTO);
1336 *archive_mask = archives;
1338 ptlrpc_request_set_replen(req);
1340 rc = mdc_queue_wait(req);
1343 ptlrpc_req_finished(req);
1347 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1348 struct md_op_data *op_data)
1350 struct hsm_current_action *hca = op_data->op_data;
1351 struct hsm_current_action *req_hca;
1352 struct ptlrpc_request *req;
1356 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1357 &RQF_MDS_HSM_ACTION);
1361 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1363 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1365 ptlrpc_request_free(req);
1369 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1370 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1372 ptlrpc_request_set_replen(req);
1374 rc = mdc_queue_wait(req);
1378 req_hca = req_capsule_server_get(&req->rq_pill,
1379 &RMF_MDS_HSM_CURRENT_ACTION);
1380 if (req_hca == NULL)
1381 GOTO(out, rc = -EPROTO);
1387 ptlrpc_req_finished(req);
1391 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1393 struct ptlrpc_request *req;
1397 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1399 MDS_HSM_CT_UNREGISTER);
1401 GOTO(out, rc = -ENOMEM);
1403 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1405 ptlrpc_request_set_replen(req);
1407 rc = mdc_queue_wait(req);
1410 ptlrpc_req_finished(req);
1414 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1415 struct md_op_data *op_data)
1417 struct hsm_user_state *hus = op_data->op_data;
1418 struct hsm_user_state *req_hus;
1419 struct ptlrpc_request *req;
1423 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1424 &RQF_MDS_HSM_STATE_GET);
1428 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1430 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1432 ptlrpc_request_free(req);
1436 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1437 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1439 ptlrpc_request_set_replen(req);
1441 rc = mdc_queue_wait(req);
1445 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1446 if (req_hus == NULL)
1447 GOTO(out, rc = -EPROTO);
1453 ptlrpc_req_finished(req);
1457 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1458 struct md_op_data *op_data)
1460 struct hsm_state_set *hss = op_data->op_data;
1461 struct hsm_state_set *req_hss;
1462 struct ptlrpc_request *req;
1466 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1467 &RQF_MDS_HSM_STATE_SET);
1471 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1473 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1475 ptlrpc_request_free(req);
1479 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1480 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1483 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1484 if (req_hss == NULL)
1485 GOTO(out, rc = -EPROTO);
1488 ptlrpc_request_set_replen(req);
1490 rc = mdc_queue_wait(req);
1495 ptlrpc_req_finished(req);
1499 static int mdc_ioc_hsm_request(struct obd_export *exp,
1500 struct hsm_user_request *hur)
1502 struct obd_import *imp = class_exp2cliimp(exp);
1503 struct ptlrpc_request *req;
1504 struct hsm_request *req_hr;
1505 struct hsm_user_item *req_hui;
1510 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1512 GOTO(out, rc = -ENOMEM);
1514 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1515 hur->hur_request.hr_itemcount
1516 * sizeof(struct hsm_user_item));
1517 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1518 hur->hur_request.hr_data_len);
1520 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1522 ptlrpc_request_free(req);
1526 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1528 /* Copy hsm_request struct */
1529 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1531 GOTO(out, rc = -EPROTO);
1532 *req_hr = hur->hur_request;
1534 /* Copy hsm_user_item structs */
1535 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1536 if (req_hui == NULL)
1537 GOTO(out, rc = -EPROTO);
1538 memcpy(req_hui, hur->hur_user_item,
1539 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1541 /* Copy opaque field */
1542 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1543 if (req_opaque == NULL)
1544 GOTO(out, rc = -EPROTO);
1545 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1547 ptlrpc_request_set_replen(req);
1549 rc = mdc_queue_wait(req);
1553 ptlrpc_req_finished(req);
1557 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1559 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1561 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1563 lh->kuc_magic = KUC_MAGIC;
1564 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1565 lh->kuc_flags = flags;
1566 lh->kuc_msgtype = CL_RECORD;
1567 lh->kuc_msglen = len;
1571 #define D_CHANGELOG 0
1573 struct changelog_show {
1578 struct obd_device *cs_obd;
1581 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1582 struct llog_rec_hdr *hdr, void *data)
1584 struct changelog_show *cs = data;
1585 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1590 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1592 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1593 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1594 rec->cr.cr_type, rc);
1598 if (rec->cr.cr_index < cs->cs_startrec) {
1599 /* Skip entries earlier than what we are interested in */
1600 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1601 rec->cr.cr_index, cs->cs_startrec);
1605 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1606 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1607 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1608 rec->cr.cr_flags & CLF_FLAGMASK,
1609 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1610 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1612 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1614 /* Set up the message */
1615 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1616 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1618 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1619 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1624 static int mdc_changelog_send_thread(void *csdata)
1626 struct changelog_show *cs = csdata;
1627 struct llog_ctxt *ctxt = NULL;
1628 struct llog_handle *llh = NULL;
1629 struct kuc_hdr *kuch;
1632 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1633 cs->cs_fp, cs->cs_startrec);
1635 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1636 if (cs->cs_buf == NULL)
1637 GOTO(out, rc = -ENOMEM);
1639 /* Set up the remote catalog handle */
1640 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1642 GOTO(out, rc = -ENOENT);
1643 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1646 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1647 cs->cs_obd->obd_name, rc);
1650 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1652 CERROR("llog_init_handle failed %d\n", rc);
1656 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1658 /* Send EOF no matter what our result */
1659 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1661 kuch->kuc_msgtype = CL_EOF;
1662 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1668 llog_cat_close(NULL, llh);
1670 llog_ctxt_put(ctxt);
1672 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1677 static int mdc_ioc_changelog_send(struct obd_device *obd,
1678 struct ioc_changelog *icc)
1680 struct changelog_show *cs;
1683 /* Freed in mdc_changelog_send_thread */
1689 cs->cs_startrec = icc->icc_recno;
1690 /* matching fput in mdc_changelog_send_thread */
1691 cs->cs_fp = fget(icc->icc_id);
1692 cs->cs_flags = icc->icc_flags;
1695 * New thread because we should return to user app before
1696 * writing into our pipe
1698 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1699 "mdc_clg_send_thread"));
1700 if (!IS_ERR_VALUE(rc)) {
1701 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1705 CERROR("Failed to start changelog thread: %d\n", rc);
1710 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1711 struct lustre_kernelcomm *lk);
1713 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1714 struct obd_quotactl *oqctl)
1716 struct client_obd *cli = &exp->exp_obd->u.cli;
1717 struct ptlrpc_request *req;
1718 struct obd_quotactl *body;
1722 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1723 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1728 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1731 ptlrpc_request_set_replen(req);
1733 /* the next poll will find -ENODATA, that means quotacheck is
1735 cli->cl_qchk_stat = -ENODATA;
1736 rc = ptlrpc_queue_wait(req);
1738 cli->cl_qchk_stat = rc;
1739 ptlrpc_req_finished(req);
1743 static int mdc_quota_poll_check(struct obd_export *exp,
1744 struct if_quotacheck *qchk)
1746 struct client_obd *cli = &exp->exp_obd->u.cli;
1750 qchk->obd_uuid = cli->cl_target_uuid;
1751 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1753 rc = cli->cl_qchk_stat;
1754 /* the client is not the previous one */
1755 if (rc == CL_NOT_QUOTACHECKED)
1760 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1761 struct obd_quotactl *oqctl)
1763 struct ptlrpc_request *req;
1764 struct obd_quotactl *oqc;
1768 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1769 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1774 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1777 ptlrpc_request_set_replen(req);
1778 ptlrpc_at_set_req_timeout(req);
1779 req->rq_no_resend = 1;
1781 rc = ptlrpc_queue_wait(req);
1783 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1785 if (req->rq_repmsg &&
1786 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1789 CERROR ("Can't unpack obd_quotactl\n");
1792 ptlrpc_req_finished(req);
1797 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1798 struct md_op_data *op_data)
1800 CFS_LIST_HEAD(cancels);
1801 struct ptlrpc_request *req;
1803 struct mdc_swap_layouts *msl, *payload;
1806 msl = op_data->op_data;
1808 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1809 * first thing it will do is to cancel the 2 layout
1810 * locks hold by this client.
1811 * So the client must cancel its layout locks on the 2 fids
1812 * with the request RPC to avoid extra RPC round trips
1814 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1815 LCK_CR, MDS_INODELOCK_LAYOUT);
1816 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1817 LCK_CR, MDS_INODELOCK_LAYOUT);
1819 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1820 &RQF_MDS_SWAP_LAYOUTS);
1822 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1826 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1827 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1829 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1831 ptlrpc_request_free(req);
1835 mdc_swap_layouts_pack(req, op_data);
1837 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1842 ptlrpc_request_set_replen(req);
1844 rc = ptlrpc_queue_wait(req);
1850 ptlrpc_req_finished(req);
1854 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1855 void *karg, void *uarg)
1857 struct obd_device *obd = exp->exp_obd;
1858 struct obd_ioctl_data *data = karg;
1859 struct obd_import *imp = obd->u.cli.cl_import;
1863 if (!try_module_get(THIS_MODULE)) {
1864 CERROR("Can't get module. Is it alive?");
1868 case OBD_IOC_CHANGELOG_SEND:
1869 rc = mdc_ioc_changelog_send(obd, karg);
1871 case OBD_IOC_CHANGELOG_CLEAR: {
1872 struct ioc_changelog *icc = karg;
1873 struct changelog_setinfo cs =
1874 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1875 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1876 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1880 case OBD_IOC_FID2PATH:
1881 rc = mdc_ioc_fid2path(exp, karg);
1883 case LL_IOC_HSM_CT_START:
1884 rc = mdc_ioc_hsm_ct_start(exp, karg);
1885 /* ignore if it was already registered on this MDS. */
1889 case LL_IOC_HSM_PROGRESS:
1890 rc = mdc_ioc_hsm_progress(exp, karg);
1892 case LL_IOC_HSM_STATE_GET:
1893 rc = mdc_ioc_hsm_state_get(exp, karg);
1895 case LL_IOC_HSM_STATE_SET:
1896 rc = mdc_ioc_hsm_state_set(exp, karg);
1898 case LL_IOC_HSM_ACTION:
1899 rc = mdc_ioc_hsm_current_action(exp, karg);
1901 case LL_IOC_HSM_REQUEST:
1902 rc = mdc_ioc_hsm_request(exp, karg);
1904 case OBD_IOC_CLIENT_RECOVER:
1905 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1909 case IOC_OSC_SET_ACTIVE:
1910 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1912 case OBD_IOC_POLL_QUOTACHECK:
1913 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1915 case OBD_IOC_PING_TARGET:
1916 rc = ptlrpc_obd_ping(obd);
1919 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1920 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1921 * there'd be no LMV layer thus we might be called here. Eventually
1922 * this code should be removed.
1925 case IOC_OBD_STATFS: {
1926 struct obd_statfs stat_buf = {0};
1928 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1929 GOTO(out, rc = -ENODEV);
1932 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1933 min((int)data->ioc_plen2,
1934 (int)sizeof(struct obd_uuid))))
1935 GOTO(out, rc = -EFAULT);
1937 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1938 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1943 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1944 min((int) data->ioc_plen1,
1945 (int) sizeof(stat_buf))))
1946 GOTO(out, rc = -EFAULT);
1950 case OBD_IOC_QUOTACTL: {
1951 struct if_quotactl *qctl = karg;
1952 struct obd_quotactl *oqctl;
1954 OBD_ALLOC_PTR(oqctl);
1956 GOTO(out, rc = -ENOMEM);
1958 QCTL_COPY(oqctl, qctl);
1959 rc = obd_quotactl(exp, oqctl);
1961 QCTL_COPY(qctl, oqctl);
1962 qctl->qc_valid = QC_MDTIDX;
1963 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1966 OBD_FREE_PTR(oqctl);
1969 case LL_IOC_GET_CONNECT_FLAGS:
1970 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1971 sizeof(*exp_connect_flags_ptr(exp))))
1972 GOTO(out, rc = -EFAULT);
1975 case LL_IOC_LOV_SWAP_LAYOUTS:
1976 rc = mdc_ioc_swap_layouts(exp, karg);
1979 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1980 GOTO(out, rc = -ENOTTY);
1983 module_put(THIS_MODULE);
1988 int mdc_get_info_rpc(struct obd_export *exp,
1989 obd_count keylen, void *key,
1990 int vallen, void *val)
1992 struct obd_import *imp = class_exp2cliimp(exp);
1993 struct ptlrpc_request *req;
1998 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2002 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2003 RCL_CLIENT, keylen);
2004 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2005 RCL_CLIENT, sizeof(__u32));
2007 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2009 ptlrpc_request_free(req);
2013 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2014 memcpy(tmp, key, keylen);
2015 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2016 memcpy(tmp, &vallen, sizeof(__u32));
2018 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2019 RCL_SERVER, vallen);
2020 ptlrpc_request_set_replen(req);
2022 rc = ptlrpc_queue_wait(req);
2023 /* -EREMOTE means the get_info result is partial, and it needs to
2024 * continue on another MDT, see fid2path part in lmv_iocontrol */
2025 if (rc == 0 || rc == -EREMOTE) {
2026 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2027 memcpy(val, tmp, vallen);
2028 if (ptlrpc_rep_need_swab(req)) {
2029 if (KEY_IS(KEY_FID2PATH))
2030 lustre_swab_fid2path(val);
2033 ptlrpc_req_finished(req);
2038 static void lustre_swab_hai(struct hsm_action_item *h)
2040 __swab32s(&h->hai_len);
2041 __swab32s(&h->hai_action);
2042 lustre_swab_lu_fid(&h->hai_fid);
2043 lustre_swab_lu_fid(&h->hai_dfid);
2044 __swab64s(&h->hai_cookie);
2045 __swab64s(&h->hai_extent.offset);
2046 __swab64s(&h->hai_extent.length);
2047 __swab64s(&h->hai_gid);
2050 static void lustre_swab_hal(struct hsm_action_list *h)
2052 struct hsm_action_item *hai;
2055 __swab32s(&h->hal_version);
2056 __swab32s(&h->hal_count);
2057 __swab32s(&h->hal_archive_id);
2058 __swab64s(&h->hal_flags);
2060 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2061 lustre_swab_hai(hai);
2064 static void lustre_swab_kuch(struct kuc_hdr *l)
2066 __swab16s(&l->kuc_magic);
2067 /* __u8 l->kuc_transport */
2068 __swab16s(&l->kuc_msgtype);
2069 __swab16s(&l->kuc_msglen);
2072 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2073 struct lustre_kernelcomm *lk)
2075 struct obd_import *imp = class_exp2cliimp(exp);
2076 __u32 archive = lk->lk_data;
2079 if (lk->lk_group != KUC_GRP_HSM) {
2080 CERROR("Bad copytool group %d\n", lk->lk_group);
2084 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2085 lk->lk_uid, lk->lk_group, lk->lk_flags);
2087 if (lk->lk_flags & LK_FLG_STOP) {
2088 /* Unregister with the coordinator */
2089 rc = mdc_ioc_hsm_ct_unregister(imp);
2091 rc = mdc_ioc_hsm_ct_register(imp, archive);
2098 * Send a message to any listening copytools
2099 * @param val KUC message (kuc_hdr + hsm_action_list)
2100 * @param len total length of message
2102 static int mdc_hsm_copytool_send(int len, void *val)
2104 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2105 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2109 if (len < sizeof(*lh) + sizeof(*hal)) {
2110 CERROR("Short HSM message %d < %d\n", len,
2111 (int) (sizeof(*lh) + sizeof(*hal)));
2114 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2115 lustre_swab_kuch(lh);
2116 lustre_swab_hal(hal);
2117 } else if (lh->kuc_magic != KUC_MAGIC) {
2118 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2122 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2124 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2125 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2127 /* Broadcast to HSM listeners */
2128 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2134 * callback function passed to kuc for re-registering each HSM copytool
2135 * running on MDC, after MDT shutdown/recovery.
2136 * @param data copytool registration data
2137 * @param cb_arg callback argument (obd_import)
2139 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2141 struct kkuc_ct_data *kcd = data;
2142 struct obd_import *imp = (struct obd_import *)cb_arg;
2145 if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2148 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2151 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2152 imp->imp_obd->obd_name, kcd->kcd_archive);
2153 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2155 /* ignore error if the copytool is already registered */
2156 return (rc == -EEXIST) ? 0 : rc;
2160 * Re-establish all kuc contexts with MDT
2161 * after MDT shutdown/recovery.
2163 static int mdc_kuc_reregister(struct obd_import *imp)
2165 /* re-register HSM agents */
2166 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2170 int mdc_set_info_async(const struct lu_env *env,
2171 struct obd_export *exp,
2172 obd_count keylen, void *key,
2173 obd_count vallen, void *val,
2174 struct ptlrpc_request_set *set)
2176 struct obd_import *imp = class_exp2cliimp(exp);
2180 if (KEY_IS(KEY_READ_ONLY)) {
2181 if (vallen != sizeof(int))
2184 spin_lock(&imp->imp_lock);
2185 if (*((int *)val)) {
2186 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2187 imp->imp_connect_data.ocd_connect_flags |=
2190 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2191 imp->imp_connect_data.ocd_connect_flags &=
2192 ~OBD_CONNECT_RDONLY;
2194 spin_unlock(&imp->imp_lock);
2196 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2197 keylen, key, vallen, val, set);
2200 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2201 sptlrpc_conf_client_adapt(exp->exp_obd);
2204 if (KEY_IS(KEY_FLUSH_CTX)) {
2205 sptlrpc_import_flush_my_ctx(imp);
2208 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2209 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2210 keylen, key, vallen, val, set);
2213 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2214 rc = mdc_hsm_copytool_send(vallen, val);
2218 CERROR("Unknown key %s\n", (char *)key);
2222 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2223 __u32 keylen, void *key, __u32 *vallen, void *val,
2224 struct lov_stripe_md *lsm)
2228 if (KEY_IS(KEY_MAX_EASIZE)) {
2229 int mdsize, *max_easize;
2231 if (*vallen != sizeof(int))
2233 mdsize = *(int*)val;
2234 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2235 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2237 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2239 } else if (KEY_IS(KEY_CONN_DATA)) {
2240 struct obd_import *imp = class_exp2cliimp(exp);
2241 struct obd_connect_data *data = val;
2243 if (*vallen != sizeof(*data))
2246 *data = imp->imp_connect_data;
2248 } else if (KEY_IS(KEY_TGT_COUNT)) {
2253 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2258 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2259 struct obd_capa *oc, struct obd_client_handle *handle,
2262 struct ptlrpc_request *req;
2263 struct mdt_body *body;
2267 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2271 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2273 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2275 ptlrpc_request_free(req);
2279 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2281 ptlrpc_request_set_replen(req);
2283 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2284 rc = ptlrpc_queue_wait(req);
2285 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2287 CERROR("Pin failed: %d\n", rc);
2291 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2293 GOTO(err_out, rc = -EPROTO);
2295 handle->och_fh = body->handle;
2296 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2298 handle->och_mod = obd_mod_alloc();
2299 if (handle->och_mod == NULL) {
2300 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2301 GOTO(err_out, rc = -ENOMEM);
2303 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2308 ptlrpc_req_finished(req);
2312 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2315 struct ptlrpc_request *req;
2316 struct mdt_body *body;
2320 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2321 LUSTRE_MDS_VERSION, MDS_UNPIN);
2325 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2326 body->handle = handle->och_fh;
2329 ptlrpc_request_set_replen(req);
2331 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2332 rc = ptlrpc_queue_wait(req);
2333 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2336 CERROR("Unpin failed: %d\n", rc);
2338 ptlrpc_req_finished(req);
2339 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2341 obd_mod_put(handle->och_mod);
2345 int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2346 struct obd_capa *oc, struct ptlrpc_request **request)
2348 struct ptlrpc_request *req;
2353 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2357 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2359 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2361 ptlrpc_request_free(req);
2365 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2367 ptlrpc_request_set_replen(req);
2369 rc = ptlrpc_queue_wait(req);
2371 ptlrpc_req_finished(req);
2377 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2378 enum obd_import_event event)
2382 LASSERT(imp->imp_obd == obd);
2385 case IMP_EVENT_DISCON: {
2387 /* XXX Pass event up to OBDs stack. used only for FLD now */
2388 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2392 case IMP_EVENT_INACTIVE: {
2393 struct client_obd *cli = &obd->u.cli;
2395 * Flush current sequence to make client obtain new one
2396 * from server in case of disconnect/reconnect.
2398 if (cli->cl_seq != NULL)
2399 seq_client_flush(cli->cl_seq);
2401 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2404 case IMP_EVENT_INVALIDATE: {
2405 struct ldlm_namespace *ns = obd->obd_namespace;
2407 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2411 case IMP_EVENT_ACTIVE:
2412 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2413 /* redo the kuc registration after reconnecting */
2415 rc = mdc_kuc_reregister(imp);
2418 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2420 case IMP_EVENT_DEACTIVATE:
2421 case IMP_EVENT_ACTIVATE:
2424 CERROR("Unknown import event %x\n", event);
2430 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2431 struct md_op_data *op_data)
2433 struct client_obd *cli = &exp->exp_obd->u.cli;
2434 struct lu_client_seq *seq = cli->cl_seq;
2436 RETURN(seq_client_alloc_fid(NULL, seq, fid));
2439 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2440 struct client_obd *cli = &exp->exp_obd->u.cli;
2441 return &cli->cl_target_uuid;
2445 * Determine whether the lock can be canceled before replaying it during
2446 * recovery, non zero value will be return if the lock can be canceled,
2447 * or zero returned for not
2449 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2451 if (lock->l_resource->lr_type != LDLM_IBITS)
2454 /* FIXME: if we ever get into a situation where there are too many
2455 * opened files with open locks on a single node, then we really
2456 * should replay these open locks to reget it */
2457 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2463 static int mdc_resource_inode_free(struct ldlm_resource *res)
2465 if (res->lr_lvb_inode)
2466 res->lr_lvb_inode = NULL;
2471 struct ldlm_valblock_ops inode_lvbo = {
2472 lvbo_free: mdc_resource_inode_free
2475 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2477 struct client_obd *cli = &obd->u.cli;
2478 struct lprocfs_static_vars lvars = { 0 };
2482 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2483 if (!cli->cl_rpc_lock)
2485 mdc_init_rpc_lock(cli->cl_rpc_lock);
2487 rc = ptlrpcd_addref();
2489 GOTO(err_rpc_lock, rc);
2491 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2492 if (!cli->cl_close_lock)
2493 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2494 mdc_init_rpc_lock(cli->cl_close_lock);
2496 rc = client_obd_setup(obd, cfg);
2498 GOTO(err_close_lock, rc);
2499 lprocfs_mdc_init_vars(&lvars);
2500 lprocfs_obd_setup(obd, lvars.obd_vars);
2501 lprocfs_alloc_md_stats(obd, 0);
2502 sptlrpc_lprocfs_cliobd_attach(obd);
2503 ptlrpc_lprocfs_register_obd(obd);
2505 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2507 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2509 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2512 CERROR("failed to setup llogging subsystems\n");
2518 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2522 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2526 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
2527 * us to make MDS RPCs with large enough reply buffers to hold the
2528 * maximum-sized (= maximum striped) EA and cookie without having to
2529 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2530 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2531 int def_easize, int cookiesize)
2533 struct obd_device *obd = exp->exp_obd;
2534 struct client_obd *cli = &obd->u.cli;
2537 if (cli->cl_max_mds_easize < easize)
2538 cli->cl_max_mds_easize = easize;
2540 if (cli->cl_default_mds_easize < def_easize)
2541 cli->cl_default_mds_easize = def_easize;
2543 if (cli->cl_max_mds_cookiesize < cookiesize)
2544 cli->cl_max_mds_cookiesize = cookiesize;
2549 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2555 case OBD_CLEANUP_EARLY:
2557 case OBD_CLEANUP_EXPORTS:
2558 /* Failsafe, ok if racy */
2559 if (obd->obd_type->typ_refcnt <= 1)
2560 libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
2562 obd_cleanup_client_import(obd);
2563 ptlrpc_lprocfs_unregister_obd(obd);
2564 lprocfs_obd_cleanup(obd);
2565 lprocfs_free_md_stats(obd);
2567 rc = obd_llog_finish(obd, 0);
2569 CERROR("failed to cleanup llogging subsystems\n");
2575 static int mdc_cleanup(struct obd_device *obd)
2577 struct client_obd *cli = &obd->u.cli;
2579 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2580 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2584 return client_obd_cleanup(obd);
2588 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2589 struct obd_device *tgt, int *index)
2591 struct llog_ctxt *ctxt;
2596 LASSERT(olg == &obd->obd_olg);
2598 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2603 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2604 llog_initiator_connect(ctxt);
2605 llog_ctxt_put(ctxt);
2610 static int mdc_llog_finish(struct obd_device *obd, int count)
2612 struct llog_ctxt *ctxt;
2616 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2618 llog_cleanup(NULL, ctxt);
2623 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2625 struct lustre_cfg *lcfg = buf;
2626 struct lprocfs_static_vars lvars = { 0 };
2629 lprocfs_mdc_init_vars(&lvars);
2630 switch (lcfg->lcfg_command) {
2632 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2642 /* get remote permission for current user on fid */
2643 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2644 struct obd_capa *oc, __u32 suppgid,
2645 struct ptlrpc_request **request)
2647 struct ptlrpc_request *req;
2651 LASSERT(client_is_remote(exp));
2654 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2658 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2660 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2662 ptlrpc_request_free(req);
2666 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2668 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2669 sizeof(struct mdt_remote_perm));
2671 ptlrpc_request_set_replen(req);
2673 rc = ptlrpc_queue_wait(req);
2675 ptlrpc_req_finished(req);
2681 static int mdc_interpret_renew_capa(const struct lu_env *env,
2682 struct ptlrpc_request *req, void *args,
2685 struct mdc_renew_capa_args *ra = args;
2686 struct mdt_body *body = NULL;
2687 struct lustre_capa *capa;
2691 GOTO(out, capa = ERR_PTR(status));
2693 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2695 GOTO(out, capa = ERR_PTR(-EFAULT));
2697 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2698 GOTO(out, capa = ERR_PTR(-ENOENT));
2700 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2702 GOTO(out, capa = ERR_PTR(-EFAULT));
2705 ra->ra_cb(ra->ra_oc, capa);
2709 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2712 struct ptlrpc_request *req;
2713 struct mdc_renew_capa_args *ra;
2716 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2717 LUSTRE_MDS_VERSION, MDS_GETATTR);
2721 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2722 * capa to renew is oss capa.
2724 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2725 ptlrpc_request_set_replen(req);
2727 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2728 ra = ptlrpc_req_async_args(req);
2731 req->rq_interpret_reply = mdc_interpret_renew_capa;
2732 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2736 struct obd_ops mdc_obd_ops = {
2737 .o_owner = THIS_MODULE,
2738 .o_setup = mdc_setup,
2739 .o_precleanup = mdc_precleanup,
2740 .o_cleanup = mdc_cleanup,
2741 .o_add_conn = client_import_add_conn,
2742 .o_del_conn = client_import_del_conn,
2743 .o_connect = client_connect_import,
2744 .o_disconnect = client_disconnect_export,
2745 .o_iocontrol = mdc_iocontrol,
2746 .o_set_info_async = mdc_set_info_async,
2747 .o_statfs = mdc_statfs,
2749 .o_unpin = mdc_unpin,
2750 .o_fid_init = client_fid_init,
2751 .o_fid_fini = client_fid_fini,
2752 .o_fid_alloc = mdc_fid_alloc,
2753 .o_import_event = mdc_import_event,
2754 .o_llog_init = mdc_llog_init,
2755 .o_llog_finish = mdc_llog_finish,
2756 .o_get_info = mdc_get_info,
2757 .o_process_config = mdc_process_config,
2758 .o_get_uuid = mdc_get_uuid,
2759 .o_quotactl = mdc_quotactl,
2760 .o_quotacheck = mdc_quotacheck
2763 struct md_ops mdc_md_ops = {
2764 .m_getstatus = mdc_getstatus,
2765 .m_null_inode = mdc_null_inode,
2766 .m_find_cbdata = mdc_find_cbdata,
2767 .m_close = mdc_close,
2768 .m_create = mdc_create,
2769 .m_done_writing = mdc_done_writing,
2770 .m_enqueue = mdc_enqueue,
2771 .m_getattr = mdc_getattr,
2772 .m_getattr_name = mdc_getattr_name,
2773 .m_intent_lock = mdc_intent_lock,
2775 .m_is_subdir = mdc_is_subdir,
2776 .m_rename = mdc_rename,
2777 .m_setattr = mdc_setattr,
2778 .m_setxattr = mdc_setxattr,
2779 .m_getxattr = mdc_getxattr,
2780 .m_fsync = mdc_fsync,
2781 .m_readpage = mdc_readpage,
2782 .m_unlink = mdc_unlink,
2783 .m_cancel_unused = mdc_cancel_unused,
2784 .m_init_ea_size = mdc_init_ea_size,
2785 .m_set_lock_data = mdc_set_lock_data,
2786 .m_lock_match = mdc_lock_match,
2787 .m_get_lustre_md = mdc_get_lustre_md,
2788 .m_free_lustre_md = mdc_free_lustre_md,
2789 .m_set_open_replay_data = mdc_set_open_replay_data,
2790 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2791 .m_renew_capa = mdc_renew_capa,
2792 .m_unpack_capa = mdc_unpack_capa,
2793 .m_get_remote_perm = mdc_get_remote_perm,
2794 .m_intent_getattr_async = mdc_intent_getattr_async,
2795 .m_revalidate_lock = mdc_revalidate_lock
2798 int __init mdc_init(void)
2801 struct lprocfs_static_vars lvars = { 0 };
2802 lprocfs_mdc_init_vars(&lvars);
2804 rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2805 LUSTRE_MDC_NAME, NULL);
2810 static void /*__exit*/ mdc_exit(void)
2812 class_unregister_type(LUSTRE_MDC_NAME);
2815 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2816 MODULE_DESCRIPTION("Lustre Metadata Client");
2817 MODULE_LICENSE("GPL");
2819 module_init(mdc_init);
2820 module_exit(mdc_exit);