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