Whamcloud - gitweb
b=619803
[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 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 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/random.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/lustre_mds.h>
31 #include <linux/obd_class.h>
32
33 /* this lock protects ldlm_handle2lock's integrity */
34 //static spinlock_t ldlm_handle_lock = SPIN_LOCK_UNLOCKED;
35
36 /* lock types */
37 char *ldlm_lockname[] = {
38         [0] "--",
39         [LCK_EX] "EX",
40         [LCK_PW] "PW",
41         [LCK_PR] "PR",
42         [LCK_CW] "CW",
43         [LCK_CR] "CR",
44         [LCK_NL] "NL"
45 };
46 char *ldlm_typename[] = {
47         [LDLM_PLAIN] "PLN",
48         [LDLM_EXTENT] "EXT",
49         [LDLM_MDSINTENT] "INT"
50 };
51
52 char *ldlm_it2str(int it)
53 {
54         switch (it) {
55         case IT_OPEN:
56                 return "open";
57         case IT_CREAT:
58                 return "creat";
59         case (IT_OPEN | IT_CREAT):
60                 return "open|creat";
61         case IT_MKDIR:
62                 return "mkdir";
63         case IT_LINK:
64                 return "link";
65         case IT_LINK2:
66                 return "link2";
67         case IT_SYMLINK:
68                 return "symlink";
69         case IT_UNLINK:
70                 return "unlink";
71         case IT_RMDIR:
72                 return "rmdir";
73         case IT_RENAME:
74                 return "rename";
75         case IT_RENAME2:
76                 return "rename2";
77         case IT_READDIR:
78                 return "readdir";
79         case IT_GETATTR:
80                 return "getattr";
81         case IT_SETATTR:
82                 return "setattr";
83         case IT_READLINK:
84                 return "readlink";
85         case IT_MKNOD:
86                 return "mknod";
87         case IT_LOOKUP:
88                 return "lookup";
89         default:
90                 CERROR("Unknown intent %d\n", it);
91                 return "UNKNOWN";
92         }
93 }
94
95 extern kmem_cache_t *ldlm_lock_slab;
96
97 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
98
99 ldlm_res_compat ldlm_res_compat_table[] = {
100         [LDLM_PLAIN] ldlm_plain_compat,
101         [LDLM_EXTENT] ldlm_extent_compat,
102         [LDLM_MDSINTENT] ldlm_plain_compat
103 };
104
105 ldlm_res_policy ldlm_res_policy_table[] = {
106         [LDLM_PLAIN] NULL,
107         [LDLM_EXTENT] ldlm_extent_policy,
108         [LDLM_MDSINTENT] NULL
109 };
110
111 void ldlm_register_intent(int (*arg) (struct ldlm_lock * lock, void *req_cookie,
112                                       ldlm_mode_t mode, void *data))
113 {
114         ldlm_res_policy_table[LDLM_MDSINTENT] = arg;
115 }
116
117 void ldlm_unregister_intent(void)
118 {
119         ldlm_res_policy_table[LDLM_MDSINTENT] = NULL;
120 }
121
122 /*
123  * REFCOUNTED LOCK OBJECTS
124  */
125
126
127 /*
128  * Lock refcounts, during creation:
129  *   - one special one for allocation, dec'd only once in destroy
130  *   - one for being a lock that's in-use
131  *   - one for the addref associated with a new lock
132  */
133 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
134 {
135         l_lock(&lock->l_resource->lr_namespace->ns_lock);
136         lock->l_refc++;
137         ldlm_resource_getref(lock->l_resource);
138         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
139         return lock;
140 }
141
142 void ldlm_lock_put(struct ldlm_lock *lock)
143 {
144         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
145         ENTRY;
146
147         l_lock(&ns->ns_lock);
148         lock->l_refc--;
149         //LDLM_DEBUG(lock, "after refc--");
150         if (lock->l_refc < 0)
151                 LBUG();
152
153         if (ldlm_resource_put(lock->l_resource))
154                 lock->l_resource = NULL;
155         if (lock->l_parent)
156                 LDLM_LOCK_PUT(lock->l_parent);
157
158         if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
159                 l_unlock(&ns->ns_lock);
160                 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
161
162                 //spin_lock(&ldlm_handle_lock);
163                 spin_lock(&ns->ns_counter_lock);
164                 ns->ns_locks--;
165                 spin_unlock(&ns->ns_counter_lock);
166
167                 lock->l_resource = NULL;
168                 lock->l_random = 0xdeadbeefcafebabe;
169                 if (lock->l_export && lock->l_export->exp_connection)
170                         ptlrpc_put_connection(lock->l_export->exp_connection);
171                 kmem_cache_free(ldlm_lock_slab, lock);
172                 //spin_unlock(&ldlm_handle_lock);
173                 CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 0).\n",
174                        sizeof(*lock), lock);
175         } else
176                 l_unlock(&ns->ns_lock);
177
178         EXIT;
179 }
180
181 void ldlm_lock_destroy(struct ldlm_lock *lock)
182 {
183         ENTRY;
184         l_lock(&lock->l_resource->lr_namespace->ns_lock);
185
186         if (!list_empty(&lock->l_children)) {
187                 LDLM_DEBUG(lock, "still has children (%p)!",
188                            lock->l_children.next);
189                 ldlm_lock_dump(lock);
190                 LBUG();
191         }
192         if (lock->l_readers || lock->l_writers) {
193                 LDLM_DEBUG(lock, "lock still has references");
194                 ldlm_lock_dump(lock);
195                 LBUG();
196         }
197
198         if (!list_empty(&lock->l_res_link)) {
199                 ldlm_lock_dump(lock);
200                 LBUG();
201         }
202
203         if (lock->l_flags & LDLM_FL_DESTROYED) {
204                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
205                 EXIT;
206                 return;
207         }
208
209         list_del(&lock->l_export_chain);
210         lock->l_export = NULL;
211         lock->l_flags |= LDLM_FL_DESTROYED;
212
213         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
214         LDLM_LOCK_PUT(lock);
215         EXIT;
216 }
217
218 /*
219    usage: pass in a resource on which you have done get
220           pass in a parent lock on which you have done a get
221           do not put the resource or the parent
222    returns: lock with refcount 1
223 */
224 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
225                                        struct ldlm_resource *resource)
226 {
227         struct ldlm_lock *lock;
228         ENTRY;
229
230         if (resource == NULL)
231                 LBUG();
232
233         lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
234         if (lock == NULL)
235                 RETURN(NULL);
236
237         memset(lock, 0, sizeof(*lock));
238         get_random_bytes(&lock->l_random, sizeof(__u64));
239
240         lock->l_resource = resource;
241         /* this refcount matches the one of the resource passed
242            in which is not being put away */
243         lock->l_refc = 1;
244         INIT_LIST_HEAD(&lock->l_children);
245         INIT_LIST_HEAD(&lock->l_res_link);
246         INIT_LIST_HEAD(&lock->l_export_chain);
247         INIT_LIST_HEAD(&lock->l_pending_chain);
248         init_waitqueue_head(&lock->l_waitq);
249
250         spin_lock(&resource->lr_namespace->ns_counter_lock);
251         resource->lr_namespace->ns_locks++;
252         spin_unlock(&resource->lr_namespace->ns_counter_lock);
253
254         if (parent != NULL) {
255                 l_lock(&parent->l_resource->lr_namespace->ns_lock);
256                 lock->l_parent = parent;
257                 list_add(&lock->l_childof, &parent->l_children);
258                 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
259         }
260
261         CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
262                "%p (tot %d).\n", sizeof(*lock), lock, 1);
263         /* this is the extra refcount, to prevent the lock from evaporating */
264         LDLM_LOCK_GET(lock);
265         RETURN(lock);
266 }
267
268 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
269 {
270         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
271         struct ldlm_resource *oldres = lock->l_resource;
272         int type, i;
273         ENTRY;
274
275         l_lock(&ns->ns_lock);
276         if (memcmp(new_resid, lock->l_resource->lr_name,
277                    sizeof(lock->l_resource->lr_name)) == 0) {
278                 /* Nothing to do */
279                 l_unlock(&ns->ns_lock);
280                 RETURN(0);
281         }
282
283         type = lock->l_resource->lr_type;
284         if (new_resid[0] == 0)
285                 LBUG();
286         lock->l_resource = ldlm_resource_get(ns, NULL, new_resid, type, 1);
287         if (lock->l_resource == NULL) {
288                 LBUG();
289                 RETURN(-ENOMEM);
290         }
291
292         /* move references over */
293         for (i = 0; i < lock->l_refc; i++) {
294                 int rc;
295                 ldlm_resource_getref(lock->l_resource);
296                 rc = ldlm_resource_put(oldres);
297                 if (rc == 1 && i != lock->l_refc - 1)
298                         LBUG();
299         }
300         /* compensate for the initial get above.. */
301         ldlm_resource_put(lock->l_resource);
302
303         l_unlock(&ns->ns_lock);
304         RETURN(0);
305 }
306
307 /*
308  *  HANDLES
309  */
310
311 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
312 {
313         lockh->addr = (__u64) (unsigned long)lock;
314         lockh->cookie = lock->l_random;
315 }
316
317 struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle)
318 {
319         struct ldlm_lock *lock = NULL, *retval = NULL;
320         ENTRY;
321
322         if (!handle || !handle->addr) {
323                 CERROR("bogus handle %p->"LPX64"\n", handle,
324                        handle ? handle->addr : -1);
325                 RETURN(NULL);
326         }
327
328         //spin_lock(&ldlm_handle_lock);
329         lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
330         if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
331                 CERROR("bogus lock %p\n", lock);
332                 GOTO(out2, retval);
333         }
334
335         if (lock->l_random != handle->cookie) {
336                 CERROR("bogus cookie: lock "LPX64", handle "LPX64"\n",
337                        lock->l_random, handle->cookie);
338                 GOTO(out, NULL);
339         }
340         if (!lock->l_resource) {
341                 CERROR("trying to lock bogus resource: lock %p\n", lock);
342                 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
343                 GOTO(out2, retval);
344         }
345         if (!lock->l_resource->lr_namespace) {
346                 CERROR("trying to lock bogus namespace: lock %p\n", lock);
347                 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
348                 GOTO(out2, retval);
349         }
350
351         l_lock(&lock->l_resource->lr_namespace->ns_lock);
352         if (lock->l_flags & LDLM_FL_DESTROYED) {
353                 CERROR("lock already destroyed: lock %p\n", lock);
354                 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
355                 GOTO(out, NULL);
356         }
357
358         retval = LDLM_LOCK_GET(lock);
359         if (!retval)
360                 CERROR("lock disappeared below us!!! %p\n", lock);
361         EXIT;
362  out:
363         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
364  out2:
365         //spin_unlock(&ldlm_handle_lock);
366         return retval;
367 }
368
369 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
370 {
371         return lockmode_compat(a->l_req_mode, b->l_req_mode);
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_extent, &lock->l_extent, sizeof(desc->l_extent));
380         memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
381 }
382
383 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
384                                    struct ldlm_lock *new)
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         OBD_ALLOC(w, sizeof(*w));
394         if (!w) {
395                 LBUG();
396                 GOTO(out, 0);
397         }
398
399         if (new) {
400                 lock->l_flags |= LDLM_FL_AST_SENT;
401                 w->w_blocking = 1;
402                 ldlm_lock2desc(new, &w->w_desc);
403         }
404
405         w->w_lock = LDLM_LOCK_GET(lock);
406         list_add(&w->w_list, lock->l_resource->lr_tmp);
407       out:
408         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
409         return;
410 }
411
412 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
413 {
414         struct ldlm_lock *lock;
415
416         lock = ldlm_handle2lock(lockh);
417         ldlm_lock_addref_internal(lock, mode);
418         LDLM_LOCK_PUT(lock);
419 }
420
421 /* only called for local locks */
422 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
423 {
424         l_lock(&lock->l_resource->lr_namespace->ns_lock);
425         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
426                 lock->l_readers++;
427         else
428                 lock->l_writers++;
429         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
430         LDLM_LOCK_GET(lock);
431         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
432 }
433
434 /* Args: unlocked lock */
435 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
436 {
437         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
438         ENTRY;
439
440         if (lock == NULL)
441                 LBUG();
442
443         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
444         l_lock(&lock->l_resource->lr_namespace->ns_lock);
445         if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
446                 lock->l_readers--;
447         else
448                 lock->l_writers--;
449
450         /* If we received a blocked AST and this was the last reference,
451          * run the callback. */
452         if (!lock->l_readers && !lock->l_writers &&
453             (lock->l_flags & LDLM_FL_CBPENDING)) {
454                 if (!lock->l_resource->lr_namespace->ns_client &&
455                     lock->l_export)
456                         CERROR("FL_CBPENDING set on non-local lock--just a "
457                                "warning\n");
458
459                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
460                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
461
462                 /* FIXME: need a real 'desc' here */
463                 lock->l_blocking_ast(lock, NULL, lock->l_data,
464                                      lock->l_data_len, LDLM_CB_BLOCKING);
465         } else
466                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
467
468         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
469         LDLM_LOCK_PUT(lock);    /* matches the handle2lock above */
470
471         EXIT;
472 }
473
474 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
475                                  struct list_head *queue)
476 {
477         struct list_head *tmp, *pos;
478         int rc = 1;
479
480         list_for_each_safe(tmp, pos, queue) {
481                 struct ldlm_lock *child;
482                 ldlm_res_compat compat;
483
484                 child = list_entry(tmp, struct ldlm_lock, l_res_link);
485                 if (lock == child)
486                         continue;
487
488                 compat = ldlm_res_compat_table[child->l_resource->lr_type];
489                 if (compat && compat(child, lock)) {
490                         CDEBUG(D_OTHER, "compat function succeded, next.\n");
491                         continue;
492                 }
493                 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
494                         CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
495                         continue;
496                 }
497
498                 rc = 0;
499
500                 if (send_cbs && child->l_blocking_ast != NULL) {
501                         CDEBUG(D_OTHER, "lock %p incompatible; sending "
502                                "blocking AST.\n", child);
503                         ldlm_add_ast_work_item(child, lock);
504                 }
505         }
506
507         return rc;
508 }
509
510 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
511 {
512         int rc;
513         ENTRY;
514
515         l_lock(&lock->l_resource->lr_namespace->ns_lock);
516         rc = ldlm_lock_compat_list(lock, send_cbs,
517                                    &lock->l_resource->lr_granted);
518         /* FIXME: should we be sending ASTs to converting? */
519         if (rc)
520                 rc = ldlm_lock_compat_list
521                         (lock, send_cbs, &lock->l_resource->lr_converting);
522
523         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
524         RETURN(rc);
525 }
526
527 /* NOTE: called by
528    - ldlm_handle_enqueuque - resource
529 */
530 void ldlm_grant_lock(struct ldlm_lock *lock)
531 {
532         struct ldlm_resource *res = lock->l_resource;
533         ENTRY;
534
535         l_lock(&lock->l_resource->lr_namespace->ns_lock);
536         ldlm_resource_add_lock(res, &res->lr_granted, lock);
537         lock->l_granted_mode = lock->l_req_mode;
538
539         if (lock->l_granted_mode < res->lr_most_restr)
540                 res->lr_most_restr = lock->l_granted_mode;
541
542         if (lock->l_completion_ast) {
543                 ldlm_add_ast_work_item(lock, NULL);
544         }
545         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
546         EXIT;
547 }
548
549 /* returns a referenced lock or NULL */
550 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
551                                       struct ldlm_extent *extent)
552 {
553         struct ldlm_lock *lock;
554         struct list_head *tmp;
555
556         list_for_each(tmp, queue) {
557                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
558
559                 if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
560                         continue;
561
562                 /* lock_convert() takes the resource lock, so we're sure that
563                  * req_mode, lr_type, and l_cookie won't change beneath us */
564                 if (lock->l_req_mode != mode)
565                         continue;
566
567                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
568                     (lock->l_extent.start > extent->start ||
569                      lock->l_extent.end < extent->end))
570                         continue;
571
572                 ldlm_lock_addref_internal(lock, mode);
573                 return lock;
574         }
575
576         return NULL;
577 }
578
579 /* Must be called with no resource or lock locks held.
580  *
581  * Returns 1 if it finds an already-existing lock that is compatible; in this
582  * case, lockh is filled in with a addref()ed lock
583 */
584 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
585                     void *cookie, int cookielen, ldlm_mode_t mode,
586                     struct lustre_handle *lockh)
587 {
588         struct ldlm_resource *res;
589         struct ldlm_lock *lock;
590         int rc = 0;
591         ENTRY;
592
593         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
594         if (res == NULL)
595                 RETURN(0);
596
597         ns = res->lr_namespace;
598         l_lock(&ns->ns_lock);
599
600         if ((lock = search_queue(&res->lr_granted, mode, cookie)))
601                 GOTO(out, rc = 1);
602         if ((lock = search_queue(&res->lr_converting, mode, cookie)))
603                 GOTO(out, rc = 1);
604         if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
605                 GOTO(out, rc = 1);
606
607         EXIT;
608       out:
609         ldlm_resource_put(res);
610         l_unlock(&ns->ns_lock);
611
612         if (lock) {
613                 ldlm_lock2handle(lock, lockh);
614                 if (lock->l_completion_ast)
615                         lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
616         }
617         if (rc)
618                 LDLM_DEBUG(lock, "matched");
619         else
620                 LDLM_DEBUG_NOLOCK("not matched");
621         return rc;
622 }
623
624 /* Returns a referenced lock */
625 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
626                                    struct lustre_handle *parent_lock_handle,
627                                    __u64 * res_id, __u32 type,
628                                    ldlm_mode_t mode, void *data, __u32 data_len)
629 {
630         struct ldlm_resource *res, *parent_res = NULL;
631         struct ldlm_lock *lock, *parent_lock;
632
633         parent_lock = ldlm_handle2lock(parent_lock_handle);
634         if (parent_lock)
635                 parent_res = parent_lock->l_resource;
636
637         res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
638         if (res == NULL)
639                 RETURN(NULL);
640
641         lock = ldlm_lock_new(parent_lock, res);
642         if (lock == NULL) {
643                 ldlm_resource_put(res);
644                 RETURN(NULL);
645         }
646
647         lock->l_req_mode = mode;
648         lock->l_data = data;
649         lock->l_data_len = data_len;
650
651         return lock;
652 }
653
654 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
655 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
656                                void *cookie, int cookie_len,
657                                int *flags,
658                                ldlm_completion_callback completion,
659                                ldlm_blocking_callback blocking)
660 {
661         struct ldlm_resource *res;
662         int local;
663         ldlm_res_policy policy;
664         ENTRY;
665
666         res = lock->l_resource;
667         lock->l_blocking_ast = blocking;
668
669         if (res->lr_type == LDLM_EXTENT)
670                 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
671
672         /* policies are not executed on the client */
673         local = res->lr_namespace->ns_client;
674         if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
675                 int rc;
676                 rc = policy(lock, cookie, lock->l_req_mode, NULL);
677
678                 if (rc == ELDLM_LOCK_CHANGED) {
679                         res = lock->l_resource;
680                         *flags |= LDLM_FL_LOCK_CHANGED;
681                 } else if (rc == ELDLM_LOCK_ABORTED) {
682                         ldlm_lock_destroy(lock);
683                         RETURN(rc);
684                 }
685         }
686
687         lock->l_cookie = cookie;
688         lock->l_cookie_len = cookie_len;
689
690         l_lock(&res->lr_namespace->ns_lock);
691         if (local && lock->l_req_mode == lock->l_granted_mode) {
692                 /* The server returned a blocked lock, but it was granted before
693                  * we got a chance to actually enqueue it.  We don't need to do
694                  * anything else. */
695                 *flags &= ~(LDLM_FL_BLOCK_GRANTED | 
696                           LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
697                 GOTO(out, ELDLM_OK);
698         }
699
700         /* This distinction between local lock trees is very important; a client
701          * namespace only has information about locks taken by that client, and
702          * thus doesn't have enough information to decide for itself if it can
703          * be granted (below).  In this case, we do exactly what the server
704          * tells us to do, as dictated by the 'flags' */
705         ldlm_resource_unlink_lock(lock);
706         if (local) {
707                 if (*flags & LDLM_FL_BLOCK_CONV)
708                         ldlm_resource_add_lock(res, res->lr_converting.prev,
709                                                lock);
710                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
711                         ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
712                 else
713                         ldlm_grant_lock(lock);
714                 GOTO(out, ELDLM_OK);
715         }
716
717         /* FIXME: We may want to optimize by checking lr_most_restr */
718         if (!list_empty(&res->lr_converting)) {
719                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
720                 *flags |= LDLM_FL_BLOCK_CONV;
721                 GOTO(out, ELDLM_OK);
722         }
723         if (!list_empty(&res->lr_waiting)) {
724                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
725                 *flags |= LDLM_FL_BLOCK_WAIT;
726                 GOTO(out, ELDLM_OK);
727         }
728         if (!ldlm_lock_compat(lock, 0)) {
729                 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
730                 *flags |= LDLM_FL_BLOCK_GRANTED;
731                 GOTO(out, ELDLM_OK);
732         }
733
734         ldlm_grant_lock(lock);
735         EXIT;
736       out:
737         l_unlock(&res->lr_namespace->ns_lock);
738         /* Don't set 'completion_ast' until here so that if the lock is granted
739          * immediately we don't do an unnecessary completion call. */
740         lock->l_completion_ast = completion;
741         return ELDLM_OK;
742 }
743
744 /* Must be called with namespace taken: queue is waiting or converting. */
745 static int ldlm_reprocess_queue(struct ldlm_resource *res,
746                                 struct list_head *queue)
747 {
748         struct list_head *tmp, *pos;
749         ENTRY;
750
751         list_for_each_safe(tmp, pos, queue) {
752                 struct ldlm_lock *pending;
753                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
754
755                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
756
757                 if (!ldlm_lock_compat(pending, 1))
758                         RETURN(1);
759
760                 list_del_init(&pending->l_res_link);
761                 ldlm_grant_lock(pending);
762         }
763
764         RETURN(0);
765 }
766
767 void ldlm_run_ast_work(struct list_head *rpc_list)
768 {
769         struct list_head *tmp, *pos;
770         int rc;
771         ENTRY;
772
773         list_for_each_safe(tmp, pos, rpc_list) {
774                 struct ldlm_ast_work *w =
775                         list_entry(tmp, struct ldlm_ast_work, w_list);
776
777                 if (w->w_blocking)
778                         rc = w->w_lock->l_blocking_ast
779                                 (w->w_lock, &w->w_desc, w->w_data,
780                                  w->w_datalen, LDLM_CB_BLOCKING);
781                 else
782                         rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
783                 if (rc)
784                         CERROR("Failed AST - should clean & disconnect "
785                                "client\n");
786                 LDLM_LOCK_PUT(w->w_lock);
787                 list_del(&w->w_list);
788                 OBD_FREE(w, sizeof(*w));
789         }
790         EXIT;
791 }
792
793 /* Must be called with resource->lr_lock not taken. */
794 void ldlm_reprocess_all(struct ldlm_resource *res)
795 {
796         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
797         ENTRY;
798
799         /* Local lock trees don't get reprocessed. */
800         if (res->lr_namespace->ns_client) {
801                 EXIT;
802                 return;
803         }
804
805         l_lock(&res->lr_namespace->ns_lock);
806         res->lr_tmp = &rpc_list;
807
808         ldlm_reprocess_queue(res, &res->lr_converting);
809         if (list_empty(&res->lr_converting))
810                 ldlm_reprocess_queue(res, &res->lr_waiting);
811
812         res->lr_tmp = NULL;
813         l_unlock(&res->lr_namespace->ns_lock);
814
815         ldlm_run_ast_work(&rpc_list);
816         EXIT;
817 }
818
819 void ldlm_cancel_callback(struct ldlm_lock *lock)
820 {
821         l_lock(&lock->l_resource->lr_namespace->ns_lock);
822         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
823                 lock->l_flags |= LDLM_FL_CANCEL;
824                 lock->l_blocking_ast(lock, NULL, lock->l_data,
825                                      lock->l_data_len, LDLM_CB_CANCELING);
826         }
827         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
828 }
829
830 void ldlm_lock_cancel(struct ldlm_lock *lock)
831 {
832         struct ldlm_resource *res;
833         struct ldlm_namespace *ns;
834         ENTRY;
835
836         res = lock->l_resource;
837         ns = res->lr_namespace;
838
839         l_lock(&ns->ns_lock);
840         if (lock->l_readers || lock->l_writers)
841                 CDEBUG(D_INFO, "lock still has references (%d readers, %d "
842                        "writers)\n", lock->l_readers, lock->l_writers);
843
844         ldlm_cancel_callback(lock);
845
846         ldlm_del_waiting_lock(lock);
847         ldlm_resource_unlink_lock(lock);
848         ldlm_lock_destroy(lock);
849         l_unlock(&ns->ns_lock);
850         EXIT;
851 }
852
853 void ldlm_cancel_locks_for_export(struct obd_export *exp)
854 {
855         struct list_head *iter, *n; /* MUST BE CALLED "n"! */
856
857         list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
858                 struct ldlm_lock *lock;
859                 struct ldlm_resource *res;
860                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
861                 res = ldlm_resource_getref(lock->l_resource);
862                 LDLM_DEBUG(lock, "cancelling lock for export %p", exp);
863                 ldlm_lock_cancel(lock);
864                 ldlm_reprocess_all(res);
865                 ldlm_resource_put(res);
866         }
867 }
868
869 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
870                                         int *flags)
871 {
872         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
873         struct ldlm_resource *res;
874         struct ldlm_namespace *ns;
875         int granted = 0;
876         ENTRY;
877
878         res = lock->l_resource;
879         ns = res->lr_namespace;
880
881         l_lock(&ns->ns_lock);
882
883         lock->l_req_mode = new_mode;
884         ldlm_resource_unlink_lock(lock);
885
886         /* If this is a local resource, put it on the appropriate list. */
887         if (res->lr_namespace->ns_client) {
888                 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
889                         ldlm_resource_add_lock(res, res->lr_converting.prev,
890                                                lock);
891                 else {
892                         /* This should never happen, because of the way the
893                          * server handles conversions. */
894                         LBUG();
895
896                         res->lr_tmp = &rpc_list;
897                         ldlm_grant_lock(lock);
898                         res->lr_tmp = NULL;
899                         granted = 1;
900                         /* FIXME: completion handling not with ns_lock held ! */
901                         if (lock->l_completion_ast)
902                                 lock->l_completion_ast(lock, 0);
903                 }
904         } else {
905                 /* FIXME: We should try the conversion right away and possibly
906                  * return success without the need for an extra AST */
907                 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
908                 *flags |= LDLM_FL_BLOCK_CONV;
909         }
910
911         l_unlock(&ns->ns_lock);
912
913         if (granted)
914                 ldlm_run_ast_work(&rpc_list);
915         RETURN(res);
916 }
917
918 void ldlm_lock_dump(struct ldlm_lock *lock)
919 {
920         char ver[128];
921
922         if (!(portal_debug & D_OTHER))
923                 return;
924
925         if (RES_VERSION_SIZE != 4)
926                 LBUG();
927
928         if (!lock) {
929                 CDEBUG(D_OTHER, "  NULL LDLM lock\n");
930                 return;
931         }
932
933         snprintf(ver, sizeof(ver), "%x %x %x %x",
934                  lock->l_version[0], lock->l_version[1],
935                  lock->l_version[2], lock->l_version[3]);
936
937         CDEBUG(D_OTHER, "  -- Lock dump: %p (%s)\n", lock, ver);
938         if (lock->l_export && lock->l_export->exp_connection)
939                 CDEBUG(D_OTHER, "  Node: NID %x (rhandle: "LPX64")\n",
940                        lock->l_export->exp_connection->c_peer.peer_nid,
941                        lock->l_remote_handle.addr);
942         else
943                 CDEBUG(D_OTHER, "  Node: local\n");
944         CDEBUG(D_OTHER, "  Parent: %p\n", lock->l_parent);
945         CDEBUG(D_OTHER, "  Resource: %p ("LPD64")\n", lock->l_resource,
946                lock->l_resource->lr_name[0]);
947         CDEBUG(D_OTHER, "  Requested mode: %d, granted mode: %d\n",
948                (int)lock->l_req_mode, (int)lock->l_granted_mode);
949         CDEBUG(D_OTHER, "  Readers: %u ; Writers; %u\n",
950                lock->l_readers, lock->l_writers);
951         if (lock->l_resource->lr_type == LDLM_EXTENT)
952                 CDEBUG(D_OTHER, "  Extent: %Lu -> %Lu\n",
953                        (unsigned long long)lock->l_extent.start,
954                        (unsigned long long)lock->l_extent.end);
955 }