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