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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include "mdt_internal.h"
50 static inline void set_capa_key_expiry(struct mdt_device *mdt)
52 mdt->mdt_ck_expiry = jiffies + mdt->mdt_ck_timeout * HZ;
55 static void make_capa_key(struct lustre_capa_key *key,
56 mdsno_t mdsnum, int keyid)
58 key->lk_mdsid = mdsnum;
59 key->lk_keyid = keyid + 1;
60 ll_get_random_bytes(key->lk_key, sizeof(key->lk_key));
63 static inline void lck_cpu_to_le(struct lustre_capa_key *tgt,
64 struct lustre_capa_key *src)
66 tgt->lk_mdsid = cpu_to_le64(src->lk_mdsid);
67 tgt->lk_keyid = cpu_to_le32(src->lk_keyid);
68 tgt->lk_padding = cpu_to_le32(src->lk_padding);
69 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
72 static inline void lck_le_to_cpu(struct lustre_capa_key *tgt,
73 struct lustre_capa_key *src)
75 tgt->lk_mdsid = le64_to_cpu(src->lk_mdsid);
76 tgt->lk_keyid = le32_to_cpu(src->lk_keyid);
77 tgt->lk_padding = le32_to_cpu(src->lk_padding);
78 memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
81 static int write_capa_keys(const struct lu_env *env,
82 struct mdt_device *mdt,
83 struct lustre_capa_key *keys)
85 struct mdt_thread_info *mti;
86 struct lustre_capa_key *tmp;
91 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
92 mdt_trans_credit_init(env, mdt, MDT_TXN_CAPA_KEYS_WRITE_OP);
93 th = mdt_trans_start(env, mdt);
97 tmp = &mti->mti_capa_key;
99 for (i = 0; i < 2; i++) {
100 lck_cpu_to_le(tmp, &keys[i]);
102 rc = dt_record_write(env, mdt->mdt_ck_obj,
103 mdt_buf_const(env, tmp, sizeof(*tmp)),
109 mdt_trans_stop(env, mdt, th);
111 CDEBUG(D_INFO, "write capability keys rc = %d:\n", rc);
115 static int read_capa_keys(const struct lu_env *env,
116 struct mdt_device *mdt,
117 struct lustre_capa_key *keys)
119 struct mdt_thread_info *mti;
120 struct lustre_capa_key *tmp;
124 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
125 tmp = &mti->mti_capa_key;
127 for (i = 0; i < 2; i++) {
128 rc = dt_record_read(env, mdt->mdt_ck_obj,
129 mdt_buf(env, tmp, sizeof(*tmp)), &off);
133 lck_le_to_cpu(&keys[i], tmp);
134 DEBUG_CAPA_KEY(D_SEC, &keys[i], "read");
140 int mdt_capa_keys_init(const struct lu_env *env, struct mdt_device *mdt)
142 struct lustre_capa_key *keys = mdt->mdt_capa_keys;
143 struct mdt_thread_info *mti;
144 struct dt_object *obj;
151 mdsnum = mdt_md_site(mdt)->ms_node_id;
153 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
154 LASSERT(mti != NULL);
155 la = &mti->mti_attr.ma_attr;
157 obj = mdt->mdt_ck_obj;
158 rc = obj->do_ops->do_attr_get(env, mdt->mdt_ck_obj, la, BYPASS_CAPA);
162 size = (unsigned long)la->la_size;
166 for (i = 0; i < 2; i++) {
167 make_capa_key(&keys[i], mdsnum, i);
168 DEBUG_CAPA_KEY(D_SEC, &keys[i], "initializing");
171 rc = write_capa_keys(env, mdt, keys);
173 CERROR("error writing MDS %s: rc %d\n", CAPA_KEYS, rc);
177 rc = read_capa_keys(env, mdt, keys);
179 CERROR("error reading MDS %s: rc %d\n", CAPA_KEYS, rc);
183 set_capa_key_expiry(mdt);
184 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
185 CDEBUG(D_SEC, "mds_ck_timer %lu\n", mdt->mdt_ck_expiry);
189 void mdt_ck_timer_callback(unsigned long castmeharder)
191 struct mdt_device *mdt = (struct mdt_device *)castmeharder;
192 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
195 thread->t_flags |= SVC_EVENT;
196 cfs_waitq_signal(&thread->t_ctl_waitq);
200 static int mdt_ck_thread_main(void *args)
202 struct mdt_device *mdt = args;
203 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
204 struct lustre_capa_key *bkey = &mdt->mdt_capa_keys[0],
205 *rkey = &mdt->mdt_capa_keys[1];
206 struct lustre_capa_key *tmp;
208 struct mdt_thread_info *info;
209 struct md_device *next;
210 struct l_wait_info lwi = { 0 };
215 cfs_daemonize_ctxt("mdt_ck");
218 thread->t_flags = SVC_RUNNING;
219 cfs_waitq_signal(&thread->t_ctl_waitq);
221 rc = lu_env_init(&env, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
225 thread->t_env = &env;
226 env.le_ctx.lc_thread = thread;
227 env.le_ctx.lc_cookie = 0x1;
229 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
230 LASSERT(info != NULL);
232 tmp = &info->mti_capa_key;
233 mdsnum = mdt_md_site(mdt)->ms_node_id;
235 l_wait_event(thread->t_ctl_waitq,
236 thread->t_flags & (SVC_STOPPING | SVC_EVENT),
239 if (thread->t_flags & SVC_STOPPING)
241 thread->t_flags &= ~SVC_EVENT;
243 if (cfs_time_before(cfs_time_current(), mdt->mdt_ck_expiry))
247 make_capa_key(tmp, mdsnum, rkey->lk_keyid);
249 next = mdt->mdt_child;
250 rc = next->md_ops->mdo_update_capa_key(&env, next, tmp);
252 spin_lock(&capa_lock);
255 spin_unlock(&capa_lock);
257 rc = write_capa_keys(&env, mdt, mdt->mdt_capa_keys);
259 spin_lock(&capa_lock);
261 memset(bkey, 0, sizeof(*bkey));
262 spin_unlock(&capa_lock);
264 set_capa_key_expiry(mdt);
265 DEBUG_CAPA_KEY(D_SEC, rkey, "new");
269 DEBUG_CAPA_KEY(D_ERROR, rkey, "update failed for");
270 /* next retry is in 300 sec */
271 mdt->mdt_ck_expiry = jiffies + 300 * HZ;
274 cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
275 CDEBUG(D_SEC, "mdt_ck_timer %lu\n", mdt->mdt_ck_expiry);
279 thread->t_flags = SVC_STOPPED;
280 cfs_waitq_signal(&thread->t_ctl_waitq);
284 int mdt_ck_thread_start(struct mdt_device *mdt)
286 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
289 cfs_waitq_init(&thread->t_ctl_waitq);
290 rc = cfs_kernel_thread(mdt_ck_thread_main, mdt,
291 (CLONE_VM | CLONE_FILES));
293 CERROR("cannot start mdt_ck thread, rc = %d\n", rc);
297 cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
301 void mdt_ck_thread_stop(struct mdt_device *mdt)
303 struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
305 if (!(thread->t_flags & SVC_RUNNING))
308 thread->t_flags = SVC_STOPPING;
309 cfs_waitq_signal(&thread->t_ctl_waitq);
310 cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);