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>
17 #include <linux/crc32.h>
18 #include <linux/jffs2.h>
19 #include "jffs2_fs_i.h"
20 #include "jffs2_fs_sb.h"
21 #include <linux/time.h>
24 static int jffs2_readdir (struct file
*, void *, filldir_t
);
26 static int jffs2_create (struct inode
*,struct dentry
*,int,
28 static struct dentry
*jffs2_lookup (struct inode
*,struct dentry
*,
30 static int jffs2_link (struct dentry
*,struct inode
*,struct dentry
*);
31 static int jffs2_unlink (struct inode
*,struct dentry
*);
32 static int jffs2_symlink (struct inode
*,struct dentry
*,const char *);
33 static int jffs2_mkdir (struct inode
*,struct dentry
*,int);
34 static int jffs2_rmdir (struct inode
*,struct dentry
*);
35 static int jffs2_mknod (struct inode
*,struct dentry
*,int,dev_t
);
36 static int jffs2_rename (struct inode
*, struct dentry
*,
37 struct inode
*, struct dentry
*);
39 const struct file_operations jffs2_dir_operations
=
41 .read
= generic_read_dir
,
42 .readdir
= jffs2_readdir
,
48 const struct inode_operations jffs2_dir_inode_operations
=
50 .create
= jffs2_create
,
51 .lookup
= jffs2_lookup
,
53 .unlink
= jffs2_unlink
,
54 .symlink
= jffs2_symlink
,
58 .rename
= jffs2_rename
,
59 .permission
= jffs2_permission
,
60 .setattr
= jffs2_setattr
,
61 .setxattr
= jffs2_setxattr
,
62 .getxattr
= jffs2_getxattr
,
63 .listxattr
= jffs2_listxattr
,
64 .removexattr
= jffs2_removexattr
67 /***********************************************************************/
70 /* We keep the dirent list sorted in increasing order of name hash,
71 and we use the same hash function as the dentries. Makes this
74 static struct dentry
*jffs2_lookup(struct inode
*dir_i
, struct dentry
*target
,
77 struct jffs2_inode_info
*dir_f
;
78 struct jffs2_sb_info
*c
;
79 struct jffs2_full_dirent
*fd
= NULL
, *fd_list
;
81 struct inode
*inode
= NULL
;
83 D1(printk(KERN_DEBUG
"jffs2_lookup()\n"));
85 if (target
->d_name
.len
> JFFS2_MAX_NAME_LEN
)
86 return ERR_PTR(-ENAMETOOLONG
);
88 dir_f
= JFFS2_INODE_INFO(dir_i
);
89 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
93 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
94 for (fd_list
= dir_f
->dents
; fd_list
&& fd_list
->nhash
<= target
->d_name
.hash
; fd_list
= fd_list
->next
) {
95 if (fd_list
->nhash
== target
->d_name
.hash
&&
96 (!fd
|| fd_list
->version
> fd
->version
) &&
97 strlen(fd_list
->name
) == target
->d_name
.len
&&
98 !strncmp(fd_list
->name
, target
->d_name
.name
, target
->d_name
.len
)) {
106 inode
= iget(dir_i
->i_sb
, ino
);
108 printk(KERN_WARNING
"iget() failed for ino #%u\n", ino
);
109 return (ERR_PTR(-EIO
));
113 d_add(target
, inode
);
118 /***********************************************************************/
121 static int jffs2_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
123 struct jffs2_inode_info
*f
;
124 struct jffs2_sb_info
*c
;
125 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
126 struct jffs2_full_dirent
*fd
;
127 unsigned long offset
, curofs
;
129 D1(printk(KERN_DEBUG
"jffs2_readdir() for dir_i #%lu\n", filp
->f_path
.dentry
->d_inode
->i_ino
));
131 f
= JFFS2_INODE_INFO(inode
);
132 c
= JFFS2_SB_INFO(inode
->i_sb
);
134 offset
= filp
->f_pos
;
137 D1(printk(KERN_DEBUG
"Dirent 0: \".\", ino #%lu\n", inode
->i_ino
));
138 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
143 unsigned long pino
= parent_ino(filp
->f_path
.dentry
);
144 D1(printk(KERN_DEBUG
"Dirent 1: \"..\", ino #%lu\n", pino
));
145 if (filldir(dirent
, "..", 2, 1, pino
, DT_DIR
) < 0)
152 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
155 /* First loop: curofs = 2; offset = 2 */
156 if (curofs
< offset
) {
157 D2(printk(KERN_DEBUG
"Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
158 fd
->name
, fd
->ino
, fd
->type
, curofs
, offset
));
162 D2(printk(KERN_DEBUG
"Skipping deletion dirent \"%s\"\n", fd
->name
));
166 D2(printk(KERN_DEBUG
"Dirent %ld: \"%s\", ino #%u, type %d\n", offset
, fd
->name
, fd
->ino
, fd
->type
));
167 if (filldir(dirent
, fd
->name
, strlen(fd
->name
), offset
, fd
->ino
, fd
->type
) < 0)
173 filp
->f_pos
= offset
;
177 /***********************************************************************/
180 static int jffs2_create(struct inode
*dir_i
, struct dentry
*dentry
, int mode
,
181 struct nameidata
*nd
)
183 struct jffs2_raw_inode
*ri
;
184 struct jffs2_inode_info
*f
, *dir_f
;
185 struct jffs2_sb_info
*c
;
189 ri
= jffs2_alloc_raw_inode();
193 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
195 D1(printk(KERN_DEBUG
"jffs2_create()\n"));
197 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
200 D1(printk(KERN_DEBUG
"jffs2_new_inode() failed\n"));
201 jffs2_free_raw_inode(ri
);
202 return PTR_ERR(inode
);
205 inode
->i_op
= &jffs2_file_inode_operations
;
206 inode
->i_fop
= &jffs2_file_operations
;
207 inode
->i_mapping
->a_ops
= &jffs2_file_address_operations
;
208 inode
->i_mapping
->nrpages
= 0;
210 f
= JFFS2_INODE_INFO(inode
);
211 dir_f
= JFFS2_INODE_INFO(dir_i
);
213 ret
= jffs2_do_create(c
, dir_f
, f
, ri
,
214 dentry
->d_name
.name
, dentry
->d_name
.len
);
219 ret
= jffs2_init_security(inode
, dir_i
);
222 ret
= jffs2_init_acl(inode
, dir_i
);
226 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(ri
->ctime
));
228 jffs2_free_raw_inode(ri
);
229 d_instantiate(dentry
, inode
);
231 D1(printk(KERN_DEBUG
"jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
232 inode
->i_ino
, inode
->i_mode
, inode
->i_nlink
, f
->inocache
->nlink
, inode
->i_mapping
->nrpages
));
236 make_bad_inode(inode
);
238 jffs2_free_raw_inode(ri
);
242 /***********************************************************************/
245 static int jffs2_unlink(struct inode
*dir_i
, struct dentry
*dentry
)
247 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(dir_i
->i_sb
);
248 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
249 struct jffs2_inode_info
*dead_f
= JFFS2_INODE_INFO(dentry
->d_inode
);
251 uint32_t now
= get_seconds();
253 ret
= jffs2_do_unlink(c
, dir_f
, dentry
->d_name
.name
,
254 dentry
->d_name
.len
, dead_f
, now
);
255 if (dead_f
->inocache
)
256 dentry
->d_inode
->i_nlink
= dead_f
->inocache
->nlink
;
258 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
261 /***********************************************************************/
264 static int jffs2_link (struct dentry
*old_dentry
, struct inode
*dir_i
, struct dentry
*dentry
)
266 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dentry
->d_inode
->i_sb
);
267 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
268 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
273 /* Don't let people make hard links to bad inodes. */
277 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
280 /* XXX: This is ugly */
281 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
282 if (!type
) type
= DT_REG
;
285 ret
= jffs2_do_link(c
, dir_f
, f
->inocache
->ino
, type
, dentry
->d_name
.name
, dentry
->d_name
.len
, now
);
289 old_dentry
->d_inode
->i_nlink
= ++f
->inocache
->nlink
;
291 d_instantiate(dentry
, old_dentry
->d_inode
);
292 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
293 atomic_inc(&old_dentry
->d_inode
->i_count
);
298 /***********************************************************************/
300 static int jffs2_symlink (struct inode
*dir_i
, struct dentry
*dentry
, const char *target
)
302 struct jffs2_inode_info
*f
, *dir_f
;
303 struct jffs2_sb_info
*c
;
305 struct jffs2_raw_inode
*ri
;
306 struct jffs2_raw_dirent
*rd
;
307 struct jffs2_full_dnode
*fn
;
308 struct jffs2_full_dirent
*fd
;
311 int ret
, targetlen
= strlen(target
);
313 /* FIXME: If you care. We'd need to use frags for the target
314 if it grows much more than this */
318 ri
= jffs2_alloc_raw_inode();
323 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
325 /* Try to reserve enough space for both node and dirent.
326 * Just the node will do for now, though
328 namelen
= dentry
->d_name
.len
;
329 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + targetlen
, &alloclen
,
330 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
333 jffs2_free_raw_inode(ri
);
337 inode
= jffs2_new_inode(dir_i
, S_IFLNK
| S_IRWXUGO
, ri
);
340 jffs2_free_raw_inode(ri
);
341 jffs2_complete_reservation(c
);
342 return PTR_ERR(inode
);
345 inode
->i_op
= &jffs2_symlink_inode_operations
;
347 f
= JFFS2_INODE_INFO(inode
);
349 inode
->i_size
= targetlen
;
350 ri
->isize
= ri
->dsize
= ri
->csize
= cpu_to_je32(inode
->i_size
);
351 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + inode
->i_size
);
352 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
354 ri
->compr
= JFFS2_COMPR_NONE
;
355 ri
->data_crc
= cpu_to_je32(crc32(0, target
, targetlen
));
356 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
358 fn
= jffs2_write_dnode(c
, f
, ri
, target
, targetlen
, ALLOC_NORMAL
);
360 jffs2_free_raw_inode(ri
);
363 /* Eeek. Wave bye bye */
365 jffs2_complete_reservation(c
);
366 jffs2_clear_inode(inode
);
370 /* We use f->target field to store the target path. */
371 f
->target
= kmalloc(targetlen
+ 1, GFP_KERNEL
);
373 printk(KERN_WARNING
"Can't allocate %d bytes of memory\n", targetlen
+ 1);
375 jffs2_complete_reservation(c
);
376 jffs2_clear_inode(inode
);
380 memcpy(f
->target
, target
, targetlen
+ 1);
381 D1(printk(KERN_DEBUG
"jffs2_symlink: symlink's target '%s' cached\n", (char *)f
->target
));
383 /* No data here. Only a metadata node, which will be
384 obsoleted by the first data write
389 jffs2_complete_reservation(c
);
391 ret
= jffs2_init_security(inode
, dir_i
);
393 jffs2_clear_inode(inode
);
396 ret
= jffs2_init_acl(inode
, dir_i
);
398 jffs2_clear_inode(inode
);
402 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
403 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
406 jffs2_clear_inode(inode
);
410 rd
= jffs2_alloc_raw_dirent();
412 /* Argh. Now we treat it like a normal delete */
413 jffs2_complete_reservation(c
);
414 jffs2_clear_inode(inode
);
418 dir_f
= JFFS2_INODE_INFO(dir_i
);
421 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
422 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
423 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
424 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
426 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
427 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
428 rd
->ino
= cpu_to_je32(inode
->i_ino
);
429 rd
->mctime
= cpu_to_je32(get_seconds());
432 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
433 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
435 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
438 /* dirent failed to write. Delete the inode normally
439 as if it were the final unlink() */
440 jffs2_complete_reservation(c
);
441 jffs2_free_raw_dirent(rd
);
443 jffs2_clear_inode(inode
);
447 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
449 jffs2_free_raw_dirent(rd
);
451 /* Link the fd into the inode's list, obsoleting an old
453 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
456 jffs2_complete_reservation(c
);
458 d_instantiate(dentry
, inode
);
463 static int jffs2_mkdir (struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
465 struct jffs2_inode_info
*f
, *dir_f
;
466 struct jffs2_sb_info
*c
;
468 struct jffs2_raw_inode
*ri
;
469 struct jffs2_raw_dirent
*rd
;
470 struct jffs2_full_dnode
*fn
;
471 struct jffs2_full_dirent
*fd
;
478 ri
= jffs2_alloc_raw_inode();
482 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
484 /* Try to reserve enough space for both node and dirent.
485 * Just the node will do for now, though
487 namelen
= dentry
->d_name
.len
;
488 ret
= jffs2_reserve_space(c
, sizeof(*ri
), &alloclen
, ALLOC_NORMAL
,
489 JFFS2_SUMMARY_INODE_SIZE
);
492 jffs2_free_raw_inode(ri
);
496 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
499 jffs2_free_raw_inode(ri
);
500 jffs2_complete_reservation(c
);
501 return PTR_ERR(inode
);
504 inode
->i_op
= &jffs2_dir_inode_operations
;
505 inode
->i_fop
= &jffs2_dir_operations
;
506 /* Directories get nlink 2 at start */
509 f
= JFFS2_INODE_INFO(inode
);
511 ri
->data_crc
= cpu_to_je32(0);
512 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
514 fn
= jffs2_write_dnode(c
, f
, ri
, NULL
, 0, ALLOC_NORMAL
);
516 jffs2_free_raw_inode(ri
);
519 /* Eeek. Wave bye bye */
521 jffs2_complete_reservation(c
);
522 jffs2_clear_inode(inode
);
525 /* No data here. Only a metadata node, which will be
526 obsoleted by the first data write
531 jffs2_complete_reservation(c
);
533 ret
= jffs2_init_security(inode
, dir_i
);
535 jffs2_clear_inode(inode
);
538 ret
= jffs2_init_acl(inode
, dir_i
);
540 jffs2_clear_inode(inode
);
544 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
545 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
548 jffs2_clear_inode(inode
);
552 rd
= jffs2_alloc_raw_dirent();
554 /* Argh. Now we treat it like a normal delete */
555 jffs2_complete_reservation(c
);
556 jffs2_clear_inode(inode
);
560 dir_f
= JFFS2_INODE_INFO(dir_i
);
563 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
564 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
565 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
566 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
568 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
569 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
570 rd
->ino
= cpu_to_je32(inode
->i_ino
);
571 rd
->mctime
= cpu_to_je32(get_seconds());
574 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
575 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
577 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
580 /* dirent failed to write. Delete the inode normally
581 as if it were the final unlink() */
582 jffs2_complete_reservation(c
);
583 jffs2_free_raw_dirent(rd
);
585 jffs2_clear_inode(inode
);
589 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
592 jffs2_free_raw_dirent(rd
);
594 /* Link the fd into the inode's list, obsoleting an old
596 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
599 jffs2_complete_reservation(c
);
601 d_instantiate(dentry
, inode
);
605 static int jffs2_rmdir (struct inode
*dir_i
, struct dentry
*dentry
)
607 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(dentry
->d_inode
);
608 struct jffs2_full_dirent
*fd
;
611 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
615 ret
= jffs2_unlink(dir_i
, dentry
);
621 static int jffs2_mknod (struct inode
*dir_i
, struct dentry
*dentry
, int mode
, dev_t rdev
)
623 struct jffs2_inode_info
*f
, *dir_f
;
624 struct jffs2_sb_info
*c
;
626 struct jffs2_raw_inode
*ri
;
627 struct jffs2_raw_dirent
*rd
;
628 struct jffs2_full_dnode
*fn
;
629 struct jffs2_full_dirent
*fd
;
631 union jffs2_device_node dev
;
636 if (!new_valid_dev(rdev
))
639 ri
= jffs2_alloc_raw_inode();
643 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
645 if (S_ISBLK(mode
) || S_ISCHR(mode
))
646 devlen
= jffs2_encode_dev(&dev
, rdev
);
648 /* Try to reserve enough space for both node and dirent.
649 * Just the node will do for now, though
651 namelen
= dentry
->d_name
.len
;
652 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + devlen
, &alloclen
,
653 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
656 jffs2_free_raw_inode(ri
);
660 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
663 jffs2_free_raw_inode(ri
);
664 jffs2_complete_reservation(c
);
665 return PTR_ERR(inode
);
667 inode
->i_op
= &jffs2_file_inode_operations
;
668 init_special_inode(inode
, inode
->i_mode
, rdev
);
670 f
= JFFS2_INODE_INFO(inode
);
672 ri
->dsize
= ri
->csize
= cpu_to_je32(devlen
);
673 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + devlen
);
674 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
676 ri
->compr
= JFFS2_COMPR_NONE
;
677 ri
->data_crc
= cpu_to_je32(crc32(0, &dev
, devlen
));
678 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
680 fn
= jffs2_write_dnode(c
, f
, ri
, (char *)&dev
, devlen
, ALLOC_NORMAL
);
682 jffs2_free_raw_inode(ri
);
685 /* Eeek. Wave bye bye */
687 jffs2_complete_reservation(c
);
688 jffs2_clear_inode(inode
);
691 /* No data here. Only a metadata node, which will be
692 obsoleted by the first data write
697 jffs2_complete_reservation(c
);
699 ret
= jffs2_init_security(inode
, dir_i
);
701 jffs2_clear_inode(inode
);
704 ret
= jffs2_init_acl(inode
, dir_i
);
706 jffs2_clear_inode(inode
);
710 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &alloclen
,
711 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
714 jffs2_clear_inode(inode
);
718 rd
= jffs2_alloc_raw_dirent();
720 /* Argh. Now we treat it like a normal delete */
721 jffs2_complete_reservation(c
);
722 jffs2_clear_inode(inode
);
726 dir_f
= JFFS2_INODE_INFO(dir_i
);
729 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
730 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
731 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
732 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
734 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
735 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
736 rd
->ino
= cpu_to_je32(inode
->i_ino
);
737 rd
->mctime
= cpu_to_je32(get_seconds());
740 /* XXX: This is ugly. */
741 rd
->type
= (mode
& S_IFMT
) >> 12;
743 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
744 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
746 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, ALLOC_NORMAL
);
749 /* dirent failed to write. Delete the inode normally
750 as if it were the final unlink() */
751 jffs2_complete_reservation(c
);
752 jffs2_free_raw_dirent(rd
);
754 jffs2_clear_inode(inode
);
758 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
760 jffs2_free_raw_dirent(rd
);
762 /* Link the fd into the inode's list, obsoleting an old
764 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
767 jffs2_complete_reservation(c
);
769 d_instantiate(dentry
, inode
);
774 static int jffs2_rename (struct inode
*old_dir_i
, struct dentry
*old_dentry
,
775 struct inode
*new_dir_i
, struct dentry
*new_dentry
)
778 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dir_i
->i_sb
);
779 struct jffs2_inode_info
*victim_f
= NULL
;
783 /* The VFS will check for us and prevent trying to rename a
784 * file over a directory and vice versa, but if it's a directory,
785 * the VFS can't check whether the victim is empty. The filesystem
786 * needs to do that for itself.
788 if (new_dentry
->d_inode
) {
789 victim_f
= JFFS2_INODE_INFO(new_dentry
->d_inode
);
790 if (S_ISDIR(new_dentry
->d_inode
->i_mode
)) {
791 struct jffs2_full_dirent
*fd
;
793 down(&victim_f
->sem
);
794 for (fd
= victim_f
->dents
; fd
; fd
= fd
->next
) {
804 /* XXX: We probably ought to alloc enough space for
805 both nodes at the same time. Writing the new link,
806 then getting -ENOSPC, is quite bad :)
809 /* Make a hard link */
811 /* XXX: This is ugly */
812 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
813 if (!type
) type
= DT_REG
;
816 ret
= jffs2_do_link(c
, JFFS2_INODE_INFO(new_dir_i
),
817 old_dentry
->d_inode
->i_ino
, type
,
818 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
, now
);
824 /* There was a victim. Kill it off nicely */
825 drop_nlink(new_dentry
->d_inode
);
826 /* Don't oops if the victim was a dirent pointing to an
827 inode which didn't exist. */
828 if (victim_f
->inocache
) {
829 down(&victim_f
->sem
);
830 victim_f
->inocache
->nlink
--;
835 /* If it was a directory we moved, and there was no victim,
836 increase i_nlink on its new parent */
837 if (S_ISDIR(old_dentry
->d_inode
->i_mode
) && !victim_f
)
838 inc_nlink(new_dir_i
);
840 /* Unlink the original */
841 ret
= jffs2_do_unlink(c
, JFFS2_INODE_INFO(old_dir_i
),
842 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, NULL
, now
);
844 /* We don't touch inode->i_nlink */
847 /* Oh shit. We really ought to make a single node which can do both atomically */
848 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
850 inc_nlink(old_dentry
->d_inode
);
852 f
->inocache
->nlink
++;
855 printk(KERN_NOTICE
"jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret
);
856 /* Might as well let the VFS know */
857 d_instantiate(new_dentry
, old_dentry
->d_inode
);
858 atomic_inc(&old_dentry
->d_inode
->i_count
);
859 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= ITIME(now
);
863 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
864 drop_nlink(old_dir_i
);
866 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= old_dir_i
->i_mtime
= old_dir_i
->i_ctime
= ITIME(now
);