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)
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
22 #include <linux/jbd.h>
23 #include <linux/sched.h>
24 #include <linux/ext3_fs.h>
25 #include <linux/ext3_jbd.h>
26 #include <linux/fcntl.h>
27 #include <linux/stat.h>
28 #include <linux/string.h>
29 #include <linux/locks.h>
30 #include <linux/quotaops.h>
34 * define how far ahead to read directories while searching them.
36 #define NAMEI_RA_CHUNKS 2
37 #define NAMEI_RA_BLOCKS 4
38 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
39 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
42 * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
44 * `len <= EXT3_NAME_LEN' is guaranteed by caller.
45 * `de != NULL' is guaranteed by caller.
47 static inline int ext3_match (int len, const char * const name,
48 struct ext3_dir_entry_2 * de)
50 if (len != de->name_len)
54 return !memcmp(name, de->name, len);
58 * Returns 0 if not found, -1 on failure, and 1 on success
60 static int inline search_dirblock(struct buffer_head * bh,
62 struct dentry *dentry,
64 struct ext3_dir_entry_2 ** res_dir)
66 struct ext3_dir_entry_2 * de;
69 const char *name = dentry->d_name.name;
70 int namelen = dentry->d_name.len;
72 de = (struct ext3_dir_entry_2 *) bh->b_data;
73 dlimit = bh->b_data + dir->i_sb->s_blocksize;
74 while ((char *) de < dlimit) {
75 /* this code is executed quadratically often */
76 /* do minimal checking `by hand' */
78 if ((char *) de + namelen <= dlimit &&
79 ext3_match (namelen, name, de)) {
80 /* found a match - just to be sure, do a full check */
81 if (!ext3_check_dir_entry("ext3_find_entry",
87 /* prevent looping on a bad block */
88 de_len = le16_to_cpu(de->rec_len);
92 de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
100 * finds an entry in the specified directory with the wanted name. It
101 * returns the cache buffer in which the entry was found, and the entry
102 * itself (as a parameter - res_dir). It does NOT read the inode of the
103 * entry - you'll have to do that yourself if you want to.
105 * The returned buffer_head has ->b_count elevated. The caller is expected
106 * to brelse() it when appropriate.
108 static struct buffer_head * ext3_find_entry (struct dentry *dentry,
109 struct ext3_dir_entry_2 ** res_dir)
111 struct super_block * sb;
112 struct buffer_head * bh_use[NAMEI_RA_SIZE];
113 struct buffer_head * bh, *ret = NULL;
114 unsigned long start, block, b;
115 int ra_max = 0; /* Number of bh's in the readahead
117 int ra_ptr = 0; /* Current index into readahead
121 struct inode *dir = dentry->d_parent->d_inode;
126 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
127 start = dir->u.ext3_i.i_dir_start_lookup;
128 if (start >= nblocks)
134 * We deal with the read-ahead logic here.
136 if (ra_ptr >= ra_max) {
137 /* Refill the readahead buffer */
140 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
142 * Terminate if we reach the end of the
143 * directory and must wrap, or if our
144 * search has finished at this block.
146 if (b >= nblocks || (num && block == start)) {
147 bh_use[ra_max] = NULL;
151 bh = ext3_getblk(NULL, dir, b++, 0, &err);
154 ll_rw_block(READ, 1, &bh);
157 if ((bh = bh_use[ra_ptr++]) == NULL)
160 if (!buffer_uptodate(bh)) {
161 /* read error, skip block & hope for the best */
165 i = search_dirblock(bh, dir, dentry,
166 block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
168 dir->u.ext3_i.i_dir_start_lookup = block;
170 goto cleanup_and_exit;
174 goto cleanup_and_exit;
177 if (++block >= nblocks)
179 } while (block != start);
182 * If the directory has grown while we were searching, then
183 * search the last part of the directory before giving up.
186 nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
187 if (block < nblocks) {
193 /* Clean up the read-ahead blocks */
194 for (; ra_ptr < ra_max; ra_ptr++)
195 brelse (bh_use[ra_ptr]);
199 static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry)
201 struct inode * inode;
202 struct ext3_dir_entry_2 * de;
203 struct buffer_head * bh;
205 if (dentry->d_name.len > EXT3_NAME_LEN)
206 return ERR_PTR(-ENAMETOOLONG);
208 bh = ext3_find_entry(dentry, &de);
211 unsigned long ino = le32_to_cpu(de->inode);
213 inode = iget(dir->i_sb, ino);
216 return ERR_PTR(-EACCES);
218 d_add(dentry, inode);
223 static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
224 [S_IFREG >> S_SHIFT] EXT3_FT_REG_FILE,
225 [S_IFDIR >> S_SHIFT] EXT3_FT_DIR,
226 [S_IFCHR >> S_SHIFT] EXT3_FT_CHRDEV,
227 [S_IFBLK >> S_SHIFT] EXT3_FT_BLKDEV,
228 [S_IFIFO >> S_SHIFT] EXT3_FT_FIFO,
229 [S_IFSOCK >> S_SHIFT] EXT3_FT_SOCK,
230 [S_IFLNK >> S_SHIFT] EXT3_FT_SYMLINK,
233 static inline void ext3_set_de_type(struct super_block *sb,
234 struct ext3_dir_entry_2 *de,
236 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
237 de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
243 * adds a file entry to the specified directory, using the same
244 * semantics as ext3_find_entry(). It returns NULL if it failed.
246 * NOTE!! The inode part of 'de' is left at 0 - which means you
247 * may not sleep between calling this and putting something into
248 * the entry, as someone else might have used it while you slept.
252 * AKPM: the journalling code here looks wrong on the error paths
254 static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
257 struct inode *dir = dentry->d_parent->d_inode;
258 const char *name = dentry->d_name.name;
259 int namelen = dentry->d_name.len;
260 unsigned long offset;
261 unsigned short rec_len;
262 struct buffer_head * bh;
263 struct ext3_dir_entry_2 * de, * de1;
264 struct super_block * sb;
271 bh = ext3_bread (handle, dir, 0, 0, &retval);
274 rec_len = EXT3_DIR_REC_LEN(namelen);
276 de = (struct ext3_dir_entry_2 *) bh->b_data;
278 if ((char *)de >= sb->s_blocksize + bh->b_data) {
281 bh = ext3_bread (handle, dir,
282 offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);
285 if (dir->i_size <= offset) {
286 if (dir->i_size == 0) {
291 ext3_debug ("creating next block\n");
293 BUFFER_TRACE(bh, "get_write_access");
294 ext3_journal_get_write_access(handle, bh);
295 de = (struct ext3_dir_entry_2 *) bh->b_data;
297 de->rec_len = le16_to_cpu(sb->s_blocksize);
298 dir->u.ext3_i.i_disksize =
299 dir->i_size = offset + sb->s_blocksize;
300 dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
301 ext3_mark_inode_dirty(handle, dir);
304 ext3_debug ("skipping to next block\n");
306 de = (struct ext3_dir_entry_2 *) bh->b_data;
309 if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,
314 if (ext3_match (namelen, name, de)) {
318 if ((le32_to_cpu(de->inode) == 0 &&
319 le16_to_cpu(de->rec_len) >= rec_len) ||
320 (le16_to_cpu(de->rec_len) >=
321 EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {
322 BUFFER_TRACE(bh, "get_write_access");
323 ext3_journal_get_write_access(handle, bh);
324 /* By now the buffer is marked for journaling */
325 offset += le16_to_cpu(de->rec_len);
326 if (le32_to_cpu(de->inode)) {
327 de1 = (struct ext3_dir_entry_2 *) ((char *) de +
328 EXT3_DIR_REC_LEN(de->name_len));
330 cpu_to_le16(le16_to_cpu(de->rec_len) -
331 EXT3_DIR_REC_LEN(de->name_len));
332 de->rec_len = cpu_to_le16(
333 EXT3_DIR_REC_LEN(de->name_len));
336 de->file_type = EXT3_FT_UNKNOWN;
338 de->inode = cpu_to_le32(inode->i_ino);
339 ext3_set_de_type(dir->i_sb, de, inode->i_mode);
342 de->name_len = namelen;
343 memcpy (de->name, name, namelen);
345 * XXX shouldn't update any times until successful
346 * completion of syscall, but too many callers depend
349 * XXX similarly, too many callers depend on
350 * ext3_new_inode() setting the times, but error
351 * recovery deletes the inode, so the worst that can
352 * happen is that the times are slightly out of date
353 * and/or different from the directory change time.
355 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
356 dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
357 dir->i_version = ++event;
358 ext3_mark_inode_dirty(handle, dir);
359 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
360 ext3_journal_dirty_metadata(handle, bh);
364 offset += le16_to_cpu(de->rec_len);
365 de = (struct ext3_dir_entry_2 *)
366 ((char *) de + le16_to_cpu(de->rec_len));
373 * ext3_delete_entry deletes a directory entry by merging it with the
376 static int ext3_delete_entry (handle_t *handle,
378 struct ext3_dir_entry_2 * de_del,
379 struct buffer_head * bh)
381 struct ext3_dir_entry_2 * de, * pde;
386 de = (struct ext3_dir_entry_2 *) bh->b_data;
387 while (i < bh->b_size) {
388 if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
391 BUFFER_TRACE(bh, "get_write_access");
392 ext3_journal_get_write_access(handle, bh);
395 cpu_to_le16(le16_to_cpu(pde->rec_len) +
396 le16_to_cpu(de->rec_len));
399 dir->i_version = ++event;
400 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
401 ext3_journal_dirty_metadata(handle, bh);
404 i += le16_to_cpu(de->rec_len);
406 de = (struct ext3_dir_entry_2 *)
407 ((char *) de + le16_to_cpu(de->rec_len));
413 * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we
414 * do not perform it in these functions. We perform it at the call site,
417 static inline void ext3_inc_count(handle_t *handle, struct inode *inode)
422 static inline void ext3_dec_count(handle_t *handle, struct inode *inode)
427 static int ext3_add_nondir(handle_t *handle,
428 struct dentry *dentry, struct inode *inode)
430 int err = ext3_add_entry(handle, dentry, inode);
432 err = ext3_mark_inode_dirty(handle, inode);
434 d_instantiate(dentry, inode);
438 ext3_dec_count(handle, inode);
444 * By the time this is called, we already have created
445 * the directory cache entry for the new file, but it
446 * is so far negative - it has no inode.
448 * If the create succeeds, we fill in the inode information
449 * with d_instantiate().
451 static int ext3_create (struct inode * dir, struct dentry * dentry, int mode)
454 struct inode * inode;
457 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
459 return PTR_ERR(handle);
464 inode = ext3_new_inode (handle, dir, mode);
465 err = PTR_ERR(inode);
466 if (!IS_ERR(inode)) {
467 inode->i_op = &ext3_file_inode_operations;
468 inode->i_fop = &ext3_file_operations;
469 inode->i_mapping->a_ops = &ext3_aops;
470 err = ext3_add_nondir(handle, dentry, inode);
472 ext3_journal_stop(handle, dir);
476 static int ext3_mknod (struct inode * dir, struct dentry *dentry,
483 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
485 return PTR_ERR(handle);
490 inode = ext3_new_inode (handle, dir, mode);
491 err = PTR_ERR(inode);
492 if (!IS_ERR(inode)) {
493 init_special_inode(inode, mode, rdev);
494 err = ext3_add_nondir(handle, dentry, inode);
496 ext3_journal_stop(handle, dir);
500 static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
503 struct inode * inode;
504 struct buffer_head * dir_block;
505 struct ext3_dir_entry_2 * de;
508 if (dir->i_nlink >= EXT3_LINK_MAX)
511 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);
513 return PTR_ERR(handle);
518 inode = ext3_new_inode (handle, dir, S_IFDIR);
519 err = PTR_ERR(inode);
523 inode->i_op = &ext3_dir_inode_operations;
524 inode->i_fop = &ext3_dir_operations;
525 inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;
527 dir_block = ext3_bread (handle, inode, 0, 1, &err);
529 inode->i_nlink--; /* is this nlink == 0? */
530 ext3_mark_inode_dirty(handle, inode);
534 BUFFER_TRACE(dir_block, "get_write_access");
535 ext3_journal_get_write_access(handle, dir_block);
536 de = (struct ext3_dir_entry_2 *) dir_block->b_data;
537 de->inode = cpu_to_le32(inode->i_ino);
539 de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));
540 strcpy (de->name, ".");
541 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
542 de = (struct ext3_dir_entry_2 *)
543 ((char *) de + le16_to_cpu(de->rec_len));
544 de->inode = cpu_to_le32(dir->i_ino);
545 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));
547 strcpy (de->name, "..");
548 ext3_set_de_type(dir->i_sb, de, S_IFDIR);
550 BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
551 ext3_journal_dirty_metadata(handle, dir_block);
553 inode->i_mode = S_IFDIR | mode;
554 if (dir->i_mode & S_ISGID)
555 inode->i_mode |= S_ISGID;
556 ext3_mark_inode_dirty(handle, inode);
557 err = ext3_add_entry (handle, dentry, inode);
561 dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
562 ext3_mark_inode_dirty(handle, dir);
563 d_instantiate(dentry, inode);
565 ext3_journal_stop(handle, dir);
570 ext3_mark_inode_dirty(handle, inode);
576 * routine to check that the specified directory is empty (for rmdir)
578 static int empty_dir (struct inode * inode)
580 unsigned long offset;
581 struct buffer_head * bh;
582 struct ext3_dir_entry_2 * de, * de1;
583 struct super_block * sb;
587 if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
588 !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
589 ext3_warning (inode->i_sb, "empty_dir",
590 "bad directory (dir #%lu) - no data block",
594 de = (struct ext3_dir_entry_2 *) bh->b_data;
595 de1 = (struct ext3_dir_entry_2 *)
596 ((char *) de + le16_to_cpu(de->rec_len));
597 if (le32_to_cpu(de->inode) != inode->i_ino ||
598 !le32_to_cpu(de1->inode) ||
599 strcmp (".", de->name) ||
600 strcmp ("..", de1->name)) {
601 ext3_warning (inode->i_sb, "empty_dir",
602 "bad directory (dir #%lu) - no `.' or `..'",
607 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
608 de = (struct ext3_dir_entry_2 *)
609 ((char *) de1 + le16_to_cpu(de1->rec_len));
610 while (offset < inode->i_size ) {
612 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
614 bh = ext3_bread (NULL, inode,
615 offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err);
618 ext3_error (sb, "empty_dir",
619 "directory #%lu contains a hole at offset %lu",
620 inode->i_ino, offset);
622 offset += sb->s_blocksize;
625 de = (struct ext3_dir_entry_2 *) bh->b_data;
627 if (!ext3_check_dir_entry ("empty_dir", inode, de, bh,
632 if (le32_to_cpu(de->inode)) {
636 offset += le16_to_cpu(de->rec_len);
637 de = (struct ext3_dir_entry_2 *)
638 ((char *) de + le16_to_cpu(de->rec_len));
644 /* ext3_orphan_add() links an unlinked or truncated inode into a list of
645 * such inodes, starting at the superblock, in case we crash before the
646 * file is closed/deleted, or in case the inode truncate spans multiple
647 * transactions and the last transaction is not recovered after a crash.
649 * At filesystem recovery time, we walk this list deleting unlinked
650 * inodes and truncating linked inodes in ext3_orphan_cleanup().
652 int ext3_orphan_add(handle_t *handle, struct inode *inode)
654 struct super_block *sb = inode->i_sb;
655 struct ext3_iloc iloc;
659 if (!list_empty(&inode->u.ext3_i.i_orphan))
662 /* Orphan handling is only valid for files with data blocks
663 * being truncated, or files being unlinked. */
665 /* @@@ FIXME: Observation from aviro:
666 * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
667 * here (on lock_super()), so race with ext3_link() which might bump
668 * ->i_nlink. For, say it, character device. Not a regular file,
669 * not a directory, not a symlink and ->i_nlink > 0.
671 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
672 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
674 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
675 err = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
679 err = ext3_reserve_inode_write(handle, inode, &iloc);
683 /* Insert this inode at the head of the on-disk orphan list... */
684 NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
685 EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
686 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
687 rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
691 /* Only add to the head of the in-memory list if all the
692 * previous operations succeeded. If the orphan_add is going to
693 * fail (possibly taking the journal offline), we can't risk
694 * leaving the inode on the orphan list: stray orphan-list
695 * entries can cause panics at unmount time.
697 * This is safe: on error we're going to ignore the orphan list
698 * anyway on the next recovery. */
700 list_add(&inode->u.ext3_i.i_orphan, &EXT3_SB(sb)->s_orphan);
702 jbd_debug(4, "superblock will point to %ld\n", inode->i_ino);
703 jbd_debug(4, "orphan inode %ld will point to %d\n",
704 inode->i_ino, NEXT_ORPHAN(inode));
707 ext3_std_error(inode->i_sb, err);
712 * ext3_orphan_del() removes an unlinked or truncated inode from the list
713 * of such inodes stored on disk, because it is finally being cleaned up.
715 int ext3_orphan_del(handle_t *handle, struct inode *inode)
717 struct list_head *prev;
718 struct ext3_sb_info *sbi;
719 unsigned long ino_next;
720 struct ext3_iloc iloc;
723 lock_super(inode->i_sb);
724 if (list_empty(&inode->u.ext3_i.i_orphan)) {
725 unlock_super(inode->i_sb);
729 ino_next = NEXT_ORPHAN(inode);
730 prev = inode->u.ext3_i.i_orphan.prev;
731 sbi = EXT3_SB(inode->i_sb);
733 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
735 list_del(&inode->u.ext3_i.i_orphan);
736 INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
738 /* If we're on an error path, we may not have a valid
739 * transaction handle with which to update the orphan list on
740 * disk, but we still need to remove the inode from the linked
745 err = ext3_reserve_inode_write(handle, inode, &iloc);
749 if (prev == &sbi->s_orphan) {
750 jbd_debug(4, "superblock will point to %lu\n", ino_next);
751 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
752 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
755 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
756 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
758 struct ext3_iloc iloc2;
759 struct inode *i_prev =
760 list_entry(prev, struct inode, u.ext3_i.i_orphan);
762 jbd_debug(4, "orphan inode %lu will point to %lu\n",
763 i_prev->i_ino, ino_next);
764 err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
767 NEXT_ORPHAN(i_prev) = ino_next;
768 err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
772 NEXT_ORPHAN(inode) = 0;
773 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
778 ext3_std_error(inode->i_sb, err);
780 unlock_super(inode->i_sb);
788 static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
791 struct inode * inode;
792 struct buffer_head * bh;
793 struct ext3_dir_entry_2 * de;
796 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
798 return PTR_ERR(handle);
801 bh = ext3_find_entry (dentry, &de);
808 inode = dentry->d_inode;
812 if (le32_to_cpu(de->inode) != inode->i_ino)
816 if (!empty_dir (inode))
819 retval = ext3_delete_entry(handle, dir, de, bh);
822 if (inode->i_nlink != 2)
823 ext3_warning (inode->i_sb, "ext3_rmdir",
824 "empty directory has nlink!=2 (%d)",
826 inode->i_version = ++event;
828 /* There's no need to set i_disksize: the fact that i_nlink is
829 * zero will ensure that the right thing happens during any
832 ext3_orphan_add(handle, inode);
834 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
835 ext3_mark_inode_dirty(handle, inode);
836 dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
837 ext3_mark_inode_dirty(handle, dir);
840 ext3_journal_stop(handle, dir);
845 static int ext3_unlink(struct inode * dir, struct dentry *dentry)
848 struct inode * inode;
849 struct buffer_head * bh;
850 struct ext3_dir_entry_2 * de;
853 handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS);
855 return PTR_ERR(handle);
861 bh = ext3_find_entry (dentry, &de);
865 inode = dentry->d_inode;
869 if (le32_to_cpu(de->inode) != inode->i_ino)
872 if (!inode->i_nlink) {
873 ext3_warning (inode->i_sb, "ext3_unlink",
874 "Deleting nonexistent file (%lu), %d",
875 inode->i_ino, inode->i_nlink);
878 retval = ext3_delete_entry(handle, dir, de, bh);
881 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
882 dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
883 ext3_mark_inode_dirty(handle, dir);
886 ext3_orphan_add(handle, inode);
887 inode->i_ctime = dir->i_ctime;
888 ext3_mark_inode_dirty(handle, inode);
892 ext3_journal_stop(handle, dir);
897 static int ext3_symlink (struct inode * dir,
898 struct dentry *dentry, const char * symname)
901 struct inode * inode;
904 l = strlen(symname)+1;
905 if (l > dir->i_sb->s_blocksize)
906 return -ENAMETOOLONG;
908 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 5);
910 return PTR_ERR(handle);
915 inode = ext3_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
916 err = PTR_ERR(inode);
920 if (l > sizeof (inode->u.ext3_i.i_data)) {
921 inode->i_op = &page_symlink_inode_operations;
922 inode->i_mapping->a_ops = &ext3_aops;
924 * block_symlink() calls back into ext3_prepare/commit_write.
925 * We have a transaction open. All is sweetness. It also sets
926 * i_size in generic_commit_write().
928 err = block_symlink(inode, symname, l);
932 inode->i_op = &ext3_fast_symlink_inode_operations;
933 memcpy((char*)&inode->u.ext3_i.i_data,symname,l);
936 inode->u.ext3_i.i_disksize = inode->i_size;
937 err = ext3_add_nondir(handle, dentry, inode);
939 ext3_journal_stop(handle, dir);
943 ext3_dec_count(handle, inode);
944 ext3_mark_inode_dirty(handle, inode);
949 static int ext3_link (struct dentry * old_dentry,
950 struct inode * dir, struct dentry *dentry)
953 struct inode *inode = old_dentry->d_inode;
956 if (S_ISDIR(inode->i_mode))
959 if (inode->i_nlink >= EXT3_LINK_MAX)
962 handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS);
964 return PTR_ERR(handle);
969 inode->i_ctime = CURRENT_TIME;
970 ext3_inc_count(handle, inode);
971 atomic_inc(&inode->i_count);
973 err = ext3_add_nondir(handle, dentry, inode);
974 ext3_journal_stop(handle, dir);
978 #define PARENT_INO(buffer) \
979 ((struct ext3_dir_entry_2 *) ((char *) buffer + \
980 le16_to_cpu(((struct ext3_dir_entry_2 *) buffer)->rec_len)))->inode
983 * Anybody can rename anything with this: the permission checks are left to the
984 * higher-level routines.
986 static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
987 struct inode * new_dir,struct dentry *new_dentry)
990 struct inode * old_inode, * new_inode;
991 struct buffer_head * old_bh, * new_bh, * dir_bh;
992 struct ext3_dir_entry_2 * old_de, * new_de;
995 old_bh = new_bh = dir_bh = NULL;
997 handle = ext3_journal_start(old_dir, 2 * EXT3_DATA_TRANS_BLOCKS + 2);
999 return PTR_ERR(handle);
1001 if (IS_SYNC(old_dir) || IS_SYNC(new_dir))
1004 old_bh = ext3_find_entry (old_dentry, &old_de);
1006 * Check for inode number is _not_ due to possible IO errors.
1007 * We might rmdir the source, keep it as pwd of some process
1008 * and merrily kill the link to whatever was created under the
1009 * same name. Goodbye sticky bit ;-<
1011 old_inode = old_dentry->d_inode;
1013 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
1016 new_inode = new_dentry->d_inode;
1017 new_bh = ext3_find_entry (new_dentry, &new_de);
1023 DQUOT_INIT(new_inode);
1026 if (S_ISDIR(old_inode->i_mode)) {
1028 retval = -ENOTEMPTY;
1029 if (!empty_dir (new_inode))
1033 dir_bh = ext3_bread (handle, old_inode, 0, 0, &retval);
1036 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
1039 if (!new_inode && new_dir!=old_dir &&
1040 new_dir->i_nlink >= EXT3_LINK_MAX)
1044 retval = ext3_add_entry (handle, new_dentry, old_inode);
1048 BUFFER_TRACE(new_bh, "get write access");
1049 BUFFER_TRACE(new_bh, "get_write_access");
1050 ext3_journal_get_write_access(handle, new_bh);
1051 new_de->inode = le32_to_cpu(old_inode->i_ino);
1052 if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
1053 EXT3_FEATURE_INCOMPAT_FILETYPE))
1054 new_de->file_type = old_de->file_type;
1055 new_dir->i_version = ++event;
1056 BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
1057 ext3_journal_dirty_metadata(handle, new_bh);
1063 * Like most other Unix systems, set the ctime for inodes on a
1066 old_inode->i_ctime = CURRENT_TIME;
1067 ext3_mark_inode_dirty(handle, old_inode);
1072 ext3_delete_entry(handle, old_dir, old_de, old_bh);
1075 new_inode->i_nlink--;
1076 new_inode->i_ctime = CURRENT_TIME;
1078 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1079 old_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1081 BUFFER_TRACE(dir_bh, "get_write_access");
1082 ext3_journal_get_write_access(handle, dir_bh);
1083 PARENT_INO(dir_bh->b_data) = le32_to_cpu(new_dir->i_ino);
1084 BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
1085 ext3_journal_dirty_metadata(handle, dir_bh);
1088 new_inode->i_nlink--;
1091 new_dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;
1092 ext3_mark_inode_dirty(handle, new_dir);
1095 ext3_mark_inode_dirty(handle, old_dir);
1097 ext3_mark_inode_dirty(handle, new_inode);
1098 if (!new_inode->i_nlink)
1099 ext3_orphan_add(handle, new_inode);
1107 ext3_journal_stop(handle, old_dir);
1112 * directories can handle most operations...
1114 struct inode_operations ext3_dir_inode_operations = {
1115 create: ext3_create, /* BKL held */
1116 lookup: ext3_lookup, /* BKL held */
1117 link: ext3_link, /* BKL held */
1118 unlink: ext3_unlink, /* BKL held */
1119 symlink: ext3_symlink, /* BKL held */
1120 mkdir: ext3_mkdir, /* BKL held */
1121 rmdir: ext3_rmdir, /* BKL held */
1122 mknod: ext3_mknod, /* BKL held */
1123 rename: ext3_rename, /* BKL held */