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