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