Whamcloud - gitweb
Fix assertion problem at flock cleanup.
[fs/lustre-release.git] / lustre / ldlm / ldlm_flock.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LDLM
25
26 #ifdef __KERNEL__
27 #include <linux/lustre_dlm.h>
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_lib.h>
31 #include <portals/list.h>
32 #else
33 #include <liblustre.h>
34 #endif
35
36 #include "ldlm_internal.h"
37
38 #define l_flock_waitq   l_lru
39
40 static struct list_head ldlm_flock_waitq = LIST_HEAD_INIT(ldlm_flock_waitq);
41
42 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
43                             void *data, int flag);
44
45 /**
46  * list_for_remaining_safe - iterate over the remaining entries in a list
47  *              and safeguard against removal of a list entry.
48  * @pos:        the &struct list_head to use as a loop counter. pos MUST
49  *              have been initialized prior to using it in this macro.
50  * @n:          another &struct list_head to use as temporary storage
51  * @head:       the head for your list.
52  */
53 #define list_for_remaining_safe(pos, n, head) \
54         for (n = pos->next; pos != (head); pos = n, n = pos->next)
55
56 static inline int
57 ldlm_same_flock_owner(struct ldlm_lock *lock, struct ldlm_lock *new)
58 {
59         return((new->l_policy_data.l_flock.pid ==
60                 lock->l_policy_data.l_flock.pid) &&
61                (new->l_export == lock->l_export));
62 }
63
64 static inline int
65 ldlm_flocks_overlap(struct ldlm_lock *lock, struct ldlm_lock *new)
66 {
67         return((new->l_policy_data.l_flock.start <=
68                 lock->l_policy_data.l_flock.end) &&
69                (new->l_policy_data.l_flock.end >=
70                 lock->l_policy_data.l_flock.start));
71 }
72
73 static inline void
74 ldlm_flock_destroy(struct ldlm_lock *lock, ldlm_mode_t mode, int flags)
75 {
76         ENTRY;
77
78         LDLM_DEBUG(lock, "ldlm_flock_destroy(mode: %d, flags: 0x%x)",
79                    mode, flags);
80
81         LASSERT(list_empty(&lock->l_flock_waitq));
82
83         list_del_init(&lock->l_res_link);
84         if (flags == LDLM_FL_WAIT_NOREPROC) {
85                 /* client side - set a flag to prevent sending a CANCEL */
86                 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_CBPENDING;
87                 ldlm_lock_decref_internal(lock, mode);
88         }
89
90         ldlm_lock_destroy(lock);
91         EXIT;
92 }
93
94 static int
95 ldlm_flock_deadlock(struct ldlm_lock *req, struct ldlm_lock *blocking_lock)
96 {
97         struct obd_export *req_export = req->l_export;
98         struct obd_export *blocking_export = blocking_lock->l_export;
99         pid_t req_pid = req->l_policy_data.l_flock.pid;
100         pid_t blocking_pid = blocking_lock->l_policy_data.l_flock.pid;
101         struct ldlm_lock *lock;
102
103 restart:
104         list_for_each_entry(lock, &ldlm_flock_waitq, l_flock_waitq) {
105                 if ((lock->l_policy_data.l_flock.pid != blocking_pid) ||
106                     (lock->l_export != blocking_export))
107                         continue;
108
109                 blocking_pid = lock->l_policy_data.l_flock.blocking_pid;
110                 blocking_export = (struct obd_export *)(long)
111                         lock->l_policy_data.l_flock.blocking_export;
112                 if (blocking_pid == req_pid && blocking_export == req_export)
113                         return 1;
114
115                 goto restart;
116         }
117
118         return 0;
119 }
120
121 int
122 ldlm_process_flock_lock(struct ldlm_lock *req, int *flags, int first_enq,
123                         ldlm_error_t *err)
124 {
125         struct ldlm_resource *res = req->l_resource;
126         struct ldlm_namespace *ns = res->lr_namespace;
127         struct list_head *tmp;
128         struct list_head *ownlocks = NULL;
129         struct ldlm_lock *lock = NULL;
130         struct ldlm_lock *new = req;
131         struct ldlm_lock *new2 = NULL;
132         ldlm_mode_t mode = req->l_req_mode;
133         int local = ns->ns_client;
134         int added = (mode == LCK_NL);
135         int overlaps = 0;
136         ENTRY;
137
138         CDEBUG(D_DLMTRACE, "flags %#x pid "LPU64" mode %u start "LPU64" end "
139                LPU64"\n", *flags, new->l_policy_data.l_flock.pid, mode,
140                req->l_policy_data.l_flock.start,
141                req->l_policy_data.l_flock.end);
142
143         *err = ELDLM_OK;
144
145         if (local) {
146                 /* No blocking ASTs are sent to the clients for
147                  * Posix file & record locks */
148                 req->l_blocking_ast = NULL;
149         } else {
150                 /* Called on the server for lock cancels. */
151                 req->l_blocking_ast = ldlm_flock_blocking_ast;
152         }
153
154         if ((*flags == LDLM_FL_WAIT_NOREPROC) || (mode == LCK_NL)) {
155                 /* This loop determines where this processes locks start
156                  * in the resource lr_granted list. */
157                 list_for_each(tmp, &res->lr_granted) {
158                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
159                         if (ldlm_same_flock_owner(lock, req)) {
160                                 ownlocks = tmp;
161                                 break;
162                         }
163                 }
164         } else {
165                 lockmode_verify(mode);
166
167                 /* This loop determines if there are existing locks
168                  * that conflict with the new lock request. */
169                 list_for_each(tmp, &res->lr_granted) {
170                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
171
172                         if (ldlm_same_flock_owner(lock, req)) {
173                                 if (!ownlocks)
174                                         ownlocks = tmp;
175                                 continue;
176                         }
177
178                         /* locks are compatible, overlap doesn't matter */
179                         if (lockmode_compat(lock->l_granted_mode, mode))
180                                 continue;
181
182                         if (!ldlm_flocks_overlap(lock, req))
183                                 continue;
184
185                         if (!first_enq)
186                                 RETURN(LDLM_ITER_CONTINUE);
187
188                         if (*flags & LDLM_FL_BLOCK_NOWAIT) {
189                                 ldlm_flock_destroy(req, mode, *flags);
190                                 *err = -EAGAIN;
191                                 RETURN(LDLM_ITER_STOP);
192                         }
193
194                         if (*flags & LDLM_FL_TEST_LOCK) {
195                                 ldlm_flock_destroy(req, mode, *flags);
196                                 req->l_req_mode = lock->l_granted_mode;
197                                 req->l_policy_data.l_flock.pid =
198                                         lock->l_policy_data.l_flock.pid;
199                                 req->l_policy_data.l_flock.start =
200                                         lock->l_policy_data.l_flock.start;
201                                 req->l_policy_data.l_flock.end =
202                                         lock->l_policy_data.l_flock.end;
203                                 *flags |= LDLM_FL_LOCK_CHANGED;
204                                 RETURN(LDLM_ITER_STOP);
205                         }
206
207                         if (ldlm_flock_deadlock(req, lock)) {
208                                 ldlm_flock_destroy(req, mode, *flags);
209                                 *err = -EDEADLK;
210                                 RETURN(LDLM_ITER_STOP);
211                         }
212
213                         req->l_policy_data.l_flock.blocking_pid =
214                                 lock->l_policy_data.l_flock.pid;
215                         req->l_policy_data.l_flock.blocking_export =
216                                 (long)(void *)lock->l_export;
217
218                         LASSERT(list_empty(&req->l_flock_waitq));
219                         list_add_tail(&req->l_flock_waitq, &ldlm_flock_waitq);
220
221                         ldlm_resource_add_lock(res, &res->lr_waiting, req);
222                         *flags |= LDLM_FL_BLOCK_GRANTED;
223                         RETURN(LDLM_ITER_STOP);
224                 }
225         }
226
227         if (*flags & LDLM_FL_TEST_LOCK) {
228                 ldlm_flock_destroy(req, mode, *flags);
229                 req->l_req_mode = LCK_NL;
230                 *flags |= LDLM_FL_LOCK_CHANGED;
231                 RETURN(LDLM_ITER_STOP);
232         }
233
234         /* In case we had slept on this lock request take it off of the
235          * deadlock detection waitq. */
236         list_del_init(&req->l_flock_waitq);
237
238         /* Scan the locks owned by this process that overlap this request.
239          * We may have to merge or split existing locks. */
240
241         if (!ownlocks)
242                 ownlocks = &res->lr_granted;
243
244         list_for_remaining_safe(ownlocks, tmp, &res->lr_granted) {
245                 lock = list_entry(ownlocks, struct ldlm_lock, l_res_link);
246
247                 if (!ldlm_same_flock_owner(lock, new))
248                         break;
249
250                 if (lock->l_granted_mode == mode) {
251                         /* If the modes are the same then we need to process
252                          * locks that overlap OR adjoin the new lock. The extra
253                          * logic condition is necessary to deal with arithmetic
254                          * overflow and underflow. */
255                         if ((new->l_policy_data.l_flock.start >
256                              (lock->l_policy_data.l_flock.end + 1))
257                             && (lock->l_policy_data.l_flock.end != ~0))
258                                 continue;
259
260                         if ((new->l_policy_data.l_flock.end <
261                              (lock->l_policy_data.l_flock.start - 1))
262                             && (lock->l_policy_data.l_flock.start != 0))
263                                 break;
264
265                         if (new->l_policy_data.l_flock.start <
266                             lock->l_policy_data.l_flock.start) {
267                                 lock->l_policy_data.l_flock.start =
268                                         new->l_policy_data.l_flock.start;
269                         } else {
270                                 new->l_policy_data.l_flock.start =
271                                         lock->l_policy_data.l_flock.start;
272                         }
273
274                         if (new->l_policy_data.l_flock.end >
275                             lock->l_policy_data.l_flock.end) {
276                                 lock->l_policy_data.l_flock.end =
277                                         new->l_policy_data.l_flock.end;
278                         } else {
279                                 new->l_policy_data.l_flock.end =
280                                         lock->l_policy_data.l_flock.end;
281                         }
282
283                         if (added) {
284                                 ldlm_flock_destroy(lock, mode, *flags);
285                         } else {
286                                 new = lock;
287                                 added = 1;
288                         }
289                         continue;
290                 }
291
292                 if (new->l_policy_data.l_flock.start >
293                     lock->l_policy_data.l_flock.end)
294                         continue;
295
296                 if (new->l_policy_data.l_flock.end <
297                     lock->l_policy_data.l_flock.start)
298                         break;
299
300                 ++overlaps;
301
302                 if (new->l_policy_data.l_flock.start <=
303                     lock->l_policy_data.l_flock.start) {
304                         if (new->l_policy_data.l_flock.end <
305                             lock->l_policy_data.l_flock.end) {
306                                 lock->l_policy_data.l_flock.start =
307                                         new->l_policy_data.l_flock.end + 1;
308                                 break;
309                         }
310                         ldlm_flock_destroy(lock, lock->l_req_mode, *flags);
311                         continue;
312                 }
313                 if (new->l_policy_data.l_flock.end >=
314                     lock->l_policy_data.l_flock.end) {
315                         lock->l_policy_data.l_flock.end =
316                                 new->l_policy_data.l_flock.start - 1;
317                         continue;
318                 }
319
320                 /* split the existing lock into two locks */
321
322                 /* if this is an F_UNLCK operation then we could avoid
323                  * allocating a new lock and use the req lock passed in
324                  * with the request but this would complicate the reply
325                  * processing since updates to req get reflected in the
326                  * reply. The client side replays the lock request so
327                  * it must see the original lock data in the reply. */
328
329                 /* XXX - if ldlm_lock_new() can sleep we should
330                  * release the ns_lock, allocate the new lock,
331                  * and restart processing this lock. */
332                 new2 = ldlm_lock_create(ns, NULL, res->lr_name, LDLM_FLOCK,
333                                         lock->l_granted_mode, NULL, NULL, NULL,
334                                         NULL, 0);
335                 if (!new2) {
336                         ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
337                         *err = -ENOLCK;
338                         RETURN(LDLM_ITER_STOP);
339                 }
340
341                 new2->l_granted_mode = lock->l_granted_mode;
342                 new2->l_policy_data.l_flock.pid =
343                         new->l_policy_data.l_flock.pid;
344                 new2->l_policy_data.l_flock.start =
345                         lock->l_policy_data.l_flock.start;
346                 new2->l_policy_data.l_flock.end =
347                         new->l_policy_data.l_flock.start - 1;
348                 lock->l_policy_data.l_flock.start =
349                         new->l_policy_data.l_flock.end + 1;
350                 new2->l_conn_export = lock->l_conn_export;
351                 if (lock->l_export != NULL) {
352                         new2->l_export = class_export_get(lock->l_export);
353                         list_add(&new2->l_export_chain,
354                                  &new2->l_export->exp_ldlm_data.led_held_locks);
355                 }
356                 if (*flags == LDLM_FL_WAIT_NOREPROC)
357                         ldlm_lock_addref_internal(new2, lock->l_granted_mode);
358
359                 /* insert new2 at lock */
360                 ldlm_resource_add_lock(res, ownlocks, new2);
361                 LDLM_LOCK_PUT(new2);
362                 break;
363         }
364
365         /* At this point we're granting the lock request. */
366         req->l_granted_mode = req->l_req_mode;
367
368         /* Add req to the granted queue before calling ldlm_reprocess_all(). */
369         if (!added) {
370                 list_del_init(&req->l_res_link);
371                 /* insert new lock before ownlocks in list. */
372                 ldlm_resource_add_lock(res, ownlocks, req);
373         }
374
375         if (*flags != LDLM_FL_WAIT_NOREPROC) {
376                 if (first_enq) {
377                         /* If this is an unlock, reprocess the waitq and
378                          * send completions ASTs for locks that can now be 
379                          * granted. The only problem with doing this
380                          * reprocessing here is that the completion ASTs for
381                          * newly granted locks will be sent before the unlock
382                          * completion is sent. It shouldn't be an issue. Also
383                          * note that ldlm_process_flock_lock() will recurse,
384                          * but only once because first_enq will be false from
385                          * ldlm_reprocess_queue. */
386                         if ((mode == LCK_NL) && overlaps) {
387                                 struct list_head rpc_list
388                                                     = LIST_HEAD_INIT(rpc_list);
389                                 int rc;
390 restart:
391                                 res->lr_tmp = &rpc_list;
392                                 ldlm_reprocess_queue(res, &res->lr_waiting);
393                                 res->lr_tmp = NULL;
394
395                                 l_unlock(&ns->ns_lock);
396                                 rc = ldlm_run_ast_work(res->lr_namespace,
397                                                        &rpc_list);
398                                 l_lock(&ns->ns_lock);
399                                 if (rc == -ERESTART)
400                                         GOTO(restart, -ERESTART);
401                        }
402                 } else {
403                         LASSERT(req->l_completion_ast);
404                         ldlm_add_ast_work_item(req, NULL, NULL, 0);
405                 }
406         }
407
408         /* In case we're reprocessing the requested lock we can't destroy
409          * it until after calling ldlm_ast_work_item() above so that lawi()
410          * can bump the reference count on req. Otherwise req could be freed
411          * before the completion AST can be sent.  */
412         if (added)
413                 ldlm_flock_destroy(req, mode, *flags);
414
415         ldlm_resource_dump(res);
416         RETURN(LDLM_ITER_CONTINUE);
417 }
418
419 struct ldlm_flock_wait_data {
420         struct ldlm_lock *fwd_lock;
421         int               fwd_generation;
422 };
423
424 static void
425 ldlm_flock_interrupted_wait(void *data)
426 {
427         struct ldlm_lock *lock;
428         struct lustre_handle lockh;
429         int rc;
430         ENTRY;
431
432         lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
433
434         /* take lock off the deadlock detection waitq. */
435         list_del_init(&lock->l_flock_waitq);
436
437         ldlm_lock_decref_internal(lock, lock->l_req_mode);
438         ldlm_lock2handle(lock, &lockh);
439         rc = ldlm_cli_cancel(&lockh);
440         EXIT;
441 }
442
443 int
444 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
445 {
446         struct ldlm_namespace *ns;
447         struct file_lock *getlk = lock->l_ast_data;
448         struct ldlm_flock_wait_data fwd;
449         unsigned long irqflags;
450         struct obd_device *obd;
451         struct obd_import *imp = NULL;
452         ldlm_error_t err;
453         int rc = 0;
454         struct l_wait_info lwi;
455         ENTRY;
456
457         CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
458                flags, data, getlk);
459
460         LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
461
462         if (flags == 0) {
463                 wake_up(&lock->l_waitq);
464                 RETURN(0);
465         }
466
467         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
468                        LDLM_FL_BLOCK_CONV)))
469                 goto  granted;
470
471         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
472                    "sleeping");
473
474         ldlm_lock_dump(D_DLMTRACE, lock, 0);
475
476         fwd.fwd_lock = lock;
477         obd = class_exp2obd(lock->l_conn_export);
478
479         /* if this is a local lock, then there is no import */
480         if (obd != NULL)
481                 imp = obd->u.cli.cl_import;
482
483         if (imp != NULL) {
484                 spin_lock_irqsave(&imp->imp_lock, irqflags);
485                 fwd.fwd_generation = imp->imp_generation;
486                 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
487         }
488
489         lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
490
491         /* Go to sleep until the lock is granted. */
492         rc = l_wait_event(lock->l_waitq,
493                           ((lock->l_req_mode == lock->l_granted_mode) ||
494                            lock->l_destroyed), &lwi);
495
496         if (rc) {
497                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
498                            rc);
499                 RETURN(rc);
500         }
501
502         LASSERT(!(lock->l_destroyed));
503
504 granted:
505
506         LDLM_DEBUG(lock, "client-side enqueue waking up");
507         ns = lock->l_resource->lr_namespace;
508         l_lock(&ns->ns_lock);
509
510         /* take lock off the deadlock detection waitq. */
511         list_del_init(&lock->l_flock_waitq);
512
513         /* ldlm_lock_enqueue() has already placed lock on the granted list. */
514         list_del_init(&lock->l_res_link);
515
516         if (flags & LDLM_FL_TEST_LOCK) {
517                 /* fcntl(F_GETLK) request */
518                 /* The old mode was saved in getlk->fl_type so that if the mode
519                  * in the lock changes we can decref the approprate refcount. */
520                 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
521                 switch (lock->l_granted_mode) {
522                 case LCK_PR:
523                         getlk->fl_type = F_RDLCK;
524                         break;
525                 case LCK_PW:
526                         getlk->fl_type = F_WRLCK;
527                         break;
528                 default:
529                         getlk->fl_type = F_UNLCK;
530                 }
531                 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
532                 getlk->fl_start = lock->l_policy_data.l_flock.start;
533                 getlk->fl_end = lock->l_policy_data.l_flock.end;
534         } else {
535                 /* We need to reprocess the lock to do merges or splits
536                  * with existing locks owned by this process. */
537                 flags = LDLM_FL_WAIT_NOREPROC;
538                 ldlm_process_flock_lock(lock, &flags, 1, &err);
539         }
540         l_unlock(&ns->ns_lock);
541         RETURN(0);
542 }
543
544 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
545                             void *data, int flag)
546 {
547         struct ldlm_namespace *ns;
548         ENTRY;
549
550         LASSERT(lock);
551         LASSERT(flag == LDLM_CB_CANCELING);
552
553         ns = lock->l_resource->lr_namespace;
554         
555         /* take lock off the deadlock detection waitq. */
556         l_lock(&ns->ns_lock);
557         list_del_init(&lock->l_flock_waitq);
558         l_unlock(&ns->ns_lock);
559         RETURN(0);
560 }