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