blob: 15a6863f13483ba2550100422274adb2eb386b0a [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#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include "ext2fs.h"
28
Theodore Ts'oe6198e51999-10-23 00:58:54 +000029#ifndef EXT2_FT_DIR
30#define EXT2_FT_DIR 2
31#endif
32
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000033errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
Theodore Ts'o3839e651997-04-26 13:21:57 +000034 const char *name)
35{
36 errcode_t retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000037 struct ext2_inode parent_inode, inode;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000038 ext2_ino_t ino = inum;
39 ext2_ino_t scratch_ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000040 blk_t blk;
41 char *block = 0;
42 int group;
43
Theodore Ts'of3db3561997-04-26 13:34:30 +000044 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45
Theodore Ts'o3839e651997-04-26 13:21:57 +000046 /*
47 * Allocate an inode, if necessary
48 */
49 if (!ino) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000050 retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
51 0, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 if (retval)
53 goto cleanup;
54 }
55
56 /*
57 * Allocate a data block for the directory
58 */
59 retval = ext2fs_new_block(fs, 0, 0, &blk);
60 if (retval)
61 goto cleanup;
62
63 /*
64 * Create a scratch template for the directory
65 */
66 retval = ext2fs_new_dir_block(fs, ino, parent, &block);
67 if (retval)
68 goto cleanup;
69
70 /*
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000071 * Get the parent's inode, if necessary
72 */
73 if (parent != ino) {
74 retval = ext2fs_read_inode(fs, parent, &parent_inode);
75 if (retval)
76 goto cleanup;
77 } else
78 memset(&parent_inode, 0, sizeof(parent_inode));
79
80 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 * Create the inode structure....
82 */
83 memset(&inode, 0, sizeof(struct ext2_inode));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000084 inode.i_mode = LINUX_S_IFDIR | 0755;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 inode.i_uid = inode.i_gid = 0;
86 inode.i_blocks = fs->blocksize / 512;
87 inode.i_block[0] = blk;
88 inode.i_links_count = 2;
89 inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
90 inode.i_size = fs->blocksize;
91
92 /*
93 * Write out the inode and inode data block
94 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000095 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 if (retval)
97 goto cleanup;
98 retval = ext2fs_write_inode(fs, ino, &inode);
99 if (retval)
100 goto cleanup;
101
102 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 * 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) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000109 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 name = 0;
111 goto cleanup;
112 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000113 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 goto cleanup;
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000115 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (retval)
117 goto cleanup;
118 }
119
120 /*
Theodore Ts'odab278a1999-11-19 18:49:27 +0000121 * Update parent inode's counts
122 */
123 if (parent != ino) {
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000124 parent_inode.i_links_count++;
125 retval = ext2fs_write_inode(fs, parent, &parent_inode);
Theodore Ts'odab278a1999-11-19 18:49:27 +0000126 if (retval)
127 goto cleanup;
128 }
129
130 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131 * Update accounting....
132 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000133 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 ext2fs_mark_bb_dirty(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000135 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 ext2fs_mark_ib_dirty(fs);
137
138 group = ext2fs_group_of_blk(fs, blk);
139 fs->group_desc[group].bg_free_blocks_count--;
140 group = ext2fs_group_of_ino(fs, ino);
141 fs->group_desc[group].bg_free_inodes_count--;
142 fs->group_desc[group].bg_used_dirs_count++;
143 fs->super->s_free_blocks_count--;
144 fs->super->s_free_inodes_count--;
145 ext2fs_mark_super_dirty(fs);
146
147cleanup:
148 if (block)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000149 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 return retval;
151
152}
153
154