readme.locking

来自「Linux Kernel 2.6.9 for OMAP1710」· LOCKING 代码 · 共 117 行

LOCKING
117
字号
	$Id: README.Locking,v 1.4 2002/03/08 16:20:06 dwmw2 Exp $	JFFS2 LOCKING DOCUMENTATION	---------------------------At least theoretically, JFFS2 does not require the Big Kernel Lock(BKL), which was always helpfully obtained for it by Linux 2.4 VFScode. It has its own locking, as described below.This document attempts to describe the existing locking rules forJFFS2. It is not expected to remain perfectly up to date, but ought tobe fairly close.	alloc_sem	---------The alloc_sem is a per-filesystem semaphore, used primarily to ensurecontiguous allocation of space on the medium. It is automaticallyobtained during space allocations (jffs2_reserve_space()) and freedupon write completion (jffs2_complete_reservation()). Note thatthe garbage collector will obtain this right at the beginning ofjffs2_garbage_collect_pass() and release it at the end, therebypreventing any other write activity on the file system during agarbage collect pass.When writing new nodes, the alloc_sem must be held until the new nodeshave been properly linked into the data structures for the inode towhich they belong. This is for the benefit of NAND flash - adding newnodes to an inode may obsolete old ones, and by holding the alloc_semuntil this happens we ensure that any data in the write-buffer at thetime this happens are part of the new node, not just something thatwas written afterwards. Hence, we can ensure the newly-obsoleted nodesdon't actually get erased until the write-buffer has been flushed tothe medium.With the introduction of NAND flash support and the write-buffer, the alloc_sem is also used to protect the wbuf-related members of thejffs2_sb_info structure. Atomically reading the wbuf_len member to seeif the wbuf is currently holding any data is permitted, though.Ordering constraints: See f->sem.	File Semaphore f->sem	---------------------This is the JFFS2-internal equivalent of the inode semaphore i->i_sem.It protects the contents of the jffs2_inode_info private inode data,including the linked list of node fragments (but see the notes below onerase_completion_lock), etc.The reason that the i_sem itself isn't used for this purpose is toavoid deadlocks with garbage collection -- the VFS will lock the i_sembefore calling a function which may need to allocate space. Theallocation may trigger garbage-collection, which may need to move anode belonging to the inode which was locked in the first place by theVFS. If the garbage collection code were to attempt to lock the i_semof the inode from which it's garbage-collecting a physical node, thislead to deadlock, unless we played games with unlocking the i_sembefore calling the space allocation functions.Instead of playing such games, we just have an extra internalsemaphore, which is obtained by the garbage collection code and alsoby the normal file system code _after_ allocation of space.Ordering constraints: 	1. Never attempt to allocate space or lock alloc_sem with 	   any f->sem held.	2. Never attempt to lock two file semaphores in one thread.	   No ordering rules have been made for doing so.	erase_completion_lock spinlock	------------------------------This is used to serialise access to the eraseblock lists, to theper-eraseblock lists of physical jffs2_raw_node_ref structures, and(NB) the per-inode list of physical nodes. The latter is a specialcase - see below.As the MTD API permits erase-completion callback functions to becalled from bottom-half (timer) context, and these functions accessthe data structures protected by this lock, it must be locked withspin_lock_bh().Note that the per-inode list of physical nodes (f->nodes) is a specialcase. Any changes to _valid_ nodes (i.e. ->flash_offset & 1 == 0) inthe list are protected by the file semaphore f->sem. But the erasecode may remove _obsolete_ nodes from the list while holding only theerase_completion_lock. So you can walk the list only while holding theerase_completion_lock, and can drop the lock temporarily mid-walk aslong as the pointer you're holding is to a _valid_ node, not anobsolete one.The erase_completion_lock is also used to protect the c->gc_taskpointer when the garbage collection thread exits. The code to kill theGC thread locks it, sends the signal, then unlocks it - while the GCthread itself locks it, zeroes c->gc_task, then unlocks on the exit path.	node_free_sem	-------------This semaphore is only used by the erase code which frees obsoletenode references and the jffs2_garbage_collect_deletion_dirent()function. The latter function on NAND flash must read _obsolete_ nodesto determine whether the 'deletion dirent' under consideration can bediscarded or whether it is still required to show that an inode hasbeen unlinked. Because reading from the flash may sleep, theerase_completion_lock cannot be held, so an alternative, moreheavyweight lock was required to prevent the erase code from freeingthe jffs2_raw_node_ref structures in question while the garbagecollection code is looking at them.Suggestions for alternative solutions to this problem would be welcomed.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?