David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 1 | /* AFS file locking support |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 12 | #include "internal.h" |
| 13 | |
| 14 | #define AFS_LOCK_GRANTED 0 |
| 15 | #define AFS_LOCK_PENDING 1 |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 16 | #define AFS_LOCK_YOUR_TRY 2 |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 17 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 18 | struct workqueue_struct *afs_lock_manager; |
| 19 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 20 | static void afs_next_locker(struct afs_vnode *vnode, int error); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 21 | static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl); |
| 22 | static void afs_fl_release_private(struct file_lock *fl); |
| 23 | |
Alexey Dobriyan | 6aed628 | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 24 | static const struct file_lock_operations afs_lock_ops = { |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 25 | .fl_copy_lock = afs_fl_copy_lock, |
| 26 | .fl_release_private = afs_fl_release_private, |
| 27 | }; |
| 28 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 29 | static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state) |
| 30 | { |
| 31 | _debug("STATE %u -> %u", vnode->lock_state, state); |
| 32 | vnode->lock_state = state; |
| 33 | } |
| 34 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 35 | static atomic_t afs_file_lock_debug_id; |
| 36 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 37 | /* |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 38 | * if the callback is broken on this vnode, then the lock may now be available |
| 39 | */ |
| 40 | void afs_lock_may_be_available(struct afs_vnode *vnode) |
| 41 | { |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 42 | _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 43 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 44 | if (vnode->lock_state != AFS_VNODE_LOCK_WAITING_FOR_CB) |
| 45 | return; |
| 46 | |
| 47 | spin_lock(&vnode->lock); |
| 48 | if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB) |
| 49 | afs_next_locker(vnode, 0); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 50 | trace_afs_flock_ev(vnode, NULL, afs_flock_callback_break, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 51 | spin_unlock(&vnode->lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * the lock will time out in 5 minutes unless we extend it, so schedule |
| 56 | * extension in a bit less than that time |
| 57 | */ |
| 58 | static void afs_schedule_lock_extension(struct afs_vnode *vnode) |
| 59 | { |
David Howells | a690f60 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 60 | ktime_t expires_at, now, duration; |
| 61 | u64 duration_j; |
| 62 | |
| 63 | expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2); |
| 64 | now = ktime_get_real(); |
| 65 | duration = ktime_sub(expires_at, now); |
| 66 | if (duration <= 0) |
| 67 | duration_j = 0; |
| 68 | else |
| 69 | duration_j = nsecs_to_jiffies(ktime_to_ns(duration)); |
| 70 | |
| 71 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * In the case of successful completion of a lock operation, record the time |
| 76 | * the reply appeared and start the lock extension timer. |
| 77 | */ |
| 78 | void afs_lock_op_done(struct afs_call *call) |
| 79 | { |
| 80 | struct afs_vnode *vnode = call->reply[0]; |
| 81 | |
| 82 | if (call->error == 0) { |
| 83 | spin_lock(&vnode->lock); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 84 | trace_afs_flock_ev(vnode, NULL, afs_flock_timestamp, 0); |
David Howells | a690f60 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 85 | vnode->locked_at = call->reply_time; |
| 86 | afs_schedule_lock_extension(vnode); |
| 87 | spin_unlock(&vnode->lock); |
| 88 | } |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
David Howells | ff8e210 | 2007-07-31 00:38:49 -0700 | [diff] [blame] | 92 | * grant one or more locks (readlocks are allowed to jump the queue if the |
| 93 | * first lock in the queue is itself a readlock) |
| 94 | * - the caller must hold the vnode lock |
| 95 | */ |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 96 | static void afs_grant_locks(struct afs_vnode *vnode) |
David Howells | ff8e210 | 2007-07-31 00:38:49 -0700 | [diff] [blame] | 97 | { |
| 98 | struct file_lock *p, *_p; |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 99 | bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE); |
David Howells | ff8e210 | 2007-07-31 00:38:49 -0700 | [diff] [blame] | 100 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 101 | list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) { |
| 102 | if (!exclusive && p->fl_type == F_WRLCK) |
| 103 | continue; |
| 104 | |
| 105 | list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks); |
| 106 | p->fl_u.afs.state = AFS_LOCK_GRANTED; |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 107 | trace_afs_flock_op(vnode, p, afs_flock_op_grant); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 108 | wake_up(&p->fl_wait); |
David Howells | ff8e210 | 2007-07-31 00:38:49 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 113 | * If an error is specified, reject every pending lock that matches the |
| 114 | * authentication and type of the lock we failed to get. If there are any |
| 115 | * remaining lockers, try to wake up one of them to have a go. |
| 116 | */ |
| 117 | static void afs_next_locker(struct afs_vnode *vnode, int error) |
| 118 | { |
| 119 | struct file_lock *p, *_p, *next = NULL; |
| 120 | struct key *key = vnode->lock_key; |
| 121 | unsigned int fl_type = F_RDLCK; |
| 122 | |
| 123 | _enter(""); |
| 124 | |
| 125 | if (vnode->lock_type == AFS_LOCK_WRITE) |
| 126 | fl_type = F_WRLCK; |
| 127 | |
| 128 | list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) { |
| 129 | if (error && |
| 130 | p->fl_type == fl_type && |
| 131 | afs_file_key(p->fl_file) == key) { |
| 132 | list_del_init(&p->fl_u.afs.link); |
| 133 | p->fl_u.afs.state = error; |
| 134 | wake_up(&p->fl_wait); |
| 135 | } |
| 136 | |
| 137 | /* Select the next locker to hand off to. */ |
| 138 | if (next && |
| 139 | (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK)) |
| 140 | continue; |
| 141 | next = p; |
| 142 | } |
| 143 | |
| 144 | vnode->lock_key = NULL; |
| 145 | key_put(key); |
| 146 | |
| 147 | if (next) { |
| 148 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); |
| 149 | next->fl_u.afs.state = AFS_LOCK_YOUR_TRY; |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 150 | trace_afs_flock_op(vnode, next, afs_flock_op_wake); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 151 | wake_up(&next->fl_wait); |
| 152 | } else { |
| 153 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 154 | trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | _leave(""); |
| 158 | } |
| 159 | |
| 160 | /* |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 161 | * Kill off all waiters in the the pending lock queue due to the vnode being |
| 162 | * deleted. |
| 163 | */ |
| 164 | static void afs_kill_lockers_enoent(struct afs_vnode *vnode) |
| 165 | { |
| 166 | struct file_lock *p; |
| 167 | |
| 168 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_DELETED); |
| 169 | |
| 170 | while (!list_empty(&vnode->pending_locks)) { |
| 171 | p = list_entry(vnode->pending_locks.next, |
| 172 | struct file_lock, fl_u.afs.link); |
| 173 | list_del_init(&p->fl_u.afs.link); |
| 174 | p->fl_u.afs.state = -ENOENT; |
| 175 | wake_up(&p->fl_wait); |
| 176 | } |
| 177 | |
| 178 | key_put(vnode->lock_key); |
| 179 | vnode->lock_key = NULL; |
| 180 | } |
| 181 | |
| 182 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 183 | * Get a lock on a file |
| 184 | */ |
| 185 | static int afs_set_lock(struct afs_vnode *vnode, struct key *key, |
| 186 | afs_lock_type_t type) |
| 187 | { |
| 188 | struct afs_fs_cursor fc; |
| 189 | int ret; |
| 190 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 191 | _enter("%s{%llx:%llu.%u},%x,%u", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 192 | vnode->volume->name, |
| 193 | vnode->fid.vid, |
| 194 | vnode->fid.vnode, |
| 195 | vnode->fid.unique, |
| 196 | key_serial(key), type); |
| 197 | |
| 198 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame^] | 199 | if (afs_begin_vnode_operation(&fc, vnode, key, true)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 200 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 201 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 202 | afs_fs_set_lock(&fc, type); |
| 203 | } |
| 204 | |
| 205 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 206 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); |
| 207 | ret = afs_end_vnode_operation(&fc); |
| 208 | } |
| 209 | |
| 210 | _leave(" = %d", ret); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Extend a lock on a file |
| 216 | */ |
| 217 | static int afs_extend_lock(struct afs_vnode *vnode, struct key *key) |
| 218 | { |
| 219 | struct afs_fs_cursor fc; |
| 220 | int ret; |
| 221 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 222 | _enter("%s{%llx:%llu.%u},%x", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 223 | vnode->volume->name, |
| 224 | vnode->fid.vid, |
| 225 | vnode->fid.vnode, |
| 226 | vnode->fid.unique, |
| 227 | key_serial(key)); |
| 228 | |
| 229 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame^] | 230 | if (afs_begin_vnode_operation(&fc, vnode, key, false)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 231 | while (afs_select_current_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 232 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 233 | afs_fs_extend_lock(&fc); |
| 234 | } |
| 235 | |
| 236 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 237 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); |
| 238 | ret = afs_end_vnode_operation(&fc); |
| 239 | } |
| 240 | |
| 241 | _leave(" = %d", ret); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Release a lock on a file |
| 247 | */ |
| 248 | static int afs_release_lock(struct afs_vnode *vnode, struct key *key) |
| 249 | { |
| 250 | struct afs_fs_cursor fc; |
| 251 | int ret; |
| 252 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 253 | _enter("%s{%llx:%llu.%u},%x", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 254 | vnode->volume->name, |
| 255 | vnode->fid.vid, |
| 256 | vnode->fid.vnode, |
| 257 | vnode->fid.unique, |
| 258 | key_serial(key)); |
| 259 | |
| 260 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame^] | 261 | if (afs_begin_vnode_operation(&fc, vnode, key, false)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 262 | while (afs_select_current_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 263 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 264 | afs_fs_release_lock(&fc); |
| 265 | } |
| 266 | |
| 267 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 268 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); |
| 269 | ret = afs_end_vnode_operation(&fc); |
| 270 | } |
| 271 | |
| 272 | _leave(" = %d", ret); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | /* |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 277 | * do work for a lock, including: |
| 278 | * - probing for a lock we're waiting on but didn't get immediately |
| 279 | * - extending a lock that's close to timing out |
| 280 | */ |
| 281 | void afs_lock_work(struct work_struct *work) |
| 282 | { |
| 283 | struct afs_vnode *vnode = |
| 284 | container_of(work, struct afs_vnode, lock_work.work); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 285 | struct key *key; |
| 286 | int ret; |
| 287 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 288 | _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 289 | |
| 290 | spin_lock(&vnode->lock); |
| 291 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 292 | again: |
| 293 | _debug("wstate %u for %p", vnode->lock_state, vnode); |
| 294 | switch (vnode->lock_state) { |
| 295 | case AFS_VNODE_LOCK_NEED_UNLOCK: |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 296 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 297 | trace_afs_flock_ev(vnode, NULL, afs_flock_work_unlocking, 0); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 298 | spin_unlock(&vnode->lock); |
| 299 | |
| 300 | /* attempt to release the server lock; if it fails, we just |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 301 | * wait 5 minutes and it'll expire anyway */ |
| 302 | ret = afs_release_lock(vnode, vnode->lock_key); |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 303 | if (ret < 0 && vnode->lock_state != AFS_VNODE_LOCK_DELETED) { |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 304 | trace_afs_flock_ev(vnode, NULL, afs_flock_release_fail, |
| 305 | ret); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 306 | printk(KERN_WARNING "AFS:" |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 307 | " Failed to release lock on {%llx:%llx} error %d\n", |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 308 | vnode->fid.vid, vnode->fid.vnode, ret); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 309 | } |
| 310 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 311 | spin_lock(&vnode->lock); |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 312 | if (ret == -ENOENT) |
| 313 | afs_kill_lockers_enoent(vnode); |
| 314 | else |
| 315 | afs_next_locker(vnode, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 316 | spin_unlock(&vnode->lock); |
| 317 | return; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 318 | |
| 319 | /* If we've already got a lock, then it must be time to extend that |
| 320 | * lock as AFS locks time out after 5 minutes. |
| 321 | */ |
| 322 | case AFS_VNODE_LOCK_GRANTED: |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 323 | _debug("extend"); |
| 324 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 325 | ASSERT(!list_empty(&vnode->granted_locks)); |
| 326 | |
| 327 | key = key_get(vnode->lock_key); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 328 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 329 | trace_afs_flock_ev(vnode, NULL, afs_flock_work_extending, 0); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 330 | spin_unlock(&vnode->lock); |
| 331 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 332 | ret = afs_extend_lock(vnode, key); /* RPC */ |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 333 | key_put(key); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 334 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 335 | if (ret < 0) { |
| 336 | trace_afs_flock_ev(vnode, NULL, afs_flock_extend_fail, |
| 337 | ret); |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 338 | pr_warning("AFS: Failed to extend lock on {%llx:%llx} error %d\n", |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 339 | vnode->fid.vid, vnode->fid.vnode, ret); |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 340 | } |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 341 | |
| 342 | spin_lock(&vnode->lock); |
| 343 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 344 | if (ret == -ENOENT) { |
| 345 | afs_kill_lockers_enoent(vnode); |
| 346 | spin_unlock(&vnode->lock); |
| 347 | return; |
| 348 | } |
| 349 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 350 | if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING) |
| 351 | goto again; |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 352 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 353 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 354 | if (ret != 0) |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 355 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, |
| 356 | HZ * 10); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 357 | spin_unlock(&vnode->lock); |
| 358 | _leave(" [ext]"); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 359 | return; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 360 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 361 | /* If we're waiting for a callback to indicate lock release, we can't |
| 362 | * actually rely on this, so need to recheck at regular intervals. The |
| 363 | * problem is that the server might not notify us if the lock just |
| 364 | * expires (say because a client died) rather than being explicitly |
| 365 | * released. |
| 366 | */ |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 367 | case AFS_VNODE_LOCK_WAITING_FOR_CB: |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 368 | _debug("retry"); |
| 369 | afs_next_locker(vnode, 0); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 370 | spin_unlock(&vnode->lock); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 371 | return; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 372 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 373 | case AFS_VNODE_LOCK_DELETED: |
| 374 | afs_kill_lockers_enoent(vnode); |
| 375 | spin_unlock(&vnode->lock); |
| 376 | return; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 377 | |
Gustavo A. R. Silva | e690c9e | 2019-01-10 15:52:25 -0600 | [diff] [blame] | 378 | /* Fall through */ |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 379 | default: |
| 380 | /* Looks like a lock request was withdrawn. */ |
| 381 | spin_unlock(&vnode->lock); |
| 382 | _leave(" [no]"); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * pass responsibility for the unlocking of a vnode on the server to the |
| 389 | * manager thread, lest a pending signal in the calling thread interrupt |
| 390 | * AF_RXRPC |
| 391 | * - the caller must hold the vnode lock |
| 392 | */ |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 393 | static void afs_defer_unlock(struct afs_vnode *vnode) |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 394 | { |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 395 | _enter("%u", vnode->lock_state); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 396 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 397 | if (list_empty(&vnode->granted_locks) && |
| 398 | (vnode->lock_state == AFS_VNODE_LOCK_GRANTED || |
| 399 | vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 400 | cancel_delayed_work(&vnode->lock_work); |
| 401 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 402 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 403 | trace_afs_flock_ev(vnode, NULL, afs_flock_defer_unlock, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 404 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Check that our view of the file metadata is up to date and check to see |
| 410 | * whether we think that we have a locking permit. |
| 411 | */ |
| 412 | static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key, |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 413 | enum afs_flock_mode mode, afs_lock_type_t type) |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 414 | { |
| 415 | afs_access_t access; |
| 416 | int ret; |
| 417 | |
| 418 | /* Make sure we've got a callback on this file and that our view of the |
| 419 | * data version is up to date. |
| 420 | */ |
| 421 | ret = afs_validate(vnode, key); |
| 422 | if (ret < 0) |
| 423 | return ret; |
| 424 | |
| 425 | /* Check the permission set to see if we're actually going to be |
| 426 | * allowed to get a lock on this file. |
| 427 | */ |
| 428 | ret = afs_check_permit(vnode, key, &access); |
| 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
| 432 | /* At a rough estimation, you need LOCK, WRITE or INSERT perm to |
| 433 | * read-lock a file and WRITE or INSERT perm to write-lock a file. |
| 434 | * |
| 435 | * We can't rely on the server to do this for us since if we want to |
| 436 | * share a read lock that we already have, we won't go the server. |
| 437 | */ |
| 438 | if (type == AFS_LOCK_READ) { |
| 439 | if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK))) |
| 440 | return -EACCES; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 441 | } else { |
| 442 | if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE))) |
| 443 | return -EACCES; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 450 | * request a lock on a file on the server |
| 451 | */ |
| 452 | static int afs_do_setlk(struct file *file, struct file_lock *fl) |
| 453 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 454 | struct inode *inode = locks_inode(file); |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 455 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 456 | enum afs_flock_mode mode = AFS_FS_S(inode->i_sb)->flock_mode; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 457 | afs_lock_type_t type; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 458 | struct key *key = afs_file_key(file); |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 459 | bool partial, no_server_lock = false; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 460 | int ret; |
| 461 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 462 | if (mode == afs_flock_mode_unset) |
| 463 | mode = afs_flock_mode_openafs; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 464 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 465 | _enter("{%llx:%llu},%llu-%llu,%u,%u", |
| 466 | vnode->fid.vid, vnode->fid.vnode, |
| 467 | fl->fl_start, fl->fl_end, fl->fl_type, mode); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 468 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 469 | fl->fl_ops = &afs_lock_ops; |
| 470 | INIT_LIST_HEAD(&fl->fl_u.afs.link); |
| 471 | fl->fl_u.afs.state = AFS_LOCK_PENDING; |
| 472 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 473 | partial = (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 474 | type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE; |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 475 | if (mode == afs_flock_mode_write && partial) |
| 476 | type = AFS_LOCK_WRITE; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 477 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 478 | ret = afs_do_setlk_check(vnode, key, mode, type); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 479 | if (ret < 0) |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 480 | return ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 481 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 482 | trace_afs_flock_op(vnode, fl, afs_flock_op_set_lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 483 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 484 | /* AFS3 protocol only supports full-file locks and doesn't provide any |
| 485 | * method of upgrade/downgrade, so we need to emulate for partial-file |
| 486 | * locks. |
| 487 | * |
| 488 | * The OpenAFS client only gets a server lock for a full-file lock and |
| 489 | * keeps partial-file locks local. Allow this behaviour to be emulated |
| 490 | * (as the default). |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 491 | */ |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 492 | if (mode == afs_flock_mode_local || |
| 493 | (partial && mode == afs_flock_mode_openafs)) { |
| 494 | no_server_lock = true; |
| 495 | goto skip_server_lock; |
David Howells | ff8e210 | 2007-07-31 00:38:49 -0700 | [diff] [blame] | 496 | } |
| 497 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 498 | spin_lock(&vnode->lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 499 | list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 500 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 501 | ret = -ENOENT; |
| 502 | if (vnode->lock_state == AFS_VNODE_LOCK_DELETED) |
| 503 | goto error_unlock; |
| 504 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 505 | /* If we've already got a lock on the server then try to move to having |
| 506 | * the VFS grant the requested lock. Note that this means that other |
| 507 | * clients may get starved out. |
| 508 | */ |
| 509 | _debug("try %u", vnode->lock_state); |
| 510 | if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) { |
| 511 | if (type == AFS_LOCK_READ) { |
| 512 | _debug("instant readlock"); |
| 513 | list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks); |
| 514 | fl->fl_u.afs.state = AFS_LOCK_GRANTED; |
| 515 | goto vnode_is_locked_u; |
| 516 | } |
| 517 | |
| 518 | if (vnode->lock_type == AFS_LOCK_WRITE) { |
| 519 | _debug("instant writelock"); |
| 520 | list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks); |
| 521 | fl->fl_u.afs.state = AFS_LOCK_GRANTED; |
| 522 | goto vnode_is_locked_u; |
| 523 | } |
| 524 | } |
| 525 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 526 | if (vnode->lock_state == AFS_VNODE_LOCK_NONE && |
| 527 | !(fl->fl_flags & FL_SLEEP)) { |
| 528 | ret = -EAGAIN; |
| 529 | if (type == AFS_LOCK_READ) { |
| 530 | if (vnode->status.lock_count == -1) |
| 531 | goto lock_is_contended; /* Write locked */ |
| 532 | } else { |
| 533 | if (vnode->status.lock_count != 0) |
| 534 | goto lock_is_contended; /* Locked */ |
| 535 | } |
| 536 | } |
| 537 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 538 | if (vnode->lock_state != AFS_VNODE_LOCK_NONE) |
| 539 | goto need_to_wait; |
| 540 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 541 | try_to_lock: |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 542 | /* We don't have a lock on this vnode and we aren't currently waiting |
| 543 | * for one either, so ask the server for a lock. |
| 544 | * |
| 545 | * Note that we need to be careful if we get interrupted by a signal |
| 546 | * after dispatching the request as we may still get the lock, even |
| 547 | * though we don't wait for the reply (it's not too bad a problem - the |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 548 | * lock will expire in 5 mins anyway). |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 549 | */ |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 550 | trace_afs_flock_ev(vnode, fl, afs_flock_try_to_lock, 0); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 551 | vnode->lock_key = key_get(key); |
| 552 | vnode->lock_type = type; |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 553 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 554 | spin_unlock(&vnode->lock); |
| 555 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 556 | ret = afs_set_lock(vnode, key, type); /* RPC */ |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 557 | |
| 558 | spin_lock(&vnode->lock); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 559 | switch (ret) { |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 560 | case -EKEYREJECTED: |
| 561 | case -EKEYEXPIRED: |
| 562 | case -EKEYREVOKED: |
| 563 | case -EPERM: |
| 564 | case -EACCES: |
| 565 | fl->fl_u.afs.state = ret; |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 566 | trace_afs_flock_ev(vnode, fl, afs_flock_fail_perm, ret); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 567 | list_del_init(&fl->fl_u.afs.link); |
| 568 | afs_next_locker(vnode, ret); |
| 569 | goto error_unlock; |
| 570 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 571 | case -ENOENT: |
| 572 | fl->fl_u.afs.state = ret; |
| 573 | trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret); |
| 574 | list_del_init(&fl->fl_u.afs.link); |
| 575 | afs_kill_lockers_enoent(vnode); |
| 576 | goto error_unlock; |
| 577 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 578 | default: |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 579 | fl->fl_u.afs.state = ret; |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 580 | trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 581 | list_del_init(&fl->fl_u.afs.link); |
| 582 | afs_next_locker(vnode, 0); |
| 583 | goto error_unlock; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 584 | |
| 585 | case -EWOULDBLOCK: |
| 586 | /* The server doesn't have a lock-waiting queue, so the client |
| 587 | * will have to retry. The server will break the outstanding |
| 588 | * callbacks on a file when a lock is released. |
| 589 | */ |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 590 | ASSERT(list_empty(&vnode->granted_locks)); |
| 591 | ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 592 | goto lock_is_contended; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 593 | |
| 594 | case 0: |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 595 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 596 | trace_afs_flock_ev(vnode, fl, afs_flock_acquired, type); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 597 | afs_grant_locks(vnode); |
| 598 | goto vnode_is_locked_u; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 599 | } |
| 600 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 601 | vnode_is_locked_u: |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 602 | spin_unlock(&vnode->lock); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 603 | vnode_is_locked: |
| 604 | /* the lock has been granted by the server... */ |
| 605 | ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 606 | |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 607 | skip_server_lock: |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 608 | /* ... but the VFS still needs to distribute access on this client. */ |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 609 | trace_afs_flock_ev(vnode, fl, afs_flock_vfs_locking, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 610 | ret = locks_lock_file_wait(file, fl); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 611 | trace_afs_flock_ev(vnode, fl, afs_flock_vfs_lock, ret); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 612 | if (ret < 0) |
| 613 | goto vfs_rejected_lock; |
| 614 | |
| 615 | /* Again, make sure we've got a callback on this file and, again, make |
| 616 | * sure that our view of the data version is up to date (we ignore |
| 617 | * errors incurred here and deal with the consequences elsewhere). |
| 618 | */ |
| 619 | afs_validate(vnode, key); |
| 620 | _leave(" = 0"); |
| 621 | return 0; |
| 622 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 623 | lock_is_contended: |
| 624 | if (!(fl->fl_flags & FL_SLEEP)) { |
| 625 | list_del_init(&fl->fl_u.afs.link); |
| 626 | afs_next_locker(vnode, 0); |
| 627 | ret = -EAGAIN; |
| 628 | goto error_unlock; |
| 629 | } |
| 630 | |
| 631 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 632 | trace_afs_flock_ev(vnode, fl, afs_flock_would_block, ret); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 633 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5); |
| 634 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 635 | need_to_wait: |
| 636 | /* We're going to have to wait. Either this client doesn't have a lock |
| 637 | * on the server yet and we need to wait for a callback to occur, or |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 638 | * the client does have a lock on the server, but it's shared and we |
| 639 | * need an exclusive lock. |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 640 | */ |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 641 | spin_unlock(&vnode->lock); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 642 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 643 | trace_afs_flock_ev(vnode, fl, afs_flock_waiting, 0); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 644 | ret = wait_event_interruptible(fl->fl_wait, |
| 645 | fl->fl_u.afs.state != AFS_LOCK_PENDING); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 646 | trace_afs_flock_ev(vnode, fl, afs_flock_waited, ret); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 647 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 648 | if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 649 | spin_lock(&vnode->lock); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 650 | |
| 651 | switch (fl->fl_u.afs.state) { |
| 652 | case AFS_LOCK_YOUR_TRY: |
| 653 | fl->fl_u.afs.state = AFS_LOCK_PENDING; |
| 654 | goto try_to_lock; |
| 655 | case AFS_LOCK_PENDING: |
| 656 | if (ret > 0) { |
| 657 | /* We need to retry the lock. We may not be |
| 658 | * notified by the server if it just expired |
| 659 | * rather than being released. |
| 660 | */ |
| 661 | ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB); |
| 662 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); |
| 663 | fl->fl_u.afs.state = AFS_LOCK_PENDING; |
| 664 | goto try_to_lock; |
| 665 | } |
| 666 | goto error_unlock; |
| 667 | case AFS_LOCK_GRANTED: |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | spin_unlock(&vnode->lock); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | if (fl->fl_u.afs.state == AFS_LOCK_GRANTED) |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 676 | goto vnode_is_locked; |
| 677 | ret = fl->fl_u.afs.state; |
| 678 | goto error; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 679 | |
| 680 | vfs_rejected_lock: |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 681 | /* The VFS rejected the lock we just obtained, so we have to discard |
| 682 | * what we just got. We defer this to the lock manager work item to |
| 683 | * deal with. |
| 684 | */ |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 685 | _debug("vfs refused %d", ret); |
David Howells | 6c6c1d6 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 686 | if (no_server_lock) |
| 687 | goto error; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 688 | spin_lock(&vnode->lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 689 | list_del_init(&fl->fl_u.afs.link); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 690 | afs_defer_unlock(vnode); |
| 691 | |
| 692 | error_unlock: |
| 693 | spin_unlock(&vnode->lock); |
| 694 | error: |
| 695 | _leave(" = %d", ret); |
| 696 | return ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* |
| 700 | * unlock on a file on the server |
| 701 | */ |
| 702 | static int afs_do_unlk(struct file *file, struct file_lock *fl) |
| 703 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 704 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 705 | int ret; |
| 706 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 707 | _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 708 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 709 | trace_afs_flock_op(vnode, fl, afs_flock_op_unlock); |
| 710 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 711 | /* Flush all pending writes before doing anything with locks. */ |
| 712 | vfs_fsync(file, 0); |
| 713 | |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 714 | ret = locks_lock_file_wait(file, fl); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 715 | _leave(" = %d [%u]", ret, vnode->lock_state); |
| 716 | return ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* |
| 720 | * return information about a lock we currently hold, if indeed we hold one |
| 721 | */ |
| 722 | static int afs_do_getlk(struct file *file, struct file_lock *fl) |
| 723 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 724 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 725 | struct key *key = afs_file_key(file); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 726 | int ret, lock_count; |
| 727 | |
| 728 | _enter(""); |
| 729 | |
David Howells | cdfb26b | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 730 | if (vnode->lock_state == AFS_VNODE_LOCK_DELETED) |
| 731 | return -ENOENT; |
| 732 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 733 | fl->fl_type = F_UNLCK; |
| 734 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 735 | /* check local lock records first */ |
Andrew Morton | 275afca | 2007-07-19 01:50:35 -0700 | [diff] [blame] | 736 | posix_test_lock(file, fl); |
| 737 | if (fl->fl_type == F_UNLCK) { |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 738 | /* no local locks; consult the server */ |
David Howells | 0c3a5ac | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 739 | ret = afs_fetch_status(vnode, key, false); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 740 | if (ret < 0) |
| 741 | goto error; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 742 | |
| 743 | lock_count = READ_ONCE(vnode->status.lock_count); |
David Howells | 68ce801 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 744 | if (lock_count != 0) { |
| 745 | if (lock_count > 0) |
| 746 | fl->fl_type = F_RDLCK; |
| 747 | else |
| 748 | fl->fl_type = F_WRLCK; |
| 749 | fl->fl_start = 0; |
| 750 | fl->fl_end = OFFSET_MAX; |
| 751 | fl->fl_pid = 0; |
| 752 | } |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 753 | } |
| 754 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 755 | ret = 0; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 756 | error: |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 757 | _leave(" = %d [%hd]", ret, fl->fl_type); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * manage POSIX locks on a file |
| 763 | */ |
| 764 | int afs_lock(struct file *file, int cmd, struct file_lock *fl) |
| 765 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 766 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 767 | enum afs_flock_operation op; |
| 768 | int ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 769 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 770 | _enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}", |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 771 | vnode->fid.vid, vnode->fid.vnode, cmd, |
| 772 | fl->fl_type, fl->fl_flags, |
| 773 | (long long) fl->fl_start, (long long) fl->fl_end); |
| 774 | |
| 775 | /* AFS doesn't support mandatory locks */ |
Pavel Emelyanov | fc5846e | 2007-10-01 14:41:14 -0700 | [diff] [blame] | 776 | if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK) |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 777 | return -ENOLCK; |
| 778 | |
| 779 | if (IS_GETLK(cmd)) |
| 780 | return afs_do_getlk(file, fl); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 781 | |
| 782 | fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id); |
| 783 | trace_afs_flock_op(vnode, fl, afs_flock_op_lock); |
| 784 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 785 | if (fl->fl_type == F_UNLCK) |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 786 | ret = afs_do_unlk(file, fl); |
| 787 | else |
| 788 | ret = afs_do_setlk(file, fl); |
| 789 | |
| 790 | switch (ret) { |
| 791 | case 0: op = afs_flock_op_return_ok; break; |
| 792 | case -EAGAIN: op = afs_flock_op_return_eagain; break; |
| 793 | case -EDEADLK: op = afs_flock_op_return_edeadlk; break; |
| 794 | default: op = afs_flock_op_return_error; break; |
| 795 | } |
| 796 | trace_afs_flock_op(vnode, fl, op); |
| 797 | return ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* |
| 801 | * manage FLOCK locks on a file |
| 802 | */ |
| 803 | int afs_flock(struct file *file, int cmd, struct file_lock *fl) |
| 804 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 805 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 806 | enum afs_flock_operation op; |
| 807 | int ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 808 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 809 | _enter("{%llx:%llu},%d,{t=%x,fl=%x}", |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 810 | vnode->fid.vid, vnode->fid.vnode, cmd, |
| 811 | fl->fl_type, fl->fl_flags); |
| 812 | |
| 813 | /* |
| 814 | * No BSD flocks over NFS allowed. |
| 815 | * Note: we could try to fake a POSIX lock request here by |
| 816 | * using ((u32) filp | 0x80000000) or some such as the pid. |
| 817 | * Not sure whether that would be unique, though, or whether |
| 818 | * that would break in other places. |
| 819 | */ |
| 820 | if (!(fl->fl_flags & FL_FLOCK)) |
| 821 | return -ENOLCK; |
| 822 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 823 | fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id); |
| 824 | trace_afs_flock_op(vnode, fl, afs_flock_op_flock); |
| 825 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 826 | /* we're simulating flock() locks using posix locks on the server */ |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 827 | if (fl->fl_type == F_UNLCK) |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 828 | ret = afs_do_unlk(file, fl); |
| 829 | else |
| 830 | ret = afs_do_setlk(file, fl); |
| 831 | |
| 832 | switch (ret) { |
| 833 | case 0: op = afs_flock_op_return_ok; break; |
| 834 | case -EAGAIN: op = afs_flock_op_return_eagain; break; |
| 835 | case -EDEADLK: op = afs_flock_op_return_edeadlk; break; |
| 836 | default: op = afs_flock_op_return_error; break; |
| 837 | } |
| 838 | trace_afs_flock_op(vnode, fl, op); |
| 839 | return ret; |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * the POSIX lock management core VFS code copies the lock record and adds the |
| 844 | * copy into its own list, so we need to add that copy to the vnode's lock |
| 845 | * queue in the same place as the original (which will be deleted shortly |
| 846 | * after) |
| 847 | */ |
| 848 | static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl) |
| 849 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 850 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file)); |
| 851 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 852 | _enter(""); |
| 853 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 854 | new->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id); |
| 855 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 856 | spin_lock(&vnode->lock); |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 857 | trace_afs_flock_op(vnode, new, afs_flock_op_copy_lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 858 | list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 859 | spin_unlock(&vnode->lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* |
| 863 | * need to remove this lock from the vnode queue when it's removed from the |
| 864 | * VFS's list |
| 865 | */ |
| 866 | static void afs_fl_release_private(struct file_lock *fl) |
| 867 | { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 868 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file)); |
| 869 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 870 | _enter(""); |
| 871 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 872 | spin_lock(&vnode->lock); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 873 | |
David Howells | d469660 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 874 | trace_afs_flock_op(vnode, fl, afs_flock_op_release_lock); |
David Howells | 4be5975 | 2019-04-25 14:26:50 +0100 | [diff] [blame] | 875 | list_del_init(&fl->fl_u.afs.link); |
| 876 | if (list_empty(&vnode->granted_locks)) |
| 877 | afs_defer_unlock(vnode); |
| 878 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 879 | _debug("state %u for %p", vnode->lock_state, vnode); |
| 880 | spin_unlock(&vnode->lock); |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 881 | } |