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

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