-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdir.c
254 lines (208 loc) · 6.49 KB
/
dir.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
253
254
/*
* linux/fs/sfs/file.c
*
* Copyright (C) 2013
*/
#include <linux/buffer_head.h>
#include <asm-generic/errno-base.h>
#include "simplefs.h"
static int simplefs_readdir(struct file *filp, void *dirent, filldir_t filldir) {
unsigned long pos = filp->f_pos;
struct inode *inode = filp->f_path.dentry->d_inode;
unsigned offset = pos & ~PAGE_CACHE_MASK;
unsigned long n = pos >> PAGE_CACHE_SHIFT;
unsigned long npages = inode_pages(inode);
printk(KERN_INFO "simplefs_readdir\n");
pos = (pos + SIMPLEFS_DENTRY_SIZE - 1) & ~(SIMPLEFS_DENTRY_SIZE - 1);
if (pos >= inode->i_size)
goto done;
for ( ; n < npages; n++, offset = 0) {
char *p, *kaddr, *limit;
struct page *page = simplefs_get_page(inode, n);
if (IS_ERR(page))
continue;
kaddr = (char *)page_address(page);
p = kaddr + offset;
limit = kaddr + inode_last_bytes(inode, n) - SIMPLEFS_DENTRY_SIZE;
for ( ; p <= limit; p = p + SIMPLEFS_DENTRY_SIZE) {
struct simplefs_dentry *de = (struct simplefs_dentry *)p;
if (de->flags == SIMPLEFS_DENTRY_UNUSED)
continue;
if (de->inode) {
int over;
unsigned len = strnlen(de->name, SIMPLEFS_NAME_LEN);
offset = p - kaddr;
over = filldir(dirent, de->name, len,
(n << PAGE_CACHE_SHIFT) | offset, de->inode, DT_UNKNOWN);
if (over) {
simplefs_put_page(page);
goto done;
}
}
}
simplefs_put_page(page);
}
done:
filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
return 0;
}
const struct file_operations simplefs_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.readdir = simplefs_readdir,
.fsync = simple_fsync,
};
static void ext2_iput(struct dentry *dentry, struct inode *inode) {
printk(KERN_INFO "d_iput %s %d\n", dentry->d_name.name, dentry->d_count.counter);
printk(KERN_INFO "d_iput inode %d\n", inode->i_count.counter);
iput(inode);
}
static int ext2_delete(struct dentry *dentry) {
printk(KERN_INFO "d_delete %s\n", dentry->d_name.name);
return 0;
}
const struct dentry_operations d_op = {
.d_iput = ext2_iput,
.d_delete = ext2_delete,
};
static struct dentry *simplefs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) {
struct inode *inode = NULL;
struct page *pagep = NULL;
struct simplefs_dentry *raw_de;
long ino;
dentry->d_op = dir->i_sb->s_root->d_op;
if (dentry->d_name.len > SIMPLEFS_NAME_LEN)
return ERR_PTR(-ENAMETOOLONG);
raw_de = simplefs_find_dentry(dir, dentry, &pagep);
if (raw_de) {
ino = raw_de->inode;
simplefs_put_page(pagep);
inode = simplefs_iget(dir->i_sb, ino);
if (IS_ERR(inode))
return ERR_CAST(inode);
}
//dentry->d_op = &d_op;
if (inode) {
printk(KERN_INFO "simplefs_lookup: dentry %d", dentry->d_count.counter);
printk(KERN_INFO "simplefs_lookup: inode %ld %ld %d %d", inode->i_ino, inode->i_state, inode->i_count.counter, inode->i_nlink);
}
return d_splice_alias(inode, dentry);
}
static int simplefs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd) {
struct inode *inode = NULL;
long ino;
int err = 0;
ino = bitmap_alloc_inode(dir->i_sb);
if (ino < 0)
return -EIO;
inode = simplefs_iget(dir->i_sb, ino);
if (IS_ERR(inode))
return -EIO;
inode->i_state = 0;
inode->i_mode = mode | S_IFREG;
inode->i_nlink = 1;
inode->i_size = inode->i_blocks = inode->i_bytes = 0;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
err = simplefs_insert_dentry(dentry, inode);
if (!err) {
//dentry->d_op = &d_op;
d_instantiate(dentry, inode);
if (inode) {
printk(KERN_INFO "simplefs_create: dentry %d", dentry->d_count.counter);
printk(KERN_INFO "simplefs_create: inode %ld %ld %d %d", inode->i_ino, inode->i_state, inode->i_count.counter, inode->i_nlink);
}
return 0;
}
inode_dec_link_count(inode);
iput(inode);
return err;
}
/*
static int simplefs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) {
struct inode *inode = old_dentry->d_inode;
inode->i_ctime = CURRENT_TIME_SEC;
inode_inc_link_count(inode);
atomic_inc(&inode->i_count);
}
*/
static int simplefs_unlink(struct inode *dir, struct dentry *dentry) {
struct page *rs_page = NULL;
struct simplefs_dentry *raw_de;
struct inode *inode = dentry->d_inode;
int err = -ENOENT;
printk(KERN_INFO "simplefs_unlink %s %d\n", dentry->d_name.name, dentry->d_count.counter);
printk(KERN_INFO "simplefs_unlink %d %d \n", inode->i_count.counter, inode->i_nlink);
raw_de = simplefs_find_dentry(dir, dentry, &rs_page);
if (!raw_de)
goto out;
err = simplefs_delete_dentry(raw_de, rs_page);
if (err)
goto out;
inode->i_ctime = dir->i_ctime;
inode_dec_link_count(inode);
printk(KERN_INFO "simplefs_unlink %s %d\n", dentry->d_name.name, dentry->d_count.counter);
printk(KERN_INFO "simplefs_unlink %d %d \n", inode->i_count.counter, inode->i_nlink);
out:
return err;
}
static int simplefs_mkdir(struct inode *dir, struct dentry *dentry, int mode) {
struct inode *inode = NULL;
long ino;
int err = 0;
printk(KERN_INFO "simplefs_mkdir: %s %d\n", dentry->d_name.name, mode);
inode_inc_link_count(dir);
ino = bitmap_alloc_inode(dir->i_sb);
if (ino < 0)
return -EIO;
inode = simplefs_iget(dir->i_sb, ino);
if (IS_ERR(inode))
return -EIO;
// set inode operations
inode->i_op = &simplefs_file_inode_operations;
inode->i_fop = &simplefs_file_operations;
inode->i_mapping->a_ops = &simplefs_aops;
inode->i_mode = mode | S_IFDIR;
inode->i_nlink = 2;
inode->i_size = inode->i_blocks = inode->i_bytes = 0;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
err = simplefs_insert_dentry(dentry, inode);
if (!err) {
d_instantiate(dentry, inode);
return 0;
}
inode_dec_link_count(inode);
inode_dec_link_count(inode);
iput(inode);
inode_dec_link_count(dir);
return err;
}
static int simplefs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) {
struct inode *inode = old_dentry->d_inode;
int err;
inode->i_ctime = CURRENT_TIME_SEC;
inode_inc_link_count(inode);
atomic_inc(&inode->i_count);
err = simplefs_insert_dentry(dentry, inode);
if (!err) {
d_instantiate(dentry, inode);
return 0;
}
inode_dec_link_count(inode);
iput(inode);
return err;
}
static int simplefs_rmdir(struct inode *dir, struct dentry *dentry) {
return -ENOTEMPTY;
}
const struct inode_operations simplefs_dir_inode_operations = {
.create = simplefs_create,
.lookup = simplefs_lookup,
.link = simplefs_link,
.unlink = simplefs_unlink,
.mkdir = simplefs_mkdir,
.rmdir = simplefs_rmdir,
.rename = NULL,
};