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