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>
33 errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
37 struct ext2_inode parent_inode, inode;
38 ext2_ino_t ino = inum;
39 ext2_ino_t scratch_ino;
44 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
47 * Allocate an inode, if necessary
50 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
57 * Allocate a data block for the directory
59 retval = ext2fs_new_block(fs, 0, 0, &blk);
64 * Create a scratch template for the directory
66 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
71 * Get the parent's inode, if necessary
74 retval = ext2fs_read_inode(fs, parent, &parent_inode);
78 memset(&parent_inode, 0, sizeof(parent_inode));
81 * Create the inode structure....
83 memset(&inode, 0, sizeof(struct ext2_inode));
84 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
85 inode.i_uid = inode.i_gid = 0;
86 inode.i_blocks = fs->blocksize / 512;
87 inode.i_block[0] = blk;
88 inode.i_links_count = 2;
89 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
90 inode.i_size = fs->blocksize;
93 * Write out the inode and inode data block
95 retval = ext2fs_write_dir_block(fs, blk, block);
98 retval = ext2fs_write_inode(fs, ino, &inode);
103 * Link the directory into the filesystem hierarchy
106 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
109 retval = EXT2_ET_DIR_EXISTS;
113 if (retval != EXT2_ET_FILE_NOT_FOUND)
115 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
121 * Update parent inode's counts
124 parent_inode.i_links_count++;
125 retval = ext2fs_write_inode(fs, parent, &parent_inode);
131 * Update accounting....
133 ext2fs_mark_block_bitmap(fs->block_map, blk);
134 ext2fs_mark_bb_dirty(fs);
135 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
136 ext2fs_mark_ib_dirty(fs);
138 group = ext2fs_group_of_blk(fs, blk);
139 fs->group_desc[group].bg_free_blocks_count--;
140 group = ext2fs_group_of_ino(fs, ino);
141 fs->group_desc[group].bg_free_inodes_count--;
142 fs->group_desc[group].bg_used_dirs_count++;
143 fs->super->s_free_blocks_count--;
144 fs->super->s_free_inodes_count--;
145 ext2fs_mark_super_dirty(fs);
149 ext2fs_free_mem((void **) &block);