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_blocking_ast(struct ldlm_lock *lock,
31 struct ldlm_lock_desc *new,
32 void *data, __u32 data_len)
35 CERROR("ldlm_blocking_ast: lock=%p, new=%p\n", lock, new);
39 int ldlm_test_basics(struct obd_device *obddev)
41 struct ldlm_namespace *ns;
42 struct ldlm_resource *res;
43 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
45 struct ldlm_lock *lock1, *lock;
48 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
52 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_CR, NULL, 0);
55 err = ldlm_lock_enqueue(lock1, NULL, 0, &flags,
56 ldlm_completion_ast, ldlm_blocking_ast);
60 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_EX, NULL, 0);
63 err = ldlm_lock_enqueue(lock, NULL, 0, &flags,
64 ldlm_completion_ast, ldlm_blocking_ast);
67 if (!(flags & LDLM_FL_BLOCK_GRANTED))
70 res = ldlm_resource_get(ns, NULL, res_id, LDLM_PLAIN, 1);
73 ldlm_resource_dump(res);
75 res = ldlm_lock_convert(lock1, LCK_NL, &flags);
77 ldlm_reprocess_all(res);
79 ldlm_resource_dump(res);
80 ldlm_namespace_free(ns);
85 int ldlm_test_extents(struct obd_device *obddev)
87 struct ldlm_namespace *ns;
88 struct ldlm_resource *res;
89 struct ldlm_lock *lock, *lock1, *lock2;
90 __u64 res_id[RES_NAME_SIZE] = {0, 0, 0};
91 struct ldlm_extent ext1 = {4, 6}, ext2 = {6, 9}, ext3 = {10, 11};
95 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
100 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR, NULL,
104 err = ldlm_lock_enqueue(lock1, &ext1, sizeof(ext1), &flags, NULL, NULL);
107 if (!(flags & LDLM_FL_LOCK_CHANGED))
111 lock2 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR,
113 err = ldlm_lock_enqueue(lock2, &ext2, sizeof(ext2), &flags, NULL, NULL);
116 if (!(flags & LDLM_FL_LOCK_CHANGED))
120 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_EX, NULL, 0);
123 err = ldlm_lock_enqueue(lock, &ext3, sizeof(ext3), &flags,
127 if (!(flags & LDLM_FL_BLOCK_GRANTED))
129 if (flags & LDLM_FL_LOCK_CHANGED)
132 /* Convert/cancel blocking locks */
134 res = ldlm_lock_convert(lock1, LCK_NL, &flags);
136 ldlm_reprocess_all(res);
138 ldlm_lock_cancel(lock2);
140 ldlm_reprocess_all(res);
142 /* Dump the results */
143 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
146 ldlm_resource_dump(res);
147 ldlm_namespace_free(ns);
152 static int ldlm_test_network(struct obd_device *obddev,
153 struct ptlrpc_connection *conn)
156 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
157 struct ldlm_extent ext = {4, 6};
158 struct lustre_handle lockh1;
162 /* FIXME: this needs a connh as 3rd paramter, before it will work */
164 err = ldlm_cli_enqueue(NULL, NULL, obddev->obd_namespace, NULL, res_id,
165 LDLM_EXTENT, &ext, sizeof(ext), LCK_PR, &flags,
166 NULL, NULL, NULL, 0, &lockh1);
167 CERROR("ldlm_cli_enqueue: %d\n", err);
169 ldlm_lock_decref(&lockh1, LCK_PR);
174 static int ldlm_test_main(void *data)
176 struct ldlm_test_thread *thread = data;
177 struct ldlm_namespace *ns;
182 spin_lock_irq(¤t->sigmask_lock);
183 sigfillset(¤t->blocked);
184 recalc_sigpending(current);
185 spin_unlock_irq(¤t->sigmask_lock);
187 sprintf(current->comm, "ldlm_test");
189 ns = ldlm_namespace_new("ldlm_test", LDLM_NAMESPACE_CLIENT);
195 /* Record that the thread is running */
196 thread->t_flags |= SVC_RUNNING;
197 wake_up(&thread->t_ctl_waitq);
200 struct lustre_handle lockh;
201 __u64 res_id[3] = {0};
206 /* Pick a random resource from 1 to 10 */
207 get_random_bytes(&random, sizeof(random));
208 res_id[0] = random % 10 + 1;
210 /* Pick a random lock mode */
211 get_random_bytes(&random, sizeof(random));
212 lock_mode = random % LCK_NL + 1;
214 rc = ldlm_cli_enqueue(NULL, NULL, ns, NULL,
215 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
216 &flags, ldlm_completion_ast,
217 ldlm_blocking_ast, NULL, 0, &lockh);
219 CERROR("ldlm_cli_enqueue: %d\n", rc);
225 thread->t_flags |= SVC_STOPPED;
226 wake_up(&thread->t_ctl_waitq);
231 static int ldlm_start_thread(void)
233 struct ldlm_test_thread *test;
237 OBD_ALLOC(test, sizeof(*test));
242 init_waitqueue_head(&test->t_ctl_waitq);
244 spin_lock(&ctl_lock);
245 list_add(&test->t_link, &ctl_threads);
246 spin_unlock(&ctl_lock);
248 rc = kernel_thread(ldlm_test_main, (void *)test,
249 CLONE_VM | CLONE_FS | CLONE_FILES);
251 CERROR("cannot start thread\n");
254 wait_event(test->t_ctl_waitq, test->t_flags & SVC_RUNNING);
259 int ldlm_regression_start(struct obd_device *obddev,
260 struct ptlrpc_connection *conn, int count)
265 spin_lock(&ctl_lock);
266 if (regression_running) {
267 CERROR("You can't start the ldlm regression twice.\n");
268 spin_unlock(&ctl_lock);
271 regression_running = 1;
272 spin_unlock(&ctl_lock);
274 for (i = 0; i < count; i++) {
275 rc = ldlm_start_thread();
284 int ldlm_regression_stop(void)
288 spin_lock(&ctl_lock);
289 if (!regression_running) {
290 CERROR("The ldlm regression isn't started.\n");
291 spin_unlock(&ctl_lock);
295 while (!list_empty(&ctl_threads)) {
296 struct ldlm_test_thread *thread;
297 thread = list_entry(ctl_threads.next, struct ldlm_test_thread,
300 thread->t_flags |= SVC_STOPPING;
301 spin_unlock(&ctl_lock);
303 wake_up(&thread->t_ctl_waitq);
304 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
306 spin_lock(&ctl_lock);
307 list_del(&thread->t_link);
308 OBD_FREE(thread, sizeof(*thread));
311 regression_running = 0;
312 spin_unlock(&ctl_lock);
317 int ldlm_test(struct obd_device *obddev, struct ptlrpc_connection *conn)
320 rc = ldlm_test_basics(obddev);
324 rc = ldlm_test_extents(obddev);
328 rc = ldlm_test_network(obddev, conn);