flock.c 18.9 KB
Newer Older
D
David Howells 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* AFS file locking support
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include "internal.h"

#define AFS_LOCK_GRANTED	0
#define AFS_LOCK_PENDING	1

17 18
struct workqueue_struct *afs_lock_manager;

D
David Howells 已提交
19 20 21
static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
static void afs_fl_release_private(struct file_lock *fl);

22
static const struct file_lock_operations afs_lock_ops = {
D
David Howells 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	.fl_copy_lock		= afs_fl_copy_lock,
	.fl_release_private	= afs_fl_release_private,
};

/*
 * if the callback is broken on this vnode, then the lock may now be available
 */
void afs_lock_may_be_available(struct afs_vnode *vnode)
{
	_enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);

	queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
}

/*
 * the lock will time out in 5 minutes unless we extend it, so schedule
 * extension in a bit less than that time
 */
static void afs_schedule_lock_extension(struct afs_vnode *vnode)
{
	queue_delayed_work(afs_lock_manager, &vnode->lock_work,
			   AFS_LOCKWAIT * HZ / 2);
}

D
David Howells 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * grant one or more locks (readlocks are allowed to jump the queue if the
 * first lock in the queue is itself a readlock)
 * - the caller must hold the vnode lock
 */
static void afs_grant_locks(struct afs_vnode *vnode, struct file_lock *fl)
{
	struct file_lock *p, *_p;

	list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
	if (fl->fl_type == F_RDLCK) {
		list_for_each_entry_safe(p, _p, &vnode->pending_locks,
					 fl_u.afs.link) {
			if (p->fl_type == F_RDLCK) {
				p->fl_u.afs.state = AFS_LOCK_GRANTED;
				list_move_tail(&p->fl_u.afs.link,
					       &vnode->granted_locks);
				wake_up(&p->fl_wait);
			}
		}
	}
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
/*
 * Get a lock on a file
 */
static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
			afs_lock_type_t type)
{
	struct afs_fs_cursor fc;
	int ret;

	_enter("%s{%x:%u.%u},%x,%u",
	       vnode->volume->name,
	       vnode->fid.vid,
	       vnode->fid.vnode,
	       vnode->fid.unique,
	       key_serial(key), type);

	ret = -ERESTARTSYS;
	if (afs_begin_vnode_operation(&fc, vnode, key)) {
		while (afs_select_fileserver(&fc)) {
			fc.cb_break = vnode->cb_break + vnode->cb_s_break;
			afs_fs_set_lock(&fc, type);
		}

		afs_check_for_remote_deletion(&fc, fc.vnode);
		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
		ret = afs_end_vnode_operation(&fc);
	}

	_leave(" = %d", ret);
	return ret;
}

/*
 * Extend a lock on a file
 */
static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
{
	struct afs_fs_cursor fc;
	int ret;

	_enter("%s{%x:%u.%u},%x",
	       vnode->volume->name,
	       vnode->fid.vid,
	       vnode->fid.vnode,
	       vnode->fid.unique,
	       key_serial(key));

	ret = -ERESTARTSYS;
	if (afs_begin_vnode_operation(&fc, vnode, key)) {
		while (afs_select_current_fileserver(&fc)) {
			fc.cb_break = vnode->cb_break + vnode->cb_s_break;
			afs_fs_extend_lock(&fc);
		}

		afs_check_for_remote_deletion(&fc, fc.vnode);
		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
		ret = afs_end_vnode_operation(&fc);
	}

	_leave(" = %d", ret);
	return ret;
}

/*
 * Release a lock on a file
 */
static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
{
	struct afs_fs_cursor fc;
	int ret;

	_enter("%s{%x:%u.%u},%x",
	       vnode->volume->name,
	       vnode->fid.vid,
	       vnode->fid.vnode,
	       vnode->fid.unique,
	       key_serial(key));

	ret = -ERESTARTSYS;
	if (afs_begin_vnode_operation(&fc, vnode, key)) {
		while (afs_select_current_fileserver(&fc)) {
			fc.cb_break = vnode->cb_break + vnode->cb_s_break;
			afs_fs_release_lock(&fc);
		}

		afs_check_for_remote_deletion(&fc, fc.vnode);
		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
		ret = afs_end_vnode_operation(&fc);
	}

	_leave(" = %d", ret);
	return ret;
}

D
David Howells 已提交
164 165 166 167 168 169 170 171 172
/*
 * do work for a lock, including:
 * - probing for a lock we're waiting on but didn't get immediately
 * - extending a lock that's close to timing out
 */
void afs_lock_work(struct work_struct *work)
{
	struct afs_vnode *vnode =
		container_of(work, struct afs_vnode, lock_work.work);
D
David Howells 已提交
173
	struct file_lock *fl, *next;
D
David Howells 已提交
174 175 176 177 178 179 180 181
	afs_lock_type_t type;
	struct key *key;
	int ret;

	_enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);

	spin_lock(&vnode->lock);

D
David Howells 已提交
182 183 184 185
again:
	_debug("wstate %u for %p", vnode->lock_state, vnode);
	switch (vnode->lock_state) {
	case AFS_VNODE_LOCK_NEED_UNLOCK:
D
David Howells 已提交
186
		_debug("unlock");
D
David Howells 已提交
187
		vnode->lock_state = AFS_VNODE_LOCK_UNLOCKING;
D
David Howells 已提交
188 189 190
		spin_unlock(&vnode->lock);

		/* attempt to release the server lock; if it fails, we just
D
David Howells 已提交
191 192
		 * wait 5 minutes and it'll expire anyway */
		ret = afs_release_lock(vnode, vnode->lock_key);
D
David Howells 已提交
193 194 195 196 197 198
		if (ret < 0)
			printk(KERN_WARNING "AFS:"
			       " Failed to release lock on {%x:%x} error %d\n",
			       vnode->fid.vid, vnode->fid.vnode, ret);

		spin_lock(&vnode->lock);
D
David Howells 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		key_put(vnode->lock_key);
		vnode->lock_key = NULL;
		vnode->lock_state = AFS_VNODE_LOCK_NONE;

		if (list_empty(&vnode->pending_locks)) {
			spin_unlock(&vnode->lock);
			return;
		}

		/* The new front of the queue now owns the state variables. */
		next = list_entry(vnode->pending_locks.next,
				  struct file_lock, fl_u.afs.link);
		vnode->lock_key = afs_file_key(next->fl_file);
		vnode->lock_type = (next->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
		vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
		goto again;
D
David Howells 已提交
215

D
David Howells 已提交
216 217 218 219
	/* If we've already got a lock, then it must be time to extend that
	 * lock as AFS locks time out after 5 minutes.
	 */
	case AFS_VNODE_LOCK_GRANTED:
D
David Howells 已提交
220 221
		_debug("extend");

D
David Howells 已提交
222 223 224 225
		ASSERT(!list_empty(&vnode->granted_locks));

		key = key_get(vnode->lock_key);
		vnode->lock_state = AFS_VNODE_LOCK_EXTENDING;
D
David Howells 已提交
226 227
		spin_unlock(&vnode->lock);

D
David Howells 已提交
228
		ret = afs_extend_lock(vnode, key); /* RPC */
D
David Howells 已提交
229
		key_put(key);
D
David Howells 已提交
230 231 232 233 234 235 236 237 238 239 240 241

		if (ret < 0)
			pr_warning("AFS: Failed to extend lock on {%x:%x} error %d\n",
				   vnode->fid.vid, vnode->fid.vnode, ret);

		spin_lock(&vnode->lock);

		if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
			goto again;
		vnode->lock_state = AFS_VNODE_LOCK_GRANTED;

		if (ret == 0)
D
David Howells 已提交
242
			afs_schedule_lock_extension(vnode);
D
David Howells 已提交
243
		else
D
David Howells 已提交
244 245
			queue_delayed_work(afs_lock_manager, &vnode->lock_work,
					   HZ * 10);
D
David Howells 已提交
246 247
		spin_unlock(&vnode->lock);
		_leave(" [ext]");
D
David Howells 已提交
248 249
		return;

D
David Howells 已提交
250 251 252 253 254
		/* If we don't have a granted lock, then we must've been called
		 * back by the server, and so if might be possible to get a
		 * lock we're currently waiting for.
		 */
	case AFS_VNODE_LOCK_WAITING_FOR_CB:
D
David Howells 已提交
255 256
		_debug("get");

D
David Howells 已提交
257 258 259
		key = key_get(vnode->lock_key);
		type = vnode->lock_type;
		vnode->lock_state = AFS_VNODE_LOCK_SETTING;
D
David Howells 已提交
260 261
		spin_unlock(&vnode->lock);

D
David Howells 已提交
262 263 264 265
		ret = afs_set_lock(vnode, key, type); /* RPC */
		key_put(key);

		spin_lock(&vnode->lock);
D
David Howells 已提交
266 267 268 269 270 271
		switch (ret) {
		case -EWOULDBLOCK:
			_debug("blocked");
			break;
		case 0:
			_debug("acquired");
D
David Howells 已提交
272 273
			vnode->lock_state = AFS_VNODE_LOCK_GRANTED;
			/* Fall through */
D
David Howells 已提交
274
		default:
D
David Howells 已提交
275 276 277 278 279 280
			/* Pass the lock or the error onto the first locker in
			 * the list - if they're looking for this type of lock.
			 * If they're not, we assume that whoever asked for it
			 * took a signal.
			 */
			if (list_empty(&vnode->pending_locks)) {
D
David Howells 已提交
281
				_debug("withdrawn");
D
David Howells 已提交
282 283
				vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
				goto again;
D
David Howells 已提交
284
			}
D
David Howells 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

			fl = list_entry(vnode->pending_locks.next,
					struct file_lock, fl_u.afs.link);
			type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
			if (vnode->lock_type != type) {
				_debug("changed");
				vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
				goto again;
			}

			fl->fl_u.afs.state = ret;
			if (ret == 0)
				afs_grant_locks(vnode, fl);
			else
				list_del_init(&fl->fl_u.afs.link);
			wake_up(&fl->fl_wait);
			spin_unlock(&vnode->lock);
			_leave(" [granted]");
			return;
D
David Howells 已提交
304
		}
D
David Howells 已提交
305 306 307 308 309

	default:
		/* Looks like a lock request was withdrawn. */
		spin_unlock(&vnode->lock);
		_leave(" [no]");
D
David Howells 已提交
310 311 312 313 314 315 316 317 318 319
		return;
	}
}

/*
 * pass responsibility for the unlocking of a vnode on the server to the
 * manager thread, lest a pending signal in the calling thread interrupt
 * AF_RXRPC
 * - the caller must hold the vnode lock
 */
D
David Howells 已提交
320
static void afs_defer_unlock(struct afs_vnode *vnode)
D
David Howells 已提交
321
{
D
David Howells 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	_enter("");

	if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
	    vnode->lock_state == AFS_VNODE_LOCK_EXTENDING) {
		cancel_delayed_work(&vnode->lock_work);

		vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
		afs_lock_may_be_available(vnode);
	}
}

/*
 * Check that our view of the file metadata is up to date and check to see
 * whether we think that we have a locking permit.
 */
static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
			      afs_lock_type_t type, bool can_sleep)
{
	afs_access_t access;
	int ret;

	/* Make sure we've got a callback on this file and that our view of the
	 * data version is up to date.
	 */
	ret = afs_validate(vnode, key);
	if (ret < 0)
		return ret;

	/* Check the permission set to see if we're actually going to be
	 * allowed to get a lock on this file.
	 */
	ret = afs_check_permit(vnode, key, &access);
	if (ret < 0)
		return ret;

	/* At a rough estimation, you need LOCK, WRITE or INSERT perm to
	 * read-lock a file and WRITE or INSERT perm to write-lock a file.
	 *
	 * We can't rely on the server to do this for us since if we want to
	 * share a read lock that we already have, we won't go the server.
	 */
	if (type == AFS_LOCK_READ) {
		if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
			return -EACCES;
		if (vnode->status.lock_count == -1 && !can_sleep)
			return -EAGAIN; /* Write locked */
	} else {
		if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
			return -EACCES;
		if (vnode->status.lock_count != 0 && !can_sleep)
			return -EAGAIN; /* Locked */
	}

	return 0;
}

/*
 * Remove the front runner from the pending queue.
 * - The caller must hold vnode->lock.
 */
static void afs_dequeue_lock(struct afs_vnode *vnode, struct file_lock *fl)
{
	struct file_lock *next;

	_enter("");

	/* ->lock_type, ->lock_key and ->lock_state only belong to this
	 * file_lock if we're at the front of the pending queue or if we have
	 * the lock granted or if the lock_state is NEED_UNLOCK or UNLOCKING.
	 */
	if (vnode->granted_locks.next == &fl->fl_u.afs.link &&
	    vnode->granted_locks.prev == &fl->fl_u.afs.link) {
		list_del_init(&fl->fl_u.afs.link);
		afs_defer_unlock(vnode);
		return;
	}

	if (!list_empty(&vnode->granted_locks) ||
	    vnode->pending_locks.next != &fl->fl_u.afs.link) {
		list_del_init(&fl->fl_u.afs.link);
		return;
	}

	list_del_init(&fl->fl_u.afs.link);
	key_put(vnode->lock_key);
	vnode->lock_key = NULL;
	vnode->lock_state = AFS_VNODE_LOCK_NONE;

	if (list_empty(&vnode->pending_locks))
		return;

	/* The new front of the queue now owns the state variables. */
	next = list_entry(vnode->pending_locks.next,
			  struct file_lock, fl_u.afs.link);
	vnode->lock_key = afs_file_key(next->fl_file);
	vnode->lock_type = (next->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
	vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
D
David Howells 已提交
419 420 421 422 423 424 425 426
	afs_lock_may_be_available(vnode);
}

/*
 * request a lock on a file on the server
 */
static int afs_do_setlk(struct file *file, struct file_lock *fl)
{
D
David Howells 已提交
427
	struct inode *inode = locks_inode(file);
428
	struct afs_vnode *vnode = AFS_FS_I(inode);
D
David Howells 已提交
429
	afs_lock_type_t type;
430
	struct key *key = afs_file_key(file);
D
David Howells 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444
	int ret;

	_enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);

	/* only whole-file locks are supported */
	if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
		return -EINVAL;

	fl->fl_ops = &afs_lock_ops;
	INIT_LIST_HEAD(&fl->fl_u.afs.link);
	fl->fl_u.afs.state = AFS_LOCK_PENDING;

	type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;

D
David Howells 已提交
445
	ret = afs_do_setlk_check(vnode, key, type, fl->fl_flags & FL_SLEEP);
D
David Howells 已提交
446
	if (ret < 0)
D
David Howells 已提交
447
		return ret;
D
David Howells 已提交
448 449 450

	spin_lock(&vnode->lock);

D
David Howells 已提交
451
	/* If we've already got a readlock on the server then we instantly
D
David Howells 已提交
452
	 * grant another readlock, irrespective of whether there are any
D
David Howells 已提交
453 454
	 * pending writelocks.
	 */
D
David Howells 已提交
455
	if (type == AFS_LOCK_READ &&
D
David Howells 已提交
456 457
	    vnode->lock_state == AFS_VNODE_LOCK_GRANTED &&
	    vnode->lock_type == AFS_LOCK_READ) {
D
David Howells 已提交
458 459
		_debug("instant readlock");
		ASSERT(!list_empty(&vnode->granted_locks));
D
David Howells 已提交
460
		goto share_existing_lock;
D
David Howells 已提交
461
	}
D
David Howells 已提交
462

D
David Howells 已提交
463
	list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
D
David Howells 已提交
464

D
David Howells 已提交
465 466
	if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
		goto need_to_wait;
D
David Howells 已提交
467

D
David Howells 已提交
468 469 470 471 472 473 474 475 476 477 478 479
	/* We don't have a lock on this vnode and we aren't currently waiting
	 * for one either, so ask the server for a lock.
	 *
	 * Note that we need to be careful if we get interrupted by a signal
	 * after dispatching the request as we may still get the lock, even
	 * though we don't wait for the reply (it's not too bad a problem - the
	 * lock will expire in 10 mins anyway).
	 */
	_debug("not locked");
	vnode->lock_key = key_get(key);
	vnode->lock_type = type;
	vnode->lock_state = AFS_VNODE_LOCK_SETTING;
D
David Howells 已提交
480 481
	spin_unlock(&vnode->lock);

D
David Howells 已提交
482
	ret = afs_set_lock(vnode, key, type); /* RPC */
D
David Howells 已提交
483 484

	spin_lock(&vnode->lock);
D
David Howells 已提交
485 486 487
	switch (ret) {
	default:
		goto abort_attempt;
D
David Howells 已提交
488

D
David Howells 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502
	case -EWOULDBLOCK:
		/* The server doesn't have a lock-waiting queue, so the client
		 * will have to retry.  The server will break the outstanding
		 * callbacks on a file when a lock is released.
		 */
		_debug("would block");
		ASSERT(list_empty(&vnode->granted_locks));
		ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
		vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
		goto need_to_wait;

	case 0:
		_debug("acquired");
		break;
D
David Howells 已提交
503 504 505 506
	}

	/* we've acquired a server lock, but it needs to be renewed after 5
	 * mins */
D
David Howells 已提交
507
	vnode->lock_state = AFS_VNODE_LOCK_GRANTED;
D
David Howells 已提交
508
	afs_schedule_lock_extension(vnode);
D
David Howells 已提交
509 510

share_existing_lock:
D
David Howells 已提交
511 512 513
	/* the lock has been granted as far as we're concerned... */
	fl->fl_u.afs.state = AFS_LOCK_GRANTED;
	list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
D
David Howells 已提交
514

D
David Howells 已提交
515 516
given_lock:
	/* ... but we do still need to get the VFS's blessing */
D
David Howells 已提交
517 518
	spin_unlock(&vnode->lock);

D
David Howells 已提交
519 520 521 522
	ret = posix_lock_file(file, fl, NULL);
	if (ret < 0)
		goto vfs_rejected_lock;

D
David Howells 已提交
523
	/* Again, make sure we've got a callback on this file and, again, make
D
David Howells 已提交
524
	 * sure that our view of the data version is up to date (we ignore
D
David Howells 已提交
525 526
	 * errors incurred here and deal with the consequences elsewhere).
	 */
527
	afs_validate(vnode, key);
D
David Howells 已提交
528 529
	_leave(" = 0");
	return 0;
D
David Howells 已提交
530

D
David Howells 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
need_to_wait:
	/* We're going to have to wait.  Either this client doesn't have a lock
	 * on the server yet and we need to wait for a callback to occur, or
	 * the client does have a lock on the server, but it belongs to some
	 * other process(es) and is incompatible with the lock we want.
	 */
	ret = -EAGAIN;
	if (fl->fl_flags & FL_SLEEP) {
		spin_unlock(&vnode->lock);

		_debug("sleep");
		ret = wait_event_interruptible(fl->fl_wait,
					       fl->fl_u.afs.state != AFS_LOCK_PENDING);

		spin_lock(&vnode->lock);
	}

	if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
		goto given_lock;
	if (fl->fl_u.afs.state < 0)
		ret = fl->fl_u.afs.state;

abort_attempt:
	/* we aren't going to get the lock, either because we're unwilling to
	 * wait, or because some signal happened */
	_debug("abort");
	afs_dequeue_lock(vnode, fl);

error_unlock:
	spin_unlock(&vnode->lock);
D
David Howells 已提交
561 562 563 564
	_leave(" = %d", ret);
	return ret;

vfs_rejected_lock:
D
David Howells 已提交
565 566 567 568
	/* The VFS rejected the lock we just obtained, so we have to discard
	 * what we just got.  We defer this to the lock manager work item to
	 * deal with.
	 */
D
David Howells 已提交
569
	_debug("vfs refused %d", ret);
D
David Howells 已提交
570
	spin_lock(&vnode->lock);
D
David Howells 已提交
571 572
	list_del_init(&fl->fl_u.afs.link);
	if (list_empty(&vnode->granted_locks))
D
David Howells 已提交
573 574
		afs_defer_unlock(vnode);
	goto error_unlock;
D
David Howells 已提交
575 576 577 578 579 580 581
}

/*
 * unlock on a file on the server
 */
static int afs_do_unlk(struct file *file, struct file_lock *fl)
{
D
David Howells 已提交
582
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
D
David Howells 已提交
583 584 585 586
	int ret;

	_enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);

D
David Howells 已提交
587 588 589
	/* Flush all pending writes before doing anything with locks. */
	vfs_fsync(file, 0);

D
David Howells 已提交
590 591 592 593 594
	/* only whole-file unlocks are supported */
	if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
		return -EINVAL;

	ret = posix_lock_file(file, fl, NULL);
D
David Howells 已提交
595 596
	_leave(" = %d [%u]", ret, vnode->lock_state);
	return ret;
D
David Howells 已提交
597 598 599 600 601 602 603
}

/*
 * return information about a lock we currently hold, if indeed we hold one
 */
static int afs_do_getlk(struct file *file, struct file_lock *fl)
{
D
David Howells 已提交
604
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
605
	struct key *key = afs_file_key(file);
D
David Howells 已提交
606 607 608 609 610 611 612
	int ret, lock_count;

	_enter("");

	fl->fl_type = F_UNLCK;

	/* check local lock records first */
A
Andrew Morton 已提交
613 614
	posix_test_lock(file, fl);
	if (fl->fl_type == F_UNLCK) {
D
David Howells 已提交
615
		/* no local locks; consult the server */
616
		ret = afs_fetch_status(vnode, key, false);
D
David Howells 已提交
617 618
		if (ret < 0)
			goto error;
D
David Howells 已提交
619 620 621 622 623 624 625 626

		lock_count = READ_ONCE(vnode->status.lock_count);
		if (lock_count > 0)
			fl->fl_type = F_RDLCK;
		else
			fl->fl_type = F_WRLCK;
		fl->fl_start = 0;
		fl->fl_end = OFFSET_MAX;
D
David Howells 已提交
627 628
	}

D
David Howells 已提交
629
	ret = 0;
D
David Howells 已提交
630 631 632 633 634 635 636 637 638 639
error:
	_leave(" = %d [%hd]", ret, fl->fl_type);
	return ret;
}

/*
 * manage POSIX locks on a file
 */
int afs_lock(struct file *file, int cmd, struct file_lock *fl)
{
D
David Howells 已提交
640
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
D
David Howells 已提交
641 642 643 644 645 646 647

	_enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
	       vnode->fid.vid, vnode->fid.vnode, cmd,
	       fl->fl_type, fl->fl_flags,
	       (long long) fl->fl_start, (long long) fl->fl_end);

	/* AFS doesn't support mandatory locks */
648
	if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
D
David Howells 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662
		return -ENOLCK;

	if (IS_GETLK(cmd))
		return afs_do_getlk(file, fl);
	if (fl->fl_type == F_UNLCK)
		return afs_do_unlk(file, fl);
	return afs_do_setlk(file, fl);
}

/*
 * manage FLOCK locks on a file
 */
int afs_flock(struct file *file, int cmd, struct file_lock *fl)
{
D
David Howells 已提交
663
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
D
David Howells 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

	_enter("{%x:%u},%d,{t=%x,fl=%x}",
	       vnode->fid.vid, vnode->fid.vnode, cmd,
	       fl->fl_type, fl->fl_flags);

	/*
	 * No BSD flocks over NFS allowed.
	 * Note: we could try to fake a POSIX lock request here by
	 * using ((u32) filp | 0x80000000) or some such as the pid.
	 * Not sure whether that would be unique, though, or whether
	 * that would break in other places.
	 */
	if (!(fl->fl_flags & FL_FLOCK))
		return -ENOLCK;

	/* we're simulating flock() locks using posix locks on the server */
	if (fl->fl_type == F_UNLCK)
		return afs_do_unlk(file, fl);
	return afs_do_setlk(file, fl);
}

/*
 * the POSIX lock management core VFS code copies the lock record and adds the
 * copy into its own list, so we need to add that copy to the vnode's lock
 * queue in the same place as the original (which will be deleted shortly
 * after)
 */
static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
{
D
David Howells 已提交
693 694
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));

D
David Howells 已提交
695 696
	_enter("");

D
David Howells 已提交
697
	spin_lock(&vnode->lock);
D
David Howells 已提交
698
	list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
D
David Howells 已提交
699
	spin_unlock(&vnode->lock);
D
David Howells 已提交
700 701 702 703 704 705 706 707
}

/*
 * need to remove this lock from the vnode queue when it's removed from the
 * VFS's list
 */
static void afs_fl_release_private(struct file_lock *fl)
{
D
David Howells 已提交
708 709
	struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));

D
David Howells 已提交
710 711
	_enter("");

D
David Howells 已提交
712 713 714 715
	spin_lock(&vnode->lock);
	afs_dequeue_lock(vnode, fl);
	_debug("state %u for %p", vnode->lock_state, vnode);
	spin_unlock(&vnode->lock);
D
David Howells 已提交
716
}