Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[safe/jmp/linux-2.6] / fs / jffs2 / dir.c
index f92840a..f948f7e 100644 (file)
@@ -1,24 +1,21 @@
 /*
  * 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.90 2005/11/07 11:14:39 gleixner 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"
 
@@ -35,7 +32,7 @@ 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,dev_t);
 static int jffs2_rename (struct inode *, struct dentry *,
-                        struct inode *, struct dentry *);
+                        struct inode *, struct dentry *);
 
 const struct file_operations jffs2_dir_operations =
 {
@@ -46,7 +43,7 @@ const struct file_operations jffs2_dir_operations =
 };
 
 
-struct inode_operations jffs2_dir_inode_operations =
+const struct inode_operations jffs2_dir_inode_operations =
 {
        .create =       jffs2_create,
        .lookup =       jffs2_lookup,
@@ -57,7 +54,12 @@ struct inode_operations jffs2_dir_inode_operations =
        .rmdir =        jffs2_rmdir,
        .mknod =        jffs2_mknod,
        .rename =       jffs2_rename,
+       .permission =   jffs2_permission,
        .setattr =      jffs2_setattr,
+       .setxattr =     jffs2_setxattr,
+       .getxattr =     jffs2_getxattr,
+       .listxattr =    jffs2_listxattr,
+       .removexattr =  jffs2_removexattr
 };
 
 /***********************************************************************/
@@ -99,10 +101,10 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
                ino = fd->ino;
        up(&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);
                }
        }
 
@@ -118,11 +120,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);
@@ -136,7 +138,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;
@@ -208,13 +210,8 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
 
        ret = jffs2_do_create(c, dir_f, f, ri,
                              dentry->d_name.name, dentry->d_name.len);
-
-       if (ret) {
-               make_bad_inode(inode);
-               iput(inode);
-               jffs2_free_raw_inode(ri);
-               return ret;
-       }
+       if (ret)
+               goto fail;
 
        dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
 
@@ -224,6 +221,12 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
        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));
        return 0;
+
+ fail:
+       make_bad_inode(inode);
+       iput(inode);
+       jffs2_free_raw_inode(ri);
+       return ret;
 }
 
 /***********************************************************************/
@@ -294,7 +297,7 @@ 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
@@ -313,8 +316,8 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
         * 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, JFFS2_SUMMARY_INODE_SIZE);
+       ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -342,7 +345,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        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);
 
@@ -374,8 +377,20 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
        up(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
-                               ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
+
+       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);
@@ -407,7 +422,7 @@ 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
@@ -445,7 +460,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;
@@ -460,8 +475,8 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
         * 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,
-                               JFFS2_SUMMARY_INODE_SIZE);
+       ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
+                                 JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -486,7 +501,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);
+       fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
 
        jffs2_free_raw_inode(ri);
 
@@ -504,8 +519,20 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        up(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
-                               ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
+
+       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);
@@ -537,7 +564,7 @@ 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
@@ -550,7 +577,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
        }
 
        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);
 
@@ -577,7 +604,7 @@ static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
        }
        ret = jffs2_unlink(dir_i, dentry);
        if (!ret)
-               dir_i->i_nlink--;
+               drop_nlink(dir_i);
        return ret;
 }
 
@@ -591,12 +618,12 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
        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();
@@ -605,17 +632,15 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
 
        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);
-       }
+       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, JFFS2_SUMMARY_INODE_SIZE);
+       ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
+                                 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
 
        if (ret) {
                jffs2_free_raw_inode(ri);
@@ -642,7 +667,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
        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);
 
@@ -660,8 +685,20 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
        up(&f->sem);
 
        jffs2_complete_reservation(c);
-       ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
-                               ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
+
+       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);
@@ -696,7 +733,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
        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
@@ -725,7 +762,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
 }
 
 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);
@@ -775,7 +812,7 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
 
        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) {
@@ -788,7 +825,7 @@ 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,
           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),
@@ -800,7 +837,7 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
                /* 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++;
+               inc_nlink(old_dentry->d_inode);
                if (f->inocache)
                        f->inocache->nlink++;
                up(&f->sem);
@@ -814,7 +851,7 @@ static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
        }
 
        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);