contents.c revision b4eca4b24af9c80ebb2a7fa2ba539a48096b7576
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <sys/stat.h> 18#include <string.h> 19#include <stdio.h> 20 21#include "ext4_utils.h" 22#include "ext4.h" 23#include "make_ext4fs.h" 24#include "allocate.h" 25#include "contents.h" 26#include "extent.h" 27#include "indirect.h" 28#include "xattr.h" 29 30static u32 dentry_size(u32 entries, struct dentry *dentries) 31{ 32 u32 len = 24; 33 unsigned int i; 34 unsigned int dentry_len; 35 36 for (i = 0; i < entries; i++) { 37 dentry_len = 8 + ALIGN(strlen(dentries[i].filename), 4); 38 if (len % info.block_size + dentry_len > info.block_size) 39 len += info.block_size - (len % info.block_size); 40 len += dentry_len; 41 } 42 43 /* include size of the dentry used to pad until the end of the block */ 44 if (len % info.block_size + 8 > info.block_size) 45 len += info.block_size - (len % info.block_size); 46 len += 8; 47 48 return len; 49} 50 51static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset, 52 struct ext4_dir_entry_2 *prev, u32 inode, const char *name, 53 u8 file_type) 54{ 55 u8 name_len = strlen(name); 56 u16 rec_len = 8 + ALIGN(name_len, 4); 57 struct ext4_dir_entry_2 *dentry; 58 59 u32 start_block = *offset / info.block_size; 60 u32 end_block = (*offset + rec_len - 1) / info.block_size; 61 if (start_block != end_block) { 62 /* Adding this dentry will cross a block boundary, so pad the previous 63 dentry to the block boundary */ 64 if (!prev) 65 critical_error("no prev"); 66 prev->rec_len += end_block * info.block_size - *offset; 67 *offset = end_block * info.block_size; 68 } 69 70 dentry = (struct ext4_dir_entry_2 *)(data + *offset); 71 dentry->inode = inode; 72 dentry->rec_len = rec_len; 73 dentry->name_len = name_len; 74 dentry->file_type = file_type; 75 memcpy(dentry->name, name, name_len); 76 77 *offset += rec_len; 78 return dentry; 79} 80 81/* Creates a directory structure for an array of directory entries, dentries, 82 and stores the location of the structure in an inode. The new inode's 83 .. link is set to dir_inode_num. Stores the location of the inode number 84 of each directory entry into dentries[i].inode, to be filled in later 85 when the inode for the entry is allocated. Returns the inode number of the 86 new directory */ 87u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries, 88 u32 dirs) 89{ 90 struct ext4_inode *inode; 91 u32 blocks; 92 u32 len; 93 u32 offset = 0; 94 u32 inode_num; 95 u8 *data; 96 unsigned int i; 97 struct ext4_dir_entry_2 *dentry; 98 99 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size); 100 len = blocks * info.block_size; 101 102 if (dir_inode_num) { 103 inode_num = allocate_inode(info); 104 } else { 105 dir_inode_num = EXT4_ROOT_INO; 106 inode_num = EXT4_ROOT_INO; 107 } 108 109 if (inode_num == EXT4_ALLOCATE_FAILED) { 110 error("failed to allocate inode\n"); 111 return EXT4_ALLOCATE_FAILED; 112 } 113 114 add_directory(inode_num); 115 116 inode = get_inode(inode_num); 117 if (inode == NULL) { 118 error("failed to get inode %u", inode_num); 119 return EXT4_ALLOCATE_FAILED; 120 } 121 122 data = inode_allocate_data_extents(inode, len, len); 123 if (data == NULL) { 124 error("failed to allocate %u extents", len); 125 return EXT4_ALLOCATE_FAILED; 126 } 127 128 inode->i_mode = S_IFDIR; 129 inode->i_links_count = dirs + 2; 130 inode->i_flags |= aux_info.default_i_flags; 131 132 dentry = NULL; 133 134 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR); 135 if (!dentry) { 136 error("failed to add . directory"); 137 return EXT4_ALLOCATE_FAILED; 138 } 139 140 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR); 141 if (!dentry) { 142 error("failed to add .. directory"); 143 return EXT4_ALLOCATE_FAILED; 144 } 145 146 for (i = 0; i < entries; i++) { 147 dentry = add_dentry(data, &offset, dentry, 0, 148 dentries[i].filename, dentries[i].file_type); 149 if (offset > len || (offset == len && i != entries - 1)) 150 critical_error("internal error: dentry for %s ends at %d, past %d\n", 151 dentries[i].filename, offset, len); 152 dentries[i].inode = &dentry->inode; 153 if (!dentry) { 154 error("failed to add directory"); 155 return EXT4_ALLOCATE_FAILED; 156 } 157 } 158 159 dentry = (struct ext4_dir_entry_2 *)(data + offset); 160 dentry->inode = 0; 161 dentry->rec_len = len - offset; 162 dentry->name_len = 0; 163 dentry->file_type = EXT4_FT_UNKNOWN; 164 165 return inode_num; 166} 167 168/* Creates a file on disk. Returns the inode number of the new file */ 169u32 make_file(const char *filename, u64 len) 170{ 171 struct ext4_inode *inode; 172 u32 inode_num; 173 174 inode_num = allocate_inode(info); 175 if (inode_num == EXT4_ALLOCATE_FAILED) { 176 error("failed to allocate inode\n"); 177 return EXT4_ALLOCATE_FAILED; 178 } 179 180 inode = get_inode(inode_num); 181 if (inode == NULL) { 182 error("failed to get inode %u", inode_num); 183 return EXT4_ALLOCATE_FAILED; 184 } 185 186 if (len > 0) 187 inode_allocate_file_extents(inode, len, filename); 188 189 inode->i_mode = S_IFREG; 190 inode->i_links_count = 1; 191 inode->i_flags |= aux_info.default_i_flags; 192 193 return inode_num; 194} 195 196/* Creates a file on disk. Returns the inode number of the new file */ 197u32 make_link(const char *filename, const char *link) 198{ 199 struct ext4_inode *inode; 200 u32 inode_num; 201 u32 len = strlen(link); 202 203 inode_num = allocate_inode(info); 204 if (inode_num == EXT4_ALLOCATE_FAILED) { 205 error("failed to allocate inode\n"); 206 return EXT4_ALLOCATE_FAILED; 207 } 208 209 inode = get_inode(inode_num); 210 if (inode == NULL) { 211 error("failed to get inode %u", inode_num); 212 return EXT4_ALLOCATE_FAILED; 213 } 214 215 inode->i_mode = S_IFLNK; 216 inode->i_links_count = 1; 217 inode->i_flags |= aux_info.default_i_flags; 218 inode->i_size_lo = len; 219 220 if (len + 1 <= sizeof(inode->i_block)) { 221 /* Fast symlink */ 222 memcpy((char*)inode->i_block, link, len); 223 } else { 224 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size); 225 memcpy(data, link, len); 226 inode->i_blocks_lo = info.block_size / 512; 227 } 228 229 return inode_num; 230} 231 232int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime) 233{ 234 struct ext4_inode *inode = get_inode(inode_num); 235 236 if (!inode) 237 return -1; 238 239 inode->i_mode |= mode; 240 inode->i_uid = uid; 241 inode->i_gid = gid; 242 inode->i_mtime = mtime; 243 inode->i_atime = mtime; 244 inode->i_ctime = mtime; 245 246 return 0; 247} 248 249#ifdef HAVE_SELINUX 250#define XATTR_SELINUX_SUFFIX "selinux" 251 252/* XXX */ 253#define cpu_to_le32(x) (x) 254#define cpu_to_le16(x) (x) 255 256int inode_set_selinux(u32 inode_num, const char *secon) 257{ 258 struct ext4_inode *inode = get_inode(inode_num); 259 u32 *hdr; 260 struct ext4_xattr_entry *entry; 261 size_t name_len = strlen(XATTR_SELINUX_SUFFIX); 262 size_t value_len = strlen(secon)+1; 263 size_t size, min_offs; 264 char *val; 265 266 if (!secon) 267 return 0; 268 269 if (!inode) 270 return -1; 271 272 hdr = (u32 *) (inode + 1); 273 *hdr = cpu_to_le32(EXT4_XATTR_MAGIC); 274 entry = (struct ext4_xattr_entry *) (hdr+1); 275 memset(entry, 0, EXT4_XATTR_LEN(name_len)); 276 entry->e_name_index = EXT4_XATTR_INDEX_SECURITY; 277 entry->e_name_len = name_len; 278 memcpy(entry->e_name, XATTR_SELINUX_SUFFIX, name_len); 279 entry->e_value_size = cpu_to_le32(value_len); 280 min_offs = (char *)inode + info.inode_size - (char*) entry; 281 size = EXT4_XATTR_SIZE(value_len); 282 val = (char *)entry + min_offs - size; 283 entry->e_value_offs = cpu_to_le16(min_offs - size); 284 memset(val + size - EXT4_XATTR_PAD, 0, EXT4_XATTR_PAD); 285 memcpy(val, secon, value_len); 286 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE); 287 288 return 0; 289} 290#else 291int inode_set_selinux(u32 inode_num, const char *secon) 292{ 293 return 0; 294} 295#endif 296