Linux Audio

Check our new training course

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