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