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.85 2005/03/01 10:34:03 dedekind 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 make_bad_inode(inode
);
222 jffs2_free_raw_inode(ri
);
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 /***********************************************************************/
239 static int jffs2_unlink(struct inode
*dir_i
, struct dentry
*dentry
)
241 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(dir_i
->i_sb
);
242 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
243 struct jffs2_inode_info
*dead_f
= JFFS2_INODE_INFO(dentry
->d_inode
);
246 ret
= jffs2_do_unlink(c
, dir_f
, dentry
->d_name
.name
,
247 dentry
->d_name
.len
, dead_f
);
248 if (dead_f
->inocache
)
249 dentry
->d_inode
->i_nlink
= dead_f
->inocache
->nlink
;
252 /***********************************************************************/
255 static int jffs2_link (struct dentry
*old_dentry
, struct inode
*dir_i
, struct dentry
*dentry
)
257 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dentry
->d_inode
->i_sb
);
258 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
259 struct jffs2_inode_info
*dir_f
= JFFS2_INODE_INFO(dir_i
);
263 /* Don't let people make hard links to bad inodes. */
267 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
270 /* XXX: This is ugly */
271 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
272 if (!type
) type
= DT_REG
;
274 ret
= jffs2_do_link(c
, dir_f
, f
->inocache
->ino
, type
, dentry
->d_name
.name
, dentry
->d_name
.len
);
278 old_dentry
->d_inode
->i_nlink
= ++f
->inocache
->nlink
;
280 d_instantiate(dentry
, old_dentry
->d_inode
);
281 atomic_inc(&old_dentry
->d_inode
->i_count
);
286 /***********************************************************************/
288 static int jffs2_symlink (struct inode
*dir_i
, struct dentry
*dentry
, const char *target
)
290 struct jffs2_inode_info
*f
, *dir_f
;
291 struct jffs2_sb_info
*c
;
293 struct jffs2_raw_inode
*ri
;
294 struct jffs2_raw_dirent
*rd
;
295 struct jffs2_full_dnode
*fn
;
296 struct jffs2_full_dirent
*fd
;
298 uint32_t alloclen
, phys_ofs
;
299 int ret
, targetlen
= strlen(target
);
301 /* FIXME: If you care. We'd need to use frags for the target
302 if it grows much more than this */
306 ri
= jffs2_alloc_raw_inode();
311 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
313 /* Try to reserve enough space for both node and dirent.
314 * Just the node will do for now, though
316 namelen
= dentry
->d_name
.len
;
317 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + targetlen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
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->dents field to store the target path. */
358 f
->dents
= 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
->dents
, target
, targetlen
+ 1);
368 D1(printk(KERN_DEBUG
"jffs2_symlink: symlink's target '%s' cached\n", (char *)f
->dents
));
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
, ALLOC_NORMAL
);
380 jffs2_clear_inode(inode
);
384 rd
= jffs2_alloc_raw_dirent();
386 /* Argh. Now we treat it like a normal delete */
387 jffs2_complete_reservation(c
);
388 jffs2_clear_inode(inode
);
392 dir_f
= JFFS2_INODE_INFO(dir_i
);
395 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
396 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
397 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
398 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
400 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
401 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
402 rd
->ino
= cpu_to_je32(inode
->i_ino
);
403 rd
->mctime
= cpu_to_je32(get_seconds());
406 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
407 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
409 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
412 /* dirent failed to write. Delete the inode normally
413 as if it were the final unlink() */
414 jffs2_complete_reservation(c
);
415 jffs2_free_raw_dirent(rd
);
417 jffs2_clear_inode(inode
);
421 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
423 jffs2_free_raw_dirent(rd
);
425 /* Link the fd into the inode's list, obsoleting an old
427 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
430 jffs2_complete_reservation(c
);
432 d_instantiate(dentry
, inode
);
437 static int jffs2_mkdir (struct inode
*dir_i
, struct dentry
*dentry
, int mode
)
439 struct jffs2_inode_info
*f
, *dir_f
;
440 struct jffs2_sb_info
*c
;
442 struct jffs2_raw_inode
*ri
;
443 struct jffs2_raw_dirent
*rd
;
444 struct jffs2_full_dnode
*fn
;
445 struct jffs2_full_dirent
*fd
;
447 uint32_t alloclen
, phys_ofs
;
452 ri
= jffs2_alloc_raw_inode();
456 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
458 /* Try to reserve enough space for both node and dirent.
459 * Just the node will do for now, though
461 namelen
= dentry
->d_name
.len
;
462 ret
= jffs2_reserve_space(c
, sizeof(*ri
), &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
465 jffs2_free_raw_inode(ri
);
469 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
472 jffs2_free_raw_inode(ri
);
473 jffs2_complete_reservation(c
);
474 return PTR_ERR(inode
);
477 inode
->i_op
= &jffs2_dir_inode_operations
;
478 inode
->i_fop
= &jffs2_dir_operations
;
479 /* Directories get nlink 2 at start */
482 f
= JFFS2_INODE_INFO(inode
);
484 ri
->data_crc
= cpu_to_je32(0);
485 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
487 fn
= jffs2_write_dnode(c
, f
, ri
, NULL
, 0, phys_ofs
, ALLOC_NORMAL
);
489 jffs2_free_raw_inode(ri
);
492 /* Eeek. Wave bye bye */
494 jffs2_complete_reservation(c
);
495 jffs2_clear_inode(inode
);
498 /* No data here. Only a metadata node, which will be
499 obsoleted by the first data write
504 jffs2_complete_reservation(c
);
505 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
508 jffs2_clear_inode(inode
);
512 rd
= jffs2_alloc_raw_dirent();
514 /* Argh. Now we treat it like a normal delete */
515 jffs2_complete_reservation(c
);
516 jffs2_clear_inode(inode
);
520 dir_f
= JFFS2_INODE_INFO(dir_i
);
523 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
524 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
525 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
526 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
528 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
529 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
530 rd
->ino
= cpu_to_je32(inode
->i_ino
);
531 rd
->mctime
= cpu_to_je32(get_seconds());
534 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
535 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
537 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
540 /* dirent failed to write. Delete the inode normally
541 as if it were the final unlink() */
542 jffs2_complete_reservation(c
);
543 jffs2_free_raw_dirent(rd
);
545 jffs2_clear_inode(inode
);
549 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
552 jffs2_free_raw_dirent(rd
);
554 /* Link the fd into the inode's list, obsoleting an old
556 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
559 jffs2_complete_reservation(c
);
561 d_instantiate(dentry
, inode
);
565 static int jffs2_rmdir (struct inode
*dir_i
, struct dentry
*dentry
)
567 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(dentry
->d_inode
);
568 struct jffs2_full_dirent
*fd
;
571 for (fd
= f
->dents
; fd
; fd
= fd
->next
) {
575 ret
= jffs2_unlink(dir_i
, dentry
);
581 static int jffs2_mknod (struct inode
*dir_i
, struct dentry
*dentry
, int mode
, mknod_arg_t rdev
)
583 struct jffs2_inode_info
*f
, *dir_f
;
584 struct jffs2_sb_info
*c
;
586 struct jffs2_raw_inode
*ri
;
587 struct jffs2_raw_dirent
*rd
;
588 struct jffs2_full_dnode
*fn
;
589 struct jffs2_full_dirent
*fd
;
593 uint32_t alloclen
, phys_ofs
;
596 if (!old_valid_dev(rdev
))
599 ri
= jffs2_alloc_raw_inode();
603 c
= JFFS2_SB_INFO(dir_i
->i_sb
);
605 if (S_ISBLK(mode
) || S_ISCHR(mode
)) {
606 dev
= cpu_to_je16(old_encode_dev(rdev
));
607 devlen
= sizeof(dev
);
610 /* Try to reserve enough space for both node and dirent.
611 * Just the node will do for now, though
613 namelen
= dentry
->d_name
.len
;
614 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + devlen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
617 jffs2_free_raw_inode(ri
);
621 inode
= jffs2_new_inode(dir_i
, mode
, ri
);
624 jffs2_free_raw_inode(ri
);
625 jffs2_complete_reservation(c
);
626 return PTR_ERR(inode
);
628 inode
->i_op
= &jffs2_file_inode_operations
;
629 init_special_inode(inode
, inode
->i_mode
, rdev
);
631 f
= JFFS2_INODE_INFO(inode
);
633 ri
->dsize
= ri
->csize
= cpu_to_je32(devlen
);
634 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + devlen
);
635 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
637 ri
->compr
= JFFS2_COMPR_NONE
;
638 ri
->data_crc
= cpu_to_je32(crc32(0, &dev
, devlen
));
639 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
641 fn
= jffs2_write_dnode(c
, f
, ri
, (char *)&dev
, devlen
, phys_ofs
, ALLOC_NORMAL
);
643 jffs2_free_raw_inode(ri
);
646 /* Eeek. Wave bye bye */
648 jffs2_complete_reservation(c
);
649 jffs2_clear_inode(inode
);
652 /* No data here. Only a metadata node, which will be
653 obsoleted by the first data write
658 jffs2_complete_reservation(c
);
659 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
, ALLOC_NORMAL
);
662 jffs2_clear_inode(inode
);
666 rd
= jffs2_alloc_raw_dirent();
668 /* Argh. Now we treat it like a normal delete */
669 jffs2_complete_reservation(c
);
670 jffs2_clear_inode(inode
);
674 dir_f
= JFFS2_INODE_INFO(dir_i
);
677 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
678 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
679 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
680 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
682 rd
->pino
= cpu_to_je32(dir_i
->i_ino
);
683 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
684 rd
->ino
= cpu_to_je32(inode
->i_ino
);
685 rd
->mctime
= cpu_to_je32(get_seconds());
688 /* XXX: This is ugly. */
689 rd
->type
= (mode
& S_IFMT
) >> 12;
691 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
692 rd
->name_crc
= cpu_to_je32(crc32(0, dentry
->d_name
.name
, namelen
));
694 fd
= jffs2_write_dirent(c
, dir_f
, rd
, dentry
->d_name
.name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
697 /* dirent failed to write. Delete the inode normally
698 as if it were the final unlink() */
699 jffs2_complete_reservation(c
);
700 jffs2_free_raw_dirent(rd
);
702 jffs2_clear_inode(inode
);
706 dir_i
->i_mtime
= dir_i
->i_ctime
= ITIME(je32_to_cpu(rd
->mctime
));
708 jffs2_free_raw_dirent(rd
);
710 /* Link the fd into the inode's list, obsoleting an old
712 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
715 jffs2_complete_reservation(c
);
717 d_instantiate(dentry
, inode
);
722 static int jffs2_rename (struct inode
*old_dir_i
, struct dentry
*old_dentry
,
723 struct inode
*new_dir_i
, struct dentry
*new_dentry
)
726 struct jffs2_sb_info
*c
= JFFS2_SB_INFO(old_dir_i
->i_sb
);
727 struct jffs2_inode_info
*victim_f
= NULL
;
730 /* The VFS will check for us and prevent trying to rename a
731 * file over a directory and vice versa, but if it's a directory,
732 * the VFS can't check whether the victim is empty. The filesystem
733 * needs to do that for itself.
735 if (new_dentry
->d_inode
) {
736 victim_f
= JFFS2_INODE_INFO(new_dentry
->d_inode
);
737 if (S_ISDIR(new_dentry
->d_inode
->i_mode
)) {
738 struct jffs2_full_dirent
*fd
;
740 down(&victim_f
->sem
);
741 for (fd
= victim_f
->dents
; fd
; fd
= fd
->next
) {
751 /* XXX: We probably ought to alloc enough space for
752 both nodes at the same time. Writing the new link,
753 then getting -ENOSPC, is quite bad :)
756 /* Make a hard link */
758 /* XXX: This is ugly */
759 type
= (old_dentry
->d_inode
->i_mode
& S_IFMT
) >> 12;
760 if (!type
) type
= DT_REG
;
762 ret
= jffs2_do_link(c
, JFFS2_INODE_INFO(new_dir_i
),
763 old_dentry
->d_inode
->i_ino
, type
,
764 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
);
770 /* There was a victim. Kill it off nicely */
771 new_dentry
->d_inode
->i_nlink
--;
772 /* Don't oops if the victim was a dirent pointing to an
773 inode which didn't exist. */
774 if (victim_f
->inocache
) {
775 down(&victim_f
->sem
);
776 victim_f
->inocache
->nlink
--;
781 /* If it was a directory we moved, and there was no victim,
782 increase i_nlink on its new parent */
783 if (S_ISDIR(old_dentry
->d_inode
->i_mode
) && !victim_f
)
784 new_dir_i
->i_nlink
++;
786 /* Unlink the original */
787 ret
= jffs2_do_unlink(c
, JFFS2_INODE_INFO(old_dir_i
),
788 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
, NULL
);
790 /* We don't touch inode->i_nlink */
793 /* Oh shit. We really ought to make a single node which can do both atomically */
794 struct jffs2_inode_info
*f
= JFFS2_INODE_INFO(old_dentry
->d_inode
);
796 old_dentry
->d_inode
->i_nlink
++;
798 f
->inocache
->nlink
++;
801 printk(KERN_NOTICE
"jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret
);
802 /* Might as well let the VFS know */
803 d_instantiate(new_dentry
, old_dentry
->d_inode
);
804 atomic_inc(&old_dentry
->d_inode
->i_count
);
808 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
809 old_dir_i
->i_nlink
--;