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