1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
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.
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.
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.
23 #define DEBUG_SUBSYSTEM S_LDLM
25 #include <linux/config.h>
26 #include <linux/kernel.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>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #include <asm/segment.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
45 #include <liblustre.h>
48 #include <linux/lustre_dlm.h>
49 #include <linux/obd_class.h>
50 #include <linux/lustre_lib.h>
53 - only the owner of the lock changes l_owner/l_depth
54 - if a non-owner changes or checks the variables a spin lock is taken
57 void l_lock_init(struct lustre_lock *lock)
59 sema_init(&lock->l_sem, 1);
60 spin_lock_init(&lock->l_spin);
63 void l_lock(struct lustre_lock *lock)
67 spin_lock(&lock->l_spin);
68 if (lock->l_owner == current)
70 spin_unlock(&lock->l_spin);
72 /* This is safe to increment outside the spinlock because we
73 * can only have 1 CPU running on the current task
74 * (i.e. l_owner == current), regardless of the number of CPUs.
80 spin_lock(&lock->l_spin);
81 lock->l_owner = current;
83 spin_unlock(&lock->l_spin);
87 void l_unlock(struct lustre_lock *lock)
89 LASSERT(lock->l_owner == current);
90 LASSERT(lock->l_depth >= 0);
92 spin_lock(&lock->l_spin);
93 if (--lock->l_depth < 0) {
95 spin_unlock(&lock->l_spin);
99 spin_unlock(&lock->l_spin);
102 int l_has_lock(struct lustre_lock *lock)
104 int depth = -1, owner = 0;
106 spin_lock(&lock->l_spin);
107 if (lock->l_owner == current) {
108 depth = lock->l_depth;
111 spin_unlock(&lock->l_spin);
114 CDEBUG(D_INFO, "lock_depth: %d\n", depth);