117 #include <linux/capability.h>
120 #include <linux/fs.h>
122 #include <linux/module.h>
124 #include <linux/slab.h>
126 #include <linux/time.h>
130 #include <asm/uaccess.h>
132 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
133 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
134 #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
153 #define for_each_lock(inode, lockp) \
154 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
165 spin_lock(&file_lock_lock);
171 spin_unlock(&file_lock_lock);
177 static void locks_init_lock_heads(
struct file_lock *fl)
190 locks_init_lock_heads(fl);
199 if (fl->
fl_ops->fl_release_private)
200 fl->
fl_ops->fl_release_private(fl);
223 locks_init_lock_heads(fl);
231 if (fl->
fl_ops->fl_copy_lock)
232 fl->
fl_ops->fl_copy_lock(
new, fl);
252 new->fl_lmops =
NULL;
265 locks_copy_private(
new, fl);
270 static inline int flock_translate_cmd(
int cmd) {
272 return cmd & (LOCK_MAND |
LOCK_RW);
285 static int flock_make_lock(
struct file *filp,
struct file_lock **lock,
289 int type = flock_translate_cmd(cmd);
307 static int assign_type(
struct file_lock *fl,
long type)
324 static int flock_to_posix_lock(
struct file *filp,
struct file_lock *fl,
337 start = i_size_read(filp->
f_path.dentry->d_inode);
350 end = start + l->
l_len - 1;
352 }
else if (l->
l_len < 0) {
370 return assign_type(fl, l->
l_type);
373 #if BITS_PER_LONG == 32
374 static int flock64_to_posix_lock(
struct file *filp,
struct file_lock *fl,
387 start = i_size_read(filp->
f_path.dentry->d_inode);
399 }
else if (l->
l_len < 0) {
416 return assign_type(fl, l->
l_type);
421 static void lease_break_callback(
struct file_lock *fl)
427 .lm_break = lease_break_callback,
434 static int lease_init(
struct file *filp,
long type,
struct file_lock *fl)
436 if (assign_type(fl, type) != 0)
452 static struct file_lock *lease_alloc(
struct file *filp,
long type)
458 return ERR_PTR(error);
460 error = lease_init(filp, type, fl);
463 return ERR_PTR(error);
483 fl1->
fl_lmops->lm_compare_owner(fl1, fl2);
493 list_del_init(&waiter->
fl_link);
502 __locks_delete_block(waiter);
512 static void locks_insert_block(
struct file_lock *blocker,
519 list_add(&waiter->
fl_link, &blocked_list);
526 static void locks_wake_up_blocks(
struct file_lock *blocker)
528 while (!list_empty(&blocker->
fl_block)) {
533 __locks_delete_block(waiter);
535 waiter->
fl_lmops->lm_notify(waiter);
546 list_add(&fl->
fl_link, &file_lock_list);
561 static void locks_delete_lock(
struct file_lock **thisfl_p)
574 locks_wake_up_blocks(fl);
593 static int posix_locks_conflict(
struct file_lock *caller_fl,
struct file_lock *sys_fl)
598 if (!
IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
602 if (!locks_overlap(caller_fl, sys_fl))
605 return (locks_conflict(caller_fl, sys_fl));
611 static int flock_locks_conflict(
struct file_lock *caller_fl,
struct file_lock *sys_fl)
618 if ((caller_fl->
fl_type & LOCK_MAND) || (sys_fl->
fl_type & LOCK_MAND))
621 return (locks_conflict(caller_fl, sys_fl));
630 for (cfl = filp->
f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->
fl_next) {
633 if (posix_locks_conflict(fl, cfl))
672 #define MAX_DEADLK_ITERATIONS 10
680 if (posix_same_owner(fl, block_fl))
686 static int posix_locks_deadlock(
struct file_lock *caller_fl,
691 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
694 if (posix_same_owner(caller_fl, block_fl))
736 locks_delete_lock(before);
763 if (!flock_locks_conflict(request, fl))
769 locks_insert_block(fl, request);
775 locks_insert_lock(before, new_fl);
786 static int __posix_lock_file(
struct inode *inode,
struct file_lock *request,
struct file_lock *conflock)
794 int error, added = 0;
815 if (!posix_locks_conflict(request, fl))
823 if (posix_locks_deadlock(request, fl))
826 locks_insert_block(fl, request);
843 while ((fl = *before) && (!
IS_POSIX(fl) ||
844 !posix_same_owner(request, fl))) {
849 while ((fl = *before) && posix_same_owner(request, fl)) {
879 locks_delete_lock(before);
909 locks_delete_lock(before);
917 locks_wake_up_blocks(fl);
922 locks_copy_private(fl, request);
940 if (right && left == right && !new_fl2)
956 locks_insert_lock(before, new_fl);
967 locks_insert_lock(before, left);
970 locks_wake_up_blocks(right);
974 locks_wake_up_blocks(left);
1005 return __posix_lock_file(filp->
f_path.dentry->d_inode, fl, conflock);
1092 fl.
fl_end = offset + count - 1;
1095 error = __posix_lock_file(inode, &fl,
NULL);
1104 if (__mandatory_lock(inode))
1117 static void lease_clear_pending(
struct file_lock *fl,
int arg)
1132 int error = assign_type(fl, arg);
1136 lease_clear_pending(fl, arg);
1137 locks_wake_up_blocks(fl);
1148 locks_delete_lock(before);
1155 static bool past_time(
unsigned long then)
1163 static void time_out_leases(
struct inode *inode)
1169 while ((fl = *before) &&
IS_LEASE(fl) && lease_breaking(fl)) {
1194 unsigned long break_time;
1195 int i_have_this_lease = 0;
1200 return PTR_ERR(new_fl);
1204 time_out_leases(inode);
1210 if (!locks_conflict(flock, new_fl))
1215 i_have_this_lease = 1;
1218 if (lease_break_time > 0) {
1219 break_time =
jiffies + lease_break_time *
HZ;
1220 if (break_time == 0)
1231 if (lease_breaking(flock))
1239 if (i_have_this_lease || (mode &
O_NONBLOCK)) {
1246 if (break_time != 0) {
1248 if (break_time == 0)
1251 locks_insert_block(flock, new_fl);
1254 !new_fl->
fl_next, break_time);
1256 __locks_delete_block(new_fl);
1259 time_out_leases(inode);
1264 for (flock = inode->
i_flock; flock && IS_LEASE(flock);
1266 if (locks_conflict(new_fl, flock))
1329 time_out_leases(filp->
f_path.dentry->d_inode);
1330 for (fl = filp->
f_path.dentry->d_inode->i_flock; fl &&
IS_LEASE(fl);
1333 type = target_leasetype(fl);
1345 struct inode *inode = dentry->
d_inode;
1367 for (before = &inode->
i_flock;
1388 if (my_before !=
NULL) {
1389 error = lease->fl_lmops->lm_change(my_before, arg);
1399 locks_insert_lock(before, lease);
1410 struct inode *inode = dentry->
d_inode;
1412 for (before = &inode->
i_flock;
1417 return (*flp)->fl_lmops->lm_change(before,
F_UNLCK);
1436 struct inode *inode = dentry->
d_inode;
1447 time_out_leases(inode);
1449 BUG_ON(!(*flp)->fl_lmops->lm_break);
1463 static int __vfs_setlease(
struct file *filp,
long arg,
struct file_lock **lease)
1465 if (filp->
f_op && filp->
f_op->setlease)
1466 return filp->
f_op->setlease(filp, arg, lease);
1503 error = __vfs_setlease(filp, arg, lease);
1510 static int do_fcntl_delete_lease(
struct file *filp)
1514 lease_init(filp,
F_UNLCK, flp);
1519 static int do_fcntl_add_lease(
unsigned int fd,
struct file *filp,
long arg)
1525 fl = lease_alloc(filp, arg);
1536 error = __vfs_setlease(filp, arg, &ret);
1540 goto out_free_fasync;
1576 return do_fcntl_delete_lease(filp);
1577 return do_fcntl_add_lease(fd, filp, arg);
1592 error = flock_lock_file(filp, fl);
1628 struct fd f = fdget(fd);
1630 int can_sleep, unlock;
1641 if (!unlock && !(cmd & LOCK_MAND) &&
1645 error = flock_make_lock(f.
file, &lock, cmd);
1655 if (f.
file->f_op && f.
file->f_op->flock)
1656 error = f.
file->f_op->flock(f.
file,
1681 if (filp->
f_op && filp->
f_op->lock)
1691 #if BITS_PER_LONG == 32
1709 #if BITS_PER_LONG == 32
1710 static void posix_lock_to_flock64(
struct flock64 *flock,
struct file_lock *fl)
1737 error = flock_to_posix_lock(filp, &file_lock, &flock);
1747 error = posix_lock_to_flock(&flock, &file_lock);
1793 if (filp->
f_op && filp->
f_op->lock)
1794 return filp->
f_op->lock(filp, cmd, fl);
1800 static int do_lock_file_wait(
struct file *filp,
unsigned int cmd,
1828 struct flock __user *l)
1832 struct inode *
inode;
1836 if (file_lock ==
NULL)
1846 inode = filp->
f_path.dentry->d_inode;
1851 if (mandatory_lock(inode) && mapping_writably_mapped(filp->
f_mapping)) {
1857 error = flock_to_posix_lock(filp, file_lock, &flock);
1881 error = do_lock_file_wait(filp, cmd, file_lock);
1892 spin_lock(&
current->files->file_lock);
1894 spin_unlock(&
current->files->file_lock);
1905 #if BITS_PER_LONG == 32
1909 int fcntl_getlk64(
struct file *filp,
struct flock64 __user *l)
1922 error = flock64_to_posix_lock(filp, &
file_lock, &flock);
1932 posix_lock_to_flock64(&flock, &
file_lock);
1945 int fcntl_setlk64(
unsigned int fd,
struct file *filp,
unsigned int cmd,
1950 struct inode *
inode;
1954 if (file_lock ==
NULL)
1964 inode = filp->
f_path.dentry->d_inode;
1969 if (mandatory_lock(inode) && mapping_writably_mapped(filp->
f_mapping)) {
1975 error = flock64_to_posix_lock(filp, file_lock, &flock);
1999 error = do_lock_file_wait(filp, cmd, file_lock);
2005 spin_lock(&
current->files->file_lock);
2007 spin_unlock(&
current->files->file_lock);
2026 struct file_lock lock;
2033 if (!filp->
f_path.dentry->d_inode->i_flock)
2049 lock.
fl_ops->fl_release_private(&lock);
2059 struct inode * inode = filp->
f_path.dentry->d_inode;
2060 struct file_lock *
fl;
2061 struct file_lock **before;
2066 if (filp->
f_op && filp->
f_op->flock) {
2067 struct file_lock fl = {
2076 fl.
fl_ops->fl_release_private(&fl);
2082 while ((fl = *before) !=
NULL) {
2085 locks_delete_lock(before);
2114 __locks_delete_block(waiter);
2132 if (filp->
f_op && filp->
f_op->lock)
2139 #ifdef CONFIG_PROC_FS
2143 static void lock_get_status(
struct seq_file *f,
struct file_lock *fl,
2144 loff_t
id,
char *pfx)
2146 struct inode *inode =
NULL;
2147 unsigned int fl_pid;
2155 inode = fl->
fl_file->f_path.dentry->d_inode;
2161 (inode ==
NULL) ?
"*NOINODE*" :
2162 mandatory_lock(inode) ?
"MANDATORY" :
"ADVISORY ");
2164 if (fl->
fl_type & LOCK_MAND) {
2171 if (lease_breaking(fl))
2180 if (fl->
fl_type & LOCK_MAND) {
2187 (lease_breaking(fl))
2192 #ifdef WE_CAN_BREAK_LSLK_NOW
2214 static int locks_show(
struct seq_file *f,
void *
v)
2216 struct file_lock *
fl, *bfl;
2220 lock_get_status(f, fl, *((loff_t *)f->
private),
"");
2223 lock_get_status(f, bfl, *((loff_t *)f->private), " ->");
2230 loff_t *
p = f->private;
2237 static void *locks_next(
struct seq_file *f,
void *v, loff_t *pos)
2244 static void locks_stop(
struct seq_file *f,
void *v)
2250 .
start = locks_start,
2256 static int locks_open(
struct inode *inode,
struct file *filp)
2268 static int __init proc_locks_init(
void)
2270 proc_create(
"locks", 0,
NULL, &proc_locks_operations);
2291 struct file_lock *
fl;
2301 if (!(fl->
fl_type & LOCK_MAND))
2331 struct file_lock *
fl;
2339 if (!(fl->
fl_type & LOCK_MAND))
2354 static int __init filelock_init(
void)