1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Hewlett-Packard Development Company LP.
5 * Developed under the sponsorship of the US Government under
6 * Subcontract No. B514193
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
30 #include <lustre_dlm.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_lib.h>
34 #include <libcfs/list.h>
36 #include <liblustre.h>
37 #include <obd_class.h>
40 #include "ldlm_internal.h"
42 #define l_flock_waitq l_lru
44 static struct list_head ldlm_flock_waitq = CFS_LIST_HEAD_INIT(ldlm_flock_waitq);
46 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
47 void *data, int flag);
50 * list_for_remaining_safe - iterate over the remaining entries in a list
51 * and safeguard against removal of a list entry.
52 * @pos: the &struct list_head to use as a loop counter. pos MUST
53 * have been initialized prior to using it in this macro.
54 * @n: another &struct list_head to use as temporary storage
55 * @head: the head for your list.
57 #define list_for_remaining_safe(pos, n, head) \
58 for (n = pos->next; pos != (head); pos = n, n = pos->next)
61 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
63 return((new->l_policy_data.l_flock.pid ==
64 lock->l_policy_data.l_flock.pid) &&
65 (new->l_export == lock->l_export));
69 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
71 return((new->l_policy_data.l_flock.start <=
72 lock->l_policy_data.l_flock.end) &&
73 (new->l_policy_data.l_flock.end >=
74 lock->l_policy_data.l_flock.start));
78 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
82 LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
85 LASSERT(list_empty(&lock->l_flock_waitq));
87 list_del_init(&lock->l_res_link);
88 if (flags == LDLM_FL_WAIT_NOREPROC) {
89 /* client side - set a flag to prevent sending a CANCEL */
90 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
92 /* when reaching here, it is under lock_res_and_lock(). Thus,
93 need call the nolock version of ldlm_lock_decref_internal*/
94 ldlm_lock_decref_internal_nolock(lock, mode);
97 ldlm_lock_destroy_nolock(lock);
102 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
104 struct obd_export *req_export = req->l_export;
105 struct obd_export *blocking_export = blocking_lock->l_export;
106 pid_t req_pid = req->l_policy_data.l_flock.pid;
107 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
108 struct ldlm_lock *lock;
111 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
112 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
113 (lock->l_export != blocking_export))
116 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
117 blocking_export = (struct obd_export *)(long)
118 lock->l_policy_data.l_flock.blocking_export;
119 if (blocking_pid == req_pid && blocking_export == req_export)
129 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
130 ldlm_error_t *err, struct list_head *work_list)
132 struct ldlm_resource *res = req->l_resource;
133 struct ldlm_namespace *ns = res->lr_namespace;
134 struct list_head *tmp;
135 struct list_head *ownlocks = NULL;
136 struct ldlm_lock *lock = NULL;
137 struct ldlm_lock *new = req;
138 struct ldlm_lock *new2 = NULL;
139 ldlm_mode_t mode = req->l_req_mode;
140 int local = ns_is_client(ns);
141 int added = (mode == LCK_NL);
146 CDEBUG(D_DLMTRACE, "flags %#x pid %u mode %u start "LPU64" end "LPU64
147 "\n", *flags, new->l_policy_data.l_flock.pid, mode,
148 req->l_policy_data.l_flock.start,
149 req->l_policy_data.l_flock.end);
154 /* No blocking ASTs are sent to the clients for
155 * Posix file & record locks */
156 req->l_blocking_ast = NULL;
158 /* Called on the server for lock cancels. */
159 req->l_blocking_ast = ldlm_flock_blocking_ast;
163 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
164 /* This loop determines where this processes locks start
165 * in the resource lr_granted list. */
166 list_for_each(tmp, &res->lr_granted) {
167 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
168 if (ldlm_same_flock_owner(lock, req)) {
174 lockmode_verify(mode);
176 /* This loop determines if there are existing locks
177 * that conflict with the new lock request. */
178 list_for_each(tmp, &res->lr_granted) {
179 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
181 if (ldlm_same_flock_owner(lock, req)) {
187 /* locks are compatible, overlap doesn't matter */
188 if (lockmode_compat(lock->l_granted_mode, mode))
191 if (!ldlm_flocks_overlap(lock, req))
195 RETURN(LDLM_ITER_CONTINUE);
197 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
198 ldlm_flock_destroy(req, mode, *flags);
200 RETURN(LDLM_ITER_STOP);
203 if (*flags & LDLM_FL_TEST_LOCK) {
204 ldlm_flock_destroy(req, mode, *flags);
205 req->l_req_mode = lock->l_granted_mode;
206 req->l_policy_data.l_flock.pid =
207 lock->l_policy_data.l_flock.pid;
208 req->l_policy_data.l_flock.start =
209 lock->l_policy_data.l_flock.start;
210 req->l_policy_data.l_flock.end =
211 lock->l_policy_data.l_flock.end;
212 *flags |= LDLM_FL_LOCK_CHANGED;
213 RETURN(LDLM_ITER_STOP);
216 if (ldlm_flock_deadlock(req, lock)) {
217 ldlm_flock_destroy(req, mode, *flags);
219 RETURN(LDLM_ITER_STOP);
222 req->l_policy_data.l_flock.blocking_pid =
223 lock->l_policy_data.l_flock.pid;
224 req->l_policy_data.l_flock.blocking_export =
225 (long)(void *)lock->l_export;
227 LASSERT(list_empty(&req->l_flock_waitq));
228 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
230 ldlm_resource_add_lock(res, &res->lr_waiting, req);
231 *flags |= LDLM_FL_BLOCK_GRANTED;
232 RETURN(LDLM_ITER_STOP);
236 if (*flags & LDLM_FL_TEST_LOCK) {
237 ldlm_flock_destroy(req, mode, *flags);
238 req->l_req_mode = LCK_NL;
239 *flags |= LDLM_FL_LOCK_CHANGED;
240 RETURN(LDLM_ITER_STOP);
243 /* In case we had slept on this lock request take it off of the
244 * deadlock detection waitq. */
245 list_del_init(&req->l_flock_waitq);
247 /* Scan the locks owned by this process that overlap this request.
248 * We may have to merge or split existing locks. */
251 ownlocks = &res->lr_granted;
253 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
254 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
256 if (!ldlm_same_flock_owner(lock, new))
259 if (lock->l_granted_mode == mode) {
260 /* If the modes are the same then we need to process
261 * locks that overlap OR adjoin the new lock. The extra
262 * logic condition is necessary to deal with arithmetic
263 * overflow and underflow. */
264 if ((new->l_policy_data.l_flock.start >
265 (lock->l_policy_data.l_flock.end + 1))
266 && (lock->l_policy_data.l_flock.end !=
270 if ((new->l_policy_data.l_flock.end <
271 (lock->l_policy_data.l_flock.start - 1))
272 && (lock->l_policy_data.l_flock.start != 0))
275 if (new->l_policy_data.l_flock.start <
276 lock->l_policy_data.l_flock.start) {
277 lock->l_policy_data.l_flock.start =
278 new->l_policy_data.l_flock.start;
280 new->l_policy_data.l_flock.start =
281 lock->l_policy_data.l_flock.start;
284 if (new->l_policy_data.l_flock.end >
285 lock->l_policy_data.l_flock.end) {
286 lock->l_policy_data.l_flock.end =
287 new->l_policy_data.l_flock.end;
289 new->l_policy_data.l_flock.end =
290 lock->l_policy_data.l_flock.end;
294 ldlm_flock_destroy(lock, mode, *flags);
302 if (new->l_policy_data.l_flock.start >
303 lock->l_policy_data.l_flock.end)
306 if (new->l_policy_data.l_flock.end <
307 lock->l_policy_data.l_flock.start)
312 if (new->l_policy_data.l_flock.start <=
313 lock->l_policy_data.l_flock.start) {
314 if (new->l_policy_data.l_flock.end <
315 lock->l_policy_data.l_flock.end) {
316 lock->l_policy_data.l_flock.start =
317 new->l_policy_data.l_flock.end + 1;
320 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
323 if (new->l_policy_data.l_flock.end >=
324 lock->l_policy_data.l_flock.end) {
325 lock->l_policy_data.l_flock.end =
326 new->l_policy_data.l_flock.start - 1;
330 /* split the existing lock into two locks */
332 /* if this is an F_UNLCK operation then we could avoid
333 * allocating a new lock and use the req lock passed in
334 * with the request but this would complicate the reply
335 * processing since updates to req get reflected in the
336 * reply. The client side replays the lock request so
337 * it must see the original lock data in the reply. */
339 /* XXX - if ldlm_lock_new() can sleep we should
340 * release the ns_lock, allocate the new lock,
341 * and restart processing this lock. */
343 unlock_res_and_lock(req);
344 new2 = ldlm_lock_create(ns, res->lr_name, LDLM_FLOCK,
345 lock->l_granted_mode, NULL, NULL, NULL,
347 lock_res_and_lock(req);
349 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
351 RETURN(LDLM_ITER_STOP);
358 new2->l_granted_mode = lock->l_granted_mode;
359 new2->l_policy_data.l_flock.pid =
360 new->l_policy_data.l_flock.pid;
361 new2->l_policy_data.l_flock.start =
362 lock->l_policy_data.l_flock.start;
363 new2->l_policy_data.l_flock.end =
364 new->l_policy_data.l_flock.start - 1;
365 lock->l_policy_data.l_flock.start =
366 new->l_policy_data.l_flock.end + 1;
367 new2->l_conn_export = lock->l_conn_export;
368 if (lock->l_export != NULL) {
369 new2->l_export = class_export_get(lock->l_export);
370 spin_lock(&new2->l_export->exp_ldlm_data.led_lock);
371 list_add(&new2->l_export_chain,
372 &new2->l_export->exp_ldlm_data.led_held_locks);
373 spin_unlock(&new2->l_export->exp_ldlm_data.led_lock);
375 if (*flags == LDLM_FL_WAIT_NOREPROC) {
376 ldlm_lock_addref_internal_nolock(new2, lock->l_granted_mode);
379 /* insert new2 at lock */
380 ldlm_resource_add_lock(res, ownlocks, new2);
385 /* if new2 is created but never used, destroy it*/
386 if (splitted == 0 && new2 != NULL)
387 ldlm_lock_destroy_nolock(new2);
389 /* At this point we're granting the lock request. */
390 req->l_granted_mode = req->l_req_mode;
392 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
394 list_del_init(&req->l_res_link);
395 /* insert new lock before ownlocks in list. */
396 ldlm_resource_add_lock(res, ownlocks, req);
399 if (*flags != LDLM_FL_WAIT_NOREPROC) {
401 /* If this is an unlock, reprocess the waitq and
402 * send completions ASTs for locks that can now be
403 * granted. The only problem with doing this
404 * reprocessing here is that the completion ASTs for
405 * newly granted locks will be sent before the unlock
406 * completion is sent. It shouldn't be an issue. Also
407 * note that ldlm_process_flock_lock() will recurse,
408 * but only once because first_enq will be false from
409 * ldlm_reprocess_queue. */
410 if ((mode == LCK_NL) && overlaps) {
411 struct list_head rpc_list
412 = CFS_LIST_HEAD_INIT(rpc_list);
415 ldlm_reprocess_queue(res, &res->lr_waiting,
418 unlock_res_and_lock(req);
419 rc = ldlm_run_cp_ast_work(&rpc_list);
420 lock_res_and_lock(req);
422 GOTO(restart, -ERESTART);
425 LASSERT(req->l_completion_ast);
426 ldlm_add_ast_work_item(req, NULL, work_list);
430 /* In case we're reprocessing the requested lock we can't destroy
431 * it until after calling ldlm_ast_work_item() above so that lawi()
432 * can bump the reference count on req. Otherwise req could be freed
433 * before the completion AST can be sent. */
435 ldlm_flock_destroy(req, mode, *flags);
437 ldlm_resource_dump(D_OTHER, res);
438 RETURN(LDLM_ITER_CONTINUE);
441 struct ldlm_flock_wait_data {
442 struct ldlm_lock *fwd_lock;
447 ldlm_flock_interrupted_wait(void *data)
449 struct ldlm_lock *lock;
450 struct lustre_handle lockh;
454 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
456 /* take lock off the deadlock detection waitq. */
457 list_del_init(&lock->l_flock_waitq);
459 /* client side - set flag to prevent lock from being put on lru list */
460 lock->l_flags |= LDLM_FL_CBPENDING;
462 ldlm_lock_decref_internal(lock, lock->l_req_mode);
463 ldlm_lock2handle(lock, &lockh);
464 rc = ldlm_cli_cancel(&lockh);
466 CERROR("ldlm_cli_cancel: %d\n", rc);
472 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
474 struct ldlm_namespace *ns;
475 cfs_flock_t *getlk = lock->l_ast_data;
476 struct ldlm_flock_wait_data fwd;
477 struct obd_device *obd;
478 struct obd_import *imp = NULL;
481 struct l_wait_info lwi;
484 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
487 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
489 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
490 LDLM_FL_BLOCK_CONV)))
493 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
497 obd = class_exp2obd(lock->l_conn_export);
499 /* if this is a local lock, then there is no import */
501 imp = obd->u.cli.cl_import;
504 spin_lock(&imp->imp_lock);
505 fwd.fwd_generation = imp->imp_generation;
506 spin_unlock(&imp->imp_lock);
509 lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
511 /* Go to sleep until the lock is granted. */
512 rc = l_wait_event(lock->l_waitq,
513 ((lock->l_req_mode == lock->l_granted_mode) ||
514 lock->l_destroyed), &lwi);
516 LDLM_DEBUG(lock, "client-side enqueue waking up: rc = %d", rc);
521 LDLM_DEBUG(lock, "client-side enqueue granted");
522 ns = lock->l_resource->lr_namespace;
523 lock_res_and_lock(lock);
525 /* take lock off the deadlock detection waitq. */
526 list_del_init(&lock->l_flock_waitq);
528 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
529 list_del_init(&lock->l_res_link);
531 if (flags & LDLM_FL_TEST_LOCK) {
532 /* fcntl(F_GETLK) request */
533 /* The old mode was saved in getlk->fl_type so that if the mode
534 * in the lock changes we can decref the approprate refcount. */
535 ldlm_flock_destroy(lock, cfs_flock_type(getlk), LDLM_FL_WAIT_NOREPROC);
536 switch (lock->l_granted_mode) {
538 cfs_flock_set_type(getlk, F_RDLCK);
541 cfs_flock_set_type(getlk, F_WRLCK);
544 cfs_flock_set_type(getlk, F_UNLCK);
546 cfs_flock_set_pid(getlk, (pid_t)lock->l_policy_data.l_flock.pid);
547 cfs_flock_set_start(getlk, (loff_t)lock->l_policy_data.l_flock.start);
548 cfs_flock_set_end(getlk, (loff_t)lock->l_policy_data.l_flock.end);
550 int noreproc = LDLM_FL_WAIT_NOREPROC;
552 /* We need to reprocess the lock to do merges or splits
553 * with existing locks owned by this process. */
554 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
556 cfs_waitq_signal(&lock->l_waitq);
558 unlock_res_and_lock(lock);
561 EXPORT_SYMBOL(ldlm_flock_completion_ast);
563 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
564 void *data, int flag)
566 struct ldlm_namespace *ns;
570 LASSERT(flag == LDLM_CB_CANCELING);
572 ns = lock->l_resource->lr_namespace;
574 /* take lock off the deadlock detection waitq. */
575 lock_res_and_lock(lock);
576 list_del_init(&lock->l_flock_waitq);
577 unlock_res_and_lock(lock);