Whamcloud - gitweb
Modifications for "circulating" request buffers (sized in lustre_net.h)
[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
24
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/locks.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37
38 #include <linux/fs.h>
39 #include <linux/stat.h>
40 #include <asm/uaccess.h>
41 #include <asm/segment.h>
42 #include <linux/mm.h>
43 #include <linux/pagemap.h>
44 #include <linux/smp_lock.h>
45
46 #define DEBUG_SUBSYSTEM S_LDLM
47
48 #include <linux/obd_class.h>
49 #include <linux/lustre_lib.h>
50
51 /* invariants:
52  - only the owner of the lock changes l_owner/l_depth
53  - if a non-owner changes or checks the variables a spin lock is taken
54 */
55
56 void l_lock_init(struct lustre_lock *lock)
57 {
58         sema_init(&lock->l_sem, 1);
59         spin_lock_init(&lock->l_spin);
60 }
61
62 void l_lock(struct lustre_lock *lock)
63 {
64         int owner = 0;
65         spin_lock(&lock->l_spin);
66         if (lock->l_owner == current) { 
67                 owner = 1;
68         }
69         spin_unlock(&lock->l_spin);
70         if (owner)
71                  ++lock->l_depth;
72         else { 
73                 down(&lock->l_sem);
74                 spin_lock(&lock->l_spin);
75                 lock->l_owner = current;
76                 lock->l_depth = 0;
77                 spin_unlock(&lock->l_spin);
78         }
79 }
80
81 void l_unlock(struct lustre_lock *lock)
82 {
83         if (lock->l_owner != current)
84                 LBUG();
85         if (lock->l_depth < 0)
86                 LBUG();
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 }