1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * by Cluster File Systems, Inc.
12 #define DEBUG_SUBSYSTEM S_LDLM
14 #include <linux/types.h>
15 #include <linux/random.h>
17 #include <linux/lustre_dlm.h>
19 struct ldlm_test_thread {
20 struct ldlm_namespace *t_ns;
21 struct list_head t_link;
23 wait_queue_head_t t_ctl_waitq;
26 static spinlock_t ctl_lock = SPIN_LOCK_UNLOCKED;
27 static struct list_head ctl_threads;
28 static int regression_running = 0;
30 static int ldlm_test_callback(struct lustre_handle *lockh,
31 struct ldlm_lock_desc *new,
32 void *data, __u32 data_len)
34 struct ldlm_lock *lock;
37 lock = ldlm_handle2lock(lockh);
39 CERROR("invalid handle in callback\n");
43 printk("ldlm_test_callback: lock=%Lu, new=%p\n", lockh->addr, new);
47 int ldlm_test_basics(struct obd_device *obddev)
49 struct ldlm_namespace *ns;
50 struct ldlm_resource *res;
51 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
53 struct ldlm_lock *lock1, *lock;
56 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
60 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_CR, NULL, 0);
63 err = ldlm_lock_enqueue(lock1, NULL, 0, &flags,
64 ldlm_completion_ast, ldlm_test_callback);
68 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_EX, NULL, 0);
71 err = ldlm_lock_enqueue(lock, NULL, 0, &flags,
72 ldlm_completion_ast, ldlm_test_callback);
75 if (!(flags & LDLM_FL_BLOCK_GRANTED))
78 res = ldlm_resource_get(ns, NULL, res_id, LDLM_PLAIN, 1);
81 ldlm_resource_dump(res);
83 res = ldlm_lock_convert(lock1, LCK_NL, &flags);
85 ldlm_reprocess_all(res);
87 ldlm_resource_dump(res);
88 ldlm_namespace_free(ns);
93 int ldlm_test_extents(struct obd_device *obddev)
95 struct ldlm_namespace *ns;
96 struct ldlm_resource *res;
97 struct ldlm_lock *lock, *lock1, *lock2;
98 __u64 res_id[RES_NAME_SIZE] = {0, 0, 0};
99 struct ldlm_extent ext1 = {4, 6}, ext2 = {6, 9}, ext3 = {10, 11};
103 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
108 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR, NULL,
112 err = ldlm_lock_enqueue(lock1, &ext1, sizeof(ext1), &flags, NULL, NULL);
115 if (!(flags & LDLM_FL_LOCK_CHANGED))
119 lock2 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR,
121 err = ldlm_lock_enqueue(lock2, &ext2, sizeof(ext2), &flags, NULL, NULL);
124 if (!(flags & LDLM_FL_LOCK_CHANGED))
128 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_EX, NULL, 0);
131 err = ldlm_lock_enqueue(lock, &ext3, sizeof(ext3), &flags,
135 if (!(flags & LDLM_FL_BLOCK_GRANTED))
137 if (flags & LDLM_FL_LOCK_CHANGED)
140 /* Convert/cancel blocking locks */
142 res = ldlm_lock_convert(lock1, LCK_NL, &flags);
144 ldlm_reprocess_all(res);
146 ldlm_lock_cancel(lock2);
148 ldlm_reprocess_all(res);
150 /* Dump the results */
151 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
154 ldlm_resource_dump(res);
155 ldlm_namespace_free(ns);
160 static int ldlm_test_network(struct obd_device *obddev,
161 struct ptlrpc_connection *conn)
164 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
165 struct ldlm_extent ext = {4, 6};
166 struct lustre_handle lockh1;
170 /* FIXME: this needs a connh as 3rd paramter, before it will work */
172 err = ldlm_cli_enqueue(NULL, NULL, obddev->obd_namespace, NULL, res_id, LDLM_EXTENT,
173 &ext, sizeof(ext), LCK_PR, &flags, NULL, NULL, NULL, 0,
175 CERROR("ldlm_cli_enqueue: %d\n", err);
177 ldlm_lock_decref(&lockh1, LCK_PR);
182 static int ldlm_test_main(void *data)
184 struct ldlm_test_thread *thread = data;
185 struct ldlm_namespace *ns;
190 spin_lock_irq(¤t->sigmask_lock);
191 sigfillset(¤t->blocked);
192 recalc_sigpending(current);
193 spin_unlock_irq(¤t->sigmask_lock);
195 sprintf(current->comm, "ldlm_test");
197 ns = ldlm_namespace_new("ldlm_test", LDLM_NAMESPACE_CLIENT);
203 /* Record that the thread is running */
204 thread->t_flags |= SVC_RUNNING;
205 wake_up(&thread->t_ctl_waitq);
208 struct lustre_handle lockh;
209 __u64 res_id[3] = {0};
214 /* Pick a random resource from 1 to 10 */
215 get_random_bytes(&random, sizeof(random));
216 res_id[0] = random % 10 + 1;
218 /* Pick a random lock mode */
219 get_random_bytes(&random, sizeof(random));
220 lock_mode = random % LCK_NL + 1;
222 rc = ldlm_cli_enqueue(NULL, NULL, ns, NULL,
223 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
224 &flags, ldlm_completion_ast, ldlm_test_callback, NULL, 0,
227 CERROR("ldlm_cli_enqueue: %d\n", rc);
233 thread->t_flags |= SVC_STOPPED;
234 wake_up(&thread->t_ctl_waitq);
239 static int ldlm_start_thread(void)
241 struct ldlm_test_thread *test;
245 OBD_ALLOC(test, sizeof(*test));
250 init_waitqueue_head(&test->t_ctl_waitq);
252 spin_lock(&ctl_lock);
253 list_add(&test->t_link, &ctl_threads);
254 spin_unlock(&ctl_lock);
256 rc = kernel_thread(ldlm_test_main, (void *)test,
257 CLONE_VM | CLONE_FS | CLONE_FILES);
259 CERROR("cannot start thread\n");
262 wait_event(test->t_ctl_waitq, test->t_flags & SVC_RUNNING);
267 int ldlm_regression_start(struct obd_device *obddev,
268 struct ptlrpc_connection *conn, int count)
273 spin_lock(&ctl_lock);
274 if (regression_running) {
275 CERROR("You can't start the ldlm regression twice.\n");
276 spin_unlock(&ctl_lock);
279 regression_running = 1;
280 spin_unlock(&ctl_lock);
282 for (i = 0; i < count; i++) {
283 rc = ldlm_start_thread();
292 int ldlm_regression_stop(void)
296 spin_lock(&ctl_lock);
297 if (!regression_running) {
298 CERROR("The ldlm regression isn't started.\n");
299 spin_unlock(&ctl_lock);
303 while (!list_empty(&ctl_threads)) {
304 struct ldlm_test_thread *thread;
305 thread = list_entry(ctl_threads.next, struct ldlm_test_thread,
308 thread->t_flags |= SVC_STOPPING;
309 spin_unlock(&ctl_lock);
311 wake_up(&thread->t_ctl_waitq);
312 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
314 spin_lock(&ctl_lock);
315 list_del(&thread->t_link);
316 OBD_FREE(thread, sizeof(*thread));
319 regression_running = 0;
320 spin_unlock(&ctl_lock);
325 int ldlm_test(struct obd_device *obddev, struct ptlrpc_connection *conn)
328 rc = ldlm_test_basics(obddev);
332 rc = ldlm_test_extents(obddev);
336 rc = ldlm_test_network(obddev, conn);