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