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. locks_deadlock
  19. flock_lock_file
  20. posix_lock_file
  21. locks_alloc_lock
  22. locks_insert_lock
  23. locks_delete_lock
  24. lock_get_status
  25. get_locks_status

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

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