[Samba] Samba client file locking

Matthias Reif matthreif at gmail.com
Mon May 22 06:13:18 GMT 2006


I am trying to work out the file locking mechanism on our Samba server when
connecting from Linux clients. I am getting some strange behaviour in a
mixed Linux/Windows environment when multiple clients open/lock the same
file for writing. Everything seems fine when a Windows client creates the
lock, but if a Linux client creates the lock, we get either two or more
Linux clients being able to open the same file for writing (SMBFS) or the
other extreme, if one Linux client opens the file none of the others can't
even open it in read-only mode (CIFS). The main application used on the
network is OpenOffice2.

I have been doing some testing using the cifs and smbfs kernel modules on
one FC4 and one Debian client (both 2.6 kernels), plus a Windows 2000

I used a simple test program that opens a file on a mounted Samba share and
tries to set a write lock.

I noticed that depending on the client I use Samba creates a different kind
of lock with different behaviour when multiple clients attempt to open the
same file. Neither smbfs nor cifs create the correct lock share mode
(DENY_WRITE) and the access mode is different (smbfs:0x3, cifs:0x12019f as
opposed to Windows: 0x2019f)

Is there any way we can manipulate the share mode and access mode that is
being created with the lock, e.g. with some mount options?

Thanks for any help.

mount -t cifs -o username=user1,rw,dir_mode=0777,file_mode=0777
//server/corp /network/corp
mount -t cifs -o username=user2,rw,dir_mode=0777,file_mode=0777
//server/corp /network/corp
21757  DENY_NONE  0x12019f    RDWR       NONE
/opt/samba/corporate/flt.ods   Mon May 22 13:43:50 2006
21757  DENY_NONE  0x12019f    RDWR       NONE
/opt/samba/corporate/flt.ods   Mon May 22 13:43:44 2006
Behaviour: First client opens file read/write, second client cannot set
write lock (ok).

mount -t smbfs -o username=user1,rw,dmask=777,fmask=777 //server/corp
mount -t smbfs -o username=user1,rw,dmask=777,fmask=777 //server/corp
22587  DENY_NONE  0x3         RDWR       NONE
/opt/samba/corporate/flt.ods   Mon May 22 14:16:40 2006
23034  DENY_NONE  0x3         RDWR       NONE
/opt/samba/corporate/flt.ods   Mon May 22 14:16:11 2006

- Open OpenOffice file
5106  DENY_WRITE 0x2019f     RDWR       EXCLUSIVE+BATCH
/opt/samba/corporate/flt.ods   Mon May 22 16:07:25 2006

Simple Test Program:

int main()
        struct flock lock;

        char input[80];

        // open read-only mode first
        int fd = open("/network/corp/flt.ods", O_RDWR );

        if ( fd != -1 )
                printf("File opened.\n");

                // try to lock the entire file
                lock.l_type = F_WRLCK;
                lock.l_whence = SEEK_SET;
                lock.l_start = 0;
                lock.l_len = 0;

                // request a write lock
                int fnctlres = fcntl(fd, F_SETLK, &lock);

                // check if we got granted the lock
                if ( fnctlres == -1)
                        printf("Could not set write lock.\n");
                        printf("File successfully locked for writing.\n");

                printf("Press a key.\n");

                scanf("%s", input);

                fcntl(fd, F_SETLK, &lock);

Extract from smb.conf


   posix locking = yes
   strict locking = yes
   share modes = yes
   debug level = 7
   unix extensions = yes
   ea support = yes


#============================ Share Definitions


   comment = Corporate Files
   path = /opt/samba/corporate
   public = no
   guest ok = no
   writable = yes
   browseable = yes
   share modes = yes
   create mask = 0777


More information about the samba mailing list