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