hammer2 - Start adding ioctl infrastructure, start writing hammer2 utility
[dragonfly.git] / sys / vfs / hammer2 / hammer2_subr.c
CommitLineData
e118c14f
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
703720e4 35#include <sys/cdefs.h>
703720e4
MD
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/types.h>
39#include <sys/lock.h>
40#include <sys/uuid.h>
e028fa74 41#include <sys/dirent.h>
703720e4
MD
42
43#include "hammer2.h"
44
45/*
46 * HAMMER2 inode locks
47 *
48 * HAMMER2 offers shared locks, update locks, and exclusive locks on inodes.
49 *
50 * Shared locks allow concurrent access to an inode's fields, but exclude
51 * access by concurrent exclusive locks.
52 *
53 * Update locks are interesting -- an update lock will be taken after all
54 * shared locks on an inode are released, but once it is in place, shared
55 * locks may proceed. The update field is signalled by a busy flag in the
56 * inode. Only one update lock may be in place at a given time on an inode.
57 *
58 * Exclusive locks prevent concurrent access to the inode.
59 *
60 * XXX: What do we use each for? How is visibility to the inode controlled?
61 */
62
9c2e0de0 63
e118c14f 64void
9c2e0de0 65hammer2_inode_lock_ex(hammer2_inode_t *ip)
703720e4 66{
01eabad4 67 hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
703720e4
MD
68}
69
e118c14f 70void
9c2e0de0 71hammer2_inode_unlock_ex(hammer2_inode_t *ip)
703720e4 72{
9c2e0de0 73 hammer2_chain_unlock(ip->hmp, &ip->chain);
703720e4
MD
74}
75
e118c14f 76void
9c2e0de0 77hammer2_inode_lock_sh(hammer2_inode_t *ip)
703720e4 78{
9c2e0de0
MD
79 KKASSERT(ip->chain.refs > 0);
80 lockmgr(&ip->chain.lk, LK_SHARED);
703720e4
MD
81}
82
e118c14f 83void
9c2e0de0 84hammer2_inode_unlock_sh(hammer2_inode_t *ip)
703720e4 85{
5c23d7f1 86 lockmgr(&ip->chain.lk, LK_RELEASE);
703720e4
MD
87}
88
9c2e0de0
MD
89/*
90 * Soft-busy an inode.
91 *
92 * The inode must be exclusively locked while soft-busying or soft-unbusying
93 * an inode. Once busied or unbusied the caller can release the lock.
94 */
e118c14f 95void
9c2e0de0 96hammer2_inode_busy(hammer2_inode_t *ip)
703720e4 97{
9c2e0de0
MD
98 if (ip->chain.busy++ == 0)
99 hammer2_chain_ref(ip->hmp, &ip->chain);
703720e4
MD
100}
101
e118c14f 102void
9c2e0de0 103hammer2_inode_unbusy(hammer2_inode_t *ip)
703720e4 104{
9c2e0de0
MD
105 if (--ip->chain.busy == 0)
106 hammer2_chain_drop(ip->hmp, &ip->chain);
703720e4
MD
107}
108
109/*
110 * Mount-wide locks
111 */
112
113void
54eb943b 114hammer2_mount_exlock(hammer2_mount_t *hmp)
703720e4 115{
5c23d7f1 116 lockmgr(&hmp->vchain.lk, LK_EXCLUSIVE);
703720e4
MD
117}
118
119void
54eb943b 120hammer2_mount_shlock(hammer2_mount_t *hmp)
703720e4 121{
5c23d7f1 122 lockmgr(&hmp->vchain.lk, LK_SHARED);
703720e4
MD
123}
124
125void
54eb943b 126hammer2_mount_unlock(hammer2_mount_t *hmp)
703720e4 127{
5c23d7f1 128 lockmgr(&hmp->vchain.lk, LK_RELEASE);
703720e4
MD
129}
130
2910a90c
MD
131void
132hammer2_voldata_lock(hammer2_mount_t *hmp)
133{
134 lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
135}
136
137void
138hammer2_voldata_unlock(hammer2_mount_t *hmp)
139{
140 lockmgr(&hmp->voldatalk, LK_RELEASE);
141}
142
703720e4 143/*
e028fa74
MD
144 * Return the directory entry type for an inode
145 */
146int
147hammer2_get_dtype(hammer2_inode_t *ip)
148{
149 switch(ip->ip_data.type) {
150 case HAMMER2_OBJTYPE_UNKNOWN:
151 return (DT_UNKNOWN);
152 case HAMMER2_OBJTYPE_DIRECTORY:
153 return (DT_DIR);
154 case HAMMER2_OBJTYPE_REGFILE:
155 return (DT_REG);
156 case HAMMER2_OBJTYPE_FIFO:
157 return (DT_FIFO);
158 case HAMMER2_OBJTYPE_CDEV: /* not supported */
159 return (DT_CHR);
160 case HAMMER2_OBJTYPE_BDEV: /* not supported */
161 return (DT_BLK);
162 case HAMMER2_OBJTYPE_SOFTLINK:
163 return (DT_LNK);
db0c2eb3 164 case HAMMER2_OBJTYPE_HARDLINK: /* (never directly associated w/vp) */
e028fa74
MD
165 return (DT_UNKNOWN);
166 case HAMMER2_OBJTYPE_SOCKET:
167 return (DT_SOCK);
168 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
169 return (DT_UNKNOWN);
170 default:
171 return (DT_UNKNOWN);
172 }
173 /* not reached */
174}
175
cd4b3d92
MD
176/*
177 * Return the directory entry type for an inode
178 */
179int
180hammer2_get_vtype(hammer2_inode_t *ip)
181{
182 switch(ip->ip_data.type) {
183 case HAMMER2_OBJTYPE_UNKNOWN:
184 return (VBAD);
185 case HAMMER2_OBJTYPE_DIRECTORY:
186 return (VDIR);
187 case HAMMER2_OBJTYPE_REGFILE:
188 return (VREG);
189 case HAMMER2_OBJTYPE_FIFO:
190 return (VFIFO);
191 case HAMMER2_OBJTYPE_CDEV: /* not supported */
192 return (VCHR);
193 case HAMMER2_OBJTYPE_BDEV: /* not supported */
194 return (VBLK);
195 case HAMMER2_OBJTYPE_SOFTLINK:
196 return (VLNK);
197 case HAMMER2_OBJTYPE_HARDLINK: /* XXX */
198 return (VBAD);
199 case HAMMER2_OBJTYPE_SOCKET:
200 return (VSOCK);
201 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
202 return (DT_UNKNOWN);
203 default:
204 return (DT_UNKNOWN);
205 }
206 /* not reached */
207}
208
37494cab
MD
209u_int8_t
210hammer2_get_obj_type(enum vtype vtype)
211{
212 switch(vtype) {
213 case VDIR:
214 return(HAMMER2_OBJTYPE_DIRECTORY);
215 case VREG:
216 return(HAMMER2_OBJTYPE_REGFILE);
217 case VFIFO:
218 return(HAMMER2_OBJTYPE_FIFO);
219 case VSOCK:
220 return(HAMMER2_OBJTYPE_SOCKET);
221 case VCHR:
222 return(HAMMER2_OBJTYPE_CDEV);
223 case VBLK:
224 return(HAMMER2_OBJTYPE_BDEV);
225 case VLNK:
226 return(HAMMER2_OBJTYPE_SOFTLINK);
227 default:
228 return(HAMMER2_OBJTYPE_UNKNOWN);
229 }
230 /* not reached */
231}
232
cd4b3d92
MD
233/*
234 * Convert a hammer2 64-bit time to a timespec.
235 */
236void
237hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
238{
239 ts->tv_sec = (unsigned long)(xtime / 1000000);
240 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
241}
242
37494cab
MD
243/*
244 * Convert a uuid to a unix uid or gid
245 */
246u_int32_t
247hammer2_to_unix_xid(uuid_t *uuid)
248{
249 return(*(u_int32_t *)&uuid->node[2]);
250}
e028fa74
MD
251
252/*
50e4f8f4
MD
253 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
254 * The filename is split into fields which are hashed separately and then
255 * added together.
256 *
257 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
258 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
259 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
260 *
261 * Also, the iscsi crc code is used instead of the old crc32 code.
262 */
263hammer2_key_t
264hammer2_dirhash(const unsigned char *name, size_t len)
265{
266 const unsigned char *aname = name;
267 uint32_t crcx;
268 uint64_t key;
269 size_t i;
270 size_t j;
271
50e4f8f4
MD
272 key = 0;
273
274 /*
275 * m32
276 */
277 crcx = 0;
278 for (i = j = 0; i < len; ++i) {
279 if (aname[i] == '.' ||
280 aname[i] == '-' ||
281 aname[i] == '_' ||
282 aname[i] == '~') {
283 if (i != j)
284 crcx += hammer2_icrc32(aname + j, i - j);
285 j = i + 1;
286 }
287 }
288 if (i != j)
289 crcx += hammer2_icrc32(aname + j, i - j);
290
291 /*
292 * The directory hash utilizes the top 32 bits of the 64-bit key.
293 * Bit 63 must be set to 1.
294 */
295 crcx |= 0x80000000U;
296 key |= (uint64_t)crcx << 32;
297
298 /*
299 * l16 - crc of entire filename
300 *
301 * This crc reduces degenerate hash collision conditions
302 */
303 crcx = hammer2_icrc32(aname, len);
304 crcx = crcx ^ (crcx << 16);
305 key |= crcx & 0xFFFF0000U;
306
e028fa74
MD
307 /*
308 * Set bit 15. This allows readdir to strip bit 63 so a positive
309 * 64-bit cookie/offset can always be returned, and still guarantee
310 * that the values 0x0000-0x7FFF are available for artificial entries.
311 * ('.' and '..').
312 */
313 key |= 0x8000U;
314
50e4f8f4
MD
315 return (key);
316}
6ba3b984
MD
317
318/*
319 * Return the power-of-2 radix greater or equal to
320 * the specified number of bytes.
321 *
322 * Always returns at least HAMMER2_MIN_RADIX (2^6).
323 */
324int
325hammer2_bytes_to_radix(size_t bytes)
326{
327 int radix;
328
329 if (bytes < HAMMER2_MIN_ALLOC)
330 bytes = HAMMER2_MIN_ALLOC;
331 if (bytes == HAMMER2_PBUFSIZE)
332 radix = HAMMER2_PBUFRADIX;
333 else if (bytes >= 1024)
334 radix = 10;
335 else
336 radix = HAMMER2_MIN_RADIX;
337
338 while (((size_t)1 << radix) < bytes)
339 ++radix;
340 return (radix);
341}
8cce658d
MD
342
343int
344hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
345 hammer2_key_t *lbasep, hammer2_key_t *leofp)
346{
347 int radix;
348
349 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
350 *leofp = ip->ip_data.size & ~HAMMER2_PBUFMASK;
351 KKASSERT(*lbasep <= *leofp);
352 if (*lbasep == *leofp) {
353 radix = hammer2_bytes_to_radix(
354 (size_t)(ip->ip_data.size - *leofp));
01eabad4
MD
355 if (radix < HAMMER2_MINALLOCRADIX)
356 radix = HAMMER2_MINALLOCRADIX;
8cce658d
MD
357 *leofp += 1U << radix;
358 return (1U << radix);
359 } else {
360 return (HAMMER2_PBUFSIZE);
361 }
362}