2 * mkdir.c --- make a directory in the filesystem
4 * Copyright (C) 1994, 1995 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
24 #include <sys/types.h>
34 errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
37 ext2_extent_handle_t handle;
39 struct ext2_inode parent_inode, inode;
40 ext2_ino_t ino = inum;
41 ext2_ino_t scratch_ino;
45 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
48 * Allocate an inode, if necessary
51 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
58 * Allocate a data block for the directory
60 retval = ext2fs_new_block2(fs, 0, 0, &blk);
65 * Create a scratch template for the directory
67 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
72 * Get the parent's inode, if necessary
75 retval = ext2fs_read_inode(fs, parent, &parent_inode);
79 memset(&parent_inode, 0, sizeof(parent_inode));
82 * Create the inode structure....
84 memset(&inode, 0, sizeof(struct ext2_inode));
85 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
86 inode.i_uid = inode.i_gid = 0;
87 ext2fs_iblk_set(fs, &inode, 1);
88 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
89 inode.i_flags |= EXT4_EXTENTS_FL;
91 inode.i_block[0] = blk;
92 inode.i_links_count = 2;
93 inode.i_size = fs->blocksize;
96 * Write out the inode and inode data block
98 retval = ext2fs_write_dir_block(fs, blk, block);
101 retval = ext2fs_write_new_inode(fs, ino, &inode);
105 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
106 retval = ext2fs_extent_open2(fs, ino, &inode, &handle);
109 retval = ext2fs_extent_set_bmap(handle, 0, blk, 0);
110 ext2fs_extent_free(handle);
116 * Link the directory into the filesystem hierarchy
119 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
122 retval = EXT2_ET_DIR_EXISTS;
126 if (retval != EXT2_ET_FILE_NOT_FOUND)
128 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
134 * Update parent inode's counts
137 parent_inode.i_links_count++;
138 retval = ext2fs_write_inode(fs, parent, &parent_inode);
144 * Update accounting....
146 ext2fs_block_alloc_stats2(fs, blk, +1);
147 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
151 ext2fs_free_mem(&block);