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