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 .setattr
= jffs2_setattr
,
63 /***********************************************************************/
66 /* We keep the dirent list sorted in increasing order of name hash,
67 and we use the same hash function as the dentries. Makes this
70 static struct dentry
*jffs2_lookup(struct inode
*dir_i
, struct dentry
*target
,
73 struct jffs2_inode_info
*dir_f
;
74 struct jffs2_sb_info
*c
;
75 struct jffs2_full_dirent
*fd
= NULL
, *fd_list
;
77 struct inode
*inode
= NULL
;
79 D1(printk(KERN_DEBUG
"jffs2_lookup()\n"));
81 if (target
->d_name
.len
> JFFS2_MAX_NAME_LEN
)
82 return ERR_PTR(-ENAMETOOLONG
);
84 dir_f
= JFFS2_INODE_INFO(dir_i
);
85 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
89 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
90 for (fd_list
= dir_f
->dents
; fd_list
&& fd_list
->nhash
<= target
->d_name
.hash
; fd_list
= fd_list
->next
) {
91 if (fd_list
->nhash
== target
->d_name
.hash
&&
92 (!fd
|| fd_list
->version
> fd
->version
) &&
93 strlen(fd_list
->name
) == target
->d_name
.len
&&
94 !strncmp(fd_list
->name
, target
->d_name
.name
, target
->d_name
.len
)) {
102 inode
= iget(dir_i
->i_sb
, ino
);
104 printk(KERN_WARNING
"iget() failed for ino #%u\n", ino
);
105 return (ERR_PTR(-EIO
));
109 d_add(target
, inode
);
114 /***********************************************************************/
117 static int jffs2_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
119 struct jffs2_inode_info
*f
;
120 struct jffs2_sb_info
*c
;
121 struct inode
*inode
= filp
->f_dentry
->d_inode
;
122 struct jffs2_full_dirent
*fd
;
123 unsigned long offset
, curofs
;
125 D1(printk(KERN_DEBUG
"jffs2_readdir() for dir_i #%lu\n", filp
->f_dentry
->d_inode
->i_ino
));
127 f
= JFFS2_INODE_INFO(inode
);
128 c
= JFFS2_SB_INFO(inode
->i_sb
);
130 offset
= filp
->f_pos
;
133 D1(printk(KERN_DEBUG
"Dirent 0: \".\", ino #%lu\n", inode
->i_ino
));
134 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
139 unsigned long pino
= parent_ino(filp
->f_dentry
);
140 D1(printk(KERN_DEBUG
"Dirent 1: \"..\", ino #%lu\n", pino
));
141 if (filldir(dirent
, "..", 2, 1, pino
, DT_DIR
) < 0)
148 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
151 /* First loop: curofs = 2; offset = 2 */
152 if (curofs
< offset
) {
153 D2(printk(KERN_DEBUG
"Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
154 fd
->name
, fd
->ino
, fd
->type
, curofs
, offset
));
158 D2(printk(KERN_DEBUG
"Skipping deletion dirent \"%s\"\n", fd
->name
));
162 D2(printk(KERN_DEBUG
"Dirent %ld: \"%s\", ino #%u, type %d\n", offset
, fd
->name
, fd
->ino
, fd
->type
));
163 if (filldir(dirent
, fd
->name
, strlen(fd
->name
), offset
, fd
->ino
, fd
->type
) < 0)
169 filp
->f_pos
= offset
;
173 /***********************************************************************/
176 static int jffs2_create(struct inode
*dir_i
, struct dentry
*dentry
, int mode
,
177 struct nameidata
*nd
)
179 struct jffs2_raw_inode
*ri
;
180 struct jffs2_inode_info
*f
, *dir_f
;
181 struct jffs2_sb_info
*c
;
185 ri
= jffs2_alloc_raw_inode();
189 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
191 D1(printk(KERN_DEBUG
"jffs2_create()\n"));
193 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
196 D1(printk(KERN_DEBUG
"jffs2_new_inode() failed\n"));
197 jffs2_free_raw_inode(ri
);
198 return PTR_ERR(inode
);
201 inode
->i_op
= &jffs2_file_inode_operations
;
202 inode
->i_fop
= &jffs2_file_operations
;
203 inode
->i_mapping
->a_ops
= &jffs2_file_address_operations
;
204 inode
->i_mapping
->nrpages
= 0;
206 f
= JFFS2_INODE_INFO(inode
);
207 dir_f
= JFFS2_INODE_INFO(dir_i
);
209 ret
= jffs2_do_create(c
, dir_f
, f
, ri
,
210 dentry
->d_name
.name
, dentry
->d_name
.len
);
213 make_bad_inode(inode
);
215 jffs2_free_raw_inode(ri
);
219 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(ri
->ctime
));
221 jffs2_free_raw_inode(ri
);
222 d_instantiate(dentry
, inode
);
224 D1(printk(KERN_DEBUG
"jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
225 inode
->i_ino
, inode
->i_mode
, inode
->i_nlink
, f
->inocache
->nlink
, inode
->i_mapping
->nrpages
));
229 /***********************************************************************/
232 static int jffs2_unlink(struct inode
*dir_i
, struct dentry
*dentry
)
234 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(dir_i
->i_sb
);
235 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
236 struct jffs2_inode_info
*dead_f
= JFFS2_INODE_INFO(dentry
->d_inode
);
238 uint32_t now
= get_seconds();
240 ret
= jffs2_do_unlink(c
, dir_f
, dentry
->d_name
.name
,
241 dentry
->d_name
.len
, dead_f
, now
);
242 if (dead_f
->inocache
)
243 dentry
->d_inode
->i_nlink
= dead_f
->inocache
->nlink
;
245 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
248 /***********************************************************************/
251 static int jffs2_link (struct dentry
*old_dentry
, struct inode
*dir_i
, struct dentry
*dentry
)
253 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dentry
->d_inode
->i_sb
);
254 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
255 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
260 /* Don't let people make hard links to bad inodes. */
264 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
267 /* XXX: This is ugly */
268 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
269 if (!type
) type
= DT_REG
;
272 ret
= jffs2_do_link(c
, dir_f
, f
->inocache
->ino
, type
, dentry
->d_name
.name
, dentry
->d_name
.len
, now
);
276 old_dentry
->d_inode
->i_nlink
= ++f
->inocache
->nlink
;
278 d_instantiate(dentry
, old_dentry
->d_inode
);
279 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(now
);
280 atomic_inc(&old_dentry
->d_inode
->i_count
);
285 /***********************************************************************/
287 static int jffs2_symlink (struct inode
*dir_i
, struct dentry
*dentry
, const char *target
)
289 struct jffs2_inode_info
*f
, *dir_f
;
290 struct jffs2_sb_info
*c
;
292 struct jffs2_raw_inode
*ri
;
293 struct jffs2_raw_dirent
*rd
;
294 struct jffs2_full_dnode
*fn
;
295 struct jffs2_full_dirent
*fd
;
297 uint32_t alloclen
, phys_ofs
;
298 int ret
, targetlen
= strlen(target
);
300 /* FIXME: If you care. We'd need to use frags for the target
301 if it grows much more than this */
305 ri
= jffs2_alloc_raw_inode();
310 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
312 /* Try to reserve enough space for both node and dirent.
313 * Just the node will do for now, though
315 namelen
= dentry
->d_name
.len
;
316 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + targetlen
, &phys_ofs
, &alloclen
,
317 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
320 jffs2_free_raw_inode(ri
);
324 inode
= jffs2_new_inode(dir_i
, S_IFLNK
| S_IRWXUGO
, ri
);
327 jffs2_free_raw_inode(ri
);
328 jffs2_complete_reservation(c
);
329 return PTR_ERR(inode
);
332 inode
->i_op
= &jffs2_symlink_inode_operations
;
334 f
= JFFS2_INODE_INFO(inode
);
336 inode
->i_size
= targetlen
;
337 ri
->isize
= ri
->dsize
= ri
->csize
= cpu_to_je32(inode
->i_size
);
338 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + inode
->i_size
);
339 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
341 ri
->compr
= JFFS2_COMPR_NONE
;
342 ri
->data_crc
= cpu_to_je32(crc32(0, target
, targetlen
));
343 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
345 fn
= jffs2_write_dnode(c
, f
, ri
, target
, targetlen
, phys_ofs
, ALLOC_NORMAL
);
347 jffs2_free_raw_inode(ri
);
350 /* Eeek. Wave bye bye */
352 jffs2_complete_reservation(c
);
353 jffs2_clear_inode(inode
);
357 /* We use f->target field to store the target path. */
358 f
->target
= kmalloc(targetlen
+ 1, GFP_KERNEL
);
360 printk(KERN_WARNING
"Can't allocate %d bytes of memory\n", targetlen
+ 1);
362 jffs2_complete_reservation(c
);
363 jffs2_clear_inode(inode
);
367 memcpy(f
->target
, target
, targetlen
+ 1);
368 D1(printk(KERN_DEBUG
"jffs2_symlink: symlink's target '%s' cached\n", (char *)f
->target
));
370 /* No data here. Only a metadata node, which will be
371 obsoleted by the first data write
376 jffs2_complete_reservation(c
);
377 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
378 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
381 jffs2_clear_inode(inode
);
385 rd
= jffs2_alloc_raw_dirent();
387 /* Argh. Now we treat it like a normal delete */
388 jffs2_complete_reservation(c
);
389 jffs2_clear_inode(inode
);
393 dir_f
= JFFS2_INODE_INFO(dir_i
);
396 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
397 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
398 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
399 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
401 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
402 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
403 rd
->ino
= cpu_to_je32(inode
->i_ino
);
404 rd
->mctime
= cpu_to_je32(get_seconds());
407 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
408 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
410 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
413 /* dirent failed to write. Delete the inode normally
414 as if it were the final unlink() */
415 jffs2_complete_reservation(c
);
416 jffs2_free_raw_dirent(rd
);
418 jffs2_clear_inode(inode
);
422 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
424 jffs2_free_raw_dirent(rd
);
426 /* Link the fd into the inode's list, obsoleting an old
428 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
431 jffs2_complete_reservation(c
);
433 d_instantiate(dentry
, inode
);
438 static int jffs2_mkdir (struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
440 struct jffs2_inode_info
*f
, *dir_f
;
441 struct jffs2_sb_info
*c
;
443 struct jffs2_raw_inode
*ri
;
444 struct jffs2_raw_dirent
*rd
;
445 struct jffs2_full_dnode
*fn
;
446 struct jffs2_full_dirent
*fd
;
448 uint32_t alloclen
, phys_ofs
;
453 ri
= jffs2_alloc_raw_inode();
457 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
459 /* Try to reserve enough space for both node and dirent.
460 * Just the node will do for now, though
462 namelen
= dentry
->d_name
.len
;
463 ret
= jffs2_reserve_space(c
, sizeof(*ri
), &phys_ofs
, &alloclen
, ALLOC_NORMAL
,
464 JFFS2_SUMMARY_INODE_SIZE
);
467 jffs2_free_raw_inode(ri
);
471 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
474 jffs2_free_raw_inode(ri
);
475 jffs2_complete_reservation(c
);
476 return PTR_ERR(inode
);
479 inode
->i_op
= &jffs2_dir_inode_operations
;
480 inode
->i_fop
= &jffs2_dir_operations
;
481 /* Directories get nlink 2 at start */
484 f
= JFFS2_INODE_INFO(inode
);
486 ri
->data_crc
= cpu_to_je32(0);
487 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
489 fn
= jffs2_write_dnode(c
, f
, ri
, NULL
, 0, phys_ofs
, ALLOC_NORMAL
);
491 jffs2_free_raw_inode(ri
);
494 /* Eeek. Wave bye bye */
496 jffs2_complete_reservation(c
);
497 jffs2_clear_inode(inode
);
500 /* No data here. Only a metadata node, which will be
501 obsoleted by the first data write
506 jffs2_complete_reservation(c
);
507 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
508 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
511 jffs2_clear_inode(inode
);
515 rd
= jffs2_alloc_raw_dirent();
517 /* Argh. Now we treat it like a normal delete */
518 jffs2_complete_reservation(c
);
519 jffs2_clear_inode(inode
);
523 dir_f
= JFFS2_INODE_INFO(dir_i
);
526 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
527 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
528 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
529 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
531 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
532 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
533 rd
->ino
= cpu_to_je32(inode
->i_ino
);
534 rd
->mctime
= cpu_to_je32(get_seconds());
537 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
538 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
540 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
543 /* dirent failed to write. Delete the inode normally
544 as if it were the final unlink() */
545 jffs2_complete_reservation(c
);
546 jffs2_free_raw_dirent(rd
);
548 jffs2_clear_inode(inode
);
552 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
555 jffs2_free_raw_dirent(rd
);
557 /* Link the fd into the inode's list, obsoleting an old
559 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
562 jffs2_complete_reservation(c
);
564 d_instantiate(dentry
, inode
);
568 static int jffs2_rmdir (struct inode
*dir_i
, struct dentry
*dentry
)
570 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(dentry
->d_inode
);
571 struct jffs2_full_dirent
*fd
;
574 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
578 ret
= jffs2_unlink(dir_i
, dentry
);
584 static int jffs2_mknod (struct inode
*dir_i
, struct dentry
*dentry
, int mode
, dev_t rdev
)
586 struct jffs2_inode_info
*f
, *dir_f
;
587 struct jffs2_sb_info
*c
;
589 struct jffs2_raw_inode
*ri
;
590 struct jffs2_raw_dirent
*rd
;
591 struct jffs2_full_dnode
*fn
;
592 struct jffs2_full_dirent
*fd
;
594 union jffs2_device_node dev
;
596 uint32_t alloclen
, phys_ofs
;
599 if (!new_valid_dev(rdev
))
602 ri
= jffs2_alloc_raw_inode();
606 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
608 if (S_ISBLK(mode
) || S_ISCHR(mode
))
609 devlen
= jffs2_encode_dev(&dev
, rdev
);
611 /* Try to reserve enough space for both node and dirent.
612 * Just the node will do for now, though
614 namelen
= dentry
->d_name
.len
;
615 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + devlen
, &phys_ofs
, &alloclen
,
616 ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
619 jffs2_free_raw_inode(ri
);
623 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
626 jffs2_free_raw_inode(ri
);
627 jffs2_complete_reservation(c
);
628 return PTR_ERR(inode
);
630 inode
->i_op
= &jffs2_file_inode_operations
;
631 init_special_inode(inode
, inode
->i_mode
, rdev
);
633 f
= JFFS2_INODE_INFO(inode
);
635 ri
->dsize
= ri
->csize
= cpu_to_je32(devlen
);
636 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + devlen
);
637 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
639 ri
->compr
= JFFS2_COMPR_NONE
;
640 ri
->data_crc
= cpu_to_je32(crc32(0, &dev
, devlen
));
641 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
643 fn
= jffs2_write_dnode(c
, f
, ri
, (char *)&dev
, devlen
, phys_ofs
, ALLOC_NORMAL
);
645 jffs2_free_raw_inode(ri
);
648 /* Eeek. Wave bye bye */
650 jffs2_complete_reservation(c
);
651 jffs2_clear_inode(inode
);
654 /* No data here. Only a metadata node, which will be
655 obsoleted by the first data write
660 jffs2_complete_reservation(c
);
661 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
662 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
665 jffs2_clear_inode(inode
);
669 rd
= jffs2_alloc_raw_dirent();
671 /* Argh. Now we treat it like a normal delete */
672 jffs2_complete_reservation(c
);
673 jffs2_clear_inode(inode
);
677 dir_f
= JFFS2_INODE_INFO(dir_i
);
680 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
681 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
682 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
683 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
685 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
686 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
687 rd
->ino
= cpu_to_je32(inode
->i_ino
);
688 rd
->mctime
= cpu_to_je32(get_seconds());
691 /* XXX: This is ugly. */
692 rd
->type
= (mode
& S_IFMT
) >> 12;
694 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
695 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
697 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
700 /* dirent failed to write. Delete the inode normally
701 as if it were the final unlink() */
702 jffs2_complete_reservation(c
);
703 jffs2_free_raw_dirent(rd
);
705 jffs2_clear_inode(inode
);
709 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
711 jffs2_free_raw_dirent(rd
);
713 /* Link the fd into the inode's list, obsoleting an old
715 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
718 jffs2_complete_reservation(c
);
720 d_instantiate(dentry
, inode
);
725 static int jffs2_rename (struct inode
*old_dir_i
, struct dentry
*old_dentry
,
726 struct inode
*new_dir_i
, struct dentry
*new_dentry
)
729 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dir_i
->i_sb
);
730 struct jffs2_inode_info
*victim_f
= NULL
;
734 /* The VFS will check for us and prevent trying to rename a
735 * file over a directory and vice versa, but if it's a directory,
736 * the VFS can't check whether the victim is empty. The filesystem
737 * needs to do that for itself.
739 if (new_dentry
->d_inode
) {
740 victim_f
= JFFS2_INODE_INFO(new_dentry
->d_inode
);
741 if (S_ISDIR(new_dentry
->d_inode
->i_mode
)) {
742 struct jffs2_full_dirent
*fd
;
744 down(&victim_f
->sem
);
745 for (fd
= victim_f
->dents
; fd
; fd
= fd
->next
) {
755 /* XXX: We probably ought to alloc enough space for
756 both nodes at the same time. Writing the new link,
757 then getting -ENOSPC, is quite bad :)
760 /* Make a hard link */
762 /* XXX: This is ugly */
763 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
764 if (!type
) type
= DT_REG
;
767 ret
= jffs2_do_link(c
, JFFS2_INODE_INFO(new_dir_i
),
768 old_dentry
->d_inode
->i_ino
, type
,
769 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
, now
);
775 /* There was a victim. Kill it off nicely */
776 new_dentry
->d_inode
->i_nlink
--;
777 /* Don't oops if the victim was a dirent pointing to an
778 inode which didn't exist. */
779 if (victim_f
->inocache
) {
780 down(&victim_f
->sem
);
781 victim_f
->inocache
->nlink
--;
786 /* If it was a directory we moved, and there was no victim,
787 increase i_nlink on its new parent */
788 if (S_ISDIR(old_dentry
->d_inode
->i_mode
) && !victim_f
)
789 new_dir_i
->i_nlink
++;
791 /* Unlink the original */
792 ret
= jffs2_do_unlink(c
, JFFS2_INODE_INFO(old_dir_i
),
793 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, NULL
, now
);
795 /* We don't touch inode->i_nlink */
798 /* Oh shit. We really ought to make a single node which can do both atomically */
799 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
801 old_dentry
->d_inode
->i_nlink
++;
803 f
->inocache
->nlink
++;
806 printk(KERN_NOTICE
"jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret
);
807 /* Might as well let the VFS know */
808 d_instantiate(new_dentry
, old_dentry
->d_inode
);
809 atomic_inc(&old_dentry
->d_inode
->i_count
);
810 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= ITIME(now
);
814 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
815 old_dir_i
->i_nlink
--;
817 new_dir_i
->i_mtime
= new_dir_i
->i_ctime
= old_dir_i
->i_mtime
= old_dir_i
->i_ctime
= ITIME(now
);