blob: 9b6b7996e3ccbce6cfc746a6aa9862bef97654df [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mkdir.c --- make a directory in the filesystem
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1994, 1995 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %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{
JP Abgralle0ed7402014-03-19 19:08:39 -070036 ext2_extent_handle_t handle;
Theodore Ts'o3839e651997-04-26 13:21:57 +000037 errcode_t retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000038 struct ext2_inode parent_inode, inode;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000039 ext2_ino_t ino = inum;
40 ext2_ino_t scratch_ino;
JP Abgralle0ed7402014-03-19 19:08:39 -070041 blk64_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 char *block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
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 */
JP Abgralle0ed7402014-03-19 19:08:39 -070059 retval = ext2fs_new_block2(fs, 0, 0, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 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'o6a525062001-12-24 09:40:00 -050084 inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 inode.i_uid = inode.i_gid = 0;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040086 ext2fs_iblk_set(fs, &inode, 1);
JP Abgralle0ed7402014-03-19 19:08:39 -070087 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
88 inode.i_flags |= EXT4_EXTENTS_FL;
89 else
90 inode.i_block[0] = blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 inode.i_links_count = 2;
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 inode.i_size = fs->blocksize;
93
94 /*
95 * Write out the inode and inode data block
96 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000097 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 if (retval)
99 goto cleanup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400100 retval = ext2fs_write_new_inode(fs, ino, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 if (retval)
102 goto cleanup;
103
JP Abgralle0ed7402014-03-19 19:08:39 -0700104 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
105 retval = ext2fs_extent_open2(fs, ino, &inode, &handle);
106 if (retval)
107 goto cleanup;
108 retval = ext2fs_extent_set_bmap(handle, 0, blk, 0);
109 ext2fs_extent_free(handle);
110 if (retval)
111 goto cleanup;
112 }
113
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 * Link the directory into the filesystem hierarchy
116 */
117 if (name) {
118 retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
119 &scratch_ino);
120 if (!retval) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000121 retval = EXT2_ET_DIR_EXISTS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 name = 0;
123 goto cleanup;
124 }
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000125 if (retval != EXT2_ET_FILE_NOT_FOUND)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 goto cleanup;
Theodore Ts'oe6198e51999-10-23 00:58:54 +0000127 retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 if (retval)
129 goto cleanup;
130 }
131
132 /*
Theodore Ts'odab278a1999-11-19 18:49:27 +0000133 * Update parent inode's counts
134 */
135 if (parent != ino) {
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000136 parent_inode.i_links_count++;
137 retval = ext2fs_write_inode(fs, parent, &parent_inode);
Theodore Ts'odab278a1999-11-19 18:49:27 +0000138 if (retval)
139 goto cleanup;
140 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400141
Theodore Ts'odab278a1999-11-19 18:49:27 +0000142 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 * Update accounting....
144 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700145 ext2fs_block_alloc_stats2(fs, blk, +1);
Theodore Ts'o7f961d42002-02-03 01:28:52 -0500146 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148cleanup:
149 if (block)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400150 ext2fs_free_mem(&block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 return retval;
152
153}
154
155