Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / ufs / ffs / ffs_appleufs.c
blobae5287b95cd9e53e4aeb5827fe6aadd71560ae65
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2002 Darrin B. Jewell
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Darrin B. Jewell
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD$");
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #if defined(_KERNEL)
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #endif
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/ufs_bswap.h>
45 #include <ufs/ffs/fs.h>
46 #include <ufs/ffs/ffs_extern.h>
48 #if !defined(_KERNEL) && !defined(STANDALONE)
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <assert.h>
55 #define KASSERT(x) assert(x)
56 #endif
59 * this is the same calculation as in_cksum
61 u_int16_t
62 ffs_appleufs_cksum(const struct appleufslabel *appleufs)
64 const u_int16_t *p = (const u_int16_t *)appleufs;
65 int len = APPLEUFS_LABEL_SIZE; /* sizeof(struct appleufslabel) */
66 long res = 0;
67 while (len > 1) {
68 res += *p++;
69 len -= 2;
71 #if 0 /* APPLEUFS_LABEL_SIZE is guaranteed to be even */
72 if (len == 1)
73 res += htobe16(*(u_char *)p<<8);
74 #endif
75 res = (res >> 16) + (res & 0xffff);
76 res += (res >> 16);
77 return (~res);
80 /* copies o to n, validating and byteswapping along the way
81 * returns 0 if ok, EINVAL if not valid
83 int
84 ffs_appleufs_validate(const char *name, const struct appleufslabel *o,
85 struct appleufslabel *n)
87 struct appleufslabel tmp;
88 if (!n) n = &tmp;
90 if (o->ul_magic != be32toh(APPLEUFS_LABEL_MAGIC)) {
91 return EINVAL;
93 *n = *o;
94 n->ul_checksum = 0;
95 n->ul_checksum = ffs_appleufs_cksum(n);
96 if (n->ul_checksum != o->ul_checksum) {
97 #if defined(DIAGNOSTIC) || !defined(_KERNEL)
98 printf("%s: invalid APPLE UFS checksum. found 0x%x, expecting 0x%x",
99 name,o->ul_checksum,n->ul_checksum);
100 #endif
101 return EINVAL;
103 n->ul_magic = be32toh(o->ul_magic);
104 n->ul_version = be32toh(o->ul_version);
105 n->ul_time = be32toh(o->ul_time);
106 n->ul_namelen = be16toh(o->ul_namelen);
108 if (n->ul_namelen > APPLEUFS_MAX_LABEL_NAME) {
109 #if defined(DIAGNOSTIC) || !defined(_KERNEL)
110 printf("%s: APPLE UFS label name too long, truncated.\n",
111 name);
112 #endif
113 n->ul_namelen = APPLEUFS_MAX_LABEL_NAME;
115 /* if len is max, will set ul_unused1 */
116 n->ul_name[n->ul_namelen] = '\0';
118 #ifdef DEBUG
119 printf("%s: found APPLE UFS label v%d: \"%s\"\n",
120 name,n->ul_version,n->ul_name);
121 #endif
122 n->ul_uuid = be64toh(o->ul_uuid);
124 return 0;
127 void
128 ffs_appleufs_set(struct appleufslabel *appleufs, const char *name, time_t t,
129 uint64_t uuid)
131 size_t namelen;
132 if (!name) name = "untitled";
133 if (t == ((time_t)-1)) {
134 #if defined(_KERNEL)
135 t = time_second;
136 #elif defined(STANDALONE)
137 t = 0;
138 #else
139 (void)time(&t);
140 #endif
142 if (uuid == 0) {
143 #if defined(_KERNEL) && !defined(STANDALONE)
144 uuid = arc4random();
145 uuid <<= 32;
146 uuid |= arc4random();
147 #endif
149 namelen = strlen(name);
150 if (namelen > APPLEUFS_MAX_LABEL_NAME)
151 namelen = APPLEUFS_MAX_LABEL_NAME;
152 memset(appleufs, 0, APPLEUFS_LABEL_SIZE);
153 appleufs->ul_magic = htobe32(APPLEUFS_LABEL_MAGIC);
154 appleufs->ul_version = htobe32(APPLEUFS_LABEL_VERSION);
155 appleufs->ul_time = htobe32((u_int32_t)t);
156 appleufs->ul_namelen = htobe16(namelen);
157 strncpy(appleufs->ul_name,name,namelen);
158 appleufs->ul_uuid = htobe64(uuid);
159 appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);