]> git.saurik.com Git - apple/xnu.git/blame - bsd/ufs/ufs/ufs_attrlist.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / ufs / ufs / ufs_attrlist.c
CommitLineData
55e303ae 1/*
91447636 2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
55e303ae
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
55e303ae 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
55e303ae
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
55e303ae
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23/*
24 * ufs_attrlist.c - UFS attribute list processing
25 *
26 * Copyright (c) 2002, Apple Computer, Inc. All Rights Reserved.
27 */
28
29#include <sys/types.h>
30#include <sys/systm.h>
91447636 31#include <sys/vnode_internal.h>
55e303ae
A
32#include <sys/malloc.h>
33#include <sys/attr.h>
34#include <sys/kernel.h>
91447636 35#include <sys/kauth.h>
55e303ae 36
55e303ae
A
37#include <ufs/ufs/dinode.h>
38#include <ufs/ffs/fs.h>
91447636 39#include <sys/mount_internal.h>
55e303ae
A
40#include "ufsmount.h"
41
55e303ae
A
42static char ufs_label_magic[4] = UFS_LABEL_MAGIC;
43
44/* Copied from diskdev_cmds/disklib/ufslabel.c */
45typedef union {
46 char c[2];
47 u_short s;
48} short_union_t;
49
50/* Copied from diskdev_cmds/disklib/ufslabel.c */
51typedef union {
52 u_short s[2];
53 long l;
54} long_union_t;
55
56/* Copied from diskdev_cmds/disklib/ufslabel.c */
57static __inline__ void
58reduce(int *sum)
59{
60 long_union_t l_util;
61
62 l_util.l = *sum;
63 *sum = l_util.s[0] + l_util.s[1];
64 if (*sum > 65535)
65 *sum -= 65535;
66 return;
67}
68
69/* Copied from diskdev_cmds/disklib/ufslabel.c */
91447636
A
70__private_extern__ unsigned short
71ul_cksum(void *data, int len)
55e303ae
A
72{
73 u_short *w;
74 int sum;
75
76 sum = 0;
77 w = (u_short *)data;
78 while ((len -= 32) >= 0) {
79 sum += w[0]; sum += w[1];
80 sum += w[2]; sum += w[3];
81 sum += w[4]; sum += w[5];
82 sum += w[6]; sum += w[7];
83 sum += w[8]; sum += w[9];
84 sum += w[10]; sum += w[11];
85 sum += w[12]; sum += w[13];
86 sum += w[14]; sum += w[15];
87 w += 16;
88 }
89 len += 32;
90 while ((len -= 8) >= 0) {
91 sum += w[0]; sum += w[1];
92 sum += w[2]; sum += w[3];
93 w += 4;
94 }
95 len += 8;
96 if (len) {
97 reduce(&sum);
98 while ((len -= 2) >= 0) {
99 sum += *w++;
100 }
101 }
102 if (len == -1) { /* odd-length data */
103 short_union_t s_util;
104
105 s_util.s = 0;
106 s_util.c[0] = *((char *)w);
107 s_util.c[1] = 0;
108 sum += s_util.s;
109 }
110 reduce(&sum);
111 return (~sum & 0xffff);
112}
113
114/* Adapted from diskdev_cmds/disklib/ufslabel.c */
91447636 115__private_extern__ boolean_t
55e303ae
A
116ufs_label_check(struct ufslabel *ul_p)
117{
118 u_int16_t calc;
119 u_int16_t checksum;
120
121 if (bcmp(&ul_p->ul_magic, ufs_label_magic,
122 sizeof(ul_p->ul_magic))) {
123#ifdef DEBUG
124 printf("ufslabel_check: label has bad magic number\n");
125#endif
126 return (FALSE);
127 }
128 if (ntohl(ul_p->ul_version) != UFS_LABEL_VERSION) {
129#ifdef DEBUG
130 printf("ufslabel_check: label has incorect version %d "
131 "(should be %d)\n", ntohl(ul_p->ul_version),
132 UFS_LABEL_VERSION);
133#endif
134 return (FALSE);
135 }
136 if (ntohs(ul_p->ul_namelen) > UFS_MAX_LABEL_NAME) {
137#ifdef DEBUG
138 printf("ufslabel_check: name length %d is too big (> %d)\n",
139 ntohs(ul_p->ul_namelen), UFS_MAX_LABEL_NAME);
140#endif
141 return (FALSE);
142 }
143
144 checksum = ul_p->ul_checksum; /* Remember previous checksum. */
145 ul_p->ul_checksum = 0;
91447636 146 calc = ul_cksum(ul_p, sizeof(*ul_p));
55e303ae
A
147 if (calc != checksum) {
148#ifdef DEBUG
149 printf("ufslabel_check: label checksum %x (should be %x)\n",
150 checksum, calc);
151#endif
152 return (FALSE);
153 }
154 return (TRUE);
155}
156
91447636 157__private_extern__ void
55e303ae
A
158ufs_label_init(struct ufslabel *ul_p)
159{
91447636
A
160 struct timeval tv;
161
162 microtime(&tv);
163
55e303ae
A
164 bzero(ul_p, sizeof(*ul_p));
165 ul_p->ul_version = htonl(UFS_LABEL_VERSION);
166 bcopy(ufs_label_magic, &ul_p->ul_magic, sizeof(ul_p->ul_magic));
91447636 167 ul_p->ul_time = htonl(tv.tv_sec);
55e303ae
A
168}
169