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_thread_t
*thr
,
88 static void hammer2_strategy_xop_write(hammer2_thread_t
*thr
,
90 static int hammer2_strategy_read(struct vop_strategy_args
*ap
);
91 static int hammer2_strategy_write(struct vop_strategy_args
*ap
);
92 static void hammer2_strategy_read_completion(hammer2_chain_t
*chain
,
93 char *data
, struct bio
*bio
);
95 static hammer2_off_t
hammer2_dedup_lookup(hammer2_dev_t
*hmp
,
96 char **datap
, int pblksize
);
102 #define TIMER(which) do { \
104 h2timer[h2lid] += (int)(ticks - h2last);\
110 hammer2_vop_strategy(struct vop_strategy_args
*ap
)
121 error
= hammer2_strategy_read(ap
);
122 ++hammer2_iod_file_read
;
125 error
= hammer2_strategy_write(ap
);
126 ++hammer2_iod_file_write
;
129 bp
->b_error
= error
= EINVAL
;
130 bp
->b_flags
|= B_ERROR
;
138 * Return the largest contiguous physical disk range for the logical
141 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
143 * Basically disabled, the logical buffer write thread has to deal with
144 * buffers one-at-a-time. Note that this should not prevent cluster_read()
145 * from reading-ahead, it simply prevents it from trying form a single
146 * cluster buffer for the logical request. H2 already uses 64KB buffers!
149 hammer2_vop_bmap(struct vop_bmap_args
*ap
)
151 *ap
->a_doffsetp
= NOOFFSET
;
159 /****************************************************************************
161 ****************************************************************************/
163 * Callback used in read path in case that a block is compressed with LZ4.
167 hammer2_decompress_LZ4_callback(const char *data
, u_int bytes
, struct bio
*bio
)
170 char *compressed_buffer
;
177 if bio
->bio_caller_info2
.index
&&
178 bio
->bio_caller_info1
.uvalue32
!=
179 crc32(bp
->b_data
, bp
->b_bufsize
) --- return error
182 KKASSERT(bp
->b_bufsize
<= HAMMER2_PBUFSIZE
);
183 compressed_size
= *(const int *)data
;
184 KKASSERT((uint32_t)compressed_size
<= bytes
- sizeof(int));
186 compressed_buffer
= objcache_get(cache_buffer_read
, M_INTWAIT
);
187 result
= LZ4_decompress_safe(__DECONST(char *, &data
[sizeof(int)]),
192 kprintf("READ PATH: Error during decompression."
194 (intmax_t)bio
->bio_offset
, bytes
);
195 /* make sure it isn't random garbage */
196 bzero(compressed_buffer
, bp
->b_bufsize
);
198 KKASSERT(result
<= bp
->b_bufsize
);
199 bcopy(compressed_buffer
, bp
->b_data
, bp
->b_bufsize
);
200 if (result
< bp
->b_bufsize
)
201 bzero(bp
->b_data
+ result
, bp
->b_bufsize
- result
);
202 objcache_put(cache_buffer_read
, compressed_buffer
);
204 bp
->b_flags
|= B_AGE
;
208 * Callback used in read path in case that a block is compressed with ZLIB.
209 * It is almost identical to LZ4 callback, so in theory they can be unified,
210 * but we didn't want to make changes in bio structure for that.
214 hammer2_decompress_ZLIB_callback(const char *data
, u_int bytes
, struct bio
*bio
)
217 char *compressed_buffer
;
218 z_stream strm_decompress
;
224 KKASSERT(bp
->b_bufsize
<= HAMMER2_PBUFSIZE
);
225 strm_decompress
.avail_in
= 0;
226 strm_decompress
.next_in
= Z_NULL
;
228 ret
= inflateInit(&strm_decompress
);
231 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
233 compressed_buffer
= objcache_get(cache_buffer_read
, M_INTWAIT
);
234 strm_decompress
.next_in
= __DECONST(char *, data
);
236 /* XXX supply proper size, subset of device bp */
237 strm_decompress
.avail_in
= bytes
;
238 strm_decompress
.next_out
= compressed_buffer
;
239 strm_decompress
.avail_out
= bp
->b_bufsize
;
241 ret
= inflate(&strm_decompress
, Z_FINISH
);
242 if (ret
!= Z_STREAM_END
) {
243 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
244 bzero(compressed_buffer
, bp
->b_bufsize
);
246 bcopy(compressed_buffer
, bp
->b_data
, bp
->b_bufsize
);
247 result
= bp
->b_bufsize
- strm_decompress
.avail_out
;
248 if (result
< bp
->b_bufsize
)
249 bzero(bp
->b_data
+ result
, strm_decompress
.avail_out
);
250 objcache_put(cache_buffer_read
, compressed_buffer
);
251 ret
= inflateEnd(&strm_decompress
);
254 bp
->b_flags
|= B_AGE
;
258 * Logical buffer I/O, async read.
262 hammer2_strategy_read(struct vop_strategy_args
*ap
)
264 hammer2_xop_strategy_t
*xop
;
274 nbio
= push_bio(bio
);
276 lbase
= bio
->bio_offset
;
277 KKASSERT(((int)lbase
& HAMMER2_PBUFMASK
) == 0);
279 xop
= hammer2_xop_alloc(ip
, HAMMER2_XOP_STRATEGY
);
283 hammer2_mtx_init(&xop
->lock
, "h2bior");
284 hammer2_xop_start(&xop
->head
, hammer2_strategy_xop_read
);
285 /* asynchronous completion */
291 * Per-node XOP (threaded), do a synchronous lookup of the chain and
292 * its data. The frontend is asynchronous, so we are also responsible
293 * for racing to terminate the frontend.
297 hammer2_strategy_xop_read(hammer2_thread_t
*thr
, hammer2_xop_t
*arg
)
299 hammer2_xop_strategy_t
*xop
= &arg
->xop_strategy
;
300 hammer2_chain_t
*parent
;
301 hammer2_chain_t
*chain
;
302 hammer2_key_t key_dummy
;
306 int cache_index
= -1;
310 * Note that we can race completion of the bio supplied by
311 * the front-end so we cannot access it until we determine
312 * that we are the ones finishing it up.
318 * This is difficult to optimize. The logical buffer might be
319 * partially dirty (contain dummy zero-fill pages), which would
320 * mess up our crc calculation if we were to try a direct read.
321 * So for now we always double-buffer through the underlying
324 * If not for the above problem we could conditionalize on
325 * (1) 64KB buffer, (2) one chain (not multi-master) and
326 * (3) !hammer2_double_buffer, and issue a direct read into the
329 parent
= hammer2_inode_chain(xop
->head
.ip1
, thr
->clindex
,
330 HAMMER2_RESOLVE_ALWAYS
|
331 HAMMER2_RESOLVE_SHARED
);
334 chain
= hammer2_chain_lookup(&parent
, &key_dummy
,
337 HAMMER2_LOOKUP_ALWAYS
|
338 HAMMER2_LOOKUP_SHARED
);
339 error
= chain
? chain
->error
: 0;
345 error
= hammer2_xop_feed(&xop
->head
, chain
, thr
->clindex
, error
);
348 hammer2_chain_unlock(chain
);
349 hammer2_chain_drop(chain
);
352 hammer2_chain_unlock(parent
);
353 hammer2_chain_drop(parent
);
355 chain
= NULL
; /* safety */
356 parent
= NULL
; /* safety */
360 * Race to finish the frontend. First-to-complete. bio is only
361 * valid if we are determined to be the ones able to complete
366 hammer2_mtx_ex(&xop
->lock
);
368 hammer2_mtx_unlock(&xop
->lock
);
375 * Async operation has not completed and we now own the lock.
376 * Determine if we can complete the operation by issuing the
377 * frontend collection non-blocking.
379 * H2 double-buffers the data, setting B_NOTMETA on the logical
380 * buffer hints to the OS that the logical buffer should not be
381 * swapcached (since the device buffer can be).
383 * Also note that even for compressed data we would rather the
384 * kernel cache/swapcache device buffers more and (decompressed)
385 * logical buffers less, since that will significantly improve
386 * the amount of end-user data that can be cached.
388 error
= hammer2_xop_collect(&xop
->head
, HAMMER2_XOP_COLLECT_NOWAIT
);
394 hammer2_mtx_unlock(&xop
->lock
);
395 bp
->b_flags
|= B_NOTMETA
;
396 chain
= xop
->head
.cluster
.focus
;
397 hammer2_strategy_read_completion(chain
, (char *)chain
->data
,
400 hammer2_xop_retire(&xop
->head
, HAMMER2_XOPMASK_VOP
);
404 hammer2_mtx_unlock(&xop
->lock
);
405 bp
->b_flags
|= B_NOTMETA
;
408 bzero(bp
->b_data
, bp
->b_bcount
);
410 hammer2_xop_retire(&xop
->head
, HAMMER2_XOPMASK_VOP
);
413 hammer2_mtx_unlock(&xop
->lock
);
416 kprintf("strategy_xop_read: error %d loff=%016jx\n",
417 error
, bp
->b_loffset
);
419 hammer2_mtx_unlock(&xop
->lock
);
420 bp
->b_flags
|= B_ERROR
;
423 hammer2_xop_retire(&xop
->head
, HAMMER2_XOPMASK_VOP
);
431 hammer2_strategy_read_completion(hammer2_chain_t
*chain
, char *data
,
434 struct buf
*bp
= bio
->bio_buf
;
436 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
) {
438 * Copy from in-memory inode structure.
440 bcopy(((hammer2_inode_data_t
*)data
)->u
.data
,
441 bp
->b_data
, HAMMER2_EMBEDDED_BYTES
);
442 bzero(bp
->b_data
+ HAMMER2_EMBEDDED_BYTES
,
443 bp
->b_bcount
- HAMMER2_EMBEDDED_BYTES
);
446 } else if (chain
->bref
.type
== HAMMER2_BREF_TYPE_DATA
) {
448 * Data is on-media, record for live dedup. Release the
449 * chain (try to free it) when done. The data is still
450 * cached by both the buffer cache in front and the
451 * block device behind us. This leaves more room in the
452 * LRU chain cache for meta-data chains which we really
455 * NOTE: Deduplication cannot be safely recorded for
456 * records without a check code.
458 hammer2_dedup_record(chain
, NULL
, data
);
459 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_RELEASE
);
462 * Decompression and copy.
464 switch (HAMMER2_DEC_COMP(chain
->bref
.methods
)) {
465 case HAMMER2_COMP_LZ4
:
466 hammer2_decompress_LZ4_callback(data
, chain
->bytes
,
468 /* b_resid set by call */
470 case HAMMER2_COMP_ZLIB
:
471 hammer2_decompress_ZLIB_callback(data
, chain
->bytes
,
473 /* b_resid set by call */
475 case HAMMER2_COMP_NONE
:
476 KKASSERT(chain
->bytes
<= bp
->b_bcount
);
477 bcopy(data
, bp
->b_data
, chain
->bytes
);
478 if (chain
->bytes
< bp
->b_bcount
) {
479 bzero(bp
->b_data
+ chain
->bytes
,
480 bp
->b_bcount
- chain
->bytes
);
486 panic("hammer2_strategy_read: "
487 "unknown compression type");
490 panic("hammer2_strategy_read: unknown bref type");
494 /****************************************************************************
496 ****************************************************************************/
499 * Functions for compression in threads,
500 * from hammer2_vnops.c
502 static void hammer2_write_file_core(char *data
, hammer2_inode_t
*ip
,
503 hammer2_chain_t
**parentp
,
504 hammer2_key_t lbase
, int ioflag
, int pblksize
,
505 hammer2_tid_t mtid
, int *errorp
);
506 static void hammer2_compress_and_write(char *data
, hammer2_inode_t
*ip
,
507 hammer2_chain_t
**parentp
,
508 hammer2_key_t lbase
, int ioflag
, int pblksize
,
509 hammer2_tid_t mtid
, int *errorp
,
510 int comp_algo
, int check_algo
);
511 static void hammer2_zero_check_and_write(char *data
, hammer2_inode_t
*ip
,
512 hammer2_chain_t
**parentp
,
513 hammer2_key_t lbase
, int ioflag
, int pblksize
,
514 hammer2_tid_t mtid
, int *errorp
,
516 static int test_block_zeros(const char *buf
, size_t bytes
);
517 static void zero_write(char *data
, hammer2_inode_t
*ip
,
518 hammer2_chain_t
**parentp
,
520 hammer2_tid_t mtid
, int *errorp
);
521 static void hammer2_write_bp(hammer2_chain_t
*chain
, char *data
,
522 int ioflag
, int pblksize
,
523 hammer2_tid_t mtid
, int *errorp
,
528 hammer2_strategy_write(struct vop_strategy_args
*ap
)
530 hammer2_xop_strategy_t
*xop
;
541 hammer2_lwinprog_ref(pmp
);
542 hammer2_trans_assert_strategy(pmp
);
543 hammer2_trans_init(pmp
, HAMMER2_TRANS_BUFCACHE
);
545 xop
= hammer2_xop_alloc(ip
, HAMMER2_XOP_MODIFYING
|
546 HAMMER2_XOP_STRATEGY
);
549 xop
->lbase
= bio
->bio_offset
;
550 hammer2_mtx_init(&xop
->lock
, "h2biow");
551 hammer2_xop_start(&xop
->head
, hammer2_strategy_xop_write
);
552 /* asynchronous completion */
554 hammer2_lwinprog_wait(pmp
, hammer2_flush_pipe
);
560 * Per-node XOP (threaded). Write the logical buffer to the media.
562 * This is a bit problematic because there may be multiple target and
563 * any of them may be able to release the bp. In addition, if our
564 * particulr target is offline we don't want to block the bp (and thus
565 * the frontend). To accomplish this we copy the data to the per-thr
570 hammer2_strategy_xop_write(hammer2_thread_t
*thr
, hammer2_xop_t
*arg
)
572 hammer2_xop_strategy_t
*xop
= &arg
->xop_strategy
;
573 hammer2_chain_t
*parent
;
581 hammer2_off_t bio_offset
;
585 * We can only access the bp/bio if the frontend has not yet
590 hammer2_mtx_sh(&xop
->lock
);
592 hammer2_mtx_unlock(&xop
->lock
);
597 bio
= xop
->bio
; /* ephermal */
598 bp
= bio
->bio_buf
; /* ephermal */
599 ip
= xop
->head
.ip1
; /* retained by ref */
600 bio_offset
= bio
->bio_offset
;
601 bio_data
= thr
->scratch
;
603 /* hammer2_trans_init(parent->hmp->spmp, HAMMER2_TRANS_BUFCACHE); */
605 lblksize
= hammer2_calc_logical(ip
, bio
->bio_offset
, &lbase
, NULL
);
606 pblksize
= hammer2_calc_physical(ip
, lbase
);
607 bcopy(bp
->b_data
, bio_data
, lblksize
);
609 hammer2_mtx_unlock(&xop
->lock
);
610 bp
= NULL
; /* safety, illegal to access after unlock */
611 bio
= NULL
; /* safety, illegal to access after unlock */
616 parent
= hammer2_inode_chain(ip
, thr
->clindex
, HAMMER2_RESOLVE_ALWAYS
);
617 hammer2_write_file_core(bio_data
, ip
, &parent
,
618 lbase
, IO_ASYNC
, pblksize
,
619 xop
->head
.mtid
, &error
);
621 hammer2_chain_unlock(parent
);
622 hammer2_chain_drop(parent
);
623 parent
= NULL
; /* safety */
625 hammer2_xop_feed(&xop
->head
, NULL
, thr
->clindex
, error
);
628 * Try to complete the operation on behalf of the front-end.
632 hammer2_mtx_ex(&xop
->lock
);
634 hammer2_mtx_unlock(&xop
->lock
);
639 * Async operation has not completed and we now own the lock.
640 * Determine if we can complete the operation by issuing the
641 * frontend collection non-blocking.
643 * H2 double-buffers the data, setting B_NOTMETA on the logical
644 * buffer hints to the OS that the logical buffer should not be
645 * swapcached (since the device buffer can be).
647 error
= hammer2_xop_collect(&xop
->head
, HAMMER2_XOP_COLLECT_NOWAIT
);
649 if (error
== EINPROGRESS
) {
650 hammer2_mtx_unlock(&xop
->lock
);
655 * Async operation has completed.
658 hammer2_mtx_unlock(&xop
->lock
);
660 bio
= xop
->bio
; /* now owned by us */
661 bp
= bio
->bio_buf
; /* now owned by us */
663 if (error
== ENOENT
|| error
== 0) {
664 bp
->b_flags
|= B_NOTMETA
;
669 kprintf("strategy_xop_write: error %d loff=%016jx\n",
670 error
, bp
->b_loffset
);
671 bp
->b_flags
|= B_ERROR
;
675 hammer2_xop_retire(&xop
->head
, HAMMER2_XOPMASK_VOP
);
676 hammer2_trans_assert_strategy(ip
->pmp
);
677 hammer2_lwinprog_drop(ip
->pmp
);
678 hammer2_trans_done(ip
->pmp
);
682 * Wait for pending I/O to complete
685 hammer2_bioq_sync(hammer2_pfs_t
*pmp
)
687 hammer2_lwinprog_wait(pmp
, 0);
691 * Create a new cluster at (cparent, lbase) and assign physical storage,
692 * returning a cluster suitable for I/O. The cluster will be in a modified
695 * cparent can wind up being anything.
697 * If datap is not NULL, *datap points to the real data we intend to write.
698 * If we can dedup the storage location we set *datap to NULL to indicate
699 * to the caller that a dedup occurred.
701 * NOTE: Special case for data embedded in inode.
705 hammer2_assign_physical(hammer2_inode_t
*ip
, hammer2_chain_t
**parentp
,
706 hammer2_key_t lbase
, int pblksize
,
707 hammer2_tid_t mtid
, char **datap
, int *errorp
)
709 hammer2_chain_t
*chain
;
710 hammer2_key_t key_dummy
;
711 hammer2_off_t dedup_off
;
712 int pradix
= hammer2_getradix(pblksize
);
713 int cache_index
= -1;
716 * Locate the chain associated with lbase, return a locked chain.
717 * However, do not instantiate any data reference (which utilizes a
718 * device buffer) because we will be using direct IO via the
719 * logical buffer cache buffer.
722 KKASSERT(pblksize
>= HAMMER2_ALLOC_MIN
);
725 chain
= hammer2_chain_lookup(parentp
, &key_dummy
,
728 HAMMER2_LOOKUP_NODATA
);
731 * The lookup code should not return a DELETED chain to us, unless
732 * its a short-file embedded in the inode. Then it is possible for
733 * the lookup to return a deleted inode.
735 if (chain
&& (chain
->flags
& HAMMER2_CHAIN_DELETED
) &&
736 chain
->bref
.type
!= HAMMER2_BREF_TYPE_INODE
) {
737 kprintf("assign physical deleted chain @ "
738 "%016jx (%016jx.%02x) ip %016jx\n",
739 lbase
, chain
->bref
.data_off
, chain
->bref
.type
,
746 * We found a hole, create a new chain entry.
748 * NOTE: DATA chains are created without device backing
749 * store (nor do we want any).
751 dedup_off
= hammer2_dedup_lookup((*parentp
)->hmp
, datap
,
753 *errorp
= hammer2_chain_create(parentp
, &chain
,
755 HAMMER2_ENC_CHECK(ip
->meta
.check_algo
) |
756 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE
),
757 lbase
, HAMMER2_PBUFRADIX
,
758 HAMMER2_BREF_TYPE_DATA
,
762 panic("hammer2_chain_create: par=%p error=%d\n",
766 /*ip->delta_dcount += pblksize;*/
768 switch (chain
->bref
.type
) {
769 case HAMMER2_BREF_TYPE_INODE
:
771 * The data is embedded in the inode, which requires
774 hammer2_chain_modify_ip(ip
, chain
, mtid
, 0);
776 case HAMMER2_BREF_TYPE_DATA
:
777 dedup_off
= hammer2_dedup_lookup(chain
->hmp
, datap
,
779 if (chain
->bytes
!= pblksize
) {
780 hammer2_chain_resize(chain
,
783 HAMMER2_MODIFY_OPTDATA
);
787 * DATA buffers must be marked modified whether the
788 * data is in a logical buffer or not. We also have
789 * to make this call to fixup the chain data pointers
790 * after resizing in case this is an encrypted or
793 hammer2_chain_modify(chain
, mtid
, dedup_off
,
794 HAMMER2_MODIFY_OPTDATA
);
797 panic("hammer2_assign_physical: bad type");
807 * hammer2_write_file_core() - hammer2_write_thread() helper
809 * The core write function which determines which path to take
810 * depending on compression settings. We also have to locate the
811 * related chains so we can calculate and set the check data for
816 hammer2_write_file_core(char *data
, hammer2_inode_t
*ip
,
817 hammer2_chain_t
**parentp
,
818 hammer2_key_t lbase
, int ioflag
, int pblksize
,
819 hammer2_tid_t mtid
, int *errorp
)
821 hammer2_chain_t
*chain
;
826 switch(HAMMER2_DEC_ALGO(ip
->meta
.comp_algo
)) {
827 case HAMMER2_COMP_NONE
:
829 * We have to assign physical storage to the buffer
830 * we intend to dirty or write now to avoid deadlocks
831 * in the strategy code later.
833 * This can return NOOFFSET for inode-embedded data.
834 * The strategy code will take care of it in that case.
837 chain
= hammer2_assign_physical(ip
, parentp
, lbase
, pblksize
,
838 mtid
, &bdata
, errorp
);
839 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
) {
840 hammer2_inode_data_t
*wipdata
;
842 wipdata
= &chain
->data
->ipdata
;
843 KKASSERT(wipdata
->meta
.op_flags
&
844 HAMMER2_OPFLAG_DIRECTDATA
);
845 bcopy(data
, wipdata
->u
.data
, HAMMER2_EMBEDDED_BYTES
);
846 ++hammer2_iod_file_wembed
;
847 } else if (bdata
== NULL
) {
849 * Copy of data already present on-media.
851 chain
->bref
.methods
=
852 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE
) +
853 HAMMER2_ENC_CHECK(ip
->meta
.check_algo
);
854 hammer2_chain_setcheck(chain
, data
);
856 hammer2_write_bp(chain
, data
, ioflag
, pblksize
,
857 mtid
, errorp
, ip
->meta
.check_algo
);
860 hammer2_chain_unlock(chain
);
861 hammer2_chain_drop(chain
);
864 case HAMMER2_COMP_AUTOZERO
:
866 * Check for zero-fill only
868 hammer2_zero_check_and_write(data
, ip
, parentp
,
869 lbase
, ioflag
, pblksize
,
871 ip
->meta
.check_algo
);
873 case HAMMER2_COMP_LZ4
:
874 case HAMMER2_COMP_ZLIB
:
877 * Check for zero-fill and attempt compression.
879 hammer2_compress_and_write(data
, ip
, parentp
,
880 lbase
, ioflag
, pblksize
,
883 ip
->meta
.check_algo
);
891 * Generic function that will perform the compression in compression
892 * write path. The compression algorithm is determined by the settings
893 * obtained from inode.
897 hammer2_compress_and_write(char *data
, hammer2_inode_t
*ip
,
898 hammer2_chain_t
**parentp
,
899 hammer2_key_t lbase
, int ioflag
, int pblksize
,
900 hammer2_tid_t mtid
, int *errorp
, int comp_algo
, int check_algo
)
902 hammer2_chain_t
*chain
;
909 * An all-zeros write creates a hole unless the check code
910 * is disabled. When the check code is disabled all writes
911 * are done in-place, including any all-zeros writes.
913 * NOTE: A snapshot will still force a copy-on-write
914 * (see the HAMMER2_CHECK_NONE in hammer2_chain.c).
916 if (check_algo
!= HAMMER2_CHECK_NONE
&&
917 test_block_zeros(data
, pblksize
)) {
918 zero_write(data
, ip
, parentp
, lbase
, mtid
, errorp
);
923 * Compression requested. Try to compress the block. We store
924 * the data normally if we cannot sufficiently compress it.
926 * We have a heuristic to detect files which are mostly
927 * uncompressable and avoid the compression attempt in that
928 * case. If the compression heuristic is turned off, we always
934 KKASSERT(pblksize
/ 2 <= 32768);
936 if (ip
->comp_heuristic
< 8 || (ip
->comp_heuristic
& 7) == 0 ||
937 hammer2_always_compress
) {
938 z_stream strm_compress
;
942 switch(HAMMER2_DEC_ALGO(comp_algo
)) {
943 case HAMMER2_COMP_LZ4
:
944 comp_buffer
= objcache_get(cache_buffer_write
,
946 comp_size
= LZ4_compress_limitedOutput(
948 &comp_buffer
[sizeof(int)],
950 pblksize
/ 2 - sizeof(int));
952 * We need to prefix with the size, LZ4
953 * doesn't do it for us. Add the related
956 *(int *)comp_buffer
= comp_size
;
958 comp_size
+= sizeof(int);
960 case HAMMER2_COMP_ZLIB
:
961 comp_level
= HAMMER2_DEC_LEVEL(comp_algo
);
963 comp_level
= 6; /* default zlib compression */
964 else if (comp_level
< 6)
966 else if (comp_level
> 9)
968 ret
= deflateInit(&strm_compress
, comp_level
);
970 kprintf("HAMMER2 ZLIB: fatal error "
971 "on deflateInit.\n");
974 comp_buffer
= objcache_get(cache_buffer_write
,
976 strm_compress
.next_in
= data
;
977 strm_compress
.avail_in
= pblksize
;
978 strm_compress
.next_out
= comp_buffer
;
979 strm_compress
.avail_out
= pblksize
/ 2;
980 ret
= deflate(&strm_compress
, Z_FINISH
);
981 if (ret
== Z_STREAM_END
) {
982 comp_size
= pblksize
/ 2 -
983 strm_compress
.avail_out
;
987 ret
= deflateEnd(&strm_compress
);
990 kprintf("Error: Unknown compression method.\n");
991 kprintf("Comp_method = %d.\n", comp_algo
);
996 if (comp_size
== 0) {
998 * compression failed or turned off
1000 comp_block_size
= pblksize
; /* safety */
1001 if (++ip
->comp_heuristic
> 128)
1002 ip
->comp_heuristic
= 8;
1005 * compression succeeded
1007 ip
->comp_heuristic
= 0;
1008 if (comp_size
<= 1024) {
1009 comp_block_size
= 1024;
1010 } else if (comp_size
<= 2048) {
1011 comp_block_size
= 2048;
1012 } else if (comp_size
<= 4096) {
1013 comp_block_size
= 4096;
1014 } else if (comp_size
<= 8192) {
1015 comp_block_size
= 8192;
1016 } else if (comp_size
<= 16384) {
1017 comp_block_size
= 16384;
1018 } else if (comp_size
<= 32768) {
1019 comp_block_size
= 32768;
1021 panic("hammer2: WRITE PATH: "
1022 "Weird comp_size value.");
1024 comp_block_size
= pblksize
;
1028 * Must zero the remainder or dedup (which operates on a
1029 * physical block basis) will not find matches.
1031 if (comp_size
< comp_block_size
) {
1032 bzero(comp_buffer
+ comp_size
,
1033 comp_block_size
- comp_size
);
1038 * Assign physical storage, data will be set to NULL if a live-dedup
1041 bdata
= comp_size
? comp_buffer
: data
;
1042 chain
= hammer2_assign_physical(ip
, parentp
, lbase
, comp_block_size
,
1043 mtid
, &bdata
, errorp
);
1046 kprintf("WRITE PATH: An error occurred while "
1047 "assigning physical space.\n");
1048 KKASSERT(chain
== NULL
);
1052 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
) {
1053 hammer2_inode_data_t
*wipdata
;
1055 hammer2_chain_modify_ip(ip
, chain
, mtid
, 0);
1056 wipdata
= &chain
->data
->ipdata
;
1057 KKASSERT(wipdata
->meta
.op_flags
& HAMMER2_OPFLAG_DIRECTDATA
);
1058 bcopy(data
, wipdata
->u
.data
, HAMMER2_EMBEDDED_BYTES
);
1059 ++hammer2_iod_file_wembed
;
1060 } else if (bdata
== NULL
) {
1062 * Live deduplication, a copy of the data is already present
1066 chain
->bref
.methods
=
1067 HAMMER2_ENC_COMP(comp_algo
) +
1068 HAMMER2_ENC_CHECK(check_algo
);
1070 chain
->bref
.methods
=
1072 HAMMER2_COMP_NONE
) +
1073 HAMMER2_ENC_CHECK(check_algo
);
1075 bdata
= comp_size
? comp_buffer
: data
;
1076 hammer2_chain_setcheck(chain
, bdata
);
1077 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_INITIAL
);
1081 KKASSERT(chain
->flags
& HAMMER2_CHAIN_MODIFIED
);
1083 switch(chain
->bref
.type
) {
1084 case HAMMER2_BREF_TYPE_INODE
:
1085 panic("hammer2_write_bp: unexpected inode\n");
1087 case HAMMER2_BREF_TYPE_DATA
:
1089 * Optimize out the read-before-write
1092 *errorp
= hammer2_io_newnz(chain
->hmp
,
1094 chain
->bref
.data_off
,
1098 hammer2_io_brelse(&dio
);
1099 kprintf("hammer2: WRITE PATH: "
1100 "dbp bread error\n");
1103 bdata
= hammer2_io_data(dio
, chain
->bref
.data_off
);
1106 * When loading the block make sure we don't
1107 * leave garbage after the compressed data.
1110 chain
->bref
.methods
=
1111 HAMMER2_ENC_COMP(comp_algo
) +
1112 HAMMER2_ENC_CHECK(check_algo
);
1113 bcopy(comp_buffer
, bdata
, comp_size
);
1115 chain
->bref
.methods
=
1117 HAMMER2_COMP_NONE
) +
1118 HAMMER2_ENC_CHECK(check_algo
);
1119 bcopy(data
, bdata
, pblksize
);
1123 * The flush code doesn't calculate check codes for
1124 * file data (doing so can result in excessive I/O),
1127 hammer2_chain_setcheck(chain
, bdata
);
1130 * Device buffer is now valid, chain is no longer in
1131 * the initial state.
1133 * (No blockref table worries with file data)
1135 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_INITIAL
);
1136 hammer2_dedup_record(chain
, dio
, bdata
);
1138 /* Now write the related bdp. */
1139 if (ioflag
& IO_SYNC
) {
1141 * Synchronous I/O requested.
1143 hammer2_io_bwrite(&dio
);
1145 } else if ((ioflag & IO_DIRECT) &&
1146 loff + n == pblksize) {
1147 hammer2_io_bdwrite(&dio);
1149 } else if (ioflag
& IO_ASYNC
) {
1150 hammer2_io_bawrite(&dio
);
1152 hammer2_io_bdwrite(&dio
);
1156 panic("hammer2_write_bp: bad chain type %d\n",
1164 hammer2_chain_unlock(chain
);
1165 hammer2_chain_drop(chain
);
1168 objcache_put(cache_buffer_write
, comp_buffer
);
1174 * Function that performs zero-checking and writing without compression,
1175 * it corresponds to default zero-checking path.
1179 hammer2_zero_check_and_write(char *data
, hammer2_inode_t
*ip
,
1180 hammer2_chain_t
**parentp
,
1181 hammer2_key_t lbase
, int ioflag
, int pblksize
,
1182 hammer2_tid_t mtid
, int *errorp
,
1185 hammer2_chain_t
*chain
;
1188 if (check_algo
!= HAMMER2_CHECK_NONE
&&
1189 test_block_zeros(data
, pblksize
)) {
1191 * An all-zeros write creates a hole unless the check code
1192 * is disabled. When the check code is disabled all writes
1193 * are done in-place, including any all-zeros writes.
1195 * NOTE: A snapshot will still force a copy-on-write
1196 * (see the HAMMER2_CHECK_NONE in hammer2_chain.c).
1198 zero_write(data
, ip
, parentp
, lbase
, mtid
, errorp
);
1204 chain
= hammer2_assign_physical(ip
, parentp
, lbase
, pblksize
,
1205 mtid
, &bdata
, errorp
);
1207 hammer2_write_bp(chain
, data
, ioflag
, pblksize
,
1208 mtid
, errorp
, check_algo
);
1210 /* dedup occurred */
1211 chain
->bref
.methods
=
1212 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE
) +
1213 HAMMER2_ENC_CHECK(check_algo
);
1214 hammer2_chain_setcheck(chain
, data
);
1217 hammer2_chain_unlock(chain
);
1218 hammer2_chain_drop(chain
);
1226 * A function to test whether a block of data contains only zeros,
1227 * returns TRUE (non-zero) if the block is all zeros.
1231 test_block_zeros(const char *buf
, size_t bytes
)
1235 for (i
= 0; i
< bytes
; i
+= sizeof(long)) {
1236 if (*(const long *)(buf
+ i
) != 0)
1245 * Function to "write" a block that contains only zeros.
1249 zero_write(char *data
, hammer2_inode_t
*ip
,
1250 hammer2_chain_t
**parentp
,
1251 hammer2_key_t lbase
, hammer2_tid_t mtid
, int *errorp
)
1253 hammer2_chain_t
*chain
;
1254 hammer2_key_t key_dummy
;
1255 int cache_index
= -1;
1258 chain
= hammer2_chain_lookup(parentp
, &key_dummy
,
1261 HAMMER2_LOOKUP_NODATA
);
1263 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
) {
1264 hammer2_inode_data_t
*wipdata
;
1266 hammer2_chain_modify_ip(ip
, chain
, mtid
, 0);
1267 wipdata
= &chain
->data
->ipdata
;
1268 KKASSERT(wipdata
->meta
.op_flags
&
1269 HAMMER2_OPFLAG_DIRECTDATA
);
1270 bzero(wipdata
->u
.data
, HAMMER2_EMBEDDED_BYTES
);
1271 ++hammer2_iod_file_wembed
;
1273 hammer2_chain_delete(*parentp
, chain
,
1274 mtid
, HAMMER2_DELETE_PERMANENT
);
1275 ++hammer2_iod_file_wzero
;
1277 hammer2_chain_unlock(chain
);
1278 hammer2_chain_drop(chain
);
1280 ++hammer2_iod_file_wzero
;
1287 * Function to write the data as it is, without performing any sort of
1288 * compression. This function is used in path without compression and
1289 * default zero-checking path.
1293 hammer2_write_bp(hammer2_chain_t
*chain
, char *data
, int ioflag
,
1295 hammer2_tid_t mtid
, int *errorp
, int check_algo
)
1297 hammer2_inode_data_t
*wipdata
;
1302 error
= 0; /* XXX TODO below */
1304 KKASSERT(chain
->flags
& HAMMER2_CHAIN_MODIFIED
);
1306 switch(chain
->bref
.type
) {
1307 case HAMMER2_BREF_TYPE_INODE
:
1308 wipdata
= &chain
->data
->ipdata
;
1309 KKASSERT(wipdata
->meta
.op_flags
& HAMMER2_OPFLAG_DIRECTDATA
);
1310 bcopy(data
, wipdata
->u
.data
, HAMMER2_EMBEDDED_BYTES
);
1312 ++hammer2_iod_file_wembed
;
1314 case HAMMER2_BREF_TYPE_DATA
:
1315 error
= hammer2_io_newnz(chain
->hmp
,
1317 chain
->bref
.data_off
,
1318 chain
->bytes
, &dio
);
1320 hammer2_io_bqrelse(&dio
);
1321 kprintf("hammer2: WRITE PATH: "
1322 "dbp bread error\n");
1325 bdata
= hammer2_io_data(dio
, chain
->bref
.data_off
);
1327 chain
->bref
.methods
= HAMMER2_ENC_COMP(HAMMER2_COMP_NONE
) +
1328 HAMMER2_ENC_CHECK(check_algo
);
1329 bcopy(data
, bdata
, chain
->bytes
);
1332 * The flush code doesn't calculate check codes for
1333 * file data (doing so can result in excessive I/O),
1336 hammer2_chain_setcheck(chain
, bdata
);
1339 * Device buffer is now valid, chain is no longer in
1340 * the initial state.
1342 * (No blockref table worries with file data)
1344 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_INITIAL
);
1345 hammer2_dedup_record(chain
, dio
, bdata
);
1347 if (ioflag
& IO_SYNC
) {
1349 * Synchronous I/O requested.
1351 hammer2_io_bwrite(&dio
);
1353 } else if ((ioflag & IO_DIRECT) &&
1354 loff + n == pblksize) {
1355 hammer2_io_bdwrite(&dio);
1357 } else if (ioflag
& IO_ASYNC
) {
1358 hammer2_io_bawrite(&dio
);
1360 hammer2_io_bdwrite(&dio
);
1364 panic("hammer2_write_bp: bad chain type %d\n",
1370 KKASSERT(error
== 0); /* XXX TODO */
1375 * LIVE DEDUP HEURISTICS
1377 * Record media and crc information for possible dedup operation. Note
1378 * that the dedup mask bits must also be set in the related DIO for a dedup
1379 * to be fully validated (which is handled in the freemap allocation code).
1381 * WARNING! This code is SMP safe but the heuristic allows SMP collisions.
1382 * All fields must be loaded into locals and validated.
1384 * WARNING! Should only be used for file data and directory entries,
1385 * hammer2_chain_modify() only checks for the dedup case on data
1386 * chains. Also, dedup data can only be recorded for committed
1387 * chains (so NOT strategy writes which can undergo further
1388 * modification after the fact!).
1391 hammer2_dedup_record(hammer2_chain_t
*chain
, hammer2_io_t
*dio
, char *data
)
1394 hammer2_dedup_t
*dedup
;
1402 * We can only record a dedup if we have media data to test against.
1403 * If dedup is not enabled, return early, which allows a chain to
1404 * remain marked MODIFIED (which might have benefits in special
1405 * situations, though typically it does not).
1407 if (hammer2_dedup_enable
== 0)
1417 * Only committed data can be recorded for de-duplication, otherwise
1418 * the contents may change out from under us. So, on read if the
1419 * chain is not modified, and on flush when the chain is committed.
1422 (HAMMER2_CHAIN_MODIFIED
| HAMMER2_CHAIN_INITIAL
)) == 0) {
1429 switch(HAMMER2_DEC_CHECK(chain
->bref
.methods
)) {
1430 case HAMMER2_CHECK_ISCSI32
:
1432 * XXX use the built-in crc (the dedup lookup sequencing
1433 * needs to be fixed so the check code is already present
1434 * when dedup_lookup is called)
1437 crc
= (uint64_t)(uint32_t)chain
->bref
.check
.iscsi32
.value
;
1439 crc
= XXH64(data
, chain
->bytes
, XXH_HAMMER2_SEED
);
1441 case HAMMER2_CHECK_XXHASH64
:
1442 crc
= chain
->bref
.check
.xxhash64
.value
;
1444 case HAMMER2_CHECK_SHA192
:
1446 * XXX use the built-in crc (the dedup lookup sequencing
1447 * needs to be fixed so the check code is already present
1448 * when dedup_lookup is called)
1451 crc
= ((uint64_t *)chain
->bref
.check
.sha192
.data
)[0] ^
1452 ((uint64_t *)chain
->bref
.check
.sha192
.data
)[1] ^
1453 ((uint64_t *)chain
->bref
.check
.sha192
.data
)[2];
1455 crc
= XXH64(data
, chain
->bytes
, XXH_HAMMER2_SEED
);
1459 * Cannot dedup without a check code
1461 * NOTE: In particular, CHECK_NONE allows a sector to be
1462 * overwritten without copy-on-write, recording
1463 * a dedup block for a CHECK_NONE object would be
1468 dedup
= &hmp
->heur_dedup
[crc
& (HAMMER2_DEDUP_HEUR_MASK
& ~3)];
1469 for (i
= 0; i
< 4; ++i
) {
1470 if (dedup
[i
].data_crc
== crc
) {
1474 dticks
= (int)(dedup
[i
].ticks
- dedup
[best
].ticks
);
1475 if (dticks
< 0 || dticks
> hz
* 60 * 30)
1479 if (hammer2_debug
& 0x40000) {
1480 kprintf("REC %04x %016jx %016jx\n",
1481 (int)(dedup
- hmp
->heur_dedup
),
1483 chain
->bref
.data_off
);
1485 dedup
->ticks
= ticks
;
1486 dedup
->data_off
= chain
->bref
.data_off
;
1487 dedup
->data_crc
= crc
;
1490 * Set the valid bits for the dedup only after we know the data
1491 * buffer has been updated. The alloc bits were set (and the valid
1492 * bits cleared) when the media was allocated.
1494 * This is done in two stages becuase the bulkfree code can race
1495 * the gap between allocation and data population. Both masks must
1496 * be set before a bcmp/dedup operation is able to use the block.
1498 mask
= hammer2_dedup_mask(dio
, chain
->bref
.data_off
, chain
->bytes
);
1499 atomic_set_64(&dio
->dedup_valid
, mask
);
1502 * Once we record the dedup the chain must be marked clean to
1503 * prevent reuse of the underlying block. Remember that this
1504 * write occurs when the buffer cache is flushed (i.e. on sync(),
1505 * fsync(), filesystem periodic sync, or when the kernel needs to
1506 * flush a buffer), and not whenever the user write()s.
1508 if (chain
->flags
& HAMMER2_CHAIN_MODIFIED
) {
1509 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_MODIFIED
);
1510 atomic_add_long(&hammer2_count_modified_chains
, -1);
1512 hammer2_pfs_memory_wakeup(chain
->pmp
);
1518 hammer2_dedup_lookup(hammer2_dev_t
*hmp
, char **datap
, int pblksize
)
1520 hammer2_dedup_t
*dedup
;
1529 if (hammer2_dedup_enable
== 0)
1536 * XXX use the built-in crc (the dedup lookup sequencing
1537 * needs to be fixed so the check code is already present
1538 * when dedup_lookup is called)
1540 crc
= XXH64(data
, pblksize
, XXH_HAMMER2_SEED
);
1541 dedup
= &hmp
->heur_dedup
[crc
& (HAMMER2_DEDUP_HEUR_MASK
& ~3)];
1543 if (hammer2_debug
& 0x40000) {
1544 kprintf("LOC %04x/4 %016jx\n",
1545 (int)(dedup
- hmp
->heur_dedup
),
1549 for (i
= 0; i
< 4; ++i
) {
1550 off
= dedup
[i
].data_off
;
1552 if (dedup
[i
].data_crc
!= crc
)
1554 if ((1 << (int)(off
& HAMMER2_OFF_MASK_RADIX
)) != pblksize
)
1556 dio
= hammer2_io_getquick(hmp
, off
, pblksize
, 0);
1558 dtmp
= hammer2_io_data(dio
, off
),
1559 mask
= hammer2_dedup_mask(dio
, off
, pblksize
);
1560 if ((dio
->dedup_alloc
& mask
) == mask
&&
1561 (dio
->dedup_valid
& mask
) == mask
&&
1562 bcmp(data
, dtmp
, pblksize
) == 0) {
1563 if (hammer2_debug
& 0x40000) {
1564 kprintf("DEDUP SUCCESS %016jx\n",
1567 hammer2_io_putblk(&dio
);
1569 dedup
[i
].ticks
= ticks
; /* update use */
1570 atomic_add_long(&hammer2_iod_file_wdedup
,
1573 return off
; /* RETURN */
1575 hammer2_io_putblk(&dio
);
1582 * Poof. Races are ok, if someone gets in and reuses a dedup offset
1583 * before or while we are clearing it they will also recover the freemap
1584 * entry (set it to fully allocated), so a bulkfree race can only set it
1585 * to a possibly-free state.
1587 * XXX ok, well, not really sure races are ok but going to run with it
1591 hammer2_dedup_clear(hammer2_dev_t
*hmp
)
1595 for (i
= 0; i
< HAMMER2_DEDUP_HEUR_SIZE
; ++i
) {
1596 hmp
->heur_dedup
[i
].data_off
= 0;
1597 hmp
->heur_dedup
[i
].ticks
= ticks
- 1;