sbinfo.c 7.24 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
// SPDX-License-Identifier: GPL-2.0
/*
3
 * Copyright (C) 2005-2020 Junjiro R. Okajima
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
J. R. Okajima's avatar
J. R. Okajima committed
17
18
19
20
21
22
 */

/*
 * superblock private data
 */

J. R. Okajima's avatar
J. R. Okajima committed
23
#include <linux/iversion.h>
J. R. Okajima's avatar
J. R. Okajima committed
24
25
26
27
28
29
30
#include "aufs.h"

/*
 * they are necessary regardless sysfs is disabled.
 */
void au_si_free(struct kobject *kobj)
{
31
	int i;
J. R. Okajima's avatar
J. R. Okajima committed
32
	struct au_sbinfo *sbinfo;
33
	char *locked __maybe_unused; /* debug only */
J. R. Okajima's avatar
J. R. Okajima committed
34
35

	sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
36
37
	for (i = 0; i < AuPlink_NHASH; i++)
		AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
J. R. Okajima's avatar
J. R. Okajima committed
38
	AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
39

40
41
	AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
	au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
42
43
	AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
	au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
44

J. R. Okajima's avatar
J. R. Okajima committed
45
	dbgaufs_si_fin(sbinfo);
46
47
48
49
	au_rw_write_lock(&sbinfo->si_rwsem);
	au_br_free(sbinfo);
	au_rw_write_unlock(&sbinfo->si_rwsem);

J. R. Okajima's avatar
J. R. Okajima committed
50
	au_kfree_try_rcu(sbinfo->si_branch);
J. R. Okajima's avatar
J. R. Okajima committed
51
	mutex_destroy(&sbinfo->si_xib_mtx);
J. R. Okajima's avatar
J. R. Okajima committed
52
53
	AuRwDestroy(&sbinfo->si_rwsem);

54
	au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
55
	/* si_nfiles is waited too */
J. R. Okajima's avatar
J. R. Okajima committed
56
57
58
59
60
	au_kfree_rcu(sbinfo);
}

int au_si_alloc(struct super_block *sb)
{
61
	int err, i;
J. R. Okajima's avatar
J. R. Okajima committed
62
63
64
65
66
67
68
69
70
71
72
73
	struct au_sbinfo *sbinfo;

	err = -ENOMEM;
	sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
	if (unlikely(!sbinfo))
		goto out;

	/* will be reallocated separately */
	sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
	if (unlikely(!sbinfo->si_branch))
		goto out_sbinfo;

J. R. Okajima's avatar
J. R. Okajima committed
74
	err = sysaufs_si_init(sbinfo);
J. R. Okajima's avatar
J. R. Okajima committed
75
76
77
78
79
80
	if (!err) {
		dbgaufs_si_null(sbinfo);
		err = dbgaufs_si_init(sbinfo);
		if (unlikely(err))
			kobject_put(&sbinfo->si_kobj);
	}
J. R. Okajima's avatar
J. R. Okajima committed
81
82
83
	if (unlikely(err))
		goto out_br;

J. R. Okajima's avatar
J. R. Okajima committed
84
	au_nwt_init(&sbinfo->si_nowait);
J. R. Okajima's avatar
J. R. Okajima committed
85
86
	au_rw_init_wlock(&sbinfo->si_rwsem);

87
	au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
88
	au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
89

J. R. Okajima's avatar
J. R. Okajima committed
90
	sbinfo->si_bbot = -1;
91
	sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
J. R. Okajima's avatar
J. R. Okajima committed
92

93
94
95
96
97
	sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
	sbinfo->si_wbr_create = AuWbrCreate_Def;
	sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
	sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;

98
99
	au_fhsm_init(sbinfo);

100
	sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
J. R. Okajima's avatar
J. R. Okajima committed
101

J. R. Okajima's avatar
J. R. Okajima committed
102
103
104
	sbinfo->si_xino_jiffy = jiffies;
	sbinfo->si_xino_expire
		= msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
J. R. Okajima's avatar
J. R. Okajima committed
105
106
107
	mutex_init(&sbinfo->si_xib_mtx);
	/* leave si_xib_last_pindex and si_xib_next_bit */

108
109
	INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);

110
111
112
	sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
	sbinfo->si_rdblk = AUFS_RDBLK_DEF;
	sbinfo->si_rdhash = AUFS_RDHASH_DEF;
J. R. Okajima's avatar
J. R. Okajima committed
113
	sbinfo->si_dirwh = AUFS_DIRWH_DEF;
114

115
116
117
118
119
	for (i = 0; i < AuPlink_NHASH; i++)
		INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
	init_waitqueue_head(&sbinfo->si_plink_wq);
	spin_lock_init(&sbinfo->si_plink_maint_lock);

120
121
	INIT_HLIST_BL_HEAD(&sbinfo->si_files);

122
123
124
	/* with getattr by default */
	sbinfo->si_iop_array = aufs_iop;

J. R. Okajima's avatar
J. R. Okajima committed
125
	/* leave other members for sysaufs and si_mnt. */
J. R. Okajima's avatar
J. R. Okajima committed
126
	sbinfo->si_sb = sb;
J. R. Okajima's avatar
J. R. Okajima committed
127
	sb->s_fs_info = sbinfo;
128
	si_pid_set(sb);
J. R. Okajima's avatar
J. R. Okajima committed
129
130
	return 0; /* success */

J. R. Okajima's avatar
J. R. Okajima committed
131
out_br:
J. R. Okajima's avatar
J. R. Okajima committed
132
133
134
135
136
137
	au_kfree_try_rcu(sbinfo->si_branch);
out_sbinfo:
	au_kfree_rcu(sbinfo);
out:
	return err;
}
J. R. Okajima's avatar
J. R. Okajima committed
138

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
{
	int err, sz;
	struct au_branch **brp;

	AuRwMustWriteLock(&sbinfo->si_rwsem);

	err = -ENOMEM;
	sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
	if (unlikely(!sz))
		sz = sizeof(*brp);
	brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
			   may_shrink);
	if (brp) {
		sbinfo->si_branch = brp;
		err = 0;
	}

	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* ---------------------------------------------------------------------- */

unsigned int au_sigen_inc(struct super_block *sb)
{
	unsigned int gen;
	struct inode *inode;

	SiMustWriteLock(sb);

	gen = ++au_sbi(sb)->si_generation;
	au_update_digen(sb->s_root);
	inode = d_inode(sb->s_root);
	au_update_iigen(inode, /*half*/0);
	inode_inc_iversion(inode);
	return gen;
}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

aufs_bindex_t au_new_br_id(struct super_block *sb)
{
	aufs_bindex_t br_id;
	int i;
	struct au_sbinfo *sbinfo;

	SiMustWriteLock(sb);

	sbinfo = au_sbi(sb);
	for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
		br_id = ++sbinfo->si_last_br_id;
		AuDebugOn(br_id < 0);
		if (br_id && au_br_index(sb, br_id) < 0)
			return br_id;
	}

	return -1;
}
J. R. Okajima's avatar
J. R. Okajima committed
195
196
197
198
199
200

/* ---------------------------------------------------------------------- */

/* it is ok that new 'nwt' tasks are appended while we are sleeping */
int si_read_lock(struct super_block *sb, int flags)
{
201
202
203
	int err;

	err = 0;
J. R. Okajima's avatar
J. R. Okajima committed
204
205
206
207
	if (au_ftest_lock(flags, FLUSH))
		au_nwt_flush(&au_sbi(sb)->si_nowait);

	si_noflush_read_lock(sb);
208
209
210
	err = au_plink_maint(sb, flags);
	if (unlikely(err))
		si_read_unlock(sb);
J. R. Okajima's avatar
J. R. Okajima committed
211

212
	return err;
J. R. Okajima's avatar
J. R. Okajima committed
213
214
215
216
}

int si_write_lock(struct super_block *sb, int flags)
{
217
218
	int err;

J. R. Okajima's avatar
J. R. Okajima committed
219
220
221
222
	if (au_ftest_lock(flags, FLUSH))
		au_nwt_flush(&au_sbi(sb)->si_nowait);

	si_noflush_write_lock(sb);
223
224
225
	err = au_plink_maint(sb, flags);
	if (unlikely(err))
		si_write_unlock(sb);
J. R. Okajima's avatar
J. R. Okajima committed
226

227
	return err;
J. R. Okajima's avatar
J. R. Okajima committed
228
}
J. R. Okajima's avatar
J. R. Okajima committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

/* dentry and super_block lock. call at entry point */
int aufs_read_lock(struct dentry *dentry, int flags)
{
	int err;
	struct super_block *sb;

	sb = dentry->d_sb;
	err = si_read_lock(sb, flags);
	if (unlikely(err))
		goto out;

	if (au_ftest_lock(flags, DW))
		di_write_lock_child(dentry);
	else
		di_read_lock_child(dentry, flags);

	if (au_ftest_lock(flags, GEN)) {
		err = au_digen_test(dentry, au_sigen(sb));
		if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
			AuDebugOn(!err && au_dbrange_test(dentry));
		else if (!err)
			err = au_dbrange_test(dentry);
		if (unlikely(err))
			aufs_read_unlock(dentry, flags);
	}

out:
	return err;
}

void aufs_read_unlock(struct dentry *dentry, int flags)
{
	if (au_ftest_lock(flags, DW))
		di_write_unlock(dentry);
	else
		di_read_unlock(dentry, flags);
	si_read_unlock(dentry->d_sb);
}

void aufs_write_lock(struct dentry *dentry)
{
	si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
	di_write_lock_child(dentry);
}

void aufs_write_unlock(struct dentry *dentry)
{
	di_write_unlock(dentry);
	si_write_unlock(dentry->d_sb);
}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
{
	int err;
	unsigned int sigen;
	struct super_block *sb;

	sb = d1->d_sb;
	err = si_read_lock(sb, flags);
	if (unlikely(err))
		goto out;

	di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));

	if (au_ftest_lock(flags, GEN)) {
		sigen = au_sigen(sb);
		err = au_digen_test(d1, sigen);
		AuDebugOn(!err && au_dbrange_test(d1));
		if (!err) {
			err = au_digen_test(d2, sigen);
			AuDebugOn(!err && au_dbrange_test(d2));
		}
		if (unlikely(err))
			aufs_read_and_write_unlock2(d1, d2);
	}

out:
	return err;
}

void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
{
	di_write_unlock2(d1, d2);
	si_read_unlock(d1->d_sb);
}