root/fs/locks.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. locks_free_lock
  2. locks_insert_block
  3. locks_delete_block
  4. sys_flock
  5. fcntl_getlk
  6. fcntl_setlk
  7. locks_remove_locks
  8. locks_verify_locked
  9. locks_mandatory_locked
  10. locks_verify_area
  11. locks_mandatory_area
  12. posix_make_lock
  13. flock_make_lock
  14. posix_locks_conflict
  15. flock_locks_conflict
  16. locks_conflict
  17. locks_overlap
  18. posix_locks_deadlock
  19. flock_lock_file
  20. posix_lock_file
  21. locks_alloc_lock
  22. locks_insert_lock
  23. locks_delete_lock

   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 - F_FLOCK and F_POSIX.
  32  *
  33  *  F_POSIX locks are created with calls to fcntl() and lockf() through the
  34  *  fcntl() system call. They have the semantics described above.
  35  *
  36  *  F_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  *  F_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  *  F_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 (begemot@bgm.rosprint.net), 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 'linux/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.
  73  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
  74  */
  75 
  76 #include <linux/malloc.h>
  77 #include <linux/sched.h>
  78 #include <linux/kernel.h>
  79 #include <linux/errno.h>
  80 #include <linux/stat.h>
  81 #include <linux/fcntl.h>
  82 
  83 #include <asm/segment.h>
  84 
  85 #define OFFSET_MAX      ((off_t)0x7fffffff)     /* FIXME: move elsewhere? */
  86 
  87 static int flock_make_lock(struct file *filp, struct file_lock *fl,
  88                                unsigned int cmd);
  89 static int posix_make_lock(struct file *filp, struct file_lock *fl,
  90                                struct flock *l);
  91 static int flock_locks_conflict(struct file_lock *caller_fl,
  92                                 struct file_lock *sys_fl);
  93 static int posix_locks_conflict(struct file_lock *caller_fl,
  94                                 struct file_lock *sys_fl);
  95 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
  96 static int flock_lock_file(struct file *filp, struct file_lock *caller,
  97                            unsigned int wait);
  98 static int posix_lock_file(struct file *filp, struct file_lock *caller,
  99                            unsigned int wait);
 100 static int posix_locks_deadlock(struct task_struct *my_task,
 101                                 struct task_struct *blocked_task);
 102 static int locks_overlap(struct file_lock *fl1, struct file_lock *fl2);
 103 
 104 static struct file_lock *locks_alloc_lock(struct file_lock *fl);
 105 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
 106 static void locks_delete_lock(struct file_lock **fl, unsigned int wait);
 107 
 108 static struct file_lock *file_lock_table = NULL;
 109 
 110 /* Free lock not inserted in any queue */
 111 static inline void locks_free_lock(struct file_lock *fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113         kfree(fl);
 114         return;
 115 }
 116 
 117 /* Add lock fl to the blocked list pointed to by block.
 118  * We search to the end of the existing list and insert the the new
 119  * struct. This ensures processes will be woken up in the order they
 120  * blocked.
 121  * NOTE: nowhere does the documentation insist that processes be woken
 122  * up in this order, but it seems like the reasonable thing to do.
 123  * If the blocked list gets long then this search could get expensive,
 124  * in which case we could consider waking the processes up in reverse
 125  * order, or making the blocked list a doubly linked circular list.
 126  * 
 127  * This functions are called only from one place (flock_lock_file)
 128  * so they are inlined now. -- Dmitry Gorodchanin 02/09/96.
 129  */
 130 
 131 static inline void locks_insert_block(struct file_lock *bfl, 
     /* [previous][next][first][last][top][bottom][index][help] */
 132                                       struct file_lock *fl)
 133 {
 134         while (bfl->fl_block != NULL) {
 135                 bfl = bfl->fl_block;
 136         }
 137 
 138         bfl->fl_block = fl;
 139         fl->fl_block = NULL;
 140         
 141         return;
 142 }
 143 
 144 static inline void locks_delete_block(struct file_lock *bfl,
     /* [previous][next][first][last][top][bottom][index][help] */
 145                                       struct file_lock *fl)
 146 {
 147         struct file_lock *tfl;
 148         
 149         while ((tfl = bfl->fl_block) != NULL) {
 150                 if (tfl == fl) {
 151                         bfl->fl_block = fl->fl_block;
 152                         fl->fl_block = NULL;
 153                         return;
 154                 }
 155                 bfl = tfl;
 156         }
 157         return;
 158 }
 159 
 160 /* flock() system call entry point. Apply a FLOCK style lock to
 161  * an open file descriptor.
 162  */
 163 asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 164 {
 165         struct file_lock file_lock;
 166         struct file *filp;
 167 
 168         if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
 169                 return (-EBADF);
 170 
 171         if (!flock_make_lock(filp, &file_lock, cmd))
 172                 return (-EINVAL);
 173         
 174         if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
 175                 return (-EBADF);
 176         
 177         return (flock_lock_file(filp, &file_lock, cmd & LOCK_UN ? 0 : cmd & LOCK_NB ? 0 : 1));
 178 }
 179 
 180 /* Report the first existing lock that would conflict with l.
 181  * This implements the F_GETLK command of fcntl().
 182  */
 183 int fcntl_getlk(unsigned int fd, struct flock *l)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185         int error;
 186         struct flock flock;
 187         struct file *filp;
 188         struct file_lock *fl,file_lock;
 189 
 190         if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
 191                 return (-EBADF);
 192         error = verify_area(VERIFY_WRITE, l, sizeof(*l));
 193         if (error)
 194                 return (error);
 195 
 196         memcpy_fromfs(&flock, l, sizeof(flock));
 197         if ((flock.l_type == F_UNLCK) || (flock.l_type == F_EXLCK) ||
 198             (flock.l_type == F_SHLCK))
 199                 return (-EINVAL);
 200 
 201         if (!filp->f_inode || !posix_make_lock(filp, &file_lock, &flock))
 202                 return (-EINVAL);
 203 
 204         for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
 205                 if (posix_locks_conflict(&file_lock, fl)) {
 206                         flock.l_pid = fl->fl_owner->pid;
 207                         flock.l_start = fl->fl_start;
 208                         flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
 209                                 fl->fl_end - fl->fl_start + 1;
 210                         flock.l_whence = 0;
 211                         flock.l_type = fl->fl_type;
 212                         memcpy_tofs(l, &flock, sizeof(flock));
 213                         return (0);
 214                 }
 215         }
 216 
 217         flock.l_type = F_UNLCK;                 /* no conflict found */
 218         memcpy_tofs(l, &flock, sizeof(flock));
 219         return (0);
 220 }
 221 
 222 /* Apply the lock described by l to an open file descriptor.
 223  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
 224  * It also emulates flock() in a pretty broken way for older C
 225  * libraries.
 226  */
 227 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
     /* [previous][next][first][last][top][bottom][index][help] */
 228 {
 229         int error;
 230         struct file *filp;
 231         struct file_lock file_lock;
 232         struct flock flock;
 233         struct inode *inode;
 234 
 235         /*
 236          * Get arguments and validate them ...
 237          */
 238 
 239         if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
 240                 return (-EBADF);
 241         
 242         error = verify_area(VERIFY_READ, l, sizeof(*l));
 243         if (error)
 244                 return (error);
 245         
 246         if (!(inode = filp->f_inode))
 247                 return (-EINVAL);
 248         
 249         /* Don't allow mandatory locks on files that may be memory mapped
 250          * and shared.
 251          */
 252         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && inode->i_mmap) {
 253                 struct vm_area_struct *vma = inode->i_mmap;
 254                 do {
 255                         if (vma->vm_flags & VM_MAYSHARE)
 256                                 return (-EAGAIN);
 257                         vma = vma->vm_next_share;
 258                 } while (vma != inode->i_mmap);
 259         }
 260 
 261         memcpy_fromfs(&flock, l, sizeof(flock));
 262         if (!posix_make_lock(filp, &file_lock, &flock))
 263                 return (-EINVAL);
 264         
 265         switch (flock.l_type) {
 266         case F_RDLCK :
 267                 if (!(filp->f_mode & 1))
 268                         return (-EBADF);
 269                 break;
 270         case F_WRLCK :
 271                 if (!(filp->f_mode & 2))
 272                         return (-EBADF);
 273                 break;
 274         case F_SHLCK :
 275         case F_EXLCK :
 276 #if 1
 277 /* warn a bit for now, but don't overdo it */
 278 {
 279         static int count = 0;
 280         if (count < 5) {
 281                 count++;
 282                 printk(KERN_WARNING
 283                        "fcntl_setlk() called by process %d with broken flock() emulation\n",
 284                        current->pid);
 285         }
 286 }
 287 #endif
 288                 if (!(filp->f_mode & 3))
 289                         return (-EBADF);
 290                 break;
 291         case F_UNLCK :
 292                 break;
 293         }
 294         
 295         return (posix_lock_file(filp, &file_lock, cmd == F_SETLKW));
 296 }
 297 
 298 /* This function is called when the file is closed.
 299  */
 300 void locks_remove_locks(struct task_struct *task, struct file *filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 301 {
 302         struct file_lock *fl;
 303         struct file_lock **before;
 304 
 305         /* For POSIX locks we free all locks on this file for the given task.
 306          * For FLOCK we only free locks on this *open* file if it is the last
 307          * close on that file.
 308          */
 309         before = &filp->f_inode->i_flock;
 310         while ((fl = *before) != NULL) {
 311                 if (((fl->fl_flags == F_POSIX) && (fl->fl_owner == task)) ||
 312                     ((fl->fl_flags == F_FLOCK) && (fl->fl_file == filp) &&
 313                      (filp->f_count == 1)))
 314                         locks_delete_lock(before, 0);
 315                 else
 316                         before = &fl->fl_next;
 317         }
 318 
 319         return;
 320 }
 321 
 322 int locks_verify_locked(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 323 {
 324         /* Candidates for mandatory locking have the setgid bit set
 325          * but no group execute bit -  an otherwise meaningless combination.
 326          */
 327         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
 328                 return (locks_mandatory_locked(inode));
 329         return (0);
 330 }
 331 
 332 int locks_mandatory_locked(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 333 {
 334         struct file_lock *fl;
 335 
 336         /* Search the lock list for this inode for any POSIX locks.
 337          */
 338         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
 339                 if (fl->fl_flags == F_POSIX && fl->fl_owner != current)
 340                         return (-EAGAIN);
 341         }
 342         return (0);
 343 }
 344 
 345 int locks_verify_area(int read_write, struct inode *inode, struct file *filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 346                       unsigned int offset, unsigned int count)
 347 {
 348         /* Candidates for mandatory locking have the setgid bit set
 349          * but no group execute bit -  an otherwise meaningless combination.
 350          */
 351         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
 352                 return (locks_mandatory_area(read_write, inode, filp, offset,
 353                                              count));
 354         return (0);
 355 }
 356         
 357 int locks_mandatory_area(int read_write, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 358                          struct file *filp, unsigned int offset,
 359                          unsigned int count)
 360 {
 361         struct file_lock *fl;
 362 
 363 repeat:
 364         /*
 365          * Search the lock list for this inode for locks that conflict with
 366          * the proposed read/write.
 367          */
 368         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
 369                 if (fl->fl_flags == F_FLOCK ||
 370                     (fl->fl_flags == F_POSIX && fl->fl_owner == current))
 371                         continue;
 372                 if (fl->fl_end < offset ||
 373                     fl->fl_start >= offset + count)
 374                         continue;
 375                 /*
 376                  * Block for writes against a "read" lock, and both reads and
 377                  * writes against a "write" lock.
 378                  */
 379                 if (read_write == FLOCK_VERIFY_WRITE ||
 380                     fl->fl_type == F_WRLCK) {
 381                         if (filp && (filp->f_flags & O_NONBLOCK))
 382                                 return (-EAGAIN);
 383                         if (current->signal & ~current->blocked)
 384                                 return (-ERESTARTSYS);
 385                         if (posix_locks_deadlock(current, fl->fl_owner))
 386                                 return (-EDEADLOCK);
 387                         interruptible_sleep_on(&fl->fl_wait);
 388                         if (current->signal & ~current->blocked)
 389                                 return (-ERESTARTSYS);
 390                         /*
 391                          * If we've been sleeping someone might have changed
 392                          * the permissions behind our back.
 393                          */
 394                         if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
 395                                 break;
 396                         goto repeat;
 397                 }
 398         }
 399         return (0);
 400 }
 401 
 402 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
 403  * style lock.
 404  */
 405 static int posix_make_lock(struct file *filp, struct file_lock *fl,
     /* [previous][next][first][last][top][bottom][index][help] */
 406                            struct flock *l)
 407 {
 408         off_t start;
 409 
 410         switch (l->l_type) {
 411         case F_RDLCK :
 412         case F_WRLCK :
 413         case F_UNLCK :
 414                 fl->fl_type = l->l_type;
 415                 break;
 416         case F_SHLCK :
 417                 fl->fl_type = F_RDLCK;
 418                 break;
 419         case F_EXLCK :
 420                 fl->fl_type = F_WRLCK;
 421                 break;
 422         default :
 423                 return (0);
 424         }
 425 
 426         switch (l->l_whence) {
 427         case 0 : /*SEEK_SET*/
 428                 start = 0;
 429                 break;
 430         case 1 : /*SEEK_CUR*/
 431                 start = filp->f_pos;
 432                 break;
 433         case 2 : /*SEEK_END*/
 434                 start = filp->f_inode->i_size;
 435                 break;
 436         default :
 437                 return (0);
 438         }
 439 
 440         if (((start += l->l_start) < 0) || (l->l_len < 0))
 441                 return (0);
 442         fl->fl_start = start;   /* we record the absolute position */
 443         if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
 444                 fl->fl_end = OFFSET_MAX;
 445         
 446         fl->fl_flags = F_POSIX;
 447         fl->fl_file = filp;
 448         fl->fl_owner = current;
 449         fl->fl_wait = NULL;             /* just for cleanliness */
 450         
 451         return (1);
 452 }
 453 
 454 /* Verify a call to flock() and fill in a file_lock structure with
 455  * an appropriate FLOCK lock.
 456  */
 457 static int flock_make_lock(struct file *filp, struct file_lock *fl,
     /* [previous][next][first][last][top][bottom][index][help] */
 458                            unsigned int cmd)
 459 {
 460         if (!filp->f_inode)     /* just in case */
 461                 return (0);
 462 
 463         switch (cmd & ~LOCK_NB) {
 464         case LOCK_SH :
 465                 fl->fl_type = F_RDLCK;
 466                 break;
 467         case LOCK_EX :
 468                 fl->fl_type = F_WRLCK;
 469                 break;
 470         case LOCK_UN :
 471                 fl->fl_type = F_UNLCK;
 472                 break;
 473         default :
 474                 return (0);
 475         }
 476 
 477         fl->fl_flags = F_FLOCK;
 478         fl->fl_start = 0;
 479         fl->fl_end = OFFSET_MAX;
 480         fl->fl_file = filp;
 481         fl->fl_owner = current;
 482         fl->fl_wait = NULL;             /* just for cleanliness */
 483         
 484         return (1);
 485 }
 486 
 487 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
 488  * checking before calling the locks_conflict().
 489  */
 490 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 491 {
 492         /* POSIX locks owned by the same process do not conflict with
 493          * each other.
 494          */
 495         if ((sys_fl->fl_flags == F_POSIX) &&
 496             (caller_fl->fl_owner == sys_fl->fl_owner))
 497                 return (0);
 498 
 499         return (locks_conflict(caller_fl, sys_fl));
 500 }
 501 
 502 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
 503  * checking before calling the locks_conflict().
 504  */
 505 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 506 {
 507         /* FLOCK locks referring to the same filp do not conflict with
 508          * each other.
 509          */
 510         if ((sys_fl->fl_flags == F_FLOCK) &&
 511             (caller_fl->fl_file == sys_fl->fl_file))
 512                 return (0);
 513 
 514         return (locks_conflict(caller_fl, sys_fl));
 515 }
 516 
 517 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
 518  * checks for overlapping locks and shared/exclusive status.
 519  */
 520 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 521 {
 522         if (!locks_overlap(caller_fl, sys_fl))
 523                 return (0);
 524 
 525         switch (caller_fl->fl_type) {
 526         case F_RDLCK :
 527                 return (sys_fl->fl_type == F_WRLCK);
 528                 
 529         case F_WRLCK :
 530                 return (1);
 531 
 532         default:
 533                 printk("locks_conflict(): impossible lock type - %d\n",
 534                        caller_fl->fl_type);
 535                 break;
 536         }
 537         return (0);     /* This should never happen */
 538 }
 539 
 540 /* Check if two locks overlap each other.
 541  */
 542 static int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
     /* [previous][next][first][last][top][bottom][index][help] */
 543 {
 544         return ((fl1->fl_end >= fl2->fl_start) &&
 545                 (fl2->fl_end >= fl1->fl_start));
 546 }
 547 
 548 /* This function tests for deadlock condition before putting a process to
 549  * sleep. The detection scheme is no longer recursive. Recursive was neat,
 550  * but dangerous - we risked stack corruption if the lock data was bad, or
 551  * if the recursion was too deep for any other reason.
 552  *
 553  * We rely on the fact that a task can only be on one lock's wait queue
 554  * at a time. When we find blocked_task on a wait queue we can re-search
 555  * with blocked_task equal to that queue's owner, until either blocked_task
 556  * isn't found, or blocked_task is found on a queue owned by my_task.
 557  */
 558 static int posix_locks_deadlock(struct task_struct *my_task,
     /* [previous][next][first][last][top][bottom][index][help] */
 559                                 struct task_struct *blocked_task)
 560 {
 561         struct wait_queue *dlock_wait;
 562         struct file_lock *fl;
 563 
 564 next_task:
 565         for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
 566                 if (fl->fl_owner == NULL || fl->fl_wait == NULL)
 567                         continue;
 568                 dlock_wait = fl->fl_wait;
 569                 do {
 570                         if (dlock_wait->task == blocked_task) {
 571                                 if (fl->fl_owner == my_task) {
 572                                         return(-EDEADLOCK);
 573                                 }
 574                                 blocked_task = fl->fl_owner;
 575                                 goto next_task;
 576                         }
 577                         dlock_wait = dlock_wait->next;
 578                 } while (dlock_wait != fl->fl_wait);
 579         }
 580         return (0);
 581 }
 582 
 583 /* Try to create a FLOCK lock on filp. We rely on FLOCK locks being sorted
 584  * first in an inode's lock list, and always insert new locks at the head
 585  * of the list.
 586  */
 587 static int flock_lock_file(struct file *filp, struct file_lock *caller,
     /* [previous][next][first][last][top][bottom][index][help] */
 588                            unsigned int wait)
 589 {
 590         struct file_lock *fl;
 591         struct file_lock *new_fl;
 592         struct file_lock **before;
 593         int change = 0;
 594 
 595         /* This a compact little algorithm based on us always placing FLOCK
 596          * locks at the front of the list.
 597          */
 598         before = &filp->f_inode->i_flock;
 599         while ((fl = *before) && (fl->fl_flags == F_FLOCK)) {
 600                 if (caller->fl_file == fl->fl_file) {
 601                         if (caller->fl_type == fl->fl_type)
 602                                 return (0);
 603                         change = 1;
 604                         break;
 605                 }
 606                 before = &fl->fl_next;
 607         }
 608         /* change means that we are changing the type of an existing lock, or
 609          * or else unlocking it.
 610          */
 611         if (change)
 612                 locks_delete_lock(before, caller->fl_type != F_UNLCK);
 613         if (caller->fl_type == F_UNLCK)
 614                 return (0);
 615         if ((new_fl = locks_alloc_lock(caller)) == NULL)
 616                 return (-ENOLCK);
 617  repeat:
 618         for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
 619                 if (!flock_locks_conflict(new_fl, fl))
 620                         continue;
 621                 
 622                 if (wait) {
 623                         if (current->signal & ~current->blocked) {
 624                                 /* Note: new_fl is not in any queue at this
 625                                  * point. So we must use locks_free_lock()
 626                                  * instead of locks_delete_lock()
 627                                  *      Dmitry Gorodchanin 09/02/96.
 628                                  */
 629                                 locks_free_lock(new_fl);
 630                                 return (-ERESTARTSYS);
 631                         }
 632                         locks_insert_block(fl, new_fl);
 633                         interruptible_sleep_on(&new_fl->fl_wait);
 634                         wake_up(&new_fl->fl_wait);
 635                         if (current->signal & ~current->blocked) {
 636                                 /* If we are here, than we were awaken
 637                                  * by signal, so new_fl is still in 
 638                                  * block queue of fl. We need remove 
 639                                  * new_fl and then free it. 
 640                                  *      Dmitry Gorodchanin 09/02/96.
 641                                  */
 642 
 643                                 locks_delete_block(fl, new_fl);
 644                                 locks_free_lock(new_fl);
 645                                 return (-ERESTARTSYS);
 646                         }
 647                         goto repeat;
 648                 }
 649                 
 650                 locks_free_lock(new_fl);
 651                 return (-EAGAIN);
 652         }
 653         locks_insert_lock(&filp->f_inode->i_flock, new_fl);
 654         return (0);
 655 }
 656 
 657 /* Add a POSIX style lock to a file.
 658  * We merge adjacent locks whenever possible. POSIX locks come after FLOCK
 659  * locks in the list and are sorted by owner task, then by starting address
 660  *
 661  * Kai Petzke writes:
 662  * To make freeing a lock much faster, we keep a pointer to the lock before the
 663  * actual one. But the real gain of the new coding was, that lock_it() and
 664  * unlock_it() became one function.
 665  *
 666  * To all purists: Yes, I use a few goto's. Just pass on to the next function.
 667  */
 668 
 669 static int posix_lock_file(struct file *filp, struct file_lock *caller,
     /* [previous][next][first][last][top][bottom][index][help] */
 670                            unsigned int wait)
 671 {
 672         struct file_lock *fl;
 673         struct file_lock *new_fl;
 674         struct file_lock *left = NULL;
 675         struct file_lock *right = NULL;
 676         struct file_lock **before;
 677         int added = 0;
 678 
 679         if (caller->fl_type != F_UNLCK) {
 680 repeat:
 681                 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
 682                         if (!posix_locks_conflict(caller, fl))
 683                                 continue;
 684                         if (wait) {
 685                                 if (current->signal & ~current->blocked)
 686                                         return (-ERESTARTSYS);
 687                                 if (fl->fl_flags == F_POSIX)
 688                                         if (posix_locks_deadlock(caller->fl_owner, fl->fl_owner))
 689                                                 return (-EDEADLOCK);
 690                                 interruptible_sleep_on(&fl->fl_wait);
 691                                 if (current->signal & ~current->blocked)
 692                                         return (-ERESTARTSYS);
 693                                 goto repeat;
 694                         }
 695                         return (-EAGAIN);
 696                 }
 697         }
 698         /*
 699          * Find the first old lock with the same owner as the new lock.
 700          */
 701         
 702         before = &filp->f_inode->i_flock;
 703 
 704         /* First skip FLOCK locks and locks owned by other processes.
 705          */
 706         while ((fl = *before) && ((fl->fl_flags == F_FLOCK) ||
 707                                   (caller->fl_owner != fl->fl_owner))) {
 708                 before = &fl->fl_next;
 709         }
 710         
 711 
 712         /* Process locks with this owner.
 713          */
 714         while ((fl = *before) && (caller->fl_owner == fl->fl_owner)) {
 715                 /* Detect adjacent or overlapping regions (if same lock type)
 716                  */
 717                 if (caller->fl_type == fl->fl_type) {
 718                         if (fl->fl_end < caller->fl_start - 1)
 719                                 goto next_lock;
 720                         /* If the next lock in the list has entirely bigger
 721                          * addresses than the new one, insert the lock here.
 722                          */
 723                         if (fl->fl_start > caller->fl_end + 1)
 724                                 break;
 725 
 726                         /* If we come here, the new and old lock are of the
 727                          * same type and adjacent or overlapping. Make one
 728                          * lock yielding from the lower start address of both
 729                          * locks to the higher end address.
 730                          */
 731                         if (fl->fl_start > caller->fl_start)
 732                                 fl->fl_start = caller->fl_start;
 733                         else
 734                                 caller->fl_start = fl->fl_start;
 735                         if (fl->fl_end < caller->fl_end)
 736                                 fl->fl_end = caller->fl_end;
 737                         else
 738                                 caller->fl_end = fl->fl_end;
 739                         if (added) {
 740                                 locks_delete_lock(before, 0);
 741                                 continue;
 742                         }
 743                         caller = fl;
 744                         added = 1;
 745                 }
 746                 else {
 747                         /* Processing for different lock types is a bit
 748                          * more complex.
 749                          */
 750                         if (fl->fl_end < caller->fl_start)
 751                                 goto next_lock;
 752                         if (fl->fl_start > caller->fl_end)
 753                                 break;
 754                         if (caller->fl_type == F_UNLCK)
 755                                 added = 1;
 756                         if (fl->fl_start < caller->fl_start)
 757                                 left = fl;
 758                         /* If the next lock in the list has a higher end
 759                          * address than the new one, insert the new one here.
 760                          */
 761                         if (fl->fl_end > caller->fl_end) {
 762                                 right = fl;
 763                                 break;
 764                         }
 765                         if (fl->fl_start >= caller->fl_start) {
 766                                 /* The new lock completely replaces an old
 767                                  * one (This may happen several times).
 768                                  */
 769                                 if (added) {
 770                                         locks_delete_lock(before, 0);
 771                                         continue;
 772                                 }
 773                                 /* Replace the old lock with the new one.
 774                                  * Wake up anybody waiting for the old one,
 775                                  * as the change in lock type might satisfy
 776                                  * their needs.
 777                                  */
 778                                 wake_up(&fl->fl_wait);
 779                                 fl->fl_start = caller->fl_start;
 780                                 fl->fl_end = caller->fl_end;
 781                                 fl->fl_type = caller->fl_type;
 782                                 caller = fl;
 783                                 added = 1;
 784                         }
 785                 }
 786                 /* Go on to next lock.
 787                  */
 788         next_lock:
 789                 before = &(*before)->fl_next;
 790         }
 791 
 792         if (!added) {
 793                 if (caller->fl_type == F_UNLCK)
 794                         return (0);
 795                 if ((new_fl = locks_alloc_lock(caller)) == NULL)
 796                         return (-ENOLCK);
 797                 locks_insert_lock(before, new_fl);
 798 
 799         }
 800         if (right) {
 801                 if (left == right) {
 802                         /* The new lock breaks the old one in two pieces, so we
 803                          * have to allocate one more lock (in this case, even
 804                          * F_UNLCK may fail!).
 805                          */
 806                         if ((left = locks_alloc_lock(right)) == NULL) {
 807                                 if (!added)
 808                                         locks_delete_lock(before, 0);
 809                                 return (-ENOLCK);
 810                         }
 811                         locks_insert_lock(before, left);
 812                 }
 813                 right->fl_start = caller->fl_end + 1;
 814         }
 815         if (left)
 816                 left->fl_end = caller->fl_start - 1;
 817         return (0);
 818 }
 819 
 820 /* Allocate memory for a new lock and initialize its fields from
 821  * fl. The lock is not inserted into any lists until locks_insert_lock()
 822  * or locks_insert_block() are called.
 823  */
 824 
 825 static struct file_lock *locks_alloc_lock(struct file_lock *fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 826 {
 827         struct file_lock *tmp;
 828 
 829         /* Okay, let's make a new file_lock structure... */
 830         if ((tmp = (struct file_lock *)kmalloc(sizeof(struct file_lock),
 831                                                GFP_ATOMIC)) == NULL)
 832                 return (tmp);
 833 
 834         tmp->fl_nextlink = NULL;
 835         tmp->fl_prevlink = NULL;
 836         tmp->fl_next = NULL;
 837         tmp->fl_block = NULL;
 838         tmp->fl_flags = fl->fl_flags;
 839         tmp->fl_owner = fl->fl_owner;
 840         tmp->fl_file = fl->fl_file;
 841         tmp->fl_wait = NULL;
 842         tmp->fl_type = fl->fl_type;
 843         tmp->fl_start = fl->fl_start;
 844         tmp->fl_end = fl->fl_end;
 845 
 846         return (tmp);
 847 }
 848 
 849 /* Insert file lock fl into an inode's lock list at the position indicated
 850  * by pos. At the same time add the lock to the global file lock list.
 851  */
 852 
 853 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
     /* [previous][next][first][last][top][bottom][index][help] */
 854 {
 855         fl->fl_nextlink = file_lock_table;
 856         fl->fl_prevlink = NULL;
 857         if (file_lock_table != NULL)
 858                 file_lock_table->fl_prevlink = fl;
 859         file_lock_table = fl;
 860         fl->fl_next = *pos;     /* insert into file's list */
 861         *pos = fl;
 862 
 863         return;
 864 }
 865 
 866 /* Delete a lock and free it.
 867  * First remove our lock from the lock lists. Then remove all the blocked
 868  * locks from our blocked list, waking up the processes that own them. If
 869  * told to wait, then sleep on each of these lock's wait queues. Each
 870  * blocked process will wake up and immediately wake up its own wait queue
 871  * allowing us to be scheduled again. Lastly, wake up our own wait queue
 872  * before freeing the file_lock structure.
 873  */
 874 
 875 static void locks_delete_lock(struct file_lock **fl_p, unsigned int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877         struct file_lock *fl;
 878         struct file_lock *pfl;
 879         struct file_lock *nfl;
 880         
 881         fl = *fl_p;
 882         *fl_p = fl->fl_next;
 883         pfl = fl->fl_prevlink;
 884         nfl = fl->fl_nextlink;
 885 
 886         if (nfl != NULL)
 887                 nfl->fl_prevlink = pfl;
 888 
 889         if (pfl != NULL)
 890                 pfl->fl_nextlink = nfl;
 891         else
 892                 file_lock_table = nfl;
 893         
 894         while ((nfl = fl->fl_block) != NULL) {
 895                 fl->fl_block = nfl->fl_block;
 896                 nfl->fl_block = NULL;
 897                 wake_up(&nfl->fl_wait);
 898                 if (wait)
 899                         sleep_on(&nfl->fl_wait);
 900         }
 901 
 902         wake_up(&fl->fl_wait);
 903         kfree(fl);
 904 
 905         return;
 906 }

/* [previous][next][first][last][top][bottom][index][help] */