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