Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LDLM
28
29 #ifdef __KERNEL__
30 # include <linux/slab.h>
31 # include <linux/module.h>
32 # include <linux/lustre_dlm.h>
33 #else
34 # include <liblustre.h>
35 # include <libcfs/kp30.h>
36 #endif
37
38 #include <linux/obd_class.h>
39 #include "ldlm_internal.h"
40
41 //struct lustre_lock ldlm_everything_lock;
42
43 /* lock types */
44 char *ldlm_lockname[] = {
45         [0] "--",
46         [LCK_EX] "EX",
47         [LCK_PW] "PW",
48         [LCK_PR] "PR",
49         [LCK_CW] "CW",
50         [LCK_CR] "CR",
51         [LCK_NL] "NL",
52         [LCK_GROUP] "GROUP"
53 };
54 char *ldlm_typename[] = {
55         [LDLM_PLAIN] "PLN",
56         [LDLM_EXTENT] "EXT",
57         [LDLM_FLOCK] "FLK",
58 };
59
60 char *ldlm_it2str(int it)
61 {
62         switch (it) {
63         case IT_OPEN:
64                 return "open";
65         case IT_CREAT:
66                 return "creat";
67         case (IT_OPEN | IT_CREAT):
68                 return "open|creat";
69         case IT_READDIR:
70                 return "readdir";
71         case IT_GETATTR:
72                 return "getattr";
73         case IT_LOOKUP:
74                 return "lookup";
75         case IT_UNLINK:
76                 return "unlink";
77         case IT_GETXATTR:
78                 return "getxattr";
79         default:
80                 CERROR("Unknown intent %d\n", it);
81                 return "UNKNOWN";
82         }
83 }
84
85 extern kmem_cache_t *ldlm_lock_slab;
86 struct lustre_lock ldlm_handle_lock;
87
88 static ldlm_processing_policy ldlm_processing_policy_table[] = {
89         [LDLM_PLAIN] ldlm_process_plain_lock,
90         [LDLM_EXTENT] ldlm_process_extent_lock,
91 #ifdef __KERNEL__
92         [LDLM_FLOCK] ldlm_process_flock_lock,
93         //[LDLM_LLOG]  ldlm_process_llog_lock,
94 #endif
95 };
96
97 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
98 {
99         return ldlm_processing_policy_table[res->lr_type];
100 }
101
102 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
103 {
104         ns->ns_policy = arg;
105 }
106
107 /*
108  * REFCOUNTED LOCK OBJECTS
109  */
110
111
112 /*
113  * Lock refcounts, during creation:
114  *   - one special one for allocation, dec'd only once in destroy
115  *   - one for being a lock that's in-use
116  *   - one for the addref associated with a new lock
117  */
118 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
119 {
120         atomic_inc(&lock->l_refc);
121         return lock;
122 }
123
124 void ldlm_lock_put(struct ldlm_lock *lock)
125 {
126         ENTRY;
127
128         if (atomic_dec_and_test(&lock->l_refc)) {
129                 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
130                 struct obd_export *export = NULL;
131
132                 l_lock(&ns->ns_lock);
133                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
134                 LASSERT(lock->l_destroyed);
135                 LASSERT(list_empty(&lock->l_res_link));
136
137                 spin_lock(&ns->ns_counter_lock);
138                 ns->ns_locks--;
139                 spin_unlock(&ns->ns_counter_lock);
140
141                 ldlm_resource_putref(lock->l_resource);
142                 lock->l_resource = NULL;
143                 export = lock->l_export;
144
145                 if (lock->l_parent)
146                         LDLM_LOCK_PUT(lock->l_parent);
147
148                 if (lock->l_lvb_data != NULL)
149                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
150
151                 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
152                 l_unlock(&ns->ns_lock);
153                 if (export)
154                         class_export_put(export);
155         }
156
157         EXIT;
158 }
159
160 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
161 {
162         ENTRY;
163         l_lock(&lock->l_resource->lr_namespace->ns_lock);
164         if (!list_empty(&lock->l_lru)) {
165                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
166                 list_del_init(&lock->l_lru);
167                 lock->l_resource->lr_namespace->ns_nr_unused--;
168                 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
169         }
170         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
171         EXIT;
172 }
173
174 /* This used to have a 'strict' flag, which recovery would use to mark an
175  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
176  * shall explain why it's gone: with the new hash table scheme, once you call
177  * ldlm_lock_destroy, you can never drop your final references on this lock.
178  * Because it's not in the hash table anymore.  -phil */
179 void ldlm_lock_destroy(struct ldlm_lock *lock)
180 {
181         ENTRY;
182         l_lock(&lock->l_resource->lr_namespace->ns_lock);
183
184         if (!list_empty(&lock->l_children)) {
185                 LDLM_ERROR(lock, "still has children (%p)!",
186                            lock->l_children.next);
187                 ldlm_lock_dump(D_ERROR, lock, 0);
188                 LBUG();
189         }
190         if (lock->l_readers || lock->l_writers) {
191                 LDLM_ERROR(lock, "lock still has references");
192                 ldlm_lock_dump(D_ERROR, lock, 0);
193                 LBUG();
194         }
195
196         if (!list_empty(&lock->l_res_link)) {
197                 LDLM_ERROR(lock, "lock still on resource");
198                 ldlm_lock_dump(D_ERROR, lock, 0);
199                 LBUG();
200         }
201
202         if (lock->l_destroyed) {
203                 LASSERT(list_empty(&lock->l_lru));
204                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
205                 EXIT;
206                 return;
207         }
208         lock->l_destroyed = 1;
209
210         list_del_init(&lock->l_export_chain);
211         ldlm_lock_remove_from_lru(lock);
212         class_handle_unhash(&lock->l_handle);
213
214 #if 0
215         /* Wake anyone waiting for this lock */
216         /* FIXME: I should probably add yet another flag, instead of using
217          * l_export to only call this on clients */
218         if (lock->l_export)
219                 class_export_put(lock->l_export);
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 2 - one for current caller and one for remote
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         OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_NOFS, 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_childof);
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_namespace *ns;
332         struct ldlm_lock *lock = NULL, *retval = NULL;
333         ENTRY;
334
335         LASSERT(handle);
336
337         lock = class_handle2object(handle->cookie);
338         if (lock == NULL)
339                 RETURN(NULL);
340
341         LASSERT(lock->l_resource != NULL);
342         ns = lock->l_resource->lr_namespace;
343         LASSERT(ns != NULL);
344
345         l_lock(&ns->ns_lock);
346
347         /* It's unlikely but possible that someone marked the lock as
348          * destroyed after we did handle2object on it */
349         if (lock->l_destroyed) {
350                 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
351                 LDLM_LOCK_PUT(lock);
352                 GOTO(out, retval);
353         }
354
355         if (flags && (lock->l_flags & flags)) {
356                 LDLM_LOCK_PUT(lock);
357                 GOTO(out, retval);
358         }
359
360         if (flags)
361                 lock->l_flags |= flags;
362
363         retval = lock;
364         EXIT;
365  out:
366         l_unlock(&ns->ns_lock);
367         return retval;
368 }
369
370 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
371                                       struct lustre_handle *handle)
372 {
373         struct ldlm_lock *retval = NULL;
374
375         l_lock(&ns->ns_lock);
376         retval = __ldlm_handle2lock(handle, 0);
377         l_unlock(&ns->ns_lock);
378
379         return retval;
380 }
381
382 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
383 {
384         ldlm_res2desc(lock->l_resource, &desc->l_resource);
385         desc->l_req_mode = lock->l_req_mode;
386         desc->l_granted_mode = lock->l_granted_mode;
387         memcpy(&desc->l_policy_data, &lock->l_policy_data,
388                sizeof(desc->l_policy_data));
389 }
390
391 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
392                             void *data, int datalen)
393 {
394         struct ldlm_ast_work *w;
395         ENTRY;
396
397         l_lock(&lock->l_resource->lr_namespace->ns_lock);
398         if (new && (lock->l_flags & LDLM_FL_AST_SENT))
399                 GOTO(out, 0);
400
401         CDEBUG(D_OTHER, "lock %p incompatible; sending blocking AST.\n", lock);
402
403         OBD_ALLOC(w, sizeof(*w));
404         if (!w) {
405                 LBUG();
406                 GOTO(out, 0);
407         }
408
409         w->w_data = data;
410         w->w_datalen = datalen;
411         if (new) {
412                 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
413                 lock->l_flags |= LDLM_FL_AST_SENT;
414                 /* If the enqueuing client said so, tell the AST recipient to
415                  * discard dirty data, rather than writing back. */
416                 if (new->l_flags & LDLM_AST_DISCARD_DATA)
417                         lock->l_flags |= LDLM_FL_DISCARD_DATA;
418                 w->w_blocking = 1;
419                 ldlm_lock2desc(new, &w->w_desc);
420         }
421
422         w->w_lock = LDLM_LOCK_GET(lock);
423         list_add(&w->w_list, lock->l_resource->lr_tmp);
424         EXIT;
425  out:
426         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
427 }
428
429 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
430 {
431         struct ldlm_lock *lock;
432
433         lock = ldlm_handle2lock(lockh);
434         ldlm_lock_addref_internal(lock, mode);
435         LDLM_LOCK_PUT(lock);
436 }
437
438 /* only called for local locks */
439 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
440 {
441         l_lock(&lock->l_resource->lr_namespace->ns_lock);
442         ldlm_lock_remove_from_lru(lock);
443         if (mode & (LCK_NL | LCK_CR | LCK_PR))
444                 lock->l_readers++;
445         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
446                 lock->l_writers++;
447         lock->l_last_used = jiffies;
448         LDLM_LOCK_GET(lock);
449         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
450         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
451 }
452
453 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
454 {
455         struct ldlm_namespace *ns;
456         ENTRY;
457
458         ns = lock->l_resource->lr_namespace;
459         l_lock(&ns->ns_lock);
460         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
461         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
462                 LASSERT(lock->l_readers > 0);
463                 lock->l_readers--;
464         }
465         if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
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 == LDLM_NAMESPACE_SERVER && 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                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
489                 ldlm_lock_remove_from_lru(lock);
490                 l_unlock(&ns->ns_lock);
491                 if (ldlm_bl_to_thread(ns, NULL, lock) != 0)
492                         ldlm_handle_bl_callback(ns, NULL, lock);
493         } else if (ns->ns_client == LDLM_NAMESPACE_CLIENT &&
494                    !lock->l_readers && !lock->l_writers &&
495                    !(lock->l_flags & LDLM_FL_NO_LRU)) {
496                 /* If this is a client-side namespace and this was the last
497                  * reference, put it on the LRU. */
498                 LASSERT(list_empty(&lock->l_lru));
499                 LASSERT(ns->ns_nr_unused >= 0);
500                 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
501                 ns->ns_nr_unused++;
502                 l_unlock(&ns->ns_lock);
503                 ldlm_cancel_lru(ns, LDLM_ASYNC);
504         } else {
505                 l_unlock(&ns->ns_lock);
506         }
507
508         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
509
510         EXIT;
511 }
512
513 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
514 {
515         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
516         LASSERT(lock != NULL);
517         ldlm_lock_decref_internal(lock, mode);
518         LDLM_LOCK_PUT(lock);
519 }
520
521 /* This will drop a lock reference and mark it for destruction, but will not
522  * necessarily cancel the lock before returning. */
523 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
524 {
525         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
526         ENTRY;
527
528         LASSERT(lock != NULL);
529
530         l_lock(&lock->l_resource->lr_namespace->ns_lock);
531         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
532         lock->l_flags |= LDLM_FL_CBPENDING;
533         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
534         ldlm_lock_decref_internal(lock, mode);
535         LDLM_LOCK_PUT(lock);
536 }
537
538 /* NOTE: called by
539  *  - ldlm_lock_enqueue
540  *  - ldlm_reprocess_queue
541  *  - ldlm_lock_convert
542  */
543 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen,
544                      int run_ast)
545 {
546         struct ldlm_resource *res = lock->l_resource;
547         ENTRY;
548
549         l_lock(&lock->l_resource->lr_namespace->ns_lock);
550         lock->l_granted_mode = lock->l_req_mode;
551         ldlm_resource_add_lock(res, &res->lr_granted, lock);
552
553         if (lock->l_granted_mode < res->lr_most_restr)
554                 res->lr_most_restr = lock->l_granted_mode;
555
556         if (run_ast && lock->l_completion_ast != NULL)
557                 ldlm_add_ast_work_item(lock, NULL, data, datalen);
558
559         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
560         EXIT;
561 }
562
563 /* returns a referenced lock or NULL.  See the flag descriptions below, in the
564  * comment above ldlm_lock_match */
565 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
566                                       ldlm_policy_data_t *policy,
567                                       struct ldlm_lock *old_lock, int flags)
568 {
569         struct ldlm_lock *lock;
570         struct list_head *tmp;
571
572         list_for_each(tmp, queue) {
573                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
574
575                 if (lock == old_lock)
576                         break;
577
578                 /* llite sometimes wants to match locks that will be
579                  * canceled when their users drop, but we allow it to match
580                  * if it passes in CBPENDING and the lock still has users.
581                  * this is generally only going to be used by children
582                  * whose parents already hold a lock so forward progress
583                  * can still happen. */
584                 if (lock->l_flags & LDLM_FL_CBPENDING &&
585                     !(flags & LDLM_FL_CBPENDING))
586                         continue;
587                 if (lock->l_flags & LDLM_FL_CBPENDING &&
588                     lock->l_readers == 0 && lock->l_writers == 0)
589                         continue;
590
591                 if (!(lock->l_req_mode & mode))
592                         continue;
593
594                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
595                     (lock->l_policy_data.l_extent.start >
596                      policy->l_extent.start ||
597                      lock->l_policy_data.l_extent.end < policy->l_extent.end))
598                         continue;
599
600                 if (unlikely(mode == LCK_GROUP) &&
601                     lock->l_resource->lr_type == LDLM_EXTENT &&
602                     lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
603                         continue;
604
605                 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
606                         continue;
607
608                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
609                     !(lock->l_flags & LDLM_FL_LOCAL))
610                         continue;
611
612                 if (flags & LDLM_FL_TEST_LOCK)
613                         LDLM_LOCK_GET(lock);
614                 else
615                         ldlm_lock_addref_internal(lock, mode);
616                 return lock;
617         }
618
619         return NULL;
620 }
621
622 void ldlm_lock_allow_match(struct ldlm_lock *lock)
623 {
624         l_lock(&lock->l_resource->lr_namespace->ns_lock);
625         lock->l_flags |= LDLM_FL_CAN_MATCH;
626         wake_up(&lock->l_waitq);
627         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
628 }
629
630 /* Can be called in two ways:
631  *
632  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
633  * for a duplicate of.
634  *
635  * Otherwise, all of the fields must be filled in, to match against.
636  *
637  * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
638  *     server (ie, connh is NULL)
639  * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
640  *     list will be considered
641  * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
642  *     to be canceled can still be matched as long as they still have reader
643  *     or writer refernces
644  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
645  *     just tell us if we would have matched.
646  *
647  * Returns 1 if it finds an already-existing lock that is compatible; in this
648  * case, lockh is filled in with a addref()ed lock
649  */
650 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
651                     struct ldlm_res_id *res_id, ldlm_type_t type,
652                     ldlm_policy_data_t *policy, ldlm_mode_t mode,
653                     struct lustre_handle *lockh)
654 {
655         struct ldlm_resource *res;
656         struct ldlm_lock *lock, *old_lock = NULL;
657         int rc = 0;
658         ENTRY;
659
660         if (ns == NULL) {
661                 old_lock = ldlm_handle2lock(lockh);
662                 LASSERT(old_lock);
663
664                 ns = old_lock->l_resource->lr_namespace;
665                 res_id = &old_lock->l_resource->lr_name;
666                 type = old_lock->l_resource->lr_type;
667                 mode = old_lock->l_req_mode;
668         }
669
670         res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
671         if (res == NULL) {
672                 LASSERT(old_lock == NULL);
673                 RETURN(0);
674         }
675
676         l_lock(&ns->ns_lock);
677
678         lock = search_queue(&res->lr_granted, mode, policy, old_lock, flags);
679         if (lock != NULL)
680                 GOTO(out, rc = 1);
681         if (flags & LDLM_FL_BLOCK_GRANTED)
682                 GOTO(out, rc = 0);
683         lock = search_queue(&res->lr_converting, mode, policy, old_lock, flags);
684         if (lock != NULL)
685                 GOTO(out, rc = 1);
686         lock = search_queue(&res->lr_waiting, mode, policy, old_lock, flags);
687         if (lock != NULL)
688                 GOTO(out, rc = 1);
689
690         EXIT;
691  out:
692         ldlm_resource_putref(res);
693         l_unlock(&ns->ns_lock);
694
695         if (lock) {
696                 ldlm_lock2handle(lock, lockh);
697                 if (!(lock->l_flags & LDLM_FL_CAN_MATCH)) {
698                         struct l_wait_info lwi;
699                         if (lock->l_completion_ast) {
700                                 int err = lock->l_completion_ast(lock,
701                                                           LDLM_FL_WAIT_NOREPROC,
702                                                                  NULL);
703                                 if (err) {
704                                         rc = 0;
705                                         goto out2;
706                                 }
707                         }
708
709                         lwi = LWI_TIMEOUT_INTR(obd_timeout*HZ, NULL,NULL,NULL);
710
711                         /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
712                         l_wait_event(lock->l_waitq,
713                                      (lock->l_flags & LDLM_FL_CAN_MATCH), &lwi);
714                 }
715         }
716  out2:
717         if (rc) {
718                 l_lock(&ns->ns_lock);
719                 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
720                            type == LDLM_PLAIN ? res_id->name[2] :
721                                 policy->l_extent.start,
722                            type == LDLM_PLAIN ? res_id->name[3] :
723                                 policy->l_extent.end);
724                 l_unlock(&ns->ns_lock);
725         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
726                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
727                                   LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
728                                   type, mode, res_id->name[0], res_id->name[1],
729                                   type == LDLM_PLAIN ? res_id->name[2] :
730                                         policy->l_extent.start,
731                                   type == LDLM_PLAIN ? res_id->name[3] :
732                                         policy->l_extent.end);
733         }
734         if (old_lock)
735                 LDLM_LOCK_PUT(old_lock);
736         if (flags & LDLM_FL_TEST_LOCK && rc)
737                 LDLM_LOCK_PUT(lock);
738
739         return rc;
740 }
741
742 /* Returns a referenced lock */
743 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
744                                    struct lustre_handle *parent_lock_handle,
745                                    struct ldlm_res_id res_id, ldlm_type_t type,
746                                    ldlm_mode_t mode,
747                                    ldlm_blocking_callback blocking,
748                                    ldlm_completion_callback completion,
749                                    ldlm_glimpse_callback glimpse,
750                                    void *data, __u32 lvb_len)
751 {
752         struct ldlm_resource *res, *parent_res = NULL;
753         struct ldlm_lock *lock, *parent_lock = NULL;
754         ENTRY;
755
756         if (parent_lock_handle) {
757                 parent_lock = ldlm_handle2lock(parent_lock_handle);
758                 if (parent_lock)
759                         parent_res = parent_lock->l_resource;
760         }
761
762         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
763         if (res == NULL)
764                 RETURN(NULL);
765
766         lock = ldlm_lock_new(parent_lock, res);
767         ldlm_resource_putref(res);
768         if (parent_lock != NULL)
769                 LDLM_LOCK_PUT(parent_lock);
770
771         if (lock == NULL)
772                 RETURN(NULL);
773
774         lock->l_req_mode = mode;
775         lock->l_ast_data = data;
776         lock->l_blocking_ast = blocking;
777         lock->l_completion_ast = completion;
778         lock->l_glimpse_ast = glimpse;
779         lock->l_pid = current->pid;
780
781         if (lvb_len) {
782                 lock->l_lvb_len = lvb_len;
783                 OBD_ALLOC(lock->l_lvb_data, lvb_len);
784                 if (lock->l_lvb_data == NULL) {
785                         OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
786                         RETURN(NULL);
787                 }
788         }
789
790         RETURN(lock);
791 }
792
793 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
794                                struct ldlm_lock **lockp,
795                                void *cookie, int *flags)
796 {
797         struct ldlm_lock *lock = *lockp;
798         struct ldlm_resource *res = lock->l_resource;
799         int local = res->lr_namespace->ns_client;
800         ldlm_processing_policy policy;
801         ldlm_error_t rc = ELDLM_OK;
802         ENTRY;
803
804         /* policies are not executed on the client or during replay */
805         if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
806             && !local && ns->ns_policy) {
807                 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
808                                    NULL);
809                 if (rc == ELDLM_LOCK_REPLACED) {
810                         /* The lock that was returned has already been granted,
811                          * and placed into lockp.  If it's not the same as the
812                          * one we passed in, then destroy the old one and our
813                          * work here is done. */
814                         if (lock != *lockp) {
815                                 ldlm_lock_destroy(lock);
816                                 LDLM_LOCK_PUT(lock);
817                         }
818                         *flags |= LDLM_FL_LOCK_CHANGED;
819                         RETURN(0);
820                 } else if (rc != ELDLM_OK ||
821                            (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
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         /* Some flags from the enqueue want to make it into the AST, via the
838          * lock's l_flags. */
839         lock->l_flags |= (*flags & (LDLM_AST_DISCARD_DATA|LDLM_INHERIT_FLAGS));
840
841         /* This distinction between local lock trees is very important; a client
842          * namespace only has information about locks taken by that client, and
843          * thus doesn't have enough information to decide for itself if it can
844          * be granted (below).  In this case, we do exactly what the server
845          * tells us to do, as dictated by the 'flags'.
846          *
847          * We do exactly the same thing during recovery, when the server is
848          * more or less trusting the clients not to lie.
849          *
850          * FIXME (bug 268): Detect obvious lies by checking compatibility in
851          * granted/converting queues. */
852         ldlm_resource_unlink_lock(lock);
853         if (local) {
854                 if (*flags & LDLM_FL_BLOCK_CONV)
855                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
856                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
857                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
858                 else
859                         ldlm_grant_lock(lock, NULL, 0, 0);
860                 GOTO(out, ELDLM_OK);
861         } else if (*flags & LDLM_FL_REPLAY) {
862                 if (*flags & LDLM_FL_BLOCK_CONV) {
863                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
864                         GOTO(out, ELDLM_OK);
865                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
866                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
867                         GOTO(out, ELDLM_OK);
868                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
869                         ldlm_grant_lock(lock, NULL, 0, 0);
870                         GOTO(out, ELDLM_OK);
871                 }
872                 /* If no flags, fall through to normal enqueue path. */
873         }
874
875         policy = ldlm_processing_policy_table[res->lr_type];
876         policy(lock, flags, 1, &rc);
877         GOTO(out, rc);
878 out:
879         l_unlock(&ns->ns_lock);
880         return rc;
881 }
882
883 /* Must be called with namespace taken: queue is waiting or converting. */
884 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue)
885 {
886         struct list_head *tmp, *pos;
887         ldlm_processing_policy policy;
888         int flags;
889         int rc = LDLM_ITER_CONTINUE;
890         ldlm_error_t err;
891         ENTRY;
892
893         policy = ldlm_processing_policy_table[res->lr_type];
894         LASSERT(policy);
895
896         list_for_each_safe(tmp, pos, queue) {
897                 struct ldlm_lock *pending;
898                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
899
900                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
901
902                 flags = 0;
903                 rc = policy(pending, &flags, 0, &err);
904                 if (rc != LDLM_ITER_CONTINUE)
905                         break;
906         }
907
908         RETURN(rc);
909 }
910
911 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list)
912 {
913         struct list_head *tmp, *pos;
914         int rc, retval = 0;
915         ENTRY;
916
917         l_check_no_ns_lock(ns);
918
919         list_for_each_safe(tmp, pos, rpc_list) {
920                 struct ldlm_ast_work *w =
921                         list_entry(tmp, struct ldlm_ast_work, w_list);
922
923                 /* It's possible to receive a completion AST before we've set
924                  * the l_completion_ast pointer: either because the AST arrived
925                  * before the reply, or simply because there's a small race
926                  * window between receiving the reply and finishing the local
927                  * enqueue. (bug 842)
928                  *
929                  * This can't happen with the blocking_ast, however, because we
930                  * will never call the local blocking_ast until we drop our
931                  * reader/writer reference, which we won't do until we get the
932                  * reply and finish enqueueing. */
933                 LASSERT(w->w_lock != NULL);
934                 if (w->w_blocking) {
935                         LASSERT(w->w_lock->l_blocking_ast != NULL);
936                         rc = w->w_lock->l_blocking_ast
937                                 (w->w_lock, &w->w_desc, w->w_data,
938                                  LDLM_CB_BLOCKING);
939                 } else if (w->w_lock->l_completion_ast != NULL) {
940                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
941                                                          w->w_data);
942                 } else {
943                         rc = 0;
944                 }
945
946                 if (rc == -ERESTART)
947                         retval = rc;
948                 else if (rc)
949                         CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
950                                "disconnect client\n");
951                 LDLM_LOCK_PUT(w->w_lock);
952                 list_del(&w->w_list);
953                 OBD_FREE(w, sizeof(*w));
954         }
955         RETURN(retval);
956 }
957
958 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
959 {
960         ldlm_reprocess_all(res);
961         return LDLM_ITER_CONTINUE;
962 }
963
964 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
965 {
966         int i, rc;
967
968         l_lock(&ns->ns_lock);
969         for (i = 0; i < RES_HASH_SIZE; i++) {
970                 struct list_head *tmp, *next;
971                 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
972                         struct ldlm_resource *res =
973                                 list_entry(tmp, struct ldlm_resource, lr_hash);
974
975                         ldlm_resource_getref(res);
976                         l_unlock(&ns->ns_lock);
977                         rc = reprocess_one_queue(res, NULL);
978                         l_lock(&ns->ns_lock);
979                         next = tmp->next;
980                         ldlm_resource_putref(res);
981                         if (rc == LDLM_ITER_STOP)
982                                 GOTO(out, rc);
983                 }
984         }
985  out:
986         l_unlock(&ns->ns_lock);
987         EXIT;
988 }
989
990 void ldlm_reprocess_all(struct ldlm_resource *res)
991 {
992         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
993         int rc;
994         ENTRY;
995
996         /* Local lock trees don't get reprocessed. */
997         if (res->lr_namespace->ns_client) {
998                 EXIT;
999                 return;
1000         }
1001
1002  restart:
1003         l_lock(&res->lr_namespace->ns_lock);
1004         res->lr_tmp = &rpc_list;
1005
1006         rc = ldlm_reprocess_queue(res, &res->lr_converting);
1007         if (rc == LDLM_ITER_CONTINUE)
1008                 ldlm_reprocess_queue(res, &res->lr_waiting);
1009
1010         res->lr_tmp = NULL;
1011         l_unlock(&res->lr_namespace->ns_lock);
1012
1013         rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
1014         if (rc == -ERESTART) {
1015                 LASSERT(list_empty(&rpc_list));
1016                 goto restart;
1017         }
1018         EXIT;
1019 }
1020
1021 void ldlm_cancel_callback(struct ldlm_lock *lock)
1022 {
1023         struct ldlm_namespace *ns;
1024
1025         ns = lock->l_resource->lr_namespace;
1026         l_lock(&ns->ns_lock);
1027         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1028                 lock->l_flags |= LDLM_FL_CANCEL;
1029                 if (lock->l_blocking_ast) {
1030                         l_unlock(&ns->ns_lock);
1031                         // l_check_no_ns_lock(ns);
1032                         lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1033                                              LDLM_CB_CANCELING);
1034                         return;
1035                 } else {
1036                         LDLM_DEBUG(lock, "no blocking ast");
1037                 }
1038         }
1039         l_unlock(&ns->ns_lock);
1040 }
1041
1042 void ldlm_lock_cancel(struct ldlm_lock *lock)
1043 {
1044         struct ldlm_resource *res;
1045         struct ldlm_namespace *ns;
1046         ENTRY;
1047
1048         res = lock->l_resource;
1049         ns = res->lr_namespace;
1050
1051         l_lock(&ns->ns_lock);
1052         ldlm_del_waiting_lock(lock);
1053
1054         /* Please do not, no matter how tempting, remove this LBUG without
1055          * talking to me first. -phik */
1056         if (lock->l_readers || lock->l_writers) {
1057                 LDLM_ERROR(lock, "lock still has references");
1058                 LBUG();
1059         }
1060
1061         ldlm_cancel_callback(lock);
1062
1063         ldlm_resource_unlink_lock(lock);
1064         ldlm_lock_destroy(lock);
1065         l_unlock(&ns->ns_lock);
1066         EXIT;
1067 }
1068
1069 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1070 {
1071         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1072         ENTRY;
1073
1074         if (lock == NULL)
1075                 RETURN(-EINVAL);
1076
1077         lock->l_ast_data = data;
1078         LDLM_LOCK_PUT(lock);
1079         RETURN(0);
1080 }
1081
1082 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1083 {
1084         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
1085         struct ldlm_lock *lock;
1086         struct ldlm_resource *res;
1087
1088         l_lock(&ns->ns_lock);
1089         while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1090                 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1091                                   struct ldlm_lock, l_export_chain);
1092                 res = ldlm_resource_getref(lock->l_resource);
1093                 LDLM_DEBUG(lock, "export %p", exp);
1094                 ldlm_lock_cancel(lock);
1095                 l_unlock(&ns->ns_lock);
1096                 ldlm_reprocess_all(res);
1097                 ldlm_resource_putref(res);
1098                 l_lock(&ns->ns_lock);
1099         }
1100         l_unlock(&ns->ns_lock);
1101 }
1102
1103 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1104                                         int *flags)
1105 {
1106         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1107         struct ldlm_resource *res;
1108         struct ldlm_namespace *ns;
1109         int granted = 0;
1110         int old_mode, rc;
1111         ldlm_error_t err;
1112         ENTRY;
1113
1114         if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1115                 *flags |= LDLM_FL_BLOCK_GRANTED;
1116                 RETURN(lock->l_resource);
1117         }
1118
1119         LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1120                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1121
1122         res = lock->l_resource;
1123         ns = res->lr_namespace;
1124
1125         l_lock(&ns->ns_lock);
1126
1127         old_mode = lock->l_req_mode;
1128         lock->l_req_mode = new_mode;
1129         ldlm_resource_unlink_lock(lock);
1130
1131         /* If this is a local resource, put it on the appropriate list. */
1132         if (res->lr_namespace->ns_client) {
1133                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1134                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
1135                 } else {
1136                         /* This should never happen, because of the way the
1137                          * server handles conversions. */
1138                         LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1139                                    *flags);
1140                         LBUG();
1141
1142                         res->lr_tmp = &rpc_list;
1143                         ldlm_grant_lock(lock, NULL, 0, 0);
1144                         res->lr_tmp = NULL;
1145                         granted = 1;
1146                         /* FIXME: completion handling not with ns_lock held ! */
1147                         if (lock->l_completion_ast)
1148                                 lock->l_completion_ast(lock, 0, NULL);
1149                 }
1150         } else {
1151                 int pflags = 0;
1152                 ldlm_processing_policy policy;
1153                 policy = ldlm_processing_policy_table[res->lr_type];
1154                 res->lr_tmp = &rpc_list;
1155                 rc = policy(lock, &pflags, 0, &err);
1156                 res->lr_tmp = NULL;
1157                 if (rc == LDLM_ITER_STOP) {
1158                         lock->l_req_mode = old_mode;
1159                         ldlm_resource_add_lock(res, &res->lr_granted, lock);
1160                         res = NULL;
1161                 } else {
1162                         *flags |= LDLM_FL_BLOCK_GRANTED;
1163                         granted = 1;
1164                 }
1165         }
1166
1167         l_unlock(&ns->ns_lock);
1168
1169         if (granted)
1170                 ldlm_run_ast_work(ns, &rpc_list);
1171         RETURN(res);
1172 }
1173
1174 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1175 {
1176         struct obd_device *obd = NULL;
1177
1178         if (!((libcfs_debug | D_ERROR) & level))
1179                 return;
1180
1181         if (!lock) {
1182                 CDEBUG(level, "  NULL LDLM lock\n");
1183                 return;
1184         }
1185
1186         CDEBUG(level, "  -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1187                lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1188                pos, lock->l_pid);
1189         if (lock->l_conn_export != NULL)
1190                 obd = lock->l_conn_export->exp_obd;
1191         if (lock->l_export && lock->l_export->exp_connection) {
1192                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1193                        libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1194                        lock->l_remote_handle.cookie);
1195         } else if (obd == NULL) {
1196                 CDEBUG(level, "  Node: local\n");
1197         } else {
1198                 struct obd_import *imp = obd->u.cli.cl_import;
1199                 CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
1200                        libcfs_nid2str(imp->imp_connection->c_peer.nid),
1201                        lock->l_remote_handle.cookie);
1202         }
1203         CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1204                lock->l_resource->lr_name.name[0],
1205                lock->l_resource->lr_name.name[1]);
1206         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1207                "write: %d\n", ldlm_lockname[lock->l_req_mode],
1208                ldlm_lockname[lock->l_granted_mode],
1209                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers);
1210         if (lock->l_resource->lr_type == LDLM_EXTENT)
1211                 CDEBUG(level, "  Extent: "LPU64" -> "LPU64
1212                        " (req "LPU64"-"LPU64")\n",
1213                        lock->l_policy_data.l_extent.start,
1214                        lock->l_policy_data.l_extent.end,
1215                        lock->l_req_extent.start, lock->l_req_extent.end);
1216         else if (lock->l_resource->lr_type == LDLM_FLOCK)
1217                 CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
1218                        lock->l_policy_data.l_flock.pid,
1219                        lock->l_policy_data.l_flock.start,
1220                        lock->l_policy_data.l_flock.end);
1221 }
1222
1223 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1224 {
1225         struct ldlm_lock *lock;
1226
1227         lock = ldlm_handle2lock(lockh);
1228         if (lock == NULL)
1229                 return;
1230
1231         ldlm_lock_dump(D_OTHER, lock, 0);
1232
1233         LDLM_LOCK_PUT(lock);
1234 }