19
#include <linux/kernel.h>
20
#include <linux/crc32.h>
24
struct _inode *jffs2_lookup(struct _inode *dir_i,
25
const unsigned char *d_name, int namelen) {
26
struct jffs2_inode_info *dir_f;
27
struct jffs2_full_dirent *fd = NULL, *fd_list;
29
uint32_t hash = full_name_hash(d_name, namelen);
30
struct _inode *inode = NULL;
32
D1(printk("jffs2_lookup()\n"));
34
dir_f = JFFS2_INODE_INFO(dir_i);
39
for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash;
40
fd_list = fd_list->next) {
41
if (fd_list->nhash == hash &&
42
(!fd || fd_list->version > fd->version) &&
43
strlen((char *)fd_list->name) == namelen &&
44
!strncmp((char *)fd_list->name, (char *)d_name, namelen)) {
53
inode = jffs2_iget(dir_i->i_sb, ino);
55
printk("jffs2_iget() failed for ino #%u\n", ino);
63
int jffs2_create(struct _inode *dir_i, const unsigned char *d_name,
64
int mode, struct _inode **new_i) {
65
struct jffs2_raw_inode *ri;
66
struct jffs2_inode_info *f, *dir_f;
67
struct jffs2_sb_info *c;
71
ri = jffs2_alloc_raw_inode();
76
c = &dir_i->i_sb->jffs2_sb;
78
D1(printk( "jffs2_create()\n"));
80
inode = jffs2_new_inode(dir_i, mode, ri);
83
D1(printk( "jffs2_new_inode() failed\n"));
84
jffs2_free_raw_inode(ri);
85
return PTR_ERR(inode);
88
f = JFFS2_INODE_INFO(inode);
89
dir_f = JFFS2_INODE_INFO(dir_i);
91
ret = jffs2_do_create(c, dir_f, f, ri,
93
strlen((char *)d_name));
98
jffs2_free_raw_inode(ri);
102
jffs2_free_raw_inode(ri);
104
D1(printk("jffs2_create: Created ino #%d with mode %o, nlink %d(%d)\n",
105
inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink));
113
int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode,
114
const unsigned char *d_name) {
115
struct jffs2_sb_info *c;
116
struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
117
struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode);
120
c = &dir_i->i_sb->jffs2_sb;
122
ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
123
strlen((char *)d_name), dead_f);
124
if (dead_f->inocache) {
125
d_inode->i_nlink = dead_f->inocache->nlink;
133
int jffs2_link (struct _inode *old_d_inode,
134
struct _inode *dir_i, const unsigned char *d_name) {
135
struct jffs2_sb_info *c;
136
struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode);
137
struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
141
c = &old_d_inode->i_sb->jffs2_sb;
144
type = (old_d_inode->i_mode & S_IFMT) >> 12;
149
ret = jffs2_do_link(c, dir_f, f->inocache->ino, type,
150
(const char * )d_name, strlen((char *)d_name));
153
old_d_inode->i_nlink = ++f->inocache->nlink;
159
int jffs2_mkdir (struct _inode *dir_i,
160
const unsigned char *d_name, int mode) {
161
struct jffs2_inode_info *f, *dir_f;
162
struct jffs2_sb_info *c;
163
struct _inode *inode;
164
struct jffs2_raw_inode *ri;
165
struct jffs2_raw_dirent *rd;
166
struct jffs2_full_dnode *fn;
167
struct jffs2_full_dirent *fd;
169
uint32_t alloclen, phys_ofs;
174
ri = jffs2_alloc_raw_inode();
178
c = &dir_i->i_sb->jffs2_sb;
183
namelen = strlen((char *)d_name);
184
ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
187
jffs2_free_raw_inode(ri);
191
inode = jffs2_new_inode(dir_i, mode, ri);
194
jffs2_free_raw_inode(ri);
195
jffs2_complete_reservation(c);
196
return PTR_ERR(inode);
199
f = JFFS2_INODE_INFO(inode);
201
ri->data_crc = cpu_to_je32(0);
202
ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
204
fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
206
jffs2_free_raw_inode(ri);
211
jffs2_complete_reservation(c);
222
jffs2_complete_reservation(c);
223
ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
231
rd = jffs2_alloc_raw_dirent();
234
jffs2_complete_reservation(c);
240
dir_f = JFFS2_INODE_INFO(dir_i);
243
rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
244
rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
245
rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
246
rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
248
rd->pino = cpu_to_je32(dir_i->i_ino);
249
rd->version = cpu_to_je32(++dir_f->highest_version);
250
rd->ino = cpu_to_je32(inode->i_ino);
251
rd->mctime = cpu_to_je32(timestamp());
254
rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
255
rd->name_crc = cpu_to_je32(crc32(0, d_name, namelen));
257
fd = jffs2_write_dirent(c, dir_f, rd, d_name, namelen, phys_ofs, ALLOC_NORMAL);
259
jffs2_complete_reservation(c);
260
jffs2_free_raw_dirent(rd);
273
jffs2_add_fd_to_list(c, fd, &dir_f->dents);
280
int jffs2_rmdir (struct _inode *dir_i,
281
struct _inode *d_inode, const unsigned char *d_name) {
282
struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
283
struct jffs2_full_dirent *fd;
285
for (fd = f->dents ; fd; fd = fd->next) {
290
return jffs2_unlink(dir_i, d_inode, d_name);
293
int jffs2_rename (struct _inode *old_dir_i,
294
struct _inode *d_inode, const unsigned char *old_d_name,
295
struct _inode *new_dir_i, const unsigned char *new_d_name) {
297
struct jffs2_sb_info *c;
298
struct jffs2_inode_info *victim_f = NULL;
301
c = &old_dir_i->i_sb->jffs2_sb;
305
if (new_dentry->d_inode) {
306
if (S_ISDIR(d_inode->i_mode) &&
307
!S_ISDIR(new_dentry->d_inode->i_mode)) {
312
victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
314
if (S_ISDIR(new_dentry->d_inode->i_mode)) {
315
struct jffs2_full_dirent *fd;
317
if (!S_ISDIR(d_inode->i_mode)) {
321
down(&victim_f->sem);
322
for (fd = victim_f->dents; fd; fd = fd->next) {
341
type = (d_inode->i_mode & S_IFMT) >> 12;
346
ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), d_inode->i_ino, type,
347
(const char *)new_d_name, strlen((char *)new_d_name));
357
if (victim_f->inocache) {
358
down(&victim_f->sem);
359
victim_f->inocache->nlink--;
365
ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
366
(const char *)old_d_name, strlen((char *)old_d_name), NULL);
370
struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
373
d_inode->i_nlink = f->inocache->nlink++;
377
printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);