Whamcloud - gitweb
c5f8873b32b321a28253b8ac6934450f6bf95eac
[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, void *data,
616                                       int flags)
617 {
618         struct ldlm_lock *lock;
619         struct list_head *tmp;
620
621         list_for_each(tmp, queue) {
622                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
623
624                 if (lock == old_lock)
625                         break;
626
627                 /* llite sometimes wants to match locks that will be
628                  * canceled when their users drop, but we allow it to match
629                  * if it passes in CBPENDING and the lock still has users.
630                  * this is generally only going to be used by children 
631                  * whose parents already hold a lock so forward progress
632                  * can still happen. */
633                 if (lock->l_flags & LDLM_FL_CBPENDING &&
634                     !(flags & LDLM_FL_CBPENDING))
635                         continue;
636                 if (lock->l_flags & LDLM_FL_CBPENDING &&
637                     lock->l_readers == 0 && lock->l_writers == 0)
638                         continue;
639
640                 if (lock->l_req_mode != mode)
641                         continue;
642
643                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
644                     (lock->l_extent.start > extent->start ||
645                      lock->l_extent.end < extent->end))
646                         continue;
647
648                 if (lock->l_destroyed)
649                         continue;
650
651                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
652                     !(lock->l_flags & LDLM_FL_LOCAL))
653                         continue;
654
655                 if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data)
656                         continue;
657
658                 ldlm_lock_addref_internal(lock, mode);
659                 return lock;
660         }
661
662         return NULL;
663 }
664
665 /* Can be called in two ways:
666  *
667  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
668  * for a duplicate of.
669  *
670  * Otherwise, all of the fields must be filled in, to match against.
671  *
672  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
673  *     server (ie, connh is NULL)
674  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
675  *     list will be considered
676  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
677  *     to be canceled can still be matched as long as they still have reader
678  *     or writer refernces
679  * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
680  *     data is the same.
681  *
682  * Returns 1 if it finds an already-existing lock that is compatible; in this
683  * case, lockh is filled in with a addref()ed lock
684  */
685 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
686                     struct ldlm_res_id *res_id, __u32 type, void *cookie,
687                     int cookielen, ldlm_mode_t mode, void *data,
688                     struct lustre_handle *lockh)
689 {
690         struct ldlm_resource *res;
691         struct ldlm_lock *lock, *old_lock = NULL;
692         int rc = 0;
693         ENTRY;
694
695         if (ns == NULL) {
696                 old_lock = ldlm_handle2lock(lockh);
697                 LASSERT(old_lock);
698
699                 ns = old_lock->l_resource->lr_namespace;
700                 res_id = &old_lock->l_resource->lr_name;
701                 type = old_lock->l_resource->lr_type;
702                 mode = old_lock->l_req_mode;
703         }
704
705         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
706         if (res == NULL) {
707                 LASSERT(old_lock == NULL);
708                 RETURN(0);
709         }
710
711         l_lock(&ns->ns_lock);
712
713         lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
714                             flags);
715         if (lock != NULL)
716                 GOTO(out, rc = 1);
717         if (flags & LDLM_FL_BLOCK_GRANTED)
718                 GOTO(out, rc = 0);
719         lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
720                             flags);
721         if (lock != NULL)
722                 GOTO(out, rc = 1);
723         lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
724                             flags);
725         if (lock != NULL)
726                 GOTO(out, rc = 1);
727
728         EXIT;
729  out:
730         ldlm_resource_putref(res);
731         l_unlock(&ns->ns_lock);
732
733         if (lock) {
734                 ldlm_lock2handle(lock, lockh);
735                 if (lock->l_completion_ast)
736                         lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
737                                                NULL);
738         }
739         if (rc)
740                 LDLM_DEBUG(lock, "matched");
741         else
742                 LDLM_DEBUG_NOLOCK("not matched");
743
744         if (old_lock)
745                 LDLM_LOCK_PUT(old_lock);
746
747         return rc;
748 }
749
750 /* Returns a referenced lock */
751 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
752                                    struct lustre_handle *parent_lock_handle,
753                                    struct ldlm_res_id res_id, __u32 type,
754                                    ldlm_mode_t mode,
755                                    ldlm_blocking_callback blocking,
756                                    void *data)
757 {
758         struct ldlm_resource *res, *parent_res = NULL;
759         struct ldlm_lock *lock, *parent_lock = NULL;
760         ENTRY;
761
762         if (parent_lock_handle) {
763                 parent_lock = ldlm_handle2lock(parent_lock_handle);
764                 if (parent_lock)
765                         parent_res = parent_lock->l_resource;
766         }
767
768         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
769         if (res == NULL)
770                 RETURN(NULL);
771
772         lock = ldlm_lock_new(parent_lock, res);
773         ldlm_resource_putref(res);
774         if (parent_lock != NULL)
775                 LDLM_LOCK_PUT(parent_lock);
776
777         if (lock == NULL)
778                 RETURN(NULL);
779
780         lock->l_req_mode = mode;
781         lock->l_data = data;
782         lock->l_blocking_ast = blocking;
783
784         RETURN(lock);
785 }
786
787 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
788                                struct ldlm_lock **lockp,
789                                void *cookie, int cookie_len,
790                                int *flags,
791                                ldlm_completion_callback completion)
792 {
793         struct ldlm_resource *res;
794         struct ldlm_lock *lock = *lockp;
795         int local;
796         ldlm_res_policy policy;
797         ENTRY;
798
799         res = lock->l_resource;
800
801         if (res->lr_type == LDLM_EXTENT)
802                 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
803
804         /* policies are not executed on the client or during replay */
805         local = res->lr_namespace->ns_client;
806         if (!local && !(*flags & LDLM_FL_REPLAY) &&
807             (policy = ldlm_res_policy_table[res->lr_type])) {
808                 int rc;
809                 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
810                 if (rc == ELDLM_LOCK_CHANGED) {
811                         res = lock->l_resource;
812                         *flags |= LDLM_FL_LOCK_CHANGED;
813                 } else if (rc == ELDLM_LOCK_REPLACED) {
814                         /* The lock that was returned has already been granted,
815                          * and placed into lockp.  Destroy the old one and our
816                          * work here is done. */
817                         ldlm_lock_destroy(lock);
818                         LDLM_LOCK_PUT(lock);
819                         *flags |= LDLM_FL_LOCK_CHANGED;
820                         RETURN(0);
821                 } else if (rc == ELDLM_LOCK_ABORTED) {
822                         ldlm_lock_destroy(lock);
823                         RETURN(rc);
824                 }
825         }
826
827         l_lock(&ns->ns_lock);
828         if (local && lock->l_req_mode == lock->l_granted_mode) {
829                 /* The server returned a blocked lock, but it was granted before
830                  * we got a chance to actually enqueue it.  We don't need to do
831                  * anything else. */
832                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
833                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
834                 GOTO(out, ELDLM_OK);
835         }
836
837         /* This distinction between local lock trees is very important; a client
838          * namespace only has information about locks taken by that client, and
839          * thus doesn't have enough information to decide for itself if it can
840          * be granted (below).  In this case, we do exactly what the server
841          * tells us to do, as dictated by the 'flags'.
842          *
843          * We do exactly the same thing during recovery, when the server is
844          * more or less trusting the clients not to lie.
845          *
846          * FIXME (bug 268): Detect obvious lies by checking compatibility in
847          * granted/converting queues. */
848         ldlm_resource_unlink_lock(lock);
849         if (local) {
850                 if (*flags & LDLM_FL_BLOCK_CONV)
851                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
852                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
853                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
854                 else
855                         ldlm_grant_lock(lock, NULL, 0);
856                 GOTO(out, ELDLM_OK);
857         } else if (*flags & LDLM_FL_REPLAY) {
858                 if (*flags & LDLM_FL_BLOCK_CONV) {
859                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
860                         GOTO(out, ELDLM_OK);
861                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
862                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
863                         GOTO(out, ELDLM_OK);
864                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
865                         ldlm_grant_lock(lock, NULL, 0);
866                         GOTO(out, ELDLM_OK);
867                 }
868                 /* If no flags, fall through to normal enqueue path. */
869         }
870
871         /* FIXME: We may want to optimize by checking lr_most_restr */
872         if (!list_empty(&res->lr_converting)) {
873                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
874                 *flags |= LDLM_FL_BLOCK_CONV;
875                 GOTO(out, ELDLM_OK);
876         }
877         if (!list_empty(&res->lr_waiting)) {
878                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
879                 *flags |= LDLM_FL_BLOCK_WAIT;
880                 GOTO(out, ELDLM_OK);
881         }
882         if (!ldlm_lock_compat(lock, 0)) {
883                 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
884                 *flags |= LDLM_FL_BLOCK_GRANTED;
885                 GOTO(out, ELDLM_OK);
886         }
887         ldlm_grant_lock(lock, NULL, 0);
888         EXIT;
889       out:
890         /* Don't set 'completion_ast' until here so that if the lock is granted
891          * immediately we don't do an unnecessary completion call. */
892         lock->l_completion_ast = completion;
893         l_unlock(&ns->ns_lock);
894         return ELDLM_OK;
895 }
896
897 /* Must be called with namespace taken: queue is waiting or converting. */
898 static int ldlm_reprocess_queue(struct ldlm_resource *res,
899                                 struct list_head *queue)
900 {
901         struct list_head *tmp, *pos;
902         ENTRY;
903
904         list_for_each_safe(tmp, pos, queue) {
905                 struct ldlm_lock *pending;
906                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
907
908                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
909
910                 if (!ldlm_lock_compat(pending, 1))
911                         RETURN(1);
912
913                 list_del_init(&pending->l_res_link);
914                 ldlm_grant_lock(pending, NULL, 0);
915         }
916
917         RETURN(0);
918 }
919
920 int ldlm_run_ast_work(struct list_head *rpc_list)
921 {
922         struct list_head *tmp, *pos;
923         int rc, retval = 0;
924         ENTRY;
925
926         list_for_each_safe(tmp, pos, rpc_list) {
927                 struct ldlm_ast_work *w =
928                         list_entry(tmp, struct ldlm_ast_work, w_list);
929
930                 /* It's possible to receive a completion AST before we've set
931                  * the l_completion_ast pointer: either because the AST arrived
932                  * before the reply, or simply because there's a small race
933                  * window between receiving the reply and finishing the local
934                  * enqueue. (bug 842)
935                  *
936                  * This can't happen with the blocking_ast, however, because we
937                  * will never call the local blocking_ast until we drop our
938                  * reader/writer reference, which we won't do until we get the
939                  * reply and finish enqueueing. */
940                 if (w->w_blocking) {
941                         LASSERT(w->w_lock->l_blocking_ast != NULL);
942                         rc = w->w_lock->l_blocking_ast
943                                 (w->w_lock, &w->w_desc, w->w_data,
944                                  LDLM_CB_BLOCKING);
945                 } else if (w->w_lock->l_completion_ast != NULL) {
946                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
947                                                          w->w_data);
948                 } else {
949                         rc = 0;
950                 }
951                 if (rc == -ERESTART)
952                         retval = rc;
953                 else if (rc)
954                         CERROR("Failed AST - should clean & disconnect "
955                                "client\n");
956                 LDLM_LOCK_PUT(w->w_lock);
957                 list_del(&w->w_list);
958                 OBD_FREE(w, sizeof(*w));
959         }
960         RETURN(retval);
961 }
962
963 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
964 {
965         ldlm_reprocess_all(res);
966         return LDLM_ITER_CONTINUE;
967 }
968
969 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
970 {
971         (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
972 }
973
974 void ldlm_reprocess_all(struct ldlm_resource *res)
975 {
976         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
977         int rc;
978         ENTRY;
979
980         /* Local lock trees don't get reprocessed. */
981         if (res->lr_namespace->ns_client) {
982                 EXIT;
983                 return;
984         }
985
986  restart:
987         l_lock(&res->lr_namespace->ns_lock);
988         res->lr_tmp = &rpc_list;
989
990         ldlm_reprocess_queue(res, &res->lr_converting);
991         if (list_empty(&res->lr_converting))
992                 ldlm_reprocess_queue(res, &res->lr_waiting);
993
994         res->lr_tmp = NULL;
995         l_unlock(&res->lr_namespace->ns_lock);
996
997         rc = ldlm_run_ast_work(&rpc_list);
998         if (rc == -ERESTART)
999                 goto restart;
1000         EXIT;
1001 }
1002
1003 void ldlm_cancel_callback(struct ldlm_lock *lock)
1004 {
1005         l_lock(&lock->l_resource->lr_namespace->ns_lock);
1006         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1007                 lock->l_flags |= LDLM_FL_CANCEL;
1008                 if (lock->l_blocking_ast) {
1009                         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1010                         lock->l_blocking_ast(lock, NULL, lock->l_data,
1011                                              LDLM_CB_CANCELING);
1012                         return;
1013                 } else {
1014                         LDLM_DEBUG(lock, "no blocking ast");
1015                 }
1016         }
1017         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1018 }
1019
1020 void ldlm_lock_cancel(struct ldlm_lock *lock)
1021 {
1022         struct ldlm_resource *res;
1023         struct ldlm_namespace *ns;
1024         ENTRY;
1025
1026         ldlm_del_waiting_lock(lock);
1027
1028         res = lock->l_resource;
1029         ns = res->lr_namespace;
1030
1031         l_lock(&ns->ns_lock);
1032         /* Please do not, no matter how tempting, remove this LBUG without
1033          * talking to me first. -phik */
1034         if (lock->l_readers || lock->l_writers) {
1035                 LDLM_DEBUG(lock, "lock still has references");
1036                 ldlm_lock_dump(D_OTHER, lock);
1037                 LBUG();
1038         }
1039
1040         ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
1041
1042         ldlm_resource_unlink_lock(lock);
1043         ldlm_lock_destroy(lock);
1044         l_unlock(&ns->ns_lock);
1045         EXIT;
1046 }
1047
1048 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1049 {
1050         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1051         ENTRY;
1052
1053         if (lock == NULL)
1054                 RETURN(-EINVAL);
1055
1056         lock->l_data = data;
1057
1058         LDLM_LOCK_PUT(lock);
1059
1060         RETURN(0);
1061 }
1062
1063 /* This function is only called from one thread (per export); no locking around
1064  * the list ops needed */
1065 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1066 {
1067         struct list_head *iter, *n;
1068
1069         list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1070                 struct ldlm_lock *lock;
1071                 struct ldlm_resource *res;
1072                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1073                 res = ldlm_resource_getref(lock->l_resource);
1074                 LDLM_DEBUG(lock, "export %p", exp);
1075                 ldlm_lock_cancel(lock);
1076                 ldlm_reprocess_all(res);
1077                 ldlm_resource_putref(res);
1078         }
1079 }
1080
1081 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1082                                         int *flags)
1083 {
1084         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1085         struct ldlm_resource *res;
1086         struct ldlm_namespace *ns;
1087         int granted = 0;
1088         ENTRY;
1089
1090         LBUG();
1091
1092         res = lock->l_resource;
1093         ns = res->lr_namespace;
1094
1095         l_lock(&ns->ns_lock);
1096
1097         lock->l_req_mode = new_mode;
1098         ldlm_resource_unlink_lock(lock);
1099
1100         /* If this is a local resource, put it on the appropriate list. */
1101         if (res->lr_namespace->ns_client) {
1102                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1103                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1104                 } else {
1105                         /* This should never happen, because of the way the
1106                          * server handles conversions. */
1107                         LBUG();
1108
1109                         res->lr_tmp = &rpc_list;
1110                         ldlm_grant_lock(lock, NULL, 0);
1111                         res->lr_tmp = NULL;
1112                         granted = 1;
1113                         /* FIXME: completion handling not with ns_lock held ! */
1114                         if (lock->l_completion_ast)
1115                                 lock->l_completion_ast(lock, 0, NULL);
1116                 }
1117         } else {
1118                 /* FIXME: We should try the conversion right away and possibly
1119                  * return success without the need for an extra AST */
1120                 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1121                 *flags |= LDLM_FL_BLOCK_CONV;
1122         }
1123
1124         l_unlock(&ns->ns_lock);
1125
1126         if (granted)
1127                 ldlm_run_ast_work(&rpc_list);
1128         RETURN(res);
1129 }
1130
1131 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1132 {
1133         char ver[128];
1134         struct obd_device *obd;
1135
1136         if (!((portal_debug | D_ERROR) & level))
1137                 return;
1138
1139         if (RES_VERSION_SIZE != 4)
1140                 LBUG();
1141
1142         if (!lock) {
1143                 CDEBUG(level, "  NULL LDLM lock\n");
1144                 return;
1145         }
1146
1147         snprintf(ver, sizeof(ver), "%x %x %x %x",
1148                  lock->l_version[0], lock->l_version[1],
1149                  lock->l_version[2], lock->l_version[3]);
1150
1151         CDEBUG(level, "  -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1152                atomic_read(&lock->l_refc));
1153         obd = class_conn2obd(lock->l_connh);
1154         if (lock->l_export && lock->l_export->exp_connection) {
1155                 CDEBUG(level, "  Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1156                        lock->l_export->exp_connection->c_peer.peer_nid,
1157                        lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1158                        lock->l_remote_handle.cookie);
1159         } else if (obd == NULL) {
1160                 CDEBUG(level, "  Node: local\n");
1161         } else {
1162                 struct obd_import *imp = obd->u.cli.cl_import;
1163                 CDEBUG(level, "  Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1164                        imp->imp_connection->c_peer.peer_nid,
1165                        imp->imp_connection->c_peer.peer_ni->pni_name,
1166                        lock->l_remote_handle.cookie);
1167         }
1168         CDEBUG(level, "  Parent: %p\n", lock->l_parent);
1169         CDEBUG(level, "  Resource: %p ("LPD64")\n", lock->l_resource,
1170                lock->l_resource->lr_name.name[0]);
1171         CDEBUG(level, "  Requested mode: %d, granted mode: %d\n",
1172                (int)lock->l_req_mode, (int)lock->l_granted_mode);
1173         CDEBUG(level, "  Readers: %u ; Writers; %u\n",
1174                lock->l_readers, lock->l_writers);
1175         if (lock->l_resource->lr_type == LDLM_EXTENT)
1176                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64"\n",
1177                        lock->l_extent.start, lock->l_extent.end);
1178 }
1179
1180 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1181 {
1182         struct ldlm_lock *lock;
1183
1184         lock = ldlm_handle2lock(lockh);
1185         if (lock == NULL)
1186                 return;
1187
1188         ldlm_lock_dump(D_OTHER, lock);
1189
1190         LDLM_LOCK_PUT(lock);
1191 }