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