blob: c4210a3964d8b3df6b5ed38feb2270e5387d95ad [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellse8d6c552007-07-15 23:40:12 -07002/* AFS file locking support
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellse8d6c552007-07-15 23:40:12 -07006 */
7
David Howellse8d6c552007-07-15 23:40:12 -07008#include "internal.h"
9
10#define AFS_LOCK_GRANTED 0
11#define AFS_LOCK_PENDING 1
David Howells4be59752019-04-25 14:26:50 +010012#define AFS_LOCK_YOUR_TRY 2
David Howellse8d6c552007-07-15 23:40:12 -070013
David Howellsf044c882017-11-02 15:27:45 +000014struct workqueue_struct *afs_lock_manager;
15
David Howells4be59752019-04-25 14:26:50 +010016static void afs_next_locker(struct afs_vnode *vnode, int error);
David Howellse8d6c552007-07-15 23:40:12 -070017static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
18static void afs_fl_release_private(struct file_lock *fl);
19
Alexey Dobriyan6aed6282009-09-21 17:01:11 -070020static const struct file_lock_operations afs_lock_ops = {
David Howellse8d6c552007-07-15 23:40:12 -070021 .fl_copy_lock = afs_fl_copy_lock,
22 .fl_release_private = afs_fl_release_private,
23};
24
David Howells4be59752019-04-25 14:26:50 +010025static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state)
26{
27 _debug("STATE %u -> %u", vnode->lock_state, state);
28 vnode->lock_state = state;
29}
30
David Howellsd4696602019-04-25 14:26:50 +010031static atomic_t afs_file_lock_debug_id;
32
David Howellse8d6c552007-07-15 23:40:12 -070033/*
David Howellse8d6c552007-07-15 23:40:12 -070034 * if the callback is broken on this vnode, then the lock may now be available
35 */
36void afs_lock_may_be_available(struct afs_vnode *vnode)
37{
David Howells3b6492d2018-10-20 00:57:57 +010038 _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
David Howellse8d6c552007-07-15 23:40:12 -070039
David Howells4be59752019-04-25 14:26:50 +010040 spin_lock(&vnode->lock);
41 if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
42 afs_next_locker(vnode, 0);
David Howellsd4696602019-04-25 14:26:50 +010043 trace_afs_flock_ev(vnode, NULL, afs_flock_callback_break, 0);
David Howells4be59752019-04-25 14:26:50 +010044 spin_unlock(&vnode->lock);
David Howellse8d6c552007-07-15 23:40:12 -070045}
46
47/*
48 * the lock will time out in 5 minutes unless we extend it, so schedule
49 * extension in a bit less than that time
50 */
51static void afs_schedule_lock_extension(struct afs_vnode *vnode)
52{
David Howellsa690f602019-04-25 14:26:50 +010053 ktime_t expires_at, now, duration;
54 u64 duration_j;
55
56 expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2);
57 now = ktime_get_real();
58 duration = ktime_sub(expires_at, now);
59 if (duration <= 0)
60 duration_j = 0;
61 else
62 duration_j = nsecs_to_jiffies(ktime_to_ns(duration));
63
64 queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j);
65}
66
67/*
68 * In the case of successful completion of a lock operation, record the time
69 * the reply appeared and start the lock extension timer.
70 */
71void afs_lock_op_done(struct afs_call *call)
72{
David Howellse49c7b22020-04-10 20:51:51 +010073 struct afs_operation *op = call->op;
David Howells5749ce92020-06-04 21:31:39 +010074 struct afs_vnode *vnode = op->file[0].vnode;
David Howellsa690f602019-04-25 14:26:50 +010075
76 if (call->error == 0) {
77 spin_lock(&vnode->lock);
David Howellsd4696602019-04-25 14:26:50 +010078 trace_afs_flock_ev(vnode, NULL, afs_flock_timestamp, 0);
David Howellsa690f602019-04-25 14:26:50 +010079 vnode->locked_at = call->reply_time;
80 afs_schedule_lock_extension(vnode);
81 spin_unlock(&vnode->lock);
82 }
David Howellse8d6c552007-07-15 23:40:12 -070083}
84
85/*
David Howellsff8e2102007-07-31 00:38:49 -070086 * grant one or more locks (readlocks are allowed to jump the queue if the
87 * first lock in the queue is itself a readlock)
88 * - the caller must hold the vnode lock
89 */
David Howells4be59752019-04-25 14:26:50 +010090static void afs_grant_locks(struct afs_vnode *vnode)
David Howellsff8e2102007-07-31 00:38:49 -070091{
92 struct file_lock *p, *_p;
David Howells4be59752019-04-25 14:26:50 +010093 bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
David Howellsff8e2102007-07-31 00:38:49 -070094
David Howells4be59752019-04-25 14:26:50 +010095 list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
96 if (!exclusive && p->fl_type == F_WRLCK)
97 continue;
98
99 list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
100 p->fl_u.afs.state = AFS_LOCK_GRANTED;
David Howellsd4696602019-04-25 14:26:50 +0100101 trace_afs_flock_op(vnode, p, afs_flock_op_grant);
David Howells4be59752019-04-25 14:26:50 +0100102 wake_up(&p->fl_wait);
David Howellsff8e2102007-07-31 00:38:49 -0700103 }
104}
105
106/*
David Howells4be59752019-04-25 14:26:50 +0100107 * If an error is specified, reject every pending lock that matches the
108 * authentication and type of the lock we failed to get. If there are any
109 * remaining lockers, try to wake up one of them to have a go.
110 */
111static void afs_next_locker(struct afs_vnode *vnode, int error)
112{
113 struct file_lock *p, *_p, *next = NULL;
114 struct key *key = vnode->lock_key;
115 unsigned int fl_type = F_RDLCK;
116
117 _enter("");
118
119 if (vnode->lock_type == AFS_LOCK_WRITE)
120 fl_type = F_WRLCK;
121
122 list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
123 if (error &&
124 p->fl_type == fl_type &&
125 afs_file_key(p->fl_file) == key) {
126 list_del_init(&p->fl_u.afs.link);
127 p->fl_u.afs.state = error;
128 wake_up(&p->fl_wait);
129 }
130
131 /* Select the next locker to hand off to. */
132 if (next &&
133 (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
134 continue;
135 next = p;
136 }
137
138 vnode->lock_key = NULL;
139 key_put(key);
140
141 if (next) {
142 afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
143 next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
David Howellsd4696602019-04-25 14:26:50 +0100144 trace_afs_flock_op(vnode, next, afs_flock_op_wake);
David Howells4be59752019-04-25 14:26:50 +0100145 wake_up(&next->fl_wait);
146 } else {
147 afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
David Howellsd4696602019-04-25 14:26:50 +0100148 trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0);
David Howells4be59752019-04-25 14:26:50 +0100149 }
150
151 _leave("");
152}
153
154/*
David Howellscdfb26b2019-04-25 14:26:51 +0100155 * Kill off all waiters in the the pending lock queue due to the vnode being
156 * deleted.
157 */
158static void afs_kill_lockers_enoent(struct afs_vnode *vnode)
159{
160 struct file_lock *p;
161
162 afs_set_lock_state(vnode, AFS_VNODE_LOCK_DELETED);
163
164 while (!list_empty(&vnode->pending_locks)) {
165 p = list_entry(vnode->pending_locks.next,
166 struct file_lock, fl_u.afs.link);
167 list_del_init(&p->fl_u.afs.link);
168 p->fl_u.afs.state = -ENOENT;
169 wake_up(&p->fl_wait);
170 }
171
172 key_put(vnode->lock_key);
173 vnode->lock_key = NULL;
174}
175
David Howellse49c7b22020-04-10 20:51:51 +0100176static void afs_lock_success(struct afs_operation *op)
177{
David Howellse49c7b22020-04-10 20:51:51 +0100178 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100179 afs_vnode_commit_status(op, &op->file[0]);
180}
181
182static const struct afs_operation_ops afs_set_lock_operation = {
183 .issue_afs_rpc = afs_fs_set_lock,
184 .issue_yfs_rpc = yfs_fs_set_lock,
185 .success = afs_lock_success,
David Howells728279a2020-06-16 00:34:09 +0100186 .aborted = afs_check_for_remote_deletion,
David Howellse49c7b22020-04-10 20:51:51 +0100187};
188
David Howellscdfb26b2019-04-25 14:26:51 +0100189/*
David Howellsd2ddc772017-11-02 15:27:50 +0000190 * Get a lock on a file
191 */
192static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
193 afs_lock_type_t type)
194{
David Howellse49c7b22020-04-10 20:51:51 +0100195 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000196
David Howells3b6492d2018-10-20 00:57:57 +0100197 _enter("%s{%llx:%llu.%u},%x,%u",
David Howellsd2ddc772017-11-02 15:27:50 +0000198 vnode->volume->name,
199 vnode->fid.vid,
200 vnode->fid.vnode,
201 vnode->fid.unique,
202 key_serial(key), type);
203
David Howellse49c7b22020-04-10 20:51:51 +0100204 op = afs_alloc_operation(key, vnode->volume);
205 if (IS_ERR(op))
206 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100207
David Howellse49c7b22020-04-10 20:51:51 +0100208 afs_op_set_vnode(op, 0, vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000209
David Howellse49c7b22020-04-10 20:51:51 +0100210 op->lock.type = type;
211 op->ops = &afs_set_lock_operation;
212 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000213}
214
David Howellse49c7b22020-04-10 20:51:51 +0100215static const struct afs_operation_ops afs_extend_lock_operation = {
216 .issue_afs_rpc = afs_fs_extend_lock,
217 .issue_yfs_rpc = yfs_fs_extend_lock,
218 .success = afs_lock_success,
219};
220
David Howellsd2ddc772017-11-02 15:27:50 +0000221/*
222 * Extend a lock on a file
223 */
224static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
225{
David Howellse49c7b22020-04-10 20:51:51 +0100226 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000227
David Howells3b6492d2018-10-20 00:57:57 +0100228 _enter("%s{%llx:%llu.%u},%x",
David Howellsd2ddc772017-11-02 15:27:50 +0000229 vnode->volume->name,
230 vnode->fid.vid,
231 vnode->fid.vnode,
232 vnode->fid.unique,
233 key_serial(key));
234
David Howellse49c7b22020-04-10 20:51:51 +0100235 op = afs_alloc_operation(key, vnode->volume);
236 if (IS_ERR(op))
237 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100238
David Howellse49c7b22020-04-10 20:51:51 +0100239 afs_op_set_vnode(op, 0, vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000240
David Howellse49c7b22020-04-10 20:51:51 +0100241 op->flags |= AFS_OPERATION_UNINTR;
242 op->ops = &afs_extend_lock_operation;
243 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000244}
245
David Howellse49c7b22020-04-10 20:51:51 +0100246static const struct afs_operation_ops afs_release_lock_operation = {
247 .issue_afs_rpc = afs_fs_release_lock,
248 .issue_yfs_rpc = yfs_fs_release_lock,
249 .success = afs_lock_success,
250};
251
David Howellsd2ddc772017-11-02 15:27:50 +0000252/*
253 * Release a lock on a file
254 */
255static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
256{
David Howellse49c7b22020-04-10 20:51:51 +0100257 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000258
David Howells3b6492d2018-10-20 00:57:57 +0100259 _enter("%s{%llx:%llu.%u},%x",
David Howellsd2ddc772017-11-02 15:27:50 +0000260 vnode->volume->name,
261 vnode->fid.vid,
262 vnode->fid.vnode,
263 vnode->fid.unique,
264 key_serial(key));
265
David Howellse49c7b22020-04-10 20:51:51 +0100266 op = afs_alloc_operation(key, vnode->volume);
267 if (IS_ERR(op))
268 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100269
David Howellse49c7b22020-04-10 20:51:51 +0100270 afs_op_set_vnode(op, 0, vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000271
David Howellse49c7b22020-04-10 20:51:51 +0100272 op->flags |= AFS_OPERATION_UNINTR;
273 op->ops = &afs_release_lock_operation;
274 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000275}
276
277/*
David Howellse8d6c552007-07-15 23:40:12 -0700278 * do work for a lock, including:
279 * - probing for a lock we're waiting on but didn't get immediately
280 * - extending a lock that's close to timing out
281 */
282void afs_lock_work(struct work_struct *work)
283{
284 struct afs_vnode *vnode =
285 container_of(work, struct afs_vnode, lock_work.work);
David Howellse8d6c552007-07-15 23:40:12 -0700286 struct key *key;
287 int ret;
288
David Howells3b6492d2018-10-20 00:57:57 +0100289 _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
David Howellse8d6c552007-07-15 23:40:12 -0700290
291 spin_lock(&vnode->lock);
292
David Howells0fafdc92017-11-13 16:59:50 +0000293again:
294 _debug("wstate %u for %p", vnode->lock_state, vnode);
295 switch (vnode->lock_state) {
296 case AFS_VNODE_LOCK_NEED_UNLOCK:
David Howells4be59752019-04-25 14:26:50 +0100297 afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING);
David Howellsd4696602019-04-25 14:26:50 +0100298 trace_afs_flock_ev(vnode, NULL, afs_flock_work_unlocking, 0);
David Howellse8d6c552007-07-15 23:40:12 -0700299 spin_unlock(&vnode->lock);
300
301 /* attempt to release the server lock; if it fails, we just
David Howells0fafdc92017-11-13 16:59:50 +0000302 * wait 5 minutes and it'll expire anyway */
303 ret = afs_release_lock(vnode, vnode->lock_key);
David Howells79ddbfa2019-04-25 14:26:51 +0100304 if (ret < 0 && vnode->lock_state != AFS_VNODE_LOCK_DELETED) {
David Howellscdfb26b2019-04-25 14:26:51 +0100305 trace_afs_flock_ev(vnode, NULL, afs_flock_release_fail,
306 ret);
David Howellse8d6c552007-07-15 23:40:12 -0700307 printk(KERN_WARNING "AFS:"
David Howells3b6492d2018-10-20 00:57:57 +0100308 " Failed to release lock on {%llx:%llx} error %d\n",
David Howellse8d6c552007-07-15 23:40:12 -0700309 vnode->fid.vid, vnode->fid.vnode, ret);
David Howells0fafdc92017-11-13 16:59:50 +0000310 }
311
David Howellse8d6c552007-07-15 23:40:12 -0700312 spin_lock(&vnode->lock);
David Howellscdfb26b2019-04-25 14:26:51 +0100313 if (ret == -ENOENT)
314 afs_kill_lockers_enoent(vnode);
315 else
316 afs_next_locker(vnode, 0);
David Howells4be59752019-04-25 14:26:50 +0100317 spin_unlock(&vnode->lock);
318 return;
David Howells0fafdc92017-11-13 16:59:50 +0000319
320 /* If we've already got a lock, then it must be time to extend that
321 * lock as AFS locks time out after 5 minutes.
322 */
323 case AFS_VNODE_LOCK_GRANTED:
David Howellse8d6c552007-07-15 23:40:12 -0700324 _debug("extend");
325
David Howells0fafdc92017-11-13 16:59:50 +0000326 ASSERT(!list_empty(&vnode->granted_locks));
327
328 key = key_get(vnode->lock_key);
David Howells4be59752019-04-25 14:26:50 +0100329 afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING);
David Howellsd4696602019-04-25 14:26:50 +0100330 trace_afs_flock_ev(vnode, NULL, afs_flock_work_extending, 0);
David Howellse8d6c552007-07-15 23:40:12 -0700331 spin_unlock(&vnode->lock);
332
David Howells0fafdc92017-11-13 16:59:50 +0000333 ret = afs_extend_lock(vnode, key); /* RPC */
David Howellse8d6c552007-07-15 23:40:12 -0700334 key_put(key);
David Howells0fafdc92017-11-13 16:59:50 +0000335
David Howellscdfb26b2019-04-25 14:26:51 +0100336 if (ret < 0) {
337 trace_afs_flock_ev(vnode, NULL, afs_flock_extend_fail,
338 ret);
Kefeng Wanga4e530a2019-10-18 11:18:40 +0800339 pr_warn("AFS: Failed to extend lock on {%llx:%llx} error %d\n",
340 vnode->fid.vid, vnode->fid.vnode, ret);
David Howellscdfb26b2019-04-25 14:26:51 +0100341 }
David Howells0fafdc92017-11-13 16:59:50 +0000342
343 spin_lock(&vnode->lock);
344
David Howellscdfb26b2019-04-25 14:26:51 +0100345 if (ret == -ENOENT) {
346 afs_kill_lockers_enoent(vnode);
347 spin_unlock(&vnode->lock);
348 return;
349 }
350
David Howells0fafdc92017-11-13 16:59:50 +0000351 if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
352 goto again;
David Howells4be59752019-04-25 14:26:50 +0100353 afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
David Howells0fafdc92017-11-13 16:59:50 +0000354
David Howells4be59752019-04-25 14:26:50 +0100355 if (ret != 0)
David Howellse8d6c552007-07-15 23:40:12 -0700356 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
357 HZ * 10);
David Howells0fafdc92017-11-13 16:59:50 +0000358 spin_unlock(&vnode->lock);
359 _leave(" [ext]");
David Howellse8d6c552007-07-15 23:40:12 -0700360 return;
David Howellse8d6c552007-07-15 23:40:12 -0700361
David Howells4be59752019-04-25 14:26:50 +0100362 /* If we're waiting for a callback to indicate lock release, we can't
363 * actually rely on this, so need to recheck at regular intervals. The
364 * problem is that the server might not notify us if the lock just
365 * expires (say because a client died) rather than being explicitly
366 * released.
367 */
David Howells0fafdc92017-11-13 16:59:50 +0000368 case AFS_VNODE_LOCK_WAITING_FOR_CB:
David Howells4be59752019-04-25 14:26:50 +0100369 _debug("retry");
370 afs_next_locker(vnode, 0);
David Howellse8d6c552007-07-15 23:40:12 -0700371 spin_unlock(&vnode->lock);
David Howells4be59752019-04-25 14:26:50 +0100372 return;
David Howellse8d6c552007-07-15 23:40:12 -0700373
David Howellscdfb26b2019-04-25 14:26:51 +0100374 case AFS_VNODE_LOCK_DELETED:
375 afs_kill_lockers_enoent(vnode);
376 spin_unlock(&vnode->lock);
377 return;
David Howells0fafdc92017-11-13 16:59:50 +0000378
379 default:
380 /* Looks like a lock request was withdrawn. */
381 spin_unlock(&vnode->lock);
382 _leave(" [no]");
David Howellse8d6c552007-07-15 23:40:12 -0700383 return;
384 }
David Howellse8d6c552007-07-15 23:40:12 -0700385}
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 Howells0fafdc92017-11-13 16:59:50 +0000393static void afs_defer_unlock(struct afs_vnode *vnode)
David Howellse8d6c552007-07-15 23:40:12 -0700394{
David Howells4be59752019-04-25 14:26:50 +0100395 _enter("%u", vnode->lock_state);
David Howells0fafdc92017-11-13 16:59:50 +0000396
David Howells4be59752019-04-25 14:26:50 +0100397 if (list_empty(&vnode->granted_locks) &&
398 (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
399 vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) {
David Howells0fafdc92017-11-13 16:59:50 +0000400 cancel_delayed_work(&vnode->lock_work);
401
David Howells4be59752019-04-25 14:26:50 +0100402 afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK);
David Howellsd4696602019-04-25 14:26:50 +0100403 trace_afs_flock_ev(vnode, NULL, afs_flock_defer_unlock, 0);
David Howells4be59752019-04-25 14:26:50 +0100404 queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
David Howells0fafdc92017-11-13 16:59:50 +0000405 }
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 */
412static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
David Howells6c6c1d62019-04-25 14:26:52 +0100413 enum afs_flock_mode mode, afs_lock_type_t type)
David Howells0fafdc92017-11-13 16:59:50 +0000414{
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 Howells0fafdc92017-11-13 16:59:50 +0000441 } else {
442 if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
443 return -EACCES;
David Howells0fafdc92017-11-13 16:59:50 +0000444 }
445
446 return 0;
447}
448
449/*
David Howellse8d6c552007-07-15 23:40:12 -0700450 * request a lock on a file on the server
451 */
452static int afs_do_setlk(struct file *file, struct file_lock *fl)
453{
David Howells0fafdc92017-11-13 16:59:50 +0000454 struct inode *inode = locks_inode(file);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400455 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells6c6c1d62019-04-25 14:26:52 +0100456 enum afs_flock_mode mode = AFS_FS_S(inode->i_sb)->flock_mode;
David Howellse8d6c552007-07-15 23:40:12 -0700457 afs_lock_type_t type;
David Howells215804a2017-11-02 15:27:52 +0000458 struct key *key = afs_file_key(file);
David Howells6c6c1d62019-04-25 14:26:52 +0100459 bool partial, no_server_lock = false;
David Howellse8d6c552007-07-15 23:40:12 -0700460 int ret;
461
David Howells6c6c1d62019-04-25 14:26:52 +0100462 if (mode == afs_flock_mode_unset)
463 mode = afs_flock_mode_openafs;
David Howellse8d6c552007-07-15 23:40:12 -0700464
David Howells6c6c1d62019-04-25 14:26:52 +0100465 _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 Howellse8d6c552007-07-15 23:40:12 -0700468
David Howellse8d6c552007-07-15 23:40:12 -0700469 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 Howells6c6c1d62019-04-25 14:26:52 +0100473 partial = (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX);
David Howellse8d6c552007-07-15 23:40:12 -0700474 type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
David Howells6c6c1d62019-04-25 14:26:52 +0100475 if (mode == afs_flock_mode_write && partial)
476 type = AFS_LOCK_WRITE;
David Howellse8d6c552007-07-15 23:40:12 -0700477
David Howells6c6c1d62019-04-25 14:26:52 +0100478 ret = afs_do_setlk_check(vnode, key, mode, type);
David Howellse8d6c552007-07-15 23:40:12 -0700479 if (ret < 0)
David Howells0fafdc92017-11-13 16:59:50 +0000480 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700481
David Howellsd4696602019-04-25 14:26:50 +0100482 trace_afs_flock_op(vnode, fl, afs_flock_op_set_lock);
David Howellse8d6c552007-07-15 23:40:12 -0700483
David Howells6c6c1d62019-04-25 14:26:52 +0100484 /* 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 Howells0fafdc92017-11-13 16:59:50 +0000491 */
David Howells6c6c1d62019-04-25 14:26:52 +0100492 if (mode == afs_flock_mode_local ||
493 (partial && mode == afs_flock_mode_openafs)) {
494 no_server_lock = true;
495 goto skip_server_lock;
David Howellsff8e2102007-07-31 00:38:49 -0700496 }
497
David Howellse8d6c552007-07-15 23:40:12 -0700498 spin_lock(&vnode->lock);
David Howellse8d6c552007-07-15 23:40:12 -0700499 list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
David Howells0fafdc92017-11-13 16:59:50 +0000500
David Howellscdfb26b2019-04-25 14:26:51 +0100501 ret = -ENOENT;
502 if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
503 goto error_unlock;
504
David Howells4be59752019-04-25 14:26:50 +0100505 /* 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 Howells6c6c1d62019-04-25 14:26:52 +0100526 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 Howells0fafdc92017-11-13 16:59:50 +0000538 if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
539 goto need_to_wait;
540
David Howells4be59752019-04-25 14:26:50 +0100541try_to_lock:
David Howells0fafdc92017-11-13 16:59:50 +0000542 /* 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 Howells4be59752019-04-25 14:26:50 +0100548 * lock will expire in 5 mins anyway).
David Howells0fafdc92017-11-13 16:59:50 +0000549 */
David Howellsd4696602019-04-25 14:26:50 +0100550 trace_afs_flock_ev(vnode, fl, afs_flock_try_to_lock, 0);
David Howells0fafdc92017-11-13 16:59:50 +0000551 vnode->lock_key = key_get(key);
552 vnode->lock_type = type;
David Howells4be59752019-04-25 14:26:50 +0100553 afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
David Howellse8d6c552007-07-15 23:40:12 -0700554 spin_unlock(&vnode->lock);
555
David Howells0fafdc92017-11-13 16:59:50 +0000556 ret = afs_set_lock(vnode, key, type); /* RPC */
David Howellse8d6c552007-07-15 23:40:12 -0700557
558 spin_lock(&vnode->lock);
David Howells0fafdc92017-11-13 16:59:50 +0000559 switch (ret) {
David Howells4be59752019-04-25 14:26:50 +0100560 case -EKEYREJECTED:
561 case -EKEYEXPIRED:
562 case -EKEYREVOKED:
563 case -EPERM:
564 case -EACCES:
565 fl->fl_u.afs.state = ret;
David Howellsd4696602019-04-25 14:26:50 +0100566 trace_afs_flock_ev(vnode, fl, afs_flock_fail_perm, ret);
David Howells4be59752019-04-25 14:26:50 +0100567 list_del_init(&fl->fl_u.afs.link);
568 afs_next_locker(vnode, ret);
569 goto error_unlock;
570
David Howellscdfb26b2019-04-25 14:26:51 +0100571 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 Howells0fafdc92017-11-13 16:59:50 +0000578 default:
David Howells4be59752019-04-25 14:26:50 +0100579 fl->fl_u.afs.state = ret;
David Howellsd4696602019-04-25 14:26:50 +0100580 trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret);
David Howells4be59752019-04-25 14:26:50 +0100581 list_del_init(&fl->fl_u.afs.link);
582 afs_next_locker(vnode, 0);
583 goto error_unlock;
David Howells0fafdc92017-11-13 16:59:50 +0000584
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 Howells0fafdc92017-11-13 16:59:50 +0000590 ASSERT(list_empty(&vnode->granted_locks));
591 ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
David Howells4be59752019-04-25 14:26:50 +0100592 goto lock_is_contended;
David Howells0fafdc92017-11-13 16:59:50 +0000593
594 case 0:
David Howells4be59752019-04-25 14:26:50 +0100595 afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
David Howellsd4696602019-04-25 14:26:50 +0100596 trace_afs_flock_ev(vnode, fl, afs_flock_acquired, type);
David Howells4be59752019-04-25 14:26:50 +0100597 afs_grant_locks(vnode);
598 goto vnode_is_locked_u;
David Howellse8d6c552007-07-15 23:40:12 -0700599 }
600
David Howells4be59752019-04-25 14:26:50 +0100601vnode_is_locked_u:
David Howells0fafdc92017-11-13 16:59:50 +0000602 spin_unlock(&vnode->lock);
David Howells4be59752019-04-25 14:26:50 +0100603vnode_is_locked:
604 /* the lock has been granted by the server... */
605 ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED);
David Howells0fafdc92017-11-13 16:59:50 +0000606
David Howells6c6c1d62019-04-25 14:26:52 +0100607skip_server_lock:
David Howells4be59752019-04-25 14:26:50 +0100608 /* ... but the VFS still needs to distribute access on this client. */
David Howellsd4696602019-04-25 14:26:50 +0100609 trace_afs_flock_ev(vnode, fl, afs_flock_vfs_locking, 0);
David Howells4be59752019-04-25 14:26:50 +0100610 ret = locks_lock_file_wait(file, fl);
David Howellsd4696602019-04-25 14:26:50 +0100611 trace_afs_flock_ev(vnode, fl, afs_flock_vfs_lock, ret);
David Howells0fafdc92017-11-13 16:59:50 +0000612 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 Howells4be59752019-04-25 14:26:50 +0100623lock_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 Howellsd4696602019-04-25 14:26:50 +0100632 trace_afs_flock_ev(vnode, fl, afs_flock_would_block, ret);
David Howells4be59752019-04-25 14:26:50 +0100633 queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5);
634
David Howells0fafdc92017-11-13 16:59:50 +0000635need_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 Howells4be59752019-04-25 14:26:50 +0100638 * the client does have a lock on the server, but it's shared and we
639 * need an exclusive lock.
David Howells0fafdc92017-11-13 16:59:50 +0000640 */
David Howells4be59752019-04-25 14:26:50 +0100641 spin_unlock(&vnode->lock);
David Howells0fafdc92017-11-13 16:59:50 +0000642
David Howellsd4696602019-04-25 14:26:50 +0100643 trace_afs_flock_ev(vnode, fl, afs_flock_waiting, 0);
David Howells4be59752019-04-25 14:26:50 +0100644 ret = wait_event_interruptible(fl->fl_wait,
645 fl->fl_u.afs.state != AFS_LOCK_PENDING);
David Howellsd4696602019-04-25 14:26:50 +0100646 trace_afs_flock_ev(vnode, fl, afs_flock_waited, ret);
David Howells0fafdc92017-11-13 16:59:50 +0000647
David Howells4be59752019-04-25 14:26:50 +0100648 if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) {
David Howells0fafdc92017-11-13 16:59:50 +0000649 spin_lock(&vnode->lock);
David Howells4be59752019-04-25 14:26:50 +0100650
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 Howells0fafdc92017-11-13 16:59:50 +0000673 }
674
675 if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
David Howells4be59752019-04-25 14:26:50 +0100676 goto vnode_is_locked;
677 ret = fl->fl_u.afs.state;
678 goto error;
David Howellse8d6c552007-07-15 23:40:12 -0700679
680vfs_rejected_lock:
David Howells0fafdc92017-11-13 16:59:50 +0000681 /* 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 Howellse8d6c552007-07-15 23:40:12 -0700685 _debug("vfs refused %d", ret);
David Howells6c6c1d62019-04-25 14:26:52 +0100686 if (no_server_lock)
687 goto error;
David Howells0fafdc92017-11-13 16:59:50 +0000688 spin_lock(&vnode->lock);
David Howellse8d6c552007-07-15 23:40:12 -0700689 list_del_init(&fl->fl_u.afs.link);
David Howells4be59752019-04-25 14:26:50 +0100690 afs_defer_unlock(vnode);
691
692error_unlock:
693 spin_unlock(&vnode->lock);
694error:
695 _leave(" = %d", ret);
696 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700697}
698
699/*
700 * unlock on a file on the server
701 */
702static int afs_do_unlk(struct file *file, struct file_lock *fl)
703{
David Howells0fafdc92017-11-13 16:59:50 +0000704 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
David Howellse8d6c552007-07-15 23:40:12 -0700705 int ret;
706
David Howells3b6492d2018-10-20 00:57:57 +0100707 _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
David Howellse8d6c552007-07-15 23:40:12 -0700708
David Howellsd4696602019-04-25 14:26:50 +0100709 trace_afs_flock_op(vnode, fl, afs_flock_op_unlock);
710
David Howells0fafdc92017-11-13 16:59:50 +0000711 /* Flush all pending writes before doing anything with locks. */
712 vfs_fsync(file, 0);
713
David Howells4be59752019-04-25 14:26:50 +0100714 ret = locks_lock_file_wait(file, fl);
David Howells0fafdc92017-11-13 16:59:50 +0000715 _leave(" = %d [%u]", ret, vnode->lock_state);
716 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700717}
718
719/*
720 * return information about a lock we currently hold, if indeed we hold one
721 */
722static int afs_do_getlk(struct file *file, struct file_lock *fl)
723{
David Howells0fafdc92017-11-13 16:59:50 +0000724 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
David Howells215804a2017-11-02 15:27:52 +0000725 struct key *key = afs_file_key(file);
David Howellse8d6c552007-07-15 23:40:12 -0700726 int ret, lock_count;
727
728 _enter("");
729
David Howellscdfb26b2019-04-25 14:26:51 +0100730 if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
731 return -ENOENT;
732
David Howellse8d6c552007-07-15 23:40:12 -0700733 fl->fl_type = F_UNLCK;
734
David Howellse8d6c552007-07-15 23:40:12 -0700735 /* check local lock records first */
Andrew Morton275afca2007-07-19 01:50:35 -0700736 posix_test_lock(file, fl);
737 if (fl->fl_type == F_UNLCK) {
David Howellse8d6c552007-07-15 23:40:12 -0700738 /* no local locks; consult the server */
David Howellsa58823a2019-05-09 15:16:10 +0100739 ret = afs_fetch_status(vnode, key, false, NULL);
David Howellse8d6c552007-07-15 23:40:12 -0700740 if (ret < 0)
741 goto error;
David Howells0fafdc92017-11-13 16:59:50 +0000742
743 lock_count = READ_ONCE(vnode->status.lock_count);
David Howells68ce8012019-04-25 14:26:50 +0100744 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 Howellse8d6c552007-07-15 23:40:12 -0700753 }
754
David Howells0fafdc92017-11-13 16:59:50 +0000755 ret = 0;
David Howellse8d6c552007-07-15 23:40:12 -0700756error:
David Howellse8d6c552007-07-15 23:40:12 -0700757 _leave(" = %d [%hd]", ret, fl->fl_type);
758 return ret;
759}
760
761/*
762 * manage POSIX locks on a file
763 */
764int afs_lock(struct file *file, int cmd, struct file_lock *fl)
765{
David Howells0fafdc92017-11-13 16:59:50 +0000766 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
David Howellsd4696602019-04-25 14:26:50 +0100767 enum afs_flock_operation op;
768 int ret;
David Howellse8d6c552007-07-15 23:40:12 -0700769
David Howells3b6492d2018-10-20 00:57:57 +0100770 _enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
David Howellse8d6c552007-07-15 23:40:12 -0700771 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
David Howellse8d6c552007-07-15 23:40:12 -0700775 if (IS_GETLK(cmd))
776 return afs_do_getlk(file, fl);
David Howellsd4696602019-04-25 14:26:50 +0100777
778 fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
779 trace_afs_flock_op(vnode, fl, afs_flock_op_lock);
780
David Howellse8d6c552007-07-15 23:40:12 -0700781 if (fl->fl_type == F_UNLCK)
David Howellsd4696602019-04-25 14:26:50 +0100782 ret = afs_do_unlk(file, fl);
783 else
784 ret = afs_do_setlk(file, fl);
785
786 switch (ret) {
787 case 0: op = afs_flock_op_return_ok; break;
788 case -EAGAIN: op = afs_flock_op_return_eagain; break;
789 case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
790 default: op = afs_flock_op_return_error; break;
791 }
792 trace_afs_flock_op(vnode, fl, op);
793 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700794}
795
796/*
797 * manage FLOCK locks on a file
798 */
799int afs_flock(struct file *file, int cmd, struct file_lock *fl)
800{
David Howells0fafdc92017-11-13 16:59:50 +0000801 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
David Howellsd4696602019-04-25 14:26:50 +0100802 enum afs_flock_operation op;
803 int ret;
David Howellse8d6c552007-07-15 23:40:12 -0700804
David Howells3b6492d2018-10-20 00:57:57 +0100805 _enter("{%llx:%llu},%d,{t=%x,fl=%x}",
David Howellse8d6c552007-07-15 23:40:12 -0700806 vnode->fid.vid, vnode->fid.vnode, cmd,
807 fl->fl_type, fl->fl_flags);
808
809 /*
810 * No BSD flocks over NFS allowed.
811 * Note: we could try to fake a POSIX lock request here by
812 * using ((u32) filp | 0x80000000) or some such as the pid.
813 * Not sure whether that would be unique, though, or whether
814 * that would break in other places.
815 */
816 if (!(fl->fl_flags & FL_FLOCK))
817 return -ENOLCK;
818
David Howellsd4696602019-04-25 14:26:50 +0100819 fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
820 trace_afs_flock_op(vnode, fl, afs_flock_op_flock);
821
David Howellse8d6c552007-07-15 23:40:12 -0700822 /* we're simulating flock() locks using posix locks on the server */
David Howellse8d6c552007-07-15 23:40:12 -0700823 if (fl->fl_type == F_UNLCK)
David Howellsd4696602019-04-25 14:26:50 +0100824 ret = afs_do_unlk(file, fl);
825 else
826 ret = afs_do_setlk(file, fl);
827
828 switch (ret) {
829 case 0: op = afs_flock_op_return_ok; break;
830 case -EAGAIN: op = afs_flock_op_return_eagain; break;
831 case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
832 default: op = afs_flock_op_return_error; break;
833 }
834 trace_afs_flock_op(vnode, fl, op);
835 return ret;
David Howellse8d6c552007-07-15 23:40:12 -0700836}
837
838/*
839 * the POSIX lock management core VFS code copies the lock record and adds the
840 * copy into its own list, so we need to add that copy to the vnode's lock
841 * queue in the same place as the original (which will be deleted shortly
842 * after)
843 */
844static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
845{
David Howells0fafdc92017-11-13 16:59:50 +0000846 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
847
David Howellse8d6c552007-07-15 23:40:12 -0700848 _enter("");
849
David Howellsd4696602019-04-25 14:26:50 +0100850 new->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
851
David Howells0fafdc92017-11-13 16:59:50 +0000852 spin_lock(&vnode->lock);
David Howellsd4696602019-04-25 14:26:50 +0100853 trace_afs_flock_op(vnode, new, afs_flock_op_copy_lock);
David Howellse8d6c552007-07-15 23:40:12 -0700854 list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
David Howells0fafdc92017-11-13 16:59:50 +0000855 spin_unlock(&vnode->lock);
David Howellse8d6c552007-07-15 23:40:12 -0700856}
857
858/*
859 * need to remove this lock from the vnode queue when it's removed from the
860 * VFS's list
861 */
862static void afs_fl_release_private(struct file_lock *fl)
863{
David Howells0fafdc92017-11-13 16:59:50 +0000864 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
865
David Howellse8d6c552007-07-15 23:40:12 -0700866 _enter("");
867
David Howells0fafdc92017-11-13 16:59:50 +0000868 spin_lock(&vnode->lock);
David Howells4be59752019-04-25 14:26:50 +0100869
David Howellsd4696602019-04-25 14:26:50 +0100870 trace_afs_flock_op(vnode, fl, afs_flock_op_release_lock);
David Howells4be59752019-04-25 14:26:50 +0100871 list_del_init(&fl->fl_u.afs.link);
872 if (list_empty(&vnode->granted_locks))
873 afs_defer_unlock(vnode);
874
David Howells0fafdc92017-11-13 16:59:50 +0000875 _debug("state %u for %p", vnode->lock_state, vnode);
876 spin_unlock(&vnode->lock);
David Howellse8d6c552007-07-15 23:40:12 -0700877}