flock: Add BSD flock functionality to FUZIX
authorAlan Cox <alan@linux.intel.com>
Sat, 3 Jan 2015 15:14:05 +0000 (15:14 +0000)
committerAlan Cox <alan@linux.intel.com>
Sat, 3 Jan 2015 15:14:05 +0000 (15:14 +0000)
commitdc20145ae7442f9473c3bac4871a54cc447ee84f
treebbd6af8e57ee119f2ce34b5c278663514f2c41c5
parentf9fb2b1e73d9a76857d36328cd1acc0c00c34b8c
flock: Add BSD flock functionality to FUZIX

System 5 has its own rather more convoluted locking so for once we'll favour
BSD over System 5.

This is an initial patch for testing.

Theory:

We recover spare bits from the inode c_busy, which is now a set of flags not
a bool with 7 wasted bits. Z80 has cheap bit ops, and the dirty bit is the top
bit which is cheap on most other processors too.

The file can be in one of 16 lock states kept in four of the recovered bits
(CFLOCK bits)

     0 - unlocked
     1 - 14 that many shared locks (15+ gives you an ENOLCKS error)
     15 - exclusive

We use one of the spare O_ bits in each file object to keep a single bit
telling us this handle has a lock. The BSD API only allows one lock per file
table entry so that one bit is sufficient because we know that

       - if our lock bit is set and the file handle is exclusive lock
         then we must own the lock (as its exclusive) so we drop to 0 locks
       - if our lock bit is set and the handle is not exclusive locked
         then we can't own an exclusive lock so we must therefore be one of
         the shared locks

Total cost about 450 bytes of kernel memory and no extra data.
Kernel/devio.c
Kernel/filesys.c
Kernel/include/kernel.h
Kernel/include/syscall_name.h
Kernel/syscall_fs2.c