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