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 #define AFS_LOCK_YOUR_TRY	2
17 
18 struct workqueue_struct *afs_lock_manager;
19 
20 static void afs_next_locker(struct afs_vnode *vnode, int error);
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 
24 static const struct file_lock_operations afs_lock_ops = {
25 	.fl_copy_lock		= afs_fl_copy_lock,
26 	.fl_release_private	= afs_fl_release_private,
27 };
28 
afs_set_lock_state(struct afs_vnode * vnode,enum afs_lock_state state)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 
35 /*
36  * if the callback is broken on this vnode, then the lock may now be available
37  */
afs_lock_may_be_available(struct afs_vnode * vnode)38 void afs_lock_may_be_available(struct afs_vnode *vnode)
39 {
40 	_enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
41 
42 	spin_lock(&vnode->lock);
43 	if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
44 		afs_next_locker(vnode, 0);
45 	spin_unlock(&vnode->lock);
46 }
47 
48 /*
49  * the lock will time out in 5 minutes unless we extend it, so schedule
50  * extension in a bit less than that time
51  */
afs_schedule_lock_extension(struct afs_vnode * vnode)52 static void __maybe_unused afs_schedule_lock_extension(struct afs_vnode *vnode)
53 {
54 	queue_delayed_work(afs_lock_manager, &vnode->lock_work,
55 			   AFS_LOCKWAIT * HZ / 2);
56 }
57 
58 /*
59  * grant one or more locks (readlocks are allowed to jump the queue if the
60  * first lock in the queue is itself a readlock)
61  * - the caller must hold the vnode lock
62  */
afs_grant_locks(struct afs_vnode * vnode)63 static void afs_grant_locks(struct afs_vnode *vnode)
64 {
65 	struct file_lock *p, *_p;
66 	bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
67 
68 	list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
69 		if (!exclusive && p->fl_type == F_WRLCK)
70 			continue;
71 
72 		list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
73 		p->fl_u.afs.state = AFS_LOCK_GRANTED;
74 		wake_up(&p->fl_wait);
75 	}
76 }
77 
78 /*
79  * If an error is specified, reject every pending lock that matches the
80  * authentication and type of the lock we failed to get.  If there are any
81  * remaining lockers, try to wake up one of them to have a go.
82  */
afs_next_locker(struct afs_vnode * vnode,int error)83 static void afs_next_locker(struct afs_vnode *vnode, int error)
84 {
85 	struct file_lock *p, *_p, *next = NULL;
86 	struct key *key = vnode->lock_key;
87 	unsigned int fl_type = F_RDLCK;
88 
89 	_enter("");
90 
91 	if (vnode->lock_type == AFS_LOCK_WRITE)
92 		fl_type = F_WRLCK;
93 
94 	list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
95 		if (error &&
96 		    p->fl_type == fl_type &&
97 		    afs_file_key(p->fl_file) == key) {
98 			list_del_init(&p->fl_u.afs.link);
99 			p->fl_u.afs.state = error;
100 			wake_up(&p->fl_wait);
101 		}
102 
103 		/* Select the next locker to hand off to. */
104 		if (next &&
105 		    (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
106 			continue;
107 		next = p;
108 	}
109 
110 	vnode->lock_key = NULL;
111 	key_put(key);
112 
113 	if (next) {
114 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
115 		next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
116 		wake_up(&next->fl_wait);
117 	} else {
118 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
119 	}
120 
121 	_leave("");
122 }
123 
124 /*
125  * Get a lock on a file
126  */
afs_set_lock(struct afs_vnode * vnode,struct key * key,afs_lock_type_t type)127 static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
128 			afs_lock_type_t type)
129 {
130 	struct afs_fs_cursor fc;
131 	int ret;
132 
133 	_enter("%s{%x:%u.%u},%x,%u",
134 	       vnode->volume->name,
135 	       vnode->fid.vid,
136 	       vnode->fid.vnode,
137 	       vnode->fid.unique,
138 	       key_serial(key), type);
139 
140 	ret = -ERESTARTSYS;
141 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
142 		while (afs_select_fileserver(&fc)) {
143 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
144 			afs_fs_set_lock(&fc, type);
145 		}
146 
147 		afs_check_for_remote_deletion(&fc, fc.vnode);
148 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
149 		ret = afs_end_vnode_operation(&fc);
150 	}
151 
152 	_leave(" = %d", ret);
153 	return ret;
154 }
155 
156 /*
157  * Extend a lock on a file
158  */
afs_extend_lock(struct afs_vnode * vnode,struct key * key)159 static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
160 {
161 	struct afs_fs_cursor fc;
162 	int ret;
163 
164 	_enter("%s{%x:%u.%u},%x",
165 	       vnode->volume->name,
166 	       vnode->fid.vid,
167 	       vnode->fid.vnode,
168 	       vnode->fid.unique,
169 	       key_serial(key));
170 
171 	ret = -ERESTARTSYS;
172 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
173 		while (afs_select_current_fileserver(&fc)) {
174 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
175 			afs_fs_extend_lock(&fc);
176 		}
177 
178 		afs_check_for_remote_deletion(&fc, fc.vnode);
179 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
180 		ret = afs_end_vnode_operation(&fc);
181 	}
182 
183 	_leave(" = %d", ret);
184 	return ret;
185 }
186 
187 /*
188  * Release a lock on a file
189  */
afs_release_lock(struct afs_vnode * vnode,struct key * key)190 static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
191 {
192 	struct afs_fs_cursor fc;
193 	int ret;
194 
195 	_enter("%s{%x:%u.%u},%x",
196 	       vnode->volume->name,
197 	       vnode->fid.vid,
198 	       vnode->fid.vnode,
199 	       vnode->fid.unique,
200 	       key_serial(key));
201 
202 	ret = -ERESTARTSYS;
203 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
204 		while (afs_select_current_fileserver(&fc)) {
205 			fc.cb_break = afs_calc_vnode_cb_break(vnode);
206 			afs_fs_release_lock(&fc);
207 		}
208 
209 		afs_check_for_remote_deletion(&fc, fc.vnode);
210 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
211 		ret = afs_end_vnode_operation(&fc);
212 	}
213 
214 	_leave(" = %d", ret);
215 	return ret;
216 }
217 
218 /*
219  * do work for a lock, including:
220  * - probing for a lock we're waiting on but didn't get immediately
221  * - extending a lock that's close to timing out
222  */
afs_lock_work(struct work_struct * work)223 void afs_lock_work(struct work_struct *work)
224 {
225 	struct afs_vnode *vnode =
226 		container_of(work, struct afs_vnode, lock_work.work);
227 	struct key *key;
228 	int ret;
229 
230 	_enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
231 
232 	spin_lock(&vnode->lock);
233 
234 again:
235 	_debug("wstate %u for %p", vnode->lock_state, vnode);
236 	switch (vnode->lock_state) {
237 	case AFS_VNODE_LOCK_NEED_UNLOCK:
238 		_debug("unlock");
239 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING);
240 		spin_unlock(&vnode->lock);
241 
242 		/* attempt to release the server lock; if it fails, we just
243 		 * wait 5 minutes and it'll expire anyway */
244 		ret = afs_release_lock(vnode, vnode->lock_key);
245 		if (ret < 0)
246 			printk(KERN_WARNING "AFS:"
247 			       " Failed to release lock on {%x:%x} error %d\n",
248 			       vnode->fid.vid, vnode->fid.vnode, ret);
249 
250 		spin_lock(&vnode->lock);
251 		afs_next_locker(vnode, 0);
252 		spin_unlock(&vnode->lock);
253 		return;
254 
255 	/* If we've already got a lock, then it must be time to extend that
256 	 * lock as AFS locks time out after 5 minutes.
257 	 */
258 	case AFS_VNODE_LOCK_GRANTED:
259 		_debug("extend");
260 
261 		ASSERT(!list_empty(&vnode->granted_locks));
262 
263 		key = key_get(vnode->lock_key);
264 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING);
265 		spin_unlock(&vnode->lock);
266 
267 		ret = afs_extend_lock(vnode, key); /* RPC */
268 		key_put(key);
269 
270 		if (ret < 0)
271 			pr_warning("AFS: Failed to extend lock on {%x:%x} error %d\n",
272 				   vnode->fid.vid, vnode->fid.vnode, ret);
273 
274 		spin_lock(&vnode->lock);
275 
276 		if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
277 			goto again;
278 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
279 
280 		if (ret != 0)
281 			queue_delayed_work(afs_lock_manager, &vnode->lock_work,
282 					   HZ * 10);
283 		spin_unlock(&vnode->lock);
284 		_leave(" [ext]");
285 		return;
286 
287 	/* If we're waiting for a callback to indicate lock release, we can't
288 	 * actually rely on this, so need to recheck at regular intervals.  The
289 	 * problem is that the server might not notify us if the lock just
290 	 * expires (say because a client died) rather than being explicitly
291 	 * released.
292 	 */
293 	case AFS_VNODE_LOCK_WAITING_FOR_CB:
294 		_debug("retry");
295 		afs_next_locker(vnode, 0);
296 		spin_unlock(&vnode->lock);
297 		return;
298 
299 	default:
300 		/* Looks like a lock request was withdrawn. */
301 		spin_unlock(&vnode->lock);
302 		_leave(" [no]");
303 		return;
304 	}
305 }
306 
307 /*
308  * pass responsibility for the unlocking of a vnode on the server to the
309  * manager thread, lest a pending signal in the calling thread interrupt
310  * AF_RXRPC
311  * - the caller must hold the vnode lock
312  */
afs_defer_unlock(struct afs_vnode * vnode)313 static void afs_defer_unlock(struct afs_vnode *vnode)
314 {
315 	_enter("%u", vnode->lock_state);
316 
317 	if (list_empty(&vnode->granted_locks) &&
318 	    (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
319 	     vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) {
320 		cancel_delayed_work(&vnode->lock_work);
321 
322 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK);
323 		queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
324 	}
325 }
326 
327 /*
328  * Check that our view of the file metadata is up to date and check to see
329  * whether we think that we have a locking permit.
330  */
afs_do_setlk_check(struct afs_vnode * vnode,struct key * key,afs_lock_type_t type,bool can_sleep)331 static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
332 			      afs_lock_type_t type, bool can_sleep)
333 {
334 	afs_access_t access;
335 	int ret;
336 
337 	/* Make sure we've got a callback on this file and that our view of the
338 	 * data version is up to date.
339 	 */
340 	ret = afs_validate(vnode, key);
341 	if (ret < 0)
342 		return ret;
343 
344 	/* Check the permission set to see if we're actually going to be
345 	 * allowed to get a lock on this file.
346 	 */
347 	ret = afs_check_permit(vnode, key, &access);
348 	if (ret < 0)
349 		return ret;
350 
351 	/* At a rough estimation, you need LOCK, WRITE or INSERT perm to
352 	 * read-lock a file and WRITE or INSERT perm to write-lock a file.
353 	 *
354 	 * We can't rely on the server to do this for us since if we want to
355 	 * share a read lock that we already have, we won't go the server.
356 	 */
357 	if (type == AFS_LOCK_READ) {
358 		if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
359 			return -EACCES;
360 		if (vnode->status.lock_count == -1 && !can_sleep)
361 			return -EAGAIN; /* Write locked */
362 	} else {
363 		if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
364 			return -EACCES;
365 		if (vnode->status.lock_count != 0 && !can_sleep)
366 			return -EAGAIN; /* Locked */
367 	}
368 
369 	return 0;
370 }
371 
372 /*
373  * request a lock on a file on the server
374  */
afs_do_setlk(struct file * file,struct file_lock * fl)375 static int afs_do_setlk(struct file *file, struct file_lock *fl)
376 {
377 	struct inode *inode = locks_inode(file);
378 	struct afs_vnode *vnode = AFS_FS_I(inode);
379 	afs_lock_type_t type;
380 	struct key *key = afs_file_key(file);
381 	int ret;
382 
383 	_enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
384 
385 	fl->fl_ops = &afs_lock_ops;
386 	INIT_LIST_HEAD(&fl->fl_u.afs.link);
387 	fl->fl_u.afs.state = AFS_LOCK_PENDING;
388 
389 	type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
390 
391 	ret = afs_do_setlk_check(vnode, key, type, fl->fl_flags & FL_SLEEP);
392 	if (ret < 0)
393 		return ret;
394 
395 	spin_lock(&vnode->lock);
396 	list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
397 
398 	/* If we've already got a lock on the server then try to move to having
399 	 * the VFS grant the requested lock.  Note that this means that other
400 	 * clients may get starved out.
401 	 */
402 	_debug("try %u", vnode->lock_state);
403 	if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) {
404 		if (type == AFS_LOCK_READ) {
405 			_debug("instant readlock");
406 			list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
407 			fl->fl_u.afs.state = AFS_LOCK_GRANTED;
408 			goto vnode_is_locked_u;
409 		}
410 
411 		if (vnode->lock_type == AFS_LOCK_WRITE) {
412 			_debug("instant writelock");
413 			list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
414 			fl->fl_u.afs.state = AFS_LOCK_GRANTED;
415 			goto vnode_is_locked_u;
416 		}
417 	}
418 
419 	if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
420 		goto need_to_wait;
421 
422 try_to_lock:
423 	/* We don't have a lock on this vnode and we aren't currently waiting
424 	 * for one either, so ask the server for a lock.
425 	 *
426 	 * Note that we need to be careful if we get interrupted by a signal
427 	 * after dispatching the request as we may still get the lock, even
428 	 * though we don't wait for the reply (it's not too bad a problem - the
429 	 * lock will expire in 5 mins anyway).
430 	 */
431 	_debug("not locked");
432 	vnode->lock_key = key_get(key);
433 	vnode->lock_type = type;
434 	afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
435 	spin_unlock(&vnode->lock);
436 
437 	ret = afs_set_lock(vnode, key, type); /* RPC */
438 
439 	spin_lock(&vnode->lock);
440 	switch (ret) {
441 	case -EKEYREJECTED:
442 	case -EKEYEXPIRED:
443 	case -EKEYREVOKED:
444 	case -EPERM:
445 	case -EACCES:
446 		fl->fl_u.afs.state = ret;
447 		list_del_init(&fl->fl_u.afs.link);
448 		afs_next_locker(vnode, ret);
449 		goto error_unlock;
450 
451 	default:
452 		fl->fl_u.afs.state = ret;
453 		list_del_init(&fl->fl_u.afs.link);
454 		afs_next_locker(vnode, 0);
455 		goto error_unlock;
456 
457 	case -EWOULDBLOCK:
458 		/* The server doesn't have a lock-waiting queue, so the client
459 		 * will have to retry.  The server will break the outstanding
460 		 * callbacks on a file when a lock is released.
461 		 */
462 		_debug("would block");
463 		ASSERT(list_empty(&vnode->granted_locks));
464 		ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
465 		goto lock_is_contended;
466 
467 	case 0:
468 		_debug("acquired");
469 		afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
470 		afs_grant_locks(vnode);
471 		goto vnode_is_locked_u;
472 	}
473 
474 vnode_is_locked_u:
475 	spin_unlock(&vnode->lock);
476 vnode_is_locked:
477 	/* the lock has been granted by the server... */
478 	ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED);
479 
480 	/* ... but the VFS still needs to distribute access on this client. */
481 	ret = locks_lock_file_wait(file, fl);
482 	if (ret < 0)
483 		goto vfs_rejected_lock;
484 
485 	/* Again, make sure we've got a callback on this file and, again, make
486 	 * sure that our view of the data version is up to date (we ignore
487 	 * errors incurred here and deal with the consequences elsewhere).
488 	 */
489 	afs_validate(vnode, key);
490 	_leave(" = 0");
491 	return 0;
492 
493 lock_is_contended:
494 	if (!(fl->fl_flags & FL_SLEEP)) {
495 		list_del_init(&fl->fl_u.afs.link);
496 		afs_next_locker(vnode, 0);
497 		ret = -EAGAIN;
498 		goto error_unlock;
499 	}
500 
501 	afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB);
502 	queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5);
503 
504 need_to_wait:
505 	/* We're going to have to wait.  Either this client doesn't have a lock
506 	 * on the server yet and we need to wait for a callback to occur, or
507 	 * the client does have a lock on the server, but it's shared and we
508 	 * need an exclusive lock.
509 	 */
510 	spin_unlock(&vnode->lock);
511 
512 	_debug("sleep");
513 	ret = wait_event_interruptible(fl->fl_wait,
514 				       fl->fl_u.afs.state != AFS_LOCK_PENDING);
515 	_debug("wait = %d", ret);
516 
517 	if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) {
518 		spin_lock(&vnode->lock);
519 
520 		switch (fl->fl_u.afs.state) {
521 		case AFS_LOCK_YOUR_TRY:
522 			fl->fl_u.afs.state = AFS_LOCK_PENDING;
523 			goto try_to_lock;
524 		case AFS_LOCK_PENDING:
525 			if (ret > 0) {
526 				/* We need to retry the lock.  We may not be
527 				 * notified by the server if it just expired
528 				 * rather than being released.
529 				 */
530 				ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB);
531 				afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
532 				fl->fl_u.afs.state = AFS_LOCK_PENDING;
533 				goto try_to_lock;
534 			}
535 			goto error_unlock;
536 		case AFS_LOCK_GRANTED:
537 		default:
538 			break;
539 		}
540 
541 		spin_unlock(&vnode->lock);
542 	}
543 
544 	if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
545 		goto vnode_is_locked;
546 	ret = fl->fl_u.afs.state;
547 	goto error;
548 
549 vfs_rejected_lock:
550 	/* The VFS rejected the lock we just obtained, so we have to discard
551 	 * what we just got.  We defer this to the lock manager work item to
552 	 * deal with.
553 	 */
554 	_debug("vfs refused %d", ret);
555 	spin_lock(&vnode->lock);
556 	list_del_init(&fl->fl_u.afs.link);
557 	afs_defer_unlock(vnode);
558 
559 error_unlock:
560 	spin_unlock(&vnode->lock);
561 error:
562 	_leave(" = %d", ret);
563 	return ret;
564 }
565 
566 /*
567  * unlock on a file on the server
568  */
afs_do_unlk(struct file * file,struct file_lock * fl)569 static int afs_do_unlk(struct file *file, struct file_lock *fl)
570 {
571 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
572 	int ret;
573 
574 	_enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
575 
576 	/* Flush all pending writes before doing anything with locks. */
577 	vfs_fsync(file, 0);
578 
579 	ret = locks_lock_file_wait(file, fl);
580 	_leave(" = %d [%u]", ret, vnode->lock_state);
581 	return ret;
582 }
583 
584 /*
585  * return information about a lock we currently hold, if indeed we hold one
586  */
afs_do_getlk(struct file * file,struct file_lock * fl)587 static int afs_do_getlk(struct file *file, struct file_lock *fl)
588 {
589 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
590 	struct key *key = afs_file_key(file);
591 	int ret, lock_count;
592 
593 	_enter("");
594 
595 	fl->fl_type = F_UNLCK;
596 
597 	/* check local lock records first */
598 	posix_test_lock(file, fl);
599 	if (fl->fl_type == F_UNLCK) {
600 		/* no local locks; consult the server */
601 		ret = afs_fetch_status(vnode, key, false);
602 		if (ret < 0)
603 			goto error;
604 
605 		lock_count = READ_ONCE(vnode->status.lock_count);
606 		if (lock_count != 0) {
607 			if (lock_count > 0)
608 				fl->fl_type = F_RDLCK;
609 			else
610 				fl->fl_type = F_WRLCK;
611 			fl->fl_start = 0;
612 			fl->fl_end = OFFSET_MAX;
613 			fl->fl_pid = 0;
614 		}
615 	}
616 
617 	ret = 0;
618 error:
619 	_leave(" = %d [%hd]", ret, fl->fl_type);
620 	return ret;
621 }
622 
623 /*
624  * manage POSIX locks on a file
625  */
afs_lock(struct file * file,int cmd,struct file_lock * fl)626 int afs_lock(struct file *file, int cmd, struct file_lock *fl)
627 {
628 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
629 
630 	_enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
631 	       vnode->fid.vid, vnode->fid.vnode, cmd,
632 	       fl->fl_type, fl->fl_flags,
633 	       (long long) fl->fl_start, (long long) fl->fl_end);
634 
635 	/* AFS doesn't support mandatory locks */
636 	if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
637 		return -ENOLCK;
638 
639 	if (IS_GETLK(cmd))
640 		return afs_do_getlk(file, fl);
641 	if (fl->fl_type == F_UNLCK)
642 		return afs_do_unlk(file, fl);
643 	return afs_do_setlk(file, fl);
644 }
645 
646 /*
647  * manage FLOCK locks on a file
648  */
afs_flock(struct file * file,int cmd,struct file_lock * fl)649 int afs_flock(struct file *file, int cmd, struct file_lock *fl)
650 {
651 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
652 
653 	_enter("{%x:%u},%d,{t=%x,fl=%x}",
654 	       vnode->fid.vid, vnode->fid.vnode, cmd,
655 	       fl->fl_type, fl->fl_flags);
656 
657 	/*
658 	 * No BSD flocks over NFS allowed.
659 	 * Note: we could try to fake a POSIX lock request here by
660 	 * using ((u32) filp | 0x80000000) or some such as the pid.
661 	 * Not sure whether that would be unique, though, or whether
662 	 * that would break in other places.
663 	 */
664 	if (!(fl->fl_flags & FL_FLOCK))
665 		return -ENOLCK;
666 
667 	/* we're simulating flock() locks using posix locks on the server */
668 	if (fl->fl_type == F_UNLCK)
669 		return afs_do_unlk(file, fl);
670 	return afs_do_setlk(file, fl);
671 }
672 
673 /*
674  * the POSIX lock management core VFS code copies the lock record and adds the
675  * copy into its own list, so we need to add that copy to the vnode's lock
676  * queue in the same place as the original (which will be deleted shortly
677  * after)
678  */
afs_fl_copy_lock(struct file_lock * new,struct file_lock * fl)679 static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
680 {
681 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
682 
683 	_enter("");
684 
685 	spin_lock(&vnode->lock);
686 	list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
687 	spin_unlock(&vnode->lock);
688 }
689 
690 /*
691  * need to remove this lock from the vnode queue when it's removed from the
692  * VFS's list
693  */
afs_fl_release_private(struct file_lock * fl)694 static void afs_fl_release_private(struct file_lock *fl)
695 {
696 	struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
697 
698 	_enter("");
699 
700 	spin_lock(&vnode->lock);
701 
702 	list_del_init(&fl->fl_u.afs.link);
703 	if (list_empty(&vnode->granted_locks))
704 		afs_defer_unlock(vnode);
705 
706 	_debug("state %u for %p", vnode->lock_state, vnode);
707 	spin_unlock(&vnode->lock);
708 }
709