1/* $NetBSD: v7fs_populate.c,v 1.2 2011/07/18 17:15:07 tron Exp $ */ 2 3/*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#if HAVE_NBTOOL_CONFIG_H 33#include "nbtool_config.h" 34#endif 35 36#include <sys/cdefs.h> 37#if defined(__RCSID) && !defined(__lint) 38__RCSID("$NetBSD: v7fs_populate.c,v 1.2 2011/07/18 17:15:07 tron Exp $"); 39#endif /* !__lint */ 40 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <unistd.h> 45#include <fcntl.h> 46#include <errno.h> 47#include <err.h> 48 49#if !HAVE_NBTOOL_CONFIG_H 50#include <sys/mount.h> 51#endif 52 53#include "makefs.h" 54#include "v7fs.h" 55#include "v7fs_impl.h" 56#include "v7fs_inode.h" 57#include "v7fs_superblock.h" 58#include "v7fs_datablock.h" 59#include "v7fs_endian.h" 60#include "v7fs_file.h" 61#include "v7fs_makefs.h" 62#include "newfs_v7fs.h" 63 64#define VPRINTF(fmt, args...) { if (v7fs_newfs_verbose) printf(fmt, ##args); } 65 66static void 67attr_setup(fsnode *node, struct v7fs_fileattr *attr) 68{ 69 struct stat *st = &node->inode->st; 70 71 attr->mode = node->type | st->st_mode; 72 attr->uid = st->st_uid; 73 attr->gid = st->st_gid; 74 attr->device = 0; 75 attr->ctime = st->st_ctime; 76 attr->atime = st->st_atime; 77 attr->mtime = st->st_mtime; 78} 79 80static int 81allocate(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node, 82 dev_t dev, struct v7fs_inode *inode) 83{ 84 int error; 85 v7fs_ino_t ino; 86 struct v7fs_fileattr attr; 87 88 memset(inode, 0, sizeof(*inode)); 89 90 attr_setup(node, &attr); 91 attr.device = dev; 92 if ((error = v7fs_file_allocate(fs, parent_inode, node->name, &attr, 93 &ino))) { 94 errno = error; 95 warn("%s", node->name); 96 return error; 97 } 98 node->inode->ino = ino; 99 node->inode->flags |= FI_ALLOCATED; 100 if ((error = v7fs_inode_load(fs, inode, ino))) { 101 errno = error; 102 warn("%s", node->name); 103 return error; 104 } 105 106 return 0; 107} 108 109struct copy_arg { 110 int fd; 111 uint8_t buf[V7FS_BSIZE]; 112}; 113 114static int 115copy_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz) 116{ 117 struct copy_arg *p = ctx; 118 119 if (read(p->fd, p->buf, sz) != (ssize_t)sz) { 120 return V7FS_ITERATOR_ERROR; 121 } 122 123 if (!fs->io.write(fs->io.cookie, p->buf, blk)) { 124 errno = EIO; 125 return V7FS_ITERATOR_ERROR; 126 } 127 progress(0); 128 129 return 0; 130} 131 132static int 133file_copy(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node, 134 const char *filepath) 135{ 136 struct v7fs_inode inode; 137 const char *errmsg; 138 fsinode *fnode = node->inode; 139 int error = 0; 140 int fd; 141 142 /* Check hard-link */ 143 if ((fnode->nlink > 1) && (fnode->flags & FI_ALLOCATED)) { 144 if ((error = v7fs_inode_load(fs, &inode, fnode->ino))) { 145 errmsg = "inode load"; 146 goto err_exit; 147 } 148 if ((error = v7fs_file_link(fs, parent_inode, &inode, 149 node->name))) { 150 errmsg = "hard link"; 151 goto err_exit; 152 } 153 return 0; 154 } 155 156 /* Allocate file */ 157 if ((error = allocate(fs, parent_inode, node, 0, &inode))) { 158 errmsg = "file allocate"; 159 goto err_exit; 160 } 161 if ((error = v7fs_datablock_expand(fs, &inode, fnode->st.st_size))) { 162 errmsg = "datablock expand"; 163 goto err_exit; 164 } 165 166 /* Data copy */ 167 if ((fd = open(filepath, O_RDONLY)) == -1) { 168 error = errno; 169 errmsg = "source file"; 170 goto err_exit; 171 } 172 173 error = v7fs_datablock_foreach(fs, &inode, copy_subr, 174 &(struct copy_arg){ .fd = fd }); 175 if (error != V7FS_ITERATOR_END) { 176 errmsg = "data copy"; 177 close(fd); 178 goto err_exit; 179 } else { 180 error = 0; 181 } 182 close(fd); 183 184 return error; 185 186err_exit: 187 errno = error; 188 warn("%s %s", node->name, errmsg); 189 190 return error; 191} 192 193static int 194populate_walk(struct v7fs_self *fs, struct v7fs_inode *parent_inode, 195 fsnode *root, char *dir) 196{ 197 fsnode *cur; 198 char *mydir = dir + strlen(dir); 199 char srcpath[MAXPATHLEN + 1]; 200 struct v7fs_inode inode; 201 int error = 0; 202 bool failed = false; 203 204 for (cur = root; cur != NULL; cur = cur->next) { 205 switch (cur->type & S_IFMT) { 206 default: 207 VPRINTF("%x\n", cur->flags & S_IFMT); 208 break; 209 case S_IFCHR: 210 /*FALLTHROUGH*/ 211 case S_IFBLK: 212 if ((error = allocate(fs, parent_inode, cur, 213 cur->inode->st.st_rdev, &inode))) { 214 errno = error; 215 warn("%s", cur->name); 216 } 217 break; 218 case S_IFDIR: 219 if (!cur->child) /*'.'*/ 220 break; 221 /* Allocate this directory. */ 222 if ((error = allocate(fs, parent_inode, cur, 0, 223 &inode))) { 224 errno = error; 225 warn("%s", cur->name); 226 } else { 227 /* Populate children. */ 228 mydir[0] = '/'; 229 strcpy(&mydir[1], cur->name); 230 error = populate_walk(fs, &inode, cur->child, 231 dir); 232 mydir[0] = '\0'; 233 } 234 break; 235 case S_IFREG: 236 snprintf(srcpath, sizeof(srcpath), "%s/%s", dir, 237 cur->name); 238 error = file_copy(fs, parent_inode, cur, srcpath); 239 break; 240 case S_IFLNK: 241 if ((error = allocate(fs, parent_inode, cur, 0, 242 &inode))) { 243 errno = error; 244 warn("%s", cur->name); 245 } else { 246 v7fs_file_symlink(fs, &inode, cur->symlink); 247 } 248 break; 249 } 250 if (error) 251 failed = true; 252 } 253 254 return failed ? 2 : 0; 255} 256 257int 258v7fs_populate(const char *dir, fsnode *root, fsinfo_t *fsopts, 259 const struct v7fs_mount_device *device) 260{ 261 v7fs_opt_t *v7fs_opts = fsopts->fs_specific; 262 static char path[MAXPATHLEN + 1]; 263 struct v7fs_inode root_inode; 264 struct v7fs_self *fs; 265 int error; 266 267 if ((error = v7fs_io_init(&fs, device, V7FS_BSIZE))) { 268 errno = error; 269 warn("I/O setup failed."); 270 return error; 271 } 272 fs->endian = device->endian; 273 v7fs_endian_init(fs); 274 275 if ((error = v7fs_superblock_load(fs))) { 276 errno = error; 277 warn("Can't load superblock."); 278 return error; 279 } 280 fsopts->superblock = &fs->superblock; /* not used. */ 281 282 if ((error = v7fs_inode_load(fs, &root_inode, V7FS_ROOT_INODE))) { 283 errno = error; 284 warn("Can't load root inode."); 285 return error; 286 } 287 288 progress(&(struct progress_arg){ .label = "populate", .tick = 289 v7fs_opts->npuredatablk / PROGRESS_BAR_GRANULE }); 290 291 strncpy(path, dir, sizeof(path)); 292 error = populate_walk(fs, &root_inode, root, path); 293 294 v7fs_inode_writeback(fs, &root_inode); 295 v7fs_superblock_writeback(fs); 296 297 return error; 298} 299