1 #define MSNFS	/* HACK HACK */
2 /*
3  *  linux/fs/locks.c
4  *
5  *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
6  *  Doug Evans (dje@spiff.uucp), August 07, 1992
7  *
8  *  Deadlock detection added.
9  *  FIXME: one thing isn't handled yet:
10  *	- mandatory locks (requires lots of changes elsewhere)
11  *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
12  *
13  *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
14  *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
15  *
16  *  Converted file_lock_table to a linked list from an array, which eliminates
17  *  the limits on how many active file locks are open.
18  *  Chad Page (pageone@netcom.com), November 27, 1994
19  *
20  *  Removed dependency on file descriptors. dup()'ed file descriptors now
21  *  get the same locks as the original file descriptors, and a close() on
22  *  any file descriptor removes ALL the locks on the file for the current
23  *  process. Since locks still depend on the process id, locks are inherited
24  *  after an exec() but not after a fork(). This agrees with POSIX, and both
25  *  BSD and SVR4 practice.
26  *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
27  *
28  *  Scrapped free list which is redundant now that we allocate locks
29  *  dynamically with kmalloc()/kfree().
30  *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
31  *
32  *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
33  *
34  *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
35  *  fcntl() system call. They have the semantics described above.
36  *
37  *  FL_FLOCK locks are created with calls to flock(), through the flock()
38  *  system call, which is new. Old C libraries implement flock() via fcntl()
39  *  and will continue to use the old, broken implementation.
40  *
41  *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
42  *  with a file pointer (filp). As a result they can be shared by a parent
43  *  process and its children after a fork(). They are removed when the last
44  *  file descriptor referring to the file pointer is closed (unless explicitly
45  *  unlocked).
46  *
47  *  FL_FLOCK locks never deadlock, an existing lock is always removed before
48  *  upgrading from shared to exclusive (or vice versa). When this happens
49  *  any processes blocked by the current lock are woken up and allowed to
50  *  run before the new lock is applied.
51  *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
52  *
53  *  Removed some race conditions in flock_lock_file(), marked other possible
54  *  races. Just grep for FIXME to see them.
55  *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
56  *
57  *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
58  *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
59  *  once we've checked for blocking and deadlocking.
60  *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
61  *
62  *  Initial implementation of mandatory locks. SunOS turned out to be
63  *  a rotten model, so I implemented the "obvious" semantics.
64  *  See 'linux/Documentation/mandatory.txt' for details.
65  *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
66  *
67  *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
68  *  check if a file has mandatory locks, used by mmap(), open() and creat() to
69  *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
70  *  Manual, Section 2.
71  *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
72  *
73  *  Tidied up block list handling. Added '/proc/locks' interface.
74  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
75  *
76  *  Fixed deadlock condition for pathological code that mixes calls to
77  *  flock() and fcntl().
78  *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
79  *
80  *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
81  *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
82  *  guarantee sensible behaviour in the case where file system modules might
83  *  be compiled with different options than the kernel itself.
84  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
85  *
86  *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
87  *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
88  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
89  *
90  *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
91  *  locks. Changed process synchronisation to avoid dereferencing locks that
92  *  have already been freed.
93  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
94  *
95  *  Made the block list a circular list to minimise searching in the list.
96  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
97  *
98  *  Made mandatory locking a mount option. Default is not to allow mandatory
99  *  locking.
100  *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
101  *
102  *  Some adaptations for NFS support.
103  *  Olaf Kirch (okir@monad.swb.de), Dec 1996,
104  *
105  *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
106  *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
107  *
108  *  Use slab allocator instead of kmalloc/kfree.
109  *  Use generic list implementation from <linux/list.h>.
110  *  Sped up posix_locks_deadlock by only considering blocked locks.
111  *  Matthew Wilcox <willy@thepuffingroup.com>, March, 2000.
112  *
113  *  Leases and LOCK_MAND
114  *  Matthew Wilcox <willy@linuxcare.com>, June, 2000.
115  *  Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
116  */
117 
118 #include <linux/slab.h>
119 #include <linux/file.h>
120 #include <linux/smp_lock.h>
121 #include <linux/init.h>
122 #include <linux/capability.h>
123 #include <linux/sched.h>
124 #include <linux/timer.h>
125 
126 #include <asm/semaphore.h>
127 #include <asm/uaccess.h>
128 
129 int leases_enable = 1;
130 int lease_break_time = 45;
131 
132 LIST_HEAD(file_lock_list);
133 static LIST_HEAD(blocked_list);
134 
135 static kmem_cache_t *filelock_cache;
136 
137 /* Allocate an empty lock structure. */
locks_alloc_lock(void)138 static struct file_lock *locks_alloc_lock(void)
139 {
140 	return kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
141 }
142 
143 /* Free a lock which is not in use. */
locks_free_lock(struct file_lock * fl)144 static inline void locks_free_lock(struct file_lock *fl)
145 {
146 	if (fl == NULL) {
147 		BUG();
148 		return;
149 	}
150 	if (waitqueue_active(&fl->fl_wait))
151 		panic("Attempting to free lock with active wait queue");
152 
153 	if (!list_empty(&fl->fl_block))
154 		panic("Attempting to free lock with active block list");
155 
156 	if (!list_empty(&fl->fl_link))
157 		panic("Attempting to free lock on active lock list");
158 
159 	kmem_cache_free(filelock_cache, fl);
160 }
161 
locks_init_lock(struct file_lock * fl)162 void locks_init_lock(struct file_lock *fl)
163 {
164 	INIT_LIST_HEAD(&fl->fl_link);
165 	INIT_LIST_HEAD(&fl->fl_block);
166 	init_waitqueue_head(&fl->fl_wait);
167 	fl->fl_next = NULL;
168 	fl->fl_fasync = NULL;
169 	fl->fl_owner = 0;
170 	fl->fl_pid = 0;
171 	fl->fl_file = NULL;
172 	fl->fl_flags = 0;
173 	fl->fl_type = 0;
174 	fl->fl_start = fl->fl_end = 0;
175 	fl->fl_notify = NULL;
176 	fl->fl_insert = NULL;
177 	fl->fl_remove = NULL;
178 }
179 
180 /*
181  * Initialises the fields of the file lock which are invariant for
182  * free file_locks.
183  */
init_once(void * foo,kmem_cache_t * cache,unsigned long flags)184 static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags)
185 {
186 	struct file_lock *lock = (struct file_lock *) foo;
187 
188 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) !=
189 					SLAB_CTOR_CONSTRUCTOR)
190 		return;
191 
192 	locks_init_lock(lock);
193 }
194 
195 /*
196  * Initialize a new lock from an existing file_lock structure.
197  */
locks_copy_lock(struct file_lock * new,struct file_lock * fl)198 void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
199 {
200 	new->fl_owner = fl->fl_owner;
201 	new->fl_pid = fl->fl_pid;
202 	new->fl_file = fl->fl_file;
203 	new->fl_flags = fl->fl_flags;
204 	new->fl_type = fl->fl_type;
205 	new->fl_start = fl->fl_start;
206 	new->fl_end = fl->fl_end;
207 	new->fl_notify = fl->fl_notify;
208 	new->fl_insert = fl->fl_insert;
209 	new->fl_remove = fl->fl_remove;
210 	new->fl_u = fl->fl_u;
211 }
212 
213 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
flock_make_lock(struct file * filp,unsigned int type)214 static struct file_lock *flock_make_lock(struct file *filp, unsigned int type)
215 {
216 	struct file_lock *fl = locks_alloc_lock();
217 	if (fl == NULL)
218 		return NULL;
219 
220 	fl->fl_owner = NULL;
221 	fl->fl_file = filp;
222 	fl->fl_pid = current->pid;
223 	fl->fl_flags = FL_FLOCK;
224 	fl->fl_type = type;
225 	fl->fl_start = 0;
226 	fl->fl_end = OFFSET_MAX;
227 	fl->fl_notify = NULL;
228 	fl->fl_insert = NULL;
229 	fl->fl_remove = NULL;
230 
231 	return fl;
232 }
233 
assign_type(struct file_lock * fl,int type)234 static int assign_type(struct file_lock *fl, int type)
235 {
236 	switch (type) {
237 	case F_RDLCK:
238 	case F_WRLCK:
239 	case F_UNLCK:
240 		fl->fl_type = type;
241 		break;
242 	default:
243 		return -EINVAL;
244 	}
245 	return 0;
246 }
247 
248 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
249  * style lock.
250  */
flock_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock * l)251 static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
252 			       struct flock *l)
253 {
254 	off_t start, end;
255 
256 	switch (l->l_whence) {
257 	case 0: /*SEEK_SET*/
258 		start = 0;
259 		break;
260 	case 1: /*SEEK_CUR*/
261 		start = filp->f_pos;
262 		break;
263 	case 2: /*SEEK_END*/
264 		start = filp->f_dentry->d_inode->i_size;
265 		break;
266 	default:
267 		return -EINVAL;
268 	}
269 
270 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
271 	   POSIX-2001 defines it. */
272 	start += l->l_start;
273 	if (l->l_len < 0) {
274 		end = start - 1;
275 		start += l->l_len;
276 	} else {
277 		end = start + l->l_len - 1;
278 	}
279 
280 	if (start < 0)
281 		return -EINVAL;
282 	if (l->l_len > 0 && end < 0)
283 		return -EOVERFLOW;
284 	fl->fl_start = start;	/* we record the absolute position */
285 	fl->fl_end = end;
286 	if (l->l_len == 0)
287 		fl->fl_end = OFFSET_MAX;
288 
289 	fl->fl_owner = current->files;
290 	fl->fl_pid = current->pid;
291 	fl->fl_file = filp;
292 	fl->fl_flags = FL_POSIX;
293 	fl->fl_notify = NULL;
294 	fl->fl_insert = NULL;
295 	fl->fl_remove = NULL;
296 
297 	return assign_type(fl, l->l_type);
298 }
299 
300 #if BITS_PER_LONG == 32
flock64_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock64 * l)301 static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
302 				 struct flock64 *l)
303 {
304 	loff_t start;
305 
306 	switch (l->l_whence) {
307 	case 0: /*SEEK_SET*/
308 		start = 0;
309 		break;
310 	case 1: /*SEEK_CUR*/
311 		start = filp->f_pos;
312 		break;
313 	case 2: /*SEEK_END*/
314 		start = filp->f_dentry->d_inode->i_size;
315 		break;
316 	default:
317 		return -EINVAL;
318 	}
319 
320 	if (((start += l->l_start) < 0) || (l->l_len < 0))
321 		return -EINVAL;
322 	fl->fl_end = start + l->l_len - 1;
323 	if (l->l_len > 0 && fl->fl_end < 0)
324 		return -EOVERFLOW;
325 	fl->fl_start = start;	/* we record the absolute position */
326 	if (l->l_len == 0)
327 		fl->fl_end = OFFSET_MAX;
328 
329 	fl->fl_owner = current->files;
330 	fl->fl_pid = current->pid;
331 	fl->fl_file = filp;
332 	fl->fl_flags = FL_POSIX;
333 	fl->fl_notify = NULL;
334 	fl->fl_insert = NULL;
335 	fl->fl_remove = NULL;
336 
337 	switch (l->l_type) {
338 	case F_RDLCK:
339 	case F_WRLCK:
340 	case F_UNLCK:
341 		fl->fl_type = l->l_type;
342 		break;
343 	default:
344 		return -EINVAL;
345 	}
346 
347 	return (0);
348 }
349 #endif
350 
351 /* Allocate a file_lock initialised to this type of lease */
lease_alloc(struct file * filp,int type,struct file_lock ** flp)352 static int lease_alloc(struct file *filp, int type, struct file_lock **flp)
353 {
354 	struct file_lock *fl = locks_alloc_lock();
355 	if (fl == NULL)
356 		return -ENOMEM;
357 
358 	fl->fl_owner = current->files;
359 	fl->fl_pid = current->pid;
360 
361 	fl->fl_file = filp;
362 	fl->fl_flags = FL_LEASE;
363 	if (assign_type(fl, type) != 0) {
364 		locks_free_lock(fl);
365 		return -EINVAL;
366 	}
367 	fl->fl_start = 0;
368 	fl->fl_end = OFFSET_MAX;
369 	fl->fl_notify = NULL;
370 	fl->fl_insert = NULL;
371 	fl->fl_remove = NULL;
372 
373 	*flp = fl;
374 	return 0;
375 }
376 
377 /* Check if two locks overlap each other.
378  */
locks_overlap(struct file_lock * fl1,struct file_lock * fl2)379 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
380 {
381 	return ((fl1->fl_end >= fl2->fl_start) &&
382 		(fl2->fl_end >= fl1->fl_start));
383 }
384 
385 /*
386  * Check whether two locks have the same owner
387  * N.B. Do we need the test on PID as well as owner?
388  * (Clone tasks should be considered as one "owner".)
389  */
390 static inline int
locks_same_owner(struct file_lock * fl1,struct file_lock * fl2)391 locks_same_owner(struct file_lock *fl1, struct file_lock *fl2)
392 {
393 	return (fl1->fl_owner == fl2->fl_owner) &&
394 	       (fl1->fl_pid   == fl2->fl_pid);
395 }
396 
397 /* Remove waiter from blocker's block list.
398  * When blocker ends up pointing to itself then the list is empty.
399  */
locks_delete_block(struct file_lock * waiter)400 static void locks_delete_block(struct file_lock *waiter)
401 {
402 	list_del(&waiter->fl_block);
403 	INIT_LIST_HEAD(&waiter->fl_block);
404 	list_del(&waiter->fl_link);
405 	INIT_LIST_HEAD(&waiter->fl_link);
406 	waiter->fl_next = NULL;
407 }
408 
409 /* Insert waiter into blocker's block list.
410  * We use a circular list so that processes can be easily woken up in
411  * the order they blocked. The documentation doesn't require this but
412  * it seems like the reasonable thing to do.
413  */
locks_insert_block(struct file_lock * blocker,struct file_lock * waiter)414 static void locks_insert_block(struct file_lock *blocker,
415 			       struct file_lock *waiter)
416 {
417 	if (!list_empty(&waiter->fl_block)) {
418 		printk(KERN_ERR "locks_insert_block: removing duplicated lock "
419 			"(pid=%d %Ld-%Ld type=%d)\n", waiter->fl_pid,
420 			waiter->fl_start, waiter->fl_end, waiter->fl_type);
421 		locks_delete_block(waiter);
422 	}
423 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
424 	waiter->fl_next = blocker;
425 	list_add(&waiter->fl_link, &blocked_list);
426 }
427 
428 static inline
locks_notify_blocked(struct file_lock * waiter)429 void locks_notify_blocked(struct file_lock *waiter)
430 {
431 	if (waiter->fl_notify)
432 		waiter->fl_notify(waiter);
433 	else
434 		wake_up(&waiter->fl_wait);
435 }
436 
437 /* Wake up processes blocked waiting for blocker.
438  * If told to wait then schedule the processes until the block list
439  * is empty, otherwise empty the block list ourselves.
440  */
locks_wake_up_blocks(struct file_lock * blocker,unsigned int wait)441 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
442 {
443 	while (!list_empty(&blocker->fl_block)) {
444 		struct file_lock *waiter = list_entry(blocker->fl_block.next, struct file_lock, fl_block);
445 
446 		if (wait) {
447 			locks_notify_blocked(waiter);
448 			/* Let the blocked process remove waiter from the
449 			 * block list when it gets scheduled.
450 			 */
451 			yield();
452 		} else {
453 			/* Remove waiter from the block list, because by the
454 			 * time it wakes up blocker won't exist any more.
455 			 */
456 			locks_delete_block(waiter);
457 			locks_notify_blocked(waiter);
458 		}
459 	}
460 }
461 
462 /* Insert file lock fl into an inode's lock list at the position indicated
463  * by pos. At the same time add the lock to the global file lock list.
464  */
locks_insert_lock(struct file_lock ** pos,struct file_lock * fl)465 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
466 {
467 	list_add(&fl->fl_link, &file_lock_list);
468 
469 	/* insert into file's list */
470 	fl->fl_next = *pos;
471 	*pos = fl;
472 
473 	if (fl->fl_insert)
474 		fl->fl_insert(fl);
475 }
476 
477 /*
478  * Remove lock from the lock lists
479  */
_unhash_lock(struct file_lock ** thisfl_p)480 static inline void _unhash_lock(struct file_lock **thisfl_p)
481 {
482 	struct file_lock *fl = *thisfl_p;
483 
484 	*thisfl_p = fl->fl_next;
485 	fl->fl_next = NULL;
486 
487 	list_del_init(&fl->fl_link);
488 }
489 
490 /*
491  * Wake up processes that are blocked waiting for this lock,
492  * notify the FS that the lock has been cleared and
493  * finally free the lock.
494  */
_delete_lock(struct file_lock * fl,unsigned int wait)495 static inline void _delete_lock(struct file_lock *fl, unsigned int wait)
496 {
497 	fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
498 	if (fl->fl_fasync != NULL){
499 		printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
500 		fl->fl_fasync = NULL;
501 	}
502 
503 	if (fl->fl_remove)
504 		fl->fl_remove(fl);
505 
506 	locks_wake_up_blocks(fl, wait);
507 	locks_free_lock(fl);
508 }
509 
510 /*
511  * Delete a lock and then free it.
512  */
locks_delete_lock(struct file_lock ** thisfl_p,unsigned int wait)513 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
514 {
515 	struct file_lock *fl = *thisfl_p;
516 
517 	_unhash_lock(thisfl_p);
518 	_delete_lock(fl, wait);
519 }
520 
521 /*
522  * Call back client filesystem in order to get it to unregister a lock,
523  * then delete lock. Essentially useful only in locks_remove_*().
524  * Note: this must be called with the semaphore already held!
525  */
locks_unlock_delete(struct file_lock ** thisfl_p)526 static inline void locks_unlock_delete(struct file_lock **thisfl_p)
527 {
528 	struct file_lock *fl = *thisfl_p;
529 	int (*lock)(struct file *, int, struct file_lock *);
530 
531 	_unhash_lock(thisfl_p);
532 	if (fl->fl_file->f_op &&
533 	    (lock = fl->fl_file->f_op->lock) != NULL) {
534 		fl->fl_type = F_UNLCK;
535 		lock(fl->fl_file, F_SETLK, fl);
536 	}
537 	_delete_lock(fl, 0);
538 }
539 
540 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
541  * checks for shared/exclusive status of overlapping locks.
542  */
locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)543 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
544 {
545 	switch (caller_fl->fl_type) {
546 	case F_RDLCK:
547 		return (sys_fl->fl_type == F_WRLCK);
548 
549 	case F_WRLCK:
550 		return (1);
551 
552 	default:
553 		printk(KERN_ERR "locks_conflict(): impossible lock type - %d\n",
554 		       caller_fl->fl_type);
555 		break;
556 	}
557 	return (0);	/* This should never happen */
558 }
559 
560 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
561  * checking before calling the locks_conflict().
562  */
posix_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)563 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
564 {
565 	/* POSIX locks owned by the same process do not conflict with
566 	 * each other.
567 	 */
568 	if (!(sys_fl->fl_flags & FL_POSIX) ||
569 	    locks_same_owner(caller_fl, sys_fl))
570 		return (0);
571 
572 	/* Check whether they overlap */
573 	if (!locks_overlap(caller_fl, sys_fl))
574 		return 0;
575 
576 	return (locks_conflict(caller_fl, sys_fl));
577 }
578 
579 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
580  * checking before calling the locks_conflict().
581  */
flock_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)582 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
583 {
584 	/* FLOCK locks referring to the same filp do not conflict with
585 	 * each other.
586 	 */
587 	if (!(sys_fl->fl_flags & FL_FLOCK) ||
588 	    (caller_fl->fl_file == sys_fl->fl_file))
589 		return (0);
590 #ifdef MSNFS
591 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
592 		return 0;
593 #endif
594 
595 	return (locks_conflict(caller_fl, sys_fl));
596 }
597 
interruptible_sleep_on_locked(wait_queue_head_t * fl_wait,int timeout)598 static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout)
599 {
600 	int result = 0;
601 	DECLARE_WAITQUEUE(wait, current);
602 
603 	current->state = TASK_INTERRUPTIBLE;
604 	add_wait_queue(fl_wait, &wait);
605 	if (timeout == 0)
606 		schedule();
607 	else
608 		result = schedule_timeout(timeout);
609 	if (signal_pending(current))
610 		result = -ERESTARTSYS;
611 	remove_wait_queue(fl_wait, &wait);
612 	current->state = TASK_RUNNING;
613 	return result;
614 }
615 
locks_block_on(struct file_lock * blocker,struct file_lock * waiter)616 static int locks_block_on(struct file_lock *blocker, struct file_lock *waiter)
617 {
618 	int result;
619 	locks_insert_block(blocker, waiter);
620 	result = interruptible_sleep_on_locked(&waiter->fl_wait, 0);
621 	locks_delete_block(waiter);
622 	return result;
623 }
624 
locks_block_on_timeout(struct file_lock * blocker,struct file_lock * waiter,int time)625 static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time)
626 {
627 	int result;
628 	locks_insert_block(blocker, waiter);
629 	result = interruptible_sleep_on_locked(&waiter->fl_wait, time);
630 	locks_delete_block(waiter);
631 	return result;
632 }
633 
634 struct file_lock *
posix_test_lock(struct file * filp,struct file_lock * fl)635 posix_test_lock(struct file *filp, struct file_lock *fl)
636 {
637 	struct file_lock *cfl;
638 
639 	lock_kernel();
640 	for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
641 		if (!(cfl->fl_flags & FL_POSIX))
642 			continue;
643 		if (posix_locks_conflict(cfl, fl))
644 			break;
645 	}
646 	unlock_kernel();
647 
648 	return (cfl);
649 }
650 
651 /* This function tests for deadlock condition before putting a process to
652  * sleep. The detection scheme is no longer recursive. Recursive was neat,
653  * but dangerous - we risked stack corruption if the lock data was bad, or
654  * if the recursion was too deep for any other reason.
655  *
656  * We rely on the fact that a task can only be on one lock's wait queue
657  * at a time. When we find blocked_task on a wait queue we can re-search
658  * with blocked_task equal to that queue's owner, until either blocked_task
659  * isn't found, or blocked_task is found on a queue owned by my_task.
660  *
661  * Note: the above assumption may not be true when handling lock requests
662  * from a broken NFS client. But broken NFS clients have a lot more to
663  * worry about than proper deadlock detection anyway... --okir
664  */
posix_locks_deadlock(struct file_lock * caller_fl,struct file_lock * block_fl)665 int posix_locks_deadlock(struct file_lock *caller_fl,
666 				struct file_lock *block_fl)
667 {
668 	struct list_head *tmp;
669 	fl_owner_t caller_owner, blocked_owner;
670 	unsigned int	 caller_pid, blocked_pid;
671 
672 	caller_owner = caller_fl->fl_owner;
673 	caller_pid = caller_fl->fl_pid;
674 	blocked_owner = block_fl->fl_owner;
675 	blocked_pid = block_fl->fl_pid;
676 
677 next_task:
678 	if (caller_owner == blocked_owner && caller_pid == blocked_pid)
679 		return 1;
680 	list_for_each(tmp, &blocked_list) {
681 		struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
682 		if ((fl->fl_owner == blocked_owner)
683 		    && (fl->fl_pid == blocked_pid)) {
684 			fl = fl->fl_next;
685 			blocked_owner = fl->fl_owner;
686 			blocked_pid = fl->fl_pid;
687 			goto next_task;
688 		}
689 	}
690 	return 0;
691 }
692 
locks_mandatory_locked(struct inode * inode)693 int locks_mandatory_locked(struct inode *inode)
694 {
695 	fl_owner_t owner = current->files;
696 	struct file_lock *fl;
697 
698 	/*
699 	 * Search the lock list for this inode for any POSIX locks.
700 	 */
701 	lock_kernel();
702 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
703 		if (!(fl->fl_flags & FL_POSIX))
704 			continue;
705 		if (fl->fl_owner != owner)
706 			break;
707 	}
708 	unlock_kernel();
709 	return fl ? -EAGAIN : 0;
710 }
711 
locks_mandatory_area(int read_write,struct inode * inode,struct file * filp,loff_t offset,size_t count)712 int locks_mandatory_area(int read_write, struct inode *inode,
713 			 struct file *filp, loff_t offset,
714 			 size_t count)
715 {
716 	struct file_lock *fl;
717 	struct file_lock *new_fl = locks_alloc_lock();
718 	int error;
719 
720 	if (new_fl == NULL)
721 		return -ENOMEM;
722 
723 	new_fl->fl_owner = current->files;
724 	new_fl->fl_pid = current->pid;
725 	new_fl->fl_file = filp;
726 	new_fl->fl_flags = FL_POSIX | FL_ACCESS;
727 	new_fl->fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
728 	new_fl->fl_start = offset;
729 	new_fl->fl_end = offset + count - 1;
730 
731 	error = 0;
732 	lock_kernel();
733 
734 repeat:
735 	/* Search the lock list for this inode for locks that conflict with
736 	 * the proposed read/write.
737 	 */
738 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
739 		if (!(fl->fl_flags & FL_POSIX))
740 			continue;
741 		if (fl->fl_start > new_fl->fl_end)
742 			break;
743 		if (posix_locks_conflict(new_fl, fl)) {
744 			error = -EAGAIN;
745 			if (filp && (filp->f_flags & O_NONBLOCK))
746 				break;
747 			error = -EDEADLK;
748 			if (posix_locks_deadlock(new_fl, fl))
749 				break;
750 
751 			error = locks_block_on(fl, new_fl);
752 			if (error != 0)
753 				break;
754 
755 			/*
756 			 * If we've been sleeping someone might have
757 			 * changed the permissions behind our back.
758 			 */
759 			if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
760 				break;
761 			goto repeat;
762 		}
763 	}
764 	locks_free_lock(new_fl);
765 	unlock_kernel();
766 	return error;
767 }
768 
769 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
770  * at the head of the list, but that's secret knowledge known only to
771  * flock_lock_file and posix_lock_file.
772  */
flock_lock_file(struct file * filp,unsigned int lock_type,unsigned int wait)773 static int flock_lock_file(struct file *filp, unsigned int lock_type,
774 			   unsigned int wait)
775 {
776 	struct file_lock *fl;
777 	struct file_lock *new_fl = NULL;
778 	struct file_lock **before;
779 	struct inode * inode = filp->f_dentry->d_inode;
780 	int error, change;
781 	int unlock = (lock_type == F_UNLCK);
782 
783 	/*
784 	 * If we need a new lock, get it in advance to avoid races.
785 	 */
786 	if (!unlock) {
787 		error = -ENOLCK;
788 		new_fl = flock_make_lock(filp, lock_type);
789 		if (!new_fl)
790 			return error;
791 	}
792 
793 	error = 0;
794 search:
795 	change = 0;
796 	before = &inode->i_flock;
797 	while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
798 		if (filp == fl->fl_file) {
799 			if (lock_type == fl->fl_type)
800 				goto out;
801 			change = 1;
802 			break;
803 		}
804 		before = &fl->fl_next;
805 	}
806 	/* change means that we are changing the type of an existing lock,
807 	 * or else unlocking it.
808 	 */
809 	if (change) {
810 		/* N.B. What if the wait argument is false? */
811 		locks_delete_lock(before, !unlock);
812 		/*
813 		 * If we waited, another lock may have been added ...
814 		 */
815 		if (!unlock)
816 			goto search;
817 	}
818 	if (unlock)
819 		goto out;
820 
821 repeat:
822 	for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
823 	     fl = fl->fl_next) {
824 		if (!flock_locks_conflict(new_fl, fl))
825 			continue;
826 		error = -EAGAIN;
827 		if (!wait)
828 			goto out;
829 		error = locks_block_on(fl, new_fl);
830 		if (error != 0)
831 			goto out;
832 		goto repeat;
833 	}
834 	locks_insert_lock(&inode->i_flock, new_fl);
835 	new_fl = NULL;
836 	error = 0;
837 
838 out:
839 	if (new_fl)
840 		locks_free_lock(new_fl);
841 	return error;
842 }
843 
844 /**
845  *	posix_lock_file:
846  *	@filp: The file to apply the lock to
847  *	@caller: The lock to be applied
848  *	@wait: 1 to retry automatically, 0 to return -EAGAIN
849  *
850  * Add a POSIX style lock to a file.
851  * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
852  * task, then by starting address
853  *
854  * Kai Petzke writes:
855  * To make freeing a lock much faster, we keep a pointer to the lock before the
856  * actual one. But the real gain of the new coding was, that lock_it() and
857  * unlock_it() became one function.
858  *
859  * To all purists: Yes, I use a few goto's. Just pass on to the next function.
860  */
861 
posix_lock_file(struct file * filp,struct file_lock * caller,unsigned int wait)862 int posix_lock_file(struct file *filp, struct file_lock *caller,
863 			   unsigned int wait)
864 {
865 	struct file_lock *fl;
866 	struct file_lock *new_fl, *new_fl2;
867 	struct file_lock *left = NULL;
868 	struct file_lock *right = NULL;
869 	struct file_lock **before;
870 	struct inode * inode = filp->f_dentry->d_inode;
871 	int error, added = 0;
872 
873 	/*
874 	 * We may need two file_lock structures for this operation,
875 	 * so we get them in advance to avoid races.
876 	 */
877 	new_fl = locks_alloc_lock();
878 	new_fl2 = locks_alloc_lock();
879 	error = -ENOLCK; /* "no luck" */
880 	if (!(new_fl && new_fl2))
881 		goto out_nolock;
882 
883 	lock_kernel();
884 	if (caller->fl_type != F_UNLCK) {
885   repeat:
886 		for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
887 			if (!(fl->fl_flags & FL_POSIX))
888 				continue;
889 			if (!posix_locks_conflict(caller, fl))
890 				continue;
891 			error = -EAGAIN;
892 			if (!wait)
893 				goto out;
894 			error = -EDEADLK;
895 			if (posix_locks_deadlock(caller, fl))
896 				goto out;
897 
898 			error = locks_block_on(fl, caller);
899 			if (error != 0)
900 				goto out;
901 			goto repeat;
902   		}
903   	}
904 
905 	/*
906 	 * We've allocated the new locks in advance, so there are no
907 	 * errors possible (and no blocking operations) from here on.
908 	 *
909 	 * Find the first old lock with the same owner as the new lock.
910 	 */
911 
912 	before = &inode->i_flock;
913 
914 	/* First skip locks owned by other processes.
915 	 */
916 	while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
917 				  !locks_same_owner(caller, fl))) {
918 		before = &fl->fl_next;
919 	}
920 
921 	/* Process locks with this owner.
922 	 */
923 	while ((fl = *before) && locks_same_owner(caller, fl)) {
924 		/* Detect adjacent or overlapping regions (if same lock type)
925 		 */
926 		if (caller->fl_type == fl->fl_type) {
927 			if (fl->fl_end < caller->fl_start - 1)
928 				goto next_lock;
929 			/* If the next lock in the list has entirely bigger
930 			 * addresses than the new one, insert the lock here.
931 			 */
932 			if (fl->fl_start > caller->fl_end + 1)
933 				break;
934 
935 			/* If we come here, the new and old lock are of the
936 			 * same type and adjacent or overlapping. Make one
937 			 * lock yielding from the lower start address of both
938 			 * locks to the higher end address.
939 			 */
940 			if (fl->fl_start > caller->fl_start)
941 				fl->fl_start = caller->fl_start;
942 			else
943 				caller->fl_start = fl->fl_start;
944 			if (fl->fl_end < caller->fl_end)
945 				fl->fl_end = caller->fl_end;
946 			else
947 				caller->fl_end = fl->fl_end;
948 			if (added) {
949 				locks_delete_lock(before, 0);
950 				continue;
951 			}
952 			caller = fl;
953 			added = 1;
954 		}
955 		else {
956 			/* Processing for different lock types is a bit
957 			 * more complex.
958 			 */
959 			if (fl->fl_end < caller->fl_start)
960 				goto next_lock;
961 			if (fl->fl_start > caller->fl_end)
962 				break;
963 			if (caller->fl_type == F_UNLCK)
964 				added = 1;
965 			if (fl->fl_start < caller->fl_start)
966 				left = fl;
967 			/* If the next lock in the list has a higher end
968 			 * address than the new one, insert the new one here.
969 			 */
970 			if (fl->fl_end > caller->fl_end) {
971 				right = fl;
972 				break;
973 			}
974 			if (fl->fl_start >= caller->fl_start) {
975 				/* The new lock completely replaces an old
976 				 * one (This may happen several times).
977 				 */
978 				if (added) {
979 					locks_delete_lock(before, 0);
980 					continue;
981 				}
982 				/* Replace the old lock with the new one.
983 				 * Wake up anybody waiting for the old one,
984 				 * as the change in lock type might satisfy
985 				 * their needs.
986 				 */
987 				locks_wake_up_blocks(fl, 0);	/* This cannot schedule()! */
988 				fl->fl_start = caller->fl_start;
989 				fl->fl_end = caller->fl_end;
990 				fl->fl_type = caller->fl_type;
991 				fl->fl_u = caller->fl_u;
992 				caller = fl;
993 				added = 1;
994 			}
995 		}
996 		/* Go on to next lock.
997 		 */
998 	next_lock:
999 		before = &fl->fl_next;
1000 	}
1001 
1002 	error = 0;
1003 	if (!added) {
1004 		if (caller->fl_type == F_UNLCK)
1005 			goto out;
1006 		locks_copy_lock(new_fl, caller);
1007 		locks_insert_lock(before, new_fl);
1008 		new_fl = NULL;
1009 	}
1010 	if (right) {
1011 		if (left == right) {
1012 			/* The new lock breaks the old one in two pieces,
1013 			 * so we have to use the second new lock.
1014 			 */
1015 			left = new_fl2;
1016 			new_fl2 = NULL;
1017 			locks_copy_lock(left, right);
1018 			locks_insert_lock(before, left);
1019 		}
1020 		right->fl_start = caller->fl_end + 1;
1021 		locks_wake_up_blocks(right, 0);
1022 	}
1023 	if (left) {
1024 		left->fl_end = caller->fl_start - 1;
1025 		locks_wake_up_blocks(left, 0);
1026 	}
1027 out:
1028 	unlock_kernel();
1029 out_nolock:
1030 	/*
1031 	 * Free any unused locks.
1032 	 */
1033 	if (new_fl)
1034 		locks_free_lock(new_fl);
1035 	if (new_fl2)
1036 		locks_free_lock(new_fl2);
1037 	return error;
1038 }
1039 
flock_translate_cmd(int cmd)1040 static inline int flock_translate_cmd(int cmd) {
1041 #ifdef MSNFS
1042 	if (cmd & LOCK_MAND)
1043 		return cmd & (LOCK_MAND | LOCK_RW);
1044 #endif
1045 	switch (cmd &~ LOCK_NB) {
1046 	case LOCK_SH:
1047 		return F_RDLCK;
1048 	case LOCK_EX:
1049 		return F_WRLCK;
1050 	case LOCK_UN:
1051 		return F_UNLCK;
1052 	}
1053 	return -EINVAL;
1054 }
1055 
1056 /* We already had a lease on this file; just change its type */
lease_modify(struct file_lock ** before,int arg)1057 static int lease_modify(struct file_lock **before, int arg)
1058 {
1059 	struct file_lock *fl = *before;
1060 	int error = assign_type(fl, arg);
1061 
1062 	if (error)
1063 		return error;
1064 	locks_wake_up_blocks(fl, 0);
1065 	if (arg == F_UNLCK) {
1066 		struct file *filp = fl->fl_file;
1067 
1068 		filp->f_owner.pid = 0;
1069 		filp->f_owner.uid = 0;
1070 		filp->f_owner.euid = 0;
1071 		filp->f_owner.signum = 0;
1072 		locks_delete_lock(before, 0);
1073 	}
1074 	return 0;
1075 }
1076 
time_out_leases(struct inode * inode)1077 static void time_out_leases(struct inode *inode)
1078 {
1079 	struct file_lock **before;
1080 	struct file_lock *fl;
1081 
1082 	before = &inode->i_flock;
1083 	while ((fl = *before) && (fl->fl_flags & FL_LEASE)
1084 			&& (fl->fl_type & F_INPROGRESS)) {
1085 		if ((fl->fl_break_time == 0)
1086 				|| time_before(jiffies, fl->fl_break_time)) {
1087 			before = &fl->fl_next;
1088 			continue;
1089 		}
1090 		lease_modify(before, fl->fl_type & ~F_INPROGRESS);
1091 		if (fl == *before)	/* lease_modify may have freed fl */
1092 			before = &fl->fl_next;
1093 	}
1094 }
1095 
1096 /**
1097  *	__get_lease	-	revoke all outstanding leases on file
1098  *	@inode: the inode of the file to return
1099  *	@mode: the open mode (read or write)
1100  *
1101  *	get_lease (inlined for speed) has checked there already
1102  *	is a lease on this file.  Leases are broken on a call to open()
1103  *	or truncate().  This function can sleep unless you
1104  *	specified %O_NONBLOCK to your open().
1105  */
__get_lease(struct inode * inode,unsigned int mode)1106 int __get_lease(struct inode *inode, unsigned int mode)
1107 {
1108 	int error = 0, future;
1109 	struct file_lock *new_fl, *flock;
1110 	struct file_lock *fl;
1111 	int alloc_err;
1112 	unsigned long break_time;
1113 	int i_have_this_lease = 0;
1114 
1115 	alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK,
1116 			&new_fl);
1117 
1118 	lock_kernel();
1119 
1120 	time_out_leases(inode);
1121 
1122 	flock = inode->i_flock;
1123 	if ((flock == NULL) || (flock->fl_flags & FL_LEASE) == 0)
1124 		goto out;
1125 
1126 	for (fl = flock; fl && (fl->fl_flags & FL_LEASE); fl = fl->fl_next)
1127 		if (fl->fl_owner == current->files)
1128 			i_have_this_lease = 1;
1129 
1130 	if (mode & FMODE_WRITE) {
1131 		/* If we want write access, we have to revoke any lease. */
1132 		future = F_UNLCK | F_INPROGRESS;
1133 	} else if (flock->fl_type & F_INPROGRESS) {
1134 		/* If the lease is already being broken, we just leave it */
1135 		future = flock->fl_type;
1136 	} else if (flock->fl_type & F_WRLCK) {
1137 		/* Downgrade the exclusive lease to a read-only lease. */
1138 		future = F_RDLCK | F_INPROGRESS;
1139 	} else {
1140 		/* the existing lease was read-only, so we can read too. */
1141 		goto out;
1142 	}
1143 
1144 	if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) {
1145 		error = alloc_err;
1146 		goto out;
1147 	}
1148 
1149 	break_time = 0;
1150 	if (lease_break_time > 0) {
1151 		break_time = jiffies + lease_break_time * HZ;
1152 		if (break_time == 0)
1153 			break_time++;	/* so that 0 means no break time */
1154 	}
1155 
1156 	for (fl = flock; fl && (fl->fl_flags & FL_LEASE); fl = fl->fl_next) {
1157 		if (fl->fl_type != future) {
1158 			fl->fl_type = future;
1159 			fl->fl_break_time = break_time;
1160 			kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
1161 		}
1162 	}
1163 
1164 	if (i_have_this_lease || (mode & O_NONBLOCK)) {
1165 		error = -EWOULDBLOCK;
1166 		goto out;
1167 	}
1168 
1169 restart:
1170 	break_time = flock->fl_break_time;
1171 	if (break_time != 0) {
1172 		break_time -= jiffies;
1173 		if (break_time == 0)
1174 			break_time++;
1175 	}
1176 	error = locks_block_on_timeout(flock, new_fl, break_time);
1177 	if (error >= 0) {
1178 		if (error == 0)
1179 			time_out_leases(inode);
1180 		/* Wait for the next lease that has not been broken yet */
1181 		for (flock = inode->i_flock;
1182 				flock && (flock->fl_flags & FL_LEASE);
1183 				flock = flock->fl_next) {
1184 			if (flock->fl_type & F_INPROGRESS)
1185 				goto restart;
1186 		}
1187 		error = 0;
1188 	}
1189 
1190 out:
1191 	unlock_kernel();
1192 	if (!alloc_err)
1193 		locks_free_lock(new_fl);
1194 	return error;
1195 }
1196 
1197 /**
1198  *	lease_get_mtime
1199  *	@inode: the inode
1200  *
1201  * This is to force NFS clients to flush their caches for files with
1202  * exclusive leases.  The justification is that if someone has an
1203  * exclusive lease, then they could be modifiying it.
1204  */
lease_get_mtime(struct inode * inode)1205 time_t lease_get_mtime(struct inode *inode)
1206 {
1207 	struct file_lock *flock = inode->i_flock;
1208 	if (flock && (flock->fl_flags & FL_LEASE) && (flock->fl_type & F_WRLCK))
1209 		return CURRENT_TIME;
1210 	return inode->i_mtime;
1211 }
1212 
1213 /**
1214  *	fcntl_getlease - Enquire what lease is currently active
1215  *	@filp: the file
1216  *
1217  *	The value returned by this function will be one of
1218  *	(if no lease break is pending):
1219  *
1220  *	%F_RDLCK to indicate a shared lease is held.
1221  *
1222  *	%F_WRLCK to indicate an exclusive lease is held.
1223  *
1224  *	%F_UNLCK to indicate no lease is held.
1225  *
1226  *	(if a lease break is pending):
1227  *
1228  *	%F_RDLCK to indicate an exclusive lease needs to be
1229  *		changed to a shared lease (or removed).
1230  *
1231  *	%F_UNLCK to indicate the lease needs to be removed.
1232  *
1233  *	XXX: sfr & willy disagree over whether F_INPROGRESS
1234  *	should be returned to userspace.
1235  */
fcntl_getlease(struct file * filp)1236 int fcntl_getlease(struct file *filp)
1237 {
1238 	struct file_lock *fl;
1239 	int type = F_UNLCK;
1240 
1241 	lock_kernel();
1242 	time_out_leases(filp->f_dentry->d_inode);
1243 	for (fl = filp->f_dentry->d_inode->i_flock;
1244 			fl && (fl->fl_flags & FL_LEASE);
1245 			fl = fl->fl_next) {
1246 		if (fl->fl_file == filp) {
1247 			type = fl->fl_type & ~F_INPROGRESS;
1248 			break;
1249 		}
1250 	}
1251 	unlock_kernel();
1252 	return type;
1253 }
1254 
1255 /**
1256  *	fcntl_setlease	-	sets a lease on an open file
1257  *	@fd: open file descriptor
1258  *	@filp: file pointer
1259  *	@arg: type of lease to obtain
1260  *
1261  *	Call this fcntl to establish a lease on the file.
1262  *	Note that you also need to call %F_SETSIG to
1263  *	receive a signal when the lease is broken.
1264  */
fcntl_setlease(unsigned int fd,struct file * filp,long arg)1265 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1266 {
1267 	struct file_lock *fl, **before, **my_before = NULL;
1268 	struct dentry *dentry;
1269 	struct inode *inode;
1270 	int error, rdlease_count = 0, wrlease_count = 0;
1271 
1272 	dentry = filp->f_dentry;
1273 	inode = dentry->d_inode;
1274 
1275 	if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
1276 		return -EACCES;
1277 	if (!S_ISREG(inode->i_mode))
1278 		return -EINVAL;
1279 
1280 	lock_kernel();
1281 
1282 	time_out_leases(inode);
1283 
1284 	/*
1285 	 * FIXME: What about F_RDLCK and files open for writing?
1286 	 */
1287 	error = -EAGAIN;
1288 	if ((arg == F_WRLCK)
1289 	    && ((atomic_read(&dentry->d_count) > 1)
1290 		|| (atomic_read(&inode->i_count) > 1)))
1291 		goto out_unlock;
1292 
1293 	/*
1294 	 * At this point, we know that if there is an exclusive
1295 	 * lease on this file, then we hold it on this filp
1296 	 * (otherwise our open of this file would have blocked).
1297 	 * And if we are trying to acquire an exclusive lease,
1298 	 * then the file is not open by anyone (including us)
1299 	 * except for this filp.
1300 	 */
1301 	for (before = &inode->i_flock;
1302 			((fl = *before) != NULL) && (fl->fl_flags & FL_LEASE);
1303 			before = &fl->fl_next) {
1304 		if (fl->fl_file == filp)
1305 			my_before = before;
1306 		else if (fl->fl_type == (F_INPROGRESS | F_UNLCK))
1307 			/*
1308 			 * Someone is in the process of opening this
1309 			 * file for writing so we may not take an
1310 			 * exclusive lease on it.
1311 			 */
1312 			wrlease_count++;
1313 		else
1314 			rdlease_count++;
1315 	}
1316 
1317 	if ((arg == F_RDLCK && (wrlease_count > 0)) ||
1318 	    (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
1319 		goto out_unlock;
1320 
1321 	if (my_before != NULL) {
1322 		error = lease_modify(my_before, arg);
1323 		goto out_unlock;
1324 	}
1325 
1326 	error = 0;
1327 	if (arg == F_UNLCK)
1328 		goto out_unlock;
1329 
1330 	error = -EINVAL;
1331 	if (!leases_enable)
1332 		goto out_unlock;
1333 
1334 	error = lease_alloc(filp, arg, &fl);
1335 	if (error)
1336 		goto out_unlock;
1337 
1338 	error = fasync_helper(fd, filp, 1, &fl->fl_fasync);
1339 	if (error < 0) {
1340 		locks_free_lock(fl);
1341 		goto out_unlock;
1342 	}
1343 	fl->fl_next = *before;
1344 	*before = fl;
1345 	list_add(&fl->fl_link, &file_lock_list);
1346 	filp->f_owner.pid = current->pid;
1347 	filp->f_owner.uid = current->uid;
1348 	filp->f_owner.euid = current->euid;
1349 out_unlock:
1350 	unlock_kernel();
1351 	return error;
1352 }
1353 
1354 /**
1355  *	sys_flock: - flock() system call.
1356  *	@fd: the file descriptor to lock.
1357  *	@cmd: the type of lock to apply.
1358  *
1359  *	Apply a %FL_FLOCK style lock to an open file descriptor.
1360  *	The @cmd can be one of
1361  *
1362  *	%LOCK_SH -- a shared lock.
1363  *
1364  *	%LOCK_EX -- an exclusive lock.
1365  *
1366  *	%LOCK_UN -- remove an existing lock.
1367  *
1368  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
1369  *
1370  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1371  *	processes read and write access respectively.
1372  */
sys_flock(unsigned int fd,unsigned int cmd)1373 asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
1374 {
1375 	struct file *filp;
1376 	int error, type;
1377 
1378 	error = -EBADF;
1379 	filp = fget(fd);
1380 	if (!filp)
1381 		goto out;
1382 
1383 	error = flock_translate_cmd(cmd);
1384 	if (error < 0)
1385 		goto out_putf;
1386 	type = error;
1387 
1388 	error = -EBADF;
1389 	if ((type != F_UNLCK)
1390 #ifdef MSNFS
1391 		&& !(type & LOCK_MAND)
1392 #endif
1393 		&& !(filp->f_mode & 3))
1394 		goto out_putf;
1395 
1396 	lock_kernel();
1397 	error = flock_lock_file(filp, type,
1398 				(cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
1399 	unlock_kernel();
1400 
1401 out_putf:
1402 	fput(filp);
1403 out:
1404 	return error;
1405 }
1406 
1407 /* Report the first existing lock that would conflict with l.
1408  * This implements the F_GETLK command of fcntl().
1409  */
fcntl_getlk(unsigned int fd,struct flock * l)1410 int fcntl_getlk(unsigned int fd, struct flock *l)
1411 {
1412 	struct file *filp;
1413 	struct file_lock *fl, file_lock;
1414 	struct flock flock;
1415 	int error;
1416 
1417 	error = -EFAULT;
1418 	if (copy_from_user(&flock, l, sizeof(flock)))
1419 		goto out;
1420 	error = -EINVAL;
1421 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1422 		goto out;
1423 
1424 	error = -EBADF;
1425 	filp = fget(fd);
1426 	if (!filp)
1427 		goto out;
1428 
1429 	error = flock_to_posix_lock(filp, &file_lock, &flock);
1430 	if (error)
1431 		goto out_putf;
1432 
1433 	if (filp->f_op && filp->f_op->lock) {
1434 		error = filp->f_op->lock(filp, F_GETLK, &file_lock);
1435 		if (error < 0)
1436 			goto out_putf;
1437 		else if (error == LOCK_USE_CLNT)
1438 		  /* Bypass for NFS with no locking - 2.0.36 compat */
1439 		  fl = posix_test_lock(filp, &file_lock);
1440 		else
1441 		  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
1442 	} else {
1443 		fl = posix_test_lock(filp, &file_lock);
1444 	}
1445 
1446 	flock.l_type = F_UNLCK;
1447 	if (fl != NULL) {
1448 		flock.l_pid = fl->fl_pid;
1449 #if BITS_PER_LONG == 32
1450 		/*
1451 		 * Make sure we can represent the posix lock via
1452 		 * legacy 32bit flock.
1453 		 */
1454 		error = -EOVERFLOW;
1455 		if (fl->fl_start > OFFT_OFFSET_MAX)
1456 			goto out_putf;
1457 		if ((fl->fl_end != OFFSET_MAX)
1458 		    && (fl->fl_end > OFFT_OFFSET_MAX))
1459 			goto out_putf;
1460 #endif
1461 		flock.l_start = fl->fl_start;
1462 		flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
1463 			fl->fl_end - fl->fl_start + 1;
1464 		flock.l_whence = 0;
1465 		flock.l_type = fl->fl_type;
1466 	}
1467 	error = -EFAULT;
1468 	if (!copy_to_user(l, &flock, sizeof(flock)))
1469 		error = 0;
1470 
1471 out_putf:
1472 	fput(filp);
1473 out:
1474 	return error;
1475 }
1476 
1477 /* Apply the lock described by l to an open file descriptor.
1478  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1479  */
fcntl_setlk(unsigned int fd,struct file * filp,unsigned int cmd,struct flock * l)1480 int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
1481 		struct flock *l)
1482 {
1483 	struct file *f;
1484 	struct file_lock *file_lock = locks_alloc_lock();
1485 	struct flock flock;
1486 	struct inode *inode;
1487 	int error;
1488 
1489 	if (file_lock == NULL)
1490 		return -ENOLCK;
1491 
1492 	/*
1493 	 * This might block, so we do it before checking the inode.
1494 	 */
1495 	error = -EFAULT;
1496 	if (copy_from_user(&flock, l, sizeof(flock)))
1497 		goto out;
1498 
1499 	/* Get arguments and validate them ...
1500 	 */
1501 
1502 	error = -EINVAL;
1503 	inode = filp->f_dentry->d_inode;
1504 
1505 	/* Don't allow mandatory locks on files that may be memory mapped
1506 	 * and shared.
1507 	 */
1508 	if (IS_MANDLOCK(inode) &&
1509 	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
1510 		struct address_space *mapping = inode->i_mapping;
1511 
1512 		if (mapping->i_mmap_shared != NULL) {
1513 			error = -EAGAIN;
1514 			goto out;
1515 		}
1516 	}
1517 
1518 	error = flock_to_posix_lock(filp, file_lock, &flock);
1519 	if (error)
1520 		goto out;
1521 
1522 	error = -EBADF;
1523 	switch (flock.l_type) {
1524 	case F_RDLCK:
1525 		if (!(filp->f_mode & FMODE_READ))
1526 			goto out;
1527 		break;
1528 	case F_WRLCK:
1529 		if (!(filp->f_mode & FMODE_WRITE))
1530 			goto out;
1531 		break;
1532 	case F_UNLCK:
1533 		break;
1534 	case F_SHLCK:
1535 	case F_EXLCK:
1536 #ifdef __sparc__
1537 /* warn a bit for now, but don't overdo it */
1538 {
1539 	static int count = 0;
1540 	if (!count) {
1541 		count=1;
1542 		printk(KERN_WARNING
1543 		       "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
1544 		       current->pid, current->comm);
1545 	}
1546 }
1547 		if (!(filp->f_mode & 3))
1548 			goto out;
1549 		break;
1550 #endif
1551 	default:
1552 		error = -EINVAL;
1553 		goto out;
1554 	}
1555 
1556 do_it:
1557 	if (filp->f_op && filp->f_op->lock != NULL) {
1558 		error = filp->f_op->lock(filp, cmd, file_lock);
1559 		if (error < 0)
1560 			goto out;
1561 	}
1562 	error = posix_lock_file(filp, file_lock, cmd == F_SETLKW);
1563 	read_lock(&current->files->file_lock);
1564 	f = fcheck(fd);
1565 	read_unlock(&current->files->file_lock);
1566 	/* lost race with close, kill stuck lock if close didn't get it */
1567 	if (!error && flock.l_type != F_UNLCK && filp != f) {
1568 		file_lock->fl_type = F_UNLCK;
1569 		goto do_it;
1570 	}
1571 out:
1572 	locks_free_lock(file_lock);
1573 	return error;
1574 }
1575 
1576 #if BITS_PER_LONG == 32
1577 /* Report the first existing lock that would conflict with l.
1578  * This implements the F_GETLK command of fcntl().
1579  */
fcntl_getlk64(unsigned int fd,struct flock64 * l)1580 int fcntl_getlk64(unsigned int fd, struct flock64 *l)
1581 {
1582 	struct file *filp;
1583 	struct file_lock *fl, file_lock;
1584 	struct flock64 flock;
1585 	int error;
1586 
1587 	error = -EFAULT;
1588 	if (copy_from_user(&flock, l, sizeof(flock)))
1589 		goto out;
1590 	error = -EINVAL;
1591 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1592 		goto out;
1593 
1594 	error = -EBADF;
1595 	filp = fget(fd);
1596 	if (!filp)
1597 		goto out;
1598 
1599 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
1600 	if (error)
1601 		goto out_putf;
1602 
1603 	if (filp->f_op && filp->f_op->lock) {
1604 		error = filp->f_op->lock(filp, F_GETLK, &file_lock);
1605 		if (error < 0)
1606 			goto out_putf;
1607 		else if (error == LOCK_USE_CLNT)
1608 		  /* Bypass for NFS with no locking - 2.0.36 compat */
1609 		  fl = posix_test_lock(filp, &file_lock);
1610 		else
1611 		  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
1612 	} else {
1613 		fl = posix_test_lock(filp, &file_lock);
1614 	}
1615 
1616 	flock.l_type = F_UNLCK;
1617 	if (fl != NULL) {
1618 		flock.l_pid = fl->fl_pid;
1619 		flock.l_start = fl->fl_start;
1620 		flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
1621 			fl->fl_end - fl->fl_start + 1;
1622 		flock.l_whence = 0;
1623 		flock.l_type = fl->fl_type;
1624 	}
1625 	error = -EFAULT;
1626 	if (!copy_to_user(l, &flock, sizeof(flock)))
1627 		error = 0;
1628 
1629 out_putf:
1630 	fput(filp);
1631 out:
1632 	return error;
1633 }
1634 
1635 /* Apply the lock described by l to an open file descriptor.
1636  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1637  */
fcntl_setlk64(unsigned int fd,struct file * filp,unsigned int cmd,struct flock64 * l)1638 int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
1639 		struct flock64 *l)
1640 {
1641 	struct file *f;
1642 	struct file_lock *file_lock = locks_alloc_lock();
1643 	struct flock64 flock;
1644 	struct inode *inode;
1645 	int error;
1646 
1647 	if (file_lock == NULL)
1648 		return -ENOLCK;
1649 
1650 	/*
1651 	 * This might block, so we do it before checking the inode.
1652 	 */
1653 	error = -EFAULT;
1654 	if (copy_from_user(&flock, l, sizeof(flock)))
1655 		goto out;
1656 
1657 	/* Get arguments and validate them ...
1658 	 */
1659 
1660 	error = -EINVAL;
1661 	inode = filp->f_dentry->d_inode;
1662 
1663 	/* Don't allow mandatory locks on files that may be memory mapped
1664 	 * and shared.
1665 	 */
1666 	if (IS_MANDLOCK(inode) &&
1667 	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
1668 		struct address_space *mapping = inode->i_mapping;
1669 
1670 		if (mapping->i_mmap_shared != NULL) {
1671 			error = -EAGAIN;
1672 			goto out;
1673 		}
1674 	}
1675 
1676 	error = flock64_to_posix_lock(filp, file_lock, &flock);
1677 	if (error)
1678 		goto out;
1679 
1680 	error = -EBADF;
1681 	switch (flock.l_type) {
1682 	case F_RDLCK:
1683 		if (!(filp->f_mode & FMODE_READ))
1684 			goto out;
1685 		break;
1686 	case F_WRLCK:
1687 		if (!(filp->f_mode & FMODE_WRITE))
1688 			goto out;
1689 		break;
1690 	case F_UNLCK:
1691 		break;
1692 	case F_SHLCK:
1693 	case F_EXLCK:
1694 	default:
1695 		error = -EINVAL;
1696 		goto out;
1697 	}
1698 
1699 do_it:
1700 	if (filp->f_op && filp->f_op->lock != NULL) {
1701 		error = filp->f_op->lock(filp, cmd, file_lock);
1702 		if (error < 0)
1703 			goto out;
1704 	}
1705 	error = posix_lock_file(filp, file_lock, cmd == F_SETLKW64);
1706 	read_lock(&current->files->file_lock);
1707 	f = fcheck(fd);
1708 	read_unlock(&current->files->file_lock);
1709 	/* lost race with close, kill stuck lock if close didn't get it */
1710 	if (!error && flock.l_type != F_UNLCK && filp != f) {
1711 		file_lock->fl_type = F_UNLCK;
1712 		goto do_it;
1713 	}
1714 out:
1715 	locks_free_lock(file_lock);
1716 	return error;
1717 }
1718 #endif /* BITS_PER_LONG == 32 */
1719 
1720 /*
1721  * This function is called when the file is being removed
1722  * from the task's fd array.
1723  */
locks_remove_posix(struct file * filp,fl_owner_t owner)1724 void locks_remove_posix(struct file *filp, fl_owner_t owner)
1725 {
1726 	struct inode * inode = filp->f_dentry->d_inode;
1727 	struct file_lock *fl;
1728 	struct file_lock **before;
1729 
1730 	/*
1731 	 * For POSIX locks we free all locks on this file for the given task.
1732 	 */
1733 	if (!inode->i_flock) {
1734 		/*
1735 		 * Notice that something might be grabbing a lock right now.
1736 		 * Consider it as a race won by us - event is async, so even if
1737 		 * we miss the lock added we can trivially consider it as added
1738 		 * after we went through this call.
1739 		 */
1740 		return;
1741 	}
1742 	lock_kernel();
1743 	before = &inode->i_flock;
1744 	while ((fl = *before) != NULL) {
1745 		if ((fl->fl_flags & FL_POSIX) && fl->fl_owner == owner) {
1746 			struct file *filp = fl->fl_file;
1747 			/* Note: locks_unlock_delete() can sleep, and
1748 			 * so we may race with the call to sys_close()
1749 			 * by the thread that actually owns this filp.
1750 			 */
1751 			get_file(filp);
1752 			locks_unlock_delete(before);
1753 			fput(filp);
1754 			before = &inode->i_flock;
1755 			continue;
1756 		}
1757 		before = &fl->fl_next;
1758 	}
1759 	unlock_kernel();
1760 }
1761 
1762 /*
1763  * This function is called on the last close of an open file.
1764  */
locks_remove_flock(struct file * filp)1765 void locks_remove_flock(struct file *filp)
1766 {
1767 	struct inode * inode = filp->f_dentry->d_inode;
1768 	struct file_lock *fl;
1769 	struct file_lock **before;
1770 
1771 	if (!inode->i_flock)
1772 		return;
1773 
1774 	lock_kernel();
1775 	before = &inode->i_flock;
1776 
1777 	while ((fl = *before) != NULL) {
1778 		if (fl->fl_file == filp) {
1779 			if (fl->fl_flags & FL_FLOCK) {
1780 				locks_delete_lock(before, 0);
1781 				continue;
1782 			}
1783 			if (fl->fl_flags & FL_LEASE) {
1784 				lease_modify(before, F_UNLCK);
1785 				continue;
1786 			}
1787  		}
1788 		before = &fl->fl_next;
1789 	}
1790 	unlock_kernel();
1791 }
1792 
1793 /**
1794  *	posix_block_lock - blocks waiting for a file lock
1795  *	@blocker: the lock which is blocking
1796  *	@waiter: the lock which conflicts and has to wait
1797  *
1798  * lockd needs to block waiting for locks.
1799  */
1800 void
posix_block_lock(struct file_lock * blocker,struct file_lock * waiter)1801 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
1802 {
1803 	locks_insert_block(blocker, waiter);
1804 }
1805 
1806 /**
1807  *	posix_unblock_lock - stop waiting for a file lock
1808  *	@waiter: the lock which was waiting
1809  *
1810  *	lockd needs to block waiting for locks.
1811  */
1812 void
posix_unblock_lock(struct file_lock * waiter)1813 posix_unblock_lock(struct file_lock *waiter)
1814 {
1815 	if (!list_empty(&waiter->fl_block))
1816 		locks_delete_block(waiter);
1817 }
1818 
lock_get_status(char * out,struct file_lock * fl,int id,char * pfx)1819 static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
1820 {
1821 	struct inode *inode = NULL;
1822 
1823 	if (fl->fl_file != NULL)
1824 		inode = fl->fl_file->f_dentry->d_inode;
1825 
1826 	out += sprintf(out, "%d:%s ", id, pfx);
1827 	if (fl->fl_flags & FL_POSIX) {
1828 		out += sprintf(out, "%6s %s ",
1829 			     (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1830 			     (inode == NULL) ? "*NOINODE*" :
1831 			     (IS_MANDLOCK(inode) &&
1832 			      (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1833 			     "MANDATORY" : "ADVISORY ");
1834 	} else if (fl->fl_flags & FL_FLOCK) {
1835 #ifdef MSNFS
1836 		if (fl->fl_type & LOCK_MAND) {
1837 			out += sprintf(out, "FLOCK  MSNFS     ");
1838 		} else
1839 #endif
1840 			out += sprintf(out, "FLOCK  ADVISORY  ");
1841 	} else if (fl->fl_flags & FL_LEASE) {
1842 		out += sprintf(out, "LEASE  ");
1843 		if (fl->fl_type & F_INPROGRESS)
1844 			out += sprintf(out, "BREAKING  ");
1845 		else if (fl->fl_file)
1846 			out += sprintf(out, "ACTIVE    ");
1847 		else
1848 			out += sprintf(out, "BREAKER   ");
1849 	} else {
1850 		out += sprintf(out, "UNKNOWN UNKNOWN  ");
1851 	}
1852 #ifdef MSNFS
1853 	if (fl->fl_type & LOCK_MAND) {
1854 		out += sprintf(out, "%s ",
1855 			       (fl->fl_type & LOCK_READ)
1856 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
1857 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
1858 	} else
1859 #endif
1860 		out += sprintf(out, "%s ",
1861 			       (fl->fl_type & F_INPROGRESS)
1862 			       ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
1863 			       : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
1864 	out += sprintf(out, "%d %s:%ld ",
1865 		     fl->fl_pid,
1866 		     inode ? kdevname(inode->i_dev) : "<none>",
1867 		     inode ? inode->i_ino : 0);
1868 	out += sprintf(out, "%Ld ", fl->fl_start);
1869 	if (fl->fl_end == OFFSET_MAX)
1870 		out += sprintf(out, "EOF ");
1871 	else
1872 		out += sprintf(out, "%Ld ", fl->fl_end);
1873 	sprintf(out, "%08lx %08lx %08lx %08lx %08lx\n",
1874 		(long)fl, (long)fl->fl_link.prev, (long)fl->fl_link.next,
1875 		(long)fl->fl_next, (long)fl->fl_block.next);
1876 }
1877 
move_lock_status(char ** p,off_t * pos,off_t offset)1878 static void move_lock_status(char **p, off_t* pos, off_t offset)
1879 {
1880 	int len;
1881 	len = strlen(*p);
1882 	if(*pos >= offset) {
1883 		/* the complete line is valid */
1884 		*p += len;
1885 		*pos += len;
1886 		return;
1887 	}
1888 	if(*pos+len > offset) {
1889 		/* use the second part of the line */
1890 		int i = offset-*pos;
1891 		memmove(*p,*p+i,len-i);
1892 		*p += len-i;
1893 		*pos += len;
1894 		return;
1895 	}
1896 	/* discard the complete line */
1897 	*pos += len;
1898 }
1899 
1900 /**
1901  *	get_locks_status	-	reports lock usage in /proc/locks
1902  *	@buffer: address in userspace to write into
1903  *	@start: ?
1904  *	@offset: how far we are through the buffer
1905  *	@length: how much to read
1906  */
1907 
get_locks_status(char * buffer,char ** start,off_t offset,int length)1908 int get_locks_status(char *buffer, char **start, off_t offset, int length)
1909 {
1910 	struct list_head *tmp;
1911 	char *q = buffer;
1912 	off_t pos = 0;
1913 	int i = 0;
1914 
1915 	lock_kernel();
1916 	list_for_each(tmp, &file_lock_list) {
1917 		struct list_head *btmp;
1918 		struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
1919 		lock_get_status(q, fl, ++i, "");
1920 		move_lock_status(&q, &pos, offset);
1921 
1922 		if(pos >= offset+length)
1923 			goto done;
1924 
1925 		list_for_each(btmp, &fl->fl_block) {
1926 			struct file_lock *bfl = list_entry(btmp,
1927 					struct file_lock, fl_block);
1928 			lock_get_status(q, bfl, i, " ->");
1929 			move_lock_status(&q, &pos, offset);
1930 
1931 			if(pos >= offset+length)
1932 				goto done;
1933 		}
1934 	}
1935 done:
1936 	unlock_kernel();
1937 	*start = buffer;
1938 	if(q-buffer < length)
1939 		return (q-buffer);
1940 	return length;
1941 }
1942 
steal_locks(fl_owner_t from)1943 void steal_locks(fl_owner_t from)
1944 {
1945 	struct list_head *tmp;
1946 
1947 	if (from == current->files)
1948 		return;
1949 
1950 	lock_kernel();
1951 	list_for_each(tmp, &file_lock_list) {
1952 		struct file_lock *fl = list_entry(tmp, struct file_lock,
1953 						  fl_link);
1954 		if (fl->fl_owner == from)
1955 			fl->fl_owner = current->files;
1956 	}
1957 	unlock_kernel();
1958 }
1959 
1960 #ifdef MSNFS
1961 /**
1962  *	lock_may_read - checks that the region is free of locks
1963  *	@inode: the inode that is being read
1964  *	@start: the first byte to read
1965  *	@len: the number of bytes to read
1966  *
1967  *	Emulates Windows locking requirements.  Whole-file
1968  *	mandatory locks (share modes) can prohibit a read and
1969  *	byte-range POSIX locks can prohibit a read if they overlap.
1970  *
1971  *	N.B. this function is only ever called
1972  *	from knfsd and ownership of locks is never checked.
1973  */
lock_may_read(struct inode * inode,loff_t start,unsigned long len)1974 int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
1975 {
1976 	struct file_lock *fl;
1977 	int result = 1;
1978 	lock_kernel();
1979 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1980 		if (fl->fl_flags == FL_POSIX) {
1981 			if (fl->fl_type == F_RDLCK)
1982 				continue;
1983 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
1984 				continue;
1985 		} else if (fl->fl_flags == FL_FLOCK) {
1986 			if (!(fl->fl_type & LOCK_MAND))
1987 				continue;
1988 			if (fl->fl_type & LOCK_READ)
1989 				continue;
1990 		} else
1991 			continue;
1992 		result = 0;
1993 		break;
1994 	}
1995 	unlock_kernel();
1996 	return result;
1997 }
1998 
1999 /**
2000  *	lock_may_write - checks that the region is free of locks
2001  *	@inode: the inode that is being written
2002  *	@start: the first byte to write
2003  *	@len: the number of bytes to write
2004  *
2005  *	Emulates Windows locking requirements.  Whole-file
2006  *	mandatory locks (share modes) can prohibit a write and
2007  *	byte-range POSIX locks can prohibit a write if they overlap.
2008  *
2009  *	N.B. this function is only ever called
2010  *	from knfsd and ownership of locks is never checked.
2011  */
lock_may_write(struct inode * inode,loff_t start,unsigned long len)2012 int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
2013 {
2014 	struct file_lock *fl;
2015 	int result = 1;
2016 	lock_kernel();
2017 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2018 		if (fl->fl_flags == FL_POSIX) {
2019 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2020 				continue;
2021 		} else if (fl->fl_flags == FL_FLOCK) {
2022 			if (!(fl->fl_type & LOCK_MAND))
2023 				continue;
2024 			if (fl->fl_type & LOCK_WRITE)
2025 				continue;
2026 		} else
2027 			continue;
2028 		result = 0;
2029 		break;
2030 	}
2031 	unlock_kernel();
2032 	return result;
2033 }
2034 #endif
2035 
filelock_init(void)2036 static int __init filelock_init(void)
2037 {
2038 	filelock_cache = kmem_cache_create("file_lock_cache",
2039 			sizeof(struct file_lock), 0, 0, init_once, NULL);
2040 	if (!filelock_cache)
2041 		panic("cannot create file lock slab cache");
2042 	return 0;
2043 }
2044 
2045 module_init(filelock_init)
2046