Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Red Hat, Inc. |
| 5 | * |
| 6 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 7 | * |
| 8 | * For licensing information, see the file 'LICENCE' in this directory. |
| 9 | * |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 10 | * $Id: dir.c,v 1.90 2005/11/07 11:14:39 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/crc32.h> |
| 19 | #include <linux/jffs2.h> |
David Woodhouse | cbb9a56 | 2006-05-03 13:07:27 +0100 | [diff] [blame] | 20 | #include "jffs2_fs_i.h" |
| 21 | #include "jffs2_fs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/time.h> |
| 23 | #include "nodelist.h" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static int jffs2_readdir (struct file *, void *, filldir_t); |
| 26 | |
| 27 | static int jffs2_create (struct inode *,struct dentry *,int, |
| 28 | struct nameidata *); |
| 29 | static struct dentry *jffs2_lookup (struct inode *,struct dentry *, |
| 30 | struct nameidata *); |
| 31 | static int jffs2_link (struct dentry *,struct inode *,struct dentry *); |
| 32 | static int jffs2_unlink (struct inode *,struct dentry *); |
| 33 | static int jffs2_symlink (struct inode *,struct dentry *,const char *); |
| 34 | static int jffs2_mkdir (struct inode *,struct dentry *,int); |
| 35 | static int jffs2_rmdir (struct inode *,struct dentry *); |
David Woodhouse | 265489f | 2005-07-06 13:13:13 +0100 | [diff] [blame] | 36 | static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static int jffs2_rename (struct inode *, struct dentry *, |
| 38 | struct inode *, struct dentry *); |
| 39 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 40 | const struct file_operations jffs2_dir_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | .read = generic_read_dir, |
| 43 | .readdir = jffs2_readdir, |
| 44 | .ioctl = jffs2_ioctl, |
| 45 | .fsync = jffs2_fsync |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | struct inode_operations jffs2_dir_inode_operations = |
| 50 | { |
David Woodhouse | 265489f | 2005-07-06 13:13:13 +0100 | [diff] [blame] | 51 | .create = jffs2_create, |
| 52 | .lookup = jffs2_lookup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .link = jffs2_link, |
| 54 | .unlink = jffs2_unlink, |
| 55 | .symlink = jffs2_symlink, |
| 56 | .mkdir = jffs2_mkdir, |
| 57 | .rmdir = jffs2_rmdir, |
| 58 | .mknod = jffs2_mknod, |
| 59 | .rename = jffs2_rename, |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 60 | .permission = jffs2_permission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | .setattr = jffs2_setattr, |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 62 | .setxattr = jffs2_setxattr, |
| 63 | .getxattr = jffs2_getxattr, |
| 64 | .listxattr = jffs2_listxattr, |
| 65 | .removexattr = jffs2_removexattr |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /***********************************************************************/ |
| 69 | |
| 70 | |
| 71 | /* We keep the dirent list sorted in increasing order of name hash, |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 72 | and we use the same hash function as the dentries. Makes this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | nice and simple |
| 74 | */ |
| 75 | static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target, |
| 76 | struct nameidata *nd) |
| 77 | { |
| 78 | struct jffs2_inode_info *dir_f; |
| 79 | struct jffs2_sb_info *c; |
| 80 | struct jffs2_full_dirent *fd = NULL, *fd_list; |
| 81 | uint32_t ino = 0; |
| 82 | struct inode *inode = NULL; |
| 83 | |
| 84 | D1(printk(KERN_DEBUG "jffs2_lookup()\n")); |
| 85 | |
Richard Purdie | 373d5e7 | 2006-04-18 02:05:46 +0100 | [diff] [blame] | 86 | if (target->d_name.len > JFFS2_MAX_NAME_LEN) |
| 87 | return ERR_PTR(-ENAMETOOLONG); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | dir_f = JFFS2_INODE_INFO(dir_i); |
| 90 | c = JFFS2_SB_INFO(dir_i->i_sb); |
| 91 | |
| 92 | down(&dir_f->sem); |
| 93 | |
| 94 | /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */ |
| 95 | for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 96 | if (fd_list->nhash == target->d_name.hash && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | (!fd || fd_list->version > fd->version) && |
| 98 | strlen(fd_list->name) == target->d_name.len && |
| 99 | !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) { |
| 100 | fd = fd_list; |
| 101 | } |
| 102 | } |
| 103 | if (fd) |
| 104 | ino = fd->ino; |
| 105 | up(&dir_f->sem); |
| 106 | if (ino) { |
| 107 | inode = iget(dir_i->i_sb, ino); |
| 108 | if (!inode) { |
| 109 | printk(KERN_WARNING "iget() failed for ino #%u\n", ino); |
| 110 | return (ERR_PTR(-EIO)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | d_add(target, inode); |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | /***********************************************************************/ |
| 120 | |
| 121 | |
| 122 | static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 123 | { |
| 124 | struct jffs2_inode_info *f; |
| 125 | struct jffs2_sb_info *c; |
| 126 | struct inode *inode = filp->f_dentry->d_inode; |
| 127 | struct jffs2_full_dirent *fd; |
| 128 | unsigned long offset, curofs; |
| 129 | |
| 130 | D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino)); |
| 131 | |
| 132 | f = JFFS2_INODE_INFO(inode); |
| 133 | c = JFFS2_SB_INFO(inode->i_sb); |
| 134 | |
| 135 | offset = filp->f_pos; |
| 136 | |
| 137 | if (offset == 0) { |
| 138 | D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino)); |
| 139 | if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) |
| 140 | goto out; |
| 141 | offset++; |
| 142 | } |
| 143 | if (offset == 1) { |
| 144 | unsigned long pino = parent_ino(filp->f_dentry); |
| 145 | D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino)); |
| 146 | if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0) |
| 147 | goto out; |
| 148 | offset++; |
| 149 | } |
| 150 | |
| 151 | curofs=1; |
| 152 | down(&f->sem); |
| 153 | for (fd = f->dents; fd; fd = fd->next) { |
| 154 | |
| 155 | curofs++; |
| 156 | /* First loop: curofs = 2; offset = 2 */ |
| 157 | if (curofs < offset) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 158 | D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | fd->name, fd->ino, fd->type, curofs, offset)); |
| 160 | continue; |
| 161 | } |
| 162 | if (!fd->ino) { |
| 163 | D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name)); |
| 164 | offset++; |
| 165 | continue; |
| 166 | } |
| 167 | D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type)); |
| 168 | if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0) |
| 169 | break; |
| 170 | offset++; |
| 171 | } |
| 172 | up(&f->sem); |
| 173 | out: |
| 174 | filp->f_pos = offset; |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /***********************************************************************/ |
| 179 | |
| 180 | |
| 181 | static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode, |
| 182 | struct nameidata *nd) |
| 183 | { |
| 184 | struct jffs2_raw_inode *ri; |
| 185 | struct jffs2_inode_info *f, *dir_f; |
| 186 | struct jffs2_sb_info *c; |
| 187 | struct inode *inode; |
| 188 | int ret; |
| 189 | |
| 190 | ri = jffs2_alloc_raw_inode(); |
| 191 | if (!ri) |
| 192 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | c = JFFS2_SB_INFO(dir_i->i_sb); |
| 195 | |
| 196 | D1(printk(KERN_DEBUG "jffs2_create()\n")); |
| 197 | |
| 198 | inode = jffs2_new_inode(dir_i, mode, ri); |
| 199 | |
| 200 | if (IS_ERR(inode)) { |
| 201 | D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n")); |
| 202 | jffs2_free_raw_inode(ri); |
| 203 | return PTR_ERR(inode); |
| 204 | } |
| 205 | |
| 206 | inode->i_op = &jffs2_file_inode_operations; |
| 207 | inode->i_fop = &jffs2_file_operations; |
| 208 | inode->i_mapping->a_ops = &jffs2_file_address_operations; |
| 209 | inode->i_mapping->nrpages = 0; |
| 210 | |
| 211 | f = JFFS2_INODE_INFO(inode); |
| 212 | dir_f = JFFS2_INODE_INFO(dir_i); |
| 213 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 214 | ret = jffs2_do_create(c, dir_f, f, ri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | dentry->d_name.name, dentry->d_name.len); |
| 216 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 217 | if (ret) |
| 218 | goto fail; |
| 219 | |
| 220 | ret = jffs2_init_security(inode, dir_i); |
| 221 | if (ret) |
| 222 | goto fail; |
| 223 | ret = jffs2_init_acl(inode, dir_i); |
| 224 | if (ret) |
| 225 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime)); |
| 228 | |
| 229 | jffs2_free_raw_inode(ri); |
| 230 | d_instantiate(dentry, inode); |
| 231 | |
| 232 | D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n", |
| 233 | inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages)); |
| 234 | return 0; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 235 | |
| 236 | fail: |
| 237 | make_bad_inode(inode); |
| 238 | iput(inode); |
| 239 | jffs2_free_raw_inode(ri); |
| 240 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /***********************************************************************/ |
| 244 | |
| 245 | |
| 246 | static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry) |
| 247 | { |
| 248 | struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb); |
| 249 | struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); |
| 250 | struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode); |
| 251 | int ret; |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 252 | uint32_t now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 254 | ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name, |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 255 | dentry->d_name.len, dead_f, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (dead_f->inocache) |
| 257 | dentry->d_inode->i_nlink = dead_f->inocache->nlink; |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 258 | if (!ret) |
| 259 | dir_i->i_mtime = dir_i->i_ctime = ITIME(now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return ret; |
| 261 | } |
| 262 | /***********************************************************************/ |
| 263 | |
| 264 | |
| 265 | static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry) |
| 266 | { |
| 267 | struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb); |
| 268 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); |
| 269 | struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i); |
| 270 | int ret; |
| 271 | uint8_t type; |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 272 | uint32_t now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /* Don't let people make hard links to bad inodes. */ |
| 275 | if (!f->inocache) |
| 276 | return -EIO; |
| 277 | |
| 278 | if (S_ISDIR(old_dentry->d_inode->i_mode)) |
| 279 | return -EPERM; |
| 280 | |
| 281 | /* XXX: This is ugly */ |
| 282 | type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; |
| 283 | if (!type) type = DT_REG; |
| 284 | |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 285 | now = get_seconds(); |
| 286 | ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | if (!ret) { |
| 289 | down(&f->sem); |
| 290 | old_dentry->d_inode->i_nlink = ++f->inocache->nlink; |
| 291 | up(&f->sem); |
| 292 | d_instantiate(dentry, old_dentry->d_inode); |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 293 | dir_i->i_mtime = dir_i->i_ctime = ITIME(now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | atomic_inc(&old_dentry->d_inode->i_count); |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | /***********************************************************************/ |
| 300 | |
| 301 | static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target) |
| 302 | { |
| 303 | struct jffs2_inode_info *f, *dir_f; |
| 304 | struct jffs2_sb_info *c; |
| 305 | struct inode *inode; |
| 306 | struct jffs2_raw_inode *ri; |
| 307 | struct jffs2_raw_dirent *rd; |
| 308 | struct jffs2_full_dnode *fn; |
| 309 | struct jffs2_full_dirent *fd; |
| 310 | int namelen; |
| 311 | uint32_t alloclen, phys_ofs; |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 312 | int ret, targetlen = strlen(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* FIXME: If you care. We'd need to use frags for the target |
| 315 | if it grows much more than this */ |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 316 | if (targetlen > 254) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | |
| 319 | ri = jffs2_alloc_raw_inode(); |
| 320 | |
| 321 | if (!ri) |
| 322 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | c = JFFS2_SB_INFO(dir_i->i_sb); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 325 | |
| 326 | /* Try to reserve enough space for both node and dirent. |
| 327 | * Just the node will do for now, though |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | */ |
| 329 | namelen = dentry->d_name.len; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 330 | ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen, |
| 331 | ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | if (ret) { |
| 334 | jffs2_free_raw_inode(ri); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri); |
| 339 | |
| 340 | if (IS_ERR(inode)) { |
| 341 | jffs2_free_raw_inode(ri); |
| 342 | jffs2_complete_reservation(c); |
| 343 | return PTR_ERR(inode); |
| 344 | } |
| 345 | |
| 346 | inode->i_op = &jffs2_symlink_inode_operations; |
| 347 | |
| 348 | f = JFFS2_INODE_INFO(inode); |
| 349 | |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 350 | inode->i_size = targetlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size); |
| 352 | ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size); |
| 353 | ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); |
| 354 | |
| 355 | ri->compr = JFFS2_COMPR_NONE; |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 356 | ri->data_crc = cpu_to_je32(crc32(0, target, targetlen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 358 | |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 359 | fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | jffs2_free_raw_inode(ri); |
| 362 | |
| 363 | if (IS_ERR(fn)) { |
| 364 | /* Eeek. Wave bye bye */ |
| 365 | up(&f->sem); |
| 366 | jffs2_complete_reservation(c); |
| 367 | jffs2_clear_inode(inode); |
| 368 | return PTR_ERR(fn); |
| 369 | } |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 370 | |
Artem B. Bityutskiy | 2b79adc | 2005-07-17 12:13:51 +0100 | [diff] [blame] | 371 | /* We use f->target field to store the target path. */ |
| 372 | f->target = kmalloc(targetlen + 1, GFP_KERNEL); |
| 373 | if (!f->target) { |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 374 | printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1); |
| 375 | up(&f->sem); |
| 376 | jffs2_complete_reservation(c); |
| 377 | jffs2_clear_inode(inode); |
| 378 | return -ENOMEM; |
| 379 | } |
| 380 | |
Artem B. Bityutskiy | 2b79adc | 2005-07-17 12:13:51 +0100 | [diff] [blame] | 381 | memcpy(f->target, target, targetlen + 1); |
| 382 | D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target)); |
Artem B. Bityuckiy | 32f1a95 | 2005-03-01 10:50:52 +0000 | [diff] [blame] | 383 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 384 | /* No data here. Only a metadata node, which will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | obsoleted by the first data write |
| 386 | */ |
| 387 | f->metadata = fn; |
| 388 | up(&f->sem); |
| 389 | |
| 390 | jffs2_complete_reservation(c); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 391 | |
| 392 | ret = jffs2_init_security(inode, dir_i); |
| 393 | if (ret) { |
| 394 | jffs2_clear_inode(inode); |
| 395 | return ret; |
| 396 | } |
| 397 | ret = jffs2_init_acl(inode, dir_i); |
| 398 | if (ret) { |
| 399 | jffs2_clear_inode(inode); |
| 400 | return ret; |
| 401 | } |
| 402 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 403 | ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, |
| 404 | ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (ret) { |
| 406 | /* Eep. */ |
| 407 | jffs2_clear_inode(inode); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | rd = jffs2_alloc_raw_dirent(); |
| 412 | if (!rd) { |
| 413 | /* Argh. Now we treat it like a normal delete */ |
| 414 | jffs2_complete_reservation(c); |
| 415 | jffs2_clear_inode(inode); |
| 416 | return -ENOMEM; |
| 417 | } |
| 418 | |
| 419 | dir_f = JFFS2_INODE_INFO(dir_i); |
| 420 | down(&dir_f->sem); |
| 421 | |
| 422 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 423 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); |
| 424 | rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); |
| 425 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); |
| 426 | |
| 427 | rd->pino = cpu_to_je32(dir_i->i_ino); |
| 428 | rd->version = cpu_to_je32(++dir_f->highest_version); |
| 429 | rd->ino = cpu_to_je32(inode->i_ino); |
| 430 | rd->mctime = cpu_to_je32(get_seconds()); |
| 431 | rd->nsize = namelen; |
| 432 | rd->type = DT_LNK; |
| 433 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); |
| 434 | rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); |
| 435 | |
| 436 | fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); |
| 437 | |
| 438 | if (IS_ERR(fd)) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 439 | /* dirent failed to write. Delete the inode normally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | as if it were the final unlink() */ |
| 441 | jffs2_complete_reservation(c); |
| 442 | jffs2_free_raw_dirent(rd); |
| 443 | up(&dir_f->sem); |
| 444 | jffs2_clear_inode(inode); |
| 445 | return PTR_ERR(fd); |
| 446 | } |
| 447 | |
| 448 | dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); |
| 449 | |
| 450 | jffs2_free_raw_dirent(rd); |
| 451 | |
| 452 | /* Link the fd into the inode's list, obsoleting an old |
| 453 | one if necessary. */ |
| 454 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); |
| 455 | |
| 456 | up(&dir_f->sem); |
| 457 | jffs2_complete_reservation(c); |
| 458 | |
| 459 | d_instantiate(dentry, inode); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode) |
| 465 | { |
| 466 | struct jffs2_inode_info *f, *dir_f; |
| 467 | struct jffs2_sb_info *c; |
| 468 | struct inode *inode; |
| 469 | struct jffs2_raw_inode *ri; |
| 470 | struct jffs2_raw_dirent *rd; |
| 471 | struct jffs2_full_dnode *fn; |
| 472 | struct jffs2_full_dirent *fd; |
| 473 | int namelen; |
| 474 | uint32_t alloclen, phys_ofs; |
| 475 | int ret; |
| 476 | |
| 477 | mode |= S_IFDIR; |
| 478 | |
| 479 | ri = jffs2_alloc_raw_inode(); |
| 480 | if (!ri) |
| 481 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | c = JFFS2_SB_INFO(dir_i->i_sb); |
| 484 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 485 | /* Try to reserve enough space for both node and dirent. |
| 486 | * Just the node will do for now, though |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | */ |
| 488 | namelen = dentry->d_name.len; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 489 | ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL, |
| 490 | JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | if (ret) { |
| 493 | jffs2_free_raw_inode(ri); |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | inode = jffs2_new_inode(dir_i, mode, ri); |
| 498 | |
| 499 | if (IS_ERR(inode)) { |
| 500 | jffs2_free_raw_inode(ri); |
| 501 | jffs2_complete_reservation(c); |
| 502 | return PTR_ERR(inode); |
| 503 | } |
| 504 | |
| 505 | inode->i_op = &jffs2_dir_inode_operations; |
| 506 | inode->i_fop = &jffs2_dir_operations; |
| 507 | /* Directories get nlink 2 at start */ |
| 508 | inode->i_nlink = 2; |
| 509 | |
| 510 | f = JFFS2_INODE_INFO(inode); |
| 511 | |
| 512 | ri->data_crc = cpu_to_je32(0); |
| 513 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL); |
| 516 | |
| 517 | jffs2_free_raw_inode(ri); |
| 518 | |
| 519 | if (IS_ERR(fn)) { |
| 520 | /* Eeek. Wave bye bye */ |
| 521 | up(&f->sem); |
| 522 | jffs2_complete_reservation(c); |
| 523 | jffs2_clear_inode(inode); |
| 524 | return PTR_ERR(fn); |
| 525 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 526 | /* No data here. Only a metadata node, which will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | obsoleted by the first data write |
| 528 | */ |
| 529 | f->metadata = fn; |
| 530 | up(&f->sem); |
| 531 | |
| 532 | jffs2_complete_reservation(c); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 533 | |
| 534 | ret = jffs2_init_security(inode, dir_i); |
| 535 | if (ret) { |
| 536 | jffs2_clear_inode(inode); |
| 537 | return ret; |
| 538 | } |
| 539 | ret = jffs2_init_acl(inode, dir_i); |
| 540 | if (ret) { |
| 541 | jffs2_clear_inode(inode); |
| 542 | return ret; |
| 543 | } |
| 544 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 545 | ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, |
| 546 | ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (ret) { |
| 548 | /* Eep. */ |
| 549 | jffs2_clear_inode(inode); |
| 550 | return ret; |
| 551 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | rd = jffs2_alloc_raw_dirent(); |
| 554 | if (!rd) { |
| 555 | /* Argh. Now we treat it like a normal delete */ |
| 556 | jffs2_complete_reservation(c); |
| 557 | jffs2_clear_inode(inode); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | |
| 561 | dir_f = JFFS2_INODE_INFO(dir_i); |
| 562 | down(&dir_f->sem); |
| 563 | |
| 564 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 565 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); |
| 566 | rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); |
| 567 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); |
| 568 | |
| 569 | rd->pino = cpu_to_je32(dir_i->i_ino); |
| 570 | rd->version = cpu_to_je32(++dir_f->highest_version); |
| 571 | rd->ino = cpu_to_je32(inode->i_ino); |
| 572 | rd->mctime = cpu_to_je32(get_seconds()); |
| 573 | rd->nsize = namelen; |
| 574 | rd->type = DT_DIR; |
| 575 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); |
| 576 | rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); |
| 577 | |
| 578 | fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (IS_ERR(fd)) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 581 | /* dirent failed to write. Delete the inode normally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | as if it were the final unlink() */ |
| 583 | jffs2_complete_reservation(c); |
| 584 | jffs2_free_raw_dirent(rd); |
| 585 | up(&dir_f->sem); |
| 586 | jffs2_clear_inode(inode); |
| 587 | return PTR_ERR(fd); |
| 588 | } |
| 589 | |
| 590 | dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); |
| 591 | dir_i->i_nlink++; |
| 592 | |
| 593 | jffs2_free_raw_dirent(rd); |
| 594 | |
| 595 | /* Link the fd into the inode's list, obsoleting an old |
| 596 | one if necessary. */ |
| 597 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); |
| 598 | |
| 599 | up(&dir_f->sem); |
| 600 | jffs2_complete_reservation(c); |
| 601 | |
| 602 | d_instantiate(dentry, inode); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry) |
| 607 | { |
| 608 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode); |
| 609 | struct jffs2_full_dirent *fd; |
| 610 | int ret; |
| 611 | |
| 612 | for (fd = f->dents ; fd; fd = fd->next) { |
| 613 | if (fd->ino) |
| 614 | return -ENOTEMPTY; |
| 615 | } |
| 616 | ret = jffs2_unlink(dir_i, dentry); |
| 617 | if (!ret) |
| 618 | dir_i->i_nlink--; |
| 619 | return ret; |
| 620 | } |
| 621 | |
David Woodhouse | 265489f | 2005-07-06 13:13:13 +0100 | [diff] [blame] | 622 | static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | struct jffs2_inode_info *f, *dir_f; |
| 625 | struct jffs2_sb_info *c; |
| 626 | struct inode *inode; |
| 627 | struct jffs2_raw_inode *ri; |
| 628 | struct jffs2_raw_dirent *rd; |
| 629 | struct jffs2_full_dnode *fn; |
| 630 | struct jffs2_full_dirent *fd; |
| 631 | int namelen; |
David Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 632 | union jffs2_device_node dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | int devlen = 0; |
| 634 | uint32_t alloclen, phys_ofs; |
| 635 | int ret; |
| 636 | |
David Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 637 | if (!new_valid_dev(rdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return -EINVAL; |
| 639 | |
| 640 | ri = jffs2_alloc_raw_inode(); |
| 641 | if (!ri) |
| 642 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | c = JFFS2_SB_INFO(dir_i->i_sb); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 645 | |
David Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 646 | if (S_ISBLK(mode) || S_ISCHR(mode)) |
| 647 | devlen = jffs2_encode_dev(&dev, rdev); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 648 | |
| 649 | /* Try to reserve enough space for both node and dirent. |
| 650 | * Just the node will do for now, though |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | */ |
| 652 | namelen = dentry->d_name.len; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 653 | ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, |
David Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 654 | ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | if (ret) { |
| 657 | jffs2_free_raw_inode(ri); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | inode = jffs2_new_inode(dir_i, mode, ri); |
| 662 | |
| 663 | if (IS_ERR(inode)) { |
| 664 | jffs2_free_raw_inode(ri); |
| 665 | jffs2_complete_reservation(c); |
| 666 | return PTR_ERR(inode); |
| 667 | } |
| 668 | inode->i_op = &jffs2_file_inode_operations; |
| 669 | init_special_inode(inode, inode->i_mode, rdev); |
| 670 | |
| 671 | f = JFFS2_INODE_INFO(inode); |
| 672 | |
| 673 | ri->dsize = ri->csize = cpu_to_je32(devlen); |
| 674 | ri->totlen = cpu_to_je32(sizeof(*ri) + devlen); |
| 675 | ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); |
| 676 | |
| 677 | ri->compr = JFFS2_COMPR_NONE; |
| 678 | ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen)); |
| 679 | ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL); |
| 682 | |
| 683 | jffs2_free_raw_inode(ri); |
| 684 | |
| 685 | if (IS_ERR(fn)) { |
| 686 | /* Eeek. Wave bye bye */ |
| 687 | up(&f->sem); |
| 688 | jffs2_complete_reservation(c); |
| 689 | jffs2_clear_inode(inode); |
| 690 | return PTR_ERR(fn); |
| 691 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 692 | /* No data here. Only a metadata node, which will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | obsoleted by the first data write |
| 694 | */ |
| 695 | f->metadata = fn; |
| 696 | up(&f->sem); |
| 697 | |
| 698 | jffs2_complete_reservation(c); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 699 | |
| 700 | ret = jffs2_init_security(inode, dir_i); |
| 701 | if (ret) { |
| 702 | jffs2_clear_inode(inode); |
| 703 | return ret; |
| 704 | } |
| 705 | ret = jffs2_init_acl(inode, dir_i); |
| 706 | if (ret) { |
| 707 | jffs2_clear_inode(inode); |
| 708 | return ret; |
| 709 | } |
| 710 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 711 | ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, |
| 712 | ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | if (ret) { |
| 714 | /* Eep. */ |
| 715 | jffs2_clear_inode(inode); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | rd = jffs2_alloc_raw_dirent(); |
| 720 | if (!rd) { |
| 721 | /* Argh. Now we treat it like a normal delete */ |
| 722 | jffs2_complete_reservation(c); |
| 723 | jffs2_clear_inode(inode); |
| 724 | return -ENOMEM; |
| 725 | } |
| 726 | |
| 727 | dir_f = JFFS2_INODE_INFO(dir_i); |
| 728 | down(&dir_f->sem); |
| 729 | |
| 730 | rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 731 | rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); |
| 732 | rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); |
| 733 | rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); |
| 734 | |
| 735 | rd->pino = cpu_to_je32(dir_i->i_ino); |
| 736 | rd->version = cpu_to_je32(++dir_f->highest_version); |
| 737 | rd->ino = cpu_to_je32(inode->i_ino); |
| 738 | rd->mctime = cpu_to_je32(get_seconds()); |
| 739 | rd->nsize = namelen; |
| 740 | |
| 741 | /* XXX: This is ugly. */ |
| 742 | rd->type = (mode & S_IFMT) >> 12; |
| 743 | |
| 744 | rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); |
| 745 | rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); |
| 746 | |
| 747 | fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (IS_ERR(fd)) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 750 | /* dirent failed to write. Delete the inode normally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | as if it were the final unlink() */ |
| 752 | jffs2_complete_reservation(c); |
| 753 | jffs2_free_raw_dirent(rd); |
| 754 | up(&dir_f->sem); |
| 755 | jffs2_clear_inode(inode); |
| 756 | return PTR_ERR(fd); |
| 757 | } |
| 758 | |
| 759 | dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime)); |
| 760 | |
| 761 | jffs2_free_raw_dirent(rd); |
| 762 | |
| 763 | /* Link the fd into the inode's list, obsoleting an old |
| 764 | one if necessary. */ |
| 765 | jffs2_add_fd_to_list(c, fd, &dir_f->dents); |
| 766 | |
| 767 | up(&dir_f->sem); |
| 768 | jffs2_complete_reservation(c); |
| 769 | |
| 770 | d_instantiate(dentry, inode); |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry, |
| 776 | struct inode *new_dir_i, struct dentry *new_dentry) |
| 777 | { |
| 778 | int ret; |
| 779 | struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb); |
| 780 | struct jffs2_inode_info *victim_f = NULL; |
| 781 | uint8_t type; |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 782 | uint32_t now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 784 | /* The VFS will check for us and prevent trying to rename a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | * file over a directory and vice versa, but if it's a directory, |
| 786 | * the VFS can't check whether the victim is empty. The filesystem |
| 787 | * needs to do that for itself. |
| 788 | */ |
| 789 | if (new_dentry->d_inode) { |
| 790 | victim_f = JFFS2_INODE_INFO(new_dentry->d_inode); |
| 791 | if (S_ISDIR(new_dentry->d_inode->i_mode)) { |
| 792 | struct jffs2_full_dirent *fd; |
| 793 | |
| 794 | down(&victim_f->sem); |
| 795 | for (fd = victim_f->dents; fd; fd = fd->next) { |
| 796 | if (fd->ino) { |
| 797 | up(&victim_f->sem); |
| 798 | return -ENOTEMPTY; |
| 799 | } |
| 800 | } |
| 801 | up(&victim_f->sem); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* XXX: We probably ought to alloc enough space for |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 806 | both nodes at the same time. Writing the new link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | then getting -ENOSPC, is quite bad :) |
| 808 | */ |
| 809 | |
| 810 | /* Make a hard link */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* XXX: This is ugly */ |
| 813 | type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; |
| 814 | if (!type) type = DT_REG; |
| 815 | |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 816 | now = get_seconds(); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 817 | ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | old_dentry->d_inode->i_ino, type, |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 819 | new_dentry->d_name.name, new_dentry->d_name.len, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | if (ret) |
| 822 | return ret; |
| 823 | |
| 824 | if (victim_f) { |
| 825 | /* There was a victim. Kill it off nicely */ |
| 826 | new_dentry->d_inode->i_nlink--; |
| 827 | /* Don't oops if the victim was a dirent pointing to an |
| 828 | inode which didn't exist. */ |
| 829 | if (victim_f->inocache) { |
| 830 | down(&victim_f->sem); |
| 831 | victim_f->inocache->nlink--; |
| 832 | up(&victim_f->sem); |
| 833 | } |
| 834 | } |
| 835 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 836 | /* If it was a directory we moved, and there was no victim, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | increase i_nlink on its new parent */ |
| 838 | if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f) |
| 839 | new_dir_i->i_nlink++; |
| 840 | |
| 841 | /* Unlink the original */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 842 | ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i), |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 843 | old_dentry->d_name.name, old_dentry->d_name.len, NULL, now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
| 845 | /* We don't touch inode->i_nlink */ |
| 846 | |
| 847 | if (ret) { |
| 848 | /* Oh shit. We really ought to make a single node which can do both atomically */ |
| 849 | struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode); |
| 850 | down(&f->sem); |
| 851 | old_dentry->d_inode->i_nlink++; |
| 852 | if (f->inocache) |
| 853 | f->inocache->nlink++; |
| 854 | up(&f->sem); |
| 855 | |
| 856 | printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret); |
| 857 | /* Might as well let the VFS know */ |
| 858 | d_instantiate(new_dentry, old_dentry->d_inode); |
| 859 | atomic_inc(&old_dentry->d_inode->i_count); |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 860 | new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | if (S_ISDIR(old_dentry->d_inode->i_mode)) |
| 865 | old_dir_i->i_nlink--; |
| 866 | |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 867 | new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now); |
| 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |