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