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