Whamcloud - gitweb
lib/{ext2fs,support}: fix 32-bit Windows build
[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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29 #include "ext2fsP.h"
30
31 #ifndef EXT2_FT_DIR
32 #define EXT2_FT_DIR             2
33 #endif
34
35 errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
36                        const char *name)
37 {
38         ext2_extent_handle_t    handle;
39         errcode_t               retval;
40         struct ext2_inode       parent_inode, inode;
41         ext2_ino_t              ino = inum;
42         ext2_ino_t              scratch_ino;
43         blk64_t                 blk;
44         char                    *block = 0;
45         int                     inline_data = 0;
46         int                     drop_refcount = 0;
47
48         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
49
50         /*
51          * Create a new dir with inline data iff this feature is enabled
52          * and ino >= EXT2_FIRST_INO.
53          */
54         if ((!ino || ino >= EXT2_FIRST_INO(fs->super)) &&
55             ext2fs_has_feature_inline_data(fs->super))
56                 inline_data = 1;
57
58         /*
59          * Allocate an inode, if necessary
60          */
61         if (!ino) {
62                 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
63                                           0, &ino);
64                 if (retval)
65                         goto cleanup;
66         }
67
68         /*
69          * Allocate a data block for the directory
70          */
71         memset(&inode, 0, sizeof(struct ext2_inode));
72         if (!inline_data) {
73                 retval = ext2fs_new_block2(fs, ext2fs_find_inode_goal(fs, ino,
74                                                                       &inode,
75                                                                       0),
76                                            NULL, &blk);
77                 if (retval)
78                         goto cleanup;
79         }
80
81         /*
82          * Create a scratch template for the directory
83          */
84         if (inline_data)
85                 retval = ext2fs_new_dir_inline_data(fs, ino, parent,
86                                                     inode.i_block);
87         else
88                 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
89         if (retval)
90                 goto cleanup;
91
92         /*
93          * Get the parent's inode, if necessary
94          */
95         if (parent != ino) {
96                 retval = ext2fs_read_inode(fs, parent, &parent_inode);
97                 if (retval)
98                         goto cleanup;
99         } else
100                 memset(&parent_inode, 0, sizeof(parent_inode));
101
102         /*
103          * Create the inode structure....
104          */
105         inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
106         inode.i_uid = inode.i_gid = 0;
107         if (inline_data) {
108                 inode.i_flags |= EXT4_INLINE_DATA_FL;
109                 inode.i_size = EXT4_MIN_INLINE_DATA_SIZE;
110         } else {
111                 if (ext2fs_has_feature_extents(fs->super))
112                         inode.i_flags |= EXT4_EXTENTS_FL;
113                 else
114                         inode.i_block[0] = blk;
115                 inode.i_size = fs->blocksize;
116                 ext2fs_iblk_set(fs, &inode, 1);
117         }
118         inode.i_links_count = 2;
119
120         /*
121          * Write out the inode and inode data block.  The inode generation
122          * number is assigned by write_new_inode, which means that the call
123          * to write_dir_block must come after that.
124          */
125         retval = ext2fs_write_new_inode(fs, ino, &inode);
126         if (retval)
127                 goto cleanup;
128         if (inline_data) {
129                 /* init "system.data" for new dir */
130                 retval = ext2fs_inline_data_init(fs, ino);
131         } else {
132                 retval = ext2fs_write_dir_block4(fs, blk, block, 0, ino);
133                 if (retval)
134                         goto cleanup;
135
136                 if (ext2fs_has_feature_extents(fs->super)) {
137                         retval = ext2fs_extent_open2(fs, ino, &inode, &handle);
138                         if (retval)
139                                 goto cleanup;
140                         retval = ext2fs_extent_set_bmap(handle, 0, blk, 0);
141                         ext2fs_extent_free(handle);
142                         if (retval)
143                                 goto cleanup;
144                 }
145         }
146
147         /*
148          * Update accounting....
149          */
150         if (!inline_data)
151                 ext2fs_block_alloc_stats2(fs, blk, +1);
152         ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
153         drop_refcount = 1;
154
155         /*
156          * Link the directory into the filesystem hierarchy
157          */
158         if (name) {
159                 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
160                                        &scratch_ino);
161                 if (!retval) {
162                         retval = EXT2_ET_DIR_EXISTS;
163                         name = 0;
164                         goto cleanup;
165                 }
166                 if (retval != EXT2_ET_FILE_NOT_FOUND)
167                         goto cleanup;
168                 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
169                 if (retval)
170                         goto cleanup;
171         }
172
173         /*
174          * Update parent inode's counts
175          */
176         if (parent != ino) {
177                 /* reload parent inode due to inline data */
178                 retval = ext2fs_read_inode(fs, parent, &parent_inode);
179                 if (retval)
180                         goto cleanup;
181                 parent_inode.i_links_count++;
182                 retval = ext2fs_write_inode(fs, parent, &parent_inode);
183                 if (retval)
184                         goto cleanup;
185         }
186         drop_refcount = 0;
187
188 cleanup:
189         if (block)
190                 ext2fs_free_mem(&block);
191         if (drop_refcount) {
192                 if (!inline_data)
193                         ext2fs_block_alloc_stats2(fs, blk, -1);
194                 ext2fs_inode_alloc_stats2(fs, ino, -1, 1);
195         }
196         return retval;
197
198 }
199
200