2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/crc32.h>
16 #include <linux/jffs2.h>
17 #include "jffs2_fs_i.h"
18 #include "jffs2_fs_sb.h"
19 #include <linux/time.h>
22 static int jffs2_readdir (struct file *, void *, filldir_t);
24 static int jffs2_create (struct inode *,struct dentry *,int,
26 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
28 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
29 static int jffs2_unlink (struct inode *,struct dentry *);
30 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
31 static int jffs2_mkdir (struct inode *,struct dentry *,int);
32 static int jffs2_rmdir (struct inode *,struct dentry *);
33 static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
34 static int jffs2_rename (struct inode *, struct dentry *,
35 struct inode *, struct dentry *);
37 const struct file_operations jffs2_dir_operations =
39 .read = generic_read_dir,
40 .readdir = jffs2_readdir,
41 .unlocked_ioctl=jffs2_ioctl,
46 const struct inode_operations jffs2_dir_inode_operations =
48 .create = jffs2_create,
49 .lookup = jffs2_lookup,
51 .unlink = jffs2_unlink,
52 .symlink = jffs2_symlink,
56 .rename = jffs2_rename,
57 .permission = jffs2_permission,
58 .setattr = jffs2_setattr,
59 .setxattr = jffs2_setxattr,
60 .getxattr = jffs2_getxattr,
61 .listxattr = jffs2_listxattr,
62 .removexattr = jffs2_removexattr
65 /***********************************************************************/
68 /* We keep the dirent list sorted in increasing order of name hash,
69 and we use the same hash function as the dentries. Makes this
72 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
75 struct jffs2_inode_info *dir_f;
76 struct jffs2_sb_info *c;
77 struct jffs2_full_dirent *fd = NULL, *fd_list;
79 struct inode *inode = NULL;
81 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
83 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
84 return ERR_PTR(-ENAMETOOLONG);
86 dir_f = JFFS2_INODE_INFO(dir_i);
87 c = JFFS2_SB_INFO(dir_i->i_sb);
89 mutex_lock(&dir_f->sem);
91 /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
92 for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
93 if (fd_list->nhash == target->d_name.hash &&
94 (!fd || fd_list->version > fd->version) &&
95 strlen(fd_list->name) == target->d_name.len &&
96 !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
102 mutex_unlock(&dir_f->sem);
104 inode = jffs2_iget(dir_i->i_sb, ino);
106 printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
107 return ERR_CAST(inode);
111 return d_splice_alias(inode, target);
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_path.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_path.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_path.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)
167 mutex_unlock(&f->sem);
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 /* jffs2_do_create() will want to lock it, _after_ reserving
210 space and taking c-alloc_sem. If we keep it locked here,
211 lockdep gets unhappy (although it's a false positive;
212 nothing else will be looking at this inode yet so there's
213 no chance of AB-BA deadlock involving its f->sem). */
214 mutex_unlock(&f->sem);
216 ret = jffs2_do_create(c, dir_f, f, ri,
217 dentry->d_name.name, dentry->d_name.len);
221 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
223 jffs2_free_raw_inode(ri);
224 d_instantiate(dentry, inode);
226 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
227 inode->i_ino, inode->i_mode, inode->i_nlink,
228 f->inocache->pino_nlink, inode->i_mapping->nrpages));
232 make_bad_inode(inode);
234 jffs2_free_raw_inode(ri);
238 /***********************************************************************/
241 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
243 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
244 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
245 struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
247 uint32_t now = get_seconds();
249 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
250 dentry->d_name.len, dead_f, now);
251 if (dead_f->inocache)
252 dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink;
254 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
257 /***********************************************************************/
260 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
262 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
263 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
264 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
269 /* Don't let people make hard links to bad inodes. */
273 if (S_ISDIR(old_dentry->d_inode->i_mode))
276 /* XXX: This is ugly */
277 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
278 if (!type) type = DT_REG;
281 ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
285 old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink;
286 mutex_unlock(&f->sem);
287 d_instantiate(dentry, old_dentry->d_inode);
288 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
289 atomic_inc(&old_dentry->d_inode->i_count);
294 /***********************************************************************/
296 static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
298 struct jffs2_inode_info *f, *dir_f;
299 struct jffs2_sb_info *c;
301 struct jffs2_raw_inode *ri;
302 struct jffs2_raw_dirent *rd;
303 struct jffs2_full_dnode *fn;
304 struct jffs2_full_dirent *fd;
307 int ret, targetlen = strlen(target);
309 /* FIXME: If you care. We'd need to use frags for the target
310 if it grows much more than this */
312 return -ENAMETOOLONG;
314 ri = jffs2_alloc_raw_inode();
319 c = JFFS2_SB_INFO(dir_i->i_sb);
321 /* Try to reserve enough space for both node and dirent.
322 * Just the node will do for now, though
324 namelen = dentry->d_name.len;
325 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
326 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
329 jffs2_free_raw_inode(ri);
333 inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
336 jffs2_free_raw_inode(ri);
337 jffs2_complete_reservation(c);
338 return PTR_ERR(inode);
341 inode->i_op = &jffs2_symlink_inode_operations;
343 f = JFFS2_INODE_INFO(inode);
345 inode->i_size = targetlen;
346 ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
347 ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
348 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
350 ri->compr = JFFS2_COMPR_NONE;
351 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
352 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
354 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
356 jffs2_free_raw_inode(ri);
359 /* Eeek. Wave bye bye */
360 mutex_unlock(&f->sem);
361 jffs2_complete_reservation(c);
362 jffs2_clear_inode(inode);
366 /* We use f->target field to store the target path. */
367 f->target = kmalloc(targetlen + 1, GFP_KERNEL);
369 printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
370 mutex_unlock(&f->sem);
371 jffs2_complete_reservation(c);
372 jffs2_clear_inode(inode);
376 memcpy(f->target, target, targetlen + 1);
377 D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
379 /* No data here. Only a metadata node, which will be
380 obsoleted by the first data write
383 mutex_unlock(&f->sem);
385 jffs2_complete_reservation(c);
387 ret = jffs2_init_security(inode, dir_i);
389 jffs2_clear_inode(inode);
392 ret = jffs2_init_acl_post(inode);
394 jffs2_clear_inode(inode);
398 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
399 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
402 jffs2_clear_inode(inode);
406 rd = jffs2_alloc_raw_dirent();
408 /* Argh. Now we treat it like a normal delete */
409 jffs2_complete_reservation(c);
410 jffs2_clear_inode(inode);
414 dir_f = JFFS2_INODE_INFO(dir_i);
415 mutex_lock(&dir_f->sem);
417 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
418 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
419 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
420 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
422 rd->pino = cpu_to_je32(dir_i->i_ino);
423 rd->version = cpu_to_je32(++dir_f->highest_version);
424 rd->ino = cpu_to_je32(inode->i_ino);
425 rd->mctime = cpu_to_je32(get_seconds());
428 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
429 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
431 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
434 /* dirent failed to write. Delete the inode normally
435 as if it were the final unlink() */
436 jffs2_complete_reservation(c);
437 jffs2_free_raw_dirent(rd);
438 mutex_unlock(&dir_f->sem);
439 jffs2_clear_inode(inode);
443 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
445 jffs2_free_raw_dirent(rd);
447 /* Link the fd into the inode's list, obsoleting an old
449 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
451 mutex_unlock(&dir_f->sem);
452 jffs2_complete_reservation(c);
454 d_instantiate(dentry, inode);
459 static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
461 struct jffs2_inode_info *f, *dir_f;
462 struct jffs2_sb_info *c;
464 struct jffs2_raw_inode *ri;
465 struct jffs2_raw_dirent *rd;
466 struct jffs2_full_dnode *fn;
467 struct jffs2_full_dirent *fd;
474 ri = jffs2_alloc_raw_inode();
478 c = JFFS2_SB_INFO(dir_i->i_sb);
480 /* Try to reserve enough space for both node and dirent.
481 * Just the node will do for now, though
483 namelen = dentry->d_name.len;
484 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
485 JFFS2_SUMMARY_INODE_SIZE);
488 jffs2_free_raw_inode(ri);
492 inode = jffs2_new_inode(dir_i, mode, ri);
495 jffs2_free_raw_inode(ri);
496 jffs2_complete_reservation(c);
497 return PTR_ERR(inode);
500 inode->i_op = &jffs2_dir_inode_operations;
501 inode->i_fop = &jffs2_dir_operations;
503 f = JFFS2_INODE_INFO(inode);
505 /* Directories get nlink 2 at start */
507 /* but ic->pino_nlink is the parent ino# */
508 f->inocache->pino_nlink = dir_i->i_ino;
510 ri->data_crc = cpu_to_je32(0);
511 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
513 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
515 jffs2_free_raw_inode(ri);
518 /* Eeek. Wave bye bye */
519 mutex_unlock(&f->sem);
520 jffs2_complete_reservation(c);
521 jffs2_clear_inode(inode);
524 /* No data here. Only a metadata node, which will be
525 obsoleted by the first data write
528 mutex_unlock(&f->sem);
530 jffs2_complete_reservation(c);
532 ret = jffs2_init_security(inode, dir_i);
534 jffs2_clear_inode(inode);
537 ret = jffs2_init_acl_post(inode);
539 jffs2_clear_inode(inode);
543 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
544 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
547 jffs2_clear_inode(inode);
551 rd = jffs2_alloc_raw_dirent();
553 /* Argh. Now we treat it like a normal delete */
554 jffs2_complete_reservation(c);
555 jffs2_clear_inode(inode);
559 dir_f = JFFS2_INODE_INFO(dir_i);
560 mutex_lock(&dir_f->sem);
562 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
563 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
564 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
565 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
567 rd->pino = cpu_to_je32(dir_i->i_ino);
568 rd->version = cpu_to_je32(++dir_f->highest_version);
569 rd->ino = cpu_to_je32(inode->i_ino);
570 rd->mctime = cpu_to_je32(get_seconds());
573 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
574 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
576 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
579 /* dirent failed to write. Delete the inode normally
580 as if it were the final unlink() */
581 jffs2_complete_reservation(c);
582 jffs2_free_raw_dirent(rd);
583 mutex_unlock(&dir_f->sem);
584 jffs2_clear_inode(inode);
588 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
591 jffs2_free_raw_dirent(rd);
593 /* Link the fd into the inode's list, obsoleting an old
595 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
597 mutex_unlock(&dir_f->sem);
598 jffs2_complete_reservation(c);
600 d_instantiate(dentry, inode);
604 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
606 struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
607 struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
608 struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
609 struct jffs2_full_dirent *fd;
611 uint32_t now = get_seconds();
613 for (fd = f->dents ; fd; fd = fd->next) {
618 ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
619 dentry->d_name.len, f, now);
621 dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
622 clear_nlink(dentry->d_inode);
628 static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
630 struct jffs2_inode_info *f, *dir_f;
631 struct jffs2_sb_info *c;
633 struct jffs2_raw_inode *ri;
634 struct jffs2_raw_dirent *rd;
635 struct jffs2_full_dnode *fn;
636 struct jffs2_full_dirent *fd;
638 union jffs2_device_node dev;
643 if (!new_valid_dev(rdev))
646 ri = jffs2_alloc_raw_inode();
650 c = JFFS2_SB_INFO(dir_i->i_sb);
652 if (S_ISBLK(mode) || S_ISCHR(mode))
653 devlen = jffs2_encode_dev(&dev, rdev);
655 /* Try to reserve enough space for both node and dirent.
656 * Just the node will do for now, though
658 namelen = dentry->d_name.len;
659 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
660 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
663 jffs2_free_raw_inode(ri);
667 inode = jffs2_new_inode(dir_i, mode, ri);
670 jffs2_free_raw_inode(ri);
671 jffs2_complete_reservation(c);
672 return PTR_ERR(inode);
674 inode->i_op = &jffs2_file_inode_operations;
675 init_special_inode(inode, inode->i_mode, rdev);
677 f = JFFS2_INODE_INFO(inode);
679 ri->dsize = ri->csize = cpu_to_je32(devlen);
680 ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
681 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
683 ri->compr = JFFS2_COMPR_NONE;
684 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
685 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
687 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
689 jffs2_free_raw_inode(ri);
692 /* Eeek. Wave bye bye */
693 mutex_unlock(&f->sem);
694 jffs2_complete_reservation(c);
695 jffs2_clear_inode(inode);
698 /* No data here. Only a metadata node, which will be
699 obsoleted by the first data write
702 mutex_unlock(&f->sem);
704 jffs2_complete_reservation(c);
706 ret = jffs2_init_security(inode, dir_i);
708 jffs2_clear_inode(inode);
711 ret = jffs2_init_acl_post(inode);
713 jffs2_clear_inode(inode);
717 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
718 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
721 jffs2_clear_inode(inode);
725 rd = jffs2_alloc_raw_dirent();
727 /* Argh. Now we treat it like a normal delete */
728 jffs2_complete_reservation(c);
729 jffs2_clear_inode(inode);
733 dir_f = JFFS2_INODE_INFO(dir_i);
734 mutex_lock(&dir_f->sem);
736 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
737 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
738 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
739 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
741 rd->pino = cpu_to_je32(dir_i->i_ino);
742 rd->version = cpu_to_je32(++dir_f->highest_version);
743 rd->ino = cpu_to_je32(inode->i_ino);
744 rd->mctime = cpu_to_je32(get_seconds());
747 /* XXX: This is ugly. */
748 rd->type = (mode & S_IFMT) >> 12;
750 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
751 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
753 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
756 /* dirent failed to write. Delete the inode normally
757 as if it were the final unlink() */
758 jffs2_complete_reservation(c);
759 jffs2_free_raw_dirent(rd);
760 mutex_unlock(&dir_f->sem);
761 jffs2_clear_inode(inode);
765 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
767 jffs2_free_raw_dirent(rd);
769 /* Link the fd into the inode's list, obsoleting an old
771 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
773 mutex_unlock(&dir_f->sem);
774 jffs2_complete_reservation(c);
776 d_instantiate(dentry, inode);
781 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
782 struct inode *new_dir_i, struct dentry *new_dentry)
785 struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
786 struct jffs2_inode_info *victim_f = NULL;
790 /* The VFS will check for us and prevent trying to rename a
791 * file over a directory and vice versa, but if it's a directory,
792 * the VFS can't check whether the victim is empty. The filesystem
793 * needs to do that for itself.
795 if (new_dentry->d_inode) {
796 victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
797 if (S_ISDIR(new_dentry->d_inode->i_mode)) {
798 struct jffs2_full_dirent *fd;
800 mutex_lock(&victim_f->sem);
801 for (fd = victim_f->dents; fd; fd = fd->next) {
803 mutex_unlock(&victim_f->sem);
807 mutex_unlock(&victim_f->sem);
811 /* XXX: We probably ought to alloc enough space for
812 both nodes at the same time. Writing the new link,
813 then getting -ENOSPC, is quite bad :)
816 /* Make a hard link */
818 /* XXX: This is ugly */
819 type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
820 if (!type) type = DT_REG;
823 ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
824 old_dentry->d_inode->i_ino, type,
825 new_dentry->d_name.name, new_dentry->d_name.len, now);
831 /* There was a victim. Kill it off nicely */
832 drop_nlink(new_dentry->d_inode);
833 /* Don't oops if the victim was a dirent pointing to an
834 inode which didn't exist. */
835 if (victim_f->inocache) {
836 mutex_lock(&victim_f->sem);
837 if (S_ISDIR(new_dentry->d_inode->i_mode))
838 victim_f->inocache->pino_nlink = 0;
840 victim_f->inocache->pino_nlink--;
841 mutex_unlock(&victim_f->sem);
845 /* If it was a directory we moved, and there was no victim,
846 increase i_nlink on its new parent */
847 if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
848 inc_nlink(new_dir_i);
850 /* Unlink the original */
851 ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
852 old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
854 /* We don't touch inode->i_nlink */
857 /* Oh shit. We really ought to make a single node which can do both atomically */
858 struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
860 inc_nlink(old_dentry->d_inode);
861 if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
862 f->inocache->pino_nlink++;
863 mutex_unlock(&f->sem);
865 printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
866 /* Might as well let the VFS know */
867 d_instantiate(new_dentry, old_dentry->d_inode);
868 atomic_inc(&old_dentry->d_inode->i_count);
869 new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
873 if (S_ISDIR(old_dentry->d_inode->i_mode))
874 drop_nlink(old_dir_i);
876 new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);