usr/src/uts/common/fs/zfs/arc.c
author Joshua M. Clulow <jmc@joyent.com>
Mon, 04 Mar 2013 23:52:56 +0000
changeset 14188 afe390b9f1e0
parent 14179 15e9457c12ec
permissions -rw-r--r--
4020 Make ldi_ev_remove_callbacks safe to use in LDI callbacks Reviewed by: Robert Mustacchi <[email protected]> Approved by: Dan McDonald <[email protected]>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
     5
 * Common Development and Distribution License (the "License").
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
    22
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
13514
417c34452f03 1748 desire support for reguid in zfs
Garrett D'Amore <garrett@nexenta.com>
parents: 13061
diff changeset
    23
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
14179
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
    24
 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
    25
 * Copyright (c) 2013 by Delphix. All rights reserved.
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
    26
 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
/*
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
    30
 * DVA-based Adjustable Replacement Cache
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
 *
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
    32
 * While much of the theory of operation used here is
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
    33
 * based on the self-tuning, low overhead replacement cache
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
 * presented by Megiddo and Modha at FAST 2003, there are some
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
 * significant differences:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
 * 1. The Megiddo and Modha model assumes any page is evictable.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
 * Pages in its cache cannot be "locked" into memory.  This makes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
 * the eviction algorithm simple: evict the last page in the list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
 * This also make the performance characteristics easy to reason
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
 * about.  Our cache is not so simple.  At any given moment, some
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
 * subset of the blocks in the cache are un-evictable because we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
 * have handed out a reference to them.  Blocks are only evictable
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
 * when there are no external references active.  This makes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
 * eviction far more problematic:  we choose to evict the evictable
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
 * blocks that are the "lowest" in the list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
 * There are times when it is not possible to evict the requested
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
 * space.  In these circumstances we are unable to adjust the cache
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
 * size.  To prevent the cache growing unbounded at these times we
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
    51
 * implement a "cache throttle" that slows the flow of new data
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
    52
 * into the cache until we can make space available.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
 * 2. The Megiddo and Modha model assumes a fixed cache size.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
 * Pages are evicted when the cache is full and there is a cache
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
 * miss.  Our model has a variable sized cache.  It grows with
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
    57
 * high use, but also tries to react to memory pressure from the
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
 * operating system: decreasing its size when system memory is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
 * tight.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
 * 3. The Megiddo and Modha model assumes a fixed page size. All
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
    62
 * elements of the cache are therefore exactly the same size.  So
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
 * when adjusting the cache size following a cache miss, its simply
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
 * a matter of choosing a single page to evict.  In our model, we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
 * have variable sized cache blocks (rangeing from 512 bytes to
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
    66
 * 128K bytes).  We therefore choose a set of blocks to evict to make
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
 * space for a cache miss that approximates as closely as possible
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
 * the space used by the new block.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
 * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
 * by N. Megiddo & D. Modha, FAST 2003
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
 * The locking model:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
 * A new reference to a cache buffer can be obtained in two
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
 * ways: 1) via a hash table lookup using the DVA as a key,
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
    79
 * or 2) via one of the ARC lists.  The arc_read() interface
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
 * uses method 1, while the internal arc algorithms for
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
    81
 * adjusting the cache use method 2.  We therefore provide two
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
 * types of locks: 1) the hash table lock array, and 2) the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
 * arc list locks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 *
13721
5b51a16a186f 2618 arc.c mistypes in the comments
Bart Coddens <bart.coddens@gmail.com>
parents: 13700
diff changeset
    85
 * Buffers do not have their own mutexes, rather they rely on the
5b51a16a186f 2618 arc.c mistypes in the comments
Bart Coddens <bart.coddens@gmail.com>
parents: 13700
diff changeset
    86
 * hash table mutexes for the bulk of their protection (i.e. most
5b51a16a186f 2618 arc.c mistypes in the comments
Bart Coddens <bart.coddens@gmail.com>
parents: 13700
diff changeset
    87
 * fields in the arc_buf_hdr_t are protected by these mutexes).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
 * buf_hash_find() returns the appropriate mutex (held) when it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
 * locates the requested buffer in the hash table.  It returns
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
 * NULL for the mutex if the buffer was not in the table.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
 * buf_hash_remove() expects the appropriate hash mutex to be
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
 * already held before it is invoked.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
 * Each arc state also has a mutex which is used to protect the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
 * buffer list associated with the state.  When attempting to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
 * obtain a hash table lock while holding an arc list lock you
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
 * must use: mutex_tryenter() to avoid deadlock.  Also note that
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
   100
 * the active state mutex must be held before the ghost state mutex.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
 *
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   102
 * Arc buffers may have an associated eviction callback function.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   103
 * This function will be invoked prior to removing the buffer (e.g.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   104
 * in arc_do_user_evicts()).  Note however that the data associated
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   105
 * with the buffer may be evicted prior to the callback.  The callback
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   106
 * must be made with *no locks held* (to prevent deadlock).  Additionally,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   107
 * the users of callbacks must ensure that their private data is
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   108
 * protected from simultaneous callbacks from arc_buf_evict()
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   109
 * and arc_do_user_evicts().
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   110
 *
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
 * Note that the majority of the performance stats are manipulated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
 * with atomic operations.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   113
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   114
 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   115
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   116
 *	- L2ARC buflist creation
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   117
 *	- L2ARC buflist eviction
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   118
 *	- L2ARC write completion, which walks L2ARC buflists
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   119
 *	- ARC header destruction, as it removes from L2ARC buflists
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   120
 *	- ARC header release, as it removes from L2ARC buflists
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
#include <sys/zio.h>
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   125
#include <sys/zio_compress.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
#include <sys/arc.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
#include <sys/refcount.h>
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
   129
#include <sys/vdev.h>
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9412
diff changeset
   130
#include <sys/vdev_impl.h>
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   131
#include <sys/dsl_pool.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
#ifdef _KERNEL
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
#include <sys/vmsystm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
#include <vm/anon.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
#include <sys/fs/swapnode.h>
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
   136
#include <sys/dnlc.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
#include <sys/callb.h>
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   139
#include <sys/kstat.h>
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
   140
#include <zfs_fletcher.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   142
#ifndef _KERNEL
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   143
/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   144
boolean_t arc_watch = B_FALSE;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   145
int arc_procfd;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   146
#endif
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   147
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
static kmutex_t		arc_reclaim_thr_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
static uint8_t		arc_thread_exit;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
   152
#define	ARC_REDUCE_DNLC_PERCENT	3
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
   153
uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
   154
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
typedef enum arc_reclaim_strategy {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
} arc_reclaim_strategy_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   160
/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   161
 * The number of iterations through arc_evict_*() before we
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   162
 * drop & reacquire the lock.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   163
 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   164
int arc_evict_iterations = 100;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   165
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
/* number of seconds before growing cache again */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
static int		arc_grow_retry = 60;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   169
/* shift of arc_c for calculating both min and max arc_p */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   170
static int		arc_p_min_shift = 4;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   171
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   172
/* log2(fraction of arc to reclaim) */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   173
static int		arc_shrink_shift = 5;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   174
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
   175
/*
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2391
diff changeset
   176
 * minimum lifespan of a prefetch block in clock ticks
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2391
diff changeset
   177
 * (initialized in arc_init())
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
   178
 */
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2391
diff changeset
   179
static int		arc_min_prefetch_lifespan;
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
   180
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   181
/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   182
 * If this percent of memory is free, don't throttle.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   183
 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   184
int arc_lotsfree_percent = 10;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   185
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
static int arc_dead;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
/*
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   189
 * The arc has filled available memory and has now warmed up.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   190
 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   191
static boolean_t arc_warm;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   192
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   193
/*
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   194
 * These tunables are for performance analysis.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   195
 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   196
uint64_t zfs_arc_max;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   197
uint64_t zfs_arc_min;
4645
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
   198
uint64_t zfs_arc_meta_limit = 0;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   199
int zfs_arc_grow_retry = 0;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   200
int zfs_arc_shrink_shift = 0;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   201
int zfs_arc_p_min_shift = 0;
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   202
int zfs_disable_dup_eviction = 0;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   203
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   204
/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   205
 * Note that buffers can be in one of 6 states:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
 *	ARC_anon	- anonymous (discussed below)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   207
 *	ARC_mru		- recently used, currently cached
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   208
 *	ARC_mru_ghost	- recentely used, no longer in cache
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   209
 *	ARC_mfu		- frequently used, currently cached
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   210
 *	ARC_mfu_ghost	- frequently used, no longer in cache
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   211
 *	ARC_l2c_only	- exists in L2ARC but not other states
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   212
 * When there are no active references to the buffer, they are
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   213
 * are linked onto a list in one of these arc states.  These are
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   214
 * the only buffers that can be evicted or deleted.  Within each
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   215
 * state there are multiple lists, one for meta-data and one for
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   216
 * non-meta-data.  Meta-data (indirect blocks, blocks of dnodes,
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   217
 * etc.) is tracked separately so that it can be managed more
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   218
 * explicitly: favored over data, limited explicitly.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
 * Anonymous buffers are buffers that are not associated with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
 * a DVA.  These are buffers that hold dirty block copies
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
 * before they are written to stable storage.  By definition,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   223
 * they are "ref'd" and are considered part of arc_mru
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
 * that cannot be freed.  Generally, they will aquire a DVA
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   225
 * as they are written and migrate onto the arc_mru list.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   226
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   227
 * The ARC_l2c_only state is for buffers that are in the second
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   228
 * level ARC but no longer in any of the ARC_m* lists.  The second
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   229
 * level ARC itself may also contain buffers that are in any of
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   230
 * the ARC_m* states - meaning that a buffer can exist in two
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   231
 * places.  The reason for the ARC_l2c_only state is to keep the
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   232
 * buffer header in the hash table, so that reads that hit the
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   233
 * second level ARC benefit from these fast lookups.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
typedef struct arc_state {
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   237
	list_t	arcs_list[ARC_BUFC_NUMTYPES];	/* list of evictable buffers */
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   238
	uint64_t arcs_lsize[ARC_BUFC_NUMTYPES];	/* amount of evictable data */
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   239
	uint64_t arcs_size;	/* total amount of data in this state */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   240
	kmutex_t arcs_mtx;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
} arc_state_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   243
/* The 6 states: */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
static arc_state_t ARC_anon;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   245
static arc_state_t ARC_mru;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   246
static arc_state_t ARC_mru_ghost;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   247
static arc_state_t ARC_mfu;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   248
static arc_state_t ARC_mfu_ghost;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   249
static arc_state_t ARC_l2c_only;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   251
typedef struct arc_stats {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   252
	kstat_named_t arcstat_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   253
	kstat_named_t arcstat_misses;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   254
	kstat_named_t arcstat_demand_data_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   255
	kstat_named_t arcstat_demand_data_misses;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   256
	kstat_named_t arcstat_demand_metadata_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   257
	kstat_named_t arcstat_demand_metadata_misses;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   258
	kstat_named_t arcstat_prefetch_data_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   259
	kstat_named_t arcstat_prefetch_data_misses;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   260
	kstat_named_t arcstat_prefetch_metadata_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   261
	kstat_named_t arcstat_prefetch_metadata_misses;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   262
	kstat_named_t arcstat_mru_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   263
	kstat_named_t arcstat_mru_ghost_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   264
	kstat_named_t arcstat_mfu_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   265
	kstat_named_t arcstat_mfu_ghost_hits;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   266
	kstat_named_t arcstat_deleted;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   267
	kstat_named_t arcstat_recycle_miss;
14045
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   268
	/*
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   269
	 * Number of buffers that could not be evicted because the hash lock
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   270
	 * was held by another thread.  The lock may not necessarily be held
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   271
	 * by something using the same buffer, since hash locks are shared
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   272
	 * by multiple buffers.
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   273
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   274
	kstat_named_t arcstat_mutex_miss;
14045
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   275
	/*
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   276
	 * Number of buffers skipped because they have I/O in progress, are
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   277
	 * indrect prefetch buffers that have not lived long enough, or are
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   278
	 * not from the spa we're trying to evict from.
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
   279
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   280
	kstat_named_t arcstat_evict_skip;
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   281
	kstat_named_t arcstat_evict_l2_cached;
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   282
	kstat_named_t arcstat_evict_l2_eligible;
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   283
	kstat_named_t arcstat_evict_l2_ineligible;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   284
	kstat_named_t arcstat_hash_elements;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   285
	kstat_named_t arcstat_hash_elements_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   286
	kstat_named_t arcstat_hash_collisions;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   287
	kstat_named_t arcstat_hash_chains;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   288
	kstat_named_t arcstat_hash_chain_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   289
	kstat_named_t arcstat_p;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   290
	kstat_named_t arcstat_c;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   291
	kstat_named_t arcstat_c_min;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   292
	kstat_named_t arcstat_c_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   293
	kstat_named_t arcstat_size;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   294
	kstat_named_t arcstat_hdr_size;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   295
	kstat_named_t arcstat_data_size;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   296
	kstat_named_t arcstat_other_size;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   297
	kstat_named_t arcstat_l2_hits;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   298
	kstat_named_t arcstat_l2_misses;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   299
	kstat_named_t arcstat_l2_feeds;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   300
	kstat_named_t arcstat_l2_rw_clash;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   301
	kstat_named_t arcstat_l2_read_bytes;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   302
	kstat_named_t arcstat_l2_write_bytes;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   303
	kstat_named_t arcstat_l2_writes_sent;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   304
	kstat_named_t arcstat_l2_writes_done;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   305
	kstat_named_t arcstat_l2_writes_error;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   306
	kstat_named_t arcstat_l2_writes_hdr_miss;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   307
	kstat_named_t arcstat_l2_evict_lock_retry;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   308
	kstat_named_t arcstat_l2_evict_reading;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   309
	kstat_named_t arcstat_l2_free_on_write;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   310
	kstat_named_t arcstat_l2_abort_lowmem;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   311
	kstat_named_t arcstat_l2_cksum_bad;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   312
	kstat_named_t arcstat_l2_io_error;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   313
	kstat_named_t arcstat_l2_size;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   314
	kstat_named_t arcstat_l2_asize;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   315
	kstat_named_t arcstat_l2_hdr_size;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   316
	kstat_named_t arcstat_l2_compress_successes;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   317
	kstat_named_t arcstat_l2_compress_zeros;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   318
	kstat_named_t arcstat_l2_compress_failures;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
   319
	kstat_named_t arcstat_memory_throttle_count;
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   320
	kstat_named_t arcstat_duplicate_buffers;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   321
	kstat_named_t arcstat_duplicate_buffers_size;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   322
	kstat_named_t arcstat_duplicate_reads;
13958
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   323
	kstat_named_t arcstat_meta_used;
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   324
	kstat_named_t arcstat_meta_limit;
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   325
	kstat_named_t arcstat_meta_max;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   326
} arc_stats_t;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   327
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   328
static arc_stats_t arc_stats = {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   329
	{ "hits",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   330
	{ "misses",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   331
	{ "demand_data_hits",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   332
	{ "demand_data_misses",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   333
	{ "demand_metadata_hits",	KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   334
	{ "demand_metadata_misses",	KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   335
	{ "prefetch_data_hits",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   336
	{ "prefetch_data_misses",	KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   337
	{ "prefetch_metadata_hits",	KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   338
	{ "prefetch_metadata_misses",	KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   339
	{ "mru_hits",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   340
	{ "mru_ghost_hits",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   341
	{ "mfu_hits",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   342
	{ "mfu_ghost_hits",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   343
	{ "deleted",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   344
	{ "recycle_miss",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   345
	{ "mutex_miss",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   346
	{ "evict_skip",			KSTAT_DATA_UINT64 },
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   347
	{ "evict_l2_cached",		KSTAT_DATA_UINT64 },
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   348
	{ "evict_l2_eligible",		KSTAT_DATA_UINT64 },
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   349
	{ "evict_l2_ineligible",	KSTAT_DATA_UINT64 },
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   350
	{ "hash_elements",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   351
	{ "hash_elements_max",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   352
	{ "hash_collisions",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   353
	{ "hash_chains",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   354
	{ "hash_chain_max",		KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   355
	{ "p",				KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   356
	{ "c",				KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   357
	{ "c_min",			KSTAT_DATA_UINT64 },
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   358
	{ "c_max",			KSTAT_DATA_UINT64 },
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   359
	{ "size",			KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   360
	{ "hdr_size",			KSTAT_DATA_UINT64 },
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   361
	{ "data_size",			KSTAT_DATA_UINT64 },
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   362
	{ "other_size",			KSTAT_DATA_UINT64 },
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   363
	{ "l2_hits",			KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   364
	{ "l2_misses",			KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   365
	{ "l2_feeds",			KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   366
	{ "l2_rw_clash",		KSTAT_DATA_UINT64 },
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   367
	{ "l2_read_bytes",		KSTAT_DATA_UINT64 },
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   368
	{ "l2_write_bytes",		KSTAT_DATA_UINT64 },
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   369
	{ "l2_writes_sent",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   370
	{ "l2_writes_done",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   371
	{ "l2_writes_error",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   372
	{ "l2_writes_hdr_miss",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   373
	{ "l2_evict_lock_retry",	KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   374
	{ "l2_evict_reading",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   375
	{ "l2_free_on_write",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   376
	{ "l2_abort_lowmem",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   377
	{ "l2_cksum_bad",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   378
	{ "l2_io_error",		KSTAT_DATA_UINT64 },
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   379
	{ "l2_size",			KSTAT_DATA_UINT64 },
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   380
	{ "l2_asize",			KSTAT_DATA_UINT64 },
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
   381
	{ "l2_hdr_size",		KSTAT_DATA_UINT64 },
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   382
	{ "l2_compress_successes",	KSTAT_DATA_UINT64 },
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   383
	{ "l2_compress_zeros",		KSTAT_DATA_UINT64 },
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   384
	{ "l2_compress_failures",	KSTAT_DATA_UINT64 },
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   385
	{ "memory_throttle_count",	KSTAT_DATA_UINT64 },
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   386
	{ "duplicate_buffers",		KSTAT_DATA_UINT64 },
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
   387
	{ "duplicate_buffers_size",	KSTAT_DATA_UINT64 },
13958
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   388
	{ "duplicate_reads",		KSTAT_DATA_UINT64 },
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   389
	{ "arc_meta_used",		KSTAT_DATA_UINT64 },
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   390
	{ "arc_meta_limit",		KSTAT_DATA_UINT64 },
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   391
	{ "arc_meta_max",		KSTAT_DATA_UINT64 }
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   392
};
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   394
#define	ARCSTAT(stat)	(arc_stats.stat.value.ui64)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   395
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   396
#define	ARCSTAT_INCR(stat, val) \
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
   397
	atomic_add_64(&arc_stats.stat.value.ui64, (val))
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   398
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
   399
#define	ARCSTAT_BUMP(stat)	ARCSTAT_INCR(stat, 1)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   400
#define	ARCSTAT_BUMPDOWN(stat)	ARCSTAT_INCR(stat, -1)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   401
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   402
#define	ARCSTAT_MAX(stat, val) {					\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   403
	uint64_t m;							\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   404
	while ((val) > (m = arc_stats.stat.value.ui64) &&		\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   405
	    (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))	\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   406
		continue;						\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   407
}
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   408
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   409
#define	ARCSTAT_MAXSTAT(stat) \
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   410
	ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   412
/*
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   413
 * We define a macro to allow ARC hits/misses to be easily broken down by
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   414
 * two separate conditions, giving a total of four different subtypes for
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   415
 * each of hits and misses (so eight statistics total).
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   416
 */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   417
#define	ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   418
	if (cond1) {							\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   419
		if (cond2) {						\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   420
			ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   421
		} else {						\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   422
			ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   423
		}							\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   424
	} else {							\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   425
		if (cond2) {						\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   426
			ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   427
		} else {						\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   428
			ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   429
		}							\
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   430
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   432
kstat_t			*arc_ksp;
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
   433
static arc_state_t	*arc_anon;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   434
static arc_state_t	*arc_mru;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   435
static arc_state_t	*arc_mru_ghost;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   436
static arc_state_t	*arc_mfu;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   437
static arc_state_t	*arc_mfu_ghost;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   438
static arc_state_t	*arc_l2c_only;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   439
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   440
/*
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   441
 * There are several ARC variables that are critical to export as kstats --
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   442
 * but we don't want to have to grovel around in the kstat whenever we wish to
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   443
 * manipulate them.  For these variables, we therefore define them to be in
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   444
 * terms of the statistic variable.  This assures that we are not introducing
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   445
 * the possibility of inconsistency by having shadow copies of the variables,
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   446
 * while still allowing the code to be readable.
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   447
 */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   448
#define	arc_size	ARCSTAT(arcstat_size)	/* actual total arc size */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   449
#define	arc_p		ARCSTAT(arcstat_p)	/* target size of MRU */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   450
#define	arc_c		ARCSTAT(arcstat_c)	/* target size of cache */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   451
#define	arc_c_min	ARCSTAT(arcstat_c_min)	/* min target cache size */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   452
#define	arc_c_max	ARCSTAT(arcstat_c_max)	/* max target cache size */
13958
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   453
#define	arc_meta_limit	ARCSTAT(arcstat_meta_limit) /* max size for metadata */
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   454
#define	arc_meta_used	ARCSTAT(arcstat_meta_used) /* size of metadata */
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
   455
#define	arc_meta_max	ARCSTAT(arcstat_meta_max) /* max size of metadata */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   456
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   457
#define	L2ARC_IS_VALID_COMPRESS(_c_) \
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   458
	((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   459
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   460
static int		arc_no_grow;	/* Don't try to grow cache size */
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   461
static uint64_t		arc_tempreserve;
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
   462
static uint64_t		arc_loaned_bytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   464
typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   465
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
typedef struct arc_callback arc_callback_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
struct arc_callback {
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   469
	void			*acb_private;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
	arc_done_func_t		*acb_done;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
	arc_buf_t		*acb_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
	zio_t			*acb_zio_dummy;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
	arc_callback_t		*acb_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   476
typedef struct arc_write_callback arc_write_callback_t;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   477
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   478
struct arc_write_callback {
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   479
	void		*awcb_private;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   480
	arc_done_func_t	*awcb_ready;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
   481
	arc_done_func_t	*awcb_physdone;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   482
	arc_done_func_t	*awcb_done;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   483
	arc_buf_t	*awcb_buf;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   484
};
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
   485
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
struct arc_buf_hdr {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	/* protected by hash lock */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
	dva_t			b_dva;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	uint64_t		b_birth;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
	uint64_t		b_cksum0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   492
	kmutex_t		b_freeze_lock;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   493
	zio_cksum_t		*b_freeze_cksum;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   494
	void			*b_thawed;
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   495
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	arc_buf_hdr_t		*b_hash_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
	arc_buf_t		*b_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
	uint32_t		b_flags;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   499
	uint32_t		b_datacnt;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
   501
	arc_callback_t		*b_acb;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
	kcondvar_t		b_cv;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
   503
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
   504
	/* immutable */
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
   505
	arc_buf_contents_t	b_type;
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
   506
	uint64_t		b_size;
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
   507
	uint64_t		b_spa;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
	/* protected by arc state mutex */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
	arc_state_t		*b_state;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
	list_node_t		b_arc_node;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
	/* updated atomically */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
	clock_t			b_arc_access;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
	/* self protecting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
	refcount_t		b_refcnt;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   518
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   519
	l2arc_buf_hdr_t		*b_l2hdr;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   520
	list_node_t		b_l2node;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   523
static arc_buf_t *arc_eviction_list;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   524
static kmutex_t arc_eviction_mtx;
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
   525
static arc_buf_hdr_t arc_eviction_hdr;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
   526
static void arc_get_data_buf(arc_buf_t *buf);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
   527
static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
   528
static int arc_evict_needed(arc_buf_contents_t type);
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
   529
static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
   530
static void arc_buf_watch(arc_buf_t *buf);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   531
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   532
static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
   533
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   534
#define	GHOST_STATE(state)	\
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   535
	((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||	\
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   536
	(state) == arc_l2c_only)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   537
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
 * Private ARC flags.  These flags are private ARC only flags that will show up
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
 * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
 * be passed in as arc_flags in things like arc_read.  However, these flags
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
 * should never be passed and should only be set by ARC code.  When adding new
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
 * public flags, make sure not to smash the private ones.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   546
#define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
#define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
#define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
#define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   550
#define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
   551
#define	ARC_INDIRECT		(1 << 14)	/* this is an indirect block */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   552
#define	ARC_FREE_IN_PROGRESS	(1 << 15)	/* hdr about to be freed */
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
   553
#define	ARC_L2_WRITING		(1 << 16)	/* L2ARC write in progress */
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
   554
#define	ARC_L2_EVICTED		(1 << 17)	/* evicted during I/O */
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
   555
#define	ARC_L2_WRITE_HEAD	(1 << 18)	/* head of write list */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   557
#define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
#define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
#define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   560
#define	HDR_PREFETCH(hdr)	((hdr)->b_flags & ARC_PREFETCH)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
#define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   562
#define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   563
#define	HDR_FREE_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
   564
#define	HDR_L2CACHE(hdr)	((hdr)->b_flags & ARC_L2CACHE)
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   565
#define	HDR_L2_READING(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS &&	\
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   566
				    (hdr)->b_l2hdr != NULL)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   567
#define	HDR_L2_WRITING(hdr)	((hdr)->b_flags & ARC_L2_WRITING)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   568
#define	HDR_L2_EVICTED(hdr)	((hdr)->b_flags & ARC_L2_EVICTED)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   569
#define	HDR_L2_WRITE_HEAD(hdr)	((hdr)->b_flags & ARC_L2_WRITE_HEAD)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
/*
6018
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   572
 * Other sizes
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   573
 */
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   574
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   575
#define	HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   576
#define	L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   577
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
   578
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
 * Hash table routines
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
#define	HT_LOCK_PAD	64
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
struct ht_lock {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
	kmutex_t	ht_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
#ifdef _KERNEL
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
#define	BUF_LOCKS 256
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
typedef struct buf_hash_table {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
	uint64_t ht_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
	arc_buf_hdr_t **ht_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
	struct ht_lock ht_locks[BUF_LOCKS];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
} buf_hash_table_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
static buf_hash_table_t buf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
#define	BUF_HASH_INDEX(spa, dva, birth) \
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
#define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
#define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   604
#define	HDR_LOCK(hdr) \
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   605
	(BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
uint64_t zfs_crc64_table[256];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   609
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   610
 * Level 2 ARC
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   611
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   612
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   613
#define	L2ARC_WRITE_SIZE	(8 * 1024 * 1024)	/* initial write max */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   614
#define	L2ARC_HEADROOM		2			/* num of writes */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   615
/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   616
 * If we discover during ARC scan any buffers to be compressed, we boost
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   617
 * our headroom for the next scanning cycle by this percentage multiple.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   618
 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   619
#define	L2ARC_HEADROOM_BOOST	200
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   620
#define	L2ARC_FEED_SECS		1		/* caching interval secs */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   621
#define	L2ARC_FEED_MIN_MS	200		/* min caching interval ms */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   622
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   623
#define	l2arc_writes_sent	ARCSTAT(arcstat_l2_writes_sent)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   624
#define	l2arc_writes_done	ARCSTAT(arcstat_l2_writes_done)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   625
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
   626
/* L2ARC Performance Tunables */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   627
uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;	/* default max write size */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
   628
uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE;	/* extra write during warmup */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   629
uint64_t l2arc_headroom = L2ARC_HEADROOM;	/* number of dev writes */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   630
uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   631
uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;	/* interval seconds */
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   632
uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS;	/* min interval milliseconds */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   633
boolean_t l2arc_noprefetch = B_TRUE;		/* don't cache prefetch bufs */
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   634
boolean_t l2arc_feed_again = B_TRUE;		/* turbo warmup */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   635
boolean_t l2arc_norw = B_TRUE;			/* no reads during writes */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   636
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   637
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   638
 * L2ARC Internals
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   639
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   640
typedef struct l2arc_dev {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   641
	vdev_t			*l2ad_vdev;	/* vdev */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   642
	spa_t			*l2ad_spa;	/* spa */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   643
	uint64_t		l2ad_hand;	/* next write location */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   644
	uint64_t		l2ad_start;	/* first addr on device */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   645
	uint64_t		l2ad_end;	/* last addr on device */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   646
	uint64_t		l2ad_evict;	/* last addr eviction reached */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   647
	boolean_t		l2ad_first;	/* first sweep through */
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   648
	boolean_t		l2ad_writing;	/* currently writing */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   649
	list_t			*l2ad_buflist;	/* buffer list */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   650
	list_node_t		l2ad_node;	/* device list node */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   651
} l2arc_dev_t;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   652
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   653
static list_t L2ARC_dev_list;			/* device list */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   654
static list_t *l2arc_dev_list;			/* device list pointer */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   655
static kmutex_t l2arc_dev_mtx;			/* device list mutex */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   656
static l2arc_dev_t *l2arc_dev_last;		/* last device used */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   657
static kmutex_t l2arc_buflist_mtx;		/* mutex for all buflists */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   658
static list_t L2ARC_free_on_write;		/* free after write buf list */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   659
static list_t *l2arc_free_on_write;		/* free after write list ptr */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   660
static kmutex_t l2arc_free_on_write_mtx;	/* mutex for list */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   661
static uint64_t l2arc_ndev;			/* number of devices */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   662
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   663
typedef struct l2arc_read_callback {
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   664
	arc_buf_t		*l2rcb_buf;		/* read buffer */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   665
	spa_t			*l2rcb_spa;		/* spa */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   666
	blkptr_t		l2rcb_bp;		/* original blkptr */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   667
	zbookmark_t		l2rcb_zb;		/* original bookmark */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   668
	int			l2rcb_flags;		/* original flags */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   669
	enum zio_compress	l2rcb_compress;		/* applied compress */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   670
} l2arc_read_callback_t;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   671
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   672
typedef struct l2arc_write_callback {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   673
	l2arc_dev_t	*l2wcb_dev;		/* device info */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   674
	arc_buf_hdr_t	*l2wcb_head;		/* head of write buflist */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   675
} l2arc_write_callback_t;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   676
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   677
struct l2arc_buf_hdr {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   678
	/* protected by arc_buf_hdr  mutex */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   679
	l2arc_dev_t		*b_dev;		/* L2ARC device */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   680
	uint64_t		b_daddr;	/* disk address, offset byte */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   681
	/* compression applied to buffer data */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   682
	enum zio_compress	b_compress;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   683
	/* real alloc'd buffer size depending on b_compress applied */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   684
	int			b_asize;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   685
	/* temporary buffer holder for in-flight compressed data */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   686
	void			*b_tmp_cdata;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   687
};
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   688
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   689
typedef struct l2arc_data_free {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   690
	/* protected by l2arc_free_on_write_mtx */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   691
	void		*l2df_data;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   692
	size_t		l2df_size;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   693
	void		(*l2df_func)(void *, size_t);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   694
	list_node_t	l2df_list_node;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   695
} l2arc_data_free_t;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   696
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   697
static kmutex_t l2arc_feed_thr_lock;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   698
static kcondvar_t l2arc_feed_thr_cv;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   699
static uint8_t l2arc_thread_exit;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   700
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   701
static void l2arc_read_done(zio_t *zio);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   702
static void l2arc_hdr_stat_add(void);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   703
static void l2arc_hdr_stat_remove(void);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   704
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   705
static boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   706
static void l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   707
    enum zio_compress c);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   708
static void l2arc_release_cdata_buf(arc_buf_hdr_t *ab);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
   709
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
static uint64_t
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
   711
buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	uint8_t *vdva = (uint8_t *)dva;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
	uint64_t crc = -1ULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
	for (i = 0; i < sizeof (dva_t); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
   722
	crc ^= (spa>>8) ^ birth;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	return (crc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
#define	BUF_EMPTY(buf)						\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
	((buf)->b_dva.dva_word[0] == 0 &&			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
	(buf)->b_dva.dva_word[1] == 0 &&			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
	(buf)->b_birth == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
#define	BUF_EQUAL(spa, dva, birth, buf)				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   737
static void
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   738
buf_discard_identity(arc_buf_hdr_t *hdr)
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   739
{
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   740
	hdr->b_dva.dva_word[0] = 0;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   741
	hdr->b_dva.dva_word[1] = 0;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   742
	hdr->b_birth = 0;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   743
	hdr->b_cksum0 = 0;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   744
}
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   745
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
static arc_buf_hdr_t *
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
   747
buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
	arc_buf_hdr_t *buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
	mutex_enter(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
	    buf = buf->b_hash_next) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
		if (BUF_EQUAL(spa, dva, birth, buf)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
			*lockp = hash_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
			return (buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
	mutex_exit(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
	*lockp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
 * Insert an entry into the hash table.  If there is already an element
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
 * equal to elem in the hash table, then the already existing element
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
 * will be returned and the new element will not be inserted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
 * Otherwise returns NULL.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
static arc_buf_hdr_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	arc_buf_hdr_t *fbuf;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   778
	uint32_t i;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   780
	ASSERT(!HDR_IN_HASH_TABLE(buf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
	*lockp = hash_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
	mutex_enter(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
	    fbuf = fbuf->b_hash_next, i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
			return (fbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
	buf->b_hash_next = buf_hash_table.ht_table[idx];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
	buf_hash_table.ht_table[idx] = buf;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   791
	buf->b_flags |= ARC_IN_HASH_TABLE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
	/* collect some hash table performance data */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
	if (i > 0) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   795
		ARCSTAT_BUMP(arcstat_hash_collisions);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
		if (i == 1)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   797
			ARCSTAT_BUMP(arcstat_hash_chains);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   798
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   799
		ARCSTAT_MAX(arcstat_hash_chain_max, i);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
	}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   801
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   802
	ARCSTAT_BUMP(arcstat_hash_elements);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   803
	ARCSTAT_MAXSTAT(arcstat_hash_elements);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
buf_hash_remove(arc_buf_hdr_t *buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
	arc_buf_hdr_t *fbuf, **bufp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   815
	ASSERT(HDR_IN_HASH_TABLE(buf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	bufp = &buf_hash_table.ht_table[idx];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
	while ((fbuf = *bufp) != buf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
		ASSERT(fbuf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
		bufp = &fbuf->b_hash_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
	*bufp = buf->b_hash_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
	buf->b_hash_next = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   824
	buf->b_flags &= ~ARC_IN_HASH_TABLE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
	/* collect some hash table performance data */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   827
	ARCSTAT_BUMPDOWN(arcstat_hash_elements);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   828
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
	if (buf_hash_table.ht_table[idx] &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
   831
		ARCSTAT_BUMPDOWN(arcstat_hash_chains);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
 * Global data structures and functions for the buf kmem cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
static kmem_cache_t *hdr_cache;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
static kmem_cache_t *buf_cache;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   839
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
buf_fini(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	kmem_free(buf_hash_table.ht_table,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
	for (i = 0; i < BUF_LOCKS; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
	kmem_cache_destroy(hdr_cache);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
	kmem_cache_destroy(buf_cache);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
 * Constructor callback - called when the cache is empty
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
 * and a new buf is requested.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
hdr_cons(void *vbuf, void *unused, int kmflag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
	arc_buf_hdr_t *buf = vbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
	bzero(buf, sizeof (arc_buf_hdr_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	refcount_create(&buf->b_refcnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
4831
41ec732c6d9f 6584470 zdb needs to initialize the bpl_lock mutex
gw25295
parents: 4709
diff changeset
   866
	mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   867
	arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   868
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   872
/* ARGSUSED */
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   873
static int
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   874
buf_cons(void *vbuf, void *unused, int kmflag)
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   875
{
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   876
	arc_buf_t *buf = vbuf;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   877
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   878
	bzero(buf, sizeof (arc_buf_t));
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   879
	mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   880
	arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   881
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   882
	return (0);
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   883
}
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   884
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
 * Destructor callback - called when a cached buf is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
 * no longer required.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
hdr_dest(void *vbuf, void *unused)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   893
	arc_buf_hdr_t *buf = vbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
   895
	ASSERT(BUF_EMPTY(buf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
	refcount_destroy(&buf->b_refcnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
	cv_destroy(&buf->b_cv);
4831
41ec732c6d9f 6584470 zdb needs to initialize the bpl_lock mutex
gw25295
parents: 4709
diff changeset
   898
	mutex_destroy(&buf->b_freeze_lock);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   899
	arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   902
/* ARGSUSED */
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   903
static void
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   904
buf_dest(void *vbuf, void *unused)
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   905
{
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   906
	arc_buf_t *buf = vbuf;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   907
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
   908
	mutex_destroy(&buf->b_evict_lock);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
   909
	arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   910
}
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   911
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   912
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
 * Reclaim callback -- invoked when memory is low.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   914
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   916
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   917
hdr_recl(void *unused)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   918
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   919
	dprintf("hdr_recl called\n");
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   920
	/*
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   921
	 * umem calls the reclaim func when we destroy the buf cache,
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   922
	 * which is after we do arc_fini().
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   923
	 */
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   924
	if (!arc_dead)
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
   925
		cv_signal(&arc_reclaim_thr_cv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
buf_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
	uint64_t *ct;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   932
	uint64_t hsize = 1ULL << 12;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
	int i, j;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
	 * The hash table is big enough to fill all of physical memory
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   937
	 * with an average 64K block size.  The table will take up
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   938
	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   940
	while (hsize * 65536 < physmem * PAGESIZE)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
		hsize <<= 1;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   942
retry:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
	buf_hash_table.ht_mask = hsize - 1;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   944
	buf_hash_table.ht_table =
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   945
	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   946
	if (buf_hash_table.ht_table == NULL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   947
		ASSERT(hsize > (1ULL << 8));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   948
		hsize >>= 1;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   949
		goto retry;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
   950
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
   955
	    0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
	for (i = 0; i < 256; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
	for (i = 0; i < BUF_LOCKS; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
		    NULL, MUTEX_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   965
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   966
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
#define	ARC_MINTIME	(hz>>4) /* 62 ms */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   968
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
static void
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   970
arc_cksum_verify(arc_buf_t *buf)
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   971
{
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   972
	zio_cksum_t zc;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   973
3312
d9c746daf22f 6505625 large regression with zfs in snv_53
ahrens
parents: 3307
diff changeset
   974
	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   975
		return;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   976
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   977
	mutex_enter(&buf->b_hdr->b_freeze_lock);
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3158
diff changeset
   978
	if (buf->b_hdr->b_freeze_cksum == NULL ||
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3158
diff changeset
   979
	    (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   980
		mutex_exit(&buf->b_hdr->b_freeze_lock);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   981
		return;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   982
	}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   983
	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   984
	if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   985
		panic("buffer modified while frozen!");
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   986
	mutex_exit(&buf->b_hdr->b_freeze_lock);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   987
}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
   988
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   989
static int
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   990
arc_cksum_equal(arc_buf_t *buf)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   991
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   992
	zio_cksum_t zc;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   993
	int equal;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   994
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   995
	mutex_enter(&buf->b_hdr->b_freeze_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   996
	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   997
	equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   998
	mutex_exit(&buf->b_hdr->b_freeze_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
   999
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1000
	return (equal);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1001
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1002
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1003
static void
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1004
arc_cksum_compute(arc_buf_t *buf, boolean_t force)
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1005
{
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1006
	if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1007
		return;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1008
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1009
	mutex_enter(&buf->b_hdr->b_freeze_lock);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1010
	if (buf->b_hdr->b_freeze_cksum != NULL) {
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1011
		mutex_exit(&buf->b_hdr->b_freeze_lock);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1012
		return;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1013
	}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1014
	buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1015
	fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1016
	    buf->b_hdr->b_freeze_cksum);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1017
	mutex_exit(&buf->b_hdr->b_freeze_lock);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1018
	arc_buf_watch(buf);
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1019
}
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1020
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1021
#ifndef _KERNEL
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1022
typedef struct procctl {
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1023
	long cmd;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1024
	prwatch_t prwatch;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1025
} procctl_t;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1026
#endif
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1027
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1028
/* ARGSUSED */
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1029
static void
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1030
arc_buf_unwatch(arc_buf_t *buf)
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1031
{
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1032
#ifndef _KERNEL
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1033
	if (arc_watch) {
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1034
		int result;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1035
		procctl_t ctl;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1036
		ctl.cmd = PCWATCH;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1037
		ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1038
		ctl.prwatch.pr_size = 0;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1039
		ctl.prwatch.pr_wflags = 0;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1040
		result = write(arc_procfd, &ctl, sizeof (ctl));
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1041
		ASSERT3U(result, ==, sizeof (ctl));
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1042
	}
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1043
#endif
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1044
}
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1045
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1046
/* ARGSUSED */
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1047
static void
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1048
arc_buf_watch(arc_buf_t *buf)
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1049
{
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1050
#ifndef _KERNEL
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1051
	if (arc_watch) {
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1052
		int result;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1053
		procctl_t ctl;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1054
		ctl.cmd = PCWATCH;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1055
		ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1056
		ctl.prwatch.pr_size = buf->b_hdr->b_size;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1057
		ctl.prwatch.pr_wflags = WA_WRITE;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1058
		result = write(arc_procfd, &ctl, sizeof (ctl));
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1059
		ASSERT3U(result, ==, sizeof (ctl));
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1060
	}
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1061
#endif
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1062
}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1063
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1064
void
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1065
arc_buf_thaw(arc_buf_t *buf)
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1066
{
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1067
	if (zfs_flags & ZFS_DEBUG_MODIFY) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1068
		if (buf->b_hdr->b_state != arc_anon)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1069
			panic("modifying non-anon buffer!");
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1070
		if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1071
			panic("modifying buffer while i/o in progress!");
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1072
		arc_cksum_verify(buf);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1073
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1074
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1075
	mutex_enter(&buf->b_hdr->b_freeze_lock);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1076
	if (buf->b_hdr->b_freeze_cksum != NULL) {
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1077
		kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1078
		buf->b_hdr->b_freeze_cksum = NULL;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1079
	}
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1080
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1081
	if (zfs_flags & ZFS_DEBUG_MODIFY) {
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1082
		if (buf->b_hdr->b_thawed)
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1083
			kmem_free(buf->b_hdr->b_thawed, 1);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1084
		buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1085
	}
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1086
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1087
	mutex_exit(&buf->b_hdr->b_freeze_lock);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1088
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1089
	arc_buf_unwatch(buf);
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1090
}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1091
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1092
void
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1093
arc_buf_freeze(arc_buf_t *buf)
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1094
{
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1095
	kmutex_t *hash_lock;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1096
3312
d9c746daf22f 6505625 large regression with zfs in snv_53
ahrens
parents: 3307
diff changeset
  1097
	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
d9c746daf22f 6505625 large regression with zfs in snv_53
ahrens
parents: 3307
diff changeset
  1098
		return;
d9c746daf22f 6505625 large regression with zfs in snv_53
ahrens
parents: 3307
diff changeset
  1099
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1100
	hash_lock = HDR_LOCK(buf->b_hdr);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1101
	mutex_enter(hash_lock);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1102
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1103
	ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1104
	    buf->b_hdr->b_state == arc_anon);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1105
	arc_cksum_compute(buf, B_FALSE);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1106
	mutex_exit(hash_lock);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1107
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1108
}
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1109
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1110
static void
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1111
add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1112
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
	ASSERT(MUTEX_HELD(hash_lock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1116
	    (ab->b_state != arc_anon)) {
3700
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3552
diff changeset
  1117
		uint64_t delta = ab->b_size * ab->b_datacnt;
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1118
		list_t *list = &ab->b_state->arcs_list[ab->b_type];
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1119
		uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1120
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1121
		ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1122
		mutex_enter(&ab->b_state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
		ASSERT(list_link_active(&ab->b_arc_node));
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1124
		list_remove(list, ab);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1125
		if (GHOST_STATE(ab->b_state)) {
13805
e3a9ae14a119 3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument is zero
Madhav Suresh <madhav.suresh@delphix.com>
parents: 13790
diff changeset
  1126
			ASSERT0(ab->b_datacnt);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1127
			ASSERT3P(ab->b_buf, ==, NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1128
			delta = ab->b_size;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1129
		}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1130
		ASSERT(delta > 0);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1131
		ASSERT3U(*size, >=, delta);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1132
		atomic_add_64(size, -delta);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1133
		mutex_exit(&ab->b_state->arcs_mtx);
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  1134
		/* remove the prefetch flag if we get a reference */
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1135
		if (ab->b_flags & ARC_PREFETCH)
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1136
			ab->b_flags &= ~ARC_PREFETCH;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
	int cnt;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1144
	arc_state_t *state = ab->b_state;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1146
	ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1147
	ASSERT(!GHOST_STATE(state));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1150
	    (state != arc_anon)) {
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1151
		uint64_t *size = &state->arcs_lsize[ab->b_type];
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1152
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1153
		ASSERT(!MUTEX_HELD(&state->arcs_mtx));
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1154
		mutex_enter(&state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
		ASSERT(!list_link_active(&ab->b_arc_node));
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1156
		list_insert_head(&state->arcs_list[ab->b_type], ab);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1157
		ASSERT(ab->b_datacnt > 0);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1158
		atomic_add_64(size, ab->b_size * ab->b_datacnt);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1159
		mutex_exit(&state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
	return (cnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
 * Move the supplied buffer to the indicated state.  The mutex
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
 * for the buffer must be held by the caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1167
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
static void
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1169
arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1170
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1171
	arc_state_t *old_state = ab->b_state;
3700
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3552
diff changeset
  1172
	int64_t refcnt = refcount_count(&ab->b_refcnt);
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3552
diff changeset
  1173
	uint64_t from_delta, to_delta;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
	ASSERT(MUTEX_HELD(hash_lock));
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1176
	ASSERT3P(new_state, !=, old_state);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1177
	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1178
	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1179
	ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1180
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1181
	from_delta = to_delta = ab->b_datacnt * ab->b_size;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
	 * If this buffer is evictable, transfer it from the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
	 * old state list to the new state list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1187
	if (refcnt == 0) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1188
		if (old_state != arc_anon) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1189
			int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1190
			uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1191
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1192
			if (use_mutex)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1193
				mutex_enter(&old_state->arcs_mtx);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1194
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1195
			ASSERT(list_link_active(&ab->b_arc_node));
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1196
			list_remove(&old_state->arcs_list[ab->b_type], ab);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1198
			/*
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1199
			 * If prefetching out of the ghost cache,
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1200
			 * we will have a non-zero datacnt.
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1201
			 */
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1202
			if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1203
				/* ghost elements have a ghost size */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1204
				ASSERT(ab->b_buf == NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1205
				from_delta = ab->b_size;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
			}
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1207
			ASSERT3U(*size, >=, from_delta);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1208
			atomic_add_64(size, -from_delta);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1209
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1210
			if (use_mutex)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1211
				mutex_exit(&old_state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
		}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1213
		if (new_state != arc_anon) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1214
			int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1215
			uint64_t *size = &new_state->arcs_lsize[ab->b_type];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1217
			if (use_mutex)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1218
				mutex_enter(&new_state->arcs_mtx);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1219
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1220
			list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1221
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1222
			/* ghost elements have a ghost size */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1223
			if (GHOST_STATE(new_state)) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1224
				ASSERT(ab->b_datacnt == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1225
				ASSERT(ab->b_buf == NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1226
				to_delta = ab->b_size;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1227
			}
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1228
			atomic_add_64(size, to_delta);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1229
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1230
			if (use_mutex)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1231
				mutex_exit(&new_state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1234
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1235
	ASSERT(!BUF_EMPTY(ab));
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1236
	if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
		buf_hash_remove(ab);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1239
	/* adjust state sizes */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1240
	if (to_delta)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1241
		atomic_add_64(&new_state->arcs_size, to_delta);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1242
	if (from_delta) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1243
		ASSERT3U(old_state->arcs_size, >=, from_delta);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1244
		atomic_add_64(&old_state->arcs_size, -from_delta);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
	ab->b_state = new_state;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1247
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1248
	/* adjust l2arc hdr stats */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1249
	if (new_state == arc_l2c_only)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1250
		l2arc_hdr_stat_add();
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1251
	else if (old_state == arc_l2c_only)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1252
		l2arc_hdr_stat_remove();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1253
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1254
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1255
void
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1256
arc_space_consume(uint64_t space, arc_space_type_t type)
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1257
{
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1258
	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1259
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1260
	switch (type) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1261
	case ARC_SPACE_DATA:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1262
		ARCSTAT_INCR(arcstat_data_size, space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1263
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1264
	case ARC_SPACE_OTHER:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1265
		ARCSTAT_INCR(arcstat_other_size, space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1266
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1267
	case ARC_SPACE_HDRS:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1268
		ARCSTAT_INCR(arcstat_hdr_size, space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1269
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1270
	case ARC_SPACE_L2HDRS:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1271
		ARCSTAT_INCR(arcstat_l2_hdr_size, space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1272
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1273
	}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1274
13958
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
  1275
	ARCSTAT_INCR(arcstat_meta_used, space);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1276
	atomic_add_64(&arc_size, space);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1277
}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1278
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1279
void
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1280
arc_space_return(uint64_t space, arc_space_type_t type)
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1281
{
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1282
	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1283
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1284
	switch (type) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1285
	case ARC_SPACE_DATA:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1286
		ARCSTAT_INCR(arcstat_data_size, -space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1287
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1288
	case ARC_SPACE_OTHER:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1289
		ARCSTAT_INCR(arcstat_other_size, -space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1290
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1291
	case ARC_SPACE_HDRS:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1292
		ARCSTAT_INCR(arcstat_hdr_size, -space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1293
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1294
	case ARC_SPACE_L2HDRS:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1295
		ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1296
		break;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1297
	}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1298
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1299
	ASSERT(arc_meta_used >= space);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1300
	if (arc_meta_max < arc_meta_used)
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1301
		arc_meta_max = arc_meta_used;
13958
1fd91513472c 3561 arc_meta_limit should be exposed via kstats
George Wilson <george.wilson@delphix.com>
parents: 13952
diff changeset
  1302
	ARCSTAT_INCR(arcstat_meta_used, -space);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1303
	ASSERT(arc_size >= space);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1304
	atomic_add_64(&arc_size, -space);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1305
}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1306
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1307
void *
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1308
arc_data_buf_alloc(uint64_t size)
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1309
{
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1310
	if (arc_evict_needed(ARC_BUFC_DATA))
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1311
		cv_signal(&arc_reclaim_thr_cv);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1312
	atomic_add_64(&arc_size, size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1313
	return (zio_data_buf_alloc(size));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1314
}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1315
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1316
void
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1317
arc_data_buf_free(void *buf, uint64_t size)
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1318
{
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1319
	zio_data_buf_free(buf, size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1320
	ASSERT(arc_size >= size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1321
	atomic_add_64(&arc_size, -size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1322
}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1323
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1324
arc_buf_t *
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1325
arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
	arc_buf_hdr_t *hdr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1328
	arc_buf_t *buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
	ASSERT3U(size, >, 0);
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  1331
	hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
	ASSERT(BUF_EMPTY(hdr));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
	hdr->b_size = size;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1334
	hdr->b_type = type;
13514
417c34452f03 1748 desire support for reguid in zfs
Garrett D'Amore <garrett@nexenta.com>
parents: 13061
diff changeset
  1335
	hdr->b_spa = spa_load_guid(spa);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1336
	hdr->b_state = arc_anon;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1337
	hdr->b_arc_access = 0;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  1338
	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1339
	buf->b_hdr = hdr;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1340
	buf->b_data = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1341
	buf->b_efunc = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1342
	buf->b_private = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
	buf->b_next = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1344
	hdr->b_buf = buf;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1345
	arc_get_data_buf(buf);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1346
	hdr->b_datacnt = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1347
	hdr->b_flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1348
	ASSERT(refcount_is_zero(&hdr->b_refcnt));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1349
	(void) refcount_add(&hdr->b_refcnt, tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1351
	return (buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1353
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1354
static char *arc_onloan_tag = "onloan";
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1355
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1356
/*
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1357
 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1358
 * flight data by arc_tempreserve_space() until they are "returned". Loaned
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1359
 * buffers must be returned to the arc before they can be used by the DMU or
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1360
 * freed.
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1361
 */
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1362
arc_buf_t *
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1363
arc_loan_buf(spa_t *spa, int size)
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1364
{
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1365
	arc_buf_t *buf;
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1366
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1367
	buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1368
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1369
	atomic_add_64(&arc_loaned_bytes, size);
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1370
	return (buf);
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1371
}
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1372
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1373
/*
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1374
 * Return a loaned arc buffer to the arc.
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1375
 */
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1376
void
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1377
arc_return_buf(arc_buf_t *buf, void *tag)
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1378
{
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1379
	arc_buf_hdr_t *hdr = buf->b_hdr;
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1380
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1381
	ASSERT(buf->b_data != NULL);
11539
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1382
	(void) refcount_add(&hdr->b_refcnt, tag);
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1383
	(void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1384
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1385
	atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1386
}
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  1387
11539
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1388
/* Detach an arc_buf from a dbuf (tag) */
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1389
void
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1390
arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1391
{
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1392
	arc_buf_hdr_t *hdr;
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1393
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1394
	ASSERT(buf->b_data != NULL);
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1395
	hdr = buf->b_hdr;
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1396
	(void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1397
	(void) refcount_remove(&hdr->b_refcnt, tag);
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1398
	buf->b_efunc = NULL;
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1399
	buf->b_private = NULL;
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1400
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1401
	atomic_add_64(&arc_loaned_bytes, hdr->b_size);
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1402
}
10d35fc3d7fd 6873106 Need a mechanism to share buffers between fs modules
chunli zhang - Sun Microsystems - Irvine United States <Chunli.Zhang@Sun.COM>
parents: 11066
diff changeset
  1403
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1404
static arc_buf_t *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1405
arc_buf_clone(arc_buf_t *from)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1406
{
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1407
	arc_buf_t *buf;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1408
	arc_buf_hdr_t *hdr = from->b_hdr;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1409
	uint64_t size = hdr->b_size;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1410
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1411
	ASSERT(hdr->b_state != arc_anon);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1412
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  1413
	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1414
	buf->b_hdr = hdr;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1415
	buf->b_data = NULL;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1416
	buf->b_efunc = NULL;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1417
	buf->b_private = NULL;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1418
	buf->b_next = hdr->b_buf;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1419
	hdr->b_buf = buf;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1420
	arc_get_data_buf(buf);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1421
	bcopy(from->b_data, buf->b_data, size);
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1422
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1423
	/*
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1424
	 * This buffer already exists in the arc so create a duplicate
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1425
	 * copy for the caller.  If the buffer is associated with user data
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1426
	 * then track the size and number of duplicates.  These stats will be
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1427
	 * updated as duplicate buffers are created and destroyed.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1428
	 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1429
	if (hdr->b_type == ARC_BUFC_DATA) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1430
		ARCSTAT_BUMP(arcstat_duplicate_buffers);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1431
		ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1432
	}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1433
	hdr->b_datacnt += 1;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1434
	return (buf);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1435
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1436
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1437
void
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1438
arc_buf_add_ref(arc_buf_t *buf, void* tag)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1439
{
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  1440
	arc_buf_hdr_t *hdr;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1441
	kmutex_t *hash_lock;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1442
2724
99be9a0b86f6 6469119 race between arc_buf_clone() and arc_buf_add_ref() results in NULL pointer dereference
maybee
parents: 2688
diff changeset
  1443
	/*
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1444
	 * Check to see if this buffer is evicted.  Callers
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1445
	 * must verify b_data != NULL to know if the add_ref
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1446
	 * was successful.
2724
99be9a0b86f6 6469119 race between arc_buf_clone() and arc_buf_add_ref() results in NULL pointer dereference
maybee
parents: 2688
diff changeset
  1447
	 */
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1448
	mutex_enter(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1449
	if (buf->b_data == NULL) {
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1450
		mutex_exit(&buf->b_evict_lock);
2724
99be9a0b86f6 6469119 race between arc_buf_clone() and arc_buf_add_ref() results in NULL pointer dereference
maybee
parents: 2688
diff changeset
  1451
		return;
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  1452
	}
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1453
	hash_lock = HDR_LOCK(buf->b_hdr);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1454
	mutex_enter(hash_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1455
	hdr = buf->b_hdr;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1456
	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1457
	mutex_exit(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1458
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1459
	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1460
	add_reference(hdr, hash_lock, tag);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1461
	DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1462
	arc_access(hdr, hash_lock);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1463
	mutex_exit(hash_lock);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1464
	ARCSTAT_BUMP(arcstat_hits);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1465
	ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1466
	    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1467
	    data, metadata, hits);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1468
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1469
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1470
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1471
 * Free the arc data buffer.  If it is an l2arc write in progress,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1472
 * the buffer is placed on l2arc_free_on_write to be freed later.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1473
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1474
static void
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1475
arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1476
{
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1477
	arc_buf_hdr_t *hdr = buf->b_hdr;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1478
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1479
	if (HDR_L2_WRITING(hdr)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1480
		l2arc_data_free_t *df;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1481
		df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1482
		df->l2df_data = buf->b_data;
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1483
		df->l2df_size = hdr->b_size;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1484
		df->l2df_func = free_func;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1485
		mutex_enter(&l2arc_free_on_write_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1486
		list_insert_head(l2arc_free_on_write, df);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1487
		mutex_exit(&l2arc_free_on_write_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1488
		ARCSTAT_BUMP(arcstat_l2_free_on_write);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1489
	} else {
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1490
		free_func(buf->b_data, hdr->b_size);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1491
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1492
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1493
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
static void
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1495
arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1496
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1497
	arc_buf_t **bufp;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1498
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1499
	/* free up data associated with the buf */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1500
	if (buf->b_data) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1501
		arc_state_t *state = buf->b_hdr->b_state;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1502
		uint64_t size = buf->b_hdr->b_size;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1503
		arc_buf_contents_t type = buf->b_hdr->b_type;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1504
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1505
		arc_cksum_verify(buf);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1506
		arc_buf_unwatch(buf);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1507
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1508
		if (!recycle) {
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1509
			if (type == ARC_BUFC_METADATA) {
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1510
				arc_buf_data_free(buf, zio_buf_free);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1511
				arc_space_return(size, ARC_SPACE_DATA);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1512
			} else {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1513
				ASSERT(type == ARC_BUFC_DATA);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  1514
				arc_buf_data_free(buf, zio_data_buf_free);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  1515
				ARCSTAT_INCR(arcstat_data_size, -size);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1516
				atomic_add_64(&arc_size, -size);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1517
			}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1518
		}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1519
		if (list_link_active(&buf->b_hdr->b_arc_node)) {
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1520
			uint64_t *cnt = &state->arcs_lsize[type];
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1521
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1522
			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1523
			ASSERT(state != arc_anon);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1524
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1525
			ASSERT3U(*cnt, >=, size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1526
			atomic_add_64(cnt, -size);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1527
		}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1528
		ASSERT3U(state->arcs_size, >=, size);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1529
		atomic_add_64(&state->arcs_size, -size);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1530
		buf->b_data = NULL;
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1531
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1532
		/*
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1533
		 * If we're destroying a duplicate buffer make sure
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1534
		 * that the appropriate statistics are updated.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1535
		 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1536
		if (buf->b_hdr->b_datacnt > 1 &&
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1537
		    buf->b_hdr->b_type == ARC_BUFC_DATA) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1538
			ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1539
			ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1540
		}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1541
		ASSERT(buf->b_hdr->b_datacnt > 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1542
		buf->b_hdr->b_datacnt -= 1;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1543
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1545
	/* only remove the buf if requested */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1546
	if (!all)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1547
		return;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1548
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1549
	/* remove the buf from the hdr list */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1550
	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1551
		continue;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1552
	*bufp = buf->b_next;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1553
	buf->b_next = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1554
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1555
	ASSERT(buf->b_efunc == NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1556
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1557
	/* clean up the buf */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1558
	buf->b_hdr = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1559
	kmem_cache_free(buf_cache, buf);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1560
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1561
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1562
static void
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1563
arc_hdr_destroy(arc_buf_hdr_t *hdr)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1564
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
	ASSERT(refcount_is_zero(&hdr->b_refcnt));
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1566
	ASSERT3P(hdr->b_state, ==, arc_anon);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1567
	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1568
	l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1569
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1570
	if (l2hdr != NULL) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1571
		boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1572
		/*
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1573
		 * To prevent arc_free() and l2arc_evict() from
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1574
		 * attempting to free the same buffer at the same time,
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1575
		 * a FREE_IN_PROGRESS flag is given to arc_free() to
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1576
		 * give it priority.  l2arc_evict() can't destroy this
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1577
		 * header while we are waiting on l2arc_buflist_mtx.
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1578
		 *
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1579
		 * The hdr may be removed from l2ad_buflist before we
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1580
		 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1581
		 */
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1582
		if (!buflist_held) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1583
			mutex_enter(&l2arc_buflist_mtx);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1584
			l2hdr = hdr->b_l2hdr;
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1585
		}
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1586
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1587
		if (l2hdr != NULL) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1588
			list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1589
			ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  1590
			ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1591
			kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1592
			if (hdr->b_state == arc_l2c_only)
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1593
				l2arc_hdr_stat_remove();
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1594
			hdr->b_l2hdr = NULL;
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1595
		}
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1596
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1597
		if (!buflist_held)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1598
			mutex_exit(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1599
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1600
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1601
	if (!BUF_EMPTY(hdr)) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1602
		ASSERT(!HDR_IN_HASH_TABLE(hdr));
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1603
		buf_discard_identity(hdr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1604
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1605
	while (hdr->b_buf) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
		arc_buf_t *buf = hdr->b_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1608
		if (buf->b_efunc) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1609
			mutex_enter(&arc_eviction_mtx);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1610
			mutex_enter(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1611
			ASSERT(buf->b_hdr != NULL);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1612
			arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1613
			hdr->b_buf = buf->b_next;
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  1614
			buf->b_hdr = &arc_eviction_hdr;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1615
			buf->b_next = arc_eviction_list;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1616
			arc_eviction_list = buf;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1617
			mutex_exit(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1618
			mutex_exit(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1619
		} else {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1620
			arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1621
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
	}
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1623
	if (hdr->b_freeze_cksum != NULL) {
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1624
		kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1625
		hdr->b_freeze_cksum = NULL;
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  1626
	}
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1627
	if (hdr->b_thawed) {
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1628
		kmem_free(hdr->b_thawed, 1);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1629
		hdr->b_thawed = NULL;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1630
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1631
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1632
	ASSERT(!list_link_active(&hdr->b_arc_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1633
	ASSERT3P(hdr->b_hash_next, ==, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1634
	ASSERT3P(hdr->b_acb, ==, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
	kmem_cache_free(hdr_cache, hdr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1636
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1638
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
arc_buf_free(arc_buf_t *buf, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	arc_buf_hdr_t *hdr = buf->b_hdr;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1642
	int hashed = hdr->b_state != arc_anon;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1643
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1644
	ASSERT(buf->b_efunc == NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1645
	ASSERT(buf->b_data != NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1646
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1647
	if (hashed) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1648
		kmutex_t *hash_lock = HDR_LOCK(hdr);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1649
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1650
		mutex_enter(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1651
		hdr = buf->b_hdr;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1652
		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1653
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1654
		(void) remove_reference(hdr, hash_lock, tag);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1655
		if (hdr->b_datacnt > 1) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1656
			arc_buf_destroy(buf, FALSE, TRUE);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1657
		} else {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1658
			ASSERT(buf == hdr->b_buf);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1659
			ASSERT(buf->b_efunc == NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1660
			hdr->b_flags |= ARC_BUF_AVAILABLE;
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1661
		}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1662
		mutex_exit(hash_lock);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1663
	} else if (HDR_IO_IN_PROGRESS(hdr)) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1664
		int destroy_hdr;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1665
		/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1666
		 * We are in the middle of an async write.  Don't destroy
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1667
		 * this buffer unless the write completes before we finish
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1668
		 * decrementing the reference count.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1669
		 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1670
		mutex_enter(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1671
		(void) remove_reference(hdr, NULL, tag);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1672
		ASSERT(refcount_is_zero(&hdr->b_refcnt));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1673
		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1674
		mutex_exit(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1675
		if (destroy_hdr)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1676
			arc_hdr_destroy(hdr);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1677
	} else {
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1678
		if (remove_reference(hdr, NULL, tag) > 0)
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1679
			arc_buf_destroy(buf, FALSE, TRUE);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1680
		else
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1681
			arc_hdr_destroy(hdr);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1682
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1683
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1684
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13958
diff changeset
  1685
boolean_t
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1686
arc_buf_remove_ref(arc_buf_t *buf, void* tag)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1687
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1688
	arc_buf_hdr_t *hdr = buf->b_hdr;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
	kmutex_t *hash_lock = HDR_LOCK(hdr);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13958
diff changeset
  1690
	boolean_t no_callback = (buf->b_efunc == NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1691
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1692
	if (hdr->b_state == arc_anon) {
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1693
		ASSERT(hdr->b_datacnt == 1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1694
		arc_buf_free(buf, tag);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1695
		return (no_callback);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1696
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
	mutex_enter(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1699
	hdr = buf->b_hdr;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1700
	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1701
	ASSERT(hdr->b_state != arc_anon);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1702
	ASSERT(buf->b_data != NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1704
	(void) remove_reference(hdr, hash_lock, tag);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1705
	if (hdr->b_datacnt > 1) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1706
		if (no_callback)
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1707
			arc_buf_destroy(buf, FALSE, TRUE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1708
	} else if (no_callback) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1709
		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  1710
		ASSERT(buf->b_efunc == NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1711
		hdr->b_flags |= ARC_BUF_AVAILABLE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1712
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1713
	ASSERT(no_callback || hdr->b_datacnt > 1 ||
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1714
	    refcount_is_zero(&hdr->b_refcnt));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1715
	mutex_exit(hash_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1716
	return (no_callback);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1719
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
arc_buf_size(arc_buf_t *buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1721
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
	return (buf->b_hdr->b_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1723
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1724
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1725
/*
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1726
 * Called from the DMU to determine if the current buffer should be
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1727
 * evicted. In order to ensure proper locking, the eviction must be initiated
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1728
 * from the DMU. Return true if the buffer is associated with user data and
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1729
 * duplicate buffers still exist.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1730
 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1731
boolean_t
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1732
arc_buf_eviction_needed(arc_buf_t *buf)
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1733
{
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1734
	arc_buf_hdr_t *hdr;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1735
	boolean_t evict_needed = B_FALSE;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1736
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1737
	if (zfs_disable_dup_eviction)
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1738
		return (B_FALSE);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1739
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1740
	mutex_enter(&buf->b_evict_lock);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1741
	hdr = buf->b_hdr;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1742
	if (hdr == NULL) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1743
		/*
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1744
		 * We are in arc_do_user_evicts(); let that function
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1745
		 * perform the eviction.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1746
		 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1747
		ASSERT(buf->b_data == NULL);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1748
		mutex_exit(&buf->b_evict_lock);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1749
		return (B_FALSE);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1750
	} else if (buf->b_data == NULL) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1751
		/*
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1752
		 * We have already been added to the arc eviction list;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1753
		 * recommend eviction.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1754
		 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1755
		ASSERT3P(hdr, ==, &arc_eviction_hdr);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1756
		mutex_exit(&buf->b_evict_lock);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1757
		return (B_TRUE);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1758
	}
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1759
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1760
	if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1761
		evict_needed = B_TRUE;
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1762
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1763
	mutex_exit(&buf->b_evict_lock);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1764
	return (evict_needed);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1765
}
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1766
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  1767
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
 * Evict buffers from list until we've removed the specified number of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
 * bytes.  Move the removed buffers to the appropriate evict state.
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1770
 * If the recycle flag is set, then attempt to "recycle" a buffer:
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1771
 * - look for a buffer to evict that is `bytes' long.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1772
 * - return the data block from this buffer rather than freeing it.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1773
 * This flag is used by callers that are trying to make space for a
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1774
 * new buffer in a full arc cache.
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1775
 *
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1776
 * This function makes a "best effort".  It skips over any buffers
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1777
 * it can't get a hash_lock on, and so may not catch all candidates.
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1778
 * It may also return without evicting as much space as requested.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1779
 */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1780
static void *
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  1781
arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1782
    arc_buf_contents_t type)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
	arc_state_t *evicted_state;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1785
	uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1786
	arc_buf_hdr_t *ab, *ab_prev = NULL;
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1787
	list_t *list = &state->arcs_list[type];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
	kmutex_t *hash_lock;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1789
	boolean_t have_lock;
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1790
	void *stolen = NULL;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1791
	arc_buf_hdr_t marker = { 0 };
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1792
	int count = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1794
	ASSERT(state == arc_mru || state == arc_mfu);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1796
	evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1797
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1798
	mutex_enter(&state->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1799
	mutex_enter(&evicted_state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1801
	for (ab = list_tail(list); ab; ab = ab_prev) {
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1802
		ab_prev = list_prev(list, ab);
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1803
		/* prefetch buffers have a minimum lifespan */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1804
		if (HDR_IO_IN_PROGRESS(ab) ||
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1805
		    (spa && ab->b_spa != spa) ||
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1806
		    (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  1807
		    ddi_get_lbolt() - ab->b_arc_access <
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  1808
		    arc_min_prefetch_lifespan)) {
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1809
			skipped++;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1810
			continue;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1811
		}
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1812
		/* "lookahead" for better eviction candidate */
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1813
		if (recycle && ab->b_size != bytes &&
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1814
		    ab_prev && ab_prev->b_size == bytes)
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1815
			continue;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1816
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1817
		/* ignore markers */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1818
		if (ab->b_spa == 0)
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1819
			continue;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1820
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1821
		/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1822
		 * It may take a long time to evict all the bufs requested.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1823
		 * To avoid blocking all arc activity, periodically drop
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1824
		 * the arcs_mtx and give other threads a chance to run
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1825
		 * before reacquiring the lock.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1826
		 *
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1827
		 * If we are looking for a buffer to recycle, we are in
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1828
		 * the hot code path, so don't sleep.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1829
		 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1830
		if (!recycle && count++ > arc_evict_iterations) {
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1831
			list_insert_after(list, ab, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1832
			mutex_exit(&evicted_state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1833
			mutex_exit(&state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1834
			kpreempt(KPREEMPT_SYNC);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1835
			mutex_enter(&state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1836
			mutex_enter(&evicted_state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1837
			ab_prev = list_prev(list, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1838
			list_remove(list, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1839
			count = 0;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1840
			continue;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1841
		}
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1842
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1843
		hash_lock = HDR_LOCK(ab);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1844
		have_lock = MUTEX_HELD(hash_lock);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1845
		if (have_lock || mutex_tryenter(hash_lock)) {
13805
e3a9ae14a119 3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument is zero
Madhav Suresh <madhav.suresh@delphix.com>
parents: 13790
diff changeset
  1846
			ASSERT0(refcount_count(&ab->b_refcnt));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1847
			ASSERT(ab->b_datacnt > 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1848
			while (ab->b_buf) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1849
				arc_buf_t *buf = ab->b_buf;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1850
				if (!mutex_tryenter(&buf->b_evict_lock)) {
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1851
					missed += 1;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1852
					break;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1853
				}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1854
				if (buf->b_data) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1855
					bytes_evicted += ab->b_size;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  1856
					if (recycle && ab->b_type == type &&
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1857
					    ab->b_size == bytes &&
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1858
					    !HDR_L2_WRITING(ab)) {
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1859
						stolen = buf->b_data;
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1860
						recycle = FALSE;
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1861
					}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1862
				}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1863
				if (buf->b_efunc) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1864
					mutex_enter(&arc_eviction_mtx);
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1865
					arc_buf_destroy(buf,
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1866
					    buf->b_data == stolen, FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1867
					ab->b_buf = buf->b_next;
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  1868
					buf->b_hdr = &arc_eviction_hdr;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1869
					buf->b_next = arc_eviction_list;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1870
					arc_eviction_list = buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1871
					mutex_exit(&arc_eviction_mtx);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1872
					mutex_exit(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1873
				} else {
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  1874
					mutex_exit(&buf->b_evict_lock);
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1875
					arc_buf_destroy(buf,
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1876
					    buf->b_data == stolen, TRUE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1877
				}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1878
			}
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1879
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1880
			if (ab->b_l2hdr) {
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1881
				ARCSTAT_INCR(arcstat_evict_l2_cached,
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1882
				    ab->b_size);
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1883
			} else {
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1884
				if (l2arc_write_eligible(ab->b_spa, ab)) {
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1885
					ARCSTAT_INCR(arcstat_evict_l2_eligible,
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1886
					    ab->b_size);
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1887
				} else {
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1888
					ARCSTAT_INCR(
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1889
					    arcstat_evict_l2_ineligible,
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1890
					    ab->b_size);
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1891
				}
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1892
			}
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  1893
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1894
			if (ab->b_datacnt == 0) {
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1895
				arc_change_state(evicted_state, ab, hash_lock);
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1896
				ASSERT(HDR_IN_HASH_TABLE(ab));
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1897
				ab->b_flags |= ARC_IN_HASH_TABLE;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1898
				ab->b_flags &= ~ARC_BUF_AVAILABLE;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1899
				DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  1900
			}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1901
			if (!have_lock)
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1902
				mutex_exit(hash_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1903
			if (bytes >= 0 && bytes_evicted >= bytes)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1904
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1905
		} else {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1906
			missed += 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1907
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
	}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1909
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1910
	mutex_exit(&evicted_state->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1911
	mutex_exit(&state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1913
	if (bytes_evicted < bytes)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
		dprintf("only evicted %lld bytes from %x",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1915
		    (longlong_t)bytes_evicted, state);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1917
	if (skipped)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1918
		ARCSTAT_INCR(arcstat_evict_skip, skipped);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1919
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  1920
	if (missed)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1921
		ARCSTAT_INCR(arcstat_mutex_miss, missed);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1922
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4645
diff changeset
  1923
	/*
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1924
	 * Note: we have just evicted some data into the ghost state,
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1925
	 * potentially putting the ghost size over the desired size.  Rather
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1926
	 * that evicting from the ghost list in this hot code path, leave
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1927
	 * this chore to the arc_reclaim_thread().
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4645
diff changeset
  1928
	 */
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4645
diff changeset
  1929
2918
5e0808c08daa 6478928 buffer recycling causes long arc mutex spins
maybee
parents: 2887
diff changeset
  1930
	return (stolen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1931
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1932
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
 * Remove buffers from list until we've removed the specified number of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
 * bytes.  Destroy the buffers that are removed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1936
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
static void
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  1938
arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
	arc_buf_hdr_t *ab, *ab_prev;
12674
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1941
	arc_buf_hdr_t marker = { 0 };
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1942
	list_t *list = &state->arcs_list[ARC_BUFC_DATA];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
	kmutex_t *hash_lock;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1944
	uint64_t bytes_deleted = 0;
3700
3a7e0afcf1b4 6523936 BAD TRAP: type=8 (#df Double fault) rp=fec266f8 addr=0
ek110237
parents: 3552
diff changeset
  1945
	uint64_t bufs_skipped = 0;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1946
	int count = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1947
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1948
	ASSERT(GHOST_STATE(state));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
top:
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1950
	mutex_enter(&state->arcs_mtx);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1951
	for (ab = list_tail(list); ab; ab = ab_prev) {
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  1952
		ab_prev = list_prev(list, ab);
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1953
		if (ab->b_type > ARC_BUFC_NUMTYPES)
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1954
			panic("invalid ab=%p", (void *)ab);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1955
		if (spa && ab->b_spa != spa)
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  1956
			continue;
12674
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1957
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1958
		/* ignore markers */
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1959
		if (ab->b_spa == 0)
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1960
			continue;
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  1961
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1962
		hash_lock = HDR_LOCK(ab);
12033
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1963
		/* caller may be trying to modify this buffer, skip it */
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1964
		if (MUTEX_HELD(hash_lock))
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1965
			continue;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1966
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1967
		/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1968
		 * It may take a long time to evict all the bufs requested.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1969
		 * To avoid blocking all arc activity, periodically drop
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1970
		 * the arcs_mtx and give other threads a chance to run
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1971
		 * before reacquiring the lock.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1972
		 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1973
		if (count++ > arc_evict_iterations) {
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1974
			list_insert_after(list, ab, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1975
			mutex_exit(&state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1976
			kpreempt(KPREEMPT_SYNC);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1977
			mutex_enter(&state->arcs_mtx);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1978
			ab_prev = list_prev(list, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1979
			list_remove(list, &marker);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1980
			count = 0;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1981
			continue;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  1982
		}
12033
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1983
		if (mutex_tryenter(hash_lock)) {
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  1984
			ASSERT(!HDR_IO_IN_PROGRESS(ab));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1985
			ASSERT(ab->b_buf == NULL);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  1986
			ARCSTAT_BUMP(arcstat_deleted);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  1987
			bytes_deleted += ab->b_size;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1988
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1989
			if (ab->b_l2hdr != NULL) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1990
				/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1991
				 * This buffer is cached on the 2nd Level ARC;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1992
				 * don't destroy the header.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1993
				 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1994
				arc_change_state(arc_l2c_only, ab, hash_lock);
12033
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1995
				mutex_exit(hash_lock);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1996
			} else {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1997
				arc_change_state(arc_anon, ab, hash_lock);
12033
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  1998
				mutex_exit(hash_lock);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  1999
				arc_hdr_destroy(ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2000
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2001
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2002
			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2003
			if (bytes >= 0 && bytes_deleted >= bytes)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2004
				break;
12674
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2005
		} else if (bytes < 0) {
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2006
			/*
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2007
			 * Insert a list marker and then wait for the
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2008
			 * hash lock to become available. Once its
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2009
			 * available, restart from where we left off.
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2010
			 */
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2011
			list_insert_after(list, ab, &marker);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2012
			mutex_exit(&state->arcs_mtx);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2013
			mutex_enter(hash_lock);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2014
			mutex_exit(hash_lock);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2015
			mutex_enter(&state->arcs_mtx);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2016
			ab_prev = list_prev(list, &marker);
fb4985fff3dd 6954665 system stuck spinning in arc_evict_ghost()
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 12636
diff changeset
  2017
			list_remove(list, &marker);
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  2018
		} else {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
			bufs_skipped += 1;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  2020
		}
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  2021
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2022
	}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2023
	mutex_exit(&state->arcs_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2024
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2025
	if (list == &state->arcs_list[ARC_BUFC_DATA] &&
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2026
	    (bytes < 0 || bytes_deleted < bytes)) {
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2027
		list = &state->arcs_list[ARC_BUFC_METADATA];
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2028
		goto top;
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2029
	}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2030
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2031
	if (bufs_skipped) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2032
		ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2033
		ASSERT(bytes >= 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2034
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2035
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2036
	if (bytes_deleted < bytes)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2037
		dprintf("only deleted %lld bytes from %p",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2038
		    (longlong_t)bytes_deleted, state);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2039
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2040
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2041
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2042
arc_adjust(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
{
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2044
	int64_t adjustment, delta;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2045
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2046
	/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2047
	 * Adjust MRU size
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2048
	 */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2049
12636
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2050
	adjustment = MIN((int64_t)(arc_size - arc_c),
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2051
	    (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2052
	    arc_p));
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2053
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2054
	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2055
		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2056
		(void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2057
		adjustment -= delta;
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2058
	}
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2059
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2060
	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2061
		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2062
		(void) arc_evict(arc_mru, NULL, delta, FALSE,
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2063
		    ARC_BUFC_METADATA);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2064
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2065
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2066
	/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2067
	 * Adjust MFU size
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2068
	 */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2069
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2070
	adjustment = arc_size - arc_c;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2071
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2072
	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2073
		delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2074
		(void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2075
		adjustment -= delta;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2076
	}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2077
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2078
	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2079
		int64_t delta = MIN(adjustment,
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2080
		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2081
		(void) arc_evict(arc_mfu, NULL, delta, FALSE,
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2082
		    ARC_BUFC_METADATA);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2083
	}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2084
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2085
	/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2086
	 * Adjust ghost lists
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2087
	 */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2088
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2089
	adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2090
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2091
	if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2092
		delta = MIN(arc_mru_ghost->arcs_size, adjustment);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2093
		arc_evict_ghost(arc_mru_ghost, NULL, delta);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2094
	}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2095
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2096
	adjustment =
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2097
	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2098
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2099
	if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2100
		delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2101
		arc_evict_ghost(arc_mfu_ghost, NULL, delta);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2103
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2104
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2105
static void
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2106
arc_do_user_evicts(void)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2107
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2108
	mutex_enter(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2109
	while (arc_eviction_list != NULL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2110
		arc_buf_t *buf = arc_eviction_list;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2111
		arc_eviction_list = buf->b_next;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2112
		mutex_enter(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2113
		buf->b_hdr = NULL;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2114
		mutex_exit(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2115
		mutex_exit(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2116
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1775
diff changeset
  2117
		if (buf->b_efunc != NULL)
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1775
diff changeset
  2118
			VERIFY(buf->b_efunc(buf) == 0);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2119
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2120
		buf->b_efunc = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2121
		buf->b_private = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2122
		kmem_cache_free(buf_cache, buf);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2123
		mutex_enter(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2124
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2125
	mutex_exit(&arc_eviction_mtx);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2126
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2127
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2128
/*
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2129
 * Flush all *evictable* data from the cache for the given spa.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2130
 * NOTE: this will not touch "active" (i.e. referenced) data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2131
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2132
void
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2133
arc_flush(spa_t *spa)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
{
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2135
	uint64_t guid = 0;
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2136
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2137
	if (spa)
13514
417c34452f03 1748 desire support for reguid in zfs
Garrett D'Amore <garrett@nexenta.com>
parents: 13061
diff changeset
  2138
		guid = spa_load_guid(spa);
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2139
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2140
	while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2141
		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2142
		if (spa)
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2143
			break;
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2144
	}
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2145
	while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2146
		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2147
		if (spa)
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2148
			break;
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2149
	}
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2150
	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2151
		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2152
		if (spa)
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2153
			break;
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2154
	}
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2155
	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2156
		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2157
		if (spa)
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2158
			break;
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2159
	}
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2160
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2161
	arc_evict_ghost(arc_mru_ghost, guid, -1);
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2162
	arc_evict_ghost(arc_mfu_ghost, guid, -1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2163
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2164
	mutex_enter(&arc_reclaim_thr_lock);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2165
	arc_do_user_evicts();
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2166
	mutex_exit(&arc_reclaim_thr_lock);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2167
	ASSERT(spa || arc_eviction_list == NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2168
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2169
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
void
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2171
arc_shrink(void)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
{
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2173
	if (arc_c > arc_c_min) {
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2174
		uint64_t to_free;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
2048
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2176
#ifdef _KERNEL
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2177
		to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
2048
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2178
#else
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2179
		to_free = arc_c >> arc_shrink_shift;
2048
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2180
#endif
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2181
		if (arc_c > arc_c_min + to_free)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2182
			atomic_add_64(&arc_c, -to_free);
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2183
		else
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2184
			arc_c = arc_c_min;
2048
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2185
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2186
		atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2187
		if (arc_c > arc_size)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2188
			arc_c = MAX(arc_size, arc_c_min);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2189
		if (arc_p > arc_c)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2190
			arc_p = (arc_c >> 1);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2191
		ASSERT(arc_c >= arc_c_min);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2192
		ASSERT((int64_t)arc_p >= 0);
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2193
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2194
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2195
	if (arc_size > arc_c)
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2196
		arc_adjust();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2197
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2198
13682
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2199
/*
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2200
 * Determine if the system is under memory pressure and is asking
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2201
 * to reclaim memory. A return value of 1 indicates that the system
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2202
 * is under memory pressure and that the arc should adjust accordingly.
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2203
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2204
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2205
arc_reclaim_needed(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2206
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2207
	uint64_t extra;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2208
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2209
#ifdef _KERNEL
2048
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2210
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2211
	if (needfree)
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2212
		return (1);
8ceabdf91507 4034947 anon_swap_adjust() should call kmem_reap() if availrmem is low.
stans
parents: 1990
diff changeset
  2213
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2214
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2215
	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2216
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2217
	extra = desfree;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2218
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2219
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2220
	 * check that we're out of range of the pageout scanner.  It starts to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2221
	 * schedule paging if freemem is less than lotsfree and needfree.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2222
	 * lotsfree is the high-water mark for pageout, and needfree is the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2223
	 * number of needed free pages.  We add extra pages here to make sure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2224
	 * the scanner doesn't start up while we're freeing memory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2225
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2226
	if (freemem < lotsfree + needfree + extra)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2227
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2229
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2230
	 * check to make sure that swapfs has enough space so that anon
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2231
	 * reservations can still succeed. anon_resvmem() checks that the
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2232
	 * availrmem is greater than swapfs_minfree, and the number of reserved
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2233
	 * swap pages.  We also add a bit of extra here just to prevent
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2234
	 * circumstances from getting really dire.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2235
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2236
	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2237
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2238
14179
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2239
	/*
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2240
	 * Check that we have enough availrmem that memory locking (e.g., via
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2241
	 * mlock(3C) or memcntl(2)) can still succeed.  (pages_pp_maximum
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2242
	 * stores the number of pages that cannot be locked; when availrmem
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2243
	 * drops below pages_pp_maximum, page locking mechanisms such as
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2244
	 * page_pp_lock() will fail.)
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2245
	 */
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2246
	if (availrmem <= pages_pp_maximum)
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2247
		return (1);
15e9457c12ec 1974 pages_pp_maximum/lotsfree discrepancy leads to mlock() failure
Bryan Cantrill <bryan@joyent.com>
parents: 14167
diff changeset
  2248
1936
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2249
#if defined(__i386)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2250
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2251
	 * If we're on an i386 platform, it's possible that we'll exhaust the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2252
	 * kernel heap space before we ever run out of available physical
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2253
	 * memory.  Most checks of the size of the heap_area compare against
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2254
	 * tune.t_minarmem, which is the minimum available real memory that we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2255
	 * can have in the system.  However, this is generally fixed at 25 pages
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2256
	 * which is so low that it's useless.  In this comparison, we seek to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2257
	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2258
	 * heap is allocated.  (Or, in the calculation, if less than 1/4th is
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2259
	 * free)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2260
	 */
13682
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2261
	if (vmem_size(heap_arena, VMEM_FREE) <
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2262
	    (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2263
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2264
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2265
13682
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2266
	/*
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2267
	 * If zio data pages are being allocated out of a separate heap segment,
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2268
	 * then enforce that the size of available vmem for this arena remains
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2269
	 * above about 1/16th free.
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2270
	 *
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2271
	 * Note: The 1/16th arena free requirement was put in place
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2272
	 * to aggressively evict memory from the arc in order to avoid
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2273
	 * memory fragmentation issues.
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2274
	 */
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2275
	if (zio_arena != NULL &&
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2276
	    vmem_size(zio_arena, VMEM_FREE) <
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2277
	    (vmem_size(zio_arena, VMEM_ALLOC) >> 4))
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2278
		return (1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2279
#else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2280
	if (spa_get_random(100) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2281
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2282
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2283
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2284
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2285
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2286
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2287
arc_kmem_reap_now(arc_reclaim_strategy_t strat)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2288
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2289
	size_t			i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2290
	kmem_cache_t		*prev_cache = NULL;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2291
	kmem_cache_t		*prev_data_cache = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2292
	extern kmem_cache_t	*zio_buf_cache[];
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2293
	extern kmem_cache_t	*zio_data_buf_cache[];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2294
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
  2295
#ifdef _KERNEL
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2296
	if (arc_meta_used >= arc_meta_limit) {
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2297
		/*
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2298
		 * We are exceeding our meta-data cache limit.
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2299
		 * Purge some DNLC entries to release holds on meta-data.
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2300
		 */
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2301
		dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2302
	}
1936
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2303
#if defined(__i386)
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2304
	/*
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2305
	 * Reclaim unused memory from all kmem caches.
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2306
	 */
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2307
	kmem_reap();
b6ded90e4d4b 6398177 zfs: poor nightly build performance in 32-bit mode (high disk activity)
maybee
parents: 1819
diff changeset
  2308
#endif
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
  2309
#endif
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 982
diff changeset
  2310
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
	/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2312
	 * An aggressive reclamation will shrink the cache size as well as
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2313
	 * reap free buffers from the arc kmem caches.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2314
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
	if (strat == ARC_RECLAIM_AGGR)
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  2316
		arc_shrink();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2319
		if (zio_buf_cache[i] != prev_cache) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2320
			prev_cache = zio_buf_cache[i];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2321
			kmem_cache_reap_now(zio_buf_cache[i]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2322
		}
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2323
		if (zio_data_buf_cache[i] != prev_data_cache) {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2324
			prev_data_cache = zio_data_buf_cache[i];
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2325
			kmem_cache_reap_now(zio_data_buf_cache[i]);
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2326
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2327
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2328
	kmem_cache_reap_now(buf_cache);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2329
	kmem_cache_reap_now(hdr_cache);
13682
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2330
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2331
	/*
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2332
	 * Ask the vmem areana to reclaim unused memory from its
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2333
	 * quantum caches.
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2334
	 */
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2335
	if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR)
e7836650181b 1618 zfs causing system to hang in vmem_xalloc()
George Wilson <gwilson@delphix.com>
parents: 13514
diff changeset
  2336
		vmem_qcache_reap(zio_arena);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2337
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2338
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2339
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2340
arc_reclaim_thread(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2341
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2342
	clock_t			growtime = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2343
	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2344
	callb_cpr_t		cpr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2345
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2346
	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2347
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2348
	mutex_enter(&arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
	while (arc_thread_exit == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
		if (arc_reclaim_needed()) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2351
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2352
			if (arc_no_grow) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2353
				if (last_reclaim == ARC_RECLAIM_CONS) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2354
					last_reclaim = ARC_RECLAIM_AGGR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2355
				} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2356
					last_reclaim = ARC_RECLAIM_CONS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2357
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
			} else {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2359
				arc_no_grow = TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
				last_reclaim = ARC_RECLAIM_AGGR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
				membar_producer();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2362
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2363
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2364
			/* reset the growth delay for every reclaim */
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2365
			growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2366
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2367
			arc_kmem_reap_now(last_reclaim);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  2368
			arc_warm = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2369
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2370
		} else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2371
			arc_no_grow = FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2372
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
12636
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2374
		arc_adjust();
3298
1fb2668efa39 6505658 target MRU size (arc.p) needs to be adjusted more aggressively
maybee
parents: 3290
diff changeset
  2375
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2376
		if (arc_eviction_list != NULL)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2377
			arc_do_user_evicts();
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2378
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2379
		/* block until needed, or one second, whichever is shorter */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
		CALLB_CPR_SAFE_BEGIN(&cpr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
		(void) cv_timedwait(&arc_reclaim_thr_cv,
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2382
		    &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2383
		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2385
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2386
	arc_thread_exit = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2387
	cv_broadcast(&arc_reclaim_thr_cv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2388
	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
	thread_exit();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2391
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2392
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2393
 * Adapt arc info given the number of bytes we are trying to add and
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2394
 * the state that we are comming from.  This function is only called
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2395
 * when we are adding new content to the cache.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2396
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2397
static void
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2398
arc_adapt(int bytes, arc_state_t *state)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2400
	int mult;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2401
	uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2402
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2403
	if (state == arc_l2c_only)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2404
		return;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2405
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2406
	ASSERT(bytes > 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2407
	/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2408
	 * Adapt the target size of the MRU list:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2409
	 *	- if we just hit in the MRU ghost list, then increase
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2410
	 *	  the target size of the MRU list.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2411
	 *	- if we just hit in the MFU ghost list, then increase
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2412
	 *	  the target size of the MFU list by decreasing the
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2413
	 *	  target size of the MRU list.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2414
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2415
	if (state == arc_mru_ghost) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2416
		mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2417
		    1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
12636
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2418
		mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2419
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2420
		arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2421
	} else if (state == arc_mfu_ghost) {
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2422
		uint64_t delta;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2423
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2424
		mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2425
		    1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
12636
13b5d698941e 6950219 large ghost eviction causes high write latency
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 12515
diff changeset
  2426
		mult = MIN(mult, 10);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2427
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2428
		delta = MIN(bytes * mult, arc_p);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2429
		arc_p = MAX(arc_p_min, arc_p - delta);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2430
	}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2431
	ASSERT((int64_t)arc_p >= 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2432
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
	if (arc_reclaim_needed()) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2434
		cv_signal(&arc_reclaim_thr_cv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2435
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2436
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2437
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2438
	if (arc_no_grow)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2439
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2440
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2441
	if (arc_c >= arc_c_max)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2442
		return;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2443
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2444
	/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2445
	 * If we're within (2 * maxblocksize) bytes of the target
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2446
	 * cache size, increment the target cache size
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2447
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2448
	if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2449
		atomic_add_64(&arc_c, (int64_t)bytes);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2450
		if (arc_c > arc_c_max)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2451
			arc_c = arc_c_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2452
		else if (state == arc_anon)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2453
			atomic_add_64(&arc_p, (int64_t)bytes);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2454
		if (arc_p > arc_c)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2455
			arc_p = arc_c;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2456
	}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2457
	ASSERT((int64_t)arc_p >= 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2458
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2460
/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2461
 * Check if the cache has reached its limits and eviction is required
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2462
 * prior to insert.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2463
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2464
static int
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2465
arc_evict_needed(arc_buf_contents_t type)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2466
{
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2467
	if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2468
		return (1);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2469
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2470
	if (arc_reclaim_needed())
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2471
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2472
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2473
	return (arc_size > arc_c);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2474
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2475
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2476
/*
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2477
 * The buffer, supplied as the first argument, needs a data block.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2478
 * So, if we are at cache max, determine which cache should be victimized.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2479
 * We have the following cases:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2480
 *
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2481
 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2482
 * In this situation if we're out of space, but the resident size of the MFU is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2483
 * under the limit, victimize the MFU cache to satisfy this insertion request.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2484
 *
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2485
 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2486
 * Here, we've used up all of the available space for the MRU, so we need to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2487
 * evict from our own cache instead.  Evict from the set of resident MRU
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2488
 * entries.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2489
 *
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2490
 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2491
 * c minus p represents the MFU space in the cache, since p is the size of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2492
 * cache that is dedicated to the MRU.  In this situation there's still space on
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2493
 * the MFU side, so the MRU side needs to be victimized.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2494
 *
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2495
 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2496
 * MFU's resident set is consuming more space than it has been allotted.  In
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2497
 * this situation, we must victimize our own cache, the MFU, for this insertion.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2498
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2499
static void
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2500
arc_get_data_buf(arc_buf_t *buf)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
{
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2502
	arc_state_t		*state = buf->b_hdr->b_state;
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2503
	uint64_t		size = buf->b_hdr->b_size;
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2504
	arc_buf_contents_t	type = buf->b_hdr->b_type;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2505
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2506
	arc_adapt(size, state);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2507
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2508
	/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2509
	 * We have not yet reached cache maximum size,
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2510
	 * just allocate a new buffer.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2511
	 */
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2512
	if (!arc_evict_needed(type)) {
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2513
		if (type == ARC_BUFC_METADATA) {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2514
			buf->b_data = zio_buf_alloc(size);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2515
			arc_space_consume(size, ARC_SPACE_DATA);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2516
		} else {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2517
			ASSERT(type == ARC_BUFC_DATA);
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2518
			buf->b_data = zio_data_buf_alloc(size);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2519
			ARCSTAT_INCR(arcstat_data_size, size);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2520
			atomic_add_64(&arc_size, size);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2521
		}
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2522
		goto out;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2523
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2524
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2525
	/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2526
	 * If we are prefetching from the mfu ghost list, this buffer
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2527
	 * will end up on the mru list; so steal space from there.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2528
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2529
	if (state == arc_mfu_ghost)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2530
		state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2531
	else if (state == arc_mru_ghost)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2532
		state = arc_mru;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2533
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2534
	if (state == arc_mru || state == arc_anon) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2535
		uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2536
		state = (arc_mfu->arcs_lsize[type] >= size &&
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2537
		    arc_p > mru_used) ? arc_mfu : arc_mru;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2538
	} else {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2539
		/* MFU cases */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2540
		uint64_t mfu_space = arc_c - arc_p;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2541
		state =  (arc_mru->arcs_lsize[type] >= size &&
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2542
		    mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2543
	}
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  2544
	if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2545
		if (type == ARC_BUFC_METADATA) {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2546
			buf->b_data = zio_buf_alloc(size);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2547
			arc_space_consume(size, ARC_SPACE_DATA);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2548
		} else {
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2549
			ASSERT(type == ARC_BUFC_DATA);
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2550
			buf->b_data = zio_data_buf_alloc(size);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2551
			ARCSTAT_INCR(arcstat_data_size, size);
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2552
			atomic_add_64(&arc_size, size);
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2553
		}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2554
		ARCSTAT_BUMP(arcstat_recycle_miss);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2555
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2556
	ASSERT(buf->b_data != NULL);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2557
out:
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2558
	/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2559
	 * Update the state size.  Note that ghost states have a
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2560
	 * "ghost size" and so don't need to be updated.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2561
	 */
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2562
	if (!GHOST_STATE(buf->b_hdr->b_state)) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2563
		arc_buf_hdr_t *hdr = buf->b_hdr;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2564
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2565
		atomic_add_64(&hdr->b_state->arcs_size, size);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2566
		if (list_link_active(&hdr->b_arc_node)) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2567
			ASSERT(refcount_is_zero(&hdr->b_refcnt));
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2568
			atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2569
		}
3298
1fb2668efa39 6505658 target MRU size (arc.p) needs to be adjusted more aggressively
maybee
parents: 3290
diff changeset
  2570
		/*
1fb2668efa39 6505658 target MRU size (arc.p) needs to be adjusted more aggressively
maybee
parents: 3290
diff changeset
  2571
		 * If we are growing the cache, and we are adding anonymous
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2572
		 * data, and we have outgrown arc_p, update arc_p
3298
1fb2668efa39 6505658 target MRU size (arc.p) needs to be adjusted more aggressively
maybee
parents: 3290
diff changeset
  2573
		 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2574
		if (arc_size < arc_c && hdr->b_state == arc_anon &&
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2575
		    arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2576
			arc_p = MIN(arc_c, arc_p + size);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2577
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2578
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2579
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2580
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2581
 * This routine is called whenever a buffer is accessed.
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2582
 * NOTE: the hash lock is dropped in this function.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2583
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2584
static void
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2585
arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2586
{
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2587
	clock_t now;
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2588
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2589
	ASSERT(MUTEX_HELD(hash_lock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2590
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2591
	if (buf->b_state == arc_anon) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2592
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2593
		 * This buffer is not in the cache, and does not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2594
		 * appear in our "ghost" list.  Add the new buffer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2595
		 * to the MRU state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2596
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2598
		ASSERT(buf->b_arc_access == 0);
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2599
		buf->b_arc_access = ddi_get_lbolt();
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2600
		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2601
		arc_change_state(arc_mru, buf, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2602
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2603
	} else if (buf->b_state == arc_mru) {
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2604
		now = ddi_get_lbolt();
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2605
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2606
		/*
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2607
		 * If this buffer is here because of a prefetch, then either:
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2608
		 * - clear the flag if this is a "referencing" read
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2609
		 *   (any subsequent access will bump this into the MFU state).
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2610
		 * or
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2611
		 * - move the buffer to the head of the list if this is
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2612
		 *   another prefetch (to make it less likely to be evicted).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2613
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2614
		if ((buf->b_flags & ARC_PREFETCH) != 0) {
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2615
			if (refcount_count(&buf->b_refcnt) == 0) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2616
				ASSERT(list_link_active(&buf->b_arc_node));
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2617
			} else {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2618
				buf->b_flags &= ~ARC_PREFETCH;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2619
				ARCSTAT_BUMP(arcstat_mru_hits);
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2620
			}
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2621
			buf->b_arc_access = now;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2622
			return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2623
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2625
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2626
		 * This buffer has been "accessed" only once so far,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2627
		 * but it is still in the cache. Move it to the MFU
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2628
		 * state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2629
		 */
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2630
		if (now > buf->b_arc_access + ARC_MINTIME) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
			 * More than 125ms have passed since we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2633
			 * instantiated this buffer.  Move it to the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2634
			 * most frequently used state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2635
			 */
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2636
			buf->b_arc_access = now;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2637
			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2638
			arc_change_state(arc_mfu, buf, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2639
		}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2640
		ARCSTAT_BUMP(arcstat_mru_hits);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2641
	} else if (buf->b_state == arc_mru_ghost) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2642
		arc_state_t	*new_state;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2643
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2644
		 * This buffer has been "accessed" recently, but
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2645
		 * was evicted from the cache.  Move it to the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2646
		 * MFU state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2647
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2648
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2649
		if (buf->b_flags & ARC_PREFETCH) {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2650
			new_state = arc_mru;
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2651
			if (refcount_count(&buf->b_refcnt) > 0)
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2652
				buf->b_flags &= ~ARC_PREFETCH;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2653
			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2654
		} else {
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2655
			new_state = arc_mfu;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2656
			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2657
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2658
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2659
		buf->b_arc_access = ddi_get_lbolt();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2660
		arc_change_state(new_state, buf, hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2661
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2662
		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2663
	} else if (buf->b_state == arc_mfu) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2664
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2665
		 * This buffer has been accessed more than once and is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2666
		 * still in the cache.  Keep it in the MFU state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2667
		 *
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2668
		 * NOTE: an add_reference() that occurred when we did
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2669
		 * the arc_read() will have kicked this off the list.
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2670
		 * If it was a prefetch, we will explicitly move it to
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2671
		 * the head of the list now.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2672
		 */
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2673
		if ((buf->b_flags & ARC_PREFETCH) != 0) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2674
			ASSERT(refcount_count(&buf->b_refcnt) == 0);
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2675
			ASSERT(list_link_active(&buf->b_arc_node));
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2676
		}
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2677
		ARCSTAT_BUMP(arcstat_mfu_hits);
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2678
		buf->b_arc_access = ddi_get_lbolt();
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2679
	} else if (buf->b_state == arc_mfu_ghost) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2680
		arc_state_t	*new_state = arc_mfu;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2681
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2682
		 * This buffer has been accessed more than once but has
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2683
		 * been evicted from the cache.  Move it back to the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2684
		 * MFU state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2685
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2686
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2687
		if (buf->b_flags & ARC_PREFETCH) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2688
			/*
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2689
			 * This is a prefetch access...
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2690
			 * move this block back to the MRU state.
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2691
			 */
13805
e3a9ae14a119 3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument is zero
Madhav Suresh <madhav.suresh@delphix.com>
parents: 13790
diff changeset
  2692
			ASSERT0(refcount_count(&buf->b_refcnt));
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2693
			new_state = arc_mru;
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2694
		}
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2695
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2696
		buf->b_arc_access = ddi_get_lbolt();
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2697
		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2698
		arc_change_state(new_state, buf, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2699
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2700
		ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2701
	} else if (buf->b_state == arc_l2c_only) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2702
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2703
		 * This buffer is on the 2nd Level ARC.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2704
		 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2705
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  2706
		buf->b_arc_access = ddi_get_lbolt();
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2707
		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2708
		arc_change_state(arc_mfu, buf, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2709
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2710
		ASSERT(!"invalid arc state");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2711
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2712
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2713
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2714
/* a generic arc_done_func_t which you can use */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2715
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2716
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2717
arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2718
{
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2719
	if (zio == NULL || zio->io_error == 0)
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2720
		bcopy(buf->b_data, arg, buf->b_hdr->b_size);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13958
diff changeset
  2721
	VERIFY(arc_buf_remove_ref(buf, arg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2722
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2723
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  2724
/* a generic arc_done_func_t */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2725
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2726
arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2727
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
	arc_buf_t **bufp = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2729
	if (zio && zio->io_error) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13958
diff changeset
  2730
		VERIFY(arc_buf_remove_ref(buf, arg));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
		*bufp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
		*bufp = buf;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2734
		ASSERT(buf->b_data);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2736
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2737
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2738
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2739
arc_read_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2740
{
1589
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2741
	arc_buf_hdr_t	*hdr, *found;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2742
	arc_buf_t	*buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2743
	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2744
	kmutex_t	*hash_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2745
	arc_callback_t	*callback_list, *acb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2746
	int		freeable = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2747
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2748
	buf = zio->io_private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2749
	hdr = buf->b_hdr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2750
1589
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2751
	/*
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2752
	 * The hdr was inserted into hash-table and removed from lists
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2753
	 * prior to starting I/O.  We should find this header, since
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2754
	 * it's in the hash table, and it should be legit since it's
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2755
	 * not possible to evict it during the I/O.  The only possible
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2756
	 * reason for it not to be found is if we were freed during the
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2757
	 * read.
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2758
	 */
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  2759
	found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  2760
	    &hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2761
1589
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2762
	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2763
	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2764
	    (found == hdr && HDR_L2_READING(hdr)));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2765
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  2766
	hdr->b_flags &= ~ARC_L2_EVICTED;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2767
	if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  2768
		hdr->b_flags &= ~ARC_L2CACHE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2769
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2770
	/* byteswap if necessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2771
	callback_list = hdr->b_acb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2772
	ASSERT(callback_list != NULL);
10839
cf83b553a2ab 6836714 arc_read_done may try to byteswap undefined data
William Gorrell <william.gorrell@sun.com>
parents: 10409
diff changeset
  2773
	if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13682
diff changeset
  2774
		dmu_object_byteswap_t bswap =
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13682
diff changeset
  2775
		    DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  2776
		arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  2777
		    byteswap_uint64_array :
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13682
diff changeset
  2778
		    dmu_ot_byteswap[bswap].ob_func;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  2779
		func(buf->b_data, hdr->b_size);
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  2780
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2781
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2782
	arc_cksum_compute(buf, B_FALSE);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  2783
	arc_buf_watch(buf);
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  2784
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2785
	if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2786
		/*
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2787
		 * Only call arc_access on anonymous buffers.  This is because
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2788
		 * if we've issued an I/O for an evicted buffer, we've already
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2789
		 * called arc_access (to prevent any simultaneous readers from
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2790
		 * getting confused).
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2791
		 */
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2792
		arc_access(hdr, hash_lock);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2793
	}
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2794
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2795
	/* create copies of the data buffer for the callers */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2796
	abuf = buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2797
	for (acb = callback_list; acb; acb = acb->acb_next) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2798
		if (acb->acb_done) {
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  2799
			if (abuf == NULL) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  2800
				ARCSTAT_BUMP(arcstat_duplicate_reads);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2801
				abuf = arc_buf_clone(buf);
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  2802
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2803
			acb->acb_buf = abuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2804
			abuf = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2805
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2806
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2807
	hdr->b_acb = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2808
	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2809
	ASSERT(!HDR_BUF_AVAILABLE(hdr));
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2810
	if (abuf == buf) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2811
		ASSERT(buf->b_efunc == NULL);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2812
		ASSERT(hdr->b_datacnt == 1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2813
		hdr->b_flags |= ARC_BUF_AVAILABLE;
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2814
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2815
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2816
	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2817
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2818
	if (zio->io_error != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2819
		hdr->b_flags |= ARC_IO_ERROR;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2820
		if (hdr->b_state != arc_anon)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2821
			arc_change_state(arc_anon, hdr, hash_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2822
		if (HDR_IN_HASH_TABLE(hdr))
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2823
			buf_hash_remove(hdr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2824
		freeable = refcount_is_zero(&hdr->b_refcnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2825
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2826
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2827
	/*
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2828
	 * Broadcast before we drop the hash_lock to avoid the possibility
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2829
	 * that the hdr (and hence the cv) might be freed before we get to
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2830
	 * the cv_broadcast().
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2831
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2832
	cv_broadcast(&hdr->b_cv);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2833
1589
64c230a17c47 6395526 assertion failed: refcount_count(&ab->b_refcnt) == 1 (0x2 == 0x1)
maybee
parents: 1544
diff changeset
  2834
	if (hash_lock) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2835
		mutex_exit(hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2836
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2837
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2838
		 * This block was freed while we waited for the read to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2839
		 * complete.  It has been removed from the hash table and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2840
		 * moved to the anonymous state (so that it won't show up
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2841
		 * in the cache).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2842
		 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2843
		ASSERT3P(hdr->b_state, ==, arc_anon);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2844
		freeable = refcount_is_zero(&hdr->b_refcnt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2845
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2846
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2847
	/* execute each callback and free its structure */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2848
	while ((acb = callback_list) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2849
		if (acb->acb_done)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2850
			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2851
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2852
		if (acb->acb_zio_dummy != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2853
			acb->acb_zio_dummy->io_error = zio->io_error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2854
			zio_nowait(acb->acb_zio_dummy);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2855
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2856
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2857
		callback_list = acb->acb_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2858
		kmem_free(acb, sizeof (arc_callback_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2859
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2860
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2861
	if (freeable)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2862
		arc_hdr_destroy(hdr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2863
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2864
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2865
/*
13721
5b51a16a186f 2618 arc.c mistypes in the comments
Bart Coddens <bart.coddens@gmail.com>
parents: 13700
diff changeset
  2866
 * "Read" the block at the specified DVA (in bp) via the
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2867
 * cache.  If the block is found in the cache, invoke the provided
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2868
 * callback immediately and return.  Note that the `zio' parameter
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2869
 * in the callback will be NULL in this case, since no IO was
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2870
 * required.  If the block is not in the cache pass the read request
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2871
 * on to the spa with a substitute callback function, so that the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2872
 * requested block will be added to the cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2873
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2874
 * If a read request arrives for a block that has a read in-progress,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2875
 * either wait for the in-progress read to complete (and return the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2876
 * results); or, if this is a read with a "done" func, add a record
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2877
 * to the read to invoke the "done" func when the read completes,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2878
 * and return; or just return.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2879
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2880
 * arc_read_done() will invoke all the requested "done" functions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2881
 * for readers of this block.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2882
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2883
int
13941
d48547176ab4 3498 panic in arc_read(): !refcount_is_zero(&pbuf->b_hdr->b_refcnt)
George Wilson <george.wilson@delphix.com>
parents: 13887
diff changeset
  2884
arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  2885
    void *private, zio_priority_t priority, int zio_flags, uint32_t *arc_flags,
13941
d48547176ab4 3498 panic in arc_read(): !refcount_is_zero(&pbuf->b_hdr->b_refcnt)
George Wilson <george.wilson@delphix.com>
parents: 13887
diff changeset
  2886
    const zbookmark_t *zb)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2887
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2888
	arc_buf_hdr_t *hdr;
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  2889
	arc_buf_t *buf = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2890
	kmutex_t *hash_lock;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  2891
	zio_t *rzio;
13514
417c34452f03 1748 desire support for reguid in zfs
Garrett D'Amore <garrett@nexenta.com>
parents: 13061
diff changeset
  2892
	uint64_t guid = spa_load_guid(spa);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2893
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2894
top:
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2895
	hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2896
	    &hash_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2897
	if (hdr && hdr->b_datacnt > 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2898
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2899
		*arc_flags |= ARC_CACHED;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2900
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2901
		if (HDR_IO_IN_PROGRESS(hdr)) {
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2902
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2903
			if (*arc_flags & ARC_WAIT) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2904
				cv_wait(&hdr->b_cv, hash_lock);
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2905
				mutex_exit(hash_lock);
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2906
				goto top;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2907
			}
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2908
			ASSERT(*arc_flags & ARC_NOWAIT);
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2909
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2910
			if (done) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2911
				arc_callback_t	*acb = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2912
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2913
				acb = kmem_zalloc(sizeof (arc_callback_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2914
				    KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2915
				acb->acb_done = done;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
				acb->acb_private = private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2917
				if (pio != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
					acb->acb_zio_dummy = zio_null(pio,
8632
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  2919
					    spa, NULL, NULL, NULL, zio_flags);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2920
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2921
				ASSERT(acb->acb_done != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2922
				acb->acb_next = hdr->b_acb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2923
				hdr->b_acb = acb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2924
				add_reference(hdr, hash_lock, private);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2925
				mutex_exit(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2926
				return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2927
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2928
			mutex_exit(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2929
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2930
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2931
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2932
		ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2933
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2934
		if (done) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2935
			add_reference(hdr, hash_lock, private);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2936
			/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2937
			 * If this block is already in use, create a new
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2938
			 * copy of the data so that we will be guaranteed
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2939
			 * that arc_release() will always succeed.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2940
			 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2941
			buf = hdr->b_buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2942
			ASSERT(buf);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2943
			ASSERT(buf->b_data);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2944
			if (HDR_BUF_AVAILABLE(hdr)) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2945
				ASSERT(buf->b_efunc == NULL);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2946
				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2947
			} else {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2948
				buf = arc_buf_clone(buf);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2949
			}
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2950
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2951
		} else if (*arc_flags & ARC_PREFETCH &&
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2952
		    refcount_count(&hdr->b_refcnt) == 0) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2953
			hdr->b_flags |= ARC_PREFETCH;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2954
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2955
		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2956
		arc_access(hdr, hash_lock);
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  2957
		if (*arc_flags & ARC_L2CACHE)
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  2958
			hdr->b_flags |= ARC_L2CACHE;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  2959
		if (*arc_flags & ARC_L2COMPRESS)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  2960
			hdr->b_flags |= ARC_L2COMPRESS;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2961
		mutex_exit(hash_lock);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2962
		ARCSTAT_BUMP(arcstat_hits);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2963
		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2964
		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2965
		    data, metadata, hits);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  2966
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2967
		if (done)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2968
			done(NULL, buf, private);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2969
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2970
		uint64_t size = BP_GET_LSIZE(bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2971
		arc_callback_t	*acb;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  2972
		vdev_t *vd = NULL;
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  2973
		uint64_t addr = 0;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  2974
		boolean_t devw = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2975
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2976
		if (hdr == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2977
			/* this block is not in the cache */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2978
			arc_buf_hdr_t	*exists;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2979
			arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  2980
			buf = arc_buf_alloc(spa, size, private, type);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2981
			hdr = buf->b_hdr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2982
			hdr->b_dva = *BP_IDENTITY(bp);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  2983
			hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2984
			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2985
			exists = buf_hash_insert(hdr, &hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2986
			if (exists) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2987
				/* somebody beat us to the hash insert */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2988
				mutex_exit(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  2989
				buf_discard_identity(hdr);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  2990
				(void) arc_buf_remove_ref(buf, private);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2991
				goto top; /* restart the IO request */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2992
			}
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2993
			/* if this is a prefetch, we don't have a reference */
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2994
			if (*arc_flags & ARC_PREFETCH) {
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2995
				(void) remove_reference(hdr, hash_lock,
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2996
				    private);
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2997
				hdr->b_flags |= ARC_PREFETCH;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  2998
			}
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  2999
			if (*arc_flags & ARC_L2CACHE)
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3000
				hdr->b_flags |= ARC_L2CACHE;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3001
			if (*arc_flags & ARC_L2COMPRESS)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3002
				hdr->b_flags |= ARC_L2COMPRESS;
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3003
			if (BP_GET_LEVEL(bp) > 0)
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3004
				hdr->b_flags |= ARC_INDIRECT;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3005
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3006
			/* this block is in the ghost cache */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3007
			ASSERT(GHOST_STATE(hdr->b_state));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3008
			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
13805
e3a9ae14a119 3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument is zero
Madhav Suresh <madhav.suresh@delphix.com>
parents: 13790
diff changeset
  3009
			ASSERT0(refcount_count(&hdr->b_refcnt));
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3010
			ASSERT(hdr->b_buf == NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3011
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3012
			/* if this is a prefetch, we don't have a reference */
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3013
			if (*arc_flags & ARC_PREFETCH)
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3014
				hdr->b_flags |= ARC_PREFETCH;
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3015
			else
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3016
				add_reference(hdr, hash_lock, private);
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3017
			if (*arc_flags & ARC_L2CACHE)
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3018
				hdr->b_flags |= ARC_L2CACHE;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3019
			if (*arc_flags & ARC_L2COMPRESS)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3020
				hdr->b_flags |= ARC_L2COMPRESS;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3021
			buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3022
			buf->b_hdr = hdr;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3023
			buf->b_data = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3024
			buf->b_efunc = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3025
			buf->b_private = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3026
			buf->b_next = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3027
			hdr->b_buf = buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3028
			ASSERT(hdr->b_datacnt == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3029
			hdr->b_datacnt = 1;
12033
308d70e4dce8 6935088 elpaso panics: kernel heap corruption detected
William Gorrell <william.gorrell@sun.com>
parents: 11805
diff changeset
  3030
			arc_get_data_buf(buf);
11805
d83782e908a6 6928826 panic: assertion failed: ab->b_buf == NULL, file: ../../common/fs/zfs/arc.c, line: 1738 - nightly
William Gorrell <william.gorrell@sun.com>
parents: 11712
diff changeset
  3031
			arc_access(hdr, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3032
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3033
11805
d83782e908a6 6928826 panic: assertion failed: ab->b_buf == NULL, file: ../../common/fs/zfs/arc.c, line: 1738 - nightly
William Gorrell <william.gorrell@sun.com>
parents: 11712
diff changeset
  3034
		ASSERT(!GHOST_STATE(hdr->b_state));
d83782e908a6 6928826 panic: assertion failed: ab->b_buf == NULL, file: ../../common/fs/zfs/arc.c, line: 1738 - nightly
William Gorrell <william.gorrell@sun.com>
parents: 11712
diff changeset
  3035
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3036
		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3037
		acb->acb_done = done;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3038
		acb->acb_private = private;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3039
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3040
		ASSERT(hdr->b_acb == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3041
		hdr->b_acb = acb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3042
		hdr->b_flags |= ARC_IO_IN_PROGRESS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3043
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3044
		if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3045
		    (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3046
			devw = hdr->b_l2hdr->b_dev->l2ad_writing;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3047
			addr = hdr->b_l2hdr->b_daddr;
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3048
			/*
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3049
			 * Lock out device removal.
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3050
			 */
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3051
			if (vdev_is_dead(vd) ||
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3052
			    !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3053
				vd = NULL;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3054
		}
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3055
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3056
		mutex_exit(hash_lock);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3057
14045
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3058
		/*
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3059
		 * At this point, we have a level 1 cache miss.  Try again in
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3060
		 * L2ARC if possible.
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3061
		 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3062
		ASSERT3U(hdr->b_size, ==, size);
10409
1f7aaa44950d 6876733 sdt:::arc-hit and sdt:::arc-miss provide inconsistent args[0]
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10407
diff changeset
  3063
		DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
1f7aaa44950d 6876733 sdt:::arc-hit and sdt:::arc-miss provide inconsistent args[0]
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10407
diff changeset
  3064
		    uint64_t, size, zbookmark_t *, zb);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3065
		ARCSTAT_BUMP(arcstat_misses);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3066
		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3067
		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3068
		    data, metadata, misses);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3069
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3070
		if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3071
			/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3072
			 * Read from the L2ARC if the following are true:
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3073
			 * 1. The L2ARC vdev was previously cached.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3074
			 * 2. This buffer still has L2ARC metadata.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3075
			 * 3. This buffer isn't currently writing to the L2ARC.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3076
			 * 4. The L2ARC entry wasn't evicted, which may
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3077
			 *    also have invalidated the vdev.
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3078
			 * 5. This isn't prefetch and l2arc_noprefetch is set.
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3079
			 */
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3080
			if (hdr->b_l2hdr != NULL &&
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3081
			    !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3082
			    !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3083
				l2arc_read_callback_t *cb;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3084
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  3085
				DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  3086
				ARCSTAT_BUMP(arcstat_l2_hits);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  3087
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3088
				cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3089
				    KM_SLEEP);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3090
				cb->l2rcb_buf = buf;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3091
				cb->l2rcb_spa = spa;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3092
				cb->l2rcb_bp = *bp;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3093
				cb->l2rcb_zb = *zb;
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3094
				cb->l2rcb_flags = zio_flags;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3095
				cb->l2rcb_compress = hdr->b_l2hdr->b_compress;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3096
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  3097
				ASSERT(addr >= VDEV_LABEL_START_SIZE &&
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  3098
				    addr + size < vd->vdev_psize -
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  3099
				    VDEV_LABEL_END_SIZE);
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  3100
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3101
				/*
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3102
				 * l2arc read.  The SCL_L2ARC lock will be
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3103
				 * released by l2arc_read_done().
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3104
				 * Issue a null zio if the underlying buffer
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3105
				 * was squashed to zero size by compression.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3106
				 */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3107
				if (hdr->b_l2hdr->b_compress ==
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3108
				    ZIO_COMPRESS_EMPTY) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3109
					rzio = zio_null(pio, spa, vd,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3110
					    l2arc_read_done, cb,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3111
					    zio_flags | ZIO_FLAG_DONT_CACHE |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3112
					    ZIO_FLAG_CANFAIL |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3113
					    ZIO_FLAG_DONT_PROPAGATE |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3114
					    ZIO_FLAG_DONT_RETRY);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3115
				} else {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3116
					rzio = zio_read_phys(pio, vd, addr,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3117
					    hdr->b_l2hdr->b_asize,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3118
					    buf->b_data, ZIO_CHECKSUM_OFF,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3119
					    l2arc_read_done, cb, priority,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3120
					    zio_flags | ZIO_FLAG_DONT_CACHE |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3121
					    ZIO_FLAG_CANFAIL |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3122
					    ZIO_FLAG_DONT_PROPAGATE |
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3123
					    ZIO_FLAG_DONT_RETRY, B_FALSE);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3124
				}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3125
				DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3126
				    zio_t *, rzio);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3127
				ARCSTAT_INCR(arcstat_l2_read_bytes,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3128
				    hdr->b_l2hdr->b_asize);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3129
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3130
				if (*arc_flags & ARC_NOWAIT) {
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3131
					zio_nowait(rzio);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3132
					return (0);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3133
				}
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3134
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3135
				ASSERT(*arc_flags & ARC_WAIT);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3136
				if (zio_wait(rzio) == 0)
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3137
					return (0);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3138
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3139
				/* l2arc read error; goto zio_read() */
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3140
			} else {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3141
				DTRACE_PROBE1(l2arc__miss,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3142
				    arc_buf_hdr_t *, hdr);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3143
				ARCSTAT_BUMP(arcstat_l2_misses);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3144
				if (HDR_L2_WRITING(hdr))
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3145
					ARCSTAT_BUMP(arcstat_l2_rw_clash);
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3146
				spa_config_exit(spa, SCL_L2ARC, vd);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3147
			}
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3148
		} else {
8628
97dcded6e556 6798268 missing spa_config_exit() in L2ARC hangs system
Bill Moore <Bill.Moore@Sun.COM>
parents: 8582
diff changeset
  3149
			if (vd != NULL)
97dcded6e556 6798268 missing spa_config_exit() in L2ARC hangs system
Bill Moore <Bill.Moore@Sun.COM>
parents: 8582
diff changeset
  3150
				spa_config_exit(spa, SCL_L2ARC, vd);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3151
			if (l2arc_ndev != 0) {
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3152
				DTRACE_PROBE1(l2arc__miss,
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3153
				    arc_buf_hdr_t *, hdr);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3154
				ARCSTAT_BUMP(arcstat_l2_misses);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3155
			}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3156
		}
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  3157
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3158
		rzio = zio_read(pio, spa, bp, buf->b_data, size,
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3159
		    arc_read_done, buf, priority, zio_flags, zb);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3160
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3161
		if (*arc_flags & ARC_WAIT)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3162
			return (zio_wait(rzio));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3163
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3164
		ASSERT(*arc_flags & ARC_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3165
		zio_nowait(rzio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3166
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3167
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3168
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3169
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3170
void
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3171
arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3172
{
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3173
	ASSERT(buf->b_hdr != NULL);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3174
	ASSERT(buf->b_hdr->b_state != arc_anon);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3175
	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3176
	ASSERT(buf->b_efunc == NULL);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3177
	ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3178
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3179
	buf->b_efunc = func;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3180
	buf->b_private = private;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3181
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3182
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3183
/*
14041
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3184
 * Notify the arc that a block was freed, and thus will never be used again.
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3185
 */
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3186
void
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3187
arc_freed(spa_t *spa, const blkptr_t *bp)
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3188
{
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3189
	arc_buf_hdr_t *hdr;
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3190
	kmutex_t *hash_lock;
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3191
	uint64_t guid = spa_load_guid(spa);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3192
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3193
	hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3194
	    &hash_lock);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3195
	if (hdr == NULL)
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3196
		return;
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3197
	if (HDR_BUF_AVAILABLE(hdr)) {
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3198
		arc_buf_t *buf = hdr->b_buf;
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3199
		add_reference(hdr, hash_lock, FTAG);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3200
		hdr->b_flags &= ~ARC_BUF_AVAILABLE;
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3201
		mutex_exit(hash_lock);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3202
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3203
		arc_release(buf, FTAG);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3204
		(void) arc_buf_remove_ref(buf, FTAG);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3205
	} else {
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3206
		mutex_exit(hash_lock);
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3207
	}
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3208
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3209
}
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3210
cb4a51285eeb 3805 arc shouldn't cache freed blocks
Matthew Ahrens <mahrens@delphix.com>
parents: 14039
diff changeset
  3211
/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3212
 * This is used by the DMU to let the ARC know that a buffer is
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3213
 * being evicted, so the ARC should clean up.  If this arc buf
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3214
 * is not yet in the evicted state, it will be put there.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3215
 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3216
int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3217
arc_buf_evict(arc_buf_t *buf)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3218
{
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  3219
	arc_buf_hdr_t *hdr;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3220
	kmutex_t *hash_lock;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3221
	arc_buf_t **bufp;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3222
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3223
	mutex_enter(&buf->b_evict_lock);
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  3224
	hdr = buf->b_hdr;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3225
	if (hdr == NULL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3226
		/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3227
		 * We are in arc_do_user_evicts().
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3228
		 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3229
		ASSERT(buf->b_data == NULL);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3230
		mutex_exit(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3231
		return (0);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3232
	} else if (buf->b_data == NULL) {
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3233
		arc_buf_t copy = *buf; /* structure assignment */
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3234
		/*
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3235
		 * We are on the eviction list; process this buffer now
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3236
		 * but let arc_do_user_evicts() do the reaping.
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3237
		 */
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3238
		buf->b_efunc = NULL;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3239
		mutex_exit(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3240
		VERIFY(copy.b_efunc(&copy) == 0);
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3241
		return (1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3242
	}
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  3243
	hash_lock = HDR_LOCK(hdr);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3244
	mutex_enter(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3245
	hdr = buf->b_hdr;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3246
	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3247
2724
99be9a0b86f6 6469119 race between arc_buf_clone() and arc_buf_add_ref() results in NULL pointer dereference
maybee
parents: 2688
diff changeset
  3248
	ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3249
	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3250
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3251
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3252
	 * Pull this buffer off of the hdr
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3253
	 */
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3254
	bufp = &hdr->b_buf;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3255
	while (*bufp != buf)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3256
		bufp = &(*bufp)->b_next;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3257
	*bufp = buf->b_next;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3258
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3259
	ASSERT(buf->b_data != NULL);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3260
	arc_buf_destroy(buf, FALSE, FALSE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3261
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3262
	if (hdr->b_datacnt == 0) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3263
		arc_state_t *old_state = hdr->b_state;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3264
		arc_state_t *evicted_state;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3265
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3266
		ASSERT(hdr->b_buf == NULL);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3267
		ASSERT(refcount_is_zero(&hdr->b_refcnt));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3268
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3269
		evicted_state =
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3270
		    (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3271
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3272
		mutex_enter(&old_state->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3273
		mutex_enter(&evicted_state->arcs_mtx);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3274
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3275
		arc_change_state(evicted_state, hdr, hash_lock);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3276
		ASSERT(HDR_IN_HASH_TABLE(hdr));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3277
		hdr->b_flags |= ARC_IN_HASH_TABLE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3278
		hdr->b_flags &= ~ARC_BUF_AVAILABLE;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3279
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3280
		mutex_exit(&evicted_state->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3281
		mutex_exit(&old_state->arcs_mtx);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3282
	}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3283
	mutex_exit(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3284
	mutex_exit(&buf->b_evict_lock);
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1775
diff changeset
  3285
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3286
	VERIFY(buf->b_efunc(buf) == 0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3287
	buf->b_efunc = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3288
	buf->b_private = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3289
	buf->b_hdr = NULL;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3290
	buf->b_next = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3291
	kmem_cache_free(buf_cache, buf);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3292
	return (1);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3293
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3294
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3295
/*
14045
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3296
 * Release this buffer from the cache, making it an anonymous buffer.  This
9475b3fef59d 3741 zfs needs better comments
Will Andrews <will@firepipe.net>
parents: 14041
diff changeset
  3297
 * must be done after a read and prior to modifying the buffer contents.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3298
 * If the buffer has more than one reference, we must make
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  3299
 * a new hdr for the buffer.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3300
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3301
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3302
arc_release(arc_buf_t *buf, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3303
{
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3304
	arc_buf_hdr_t *hdr;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3305
	kmutex_t *hash_lock = NULL;
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3306
	l2arc_buf_hdr_t *l2hdr;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3307
	uint64_t buf_size;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3308
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3309
	/*
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3310
	 * It would be nice to assert that if it's DMU metadata (level >
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3311
	 * 0 || it's the dnode file), then it must be syncing context.
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3312
	 * But we don't know that information at this level.
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3313
	 */
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3314
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3315
	mutex_enter(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3316
	hdr = buf->b_hdr;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3317
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3318
	/* this buffer is not on any list */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3319
	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3320
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3321
	if (hdr->b_state == arc_anon) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3322
		/* this buffer is already released */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3323
		ASSERT(buf->b_efunc == NULL);
9274
a10f8bd993c1 6790261 NULL pointer dereference in arc_cksum_equal()
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 9215
diff changeset
  3324
	} else {
a10f8bd993c1 6790261 NULL pointer dereference in arc_cksum_equal()
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 9215
diff changeset
  3325
		hash_lock = HDR_LOCK(hdr);
a10f8bd993c1 6790261 NULL pointer dereference in arc_cksum_equal()
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 9215
diff changeset
  3326
		mutex_enter(hash_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3327
		hdr = buf->b_hdr;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3328
		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3329
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3330
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3331
	l2hdr = hdr->b_l2hdr;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3332
	if (l2hdr) {
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3333
		mutex_enter(&l2arc_buflist_mtx);
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3334
		hdr->b_l2hdr = NULL;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3335
	}
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  3336
	buf_size = hdr->b_size;
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3337
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3338
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3339
	 * Do we have more than one buf?
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3340
	 */
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3341
	if (hdr->b_datacnt > 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3342
		arc_buf_hdr_t *nhdr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3343
		arc_buf_t **bufp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3344
		uint64_t blksz = hdr->b_size;
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  3345
		uint64_t spa = hdr->b_spa;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  3346
		arc_buf_contents_t type = hdr->b_type;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3347
		uint32_t flags = hdr->b_flags;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3348
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3349
		ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3350
		/*
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3351
		 * Pull the data off of this hdr and attach it to
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3352
		 * a new anonymous hdr.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3353
		 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3354
		(void) remove_reference(hdr, hash_lock, tag);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3355
		bufp = &hdr->b_buf;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3356
		while (*bufp != buf)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3357
			bufp = &(*bufp)->b_next;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3358
		*bufp = buf->b_next;
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3718
diff changeset
  3359
		buf->b_next = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3360
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3361
		ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3362
		atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3363
		if (refcount_is_zero(&hdr->b_refcnt)) {
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3364
			uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3365
			ASSERT3U(*size, >=, hdr->b_size);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3366
			atomic_add_64(size, -hdr->b_size);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3367
		}
13840
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3368
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3369
		/*
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3370
		 * We're releasing a duplicate user data buffer, update
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3371
		 * our statistics accordingly.
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3372
		 */
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3373
		if (hdr->b_type == ARC_BUFC_DATA) {
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3374
			ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3375
			ARCSTAT_INCR(arcstat_duplicate_buffers_size,
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3376
			    -hdr->b_size);
97fd5cdf328a 3145 single-copy arc
George Wilson <george.wilson@delphix.com>
parents: 13805
diff changeset
  3377
		}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3378
		hdr->b_datacnt -= 1;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3379
		arc_cksum_verify(buf);
13790
ac6eff781c67 3112 ztest does not honor ZFS_DEBUG
Matthew Ahrens <mahrens@delphix.com>
parents: 13765
diff changeset
  3380
		arc_buf_unwatch(buf);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3381
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3382
		mutex_exit(hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3383
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3384
		nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3385
		nhdr->b_size = blksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3386
		nhdr->b_spa = spa;
3290
256464cbb73c 4894692 caching data in heap inflates crash dump
johansen
parents: 3265
diff changeset
  3387
		nhdr->b_type = type;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3388
		nhdr->b_buf = buf;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3389
		nhdr->b_state = arc_anon;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3390
		nhdr->b_arc_access = 0;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3391
		nhdr->b_flags = flags & ARC_L2_WRITING;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3392
		nhdr->b_l2hdr = NULL;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3393
		nhdr->b_datacnt = 1;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3394
		nhdr->b_freeze_cksum = NULL;
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3718
diff changeset
  3395
		(void) refcount_add(&nhdr->b_refcnt, tag);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3396
		buf->b_hdr = nhdr;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3397
		mutex_exit(&buf->b_evict_lock);
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3398
		atomic_add_64(&arc_anon->arcs_size, blksz);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3399
	} else {
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3400
		mutex_exit(&buf->b_evict_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3401
		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3402
		ASSERT(!list_link_active(&hdr->b_arc_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3403
		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3404
		if (hdr->b_state != arc_anon)
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3405
			arc_change_state(arc_anon, hdr, hash_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3406
		hdr->b_arc_access = 0;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3407
		if (hash_lock)
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3408
			mutex_exit(hash_lock);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3409
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3410
		buf_discard_identity(hdr);
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3411
		arc_buf_thaw(buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3412
	}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3413
	buf->b_efunc = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3414
	buf->b_private = NULL;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3415
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3416
	if (l2hdr) {
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3417
		ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3418
		list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3419
		kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3420
		ARCSTAT_INCR(arcstat_l2_size, -buf_size);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3421
		mutex_exit(&l2arc_buflist_mtx);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3422
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3423
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3424
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3425
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3426
arc_released(arc_buf_t *buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3427
{
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3428
	int released;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3429
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3430
	mutex_enter(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3431
	released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3432
	mutex_exit(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3433
	return (released);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3434
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3435
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3436
int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3437
arc_has_callback(arc_buf_t *buf)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3438
{
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3439
	int callback;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3440
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3441
	mutex_enter(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3442
	callback = (buf->b_efunc != NULL);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3443
	mutex_exit(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3444
	return (callback);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3445
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3446
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3447
#ifdef ZFS_DEBUG
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3448
int
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3449
arc_referenced(arc_buf_t *buf)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3450
{
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3451
	int referenced;
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3452
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3453
	mutex_enter(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3454
	referenced = (refcount_count(&buf->b_hdr->b_refcnt));
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3455
	mutex_exit(&buf->b_evict_lock);
7545
d8ac8023a8e3 6737207 assertion failed: race between arc_release() and arc_referenced()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 7468
diff changeset
  3456
	return (referenced);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3457
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3458
#endif
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3459
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3460
static void
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3461
arc_write_ready(zio_t *zio)
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3462
{
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3463
	arc_write_callback_t *callback = zio->io_private;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3464
	arc_buf_t *buf = callback->awcb_buf;
5329
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3465
	arc_buf_hdr_t *hdr = buf->b_hdr;
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3466
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3467
	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3468
	callback->awcb_ready(zio, buf, callback->awcb_private);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3469
5329
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3470
	/*
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3471
	 * If the IO is already in progress, then this is a re-write
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3472
	 * attempt, so we need to thaw and re-compute the cksum.
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3473
	 * It is the responsibility of the callback to handle the
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3474
	 * accounting for any re-write attempt.
5329
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3475
	 */
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3476
	if (HDR_IO_IN_PROGRESS(hdr)) {
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3477
		mutex_enter(&hdr->b_freeze_lock);
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3478
		if (hdr->b_freeze_cksum != NULL) {
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3479
			kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3480
			hdr->b_freeze_cksum = NULL;
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3481
		}
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3482
		mutex_exit(&hdr->b_freeze_lock);
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3483
	}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3484
	arc_cksum_compute(buf, B_FALSE);
5329
33cb98223b2d PSARC 2007/567 zpool failmode property
gw25295
parents: 4831
diff changeset
  3485
	hdr->b_flags |= ARC_IO_IN_PROGRESS;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3486
}
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3487
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3488
/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3489
 * The SPA calls this callback for each physical write that happens on behalf
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3490
 * of a logical write.  See the comment in dbuf_write_physdone() for details.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3491
 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3492
static void
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3493
arc_write_physdone(zio_t *zio)
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3494
{
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3495
	arc_write_callback_t *cb = zio->io_private;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3496
	if (cb->awcb_physdone != NULL)
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3497
		cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3498
}
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3499
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3500
static void
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3501
arc_write_done(zio_t *zio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3502
{
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3503
	arc_write_callback_t *callback = zio->io_private;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3504
	arc_buf_t *buf = callback->awcb_buf;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3505
	arc_buf_hdr_t *hdr = buf->b_hdr;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3506
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3507
	ASSERT(hdr->b_acb == NULL);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3508
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3509
	if (zio->io_error == 0) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3510
		hdr->b_dva = *BP_IDENTITY(zio->io_bp);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3511
		hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3512
		hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3513
	} else {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3514
		ASSERT(BUF_EMPTY(hdr));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3515
	}
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3516
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3517
	/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3518
	 * If the block to be written was all-zero, we may have
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3519
	 * compressed it away.  In this case no write was performed
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3520
	 * so there will be no dva/birth/checksum.  The buffer must
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  3521
	 * therefore remain anonymous (and uncached).
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3522
	 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3523
	if (!BUF_EMPTY(hdr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3524
		arc_buf_hdr_t *exists;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3525
		kmutex_t *hash_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3526
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3527
		ASSERT(zio->io_error == 0);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3528
3093
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  3529
		arc_cksum_verify(buf);
71525e4187d5 6490569 verify arc bufs are not modified when they shouldn't be
ahrens
parents: 2918
diff changeset
  3530
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3531
		exists = buf_hash_insert(hdr, &hash_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3532
		if (exists) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3533
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3534
			 * This can only happen if we overwrite for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3535
			 * sync-to-convergence, because we remove
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3536
			 * buffers from the hash table when we arc_free().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3537
			 */
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3538
			if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3539
				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3540
					panic("bad overwrite, hdr=%p exists=%p",
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3541
					    (void *)hdr, (void *)exists);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3542
				ASSERT(refcount_is_zero(&exists->b_refcnt));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3543
				arc_change_state(arc_anon, exists, hash_lock);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3544
				mutex_exit(hash_lock);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3545
				arc_hdr_destroy(exists);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3546
				exists = buf_hash_insert(hdr, &hash_lock);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3547
				ASSERT3P(exists, ==, NULL);
13887
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3548
			} else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3549
				/* nopwrite */
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3550
				ASSERT(zio->io_prop.zp_nopwrite);
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3551
				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3552
					panic("bad nopwrite, hdr=%p exists=%p",
196932ec9e6a 3236 zio nop-write
George Wilson <george.wilson@delphix.com>
parents: 13840
diff changeset
  3553
					    (void *)hdr, (void *)exists);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3554
			} else {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3555
				/* Dedup */
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3556
				ASSERT(hdr->b_datacnt == 1);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3557
				ASSERT(hdr->b_state == arc_anon);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3558
				ASSERT(BP_GET_DEDUP(zio->io_bp));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3559
				ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
10272
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 9816
diff changeset
  3560
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3561
		}
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3562
		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  3563
		/* if it's not anon, we are doing a scrub */
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3564
		if (!exists && hdr->b_state == arc_anon)
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  3565
			arc_access(hdr, hash_lock);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3566
		mutex_exit(hash_lock);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3567
	} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3568
		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3569
	}
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3570
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3571
	ASSERT(!refcount_is_zero(&hdr->b_refcnt));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3572
	callback->awcb_done(zio, buf, callback->awcb_private);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3573
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3574
	kmem_free(callback, sizeof (arc_write_callback_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3575
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3576
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3577
zio_t *
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3578
arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3579
    blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress,
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3580
    const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone,
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3581
    arc_done_func_t *done, void *private, zio_priority_t priority,
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3582
    int zio_flags, const zbookmark_t *zb)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3583
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3584
	arc_buf_hdr_t *hdr = buf->b_hdr;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3585
	arc_write_callback_t *callback;
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3586
	zio_t *zio;
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3587
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  3588
	ASSERT(ready != NULL);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3589
	ASSERT(done != NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3590
	ASSERT(!HDR_IO_ERROR(hdr));
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2082
diff changeset
  3591
	ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3592
	ASSERT(hdr->b_acb == NULL);
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3593
	if (l2arc)
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7046
diff changeset
  3594
		hdr->b_flags |= ARC_L2CACHE;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3595
	if (l2arc_compress)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3596
		hdr->b_flags |= ARC_L2COMPRESS;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3597
	callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3598
	callback->awcb_ready = ready;
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3599
	callback->awcb_physdone = physdone;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3600
	callback->awcb_done = done;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3601
	callback->awcb_private = private;
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3602
	callback->awcb_buf = buf;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6987
diff changeset
  3603
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  3604
	zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3605
	    arc_write_ready, arc_write_physdone, arc_write_done, callback,
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3606
	    priority, zio_flags, zb);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3607
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3403
diff changeset
  3608
	return (zio);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3609
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3610
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3611
static int
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3612
arc_memory_throttle(uint64_t reserve, uint64_t txg)
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3613
{
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3614
#ifdef _KERNEL
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3615
	uint64_t available_memory = ptob(freemem);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3616
	static uint64_t page_load = 0;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3617
	static uint64_t last_txg = 0;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3618
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3619
#if defined(__i386)
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3620
	available_memory =
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3621
	    MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3622
#endif
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3623
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3624
	if (freemem > physmem * arc_lotsfree_percent / 100)
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3625
		return (0);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3626
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3627
	if (txg > last_txg) {
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3628
		last_txg = txg;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3629
		page_load = 0;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3630
	}
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3631
	/*
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3632
	 * If we are in pageout, we know that memory is already tight,
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3633
	 * the arc is already going to be evicting, so we just want to
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3634
	 * continue to let page writes occur as quickly as possible.
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3635
	 */
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3636
	if (curproc == proc_pageout) {
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3637
		if (page_load > MAX(ptob(minfree), available_memory) / 4)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  3638
			return (SET_ERROR(ERESTART));
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3639
		/* Note: reserve is inflated, so we deflate */
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3640
		page_load += reserve / 8;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3641
		return (0);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3642
	} else if (page_load > 0 && arc_reclaim_needed()) {
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3643
		/* memory is low, delay before restarting */
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3644
		ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  3645
		return (SET_ERROR(EAGAIN));
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3646
	}
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3647
	page_load = 0;
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3648
#endif
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3649
	return (0);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3650
}
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3651
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3652
void
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3653
arc_tempreserve_clear(uint64_t reserve)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3654
{
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3655
	atomic_add_64(&arc_tempreserve, -reserve);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3656
	ASSERT((int64_t)arc_tempreserve >= 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3657
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3658
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3659
int
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3660
arc_tempreserve_space(uint64_t reserve, uint64_t txg)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3661
{
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3662
	int error;
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3663
	uint64_t anon_size;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3664
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3665
	if (reserve > arc_c/4 && !arc_no_grow)
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3666
		arc_c = MIN(arc_c_max, reserve * 4);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3667
	if (reserve > arc_c)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  3668
		return (SET_ERROR(ENOMEM));
982
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3669
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3670
	/*
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3671
	 * Don't count loaned bufs as in flight dirty data to prevent long
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3672
	 * network delays from blocking transactions that are ready to be
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3673
	 * assigned to a txg.
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3674
	 */
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3675
	anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3676
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3677
	/*
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3678
	 * Writes will, almost always, require additional memory allocations
14046
6a5a2f4a47db 3742 zfs comments need cleaner, more consistent style
Will Andrews <will@firepipe.net>
parents: 14045
diff changeset
  3679
	 * in order to compress/encrypt/etc the data.  We therefore need to
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3680
	 * make sure that there is sufficient available memory for this.
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3681
	 */
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3682
	error = arc_memory_throttle(reserve, txg);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3683
	if (error != 0)
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3684
		return (error);
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3685
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3686
	/*
982
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3687
	 * Throttle writes when the amount of dirty data in the cache
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3688
	 * gets too large.  We try to keep the cache less than half full
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3689
	 * of dirty blocks so that our sync times don't grow too large.
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3690
	 * Note: if two requests come in concurrently, we might let them
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3691
	 * both succeed, when one of them should fail.  Not a huge deal.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3692
	 */
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3693
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3694
	if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3695
	    anon_size > arc_c / 4) {
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3696
		dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3697
		    "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3698
		    arc_tempreserve>>10,
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3699
		    arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3700
		    arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3701
		    reserve>>10, arc_c>>10);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  3702
		return (SET_ERROR(ERESTART));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3703
	}
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3704
	atomic_add_64(&arc_tempreserve, reserve);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3705
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3706
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3707
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3708
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3709
arc_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3710
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3711
	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3712
	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3713
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3714
	/* Convert seconds to clock ticks */
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2391
diff changeset
  3715
	arc_min_prefetch_lifespan = 1 * hz;
2391
2fa3fd1db808 6447377 ZFS prefetch is inconsistant
maybee
parents: 2237
diff changeset
  3716
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3717
	/* Start out with 1/8 of all memory */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3718
	arc_c = physmem * PAGESIZE / 8;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3719
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3720
#ifdef _KERNEL
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3721
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3722
	 * On architectures where the physical memory can be larger
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3723
	 * than the addressable space (intel in 32-bit mode), we may
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3724
	 * need to limit the cache to 1/8 of VM size.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3725
	 */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3726
	arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3727
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3728
982
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3729
	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3730
	arc_c_min = MAX(arc_c / 4, 64<<20);
982
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 789
diff changeset
  3731
	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3732
	if (arc_c * 8 >= 1<<30)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3733
		arc_c_max = (arc_c * 8) - (1<<30);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3734
	else
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3735
		arc_c_max = arc_c_min;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3736
	arc_c_max = MAX(arc_c * 6, arc_c_max);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3737
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3738
	/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3739
	 * Allow the tunables to override our calculations if they are
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3740
	 * reasonable (ie. over 64MB)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3741
	 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3742
	if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3743
		arc_c_max = zfs_arc_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3744
	if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3745
		arc_c_min = zfs_arc_min;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3746
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3747
	arc_c = arc_c_max;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3748
	arc_p = (arc_c >> 1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3749
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3750
	/* limit meta-data to 1/4 of the arc capacity */
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3751
	arc_meta_limit = arc_c_max / 4;
4645
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
  3752
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
  3753
	/* Allow the tunable to override if it is reasonable */
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
  3754
	if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
  3755
		arc_meta_limit = zfs_arc_meta_limit;
5725b2efaa7f 6578493 arc_meta_limit should be configurable
ek110237
parents: 4309
diff changeset
  3756
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3757
	if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3758
		arc_c_min = arc_meta_limit / 2;
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3759
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3760
	if (zfs_arc_grow_retry > 0)
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3761
		arc_grow_retry = zfs_arc_grow_retry;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3762
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3763
	if (zfs_arc_shrink_shift > 0)
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3764
		arc_shrink_shift = zfs_arc_shrink_shift;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3765
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3766
	if (zfs_arc_p_min_shift > 0)
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3767
		arc_p_min_shift = zfs_arc_p_min_shift;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  3768
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3769
	/* if kmem_flags are set, lets try to use less memory */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3770
	if (kmem_debugging())
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3771
		arc_c = arc_c / 2;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3772
	if (arc_c < arc_c_min)
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3773
		arc_c = arc_c_min;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3774
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3775
	arc_anon = &ARC_anon;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3776
	arc_mru = &ARC_mru;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3777
	arc_mru_ghost = &ARC_mru_ghost;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3778
	arc_mfu = &ARC_mfu;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3779
	arc_mfu_ghost = &ARC_mfu_ghost;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3780
	arc_l2c_only = &ARC_l2c_only;
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3781
	arc_size = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3782
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3783
	mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3784
	mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3785
	mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3786
	mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3787
	mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3788
	mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3789
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3790
	list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3791
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3792
	list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3793
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3794
	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3795
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3796
	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3797
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3798
	list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3799
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3800
	list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3801
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3802
	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3803
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3804
	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3805
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3806
	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3807
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3808
	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3809
	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3810
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3811
	buf_init();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3812
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3813
	arc_thread_exit = 0;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3814
	arc_eviction_list = NULL;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3815
	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
2887
eef7119b31c5 6476663 bad trap possible when dereferencing b_hdr after buf eviction
maybee
parents: 2885
diff changeset
  3816
	bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3817
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3818
	arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3819
	    sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3820
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3821
	if (arc_ksp != NULL) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3822
		arc_ksp->ks_data = &arc_stats;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3823
		kstat_install(arc_ksp);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3824
	}
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3825
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3826
	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3827
	    TS_RUN, minclsyspri);
3158
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  3828
f30a3849aa23 6493923 nfsfind on ZFS filesystem quickly depletes memory in a 1GB system
maybee
parents: 3093
diff changeset
  3829
	arc_dead = FALSE;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3830
	arc_warm = B_FALSE;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  3831
14167
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3832
	/*
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3833
	 * Calculate maximum amount of dirty data per pool.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3834
	 *
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3835
	 * If it has been set by /etc/system, take that.
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3836
	 * Otherwise, use a percentage of physical memory defined by
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3837
	 * zfs_dirty_data_max_percent (default 10%) with a cap at
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3838
	 * zfs_dirty_data_max_max (default 4GB).
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3839
	 */
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3840
	if (zfs_dirty_data_max == 0) {
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3841
		zfs_dirty_data_max = physmem * PAGESIZE *
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3842
		    zfs_dirty_data_max_percent / 100;
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3843
		zfs_dirty_data_max = MIN(zfs_dirty_data_max,
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3844
		    zfs_dirty_data_max_max);
dceb17481b99 4045 zfs write throttle & i/o scheduler performance work
Matthew Ahrens <mahrens@delphix.com>
parents: 14046
diff changeset
  3845
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3846
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3847
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3848
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3849
arc_fini(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3850
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3851
	mutex_enter(&arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3852
	arc_thread_exit = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3853
	while (arc_thread_exit != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3854
		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3855
	mutex_exit(&arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3856
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5450
diff changeset
  3857
	arc_flush(NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3858
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3859
	arc_dead = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3860
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3861
	if (arc_ksp != NULL) {
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3862
		kstat_delete(arc_ksp);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3863
		arc_ksp = NULL;
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3864
	}
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3865
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1505
diff changeset
  3866
	mutex_destroy(&arc_eviction_mtx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3867
	mutex_destroy(&arc_reclaim_thr_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3868
	cv_destroy(&arc_reclaim_thr_cv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3869
4309
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3870
	list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3871
	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3872
	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3873
	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3874
	list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3875
	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3876
	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3dfde0f4662d 6542676 ARC needs to track meta-data memory overhead
maybee
parents: 3897
diff changeset
  3877
	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3878
3403
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3879
	mutex_destroy(&arc_anon->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3880
	mutex_destroy(&arc_mru->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3881
	mutex_destroy(&arc_mru_ghost->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3882
	mutex_destroy(&arc_mfu->arcs_mtx);
e52013d23622 6510807 ARC statistics should be exported via kstat
bmc
parents: 3312
diff changeset
  3883
	mutex_destroy(&arc_mfu_ghost->arcs_mtx);
8214
d7abf7c1f1c1 6747934 Some locking variables are not properly initialized or destroyed
Ricardo M. Correia <Ricardo.M.Correia@Sun.COM>
parents: 8213
diff changeset
  3884
	mutex_destroy(&arc_l2c_only->arcs_mtx);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2724
diff changeset
  3885
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3886
	buf_fini();
9412
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3887
4aefd8704ce0 6717022 ZFS DMU needs zero-copy support
Aleksandr Guzovskiy <Aleksandr.Guzovskiy@Sun.COM>
parents: 9396
diff changeset
  3888
	ASSERT(arc_loaned_bytes == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3889
}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3890
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3891
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3892
 * Level 2 ARC
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3893
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3894
 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3895
 * It uses dedicated storage devices to hold cached data, which are populated
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3896
 * using large infrequent writes.  The main role of this cache is to boost
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3897
 * the performance of random read workloads.  The intended L2ARC devices
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3898
 * include short-stroked disks, solid state disks, and other media with
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3899
 * substantially faster read latency than disk.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3900
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3901
 *                 +-----------------------+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3902
 *                 |         ARC           |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3903
 *                 +-----------------------+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3904
 *                    |         ^     ^
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3905
 *                    |         |     |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3906
 *      l2arc_feed_thread()    arc_read()
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3907
 *                    |         |     |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3908
 *                    |  l2arc read   |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3909
 *                    V         |     |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3910
 *               +---------------+    |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3911
 *               |     L2ARC     |    |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3912
 *               +---------------+    |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3913
 *                   |    ^           |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3914
 *          l2arc_write() |           |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3915
 *                   |    |           |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3916
 *                   V    |           |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3917
 *                 +-------+      +-------+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3918
 *                 | vdev  |      | vdev  |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3919
 *                 | cache |      | cache |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3920
 *                 +-------+      +-------+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3921
 *                 +=========+     .-----.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3922
 *                 :  L2ARC  :    |-_____-|
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3923
 *                 : devices :    | Disks |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3924
 *                 +=========+    `-_____-'
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3925
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3926
 * Read requests are satisfied from the following sources, in order:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3927
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3928
 *	1) ARC
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3929
 *	2) vdev cache of L2ARC devices
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3930
 *	3) L2ARC devices
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3931
 *	4) vdev cache of disks
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3932
 *	5) disks
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3933
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3934
 * Some L2ARC device types exhibit extremely slow write performance.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3935
 * To accommodate for this there are some significant differences between
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3936
 * the L2ARC and traditional cache design:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3937
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3938
 * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3939
 * the ARC behave as usual, freeing buffers and placing headers on ghost
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3940
 * lists.  The ARC does not send buffers to the L2ARC during eviction as
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3941
 * this would add inflated write latencies for all ARC memory pressure.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3942
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3943
 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3944
 * It does this by periodically scanning buffers from the eviction-end of
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3945
 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3946
 * not already there. It scans until a headroom of buffers is satisfied,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3947
 * which itself is a buffer for ARC eviction. If a compressible buffer is
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3948
 * found during scanning and selected for writing to an L2ARC device, we
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3949
 * temporarily boost scanning headroom during the next scan cycle to make
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3950
 * sure we adapt to compression effects (which might significantly reduce
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  3951
 * the data volume we write to L2ARC). The thread that does this is
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3952
 * l2arc_feed_thread(), illustrated below; example sizes are included to
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3953
 * provide a better sense of ratio than this diagram:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3954
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3955
 *	       head -->                        tail
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3956
 *	        +---------------------+----------+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3957
 *	ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3958
 *	        +---------------------+----------+   |   o L2ARC eligible
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3959
 *	ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3960
 *	        +---------------------+----------+   |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3961
 *	             15.9 Gbytes      ^ 32 Mbytes    |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3962
 *	                           headroom          |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3963
 *	                                      l2arc_feed_thread()
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3964
 *	                                             |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3965
 *	                 l2arc write hand <--[oooo]--'
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3966
 *	                         |           8 Mbyte
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3967
 *	                         |          write max
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3968
 *	                         V
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3969
 *		  +==============================+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3970
 *	L2ARC dev |####|#|###|###|    |####| ... |
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3971
 *	          +==============================+
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3972
 *	                     32 Gbytes
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3973
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3974
 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3975
 * evicted, then the L2ARC has cached a buffer much sooner than it probably
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3976
 * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3977
 * safe to say that this is an uncommon case, since buffers at the end of
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3978
 * the ARC lists have moved there due to inactivity.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3979
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3980
 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3981
 * then the L2ARC simply misses copying some buffers.  This serves as a
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3982
 * pressure valve to prevent heavy read workloads from both stalling the ARC
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3983
 * with waits and clogging the L2ARC with writes.  This also helps prevent
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3984
 * the potential for the L2ARC to churn if it attempts to cache content too
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3985
 * quickly, such as during backups of the entire pool.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3986
 *
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3987
 * 5. After system boot and before the ARC has filled main memory, there are
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3988
 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3989
 * lists can remain mostly static.  Instead of searching from tail of these
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3990
 * lists as pictured, the l2arc_feed_thread() will search from the list heads
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3991
 * for eligible buffers, greatly increasing its chance of finding them.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3992
 *
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3993
 * The L2ARC device write speed is also boosted during this time so that
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3994
 * the L2ARC warms up faster.  Since there have been no ARC evictions yet,
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3995
 * there are no L2ARC reads, and no fear of degrading read performance
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3996
 * through increased writes.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3997
 *
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  3998
 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  3999
 * the vdev queue can aggregate them into larger and fewer writes.  Each
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4000
 * device is written to in a rotor fashion, sweeping writes through
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4001
 * available space then repeating.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4002
 *
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4003
 * 7. The L2ARC does not store dirty content.  It never needs to flush
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4004
 * write buffers back to disk based storage.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4005
 *
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4006
 * 8. If an ARC buffer is written (and dirtied) which also exists in the
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4007
 * L2ARC, the now stale L2ARC buffer is immediately dropped.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4008
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4009
 * The performance of the L2ARC can be tweaked by a number of tunables, which
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4010
 * may be necessary for different workloads:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4011
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4012
 *	l2arc_write_max		max write bytes per interval
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4013
 *	l2arc_write_boost	extra write bytes during device warmup
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4014
 *	l2arc_noprefetch	skip caching prefetched buffers
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4015
 *	l2arc_headroom		number of max device writes to precache
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4016
 *	l2arc_headroom_boost	when we find compressed buffers during ARC
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4017
 *				scanning, we multiply headroom by this
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4018
 *				percentage factor for the next scan cycle,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4019
 *				since more compressed buffers are likely to
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4020
 *				be present
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4021
 *	l2arc_feed_secs		seconds between L2ARC writing
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4022
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4023
 * Tunables may be removed or added as future performance improvements are
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4024
 * integrated, and also may become zpool properties.
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4025
 *
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4026
 * There are three key functions that control how the L2ARC warms up:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4027
 *
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4028
 *	l2arc_write_eligible()	check if a buffer is eligible to cache
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4029
 *	l2arc_write_size()	calculate how much to write
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4030
 *	l2arc_write_interval()	calculate sleep delay between writes
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4031
 *
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4032
 * These three functions determine what to write, how much, and how quickly
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4033
 * to send writes.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4034
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4035
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4036
static boolean_t
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  4037
l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4038
{
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4039
	/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4040
	 * A buffer is *not* eligible for the L2ARC if it:
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4041
	 * 1. belongs to a different spa.
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  4042
	 * 2. is already cached on the L2ARC.
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  4043
	 * 3. has an I/O in progress (it may be an incomplete read).
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  4044
	 * 4. is flagged not eligible (zfs property).
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4045
	 */
10357
29060492b29d 6871680 kstats for ARC evicted bytes by L2ARC state
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 10272
diff changeset
  4046
	if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4047
	    HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4048
		return (B_FALSE);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4049
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4050
	return (B_TRUE);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4051
}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4052
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4053
static uint64_t
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4054
l2arc_write_size(void)
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4055
{
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4056
	uint64_t size;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4057
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4058
	/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4059
	 * Make sure our globals have meaningful values in case the user
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4060
	 * altered them.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4061
	 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4062
	size = l2arc_write_max;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4063
	if (size == 0) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4064
		cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must "
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4065
		    "be greater than zero, resetting it to the default (%d)",
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4066
		    L2ARC_WRITE_SIZE);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4067
		size = l2arc_write_max = L2ARC_WRITE_SIZE;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4068
	}
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4069
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4070
	if (arc_warm == B_FALSE)
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4071
		size += l2arc_write_boost;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4072
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4073
	return (size);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4074
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4075
}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4076
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4077
static clock_t
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4078
l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4079
{
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4080
	clock_t interval, next, now;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4081
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4082
	/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4083
	 * If the ARC lists are busy, increase our write rate; if the
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4084
	 * lists are stale, idle back.  This is achieved by checking
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4085
	 * how much we previously wrote - if it was more than half of
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4086
	 * what we wanted, schedule the next write much sooner.
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4087
	 */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4088
	if (l2arc_feed_again && wrote > (wanted / 2))
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4089
		interval = (hz * l2arc_feed_min_ms) / 1000;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4090
	else
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4091
		interval = hz * l2arc_feed_secs;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4092
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4093
	now = ddi_get_lbolt();
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4094
	next = MAX(now, MIN(now + interval, began + interval));
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4095
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4096
	return (next);
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4097
}
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4098
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4099
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4100
l2arc_hdr_stat_add(void)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4101
{
6018
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
  4102
	ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
  4103
	ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4104
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4105
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4106
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4107
l2arc_hdr_stat_remove(void)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4108
{
6018
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
  4109
	ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
109fdf2d7ad7 6628084 Incorrect error message when trying to upgrade a v10 pool
brendan
parents: 5642
diff changeset
  4110
	ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4111
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4112
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4113
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4114
 * Cycle through L2ARC devices.  This is how L2ARC load balances.
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4115
 * If a device is returned, this also returns holding the spa config lock.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4116
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4117
static l2arc_dev_t *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4118
l2arc_dev_get_next(void)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4119
{
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4120
	l2arc_dev_t *first, *next = NULL;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4121
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4122
	/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4123
	 * Lock out the removal of spas (spa_namespace_lock), then removal
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4124
	 * of cache devices (l2arc_dev_mtx).  Once a device has been selected,
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4125
	 * both locks will be dropped and a spa config lock held instead.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4126
	 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4127
	mutex_enter(&spa_namespace_lock);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4128
	mutex_enter(&l2arc_dev_mtx);
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4129
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4130
	/* if there are no vdevs, there is nothing to do */
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4131
	if (l2arc_ndev == 0)
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4132
		goto out;
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4133
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4134
	first = NULL;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4135
	next = l2arc_dev_last;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4136
	do {
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4137
		/* loop around the list looking for a non-faulted vdev */
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4138
		if (next == NULL) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4139
			next = list_head(l2arc_dev_list);
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4140
		} else {
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4141
			next = list_next(l2arc_dev_list, next);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4142
			if (next == NULL)
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4143
				next = list_head(l2arc_dev_list);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4144
		}
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4145
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4146
		/* if we have come back to the start, bail out */
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4147
		if (first == NULL)
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4148
			first = next;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4149
		else if (next == first)
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4150
			break;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4151
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4152
	} while (vdev_is_dead(next->l2ad_vdev));
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4153
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4154
	/* if we were unable to find any usable vdevs, return NULL */
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4155
	if (vdev_is_dead(next->l2ad_vdev))
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4156
		next = NULL;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4157
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4158
	l2arc_dev_last = next;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4159
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4160
out:
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4161
	mutex_exit(&l2arc_dev_mtx);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4162
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4163
	/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4164
	 * Grab the config lock to prevent the 'next' device from being
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4165
	 * removed while we are writing to it.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4166
	 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4167
	if (next != NULL)
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4168
		spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4169
	mutex_exit(&spa_namespace_lock);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4170
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4171
	return (next);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4172
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4173
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4174
/*
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4175
 * Free buffers that were tagged for destruction.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4176
 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4177
static void
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4178
l2arc_do_free_on_write()
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4179
{
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4180
	list_t *buflist;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4181
	l2arc_data_free_t *df, *df_prev;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4182
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4183
	mutex_enter(&l2arc_free_on_write_mtx);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4184
	buflist = l2arc_free_on_write;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4185
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4186
	for (df = list_tail(buflist); df; df = df_prev) {
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4187
		df_prev = list_prev(buflist, df);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4188
		ASSERT(df->l2df_data != NULL);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4189
		ASSERT(df->l2df_func != NULL);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4190
		df->l2df_func(df->l2df_data, df->l2df_size);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4191
		list_remove(buflist, df);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4192
		kmem_free(df, sizeof (l2arc_data_free_t));
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4193
	}
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4194
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4195
	mutex_exit(&l2arc_free_on_write_mtx);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4196
}
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4197
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4198
/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4199
 * A write to a cache device has completed.  Update all headers to allow
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4200
 * reads from these buffers to begin.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4201
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4202
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4203
l2arc_write_done(zio_t *zio)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4204
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4205
	l2arc_write_callback_t *cb;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4206
	l2arc_dev_t *dev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4207
	list_t *buflist;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4208
	arc_buf_hdr_t *head, *ab, *ab_prev;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4209
	l2arc_buf_hdr_t *abl2;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4210
	kmutex_t *hash_lock;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4211
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4212
	cb = zio->io_private;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4213
	ASSERT(cb != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4214
	dev = cb->l2wcb_dev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4215
	ASSERT(dev != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4216
	head = cb->l2wcb_head;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4217
	ASSERT(head != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4218
	buflist = dev->l2ad_buflist;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4219
	ASSERT(buflist != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4220
	DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4221
	    l2arc_write_callback_t *, cb);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4222
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4223
	if (zio->io_error != 0)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4224
		ARCSTAT_BUMP(arcstat_l2_writes_error);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4225
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4226
	mutex_enter(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4227
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4228
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4229
	 * All writes completed, or an error was hit.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4230
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4231
	for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4232
		ab_prev = list_prev(buflist, ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4233
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4234
		hash_lock = HDR_LOCK(ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4235
		if (!mutex_tryenter(hash_lock)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4236
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4237
			 * This buffer misses out.  It may be in a stage
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4238
			 * of eviction.  Its ARC_L2_WRITING flag will be
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4239
			 * left set, denying reads to this buffer.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4240
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4241
			ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4242
			continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4243
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4244
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4245
		abl2 = ab->b_l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4246
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4247
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4248
		 * Release the temporary compressed buffer as soon as possible.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4249
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4250
		if (abl2->b_compress != ZIO_COMPRESS_OFF)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4251
			l2arc_release_cdata_buf(ab);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4252
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4253
		if (zio->io_error != 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4254
			/*
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4255
			 * Error - drop L2ARC entry.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4256
			 */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4257
			list_remove(buflist, ab);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4258
			ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4259
			ab->b_l2hdr = NULL;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4260
			kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4261
			ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4262
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4263
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4264
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4265
		 * Allow ARC to begin reads to this L2ARC entry.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4266
		 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4267
		ab->b_flags &= ~ARC_L2_WRITING;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4268
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4269
		mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4270
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4271
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4272
	atomic_inc_64(&l2arc_writes_done);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4273
	list_remove(buflist, head);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4274
	kmem_cache_free(hdr_cache, head);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4275
	mutex_exit(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4276
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4277
	l2arc_do_free_on_write();
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4278
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4279
	kmem_free(cb, sizeof (l2arc_write_callback_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4280
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4281
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4282
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4283
 * A read to a cache device completed.  Validate buffer contents before
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4284
 * handing over to the regular ARC routines.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4285
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4286
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4287
l2arc_read_done(zio_t *zio)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4288
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4289
	l2arc_read_callback_t *cb;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4290
	arc_buf_hdr_t *hdr;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4291
	arc_buf_t *buf;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4292
	kmutex_t *hash_lock;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4293
	int equal;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4294
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4295
	ASSERT(zio->io_vd != NULL);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4296
	ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4297
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4298
	spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4299
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4300
	cb = zio->io_private;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4301
	ASSERT(cb != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4302
	buf = cb->l2rcb_buf;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4303
	ASSERT(buf != NULL);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  4304
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  4305
	hash_lock = HDR_LOCK(buf->b_hdr);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  4306
	mutex_enter(hash_lock);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4307
	hdr = buf->b_hdr;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12033
diff changeset
  4308
	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4309
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4310
	/*
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4311
	 * If the buffer was compressed, decompress it first.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4312
	 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4313
	if (cb->l2rcb_compress != ZIO_COMPRESS_OFF)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4314
		l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4315
	ASSERT(zio->io_data != NULL);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4316
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4317
	/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4318
	 * Check this survived the L2ARC journey.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4319
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4320
	equal = arc_cksum_equal(buf);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4321
	if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4322
		mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4323
		zio->io_private = buf;
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4324
		zio->io_bp_copy = cb->l2rcb_bp;	/* XXX fix in L2ARC 2.0	*/
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4325
		zio->io_bp = &zio->io_bp_copy;	/* XXX fix in L2ARC 2.0	*/
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4326
		arc_read_done(zio);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4327
	} else {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4328
		mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4329
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4330
		 * Buffer didn't survive caching.  Increment stats and
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4331
		 * reissue to the original storage device.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4332
		 */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4333
		if (zio->io_error != 0) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4334
			ARCSTAT_BUMP(arcstat_l2_io_error);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4335
		} else {
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  4336
			zio->io_error = SET_ERROR(EIO);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4337
		}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4338
		if (!equal)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4339
			ARCSTAT_BUMP(arcstat_l2_cksum_bad);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4340
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4341
		/*
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4342
		 * If there's no waiter, issue an async i/o to the primary
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4343
		 * storage now.  If there *is* a waiter, the caller must
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4344
		 * issue the i/o in a context where it's OK to block.
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4345
		 */
8632
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4346
		if (zio->io_waiter == NULL) {
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4347
			zio_t *pio = zio_unique_parent(zio);
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4348
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4349
			ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4350
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4351
			zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4352
			    buf->b_data, zio->io_size, arc_read_done, buf,
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4353
			    zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
8632
36ef517870a3 6798384 It can take a village to raise a zio
Bill Moore <Bill.Moore@Sun.COM>
parents: 8628
diff changeset
  4354
		}
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4355
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4356
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4357
	kmem_free(cb, sizeof (l2arc_read_callback_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4358
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4359
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4360
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4361
 * This is the list priority from which the L2ARC will search for pages to
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4362
 * cache.  This is used within loops (0..3) to cycle through lists in the
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4363
 * desired order.  This order can have a significant effect on cache
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4364
 * performance.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4365
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4366
 * Currently the metadata lists are hit first, MFU then MRU, followed by
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4367
 * the data lists.  This function returns a locked list, and also returns
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4368
 * the lock pointer.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4369
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4370
static list_t *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4371
l2arc_list_locked(int list_num, kmutex_t **lock)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4372
{
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
  4373
	list_t *list = NULL;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4374
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4375
	ASSERT(list_num >= 0 && list_num <= 3);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4376
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4377
	switch (list_num) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4378
	case 0:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4379
		list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4380
		*lock = &arc_mfu->arcs_mtx;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4381
		break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4382
	case 1:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4383
		list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4384
		*lock = &arc_mru->arcs_mtx;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4385
		break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4386
	case 2:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4387
		list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4388
		*lock = &arc_mfu->arcs_mtx;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4389
		break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4390
	case 3:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4391
		list = &arc_mru->arcs_list[ARC_BUFC_DATA];
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4392
		*lock = &arc_mru->arcs_mtx;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4393
		break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4394
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4395
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4396
	ASSERT(!(MUTEX_HELD(*lock)));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4397
	mutex_enter(*lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4398
	return (list);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4399
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4400
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4401
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4402
 * Evict buffers from the device write hand to the distance specified in
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4403
 * bytes.  This distance may span populated buffers, it may span nothing.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4404
 * This is clearing a region on the L2ARC device ready for writing.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4405
 * If the 'all' boolean is set, every buffer is evicted.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4406
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4407
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4408
l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4409
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4410
	list_t *buflist;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4411
	l2arc_buf_hdr_t *abl2;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4412
	arc_buf_hdr_t *ab, *ab_prev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4413
	kmutex_t *hash_lock;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4414
	uint64_t taddr;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4415
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4416
	buflist = dev->l2ad_buflist;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4417
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4418
	if (buflist == NULL)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4419
		return;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4420
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4421
	if (!all && dev->l2ad_first) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4422
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4423
		 * This is the first sweep through the device.  There is
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4424
		 * nothing to evict.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4425
		 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4426
		return;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4427
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4428
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4429
	if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4430
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4431
		 * When nearing the end of the device, evict to the end
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4432
		 * before the device write hand jumps to the start.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4433
		 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4434
		taddr = dev->l2ad_end;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4435
	} else {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4436
		taddr = dev->l2ad_hand + distance;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4437
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4438
	DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4439
	    uint64_t, taddr, boolean_t, all);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4440
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4441
top:
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4442
	mutex_enter(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4443
	for (ab = list_tail(buflist); ab; ab = ab_prev) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4444
		ab_prev = list_prev(buflist, ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4445
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4446
		hash_lock = HDR_LOCK(ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4447
		if (!mutex_tryenter(hash_lock)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4448
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4449
			 * Missed the hash lock.  Retry.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4450
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4451
			ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4452
			mutex_exit(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4453
			mutex_enter(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4454
			mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4455
			goto top;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4456
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4457
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4458
		if (HDR_L2_WRITE_HEAD(ab)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4459
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4460
			 * We hit a write head node.  Leave it for
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4461
			 * l2arc_write_done().
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4462
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4463
			list_remove(buflist, ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4464
			mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4465
			continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4466
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4467
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4468
		if (!all && ab->b_l2hdr != NULL &&
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4469
		    (ab->b_l2hdr->b_daddr > taddr ||
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4470
		    ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4471
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4472
			 * We've evicted to the target address,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4473
			 * or the end of the device.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4474
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4475
			mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4476
			break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4477
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4478
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4479
		if (HDR_FREE_IN_PROGRESS(ab)) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4480
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4481
			 * Already on the path to destruction.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4482
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4483
			mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4484
			continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4485
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4486
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4487
		if (ab->b_state == arc_l2c_only) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4488
			ASSERT(!HDR_L2_READING(ab));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4489
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4490
			 * This doesn't exist in the ARC.  Destroy.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4491
			 * arc_hdr_destroy() will call list_remove()
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4492
			 * and decrement arcstat_l2_size.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4493
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4494
			arc_change_state(arc_anon, ab, hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4495
			arc_hdr_destroy(ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4496
		} else {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4497
			/*
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4498
			 * Invalidate issued or about to be issued
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4499
			 * reads, since we may be about to write
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4500
			 * over this location.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4501
			 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4502
			if (HDR_L2_READING(ab)) {
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4503
				ARCSTAT_BUMP(arcstat_l2_evict_reading);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4504
				ab->b_flags |= ARC_L2_EVICTED;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4505
			}
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4506
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4507
			/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4508
			 * Tell ARC this no longer exists in L2ARC.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4509
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4510
			if (ab->b_l2hdr != NULL) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4511
				abl2 = ab->b_l2hdr;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4512
				ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4513
				ab->b_l2hdr = NULL;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4514
				kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4515
				ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4516
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4517
			list_remove(buflist, ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4518
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4519
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4520
			 * This may have been leftover after a
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4521
			 * failed write.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4522
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4523
			ab->b_flags &= ~ARC_L2_WRITING;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4524
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4525
		mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4526
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4527
	mutex_exit(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4528
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  4529
	vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4530
	dev->l2ad_evict = taddr;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4531
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4532
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4533
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4534
 * Find and write ARC buffers to the L2ARC device.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4535
 *
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4536
 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4537
 * for reading until they have completed writing.
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4538
 * The headroom_boost is an in-out parameter used to maintain headroom boost
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4539
 * state between calls to this function.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4540
 *
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4541
 * Returns the number of bytes actually written (which may be smaller than
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4542
 * the delta by which the device hand has changed due to alignment).
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4543
 */
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4544
static uint64_t
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4545
l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4546
    boolean_t *headroom_boost)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4547
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4548
	arc_buf_hdr_t *ab, *ab_prev, *head;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4549
	list_t *list;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4550
	uint64_t write_asize, write_psize, write_sz, headroom,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4551
	    buf_compress_minsz;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4552
	void *buf_data;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4553
	kmutex_t *list_lock;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4554
	boolean_t full;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4555
	l2arc_write_callback_t *cb;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4556
	zio_t *pio, *wzio;
13514
417c34452f03 1748 desire support for reguid in zfs
Garrett D'Amore <garrett@nexenta.com>
parents: 13061
diff changeset
  4557
	uint64_t guid = spa_load_guid(spa);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4558
	const boolean_t do_headroom_boost = *headroom_boost;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4559
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4560
	ASSERT(dev->l2ad_vdev != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4561
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4562
	/* Lower the flag now, we might want to raise it again later. */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4563
	*headroom_boost = B_FALSE;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4564
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4565
	pio = NULL;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4566
	write_sz = write_asize = write_psize = 0;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4567
	full = B_FALSE;
6245
1a2a7cfb9f26 6429205 each zpool needs to monitor its throughput and throttle heavy writers
maybee
parents: 6018
diff changeset
  4568
	head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4569
	head->b_flags |= ARC_L2_WRITE_HEAD;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4570
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4571
	/*
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4572
	 * We will want to try to compress buffers that are at least 2x the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4573
	 * device sector size.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4574
	 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4575
	buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4576
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4577
	/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4578
	 * Copy buffers for L2ARC writing.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4579
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4580
	mutex_enter(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4581
	for (int try = 0; try <= 3; try++) {
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4582
		uint64_t passed_sz = 0;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4583
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4584
		list = l2arc_list_locked(try, &list_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4585
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4586
		/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4587
		 * L2ARC fast warmup.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4588
		 *
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4589
		 * Until the ARC is warm and starts to evict, read from the
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4590
		 * head of the ARC lists rather than the tail.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4591
		 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4592
		if (arc_warm == B_FALSE)
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4593
			ab = list_head(list);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4594
		else
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4595
			ab = list_tail(list);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4596
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4597
		headroom = target_sz * l2arc_headroom;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4598
		if (do_headroom_boost)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4599
			headroom = (headroom * l2arc_headroom_boost) / 100;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4600
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4601
		for (; ab; ab = ab_prev) {
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4602
			l2arc_buf_hdr_t *l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4603
			kmutex_t *hash_lock;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4604
			uint64_t buf_sz;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4605
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4606
			if (arc_warm == B_FALSE)
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4607
				ab_prev = list_next(list, ab);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4608
			else
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4609
				ab_prev = list_prev(list, ab);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4610
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4611
			hash_lock = HDR_LOCK(ab);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4612
			if (!mutex_tryenter(hash_lock)) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4613
				/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4614
				 * Skip this buffer rather than waiting.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4615
				 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4616
				continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4617
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4618
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4619
			passed_sz += ab->b_size;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4620
			if (passed_sz > headroom) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4621
				/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4622
				 * Searched too far.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4623
				 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4624
				mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4625
				break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4626
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4627
8636
7e4ce9158df3 6551866 deadlock between zfs_write(), zfs_freesp(), and zfs_putapage()
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 8632
diff changeset
  4628
			if (!l2arc_write_eligible(guid, ab)) {
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4629
				mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4630
				continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4631
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4632
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4633
			if ((write_sz + ab->b_size) > target_sz) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4634
				full = B_TRUE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4635
				mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4636
				break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4637
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4638
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4639
			if (pio == NULL) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4640
				/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4641
				 * Insert a dummy header on the buflist so
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4642
				 * l2arc_write_done() can find where the
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4643
				 * write buffers begin without searching.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4644
				 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4645
				list_insert_head(dev->l2ad_buflist, head);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4646
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4647
				cb = kmem_alloc(
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4648
				    sizeof (l2arc_write_callback_t), KM_SLEEP);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4649
				cb->l2wcb_dev = dev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4650
				cb->l2wcb_head = head;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4651
				pio = zio_root(spa, l2arc_write_done, cb,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4652
				    ZIO_FLAG_CANFAIL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4653
			}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4654
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4655
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4656
			 * Create and add a new L2ARC header.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4657
			 */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4658
			l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4659
			l2hdr->b_dev = dev;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4660
			ab->b_flags |= ARC_L2_WRITING;
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4661
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4662
			/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4663
			 * Temporarily stash the data buffer in b_tmp_cdata.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4664
			 * The subsequent write step will pick it up from
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4665
			 * there. This is because can't access ab->b_buf
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4666
			 * without holding the hash_lock, which we in turn
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4667
			 * can't access without holding the ARC list locks
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4668
			 * (which we want to avoid during compression/writing).
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4669
			 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4670
			l2hdr->b_compress = ZIO_COMPRESS_OFF;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4671
			l2hdr->b_asize = ab->b_size;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4672
			l2hdr->b_tmp_cdata = ab->b_buf->b_data;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4673
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4674
			buf_sz = ab->b_size;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4675
			ab->b_l2hdr = l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4676
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4677
			list_insert_head(dev->l2ad_buflist, ab);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4678
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4679
			/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4680
			 * Compute and store the buffer cksum before
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4681
			 * writing.  On debug the cksum is verified first.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4682
			 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4683
			arc_cksum_verify(ab->b_buf);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4684
			arc_cksum_compute(ab->b_buf, B_TRUE);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4685
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4686
			mutex_exit(hash_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4687
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4688
			write_sz += buf_sz;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4689
		}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4690
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4691
		mutex_exit(list_lock);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4692
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4693
		if (full == B_TRUE)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4694
			break;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4695
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4696
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4697
	/* No buffers selected for writing? */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4698
	if (pio == NULL) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4699
		ASSERT0(write_sz);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4700
		mutex_exit(&l2arc_buflist_mtx);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4701
		kmem_cache_free(hdr_cache, head);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4702
		return (0);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4703
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4704
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4705
	/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4706
	 * Now start writing the buffers. We're starting at the write head
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4707
	 * and work backwards, retracing the course of the buffer selector
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4708
	 * loop above.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4709
	 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4710
	for (ab = list_prev(dev->l2ad_buflist, head); ab;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4711
	    ab = list_prev(dev->l2ad_buflist, ab)) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4712
		l2arc_buf_hdr_t *l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4713
		uint64_t buf_sz;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4714
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4715
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4716
		 * We shouldn't need to lock the buffer here, since we flagged
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4717
		 * it as ARC_L2_WRITING in the previous step, but we must take
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4718
		 * care to only access its L2 cache parameters. In particular,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4719
		 * ab->b_buf may be invalid by now due to ARC eviction.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4720
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4721
		l2hdr = ab->b_l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4722
		l2hdr->b_daddr = dev->l2ad_hand;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4723
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4724
		if ((ab->b_flags & ARC_L2COMPRESS) &&
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4725
		    l2hdr->b_asize >= buf_compress_minsz) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4726
			if (l2arc_compress_buf(l2hdr)) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4727
				/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4728
				 * If compression succeeded, enable headroom
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4729
				 * boost on the next scan cycle.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4730
				 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4731
				*headroom_boost = B_TRUE;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4732
			}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4733
		}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4734
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4735
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4736
		 * Pick up the buffer data we had previously stashed away
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4737
		 * (and now potentially also compressed).
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4738
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4739
		buf_data = l2hdr->b_tmp_cdata;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4740
		buf_sz = l2hdr->b_asize;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4741
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4742
		/* Compression may have squashed the buffer to zero length. */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4743
		if (buf_sz != 0) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4744
			uint64_t buf_p_sz;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4745
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4746
			wzio = zio_write_phys(pio, dev->l2ad_vdev,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4747
			    dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4748
			    NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4749
			    ZIO_FLAG_CANFAIL, B_FALSE);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4750
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4751
			DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4752
			    zio_t *, wzio);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4753
			(void) zio_nowait(wzio);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4754
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4755
			write_asize += buf_sz;
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4756
			/*
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4757
			 * Keep the clock hand suitably device-aligned.
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  4758
			 */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4759
			buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4760
			write_psize += buf_p_sz;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4761
			dev->l2ad_hand += buf_p_sz;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4762
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4763
	}
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4764
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4765
	mutex_exit(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4766
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4767
	ASSERT3U(write_asize, <=, target_sz);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4768
	ARCSTAT_BUMP(arcstat_l2_writes_sent);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4769
	ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4770
	ARCSTAT_INCR(arcstat_l2_size, write_sz);
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4771
	ARCSTAT_INCR(arcstat_l2_asize, write_asize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4772
	vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4773
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4774
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4775
	 * Bump device hand to the device start if it is approaching the end.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4776
	 * l2arc_evict() will already have evicted ahead for this case.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4777
	 */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4778
	if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  4779
		vdev_space_update(dev->l2ad_vdev,
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  4780
		    dev->l2ad_end - dev->l2ad_hand, 0, 0);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4781
		dev->l2ad_hand = dev->l2ad_start;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4782
		dev->l2ad_evict = dev->l2ad_start;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4783
		dev->l2ad_first = B_FALSE;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4784
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4785
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4786
	dev->l2ad_writing = B_TRUE;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4787
	(void) zio_wait(pio);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4788
	dev->l2ad_writing = B_FALSE;
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4789
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4790
	return (write_asize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4791
}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4792
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4793
/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4794
 * Compresses an L2ARC buffer.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4795
 * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4796
 * size in l2hdr->b_asize. This routine tries to compress the data and
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4797
 * depending on the compression result there are three possible outcomes:
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4798
 * *) The buffer was incompressible. The original l2hdr contents were left
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4799
 *    untouched and are ready for writing to an L2 device.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4800
 * *) The buffer was all-zeros, so there is no need to write it to an L2
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4801
 *    device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4802
 *    set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4803
 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4804
 *    data buffer which holds the compressed data to be written, and b_asize
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4805
 *    tells us how much data there is. b_compress is set to the appropriate
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4806
 *    compression algorithm. Once writing is done, invoke
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4807
 *    l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4808
 *
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4809
 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4810
 * buffer was incompressible).
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4811
 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4812
static boolean_t
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4813
l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4814
{
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4815
	void *cdata;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4816
	size_t csize, len;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4817
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4818
	ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4819
	ASSERT(l2hdr->b_tmp_cdata != NULL);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4820
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4821
	len = l2hdr->b_asize;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4822
	cdata = zio_data_buf_alloc(len);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4823
	csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4824
	    cdata, l2hdr->b_asize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4825
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4826
	if (csize == 0) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4827
		/* zero block, indicate that there's nothing to write */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4828
		zio_data_buf_free(cdata, len);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4829
		l2hdr->b_compress = ZIO_COMPRESS_EMPTY;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4830
		l2hdr->b_asize = 0;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4831
		l2hdr->b_tmp_cdata = NULL;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4832
		ARCSTAT_BUMP(arcstat_l2_compress_zeros);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4833
		return (B_TRUE);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4834
	} else if (csize > 0 && csize < len) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4835
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4836
		 * Compression succeeded, we'll keep the cdata around for
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4837
		 * writing and release it afterwards.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4838
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4839
		l2hdr->b_compress = ZIO_COMPRESS_LZ4;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4840
		l2hdr->b_asize = csize;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4841
		l2hdr->b_tmp_cdata = cdata;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4842
		ARCSTAT_BUMP(arcstat_l2_compress_successes);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4843
		return (B_TRUE);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4844
	} else {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4845
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4846
		 * Compression failed, release the compressed buffer.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4847
		 * l2hdr will be left unmodified.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4848
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4849
		zio_data_buf_free(cdata, len);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4850
		ARCSTAT_BUMP(arcstat_l2_compress_failures);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4851
		return (B_FALSE);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4852
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4853
}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4854
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4855
/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4856
 * Decompresses a zio read back from an l2arc device. On success, the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4857
 * underlying zio's io_data buffer is overwritten by the uncompressed
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4858
 * version. On decompression error (corrupt compressed stream), the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4859
 * zio->io_error value is set to signal an I/O error.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4860
 *
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4861
 * Please note that the compressed data stream is not checksummed, so
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4862
 * if the underlying device is experiencing data corruption, we may feed
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4863
 * corrupt data to the decompressor, so the decompressor needs to be
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4864
 * able to handle this situation (LZ4 does).
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4865
 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4866
static void
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4867
l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4868
{
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4869
	ASSERT(L2ARC_IS_VALID_COMPRESS(c));
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4870
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4871
	if (zio->io_error != 0) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4872
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4873
		 * An io error has occured, just restore the original io
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4874
		 * size in preparation for a main pool read.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4875
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4876
		zio->io_orig_size = zio->io_size = hdr->b_size;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4877
		return;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4878
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4879
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4880
	if (c == ZIO_COMPRESS_EMPTY) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4881
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4882
		 * An empty buffer results in a null zio, which means we
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4883
		 * need to fill its io_data after we're done restoring the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4884
		 * buffer's contents.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4885
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4886
		ASSERT(hdr->b_buf != NULL);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4887
		bzero(hdr->b_buf->b_data, hdr->b_size);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4888
		zio->io_data = zio->io_orig_data = hdr->b_buf->b_data;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4889
	} else {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4890
		ASSERT(zio->io_data != NULL);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4891
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4892
		 * We copy the compressed data from the start of the arc buffer
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4893
		 * (the zio_read will have pulled in only what we need, the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4894
		 * rest is garbage which we will overwrite at decompression)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4895
		 * and then decompress back to the ARC data buffer. This way we
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4896
		 * can minimize copying by simply decompressing back over the
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4897
		 * original compressed data (rather than decompressing to an
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4898
		 * aux buffer and then copying back the uncompressed buffer,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4899
		 * which is likely to be much larger).
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4900
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4901
		uint64_t csize;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4902
		void *cdata;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4903
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4904
		csize = zio->io_size;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4905
		cdata = zio_data_buf_alloc(csize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4906
		bcopy(zio->io_data, cdata, csize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4907
		if (zio_decompress_data(c, cdata, zio->io_data, csize,
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4908
		    hdr->b_size) != 0)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4909
			zio->io_error = EIO;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4910
		zio_data_buf_free(cdata, csize);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4911
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4912
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4913
	/* Restore the expected uncompressed IO size. */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4914
	zio->io_orig_size = zio->io_size = hdr->b_size;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4915
}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4916
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4917
/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4918
 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4919
 * This buffer serves as a temporary holder of compressed data while
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4920
 * the buffer entry is being written to an l2arc device. Once that is
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4921
 * done, we can dispose of it.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4922
 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4923
static void
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4924
l2arc_release_cdata_buf(arc_buf_hdr_t *ab)
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4925
{
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4926
	l2arc_buf_hdr_t *l2hdr = ab->b_l2hdr;
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4927
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4928
	if (l2hdr->b_compress == ZIO_COMPRESS_LZ4) {
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4929
		/*
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4930
		 * If the data was compressed, then we've allocated a
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4931
		 * temporary buffer for it, so now we need to release it.
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4932
		 */
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4933
		ASSERT(l2hdr->b_tmp_cdata != NULL);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4934
		zio_data_buf_free(l2hdr->b_tmp_cdata, ab->b_size);
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4935
	}
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4936
	l2hdr->b_tmp_cdata = NULL;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4937
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4938
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4939
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4940
 * This thread feeds the L2ARC at regular intervals.  This is the beating
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4941
 * heart of the L2ARC.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4942
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4943
static void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4944
l2arc_feed_thread(void)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4945
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4946
	callb_cpr_t cpr;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4947
	l2arc_dev_t *dev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4948
	spa_t *spa;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4949
	uint64_t size, wrote;
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4950
	clock_t begin, next = ddi_get_lbolt();
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  4951
	boolean_t headroom_boost = B_FALSE;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4952
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4953
	CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4954
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4955
	mutex_enter(&l2arc_feed_thr_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4956
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4957
	while (l2arc_thread_exit == 0) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4958
		CALLB_CPR_SAFE_BEGIN(&cpr);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4959
		(void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  4960
		    next);
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4961
		CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4962
		next = ddi_get_lbolt() + hz;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4963
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4964
		/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4965
		 * Quick check for L2ARC devices.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4966
		 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4967
		mutex_enter(&l2arc_dev_mtx);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4968
		if (l2arc_ndev == 0) {
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4969
			mutex_exit(&l2arc_dev_mtx);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4970
			continue;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4971
		}
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4972
		mutex_exit(&l2arc_dev_mtx);
11066
cebb50cbe4f9 PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling
Rafael Vanoni <rafael.vanoni@sun.com>
parents: 10922
diff changeset
  4973
		begin = ddi_get_lbolt();
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4974
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4975
		/*
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4976
		 * This selects the next l2arc device to write to, and in
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  4977
		 * doing so the next spa to feed from: dev->l2ad_spa.   This
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4978
		 * will return NULL if there are now no l2arc devices or if
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4979
		 * they are all faulted.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4980
		 *
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4981
		 * If a device is returned, its spa's config lock is also
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4982
		 * held to prevent device removal.  l2arc_dev_get_next()
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4983
		 * will grab and release l2arc_dev_mtx.
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4984
		 */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4985
		if ((dev = l2arc_dev_get_next()) == NULL)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4986
			continue;
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4988
		spa = dev->l2ad_spa;
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  4989
		ASSERT(spa != NULL);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4990
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  4991
		/*
13061
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4992
		 * If the pool is read-only then force the feed thread to
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4993
		 * sleep a little longer.
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4994
		 */
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4995
		if (!spa_writeable(spa)) {
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4996
			next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4997
			spa_config_exit(spa, SCL_L2ARC, dev);
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4998
			continue;
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  4999
		}
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  5000
bda0decf867b PSARC 2010/306 Read-only ZFS pools
George Wilson <George.Wilson@Sun.COM>
parents: 12674
diff changeset
  5001
		/*
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5002
		 * Avoid contributing to memory pressure.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5003
		 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5004
		if (arc_reclaim_needed()) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5005
			ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5006
			spa_config_exit(spa, SCL_L2ARC, dev);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5007
			continue;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5008
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5009
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5010
		ARCSTAT_BUMP(arcstat_l2_feeds);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5011
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  5012
		size = l2arc_write_size();
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5013
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5014
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5015
		 * Evict L2ARC buffers that will be overwritten.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5016
		 */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5017
		l2arc_evict(dev, size, B_FALSE);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5018
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5019
		/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5020
		 * Write ARC buffers.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5021
		 */
14039
6cfd5a5778b9 3137 L2ARC compression
Saso Kiselkov <skiselkov@gmail.com>
parents: 13980
diff changeset
  5022
		wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost);
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5023
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5024
		/*
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5025
		 * Calculate interval between writes.
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5026
		 */
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5027
		next = l2arc_write_interval(begin, size, wrote);
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5028
		spa_config_exit(spa, SCL_L2ARC, dev);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5029
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5030
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5031
	l2arc_thread_exit = 0;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5032
	cv_broadcast(&l2arc_feed_thr_cv);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5033
	CALLB_CPR_EXIT(&cpr);		/* drops l2arc_feed_thr_lock */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5034
	thread_exit();
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5035
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5036
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5037
boolean_t
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5038
l2arc_vdev_present(vdev_t *vd)
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5039
{
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5040
	l2arc_dev_t *dev;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5041
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5042
	mutex_enter(&l2arc_dev_mtx);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5043
	for (dev = list_head(l2arc_dev_list); dev != NULL;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5044
	    dev = list_next(l2arc_dev_list, dev)) {
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5045
		if (dev->l2ad_vdev == vd)
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5046
			break;
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5047
	}
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5048
	mutex_exit(&l2arc_dev_mtx);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5049
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5050
	return (dev != NULL);
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5051
}
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5052
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5053
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5054
 * Add a vdev for use by the L2ARC.  By this point the spa has already
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5055
 * validated the vdev and opened it.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5056
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5057
void
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9412
diff changeset
  5058
l2arc_add_vdev(spa_t *spa, vdev_t *vd)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5059
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5060
	l2arc_dev_t *adddev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5061
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5062
	ASSERT(!l2arc_vdev_present(vd));
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6245
diff changeset
  5063
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5064
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5065
	 * Create a new l2arc device entry.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5066
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5067
	adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5068
	adddev->l2ad_spa = spa;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5069
	adddev->l2ad_vdev = vd;
9816
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9412
diff changeset
  5070
	adddev->l2ad_start = VDEV_LABEL_START_SIZE;
847676ec1c5b PSARC 2008/353 zpool autoexpand property
George Wilson <George.Wilson@Sun.COM>
parents: 9412
diff changeset
  5071
	adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5072
	adddev->l2ad_hand = adddev->l2ad_start;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5073
	adddev->l2ad_evict = adddev->l2ad_start;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5074
	adddev->l2ad_first = B_TRUE;
8582
df9361868dbe 6748019 ARC/L2ARC metadata accounting for arc_size
Brendan Gregg - Sun Microsystems <Brendan.Gregg@Sun.COM>
parents: 8241
diff changeset
  5075
	adddev->l2ad_writing = B_FALSE;
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5076
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5077
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5078
	 * This is a list of all ARC buffers that are still valid on the
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5079
	 * device.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5080
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5081
	adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5082
	list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5083
	    offsetof(arc_buf_hdr_t, b_l2node));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5084
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10839
diff changeset
  5085
	vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5086
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5087
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5088
	 * Add device to global list
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5089
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5090
	mutex_enter(&l2arc_dev_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5091
	list_insert_head(l2arc_dev_list, adddev);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5092
	atomic_inc_64(&l2arc_ndev);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5093
	mutex_exit(&l2arc_dev_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5094
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5095
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5096
/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5097
 * Remove a vdev from the L2ARC.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5098
 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5099
void
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5100
l2arc_remove_vdev(vdev_t *vd)
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5101
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5102
	l2arc_dev_t *dev, *nextdev, *remdev = NULL;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5103
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5104
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5105
	 * Find the device by vdev
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5106
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5107
	mutex_enter(&l2arc_dev_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5108
	for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5109
		nextdev = list_next(l2arc_dev_list, dev);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5110
		if (vd == dev->l2ad_vdev) {
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5111
			remdev = dev;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5112
			break;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5113
		}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5114
	}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5115
	ASSERT(remdev != NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5116
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5117
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5118
	 * Remove device from global list
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5119
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5120
	list_remove(l2arc_dev_list, remdev);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5121
	l2arc_dev_last = NULL;		/* may have been invalidated */
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5122
	atomic_dec_64(&l2arc_ndev);
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5123
	mutex_exit(&l2arc_dev_mtx);
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5124
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5125
	/*
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5126
	 * Clear all buflists and ARC references.  L2ARC device flush.
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5127
	 */
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5128
	l2arc_evict(remdev, 0, B_TRUE);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5129
	list_destroy(remdev->l2ad_buflist);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5130
	kmem_free(remdev->l2ad_buflist, sizeof (list_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5131
	kmem_free(remdev, sizeof (l2arc_dev_t));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5132
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5133
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5134
void
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5135
l2arc_init(void)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5136
{
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5137
	l2arc_thread_exit = 0;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5138
	l2arc_ndev = 0;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5139
	l2arc_writes_sent = 0;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5140
	l2arc_writes_done = 0;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5141
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5142
	mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5143
	cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5144
	mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5145
	mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5146
	mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5147
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5148
	l2arc_dev_list = &L2ARC_dev_list;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5149
	l2arc_free_on_write = &L2ARC_free_on_write;
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5150
	list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5151
	    offsetof(l2arc_dev_t, l2ad_node));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5152
	list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5153
	    offsetof(l2arc_data_free_t, l2df_list_node));
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5154
}
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5155
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5156
void
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5157
l2arc_fini(void)
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5158
{
6987
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5159
	/*
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5160
	 * This is called from dmu_fini(), which is called from spa_fini();
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5161
	 * Because of this, we can assume that all l2arc devices have
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5162
	 * already been removed when the pools themselves were removed.
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5163
	 */
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5164
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5165
	l2arc_do_free_on_write();
877c018eb06c 6628282 arc_stats.arcstat_l2_writes_sent.value.ui64) == (arc_stats.arcstat_l2_writes_done.value.ui64)
brendan
parents: 6643
diff changeset
  5166
5450
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5167
	mutex_destroy(&l2arc_feed_thr_lock);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5168
	cv_destroy(&l2arc_feed_thr_cv);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5169
	mutex_destroy(&l2arc_dev_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5170
	mutex_destroy(&l2arc_buflist_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5171
	mutex_destroy(&l2arc_free_on_write_mtx);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5172
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5173
	list_destroy(l2arc_dev_list);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5174
	list_destroy(l2arc_free_on_write);
b25030891c44 PSARC 2007/618 ZFS L2ARC
brendan
parents: 5329
diff changeset
  5175
}
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5176
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5177
void
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5178
l2arc_start(void)
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5179
{
8241
5a60f16123ba 6328632 zpool offline is a bit too conservative
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 8214
diff changeset
  5180
	if (!(spa_mode_global & FWRITE))
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5181
		return;
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5182
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5183
	(void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5184
	    TS_RUN, minclsyspri);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5185
}
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5186
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5187
void
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5188
l2arc_stop(void)
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5189
{
8241
5a60f16123ba 6328632 zpool offline is a bit too conservative
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 8214
diff changeset
  5190
	if (!(spa_mode_global & FWRITE))
7754
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5191
		return;
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5192
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5193
	mutex_enter(&l2arc_feed_thr_lock);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5194
	cv_signal(&l2arc_feed_thr_cv);	/* kick thread out of startup */
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5195
	l2arc_thread_exit = 1;
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5196
	while (l2arc_thread_exit != 0)
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5197
		cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5198
	mutex_exit(&l2arc_feed_thr_lock);
b80e4842ad54 6754011 SPA 3.0: lock breakup, i/o pipeline refactoring, device failure handling
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 7545
diff changeset
  5199
}