blob: 98c5ccf2cb40a74b03643dfa8f112e2f55d5f061 [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 <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000031
32#include "ext2fs.h"
33
Theodore Ts'oe6198e51999-10-23 00:58:54 +000034#ifndef EXT2_FT_DIR
35#define EXT2_FT_DIR 2
36#endif
37
Theodore Ts'o3839e651997-04-26 13:21:57 +000038errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
39 const char *name)
40{
41 errcode_t retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000042 struct ext2_inode parent_inode, inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 ino_t ino = inum;
44 ino_t scratch_ino;
45 blk_t blk;
46 char *block = 0;
47 int group;
48
Theodore Ts'of3db3561997-04-26 13:34:30 +000049 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
50
Theodore Ts'o3839e651997-04-26 13:21:57 +000051 /*
52 * Allocate an inode, if necessary
53 */
54 if (!ino) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000055 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
56 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 if (retval)
58 goto cleanup;
59 }
60
61 /*
62 * Allocate a data block for the directory
63 */
64 retval = ext2fs_new_block(fs, 0, 0, &blk);
65 if (retval)
66 goto cleanup;
67
68 /*
69 * Create a scratch template for the directory
70 */
71 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
72 if (retval)
73 goto cleanup;
74
75 /*
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000076 * Get the parent's inode, if necessary
77 */
78 if (parent != ino) {
79 retval = ext2fs_read_inode(fs, parent, &parent_inode);
80 if (retval)
81 goto cleanup;
82 } else
83 memset(&parent_inode, 0, sizeof(parent_inode));
84
85 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 * Create the inode structure....
87 */
88 memset(&inode, 0, sizeof(struct ext2_inode));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000089 inode.i_mode = LINUX_S_IFDIR | 0755;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 inode.i_uid = inode.i_gid = 0;
91 inode.i_blocks = fs->blocksize / 512;
92 inode.i_block[0] = blk;
93 inode.i_links_count = 2;
94 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
95 inode.i_size = fs->blocksize;
96
97 /*
98 * Write out the inode and inode data block
99 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000100 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 if (retval)
102 goto cleanup;
103 retval = ext2fs_write_inode(fs, ino, &inode);
104 if (retval)
105 goto cleanup;
106
107 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 * Link the directory into the filesystem hierarchy
109 */
110 if (name) {
111 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
112 &scratch_ino);
113 if (!retval) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000114 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 name = 0;
116 goto cleanup;
117 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000118 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 goto cleanup;
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000120 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 if (retval)
122 goto cleanup;
123 }
124
125 /*
Theodore Ts'odab278a1999-11-19 18:49:27 +0000126 * Update parent inode's counts
127 */
128 if (parent != ino) {
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000129 parent_inode.i_links_count++;
130 retval = ext2fs_write_inode(fs, parent, &parent_inode);
Theodore Ts'odab278a1999-11-19 18:49:27 +0000131 if (retval)
132 goto cleanup;
133 }
134
135 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 * Update accounting....
137 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000140 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 ext2fs_mark_ib_dirty(fs);
142
143 group = ext2fs_group_of_blk(fs, blk);
144 fs->group_desc[group].bg_free_blocks_count--;
145 group = ext2fs_group_of_ino(fs, ino);
146 fs->group_desc[group].bg_free_inodes_count--;
147 fs->group_desc[group].bg_used_dirs_count++;
148 fs->super->s_free_blocks_count--;
149 fs->super->s_free_inodes_count--;
150 ext2fs_mark_super_dirty(fs);
151
152cleanup:
153 if (block)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000154 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 return retval;
156
157}
158
159