nfs: new subdir Documentation/filesystems/nfs
[safe/jmp/linux-2.6] / fs / jffs2 / dir.c
index 6421be8..7aa4417 100644 (file)
@@ -1,37 +1,24 @@
 /*
  * JFFS2 -- Journalling Flash File System, Version 2.
  *
- * Copyright (C) 2001-2003 Red Hat, Inc.
+ * Copyright © 2001-2007 Red Hat, Inc.
  *
  * Created by David Woodhouse <dwmw2@infradead.org>
  *
  * For licensing information, see the file 'LICENCE' in this directory.
  *
- * $Id: dir.c,v 1.85 2005/03/01 10:34:03 dedekind Exp $
- *
  */
 
 #include <linux/kernel.h>
 #include <linux/slab.h>
-#include <linux/sched.h>
 #include <linux/fs.h>
 #include <linux/crc32.h>
 #include <linux/jffs2.h>
-#include <linux/jffs2_fs_i.h>
-#include <linux/jffs2_fs_sb.h>
+#include "jffs2_fs_i.h"
+#include "jffs2_fs_sb.h"
 #include <linux/time.h>
 #include "nodelist.h"
 
-/* Urgh. Please tell me there's a nicer way of doing these. */
-#include <linux/version.h>
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,48)
-typedef int mknod_arg_t;
-#define NAMEI_COMPAT(x) ((void *)x)
-#else
-typedef dev_t mknod_arg_t;
-#define NAMEI_COMPAT(x) (x)
-#endif
-
 static int jffs2_readdir (struct file *, void *, filldir_t);
 
 static int jffs2_create (struct inode *,struct dentry *,int,
@@ -43,23 +30,24 @@ static int jffs2_unlink (struct inode *,struct dentry *);
 static int jffs2_symlink (struct inode *,struct dentry *,const char *);
 static int jffs2_mkdir (struct inode *,struct dentry *,int);
 static int jffs2_rmdir (struct inode *,struct dentry *);
-static int jffs2_mknod (struct inode *,struct dentry *,int,mknod_arg_t);
+static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
 static int jffs2_rename (struct inode *, struct dentry *,
-                        struct inode *, struct dentry *);
+                        struct inode *, struct dentry *);
 
-struct file_operations jffs2_dir_operations =
+const struct file_operations jffs2_dir_operations =
 {
        .read =         generic_read_dir,
        .readdir =      jffs2_readdir,
-       .ioctl =        jffs2_ioctl,
-       .fsync =        jffs2_fsync
+       .unlocked_ioctl=jffs2_ioctl,
+       .fsync =        jffs2_fsync,
+       .llseek =       generic_file_llseek,
 };
 
 
-struct inode_operations jffs2_dir_inode_operations =
+const struct inode_operations jffs2_dir_inode_operations =
 {
-       .create =       NAMEI_COMPAT(jffs2_create),
-       .lookup =       NAMEI_COMPAT(jffs2_lookup),
+       .create =       jffs2_create,
+       .lookup =       jffs2_lookup,
        .link =         jffs2_link,
        .unlink =       jffs2_unlink,
        .symlink =      jffs2_symlink,
@@ -67,14 +55,19 @@ struct inode_operations jffs2_dir_inode_operations =
        .rmdir =        jffs2_rmdir,
        .mknod =        jffs2_mknod,
        .rename =       jffs2_rename,
+       .check_acl =    jffs2_check_acl,
        .setattr =      jffs2_setattr,
+       .setxattr =     jffs2_setxattr,
+       .getxattr =     jffs2_getxattr,
+       .listxattr =    jffs2_listxattr,
+       .removexattr =  jffs2_removexattr
 };
 
 /***********************************************************************/
 
 
 /* We keep the dirent list sorted in increasing order of name hash,
-   and we use the same hash function as the dentries. Makes this 
+   and we use the same hash function as the dentries. Makes this
    nice and simple
 */
 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
@@ -88,14 +81,17 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
 
        D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
 
+       if (target->d_name.len > JFFS2_MAX_NAME_LEN)
+               return ERR_PTR(-ENAMETOOLONG);
+
        dir_f = JFFS2_INODE_INFO(dir_i);
        c = JFFS2_SB_INFO(dir_i->i_sb);
 
-       down(&dir_f->sem);
+       mutex_lock(&dir_f->sem);
 
        /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
        for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
-               if (fd_list->nhash == target->d_name.hash && 
+               if (fd_list->nhash == target->d_name.hash &&
                    (!fd || fd_list->version > fd->version) &&
                    strlen(fd_list->name) == target->d_name.len &&
                    !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
@@ -104,18 +100,16 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
        }
        if (fd)
                ino = fd->ino;
-       up(&dir_f->sem);
+       mutex_unlock(&dir_f->sem);
        if (ino) {
-               inode = iget(dir_i->i_sb, ino);
-               if (!inode) {
+               inode = jffs2_iget(dir_i->i_sb, ino);
+               if (IS_ERR(inode)) {
                        printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
-                       return (ERR_PTR(-EIO));
+                       return ERR_CAST(inode);
                }
        }
 
-       d_add(target, inode);
-
-       return NULL;
+       return d_splice_alias(inode, target);
 }
 
 /***********************************************************************/
@@ -125,11 +119,11 @@ static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
        struct jffs2_inode_info *f;
        struct jffs2_sb_info *c;
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        struct jffs2_full_dirent *fd;
        unsigned long offset, curofs;
 
-       D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
+       D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_path.dentry->d_inode->i_ino));
 
        f = JFFS2_INODE_INFO(inode);
        c = JFFS2_SB_INFO(inode->i_sb);
@@ -143,7 +137,7 @@ static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
                offset++;
        }
        if (offset == 1) {
-               unsigned long pino = parent_ino(filp->f_dentry);
+               unsigned long pino = parent_ino(filp->f_path.dentry);
                D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
                if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
                        goto out;
@@ -151,13 +145,13 @@ static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
        }
 
        curofs=1;
-       down(&f->sem);
+       mutex_lock(&f->sem);
        for (fd = f->dents; fd; fd = fd->next) {
 
                curofs++;
                /* First loop: curofs = 2; offset = 2 */
                if (curofs < offset) {
-                       D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n", 
+                       D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
                                  fd->name, fd->ino, fd->type, curofs, offset));
                        continue;
                }
@@ -171,7 +165,7 @@ static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
                        break;
                offset++;
        }
-       up(&f->sem);
+       mutex_unlock(&f->sem);
  out:
        filp->f_pos = offset;
        return 0;
@@ -192,7 +186,7 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
        ri = jffs2_alloc_raw_inode();
        if (!ri)
                return -ENOMEM;
-       
+
        c = JFFS2_SB_INFO(dir_i->i_sb);
 
        D1(printk(KERN_DEBUG "jffs2_create()\n"));
@@ -213,15 +207,17 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
        f = JFFS2_INODE_INFO(inode);
        dir_f = JFFS2_INODE_INFO(dir_i);
 
-       ret = jffs2_do_create(c, dir_f, f, ri, 
-                             dentry->d_name.name, dentry->d_name.len);
+       /* jffs2_do_create() will want to lock it, _after_ reserving
+          space and taking c-alloc_sem. If we keep it locked here,
+          lockdep gets unhappy (although it's a false positive;
+          nothing else will be looking at this inode yet so there's
+          no chance of AB-BA deadlock involving its f->sem). */
+       mutex_unlock(&f->sem);
 
-       if (ret) {
-               make_bad_inode(inode);
-               iput(inode);
-               jffs2_free_raw_inode(ri);
-               return ret;
-       }
+       ret = jffs2_do_create(c, dir_f, f, ri,
+                             dentry->d_name.name, dentry->d_name.len);
+       if (ret)
+               goto fail;
 
        dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
 
@@ -229,8 +225,15 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
        d_instantiate(dentry, inode);
 
        D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
-                 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
+                 inode->i_ino, inode->i_mode, inode->i_nlink,
+                 f->inocache->pino_nlink, inode->i_mapping->nrpages));
        return 0;
+
+ fail:
+       make_bad_inode(inode);
+       iput(inode);
+       jffs2_free_raw_inode(ri);
+       return ret;
 }
 
 /***********************************************************************/
@@ -242,11 +245,14 @@ static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
        struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
        struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
        int ret;
+       uint32_t now = get_seconds();
 
-       ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, 
-                              dentry->d_name.len, dead_f);
+       ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
+                             dentry->d_name.len, dead_f, now);
        if (dead_f->inocache)
-               dentry->d_inode->i_nlink = dead_f->inocache->nlink;
+               dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink;
+       if (!ret)
+               dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
        return ret;
 }
 /***********************************************************************/
@@ -259,6 +265,7 @@ static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct de
        struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
        int ret;
        uint8_t type;
+       uint32_t now;
 
        /* Don't let people make hard links to bad inodes. */
        if (!f->inocache)
@@ -271,13 +278,15 @@ static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct de
        type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
        if (!type) type = DT_REG;
 
-       ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
+       now = get_seconds();
+       ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
 
        if (!ret) {
-               down(&f->sem);
-               old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
-               up(&f->sem);
+               mutex_lock(&f->sem);
+               old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink;
+               mutex_unlock(&f->sem);
                d_instantiate(dentry, old_dentry->d_inode);
+               dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
                atomic_inc(&old_dentry->d_inode->i_count);
        }
        return ret;
@@ -295,26 +304,27 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        struct jffs2_full_dnode *fn;
        struct jffs2_full_dirent *fd;
        int namelen;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen;
        int ret, targetlen = strlen(target);
 
        /* FIXME: If you care. We'd need to use frags for the target
           if it grows much more than this */
        if (targetlen > 254)
-               return -EINVAL;
+               return -ENAMETOOLONG;
 
        ri = jffs2_alloc_raw_inode();
 
        if (!ri)
                return -ENOMEM;
-       
+
        c = JFFS2_SB_INFO(dir_i->i_sb);
-       
-       /* Try to reserve enough space for both node and dirent. 
-        * Just the node will do for now, though 
+
+       /* Try to reserve enough space for both node and dirent.
+        * Just the node will do for now, though
         */
        namelen = dentry->d_name.len;
-       ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -341,40 +351,53 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        ri->compr = JFFS2_COMPR_NONE;
        ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
        ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
-       
-       fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL);
+
+       fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
 
        jffs2_free_raw_inode(ri);
 
        if (IS_ERR(fn)) {
                /* Eeek. Wave bye bye */
-               up(&f->sem);
+               mutex_unlock(&f->sem);
                jffs2_complete_reservation(c);
                jffs2_clear_inode(inode);
                return PTR_ERR(fn);
        }
 
-       /* We use f->dents field to store the target path. */
-       f->dents = kmalloc(targetlen + 1, GFP_KERNEL);
-       if (!f->dents) {
+       /* We use f->target field to store the target path. */
+       f->target = kmalloc(targetlen + 1, GFP_KERNEL);
+       if (!f->target) {
                printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
-               up(&f->sem);
+               mutex_unlock(&f->sem);
                jffs2_complete_reservation(c);
                jffs2_clear_inode(inode);
                return -ENOMEM;
        }
 
-       memcpy(f->dents, target, targetlen + 1);
-       D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->dents));
+       memcpy(f->target, target, targetlen + 1);
+       D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
 
-       /* No data here. Only a metadata node, which will be 
+       /* No data here. Only a metadata node, which will be
           obsoleted by the first data write
        */
        f->metadata = fn;
-       up(&f->sem);
+       mutex_unlock(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       ret = jffs2_init_security(inode, dir_i);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+       ret = jffs2_init_acl_post(inode);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
        if (ret) {
                /* Eep. */
                jffs2_clear_inode(inode);
@@ -390,7 +413,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        }
 
        dir_f = JFFS2_INODE_INFO(dir_i);
-       down(&dir_f->sem);
+       mutex_lock(&dir_f->sem);
 
        rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
        rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
@@ -406,14 +429,14 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
        rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
 
-       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
+       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
 
        if (IS_ERR(fd)) {
-               /* dirent failed to write. Delete the inode normally 
+               /* dirent failed to write. Delete the inode normally
                   as if it were the final unlink() */
                jffs2_complete_reservation(c);
                jffs2_free_raw_dirent(rd);
-               up(&dir_f->sem);
+               mutex_unlock(&dir_f->sem);
                jffs2_clear_inode(inode);
                return PTR_ERR(fd);
        }
@@ -426,7 +449,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
           one if necessary. */
        jffs2_add_fd_to_list(c, fd, &dir_f->dents);
 
-       up(&dir_f->sem);
+       mutex_unlock(&dir_f->sem);
        jffs2_complete_reservation(c);
 
        d_instantiate(dentry, inode);
@@ -444,7 +467,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        struct jffs2_full_dnode *fn;
        struct jffs2_full_dirent *fd;
        int namelen;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen;
        int ret;
 
        mode |= S_IFDIR;
@@ -452,14 +475,15 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        ri = jffs2_alloc_raw_inode();
        if (!ri)
                return -ENOMEM;
-       
+
        c = JFFS2_SB_INFO(dir_i->i_sb);
 
-       /* Try to reserve enough space for both node and dirent. 
-        * Just the node will do for now, though 
+       /* Try to reserve enough space for both node and dirent.
+        * Just the node will do for now, though
         */
        namelen = dentry->d_name.len;
-       ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
+                                 JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -476,39 +500,55 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
 
        inode->i_op = &jffs2_dir_inode_operations;
        inode->i_fop = &jffs2_dir_operations;
-       /* Directories get nlink 2 at start */
-       inode->i_nlink = 2;
 
        f = JFFS2_INODE_INFO(inode);
 
+       /* Directories get nlink 2 at start */
+       inode->i_nlink = 2;
+       /* but ic->pino_nlink is the parent ino# */
+       f->inocache->pino_nlink = dir_i->i_ino;
+
        ri->data_crc = cpu_to_je32(0);
        ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
-       
-       fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
+
+       fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
 
        jffs2_free_raw_inode(ri);
 
        if (IS_ERR(fn)) {
                /* Eeek. Wave bye bye */
-               up(&f->sem);
+               mutex_unlock(&f->sem);
                jffs2_complete_reservation(c);
                jffs2_clear_inode(inode);
                return PTR_ERR(fn);
        }
-       /* No data here. Only a metadata node, which will be 
+       /* No data here. Only a metadata node, which will be
           obsoleted by the first data write
        */
        f->metadata = fn;
-       up(&f->sem);
+       mutex_unlock(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       ret = jffs2_init_security(inode, dir_i);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+       ret = jffs2_init_acl_post(inode);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
        if (ret) {
                /* Eep. */
                jffs2_clear_inode(inode);
                return ret;
        }
-       
+
        rd = jffs2_alloc_raw_dirent();
        if (!rd) {
                /* Argh. Now we treat it like a normal delete */
@@ -518,7 +558,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        }
 
        dir_f = JFFS2_INODE_INFO(dir_i);
-       down(&dir_f->sem);
+       mutex_lock(&dir_f->sem);
 
        rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
        rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
@@ -534,20 +574,20 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
        rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
 
-       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
-       
+       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
+
        if (IS_ERR(fd)) {
-               /* dirent failed to write. Delete the inode normally 
+               /* dirent failed to write. Delete the inode normally
                   as if it were the final unlink() */
                jffs2_complete_reservation(c);
                jffs2_free_raw_dirent(rd);
-               up(&dir_f->sem);
+               mutex_unlock(&dir_f->sem);
                jffs2_clear_inode(inode);
                return PTR_ERR(fd);
        }
 
        dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
-       dir_i->i_nlink++;
+       inc_nlink(dir_i);
 
        jffs2_free_raw_dirent(rd);
 
@@ -555,7 +595,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
           one if necessary. */
        jffs2_add_fd_to_list(c, fd, &dir_f->dents);
 
-       up(&dir_f->sem);
+       mutex_unlock(&dir_f->sem);
        jffs2_complete_reservation(c);
 
        d_instantiate(dentry, inode);
@@ -564,21 +604,29 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
 
 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
 {
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
+       struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
        struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
        struct jffs2_full_dirent *fd;
        int ret;
+       uint32_t now = get_seconds();
 
        for (fd = f->dents ; fd; fd = fd->next) {
                if (fd->ino)
                        return -ENOTEMPTY;
        }
-       ret = jffs2_unlink(dir_i, dentry);
-       if (!ret)
-               dir_i->i_nlink--;
+
+       ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
+                             dentry->d_name.len, f, now);
+       if (!ret) {
+               dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
+               clear_nlink(dentry->d_inode);
+               drop_nlink(dir_i);
+       }
        return ret;
 }
 
-static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mknod_arg_t rdev)
+static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
 {
        struct jffs2_inode_info *f, *dir_f;
        struct jffs2_sb_info *c;
@@ -588,30 +636,29 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        struct jffs2_full_dnode *fn;
        struct jffs2_full_dirent *fd;
        int namelen;
-       jint16_t dev;
+       union jffs2_device_node dev;
        int devlen = 0;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen;
        int ret;
 
-       if (!old_valid_dev(rdev))
+       if (!new_valid_dev(rdev))
                return -EINVAL;
 
        ri = jffs2_alloc_raw_inode();
        if (!ri)
                return -ENOMEM;
-       
+
        c = JFFS2_SB_INFO(dir_i->i_sb);
-       
-       if (S_ISBLK(mode) || S_ISCHR(mode)) {
-               dev = cpu_to_je16(old_encode_dev(rdev));
-               devlen = sizeof(dev);
-       }
-       
-       /* Try to reserve enough space for both node and dirent. 
-        * Just the node will do for now, though 
+
+       if (S_ISBLK(mode) || S_ISCHR(mode))
+               devlen = jffs2_encode_dev(&dev, rdev);
+
+       /* Try to reserve enough space for both node and dirent.
+        * Just the node will do for now, though
         */
        namelen = dentry->d_name.len;
-       ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -637,26 +684,39 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        ri->compr = JFFS2_COMPR_NONE;
        ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
        ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
-       
-       fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
+
+       fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
 
        jffs2_free_raw_inode(ri);
 
        if (IS_ERR(fn)) {
                /* Eeek. Wave bye bye */
-               up(&f->sem);
+               mutex_unlock(&f->sem);
                jffs2_complete_reservation(c);
                jffs2_clear_inode(inode);
                return PTR_ERR(fn);
        }
-       /* No data here. Only a metadata node, which will be 
+       /* No data here. Only a metadata node, which will be
           obsoleted by the first data write
        */
        f->metadata = fn;
-       up(&f->sem);
+       mutex_unlock(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       ret = jffs2_init_security(inode, dir_i);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+       ret = jffs2_init_acl_post(inode);
+       if (ret) {
+               jffs2_clear_inode(inode);
+               return ret;
+       }
+
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
        if (ret) {
                /* Eep. */
                jffs2_clear_inode(inode);
@@ -672,7 +732,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        }
 
        dir_f = JFFS2_INODE_INFO(dir_i);
-       down(&dir_f->sem);
+       mutex_lock(&dir_f->sem);
 
        rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
        rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
@@ -691,14 +751,14 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
        rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
 
-       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
-       
+       fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
+
        if (IS_ERR(fd)) {
-               /* dirent failed to write. Delete the inode normally 
+               /* dirent failed to write. Delete the inode normally
                   as if it were the final unlink() */
                jffs2_complete_reservation(c);
                jffs2_free_raw_dirent(rd);
-               up(&dir_f->sem);
+               mutex_unlock(&dir_f->sem);
                jffs2_clear_inode(inode);
                return PTR_ERR(fd);
        }
@@ -711,7 +771,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
           one if necessary. */
        jffs2_add_fd_to_list(c, fd, &dir_f->dents);
 
-       up(&dir_f->sem);
+       mutex_unlock(&dir_f->sem);
        jffs2_complete_reservation(c);
 
        d_instantiate(dentry, inode);
@@ -720,14 +780,15 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
 }
 
 static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
-                        struct inode *new_dir_i, struct dentry *new_dentry)
+                        struct inode *new_dir_i, struct dentry *new_dentry)
 {
        int ret;
        struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
        struct jffs2_inode_info *victim_f = NULL;
        uint8_t type;
+       uint32_t now;
 
-       /* The VFS will check for us and prevent trying to rename a 
+       /* The VFS will check for us and prevent trying to rename a
         * file over a directory and vice versa, but if it's a directory,
         * the VFS can't check whether the victim is empty. The filesystem
         * needs to do that for itself.
@@ -737,76 +798,83 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
                if (S_ISDIR(new_dentry->d_inode->i_mode)) {
                        struct jffs2_full_dirent *fd;
 
-                       down(&victim_f->sem);
+                       mutex_lock(&victim_f->sem);
                        for (fd = victim_f->dents; fd; fd = fd->next) {
                                if (fd->ino) {
-                                       up(&victim_f->sem);
+                                       mutex_unlock(&victim_f->sem);
                                        return -ENOTEMPTY;
                                }
                        }
-                       up(&victim_f->sem);
+                       mutex_unlock(&victim_f->sem);
                }
        }
 
        /* XXX: We probably ought to alloc enough space for
-          both nodes at the same time. Writing the new link, 
+          both nodes at the same time. Writing the new link,
           then getting -ENOSPC, is quite bad :)
        */
 
        /* Make a hard link */
-       
+
        /* XXX: This is ugly */
        type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
        if (!type) type = DT_REG;
 
-       ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), 
+       now = get_seconds();
+       ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
                            old_dentry->d_inode->i_ino, type,
-                           new_dentry->d_name.name, new_dentry->d_name.len);
+                           new_dentry->d_name.name, new_dentry->d_name.len, now);
 
        if (ret)
                return ret;
 
        if (victim_f) {
                /* There was a victim. Kill it off nicely */
-               new_dentry->d_inode->i_nlink--;
+               drop_nlink(new_dentry->d_inode);
                /* Don't oops if the victim was a dirent pointing to an
                   inode which didn't exist. */
                if (victim_f->inocache) {
-                       down(&victim_f->sem);
-                       victim_f->inocache->nlink--;
-                       up(&victim_f->sem);
+                       mutex_lock(&victim_f->sem);
+                       if (S_ISDIR(new_dentry->d_inode->i_mode))
+                               victim_f->inocache->pino_nlink = 0;
+                       else
+                               victim_f->inocache->pino_nlink--;
+                       mutex_unlock(&victim_f->sem);
                }
        }
 
-       /* If it was a directory we moved, and there was no victim, 
+       /* If it was a directory we moved, and there was no victim,
           increase i_nlink on its new parent */
        if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
-               new_dir_i->i_nlink++;
+               inc_nlink(new_dir_i);
 
        /* Unlink the original */
-       ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), 
-                     old_dentry->d_name.name, old_dentry->d_name.len, NULL);
+       ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
+                             old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
 
        /* We don't touch inode->i_nlink */
 
        if (ret) {
                /* Oh shit. We really ought to make a single node which can do both atomically */
                struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
-               down(&f->sem);
-               old_dentry->d_inode->i_nlink++;
-               if (f->inocache)
-                       f->inocache->nlink++;
-               up(&f->sem);
+               mutex_lock(&f->sem);
+               inc_nlink(old_dentry->d_inode);
+               if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
+                       f->inocache->pino_nlink++;
+               mutex_unlock(&f->sem);
 
                printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
                /* Might as well let the VFS know */
                d_instantiate(new_dentry, old_dentry->d_inode);
                atomic_inc(&old_dentry->d_inode->i_count);
+               new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
                return ret;
        }
 
        if (S_ISDIR(old_dentry->d_inode->i_mode))
-               old_dir_i->i_nlink--;
+               drop_nlink(old_dir_i);
+
+       new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
 
        return 0;
 }