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