This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dentry.c
executable file
·242 lines (211 loc) · 7.06 KB
/
dentry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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
106
107
108
109
110
111
112
113
114
115
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
239
240
241
242
/* dazukofs: access control stackable filesystem
Copyright (C) 1997-2003 Erez Zadok
Copyright (C) 2001-2003 Stony Brook University
Copyright (C) 2004-2006 International Business Machines Corp.
Copyright (C) 2008-2010 John Ogness
Author: John Ogness <[email protected]>
This program 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/fs_stack.h>
#include "dazukofs_fs.h"
/**
* dazukofs_d_revalidate - revalidate a dentry found in the dcache
* @dentry: dentry to revalidate
* @nd: nameidata associated with dentry
*
* Description: Called when the VFS needs to revalidate a dentry. This is
* called whenever a name look-up finds a dentry in the dcache. Most
* filesystems leave this as NULL, because all their dentries in the dcache
* are valid.
*
* Call d_revalidate() on the lower dentry if available. The mnt/dentry
* (path) data in the nameidata needs to be temporarily swapped out for the
* lower call.
*
* After the call, the original path data is restored and the dentry's inode
* attributes are updated to match the lower inode.
*
* Returns 1 if dentry is valid, otherwise 0.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
static int dazukofs_d_revalidate(struct dentry *dentry, unsigned int flags)
{
struct vfsmount *lower_mnt_parent;
struct dentry *lower_dentry_parent, *lower_dentry;
int valid, err;
struct nameidata new_nd;
lower_dentry = get_lower_dentry(dentry);
if (!lower_dentry->d_op || !lower_dentry->d_op->d_revalidate)
return 1;
lower_mnt_parent = get_lower_mnt(dentry->d_parent);
lower_dentry_parent = get_lower_dentry(dentry->d_parent);
err = vfs_path_lookup(lower_dentry_parent,
lower_mnt_parent,
dentry->d_name.name, 0, &(new_nd.path));
if (err)
return -EINVAL;
valid = lower_dentry->d_op->d_revalidate(lower_dentry, 0);
path_put(&new_nd.path);
/* update the inode, even if d_revalidate() != 1 */
if (dentry->d_inode) {
struct inode *lower_inode = get_lower_inode(dentry->d_inode);
fsstack_copy_attr_all(dentry->d_inode, lower_inode);
}
return valid;
}
#else
static int dazukofs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
int valid;
if (!lower_dentry->d_op || !lower_dentry->d_op->d_revalidate)
return 1;
if (!nd) {
/* No nameidata provided. We are probably
* in a lookup_one_len() calling stack.
* Setup new nameidata ourselves. */
struct nameidata new_nd;
struct vfsmount *lower_mnt_parent =
get_lower_mnt(dentry->d_parent);
struct dentry *lower_dentry_parent =
get_lower_dentry(dentry->d_parent);
int err = vfs_path_lookup(lower_dentry_parent,
lower_mnt_parent,
dentry->d_name.name, 0, &new_nd);
if (err)
return -EINVAL;
valid = lower_dentry->d_op->d_revalidate(lower_dentry,
&new_nd);
path_put(&new_nd.path);
} else {
struct vfsmount *lower_mnt = get_lower_mnt(dentry);
struct vfsmount *vfsmount_save = nd->path.mnt;
struct dentry *dentry_save = nd->path.dentry;
nd->path.mnt = mntget(lower_mnt);
nd->path.dentry = dget(lower_dentry);
valid = lower_dentry->d_op->d_revalidate(lower_dentry, nd);
mntput(lower_mnt);
dput(lower_dentry);
nd->path.mnt = vfsmount_save;
nd->path.dentry = dentry_save;
}
/* update the inode, even if d_revalidate() != 1 */
if (dentry->d_inode) {
struct inode *lower_inode = get_lower_inode(dentry->d_inode);
fsstack_copy_attr_all(dentry->d_inode, lower_inode);
}
return valid;
}
#endif
/**
* dazukofs_d_hash - hash the given name
* @dentry: the parent dentry
* @name: the name to hash
*
* Description: Called when the VFS adds a dentry to the hash table.
*
* Call d_hash() on the lower dentry if available. Otherwise dazukofs
* does nothing. This is ok because the VFS will compute a default
* hash.
*
* Returns 0 on success.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
static int dazukofs_d_hash(const struct dentry *dentry,
const struct inode *inode, struct qstr *name)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
if (!lower_dentry->d_op || !lower_dentry->d_op->d_hash)
return 0;
return lower_dentry->d_op->d_hash(lower_dentry, inode, name);
}
#else
static int dazukofs_d_hash(struct dentry *dentry, struct qstr *name)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
if (!lower_dentry->d_op || !lower_dentry->d_op->d_hash)
return 0;
return lower_dentry->d_op->d_hash(lower_dentry, name);
}
#endif
/**
* dazukofs_d_release - clean up dentry
* @dentry: the dentry that will be released
*
* Description: Called when a dentry is really deallocated.
*
* Release our hold on the lower dentry and mnt. Then free the structure
* (from the cache) containing the lower data for this dentry.
*/
static void dazukofs_d_release(struct dentry *dentry)
{
if (get_dentry_private(dentry)) {
dput(get_lower_dentry(dentry));
mntput(get_lower_mnt(dentry));
kmem_cache_free(dazukofs_dentry_info_cachep,
get_dentry_private(dentry));
}
}
/**
* dazukofs_d_compare - used to compare dentry's
* @dentry: the parent dentry
* @a: qstr of an existing dentry
* @b: qstr of a second dentry (dentry may not be valid)
*
* Description: Called when a dentry should be compared with another.
*
* Call d_compare() on the lower dentry if available. Otherwise, perform
* some basic comparisons between the two qstr's.
*
* Returns 0 if they are the same, otherwise 1.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
static int dazukofs_d_compare(const struct dentry *parent,
const struct inode *pinode,
const struct dentry *dentry, const struct inode *inode,
unsigned int len, const char *str, const struct qstr *name)
{
if (len != name->len || memcmp(str, name->name, len))
return 1;
return 0;
}
#else
static int dazukofs_d_compare(struct dentry *dentry, struct qstr *a,
struct qstr *b)
{
struct dentry *lower_dentry = get_lower_dentry(dentry);
if (lower_dentry->d_op && lower_dentry->d_op->d_compare)
return lower_dentry->d_op->d_compare(lower_dentry, a, b);
if (a->len != b->len)
return 1;
if (memcmp(a->name, b->name, a->len))
return 1;
return 0;
}
#endif
/**
* Unused operations:
* - d_delete
* - d_iput
* - d_dname
*/
struct dentry_operations dazukofs_dops = {
.d_revalidate = dazukofs_d_revalidate,
.d_hash = dazukofs_d_hash,
.d_release = dazukofs_d_release,
.d_compare = dazukofs_d_compare,
};