Whamcloud - gitweb
b=1021,2720
[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                                         NULL, 0);
317                 if (!new2) {
318                         ldlm_flock_destroy(req, lock->l_granted_mode, *flags);
319                         *err = -ENOLCK;
320                         RETURN(LDLM_ITER_STOP);
321                 }
322
323                 new2->l_granted_mode = lock->l_granted_mode;
324                 new2->l_policy_data.l_flock.pid =
325                         new->l_policy_data.l_flock.pid;
326                 new2->l_policy_data.l_flock.start =
327                         lock->l_policy_data.l_flock.start;
328                 new2->l_policy_data.l_flock.end =
329                         new->l_policy_data.l_flock.start - 1;
330                 lock->l_policy_data.l_flock.start =
331                         new->l_policy_data.l_flock.end + 1;
332                 new2->l_conn_export = lock->l_conn_export;
333                 if (lock->l_export != NULL) {
334                         new2->l_export = class_export_get(lock->l_export);
335                         list_add(&new2->l_export_chain,
336                                  &new2->l_export->exp_ldlm_data.led_held_locks);
337                 }
338                 if (*flags == LDLM_FL_WAIT_NOREPROC)
339                         ldlm_lock_addref_internal(new2, lock->l_granted_mode);
340
341                 /* insert new2 at lock */
342                 ldlm_resource_add_lock(res, ownlocks, new2);
343                 LDLM_LOCK_PUT(new2);
344                 break;
345         }
346
347         /* Add req to the granted queue before calling ldlm_reprocess_all(). */
348         if (!added) {
349                 req->l_granted_mode = req->l_req_mode;
350                 list_del_init(&req->l_res_link);
351                 /* insert new lock before ownlocks in list. */
352                 ldlm_resource_add_lock(res, ownlocks, req);
353         }
354
355         if (*flags != LDLM_FL_WAIT_NOREPROC) {
356                 if (first_enq) {
357                         /* If this is an unlock, reprocess the waitq and
358                          * send completions ASTs for locks that can now be 
359                          * granted. The only problem with doing this
360                          * reprocessing here is that the completion ASTs for
361                          * newly granted locks will be sent before the unlock
362                          * completion is sent. It shouldn't be an issue. Also
363                          * note that ldlm_process_flock_lock() will recurse,
364                          * but only once because first_enq will be false from
365                          * ldlm_reprocess_queue. */
366                         if ((mode == LCK_NL) && overlaps) {
367                                 struct list_head rpc_list
368                                                     = LIST_HEAD_INIT(rpc_list);
369                                 int rc;
370 restart:
371                                 res->lr_tmp = &rpc_list;
372                                 ldlm_reprocess_queue(res, &res->lr_waiting);
373                                 res->lr_tmp = NULL;
374
375                                 l_unlock(&ns->ns_lock);
376                                 rc = ldlm_run_ast_work(res->lr_namespace,
377                                                        &rpc_list);
378                                 l_lock(&ns->ns_lock);
379                                 if (rc == -ERESTART)
380                                         GOTO(restart, -ERESTART);
381                        }
382                 } else {
383                         LASSERT(req->l_completion_ast);
384                         ldlm_add_ast_work_item(req, NULL, NULL, 0);
385                 }
386         }
387
388         /* In case we're reprocessing the requested lock we can't destroy
389          * it until after calling ldlm_ast_work_item() above so that lawi()
390          * can bump the reference count on req. Otherwise req could be freed
391          * before the completion AST can be sent.  */
392         if (added)
393                 ldlm_flock_destroy(req, mode, *flags);
394
395         ldlm_resource_dump(res);
396         RETURN(LDLM_ITER_CONTINUE);
397 }
398
399 struct ldlm_flock_wait_data {
400         struct ldlm_lock *fwd_lock;
401         int               fwd_generation;
402 };
403
404 static void
405 ldlm_flock_interrupted_wait(void *data)
406 {
407         struct ldlm_lock *lock;
408         struct lustre_handle lockh;
409         int rc;
410         ENTRY;
411
412         lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
413         ldlm_lock_decref_internal(lock, lock->l_req_mode);
414         ldlm_lock2handle(lock, &lockh);
415         rc = ldlm_cli_cancel(&lockh);
416         CDEBUG(D_DLMTRACE, "ldlm_cli_cancel: %d\n", rc);
417         EXIT;
418 }
419
420 int
421 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
422 {
423         struct ldlm_namespace *ns;
424         struct file_lock *getlk = lock->l_ast_data;
425         struct ldlm_flock_wait_data fwd;
426         unsigned long irqflags;
427         struct obd_device *obd;
428         struct obd_import *imp = NULL;
429         ldlm_error_t err;
430         int rc = 0;
431         struct l_wait_info lwi;
432         ENTRY;
433
434         CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
435                flags, data, getlk);
436
437         LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
438
439         if (flags == 0) {
440                 wake_up(&lock->l_waitq);
441                 RETURN(0);
442         }
443
444         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
445                        LDLM_FL_BLOCK_CONV)))
446                 goto  granted;
447
448         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
449                    "sleeping");
450
451         ldlm_lock_dump(D_OTHER, lock, 0);
452
453         fwd.fwd_lock = lock;
454         obd = class_exp2obd(lock->l_conn_export);
455
456         /* if this is a local lock, then there is no import */
457         if (obd != NULL)
458                 imp = obd->u.cli.cl_import;
459
460         if (imp != NULL) {
461                 spin_lock_irqsave(&imp->imp_lock, irqflags);
462                 fwd.fwd_generation = imp->imp_generation;
463                 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
464         }
465
466         lwi = LWI_TIMEOUT_INTR(0,NULL,ldlm_flock_interrupted_wait,&fwd);
467
468         /* Go to sleep until the lock is granted. */
469         rc = l_wait_event(lock->l_waitq,
470                           ((lock->l_req_mode == lock->l_granted_mode) ||
471                            lock->l_destroyed), &lwi);
472
473         if (rc) {
474                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
475                            rc);
476                 RETURN(rc);
477         }
478
479         LASSERT(!(lock->l_destroyed));
480
481 granted:
482
483         LDLM_DEBUG(lock, "client-side enqueue waking up");
484         ns = lock->l_resource->lr_namespace;
485         l_lock(&ns->ns_lock);
486
487         /* take data off of deadlock detection waitq. */
488         list_del_init(&lock->l_flock_waitq);
489
490         /* ldlm_lock_enqueue() has already placed lock on the granted list. */
491         list_del_init(&lock->l_res_link);
492
493         if (flags & LDLM_FL_TEST_LOCK) {
494                 /* fcntl(F_GETLK) request */
495                 /* The old mode was saved in getlk->fl_type so that if the mode
496                  * in the lock changes we can decref the approprate refcount. */
497                 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
498                 switch (lock->l_granted_mode) {
499                 case LCK_PR:
500                         getlk->fl_type = F_RDLCK;
501                         break;
502                 case LCK_PW:
503                         getlk->fl_type = F_WRLCK;
504                         break;
505                 default:
506                         getlk->fl_type = F_UNLCK;
507                 }
508                 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
509                 getlk->fl_start = lock->l_policy_data.l_flock.start;
510                 getlk->fl_end = lock->l_policy_data.l_flock.end;
511         } else {
512                 /* We need to reprocess the lock to do merges or splits
513                  * with existing locks owne by this process. */
514                 flags = LDLM_FL_WAIT_NOREPROC;
515                 ldlm_process_flock_lock(lock, &flags, 1, &err);
516         }
517         l_unlock(&ns->ns_lock);
518         RETURN(0);
519 }