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