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