2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
14 #include <linux/kernel.h>
15 #include <linux/crc32.h>
18 /***********************************************************************/
20 /* Takes length argument because it can be either NUL-terminated or '/'-terminated */
21 struct _inode *jffs2_lookup(struct _inode *dir_i, const unsigned char *d_name, int namelen)
23 struct jffs2_inode_info *dir_f;
24 struct jffs2_sb_info *c;
25 struct jffs2_full_dirent *fd = NULL, *fd_list;
27 uint32_t hash = full_name_hash(d_name, namelen);
28 struct _inode *inode = NULL;
30 D1(printk("jffs2_lookup()\n"));
32 dir_f = JFFS2_INODE_INFO(dir_i);
33 c = JFFS2_SB_INFO(dir_i->i_sb);
37 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
38 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash; fd_list = fd_list->next) {
39 if (fd_list->nhash == hash &&
40 (!fd || fd_list->version > fd->version) &&
41 strlen((char *)fd_list->name) == namelen &&
42 !strncmp((char *)fd_list->name, (char *)d_name, namelen)) {
50 inode = jffs2_iget(dir_i->i_sb, ino);
52 printk("jffs2_iget() failed for ino #%u\n", ino);
60 /***********************************************************************/
64 int jffs2_create(struct _inode *dir_i, const unsigned char *d_name, int mode,
65 struct _inode **new_i)
67 struct jffs2_raw_inode *ri;
68 struct jffs2_inode_info *f, *dir_f;
69 struct jffs2_sb_info *c;
73 ri = jffs2_alloc_raw_inode();
77 c = JFFS2_SB_INFO(dir_i->i_sb);
79 D1(printk(KERN_DEBUG "jffs2_create()\n"));
81 inode = jffs2_new_inode(dir_i, mode, ri);
84 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
85 jffs2_free_raw_inode(ri);
86 return PTR_ERR(inode);
89 f = JFFS2_INODE_INFO(inode);
90 dir_f = JFFS2_INODE_INFO(dir_i);
92 ret = jffs2_do_create(c, dir_f, f, ri,
94 strlen((char *)d_name));
99 jffs2_free_raw_inode(ri);
103 jffs2_free_raw_inode(ri);
105 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d)\n",
106 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink));
111 /***********************************************************************/
114 int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name)
116 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
117 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
118 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode);
121 ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
122 strlen((char *)d_name), dead_f);
123 if (dead_f->inocache)
124 d_inode->i_nlink = dead_f->inocache->nlink;
127 /***********************************************************************/
130 int jffs2_link (struct _inode *old_d_inode, struct _inode *dir_i, const unsigned char *d_name)
132 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_d_inode->i_sb);
133 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode);
134 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
137 /* XXX: This is ugly */
138 uint8_t type = (old_d_inode->i_mode & S_IFMT) >> 12;
139 if (!type) type = DT_REG;
141 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type,
142 (const char * )d_name,
143 strlen((char *)d_name));
147 old_d_inode->i_nlink = ++f->inocache->nlink;
153 int jffs2_mkdir (struct _inode *dir_i, const unsigned char *d_name, int mode)
155 struct jffs2_inode_info *f, *dir_f;
156 struct jffs2_sb_info *c;
157 struct _inode *inode;
158 struct jffs2_raw_inode *ri;
159 struct jffs2_raw_dirent *rd;
160 struct jffs2_full_dnode *fn;
161 struct jffs2_full_dirent *fd;
163 uint32_t alloclen, phys_ofs;
168 ri = jffs2_alloc_raw_inode();
172 c = JFFS2_SB_INFO(dir_i->i_sb);
174 /* Try to reserve enough space for both node and dirent.
175 * Just the node will do for now, though
177 namelen = strlen((char *)d_name);
178 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
181 jffs2_free_raw_inode(ri);
185 inode = jffs2_new_inode(dir_i, mode, ri);
188 jffs2_free_raw_inode(ri);
189 jffs2_complete_reservation(c);
190 return PTR_ERR(inode);
193 f = JFFS2_INODE_INFO(inode);
195 ri->data_crc = cpu_to_je32(0);
196 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
198 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
200 jffs2_free_raw_inode(ri);
203 /* Eeek. Wave bye bye */
205 jffs2_complete_reservation(c);
210 /* No data here. Only a metadata node, which will be
211 obsoleted by the first data write
216 jffs2_complete_reservation(c);
217 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
225 rd = jffs2_alloc_raw_dirent();
227 /* Argh. Now we treat it like a normal delete */
228 jffs2_complete_reservation(c);
234 dir_f = JFFS2_INODE_INFO(dir_i);
237 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
238 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
239 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
240 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
242 rd->pino = cpu_to_je32(dir_i->i_ino);
243 rd->version = cpu_to_je32(++dir_f->highest_version);
244 rd->ino = cpu_to_je32(inode->i_ino);
245 rd->mctime = cpu_to_je32(cyg_timestamp());
248 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
249 rd->name_crc = cpu_to_je32(crc32(0, d_name, namelen));
251 fd = jffs2_write_dirent(c, dir_f, rd, d_name, namelen, phys_ofs, ALLOC_NORMAL);
253 jffs2_complete_reservation(c);
254 jffs2_free_raw_dirent(rd);
257 /* dirent failed to write. Delete the inode normally
258 as if it were the final unlink() */
265 /* Link the fd into the inode's list, obsoleting an old
267 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
274 int jffs2_rmdir (struct _inode *dir_i, struct _inode *d_inode, const unsigned char *d_name)
276 struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
277 struct jffs2_full_dirent *fd;
279 for (fd = f->dents ; fd; fd = fd->next) {
281 return EPERM; //-ENOTEMPTY;
283 return jffs2_unlink(dir_i, d_inode, d_name);
286 int jffs2_rename (struct _inode *old_dir_i, struct _inode *d_inode, const unsigned char *old_d_name,
287 struct _inode *new_dir_i, const unsigned char *new_d_name)
290 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
291 struct jffs2_inode_info *victim_f = NULL;
294 #if 0 /* FIXME -- this really doesn't belong in individual file systems.
295 The fileio code ought to do this for us, or at least part of it */
296 if (new_dentry->d_inode) {
297 if (S_ISDIR(d_inode->i_mode) &&
298 !S_ISDIR(new_dentry->d_inode->i_mode)) {
299 /* Cannot rename directory over non-directory */
303 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
305 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
306 struct jffs2_full_dirent *fd;
308 if (!S_ISDIR(d_inode->i_mode)) {
309 /* Cannot rename non-directory over directory */
312 down(&victim_f->sem);
313 for (fd = victim_f->dents; fd; fd = fd->next) {
324 /* XXX: We probably ought to alloc enough space for
325 both nodes at the same time. Writing the new link,
326 then getting -ENOSPC, is quite bad :)
329 /* Make a hard link */
331 /* XXX: This is ugly */
332 type = (d_inode->i_mode & S_IFMT) >> 12;
333 if (!type) type = DT_REG;
335 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
336 d_inode->i_ino, type,
337 (const char *)new_d_name,
338 strlen((char *)new_d_name));
344 /* There was a victim. Kill it off nicely */
345 /* Don't oops if the victim was a dirent pointing to an
346 inode which didn't exist. */
347 if (victim_f->inocache) {
348 down(&victim_f->sem);
349 victim_f->inocache->nlink--;
354 /* Unlink the original */
355 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
356 (const char *)old_d_name,
357 strlen((char *)old_d_name), NULL);
360 /* Oh shit. We really ought to make a single node which can do both atomically */
361 struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
364 d_inode->i_nlink = f->inocache->nlink++;
367 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);