The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/vinum/vinumlock.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1997, 1998
    3  *      Nan Yang Computer Services Limited.  All rights reserved.
    4  *
    5  *  Parts copyright (c) 1997, 1998 Cybernet Corporation, NetMAX project.
    6  *
    7  *  Written by Greg Lehey
    8  *
    9  *  This software is distributed under the so-called ``Berkeley
   10  *  License'':
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by Nan Yang Computer
   23  *      Services Limited.
   24  * 4. Neither the name of the Company nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * This software is provided ``as is'', and any express or implied
   29  * warranties, including, but not limited to, the implied warranties of
   30  * merchantability and fitness for a particular purpose are disclaimed.
   31  * In no event shall the company or contributors be liable for any
   32  * direct, indirect, incidental, special, exemplary, or consequential
   33  * damages (including, but not limited to, procurement of substitute
   34  * goods or services; loss of use, data, or profits; or business
   35  * interruption) however caused and on any theory of liability, whether
   36  * in contract, strict liability, or tort (including negligence or
   37  * otherwise) arising in any way out of the use of this software, even if
   38  * advised of the possibility of such damage.
   39  *
   40  * $Id: vinumlock.c,v 1.19 2003/05/23 01:07:18 grog Exp $
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __FBSDID("$FreeBSD$");
   45 
   46 #include <dev/vinum/vinumhdr.h>
   47 #include <dev/vinum/request.h>
   48 
   49 /* Lock a drive, wait if it's in use */
   50 #ifdef VINUMDEBUG
   51 int
   52 lockdrive(struct drive *drive, char *file, int line)
   53 #else
   54 int
   55 lockdrive(struct drive *drive)
   56 #endif
   57 {
   58     int error;
   59 
   60     /* XXX get rid of     drive->flags |= VF_LOCKING; */
   61     if ((drive->flags & VF_LOCKED)                          /* it's locked */
   62     &&(drive->pid == curproc->p_pid)) {                     /* by us! */
   63 #ifdef VINUMDEBUG
   64         log(LOG_WARNING,
   65             "vinum lockdrive: already locking %s from %s:%d, called from %s:%d\n",
   66             drive->label.name,
   67             drive->lockfilename,
   68             drive->lockline,
   69             basename(file),
   70             line);
   71 #else
   72         log(LOG_WARNING,
   73             "vinum lockdrive: already locking %s\n",
   74             drive->label.name);
   75 #endif
   76         return 0;
   77     }
   78     while ((drive->flags & VF_LOCKED) != 0) {
   79         /*
   80          * There are problems sleeping on a unique identifier,
   81          * since the drive structure can move, and the unlock
   82          * function can be called after killing the drive.
   83          * Solve this by waiting on this function; the number
   84          * of conflicts is negligible.
   85          */
   86         if ((error = tsleep(&lockdrive,
   87                     PRIBIO,
   88                     "vindrv",
   89                     0)) != 0)
   90             return error;
   91     }
   92     drive->flags |= VF_LOCKED;
   93     drive->pid = curproc->p_pid;                            /* it's a panic error if curproc is null */
   94 #ifdef VINUMDEBUG
   95     bcopy(basename(file), drive->lockfilename, 15);
   96     drive->lockfilename[15] = '\0';                         /* truncate if necessary */
   97     drive->lockline = line;
   98 #endif
   99     return 0;
  100 }
  101 
  102 /* Unlock a drive and let the next one at it */
  103 void
  104 unlockdrive(struct drive *drive)
  105 {
  106     drive->flags &= ~VF_LOCKED;
  107     /* we don't reset pid: it's of hysterical interest */
  108     wakeup(&lockdrive);
  109 }
  110 
  111 /* Lock a stripe of a plex, wait if it's in use */
  112 struct rangelock *
  113 lockrange(daddr_t stripe, struct buf *bp, struct plex *plex)
  114 {
  115     struct rangelock *lock;
  116     struct rangelock *pos;                                  /* position of first free lock */
  117     int foundlocks;                                         /* number of locks found */
  118 
  119     /*
  120      * We could get by without counting the number
  121      * of locks we find, but we have a linear search
  122      * through a table which in most cases will be
  123      * empty.  It's faster to stop when we've found
  124      * all the locks that are there.  This is also
  125      * the reason why we put pos at the beginning
  126      * instead of the end, though it requires an
  127      * extra test.
  128      */
  129     pos = NULL;
  130     foundlocks = 0;
  131 
  132     /*
  133      * we can't use 0 as a valid address, so
  134      * increment all addresses by 1.
  135      */
  136     stripe++;
  137     mtx_lock(plex->lockmtx);
  138 
  139     /* Wait here if the table is full */
  140     while (plex->usedlocks == PLEX_LOCKS)                   /* all in use */
  141         msleep(&plex->usedlocks, plex->lockmtx, PRIBIO, "vlock", 0);
  142 
  143 #ifdef DIAGNOSTIC
  144     if (plex->usedlocks >= PLEX_LOCKS)
  145         panic("lockrange: Too many locks in use");
  146 #endif
  147 
  148     lock = plex->lock;                                      /* pointer in lock table */
  149     if (plex->usedlocks > 0)                                /* something locked, */
  150         /* Search the lock table for our stripe */
  151         for (; lock < &plex->lock[PLEX_LOCKS]
  152             && foundlocks < plex->usedlocks;
  153             lock++) {
  154             if (lock->stripe) {                             /* in use */
  155                 foundlocks++;                               /* found another one in use */
  156                 if ((lock->stripe == stripe)                /* it's our stripe */
  157                 &&(lock->bp != bp)) {                       /* but not our request */
  158 #ifdef VINUMDEBUG
  159                     if (debug & DEBUG_LOCKREQS) {
  160                         struct rangelockinfo lockinfo;
  161 
  162                         lockinfo.stripe = stripe;
  163                         lockinfo.bp = bp;
  164                         lockinfo.plexno = plex->plexno;
  165                         logrq(loginfo_lockwait, (union rqinfou) &lockinfo, bp);
  166                     }
  167 #endif
  168                     plex->lockwaits++;                      /* waited one more time */
  169                     msleep(lock, plex->lockmtx, PRIBIO, "vrlock", 0);
  170                     lock = &plex->lock[-1];                 /* start again */
  171                     foundlocks = 0;
  172                     pos = NULL;
  173                 }
  174             } else if (pos == NULL)                         /* still looking for somewhere? */
  175                 pos = lock;                                 /* a place to put this one */
  176         }
  177     /*
  178      * This untidy looking code ensures that we'll
  179      * always end up pointing to the first free lock
  180      * entry, thus minimizing the number of
  181      * iterations necessary.
  182      */
  183     if (pos == NULL)                                        /* didn't find one on the way, */
  184         pos = lock;                                         /* use the one we're pointing to */
  185 
  186     /*
  187      * The address range is free, and we're pointing
  188      * to the first unused entry.  Make it ours.
  189      */
  190     pos->stripe = stripe;
  191     pos->bp = bp;
  192     plex->usedlocks++;                                      /* one more lock */
  193     mtx_unlock(plex->lockmtx);
  194 #ifdef VINUMDEBUG
  195     if (debug & DEBUG_LOCKREQS) {
  196         struct rangelockinfo lockinfo;
  197 
  198         lockinfo.stripe = stripe;
  199         lockinfo.bp = bp;
  200         lockinfo.plexno = plex->plexno;
  201         logrq(loginfo_lock, (union rqinfou) &lockinfo, bp);
  202     }
  203 #endif
  204     return pos;
  205 }
  206 
  207 /* Unlock a volume and let the next one at it */
  208 void
  209 unlockrange(int plexno, struct rangelock *lock)
  210 {
  211     struct plex *plex;
  212 
  213     plex = &PLEX[plexno];
  214 #ifdef DIAGNOSTIC
  215     if (lock < &plex->lock[0] || lock >= &plex->lock[PLEX_LOCKS])
  216         panic("vinum: rangelock %p on plex %d invalid, not between %p and %p",
  217             lock,
  218             plexno,
  219             &plex->lock[0],
  220             &plex->lock[PLEX_LOCKS]);
  221 #endif
  222 #ifdef VINUMDEBUG
  223     if (debug & DEBUG_LOCKREQS) {
  224         struct rangelockinfo lockinfo;
  225 
  226         lockinfo.stripe = lock->stripe;
  227         lockinfo.bp = lock->bp;
  228         lockinfo.plexno = plex->plexno;
  229         logrq(loginfo_lockwait, (union rqinfou) &lockinfo, lock->bp);
  230     }
  231 #endif
  232     lock->stripe = 0;                                       /* no longer used */
  233     plex->usedlocks--;                                      /* one less lock */
  234     if (plex->usedlocks == PLEX_LOCKS - 1)                  /* we were full, */
  235         wakeup(&plex->usedlocks);                           /* get a waiter if one's there */
  236     wakeup((void *) lock);
  237 }
  238 
  239 /* Get a lock for the global config.  Wait if it's not available. */
  240 int
  241 lock_config(void)
  242 {
  243     int error;
  244 
  245     while ((vinum_conf.flags & VF_LOCKED) != 0) {
  246         vinum_conf.flags |= VF_LOCKING;
  247         if ((error = tsleep(&vinum_conf, PRIBIO, "vincfg", 0)) != 0)
  248             return error;
  249     }
  250     vinum_conf.flags |= VF_LOCKED;
  251     return 0;
  252 }
  253 
  254 /* Unlock global config and wake up any waiters. */
  255 void
  256 unlock_config(void)
  257 {
  258     vinum_conf.flags &= ~VF_LOCKED;
  259     if ((vinum_conf.flags & VF_LOCKING) != 0) {
  260         vinum_conf.flags &= ~VF_LOCKING;
  261         wakeup(&vinum_conf);
  262     }
  263 }
  264 /* Local Variables: */
  265 /* fill-column: 50 */
  266 /* End: */

Cache object: 68166beb93ff938df4c5862bebea9f4b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.