blob: b2e8094db37b226b67c70f40ee97b5a78f4ab014 [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
34errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
35 const char *name)
36{
37 errcode_t retval;
38 struct ext2_inode inode;
39 ino_t ino = inum;
40 ino_t scratch_ino;
41 blk_t blk;
42 char *block = 0;
43 int group;
44
Theodore Ts'of3db3561997-04-26 13:34:30 +000045 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
46
Theodore Ts'o3839e651997-04-26 13:21:57 +000047 /*
48 * Allocate an inode, if necessary
49 */
50 if (!ino) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000051 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
52 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000053 if (retval)
54 goto cleanup;
55 }
56
57 /*
58 * Allocate a data block for the directory
59 */
60 retval = ext2fs_new_block(fs, 0, 0, &blk);
61 if (retval)
62 goto cleanup;
63
64 /*
65 * Create a scratch template for the directory
66 */
67 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
68 if (retval)
69 goto cleanup;
70
71 /*
72 * Create the inode structure....
73 */
74 memset(&inode, 0, sizeof(struct ext2_inode));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000075 inode.i_mode = LINUX_S_IFDIR | 0755;
Theodore Ts'o3839e651997-04-26 13:21:57 +000076 inode.i_uid = inode.i_gid = 0;
77 inode.i_blocks = fs->blocksize / 512;
78 inode.i_block[0] = blk;
79 inode.i_links_count = 2;
80 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
81 inode.i_size = fs->blocksize;
82
83 /*
84 * Write out the inode and inode data block
85 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000086 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 if (retval)
88 goto cleanup;
89 retval = ext2fs_write_inode(fs, ino, &inode);
90 if (retval)
91 goto cleanup;
92
93 /*
94 * Update parent inode's counts
95 */
96 if (parent != ino) {
97 retval = ext2fs_read_inode(fs, parent, &inode);
98 if (retval)
99 goto cleanup;
100 inode.i_links_count++;
101 retval = ext2fs_write_inode(fs, parent, &inode);
102 if (retval)
103 goto cleanup;
104 }
105
106 /*
107 * Link the directory into the filesystem hierarchy
108 */
109 if (name) {
110 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
111 &scratch_ino);
112 if (!retval) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000113 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 name = 0;
115 goto cleanup;
116 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000117 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 goto cleanup;
119 retval = ext2fs_link(fs, parent, name, ino, 0);
120 if (retval)
121 goto cleanup;
122 }
123
124 /*
125 * Update accounting....
126 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000127 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000129 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130 ext2fs_mark_ib_dirty(fs);
131
132 group = ext2fs_group_of_blk(fs, blk);
133 fs->group_desc[group].bg_free_blocks_count--;
134 group = ext2fs_group_of_ino(fs, ino);
135 fs->group_desc[group].bg_free_inodes_count--;
136 fs->group_desc[group].bg_used_dirs_count++;
137 fs->super->s_free_blocks_count--;
138 fs->super->s_free_inodes_count--;
139 ext2fs_mark_super_dirty(fs);
140
141cleanup:
142 if (block)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000143 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 return retval;
145
146}
147
148