2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
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 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This module handles low level logical file I/O (strategy) which backs
38 * the logical buffer cache.
40 * [De]compression, zero-block, check codes, and buffer cache operations
41 * for file data is handled here.
43 * Live dedup makes its home here as well.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
52 #include <sys/namei.h>
53 #include <sys/mount.h>
54 #include <sys/vnode.h>
55 #include <sys/mountctl.h>
56 #include <sys/dirent.h>
58 #include <sys/objcache.h>
59 #include <sys/event.h>
61 #include <vfs/fifofs/fifo.h>
64 #include "hammer2_lz4.h"
66 #include "zlib/hammer2_zlib.h"
68 struct objcache *cache_buffer_read;
69 struct objcache *cache_buffer_write;
72 * Strategy code (async logical file buffer I/O from system)
74 * Except for the transaction init (which should normally not block),
75 * we essentially run the strategy operation asynchronously via a XOP.
77 * XXX This isn't supposed to be able to deadlock against vfs_sync vfsync()
78 * calls but it has in the past when multiple flushes are queued.
80 * XXX We currently terminate the transaction once we get a quorum, otherwise
81 * the frontend can stall, but this can leave the remaining nodes with
82 * a potential flush conflict. We need to delay flushes on those nodes
83 * until running transactions complete separately from the normal
84 * transaction sequencing. FIXME TODO.
86 static void hammer2_strategy_xop_read(hammer2_xop_t *arg, int clindex);
87 static void hammer2_strategy_xop_write(hammer2_xop_t *arg, int clindex);
88 static int hammer2_strategy_read(struct vop_strategy_args *ap);
89 static int hammer2_strategy_write(struct vop_strategy_args *ap);
90 static void hammer2_strategy_read_completion(hammer2_chain_t *chain,
91 char *data, struct bio *bio);
93 static hammer2_off_t hammer2_dedup_lookup(hammer2_dev_t *hmp,
94 char **datap, int pblksize);
100 #define TIMER(which) do { \
102 h2timer[h2lid] += (int)(ticks - h2last);\
108 hammer2_vop_strategy(struct vop_strategy_args *ap)
119 error = hammer2_strategy_read(ap);
120 ++hammer2_iod_file_read;
123 error = hammer2_strategy_write(ap);
124 ++hammer2_iod_file_write;
127 bp->b_error = error = EINVAL;
128 bp->b_flags |= B_ERROR;
136 * Return the largest contiguous physical disk range for the logical
139 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
141 * Basically disabled, the logical buffer write thread has to deal with
142 * buffers one-at-a-time. Note that this should not prevent cluster_read()
143 * from reading-ahead, it simply prevents it from trying form a single
144 * cluster buffer for the logical request. H2 already uses 64KB buffers!
147 hammer2_vop_bmap(struct vop_bmap_args *ap)
149 *ap->a_doffsetp = NOOFFSET;
157 /****************************************************************************
159 ****************************************************************************/
161 * Callback used in read path in case that a block is compressed with LZ4.
165 hammer2_decompress_LZ4_callback(const char *data, u_int bytes, struct bio *bio)
168 char *compressed_buffer;
175 if bio->bio_caller_info2.index &&
176 bio->bio_caller_info1.uvalue32 !=
177 crc32(bp->b_data, bp->b_bufsize) --- return error
180 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
181 compressed_size = *(const int *)data;
182 KKASSERT((uint32_t)compressed_size <= bytes - sizeof(int));
184 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
185 result = LZ4_decompress_safe(__DECONST(char *, &data[sizeof(int)]),
190 kprintf("READ PATH: Error during decompression."
192 (intmax_t)bio->bio_offset, bytes);
193 /* make sure it isn't random garbage */
194 bzero(compressed_buffer, bp->b_bufsize);
196 KKASSERT(result <= bp->b_bufsize);
197 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
198 if (result < bp->b_bufsize)
199 bzero(bp->b_data + result, bp->b_bufsize - result);
200 objcache_put(cache_buffer_read, compressed_buffer);
202 bp->b_flags |= B_AGE;
206 * Callback used in read path in case that a block is compressed with ZLIB.
207 * It is almost identical to LZ4 callback, so in theory they can be unified,
208 * but we didn't want to make changes in bio structure for that.
212 hammer2_decompress_ZLIB_callback(const char *data, u_int bytes, struct bio *bio)
215 char *compressed_buffer;
216 z_stream strm_decompress;
222 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
223 strm_decompress.avail_in = 0;
224 strm_decompress.next_in = Z_NULL;
226 ret = inflateInit(&strm_decompress);
229 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
231 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
232 strm_decompress.next_in = __DECONST(char *, data);
234 /* XXX supply proper size, subset of device bp */
235 strm_decompress.avail_in = bytes;
236 strm_decompress.next_out = compressed_buffer;
237 strm_decompress.avail_out = bp->b_bufsize;
239 ret = inflate(&strm_decompress, Z_FINISH);
240 if (ret != Z_STREAM_END) {
241 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
242 bzero(compressed_buffer, bp->b_bufsize);
244 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
245 result = bp->b_bufsize - strm_decompress.avail_out;
246 if (result < bp->b_bufsize)
247 bzero(bp->b_data + result, strm_decompress.avail_out);
248 objcache_put(cache_buffer_read, compressed_buffer);
249 ret = inflateEnd(&strm_decompress);
252 bp->b_flags |= B_AGE;
256 * Logical buffer I/O, async read.
260 hammer2_strategy_read(struct vop_strategy_args *ap)
262 hammer2_xop_strategy_t *xop;
272 nbio = push_bio(bio);
274 lbase = bio->bio_offset;
275 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
277 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_STRATEGY);
281 hammer2_mtx_init(&xop->lock, "h2bior");
282 hammer2_xop_start(&xop->head, hammer2_strategy_xop_read);
283 /* asynchronous completion */
289 * Per-node XOP (threaded), do a synchronous lookup of the chain and
290 * its data. The frontend is asynchronous, so we are also responsible
291 * for racing to terminate the frontend.
295 hammer2_strategy_xop_read(hammer2_xop_t *arg, int clindex)
297 hammer2_xop_strategy_t *xop = &arg->xop_strategy;
298 hammer2_chain_t *parent;
299 hammer2_chain_t *chain;
300 hammer2_key_t key_dummy;
304 int cache_index = -1;
313 * This is difficult to optimize. The logical buffer might be
314 * partially dirty (contain dummy zero-fill pages), which would
315 * mess up our crc calculation if we were to try a direct read.
316 * So for now we always double-buffer through the underlying
319 * If not for the above problem we could conditionalize on
320 * (1) 64KB buffer, (2) one chain (not multi-master) and
321 * (3) !hammer2_double_buffer, and issue a direct read into the
324 parent = hammer2_inode_chain(xop->head.ip1, clindex,
325 HAMMER2_RESOLVE_ALWAYS |
326 HAMMER2_RESOLVE_SHARED);
329 chain = hammer2_chain_lookup(&parent, &key_dummy,
332 HAMMER2_LOOKUP_ALWAYS |
333 HAMMER2_LOOKUP_SHARED);
334 error = chain ? chain->error : 0;
340 error = hammer2_xop_feed(&xop->head, chain, clindex, error);
343 hammer2_chain_unlock(chain);
344 hammer2_chain_drop(chain);
347 hammer2_chain_unlock(parent);
348 hammer2_chain_drop(parent);
350 chain = NULL; /* safety */
351 parent = NULL; /* safety */
355 * Race to finish the frontend
359 hammer2_mtx_ex(&xop->lock);
361 hammer2_mtx_unlock(&xop->lock);
366 * Async operation has not completed and we now own the lock.
367 * Determine if we can complete the operation by issuing the
368 * frontend collection non-blocking.
370 * H2 double-buffers the data, setting B_NOTMETA on the logical
371 * buffer hints to the OS that the logical buffer should not be
372 * swapcached (since the device buffer can be).
374 * Also note that even for compressed data we would rather the
375 * kernel cache/swapcache device buffers more and (decompressed)
376 * logical buffers less, since that will significantly improve
377 * the amount of end-user data that can be cached.
379 error = hammer2_xop_collect(&xop->head, HAMMER2_XOP_COLLECT_NOWAIT);
385 hammer2_mtx_unlock(&xop->lock);
386 bp->b_flags |= B_NOTMETA;
387 chain = xop->head.cluster.focus;
388 hammer2_strategy_read_completion(chain, (char *)chain->data,
391 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
395 hammer2_mtx_unlock(&xop->lock);
396 bp->b_flags |= B_NOTMETA;
399 bzero(bp->b_data, bp->b_bcount);
401 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
404 hammer2_mtx_unlock(&xop->lock);
407 kprintf("strategy_xop_read: error %d loff=%016jx\n",
408 error, bp->b_loffset);
410 hammer2_mtx_unlock(&xop->lock);
411 bp->b_flags |= B_ERROR;
414 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
422 hammer2_strategy_read_completion(hammer2_chain_t *chain, char *data,
425 struct buf *bp = bio->bio_buf;
427 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
429 * Data is embedded in the inode (copy from inode).
431 bcopy(((hammer2_inode_data_t *)data)->u.data,
432 bp->b_data, HAMMER2_EMBEDDED_BYTES);
433 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
434 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
437 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
439 * Data is on-media, record for live dedup. Release the
440 * chain (try to free it) when done. The data is still
441 * cached by both the buffer cache in front and the
442 * block device behind us. This leaves more room in the
443 * LRU chain cache for meta-data chains which we really
446 hammer2_dedup_record(chain, data);
447 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
450 * Decompression and copy.
452 switch (HAMMER2_DEC_COMP(chain->bref.methods)) {
453 case HAMMER2_COMP_LZ4:
454 hammer2_decompress_LZ4_callback(data, chain->bytes,
456 /* b_resid set by call */
458 case HAMMER2_COMP_ZLIB:
459 hammer2_decompress_ZLIB_callback(data, chain->bytes,
461 /* b_resid set by call */
463 case HAMMER2_COMP_NONE:
464 KKASSERT(chain->bytes <= bp->b_bcount);
465 bcopy(data, bp->b_data, chain->bytes);
466 if (chain->bytes < bp->b_bcount) {
467 bzero(bp->b_data + chain->bytes,
468 bp->b_bcount - chain->bytes);
474 panic("hammer2_strategy_read: "
475 "unknown compression type");
478 panic("hammer2_strategy_read: unknown bref type");
482 /****************************************************************************
484 ****************************************************************************/
487 * Functions for compression in threads,
488 * from hammer2_vnops.c
490 static void hammer2_write_file_core(struct buf *bp, hammer2_inode_t *ip,
491 hammer2_chain_t **parentp,
492 hammer2_key_t lbase, int ioflag, int pblksize,
493 hammer2_tid_t mtid, int *errorp);
494 static void hammer2_compress_and_write(struct buf *bp, hammer2_inode_t *ip,
495 hammer2_chain_t **parentp,
496 hammer2_key_t lbase, int ioflag, int pblksize,
497 hammer2_tid_t mtid, int *errorp,
498 int comp_algo, int check_algo);
499 static void hammer2_zero_check_and_write(struct buf *bp, hammer2_inode_t *ip,
500 hammer2_chain_t **parentp,
501 hammer2_key_t lbase, int ioflag, int pblksize,
502 hammer2_tid_t mtid, int *errorp,
504 static int test_block_zeros(const char *buf, size_t bytes);
505 static void zero_write(struct buf *bp, hammer2_inode_t *ip,
506 hammer2_chain_t **parentp,
508 hammer2_tid_t mtid, int *errorp);
509 static void hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp,
510 int ioflag, int pblksize,
511 hammer2_tid_t mtid, int *errorp,
516 hammer2_strategy_write(struct vop_strategy_args *ap)
518 hammer2_xop_strategy_t *xop;
529 hammer2_lwinprog_ref(pmp);
530 hammer2_trans_assert_strategy(pmp);
531 hammer2_trans_init(pmp, HAMMER2_TRANS_BUFCACHE);
533 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING |
534 HAMMER2_XOP_STRATEGY);
537 xop->lbase = bio->bio_offset;
538 hammer2_mtx_init(&xop->lock, "h2biow");
539 hammer2_xop_start(&xop->head, hammer2_strategy_xop_write);
540 /* asynchronous completion */
542 hammer2_lwinprog_wait(pmp, hammer2_flush_pipe);
548 * Per-node XOP (threaded). Write the logical buffer to the media.
552 hammer2_strategy_xop_write(hammer2_xop_t *arg, int clindex)
554 hammer2_xop_strategy_t *xop = &arg->xop_strategy;
555 hammer2_chain_t *parent;
569 /* hammer2_trans_init(parent->hmp->spmp, HAMMER2_TRANS_BUFCACHE); */
571 lblksize = hammer2_calc_logical(ip, bio->bio_offset, &lbase, NULL);
572 pblksize = hammer2_calc_physical(ip, lbase);
573 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
574 hammer2_write_file_core(bp, ip, &parent,
575 lbase, IO_ASYNC, pblksize,
576 xop->head.mtid, &error);
578 hammer2_chain_unlock(parent);
579 hammer2_chain_drop(parent);
580 parent = NULL; /* safety */
582 hammer2_xop_feed(&xop->head, NULL, clindex, error);
585 * Race to finish the frontend
589 hammer2_mtx_ex(&xop->lock);
591 hammer2_mtx_unlock(&xop->lock);
596 * Async operation has not completed and we now own the lock.
597 * Determine if we can complete the operation by issuing the
598 * frontend collection non-blocking.
600 * H2 double-buffers the data, setting B_NOTMETA on the logical
601 * buffer hints to the OS that the logical buffer should not be
602 * swapcached (since the device buffer can be).
604 error = hammer2_xop_collect(&xop->head, HAMMER2_XOP_COLLECT_NOWAIT);
606 if (error == EINPROGRESS) {
607 hammer2_mtx_unlock(&xop->lock);
612 * Async operation has completed.
615 hammer2_mtx_unlock(&xop->lock);
617 if (error == ENOENT || error == 0) {
618 bp->b_flags |= B_NOTMETA;
623 kprintf("strategy_xop_write: error %d loff=%016jx\n",
624 error, bp->b_loffset);
625 bp->b_flags |= B_ERROR;
629 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
630 hammer2_trans_assert_strategy(ip->pmp);
631 hammer2_lwinprog_drop(ip->pmp);
632 hammer2_trans_done(ip->pmp);
636 * Wait for pending I/O to complete
639 hammer2_bioq_sync(hammer2_pfs_t *pmp)
641 hammer2_lwinprog_wait(pmp, 0);
645 * Create a new cluster at (cparent, lbase) and assign physical storage,
646 * returning a cluster suitable for I/O. The cluster will be in a modified
649 * cparent can wind up being anything.
651 * If datap is not NULL, *datap points to the real data we intend to write.
652 * If we can dedup the storage location we set *datap to NULL to indicate
653 * to the caller that a dedup occurred.
655 * NOTE: Special case for data embedded in inode.
659 hammer2_assign_physical(hammer2_inode_t *ip, hammer2_chain_t **parentp,
660 hammer2_key_t lbase, int pblksize,
661 hammer2_tid_t mtid, char **datap, int *errorp)
663 hammer2_chain_t *chain;
664 hammer2_key_t key_dummy;
665 hammer2_off_t dedup_off;
666 int pradix = hammer2_getradix(pblksize);
667 int cache_index = -1;
670 * Locate the chain associated with lbase, return a locked chain.
671 * However, do not instantiate any data reference (which utilizes a
672 * device buffer) because we will be using direct IO via the
673 * logical buffer cache buffer.
676 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
679 chain = hammer2_chain_lookup(parentp, &key_dummy,
682 HAMMER2_LOOKUP_NODATA);
685 * The lookup code should not return a DELETED chain to us, unless
686 * its a short-file embedded in the inode. Then it is possible for
687 * the lookup to return a deleted inode.
689 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED) &&
690 chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
691 kprintf("assign physical deleted chain @ "
692 "%016jx (%016jx.%02x) ip %016jx\n",
693 lbase, chain->bref.data_off, chain->bref.type,
700 * We found a hole, create a new chain entry.
702 * NOTE: DATA chains are created without device backing
703 * store (nor do we want any).
705 dedup_off = hammer2_dedup_lookup((*parentp)->hmp, datap,
707 *errorp = hammer2_chain_create(parentp, &chain,
709 HAMMER2_ENC_CHECK(ip->meta.check_algo) |
710 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE),
711 lbase, HAMMER2_PBUFRADIX,
712 HAMMER2_BREF_TYPE_DATA,
716 panic("hammer2_chain_create: par=%p error=%d\n",
720 /*ip->delta_dcount += pblksize;*/
722 switch (chain->bref.type) {
723 case HAMMER2_BREF_TYPE_INODE:
725 * The data is embedded in the inode, which requires
728 hammer2_chain_modify_ip(ip, chain, mtid, 0);
730 case HAMMER2_BREF_TYPE_DATA:
731 dedup_off = hammer2_dedup_lookup(chain->hmp, datap,
733 if (chain->bytes != pblksize) {
734 hammer2_chain_resize(ip, *parentp, chain,
737 HAMMER2_MODIFY_OPTDATA);
741 * DATA buffers must be marked modified whether the
742 * data is in a logical buffer or not. We also have
743 * to make this call to fixup the chain data pointers
744 * after resizing in case this is an encrypted or
747 hammer2_chain_modify(chain, mtid, dedup_off,
748 HAMMER2_MODIFY_OPTDATA);
751 panic("hammer2_assign_physical: bad type");
761 * hammer2_write_file_core() - hammer2_write_thread() helper
763 * The core write function which determines which path to take
764 * depending on compression settings. We also have to locate the
765 * related chains so we can calculate and set the check data for
770 hammer2_write_file_core(struct buf *bp, hammer2_inode_t *ip,
771 hammer2_chain_t **parentp,
772 hammer2_key_t lbase, int ioflag, int pblksize,
773 hammer2_tid_t mtid, int *errorp)
775 hammer2_chain_t *chain;
776 char *data = bp->b_data;
780 switch(HAMMER2_DEC_ALGO(ip->meta.comp_algo)) {
781 case HAMMER2_COMP_NONE:
783 * We have to assign physical storage to the buffer
784 * we intend to dirty or write now to avoid deadlocks
785 * in the strategy code later.
787 * This can return NOOFFSET for inode-embedded data.
788 * The strategy code will take care of it in that case.
790 chain = hammer2_assign_physical(ip, parentp, lbase, pblksize,
791 mtid, &data, errorp);
792 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
793 hammer2_inode_data_t *wipdata;
795 wipdata = &chain->data->ipdata;
796 KKASSERT(wipdata->meta.op_flags &
797 HAMMER2_OPFLAG_DIRECTDATA);
798 KKASSERT(bp->b_loffset == 0);
799 bcopy(bp->b_data, wipdata->u.data,
800 HAMMER2_EMBEDDED_BYTES);
801 ++hammer2_iod_file_wembed;
802 } else if (data == NULL) {
804 * Copy of data already present on-media.
806 chain->bref.methods =
807 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
808 HAMMER2_ENC_CHECK(ip->meta.check_algo);
809 hammer2_chain_setcheck(chain, bp->b_data);
811 hammer2_write_bp(chain, bp, ioflag, pblksize,
812 mtid, errorp, ip->meta.check_algo);
815 hammer2_chain_unlock(chain);
816 hammer2_chain_drop(chain);
819 case HAMMER2_COMP_AUTOZERO:
821 * Check for zero-fill only
823 hammer2_zero_check_and_write(bp, ip, parentp,
824 lbase, ioflag, pblksize,
826 ip->meta.check_algo);
828 case HAMMER2_COMP_LZ4:
829 case HAMMER2_COMP_ZLIB:
832 * Check for zero-fill and attempt compression.
834 hammer2_compress_and_write(bp, ip, parentp,
835 lbase, ioflag, pblksize,
838 ip->meta.check_algo);
846 * Generic function that will perform the compression in compression
847 * write path. The compression algorithm is determined by the settings
848 * obtained from inode.
852 hammer2_compress_and_write(struct buf *bp, hammer2_inode_t *ip,
853 hammer2_chain_t **parentp,
854 hammer2_key_t lbase, int ioflag, int pblksize,
855 hammer2_tid_t mtid, int *errorp, int comp_algo, int check_algo)
857 hammer2_chain_t *chain;
863 if (test_block_zeros(bp->b_data, pblksize)) {
864 zero_write(bp, ip, parentp, lbase, mtid, errorp);
871 KKASSERT(pblksize / 2 <= 32768);
873 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
874 z_stream strm_compress;
878 switch(HAMMER2_DEC_ALGO(comp_algo)) {
879 case HAMMER2_COMP_LZ4:
880 comp_buffer = objcache_get(cache_buffer_write,
882 comp_size = LZ4_compress_limitedOutput(
884 &comp_buffer[sizeof(int)],
886 pblksize / 2 - sizeof(int));
888 * We need to prefix with the size, LZ4
889 * doesn't do it for us. Add the related
892 *(int *)comp_buffer = comp_size;
894 comp_size += sizeof(int);
896 case HAMMER2_COMP_ZLIB:
897 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
899 comp_level = 6; /* default zlib compression */
900 else if (comp_level < 6)
902 else if (comp_level > 9)
904 ret = deflateInit(&strm_compress, comp_level);
906 kprintf("HAMMER2 ZLIB: fatal error "
907 "on deflateInit.\n");
910 comp_buffer = objcache_get(cache_buffer_write,
912 strm_compress.next_in = bp->b_data;
913 strm_compress.avail_in = pblksize;
914 strm_compress.next_out = comp_buffer;
915 strm_compress.avail_out = pblksize / 2;
916 ret = deflate(&strm_compress, Z_FINISH);
917 if (ret == Z_STREAM_END) {
918 comp_size = pblksize / 2 -
919 strm_compress.avail_out;
923 ret = deflateEnd(&strm_compress);
926 kprintf("Error: Unknown compression method.\n");
927 kprintf("Comp_method = %d.\n", comp_algo);
932 if (comp_size == 0) {
934 * compression failed or turned off
936 comp_block_size = pblksize; /* safety */
937 if (++ip->comp_heuristic > 128)
938 ip->comp_heuristic = 8;
941 * compression succeeded
943 ip->comp_heuristic = 0;
944 if (comp_size <= 1024) {
945 comp_block_size = 1024;
946 } else if (comp_size <= 2048) {
947 comp_block_size = 2048;
948 } else if (comp_size <= 4096) {
949 comp_block_size = 4096;
950 } else if (comp_size <= 8192) {
951 comp_block_size = 8192;
952 } else if (comp_size <= 16384) {
953 comp_block_size = 16384;
954 } else if (comp_size <= 32768) {
955 comp_block_size = 32768;
957 panic("hammer2: WRITE PATH: "
958 "Weird comp_size value.");
960 comp_block_size = pblksize;
964 * Must zero the remainder or dedup (which operates on a
965 * physical block basis) will not find matches.
967 if (comp_size < comp_block_size) {
968 bzero(comp_buffer + comp_size,
969 comp_block_size - comp_size);
974 * Assign physical storage, data will be set to NULL if a live-dedup
977 data = comp_size ? comp_buffer : bp->b_data;
978 chain = hammer2_assign_physical(ip, parentp, lbase, comp_block_size,
979 mtid, &data, errorp);
982 kprintf("WRITE PATH: An error occurred while "
983 "assigning physical space.\n");
984 KKASSERT(chain == NULL);
988 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
989 hammer2_inode_data_t *wipdata;
991 hammer2_chain_modify_ip(ip, chain, mtid, 0);
992 wipdata = &chain->data->ipdata;
993 KKASSERT(wipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA);
994 KKASSERT(bp->b_loffset == 0);
995 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
996 ++hammer2_iod_file_wembed;
997 } else if (data == NULL) {
999 * Live deduplication, a copy of the data is already present
1005 chain->bref.methods =
1006 HAMMER2_ENC_COMP(comp_algo) +
1007 HAMMER2_ENC_CHECK(check_algo);
1009 chain->bref.methods =
1011 HAMMER2_COMP_NONE) +
1012 HAMMER2_ENC_CHECK(check_algo);
1014 bdata = comp_size ? comp_buffer : bp->b_data;
1015 hammer2_chain_setcheck(chain, bdata);
1016 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1021 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1023 switch(chain->bref.type) {
1024 case HAMMER2_BREF_TYPE_INODE:
1025 panic("hammer2_write_bp: unexpected inode\n");
1027 case HAMMER2_BREF_TYPE_DATA:
1029 * Optimize out the read-before-write
1032 *errorp = hammer2_io_newnz(chain->hmp,
1034 chain->bref.data_off,
1038 hammer2_io_brelse(&dio);
1039 kprintf("hammer2: WRITE PATH: "
1040 "dbp bread error\n");
1043 bdata = hammer2_io_data(dio, chain->bref.data_off);
1046 * When loading the block make sure we don't
1047 * leave garbage after the compressed data.
1050 chain->bref.methods =
1051 HAMMER2_ENC_COMP(comp_algo) +
1052 HAMMER2_ENC_CHECK(check_algo);
1053 bcopy(comp_buffer, bdata, comp_size);
1055 chain->bref.methods =
1057 HAMMER2_COMP_NONE) +
1058 HAMMER2_ENC_CHECK(check_algo);
1059 bcopy(bp->b_data, bdata, pblksize);
1063 * The flush code doesn't calculate check codes for
1064 * file data (doing so can result in excessive I/O),
1067 hammer2_chain_setcheck(chain, bdata);
1068 hammer2_dedup_record(chain, bdata);
1071 * Device buffer is now valid, chain is no longer in
1072 * the initial state.
1074 * (No blockref table worries with file data)
1076 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1078 /* Now write the related bdp. */
1079 if (ioflag & IO_SYNC) {
1081 * Synchronous I/O requested.
1083 hammer2_io_bwrite(&dio);
1085 } else if ((ioflag & IO_DIRECT) &&
1086 loff + n == pblksize) {
1087 hammer2_io_bdwrite(&dio);
1089 } else if (ioflag & IO_ASYNC) {
1090 hammer2_io_bawrite(&dio);
1092 hammer2_io_bdwrite(&dio);
1096 panic("hammer2_write_bp: bad chain type %d\n",
1104 hammer2_chain_unlock(chain);
1105 hammer2_chain_drop(chain);
1108 objcache_put(cache_buffer_write, comp_buffer);
1114 * Function that performs zero-checking and writing without compression,
1115 * it corresponds to default zero-checking path.
1119 hammer2_zero_check_and_write(struct buf *bp, hammer2_inode_t *ip,
1120 hammer2_chain_t **parentp,
1121 hammer2_key_t lbase, int ioflag, int pblksize,
1122 hammer2_tid_t mtid, int *errorp,
1125 hammer2_chain_t *chain;
1126 char *data = bp->b_data;
1128 if (test_block_zeros(bp->b_data, pblksize)) {
1129 zero_write(bp, ip, parentp, lbase, mtid, errorp);
1131 chain = hammer2_assign_physical(ip, parentp, lbase, pblksize,
1132 mtid, &data, errorp);
1134 hammer2_write_bp(chain, bp, ioflag, pblksize,
1135 mtid, errorp, check_algo);
1136 } /* else dedup occurred */
1138 hammer2_chain_unlock(chain);
1139 hammer2_chain_drop(chain);
1147 * A function to test whether a block of data contains only zeros,
1148 * returns TRUE (non-zero) if the block is all zeros.
1152 test_block_zeros(const char *buf, size_t bytes)
1156 for (i = 0; i < bytes; i += sizeof(long)) {
1157 if (*(const long *)(buf + i) != 0)
1166 * Function to "write" a block that contains only zeros.
1170 zero_write(struct buf *bp, hammer2_inode_t *ip,
1171 hammer2_chain_t **parentp,
1172 hammer2_key_t lbase, hammer2_tid_t mtid, int *errorp)
1174 hammer2_chain_t *chain;
1175 hammer2_key_t key_dummy;
1176 int cache_index = -1;
1179 chain = hammer2_chain_lookup(parentp, &key_dummy,
1182 HAMMER2_LOOKUP_NODATA);
1184 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1185 hammer2_inode_data_t *wipdata;
1187 hammer2_chain_modify_ip(ip, chain, mtid, 0);
1188 wipdata = &chain->data->ipdata;
1189 KKASSERT(wipdata->meta.op_flags &
1190 HAMMER2_OPFLAG_DIRECTDATA);
1191 KKASSERT(bp->b_loffset == 0);
1192 bzero(wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1193 ++hammer2_iod_file_wembed;
1195 hammer2_chain_delete(*parentp, chain,
1196 mtid, HAMMER2_DELETE_PERMANENT);
1197 ++hammer2_iod_file_wzero;
1199 hammer2_chain_unlock(chain);
1200 hammer2_chain_drop(chain);
1202 ++hammer2_iod_file_wzero;
1209 * Function to write the data as it is, without performing any sort of
1210 * compression. This function is used in path without compression and
1211 * default zero-checking path.
1215 hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp, int ioflag,
1217 hammer2_tid_t mtid, int *errorp, int check_algo)
1219 hammer2_inode_data_t *wipdata;
1224 error = 0; /* XXX TODO below */
1226 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1228 switch(chain->bref.type) {
1229 case HAMMER2_BREF_TYPE_INODE:
1230 wipdata = &chain->data->ipdata;
1231 KKASSERT(wipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1232 KKASSERT(bp->b_loffset == 0);
1233 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1235 ++hammer2_iod_file_wembed;
1237 case HAMMER2_BREF_TYPE_DATA:
1238 error = hammer2_io_newnz(chain->hmp,
1240 chain->bref.data_off,
1241 chain->bytes, &dio);
1243 hammer2_io_bqrelse(&dio);
1244 kprintf("hammer2: WRITE PATH: "
1245 "dbp bread error\n");
1248 bdata = hammer2_io_data(dio, chain->bref.data_off);
1250 chain->bref.methods = HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
1251 HAMMER2_ENC_CHECK(check_algo);
1252 bcopy(bp->b_data, bdata, chain->bytes);
1255 * The flush code doesn't calculate check codes for
1256 * file data (doing so can result in excessive I/O),
1259 hammer2_chain_setcheck(chain, bdata);
1260 hammer2_dedup_record(chain, bdata);
1263 * Device buffer is now valid, chain is no longer in
1264 * the initial state.
1266 * (No blockref table worries with file data)
1268 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1270 if (ioflag & IO_SYNC) {
1272 * Synchronous I/O requested.
1274 hammer2_io_bwrite(&dio);
1276 } else if ((ioflag & IO_DIRECT) &&
1277 loff + n == pblksize) {
1278 hammer2_io_bdwrite(&dio);
1280 } else if (ioflag & IO_ASYNC) {
1281 hammer2_io_bawrite(&dio);
1283 hammer2_io_bdwrite(&dio);
1287 panic("hammer2_write_bp: bad chain type %d\n",
1293 KKASSERT(error == 0); /* XXX TODO */
1298 * LIVE DEDUP HEURISTIC
1300 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1301 * All fields must be loaded into locals and validated.
1303 * WARNING! Should only be used for file data, hammer2_chain_modify() only
1304 * checks for the dedup case on data chains. Also, dedup data can
1305 * only be recorded for committed chains (so NOT strategy writes
1306 * which can undergo further modification after the fact!).
1309 hammer2_dedup_record(hammer2_chain_t *chain, char *data)
1312 hammer2_dedup_t *dedup;
1318 if (hammer2_dedup_enable == 0)
1322 * Only committed data can be recorded for de-duplication, otherwise
1323 * the contents may change out from under us. So, on read if the
1324 * chain is not modified, and on flush when the chain is committed.
1327 (HAMMER2_CHAIN_MODIFIED | HAMMER2_CHAIN_INITIAL)) == 0) {
1334 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
1335 case HAMMER2_CHECK_ISCSI32:
1337 * XXX use the built-in crc (the dedup lookup sequencing
1338 * needs to be fixed so the check code is already present
1339 * when dedup_lookup is called)
1342 crc = (uint64_t)(uint32_t)chain->bref.check.iscsi32.value;
1344 crc = XXH64(data, chain->bytes, XXH_HAMMER2_SEED);
1346 case HAMMER2_CHECK_XXHASH64:
1347 crc = chain->bref.check.xxhash64.value;
1349 case HAMMER2_CHECK_SHA192:
1351 * XXX use the built-in crc (the dedup lookup sequencing
1352 * needs to be fixed so the check code is already present
1353 * when dedup_lookup is called)
1356 crc = ((uint64_t *)chain->bref.check.sha192.data)[0] ^
1357 ((uint64_t *)chain->bref.check.sha192.data)[1] ^
1358 ((uint64_t *)chain->bref.check.sha192.data)[2];
1360 crc = XXH64(data, chain->bytes, XXH_HAMMER2_SEED);
1364 * Cannot dedup without a check code
1366 * NOTE: In particular, CHECK_NONE allows a sector to be
1367 * overwritten without copy-on-write, recording
1368 * a dedup block for a CHECK_NONE object would be
1373 dedup = &hmp->heur_dedup[crc & (HAMMER2_DEDUP_HEUR_MASK & ~3)];
1374 for (i = 0; i < 4; ++i) {
1375 if (dedup[i].data_crc == crc) {
1379 dticks = (int)(dedup[i].ticks - dedup[best].ticks);
1380 if (dticks < 0 || dticks > hz * 60 * 30)
1384 if (hammer2_debug & 0x40000) {
1385 kprintf("REC %04x %016jx %016jx\n",
1386 (int)(dedup - hmp->heur_dedup),
1388 chain->bref.data_off);
1390 dedup->ticks = ticks;
1391 dedup->data_off = chain->bref.data_off;
1392 dedup->data_crc = crc;
1393 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DEDUP);
1398 hammer2_dedup_lookup(hammer2_dev_t *hmp, char **datap, int pblksize)
1400 hammer2_dedup_t *dedup;
1407 if (hammer2_dedup_enable == 0)
1414 * XXX use the built-in crc (the dedup lookup sequencing
1415 * needs to be fixed so the check code is already present
1416 * when dedup_lookup is called)
1418 crc = XXH64(data, pblksize, XXH_HAMMER2_SEED);
1419 dedup = &hmp->heur_dedup[crc & (HAMMER2_DEDUP_HEUR_MASK & ~3)];
1421 if (hammer2_debug & 0x40000) {
1422 kprintf("LOC %04x/4 %016jx\n",
1423 (int)(dedup - hmp->heur_dedup),
1427 for (i = 0; i < 4; ++i) {
1428 off = dedup[i].data_off;
1430 if (dedup[i].data_crc != crc)
1432 if ((1 << (int)(off & HAMMER2_OFF_MASK_RADIX)) != pblksize)
1434 dio = hammer2_io_getquick(hmp, off, pblksize);
1436 bcmp(data, hammer2_io_data(dio, off), pblksize) == 0) {
1438 * Make sure the INVALOK flag is cleared to prevent
1439 * the possibly-dirty bp from being invalidated now
1440 * that we are using it as part of a de-dup operation.
1442 if (hammer2_debug & 0x40000) {
1443 kprintf("DEDUP SUCCESS %016jx\n",
1446 atomic_clear_64(&dio->refs, HAMMER2_DIO_INVALOK);
1447 hammer2_io_putblk(&dio);
1449 dedup[i].ticks = ticks; /* update use */
1450 ++hammer2_iod_file_wdedup;
1452 return off; /* RETURN */
1455 hammer2_io_putblk(&dio);
1461 * Poof. Races are ok, if someone gets in and reuses a dedup offset
1462 * before or while we are clearing it they will also recover the freemap
1463 * entry (set it to fully allocated), so a bulkfree race can only set it
1464 * to a possibly-free state.
1466 * XXX ok, well, not really sure races are ok but going to run with it
1470 hammer2_dedup_clear(hammer2_dev_t *hmp)
1474 for (i = 0; i < HAMMER2_DEDUP_HEUR_SIZE; ++i) {
1475 hmp->heur_dedup[i].data_off = 0;
1476 hmp->heur_dedup[i].ticks = ticks - 1;