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