Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sys / vfs / hammer2 / hammer2_subr.c
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  */
35 #include <sys/cdefs.h>
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>
41 #include <sys/dirent.h>
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
63
64 void
65 hammer2_inode_lock_ex(hammer2_inode_t *ip)
66 {
67         hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
68 }
69
70 void
71 hammer2_inode_unlock_ex(hammer2_inode_t *ip)
72 {
73         hammer2_chain_unlock(ip->hmp, &ip->chain);
74 }
75
76 void
77 hammer2_inode_lock_sh(hammer2_inode_t *ip)
78 {
79         KKASSERT(ip->chain.refs > 0);
80         lockmgr(&ip->chain.lk, LK_SHARED);
81 }
82
83 void
84 hammer2_inode_unlock_sh(hammer2_inode_t *ip)
85 {
86         lockmgr(&ip->chain.lk, LK_RELEASE);
87 }
88
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  */
95 void
96 hammer2_inode_busy(hammer2_inode_t *ip)
97 {
98         if (ip->chain.busy++ == 0)
99                 hammer2_chain_ref(ip->hmp, &ip->chain);
100 }
101
102 void
103 hammer2_inode_unbusy(hammer2_inode_t *ip)
104 {
105         if (--ip->chain.busy == 0)
106                 hammer2_chain_drop(ip->hmp, &ip->chain);
107 }
108
109 /*
110  * Mount-wide locks
111  */
112
113 void
114 hammer2_mount_exlock(hammer2_mount_t *hmp)
115 {
116         lockmgr(&hmp->vchain.lk, LK_EXCLUSIVE);
117 }
118
119 void
120 hammer2_mount_shlock(hammer2_mount_t *hmp)
121 {
122         lockmgr(&hmp->vchain.lk, LK_SHARED);
123 }
124
125 void
126 hammer2_mount_unlock(hammer2_mount_t *hmp)
127 {
128         lockmgr(&hmp->vchain.lk, LK_RELEASE);
129 }
130
131 void
132 hammer2_voldata_lock(hammer2_mount_t *hmp)
133 {
134         lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
135 }
136
137 void
138 hammer2_voldata_unlock(hammer2_mount_t *hmp)
139 {
140         lockmgr(&hmp->voldatalk, LK_RELEASE);
141 }
142
143 /*
144  * Return the directory entry type for an inode
145  */
146 int
147 hammer2_get_dtype(hammer2_inode_t *ip)
148 {
149         uint8_t type;
150
151         if ((type = ip->ip_data.type) == HAMMER2_OBJTYPE_HARDLINK)
152                 type = ip->ip_data.target_type;
153
154         switch(type) {
155         case HAMMER2_OBJTYPE_UNKNOWN:
156                 return (DT_UNKNOWN);
157         case HAMMER2_OBJTYPE_DIRECTORY:
158                 return (DT_DIR);
159         case HAMMER2_OBJTYPE_REGFILE:
160                 return (DT_REG);
161         case HAMMER2_OBJTYPE_FIFO:
162                 return (DT_FIFO);
163         case HAMMER2_OBJTYPE_CDEV:      /* not supported */
164                 return (DT_CHR);
165         case HAMMER2_OBJTYPE_BDEV:      /* not supported */
166                 return (DT_BLK);
167         case HAMMER2_OBJTYPE_SOFTLINK:
168                 return (DT_LNK);
169         case HAMMER2_OBJTYPE_HARDLINK:  /* (never directly associated w/vp) */
170                 return (DT_UNKNOWN);
171         case HAMMER2_OBJTYPE_SOCKET:
172                 return (DT_SOCK);
173         case HAMMER2_OBJTYPE_WHITEOUT:  /* not supported */
174                 return (DT_UNKNOWN);
175         default:
176                 return (DT_UNKNOWN);
177         }
178         /* not reached */
179 }
180
181 /*
182  * Return the directory entry type for an inode
183  */
184 int
185 hammer2_get_vtype(hammer2_inode_t *ip)
186 {
187         switch(ip->ip_data.type) {
188         case HAMMER2_OBJTYPE_UNKNOWN:
189                 return (VBAD);
190         case HAMMER2_OBJTYPE_DIRECTORY:
191                 return (VDIR);
192         case HAMMER2_OBJTYPE_REGFILE:
193                 return (VREG);
194         case HAMMER2_OBJTYPE_FIFO:
195                 return (VFIFO);
196         case HAMMER2_OBJTYPE_CDEV:      /* not supported */
197                 return (VCHR);
198         case HAMMER2_OBJTYPE_BDEV:      /* not supported */
199                 return (VBLK);
200         case HAMMER2_OBJTYPE_SOFTLINK:
201                 return (VLNK);
202         case HAMMER2_OBJTYPE_HARDLINK:  /* XXX */
203                 return (VBAD);
204         case HAMMER2_OBJTYPE_SOCKET:
205                 return (VSOCK);
206         case HAMMER2_OBJTYPE_WHITEOUT:  /* not supported */
207                 return (DT_UNKNOWN);
208         default:
209                 return (DT_UNKNOWN);
210         }
211         /* not reached */
212 }
213
214 u_int8_t
215 hammer2_get_obj_type(enum vtype vtype)
216 {
217         switch(vtype) {
218         case VDIR:
219                 return(HAMMER2_OBJTYPE_DIRECTORY);
220         case VREG:
221                 return(HAMMER2_OBJTYPE_REGFILE);
222         case VFIFO:
223                 return(HAMMER2_OBJTYPE_FIFO);
224         case VSOCK:
225                 return(HAMMER2_OBJTYPE_SOCKET);
226         case VCHR:
227                 return(HAMMER2_OBJTYPE_CDEV);
228         case VBLK:
229                 return(HAMMER2_OBJTYPE_BDEV);
230         case VLNK:
231                 return(HAMMER2_OBJTYPE_SOFTLINK);
232         default:
233                 return(HAMMER2_OBJTYPE_UNKNOWN);
234         }
235         /* not reached */
236 }
237
238 /*
239  * Convert a hammer2 64-bit time to a timespec.
240  */
241 void
242 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
243 {
244         ts->tv_sec = (unsigned long)(xtime / 1000000);
245         ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
246 }
247
248 u_int64_t
249 hammer2_timespec_to_time(struct timespec *ts)
250 {
251         u_int64_t xtime;
252
253         xtime = (unsigned)(ts->tv_nsec / 1000) +
254                 (unsigned long)ts->tv_sec * 1000000ULL;
255         return(xtime);
256 }
257
258 /*
259  * Convert a uuid to a unix uid or gid
260  */
261 u_int32_t
262 hammer2_to_unix_xid(uuid_t *uuid)
263 {
264         return(*(u_int32_t *)&uuid->node[2]);
265 }
266
267 void
268 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
269 {
270         bzero(uuid, sizeof(*uuid));
271         *(u_int32_t *)&uuid->node[2] = guid;
272 }
273
274 /*
275  * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
276  * The filename is split into fields which are hashed separately and then
277  * added together.
278  *
279  * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
280  * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
281  * (This means we do not need to do a 0-check/or-with-0x100000000 either).
282  *
283  * Also, the iscsi crc code is used instead of the old crc32 code.
284  */
285 hammer2_key_t
286 hammer2_dirhash(const unsigned char *name, size_t len)
287 {
288         const unsigned char *aname = name;
289         uint32_t crcx;
290         uint64_t key;
291         size_t i;
292         size_t j;
293
294         key = 0;
295
296         /*
297          * m32
298          */
299         crcx = 0;
300         for (i = j = 0; i < len; ++i) {
301                 if (aname[i] == '.' ||
302                     aname[i] == '-' ||
303                     aname[i] == '_' ||
304                     aname[i] == '~') {
305                         if (i != j)
306                                 crcx += hammer2_icrc32(aname + j, i - j);
307                         j = i + 1;
308                 }
309         }
310         if (i != j)
311                 crcx += hammer2_icrc32(aname + j, i - j);
312
313         /*
314          * The directory hash utilizes the top 32 bits of the 64-bit key.
315          * Bit 63 must be set to 1.
316          */
317         crcx |= 0x80000000U;
318         key |= (uint64_t)crcx << 32;
319
320         /*
321          * l16 - crc of entire filename
322          *
323          * This crc reduces degenerate hash collision conditions
324          */
325         crcx = hammer2_icrc32(aname, len);
326         crcx = crcx ^ (crcx << 16);
327         key |= crcx & 0xFFFF0000U;
328
329         /*
330          * Set bit 15.  This allows readdir to strip bit 63 so a positive
331          * 64-bit cookie/offset can always be returned, and still guarantee
332          * that the values 0x0000-0x7FFF are available for artificial entries.
333          * ('.' and '..').
334          */
335         key |= 0x8000U;
336
337         return (key);
338 }
339
340 /*
341  * Return the power-of-2 radix greater or equal to
342  * the specified number of bytes.
343  *
344  * Always returns at least HAMMER2_MIN_RADIX (2^6).
345  */
346 int
347 hammer2_bytes_to_radix(size_t bytes)
348 {
349         int radix;
350
351         if (bytes < HAMMER2_MIN_ALLOC)
352                 bytes = HAMMER2_MIN_ALLOC;
353         if (bytes == HAMMER2_PBUFSIZE)
354                 radix = HAMMER2_PBUFRADIX;
355         else if (bytes >= 1024)
356                 radix = 10;
357         else
358                 radix = HAMMER2_MIN_RADIX;
359
360         while (((size_t)1 << radix) < bytes)
361                 ++radix;
362         return (radix);
363 }
364
365 int
366 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
367                      hammer2_key_t *lbasep, hammer2_key_t *leofp)
368 {
369         int radix;
370
371         *lbasep = uoff & ~HAMMER2_PBUFMASK64;
372         *leofp = ip->ip_data.size & ~HAMMER2_PBUFMASK;
373         KKASSERT(*lbasep <= *leofp);
374         if (*lbasep == *leofp) {
375                 radix = hammer2_bytes_to_radix(
376                                 (size_t)(ip->ip_data.size - *leofp));
377                 if (radix < HAMMER2_MINALLOCRADIX)
378                         radix = HAMMER2_MINALLOCRADIX;
379                 *leofp += 1U << radix;
380                 return (1U << radix);
381         } else {
382                 return (HAMMER2_PBUFSIZE);
383         }
384 }
385
386 void
387 hammer2_update_time(uint64_t *timep)
388 {
389         struct timeval tv;
390
391         getmicrotime(&tv);
392         *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;
393 }