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>
35 errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
38 ext2_extent_handle_t handle;
40 struct ext2_inode parent_inode, inode;
41 ext2_ino_t ino = inum;
42 ext2_ino_t scratch_ino;
46 int drop_refcount = 0;
48 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
51 * Create a new dir with inline data iff this feature is enabled
52 * and ino >= EXT2_FIRST_INO.
54 if ((!ino || ino >= EXT2_FIRST_INO(fs->super)) &&
55 ext2fs_has_feature_inline_data(fs->super))
59 * Allocate an inode, if necessary
62 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
69 * Allocate a data block for the directory
71 memset(&inode, 0, sizeof(struct ext2_inode));
73 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
82 * Create a scratch template for the directory
85 retval = ext2fs_new_dir_inline_data(fs, ino, parent,
88 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
93 * Get the parent's inode, if necessary
96 retval = ext2fs_read_inode(fs, parent, &parent_inode);
100 memset(&parent_inode, 0, sizeof(parent_inode));
103 * Create the inode structure....
105 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
106 inode.i_uid = inode.i_gid = 0;
108 inode.i_flags |= EXT4_INLINE_DATA_FL;
109 inode.i_size = EXT4_MIN_INLINE_DATA_SIZE;
111 if (ext2fs_has_feature_extents(fs->super))
112 inode.i_flags |= EXT4_EXTENTS_FL;
114 inode.i_block[0] = blk;
115 inode.i_size = fs->blocksize;
116 ext2fs_iblk_set(fs, &inode, 1);
118 inode.i_links_count = 2;
121 * Write out the inode and inode data block. The inode generation
122 * number is assigned by write_new_inode, which means that the call
123 * to write_dir_block must come after that.
125 retval = ext2fs_write_new_inode(fs, ino, &inode);
129 /* init "system.data" for new dir */
130 retval = ext2fs_inline_data_init(fs, ino);
132 retval = ext2fs_write_dir_block4(fs, blk, block, 0, ino);
136 if (ext2fs_has_feature_extents(fs->super)) {
137 retval = ext2fs_extent_open2(fs, ino, &inode, &handle);
140 retval = ext2fs_extent_set_bmap(handle, 0, blk, 0);
141 ext2fs_extent_free(handle);
148 * Update accounting....
151 ext2fs_block_alloc_stats2(fs, blk, +1);
152 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
156 * Link the directory into the filesystem hierarchy
159 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
162 retval = EXT2_ET_DIR_EXISTS;
166 if (retval != EXT2_ET_FILE_NOT_FOUND)
168 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
174 * Update parent inode's counts
177 /* reload parent inode due to inline data */
178 retval = ext2fs_read_inode(fs, parent, &parent_inode);
181 parent_inode.i_links_count++;
182 retval = ext2fs_write_inode(fs, parent, &parent_inode);
190 ext2fs_free_mem(&block);
193 ext2fs_block_alloc_stats2(fs, blk, -1);
194 ext2fs_inode_alloc_stats2(fs, ino, -1, 1);