From owner-svn-src-user@FreeBSD.ORG Thu Mar 20 20:14:29 2014 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 09E9FF3E; Thu, 20 Mar 2014 20:14:29 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id DE3BEC42; Thu, 20 Mar 2014 20:14:28 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s2KKESOh004178; Thu, 20 Mar 2014 20:14:28 GMT (envelope-from marcel@svn.freebsd.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s2KKEQsa004166; Thu, 20 Mar 2014 20:14:26 GMT (envelope-from marcel@svn.freebsd.org) Message-Id: <201403202014.s2KKEQsa004166@svn.freebsd.org> From: Marcel Moolenaar Date: Thu, 20 Mar 2014 20:14:26 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r263442 - user/marcel/mkimg X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Mar 2014 20:14:29 -0000 Author: marcel Date: Thu Mar 20 20:14:26 2014 New Revision: 263442 URL: http://svnweb.freebsd.org/changeset/base/263442 Log: Add a scheme-specific write callback to write the metadata. While here: 1. Move FreeBSD-specific headers to a more centralized place, so that it's easier to port mkimg. 2. Remove inclusion of where we don't need it (copy-pasted). 3. Check the partition data given on the command line before we do any I/O. 4. Add scheme_round() for rounding the partition size. 5. Have scheme_write call the scheme-specific write callback and have it propagate errors back to the caller. Modified: user/marcel/mkimg/apm.c user/marcel/mkimg/bsd.c user/marcel/mkimg/ebr.c user/marcel/mkimg/gpt.c user/marcel/mkimg/mbr.c user/marcel/mkimg/mkimg.c user/marcel/mkimg/mkimg.h user/marcel/mkimg/pc98.c user/marcel/mkimg/scheme.c user/marcel/mkimg/scheme.h user/marcel/mkimg/vtoc8.c Modified: user/marcel/mkimg/apm.c ============================================================================== --- user/marcel/mkimg/apm.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/apm.c Thu Mar 20 20:14:26 2014 (r263442) @@ -29,8 +29,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include +#include #include #include "mkimg.h" @@ -49,11 +48,19 @@ apm_metadata(u_int where, u_int parts, u return (secs); } +static int +apm_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme apm_scheme = { .name = "apm", .description = "Apple Partition Map", .aliases = apm_aliases, .metadata = apm_metadata, + .write = apm_write, .nparts = 4096 }; Modified: user/marcel/mkimg/bsd.c ============================================================================== --- user/marcel/mkimg/bsd.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/bsd.c Thu Mar 20 20:14:26 2014 (r263442) @@ -28,10 +28,9 @@ __FBSDID("$FreeBSD$"); #include -#include -#include +#include +#include #include -#include #include "mkimg.h" #include "scheme.h" @@ -49,11 +48,19 @@ bsd_metadata(u_int where, u_int parts __ return (secs); } +static int +bsd_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme bsd_scheme = { .name = "bsd", .description = "BSD disk label", .aliases = bsd_aliases, .metadata = bsd_metadata, + .write = bsd_write, .nparts = 20 }; Modified: user/marcel/mkimg/ebr.c ============================================================================== --- user/marcel/mkimg/ebr.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/ebr.c Thu Mar 20 20:14:26 2014 (r263442) @@ -28,10 +28,9 @@ __FBSDID("$FreeBSD$"); #include -#include -#include +#include +#include #include -#include #include "mkimg.h" #include "scheme.h" @@ -49,11 +48,19 @@ ebr_metadata(u_int where, u_int parts __ return (secs); } +static int +ebr_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme ebr_scheme = { .name = "ebr", .description = "Extended Boot Record", .aliases = ebr_aliases, .metadata = ebr_metadata, + .write = ebr_write, .nparts = 4096 }; Modified: user/marcel/mkimg/gpt.c ============================================================================== --- user/marcel/mkimg/gpt.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/gpt.c Thu Mar 20 20:14:26 2014 (r263442) @@ -28,9 +28,8 @@ __FBSDID("$FreeBSD$"); #include +#include #include -#include -#include #include #include @@ -58,11 +57,19 @@ gpt_metadata(u_int where, u_int parts, u return (secs); } +static int +gpt_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme gpt_scheme = { .name = "gpt", .description = "GUID Partition Table", .aliases = gpt_aliases, .metadata = gpt_metadata, + .write = gpt_write, .nparts = 4096 }; Modified: user/marcel/mkimg/mbr.c ============================================================================== --- user/marcel/mkimg/mbr.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/mbr.c Thu Mar 20 20:14:26 2014 (r263442) @@ -29,10 +29,8 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include +#include #include -#include #include "mkimg.h" #include "scheme.h" @@ -50,11 +48,19 @@ mbr_metadata(u_int where, u_int parts __ return (secs); } +static int +mbr_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme mbr_scheme = { .name = "mbr", .description = "Master Boot Record", .aliases = mbr_aliases, .metadata = mbr_metadata, + .write = mbr_write, .nparts = NDOSPART }; Modified: user/marcel/mkimg/mkimg.c ============================================================================== --- user/marcel/mkimg/mkimg.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/mkimg.c Thu Mar 20 20:14:26 2014 (r263442) @@ -216,6 +216,13 @@ mkimg(void) errc(EX_DATAERR, ENOSPC, "only %d partitions are supported", scheme_max_parts()); + /* First check partition information */ + STAILQ_FOREACH(part, &partlist, link) { + error = scheme_check_part(part); + if (error) + errc(EX_DATAERR, error, "partition %d", part->index+1); + } + offset = scheme_first_offset(nparts); STAILQ_FOREACH(part, &partlist, link) { part->offset = offset; @@ -245,14 +252,14 @@ mkimg(void) error = errno; break; } - part->size = size; - error = scheme_check_part(part); if (error) - errc(EX_DATAERR, error, "partition %d", part->index+1); + errc(EX_IOERR, error, "partition %d", part->index+1); + size = scheme_round(size); + part->size = size; offset = scheme_next_offset(offset, size); } - scheme_write(tmpfd, offset); + error = (scheme_write(tmpfd, offset)); } int Modified: user/marcel/mkimg/mkimg.h ============================================================================== --- user/marcel/mkimg/mkimg.h Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/mkimg.h Thu Mar 20 20:14:26 2014 (r263442) @@ -29,6 +29,8 @@ #ifndef _MKIMG_MKIMG_H_ #define _MKIMG_MKIMG_H_ +#include + struct part { STAILQ_ENTRY(part) link; char *alias; /* Partition type alias. */ Modified: user/marcel/mkimg/pc98.c ============================================================================== --- user/marcel/mkimg/pc98.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/pc98.c Thu Mar 20 20:14:26 2014 (r263442) @@ -29,10 +29,8 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include +#include #include -#include #include "mkimg.h" #include "scheme.h" @@ -50,11 +48,19 @@ pc98_metadata(u_int where, u_int parts _ return (secs); } +static int +pc98_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme pc98_scheme = { .name = "pc98", .description = "PC-9800 disk partitions", .aliases = pc98_aliases, .metadata = pc98_metadata, + .write = pc98_write, .nparts = PC98_NPARTS }; Modified: user/marcel/mkimg/scheme.c ============================================================================== --- user/marcel/mkimg/scheme.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/scheme.c Thu Mar 20 20:14:26 2014 (r263442) @@ -96,6 +96,14 @@ scheme_max_parts(void) return (scheme->nparts); } +uint64_t +scheme_round(uint64_t sz) +{ + + sz = (sz + secsz - 1) & ~(secsz - 1); + return (sz); +} + off_t scheme_first_offset(u_int parts) { @@ -111,17 +119,17 @@ scheme_next_offset(off_t off, uint64_t s { u_int secs; - sz = (sz + secsz - 1) & ~(secsz - 1); secs = scheme->metadata(SCHEME_META_PART_AFTER, 0, secsz) + scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz); sz += (secs * secsz); return (off + sz); } -void +int scheme_write(int fd, off_t off) { u_int secs; + int error; /* Fixup offset: it has an extra metadata before the partition */ secs = scheme->metadata(SCHEME_META_PART_BEFORE, 0, secsz); @@ -129,5 +137,9 @@ scheme_write(int fd, off_t off) secs = scheme->metadata(SCHEME_META_IMG_END, nparts, secsz); off += (secs * secsz); - ftruncate(fd, off); + if (ftruncate(fd, off) == -1) + return (errno); + + error = scheme->write(fd, off, nparts, secsz); + return (error); } Modified: user/marcel/mkimg/scheme.h ============================================================================== --- user/marcel/mkimg/scheme.h Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/scheme.h Thu Mar 20 20:14:26 2014 (r263442) @@ -29,6 +29,8 @@ #ifndef _MKIMG_SCHEME_H_ #define _MKIMG_SCHEME_H_ +#include + struct mkimg_alias { const char *name; uintptr_t tp; @@ -45,6 +47,7 @@ struct mkimg_scheme { #define SCHEME_META_IMG_END 2 #define SCHEME_META_PART_BEFORE 3 #define SCHEME_META_PART_AFTER 4 + int (*write)(int, off_t, u_int, u_int); int nparts; }; @@ -56,8 +59,9 @@ struct mkimg_scheme *scheme_selected(voi int scheme_check_part(struct part *); u_int scheme_max_parts(void); +uint64_t scheme_round(uint64_t); off_t scheme_first_offset(u_int); off_t scheme_next_offset(off_t, uint64_t); -void scheme_write(int, off_t); +int scheme_write(int, off_t); #endif /* _MKIMG_SCHEME_H_ */ Modified: user/marcel/mkimg/vtoc8.c ============================================================================== --- user/marcel/mkimg/vtoc8.c Thu Mar 20 20:14:04 2014 (r263441) +++ user/marcel/mkimg/vtoc8.c Thu Mar 20 20:14:26 2014 (r263442) @@ -28,11 +28,9 @@ __FBSDID("$FreeBSD$"); #include -#include -#include +#include #include #include -#include #include "mkimg.h" #include "scheme.h" @@ -50,11 +48,19 @@ vtoc8_metadata(u_int where, u_int parts return (secs); } +static int +vtoc8_write(int fd __unused, off_t imgsz __unused, u_int parts __unused, + u_int secsz __unused) +{ + return (ENOSYS); +} + static struct mkimg_scheme vtoc8_scheme = { .name = "vtoc8", .description = "SMI VTOC8 disk labels", .aliases = vtoc8_aliases, .metadata = vtoc8_metadata, + .write = vtoc8_write, .nparts = VTOC8_NPARTS };