Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.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/slab.h>
28 # include <linux/module.h>
29 # include <linux/lustre_dlm.h>
30 # include <linux/lustre_mds.h>
31 #else
32 # include <liblustre.h>
33 # include <linux/kp30.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include "ldlm_internal.h"
38
39 //struct lustre_lock ldlm_everything_lock;
40
41 /* lock types */
42 char *ldlm_lockname[] = {
43         [0] "--",
44         [LCK_EX] "EX",
45         [LCK_PW] "PW",
46         [LCK_PR] "PR",
47         [LCK_CW] "CW",
48         [LCK_CR] "CR",
49         [LCK_NL] "NL"
50 };
51 char *ldlm_typename[] = {
52         [LDLM_PLAIN] "PLN",
53         [LDLM_EXTENT] "EXT",
54 };
55
56 #ifdef __KERNEL__
57 char *ldlm_it2str(int it)
58 {
59         switch (it) {
60         case IT_OPEN:
61                 return "open";
62         case IT_CREAT:
63                 return "creat";
64         case (IT_OPEN | IT_CREAT):
65                 return "open|creat";
66         case IT_READDIR:
67                 return "readdir";
68         case IT_GETATTR:
69                 return "getattr";
70         case IT_LOOKUP:
71                 return "lookup";
72         case IT_UNLINK:
73                 return "unlink";
74         default:
75                 CERROR("Unknown intent %d\n", it);
76                 return "UNKNOWN";
77         }
78 }
79 #endif
80
81 extern kmem_cache_t *ldlm_lock_slab;
82 struct lustre_lock ldlm_handle_lock;
83
84 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
85
86 static ldlm_res_compat ldlm_res_compat_table[] = {
87         [LDLM_PLAIN] ldlm_plain_compat,
88         [LDLM_EXTENT] ldlm_extent_compat,
89 };
90
91 static ldlm_res_policy ldlm_intent_policy_func;
92
93 static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
94                              void *req_cookie, ldlm_mode_t mode, int flags,
95                              void *data)
96 {
97         if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
98                 return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
99                                                flags, data);
100         }
101
102         return ELDLM_OK;
103 }
104
105 static ldlm_res_policy ldlm_res_policy_table[] = {
106         [LDLM_PLAIN] ldlm_plain_policy,
107         [LDLM_EXTENT] ldlm_extent_policy,
108 };
109
110 void ldlm_register_intent(ldlm_res_policy arg)
111 {
112         ldlm_intent_policy_func = arg;
113 }
114
115 void ldlm_unregister_intent(void)
116 {
117         ldlm_intent_policy_func = NULL;
118 }
119
120 /*
121  * REFCOUNTED LOCK OBJECTS
122  */
123
124
125 /*
126  * Lock refcounts, during creation:
127  *   - one special one for allocation, dec'd only once in destroy
128  *   - one for being a lock that's in-use
129  *   - one for the addref associated with a new lock
130  */
131 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
132 {
133         atomic_inc(&lock->l_refc);
134         return lock;
135 }
136
137 void ldlm_lock_put(struct ldlm_lock *lock)
138 {
139         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
140         ENTRY;
141
142         if (atomic_dec_and_test(&lock->l_refc)) {
143                 l_lock(&ns->ns_lock);
144                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
145                 LASSERT(lock->l_destroyed);
146                 LASSERT(list_empty(&lock->l_res_link));
147
148                 spin_lock(&ns->ns_counter_lock);
149                 ns->ns_locks--;
150                 spin_unlock(&ns->ns_counter_lock);
151
152                 ldlm_resource_putref(lock->l_resource);
153                 lock->l_resource = NULL;
154
155                 if (lock->l_parent)
156                         LDLM_LOCK_PUT(lock->l_parent);
157
158                 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
159                 l_unlock(&ns->ns_lock);
160         }
161
162         EXIT;
163 }
164
165 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
166 {
167         ENTRY;
168         l_lock(&lock->l_resource->lr_namespace->ns_lock);
169         if (!list_empty(&lock->l_lru)) {
170                 list_del_init(&lock->l_lru);
171                 lock->l_resource->lr_namespace->ns_nr_unused--;
172                 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
173         }
174         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
175         EXIT;
176 }
177
178 /* This used to have a 'strict' flact, which recovery would use to mark an
179  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
180  * shall explain why it's gone: with the new hash table scheme, once you call
181  * ldlm_lock_destroy, you can never drop your final references on this lock.
182  * Because it's not in the hash table anymore.  -phil */
183 void ldlm_lock_destroy(struct ldlm_lock *lock)
184 {
185         ENTRY;
186         l_lock(&lock->l_resource->lr_namespace->ns_lock);
187
188         if (!list_empty(&lock->l_children)) {
189                 LDLM_ERROR(lock, "still has children (%p)!",
190                            lock->l_children.next);
191                 ldlm_lock_dump(D_ERROR, lock);
192                 LBUG();
193         }
194         if (lock->l_readers || lock->l_writers) {
195                 LDLM_ERROR(lock, "lock still has references");
196                 ldlm_lock_dump(D_ERROR, lock);
197                 LBUG();
198         }
199
200         if (!list_empty(&lock->l_res_link)) {
201                 ldlm_lock_dump(D_ERROR, lock);
202                 LBUG();
203         }
204
205         if (lock->l_destroyed) {
206                 LASSERT(list_empty(&lock->l_lru));
207                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
208                 EXIT;
209                 return;
210         }
211         lock->l_destroyed = 1;
212
213         list_del_init(&lock->l_export_chain);
214         ldlm_lock_remove_from_lru(lock);
215         class_handle_unhash(&lock->l_handle);
216
217 #if 0
218         /* Wake anyone waiting for this lock */
219         /* FIXME: I should probably add yet another flag, instead of using
220          * l_export to only call this on clients */
221         lock->l_export = NULL;
222         if (lock->l_export && lock->l_completion_ast)
223                 lock->l_completion_ast(lock, 0);
224 #endif
225
226         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
227         LDLM_LOCK_PUT(lock);
228         EXIT;
229 }
230
231 /* this is called by portals_handle2object with the handle lock taken */
232 static void lock_handle_addref(void *lock)
233 {
234         LDLM_LOCK_GET((struct ldlm_lock *)lock);
235 }
236
237 /*
238  * usage: pass in a resource on which you have done ldlm_resource_get
239  *        pass in a parent lock on which you have done a ldlm_lock_get
240  *        after return, ldlm_*_put the resource and parent
241  * returns: lock with refcount 1
242  */
243 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
244                                        struct ldlm_resource *resource)
245 {
246         struct ldlm_lock *lock;
247         ENTRY;
248
249         if (resource == NULL)
250                 LBUG();
251
252         OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_KERNEL, sizeof(*lock));
253         if (lock == NULL)
254                 RETURN(NULL);
255
256         lock->l_resource = ldlm_resource_getref(resource);
257
258         atomic_set(&lock->l_refc, 2);
259         INIT_LIST_HEAD(&lock->l_children);
260         INIT_LIST_HEAD(&lock->l_res_link);
261         INIT_LIST_HEAD(&lock->l_lru);
262         INIT_LIST_HEAD(&lock->l_export_chain);
263         INIT_LIST_HEAD(&lock->l_pending_chain);
264         init_waitqueue_head(&lock->l_waitq);
265
266         spin_lock(&resource->lr_namespace->ns_counter_lock);
267         resource->lr_namespace->ns_locks++;
268         spin_unlock(&resource->lr_namespace->ns_counter_lock);
269
270         if (parent != NULL) {
271                 l_lock(&parent->l_resource->lr_namespace->ns_lock);
272                 lock->l_parent = LDLM_LOCK_GET(parent);
273                 list_add(&lock->l_childof, &parent->l_children);
274                 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
275         }
276
277         INIT_LIST_HEAD(&lock->l_handle.h_link);
278         class_handle_hash(&lock->l_handle, lock_handle_addref);
279
280         RETURN(lock);
281 }
282
283 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
284                               struct ldlm_res_id new_resid)
285 {
286         struct ldlm_resource *oldres = lock->l_resource;
287         ENTRY;
288
289         l_lock(&ns->ns_lock);
290         if (memcmp(&new_resid, &lock->l_resource->lr_name,
291                    sizeof(lock->l_resource->lr_name)) == 0) {
292                 /* Nothing to do */
293                 l_unlock(&ns->ns_lock);
294                 RETURN(0);
295         }
296
297         LASSERT(new_resid.name[0] != 0);
298
299         /* This function assumes that the lock isn't on any lists */
300         LASSERT(list_empty(&lock->l_res_link));
301
302         lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
303                                              lock->l_resource->lr_type, 1);
304         if (lock->l_resource == NULL) {
305                 LBUG();
306                 RETURN(-ENOMEM);
307         }
308
309         /* ...and the flowers are still standing! */
310         ldlm_resource_putref(oldres);
311
312         l_unlock(&ns->ns_lock);
313         RETURN(0);
314 }
315
316 /*
317  *  HANDLES
318  */
319
320 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
321 {
322         lockh->cookie = lock->l_handle.h_cookie;
323 }
324
325 /* if flags: atomically get the lock and set the flags.
326  *           Return NULL if flag already set
327  */
328
329 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
330 {
331         struct ldlm_lock *lock = NULL, *retval = NULL;
332         ENTRY;
333
334         LASSERT(handle);
335
336         lock = class_handle2object(handle->cookie);
337         if (lock == NULL)
338                 RETURN(NULL);
339
340         LASSERT(lock->l_resource != NULL);
341         LASSERT(lock->l_resource->lr_namespace != NULL);
342
343         l_lock(&lock->l_resource->lr_namespace->ns_lock);
344
345         /* It's unlikely but possible that someone marked the lock as
346          * destroyed after we did handle2object on it */
347         if (lock->l_destroyed) {
348                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
349                 LDLM_LOCK_PUT(lock);
350                 GOTO(out, retval);
351         }
352
353         if (flags && (lock->l_flags & flags)) {
354                 LDLM_LOCK_PUT(lock);
355                 GOTO(out, retval);
356         }
357
358         if (flags)
359                 lock->l_flags |= flags;
360
361         retval = lock;
362         EXIT;
363  out:
364         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
365         return retval;
366 }
367
368 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
369                                       struct lustre_handle *handle)
370 {
371         struct ldlm_lock *retval = NULL;
372
373         l_lock(&ns->ns_lock);
374         retval = __ldlm_handle2lock(handle, 0);
375         l_unlock(&ns->ns_lock);
376
377         return retval;
378 }
379
380 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
381 {
382         return lockmode_compat(a->l_req_mode, b->l_req_mode);
383 }
384
385 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
386 {
387         ldlm_res2desc(lock->l_resource, &desc->l_resource);
388         desc->l_req_mode = lock->l_req_mode;
389         desc->l_granted_mode = lock->l_granted_mode;
390         memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
391         memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
392 }
393
394 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
395                                    struct ldlm_lock *new,
396                                    void *data, int datalen)
397 {
398         struct ldlm_ast_work *w;
399         ENTRY;
400
401         l_lock(&lock->l_resource->lr_namespace->ns_lock);
402         if (new && (lock->l_flags & LDLM_FL_AST_SENT))
403                 GOTO(out, 0);
404
405         OBD_ALLOC(w, sizeof(*w));
406         if (!w) {
407                 LBUG();
408                 GOTO(out, 0);
409         }
410
411         w->w_data = data;
412         w->w_datalen = datalen;
413         if (new) {
414                 lock->l_flags |= LDLM_FL_AST_SENT;
415                 w->w_blocking = 1;
416                 ldlm_lock2desc(new, &w->w_desc);
417         }
418
419         w->w_lock = LDLM_LOCK_GET(lock);
420         list_add(&w->w_list, lock->l_resource->lr_tmp);
421         EXIT;
422  out:
423         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
424         return;
425 }
426
427 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
428 {
429         struct ldlm_lock *lock;
430
431         lock = ldlm_handle2lock(lockh);
432         ldlm_lock_addref_internal(lock, mode);
433         LDLM_LOCK_PUT(lock);
434 }
435
436 /* only called for local locks */
437 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
438 {
439         l_lock(&lock->l_resource->lr_namespace->ns_lock);
440         ldlm_lock_remove_from_lru(lock);
441         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
442                 lock->l_readers++;
443         else
444                 lock->l_writers++;
445         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
446         LDLM_LOCK_GET(lock);
447         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
448 }
449
450 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
451 {
452         struct ldlm_namespace *ns;
453         ENTRY;
454
455         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
456         ns = lock->l_resource->lr_namespace;
457         l_lock(&ns->ns_lock);
458         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
459                 LASSERT(lock->l_readers > 0);
460                 lock->l_readers--;
461         } else {
462                 LASSERT(lock->l_writers > 0);
463                 lock->l_writers--;
464         }
465
466         if (lock->l_flags & LDLM_FL_LOCAL &&
467             !lock->l_readers && !lock->l_writers) {
468                 /* If this is a local lock on a server namespace and this was
469                  * the last reference, cancel the lock. */
470                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
471                 lock->l_flags |= LDLM_FL_CBPENDING;
472         }
473
474         if (!lock->l_readers && !lock->l_writers &&
475             (lock->l_flags & LDLM_FL_CBPENDING)) {
476                 /* If we received a blocked AST and this was the last reference,
477                  * run the callback. */
478                 if (!ns->ns_client && lock->l_export)
479                         CERROR("FL_CBPENDING set on non-local lock--just a "
480                                "warning\n");
481
482                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
483                 l_unlock(&ns->ns_lock);
484
485                 /* FIXME: need a real 'desc' here */
486                 if (lock->l_blocking_ast != NULL)
487                         lock->l_blocking_ast(lock, NULL, lock->l_data,
488                                              LDLM_CB_BLOCKING);
489                 else
490                         LDLM_DEBUG(lock, "No blocking AST?");
491         } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
492                 /* If this is a client-side namespace and this was the last
493                  * reference, put it on the LRU. */
494                 LASSERT(list_empty(&lock->l_lru));
495                 LASSERT(ns->ns_nr_unused >= 0);
496                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
497                 ns->ns_nr_unused++;
498                 l_unlock(&ns->ns_lock);
499                 ldlm_cancel_lru(ns);
500         } else {
501                 l_unlock(&ns->ns_lock);
502         }
503
504         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
505
506         EXIT;
507 }
508
509 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
510 {
511         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
512         LASSERT(lock != NULL);
513         ldlm_lock_decref_internal(lock, mode);
514         LDLM_LOCK_PUT(lock);
515 }
516
517 /* This will drop a lock reference and mark it for destruction, but will not
518  * necessarily cancel the lock before returning. */
519 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
520 {
521         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
522         ENTRY;
523
524         LASSERT(lock != NULL);
525
526         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
527         l_lock(&lock->l_resource->lr_namespace->ns_lock);
528         lock->l_flags |= LDLM_FL_CBPENDING;
529         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
530         ldlm_lock_decref_internal(lock, mode);
531         LDLM_LOCK_PUT(lock);
532 }
533
534 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
535                                  struct list_head *queue)
536 {
537         struct list_head *tmp, *pos;
538         int rc = 1;
539
540         list_for_each_safe(tmp, pos, queue) {
541                 struct ldlm_lock *child;
542                 ldlm_res_compat compat;
543
544                 child = list_entry(tmp, struct ldlm_lock, l_res_link);
545                 if (lock == child)
546                         continue;
547
548                 compat = ldlm_res_compat_table[child->l_resource->lr_type];
549                 if (compat && compat(child, lock)) {
550                         CDEBUG(D_OTHER, "compat function succeded, next.\n");
551                         continue;
552                 }
553                 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
554                         CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
555                         continue;
556                 }
557
558                 rc = 0;
559
560                 if (send_cbs && child->l_blocking_ast != NULL) {
561                         CDEBUG(D_OTHER, "lock %p incompatible; sending "
562                                "blocking AST.\n", child);
563                         ldlm_add_ast_work_item(child, lock, NULL, 0);
564                 }
565         }
566
567         return rc;
568 }
569
570 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
571 {
572         int rc;
573         ENTRY;
574
575         l_lock(&lock->l_resource->lr_namespace->ns_lock);
576         rc = ldlm_lock_compat_list(lock, send_cbs,
577                                    &lock->l_resource->lr_granted);
578         /* FIXME: should we be sending ASTs to converting? */
579         if (rc)
580                 rc = ldlm_lock_compat_list
581                         (lock, send_cbs, &lock->l_resource->lr_converting);
582
583         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
584         RETURN(rc);
585 }
586
587 /* NOTE: called by
588  *  - ldlm_lock_enqueue
589  *  - ldlm_reprocess_queue
590  *  - ldlm_lock_convert
591  */
592 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
593 {
594         struct ldlm_resource *res = lock->l_resource;
595         ENTRY;
596
597         l_lock(&lock->l_resource->lr_namespace->ns_lock);
598         ldlm_resource_add_lock(res, &res->lr_granted, lock);
599         lock->l_granted_mode = lock->l_req_mode;
600
601         if (lock->l_granted_mode < res->lr_most_restr)
602                 res->lr_most_restr = lock->l_granted_mode;
603
604         if (lock->l_completion_ast != NULL)
605                 ldlm_add_ast_work_item(lock, NULL, data, datalen);
606
607         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
608         EXIT;
609 }
610
611 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
612  * comment above ldlm_lock_match */
613 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
614                                       struct ldlm_extent *extent,
615                                       struct ldlm_lock *old_lock, void *data,
616                                       int flags)
617 {
618         struct ldlm_lock *lock;
619         struct list_head *tmp;
620
621         list_for_each(tmp, queue) {
622                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
623
624                 if (lock == old_lock)
625                         break;
626
627                 /* llite sometimes wants to match locks that will be
628                  * canceled when their users drop, but we allow it to match
629                  * if it passes in CBPENDING and the lock still has users.
630                  * this is generally only going to be used by children 
631                  * whose parents already hold a lock so forward progress
632                  * can still happen. */
633                 if (lock->l_flags & LDLM_FL_CBPENDING &&
634                     !(flags & LDLM_FL_CBPENDING))
635                         continue;
636                 if (lock->l_flags & LDLM_FL_CBPENDING &&
637                     lock->l_readers == 0 && lock->l_writers == 0)
638                         continue;
639
640                 if (lock->l_req_mode != mode)
641                         continue;
642
643                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
644                     (lock->l_extent.start > extent->start ||
645                      lock->l_extent.end < extent->end))
646                         continue;
647
648                 if (lock->l_destroyed)
649                         continue;
650
651                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
652                     !(lock->l_flags & LDLM_FL_LOCAL))
653                         continue;
654
655                 if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data) {
656                         LDLM_DEBUG(lock, "data mismatch: have %p, want %p",
657                                    lock->l_data, data);
658                         continue;
659                 }
660
661                 ldlm_lock_addref_internal(lock, mode);
662                 return lock;
663         }
664
665         return NULL;
666 }
667
668 /* Can be called in two ways:
669  *
670  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
671  * for a duplicate of.
672  *
673  * Otherwise, all of the fields must be filled in, to match against.
674  *
675  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
676  *     server (ie, connh is NULL)
677  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
678  *     list will be considered
679  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
680  *     to be canceled can still be matched as long as they still have reader
681  *     or writer refernces
682  * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
683  *     data is the same.
684  *
685  * Returns 1 if it finds an already-existing lock that is compatible; in this
686  * case, lockh is filled in with a addref()ed lock
687  */
688 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
689                     struct ldlm_res_id *res_id, __u32 type, void *cookie,
690                     int cookielen, ldlm_mode_t mode, void *data,
691                     struct lustre_handle *lockh)
692 {
693         struct ldlm_resource *res;
694         struct ldlm_lock *lock, *old_lock = NULL;
695         int rc = 0;
696         ENTRY;
697
698         if (ns == NULL) {
699                 old_lock = ldlm_handle2lock(lockh);
700                 LASSERT(old_lock);
701
702                 ns = old_lock->l_resource->lr_namespace;
703                 res_id = &old_lock->l_resource->lr_name;
704                 type = old_lock->l_resource->lr_type;
705                 mode = old_lock->l_req_mode;
706         }
707
708         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
709         if (res == NULL) {
710                 LASSERT(old_lock == NULL);
711                 RETURN(0);
712         }
713
714         l_lock(&ns->ns_lock);
715
716         lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
717                             flags);
718         if (lock != NULL)
719                 GOTO(out, rc = 1);
720         if (flags & LDLM_FL_BLOCK_GRANTED)
721                 GOTO(out, rc = 0);
722         lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
723                             flags);
724         if (lock != NULL)
725                 GOTO(out, rc = 1);
726         lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
727                             flags);
728         if (lock != NULL)
729                 GOTO(out, rc = 1);
730
731         EXIT;
732  out:
733         ldlm_resource_putref(res);
734         l_unlock(&ns->ns_lock);
735
736         if (lock) {
737                 ldlm_lock2handle(lock, lockh);
738                 if (lock->l_completion_ast)
739                         lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
740                                                NULL);
741         }
742         if (rc)
743                 LDLM_DEBUG(lock, "matched");
744         else
745                 LDLM_DEBUG_NOLOCK("not matched");
746
747         if (old_lock)
748                 LDLM_LOCK_PUT(old_lock);
749
750         return rc;
751 }
752
753 /* Returns a referenced lock */
754 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
755                                    struct lustre_handle *parent_lock_handle,
756                                    struct ldlm_res_id res_id, __u32 type,
757                                    ldlm_mode_t mode,
758                                    ldlm_blocking_callback blocking,
759                                    void *data)
760 {
761         struct ldlm_resource *res, *parent_res = NULL;
762         struct ldlm_lock *lock, *parent_lock = NULL;
763         ENTRY;
764
765         if (parent_lock_handle) {
766                 parent_lock = ldlm_handle2lock(parent_lock_handle);
767                 if (parent_lock)
768                         parent_res = parent_lock->l_resource;
769         }
770
771         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
772         if (res == NULL)
773                 RETURN(NULL);
774
775         lock = ldlm_lock_new(parent_lock, res);
776         ldlm_resource_putref(res);
777         if (parent_lock != NULL)
778                 LDLM_LOCK_PUT(parent_lock);
779
780         if (lock == NULL)
781                 RETURN(NULL);
782
783         lock->l_req_mode = mode;
784         lock->l_data = data;
785         lock->l_blocking_ast = blocking;
786
787         RETURN(lock);
788 }
789
790 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
791                                struct ldlm_lock **lockp,
792                                void *cookie, int cookie_len,
793                                int *flags,
794                                ldlm_completion_callback completion)
795 {
796         struct ldlm_resource *res;
797         struct ldlm_lock *lock = *lockp;
798         int local;
799         ldlm_res_policy policy;
800         ENTRY;
801
802         res = lock->l_resource;
803
804         if (res->lr_type == LDLM_EXTENT)
805                 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
806
807         /* policies are not executed on the client or during replay */
808         local = res->lr_namespace->ns_client;
809         if (!local && !(*flags & LDLM_FL_REPLAY) &&
810             (policy = ldlm_res_policy_table[res->lr_type])) {
811                 int rc;
812                 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
813                 if (rc == ELDLM_LOCK_CHANGED) {
814                         res = lock->l_resource;
815                         *flags |= LDLM_FL_LOCK_CHANGED;
816                 } else if (rc == ELDLM_LOCK_REPLACED) {
817                         /* The lock that was returned has already been granted,
818                          * and placed into lockp.  Destroy the old one and our
819                          * work here is done. */
820                         ldlm_lock_destroy(lock);
821                         LDLM_LOCK_PUT(lock);
822                         *flags |= LDLM_FL_LOCK_CHANGED;
823                         RETURN(0);
824                 } else if (rc == ELDLM_LOCK_ABORTED) {
825                         ldlm_lock_destroy(lock);
826                         RETURN(rc);
827                 }
828         }
829
830         l_lock(&ns->ns_lock);
831         if (local && lock->l_req_mode == lock->l_granted_mode) {
832                 /* The server returned a blocked lock, but it was granted before
833                  * we got a chance to actually enqueue it.  We don't need to do
834                  * anything else. */
835                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
836                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
837                 GOTO(out, ELDLM_OK);
838         }
839
840         /* This distinction between local lock trees is very important; a client
841          * namespace only has information about locks taken by that client, and
842          * thus doesn't have enough information to decide for itself if it can
843          * be granted (below).  In this case, we do exactly what the server
844          * tells us to do, as dictated by the 'flags'.
845          *
846          * We do exactly the same thing during recovery, when the server is
847          * more or less trusting the clients not to lie.
848          *
849          * FIXME (bug 268): Detect obvious lies by checking compatibility in
850          * granted/converting queues. */
851         ldlm_resource_unlink_lock(lock);
852         if (local) {
853                 if (*flags & LDLM_FL_BLOCK_CONV)
854                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
855                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
856                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
857                 else
858                         ldlm_grant_lock(lock, NULL, 0);
859                 GOTO(out, ELDLM_OK);
860         } else if (*flags & LDLM_FL_REPLAY) {
861                 if (*flags & LDLM_FL_BLOCK_CONV) {
862                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
863                         GOTO(out, ELDLM_OK);
864                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
865                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
866                         GOTO(out, ELDLM_OK);
867                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
868                         ldlm_grant_lock(lock, NULL, 0);
869                         GOTO(out, ELDLM_OK);
870                 }
871                 /* If no flags, fall through to normal enqueue path. */
872         }
873
874         /* FIXME: We may want to optimize by checking lr_most_restr */
875         if (!list_empty(&res->lr_converting)) {
876                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
877                 *flags |= LDLM_FL_BLOCK_CONV;
878                 GOTO(out, ELDLM_OK);
879         }
880         if (!list_empty(&res->lr_waiting)) {
881                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
882                 *flags |= LDLM_FL_BLOCK_WAIT;
883                 GOTO(out, ELDLM_OK);
884         }
885         if (!ldlm_lock_compat(lock, 0)) {
886                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
887                 *flags |= LDLM_FL_BLOCK_GRANTED;
888                 GOTO(out, ELDLM_OK);
889         }
890         ldlm_grant_lock(lock, NULL, 0);
891         EXIT;
892       out:
893         /* Don't set 'completion_ast' until here so that if the lock is granted
894          * immediately we don't do an unnecessary completion call. */
895         lock->l_completion_ast = completion;
896         l_unlock(&ns->ns_lock);
897         return ELDLM_OK;
898 }
899
900 /* Must be called with namespace taken: queue is waiting or converting. */
901 static int ldlm_reprocess_queue(struct ldlm_resource *res,
902                                 struct list_head *queue)
903 {
904         struct list_head *tmp, *pos;
905         ENTRY;
906
907         list_for_each_safe(tmp, pos, queue) {
908                 struct ldlm_lock *pending;
909                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
910
911                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
912
913                 if (!ldlm_lock_compat(pending, 1))
914                         RETURN(1);
915
916                 list_del_init(&pending->l_res_link);
917                 ldlm_grant_lock(pending, NULL, 0);
918         }
919
920         RETURN(0);
921 }
922
923 int ldlm_run_ast_work(struct list_head *rpc_list)
924 {
925         struct list_head *tmp, *pos;
926         int rc, retval = 0;
927         ENTRY;
928
929         list_for_each_safe(tmp, pos, rpc_list) {
930                 struct ldlm_ast_work *w =
931                         list_entry(tmp, struct ldlm_ast_work, w_list);
932
933                 /* It's possible to receive a completion AST before we've set
934                  * the l_completion_ast pointer: either because the AST arrived
935                  * before the reply, or simply because there's a small race
936                  * window between receiving the reply and finishing the local
937                  * enqueue. (bug 842)
938                  *
939                  * This can't happen with the blocking_ast, however, because we
940                  * will never call the local blocking_ast until we drop our
941                  * reader/writer reference, which we won't do until we get the
942                  * reply and finish enqueueing. */
943                 if (w->w_blocking) {
944                         LASSERT(w->w_lock->l_blocking_ast != NULL);
945                         rc = w->w_lock->l_blocking_ast
946                                 (w->w_lock, &w->w_desc, w->w_data,
947                                  LDLM_CB_BLOCKING);
948                 } else if (w->w_lock->l_completion_ast != NULL) {
949                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
950                                                          w->w_data);
951                 } else {
952                         rc = 0;
953                 }
954                 if (rc == -ERESTART)
955                         retval = rc;
956                 else if (rc)
957                         CERROR("Failed AST - should clean & disconnect "
958                                "client\n");
959                 LDLM_LOCK_PUT(w->w_lock);
960                 list_del(&w->w_list);
961                 OBD_FREE(w, sizeof(*w));
962         }
963         RETURN(retval);
964 }
965
966 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
967 {
968         ldlm_reprocess_all(res);
969         return LDLM_ITER_CONTINUE;
970 }
971
972 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
973 {
974         (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
975 }
976
977 void ldlm_reprocess_all(struct ldlm_resource *res)
978 {
979         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
980         int rc;
981         ENTRY;
982
983         /* Local lock trees don't get reprocessed. */
984         if (res->lr_namespace->ns_client) {
985                 EXIT;
986                 return;
987         }
988
989  restart:
990         l_lock(&res->lr_namespace->ns_lock);
991         res->lr_tmp = &rpc_list;
992
993         ldlm_reprocess_queue(res, &res->lr_converting);
994         if (list_empty(&res->lr_converting))
995                 ldlm_reprocess_queue(res, &res->lr_waiting);
996
997         res->lr_tmp = NULL;
998         l_unlock(&res->lr_namespace->ns_lock);
999
1000         rc = ldlm_run_ast_work(&rpc_list);
1001         if (rc == -ERESTART)
1002                 goto restart;
1003         EXIT;
1004 }
1005
1006 void ldlm_cancel_callback(struct ldlm_lock *lock)
1007 {
1008         l_lock(&lock->l_resource->lr_namespace->ns_lock);
1009         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1010                 lock->l_flags |= LDLM_FL_CANCEL;
1011                 if (lock->l_blocking_ast) {
1012                         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1013                         lock->l_blocking_ast(lock, NULL, lock->l_data,
1014                                              LDLM_CB_CANCELING);
1015                         return;
1016                 } else {
1017                         LDLM_DEBUG(lock, "no blocking ast");
1018                 }
1019         }
1020         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1021 }
1022
1023 void ldlm_lock_cancel(struct ldlm_lock *lock)
1024 {
1025         struct ldlm_resource *res;
1026         struct ldlm_namespace *ns;
1027         ENTRY;
1028
1029         ldlm_del_waiting_lock(lock);
1030
1031         res = lock->l_resource;
1032         ns = res->lr_namespace;
1033
1034         l_lock(&ns->ns_lock);
1035         /* Please do not, no matter how tempting, remove this LBUG without
1036          * talking to me first. -phik */
1037         if (lock->l_readers || lock->l_writers) {
1038                 LDLM_DEBUG(lock, "lock still has references");
1039                 ldlm_lock_dump(D_OTHER, lock);
1040                 LBUG();
1041         }
1042
1043         ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
1044
1045         ldlm_resource_unlink_lock(lock);
1046         ldlm_lock_destroy(lock);
1047         l_unlock(&ns->ns_lock);
1048         EXIT;
1049 }
1050
1051 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1052 {
1053         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1054         ENTRY;
1055
1056         if (lock == NULL)
1057                 RETURN(-EINVAL);
1058
1059         lock->l_data = data;
1060
1061         LDLM_LOCK_PUT(lock);
1062
1063         RETURN(0);
1064 }
1065
1066 /* This function is only called from one thread (per export); no locking around
1067  * the list ops needed */
1068 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1069 {
1070         struct list_head *iter, *n;
1071
1072         list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1073                 struct ldlm_lock *lock;
1074                 struct ldlm_resource *res;
1075                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1076                 res = ldlm_resource_getref(lock->l_resource);
1077                 LDLM_DEBUG(lock, "export %p", exp);
1078                 ldlm_lock_cancel(lock);
1079                 ldlm_reprocess_all(res);
1080                 ldlm_resource_putref(res);
1081         }
1082 }
1083
1084 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1085                                         int *flags)
1086 {
1087         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1088         struct ldlm_resource *res;
1089         struct ldlm_namespace *ns;
1090         int granted = 0;
1091         ENTRY;
1092
1093         LBUG();
1094
1095         res = lock->l_resource;
1096         ns = res->lr_namespace;
1097
1098         l_lock(&ns->ns_lock);
1099
1100         lock->l_req_mode = new_mode;
1101         ldlm_resource_unlink_lock(lock);
1102
1103         /* If this is a local resource, put it on the appropriate list. */
1104         if (res->lr_namespace->ns_client) {
1105                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1106                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1107                 } else {
1108                         /* This should never happen, because of the way the
1109                          * server handles conversions. */
1110                         LBUG();
1111
1112                         res->lr_tmp = &rpc_list;
1113                         ldlm_grant_lock(lock, NULL, 0);
1114                         res->lr_tmp = NULL;
1115                         granted = 1;
1116                         /* FIXME: completion handling not with ns_lock held ! */
1117                         if (lock->l_completion_ast)
1118                                 lock->l_completion_ast(lock, 0, NULL);
1119                 }
1120         } else {
1121                 /* FIXME: We should try the conversion right away and possibly
1122                  * return success without the need for an extra AST */
1123                 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1124                 *flags |= LDLM_FL_BLOCK_CONV;
1125         }
1126
1127         l_unlock(&ns->ns_lock);
1128
1129         if (granted)
1130                 ldlm_run_ast_work(&rpc_list);
1131         RETURN(res);
1132 }
1133
1134 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1135 {
1136         char ver[128];
1137         struct obd_device *obd;
1138
1139         if (!((portal_debug | D_ERROR) & level))
1140                 return;
1141
1142         if (RES_VERSION_SIZE != 4)
1143                 LBUG();
1144
1145         if (!lock) {
1146                 CDEBUG(level, "  NULL LDLM lock\n");
1147                 return;
1148         }
1149
1150         snprintf(ver, sizeof(ver), "%x %x %x %x",
1151                  lock->l_version[0], lock->l_version[1],
1152                  lock->l_version[2], lock->l_version[3]);
1153
1154         CDEBUG(level, "  -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1155                atomic_read(&lock->l_refc));
1156         obd = class_conn2obd(lock->l_connh);
1157         if (lock->l_export && lock->l_export->exp_connection) {
1158                 CDEBUG(level, "  Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1159                        lock->l_export->exp_connection->c_peer.peer_nid,
1160                        lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1161                        lock->l_remote_handle.cookie);
1162         } else if (obd == NULL) {
1163                 CDEBUG(level, "  Node: local\n");
1164         } else {
1165                 struct obd_import *imp = obd->u.cli.cl_import;
1166                 CDEBUG(level, "  Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1167                        imp->imp_connection->c_peer.peer_nid,
1168                        imp->imp_connection->c_peer.peer_ni->pni_name,
1169                        lock->l_remote_handle.cookie);
1170         }
1171         CDEBUG(level, "  Parent: %p\n", lock->l_parent);
1172         CDEBUG(level, "  Resource: %p ("LPD64")\n", lock->l_resource,
1173                lock->l_resource->lr_name.name[0]);
1174         CDEBUG(level, "  Requested mode: %d, granted mode: %d\n",
1175                (int)lock->l_req_mode, (int)lock->l_granted_mode);
1176         CDEBUG(level, "  Readers: %u ; Writers; %u\n",
1177                lock->l_readers, lock->l_writers);
1178         if (lock->l_resource->lr_type == LDLM_EXTENT)
1179                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64"\n",
1180                        lock->l_extent.start, lock->l_extent.end);
1181 }
1182
1183 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1184 {
1185         struct ldlm_lock *lock;
1186
1187         lock = ldlm_handle2lock(lockh);
1188         if (lock == NULL)
1189                 return;
1190
1191         ldlm_lock_dump(D_OTHER, lock);
1192
1193         LDLM_LOCK_PUT(lock);
1194 }