Whamcloud - gitweb
978d0ed871fdab5c26db257025b781982256663f
[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 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 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/random.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/lustre_mds.h>
31 #include <linux/obd_class.h>
32
33 //struct lustre_lock ldlm_everything_lock;
34
35 /* lock types */
36 char *ldlm_lockname[] = {
37         [0] "--",
38         [LCK_EX] "EX",
39         [LCK_PW] "PW",
40         [LCK_PR] "PR",
41         [LCK_CW] "CW",
42         [LCK_CR] "CR",
43         [LCK_NL] "NL"
44 };
45 char *ldlm_typename[] = {
46         [LDLM_PLAIN] "PLN",
47         [LDLM_EXTENT] "EXT",
48 };
49
50 char *ldlm_it2str(int it)
51 {
52         switch (it) {
53         case IT_OPEN:
54                 return "open";
55         case IT_CREAT:
56                 return "creat";
57         case (IT_OPEN | IT_CREAT):
58                 return "open|creat";
59         case IT_MKDIR:
60                 return "mkdir";
61         case IT_LINK:
62                 return "link";
63         case IT_LINK2:
64                 return "link2";
65         case IT_SYMLINK:
66                 return "symlink";
67         case IT_UNLINK:
68                 return "unlink";
69         case IT_RMDIR:
70                 return "rmdir";
71         case IT_RENAME:
72                 return "rename";
73         case IT_RENAME2:
74                 return "rename2";
75         case IT_READDIR:
76                 return "readdir";
77         case IT_GETATTR:
78                 return "getattr";
79         case IT_SETATTR:
80                 return "setattr";
81         case IT_READLINK:
82                 return "readlink";
83         case IT_MKNOD:
84                 return "mknod";
85         case IT_LOOKUP:
86                 return "lookup";
87         default:
88                 CERROR("Unknown intent %d\n", it);
89                 return "UNKNOWN";
90         }
91 }
92
93 extern kmem_cache_t *ldlm_lock_slab;
94
95 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
96
97 ldlm_res_compat ldlm_res_compat_table[] = {
98         [LDLM_PLAIN] ldlm_plain_compat,
99         [LDLM_EXTENT] ldlm_extent_compat,
100 };
101
102 static ldlm_res_policy ldlm_intent_policy_func;
103
104 static int ldlm_plain_policy(struct ldlm_lock *lock, void *req_cookie,
105                              ldlm_mode_t mode, int flags, void *data)
106 {
107         if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
108                 return ldlm_intent_policy_func(lock, req_cookie, mode, flags, 
109                                                data);
110         }
111
112         return ELDLM_OK;
113 }
114
115 ldlm_res_policy ldlm_res_policy_table[] = {
116         [LDLM_PLAIN] ldlm_plain_policy,
117         [LDLM_EXTENT] ldlm_extent_policy,
118 };
119
120 void ldlm_register_intent(ldlm_res_policy arg)
121 {
122         ldlm_intent_policy_func = arg;
123 }
124
125 void ldlm_unregister_intent(void)
126 {
127         ldlm_intent_policy_func = NULL;
128 }
129
130 /*
131  * REFCOUNTED LOCK OBJECTS
132  */
133
134
135 /*
136  * Lock refcounts, during creation:
137  *   - one special one for allocation, dec'd only once in destroy
138  *   - one for being a lock that's in-use
139  *   - one for the addref associated with a new lock
140  */
141 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
142 {
143         l_lock(&lock->l_resource->lr_namespace->ns_lock);
144         lock->l_refc++;
145         ldlm_resource_getref(lock->l_resource);
146         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
147         return lock;
148 }
149
150 void ldlm_lock_put(struct ldlm_lock *lock)
151 {
152         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
153         ENTRY;
154
155         l_lock(&ns->ns_lock);
156         lock->l_refc--;
157         //LDLM_DEBUG(lock, "after refc--");
158         if (lock->l_refc < 0)
159                 LBUG();
160
161         if (ldlm_resource_put(lock->l_resource)) {
162                 LASSERT(lock->l_refc == 0);
163                 lock->l_resource = NULL;
164         }
165         if (lock->l_parent)
166                 LDLM_LOCK_PUT(lock->l_parent);
167
168         if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
169                 l_unlock(&ns->ns_lock);
170                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
171
172                 //spin_lock(&ldlm_handle_lock);
173                 spin_lock(&ns->ns_counter_lock);
174                 ns->ns_locks--;
175                 spin_unlock(&ns->ns_counter_lock);
176
177                 lock->l_resource = NULL;
178                 lock->l_random = DEAD_HANDLE_MAGIC;
179                 if (lock->l_export && lock->l_export->exp_connection)
180                         ptlrpc_put_connection(lock->l_export->exp_connection);
181                 kmem_cache_free(ldlm_lock_slab, lock);
182                 //spin_unlock(&ldlm_handle_lock);
183                 CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 0).\n",
184                        sizeof(*lock), lock);
185         } else
186                 l_unlock(&ns->ns_lock);
187
188         EXIT;
189 }
190
191 void ldlm_lock_destroy(struct ldlm_lock *lock)
192 {
193         ENTRY;
194         l_lock(&lock->l_resource->lr_namespace->ns_lock);
195
196         if (!list_empty(&lock->l_children)) {
197                 LDLM_DEBUG(lock, "still has children (%p)!",
198                            lock->l_children.next);
199                 ldlm_lock_dump(lock);
200                 LBUG();
201         }
202         if (lock->l_readers || lock->l_writers) {
203                 LDLM_DEBUG(lock, "lock still has references");
204                 ldlm_lock_dump(lock);
205         }
206
207         if (!list_empty(&lock->l_res_link)) {
208                 ldlm_lock_dump(lock);
209                 LBUG();
210         }
211
212         if (lock->l_flags & LDLM_FL_DESTROYED) {
213                 LASSERT(list_empty(&lock->l_lru));
214                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
215                 EXIT;
216                 return;
217         }
218
219         list_del_init(&lock->l_lru);
220         list_del(&lock->l_export_chain);
221         lock->l_export = NULL;
222         lock->l_flags |= LDLM_FL_DESTROYED;
223
224         /* Wake anyone waiting for this lock */
225         /* FIXME: I should probably add yet another flag, instead of using
226          * l_export to only call this on clients */
227         if (lock->l_export && lock->l_completion_ast)
228                 lock->l_completion_ast(lock, 0);
229
230         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
231         LDLM_LOCK_PUT(lock);
232         EXIT;
233 }
234
235 /*
236    usage: pass in a resource on which you have done get
237           pass in a parent lock on which you have done a get
238           do not put the resource or the parent
239    returns: lock with refcount 1
240 */
241 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
242                                        struct ldlm_resource *resource)
243 {
244         struct ldlm_lock *lock;
245         ENTRY;
246
247         if (resource == NULL)
248                 LBUG();
249
250         lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
251         if (lock == NULL)
252                 RETURN(NULL);
253
254         memset(lock, 0, sizeof(*lock));
255         get_random_bytes(&lock->l_random, sizeof(__u64));
256
257         lock->l_resource = resource;
258         /* this refcount matches the one of the resource passed
259            in which is not being put away */
260         lock->l_refc = 1;
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 = parent;
275                 list_add(&lock->l_childof, &parent->l_children);
276                 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
277         }
278
279         CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
280                "%p (tot %d).\n", sizeof(*lock), lock, 1);
281         /* this is the extra refcount, to prevent the lock from evaporating */
282         LDLM_LOCK_GET(lock);
283         RETURN(lock);
284 }
285
286 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
287 {
288         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
289         struct ldlm_resource *oldres = lock->l_resource;
290         int i;
291         ENTRY;
292
293         l_lock(&ns->ns_lock);
294         if (memcmp(new_resid, lock->l_resource->lr_name,
295                    sizeof(lock->l_resource->lr_name)) == 0) {
296                 /* Nothing to do */
297                 l_unlock(&ns->ns_lock);
298                 RETURN(0);
299         }
300
301         LASSERT(new_resid[0] != 0);
302
303         /* This function assumes that the lock isn't on any lists */
304         LASSERT(list_empty(&lock->l_res_link));
305
306         lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
307                                              lock->l_resource->lr_type, 1);
308         if (lock->l_resource == NULL) {
309                 LBUG();
310                 RETURN(-ENOMEM);
311         }
312
313         /* move references over */
314         for (i = 0; i < lock->l_refc; i++) {
315                 int rc;
316                 ldlm_resource_getref(lock->l_resource);
317                 rc = ldlm_resource_put(oldres);
318                 if (rc == 1 && i != lock->l_refc - 1)
319                         LBUG();
320         }
321         /* compensate for the initial get above.. */
322         ldlm_resource_put(lock->l_resource);
323
324         l_unlock(&ns->ns_lock);
325         RETURN(0);
326 }
327
328 /*
329  *  HANDLES
330  */
331
332 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
333 {
334         lockh->addr = (__u64) (unsigned long)lock;
335         lockh->cookie = lock->l_random;
336 }
337
338 /* 
339  * if flags: atomically get the lock and set the flags. 
340  * Return NULL if flag already set
341  */
342
343 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle,
344                                      int strict, int flags)
345 {
346         struct ldlm_lock *lock = NULL, *retval = NULL;
347         ENTRY;
348
349         if (!handle || !handle->addr)
350                 RETURN(NULL);
351
352         //spin_lock(&ldlm_handle_lock);
353         lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
354         if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
355                 //CERROR("bogus lock %p\n", lock);
356                 GOTO(out2, retval);
357         }
358
359         if (lock->l_random != handle->cookie) {
360                 //CERROR("bogus cookie: lock %p has "LPX64" vs. handle "LPX64"\n",
361                 //       lock, lock->l_random, handle->cookie);
362                 GOTO(out2, NULL);
363         }
364         if (!lock->l_resource) {
365                 CERROR("trying to lock bogus resource: lock %p\n", lock);
366                 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
367                 GOTO(out2, retval);
368         }
369         if (!lock->l_resource->lr_namespace) {
370                 CERROR("trying to lock bogus namespace: lock %p\n", lock);
371                 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
372                 GOTO(out2, retval);
373         }
374
375         l_lock(&lock->l_resource->lr_namespace->ns_lock);
376         if (strict && lock->l_flags & LDLM_FL_DESTROYED) {
377                 CERROR("lock already destroyed: lock %p\n", lock);
378                 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
379                 GOTO(out, NULL);
380         }
381
382         if (flags && (lock->l_flags & flags)) 
383                 GOTO(out, NULL);
384
385         if (flags)
386                 lock->l_flags |= flags;
387
388         retval = LDLM_LOCK_GET(lock);
389         if (!retval)
390                 CERROR("lock disappeared below us!!! %p\n", lock);
391         EXIT;
392  out:
393         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
394  out2:
395         //spin_unlock(&ldlm_handle_lock);
396         return retval;
397 }
398
399 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
400 {
401         return lockmode_compat(a->l_req_mode, b->l_req_mode);
402 }
403
404 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
405 {
406         ldlm_res2desc(lock->l_resource, &desc->l_resource);
407         desc->l_req_mode = lock->l_req_mode;
408         desc->l_granted_mode = lock->l_granted_mode;
409         memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
410         memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
411 }
412
413 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
414                                    struct ldlm_lock *new)
415 {
416         struct ldlm_ast_work *w;
417         ENTRY;
418
419         l_lock(&lock->l_resource->lr_namespace->ns_lock);
420         if (new && (lock->l_flags & LDLM_FL_AST_SENT))
421                 GOTO(out, 0);
422
423         OBD_ALLOC(w, sizeof(*w));
424         if (!w) {
425                 LBUG();
426                 GOTO(out, 0);
427         }
428
429         if (new) {
430                 lock->l_flags |= LDLM_FL_AST_SENT;
431                 w->w_blocking = 1;
432                 ldlm_lock2desc(new, &w->w_desc);
433         }
434
435         w->w_lock = LDLM_LOCK_GET(lock);
436         list_add(&w->w_list, lock->l_resource->lr_tmp);
437       out:
438         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
439         return;
440 }
441
442 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
443 {
444         struct ldlm_lock *lock;
445
446         lock = ldlm_handle2lock(lockh);
447         ldlm_lock_addref_internal(lock, mode);
448         LDLM_LOCK_PUT(lock);
449 }
450
451 /* only called for local locks */
452 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
453 {
454         l_lock(&lock->l_resource->lr_namespace->ns_lock);
455
456         if (!list_empty(&lock->l_lru)) { 
457                 list_del_init(&lock->l_lru);
458                 lock->l_resource->lr_namespace->ns_nr_unused--;
459                 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
460         }
461
462         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
463                 lock->l_readers++;
464         else
465                 lock->l_writers++;
466         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
467         LDLM_LOCK_GET(lock);
468         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
469 }
470
471 /* Args: unlocked lock */
472 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
473                                     __u64 *res_id, int flags);
474
475 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
476 {
477         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0, 0);
478         struct ldlm_namespace *ns;
479         ENTRY;
480
481         if (lock == NULL)
482                 LBUG();
483
484         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
485         ns = lock->l_resource->lr_namespace;
486         l_lock(&lock->l_resource->lr_namespace->ns_lock);
487         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
488                 lock->l_readers--;
489         else
490                 lock->l_writers--;
491
492         /* If we received a blocked AST and this was the last reference,
493          * run the callback. */
494         if (!lock->l_readers && !lock->l_writers &&
495             (lock->l_flags & LDLM_FL_CBPENDING)) {
496                 if (!lock->l_resource->lr_namespace->ns_client &&
497                     lock->l_export)
498                         CERROR("FL_CBPENDING set on non-local lock--just a "
499                                "warning\n");
500
501                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
502                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
503
504                 /* FIXME: need a real 'desc' here */
505                 lock->l_blocking_ast(lock, NULL, lock->l_data,
506                                      lock->l_data_len, LDLM_CB_BLOCKING);
507         } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
508                 LASSERT(list_empty(&lock->l_lru));
509                 LASSERT(ns->ns_nr_unused >= 0);
510                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
511                 ns->ns_nr_unused++;
512                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
513                 ldlm_cancel_lru(ns);
514         } else
515                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
516
517         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
518         LDLM_LOCK_PUT(lock);    /* matches the handle2lock above */
519
520         EXIT;
521 }
522
523 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
524                                  struct list_head *queue)
525 {
526         struct list_head *tmp, *pos;
527         int rc = 1;
528
529         list_for_each_safe(tmp, pos, queue) {
530                 struct ldlm_lock *child;
531                 ldlm_res_compat compat;
532
533                 child = list_entry(tmp, struct ldlm_lock, l_res_link);
534                 if (lock == child)
535                         continue;
536
537                 compat = ldlm_res_compat_table[child->l_resource->lr_type];
538                 if (compat && compat(child, lock)) {
539                         CDEBUG(D_OTHER, "compat function succeded, next.\n");
540                         continue;
541                 }
542                 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
543                         CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
544                         continue;
545                 }
546
547                 rc = 0;
548
549                 if (send_cbs && child->l_blocking_ast != NULL) {
550                         CDEBUG(D_OTHER, "lock %p incompatible; sending "
551                                "blocking AST.\n", child);
552                         ldlm_add_ast_work_item(child, lock);
553                 }
554         }
555
556         return rc;
557 }
558
559 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
560 {
561         int rc;
562         ENTRY;
563
564         l_lock(&lock->l_resource->lr_namespace->ns_lock);
565         rc = ldlm_lock_compat_list(lock, send_cbs,
566                                    &lock->l_resource->lr_granted);
567         /* FIXME: should we be sending ASTs to converting? */
568         if (rc)
569                 rc = ldlm_lock_compat_list
570                         (lock, send_cbs, &lock->l_resource->lr_converting);
571
572         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
573         RETURN(rc);
574 }
575
576 /* NOTE: called by
577    - ldlm_handle_enqueuque - resource
578 */
579 void ldlm_grant_lock(struct ldlm_lock *lock)
580 {
581         struct ldlm_resource *res = lock->l_resource;
582         ENTRY;
583
584         l_lock(&lock->l_resource->lr_namespace->ns_lock);
585         ldlm_resource_add_lock(res, &res->lr_granted, lock);
586         lock->l_granted_mode = lock->l_req_mode;
587
588         if (lock->l_granted_mode < res->lr_most_restr)
589                 res->lr_most_restr = lock->l_granted_mode;
590
591         if (lock->l_completion_ast) {
592                 ldlm_add_ast_work_item(lock, NULL);
593         }
594         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
595         EXIT;
596 }
597
598 /* returns a referenced lock or NULL */
599 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
600                                       struct ldlm_extent *extent,
601                                       struct ldlm_lock *old_lock)
602 {
603         struct ldlm_lock *lock;
604         struct list_head *tmp;
605
606         list_for_each(tmp, queue) {
607                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
608
609                 if (lock == old_lock)
610                         continue;
611
612                 if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
613                         continue;
614
615                 if (lock->l_req_mode != mode)
616                         continue;
617
618                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
619                     (lock->l_extent.start > extent->start ||
620                      lock->l_extent.end < extent->end))
621                         continue;
622
623                 ldlm_lock_addref_internal(lock, mode);
624                 return lock;
625         }
626
627         return NULL;
628 }
629
630 /* Can be called in two ways:
631  *
632  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
633  * for a duplicate of.
634  *
635  * Otherwise, all of the fields must be filled in, to match against.
636  *
637  * Returns 1 if it finds an already-existing lock that is compatible; in this
638  * case, lockh is filled in with a addref()ed lock
639  */
640 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
641                     void *cookie, int cookielen, ldlm_mode_t mode,
642                     struct lustre_handle *lockh)
643 {
644         struct ldlm_resource *res;
645         struct ldlm_lock *lock, *old_lock = NULL;
646         int rc = 0;
647         ENTRY;
648
649         if (ns == NULL) {
650                 old_lock = ldlm_handle2lock(lockh);
651                 LASSERT(old_lock);
652
653                 ns = old_lock->l_resource->lr_namespace;
654                 res_id = old_lock->l_resource->lr_name;
655                 type = old_lock->l_resource->lr_type;
656                 mode = old_lock->l_req_mode;
657         }
658
659         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
660         if (res == NULL) {
661                 LASSERT(old_lock == NULL);
662                 RETURN(0);
663         }
664
665         l_lock(&ns->ns_lock);
666
667         if ((lock = search_queue(&res->lr_granted, mode, cookie, old_lock)))
668                 GOTO(out, rc = 1);
669         if ((lock = search_queue(&res->lr_converting, mode, cookie, old_lock)))
670                 GOTO(out, rc = 1);
671         if ((lock = search_queue(&res->lr_waiting, mode, cookie, old_lock)))
672                 GOTO(out, rc = 1);
673
674         EXIT;
675        out:
676         ldlm_resource_put(res);
677         l_unlock(&ns->ns_lock);
678
679         if (lock) {
680                 ldlm_lock2handle(lock, lockh);
681                 if (lock->l_completion_ast)
682                         lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
683         }
684         if (rc)
685                 LDLM_DEBUG(lock, "matched");
686         else
687                 LDLM_DEBUG_NOLOCK("not matched");
688
689         if (old_lock)
690                 LDLM_LOCK_PUT(old_lock);
691
692         return rc;
693 }
694
695 /* Returns a referenced lock */
696 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
697                                    struct lustre_handle *parent_lock_handle,
698                                    __u64 * res_id, __u32 type,
699                                    ldlm_mode_t mode, void *data, __u32 data_len)
700 {
701         struct ldlm_resource *res, *parent_res = NULL;
702         struct ldlm_lock *lock, *parent_lock;
703
704         parent_lock = ldlm_handle2lock(parent_lock_handle);
705         if (parent_lock)
706                 parent_res = parent_lock->l_resource;
707
708         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
709         if (res == NULL)
710                 RETURN(NULL);
711
712         lock = ldlm_lock_new(parent_lock, res);
713         if (lock == NULL) {
714                 ldlm_resource_put(res);
715                 RETURN(NULL);
716         }
717
718         lock->l_req_mode = mode;
719         lock->l_data = data;
720         lock->l_data_len = data_len;
721
722         return lock;
723 }
724
725 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
726 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
727                                void *cookie, int cookie_len,
728                                int *flags,
729                                ldlm_completion_callback completion,
730                                ldlm_blocking_callback blocking)
731 {
732         struct ldlm_resource *res;
733         int local;
734         ldlm_res_policy policy;
735         ENTRY;
736
737         res = lock->l_resource;
738         lock->l_blocking_ast = blocking;
739
740         if (res->lr_type == LDLM_EXTENT)
741                 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
742
743         /* policies are not executed on the client or during replay */
744         local = res->lr_namespace->ns_client;
745         if (!local && !(*flags & LDLM_FL_REPLAY) &&
746             (policy = ldlm_res_policy_table[res->lr_type])) {
747                 int rc;
748                 rc = policy(lock, cookie, lock->l_req_mode, *flags, NULL);
749
750                 if (rc == ELDLM_LOCK_CHANGED) {
751                         res = lock->l_resource;
752                         *flags |= LDLM_FL_LOCK_CHANGED;
753                 } else if (rc == ELDLM_LOCK_ABORTED) {
754                         ldlm_lock_destroy(lock);
755                         RETURN(rc);
756                 }
757         }
758
759         l_lock(&res->lr_namespace->ns_lock);
760         if (local && lock->l_req_mode == lock->l_granted_mode) {
761                 /* The server returned a blocked lock, but it was granted before
762                  * we got a chance to actually enqueue it.  We don't need to do
763                  * anything else. */
764                 *flags &= ~(LDLM_FL_BLOCK_GRANTED | 
765                           LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
766                 GOTO(out, ELDLM_OK);
767         }
768
769         /* This distinction between local lock trees is very important; a client
770          * namespace only has information about locks taken by that client, and
771          * thus doesn't have enough information to decide for itself if it can
772          * be granted (below).  In this case, we do exactly what the server
773          * tells us to do, as dictated by the 'flags'.
774          *
775          * We do exactly the same thing during recovery, when the server is
776          * more or less trusting the clients not to lie.
777          *
778          * FIXME (bug 629283): Detect obvious lies by checking compatibility in
779          * granted/converting queues. */
780         ldlm_resource_unlink_lock(lock);
781         if (local || (*flags & LDLM_FL_REPLAY)) {
782                 if (*flags & LDLM_FL_BLOCK_CONV)
783                         ldlm_resource_add_lock(res, res->lr_converting.prev,
784                                                lock);
785                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
786                         ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
787                 else
788                         ldlm_grant_lock(lock);
789                 GOTO(out, ELDLM_OK);
790         }
791
792         /* FIXME: We may want to optimize by checking lr_most_restr */
793         if (!list_empty(&res->lr_converting)) {
794                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
795                 *flags |= LDLM_FL_BLOCK_CONV;
796                 GOTO(out, ELDLM_OK);
797         }
798         if (!list_empty(&res->lr_waiting)) {
799                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
800                 *flags |= LDLM_FL_BLOCK_WAIT;
801                 GOTO(out, ELDLM_OK);
802         }
803         if (!ldlm_lock_compat(lock, 0)) {
804                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
805                 *flags |= LDLM_FL_BLOCK_GRANTED;
806                 GOTO(out, ELDLM_OK);
807         }
808
809         ldlm_grant_lock(lock);
810         EXIT;
811       out:
812         l_unlock(&res->lr_namespace->ns_lock);
813         /* Don't set 'completion_ast' until here so that if the lock is granted
814          * immediately we don't do an unnecessary completion call. */
815         lock->l_completion_ast = completion;
816         return ELDLM_OK;
817 }
818
819 /* Must be called with namespace taken: queue is waiting or converting. */
820 static int ldlm_reprocess_queue(struct ldlm_resource *res,
821                                 struct list_head *queue)
822 {
823         struct list_head *tmp, *pos;
824         ENTRY;
825
826         list_for_each_safe(tmp, pos, queue) {
827                 struct ldlm_lock *pending;
828                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
829
830                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
831
832                 if (!ldlm_lock_compat(pending, 1))
833                         RETURN(1);
834
835                 list_del_init(&pending->l_res_link);
836                 ldlm_grant_lock(pending);
837         }
838
839         RETURN(0);
840 }
841
842 void ldlm_run_ast_work(struct list_head *rpc_list)
843 {
844         struct list_head *tmp, *pos;
845         int rc;
846         ENTRY;
847
848         list_for_each_safe(tmp, pos, rpc_list) {
849                 struct ldlm_ast_work *w =
850                         list_entry(tmp, struct ldlm_ast_work, w_list);
851
852                 if (w->w_blocking)
853                         rc = w->w_lock->l_blocking_ast
854                                 (w->w_lock, &w->w_desc, w->w_data,
855                                  w->w_datalen, LDLM_CB_BLOCKING);
856                 else
857                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
858                 if (rc)
859                         CERROR("Failed AST - should clean & disconnect "
860                                "client\n");
861                 LDLM_LOCK_PUT(w->w_lock);
862                 list_del(&w->w_list);
863                 OBD_FREE(w, sizeof(*w));
864         }
865         EXIT;
866 }
867
868 /* Must be called with resource->lr_lock not taken. */
869 void ldlm_reprocess_all(struct ldlm_resource *res)
870 {
871         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
872         ENTRY;
873
874         /* Local lock trees don't get reprocessed. */
875         if (res->lr_namespace->ns_client) {
876                 EXIT;
877                 return;
878         }
879
880         l_lock(&res->lr_namespace->ns_lock);
881         res->lr_tmp = &rpc_list;
882
883         ldlm_reprocess_queue(res, &res->lr_converting);
884         if (list_empty(&res->lr_converting))
885                 ldlm_reprocess_queue(res, &res->lr_waiting);
886
887         res->lr_tmp = NULL;
888         l_unlock(&res->lr_namespace->ns_lock);
889
890         ldlm_run_ast_work(&rpc_list);
891         EXIT;
892 }
893
894 void ldlm_cancel_callback(struct ldlm_lock *lock)
895 {
896         l_lock(&lock->l_resource->lr_namespace->ns_lock);
897         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
898                 lock->l_flags |= LDLM_FL_CANCEL;
899                 if (lock->l_blocking_ast)
900                         lock->l_blocking_ast(lock, NULL, lock->l_data,
901                                              lock->l_data_len,
902                                              LDLM_CB_CANCELING);
903                 else
904                         LDLM_DEBUG(lock, "no blocking ast");
905         }
906         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
907 }
908
909 void ldlm_lock_cancel(struct ldlm_lock *lock)
910 {
911         struct ldlm_resource *res;
912         struct ldlm_namespace *ns;
913         ENTRY;
914
915         res = lock->l_resource;
916         ns = res->lr_namespace;
917
918         l_lock(&ns->ns_lock);
919         if (lock->l_readers || lock->l_writers)
920                 LDLM_DEBUG(lock, "lock still has references");
921
922         ldlm_cancel_callback(lock);
923
924         ldlm_del_waiting_lock(lock);
925         ldlm_resource_unlink_lock(lock);
926         ldlm_lock_destroy(lock);
927         l_unlock(&ns->ns_lock);
928         EXIT;
929 }
930
931 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data, int datalen)
932 {
933         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
934         ENTRY;
935
936         if (lock == NULL)
937                 RETURN(-EINVAL);
938
939         lock->l_data = data;
940         lock->l_data_len = datalen;
941
942         LDLM_LOCK_PUT(lock);
943
944         RETURN(0);
945 }
946
947 void ldlm_cancel_locks_for_export(struct obd_export *exp)
948 {
949         struct list_head *iter, *n; /* MUST BE CALLED "n"! */
950
951         list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
952                 struct ldlm_lock *lock;
953                 struct ldlm_resource *res;
954                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
955                 res = ldlm_resource_getref(lock->l_resource);
956                 LDLM_DEBUG(lock, "export %p", exp);
957                 ldlm_lock_cancel(lock);
958                 ldlm_reprocess_all(res);
959                 ldlm_resource_put(res);
960         }
961 }
962
963 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
964                                         int *flags)
965 {
966         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
967         struct ldlm_resource *res;
968         struct ldlm_namespace *ns;
969         int granted = 0;
970         ENTRY;
971
972         res = lock->l_resource;
973         ns = res->lr_namespace;
974
975         l_lock(&ns->ns_lock);
976
977         lock->l_req_mode = new_mode;
978         ldlm_resource_unlink_lock(lock);
979
980         /* If this is a local resource, put it on the appropriate list. */
981         if (res->lr_namespace->ns_client) {
982                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
983                         ldlm_resource_add_lock(res, res->lr_converting.prev,
984                                                lock);
985                 else {
986                         /* This should never happen, because of the way the
987                          * server handles conversions. */
988                         LBUG();
989
990                         res->lr_tmp = &rpc_list;
991                         ldlm_grant_lock(lock);
992                         res->lr_tmp = NULL;
993                         granted = 1;
994                         /* FIXME: completion handling not with ns_lock held ! */
995                         if (lock->l_completion_ast)
996                                 lock->l_completion_ast(lock, 0);
997                 }
998         } else {
999                 /* FIXME: We should try the conversion right away and possibly
1000                  * return success without the need for an extra AST */
1001                 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
1002                 *flags |= LDLM_FL_BLOCK_CONV;
1003         }
1004
1005         l_unlock(&ns->ns_lock);
1006
1007         if (granted)
1008                 ldlm_run_ast_work(&rpc_list);
1009         RETURN(res);
1010 }
1011
1012 void ldlm_lock_dump(struct ldlm_lock *lock)
1013 {
1014         char ver[128];
1015
1016         if (!(portal_debug & D_OTHER))
1017                 return;
1018
1019         if (RES_VERSION_SIZE != 4)
1020                 LBUG();
1021
1022         if (!lock) {
1023                 CDEBUG(D_OTHER, "  NULL LDLM lock\n");
1024                 return;
1025         }
1026
1027         snprintf(ver, sizeof(ver), "%x %x %x %x",
1028                  lock->l_version[0], lock->l_version[1],
1029                  lock->l_version[2], lock->l_version[3]);
1030
1031         CDEBUG(D_OTHER, "  -- Lock dump: %p (%s)\n", lock, ver);
1032         if (lock->l_export && lock->l_export->exp_connection)
1033                 CDEBUG(D_OTHER, "  Node: NID %x (rhandle: "LPX64")\n",
1034                        lock->l_export->exp_connection->c_peer.peer_nid,
1035                        lock->l_remote_handle.addr);
1036         else
1037                 CDEBUG(D_OTHER, "  Node: local\n");
1038         CDEBUG(D_OTHER, "  Parent: %p\n", lock->l_parent);
1039         CDEBUG(D_OTHER, "  Resource: %p ("LPD64")\n", lock->l_resource,
1040                lock->l_resource->lr_name[0]);
1041         CDEBUG(D_OTHER, "  Requested mode: %d, granted mode: %d\n",
1042                (int)lock->l_req_mode, (int)lock->l_granted_mode);
1043         CDEBUG(D_OTHER, "  Readers: %u ; Writers; %u\n",
1044                lock->l_readers, lock->l_writers);
1045         if (lock->l_resource->lr_type == LDLM_EXTENT)
1046                 CDEBUG(D_OTHER, "  Extent: %Lu -> %Lu\n",
1047                        (unsigned long long)lock->l_extent.start,
1048                        (unsigned long long)lock->l_extent.end);
1049 }