


_O_n_l_i_n_e _M_a_n_u_a_l	     Devices and Facilities		  lock(4)



NNNNAAAAMMMMEEEE
     lock - hierarchical lock management

DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
     The TX lock manager supports two-level hierarchical locking:
     at	the file level and at the block	level.	Each file has a
     set of blocks as its immediate descendants.  Each block has
     a unique file as parent.  The lock	manager	can be invoked
     indirectly	by the _o_p_e_n and	_f_l_o_c_k system calls; see	the sys-
     tem calls in this manual.

     TTTTyyyyppppeeeessss ooooffff AAAAcccccccceeeessssssss
     The interfaces described below are	specific to the	TX
     operating system; there is	some level of compatibility with
     4.2BSD and	with UNIX System V.

     Access to a resource (a file or a single block of a file)
     may be either _e_x_c_l_u_s_i_v_e (X) or _s_h_a_r_e_d (S).	 Exclusive access
     prohibits _a_l_l other access	to the resource; shared	access
     permits other shared access to the	resource and prohibits
     anyone from obtaining exclusive access to the resource.
     Exclusive access is generally used	for writing, and shared
     access for	reading.

     User programs may request either shared or	exclusive access
     to	an entire file using the _f_l_o_c_k system call or with the
     O_SHLOCK or O_EXLOCK options to the _o_p_e_n system call.  The
     4.2BSD release of UNIX uses the arguments LOCK_SH and
     LOCK_EX to	_f_l_o_c_k to request shared	and exclusive file lock-
     ing, respectively.	 Programs which	do not use the _f_l_o_c_k call
     automatically are granted _i_n_t_e_n_t locks to the file	when the
     _o_p_e_n system call is issued:  an _i_n_t_e_n_t _s_h_a_r_e_d (IS)	lock is
     obtained when read-only access is required, and an	_i_n_t_e_n_t
     _e_x_c_l_u_s_i_v_e (IX) lock is obtained when any form of write
     access is required.

     _U_p_g_r_a_d_e_s of file locks are	possible:  a program may open the
     file initially and	obtain either type of intent lock, and
     then issue	the _f_l_o_c_k system call to increase the level of
     the file lock.  For example, if a user program which already
     had a file	open invoked _f_l_o_c_k to obtain _s_h_a_r_e_d access to the
     file, thus	disallowing any	other processes	from updating the
     file, the lock upgrade would occur	as follows: an _i_n_t_e_n_t
     _s_h_a_r_e_d (IS) lock (read-only access) would be upgraded to a
     _s_h_a_r_e_d (S)	lock, and an _i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (IX) lock	(read-
     write access) would be upgraded to	the special lock type
     _s_h_a_r_e_d-_a_n_d-_i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (SIX).

     Whenever a	program	requests a lock	or an upgrade, one of
     four results occurs:

     1.	   The resource	is available, and the upgrade is per-
	   formed immediately.	_f_l_o_c_k returns immediately to the
	   user; the system call (_o_p_e_n,	_r_e_a_d, or _w_r_i_t_e)	completes
	   and returns to the user.

     2.	   The resource	is in use by one or more other processes
	   (see	LLLLoooocccckkkk CCCCoooommmmppppaaaattttiiiibbbbiiiilllliiiittttyyyy below).  If the user	requested
	   _n_o_n-_b_l_o_c_k_i_n_g	action,	the process receives an	immediate






	   error return	(EAGAIN).

     3.	   The resource	is in use by one or more other processes
	   and the user	did _n_o_t	request	non-blocking action.  The
	   lock-management system determines whether the process
	   can be safely blocked.  If so, the process is blocked
	   until the resource is available.

     4.	   The resource	is in use by one or more other processes,
	   the caller did not request non-blocking action, and
	   the system detects that the _d_e_a_d_l_o_c_k	condition would
	   result from blocking	the process.  If the system call
	   causing the deadlock	specifies IO_NOSIGABORT, the
	   error EDEADLOCK is returned to the caller.  If
	   IO_NOSIGABORT is not	specified, the SIGABORT	signal is
	   delivered to	the process with the _c_o_d_e value
	   TR_DEADLOCK.	 If no signal handler is present, the
	   default action is to	kill the process.

     RRRReeeelllleeeeaaaassssiiiinnnngggg aaaa LLLLoooocccckkkk
     Regarding releasing locks,	processes which	are in transac-
     tion state	behave differently from	processes that are not.

     Non-transaction processes may acquire file-level locks.
     Such locks	are released either when the file is _c_l_o_s_ed or
     when the _f_l_o_c_k system call	is invoked with	the unlock
     (LOCK_UN) operation.  Each	file-level lock	is associated
     with a particular file (actually the _i-_n_o_d_e) and a	process.
     (A	file is	not usually _c_l_o_s_ed unless this is the last refer-
     ence by the calling process.)

     Non-transaction processes can also	acquire	block-level locks
     with the _m_b_l_o_c_k system call.  This	call is	allowed	only on
     un-controlled (non-transaction-oriented) files.

     Programs that operate in transaction state	may acquire both
     file-level	and block-level	locks.	File-level locks acquired
     either _b_e_f_o_r_e the beginning of a transaction (or inside a
     transaction) are retained to the end of the transaction even
     if	the program releases them during the transaction.  For
     example, if a program _o_p_e_ns a file	for exclusive access,
     begins a transaction via _t_r_a_n_s (TR_BEGIN, 0), and then,
     while in the transaction, _c_l_o_s_es the file,	the lock will not
     be	released until the transaction commits or aborts.

     BBBBlllloooocccckkkk----LLLLeeeevvvveeeellll LLLLoooocccckkkkiiiinnnngggg
     When a program is using a file that was opened in _c_o_n_t_r_o_l_l_e_d
     (transaction) mode	and in one of the intent-locking modes,
     individual	block-level locks are automatically set	for each
     data page.

     Block-level locks may be set on un-controlled files using
     the _m_b_l_o_c_k	system call.

     Only two forms of block-level lock	exist: a _s_h_a_r_e_d	(S) lock
     on	the page is obtained when a program _r_e_a_ds a page, and an
     _e_x_c_l_u_s_i_v_e (X) lock	on the page is obtained	when a program
     _w_r_i_t_es a page.  An	_e_x_c_l_u_s_i_v_e (X) lock may also be obtained
     by	doing a	``read with lock'' (IO_WINTENT)	operation.  The
     _e_x_c_l_u_s_i_v_e (X) lock, when granted, may actually be an upgrade






     of	an already-existing shared lock.

     MMMMuuuullllttttiiiipppprrrroooocccceeeessssssss TTTTrrrraaaannnnssssaaaaccccttttiiiioooonnnn LLLLoooocccckkkkiiiinnnngggg
     Where multiple processes are involved in the same transac-
     tion via an MXIPC channel,	file and block-level locks are
     assigned on a per transaction basis, rather than per pro-
     cess.  These locks	restrict access	by processes not involved
     in	this transaction, but do not restrict processes	within
     the transaction.  The server process is therefore not locked
     out of a file because the client process has a lock.  To
     maintain data integrity, however, the client process is not
     allowed access to controlled files	while a	server process
     has joined	the transaction.  The client can access	the file
     again when	the server has completed its part of the transac-
     tion.

     LLLLoooocccckkkk MMMMooooddddeeeessss

     The following lock	modes are used within the TX operating
     system:

     #define   LOCK_X		   0xc0	/* exclusive access */
     #define   LOCK_H		   0xc1	/* hold	access -- block	level only */
     #define   LOCK_S		   0xc2	/* share access	*/
     #define   LOCK_IX		   0xc3	/* intention exclusive access */
     #define   LOCK_IS		   0xc4	/* intention share access */
     #define   LOCK_SIX	      0xc5 /* share & intention	exclusive access */
     #define   LOCK_NULL      0xc6 /* no request to access */

     _i_n_t_e_n_t _s_h_a_r_e_d (IS)	and _i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (IX) modes	are
     allowed at	the file level only.  The lock modes have the
     following characteristics:

     _i_n_t_e_n_t _s_h_a_r_e_d (IS)
	      Provides the weakest non-null form of access.  IS
	      implies less access privilege than _i_n_t_e_n_t	_e_x_c_l_u_s_i_v_e
	      (IX) or _s_h_a_r_e_d (S) modes and allows shared (S)
	      locks to be set at the block level for reading.

     _i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (IX)
	      Allows _s_h_a_r_e_d (S)	and _e_x_c_l_u_s_i_v_e (X) locks	to be set
	      at the block level for reading and writing, respec-
	      tively.

     _s_h_a_r_e_d (S)
	      If set at	the file level,	allows read-only access
	      to all descendant	blocks without further locking.

     _h_o_l_d (H) Effective	only at	the block level, is like a _s_h_a_r_e_d
	      (S) lock except incompatible with	itself.	 But will
	      permit another process to	hold a _s_h_a_r_e_d (s) lock on
	      the same block.  This option can be used to reduce
	      dealocking but also reduces concurrency.

     _s_h_a_r_e_d-_a_n_d-_i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (SIX)
	      Combines the privileges of _s_h_a_r_e_d	(S) and	of _i_n_t_e_n_t
	      _e_x_c_l_u_s_i_v_e	(IX) mode.

     _e_x_c_l_u_s_i_v_e (X)
	      Provides the most	restricted access.  If set at the






	      file level, allows reads and writes to all descen-
	      dant blocks with no additional locking.

	  PPPPaaaarrrrttttiiiiaaaallll OOOOrrrrddddeeeerrrriiiinnnngggg ooooffff LLLLoooocccckkkk MMMMooooddddeeeessss bbbbyyyy PPPPrrrriiiivvvviiiilllleeeeggggeeeessss

			 _m_o_s_t _p_r_i_v_i_l_e_g_e_d

				  X
				  |
				 SIX
				  |
			      +-------+
		      S			      IX
			      +-------+
				  |
				  IS
				  |
				 NULL

			_l_e_a_s_t _p_r_i_v_i_l_e_g_e_d

     The lock modes can	be ranked in a partial order by
     privilege,	as shown above.	 Note that it is not a complete
     ordering since IX and S are incomparable.

     FFFFiiiilllleeee----LLLLeeeevvvveeeellll	LLLLoooocccckkkkiiiinnnngggg
     At	the file level,	locking	is of two types: automatic lock-
     ing and manual locking.  Automatic	locks (IS and IX) are set
     at	file _o_p_e_n time.	 An _i_n_t_e_n_t _s_h_a_r_e_d (IS) lock is set for a
     file opened with the O_RDONLY (read only) flag.  An _i_n_t_e_n_t
     _e_x_c_l_u_s_i_v_e (IX) lock is set	for a file opened with O_WRONLY
     (write only), O_RDWR (read/write),	O_APPEND (append),
     O_CREAT (create) or O_TRUNC (truncate to zero) flags.
     Manual locks (S and X) may	be set at file _o_p_e_n time using
     the O_SHLOCK and O_EXLOCK options;	they may also be set
     after the file is opened via the _f_l_o_c_k system call.  A
     _s_h_a_r_e_d (S)	lock is	set using the LOCK_SH operation; LOCK_EX
     sets an _e_x_c_l_u_s_i_v_e (X) lock.

     LLLLoooocccckkkk CCCCoooommmmppppaaaattttiiiibbbbiiiilllliiiittttyyyy
     Two locks are _c_o_m_p_a_t_i_b_l_e if they are allowed simultaneously
     on	a given	resource.  If a	lock of	type _x exists, and a user
     requests a	lock of	type _y,	the system checks the compatibil-
     ity of _x and _y.  If they are compatible, the lock is
     granted; otherwise, the requestor is blocked.  (The user
     process may or may	not be blocked depending on whether non-
     blocking I/O was specified	and whether a potential	deadlock
     is	detected.)

     The following tables define the compatibility checking per-
     formed by TX:

     FFFFiiiilllleeee LLLLeeeevvvveeeellll	CCCCoooommmmppppaaaattttiiiibbbbiiiilllliiiittttyyyy....	This table describes the compati-
     bility of the request modes at file level,	where NULL
     represents	the absence of a request.  This	table is sym-
     metric:  one side is the current file-lock	state and the
     other side	is the requested new state.  Note that _i_n_t_e_n_t
     _s_h_a_r_e_d (IS) and _i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (IX) locks are compatible;








     but _s_h_a_r_e_d	(S) and	_i_n_t_e_n_t _e_x_c_l_u_s_i_v_e (IX) locks are	not.

		 MMMMooooddddeeee	NULL   IS   IX	 S   SIX   X
		 ___________________________________
		 NULL	 x     x    x	 x    x	   x
		  IS	 x     x    x	 x    x
		  IX	 x     x    x
		  S	 x     x	 x
		 SIX	 x     x
		  X	 x

		     |
		     |
		     |
		     |
		     |
		     |
		     |
		     |








     BBBBlllloooocccckkkk LLLLeeeevvvveeeellll CCCCoooommmmppppaaaattttiiiibbbbiiiilllliiiittttyyyy....	 This table describes the compa-
     tibility of the request modes at block level.  This table is
     symmetric:	 one side is the current block-lock state and the
     other side	is the requested new state.

		       MMMMooooddddeeee   NULL   S	 H   X
		       _______________________
		       NULL    x     x	 x   x
			S      x     x	 x
			H      x     x
			X      x

			   |
			   |
			   |
			   |
			   |
			   |






     This table	defines	the resulting (new) lock mode of a file
     given a requested lock mode and the current combined lock
     mode.  This table is symmetric.

			 LLLLoooocccckkkk MMMMooooddddeeee CCCCoooonnnnvvvveeeerrrrssssiiiioooonnnn
		   MMMMooooddddeeee	  IS	IX     S    SIX	  X
		   ________________________________
		    IS	  IS	IX     S    SIX	  X
		    IX	  IX	IX    SIX   SIX	  X
		    S	   S	SIX    S    SIX	  X
		   SIX	  SIX	SIX   SIX   SIX	  X
		    X	   X	 X     X     X	  X

		       |
		       |
		       |
		       |
		       |
		       |
		       |







     NNNNoooonnnn----TTTTrrrraaaannnnssssaaaaccccttttiiiioooonnnn LLLLoooocccckkkkiiiinnnngggg
     Using locks outside of transactions requires that processes
     co-operate.  For example, if 2 processes open the same file
     with the O_RDWR flag (which sets only the IX lock)	then, the
     processes must ensure data	integrity through co-operation,
     if	both processes write to	the file.  TX cannot guarantee
     data integrity in such a case.)

SSSSEEEEEEEE AAAALLLLSSSSOOOO
     System Calls (2):	close, flock, mblock, open, trans

December 1990				TX Programmer's	Reference












