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