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