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;
47 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
50 * Create a new dir with inline data iff this feature is enabled
51 * and ino >= EXT2_FIRST_INO.
53 if ((!ino || ino >= EXT2_FIRST_INO(fs->super)) &&
54 ext2fs_has_feature_inline_data(fs->super))
58 * Allocate an inode, if necessary
61 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
68 * Allocate a data block for the directory
70 memset(&inode, 0, sizeof(struct ext2_inode));
72 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
81 * Create a scratch template for the directory
84 retval = ext2fs_new_dir_inline_data(fs, ino, parent,
87 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
92 * Get the parent's inode, if necessary
95 retval = ext2fs_read_inode(fs, parent, &parent_inode);
99 memset(&parent_inode, 0, sizeof(parent_inode));
102 * Create the inode structure....
104 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
105 inode.i_uid = inode.i_gid = 0;
107 inode.i_flags |= EXT4_INLINE_DATA_FL;
108 inode.i_size = EXT4_MIN_INLINE_DATA_SIZE;
110 if (ext2fs_has_feature_extents(fs->super))
111 inode.i_flags |= EXT4_EXTENTS_FL;
113 inode.i_block[0] = blk;
114 inode.i_size = fs->blocksize;
115 ext2fs_iblk_set(fs, &inode, 1);
117 inode.i_links_count = 2;
120 * Write out the inode and inode data block. The inode generation
121 * number is assigned by write_new_inode, which means that the call
122 * to write_dir_block must come after that.
124 retval = ext2fs_write_new_inode(fs, ino, &inode);
128 /* init "system.data" for new dir */
129 retval = ext2fs_inline_data_init(fs, ino);
131 retval = ext2fs_write_dir_block4(fs, blk, block, 0, ino);
135 if (ext2fs_has_feature_extents(fs->super)) {
136 retval = ext2fs_extent_open2(fs, ino, &inode, &handle);
139 retval = ext2fs_extent_set_bmap(handle, 0, blk, 0);
140 ext2fs_extent_free(handle);
147 * Link the directory into the filesystem hierarchy
150 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
153 retval = EXT2_ET_DIR_EXISTS;
157 if (retval != EXT2_ET_FILE_NOT_FOUND)
159 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
165 * Update parent inode's counts
168 /* reload parent inode due to inline data */
169 retval = ext2fs_read_inode(fs, parent, &parent_inode);
172 parent_inode.i_links_count++;
173 retval = ext2fs_write_inode(fs, parent, &parent_inode);
179 * Update accounting....
182 ext2fs_block_alloc_stats2(fs, blk, +1);
183 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
187 ext2fs_free_mem(&block);