Whamcloud - gitweb
- landed b_hd_cray_merge3
[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)
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(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                                 res->lr_tmp = &rpc_list;
391                                 ldlm_reprocess_queue(res, &res->lr_waiting);
392                                 res->lr_tmp = NULL;
393
394                                 l_unlock(&ns->ns_lock);
395                                 rc = ldlm_run_ast_work(res->lr_namespace,
396                                                        &rpc_list);
397                                 l_lock(&ns->ns_lock);
398                                 if (rc == -ERESTART)
399                                         GOTO(restart, -ERESTART);
400                        }
401                 } else {
402                         LASSERT(req->l_completion_ast);
403                         ldlm_add_ast_work_item(req, NULL, NULL, 0);
404                 }
405         }
406
407         /* In case we're reprocessing the requested lock we can't destroy
408          * it until after calling ldlm_ast_work_item() above so that lawi()
409          * can bump the reference count on req. Otherwise req could be freed
410          * before the completion AST can be sent.  */
411         if (added)
412                 ldlm_flock_destroy(req, mode, *flags);
413
414         ldlm_resource_dump(D_OTHER, res);
415         RETURN(LDLM_ITER_CONTINUE);
416 }
417
418 struct ldlm_flock_wait_data {
419         struct ldlm_lock *fwd_lock;
420         int               fwd_generation;
421 };
422
423 static void
424 ldlm_flock_interrupted_wait(void *data)
425 {
426         struct ldlm_lock *lock;
427         struct lustre_handle lockh;
428         ENTRY;
429
430         lock = ((struct ldlm_flock_wait_data *)data)->fwd_lock;
431
432         /* take lock off the deadlock detection waitq. */
433         list_del_init(&lock->l_flock_waitq);
434
435         /* client side - set flag to prevent lock from being put on lru list */
436         lock->l_flags |= LDLM_FL_CBPENDING;
437
438         ldlm_lock_decref_internal(lock, lock->l_req_mode);
439         ldlm_lock2handle(lock, &lockh);
440         ldlm_cli_cancel(&lockh);
441         EXIT;
442 }
443
444 int
445 ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data)
446 {
447         struct ldlm_namespace *ns;
448         struct file_lock *getlk = lock->l_ast_data;
449         struct ldlm_flock_wait_data fwd;
450         unsigned long irqflags;
451         struct obd_device *obd;
452         struct obd_import *imp = NULL;
453         ldlm_error_t err;
454         int rc = 0;
455         struct l_wait_info lwi;
456         ENTRY;
457
458         CDEBUG(D_DLMTRACE, "flags: 0x%x data: %p getlk: %p\n",
459                flags, data, getlk);
460
461         LASSERT(flags != LDLM_FL_WAIT_NOREPROC);
462
463         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
464                        LDLM_FL_BLOCK_CONV)))
465                 goto  granted;
466
467         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
468                    "sleeping");
469
470         ldlm_lock_dump(D_DLMTRACE, lock, 0);
471         fwd.fwd_lock = lock;
472         obd = class_exp2obd(lock->l_conn_export);
473
474         /* if this is a local lock, then there is no import */
475         if (obd != NULL)
476                 imp = obd->u.cli.cl_import;
477
478         if (imp != NULL) {
479                 spin_lock_irqsave(&imp->imp_lock, irqflags);
480                 fwd.fwd_generation = imp->imp_generation;
481                 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
482         }
483
484         lwi = LWI_TIMEOUT_INTR(0, NULL, ldlm_flock_interrupted_wait, &fwd);
485
486         /* Go to sleep until the lock is granted. */
487         rc = l_wait_event(lock->l_waitq,
488                           ((lock->l_req_mode == lock->l_granted_mode) ||
489                            lock->l_destroyed), &lwi);
490
491         LDLM_DEBUG(lock, "client-side enqueue waking up: rc = %d", rc);
492         RETURN(rc);
493  
494 granted:
495
496         LDLM_DEBUG(lock, "client-side enqueue granted");
497         ns = lock->l_resource->lr_namespace;
498         l_lock(&ns->ns_lock);
499
500         /* take lock off the deadlock detection waitq. */
501         list_del_init(&lock->l_flock_waitq);
502
503         /* ldlm_lock_enqueue() has already placed lock on the granted list. */
504         list_del_init(&lock->l_res_link);
505
506         if (flags & LDLM_FL_TEST_LOCK) {
507                 /* fcntl(F_GETLK) request */
508                 /* The old mode was saved in getlk->fl_type so that if the mode
509                  * in the lock changes we can decref the approprate refcount. */
510                 ldlm_flock_destroy(lock, getlk->fl_type, LDLM_FL_WAIT_NOREPROC);
511                 switch (lock->l_granted_mode) {
512                 case LCK_PR:
513                         getlk->fl_type = F_RDLCK;
514                         break;
515                 case LCK_PW:
516                         getlk->fl_type = F_WRLCK;
517                         break;
518                 default:
519                         getlk->fl_type = F_UNLCK;
520                 }
521                 getlk->fl_pid = lock->l_policy_data.l_flock.pid;
522                 getlk->fl_start = lock->l_policy_data.l_flock.start;
523                 getlk->fl_end = lock->l_policy_data.l_flock.end;
524         } else {
525                 int noreproc = LDLM_FL_WAIT_NOREPROC;
526
527                 /* We need to reprocess the lock to do merges or splits
528                  * with existing locks owned by this process. */
529                 ldlm_process_flock_lock(lock, &noreproc, 1, &err);
530                 if (flags == 0)
531                         wake_up(&lock->l_waitq);
532         }
533         l_unlock(&ns->ns_lock);
534         RETURN(0);
535 }
536
537 int ldlm_flock_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
538                             void *data, int flag)
539 {
540         struct ldlm_namespace *ns;
541         ENTRY;
542                                                                                                                              
543         LASSERT(lock);
544         LASSERT(flag == LDLM_CB_CANCELING);
545                                                                                                                              
546         ns = lock->l_resource->lr_namespace;
547                                                                                                                              
548         /* take lock off the deadlock detection waitq. */
549         l_lock(&ns->ns_lock);
550         list_del_init(&lock->l_flock_waitq);
551         l_unlock(&ns->ns_lock);
552         RETURN(0);
553 }