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 89*ced22070SDavid 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; 102*ced22070SDavid 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; 149*ced22070SDavid 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 } 169*ced22070SDavid 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 2111da177e4SLinus Torvalds ret = jffs2_do_create(c, dir_f, f, ri, 2121da177e4SLinus Torvalds dentry->d_name.name, dentry->d_name.len); 213aa98d7cfSKaiGai Kohei if (ret) 214aa98d7cfSKaiGai Kohei goto fail; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime)); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 2191da177e4SLinus Torvalds d_instantiate(dentry, inode); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n", 2221da177e4SLinus Torvalds inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages)); 2231da177e4SLinus Torvalds return 0; 224aa98d7cfSKaiGai Kohei 225aa98d7cfSKaiGai Kohei fail: 226aa98d7cfSKaiGai Kohei make_bad_inode(inode); 227aa98d7cfSKaiGai Kohei iput(inode); 228aa98d7cfSKaiGai Kohei jffs2_free_raw_inode(ri); 229aa98d7cfSKaiGai Kohei return ret; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /***********************************************************************/ 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb); 2381da177e4SLinus Torvalds struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 2391da177e4SLinus Torvalds struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode); 2401da177e4SLinus Torvalds int ret; 2413a69e0cdSArtem B. Bityutskiy uint32_t now = get_seconds(); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, 2443a69e0cdSArtem B. Bityutskiy dentry->d_name.len, dead_f, now); 2451da177e4SLinus Torvalds if (dead_f->inocache) 2461da177e4SLinus Torvalds dentry->d_inode->i_nlink = dead_f->inocache->nlink; 2473a69e0cdSArtem B. Bityutskiy if (!ret) 2483a69e0cdSArtem B. Bityutskiy dir_i->i_mtime = dir_i->i_ctime = ITIME(now); 2491da177e4SLinus Torvalds return ret; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds /***********************************************************************/ 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb); 2571da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); 2581da177e4SLinus Torvalds struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); 2591da177e4SLinus Torvalds int ret; 2601da177e4SLinus Torvalds uint8_t type; 2613a69e0cdSArtem B. Bityutskiy uint32_t now; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* Don't let people make hard links to bad inodes. */ 2641da177e4SLinus Torvalds if (!f->inocache) 2651da177e4SLinus Torvalds return -EIO; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode)) 2681da177e4SLinus Torvalds return -EPERM; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* XXX: This is ugly */ 2711da177e4SLinus Torvalds type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; 2721da177e4SLinus Torvalds if (!type) type = DT_REG; 2731da177e4SLinus Torvalds 2743a69e0cdSArtem B. Bityutskiy now = get_seconds(); 2753a69e0cdSArtem B. Bityutskiy ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds if (!ret) { 278*ced22070SDavid Woodhouse mutex_lock(&f->sem); 2791da177e4SLinus Torvalds old_dentry->d_inode->i_nlink = ++f->inocache->nlink; 280*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 2811da177e4SLinus Torvalds d_instantiate(dentry, old_dentry->d_inode); 2823a69e0cdSArtem B. Bityutskiy dir_i->i_mtime = dir_i->i_ctime = ITIME(now); 2831da177e4SLinus Torvalds atomic_inc(&old_dentry->d_inode->i_count); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds return ret; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /***********************************************************************/ 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 2931da177e4SLinus Torvalds struct jffs2_sb_info *c; 2941da177e4SLinus Torvalds struct inode *inode; 2951da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 2961da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 2971da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 2981da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 2991da177e4SLinus Torvalds int namelen; 3009fe4854cSDavid Woodhouse uint32_t alloclen; 30132f1a95dSArtem B. Bityuckiy int ret, targetlen = strlen(target); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* FIXME: If you care. We'd need to use frags for the target 3041da177e4SLinus Torvalds if it grows much more than this */ 30532f1a95dSArtem B. Bityuckiy if (targetlen > 254) 3061da177e4SLinus Torvalds return -EINVAL; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds if (!ri) 3111da177e4SLinus Torvalds return -ENOMEM; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 3161da177e4SLinus Torvalds * Just the node will do for now, though 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds namelen = dentry->d_name.len; 3199fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen, 320e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds if (ret) { 3231da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3241da177e4SLinus Torvalds return ret; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 327cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri); 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (IS_ERR(inode)) { 3301da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3311da177e4SLinus Torvalds jffs2_complete_reservation(c); 3321da177e4SLinus Torvalds return PTR_ERR(inode); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds inode->i_op = &jffs2_symlink_inode_operations; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 3381da177e4SLinus Torvalds 33932f1a95dSArtem B. Bityuckiy inode->i_size = targetlen; 3401da177e4SLinus Torvalds ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size); 3411da177e4SLinus Torvalds ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size); 3421da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds ri->compr = JFFS2_COMPR_NONE; 34532f1a95dSArtem B. Bityuckiy ri->data_crc = cpu_to_je32(crc32(0, target, targetlen)); 3461da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 3471da177e4SLinus Torvalds 3489fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds if (IS_ERR(fn)) { 3531da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 354*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 3551da177e4SLinus Torvalds jffs2_complete_reservation(c); 3561da177e4SLinus Torvalds jffs2_clear_inode(inode); 3571da177e4SLinus Torvalds return PTR_ERR(fn); 3581da177e4SLinus Torvalds } 35932f1a95dSArtem B. Bityuckiy 3602b79adccSArtem B. Bityutskiy /* We use f->target field to store the target path. */ 3612b79adccSArtem B. Bityutskiy f->target = kmalloc(targetlen + 1, GFP_KERNEL); 3622b79adccSArtem B. Bityutskiy if (!f->target) { 36332f1a95dSArtem B. Bityuckiy printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1); 364*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 36532f1a95dSArtem B. Bityuckiy jffs2_complete_reservation(c); 36632f1a95dSArtem B. Bityuckiy jffs2_clear_inode(inode); 36732f1a95dSArtem B. Bityuckiy return -ENOMEM; 36832f1a95dSArtem B. Bityuckiy } 36932f1a95dSArtem B. Bityuckiy 3702b79adccSArtem B. Bityutskiy memcpy(f->target, target, targetlen + 1); 3712b79adccSArtem B. Bityutskiy D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target)); 37232f1a95dSArtem B. Bityuckiy 3731da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 3741da177e4SLinus Torvalds obsoleted by the first data write 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds f->metadata = fn; 377*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds jffs2_complete_reservation(c); 380aa98d7cfSKaiGai Kohei 381aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 382aa98d7cfSKaiGai Kohei if (ret) { 383aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 384aa98d7cfSKaiGai Kohei return ret; 385aa98d7cfSKaiGai Kohei } 386cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 387aa98d7cfSKaiGai Kohei if (ret) { 388aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 389aa98d7cfSKaiGai Kohei return ret; 390aa98d7cfSKaiGai Kohei } 391aa98d7cfSKaiGai Kohei 3929fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 393e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 3941da177e4SLinus Torvalds if (ret) { 3951da177e4SLinus Torvalds /* Eep. */ 3961da177e4SLinus Torvalds jffs2_clear_inode(inode); 3971da177e4SLinus Torvalds return ret; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 4011da177e4SLinus Torvalds if (!rd) { 4021da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 4031da177e4SLinus Torvalds jffs2_complete_reservation(c); 4041da177e4SLinus Torvalds jffs2_clear_inode(inode); 4051da177e4SLinus Torvalds return -ENOMEM; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 409*ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 4121da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 4131da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 4141da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 4171da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 4181da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 4191da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 4201da177e4SLinus Torvalds rd->nsize = namelen; 4211da177e4SLinus Torvalds rd->type = DT_LNK; 4221da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 4231da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 4241da177e4SLinus Torvalds 4259fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds if (IS_ERR(fd)) { 4281da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 4291da177e4SLinus Torvalds as if it were the final unlink() */ 4301da177e4SLinus Torvalds jffs2_complete_reservation(c); 4311da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 432*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 4331da177e4SLinus Torvalds jffs2_clear_inode(inode); 4341da177e4SLinus Torvalds return PTR_ERR(fd); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 4421da177e4SLinus Torvalds one if necessary. */ 4431da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 4441da177e4SLinus Torvalds 445*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 4461da177e4SLinus Torvalds jffs2_complete_reservation(c); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds d_instantiate(dentry, inode); 4491da177e4SLinus Torvalds return 0; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 4561da177e4SLinus Torvalds struct jffs2_sb_info *c; 4571da177e4SLinus Torvalds struct inode *inode; 4581da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 4591da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 4601da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 4611da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 4621da177e4SLinus Torvalds int namelen; 4639fe4854cSDavid Woodhouse uint32_t alloclen; 4641da177e4SLinus Torvalds int ret; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds mode |= S_IFDIR; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 4691da177e4SLinus Torvalds if (!ri) 4701da177e4SLinus Torvalds return -ENOMEM; 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 4751da177e4SLinus Torvalds * Just the node will do for now, though 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds namelen = dentry->d_name.len; 4789fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, 479e631ddbaSFerenc Havasi JFFS2_SUMMARY_INODE_SIZE); 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds if (ret) { 4821da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 4831da177e4SLinus Torvalds return ret; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 486cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, mode, ri); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (IS_ERR(inode)) { 4891da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 4901da177e4SLinus Torvalds jffs2_complete_reservation(c); 4911da177e4SLinus Torvalds return PTR_ERR(inode); 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds inode->i_op = &jffs2_dir_inode_operations; 4951da177e4SLinus Torvalds inode->i_fop = &jffs2_dir_operations; 4961da177e4SLinus Torvalds /* Directories get nlink 2 at start */ 4971da177e4SLinus Torvalds inode->i_nlink = 2; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(0); 5021da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 5031da177e4SLinus Torvalds 5049fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL); 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (IS_ERR(fn)) { 5091da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 510*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 5111da177e4SLinus Torvalds jffs2_complete_reservation(c); 5121da177e4SLinus Torvalds jffs2_clear_inode(inode); 5131da177e4SLinus Torvalds return PTR_ERR(fn); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 5161da177e4SLinus Torvalds obsoleted by the first data write 5171da177e4SLinus Torvalds */ 5181da177e4SLinus Torvalds f->metadata = fn; 519*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds jffs2_complete_reservation(c); 522aa98d7cfSKaiGai Kohei 523aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 524aa98d7cfSKaiGai Kohei if (ret) { 525aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 526aa98d7cfSKaiGai Kohei return ret; 527aa98d7cfSKaiGai Kohei } 528cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 529aa98d7cfSKaiGai Kohei if (ret) { 530aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 531aa98d7cfSKaiGai Kohei return ret; 532aa98d7cfSKaiGai Kohei } 533aa98d7cfSKaiGai Kohei 5349fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 535e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 5361da177e4SLinus Torvalds if (ret) { 5371da177e4SLinus Torvalds /* Eep. */ 5381da177e4SLinus Torvalds jffs2_clear_inode(inode); 5391da177e4SLinus Torvalds return ret; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 5431da177e4SLinus Torvalds if (!rd) { 5441da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 5451da177e4SLinus Torvalds jffs2_complete_reservation(c); 5461da177e4SLinus Torvalds jffs2_clear_inode(inode); 5471da177e4SLinus Torvalds return -ENOMEM; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 551*ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 5541da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 5551da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 5561da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 5591da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 5601da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 5611da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 5621da177e4SLinus Torvalds rd->nsize = namelen; 5631da177e4SLinus Torvalds rd->type = DT_DIR; 5641da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 5651da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 5661da177e4SLinus Torvalds 5679fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (IS_ERR(fd)) { 5701da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 5711da177e4SLinus Torvalds as if it were the final unlink() */ 5721da177e4SLinus Torvalds jffs2_complete_reservation(c); 5731da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 574*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 5751da177e4SLinus Torvalds jffs2_clear_inode(inode); 5761da177e4SLinus Torvalds return PTR_ERR(fd); 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 580d8c76e6fSDave Hansen inc_nlink(dir_i); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 5851da177e4SLinus Torvalds one if necessary. */ 5861da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 5871da177e4SLinus Torvalds 588*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 5891da177e4SLinus Torvalds jffs2_complete_reservation(c); 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds d_instantiate(dentry, inode); 5921da177e4SLinus Torvalds return 0; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry) 5961da177e4SLinus Torvalds { 5971da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode); 5981da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 5991da177e4SLinus Torvalds int ret; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds for (fd = f->dents ; fd; fd = fd->next) { 6021da177e4SLinus Torvalds if (fd->ino) 6031da177e4SLinus Torvalds return -ENOTEMPTY; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds ret = jffs2_unlink(dir_i, dentry); 6061da177e4SLinus Torvalds if (!ret) 6079a53c3a7SDave Hansen drop_nlink(dir_i); 6081da177e4SLinus Torvalds return ret; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 611265489f0SDavid Woodhouse static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds struct jffs2_inode_info *f, *dir_f; 6141da177e4SLinus Torvalds struct jffs2_sb_info *c; 6151da177e4SLinus Torvalds struct inode *inode; 6161da177e4SLinus Torvalds struct jffs2_raw_inode *ri; 6171da177e4SLinus Torvalds struct jffs2_raw_dirent *rd; 6181da177e4SLinus Torvalds struct jffs2_full_dnode *fn; 6191da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 6201da177e4SLinus Torvalds int namelen; 621aef9ab47SDavid Woodhouse union jffs2_device_node dev; 6221da177e4SLinus Torvalds int devlen = 0; 6239fe4854cSDavid Woodhouse uint32_t alloclen; 6241da177e4SLinus Torvalds int ret; 6251da177e4SLinus Torvalds 626aef9ab47SDavid Woodhouse if (!new_valid_dev(rdev)) 6271da177e4SLinus Torvalds return -EINVAL; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds ri = jffs2_alloc_raw_inode(); 6301da177e4SLinus Torvalds if (!ri) 6311da177e4SLinus Torvalds return -ENOMEM; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds c = JFFS2_SB_INFO(dir_i->i_sb); 6341da177e4SLinus Torvalds 635aef9ab47SDavid Woodhouse if (S_ISBLK(mode) || S_ISCHR(mode)) 636aef9ab47SDavid Woodhouse devlen = jffs2_encode_dev(&dev, rdev); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* Try to reserve enough space for both node and dirent. 6391da177e4SLinus Torvalds * Just the node will do for now, though 6401da177e4SLinus Torvalds */ 6411da177e4SLinus Torvalds namelen = dentry->d_name.len; 6429fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen, 643e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds if (ret) { 6461da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6471da177e4SLinus Torvalds return ret; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650cfc8dc6fSKaiGai Kohei inode = jffs2_new_inode(dir_i, mode, ri); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds if (IS_ERR(inode)) { 6531da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6541da177e4SLinus Torvalds jffs2_complete_reservation(c); 6551da177e4SLinus Torvalds return PTR_ERR(inode); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds inode->i_op = &jffs2_file_inode_operations; 6581da177e4SLinus Torvalds init_special_inode(inode, inode->i_mode, rdev); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds f = JFFS2_INODE_INFO(inode); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds ri->dsize = ri->csize = cpu_to_je32(devlen); 6631da177e4SLinus Torvalds ri->totlen = cpu_to_je32(sizeof(*ri) + devlen); 6641da177e4SLinus Torvalds ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds ri->compr = JFFS2_COMPR_NONE; 6671da177e4SLinus Torvalds ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen)); 6681da177e4SLinus Torvalds ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 6691da177e4SLinus Torvalds 6709fe4854cSDavid Woodhouse fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds jffs2_free_raw_inode(ri); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds if (IS_ERR(fn)) { 6751da177e4SLinus Torvalds /* Eeek. Wave bye bye */ 676*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 6771da177e4SLinus Torvalds jffs2_complete_reservation(c); 6781da177e4SLinus Torvalds jffs2_clear_inode(inode); 6791da177e4SLinus Torvalds return PTR_ERR(fn); 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds /* No data here. Only a metadata node, which will be 6821da177e4SLinus Torvalds obsoleted by the first data write 6831da177e4SLinus Torvalds */ 6841da177e4SLinus Torvalds f->metadata = fn; 685*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds jffs2_complete_reservation(c); 688aa98d7cfSKaiGai Kohei 689aa98d7cfSKaiGai Kohei ret = jffs2_init_security(inode, dir_i); 690aa98d7cfSKaiGai Kohei if (ret) { 691aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 692aa98d7cfSKaiGai Kohei return ret; 693aa98d7cfSKaiGai Kohei } 694cfc8dc6fSKaiGai Kohei ret = jffs2_init_acl_post(inode); 695aa98d7cfSKaiGai Kohei if (ret) { 696aa98d7cfSKaiGai Kohei jffs2_clear_inode(inode); 697aa98d7cfSKaiGai Kohei return ret; 698aa98d7cfSKaiGai Kohei } 699aa98d7cfSKaiGai Kohei 7009fe4854cSDavid Woodhouse ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 701e631ddbaSFerenc Havasi ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 7021da177e4SLinus Torvalds if (ret) { 7031da177e4SLinus Torvalds /* Eep. */ 7041da177e4SLinus Torvalds jffs2_clear_inode(inode); 7051da177e4SLinus Torvalds return ret; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds rd = jffs2_alloc_raw_dirent(); 7091da177e4SLinus Torvalds if (!rd) { 7101da177e4SLinus Torvalds /* Argh. Now we treat it like a normal delete */ 7111da177e4SLinus Torvalds jffs2_complete_reservation(c); 7121da177e4SLinus Torvalds jffs2_clear_inode(inode); 7131da177e4SLinus Torvalds return -ENOMEM; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds dir_f = JFFS2_INODE_INFO(dir_i); 717*ced22070SDavid Woodhouse mutex_lock(&dir_f->sem); 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 7201da177e4SLinus Torvalds rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 7211da177e4SLinus Torvalds rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 7221da177e4SLinus Torvalds rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds rd->pino = cpu_to_je32(dir_i->i_ino); 7251da177e4SLinus Torvalds rd->version = cpu_to_je32(++dir_f->highest_version); 7261da177e4SLinus Torvalds rd->ino = cpu_to_je32(inode->i_ino); 7271da177e4SLinus Torvalds rd->mctime = cpu_to_je32(get_seconds()); 7281da177e4SLinus Torvalds rd->nsize = namelen; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* XXX: This is ugly. */ 7311da177e4SLinus Torvalds rd->type = (mode & S_IFMT) >> 12; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 7341da177e4SLinus Torvalds rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 7351da177e4SLinus Torvalds 7369fe4854cSDavid Woodhouse fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds if (IS_ERR(fd)) { 7391da177e4SLinus Torvalds /* dirent failed to write. Delete the inode normally 7401da177e4SLinus Torvalds as if it were the final unlink() */ 7411da177e4SLinus Torvalds jffs2_complete_reservation(c); 7421da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 743*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 7441da177e4SLinus Torvalds jffs2_clear_inode(inode); 7451da177e4SLinus Torvalds return PTR_ERR(fd); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds jffs2_free_raw_dirent(rd); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* Link the fd into the inode's list, obsoleting an old 7531da177e4SLinus Torvalds one if necessary. */ 7541da177e4SLinus Torvalds jffs2_add_fd_to_list(c, fd, &dir_f->dents); 7551da177e4SLinus Torvalds 756*ced22070SDavid Woodhouse mutex_unlock(&dir_f->sem); 7571da177e4SLinus Torvalds jffs2_complete_reservation(c); 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds d_instantiate(dentry, inode); 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds return 0; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry, 7651da177e4SLinus Torvalds struct inode *new_dir_i, struct dentry *new_dentry) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds int ret; 7681da177e4SLinus Torvalds struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb); 7691da177e4SLinus Torvalds struct jffs2_inode_info *victim_f = NULL; 7701da177e4SLinus Torvalds uint8_t type; 7713a69e0cdSArtem B. Bityutskiy uint32_t now; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* The VFS will check for us and prevent trying to rename a 7741da177e4SLinus Torvalds * file over a directory and vice versa, but if it's a directory, 7751da177e4SLinus Torvalds * the VFS can't check whether the victim is empty. The filesystem 7761da177e4SLinus Torvalds * needs to do that for itself. 7771da177e4SLinus Torvalds */ 7781da177e4SLinus Torvalds if (new_dentry->d_inode) { 7791da177e4SLinus Torvalds victim_f = JFFS2_INODE_INFO(new_dentry->d_inode); 7801da177e4SLinus Torvalds if (S_ISDIR(new_dentry->d_inode->i_mode)) { 7811da177e4SLinus Torvalds struct jffs2_full_dirent *fd; 7821da177e4SLinus Torvalds 783*ced22070SDavid Woodhouse mutex_lock(&victim_f->sem); 7841da177e4SLinus Torvalds for (fd = victim_f->dents; fd; fd = fd->next) { 7851da177e4SLinus Torvalds if (fd->ino) { 786*ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 7871da177e4SLinus Torvalds return -ENOTEMPTY; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds } 790*ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* XXX: We probably ought to alloc enough space for 7951da177e4SLinus Torvalds both nodes at the same time. Writing the new link, 7961da177e4SLinus Torvalds then getting -ENOSPC, is quite bad :) 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds /* Make a hard link */ 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* XXX: This is ugly */ 8021da177e4SLinus Torvalds type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; 8031da177e4SLinus Torvalds if (!type) type = DT_REG; 8041da177e4SLinus Torvalds 8053a69e0cdSArtem B. Bityutskiy now = get_seconds(); 8061da177e4SLinus Torvalds ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), 8071da177e4SLinus Torvalds old_dentry->d_inode->i_ino, type, 8083a69e0cdSArtem B. Bityutskiy new_dentry->d_name.name, new_dentry->d_name.len, now); 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds if (ret) 8111da177e4SLinus Torvalds return ret; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds if (victim_f) { 8141da177e4SLinus Torvalds /* There was a victim. Kill it off nicely */ 8159a53c3a7SDave Hansen drop_nlink(new_dentry->d_inode); 8161da177e4SLinus Torvalds /* Don't oops if the victim was a dirent pointing to an 8171da177e4SLinus Torvalds inode which didn't exist. */ 8181da177e4SLinus Torvalds if (victim_f->inocache) { 819*ced22070SDavid Woodhouse mutex_lock(&victim_f->sem); 8201da177e4SLinus Torvalds victim_f->inocache->nlink--; 821*ced22070SDavid Woodhouse mutex_unlock(&victim_f->sem); 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* If it was a directory we moved, and there was no victim, 8261da177e4SLinus Torvalds increase i_nlink on its new parent */ 8271da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f) 828d8c76e6fSDave Hansen inc_nlink(new_dir_i); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* Unlink the original */ 8311da177e4SLinus Torvalds ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), 8323a69e0cdSArtem B. Bityutskiy old_dentry->d_name.name, old_dentry->d_name.len, NULL, now); 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* We don't touch inode->i_nlink */ 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (ret) { 8371da177e4SLinus Torvalds /* Oh shit. We really ought to make a single node which can do both atomically */ 8381da177e4SLinus Torvalds struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); 839*ced22070SDavid Woodhouse mutex_lock(&f->sem); 840d8c76e6fSDave Hansen inc_nlink(old_dentry->d_inode); 8411da177e4SLinus Torvalds if (f->inocache) 8421da177e4SLinus Torvalds f->inocache->nlink++; 843*ced22070SDavid Woodhouse mutex_unlock(&f->sem); 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret); 8461da177e4SLinus Torvalds /* Might as well let the VFS know */ 8471da177e4SLinus Torvalds d_instantiate(new_dentry, old_dentry->d_inode); 8481da177e4SLinus Torvalds atomic_inc(&old_dentry->d_inode->i_count); 8493a69e0cdSArtem B. Bityutskiy new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now); 8501da177e4SLinus Torvalds return ret; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds if (S_ISDIR(old_dentry->d_inode->i_mode)) 8549a53c3a7SDave Hansen drop_nlink(old_dir_i); 8551da177e4SLinus Torvalds 8563a69e0cdSArtem B. Bityutskiy new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now); 8573a69e0cdSArtem B. Bityutskiy 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 861