first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / locking / locking_shm.c
index cded5e628dc20c32c2e06fde7186a7607eb40493..174ad73e97758d3788b1a6537a9ea53c303e0782 100644 (file)
@@ -45,8 +45,8 @@ typedef struct
 {
   int next_offset; /* offset of next record in chain from hash bucket */
   int locking_version;
-  int32 st_dev;
-  int32 st_ino;
+  SMB_DEV_T st_dev;
+  SMB_INO_T st_ino;
   int num_share_mode_entries;
   int share_mode_entries; /* Chain of share mode entries for this file */
   char file_name[1];
@@ -63,7 +63,7 @@ static int read_only;
 
 
 /* Conversion to hash entry index from device and inode numbers. */
-#define HASH_ENTRY(dev,ino) ((((uint32)(dev)) ^ ((uint32)(ino))) % shmops->hash_size())
+#define HASH_ENTRY(dev,ino) ((unsigned int)(((dev) ^ (ino)) % shmops->hash_size()))
 
 
 /*******************************************************************
@@ -78,8 +78,9 @@ static BOOL shm_stop_share_mode_mgmt(void)
   lock a hash bucket entry in shared memory for share_mode management 
   ******************************************************************/
 static BOOL shm_lock_share_entry(connection_struct *conn,
-                                uint32 dev, uint32 inode, int *ptok)
+                                SMB_DEV_T dev, SMB_INO_T inode, int *ptok)
 {
+       *ptok = 0; /* For purify... */
        return shmops->lock_hash_entry(HASH_ENTRY(dev, inode));
 }
 
@@ -87,7 +88,7 @@ static BOOL shm_lock_share_entry(connection_struct *conn,
   unlock a hash bucket entry in shared memory for share_mode management 
   ******************************************************************/
 static BOOL shm_unlock_share_entry(connection_struct *conn,
-                                  uint32 dev, uint32 inode, int token)
+                                  SMB_DEV_T dev, SMB_INO_T inode, int token)
 {
   return shmops->unlock_hash_entry(HASH_ENTRY(dev, inode));
 }
@@ -96,7 +97,7 @@ static BOOL shm_unlock_share_entry(connection_struct *conn,
 get all share mode entries in shared memory for a dev/inode pair.
 ********************************************************************/
 static int shm_get_share_modes(connection_struct *conn,
-                              int token, uint32 dev, uint32 inode, 
+                              int token, SMB_DEV_T dev, SMB_INO_T inode, 
                               share_mode_entry **old_shares)
 {
   int *mode_array;
@@ -139,15 +140,16 @@ static int shm_get_share_modes(connection_struct *conn,
   
   if(!found)
   {
-    DEBUG(5,("get_share_modes no entry for file dev = %d ino = %d\n",
-            dev, inode));
+    DEBUG(5,("get_share_modes no entry for file dev = %x ino = %.0f\n",
+            (unsigned int)dev, (double)inode));
     return (0);
   }
   
   if(file_scanner_p->locking_version != LOCKING_VERSION)
   {
-    DEBUG(0,("ERROR: get_share_modes  Deleting old share mode v1 %d dev=%d ino=%d\n", 
-            file_scanner_p->locking_version, dev, inode));
+    DEBUG(0,("ERROR: get_share_modes  Deleting old share mode v1 %d dev=%x ino=%.0f\n", 
+            file_scanner_p->locking_version, (unsigned int)dev, (double)inode));
+
     if(file_prev_p == file_scanner_p)
       mode_array[hash_entry] = file_scanner_p->next_offset;
     else
@@ -164,7 +166,7 @@ static int shm_get_share_modes(connection_struct *conn,
                  malloc(num_entries * sizeof(share_mode_entry));
     if(*old_shares == 0)
     {
-      DEBUG(0,("get_share_modes: malloc fail!\n"));
+      DEBUG(0,("get_share_modes: malloc fail for size 0x%x!\n", (unsigned int)(num_entries * sizeof(share_mode_entry))));
       return 0;
     }
   }
@@ -176,7 +178,7 @@ static int shm_get_share_modes(connection_struct *conn,
   entry_prev_p = entry_scanner_p;
   while(entry_scanner_p)
   {
-    int pid = entry_scanner_p->e.pid;
+    pid_t pid = entry_scanner_p->e.pid;
 
     if (pid && !process_exists(pid))
     {
@@ -203,12 +205,12 @@ static int shm_get_share_modes(connection_struct *conn,
       /* PARANOIA TEST */
       if(file_scanner_p->num_share_mode_entries < 0)
       {
-        DEBUG(0,("PANIC ERROR: get_share_mode: entries=%d dev=%d ino=%d\n",
-                file_scanner_p->num_share_mode_entries,dev, inode));
+        DEBUG(0,("PANIC ERROR: get_share_mode: entries=%d dev=%x ino=%.0f\n",
+                file_scanner_p->num_share_mode_entries, (unsigned int)dev, (double)inode));
         return 0;
       }
 
-      DEBUG(0,("get_share_modes: process %d no longer exists\n", pid));
+      DEBUG(0,("get_share_modes: process %d no longer exists\n", (int)pid));
 
       shmops->shm_free(shmops->addr2offset(delete_entry_p));
     } 
@@ -225,7 +227,7 @@ static int shm_get_share_modes(connection_struct *conn,
               sizeof(struct timeval));
        num_entries_copied++;
        DEBUG(5,("get_share_modes Read share mode 0x%X pid=%d\n", 
-               entry_scanner_p->e.share_mode, entry_scanner_p->e.pid));
+               entry_scanner_p->e.share_mode, (int)entry_scanner_p->e.pid));
        entry_prev_p = entry_scanner_p;
        entry_scanner_p = (shm_share_mode_entry *)
                            shmops->offset2addr(entry_scanner_p->next_share_mode_entry);
@@ -235,8 +237,9 @@ static int shm_get_share_modes(connection_struct *conn,
   /* If no valid share mode entries were found then this record shouldn't exist ! */
   if(num_entries_copied == 0)
   {
-    DEBUG(0,("get_share_modes: file with dev %d inode %d empty\n", 
-            dev, inode));
+    DEBUG(0,("get_share_modes: file with dev %x inode %.0f empty\n", 
+            (unsigned int)dev, (double)inode));
+
     if(*old_shares)
       free((char *)*old_shares);
     *old_shares = 0;
@@ -248,8 +251,8 @@ static int shm_get_share_modes(connection_struct *conn,
     shmops->shm_free(shmops->addr2offset(file_scanner_p));
   }
 
-  DEBUG(5,("get_share_modes: file with dev %d inode %d -> %d entries\n",
-          dev, inode, num_entries_copied));
+  DEBUG(5,("get_share_modes: file with dev %x inode %.0f -> %d entries\n",
+          (unsigned int)dev, (double)inode, num_entries_copied));
 
   return(num_entries_copied);
 }  
@@ -259,7 +262,8 @@ del the share mode of a file.
 ********************************************************************/
 static void shm_del_share_mode(int token, files_struct *fsp)
 {
-  uint32 dev, inode;
+  SMB_DEV_T dev;
+  SMB_INO_T inode;
   int *mode_array;
   unsigned int hash_entry;
   share_mode_record *file_scanner_p;
@@ -267,7 +271,7 @@ static void shm_del_share_mode(int token, files_struct *fsp)
   shm_share_mode_entry *entry_scanner_p;
   shm_share_mode_entry *entry_prev_p;
   BOOL found = False;
-  int pid = getpid();
+  pid_t pid = getpid();
 
   dev = fsp->fd_ptr->dev;
   inode = fsp->fd_ptr->inode;
@@ -303,15 +307,16 @@ static void shm_del_share_mode(int token, files_struct *fsp)
     
   if(!found)
   {
-     DEBUG(0,("ERROR: del_share_mode no entry for dev %d inode %d\n",
-             dev, inode));
+     DEBUG(0,("ERROR: del_share_mode no entry for dev %x inode %.0f\n",
+             (unsigned int)dev, (double)inode));
      return;
   }
   
   if(file_scanner_p->locking_version != LOCKING_VERSION)
   {
-    DEBUG(0,("ERROR: del_share_modes Deleting old share mode v1 %d dev=%d ino=%d\n",
-            file_scanner_p->locking_version, dev, inode));
+    DEBUG(0,("ERROR: del_share_modes Deleting old share mode v1 %d dev=%x ino=%.0f\n",
+            file_scanner_p->locking_version, (unsigned int)dev, (double)inode));
+
     if(file_prev_p == file_scanner_p)
       mode_array[hash_entry] = file_scanner_p->next_offset;
     else
@@ -346,8 +351,9 @@ static void shm_del_share_mode(int token, files_struct *fsp)
     /* Decrement the number of entries in the record. */
     file_scanner_p->num_share_mode_entries -= 1;
 
-    DEBUG(2,("del_share_modes Deleting share mode entry dev=%d ino=%d\n",
-              dev, inode));
+    DEBUG(2,("del_share_modes Deleting share mode entry dev=%x ino=%.0f\n",
+          (unsigned int)dev, (double)inode));
+
     if(entry_prev_p == entry_scanner_p)
       /* We are at start of list */
       file_scanner_p->share_mode_entries = entry_scanner_p->next_share_mode_entry;
@@ -366,8 +372,9 @@ static void shm_del_share_mode(int token, files_struct *fsp)
     /* If we deleted the last share mode entry then remove the share mode record. */
     if(file_scanner_p->num_share_mode_entries == 0)
     {
-      DEBUG(2,("del_share_modes num entries = 0, deleting share_mode dev=%d ino=%d\n", 
-              dev, inode));
+      DEBUG(2,("del_share_modes num entries = 0, deleting share_mode dev=%x ino=%.0f\n", 
+              (unsigned int)dev, (double)inode));
+
       if(file_prev_p == file_scanner_p)
         mode_array[hash_entry] = file_scanner_p->next_offset;
       else
@@ -377,8 +384,8 @@ static void shm_del_share_mode(int token, files_struct *fsp)
   }
   else
   {
-    DEBUG(0,("ERROR: del_share_modes No share mode dev=%d ino=%d\n", 
-            dev, inode));
+    DEBUG(0,("ERROR: del_share_modes No share mode dev=%x ino=%.0f\n", 
+            (unsigned int)dev, (double)inode));
   }
 }
 
@@ -387,11 +394,11 @@ set the share mode of a file. Return False on fail, True on success.
 ********************************************************************/
 static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16 op_type)
 {
-  int32 dev, inode;
+  SMB_DEV_T dev;
+  SMB_INO_T inode;
   int *mode_array;
   unsigned int hash_entry;
   share_mode_record *file_scanner_p;
-  share_mode_record *file_prev_p;
   shm_share_mode_entry *new_entry_p;
   int new_entry_offset;
   BOOL found = False;
@@ -404,7 +411,6 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
   mode_array = (int *)shmops->offset2addr(shmops->get_userdef_off());
 
   file_scanner_p = (share_mode_record *)shmops->offset2addr(mode_array[hash_entry]);
-  file_prev_p = file_scanner_p;
   
   while(file_scanner_p)
   {
@@ -415,7 +421,6 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
     }
     else
     {
-      file_prev_p = file_scanner_p ;
       file_scanner_p = (share_mode_record *)
                          shmops->offset2addr(file_scanner_p->next_offset);
     }
@@ -426,7 +431,7 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
     /* We must create a share_mode_record */
     share_mode_record *new_mode_p = NULL;
     int new_offset = shmops->shm_alloc(sizeof(share_mode_record) +
-                                  strlen(fsp->fsp_name) + 1);
+                                  strlen(fsp->fsp_name) + strlen(fsp->conn->connectpath) + 2);
     if(new_offset == 0) {
            DEBUG(0,("ERROR:set_share_mode shmops->shm_alloc fail!\n"));
            return False;
@@ -437,7 +442,9 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
     new_mode_p->st_ino = inode;
     new_mode_p->num_share_mode_entries = 0;
     new_mode_p->share_mode_entries = 0;
-    pstrcpy(new_mode_p->file_name, fsp->fsp_name);
+    pstrcpy(new_mode_p->file_name, fsp->conn->connectpath);
+    pstrcat(new_mode_p->file_name, "/");
+    pstrcat(new_mode_p->file_name, fsp->fsp_name);
 
     /* Chain onto the start of the hash chain (in the hope we will be used first). */
     new_mode_p->next_offset = mode_array[hash_entry];
@@ -445,8 +452,8 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
 
     file_scanner_p = new_mode_p;
 
-    DEBUG(3,("set_share_mode: Created share record for %s (dev %d inode %d)\n", 
-            fsp->fsp_name, dev, inode));
+    DEBUG(3,("set_share_mode: Created share record for %s (dev %x inode %.0f)\n", 
+            fsp->fsp_name, (unsigned int)dev, (double)inode));
   }
  
   /* Now create the share mode entry */ 
@@ -485,25 +492,28 @@ static BOOL shm_set_share_mode(int token, files_struct *fsp, uint16 port, uint16
   file_scanner_p->num_share_mode_entries += 1;
 
   DEBUG(3,("set_share_mode: Created share entry for %s with mode 0x%X pid=%d\n",
-          fsp->fsp_name, fsp->share_mode, new_entry_p->e.pid));
+          fsp->fsp_name, fsp->share_mode, (int)new_entry_p->e.pid));
 
   return(True);
 }
 
 /*******************************************************************
-Remove an oplock port and mode entry from a share mode.
+ Call a generic modify function for a share mode entry.
 ********************************************************************/
-static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
+
+static BOOL shm_mod_share_entry(int token, files_struct *fsp,
+                                void (*mod_fn)(share_mode_entry *, SMB_DEV_T, SMB_INO_T, void *),
+                                void *param)
 {
-  uint32 dev, inode;
+  SMB_DEV_T dev;
+  SMB_INO_T inode;
   int *mode_array;
   unsigned int hash_entry;
   share_mode_record *file_scanner_p;
   share_mode_record *file_prev_p;
   shm_share_mode_entry *entry_scanner_p;
-  shm_share_mode_entry *entry_prev_p;
   BOOL found = False;
-  int pid = getpid();
+  pid_t pid = getpid();
 
   dev = fsp->fd_ptr->dev;
   inode = fsp->fd_ptr->inode;
@@ -514,7 +524,7 @@ static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
 
   if(mode_array[hash_entry] == 0)
   {
-    DEBUG(0,("PANIC ERROR:remove_share_oplock: hash bucket %d empty\n",
+    DEBUG(0,("PANIC ERROR:modify_share_entry: hash bucket %d empty\n",
                   hash_entry));
     return False;
   } 
@@ -539,15 +549,16 @@ static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
    
   if(!found)
   { 
-     DEBUG(0,("ERROR:remove_share_oplock: no entry found for dev=%d ino=%d\n", 
-             dev, inode));
+     DEBUG(0,("ERROR:modify_share_entry: no entry found for dev=%x ino=%.0f\n", 
+             (unsigned int)dev, (double)inode));
      return False;
   } 
 
   if(file_scanner_p->locking_version != LOCKING_VERSION)
   {
-    DEBUG(0,("ERROR: remove_share_oplock: Deleting old share mode v1=%d dev=%d ino=%d\n",
-            file_scanner_p->locking_version, dev, inode));
+    DEBUG(0,("ERROR: modify_share_entry: Deleting old share mode v1=%d dev=%x ino=%.0f\n",
+            file_scanner_p->locking_version, (unsigned int)dev, (double)inode));
+
     if(file_prev_p == file_scanner_p)
       mode_array[hash_entry] = file_scanner_p->next_offset;
     else
@@ -559,7 +570,6 @@ static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
   found = False;
   entry_scanner_p = (shm_share_mode_entry*)shmops->offset2addr(
                                          file_scanner_p->share_mode_entries);
-  entry_prev_p = entry_scanner_p;
   while(entry_scanner_p)
   {
     if( (pid == entry_scanner_p->e.pid) && 
@@ -567,15 +577,19 @@ static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
         (memcmp(&entry_scanner_p->e.time, 
                 &fsp->open_time,sizeof(struct timeval)) == 0) )
     {
-      /* Delete the oplock info. */
-      entry_scanner_p->e.op_port = 0;
-      entry_scanner_p->e.op_type = 0;
+      /*
+       * Call the generic function with the given parameter.
+       */
+
+      DEBUG(5,("modify_share_entry: Calling generic function to modify entry for dev=%x ino=%.0f\n",
+            (unsigned int)dev, (double)inode));
+
+      (*mod_fn)( &entry_scanner_p->e, dev, inode, param);
       found = True;
       break;
     }
     else
     {
-      entry_prev_p = entry_scanner_p;
       entry_scanner_p = (shm_share_mode_entry *)
                           shmops->offset2addr(entry_scanner_p->next_share_mode_entry);
     }
@@ -583,15 +597,14 @@ static BOOL shm_remove_share_oplock(files_struct *fsp, int token)
 
   if(!found)
   {
-    DEBUG(0,("ERROR: remove_share_oplock: No oplock granted. dev=%d ino=%d\n", 
-            dev, inode));
+    DEBUG(0,("ERROR: modify_share_entry: No entry found for dev=%x ino=%.0f\n", 
+            (unsigned int)dev, (double)inode));
     return False;
   }
 
   return True;
 }
 
-
 /*******************************************************************
 call the specified function on each entry under management by the
 share mode system
@@ -667,7 +680,7 @@ static struct share_ops share_ops = {
        shm_get_share_modes,
        shm_del_share_mode,
        shm_set_share_mode,
-       shm_remove_share_oplock,
+       shm_mod_share_entry,
        shm_share_forall,
        shm_share_status,
 };
@@ -679,12 +692,12 @@ struct share_ops *locking_shm_init(int ronly)
 {
        read_only = ronly;
 
-#ifdef HAVE_SYSV_IPC
+#ifdef USE_SYSV_IPC
        shmops = sysv_shm_open(read_only);
        if (shmops) return &share_ops;
 #endif
 
-#ifdef HAVE_SHARED_MMAP
+#ifdef USE_SHARED_MMAP
        shmops = smb_shm_open(read_only);
        if (shmops) return &share_ops;
 #endif
@@ -696,6 +709,3 @@ struct share_ops *locking_shm_init(int ronly)
  int locking_shm_dummy_procedure(void)
 {return 0;}
 #endif /* FAST_SHARE_MODES */
-
-
-