blob: 9348cb7ce7908561cd952f2e727162729ada27dd [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mkdir.c --- make a directory in the filesystem
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <stdlib.h>
18#include <fcntl.h>
19#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <linux/ext2_fs.h>
28
29#include "ext2fs.h"
30
31errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
32 const char *name)
33{
34 errcode_t retval;
35 struct ext2_inode inode;
36 ino_t ino = inum;
37 ino_t scratch_ino;
38 blk_t blk;
39 char *block = 0;
40 int group;
41
Theodore Ts'of3db3561997-04-26 13:34:30 +000042 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43
Theodore Ts'o3839e651997-04-26 13:21:57 +000044 /*
45 * Allocate an inode, if necessary
46 */
47 if (!ino) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000048 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
49 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000050 if (retval)
51 goto cleanup;
52 }
53
54 /*
55 * Allocate a data block for the directory
56 */
57 retval = ext2fs_new_block(fs, 0, 0, &blk);
58 if (retval)
59 goto cleanup;
60
61 /*
62 * Create a scratch template for the directory
63 */
64 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
65 if (retval)
66 goto cleanup;
67
68 /*
69 * Create the inode structure....
70 */
71 memset(&inode, 0, sizeof(struct ext2_inode));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000072 inode.i_mode = LINUX_S_IFDIR | 0755;
Theodore Ts'o3839e651997-04-26 13:21:57 +000073 inode.i_uid = inode.i_gid = 0;
74 inode.i_blocks = fs->blocksize / 512;
75 inode.i_block[0] = blk;
76 inode.i_links_count = 2;
77 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
78 inode.i_size = fs->blocksize;
79
80 /*
81 * Write out the inode and inode data block
82 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000083 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 if (retval)
85 goto cleanup;
86 retval = ext2fs_write_inode(fs, ino, &inode);
87 if (retval)
88 goto cleanup;
89
90 /*
91 * Update parent inode's counts
92 */
93 if (parent != ino) {
94 retval = ext2fs_read_inode(fs, parent, &inode);
95 if (retval)
96 goto cleanup;
97 inode.i_links_count++;
98 retval = ext2fs_write_inode(fs, parent, &inode);
99 if (retval)
100 goto cleanup;
101 }
102
103 /*
104 * Link the directory into the filesystem hierarchy
105 */
106 if (name) {
107 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
108 &scratch_ino);
109 if (!retval) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000110 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 name = 0;
112 goto cleanup;
113 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000114 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 goto cleanup;
116 retval = ext2fs_link(fs, parent, name, ino, 0);
117 if (retval)
118 goto cleanup;
119 }
120
121 /*
122 * Update accounting....
123 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000124 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000126 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127 ext2fs_mark_ib_dirty(fs);
128
129 group = ext2fs_group_of_blk(fs, blk);
130 fs->group_desc[group].bg_free_blocks_count--;
131 group = ext2fs_group_of_ino(fs, ino);
132 fs->group_desc[group].bg_free_inodes_count--;
133 fs->group_desc[group].bg_used_dirs_count++;
134 fs->super->s_free_blocks_count--;
135 fs->super->s_free_inodes_count--;
136 ext2fs_mark_super_dirty(fs);
137
138cleanup:
139 if (block)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000140 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 return retval;
142
143}
144
145