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