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

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