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