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