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