Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
0006  *
0007  * Created by David Woodhouse <dwmw2@infradead.org>
0008  *
0009  * For licensing information, see the file 'LICENCE' in this directory.
0010  *
0011  */
0012 
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/slab.h>
0017 #include <linux/fs.h>
0018 #include <linux/crc32.h>
0019 #include <linux/jffs2.h>
0020 #include "jffs2_fs_i.h"
0021 #include "jffs2_fs_sb.h"
0022 #include <linux/time.h>
0023 #include "nodelist.h"
0024 
0025 static int jffs2_readdir (struct file *, struct dir_context *);
0026 
0027 static int jffs2_create (struct user_namespace *, struct inode *,
0028                  struct dentry *, umode_t, bool);
0029 static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
0030                     unsigned int);
0031 static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
0032 static int jffs2_unlink (struct inode *,struct dentry *);
0033 static int jffs2_symlink (struct user_namespace *, struct inode *,
0034               struct dentry *, const char *);
0035 static int jffs2_mkdir (struct user_namespace *, struct inode *,struct dentry *,
0036             umode_t);
0037 static int jffs2_rmdir (struct inode *,struct dentry *);
0038 static int jffs2_mknod (struct user_namespace *, struct inode *,struct dentry *,
0039             umode_t,dev_t);
0040 static int jffs2_rename (struct user_namespace *, struct inode *,
0041              struct dentry *, struct inode *, struct dentry *,
0042              unsigned int);
0043 
0044 const struct file_operations jffs2_dir_operations =
0045 {
0046     .read =     generic_read_dir,
0047     .iterate_shared=jffs2_readdir,
0048     .unlocked_ioctl=jffs2_ioctl,
0049     .fsync =    jffs2_fsync,
0050     .llseek =   generic_file_llseek,
0051 };
0052 
0053 
0054 const struct inode_operations jffs2_dir_inode_operations =
0055 {
0056     .create =   jffs2_create,
0057     .lookup =   jffs2_lookup,
0058     .link =     jffs2_link,
0059     .unlink =   jffs2_unlink,
0060     .symlink =  jffs2_symlink,
0061     .mkdir =    jffs2_mkdir,
0062     .rmdir =    jffs2_rmdir,
0063     .mknod =    jffs2_mknod,
0064     .rename =   jffs2_rename,
0065     .get_acl =  jffs2_get_acl,
0066     .set_acl =  jffs2_set_acl,
0067     .setattr =  jffs2_setattr,
0068     .listxattr =    jffs2_listxattr,
0069 };
0070 
0071 /***********************************************************************/
0072 
0073 
0074 /* We keep the dirent list sorted in increasing order of name hash,
0075    and we use the same hash function as the dentries. Makes this
0076    nice and simple
0077 */
0078 static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
0079                    unsigned int flags)
0080 {
0081     struct jffs2_inode_info *dir_f;
0082     struct jffs2_full_dirent *fd = NULL, *fd_list;
0083     uint32_t ino = 0;
0084     struct inode *inode = NULL;
0085     unsigned int nhash;
0086 
0087     jffs2_dbg(1, "jffs2_lookup()\n");
0088 
0089     if (target->d_name.len > JFFS2_MAX_NAME_LEN)
0090         return ERR_PTR(-ENAMETOOLONG);
0091 
0092     dir_f = JFFS2_INODE_INFO(dir_i);
0093 
0094     /* The 'nhash' on the fd_list is not the same as the dentry hash */
0095     nhash = full_name_hash(NULL, target->d_name.name, target->d_name.len);
0096 
0097     mutex_lock(&dir_f->sem);
0098 
0099     /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
0100     for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= nhash; fd_list = fd_list->next) {
0101         if (fd_list->nhash == nhash &&
0102             (!fd || fd_list->version > fd->version) &&
0103             strlen(fd_list->name) == target->d_name.len &&
0104             !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
0105             fd = fd_list;
0106         }
0107     }
0108     if (fd)
0109         ino = fd->ino;
0110     mutex_unlock(&dir_f->sem);
0111     if (ino) {
0112         inode = jffs2_iget(dir_i->i_sb, ino);
0113         if (IS_ERR(inode))
0114             pr_warn("iget() failed for ino #%u\n", ino);
0115     }
0116 
0117     return d_splice_alias(inode, target);
0118 }
0119 
0120 /***********************************************************************/
0121 
0122 
0123 static int jffs2_readdir(struct file *file, struct dir_context *ctx)
0124 {
0125     struct inode *inode = file_inode(file);
0126     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0127     struct jffs2_full_dirent *fd;
0128     unsigned long curofs = 1;
0129 
0130     jffs2_dbg(1, "jffs2_readdir() for dir_i #%lu\n", inode->i_ino);
0131 
0132     if (!dir_emit_dots(file, ctx))
0133         return 0;
0134 
0135     mutex_lock(&f->sem);
0136     for (fd = f->dents; fd; fd = fd->next) {
0137         curofs++;
0138         /* First loop: curofs = 2; pos = 2 */
0139         if (curofs < ctx->pos) {
0140             jffs2_dbg(2, "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
0141                   fd->name, fd->ino, fd->type, curofs, (unsigned long)ctx->pos);
0142             continue;
0143         }
0144         if (!fd->ino) {
0145             jffs2_dbg(2, "Skipping deletion dirent \"%s\"\n",
0146                   fd->name);
0147             ctx->pos++;
0148             continue;
0149         }
0150         jffs2_dbg(2, "Dirent %ld: \"%s\", ino #%u, type %d\n",
0151               (unsigned long)ctx->pos, fd->name, fd->ino, fd->type);
0152         if (!dir_emit(ctx, fd->name, strlen(fd->name), fd->ino, fd->type))
0153             break;
0154         ctx->pos++;
0155     }
0156     mutex_unlock(&f->sem);
0157     return 0;
0158 }
0159 
0160 /***********************************************************************/
0161 
0162 
0163 static int jffs2_create(struct user_namespace *mnt_userns, struct inode *dir_i,
0164             struct dentry *dentry, umode_t mode, bool excl)
0165 {
0166     struct jffs2_raw_inode *ri;
0167     struct jffs2_inode_info *f, *dir_f;
0168     struct jffs2_sb_info *c;
0169     struct inode *inode;
0170     int ret;
0171 
0172     ri = jffs2_alloc_raw_inode();
0173     if (!ri)
0174         return -ENOMEM;
0175 
0176     c = JFFS2_SB_INFO(dir_i->i_sb);
0177 
0178     jffs2_dbg(1, "%s()\n", __func__);
0179 
0180     inode = jffs2_new_inode(dir_i, mode, ri);
0181 
0182     if (IS_ERR(inode)) {
0183         jffs2_dbg(1, "jffs2_new_inode() failed\n");
0184         jffs2_free_raw_inode(ri);
0185         return PTR_ERR(inode);
0186     }
0187 
0188     inode->i_op = &jffs2_file_inode_operations;
0189     inode->i_fop = &jffs2_file_operations;
0190     inode->i_mapping->a_ops = &jffs2_file_address_operations;
0191     inode->i_mapping->nrpages = 0;
0192 
0193     f = JFFS2_INODE_INFO(inode);
0194     dir_f = JFFS2_INODE_INFO(dir_i);
0195 
0196     /* jffs2_do_create() will want to lock it, _after_ reserving
0197        space and taking c-alloc_sem. If we keep it locked here,
0198        lockdep gets unhappy (although it's a false positive;
0199        nothing else will be looking at this inode yet so there's
0200        no chance of AB-BA deadlock involving its f->sem). */
0201     mutex_unlock(&f->sem);
0202 
0203     ret = jffs2_do_create(c, dir_f, f, ri, &dentry->d_name);
0204     if (ret)
0205         goto fail;
0206 
0207     dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
0208 
0209     jffs2_free_raw_inode(ri);
0210 
0211     jffs2_dbg(1, "%s(): Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
0212           __func__, inode->i_ino, inode->i_mode, inode->i_nlink,
0213           f->inocache->pino_nlink, inode->i_mapping->nrpages);
0214 
0215     d_instantiate_new(dentry, inode);
0216     return 0;
0217 
0218  fail:
0219     iget_failed(inode);
0220     jffs2_free_raw_inode(ri);
0221     return ret;
0222 }
0223 
0224 /***********************************************************************/
0225 
0226 
0227 static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
0228 {
0229     struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
0230     struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
0231     struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode(dentry));
0232     int ret;
0233     uint32_t now = JFFS2_NOW();
0234 
0235     ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
0236                   dentry->d_name.len, dead_f, now);
0237     if (dead_f->inocache)
0238         set_nlink(d_inode(dentry), dead_f->inocache->pino_nlink);
0239     if (!ret)
0240         dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
0241     return ret;
0242 }
0243 /***********************************************************************/
0244 
0245 
0246 static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
0247 {
0248     struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_sb);
0249     struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
0250     struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
0251     int ret;
0252     uint8_t type;
0253     uint32_t now;
0254 
0255     /* Don't let people make hard links to bad inodes. */
0256     if (!f->inocache)
0257         return -EIO;
0258 
0259     if (d_is_dir(old_dentry))
0260         return -EPERM;
0261 
0262     /* XXX: This is ugly */
0263     type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
0264     if (!type) type = DT_REG;
0265 
0266     now = JFFS2_NOW();
0267     ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
0268 
0269     if (!ret) {
0270         mutex_lock(&f->sem);
0271         set_nlink(d_inode(old_dentry), ++f->inocache->pino_nlink);
0272         mutex_unlock(&f->sem);
0273         d_instantiate(dentry, d_inode(old_dentry));
0274         dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
0275         ihold(d_inode(old_dentry));
0276     }
0277     return ret;
0278 }
0279 
0280 /***********************************************************************/
0281 
0282 static int jffs2_symlink (struct user_namespace *mnt_userns, struct inode *dir_i,
0283               struct dentry *dentry, const char *target)
0284 {
0285     struct jffs2_inode_info *f, *dir_f;
0286     struct jffs2_sb_info *c;
0287     struct inode *inode;
0288     struct jffs2_raw_inode *ri;
0289     struct jffs2_raw_dirent *rd;
0290     struct jffs2_full_dnode *fn;
0291     struct jffs2_full_dirent *fd;
0292     int namelen;
0293     uint32_t alloclen;
0294     int ret, targetlen = strlen(target);
0295 
0296     /* FIXME: If you care. We'd need to use frags for the target
0297        if it grows much more than this */
0298     if (targetlen > 254)
0299         return -ENAMETOOLONG;
0300 
0301     ri = jffs2_alloc_raw_inode();
0302 
0303     if (!ri)
0304         return -ENOMEM;
0305 
0306     c = JFFS2_SB_INFO(dir_i->i_sb);
0307 
0308     /* Try to reserve enough space for both node and dirent.
0309      * Just the node will do for now, though
0310      */
0311     namelen = dentry->d_name.len;
0312     ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
0313                   ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
0314 
0315     if (ret) {
0316         jffs2_free_raw_inode(ri);
0317         return ret;
0318     }
0319 
0320     inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
0321 
0322     if (IS_ERR(inode)) {
0323         jffs2_free_raw_inode(ri);
0324         jffs2_complete_reservation(c);
0325         return PTR_ERR(inode);
0326     }
0327 
0328     inode->i_op = &jffs2_symlink_inode_operations;
0329 
0330     f = JFFS2_INODE_INFO(inode);
0331 
0332     inode->i_size = targetlen;
0333     ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
0334     ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
0335     ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
0336 
0337     ri->compr = JFFS2_COMPR_NONE;
0338     ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
0339     ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0340 
0341     fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
0342 
0343     jffs2_free_raw_inode(ri);
0344 
0345     if (IS_ERR(fn)) {
0346         /* Eeek. Wave bye bye */
0347         mutex_unlock(&f->sem);
0348         jffs2_complete_reservation(c);
0349         ret = PTR_ERR(fn);
0350         goto fail;
0351     }
0352 
0353     /* We use f->target field to store the target path. */
0354     f->target = kmemdup(target, targetlen + 1, GFP_KERNEL);
0355     if (!f->target) {
0356         pr_warn("Can't allocate %d bytes of memory\n", targetlen + 1);
0357         mutex_unlock(&f->sem);
0358         jffs2_complete_reservation(c);
0359         ret = -ENOMEM;
0360         goto fail;
0361     }
0362     inode->i_link = f->target;
0363 
0364     jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
0365           __func__, (char *)f->target);
0366 
0367     /* No data here. Only a metadata node, which will be
0368        obsoleted by the first data write
0369     */
0370     f->metadata = fn;
0371     mutex_unlock(&f->sem);
0372 
0373     jffs2_complete_reservation(c);
0374 
0375     ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
0376     if (ret)
0377         goto fail;
0378 
0379     ret = jffs2_init_acl_post(inode);
0380     if (ret)
0381         goto fail;
0382 
0383     ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
0384                   ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0385     if (ret)
0386         goto fail;
0387 
0388     rd = jffs2_alloc_raw_dirent();
0389     if (!rd) {
0390         /* Argh. Now we treat it like a normal delete */
0391         jffs2_complete_reservation(c);
0392         ret = -ENOMEM;
0393         goto fail;
0394     }
0395 
0396     dir_f = JFFS2_INODE_INFO(dir_i);
0397     mutex_lock(&dir_f->sem);
0398 
0399     rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0400     rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0401     rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
0402     rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0403 
0404     rd->pino = cpu_to_je32(dir_i->i_ino);
0405     rd->version = cpu_to_je32(++dir_f->highest_version);
0406     rd->ino = cpu_to_je32(inode->i_ino);
0407     rd->mctime = cpu_to_je32(JFFS2_NOW());
0408     rd->nsize = namelen;
0409     rd->type = DT_LNK;
0410     rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0411     rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
0412 
0413     fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
0414 
0415     if (IS_ERR(fd)) {
0416         /* dirent failed to write. Delete the inode normally
0417            as if it were the final unlink() */
0418         jffs2_complete_reservation(c);
0419         jffs2_free_raw_dirent(rd);
0420         mutex_unlock(&dir_f->sem);
0421         ret = PTR_ERR(fd);
0422         goto fail;
0423     }
0424 
0425     dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
0426 
0427     jffs2_free_raw_dirent(rd);
0428 
0429     /* Link the fd into the inode's list, obsoleting an old
0430        one if necessary. */
0431     jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0432 
0433     mutex_unlock(&dir_f->sem);
0434     jffs2_complete_reservation(c);
0435 
0436     d_instantiate_new(dentry, inode);
0437     return 0;
0438 
0439  fail:
0440     iget_failed(inode);
0441     return ret;
0442 }
0443 
0444 
0445 static int jffs2_mkdir (struct user_namespace *mnt_userns, struct inode *dir_i,
0446                 struct dentry *dentry, umode_t mode)
0447 {
0448     struct jffs2_inode_info *f, *dir_f;
0449     struct jffs2_sb_info *c;
0450     struct inode *inode;
0451     struct jffs2_raw_inode *ri;
0452     struct jffs2_raw_dirent *rd;
0453     struct jffs2_full_dnode *fn;
0454     struct jffs2_full_dirent *fd;
0455     int namelen;
0456     uint32_t alloclen;
0457     int ret;
0458 
0459     mode |= S_IFDIR;
0460 
0461     ri = jffs2_alloc_raw_inode();
0462     if (!ri)
0463         return -ENOMEM;
0464 
0465     c = JFFS2_SB_INFO(dir_i->i_sb);
0466 
0467     /* Try to reserve enough space for both node and dirent.
0468      * Just the node will do for now, though
0469      */
0470     namelen = dentry->d_name.len;
0471     ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
0472                   JFFS2_SUMMARY_INODE_SIZE);
0473 
0474     if (ret) {
0475         jffs2_free_raw_inode(ri);
0476         return ret;
0477     }
0478 
0479     inode = jffs2_new_inode(dir_i, mode, ri);
0480 
0481     if (IS_ERR(inode)) {
0482         jffs2_free_raw_inode(ri);
0483         jffs2_complete_reservation(c);
0484         return PTR_ERR(inode);
0485     }
0486 
0487     inode->i_op = &jffs2_dir_inode_operations;
0488     inode->i_fop = &jffs2_dir_operations;
0489 
0490     f = JFFS2_INODE_INFO(inode);
0491 
0492     /* Directories get nlink 2 at start */
0493     set_nlink(inode, 2);
0494     /* but ic->pino_nlink is the parent ino# */
0495     f->inocache->pino_nlink = dir_i->i_ino;
0496 
0497     ri->data_crc = cpu_to_je32(0);
0498     ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0499 
0500     fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
0501 
0502     jffs2_free_raw_inode(ri);
0503 
0504     if (IS_ERR(fn)) {
0505         /* Eeek. Wave bye bye */
0506         mutex_unlock(&f->sem);
0507         jffs2_complete_reservation(c);
0508         ret = PTR_ERR(fn);
0509         goto fail;
0510     }
0511     /* No data here. Only a metadata node, which will be
0512        obsoleted by the first data write
0513     */
0514     f->metadata = fn;
0515     mutex_unlock(&f->sem);
0516 
0517     jffs2_complete_reservation(c);
0518 
0519     ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
0520     if (ret)
0521         goto fail;
0522 
0523     ret = jffs2_init_acl_post(inode);
0524     if (ret)
0525         goto fail;
0526 
0527     ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
0528                   ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0529     if (ret)
0530         goto fail;
0531 
0532     rd = jffs2_alloc_raw_dirent();
0533     if (!rd) {
0534         /* Argh. Now we treat it like a normal delete */
0535         jffs2_complete_reservation(c);
0536         ret = -ENOMEM;
0537         goto fail;
0538     }
0539 
0540     dir_f = JFFS2_INODE_INFO(dir_i);
0541     mutex_lock(&dir_f->sem);
0542 
0543     rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0544     rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0545     rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
0546     rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0547 
0548     rd->pino = cpu_to_je32(dir_i->i_ino);
0549     rd->version = cpu_to_je32(++dir_f->highest_version);
0550     rd->ino = cpu_to_je32(inode->i_ino);
0551     rd->mctime = cpu_to_je32(JFFS2_NOW());
0552     rd->nsize = namelen;
0553     rd->type = DT_DIR;
0554     rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0555     rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
0556 
0557     fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
0558 
0559     if (IS_ERR(fd)) {
0560         /* dirent failed to write. Delete the inode normally
0561            as if it were the final unlink() */
0562         jffs2_complete_reservation(c);
0563         jffs2_free_raw_dirent(rd);
0564         mutex_unlock(&dir_f->sem);
0565         ret = PTR_ERR(fd);
0566         goto fail;
0567     }
0568 
0569     dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
0570     inc_nlink(dir_i);
0571 
0572     jffs2_free_raw_dirent(rd);
0573 
0574     /* Link the fd into the inode's list, obsoleting an old
0575        one if necessary. */
0576     jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0577 
0578     mutex_unlock(&dir_f->sem);
0579     jffs2_complete_reservation(c);
0580 
0581     d_instantiate_new(dentry, inode);
0582     return 0;
0583 
0584  fail:
0585     iget_failed(inode);
0586     return ret;
0587 }
0588 
0589 static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
0590 {
0591     struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
0592     struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
0593     struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(dentry));
0594     struct jffs2_full_dirent *fd;
0595     int ret;
0596     uint32_t now = JFFS2_NOW();
0597 
0598     mutex_lock(&f->sem);
0599     for (fd = f->dents ; fd; fd = fd->next) {
0600         if (fd->ino) {
0601             mutex_unlock(&f->sem);
0602             return -ENOTEMPTY;
0603         }
0604     }
0605     mutex_unlock(&f->sem);
0606 
0607     ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
0608                   dentry->d_name.len, f, now);
0609     if (!ret) {
0610         dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
0611         clear_nlink(d_inode(dentry));
0612         drop_nlink(dir_i);
0613     }
0614     return ret;
0615 }
0616 
0617 static int jffs2_mknod (struct user_namespace *mnt_userns, struct inode *dir_i,
0618                 struct dentry *dentry, umode_t mode, dev_t rdev)
0619 {
0620     struct jffs2_inode_info *f, *dir_f;
0621     struct jffs2_sb_info *c;
0622     struct inode *inode;
0623     struct jffs2_raw_inode *ri;
0624     struct jffs2_raw_dirent *rd;
0625     struct jffs2_full_dnode *fn;
0626     struct jffs2_full_dirent *fd;
0627     int namelen;
0628     union jffs2_device_node dev;
0629     int devlen = 0;
0630     uint32_t alloclen;
0631     int ret;
0632 
0633     ri = jffs2_alloc_raw_inode();
0634     if (!ri)
0635         return -ENOMEM;
0636 
0637     c = JFFS2_SB_INFO(dir_i->i_sb);
0638 
0639     if (S_ISBLK(mode) || S_ISCHR(mode))
0640         devlen = jffs2_encode_dev(&dev, rdev);
0641 
0642     /* Try to reserve enough space for both node and dirent.
0643      * Just the node will do for now, though
0644      */
0645     namelen = dentry->d_name.len;
0646     ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
0647                   ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
0648 
0649     if (ret) {
0650         jffs2_free_raw_inode(ri);
0651         return ret;
0652     }
0653 
0654     inode = jffs2_new_inode(dir_i, mode, ri);
0655 
0656     if (IS_ERR(inode)) {
0657         jffs2_free_raw_inode(ri);
0658         jffs2_complete_reservation(c);
0659         return PTR_ERR(inode);
0660     }
0661     inode->i_op = &jffs2_file_inode_operations;
0662     init_special_inode(inode, inode->i_mode, rdev);
0663 
0664     f = JFFS2_INODE_INFO(inode);
0665 
0666     ri->dsize = ri->csize = cpu_to_je32(devlen);
0667     ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
0668     ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
0669 
0670     ri->compr = JFFS2_COMPR_NONE;
0671     ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
0672     ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0673 
0674     fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
0675 
0676     jffs2_free_raw_inode(ri);
0677 
0678     if (IS_ERR(fn)) {
0679         /* Eeek. Wave bye bye */
0680         mutex_unlock(&f->sem);
0681         jffs2_complete_reservation(c);
0682         ret = PTR_ERR(fn);
0683         goto fail;
0684     }
0685     /* No data here. Only a metadata node, which will be
0686        obsoleted by the first data write
0687     */
0688     f->metadata = fn;
0689     mutex_unlock(&f->sem);
0690 
0691     jffs2_complete_reservation(c);
0692 
0693     ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
0694     if (ret)
0695         goto fail;
0696 
0697     ret = jffs2_init_acl_post(inode);
0698     if (ret)
0699         goto fail;
0700 
0701     ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
0702                   ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
0703     if (ret)
0704         goto fail;
0705 
0706     rd = jffs2_alloc_raw_dirent();
0707     if (!rd) {
0708         /* Argh. Now we treat it like a normal delete */
0709         jffs2_complete_reservation(c);
0710         ret = -ENOMEM;
0711         goto fail;
0712     }
0713 
0714     dir_f = JFFS2_INODE_INFO(dir_i);
0715     mutex_lock(&dir_f->sem);
0716 
0717     rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0718     rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
0719     rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
0720     rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
0721 
0722     rd->pino = cpu_to_je32(dir_i->i_ino);
0723     rd->version = cpu_to_je32(++dir_f->highest_version);
0724     rd->ino = cpu_to_je32(inode->i_ino);
0725     rd->mctime = cpu_to_je32(JFFS2_NOW());
0726     rd->nsize = namelen;
0727 
0728     /* XXX: This is ugly. */
0729     rd->type = (mode & S_IFMT) >> 12;
0730 
0731     rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
0732     rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
0733 
0734     fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
0735 
0736     if (IS_ERR(fd)) {
0737         /* dirent failed to write. Delete the inode normally
0738            as if it were the final unlink() */
0739         jffs2_complete_reservation(c);
0740         jffs2_free_raw_dirent(rd);
0741         mutex_unlock(&dir_f->sem);
0742         ret = PTR_ERR(fd);
0743         goto fail;
0744     }
0745 
0746     dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
0747 
0748     jffs2_free_raw_dirent(rd);
0749 
0750     /* Link the fd into the inode's list, obsoleting an old
0751        one if necessary. */
0752     jffs2_add_fd_to_list(c, fd, &dir_f->dents);
0753 
0754     mutex_unlock(&dir_f->sem);
0755     jffs2_complete_reservation(c);
0756 
0757     d_instantiate_new(dentry, inode);
0758     return 0;
0759 
0760  fail:
0761     iget_failed(inode);
0762     return ret;
0763 }
0764 
0765 static int jffs2_rename (struct user_namespace *mnt_userns,
0766              struct inode *old_dir_i, struct dentry *old_dentry,
0767              struct inode *new_dir_i, struct dentry *new_dentry,
0768              unsigned int flags)
0769 {
0770     int ret;
0771     struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
0772     struct jffs2_inode_info *victim_f = NULL;
0773     uint8_t type;
0774     uint32_t now;
0775 
0776     if (flags & ~RENAME_NOREPLACE)
0777         return -EINVAL;
0778 
0779     /* The VFS will check for us and prevent trying to rename a
0780      * file over a directory and vice versa, but if it's a directory,
0781      * the VFS can't check whether the victim is empty. The filesystem
0782      * needs to do that for itself.
0783      */
0784     if (d_really_is_positive(new_dentry)) {
0785         victim_f = JFFS2_INODE_INFO(d_inode(new_dentry));
0786         if (d_is_dir(new_dentry)) {
0787             struct jffs2_full_dirent *fd;
0788 
0789             mutex_lock(&victim_f->sem);
0790             for (fd = victim_f->dents; fd; fd = fd->next) {
0791                 if (fd->ino) {
0792                     mutex_unlock(&victim_f->sem);
0793                     return -ENOTEMPTY;
0794                 }
0795             }
0796             mutex_unlock(&victim_f->sem);
0797         }
0798     }
0799 
0800     /* XXX: We probably ought to alloc enough space for
0801        both nodes at the same time. Writing the new link,
0802        then getting -ENOSPC, is quite bad :)
0803     */
0804 
0805     /* Make a hard link */
0806 
0807     /* XXX: This is ugly */
0808     type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
0809     if (!type) type = DT_REG;
0810 
0811     now = JFFS2_NOW();
0812     ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
0813                 d_inode(old_dentry)->i_ino, type,
0814                 new_dentry->d_name.name, new_dentry->d_name.len, now);
0815 
0816     if (ret)
0817         return ret;
0818 
0819     if (victim_f) {
0820         /* There was a victim. Kill it off nicely */
0821         if (d_is_dir(new_dentry))
0822             clear_nlink(d_inode(new_dentry));
0823         else
0824             drop_nlink(d_inode(new_dentry));
0825         /* Don't oops if the victim was a dirent pointing to an
0826            inode which didn't exist. */
0827         if (victim_f->inocache) {
0828             mutex_lock(&victim_f->sem);
0829             if (d_is_dir(new_dentry))
0830                 victim_f->inocache->pino_nlink = 0;
0831             else
0832                 victim_f->inocache->pino_nlink--;
0833             mutex_unlock(&victim_f->sem);
0834         }
0835     }
0836 
0837     /* If it was a directory we moved, and there was no victim,
0838        increase i_nlink on its new parent */
0839     if (d_is_dir(old_dentry) && !victim_f)
0840         inc_nlink(new_dir_i);
0841 
0842     /* Unlink the original */
0843     ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
0844                   old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
0845 
0846     /* We don't touch inode->i_nlink */
0847 
0848     if (ret) {
0849         /* Oh shit. We really ought to make a single node which can do both atomically */
0850         struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
0851         mutex_lock(&f->sem);
0852         inc_nlink(d_inode(old_dentry));
0853         if (f->inocache && !d_is_dir(old_dentry))
0854             f->inocache->pino_nlink++;
0855         mutex_unlock(&f->sem);
0856 
0857         pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
0858               __func__, ret);
0859         /*
0860          * We can't keep the target in dcache after that.
0861          * For one thing, we can't afford dentry aliases for directories.
0862          * For another, if there was a victim, we _can't_ set new inode
0863          * for that sucker and we have to trigger mount eviction - the
0864          * caller won't do it on its own since we are returning an error.
0865          */
0866         d_invalidate(new_dentry);
0867         new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
0868         return ret;
0869     }
0870 
0871     if (d_is_dir(old_dentry))
0872         drop_nlink(old_dir_i);
0873 
0874     new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
0875 
0876     return 0;
0877 }
0878