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