iinfo.c 6.53 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
23
24
25
26
27
28
29
 */

/*
 * inode private data
 */

#include "aufs.h"

struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
{
	struct inode *h_inode;
	struct au_hinode *hinode;

J. R. Okajima's avatar
J. R. Okajima committed
30
31
	IiMustAnyLock(inode);

J. R. Okajima's avatar
J. R. Okajima committed
32
33
34
35
36
37
38
39
40
	hinode = au_hinode(au_ii(inode), bindex);
	h_inode = hinode->hi_inode;
	AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
	return h_inode;
}

/* todo: hard/soft set? */
void au_hiput(struct au_hinode *hinode)
{
J. R. Okajima's avatar
J. R. Okajima committed
41
	au_hn_free(hinode);
J. R. Okajima's avatar
J. R. Okajima committed
42
	dput(hinode->hi_whdentry);
J. R. Okajima's avatar
J. R. Okajima committed
43
44
45
	iput(hinode->hi_inode);
}

J. R. Okajima's avatar
J. R. Okajima committed
46
47
48
49
50
51
52
53
unsigned int au_hi_flags(struct inode *inode, int isdir)
{
	unsigned int flags;
	const unsigned int mnt_flags = au_mntflags(inode->i_sb);

	flags = 0;
	if (au_opt_test(mnt_flags, XINO))
		au_fset_hi(flags, XINO);
J. R. Okajima's avatar
J. R. Okajima committed
54
55
	if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
		au_fset_hi(flags, HNOTIFY);
J. R. Okajima's avatar
J. R. Okajima committed
56
57
58
	return flags;
}

J. R. Okajima's avatar
J. R. Okajima committed
59
60
61
62
63
64
65
void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
		   struct inode *h_inode, unsigned int flags)
{
	struct au_hinode *hinode;
	struct inode *hi;
	struct au_iinfo *iinfo = au_ii(inode);

J. R. Okajima's avatar
J. R. Okajima committed
66
67
	IiMustWriteLock(inode);

J. R. Okajima's avatar
J. R. Okajima committed
68
69
70
71
72
73
74
75
	hinode = au_hinode(iinfo, bindex);
	hi = hinode->hi_inode;
	AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);

	if (hi)
		au_hiput(hinode);
	hinode->hi_inode = h_inode;
	if (h_inode) {
J. R. Okajima's avatar
J. R. Okajima committed
76
		int err;
77
78
79
		struct super_block *sb = inode->i_sb;
		struct au_branch *br;

J. R. Okajima's avatar
J. R. Okajima committed
80
81
82
		AuDebugOn(inode->i_mode
			  && (h_inode->i_mode & S_IFMT)
			  != (inode->i_mode & S_IFMT));
J. R. Okajima's avatar
J. R. Okajima committed
83
84
		if (bindex == iinfo->ii_btop)
			au_cpup_igen(inode, h_inode);
85
86
		br = au_sbr(sb, bindex);
		hinode->hi_id = br->br_id;
J. R. Okajima's avatar
J. R. Okajima committed
87
88
89
90
91
92
		if (au_ftest_hi(flags, XINO)) {
			err = au_xino_write(sb, bindex, h_inode->i_ino,
					    inode->i_ino);
			if (unlikely(err))
				AuIOErr1("failed au_xino_write() %d\n", err);
		}
J. R. Okajima's avatar
J. R. Okajima committed
93
94
95
96
97
98
99

		if (au_ftest_hi(flags, HNOTIFY)
		    && au_br_hnotifyable(br->br_perm)) {
			err = au_hn_alloc(hinode, inode);
			if (unlikely(err))
				AuIOErr1("au_hn_alloc() %d\n", err);
		}
J. R. Okajima's avatar
J. R. Okajima committed
100
101
102
	}
}

J. R. Okajima's avatar
J. R. Okajima committed
103
104
105
106
107
108
109
110
111
112
113
114
void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
		  struct dentry *h_wh)
{
	struct au_hinode *hinode;

	IiMustWriteLock(inode);

	hinode = au_hinode(au_ii(inode), bindex);
	AuDebugOn(hinode->hi_whdentry);
	hinode->hi_whdentry = h_wh;
}

J. R. Okajima's avatar
J. R. Okajima committed
115
116
117
118
119
120
121
122
123
124
125
void au_update_iigen(struct inode *inode, int half)
{
	struct au_iinfo *iinfo;
	struct au_iigen *iigen;
	unsigned int sigen;

	sigen = au_sigen(inode->i_sb);
	iinfo = au_ii(inode);
	iigen = &iinfo->ii_generation;
	spin_lock(&iigen->ig_spin);
	iigen->ig_generation = sigen;
J. R. Okajima's avatar
J. R. Okajima committed
126
127
128
129
	if (half)
		au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
	else
		au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
J. R. Okajima's avatar
J. R. Okajima committed
130
131
132
	spin_unlock(&iigen->ig_spin);
}

J. R. Okajima's avatar
J. R. Okajima committed
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
/* it may be called at remount time, too */
void au_update_ibrange(struct inode *inode, int do_put_zero)
{
	struct au_iinfo *iinfo;
	aufs_bindex_t bindex, bbot;

	AuDebugOn(au_is_bad_inode(inode));
	IiMustWriteLock(inode);

	iinfo = au_ii(inode);
	if (do_put_zero && iinfo->ii_btop >= 0) {
		for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
		     bindex++) {
			struct inode *h_i;

			h_i = au_hinode(iinfo, bindex)->hi_inode;
			if (h_i
			    && !h_i->i_nlink
			    && !(h_i->i_state & I_LINKABLE))
				au_set_h_iptr(inode, bindex, NULL, 0);
		}
	}

	iinfo->ii_btop = -1;
	iinfo->ii_bbot = -1;
	bbot = au_sbbot(inode->i_sb);
	for (bindex = 0; bindex <= bbot; bindex++)
		if (au_hinode(iinfo, bindex)->hi_inode) {
			iinfo->ii_btop = bindex;
			break;
		}
	if (iinfo->ii_btop >= 0)
		for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
			if (au_hinode(iinfo, bindex)->hi_inode) {
				iinfo->ii_bbot = bindex;
				break;
			}
	AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
}

J. R. Okajima's avatar
J. R. Okajima committed
173
174
175
176
177
178
179
/* ---------------------------------------------------------------------- */

void au_icntnr_init_once(void *_c)
{
	struct au_icntnr *c = _c;
	struct au_iinfo *iinfo = &c->iinfo;

J. R. Okajima's avatar
J. R. Okajima committed
180
	spin_lock_init(&iinfo->ii_generation.ig_spin);
J. R. Okajima's avatar
J. R. Okajima committed
181
	au_rw_init(&iinfo->ii_rwsem);
J. R. Okajima's avatar
J. R. Okajima committed
182
183
184
185
186
187
	inode_init_once(&c->vfs_inode);
}

void au_hinode_init(struct au_hinode *hinode)
{
	hinode->hi_inode = NULL;
188
	hinode->hi_id = -1;
J. R. Okajima's avatar
J. R. Okajima committed
189
	au_hn_init(hinode);
J. R. Okajima's avatar
J. R. Okajima committed
190
	hinode->hi_whdentry = NULL;
J. R. Okajima's avatar
J. R. Okajima committed
191
192
193
194
195
196
197
198
199
200
201
}

int au_iinfo_init(struct inode *inode)
{
	struct au_iinfo *iinfo;
	struct super_block *sb;
	struct au_hinode *hi;
	int nbr, i;

	sb = inode->i_sb;
	iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
202
203
204
	nbr = au_sbbot(sb) + 1;
	if (unlikely(nbr <= 0))
		nbr = 1;
J. R. Okajima's avatar
J. R. Okajima committed
205
206
	hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
	if (hi) {
207
208
		au_lcnt_inc(&au_sbi(sb)->si_ninodes);

J. R. Okajima's avatar
J. R. Okajima committed
209
210
211
212
		iinfo->ii_hinode = hi;
		for (i = 0; i < nbr; i++, hi++)
			au_hinode_init(hi);

J. R. Okajima's avatar
J. R. Okajima committed
213
		iinfo->ii_generation.ig_generation = au_sigen(sb);
J. R. Okajima's avatar
J. R. Okajima committed
214
215
		iinfo->ii_btop = -1;
		iinfo->ii_bbot = -1;
216
		iinfo->ii_vdir = NULL;
J. R. Okajima's avatar
J. R. Okajima committed
217
218
219
220
221
		return 0;
	}
	return -ENOMEM;
}

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
{
	int err, i;
	struct au_hinode *hip;

	AuRwMustWriteLock(&iinfo->ii_rwsem);

	err = -ENOMEM;
	hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
			  may_shrink);
	if (hip) {
		iinfo->ii_hinode = hip;
		i = iinfo->ii_bbot + 1;
		hip += i;
		for (; i < nbr; i++, hip++)
			au_hinode_init(hip);
		err = 0;
	}

	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
244
245
246
247
void au_iinfo_fin(struct inode *inode)
{
	struct au_iinfo *iinfo;
	struct au_hinode *hi;
248
	struct super_block *sb;
J. R. Okajima's avatar
J. R. Okajima committed
249
	aufs_bindex_t bindex, bbot;
J. R. Okajima's avatar
J. R. Okajima committed
250
	const unsigned char unlinked = !inode->i_nlink;
J. R. Okajima's avatar
J. R. Okajima committed
251
252
253

	AuDebugOn(au_is_bad_inode(inode));

254
	sb = inode->i_sb;
255
	au_lcnt_dec(&au_sbi(sb)->si_ninodes);
256
257
258
259
260
261
262
263
264
265
266
267
268
	if (si_pid_test(sb))
		au_xino_delete_inode(inode, unlinked);
	else {
		/*
		 * it is safe to hide the dependency between sbinfo and
		 * sb->s_umount.
		 */
		lockdep_off();
		si_noflush_read_lock(sb);
		au_xino_delete_inode(inode, unlinked);
		si_read_unlock(sb);
		lockdep_on();
	}
J. R. Okajima's avatar
J. R. Okajima committed
269

J. R. Okajima's avatar
J. R. Okajima committed
270
	iinfo = au_ii(inode);
271
272
273
	if (iinfo->ii_vdir)
		au_vdir_free(iinfo->ii_vdir);

J. R. Okajima's avatar
J. R. Okajima committed
274
275
276
277
278
279
280
281
282
283
	bindex = iinfo->ii_btop;
	if (bindex >= 0) {
		hi = au_hinode(iinfo, bindex);
		bbot = iinfo->ii_bbot;
		while (bindex++ <= bbot) {
			if (hi->hi_inode)
				au_hiput(hi);
			hi++;
		}
	}
284
	au_kfree_rcu(iinfo->ii_hinode);
J. R. Okajima's avatar
J. R. Okajima committed
285
	AuRwDestroy(&iinfo->ii_rwsem);
J. R. Okajima's avatar
J. R. Okajima committed
286
}