Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext3/namei.c |
| 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/minix/namei.c |
| 12 | * |
| 13 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 14 | * |
| 15 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 16 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 17 | * Directory entry file type support and forward compatibility hooks |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 18 | * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Hash Tree Directory indexing (c) |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 20 | * Daniel Phillips, 2001 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * Hash Tree Directory indexing porting |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 22 | * Christopher Li, 2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * Hash Tree Directory indexing cleanup |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 24 | * Theodore Ts'o, 2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/quotaops.h> |
Al Viro | 4613ad1 | 2012-03-29 22:30:07 -0400 | [diff] [blame] | 28 | #include "ext3.h" |
Ben Dooks | 381be254 | 2005-10-30 15:02:56 -0800 | [diff] [blame] | 29 | #include "namei.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xattr.h" |
| 31 | #include "acl.h" |
| 32 | |
| 33 | /* |
| 34 | * define how far ahead to read directories while searching them. |
| 35 | */ |
| 36 | #define NAMEI_RA_CHUNKS 2 |
| 37 | #define NAMEI_RA_BLOCKS 4 |
| 38 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | static struct buffer_head *ext3_append(handle_t *handle, |
| 41 | struct inode *inode, |
| 42 | u32 *block, int *err) |
| 43 | { |
| 44 | struct buffer_head *bh; |
| 45 | |
| 46 | *block = inode->i_size >> inode->i_sb->s_blocksize_bits; |
| 47 | |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 48 | if ((bh = ext3_dir_bread(handle, inode, *block, 1, err))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | inode->i_size += inode->i_sb->s_blocksize; |
| 50 | EXT3_I(inode)->i_disksize = inode->i_size; |
Akinobu Mita | 33575f8 | 2008-04-28 02:16:08 -0700 | [diff] [blame] | 51 | *err = ext3_journal_get_write_access(handle, bh); |
| 52 | if (*err) { |
| 53 | brelse(bh); |
| 54 | bh = NULL; |
| 55 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | return bh; |
| 58 | } |
| 59 | |
| 60 | #ifndef assert |
| 61 | #define assert(test) J_ASSERT(test) |
| 62 | #endif |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #ifdef DX_DEBUG |
| 65 | #define dxtrace(command) command |
| 66 | #else |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 67 | #define dxtrace(command) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | struct fake_dirent |
| 71 | { |
| 72 | __le32 inode; |
| 73 | __le16 rec_len; |
| 74 | u8 name_len; |
| 75 | u8 file_type; |
| 76 | }; |
| 77 | |
| 78 | struct dx_countlimit |
| 79 | { |
| 80 | __le16 limit; |
| 81 | __le16 count; |
| 82 | }; |
| 83 | |
| 84 | struct dx_entry |
| 85 | { |
| 86 | __le32 hash; |
| 87 | __le32 block; |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * dx_root_info is laid out so that if it should somehow get overlaid by a |
| 92 | * dirent the two low bits of the hash version will be zero. Therefore, the |
| 93 | * hash version mod 4 should never be 0. Sincerely, the paranoia department. |
| 94 | */ |
| 95 | |
| 96 | struct dx_root |
| 97 | { |
| 98 | struct fake_dirent dot; |
| 99 | char dot_name[4]; |
| 100 | struct fake_dirent dotdot; |
| 101 | char dotdot_name[4]; |
| 102 | struct dx_root_info |
| 103 | { |
| 104 | __le32 reserved_zero; |
| 105 | u8 hash_version; |
| 106 | u8 info_length; /* 8 */ |
| 107 | u8 indirect_levels; |
| 108 | u8 unused_flags; |
| 109 | } |
| 110 | info; |
| 111 | struct dx_entry entries[0]; |
| 112 | }; |
| 113 | |
| 114 | struct dx_node |
| 115 | { |
| 116 | struct fake_dirent fake; |
| 117 | struct dx_entry entries[0]; |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | struct dx_frame |
| 122 | { |
| 123 | struct buffer_head *bh; |
| 124 | struct dx_entry *entries; |
| 125 | struct dx_entry *at; |
| 126 | }; |
| 127 | |
| 128 | struct dx_map_entry |
| 129 | { |
| 130 | u32 hash; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 131 | u16 offs; |
| 132 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static inline unsigned dx_get_block (struct dx_entry *entry); |
| 136 | static void dx_set_block (struct dx_entry *entry, unsigned value); |
| 137 | static inline unsigned dx_get_hash (struct dx_entry *entry); |
| 138 | static void dx_set_hash (struct dx_entry *entry, unsigned value); |
| 139 | static unsigned dx_get_count (struct dx_entry *entries); |
| 140 | static unsigned dx_get_limit (struct dx_entry *entries); |
| 141 | static void dx_set_count (struct dx_entry *entries, unsigned value); |
| 142 | static void dx_set_limit (struct dx_entry *entries, unsigned value); |
| 143 | static unsigned dx_root_limit (struct inode *dir, unsigned infosize); |
| 144 | static unsigned dx_node_limit (struct inode *dir); |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 145 | static struct dx_frame *dx_probe(struct qstr *entry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | struct inode *dir, |
| 147 | struct dx_hash_info *hinfo, |
| 148 | struct dx_frame *frame, |
| 149 | int *err); |
| 150 | static void dx_release (struct dx_frame *frames); |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 151 | static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | struct dx_hash_info *hinfo, struct dx_map_entry map[]); |
| 153 | static void dx_sort_map(struct dx_map_entry *map, unsigned count); |
| 154 | static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to, |
| 155 | struct dx_map_entry *offsets, int count); |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 156 | static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block); |
| 158 | static int ext3_htree_next_block(struct inode *dir, __u32 hash, |
| 159 | struct dx_frame *frame, |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 160 | struct dx_frame *frames, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | __u32 *start_hash); |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 162 | static struct buffer_head * ext3_dx_find_entry(struct inode *dir, |
| 163 | struct qstr *entry, struct ext3_dir_entry_2 **res_dir, |
| 164 | int *err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry, |
| 166 | struct inode *inode); |
| 167 | |
| 168 | /* |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 169 | * p is at least 6 bytes before the end of page |
| 170 | */ |
| 171 | static inline struct ext3_dir_entry_2 * |
| 172 | ext3_next_entry(struct ext3_dir_entry_2 *p) |
| 173 | { |
| 174 | return (struct ext3_dir_entry_2 *)((char *)p + |
| 175 | ext3_rec_len_from_disk(p->rec_len)); |
| 176 | } |
| 177 | |
| 178 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * Future: use high four bits of block for coalesce-on-delete flags |
| 180 | * Mask them off for now. |
| 181 | */ |
| 182 | |
| 183 | static inline unsigned dx_get_block (struct dx_entry *entry) |
| 184 | { |
| 185 | return le32_to_cpu(entry->block) & 0x00ffffff; |
| 186 | } |
| 187 | |
| 188 | static inline void dx_set_block (struct dx_entry *entry, unsigned value) |
| 189 | { |
| 190 | entry->block = cpu_to_le32(value); |
| 191 | } |
| 192 | |
| 193 | static inline unsigned dx_get_hash (struct dx_entry *entry) |
| 194 | { |
| 195 | return le32_to_cpu(entry->hash); |
| 196 | } |
| 197 | |
| 198 | static inline void dx_set_hash (struct dx_entry *entry, unsigned value) |
| 199 | { |
| 200 | entry->hash = cpu_to_le32(value); |
| 201 | } |
| 202 | |
| 203 | static inline unsigned dx_get_count (struct dx_entry *entries) |
| 204 | { |
| 205 | return le16_to_cpu(((struct dx_countlimit *) entries)->count); |
| 206 | } |
| 207 | |
| 208 | static inline unsigned dx_get_limit (struct dx_entry *entries) |
| 209 | { |
| 210 | return le16_to_cpu(((struct dx_countlimit *) entries)->limit); |
| 211 | } |
| 212 | |
| 213 | static inline void dx_set_count (struct dx_entry *entries, unsigned value) |
| 214 | { |
| 215 | ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); |
| 216 | } |
| 217 | |
| 218 | static inline void dx_set_limit (struct dx_entry *entries, unsigned value) |
| 219 | { |
| 220 | ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); |
| 221 | } |
| 222 | |
| 223 | static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize) |
| 224 | { |
| 225 | unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) - |
| 226 | EXT3_DIR_REC_LEN(2) - infosize; |
Li Zefan | 8ef2720 | 2008-07-25 01:46:29 -0700 | [diff] [blame] | 227 | return entry_space / sizeof(struct dx_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static inline unsigned dx_node_limit (struct inode *dir) |
| 231 | { |
| 232 | unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0); |
Li Zefan | 8ef2720 | 2008-07-25 01:46:29 -0700 | [diff] [blame] | 233 | return entry_space / sizeof(struct dx_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Debug |
| 238 | */ |
| 239 | #ifdef DX_DEBUG |
| 240 | static void dx_show_index (char * label, struct dx_entry *entries) |
| 241 | { |
| 242 | int i, n = dx_get_count (entries); |
| 243 | printk("%s index ", label); |
| 244 | for (i = 0; i < n; i++) |
| 245 | { |
| 246 | printk("%x->%u ", i? dx_get_hash(entries + i): 0, dx_get_block(entries + i)); |
| 247 | } |
| 248 | printk("\n"); |
| 249 | } |
| 250 | |
| 251 | struct stats |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 252 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | unsigned names; |
| 254 | unsigned space; |
| 255 | unsigned bcount; |
| 256 | }; |
| 257 | |
| 258 | static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de, |
| 259 | int size, int show_names) |
| 260 | { |
| 261 | unsigned names = 0, space = 0; |
| 262 | char *base = (char *) de; |
| 263 | struct dx_hash_info h = *hinfo; |
| 264 | |
| 265 | printk("names: "); |
| 266 | while ((char *) de < base + size) |
| 267 | { |
| 268 | if (de->inode) |
| 269 | { |
| 270 | if (show_names) |
| 271 | { |
| 272 | int len = de->name_len; |
| 273 | char *name = de->name; |
| 274 | while (len--) printk("%c", *name++); |
| 275 | ext3fs_dirhash(de->name, de->name_len, &h); |
| 276 | printk(":%x.%u ", h.hash, |
Bernd Schubert | c878c73 | 2011-07-20 23:16:33 +0200 | [diff] [blame] | 277 | (unsigned) ((char *) de - base)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | space += EXT3_DIR_REC_LEN(de->name_len); |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 280 | names++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 282 | de = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | printk("(%i)\n", names); |
| 285 | return (struct stats) { names, space, 1 }; |
| 286 | } |
| 287 | |
| 288 | struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, |
| 289 | struct dx_entry *entries, int levels) |
| 290 | { |
| 291 | unsigned blocksize = dir->i_sb->s_blocksize; |
| 292 | unsigned count = dx_get_count (entries), names = 0, space = 0, i; |
| 293 | unsigned bcount = 0; |
| 294 | struct buffer_head *bh; |
| 295 | int err; |
| 296 | printk("%i indexed blocks...\n", count); |
| 297 | for (i = 0; i < count; i++, entries++) |
| 298 | { |
| 299 | u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0; |
| 300 | u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash; |
| 301 | struct stats stats; |
| 302 | printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range); |
| 303 | if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue; |
| 304 | stats = levels? |
| 305 | dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): |
| 306 | dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0); |
| 307 | names += stats.names; |
| 308 | space += stats.space; |
| 309 | bcount += stats.bcount; |
| 310 | brelse (bh); |
| 311 | } |
| 312 | if (bcount) |
| 313 | printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ", |
| 314 | names, space/bcount,(space/bcount)*100/blocksize); |
| 315 | return (struct stats) { names, space, bcount}; |
| 316 | } |
| 317 | #endif /* DX_DEBUG */ |
| 318 | |
| 319 | /* |
| 320 | * Probe for a directory leaf block to search. |
| 321 | * |
| 322 | * dx_probe can return ERR_BAD_DX_DIR, which means there was a format |
| 323 | * error in the directory index, and the caller should fall back to |
| 324 | * searching the directory normally. The callers of dx_probe **MUST** |
| 325 | * check for this error code, and make sure it never gets reflected |
| 326 | * back to userspace. |
| 327 | */ |
| 328 | static struct dx_frame * |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 329 | dx_probe(struct qstr *entry, struct inode *dir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err) |
| 331 | { |
| 332 | unsigned count, indirect; |
| 333 | struct dx_entry *at, *entries, *p, *q, *m; |
| 334 | struct dx_root *root; |
| 335 | struct buffer_head *bh; |
| 336 | struct dx_frame *frame = frame_in; |
| 337 | u32 hash; |
| 338 | |
| 339 | frame->bh = NULL; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 340 | if (!(bh = ext3_dir_bread(NULL, dir, 0, 0, err))) { |
| 341 | *err = ERR_BAD_DX_DIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | goto fail; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | root = (struct dx_root *) bh->b_data; |
| 345 | if (root->info.hash_version != DX_HASH_TEA && |
| 346 | root->info.hash_version != DX_HASH_HALF_MD4 && |
| 347 | root->info.hash_version != DX_HASH_LEGACY) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 348 | ext3_warning(dir->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | "Unrecognised inode hash code %d", |
| 350 | root->info.hash_version); |
| 351 | brelse(bh); |
| 352 | *err = ERR_BAD_DX_DIR; |
| 353 | goto fail; |
| 354 | } |
| 355 | hinfo->hash_version = root->info.hash_version; |
Theodore Ts'o | 5e1f8c9 | 2008-10-28 13:21:55 -0400 | [diff] [blame] | 356 | if (hinfo->hash_version <= DX_HASH_TEA) |
| 357 | hinfo->hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed; |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 359 | if (entry) |
| 360 | ext3fs_dirhash(entry->name, entry->len, hinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | hash = hinfo->hash; |
| 362 | |
| 363 | if (root->info.unused_flags & 1) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 364 | ext3_warning(dir->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | "Unimplemented inode hash flags: %#06x", |
| 366 | root->info.unused_flags); |
| 367 | brelse(bh); |
| 368 | *err = ERR_BAD_DX_DIR; |
| 369 | goto fail; |
| 370 | } |
| 371 | |
| 372 | if ((indirect = root->info.indirect_levels) > 1) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 373 | ext3_warning(dir->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | "Unimplemented inode hash depth: %#06x", |
| 375 | root->info.indirect_levels); |
| 376 | brelse(bh); |
| 377 | *err = ERR_BAD_DX_DIR; |
| 378 | goto fail; |
| 379 | } |
| 380 | |
| 381 | entries = (struct dx_entry *) (((char *)&root->info) + |
| 382 | root->info.info_length); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 383 | |
| 384 | if (dx_get_limit(entries) != dx_root_limit(dir, |
| 385 | root->info.info_length)) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 386 | ext3_warning(dir->i_sb, __func__, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 387 | "dx entry: limit != root limit"); |
| 388 | brelse(bh); |
| 389 | *err = ERR_BAD_DX_DIR; |
| 390 | goto fail; |
| 391 | } |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | dxtrace (printk("Look up %x", hash)); |
| 394 | while (1) |
| 395 | { |
| 396 | count = dx_get_count(entries); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 397 | if (!count || count > dx_get_limit(entries)) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 398 | ext3_warning(dir->i_sb, __func__, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 399 | "dx entry: no count or count > limit"); |
| 400 | brelse(bh); |
| 401 | *err = ERR_BAD_DX_DIR; |
| 402 | goto fail2; |
| 403 | } |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | p = entries + 1; |
| 406 | q = entries + count - 1; |
| 407 | while (p <= q) |
| 408 | { |
| 409 | m = p + (q - p)/2; |
| 410 | dxtrace(printk(".")); |
| 411 | if (dx_get_hash(m) > hash) |
| 412 | q = m - 1; |
| 413 | else |
| 414 | p = m + 1; |
| 415 | } |
| 416 | |
| 417 | if (0) // linear search cross check |
| 418 | { |
| 419 | unsigned n = count - 1; |
| 420 | at = entries; |
| 421 | while (n--) |
| 422 | { |
| 423 | dxtrace(printk(",")); |
| 424 | if (dx_get_hash(++at) > hash) |
| 425 | { |
| 426 | at--; |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | assert (at == p - 1); |
| 431 | } |
| 432 | |
| 433 | at = p - 1; |
| 434 | dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at))); |
| 435 | frame->bh = bh; |
| 436 | frame->entries = entries; |
| 437 | frame->at = at; |
| 438 | if (!indirect--) return frame; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 439 | if (!(bh = ext3_dir_bread(NULL, dir, dx_get_block(at), 0, err))) { |
| 440 | *err = ERR_BAD_DX_DIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | goto fail2; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | at = entries = ((struct dx_node *) bh->b_data)->entries; |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 444 | if (dx_get_limit(entries) != dx_node_limit (dir)) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 445 | ext3_warning(dir->i_sb, __func__, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 446 | "dx entry: limit != node limit"); |
| 447 | brelse(bh); |
| 448 | *err = ERR_BAD_DX_DIR; |
| 449 | goto fail2; |
| 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | frame++; |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 452 | frame->bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | fail2: |
| 455 | while (frame >= frame_in) { |
| 456 | brelse(frame->bh); |
| 457 | frame--; |
| 458 | } |
| 459 | fail: |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 460 | if (*err == ERR_BAD_DX_DIR) |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 461 | ext3_warning(dir->i_sb, __func__, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 462 | "Corrupt dir inode %ld, running e2fsck is " |
| 463 | "recommended.", dir->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | static void dx_release (struct dx_frame *frames) |
| 468 | { |
| 469 | if (frames[0].bh == NULL) |
| 470 | return; |
| 471 | |
| 472 | if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels) |
| 473 | brelse(frames[1].bh); |
| 474 | brelse(frames[0].bh); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * This function increments the frame pointer to search the next leaf |
| 479 | * block, and reads in the necessary intervening nodes if the search |
| 480 | * should be necessary. Whether or not the search is necessary is |
| 481 | * controlled by the hash parameter. If the hash value is even, then |
| 482 | * the search is only continued if the next block starts with that |
| 483 | * hash value. This is used if we are searching for a specific file. |
| 484 | * |
| 485 | * If the hash value is HASH_NB_ALWAYS, then always go to the next block. |
| 486 | * |
| 487 | * This function returns 1 if the caller should continue to search, |
| 488 | * or 0 if it should not. If there is an error reading one of the |
| 489 | * index blocks, it will a negative error code. |
| 490 | * |
| 491 | * If start_hash is non-null, it will be filled in with the starting |
| 492 | * hash of the next page. |
| 493 | */ |
| 494 | static int ext3_htree_next_block(struct inode *dir, __u32 hash, |
| 495 | struct dx_frame *frame, |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 496 | struct dx_frame *frames, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | __u32 *start_hash) |
| 498 | { |
| 499 | struct dx_frame *p; |
| 500 | struct buffer_head *bh; |
| 501 | int err, num_frames = 0; |
| 502 | __u32 bhash; |
| 503 | |
| 504 | p = frame; |
| 505 | /* |
| 506 | * Find the next leaf page by incrementing the frame pointer. |
| 507 | * If we run out of entries in the interior node, loop around and |
| 508 | * increment pointer in the parent node. When we break out of |
| 509 | * this loop, num_frames indicates the number of interior |
| 510 | * nodes need to be read. |
| 511 | */ |
| 512 | while (1) { |
| 513 | if (++(p->at) < p->entries + dx_get_count(p->entries)) |
| 514 | break; |
| 515 | if (p == frames) |
| 516 | return 0; |
| 517 | num_frames++; |
| 518 | p--; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * If the hash is 1, then continue only if the next page has a |
| 523 | * continuation hash of any value. This is used for readdir |
| 524 | * handling. Otherwise, check to see if the hash matches the |
| 525 | * desired contiuation hash. If it doesn't, return since |
| 526 | * there's no point to read in the successive index pages. |
| 527 | */ |
| 528 | bhash = dx_get_hash(p->at); |
| 529 | if (start_hash) |
| 530 | *start_hash = bhash; |
| 531 | if ((hash & 1) == 0) { |
| 532 | if ((bhash & ~1) != hash) |
| 533 | return 0; |
| 534 | } |
| 535 | /* |
| 536 | * If the hash is HASH_NB_ALWAYS, we always go to the next |
| 537 | * block so no check is necessary |
| 538 | */ |
| 539 | while (num_frames--) { |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 540 | if (!(bh = ext3_dir_bread(NULL, dir, dx_get_block(p->at), |
| 541 | 0, &err))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return err; /* Failure */ |
| 543 | p++; |
| 544 | brelse (p->bh); |
| 545 | p->bh = bh; |
| 546 | p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; |
| 547 | } |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | * This function fills a red-black tree with information from a |
| 554 | * directory block. It returns the number directory entries loaded |
| 555 | * into the tree. If there is an error it is returned in err. |
| 556 | */ |
| 557 | static int htree_dirblock_to_tree(struct file *dir_file, |
| 558 | struct inode *dir, int block, |
| 559 | struct dx_hash_info *hinfo, |
| 560 | __u32 start_hash, __u32 start_minor_hash) |
| 561 | { |
| 562 | struct buffer_head *bh; |
| 563 | struct ext3_dir_entry_2 *de, *top; |
Carlos Maiolino | aa96601 | 2012-10-01 16:50:54 -0300 | [diff] [blame] | 564 | int err = 0, count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | dxtrace(printk("In htree dirblock_to_tree: block %d\n", block)); |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 567 | |
| 568 | if (!(bh = ext3_dir_bread(NULL, dir, block, 0, &err))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return err; |
| 570 | |
| 571 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
| 572 | top = (struct ext3_dir_entry_2 *) ((char *) de + |
| 573 | dir->i_sb->s_blocksize - |
| 574 | EXT3_DIR_REC_LEN(0)); |
| 575 | for (; de < top; de = ext3_next_entry(de)) { |
Eric Sandeen | 40b8513 | 2006-12-06 20:36:26 -0800 | [diff] [blame] | 576 | if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh, |
| 577 | (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb)) |
| 578 | +((char *)de - bh->b_data))) { |
Al Viro | 64cb927 | 2013-07-01 08:12:38 -0400 | [diff] [blame] | 579 | /* silently ignore the rest of the block */ |
| 580 | break; |
Eric Sandeen | 40b8513 | 2006-12-06 20:36:26 -0800 | [diff] [blame] | 581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | ext3fs_dirhash(de->name, de->name_len, hinfo); |
| 583 | if ((hinfo->hash < start_hash) || |
| 584 | ((hinfo->hash == start_hash) && |
| 585 | (hinfo->minor_hash < start_minor_hash))) |
| 586 | continue; |
| 587 | if (de->inode == 0) |
| 588 | continue; |
| 589 | if ((err = ext3_htree_store_dirent(dir_file, |
| 590 | hinfo->hash, hinfo->minor_hash, de)) != 0) { |
| 591 | brelse(bh); |
| 592 | return err; |
| 593 | } |
| 594 | count++; |
| 595 | } |
| 596 | brelse(bh); |
| 597 | return count; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /* |
| 602 | * This function fills a red-black tree with information from a |
| 603 | * directory. We start scanning the directory in hash order, starting |
| 604 | * at start_hash and start_minor_hash. |
| 605 | * |
| 606 | * This function returns the number of entries inserted into the tree, |
| 607 | * or a negative error code. |
| 608 | */ |
| 609 | int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash, |
| 610 | __u32 start_minor_hash, __u32 *next_hash) |
| 611 | { |
| 612 | struct dx_hash_info hinfo; |
| 613 | struct ext3_dir_entry_2 *de; |
| 614 | struct dx_frame frames[2], *frame; |
| 615 | struct inode *dir; |
| 616 | int block, err; |
| 617 | int count = 0; |
| 618 | int ret; |
| 619 | __u32 hashval; |
| 620 | |
| 621 | dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash, |
| 622 | start_minor_hash)); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 623 | dir = file_inode(dir_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (!(EXT3_I(dir)->i_flags & EXT3_INDEX_FL)) { |
| 625 | hinfo.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version; |
Theodore Ts'o | 5e1f8c9 | 2008-10-28 13:21:55 -0400 | [diff] [blame] | 626 | if (hinfo.hash_version <= DX_HASH_TEA) |
| 627 | hinfo.hash_version += |
| 628 | EXT3_SB(dir->i_sb)->s_hash_unsigned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed; |
| 630 | count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo, |
| 631 | start_hash, start_minor_hash); |
| 632 | *next_hash = ~0; |
| 633 | return count; |
| 634 | } |
| 635 | hinfo.hash = start_hash; |
| 636 | hinfo.minor_hash = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 637 | frame = dx_probe(NULL, file_inode(dir_file), &hinfo, frames, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (!frame) |
| 639 | return err; |
| 640 | |
| 641 | /* Add '.' and '..' from the htree header */ |
| 642 | if (!start_hash && !start_minor_hash) { |
| 643 | de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data; |
| 644 | if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0) |
| 645 | goto errout; |
| 646 | count++; |
| 647 | } |
| 648 | if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { |
| 649 | de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data; |
| 650 | de = ext3_next_entry(de); |
| 651 | if ((err = ext3_htree_store_dirent(dir_file, 2, 0, de)) != 0) |
| 652 | goto errout; |
| 653 | count++; |
| 654 | } |
| 655 | |
| 656 | while (1) { |
| 657 | block = dx_get_block(frame->at); |
| 658 | ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo, |
| 659 | start_hash, start_minor_hash); |
| 660 | if (ret < 0) { |
| 661 | err = ret; |
| 662 | goto errout; |
| 663 | } |
| 664 | count += ret; |
| 665 | hashval = ~0; |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 666 | ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | frame, frames, &hashval); |
| 668 | *next_hash = hashval; |
| 669 | if (ret < 0) { |
| 670 | err = ret; |
| 671 | goto errout; |
| 672 | } |
| 673 | /* |
| 674 | * Stop if: (a) there are no more entries, or |
| 675 | * (b) we have inserted at least one entry and the |
| 676 | * next hash value is not a continuation |
| 677 | */ |
| 678 | if ((ret == 0) || |
| 679 | (count && ((hashval & 1) == 0))) |
| 680 | break; |
| 681 | } |
| 682 | dx_release(frames); |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 683 | dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | count, *next_hash)); |
| 685 | return count; |
| 686 | errout: |
| 687 | dx_release(frames); |
| 688 | return (err); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | /* |
| 693 | * Directory block splitting, compacting |
| 694 | */ |
| 695 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 696 | /* |
| 697 | * Create map of hash values, offsets, and sizes, stored at end of block. |
| 698 | * Returns number of entries mapped. |
| 699 | */ |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 700 | static int dx_make_map(struct ext3_dir_entry_2 *de, unsigned blocksize, |
| 701 | struct dx_hash_info *hinfo, struct dx_map_entry *map_tail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
| 703 | int count = 0; |
| 704 | char *base = (char *) de; |
| 705 | struct dx_hash_info h = *hinfo; |
| 706 | |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 707 | while ((char *) de < base + blocksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
| 709 | if (de->name_len && de->inode) { |
| 710 | ext3fs_dirhash(de->name, de->name_len, &h); |
| 711 | map_tail--; |
| 712 | map_tail->hash = h.hash; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 713 | map_tail->offs = (u16) ((char *) de - base); |
| 714 | map_tail->size = le16_to_cpu(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | count++; |
| 716 | cond_resched(); |
| 717 | } |
| 718 | /* XXX: do we need to check rec_len == 0 case? -Chris */ |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 719 | de = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | return count; |
| 722 | } |
| 723 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 724 | /* Sort map by hash value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | static void dx_sort_map (struct dx_map_entry *map, unsigned count) |
| 726 | { |
| 727 | struct dx_map_entry *p, *q, *top = map + count - 1; |
| 728 | int more; |
| 729 | /* Combsort until bubble sort doesn't suck */ |
| 730 | while (count > 2) |
| 731 | { |
| 732 | count = count*10/13; |
| 733 | if (count - 9 < 2) /* 9, 10 -> 11 */ |
| 734 | count = 11; |
| 735 | for (p = top, q = p - count; q >= map; p--, q--) |
| 736 | if (p->hash < q->hash) |
| 737 | swap(*p, *q); |
| 738 | } |
| 739 | /* Garden variety bubble sort */ |
| 740 | do { |
| 741 | more = 0; |
| 742 | q = top; |
| 743 | while (q-- > map) |
| 744 | { |
| 745 | if (q[1].hash >= q[0].hash) |
| 746 | continue; |
| 747 | swap(*(q+1), *q); |
| 748 | more = 1; |
| 749 | } |
| 750 | } while(more); |
| 751 | } |
| 752 | |
| 753 | static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block) |
| 754 | { |
| 755 | struct dx_entry *entries = frame->entries; |
| 756 | struct dx_entry *old = frame->at, *new = old + 1; |
| 757 | int count = dx_get_count(entries); |
| 758 | |
| 759 | assert(count < dx_get_limit(entries)); |
| 760 | assert(old < entries + count); |
| 761 | memmove(new + 1, new, (char *)(entries + count) - (char *)(new)); |
| 762 | dx_set_hash(new, hash); |
| 763 | dx_set_block(new, block); |
| 764 | dx_set_count(entries, count + 1); |
| 765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | static void ext3_update_dx_flag(struct inode *inode) |
| 768 | { |
| 769 | if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb, |
| 770 | EXT3_FEATURE_COMPAT_DIR_INDEX)) |
| 771 | EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure. |
| 776 | * |
| 777 | * `len <= EXT3_NAME_LEN' is guaranteed by caller. |
| 778 | * `de != NULL' is guaranteed by caller. |
| 779 | */ |
| 780 | static inline int ext3_match (int len, const char * const name, |
| 781 | struct ext3_dir_entry_2 * de) |
| 782 | { |
| 783 | if (len != de->name_len) |
| 784 | return 0; |
| 785 | if (!de->inode) |
| 786 | return 0; |
| 787 | return !memcmp(name, de->name, len); |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 792 | */ |
| 793 | static inline int search_dirblock(struct buffer_head * bh, |
| 794 | struct inode *dir, |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 795 | struct qstr *child, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | unsigned long offset, |
| 797 | struct ext3_dir_entry_2 ** res_dir) |
| 798 | { |
| 799 | struct ext3_dir_entry_2 * de; |
| 800 | char * dlimit; |
| 801 | int de_len; |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 802 | const char *name = child->name; |
| 803 | int namelen = child->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
| 806 | dlimit = bh->b_data + dir->i_sb->s_blocksize; |
| 807 | while ((char *) de < dlimit) { |
| 808 | /* this code is executed quadratically often */ |
| 809 | /* do minimal checking `by hand' */ |
| 810 | |
| 811 | if ((char *) de + namelen <= dlimit && |
| 812 | ext3_match (namelen, name, de)) { |
| 813 | /* found a match - just to be sure, do a full check */ |
| 814 | if (!ext3_check_dir_entry("ext3_find_entry", |
| 815 | dir, de, bh, offset)) |
| 816 | return -1; |
| 817 | *res_dir = de; |
| 818 | return 1; |
| 819 | } |
| 820 | /* prevent looping on a bad block */ |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 821 | de_len = ext3_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (de_len <= 0) |
| 823 | return -1; |
| 824 | offset += de_len; |
| 825 | de = (struct ext3_dir_entry_2 *) ((char *) de + de_len); |
| 826 | } |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | /* |
| 832 | * ext3_find_entry() |
| 833 | * |
| 834 | * finds an entry in the specified directory with the wanted name. It |
| 835 | * returns the cache buffer in which the entry was found, and the entry |
| 836 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 837 | * entry - you'll have to do that yourself if you want to. |
| 838 | * |
| 839 | * The returned buffer_head has ->b_count elevated. The caller is expected |
| 840 | * to brelse() it when appropriate. |
| 841 | */ |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 842 | static struct buffer_head *ext3_find_entry(struct inode *dir, |
| 843 | struct qstr *entry, |
| 844 | struct ext3_dir_entry_2 **res_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
| 846 | struct super_block * sb; |
| 847 | struct buffer_head * bh_use[NAMEI_RA_SIZE]; |
| 848 | struct buffer_head * bh, *ret = NULL; |
| 849 | unsigned long start, block, b; |
Theodore Ts'o | f0cad89 | 2010-10-16 19:36:59 -0400 | [diff] [blame] | 850 | const u8 *name = entry->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | int ra_max = 0; /* Number of bh's in the readahead |
| 852 | buffer, bh_use[] */ |
| 853 | int ra_ptr = 0; /* Current index into readahead |
| 854 | buffer */ |
| 855 | int num = 0; |
| 856 | int nblocks, i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | int namelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
| 859 | *res_dir = NULL; |
| 860 | sb = dir->i_sb; |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 861 | namelen = entry->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (namelen > EXT3_NAME_LEN) |
| 863 | return NULL; |
Theodore Ts'o | f0cad89 | 2010-10-16 19:36:59 -0400 | [diff] [blame] | 864 | if ((namelen <= 2) && (name[0] == '.') && |
| 865 | (name[1] == '.' || name[1] == 0)) { |
| 866 | /* |
| 867 | * "." or ".." will only be in the first block |
| 868 | * NFS may look up ".."; "." should be handled by the VFS |
| 869 | */ |
| 870 | block = start = 0; |
| 871 | nblocks = 1; |
| 872 | goto restart; |
| 873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | if (is_dx(dir)) { |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 875 | bh = ext3_dx_find_entry(dir, entry, res_dir, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | /* |
| 877 | * On success, or if the error was file not found, |
| 878 | * return. Otherwise, fall back to doing a search the |
| 879 | * old fashioned way. |
| 880 | */ |
| 881 | if (bh || (err != ERR_BAD_DX_DIR)) |
| 882 | return bh; |
| 883 | dxtrace(printk("ext3_find_entry: dx failed, falling back\n")); |
| 884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb); |
| 886 | start = EXT3_I(dir)->i_dir_start_lookup; |
| 887 | if (start >= nblocks) |
| 888 | start = 0; |
| 889 | block = start; |
| 890 | restart: |
| 891 | do { |
| 892 | /* |
| 893 | * We deal with the read-ahead logic here. |
| 894 | */ |
| 895 | if (ra_ptr >= ra_max) { |
| 896 | /* Refill the readahead buffer */ |
| 897 | ra_ptr = 0; |
| 898 | b = block; |
| 899 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 900 | /* |
| 901 | * Terminate if we reach the end of the |
| 902 | * directory and must wrap, or if our |
| 903 | * search has finished at this block. |
| 904 | */ |
| 905 | if (b >= nblocks || (num && block == start)) { |
| 906 | bh_use[ra_max] = NULL; |
| 907 | break; |
| 908 | } |
| 909 | num++; |
| 910 | bh = ext3_getblk(NULL, dir, b++, 0, &err); |
| 911 | bh_use[ra_max] = bh; |
Zheng Liu | d03e129 | 2011-12-05 15:55:11 +0800 | [diff] [blame] | 912 | if (bh && !bh_uptodate_or_lock(bh)) { |
| 913 | get_bh(bh); |
| 914 | bh->b_end_io = end_buffer_read_sync; |
| 915 | submit_bh(READ | REQ_META | REQ_PRIO, |
| 916 | bh); |
| 917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 921 | goto next; |
| 922 | wait_on_buffer(bh); |
| 923 | if (!buffer_uptodate(bh)) { |
| 924 | /* read error, skip block & hope for the best */ |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 925 | ext3_error(sb, __func__, "reading directory #%lu " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | "offset %lu", dir->i_ino, block); |
| 927 | brelse(bh); |
| 928 | goto next; |
| 929 | } |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 930 | i = search_dirblock(bh, dir, entry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | block << EXT3_BLOCK_SIZE_BITS(sb), res_dir); |
| 932 | if (i == 1) { |
| 933 | EXT3_I(dir)->i_dir_start_lookup = block; |
| 934 | ret = bh; |
| 935 | goto cleanup_and_exit; |
| 936 | } else { |
| 937 | brelse(bh); |
| 938 | if (i < 0) |
| 939 | goto cleanup_and_exit; |
| 940 | } |
| 941 | next: |
| 942 | if (++block >= nblocks) |
| 943 | block = 0; |
| 944 | } while (block != start); |
| 945 | |
| 946 | /* |
| 947 | * If the directory has grown while we were searching, then |
| 948 | * search the last part of the directory before giving up. |
| 949 | */ |
| 950 | block = nblocks; |
| 951 | nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb); |
| 952 | if (block < nblocks) { |
| 953 | start = 0; |
| 954 | goto restart; |
| 955 | } |
| 956 | |
| 957 | cleanup_and_exit: |
| 958 | /* Clean up the read-ahead blocks */ |
| 959 | for (; ra_ptr < ra_max; ra_ptr++) |
| 960 | brelse (bh_use[ra_ptr]); |
| 961 | return ret; |
| 962 | } |
| 963 | |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 964 | static struct buffer_head * ext3_dx_find_entry(struct inode *dir, |
| 965 | struct qstr *entry, struct ext3_dir_entry_2 **res_dir, |
| 966 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
Theodore Ts'o | f0cad89 | 2010-10-16 19:36:59 -0400 | [diff] [blame] | 968 | struct super_block *sb = dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | struct dx_hash_info hinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | struct dx_frame frames[2], *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | struct buffer_head *bh; |
| 972 | unsigned long block; |
| 973 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Theodore Ts'o | f0cad89 | 2010-10-16 19:36:59 -0400 | [diff] [blame] | 975 | if (!(frame = dx_probe(entry, dir, &hinfo, frames, err))) |
| 976 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | do { |
| 978 | block = dx_get_block(frame->at); |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 979 | if (!(bh = ext3_dir_bread (NULL, dir, block, 0, err))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | goto errout; |
Duane Griffin | 275c0a8 | 2008-07-25 01:46:31 -0700 | [diff] [blame] | 981 | |
Theodore Ts'o | 5026e90 | 2010-10-16 19:37:00 -0400 | [diff] [blame] | 982 | retval = search_dirblock(bh, dir, entry, |
| 983 | block << EXT3_BLOCK_SIZE_BITS(sb), |
| 984 | res_dir); |
| 985 | if (retval == 1) { |
| 986 | dx_release(frames); |
| 987 | return bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Theodore Ts'o | 5026e90 | 2010-10-16 19:37:00 -0400 | [diff] [blame] | 989 | brelse(bh); |
| 990 | if (retval == -1) { |
| 991 | *err = ERR_BAD_DX_DIR; |
| 992 | goto errout; |
| 993 | } |
| 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | /* Check to see if we should continue to search */ |
Theodore Ts'o | f0cad89 | 2010-10-16 19:36:59 -0400 | [diff] [blame] | 996 | retval = ext3_htree_next_block(dir, hinfo.hash, frame, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | frames, NULL); |
| 998 | if (retval < 0) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 999 | ext3_warning(sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | "error reading index page in directory #%lu", |
| 1001 | dir->i_ino); |
| 1002 | *err = retval; |
| 1003 | goto errout; |
| 1004 | } |
| 1005 | } while (retval == 1); |
| 1006 | |
| 1007 | *err = -ENOENT; |
| 1008 | errout: |
Bernd Schubert | c878c73 | 2011-07-20 23:16:33 +0200 | [diff] [blame] | 1009 | dxtrace(printk("%s not found\n", entry->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | dx_release (frames); |
| 1011 | return NULL; |
| 1012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1014 | static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | { |
| 1016 | struct inode * inode; |
| 1017 | struct ext3_dir_entry_2 * de; |
| 1018 | struct buffer_head * bh; |
| 1019 | |
| 1020 | if (dentry->d_name.len > EXT3_NAME_LEN) |
| 1021 | return ERR_PTR(-ENAMETOOLONG); |
| 1022 | |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 1023 | bh = ext3_find_entry(dir, &dentry->d_name, &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | inode = NULL; |
| 1025 | if (bh) { |
| 1026 | unsigned long ino = le32_to_cpu(de->inode); |
| 1027 | brelse (bh); |
Neil Brown | 2ccb48e | 2006-07-30 03:03:01 -0700 | [diff] [blame] | 1028 | if (!ext3_valid_inum(dir->i_sb, ino)) { |
| 1029 | ext3_error(dir->i_sb, "ext3_lookup", |
| 1030 | "bad inode number: %lu", ino); |
David Howells | 473043d | 2008-02-07 00:15:36 -0800 | [diff] [blame] | 1031 | return ERR_PTR(-EIO); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1032 | } |
David Howells | 473043d | 2008-02-07 00:15:36 -0800 | [diff] [blame] | 1033 | inode = ext3_iget(dir->i_sb, ino); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 1034 | if (inode == ERR_PTR(-ESTALE)) { |
| 1035 | ext3_error(dir->i_sb, __func__, |
| 1036 | "deleted inode referenced: %lu", |
| 1037 | ino); |
| 1038 | return ERR_PTR(-EIO); |
Bryan Donlan | de18f3b | 2009-04-02 16:57:15 -0700 | [diff] [blame] | 1039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
Pekka Enberg | ba7fe36 | 2006-01-14 13:21:08 -0800 | [diff] [blame] | 1041 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | struct dentry *ext3_get_parent(struct dentry *child) |
| 1046 | { |
| 1047 | unsigned long ino; |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 1048 | struct qstr dotdot = QSTR_INIT("..", 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | struct ext3_dir_entry_2 * de; |
| 1050 | struct buffer_head *bh; |
| 1051 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1052 | bh = ext3_find_entry(d_inode(child), &dotdot, &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | if (!bh) |
| 1054 | return ERR_PTR(-ENOENT); |
| 1055 | ino = le32_to_cpu(de->inode); |
| 1056 | brelse(bh); |
Neil Brown | 2ccb48e | 2006-07-30 03:03:01 -0700 | [diff] [blame] | 1057 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1058 | if (!ext3_valid_inum(d_inode(child)->i_sb, ino)) { |
| 1059 | ext3_error(d_inode(child)->i_sb, "ext3_get_parent", |
Neil Brown | 2ccb48e | 2006-07-30 03:03:01 -0700 | [diff] [blame] | 1060 | "bad inode number: %lu", ino); |
David Howells | 473043d | 2008-02-07 00:15:36 -0800 | [diff] [blame] | 1061 | return ERR_PTR(-EIO); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1064 | return d_obtain_alias(ext3_iget(d_inode(child)->i_sb, ino)); |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | #define S_SHIFT 12 |
| 1068 | static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 1069 | [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE, |
| 1070 | [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR, |
| 1071 | [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV, |
| 1072 | [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV, |
| 1073 | [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO, |
| 1074 | [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK, |
| 1075 | [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK, |
| 1076 | }; |
| 1077 | |
| 1078 | static inline void ext3_set_de_type(struct super_block *sb, |
| 1079 | struct ext3_dir_entry_2 *de, |
| 1080 | umode_t mode) { |
| 1081 | if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE)) |
| 1082 | de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; |
| 1083 | } |
| 1084 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * Move count entries from end of map between two memory locations. |
| 1087 | * Returns pointer to last entry moved. |
| 1088 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | static struct ext3_dir_entry_2 * |
| 1090 | dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count) |
| 1091 | { |
| 1092 | unsigned rec_len = 0; |
| 1093 | |
| 1094 | while (count--) { |
| 1095 | struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs); |
| 1096 | rec_len = EXT3_DIR_REC_LEN(de->name_len); |
| 1097 | memcpy (to, de, rec_len); |
| 1098 | ((struct ext3_dir_entry_2 *) to)->rec_len = |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1099 | ext3_rec_len_to_disk(rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | de->inode = 0; |
| 1101 | map++; |
| 1102 | to += rec_len; |
| 1103 | } |
| 1104 | return (struct ext3_dir_entry_2 *) (to - rec_len); |
| 1105 | } |
| 1106 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1107 | /* |
| 1108 | * Compact each dir entry in the range to the minimal rec_len. |
| 1109 | * Returns pointer to last entry in range. |
| 1110 | */ |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 1111 | static struct ext3_dir_entry_2 *dx_pack_dirents(char *base, unsigned blocksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 1113 | struct ext3_dir_entry_2 *next, *to, *prev; |
| 1114 | struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *)base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | unsigned rec_len = 0; |
| 1116 | |
| 1117 | prev = to = de; |
Wei Yongjun | 45f9021 | 2009-04-02 16:57:14 -0700 | [diff] [blame] | 1118 | while ((char *)de < base + blocksize) { |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1119 | next = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | if (de->inode && de->name_len) { |
| 1121 | rec_len = EXT3_DIR_REC_LEN(de->name_len); |
| 1122 | if (de > to) |
| 1123 | memmove(to, de, rec_len); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1124 | to->rec_len = ext3_rec_len_to_disk(rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | prev = to; |
| 1126 | to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len); |
| 1127 | } |
| 1128 | de = next; |
| 1129 | } |
| 1130 | return prev; |
| 1131 | } |
| 1132 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1133 | /* |
| 1134 | * Split a full leaf block to make room for a new dir entry. |
| 1135 | * Allocate a new block, and move entries so that they are approx. equally full. |
| 1136 | * Returns pointer to de in block into which the new entry will be inserted. |
| 1137 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, |
| 1139 | struct buffer_head **bh,struct dx_frame *frame, |
| 1140 | struct dx_hash_info *hinfo, int *error) |
| 1141 | { |
| 1142 | unsigned blocksize = dir->i_sb->s_blocksize; |
| 1143 | unsigned count, continued; |
| 1144 | struct buffer_head *bh2; |
| 1145 | u32 newblock; |
| 1146 | u32 hash2; |
| 1147 | struct dx_map_entry *map; |
| 1148 | char *data1 = (*bh)->b_data, *data2; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1149 | unsigned split, move, size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | struct ext3_dir_entry_2 *de = NULL, *de2; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1151 | int err = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1153 | bh2 = ext3_append (handle, dir, &newblock, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | if (!(bh2)) { |
| 1155 | brelse(*bh); |
| 1156 | *bh = NULL; |
| 1157 | goto errout; |
| 1158 | } |
| 1159 | |
| 1160 | BUFFER_TRACE(*bh, "get_write_access"); |
| 1161 | err = ext3_journal_get_write_access(handle, *bh); |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1162 | if (err) |
| 1163 | goto journal_error; |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | BUFFER_TRACE(frame->bh, "get_write_access"); |
| 1166 | err = ext3_journal_get_write_access(handle, frame->bh); |
| 1167 | if (err) |
| 1168 | goto journal_error; |
| 1169 | |
| 1170 | data2 = bh2->b_data; |
| 1171 | |
| 1172 | /* create map in the end of data2 block */ |
| 1173 | map = (struct dx_map_entry *) (data2 + blocksize); |
| 1174 | count = dx_make_map ((struct ext3_dir_entry_2 *) data1, |
| 1175 | blocksize, hinfo, map); |
| 1176 | map -= count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | dx_sort_map (map, count); |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1178 | /* Split the existing block in the middle, size-wise */ |
| 1179 | size = 0; |
| 1180 | move = 0; |
| 1181 | for (i = count-1; i >= 0; i--) { |
| 1182 | /* is more than half of this entry in 2nd half of the block? */ |
| 1183 | if (size + map[i].size/2 > blocksize/2) |
| 1184 | break; |
| 1185 | size += map[i].size; |
| 1186 | move++; |
| 1187 | } |
| 1188 | /* map index at which we will split */ |
| 1189 | split = count - move; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | hash2 = map[split].hash; |
| 1191 | continued = hash2 == map[split - 1].hash; |
| 1192 | dxtrace(printk("Split block %i at %x, %i/%i\n", |
| 1193 | dx_get_block(frame->at), hash2, split, count-split)); |
| 1194 | |
| 1195 | /* Fancy dance to stay within two buffers */ |
| 1196 | de2 = dx_move_dirents(data1, data2, map + split, count - split); |
| 1197 | de = dx_pack_dirents(data1,blocksize); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1198 | de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de); |
| 1199 | de2->rec_len = ext3_rec_len_to_disk(data2 + blocksize - (char *) de2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1)); |
| 1201 | dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1)); |
| 1202 | |
| 1203 | /* Which block gets the new entry? */ |
| 1204 | if (hinfo->hash >= hash2) |
| 1205 | { |
| 1206 | swap(*bh, bh2); |
| 1207 | de = de2; |
| 1208 | } |
| 1209 | dx_insert_block (frame, hash2 + continued, newblock); |
| 1210 | err = ext3_journal_dirty_metadata (handle, bh2); |
| 1211 | if (err) |
| 1212 | goto journal_error; |
| 1213 | err = ext3_journal_dirty_metadata (handle, frame->bh); |
| 1214 | if (err) |
| 1215 | goto journal_error; |
| 1216 | brelse (bh2); |
| 1217 | dxtrace(dx_show_index ("frame", frame->entries)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | return de; |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1219 | |
| 1220 | journal_error: |
| 1221 | brelse(*bh); |
| 1222 | brelse(bh2); |
| 1223 | *bh = NULL; |
| 1224 | ext3_std_error(dir->i_sb, err); |
| 1225 | errout: |
| 1226 | *error = err; |
| 1227 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
| 1230 | |
| 1231 | /* |
| 1232 | * Add a new entry into a directory (leaf) block. If de is non-NULL, |
| 1233 | * it points to a directory entry which is guaranteed to be large |
| 1234 | * enough for new directory entry. If de is NULL, then |
| 1235 | * add_dirent_to_buf will attempt search the directory block for |
| 1236 | * space. It will return -ENOSPC if no space is available, and -EIO |
| 1237 | * and -EEXIST if directory entry already exists. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1238 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | * NOTE! bh is NOT released in the case where ENOSPC is returned. In |
| 1240 | * all other cases bh is released. |
| 1241 | */ |
| 1242 | static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry, |
| 1243 | struct inode *inode, struct ext3_dir_entry_2 *de, |
| 1244 | struct buffer_head * bh) |
| 1245 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1246 | struct inode *dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | const char *name = dentry->d_name.name; |
| 1248 | int namelen = dentry->d_name.len; |
| 1249 | unsigned long offset = 0; |
| 1250 | unsigned short reclen; |
| 1251 | int nlen, rlen, err; |
| 1252 | char *top; |
| 1253 | |
| 1254 | reclen = EXT3_DIR_REC_LEN(namelen); |
| 1255 | if (!de) { |
| 1256 | de = (struct ext3_dir_entry_2 *)bh->b_data; |
| 1257 | top = bh->b_data + dir->i_sb->s_blocksize - reclen; |
| 1258 | while ((char *) de <= top) { |
| 1259 | if (!ext3_check_dir_entry("ext3_add_entry", dir, de, |
| 1260 | bh, offset)) { |
| 1261 | brelse (bh); |
| 1262 | return -EIO; |
| 1263 | } |
| 1264 | if (ext3_match (namelen, name, de)) { |
| 1265 | brelse (bh); |
| 1266 | return -EEXIST; |
| 1267 | } |
| 1268 | nlen = EXT3_DIR_REC_LEN(de->name_len); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1269 | rlen = ext3_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | if ((de->inode? rlen - nlen: rlen) >= reclen) |
| 1271 | break; |
| 1272 | de = (struct ext3_dir_entry_2 *)((char *)de + rlen); |
| 1273 | offset += rlen; |
| 1274 | } |
| 1275 | if ((char *) de > top) |
| 1276 | return -ENOSPC; |
| 1277 | } |
| 1278 | BUFFER_TRACE(bh, "get_write_access"); |
| 1279 | err = ext3_journal_get_write_access(handle, bh); |
| 1280 | if (err) { |
| 1281 | ext3_std_error(dir->i_sb, err); |
| 1282 | brelse(bh); |
| 1283 | return err; |
| 1284 | } |
| 1285 | |
| 1286 | /* By now the buffer is marked for journaling */ |
| 1287 | nlen = EXT3_DIR_REC_LEN(de->name_len); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1288 | rlen = ext3_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (de->inode) { |
| 1290 | struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1291 | de1->rec_len = ext3_rec_len_to_disk(rlen - nlen); |
| 1292 | de->rec_len = ext3_rec_len_to_disk(nlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | de = de1; |
| 1294 | } |
| 1295 | de->file_type = EXT3_FT_UNKNOWN; |
| 1296 | if (inode) { |
| 1297 | de->inode = cpu_to_le32(inode->i_ino); |
| 1298 | ext3_set_de_type(dir->i_sb, de, inode->i_mode); |
| 1299 | } else |
| 1300 | de->inode = 0; |
| 1301 | de->name_len = namelen; |
| 1302 | memcpy (de->name, name, namelen); |
| 1303 | /* |
| 1304 | * XXX shouldn't update any times until successful |
| 1305 | * completion of syscall, but too many callers depend |
| 1306 | * on this. |
| 1307 | * |
| 1308 | * XXX similarly, too many callers depend on |
| 1309 | * ext3_new_inode() setting the times, but error |
| 1310 | * recovery deletes the inode, so the worst that can |
| 1311 | * happen is that the times are slightly out of date |
| 1312 | * and/or different from the directory change time. |
| 1313 | */ |
| 1314 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 1315 | ext3_update_dx_flag(dir); |
| 1316 | dir->i_version++; |
| 1317 | ext3_mark_inode_dirty(handle, dir); |
| 1318 | BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata"); |
| 1319 | err = ext3_journal_dirty_metadata(handle, bh); |
| 1320 | if (err) |
| 1321 | ext3_std_error(dir->i_sb, err); |
| 1322 | brelse(bh); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | /* |
| 1327 | * This converts a one block unindexed directory to a 3 block indexed |
| 1328 | * directory, and adds the dentry to the indexed directory. |
| 1329 | */ |
| 1330 | static int make_indexed_dir(handle_t *handle, struct dentry *dentry, |
| 1331 | struct inode *inode, struct buffer_head *bh) |
| 1332 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1333 | struct inode *dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | const char *name = dentry->d_name.name; |
| 1335 | int namelen = dentry->d_name.len; |
| 1336 | struct buffer_head *bh2; |
| 1337 | struct dx_root *root; |
| 1338 | struct dx_frame frames[2], *frame; |
| 1339 | struct dx_entry *entries; |
| 1340 | struct ext3_dir_entry_2 *de, *de2; |
| 1341 | char *data1, *top; |
| 1342 | unsigned len; |
| 1343 | int retval; |
| 1344 | unsigned blocksize; |
| 1345 | struct dx_hash_info hinfo; |
| 1346 | u32 block; |
| 1347 | struct fake_dirent *fde; |
| 1348 | |
| 1349 | blocksize = dir->i_sb->s_blocksize; |
Theodore Ts'o | a21102b | 2009-01-16 11:13:47 -0500 | [diff] [blame] | 1350 | dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | retval = ext3_journal_get_write_access(handle, bh); |
| 1352 | if (retval) { |
| 1353 | ext3_std_error(dir->i_sb, retval); |
| 1354 | brelse(bh); |
| 1355 | return retval; |
| 1356 | } |
| 1357 | root = (struct dx_root *) bh->b_data; |
| 1358 | |
Theodore Ts'o | a21102b | 2009-01-16 11:13:47 -0500 | [diff] [blame] | 1359 | /* The 0th block becomes the root, move the dirents out */ |
| 1360 | fde = &root->dotdot; |
| 1361 | de = (struct ext3_dir_entry_2 *)((char *)fde + |
| 1362 | ext3_rec_len_from_disk(fde->rec_len)); |
| 1363 | if ((char *) de >= (((char *) root) + blocksize)) { |
| 1364 | ext3_error(dir->i_sb, __func__, |
| 1365 | "invalid rec_len for '..' in inode %lu", |
| 1366 | dir->i_ino); |
| 1367 | brelse(bh); |
| 1368 | return -EIO; |
| 1369 | } |
| 1370 | len = ((char *) root) + blocksize - (char *) de; |
| 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | bh2 = ext3_append (handle, dir, &block, &retval); |
| 1373 | if (!(bh2)) { |
| 1374 | brelse(bh); |
| 1375 | return retval; |
| 1376 | } |
| 1377 | EXT3_I(dir)->i_flags |= EXT3_INDEX_FL; |
| 1378 | data1 = bh2->b_data; |
| 1379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | memcpy (data1, de, len); |
| 1381 | de = (struct ext3_dir_entry_2 *) data1; |
| 1382 | top = data1 + len; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1383 | while ((char *)(de2 = ext3_next_entry(de)) < top) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | de = de2; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1385 | de->rec_len = ext3_rec_len_to_disk(data1 + blocksize - (char *) de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | /* Initialize the root; the dot dirents already exist */ |
| 1387 | de = (struct ext3_dir_entry_2 *) (&root->dotdot); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1388 | de->rec_len = ext3_rec_len_to_disk(blocksize - EXT3_DIR_REC_LEN(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | memset (&root->info, 0, sizeof(root->info)); |
| 1390 | root->info.info_length = sizeof(root->info); |
| 1391 | root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version; |
| 1392 | entries = root->entries; |
| 1393 | dx_set_block (entries, 1); |
| 1394 | dx_set_count (entries, 1); |
| 1395 | dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info))); |
| 1396 | |
| 1397 | /* Initialize as for dx_probe */ |
| 1398 | hinfo.hash_version = root->info.hash_version; |
Theodore Ts'o | 5e1f8c9 | 2008-10-28 13:21:55 -0400 | [diff] [blame] | 1399 | if (hinfo.hash_version <= DX_HASH_TEA) |
| 1400 | hinfo.hash_version += EXT3_SB(dir->i_sb)->s_hash_unsigned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed; |
| 1402 | ext3fs_dirhash(name, namelen, &hinfo); |
| 1403 | frame = frames; |
| 1404 | frame->entries = entries; |
| 1405 | frame->at = entries; |
| 1406 | frame->bh = bh; |
| 1407 | bh = bh2; |
Jan Kara | 86c4f6d8 | 2011-04-27 18:20:44 +0200 | [diff] [blame] | 1408 | /* |
| 1409 | * Mark buffers dirty here so that if do_split() fails we write a |
| 1410 | * consistent set of buffers to disk. |
| 1411 | */ |
| 1412 | ext3_journal_dirty_metadata(handle, frame->bh); |
| 1413 | ext3_journal_dirty_metadata(handle, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | de = do_split(handle,dir, &bh, frame, &hinfo, &retval); |
Jan Kara | 86c4f6d8 | 2011-04-27 18:20:44 +0200 | [diff] [blame] | 1415 | if (!de) { |
| 1416 | ext3_mark_inode_dirty(handle, dir); |
| 1417 | dx_release(frames); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | return retval; |
Jan Kara | 86c4f6d8 | 2011-04-27 18:20:44 +0200 | [diff] [blame] | 1419 | } |
| 1420 | dx_release(frames); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | return add_dirent_to_buf(handle, dentry, inode, de, bh); |
| 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | /* |
| 1426 | * ext3_add_entry() |
| 1427 | * |
| 1428 | * adds a file entry to the specified directory, using the same |
| 1429 | * semantics as ext3_find_entry(). It returns NULL if it failed. |
| 1430 | * |
| 1431 | * NOTE!! The inode part of 'de' is left at 0 - which means you |
| 1432 | * may not sleep between calling this and putting something into |
| 1433 | * the entry, as someone else might have used it while you slept. |
| 1434 | */ |
| 1435 | static int ext3_add_entry (handle_t *handle, struct dentry *dentry, |
| 1436 | struct inode *inode) |
| 1437 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1438 | struct inode *dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | struct buffer_head * bh; |
| 1440 | struct ext3_dir_entry_2 *de; |
| 1441 | struct super_block * sb; |
| 1442 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | int dx_fallback=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | unsigned blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | u32 block, blocks; |
| 1446 | |
| 1447 | sb = dir->i_sb; |
| 1448 | blocksize = sb->s_blocksize; |
| 1449 | if (!dentry->d_name.len) |
| 1450 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | if (is_dx(dir)) { |
| 1452 | retval = ext3_dx_add_entry(handle, dentry, inode); |
| 1453 | if (!retval || (retval != ERR_BAD_DX_DIR)) |
| 1454 | return retval; |
| 1455 | EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL; |
| 1456 | dx_fallback++; |
| 1457 | ext3_mark_inode_dirty(handle, dir); |
| 1458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | blocks = dir->i_size >> sb->s_blocksize_bits; |
Andi Kleen | 0411ba7 | 2010-06-10 13:10:53 +0200 | [diff] [blame] | 1460 | for (block = 0; block < blocks; block++) { |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1461 | if (!(bh = ext3_dir_bread(handle, dir, block, 0, &retval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | return retval; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh); |
| 1465 | if (retval != -ENOSPC) |
| 1466 | return retval; |
| 1467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | if (blocks == 1 && !dx_fallback && |
| 1469 | EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX)) |
| 1470 | return make_indexed_dir(handle, dentry, inode, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | brelse(bh); |
| 1472 | } |
| 1473 | bh = ext3_append(handle, dir, &block, &retval); |
| 1474 | if (!bh) |
| 1475 | return retval; |
| 1476 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
| 1477 | de->inode = 0; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1478 | de->rec_len = ext3_rec_len_to_disk(blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | return add_dirent_to_buf(handle, dentry, inode, de, bh); |
| 1480 | } |
| 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | /* |
| 1483 | * Returns 0 for success, or a negative error value |
| 1484 | */ |
| 1485 | static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry, |
| 1486 | struct inode *inode) |
| 1487 | { |
| 1488 | struct dx_frame frames[2], *frame; |
| 1489 | struct dx_entry *entries, *at; |
| 1490 | struct dx_hash_info hinfo; |
| 1491 | struct buffer_head * bh; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1492 | struct inode *dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | struct super_block * sb = dir->i_sb; |
| 1494 | struct ext3_dir_entry_2 *de; |
| 1495 | int err; |
| 1496 | |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 1497 | frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | if (!frame) |
| 1499 | return err; |
| 1500 | entries = frame->entries; |
| 1501 | at = frame->at; |
| 1502 | |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1503 | if (!(bh = ext3_dir_bread(handle, dir, dx_get_block(frame->at), 0, &err))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | goto cleanup; |
| 1505 | |
| 1506 | BUFFER_TRACE(bh, "get_write_access"); |
| 1507 | err = ext3_journal_get_write_access(handle, bh); |
| 1508 | if (err) |
| 1509 | goto journal_error; |
| 1510 | |
| 1511 | err = add_dirent_to_buf(handle, dentry, inode, NULL, bh); |
| 1512 | if (err != -ENOSPC) { |
| 1513 | bh = NULL; |
| 1514 | goto cleanup; |
| 1515 | } |
| 1516 | |
| 1517 | /* Block full, should compress but for now just split */ |
| 1518 | dxtrace(printk("using %u of %u node entries\n", |
| 1519 | dx_get_count(entries), dx_get_limit(entries))); |
| 1520 | /* Need to split index? */ |
| 1521 | if (dx_get_count(entries) == dx_get_limit(entries)) { |
| 1522 | u32 newblock; |
| 1523 | unsigned icount = dx_get_count(entries); |
| 1524 | int levels = frame - frames; |
| 1525 | struct dx_entry *entries2; |
| 1526 | struct dx_node *node2; |
| 1527 | struct buffer_head *bh2; |
| 1528 | |
| 1529 | if (levels && (dx_get_count(frames->entries) == |
| 1530 | dx_get_limit(frames->entries))) { |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 1531 | ext3_warning(sb, __func__, |
Glauber de Oliveira Costa | 9f40668 | 2006-01-08 01:03:22 -0800 | [diff] [blame] | 1532 | "Directory index full!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | err = -ENOSPC; |
| 1534 | goto cleanup; |
| 1535 | } |
| 1536 | bh2 = ext3_append (handle, dir, &newblock, &err); |
| 1537 | if (!(bh2)) |
| 1538 | goto cleanup; |
| 1539 | node2 = (struct dx_node *)(bh2->b_data); |
| 1540 | entries2 = node2->entries; |
Eric Sandeen | d743314 | 2011-03-04 16:04:08 -0600 | [diff] [blame] | 1541 | memset(&node2->fake, 0, sizeof(struct fake_dirent)); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1542 | node2->fake.rec_len = ext3_rec_len_to_disk(sb->s_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | BUFFER_TRACE(frame->bh, "get_write_access"); |
| 1544 | err = ext3_journal_get_write_access(handle, frame->bh); |
| 1545 | if (err) |
| 1546 | goto journal_error; |
| 1547 | if (levels) { |
| 1548 | unsigned icount1 = icount/2, icount2 = icount - icount1; |
| 1549 | unsigned hash2 = dx_get_hash(entries + icount1); |
| 1550 | dxtrace(printk("Split index %i/%i\n", icount1, icount2)); |
| 1551 | |
| 1552 | BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ |
| 1553 | err = ext3_journal_get_write_access(handle, |
| 1554 | frames[0].bh); |
| 1555 | if (err) |
| 1556 | goto journal_error; |
| 1557 | |
| 1558 | memcpy ((char *) entries2, (char *) (entries + icount1), |
| 1559 | icount2 * sizeof(struct dx_entry)); |
| 1560 | dx_set_count (entries, icount1); |
| 1561 | dx_set_count (entries2, icount2); |
| 1562 | dx_set_limit (entries2, dx_node_limit(dir)); |
| 1563 | |
| 1564 | /* Which index block gets the new entry? */ |
| 1565 | if (at - entries >= icount1) { |
| 1566 | frame->at = at = at - entries - icount1 + entries2; |
| 1567 | frame->entries = entries = entries2; |
| 1568 | swap(frame->bh, bh2); |
| 1569 | } |
| 1570 | dx_insert_block (frames + 0, hash2, newblock); |
| 1571 | dxtrace(dx_show_index ("node", frames[1].entries)); |
| 1572 | dxtrace(dx_show_index ("node", |
| 1573 | ((struct dx_node *) bh2->b_data)->entries)); |
| 1574 | err = ext3_journal_dirty_metadata(handle, bh2); |
| 1575 | if (err) |
| 1576 | goto journal_error; |
| 1577 | brelse (bh2); |
| 1578 | } else { |
| 1579 | dxtrace(printk("Creating second level index...\n")); |
| 1580 | memcpy((char *) entries2, (char *) entries, |
| 1581 | icount * sizeof(struct dx_entry)); |
| 1582 | dx_set_limit(entries2, dx_node_limit(dir)); |
| 1583 | |
| 1584 | /* Set up root */ |
| 1585 | dx_set_count(entries, 1); |
| 1586 | dx_set_block(entries + 0, newblock); |
| 1587 | ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1; |
| 1588 | |
| 1589 | /* Add new access path frame */ |
| 1590 | frame = frames + 1; |
| 1591 | frame->at = at = at - entries + entries2; |
| 1592 | frame->entries = entries = entries2; |
| 1593 | frame->bh = bh2; |
| 1594 | err = ext3_journal_get_write_access(handle, |
| 1595 | frame->bh); |
| 1596 | if (err) |
| 1597 | goto journal_error; |
| 1598 | } |
Namhyung Kim | 156e743 | 2010-11-25 01:53:13 +0900 | [diff] [blame] | 1599 | err = ext3_journal_dirty_metadata(handle, frames[0].bh); |
| 1600 | if (err) |
| 1601 | goto journal_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | } |
| 1603 | de = do_split(handle, dir, &bh, frame, &hinfo, &err); |
| 1604 | if (!de) |
| 1605 | goto cleanup; |
| 1606 | err = add_dirent_to_buf(handle, dentry, inode, de, bh); |
| 1607 | bh = NULL; |
| 1608 | goto cleanup; |
| 1609 | |
| 1610 | journal_error: |
| 1611 | ext3_std_error(dir->i_sb, err); |
| 1612 | cleanup: |
| 1613 | if (bh) |
| 1614 | brelse(bh); |
| 1615 | dx_release(frames); |
| 1616 | return err; |
| 1617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | /* |
| 1620 | * ext3_delete_entry deletes a directory entry by merging it with the |
| 1621 | * previous entry |
| 1622 | */ |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1623 | static int ext3_delete_entry (handle_t *handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | struct inode * dir, |
| 1625 | struct ext3_dir_entry_2 * de_del, |
| 1626 | struct buffer_head * bh) |
| 1627 | { |
| 1628 | struct ext3_dir_entry_2 * de, * pde; |
| 1629 | int i; |
| 1630 | |
| 1631 | i = 0; |
| 1632 | pde = NULL; |
| 1633 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
| 1634 | while (i < bh->b_size) { |
| 1635 | if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i)) |
| 1636 | return -EIO; |
| 1637 | if (de == de_del) { |
Namhyung Kim | fbcae8e | 2010-11-19 16:28:35 +0900 | [diff] [blame] | 1638 | int err; |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | BUFFER_TRACE(bh, "get_write_access"); |
Namhyung Kim | fbcae8e | 2010-11-19 16:28:35 +0900 | [diff] [blame] | 1641 | err = ext3_journal_get_write_access(handle, bh); |
| 1642 | if (err) |
| 1643 | goto journal_error; |
| 1644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | if (pde) |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1646 | pde->rec_len = ext3_rec_len_to_disk( |
| 1647 | ext3_rec_len_from_disk(pde->rec_len) + |
| 1648 | ext3_rec_len_from_disk(de->rec_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | else |
| 1650 | de->inode = 0; |
| 1651 | dir->i_version++; |
| 1652 | BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata"); |
Namhyung Kim | fbcae8e | 2010-11-19 16:28:35 +0900 | [diff] [blame] | 1653 | err = ext3_journal_dirty_metadata(handle, bh); |
| 1654 | if (err) { |
| 1655 | journal_error: |
| 1656 | ext3_std_error(dir->i_sb, err); |
| 1657 | return err; |
| 1658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | return 0; |
| 1660 | } |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1661 | i += ext3_rec_len_from_disk(de->rec_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | pde = de; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1663 | de = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } |
| 1665 | return -ENOENT; |
| 1666 | } |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | static int ext3_add_nondir(handle_t *handle, |
| 1669 | struct dentry *dentry, struct inode *inode) |
| 1670 | { |
| 1671 | int err = ext3_add_entry(handle, dentry, inode); |
| 1672 | if (!err) { |
| 1673 | ext3_mark_inode_dirty(handle, inode); |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 1674 | unlock_new_inode(inode); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 1675 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
Eric Sandeen | 731b9a5 | 2007-02-10 01:46:16 -0800 | [diff] [blame] | 1678 | drop_nlink(inode); |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 1679 | unlock_new_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | iput(inode); |
| 1681 | return err; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * By the time this is called, we already have created |
| 1686 | * the directory cache entry for the new file, but it |
| 1687 | * is so far negative - it has no inode. |
| 1688 | * |
| 1689 | * If the create succeeds, we fill in the inode information |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1690 | * with d_instantiate(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 1692 | static int ext3_create (struct inode * dir, struct dentry * dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 1693 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | { |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1695 | handle_t *handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | struct inode * inode; |
| 1697 | int err, retries = 0; |
| 1698 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 1699 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 1700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | retry: |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 1702 | handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | c459001 | 2009-12-09 03:05:30 +0300 | [diff] [blame] | 1704 | EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | if (IS_ERR(handle)) |
| 1706 | return PTR_ERR(handle); |
| 1707 | |
| 1708 | if (IS_DIRSYNC(dir)) |
| 1709 | handle->h_sync = 1; |
| 1710 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1711 | inode = ext3_new_inode (handle, dir, &dentry->d_name, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | err = PTR_ERR(inode); |
| 1713 | if (!IS_ERR(inode)) { |
| 1714 | inode->i_op = &ext3_file_inode_operations; |
| 1715 | inode->i_fop = &ext3_file_operations; |
| 1716 | ext3_set_aops(inode); |
| 1717 | err = ext3_add_nondir(handle, dentry, inode); |
| 1718 | } |
| 1719 | ext3_journal_stop(handle); |
| 1720 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 1721 | goto retry; |
| 1722 | return err; |
| 1723 | } |
| 1724 | |
| 1725 | static int ext3_mknod (struct inode * dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 1726 | umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
| 1728 | handle_t *handle; |
| 1729 | struct inode *inode; |
| 1730 | int err, retries = 0; |
| 1731 | |
| 1732 | if (!new_valid_dev(rdev)) |
| 1733 | return -EINVAL; |
| 1734 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 1735 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 1736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | retry: |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 1738 | handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 1739 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | c459001 | 2009-12-09 03:05:30 +0300 | [diff] [blame] | 1740 | EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | if (IS_ERR(handle)) |
| 1742 | return PTR_ERR(handle); |
| 1743 | |
| 1744 | if (IS_DIRSYNC(dir)) |
| 1745 | handle->h_sync = 1; |
| 1746 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1747 | inode = ext3_new_inode (handle, dir, &dentry->d_name, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | err = PTR_ERR(inode); |
| 1749 | if (!IS_ERR(inode)) { |
| 1750 | init_special_inode(inode, inode->i_mode, rdev); |
| 1751 | #ifdef CONFIG_EXT3_FS_XATTR |
| 1752 | inode->i_op = &ext3_special_inode_operations; |
| 1753 | #endif |
| 1754 | err = ext3_add_nondir(handle, dentry, inode); |
| 1755 | } |
| 1756 | ext3_journal_stop(handle); |
| 1757 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 1758 | goto retry; |
| 1759 | return err; |
| 1760 | } |
| 1761 | |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1762 | static int ext3_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 1763 | { |
| 1764 | handle_t *handle; |
| 1765 | struct inode *inode; |
| 1766 | int err, retries = 0; |
| 1767 | |
| 1768 | dquot_initialize(dir); |
| 1769 | |
| 1770 | retry: |
| 1771 | handle = ext3_journal_start(dir, EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + |
| 1772 | 4 + EXT3_XATTR_TRANS_BLOCKS); |
| 1773 | |
| 1774 | if (IS_ERR(handle)) |
| 1775 | return PTR_ERR(handle); |
| 1776 | |
| 1777 | inode = ext3_new_inode (handle, dir, NULL, mode); |
| 1778 | err = PTR_ERR(inode); |
| 1779 | if (!IS_ERR(inode)) { |
| 1780 | inode->i_op = &ext3_file_inode_operations; |
| 1781 | inode->i_fop = &ext3_file_operations; |
| 1782 | ext3_set_aops(inode); |
Zheng Liu | dda5690 | 2013-07-20 22:03:20 -0400 | [diff] [blame] | 1783 | d_tmpfile(dentry, inode); |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1784 | err = ext3_orphan_add(handle, inode); |
| 1785 | if (err) |
Miklos Szeredi | 43ae9e3 | 2013-10-10 16:48:19 +0200 | [diff] [blame] | 1786 | goto err_unlock_inode; |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1787 | mark_inode_dirty(inode); |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1788 | unlock_new_inode(inode); |
| 1789 | } |
| 1790 | ext3_journal_stop(handle); |
| 1791 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 1792 | goto retry; |
| 1793 | return err; |
Miklos Szeredi | 43ae9e3 | 2013-10-10 16:48:19 +0200 | [diff] [blame] | 1794 | err_unlock_inode: |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1795 | ext3_journal_stop(handle); |
| 1796 | unlock_new_inode(inode); |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 1797 | return err; |
| 1798 | } |
| 1799 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 1800 | static int ext3_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
| 1802 | handle_t *handle; |
| 1803 | struct inode * inode; |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1804 | struct buffer_head * dir_block = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | struct ext3_dir_entry_2 * de; |
| 1806 | int err, retries = 0; |
| 1807 | |
| 1808 | if (dir->i_nlink >= EXT3_LINK_MAX) |
| 1809 | return -EMLINK; |
| 1810 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 1811 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | retry: |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 1814 | handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | c459001 | 2009-12-09 03:05:30 +0300 | [diff] [blame] | 1816 | EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | if (IS_ERR(handle)) |
| 1818 | return PTR_ERR(handle); |
| 1819 | |
| 1820 | if (IS_DIRSYNC(dir)) |
| 1821 | handle->h_sync = 1; |
| 1822 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1823 | inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFDIR | mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | err = PTR_ERR(inode); |
| 1825 | if (IS_ERR(inode)) |
| 1826 | goto out_stop; |
| 1827 | |
| 1828 | inode->i_op = &ext3_dir_inode_operations; |
| 1829 | inode->i_fop = &ext3_dir_operations; |
| 1830 | inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1831 | if (!(dir_block = ext3_dir_bread(handle, inode, 0, 1, &err))) |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1832 | goto out_clear_inode; |
| 1833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | BUFFER_TRACE(dir_block, "get_write_access"); |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1835 | err = ext3_journal_get_write_access(handle, dir_block); |
| 1836 | if (err) |
| 1837 | goto out_clear_inode; |
| 1838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | de = (struct ext3_dir_entry_2 *) dir_block->b_data; |
| 1840 | de->inode = cpu_to_le32(inode->i_ino); |
| 1841 | de->name_len = 1; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1842 | de->rec_len = ext3_rec_len_to_disk(EXT3_DIR_REC_LEN(de->name_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | strcpy (de->name, "."); |
| 1844 | ext3_set_de_type(dir->i_sb, de, S_IFDIR); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1845 | de = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | de->inode = cpu_to_le32(dir->i_ino); |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1847 | de->rec_len = ext3_rec_len_to_disk(inode->i_sb->s_blocksize - |
| 1848 | EXT3_DIR_REC_LEN(1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | de->name_len = 2; |
| 1850 | strcpy (de->name, ".."); |
| 1851 | ext3_set_de_type(dir->i_sb, de, S_IFDIR); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 1852 | set_nlink(inode, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata"); |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1854 | err = ext3_journal_dirty_metadata(handle, dir_block); |
| 1855 | if (err) |
| 1856 | goto out_clear_inode; |
| 1857 | |
| 1858 | err = ext3_mark_inode_dirty(handle, inode); |
| 1859 | if (!err) |
| 1860 | err = ext3_add_entry (handle, dentry, inode); |
| 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | if (err) { |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1863 | out_clear_inode: |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 1864 | clear_nlink(inode); |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 1865 | unlock_new_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | ext3_mark_inode_dirty(handle, inode); |
| 1867 | iput (inode); |
| 1868 | goto out_stop; |
| 1869 | } |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1870 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | ext3_update_dx_flag(dir); |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1872 | err = ext3_mark_inode_dirty(handle, dir); |
| 1873 | if (err) |
| 1874 | goto out_clear_inode; |
| 1875 | |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 1876 | unlock_new_inode(inode); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 1877 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | out_stop: |
Namhyung Kim | 2b543ed | 2010-11-16 20:18:12 +0900 | [diff] [blame] | 1879 | brelse(dir_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | ext3_journal_stop(handle); |
| 1881 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 1882 | goto retry; |
| 1883 | return err; |
| 1884 | } |
| 1885 | |
| 1886 | /* |
| 1887 | * routine to check that the specified directory is empty (for rmdir) |
| 1888 | */ |
| 1889 | static int empty_dir (struct inode * inode) |
| 1890 | { |
| 1891 | unsigned long offset; |
| 1892 | struct buffer_head * bh; |
| 1893 | struct ext3_dir_entry_2 * de, * de1; |
| 1894 | struct super_block * sb; |
| 1895 | int err = 0; |
| 1896 | |
| 1897 | sb = inode->i_sb; |
| 1898 | if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) || |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1899 | !(bh = ext3_dir_bread(NULL, inode, 0, 0, &err))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | if (err) |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 1901 | ext3_error(inode->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | "error %d reading directory #%lu offset 0", |
| 1903 | err, inode->i_ino); |
| 1904 | else |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 1905 | ext3_warning(inode->i_sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | "bad directory (dir #%lu) - no data block", |
| 1907 | inode->i_ino); |
| 1908 | return 1; |
| 1909 | } |
| 1910 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1911 | de1 = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | if (le32_to_cpu(de->inode) != inode->i_ino || |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1913 | !le32_to_cpu(de1->inode) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | strcmp (".", de->name) || |
| 1915 | strcmp ("..", de1->name)) { |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 1916 | ext3_warning (inode->i_sb, "empty_dir", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | "bad directory (dir #%lu) - no `.' or `..'", |
| 1918 | inode->i_ino); |
| 1919 | brelse (bh); |
| 1920 | return 1; |
| 1921 | } |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1922 | offset = ext3_rec_len_from_disk(de->rec_len) + |
| 1923 | ext3_rec_len_from_disk(de1->rec_len); |
| 1924 | de = ext3_next_entry(de1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | while (offset < inode->i_size ) { |
| 1926 | if (!bh || |
| 1927 | (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) { |
| 1928 | err = 0; |
| 1929 | brelse (bh); |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 1930 | if (!(bh = ext3_dir_bread (NULL, inode, |
| 1931 | offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | if (err) |
Harvey Harrison | e05b6b5 | 2008-04-28 02:16:15 -0700 | [diff] [blame] | 1933 | ext3_error(sb, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | "error %d reading directory" |
| 1935 | " #%lu offset %lu", |
| 1936 | err, inode->i_ino, offset); |
| 1937 | offset += sb->s_blocksize; |
| 1938 | continue; |
| 1939 | } |
| 1940 | de = (struct ext3_dir_entry_2 *) bh->b_data; |
| 1941 | } |
| 1942 | if (!ext3_check_dir_entry("empty_dir", inode, de, bh, offset)) { |
| 1943 | de = (struct ext3_dir_entry_2 *)(bh->b_data + |
| 1944 | sb->s_blocksize); |
| 1945 | offset = (offset | (sb->s_blocksize - 1)) + 1; |
| 1946 | continue; |
| 1947 | } |
| 1948 | if (le32_to_cpu(de->inode)) { |
| 1949 | brelse (bh); |
| 1950 | return 0; |
| 1951 | } |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 1952 | offset += ext3_rec_len_from_disk(de->rec_len); |
| 1953 | de = ext3_next_entry(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | } |
| 1955 | brelse (bh); |
| 1956 | return 1; |
| 1957 | } |
| 1958 | |
| 1959 | /* ext3_orphan_add() links an unlinked or truncated inode into a list of |
| 1960 | * such inodes, starting at the superblock, in case we crash before the |
| 1961 | * file is closed/deleted, or in case the inode truncate spans multiple |
| 1962 | * transactions and the last transaction is not recovered after a crash. |
| 1963 | * |
| 1964 | * At filesystem recovery time, we walk this list deleting unlinked |
| 1965 | * inodes and truncating linked inodes in ext3_orphan_cleanup(). |
| 1966 | */ |
| 1967 | int ext3_orphan_add(handle_t *handle, struct inode *inode) |
| 1968 | { |
| 1969 | struct super_block *sb = inode->i_sb; |
| 1970 | struct ext3_iloc iloc; |
| 1971 | int err = 0, rc; |
| 1972 | |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 1973 | mutex_lock(&EXT3_SB(sb)->s_orphan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | if (!list_empty(&EXT3_I(inode)->i_orphan)) |
| 1975 | goto out_unlock; |
| 1976 | |
| 1977 | /* Orphan handling is only valid for files with data blocks |
| 1978 | * being truncated, or files being unlinked. */ |
| 1979 | |
| 1980 | /* @@@ FIXME: Observation from aviro: |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1981 | * I think I can trigger J_ASSERT in ext3_orphan_add(). We block |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 1982 | * here (on s_orphan_lock), so race with ext3_link() which might bump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | * ->i_nlink. For, say it, character device. Not a regular file, |
| 1984 | * not a directory, not a symlink and ->i_nlink > 0. |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 1985 | * |
| 1986 | * tytso, 4/25/2009: I'm not sure how that could happen; |
| 1987 | * shouldn't the fs core protect us from these sort of |
| 1988 | * unlink()/link() races? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | */ |
| 1990 | J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 1991 | S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); |
| 1992 | |
| 1993 | BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "get_write_access"); |
| 1994 | err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh); |
| 1995 | if (err) |
| 1996 | goto out_unlock; |
| 1997 | |
| 1998 | err = ext3_reserve_inode_write(handle, inode, &iloc); |
| 1999 | if (err) |
| 2000 | goto out_unlock; |
| 2001 | |
| 2002 | /* Insert this inode at the head of the on-disk orphan list... */ |
| 2003 | NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan); |
| 2004 | EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); |
| 2005 | err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh); |
| 2006 | rc = ext3_mark_iloc_dirty(handle, inode, &iloc); |
| 2007 | if (!err) |
| 2008 | err = rc; |
| 2009 | |
| 2010 | /* Only add to the head of the in-memory list if all the |
| 2011 | * previous operations succeeded. If the orphan_add is going to |
| 2012 | * fail (possibly taking the journal offline), we can't risk |
| 2013 | * leaving the inode on the orphan list: stray orphan-list |
| 2014 | * entries can cause panics at unmount time. |
| 2015 | * |
| 2016 | * This is safe: on error we're going to ignore the orphan list |
| 2017 | * anyway on the next recovery. */ |
| 2018 | if (!err) |
| 2019 | list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan); |
| 2020 | |
Eric Sandeen | eee194e | 2006-09-27 01:49:30 -0700 | [diff] [blame] | 2021 | jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); |
| 2022 | jbd_debug(4, "orphan inode %lu will point to %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | inode->i_ino, NEXT_ORPHAN(inode)); |
| 2024 | out_unlock: |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 2025 | mutex_unlock(&EXT3_SB(sb)->s_orphan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | ext3_std_error(inode->i_sb, err); |
| 2027 | return err; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | * ext3_orphan_del() removes an unlinked or truncated inode from the list |
| 2032 | * of such inodes stored on disk, because it is finally being cleaned up. |
| 2033 | */ |
| 2034 | int ext3_orphan_del(handle_t *handle, struct inode *inode) |
| 2035 | { |
| 2036 | struct list_head *prev; |
| 2037 | struct ext3_inode_info *ei = EXT3_I(inode); |
| 2038 | struct ext3_sb_info *sbi; |
| 2039 | unsigned long ino_next; |
| 2040 | struct ext3_iloc iloc; |
| 2041 | int err = 0; |
| 2042 | |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 2043 | mutex_lock(&EXT3_SB(inode->i_sb)->s_orphan_lock); |
| 2044 | if (list_empty(&ei->i_orphan)) |
| 2045 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
| 2047 | ino_next = NEXT_ORPHAN(inode); |
| 2048 | prev = ei->i_orphan.prev; |
| 2049 | sbi = EXT3_SB(inode->i_sb); |
| 2050 | |
| 2051 | jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); |
| 2052 | |
| 2053 | list_del_init(&ei->i_orphan); |
| 2054 | |
| 2055 | /* If we're on an error path, we may not have a valid |
| 2056 | * transaction handle with which to update the orphan list on |
| 2057 | * disk, but we still need to remove the inode from the linked |
| 2058 | * list in memory. */ |
| 2059 | if (!handle) |
| 2060 | goto out; |
| 2061 | |
| 2062 | err = ext3_reserve_inode_write(handle, inode, &iloc); |
| 2063 | if (err) |
| 2064 | goto out_err; |
| 2065 | |
| 2066 | if (prev == &sbi->s_orphan) { |
| 2067 | jbd_debug(4, "superblock will point to %lu\n", ino_next); |
| 2068 | BUFFER_TRACE(sbi->s_sbh, "get_write_access"); |
| 2069 | err = ext3_journal_get_write_access(handle, sbi->s_sbh); |
| 2070 | if (err) |
| 2071 | goto out_brelse; |
| 2072 | sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); |
| 2073 | err = ext3_journal_dirty_metadata(handle, sbi->s_sbh); |
| 2074 | } else { |
| 2075 | struct ext3_iloc iloc2; |
| 2076 | struct inode *i_prev = |
| 2077 | &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode; |
| 2078 | |
| 2079 | jbd_debug(4, "orphan inode %lu will point to %lu\n", |
| 2080 | i_prev->i_ino, ino_next); |
| 2081 | err = ext3_reserve_inode_write(handle, i_prev, &iloc2); |
| 2082 | if (err) |
| 2083 | goto out_brelse; |
| 2084 | NEXT_ORPHAN(i_prev) = ino_next; |
| 2085 | err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2); |
| 2086 | } |
| 2087 | if (err) |
| 2088 | goto out_brelse; |
| 2089 | NEXT_ORPHAN(inode) = 0; |
| 2090 | err = ext3_mark_iloc_dirty(handle, inode, &iloc); |
| 2091 | |
| 2092 | out_err: |
| 2093 | ext3_std_error(inode->i_sb, err); |
| 2094 | out: |
Eric Sandeen | b8a052d | 2009-12-14 13:00:30 -0600 | [diff] [blame] | 2095 | mutex_unlock(&EXT3_SB(inode->i_sb)->s_orphan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | return err; |
| 2097 | |
| 2098 | out_brelse: |
| 2099 | brelse(iloc.bh); |
| 2100 | goto out_err; |
| 2101 | } |
| 2102 | |
| 2103 | static int ext3_rmdir (struct inode * dir, struct dentry *dentry) |
| 2104 | { |
| 2105 | int retval; |
| 2106 | struct inode * inode; |
| 2107 | struct buffer_head * bh; |
| 2108 | struct ext3_dir_entry_2 * de; |
| 2109 | handle_t *handle; |
| 2110 | |
| 2111 | /* Initialize quotas before so that eventual writes go in |
| 2112 | * separate transaction */ |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2113 | dquot_initialize(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2114 | dquot_initialize(d_inode(dentry)); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2115 | |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 2116 | handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | if (IS_ERR(handle)) |
| 2118 | return PTR_ERR(handle); |
| 2119 | |
| 2120 | retval = -ENOENT; |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 2121 | bh = ext3_find_entry(dir, &dentry->d_name, &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | if (!bh) |
| 2123 | goto end_rmdir; |
| 2124 | |
| 2125 | if (IS_DIRSYNC(dir)) |
| 2126 | handle->h_sync = 1; |
| 2127 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2128 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | retval = -EIO; |
| 2131 | if (le32_to_cpu(de->inode) != inode->i_ino) |
| 2132 | goto end_rmdir; |
| 2133 | |
| 2134 | retval = -ENOTEMPTY; |
| 2135 | if (!empty_dir (inode)) |
| 2136 | goto end_rmdir; |
| 2137 | |
| 2138 | retval = ext3_delete_entry(handle, dir, de, bh); |
| 2139 | if (retval) |
| 2140 | goto end_rmdir; |
| 2141 | if (inode->i_nlink != 2) |
| 2142 | ext3_warning (inode->i_sb, "ext3_rmdir", |
| 2143 | "empty directory has nlink!=2 (%d)", |
| 2144 | inode->i_nlink); |
| 2145 | inode->i_version++; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 2146 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | /* There's no need to set i_disksize: the fact that i_nlink is |
| 2148 | * zero will ensure that the right thing happens during any |
| 2149 | * recovery. */ |
| 2150 | inode->i_size = 0; |
| 2151 | ext3_orphan_add(handle, inode); |
| 2152 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; |
| 2153 | ext3_mark_inode_dirty(handle, inode); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2154 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | ext3_update_dx_flag(dir); |
| 2156 | ext3_mark_inode_dirty(handle, dir); |
| 2157 | |
| 2158 | end_rmdir: |
| 2159 | ext3_journal_stop(handle); |
| 2160 | brelse (bh); |
| 2161 | return retval; |
| 2162 | } |
| 2163 | |
| 2164 | static int ext3_unlink(struct inode * dir, struct dentry *dentry) |
| 2165 | { |
| 2166 | int retval; |
| 2167 | struct inode * inode; |
| 2168 | struct buffer_head * bh; |
| 2169 | struct ext3_dir_entry_2 * de; |
| 2170 | handle_t *handle; |
| 2171 | |
Lukas Czerner | 785c4bc | 2011-05-23 18:33:01 +0200 | [diff] [blame] | 2172 | trace_ext3_unlink_enter(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | /* Initialize quotas before so that eventual writes go |
| 2174 | * in separate transaction */ |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2175 | dquot_initialize(dir); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2176 | dquot_initialize(d_inode(dentry)); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2177 | |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 2178 | handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | if (IS_ERR(handle)) |
| 2180 | return PTR_ERR(handle); |
| 2181 | |
| 2182 | if (IS_DIRSYNC(dir)) |
| 2183 | handle->h_sync = 1; |
| 2184 | |
| 2185 | retval = -ENOENT; |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 2186 | bh = ext3_find_entry(dir, &dentry->d_name, &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | if (!bh) |
| 2188 | goto end_unlink; |
| 2189 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2190 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | |
| 2192 | retval = -EIO; |
| 2193 | if (le32_to_cpu(de->inode) != inode->i_ino) |
| 2194 | goto end_unlink; |
| 2195 | |
| 2196 | if (!inode->i_nlink) { |
| 2197 | ext3_warning (inode->i_sb, "ext3_unlink", |
| 2198 | "Deleting nonexistent file (%lu), %d", |
| 2199 | inode->i_ino, inode->i_nlink); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2200 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | } |
| 2202 | retval = ext3_delete_entry(handle, dir, de, bh); |
| 2203 | if (retval) |
| 2204 | goto end_unlink; |
| 2205 | dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; |
| 2206 | ext3_update_dx_flag(dir); |
| 2207 | ext3_mark_inode_dirty(handle, dir); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2208 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | if (!inode->i_nlink) |
| 2210 | ext3_orphan_add(handle, inode); |
| 2211 | inode->i_ctime = dir->i_ctime; |
| 2212 | ext3_mark_inode_dirty(handle, inode); |
| 2213 | retval = 0; |
| 2214 | |
| 2215 | end_unlink: |
| 2216 | ext3_journal_stop(handle); |
| 2217 | brelse (bh); |
Lukas Czerner | 785c4bc | 2011-05-23 18:33:01 +0200 | [diff] [blame] | 2218 | trace_ext3_unlink_exit(dentry, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | return retval; |
| 2220 | } |
| 2221 | |
| 2222 | static int ext3_symlink (struct inode * dir, |
| 2223 | struct dentry *dentry, const char * symname) |
| 2224 | { |
| 2225 | handle_t *handle; |
| 2226 | struct inode * inode; |
| 2227 | int l, err, retries = 0; |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2228 | int credits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
| 2230 | l = strlen(symname)+1; |
| 2231 | if (l > dir->i_sb->s_blocksize) |
| 2232 | return -ENAMETOOLONG; |
| 2233 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2234 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2235 | |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2236 | if (l > EXT3_N_BLOCKS * 4) { |
| 2237 | /* |
| 2238 | * For non-fast symlinks, we just allocate inode and put it on |
| 2239 | * orphan list in the first transaction => we need bitmap, |
Eric Sandeen | d2db60d | 2011-08-11 09:51:46 -0500 | [diff] [blame] | 2240 | * group descriptor, sb, inode block, quota blocks, and |
| 2241 | * possibly selinux xattr blocks. |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2242 | */ |
Eric Sandeen | d2db60d | 2011-08-11 09:51:46 -0500 | [diff] [blame] | 2243 | credits = 4 + EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + |
| 2244 | EXT3_XATTR_TRANS_BLOCKS; |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2245 | } else { |
| 2246 | /* |
| 2247 | * Fast symlink. We have to add entry to directory |
| 2248 | * (EXT3_DATA_TRANS_BLOCKS + EXT3_INDEX_EXTRA_TRANS_BLOCKS), |
| 2249 | * allocate new inode (bitmap, group descriptor, inode block, |
| 2250 | * quota blocks, sb is already counted in previous macros). |
| 2251 | */ |
| 2252 | credits = EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2253 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
| 2254 | EXT3_MAXQUOTAS_INIT_BLOCKS(dir->i_sb); |
| 2255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | retry: |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2257 | handle = ext3_journal_start(dir, credits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | if (IS_ERR(handle)) |
| 2259 | return PTR_ERR(handle); |
| 2260 | |
| 2261 | if (IS_DIRSYNC(dir)) |
| 2262 | handle->h_sync = 1; |
| 2263 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2264 | inode = ext3_new_inode (handle, dir, &dentry->d_name, S_IFLNK|S_IRWXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | err = PTR_ERR(inode); |
| 2266 | if (IS_ERR(inode)) |
| 2267 | goto out_stop; |
| 2268 | |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2269 | if (l > EXT3_N_BLOCKS * 4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | inode->i_op = &ext3_symlink_inode_operations; |
| 2271 | ext3_set_aops(inode); |
| 2272 | /* |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2273 | * We cannot call page_symlink() with transaction started |
| 2274 | * because it calls into ext3_write_begin() which acquires page |
| 2275 | * lock which ranks below transaction start (and it can also |
| 2276 | * wait for journal commit if we are running out of space). So |
| 2277 | * we have to stop transaction now and restart it when symlink |
| 2278 | * contents is written. |
| 2279 | * |
| 2280 | * To keep fs consistent in case of crash, we have to put inode |
| 2281 | * to orphan list in the mean time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | */ |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2283 | drop_nlink(inode); |
| 2284 | err = ext3_orphan_add(handle, inode); |
| 2285 | ext3_journal_stop(handle); |
| 2286 | if (err) |
| 2287 | goto err_drop_inode; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2288 | err = __page_symlink(inode, symname, l, 1); |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2289 | if (err) |
| 2290 | goto err_drop_inode; |
| 2291 | /* |
| 2292 | * Now inode is being linked into dir (EXT3_DATA_TRANS_BLOCKS |
| 2293 | * + EXT3_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified |
| 2294 | */ |
| 2295 | handle = ext3_journal_start(dir, |
| 2296 | EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2297 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 1); |
| 2298 | if (IS_ERR(handle)) { |
| 2299 | err = PTR_ERR(handle); |
| 2300 | goto err_drop_inode; |
| 2301 | } |
Al Viro | 0ce8c010 | 2012-01-08 19:50:23 -0500 | [diff] [blame] | 2302 | set_nlink(inode, 1); |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2303 | err = ext3_orphan_del(handle, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | if (err) { |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2305 | ext3_journal_stop(handle); |
Eric Sandeen | 731b9a5 | 2007-02-10 01:46:16 -0800 | [diff] [blame] | 2306 | drop_nlink(inode); |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2307 | goto err_drop_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | } |
| 2309 | } else { |
| 2310 | inode->i_op = &ext3_fast_symlink_inode_operations; |
| 2311 | memcpy((char*)&EXT3_I(inode)->i_data,symname,l); |
| 2312 | inode->i_size = l-1; |
| 2313 | } |
| 2314 | EXT3_I(inode)->i_disksize = inode->i_size; |
| 2315 | err = ext3_add_nondir(handle, dentry, inode); |
| 2316 | out_stop: |
| 2317 | ext3_journal_stop(handle); |
| 2318 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 2319 | goto retry; |
| 2320 | return err; |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2321 | err_drop_inode: |
| 2322 | unlock_new_inode(inode); |
Jan Kara | 86c4f6d8 | 2011-04-27 18:20:44 +0200 | [diff] [blame] | 2323 | iput(inode); |
Jan Kara | ae54870a | 2011-04-21 23:47:16 +0200 | [diff] [blame] | 2324 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | static int ext3_link (struct dentry * old_dentry, |
| 2328 | struct inode * dir, struct dentry *dentry) |
| 2329 | { |
| 2330 | handle_t *handle; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2331 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | int err, retries = 0; |
| 2333 | |
| 2334 | if (inode->i_nlink >= EXT3_LINK_MAX) |
| 2335 | return -EMLINK; |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2336 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2337 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | retry: |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 2340 | handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) + |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 2341 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | if (IS_ERR(handle)) |
| 2343 | return PTR_ERR(handle); |
| 2344 | |
| 2345 | if (IS_DIRSYNC(dir)) |
| 2346 | handle->h_sync = 1; |
| 2347 | |
| 2348 | inode->i_ctime = CURRENT_TIME_SEC; |
Eric Sandeen | 731b9a5 | 2007-02-10 01:46:16 -0800 | [diff] [blame] | 2349 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 2350 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 2352 | err = ext3_add_entry(handle, dentry, inode); |
| 2353 | if (!err) { |
| 2354 | ext3_mark_inode_dirty(handle, inode); |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 2355 | /* this can happen only for tmpfile being |
| 2356 | * linked the first time |
| 2357 | */ |
| 2358 | if (inode->i_nlink == 1) |
| 2359 | ext3_orphan_del(handle, inode); |
Al Viro | c38012d | 2008-12-30 02:02:50 -0500 | [diff] [blame] | 2360 | d_instantiate(dentry, inode); |
| 2361 | } else { |
| 2362 | drop_nlink(inode); |
| 2363 | iput(inode); |
| 2364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | ext3_journal_stop(handle); |
| 2366 | if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries)) |
| 2367 | goto retry; |
| 2368 | return err; |
| 2369 | } |
| 2370 | |
| 2371 | #define PARENT_INO(buffer) \ |
Jan Kara | 7c06a8d | 2007-11-14 17:00:19 -0800 | [diff] [blame] | 2372 | (ext3_next_entry((struct ext3_dir_entry_2 *)(buffer))->inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
| 2374 | /* |
| 2375 | * Anybody can rename anything with this: the permission checks are left to the |
| 2376 | * higher-level routines. |
| 2377 | */ |
| 2378 | static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry, |
| 2379 | struct inode * new_dir,struct dentry *new_dentry) |
| 2380 | { |
| 2381 | handle_t *handle; |
| 2382 | struct inode * old_inode, * new_inode; |
| 2383 | struct buffer_head * old_bh, * new_bh, * dir_bh; |
| 2384 | struct ext3_dir_entry_2 * old_de, * new_de; |
Theodore Ts'o | e7c8f50 | 2009-04-03 01:34:49 -0400 | [diff] [blame] | 2385 | int retval, flush_file = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2387 | dquot_initialize(old_dir); |
| 2388 | dquot_initialize(new_dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | old_bh = new_bh = dir_bh = NULL; |
| 2391 | |
| 2392 | /* Initialize quotas before so that eventual writes go |
| 2393 | * in separate transaction */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2394 | if (d_really_is_positive(new_dentry)) |
| 2395 | dquot_initialize(d_inode(new_dentry)); |
Jan Kara | 1f54587 | 2005-06-23 22:01:04 -0700 | [diff] [blame] | 2396 | handle = ext3_journal_start(old_dir, 2 * |
| 2397 | EXT3_DATA_TRANS_BLOCKS(old_dir->i_sb) + |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 2398 | EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | if (IS_ERR(handle)) |
| 2400 | return PTR_ERR(handle); |
| 2401 | |
| 2402 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
| 2403 | handle->h_sync = 1; |
| 2404 | |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 2405 | old_bh = ext3_find_entry(old_dir, &old_dentry->d_name, &old_de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | /* |
| 2407 | * Check for inode number is _not_ due to possible IO errors. |
| 2408 | * We might rmdir the source, keep it as pwd of some process |
| 2409 | * and merrily kill the link to whatever was created under the |
| 2410 | * same name. Goodbye sticky bit ;-< |
| 2411 | */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2412 | old_inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | retval = -ENOENT; |
| 2414 | if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino) |
| 2415 | goto end_rename; |
| 2416 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2417 | new_inode = d_inode(new_dentry); |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 2418 | new_bh = ext3_find_entry(new_dir, &new_dentry->d_name, &new_de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | if (new_bh) { |
| 2420 | if (!new_inode) { |
| 2421 | brelse (new_bh); |
| 2422 | new_bh = NULL; |
| 2423 | } |
| 2424 | } |
| 2425 | if (S_ISDIR(old_inode->i_mode)) { |
| 2426 | if (new_inode) { |
| 2427 | retval = -ENOTEMPTY; |
| 2428 | if (!empty_dir (new_inode)) |
| 2429 | goto end_rename; |
| 2430 | } |
| 2431 | retval = -EIO; |
Carlos Maiolino | c3d59ad | 2012-10-02 23:59:23 -0300 | [diff] [blame] | 2432 | dir_bh = ext3_dir_bread(handle, old_inode, 0, 0, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | if (!dir_bh) |
| 2434 | goto end_rename; |
| 2435 | if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino) |
| 2436 | goto end_rename; |
| 2437 | retval = -EMLINK; |
| 2438 | if (!new_inode && new_dir!=old_dir && |
| 2439 | new_dir->i_nlink >= EXT3_LINK_MAX) |
| 2440 | goto end_rename; |
| 2441 | } |
| 2442 | if (!new_bh) { |
| 2443 | retval = ext3_add_entry (handle, new_dentry, old_inode); |
| 2444 | if (retval) |
| 2445 | goto end_rename; |
| 2446 | } else { |
| 2447 | BUFFER_TRACE(new_bh, "get write access"); |
Namhyung Kim | ad1857a | 2010-11-23 13:30:33 +0900 | [diff] [blame] | 2448 | retval = ext3_journal_get_write_access(handle, new_bh); |
| 2449 | if (retval) |
| 2450 | goto journal_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | new_de->inode = cpu_to_le32(old_inode->i_ino); |
| 2452 | if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb, |
| 2453 | EXT3_FEATURE_INCOMPAT_FILETYPE)) |
| 2454 | new_de->file_type = old_de->file_type; |
| 2455 | new_dir->i_version++; |
Jan Kara | 0b23076 | 2008-04-28 02:16:12 -0700 | [diff] [blame] | 2456 | new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME_SEC; |
| 2457 | ext3_mark_inode_dirty(handle, new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata"); |
Namhyung Kim | ad1857a | 2010-11-23 13:30:33 +0900 | [diff] [blame] | 2459 | retval = ext3_journal_dirty_metadata(handle, new_bh); |
| 2460 | if (retval) |
| 2461 | goto journal_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | brelse(new_bh); |
| 2463 | new_bh = NULL; |
| 2464 | } |
| 2465 | |
| 2466 | /* |
| 2467 | * Like most other Unix systems, set the ctime for inodes on a |
| 2468 | * rename. |
| 2469 | */ |
| 2470 | old_inode->i_ctime = CURRENT_TIME_SEC; |
| 2471 | ext3_mark_inode_dirty(handle, old_inode); |
| 2472 | |
| 2473 | /* |
| 2474 | * ok, that's it |
| 2475 | */ |
| 2476 | if (le32_to_cpu(old_de->inode) != old_inode->i_ino || |
| 2477 | old_de->name_len != old_dentry->d_name.len || |
| 2478 | strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) || |
| 2479 | (retval = ext3_delete_entry(handle, old_dir, |
| 2480 | old_de, old_bh)) == -ENOENT) { |
| 2481 | /* old_de could have moved from under us during htree split, so |
| 2482 | * make sure that we are deleting the right entry. We might |
| 2483 | * also be pointing to a stale entry in the unused part of |
| 2484 | * old_bh so just checking inum and the name isn't enough. */ |
| 2485 | struct buffer_head *old_bh2; |
| 2486 | struct ext3_dir_entry_2 *old_de2; |
| 2487 | |
Al Viro | 734711a | 2008-08-24 07:26:48 -0400 | [diff] [blame] | 2488 | old_bh2 = ext3_find_entry(old_dir, &old_dentry->d_name, |
| 2489 | &old_de2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | if (old_bh2) { |
| 2491 | retval = ext3_delete_entry(handle, old_dir, |
| 2492 | old_de2, old_bh2); |
| 2493 | brelse(old_bh2); |
| 2494 | } |
| 2495 | } |
| 2496 | if (retval) { |
| 2497 | ext3_warning(old_dir->i_sb, "ext3_rename", |
| 2498 | "Deleting old file (%lu), %d, error=%d", |
| 2499 | old_dir->i_ino, old_dir->i_nlink, retval); |
| 2500 | } |
| 2501 | |
| 2502 | if (new_inode) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2503 | drop_nlink(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | new_inode->i_ctime = CURRENT_TIME_SEC; |
| 2505 | } |
| 2506 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC; |
| 2507 | ext3_update_dx_flag(old_dir); |
| 2508 | if (dir_bh) { |
| 2509 | BUFFER_TRACE(dir_bh, "get_write_access"); |
Namhyung Kim | ad1857a | 2010-11-23 13:30:33 +0900 | [diff] [blame] | 2510 | retval = ext3_journal_get_write_access(handle, dir_bh); |
| 2511 | if (retval) |
| 2512 | goto journal_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino); |
| 2514 | BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata"); |
Namhyung Kim | ad1857a | 2010-11-23 13:30:33 +0900 | [diff] [blame] | 2515 | retval = ext3_journal_dirty_metadata(handle, dir_bh); |
| 2516 | if (retval) { |
| 2517 | journal_error: |
| 2518 | ext3_std_error(new_dir->i_sb, retval); |
| 2519 | goto end_rename; |
| 2520 | } |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2521 | drop_nlink(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | if (new_inode) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 2523 | drop_nlink(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | } else { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2525 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | ext3_update_dx_flag(new_dir); |
| 2527 | ext3_mark_inode_dirty(handle, new_dir); |
| 2528 | } |
| 2529 | } |
| 2530 | ext3_mark_inode_dirty(handle, old_dir); |
| 2531 | if (new_inode) { |
| 2532 | ext3_mark_inode_dirty(handle, new_inode); |
| 2533 | if (!new_inode->i_nlink) |
| 2534 | ext3_orphan_add(handle, new_inode); |
Theodore Ts'o | e7c8f50 | 2009-04-03 01:34:49 -0400 | [diff] [blame] | 2535 | if (ext3_should_writeback_data(new_inode)) |
| 2536 | flush_file = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
| 2538 | retval = 0; |
| 2539 | |
| 2540 | end_rename: |
| 2541 | brelse (dir_bh); |
| 2542 | brelse (old_bh); |
| 2543 | brelse (new_bh); |
| 2544 | ext3_journal_stop(handle); |
Theodore Ts'o | e7c8f50 | 2009-04-03 01:34:49 -0400 | [diff] [blame] | 2545 | if (retval == 0 && flush_file) |
| 2546 | filemap_flush(old_inode->i_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | return retval; |
| 2548 | } |
| 2549 | |
| 2550 | /* |
| 2551 | * directories can handle most operations... |
| 2552 | */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 2553 | const struct inode_operations ext3_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | .create = ext3_create, |
| 2555 | .lookup = ext3_lookup, |
| 2556 | .link = ext3_link, |
| 2557 | .unlink = ext3_unlink, |
| 2558 | .symlink = ext3_symlink, |
| 2559 | .mkdir = ext3_mkdir, |
| 2560 | .rmdir = ext3_rmdir, |
| 2561 | .mknod = ext3_mknod, |
Al Viro | e6bbef9 | 2013-06-11 12:52:02 +0400 | [diff] [blame] | 2562 | .tmpfile = ext3_tmpfile, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | .rename = ext3_rename, |
| 2564 | .setattr = ext3_setattr, |
| 2565 | #ifdef CONFIG_EXT3_FS_XATTR |
| 2566 | .setxattr = generic_setxattr, |
| 2567 | .getxattr = generic_getxattr, |
| 2568 | .listxattr = ext3_listxattr, |
| 2569 | .removexattr = generic_removexattr, |
| 2570 | #endif |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 2571 | .get_acl = ext3_get_acl, |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 2572 | .set_acl = ext3_set_acl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | }; |
| 2574 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 2575 | const struct inode_operations ext3_special_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | .setattr = ext3_setattr, |
| 2577 | #ifdef CONFIG_EXT3_FS_XATTR |
| 2578 | .setxattr = generic_setxattr, |
| 2579 | .getxattr = generic_getxattr, |
| 2580 | .listxattr = ext3_listxattr, |
| 2581 | .removexattr = generic_removexattr, |
| 2582 | #endif |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 2583 | .get_acl = ext3_get_acl, |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 2584 | .set_acl = ext3_set_acl, |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 2585 | }; |