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