Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / vfs / hammer2 / hammer2_freemap.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/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/namei.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/mountctl.h>
45
46 #include "hammer2.h"
47
48 #define FREEMAP_DEBUG   0
49
50 struct hammer2_fiterate {
51         hammer2_off_t   bpref;
52         hammer2_off_t   bnext;
53         int             loops;
54 };
55
56 typedef struct hammer2_fiterate hammer2_fiterate_t;
57
58 static int hammer2_freemap_try_alloc(hammer2_trans_t *trans,
59                         hammer2_chain_t **parentp, hammer2_blockref_t *bref,
60                         int radix, hammer2_fiterate_t *iter);
61 static void hammer2_freemap_init(hammer2_trans_t *trans, hammer2_dev_t *hmp,
62                         hammer2_key_t key, hammer2_chain_t *chain);
63 static int hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_dev_t *hmp,
64                         hammer2_bmap_data_t *bmap, uint16_t class,
65                         int n, int radix, hammer2_key_t *basep);
66 static int hammer2_freemap_iterate(hammer2_trans_t *trans,
67                         hammer2_chain_t **parentp, hammer2_chain_t **chainp,
68                         hammer2_fiterate_t *iter);
69
70 static __inline
71 int
72 hammer2_freemapradix(int radix)
73 {
74         return(radix);
75 }
76
77 /*
78  * Calculate the device offset for the specified FREEMAP_NODE or FREEMAP_LEAF
79  * bref.  Return a combined media offset and physical size radix.  Freemap
80  * chains use fixed storage offsets in the 4MB reserved area at the
81  * beginning of each 2GB zone
82  *
83  * Rotate between four possibilities.  Theoretically this means we have three
84  * good freemaps in case of a crash which we can use as a base for the fixup
85  * scan at mount-time.
86  */
87 #define H2FMBASE(key, radix)    ((key) & ~(((hammer2_off_t)1 << (radix)) - 1))
88 #define H2FMSHIFT(radix)        ((hammer2_off_t)1 << (radix))
89
90 static
91 int
92 hammer2_freemap_reserve(hammer2_trans_t *trans, hammer2_chain_t *chain,
93                         int radix)
94 {
95         hammer2_blockref_t *bref = &chain->bref;
96         hammer2_off_t off;
97         int index;
98         int index_inc;
99         size_t bytes;
100
101         /*
102          * Physical allocation size.
103          */
104         bytes = (size_t)1 << radix;
105
106         /*
107          * Calculate block selection index 0..7 of current block.  If this
108          * is the first allocation of the block (verses a modification of an
109          * existing block), we use index 0, otherwise we use the next rotating
110          * index.
111          */
112         if ((bref->data_off & ~HAMMER2_OFF_MASK_RADIX) == 0) {
113                 index = 0;
114         } else {
115                 off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX &
116                       (((hammer2_off_t)1 <<
117                         HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
118                 off = off / HAMMER2_PBUFSIZE;
119                 KKASSERT(off >= HAMMER2_ZONE_FREEMAP_00 &&
120                          off < HAMMER2_ZONE_FREEMAP_END);
121                 index = (int)(off - HAMMER2_ZONE_FREEMAP_00) /
122                         HAMMER2_ZONE_FREEMAP_INC;
123                 KKASSERT(index >= 0 && index < HAMMER2_NFREEMAPS);
124                 if (++index == HAMMER2_NFREEMAPS)
125                         index = 0;
126         }
127
128         /*
129          * Calculate the block offset of the reserved block.  This will
130          * point into the 4MB reserved area at the base of the appropriate
131          * 2GB zone, once added to the FREEMAP_x selection above.
132          */
133         index_inc = index * HAMMER2_ZONE_FREEMAP_INC;
134
135         switch(bref->keybits) {
136         /* case HAMMER2_FREEMAP_LEVEL6_RADIX: not applicable */
137         case HAMMER2_FREEMAP_LEVEL5_RADIX:      /* 2EB */
138                 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
139                 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
140                 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL5_RADIX) +
141                       (index_inc + HAMMER2_ZONE_FREEMAP_00 +
142                        HAMMER2_ZONEFM_LEVEL5) * HAMMER2_PBUFSIZE;
143                 break;
144         case HAMMER2_FREEMAP_LEVEL4_RADIX:      /* 2EB */
145                 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
146                 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
147                 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL4_RADIX) +
148                       (index_inc + HAMMER2_ZONE_FREEMAP_00 +
149                        HAMMER2_ZONEFM_LEVEL4) * HAMMER2_PBUFSIZE;
150                 break;
151         case HAMMER2_FREEMAP_LEVEL3_RADIX:      /* 2PB */
152                 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
153                 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
154                 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL3_RADIX) +
155                       (index_inc + HAMMER2_ZONE_FREEMAP_00 +
156                        HAMMER2_ZONEFM_LEVEL3) * HAMMER2_PBUFSIZE;
157                 break;
158         case HAMMER2_FREEMAP_LEVEL2_RADIX:      /* 2TB */
159                 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE);
160                 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
161                 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL2_RADIX) +
162                       (index_inc + HAMMER2_ZONE_FREEMAP_00 +
163                        HAMMER2_ZONEFM_LEVEL2) * HAMMER2_PBUFSIZE;
164                 break;
165         case HAMMER2_FREEMAP_LEVEL1_RADIX:      /* 2GB */
166                 KKASSERT(bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
167                 KKASSERT(bytes == HAMMER2_FREEMAP_LEVELN_PSIZE);
168                 off = H2FMBASE(bref->key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
169                       (index_inc + HAMMER2_ZONE_FREEMAP_00 +
170                        HAMMER2_ZONEFM_LEVEL1) * HAMMER2_PBUFSIZE;
171                 break;
172         default:
173                 panic("freemap: bad radix(2) %p %d\n", bref, bref->keybits);
174                 /* NOT REACHED */
175                 off = (hammer2_off_t)-1;
176                 break;
177         }
178         bref->data_off = off | radix;
179 #if FREEMAP_DEBUG
180         kprintf("FREEMAP BLOCK TYPE %d %016jx/%d DATA_OFF=%016jx\n",
181                 bref->type, bref->key, bref->keybits, bref->data_off);
182 #endif
183         return (0);
184 }
185
186 /*
187  * Normal freemap allocator
188  *
189  * Use available hints to allocate space using the freemap.  Create missing
190  * freemap infrastructure on-the-fly as needed (including marking initial
191  * allocations using the iterator as allocated, instantiating new 2GB zones,
192  * and dealing with the end-of-media edge case).
193  *
194  * ip and bpref are only used as a heuristic to determine locality of
195  * reference.  bref->key may also be used heuristically.
196  */
197 int
198 hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
199                       size_t bytes)
200 {
201         hammer2_dev_t *hmp = chain->hmp;
202         hammer2_blockref_t *bref = &chain->bref;
203         hammer2_chain_t *parent;
204         int radix;
205         int error;
206         unsigned int hindex;
207         hammer2_fiterate_t iter;
208
209         /*
210          * Validate the allocation size.  It must be a power of 2.
211          *
212          * For now require that the caller be aware of the minimum
213          * allocation (1K).
214          */
215         radix = hammer2_getradix(bytes);
216         KKASSERT((size_t)1 << radix == bytes);
217
218         if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
219             bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
220                 /*
221                  * Freemap blocks themselves are assigned from the reserve
222                  * area, not allocated from the freemap.
223                  */
224                 error = hammer2_freemap_reserve(trans, chain, radix);
225                 return error;
226         }
227
228         KKASSERT(bytes >= HAMMER2_ALLOC_MIN && bytes <= HAMMER2_ALLOC_MAX);
229
230         if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
231                 ++trans->sync_xid;
232
233         /*
234          * Calculate the starting point for our allocation search.
235          *
236          * Each freemap leaf is dedicated to a specific freemap_radix.
237          * The freemap_radix can be more fine-grained than the device buffer
238          * radix which results in inodes being grouped together in their
239          * own segment, terminal-data (16K or less) and initial indirect
240          * block being grouped together, and then full-indirect and full-data
241          * blocks (64K) being grouped together.
242          *
243          * The single most important aspect of this is the inode grouping
244          * because that is what allows 'find' and 'ls' and other filesystem
245          * topology operations to run fast.
246          */
247 #if 0
248         if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
249                 bpref = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
250         else if (trans->tmp_bpref)
251                 bpref = trans->tmp_bpref;
252         else if (trans->tmp_ip)
253                 bpref = trans->tmp_ip->chain->bref.data_off;
254         else
255 #endif
256         /*
257          * Heuristic tracking index.  We would like one for each distinct
258          * bref type if possible.  heur_freemap[] has room for two classes
259          * for each type.  At a minimum we have to break-up our heuristic
260          * by device block sizes.
261          */
262         hindex = hammer2_devblkradix(radix) - HAMMER2_MINIORADIX;
263         KKASSERT(hindex < HAMMER2_FREEMAP_HEUR_NRADIX);
264         hindex += bref->type * HAMMER2_FREEMAP_HEUR_NRADIX;
265         hindex &= HAMMER2_FREEMAP_HEUR_TYPES * HAMMER2_FREEMAP_HEUR_NRADIX - 1;
266         KKASSERT(hindex < HAMMER2_FREEMAP_HEUR);
267
268         iter.bpref = hmp->heur_freemap[hindex];
269
270         /*
271          * Make sure bpref is in-bounds.  It's ok if bpref covers a zone's
272          * reserved area, the try code will iterate past it.
273          */
274         if (iter.bpref > hmp->voldata.volu_size)
275                 iter.bpref = hmp->voldata.volu_size - 1;
276
277         /*
278          * Iterate the freemap looking for free space before and after.
279          */
280         parent = &hmp->fchain;
281         hammer2_chain_ref(parent);
282         hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
283         error = EAGAIN;
284         iter.bnext = iter.bpref;
285         iter.loops = 0;
286
287         while (error == EAGAIN) {
288                 error = hammer2_freemap_try_alloc(trans, &parent, bref,
289                                                   radix, &iter);
290         }
291         hmp->heur_freemap[hindex] = iter.bnext;
292         hammer2_chain_unlock(parent);
293         hammer2_chain_drop(parent);
294
295         if (trans->flags & (HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_PREFLUSH))
296                 --trans->sync_xid;
297
298         return (error);
299 }
300
301 static int
302 hammer2_freemap_try_alloc(hammer2_trans_t *trans, hammer2_chain_t **parentp,
303                           hammer2_blockref_t *bref, int radix,
304                           hammer2_fiterate_t *iter)
305 {
306         hammer2_dev_t *hmp = (*parentp)->hmp;
307         hammer2_off_t l0size;
308         hammer2_off_t l1size;
309         hammer2_off_t l1mask;
310         hammer2_key_t key_dummy;
311         hammer2_chain_t *chain;
312         hammer2_off_t key;
313         size_t bytes;
314         uint16_t class;
315         int error = 0;
316         int cache_index = -1;
317
318         /*
319          * Calculate the number of bytes being allocated, the number
320          * of contiguous bits of bitmap being allocated, and the bitmap
321          * mask.
322          *
323          * WARNING! cpu hardware may mask bits == 64 -> 0 and blow up the
324          *          mask calculation.
325          */
326         bytes = (size_t)1 << radix;
327         class = (bref->type << 8) | hammer2_devblkradix(radix);
328
329         /*
330          * Lookup the level1 freemap chain, creating and initializing one
331          * if necessary.  Intermediate levels will be created automatically
332          * when necessary by hammer2_chain_create().
333          */
334         key = H2FMBASE(iter->bnext, HAMMER2_FREEMAP_LEVEL1_RADIX);
335         l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
336         l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
337         l1mask = l1size - 1;
338
339         chain = hammer2_chain_lookup(parentp, &key_dummy, key, key + l1mask,
340                                      &cache_index,
341                                      HAMMER2_LOOKUP_ALWAYS |
342                                      HAMMER2_LOOKUP_MATCHIND);
343
344         if (chain == NULL) {
345                 /*
346                  * Create the missing leaf, be sure to initialize
347                  * the auxillary freemap tracking information in
348                  * the bref.check.freemap structure.
349                  */
350 #if 0
351                 kprintf("freemap create L1 @ %016jx bpref %016jx\n",
352                         key, iter->bpref);
353 #endif
354                 error = hammer2_chain_create(trans, parentp, &chain, hmp->spmp,
355                                      key, HAMMER2_FREEMAP_LEVEL1_RADIX,
356                                      HAMMER2_BREF_TYPE_FREEMAP_LEAF,
357                                      HAMMER2_FREEMAP_LEVELN_PSIZE,
358                                      0);
359                 KKASSERT(error == 0);
360                 if (error == 0) {
361                         hammer2_chain_modify(trans, chain, 0);
362                         bzero(&chain->data->bmdata[0],
363                               HAMMER2_FREEMAP_LEVELN_PSIZE);
364                         chain->bref.check.freemap.bigmask = (uint32_t)-1;
365                         chain->bref.check.freemap.avail = l1size;
366                         /* bref.methods should already be inherited */
367
368                         hammer2_freemap_init(trans, hmp, key, chain);
369                 }
370         } else if (chain->error) {
371                 /*
372                  * Error during lookup.
373                  */
374                 kprintf("hammer2_freemap_try_alloc: %016jx: error %s\n",
375                         (intmax_t)bref->data_off,
376                         hammer2_error_str(chain->error));
377                 error = EIO;
378         } else if ((chain->bref.check.freemap.bigmask &
379                    ((size_t)1 << radix)) == 0) {
380                 /*
381                  * Already flagged as not having enough space
382                  */
383                 error = ENOSPC;
384         } else {
385                 /*
386                  * Modify existing chain to setup for adjustment.
387                  */
388                 hammer2_chain_modify(trans, chain, 0);
389         }
390
391         /*
392          * Scan 2MB entries.
393          */
394         if (error == 0) {
395                 hammer2_bmap_data_t *bmap;
396                 hammer2_key_t base_key;
397                 int count;
398                 int start;
399                 int n;
400
401                 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF);
402                 start = (int)((iter->bnext - key) >>
403                               HAMMER2_FREEMAP_LEVEL0_RADIX);
404                 KKASSERT(start >= 0 && start < HAMMER2_FREEMAP_COUNT);
405                 hammer2_chain_modify(trans, chain, 0);
406
407                 error = ENOSPC;
408                 for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
409                         int availchk;
410
411                         if (start + count >= HAMMER2_FREEMAP_COUNT &&
412                             start - count < 0) {
413                                 break;
414                         }
415
416                         /*
417                          * Calculate bmap pointer
418                          *
419                          * NOTE: bmap pointer is invalid if n >= FREEMAP_COUNT.
420                          */
421                         n = start + count;
422                         bmap = &chain->data->bmdata[n];
423
424                         if (n >= HAMMER2_FREEMAP_COUNT) {
425                                 availchk = 0;
426                         } else if (bmap->avail) {
427                                 availchk = 1;
428                         } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
429                                   (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
430                                 availchk = 1;
431                         } else {
432                                 availchk = 0;
433                         }
434
435                         if (availchk &&
436                             (bmap->class == 0 || bmap->class == class)) {
437                                 base_key = key + n * l0size;
438                                 error = hammer2_bmap_alloc(trans, hmp, bmap,
439                                                            class, n, radix,
440                                                            &base_key);
441                                 if (error != ENOSPC) {
442                                         key = base_key;
443                                         break;
444                                 }
445                         }
446
447                         /*
448                          * Must recalculate after potentially having called
449                          * hammer2_bmap_alloc() above in case chain was
450                          * reallocated.
451                          *
452                          * NOTE: bmap pointer is invalid if n < 0.
453                          */
454                         n = start - count;
455                         bmap = &chain->data->bmdata[n];
456                         if (n < 0) {
457                                 availchk = 0;
458                         } else if (bmap->avail) {
459                                 availchk = 1;
460                         } else if (radix < HAMMER2_FREEMAP_BLOCK_RADIX &&
461                                   (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK)) {
462                                 availchk = 1;
463                         } else {
464                                 availchk = 0;
465                         }
466
467                         if (availchk &&
468                             (bmap->class == 0 || bmap->class == class)) {
469                                 base_key = key + n * l0size;
470                                 error = hammer2_bmap_alloc(trans, hmp, bmap,
471                                                            class, n, radix,
472                                                            &base_key);
473                                 if (error != ENOSPC) {
474                                         key = base_key;
475                                         break;
476                                 }
477                         }
478                 }
479                 if (error == ENOSPC) {
480                         chain->bref.check.freemap.bigmask &=
481                                 (uint32_t)~((size_t)1 << radix);
482                 }
483                 /* XXX also scan down from original count */
484         }
485
486         if (error == 0) {
487                 /*
488                  * Assert validity.  Must be beyond the static allocator used
489                  * by newfs_hammer2 (and thus also beyond the aux area),
490                  * not go past the volume size, and must not be in the
491                  * reserved segment area for a zone.
492                  */
493                 KKASSERT(key >= hmp->voldata.allocator_beg &&
494                          key + bytes <= hmp->voldata.volu_size);
495                 KKASSERT((key & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
496                 bref->data_off = key | radix;
497
498 #if 0
499                 kprintf("alloc cp=%p %016jx %016jx using %016jx\n",
500                         chain,
501                         bref->key, bref->data_off, chain->bref.data_off);
502 #endif
503         } else if (error == ENOSPC) {
504                 /*
505                  * Return EAGAIN with next iteration in iter->bnext, or
506                  * return ENOSPC if the allocation map has been exhausted.
507                  */
508                 error = hammer2_freemap_iterate(trans, parentp, &chain, iter);
509         }
510
511         /*
512          * Cleanup
513          */
514         if (chain) {
515                 hammer2_chain_unlock(chain);
516                 hammer2_chain_drop(chain);
517         }
518         return (error);
519 }
520
521 /*
522  * Allocate (1<<radix) bytes from the bmap whos base data offset is (*basep).
523  *
524  * If the linear iterator is mid-block we use it directly (the bitmap should
525  * already be marked allocated), otherwise we search for a block in the bitmap
526  * that fits the allocation request.
527  *
528  * A partial bitmap allocation sets the minimum bitmap granularity (16KB)
529  * to fully allocated and adjusts the linear allocator to allow the
530  * remaining space to be allocated.
531  */
532 static
533 int
534 hammer2_bmap_alloc(hammer2_trans_t *trans, hammer2_dev_t *hmp,
535                    hammer2_bmap_data_t *bmap,
536                    uint16_t class, int n, int radix, hammer2_key_t *basep)
537 {
538         hammer2_io_t *dio;
539         size_t size;
540         size_t bgsize;
541         int bmradix;
542         hammer2_bitmap_t bmmask;
543         int offset;
544         int error;
545         int i;
546         int j;
547
548         /*
549          * Take into account 2-bits per block when calculating bmradix.
550          */
551         size = (size_t)1 << radix;
552
553         if (radix <= HAMMER2_FREEMAP_BLOCK_RADIX) {
554                 bmradix = 2;
555                 /* (16K) 2 bits per allocation block */
556         } else {
557                 bmradix = (hammer2_bitmap_t)2 <<
558                           (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
559                 /* (32K-256K) 4, 8, 16, 32 bits per allocation block */
560         }
561
562         /*
563          * Use the linear iterator to pack small allocations, otherwise
564          * fall-back to finding a free 16KB chunk.  The linear iterator
565          * is only valid when *NOT* on a freemap chunking boundary (16KB).
566          * If it is the bitmap must be scanned.  It can become invalid
567          * once we pack to the boundary.  We adjust it after a bitmap
568          * allocation only for sub-16KB allocations (so the perfectly good
569          * previous value can still be used for fragments when 16KB+
570          * allocations are made).
571          *
572          * Beware of hardware artifacts when bmradix == 64 (intermediate
573          * result can wind up being '1' instead of '0' if hardware masks
574          * bit-count & 31).
575          *
576          * NOTE: j needs to be even in the j= calculation.  As an artifact
577          *       of the /2 division, our bitmask has to clear bit 0.
578          *
579          * NOTE: TODO this can leave little unallocatable fragments lying
580          *       around.
581          */
582         if (((uint32_t)bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) + size <=
583             HAMMER2_FREEMAP_BLOCK_SIZE &&
584             (bmap->linear & HAMMER2_FREEMAP_BLOCK_MASK) &&
585             bmap->linear < HAMMER2_SEGSIZE) {
586                 KKASSERT(bmap->linear >= 0 &&
587                          bmap->linear + size <= HAMMER2_SEGSIZE &&
588                          (bmap->linear & (HAMMER2_ALLOC_MIN - 1)) == 0);
589                 offset = bmap->linear;
590                 i = offset / (HAMMER2_SEGSIZE / 8);
591                 j = (offset / (HAMMER2_FREEMAP_BLOCK_SIZE / 2)) & 30;
592                 bmmask = (bmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
593                          HAMMER2_BMAP_ALLONES :
594                          ((hammer2_bitmap_t)1 << bmradix) - 1;
595                 bmmask <<= j;
596                 bmap->linear = offset + size;
597         } else {
598                 for (i = 0; i < HAMMER2_BMAP_ELEMENTS; ++i) {
599                         bmmask = (bmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
600                                  HAMMER2_BMAP_ALLONES :
601                                  ((hammer2_bitmap_t)1 << bmradix) - 1;
602                         for (j = 0;
603                              j < HAMMER2_BMAP_BITS_PER_ELEMENT;
604                              j += bmradix) {
605                                 if ((bmap->bitmapq[i] & bmmask) == 0)
606                                         goto success;
607                                 bmmask <<= bmradix;
608                         }
609                 }
610                 /*fragments might remain*/
611                 /*KKASSERT(bmap->avail == 0);*/
612                 return (ENOSPC);
613 success:
614                 offset = i * (HAMMER2_SEGSIZE / HAMMER2_BMAP_ELEMENTS) +
615                          (j * (HAMMER2_FREEMAP_BLOCK_SIZE / 2));
616                 if (size & HAMMER2_FREEMAP_BLOCK_MASK)
617                         bmap->linear = offset + size;
618         }
619
620         /* 8 x (64/2) -> 256 x 16K -> 4MB */
621         KKASSERT(i >= 0 && i < HAMMER2_BMAP_ELEMENTS);
622
623         /*
624          * Optimize the buffer cache to avoid unnecessary read-before-write
625          * operations.
626          *
627          * The device block size could be larger than the allocation size
628          * so the actual bitmap test is somewhat more involved.  We have
629          * to use a compatible buffer size for this operation.
630          */
631         if ((bmap->bitmapq[i] & bmmask) == 0 &&
632             hammer2_devblksize(size) != size) {
633                 size_t psize = hammer2_devblksize(size);
634                 hammer2_off_t pmask = (hammer2_off_t)psize - 1;
635                 int pbmradix = (hammer2_bitmap_t)2 <<
636                                         (hammer2_devblkradix(radix) -
637                                HAMMER2_FREEMAP_BLOCK_RADIX);
638                 hammer2_bitmap_t pbmmask;
639                 int pradix = hammer2_getradix(psize);
640
641                 pbmmask = (pbmradix == HAMMER2_BMAP_BITS_PER_ELEMENT) ?
642                         HAMMER2_BMAP_ALLONES :
643                         ((hammer2_bitmap_t)1 << pbmradix) - 1;
644                 while ((pbmmask & bmmask) == 0)
645                         pbmmask <<= pbmradix;
646
647 #if 0
648                 kprintf("%016jx mask %016jx %016jx %016jx (%zd/%zd)\n",
649                         *basep + offset, bmap->bitmapq[i],
650                         pbmmask, bmmask, size, psize);
651 #endif
652
653                 if ((bmap->bitmapq[i] & pbmmask) == 0) {
654                         error = hammer2_io_newq(hmp,
655                                                 (*basep + (offset & ~pmask)) |
656                                                  pradix,
657                                                 psize, &dio);
658                         hammer2_io_bqrelse(&dio);
659                 }
660         }
661
662 #if 0
663         /*
664          * When initializing a new inode segment also attempt to initialize
665          * an adjacent segment.  Be careful not to index beyond the array
666          * bounds.
667          *
668          * We do this to try to localize inode accesses to improve
669          * directory scan rates.  XXX doesn't improve scan rates.
670          */
671         if (size == HAMMER2_INODE_BYTES) {
672                 if (n & 1) {
673                         if (bmap[-1].radix == 0 && bmap[-1].avail)
674                                 bmap[-1].radix = radix;
675                 } else {
676                         if (bmap[1].radix == 0 && bmap[1].avail)
677                                 bmap[1].radix = radix;
678                 }
679         }
680 #endif
681         /*
682          * Calculate the bitmap-granular change in bgsize for the volume
683          * header.  We cannot use the fine-grained change here because
684          * the bulkfree code can't undo it.  If the bitmap element is already
685          * marked allocated it has already been accounted for.
686          */
687         if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
688                 if (bmap->bitmapq[i] & bmmask)
689                         bgsize = 0;
690                 else
691                         bgsize = HAMMER2_FREEMAP_BLOCK_SIZE;
692         } else {
693                 bgsize = size;
694         }
695
696         /*
697          * Adjust the bitmap, set the class (it might have been 0),
698          * and available bytes, update the allocation offset (*basep)
699          * from the L0 base to the actual offset.
700          *
701          * avail must reflect the bitmap-granular availability.  The allocator
702          * tests will also check the linear iterator.
703          */
704         bmap->bitmapq[i] |= bmmask;
705         bmap->class = class;
706         bmap->avail -= bgsize;
707         *basep += offset;
708
709         /*
710          * Adjust the volume header's allocator_free parameter.  This
711          * parameter has to be fixed up by bulkfree which has no way to
712          * figure out sub-16K chunking, so it must be adjusted by the
713          * bitmap-granular size.
714          */
715         if (bgsize) {
716                 hammer2_voldata_lock(hmp);
717                 hammer2_voldata_modify(hmp);
718                 hmp->voldata.allocator_free -= bgsize;
719                 hammer2_voldata_unlock(hmp);
720         }
721
722         return(0);
723 }
724
725 static
726 void
727 hammer2_freemap_init(hammer2_trans_t *trans, hammer2_dev_t *hmp,
728                      hammer2_key_t key, hammer2_chain_t *chain)
729 {
730         hammer2_off_t l1size;
731         hammer2_off_t lokey;
732         hammer2_off_t hikey;
733         hammer2_bmap_data_t *bmap;
734         int count;
735
736         l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
737
738         /*
739          * Calculate the portion of the 2GB map that should be initialized
740          * as free.  Portions below or after will be initialized as allocated.
741          * SEGMASK-align the areas so we don't have to worry about sub-scans
742          * or endianess when using memset.
743          *
744          * (1) Ensure that all statically allocated space from newfs_hammer2
745          *     is marked allocated.
746          *
747          * (2) Ensure that the reserved area is marked allocated (typically
748          *     the first 4MB of the 2GB area being represented).
749          *
750          * (3) Ensure that any trailing space at the end-of-volume is marked
751          *     allocated.
752          *
753          * WARNING! It is possible for lokey to be larger than hikey if the
754          *          entire 2GB segment is within the static allocation.
755          */
756         lokey = (hmp->voldata.allocator_beg + HAMMER2_SEGMASK64) &
757                 ~HAMMER2_SEGMASK64;
758
759         if (lokey < H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
760                   HAMMER2_ZONE_SEG64) {
761                 lokey = H2FMBASE(key, HAMMER2_FREEMAP_LEVEL1_RADIX) +
762                         HAMMER2_ZONE_SEG64;
763         }
764
765         hikey = key + H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
766         if (hikey > hmp->voldata.volu_size) {
767                 hikey = hmp->voldata.volu_size & ~HAMMER2_SEGMASK64;
768         }
769
770         chain->bref.check.freemap.avail =
771                 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
772         bmap = &chain->data->bmdata[0];
773
774         for (count = 0; count < HAMMER2_FREEMAP_COUNT; ++count) {
775                 if (key < lokey || key >= hikey) {
776                         memset(bmap->bitmapq, -1,
777                                sizeof(bmap->bitmapq));
778                         bmap->avail = 0;
779                         bmap->linear = HAMMER2_SEGSIZE;
780                         chain->bref.check.freemap.avail -=
781                                 H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
782                 } else {
783                         bmap->avail = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
784                 }
785                 key += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
786                 ++bmap;
787         }
788 }
789
790 /*
791  * The current Level 1 freemap has been exhausted, iterate to the next
792  * one, return ENOSPC if no freemaps remain.
793  *
794  * XXX this should rotate back to the beginning to handle freed-up space
795  * XXX or use intermediate entries to locate free space. TODO
796  */
797 static int
798 hammer2_freemap_iterate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
799                         hammer2_chain_t **chainp, hammer2_fiterate_t *iter)
800 {
801         hammer2_dev_t *hmp = (*parentp)->hmp;
802
803         iter->bnext &= ~(H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX) - 1);
804         iter->bnext += H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
805         if (iter->bnext >= hmp->voldata.volu_size) {
806                 iter->bnext = 0;
807                 if (++iter->loops == 2)
808                         return (ENOSPC);
809         }
810         return(EAGAIN);
811 }
812
813 /*
814  * Adjust the bit-pattern for data in the freemap bitmap according to
815  * (how).  This code is called from on-mount recovery to fixup (mark
816  * as allocated) blocks whos freemap upates might not have been committed
817  * in the last crash and is used by the bulk freemap scan to stage frees.
818  *
819  * XXX currently disabled when how == 0 (the normal real-time case).  At
820  * the moment we depend on the bulk freescan to actually free blocks.  It
821  * will still call this routine with a non-zero how to stage possible frees
822  * and to do the actual free.
823  */
824 void
825 hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_dev_t *hmp,
826                        hammer2_blockref_t *bref, int how)
827 {
828         hammer2_off_t data_off = bref->data_off;
829         hammer2_chain_t *chain;
830         hammer2_chain_t *parent;
831         hammer2_bmap_data_t *bmap;
832         hammer2_key_t key;
833         hammer2_key_t key_dummy;
834         hammer2_off_t l0size;
835         hammer2_off_t l1size;
836         hammer2_off_t l1mask;
837         hammer2_bitmap_t *bitmap;
838         const hammer2_bitmap_t bmmask00 = 0;
839         hammer2_bitmap_t bmmask01;
840         hammer2_bitmap_t bmmask10;
841         hammer2_bitmap_t bmmask11;
842         size_t bytes;
843         uint16_t class;
844         int radix;
845         int start;
846         int count;
847         int modified = 0;
848         int cache_index = -1;
849         int error;
850
851         KKASSERT(how == HAMMER2_FREEMAP_DORECOVER);
852
853         radix = (int)data_off & HAMMER2_OFF_MASK_RADIX;
854         data_off &= ~HAMMER2_OFF_MASK_RADIX;
855         KKASSERT(radix <= HAMMER2_RADIX_MAX);
856
857         bytes = (size_t)1 << radix;
858         class = (bref->type << 8) | hammer2_devblkradix(radix);
859
860         /*
861          * We can't adjust thre freemap for data allocations made by
862          * newfs_hammer2.
863          */
864         if (data_off < hmp->voldata.allocator_beg)
865                 return;
866
867         KKASSERT((data_off & HAMMER2_ZONE_MASK64) >= HAMMER2_ZONE_SEG);
868
869         /*
870          * Lookup the level1 freemap chain.  The chain must exist.
871          */
872         key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL1_RADIX);
873         l0size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL0_RADIX);
874         l1size = H2FMSHIFT(HAMMER2_FREEMAP_LEVEL1_RADIX);
875         l1mask = l1size - 1;
876
877         parent = &hmp->fchain;
878         hammer2_chain_ref(parent);
879         hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
880
881         chain = hammer2_chain_lookup(&parent, &key_dummy, key, key + l1mask,
882                                      &cache_index,
883                                      HAMMER2_LOOKUP_ALWAYS |
884                                      HAMMER2_LOOKUP_MATCHIND);
885
886         /*
887          * Stop early if we are trying to free something but no leaf exists.
888          */
889         if (chain == NULL && how != HAMMER2_FREEMAP_DORECOVER) {
890                 kprintf("hammer2_freemap_adjust: %016jx: no chain\n",
891                         (intmax_t)bref->data_off);
892                 goto done;
893         }
894         if (chain->error) {
895                 kprintf("hammer2_freemap_adjust: %016jx: error %s\n",
896                         (intmax_t)bref->data_off,
897                         hammer2_error_str(chain->error));
898                 hammer2_chain_unlock(chain);
899                 hammer2_chain_drop(chain);
900                 chain = NULL;
901                 goto done;
902         }
903
904         /*
905          * Create any missing leaf(s) if we are doing a recovery (marking
906          * the block(s) as being allocated instead of being freed).  Be sure
907          * to initialize the auxillary freemap tracking info in the
908          * bref.check.freemap structure.
909          */
910         if (chain == NULL && how == HAMMER2_FREEMAP_DORECOVER) {
911                 error = hammer2_chain_create(trans, &parent, &chain, hmp->spmp,
912                                      key, HAMMER2_FREEMAP_LEVEL1_RADIX,
913                                      HAMMER2_BREF_TYPE_FREEMAP_LEAF,
914                                      HAMMER2_FREEMAP_LEVELN_PSIZE,
915                                      0);
916
917                 if (hammer2_debug & 0x0040) {
918                         kprintf("fixup create chain %p %016jx:%d\n",
919                                 chain, chain->bref.key, chain->bref.keybits);
920                 }
921
922                 if (error == 0) {
923                         hammer2_chain_modify(trans, chain, 0);
924                         bzero(&chain->data->bmdata[0],
925                               HAMMER2_FREEMAP_LEVELN_PSIZE);
926                         chain->bref.check.freemap.bigmask = (uint32_t)-1;
927                         chain->bref.check.freemap.avail = l1size;
928                         /* bref.methods should already be inherited */
929
930                         hammer2_freemap_init(trans, hmp, key, chain);
931                 }
932                 /* XXX handle error */
933         }
934
935 #if FREEMAP_DEBUG
936         kprintf("FREEMAP ADJUST TYPE %d %016jx/%d DATA_OFF=%016jx\n",
937                 chain->bref.type, chain->bref.key,
938                 chain->bref.keybits, chain->bref.data_off);
939 #endif
940
941         /*
942          * Calculate the bitmask (runs in 2-bit pairs).
943          */
944         start = ((int)(data_off >> HAMMER2_FREEMAP_BLOCK_RADIX) & 15) * 2;
945         bmmask01 = (hammer2_bitmap_t)1 << start;
946         bmmask10 = (hammer2_bitmap_t)2 << start;
947         bmmask11 = (hammer2_bitmap_t)3 << start;
948
949         /*
950          * Fixup the bitmap.  Partial blocks cannot be fully freed unless
951          * a bulk scan is able to roll them up.
952          */
953         if (radix < HAMMER2_FREEMAP_BLOCK_RADIX) {
954                 count = 1;
955                 if (how == HAMMER2_FREEMAP_DOREALFREE)
956                         how = HAMMER2_FREEMAP_DOMAYFREE;
957         } else {
958                 count = 1 << (radix - HAMMER2_FREEMAP_BLOCK_RADIX);
959         }
960
961         /*
962          * [re]load the bmap and bitmap pointers.  Each bmap entry covers
963          * a 2MB swath.  The bmap itself (LEVEL1) covers 2GB.
964          *
965          * Be sure to reset the linear iterator to ensure that the adjustment
966          * is not ignored.
967          */
968 again:
969         bmap = &chain->data->bmdata[(int)(data_off >> HAMMER2_SEGRADIX) &
970                                     (HAMMER2_FREEMAP_COUNT - 1)];
971         bitmap = &bmap->bitmapq[(int)(data_off >> (HAMMER2_SEGRADIX - 3)) & 7];
972
973         if (modified)
974                 bmap->linear = 0;
975
976         while (count) {
977                 KKASSERT(bmmask11);
978                 if (how == HAMMER2_FREEMAP_DORECOVER) {
979                         /*
980                          * Recovery request, mark as allocated.
981                          */
982                         if ((*bitmap & bmmask11) != bmmask11) {
983                                 if (modified == 0) {
984                                         hammer2_chain_modify(trans, chain, 0);
985                                         modified = 1;
986                                         goto again;
987                                 }
988                                 if ((*bitmap & bmmask11) == bmmask00) {
989                                         bmap->avail -=
990                                                 HAMMER2_FREEMAP_BLOCK_SIZE;
991                                 }
992                                 if (bmap->class == 0)
993                                         bmap->class = class;
994                                 *bitmap |= bmmask11;
995                                 if (hammer2_debug & 0x0040) {
996                                         kprintf("hammer2_freemap_recover: "
997                                                 "fixup type=%02x "
998                                                 "block=%016jx/%zd\n",
999                                                 bref->type, data_off, bytes);
1000                                 }
1001                         } else {
1002                                 /*
1003                                 kprintf("hammer2_freemap_recover:  good "
1004                                         "type=%02x block=%016jx/%zd\n",
1005                                         bref->type, data_off, bytes);
1006                                 */
1007                         }
1008                 }
1009 #if 0
1010                 /*
1011                  * XXX this stuff doesn't work, avail is miscalculated and
1012                  * code 10 means something else now.
1013                  */
1014                 else if ((*bitmap & bmmask11) == bmmask11) {
1015                         /*
1016                          * Mayfree/Realfree request and bitmap is currently
1017                          * marked as being fully allocated.
1018                          */
1019                         if (!modified) {
1020                                 hammer2_chain_modify(trans, chain, 0);
1021                                 modified = 1;
1022                                 goto again;
1023                         }
1024                         if (how == HAMMER2_FREEMAP_DOREALFREE)
1025                                 *bitmap &= ~bmmask11;
1026                         else
1027                                 *bitmap = (*bitmap & ~bmmask11) | bmmask10;
1028                 } else if ((*bitmap & bmmask11) == bmmask10) {
1029                         /*
1030                          * Mayfree/Realfree request and bitmap is currently
1031                          * marked as being possibly freeable.
1032                          */
1033                         if (how == HAMMER2_FREEMAP_DOREALFREE) {
1034                                 if (!modified) {
1035                                         hammer2_chain_modify(trans, chain, 0);
1036                                         modified = 1;
1037                                         goto again;
1038                                 }
1039                                 *bitmap &= ~bmmask11;
1040                         }
1041                 } else {
1042                         /*
1043                          * 01 - Not implemented, currently illegal state
1044                          * 00 - Not allocated at all, illegal free.
1045                          */
1046                         panic("hammer2_freemap_adjust: "
1047                               "Illegal state %08x(%08x)",
1048                               *bitmap, *bitmap & bmmask11);
1049                 }
1050 #endif
1051                 --count;
1052                 bmmask01 <<= 2;
1053                 bmmask10 <<= 2;
1054                 bmmask11 <<= 2;
1055         }
1056 #if HAMMER2_BMAP_ELEMENTS != 8
1057 #error "hammer2_freemap.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
1058 #endif
1059         if (how == HAMMER2_FREEMAP_DOREALFREE && modified) {
1060                 bmap->avail += 1 << radix;
1061                 KKASSERT(bmap->avail <= HAMMER2_SEGSIZE);
1062                 if (bmap->avail == HAMMER2_SEGSIZE &&
1063                     bmap->bitmapq[0] == 0 &&
1064                     bmap->bitmapq[1] == 0 &&
1065                     bmap->bitmapq[2] == 0 &&
1066                     bmap->bitmapq[3] == 0 &&
1067                     bmap->bitmapq[4] == 0 &&
1068                     bmap->bitmapq[5] == 0 &&
1069                     bmap->bitmapq[6] == 0 &&
1070                     bmap->bitmapq[7] == 0) {
1071                         key = H2FMBASE(data_off, HAMMER2_FREEMAP_LEVEL0_RADIX);
1072                         kprintf("Freeseg %016jx\n", (intmax_t)key);
1073                         bmap->class = 0;
1074                 }
1075         }
1076
1077         /*
1078          * chain->bref.check.freemap.bigmask (XXX)
1079          *
1080          * Setting bigmask is a hint to the allocation code that there might
1081          * be something allocatable.  We also set this in recovery... it
1082          * doesn't hurt and we might want to use the hint for other validation
1083          * operations later on.
1084          */
1085         if (modified)
1086                 chain->bref.check.freemap.bigmask |= 1 << radix;
1087
1088         hammer2_chain_unlock(chain);
1089         hammer2_chain_drop(chain);
1090 done:
1091         hammer2_chain_unlock(parent);
1092         hammer2_chain_drop(parent);
1093 }
1094
1095 /*
1096  * Validate the freemap, in three stages.
1097  *
1098  * stage-1      ALLOCATED     -> POSSIBLY FREE
1099  *              POSSIBLY FREE -> POSSIBLY FREE (type corrected)
1100  *
1101  *      This transitions bitmap entries from ALLOCATED to POSSIBLY FREE.
1102  *      The POSSIBLY FREE state does not mean that a block is actually free
1103  *      and may be transitioned back to ALLOCATED in stage-2.
1104  *
1105  *      This is typically done during normal filesystem operations when
1106  *      something is deleted or a block is replaced.
1107  *
1108  *      This is done by bulkfree in-bulk after a memory-bounded meta-data
1109  *      scan to try to determine what might be freeable.
1110  *
1111  *      This can be done unconditionally through a freemap scan when the
1112  *      intention is to brute-force recover the proper state of the freemap.
1113  *
1114  * stage-2      POSSIBLY FREE -> ALLOCATED      (scan metadata topology)
1115  *
1116  *      This is done by bulkfree during a meta-data scan to ensure that
1117  *      all blocks still actually allocated by the filesystem are marked
1118  *      as such.
1119  *
1120  *      NOTE! Live filesystem transitions to POSSIBLY FREE can occur while
1121  *            the bulkfree stage-2 and stage-3 is running.  The live filesystem
1122  *            will use the alternative POSSIBLY FREE type (2) to prevent
1123  *            stage-3 from improperly transitioning unvetted possibly-free
1124  *            blocks to FREE.
1125  *
1126  * stage-3      POSSIBLY FREE (type 1) -> FREE  (scan freemap)
1127  *
1128  *      This is done by bulkfree to finalize POSSIBLY FREE states.
1129  *
1130  */