2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: dir.c,v 1.90 2005/11/07 11:14:39 gleixner Exp $
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
18 #include <linux/crc32.h>
19 #include <linux/jffs2.h>
20 #include "jffs2_fs_i.h"
21 #include "jffs2_fs_sb.h"
22 #include <linux/time.h>
25 static int jffs2_readdir (struct file
*, void *, filldir_t
);
27 static int jffs2_create (struct inode
*,struct dentry
*,int,
29 static struct dentry
*jffs2_lookup (struct inode
*,struct dentry
*,
31 static int jffs2_link (struct dentry
*,struct inode
*,struct dentry
*);
32 static int jffs2_unlink (struct inode
*,struct dentry
*);
33 static int jffs2_symlink (struct inode
*,struct dentry
*,const char *);
34 static int jffs2_mkdir (struct inode
*,struct dentry
*,int);
35 static int jffs2_rmdir (struct inode
*,struct dentry
*);
36 static int jffs2_mknod (struct inode
*,struct dentry
*,int,dev_t
);
37 static int jffs2_rename (struct inode
*, struct dentry
*,
38 struct inode
*, struct dentry
*);
40 const struct file_operations jffs2_dir_operations
=
42 .read
= generic_read_dir
,
43 .readdir
= jffs2_readdir
,
49 struct inode_operations jffs2_dir_inode_operations
=
51 .create
= jffs2_create
,
52 .lookup
= jffs2_lookup
,
54 .unlink
= jffs2_unlink
,
55 .symlink
= jffs2_symlink
,
59 .rename
= jffs2_rename
,
60 .permission
= jffs2_permission
,
61 .setattr
= jffs2_setattr
,
62 .setxattr
= jffs2_setxattr
,
63 .getxattr
= jffs2_getxattr
,
64 .listxattr
= jffs2_listxattr
,
65 .removexattr
= jffs2_removexattr
68 /***********************************************************************/
71 /* We keep the dirent list sorted in increasing order of name hash,
72 and we use the same hash function as the dentries. Makes this
75 static struct dentry
*jffs2_lookup(struct inode
*dir_i
, struct dentry
*target
,
78 struct jffs2_inode_info
*dir_f
;
79 struct jffs2_sb_info
*c
;
80 struct jffs2_full_dirent
*fd
= NULL
, *fd_list
;
82 struct inode
*inode
= NULL
;
84 D1(printk(KERN_DEBUG
"jffs2_lookup()\n"));
86 if (target
->d_name
.len
> JFFS2_MAX_NAME_LEN
)
87 return ERR_PTR(-ENAMETOOLONG
);
89 dir_f
= JFFS2_INODE_INFO(dir_i
);
90 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
94 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
95 for (fd_list
= dir_f
->dents
; fd_list
&& fd_list
->nhash
<= target
->d_name
.hash
; fd_list
= fd_list
->next
) {
96 if (fd_list
->nhash
== target
->d_name
.hash
&&
97 (!fd
|| fd_list
->version
> fd
->version
) &&
98 strlen(fd_list
->name
) == target
->d_name
.len
&&
99 !strncmp(fd_list
->name
, target
->d_name
.name
, target
->d_name
.len
)) {
107 inode
= iget(dir_i
->i_sb
, ino
);
109 printk(KERN_WARNING
"iget() failed for ino #%u\n", ino
);
110 return (ERR_PTR(-EIO
));
114 d_add(target
, inode
);
119 /***********************************************************************/
122 static int jffs2_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
124 struct jffs2_inode_info
*f
;
125 struct jffs2_sb_info
*c
;
126 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
127 struct jffs2_full_dirent
*fd
;
128 unsigned long offset
, curofs
;
130 D1(printk(KERN_DEBUG
"jffs2_readdir() for dir_i #%lu\n", filp
->f_path
.dentry
->d_inode
->i_ino
));
132 f
= JFFS2_INODE_INFO(inode
);
133 c
= JFFS2_SB_INFO(inode
->i_sb
);
135 offset
= filp
->f_pos
;
138 D1(printk(KERN_DEBUG
"Dirent 0: \".\", ino #%lu\n", inode
->i_ino
));
139 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
144 unsigned long pino
= parent_ino(filp
->f_path
.dentry
);
145 D1(printk(KERN_DEBUG
"Dirent 1: \"..\", ino #%lu\n", pino
));
146 if (filldir(dirent
, "..", 2, 1, pino
, DT_DIR
) < 0)
153 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
156 /* First loop: curofs = 2; offset = 2 */
157 if (curofs
< offset
) {
158 D2(printk(KERN_DEBUG
"Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
159 fd
->name
, fd
->ino
, fd
->type
, curofs
, offset
));
163 D2(printk(KERN_DEBUG
"Skipping deletion dirent \"%s\"\n", fd
->name
));
167 D2(printk(KERN_DEBUG
"Dirent %ld: \"%s\", ino #%u, type %d\n", offset
, fd
->name
, fd
->ino
, fd
->type
));
168 if (filldir(dirent
, fd
->name
, strlen(fd
->name
), offset
, fd
->ino
, fd
->type
) < 0)
174 filp
->f_pos
= offset
;
178 /***********************************************************************/
181 static int jffs2_create(struct inode
*dir_i
, struct dentry
*dentry
, int mode
,
182 struct nameidata
*nd
)
184 struct jffs2_raw_inode
*ri
;
185 struct jffs2_inode_info
*f
, *dir_f
;
186 struct jffs2_sb_info
*c
;
190 ri
= jffs2_alloc_raw_inode();
194 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
196 D1(printk(KERN_DEBUG
"jffs2_create()\n"));
198 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
201 D1(printk(KERN_DEBUG
"jffs2_new_inode() failed\n"));
202 jffs2_free_raw_inode(ri
);
203 return PTR_ERR(inode
);
206 inode
->i_op
= &jffs2_file_inode_operations
;
207 inode
->i_fop
= &jffs2_file_operations
;
208 inode
->i_mapping
->a_ops
= &jffs2_file_address_operations
;
209 inode
->i_mapping
->nrpages
= 0;
211 f
= JFFS2_INODE_INFO(inode
);
212 dir_f
= JFFS2_INODE_INFO(dir_i
);
214 ret
= jffs2_do_create(c
, dir_f
, f
, ri
,
215 dentry
->d_name
.name
, dentry
->d_name
.len
);
220 ret
= jffs2_init_security(inode
, dir_i
);
223 ret
= jffs2_init_acl(inode
, dir_i
);
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
));
237 make_bad_inode(inode
);
239 jffs2_free_raw_inode(ri
);
243 /***********************************************************************/
246 static int jffs2_unlink(struct inode
*dir_i
, struct dentry
*dentry
)
248 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(dir_i
->i_sb
);
249 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
250 struct jffs2_inode_info
*dead_f
= JFFS2_INODE_INFO(dentry
->d_inode
);
252 uint32_t now
= get_seconds();
254 ret
= jffs2_do_unlink(c
, dir_f
, dentry
->d_name
.name
,
255 dentry
->d_name
.len
, dead_f
, now
);
256 if (dead_f
->inocache
)
257 dentry
->d_inode
->i_nlink
= dead_f
->inocache
->nlink
;
259 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
262 /***********************************************************************/
265 static int jffs2_link (struct dentry
*old_dentry
, struct inode
*dir_i
, struct dentry
*dentry
)
267 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dentry
->d_inode
->i_sb
);
268 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
269 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
274 /* Don't let people make hard links to bad inodes. */
278 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
281 /* XXX: This is ugly */
282 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
283 if (!type
) type
= DT_REG
;
286 ret
= jffs2_do_link(c
, dir_f
, f
->inocache
->ino
, type
, dentry
->d_name
.name
, dentry
->d_name
.len
, now
);
290 old_dentry
->d_inode
->i_nlink
= ++f
->inocache
->nlink
;
292 d_instantiate(dentry
, old_dentry
->d_inode
);
293 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
294 atomic_inc(&old_dentry
->d_inode
->i_count
);
299 /***********************************************************************/
301 static int jffs2_symlink (struct inode
*dir_i
, struct dentry
*dentry
, const char *target
)
303 struct jffs2_inode_info
*f
, *dir_f
;
304 struct jffs2_sb_info
*c
;
306 struct jffs2_raw_inode
*ri
;
307 struct jffs2_raw_dirent
*rd
;
308 struct jffs2_full_dnode
*fn
;
309 struct jffs2_full_dirent
*fd
;
312 int ret
, targetlen
= strlen(target
);
314 /* FIXME: If you care. We'd need to use frags for the target
315 if it grows much more than this */
319 ri
= jffs2_alloc_raw_inode();
324 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
326 /* Try to reserve enough space for both node and dirent.
327 * Just the node will do for now, though
329 namelen
= dentry
->d_name
.len
;
330 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + targetlen
, &alloclen
,
331 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
334 jffs2_free_raw_inode(ri
);
338 inode
= jffs2_new_inode(dir_i
, S_IFLNK
| S_IRWXUGO
, ri
);
341 jffs2_free_raw_inode(ri
);
342 jffs2_complete_reservation(c
);
343 return PTR_ERR(inode
);
346 inode
->i_op
= &jffs2_symlink_inode_operations
;
348 f
= JFFS2_INODE_INFO(inode
);
350 inode
->i_size
= targetlen
;
351 ri
->isize
= ri
->dsize
= ri
->csize
= cpu_to_je32(inode
->i_size
);
352 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + inode
->i_size
);
353 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
355 ri
->compr
= JFFS2_COMPR_NONE
;
356 ri
->data_crc
= cpu_to_je32(crc32(0, target
, targetlen
));
357 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
359 fn
= jffs2_write_dnode(c
, f
, ri
, target
, targetlen
, ALLOC_NORMAL
);
361 jffs2_free_raw_inode(ri
);
364 /* Eeek. Wave bye bye */
366 jffs2_complete_reservation(c
);
367 jffs2_clear_inode(inode
);
371 /* We use f->target field to store the target path. */
372 f
->target
= kmalloc(targetlen
+ 1, GFP_KERNEL
);
374 printk(KERN_WARNING
"Can't allocate %d bytes of memory\n", targetlen
+ 1);
376 jffs2_complete_reservation(c
);
377 jffs2_clear_inode(inode
);
381 memcpy(f
->target
, target
, targetlen
+ 1);
382 D1(printk(KERN_DEBUG
"jffs2_symlink: symlink's target '%s' cached\n", (char *)f
->target
));
384 /* No data here. Only a metadata node, which will be
385 obsoleted by the first data write
390 jffs2_complete_reservation(c
);
392 ret
= jffs2_init_security(inode
, dir_i
);
394 jffs2_clear_inode(inode
);
397 ret
= jffs2_init_acl(inode
, dir_i
);
399 jffs2_clear_inode(inode
);
403 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
404 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
407 jffs2_clear_inode(inode
);
411 rd
= jffs2_alloc_raw_dirent();
413 /* Argh. Now we treat it like a normal delete */
414 jffs2_complete_reservation(c
);
415 jffs2_clear_inode(inode
);
419 dir_f
= JFFS2_INODE_INFO(dir_i
);
422 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
423 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
424 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
425 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
427 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
428 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
429 rd
->ino
= cpu_to_je32(inode
->i_ino
);
430 rd
->mctime
= cpu_to_je32(get_seconds());
433 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
434 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
436 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
439 /* dirent failed to write. Delete the inode normally
440 as if it were the final unlink() */
441 jffs2_complete_reservation(c
);
442 jffs2_free_raw_dirent(rd
);
444 jffs2_clear_inode(inode
);
448 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
450 jffs2_free_raw_dirent(rd
);
452 /* Link the fd into the inode's list, obsoleting an old
454 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
457 jffs2_complete_reservation(c
);
459 d_instantiate(dentry
, inode
);
464 static int jffs2_mkdir (struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
466 struct jffs2_inode_info
*f
, *dir_f
;
467 struct jffs2_sb_info
*c
;
469 struct jffs2_raw_inode
*ri
;
470 struct jffs2_raw_dirent
*rd
;
471 struct jffs2_full_dnode
*fn
;
472 struct jffs2_full_dirent
*fd
;
479 ri
= jffs2_alloc_raw_inode();
483 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
485 /* Try to reserve enough space for both node and dirent.
486 * Just the node will do for now, though
488 namelen
= dentry
->d_name
.len
;
489 ret
= jffs2_reserve_space(c
, sizeof(*ri
), &alloclen
, ALLOC_NORMAL
,
490 JFFS2_SUMMARY_INODE_SIZE
);
493 jffs2_free_raw_inode(ri
);
497 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
500 jffs2_free_raw_inode(ri
);
501 jffs2_complete_reservation(c
);
502 return PTR_ERR(inode
);
505 inode
->i_op
= &jffs2_dir_inode_operations
;
506 inode
->i_fop
= &jffs2_dir_operations
;
507 /* Directories get nlink 2 at start */
510 f
= JFFS2_INODE_INFO(inode
);
512 ri
->data_crc
= cpu_to_je32(0);
513 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
515 fn
= jffs2_write_dnode(c
, f
, ri
, NULL
, 0, ALLOC_NORMAL
);
517 jffs2_free_raw_inode(ri
);
520 /* Eeek. Wave bye bye */
522 jffs2_complete_reservation(c
);
523 jffs2_clear_inode(inode
);
526 /* No data here. Only a metadata node, which will be
527 obsoleted by the first data write
532 jffs2_complete_reservation(c
);
534 ret
= jffs2_init_security(inode
, dir_i
);
536 jffs2_clear_inode(inode
);
539 ret
= jffs2_init_acl(inode
, dir_i
);
541 jffs2_clear_inode(inode
);
545 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
546 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
549 jffs2_clear_inode(inode
);
553 rd
= jffs2_alloc_raw_dirent();
555 /* Argh. Now we treat it like a normal delete */
556 jffs2_complete_reservation(c
);
557 jffs2_clear_inode(inode
);
561 dir_f
= JFFS2_INODE_INFO(dir_i
);
564 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
565 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
566 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
567 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
569 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
570 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
571 rd
->ino
= cpu_to_je32(inode
->i_ino
);
572 rd
->mctime
= cpu_to_je32(get_seconds());
575 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
576 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
578 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
581 /* dirent failed to write. Delete the inode normally
582 as if it were the final unlink() */
583 jffs2_complete_reservation(c
);
584 jffs2_free_raw_dirent(rd
);
586 jffs2_clear_inode(inode
);
590 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
593 jffs2_free_raw_dirent(rd
);
595 /* Link the fd into the inode's list, obsoleting an old
597 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
600 jffs2_complete_reservation(c
);
602 d_instantiate(dentry
, inode
);
606 static int jffs2_rmdir (struct inode
*dir_i
, struct dentry
*dentry
)
608 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(dentry
->d_inode
);
609 struct jffs2_full_dirent
*fd
;
612 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
616 ret
= jffs2_unlink(dir_i
, dentry
);
622 static int jffs2_mknod (struct inode
*dir_i
, struct dentry
*dentry
, int mode
, dev_t rdev
)
624 struct jffs2_inode_info
*f
, *dir_f
;
625 struct jffs2_sb_info
*c
;
627 struct jffs2_raw_inode
*ri
;
628 struct jffs2_raw_dirent
*rd
;
629 struct jffs2_full_dnode
*fn
;
630 struct jffs2_full_dirent
*fd
;
632 union jffs2_device_node dev
;
637 if (!new_valid_dev(rdev
))
640 ri
= jffs2_alloc_raw_inode();
644 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
646 if (S_ISBLK(mode
) || S_ISCHR(mode
))
647 devlen
= jffs2_encode_dev(&dev
, rdev
);
649 /* Try to reserve enough space for both node and dirent.
650 * Just the node will do for now, though
652 namelen
= dentry
->d_name
.len
;
653 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + devlen
, &alloclen
,
654 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
657 jffs2_free_raw_inode(ri
);
661 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
664 jffs2_free_raw_inode(ri
);
665 jffs2_complete_reservation(c
);
666 return PTR_ERR(inode
);
668 inode
->i_op
= &jffs2_file_inode_operations
;
669 init_special_inode(inode
, inode
->i_mode
, rdev
);
671 f
= JFFS2_INODE_INFO(inode
);
673 ri
->dsize
= ri
->csize
= cpu_to_je32(devlen
);
674 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + devlen
);
675 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
677 ri
->compr
= JFFS2_COMPR_NONE
;
678 ri
->data_crc
= cpu_to_je32(crc32(0, &dev
, devlen
));
679 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
681 fn
= jffs2_write_dnode(c
, f
, ri
, (char *)&dev
, devlen
, ALLOC_NORMAL
);
683 jffs2_free_raw_inode(ri
);
686 /* Eeek. Wave bye bye */
688 jffs2_complete_reservation(c
);
689 jffs2_clear_inode(inode
);
692 /* No data here. Only a metadata node, which will be
693 obsoleted by the first data write
698 jffs2_complete_reservation(c
);
700 ret
= jffs2_init_security(inode
, dir_i
);
702 jffs2_clear_inode(inode
);
705 ret
= jffs2_init_acl(inode
, dir_i
);
707 jffs2_clear_inode(inode
);
711 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
712 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
715 jffs2_clear_inode(inode
);
719 rd
= jffs2_alloc_raw_dirent();
721 /* Argh. Now we treat it like a normal delete */
722 jffs2_complete_reservation(c
);
723 jffs2_clear_inode(inode
);
727 dir_f
= JFFS2_INODE_INFO(dir_i
);
730 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
731 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
732 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
733 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
735 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
736 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
737 rd
->ino
= cpu_to_je32(inode
->i_ino
);
738 rd
->mctime
= cpu_to_je32(get_seconds());
741 /* XXX: This is ugly. */
742 rd
->type
= (mode
& S_IFMT
) >> 12;
744 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
745 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
747 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
750 /* dirent failed to write. Delete the inode normally
751 as if it were the final unlink() */
752 jffs2_complete_reservation(c
);
753 jffs2_free_raw_dirent(rd
);
755 jffs2_clear_inode(inode
);
759 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
761 jffs2_free_raw_dirent(rd
);
763 /* Link the fd into the inode's list, obsoleting an old
765 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
768 jffs2_complete_reservation(c
);
770 d_instantiate(dentry
, inode
);
775 static int jffs2_rename (struct inode
*old_dir_i
, struct dentry
*old_dentry
,
776 struct inode
*new_dir_i
, struct dentry
*new_dentry
)
779 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dir_i
->i_sb
);
780 struct jffs2_inode_info
*victim_f
= NULL
;
784 /* The VFS will check for us and prevent trying to rename a
785 * file over a directory and vice versa, but if it's a directory,
786 * the VFS can't check whether the victim is empty. The filesystem
787 * needs to do that for itself.
789 if (new_dentry
->d_inode
) {
790 victim_f
= JFFS2_INODE_INFO(new_dentry
->d_inode
);
791 if (S_ISDIR(new_dentry
->d_inode
->i_mode
)) {
792 struct jffs2_full_dirent
*fd
;
794 down(&victim_f
->sem
);
795 for (fd
= victim_f
->dents
; fd
; fd
= fd
->next
) {
805 /* XXX: We probably ought to alloc enough space for
806 both nodes at the same time. Writing the new link,
807 then getting -ENOSPC, is quite bad :)
810 /* Make a hard link */
812 /* XXX: This is ugly */
813 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
814 if (!type
) type
= DT_REG
;
817 ret
= jffs2_do_link(c
, JFFS2_INODE_INFO(new_dir_i
),
818 old_dentry
->d_inode
->i_ino
, type
,
819 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
, now
);
825 /* There was a victim. Kill it off nicely */
826 drop_nlink(new_dentry
->d_inode
);
827 /* Don't oops if the victim was a dirent pointing to an
828 inode which didn't exist. */
829 if (victim_f
->inocache
) {
830 down(&victim_f
->sem
);
831 victim_f
->inocache
->nlink
--;
836 /* If it was a directory we moved, and there was no victim,
837 increase i_nlink on its new parent */
838 if (S_ISDIR(old_dentry
->d_inode
->i_mode
) && !victim_f
)
839 inc_nlink(new_dir_i
);
841 /* Unlink the original */
842 ret
= jffs2_do_unlink(c
, JFFS2_INODE_INFO(old_dir_i
),
843 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, NULL
, now
);
845 /* We don't touch inode->i_nlink */
848 /* Oh shit. We really ought to make a single node which can do both atomically */
849 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
851 inc_nlink(old_dentry
->d_inode
);
853 f
->inocache
->nlink
++;
856 printk(KERN_NOTICE
"jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret
);
857 /* Might as well let the VFS know */
858 d_instantiate(new_dentry
, old_dentry
->d_inode
);
859 atomic_inc(&old_dentry
->d_inode
->i_count
);
860 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= ITIME(now
);
864 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
865 drop_nlink(old_dir_i
);
867 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= old_dir_i
->i_mtime
= old_dir_i
->i_ctime
= ITIME(now
);