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