finfo.c 3.34 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
30
31
32
33
34
35
36
37
38
 */

/*
 * file private data
 */

#include "aufs.h"

void au_hfput(struct au_hfile *hf, int execed)
{
	if (execed)
		allow_write_access(hf->hf_file);
	fput(hf->hf_file);
	hf->hf_file = NULL;
	au_lcnt_dec(&hf->hf_br->br_nfiles);
	hf->hf_br = NULL;
}

void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
{
	struct au_finfo *finfo = au_fi(file);
	struct au_hfile *hf;
J. R. Okajima's avatar
J. R. Okajima committed
39
	struct au_fidir *fidir;
J. R. Okajima's avatar
J. R. Okajima committed
40

J. R. Okajima's avatar
J. R. Okajima committed
41
42
43
44
45
46
	fidir = finfo->fi_hdir;
	if (!fidir) {
		AuDebugOn(finfo->fi_btop != bindex);
		hf = &finfo->fi_htop;
	} else
		hf = fidir->fd_hfile + bindex;
J. R. Okajima's avatar
J. R. Okajima committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

	if (hf && hf->hf_file)
		au_hfput(hf, vfsub_file_execed(file));
	if (val) {
		FiMustWriteLock(file);
		AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
		hf->hf_file = val;
		hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
	}
}

void au_update_figen(struct file *file)
{
	atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
	/* smp_mb(); */ /* atomic_set */
}

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

J. R. Okajima's avatar
J. R. Okajima committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
struct au_fidir *au_fidir_alloc(struct super_block *sb)
{
	struct au_fidir *fidir;
	int nbr;

	nbr = au_sbbot(sb) + 1;
	if (nbr < 2)
		nbr = 2; /* initial allocate for 2 branches */
	fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
	if (fidir) {
		fidir->fd_bbot = -1;
		fidir->fd_nent = nbr;
	}

	return fidir;
}

int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
{
	int err;
	struct au_fidir *fidir, *p;

	AuRwMustWriteLock(&finfo->fi_rwsem);
	fidir = finfo->fi_hdir;
	AuDebugOn(!fidir);

	err = -ENOMEM;
	p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
			 GFP_NOFS, may_shrink);
	if (p) {
		p->fd_nent = nbr;
		finfo->fi_hdir = p;
		err = 0;
	}

	return err;
}

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

J. R. Okajima's avatar
J. R. Okajima committed
106
107
108
109
void au_finfo_fin(struct file *file)
{
	struct au_finfo *finfo;

110
111
	au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);

J. R. Okajima's avatar
J. R. Okajima committed
112
	finfo = au_fi(file);
J. R. Okajima's avatar
J. R. Okajima committed
113
	AuDebugOn(finfo->fi_hdir);
J. R. Okajima's avatar
J. R. Okajima committed
114
115
116
117
118
119
120
121
122
123
124
	AuRwDestroy(&finfo->fi_rwsem);
	au_cache_free_finfo(finfo);
}

void au_fi_init_once(void *_finfo)
{
	struct au_finfo *finfo = _finfo;

	au_rw_init(&finfo->fi_rwsem);
}

J. R. Okajima's avatar
J. R. Okajima committed
125
int au_finfo_init(struct file *file, struct au_fidir *fidir)
J. R. Okajima's avatar
J. R. Okajima committed
126
127
128
129
130
131
132
133
134
135
136
137
{
	int err;
	struct au_finfo *finfo;
	struct dentry *dentry;

	err = -ENOMEM;
	dentry = file->f_path.dentry;
	finfo = au_cache_alloc_finfo();
	if (unlikely(!finfo))
		goto out;

	err = 0;
138
	au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
J. R. Okajima's avatar
J. R. Okajima committed
139
140
	au_rw_write_lock(&finfo->fi_rwsem);
	finfo->fi_btop = -1;
J. R. Okajima's avatar
J. R. Okajima committed
141
	finfo->fi_hdir = fidir;
J. R. Okajima's avatar
J. R. Okajima committed
142
143
144
145
146
147
148
149
	atomic_set(&finfo->fi_generation, au_digen(dentry));
	/* smp_mb(); */ /* atomic_set */

	file->private_data = finfo;

out:
	return err;
}