Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mkdir.c --- make a directory in the filesystem |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 4 | * 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'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 19 | #if HAVE_SYS_STAT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #if EXT2_FLAT_INCLUDES |
| 27 | #include "ext2_fs.h" |
| 28 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 30 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | |
| 32 | #include "ext2fs.h" |
| 33 | |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame] | 34 | #ifndef EXT2_FT_DIR |
| 35 | #define EXT2_FT_DIR 2 |
| 36 | #endif |
| 37 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum, |
| 39 | const char *name) |
| 40 | { |
| 41 | errcode_t retval; |
| 42 | struct ext2_inode inode; |
| 43 | ino_t ino = inum; |
| 44 | ino_t scratch_ino; |
| 45 | blk_t blk; |
| 46 | char *block = 0; |
| 47 | int group; |
| 48 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 49 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 50 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | /* |
| 52 | * Allocate an inode, if necessary |
| 53 | */ |
| 54 | if (!ino) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 55 | retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755, |
| 56 | 0, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | 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 | /* |
| 76 | * Create the inode structure.... |
| 77 | */ |
| 78 | memset(&inode, 0, sizeof(struct ext2_inode)); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 79 | inode.i_mode = LINUX_S_IFDIR | 0755; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | inode.i_uid = inode.i_gid = 0; |
| 81 | inode.i_blocks = fs->blocksize / 512; |
| 82 | inode.i_block[0] = blk; |
| 83 | inode.i_links_count = 2; |
| 84 | inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL); |
| 85 | inode.i_size = fs->blocksize; |
| 86 | |
| 87 | /* |
| 88 | * Write out the inode and inode data block |
| 89 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 90 | retval = ext2fs_write_dir_block(fs, blk, block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 91 | if (retval) |
| 92 | goto cleanup; |
| 93 | retval = ext2fs_write_inode(fs, ino, &inode); |
| 94 | if (retval) |
| 95 | goto cleanup; |
| 96 | |
| 97 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | * Link the directory into the filesystem hierarchy |
| 99 | */ |
| 100 | if (name) { |
| 101 | retval = ext2fs_lookup(fs, parent, name, strlen(name), 0, |
| 102 | &scratch_ino); |
| 103 | if (!retval) { |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 104 | retval = EXT2_ET_DIR_EXISTS; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | name = 0; |
| 106 | goto cleanup; |
| 107 | } |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 108 | if (retval != EXT2_ET_FILE_NOT_FOUND) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | goto cleanup; |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame] | 110 | retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 111 | if (retval) |
| 112 | goto cleanup; |
| 113 | } |
| 114 | |
| 115 | /* |
Theodore Ts'o | dab278a | 1999-11-19 18:49:27 +0000 | [diff] [blame^] | 116 | * Update parent inode's counts |
| 117 | */ |
| 118 | if (parent != ino) { |
| 119 | retval = ext2fs_read_inode(fs, parent, &inode); |
| 120 | if (retval) |
| 121 | goto cleanup; |
| 122 | inode.i_links_count++; |
| 123 | retval = ext2fs_write_inode(fs, parent, &inode); |
| 124 | if (retval) |
| 125 | goto cleanup; |
| 126 | } |
| 127 | |
| 128 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 129 | * Update accounting.... |
| 130 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 131 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 132 | ext2fs_mark_bb_dirty(fs); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 133 | ext2fs_mark_inode_bitmap(fs->inode_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 134 | ext2fs_mark_ib_dirty(fs); |
| 135 | |
| 136 | group = ext2fs_group_of_blk(fs, blk); |
| 137 | fs->group_desc[group].bg_free_blocks_count--; |
| 138 | group = ext2fs_group_of_ino(fs, ino); |
| 139 | fs->group_desc[group].bg_free_inodes_count--; |
| 140 | fs->group_desc[group].bg_used_dirs_count++; |
| 141 | fs->super->s_free_blocks_count--; |
| 142 | fs->super->s_free_inodes_count--; |
| 143 | ext2fs_mark_super_dirty(fs); |
| 144 | |
| 145 | cleanup: |
| 146 | if (block) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 147 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 148 | return retval; |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |