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
21 #include <sys/types.h>
26 #include <linux/ext2_fs.h>
30 errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
34 struct ext2_inode inode;
41 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44 * Allocate an inode, if necessary
47 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
54 * Allocate a data block for the directory
56 retval = ext2fs_new_block(fs, 0, 0, &blk);
61 * Create a scratch template for the directory
63 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
68 * Create the inode structure....
70 memset(&inode, 0, sizeof(struct ext2_inode));
71 inode.i_mode = LINUX_S_IFDIR | 0755;
72 inode.i_uid = inode.i_gid = 0;
73 inode.i_blocks = fs->blocksize / 512;
74 inode.i_block[0] = blk;
75 inode.i_links_count = 2;
76 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
77 inode.i_size = fs->blocksize;
80 * Write out the inode and inode data block
82 retval = ext2fs_write_dir_block(fs, blk, block);
85 retval = ext2fs_write_inode(fs, ino, &inode);
90 * Update parent inode's counts
93 retval = ext2fs_read_inode(fs, parent, &inode);
96 inode.i_links_count++;
97 retval = ext2fs_write_inode(fs, parent, &inode);
103 * Link the directory into the filesystem hierarchy
106 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
113 if (retval != ENOENT)
115 retval = ext2fs_link(fs, parent, name, ino, 0);
121 * Update accounting....
123 ext2fs_mark_block_bitmap(fs->block_map, blk);
124 ext2fs_mark_bb_dirty(fs);
125 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
126 ext2fs_mark_ib_dirty(fs);
128 group = ext2fs_group_of_blk(fs, blk);
129 fs->group_desc[group].bg_free_blocks_count--;
130 group = ext2fs_group_of_ino(fs, ino);
131 fs->group_desc[group].bg_free_inodes_count--;
132 fs->group_desc[group].bg_used_dirs_count++;
133 fs->super->s_free_blocks_count--;
134 fs->super->s_free_inodes_count--;
135 ext2fs_mark_super_dirty(fs);