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>
15 #include <linux/obd_class.h>
16 #include <linux/obd.h>
18 static int interrupted_completion_wait(void *data)
23 static int expired_completion_wait(void *data)
25 struct ldlm_lock *lock = data;
26 class_signal_connection_failure(lock->l_export->exp_connection);
30 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
32 struct l_wait_info lwi =
33 LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_completion_wait,
34 interrupted_completion_wait, lock);
38 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
39 LDLM_FL_BLOCK_CONV)) {
40 /* Go to sleep until the lock is granted. */
41 /* FIXME: or cancelled. */
42 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock,"
45 ldlm_reprocess_all(lock->l_resource);
46 rc = l_wait_event(lock->l_waitq,
47 (lock->l_req_mode == lock->l_granted_mode),
51 "client-side enqueue waking up: failed (%d)",
55 "client-side enqueue waking up: granted");
57 } else if (flags == LDLM_FL_WAIT_NOREPROC) {
58 rc = l_wait_event(lock->l_waitq,
59 (lock->l_req_mode == lock->l_granted_mode),
61 } else if (flags == 0) {
62 wake_up(&lock->l_waitq);
68 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
69 struct lustre_handle *parent_lockh,
72 void *cookie, int cookielen,
75 ldlm_completion_callback completion,
76 ldlm_blocking_callback blocking,
79 struct lustre_handle *lockh)
81 struct ldlm_lock *lock;
85 CERROR("Trying to cancel local lock\n");
89 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data, data_len);
91 GOTO(out_nolock, err = -ENOMEM);
92 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
94 ldlm_lock_addref_internal(lock, mode);
95 ldlm_lock2handle(lock, lockh);
98 err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
103 if (type == LDLM_EXTENT)
104 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
105 if ((*flags) & LDLM_FL_LOCK_CHANGED)
106 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
108 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
111 if (lock->l_completion_ast)
112 lock->l_completion_ast(lock, *flags);
114 LDLM_DEBUG(lock, "client-side local enqueue END");
122 int ldlm_cli_enqueue(struct lustre_handle *connh,
123 struct ptlrpc_request *req,
124 struct ldlm_namespace *ns,
125 struct lustre_handle *parent_lock_handle,
128 void *cookie, int cookielen,
131 ldlm_completion_callback completion,
132 ldlm_blocking_callback blocking,
135 struct lustre_handle *lockh)
137 struct ldlm_lock *lock;
138 struct ldlm_request *body;
139 struct ldlm_reply *reply;
140 int rc, size = sizeof(*body), req_passed_in = 1;
144 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
145 type, cookie, cookielen, mode,
146 flags, completion, blocking, data,
150 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
153 GOTO(out_nolock, rc = -ENOMEM);
154 LDLM_DEBUG(lock, "client-side enqueue START");
155 /* for the local lock, add the reference */
156 ldlm_lock_addref_internal(lock, mode);
157 ldlm_lock2handle(lock, lockh);
160 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
163 GOTO(out, rc = -ENOMEM);
165 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
168 /* Dump all of this data into the request buffer */
169 body = lustre_msg_buf(req->rq_reqmsg, 0);
170 ldlm_lock2desc(lock, &body->lock_desc);
171 /* Phil: make this part of ldlm_lock2desc */
172 if (type == LDLM_EXTENT)
173 memcpy(&body->lock_desc.l_extent, cookie,
174 sizeof(body->lock_desc.l_extent));
175 body->lock_flags = *flags;
177 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
178 if (parent_lock_handle)
179 memcpy(&body->lock_handle2, parent_lock_handle,
180 sizeof(body->lock_handle2));
182 /* Continue as normal. */
183 if (!req_passed_in) {
184 size = sizeof(*reply);
185 req->rq_replen = lustre_msg_size(1, &size);
187 lock->l_connh = connh;
188 lock->l_export = NULL;
190 rc = ptlrpc_queue_wait(req);
191 /* FIXME: status check here? */
192 rc = ptlrpc_check_status(req, rc);
194 if (rc != ELDLM_OK) {
195 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
196 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
197 ldlm_lock_decref(lockh, mode);
198 /* FIXME: if we've already received a completion AST, this will
200 ldlm_lock_destroy(lock);
204 reply = lustre_msg_buf(req->rq_repmsg, 0);
205 memcpy(&lock->l_remote_handle, &reply->lock_handle,
206 sizeof(lock->l_remote_handle));
207 if (type == LDLM_EXTENT)
208 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
209 *flags = reply->lock_flags;
211 CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
212 (void *)(unsigned long)reply->lock_handle.addr, *flags);
213 CDEBUG(D_INFO, "extent: %Lu -> %Lu\n",
214 (unsigned long long)reply->lock_extent.start,
215 (unsigned long long)reply->lock_extent.end);
217 /* If enqueue returned a blocked lock but the completion handler has
218 * already run, then it fixed up the resource and we don't need to do it
220 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
221 int newmode = reply->lock_mode;
222 if (newmode && newmode != lock->l_req_mode) {
223 LDLM_DEBUG(lock, "server returned different mode %s",
224 ldlm_lockname[newmode]);
225 lock->l_req_mode = newmode;
228 if (reply->lock_resource_name[0] !=
229 lock->l_resource->lr_name[0]) {
230 CDEBUG(D_INFO, "remote intent success, locking %ld "
232 (long)reply->lock_resource_name[0],
233 (long)lock->l_resource->lr_name[0]);
235 ldlm_lock_change_resource(lock,
236 reply->lock_resource_name);
237 if (lock->l_resource == NULL) {
241 LDLM_DEBUG(lock, "client-side enqueue, new resource");
246 ptlrpc_free_req(req);
248 rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
250 if (lock->l_completion_ast)
251 lock->l_completion_ast(lock, *flags);
253 LDLM_DEBUG(lock, "client-side enqueue END");
261 int ldlm_match_or_enqueue(struct lustre_handle *connh,
262 struct ptlrpc_request *req,
263 struct ldlm_namespace *ns,
264 struct lustre_handle *parent_lock_handle,
267 void *cookie, int cookielen,
270 ldlm_completion_callback completion,
271 ldlm_blocking_callback blocking,
274 struct lustre_handle *lockh)
278 rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
280 rc = ldlm_cli_enqueue(connh, req, ns,
281 parent_lock_handle, res_id, type, cookie,
282 cookielen, mode, flags, completion,
283 blocking, data, data_len, lockh);
285 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
291 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
295 if (lock->l_resource->lr_namespace->ns_client) {
296 CERROR("Trying to cancel local lock\n");
299 LDLM_DEBUG(lock, "client-side local convert");
301 ldlm_lock_convert(lock, new_mode, flags);
302 ldlm_reprocess_all(lock->l_resource);
304 LDLM_DEBUG(lock, "client-side local convert handler END");
309 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
311 struct ldlm_request *body;
312 struct lustre_handle *connh;
313 struct ldlm_reply *reply;
314 struct ldlm_lock *lock;
315 struct ldlm_resource *res;
316 struct ptlrpc_request *req;
317 int rc, size = sizeof(*body);
320 lock = ldlm_handle2lock(lockh);
326 connh = lock->l_connh;
329 return ldlm_cli_convert_local(lock, new_mode, flags);
331 LDLM_DEBUG(lock, "client-side convert");
333 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
336 GOTO(out, rc = -ENOMEM);
338 body = lustre_msg_buf(req->rq_reqmsg, 0);
339 memcpy(&body->lock_handle1, &lock->l_remote_handle,
340 sizeof(body->lock_handle1));
342 body->lock_desc.l_req_mode = new_mode;
343 body->lock_flags = *flags;
345 size = sizeof(*reply);
346 req->rq_replen = lustre_msg_size(1, &size);
348 rc = ptlrpc_queue_wait(req);
349 rc = ptlrpc_check_status(req, rc);
353 reply = lustre_msg_buf(req->rq_repmsg, 0);
354 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
356 ldlm_reprocess_all(res);
357 /* Go to sleep until the lock is granted. */
358 /* FIXME: or cancelled. */
359 if (lock->l_completion_ast)
360 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
364 ptlrpc_free_req(req);
368 int ldlm_cli_cancel(struct lustre_handle *lockh)
370 struct ptlrpc_request *req;
371 struct ldlm_lock *lock;
372 struct ldlm_request *body;
373 int rc = 0, size = sizeof(*body);
376 lock = ldlm_handle2lock(lockh);
378 /* It's possible that the decref that we did just before this
379 * cancel was the last reader/writer, and caused a cancel before
380 * we could call this function. If we want to make this
381 * impossible (by adding a dec_and_cancel() or similar), then
382 * we can put the LBUG back. */
388 LDLM_DEBUG(lock, "client-side cancel");
389 /* Set this flag to prevent others from getting new references*/
390 l_lock(&lock->l_resource->lr_namespace->ns_lock);
391 lock->l_flags |= LDLM_FL_CBPENDING;
392 ldlm_cancel_callback(lock);
393 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
395 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
396 LDLM_CANCEL, 1, &size, NULL);
398 GOTO(out, rc = -ENOMEM);
400 body = lustre_msg_buf(req->rq_reqmsg, 0);
401 memcpy(&body->lock_handle1, &lock->l_remote_handle,
402 sizeof(body->lock_handle1));
404 req->rq_replen = lustre_msg_size(0, NULL);
406 rc = ptlrpc_queue_wait(req);
407 rc = ptlrpc_check_status(req, rc);
408 ptlrpc_free_req(req);
412 ldlm_lock_cancel(lock);
414 LDLM_DEBUG(lock, "client-side local cancel");
415 if (lock->l_resource->lr_namespace->ns_client) {
416 CERROR("Trying to cancel local lock\n");
419 ldlm_lock_cancel(lock);
420 ldlm_reprocess_all(lock->l_resource);
421 LDLM_DEBUG(lock, "client-side local cancel handler END");
430 /* Cancel all locks on a given resource that have 0 readers/writers.
432 * If 'local_only' is true, throw the locks away without trying to notify the
434 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
437 struct ldlm_resource *res;
438 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
439 struct ldlm_ast_work *w;
442 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
446 l_lock(&ns->ns_lock);
447 list_for_each(tmp, &res->lr_granted) {
448 struct ldlm_lock *lock;
449 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
451 if (lock->l_readers || lock->l_writers)
454 /* Setting the CBPENDING flag is a little misleading, but
455 * prevents an important race; namely, once CBPENDING is set,
456 * the lock can accumulate no more readers/writers. Since
457 * readers and writers are already zero here, ldlm_lock_decref
458 * won't see this flag and call l_blocking_ast */
459 lock->l_flags |= LDLM_FL_CBPENDING;
461 OBD_ALLOC(w, sizeof(*w));
464 w->w_lock = LDLM_LOCK_GET(lock);
465 list_add(&w->w_list, &list);
467 l_unlock(&ns->ns_lock);
469 list_for_each_safe(tmp, next, &list) {
470 struct lustre_handle lockh;
472 w = list_entry(tmp, struct ldlm_ast_work, w_list);
475 ldlm_lock_cancel(w->w_lock);
477 ldlm_lock2handle(w->w_lock, &lockh);
478 rc = ldlm_cli_cancel(&lockh);
480 CERROR("ldlm_cli_cancel: %d\n", rc);
482 LDLM_LOCK_PUT(w->w_lock);
483 list_del(&w->w_list);
484 OBD_FREE(w, sizeof(*w));
487 ldlm_resource_put(res);