1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
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.
27 #include <linux/fcntl.h>
29 #include <cluster/masklog.h>
38 static int ocfs2_do_flock(struct file
*file
, struct inode
*inode
,
39 int cmd
, struct file_lock
*fl
)
41 int ret
= 0, level
= 0, trylock
= 0;
42 struct ocfs2_file_private
*fp
= file
->private_data
;
43 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
45 if (fl
->fl_type
== F_WRLCK
)
50 mutex_lock(&fp
->fp_mutex
);
52 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
53 lockres
->l_level
> LKM_NLMODE
) {
56 if (lockres
->l_level
== LKM_EXMODE
)
59 if (level
== old_level
)
63 * Converting an existing lock is not guaranteed to be
64 * atomic, so we can get away with simply unlocking
65 * here and allowing the lock code to try at the new
69 flock_lock_file_wait(file
,
70 &(struct file_lock
){.fl_type
= F_UNLCK
});
72 ocfs2_file_unlock(file
);
75 ret
= ocfs2_file_lock(file
, level
, trylock
);
77 if (ret
== -EAGAIN
&& trylock
)
84 ret
= flock_lock_file_wait(file
, fl
);
86 ocfs2_file_unlock(file
);
89 mutex_unlock(&fp
->fp_mutex
);
94 static int ocfs2_do_funlock(struct file
*file
, int cmd
, struct file_lock
*fl
)
97 struct ocfs2_file_private
*fp
= file
->private_data
;
99 mutex_lock(&fp
->fp_mutex
);
100 ocfs2_file_unlock(file
);
101 ret
= flock_lock_file_wait(file
, fl
);
102 mutex_unlock(&fp
->fp_mutex
);
108 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
110 int ocfs2_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
112 struct inode
*inode
= file
->f_mapping
->host
;
113 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
115 if (!(fl
->fl_flags
& FL_FLOCK
))
117 if (__mandatory_lock(inode
))
120 if ((osb
->s_mount_opt
& OCFS2_MOUNT_LOCALFLOCKS
) ||
121 ocfs2_mount_local(osb
))
122 return flock_lock_file_wait(file
, fl
);
124 if (fl
->fl_type
== F_UNLCK
)
125 return ocfs2_do_funlock(file
, cmd
, fl
);
127 return ocfs2_do_flock(file
, inode
, cmd
, fl
);
130 int ocfs2_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
132 struct inode
*inode
= file
->f_mapping
->host
;
133 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
135 if (!(fl
->fl_flags
& FL_POSIX
))
137 if (__mandatory_lock(inode
) && fl
->fl_type
!= F_UNLCK
)
140 return ocfs2_plock(osb
->cconn
, OCFS2_I(inode
)->ip_blkno
, file
, cmd
, fl
);