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