hammer2 - serialized flush work part 2
[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 and exclusive locks on inodes.
49  *
50  * An inode's ip->chain pointer is resolved and stable while an inode is
51  * locked, and can be cleaned out at any time (become NULL) when an inode
52  * is not locked.
53  *
54  * The underlying chain is also locked and returned.
55  *
56  * NOTE: We don't combine the inode/chain lock because putting away an
57  *       inode would otherwise confuse multiple lock holders of the inode.
58  */
59 hammer2_chain_t *
60 hammer2_inode_lock_ex(hammer2_inode_t *ip)
61 {
62         hammer2_chain_t *chain;
63
64         hammer2_inode_ref(ip);
65         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
66
67         chain = ip->chain;
68         KKASSERT(chain != NULL);        /* for now */
69         hammer2_chain_lock(ip->hmp, chain, HAMMER2_RESOLVE_ALWAYS);
70
71         return (chain);
72 }
73
74 void
75 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
76 {
77         if (chain)
78                 hammer2_chain_unlock(ip->hmp, chain);
79         ccms_thread_unlock(&ip->topo_cst);
80         hammer2_inode_drop(ip);
81 }
82
83 /*
84  * NOTE: We don't combine the inode/chain lock because putting away an
85  *       inode would otherwise confuse multiple lock holders of the inode.
86  *
87  *       Shared locks are especially sensitive to having too many shared
88  *       lock counts (from the same thread) on certain paths which might
89  *       need to upgrade them.  Only one count of a shared lock can be
90  *       upgraded.
91  */
92 hammer2_chain_t *
93 hammer2_inode_lock_sh(hammer2_inode_t *ip)
94 {
95         hammer2_chain_t *chain;
96
97         hammer2_inode_ref(ip);
98         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
99
100         chain = ip->chain;
101         KKASSERT(chain != NULL);        /* for now */
102         hammer2_chain_lock(ip->hmp, chain, HAMMER2_RESOLVE_ALWAYS |
103                                            HAMMER2_RESOLVE_SHARED);
104         return (chain);
105 }
106
107 void
108 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
109 {
110         if (chain)
111                 hammer2_chain_unlock(ip->hmp, chain);
112         ccms_thread_unlock(&ip->topo_cst);
113         hammer2_inode_drop(ip);
114 }
115
116 ccms_state_t
117 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
118 {
119         return(ccms_thread_lock_temp_release(&ip->topo_cst));
120 }
121
122 ccms_state_t
123 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
124 {
125         return(ccms_thread_lock_upgrade(&ip->topo_cst));
126 }
127
128 void
129 hammer2_inode_lock_restore(hammer2_inode_t *ip, ccms_state_t ostate)
130 {
131         ccms_thread_lock_restore(&ip->topo_cst, ostate);
132 }
133
134 /*
135  * Mount-wide locks
136  */
137
138 void
139 hammer2_mount_exlock(hammer2_mount_t *hmp)
140 {
141         ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_EXCLUSIVE);
142 }
143
144 void
145 hammer2_mount_shlock(hammer2_mount_t *hmp)
146 {
147         ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_SHARED);
148 }
149
150 void
151 hammer2_mount_unlock(hammer2_mount_t *hmp)
152 {
153         ccms_thread_unlock(&hmp->vchain.cst);
154 }
155
156 void
157 hammer2_voldata_lock(hammer2_mount_t *hmp)
158 {
159         lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
160 }
161
162 void
163 hammer2_voldata_unlock(hammer2_mount_t *hmp)
164 {
165         lockmgr(&hmp->voldatalk, LK_RELEASE);
166 }
167
168 /*
169  * Return the directory entry type for an inode.
170  *
171  * ip must be locked sh/ex.
172  */
173 int
174 hammer2_get_dtype(hammer2_chain_t *chain)
175 {
176         uint8_t type;
177
178         KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
179
180         if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK)
181                 type = chain->data->ipdata.target_type;
182
183         switch(type) {
184         case HAMMER2_OBJTYPE_UNKNOWN:
185                 return (DT_UNKNOWN);
186         case HAMMER2_OBJTYPE_DIRECTORY:
187                 return (DT_DIR);
188         case HAMMER2_OBJTYPE_REGFILE:
189                 return (DT_REG);
190         case HAMMER2_OBJTYPE_FIFO:
191                 return (DT_FIFO);
192         case HAMMER2_OBJTYPE_CDEV:      /* not supported */
193                 return (DT_CHR);
194         case HAMMER2_OBJTYPE_BDEV:      /* not supported */
195                 return (DT_BLK);
196         case HAMMER2_OBJTYPE_SOFTLINK:
197                 return (DT_LNK);
198         case HAMMER2_OBJTYPE_HARDLINK:  /* (never directly associated w/vp) */
199                 return (DT_UNKNOWN);
200         case HAMMER2_OBJTYPE_SOCKET:
201                 return (DT_SOCK);
202         case HAMMER2_OBJTYPE_WHITEOUT:  /* not supported */
203                 return (DT_UNKNOWN);
204         default:
205                 return (DT_UNKNOWN);
206         }
207         /* not reached */
208 }
209
210 /*
211  * Return the directory entry type for an inode
212  */
213 int
214 hammer2_get_vtype(hammer2_chain_t *chain)
215 {
216         KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
217
218         switch(chain->data->ipdata.type) {
219         case HAMMER2_OBJTYPE_UNKNOWN:
220                 return (VBAD);
221         case HAMMER2_OBJTYPE_DIRECTORY:
222                 return (VDIR);
223         case HAMMER2_OBJTYPE_REGFILE:
224                 return (VREG);
225         case HAMMER2_OBJTYPE_FIFO:
226                 return (VFIFO);
227         case HAMMER2_OBJTYPE_CDEV:      /* not supported */
228                 return (VCHR);
229         case HAMMER2_OBJTYPE_BDEV:      /* not supported */
230                 return (VBLK);
231         case HAMMER2_OBJTYPE_SOFTLINK:
232                 return (VLNK);
233         case HAMMER2_OBJTYPE_HARDLINK:  /* XXX */
234                 return (VBAD);
235         case HAMMER2_OBJTYPE_SOCKET:
236                 return (VSOCK);
237         case HAMMER2_OBJTYPE_WHITEOUT:  /* not supported */
238                 return (DT_UNKNOWN);
239         default:
240                 return (DT_UNKNOWN);
241         }
242         /* not reached */
243 }
244
245 u_int8_t
246 hammer2_get_obj_type(enum vtype vtype)
247 {
248         switch(vtype) {
249         case VDIR:
250                 return(HAMMER2_OBJTYPE_DIRECTORY);
251         case VREG:
252                 return(HAMMER2_OBJTYPE_REGFILE);
253         case VFIFO:
254                 return(HAMMER2_OBJTYPE_FIFO);
255         case VSOCK:
256                 return(HAMMER2_OBJTYPE_SOCKET);
257         case VCHR:
258                 return(HAMMER2_OBJTYPE_CDEV);
259         case VBLK:
260                 return(HAMMER2_OBJTYPE_BDEV);
261         case VLNK:
262                 return(HAMMER2_OBJTYPE_SOFTLINK);
263         default:
264                 return(HAMMER2_OBJTYPE_UNKNOWN);
265         }
266         /* not reached */
267 }
268
269 /*
270  * Convert a hammer2 64-bit time to a timespec.
271  */
272 void
273 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
274 {
275         ts->tv_sec = (unsigned long)(xtime / 1000000);
276         ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
277 }
278
279 u_int64_t
280 hammer2_timespec_to_time(struct timespec *ts)
281 {
282         u_int64_t xtime;
283
284         xtime = (unsigned)(ts->tv_nsec / 1000) +
285                 (unsigned long)ts->tv_sec * 1000000ULL;
286         return(xtime);
287 }
288
289 /*
290  * Convert a uuid to a unix uid or gid
291  */
292 u_int32_t
293 hammer2_to_unix_xid(uuid_t *uuid)
294 {
295         return(*(u_int32_t *)&uuid->node[2]);
296 }
297
298 void
299 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
300 {
301         bzero(uuid, sizeof(*uuid));
302         *(u_int32_t *)&uuid->node[2] = guid;
303 }
304
305 /*
306  * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
307  * The filename is split into fields which are hashed separately and then
308  * added together.
309  *
310  * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
311  * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
312  * (This means we do not need to do a 0-check/or-with-0x100000000 either).
313  *
314  * Also, the iscsi crc code is used instead of the old crc32 code.
315  */
316 hammer2_key_t
317 hammer2_dirhash(const unsigned char *name, size_t len)
318 {
319         const unsigned char *aname = name;
320         uint32_t crcx;
321         uint64_t key;
322         size_t i;
323         size_t j;
324
325         key = 0;
326
327         /*
328          * m32
329          */
330         crcx = 0;
331         for (i = j = 0; i < len; ++i) {
332                 if (aname[i] == '.' ||
333                     aname[i] == '-' ||
334                     aname[i] == '_' ||
335                     aname[i] == '~') {
336                         if (i != j)
337                                 crcx += hammer2_icrc32(aname + j, i - j);
338                         j = i + 1;
339                 }
340         }
341         if (i != j)
342                 crcx += hammer2_icrc32(aname + j, i - j);
343
344         /*
345          * The directory hash utilizes the top 32 bits of the 64-bit key.
346          * Bit 63 must be set to 1.
347          */
348         crcx |= 0x80000000U;
349         key |= (uint64_t)crcx << 32;
350
351         /*
352          * l16 - crc of entire filename
353          *
354          * This crc reduces degenerate hash collision conditions
355          */
356         crcx = hammer2_icrc32(aname, len);
357         crcx = crcx ^ (crcx << 16);
358         key |= crcx & 0xFFFF0000U;
359
360         /*
361          * Set bit 15.  This allows readdir to strip bit 63 so a positive
362          * 64-bit cookie/offset can always be returned, and still guarantee
363          * that the values 0x0000-0x7FFF are available for artificial entries.
364          * ('.' and '..').
365          */
366         key |= 0x8000U;
367
368         return (key);
369 }
370
371 /*
372  * Return the power-of-2 radix greater or equal to
373  * the specified number of bytes.
374  *
375  * Always returns at least the minimum media allocation
376  * size radix, HAMMER2_MIN_RADIX (10), which is 1KB.
377  */
378 int
379 hammer2_allocsize(size_t bytes)
380 {
381         int radix;
382
383         if (bytes < HAMMER2_MIN_ALLOC)
384                 bytes = HAMMER2_MIN_ALLOC;
385         if (bytes == HAMMER2_PBUFSIZE)
386                 radix = HAMMER2_PBUFRADIX;
387         else if (bytes >= 16384)
388                 radix = 14;
389         else if (bytes >= 1024)
390                 radix = 10;
391         else
392                 radix = HAMMER2_MIN_RADIX;
393
394         while (((size_t)1 << radix) < bytes)
395                 ++radix;
396         return (radix);
397 }
398
399 /*
400  * ip must be locked sh/ex
401  */
402 int
403 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
404                      hammer2_key_t *lbasep, hammer2_key_t *leofp)
405 {
406         hammer2_inode_data_t *ipdata = &ip->chain->data->ipdata;
407         int radix;
408
409         *lbasep = uoff & ~HAMMER2_PBUFMASK64;
410         *leofp = ipdata->size & ~HAMMER2_PBUFMASK64;
411         KKASSERT(*lbasep <= *leofp);
412         if (*lbasep == *leofp /*&& *leofp < 1024 * 1024*/) {
413                 radix = hammer2_allocsize((size_t)(ipdata->size - *leofp));
414                 if (radix < HAMMER2_MINALLOCRADIX)
415                         radix = HAMMER2_MINALLOCRADIX;
416                 *leofp += 1U << radix;
417                 return (1U << radix);
418         } else {
419                 return (HAMMER2_PBUFSIZE);
420         }
421 }
422
423 void
424 hammer2_update_time(uint64_t *timep)
425 {
426         struct timeval tv;
427
428         getmicrotime(&tv);
429         *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;
430 }