forked from dlorch/hammer-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
252 lines (216 loc) · 7.55 KB
/
file.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
243
244
245
246
247
248
249
250
251
252
/*
* file operations for HAMMER Filesystem
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/version.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/errno.h>
#include <linux/string.h>
#include "hammerfs.h"
#include "dfly_wrap.h"
#include <vfs/hammer/hammer.h>
static int hammerfs_open(struct inode *inode, struct file *file)
{
printk("hammerfs_open(node->i_ino=%lu)\n", inode->i_ino);
file->f_ra.ra_pages = 0; /* No read-ahead */
return generic_file_open (inode, file);
}
// corresponds to hammer_vop_readdir
int hammerfs_readdir(struct file *file, void *dirent, filldir_t filldir)
{
struct dentry *de = file->f_dentry;
struct hammer_transaction trans;
struct hammer_cursor cursor;
struct hammer_inode *ip = (struct hammer_inode *)de->d_inode->i_private;
hammer_base_elm_t base;
int r;
int error;
int dtype;
printk("hammerfs_readdir(file->f_pos=%d)\n", file->f_pos);
/*
* Handle artificial entries
*/
if(file->f_pos == 0) {
r = filldir(dirent, ".", 1, file->f_pos++, de->d_inode->i_ino, DT_DIR);
}
if(!r && file->f_pos == 1) {
if(de->d_parent->d_inode) {
r = filldir(dirent, "..", 2, file->f_pos++, de->d_parent->d_inode->i_ino, DT_DIR);
} else {
r = filldir(dirent, "..", 2, file->f_pos++, de->d_inode->i_ino, DT_DIR);
}
}
if(!r) {
hammer_simple_transaction(&trans, ip->hmp);
/*
* Key range (begin and end inclusive) to scan. Directory keys
* directly translate to a 64 bit 'seek' position.
*/
hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
cursor.key_beg.localization = ip->obj_localization +
HAMMER_LOCALIZE_MISC;
cursor.key_beg.obj_id = ip->obj_id;
cursor.key_beg.create_tid = 0;
cursor.key_beg.delete_tid = 0;
cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
cursor.key_beg.obj_type = 0;
cursor.key_beg.key = file->f_pos;
cursor.key_end = cursor.key_beg;
cursor.key_end.key = HAMMER_MAX_KEY;
cursor.asof = ip->obj_asof;
cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
error = hammer_ip_first(&cursor);
while (error == 0) {
error = hammer_ip_resolve_data(&cursor);
if (error)
break;
base = &cursor.leaf->base;
KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
/* if (base->obj_id != de->d_inode->i_ino)
panic("readdir: bad record at %p", cursor.node);
*/
/*
* Convert pseudo-filesystems into softlinks
*/
dtype = hammerfs_get_itype(cursor.leaf->base.obj_type);
r = filldir(dirent, (void *)cursor.data->entry.name,
cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF,
file->f_pos, cursor.data->entry.obj_id, dtype);
if (r)
break;
file->f_pos++;
error = hammer_ip_next(&cursor);
}
hammer_done_cursor(&cursor);
}
done:
/*hammer_done_transaction(&trans);*/
return(1); // TODO
failed:
return(1);
}
// corresponds to hammer_vop_nresolve
struct dentry *hammerfs_inode_lookup(struct inode *parent_inode, struct dentry *dentry,
struct nameidata *nameidata)
{
struct hammer_transaction trans;
struct super_block *sb;
struct inode *inode;
hammer_inode_t dip;
hammer_inode_t ip;
hammer_tid_t asof;
struct hammer_cursor cursor;
int64_t namekey;
u_int32_t max_iterations;
int64_t obj_id;
int nlen;
int flags;
int error;
u_int32_t localization;
printk("hammerfs_inode_lookup(parent_inode->i_ino=%d, dentry->d_name.name=%s)\n",
parent_inode->i_ino, dentry->d_name.name);
sb = parent_inode->i_sb;
dip = (hammer_inode_t)parent_inode->i_private;
asof = dip->obj_asof;
localization = dip->obj_localization; /* for code consistency */
nlen = dentry->d_name.len;
flags = dip->flags & HAMMER_INODE_RO;
hammer_simple_transaction(&trans, dip->hmp);
/*
* Calculate the namekey and setup the key range for the scan. This
* works kinda like a chained hash table where the lower 32 bits
* of the namekey synthesize the chain.
*
* The key range is inclusive of both key_beg and key_end.
*/
namekey = hammer_directory_namekey(dip, dentry->d_name.name, nlen,
&max_iterations);
error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
cursor.key_beg.localization = dip->obj_localization +
HAMMER_LOCALIZE_MISC;
cursor.key_beg.obj_id = dip->obj_id;
cursor.key_beg.key = namekey;
cursor.key_beg.create_tid = 0;
cursor.key_beg.delete_tid = 0;
cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
cursor.key_beg.obj_type = 0;
cursor.key_end = cursor.key_beg;
cursor.key_end.key += max_iterations;
cursor.asof = asof;
cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
/*
* Scan all matching records (the chain), locate the one matching
* the requested path component.
*
* The hammer_ip_*() functions merge in-memory records with on-disk
* records for the purposes of the search.
*/
obj_id = 0;
localization = HAMMER_DEF_LOCALIZATION;
if (error == 0) {
error = hammer_ip_first(&cursor);
while (error == 0) {
error = hammer_ip_resolve_data(&cursor);
if (error)
break;
if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
bcmp(dentry->d_name.name, cursor.data->entry.name, nlen) == 0) {
obj_id = cursor.data->entry.obj_id;
localization = cursor.data->entry.localization;
break;
}
error = hammer_ip_next(&cursor);
}
}
hammer_done_cursor(&cursor);
if (error == 0) {
ip = hammer_get_inode(&trans, dip, obj_id,
asof, localization,
flags, &error);
if(error == 0) {
error = hammerfs_get_inode(sb, ip, &inode);
/* hammer_rel_inode(ip, 0); */
} else {
ip = NULL;
}
if(error == 0) {
d_add(dentry, inode);
}
goto done;
}
done:
/*hammer_done_transaction(&trans);*/
return NULL;
}
struct file_operations hammerfs_file_operations = {
.owner = THIS_MODULE,
.open = hammerfs_open,
// .read = hammerfs_read,
.read = &do_sync_read,
.aio_read = generic_file_aio_read,
.aio_write = generic_file_aio_write,
.readdir = hammerfs_readdir
};
int hammerfs_setattr(struct dentry *dentry, struct iattr *iattr)
{
printk(KERN_INFO "hammerfs_setattr(ino=%lu, name=%s)\n",
dentry->d_inode->i_ino, dentry->d_name.name);
return -EPERM;
}
int hammerfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
struct kstat *stat)
{
struct inode *inode;
printk(KERN_INFO "hammerfs_getattr(ino=%lu, name=%s)\n",
dentry->d_inode->i_ino, dentry->d_name.name);
inode = dentry->d_inode;
generic_fillattr(inode, stat);
return 0;
}
struct inode_operations hammerfs_inode_operations = {
.lookup = hammerfs_inode_lookup,
.setattr = hammerfs_setattr,
.getattr = hammerfs_getattr
};