Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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, 2003 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 #ifdef __KERNEL__
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/lustre_mds.h>
31 #else
32 #include <liblustre.h>
33 #include <linux/kp30.h>
34 #endif
35
36 #include <linux/obd_class.h>
37
38 //struct lustre_lock ldlm_everything_lock;
39
40 /* lock types */
41 char *ldlm_lockname[] = {
42         [0] "--",
43         [LCK_EX] "EX",
44         [LCK_PW] "PW",
45         [LCK_PR] "PR",
46         [LCK_CW] "CW",
47         [LCK_CR] "CR",
48         [LCK_NL] "NL"
49 };
50 char *ldlm_typename[] = {
51         [LDLM_PLAIN] "PLN",
52         [LDLM_EXTENT] "EXT",
53 };
54
55 #ifdef __KERNEL__
56 char *ldlm_it2str(int it)
57 {
58         switch (it) {
59         case IT_OPEN:
60                 return "open";
61         case IT_CREAT:
62                 return "creat";
63         case (IT_OPEN | IT_CREAT):
64                 return "open|creat";
65         case IT_READDIR:
66                 return "readdir";
67         case IT_GETATTR:
68                 return "getattr";
69         case IT_LOOKUP:
70                 return "lookup";
71         case IT_UNLINK:
72                 return "unlink";
73         default:
74                 CERROR("Unknown intent %d\n", it);
75                 return "UNKNOWN";
76         }
77 }
78 #endif
79
80 extern kmem_cache_t *ldlm_lock_slab;
81 struct lustre_lock ldlm_handle_lock;
82
83 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
84
85 static ldlm_res_compat ldlm_res_compat_table[] = {
86         [LDLM_PLAIN] ldlm_plain_compat,
87         [LDLM_EXTENT] ldlm_extent_compat,
88 };
89
90 static ldlm_res_policy ldlm_intent_policy_func;
91
92 static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
93                              void *req_cookie, ldlm_mode_t mode, int flags,
94                              void *data)
95 {
96         if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
97                 return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
98                                                flags, data);
99         }
100
101         return ELDLM_OK;
102 }
103
104 static ldlm_res_policy ldlm_res_policy_table[] = {
105         [LDLM_PLAIN] ldlm_plain_policy,
106         [LDLM_EXTENT] ldlm_extent_policy,
107 };
108
109 void ldlm_register_intent(ldlm_res_policy arg)
110 {
111         ldlm_intent_policy_func = arg;
112 }
113
114 void ldlm_unregister_intent(void)
115 {
116         ldlm_intent_policy_func = NULL;
117 }
118
119 /*
120  * REFCOUNTED LOCK OBJECTS
121  */
122
123
124 /*
125  * Lock refcounts, during creation:
126  *   - one special one for allocation, dec'd only once in destroy
127  *   - one for being a lock that's in-use
128  *   - one for the addref associated with a new lock
129  */
130 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
131 {
132         atomic_inc(&lock->l_refc);
133         return lock;
134 }
135
136 void ldlm_lock_put(struct ldlm_lock *lock)
137 {
138         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
139         ENTRY;
140
141         if (atomic_dec_and_test(&lock->l_refc)) {
142                 l_lock(&ns->ns_lock);
143                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
144                 LASSERT(lock->l_destroyed);
145                 LASSERT(list_empty(&lock->l_res_link));
146
147                 spin_lock(&ns->ns_counter_lock);
148                 ns->ns_locks--;
149                 spin_unlock(&ns->ns_counter_lock);
150
151                 ldlm_resource_putref(lock->l_resource);
152                 lock->l_resource = NULL;
153
154                 if (lock->l_parent)
155                         LDLM_LOCK_PUT(lock->l_parent);
156
157                 PORTAL_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
158                 l_unlock(&ns->ns_lock);
159         }
160
161         EXIT;
162 }
163
164 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
165 {
166         ENTRY;
167         l_lock(&lock->l_resource->lr_namespace->ns_lock);
168         if (!list_empty(&lock->l_lru)) {
169                 list_del_init(&lock->l_lru);
170                 lock->l_resource->lr_namespace->ns_nr_unused--;
171                 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
172         }
173         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
174         EXIT;
175 }
176
177 /* This used to have a 'strict' flact, which recovery would use to mark an
178  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
179  * shall explain why it's gone: with the new hash table scheme, once you call
180  * ldlm_lock_destroy, you can never drop your final references on this lock.
181  * Because it's not in the hash table anymore.  -phil */
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_ERROR(lock, "still has children (%p)!",
189                            lock->l_children.next);
190                 ldlm_lock_dump(D_ERROR, lock);
191                 LBUG();
192         }
193         if (lock->l_readers || lock->l_writers) {
194                 LDLM_ERROR(lock, "lock still has references");
195                 ldlm_lock_dump(D_ERROR, lock);
196                 LBUG();
197         }
198
199         if (!list_empty(&lock->l_res_link)) {
200                 ldlm_lock_dump(D_ERROR, lock);
201                 LBUG();
202         }
203
204         if (lock->l_destroyed) {
205                 LASSERT(list_empty(&lock->l_lru));
206                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
207                 EXIT;
208                 return;
209         }
210         lock->l_destroyed = 1;
211
212         list_del_init(&lock->l_export_chain);
213         ldlm_lock_remove_from_lru(lock);
214         class_handle_unhash(&lock->l_handle);
215
216 #if 0
217         /* Wake anyone waiting for this lock */
218         /* FIXME: I should probably add yet another flag, instead of using
219          * l_export to only call this on clients */
220         lock->l_export = NULL;
221         if (lock->l_export && lock->l_completion_ast)
222                 lock->l_completion_ast(lock, 0);
223 #endif
224
225         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
226         LDLM_LOCK_PUT(lock);
227         EXIT;
228 }
229
230 /* this is called by portals_handle2object with the handle lock taken */
231 static void lock_handle_addref(void *lock)
232 {
233         LDLM_LOCK_GET((struct ldlm_lock *)lock);
234 }
235
236 /*
237  * usage: pass in a resource on which you have done ldlm_resource_get
238  *        pass in a parent lock on which you have done a ldlm_lock_get
239  *        after return, ldlm_*_put the resource and parent
240  * returns: lock with refcount 1
241  */
242 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
243                                        struct ldlm_resource *resource)
244 {
245         struct ldlm_lock *lock;
246         ENTRY;
247
248         if (resource == NULL)
249                 LBUG();
250
251         PORTAL_SLAB_ALLOC(lock, ldlm_lock_slab, sizeof(*lock));
252         if (lock == NULL)
253                 RETURN(NULL);
254
255         lock->l_resource = ldlm_resource_getref(resource);
256
257         atomic_set(&lock->l_refc, 2);
258         INIT_LIST_HEAD(&lock->l_children);
259         INIT_LIST_HEAD(&lock->l_res_link);
260         INIT_LIST_HEAD(&lock->l_lru);
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 = LDLM_LOCK_GET(parent);
272                 list_add(&lock->l_childof, &parent->l_children);
273                 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
274         }
275
276         INIT_LIST_HEAD(&lock->l_handle.h_link);
277         class_handle_hash(&lock->l_handle, lock_handle_addref);
278
279         RETURN(lock);
280 }
281
282 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
283                               struct ldlm_res_id new_resid)
284 {
285         struct ldlm_resource *oldres = lock->l_resource;
286         ENTRY;
287
288         l_lock(&ns->ns_lock);
289         if (memcmp(&new_resid, &lock->l_resource->lr_name,
290                    sizeof(lock->l_resource->lr_name)) == 0) {
291                 /* Nothing to do */
292                 l_unlock(&ns->ns_lock);
293                 RETURN(0);
294         }
295
296         LASSERT(new_resid.name[0] != 0);
297
298         /* This function assumes that the lock isn't on any lists */
299         LASSERT(list_empty(&lock->l_res_link));
300
301         lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
302                                              lock->l_resource->lr_type, 1);
303         if (lock->l_resource == NULL) {
304                 LBUG();
305                 RETURN(-ENOMEM);
306         }
307
308         /* ...and the flowers are still standing! */
309         ldlm_resource_putref(oldres);
310
311         l_unlock(&ns->ns_lock);
312         RETURN(0);
313 }
314
315 /*
316  *  HANDLES
317  */
318
319 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
320 {
321         POISON(&lockh->addr, 0x69, sizeof(lockh->addr));
322         lockh->cookie = lock->l_handle.h_cookie;
323 }
324
325 /* if flags: atomically get the lock and set the flags.
326  *           Return NULL if flag already set
327  */
328
329 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
330 {
331         struct ldlm_lock *lock = NULL, *retval = NULL;
332         ENTRY;
333
334         LASSERT(handle);
335
336         lock = class_handle2object(handle->cookie);
337         if (lock == NULL)
338                 RETURN(NULL);
339
340         LASSERT(lock->l_resource != NULL);
341         LASSERT(lock->l_resource->lr_namespace != NULL);
342
343         l_lock(&lock->l_resource->lr_namespace->ns_lock);
344
345         /* It's unlikely but possible that someone marked the lock as
346          * destroyed after we did handle2object on it */
347         if (lock->l_destroyed) {
348                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
349                 LDLM_LOCK_PUT(lock);
350                 GOTO(out, retval);
351         }
352
353         if (flags && (lock->l_flags & flags)) {
354                 LDLM_LOCK_PUT(lock);
355                 GOTO(out, retval);
356         }
357
358         if (flags)
359                 lock->l_flags |= flags;
360
361         retval = lock;
362         EXIT;
363  out:
364         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
365         return retval;
366 }
367
368 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
369                                       struct lustre_handle *handle)
370 {
371         struct ldlm_lock *retval = NULL;
372
373         l_lock(&ns->ns_lock);
374         retval = __ldlm_handle2lock(handle, 0);
375         l_unlock(&ns->ns_lock);
376
377         return retval;
378 }
379
380 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
381 {
382         return lockmode_compat(a->l_req_mode, b->l_req_mode);
383 }
384
385 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
386 {
387         ldlm_res2desc(lock->l_resource, &desc->l_resource);
388         desc->l_req_mode = lock->l_req_mode;
389         desc->l_granted_mode = lock->l_granted_mode;
390         memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
391         memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
392 }
393
394 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
395                                    struct ldlm_lock *new,
396                                    void *data, int datalen)
397 {
398         struct ldlm_ast_work *w;
399         ENTRY;
400
401         l_lock(&lock->l_resource->lr_namespace->ns_lock);
402         if (new && (lock->l_flags & LDLM_FL_AST_SENT))
403                 GOTO(out, 0);
404
405         OBD_ALLOC(w, sizeof(*w));
406         if (!w) {
407                 LBUG();
408                 GOTO(out, 0);
409         }
410
411         w->w_data = data;
412         w->w_datalen = datalen;
413         if (new) {
414                 lock->l_flags |= LDLM_FL_AST_SENT;
415                 w->w_blocking = 1;
416                 ldlm_lock2desc(new, &w->w_desc);
417         }
418
419         w->w_lock = LDLM_LOCK_GET(lock);
420         list_add(&w->w_list, lock->l_resource->lr_tmp);
421         EXIT;
422  out:
423         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
424         return;
425 }
426
427 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
428 {
429         struct ldlm_lock *lock;
430
431         lock = ldlm_handle2lock(lockh);
432         ldlm_lock_addref_internal(lock, mode);
433         LDLM_LOCK_PUT(lock);
434 }
435
436 /* only called for local locks */
437 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
438 {
439         l_lock(&lock->l_resource->lr_namespace->ns_lock);
440         ldlm_lock_remove_from_lru(lock);
441         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
442                 lock->l_readers++;
443         else
444                 lock->l_writers++;
445         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
446         LDLM_LOCK_GET(lock);
447         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
448 }
449
450 /* Args: unlocked lock */
451 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
452                                     struct ldlm_res_id, int flags);
453
454 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
455 {
456         struct ldlm_namespace *ns;
457         ENTRY;
458
459         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
460         ns = lock->l_resource->lr_namespace;
461         l_lock(&ns->ns_lock);
462         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
463                 LASSERT(lock->l_readers > 0);
464                 lock->l_readers--;
465         } else {
466                 LASSERT(lock->l_writers > 0);
467                 lock->l_writers--;
468         }
469
470         if (lock->l_flags & LDLM_FL_LOCAL &&
471             !lock->l_readers && !lock->l_writers) {
472                 /* If this is a local lock on a server namespace and this was
473                  * the last reference, cancel the lock. */
474                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
475                 lock->l_flags |= LDLM_FL_CBPENDING;
476         }
477
478         if (!lock->l_readers && !lock->l_writers &&
479             (lock->l_flags & LDLM_FL_CBPENDING)) {
480                 /* If we received a blocked AST and this was the last reference,
481                  * run the callback. */
482                 if (!ns->ns_client && lock->l_export)
483                         CERROR("FL_CBPENDING set on non-local lock--just a "
484                                "warning\n");
485
486                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
487
488                 if (lock->l_blocking_ast == NULL) {
489                         /* The lock wasn't even fully formed; just destroy it */
490                         ldlm_lock_destroy(lock);
491                 }
492                 l_unlock(&ns->ns_lock);
493
494                 /* FIXME: need a real 'desc' here */
495                 if (lock->l_blocking_ast != NULL)
496                         lock->l_blocking_ast(lock, NULL, lock->l_data,
497                                              LDLM_CB_BLOCKING);
498         } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
499                 /* If this is a client-side namespace and this was the last
500                  * reference, put it on the LRU. */
501                 LASSERT(list_empty(&lock->l_lru));
502                 LASSERT(ns->ns_nr_unused >= 0);
503                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
504                 ns->ns_nr_unused++;
505                 l_unlock(&ns->ns_lock);
506                 ldlm_cancel_lru(ns);
507         } else {
508                 l_unlock(&ns->ns_lock);
509         }
510
511         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
512
513         EXIT;
514 }
515
516 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
517 {
518         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
519         LASSERT(lock != NULL);
520         ldlm_lock_decref_internal(lock, mode);
521         LDLM_LOCK_PUT(lock);
522 }
523
524 /* This will drop a lock reference and mark it for destruction, but will not
525  * necessarily cancel the lock before returning. */
526 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
527 {
528         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
529         ENTRY;
530
531         LASSERT(lock != NULL);
532
533         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
534         l_lock(&lock->l_resource->lr_namespace->ns_lock);
535         lock->l_flags |= LDLM_FL_CBPENDING;
536         ldlm_lock_decref_internal(lock, mode);
537         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
538         LDLM_LOCK_PUT(lock);
539 }
540
541 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
542                                  struct list_head *queue)
543 {
544         struct list_head *tmp, *pos;
545         int rc = 1;
546
547         list_for_each_safe(tmp, pos, queue) {
548                 struct ldlm_lock *child;
549                 ldlm_res_compat compat;
550
551                 child = list_entry(tmp, struct ldlm_lock, l_res_link);
552                 if (lock == child)
553                         continue;
554
555                 compat = ldlm_res_compat_table[child->l_resource->lr_type];
556                 if (compat && compat(child, lock)) {
557                         CDEBUG(D_OTHER, "compat function succeded, next.\n");
558                         continue;
559                 }
560                 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
561                         CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
562                         continue;
563                 }
564
565                 rc = 0;
566
567                 if (send_cbs && child->l_blocking_ast != NULL) {
568                         CDEBUG(D_OTHER, "lock %p incompatible; sending "
569                                "blocking AST.\n", child);
570                         ldlm_add_ast_work_item(child, lock, NULL, 0);
571                 }
572         }
573
574         return rc;
575 }
576
577 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
578 {
579         int rc;
580         ENTRY;
581
582         l_lock(&lock->l_resource->lr_namespace->ns_lock);
583         rc = ldlm_lock_compat_list(lock, send_cbs,
584                                    &lock->l_resource->lr_granted);
585         /* FIXME: should we be sending ASTs to converting? */
586         if (rc)
587                 rc = ldlm_lock_compat_list
588                         (lock, send_cbs, &lock->l_resource->lr_converting);
589
590         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
591         RETURN(rc);
592 }
593
594 /* NOTE: called by
595  *  - ldlm_lock_enqueue
596  *  - ldlm_reprocess_queue
597  *  - ldlm_lock_convert
598  */
599 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
600 {
601         struct ldlm_resource *res = lock->l_resource;
602         ENTRY;
603
604         l_lock(&lock->l_resource->lr_namespace->ns_lock);
605         ldlm_resource_add_lock(res, &res->lr_granted, lock);
606         lock->l_granted_mode = lock->l_req_mode;
607
608         if (lock->l_granted_mode < res->lr_most_restr)
609                 res->lr_most_restr = lock->l_granted_mode;
610
611         if (lock->l_completion_ast != NULL)
612                 ldlm_add_ast_work_item(lock, NULL, data, datalen);
613
614         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
615         EXIT;
616 }
617
618 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
619  * comment above ldlm_lock_match */
620 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
621                                       struct ldlm_extent *extent,
622                                       struct ldlm_lock *old_lock, int flags)
623 {
624         struct ldlm_lock *lock;
625         struct list_head *tmp;
626
627         list_for_each(tmp, queue) {
628                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
629
630                 if (lock == old_lock)
631                         break;
632
633                 if (lock->l_flags & LDLM_FL_CBPENDING)
634                         continue;
635
636                 if (lock->l_req_mode != mode)
637                         continue;
638
639                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
640                     (lock->l_extent.start > extent->start ||
641                      lock->l_extent.end < extent->end))
642                         continue;
643
644                 if (lock->l_destroyed)
645                         continue;
646
647                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
648                     !(lock->l_flags & LDLM_FL_LOCAL))
649                         continue;
650
651                 ldlm_lock_addref_internal(lock, mode);
652                 return lock;
653         }
654
655         return NULL;
656 }
657
658 /* Can be called in two ways:
659  *
660  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
661  * for a duplicate of.
662  *
663  * Otherwise, all of the fields must be filled in, to match against.
664  *
665  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
666  *     server (ie, connh is NULL)
667  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
668  *     list will be considered
669  *
670  * Returns 1 if it finds an already-existing lock that is compatible; in this
671  * case, lockh is filled in with a addref()ed lock
672  */
673 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
674                     struct ldlm_res_id *res_id, __u32 type, void *cookie,
675                     int cookielen, ldlm_mode_t mode,struct lustre_handle *lockh)
676 {
677         struct ldlm_resource *res;
678         struct ldlm_lock *lock, *old_lock = NULL;
679         int rc = 0;
680         ENTRY;
681
682         if (ns == NULL) {
683                 old_lock = ldlm_handle2lock(lockh);
684                 LASSERT(old_lock);
685
686                 ns = old_lock->l_resource->lr_namespace;
687                 res_id = &old_lock->l_resource->lr_name;
688                 type = old_lock->l_resource->lr_type;
689                 mode = old_lock->l_req_mode;
690         }
691
692         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
693         if (res == NULL) {
694                 LASSERT(old_lock == NULL);
695                 RETURN(0);
696         }
697
698         l_lock(&ns->ns_lock);
699
700         lock = search_queue(&res->lr_granted, mode, cookie, old_lock, flags);
701         if (lock != NULL)
702                 GOTO(out, rc = 1);
703         if (flags & LDLM_FL_BLOCK_GRANTED)
704                 GOTO(out, rc = 0);
705         lock = search_queue(&res->lr_converting, mode, cookie, old_lock, flags);
706         if (lock != NULL)
707                 GOTO(out, rc = 1);
708         lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, flags);
709         if (lock != NULL)
710                 GOTO(out, rc = 1);
711
712         EXIT;
713        out:
714         ldlm_resource_putref(res);
715         l_unlock(&ns->ns_lock);
716
717         if (lock) {
718                 ldlm_lock2handle(lock, lockh);
719                 if (lock->l_completion_ast)
720                         lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
721         }
722         if (rc)
723                 LDLM_DEBUG(lock, "matched");
724         else
725                 LDLM_DEBUG_NOLOCK("not matched");
726
727         if (old_lock)
728                 LDLM_LOCK_PUT(old_lock);
729
730         return rc;
731 }
732
733 /* Returns a referenced lock */
734 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
735                                    struct lustre_handle *parent_lock_handle,
736                                    struct ldlm_res_id res_id, __u32 type,
737                                    ldlm_mode_t mode, void *data, void *cp_data)
738 {
739         struct ldlm_resource *res, *parent_res = NULL;
740         struct ldlm_lock *lock, *parent_lock = NULL;
741         ENTRY;
742
743         if (parent_lock_handle) {
744                 parent_lock = ldlm_handle2lock(parent_lock_handle);
745                 if (parent_lock)
746                         parent_res = parent_lock->l_resource;
747         }
748
749         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
750         if (res == NULL)
751                 RETURN(NULL);
752
753         lock = ldlm_lock_new(parent_lock, res);
754         ldlm_resource_putref(res);
755         if (parent_lock != NULL)
756                 LDLM_LOCK_PUT(parent_lock);
757
758         if (lock == NULL)
759                 RETURN(NULL);
760
761         lock->l_req_mode = mode;
762         lock->l_data = data;
763         lock->l_cp_data = cp_data;
764
765         RETURN(lock);
766 }
767
768 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
769                                struct ldlm_lock **lockp,
770                                void *cookie, int cookie_len,
771                                int *flags,
772                                ldlm_completion_callback completion,
773                                ldlm_blocking_callback blocking)
774 {
775         struct ldlm_resource *res;
776         struct ldlm_lock *lock = *lockp;
777         int local;
778         ldlm_res_policy policy;
779         ENTRY;
780
781         res = lock->l_resource;
782         lock->l_blocking_ast = blocking;
783
784         if (res->lr_type == LDLM_EXTENT)
785                 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
786
787         /* policies are not executed on the client or during replay */
788         local = res->lr_namespace->ns_client;
789         if (!local && !(*flags & LDLM_FL_REPLAY) &&
790             (policy = ldlm_res_policy_table[res->lr_type])) {
791                 int rc;
792                 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
793                 if (rc == ELDLM_LOCK_CHANGED) {
794                         res = lock->l_resource;
795                         *flags |= LDLM_FL_LOCK_CHANGED;
796                 } else if (rc == ELDLM_LOCK_REPLACED) {
797                         /* The lock that was returned has already been granted,
798                          * and placed into lockp.  Destroy the old one and our
799                          * work here is done. */
800                         ldlm_lock_destroy(lock);
801                         LDLM_LOCK_PUT(lock);
802                         *flags |= LDLM_FL_LOCK_CHANGED;
803                         RETURN(0);
804                 } else if (rc == ELDLM_LOCK_ABORTED) {
805                         ldlm_lock_destroy(lock);
806                         RETURN(rc);
807                 }
808         }
809
810         l_lock(&ns->ns_lock);
811         if (local && lock->l_req_mode == lock->l_granted_mode) {
812                 /* The server returned a blocked lock, but it was granted before
813                  * we got a chance to actually enqueue it.  We don't need to do
814                  * anything else. */
815                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
816                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
817                 GOTO(out, ELDLM_OK);
818         }
819
820         /* This distinction between local lock trees is very important; a client
821          * namespace only has information about locks taken by that client, and
822          * thus doesn't have enough information to decide for itself if it can
823          * be granted (below).  In this case, we do exactly what the server
824          * tells us to do, as dictated by the 'flags'.
825          *
826          * We do exactly the same thing during recovery, when the server is
827          * more or less trusting the clients not to lie.
828          *
829          * FIXME (bug 268): Detect obvious lies by checking compatibility in
830          * granted/converting queues. */
831         ldlm_resource_unlink_lock(lock);
832         if (local) {
833                 if (*flags & LDLM_FL_BLOCK_CONV)
834                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
835                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
836                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
837                 else
838                         ldlm_grant_lock(lock, NULL, 0);
839                 GOTO(out, ELDLM_OK);
840         } else if (*flags & LDLM_FL_REPLAY) {
841                 if (*flags & LDLM_FL_BLOCK_CONV) {
842                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
843                         GOTO(out, ELDLM_OK);
844                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
845                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
846                         GOTO(out, ELDLM_OK);
847                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
848                         ldlm_grant_lock(lock, NULL, 0);
849                         GOTO(out, ELDLM_OK);
850                 }
851                 /* If no flags, fall through to normal enqueue path. */
852         }
853
854         /* FIXME: We may want to optimize by checking lr_most_restr */
855         if (!list_empty(&res->lr_converting)) {
856                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
857                 *flags |= LDLM_FL_BLOCK_CONV;
858                 GOTO(out, ELDLM_OK);
859         }
860         if (!list_empty(&res->lr_waiting)) {
861                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
862                 *flags |= LDLM_FL_BLOCK_WAIT;
863                 GOTO(out, ELDLM_OK);
864         }
865         if (!ldlm_lock_compat(lock, 0)) {
866                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
867                 *flags |= LDLM_FL_BLOCK_GRANTED;
868                 GOTO(out, ELDLM_OK);
869         }
870
871         if (lock->l_granted_cb != NULL && lock->l_data != NULL) {
872                 /* We just -know- */
873                 struct ptlrpc_request *req = lock->l_data;
874                 lock->l_granted_cb(lock, req->rq_repmsg, 0);
875         }
876         ldlm_grant_lock(lock, NULL, 0);
877         EXIT;
878       out:
879         /* Don't set 'completion_ast' until here so that if the lock is granted
880          * immediately we don't do an unnecessary completion call. */
881         lock->l_completion_ast = completion;
882         l_unlock(&ns->ns_lock);
883         return ELDLM_OK;
884 }
885
886 /* Must be called with namespace taken: queue is waiting or converting. */
887 static int ldlm_reprocess_queue(struct ldlm_resource *res,
888                                 struct list_head *queue)
889 {
890         struct list_head *tmp, *pos;
891         ENTRY;
892
893         list_for_each_safe(tmp, pos, queue) {
894                 struct ldlm_lock *pending;
895                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
896
897                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
898
899                 if (!ldlm_lock_compat(pending, 1))
900                         RETURN(1);
901
902                 list_del_init(&pending->l_res_link);
903                 ldlm_grant_lock(pending, NULL, 0);
904         }
905
906         RETURN(0);
907 }
908
909 int ldlm_run_ast_work(struct list_head *rpc_list)
910 {
911         struct list_head *tmp, *pos;
912         int rc, retval = 0;
913         ENTRY;
914
915         list_for_each_safe(tmp, pos, rpc_list) {
916                 struct ldlm_ast_work *w =
917                         list_entry(tmp, struct ldlm_ast_work, w_list);
918
919                 /* It's possible to receive a completion AST before we've set
920                  * the l_completion_ast pointer: either because the AST arrived
921                  * before the reply, or simply because there's a small race
922                  * window between receiving the reply and finishing the local
923                  * enqueue. (bug 842)
924                  *
925                  * This can't happen with the blocking_ast, however, because we
926                  * will never call the local blocking_ast until we drop our
927                  * reader/writer reference, which we won't do until we get the
928                  * reply and finish enqueueing. */
929                 if (w->w_blocking) {
930                         LASSERT(w->w_lock->l_blocking_ast != NULL);
931                         rc = w->w_lock->l_blocking_ast
932                                 (w->w_lock, &w->w_desc, w->w_data,
933                                  LDLM_CB_BLOCKING);
934                 } else if (w->w_lock->l_completion_ast != NULL) {
935                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
936                                                          w->w_data);
937                 } else {
938                         rc = 0;
939                 }
940                 if (rc == -ERESTART)
941                         retval = rc;
942                 else if (rc)
943                         CERROR("Failed AST - should clean & disconnect "
944                                "client\n");
945                 LDLM_LOCK_PUT(w->w_lock);
946                 list_del(&w->w_list);
947                 OBD_FREE(w, sizeof(*w));
948         }
949         RETURN(retval);
950 }
951
952 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
953 {
954         ldlm_reprocess_all(res);
955         return LDLM_ITER_CONTINUE;
956 }
957
958 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
959 {
960         (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
961 }
962
963 void ldlm_reprocess_all(struct ldlm_resource *res)
964 {
965         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
966         int rc;
967         ENTRY;
968
969         /* Local lock trees don't get reprocessed. */
970         if (res->lr_namespace->ns_client) {
971                 EXIT;
972                 return;
973         }
974
975  restart:
976         l_lock(&res->lr_namespace->ns_lock);
977         res->lr_tmp = &rpc_list;
978
979         ldlm_reprocess_queue(res, &res->lr_converting);
980         if (list_empty(&res->lr_converting))
981                 ldlm_reprocess_queue(res, &res->lr_waiting);
982
983         res->lr_tmp = NULL;
984         l_unlock(&res->lr_namespace->ns_lock);
985
986         rc = ldlm_run_ast_work(&rpc_list);
987         if (rc == -ERESTART)
988                 goto restart;
989         EXIT;
990 }
991
992 void ldlm_cancel_callback(struct ldlm_lock *lock)
993 {
994         l_lock(&lock->l_resource->lr_namespace->ns_lock);
995         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
996                 lock->l_flags |= LDLM_FL_CANCEL;
997                 if (lock->l_blocking_ast)
998                         lock->l_blocking_ast(lock, NULL, lock->l_data,
999                                              LDLM_CB_CANCELING);
1000                 else
1001                         LDLM_DEBUG(lock, "no blocking ast");
1002         }
1003         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1004 }
1005
1006 void ldlm_lock_cancel(struct ldlm_lock *lock)
1007 {
1008         struct ldlm_resource *res;
1009         struct ldlm_namespace *ns;
1010         ENTRY;
1011
1012         ldlm_del_waiting_lock(lock);
1013
1014         res = lock->l_resource;
1015         ns = res->lr_namespace;
1016
1017         l_lock(&ns->ns_lock);
1018         /* Please do not, no matter how tempting, remove this LBUG without
1019          * talking to me first. -phik */
1020         if (lock->l_readers || lock->l_writers) {
1021                 LDLM_DEBUG(lock, "lock still has references");
1022                 ldlm_lock_dump(D_OTHER, lock);
1023                 LBUG();
1024         }
1025
1026         ldlm_cancel_callback(lock);
1027
1028         ldlm_resource_unlink_lock(lock);
1029         ldlm_lock_destroy(lock);
1030         l_unlock(&ns->ns_lock);
1031         EXIT;
1032 }
1033
1034 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data, void *cp_data)
1035 {
1036         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1037         ENTRY;
1038
1039         if (lock == NULL)
1040                 RETURN(-EINVAL);
1041
1042         lock->l_data = data;
1043         lock->l_cp_data = cp_data;
1044
1045         LDLM_LOCK_PUT(lock);
1046
1047         RETURN(0);
1048 }
1049
1050 /* This function is only called from one thread (per export); no locking around
1051  * the list ops needed */
1052 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1053 {
1054         struct list_head *iter, *n;
1055
1056         list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1057                 struct ldlm_lock *lock;
1058                 struct ldlm_resource *res;
1059                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1060                 res = ldlm_resource_getref(lock->l_resource);
1061                 LDLM_DEBUG(lock, "export %p", exp);
1062                 ldlm_lock_cancel(lock);
1063                 ldlm_reprocess_all(res);
1064                 ldlm_resource_putref(res);
1065         }
1066 }
1067
1068 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1069                                         int *flags)
1070 {
1071         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1072         struct ldlm_resource *res;
1073         struct ldlm_namespace *ns;
1074         int granted = 0;
1075         ENTRY;
1076
1077         LBUG();
1078
1079         res = lock->l_resource;
1080         ns = res->lr_namespace;
1081
1082         l_lock(&ns->ns_lock);
1083
1084         lock->l_req_mode = new_mode;
1085         ldlm_resource_unlink_lock(lock);
1086
1087         /* If this is a local resource, put it on the appropriate list. */
1088         if (res->lr_namespace->ns_client) {
1089                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1090                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1091                 } else {
1092                         /* This should never happen, because of the way the
1093                          * server handles conversions. */
1094                         LBUG();
1095
1096                         res->lr_tmp = &rpc_list;
1097                         ldlm_grant_lock(lock, NULL, 0);
1098                         res->lr_tmp = NULL;
1099                         granted = 1;
1100                         /* FIXME: completion handling not with ns_lock held ! */
1101                         if (lock->l_completion_ast)
1102                                 lock->l_completion_ast(lock, 0, NULL);
1103                 }
1104         } else {
1105                 /* FIXME: We should try the conversion right away and possibly
1106                  * return success without the need for an extra AST */
1107                 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1108                 *flags |= LDLM_FL_BLOCK_CONV;
1109         }
1110
1111         l_unlock(&ns->ns_lock);
1112
1113         if (granted)
1114                 ldlm_run_ast_work(&rpc_list);
1115         RETURN(res);
1116 }
1117
1118 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1119 {
1120         char ver[128];
1121
1122         if (!((portal_debug | D_ERROR) & level))
1123                 return;
1124
1125         if (RES_VERSION_SIZE != 4)
1126                 LBUG();
1127
1128         if (!lock) {
1129                 CDEBUG(level, "  NULL LDLM lock\n");
1130                 return;
1131         }
1132
1133         snprintf(ver, sizeof(ver), "%x %x %x %x",
1134                  lock->l_version[0], lock->l_version[1],
1135                  lock->l_version[2], lock->l_version[3]);
1136
1137         CDEBUG(level, "  -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1138                atomic_read(&lock->l_refc));
1139         if (lock->l_export && lock->l_export->exp_connection)
1140                 CDEBUG(level, "  Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1141                        lock->l_export->exp_connection->c_peer.peer_nid,
1142                        lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1143                        lock->l_remote_handle.cookie);
1144         else
1145                 CDEBUG(level, "  Node: local\n");
1146         CDEBUG(level, "  Parent: %p\n", lock->l_parent);
1147         CDEBUG(level, "  Resource: %p ("LPD64")\n", lock->l_resource,
1148                lock->l_resource->lr_name.name[0]);
1149         CDEBUG(level, "  Requested mode: %d, granted mode: %d\n",
1150                (int)lock->l_req_mode, (int)lock->l_granted_mode);
1151         CDEBUG(level, "  Readers: %u ; Writers; %u\n",
1152                lock->l_readers, lock->l_writers);
1153         if (lock->l_resource->lr_type == LDLM_EXTENT)
1154                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64"\n",
1155                        lock->l_extent.start, lock->l_extent.end);
1156 }
1157
1158 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1159 {
1160         struct ldlm_lock *lock;
1161
1162         lock = ldlm_handle2lock(lockh);
1163         if (lock == NULL)
1164                 return;
1165
1166         ldlm_lock_dump(D_OTHER, lock);
1167
1168         LDLM_LOCK_PUT(lock);
1169 }