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