Author: vlendec
Date: 2007-09-04 15:01:57 +0000 (Tue, 04 Sep 2007)
New Revision: 24956

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=24956

Log:
Merge r21879:

    Move process_blocking_lock_queue to a timed event.
    
    The idea is that we have blocking.c:brl_timeout as a timed
    event that is present whenever we do have a blocking lock
    pending. It fires brl_timeout_fn() which calls
    process_blocking_lock_queue().
    
    Whenever we make changes to blocking_lock_queue, we trigger
    a recalc_brl_timeout() which sets a new brl_timout event if
    necessary. This makes the call to
    blocking_locks_timeout_ms() in setup_select_timeout()
    unnecessary, this is implicitly done in
    event_add_to_select_args() from the timed events.
    
    Volker
    


Modified:
   branches/SAMBA_3_2_0/source/smbd/blocking.c
   branches/SAMBA_3_2_0/source/smbd/process.c


Changeset:
Modified: branches/SAMBA_3_2_0/source/smbd/blocking.c
===================================================================
--- branches/SAMBA_3_2_0/source/smbd/blocking.c 2007-09-04 14:54:00 UTC (rev 
24955)
+++ branches/SAMBA_3_2_0/source/smbd/blocking.c 2007-09-04 15:01:57 UTC (rev 
24956)
@@ -49,6 +49,9 @@
 /* dlink list we move cancelled lock records onto. */
 static blocking_lock_record *blocking_lock_cancelled_queue;
 
+/* The event that makes us process our blocking lock queue */
+static struct timed_event *brl_timeout;
+
 /****************************************************************************
  Destructor for the above structure.
 ****************************************************************************/
@@ -73,8 +76,75 @@
                                uint32_t msg_type,
                                struct server_id server_id,
                                DATA_BLOB *data);
+static void process_blocking_lock_queue(void);
 
+static void brl_timeout_fn(struct event_context *event_ctx,
+                          struct timed_event *te,
+                          const struct timeval *now,
+                          void *private_data)
+{
+       SMB_ASSERT(brl_timeout == te);
+       TALLOC_FREE(brl_timeout);
+
+       change_to_root_user();  /* TODO: Possibly run all timed events as
+                                * root */
+
+       process_blocking_lock_queue();
+}
+
 /****************************************************************************
+ After a change to blocking_lock_queue, recalculate the timed_event for the
+ next processing.
+****************************************************************************/
+
+static BOOL recalc_brl_timeout(void)
+{
+       blocking_lock_record *brl;
+       struct timeval next_timeout;
+
+       TALLOC_FREE(brl_timeout);
+
+       next_timeout = timeval_zero();  
+
+       for (brl = blocking_lock_queue; brl; brl = brl->next) {
+               if (timeval_is_zero(&brl->expire_time)) {
+                       /*
+                        * If we're blocked on pid 0xFFFFFFFF this is
+                        * a POSIX lock, so calculate a timeout of
+                        * 10 seconds into the future.
+                        */
+                        if (brl->blocking_pid == 0xFFFFFFFF) {
+                               struct timeval psx_to = timeval_current_ofs(10, 
0);
+                               next_timeout = timeval_min(&next_timeout, 
&psx_to);
+                        }
+
+                       continue;
+               }
+
+               if (timeval_is_zero(&next_timeout)) {
+                       next_timeout = brl->expire_time;
+               }
+               else {
+                       next_timeout = timeval_min(&next_timeout,
+                                                  &brl->expire_time);
+               }
+       }
+
+       if (timeval_is_zero(&next_timeout)) {
+               return True;
+       }
+
+       if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
+                                           next_timeout, "brl_timeout",
+                                           brl_timeout_fn, NULL))) {
+               return False;
+       }
+
+       return True;
+}
+
+
+/****************************************************************************
  Function to push a blocking lock request onto the lock queue.
 ****************************************************************************/
 
@@ -156,6 +226,7 @@
        }
 
        DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
+       recalc_brl_timeout();
 
        /* Ensure we'll receive messages when this is unlocked. */
        if (!set_lock_msg) {
@@ -596,66 +667,14 @@
 }
 
 /****************************************************************************
- Return the number of milliseconds to the next blocking locks timeout, or 
default_timeout
-*****************************************************************************/
-
-unsigned int blocking_locks_timeout_ms(unsigned int default_timeout_ms)
-{
-       unsigned int timeout_ms = default_timeout_ms;
-       struct timeval tv_curr;
-       SMB_BIG_INT min_tv_dif_us = default_timeout_ms * 1000;
-       blocking_lock_record *blr = blocking_lock_queue;
-
-       /* note that we avoid the GetTimeOfDay() syscall if there are no 
blocking locks */
-       if (!blr) {
-               return timeout_ms;
-       }
-
-       tv_curr = timeval_current();
-
-       for (; blr; blr = blr->next) {
-               SMB_BIG_INT tv_dif_us;
-
-               if (timeval_is_zero(&blr->expire_time)) {
-                       /*
-                        * If we're blocked on pid 0xFFFFFFFF this is
-                        * a POSIX lock, so calculate a timeout of
-                        * 10 seconds.
-                        */
-                       if (blr->blocking_pid == 0xFFFFFFFF) {
-                               tv_dif_us = 10 * 1000 * 1000;
-                               min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
-                       }
-                       continue; /* Never timeout. */
-               }
-
-               tv_dif_us = usec_time_diff(&blr->expire_time, &tv_curr);
-               min_tv_dif_us = MIN(min_tv_dif_us, tv_dif_us);
-       }
-
-       if (min_tv_dif_us < 0) {
-               min_tv_dif_us = 0;
-       }
-
-       timeout_ms = (unsigned int)(min_tv_dif_us / (SMB_BIG_INT)1000);
-
-       if (timeout_ms < 1) {
-               timeout_ms = 1;
-       }
-
-       DEBUG(10,("blocking_locks_timeout_ms: returning %u\n", timeout_ms));
-
-       return timeout_ms;
-}
-
-/****************************************************************************
  Process the blocking lock queue. Note that this is only called as root.
 *****************************************************************************/
 
-void process_blocking_lock_queue(void)
+static void process_blocking_lock_queue(void)
 {
        struct timeval tv_curr = timeval_current();
        blocking_lock_record *blr, *next = NULL;
+       BOOL recalc_timeout = False;
 
        /*
         * Go through the queue and see if we can get any of the locks.
@@ -682,6 +701,7 @@
 
                DEBUG(5,("process_blocking_lock_queue: examining pending lock 
fnum = %d for file %s\n",
                        fsp->fnum, fsp->fsp_name ));
+
                if(!change_to_user(conn,vuid)) {
                        struct byte_range_lock *br_lck = brl_get_locks(NULL, 
fsp);
 
@@ -704,6 +724,7 @@
                        blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
                        DLIST_REMOVE(blocking_lock_queue, blr);
                        free_blocking_lock_record(blr);
+                       recalc_timeout = True;
                        continue;
                }
 
@@ -728,6 +749,7 @@
                        blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
                        DLIST_REMOVE(blocking_lock_queue, blr);
                        free_blocking_lock_record(blr);
+                       recalc_timeout = True;
                        change_to_root_user();
                        continue;
                }
@@ -753,9 +775,11 @@
 
                        DLIST_REMOVE(blocking_lock_queue, blr);
                        free_blocking_lock_record(blr);
+                       recalc_timeout = True;
                        change_to_root_user();
                        continue;
                }
+
                change_to_root_user();
 
                /*
@@ -787,9 +811,13 @@
                        
blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
                        DLIST_REMOVE(blocking_lock_queue, blr);
                        free_blocking_lock_record(blr);
-                       continue;
+                       recalc_timeout = True;
                }
        }
+
+       if (recalc_timeout) {
+               recalc_brl_timeout();
+       }
 }
 
 /****************************************************************************

Modified: branches/SAMBA_3_2_0/source/smbd/process.c
===================================================================
--- branches/SAMBA_3_2_0/source/smbd/process.c  2007-09-04 14:54:00 UTC (rev 
24955)
+++ branches/SAMBA_3_2_0/source/smbd/process.c  2007-09-04 15:01:57 UTC (rev 
24956)
@@ -1441,7 +1441,7 @@
 {
        int select_timeout;
 
-       select_timeout = blocking_locks_timeout_ms(SMBD_SELECT_TIMEOUT*1000);
+       select_timeout = SMBD_SELECT_TIMEOUT*1000;
 
        if (print_notify_messages_pending()) {
                select_timeout = MIN(select_timeout, 1000);
@@ -1627,12 +1627,6 @@
                secrets_lock_trust_account_password(lp_workgroup(), False);
        }
 
-       /*
-        * Check to see if we have any blocking locks
-        * outstanding on the queue.
-        */
-       process_blocking_lock_queue();
-
        /* update printer queue caches if necessary */
   
        update_monitored_printq_cache();

Reply via email to