summaryrefslogtreecommitdiffstats
path: root/source/locking/shmem.c
blob: f3f84ec8c36882b387952e9b0501246bc8994144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Shared memory functions
   Copyright (C) Erik Devriendt 1996-1997
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "includes.h"


#ifdef FAST_SHARE_MODES


extern int DEBUGLEVEL;


#define SMB_SHM_MAGIC 0x53484100
/* = "SHM" in hex */

#define SMB_SHM_VERSION 2

/* WARNING : offsets are used because mmap() does not guarantee that all processes have the 
   shared memory mapped to the same address */

struct SmbShmHeader
{
   int smb_shm_magic;
   int smb_shm_version;
   int total_size;	/* in bytes */
   BOOL consistent;
   smb_shm_offset_t first_free_off;
   smb_shm_offset_t userdef_off;    /* a userdefined offset. can be used to store root of tree or list */
   struct {		/* a cell is a range of bytes of sizeof(struct SmbShmBlockDesc) size */
      int cells_free;
      int cells_used;
      int cells_system; /* number of cells used as allocated block descriptors */
   } statistics;
};

#define SMB_SHM_NOT_FREE_OFF (-1)
struct SmbShmBlockDesc
{
   smb_shm_offset_t next;	/* offset of next block in the free list or SMB_SHM_NOT_FREE_OFF when block in use  */
   int          size;   /* user size in BlockDescSize units */
};

#define	EOList_Addr	(struct SmbShmBlockDesc *)( 0 )
#define EOList_Off      (NULL_OFFSET)

#define	CellSize	sizeof(struct SmbShmBlockDesc)

/* HeaderSize aligned on 8 byte boundary */
#define	AlignedHeaderSize  	((sizeof(struct SmbShmHeader)+7) & ~7)

static int  smb_shm_fd = -1;
static pstring smb_shm_processreg_name = "";

static struct SmbShmHeader *smb_shm_header_p = (struct SmbShmHeader *)0;
static int smb_shm_times_locked = 0;

static BOOL smb_shm_initialize_called = False;

static BOOL smb_shm_global_lock(void)
{
   if (smb_shm_fd < 0)
   {
      DEBUG(0,("ERROR smb_shm_global_lock : bad smb_shm_fd (%d)\n",smb_shm_fd));
      return False;
   }
   
   smb_shm_times_locked++;
   
   if(smb_shm_times_locked > 1)
   {
      DEBUG(5,("smb_shm_global_lock : locked %d times\n",smb_shm_times_locked));
      return True;
   }
   
   /* Do an exclusive wait lock on the first byte of the file */
   if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_WRLCK) == False)
   {
      DEBUG(0,("ERROR smb_shm_global_lock : fcntl_lock failed with code %d\n",errno));
      smb_shm_times_locked--;
      return False;
   }
   
   return True;
   
}

static BOOL smb_shm_global_unlock(void)
{
   if (smb_shm_fd < 0)
   {
      DEBUG(0,("ERROR smb_shm_global_unlock : bad smb_shm_fd (%d)\n",smb_shm_fd));
      return False;
   }
   
   if(smb_shm_times_locked == 0)
   {
      DEBUG(0,("ERROR smb_shm_global_unlock : shmem not locked\n",smb_shm_fd));
      return False;
   }
   
   smb_shm_times_locked--;
   
   if(smb_shm_times_locked > 0)
   {
      DEBUG(5,("smb_shm_global_unlock : still locked %d times\n",smb_shm_times_locked));
      return True;
   }
   
   /* Do a wait unlock on the first byte of the file */
   if (fcntl_lock(smb_shm_fd, F_SETLKW, 0, 1, F_UNLCK) == False)
   {
      DEBUG(0,("ERROR smb_shm_global_unlock : fcntl_lock failed with code %d\n",errno));
      smb_shm_times_locked++;
      return False;
   }
   
   return True;
   
}

/* 
 * Function to create the hash table for the share mode entries. Called
 * when smb shared memory is global locked.
 */

BOOL smb_shm_create_hash_table( unsigned int size )
{
  size *= sizeof(smb_shm_offset_t);

  smb_shm_global_lock();
  smb_shm_header_p->userdef_off = smb_shm_alloc( size );

  if(smb_shm_header_p->userdef_off == NULL_OFFSET)
    {
      DEBUG(0,("smb_shm_create_hash_table: Failed to create hash table of size %d\n",size));
      smb_shm_global_unlock();
      return False;
    }

  /* Clear hash buckets. */
  memset( smb_shm_offset2addr(smb_shm_header_p->userdef_off), '\0', size);
  smb_shm_global_unlock();
  return True;
}

static BOOL smb_shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes)
{
   int smb_shm_processes_fd = -1;
   int nb_read;
   pid_t other_pid;
   int seek_back = -sizeof(other_pid);
   int free_slot = -1;
   int erased_slot;   
   
#ifndef SECURE_SHARE_MODES
   smb_shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666);
#else /* SECURE_SHARE_MODES */
   smb_shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0600);
#endif /* SECURE_SHARE_MODES */
   if ( smb_shm_processes_fd < 0 )
   {
      DEBUG(0,("ERROR smb_shm_register_process : processreg_file open failed with code %d\n",errno));
      return False;
   }
   
   *other_processes = False;
   
   while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
   {
      if(other_pid)
      {
	 if(process_exists(other_pid))
	    *other_processes = True;
	 else
	 {
	    /* erase old pid */
            DEBUG(5,("smb_shm_register_process : erasing stale record for pid %d (seek_back = %d)\n",
                      other_pid, seek_back));
	    other_pid = (pid_t)0;
	    erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
	    write(smb_shm_processes_fd, &other_pid, sizeof(other_pid));
	    if(free_slot < 0)
	       free_slot = erased_slot;
	 }
      }
      else 
	 if(free_slot < 0)
	    free_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
   }
   if (nb_read < 0)
   {
      DEBUG(0,("ERROR smb_shm_register_process : processreg_file read failed with code %d\n",errno));
      close(smb_shm_processes_fd);
      return False;
   }
   
   if(free_slot < 0)
      free_slot = lseek(smb_shm_processes_fd, 0, SEEK_END);

   DEBUG(5,("smb_shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot));
   lseek(smb_shm_processes_fd, free_slot, SEEK_SET);
   if(write(smb_shm_processes_fd, &pid, sizeof(pid)) < 0)
   {
      DEBUG(0,("ERROR smb_shm_register_process : processreg_file write failed with code %d\n",errno));
      close(smb_shm_processes_fd);
      return False;
   }

   close(smb_shm_processes_fd);

   return True;
}

static BOOL smb_shm_unregister_process(char *processreg_file, pid_t pid)
{
   int old_umask;
   int smb_shm_processes_fd = -1;
   int nb_read;
   pid_t other_pid;
   int seek_back = -sizeof(other_pid);
   int erased_slot;
   BOOL found = False;
   
   
   old_umask = umask(0);
   smb_shm_processes_fd = open(processreg_file, O_RDWR);
   umask(old_umask);
   if ( smb_shm_processes_fd < 0 )
   {
      DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file open failed with code %d\n",errno));
      return False;
   }
   
   while ((nb_read = read(smb_shm_processes_fd, &other_pid, sizeof(other_pid))) > 0)
   {
      DEBUG(5,("smb_shm_unregister_process : read record for pid %d\n",other_pid));
      if(other_pid == pid)
      {
	 /* erase pid */
         DEBUG(5,("smb_shm_unregister_process : erasing record for pid %d (seek_val = %d)\n",
                     other_pid, seek_back));
	 other_pid = (pid_t)0;
	 erased_slot = lseek(smb_shm_processes_fd, seek_back, SEEK_CUR);
	 if(write(smb_shm_processes_fd, &other_pid, sizeof(other_pid)) < 0)
	 {
	    DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file write failed with code %d\n",errno));
	    close(smb_shm_processes_fd);
	    return False;
	 }
	 
	 found = True;
	 break;
      }
   }
   if (nb_read < 0)
   {
      DEBUG(0,("ERROR smb_shm_unregister_process : processreg_file read failed with code %d\n",errno));
      close(smb_shm_processes_fd);
      return False;
   }
   
   if(!found)
   {
      DEBUG(0,("ERROR smb_shm_unregister_process : couldn't find pid %d in file %s\n",pid,processreg_file));
      close(smb_shm_processes_fd);
      return False;
   }
      
   
   close(smb_shm_processes_fd);

   return True;
}


static BOOL smb_shm_validate_header(int size)
{
   if( !smb_shm_header_p )
   {
      /* not mapped yet */
      DEBUG(0,("ERROR smb_shm_validate_header : shmem not mapped\n"));
      return False;
   }
   
   if(smb_shm_header_p->smb_shm_magic != SMB_SHM_MAGIC)
   {
      DEBUG(0,("ERROR smb_shm_validate_header : bad magic\n"));
      return False;
   }
   if(smb_shm_header_p->smb_shm_version != SMB_SHM_VERSION)
   {
      DEBUG(0,("ERROR smb_shm_validate_header : bad version %X\n",smb_shm_header_p->smb_shm_version));
      return False;
   }
   
   if(smb_shm_header_p->total_size != size)
   {
      DEBUG(0,("ERROR smb_shm_validate_header : shmem size mismatch (old = %d, new = %d)\n",smb_shm_header_p->total_size,size));
      return False;
   }

   if(!smb_shm_header_p->consistent)
   {
      DEBUG(0,("ERROR smb_shm_validate_header : shmem not consistent\n"));
      return False;
   }
   return True;
}

static BOOL smb_shm_initialize(int size)
{
   struct SmbShmBlockDesc * first_free_block_p;
   
   DEBUG(5,("smb_shm_initialize : initializing shmem file of size %d\n",size));
   
   if( !smb_shm_header_p )
   {
      /* not mapped yet */
      DEBUG(0,("ERROR smb_shm_initialize : shmem not mapped\n"));
      return False;
   }
   
   smb_shm_header_p->smb_shm_magic = SMB_SHM_MAGIC;
   smb_shm_header_p->smb_shm_version = SMB_SHM_VERSION;
   smb_shm_header_p->total_size = size;
   smb_shm_header_p->first_free_off = AlignedHeaderSize;
   smb_shm_header_p->userdef_off = NULL_OFFSET;
   
   first_free_block_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
   first_free_block_p->next = EOList_Off;
   first_free_block_p->size = ( size - AlignedHeaderSize - CellSize ) / CellSize ;
   
   smb_shm_header_p->statistics.cells_free = first_free_block_p->size;
   smb_shm_header_p->statistics.cells_used = 0;
   smb_shm_header_p->statistics.cells_system = 1;
   
   smb_shm_header_p->consistent = True;
   
   smb_shm_initialize_called = True;

   return True;
}
   
static void smb_shm_solve_neighbors(struct SmbShmBlockDesc *head_p )
{
   struct SmbShmBlockDesc *next_p;
   
   /* Check if head_p and head_p->next are neighbors and if so join them */
   if ( head_p == EOList_Addr ) return ;
   if ( head_p->next == EOList_Off ) return ;
   
   next_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(head_p->next);
   if ( ( head_p + head_p->size + 1 ) == next_p)
   {
      head_p->size += next_p->size +1 ;	/* adapt size */
      head_p->next = next_p->next	  ; /* link out */
      
      smb_shm_header_p->statistics.cells_free += 1;
      smb_shm_header_p->statistics.cells_system -= 1;
   }
}



BOOL smb_shm_open( char *file_name, int size)
{
   int filesize;
   BOOL created_new = False;
   BOOL other_processes = True;
   int old_umask;
   
   DEBUG(5,("smb_shm_open : using shmem file %s to be of size %d\n",file_name,size));

   old_umask = umask(0);
#ifndef SECURE_SHARE_MODES
   smb_shm_fd = open(file_name, O_RDWR | O_CREAT, 0666);
#else /* SECURE_SHARE_MODES */
   smb_shm_fd = open(file_name, O_RDWR | O_CREAT, 0600);
#endif /* SECURE_SHARE_MODE */
   umask(old_umask);
   if ( smb_shm_fd < 0 )
   {
      DEBUG(0,("ERROR smb_shm_open : open failed with code %d\n",errno));
      return False;
   }
   
   if (!smb_shm_global_lock())
   {
      DEBUG(0,("ERROR smb_shm_open : can't do smb_shm_global_lock\n"));
      return False;
   }
   
   if( (filesize = lseek(smb_shm_fd, 0, SEEK_END)) < 0)
   {
      DEBUG(0,("ERROR smb_shm_open : lseek failed with code %d\n",errno));
      smb_shm_global_unlock();
      close(smb_shm_fd);
      return False;
   }

   /* return the file offset to 0 to save on later seeks */
   lseek(smb_shm_fd,0,SEEK_SET);

   if (filesize == 0)
   {
      /* we just created a new one */
      created_new = True;
   }
   
   /* to find out if some other process is already mapping the file,
      we use a registration file containing the processids of the file mapping processes
      */

   /* construct processreg file name */
   strcpy(smb_shm_processreg_name, file_name);
   strcat(smb_shm_processreg_name, ".processes");

   if (! smb_shm_register_process(smb_shm_processreg_name, getpid(), &other_processes))
   {
      smb_shm_global_unlock();
      close(smb_shm_fd);
      return False;
   }

   if (created_new || !other_processes)
   {
      /* we just created a new one, or are the first opener, lets set it size */
      if( ftruncate(smb_shm_fd, size) <0)
      {
         DEBUG(0,("ERROR smb_shm_open : ftruncate failed with code %d\n",errno));
	 smb_shm_unregister_process(smb_shm_processreg_name, getpid());
	 smb_shm_global_unlock();
	 close(smb_shm_fd);
	 return False;
      }

      /* paranoia */
      lseek(smb_shm_fd,0,SEEK_SET);

      filesize = size;
   }
   
   if (size != filesize )
   {
      /* the existing file has a different size and we are not the first opener.
	 Since another process is still using it, we will use the file size */
      DEBUG(0,("WARNING smb_shm_open : filesize (%d) != expected size (%d), using filesize\n",filesize,size));
      size = filesize;
   }
   
   smb_shm_header_p = (struct SmbShmHeader *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, smb_shm_fd, 0);
   /* WARNING, smb_shm_header_p can be different for different processes mapping the same file ! */
   if (smb_shm_header_p  == (struct SmbShmHeader *)(-1))
   {
      DEBUG(0,("ERROR smb_shm_open : mmap failed with code %d\n",errno));
      smb_shm_unregister_process(smb_shm_processreg_name, getpid());
      smb_shm_global_unlock();
      close(smb_shm_fd);
      return False;
   }      
   
      
   if (created_new || !other_processes)
   {
      smb_shm_initialize(size);
      /* Create the hash buckets for the share file entries. */
      smb_shm_create_hash_table( lp_shmem_hash_size() );
   }
   else if (!smb_shm_validate_header(size) )
   {
      /* existing file is corrupt, samba admin should remove it by hand */
      DEBUG(0,("ERROR smb_shm_open : corrupt shared mem file, remove it manually\n"));
      munmap((caddr_t)smb_shm_header_p, size);
      smb_shm_unregister_process(smb_shm_processreg_name, getpid());
      smb_shm_global_unlock();
      close(smb_shm_fd);
      return False;
   }
   
   smb_shm_global_unlock();
   return True;
      
}


BOOL smb_shm_close( void )
{
   
   if(smb_shm_initialize_called == False)
     return True;

   DEBUG(5,("smb_shm_close\n"));
   if(smb_shm_times_locked > 0)
      DEBUG(0,("WARNING smb_shm_close : shmem was still locked %d times\n",smb_shm_times_locked));;
   if ((smb_shm_header_p != NULL) && 
              (munmap((caddr_t)smb_shm_header_p, smb_shm_header_p->total_size) < 0))
   {
      DEBUG(0,("ERROR smb_shm_close : munmap failed with code %d\n",errno));
   }

   smb_shm_global_lock();
   DEBUG(5,("calling smb_shm_unregister_process(%s, %d)\n", smb_shm_processreg_name, getpid()));
   smb_shm_unregister_process(smb_shm_processreg_name, getpid());
   smb_shm_global_unlock();
   
   close(smb_shm_fd);
   
   smb_shm_fd = -1;
   smb_shm_processreg_name[0] = '\0';

   smb_shm_header_p = (struct SmbShmHeader *)0;
   smb_shm_times_locked = 0;
   
   return True;
}

smb_shm_offset_t smb_shm_alloc(int size)
{
   unsigned num_cells ;
   struct SmbShmBlockDesc *scanner_p;
   struct SmbShmBlockDesc *prev_p;
   struct SmbShmBlockDesc *new_p;
   smb_shm_offset_t result_offset;
   
   
   if( !smb_shm_header_p )
   {
      /* not mapped yet */
      DEBUG(0,("ERROR smb_shm_alloc : shmem not mapped\n"));
      return NULL_OFFSET;
   }
   
   smb_shm_global_lock();

   if( !smb_shm_header_p->consistent)
   {
      DEBUG(0,("ERROR smb_shm_alloc : shmem not consistent\n"));
      smb_shm_global_unlock();
      return NULL_OFFSET;
   }
   
   
   /* calculate	the number of cells */
   num_cells = (size + CellSize -1) / CellSize;

   /* set start	of scan */
   prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
   scanner_p =	prev_p ;
   
   /* scan the free list to find a matching free space */
   while ( ( scanner_p != EOList_Addr ) && ( scanner_p->size < num_cells ) )
   {
      prev_p = scanner_p;
      scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
   }
   
   /* at this point scanner point to a block header or to the end of the list */
   if ( scanner_p == EOList_Addr )	
   {
      DEBUG(0,("ERROR smb_shm_alloc : alloc of %d bytes failed, no free space found\n",size));
      smb_shm_global_unlock();
      return (NULL_OFFSET);
   }
   
   /* going to modify shared mem */
   smb_shm_header_p->consistent = False;
   
   /* if we found a good one : scanner == the good one */
   if ( scanner_p->size <= num_cells + 2 )
   {
      /* there is no use in making a new one, it will be too small anyway 
      *	 we will link out scanner
      */
      if ( prev_p == scanner_p )
      {
	 smb_shm_header_p->first_free_off = scanner_p->next ;
      }
      else
      {
	 prev_p->next = scanner_p->next ;
      }
      smb_shm_header_p->statistics.cells_free -= scanner_p->size;
      smb_shm_header_p->statistics.cells_used += scanner_p->size;
   }
   else
   {
      /* Make a new one */
      new_p = scanner_p + 1 + num_cells;
      new_p->size = scanner_p->size - num_cells - 1;
      new_p->next = scanner_p->next;
      scanner_p->size = num_cells;
      scanner_p->next = smb_shm_addr2offset(new_p);
      
      if ( prev_p	!= scanner_p )
      {
	 prev_p->next	   = smb_shm_addr2offset(new_p)  ;
      }
      else
      {
	 smb_shm_header_p->first_free_off = smb_shm_addr2offset(new_p)  ;
      }
      smb_shm_header_p->statistics.cells_free -= num_cells+1;
      smb_shm_header_p->statistics.cells_used += num_cells;
      smb_shm_header_p->statistics.cells_system += 1;
   }

   result_offset = smb_shm_addr2offset( &(scanner_p[1]) );
   scanner_p->next =	SMB_SHM_NOT_FREE_OFF ;

   /* end modification of shared mem */
   smb_shm_header_p->consistent = True;

   DEBUG(6,("smb_shm_alloc : request for %d bytes, allocated %d bytes at offset %d\n",size,scanner_p->size*CellSize,result_offset ));

   smb_shm_global_unlock();
   return ( result_offset );
}   



BOOL smb_shm_free(smb_shm_offset_t offset)
{
   struct SmbShmBlockDesc *header_p  ; /*	pointer	to header of block to free */
   struct SmbShmBlockDesc *scanner_p ; /*	used to	scan the list			   */
   struct SmbShmBlockDesc *prev_p	   ; /*	holds previous in the list		   */
   
   if( !smb_shm_header_p )
   {
      /* not mapped yet */
      DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
      return False;
   }
   
   smb_shm_global_lock();

   if( !smb_shm_header_p->consistent)
   {
      DEBUG(0,("ERROR smb_shm_free : shmem not consistent\n"));
      smb_shm_global_unlock();
      return False;
   }
   
   header_p = (	(struct SmbShmBlockDesc *)smb_shm_offset2addr(offset) - 1); /* make pointer to header of block */

   if (header_p->next != SMB_SHM_NOT_FREE_OFF)
   {
      DEBUG(0,("ERROR smb_shm_free : bad offset (%d)\n",offset));
      smb_shm_global_unlock();
      return False;
   }
   
   /* find a place in the free_list to put the header in */
   
   /* set scanner and previous pointer to start of list */
   prev_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(smb_shm_header_p->first_free_off);
   scanner_p = prev_p ;
   
   while ( ( scanner_p != EOList_Addr) && (scanner_p < header_p) ) /* while we didn't scan past its position */
   {
      prev_p = scanner_p ;
      scanner_p = (struct SmbShmBlockDesc *)smb_shm_offset2addr(scanner_p->next);
   }
   
   smb_shm_header_p->consistent = False;
   
   DEBUG(6,("smb_shm_free : freeing %d bytes at offset %d\n",header_p->size*CellSize,offset));

   if ( scanner_p == prev_p )
   {
      smb_shm_header_p->statistics.cells_free += header_p->size;
      smb_shm_header_p->statistics.cells_used -= header_p->size;

      /* we must free it at the beginning of the list */
      smb_shm_header_p->first_free_off = smb_shm_addr2offset(header_p);						 /*	set	the free_list_pointer to this block_header */

      /* scanner is the one that was first in the list */
      header_p->next = smb_shm_addr2offset(scanner_p);
      smb_shm_solve_neighbors( header_p ); /* if neighbors then link them */
      
      smb_shm_header_p->consistent = True;
      smb_shm_global_unlock();
      return True;
   } 
   else
   {
      smb_shm_header_p->statistics.cells_free += header_p->size;
      smb_shm_header_p->statistics.cells_used -= header_p->size;

      prev_p->next = smb_shm_addr2offset(header_p);
      header_p->next = smb_shm_addr2offset(scanner_p);
      smb_shm_solve_neighbors(header_p) ;
      smb_shm_solve_neighbors(prev_p) ;

      smb_shm_header_p->consistent = True;
      smb_shm_global_unlock();
      return True;
   }
}

smb_shm_offset_t smb_shm_get_userdef_off(void)
{
   if (!smb_shm_header_p)
      return NULL_OFFSET;
   else
      return smb_shm_header_p->userdef_off;
}

BOOL smb_shm_set_userdef_off(smb_shm_offset_t userdef_off)
{
   if (!smb_shm_header_p)
      return False;
   else
      smb_shm_header_p->userdef_off = userdef_off;
   return True;
}

void * smb_shm_offset2addr(smb_shm_offset_t offset)
{
   if (offset == NULL_OFFSET )
      return (void *)(0);
   
   if (!smb_shm_header_p)
      return (void *)(0);
   
   return (void *)((char *)smb_shm_header_p + offset );
}

smb_shm_offset_t smb_shm_addr2offset(void *addr)
{
   if (!addr)
      return NULL_OFFSET;
   
   if (!smb_shm_header_p)
      return NULL_OFFSET;
   
   return (smb_shm_offset_t)((char *)addr - (char *)smb_shm_header_p);
}

/*******************************************************************
  Lock a particular hash bucket entry.
  ******************************************************************/

BOOL smb_shm_lock_hash_entry( unsigned int entry)
{
  int start = (smb_shm_header_p->userdef_off + (entry * sizeof(smb_shm_offset_t)));

  if (smb_shm_fd < 0)
    {
      DEBUG(0,("ERROR smb_shm_lock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
      return False;
    }

  if(entry >= lp_shmem_hash_size())
    {
      DEBUG(0,("ERROR smb_shm_lock_hash_entry : hash entry size too big (%d)\n", entry));
      return False;
    }
  
  /* Do an exclusive wait lock on the 4 byte region mapping into this entry  */
  if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(smb_shm_offset_t), F_WRLCK) == False)
    {
      DEBUG(0,("ERROR smb_shm_lock_hash_entry : fcntl_lock failed with code %d\n",errno));
      return False;
    }
  
  DEBUG(9,("smb_shm_lock_hash_entry: locked hash bucket %d\n", entry)); 
  return True;
}

/*******************************************************************
  Unlock a particular hash bucket entry.
  ******************************************************************/

BOOL smb_shm_unlock_hash_entry( unsigned int entry )
{
  int start = (smb_shm_header_p->userdef_off + (entry * sizeof(smb_shm_offset_t)));

  if (smb_shm_fd < 0)
    {
      DEBUG(0,("ERROR smb_shm_unlock_hash_entry : bad smb_shm_fd (%d)\n",smb_shm_fd));
      return False;
    }
   
  if(entry >= lp_shmem_hash_size())
    {
      DEBUG(0,("ERROR smb_shm_unlock_hash_entry : hash entry size too big (%d)\n", entry));
      return False;
    }

  /* Do a wait lock on the 4 byte region mapping into this entry  */
  if (fcntl_lock(smb_shm_fd, F_SETLKW, start, sizeof(smb_shm_offset_t), F_UNLCK) == False)
    {
      DEBUG(0,("ERROR smb_shm_unlock_hash_entry : fcntl_lock failed with code %d\n",errno));
      return False;
    }
  
  DEBUG(9,("smb_shm_unlock_hash_entry: unlocked hash bucket %d\n", entry)); 
  return True;
}

/*******************************************************************
  Gather statistics on shared memory usage.
  ******************************************************************/

BOOL smb_shm_get_usage(int *bytes_free,
		   int *bytes_used,
		   int *bytes_overhead)
{
   if( !smb_shm_header_p )
   {
      /* not mapped yet */
      DEBUG(0,("ERROR smb_shm_free : shmem not mapped\n"));
      return False;
   }
   *bytes_free = smb_shm_header_p->statistics.cells_free * CellSize;
   *bytes_used = smb_shm_header_p->statistics.cells_used * CellSize;
   *bytes_overhead = smb_shm_header_p->statistics.cells_system * CellSize + AlignedHeaderSize;
   
   return True;
}

#else /* FAST_SHARE_MODES */
 int shmem_dummy_procedure(void)
{return 0;}
#endif /* FAST_SHARE_MODES */