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