Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/mini2440.git] / fs / ocfs2 / locks.c
blob544ac6245175f26c23249cf916c5001ae8f5d067
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * locks.c
6 * Userspace file locking support
8 * Copyright (C) 2007 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/fs.h>
27 #include <linux/fcntl.h>
29 #define MLOG_MASK_PREFIX ML_INODE
30 #include <cluster/masklog.h>
32 #include "ocfs2.h"
34 #include "dlmglue.h"
35 #include "file.h"
36 #include "inode.h"
37 #include "locks.h"
39 static int ocfs2_do_flock(struct file *file, struct inode *inode,
40 int cmd, struct file_lock *fl)
42 int ret = 0, level = 0, trylock = 0;
43 struct ocfs2_file_private *fp = file->private_data;
44 struct ocfs2_lock_res *lockres = &fp->fp_flock;
46 if (fl->fl_type == F_WRLCK)
47 level = 1;
48 if (!IS_SETLKW(cmd))
49 trylock = 1;
51 mutex_lock(&fp->fp_mutex);
53 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
54 lockres->l_level > LKM_NLMODE) {
55 int old_level = 0;
57 if (lockres->l_level == LKM_EXMODE)
58 old_level = 1;
60 if (level == old_level)
61 goto out;
64 * Converting an existing lock is not guaranteed to be
65 * atomic, so we can get away with simply unlocking
66 * here and allowing the lock code to try at the new
67 * level.
70 flock_lock_file_wait(file,
71 &(struct file_lock){.fl_type = F_UNLCK});
73 ocfs2_file_unlock(file);
76 ret = ocfs2_file_lock(file, level, trylock);
77 if (ret) {
78 if (ret == -EAGAIN && trylock)
79 ret = -EWOULDBLOCK;
80 else
81 mlog_errno(ret);
82 goto out;
85 ret = flock_lock_file_wait(file, fl);
87 out:
88 mutex_unlock(&fp->fp_mutex);
90 return ret;
93 static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl)
95 int ret;
96 struct ocfs2_file_private *fp = file->private_data;
98 mutex_lock(&fp->fp_mutex);
99 ocfs2_file_unlock(file);
100 ret = flock_lock_file_wait(file, fl);
101 mutex_unlock(&fp->fp_mutex);
103 return ret;
107 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
109 int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
111 struct inode *inode = file->f_mapping->host;
112 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
114 if (!(fl->fl_flags & FL_FLOCK))
115 return -ENOLCK;
116 if (__mandatory_lock(inode))
117 return -ENOLCK;
119 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
120 ocfs2_mount_local(osb))
121 return flock_lock_file_wait(file, fl);
123 if (fl->fl_type == F_UNLCK)
124 return ocfs2_do_funlock(file, cmd, fl);
125 else
126 return ocfs2_do_flock(file, inode, cmd, fl);
129 int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl)
131 struct inode *inode = file->f_mapping->host;
132 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
134 if (!(fl->fl_flags & FL_POSIX))
135 return -ENOLCK;
136 if (__mandatory_lock(inode))
137 return -ENOLCK;
139 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);