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 Public
23 #include <sys/types.h>
26 #if EXT2_FLAT_INCLUDES
29 #include <linux/ext2_fs.h>
38 errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
42 struct ext2_inode parent_inode, inode;
43 ext2_ino_t ino = inum;
44 ext2_ino_t scratch_ino;
49 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
52 * Allocate an inode, if necessary
55 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
62 * Allocate a data block for the directory
64 retval = ext2fs_new_block(fs, 0, 0, &blk);
69 * Create a scratch template for the directory
71 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
76 * Get the parent's inode, if necessary
79 retval = ext2fs_read_inode(fs, parent, &parent_inode);
83 memset(&parent_inode, 0, sizeof(parent_inode));
86 * Create the inode structure....
88 memset(&inode, 0, sizeof(struct ext2_inode));
89 inode.i_mode = LINUX_S_IFDIR | 0755;
90 inode.i_uid = inode.i_gid = 0;
91 inode.i_blocks = fs->blocksize / 512;
92 inode.i_block[0] = blk;
93 inode.i_links_count = 2;
94 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
95 inode.i_size = fs->blocksize;
98 * Write out the inode and inode data block
100 retval = ext2fs_write_dir_block(fs, blk, block);
103 retval = ext2fs_write_inode(fs, ino, &inode);
108 * Link the directory into the filesystem hierarchy
111 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
114 retval = EXT2_ET_DIR_EXISTS;
118 if (retval != EXT2_ET_FILE_NOT_FOUND)
120 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
126 * Update parent inode's counts
129 parent_inode.i_links_count++;
130 retval = ext2fs_write_inode(fs, parent, &parent_inode);
136 * Update accounting....
138 ext2fs_mark_block_bitmap(fs->block_map, blk);
139 ext2fs_mark_bb_dirty(fs);
140 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
141 ext2fs_mark_ib_dirty(fs);
143 group = ext2fs_group_of_blk(fs, blk);
144 fs->group_desc[group].bg_free_blocks_count--;
145 group = ext2fs_group_of_ino(fs, ino);
146 fs->group_desc[group].bg_free_inodes_count--;
147 fs->group_desc[group].bg_used_dirs_count++;
148 fs->super->s_free_blocks_count--;
149 fs->super->s_free_inodes_count--;
150 ext2fs_mark_super_dirty(fs);
154 ext2fs_free_mem((void **) &block);