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