Whamcloud - gitweb
fb41ccbbe6efdf7c158fd8f161bf45f7321c7351
[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 #define DEBUG_SUBSYSTEM S_LDLM
24 #ifdef __KERNEL__
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/unistd.h>
32 #include <linux/version.h>
33
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/fs.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #include <asm/segment.h>
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
44 #else 
45 #include <liblustre.h>
46 #endif
47
48 #include <linux/lustre_dlm.h>
49 #include <linux/lustre_lib.h>
50
51 /*
52  * ldlm locking uses resource to serialize access to locks
53  * but there is a case when we change resource of lock upon
54  * enqueue reply. we rely on that lock->l_resource = new_res
55  * is atomic
56  */
57 struct ldlm_resource * lock_res_and_lock(struct ldlm_lock *lock)
58 {
59         struct ldlm_resource *res = lock->l_resource;
60
61         if (!res->lr_namespace->ns_client) {
62                 /* on server-side resource of lock doesn't change */
63                 lock_res(res);
64                 return res;
65         } 
66
67         bit_spin_lock(LDLM_FL_LOCK_PROTECT_BIT, (void *) &lock->l_flags);
68         LASSERT(lock->l_pidb == 0);
69         res = lock->l_resource;
70         lock->l_pidb = current->pid;
71         lock_res(res);
72         return res;
73 }
74
75 void unlock_bitlock(struct ldlm_lock *lock)
76 {
77         LASSERT(lock->l_pidb == current->pid);
78         lock->l_pidb = 0;
79         bit_spin_unlock(LDLM_FL_LOCK_PROTECT_BIT, (void *) &lock->l_flags);
80 }
81
82 void unlock_res_and_lock(struct ldlm_lock *lock)
83 {
84         struct ldlm_resource *res = lock->l_resource;
85
86         if (!res->lr_namespace->ns_client) {
87                 /* on server-side resource of lock doesn't change */
88                 unlock_res(res);
89                 return;
90         }
91
92         unlock_res(res);
93         unlock_bitlock(lock);
94 }
95