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