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_lmv.h>
52 #include <lustre_fid.h>
53 #include <lprocfs_status.h>
54 #include <lustre_param.h>
55 #include <lustre_log.h>
56 #include <cl_object.h>
59 #include "mdc_internal.h"
61 #define REQUEST_MINOR 244
63 struct mdc_renew_capa_args {
64 struct obd_capa *ra_oc;
65 renew_capa_cb_t ra_cb;
68 static int mdc_cleanup(struct obd_device *obd);
70 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
71 const struct req_msg_field *field, struct obd_capa **oc)
73 struct lustre_capa *capa;
77 /* swabbed already in mdc_enqueue */
78 capa = req_capsule_server_get(&req->rq_pill, field);
82 c = alloc_capa(CAPA_SITE_CLIENT);
84 CDEBUG(D_INFO, "alloc capa failed!\n");
93 static inline int mdc_queue_wait(struct ptlrpc_request *req)
95 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
98 /* mdc_enter_request() ensures that this client has no more
99 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
101 rc = mdc_enter_request(cli);
105 rc = ptlrpc_queue_wait(req);
106 mdc_exit_request(cli);
111 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
112 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
113 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
114 struct obd_capa **pc, int level, int msg_flags)
116 struct ptlrpc_request *req;
117 struct mdt_body *body;
121 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
122 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
126 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
127 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
128 req->rq_send_state = level;
130 ptlrpc_request_set_replen(req);
132 rc = ptlrpc_queue_wait(req);
136 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
138 GOTO(out, rc = -EPROTO);
140 if (body->valid & OBD_MD_FLMDSCAPA) {
141 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
146 *rootfid = body->fid1;
148 "root fid="DFID", last_committed="LPU64"\n",
150 lustre_msg_get_last_committed(req->rq_repmsg));
153 ptlrpc_req_finished(req);
157 /* This should be mdc_get_info("rootfid") */
158 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
159 struct obd_capa **pc)
161 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
166 * This function now is known to always saying that it will receive 4 buffers
167 * from server. Even for cases when acl_size and md_size is zero, RPC header
168 * will contain 4 fields and RPC itself will contain zero size fields. This is
169 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
170 * and thus zero, it shrinks it, making zero size. The same story about
171 * md_size. And this is course of problem when client waits for smaller number
172 * of fields. This issue will be fixed later when client gets aware of RPC
175 static int mdc_getattr_common(struct obd_export *exp,
176 struct ptlrpc_request *req)
178 struct req_capsule *pill = &req->rq_pill;
179 struct mdt_body *body;
184 /* Request message already built. */
185 rc = ptlrpc_queue_wait(req);
189 /* sanity check for the reply */
190 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
194 CDEBUG(D_NET, "mode: %o\n", body->mode);
196 mdc_update_max_ea_from_body(exp, body);
197 if (body->eadatasize != 0) {
198 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
204 if (body->valid & OBD_MD_FLRMTPERM) {
205 struct mdt_remote_perm *perm;
207 LASSERT(client_is_remote(exp));
208 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
209 lustre_swab_mdt_remote_perm);
214 if (body->valid & OBD_MD_FLMDSCAPA) {
215 struct lustre_capa *capa;
216 capa = req_capsule_server_get(pill, &RMF_CAPA1);
224 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
225 struct ptlrpc_request **request)
227 struct ptlrpc_request *req;
231 /* Single MDS without an LMV case */
232 if (op_data->op_flags & MF_GET_MDT_IDX) {
237 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
241 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
243 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
245 ptlrpc_request_free(req);
249 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
250 op_data->op_valid, op_data->op_mode, -1, 0);
252 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
254 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
255 LASSERT(client_is_remote(exp));
256 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
257 sizeof(struct mdt_remote_perm));
259 ptlrpc_request_set_replen(req);
261 rc = mdc_getattr_common(exp, req);
263 ptlrpc_req_finished(req);
269 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
270 struct ptlrpc_request **request)
272 struct ptlrpc_request *req;
277 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
278 &RQF_MDS_GETATTR_NAME);
282 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
283 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
284 op_data->op_namelen + 1);
286 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
288 ptlrpc_request_free(req);
292 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
293 op_data->op_valid, op_data->op_mode,
294 op_data->op_suppgids[0], 0);
296 if (op_data->op_name) {
297 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
298 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
299 op_data->op_namelen);
300 memcpy(name, op_data->op_name, op_data->op_namelen);
303 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
305 ptlrpc_request_set_replen(req);
307 rc = mdc_getattr_common(exp, req);
309 ptlrpc_req_finished(req);
315 static int mdc_is_subdir(struct obd_export *exp,
316 const struct lu_fid *pfid,
317 const struct lu_fid *cfid,
318 struct ptlrpc_request **request)
320 struct ptlrpc_request *req;
326 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
327 &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
332 mdc_is_subdir_pack(req, pfid, cfid, 0);
333 ptlrpc_request_set_replen(req);
335 rc = ptlrpc_queue_wait(req);
336 if (rc && rc != -EREMOTE)
337 ptlrpc_req_finished(req);
343 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
344 const struct lu_fid *fid,
345 struct obd_capa *oc, int opcode, obd_valid valid,
346 const char *xattr_name, const char *input,
347 int input_size, int output_size, int flags,
348 __u32 suppgid, struct ptlrpc_request **request)
350 struct ptlrpc_request *req;
351 int xattr_namelen = 0;
357 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
361 mdc_set_capa_size(req, &RMF_CAPA1, oc);
363 xattr_namelen = strlen(xattr_name) + 1;
364 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
369 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
373 /* Flush local XATTR locks to get rid of a possible cancel RPC */
374 if (opcode == MDS_REINT && fid_is_sane(fid) &&
375 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
376 CFS_LIST_HEAD(cancels);
379 /* Without that packing would fail */
381 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
384 count = mdc_resource_get_unused(exp, fid,
386 MDS_INODELOCK_XATTR);
388 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
390 ptlrpc_request_free(req);
394 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
396 ptlrpc_request_free(req);
401 if (opcode == MDS_REINT) {
402 struct mdt_rec_setxattr *rec;
404 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
405 sizeof(struct mdt_rec_reint));
406 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
407 rec->sx_opcode = REINT_SETXATTR;
408 rec->sx_fsuid = current_fsuid();
409 rec->sx_fsgid = current_fsgid();
410 rec->sx_cap = cfs_curproc_cap_pack();
411 rec->sx_suppgid1 = suppgid;
412 rec->sx_suppgid2 = -1;
414 rec->sx_valid = valid | OBD_MD_FLCTIME;
415 rec->sx_time = cfs_time_current_sec();
416 rec->sx_size = output_size;
417 rec->sx_flags = flags;
419 mdc_pack_capa(req, &RMF_CAPA1, oc);
421 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
425 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
426 memcpy(tmp, xattr_name, xattr_namelen);
429 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
430 memcpy(tmp, input, input_size);
433 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
434 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
435 RCL_SERVER, output_size);
436 ptlrpc_request_set_replen(req);
439 if (opcode == MDS_REINT)
440 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
442 rc = ptlrpc_queue_wait(req);
444 if (opcode == MDS_REINT)
445 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
448 ptlrpc_req_finished(req);
454 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
455 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
456 const char *input, int input_size, int output_size,
457 int flags, __u32 suppgid, struct ptlrpc_request **request)
459 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
460 fid, oc, MDS_REINT, valid, xattr_name,
461 input, input_size, output_size, flags,
465 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
466 struct obd_capa *oc, obd_valid valid, const char *xattr_name,
467 const char *input, int input_size, int output_size,
468 int flags, struct ptlrpc_request **request)
470 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
471 fid, oc, MDS_GETXATTR, valid, xattr_name,
472 input, input_size, output_size, flags,
476 #ifdef CONFIG_FS_POSIX_ACL
477 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
479 struct req_capsule *pill = &req->rq_pill;
480 struct mdt_body *body = md->body;
481 struct posix_acl *acl;
489 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
494 acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
497 CERROR("convert xattr to acl: %d\n", rc);
501 rc = posix_acl_valid(acl);
503 CERROR("validate acl: %d\n", rc);
504 posix_acl_release(acl);
512 #define mdc_unpack_acl(req, md) 0
515 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
516 struct obd_export *dt_exp, struct obd_export *md_exp,
517 struct lustre_md *md)
519 struct req_capsule *pill = &req->rq_pill;
524 memset(md, 0, sizeof(*md));
526 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
527 LASSERT(md->body != NULL);
529 if (md->body->valid & OBD_MD_FLEASIZE) {
531 struct lov_mds_md *lmm;
533 if (!S_ISREG(md->body->mode)) {
534 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
535 "regular file, but is not\n");
536 GOTO(out, rc = -EPROTO);
539 if (md->body->eadatasize == 0) {
540 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
541 "but eadatasize 0\n");
542 GOTO(out, rc = -EPROTO);
544 lmmsize = md->body->eadatasize;
545 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
547 GOTO(out, rc = -EPROTO);
549 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
553 if (rc < sizeof(*md->lsm)) {
554 CDEBUG(D_INFO, "lsm size too small: "
555 "rc < sizeof (*md->lsm) (%d < %d)\n",
556 rc, (int)sizeof(*md->lsm));
557 GOTO(out, rc = -EPROTO);
560 } else if (md->body->valid & OBD_MD_FLDIREA) {
562 struct lov_mds_md *lmv;
564 if(!S_ISDIR(md->body->mode)) {
565 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
566 "directory, but is not\n");
567 GOTO(out, rc = -EPROTO);
570 if (md->body->eadatasize == 0) {
571 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
572 "but eadatasize 0\n");
575 if (md->body->valid & OBD_MD_MEA) {
576 lmvsize = md->body->eadatasize;
577 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
580 GOTO(out, rc = -EPROTO);
582 rc = obd_unpackmd(md_exp, (void *)&md->lmv, lmv,
587 if (rc < sizeof(*md->lmv)) {
588 CDEBUG(D_INFO, "size too small: "
589 "rc < sizeof(*md->lmv) (%d < %d)\n",
590 rc, (int)sizeof(*md->lmv));
591 GOTO(out, rc = -EPROTO);
597 if (md->body->valid & OBD_MD_FLRMTPERM) {
598 /* remote permission */
599 LASSERT(client_is_remote(exp));
600 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
601 lustre_swab_mdt_remote_perm);
602 if (!md->remote_perm)
603 GOTO(out, rc = -EPROTO);
605 else if (md->body->valid & OBD_MD_FLACL) {
606 /* for ACL, it's possible that FLACL is set but aclsize is zero.
607 * only when aclsize != 0 there's an actual segment for ACL
610 if (md->body->aclsize) {
611 rc = mdc_unpack_acl(req, md);
614 #ifdef CONFIG_FS_POSIX_ACL
616 md->posix_acl = NULL;
620 if (md->body->valid & OBD_MD_FLMDSCAPA) {
621 struct obd_capa *oc = NULL;
623 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
629 if (md->body->valid & OBD_MD_FLOSSCAPA) {
630 struct obd_capa *oc = NULL;
632 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
642 capa_put(md->oss_capa);
646 capa_put(md->mds_capa);
649 #ifdef CONFIG_FS_POSIX_ACL
650 posix_acl_release(md->posix_acl);
653 obd_free_memmd(dt_exp, &md->lsm);
658 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
665 * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
668 void mdc_replay_open(struct ptlrpc_request *req)
670 struct md_open_data *mod = req->rq_cb_data;
671 struct ptlrpc_request *close_req;
672 struct obd_client_handle *och;
673 struct lustre_handle old;
674 struct mdt_body *body;
678 DEBUG_REQ(D_ERROR, req,
679 "Can't properly replay without open data.");
684 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
685 LASSERT(body != NULL);
689 struct lustre_handle *file_fh;
691 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
693 file_fh = &och->och_fh;
694 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
695 file_fh->cookie, body->handle.cookie);
697 *file_fh = body->handle;
699 close_req = mod->mod_close_req;
700 if (close_req != NULL) {
701 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
702 struct mdt_ioepoch *epoch;
704 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
705 epoch = req_capsule_client_get(&close_req->rq_pill,
710 LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
711 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
712 epoch->handle = body->handle;
717 void mdc_commit_open(struct ptlrpc_request *req)
719 struct md_open_data *mod = req->rq_cb_data;
724 * No need to touch md_open_data::mod_och, it holds a reference on
725 * \var mod and will zero references to each other, \var mod will be
726 * freed after that when md_open_data::mod_och will put the reference.
730 * Do not let open request to disappear as it still may be needed
731 * for close rpc to happen (it may happen on evict only, otherwise
732 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
733 * called), just mark this rpc as committed to distinguish these 2
734 * cases, see mdc_close() for details. The open request reference will
735 * be put along with freeing \var mod.
737 ptlrpc_request_addref(req);
738 spin_lock(&req->rq_lock);
739 req->rq_committed = 1;
740 spin_unlock(&req->rq_lock);
741 req->rq_cb_data = NULL;
745 int mdc_set_open_replay_data(struct obd_export *exp,
746 struct obd_client_handle *och,
747 struct lookup_intent *it)
749 struct md_open_data *mod;
750 struct mdt_rec_create *rec;
751 struct mdt_body *body;
752 struct ptlrpc_request *open_req = it->d.lustre.it_data;
753 struct obd_import *imp = open_req->rq_import;
756 if (!open_req->rq_replay)
759 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
760 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
761 LASSERT(rec != NULL);
762 /* Incoming message in my byte order (it's been swabbed). */
763 /* Outgoing messages always in my byte order. */
764 LASSERT(body != NULL);
766 /* Only if the import is replayable, we set replay_open data */
767 if (och && imp->imp_replayable) {
768 mod = obd_mod_alloc();
770 DEBUG_REQ(D_ERROR, open_req,
771 "Can't allocate md_open_data");
776 * Take a reference on \var mod, to be freed on mdc_close().
777 * It protects \var mod from being freed on eviction (commit
778 * callback is called despite rq_replay flag).
779 * Another reference for \var och.
784 spin_lock(&open_req->rq_lock);
787 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
788 it_disposition(it, DISP_OPEN_STRIPE);
789 mod->mod_open_req = open_req;
790 open_req->rq_cb_data = mod;
791 open_req->rq_commit_cb = mdc_commit_open;
792 spin_unlock(&open_req->rq_lock);
795 rec->cr_fid2 = body->fid1;
796 rec->cr_ioepoch = body->ioepoch;
797 rec->cr_old_handle.cookie = body->handle.cookie;
798 open_req->rq_replay_cb = mdc_replay_open;
799 if (!fid_is_sane(&body->fid1)) {
800 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
805 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
809 static void mdc_free_open(struct md_open_data *mod)
813 if (mod->mod_is_create == 0 &&
814 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
817 LASSERT(mod->mod_open_req->rq_replay == 0);
819 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
821 ptlrpc_request_committed(mod->mod_open_req, committed);
822 if (mod->mod_close_req)
823 ptlrpc_request_committed(mod->mod_close_req, committed);
826 int mdc_clear_open_replay_data(struct obd_export *exp,
827 struct obd_client_handle *och)
829 struct md_open_data *mod = och->och_mod;
833 * It is possible to not have \var mod in a case of eviction between
834 * lookup and ll_file_open().
839 LASSERT(mod != LP_POISON);
840 LASSERT(mod->mod_open_req != NULL);
850 /* Prepares the request for the replay by the given reply */
851 static void mdc_close_handle_reply(struct ptlrpc_request *req,
852 struct md_op_data *op_data, int rc) {
853 struct mdt_body *repbody;
854 struct mdt_ioepoch *epoch;
856 if (req && rc == -EAGAIN) {
857 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
858 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
860 epoch->flags |= MF_SOM_AU;
861 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
862 op_data->op_flags |= MF_GETATTR_LOCK;
866 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
867 struct md_open_data *mod, struct ptlrpc_request **request)
869 struct obd_device *obd = class_exp2obd(exp);
870 struct ptlrpc_request *req;
871 struct req_format *req_fmt;
876 req_fmt = &RQF_MDS_CLOSE;
877 if (op_data->op_bias & MDS_HSM_RELEASE) {
878 req_fmt = &RQF_MDS_RELEASE_CLOSE;
880 /* allocate a FID for volatile file */
881 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
883 CERROR("%s: "DFID" failed to allocate FID: %d\n",
884 obd->obd_name, PFID(&op_data->op_fid1), rc);
885 /* save the errcode and proceed to close */
891 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
895 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
897 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
899 ptlrpc_request_free(req);
903 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
904 * portal whose threads are not taking any DLM locks and are therefore
905 * always progressing */
906 req->rq_request_portal = MDS_READPAGE_PORTAL;
907 ptlrpc_at_set_req_timeout(req);
909 /* Ensure that this close's handle is fixed up during replay. */
910 if (likely(mod != NULL)) {
911 LASSERTF(mod->mod_open_req != NULL &&
912 mod->mod_open_req->rq_type != LI_POISON,
913 "POISONED open %p!\n", mod->mod_open_req);
915 mod->mod_close_req = req;
917 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
918 /* We no longer want to preserve this open for replay even
919 * though the open was committed. b=3632, b=3633 */
920 spin_lock(&mod->mod_open_req->rq_lock);
921 mod->mod_open_req->rq_replay = 0;
922 spin_unlock(&mod->mod_open_req->rq_lock);
924 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
927 mdc_close_pack(req, op_data);
929 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
930 obd->u.cli.cl_max_mds_easize);
931 req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
932 obd->u.cli.cl_max_mds_cookiesize);
934 ptlrpc_request_set_replen(req);
936 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
937 rc = ptlrpc_queue_wait(req);
938 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
940 if (req->rq_repmsg == NULL) {
941 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
944 rc = req->rq_status ?: -EIO;
945 } else if (rc == 0 || rc == -EAGAIN) {
946 struct mdt_body *body;
948 rc = lustre_msg_get_status(req->rq_repmsg);
949 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
950 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
955 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
958 } else if (rc == -ESTALE) {
960 * it can be allowed error after 3633 if open was committed and
961 * server failed before close was sent. Let's check if mod
962 * exists and return no error in that case
965 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
966 LASSERT(mod->mod_open_req != NULL);
967 if (mod->mod_open_req->rq_committed)
974 mod->mod_close_req = NULL;
975 /* Since now, mod is accessed through open_req only,
976 * thus close req does not keep a reference on mod anymore. */
980 mdc_close_handle_reply(req, op_data, rc);
981 RETURN(rc < 0 ? rc : saved_rc);
984 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
985 struct md_open_data *mod)
987 struct obd_device *obd = class_exp2obd(exp);
988 struct ptlrpc_request *req;
992 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
993 &RQF_MDS_DONE_WRITING);
997 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
998 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
1000 ptlrpc_request_free(req);
1005 LASSERTF(mod->mod_open_req != NULL &&
1006 mod->mod_open_req->rq_type != LI_POISON,
1007 "POISONED setattr %p!\n", mod->mod_open_req);
1009 mod->mod_close_req = req;
1010 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
1011 /* We no longer want to preserve this setattr for replay even
1012 * though the open was committed. b=3632, b=3633 */
1013 spin_lock(&mod->mod_open_req->rq_lock);
1014 mod->mod_open_req->rq_replay = 0;
1015 spin_unlock(&mod->mod_open_req->rq_lock);
1018 mdc_close_pack(req, op_data);
1019 ptlrpc_request_set_replen(req);
1021 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1022 rc = ptlrpc_queue_wait(req);
1023 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1025 if (rc == -ESTALE) {
1027 * it can be allowed error after 3633 if open or setattr were
1028 * committed and server failed before close was sent.
1029 * Let's check if mod exists and return no error in that case
1032 LASSERT(mod->mod_open_req != NULL);
1033 if (mod->mod_open_req->rq_committed)
1040 mod->mod_close_req = NULL;
1041 LASSERT(mod->mod_open_req != NULL);
1044 /* Since now, mod is accessed through setattr req only,
1045 * thus DW req does not keep a reference on mod anymore. */
1049 mdc_close_handle_reply(req, op_data, rc);
1050 ptlrpc_req_finished(req);
1054 #ifdef HAVE_SPLIT_SUPPORT
1055 int mdc_sendpage(struct obd_export *exp, const struct lu_fid *fid,
1056 const struct page *page, int offset)
1058 struct ptlrpc_request *req;
1059 struct ptlrpc_bulk_desc *desc;
1063 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_WRITEPAGE);
1067 /* FIXME: capa doesn't support split yet */
1068 mdc_set_capa_size(req, &RMF_CAPA1, NULL);
1070 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_WRITEPAGE);
1072 ptlrpc_request_free(req);
1076 req->rq_request_portal = MDS_READPAGE_PORTAL;
1077 ptlrpc_at_set_req_timeout(req);
1079 desc = ptlrpc_prep_bulk_imp(req, 1, 1,BULK_GET_SOURCE, MDS_BULK_PORTAL);
1081 GOTO(out, rc = -ENOMEM);
1083 /* NB req now owns desc and will free it when it gets freed. */
1084 ptlrpc_prep_bulk_page(desc, (struct page *)page, 0, offset);
1085 mdc_readdir_pack(req, 0, offset, fid, NULL);
1087 ptlrpc_request_set_replen(req);
1088 rc = ptlrpc_queue_wait(req);
1092 rc = sptlrpc_cli_unwrap_bulk_write(req, req->rq_bulk);
1094 ptlrpc_req_finished(req);
1097 EXPORT_SYMBOL(mdc_sendpage);
1100 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
1101 __u64 offset, struct obd_capa *oc,
1102 struct page **pages, int npages,
1103 struct ptlrpc_request **request)
1105 struct ptlrpc_request *req;
1106 struct ptlrpc_bulk_desc *desc;
1108 wait_queue_head_t waitq;
1110 struct l_wait_info lwi;
1115 init_waitqueue_head(&waitq);
1118 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1122 mdc_set_capa_size(req, &RMF_CAPA1, oc);
1124 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1126 ptlrpc_request_free(req);
1130 req->rq_request_portal = MDS_READPAGE_PORTAL;
1131 ptlrpc_at_set_req_timeout(req);
1133 desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
1136 ptlrpc_request_free(req);
1140 /* NB req now owns desc and will free it when it gets freed */
1141 for (i = 0; i < npages; i++)
1142 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1144 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE * npages, fid, oc);
1146 ptlrpc_request_set_replen(req);
1147 rc = ptlrpc_queue_wait(req);
1149 ptlrpc_req_finished(req);
1150 if (rc != -ETIMEDOUT)
1154 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1155 CERROR("%s: too many resend retries: rc = %d\n",
1156 exp->exp_obd->obd_name, -EIO);
1159 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL,
1161 l_wait_event(waitq, 0, &lwi);
1166 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1167 req->rq_bulk->bd_nob_transferred);
1169 ptlrpc_req_finished(req);
1173 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1174 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1175 exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1176 PAGE_CACHE_SIZE * npages);
1177 ptlrpc_req_finished(req);
1186 static void mdc_release_page(struct page *page, int remove)
1190 if (likely(page->mapping != NULL))
1191 truncate_complete_page(page->mapping, page);
1194 page_cache_release(page);
1197 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1198 __u64 *start, __u64 *end, int hash64)
1201 * Complement of hash is used as an index so that
1202 * radix_tree_gang_lookup() can be used to find a page with starting
1203 * hash _smaller_ than one we are looking for.
1205 unsigned long offset = hash_x_index(*hash, hash64);
1209 spin_lock_irq(&mapping->tree_lock);
1210 found = radix_tree_gang_lookup(&mapping->page_tree,
1211 (void **)&page, offset, 1);
1213 struct lu_dirpage *dp;
1215 page_cache_get(page);
1216 spin_unlock_irq(&mapping->tree_lock);
1218 * In contrast to find_lock_page() we are sure that directory
1219 * page cannot be truncated (while DLM lock is held) and,
1220 * hence, can avoid restart.
1222 * In fact, page cannot be locked here at all, because
1223 * mdc_read_page_remote does synchronous io.
1225 wait_on_page_locked(page);
1226 if (PageUptodate(page)) {
1228 if (BITS_PER_LONG == 32 && hash64) {
1229 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1230 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1231 *hash = *hash >> 32;
1233 *start = le64_to_cpu(dp->ldp_hash_start);
1234 *end = le64_to_cpu(dp->ldp_hash_end);
1236 LASSERTF(*start <= *hash, "start = "LPX64",end = "
1237 LPX64",hash = "LPX64"\n", *start, *end, *hash);
1238 CDEBUG(D_VFSTRACE, "page%lu [%llu %llu], hash"LPU64"\n",
1239 offset, *start, *end, *hash);
1242 mdc_release_page(page, 0);
1244 } else if (*end != *start && *hash == *end) {
1246 * upon hash collision, remove this page,
1247 * otherwise put page reference, and
1248 * ll_get_dir_page() will issue RPC to fetch
1252 mdc_release_page(page,
1253 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1257 page_cache_release(page);
1258 page = ERR_PTR(-EIO);
1261 spin_unlock_irq(&mapping->tree_lock);
1268 * Adjust a set of pages, each page containing an array of lu_dirpages,
1269 * so that each page can be used as a single logical lu_dirpage.
1271 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1272 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1273 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
1274 * value is used as a cookie to request the next lu_dirpage in a
1275 * directory listing that spans multiple pages (two in this example):
1278 * .|--------v------- -----.
1279 * |s|e|f|p|ent|ent| ... |ent|
1280 * '--|-------------- -----' Each CFS_PAGE contains a single
1281 * '------. lu_dirpage.
1282 * .---------v------- -----.
1283 * |s|e|f|p|ent| 0 | ... | 0 |
1284 * '----------------- -----'
1286 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
1287 * larger than LU_PAGE_SIZE, a single host page may contain multiple
1288 * lu_dirpages. After reading the lu_dirpages from the MDS, the
1289 * ldp_hash_end of the first lu_dirpage refers to the one immediately
1290 * after it in the same CFS_PAGE (arrows simplified for brevity, but
1291 * in general e0==s1, e1==s2, etc.):
1293 * .-------------------- -----.
1294 * |s0|e0|f0|p|ent|ent| ... |ent|
1295 * |---v---------------- -----|
1296 * |s1|e1|f1|p|ent|ent| ... |ent|
1297 * |---v---------------- -----| Here, each CFS_PAGE contains
1298 * ... multiple lu_dirpages.
1299 * |---v---------------- -----|
1300 * |s'|e'|f'|p|ent|ent| ... |ent|
1301 * '---|---------------- -----'
1303 * .----------------------------.
1306 * This structure is transformed into a single logical lu_dirpage as follows:
1308 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1309 * labeled 'next CFS_PAGE'.
1311 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1312 * a hash collision with the next page exists.
1314 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1315 * to the first entry of the next lu_dirpage.
1317 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
1318 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1322 for (i = 0; i < cfs_pgs; i++) {
1323 struct lu_dirpage *dp = kmap(pages[i]);
1324 struct lu_dirpage *first = dp;
1325 struct lu_dirent *end_dirent = NULL;
1326 struct lu_dirent *ent;
1327 __u64 hash_end = dp->ldp_hash_end;
1328 __u32 flags = dp->ldp_flags;
1330 while (--lu_pgs > 0) {
1331 ent = lu_dirent_start(dp);
1332 for (end_dirent = ent; ent != NULL;
1333 end_dirent = ent, ent = lu_dirent_next(ent));
1335 /* Advance dp to next lu_dirpage. */
1336 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1338 /* Check if we've reached the end of the CFS_PAGE. */
1339 if (!((unsigned long)dp & ~CFS_PAGE_MASK))
1342 /* Save the hash and flags of this lu_dirpage. */
1343 hash_end = dp->ldp_hash_end;
1344 flags = dp->ldp_flags;
1346 /* Check if lu_dirpage contains no entries. */
1347 if (end_dirent == NULL)
1350 /* Enlarge the end entry lde_reclen from 0 to
1351 * first entry of next lu_dirpage. */
1352 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1353 end_dirent->lde_reclen =
1354 cpu_to_le16((char *)(dp->ldp_entries) -
1355 (char *)end_dirent);
1358 first->ldp_hash_end = hash_end;
1359 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1360 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1364 LASSERTF(lu_pgs == 0, "left = %d", lu_pgs);
1367 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1368 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
1370 /* parameters for readdir page */
1371 struct readpage_param {
1372 struct md_op_data *rp_mod;
1375 struct obd_export *rp_exp;
1376 struct md_callback *rp_cb;
1380 * Read pages from server.
1382 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1383 * a header lu_dirpage which describes the start/end hash, and whether this
1384 * page is empty (contains no dir entry) or hash collide with next page.
1385 * After client receives reply, several pages will be integrated into dir page
1386 * in CFS_PAGE_SIZE (if CFS_PAGE_SIZE greater than LU_PAGE_SIZE), and the
1387 * lu_dirpage for this integrated page will be adjusted.
1389 static int mdc_read_page_remote(void *data, struct page *page0)
1391 struct readpage_param *rp = data;
1392 struct page **page_pool;
1394 struct lu_dirpage *dp;
1395 int rd_pgs = 0; /* number of pages read actually */
1397 struct md_op_data *op_data = rp->rp_mod;
1398 struct ptlrpc_request *req;
1399 int max_pages = op_data->op_max_pages;
1400 struct inode *inode;
1406 LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
1407 if (op_data->op_mea1 != NULL) {
1408 __u32 index = op_data->op_stripe_offset;
1410 inode = op_data->op_mea1->lsm_md_oinfo[index].lmo_root;
1411 fid = &op_data->op_mea1->lsm_md_oinfo[index].lmo_fid;
1413 inode = op_data->op_data;
1414 fid = &op_data->op_fid1;
1416 LASSERT(inode != NULL);
1418 OBD_ALLOC(page_pool, sizeof(page_pool[0]) * max_pages);
1419 if (page_pool != NULL) {
1420 page_pool[0] = page0;
1426 for (npages = 1; npages < max_pages; npages++) {
1427 page = page_cache_alloc_cold(inode->i_mapping);
1430 page_pool[npages] = page;
1433 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, op_data->op_capa1,
1434 page_pool, npages, &req);
1438 rd_pgs = (req->rq_bulk->bd_nob_transferred +
1439 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1440 lu_pgs = req->rq_bulk->bd_nob_transferred >>
1442 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1444 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", rd_pgs, lu_pgs,
1445 op_data->op_npages);
1447 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1449 SetPageUptodate(page0);
1453 ptlrpc_req_finished(req);
1454 CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1455 for (i = 1; i < npages; i++) {
1456 unsigned long offset;
1460 page = page_pool[i];
1462 if (rc < 0 || i >= rd_pgs) {
1463 page_cache_release(page);
1467 SetPageUptodate(page);
1470 hash = le64_to_cpu(dp->ldp_hash_start);
1473 offset = hash_x_index(hash, rp->rp_hash64);
1475 prefetchw(&page->flags);
1476 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1481 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1482 " rc = %d\n", offset, ret);
1483 page_cache_release(page);
1486 if (page_pool != &page0)
1487 OBD_FREE(page_pool, sizeof(page_pool[0]) * max_pages);
1493 * Read dir page from cache first, if it can not find it, read it from
1494 * server and add into the cache.
1496 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1497 struct md_callback *cb_op, struct page **ppage)
1499 struct lookup_intent it = { .it_op = IT_READDIR };
1501 struct inode *dir = NULL;
1502 struct address_space *mapping;
1503 struct lu_dirpage *dp;
1506 struct lustre_handle lockh;
1507 struct ptlrpc_request *enq_req = NULL;
1508 struct readpage_param rp_param;
1515 if (op_data->op_mea1 != NULL) {
1516 __u32 index = op_data->op_stripe_offset;
1518 dir = op_data->op_mea1->lsm_md_oinfo[index].lmo_root;
1520 dir = op_data->op_data;
1522 LASSERT(dir != NULL);
1524 mapping = dir->i_mapping;
1526 rc = mdc_intent_lock(exp, op_data, NULL, 0, &it, 0, &enq_req,
1527 cb_op->md_blocking_ast, 0);
1528 if (enq_req != NULL)
1529 ptlrpc_req_finished(enq_req);
1532 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1533 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1538 mdc_set_lock_data(exp, &it.d.lustre.it_lock_handle, dir, NULL);
1540 rp_param.rp_off = op_data->op_hash_offset;
1541 rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1542 page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1543 rp_param.rp_hash64);
1545 CERROR("%s: dir page locate: "DFID" at "LPU64": rc %ld\n",
1546 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1547 rp_param.rp_off, PTR_ERR(page));
1548 GOTO(out_unlock, rc = PTR_ERR(page));
1549 } else if (page != NULL) {
1551 * XXX nikita: not entirely correct handling of a corner case:
1552 * suppose hash chain of entries with hash value HASH crosses
1553 * border between pages P0 and P1. First both P0 and P1 are
1554 * cached, seekdir() is called for some entry from the P0 part
1555 * of the chain. Later P0 goes out of cache. telldir(HASH)
1556 * happens and finds P1, as it starts with matching hash
1557 * value. Remaining entries from P0 part of the chain are
1558 * skipped. (Is that really a bug?)
1560 * Possible solutions: 0. don't cache P1 is such case, handle
1561 * it as an "overflow" page. 1. invalidate all pages at
1562 * once. 2. use HASH|1 as an index for P1.
1564 GOTO(hash_collision, page);
1567 rp_param.rp_exp = exp;
1568 rp_param.rp_mod = op_data;
1569 page = read_cache_page(mapping,
1570 hash_x_index(rp_param.rp_off,
1571 rp_param.rp_hash64),
1572 mdc_read_page_remote, &rp_param);
1574 CERROR("%s: read cache page: "DFID" at "LPU64": rc %ld\n",
1575 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1576 rp_param.rp_off, PTR_ERR(page));
1577 GOTO(out_unlock, rc = PTR_ERR(page));
1580 wait_on_page_locked(page);
1582 if (!PageUptodate(page)) {
1583 CERROR("%s: page not updated: "DFID" at "LPU64": rc %d\n",
1584 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1585 rp_param.rp_off, -5);
1588 if (!PageChecked(page))
1589 SetPageChecked(page);
1590 if (PageError(page)) {
1591 CERROR("%s: page error: "DFID" at "LPU64": rc %d\n",
1592 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1593 rp_param.rp_off, -5);
1598 dp = page_address(page);
1599 if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1600 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1601 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1602 rp_param.rp_off = op_data->op_hash_offset >> 32;
1604 start = le64_to_cpu(dp->ldp_hash_start);
1605 end = le64_to_cpu(dp->ldp_hash_end);
1606 rp_param.rp_off = op_data->op_hash_offset;
1609 LASSERT(start == rp_param.rp_off);
1610 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1611 #if BITS_PER_LONG == 32
1612 CWARN("Real page-wide hash collision at ["LPU64" "LPU64"] with "
1613 "hash "LPU64"\n", le64_to_cpu(dp->ldp_hash_start),
1614 le64_to_cpu(dp->ldp_hash_end), op_data->op_hash_offset);
1618 * Fetch whole overflow chain...
1626 lockh.cookie = it.d.lustre.it_lock_handle;
1627 ldlm_lock_decref(&lockh, it.d.lustre.it_lock_mode);
1628 it.d.lustre.it_lock_handle = 0;
1632 mdc_release_page(page, 1);
1638 * Read one directory entry from the cache.
1640 int mdc_read_entry(struct obd_export *exp, struct md_op_data *op_data,
1641 struct md_callback *cb_op, struct lu_dirent **entp,
1642 struct page **ppage)
1644 struct page *page = NULL;
1645 struct lu_dirpage *dp;
1646 struct lu_dirent *ent;
1650 CDEBUG(D_INFO, DFID "offset = "LPU64"\n", PFID(&op_data->op_fid1),
1651 op_data->op_hash_offset);
1656 if (op_data->op_hash_offset == MDS_DIR_END_OFF)
1659 rc = mdc_read_page(exp, op_data, cb_op, &page);
1663 dp = page_address(page);
1664 for (ent = lu_dirent_start(dp); ent != NULL;
1665 ent = lu_dirent_next(ent)) {
1666 /* Skip dummy entry */
1667 if (le16_to_cpu(ent->lde_namelen) == 0)
1670 if (le64_to_cpu(ent->lde_hash) > op_data->op_hash_offset)
1674 /* If it can not find entry in current page, try next page. */
1676 __u64 orig_offset = op_data->op_hash_offset;
1678 if (dp->ldp_hash_end == MDS_DIR_END_OFF) {
1679 mdc_release_page(page, 0);
1683 op_data->op_hash_offset = dp->ldp_hash_end;
1684 mdc_release_page(page,
1685 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1686 rc = mdc_read_page(exp, op_data, cb_op, &page);
1691 dp = page_address(page);
1692 ent = lu_dirent_start(dp);
1695 op_data->op_hash_offset = orig_offset;
1704 #else /* __KERNEL__ */
1707 *mdc_read_page_remote(struct obd_export *exp, const struct lmv_oinfo *lmo,
1708 const __u64 hash, struct obd_capa *oc)
1710 struct ptlrpc_request *req = NULL;
1714 OBD_PAGE_ALLOC(page, 0);
1716 return ERR_PTR(-ENOMEM);
1718 rc = mdc_getpage(exp, &lmo->lmo_fid, hash, oc, &page, 1, &req);
1720 ptlrpc_req_finished(req);
1723 OBD_PAGE_FREE(page);
1730 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1731 struct md_callback *cb_op,
1732 struct page **ppage)
1735 struct lmv_oinfo *lmo;
1738 /* No local cache for liblustre, always read entry remotely */
1739 lmo = &op_data->op_mea1->lsm_md_oinfo[op_data->op_stripe_offset];
1740 page = mdc_read_page_remote(exp, lmo, op_data->op_hash_offset,
1743 return PTR_ERR(page);
1750 int mdc_read_entry(struct obd_export *exp, struct md_op_data *op_data,
1751 struct md_callback *cb_op, struct lu_dirent **entp,
1752 struct page **ppage)
1754 struct page *page = NULL;
1755 struct lu_dirpage *dp;
1756 struct lu_dirent *ent;
1760 rc = mdc_read_page(exp, op_data, cb_op, &page);
1764 dp = page_address(page);
1765 if (dp->ldp_hash_end < op_data->op_hash_offset)
1766 GOTO(out, *entp = NULL);
1768 for (ent = lu_dirent_start(dp); ent != NULL;
1769 ent = lu_dirent_next(ent))
1770 if (ent->lde_hash >= op_data->op_hash_offset)
1775 OBD_PAGE_FREE(page);
1781 static int mdc_statfs(const struct lu_env *env,
1782 struct obd_export *exp, struct obd_statfs *osfs,
1783 __u64 max_age, __u32 flags)
1785 struct obd_device *obd = class_exp2obd(exp);
1786 struct ptlrpc_request *req;
1787 struct obd_statfs *msfs;
1788 struct obd_import *imp = NULL;
1793 * Since the request might also come from lprocfs, so we need
1794 * sync this with client_disconnect_export Bug15684
1796 down_read(&obd->u.cli.cl_sem);
1797 if (obd->u.cli.cl_import)
1798 imp = class_import_get(obd->u.cli.cl_import);
1799 up_read(&obd->u.cli.cl_sem);
1803 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1804 LUSTRE_MDS_VERSION, MDS_STATFS);
1806 GOTO(output, rc = -ENOMEM);
1808 ptlrpc_request_set_replen(req);
1810 if (flags & OBD_STATFS_NODELAY) {
1811 /* procfs requests not want stay in wait for avoid deadlock */
1812 req->rq_no_resend = 1;
1813 req->rq_no_delay = 1;
1816 rc = ptlrpc_queue_wait(req);
1818 /* check connection error first */
1819 if (imp->imp_connect_error)
1820 rc = imp->imp_connect_error;
1824 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1826 GOTO(out, rc = -EPROTO);
1831 ptlrpc_req_finished(req);
1833 class_import_put(imp);
1837 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1839 __u32 keylen, vallen;
1843 if (gf->gf_pathlen > PATH_MAX)
1844 RETURN(-ENAMETOOLONG);
1845 if (gf->gf_pathlen < 2)
1848 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1849 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1850 OBD_ALLOC(key, keylen);
1853 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1854 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1856 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1857 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1859 if (!fid_is_sane(&gf->gf_fid))
1860 GOTO(out, rc = -EINVAL);
1862 /* Val is struct getinfo_fid2path result plus path */
1863 vallen = sizeof(*gf) + gf->gf_pathlen;
1865 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1866 if (rc != 0 && rc != -EREMOTE)
1869 if (vallen <= sizeof(*gf))
1870 GOTO(out, rc = -EPROTO);
1871 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1872 GOTO(out, rc = -EOVERFLOW);
1874 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1875 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1878 OBD_FREE(key, keylen);
1882 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1883 struct hsm_progress_kernel *hpk)
1885 struct obd_import *imp = class_exp2cliimp(exp);
1886 struct hsm_progress_kernel *req_hpk;
1887 struct ptlrpc_request *req;
1891 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1892 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1894 GOTO(out, rc = -ENOMEM);
1896 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1898 /* Copy hsm_progress struct */
1899 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1900 if (req_hpk == NULL)
1901 GOTO(out, rc = -EPROTO);
1904 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1906 ptlrpc_request_set_replen(req);
1908 rc = mdc_queue_wait(req);
1911 ptlrpc_req_finished(req);
1915 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1917 __u32 *archive_mask;
1918 struct ptlrpc_request *req;
1922 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1924 MDS_HSM_CT_REGISTER);
1926 GOTO(out, rc = -ENOMEM);
1928 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1930 /* Copy hsm_progress struct */
1931 archive_mask = req_capsule_client_get(&req->rq_pill,
1932 &RMF_MDS_HSM_ARCHIVE);
1933 if (archive_mask == NULL)
1934 GOTO(out, rc = -EPROTO);
1936 *archive_mask = archives;
1938 ptlrpc_request_set_replen(req);
1940 rc = mdc_queue_wait(req);
1943 ptlrpc_req_finished(req);
1947 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1948 struct md_op_data *op_data)
1950 struct hsm_current_action *hca = op_data->op_data;
1951 struct hsm_current_action *req_hca;
1952 struct ptlrpc_request *req;
1956 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1957 &RQF_MDS_HSM_ACTION);
1961 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1963 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1965 ptlrpc_request_free(req);
1969 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1970 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1972 ptlrpc_request_set_replen(req);
1974 rc = mdc_queue_wait(req);
1978 req_hca = req_capsule_server_get(&req->rq_pill,
1979 &RMF_MDS_HSM_CURRENT_ACTION);
1980 if (req_hca == NULL)
1981 GOTO(out, rc = -EPROTO);
1987 ptlrpc_req_finished(req);
1991 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1993 struct ptlrpc_request *req;
1997 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1999 MDS_HSM_CT_UNREGISTER);
2001 GOTO(out, rc = -ENOMEM);
2003 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
2005 ptlrpc_request_set_replen(req);
2007 rc = mdc_queue_wait(req);
2010 ptlrpc_req_finished(req);
2014 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
2015 struct md_op_data *op_data)
2017 struct hsm_user_state *hus = op_data->op_data;
2018 struct hsm_user_state *req_hus;
2019 struct ptlrpc_request *req;
2023 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2024 &RQF_MDS_HSM_STATE_GET);
2028 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2030 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
2032 ptlrpc_request_free(req);
2036 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
2037 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
2039 ptlrpc_request_set_replen(req);
2041 rc = mdc_queue_wait(req);
2045 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
2046 if (req_hus == NULL)
2047 GOTO(out, rc = -EPROTO);
2053 ptlrpc_req_finished(req);
2057 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
2058 struct md_op_data *op_data)
2060 struct hsm_state_set *hss = op_data->op_data;
2061 struct hsm_state_set *req_hss;
2062 struct ptlrpc_request *req;
2066 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2067 &RQF_MDS_HSM_STATE_SET);
2071 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2073 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
2075 ptlrpc_request_free(req);
2079 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
2080 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
2083 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
2084 if (req_hss == NULL)
2085 GOTO(out, rc = -EPROTO);
2088 ptlrpc_request_set_replen(req);
2090 rc = mdc_queue_wait(req);
2095 ptlrpc_req_finished(req);
2099 static int mdc_ioc_hsm_request(struct obd_export *exp,
2100 struct hsm_user_request *hur)
2102 struct obd_import *imp = class_exp2cliimp(exp);
2103 struct ptlrpc_request *req;
2104 struct hsm_request *req_hr;
2105 struct hsm_user_item *req_hui;
2110 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
2112 GOTO(out, rc = -ENOMEM);
2114 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
2115 hur->hur_request.hr_itemcount
2116 * sizeof(struct hsm_user_item));
2117 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
2118 hur->hur_request.hr_data_len);
2120 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
2122 ptlrpc_request_free(req);
2126 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
2128 /* Copy hsm_request struct */
2129 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
2131 GOTO(out, rc = -EPROTO);
2132 *req_hr = hur->hur_request;
2134 /* Copy hsm_user_item structs */
2135 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
2136 if (req_hui == NULL)
2137 GOTO(out, rc = -EPROTO);
2138 memcpy(req_hui, hur->hur_user_item,
2139 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
2141 /* Copy opaque field */
2142 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
2143 if (req_opaque == NULL)
2144 GOTO(out, rc = -EPROTO);
2145 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
2147 ptlrpc_request_set_replen(req);
2149 rc = mdc_queue_wait(req);
2153 ptlrpc_req_finished(req);
2157 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
2159 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
2161 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
2163 lh->kuc_magic = KUC_MAGIC;
2164 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
2165 lh->kuc_flags = flags;
2166 lh->kuc_msgtype = CL_RECORD;
2167 lh->kuc_msglen = len;
2171 #define D_CHANGELOG 0
2173 struct changelog_show {
2178 struct obd_device *cs_obd;
2181 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
2182 struct llog_rec_hdr *hdr, void *data)
2184 struct changelog_show *cs = data;
2185 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
2190 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
2192 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
2193 cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
2194 rec->cr.cr_type, rc);
2198 if (rec->cr.cr_index < cs->cs_startrec) {
2199 /* Skip entries earlier than what we are interested in */
2200 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
2201 rec->cr.cr_index, cs->cs_startrec);
2205 CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
2206 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
2207 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
2208 rec->cr.cr_flags & CLF_FLAGMASK,
2209 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
2210 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
2212 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
2214 /* Set up the message */
2215 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
2216 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
2218 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
2219 CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
2224 static int mdc_changelog_send_thread(void *csdata)
2226 struct changelog_show *cs = csdata;
2227 struct llog_ctxt *ctxt = NULL;
2228 struct llog_handle *llh = NULL;
2229 struct kuc_hdr *kuch;
2232 CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
2233 cs->cs_fp, cs->cs_startrec);
2235 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
2236 if (cs->cs_buf == NULL)
2237 GOTO(out, rc = -ENOMEM);
2239 /* Set up the remote catalog handle */
2240 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
2242 GOTO(out, rc = -ENOENT);
2243 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
2246 CERROR("%s: fail to open changelog catalog: rc = %d\n",
2247 cs->cs_obd->obd_name, rc);
2250 rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
2252 CERROR("llog_init_handle failed %d\n", rc);
2256 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
2258 /* Send EOF no matter what our result */
2259 if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
2261 kuch->kuc_msgtype = CL_EOF;
2262 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
2268 llog_cat_close(NULL, llh);
2270 llog_ctxt_put(ctxt);
2272 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
2277 static int mdc_ioc_changelog_send(struct obd_device *obd,
2278 struct ioc_changelog *icc)
2280 struct changelog_show *cs;
2283 /* Freed in mdc_changelog_send_thread */
2289 cs->cs_startrec = icc->icc_recno;
2290 /* matching fput in mdc_changelog_send_thread */
2291 cs->cs_fp = fget(icc->icc_id);
2292 cs->cs_flags = icc->icc_flags;
2295 * New thread because we should return to user app before
2296 * writing into our pipe
2298 rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
2299 "mdc_clg_send_thread"));
2300 if (!IS_ERR_VALUE(rc)) {
2301 CDEBUG(D_CHANGELOG, "start changelog thread\n");
2305 CERROR("Failed to start changelog thread: %d\n", rc);
2310 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2311 struct lustre_kernelcomm *lk);
2313 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
2314 struct obd_quotactl *oqctl)
2316 struct client_obd *cli = &exp->exp_obd->u.cli;
2317 struct ptlrpc_request *req;
2318 struct obd_quotactl *body;
2322 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2323 &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
2328 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2331 ptlrpc_request_set_replen(req);
2333 /* the next poll will find -ENODATA, that means quotacheck is
2335 cli->cl_qchk_stat = -ENODATA;
2336 rc = ptlrpc_queue_wait(req);
2338 cli->cl_qchk_stat = rc;
2339 ptlrpc_req_finished(req);
2343 static int mdc_quota_poll_check(struct obd_export *exp,
2344 struct if_quotacheck *qchk)
2346 struct client_obd *cli = &exp->exp_obd->u.cli;
2350 qchk->obd_uuid = cli->cl_target_uuid;
2351 memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
2353 rc = cli->cl_qchk_stat;
2354 /* the client is not the previous one */
2355 if (rc == CL_NOT_QUOTACHECKED)
2360 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2361 struct obd_quotactl *oqctl)
2363 struct ptlrpc_request *req;
2364 struct obd_quotactl *oqc;
2368 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2369 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
2374 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2377 ptlrpc_request_set_replen(req);
2378 ptlrpc_at_set_req_timeout(req);
2379 req->rq_no_resend = 1;
2381 rc = ptlrpc_queue_wait(req);
2383 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
2385 if (req->rq_repmsg &&
2386 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2389 CERROR ("Can't unpack obd_quotactl\n");
2392 ptlrpc_req_finished(req);
2397 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2398 struct md_op_data *op_data)
2400 CFS_LIST_HEAD(cancels);
2401 struct ptlrpc_request *req;
2403 struct mdc_swap_layouts *msl, *payload;
2406 msl = op_data->op_data;
2408 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2409 * first thing it will do is to cancel the 2 layout
2410 * locks hold by this client.
2411 * So the client must cancel its layout locks on the 2 fids
2412 * with the request RPC to avoid extra RPC round trips
2414 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2415 LCK_CR, MDS_INODELOCK_LAYOUT);
2416 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2417 LCK_CR, MDS_INODELOCK_LAYOUT);
2419 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2420 &RQF_MDS_SWAP_LAYOUTS);
2422 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2426 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2427 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
2429 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2431 ptlrpc_request_free(req);
2435 mdc_swap_layouts_pack(req, op_data);
2437 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2442 ptlrpc_request_set_replen(req);
2444 rc = ptlrpc_queue_wait(req);
2450 ptlrpc_req_finished(req);
2454 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2455 void *karg, void *uarg)
2457 struct obd_device *obd = exp->exp_obd;
2458 struct obd_ioctl_data *data = karg;
2459 struct obd_import *imp = obd->u.cli.cl_import;
2463 if (!try_module_get(THIS_MODULE)) {
2464 CERROR("Can't get module. Is it alive?");
2468 case OBD_IOC_CHANGELOG_SEND:
2469 rc = mdc_ioc_changelog_send(obd, karg);
2471 case OBD_IOC_CHANGELOG_CLEAR: {
2472 struct ioc_changelog *icc = karg;
2473 struct changelog_setinfo cs =
2474 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
2475 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2476 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2480 case OBD_IOC_FID2PATH:
2481 rc = mdc_ioc_fid2path(exp, karg);
2483 case LL_IOC_HSM_CT_START:
2484 rc = mdc_ioc_hsm_ct_start(exp, karg);
2485 /* ignore if it was already registered on this MDS. */
2489 case LL_IOC_HSM_PROGRESS:
2490 rc = mdc_ioc_hsm_progress(exp, karg);
2492 case LL_IOC_HSM_STATE_GET:
2493 rc = mdc_ioc_hsm_state_get(exp, karg);
2495 case LL_IOC_HSM_STATE_SET:
2496 rc = mdc_ioc_hsm_state_set(exp, karg);
2498 case LL_IOC_HSM_ACTION:
2499 rc = mdc_ioc_hsm_current_action(exp, karg);
2501 case LL_IOC_HSM_REQUEST:
2502 rc = mdc_ioc_hsm_request(exp, karg);
2504 case OBD_IOC_CLIENT_RECOVER:
2505 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2509 case IOC_OSC_SET_ACTIVE:
2510 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2512 case OBD_IOC_POLL_QUOTACHECK:
2513 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
2515 case OBD_IOC_PING_TARGET:
2516 rc = ptlrpc_obd_ping(obd);
2519 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2520 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2521 * there'd be no LMV layer thus we might be called here. Eventually
2522 * this code should be removed.
2525 case IOC_OBD_STATFS: {
2526 struct obd_statfs stat_buf = {0};
2528 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2529 GOTO(out, rc = -ENODEV);
2532 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2533 min((int)data->ioc_plen2,
2534 (int)sizeof(struct obd_uuid))))
2535 GOTO(out, rc = -EFAULT);
2537 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2538 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2543 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2544 min((int) data->ioc_plen1,
2545 (int) sizeof(stat_buf))))
2546 GOTO(out, rc = -EFAULT);
2550 case OBD_IOC_QUOTACTL: {
2551 struct if_quotactl *qctl = karg;
2552 struct obd_quotactl *oqctl;
2554 OBD_ALLOC_PTR(oqctl);
2556 GOTO(out, rc = -ENOMEM);
2558 QCTL_COPY(oqctl, qctl);
2559 rc = obd_quotactl(exp, oqctl);
2561 QCTL_COPY(qctl, oqctl);
2562 qctl->qc_valid = QC_MDTIDX;
2563 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2566 OBD_FREE_PTR(oqctl);
2569 case LL_IOC_GET_CONNECT_FLAGS:
2570 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2571 sizeof(*exp_connect_flags_ptr(exp))))
2572 GOTO(out, rc = -EFAULT);
2575 case LL_IOC_LOV_SWAP_LAYOUTS:
2576 rc = mdc_ioc_swap_layouts(exp, karg);
2579 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2580 GOTO(out, rc = -ENOTTY);
2583 module_put(THIS_MODULE);
2588 int mdc_get_info_rpc(struct obd_export *exp,
2589 obd_count keylen, void *key,
2590 int vallen, void *val)
2592 struct obd_import *imp = class_exp2cliimp(exp);
2593 struct ptlrpc_request *req;
2598 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2602 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2603 RCL_CLIENT, keylen);
2604 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2605 RCL_CLIENT, sizeof(__u32));
2607 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2609 ptlrpc_request_free(req);
2613 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2614 memcpy(tmp, key, keylen);
2615 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2616 memcpy(tmp, &vallen, sizeof(__u32));
2618 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2619 RCL_SERVER, vallen);
2620 ptlrpc_request_set_replen(req);
2622 rc = ptlrpc_queue_wait(req);
2623 /* -EREMOTE means the get_info result is partial, and it needs to
2624 * continue on another MDT, see fid2path part in lmv_iocontrol */
2625 if (rc == 0 || rc == -EREMOTE) {
2626 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2627 memcpy(val, tmp, vallen);
2628 if (ptlrpc_rep_need_swab(req)) {
2629 if (KEY_IS(KEY_FID2PATH))
2630 lustre_swab_fid2path(val);
2633 ptlrpc_req_finished(req);
2638 static void lustre_swab_hai(struct hsm_action_item *h)
2640 __swab32s(&h->hai_len);
2641 __swab32s(&h->hai_action);
2642 lustre_swab_lu_fid(&h->hai_fid);
2643 lustre_swab_lu_fid(&h->hai_dfid);
2644 __swab64s(&h->hai_cookie);
2645 __swab64s(&h->hai_extent.offset);
2646 __swab64s(&h->hai_extent.length);
2647 __swab64s(&h->hai_gid);
2650 static void lustre_swab_hal(struct hsm_action_list *h)
2652 struct hsm_action_item *hai;
2655 __swab32s(&h->hal_version);
2656 __swab32s(&h->hal_count);
2657 __swab32s(&h->hal_archive_id);
2658 __swab64s(&h->hal_flags);
2660 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2661 lustre_swab_hai(hai);
2664 static void lustre_swab_kuch(struct kuc_hdr *l)
2666 __swab16s(&l->kuc_magic);
2667 /* __u8 l->kuc_transport */
2668 __swab16s(&l->kuc_msgtype);
2669 __swab16s(&l->kuc_msglen);
2672 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2673 struct lustre_kernelcomm *lk)
2675 struct obd_import *imp = class_exp2cliimp(exp);
2676 __u32 archive = lk->lk_data;
2679 if (lk->lk_group != KUC_GRP_HSM) {
2680 CERROR("Bad copytool group %d\n", lk->lk_group);
2684 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2685 lk->lk_uid, lk->lk_group, lk->lk_flags);
2687 if (lk->lk_flags & LK_FLG_STOP) {
2688 /* Unregister with the coordinator */
2689 rc = mdc_ioc_hsm_ct_unregister(imp);
2691 rc = mdc_ioc_hsm_ct_register(imp, archive);
2698 * Send a message to any listening copytools
2699 * @param val KUC message (kuc_hdr + hsm_action_list)
2700 * @param len total length of message
2702 static int mdc_hsm_copytool_send(int len, void *val)
2704 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2705 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2709 if (len < sizeof(*lh) + sizeof(*hal)) {
2710 CERROR("Short HSM message %d < %d\n", len,
2711 (int) (sizeof(*lh) + sizeof(*hal)));
2714 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2715 lustre_swab_kuch(lh);
2716 lustre_swab_hal(hal);
2717 } else if (lh->kuc_magic != KUC_MAGIC) {
2718 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2722 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2724 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2725 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2727 /* Broadcast to HSM listeners */
2728 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2734 * callback function passed to kuc for re-registering each HSM copytool
2735 * running on MDC, after MDT shutdown/recovery.
2736 * @param data copytool registration data
2737 * @param cb_arg callback argument (obd_import)
2739 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2741 struct kkuc_ct_data *kcd = data;
2742 struct obd_import *imp = (struct obd_import *)cb_arg;
2745 if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2748 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2751 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2752 imp->imp_obd->obd_name, kcd->kcd_archive);
2753 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2755 /* ignore error if the copytool is already registered */
2756 return (rc == -EEXIST) ? 0 : rc;
2760 * Re-establish all kuc contexts with MDT
2761 * after MDT shutdown/recovery.
2763 static int mdc_kuc_reregister(struct obd_import *imp)
2765 /* re-register HSM agents */
2766 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2770 int mdc_set_info_async(const struct lu_env *env,
2771 struct obd_export *exp,
2772 obd_count keylen, void *key,
2773 obd_count vallen, void *val,
2774 struct ptlrpc_request_set *set)
2776 struct obd_import *imp = class_exp2cliimp(exp);
2780 if (KEY_IS(KEY_READ_ONLY)) {
2781 if (vallen != sizeof(int))
2784 spin_lock(&imp->imp_lock);
2785 if (*((int *)val)) {
2786 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2787 imp->imp_connect_data.ocd_connect_flags |=
2790 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2791 imp->imp_connect_data.ocd_connect_flags &=
2792 ~OBD_CONNECT_RDONLY;
2794 spin_unlock(&imp->imp_lock);
2796 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2797 keylen, key, vallen, val, set);
2800 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2801 sptlrpc_conf_client_adapt(exp->exp_obd);
2804 if (KEY_IS(KEY_FLUSH_CTX)) {
2805 sptlrpc_import_flush_my_ctx(imp);
2808 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2809 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2810 keylen, key, vallen, val, set);
2813 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2814 rc = mdc_hsm_copytool_send(vallen, val);
2818 CERROR("Unknown key %s\n", (char *)key);
2822 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2823 __u32 keylen, void *key, __u32 *vallen, void *val,
2824 struct lov_stripe_md *lsm)
2828 if (KEY_IS(KEY_MAX_EASIZE)) {
2829 int mdsize, *max_easize;
2831 if (*vallen != sizeof(int))
2833 mdsize = *(int*)val;
2834 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2835 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2837 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2839 } else if (KEY_IS(KEY_CONN_DATA)) {
2840 struct obd_import *imp = class_exp2cliimp(exp);
2841 struct obd_connect_data *data = val;
2843 if (*vallen != sizeof(*data))
2846 *data = imp->imp_connect_data;
2848 } else if (KEY_IS(KEY_TGT_COUNT)) {
2853 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2858 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2859 struct obd_capa *oc, struct obd_client_handle *handle,
2862 struct ptlrpc_request *req;
2863 struct mdt_body *body;
2867 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2871 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2873 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2875 ptlrpc_request_free(req);
2879 mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2881 ptlrpc_request_set_replen(req);
2883 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2884 rc = ptlrpc_queue_wait(req);
2885 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2887 CERROR("Pin failed: %d\n", rc);
2891 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2893 GOTO(err_out, rc = -EPROTO);
2895 handle->och_fh = body->handle;
2896 handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2898 handle->och_mod = obd_mod_alloc();
2899 if (handle->och_mod == NULL) {
2900 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2901 GOTO(err_out, rc = -ENOMEM);
2903 handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2908 ptlrpc_req_finished(req);
2912 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2915 struct ptlrpc_request *req;
2916 struct mdt_body *body;
2920 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2921 LUSTRE_MDS_VERSION, MDS_UNPIN);
2925 body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2926 body->handle = handle->och_fh;
2929 ptlrpc_request_set_replen(req);
2931 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2932 rc = ptlrpc_queue_wait(req);
2933 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2936 CERROR("Unpin failed: %d\n", rc);
2938 ptlrpc_req_finished(req);
2939 ptlrpc_req_finished(handle->och_mod->mod_open_req);
2941 obd_mod_put(handle->och_mod);
2945 int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2946 struct obd_capa *oc, struct ptlrpc_request **request)
2948 struct ptlrpc_request *req;
2953 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2957 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2959 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2961 ptlrpc_request_free(req);
2965 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2967 ptlrpc_request_set_replen(req);
2969 rc = ptlrpc_queue_wait(req);
2971 ptlrpc_req_finished(req);
2977 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2978 enum obd_import_event event)
2982 LASSERT(imp->imp_obd == obd);
2985 case IMP_EVENT_DISCON: {
2987 /* XXX Pass event up to OBDs stack. used only for FLD now */
2988 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2992 case IMP_EVENT_INACTIVE: {
2993 struct client_obd *cli = &obd->u.cli;
2995 * Flush current sequence to make client obtain new one
2996 * from server in case of disconnect/reconnect.
2998 if (cli->cl_seq != NULL)
2999 seq_client_flush(cli->cl_seq);
3001 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
3004 case IMP_EVENT_INVALIDATE: {
3005 struct ldlm_namespace *ns = obd->obd_namespace;
3007 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
3011 case IMP_EVENT_ACTIVE:
3012 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
3013 /* redo the kuc registration after reconnecting */
3015 rc = mdc_kuc_reregister(imp);
3018 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
3020 case IMP_EVENT_DEACTIVATE:
3021 case IMP_EVENT_ACTIVATE:
3024 CERROR("Unknown import event %x\n", event);
3030 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
3031 struct md_op_data *op_data)
3033 struct client_obd *cli = &exp->exp_obd->u.cli;
3034 struct lu_client_seq *seq = cli->cl_seq;
3036 RETURN(seq_client_alloc_fid(NULL, seq, fid));
3039 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
3040 struct client_obd *cli = &exp->exp_obd->u.cli;
3041 return &cli->cl_target_uuid;
3045 * Determine whether the lock can be canceled before replaying it during
3046 * recovery, non zero value will be return if the lock can be canceled,
3047 * or zero returned for not
3049 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
3051 if (lock->l_resource->lr_type != LDLM_IBITS)
3054 /* FIXME: if we ever get into a situation where there are too many
3055 * opened files with open locks on a single node, then we really
3056 * should replay these open locks to reget it */
3057 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
3063 static int mdc_resource_inode_free(struct ldlm_resource *res)
3065 if (res->lr_lvb_inode)
3066 res->lr_lvb_inode = NULL;
3071 struct ldlm_valblock_ops inode_lvbo = {
3072 lvbo_free: mdc_resource_inode_free
3075 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
3077 struct client_obd *cli = &obd->u.cli;
3081 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
3082 if (!cli->cl_rpc_lock)
3084 mdc_init_rpc_lock(cli->cl_rpc_lock);
3086 rc = ptlrpcd_addref();
3088 GOTO(err_rpc_lock, rc);
3090 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
3091 if (!cli->cl_close_lock)
3092 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
3093 mdc_init_rpc_lock(cli->cl_close_lock);
3095 rc = client_obd_setup(obd, cfg);
3097 GOTO(err_close_lock, rc);
3099 obd->obd_vars = lprocfs_mdc_obd_vars;
3100 lprocfs_seq_obd_setup(obd);
3101 lprocfs_alloc_md_stats(obd, 0);
3103 sptlrpc_lprocfs_cliobd_attach(obd);
3104 ptlrpc_lprocfs_register_obd(obd);
3106 ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
3108 obd->obd_namespace->ns_lvbo = &inode_lvbo;
3110 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
3113 CERROR("failed to setup llogging subsystems\n");
3119 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
3123 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
3127 /* Initialize the default and maximum LOV EA and cookie sizes. This allows
3128 * us to make MDS RPCs with large enough reply buffers to hold the
3129 * maximum-sized (= maximum striped) EA and cookie without having to
3130 * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
3131 static int mdc_init_ea_size(struct obd_export *exp, int easize,
3132 int def_easize, int cookiesize)
3134 struct obd_device *obd = exp->exp_obd;
3135 struct client_obd *cli = &obd->u.cli;
3138 if (cli->cl_max_mds_easize < easize)
3139 cli->cl_max_mds_easize = easize;
3141 if (cli->cl_default_mds_easize < def_easize)
3142 cli->cl_default_mds_easize = def_easize;
3144 if (cli->cl_max_mds_cookiesize < cookiesize)
3145 cli->cl_max_mds_cookiesize = cookiesize;
3150 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
3156 case OBD_CLEANUP_EARLY:
3158 case OBD_CLEANUP_EXPORTS:
3159 /* Failsafe, ok if racy */
3160 if (obd->obd_type->typ_refcnt <= 1)
3161 libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
3163 obd_cleanup_client_import(obd);
3164 ptlrpc_lprocfs_unregister_obd(obd);
3165 lprocfs_obd_cleanup(obd);
3166 lprocfs_free_md_stats(obd);
3168 rc = obd_llog_finish(obd, 0);
3170 CERROR("failed to cleanup llogging subsystems\n");
3176 static int mdc_cleanup(struct obd_device *obd)
3178 struct client_obd *cli = &obd->u.cli;
3180 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
3181 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
3185 return client_obd_cleanup(obd);
3189 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
3190 struct obd_device *tgt, int *index)
3192 struct llog_ctxt *ctxt;
3197 LASSERT(olg == &obd->obd_olg);
3199 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
3204 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
3205 llog_initiator_connect(ctxt);
3206 llog_ctxt_put(ctxt);
3211 static int mdc_llog_finish(struct obd_device *obd, int count)
3213 struct llog_ctxt *ctxt;
3217 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
3219 llog_cleanup(NULL, ctxt);
3224 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
3226 struct lustre_cfg *lcfg = buf;
3227 int rc = class_process_proc_seq_param(PARAM_MDC, obd->obd_vars,
3229 return (rc > 0 ? 0: rc);
3233 /* get remote permission for current user on fid */
3234 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
3235 struct obd_capa *oc, __u32 suppgid,
3236 struct ptlrpc_request **request)
3238 struct ptlrpc_request *req;
3242 LASSERT(client_is_remote(exp));
3245 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
3249 mdc_set_capa_size(req, &RMF_CAPA1, oc);
3251 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
3253 ptlrpc_request_free(req);
3257 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
3259 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
3260 sizeof(struct mdt_remote_perm));
3262 ptlrpc_request_set_replen(req);
3264 rc = ptlrpc_queue_wait(req);
3266 ptlrpc_req_finished(req);
3272 static int mdc_interpret_renew_capa(const struct lu_env *env,
3273 struct ptlrpc_request *req, void *args,
3276 struct mdc_renew_capa_args *ra = args;
3277 struct mdt_body *body = NULL;
3278 struct lustre_capa *capa;
3282 GOTO(out, capa = ERR_PTR(status));
3284 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3286 GOTO(out, capa = ERR_PTR(-EFAULT));
3288 if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
3289 GOTO(out, capa = ERR_PTR(-ENOENT));
3291 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
3293 GOTO(out, capa = ERR_PTR(-EFAULT));
3296 ra->ra_cb(ra->ra_oc, capa);
3300 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3303 struct ptlrpc_request *req;
3304 struct mdc_renew_capa_args *ra;
3307 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
3308 LUSTRE_MDS_VERSION, MDS_GETATTR);
3312 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
3313 * capa to renew is oss capa.
3315 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
3316 ptlrpc_request_set_replen(req);
3318 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
3319 ra = ptlrpc_req_async_args(req);
3322 req->rq_interpret_reply = mdc_interpret_renew_capa;
3323 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
3327 struct obd_ops mdc_obd_ops = {
3328 .o_owner = THIS_MODULE,
3329 .o_setup = mdc_setup,
3330 .o_precleanup = mdc_precleanup,
3331 .o_cleanup = mdc_cleanup,
3332 .o_add_conn = client_import_add_conn,
3333 .o_del_conn = client_import_del_conn,
3334 .o_connect = client_connect_import,
3335 .o_disconnect = client_disconnect_export,
3336 .o_iocontrol = mdc_iocontrol,
3337 .o_set_info_async = mdc_set_info_async,
3338 .o_statfs = mdc_statfs,
3340 .o_unpin = mdc_unpin,
3341 .o_fid_init = client_fid_init,
3342 .o_fid_fini = client_fid_fini,
3343 .o_fid_alloc = mdc_fid_alloc,
3344 .o_import_event = mdc_import_event,
3345 .o_llog_init = mdc_llog_init,
3346 .o_llog_finish = mdc_llog_finish,
3347 .o_get_info = mdc_get_info,
3348 .o_process_config = mdc_process_config,
3349 .o_get_uuid = mdc_get_uuid,
3350 .o_quotactl = mdc_quotactl,
3351 .o_quotacheck = mdc_quotacheck
3354 struct md_ops mdc_md_ops = {
3355 .m_getstatus = mdc_getstatus,
3356 .m_null_inode = mdc_null_inode,
3357 .m_find_cbdata = mdc_find_cbdata,
3358 .m_close = mdc_close,
3359 .m_create = mdc_create,
3360 .m_done_writing = mdc_done_writing,
3361 .m_enqueue = mdc_enqueue,
3362 .m_getattr = mdc_getattr,
3363 .m_getattr_name = mdc_getattr_name,
3364 .m_intent_lock = mdc_intent_lock,
3366 .m_is_subdir = mdc_is_subdir,
3367 .m_rename = mdc_rename,
3368 .m_setattr = mdc_setattr,
3369 .m_setxattr = mdc_setxattr,
3370 .m_getxattr = mdc_getxattr,
3371 .m_fsync = mdc_fsync,
3372 .m_read_entry = mdc_read_entry,
3373 .m_unlink = mdc_unlink,
3374 .m_cancel_unused = mdc_cancel_unused,
3375 .m_init_ea_size = mdc_init_ea_size,
3376 .m_set_lock_data = mdc_set_lock_data,
3377 .m_lock_match = mdc_lock_match,
3378 .m_get_lustre_md = mdc_get_lustre_md,
3379 .m_free_lustre_md = mdc_free_lustre_md,
3380 .m_set_open_replay_data = mdc_set_open_replay_data,
3381 .m_clear_open_replay_data = mdc_clear_open_replay_data,
3382 .m_renew_capa = mdc_renew_capa,
3383 .m_unpack_capa = mdc_unpack_capa,
3384 .m_get_remote_perm = mdc_get_remote_perm,
3385 .m_intent_getattr_async = mdc_intent_getattr_async,
3386 .m_revalidate_lock = mdc_revalidate_lock
3389 int __init mdc_init(void)
3391 return class_register_type(&mdc_obd_ops, &mdc_md_ops, NULL,
3392 #ifndef HAVE_ONLY_PROCFS_SEQ
3395 LUSTRE_MDC_NAME, NULL);
3399 static void /*__exit*/ mdc_exit(void)
3401 class_unregister_type(LUSTRE_MDC_NAME);
3404 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3405 MODULE_DESCRIPTION("Lustre Metadata Client");
3406 MODULE_LICENSE("GPL");
3408 module_init(mdc_init);
3409 module_exit(mdc_exit);