1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/mdt/mdt_capa.c
41 * Lustre Metadata Target (mdt) capability key read/write/update.
43 * Author: Lai Siyao <lsy@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include "mdt_internal.h"
53 static inline void set_capa_key_expiry(struct mdt_device *mdt)
55 mdt->mdt_ck_expiry = jiffies + mdt->mdt_ck_timeout * CFS_HZ;
58 static void make_capa_key(struct lustre_capa_key *key,
59 mdsno_t mdsnum, int keyid)
62 key->lk_keyid = keyid + 1;
63 cfs_get_random_bytes(key->lk_key, sizeof(key->lk_key));
66 static inline void lck_cpu_to_le(struct lustre_capa_key *tgt,
67 struct lustre_capa_key *src)
69 tgt->lk_seq = cpu_to_le64(src->lk_seq);
70 tgt->lk_keyid = cpu_to_le32(src->lk_keyid);
71 tgt->lk_padding = cpu_to_le32(src->lk_padding);
72 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
75 static inline void lck_le_to_cpu(struct lustre_capa_key *tgt,
76 struct lustre_capa_key *src)
78 tgt->lk_seq = le64_to_cpu(src->lk_seq);
79 tgt->lk_keyid = le32_to_cpu(src->lk_keyid);
80 tgt->lk_padding = le32_to_cpu(src->lk_padding);
81 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
84 static int write_capa_keys(const struct lu_env *env,
85 struct mdt_device *mdt,
86 struct lustre_capa_key *keys)
88 struct mdt_thread_info *mti;
89 struct lustre_capa_key *tmp;
94 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
95 mdt_trans_credit_init(env, mdt, MDT_TXN_CAPA_KEYS_WRITE_OP);
96 th = mdt_trans_start(env, mdt);
100 tmp = &mti->mti_capa_key;
102 for (i = 0; i < 2; i++) {
103 lck_cpu_to_le(tmp, &keys[i]);
105 rc = dt_record_write(env, mdt->mdt_ck_obj,
106 mdt_buf_const(env, tmp, sizeof(*tmp)),
112 mdt_trans_stop(env, mdt, th);
114 CDEBUG(D_INFO, "write capability keys rc = %d:\n", rc);
118 static int read_capa_keys(const struct lu_env *env,
119 struct mdt_device *mdt,
120 struct lustre_capa_key *keys)
122 struct mdt_thread_info *mti;
123 struct lustre_capa_key *tmp;
127 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
128 tmp = &mti->mti_capa_key;
130 for (i = 0; i < 2; i++) {
131 rc = dt_record_read(env, mdt->mdt_ck_obj,
132 mdt_buf(env, tmp, sizeof(*tmp)), &off);
136 lck_le_to_cpu(&keys[i], tmp);
137 DEBUG_CAPA_KEY(D_SEC, &keys[i], "read");
143 int mdt_capa_keys_init(const struct lu_env *env, struct mdt_device *mdt)
145 struct lustre_capa_key *keys = mdt->mdt_capa_keys;
146 struct mdt_thread_info *mti;
147 struct dt_object *obj;
154 mdsnum = mdt_md_site(mdt)->ms_node_id;
156 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
157 LASSERT(mti != NULL);
158 la = &mti->mti_attr.ma_attr;
160 obj = mdt->mdt_ck_obj;
161 rc = obj->do_ops->do_attr_get(env, mdt->mdt_ck_obj, la, BYPASS_CAPA);
165 size = (unsigned long)la->la_size;
169 for (i = 0; i < 2; i++) {
170 make_capa_key(&keys[i], mdsnum, i);
171 DEBUG_CAPA_KEY(D_SEC, &keys[i], "initializing");
174 rc = write_capa_keys(env, mdt, keys);
176 CERROR("error writing MDS %s: rc %d\n", CAPA_KEYS, rc);
180 rc = read_capa_keys(env, mdt, keys);
182 CERROR("error reading MDS %s: rc %d\n", CAPA_KEYS, rc);
186 set_capa_key_expiry(mdt);
187 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
188 CDEBUG(D_SEC, "mds_ck_timer %lu\n", mdt->mdt_ck_expiry);
192 void mdt_ck_timer_callback(unsigned long castmeharder)
194 struct mdt_device *mdt = (struct mdt_device *)castmeharder;
195 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
198 thread_add_flags(thread, SVC_EVENT);
199 cfs_waitq_signal(&thread->t_ctl_waitq);
203 static int mdt_ck_thread_main(void *args)
205 struct mdt_device *mdt = args;
206 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
207 struct lustre_capa_key *bkey = &mdt->mdt_capa_keys[0],
208 *rkey = &mdt->mdt_capa_keys[1];
209 struct lustre_capa_key *tmp;
211 struct mdt_thread_info *info;
212 struct md_device *next;
213 struct l_wait_info lwi = { 0 };
218 cfs_daemonize_ctxt("mdt_ck");
221 thread_set_flags(thread, SVC_RUNNING);
222 cfs_waitq_signal(&thread->t_ctl_waitq);
224 rc = lu_env_init(&env, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
228 thread->t_env = &env;
229 env.le_ctx.lc_thread = thread;
230 env.le_ctx.lc_cookie = 0x1;
232 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
233 LASSERT(info != NULL);
235 tmp = &info->mti_capa_key;
236 mdsnum = mdt_md_site(mdt)->ms_node_id;
238 l_wait_event(thread->t_ctl_waitq,
239 thread_is_stopping(thread) ||
240 thread_is_event(thread),
243 if (thread_is_stopping(thread))
245 thread_clear_flags(thread, SVC_EVENT);
247 if (cfs_time_before(cfs_time_current(), mdt->mdt_ck_expiry))
251 make_capa_key(tmp, mdsnum, rkey->lk_keyid);
253 next = mdt->mdt_child;
254 rc = next->md_ops->mdo_update_capa_key(&env, next, tmp);
256 cfs_spin_lock(&capa_lock);
259 cfs_spin_unlock(&capa_lock);
261 rc = write_capa_keys(&env, mdt, mdt->mdt_capa_keys);
263 cfs_spin_lock(&capa_lock);
265 memset(bkey, 0, sizeof(*bkey));
266 cfs_spin_unlock(&capa_lock);
268 set_capa_key_expiry(mdt);
269 DEBUG_CAPA_KEY(D_SEC, rkey, "new");
273 DEBUG_CAPA_KEY(D_ERROR, rkey, "update failed for");
274 /* next retry is in 300 sec */
275 mdt->mdt_ck_expiry = jiffies + 300 * CFS_HZ;
278 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
279 CDEBUG(D_SEC, "mdt_ck_timer %lu\n", mdt->mdt_ck_expiry);
283 thread_set_flags(thread, SVC_STOPPED);
284 cfs_waitq_signal(&thread->t_ctl_waitq);
288 int mdt_ck_thread_start(struct mdt_device *mdt)
290 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
293 cfs_waitq_init(&thread->t_ctl_waitq);
294 rc = cfs_create_thread(mdt_ck_thread_main, mdt, CFS_DAEMON_FLAGS);
296 CERROR("cannot start mdt_ck thread, rc = %d\n", rc);
300 l_wait_condition(thread->t_ctl_waitq, thread_is_running(thread));
304 void mdt_ck_thread_stop(struct mdt_device *mdt)
306 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
308 if (!thread_is_running(thread))
311 thread_set_flags(thread, SVC_STOPPING);
312 cfs_waitq_signal(&thread->t_ctl_waitq);
313 l_wait_condition(thread->t_ctl_waitq, thread_is_stopped(thread));