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/lustre_dlm.h>
16 struct ldlm_test_thread {
17 struct ldlm_namespace *t_ns;
18 struct list_head t_link;
20 wait_queue_head_t t_ctl_waitq;
23 static spinlock_t ctl_lock = SPIN_LOCK_UNLOCKED;
24 static struct list_head ctl_threads;
25 static int regression_running = 0;
27 static int ldlm_test_callback(struct lustre_handle *lockh,
28 struct ldlm_lock_desc *new,
29 void *data, __u32 data_len)
31 printk("ldlm_test_callback: lock=%Lu, new=%p\n", lockh->addr, new);
35 int ldlm_test_basics(struct obd_device *obddev)
37 struct ldlm_namespace *ns;
38 struct ldlm_resource *res;
39 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
41 struct ldlm_lock *lock1, *lock;
44 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
48 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_CR, NULL, 0);
51 err = ldlm_lock_enqueue(lock1, NULL, 0, &flags,
52 ldlm_test_callback, ldlm_test_callback);
56 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_PLAIN, LCK_EX, NULL, 0);
59 err = ldlm_lock_enqueue(lock, NULL, 0, &flags,
60 ldlm_test_callback, ldlm_test_callback);
63 if (!(flags & LDLM_FL_BLOCK_GRANTED))
66 res = ldlm_resource_get(ns, NULL, res_id, LDLM_PLAIN, 1);
69 ldlm_resource_dump(res);
71 res = ldlm_convert(lock1, LCK_NL, &flags);
73 ldlm_reprocess_all(res);
75 ldlm_resource_dump(res);
76 ldlm_namespace_free(ns);
81 int ldlm_test_extents(struct obd_device *obddev)
83 struct ldlm_namespace *ns;
84 struct ldlm_resource *res;
85 struct ldlm_lock *lock, *lock1, *lock2;
86 __u64 res_id[RES_NAME_SIZE] = {0, 0, 0};
87 struct ldlm_extent ext1 = {4, 6}, ext2 = {6, 9}, ext3 = {10, 11};
91 ns = ldlm_namespace_new("test_server", LDLM_NAMESPACE_SERVER);
96 lock1 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR, NULL,
100 err = ldlm_lock_enqueue(lock1, &ext1, sizeof(ext1), &flags, NULL, NULL);
103 if (!(flags & LDLM_FL_LOCK_CHANGED))
107 lock2 = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_PR,
109 err = ldlm_lock_enqueue(lock2, &ext2, sizeof(ext2), &flags, NULL, NULL);
112 if (!(flags & LDLM_FL_LOCK_CHANGED))
116 lock = ldlm_lock_create(ns, NULL, res_id, LDLM_EXTENT, LCK_EX, NULL, 0);
119 err = ldlm_lock_enqueue(lock, &ext3, sizeof(ext3), &flags,
123 if (!(flags & LDLM_FL_BLOCK_GRANTED))
125 if (flags & LDLM_FL_LOCK_CHANGED)
128 /* Convert/cancel blocking locks */
130 res = ldlm_convert(lock1, LCK_NL, &flags);
132 ldlm_reprocess_all(res);
134 ldlm_lock_cancel(lock2);
136 ldlm_reprocess_all(res);
138 /* Dump the results */
139 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
142 ldlm_resource_dump(res);
143 ldlm_namespace_free(ns);
148 static int ldlm_test_network(struct obd_device *obddev,
149 struct ptlrpc_connection *conn)
151 struct ldlm_obd *ldlm = &obddev->u.ldlm;
153 __u64 res_id[RES_NAME_SIZE] = {1, 2, 3};
154 struct ldlm_extent ext = {4, 6};
155 struct lustre_handle lockh1;
159 err = ldlm_cli_enqueue(ldlm->ldlm_client, conn, NULL,
160 obddev->obd_namespace, NULL, res_id, LDLM_EXTENT,
161 &ext, sizeof(ext), LCK_PR, &flags, NULL, NULL, 0,
163 CERROR("ldlm_cli_enqueue: %d\n", err);
168 static int ldlm_test_main(void *data)
173 static int ldlm_start_thread(void)
175 struct ldlm_test_thread *test;
179 OBD_ALLOC(test, sizeof(*test));
184 init_waitqueue_head(&test->t_ctl_waitq);
186 spin_lock(&ctl_lock);
187 list_add(&test->t_link, &ctl_threads);
188 spin_unlock(&ctl_lock);
190 rc = kernel_thread(ldlm_test_main, (void *)test,
191 CLONE_VM | CLONE_FS | CLONE_FILES);
193 CERROR("cannot start thread\n");
196 wait_event(test->t_ctl_waitq, test->t_flags & SVC_RUNNING);
201 static int ldlm_stop_all_threads(void)
203 spin_lock(&ctl_lock);
204 while (!list_empty(&ctl_threads)) {
205 struct ldlm_test_thread *thread;
206 thread = list_entry(ctl_threads.next, struct ldlm_test_thread,
208 spin_unlock(&ctl_lock);
210 thread->t_flags = SVC_STOPPING;
212 wake_up(&thread->t_ctl_waitq);
213 wait_event_interruptible(thread->t_ctl_waitq,
214 (thread->t_flags & SVC_STOPPED));
216 spin_lock(&ctl_lock);
217 list_del(&thread->t_link);
218 OBD_FREE(thread, sizeof(*thread));
220 spin_unlock(&ctl_lock);
225 int ldlm_regression_start(struct obd_device *obddev,
226 struct ptlrpc_connection *conn, int count)
231 spin_lock(&ctl_lock);
232 if (regression_running) {
233 CERROR("You can't start the ldlm regression twice.\n");
234 spin_unlock(&ctl_lock);
237 regression_running = 1;
238 spin_unlock(&ctl_lock);
240 for (i = 0; i < count; i++) {
241 rc = ldlm_start_thread();
250 int ldlm_regression_stop(void)
254 spin_lock(&ctl_lock);
255 if (!regression_running) {
256 CERROR("The ldlm regression isn't started.\n");
257 spin_unlock(&ctl_lock);
260 spin_unlock(&ctl_lock);
264 spin_lock(&ctl_lock);
265 regression_running = 0;
266 spin_unlock(&ctl_lock);
271 int ldlm_test(struct obd_device *obddev, struct ptlrpc_connection *conn)
274 rc = ldlm_test_basics(obddev);
278 rc = ldlm_test_extents(obddev);
282 rc = ldlm_test_network(obddev, conn);