11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 141da177e4SLinus Torvalds #include <linux/fs.h> 151da177e4SLinus Torvalds #include <linux/crc32.h> 161da177e4SLinus Torvalds #include <linux/jffs2.h> 17cbb9a561SDavid Woodhouse #include "jffs2_fs_i.h" 18cbb9a561SDavid Woodhouse #include "jffs2_fs_sb.h" 191da177e4SLinus Torvalds #include <linux/time.h> 201da177e4SLinus Torvalds #include "nodelist.h" 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds static int jffs2_readdir (struct file *, void *, filldir_t); 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds static int jffs2_create (struct inode *,struct dentry *,int, 251da177e4SLinus Torvalds struct nameidata *); 261da177e4SLinus Torvalds static struct dentry *jffs2_lookup (struct inode *,struct dentry *, 271da177e4SLinus Torvalds struct nameidata *); 281da177e4SLinus Torvalds static int jffs2_link (struct dentry *,struct inode *,struct dentry *); 291da177e4SLinus Torvalds static int jffs2_unlink (struct inode *,struct dentry *); 301da177e4SLinus Torvalds static int jffs2_symlink (struct inode *,struct dentry *,const char *); 311da177e4SLinus Torvalds static int jffs2_mkdir (struct inode *,struct dentry *,int); 321da177e4SLinus Torvalds static int jffs2_rmdir (struct inode *,struct dentry *); 33265489f0SDavid Woodhouse static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t); 341da177e4SLinus Torvalds static int jffs2_rename (struct inode *, struct dentry *, 351da177e4SLinus Torvalds struct inode *, struct dentry *); 361da177e4SLinus Torvalds 374b6f5d20SArjan van de Ven const struct file_operations jffs2_dir_operations = 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds .read = generic_read_dir, 401da177e4SLinus Torvalds .readdir = jffs2_readdir, 411da177e4SLinus Torvalds .ioctl = jffs2_ioctl, 421da177e4SLinus Torvalds .fsync = jffs2_fsync 431da177e4SLinus Torvalds }; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds 4692e1d5beSArjan van de Ven const struct inode_operations jffs2_dir_inode_operations = 471da177e4SLinus Torvalds { 48265489f0SDavid Woodhouse .create = jffs2_create, 49265489f0SDavid Woodhouse .lookup = jffs2_lookup, 501da177e4SLinus Torvalds .link = jffs2_link, 511da177e4SLinus Torvalds .unlink = jffs2_unlink, 521da177e4SLinus Torvalds .symlink = jffs2_symlink, 531da177e4SLinus Torvalds .mkdir = jffs2_mkdir, 541da177e4SLinus Torvalds .rmdir = jffs2_rmdir, 551da177e4SLinus Torvalds .mknod = jffs2_mknod, 561da177e4SLinus Torvalds .rename = jffs2_rename, 57aa98d7cfSKaiGai Kohei .permission = jffs2_permission, 581da177e4SLinus Torvalds .setattr = jffs2_setattr, 59aa98d7cfSKaiGai Kohei .setxattr = jffs2_setxattr, 60aa98d7cfSKaiGai Kohei .getxattr = jffs2_getxattr, 61aa98d7cfSKaiGai Kohei .listxattr = jffs2_listxattr, 62aa98d7cfSKaiGai Kohei .removexattr = jffs2_removexattr 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /***********************************************************************/ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* We keep the dirent list sorted in increasing order of name hash, 691da177e4SLinus Torvalds and we use the same hash function as the dentries. Makes this 701da177e4SLinus Torvalds nice and simple 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target, 731da177e4SLinus Torvalds struct nameidata *nd) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds struct jffs2_inode_info *dir_f; 761da177e4SLinus Torvalds struct jffs2_sb_info *c; 771da177e4SLinus Torvalds struct jffs2_full_dirent *fd = NULL, *fd_list; 781da177e4SLinus Torvalds uint32_t ino = 0; 791da177e4SLinus Torvalds struct inode *inode = NULL; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_lookup()\n")); 821da177e4SLinus Torvalds 83373d5e71SRichard Purdie if (target->d_name.len > JFFS2_MAX_NAME_LEN) 84373d5e71SRichard Purdie return ERR_PTR(-ENAMETOOLONG); 85373d5e71SRichard Purdie 861da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 871da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 881da177e4SLinus Torvalds 89ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */ 921da177e4SLinus Torvalds for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) { 931da177e4SLinus Torvalds if (fd_list->nhash == target->d_name.hash && 941da177e4SLinus Torvalds (!fd || fd_list->version > fd->version) && 951da177e4SLinus Torvalds strlen(fd_list->name) == target->d_name.len && 961da177e4SLinus Torvalds !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) { 971da177e4SLinus Torvalds fd = fd_list; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds if (fd) 1011da177e4SLinus Torvalds ino = fd->ino; 102ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 1031da177e4SLinus Torvalds if (ino) { 1045451f79fSDavid Howells inode = jffs2_iget(dir_i->i_sb, ino); 1055451f79fSDavid Howells if (IS_ERR(inode)) { 1061da177e4SLinus Torvalds printk(KERN_WARNING "iget() failed for ino #%u\n", ino); 1075451f79fSDavid Howells return ERR_CAST(inode); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds d_add(target, inode); 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds return NULL; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /***********************************************************************/ 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds struct jffs2_inode_info *f; 1221da177e4SLinus Torvalds struct jffs2_sb_info *c; 123ec2e203cSJosef Sipek struct inode *inode = filp->f_path.dentry->d_inode; 1241da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 1251da177e4SLinus Torvalds unsigned long offset, curofs; 1261da177e4SLinus Torvalds 127ec2e203cSJosef Sipek D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_path.dentry->d_inode->i_ino)); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 1301da177e4SLinus Torvalds c = JFFS2_SB_INFO(inode->i_sb); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds offset = filp->f_pos; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds if (offset == 0) { 1351da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino)); 1361da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 1371da177e4SLinus Torvalds goto out; 1381da177e4SLinus Torvalds offset++; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds if (offset == 1) { 141ec2e203cSJosef Sipek unsigned long pino = parent_ino(filp->f_path.dentry); 1421da177e4SLinus Torvalds D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino)); 1431da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0) 1441da177e4SLinus Torvalds goto out; 1451da177e4SLinus Torvalds offset++; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds curofs=1; 149ced22070SDavid Woodhouse mutex_lock(&f->sem); 1501da177e4SLinus Torvalds for (fd = f->dents; fd; fd = fd->next) { 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds curofs++; 1531da177e4SLinus Torvalds /* First loop: curofs = 2; offset = 2 */ 1541da177e4SLinus Torvalds if (curofs < offset) { 1551da177e4SLinus Torvalds D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n", 1561da177e4SLinus Torvalds fd->name, fd->ino, fd->type, curofs, offset)); 1571da177e4SLinus Torvalds continue; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds if (!fd->ino) { 1601da177e4SLinus Torvalds D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name)); 1611da177e4SLinus Torvalds offset++; 1621da177e4SLinus Torvalds continue; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type)); 1651da177e4SLinus Torvalds if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0) 1661da177e4SLinus Torvalds break; 1671da177e4SLinus Torvalds offset++; 1681da177e4SLinus Torvalds } 169ced22070SDavid Woodhouse mutex_unlock(&f->sem); 1701da177e4SLinus Torvalds out: 1711da177e4SLinus Torvalds filp->f_pos = offset; 1721da177e4SLinus Torvalds return 0; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /***********************************************************************/ 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode, 1791da177e4SLinus Torvalds struct nameidata *nd) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 1821da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 1831da177e4SLinus Torvalds struct jffs2_sb_info *c; 1841da177e4SLinus Torvalds struct inode *inode; 1851da177e4SLinus Torvalds int ret; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 1881da177e4SLinus Torvalds if (!ri) 1891da177e4SLinus Torvalds return -ENOMEM; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_create()\n")); 1941da177e4SLinus Torvalds 195cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, mode, ri); 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds if (IS_ERR(inode)) { 1981da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n")); 1991da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 2001da177e4SLinus Torvalds return PTR_ERR(inode); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds inode->i_op = &jffs2_file_inode_operations; 2041da177e4SLinus Torvalds inode->i_fop = &jffs2_file_operations; 2051da177e4SLinus Torvalds inode->i_mapping->a_ops = &jffs2_file_address_operations; 2061da177e4SLinus Torvalds inode->i_mapping->nrpages = 0; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 2091da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 2101da177e4SLinus Torvalds 211590fe34cSDavid Woodhouse /* jffs2_do_create() will want to lock it, _after_ reserving 212590fe34cSDavid Woodhouse space and taking c-alloc_sem. If we keep it locked here, 213590fe34cSDavid Woodhouse lockdep gets unhappy (although it's a false positive; 214590fe34cSDavid Woodhouse nothing else will be looking at this inode yet so there's 215590fe34cSDavid Woodhouse no chance of AB-BA deadlock involving its f->sem). */ 216590fe34cSDavid Woodhouse mutex_unlock(&f->sem); 217590fe34cSDavid Woodhouse 2181da177e4SLinus Torvalds ret = jffs2_do_create(c, dir_f, f, ri, 2191da177e4SLinus Torvalds dentry->d_name.name, dentry->d_name.len); 220aa98d7cfSKaiGai Kohei if (ret) 221aa98d7cfSKaiGai Kohei goto fail; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime)); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 2261da177e4SLinus Torvalds d_instantiate(dentry, inode); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n", 229*27c72b04SDavid Woodhouse inode->i_ino, inode->i_mode, inode->i_nlink, 230*27c72b04SDavid Woodhouse f->inocache->pino_nlink, inode->i_mapping->nrpages)); 2311da177e4SLinus Torvalds return 0; 232aa98d7cfSKaiGai Kohei 233aa98d7cfSKaiGai Kohei fail: 234aa98d7cfSKaiGai Kohei make_bad_inode(inode); 235aa98d7cfSKaiGai Kohei iput(inode); 236aa98d7cfSKaiGai Kohei jffs2_free_raw_inode(ri); 237aa98d7cfSKaiGai Kohei return ret; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /***********************************************************************/ 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb); 2461da177e4SLinus Torvalds struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 2471da177e4SLinus Torvalds struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode); 2481da177e4SLinus Torvalds int ret; 2493a69e0cdSArtem B. Bityutskiy uint32_t now = get_seconds(); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, 2523a69e0cdSArtem B. Bityutskiy dentry->d_name.len, dead_f, now); 2531da177e4SLinus Torvalds if (dead_f->inocache) 254*27c72b04SDavid Woodhouse dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink; 2553a69e0cdSArtem B. Bityutskiy if (!ret) 2563a69e0cdSArtem B. Bityutskiy dir_i->i_mtime = dir_i->i_ctime = ITIME(now); 2571da177e4SLinus Torvalds return ret; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds /***********************************************************************/ 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb); 2651da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); 2661da177e4SLinus Torvalds struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 2671da177e4SLinus Torvalds int ret; 2681da177e4SLinus Torvalds uint8_t type; 2693a69e0cdSArtem B. Bityutskiy uint32_t now; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* Don't let people make hard links to bad inodes. */ 2721da177e4SLinus Torvalds if (!f->inocache) 2731da177e4SLinus Torvalds return -EIO; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode)) 2761da177e4SLinus Torvalds return -EPERM; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* XXX: This is ugly */ 2791da177e4SLinus Torvalds type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; 2801da177e4SLinus Torvalds if (!type) type = DT_REG; 2811da177e4SLinus Torvalds 2823a69e0cdSArtem B. Bityutskiy now = get_seconds(); 2833a69e0cdSArtem B. Bityutskiy ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds if (!ret) { 286ced22070SDavid Woodhouse mutex_lock(&f->sem); 287*27c72b04SDavid Woodhouse old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink; 288ced22070SDavid Woodhouse mutex_unlock(&f->sem); 2891da177e4SLinus Torvalds d_instantiate(dentry, old_dentry->d_inode); 2903a69e0cdSArtem B. Bityutskiy dir_i->i_mtime = dir_i->i_ctime = ITIME(now); 2911da177e4SLinus Torvalds atomic_inc(&old_dentry->d_inode->i_count); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds return ret; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /***********************************************************************/ 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 3011da177e4SLinus Torvalds struct jffs2_sb_info *c; 3021da177e4SLinus Torvalds struct inode *inode; 3031da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 3041da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 3051da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 3061da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 3071da177e4SLinus Torvalds int namelen; 3089fe4854cSDavid Woodhouse uint32_t alloclen; 30932f1a95dSArtem B. Bityuckiy int ret, targetlen = strlen(target); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* FIXME: If you care. We'd need to use frags for the target 3121da177e4SLinus Torvalds if it grows much more than this */ 31332f1a95dSArtem B. Bityuckiy if (targetlen > 254) 3141da177e4SLinus Torvalds return -EINVAL; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (!ri) 3191da177e4SLinus Torvalds return -ENOMEM; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 3241da177e4SLinus Torvalds * Just the node will do for now, though 3251da177e4SLinus Torvalds */ 3261da177e4SLinus Torvalds namelen = dentry->d_name.len; 3279fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen, 328e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds if (ret) { 3311da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3321da177e4SLinus Torvalds return ret; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 335cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds if (IS_ERR(inode)) { 3381da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3391da177e4SLinus Torvalds jffs2_complete_reservation(c); 3401da177e4SLinus Torvalds return PTR_ERR(inode); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds inode->i_op = &jffs2_symlink_inode_operations; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 3461da177e4SLinus Torvalds 34732f1a95dSArtem B. Bityuckiy inode->i_size = targetlen; 3481da177e4SLinus Torvalds ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size); 3491da177e4SLinus Torvalds ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size); 3501da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds ri->compr = JFFS2_COMPR_NONE; 35332f1a95dSArtem B. Bityuckiy ri->data_crc = cpu_to_je32(crc32(0, target, targetlen)); 3541da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 3551da177e4SLinus Torvalds 3569fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds if (IS_ERR(fn)) { 3611da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 362ced22070SDavid Woodhouse mutex_unlock(&f->sem); 3631da177e4SLinus Torvalds jffs2_complete_reservation(c); 3641da177e4SLinus Torvalds jffs2_clear_inode(inode); 3651da177e4SLinus Torvalds return PTR_ERR(fn); 3661da177e4SLinus Torvalds } 36732f1a95dSArtem B. Bityuckiy 3682b79adccSArtem B. Bityutskiy /* We use f->target field to store the target path. */ 3692b79adccSArtem B. Bityutskiy f->target = kmalloc(targetlen + 1, GFP_KERNEL); 3702b79adccSArtem B. Bityutskiy if (!f->target) { 37132f1a95dSArtem B. Bityuckiy printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1); 372ced22070SDavid Woodhouse mutex_unlock(&f->sem); 37332f1a95dSArtem B. Bityuckiy jffs2_complete_reservation(c); 37432f1a95dSArtem B. Bityuckiy jffs2_clear_inode(inode); 37532f1a95dSArtem B. Bityuckiy return -ENOMEM; 37632f1a95dSArtem B. Bityuckiy } 37732f1a95dSArtem B. Bityuckiy 3782b79adccSArtem B. Bityutskiy memcpy(f->target, target, targetlen + 1); 3792b79adccSArtem B. Bityutskiy D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target)); 38032f1a95dSArtem B. Bityuckiy 3811da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 3821da177e4SLinus Torvalds obsoleted by the first data write 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds f->metadata = fn; 385ced22070SDavid Woodhouse mutex_unlock(&f->sem); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds jffs2_complete_reservation(c); 388aa98d7cfSKaiGai Kohei 389aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 390aa98d7cfSKaiGai Kohei if (ret) { 391aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 392aa98d7cfSKaiGai Kohei return ret; 393aa98d7cfSKaiGai Kohei } 394cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 395aa98d7cfSKaiGai Kohei if (ret) { 396aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 397aa98d7cfSKaiGai Kohei return ret; 398aa98d7cfSKaiGai Kohei } 399aa98d7cfSKaiGai Kohei 4009fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 401e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 4021da177e4SLinus Torvalds if (ret) { 4031da177e4SLinus Torvalds /* Eep. */ 4041da177e4SLinus Torvalds jffs2_clear_inode(inode); 4051da177e4SLinus Torvalds return ret; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 4091da177e4SLinus Torvalds if (!rd) { 4101da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 4111da177e4SLinus Torvalds jffs2_complete_reservation(c); 4121da177e4SLinus Torvalds jffs2_clear_inode(inode); 4131da177e4SLinus Torvalds return -ENOMEM; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 417ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 4201da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 4211da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 4221da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 4251da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 4261da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 4271da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 4281da177e4SLinus Torvalds rd->nsize = namelen; 4291da177e4SLinus Torvalds rd->type = DT_LNK; 4301da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 4311da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 4321da177e4SLinus Torvalds 4339fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds if (IS_ERR(fd)) { 4361da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 4371da177e4SLinus Torvalds as if it were the final unlink() */ 4381da177e4SLinus Torvalds jffs2_complete_reservation(c); 4391da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 440ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 4411da177e4SLinus Torvalds jffs2_clear_inode(inode); 4421da177e4SLinus Torvalds return PTR_ERR(fd); 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 4501da177e4SLinus Torvalds one if necessary. */ 4511da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 4521da177e4SLinus Torvalds 453ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 4541da177e4SLinus Torvalds jffs2_complete_reservation(c); 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds d_instantiate(dentry, inode); 4571da177e4SLinus Torvalds return 0; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode) 4621da177e4SLinus Torvalds { 4631da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 4641da177e4SLinus Torvalds struct jffs2_sb_info *c; 4651da177e4SLinus Torvalds struct inode *inode; 4661da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 4671da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 4681da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 4691da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 4701da177e4SLinus Torvalds int namelen; 4719fe4854cSDavid Woodhouse uint32_t alloclen; 4721da177e4SLinus Torvalds int ret; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds mode |= S_IFDIR; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 4771da177e4SLinus Torvalds if (!ri) 4781da177e4SLinus Torvalds return -ENOMEM; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 4831da177e4SLinus Torvalds * Just the node will do for now, though 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds namelen = dentry->d_name.len; 4869fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, 487e631ddbaSFerenc Havasi JFFS2_SUMMARY_INODE_SIZE); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds if (ret) { 4901da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 4911da177e4SLinus Torvalds return ret; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, mode, ri); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds if (IS_ERR(inode)) { 4971da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 4981da177e4SLinus Torvalds jffs2_complete_reservation(c); 4991da177e4SLinus Torvalds return PTR_ERR(inode); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds inode->i_op = &jffs2_dir_inode_operations; 5031da177e4SLinus Torvalds inode->i_fop = &jffs2_dir_operations; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 5061da177e4SLinus Torvalds 507*27c72b04SDavid Woodhouse /* Directories get nlink 2 at start */ 508*27c72b04SDavid Woodhouse inode->i_nlink = 2; 509*27c72b04SDavid Woodhouse /* but ic->pino_nlink is the parent ino# */ 510*27c72b04SDavid Woodhouse f->inocache->pino_nlink = dir_i->i_ino; 511*27c72b04SDavid Woodhouse 5121da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(0); 5131da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 5141da177e4SLinus Torvalds 5159fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds if (IS_ERR(fn)) { 5201da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 521ced22070SDavid Woodhouse mutex_unlock(&f->sem); 5221da177e4SLinus Torvalds jffs2_complete_reservation(c); 5231da177e4SLinus Torvalds jffs2_clear_inode(inode); 5241da177e4SLinus Torvalds return PTR_ERR(fn); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 5271da177e4SLinus Torvalds obsoleted by the first data write 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds f->metadata = fn; 530ced22070SDavid Woodhouse mutex_unlock(&f->sem); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds jffs2_complete_reservation(c); 533aa98d7cfSKaiGai Kohei 534aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 535aa98d7cfSKaiGai Kohei if (ret) { 536aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 537aa98d7cfSKaiGai Kohei return ret; 538aa98d7cfSKaiGai Kohei } 539cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 540aa98d7cfSKaiGai Kohei if (ret) { 541aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 542aa98d7cfSKaiGai Kohei return ret; 543aa98d7cfSKaiGai Kohei } 544aa98d7cfSKaiGai Kohei 5459fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 546e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 5471da177e4SLinus Torvalds if (ret) { 5481da177e4SLinus Torvalds /* Eep. */ 5491da177e4SLinus Torvalds jffs2_clear_inode(inode); 5501da177e4SLinus Torvalds return ret; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 5541da177e4SLinus Torvalds if (!rd) { 5551da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 5561da177e4SLinus Torvalds jffs2_complete_reservation(c); 5571da177e4SLinus Torvalds jffs2_clear_inode(inode); 5581da177e4SLinus Torvalds return -ENOMEM; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 562ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 5651da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 5661da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 5671da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 5701da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 5711da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 5721da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 5731da177e4SLinus Torvalds rd->nsize = namelen; 5741da177e4SLinus Torvalds rd->type = DT_DIR; 5751da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 5761da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 5771da177e4SLinus Torvalds 5789fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds if (IS_ERR(fd)) { 5811da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 5821da177e4SLinus Torvalds as if it were the final unlink() */ 5831da177e4SLinus Torvalds jffs2_complete_reservation(c); 5841da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 585ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 5861da177e4SLinus Torvalds jffs2_clear_inode(inode); 5871da177e4SLinus Torvalds return PTR_ERR(fd); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 591d8c76e6fSDave Hansen inc_nlink(dir_i); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 5961da177e4SLinus Torvalds one if necessary. */ 5971da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 5981da177e4SLinus Torvalds 599ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 6001da177e4SLinus Torvalds jffs2_complete_reservation(c); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds d_instantiate(dentry, inode); 6031da177e4SLinus Torvalds return 0; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry) 6071da177e4SLinus Torvalds { 608*27c72b04SDavid Woodhouse struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb); 609*27c72b04SDavid Woodhouse struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 6101da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode); 6111da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 6121da177e4SLinus Torvalds int ret; 613*27c72b04SDavid Woodhouse uint32_t now = get_seconds(); 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds for (fd = f->dents ; fd; fd = fd->next) { 6161da177e4SLinus Torvalds if (fd->ino) 6171da177e4SLinus Torvalds return -ENOTEMPTY; 6181da177e4SLinus Torvalds } 619*27c72b04SDavid Woodhouse 620*27c72b04SDavid Woodhouse ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, 621*27c72b04SDavid Woodhouse dentry->d_name.len, f, now); 622*27c72b04SDavid Woodhouse if (!ret) { 623*27c72b04SDavid Woodhouse dir_i->i_mtime = dir_i->i_ctime = ITIME(now); 624*27c72b04SDavid Woodhouse clear_nlink(dentry->d_inode); 6259a53c3a7SDave Hansen drop_nlink(dir_i); 626*27c72b04SDavid Woodhouse } 6271da177e4SLinus Torvalds return ret; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 630265489f0SDavid Woodhouse static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 6331da177e4SLinus Torvalds struct jffs2_sb_info *c; 6341da177e4SLinus Torvalds struct inode *inode; 6351da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 6361da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 6371da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 6381da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 6391da177e4SLinus Torvalds int namelen; 640aef9ab47SDavid Woodhouse union jffs2_device_node dev; 6411da177e4SLinus Torvalds int devlen = 0; 6429fe4854cSDavid Woodhouse uint32_t alloclen; 6431da177e4SLinus Torvalds int ret; 6441da177e4SLinus Torvalds 645aef9ab47SDavid Woodhouse if (!new_valid_dev(rdev)) 6461da177e4SLinus Torvalds return -EINVAL; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 6491da177e4SLinus Torvalds if (!ri) 6501da177e4SLinus Torvalds return -ENOMEM; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 6531da177e4SLinus Torvalds 654aef9ab47SDavid Woodhouse if (S_ISBLK(mode) || S_ISCHR(mode)) 655aef9ab47SDavid Woodhouse devlen = jffs2_encode_dev(&dev, rdev); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 6581da177e4SLinus Torvalds * Just the node will do for now, though 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds namelen = dentry->d_name.len; 6619fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen, 662e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds if (ret) { 6651da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6661da177e4SLinus Torvalds return ret; 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 669cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, mode, ri); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds if (IS_ERR(inode)) { 6721da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6731da177e4SLinus Torvalds jffs2_complete_reservation(c); 6741da177e4SLinus Torvalds return PTR_ERR(inode); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds inode->i_op = &jffs2_file_inode_operations; 6771da177e4SLinus Torvalds init_special_inode(inode, inode->i_mode, rdev); 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds ri->dsize = ri->csize = cpu_to_je32(devlen); 6821da177e4SLinus Torvalds ri->totlen = cpu_to_je32(sizeof(*ri) + devlen); 6831da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds ri->compr = JFFS2_COMPR_NONE; 6861da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen)); 6871da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 6881da177e4SLinus Torvalds 6899fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL); 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (IS_ERR(fn)) { 6941da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 695ced22070SDavid Woodhouse mutex_unlock(&f->sem); 6961da177e4SLinus Torvalds jffs2_complete_reservation(c); 6971da177e4SLinus Torvalds jffs2_clear_inode(inode); 6981da177e4SLinus Torvalds return PTR_ERR(fn); 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 7011da177e4SLinus Torvalds obsoleted by the first data write 7021da177e4SLinus Torvalds */ 7031da177e4SLinus Torvalds f->metadata = fn; 704ced22070SDavid Woodhouse mutex_unlock(&f->sem); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds jffs2_complete_reservation(c); 707aa98d7cfSKaiGai Kohei 708aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 709aa98d7cfSKaiGai Kohei if (ret) { 710aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 711aa98d7cfSKaiGai Kohei return ret; 712aa98d7cfSKaiGai Kohei } 713cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 714aa98d7cfSKaiGai Kohei if (ret) { 715aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 716aa98d7cfSKaiGai Kohei return ret; 717aa98d7cfSKaiGai Kohei } 718aa98d7cfSKaiGai Kohei 7199fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 720e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 7211da177e4SLinus Torvalds if (ret) { 7221da177e4SLinus Torvalds /* Eep. */ 7231da177e4SLinus Torvalds jffs2_clear_inode(inode); 7241da177e4SLinus Torvalds return ret; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 7281da177e4SLinus Torvalds if (!rd) { 7291da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 7301da177e4SLinus Torvalds jffs2_complete_reservation(c); 7311da177e4SLinus Torvalds jffs2_clear_inode(inode); 7321da177e4SLinus Torvalds return -ENOMEM; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 736ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 7391da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 7401da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 7411da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 7441da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 7451da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 7461da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 7471da177e4SLinus Torvalds rd->nsize = namelen; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* XXX: This is ugly. */ 7501da177e4SLinus Torvalds rd->type = (mode & S_IFMT) >> 12; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 7531da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 7541da177e4SLinus Torvalds 7559fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (IS_ERR(fd)) { 7581da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 7591da177e4SLinus Torvalds as if it were the final unlink() */ 7601da177e4SLinus Torvalds jffs2_complete_reservation(c); 7611da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 762ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 7631da177e4SLinus Torvalds jffs2_clear_inode(inode); 7641da177e4SLinus Torvalds return PTR_ERR(fd); 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 7721da177e4SLinus Torvalds one if necessary. */ 7731da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 7741da177e4SLinus Torvalds 775ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 7761da177e4SLinus Torvalds jffs2_complete_reservation(c); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds d_instantiate(dentry, inode); 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds return 0; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry, 7841da177e4SLinus Torvalds struct inode *new_dir_i, struct dentry *new_dentry) 7851da177e4SLinus Torvalds { 7861da177e4SLinus Torvalds int ret; 7871da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb); 7881da177e4SLinus Torvalds struct jffs2_inode_info *victim_f = NULL; 7891da177e4SLinus Torvalds uint8_t type; 7903a69e0cdSArtem B. Bityutskiy uint32_t now; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* The VFS will check for us and prevent trying to rename a 7931da177e4SLinus Torvalds * file over a directory and vice versa, but if it's a directory, 7941da177e4SLinus Torvalds * the VFS can't check whether the victim is empty. The filesystem 7951da177e4SLinus Torvalds * needs to do that for itself. 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds if (new_dentry->d_inode) { 7981da177e4SLinus Torvalds victim_f = JFFS2_INODE_INFO(new_dentry->d_inode); 7991da177e4SLinus Torvalds if (S_ISDIR(new_dentry->d_inode->i_mode)) { 8001da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 8011da177e4SLinus Torvalds 802ced22070SDavid Woodhouse mutex_lock(&victim_f->sem); 8031da177e4SLinus Torvalds for (fd = victim_f->dents; fd; fd = fd->next) { 8041da177e4SLinus Torvalds if (fd->ino) { 805ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 8061da177e4SLinus Torvalds return -ENOTEMPTY; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds } 809ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* XXX: We probably ought to alloc enough space for 8141da177e4SLinus Torvalds both nodes at the same time. Writing the new link, 8151da177e4SLinus Torvalds then getting -ENOSPC, is quite bad :) 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* Make a hard link */ 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* XXX: This is ugly */ 8211da177e4SLinus Torvalds type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; 8221da177e4SLinus Torvalds if (!type) type = DT_REG; 8231da177e4SLinus Torvalds 8243a69e0cdSArtem B. Bityutskiy now = get_seconds(); 8251da177e4SLinus Torvalds ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), 8261da177e4SLinus Torvalds old_dentry->d_inode->i_ino, type, 8273a69e0cdSArtem B. Bityutskiy new_dentry->d_name.name, new_dentry->d_name.len, now); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds if (ret) 8301da177e4SLinus Torvalds return ret; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds if (victim_f) { 8331da177e4SLinus Torvalds /* There was a victim. Kill it off nicely */ 8349a53c3a7SDave Hansen drop_nlink(new_dentry->d_inode); 8351da177e4SLinus Torvalds /* Don't oops if the victim was a dirent pointing to an 8361da177e4SLinus Torvalds inode which didn't exist. */ 8371da177e4SLinus Torvalds if (victim_f->inocache) { 838ced22070SDavid Woodhouse mutex_lock(&victim_f->sem); 839*27c72b04SDavid Woodhouse if (S_ISDIR(new_dentry->d_inode->i_mode)) 840*27c72b04SDavid Woodhouse victim_f->inocache->pino_nlink = 0; 841*27c72b04SDavid Woodhouse else 842*27c72b04SDavid Woodhouse victim_f->inocache->pino_nlink--; 843ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* If it was a directory we moved, and there was no victim, 8481da177e4SLinus Torvalds increase i_nlink on its new parent */ 8491da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f) 850d8c76e6fSDave Hansen inc_nlink(new_dir_i); 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* Unlink the original */ 8531da177e4SLinus Torvalds ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), 8543a69e0cdSArtem B. Bityutskiy old_dentry->d_name.name, old_dentry->d_name.len, NULL, now); 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds /* We don't touch inode->i_nlink */ 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (ret) { 8591da177e4SLinus Torvalds /* Oh shit. We really ought to make a single node which can do both atomically */ 8601da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); 861ced22070SDavid Woodhouse mutex_lock(&f->sem); 862d8c76e6fSDave Hansen inc_nlink(old_dentry->d_inode); 863*27c72b04SDavid Woodhouse if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode)) 864*27c72b04SDavid Woodhouse f->inocache->pino_nlink++; 865ced22070SDavid Woodhouse mutex_unlock(&f->sem); 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret); 8681da177e4SLinus Torvalds /* Might as well let the VFS know */ 8691da177e4SLinus Torvalds d_instantiate(new_dentry, old_dentry->d_inode); 8701da177e4SLinus Torvalds atomic_inc(&old_dentry->d_inode->i_count); 8713a69e0cdSArtem B. Bityutskiy new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now); 8721da177e4SLinus Torvalds return ret; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode)) 8769a53c3a7SDave Hansen drop_nlink(old_dir_i); 8771da177e4SLinus Torvalds 8783a69e0cdSArtem B. Bityutskiy new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now); 8793a69e0cdSArtem B. Bityutskiy 8801da177e4SLinus Torvalds return 0; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 883