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, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_MDC
39 #include <linux/module.h>
40 #include <linux/pagemap.h>
41 #include <linux/miscdevice.h>
42 #include <linux/init.h>
43 #include <linux/utsname.h>
44 #include <linux/kthread.h>
45 #include <linux/user_namespace.h>
46 #ifdef HAVE_UIDGID_HEADER
47 # include <linux/uidgid.h>
50 #include <lustre_acl.h>
51 #include <lustre_ioctl.h>
52 #include <obd_class.h>
53 #include <lustre_lmv.h>
54 #include <lustre_fid.h>
55 #include <lprocfs_status.h>
56 #include <lustre_param.h>
57 #include <lustre_log.h>
58 #include <lustre_kernelcomm.h>
59 #include <cl_object.h>
61 #include "mdc_internal.h"
63 #define REQUEST_MINOR 244
65 struct mdc_renew_capa_args {
66 struct obd_capa *ra_oc;
67 renew_capa_cb_t ra_cb;
70 static int mdc_cleanup(struct obd_device *obd);
72 static int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
73 const struct req_msg_field *field,
76 struct lustre_capa *capa;
80 /* swabbed already in mdc_enqueue */
81 capa = req_capsule_server_get(&req->rq_pill, field);
85 c = alloc_capa(CAPA_SITE_CLIENT);
87 CDEBUG(D_INFO, "alloc capa failed!\n");
96 static inline int mdc_queue_wait(struct ptlrpc_request *req)
98 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
101 /* obd_get_request_slot() ensures that this client has no more
102 * than cl_max_rpcs_in_flight RPCs simultaneously inf light
104 rc = obd_get_request_slot(cli);
108 rc = ptlrpc_queue_wait(req);
109 obd_put_request_slot(cli);
114 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
115 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
116 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
117 struct obd_capa **pc, int level, int msg_flags)
119 struct ptlrpc_request *req;
120 struct mdt_body *body;
124 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
125 LUSTRE_MDS_VERSION, MDS_GETSTATUS);
129 mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
130 lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
131 req->rq_send_state = level;
133 ptlrpc_request_set_replen(req);
135 rc = ptlrpc_queue_wait(req);
139 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
141 GOTO(out, rc = -EPROTO);
143 if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
144 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
149 *rootfid = body->mbo_fid1;
150 CDEBUG(D_NET, "root fid="DFID", last_committed="LPU64"\n",
151 PFID(rootfid), lustre_msg_get_last_committed(req->rq_repmsg));
154 ptlrpc_req_finished(req);
158 /* This should be mdc_get_info("rootfid") */
159 static int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
160 struct obd_capa **pc)
162 return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
167 * This function now is known to always saying that it will receive 4 buffers
168 * from server. Even for cases when acl_size and md_size is zero, RPC header
169 * will contain 4 fields and RPC itself will contain zero size fields. This is
170 * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
171 * and thus zero, it shrinks it, making zero size. The same story about
172 * md_size. And this is course of problem when client waits for smaller number
173 * of fields. This issue will be fixed later when client gets aware of RPC
176 static int mdc_getattr_common(struct obd_export *exp,
177 struct ptlrpc_request *req)
179 struct req_capsule *pill = &req->rq_pill;
180 struct mdt_body *body;
185 /* Request message already built. */
186 rc = ptlrpc_queue_wait(req);
190 /* sanity check for the reply */
191 body = req_capsule_server_get(pill, &RMF_MDT_BODY);
195 CDEBUG(D_NET, "mode: %o\n", body->mbo_mode);
197 mdc_update_max_ea_from_body(exp, body);
198 if (body->mbo_eadatasize != 0) {
199 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
200 body->mbo_eadatasize);
205 if (body->mbo_valid & OBD_MD_FLRMTPERM) {
206 struct mdt_remote_perm *perm;
208 LASSERT(client_is_remote(exp));
209 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
210 lustre_swab_mdt_remote_perm);
215 if (body->mbo_valid & OBD_MD_FLMDSCAPA) {
216 struct lustre_capa *capa;
217 capa = req_capsule_server_get(pill, &RMF_CAPA1);
225 static int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
226 struct ptlrpc_request **request)
228 struct ptlrpc_request *req;
232 /* Single MDS without an LMV case */
233 if (op_data->op_flags & MF_GET_MDT_IDX) {
238 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
242 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
244 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
246 ptlrpc_request_free(req);
250 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
251 op_data->op_valid, op_data->op_mode, -1, 0);
253 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
255 if (op_data->op_valid & OBD_MD_FLRMTPERM) {
256 LASSERT(client_is_remote(exp));
257 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
258 sizeof(struct mdt_remote_perm));
260 ptlrpc_request_set_replen(req);
262 rc = mdc_getattr_common(exp, req);
264 ptlrpc_req_finished(req);
270 static int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
271 struct ptlrpc_request **request)
273 struct ptlrpc_request *req;
278 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
279 &RQF_MDS_GETATTR_NAME);
283 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
284 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
285 op_data->op_namelen + 1);
287 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
289 ptlrpc_request_free(req);
293 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
294 op_data->op_valid, op_data->op_mode,
295 op_data->op_suppgids[0], 0);
297 if (op_data->op_name) {
298 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
299 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
300 op_data->op_namelen);
301 memcpy(name, op_data->op_name, op_data->op_namelen);
304 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
306 ptlrpc_request_set_replen(req);
308 rc = mdc_getattr_common(exp, req);
310 ptlrpc_req_finished(req);
316 static int mdc_xattr_common(struct obd_export *exp,const struct req_format *fmt,
317 const struct lu_fid *fid,
318 struct obd_capa *oc, int opcode, u64 valid,
319 const char *xattr_name, const char *input,
320 int input_size, int output_size, int flags,
321 __u32 suppgid, struct ptlrpc_request **request)
323 struct ptlrpc_request *req;
324 int xattr_namelen = 0;
330 req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
334 mdc_set_capa_size(req, &RMF_CAPA1, oc);
336 xattr_namelen = strlen(xattr_name) + 1;
337 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
342 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
346 /* Flush local XATTR locks to get rid of a possible cancel RPC */
347 if (opcode == MDS_REINT && fid_is_sane(fid) &&
348 exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
349 struct list_head cancels = LIST_HEAD_INIT(cancels);
352 /* Without that packing would fail */
354 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
357 count = mdc_resource_get_unused(exp, fid,
359 MDS_INODELOCK_XATTR);
361 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
363 ptlrpc_request_free(req);
367 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
369 ptlrpc_request_free(req);
374 if (opcode == MDS_REINT) {
375 struct mdt_rec_setxattr *rec;
377 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
378 sizeof(struct mdt_rec_reint));
379 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
380 rec->sx_opcode = REINT_SETXATTR;
381 rec->sx_fsuid = from_kuid(&init_user_ns, current_fsuid());
382 rec->sx_fsgid = from_kgid(&init_user_ns, current_fsgid());
383 rec->sx_cap = cfs_curproc_cap_pack();
384 rec->sx_suppgid1 = suppgid;
385 rec->sx_suppgid2 = -1;
387 rec->sx_valid = valid | OBD_MD_FLCTIME;
388 rec->sx_time = cfs_time_current_sec();
389 rec->sx_size = output_size;
390 rec->sx_flags = flags;
392 mdc_pack_capa(req, &RMF_CAPA1, oc);
394 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
398 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
399 memcpy(tmp, xattr_name, xattr_namelen);
402 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
403 memcpy(tmp, input, input_size);
406 if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
407 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
408 RCL_SERVER, output_size);
409 ptlrpc_request_set_replen(req);
412 if (opcode == MDS_REINT)
413 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
415 rc = ptlrpc_queue_wait(req);
417 if (opcode == MDS_REINT)
418 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
421 ptlrpc_req_finished(req);
427 static int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
428 struct obd_capa *oc, u64 valid,
429 const char *xattr_name,
430 const char *input, int input_size, int output_size,
431 int flags, __u32 suppgid,
432 struct ptlrpc_request **request)
434 return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
435 fid, oc, MDS_REINT, valid, xattr_name,
436 input, input_size, output_size, flags,
440 static int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
441 struct obd_capa *oc, u64 valid,
442 const char *xattr_name,
443 const char *input, int input_size, int output_size,
444 int flags, struct ptlrpc_request **request)
446 return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
447 fid, oc, MDS_GETXATTR, valid, xattr_name,
448 input, input_size, output_size, flags,
452 #ifdef CONFIG_FS_POSIX_ACL
453 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
455 struct req_capsule *pill = &req->rq_pill;
456 struct mdt_body *body = md->body;
457 struct posix_acl *acl;
462 if (!body->mbo_aclsize)
465 buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->mbo_aclsize);
470 acl = posix_acl_from_xattr(&init_user_ns, buf, body->mbo_aclsize);
475 CERROR("convert xattr to acl: %d\n", rc);
479 rc = posix_acl_valid(acl);
481 CERROR("validate acl: %d\n", rc);
482 posix_acl_release(acl);
490 #define mdc_unpack_acl(req, md) 0
493 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
494 struct obd_export *dt_exp, struct obd_export *md_exp,
495 struct lustre_md *md)
497 struct req_capsule *pill = &req->rq_pill;
502 memset(md, 0, sizeof(*md));
504 md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
505 LASSERT(md->body != NULL);
507 if (md->body->mbo_valid & OBD_MD_FLEASIZE) {
509 struct lov_mds_md *lmm;
511 if (!S_ISREG(md->body->mbo_mode)) {
512 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, should be a "
513 "regular file, but is not\n");
514 GOTO(out, rc = -EPROTO);
517 if (md->body->mbo_eadatasize == 0) {
518 CDEBUG(D_INFO, "OBD_MD_FLEASIZE set, "
519 "but eadatasize 0\n");
520 GOTO(out, rc = -EPROTO);
523 lmmsize = md->body->mbo_eadatasize;
524 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
526 GOTO(out, rc = -EPROTO);
528 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
532 if (rc < (typeof(rc))sizeof(*md->lsm)) {
533 CDEBUG(D_INFO, "lsm size too small: "
534 "rc < sizeof (*md->lsm) (%d < %d)\n",
535 rc, (int)sizeof(*md->lsm));
536 GOTO(out, rc = -EPROTO);
539 } else if (md->body->mbo_valid & OBD_MD_FLDIREA) {
541 struct lov_mds_md *lmv;
543 if (!S_ISDIR(md->body->mbo_mode)) {
544 CDEBUG(D_INFO, "OBD_MD_FLDIREA set, should be a "
545 "directory, but is not\n");
546 GOTO(out, rc = -EPROTO);
549 if (md->body->mbo_eadatasize == 0) {
550 CDEBUG(D_INFO, "OBD_MD_FLDIREA is set, "
551 "but eadatasize 0\n");
555 if (md->body->mbo_valid & OBD_MD_MEA) {
556 lmvsize = md->body->mbo_eadatasize;
557 lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
560 GOTO(out, rc = -EPROTO);
562 rc = obd_unpackmd(md_exp, (void *)&md->lmv, lmv,
567 if (rc < (typeof(rc))sizeof(*md->lmv)) {
568 CDEBUG(D_INFO, "size too small: "
569 "rc < sizeof(*md->lmv) (%d < %d)\n",
570 rc, (int)sizeof(*md->lmv));
571 GOTO(out, rc = -EPROTO);
577 if (md->body->mbo_valid & OBD_MD_FLRMTPERM) {
578 /* remote permission */
579 LASSERT(client_is_remote(exp));
580 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
581 lustre_swab_mdt_remote_perm);
582 if (!md->remote_perm)
583 GOTO(out, rc = -EPROTO);
584 } else if (md->body->mbo_valid & OBD_MD_FLACL) {
585 /* for ACL, it's possible that FLACL is set but aclsize is zero.
586 * only when aclsize != 0 there's an actual segment for ACL
589 if (md->body->mbo_aclsize) {
590 rc = mdc_unpack_acl(req, md);
593 #ifdef CONFIG_FS_POSIX_ACL
595 md->posix_acl = NULL;
599 if (md->body->mbo_valid & OBD_MD_FLMDSCAPA) {
600 struct obd_capa *oc = NULL;
602 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
608 if (md->body->mbo_valid & OBD_MD_FLOSSCAPA) {
609 struct obd_capa *oc = NULL;
611 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
621 capa_put(md->oss_capa);
625 capa_put(md->mds_capa);
628 #ifdef CONFIG_FS_POSIX_ACL
629 posix_acl_release(md->posix_acl);
632 obd_free_memmd(dt_exp, &md->lsm);
637 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
643 void mdc_replay_open(struct ptlrpc_request *req)
645 struct md_open_data *mod = req->rq_cb_data;
646 struct ptlrpc_request *close_req;
647 struct obd_client_handle *och;
648 struct lustre_handle old;
649 struct mdt_body *body;
653 DEBUG_REQ(D_ERROR, req,
654 "Can't properly replay without open data.");
659 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
660 LASSERT(body != NULL);
664 struct lustre_handle *file_fh;
666 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
668 file_fh = &och->och_fh;
669 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
670 file_fh->cookie, body->mbo_handle.cookie);
672 *file_fh = body->mbo_handle;
674 close_req = mod->mod_close_req;
675 if (close_req != NULL) {
676 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
677 struct mdt_ioepoch *epoch;
679 LASSERT(opc == MDS_CLOSE);
680 epoch = req_capsule_client_get(&close_req->rq_pill,
685 LASSERT(!memcmp(&old, &epoch->mio_handle, sizeof(old)));
687 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
688 epoch->mio_handle = body->mbo_handle;
693 void mdc_commit_open(struct ptlrpc_request *req)
695 struct md_open_data *mod = req->rq_cb_data;
700 * No need to touch md_open_data::mod_och, it holds a reference on
701 * \var mod and will zero references to each other, \var mod will be
702 * freed after that when md_open_data::mod_och will put the reference.
706 * Do not let open request to disappear as it still may be needed
707 * for close rpc to happen (it may happen on evict only, otherwise
708 * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
709 * called), just mark this rpc as committed to distinguish these 2
710 * cases, see mdc_close() for details. The open request reference will
711 * be put along with freeing \var mod.
713 ptlrpc_request_addref(req);
714 spin_lock(&req->rq_lock);
715 req->rq_committed = 1;
716 spin_unlock(&req->rq_lock);
717 req->rq_cb_data = NULL;
721 int mdc_set_open_replay_data(struct obd_export *exp,
722 struct obd_client_handle *och,
723 struct lookup_intent *it)
725 struct md_open_data *mod;
726 struct mdt_rec_create *rec;
727 struct mdt_body *body;
728 struct ptlrpc_request *open_req = it->d.lustre.it_data;
729 struct obd_import *imp = open_req->rq_import;
732 if (!open_req->rq_replay)
735 rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
736 body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
737 LASSERT(rec != NULL);
738 /* Incoming message in my byte order (it's been swabbed). */
739 /* Outgoing messages always in my byte order. */
740 LASSERT(body != NULL);
742 /* Only if the import is replayable, we set replay_open data */
743 if (och && imp->imp_replayable) {
744 mod = obd_mod_alloc();
746 DEBUG_REQ(D_ERROR, open_req,
747 "Can't allocate md_open_data");
752 * Take a reference on \var mod, to be freed on mdc_close().
753 * It protects \var mod from being freed on eviction (commit
754 * callback is called despite rq_replay flag).
755 * Another reference for \var och.
760 spin_lock(&open_req->rq_lock);
763 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
764 it_disposition(it, DISP_OPEN_STRIPE);
765 mod->mod_open_req = open_req;
766 open_req->rq_cb_data = mod;
767 open_req->rq_commit_cb = mdc_commit_open;
768 spin_unlock(&open_req->rq_lock);
771 rec->cr_fid2 = body->mbo_fid1;
772 rec->cr_ioepoch = body->mbo_ioepoch;
773 rec->cr_old_handle.cookie = body->mbo_handle.cookie;
774 open_req->rq_replay_cb = mdc_replay_open;
775 if (!fid_is_sane(&body->mbo_fid1)) {
776 DEBUG_REQ(D_ERROR, open_req, "Saving replay request with "
781 DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
785 static void mdc_free_open(struct md_open_data *mod)
789 if (mod->mod_is_create == 0 &&
790 imp_connect_disp_stripe(mod->mod_open_req->rq_import))
793 LASSERT(mod->mod_open_req->rq_replay == 0);
795 DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
797 ptlrpc_request_committed(mod->mod_open_req, committed);
798 if (mod->mod_close_req)
799 ptlrpc_request_committed(mod->mod_close_req, committed);
802 int mdc_clear_open_replay_data(struct obd_export *exp,
803 struct obd_client_handle *och)
805 struct md_open_data *mod = och->och_mod;
809 * It is possible to not have \var mod in a case of eviction between
810 * lookup and ll_file_open().
815 LASSERT(mod != LP_POISON);
816 LASSERT(mod->mod_open_req != NULL);
826 static int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
827 struct md_open_data *mod, struct ptlrpc_request **request)
829 struct obd_device *obd = class_exp2obd(exp);
830 struct ptlrpc_request *req;
831 struct req_format *req_fmt;
836 req_fmt = &RQF_MDS_CLOSE;
837 if (op_data->op_bias & MDS_HSM_RELEASE) {
838 req_fmt = &RQF_MDS_RELEASE_CLOSE;
840 /* allocate a FID for volatile file */
841 rc = mdc_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
843 CERROR("%s: "DFID" failed to allocate FID: %d\n",
844 obd->obd_name, PFID(&op_data->op_fid1), rc);
845 /* save the errcode and proceed to close */
851 req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
855 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
857 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
859 ptlrpc_request_free(req);
863 /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
864 * portal whose threads are not taking any DLM locks and are therefore
865 * always progressing */
866 req->rq_request_portal = MDS_READPAGE_PORTAL;
867 ptlrpc_at_set_req_timeout(req);
869 /* Ensure that this close's handle is fixed up during replay. */
870 if (likely(mod != NULL)) {
871 LASSERTF(mod->mod_open_req != NULL &&
872 mod->mod_open_req->rq_type != LI_POISON,
873 "POISONED open %p!\n", mod->mod_open_req);
875 mod->mod_close_req = req;
877 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
878 /* We no longer want to preserve this open for replay even
879 * though the open was committed. b=3632, b=3633 */
880 spin_lock(&mod->mod_open_req->rq_lock);
881 mod->mod_open_req->rq_replay = 0;
882 spin_unlock(&mod->mod_open_req->rq_lock);
884 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
887 mdc_close_pack(req, op_data);
889 req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
890 obd->u.cli.cl_default_mds_easize);
892 ptlrpc_request_set_replen(req);
894 mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
895 rc = ptlrpc_queue_wait(req);
896 mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
898 if (req->rq_repmsg == NULL) {
899 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
902 rc = req->rq_status ?: -EIO;
903 } else if (rc == 0 || rc == -EAGAIN) {
904 struct mdt_body *body;
906 rc = lustre_msg_get_status(req->rq_repmsg);
907 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
908 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
913 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
916 } else if (rc == -ESTALE) {
918 * it can be allowed error after 3633 if open was committed and
919 * server failed before close was sent. Let's check if mod
920 * exists and return no error in that case
923 DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
924 LASSERT(mod->mod_open_req != NULL);
925 if (mod->mod_open_req->rq_committed)
932 mod->mod_close_req = NULL;
933 /* Since now, mod is accessed through open_req only,
934 * thus close req does not keep a reference on mod anymore. */
939 RETURN(rc < 0 ? rc : saved_rc);
942 static int mdc_getpage(struct obd_export *exp, const struct lu_fid *fid,
943 __u64 offset, struct obd_capa *oc,
944 struct page **pages, int npages,
945 struct ptlrpc_request **request)
947 struct ptlrpc_request *req;
948 struct ptlrpc_bulk_desc *desc;
950 wait_queue_head_t waitq;
952 struct l_wait_info lwi;
957 init_waitqueue_head(&waitq);
960 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
964 mdc_set_capa_size(req, &RMF_CAPA1, oc);
966 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
968 ptlrpc_request_free(req);
972 req->rq_request_portal = MDS_READPAGE_PORTAL;
973 ptlrpc_at_set_req_timeout(req);
975 desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
978 ptlrpc_request_free(req);
982 /* NB req now owns desc and will free it when it gets freed */
983 for (i = 0; i < npages; i++)
984 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
986 mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE * npages, fid, oc);
988 ptlrpc_request_set_replen(req);
989 rc = ptlrpc_queue_wait(req);
991 ptlrpc_req_finished(req);
992 if (rc != -ETIMEDOUT)
996 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
997 CERROR("%s: too many resend retries: rc = %d\n",
998 exp->exp_obd->obd_name, -EIO);
1001 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends), NULL, NULL,
1003 l_wait_event(waitq, 0, &lwi);
1008 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1009 req->rq_bulk->bd_nob_transferred);
1011 ptlrpc_req_finished(req);
1015 if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1016 CERROR("%s: unexpected bytes transferred: %d (%ld expected)\n",
1017 exp->exp_obd->obd_name, req->rq_bulk->bd_nob_transferred,
1018 PAGE_CACHE_SIZE * npages);
1019 ptlrpc_req_finished(req);
1027 static void mdc_release_page(struct page *page, int remove)
1031 if (likely(page->mapping != NULL))
1032 truncate_complete_page(page->mapping, page);
1035 page_cache_release(page);
1038 static struct page *mdc_page_locate(struct address_space *mapping, __u64 *hash,
1039 __u64 *start, __u64 *end, int hash64)
1042 * Complement of hash is used as an index so that
1043 * radix_tree_gang_lookup() can be used to find a page with starting
1044 * hash _smaller_ than one we are looking for.
1046 unsigned long offset = hash_x_index(*hash, hash64);
1050 spin_lock_irq(&mapping->tree_lock);
1051 found = radix_tree_gang_lookup(&mapping->page_tree,
1052 (void **)&page, offset, 1);
1053 if (found > 0 && !radix_tree_exceptional_entry(page)) {
1054 struct lu_dirpage *dp;
1056 page_cache_get(page);
1057 spin_unlock_irq(&mapping->tree_lock);
1059 * In contrast to find_lock_page() we are sure that directory
1060 * page cannot be truncated (while DLM lock is held) and,
1061 * hence, can avoid restart.
1063 * In fact, page cannot be locked here at all, because
1064 * mdc_read_page_remote does synchronous io.
1066 wait_on_page_locked(page);
1067 if (PageUptodate(page)) {
1069 if (BITS_PER_LONG == 32 && hash64) {
1070 *start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1071 *end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1072 *hash = *hash >> 32;
1074 *start = le64_to_cpu(dp->ldp_hash_start);
1075 *end = le64_to_cpu(dp->ldp_hash_end);
1077 if (unlikely(*start == 1 && *hash == 0))
1080 LASSERTF(*start <= *hash, "start = "LPX64
1081 ",end = "LPX64",hash = "LPX64"\n",
1082 *start, *end, *hash);
1083 CDEBUG(D_VFSTRACE, "offset %lx ["LPX64" "LPX64"],"
1084 " hash "LPX64"\n", offset, *start, *end, *hash);
1087 mdc_release_page(page, 0);
1089 } else if (*end != *start && *hash == *end) {
1091 * upon hash collision, remove this page,
1092 * otherwise put page reference, and
1093 * mdc_read_page_remote() will issue RPC to
1094 * fetch the page we want.
1097 mdc_release_page(page,
1098 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1102 page_cache_release(page);
1103 page = ERR_PTR(-EIO);
1106 spin_unlock_irq(&mapping->tree_lock);
1113 * Adjust a set of pages, each page containing an array of lu_dirpages,
1114 * so that each page can be used as a single logical lu_dirpage.
1116 * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1117 * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1118 * struct lu_dirent. It has size up to LU_PAGE_SIZE. The ldp_hash_end
1119 * value is used as a cookie to request the next lu_dirpage in a
1120 * directory listing that spans multiple pages (two in this example):
1123 * .|--------v------- -----.
1124 * |s|e|f|p|ent|ent| ... |ent|
1125 * '--|-------------- -----' Each PAGE contains a single
1126 * '------. lu_dirpage.
1127 * .---------v------- -----.
1128 * |s|e|f|p|ent| 0 | ... | 0 |
1129 * '----------------- -----'
1131 * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
1132 * larger than LU_PAGE_SIZE, a single host page may contain multiple
1133 * lu_dirpages. After reading the lu_dirpages from the MDS, the
1134 * ldp_hash_end of the first lu_dirpage refers to the one immediately
1135 * after it in the same PAGE (arrows simplified for brevity, but
1136 * in general e0==s1, e1==s2, etc.):
1138 * .-------------------- -----.
1139 * |s0|e0|f0|p|ent|ent| ... |ent|
1140 * |---v---------------- -----|
1141 * |s1|e1|f1|p|ent|ent| ... |ent|
1142 * |---v---------------- -----| Here, each PAGE contains
1143 * ... multiple lu_dirpages.
1144 * |---v---------------- -----|
1145 * |s'|e'|f'|p|ent|ent| ... |ent|
1146 * '---|---------------- -----'
1148 * .----------------------------.
1151 * This structure is transformed into a single logical lu_dirpage as follows:
1153 * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1154 * labeled 'next PAGE'.
1156 * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1157 * a hash collision with the next page exists.
1159 * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1160 * to the first entry of the next lu_dirpage.
1162 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
1163 static void mdc_adjust_dirpages(struct page **pages, int cfs_pgs, int lu_pgs)
1167 for (i = 0; i < cfs_pgs; i++) {
1168 struct lu_dirpage *dp = kmap(pages[i]);
1169 struct lu_dirpage *first = dp;
1170 struct lu_dirent *end_dirent = NULL;
1171 struct lu_dirent *ent;
1172 __u64 hash_end = le64_to_cpu(dp->ldp_hash_end);
1173 __u32 flags = le32_to_cpu(dp->ldp_flags);
1175 while (--lu_pgs > 0) {
1176 ent = lu_dirent_start(dp);
1177 for (end_dirent = ent; ent != NULL;
1178 end_dirent = ent, ent = lu_dirent_next(ent));
1180 /* Advance dp to next lu_dirpage. */
1181 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1183 /* Check if we've reached the end of the PAGE. */
1184 if (!((unsigned long)dp & ~PAGE_MASK))
1187 /* Save the hash and flags of this lu_dirpage. */
1188 hash_end = le64_to_cpu(dp->ldp_hash_end);
1189 flags = le32_to_cpu(dp->ldp_flags);
1191 /* Check if lu_dirpage contains no entries. */
1192 if (end_dirent == NULL)
1195 /* Enlarge the end entry lde_reclen from 0 to
1196 * first entry of next lu_dirpage. */
1197 LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
1198 end_dirent->lde_reclen =
1199 cpu_to_le16((char *)(dp->ldp_entries) -
1200 (char *)end_dirent);
1203 first->ldp_hash_end = hash_end;
1204 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
1205 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
1209 LASSERTF(lu_pgs == 0, "left = %d\n", lu_pgs);
1212 #define mdc_adjust_dirpages(pages, cfs_pgs, lu_pgs) do {} while (0)
1213 #endif /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
1215 /* parameters for readdir page */
1216 struct readpage_param {
1217 struct md_op_data *rp_mod;
1220 struct obd_export *rp_exp;
1221 struct md_callback *rp_cb;
1224 #ifndef HAVE_DELETE_FROM_PAGE_CACHE
1225 static inline void delete_from_page_cache(struct page *page)
1227 remove_from_page_cache(page);
1228 page_cache_release(page);
1233 * Read pages from server.
1235 * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
1236 * a header lu_dirpage which describes the start/end hash, and whether this
1237 * page is empty (contains no dir entry) or hash collide with next page.
1238 * After client receives reply, several pages will be integrated into dir page
1239 * in PAGE_SIZE (if PAGE_SIZE greater than LU_PAGE_SIZE), and the
1240 * lu_dirpage for this integrated page will be adjusted.
1242 static int mdc_read_page_remote(void *data, struct page *page0)
1244 struct readpage_param *rp = data;
1245 struct page **page_pool;
1247 struct lu_dirpage *dp;
1248 int rd_pgs = 0; /* number of pages read actually */
1250 struct md_op_data *op_data = rp->rp_mod;
1251 struct ptlrpc_request *req;
1252 int max_pages = op_data->op_max_pages;
1253 struct inode *inode;
1259 LASSERT(max_pages > 0 && max_pages <= PTLRPC_MAX_BRW_PAGES);
1260 inode = op_data->op_data;
1261 fid = &op_data->op_fid1;
1262 LASSERT(inode != NULL);
1264 OBD_ALLOC(page_pool, sizeof(page_pool[0]) * max_pages);
1265 if (page_pool != NULL) {
1266 page_pool[0] = page0;
1272 for (npages = 1; npages < max_pages; npages++) {
1273 page = page_cache_alloc_cold(inode->i_mapping);
1276 page_pool[npages] = page;
1279 rc = mdc_getpage(rp->rp_exp, fid, rp->rp_off, op_data->op_capa1,
1280 page_pool, npages, &req);
1282 /* page0 is special, which was added into page cache early */
1283 delete_from_page_cache(page0);
1287 rd_pgs = (req->rq_bulk->bd_nob_transferred +
1288 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1289 lu_pgs = req->rq_bulk->bd_nob_transferred >>
1291 LASSERT(!(req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
1293 CDEBUG(D_INODE, "read %d(%d) pages\n", rd_pgs, lu_pgs);
1295 mdc_adjust_dirpages(page_pool, rd_pgs, lu_pgs);
1297 SetPageUptodate(page0);
1301 ptlrpc_req_finished(req);
1302 CDEBUG(D_CACHE, "read %d/%d pages\n", rd_pgs, npages);
1303 for (i = 1; i < npages; i++) {
1304 unsigned long offset;
1308 page = page_pool[i];
1310 if (rc < 0 || i >= rd_pgs) {
1311 page_cache_release(page);
1315 SetPageUptodate(page);
1318 hash = le64_to_cpu(dp->ldp_hash_start);
1321 offset = hash_x_index(hash, rp->rp_hash64);
1323 prefetchw(&page->flags);
1324 ret = add_to_page_cache_lru(page, inode->i_mapping, offset,
1329 CDEBUG(D_VFSTRACE, "page %lu add to page cache failed:"
1330 " rc = %d\n", offset, ret);
1331 page_cache_release(page);
1334 if (page_pool != &page0)
1335 OBD_FREE(page_pool, sizeof(page_pool[0]) * max_pages);
1341 * Read dir page from cache first, if it can not find it, read it from
1342 * server and add into the cache.
1344 * \param[in] exp MDC export
1345 * \param[in] op_data client MD stack parameters, transfering parameters
1346 * between different layers on client MD stack.
1347 * \param[in] cb_op callback required for ldlm lock enqueue during
1349 * \param[in] hash_offset the hash offset of the page to be read
1350 * \param[in] ppage the page to be read
1352 * retval = 0 get the page successfully
1353 * errno(<0) get the page failed
1355 static int mdc_read_page(struct obd_export *exp, struct md_op_data *op_data,
1356 struct md_callback *cb_op, __u64 hash_offset,
1357 struct page **ppage)
1359 struct lookup_intent it = { .it_op = IT_READDIR };
1361 struct inode *dir = op_data->op_data;
1362 struct address_space *mapping;
1363 struct lu_dirpage *dp;
1366 struct lustre_handle lockh;
1367 struct ptlrpc_request *enq_req = NULL;
1368 struct readpage_param rp_param;
1375 LASSERT(dir != NULL);
1376 mapping = dir->i_mapping;
1378 rc = mdc_intent_lock(exp, op_data, &it, &enq_req,
1379 cb_op->md_blocking_ast, 0);
1380 if (enq_req != NULL)
1381 ptlrpc_req_finished(enq_req);
1384 CERROR("%s: "DFID" lock enqueue fails: rc = %d\n",
1385 exp->exp_obd->obd_name, PFID(&op_data->op_fid1), rc);
1390 mdc_set_lock_data(exp, &it.d.lustre.it_lock_handle, dir, NULL);
1392 rp_param.rp_off = hash_offset;
1393 rp_param.rp_hash64 = op_data->op_cli_flags & CLI_HASH64;
1394 page = mdc_page_locate(mapping, &rp_param.rp_off, &start, &end,
1395 rp_param.rp_hash64);
1397 CERROR("%s: dir page locate: "DFID" at "LPU64": rc %ld\n",
1398 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1399 rp_param.rp_off, PTR_ERR(page));
1400 GOTO(out_unlock, rc = PTR_ERR(page));
1401 } else if (page != NULL) {
1403 * XXX nikita: not entirely correct handling of a corner case:
1404 * suppose hash chain of entries with hash value HASH crosses
1405 * border between pages P0 and P1. First both P0 and P1 are
1406 * cached, seekdir() is called for some entry from the P0 part
1407 * of the chain. Later P0 goes out of cache. telldir(HASH)
1408 * happens and finds P1, as it starts with matching hash
1409 * value. Remaining entries from P0 part of the chain are
1410 * skipped. (Is that really a bug?)
1412 * Possible solutions: 0. don't cache P1 is such case, handle
1413 * it as an "overflow" page. 1. invalidate all pages at
1414 * once. 2. use HASH|1 as an index for P1.
1416 GOTO(hash_collision, page);
1419 rp_param.rp_exp = exp;
1420 rp_param.rp_mod = op_data;
1421 page = read_cache_page(mapping,
1422 hash_x_index(rp_param.rp_off,
1423 rp_param.rp_hash64),
1424 mdc_read_page_remote, &rp_param);
1426 CDEBUG(D_INFO, "%s: read cache page: "DFID" at "LPU64": %ld\n",
1427 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1428 rp_param.rp_off, PTR_ERR(page));
1429 GOTO(out_unlock, rc = PTR_ERR(page));
1432 wait_on_page_locked(page);
1434 if (!PageUptodate(page)) {
1435 CERROR("%s: page not updated: "DFID" at "LPU64": rc %d\n",
1436 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1437 rp_param.rp_off, -5);
1440 if (!PageChecked(page))
1441 SetPageChecked(page);
1442 if (PageError(page)) {
1443 CERROR("%s: page error: "DFID" at "LPU64": rc %d\n",
1444 exp->exp_obd->obd_name, PFID(&op_data->op_fid1),
1445 rp_param.rp_off, -5);
1450 dp = page_address(page);
1451 if (BITS_PER_LONG == 32 && rp_param.rp_hash64) {
1452 start = le64_to_cpu(dp->ldp_hash_start) >> 32;
1453 end = le64_to_cpu(dp->ldp_hash_end) >> 32;
1454 rp_param.rp_off = hash_offset >> 32;
1456 start = le64_to_cpu(dp->ldp_hash_start);
1457 end = le64_to_cpu(dp->ldp_hash_end);
1458 rp_param.rp_off = hash_offset;
1461 LASSERT(start == rp_param.rp_off);
1462 CWARN("Page-wide hash collision: %#lx\n", (unsigned long)end);
1463 #if BITS_PER_LONG == 32
1464 CWARN("Real page-wide hash collision at ["LPU64" "LPU64"] with "
1465 "hash "LPU64"\n", le64_to_cpu(dp->ldp_hash_start),
1466 le64_to_cpu(dp->ldp_hash_end), hash_offset);
1470 * Fetch whole overflow chain...
1478 lockh.cookie = it.d.lustre.it_lock_handle;
1479 ldlm_lock_decref(&lockh, it.d.lustre.it_lock_mode);
1480 it.d.lustre.it_lock_handle = 0;
1484 mdc_release_page(page, 1);
1490 static int mdc_statfs(const struct lu_env *env,
1491 struct obd_export *exp, struct obd_statfs *osfs,
1492 __u64 max_age, __u32 flags)
1494 struct obd_device *obd = class_exp2obd(exp);
1495 struct ptlrpc_request *req;
1496 struct obd_statfs *msfs;
1497 struct obd_import *imp = NULL;
1502 * Since the request might also come from lprocfs, so we need
1503 * sync this with client_disconnect_export Bug15684
1505 down_read(&obd->u.cli.cl_sem);
1506 if (obd->u.cli.cl_import)
1507 imp = class_import_get(obd->u.cli.cl_import);
1508 up_read(&obd->u.cli.cl_sem);
1512 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1513 LUSTRE_MDS_VERSION, MDS_STATFS);
1515 GOTO(output, rc = -ENOMEM);
1517 ptlrpc_request_set_replen(req);
1519 if (flags & OBD_STATFS_NODELAY) {
1520 /* procfs requests not want stay in wait for avoid deadlock */
1521 req->rq_no_resend = 1;
1522 req->rq_no_delay = 1;
1525 rc = ptlrpc_queue_wait(req);
1527 /* check connection error first */
1528 if (imp->imp_connect_error)
1529 rc = imp->imp_connect_error;
1533 msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1535 GOTO(out, rc = -EPROTO);
1540 ptlrpc_req_finished(req);
1542 class_import_put(imp);
1546 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1548 __u32 keylen, vallen;
1552 if (gf->gf_pathlen > PATH_MAX)
1553 RETURN(-ENAMETOOLONG);
1554 if (gf->gf_pathlen < 2)
1557 /* Key is KEY_FID2PATH + getinfo_fid2path description */
1558 keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1559 OBD_ALLOC(key, keylen);
1562 memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1563 memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1565 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
1566 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1568 if (!fid_is_sane(&gf->gf_fid))
1569 GOTO(out, rc = -EINVAL);
1571 /* Val is struct getinfo_fid2path result plus path */
1572 vallen = sizeof(*gf) + gf->gf_pathlen;
1574 rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf);
1575 if (rc != 0 && rc != -EREMOTE)
1578 if (vallen <= sizeof(*gf))
1579 GOTO(out, rc = -EPROTO);
1580 else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1581 GOTO(out, rc = -EOVERFLOW);
1583 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n%s\n",
1584 PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1587 OBD_FREE(key, keylen);
1591 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1592 struct hsm_progress_kernel *hpk)
1594 struct obd_import *imp = class_exp2cliimp(exp);
1595 struct hsm_progress_kernel *req_hpk;
1596 struct ptlrpc_request *req;
1600 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1601 LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1603 GOTO(out, rc = -ENOMEM);
1605 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1607 /* Copy hsm_progress struct */
1608 req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1609 if (req_hpk == NULL)
1610 GOTO(out, rc = -EPROTO);
1613 req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1615 ptlrpc_request_set_replen(req);
1617 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1618 rc = ptlrpc_queue_wait(req);
1619 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1623 ptlrpc_req_finished(req);
1627 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1629 __u32 *archive_mask;
1630 struct ptlrpc_request *req;
1634 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1636 MDS_HSM_CT_REGISTER);
1638 GOTO(out, rc = -ENOMEM);
1640 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1642 /* Copy hsm_progress struct */
1643 archive_mask = req_capsule_client_get(&req->rq_pill,
1644 &RMF_MDS_HSM_ARCHIVE);
1645 if (archive_mask == NULL)
1646 GOTO(out, rc = -EPROTO);
1648 *archive_mask = archives;
1650 ptlrpc_request_set_replen(req);
1652 rc = mdc_queue_wait(req);
1655 ptlrpc_req_finished(req);
1659 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1660 struct md_op_data *op_data)
1662 struct hsm_current_action *hca = op_data->op_data;
1663 struct hsm_current_action *req_hca;
1664 struct ptlrpc_request *req;
1668 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1669 &RQF_MDS_HSM_ACTION);
1673 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1675 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1677 ptlrpc_request_free(req);
1681 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1682 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1684 ptlrpc_request_set_replen(req);
1686 rc = mdc_queue_wait(req);
1690 req_hca = req_capsule_server_get(&req->rq_pill,
1691 &RMF_MDS_HSM_CURRENT_ACTION);
1692 if (req_hca == NULL)
1693 GOTO(out, rc = -EPROTO);
1699 ptlrpc_req_finished(req);
1703 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1705 struct ptlrpc_request *req;
1709 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1711 MDS_HSM_CT_UNREGISTER);
1713 GOTO(out, rc = -ENOMEM);
1715 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1717 ptlrpc_request_set_replen(req);
1719 rc = mdc_queue_wait(req);
1722 ptlrpc_req_finished(req);
1726 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1727 struct md_op_data *op_data)
1729 struct hsm_user_state *hus = op_data->op_data;
1730 struct hsm_user_state *req_hus;
1731 struct ptlrpc_request *req;
1735 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1736 &RQF_MDS_HSM_STATE_GET);
1740 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1742 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1744 ptlrpc_request_free(req);
1748 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1749 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1751 ptlrpc_request_set_replen(req);
1753 rc = mdc_queue_wait(req);
1757 req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1758 if (req_hus == NULL)
1759 GOTO(out, rc = -EPROTO);
1765 ptlrpc_req_finished(req);
1769 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1770 struct md_op_data *op_data)
1772 struct hsm_state_set *hss = op_data->op_data;
1773 struct hsm_state_set *req_hss;
1774 struct ptlrpc_request *req;
1778 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1779 &RQF_MDS_HSM_STATE_SET);
1783 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1785 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1787 ptlrpc_request_free(req);
1791 mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1792 OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1795 req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1796 if (req_hss == NULL)
1797 GOTO(out, rc = -EPROTO);
1800 ptlrpc_request_set_replen(req);
1802 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1803 rc = ptlrpc_queue_wait(req);
1804 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1808 ptlrpc_req_finished(req);
1812 static int mdc_ioc_hsm_request(struct obd_export *exp,
1813 struct hsm_user_request *hur)
1815 struct obd_import *imp = class_exp2cliimp(exp);
1816 struct ptlrpc_request *req;
1817 struct hsm_request *req_hr;
1818 struct hsm_user_item *req_hui;
1823 req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1825 GOTO(out, rc = -ENOMEM);
1827 req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1828 hur->hur_request.hr_itemcount
1829 * sizeof(struct hsm_user_item));
1830 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1831 hur->hur_request.hr_data_len);
1833 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1835 ptlrpc_request_free(req);
1839 mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, -1, 0);
1841 /* Copy hsm_request struct */
1842 req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1844 GOTO(out, rc = -EPROTO);
1845 *req_hr = hur->hur_request;
1847 /* Copy hsm_user_item structs */
1848 req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1849 if (req_hui == NULL)
1850 GOTO(out, rc = -EPROTO);
1851 memcpy(req_hui, hur->hur_user_item,
1852 hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1854 /* Copy opaque field */
1855 req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1856 if (req_opaque == NULL)
1857 GOTO(out, rc = -EPROTO);
1858 memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1860 ptlrpc_request_set_replen(req);
1862 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1863 rc = ptlrpc_queue_wait(req);
1864 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1869 ptlrpc_req_finished(req);
1873 static struct kuc_hdr *changelog_kuc_hdr(char *buf, size_t len, __u32 flags)
1875 struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1877 LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1879 lh->kuc_magic = KUC_MAGIC;
1880 lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1881 lh->kuc_flags = flags;
1882 lh->kuc_msgtype = CL_RECORD;
1883 lh->kuc_msglen = len;
1887 struct changelog_show {
1889 enum changelog_send_flag cs_flags;
1892 struct obd_device *cs_obd;
1895 static inline char *cs_obd_name(struct changelog_show *cs)
1897 return cs->cs_obd->obd_name;
1900 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1901 struct llog_rec_hdr *hdr, void *data)
1903 struct changelog_show *cs = data;
1904 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1910 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1912 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1913 cs_obd_name(cs), rec->cr_hdr.lrh_type,
1914 rec->cr.cr_type, rc);
1918 if (rec->cr.cr_index < cs->cs_startrec) {
1919 /* Skip entries earlier than what we are interested in */
1920 CDEBUG(D_HSM, "rec="LPU64" start="LPU64"\n",
1921 rec->cr.cr_index, cs->cs_startrec);
1925 CDEBUG(D_HSM, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID" %.*s\n",
1926 rec->cr.cr_index, rec->cr.cr_type,
1927 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1928 rec->cr.cr_flags & CLF_FLAGMASK,
1929 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1930 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1932 len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1934 /* Set up the message */
1935 lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1936 memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1938 rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1939 CDEBUG(D_HSM, "kucmsg fp %p len %zu rc %d\n", cs->cs_fp, len, rc);
1944 static int mdc_changelog_send_thread(void *csdata)
1946 struct changelog_show *cs = csdata;
1947 struct llog_ctxt *ctxt = NULL;
1948 struct llog_handle *llh = NULL;
1949 struct kuc_hdr *kuch;
1950 enum llog_flag flags = LLOG_F_IS_CAT;
1953 CDEBUG(D_HSM, "changelog to fp=%p start "LPU64"\n",
1954 cs->cs_fp, cs->cs_startrec);
1956 OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1957 if (cs->cs_buf == NULL)
1958 GOTO(out, rc = -ENOMEM);
1960 /* Set up the remote catalog handle */
1961 ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1963 GOTO(out, rc = -ENOENT);
1964 rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1967 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1968 cs_obd_name(cs), rc);
1972 if (cs->cs_flags & CHANGELOG_FLAG_JOBID)
1973 flags |= LLOG_F_EXT_JOBID;
1975 rc = llog_init_handle(NULL, llh, flags, NULL);
1977 CERROR("llog_init_handle failed %d\n", rc);
1981 rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1983 /* Send EOF no matter what our result */
1984 kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1985 kuch->kuc_msgtype = CL_EOF;
1986 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1991 llog_cat_close(NULL, llh);
1993 llog_ctxt_put(ctxt);
1995 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
2000 static int mdc_ioc_changelog_send(struct obd_device *obd,
2001 struct ioc_changelog *icc)
2003 struct changelog_show *cs;
2004 struct task_struct *task;
2007 /* Freed in mdc_changelog_send_thread */
2013 cs->cs_startrec = icc->icc_recno;
2014 /* matching fput in mdc_changelog_send_thread */
2015 cs->cs_fp = fget(icc->icc_id);
2016 cs->cs_flags = icc->icc_flags;
2019 * New thread because we should return to user app before
2020 * writing into our pipe
2022 task = kthread_run(mdc_changelog_send_thread, cs,
2023 "mdc_clg_send_thread");
2026 CERROR("%s: cannot start changelog thread: rc = %d\n",
2027 cs_obd_name(cs), rc);
2031 CDEBUG(D_HSM, "%s: started changelog thread\n",
2038 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2039 struct lustre_kernelcomm *lk);
2041 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2042 struct obd_quotactl *oqctl)
2044 struct ptlrpc_request *req;
2045 struct obd_quotactl *oqc;
2049 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2050 &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
2055 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2058 ptlrpc_request_set_replen(req);
2059 ptlrpc_at_set_req_timeout(req);
2060 req->rq_no_resend = 1;
2062 rc = ptlrpc_queue_wait(req);
2064 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
2066 if (req->rq_repmsg &&
2067 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2070 CERROR ("Can't unpack obd_quotactl\n");
2073 ptlrpc_req_finished(req);
2078 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2079 struct md_op_data *op_data)
2081 struct list_head cancels = LIST_HEAD_INIT(cancels);
2082 struct ptlrpc_request *req;
2084 struct mdc_swap_layouts *msl, *payload;
2087 msl = op_data->op_data;
2089 /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2090 * first thing it will do is to cancel the 2 layout
2091 * locks hold by this client.
2092 * So the client must cancel its layout locks on the 2 fids
2093 * with the request RPC to avoid extra RPC round trips
2095 count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2096 LCK_EX, MDS_INODELOCK_LAYOUT |
2097 MDS_INODELOCK_XATTR);
2098 count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2099 LCK_EX, MDS_INODELOCK_LAYOUT |
2100 MDS_INODELOCK_XATTR);
2102 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2103 &RQF_MDS_SWAP_LAYOUTS);
2105 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2109 mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2110 mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
2112 rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2114 ptlrpc_request_free(req);
2118 mdc_swap_layouts_pack(req, op_data);
2120 payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2125 ptlrpc_request_set_replen(req);
2127 rc = ptlrpc_queue_wait(req);
2133 ptlrpc_req_finished(req);
2137 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2138 void *karg, void __user *uarg)
2140 struct obd_device *obd = exp->exp_obd;
2141 struct obd_ioctl_data *data = karg;
2142 struct obd_import *imp = obd->u.cli.cl_import;
2146 if (!try_module_get(THIS_MODULE)) {
2147 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2148 module_name(THIS_MODULE));
2152 case OBD_IOC_CHANGELOG_SEND:
2153 rc = mdc_ioc_changelog_send(obd, karg);
2155 case OBD_IOC_CHANGELOG_CLEAR: {
2156 struct ioc_changelog *icc = karg;
2157 struct changelog_setinfo cs =
2158 {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
2159 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2160 KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2164 case OBD_IOC_FID2PATH:
2165 rc = mdc_ioc_fid2path(exp, karg);
2167 case LL_IOC_HSM_CT_START:
2168 rc = mdc_ioc_hsm_ct_start(exp, karg);
2169 /* ignore if it was already registered on this MDS. */
2173 case LL_IOC_HSM_PROGRESS:
2174 rc = mdc_ioc_hsm_progress(exp, karg);
2176 case LL_IOC_HSM_STATE_GET:
2177 rc = mdc_ioc_hsm_state_get(exp, karg);
2179 case LL_IOC_HSM_STATE_SET:
2180 rc = mdc_ioc_hsm_state_set(exp, karg);
2182 case LL_IOC_HSM_ACTION:
2183 rc = mdc_ioc_hsm_current_action(exp, karg);
2185 case LL_IOC_HSM_REQUEST:
2186 rc = mdc_ioc_hsm_request(exp, karg);
2188 case OBD_IOC_CLIENT_RECOVER:
2189 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2193 case IOC_OSC_SET_ACTIVE:
2194 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2196 case OBD_IOC_PING_TARGET:
2197 rc = ptlrpc_obd_ping(obd);
2200 * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2201 * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2202 * there'd be no LMV layer thus we might be called here. Eventually
2203 * this code should be removed.
2206 case IOC_OBD_STATFS: {
2207 struct obd_statfs stat_buf = {0};
2209 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2210 GOTO(out, rc = -ENODEV);
2213 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2214 min((int)data->ioc_plen2,
2215 (int)sizeof(struct obd_uuid))))
2216 GOTO(out, rc = -EFAULT);
2218 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2219 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2224 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2225 min((int) data->ioc_plen1,
2226 (int) sizeof(stat_buf))))
2227 GOTO(out, rc = -EFAULT);
2231 case OBD_IOC_QUOTACTL: {
2232 struct if_quotactl *qctl = karg;
2233 struct obd_quotactl *oqctl;
2235 OBD_ALLOC_PTR(oqctl);
2237 GOTO(out, rc = -ENOMEM);
2239 QCTL_COPY(oqctl, qctl);
2240 rc = obd_quotactl(exp, oqctl);
2242 QCTL_COPY(qctl, oqctl);
2243 qctl->qc_valid = QC_MDTIDX;
2244 qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2247 OBD_FREE_PTR(oqctl);
2250 case LL_IOC_GET_CONNECT_FLAGS:
2251 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2252 sizeof(*exp_connect_flags_ptr(exp))))
2253 GOTO(out, rc = -EFAULT);
2256 case LL_IOC_LOV_SWAP_LAYOUTS:
2257 rc = mdc_ioc_swap_layouts(exp, karg);
2260 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2261 GOTO(out, rc = -ENOTTY);
2264 module_put(THIS_MODULE);
2269 static int mdc_get_info_rpc(struct obd_export *exp,
2270 u32 keylen, void *key,
2271 u32 vallen, void *val)
2273 struct obd_import *imp = class_exp2cliimp(exp);
2274 struct ptlrpc_request *req;
2279 req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2283 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2284 RCL_CLIENT, keylen);
2285 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2286 RCL_CLIENT, sizeof(vallen));
2288 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2290 ptlrpc_request_free(req);
2294 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2295 memcpy(tmp, key, keylen);
2296 tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2297 memcpy(tmp, &vallen, sizeof(vallen));
2299 req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2300 RCL_SERVER, vallen);
2301 ptlrpc_request_set_replen(req);
2303 rc = ptlrpc_queue_wait(req);
2304 /* -EREMOTE means the get_info result is partial, and it needs to
2305 * continue on another MDT, see fid2path part in lmv_iocontrol */
2306 if (rc == 0 || rc == -EREMOTE) {
2307 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2308 memcpy(val, tmp, vallen);
2309 if (ptlrpc_rep_need_swab(req)) {
2310 if (KEY_IS(KEY_FID2PATH))
2311 lustre_swab_fid2path(val);
2314 ptlrpc_req_finished(req);
2319 static void lustre_swab_hai(struct hsm_action_item *h)
2321 __swab32s(&h->hai_len);
2322 __swab32s(&h->hai_action);
2323 lustre_swab_lu_fid(&h->hai_fid);
2324 lustre_swab_lu_fid(&h->hai_dfid);
2325 __swab64s(&h->hai_cookie);
2326 __swab64s(&h->hai_extent.offset);
2327 __swab64s(&h->hai_extent.length);
2328 __swab64s(&h->hai_gid);
2331 static void lustre_swab_hal(struct hsm_action_list *h)
2333 struct hsm_action_item *hai;
2336 __swab32s(&h->hal_version);
2337 __swab32s(&h->hal_count);
2338 __swab32s(&h->hal_archive_id);
2339 __swab64s(&h->hal_flags);
2341 for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2342 lustre_swab_hai(hai);
2345 static void lustre_swab_kuch(struct kuc_hdr *l)
2347 __swab16s(&l->kuc_magic);
2348 /* __u8 l->kuc_transport */
2349 __swab16s(&l->kuc_msgtype);
2350 __swab16s(&l->kuc_msglen);
2353 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2354 struct lustre_kernelcomm *lk)
2356 struct obd_import *imp = class_exp2cliimp(exp);
2357 __u32 archive = lk->lk_data;
2360 if (lk->lk_group != KUC_GRP_HSM) {
2361 CERROR("Bad copytool group %d\n", lk->lk_group);
2365 CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2366 lk->lk_uid, lk->lk_group, lk->lk_flags);
2368 if (lk->lk_flags & LK_FLG_STOP) {
2369 /* Unregister with the coordinator */
2370 rc = mdc_ioc_hsm_ct_unregister(imp);
2372 rc = mdc_ioc_hsm_ct_register(imp, archive);
2379 * Send a message to any listening copytools
2380 * @param val KUC message (kuc_hdr + hsm_action_list)
2381 * @param len total length of message
2383 static int mdc_hsm_copytool_send(size_t len, void *val)
2385 struct kuc_hdr *lh = (struct kuc_hdr *)val;
2386 struct hsm_action_list *hal = (struct hsm_action_list *)(lh + 1);
2390 if (len < sizeof(*lh) + sizeof(*hal)) {
2391 CERROR("Short HSM message %zu < %zu\n", len,
2392 sizeof(*lh) + sizeof(*hal));
2395 if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2396 lustre_swab_kuch(lh);
2397 lustre_swab_hal(hal);
2398 } else if (lh->kuc_magic != KUC_MAGIC) {
2399 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2403 CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2405 lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2406 lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2408 /* Broadcast to HSM listeners */
2409 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2415 * callback function passed to kuc for re-registering each HSM copytool
2416 * running on MDC, after MDT shutdown/recovery.
2417 * @param data copytool registration data
2418 * @param cb_arg callback argument (obd_import)
2420 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2422 struct kkuc_ct_data *kcd = data;
2423 struct obd_import *imp = (struct obd_import *)cb_arg;
2426 if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2429 if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2432 CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2433 imp->imp_obd->obd_name, kcd->kcd_archive);
2434 rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2436 /* ignore error if the copytool is already registered */
2437 return (rc == -EEXIST) ? 0 : rc;
2441 * Re-establish all kuc contexts with MDT
2442 * after MDT shutdown/recovery.
2444 static int mdc_kuc_reregister(struct obd_import *imp)
2446 /* re-register HSM agents */
2447 return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2451 static int mdc_set_info_async(const struct lu_env *env,
2452 struct obd_export *exp,
2453 u32 keylen, void *key,
2454 u32 vallen, void *val,
2455 struct ptlrpc_request_set *set)
2457 struct obd_import *imp = class_exp2cliimp(exp);
2461 if (KEY_IS(KEY_READ_ONLY)) {
2462 if (vallen != sizeof(int))
2465 spin_lock(&imp->imp_lock);
2466 if (*((int *)val)) {
2467 imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2468 imp->imp_connect_data.ocd_connect_flags |=
2471 imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2472 imp->imp_connect_data.ocd_connect_flags &=
2473 ~OBD_CONNECT_RDONLY;
2475 spin_unlock(&imp->imp_lock);
2477 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2478 keylen, key, vallen, val, set);
2481 if (KEY_IS(KEY_SPTLRPC_CONF)) {
2482 sptlrpc_conf_client_adapt(exp->exp_obd);
2485 if (KEY_IS(KEY_FLUSH_CTX)) {
2486 sptlrpc_import_flush_my_ctx(imp);
2489 if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2490 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2491 keylen, key, vallen, val, set);
2494 if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2495 rc = mdc_hsm_copytool_send(vallen, val);
2499 if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2500 __u32 *default_easize = val;
2502 exp->exp_obd->u.cli.cl_default_mds_easize = *default_easize;
2506 CERROR("Unknown key %s\n", (char *)key);
2510 static int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2511 __u32 keylen, void *key, __u32 *vallen, void *val)
2515 if (KEY_IS(KEY_MAX_EASIZE)) {
2516 __u32 mdsize, *max_easize;
2518 if (*vallen != sizeof(int))
2520 mdsize = *(__u32 *)val;
2521 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2522 exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2524 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2526 } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2527 __u32 *default_easize;
2529 if (*vallen != sizeof(int))
2531 default_easize = val;
2532 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2534 } else if (KEY_IS(KEY_CONN_DATA)) {
2535 struct obd_import *imp = class_exp2cliimp(exp);
2536 struct obd_connect_data *data = val;
2538 if (*vallen != sizeof(*data))
2541 *data = imp->imp_connect_data;
2543 } else if (KEY_IS(KEY_TGT_COUNT)) {
2544 *((__u32 *)val) = 1;
2548 rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2553 static int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2554 struct obd_capa *oc, struct ptlrpc_request **request)
2556 struct ptlrpc_request *req;
2561 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2565 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2567 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2569 ptlrpc_request_free(req);
2573 mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2575 ptlrpc_request_set_replen(req);
2577 rc = ptlrpc_queue_wait(req);
2579 ptlrpc_req_finished(req);
2585 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2586 enum obd_import_event event)
2590 LASSERT(imp->imp_obd == obd);
2593 case IMP_EVENT_DISCON: {
2595 /* XXX Pass event up to OBDs stack. used only for FLD now */
2596 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2600 case IMP_EVENT_INACTIVE: {
2601 struct client_obd *cli = &obd->u.cli;
2603 * Flush current sequence to make client obtain new one
2604 * from server in case of disconnect/reconnect.
2606 if (cli->cl_seq != NULL)
2607 seq_client_flush(cli->cl_seq);
2609 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2612 case IMP_EVENT_INVALIDATE: {
2613 struct ldlm_namespace *ns = obd->obd_namespace;
2615 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2619 case IMP_EVENT_ACTIVE:
2620 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2621 /* redo the kuc registration after reconnecting */
2623 rc = mdc_kuc_reregister(imp);
2626 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2628 case IMP_EVENT_DEACTIVATE:
2629 case IMP_EVENT_ACTIVATE:
2632 CERROR("Unknown import event %x\n", event);
2638 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2639 struct lu_fid *fid, struct md_op_data *op_data)
2641 struct client_obd *cli = &exp->exp_obd->u.cli;
2642 struct lu_client_seq *seq = cli->cl_seq;
2644 RETURN(seq_client_alloc_fid(env, seq, fid));
2647 static struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2649 struct client_obd *cli = &exp->exp_obd->u.cli;
2650 return &cli->cl_target_uuid;
2654 * Determine whether the lock can be canceled before replaying it during
2655 * recovery, non zero value will be return if the lock can be canceled,
2656 * or zero returned for not
2658 static int mdc_cancel_weight(struct ldlm_lock *lock)
2660 if (lock->l_resource->lr_type != LDLM_IBITS)
2663 /* FIXME: if we ever get into a situation where there are too many
2664 * opened files with open locks on a single node, then we really
2665 * should replay these open locks to reget it */
2666 if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2672 static int mdc_resource_inode_free(struct ldlm_resource *res)
2674 if (res->lr_lvb_inode)
2675 res->lr_lvb_inode = NULL;
2680 static struct ldlm_valblock_ops inode_lvbo = {
2681 .lvbo_free = mdc_resource_inode_free
2684 static int mdc_llog_init(struct obd_device *obd)
2686 struct obd_llog_group *olg = &obd->obd_olg;
2687 struct llog_ctxt *ctxt;
2692 rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, obd,
2697 ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2698 llog_initiator_connect(ctxt);
2699 llog_ctxt_put(ctxt);
2704 static void mdc_llog_finish(struct obd_device *obd)
2706 struct llog_ctxt *ctxt;
2710 ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2712 llog_cleanup(NULL, ctxt);
2717 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2719 struct client_obd *cli = &obd->u.cli;
2723 OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2724 if (!cli->cl_rpc_lock)
2726 mdc_init_rpc_lock(cli->cl_rpc_lock);
2728 rc = ptlrpcd_addref();
2730 GOTO(err_rpc_lock, rc);
2732 OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2733 if (!cli->cl_close_lock)
2734 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2735 mdc_init_rpc_lock(cli->cl_close_lock);
2737 rc = client_obd_setup(obd, cfg);
2739 GOTO(err_close_lock, rc);
2740 #ifdef CONFIG_PROC_FS
2741 obd->obd_vars = lprocfs_mdc_obd_vars;
2742 lprocfs_obd_setup(obd);
2743 lprocfs_alloc_md_stats(obd, 0);
2745 sptlrpc_lprocfs_cliobd_attach(obd);
2746 ptlrpc_lprocfs_register_obd(obd);
2748 ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2750 obd->obd_namespace->ns_lvbo = &inode_lvbo;
2752 rc = mdc_llog_init(obd);
2755 CERROR("failed to setup llogging subsystems\n");
2761 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2765 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2769 /* Initialize the default and maximum LOV EA sizes. This allows
2770 * us to make MDS RPCs with large enough reply buffers to hold a default
2771 * sized EA without having to calculate this (via a call into the
2772 * LOV + OSCs) each time we make an RPC. The maximum size is also tracked
2773 * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2774 * a large number of stripes is possible. If a larger reply buffer is
2775 * required it will be reallocated in the ptlrpc layer due to overflow.
2777 static int mdc_init_ea_size(struct obd_export *exp, __u32 easize,
2780 struct obd_device *obd = exp->exp_obd;
2781 struct client_obd *cli = &obd->u.cli;
2784 if (cli->cl_max_mds_easize < easize)
2785 cli->cl_max_mds_easize = easize;
2787 if (cli->cl_default_mds_easize < def_easize)
2788 cli->cl_default_mds_easize = def_easize;
2793 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2799 case OBD_CLEANUP_EARLY:
2801 case OBD_CLEANUP_EXPORTS:
2802 /* Failsafe, ok if racy */
2803 if (obd->obd_type->typ_refcnt <= 1)
2804 libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
2806 obd_cleanup_client_import(obd);
2807 ptlrpc_lprocfs_unregister_obd(obd);
2808 lprocfs_obd_cleanup(obd);
2809 lprocfs_free_md_stats(obd);
2810 mdc_llog_finish(obd);
2816 static int mdc_cleanup(struct obd_device *obd)
2818 struct client_obd *cli = &obd->u.cli;
2820 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2821 OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2825 return client_obd_cleanup(obd);
2828 static int mdc_process_config(struct obd_device *obd, size_t len, void *buf)
2830 struct lustre_cfg *lcfg = buf;
2831 int rc = class_process_proc_param(PARAM_MDC, obd->obd_vars, lcfg, obd);
2832 return (rc > 0 ? 0: rc);
2836 /* get remote permission for current user on fid */
2837 static int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2838 struct obd_capa *oc, __u32 suppgid,
2839 struct ptlrpc_request **request)
2841 struct ptlrpc_request *req;
2845 LASSERT(client_is_remote(exp));
2848 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2852 mdc_set_capa_size(req, &RMF_CAPA1, oc);
2854 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2856 ptlrpc_request_free(req);
2860 mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2862 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2863 sizeof(struct mdt_remote_perm));
2865 ptlrpc_request_set_replen(req);
2867 rc = ptlrpc_queue_wait(req);
2869 ptlrpc_req_finished(req);
2875 static int mdc_interpret_renew_capa(const struct lu_env *env,
2876 struct ptlrpc_request *req, void *args,
2879 struct mdc_renew_capa_args *ra = args;
2880 struct mdt_body *body = NULL;
2881 struct lustre_capa *capa;
2885 GOTO(out, capa = ERR_PTR(status));
2887 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2889 GOTO(out, capa = ERR_PTR(-EFAULT));
2891 if ((body->mbo_valid & OBD_MD_FLOSSCAPA) == 0)
2892 GOTO(out, capa = ERR_PTR(-ENOENT));
2894 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2896 GOTO(out, capa = ERR_PTR(-EFAULT));
2899 ra->ra_cb(ra->ra_oc, capa);
2903 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2906 struct ptlrpc_request *req;
2907 struct mdc_renew_capa_args *ra;
2910 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2911 LUSTRE_MDS_VERSION, MDS_GETATTR);
2915 /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2916 * capa to renew is oss capa.
2918 mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2919 ptlrpc_request_set_replen(req);
2921 CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2922 ra = ptlrpc_req_async_args(req);
2925 req->rq_interpret_reply = mdc_interpret_renew_capa;
2926 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2930 static struct obd_ops mdc_obd_ops = {
2931 .o_owner = THIS_MODULE,
2932 .o_setup = mdc_setup,
2933 .o_precleanup = mdc_precleanup,
2934 .o_cleanup = mdc_cleanup,
2935 .o_add_conn = client_import_add_conn,
2936 .o_del_conn = client_import_del_conn,
2937 .o_connect = client_connect_import,
2938 .o_disconnect = client_disconnect_export,
2939 .o_iocontrol = mdc_iocontrol,
2940 .o_set_info_async = mdc_set_info_async,
2941 .o_statfs = mdc_statfs,
2942 .o_fid_init = client_fid_init,
2943 .o_fid_fini = client_fid_fini,
2944 .o_fid_alloc = mdc_fid_alloc,
2945 .o_import_event = mdc_import_event,
2946 .o_get_info = mdc_get_info,
2947 .o_process_config = mdc_process_config,
2948 .o_get_uuid = mdc_get_uuid,
2949 .o_quotactl = mdc_quotactl,
2952 static struct md_ops mdc_md_ops = {
2953 .m_getstatus = mdc_getstatus,
2954 .m_null_inode = mdc_null_inode,
2955 .m_find_cbdata = mdc_find_cbdata,
2956 .m_close = mdc_close,
2957 .m_create = mdc_create,
2958 .m_enqueue = mdc_enqueue,
2959 .m_getattr = mdc_getattr,
2960 .m_getattr_name = mdc_getattr_name,
2961 .m_intent_lock = mdc_intent_lock,
2963 .m_rename = mdc_rename,
2964 .m_setattr = mdc_setattr,
2965 .m_setxattr = mdc_setxattr,
2966 .m_getxattr = mdc_getxattr,
2967 .m_fsync = mdc_fsync,
2968 .m_read_page = mdc_read_page,
2969 .m_unlink = mdc_unlink,
2970 .m_cancel_unused = mdc_cancel_unused,
2971 .m_init_ea_size = mdc_init_ea_size,
2972 .m_set_lock_data = mdc_set_lock_data,
2973 .m_lock_match = mdc_lock_match,
2974 .m_get_lustre_md = mdc_get_lustre_md,
2975 .m_free_lustre_md = mdc_free_lustre_md,
2976 .m_set_open_replay_data = mdc_set_open_replay_data,
2977 .m_clear_open_replay_data = mdc_clear_open_replay_data,
2978 .m_renew_capa = mdc_renew_capa,
2979 .m_unpack_capa = mdc_unpack_capa,
2980 .m_get_remote_perm = mdc_get_remote_perm,
2981 .m_intent_getattr_async = mdc_intent_getattr_async,
2982 .m_revalidate_lock = mdc_revalidate_lock
2985 static int __init mdc_init(void)
2987 return class_register_type(&mdc_obd_ops, &mdc_md_ops, true, NULL,
2988 LUSTRE_MDC_NAME, NULL);
2991 static void /*__exit*/ mdc_exit(void)
2993 class_unregister_type(LUSTRE_MDC_NAME);
2996 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2997 MODULE_DESCRIPTION("Lustre Metadata Client");
2998 MODULE_LICENSE("GPL");
3000 module_init(mdc_init);
3001 module_exit(mdc_exit);