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