Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / mkdir.c
1 /*
2  * mkdir.c --- make a directory in the filesystem
3  * 
4  * 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%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #if HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25
26 #include <linux/ext2_fs.h>
27
28 #include "ext2fs.h"
29
30 errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
31                        const char *name)
32 {
33         errcode_t               retval;
34         struct ext2_inode       inode;
35         ino_t                   ino = inum;
36         ino_t                   scratch_ino;
37         blk_t                   blk;
38         char                    *block = 0;
39         int                     group;
40
41         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
42
43         /*
44          * Allocate an inode, if necessary
45          */
46         if (!ino) {
47                 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
48                                           0, &ino);
49                 if (retval)
50                         goto cleanup;
51         }
52
53         /*
54          * Allocate a data block for the directory
55          */
56         retval = ext2fs_new_block(fs, 0, 0, &blk);
57         if (retval)
58                 goto cleanup;
59
60         /*
61          * Create a scratch template for the directory
62          */
63         retval = ext2fs_new_dir_block(fs, ino, parent, &block);
64         if (retval)
65                 goto cleanup;
66
67         /*
68          * Create the inode structure....
69          */
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;
78
79         /*
80          * Write out the inode and inode data block
81          */
82         retval = ext2fs_write_dir_block(fs, blk, block);
83         if (retval)
84                 goto cleanup;
85         retval = ext2fs_write_inode(fs, ino, &inode); 
86         if (retval)
87                 goto cleanup;
88
89         /*
90          * Update parent inode's counts
91          */
92         if (parent != ino) {
93                 retval = ext2fs_read_inode(fs, parent, &inode);
94                 if (retval)
95                         goto cleanup;
96                 inode.i_links_count++;
97                 retval = ext2fs_write_inode(fs, parent, &inode);
98                 if (retval)
99                         goto cleanup;
100         }
101         
102         /*
103          * Link the directory into the filesystem hierarchy
104          */
105         if (name) {
106                 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
107                                        &scratch_ino);
108                 if (!retval) {
109                         retval = EEXIST;
110                         name = 0;
111                         goto cleanup;
112                 }
113                 if (retval != ENOENT)
114                         goto cleanup;
115                 retval = ext2fs_link(fs, parent, name, ino, 0);
116                 if (retval)
117                         goto cleanup;
118         }
119
120         /*
121          * Update accounting....
122          */
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);
127
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);
136         
137 cleanup:
138         if (block)
139                 free(block);
140         return retval;
141
142 }
143
144