Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / ldlm / l_lock.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/string.h>
27 #include <linux/stat.h>
28 #include <linux/errno.h>
29 #include <linux/unistd.h>
30 #include <linux/version.h>
31
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34
35 #include <linux/fs.h>
36 #include <linux/stat.h>
37 #include <asm/uaccess.h>
38 #include <asm/segment.h>
39 #include <linux/mm.h>
40 #include <linux/pagemap.h>
41 #include <linux/smp_lock.h>
42
43 #define DEBUG_SUBSYSTEM S_LDLM
44
45 #include <linux/obd_class.h>
46 #include <linux/lustre_lib.h>
47
48 /* invariants:
49  - only the owner of the lock changes l_owner/l_depth
50  - if a non-owner changes or checks the variables a spin lock is taken
51 */
52
53 void l_lock_init(struct lustre_lock *lock)
54 {
55         sema_init(&lock->l_sem, 1);
56         spin_lock_init(&lock->l_spin);
57 }
58
59 void l_lock(struct lustre_lock *lock)
60 {
61         int owner = 0;
62
63         spin_lock(&lock->l_spin);
64         if (lock->l_owner == current)
65                 owner = 1;
66         spin_unlock(&lock->l_spin);
67
68         /* This is safe to increment outside the spinlock because we
69          * can only have 1 CPU running on the current task
70          * (i.e. l_owner == current), regardless of the number of CPUs.
71          */
72         if (owner) {
73                 ++lock->l_depth;
74         } else {
75                 down(&lock->l_sem);
76                 spin_lock(&lock->l_spin);
77                 lock->l_owner = current;
78                 lock->l_depth = 0;
79                 spin_unlock(&lock->l_spin);
80         }
81 }
82
83 void l_unlock(struct lustre_lock *lock)
84 {
85         LASSERT(lock->l_owner == current);
86         LASSERT(lock->l_depth >= 0);
87
88         spin_lock(&lock->l_spin);
89         if (--lock->l_depth < 0) {
90                 lock->l_owner = NULL;
91                 spin_unlock(&lock->l_spin);
92                 up(&lock->l_sem);
93                 return;
94         }
95         spin_unlock(&lock->l_spin);
96 }
97
98 int l_has_lock(struct lustre_lock *lock)
99 {
100         int depth = -1, owner = 0;
101
102         spin_lock(&lock->l_spin);
103         if (lock->l_owner == current) {
104                 depth = lock->l_depth;
105                 owner = 1;
106         }
107         spin_unlock(&lock->l_spin);
108
109         if (depth >= 0)
110                 CDEBUG(D_INFO, "lock_depth: %d\n", depth);
111         return owner;
112 }