dinfo.c 6.94 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
 */

/*
 * dentry private data
 */

#include "aufs.h"

void au_di_init_once(void *_dinfo)
{
	struct au_dinfo *dinfo = _dinfo;

	au_rw_init(&dinfo->di_rwsem);
}

struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
{
	struct au_dinfo *dinfo;
22
	int nbr, i;
J. R. Okajima's avatar
J. R. Okajima committed
23
24
25
26
27

	dinfo = au_cache_alloc_dinfo();
	if (unlikely(!dinfo))
		goto out;

28
29
30
	nbr = au_sbbot(sb) + 1;
	if (nbr <= 0)
		nbr = 1;
J. R. Okajima's avatar
J. R. Okajima committed
31
32
33
34
35
	dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
	if (dinfo->di_hdentry) {
		au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
		dinfo->di_btop = -1;
		dinfo->di_bbot = -1;
J. R. Okajima's avatar
J. R. Okajima committed
36
		dinfo->di_bwh = -1;
J. R. Okajima's avatar
J. R. Okajima committed
37
		dinfo->di_bdiropq = -1;
38
39
		for (i = 0; i < nbr; i++)
			dinfo->di_hdentry[i].hd_id = -1;
J. R. Okajima's avatar
J. R. Okajima committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		goto out;
	}

	au_cache_free_dinfo(dinfo);
	dinfo = NULL;

out:
	return dinfo;
}

void au_di_free(struct au_dinfo *dinfo)
{
	struct au_hdentry *p;
	aufs_bindex_t bbot, bindex;

	/* dentry may not be revalidated */
	bindex = dinfo->di_btop;
	if (bindex >= 0) {
		bbot = dinfo->di_bbot;
		p = au_hdentry(dinfo, bindex);
		while (bindex++ <= bbot)
			au_hdput(p++);
	}
	au_kfree_try_rcu(dinfo->di_hdentry);
	au_cache_free_dinfo(dinfo);
}

int au_di_init(struct dentry *dentry)
{
	int err;
	struct super_block *sb;
	struct au_dinfo *dinfo;

	err = 0;
	sb = dentry->d_sb;
	dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
J. R. Okajima's avatar
J. R. Okajima committed
76
77
78
	if (dinfo) {
		atomic_set(&dinfo->di_generation, au_sigen(sb));
		/* smp_mb(); */ /* atomic_set */
J. R. Okajima's avatar
J. R. Okajima committed
79
		dentry->d_fsdata = dinfo;
J. R. Okajima's avatar
J. R. Okajima committed
80
	} else
J. R. Okajima's avatar
J. R. Okajima committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		err = -ENOMEM;

	return err;
}

void au_di_fin(struct dentry *dentry)
{
	struct au_dinfo *dinfo;

	dinfo = au_di(dentry);
	AuRwDestroy(&dinfo->di_rwsem);
	au_di_free(dinfo);
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
{
	int err, sz;
	struct au_hdentry *hdp;

	AuRwMustWriteLock(&dinfo->di_rwsem);

	err = -ENOMEM;
	sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
	if (!sz)
		sz = sizeof(*hdp);
	hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
			   may_shrink);
	if (hdp) {
		dinfo->di_hdentry = hdp;
		err = 0;
	}

	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ---------------------------------------------------------------------- */

static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
{
	switch (lsc) {
	case AuLsc_DI_CHILD:
		ii_write_lock_child(inode);
		break;
	case AuLsc_DI_CHILD2:
		ii_write_lock_child2(inode);
		break;
	case AuLsc_DI_CHILD3:
		ii_write_lock_child3(inode);
		break;
	case AuLsc_DI_PARENT:
		ii_write_lock_parent(inode);
		break;
	case AuLsc_DI_PARENT2:
		ii_write_lock_parent2(inode);
		break;
	case AuLsc_DI_PARENT3:
		ii_write_lock_parent3(inode);
		break;
	default:
		BUG();
	}
}

static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
{
	switch (lsc) {
	case AuLsc_DI_CHILD:
		ii_read_lock_child(inode);
		break;
	case AuLsc_DI_CHILD2:
		ii_read_lock_child2(inode);
		break;
	case AuLsc_DI_CHILD3:
		ii_read_lock_child3(inode);
		break;
	case AuLsc_DI_PARENT:
		ii_read_lock_parent(inode);
		break;
	case AuLsc_DI_PARENT2:
		ii_read_lock_parent2(inode);
		break;
	case AuLsc_DI_PARENT3:
		ii_read_lock_parent3(inode);
		break;
	default:
		BUG();
	}
}

void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
{
	struct inode *inode;

	au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
	if (d_really_is_positive(d)) {
		inode = d_inode(d);
		if (au_ftest_lock(flags, IW))
			do_ii_write_lock(inode, lsc);
		else if (au_ftest_lock(flags, IR))
			do_ii_read_lock(inode, lsc);
	}
}

void di_read_unlock(struct dentry *d, int flags)
{
	struct inode *inode;

	if (d_really_is_positive(d)) {
		inode = d_inode(d);
		if (au_ftest_lock(flags, IW)) {
			au_dbg_verify_dinode(d);
			ii_write_unlock(inode);
		} else if (au_ftest_lock(flags, IR)) {
			au_dbg_verify_dinode(d);
			ii_read_unlock(inode);
		}
	}
	au_rw_read_unlock(&au_di(d)->di_rwsem);
}

void di_downgrade_lock(struct dentry *d, int flags)
{
	if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
		ii_downgrade_lock(d_inode(d));
	au_rw_dgrade_lock(&au_di(d)->di_rwsem);
}

void di_write_lock(struct dentry *d, unsigned int lsc)
{
	au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
	if (d_really_is_positive(d))
		do_ii_write_lock(d_inode(d), lsc);
}

void di_write_unlock(struct dentry *d)
{
	au_dbg_verify_dinode(d);
	if (d_really_is_positive(d))
		ii_write_unlock(d_inode(d));
	au_rw_write_unlock(&au_di(d)->di_rwsem);
}

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

struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
{
	struct dentry *d;

	DiMustAnyLock(dentry);

	if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
		return NULL;
	AuDebugOn(bindex < 0);
	d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
	AuDebugOn(d && au_dcount(d) <= 0);
	return d;
}

J. R. Okajima's avatar
J. R. Okajima committed
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
aufs_bindex_t au_dbtail(struct dentry *dentry)
{
	aufs_bindex_t bbot, bwh;

	bbot = au_dbbot(dentry);
	if (0 <= bbot) {
		bwh = au_dbwh(dentry);
		if (!bwh)
			return bwh;
		if (0 < bwh && bwh < bbot)
			return bwh - 1;
	}
	return bbot;
}

aufs_bindex_t au_dbtaildir(struct dentry *dentry)
{
	aufs_bindex_t bbot, bopq;

	bbot = au_dbtail(dentry);
	if (0 <= bbot) {
		bopq = au_dbdiropq(dentry);
		if (0 <= bopq && bopq < bbot)
			bbot = bopq;
	}
	return bbot;
}

J. R. Okajima's avatar
J. R. Okajima committed
267
268
269
270
271
272
273
/* ---------------------------------------------------------------------- */

void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
		   struct dentry *h_dentry)
{
	struct au_dinfo *dinfo;
	struct au_hdentry *hd;
274
	struct au_branch *br;
J. R. Okajima's avatar
J. R. Okajima committed
275
276
277
278
279
280
281

	DiMustWriteLock(dentry);

	dinfo = au_di(dentry);
	hd = au_hdentry(dinfo, bindex);
	au_hdput(hd);
	hd->hd_dentry = h_dentry;
282
283
284
285
	if (h_dentry) {
		br = au_sbr(dentry->d_sb, bindex);
		hd->hd_id = br->br_id;
	}
J. R. Okajima's avatar
J. R. Okajima committed
286
287
}

J. R. Okajima's avatar
J. R. Okajima committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
int au_digen_test(struct dentry *dentry, unsigned int sigen)
{
	int err;

	err = 0;
	if (unlikely(au_digen(dentry) != sigen
		     || au_iigen_test(d_inode(dentry), sigen)))
		err = -EIO;

	return err;
}

void au_update_digen(struct dentry *dentry)
{
	atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
	/* smp_mb(); */ /* atomic_set */
}

J. R. Okajima's avatar
J. R. Okajima committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
void au_update_dbtop(struct dentry *dentry)
{
	aufs_bindex_t bindex, bbot;
	struct dentry *h_dentry;

	bbot = au_dbbot(dentry);
	for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
		h_dentry = au_h_dptr(dentry, bindex);
		if (!h_dentry)
			continue;
		if (d_is_positive(h_dentry)) {
			au_set_dbtop(dentry, bindex);
			return;
		}
		au_set_h_dptr(dentry, bindex, NULL);
	}
}

void au_update_dbbot(struct dentry *dentry)
{
	aufs_bindex_t bindex, btop;
	struct dentry *h_dentry;

	btop = au_dbtop(dentry);
	for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
		h_dentry = au_h_dptr(dentry, bindex);
		if (!h_dentry)
			continue;
		if (d_is_positive(h_dentry)) {
			au_set_dbbot(dentry, bindex);
			return;
		}
		au_set_h_dptr(dentry, bindex, NULL);
	}
}
341
342
343
344
345
346
347
348
349
350
351

int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
{
	aufs_bindex_t bindex, bbot;

	bbot = au_dbbot(dentry);
	for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
		if (au_h_dptr(dentry, bindex) == h_dentry)
			return bindex;
	return -1;
}