2 * mkdir.c --- make a directory in the filesystem
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
15 #include <sys/types.h>
17 #include <linux/ext2_fs.h>
21 errcode_t
ext2fs_mkdir(ext2_filsys fs
, ino_t parent
, ino_t inum
,
25 struct ext2_inode inode
;
32 EXT2_CHECK_MAGIC(fs
, EXT2_ET_MAGIC_EXT2FS_FILSYS
);
35 * Allocate an inode, if necessary
38 retval
= ext2fs_new_inode(fs
, parent
, S_IFDIR
| 0755, 0, &ino
);
44 * Allocate a data block for the directory
46 retval
= ext2fs_new_block(fs
, 0, 0, &blk
);
51 * Create a scratch template for the directory
53 retval
= ext2fs_new_dir_block(fs
, ino
, parent
, &block
);
58 * Create the inode structure....
60 memset(&inode
, 0, sizeof(struct ext2_inode
));
61 inode
.i_mode
= S_IFDIR
| 0755;
62 inode
.i_uid
= inode
.i_gid
= 0;
63 inode
.i_blocks
= fs
->blocksize
/ 512;
64 inode
.i_block
[0] = blk
;
65 inode
.i_links_count
= 2;
66 inode
.i_ctime
= inode
.i_atime
= inode
.i_mtime
= time(NULL
);
67 inode
.i_size
= fs
->blocksize
;
70 * Write out the inode and inode data block
72 retval
= io_channel_write_blk(fs
->io
, blk
, 1, block
);
75 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
80 * Update parent inode's counts
83 retval
= ext2fs_read_inode(fs
, parent
, &inode
);
86 inode
.i_links_count
++;
87 retval
= ext2fs_write_inode(fs
, parent
, &inode
);
93 * Link the directory into the filesystem hierarchy
96 retval
= ext2fs_lookup(fs
, parent
, name
, strlen(name
), 0,
103 if (retval
!= ENOENT
)
105 retval
= ext2fs_link(fs
, parent
, name
, ino
, 0);
111 * Update accounting....
113 ext2fs_mark_block_bitmap(fs
->block_map
, blk
);
114 ext2fs_mark_bb_dirty(fs
);
115 ext2fs_mark_inode_bitmap(fs
->inode_map
, ino
);
116 ext2fs_mark_ib_dirty(fs
);
118 group
= ext2fs_group_of_blk(fs
, blk
);
119 fs
->group_desc
[group
].bg_free_blocks_count
--;
120 group
= ext2fs_group_of_ino(fs
, ino
);
121 fs
->group_desc
[group
].bg_free_inodes_count
--;
122 fs
->group_desc
[group
].bg_used_dirs_count
++;
123 fs
->super
->s_free_blocks_count
--;
124 fs
->super
->s_free_inodes_count
--;
125 ext2fs_mark_super_dirty(fs
);