xref: /OK3568_Linux_fs/kernel/fs/reiserfs/lock.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include "reiserfs.h"
3*4882a593Smuzhiyun #include <linux/mutex.h>
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * The previous reiserfs locking scheme was heavily based on
7*4882a593Smuzhiyun  * the tricky properties of the Bkl:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * - it was acquired recursively by a same task
10*4882a593Smuzhiyun  * - the performances relied on the release-while-schedule() property
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Now that we replace it by a mutex, we still want to keep the same
13*4882a593Smuzhiyun  * recursive property to avoid big changes in the code structure.
14*4882a593Smuzhiyun  * We use our own lock_owner here because the owner field on a mutex
15*4882a593Smuzhiyun  * is only available in SMP or mutex debugging, also we only need this field
16*4882a593Smuzhiyun  * for this mutex, no need for a system wide mutex facility.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Also this lock is often released before a call that could block because
19*4882a593Smuzhiyun  * reiserfs performances were partially based on the release while schedule()
20*4882a593Smuzhiyun  * property of the Bkl.
21*4882a593Smuzhiyun  */
reiserfs_write_lock(struct super_block * s)22*4882a593Smuzhiyun void reiserfs_write_lock(struct super_block *s)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(s);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	if (sb_i->lock_owner != current) {
27*4882a593Smuzhiyun 		mutex_lock(&sb_i->lock);
28*4882a593Smuzhiyun 		sb_i->lock_owner = current;
29*4882a593Smuzhiyun 	}
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	/* No need to protect it, only the current task touches it */
32*4882a593Smuzhiyun 	sb_i->lock_depth++;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
reiserfs_write_unlock(struct super_block * s)35*4882a593Smuzhiyun void reiserfs_write_unlock(struct super_block *s)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(s);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/*
40*4882a593Smuzhiyun 	 * Are we unlocking without even holding the lock?
41*4882a593Smuzhiyun 	 * Such a situation must raise a BUG() if we don't want
42*4882a593Smuzhiyun 	 * to corrupt the data.
43*4882a593Smuzhiyun 	 */
44*4882a593Smuzhiyun 	BUG_ON(sb_i->lock_owner != current);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (--sb_i->lock_depth == -1) {
47*4882a593Smuzhiyun 		sb_i->lock_owner = NULL;
48*4882a593Smuzhiyun 		mutex_unlock(&sb_i->lock);
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
reiserfs_write_unlock_nested(struct super_block * s)52*4882a593Smuzhiyun int __must_check reiserfs_write_unlock_nested(struct super_block *s)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(s);
55*4882a593Smuzhiyun 	int depth;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* this can happen when the lock isn't always held */
58*4882a593Smuzhiyun 	if (sb_i->lock_owner != current)
59*4882a593Smuzhiyun 		return -1;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	depth = sb_i->lock_depth;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	sb_i->lock_depth = -1;
64*4882a593Smuzhiyun 	sb_i->lock_owner = NULL;
65*4882a593Smuzhiyun 	mutex_unlock(&sb_i->lock);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	return depth;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
reiserfs_write_lock_nested(struct super_block * s,int depth)70*4882a593Smuzhiyun void reiserfs_write_lock_nested(struct super_block *s, int depth)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(s);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/* this can happen when the lock isn't always held */
75*4882a593Smuzhiyun 	if (depth == -1)
76*4882a593Smuzhiyun 		return;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	mutex_lock(&sb_i->lock);
79*4882a593Smuzhiyun 	sb_i->lock_owner = current;
80*4882a593Smuzhiyun 	sb_i->lock_depth = depth;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun  * Utility function to force a BUG if it is called without the superblock
85*4882a593Smuzhiyun  * write lock held.  caller is the string printed just before calling BUG()
86*4882a593Smuzhiyun  */
reiserfs_check_lock_depth(struct super_block * sb,char * caller)87*4882a593Smuzhiyun void reiserfs_check_lock_depth(struct super_block *sb, char *caller)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(sb);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	WARN_ON(sb_i->lock_depth < 0);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #ifdef CONFIG_REISERFS_CHECK
reiserfs_lock_check_recursive(struct super_block * sb)95*4882a593Smuzhiyun void reiserfs_lock_check_recursive(struct super_block *sb)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct reiserfs_sb_info *sb_i = REISERFS_SB(sb);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	WARN_ONCE((sb_i->lock_depth > 0), "Unwanted recursive reiserfs lock!\n");
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun #endif
102