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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_capa.c
38 * Lustre Metadata Target (mdt) capability key read/write/update.
40 * Author: Lai Siyao <lsy@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include "mdt_internal.h"
47 static inline void set_capa_key_expiry(struct mdt_device *mdt)
49 mdt->mdt_ck_expiry = jiffies + mdt->mdt_ck_timeout * CFS_HZ;
52 static void make_capa_key(struct lustre_capa_key *key,
53 mdsno_t mdsnum, int keyid)
56 key->lk_keyid = keyid + 1;
57 cfs_get_random_bytes(key->lk_key, sizeof(key->lk_key));
60 static inline void lck_cpu_to_le(struct lustre_capa_key *tgt,
61 struct lustre_capa_key *src)
63 tgt->lk_seq = cpu_to_le64(src->lk_seq);
64 tgt->lk_keyid = cpu_to_le32(src->lk_keyid);
65 tgt->lk_padding = cpu_to_le32(src->lk_padding);
66 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
69 static inline void lck_le_to_cpu(struct lustre_capa_key *tgt,
70 struct lustre_capa_key *src)
72 tgt->lk_seq = le64_to_cpu(src->lk_seq);
73 tgt->lk_keyid = le32_to_cpu(src->lk_keyid);
74 tgt->lk_padding = le32_to_cpu(src->lk_padding);
75 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
78 static int write_capa_keys(const struct lu_env *env,
79 struct mdt_device *mdt,
80 struct lustre_capa_key *keys)
82 struct mdt_thread_info *mti;
83 struct lustre_capa_key *tmp;
88 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
89 th = dt_trans_create(env, mdt->mdt_bottom);
93 rc = dt_declare_record_write(env, mdt->mdt_ck_obj,
94 sizeof(*tmp) * 3, 0, th);
98 rc = dt_trans_start_local(env, mdt->mdt_bottom, th);
102 tmp = &mti->mti_capa_key;
104 for (i = 0; i < 2; i++) {
105 lck_cpu_to_le(tmp, &keys[i]);
107 rc = dt_record_write(env, mdt->mdt_ck_obj,
108 mdt_buf_const(env, tmp, sizeof(*tmp)),
115 dt_trans_stop(env, mdt->mdt_bottom, th);
117 CDEBUG(D_INFO, "write capability keys rc = %d:\n", rc);
121 static int read_capa_keys(const struct lu_env *env,
122 struct mdt_device *mdt,
123 struct lustre_capa_key *keys)
125 struct mdt_thread_info *mti;
126 struct lustre_capa_key *tmp;
130 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
131 tmp = &mti->mti_capa_key;
133 for (i = 0; i < 2; i++) {
134 rc = dt_record_read(env, mdt->mdt_ck_obj,
135 mdt_buf(env, tmp, sizeof(*tmp)), &off);
139 lck_le_to_cpu(&keys[i], tmp);
140 DEBUG_CAPA_KEY(D_SEC, &keys[i], "read");
146 int mdt_capa_keys_init(const struct lu_env *env, struct mdt_device *mdt)
148 struct lustre_capa_key *keys = mdt->mdt_capa_keys;
149 struct mdt_thread_info *mti;
150 struct dt_object *obj;
157 mdsnum = mdt_md_site(mdt)->ms_node_id;
159 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
160 LASSERT(mti != NULL);
161 la = &mti->mti_attr.ma_attr;
163 obj = mdt->mdt_ck_obj;
164 rc = obj->do_ops->do_attr_get(env, mdt->mdt_ck_obj, la, BYPASS_CAPA);
168 size = (unsigned long)la->la_size;
172 for (i = 0; i < 2; i++) {
173 make_capa_key(&keys[i], mdsnum, i);
174 DEBUG_CAPA_KEY(D_SEC, &keys[i], "initializing");
177 rc = write_capa_keys(env, mdt, keys);
179 CERROR("error writing MDS %s: rc %d\n", CAPA_KEYS, rc);
183 rc = read_capa_keys(env, mdt, keys);
185 CERROR("error reading MDS %s: rc %d\n", CAPA_KEYS, rc);
189 set_capa_key_expiry(mdt);
190 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
191 CDEBUG(D_SEC, "mds_ck_timer %lu\n", mdt->mdt_ck_expiry);
195 void mdt_ck_timer_callback(unsigned long castmeharder)
197 struct mdt_device *mdt = (struct mdt_device *)castmeharder;
198 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
201 thread_add_flags(thread, SVC_EVENT);
202 cfs_waitq_signal(&thread->t_ctl_waitq);
206 static int mdt_ck_thread_main(void *args)
208 struct mdt_device *mdt = args;
209 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
210 struct lustre_capa_key *bkey = &mdt->mdt_capa_keys[0],
211 *rkey = &mdt->mdt_capa_keys[1];
212 struct lustre_capa_key *tmp;
214 struct mdt_thread_info *info;
215 struct md_device *next;
216 struct l_wait_info lwi = { 0 };
221 cfs_daemonize_ctxt("mdt_ck");
224 thread_set_flags(thread, SVC_RUNNING);
225 cfs_waitq_signal(&thread->t_ctl_waitq);
227 rc = lu_env_init(&env, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
231 thread->t_env = &env;
232 env.le_ctx.lc_thread = thread;
233 env.le_ctx.lc_cookie = 0x1;
235 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
236 LASSERT(info != NULL);
238 tmp = &info->mti_capa_key;
239 mdsnum = mdt_md_site(mdt)->ms_node_id;
241 l_wait_event(thread->t_ctl_waitq,
242 thread_is_stopping(thread) ||
243 thread_is_event(thread),
246 if (thread_is_stopping(thread))
248 thread_clear_flags(thread, SVC_EVENT);
250 if (cfs_time_before(cfs_time_current(), mdt->mdt_ck_expiry))
254 make_capa_key(tmp, mdsnum, rkey->lk_keyid);
256 next = mdt->mdt_child;
257 rc = next->md_ops->mdo_update_capa_key(&env, next, tmp);
259 spin_lock(&capa_lock);
262 spin_unlock(&capa_lock);
264 rc = write_capa_keys(&env, mdt, mdt->mdt_capa_keys);
266 spin_lock(&capa_lock);
268 memset(bkey, 0, sizeof(*bkey));
269 spin_unlock(&capa_lock);
271 set_capa_key_expiry(mdt);
272 DEBUG_CAPA_KEY(D_SEC, rkey, "new");
276 DEBUG_CAPA_KEY(D_ERROR, rkey, "update failed for");
277 /* next retry is in 300 sec */
278 mdt->mdt_ck_expiry = jiffies + 300 * CFS_HZ;
281 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
282 CDEBUG(D_SEC, "mdt_ck_timer %lu\n", mdt->mdt_ck_expiry);
286 thread_set_flags(thread, SVC_STOPPED);
287 cfs_waitq_signal(&thread->t_ctl_waitq);
291 int mdt_ck_thread_start(struct mdt_device *mdt)
293 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
296 cfs_waitq_init(&thread->t_ctl_waitq);
297 rc = cfs_create_thread(mdt_ck_thread_main, mdt, CFS_DAEMON_FLAGS);
299 CERROR("cannot start mdt_ck thread, rc = %d\n", rc);
303 l_wait_condition(thread->t_ctl_waitq, thread_is_running(thread));
307 void mdt_ck_thread_stop(struct mdt_device *mdt)
309 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
311 if (!thread_is_running(thread))
314 thread_set_flags(thread, SVC_STOPPING);
315 cfs_waitq_signal(&thread->t_ctl_waitq);
316 l_wait_condition(thread->t_ctl_waitq, thread_is_stopped(thread));