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;
91 ldlm_lock_decref_internal(lock, mode);
94 ldlm_lock_destroy_nolock(lock);
99 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
101 struct obd_export *req_export = req->l_export;
102 struct obd_export *blocking_export = blocking_lock->l_export;
103 pid_t req_pid = req->l_policy_data.l_flock.pid;
104 pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
105 struct ldlm_lock *lock;
108 list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
109 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
110 (lock->l_export != blocking_export))
113 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
114 blocking_export = (struct obd_export *)(long)
115 lock->l_policy_data.l_flock.blocking_export;
116 if (blocking_pid == req_pid && blocking_export == req_export)
126 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
127 ldlm_error_t *err, struct list_head *work_list)
129 struct ldlm_resource *res = req->l_resource;
130 struct ldlm_namespace *ns = res->lr_namespace;
131 struct list_head *tmp;
132 struct list_head *ownlocks = NULL;
133 struct ldlm_lock *lock = NULL;
134 struct ldlm_lock *new = req;
135 struct ldlm_lock *new2 = NULL;
136 ldlm_mode_t mode = req->l_req_mode;
137 int local = ns->ns_client;
138 int added = (mode == LCK_NL);
142 CDEBUG(D_DLMTRACE, "flags %#x pid %u mode %u start "LPU64" end "LPU64
143 "\n", *flags, new->l_policy_data.l_flock.pid, mode,
144 req->l_policy_data.l_flock.start,
145 req->l_policy_data.l_flock.end);
150 /* No blocking ASTs are sent to the clients for
151 * Posix file & record locks */
152 req->l_blocking_ast = NULL;
154 /* Called on the server for lock cancels. */
155 req->l_blocking_ast = ldlm_flock_blocking_ast;
158 if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
159 /* This loop determines where this processes locks start
160 * in the resource lr_granted list. */
161 list_for_each(tmp, &res->lr_granted) {
162 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
163 if (ldlm_same_flock_owner(lock, req)) {
169 lockmode_verify(mode);
171 /* This loop determines if there are existing locks
172 * that conflict with the new lock request. */
173 list_for_each(tmp, &res->lr_granted) {
174 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
176 if (ldlm_same_flock_owner(lock, req)) {
182 /* locks are compatible, overlap doesn't matter */
183 if (lockmode_compat(lock->l_granted_mode, mode))
186 if (!ldlm_flocks_overlap(lock, req))
190 RETURN(LDLM_ITER_CONTINUE);
192 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
193 ldlm_flock_destroy(req, mode, *flags);
195 RETURN(LDLM_ITER_STOP);
198 if (*flags & LDLM_FL_TEST_LOCK) {
199 ldlm_flock_destroy(req, mode, *flags);
200 req->l_req_mode = lock->l_granted_mode;
201 req->l_policy_data.l_flock.pid =
202 lock->l_policy_data.l_flock.pid;
203 req->l_policy_data.l_flock.start =
204 lock->l_policy_data.l_flock.start;
205 req->l_policy_data.l_flock.end =
206 lock->l_policy_data.l_flock.end;
207 *flags |= LDLM_FL_LOCK_CHANGED;
208 RETURN(LDLM_ITER_STOP);
211 if (ldlm_flock_deadlock(req, lock)) {
212 ldlm_flock_destroy(req, mode, *flags);
214 RETURN(LDLM_ITER_STOP);
217 req->l_policy_data.l_flock.blocking_pid =
218 lock->l_policy_data.l_flock.pid;
219 req->l_policy_data.l_flock.blocking_export =
220 (long)(void *)lock->l_export;
222 LASSERT(list_empty(&req->l_flock_waitq));
223 list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
225 ldlm_resource_add_lock(res, &res->lr_waiting, req);
226 *flags |= LDLM_FL_BLOCK_GRANTED;
227 RETURN(LDLM_ITER_STOP);
231 if (*flags & LDLM_FL_TEST_LOCK) {
232 ldlm_flock_destroy(req, mode, *flags);
233 req->l_req_mode = LCK_NL;
234 *flags |= LDLM_FL_LOCK_CHANGED;
235 RETURN(LDLM_ITER_STOP);
238 /* In case we had slept on this lock request take it off of the
239 * deadlock detection waitq. */
240 list_del_init(&req->l_flock_waitq);
242 /* Scan the locks owned by this process that overlap this request.
243 * We may have to merge or split existing locks. */
246 ownlocks = &res->lr_granted;
248 list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
249 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
251 if (!ldlm_same_flock_owner(lock, new))
254 if (lock->l_granted_mode == mode) {
255 /* If the modes are the same then we need to process
256 * locks that overlap OR adjoin the new lock. The extra
257 * logic condition is necessary to deal with arithmetic
258 * overflow and underflow. */
259 if ((new->l_policy_data.l_flock.start >
260 (lock->l_policy_data.l_flock.end + 1))
261 && (lock->l_policy_data.l_flock.end !=
265 if ((new->l_policy_data.l_flock.end <
266 (lock->l_policy_data.l_flock.start - 1))
267 && (lock->l_policy_data.l_flock.start != 0))
270 if (new->l_policy_data.l_flock.start <
271 lock->l_policy_data.l_flock.start) {
272 lock->l_policy_data.l_flock.start =
273 new->l_policy_data.l_flock.start;
275 new->l_policy_data.l_flock.start =
276 lock->l_policy_data.l_flock.start;
279 if (new->l_policy_data.l_flock.end >
280 lock->l_policy_data.l_flock.end) {
281 lock->l_policy_data.l_flock.end =
282 new->l_policy_data.l_flock.end;
284 new->l_policy_data.l_flock.end =
285 lock->l_policy_data.l_flock.end;
289 ldlm_flock_destroy(lock, mode, *flags);
297 if (new->l_policy_data.l_flock.start >
298 lock->l_policy_data.l_flock.end)
301 if (new->l_policy_data.l_flock.end <
302 lock->l_policy_data.l_flock.start)
307 if (new->l_policy_data.l_flock.start <=
308 lock->l_policy_data.l_flock.start) {
309 if (new->l_policy_data.l_flock.end <
310 lock->l_policy_data.l_flock.end) {
311 lock->l_policy_data.l_flock.start =
312 new->l_policy_data.l_flock.end + 1;
315 ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
318 if (new->l_policy_data.l_flock.end >=
319 lock->l_policy_data.l_flock.end) {
320 lock->l_policy_data.l_flock.end =
321 new->l_policy_data.l_flock.start - 1;
325 /* split the existing lock into two locks */
327 /* if this is an F_UNLCK operation then we could avoid
328 * allocating a new lock and use the req lock passed in
329 * with the request but this would complicate the reply
330 * processing since updates to req get reflected in the
331 * reply. The client side replays the lock request so
332 * it must see the original lock data in the reply. */
334 /* XXX - if ldlm_lock_new() can sleep we should
335 * release the ns_lock, allocate the new lock,
336 * and restart processing this lock. */
337 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
338 lock->l_granted_mode, NULL, NULL, NULL,
341 ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
343 RETURN(LDLM_ITER_STOP);
346 new2->l_granted_mode = lock->l_granted_mode;
347 new2->l_policy_data.l_flock.pid =
348 new->l_policy_data.l_flock.pid;
349 new2->l_policy_data.l_flock.start =
350 lock->l_policy_data.l_flock.start;
351 new2->l_policy_data.l_flock.end =
352 new->l_policy_data.l_flock.start - 1;
353 lock->l_policy_data.l_flock.start =
354 new->l_policy_data.l_flock.end + 1;
355 new2->l_conn_export = lock->l_conn_export;
356 if (lock->l_export != NULL) {
357 new2->l_export = class_export_get(lock->l_export);
358 list_add(&new2->l_export_chain,
359 &new2->l_export->exp_ldlm_data.led_held_locks);
361 if (*flags == LDLM_FL_WAIT_NOREPROC)
362 ldlm_lock_addref_internal(new2, lock->l_granted_mode);
364 /* insert new2 at lock */
365 ldlm_resource_add_lock(res, ownlocks, new2);
370 /* At this point we're granting the lock request. */
371 req->l_granted_mode = req->l_req_mode;
373 /* Add req to the granted queue before calling ldlm_reprocess_all(). */
375 list_del_init(&req->l_res_link);
376 /* insert new lock before ownlocks in list. */
377 ldlm_resource_add_lock(res, ownlocks, req);
380 if (*flags != LDLM_FL_WAIT_NOREPROC) {
382 /* If this is an unlock, reprocess the waitq and
383 * send completions ASTs for locks that can now be
384 * granted. The only problem with doing this
385 * reprocessing here is that the completion ASTs for
386 * newly granted locks will be sent before the unlock
387 * completion is sent. It shouldn't be an issue. Also
388 * note that ldlm_process_flock_lock() will recurse,
389 * but only once because first_enq will be false from
390 * ldlm_reprocess_queue. */
391 if ((mode == LCK_NL) && overlaps) {
392 struct list_head rpc_list
393 = CFS_LIST_HEAD_INIT(rpc_list);
396 ldlm_reprocess_queue(res, &res->lr_waiting,
400 rc = ldlm_run_bl_ast_work(&rpc_list);
403 GOTO(restart, -ERESTART);
406 LASSERT(req->l_completion_ast);
407 ldlm_add_ast_work_item(req, NULL, work_list);
411 /* In case we're reprocessing the requested lock we can't destroy
412 * it until after calling ldlm_ast_work_item() above so that lawi()
413 * can bump the reference count on req. Otherwise req could be freed
414 * before the completion AST can be sent. */
416 ldlm_flock_destroy(req, mode, *flags);
418 ldlm_resource_dump(D_OTHER, res);
419 RETURN(LDLM_ITER_CONTINUE);
422 struct ldlm_flock_wait_data {
423 struct ldlm_lock *fwd_lock;
428 ldlm_flock_interrupted_wait(void *data)
430 struct ldlm_lock *lock;
431 struct lustre_handle lockh;
434 lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
436 /* take lock off the deadlock detection waitq. */
437 list_del_init(&lock->l_flock_waitq);
439 /* client side - set flag to prevent lock from being put on lru list */
440 lock->l_flags |= LDLM_FL_CBPENDING;
442 ldlm_lock_decref_internal(lock, lock->l_req_mode);
443 ldlm_lock2handle(lock, &lockh);
444 ldlm_cli_cancel(&lockh);
449 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
451 struct ldlm_namespace *ns;
452 cfs_flock_t *getlk = lock->l_ast_data;
453 struct ldlm_flock_wait_data fwd;
454 struct obd_device *obd;
455 struct obd_import *imp = NULL;
458 struct l_wait_info lwi;
461 CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
464 LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
466 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
467 LDLM_FL_BLOCK_CONV)))
470 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
474 obd = class_exp2obd(lock->l_conn_export);
476 /* if this is a local lock, then there is no import */
478 imp = obd->u.cli.cl_import;
481 spin_lock(&imp->imp_lock);
482 fwd.fwd_generation = imp->imp_generation;
483 spin_unlock(&imp->imp_lock);
486 lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
488 /* Go to sleep until the lock is granted. */
489 rc = l_wait_event(lock->l_waitq,
490 ((lock->l_req_mode == lock->l_granted_mode) ||
491 lock->l_destroyed), &lwi);
493 LDLM_DEBUG(lock, "client-side enqueue waking up: rc = %d", rc);
498 LDLM_DEBUG(lock, "client-side enqueue granted");
499 ns = lock->l_resource->lr_namespace;
500 lock_res(lock->l_resource);
502 /* take lock off the deadlock detection waitq. */
503 list_del_init(&lock->l_flock_waitq);
505 /* ldlm_lock_enqueue() has already placed lock on the granted list. */
506 list_del_init(&lock->l_res_link);
508 if (flags & LDLM_FL_TEST_LOCK) {
509 /* fcntl(F_GETLK) request */
510 /* The old mode was saved in getlk->fl_type so that if the mode
511 * in the lock changes we can decref the approprate refcount. */
512 ldlm_flock_destroy(lock, cfs_flock_type(getlk), LDLM_FL_WAIT_NOREPROC);
513 switch (lock->l_granted_mode) {
515 cfs_flock_set_type(getlk, F_RDLCK);
518 cfs_flock_set_type(getlk, F_WRLCK);
521 cfs_flock_set_type(getlk, F_UNLCK);
523 cfs_flock_set_pid(getlk, (pid_t)lock->l_policy_data.l_flock.pid);
524 cfs_flock_set_start(getlk, (off_t)lock->l_policy_data.l_flock.start);
525 cfs_flock_set_end(getlk, (off_t)lock->l_policy_data.l_flock.end);
527 int noreproc = LDLM_FL_WAIT_NOREPROC;
529 /* We need to reprocess the lock to do merges or splits
530 * with existing locks owned by this process. */
531 ldlm_process_flock_lock(lock, &noreproc, 1, &err, NULL);
533 cfs_waitq_signal(&lock->l_waitq);
535 unlock_res(lock->l_resource);
538 EXPORT_SYMBOL(ldlm_flock_completion_ast);
540 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
541 void *data, int flag)
543 struct ldlm_namespace *ns;
547 LASSERT(flag == LDLM_CB_CANCELING);
549 ns = lock->l_resource->lr_namespace;
551 /* take lock off the deadlock detection waitq. */
552 lock_res_and_lock(lock);
553 list_del_init(&lock->l_flock_waitq);
554 unlock_res_and_lock(lock);