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>
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>
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)
31 typedef dev_t mknod_arg_t
;
32 #define NAMEI_COMPAT(x) (x)
35 static int jffs2_readdir (struct file
*, void *, filldir_t
);
37 static int jffs2_create (struct inode
*,struct dentry
*,int,
39 static struct dentry
*jffs2_lookup (struct inode
*,struct dentry
*,
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
,
59 struct inode_operations jffs2_dir_inode_operations
=
61 .create
= NAMEI_COMPAT(jffs2_create
),
62 .lookup
= NAMEI_COMPAT(jffs2_lookup
),
64 .unlink
= jffs2_unlink
,
65 .symlink
= jffs2_symlink
,
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
80 static struct dentry
*jffs2_lookup(struct inode
*dir_i
, struct dentry
*target
,
83 struct jffs2_inode_info
*dir_f
;
84 struct jffs2_sb_info
*c
;
85 struct jffs2_full_dirent
*fd
= NULL
, *fd_list
;
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
);
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
)) {
109 inode
= iget(dir_i
->i_sb
, ino
);
111 printk(KERN_WARNING
"iget() failed for ino #%u\n", ino
);
112 return (ERR_PTR(-EIO
));
116 d_add(target
, inode
);
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
;
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)
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)
155 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
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
));
165 D2(printk(KERN_DEBUG
"Skipping deletion dirent \"%s\"\n", fd
->name
));
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)
176 filp
->f_pos
= offset
;
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
;
192 ri
= jffs2_alloc_raw_inode();
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
);
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
);
220 jffs2_clear_inode(inode
);
221 make_bad_inode(inode
);
223 jffs2_free_raw_inode(ri
);
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 /***********************************************************************/
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
);
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
;
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
);
264 /* Don't let people make hard links to bad inodes. */
268 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
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
);
279 old_dentry
->d_inode
->i_nlink
= ++f
->inocache
->nlink
;
281 d_instantiate(dentry
, old_dentry
->d_inode
);
282 atomic_inc(&old_dentry
->d_inode
->i_count
);
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
;
294 struct jffs2_raw_inode
*ri
;
295 struct jffs2_raw_dirent
*rd
;
296 struct jffs2_full_dnode
*fn
;
297 struct jffs2_full_dirent
*fd
;
299 uint32_t alloclen
, phys_ofs
;
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)
307 ri
= jffs2_alloc_raw_inode();
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
);
321 jffs2_free_raw_inode(ri
);
325 inode
= jffs2_new_inode(dir_i
, S_IFLNK
| S_IRWXUGO
, ri
);
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
);
351 /* Eeek. Wave bye bye */
353 jffs2_complete_reservation(c
);
354 jffs2_clear_inode(inode
);
357 /* No data here. Only a metadata node, which will be
358 obsoleted by the first data write
363 jffs2_complete_reservation(c
);
364 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
367 jffs2_clear_inode(inode
);
371 rd
= jffs2_alloc_raw_dirent();
373 /* Argh. Now we treat it like a normal delete */
374 jffs2_complete_reservation(c
);
375 jffs2_clear_inode(inode
);
379 dir_f
= JFFS2_INODE_INFO(dir_i
);
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());
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
);
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
);
404 jffs2_clear_inode(inode
);
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
414 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
417 jffs2_complete_reservation(c
);
419 d_instantiate(dentry
, inode
);
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
;
429 struct jffs2_raw_inode
*ri
;
430 struct jffs2_raw_dirent
*rd
;
431 struct jffs2_full_dnode
*fn
;
432 struct jffs2_full_dirent
*fd
;
434 uint32_t alloclen
, phys_ofs
;
439 ri
= jffs2_alloc_raw_inode();
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
);
452 jffs2_free_raw_inode(ri
);
456 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
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 */
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
);
479 /* Eeek. Wave bye bye */
481 jffs2_complete_reservation(c
);
482 jffs2_clear_inode(inode
);
485 /* No data here. Only a metadata node, which will be
486 obsoleted by the first data write
491 jffs2_complete_reservation(c
);
492 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
495 jffs2_clear_inode(inode
);
499 rd
= jffs2_alloc_raw_dirent();
501 /* Argh. Now we treat it like a normal delete */
502 jffs2_complete_reservation(c
);
503 jffs2_clear_inode(inode
);
507 dir_f
= JFFS2_INODE_INFO(dir_i
);
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());
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
);
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
);
532 jffs2_clear_inode(inode
);
536 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
539 jffs2_free_raw_dirent(rd
);
541 /* Link the fd into the inode's list, obsoleting an old
543 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
546 jffs2_complete_reservation(c
);
548 d_instantiate(dentry
, inode
);
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
;
558 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
562 ret
= jffs2_unlink(dir_i
, dentry
);
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
;
573 struct jffs2_raw_inode
*ri
;
574 struct jffs2_raw_dirent
*rd
;
575 struct jffs2_full_dnode
*fn
;
576 struct jffs2_full_dirent
*fd
;
580 uint32_t alloclen
, phys_ofs
;
583 if (!old_valid_dev(rdev
))
586 ri
= jffs2_alloc_raw_inode();
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
);
604 jffs2_free_raw_inode(ri
);
608 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
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
);
633 /* Eeek. Wave bye bye */
635 jffs2_complete_reservation(c
);
636 jffs2_clear_inode(inode
);
639 /* No data here. Only a metadata node, which will be
640 obsoleted by the first data write
645 jffs2_complete_reservation(c
);
646 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
649 jffs2_clear_inode(inode
);
653 rd
= jffs2_alloc_raw_dirent();
655 /* Argh. Now we treat it like a normal delete */
656 jffs2_complete_reservation(c
);
657 jffs2_clear_inode(inode
);
661 dir_f
= JFFS2_INODE_INFO(dir_i
);
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());
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
);
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
);
689 jffs2_clear_inode(inode
);
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
699 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
702 jffs2_complete_reservation(c
);
704 d_instantiate(dentry
, inode
);
709 static int jffs2_rename (struct inode
*old_dir_i
, struct dentry
*old_dentry
,
710 struct inode
*new_dir_i
, struct dentry
*new_dentry
)
713 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dir_i
->i_sb
);
714 struct jffs2_inode_info
*victim_f
= NULL
;
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
) {
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
);
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
--;
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 */
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
);
783 old_dentry
->d_inode
->i_nlink
++;
785 f
->inocache
->nlink
++;
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
);
795 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
796 old_dir_i
->i_nlink
--;