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