diff options
Diffstat (limited to 'fs/ocfs2/locks.c')
| -rw-r--r-- | fs/ocfs2/locks.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/fs/ocfs2/locks.c b/fs/ocfs2/locks.c new file mode 100644 index 000000000000..203f87143877 --- /dev/null +++ b/fs/ocfs2/locks.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* -*- mode: c; c-basic-offset: 8; -*- | ||
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: | ||
| 3 | * | ||
| 4 | * locks.c | ||
| 5 | * | ||
| 6 | * Userspace file locking support | ||
| 7 | * | ||
| 8 | * Copyright (C) 2007 Oracle. All rights reserved. | ||
| 9 | * | ||
| 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. | ||
| 14 | * | ||
| 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. | ||
| 19 | * | ||
| 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. | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <linux/fs.h> | ||
| 27 | |||
| 28 | #define MLOG_MASK_PREFIX ML_INODE | ||
| 29 | #include <cluster/masklog.h> | ||
| 30 | |||
| 31 | #include "ocfs2.h" | ||
| 32 | |||
| 33 | #include "dlmglue.h" | ||
| 34 | #include "file.h" | ||
| 35 | #include "locks.h" | ||
| 36 | |||
| 37 | static int ocfs2_do_flock(struct file *file, struct inode *inode, | ||
| 38 | int cmd, struct file_lock *fl) | ||
| 39 | { | ||
| 40 | int ret = 0, level = 0, trylock = 0; | ||
| 41 | struct ocfs2_file_private *fp = file->private_data; | ||
| 42 | struct ocfs2_lock_res *lockres = &fp->fp_flock; | ||
| 43 | |||
| 44 | if (fl->fl_type == F_WRLCK) | ||
| 45 | level = 1; | ||
| 46 | if (!IS_SETLKW(cmd)) | ||
| 47 | trylock = 1; | ||
| 48 | |||
| 49 | mutex_lock(&fp->fp_mutex); | ||
| 50 | |||
| 51 | if (lockres->l_flags & OCFS2_LOCK_ATTACHED && | ||
| 52 | lockres->l_level > LKM_NLMODE) { | ||
| 53 | int old_level = 0; | ||
| 54 | |||
| 55 | if (lockres->l_level == LKM_EXMODE) | ||
| 56 | old_level = 1; | ||
| 57 | |||
| 58 | if (level == old_level) | ||
| 59 | goto out; | ||
| 60 | |||
| 61 | /* | ||
| 62 | * Converting an existing lock is not guaranteed to be | ||
| 63 | * atomic, so we can get away with simply unlocking | ||
| 64 | * here and allowing the lock code to try at the new | ||
| 65 | * level. | ||
| 66 | */ | ||
| 67 | |||
| 68 | flock_lock_file_wait(file, | ||
| 69 | &(struct file_lock){.fl_type = F_UNLCK}); | ||
| 70 | |||
| 71 | ocfs2_file_unlock(file); | ||
| 72 | } | ||
| 73 | |||
| 74 | ret = ocfs2_file_lock(file, level, trylock); | ||
| 75 | if (ret) { | ||
| 76 | if (ret == -EAGAIN && trylock) | ||
| 77 | ret = -EWOULDBLOCK; | ||
| 78 | else | ||
| 79 | mlog_errno(ret); | ||
| 80 | goto out; | ||
| 81 | } | ||
| 82 | |||
| 83 | ret = flock_lock_file_wait(file, fl); | ||
| 84 | |||
| 85 | out: | ||
| 86 | mutex_unlock(&fp->fp_mutex); | ||
| 87 | |||
| 88 | return ret; | ||
| 89 | } | ||
| 90 | |||
| 91 | static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl) | ||
| 92 | { | ||
| 93 | int ret; | ||
| 94 | struct ocfs2_file_private *fp = file->private_data; | ||
| 95 | |||
| 96 | mutex_lock(&fp->fp_mutex); | ||
| 97 | ocfs2_file_unlock(file); | ||
| 98 | ret = flock_lock_file_wait(file, fl); | ||
| 99 | mutex_unlock(&fp->fp_mutex); | ||
| 100 | |||
| 101 | return ret; | ||
| 102 | } | ||
| 103 | |||
| 104 | /* | ||
| 105 | * Overall flow of ocfs2_flock() was influenced by gfs2_flock(). | ||
| 106 | */ | ||
| 107 | int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl) | ||
| 108 | { | ||
| 109 | struct inode *inode = file->f_mapping->host; | ||
| 110 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
| 111 | |||
| 112 | if (!(fl->fl_flags & FL_FLOCK)) | ||
| 113 | return -ENOLCK; | ||
| 114 | if (__mandatory_lock(inode)) | ||
| 115 | return -ENOLCK; | ||
| 116 | |||
| 117 | if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || | ||
| 118 | ocfs2_mount_local(osb)) | ||
| 119 | return flock_lock_file_wait(file, fl); | ||
| 120 | |||
| 121 | if (fl->fl_type == F_UNLCK) | ||
| 122 | return ocfs2_do_funlock(file, cmd, fl); | ||
| 123 | else | ||
| 124 | return ocfs2_do_flock(file, inode, cmd, fl); | ||
| 125 | } | ||
