1 /* 2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README 3 * 4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility 5 * 6 * Trivial Changes: 7 * Rights granted to Hans Reiser to redistribute under other terms providing 8 * he accepts all liability including but not limited to patent, fitness 9 * for purpose, and direct or indirect claims arising from failure to perform. 10 * 11 * NO WARRANTY 12 */ 13 14 #include <linux/time.h> 15 #include <linux/bitops.h> 16 #include <linux/slab.h> 17 #include "reiserfs.h" 18 #include "acl.h" 19 #include "xattr.h" 20 #include <linux/quotaops.h> 21 22 #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); } 23 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i); 24 25 // directory item contains array of entry headers. This performs 26 // binary search through that array 27 static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off) 28 { 29 struct item_head *ih = de->de_ih; 30 struct reiserfs_de_head *deh = de->de_deh; 31 int rbound, lbound, j; 32 33 lbound = 0; 34 rbound = I_ENTRY_COUNT(ih) - 1; 35 36 for (j = (rbound + lbound) / 2; lbound <= rbound; 37 j = (rbound + lbound) / 2) { 38 if (off < deh_offset(deh + j)) { 39 rbound = j - 1; 40 continue; 41 } 42 if (off > deh_offset(deh + j)) { 43 lbound = j + 1; 44 continue; 45 } 46 // this is not name found, but matched third key component 47 de->de_entry_num = j; 48 return NAME_FOUND; 49 } 50 51 de->de_entry_num = lbound; 52 return NAME_NOT_FOUND; 53 } 54 55 // comment? maybe something like set de to point to what the path points to? 56 static inline void set_de_item_location(struct reiserfs_dir_entry *de, 57 struct treepath *path) 58 { 59 de->de_bh = get_last_bh(path); 60 de->de_ih = get_ih(path); 61 de->de_deh = B_I_DEH(de->de_bh, de->de_ih); 62 de->de_item_num = PATH_LAST_POSITION(path); 63 } 64 65 // de_bh, de_ih, de_deh (points to first element of array), de_item_num is set 66 inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de) 67 { 68 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num; 69 70 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih)); 71 72 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num); 73 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0); 74 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh); 75 if (de->de_name[de->de_namelen - 1] == 0) 76 de->de_namelen = strlen(de->de_name); 77 } 78 79 // what entry points to 80 static inline void set_de_object_key(struct reiserfs_dir_entry *de) 81 { 82 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih)); 83 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num])); 84 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num])); 85 } 86 87 static inline void store_de_entry_key(struct reiserfs_dir_entry *de) 88 { 89 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num; 90 91 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih)); 92 93 /* store key of the found entry */ 94 de->de_entry_key.version = KEY_FORMAT_3_5; 95 de->de_entry_key.on_disk_key.k_dir_id = 96 le32_to_cpu(de->de_ih->ih_key.k_dir_id); 97 de->de_entry_key.on_disk_key.k_objectid = 98 le32_to_cpu(de->de_ih->ih_key.k_objectid); 99 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh)); 100 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY); 101 } 102 103 /* We assign a key to each directory item, and place multiple entries 104 in a single directory item. A directory item has a key equal to the 105 key of the first directory entry in it. 106 107 This function first calls search_by_key, then, if item whose first 108 entry matches is not found it looks for the entry inside directory 109 item found by search_by_key. Fills the path to the entry, and to the 110 entry position in the item 111 112 */ 113 114 /* The function is NOT SCHEDULE-SAFE! */ 115 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key, 116 struct treepath *path, struct reiserfs_dir_entry *de) 117 { 118 int retval; 119 120 retval = search_item(sb, key, path); 121 switch (retval) { 122 case ITEM_NOT_FOUND: 123 if (!PATH_LAST_POSITION(path)) { 124 reiserfs_error(sb, "vs-7000", "search_by_key " 125 "returned item position == 0"); 126 pathrelse(path); 127 return IO_ERROR; 128 } 129 PATH_LAST_POSITION(path)--; 130 131 case ITEM_FOUND: 132 break; 133 134 case IO_ERROR: 135 return retval; 136 137 default: 138 pathrelse(path); 139 reiserfs_error(sb, "vs-7002", "no path to here"); 140 return IO_ERROR; 141 } 142 143 set_de_item_location(de, path); 144 145 #ifdef CONFIG_REISERFS_CHECK 146 if (!is_direntry_le_ih(de->de_ih) || 147 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) { 148 print_block(de->de_bh, 0, -1, -1); 149 reiserfs_panic(sb, "vs-7005", "found item %h is not directory " 150 "item or does not belong to the same directory " 151 "as key %K", de->de_ih, key); 152 } 153 #endif /* CONFIG_REISERFS_CHECK */ 154 155 /* binary search in directory item by third componen t of the 156 key. sets de->de_entry_num of de */ 157 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key)); 158 path->pos_in_item = de->de_entry_num; 159 if (retval != NAME_NOT_FOUND) { 160 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set 161 set_de_name_and_namelen(de); 162 set_de_object_key(de); 163 } 164 return retval; 165 } 166 167 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */ 168 169 /* The third component is hashed, and you can choose from more than 170 one hash function. Per directory hashes are not yet implemented 171 but are thought about. This function should be moved to hashes.c 172 Jedi, please do so. -Hans */ 173 174 static __u32 get_third_component(struct super_block *s, 175 const char *name, int len) 176 { 177 __u32 res; 178 179 if (!len || (len == 1 && name[0] == '.')) 180 return DOT_OFFSET; 181 if (len == 2 && name[0] == '.' && name[1] == '.') 182 return DOT_DOT_OFFSET; 183 184 res = REISERFS_SB(s)->s_hash_function(name, len); 185 186 // take bits from 7-th to 30-th including both bounds 187 res = GET_HASH_VALUE(res); 188 if (res == 0) 189 // needed to have no names before "." and ".." those have hash 190 // value == 0 and generation conters 1 and 2 accordingly 191 res = 128; 192 return res + MAX_GENERATION_NUMBER; 193 } 194 195 static int reiserfs_match(struct reiserfs_dir_entry *de, 196 const char *name, int namelen) 197 { 198 int retval = NAME_NOT_FOUND; 199 200 if ((namelen == de->de_namelen) && 201 !memcmp(de->de_name, name, de->de_namelen)) 202 retval = 203 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND : 204 NAME_FOUND_INVISIBLE); 205 206 return retval; 207 } 208 209 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */ 210 211 /* used when hash collisions exist */ 212 213 static int linear_search_in_dir_item(struct cpu_key *key, 214 struct reiserfs_dir_entry *de, 215 const char *name, int namelen) 216 { 217 struct reiserfs_de_head *deh = de->de_deh; 218 int retval; 219 int i; 220 221 i = de->de_entry_num; 222 223 if (i == I_ENTRY_COUNT(de->de_ih) || 224 GET_HASH_VALUE(deh_offset(deh + i)) != 225 GET_HASH_VALUE(cpu_key_k_offset(key))) { 226 i--; 227 } 228 229 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih), 230 "vs-7010: array of entry headers not found"); 231 232 deh += i; 233 234 for (; i >= 0; i--, deh--) { 235 if (GET_HASH_VALUE(deh_offset(deh)) != 236 GET_HASH_VALUE(cpu_key_k_offset(key))) { 237 // hash value does not match, no need to check whole name 238 return NAME_NOT_FOUND; 239 } 240 241 /* mark, that this generation number is used */ 242 if (de->de_gen_number_bit_string) 243 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)), 244 de->de_gen_number_bit_string); 245 246 // calculate pointer to name and namelen 247 de->de_entry_num = i; 248 set_de_name_and_namelen(de); 249 250 if ((retval = 251 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) { 252 // de's de_name, de_namelen, de_recordlen are set. Fill the rest: 253 254 // key of pointed object 255 set_de_object_key(de); 256 257 store_de_entry_key(de); 258 259 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE 260 return retval; 261 } 262 } 263 264 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0) 265 /* we have reached left most entry in the node. In common we 266 have to go to the left neighbor, but if generation counter 267 is 0 already, we know for sure, that there is no name with 268 the same hash value */ 269 // FIXME: this work correctly only because hash value can not 270 // be 0. Btw, in case of Yura's hash it is probably possible, 271 // so, this is a bug 272 return NAME_NOT_FOUND; 273 274 RFALSE(de->de_item_num, 275 "vs-7015: two diritems of the same directory in one node?"); 276 277 return GOTO_PREVIOUS_ITEM; 278 } 279 280 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND 281 // FIXME: should add something like IOERROR 282 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen, 283 struct treepath *path_to_entry, 284 struct reiserfs_dir_entry *de) 285 { 286 struct cpu_key key_to_search; 287 int retval; 288 289 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize)) 290 return NAME_NOT_FOUND; 291 292 /* we will search for this key in the tree */ 293 make_cpu_key(&key_to_search, dir, 294 get_third_component(dir->i_sb, name, namelen), 295 TYPE_DIRENTRY, 3); 296 297 while (1) { 298 retval = 299 search_by_entry_key(dir->i_sb, &key_to_search, 300 path_to_entry, de); 301 if (retval == IO_ERROR) { 302 reiserfs_error(dir->i_sb, "zam-7001", "io error"); 303 return IO_ERROR; 304 } 305 306 /* compare names for all entries having given hash value */ 307 retval = 308 linear_search_in_dir_item(&key_to_search, de, name, 309 namelen); 310 if (retval != GOTO_PREVIOUS_ITEM) { 311 /* there is no need to scan directory anymore. Given entry found or does not exist */ 312 path_to_entry->pos_in_item = de->de_entry_num; 313 return retval; 314 } 315 316 /* there is left neighboring item of this directory and given entry can be there */ 317 set_cpu_key_k_offset(&key_to_search, 318 le_ih_k_offset(de->de_ih) - 1); 319 pathrelse(path_to_entry); 320 321 } /* while (1) */ 322 } 323 324 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry, 325 unsigned int flags) 326 { 327 int retval; 328 struct inode *inode = NULL; 329 struct reiserfs_dir_entry de; 330 INITIALIZE_PATH(path_to_entry); 331 332 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len) 333 return ERR_PTR(-ENAMETOOLONG); 334 335 reiserfs_write_lock(dir->i_sb); 336 337 de.de_gen_number_bit_string = NULL; 338 retval = 339 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, 340 &path_to_entry, &de); 341 pathrelse(&path_to_entry); 342 if (retval == NAME_FOUND) { 343 inode = reiserfs_iget(dir->i_sb, 344 (struct cpu_key *)&(de.de_dir_id)); 345 if (!inode || IS_ERR(inode)) { 346 reiserfs_write_unlock(dir->i_sb); 347 return ERR_PTR(-EACCES); 348 } 349 350 /* Propagate the private flag so we know we're 351 * in the priv tree */ 352 if (IS_PRIVATE(dir)) 353 inode->i_flags |= S_PRIVATE; 354 } 355 reiserfs_write_unlock(dir->i_sb); 356 if (retval == IO_ERROR) { 357 return ERR_PTR(-EIO); 358 } 359 360 return d_splice_alias(inode, dentry); 361 } 362 363 /* 364 ** looks up the dentry of the parent directory for child. 365 ** taken from ext2_get_parent 366 */ 367 struct dentry *reiserfs_get_parent(struct dentry *child) 368 { 369 int retval; 370 struct inode *inode = NULL; 371 struct reiserfs_dir_entry de; 372 INITIALIZE_PATH(path_to_entry); 373 struct inode *dir = child->d_inode; 374 375 if (dir->i_nlink == 0) { 376 return ERR_PTR(-ENOENT); 377 } 378 de.de_gen_number_bit_string = NULL; 379 380 reiserfs_write_lock(dir->i_sb); 381 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de); 382 pathrelse(&path_to_entry); 383 if (retval != NAME_FOUND) { 384 reiserfs_write_unlock(dir->i_sb); 385 return ERR_PTR(-ENOENT); 386 } 387 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id)); 388 reiserfs_write_unlock(dir->i_sb); 389 390 return d_obtain_alias(inode); 391 } 392 393 /* add entry to the directory (entry can be hidden). 394 395 insert definition of when hidden directories are used here -Hans 396 397 Does not mark dir inode dirty, do it after successesfull call to it */ 398 399 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th, 400 struct inode *dir, const char *name, int namelen, 401 struct inode *inode, int visible) 402 { 403 struct cpu_key entry_key; 404 struct reiserfs_de_head *deh; 405 INITIALIZE_PATH(path); 406 struct reiserfs_dir_entry de; 407 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1); 408 int gen_number; 409 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc 410 if we create file with short name */ 411 char *buffer; 412 int buflen, paste_size; 413 int retval; 414 415 BUG_ON(!th->t_trans_id); 416 417 /* cannot allow items to be added into a busy deleted directory */ 418 if (!namelen) 419 return -EINVAL; 420 421 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize)) 422 return -ENAMETOOLONG; 423 424 /* each entry has unique key. compose it */ 425 make_cpu_key(&entry_key, dir, 426 get_third_component(dir->i_sb, name, namelen), 427 TYPE_DIRENTRY, 3); 428 429 /* get memory for composing the entry */ 430 buflen = DEH_SIZE + ROUND_UP(namelen); 431 if (buflen > sizeof(small_buf)) { 432 buffer = kmalloc(buflen, GFP_NOFS); 433 if (!buffer) 434 return -ENOMEM; 435 } else 436 buffer = small_buf; 437 438 paste_size = 439 (get_inode_sd_version(dir) == 440 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen; 441 442 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */ 443 deh = (struct reiserfs_de_head *)buffer; 444 deh->deh_location = 0; /* JDM Endian safe if 0 */ 445 put_deh_offset(deh, cpu_key_k_offset(&entry_key)); 446 deh->deh_state = 0; /* JDM Endian safe if 0 */ 447 /* put key (ino analog) to de */ 448 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */ 449 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */ 450 451 /* copy name */ 452 memcpy((char *)(deh + 1), name, namelen); 453 /* padd by 0s to the 4 byte boundary */ 454 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen); 455 456 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */ 457 mark_de_without_sd(deh); 458 visible ? mark_de_visible(deh) : mark_de_hidden(deh); 459 460 /* find the proper place for the new entry */ 461 memset(bit_string, 0, sizeof(bit_string)); 462 de.de_gen_number_bit_string = bit_string; 463 retval = reiserfs_find_entry(dir, name, namelen, &path, &de); 464 if (retval != NAME_NOT_FOUND) { 465 if (buffer != small_buf) 466 kfree(buffer); 467 pathrelse(&path); 468 469 if (retval == IO_ERROR) { 470 return -EIO; 471 } 472 473 if (retval != NAME_FOUND) { 474 reiserfs_error(dir->i_sb, "zam-7002", 475 "reiserfs_find_entry() returned " 476 "unexpected value (%d)", retval); 477 } 478 479 return -EEXIST; 480 } 481 482 gen_number = 483 find_first_zero_bit(bit_string, 484 MAX_GENERATION_NUMBER + 1); 485 if (gen_number > MAX_GENERATION_NUMBER) { 486 /* there is no free generation number */ 487 reiserfs_warning(dir->i_sb, "reiserfs-7010", 488 "Congratulations! we have got hash function " 489 "screwed up"); 490 if (buffer != small_buf) 491 kfree(buffer); 492 pathrelse(&path); 493 return -EBUSY; 494 } 495 /* adjust offset of directory enrty */ 496 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number)); 497 set_cpu_key_k_offset(&entry_key, deh_offset(deh)); 498 499 /* update max-hash-collisions counter in reiserfs_sb_info */ 500 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number); 501 502 if (gen_number != 0) { /* we need to re-search for the insertion point */ 503 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) != 504 NAME_NOT_FOUND) { 505 reiserfs_warning(dir->i_sb, "vs-7032", 506 "entry with this key (%K) already " 507 "exists", &entry_key); 508 509 if (buffer != small_buf) 510 kfree(buffer); 511 pathrelse(&path); 512 return -EBUSY; 513 } 514 } 515 516 /* perform the insertion of the entry that we have prepared */ 517 retval = 518 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer, 519 paste_size); 520 if (buffer != small_buf) 521 kfree(buffer); 522 if (retval) { 523 reiserfs_check_path(&path); 524 return retval; 525 } 526 527 dir->i_size += paste_size; 528 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; 529 if (!S_ISDIR(inode->i_mode) && visible) 530 // reiserfs_mkdir or reiserfs_rename will do that by itself 531 reiserfs_update_sd(th, dir); 532 533 reiserfs_check_path(&path); 534 return 0; 535 } 536 537 /* quota utility function, call if you've had to abort after calling 538 ** new_inode_init, and have not called reiserfs_new_inode yet. 539 ** This should only be called on inodes that do not have stat data 540 ** inserted into the tree yet. 541 */ 542 static int drop_new_inode(struct inode *inode) 543 { 544 dquot_drop(inode); 545 make_bad_inode(inode); 546 inode->i_flags |= S_NOQUOTA; 547 iput(inode); 548 return 0; 549 } 550 551 /* utility function that does setup for reiserfs_new_inode. 552 ** dquot_initialize needs lots of credits so it's better to have it 553 ** outside of a transaction, so we had to pull some bits of 554 ** reiserfs_new_inode out into this func. 555 */ 556 static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode) 557 { 558 /* Make inode invalid - just in case we are going to drop it before 559 * the initialization happens */ 560 INODE_PKEY(inode)->k_objectid = 0; 561 /* the quota init calls have to know who to charge the quota to, so 562 ** we have to set uid and gid here 563 */ 564 inode_init_owner(inode, dir, mode); 565 dquot_initialize(inode); 566 return 0; 567 } 568 569 static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 570 bool excl) 571 { 572 int retval; 573 struct inode *inode; 574 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */ 575 int jbegin_count = 576 JOURNAL_PER_BALANCE_CNT * 2 + 577 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) + 578 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb)); 579 struct reiserfs_transaction_handle th; 580 struct reiserfs_security_handle security; 581 582 dquot_initialize(dir); 583 584 if (!(inode = new_inode(dir->i_sb))) { 585 return -ENOMEM; 586 } 587 new_inode_init(inode, dir, mode); 588 589 jbegin_count += reiserfs_cache_default_acl(dir); 590 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security); 591 if (retval < 0) { 592 drop_new_inode(inode); 593 return retval; 594 } 595 jbegin_count += retval; 596 reiserfs_write_lock(dir->i_sb); 597 598 retval = journal_begin(&th, dir->i_sb, jbegin_count); 599 if (retval) { 600 drop_new_inode(inode); 601 goto out_failed; 602 } 603 604 retval = 605 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry, 606 inode, &security); 607 if (retval) 608 goto out_failed; 609 610 inode->i_op = &reiserfs_file_inode_operations; 611 inode->i_fop = &reiserfs_file_operations; 612 inode->i_mapping->a_ops = &reiserfs_address_space_operations; 613 614 retval = 615 reiserfs_add_entry(&th, dir, dentry->d_name.name, 616 dentry->d_name.len, inode, 1 /*visible */ ); 617 if (retval) { 618 int err; 619 drop_nlink(inode); 620 reiserfs_update_sd(&th, inode); 621 err = journal_end(&th, dir->i_sb, jbegin_count); 622 if (err) 623 retval = err; 624 unlock_new_inode(inode); 625 iput(inode); 626 goto out_failed; 627 } 628 reiserfs_update_inode_transaction(inode); 629 reiserfs_update_inode_transaction(dir); 630 631 unlock_new_inode(inode); 632 d_instantiate(dentry, inode); 633 retval = journal_end(&th, dir->i_sb, jbegin_count); 634 635 out_failed: 636 reiserfs_write_unlock(dir->i_sb); 637 return retval; 638 } 639 640 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, 641 dev_t rdev) 642 { 643 int retval; 644 struct inode *inode; 645 struct reiserfs_transaction_handle th; 646 struct reiserfs_security_handle security; 647 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */ 648 int jbegin_count = 649 JOURNAL_PER_BALANCE_CNT * 3 + 650 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) + 651 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb)); 652 653 if (!new_valid_dev(rdev)) 654 return -EINVAL; 655 656 dquot_initialize(dir); 657 658 if (!(inode = new_inode(dir->i_sb))) { 659 return -ENOMEM; 660 } 661 new_inode_init(inode, dir, mode); 662 663 jbegin_count += reiserfs_cache_default_acl(dir); 664 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security); 665 if (retval < 0) { 666 drop_new_inode(inode); 667 return retval; 668 } 669 jbegin_count += retval; 670 reiserfs_write_lock(dir->i_sb); 671 672 retval = journal_begin(&th, dir->i_sb, jbegin_count); 673 if (retval) { 674 drop_new_inode(inode); 675 goto out_failed; 676 } 677 678 retval = 679 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry, 680 inode, &security); 681 if (retval) { 682 goto out_failed; 683 } 684 685 inode->i_op = &reiserfs_special_inode_operations; 686 init_special_inode(inode, inode->i_mode, rdev); 687 688 //FIXME: needed for block and char devices only 689 reiserfs_update_sd(&th, inode); 690 691 reiserfs_update_inode_transaction(inode); 692 reiserfs_update_inode_transaction(dir); 693 694 retval = 695 reiserfs_add_entry(&th, dir, dentry->d_name.name, 696 dentry->d_name.len, inode, 1 /*visible */ ); 697 if (retval) { 698 int err; 699 drop_nlink(inode); 700 reiserfs_update_sd(&th, inode); 701 err = journal_end(&th, dir->i_sb, jbegin_count); 702 if (err) 703 retval = err; 704 unlock_new_inode(inode); 705 iput(inode); 706 goto out_failed; 707 } 708 709 unlock_new_inode(inode); 710 d_instantiate(dentry, inode); 711 retval = journal_end(&th, dir->i_sb, jbegin_count); 712 713 out_failed: 714 reiserfs_write_unlock(dir->i_sb); 715 return retval; 716 } 717 718 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 719 { 720 int retval; 721 struct inode *inode; 722 struct reiserfs_transaction_handle th; 723 struct reiserfs_security_handle security; 724 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */ 725 int jbegin_count = 726 JOURNAL_PER_BALANCE_CNT * 3 + 727 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) + 728 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb)); 729 730 dquot_initialize(dir); 731 732 #ifdef DISPLACE_NEW_PACKING_LOCALITIES 733 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */ 734 REISERFS_I(dir)->new_packing_locality = 1; 735 #endif 736 mode = S_IFDIR | mode; 737 if (!(inode = new_inode(dir->i_sb))) { 738 return -ENOMEM; 739 } 740 new_inode_init(inode, dir, mode); 741 742 jbegin_count += reiserfs_cache_default_acl(dir); 743 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security); 744 if (retval < 0) { 745 drop_new_inode(inode); 746 return retval; 747 } 748 jbegin_count += retval; 749 reiserfs_write_lock(dir->i_sb); 750 751 retval = journal_begin(&th, dir->i_sb, jbegin_count); 752 if (retval) { 753 drop_new_inode(inode); 754 goto out_failed; 755 } 756 757 /* inc the link count now, so another writer doesn't overflow it while 758 ** we sleep later on. 759 */ 760 INC_DIR_INODE_NLINK(dir) 761 762 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ , 763 old_format_only(dir->i_sb) ? 764 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE, 765 dentry, inode, &security); 766 if (retval) { 767 DEC_DIR_INODE_NLINK(dir) 768 goto out_failed; 769 } 770 771 reiserfs_update_inode_transaction(inode); 772 reiserfs_update_inode_transaction(dir); 773 774 inode->i_op = &reiserfs_dir_inode_operations; 775 inode->i_fop = &reiserfs_dir_operations; 776 777 // note, _this_ add_entry will not update dir's stat data 778 retval = 779 reiserfs_add_entry(&th, dir, dentry->d_name.name, 780 dentry->d_name.len, inode, 1 /*visible */ ); 781 if (retval) { 782 int err; 783 clear_nlink(inode); 784 DEC_DIR_INODE_NLINK(dir); 785 reiserfs_update_sd(&th, inode); 786 err = journal_end(&th, dir->i_sb, jbegin_count); 787 if (err) 788 retval = err; 789 unlock_new_inode(inode); 790 iput(inode); 791 goto out_failed; 792 } 793 // the above add_entry did not update dir's stat data 794 reiserfs_update_sd(&th, dir); 795 796 unlock_new_inode(inode); 797 d_instantiate(dentry, inode); 798 retval = journal_end(&th, dir->i_sb, jbegin_count); 799 out_failed: 800 reiserfs_write_unlock(dir->i_sb); 801 return retval; 802 } 803 804 static inline int reiserfs_empty_dir(struct inode *inode) 805 { 806 /* we can cheat because an old format dir cannot have 807 ** EMPTY_DIR_SIZE, and a new format dir cannot have 808 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size, 809 ** regardless of disk format version, the directory is empty. 810 */ 811 if (inode->i_size != EMPTY_DIR_SIZE && 812 inode->i_size != EMPTY_DIR_SIZE_V1) { 813 return 0; 814 } 815 return 1; 816 } 817 818 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry) 819 { 820 int retval, err; 821 struct inode *inode; 822 struct reiserfs_transaction_handle th; 823 int jbegin_count; 824 INITIALIZE_PATH(path); 825 struct reiserfs_dir_entry de; 826 827 /* we will be doing 2 balancings and update 2 stat data, we change quotas 828 * of the owner of the directory and of the owner of the parent directory. 829 * The quota structure is possibly deleted only on last iput => outside 830 * of this transaction */ 831 jbegin_count = 832 JOURNAL_PER_BALANCE_CNT * 2 + 2 + 833 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb); 834 835 dquot_initialize(dir); 836 837 reiserfs_write_lock(dir->i_sb); 838 retval = journal_begin(&th, dir->i_sb, jbegin_count); 839 if (retval) 840 goto out_rmdir; 841 842 de.de_gen_number_bit_string = NULL; 843 if ((retval = 844 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, 845 &path, &de)) == NAME_NOT_FOUND) { 846 retval = -ENOENT; 847 goto end_rmdir; 848 } else if (retval == IO_ERROR) { 849 retval = -EIO; 850 goto end_rmdir; 851 } 852 853 inode = dentry->d_inode; 854 855 reiserfs_update_inode_transaction(inode); 856 reiserfs_update_inode_transaction(dir); 857 858 if (de.de_objectid != inode->i_ino) { 859 // FIXME: compare key of an object and a key found in the 860 // entry 861 retval = -EIO; 862 goto end_rmdir; 863 } 864 if (!reiserfs_empty_dir(inode)) { 865 retval = -ENOTEMPTY; 866 goto end_rmdir; 867 } 868 869 /* cut entry from dir directory */ 870 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */ 871 0 /*new file size - not used here */ ); 872 if (retval < 0) 873 goto end_rmdir; 874 875 if (inode->i_nlink != 2 && inode->i_nlink != 1) 876 reiserfs_error(inode->i_sb, "reiserfs-7040", 877 "empty directory has nlink != 2 (%d)", 878 inode->i_nlink); 879 880 clear_nlink(inode); 881 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; 882 reiserfs_update_sd(&th, inode); 883 884 DEC_DIR_INODE_NLINK(dir) 885 dir->i_size -= (DEH_SIZE + de.de_entrylen); 886 reiserfs_update_sd(&th, dir); 887 888 /* prevent empty directory from getting lost */ 889 add_save_link(&th, inode, 0 /* not truncate */ ); 890 891 retval = journal_end(&th, dir->i_sb, jbegin_count); 892 reiserfs_check_path(&path); 893 out_rmdir: 894 reiserfs_write_unlock(dir->i_sb); 895 return retval; 896 897 end_rmdir: 898 /* we must release path, because we did not call 899 reiserfs_cut_from_item, or reiserfs_cut_from_item does not 900 release path if operation was not complete */ 901 pathrelse(&path); 902 err = journal_end(&th, dir->i_sb, jbegin_count); 903 reiserfs_write_unlock(dir->i_sb); 904 return err ? err : retval; 905 } 906 907 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry) 908 { 909 int retval, err; 910 struct inode *inode; 911 struct reiserfs_dir_entry de; 912 INITIALIZE_PATH(path); 913 struct reiserfs_transaction_handle th; 914 int jbegin_count; 915 unsigned long savelink; 916 917 dquot_initialize(dir); 918 919 inode = dentry->d_inode; 920 921 /* in this transaction we can be doing at max two balancings and update 922 * two stat datas, we change quotas of the owner of the directory and of 923 * the owner of the parent directory. The quota structure is possibly 924 * deleted only on iput => outside of this transaction */ 925 jbegin_count = 926 JOURNAL_PER_BALANCE_CNT * 2 + 2 + 927 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb); 928 929 reiserfs_write_lock(dir->i_sb); 930 retval = journal_begin(&th, dir->i_sb, jbegin_count); 931 if (retval) 932 goto out_unlink; 933 934 de.de_gen_number_bit_string = NULL; 935 if ((retval = 936 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, 937 &path, &de)) == NAME_NOT_FOUND) { 938 retval = -ENOENT; 939 goto end_unlink; 940 } else if (retval == IO_ERROR) { 941 retval = -EIO; 942 goto end_unlink; 943 } 944 945 reiserfs_update_inode_transaction(inode); 946 reiserfs_update_inode_transaction(dir); 947 948 if (de.de_objectid != inode->i_ino) { 949 // FIXME: compare key of an object and a key found in the 950 // entry 951 retval = -EIO; 952 goto end_unlink; 953 } 954 955 if (!inode->i_nlink) { 956 reiserfs_warning(inode->i_sb, "reiserfs-7042", 957 "deleting nonexistent file (%lu), %d", 958 inode->i_ino, inode->i_nlink); 959 set_nlink(inode, 1); 960 } 961 962 drop_nlink(inode); 963 964 /* 965 * we schedule before doing the add_save_link call, save the link 966 * count so we don't race 967 */ 968 savelink = inode->i_nlink; 969 970 retval = 971 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, 972 0); 973 if (retval < 0) { 974 inc_nlink(inode); 975 goto end_unlink; 976 } 977 inode->i_ctime = CURRENT_TIME_SEC; 978 reiserfs_update_sd(&th, inode); 979 980 dir->i_size -= (de.de_entrylen + DEH_SIZE); 981 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; 982 reiserfs_update_sd(&th, dir); 983 984 if (!savelink) 985 /* prevent file from getting lost */ 986 add_save_link(&th, inode, 0 /* not truncate */ ); 987 988 retval = journal_end(&th, dir->i_sb, jbegin_count); 989 reiserfs_check_path(&path); 990 reiserfs_write_unlock(dir->i_sb); 991 return retval; 992 993 end_unlink: 994 pathrelse(&path); 995 err = journal_end(&th, dir->i_sb, jbegin_count); 996 reiserfs_check_path(&path); 997 if (err) 998 retval = err; 999 out_unlink: 1000 reiserfs_write_unlock(dir->i_sb); 1001 return retval; 1002 } 1003 1004 static int reiserfs_symlink(struct inode *parent_dir, 1005 struct dentry *dentry, const char *symname) 1006 { 1007 int retval; 1008 struct inode *inode; 1009 char *name; 1010 int item_len; 1011 struct reiserfs_transaction_handle th; 1012 struct reiserfs_security_handle security; 1013 int mode = S_IFLNK | S_IRWXUGO; 1014 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */ 1015 int jbegin_count = 1016 JOURNAL_PER_BALANCE_CNT * 3 + 1017 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) + 1018 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb)); 1019 1020 dquot_initialize(parent_dir); 1021 1022 if (!(inode = new_inode(parent_dir->i_sb))) { 1023 return -ENOMEM; 1024 } 1025 new_inode_init(inode, parent_dir, mode); 1026 1027 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name, 1028 &security); 1029 if (retval < 0) { 1030 drop_new_inode(inode); 1031 return retval; 1032 } 1033 jbegin_count += retval; 1034 1035 reiserfs_write_lock(parent_dir->i_sb); 1036 item_len = ROUND_UP(strlen(symname)); 1037 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) { 1038 retval = -ENAMETOOLONG; 1039 drop_new_inode(inode); 1040 goto out_failed; 1041 } 1042 1043 name = kmalloc(item_len, GFP_NOFS); 1044 if (!name) { 1045 drop_new_inode(inode); 1046 retval = -ENOMEM; 1047 goto out_failed; 1048 } 1049 memcpy(name, symname, strlen(symname)); 1050 padd_item(name, item_len, strlen(symname)); 1051 1052 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count); 1053 if (retval) { 1054 drop_new_inode(inode); 1055 kfree(name); 1056 goto out_failed; 1057 } 1058 1059 retval = 1060 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname), 1061 dentry, inode, &security); 1062 kfree(name); 1063 if (retval) { /* reiserfs_new_inode iputs for us */ 1064 goto out_failed; 1065 } 1066 1067 reiserfs_update_inode_transaction(inode); 1068 reiserfs_update_inode_transaction(parent_dir); 1069 1070 inode->i_op = &reiserfs_symlink_inode_operations; 1071 inode->i_mapping->a_ops = &reiserfs_address_space_operations; 1072 1073 // must be sure this inode is written with this transaction 1074 // 1075 //reiserfs_update_sd (&th, inode, READ_BLOCKS); 1076 1077 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name, 1078 dentry->d_name.len, inode, 1 /*visible */ ); 1079 if (retval) { 1080 int err; 1081 drop_nlink(inode); 1082 reiserfs_update_sd(&th, inode); 1083 err = journal_end(&th, parent_dir->i_sb, jbegin_count); 1084 if (err) 1085 retval = err; 1086 unlock_new_inode(inode); 1087 iput(inode); 1088 goto out_failed; 1089 } 1090 1091 unlock_new_inode(inode); 1092 d_instantiate(dentry, inode); 1093 retval = journal_end(&th, parent_dir->i_sb, jbegin_count); 1094 out_failed: 1095 reiserfs_write_unlock(parent_dir->i_sb); 1096 return retval; 1097 } 1098 1099 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir, 1100 struct dentry *dentry) 1101 { 1102 int retval; 1103 struct inode *inode = old_dentry->d_inode; 1104 struct reiserfs_transaction_handle th; 1105 /* We need blocks for transaction + update of quotas for the owners of the directory */ 1106 int jbegin_count = 1107 JOURNAL_PER_BALANCE_CNT * 3 + 1108 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb); 1109 1110 dquot_initialize(dir); 1111 1112 reiserfs_write_lock(dir->i_sb); 1113 if (inode->i_nlink >= REISERFS_LINK_MAX) { 1114 //FIXME: sd_nlink is 32 bit for new files 1115 reiserfs_write_unlock(dir->i_sb); 1116 return -EMLINK; 1117 } 1118 1119 /* inc before scheduling so reiserfs_unlink knows we are here */ 1120 inc_nlink(inode); 1121 1122 retval = journal_begin(&th, dir->i_sb, jbegin_count); 1123 if (retval) { 1124 drop_nlink(inode); 1125 reiserfs_write_unlock(dir->i_sb); 1126 return retval; 1127 } 1128 1129 /* create new entry */ 1130 retval = 1131 reiserfs_add_entry(&th, dir, dentry->d_name.name, 1132 dentry->d_name.len, inode, 1 /*visible */ ); 1133 1134 reiserfs_update_inode_transaction(inode); 1135 reiserfs_update_inode_transaction(dir); 1136 1137 if (retval) { 1138 int err; 1139 drop_nlink(inode); 1140 err = journal_end(&th, dir->i_sb, jbegin_count); 1141 reiserfs_write_unlock(dir->i_sb); 1142 return err ? err : retval; 1143 } 1144 1145 inode->i_ctime = CURRENT_TIME_SEC; 1146 reiserfs_update_sd(&th, inode); 1147 1148 ihold(inode); 1149 d_instantiate(dentry, inode); 1150 retval = journal_end(&th, dir->i_sb, jbegin_count); 1151 reiserfs_write_unlock(dir->i_sb); 1152 return retval; 1153 } 1154 1155 /* de contains information pointing to an entry which */ 1156 static int de_still_valid(const char *name, int len, 1157 struct reiserfs_dir_entry *de) 1158 { 1159 struct reiserfs_dir_entry tmp = *de; 1160 1161 // recalculate pointer to name and name length 1162 set_de_name_and_namelen(&tmp); 1163 // FIXME: could check more 1164 if (tmp.de_namelen != len || memcmp(name, de->de_name, len)) 1165 return 0; 1166 return 1; 1167 } 1168 1169 static int entry_points_to_object(const char *name, int len, 1170 struct reiserfs_dir_entry *de, 1171 struct inode *inode) 1172 { 1173 if (!de_still_valid(name, len, de)) 1174 return 0; 1175 1176 if (inode) { 1177 if (!de_visible(de->de_deh + de->de_entry_num)) 1178 reiserfs_panic(inode->i_sb, "vs-7042", 1179 "entry must be visible"); 1180 return (de->de_objectid == inode->i_ino) ? 1 : 0; 1181 } 1182 1183 /* this must be added hidden entry */ 1184 if (de_visible(de->de_deh + de->de_entry_num)) 1185 reiserfs_panic(NULL, "vs-7043", "entry must be visible"); 1186 1187 return 1; 1188 } 1189 1190 /* sets key of objectid the entry has to point to */ 1191 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de, 1192 struct reiserfs_key *key) 1193 { 1194 /* JDM These operations are endian safe - both are le */ 1195 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id; 1196 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid; 1197 } 1198 1199 /* 1200 * process, that is going to call fix_nodes/do_balance must hold only 1201 * one path. If it holds 2 or more, it can get into endless waiting in 1202 * get_empty_nodes or its clones 1203 */ 1204 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry, 1205 struct inode *new_dir, struct dentry *new_dentry) 1206 { 1207 int retval; 1208 INITIALIZE_PATH(old_entry_path); 1209 INITIALIZE_PATH(new_entry_path); 1210 INITIALIZE_PATH(dot_dot_entry_path); 1211 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih; 1212 struct reiserfs_dir_entry old_de, new_de, dot_dot_de; 1213 struct inode *old_inode, *new_dentry_inode; 1214 struct reiserfs_transaction_handle th; 1215 int jbegin_count; 1216 umode_t old_inode_mode; 1217 unsigned long savelink = 1; 1218 struct timespec ctime; 1219 1220 /* three balancings: (1) old name removal, (2) new name insertion 1221 and (3) maybe "save" link insertion 1222 stat data updates: (1) old directory, 1223 (2) new directory and (3) maybe old object stat data (when it is 1224 directory) and (4) maybe stat data of object to which new entry 1225 pointed initially and (5) maybe block containing ".." of 1226 renamed directory 1227 quota updates: two parent directories */ 1228 jbegin_count = 1229 JOURNAL_PER_BALANCE_CNT * 3 + 5 + 1230 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb); 1231 1232 dquot_initialize(old_dir); 1233 dquot_initialize(new_dir); 1234 1235 old_inode = old_dentry->d_inode; 1236 new_dentry_inode = new_dentry->d_inode; 1237 1238 // make sure, that oldname still exists and points to an object we 1239 // are going to rename 1240 old_de.de_gen_number_bit_string = NULL; 1241 reiserfs_write_lock(old_dir->i_sb); 1242 retval = 1243 reiserfs_find_entry(old_dir, old_dentry->d_name.name, 1244 old_dentry->d_name.len, &old_entry_path, 1245 &old_de); 1246 pathrelse(&old_entry_path); 1247 if (retval == IO_ERROR) { 1248 reiserfs_write_unlock(old_dir->i_sb); 1249 return -EIO; 1250 } 1251 1252 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) { 1253 reiserfs_write_unlock(old_dir->i_sb); 1254 return -ENOENT; 1255 } 1256 1257 old_inode_mode = old_inode->i_mode; 1258 if (S_ISDIR(old_inode_mode)) { 1259 // make sure, that directory being renamed has correct ".." 1260 // and that its new parent directory has not too many links 1261 // already 1262 1263 if (new_dentry_inode) { 1264 if (!reiserfs_empty_dir(new_dentry_inode)) { 1265 reiserfs_write_unlock(old_dir->i_sb); 1266 return -ENOTEMPTY; 1267 } 1268 } 1269 1270 /* directory is renamed, its parent directory will be changed, 1271 ** so find ".." entry 1272 */ 1273 dot_dot_de.de_gen_number_bit_string = NULL; 1274 retval = 1275 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path, 1276 &dot_dot_de); 1277 pathrelse(&dot_dot_entry_path); 1278 if (retval != NAME_FOUND) { 1279 reiserfs_write_unlock(old_dir->i_sb); 1280 return -EIO; 1281 } 1282 1283 /* inode number of .. must equal old_dir->i_ino */ 1284 if (dot_dot_de.de_objectid != old_dir->i_ino) { 1285 reiserfs_write_unlock(old_dir->i_sb); 1286 return -EIO; 1287 } 1288 } 1289 1290 retval = journal_begin(&th, old_dir->i_sb, jbegin_count); 1291 if (retval) { 1292 reiserfs_write_unlock(old_dir->i_sb); 1293 return retval; 1294 } 1295 1296 /* add new entry (or find the existing one) */ 1297 retval = 1298 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name, 1299 new_dentry->d_name.len, old_inode, 0); 1300 if (retval == -EEXIST) { 1301 if (!new_dentry_inode) { 1302 reiserfs_panic(old_dir->i_sb, "vs-7050", 1303 "new entry is found, new inode == 0"); 1304 } 1305 } else if (retval) { 1306 int err = journal_end(&th, old_dir->i_sb, jbegin_count); 1307 reiserfs_write_unlock(old_dir->i_sb); 1308 return err ? err : retval; 1309 } 1310 1311 reiserfs_update_inode_transaction(old_dir); 1312 reiserfs_update_inode_transaction(new_dir); 1313 1314 /* this makes it so an fsync on an open fd for the old name will 1315 ** commit the rename operation 1316 */ 1317 reiserfs_update_inode_transaction(old_inode); 1318 1319 if (new_dentry_inode) 1320 reiserfs_update_inode_transaction(new_dentry_inode); 1321 1322 while (1) { 1323 // look for old name using corresponding entry key (found by reiserfs_find_entry) 1324 if ((retval = 1325 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key, 1326 &old_entry_path, 1327 &old_de)) != NAME_FOUND) { 1328 pathrelse(&old_entry_path); 1329 journal_end(&th, old_dir->i_sb, jbegin_count); 1330 reiserfs_write_unlock(old_dir->i_sb); 1331 return -EIO; 1332 } 1333 1334 copy_item_head(&old_entry_ih, get_ih(&old_entry_path)); 1335 1336 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1); 1337 1338 // look for new name by reiserfs_find_entry 1339 new_de.de_gen_number_bit_string = NULL; 1340 retval = 1341 reiserfs_find_entry(new_dir, new_dentry->d_name.name, 1342 new_dentry->d_name.len, &new_entry_path, 1343 &new_de); 1344 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from 1345 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here. 1346 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) { 1347 pathrelse(&new_entry_path); 1348 pathrelse(&old_entry_path); 1349 journal_end(&th, old_dir->i_sb, jbegin_count); 1350 reiserfs_write_unlock(old_dir->i_sb); 1351 return -EIO; 1352 } 1353 1354 copy_item_head(&new_entry_ih, get_ih(&new_entry_path)); 1355 1356 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1); 1357 1358 if (S_ISDIR(old_inode->i_mode)) { 1359 if ((retval = 1360 search_by_entry_key(new_dir->i_sb, 1361 &dot_dot_de.de_entry_key, 1362 &dot_dot_entry_path, 1363 &dot_dot_de)) != NAME_FOUND) { 1364 pathrelse(&dot_dot_entry_path); 1365 pathrelse(&new_entry_path); 1366 pathrelse(&old_entry_path); 1367 journal_end(&th, old_dir->i_sb, jbegin_count); 1368 reiserfs_write_unlock(old_dir->i_sb); 1369 return -EIO; 1370 } 1371 copy_item_head(&dot_dot_ih, 1372 get_ih(&dot_dot_entry_path)); 1373 // node containing ".." gets into transaction 1374 reiserfs_prepare_for_journal(old_inode->i_sb, 1375 dot_dot_de.de_bh, 1); 1376 } 1377 /* we should check seals here, not do 1378 this stuff, yes? Then, having 1379 gathered everything into RAM we 1380 should lock the buffers, yes? -Hans */ 1381 /* probably. our rename needs to hold more 1382 ** than one path at once. The seals would 1383 ** have to be written to deal with multi-path 1384 ** issues -chris 1385 */ 1386 /* sanity checking before doing the rename - avoid races many 1387 ** of the above checks could have scheduled. We have to be 1388 ** sure our items haven't been shifted by another process. 1389 */ 1390 if (item_moved(&new_entry_ih, &new_entry_path) || 1391 !entry_points_to_object(new_dentry->d_name.name, 1392 new_dentry->d_name.len, 1393 &new_de, new_dentry_inode) || 1394 item_moved(&old_entry_ih, &old_entry_path) || 1395 !entry_points_to_object(old_dentry->d_name.name, 1396 old_dentry->d_name.len, 1397 &old_de, old_inode)) { 1398 reiserfs_restore_prepared_buffer(old_inode->i_sb, 1399 new_de.de_bh); 1400 reiserfs_restore_prepared_buffer(old_inode->i_sb, 1401 old_de.de_bh); 1402 if (S_ISDIR(old_inode_mode)) 1403 reiserfs_restore_prepared_buffer(old_inode-> 1404 i_sb, 1405 dot_dot_de. 1406 de_bh); 1407 continue; 1408 } 1409 if (S_ISDIR(old_inode_mode)) { 1410 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) || 1411 !entry_points_to_object("..", 2, &dot_dot_de, 1412 old_dir)) { 1413 reiserfs_restore_prepared_buffer(old_inode-> 1414 i_sb, 1415 old_de.de_bh); 1416 reiserfs_restore_prepared_buffer(old_inode-> 1417 i_sb, 1418 new_de.de_bh); 1419 reiserfs_restore_prepared_buffer(old_inode-> 1420 i_sb, 1421 dot_dot_de. 1422 de_bh); 1423 continue; 1424 } 1425 } 1426 1427 RFALSE(S_ISDIR(old_inode_mode) && 1428 !buffer_journal_prepared(dot_dot_de.de_bh), ""); 1429 1430 break; 1431 } 1432 1433 /* ok, all the changes can be done in one fell swoop when we 1434 have claimed all the buffers needed. */ 1435 1436 mark_de_visible(new_de.de_deh + new_de.de_entry_num); 1437 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode)); 1438 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh); 1439 1440 mark_de_hidden(old_de.de_deh + old_de.de_entry_num); 1441 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh); 1442 ctime = CURRENT_TIME_SEC; 1443 old_dir->i_ctime = old_dir->i_mtime = ctime; 1444 new_dir->i_ctime = new_dir->i_mtime = ctime; 1445 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of 1446 renamed object */ 1447 old_inode->i_ctime = ctime; 1448 1449 if (new_dentry_inode) { 1450 // adjust link number of the victim 1451 if (S_ISDIR(new_dentry_inode->i_mode)) { 1452 clear_nlink(new_dentry_inode); 1453 } else { 1454 drop_nlink(new_dentry_inode); 1455 } 1456 new_dentry_inode->i_ctime = ctime; 1457 savelink = new_dentry_inode->i_nlink; 1458 } 1459 1460 if (S_ISDIR(old_inode_mode)) { 1461 /* adjust ".." of renamed directory */ 1462 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir)); 1463 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh); 1464 1465 if (!new_dentry_inode) 1466 /* there (in new_dir) was no directory, so it got new link 1467 (".." of renamed directory) */ 1468 INC_DIR_INODE_NLINK(new_dir); 1469 1470 /* old directory lost one link - ".. " of renamed directory */ 1471 DEC_DIR_INODE_NLINK(old_dir); 1472 } 1473 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse 1474 pathrelse(&new_entry_path); 1475 pathrelse(&dot_dot_entry_path); 1476 1477 // FIXME: this reiserfs_cut_from_item's return value may screw up 1478 // anybody, but it will panic if will not be able to find the 1479 // entry. This needs one more clean up 1480 if (reiserfs_cut_from_item 1481 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL, 1482 0) < 0) 1483 reiserfs_error(old_dir->i_sb, "vs-7060", 1484 "couldn't not cut old name. Fsck later?"); 1485 1486 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen; 1487 1488 reiserfs_update_sd(&th, old_dir); 1489 reiserfs_update_sd(&th, new_dir); 1490 reiserfs_update_sd(&th, old_inode); 1491 1492 if (new_dentry_inode) { 1493 if (savelink == 0) 1494 add_save_link(&th, new_dentry_inode, 1495 0 /* not truncate */ ); 1496 reiserfs_update_sd(&th, new_dentry_inode); 1497 } 1498 1499 retval = journal_end(&th, old_dir->i_sb, jbegin_count); 1500 reiserfs_write_unlock(old_dir->i_sb); 1501 return retval; 1502 } 1503 1504 /* 1505 * directories can handle most operations... 1506 */ 1507 const struct inode_operations reiserfs_dir_inode_operations = { 1508 //&reiserfs_dir_operations, /* default_file_ops */ 1509 .create = reiserfs_create, 1510 .lookup = reiserfs_lookup, 1511 .link = reiserfs_link, 1512 .unlink = reiserfs_unlink, 1513 .symlink = reiserfs_symlink, 1514 .mkdir = reiserfs_mkdir, 1515 .rmdir = reiserfs_rmdir, 1516 .mknod = reiserfs_mknod, 1517 .rename = reiserfs_rename, 1518 .setattr = reiserfs_setattr, 1519 .setxattr = reiserfs_setxattr, 1520 .getxattr = reiserfs_getxattr, 1521 .listxattr = reiserfs_listxattr, 1522 .removexattr = reiserfs_removexattr, 1523 .permission = reiserfs_permission, 1524 .get_acl = reiserfs_get_acl, 1525 }; 1526 1527 /* 1528 * symlink operations.. same as page_symlink_inode_operations, with xattr 1529 * stuff added 1530 */ 1531 const struct inode_operations reiserfs_symlink_inode_operations = { 1532 .readlink = generic_readlink, 1533 .follow_link = page_follow_link_light, 1534 .put_link = page_put_link, 1535 .setattr = reiserfs_setattr, 1536 .setxattr = reiserfs_setxattr, 1537 .getxattr = reiserfs_getxattr, 1538 .listxattr = reiserfs_listxattr, 1539 .removexattr = reiserfs_removexattr, 1540 .permission = reiserfs_permission, 1541 .get_acl = reiserfs_get_acl, 1542 1543 }; 1544 1545 /* 1546 * special file operations.. just xattr/acl stuff 1547 */ 1548 const struct inode_operations reiserfs_special_inode_operations = { 1549 .setattr = reiserfs_setattr, 1550 .setxattr = reiserfs_setxattr, 1551 .getxattr = reiserfs_getxattr, 1552 .listxattr = reiserfs_listxattr, 1553 .removexattr = reiserfs_removexattr, 1554 .permission = reiserfs_permission, 1555 .get_acl = reiserfs_get_acl, 1556 }; 1557