Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / jffs2 / dir.c
index 757306f..8bc7a50 100644 (file)
@@ -7,7 +7,7 @@
  *
  * For licensing information, see the file 'LICENCE' in this directory.
  *
- * $Id: dir.c,v 1.84 2004/11/16 20:36:11 dwmw2 Exp $
+ * $Id: dir.c,v 1.90 2005/11/07 11:14:39 gleixner Exp $
  *
  */
 
 #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,11 +33,11 @@ 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 file_operations jffs2_dir_operations =
+const struct file_operations jffs2_dir_operations =
 {
        .read =         generic_read_dir,
        .readdir =      jffs2_readdir,
@@ -58,8 +48,8 @@ struct file_operations jffs2_dir_operations =
 
 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,
@@ -74,7 +64,7 @@ struct inode_operations jffs2_dir_inode_operations =
 
 
 /* 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,
@@ -95,7 +85,7 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
 
        /* 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)) {
@@ -157,7 +147,7 @@ static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
                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;
                }
@@ -192,7 +182,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,7 +203,7 @@ 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, 
+       ret = jffs2_do_create(c, dir_f, f, ri,
                              dentry->d_name.name, dentry->d_name.len);
 
        if (ret) {
@@ -242,11 +232,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;
+       if (!ret)
+               dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
        return ret;
 }
 /***********************************************************************/
@@ -259,6 +252,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 +265,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);
                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;
@@ -296,25 +292,26 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        struct jffs2_full_dirent *fd;
        int namelen;
        uint32_t alloclen, phys_ofs;
-       int ret;
+       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 (strlen(target) > 254)
+       if (targetlen > 254)
                return -EINVAL;
 
        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) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen,
+                               ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -333,16 +330,16 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
 
        f = JFFS2_INODE_INFO(inode);
 
-       inode->i_size = strlen(target);
+       inode->i_size = targetlen;
        ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
        ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
        ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
 
        ri->compr = JFFS2_COMPR_NONE;
-       ri->data_crc = cpu_to_je32(crc32(0, target, strlen(target)));
+       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, strlen(target), phys_ofs, ALLOC_NORMAL);
+
+       fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL);
 
        jffs2_free_raw_inode(ri);
 
@@ -353,14 +350,29 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
                jffs2_clear_inode(inode);
                return PTR_ERR(fn);
        }
-       /* No data here. Only a metadata node, which will be 
+
+       /* 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);
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return -ENOMEM;
+       }
+
+       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
           obsoleted by the first data write
        */
        f->metadata = fn;
        up(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
+                               ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
        if (ret) {
                /* Eep. */
                jffs2_clear_inode(inode);
@@ -395,7 +407,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, 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);
@@ -438,14 +450,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), &phys_ofs, &alloclen, ALLOC_NORMAL,
+                               JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -469,7 +482,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
 
        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);
 
        jffs2_free_raw_inode(ri);
@@ -481,20 +494,21 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
                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);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &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 */
@@ -521,9 +535,9 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        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);
-       
+
        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);
@@ -564,7 +578,7 @@ static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
        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;
@@ -585,19 +599,20 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        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 
+
+       /* 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, &phys_ofs, &alloclen,
+                               ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -623,7 +638,7 @@ 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);
 
        jffs2_free_raw_inode(ri);
@@ -635,14 +650,15 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
                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);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
+                               ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
        if (ret) {
                /* Eep. */
                jffs2_clear_inode(inode);
@@ -678,9 +694,9 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, mk
        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);
-       
+
        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);
@@ -712,8 +728,9 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
        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.
@@ -735,19 +752,20 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
        }
 
        /* 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;
@@ -764,14 +782,14 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
                }
        }
 
-       /* 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++;
 
        /* 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 */
 
@@ -788,12 +806,15 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
                /* 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--;
 
+       new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
+
        return 0;
 }