]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ext4/namei.c
ext4: create __ext4_insert_dentry for dir entry insertion
[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,
226ba972 895 bh->b_data, bh->b_size,
cad3f007
TT
896 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
897 + ((char *)de - bh->b_data))) {
e6c40211
ES
898 /* On error, skip the f_pos to the next block. */
899 dir_file->f_pos = (dir_file->f_pos |
900 (dir->i_sb->s_blocksize - 1)) + 1;
af5bc92d 901 brelse(bh);
e6c40211
ES
902 return count;
903 }
617ba13b 904 ext4fs_dirhash(de->name, de->name_len, hinfo);
ac27a0ec
DK
905 if ((hinfo->hash < start_hash) ||
906 ((hinfo->hash == start_hash) &&
907 (hinfo->minor_hash < start_minor_hash)))
908 continue;
909 if (de->inode == 0)
910 continue;
617ba13b 911 if ((err = ext4_htree_store_dirent(dir_file,
ac27a0ec
DK
912 hinfo->hash, hinfo->minor_hash, de)) != 0) {
913 brelse(bh);
914 return err;
915 }
916 count++;
917 }
918 brelse(bh);
919 return count;
920}
921
922
923/*
924 * This function fills a red-black tree with information from a
925 * directory. We start scanning the directory in hash order, starting
926 * at start_hash and start_minor_hash.
927 *
928 * This function returns the number of entries inserted into the tree,
929 * or a negative error code.
930 */
617ba13b 931int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
ac27a0ec
DK
932 __u32 start_minor_hash, __u32 *next_hash)
933{
934 struct dx_hash_info hinfo;
617ba13b 935 struct ext4_dir_entry_2 *de;
ac27a0ec
DK
936 struct dx_frame frames[2], *frame;
937 struct inode *dir;
725d26d3 938 ext4_lblk_t block;
ac27a0ec 939 int count = 0;
725d26d3 940 int ret, err;
ac27a0ec
DK
941 __u32 hashval;
942
60e6679e 943 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
4776004f 944 start_hash, start_minor_hash));
9d549890 945 dir = dir_file->f_path.dentry->d_inode;
12e9b892 946 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
617ba13b 947 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
f99b2589
TT
948 if (hinfo.hash_version <= DX_HASH_TEA)
949 hinfo.hash_version +=
950 EXT4_SB(dir->i_sb)->s_hash_unsigned;
617ba13b 951 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
ac27a0ec
DK
952 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
953 start_hash, start_minor_hash);
954 *next_hash = ~0;
955 return count;
956 }
957 hinfo.hash = start_hash;
958 hinfo.minor_hash = 0;
f702ba0f 959 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
ac27a0ec
DK
960 if (!frame)
961 return err;
962
963 /* Add '.' and '..' from the htree header */
964 if (!start_hash && !start_minor_hash) {
617ba13b
MC
965 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
966 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
ac27a0ec
DK
967 goto errout;
968 count++;
969 }
970 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
617ba13b 971 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
3d0518f4 972 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
617ba13b 973 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
ac27a0ec
DK
974 goto errout;
975 count++;
976 }
977
978 while (1) {
979 block = dx_get_block(frame->at);
980 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
981 start_hash, start_minor_hash);
982 if (ret < 0) {
983 err = ret;
984 goto errout;
985 }
986 count += ret;
987 hashval = ~0;
617ba13b 988 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
ac27a0ec
DK
989 frame, frames, &hashval);
990 *next_hash = hashval;
991 if (ret < 0) {
992 err = ret;
993 goto errout;
994 }
995 /*
996 * Stop if: (a) there are no more entries, or
997 * (b) we have inserted at least one entry and the
998 * next hash value is not a continuation
999 */
1000 if ((ret == 0) ||
1001 (count && ((hashval & 1) == 0)))
1002 break;
1003 }
1004 dx_release(frames);
4776004f
TT
1005 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1006 "next hash: %x\n", count, *next_hash));
ac27a0ec
DK
1007 return count;
1008errout:
1009 dx_release(frames);
1010 return (err);
1011}
1012
1013
1014/*
1015 * Directory block splitting, compacting
1016 */
1017
ef2b02d3
ES
1018/*
1019 * Create map of hash values, offsets, and sizes, stored at end of block.
1020 * Returns number of entries mapped.
1021 */
8bad4597
TT
1022static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1023 struct dx_hash_info *hinfo,
1024 struct dx_map_entry *map_tail)
ac27a0ec
DK
1025{
1026 int count = 0;
1027 char *base = (char *) de;
1028 struct dx_hash_info h = *hinfo;
1029
8bad4597 1030 while ((char *) de < base + blocksize) {
ac27a0ec 1031 if (de->name_len && de->inode) {
617ba13b 1032 ext4fs_dirhash(de->name, de->name_len, &h);
ac27a0ec
DK
1033 map_tail--;
1034 map_tail->hash = h.hash;
9aee2286 1035 map_tail->offs = ((char *) de - base)>>2;
ef2b02d3 1036 map_tail->size = le16_to_cpu(de->rec_len);
ac27a0ec
DK
1037 count++;
1038 cond_resched();
1039 }
1040 /* XXX: do we need to check rec_len == 0 case? -Chris */
3d0518f4 1041 de = ext4_next_entry(de, blocksize);
ac27a0ec
DK
1042 }
1043 return count;
1044}
1045
ef2b02d3 1046/* Sort map by hash value */
ac27a0ec
DK
1047static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1048{
63f57933
AM
1049 struct dx_map_entry *p, *q, *top = map + count - 1;
1050 int more;
1051 /* Combsort until bubble sort doesn't suck */
1052 while (count > 2) {
1053 count = count*10/13;
1054 if (count - 9 < 2) /* 9, 10 -> 11 */
1055 count = 11;
1056 for (p = top, q = p - count; q >= map; p--, q--)
1057 if (p->hash < q->hash)
1058 swap(*p, *q);
1059 }
1060 /* Garden variety bubble sort */
1061 do {
1062 more = 0;
1063 q = top;
1064 while (q-- > map) {
1065 if (q[1].hash >= q[0].hash)
ac27a0ec 1066 continue;
63f57933
AM
1067 swap(*(q+1), *q);
1068 more = 1;
ac27a0ec
DK
1069 }
1070 } while(more);
1071}
1072
725d26d3 1073static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
ac27a0ec
DK
1074{
1075 struct dx_entry *entries = frame->entries;
1076 struct dx_entry *old = frame->at, *new = old + 1;
1077 int count = dx_get_count(entries);
1078
1079 assert(count < dx_get_limit(entries));
1080 assert(old < entries + count);
1081 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1082 dx_set_hash(new, hash);
1083 dx_set_block(new, block);
1084 dx_set_count(entries, count + 1);
1085}
ac27a0ec 1086
ac27a0ec 1087/*
617ba13b 1088 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
ac27a0ec 1089 *
617ba13b 1090 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
ac27a0ec
DK
1091 * `de != NULL' is guaranteed by caller.
1092 */
617ba13b
MC
1093static inline int ext4_match (int len, const char * const name,
1094 struct ext4_dir_entry_2 * de)
ac27a0ec
DK
1095{
1096 if (len != de->name_len)
1097 return 0;
1098 if (!de->inode)
1099 return 0;
1100 return !memcmp(name, de->name, len);
1101}
1102
1103/*
1104 * Returns 0 if not found, -1 on failure, and 1 on success
1105 */
af5bc92d 1106static inline int search_dirblock(struct buffer_head *bh,
ac27a0ec 1107 struct inode *dir,
f702ba0f 1108 const struct qstr *d_name,
498e5f24 1109 unsigned int offset,
617ba13b 1110 struct ext4_dir_entry_2 ** res_dir)
ac27a0ec 1111{
617ba13b 1112 struct ext4_dir_entry_2 * de;
ac27a0ec
DK
1113 char * dlimit;
1114 int de_len;
f702ba0f
TT
1115 const char *name = d_name->name;
1116 int namelen = d_name->len;
ac27a0ec 1117
617ba13b 1118 de = (struct ext4_dir_entry_2 *) bh->b_data;
ac27a0ec
DK
1119 dlimit = bh->b_data + dir->i_sb->s_blocksize;
1120 while ((char *) de < dlimit) {
1121 /* this code is executed quadratically often */
1122 /* do minimal checking `by hand' */
1123
1124 if ((char *) de + namelen <= dlimit &&
617ba13b 1125 ext4_match (namelen, name, de)) {
ac27a0ec 1126 /* found a match - just to be sure, do a full check */
226ba972
TM
1127 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1128 bh->b_size, offset))
ac27a0ec
DK
1129 return -1;
1130 *res_dir = de;
1131 return 1;
1132 }
1133 /* prevent looping on a bad block */
3d0518f4
WY
1134 de_len = ext4_rec_len_from_disk(de->rec_len,
1135 dir->i_sb->s_blocksize);
ac27a0ec
DK
1136 if (de_len <= 0)
1137 return -1;
1138 offset += de_len;
617ba13b 1139 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
ac27a0ec
DK
1140 }
1141 return 0;
1142}
1143
c6af8803
DW
1144static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1145 struct ext4_dir_entry *de)
1146{
1147 struct super_block *sb = dir->i_sb;
1148
1149 if (!is_dx(dir))
1150 return 0;
1151 if (block == 0)
1152 return 1;
1153 if (de->inode == 0 &&
1154 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1155 sb->s_blocksize)
1156 return 1;
1157 return 0;
1158}
ac27a0ec
DK
1159
1160/*
617ba13b 1161 * ext4_find_entry()
ac27a0ec
DK
1162 *
1163 * finds an entry in the specified directory with the wanted name. It
1164 * returns the cache buffer in which the entry was found, and the entry
1165 * itself (as a parameter - res_dir). It does NOT read the inode of the
1166 * entry - you'll have to do that yourself if you want to.
1167 *
1168 * The returned buffer_head has ->b_count elevated. The caller is expected
1169 * to brelse() it when appropriate.
1170 */
f702ba0f
TT
1171static struct buffer_head * ext4_find_entry (struct inode *dir,
1172 const struct qstr *d_name,
617ba13b 1173 struct ext4_dir_entry_2 ** res_dir)
ac27a0ec 1174{
af5bc92d
TT
1175 struct super_block *sb;
1176 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1177 struct buffer_head *bh, *ret = NULL;
725d26d3 1178 ext4_lblk_t start, block, b;
8941ec8b 1179 const u8 *name = d_name->name;
ac27a0ec
DK
1180 int ra_max = 0; /* Number of bh's in the readahead
1181 buffer, bh_use[] */
1182 int ra_ptr = 0; /* Current index into readahead
1183 buffer */
1184 int num = 0;
725d26d3
AK
1185 ext4_lblk_t nblocks;
1186 int i, err;
ac27a0ec 1187 int namelen;
ac27a0ec
DK
1188
1189 *res_dir = NULL;
1190 sb = dir->i_sb;
f702ba0f 1191 namelen = d_name->len;
617ba13b 1192 if (namelen > EXT4_NAME_LEN)
ac27a0ec 1193 return NULL;
8941ec8b 1194 if ((namelen <= 2) && (name[0] == '.') &&
6d5c3aa8 1195 (name[1] == '.' || name[1] == '\0')) {
8941ec8b
TT
1196 /*
1197 * "." or ".." will only be in the first block
1198 * NFS may look up ".."; "." should be handled by the VFS
1199 */
1200 block = start = 0;
1201 nblocks = 1;
1202 goto restart;
1203 }
ac27a0ec 1204 if (is_dx(dir)) {
f702ba0f 1205 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
ac27a0ec
DK
1206 /*
1207 * On success, or if the error was file not found,
1208 * return. Otherwise, fall back to doing a search the
1209 * old fashioned way.
1210 */
1211 if (bh || (err != ERR_BAD_DX_DIR))
1212 return bh;
4776004f
TT
1213 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1214 "falling back\n"));
ac27a0ec 1215 }
617ba13b
MC
1216 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1217 start = EXT4_I(dir)->i_dir_start_lookup;
ac27a0ec
DK
1218 if (start >= nblocks)
1219 start = 0;
1220 block = start;
1221restart:
1222 do {
1223 /*
1224 * We deal with the read-ahead logic here.
1225 */
1226 if (ra_ptr >= ra_max) {
1227 /* Refill the readahead buffer */
1228 ra_ptr = 0;
1229 b = block;
1230 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1231 /*
1232 * Terminate if we reach the end of the
1233 * directory and must wrap, or if our
1234 * search has finished at this block.
1235 */
1236 if (b >= nblocks || (num && block == start)) {
1237 bh_use[ra_max] = NULL;
1238 break;
1239 }
1240 num++;
617ba13b 1241 bh = ext4_getblk(NULL, dir, b++, 0, &err);
ac27a0ec
DK
1242 bh_use[ra_max] = bh;
1243 if (bh)
65299a3b
CH
1244 ll_rw_block(READ | REQ_META | REQ_PRIO,
1245 1, &bh);
ac27a0ec
DK
1246 }
1247 }
1248 if ((bh = bh_use[ra_ptr++]) == NULL)
1249 goto next;
1250 wait_on_buffer(bh);
1251 if (!buffer_uptodate(bh)) {
1252 /* read error, skip block & hope for the best */
24676da4
TT
1253 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1254 (unsigned long) block);
ac27a0ec
DK
1255 brelse(bh);
1256 goto next;
1257 }
b0336e8d 1258 if (!buffer_verified(bh) &&
c6af8803
DW
1259 !is_dx_internal_node(dir, block,
1260 (struct ext4_dir_entry *)bh->b_data) &&
b0336e8d
DW
1261 !ext4_dirent_csum_verify(dir,
1262 (struct ext4_dir_entry *)bh->b_data)) {
1263 EXT4_ERROR_INODE(dir, "checksumming directory "
1264 "block %lu", (unsigned long)block);
1265 brelse(bh);
1266 goto next;
1267 }
1268 set_buffer_verified(bh);
f702ba0f 1269 i = search_dirblock(bh, dir, d_name,
617ba13b 1270 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
ac27a0ec 1271 if (i == 1) {
617ba13b 1272 EXT4_I(dir)->i_dir_start_lookup = block;
ac27a0ec
DK
1273 ret = bh;
1274 goto cleanup_and_exit;
1275 } else {
1276 brelse(bh);
1277 if (i < 0)
1278 goto cleanup_and_exit;
1279 }
1280 next:
1281 if (++block >= nblocks)
1282 block = 0;
1283 } while (block != start);
1284
1285 /*
1286 * If the directory has grown while we were searching, then
1287 * search the last part of the directory before giving up.
1288 */
1289 block = nblocks;
617ba13b 1290 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
1291 if (block < nblocks) {
1292 start = 0;
1293 goto restart;
1294 }
1295
1296cleanup_and_exit:
1297 /* Clean up the read-ahead blocks */
1298 for (; ra_ptr < ra_max; ra_ptr++)
af5bc92d 1299 brelse(bh_use[ra_ptr]);
ac27a0ec
DK
1300 return ret;
1301}
1302
f702ba0f 1303static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
617ba13b 1304 struct ext4_dir_entry_2 **res_dir, int *err)
ac27a0ec 1305{
8941ec8b 1306 struct super_block * sb = dir->i_sb;
ac27a0ec 1307 struct dx_hash_info hinfo;
ac27a0ec 1308 struct dx_frame frames[2], *frame;
ac27a0ec 1309 struct buffer_head *bh;
725d26d3 1310 ext4_lblk_t block;
ac27a0ec 1311 int retval;
ac27a0ec 1312
8941ec8b
TT
1313 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1314 return NULL;
ac27a0ec
DK
1315 do {
1316 block = dx_get_block(frame->at);
6d1ab10e
CM
1317 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1318 if (!(*err)) {
1319 *err = -EIO;
1320 ext4_error(dir->i_sb,
1321 "Directory hole detected on inode %lu\n",
1322 dir->i_ino);
1323 }
ac27a0ec 1324 goto errout;
6d1ab10e 1325 }
f3b35f06 1326
b0336e8d
DW
1327 if (!buffer_verified(bh) &&
1328 !ext4_dirent_csum_verify(dir,
1329 (struct ext4_dir_entry *)bh->b_data)) {
1330 EXT4_ERROR_INODE(dir, "checksumming directory "
1331 "block %lu", (unsigned long)block);
1332 brelse(bh);
1333 *err = -EIO;
1334 goto errout;
1335 }
1336 set_buffer_verified(bh);
7845c049
TT
1337 retval = search_dirblock(bh, dir, d_name,
1338 block << EXT4_BLOCK_SIZE_BITS(sb),
1339 res_dir);
1340 if (retval == 1) { /* Success! */
1341 dx_release(frames);
1342 return bh;
ac27a0ec 1343 }
af5bc92d 1344 brelse(bh);
7845c049
TT
1345 if (retval == -1) {
1346 *err = ERR_BAD_DX_DIR;
1347 goto errout;
1348 }
1349
ac27a0ec 1350 /* Check to see if we should continue to search */
8941ec8b 1351 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
ac27a0ec
DK
1352 frames, NULL);
1353 if (retval < 0) {
12062ddd 1354 ext4_warning(sb,
ac27a0ec
DK
1355 "error reading index page in directory #%lu",
1356 dir->i_ino);
1357 *err = retval;
1358 goto errout;
1359 }
1360 } while (retval == 1);
1361
1362 *err = -ENOENT;
1363errout:
265c6a0f 1364 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
ac27a0ec
DK
1365 dx_release (frames);
1366 return NULL;
1367}
ac27a0ec 1368
00cd8dd3 1369static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
ac27a0ec 1370{
af5bc92d
TT
1371 struct inode *inode;
1372 struct ext4_dir_entry_2 *de;
1373 struct buffer_head *bh;
ac27a0ec 1374
617ba13b 1375 if (dentry->d_name.len > EXT4_NAME_LEN)
ac27a0ec
DK
1376 return ERR_PTR(-ENAMETOOLONG);
1377
f702ba0f 1378 bh = ext4_find_entry(dir, &dentry->d_name, &de);
ac27a0ec
DK
1379 inode = NULL;
1380 if (bh) {
498e5f24 1381 __u32 ino = le32_to_cpu(de->inode);
af5bc92d 1382 brelse(bh);
617ba13b 1383 if (!ext4_valid_inum(dir->i_sb, ino)) {
24676da4 1384 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1d1fe1ee 1385 return ERR_PTR(-EIO);
a6c15c2b 1386 }
7e936b73
AD
1387 if (unlikely(ino == dir->i_ino)) {
1388 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1389 dentry->d_name.len,
1390 dentry->d_name.name);
1391 return ERR_PTR(-EIO);
1392 }
1d1fe1ee 1393 inode = ext4_iget(dir->i_sb, ino);
a9049376
AV
1394 if (inode == ERR_PTR(-ESTALE)) {
1395 EXT4_ERROR_INODE(dir,
1396 "deleted inode referenced: %u",
1397 ino);
1398 return ERR_PTR(-EIO);
e6f009b0 1399 }
ac27a0ec
DK
1400 }
1401 return d_splice_alias(inode, dentry);
1402}
1403
1404
617ba13b 1405struct dentry *ext4_get_parent(struct dentry *child)
ac27a0ec 1406{
498e5f24 1407 __u32 ino;
26fe5750 1408 static const struct qstr dotdot = QSTR_INIT("..", 2);
617ba13b 1409 struct ext4_dir_entry_2 * de;
ac27a0ec
DK
1410 struct buffer_head *bh;
1411
f702ba0f 1412 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
ac27a0ec
DK
1413 if (!bh)
1414 return ERR_PTR(-ENOENT);
1415 ino = le32_to_cpu(de->inode);
1416 brelse(bh);
1417
617ba13b 1418 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
24676da4
TT
1419 EXT4_ERROR_INODE(child->d_inode,
1420 "bad parent inode number: %u", ino);
1d1fe1ee 1421 return ERR_PTR(-EIO);
a6c15c2b
VA
1422 }
1423
44003728 1424 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
ac27a0ec
DK
1425}
1426
1427#define S_SHIFT 12
617ba13b
MC
1428static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1429 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1430 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1431 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1432 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1433 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1434 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1435 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
ac27a0ec
DK
1436};
1437
617ba13b
MC
1438static inline void ext4_set_de_type(struct super_block *sb,
1439 struct ext4_dir_entry_2 *de,
ac27a0ec 1440 umode_t mode) {
617ba13b
MC
1441 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1442 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
ac27a0ec
DK
1443}
1444
ef2b02d3
ES
1445/*
1446 * Move count entries from end of map between two memory locations.
1447 * Returns pointer to last entry moved.
1448 */
617ba13b 1449static struct ext4_dir_entry_2 *
3d0518f4
WY
1450dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1451 unsigned blocksize)
ac27a0ec
DK
1452{
1453 unsigned rec_len = 0;
1454
1455 while (count--) {
60e6679e 1456 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
9aee2286 1457 (from + (map->offs<<2));
617ba13b 1458 rec_len = EXT4_DIR_REC_LEN(de->name_len);
ac27a0ec 1459 memcpy (to, de, rec_len);
617ba13b 1460 ((struct ext4_dir_entry_2 *) to)->rec_len =
3d0518f4 1461 ext4_rec_len_to_disk(rec_len, blocksize);
ac27a0ec
DK
1462 de->inode = 0;
1463 map++;
1464 to += rec_len;
1465 }
617ba13b 1466 return (struct ext4_dir_entry_2 *) (to - rec_len);
ac27a0ec
DK
1467}
1468
ef2b02d3
ES
1469/*
1470 * Compact each dir entry in the range to the minimal rec_len.
1471 * Returns pointer to last entry in range.
1472 */
8bad4597 1473static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
ac27a0ec 1474{
617ba13b 1475 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
ac27a0ec
DK
1476 unsigned rec_len = 0;
1477
1478 prev = to = de;
8bad4597 1479 while ((char*)de < base + blocksize) {
3d0518f4 1480 next = ext4_next_entry(de, blocksize);
ac27a0ec 1481 if (de->inode && de->name_len) {
617ba13b 1482 rec_len = EXT4_DIR_REC_LEN(de->name_len);
ac27a0ec
DK
1483 if (de > to)
1484 memmove(to, de, rec_len);
3d0518f4 1485 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
ac27a0ec 1486 prev = to;
617ba13b 1487 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
ac27a0ec
DK
1488 }
1489 de = next;
1490 }
1491 return prev;
1492}
1493
ef2b02d3
ES
1494/*
1495 * Split a full leaf block to make room for a new dir entry.
1496 * Allocate a new block, and move entries so that they are approx. equally full.
1497 * Returns pointer to de in block into which the new entry will be inserted.
1498 */
617ba13b 1499static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
ac27a0ec
DK
1500 struct buffer_head **bh,struct dx_frame *frame,
1501 struct dx_hash_info *hinfo, int *error)
1502{
1503 unsigned blocksize = dir->i_sb->s_blocksize;
1504 unsigned count, continued;
1505 struct buffer_head *bh2;
725d26d3 1506 ext4_lblk_t newblock;
ac27a0ec
DK
1507 u32 hash2;
1508 struct dx_map_entry *map;
1509 char *data1 = (*bh)->b_data, *data2;
59e315b4 1510 unsigned split, move, size;
617ba13b 1511 struct ext4_dir_entry_2 *de = NULL, *de2;
b0336e8d
DW
1512 struct ext4_dir_entry_tail *t;
1513 int csum_size = 0;
59e315b4 1514 int err = 0, i;
ac27a0ec 1515
b0336e8d
DW
1516 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1517 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1518 csum_size = sizeof(struct ext4_dir_entry_tail);
1519
fedee54d 1520 bh2 = ext4_append (handle, dir, &newblock, &err);
ac27a0ec
DK
1521 if (!(bh2)) {
1522 brelse(*bh);
1523 *bh = NULL;
1524 goto errout;
1525 }
1526
1527 BUFFER_TRACE(*bh, "get_write_access");
617ba13b 1528 err = ext4_journal_get_write_access(handle, *bh);
fedee54d
DM
1529 if (err)
1530 goto journal_error;
1531
ac27a0ec 1532 BUFFER_TRACE(frame->bh, "get_write_access");
617ba13b 1533 err = ext4_journal_get_write_access(handle, frame->bh);
ac27a0ec
DK
1534 if (err)
1535 goto journal_error;
1536
1537 data2 = bh2->b_data;
1538
1539 /* create map in the end of data2 block */
1540 map = (struct dx_map_entry *) (data2 + blocksize);
af5bc92d 1541 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
ac27a0ec
DK
1542 blocksize, hinfo, map);
1543 map -= count;
af5bc92d 1544 dx_sort_map(map, count);
ef2b02d3
ES
1545 /* Split the existing block in the middle, size-wise */
1546 size = 0;
1547 move = 0;
1548 for (i = count-1; i >= 0; i--) {
1549 /* is more than half of this entry in 2nd half of the block? */
1550 if (size + map[i].size/2 > blocksize/2)
1551 break;
1552 size += map[i].size;
1553 move++;
1554 }
1555 /* map index at which we will split */
1556 split = count - move;
ac27a0ec
DK
1557 hash2 = map[split].hash;
1558 continued = hash2 == map[split - 1].hash;
725d26d3
AK
1559 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1560 (unsigned long)dx_get_block(frame->at),
1561 hash2, split, count-split));
ac27a0ec
DK
1562
1563 /* Fancy dance to stay within two buffers */
3d0518f4 1564 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
af5bc92d 1565 de = dx_pack_dirents(data1, blocksize);
b0336e8d
DW
1566 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1567 (char *) de,
3d0518f4 1568 blocksize);
b0336e8d
DW
1569 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1570 (char *) de2,
3d0518f4 1571 blocksize);
b0336e8d
DW
1572 if (csum_size) {
1573 t = EXT4_DIRENT_TAIL(data2, blocksize);
1574 initialize_dirent_tail(t, blocksize);
1575
1576 t = EXT4_DIRENT_TAIL(data1, blocksize);
1577 initialize_dirent_tail(t, blocksize);
1578 }
1579
617ba13b
MC
1580 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1581 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
ac27a0ec
DK
1582
1583 /* Which block gets the new entry? */
1584 if (hinfo->hash >= hash2)
1585 {
1586 swap(*bh, bh2);
1587 de = de2;
1588 }
af5bc92d 1589 dx_insert_block(frame, hash2 + continued, newblock);
b0336e8d 1590 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
ac27a0ec
DK
1591 if (err)
1592 goto journal_error;
dbe89444 1593 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
ac27a0ec
DK
1594 if (err)
1595 goto journal_error;
af5bc92d
TT
1596 brelse(bh2);
1597 dxtrace(dx_show_index("frame", frame->entries));
ac27a0ec 1598 return de;
fedee54d
DM
1599
1600journal_error:
1601 brelse(*bh);
1602 brelse(bh2);
1603 *bh = NULL;
1604 ext4_std_error(dir->i_sb, err);
1605errout:
1606 *error = err;
1607 return NULL;
ac27a0ec 1608}
ac27a0ec 1609
978fef91
TM
1610int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1611 struct buffer_head *bh,
1612 void *buf, int buf_size,
1613 const char *name, int namelen,
1614 struct ext4_dir_entry_2 **dest_de)
1615{
1616 struct ext4_dir_entry_2 *de;
1617 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1618 int nlen, rlen;
1619 unsigned int offset = 0;
1620 char *top;
1621
1622 de = (struct ext4_dir_entry_2 *)buf;
1623 top = buf + buf_size - reclen;
1624 while ((char *) de <= top) {
1625 if (ext4_check_dir_entry(dir, NULL, de, bh,
1626 buf, buf_size, offset))
1627 return -EIO;
1628 if (ext4_match(namelen, name, de))
1629 return -EEXIST;
1630 nlen = EXT4_DIR_REC_LEN(de->name_len);
1631 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1632 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1633 break;
1634 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1635 offset += rlen;
1636 }
1637 if ((char *) de > top)
1638 return -ENOSPC;
1639
1640 *dest_de = de;
1641 return 0;
1642}
1643
1644void ext4_insert_dentry(struct inode *inode,
1645 struct ext4_dir_entry_2 *de,
1646 int buf_size,
1647 const char *name, int namelen)
1648{
1649
1650 int nlen, rlen;
1651
1652 nlen = EXT4_DIR_REC_LEN(de->name_len);
1653 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1654 if (de->inode) {
1655 struct ext4_dir_entry_2 *de1 =
1656 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1657 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1658 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1659 de = de1;
1660 }
1661 de->file_type = EXT4_FT_UNKNOWN;
1662 de->inode = cpu_to_le32(inode->i_ino);
1663 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1664 de->name_len = namelen;
1665 memcpy(de->name, name, namelen);
1666}
ac27a0ec
DK
1667/*
1668 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1669 * it points to a directory entry which is guaranteed to be large
1670 * enough for new directory entry. If de is NULL, then
1671 * add_dirent_to_buf will attempt search the directory block for
1672 * space. It will return -ENOSPC if no space is available, and -EIO
1673 * and -EEXIST if directory entry already exists.
ac27a0ec
DK
1674 */
1675static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
617ba13b 1676 struct inode *inode, struct ext4_dir_entry_2 *de,
af5bc92d 1677 struct buffer_head *bh)
ac27a0ec
DK
1678{
1679 struct inode *dir = dentry->d_parent->d_inode;
1680 const char *name = dentry->d_name.name;
1681 int namelen = dentry->d_name.len;
3d0518f4 1682 unsigned int blocksize = dir->i_sb->s_blocksize;
ac27a0ec 1683 unsigned short reclen;
b0336e8d 1684 int csum_size = 0;
978fef91 1685 int err;
b0336e8d
DW
1686
1687 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1688 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1689 csum_size = sizeof(struct ext4_dir_entry_tail);
ac27a0ec 1690
617ba13b 1691 reclen = EXT4_DIR_REC_LEN(namelen);
ac27a0ec 1692 if (!de) {
978fef91
TM
1693 err = ext4_find_dest_de(dir, inode,
1694 bh, bh->b_data, blocksize - csum_size,
1695 name, namelen, &de);
1696 if (err)
1697 return err;
ac27a0ec
DK
1698 }
1699 BUFFER_TRACE(bh, "get_write_access");
617ba13b 1700 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec 1701 if (err) {
617ba13b 1702 ext4_std_error(dir->i_sb, err);
ac27a0ec
DK
1703 return err;
1704 }
1705
1706 /* By now the buffer is marked for journaling */
978fef91
TM
1707 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1708
ac27a0ec
DK
1709 /*
1710 * XXX shouldn't update any times until successful
1711 * completion of syscall, but too many callers depend
1712 * on this.
1713 *
1714 * XXX similarly, too many callers depend on
617ba13b 1715 * ext4_new_inode() setting the times, but error
ac27a0ec
DK
1716 * recovery deletes the inode, so the worst that can
1717 * happen is that the times are slightly out of date
1718 * and/or different from the directory change time.
1719 */
ef7f3835 1720 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
617ba13b 1721 ext4_update_dx_flag(dir);
ac27a0ec 1722 dir->i_version++;
617ba13b 1723 ext4_mark_inode_dirty(handle, dir);
0390131b 1724 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
b0336e8d 1725 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
ac27a0ec 1726 if (err)
617ba13b 1727 ext4_std_error(dir->i_sb, err);
ac27a0ec
DK
1728 return 0;
1729}
1730
ac27a0ec
DK
1731/*
1732 * This converts a one block unindexed directory to a 3 block indexed
1733 * directory, and adds the dentry to the indexed directory.
1734 */
1735static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1736 struct inode *inode, struct buffer_head *bh)
1737{
1738 struct inode *dir = dentry->d_parent->d_inode;
1739 const char *name = dentry->d_name.name;
1740 int namelen = dentry->d_name.len;
1741 struct buffer_head *bh2;
1742 struct dx_root *root;
1743 struct dx_frame frames[2], *frame;
1744 struct dx_entry *entries;
617ba13b 1745 struct ext4_dir_entry_2 *de, *de2;
b0336e8d 1746 struct ext4_dir_entry_tail *t;
ac27a0ec
DK
1747 char *data1, *top;
1748 unsigned len;
1749 int retval;
1750 unsigned blocksize;
1751 struct dx_hash_info hinfo;
725d26d3 1752 ext4_lblk_t block;
ac27a0ec 1753 struct fake_dirent *fde;
b0336e8d
DW
1754 int csum_size = 0;
1755
1756 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1757 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1758 csum_size = sizeof(struct ext4_dir_entry_tail);
ac27a0ec
DK
1759
1760 blocksize = dir->i_sb->s_blocksize;
e6b8bc09 1761 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
617ba13b 1762 retval = ext4_journal_get_write_access(handle, bh);
ac27a0ec 1763 if (retval) {
617ba13b 1764 ext4_std_error(dir->i_sb, retval);
ac27a0ec
DK
1765 brelse(bh);
1766 return retval;
1767 }
1768 root = (struct dx_root *) bh->b_data;
1769
e6b8bc09
TT
1770 /* The 0th block becomes the root, move the dirents out */
1771 fde = &root->dotdot;
1772 de = (struct ext4_dir_entry_2 *)((char *)fde +
3d0518f4 1773 ext4_rec_len_from_disk(fde->rec_len, blocksize));
e6b8bc09 1774 if ((char *) de >= (((char *) root) + blocksize)) {
24676da4 1775 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
e6b8bc09
TT
1776 brelse(bh);
1777 return -EIO;
1778 }
b0336e8d 1779 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
e6b8bc09
TT
1780
1781 /* Allocate new block for the 0th block's dirents */
af5bc92d 1782 bh2 = ext4_append(handle, dir, &block, &retval);
ac27a0ec
DK
1783 if (!(bh2)) {
1784 brelse(bh);
1785 return retval;
1786 }
12e9b892 1787 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
ac27a0ec
DK
1788 data1 = bh2->b_data;
1789
ac27a0ec 1790 memcpy (data1, de, len);
617ba13b 1791 de = (struct ext4_dir_entry_2 *) data1;
ac27a0ec 1792 top = data1 + len;
3d0518f4 1793 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
ac27a0ec 1794 de = de2;
b0336e8d
DW
1795 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1796 (char *) de,
3d0518f4 1797 blocksize);
b0336e8d
DW
1798
1799 if (csum_size) {
1800 t = EXT4_DIRENT_TAIL(data1, blocksize);
1801 initialize_dirent_tail(t, blocksize);
1802 }
1803
ac27a0ec 1804 /* Initialize the root; the dot dirents already exist */
617ba13b 1805 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
3d0518f4
WY
1806 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1807 blocksize);
ac27a0ec
DK
1808 memset (&root->info, 0, sizeof(root->info));
1809 root->info.info_length = sizeof(root->info);
617ba13b 1810 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
ac27a0ec 1811 entries = root->entries;
af5bc92d
TT
1812 dx_set_block(entries, 1);
1813 dx_set_count(entries, 1);
1814 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
ac27a0ec
DK
1815
1816 /* Initialize as for dx_probe */
1817 hinfo.hash_version = root->info.hash_version;
f99b2589
TT
1818 if (hinfo.hash_version <= DX_HASH_TEA)
1819 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
617ba13b
MC
1820 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1821 ext4fs_dirhash(name, namelen, &hinfo);
ac27a0ec
DK
1822 frame = frames;
1823 frame->entries = entries;
1824 frame->at = entries;
1825 frame->bh = bh;
1826 bh = bh2;
6976a6f2 1827
dbe89444 1828 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
b0336e8d 1829 ext4_handle_dirty_dirent_node(handle, dir, bh);
6976a6f2 1830
ac27a0ec 1831 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
7ad8e4e6
JK
1832 if (!de) {
1833 /*
1834 * Even if the block split failed, we have to properly write
1835 * out all the changes we did so far. Otherwise we can end up
1836 * with corrupted filesystem.
1837 */
1838 ext4_mark_inode_dirty(handle, dir);
7ad8e4e6 1839 dx_release(frames);
ac27a0ec 1840 return retval;
7ad8e4e6
JK
1841 }
1842 dx_release(frames);
ac27a0ec 1843
2de770a4
TT
1844 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1845 brelse(bh);
1846 return retval;
ac27a0ec 1847}
ac27a0ec
DK
1848
1849/*
617ba13b 1850 * ext4_add_entry()
ac27a0ec
DK
1851 *
1852 * adds a file entry to the specified directory, using the same
617ba13b 1853 * semantics as ext4_find_entry(). It returns NULL if it failed.
ac27a0ec
DK
1854 *
1855 * NOTE!! The inode part of 'de' is left at 0 - which means you
1856 * may not sleep between calling this and putting something into
1857 * the entry, as someone else might have used it while you slept.
1858 */
af5bc92d
TT
1859static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1860 struct inode *inode)
ac27a0ec
DK
1861{
1862 struct inode *dir = dentry->d_parent->d_inode;
af5bc92d 1863 struct buffer_head *bh;
617ba13b 1864 struct ext4_dir_entry_2 *de;
b0336e8d 1865 struct ext4_dir_entry_tail *t;
af5bc92d 1866 struct super_block *sb;
ac27a0ec 1867 int retval;
ac27a0ec 1868 int dx_fallback=0;
ac27a0ec 1869 unsigned blocksize;
725d26d3 1870 ext4_lblk_t block, blocks;
b0336e8d
DW
1871 int csum_size = 0;
1872
1873 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1874 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1875 csum_size = sizeof(struct ext4_dir_entry_tail);
ac27a0ec
DK
1876
1877 sb = dir->i_sb;
1878 blocksize = sb->s_blocksize;
1879 if (!dentry->d_name.len)
1880 return -EINVAL;
ac27a0ec 1881 if (is_dx(dir)) {
617ba13b 1882 retval = ext4_dx_add_entry(handle, dentry, inode);
ac27a0ec
DK
1883 if (!retval || (retval != ERR_BAD_DX_DIR))
1884 return retval;
12e9b892 1885 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
ac27a0ec 1886 dx_fallback++;
617ba13b 1887 ext4_mark_inode_dirty(handle, dir);
ac27a0ec 1888 }
ac27a0ec 1889 blocks = dir->i_size >> sb->s_blocksize_bits;
498e5f24 1890 for (block = 0; block < blocks; block++) {
6d1ab10e
CM
1891 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1892 if (!retval) {
1893 retval = -EIO;
1894 ext4_error(inode->i_sb,
1895 "Directory hole detected on inode %lu\n",
1896 inode->i_ino);
1897 }
ac27a0ec 1898 return retval;
6d1ab10e 1899 }
b0336e8d
DW
1900 if (!buffer_verified(bh) &&
1901 !ext4_dirent_csum_verify(dir,
1902 (struct ext4_dir_entry *)bh->b_data))
1903 return -EIO;
1904 set_buffer_verified(bh);
ac27a0ec 1905 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
2de770a4
TT
1906 if (retval != -ENOSPC) {
1907 brelse(bh);
ac27a0ec 1908 return retval;
2de770a4 1909 }
ac27a0ec 1910
ac27a0ec 1911 if (blocks == 1 && !dx_fallback &&
1e424a34
TT
1912 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1913 return make_indexed_dir(handle, dentry, inode, bh);
ac27a0ec
DK
1914 brelse(bh);
1915 }
617ba13b 1916 bh = ext4_append(handle, dir, &block, &retval);
ac27a0ec
DK
1917 if (!bh)
1918 return retval;
617ba13b 1919 de = (struct ext4_dir_entry_2 *) bh->b_data;
ac27a0ec 1920 de->inode = 0;
b0336e8d
DW
1921 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1922
1923 if (csum_size) {
1924 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1925 initialize_dirent_tail(t, blocksize);
1926 }
1927
2de770a4
TT
1928 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1929 brelse(bh);
14ece102
FM
1930 if (retval == 0)
1931 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
2de770a4 1932 return retval;
ac27a0ec
DK
1933}
1934
ac27a0ec
DK
1935/*
1936 * Returns 0 for success, or a negative error value
1937 */
617ba13b 1938static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
ac27a0ec
DK
1939 struct inode *inode)
1940{
1941 struct dx_frame frames[2], *frame;
1942 struct dx_entry *entries, *at;
1943 struct dx_hash_info hinfo;
af5bc92d 1944 struct buffer_head *bh;
ac27a0ec 1945 struct inode *dir = dentry->d_parent->d_inode;
af5bc92d 1946 struct super_block *sb = dir->i_sb;
617ba13b 1947 struct ext4_dir_entry_2 *de;
ac27a0ec
DK
1948 int err;
1949
f702ba0f 1950 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
ac27a0ec
DK
1951 if (!frame)
1952 return err;
1953 entries = frame->entries;
1954 at = frame->at;
1955
6d1ab10e
CM
1956 if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1957 if (!err) {
1958 err = -EIO;
1959 ext4_error(dir->i_sb,
1960 "Directory hole detected on inode %lu\n",
1961 dir->i_ino);
1962 }
ac27a0ec 1963 goto cleanup;
6d1ab10e 1964 }
ac27a0ec 1965
b0336e8d
DW
1966 if (!buffer_verified(bh) &&
1967 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1968 goto journal_error;
1969 set_buffer_verified(bh);
1970
ac27a0ec 1971 BUFFER_TRACE(bh, "get_write_access");
617ba13b 1972 err = ext4_journal_get_write_access(handle, bh);
ac27a0ec
DK
1973 if (err)
1974 goto journal_error;
1975
1976 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
2de770a4 1977 if (err != -ENOSPC)
ac27a0ec 1978 goto cleanup;
ac27a0ec
DK
1979
1980 /* Block full, should compress but for now just split */
4776004f 1981 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
ac27a0ec
DK
1982 dx_get_count(entries), dx_get_limit(entries)));
1983 /* Need to split index? */
1984 if (dx_get_count(entries) == dx_get_limit(entries)) {
725d26d3 1985 ext4_lblk_t newblock;
ac27a0ec
DK
1986 unsigned icount = dx_get_count(entries);
1987 int levels = frame - frames;
1988 struct dx_entry *entries2;
1989 struct dx_node *node2;
1990 struct buffer_head *bh2;
1991
1992 if (levels && (dx_get_count(frames->entries) ==
1993 dx_get_limit(frames->entries))) {
12062ddd 1994 ext4_warning(sb, "Directory index full!");
ac27a0ec
DK
1995 err = -ENOSPC;
1996 goto cleanup;
1997 }
617ba13b 1998 bh2 = ext4_append (handle, dir, &newblock, &err);
ac27a0ec
DK
1999 if (!(bh2))
2000 goto cleanup;
2001 node2 = (struct dx_node *)(bh2->b_data);
2002 entries2 = node2->entries;
1f7bebb9 2003 memset(&node2->fake, 0, sizeof(struct fake_dirent));
3d0518f4
WY
2004 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2005 sb->s_blocksize);
ac27a0ec 2006 BUFFER_TRACE(frame->bh, "get_write_access");
617ba13b 2007 err = ext4_journal_get_write_access(handle, frame->bh);
ac27a0ec
DK
2008 if (err)
2009 goto journal_error;
2010 if (levels) {
2011 unsigned icount1 = icount/2, icount2 = icount - icount1;
2012 unsigned hash2 = dx_get_hash(entries + icount1);
4776004f
TT
2013 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2014 icount1, icount2));
ac27a0ec
DK
2015
2016 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
617ba13b 2017 err = ext4_journal_get_write_access(handle,
ac27a0ec
DK
2018 frames[0].bh);
2019 if (err)
2020 goto journal_error;
2021
af5bc92d
TT
2022 memcpy((char *) entries2, (char *) (entries + icount1),
2023 icount2 * sizeof(struct dx_entry));
2024 dx_set_count(entries, icount1);
2025 dx_set_count(entries2, icount2);
2026 dx_set_limit(entries2, dx_node_limit(dir));
ac27a0ec
DK
2027
2028 /* Which index block gets the new entry? */
2029 if (at - entries >= icount1) {
2030 frame->at = at = at - entries - icount1 + entries2;
2031 frame->entries = entries = entries2;
2032 swap(frame->bh, bh2);
2033 }
af5bc92d
TT
2034 dx_insert_block(frames + 0, hash2, newblock);
2035 dxtrace(dx_show_index("node", frames[1].entries));
2036 dxtrace(dx_show_index("node",
ac27a0ec 2037 ((struct dx_node *) bh2->b_data)->entries));
dbe89444 2038 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
ac27a0ec
DK
2039 if (err)
2040 goto journal_error;
2041 brelse (bh2);
2042 } else {
4776004f
TT
2043 dxtrace(printk(KERN_DEBUG
2044 "Creating second level index...\n"));
ac27a0ec
DK
2045 memcpy((char *) entries2, (char *) entries,
2046 icount * sizeof(struct dx_entry));
2047 dx_set_limit(entries2, dx_node_limit(dir));
2048
2049 /* Set up root */
2050 dx_set_count(entries, 1);
2051 dx_set_block(entries + 0, newblock);
2052 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2053
2054 /* Add new access path frame */
2055 frame = frames + 1;
2056 frame->at = at = at - entries + entries2;
2057 frame->entries = entries = entries2;
2058 frame->bh = bh2;
617ba13b 2059 err = ext4_journal_get_write_access(handle,
ac27a0ec
DK
2060 frame->bh);
2061 if (err)
2062 goto journal_error;
2063 }
dbe89444 2064 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
b4097142
TT
2065 if (err) {
2066 ext4_std_error(inode->i_sb, err);
2067 goto cleanup;
2068 }
ac27a0ec
DK
2069 }
2070 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2071 if (!de)
2072 goto cleanup;
2073 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
ac27a0ec
DK
2074 goto cleanup;
2075
2076journal_error:
617ba13b 2077 ext4_std_error(dir->i_sb, err);
ac27a0ec
DK
2078cleanup:
2079 if (bh)
2080 brelse(bh);
2081 dx_release(frames);
2082 return err;
2083}
ac27a0ec
DK
2084
2085/*
617ba13b 2086 * ext4_delete_entry deletes a directory entry by merging it with the
ac27a0ec
DK
2087 * previous entry
2088 */
af5bc92d
TT
2089static int ext4_delete_entry(handle_t *handle,
2090 struct inode *dir,
2091 struct ext4_dir_entry_2 *de_del,
2092 struct buffer_head *bh)
ac27a0ec 2093{
af5bc92d 2094 struct ext4_dir_entry_2 *de, *pde;
3d0518f4 2095 unsigned int blocksize = dir->i_sb->s_blocksize;
b0336e8d 2096 int csum_size = 0;
b4097142 2097 int i, err;
ac27a0ec 2098
b0336e8d
DW
2099 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2100 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2101 csum_size = sizeof(struct ext4_dir_entry_tail);
2102
ac27a0ec
DK
2103 i = 0;
2104 pde = NULL;
617ba13b 2105 de = (struct ext4_dir_entry_2 *) bh->b_data;
b0336e8d 2106 while (i < bh->b_size - csum_size) {
226ba972
TM
2107 if (ext4_check_dir_entry(dir, NULL, de, bh,
2108 bh->b_data, bh->b_size, i))
ac27a0ec
DK
2109 return -EIO;
2110 if (de == de_del) {
2111 BUFFER_TRACE(bh, "get_write_access");
b4097142
TT
2112 err = ext4_journal_get_write_access(handle, bh);
2113 if (unlikely(err)) {
2114 ext4_std_error(dir->i_sb, err);
2115 return err;
2116 }
ac27a0ec 2117 if (pde)
a72d7f83 2118 pde->rec_len = ext4_rec_len_to_disk(
3d0518f4
WY
2119 ext4_rec_len_from_disk(pde->rec_len,
2120 blocksize) +
2121 ext4_rec_len_from_disk(de->rec_len,
2122 blocksize),
2123 blocksize);
ac27a0ec
DK
2124 else
2125 de->inode = 0;
2126 dir->i_version++;
0390131b 2127 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
b0336e8d 2128 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
b4097142
TT
2129 if (unlikely(err)) {
2130 ext4_std_error(dir->i_sb, err);
2131 return err;
2132 }
ac27a0ec
DK
2133 return 0;
2134 }
3d0518f4 2135 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
ac27a0ec 2136 pde = de;
3d0518f4 2137 de = ext4_next_entry(de, blocksize);
ac27a0ec
DK
2138 }
2139 return -ENOENT;
2140}
2141
f8628a14
AD
2142/*
2143 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2144 * since this indicates that nlinks count was previously 1.
2145 */
2146static void ext4_inc_count(handle_t *handle, struct inode *inode)
2147{
2148 inc_nlink(inode);
2149 if (is_dx(inode) && inode->i_nlink > 1) {
2150 /* limit is 16-bit i_links_count */
2151 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
bfe86848 2152 set_nlink(inode, 1);
f8628a14
AD
2153 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2154 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2155 }
2156 }
2157}
2158
2159/*
2160 * If a directory had nlink == 1, then we should let it be 1. This indicates
2161 * directory has >EXT4_LINK_MAX subdirs.
2162 */
2163static void ext4_dec_count(handle_t *handle, struct inode *inode)
2164{
909a4cf1
AD
2165 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2166 drop_nlink(inode);
f8628a14
AD
2167}
2168
2169
617ba13b 2170static int ext4_add_nondir(handle_t *handle,
ac27a0ec
DK
2171 struct dentry *dentry, struct inode *inode)
2172{
617ba13b 2173 int err = ext4_add_entry(handle, dentry, inode);
ac27a0ec 2174 if (!err) {
617ba13b 2175 ext4_mark_inode_dirty(handle, inode);
6b38e842 2176 unlock_new_inode(inode);
8fc37ec5 2177 d_instantiate(dentry, inode);
ac27a0ec
DK
2178 return 0;
2179 }
731b9a54 2180 drop_nlink(inode);
6b38e842 2181 unlock_new_inode(inode);
ac27a0ec
DK
2182 iput(inode);
2183 return err;
2184}
2185
2186/*
2187 * By the time this is called, we already have created
2188 * the directory cache entry for the new file, but it
2189 * is so far negative - it has no inode.
2190 *
2191 * If the create succeeds, we fill in the inode information
2192 * with d_instantiate().
2193 */
4acdaf27 2194static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 2195 bool excl)
ac27a0ec
DK
2196{
2197 handle_t *handle;
af5bc92d 2198 struct inode *inode;
ac27a0ec
DK
2199 int err, retries = 0;
2200
871a2931 2201 dquot_initialize(dir);
907f4554 2202
ac27a0ec 2203retry:
617ba13b
MC
2204 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2205 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
5aca07eb 2206 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
ac27a0ec
DK
2207 if (IS_ERR(handle))
2208 return PTR_ERR(handle);
2209
2210 if (IS_DIRSYNC(dir))
0390131b 2211 ext4_handle_sync(handle);
ac27a0ec 2212
5cb81dab 2213 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
ac27a0ec
DK
2214 err = PTR_ERR(inode);
2215 if (!IS_ERR(inode)) {
617ba13b
MC
2216 inode->i_op = &ext4_file_inode_operations;
2217 inode->i_fop = &ext4_file_operations;
2218 ext4_set_aops(inode);
2219 err = ext4_add_nondir(handle, dentry, inode);
ac27a0ec 2220 }
617ba13b
MC
2221 ext4_journal_stop(handle);
2222 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
ac27a0ec
DK
2223 goto retry;
2224 return err;
2225}
2226
af5bc92d 2227static int ext4_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 2228 umode_t mode, dev_t rdev)
ac27a0ec
DK
2229{
2230 handle_t *handle;
2231 struct inode *inode;
2232 int err, retries = 0;
2233
2234 if (!new_valid_dev(rdev))
2235 return -EINVAL;
2236
871a2931 2237 dquot_initialize(dir);
907f4554 2238
ac27a0ec 2239retry:
617ba13b
MC
2240 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2241 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
5aca07eb 2242 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
ac27a0ec
DK
2243 if (IS_ERR(handle))
2244 return PTR_ERR(handle);
2245
2246 if (IS_DIRSYNC(dir))
0390131b 2247 ext4_handle_sync(handle);
ac27a0ec 2248
5cb81dab 2249 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
ac27a0ec
DK
2250 err = PTR_ERR(inode);
2251 if (!IS_ERR(inode)) {
2252 init_special_inode(inode, inode->i_mode, rdev);
617ba13b 2253 inode->i_op = &ext4_special_inode_operations;
617ba13b 2254 err = ext4_add_nondir(handle, dentry, inode);
ac27a0ec 2255 }
617ba13b
MC
2256 ext4_journal_stop(handle);
2257 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
ac27a0ec
DK
2258 goto retry;
2259 return err;
2260}
2261
a774f9c2
TM
2262struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2263 struct ext4_dir_entry_2 *de,
2264 int blocksize, int csum_size,
2265 unsigned int parent_ino, int dotdot_real_len)
2266{
2267 de->inode = cpu_to_le32(inode->i_ino);
2268 de->name_len = 1;
2269 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2270 blocksize);
2271 strcpy(de->name, ".");
2272 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2273
2274 de = ext4_next_entry(de, blocksize);
2275 de->inode = cpu_to_le32(parent_ino);
2276 de->name_len = 2;
2277 if (!dotdot_real_len)
2278 de->rec_len = ext4_rec_len_to_disk(blocksize -
2279 (csum_size + EXT4_DIR_REC_LEN(1)),
2280 blocksize);
2281 else
2282 de->rec_len = ext4_rec_len_to_disk(
2283 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2284 strcpy(de->name, "..");
2285 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2286
2287 return ext4_next_entry(de, blocksize);
2288}
2289
2290static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2291 struct inode *inode)
ac27a0ec 2292{
dabd991f 2293 struct buffer_head *dir_block = NULL;
af5bc92d 2294 struct ext4_dir_entry_2 *de;
b0336e8d 2295 struct ext4_dir_entry_tail *t;
3d0518f4 2296 unsigned int blocksize = dir->i_sb->s_blocksize;
b0336e8d 2297 int csum_size = 0;
a774f9c2 2298 int err;
ac27a0ec 2299
b0336e8d
DW
2300 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2301 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2302 csum_size = sizeof(struct ext4_dir_entry_tail);
2303
a774f9c2
TM
2304 inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2305 dir_block = ext4_bread(handle, inode, 0, 1, &err);
2306 if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2307 if (!err) {
2308 err = -EIO;
2309 ext4_error(inode->i_sb,
2310 "Directory hole detected on inode %lu\n",
2311 inode->i_ino);
2312 }
2313 goto out;
2314 }
2315 BUFFER_TRACE(dir_block, "get_write_access");
2316 err = ext4_journal_get_write_access(handle, dir_block);
2317 if (err)
2318 goto out;
2319 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2320 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2321 set_nlink(inode, 2);
2322 if (csum_size) {
2323 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2324 initialize_dirent_tail(t, blocksize);
2325 }
2326
2327 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2328 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2329 if (err)
2330 goto out;
2331 set_buffer_verified(dir_block);
2332out:
2333 brelse(dir_block);
2334 return err;
2335}
2336
2337static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2338{
2339 handle_t *handle;
2340 struct inode *inode;
2341 int err, retries = 0;
2342
f8628a14 2343 if (EXT4_DIR_LINK_MAX(dir))
ac27a0ec
DK
2344 return -EMLINK;
2345
871a2931 2346 dquot_initialize(dir);
907f4554 2347
ac27a0ec 2348retry:
617ba13b
MC
2349 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2350 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
5aca07eb 2351 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
ac27a0ec
DK
2352 if (IS_ERR(handle))
2353 return PTR_ERR(handle);
2354
2355 if (IS_DIRSYNC(dir))
0390131b 2356 ext4_handle_sync(handle);
ac27a0ec 2357
11013911 2358 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
5cb81dab 2359 &dentry->d_name, 0, NULL);
ac27a0ec
DK
2360 err = PTR_ERR(inode);
2361 if (IS_ERR(inode))
2362 goto out_stop;
2363
617ba13b
MC
2364 inode->i_op = &ext4_dir_inode_operations;
2365 inode->i_fop = &ext4_dir_operations;
a774f9c2 2366 err = ext4_init_new_dir(handle, dir, inode);
dabd991f
NK
2367 if (err)
2368 goto out_clear_inode;
2369 err = ext4_mark_inode_dirty(handle, inode);
2370 if (!err)
2371 err = ext4_add_entry(handle, dentry, inode);
ac27a0ec 2372 if (err) {
4cdeed86
AK
2373out_clear_inode:
2374 clear_nlink(inode);
6b38e842 2375 unlock_new_inode(inode);
617ba13b 2376 ext4_mark_inode_dirty(handle, inode);
af5bc92d 2377 iput(inode);
ac27a0ec
DK
2378 goto out_stop;
2379 }
f8628a14 2380 ext4_inc_count(handle, dir);
617ba13b 2381 ext4_update_dx_flag(dir);
dabd991f
NK
2382 err = ext4_mark_inode_dirty(handle, dir);
2383 if (err)
2384 goto out_clear_inode;
6b38e842 2385 unlock_new_inode(inode);
8fc37ec5 2386 d_instantiate(dentry, inode);
ac27a0ec 2387out_stop:
617ba13b
MC
2388 ext4_journal_stop(handle);
2389 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
ac27a0ec
DK
2390 goto retry;
2391 return err;
2392}
2393
2394/*
2395 * routine to check that the specified directory is empty (for rmdir)
2396 */
af5bc92d 2397static int empty_dir(struct inode *inode)
ac27a0ec 2398{
498e5f24 2399 unsigned int offset;
af5bc92d
TT
2400 struct buffer_head *bh;
2401 struct ext4_dir_entry_2 *de, *de1;
2402 struct super_block *sb;
ac27a0ec
DK
2403 int err = 0;
2404
2405 sb = inode->i_sb;
617ba13b 2406 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
af5bc92d 2407 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
ac27a0ec 2408 if (err)
24676da4
TT
2409 EXT4_ERROR_INODE(inode,
2410 "error %d reading directory lblock 0", err);
ac27a0ec 2411 else
12062ddd 2412 ext4_warning(inode->i_sb,
ac27a0ec
DK
2413 "bad directory (dir #%lu) - no data block",
2414 inode->i_ino);
2415 return 1;
2416 }
b0336e8d
DW
2417 if (!buffer_verified(bh) &&
2418 !ext4_dirent_csum_verify(inode,
2419 (struct ext4_dir_entry *)bh->b_data)) {
2420 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2421 "lblock 0");
2422 return -EIO;
2423 }
2424 set_buffer_verified(bh);
617ba13b 2425 de = (struct ext4_dir_entry_2 *) bh->b_data;
3d0518f4 2426 de1 = ext4_next_entry(de, sb->s_blocksize);
ac27a0ec
DK
2427 if (le32_to_cpu(de->inode) != inode->i_ino ||
2428 !le32_to_cpu(de1->inode) ||
af5bc92d
TT
2429 strcmp(".", de->name) ||
2430 strcmp("..", de1->name)) {
12062ddd 2431 ext4_warning(inode->i_sb,
af5bc92d
TT
2432 "bad directory (dir #%lu) - no `.' or `..'",
2433 inode->i_ino);
2434 brelse(bh);
ac27a0ec
DK
2435 return 1;
2436 }
3d0518f4
WY
2437 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2438 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2439 de = ext4_next_entry(de1, sb->s_blocksize);
af5bc92d 2440 while (offset < inode->i_size) {
ac27a0ec 2441 if (!bh ||
24676da4
TT
2442 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2443 unsigned int lblock;
ac27a0ec 2444 err = 0;
af5bc92d 2445 brelse(bh);
24676da4
TT
2446 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2447 bh = ext4_bread(NULL, inode, lblock, 0, &err);
ac27a0ec
DK
2448 if (!bh) {
2449 if (err)
24676da4
TT
2450 EXT4_ERROR_INODE(inode,
2451 "error %d reading directory "
2452 "lblock %u", err, lblock);
6d1ab10e
CM
2453 else
2454 ext4_warning(inode->i_sb,
2455 "bad directory (dir #%lu) - no data block",
2456 inode->i_ino);
2457
ac27a0ec
DK
2458 offset += sb->s_blocksize;
2459 continue;
2460 }
b0336e8d
DW
2461 if (!buffer_verified(bh) &&
2462 !ext4_dirent_csum_verify(inode,
2463 (struct ext4_dir_entry *)bh->b_data)) {
2464 EXT4_ERROR_INODE(inode, "checksum error "
2465 "reading directory lblock 0");
2466 return -EIO;
2467 }
2468 set_buffer_verified(bh);
617ba13b 2469 de = (struct ext4_dir_entry_2 *) bh->b_data;
ac27a0ec 2470 }
226ba972
TM
2471 if (ext4_check_dir_entry(inode, NULL, de, bh,
2472 bh->b_data, bh->b_size, offset)) {
617ba13b 2473 de = (struct ext4_dir_entry_2 *)(bh->b_data +
ac27a0ec
DK
2474 sb->s_blocksize);
2475 offset = (offset | (sb->s_blocksize - 1)) + 1;
2476 continue;
2477 }
2478 if (le32_to_cpu(de->inode)) {
af5bc92d 2479 brelse(bh);
ac27a0ec
DK
2480 return 0;
2481 }
3d0518f4
WY
2482 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2483 de = ext4_next_entry(de, sb->s_blocksize);
ac27a0ec 2484 }
af5bc92d 2485 brelse(bh);
ac27a0ec
DK
2486 return 1;
2487}
2488
617ba13b 2489/* ext4_orphan_add() links an unlinked or truncated inode into a list of
ac27a0ec
DK
2490 * such inodes, starting at the superblock, in case we crash before the
2491 * file is closed/deleted, or in case the inode truncate spans multiple
2492 * transactions and the last transaction is not recovered after a crash.
2493 *
2494 * At filesystem recovery time, we walk this list deleting unlinked
617ba13b 2495 * inodes and truncating linked inodes in ext4_orphan_cleanup().
ac27a0ec 2496 */
617ba13b 2497int ext4_orphan_add(handle_t *handle, struct inode *inode)
ac27a0ec
DK
2498{
2499 struct super_block *sb = inode->i_sb;
617ba13b 2500 struct ext4_iloc iloc;
ac27a0ec
DK
2501 int err = 0, rc;
2502
c9b92530 2503 if (!EXT4_SB(sb)->s_journal)
0390131b
FM
2504 return 0;
2505
3b9d4ed2 2506 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
617ba13b 2507 if (!list_empty(&EXT4_I(inode)->i_orphan))
ac27a0ec
DK
2508 goto out_unlock;
2509
afb86178
LC
2510 /*
2511 * Orphan handling is only valid for files with data blocks
2512 * being truncated, or files being unlinked. Note that we either
2513 * hold i_mutex, or the inode can not be referenced from outside,
2514 * so i_nlink should not be bumped due to race
ac27a0ec 2515 */
af5bc92d
TT
2516 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2517 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
ac27a0ec 2518
617ba13b
MC
2519 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2520 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
ac27a0ec
DK
2521 if (err)
2522 goto out_unlock;
2523
617ba13b 2524 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec
DK
2525 if (err)
2526 goto out_unlock;
6e3617e5
DM
2527 /*
2528 * Due to previous errors inode may be already a part of on-disk
2529 * orphan list. If so skip on-disk list modification.
2530 */
2531 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2532 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2533 goto mem_insert;
ac27a0ec
DK
2534
2535 /* Insert this inode at the head of the on-disk orphan list... */
617ba13b
MC
2536 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2537 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
b50924c2 2538 err = ext4_handle_dirty_super(handle, sb);
617ba13b 2539 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec
DK
2540 if (!err)
2541 err = rc;
2542
2543 /* Only add to the head of the in-memory list if all the
2544 * previous operations succeeded. If the orphan_add is going to
2545 * fail (possibly taking the journal offline), we can't risk
2546 * leaving the inode on the orphan list: stray orphan-list
2547 * entries can cause panics at unmount time.
2548 *
2549 * This is safe: on error we're going to ignore the orphan list
2550 * anyway on the next recovery. */
6e3617e5 2551mem_insert:
ac27a0ec 2552 if (!err)
617ba13b 2553 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
ac27a0ec
DK
2554
2555 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2556 jbd_debug(4, "orphan inode %lu will point to %d\n",
2557 inode->i_ino, NEXT_ORPHAN(inode));
2558out_unlock:
3b9d4ed2 2559 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
617ba13b 2560 ext4_std_error(inode->i_sb, err);
ac27a0ec
DK
2561 return err;
2562}
2563
2564/*
617ba13b 2565 * ext4_orphan_del() removes an unlinked or truncated inode from the list
ac27a0ec
DK
2566 * of such inodes stored on disk, because it is finally being cleaned up.
2567 */
617ba13b 2568int ext4_orphan_del(handle_t *handle, struct inode *inode)
ac27a0ec
DK
2569{
2570 struct list_head *prev;
617ba13b
MC
2571 struct ext4_inode_info *ei = EXT4_I(inode);
2572 struct ext4_sb_info *sbi;
498e5f24 2573 __u32 ino_next;
617ba13b 2574 struct ext4_iloc iloc;
ac27a0ec
DK
2575 int err = 0;
2576
c9b92530 2577 if (!EXT4_SB(inode->i_sb)->s_journal)
0390131b
FM
2578 return 0;
2579
3b9d4ed2
TT
2580 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2581 if (list_empty(&ei->i_orphan))
2582 goto out;
ac27a0ec
DK
2583
2584 ino_next = NEXT_ORPHAN(inode);
2585 prev = ei->i_orphan.prev;
617ba13b 2586 sbi = EXT4_SB(inode->i_sb);
ac27a0ec
DK
2587
2588 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2589
2590 list_del_init(&ei->i_orphan);
2591
2592 /* If we're on an error path, we may not have a valid
2593 * transaction handle with which to update the orphan list on
2594 * disk, but we still need to remove the inode from the linked
2595 * list in memory. */
c9b92530 2596 if (!handle)
ac27a0ec
DK
2597 goto out;
2598
617ba13b 2599 err = ext4_reserve_inode_write(handle, inode, &iloc);
ac27a0ec
DK
2600 if (err)
2601 goto out_err;
2602
2603 if (prev == &sbi->s_orphan) {
498e5f24 2604 jbd_debug(4, "superblock will point to %u\n", ino_next);
ac27a0ec 2605 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
617ba13b 2606 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
ac27a0ec
DK
2607 if (err)
2608 goto out_brelse;
2609 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
b50924c2 2610 err = ext4_handle_dirty_super(handle, inode->i_sb);
ac27a0ec 2611 } else {
617ba13b 2612 struct ext4_iloc iloc2;
ac27a0ec 2613 struct inode *i_prev =
617ba13b 2614 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec 2615
498e5f24 2616 jbd_debug(4, "orphan inode %lu will point to %u\n",
ac27a0ec 2617 i_prev->i_ino, ino_next);
617ba13b 2618 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
ac27a0ec
DK
2619 if (err)
2620 goto out_brelse;
2621 NEXT_ORPHAN(i_prev) = ino_next;
617ba13b 2622 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
ac27a0ec
DK
2623 }
2624 if (err)
2625 goto out_brelse;
2626 NEXT_ORPHAN(inode) = 0;
617ba13b 2627 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
ac27a0ec
DK
2628
2629out_err:
617ba13b 2630 ext4_std_error(inode->i_sb, err);
ac27a0ec 2631out:
3b9d4ed2 2632 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
ac27a0ec
DK
2633 return err;
2634
2635out_brelse:
2636 brelse(iloc.bh);
2637 goto out_err;
2638}
2639
af5bc92d 2640static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
ac27a0ec
DK
2641{
2642 int retval;
af5bc92d
TT
2643 struct inode *inode;
2644 struct buffer_head *bh;
2645 struct ext4_dir_entry_2 *de;
ac27a0ec
DK
2646 handle_t *handle;
2647
2648 /* Initialize quotas before so that eventual writes go in
2649 * separate transaction */
871a2931
CH
2650 dquot_initialize(dir);
2651 dquot_initialize(dentry->d_inode);
907f4554 2652
617ba13b 2653 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
ac27a0ec
DK
2654 if (IS_ERR(handle))
2655 return PTR_ERR(handle);
2656
2657 retval = -ENOENT;
f702ba0f 2658 bh = ext4_find_entry(dir, &dentry->d_name, &de);
ac27a0ec
DK
2659 if (!bh)
2660 goto end_rmdir;
2661
2662 if (IS_DIRSYNC(dir))
0390131b 2663 ext4_handle_sync(handle);
ac27a0ec
DK
2664
2665 inode = dentry->d_inode;
2666
2667 retval = -EIO;
2668 if (le32_to_cpu(de->inode) != inode->i_ino)
2669 goto end_rmdir;
2670
2671 retval = -ENOTEMPTY;
af5bc92d 2672 if (!empty_dir(inode))
ac27a0ec
DK
2673 goto end_rmdir;
2674
617ba13b 2675 retval = ext4_delete_entry(handle, dir, de, bh);
ac27a0ec
DK
2676 if (retval)
2677 goto end_rmdir;
f8628a14 2678 if (!EXT4_DIR_LINK_EMPTY(inode))
12062ddd 2679 ext4_warning(inode->i_sb,
af5bc92d
TT
2680 "empty directory has too many links (%d)",
2681 inode->i_nlink);
ac27a0ec
DK
2682 inode->i_version++;
2683 clear_nlink(inode);
2684 /* There's no need to set i_disksize: the fact that i_nlink is
2685 * zero will ensure that the right thing happens during any
2686 * recovery. */
2687 inode->i_size = 0;
617ba13b 2688 ext4_orphan_add(handle, inode);
ef7f3835 2689 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
617ba13b 2690 ext4_mark_inode_dirty(handle, inode);
f8628a14 2691 ext4_dec_count(handle, dir);
617ba13b
MC
2692 ext4_update_dx_flag(dir);
2693 ext4_mark_inode_dirty(handle, dir);
ac27a0ec
DK
2694
2695end_rmdir:
617ba13b 2696 ext4_journal_stop(handle);
af5bc92d 2697 brelse(bh);
ac27a0ec
DK
2698 return retval;
2699}
2700
af5bc92d 2701static int ext4_unlink(struct inode *dir, struct dentry *dentry)
ac27a0ec
DK
2702{
2703 int retval;
af5bc92d
TT
2704 struct inode *inode;
2705 struct buffer_head *bh;
2706 struct ext4_dir_entry_2 *de;
ac27a0ec
DK
2707 handle_t *handle;
2708
0562e0ba 2709 trace_ext4_unlink_enter(dir, dentry);
ac27a0ec
DK
2710 /* Initialize quotas before so that eventual writes go
2711 * in separate transaction */
871a2931
CH
2712 dquot_initialize(dir);
2713 dquot_initialize(dentry->d_inode);
907f4554 2714
617ba13b 2715 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
ac27a0ec
DK
2716 if (IS_ERR(handle))
2717 return PTR_ERR(handle);
2718
2719 if (IS_DIRSYNC(dir))
0390131b 2720 ext4_handle_sync(handle);
ac27a0ec
DK
2721
2722 retval = -ENOENT;
f702ba0f 2723 bh = ext4_find_entry(dir, &dentry->d_name, &de);
ac27a0ec
DK
2724 if (!bh)
2725 goto end_unlink;
2726
2727 inode = dentry->d_inode;
2728
2729 retval = -EIO;
2730 if (le32_to_cpu(de->inode) != inode->i_ino)
2731 goto end_unlink;
2732
2733 if (!inode->i_nlink) {
12062ddd 2734 ext4_warning(inode->i_sb,
af5bc92d
TT
2735 "Deleting nonexistent file (%lu), %d",
2736 inode->i_ino, inode->i_nlink);
bfe86848 2737 set_nlink(inode, 1);
ac27a0ec 2738 }
617ba13b 2739 retval = ext4_delete_entry(handle, dir, de, bh);
ac27a0ec
DK
2740 if (retval)
2741 goto end_unlink;
ef7f3835 2742 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
617ba13b
MC
2743 ext4_update_dx_flag(dir);
2744 ext4_mark_inode_dirty(handle, dir);
825f1481 2745 drop_nlink(inode);
ac27a0ec 2746 if (!inode->i_nlink)
617ba13b 2747 ext4_orphan_add(handle, inode);
ef7f3835 2748 inode->i_ctime = ext4_current_time(inode);
617ba13b 2749 ext4_mark_inode_dirty(handle, inode);
ac27a0ec
DK
2750 retval = 0;
2751
2752end_unlink:
617ba13b 2753 ext4_journal_stop(handle);
af5bc92d 2754 brelse(bh);
0562e0ba 2755 trace_ext4_unlink_exit(dentry, retval);
ac27a0ec
DK
2756 return retval;
2757}
2758
af5bc92d
TT
2759static int ext4_symlink(struct inode *dir,
2760 struct dentry *dentry, const char *symname)
ac27a0ec
DK
2761{
2762 handle_t *handle;
af5bc92d 2763 struct inode *inode;
ac27a0ec 2764 int l, err, retries = 0;
df5e6223 2765 int credits;
ac27a0ec
DK
2766
2767 l = strlen(symname)+1;
2768 if (l > dir->i_sb->s_blocksize)
2769 return -ENAMETOOLONG;
2770
871a2931 2771 dquot_initialize(dir);
907f4554 2772
df5e6223
JK
2773 if (l > EXT4_N_BLOCKS * 4) {
2774 /*
2775 * For non-fast symlinks, we just allocate inode and put it on
2776 * orphan list in the first transaction => we need bitmap,
8c208719
ES
2777 * group descriptor, sb, inode block, quota blocks, and
2778 * possibly selinux xattr blocks.
df5e6223 2779 */
8c208719
ES
2780 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2781 EXT4_XATTR_TRANS_BLOCKS;
df5e6223
JK
2782 } else {
2783 /*
2784 * Fast symlink. We have to add entry to directory
2785 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2786 * allocate new inode (bitmap, group descriptor, inode block,
2787 * quota blocks, sb is already counted in previous macros).
2788 */
2789 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2790 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2791 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2792 }
ac27a0ec 2793retry:
df5e6223 2794 handle = ext4_journal_start(dir, credits);
ac27a0ec
DK
2795 if (IS_ERR(handle))
2796 return PTR_ERR(handle);
2797
2798 if (IS_DIRSYNC(dir))
0390131b 2799 ext4_handle_sync(handle);
ac27a0ec 2800
11013911 2801 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
5cb81dab 2802 &dentry->d_name, 0, NULL);
ac27a0ec
DK
2803 err = PTR_ERR(inode);
2804 if (IS_ERR(inode))
2805 goto out_stop;
2806
df5e6223 2807 if (l > EXT4_N_BLOCKS * 4) {
617ba13b
MC
2808 inode->i_op = &ext4_symlink_inode_operations;
2809 ext4_set_aops(inode);
ac27a0ec 2810 /*
df5e6223
JK
2811 * We cannot call page_symlink() with transaction started
2812 * because it calls into ext4_write_begin() which can wait
2813 * for transaction commit if we are running out of space
2814 * and thus we deadlock. So we have to stop transaction now
2815 * and restart it when symlink contents is written.
2816 *
2817 * To keep fs consistent in case of crash, we have to put inode
2818 * to orphan list in the mean time.
ac27a0ec 2819 */
df5e6223
JK
2820 drop_nlink(inode);
2821 err = ext4_orphan_add(handle, inode);
2822 ext4_journal_stop(handle);
2823 if (err)
2824 goto err_drop_inode;
54566b2c 2825 err = __page_symlink(inode, symname, l, 1);
df5e6223
JK
2826 if (err)
2827 goto err_drop_inode;
2828 /*
2829 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2830 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2831 */
2832 handle = ext4_journal_start(dir,
2833 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2834 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2835 if (IS_ERR(handle)) {
2836 err = PTR_ERR(handle);
2837 goto err_drop_inode;
2838 }
0ce8c010 2839 set_nlink(inode, 1);
df5e6223 2840 err = ext4_orphan_del(handle, inode);
ac27a0ec 2841 if (err) {
df5e6223 2842 ext4_journal_stop(handle);
825f1481 2843 clear_nlink(inode);
df5e6223 2844 goto err_drop_inode;
ac27a0ec
DK
2845 }
2846 } else {
e65187e6 2847 /* clear the extent format for fast symlink */
12e9b892 2848 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
617ba13b 2849 inode->i_op = &ext4_fast_symlink_inode_operations;
af5bc92d 2850 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
ac27a0ec
DK
2851 inode->i_size = l-1;
2852 }
617ba13b
MC
2853 EXT4_I(inode)->i_disksize = inode->i_size;
2854 err = ext4_add_nondir(handle, dentry, inode);
ac27a0ec 2855out_stop:
617ba13b
MC
2856 ext4_journal_stop(handle);
2857 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
ac27a0ec
DK
2858 goto retry;
2859 return err;
df5e6223
JK
2860err_drop_inode:
2861 unlock_new_inode(inode);
2862 iput(inode);
2863 return err;
ac27a0ec
DK
2864}
2865
af5bc92d
TT
2866static int ext4_link(struct dentry *old_dentry,
2867 struct inode *dir, struct dentry *dentry)
ac27a0ec
DK
2868{
2869 handle_t *handle;
2870 struct inode *inode = old_dentry->d_inode;
2871 int err, retries = 0;
2872
b05ab1dc 2873 if (inode->i_nlink >= EXT4_LINK_MAX)
ac27a0ec 2874 return -EMLINK;
f8628a14 2875
871a2931 2876 dquot_initialize(dir);
907f4554 2877
ac27a0ec 2878retry:
617ba13b
MC
2879 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2880 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
ac27a0ec
DK
2881 if (IS_ERR(handle))
2882 return PTR_ERR(handle);
2883
2884 if (IS_DIRSYNC(dir))
0390131b 2885 ext4_handle_sync(handle);
ac27a0ec 2886
ef7f3835 2887 inode->i_ctime = ext4_current_time(inode);
f8628a14 2888 ext4_inc_count(handle, inode);
7de9c6ee 2889 ihold(inode);
ac27a0ec 2890
6b38e842
AV
2891 err = ext4_add_entry(handle, dentry, inode);
2892 if (!err) {
2893 ext4_mark_inode_dirty(handle, inode);
2894 d_instantiate(dentry, inode);
2895 } else {
2896 drop_nlink(inode);
2897 iput(inode);
2898 }
617ba13b
MC
2899 ext4_journal_stop(handle);
2900 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
ac27a0ec
DK
2901 goto retry;
2902 return err;
2903}
2904
3d0518f4
WY
2905#define PARENT_INO(buffer, size) \
2906 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
ac27a0ec
DK
2907
2908/*
2909 * Anybody can rename anything with this: the permission checks are left to the
2910 * higher-level routines.
2911 */
af5bc92d
TT
2912static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2913 struct inode *new_dir, struct dentry *new_dentry)
ac27a0ec
DK
2914{
2915 handle_t *handle;
af5bc92d
TT
2916 struct inode *old_inode, *new_inode;
2917 struct buffer_head *old_bh, *new_bh, *dir_bh;
2918 struct ext4_dir_entry_2 *old_de, *new_de;
8750c6d5 2919 int retval, force_da_alloc = 0;
ac27a0ec 2920
871a2931
CH
2921 dquot_initialize(old_dir);
2922 dquot_initialize(new_dir);
907f4554 2923
ac27a0ec
DK
2924 old_bh = new_bh = dir_bh = NULL;
2925
2926 /* Initialize quotas before so that eventual writes go
2927 * in separate transaction */
2928 if (new_dentry->d_inode)
871a2931 2929 dquot_initialize(new_dentry->d_inode);
617ba13b
MC
2930 handle = ext4_journal_start(old_dir, 2 *
2931 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2932 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
ac27a0ec
DK
2933 if (IS_ERR(handle))
2934 return PTR_ERR(handle);
2935
2936 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
0390131b 2937 ext4_handle_sync(handle);
ac27a0ec 2938
f702ba0f 2939 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
ac27a0ec
DK
2940 /*
2941 * Check for inode number is _not_ due to possible IO errors.
2942 * We might rmdir the source, keep it as pwd of some process
2943 * and merrily kill the link to whatever was created under the
2944 * same name. Goodbye sticky bit ;-<
2945 */
2946 old_inode = old_dentry->d_inode;
2947 retval = -ENOENT;
2948 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2949 goto end_rename;
2950
2951 new_inode = new_dentry->d_inode;
f702ba0f 2952 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
ac27a0ec
DK
2953 if (new_bh) {
2954 if (!new_inode) {
af5bc92d 2955 brelse(new_bh);
ac27a0ec
DK
2956 new_bh = NULL;
2957 }
2958 }
2959 if (S_ISDIR(old_inode->i_mode)) {
2960 if (new_inode) {
2961 retval = -ENOTEMPTY;
af5bc92d 2962 if (!empty_dir(new_inode))
ac27a0ec
DK
2963 goto end_rename;
2964 }
2965 retval = -EIO;
6d1ab10e
CM
2966 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
2967 if (!retval) {
2968 retval = -EIO;
2969 ext4_error(old_inode->i_sb,
2970 "Directory hole detected on inode %lu\n",
2971 old_inode->i_ino);
2972 }
ac27a0ec 2973 goto end_rename;
6d1ab10e 2974 }
b0336e8d
DW
2975 if (!buffer_verified(dir_bh) &&
2976 !ext4_dirent_csum_verify(old_inode,
2977 (struct ext4_dir_entry *)dir_bh->b_data))
2978 goto end_rename;
2979 set_buffer_verified(dir_bh);
3d0518f4
WY
2980 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
2981 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
ac27a0ec
DK
2982 goto end_rename;
2983 retval = -EMLINK;
af5bc92d 2984 if (!new_inode && new_dir != old_dir &&
2c94eb86 2985 EXT4_DIR_LINK_MAX(new_dir))
ac27a0ec 2986 goto end_rename;
ef607893
AG
2987 BUFFER_TRACE(dir_bh, "get_write_access");
2988 retval = ext4_journal_get_write_access(handle, dir_bh);
2989 if (retval)
2990 goto end_rename;
ac27a0ec
DK
2991 }
2992 if (!new_bh) {
af5bc92d 2993 retval = ext4_add_entry(handle, new_dentry, old_inode);
ac27a0ec
DK
2994 if (retval)
2995 goto end_rename;
2996 } else {
2997 BUFFER_TRACE(new_bh, "get write access");
ef607893
AG
2998 retval = ext4_journal_get_write_access(handle, new_bh);
2999 if (retval)
3000 goto end_rename;
ac27a0ec 3001 new_de->inode = cpu_to_le32(old_inode->i_ino);
617ba13b
MC
3002 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
3003 EXT4_FEATURE_INCOMPAT_FILETYPE))
ac27a0ec
DK
3004 new_de->file_type = old_de->file_type;
3005 new_dir->i_version++;
53b7e9f6
JK
3006 new_dir->i_ctime = new_dir->i_mtime =
3007 ext4_current_time(new_dir);
3008 ext4_mark_inode_dirty(handle, new_dir);
0390131b 3009 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
b0336e8d 3010 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
b4097142
TT
3011 if (unlikely(retval)) {
3012 ext4_std_error(new_dir->i_sb, retval);
3013 goto end_rename;
3014 }
ac27a0ec
DK
3015 brelse(new_bh);
3016 new_bh = NULL;
3017 }
3018
3019 /*
3020 * Like most other Unix systems, set the ctime for inodes on a
3021 * rename.
3022 */
ef7f3835 3023 old_inode->i_ctime = ext4_current_time(old_inode);
617ba13b 3024 ext4_mark_inode_dirty(handle, old_inode);
ac27a0ec
DK
3025
3026 /*
3027 * ok, that's it
3028 */
3029 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3030 old_de->name_len != old_dentry->d_name.len ||
3031 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
617ba13b 3032 (retval = ext4_delete_entry(handle, old_dir,
ac27a0ec
DK
3033 old_de, old_bh)) == -ENOENT) {
3034 /* old_de could have moved from under us during htree split, so
3035 * make sure that we are deleting the right entry. We might
3036 * also be pointing to a stale entry in the unused part of
3037 * old_bh so just checking inum and the name isn't enough. */
3038 struct buffer_head *old_bh2;
617ba13b 3039 struct ext4_dir_entry_2 *old_de2;
ac27a0ec 3040
f702ba0f 3041 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
ac27a0ec 3042 if (old_bh2) {
617ba13b 3043 retval = ext4_delete_entry(handle, old_dir,
ac27a0ec
DK
3044 old_de2, old_bh2);
3045 brelse(old_bh2);
3046 }
3047 }
3048 if (retval) {
12062ddd 3049 ext4_warning(old_dir->i_sb,
ac27a0ec
DK
3050 "Deleting old file (%lu), %d, error=%d",
3051 old_dir->i_ino, old_dir->i_nlink, retval);
3052 }
3053
3054 if (new_inode) {
f8628a14 3055 ext4_dec_count(handle, new_inode);
ef7f3835 3056 new_inode->i_ctime = ext4_current_time(new_inode);
ac27a0ec 3057 }
ef7f3835 3058 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
617ba13b 3059 ext4_update_dx_flag(old_dir);
ac27a0ec 3060 if (dir_bh) {
3d0518f4
WY
3061 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3062 cpu_to_le32(new_dir->i_ino);
0390131b 3063 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
ef58f69c
TM
3064 if (is_dx(old_inode)) {
3065 retval = ext4_handle_dirty_dx_node(handle,
3066 old_inode,
3067 dir_bh);
3068 } else {
3069 retval = ext4_handle_dirty_dirent_node(handle,
3070 old_inode,
3071 dir_bh);
3072 }
b4097142
TT
3073 if (retval) {
3074 ext4_std_error(old_dir->i_sb, retval);
3075 goto end_rename;
3076 }
f8628a14 3077 ext4_dec_count(handle, old_dir);
ac27a0ec 3078 if (new_inode) {
f8628a14 3079 /* checked empty_dir above, can't have another parent,
825f1481 3080 * ext4_dec_count() won't work for many-linked dirs */
6d6b77f1 3081 clear_nlink(new_inode);
ac27a0ec 3082 } else {
f8628a14 3083 ext4_inc_count(handle, new_dir);
617ba13b
MC
3084 ext4_update_dx_flag(new_dir);
3085 ext4_mark_inode_dirty(handle, new_dir);
ac27a0ec
DK
3086 }
3087 }
617ba13b 3088 ext4_mark_inode_dirty(handle, old_dir);
ac27a0ec 3089 if (new_inode) {
617ba13b 3090 ext4_mark_inode_dirty(handle, new_inode);
ac27a0ec 3091 if (!new_inode->i_nlink)
617ba13b 3092 ext4_orphan_add(handle, new_inode);
afd4672d
TT
3093 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3094 force_da_alloc = 1;
ac27a0ec
DK
3095 }
3096 retval = 0;
3097
3098end_rename:
af5bc92d
TT
3099 brelse(dir_bh);
3100 brelse(old_bh);
3101 brelse(new_bh);
617ba13b 3102 ext4_journal_stop(handle);
8750c6d5
TT
3103 if (retval == 0 && force_da_alloc)
3104 ext4_alloc_da_blocks(old_inode);
ac27a0ec
DK
3105 return retval;
3106}
3107
3108/*
3109 * directories can handle most operations...
3110 */
754661f1 3111const struct inode_operations ext4_dir_inode_operations = {
617ba13b
MC
3112 .create = ext4_create,
3113 .lookup = ext4_lookup,
3114 .link = ext4_link,
3115 .unlink = ext4_unlink,
3116 .symlink = ext4_symlink,
3117 .mkdir = ext4_mkdir,
3118 .rmdir = ext4_rmdir,
3119 .mknod = ext4_mknod,
3120 .rename = ext4_rename,
3121 .setattr = ext4_setattr,
03010a33 3122#ifdef CONFIG_EXT4_FS_XATTR
ac27a0ec
DK
3123 .setxattr = generic_setxattr,
3124 .getxattr = generic_getxattr,
617ba13b 3125 .listxattr = ext4_listxattr,
ac27a0ec
DK
3126 .removexattr = generic_removexattr,
3127#endif
4e34e719 3128 .get_acl = ext4_get_acl,
abc8746e 3129 .fiemap = ext4_fiemap,
ac27a0ec
DK
3130};
3131
754661f1 3132const struct inode_operations ext4_special_inode_operations = {
617ba13b 3133 .setattr = ext4_setattr,
03010a33 3134#ifdef CONFIG_EXT4_FS_XATTR
ac27a0ec
DK
3135 .setxattr = generic_setxattr,
3136 .getxattr = generic_getxattr,
617ba13b 3137 .listxattr = ext4_listxattr,
ac27a0ec
DK
3138 .removexattr = generic_removexattr,
3139#endif
4e34e719 3140 .get_acl = ext4_get_acl,
ac27a0ec 3141};