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