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