initial commit with v2.6.9
[linux-2.6.9-moxart.git] / fs / jffs2 / dir.c
blob561e37f3d64407aef94d6b4e34ad51e793ff3ccd
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: dir.c,v 1.82 2003/10/11 11:47:23 dwmw2 Exp $
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/fs.h>
18 #include <linux/crc32.h>
19 #include <linux/jffs2.h>
20 #include <linux/jffs2_fs_i.h>
21 #include <linux/jffs2_fs_sb.h>
22 #include <linux/time.h>
23 #include "nodelist.h"
25 /* Urgh. Please tell me there's a nicer way of doing these. */
26 #include <linux/version.h>
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,48)
28 typedef int mknod_arg_t;
29 #define NAMEI_COMPAT(x) ((void *)x)
30 #else
31 typedef dev_t mknod_arg_t;
32 #define NAMEI_COMPAT(x) (x)
33 #endif
35 static int jffs2_readdir (struct file *, void *, filldir_t);
37 static int jffs2_create (struct inode *,struct dentry *,int,
38 struct nameidata *);
39 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
40 struct nameidata *);
41 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
42 static int jffs2_unlink (struct inode *,struct dentry *);
43 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
44 static int jffs2_mkdir (struct inode *,struct dentry *,int);
45 static int jffs2_rmdir (struct inode *,struct dentry *);
46 static int jffs2_mknod (struct inode *,struct dentry *,int,mknod_arg_t);
47 static int jffs2_rename (struct inode *, struct dentry *,
48 struct inode *, struct dentry *);
50 struct file_operations jffs2_dir_operations =
52 .read = generic_read_dir,
53 .readdir = jffs2_readdir,
54 .ioctl = jffs2_ioctl,
55 .fsync = jffs2_fsync
59 struct inode_operations jffs2_dir_inode_operations =
61 .create = NAMEI_COMPAT(jffs2_create),
62 .lookup = NAMEI_COMPAT(jffs2_lookup),
63 .link = jffs2_link,
64 .unlink = jffs2_unlink,
65 .symlink = jffs2_symlink,
66 .mkdir = jffs2_mkdir,
67 .rmdir = jffs2_rmdir,
68 .mknod = jffs2_mknod,
69 .rename = jffs2_rename,
70 .setattr = jffs2_setattr,
73 /***********************************************************************/
76 /* We keep the dirent list sorted in increasing order of name hash,
77 and we use the same hash function as the dentries. Makes this
78 nice and simple
80 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
81 struct nameidata *nd)
83 struct jffs2_inode_info *dir_f;
84 struct jffs2_sb_info *c;
85 struct jffs2_full_dirent *fd = NULL, *fd_list;
86 uint32_t ino = 0;
87 struct inode *inode = NULL;
89 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
91 dir_f = JFFS2_INODE_INFO(dir_i);
92 c = JFFS2_SB_INFO(dir_i->i_sb);
94 down(&dir_f->sem);
96 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
97 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
98 if (fd_list->nhash == target->d_name.hash &&
99 (!fd || fd_list->version > fd->version) &&
100 strlen(fd_list->name) == target->d_name.len &&
101 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
102 fd = fd_list;
105 if (fd)
106 ino = fd->ino;
107 up(&dir_f->sem);
108 if (ino) {
109 inode = iget(dir_i->i_sb, ino);
110 if (!inode) {
111 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
112 return (ERR_PTR(-EIO));
116 d_add(target, inode);
118 return NULL;
121 /***********************************************************************/
124 static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
126 struct jffs2_inode_info *f;
127 struct jffs2_sb_info *c;
128 struct inode *inode = filp->f_dentry->d_inode;
129 struct jffs2_full_dirent *fd;
130 unsigned long offset, curofs;
132 D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
134 f = JFFS2_INODE_INFO(inode);
135 c = JFFS2_SB_INFO(inode->i_sb);
137 offset = filp->f_pos;
139 if (offset == 0) {
140 D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
141 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
142 goto out;
143 offset++;
145 if (offset == 1) {
146 unsigned long pino = parent_ino(filp->f_dentry);
147 D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
148 if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
149 goto out;
150 offset++;
153 curofs=1;
154 down(&f->sem);
155 for (fd = f->dents; fd; fd = fd->next) {
157 curofs++;
158 /* First loop: curofs = 2; offset = 2 */
159 if (curofs < offset) {
160 D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
161 fd->name, fd->ino, fd->type, curofs, offset));
162 continue;
164 if (!fd->ino) {
165 D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
166 offset++;
167 continue;
169 D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
170 if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
171 break;
172 offset++;
174 up(&f->sem);
175 out:
176 filp->f_pos = offset;
177 return 0;
180 /***********************************************************************/
183 static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
184 struct nameidata *nd)
186 struct jffs2_raw_inode *ri;
187 struct jffs2_inode_info *f, *dir_f;
188 struct jffs2_sb_info *c;
189 struct inode *inode;
190 int ret;
192 ri = jffs2_alloc_raw_inode();
193 if (!ri)
194 return -ENOMEM;
196 c = JFFS2_SB_INFO(dir_i->i_sb);
198 D1(printk(KERN_DEBUG "jffs2_create()\n"));
200 inode = jffs2_new_inode(dir_i, mode, ri);
202 if (IS_ERR(inode)) {
203 D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
204 jffs2_free_raw_inode(ri);
205 return PTR_ERR(inode);
208 inode->i_op = &jffs2_file_inode_operations;
209 inode->i_fop = &jffs2_file_operations;
210 inode->i_mapping->a_ops = &jffs2_file_address_operations;
211 inode->i_mapping->nrpages = 0;
213 f = JFFS2_INODE_INFO(inode);
214 dir_f = JFFS2_INODE_INFO(dir_i);
216 ret = jffs2_do_create(c, dir_f, f, ri,
217 dentry->d_name.name, dentry->d_name.len);
219 if (ret) {
220 jffs2_clear_inode(inode);
221 make_bad_inode(inode);
222 iput(inode);
223 jffs2_free_raw_inode(ri);
224 return ret;
227 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
229 jffs2_free_raw_inode(ri);
230 d_instantiate(dentry, inode);
232 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
233 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
234 return 0;
237 /***********************************************************************/
240 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
242 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
243 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
244 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
245 int ret;
247 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
248 dentry->d_name.len, dead_f);
249 if (dead_f->inocache)
250 dentry->d_inode->i_nlink = dead_f->inocache->nlink;
251 return ret;
253 /***********************************************************************/
256 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
258 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
259 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
260 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
261 int ret;
262 uint8_t type;
264 /* Don't let people make hard links to bad inodes. */
265 if (!f->inocache)
266 return -EIO;
268 if (S_ISDIR(old_dentry->d_inode->i_mode))
269 return -EPERM;
271 /* XXX: This is ugly */
272 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
273 if (!type) type = DT_REG;
275 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
277 if (!ret) {
278 down(&f->sem);
279 old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
280 up(&f->sem);
281 d_instantiate(dentry, old_dentry->d_inode);
282 atomic_inc(&old_dentry->d_inode->i_count);
284 return ret;
287 /***********************************************************************/
289 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
291 struct jffs2_inode_info *f, *dir_f;
292 struct jffs2_sb_info *c;
293 struct inode *inode;
294 struct jffs2_raw_inode *ri;
295 struct jffs2_raw_dirent *rd;
296 struct jffs2_full_dnode *fn;
297 struct jffs2_full_dirent *fd;
298 int namelen;
299 uint32_t alloclen, phys_ofs;
300 int ret;
302 /* FIXME: If you care. We'd need to use frags for the target
303 if it grows much more than this */
304 if (strlen(target) > 254)
305 return -EINVAL;
307 ri = jffs2_alloc_raw_inode();
309 if (!ri)
310 return -ENOMEM;
312 c = JFFS2_SB_INFO(dir_i->i_sb);
314 /* Try to reserve enough space for both node and dirent.
315 * Just the node will do for now, though
317 namelen = dentry->d_name.len;
318 ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
320 if (ret) {
321 jffs2_free_raw_inode(ri);
322 return ret;
325 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
327 if (IS_ERR(inode)) {
328 jffs2_free_raw_inode(ri);
329 jffs2_complete_reservation(c);
330 return PTR_ERR(inode);
333 inode->i_op = &jffs2_symlink_inode_operations;
335 f = JFFS2_INODE_INFO(inode);
337 inode->i_size = strlen(target);
338 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
339 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
340 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
342 ri->compr = JFFS2_COMPR_NONE;
343 ri->data_crc = cpu_to_je32(crc32(0, target, strlen(target)));
344 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
346 fn = jffs2_write_dnode(c, f, ri, target, strlen(target), phys_ofs, ALLOC_NORMAL);
348 jffs2_free_raw_inode(ri);
350 if (IS_ERR(fn)) {
351 /* Eeek. Wave bye bye */
352 up(&f->sem);
353 jffs2_complete_reservation(c);
354 jffs2_clear_inode(inode);
355 return PTR_ERR(fn);
357 /* No data here. Only a metadata node, which will be
358 obsoleted by the first data write
360 f->metadata = fn;
361 up(&f->sem);
363 jffs2_complete_reservation(c);
364 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
365 if (ret) {
366 /* Eep. */
367 jffs2_clear_inode(inode);
368 return ret;
371 rd = jffs2_alloc_raw_dirent();
372 if (!rd) {
373 /* Argh. Now we treat it like a normal delete */
374 jffs2_complete_reservation(c);
375 jffs2_clear_inode(inode);
376 return -ENOMEM;
379 dir_f = JFFS2_INODE_INFO(dir_i);
380 down(&dir_f->sem);
382 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
383 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
384 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
385 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
387 rd->pino = cpu_to_je32(dir_i->i_ino);
388 rd->version = cpu_to_je32(++dir_f->highest_version);
389 rd->ino = cpu_to_je32(inode->i_ino);
390 rd->mctime = cpu_to_je32(get_seconds());
391 rd->nsize = namelen;
392 rd->type = DT_LNK;
393 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
394 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
396 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
398 if (IS_ERR(fd)) {
399 /* dirent failed to write. Delete the inode normally
400 as if it were the final unlink() */
401 jffs2_complete_reservation(c);
402 jffs2_free_raw_dirent(rd);
403 up(&dir_f->sem);
404 jffs2_clear_inode(inode);
405 return PTR_ERR(fd);
408 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
410 jffs2_free_raw_dirent(rd);
412 /* Link the fd into the inode's list, obsoleting an old
413 one if necessary. */
414 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
416 up(&dir_f->sem);
417 jffs2_complete_reservation(c);
419 d_instantiate(dentry, inode);
420 return 0;
424 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
426 struct jffs2_inode_info *f, *dir_f;
427 struct jffs2_sb_info *c;
428 struct inode *inode;
429 struct jffs2_raw_inode *ri;
430 struct jffs2_raw_dirent *rd;
431 struct jffs2_full_dnode *fn;
432 struct jffs2_full_dirent *fd;
433 int namelen;
434 uint32_t alloclen, phys_ofs;
435 int ret;
437 mode |= S_IFDIR;
439 ri = jffs2_alloc_raw_inode();
440 if (!ri)
441 return -ENOMEM;
443 c = JFFS2_SB_INFO(dir_i->i_sb);
445 /* Try to reserve enough space for both node and dirent.
446 * Just the node will do for now, though
448 namelen = dentry->d_name.len;
449 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
451 if (ret) {
452 jffs2_free_raw_inode(ri);
453 return ret;
456 inode = jffs2_new_inode(dir_i, mode, ri);
458 if (IS_ERR(inode)) {
459 jffs2_free_raw_inode(ri);
460 jffs2_complete_reservation(c);
461 return PTR_ERR(inode);
464 inode->i_op = &jffs2_dir_inode_operations;
465 inode->i_fop = &jffs2_dir_operations;
466 /* Directories get nlink 2 at start */
467 inode->i_nlink = 2;
469 f = JFFS2_INODE_INFO(inode);
471 ri->data_crc = cpu_to_je32(0);
472 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
474 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
476 jffs2_free_raw_inode(ri);
478 if (IS_ERR(fn)) {
479 /* Eeek. Wave bye bye */
480 up(&f->sem);
481 jffs2_complete_reservation(c);
482 jffs2_clear_inode(inode);
483 return PTR_ERR(fn);
485 /* No data here. Only a metadata node, which will be
486 obsoleted by the first data write
488 f->metadata = fn;
489 up(&f->sem);
491 jffs2_complete_reservation(c);
492 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
493 if (ret) {
494 /* Eep. */
495 jffs2_clear_inode(inode);
496 return ret;
499 rd = jffs2_alloc_raw_dirent();
500 if (!rd) {
501 /* Argh. Now we treat it like a normal delete */
502 jffs2_complete_reservation(c);
503 jffs2_clear_inode(inode);
504 return -ENOMEM;
507 dir_f = JFFS2_INODE_INFO(dir_i);
508 down(&dir_f->sem);
510 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
511 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
512 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
513 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
515 rd->pino = cpu_to_je32(dir_i->i_ino);
516 rd->version = cpu_to_je32(++dir_f->highest_version);
517 rd->ino = cpu_to_je32(inode->i_ino);
518 rd->mctime = cpu_to_je32(get_seconds());
519 rd->nsize = namelen;
520 rd->type = DT_DIR;
521 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
522 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
524 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
526 if (IS_ERR(fd)) {
527 /* dirent failed to write. Delete the inode normally
528 as if it were the final unlink() */
529 jffs2_complete_reservation(c);
530 jffs2_free_raw_dirent(rd);
531 up(&dir_f->sem);
532 jffs2_clear_inode(inode);
533 return PTR_ERR(fd);
536 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
537 dir_i->i_nlink++;
539 jffs2_free_raw_dirent(rd);
541 /* Link the fd into the inode's list, obsoleting an old
542 one if necessary. */
543 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
545 up(&dir_f->sem);
546 jffs2_complete_reservation(c);
548 d_instantiate(dentry, inode);
549 return 0;
552 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
554 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
555 struct jffs2_full_dirent *fd;
556 int ret;
558 for (fd = f->dents ; fd; fd = fd->next) {
559 if (fd->ino)
560 return -ENOTEMPTY;
562 ret = jffs2_unlink(dir_i, dentry);
563 if (!ret)
564 dir_i->i_nlink--;
565 return ret;
568 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mknod_arg_t rdev)
570 struct jffs2_inode_info *f, *dir_f;
571 struct jffs2_sb_info *c;
572 struct inode *inode;
573 struct jffs2_raw_inode *ri;
574 struct jffs2_raw_dirent *rd;
575 struct jffs2_full_dnode *fn;
576 struct jffs2_full_dirent *fd;
577 int namelen;
578 jint16_t dev;
579 int devlen = 0;
580 uint32_t alloclen, phys_ofs;
581 int ret;
583 if (!old_valid_dev(rdev))
584 return -EINVAL;
586 ri = jffs2_alloc_raw_inode();
587 if (!ri)
588 return -ENOMEM;
590 c = JFFS2_SB_INFO(dir_i->i_sb);
592 if (S_ISBLK(mode) || S_ISCHR(mode)) {
593 dev = cpu_to_je16(old_encode_dev(rdev));
594 devlen = sizeof(dev);
597 /* Try to reserve enough space for both node and dirent.
598 * Just the node will do for now, though
600 namelen = dentry->d_name.len;
601 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
603 if (ret) {
604 jffs2_free_raw_inode(ri);
605 return ret;
608 inode = jffs2_new_inode(dir_i, mode, ri);
610 if (IS_ERR(inode)) {
611 jffs2_free_raw_inode(ri);
612 jffs2_complete_reservation(c);
613 return PTR_ERR(inode);
615 inode->i_op = &jffs2_file_inode_operations;
616 init_special_inode(inode, inode->i_mode, rdev);
618 f = JFFS2_INODE_INFO(inode);
620 ri->dsize = ri->csize = cpu_to_je32(devlen);
621 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
622 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
624 ri->compr = JFFS2_COMPR_NONE;
625 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
626 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
628 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
630 jffs2_free_raw_inode(ri);
632 if (IS_ERR(fn)) {
633 /* Eeek. Wave bye bye */
634 up(&f->sem);
635 jffs2_complete_reservation(c);
636 jffs2_clear_inode(inode);
637 return PTR_ERR(fn);
639 /* No data here. Only a metadata node, which will be
640 obsoleted by the first data write
642 f->metadata = fn;
643 up(&f->sem);
645 jffs2_complete_reservation(c);
646 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
647 if (ret) {
648 /* Eep. */
649 jffs2_clear_inode(inode);
650 return ret;
653 rd = jffs2_alloc_raw_dirent();
654 if (!rd) {
655 /* Argh. Now we treat it like a normal delete */
656 jffs2_complete_reservation(c);
657 jffs2_clear_inode(inode);
658 return -ENOMEM;
661 dir_f = JFFS2_INODE_INFO(dir_i);
662 down(&dir_f->sem);
664 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
665 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
666 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
667 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
669 rd->pino = cpu_to_je32(dir_i->i_ino);
670 rd->version = cpu_to_je32(++dir_f->highest_version);
671 rd->ino = cpu_to_je32(inode->i_ino);
672 rd->mctime = cpu_to_je32(get_seconds());
673 rd->nsize = namelen;
675 /* XXX: This is ugly. */
676 rd->type = (mode & S_IFMT) >> 12;
678 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
679 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
681 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
683 if (IS_ERR(fd)) {
684 /* dirent failed to write. Delete the inode normally
685 as if it were the final unlink() */
686 jffs2_complete_reservation(c);
687 jffs2_free_raw_dirent(rd);
688 up(&dir_f->sem);
689 jffs2_clear_inode(inode);
690 return PTR_ERR(fd);
693 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
695 jffs2_free_raw_dirent(rd);
697 /* Link the fd into the inode's list, obsoleting an old
698 one if necessary. */
699 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
701 up(&dir_f->sem);
702 jffs2_complete_reservation(c);
704 d_instantiate(dentry, inode);
706 return 0;
709 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
710 struct inode *new_dir_i, struct dentry *new_dentry)
712 int ret;
713 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
714 struct jffs2_inode_info *victim_f = NULL;
715 uint8_t type;
717 /* The VFS will check for us and prevent trying to rename a
718 * file over a directory and vice versa, but if it's a directory,
719 * the VFS can't check whether the victim is empty. The filesystem
720 * needs to do that for itself.
722 if (new_dentry->d_inode) {
723 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
724 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
725 struct jffs2_full_dirent *fd;
727 down(&victim_f->sem);
728 for (fd = victim_f->dents; fd; fd = fd->next) {
729 if (fd->ino) {
730 up(&victim_f->sem);
731 return -ENOTEMPTY;
734 up(&victim_f->sem);
738 /* XXX: We probably ought to alloc enough space for
739 both nodes at the same time. Writing the new link,
740 then getting -ENOSPC, is quite bad :)
743 /* Make a hard link */
745 /* XXX: This is ugly */
746 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
747 if (!type) type = DT_REG;
749 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
750 old_dentry->d_inode->i_ino, type,
751 new_dentry->d_name.name, new_dentry->d_name.len);
753 if (ret)
754 return ret;
756 if (victim_f) {
757 /* There was a victim. Kill it off nicely */
758 new_dentry->d_inode->i_nlink--;
759 /* Don't oops if the victim was a dirent pointing to an
760 inode which didn't exist. */
761 if (victim_f->inocache) {
762 down(&victim_f->sem);
763 victim_f->inocache->nlink--;
764 up(&victim_f->sem);
768 /* If it was a directory we moved, and there was no victim,
769 increase i_nlink on its new parent */
770 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
771 new_dir_i->i_nlink++;
773 /* Unlink the original */
774 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
775 old_dentry->d_name.name, old_dentry->d_name.len, NULL);
777 /* We don't touch inode->i_nlink */
779 if (ret) {
780 /* Oh shit. We really ought to make a single node which can do both atomically */
781 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
782 down(&f->sem);
783 old_dentry->d_inode->i_nlink++;
784 if (f->inocache)
785 f->inocache->nlink++;
786 up(&f->sem);
788 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
789 /* Might as well let the VFS know */
790 d_instantiate(new_dentry, old_dentry->d_inode);
791 atomic_inc(&old_dentry->d_inode->i_count);
792 return ret;
795 if (S_ISDIR(old_dentry->d_inode->i_mode))
796 old_dir_i->i_nlink--;
798 return 0;