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 * WARNING: The strategy code cannot safely use hammer2 transactions
75 * as this can deadlock against vfs_sync's vfsync() call
76 * if multiple flushes are queued. All H2 structures must
77 * already be present and ready for the DIO.
79 * Reads can be initiated asynchronously, writes have to be
80 * spooled to a separate thread for action to avoid deadlocks.
82 static void hammer2_strategy_xop_read(hammer2_xop_t *arg, int clindex);
83 static void hammer2_strategy_xop_write(hammer2_xop_t *arg, int clindex);
84 static int hammer2_strategy_read(struct vop_strategy_args *ap);
85 static int hammer2_strategy_write(struct vop_strategy_args *ap);
86 static void hammer2_strategy_read_completion(hammer2_chain_t *chain,
87 char *data, struct bio *bio);
89 static void hammer2_dedup_record(hammer2_chain_t *chain, char *data);
90 static hammer2_off_t hammer2_dedup_lookup(hammer2_dev_t *hmp,
91 char **datap, int pblksize);
94 hammer2_vop_strategy(struct vop_strategy_args *ap)
105 error = hammer2_strategy_read(ap);
106 ++hammer2_iod_file_read;
109 error = hammer2_strategy_write(ap);
110 ++hammer2_iod_file_write;
113 bp->b_error = error = EINVAL;
114 bp->b_flags |= B_ERROR;
122 * Return the largest contiguous physical disk range for the logical
125 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
127 * Basically disabled, the logical buffer write thread has to deal with
128 * buffers one-at-a-time.
131 hammer2_vop_bmap(struct vop_bmap_args *ap)
133 *ap->a_doffsetp = NOOFFSET;
141 /****************************************************************************
143 ****************************************************************************/
145 * Callback used in read path in case that a block is compressed with LZ4.
149 hammer2_decompress_LZ4_callback(const char *data, u_int bytes, struct bio *bio)
152 char *compressed_buffer;
159 if bio->bio_caller_info2.index &&
160 bio->bio_caller_info1.uvalue32 !=
161 crc32(bp->b_data, bp->b_bufsize) --- return error
164 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
165 compressed_size = *(const int *)data;
166 KKASSERT(compressed_size <= bytes - sizeof(int));
168 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
169 result = LZ4_decompress_safe(__DECONST(char *, &data[sizeof(int)]),
174 kprintf("READ PATH: Error during decompression."
176 (intmax_t)bio->bio_offset, bytes);
177 /* make sure it isn't random garbage */
178 bzero(compressed_buffer, bp->b_bufsize);
180 KKASSERT(result <= bp->b_bufsize);
181 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
182 if (result < bp->b_bufsize)
183 bzero(bp->b_data + result, bp->b_bufsize - result);
184 objcache_put(cache_buffer_read, compressed_buffer);
186 bp->b_flags |= B_AGE;
190 * Callback used in read path in case that a block is compressed with ZLIB.
191 * It is almost identical to LZ4 callback, so in theory they can be unified,
192 * but we didn't want to make changes in bio structure for that.
196 hammer2_decompress_ZLIB_callback(const char *data, u_int bytes, struct bio *bio)
199 char *compressed_buffer;
200 z_stream strm_decompress;
206 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
207 strm_decompress.avail_in = 0;
208 strm_decompress.next_in = Z_NULL;
210 ret = inflateInit(&strm_decompress);
213 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
215 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
216 strm_decompress.next_in = __DECONST(char *, data);
218 /* XXX supply proper size, subset of device bp */
219 strm_decompress.avail_in = bytes;
220 strm_decompress.next_out = compressed_buffer;
221 strm_decompress.avail_out = bp->b_bufsize;
223 ret = inflate(&strm_decompress, Z_FINISH);
224 if (ret != Z_STREAM_END) {
225 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
226 bzero(compressed_buffer, bp->b_bufsize);
228 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
229 result = bp->b_bufsize - strm_decompress.avail_out;
230 if (result < bp->b_bufsize)
231 bzero(bp->b_data + result, strm_decompress.avail_out);
232 objcache_put(cache_buffer_read, compressed_buffer);
233 ret = inflateEnd(&strm_decompress);
236 bp->b_flags |= B_AGE;
240 * Logical buffer I/O, async read.
244 hammer2_strategy_read(struct vop_strategy_args *ap)
246 hammer2_xop_strategy_t *xop;
256 nbio = push_bio(bio);
258 lbase = bio->bio_offset;
259 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
261 xop = hammer2_xop_alloc(ip, 0);
265 hammer2_mtx_init(&xop->lock, "h2bio");
266 hammer2_xop_start(&xop->head, hammer2_strategy_xop_read);
267 /* asynchronous completion */
273 * Per-node XOP (threaded), do a synchronous lookup of the chain and
274 * its data. The frontend is asynchronous, so we are also responsible
275 * for racing to terminate the frontend.
279 hammer2_strategy_xop_read(hammer2_xop_t *arg, int clindex)
281 hammer2_xop_strategy_t *xop = &arg->xop_strategy;
282 hammer2_chain_t *parent;
283 hammer2_chain_t *chain;
284 hammer2_key_t key_dummy;
288 int cache_index = -1;
295 parent = hammer2_inode_chain(xop->head.ip1, clindex,
296 HAMMER2_RESOLVE_ALWAYS |
297 HAMMER2_RESOLVE_SHARED);
299 chain = hammer2_chain_lookup(&parent, &key_dummy,
302 HAMMER2_LOOKUP_ALWAYS |
303 HAMMER2_LOOKUP_SHARED);
304 error = chain ? chain->error : 0;
309 error = hammer2_xop_feed(&xop->head, chain, clindex, error);
311 hammer2_chain_drop(chain);
313 hammer2_chain_unlock(parent);
314 hammer2_chain_drop(parent);
316 chain = NULL; /* safety */
317 parent = NULL; /* safety */
320 * Race to finish the frontend
324 hammer2_mtx_ex(&xop->lock);
326 hammer2_mtx_unlock(&xop->lock);
331 * Async operation has not completed and we now own the lock.
332 * Determine if we can complete the operation by issuing the
333 * frontend collection non-blocking.
335 error = hammer2_xop_collect(&xop->head, HAMMER2_XOP_COLLECT_NOWAIT);
340 hammer2_mtx_unlock(&xop->lock);
341 chain = xop->head.cluster.focus;
342 hammer2_strategy_read_completion(chain, (char *)chain->data,
344 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
349 hammer2_mtx_unlock(&xop->lock);
352 bzero(bp->b_data, bp->b_bcount);
354 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
357 hammer2_mtx_unlock(&xop->lock);
361 hammer2_mtx_unlock(&xop->lock);
362 bp->b_flags |= B_ERROR;
365 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
372 hammer2_strategy_read_completion(hammer2_chain_t *chain, char *data,
375 struct buf *bp = bio->bio_buf;
377 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
379 * Data is embedded in the inode (copy from inode).
381 bcopy(((hammer2_inode_data_t *)data)->u.data,
382 bp->b_data, HAMMER2_EMBEDDED_BYTES);
383 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
384 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
387 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
389 * Data is on-media, record for live dedup.
391 hammer2_dedup_record(chain, data);
394 * Decopmression and copy.
396 switch (HAMMER2_DEC_COMP(chain->bref.methods)) {
397 case HAMMER2_COMP_LZ4:
398 hammer2_decompress_LZ4_callback(data, chain->bytes,
401 case HAMMER2_COMP_ZLIB:
402 hammer2_decompress_ZLIB_callback(data, chain->bytes,
405 case HAMMER2_COMP_NONE:
406 KKASSERT(chain->bytes <= bp->b_bcount);
407 bcopy(data, bp->b_data, chain->bytes);
408 if (chain->bytes < bp->b_bcount) {
409 bzero(bp->b_data + chain->bytes,
410 bp->b_bcount - chain->bytes);
412 bp->b_flags |= B_NOTMETA;
417 panic("hammer2_strategy_read: "
418 "unknown compression type");
421 panic("hammer2_strategy_read: unknown bref type");
425 /****************************************************************************
427 ****************************************************************************/
430 * Functions for compression in threads,
431 * from hammer2_vnops.c
433 static void hammer2_write_file_core(struct buf *bp, hammer2_inode_t *ip,
434 hammer2_chain_t **parentp,
435 hammer2_key_t lbase, int ioflag, int pblksize,
436 hammer2_tid_t mtid, int *errorp);
437 static void hammer2_compress_and_write(struct buf *bp, hammer2_inode_t *ip,
438 hammer2_chain_t **parentp,
439 hammer2_key_t lbase, int ioflag, int pblksize,
440 hammer2_tid_t mtid, int *errorp,
441 int comp_algo, int check_algo);
442 static void hammer2_zero_check_and_write(struct buf *bp, hammer2_inode_t *ip,
443 hammer2_chain_t **parentp,
444 hammer2_key_t lbase, int ioflag, int pblksize,
445 hammer2_tid_t mtid, int *errorp,
447 static int test_block_zeros(const char *buf, size_t bytes);
448 static void zero_write(struct buf *bp, hammer2_inode_t *ip,
449 hammer2_chain_t **parentp,
451 hammer2_tid_t mtid, int *errorp);
452 static void hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp,
453 int ioflag, int pblksize,
454 hammer2_tid_t mtid, int *errorp,
459 hammer2_strategy_write(struct vop_strategy_args *ap)
461 hammer2_xop_strategy_t *xop;
472 hammer2_lwinprog_ref(pmp);
473 hammer2_trans_assert_strategy(pmp);
475 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
478 xop->lbase = bio->bio_offset;
479 hammer2_xop_start(&xop->head, hammer2_strategy_xop_write);
480 /* asynchronous completion */
482 hammer2_lwinprog_wait(pmp, hammer2_flush_pipe);
488 * Per-node XOP (threaded). Write the logical buffer to the media.
492 hammer2_strategy_xop_write(hammer2_xop_t *arg, int clindex)
494 hammer2_xop_strategy_t *xop = &arg->xop_strategy;
495 hammer2_chain_t *parent;
509 /* hammer2_trans_init(parent->hmp->spmp, HAMMER2_TRANS_BUFCACHE); */
511 lblksize = hammer2_calc_logical(ip, bio->bio_offset, &lbase, NULL);
512 pblksize = hammer2_calc_physical(ip, lbase);
513 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
514 hammer2_write_file_core(bp, ip, &parent,
515 lbase, IO_ASYNC, pblksize,
516 xop->head.mtid, &error);
518 hammer2_chain_unlock(parent);
519 hammer2_chain_drop(parent);
520 parent = NULL; /* safety */
522 error = hammer2_xop_feed(&xop->head, NULL, clindex, error);
525 * Race to finish the frontend
529 hammer2_mtx_ex(&xop->lock);
531 hammer2_mtx_unlock(&xop->lock);
536 * Async operation has not completed and we now own the lock.
537 * Determine if we can complete the operation by issuing the
538 * frontend collection non-blocking.
540 error = hammer2_xop_collect(&xop->head, HAMMER2_XOP_COLLECT_NOWAIT);
546 hammer2_mtx_unlock(&xop->lock);
550 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
551 hammer2_lwinprog_drop(ip->pmp);
554 hammer2_mtx_unlock(&xop->lock);
558 hammer2_mtx_unlock(&xop->lock);
559 bp->b_flags |= B_ERROR;
562 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
563 hammer2_lwinprog_drop(ip->pmp);
569 * Wait for pending I/O to complete
572 hammer2_bioq_sync(hammer2_pfs_t *pmp)
574 hammer2_lwinprog_wait(pmp, 0);
578 * Create a new cluster at (cparent, lbase) and assign physical storage,
579 * returning a cluster suitable for I/O. The cluster will be in a modified
582 * cparent can wind up being anything.
584 * If datap is not NULL, *datap points to the real data we intend to write.
585 * If we can dedup the storage location we set *datap to NULL to indicate
586 * to the caller that a dedup occurred.
588 * NOTE: Special case for data embedded in inode.
592 hammer2_assign_physical(hammer2_inode_t *ip, hammer2_chain_t **parentp,
593 hammer2_key_t lbase, int pblksize,
594 hammer2_tid_t mtid, char **datap, int *errorp)
596 hammer2_chain_t *chain;
597 hammer2_key_t key_dummy;
598 hammer2_off_t dedup_off;
599 int pradix = hammer2_getradix(pblksize);
600 int cache_index = -1;
603 * Locate the chain associated with lbase, return a locked chain.
604 * However, do not instantiate any data reference (which utilizes a
605 * device buffer) because we will be using direct IO via the
606 * logical buffer cache buffer.
609 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
611 chain = hammer2_chain_lookup(parentp, &key_dummy,
614 HAMMER2_LOOKUP_NODATA);
617 * We found a hole, create a new chain entry.
619 * NOTE: DATA chains are created without device backing
620 * store (nor do we want any).
622 dedup_off = hammer2_dedup_lookup((*parentp)->hmp, datap,
624 *errorp = hammer2_chain_create(parentp, &chain, ip->pmp,
625 lbase, HAMMER2_PBUFRADIX,
626 HAMMER2_BREF_TYPE_DATA,
630 panic("hammer2_chain_create: par=%p error=%d\n",
634 /*ip->delta_dcount += pblksize;*/
636 switch (chain->bref.type) {
637 case HAMMER2_BREF_TYPE_INODE:
639 * The data is embedded in the inode, which requires
642 hammer2_chain_modify_ip(ip, chain, mtid, 0);
644 case HAMMER2_BREF_TYPE_DATA:
645 dedup_off = hammer2_dedup_lookup(chain->hmp, datap,
647 if (chain->bytes != pblksize) {
648 hammer2_chain_resize(ip, *parentp, chain,
651 HAMMER2_MODIFY_OPTDATA);
655 * DATA buffers must be marked modified whether the
656 * data is in a logical buffer or not. We also have
657 * to make this call to fixup the chain data pointers
658 * after resizing in case this is an encrypted or
661 hammer2_chain_modify(chain, mtid, dedup_off,
662 HAMMER2_MODIFY_OPTDATA);
665 panic("hammer2_assign_physical: bad type");
674 * hammer2_write_file_core() - hammer2_write_thread() helper
676 * The core write function which determines which path to take
677 * depending on compression settings. We also have to locate the
678 * related chains so we can calculate and set the check data for
683 hammer2_write_file_core(struct buf *bp, hammer2_inode_t *ip,
684 hammer2_chain_t **parentp,
685 hammer2_key_t lbase, int ioflag, int pblksize,
686 hammer2_tid_t mtid, int *errorp)
688 hammer2_chain_t *chain;
689 char *data = bp->b_data;
691 switch(HAMMER2_DEC_ALGO(ip->meta.comp_algo)) {
692 case HAMMER2_COMP_NONE:
694 * We have to assign physical storage to the buffer
695 * we intend to dirty or write now to avoid deadlocks
696 * in the strategy code later.
698 * This can return NOOFFSET for inode-embedded data.
699 * The strategy code will take care of it in that case.
701 chain = hammer2_assign_physical(ip, parentp, lbase, pblksize,
702 mtid, &data, errorp);
703 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
704 hammer2_inode_data_t *wipdata;
706 wipdata = &chain->data->ipdata;
707 KKASSERT(wipdata->meta.op_flags &
708 HAMMER2_OPFLAG_DIRECTDATA);
709 KKASSERT(bp->b_loffset == 0);
710 bcopy(bp->b_data, wipdata->u.data,
711 HAMMER2_EMBEDDED_BYTES);
712 ++hammer2_iod_file_wembed;
713 } else if (data == NULL) {
715 * Copy of data already present on-media.
717 chain->bref.methods =
718 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
719 HAMMER2_ENC_CHECK(ip->meta.check_algo);
720 hammer2_chain_setcheck(chain, bp->b_data);
722 hammer2_write_bp(chain, bp, ioflag, pblksize,
723 mtid, errorp, ip->meta.check_algo);
726 hammer2_chain_unlock(chain);
727 hammer2_chain_drop(chain);
730 case HAMMER2_COMP_AUTOZERO:
732 * Check for zero-fill only
734 hammer2_zero_check_and_write(bp, ip, parentp,
735 lbase, ioflag, pblksize,
737 ip->meta.check_algo);
739 case HAMMER2_COMP_LZ4:
740 case HAMMER2_COMP_ZLIB:
743 * Check for zero-fill and attempt compression.
745 hammer2_compress_and_write(bp, ip, parentp,
746 lbase, ioflag, pblksize,
749 ip->meta.check_algo);
757 * Generic function that will perform the compression in compression
758 * write path. The compression algorithm is determined by the settings
759 * obtained from inode.
763 hammer2_compress_and_write(struct buf *bp, hammer2_inode_t *ip,
764 hammer2_chain_t **parentp,
765 hammer2_key_t lbase, int ioflag, int pblksize,
766 hammer2_tid_t mtid, int *errorp, int comp_algo, int check_algo)
768 hammer2_chain_t *chain;
774 if (test_block_zeros(bp->b_data, pblksize)) {
775 zero_write(bp, ip, parentp, lbase, mtid, errorp);
782 KKASSERT(pblksize / 2 <= 32768);
784 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
785 z_stream strm_compress;
789 switch(HAMMER2_DEC_ALGO(comp_algo)) {
790 case HAMMER2_COMP_LZ4:
791 comp_buffer = objcache_get(cache_buffer_write,
793 comp_size = LZ4_compress_limitedOutput(
795 &comp_buffer[sizeof(int)],
797 pblksize / 2 - sizeof(int));
799 * We need to prefix with the size, LZ4
800 * doesn't do it for us. Add the related
803 *(int *)comp_buffer = comp_size;
805 comp_size += sizeof(int);
807 case HAMMER2_COMP_ZLIB:
808 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
810 comp_level = 6; /* default zlib compression */
811 else if (comp_level < 6)
813 else if (comp_level > 9)
815 ret = deflateInit(&strm_compress, comp_level);
817 kprintf("HAMMER2 ZLIB: fatal error "
818 "on deflateInit.\n");
821 comp_buffer = objcache_get(cache_buffer_write,
823 strm_compress.next_in = bp->b_data;
824 strm_compress.avail_in = pblksize;
825 strm_compress.next_out = comp_buffer;
826 strm_compress.avail_out = pblksize / 2;
827 ret = deflate(&strm_compress, Z_FINISH);
828 if (ret == Z_STREAM_END) {
829 comp_size = pblksize / 2 -
830 strm_compress.avail_out;
834 ret = deflateEnd(&strm_compress);
837 kprintf("Error: Unknown compression method.\n");
838 kprintf("Comp_method = %d.\n", comp_algo);
843 if (comp_size == 0) {
845 * compression failed or turned off
847 comp_block_size = pblksize; /* safety */
848 if (++ip->comp_heuristic > 128)
849 ip->comp_heuristic = 8;
852 * compression succeeded
854 ip->comp_heuristic = 0;
855 if (comp_size <= 1024) {
856 comp_block_size = 1024;
857 } else if (comp_size <= 2048) {
858 comp_block_size = 2048;
859 } else if (comp_size <= 4096) {
860 comp_block_size = 4096;
861 } else if (comp_size <= 8192) {
862 comp_block_size = 8192;
863 } else if (comp_size <= 16384) {
864 comp_block_size = 16384;
865 } else if (comp_size <= 32768) {
866 comp_block_size = 32768;
868 panic("hammer2: WRITE PATH: "
869 "Weird comp_size value.");
871 comp_block_size = pblksize;
875 * Must zero the remainder or dedup (which operates on a
876 * physical block basis) will not find matches.
878 if (comp_size < comp_block_size) {
879 bzero(comp_buffer + comp_size,
880 comp_block_size - comp_size);
885 * Assign physical storage, data will be set to NULL if a live-dedup
888 data = comp_size ? comp_buffer : bp->b_data;
889 chain = hammer2_assign_physical(ip, parentp, lbase, comp_block_size,
890 mtid, &data, errorp);
893 kprintf("WRITE PATH: An error occurred while "
894 "assigning physical space.\n");
895 KKASSERT(chain == NULL);
899 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
900 hammer2_inode_data_t *wipdata;
902 hammer2_chain_modify_ip(ip, chain, mtid, 0);
903 wipdata = &chain->data->ipdata;
904 KKASSERT(wipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA);
905 KKASSERT(bp->b_loffset == 0);
906 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
907 ++hammer2_iod_file_wembed;
908 } else if (data == NULL) {
910 * Live deduplication, a copy of the data is already present
916 chain->bref.methods =
917 HAMMER2_ENC_COMP(comp_algo) +
918 HAMMER2_ENC_CHECK(check_algo);
920 chain->bref.methods =
923 HAMMER2_ENC_CHECK(check_algo);
925 bdata = comp_size ? comp_buffer : bp->b_data;
926 hammer2_chain_setcheck(chain, bdata);
927 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
932 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
934 switch(chain->bref.type) {
935 case HAMMER2_BREF_TYPE_INODE:
936 panic("hammer2_write_bp: unexpected inode\n");
938 case HAMMER2_BREF_TYPE_DATA:
940 * Optimize out the read-before-write
943 *errorp = hammer2_io_newnz(chain->hmp,
944 chain->bref.data_off,
948 hammer2_io_brelse(&dio);
949 kprintf("hammer2: WRITE PATH: "
950 "dbp bread error\n");
953 bdata = hammer2_io_data(dio, chain->bref.data_off);
956 * When loading the block make sure we don't
957 * leave garbage after the compressed data.
960 chain->bref.methods =
961 HAMMER2_ENC_COMP(comp_algo) +
962 HAMMER2_ENC_CHECK(check_algo);
963 bcopy(comp_buffer, bdata, comp_size);
965 chain->bref.methods =
968 HAMMER2_ENC_CHECK(check_algo);
969 bcopy(bp->b_data, bdata, pblksize);
973 * The flush code doesn't calculate check codes for
974 * file data (doing so can result in excessive I/O),
977 hammer2_chain_setcheck(chain, bdata);
978 hammer2_dedup_record(chain, bdata);
981 * Device buffer is now valid, chain is no longer in
984 * (No blockref table worries with file data)
986 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
988 /* Now write the related bdp. */
989 if (ioflag & IO_SYNC) {
991 * Synchronous I/O requested.
993 hammer2_io_bwrite(&dio);
995 } else if ((ioflag & IO_DIRECT) &&
996 loff + n == pblksize) {
997 hammer2_io_bdwrite(&dio);
999 } else if (ioflag & IO_ASYNC) {
1000 hammer2_io_bawrite(&dio);
1002 hammer2_io_bdwrite(&dio);
1006 panic("hammer2_write_bp: bad chain type %d\n",
1014 hammer2_chain_unlock(chain);
1015 hammer2_chain_drop(chain);
1018 objcache_put(cache_buffer_write, comp_buffer);
1024 * Function that performs zero-checking and writing without compression,
1025 * it corresponds to default zero-checking path.
1029 hammer2_zero_check_and_write(struct buf *bp, hammer2_inode_t *ip,
1030 hammer2_chain_t **parentp,
1031 hammer2_key_t lbase, int ioflag, int pblksize,
1032 hammer2_tid_t mtid, int *errorp,
1035 hammer2_chain_t *chain;
1036 char *data = bp->b_data;
1038 if (test_block_zeros(bp->b_data, pblksize)) {
1039 zero_write(bp, ip, parentp, lbase, mtid, errorp);
1041 chain = hammer2_assign_physical(ip, parentp, lbase, pblksize,
1042 mtid, &data, errorp);
1044 hammer2_write_bp(chain, bp, ioflag, pblksize,
1045 mtid, errorp, check_algo);
1046 } /* else dedup occurred */
1048 hammer2_chain_unlock(chain);
1049 hammer2_chain_drop(chain);
1057 * A function to test whether a block of data contains only zeros,
1058 * returns TRUE (non-zero) if the block is all zeros.
1062 test_block_zeros(const char *buf, size_t bytes)
1066 for (i = 0; i < bytes; i += sizeof(long)) {
1067 if (*(const long *)(buf + i) != 0)
1076 * Function to "write" a block that contains only zeros.
1080 zero_write(struct buf *bp, hammer2_inode_t *ip,
1081 hammer2_chain_t **parentp,
1082 hammer2_key_t lbase, hammer2_tid_t mtid, int *errorp __unused)
1084 hammer2_chain_t *chain;
1085 hammer2_key_t key_dummy;
1086 int cache_index = -1;
1088 chain = hammer2_chain_lookup(parentp, &key_dummy,
1091 HAMMER2_LOOKUP_NODATA);
1093 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1094 hammer2_inode_data_t *wipdata;
1096 hammer2_chain_modify_ip(ip, chain, mtid, 0);
1097 wipdata = &chain->data->ipdata;
1098 KKASSERT(wipdata->meta.op_flags &
1099 HAMMER2_OPFLAG_DIRECTDATA);
1100 KKASSERT(bp->b_loffset == 0);
1101 bzero(wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1102 ++hammer2_iod_file_wembed;
1104 hammer2_chain_delete(*parentp, chain,
1105 mtid, HAMMER2_DELETE_PERMANENT);
1106 ++hammer2_iod_file_wzero;
1108 hammer2_chain_unlock(chain);
1109 hammer2_chain_drop(chain);
1111 ++hammer2_iod_file_wzero;
1118 * Function to write the data as it is, without performing any sort of
1119 * compression. This function is used in path without compression and
1120 * default zero-checking path.
1124 hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp, int ioflag,
1126 hammer2_tid_t mtid, int *errorp, int check_algo)
1128 hammer2_inode_data_t *wipdata;
1133 error = 0; /* XXX TODO below */
1135 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1137 switch(chain->bref.type) {
1138 case HAMMER2_BREF_TYPE_INODE:
1139 wipdata = &chain->data->ipdata;
1140 KKASSERT(wipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1141 KKASSERT(bp->b_loffset == 0);
1142 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1144 ++hammer2_iod_file_wembed;
1146 case HAMMER2_BREF_TYPE_DATA:
1147 error = hammer2_io_newnz(chain->hmp,
1148 chain->bref.data_off,
1149 chain->bytes, &dio);
1151 hammer2_io_bqrelse(&dio);
1152 kprintf("hammer2: WRITE PATH: "
1153 "dbp bread error\n");
1156 bdata = hammer2_io_data(dio, chain->bref.data_off);
1158 chain->bref.methods = HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
1159 HAMMER2_ENC_CHECK(check_algo);
1160 bcopy(bp->b_data, bdata, chain->bytes);
1163 * The flush code doesn't calculate check codes for
1164 * file data (doing so can result in excessive I/O),
1167 hammer2_chain_setcheck(chain, bdata);
1168 hammer2_dedup_record(chain, bdata);
1171 * Device buffer is now valid, chain is no longer in
1172 * the initial state.
1174 * (No blockref table worries with file data)
1176 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1178 if (ioflag & IO_SYNC) {
1180 * Synchronous I/O requested.
1182 hammer2_io_bwrite(&dio);
1184 } else if ((ioflag & IO_DIRECT) &&
1185 loff + n == pblksize) {
1186 hammer2_io_bdwrite(&dio);
1188 } else if (ioflag & IO_ASYNC) {
1189 hammer2_io_bawrite(&dio);
1191 hammer2_io_bdwrite(&dio);
1195 panic("hammer2_write_bp: bad chain type %d\n",
1201 KKASSERT(error == 0); /* XXX TODO */
1206 * LIVE DEDUP HEURISTIC
1208 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1209 * All fields must be loaded into locals and validated.
1213 hammer2_dedup_record(hammer2_chain_t *chain, char *data)
1216 hammer2_dedup_t *dedup;
1223 crc = hammer2_icrc32(data, chain->bytes);
1224 dedup = &hmp->heur_dedup[crc & (HAMMER2_DEDUP_HEUR_MASK & ~3)];
1225 for (i = 0; i < 4; ++i) {
1226 if (dedup[i].data_crc == crc) {
1230 dticks = (int)(dedup[i].ticks - dedup[best].ticks);
1231 if (dticks < 0 || dticks > hz * 60 * 30)
1235 if (hammer2_debug & 0x40000) {
1236 kprintf("REC %04x %08x %016jx\n",
1237 (int)(dedup - hmp->heur_dedup),
1239 chain->bref.data_off);
1241 dedup->ticks = ticks;
1242 dedup->data_off = chain->bref.data_off;
1243 dedup->data_crc = crc;
1244 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DEDUP);
1249 hammer2_dedup_lookup(hammer2_dev_t *hmp, char **datap, int pblksize)
1251 hammer2_dedup_t *dedup;
1262 crc = hammer2_icrc32(data, pblksize);
1263 dedup = &hmp->heur_dedup[crc & (HAMMER2_DEDUP_HEUR_MASK & ~3)];
1265 if (hammer2_debug & 0x40000) {
1266 kprintf("LOC %04x/4 %08x\n",
1267 (int)(dedup - hmp->heur_dedup),
1271 for (i = 0; i < 4; ++i) {
1272 off = dedup[i].data_off;
1274 if (dedup[i].data_crc != crc)
1276 if ((1 << (int)(off & HAMMER2_OFF_MASK_RADIX)) != pblksize)
1278 dio = hammer2_io_getquick(hmp, off, pblksize);
1280 bcmp(data, hammer2_io_data(dio, off), pblksize) == 0) {
1281 if (hammer2_debug & 0x40000) {
1282 kprintf("DEDUP SUCCESS %016jx\n",
1285 hammer2_io_putblk(&dio);
1287 dedup[i].ticks = ticks; /* update use */
1288 ++hammer2_iod_file_wdedup;
1289 return off; /* RETURN */
1292 hammer2_io_putblk(&dio);
1298 * Poof. Races are ok, if someone gets in and reuses a dedup offset
1299 * before or while we are clearing it they will also recover the freemap
1300 * entry (set it to fully allocated), so a bulkfree race can only set it
1301 * to a possibly-free state.
1303 * XXX ok, well, not really sure races are ok but going to run with it
1307 hammer2_dedup_clear(hammer2_dev_t *hmp)
1311 for (i = 0; i < HAMMER2_DEDUP_HEUR_SIZE; ++i) {
1312 hmp->heur_dedup[i].data_off = 0;
1313 hmp->heur_dedup[i].ticks = ticks - 1;