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