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