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