Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / fs / ext3 / namei.c
blob055dee6f64c84fa2d69c700aec8c1f716649478e
1 /*
2 * linux/fs/ext3/namei.c
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)
9 * from
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
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
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/jbd.h>
30 #include <linux/time.h>
31 #include <linux/ext3_fs.h>
32 #include <linux/ext3_jbd.h>
33 #include <linux/fcntl.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/quotaops.h>
37 #include <linux/buffer_head.h>
38 #include <linux/smp_lock.h>
39 #include "xattr.h"
40 #include "acl.h"
43 * define how far ahead to read directories while searching them.
45 #define NAMEI_RA_CHUNKS 2
46 #define NAMEI_RA_BLOCKS 4
47 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
48 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
50 static struct buffer_head *ext3_append(handle_t *handle,
51 struct inode *inode,
52 u32 *block, int *err)
54 struct buffer_head *bh;
56 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
58 if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
59 inode->i_size += inode->i_sb->s_blocksize;
60 EXT3_I(inode)->i_disksize = inode->i_size;
61 ext3_journal_get_write_access(handle,bh);
63 return bh;
66 #ifndef assert
67 #define assert(test) J_ASSERT(test)
68 #endif
70 #ifndef swap
71 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
72 #endif
74 typedef struct { u32 v; } le_u32;
75 typedef struct { u16 v; } le_u16;
77 #ifdef DX_DEBUG
78 #define dxtrace(command) command
79 #else
80 #define dxtrace(command)
81 #endif
83 struct fake_dirent
85 /*le*/u32 inode;
86 /*le*/u16 rec_len;
87 u8 name_len;
88 u8 file_type;
91 struct dx_countlimit
93 le_u16 limit;
94 le_u16 count;
97 struct dx_entry
99 le_u32 hash;
100 le_u32 block;
104 * dx_root_info is laid out so that if it should somehow get overlaid by a
105 * dirent the two low bits of the hash version will be zero. Therefore, the
106 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
109 struct dx_root
111 struct fake_dirent dot;
112 char dot_name[4];
113 struct fake_dirent dotdot;
114 char dotdot_name[4];
115 struct dx_root_info
117 le_u32 reserved_zero;
118 u8 hash_version;
119 u8 info_length; /* 8 */
120 u8 indirect_levels;
121 u8 unused_flags;
123 info;
124 struct dx_entry entries[0];
127 struct dx_node
129 struct fake_dirent fake;
130 struct dx_entry entries[0];
134 struct dx_frame
136 struct buffer_head *bh;
137 struct dx_entry *entries;
138 struct dx_entry *at;
141 struct dx_map_entry
143 u32 hash;
144 u32 offs;
147 #ifdef CONFIG_EXT3_INDEX
148 static inline unsigned dx_get_block (struct dx_entry *entry);
149 static void dx_set_block (struct dx_entry *entry, unsigned value);
150 static inline unsigned dx_get_hash (struct dx_entry *entry);
151 static void dx_set_hash (struct dx_entry *entry, unsigned value);
152 static unsigned dx_get_count (struct dx_entry *entries);
153 static unsigned dx_get_limit (struct dx_entry *entries);
154 static void dx_set_count (struct dx_entry *entries, unsigned value);
155 static void dx_set_limit (struct dx_entry *entries, unsigned value);
156 static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
157 static unsigned dx_node_limit (struct inode *dir);
158 static struct dx_frame *dx_probe(struct dentry *dentry,
159 struct inode *dir,
160 struct dx_hash_info *hinfo,
161 struct dx_frame *frame,
162 int *err);
163 static void dx_release (struct dx_frame *frames);
164 static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
165 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
166 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
167 static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
168 struct dx_map_entry *offsets, int count);
169 static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
170 static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
171 static int ext3_htree_next_block(struct inode *dir, __u32 hash,
172 struct dx_frame *frame,
173 struct dx_frame *frames, int *err,
174 __u32 *start_hash);
175 static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
176 struct ext3_dir_entry_2 **res_dir, int *err);
177 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
178 struct inode *inode);
181 * Future: use high four bits of block for coalesce-on-delete flags
182 * Mask them off for now.
185 static inline unsigned dx_get_block (struct dx_entry *entry)
187 return le32_to_cpu(entry->block.v) & 0x00ffffff;
190 static inline void dx_set_block (struct dx_entry *entry, unsigned value)
192 entry->block.v = cpu_to_le32(value);
195 static inline unsigned dx_get_hash (struct dx_entry *entry)
197 return le32_to_cpu(entry->hash.v);
200 static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
202 entry->hash.v = cpu_to_le32(value);
205 static inline unsigned dx_get_count (struct dx_entry *entries)
207 return le16_to_cpu(((struct dx_countlimit *) entries)->count.v);
210 static inline unsigned dx_get_limit (struct dx_entry *entries)
212 return le16_to_cpu(((struct dx_countlimit *) entries)->limit.v);
215 static inline void dx_set_count (struct dx_entry *entries, unsigned value)
217 ((struct dx_countlimit *) entries)->count.v = cpu_to_le16(value);
220 static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
222 ((struct dx_countlimit *) entries)->limit.v = cpu_to_le16(value);
225 static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
227 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
228 EXT3_DIR_REC_LEN(2) - infosize;
229 return 0? 20: entry_space / sizeof(struct dx_entry);
232 static inline unsigned dx_node_limit (struct inode *dir)
234 unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
235 return 0? 22: entry_space / sizeof(struct dx_entry);
239 * Debug
241 #ifdef DX_DEBUG
242 struct stats
244 unsigned names;
245 unsigned space;
246 unsigned bcount;
249 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
250 int size, int show_names)
252 unsigned names = 0, space = 0;
253 char *base = (char *) de;
254 struct dx_hash_info h = *hinfo;
256 printk("names: ");
257 while ((char *) de < base + size)
259 if (de->inode)
261 if (show_names)
263 int len = de->name_len;
264 char *name = de->name;
265 while (len--) printk("%c", *name++);
266 ext3fs_dirhash(de->name, de->name_len, &h);
267 printk(":%x.%u ", h.hash,
268 ((char *) de - base));
270 space += EXT3_DIR_REC_LEN(de->name_len);
271 names++;
273 de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
275 printk("(%i)\n", names);
276 return (struct stats) { names, space, 1 };
279 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
280 struct dx_entry *entries, int levels)
282 unsigned blocksize = dir->i_sb->s_blocksize;
283 unsigned count = dx_get_count (entries), names = 0, space = 0, i;
284 unsigned bcount = 0;
285 struct buffer_head *bh;
286 int err;
287 printk("%i indexed blocks...\n", count);
288 for (i = 0; i < count; i++, entries++)
290 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
291 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
292 struct stats stats;
293 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
294 if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
295 stats = levels?
296 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
297 dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
298 names += stats.names;
299 space += stats.space;
300 bcount += stats.bcount;
301 brelse (bh);
303 if (bcount)
304 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
305 names, space/bcount,(space/bcount)*100/blocksize);
306 return (struct stats) { names, space, bcount};
308 #endif /* DX_DEBUG */
311 * Probe for a directory leaf block to search.
313 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
314 * error in the directory index, and the caller should fall back to
315 * searching the directory normally. The callers of dx_probe **MUST**
316 * check for this error code, and make sure it never gets reflected
317 * back to userspace.
319 static struct dx_frame *
320 dx_probe(struct dentry *dentry, struct inode *dir,
321 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
323 unsigned count, indirect;
324 struct dx_entry *at, *entries, *p, *q, *m;
325 struct dx_root *root;
326 struct buffer_head *bh;
327 struct dx_frame *frame = frame_in;
328 u32 hash;
330 frame->bh = NULL;
331 if (dentry)
332 dir = dentry->d_parent->d_inode;
333 if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
334 goto fail;
335 root = (struct dx_root *) bh->b_data;
336 if (root->info.hash_version != DX_HASH_TEA &&
337 root->info.hash_version != DX_HASH_HALF_MD4 &&
338 root->info.hash_version != DX_HASH_LEGACY) {
339 ext3_warning(dir->i_sb, __FUNCTION__,
340 "Unrecognised inode hash code %d",
341 root->info.hash_version);
342 brelse(bh);
343 *err = ERR_BAD_DX_DIR;
344 goto fail;
346 hinfo->hash_version = root->info.hash_version;
347 hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
348 if (dentry)
349 ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
350 hash = hinfo->hash;
352 if (root->info.unused_flags & 1) {
353 ext3_warning(dir->i_sb, __FUNCTION__,
354 "Unimplemented inode hash flags: %#06x",
355 root->info.unused_flags);
356 brelse(bh);
357 *err = ERR_BAD_DX_DIR;
358 goto fail;
361 if ((indirect = root->info.indirect_levels) > 1) {
362 ext3_warning(dir->i_sb, __FUNCTION__,
363 "Unimplemented inode hash depth: %#06x",
364 root->info.indirect_levels);
365 brelse(bh);
366 *err = ERR_BAD_DX_DIR;
367 goto fail;
370 entries = (struct dx_entry *) (((char *)&root->info) +
371 root->info.info_length);
372 assert(dx_get_limit(entries) == dx_root_limit(dir,
373 root->info.info_length));
374 dxtrace (printk("Look up %x", hash));
375 while (1)
377 count = dx_get_count(entries);
378 assert (count && count <= dx_get_limit(entries));
379 p = entries + 1;
380 q = entries + count - 1;
381 while (p <= q)
383 m = p + (q - p)/2;
384 dxtrace(printk("."));
385 if (dx_get_hash(m) > hash)
386 q = m - 1;
387 else
388 p = m + 1;
391 if (0) // linear search cross check
393 unsigned n = count - 1;
394 at = entries;
395 while (n--)
397 dxtrace(printk(","));
398 if (dx_get_hash(++at) > hash)
400 at--;
401 break;
404 assert (at == p - 1);
407 at = p - 1;
408 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
409 frame->bh = bh;
410 frame->entries = entries;
411 frame->at = at;
412 if (!indirect--) return frame;
413 if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
414 goto fail2;
415 at = entries = ((struct dx_node *) bh->b_data)->entries;
416 assert (dx_get_limit(entries) == dx_node_limit (dir));
417 frame++;
419 fail2:
420 while (frame >= frame_in) {
421 brelse(frame->bh);
422 frame--;
424 fail:
425 return NULL;
428 static void dx_release (struct dx_frame *frames)
430 if (frames[0].bh == NULL)
431 return;
433 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
434 brelse(frames[1].bh);
435 brelse(frames[0].bh);
439 * This function increments the frame pointer to search the next leaf
440 * block, and reads in the necessary intervening nodes if the search
441 * should be necessary. Whether or not the search is necessary is
442 * controlled by the hash parameter. If the hash value is even, then
443 * the search is only continued if the next block starts with that
444 * hash value. This is used if we are searching for a specific file.
446 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
448 * This function returns 1 if the caller should continue to search,
449 * or 0 if it should not. If there is an error reading one of the
450 * index blocks, it will return -1.
452 * If start_hash is non-null, it will be filled in with the starting
453 * hash of the next page.
455 static int ext3_htree_next_block(struct inode *dir, __u32 hash,
456 struct dx_frame *frame,
457 struct dx_frame *frames, int *err,
458 __u32 *start_hash)
460 struct dx_frame *p;
461 struct buffer_head *bh;
462 int num_frames = 0;
463 __u32 bhash;
465 *err = ENOENT;
466 p = frame;
468 * Find the next leaf page by incrementing the frame pointer.
469 * If we run out of entries in the interior node, loop around and
470 * increment pointer in the parent node. When we break out of
471 * this loop, num_frames indicates the number of interior
472 * nodes need to be read.
474 while (1) {
475 if (++(p->at) < p->entries + dx_get_count(p->entries))
476 break;
477 if (p == frames)
478 return 0;
479 num_frames++;
480 p--;
484 * If the hash is 1, then continue only if the next page has a
485 * continuation hash of any value. This is used for readdir
486 * handling. Otherwise, check to see if the hash matches the
487 * desired contiuation hash. If it doesn't, return since
488 * there's no point to read in the successive index pages.
490 bhash = dx_get_hash(p->at);
491 if (start_hash)
492 *start_hash = bhash;
493 if ((hash & 1) == 0) {
494 if ((bhash & ~1) != hash)
495 return 0;
498 * If the hash is HASH_NB_ALWAYS, we always go to the next
499 * block so no check is necessary
501 while (num_frames--) {
502 if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
503 0, err)))
504 return -1; /* Failure */
505 p++;
506 brelse (p->bh);
507 p->bh = bh;
508 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
510 return 1;
515 * p is at least 6 bytes before the end of page
517 static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
519 return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
523 * This function fills a red-black tree with information from a
524 * directory. We start scanning the directory in hash order, starting
525 * at start_hash and start_minor_hash.
527 * This function returns the number of entries inserted into the tree,
528 * or a negative error code.
530 int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
531 __u32 start_minor_hash, __u32 *next_hash)
533 struct dx_hash_info hinfo;
534 struct buffer_head *bh;
535 struct ext3_dir_entry_2 *de, *top;
536 static struct dx_frame frames[2], *frame;
537 struct inode *dir;
538 int block, err;
539 int count = 0;
540 int ret;
541 __u32 hashval;
543 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
544 start_minor_hash));
545 dir = dir_file->f_dentry->d_inode;
546 hinfo.hash = start_hash;
547 hinfo.minor_hash = 0;
548 frame = dx_probe(0, dir_file->f_dentry->d_inode, &hinfo, frames, &err);
549 if (!frame)
550 return err;
552 /* Add '.' and '..' from the htree header */
553 if (!start_hash && !start_minor_hash) {
554 de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
555 if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
556 goto errout;
557 de = ext3_next_entry(de);
558 if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
559 goto errout;
560 count += 2;
563 while (1) {
564 block = dx_get_block(frame->at);
565 dxtrace(printk("Reading block %d\n", block));
566 if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
567 goto errout;
569 de = (struct ext3_dir_entry_2 *) bh->b_data;
570 top = (struct ext3_dir_entry_2 *) ((char *) de + dir->i_sb->s_blocksize -
571 EXT3_DIR_REC_LEN(0));
572 for (; de < top; de = ext3_next_entry(de)) {
573 ext3fs_dirhash(de->name, de->name_len, &hinfo);
574 if ((hinfo.hash < start_hash) ||
575 ((hinfo.hash == start_hash) &&
576 (hinfo.minor_hash < start_minor_hash)))
577 continue;
578 if ((err = ext3_htree_store_dirent(dir_file,
579 hinfo.hash, hinfo.minor_hash, de)) != 0) {
580 brelse(bh);
581 goto errout;
583 count++;
585 brelse (bh);
586 hashval = ~1;
587 ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
588 frame, frames, &err, &hashval);
589 if (next_hash)
590 *next_hash = hashval;
591 if (ret == -1)
592 goto errout;
594 * Stop if: (a) there are no more entries, or
595 * (b) we have inserted at least one entry and the
596 * next hash value is not a continuation
598 if ((ret == 0) ||
599 (count && ((hashval & 1) == 0)))
600 break;
602 dx_release(frames);
603 dxtrace(printk("Fill tree: returned %d entries\n", count));
604 return count;
605 errout:
606 dx_release(frames);
607 return (err);
612 * Directory block splitting, compacting
615 static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
616 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
618 int count = 0;
619 char *base = (char *) de;
620 struct dx_hash_info h = *hinfo;
622 while ((char *) de < base + size)
624 if (de->name_len && de->inode) {
625 ext3fs_dirhash(de->name, de->name_len, &h);
626 map_tail--;
627 map_tail->hash = h.hash;
628 map_tail->offs = (u32) ((char *) de - base);
629 count++;
631 /* XXX: do we need to check rec_len == 0 case? -Chris */
632 de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
634 return count;
637 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
639 struct dx_map_entry *p, *q, *top = map + count - 1;
640 int more;
641 /* Combsort until bubble sort doesn't suck */
642 while (count > 2)
644 count = count*10/13;
645 if (count - 9 < 2) /* 9, 10 -> 11 */
646 count = 11;
647 for (p = top, q = p - count; q >= map; p--, q--)
648 if (p->hash < q->hash)
649 swap(*p, *q);
651 /* Garden variety bubble sort */
652 do {
653 more = 0;
654 q = top;
655 while (q-- > map)
657 if (q[1].hash >= q[0].hash)
658 continue;
659 swap(*(q+1), *q);
660 more = 1;
662 } while(more);
665 static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
667 struct dx_entry *entries = frame->entries;
668 struct dx_entry *old = frame->at, *new = old + 1;
669 int count = dx_get_count(entries);
671 assert(count < dx_get_limit(entries));
672 assert(old < entries + count);
673 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
674 dx_set_hash(new, hash);
675 dx_set_block(new, block);
676 dx_set_count(entries, count + 1);
678 #endif
681 static void ext3_update_dx_flag(struct inode *inode)
683 if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
684 EXT3_FEATURE_COMPAT_DIR_INDEX))
685 EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
689 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
691 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
692 * `de != NULL' is guaranteed by caller.
694 static inline int ext3_match (int len, const char * const name,
695 struct ext3_dir_entry_2 * de)
697 if (len != de->name_len)
698 return 0;
699 if (!de->inode)
700 return 0;
701 return !memcmp(name, de->name, len);
705 * Returns 0 if not found, -1 on failure, and 1 on success
707 static inline int search_dirblock(struct buffer_head * bh,
708 struct inode *dir,
709 struct dentry *dentry,
710 unsigned long offset,
711 struct ext3_dir_entry_2 ** res_dir)
713 struct ext3_dir_entry_2 * de;
714 char * dlimit;
715 int de_len;
716 const char *name = dentry->d_name.name;
717 int namelen = dentry->d_name.len;
719 de = (struct ext3_dir_entry_2 *) bh->b_data;
720 dlimit = bh->b_data + dir->i_sb->s_blocksize;
721 while ((char *) de < dlimit) {
722 /* this code is executed quadratically often */
723 /* do minimal checking `by hand' */
725 if ((char *) de + namelen <= dlimit &&
726 ext3_match (namelen, name, de)) {
727 /* found a match - just to be sure, do a full check */
728 if (!ext3_check_dir_entry("ext3_find_entry",
729 dir, de, bh, offset))
730 return -1;
731 *res_dir = de;
732 return 1;
734 /* prevent looping on a bad block */
735 de_len = le16_to_cpu(de->rec_len);
736 if (de_len <= 0)
737 return -1;
738 offset += de_len;
739 de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
741 return 0;
746 * ext3_find_entry()
748 * finds an entry in the specified directory with the wanted name. It
749 * returns the cache buffer in which the entry was found, and the entry
750 * itself (as a parameter - res_dir). It does NOT read the inode of the
751 * entry - you'll have to do that yourself if you want to.
753 * The returned buffer_head has ->b_count elevated. The caller is expected
754 * to brelse() it when appropriate.
758 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
759 struct ext3_dir_entry_2 ** res_dir)
761 struct super_block * sb;
762 struct buffer_head * bh_use[NAMEI_RA_SIZE];
763 struct buffer_head * bh, *ret = NULL;
764 unsigned long start, block, b;
765 int ra_max = 0; /* Number of bh's in the readahead
766 buffer, bh_use[] */
767 int ra_ptr = 0; /* Current index into readahead
768 buffer */
769 int num = 0;
770 int nblocks, i, err;
771 struct inode *dir = dentry->d_parent->d_inode;
772 int namelen;
773 const u8 *name;
774 unsigned blocksize;
776 *res_dir = NULL;
777 sb = dir->i_sb;
778 blocksize = sb->s_blocksize;
779 namelen = dentry->d_name.len;
780 name = dentry->d_name.name;
781 if (namelen > EXT3_NAME_LEN)
782 return NULL;
783 #ifdef CONFIG_EXT3_INDEX
784 if (is_dx(dir)) {
785 bh = ext3_dx_find_entry(dentry, res_dir, &err);
787 * On success, or if the error was file not found,
788 * return. Otherwise, fall back to doing a search the
789 * old fashioned way.
791 if (bh || (err != ERR_BAD_DX_DIR))
792 return bh;
793 dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
795 #endif
796 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
797 start = EXT3_I(dir)->i_dir_start_lookup;
798 if (start >= nblocks)
799 start = 0;
800 block = start;
801 restart:
802 do {
804 * We deal with the read-ahead logic here.
806 if (ra_ptr >= ra_max) {
807 /* Refill the readahead buffer */
808 ra_ptr = 0;
809 b = block;
810 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
812 * Terminate if we reach the end of the
813 * directory and must wrap, or if our
814 * search has finished at this block.
816 if (b >= nblocks || (num && block == start)) {
817 bh_use[ra_max] = NULL;
818 break;
820 num++;
821 bh = ext3_getblk(NULL, dir, b++, 0, &err);
822 bh_use[ra_max] = bh;
823 if (bh)
824 ll_rw_block(READ, 1, &bh);
827 if ((bh = bh_use[ra_ptr++]) == NULL)
828 goto next;
829 wait_on_buffer(bh);
830 if (!buffer_uptodate(bh)) {
831 /* read error, skip block & hope for the best */
832 brelse(bh);
833 goto next;
835 i = search_dirblock(bh, dir, dentry,
836 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
837 if (i == 1) {
838 EXT3_I(dir)->i_dir_start_lookup = block;
839 ret = bh;
840 goto cleanup_and_exit;
841 } else {
842 brelse(bh);
843 if (i < 0)
844 goto cleanup_and_exit;
846 next:
847 if (++block >= nblocks)
848 block = 0;
849 } while (block != start);
852 * If the directory has grown while we were searching, then
853 * search the last part of the directory before giving up.
855 block = nblocks;
856 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
857 if (block < nblocks) {
858 start = 0;
859 goto restart;
862 cleanup_and_exit:
863 /* Clean up the read-ahead blocks */
864 for (; ra_ptr < ra_max; ra_ptr++)
865 brelse (bh_use[ra_ptr]);
866 return ret;
869 #ifdef CONFIG_EXT3_INDEX
870 static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
871 struct ext3_dir_entry_2 **res_dir, int *err)
873 struct super_block * sb;
874 struct dx_hash_info hinfo;
875 u32 hash;
876 struct dx_frame frames[2], *frame;
877 struct ext3_dir_entry_2 *de, *top;
878 struct buffer_head *bh;
879 unsigned long block;
880 int retval;
881 int namelen = dentry->d_name.len;
882 const u8 *name = dentry->d_name.name;
883 struct inode *dir = dentry->d_parent->d_inode;
885 sb = dir->i_sb;
886 if (!(frame = dx_probe (dentry, 0, &hinfo, frames, err)))
887 return NULL;
888 hash = hinfo.hash;
889 do {
890 block = dx_get_block(frame->at);
891 if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
892 goto errout;
893 de = (struct ext3_dir_entry_2 *) bh->b_data;
894 top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
895 EXT3_DIR_REC_LEN(0));
896 for (; de < top; de = ext3_next_entry(de))
897 if (ext3_match (namelen, name, de)) {
898 if (!ext3_check_dir_entry("ext3_find_entry",
899 dir, de, bh,
900 (block<<EXT3_BLOCK_SIZE_BITS(sb))
901 +((char *)de - bh->b_data))) {
902 brelse (bh);
903 goto errout;
905 *res_dir = de;
906 dx_release (frames);
907 return bh;
909 brelse (bh);
910 /* Check to see if we should continue to search */
911 retval = ext3_htree_next_block(dir, hash, frame,
912 frames, err, 0);
913 if (retval == -1) {
914 ext3_warning(sb, __FUNCTION__,
915 "error reading index page in directory #%lu",
916 dir->i_ino);
917 goto errout;
919 } while (retval == 1);
921 *err = -ENOENT;
922 errout:
923 dxtrace(printk("%s not found\n", name));
924 dx_release (frames);
925 return NULL;
927 #endif
929 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
931 struct inode * inode;
932 struct ext3_dir_entry_2 * de;
933 struct buffer_head * bh;
935 if (dentry->d_name.len > EXT3_NAME_LEN)
936 return ERR_PTR(-ENAMETOOLONG);
938 lock_kernel();
939 bh = ext3_find_entry(dentry, &de);
940 inode = NULL;
941 if (bh) {
942 unsigned long ino = le32_to_cpu(de->inode);
943 brelse (bh);
944 inode = iget(dir->i_sb, ino);
946 if (!inode) {
947 unlock_kernel();
948 return ERR_PTR(-EACCES);
951 unlock_kernel();
952 if (inode)
953 return d_splice_alias(inode, dentry);
954 d_add(dentry, inode);
955 return NULL;
959 struct dentry *ext3_get_parent(struct dentry *child)
961 unsigned long ino;
962 struct dentry *parent;
963 struct inode *inode;
964 struct dentry dotdot;
965 struct ext3_dir_entry_2 * de;
966 struct buffer_head *bh;
968 dotdot.d_name.name = "..";
969 dotdot.d_name.len = 2;
970 dotdot.d_parent = child; /* confusing, isn't it! */
972 lock_kernel();
973 bh = ext3_find_entry(&dotdot, &de);
974 inode = NULL;
975 if (!bh) {
976 unlock_kernel();
977 return ERR_PTR(-ENOENT);
979 ino = le32_to_cpu(de->inode);
980 brelse(bh);
981 inode = iget(child->d_inode->i_sb, ino);
982 unlock_kernel();
984 if (!inode)
985 return ERR_PTR(-EACCES);
987 parent = d_alloc_anon(inode);
988 if (!parent) {
989 iput(inode);
990 parent = ERR_PTR(-ENOMEM);
992 return parent;
995 #define S_SHIFT 12
996 static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
997 [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE,
998 [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR,
999 [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV,
1000 [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV,
1001 [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO,
1002 [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK,
1003 [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK,
1006 static inline void ext3_set_de_type(struct super_block *sb,
1007 struct ext3_dir_entry_2 *de,
1008 umode_t mode) {
1009 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
1010 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1013 #ifdef CONFIG_EXT3_INDEX
1014 static struct ext3_dir_entry_2 *
1015 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
1017 unsigned rec_len = 0;
1019 while (count--) {
1020 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
1021 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1022 memcpy (to, de, rec_len);
1023 ((struct ext3_dir_entry_2 *) to)->rec_len =
1024 cpu_to_le16(rec_len);
1025 de->inode = 0;
1026 map++;
1027 to += rec_len;
1029 return (struct ext3_dir_entry_2 *) (to - rec_len);
1032 static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
1034 struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
1035 unsigned rec_len = 0;
1037 prev = to = de;
1038 while ((char*)de < base + size) {
1039 next = (struct ext3_dir_entry_2 *) ((char *) de +
1040 le16_to_cpu(de->rec_len));
1041 if (de->inode && de->name_len) {
1042 rec_len = EXT3_DIR_REC_LEN(de->name_len);
1043 if (de > to)
1044 memmove(to, de, rec_len);
1045 to->rec_len = cpu_to_le16(rec_len);
1046 prev = to;
1047 to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
1049 de = next;
1051 return prev;
1054 static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1055 struct buffer_head **bh,struct dx_frame *frame,
1056 struct dx_hash_info *hinfo, int *error)
1058 unsigned blocksize = dir->i_sb->s_blocksize;
1059 unsigned count, continued;
1060 struct buffer_head *bh2;
1061 u32 newblock;
1062 u32 hash2;
1063 struct dx_map_entry *map;
1064 char *data1 = (*bh)->b_data, *data2;
1065 unsigned split;
1066 struct ext3_dir_entry_2 *de = NULL, *de2;
1067 int err;
1069 bh2 = ext3_append (handle, dir, &newblock, error);
1070 if (!(bh2)) {
1071 brelse(*bh);
1072 *bh = NULL;
1073 goto errout;
1076 BUFFER_TRACE(*bh, "get_write_access");
1077 err = ext3_journal_get_write_access(handle, *bh);
1078 if (err) {
1079 journal_error:
1080 brelse(*bh);
1081 brelse(bh2);
1082 *bh = NULL;
1083 ext3_std_error(dir->i_sb, err);
1084 goto errout;
1086 BUFFER_TRACE(frame->bh, "get_write_access");
1087 err = ext3_journal_get_write_access(handle, frame->bh);
1088 if (err)
1089 goto journal_error;
1091 data2 = bh2->b_data;
1093 /* create map in the end of data2 block */
1094 map = (struct dx_map_entry *) (data2 + blocksize);
1095 count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
1096 blocksize, hinfo, map);
1097 map -= count;
1098 split = count/2; // need to adjust to actual middle
1099 dx_sort_map (map, count);
1100 hash2 = map[split].hash;
1101 continued = hash2 == map[split - 1].hash;
1102 dxtrace(printk("Split block %i at %x, %i/%i\n",
1103 dx_get_block(frame->at), hash2, split, count-split));
1105 /* Fancy dance to stay within two buffers */
1106 de2 = dx_move_dirents(data1, data2, map + split, count - split);
1107 de = dx_pack_dirents(data1,blocksize);
1108 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1109 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
1110 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
1111 dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
1113 /* Which block gets the new entry? */
1114 if (hinfo->hash >= hash2)
1116 swap(*bh, bh2);
1117 de = de2;
1119 dx_insert_block (frame, hash2 + continued, newblock);
1120 err = ext3_journal_dirty_metadata (handle, bh2);
1121 if (err)
1122 goto journal_error;
1123 err = ext3_journal_dirty_metadata (handle, frame->bh);
1124 if (err)
1125 goto journal_error;
1126 brelse (bh2);
1127 dxtrace(dx_show_index ("frame", frame->entries));
1128 errout:
1129 return de;
1131 #endif
1135 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1136 * it points to a directory entry which is guaranteed to be large
1137 * enough for new directory entry. If de is NULL, then
1138 * add_dirent_to_buf will attempt search the directory block for
1139 * space. It will return -ENOSPC if no space is available, and -EIO
1140 * and -EEXIST if directory entry already exists.
1142 * NOTE! bh is NOT released in the case where ENOSPC is returned. In
1143 * all other cases bh is released.
1145 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1146 struct inode *inode, struct ext3_dir_entry_2 *de,
1147 struct buffer_head * bh)
1149 struct inode *dir = dentry->d_parent->d_inode;
1150 const char *name = dentry->d_name.name;
1151 int namelen = dentry->d_name.len;
1152 unsigned long offset = 0;
1153 unsigned short reclen;
1154 int nlen, rlen, err;
1155 char *top;
1157 reclen = EXT3_DIR_REC_LEN(namelen);
1158 if (!de) {
1159 de = (struct ext3_dir_entry_2 *)bh->b_data;
1160 top = bh->b_data + dir->i_sb->s_blocksize - reclen;
1161 while ((char *) de <= top) {
1162 if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
1163 bh, offset)) {
1164 brelse (bh);
1165 return -EIO;
1167 if (ext3_match (namelen, name, de)) {
1168 brelse (bh);
1169 return -EEXIST;
1171 nlen = EXT3_DIR_REC_LEN(de->name_len);
1172 rlen = le16_to_cpu(de->rec_len);
1173 if ((de->inode? rlen - nlen: rlen) >= reclen)
1174 break;
1175 de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
1176 offset += rlen;
1178 if ((char *) de > top)
1179 return -ENOSPC;
1181 BUFFER_TRACE(bh, "get_write_access");
1182 err = ext3_journal_get_write_access(handle, bh);
1183 if (err) {
1184 ext3_std_error(dir->i_sb, err);
1185 brelse(bh);
1186 return err;
1189 /* By now the buffer is marked for journaling */
1190 nlen = EXT3_DIR_REC_LEN(de->name_len);
1191 rlen = le16_to_cpu(de->rec_len);
1192 if (de->inode) {
1193 struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
1194 de1->rec_len = cpu_to_le16(rlen - nlen);
1195 de->rec_len = cpu_to_le16(nlen);
1196 de = de1;
1198 de->file_type = EXT3_FT_UNKNOWN;
1199 if (inode) {
1200 de->inode = cpu_to_le32(inode->i_ino);
1201 ext3_set_de_type(dir->i_sb, de, inode->i_mode);
1202 } else
1203 de->inode = 0;
1204 de->name_len = namelen;
1205 memcpy (de->name, name, namelen);
1207 * XXX shouldn't update any times until successful
1208 * completion of syscall, but too many callers depend
1209 * on this.
1211 * XXX similarly, too many callers depend on
1212 * ext3_new_inode() setting the times, but error
1213 * recovery deletes the inode, so the worst that can
1214 * happen is that the times are slightly out of date
1215 * and/or different from the directory change time.
1217 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1218 ext3_update_dx_flag(dir);
1219 dir->i_version++;
1220 ext3_mark_inode_dirty(handle, dir);
1221 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1222 err = ext3_journal_dirty_metadata(handle, bh);
1223 if (err)
1224 ext3_std_error(dir->i_sb, err);
1225 brelse(bh);
1226 return 0;
1229 #ifdef CONFIG_EXT3_INDEX
1231 * This converts a one block unindexed directory to a 3 block indexed
1232 * directory, and adds the dentry to the indexed directory.
1234 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1235 struct inode *inode, struct buffer_head *bh)
1237 struct inode *dir = dentry->d_parent->d_inode;
1238 const char *name = dentry->d_name.name;
1239 int namelen = dentry->d_name.len;
1240 struct buffer_head *bh2;
1241 struct dx_root *root;
1242 struct dx_frame frames[2], *frame;
1243 struct dx_entry *entries;
1244 struct ext3_dir_entry_2 *de, *de2;
1245 char *data1, *top;
1246 unsigned len;
1247 int retval;
1248 unsigned blocksize;
1249 struct dx_hash_info hinfo;
1250 u32 block;
1252 blocksize = dir->i_sb->s_blocksize;
1253 dxtrace(printk("Creating index\n"));
1254 retval = ext3_journal_get_write_access(handle, bh);
1255 if (retval) {
1256 ext3_std_error(dir->i_sb, retval);
1257 brelse(bh);
1258 return retval;
1260 root = (struct dx_root *) bh->b_data;
1262 EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
1263 bh2 = ext3_append (handle, dir, &block, &retval);
1264 if (!(bh2)) {
1265 brelse(bh);
1266 return retval;
1268 data1 = bh2->b_data;
1270 /* The 0th block becomes the root, move the dirents out */
1271 de = (struct ext3_dir_entry_2 *) &root->info;
1272 len = ((char *) root) + blocksize - (char *) de;
1273 memcpy (data1, de, len);
1274 de = (struct ext3_dir_entry_2 *) data1;
1275 top = data1 + len;
1276 while (((char *) de2=(char*)de+le16_to_cpu(de->rec_len)) < top)
1277 de = de2;
1278 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
1279 /* Initialize the root; the dot dirents already exist */
1280 de = (struct ext3_dir_entry_2 *) (&root->dotdot);
1281 de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
1282 memset (&root->info, 0, sizeof(root->info));
1283 root->info.info_length = sizeof(root->info);
1284 root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
1285 entries = root->entries;
1286 dx_set_block (entries, 1);
1287 dx_set_count (entries, 1);
1288 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
1290 /* Initialize as for dx_probe */
1291 hinfo.hash_version = root->info.hash_version;
1292 hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
1293 ext3fs_dirhash(name, namelen, &hinfo);
1294 frame = frames;
1295 frame->entries = entries;
1296 frame->at = entries;
1297 frame->bh = bh;
1298 bh = bh2;
1299 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1300 dx_release (frames);
1301 if (!(de))
1302 return retval;
1304 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1306 #endif
1309 * ext3_add_entry()
1311 * adds a file entry to the specified directory, using the same
1312 * semantics as ext3_find_entry(). It returns NULL if it failed.
1314 * NOTE!! The inode part of 'de' is left at 0 - which means you
1315 * may not sleep between calling this and putting something into
1316 * the entry, as someone else might have used it while you slept.
1318 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
1319 struct inode *inode)
1321 struct inode *dir = dentry->d_parent->d_inode;
1322 unsigned long offset;
1323 struct buffer_head * bh;
1324 struct ext3_dir_entry_2 *de;
1325 struct super_block * sb;
1326 int retval;
1327 #ifdef CONFIG_EXT3_INDEX
1328 int dx_fallback=0;
1329 #endif
1330 unsigned blocksize;
1331 unsigned nlen, rlen;
1332 u32 block, blocks;
1334 sb = dir->i_sb;
1335 blocksize = sb->s_blocksize;
1336 if (!dentry->d_name.len)
1337 return -EINVAL;
1338 #ifdef CONFIG_EXT3_INDEX
1339 if (is_dx(dir)) {
1340 retval = ext3_dx_add_entry(handle, dentry, inode);
1341 if (!retval || (retval != ERR_BAD_DX_DIR))
1342 return retval;
1343 EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
1344 dx_fallback++;
1345 ext3_mark_inode_dirty(handle, dir);
1347 #endif
1348 blocks = dir->i_size >> sb->s_blocksize_bits;
1349 for (block = 0, offset = 0; block < blocks; block++) {
1350 bh = ext3_bread(handle, dir, block, 0, &retval);
1351 if(!bh)
1352 return retval;
1353 retval = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1354 if (retval != -ENOSPC)
1355 return retval;
1357 #ifdef CONFIG_EXT3_INDEX
1358 if (blocks == 1 && !dx_fallback &&
1359 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1360 return make_indexed_dir(handle, dentry, inode, bh);
1361 #endif
1362 brelse(bh);
1364 bh = ext3_append(handle, dir, &block, &retval);
1365 if (!bh)
1366 return retval;
1367 de = (struct ext3_dir_entry_2 *) bh->b_data;
1368 de->inode = 0;
1369 de->rec_len = cpu_to_le16(rlen = blocksize);
1370 nlen = 0;
1371 return add_dirent_to_buf(handle, dentry, inode, de, bh);
1374 #ifdef CONFIG_EXT3_INDEX
1376 * Returns 0 for success, or a negative error value
1378 static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
1379 struct inode *inode)
1381 struct dx_frame frames[2], *frame;
1382 struct dx_entry *entries, *at;
1383 struct dx_hash_info hinfo;
1384 struct buffer_head * bh;
1385 struct inode *dir = dentry->d_parent->d_inode;
1386 struct super_block * sb = dir->i_sb;
1387 struct ext3_dir_entry_2 *de;
1388 int err;
1390 frame = dx_probe(dentry, 0, &hinfo, frames, &err);
1391 if (!frame)
1392 return err;
1393 entries = frame->entries;
1394 at = frame->at;
1396 if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1397 goto cleanup;
1399 BUFFER_TRACE(bh, "get_write_access");
1400 err = ext3_journal_get_write_access(handle, bh);
1401 if (err)
1402 goto journal_error;
1404 err = add_dirent_to_buf(handle, dentry, inode, 0, bh);
1405 if (err != -ENOSPC) {
1406 bh = 0;
1407 goto cleanup;
1410 /* Block full, should compress but for now just split */
1411 dxtrace(printk("using %u of %u node entries\n",
1412 dx_get_count(entries), dx_get_limit(entries)));
1413 /* Need to split index? */
1414 if (dx_get_count(entries) == dx_get_limit(entries)) {
1415 u32 newblock;
1416 unsigned icount = dx_get_count(entries);
1417 int levels = frame - frames;
1418 struct dx_entry *entries2;
1419 struct dx_node *node2;
1420 struct buffer_head *bh2;
1422 if (levels && (dx_get_count(frames->entries) ==
1423 dx_get_limit(frames->entries))) {
1424 ext3_warning(sb, __FUNCTION__,
1425 "Directory index full!\n");
1426 err = -ENOSPC;
1427 goto cleanup;
1429 bh2 = ext3_append (handle, dir, &newblock, &err);
1430 if (!(bh2))
1431 goto cleanup;
1432 node2 = (struct dx_node *)(bh2->b_data);
1433 entries2 = node2->entries;
1434 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
1435 node2->fake.inode = 0;
1436 BUFFER_TRACE(frame->bh, "get_write_access");
1437 err = ext3_journal_get_write_access(handle, frame->bh);
1438 if (err)
1439 goto journal_error;
1440 if (levels) {
1441 unsigned icount1 = icount/2, icount2 = icount - icount1;
1442 unsigned hash2 = dx_get_hash(entries + icount1);
1443 dxtrace(printk("Split index %i/%i\n", icount1, icount2));
1445 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1446 err = ext3_journal_get_write_access(handle,
1447 frames[0].bh);
1448 if (err)
1449 goto journal_error;
1451 memcpy ((char *) entries2, (char *) (entries + icount1),
1452 icount2 * sizeof(struct dx_entry));
1453 dx_set_count (entries, icount1);
1454 dx_set_count (entries2, icount2);
1455 dx_set_limit (entries2, dx_node_limit(dir));
1457 /* Which index block gets the new entry? */
1458 if (at - entries >= icount1) {
1459 frame->at = at = at - entries - icount1 + entries2;
1460 frame->entries = entries = entries2;
1461 swap(frame->bh, bh2);
1463 dx_insert_block (frames + 0, hash2, newblock);
1464 dxtrace(dx_show_index ("node", frames[1].entries));
1465 dxtrace(dx_show_index ("node",
1466 ((struct dx_node *) bh2->b_data)->entries));
1467 err = ext3_journal_dirty_metadata(handle, bh2);
1468 if (err)
1469 goto journal_error;
1470 brelse (bh2);
1471 } else {
1472 dxtrace(printk("Creating second level index...\n"));
1473 memcpy((char *) entries2, (char *) entries,
1474 icount * sizeof(struct dx_entry));
1475 dx_set_limit(entries2, dx_node_limit(dir));
1477 /* Set up root */
1478 dx_set_count(entries, 1);
1479 dx_set_block(entries + 0, newblock);
1480 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1482 /* Add new access path frame */
1483 frame = frames + 1;
1484 frame->at = at = at - entries + entries2;
1485 frame->entries = entries = entries2;
1486 frame->bh = bh2;
1487 err = ext3_journal_get_write_access(handle,
1488 frame->bh);
1489 if (err)
1490 goto journal_error;
1492 ext3_journal_dirty_metadata(handle, frames[0].bh);
1494 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1495 if (!de)
1496 goto cleanup;
1497 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1498 bh = 0;
1499 goto cleanup;
1501 journal_error:
1502 ext3_std_error(dir->i_sb, err);
1503 cleanup:
1504 if (bh)
1505 brelse(bh);
1506 dx_release(frames);
1507 return err;
1509 #endif
1512 * ext3_delete_entry deletes a directory entry by merging it with the
1513 * previous entry
1515 static int ext3_delete_entry (handle_t *handle,
1516 struct inode * dir,
1517 struct ext3_dir_entry_2 * de_del,
1518 struct buffer_head * bh)
1520 struct ext3_dir_entry_2 * de, * pde;
1521 int i;
1523 i = 0;
1524 pde = NULL;
1525 de = (struct ext3_dir_entry_2 *) bh->b_data;
1526 while (i < bh->b_size) {
1527 if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
1528 return -EIO;
1529 if (de == de_del) {
1530 BUFFER_TRACE(bh, "get_write_access");
1531 ext3_journal_get_write_access(handle, bh);
1532 if (pde)
1533 pde->rec_len =
1534 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1535 le16_to_cpu(de->rec_len));
1536 else
1537 de->inode = 0;
1538 dir->i_version++;
1539 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1540 ext3_journal_dirty_metadata(handle, bh);
1541 return 0;
1543 i += le16_to_cpu(de->rec_len);
1544 pde = de;
1545 de = (struct ext3_dir_entry_2 *)
1546 ((char *) de + le16_to_cpu(de->rec_len));
1548 return -ENOENT;
1552 * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we
1553 * do not perform it in these functions. We perform it at the call site,
1554 * if it is needed.
1556 static inline void ext3_inc_count(handle_t *handle, struct inode *inode)
1558 inode->i_nlink++;
1561 static inline void ext3_dec_count(handle_t *handle, struct inode *inode)
1563 inode->i_nlink--;
1566 static int ext3_add_nondir(handle_t *handle,
1567 struct dentry *dentry, struct inode *inode)
1569 int err = ext3_add_entry(handle, dentry, inode);
1570 if (!err) {
1571 err = ext3_mark_inode_dirty(handle, inode);
1572 if (!err) {
1573 d_instantiate(dentry, inode);
1574 return 0;
1577 ext3_dec_count(handle, inode);
1578 iput(inode);
1579 return err;
1583 * By the time this is called, we already have created
1584 * the directory cache entry for the new file, but it
1585 * is so far negative - it has no inode.
1587 * If the create succeeds, we fill in the inode information
1588 * with d_instantiate().
1590 static int ext3_create (struct inode * dir, struct dentry * dentry, int mode)
1592 handle_t *handle;
1593 struct inode * inode;
1594 int err;
1596 lock_kernel();
1597 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1598 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
1599 if (IS_ERR(handle)) {
1600 unlock_kernel();
1601 return PTR_ERR(handle);
1604 if (IS_DIRSYNC(dir))
1605 handle->h_sync = 1;
1607 inode = ext3_new_inode (handle, dir, mode);
1608 err = PTR_ERR(inode);
1609 if (!IS_ERR(inode)) {
1610 inode->i_op = &ext3_file_inode_operations;
1611 inode->i_fop = &ext3_file_operations;
1612 if (ext3_should_writeback_data(inode))
1613 inode->i_mapping->a_ops = &ext3_writeback_aops;
1614 else
1615 inode->i_mapping->a_ops = &ext3_aops;
1616 err = ext3_add_nondir(handle, dentry, inode);
1618 ext3_journal_stop(handle, dir);
1619 unlock_kernel();
1620 return err;
1623 static int ext3_mknod (struct inode * dir, struct dentry *dentry,
1624 int mode, dev_t rdev)
1626 handle_t *handle;
1627 struct inode *inode;
1628 int err;
1630 lock_kernel();
1631 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1632 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
1633 if (IS_ERR(handle)) {
1634 unlock_kernel();
1635 return PTR_ERR(handle);
1638 if (IS_DIRSYNC(dir))
1639 handle->h_sync = 1;
1641 inode = ext3_new_inode (handle, dir, mode);
1642 err = PTR_ERR(inode);
1643 if (!IS_ERR(inode)) {
1644 init_special_inode(inode, inode->i_mode, rdev);
1645 #ifdef CONFIG_EXT3_FS_XATTR
1646 inode->i_op = &ext3_special_inode_operations;
1647 #endif
1648 err = ext3_add_nondir(handle, dentry, inode);
1650 ext3_journal_stop(handle, dir);
1651 unlock_kernel();
1652 return err;
1655 static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
1657 handle_t *handle;
1658 struct inode * inode;
1659 struct buffer_head * dir_block;
1660 struct ext3_dir_entry_2 * de;
1661 int err;
1663 if (dir->i_nlink >= EXT3_LINK_MAX)
1664 return -EMLINK;
1666 lock_kernel();
1667 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
1668 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3);
1669 if (IS_ERR(handle)) {
1670 unlock_kernel();
1671 return PTR_ERR(handle);
1674 if (IS_DIRSYNC(dir))
1675 handle->h_sync = 1;
1677 inode = ext3_new_inode (handle, dir, S_IFDIR | mode);
1678 err = PTR_ERR(inode);
1679 if (IS_ERR(inode))
1680 goto out_stop;
1682 inode->i_op = &ext3_dir_inode_operations;
1683 inode->i_fop = &ext3_dir_operations;
1684 inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1685 dir_block = ext3_bread (handle, inode, 0, 1, &err);
1686 if (!dir_block) {
1687 inode->i_nlink--; /* is this nlink == 0? */
1688 ext3_mark_inode_dirty(handle, inode);
1689 iput (inode);
1690 goto out_stop;
1692 BUFFER_TRACE(dir_block, "get_write_access");
1693 ext3_journal_get_write_access(handle, dir_block);
1694 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
1695 de->inode = cpu_to_le32(inode->i_ino);
1696 de->name_len = 1;
1697 de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));
1698 strcpy (de->name, ".");
1699 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1700 de = (struct ext3_dir_entry_2 *)
1701 ((char *) de + le16_to_cpu(de->rec_len));
1702 de->inode = cpu_to_le32(dir->i_ino);
1703 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));
1704 de->name_len = 2;
1705 strcpy (de->name, "..");
1706 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
1707 inode->i_nlink = 2;
1708 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
1709 ext3_journal_dirty_metadata(handle, dir_block);
1710 brelse (dir_block);
1711 ext3_mark_inode_dirty(handle, inode);
1712 err = ext3_add_entry (handle, dentry, inode);
1713 if (err) {
1714 inode->i_nlink = 0;
1715 ext3_mark_inode_dirty(handle, inode);
1716 iput (inode);
1717 goto out_stop;
1719 dir->i_nlink++;
1720 ext3_update_dx_flag(dir);
1721 ext3_mark_inode_dirty(handle, dir);
1722 d_instantiate(dentry, inode);
1723 out_stop:
1724 ext3_journal_stop(handle, dir);
1725 unlock_kernel();
1726 return err;
1730 * routine to check that the specified directory is empty (for rmdir)
1732 static int empty_dir (struct inode * inode)
1734 unsigned long offset;
1735 struct buffer_head * bh;
1736 struct ext3_dir_entry_2 * de, * de1;
1737 struct super_block * sb;
1738 int err;
1740 sb = inode->i_sb;
1741 if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
1742 !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
1743 ext3_warning (inode->i_sb, "empty_dir",
1744 "bad directory (dir #%lu) - no data block",
1745 inode->i_ino);
1746 return 1;
1748 de = (struct ext3_dir_entry_2 *) bh->b_data;
1749 de1 = (struct ext3_dir_entry_2 *)
1750 ((char *) de + le16_to_cpu(de->rec_len));
1751 if (le32_to_cpu(de->inode) != inode->i_ino ||
1752 !le32_to_cpu(de1->inode) ||
1753 strcmp (".", de->name) ||
1754 strcmp ("..", de1->name)) {
1755 ext3_warning (inode->i_sb, "empty_dir",
1756 "bad directory (dir #%lu) - no `.' or `..'",
1757 inode->i_ino);
1758 brelse (bh);
1759 return 1;
1761 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
1762 de = (struct ext3_dir_entry_2 *)
1763 ((char *) de1 + le16_to_cpu(de1->rec_len));
1764 while (offset < inode->i_size ) {
1765 if (!bh ||
1766 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
1767 brelse (bh);
1768 bh = ext3_bread (NULL, inode,
1769 offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err);
1770 if (!bh) {
1771 #if 0
1772 ext3_error (sb, "empty_dir",
1773 "directory #%lu contains a hole at offset %lu",
1774 inode->i_ino, offset);
1775 #endif
1776 offset += sb->s_blocksize;
1777 continue;
1779 de = (struct ext3_dir_entry_2 *) bh->b_data;
1781 if (!ext3_check_dir_entry ("empty_dir", inode, de, bh,
1782 offset)) {
1783 brelse (bh);
1784 return 1;
1786 if (le32_to_cpu(de->inode)) {
1787 brelse (bh);
1788 return 0;
1790 offset += le16_to_cpu(de->rec_len);
1791 de = (struct ext3_dir_entry_2 *)
1792 ((char *) de + le16_to_cpu(de->rec_len));
1794 brelse (bh);
1795 return 1;
1798 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
1799 * such inodes, starting at the superblock, in case we crash before the
1800 * file is closed/deleted, or in case the inode truncate spans multiple
1801 * transactions and the last transaction is not recovered after a crash.
1803 * At filesystem recovery time, we walk this list deleting unlinked
1804 * inodes and truncating linked inodes in ext3_orphan_cleanup().
1806 int ext3_orphan_add(handle_t *handle, struct inode *inode)
1808 struct super_block *sb = inode->i_sb;
1809 struct ext3_iloc iloc;
1810 int err = 0, rc;
1812 lock_super(sb);
1813 if (!list_empty(&EXT3_I(inode)->i_orphan))
1814 goto out_unlock;
1816 /* Orphan handling is only valid for files with data blocks
1817 * being truncated, or files being unlinked. */
1819 /* @@@ FIXME: Observation from aviro:
1820 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
1821 * here (on lock_super()), so race with ext3_link() which might bump
1822 * ->i_nlink. For, say it, character device. Not a regular file,
1823 * not a directory, not a symlink and ->i_nlink > 0.
1825 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1826 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
1828 BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "get_write_access");
1829 err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
1830 if (err)
1831 goto out_unlock;
1833 err = ext3_reserve_inode_write(handle, inode, &iloc);
1834 if (err)
1835 goto out_unlock;
1837 /* Insert this inode at the head of the on-disk orphan list... */
1838 NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
1839 EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
1840 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
1841 rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
1842 if (!err)
1843 err = rc;
1845 /* Only add to the head of the in-memory list if all the
1846 * previous operations succeeded. If the orphan_add is going to
1847 * fail (possibly taking the journal offline), we can't risk
1848 * leaving the inode on the orphan list: stray orphan-list
1849 * entries can cause panics at unmount time.
1851 * This is safe: on error we're going to ignore the orphan list
1852 * anyway on the next recovery. */
1853 if (!err)
1854 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
1856 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
1857 jbd_debug(4, "orphan inode %ld will point to %d\n",
1858 inode->i_ino, NEXT_ORPHAN(inode));
1859 out_unlock:
1860 unlock_super(sb);
1861 ext3_std_error(inode->i_sb, err);
1862 return err;
1866 * ext3_orphan_del() removes an unlinked or truncated inode from the list
1867 * of such inodes stored on disk, because it is finally being cleaned up.
1869 int ext3_orphan_del(handle_t *handle, struct inode *inode)
1871 struct list_head *prev;
1872 struct ext3_inode_info *ei = EXT3_I(inode);
1873 struct ext3_sb_info *sbi;
1874 unsigned long ino_next;
1875 struct ext3_iloc iloc;
1876 int err = 0;
1878 lock_super(inode->i_sb);
1879 if (list_empty(&ei->i_orphan)) {
1880 unlock_super(inode->i_sb);
1881 return 0;
1884 ino_next = NEXT_ORPHAN(inode);
1885 prev = ei->i_orphan.prev;
1886 sbi = EXT3_SB(inode->i_sb);
1888 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
1890 list_del_init(&ei->i_orphan);
1892 /* If we're on an error path, we may not have a valid
1893 * transaction handle with which to update the orphan list on
1894 * disk, but we still need to remove the inode from the linked
1895 * list in memory. */
1896 if (!handle)
1897 goto out;
1899 err = ext3_reserve_inode_write(handle, inode, &iloc);
1900 if (err)
1901 goto out_err;
1903 if (prev == &sbi->s_orphan) {
1904 jbd_debug(4, "superblock will point to %lu\n", ino_next);
1905 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
1906 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1907 if (err)
1908 goto out_brelse;
1909 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
1910 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1911 } else {
1912 struct ext3_iloc iloc2;
1913 struct inode *i_prev =
1914 &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode;
1916 jbd_debug(4, "orphan inode %lu will point to %lu\n",
1917 i_prev->i_ino, ino_next);
1918 err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
1919 if (err)
1920 goto out_brelse;
1921 NEXT_ORPHAN(i_prev) = ino_next;
1922 err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
1924 if (err)
1925 goto out_brelse;
1926 NEXT_ORPHAN(inode) = 0;
1927 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
1928 if (err)
1929 goto out_brelse;
1931 out_err:
1932 ext3_std_error(inode->i_sb, err);
1933 out:
1934 unlock_super(inode->i_sb);
1935 return err;
1937 out_brelse:
1938 brelse(iloc.bh);
1939 goto out_err;
1942 static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
1944 int retval;
1945 struct inode * inode;
1946 struct buffer_head * bh;
1947 struct ext3_dir_entry_2 * de;
1948 handle_t *handle;
1950 lock_kernel();
1951 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
1952 if (IS_ERR(handle)) {
1953 unlock_kernel();
1954 return PTR_ERR(handle);
1957 retval = -ENOENT;
1958 bh = ext3_find_entry (dentry, &de);
1959 if (!bh)
1960 goto end_rmdir;
1962 if (IS_DIRSYNC(dir))
1963 handle->h_sync = 1;
1965 inode = dentry->d_inode;
1966 DQUOT_INIT(inode);
1968 retval = -EIO;
1969 if (le32_to_cpu(de->inode) != inode->i_ino)
1970 goto end_rmdir;
1972 retval = -ENOTEMPTY;
1973 if (!empty_dir (inode))
1974 goto end_rmdir;
1976 retval = ext3_delete_entry(handle, dir, de, bh);
1977 if (retval)
1978 goto end_rmdir;
1979 if (inode->i_nlink != 2)
1980 ext3_warning (inode->i_sb, "ext3_rmdir",
1981 "empty directory has nlink!=2 (%d)",
1982 inode->i_nlink);
1983 inode->i_version++;
1984 inode->i_nlink = 0;
1985 /* There's no need to set i_disksize: the fact that i_nlink is
1986 * zero will ensure that the right thing happens during any
1987 * recovery. */
1988 inode->i_size = 0;
1989 ext3_orphan_add(handle, inode);
1990 ext3_mark_inode_dirty(handle, inode);
1991 dir->i_nlink--;
1992 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1993 ext3_update_dx_flag(dir);
1994 ext3_mark_inode_dirty(handle, dir);
1996 end_rmdir:
1997 ext3_journal_stop(handle, dir);
1998 brelse (bh);
1999 unlock_kernel();
2000 return retval;
2003 static int ext3_unlink(struct inode * dir, struct dentry *dentry)
2005 int retval;
2006 struct inode * inode;
2007 struct buffer_head * bh;
2008 struct ext3_dir_entry_2 * de;
2009 handle_t *handle;
2011 lock_kernel();
2012 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
2013 if (IS_ERR(handle)) {
2014 unlock_kernel();
2015 return PTR_ERR(handle);
2018 if (IS_DIRSYNC(dir))
2019 handle->h_sync = 1;
2021 retval = -ENOENT;
2022 bh = ext3_find_entry (dentry, &de);
2023 if (!bh)
2024 goto end_unlink;
2026 inode = dentry->d_inode;
2027 DQUOT_INIT(inode);
2029 retval = -EIO;
2030 if (le32_to_cpu(de->inode) != inode->i_ino)
2031 goto end_unlink;
2033 if (!inode->i_nlink) {
2034 ext3_warning (inode->i_sb, "ext3_unlink",
2035 "Deleting nonexistent file (%lu), %d",
2036 inode->i_ino, inode->i_nlink);
2037 inode->i_nlink = 1;
2039 retval = ext3_delete_entry(handle, dir, de, bh);
2040 if (retval)
2041 goto end_unlink;
2042 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2043 ext3_update_dx_flag(dir);
2044 ext3_mark_inode_dirty(handle, dir);
2045 inode->i_nlink--;
2046 if (!inode->i_nlink)
2047 ext3_orphan_add(handle, inode);
2048 ext3_mark_inode_dirty(handle, inode);
2049 inode->i_ctime = dir->i_ctime;
2050 retval = 0;
2052 end_unlink:
2053 ext3_journal_stop(handle, dir);
2054 unlock_kernel();
2055 brelse (bh);
2056 return retval;
2059 static int ext3_symlink (struct inode * dir,
2060 struct dentry *dentry, const char * symname)
2062 handle_t *handle;
2063 struct inode * inode;
2064 int l, err;
2066 l = strlen(symname)+1;
2067 if (l > dir->i_sb->s_blocksize)
2068 return -ENAMETOOLONG;
2070 lock_kernel();
2071 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2072 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5);
2073 if (IS_ERR(handle)) {
2074 unlock_kernel();
2075 return PTR_ERR(handle);
2078 if (IS_DIRSYNC(dir))
2079 handle->h_sync = 1;
2081 inode = ext3_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
2082 err = PTR_ERR(inode);
2083 if (IS_ERR(inode))
2084 goto out_stop;
2086 if (l > sizeof (EXT3_I(inode)->i_data)) {
2087 inode->i_op = &ext3_symlink_inode_operations;
2088 if (ext3_should_writeback_data(inode))
2089 inode->i_mapping->a_ops = &ext3_writeback_aops;
2090 else
2091 inode->i_mapping->a_ops = &ext3_aops;
2093 * page_symlink() calls into ext3_prepare/commit_write.
2094 * We have a transaction open. All is sweetness. It also sets
2095 * i_size in generic_commit_write().
2097 err = page_symlink(inode, symname, l);
2098 if (err) {
2099 ext3_dec_count(handle, inode);
2100 ext3_mark_inode_dirty(handle, inode);
2101 iput (inode);
2102 goto out_stop;
2104 } else {
2105 inode->i_op = &ext3_fast_symlink_inode_operations;
2106 memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
2107 inode->i_size = l-1;
2109 EXT3_I(inode)->i_disksize = inode->i_size;
2110 err = ext3_add_nondir(handle, dentry, inode);
2111 out_stop:
2112 ext3_journal_stop(handle, dir);
2113 unlock_kernel();
2114 return err;
2117 static int ext3_link (struct dentry * old_dentry,
2118 struct inode * dir, struct dentry *dentry)
2120 handle_t *handle;
2121 struct inode *inode = old_dentry->d_inode;
2122 int err;
2124 lock_kernel();
2125 if (inode->i_nlink >= EXT3_LINK_MAX) {
2126 unlock_kernel();
2127 return -EMLINK;
2130 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS +
2131 EXT3_INDEX_EXTRA_TRANS_BLOCKS);
2132 if (IS_ERR(handle)) {
2133 unlock_kernel();
2134 return PTR_ERR(handle);
2137 if (IS_DIRSYNC(dir))
2138 handle->h_sync = 1;
2140 inode->i_ctime = CURRENT_TIME;
2141 ext3_inc_count(handle, inode);
2142 atomic_inc(&inode->i_count);
2144 err = ext3_add_nondir(handle, dentry, inode);
2145 ext3_journal_stop(handle, dir);
2146 unlock_kernel();
2147 return err;
2150 #define PARENT_INO(buffer) \
2151 ((struct ext3_dir_entry_2 *) ((char *) buffer + \
2152 le16_to_cpu(((struct ext3_dir_entry_2 *) buffer)->rec_len)))->inode
2155 * Anybody can rename anything with this: the permission checks are left to the
2156 * higher-level routines.
2158 static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
2159 struct inode * new_dir,struct dentry *new_dentry)
2161 handle_t *handle;
2162 struct inode * old_inode, * new_inode;
2163 struct buffer_head * old_bh, * new_bh, * dir_bh;
2164 struct ext3_dir_entry_2 * old_de, * new_de;
2165 int retval;
2167 old_bh = new_bh = dir_bh = NULL;
2169 lock_kernel();
2170 handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS +
2171 EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
2172 if (IS_ERR(handle)) {
2173 unlock_kernel();
2174 return PTR_ERR(handle);
2177 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2178 handle->h_sync = 1;
2180 old_bh = ext3_find_entry (old_dentry, &old_de);
2182 * Check for inode number is _not_ due to possible IO errors.
2183 * We might rmdir the source, keep it as pwd of some process
2184 * and merrily kill the link to whatever was created under the
2185 * same name. Goodbye sticky bit ;-<
2187 old_inode = old_dentry->d_inode;
2188 retval = -ENOENT;
2189 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2190 goto end_rename;
2192 new_inode = new_dentry->d_inode;
2193 new_bh = ext3_find_entry (new_dentry, &new_de);
2194 if (new_bh) {
2195 if (!new_inode) {
2196 brelse (new_bh);
2197 new_bh = NULL;
2198 } else {
2199 DQUOT_INIT(new_inode);
2202 if (S_ISDIR(old_inode->i_mode)) {
2203 if (new_inode) {
2204 retval = -ENOTEMPTY;
2205 if (!empty_dir (new_inode))
2206 goto end_rename;
2208 retval = -EIO;
2209 dir_bh = ext3_bread (handle, old_inode, 0, 0, &retval);
2210 if (!dir_bh)
2211 goto end_rename;
2212 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
2213 goto end_rename;
2214 retval = -EMLINK;
2215 if (!new_inode && new_dir!=old_dir &&
2216 new_dir->i_nlink >= EXT3_LINK_MAX)
2217 goto end_rename;
2219 if (!new_bh) {
2220 retval = ext3_add_entry (handle, new_dentry, old_inode);
2221 if (retval)
2222 goto end_rename;
2223 } else {
2224 BUFFER_TRACE(new_bh, "get write access");
2225 BUFFER_TRACE(new_bh, "get_write_access");
2226 ext3_journal_get_write_access(handle, new_bh);
2227 new_de->inode = le32_to_cpu(old_inode->i_ino);
2228 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2229 EXT3_FEATURE_INCOMPAT_FILETYPE))
2230 new_de->file_type = old_de->file_type;
2231 new_dir->i_version++;
2232 BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
2233 ext3_journal_dirty_metadata(handle, new_bh);
2234 brelse(new_bh);
2235 new_bh = NULL;
2239 * Like most other Unix systems, set the ctime for inodes on a
2240 * rename.
2242 old_inode->i_ctime = CURRENT_TIME;
2243 ext3_mark_inode_dirty(handle, old_inode);
2246 * ok, that's it
2248 retval = ext3_delete_entry(handle, old_dir, old_de, old_bh);
2249 if (retval == -ENOENT) {
2251 * old_de could have moved out from under us.
2253 struct buffer_head *old_bh2;
2254 struct ext3_dir_entry_2 *old_de2;
2256 old_bh2 = ext3_find_entry(old_dentry, &old_de2);
2257 if (old_bh2) {
2258 retval = ext3_delete_entry(handle, old_dir,
2259 old_de2, old_bh2);
2260 brelse(old_bh2);
2263 if (retval) {
2264 ext3_warning(old_dir->i_sb, "ext3_rename",
2265 "Deleting old file (%lu), %d, error=%d",
2266 old_dir->i_ino, old_dir->i_nlink, retval);
2269 if (new_inode) {
2270 new_inode->i_nlink--;
2271 new_inode->i_ctime = CURRENT_TIME;
2273 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
2274 ext3_update_dx_flag(old_dir);
2275 if (dir_bh) {
2276 BUFFER_TRACE(dir_bh, "get_write_access");
2277 ext3_journal_get_write_access(handle, dir_bh);
2278 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
2279 BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
2280 ext3_journal_dirty_metadata(handle, dir_bh);
2281 old_dir->i_nlink--;
2282 if (new_inode) {
2283 new_inode->i_nlink--;
2284 } else {
2285 new_dir->i_nlink++;
2286 ext3_update_dx_flag(new_dir);
2287 ext3_mark_inode_dirty(handle, new_dir);
2290 ext3_mark_inode_dirty(handle, old_dir);
2291 if (new_inode) {
2292 ext3_mark_inode_dirty(handle, new_inode);
2293 if (!new_inode->i_nlink)
2294 ext3_orphan_add(handle, new_inode);
2296 retval = 0;
2298 end_rename:
2299 brelse (dir_bh);
2300 brelse (old_bh);
2301 brelse (new_bh);
2302 ext3_journal_stop(handle, old_dir);
2303 unlock_kernel();
2304 return retval;
2308 * directories can handle most operations...
2310 struct inode_operations ext3_dir_inode_operations = {
2311 .create = ext3_create,
2312 .lookup = ext3_lookup,
2313 .link = ext3_link,
2314 .unlink = ext3_unlink,
2315 .symlink = ext3_symlink,
2316 .mkdir = ext3_mkdir,
2317 .rmdir = ext3_rmdir,
2318 .mknod = ext3_mknod,
2319 .rename = ext3_rename,
2320 .setattr = ext3_setattr,
2321 .setxattr = ext3_setxattr,
2322 .getxattr = ext3_getxattr,
2323 .listxattr = ext3_listxattr,
2324 .removexattr = ext3_removexattr,
2325 .permission = ext3_permission,
2328 struct inode_operations ext3_special_inode_operations = {
2329 .setattr = ext3_setattr,
2330 .setxattr = ext3_setxattr,
2331 .getxattr = ext3_getxattr,
2332 .listxattr = ext3_listxattr,
2333 .removexattr = ext3_removexattr,
2334 .permission = ext3_permission,