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