1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 5*1472Sperrin * Common Development and Distribution License (the "License"). 6*1472Sperrin * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221362Sperrin * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/dmu.h> 31789Sahrens #include <sys/zap.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/stat.h> 34789Sahrens #include <sys/resource.h> 35789Sahrens #include <sys/zil.h> 36789Sahrens #include <sys/zil_impl.h> 37789Sahrens #include <sys/dsl_dataset.h> 38789Sahrens #include <sys/vdev.h> 39789Sahrens 40789Sahrens /* 41789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 42789Sahrens * that change the file system in memory with enough information 43789Sahrens * to be able to replay them. These are stored in memory until 44789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 45789Sahrens * and they can be discarded, or they are flushed to the stable log 46789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 47789Sahrens * requirement. In the event of a panic or power fail then those log 48789Sahrens * records (transactions) are replayed. 49789Sahrens * 50789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 51789Sahrens * of 3 parts: 52789Sahrens * 53789Sahrens * - ZIL header 54789Sahrens * - ZIL blocks 55789Sahrens * - ZIL records 56789Sahrens * 57789Sahrens * A log record holds a system call transaction. Log blocks can 58789Sahrens * hold many log records and the blocks are chained together. 59789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 60789Sahrens * ZIL block in the chain. The ZIL header points to the first 61789Sahrens * block in the chain. Note there is not a fixed place in the pool 62789Sahrens * to hold blocks. They are dynamically allocated and freed as 63789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 64789Sahrens */ 65789Sahrens 66789Sahrens /* 67789Sahrens * These global ZIL switches affect all pools 68789Sahrens */ 69789Sahrens int zil_disable = 0; /* disable intent logging */ 70789Sahrens int zil_always = 0; /* make every transaction synchronous */ 71789Sahrens int zil_purge = 0; /* at pool open, just throw everything away */ 72789Sahrens int zil_noflush = 0; /* don't flush write cache buffers on disks */ 73789Sahrens 74789Sahrens static kmem_cache_t *zil_lwb_cache; 75789Sahrens 76789Sahrens static int 77789Sahrens zil_dva_compare(const void *x1, const void *x2) 78789Sahrens { 79789Sahrens const dva_t *dva1 = x1; 80789Sahrens const dva_t *dva2 = x2; 81789Sahrens 82789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 83789Sahrens return (-1); 84789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 85789Sahrens return (1); 86789Sahrens 87789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 88789Sahrens return (-1); 89789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 90789Sahrens return (1); 91789Sahrens 92789Sahrens return (0); 93789Sahrens } 94789Sahrens 95789Sahrens static void 96789Sahrens zil_dva_tree_init(avl_tree_t *t) 97789Sahrens { 98789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 99789Sahrens offsetof(zil_dva_node_t, zn_node)); 100789Sahrens } 101789Sahrens 102789Sahrens static void 103789Sahrens zil_dva_tree_fini(avl_tree_t *t) 104789Sahrens { 105789Sahrens zil_dva_node_t *zn; 106789Sahrens void *cookie = NULL; 107789Sahrens 108789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 109789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 110789Sahrens 111789Sahrens avl_destroy(t); 112789Sahrens } 113789Sahrens 114789Sahrens static int 115789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 116789Sahrens { 117789Sahrens zil_dva_node_t *zn; 118789Sahrens avl_index_t where; 119789Sahrens 120789Sahrens if (avl_find(t, dva, &where) != NULL) 121789Sahrens return (EEXIST); 122789Sahrens 123789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 124789Sahrens zn->zn_dva = *dva; 125789Sahrens avl_insert(t, zn, where); 126789Sahrens 127789Sahrens return (0); 128789Sahrens } 129789Sahrens 130789Sahrens /* 131789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 132789Sahrens */ 133789Sahrens static int 134789Sahrens zil_read_log_block(zilog_t *zilog, blkptr_t *bp, char *buf) 135789Sahrens { 136789Sahrens uint64_t blksz = BP_GET_LSIZE(bp); 137789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(buf + blksz) - 1; 138789Sahrens zio_cksum_t cksum; 139789Sahrens int error; 140789Sahrens 141789Sahrens error = zio_wait(zio_read(NULL, zilog->zl_spa, bp, buf, blksz, 142789Sahrens NULL, NULL, ZIO_PRIORITY_SYNC_READ, 143789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE)); 144789Sahrens if (error) { 145789Sahrens dprintf_bp(bp, "zilog %p bp %p read failed, error %d: ", 146789Sahrens zilog, bp, error); 147789Sahrens return (error); 148789Sahrens } 149789Sahrens 150789Sahrens if (BP_SHOULD_BYTESWAP(bp)) 151789Sahrens byteswap_uint64_array(buf, blksz); 152789Sahrens 153789Sahrens /* 154789Sahrens * Sequence numbers should be... sequential. The checksum verifier for 155789Sahrens * the next block should be: <logid[0], logid[1], objset id, seq + 1>. 156789Sahrens */ 157789Sahrens cksum = bp->blk_cksum; 158789Sahrens cksum.zc_word[3]++; 159789Sahrens if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum)) != 0) { 160789Sahrens dprintf_bp(bp, "zilog %p bp %p stale pointer: ", zilog, bp); 161789Sahrens return (ESTALE); 162789Sahrens } 163789Sahrens 164789Sahrens if (BP_IS_HOLE(&ztp->zit_next_blk)) { 165789Sahrens dprintf_bp(bp, "zilog %p bp %p hole: ", zilog, bp); 166789Sahrens return (ENOENT); 167789Sahrens } 168789Sahrens 169789Sahrens if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) { 170789Sahrens dprintf("zilog %p bp %p nused exceeds blksz\n", zilog, bp); 171789Sahrens return (EOVERFLOW); 172789Sahrens } 173789Sahrens 174789Sahrens dprintf_bp(bp, "zilog %p bp %p good block: ", zilog, bp); 175789Sahrens 176789Sahrens return (0); 177789Sahrens } 178789Sahrens 179789Sahrens /* 180789Sahrens * Parse the intent log, and call parse_func for each valid record within. 181789Sahrens */ 182789Sahrens void 183789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 184789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 185789Sahrens { 186789Sahrens blkptr_t blk; 187789Sahrens char *lrbuf, *lrp; 188789Sahrens zil_trailer_t *ztp; 189789Sahrens int reclen, error; 190789Sahrens 191789Sahrens blk = zilog->zl_header->zh_log; 192789Sahrens if (BP_IS_HOLE(&blk)) 193789Sahrens return; 194789Sahrens 195789Sahrens /* 196789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 197789Sahrens * For each block in the chain we strongly check that block to 198789Sahrens * ensure its validity. We stop when an invalid block is found. 199789Sahrens * For each block pointer in the chain we call parse_blk_func(). 200789Sahrens * For each record in each valid block we call parse_lr_func(). 201789Sahrens */ 202789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 203789Sahrens lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); 204789Sahrens for (;;) { 205789Sahrens error = zil_read_log_block(zilog, &blk, lrbuf); 206789Sahrens 207789Sahrens if (parse_blk_func != NULL) 208789Sahrens parse_blk_func(zilog, &blk, arg, txg); 209789Sahrens 210789Sahrens if (error) 211789Sahrens break; 212789Sahrens 213789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 214789Sahrens blk = ztp->zit_next_blk; 215789Sahrens 216789Sahrens if (parse_lr_func == NULL) 217789Sahrens continue; 218789Sahrens 219789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 220789Sahrens lr_t *lr = (lr_t *)lrp; 221789Sahrens reclen = lr->lrc_reclen; 222789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 223789Sahrens parse_lr_func(zilog, lr, arg, txg); 224789Sahrens } 225789Sahrens } 226789Sahrens zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); 227789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 228789Sahrens } 229789Sahrens 230789Sahrens /* ARGSUSED */ 231789Sahrens static void 232789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 233789Sahrens { 234789Sahrens spa_t *spa = zilog->zl_spa; 235789Sahrens int err; 236789Sahrens 237789Sahrens dprintf_bp(bp, "first_txg %llu: ", first_txg); 238789Sahrens 239789Sahrens /* 240789Sahrens * Claim log block if not already committed and not already claimed. 241789Sahrens */ 242789Sahrens if (bp->blk_birth >= first_txg && 243789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 244789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 245789Sahrens ASSERT(err == 0); 246789Sahrens } 247789Sahrens } 248789Sahrens 249789Sahrens static void 250789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 251789Sahrens { 252789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 253789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 254789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 255789Sahrens } 256789Sahrens } 257789Sahrens 258789Sahrens /* ARGSUSED */ 259789Sahrens static void 260789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 261789Sahrens { 262789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 263789Sahrens } 264789Sahrens 265789Sahrens static void 266789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 267789Sahrens { 268789Sahrens /* 269789Sahrens * If we previously claimed it, we need to free it. 270789Sahrens */ 271789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 272789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 273789Sahrens blkptr_t *bp = &lr->lr_blkptr; 274789Sahrens if (bp->blk_birth >= claim_txg && 275789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 276789Sahrens (void) arc_free(NULL, zilog->zl_spa, 277789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 278789Sahrens } 279789Sahrens } 280789Sahrens } 281789Sahrens 282789Sahrens /* 283789Sahrens * Create an on-disk intent log. 284789Sahrens */ 285789Sahrens static void 286789Sahrens zil_create(zilog_t *zilog) 287789Sahrens { 288789Sahrens lwb_t *lwb; 289789Sahrens uint64_t txg; 290789Sahrens dmu_tx_t *tx; 291789Sahrens blkptr_t blk; 292789Sahrens int error; 2931362Sperrin int no_blk; 294789Sahrens 295789Sahrens ASSERT(zilog->zl_header->zh_claim_txg == 0); 296789Sahrens ASSERT(zilog->zl_header->zh_replay_seq == 0); 297789Sahrens 298789Sahrens /* 299789Sahrens * Initialize the log header block. 300789Sahrens */ 301789Sahrens tx = dmu_tx_create(zilog->zl_os); 302789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 303789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 304789Sahrens txg = dmu_tx_get_txg(tx); 305789Sahrens 306789Sahrens /* 3071362Sperrin * If we don't have a log block already then 3081362Sperrin * allocate the first log block and assign its checksum verifier. 309789Sahrens */ 3101362Sperrin no_blk = BP_IS_HOLE(&zilog->zl_header->zh_log); 3111362Sperrin if (no_blk) { 3121362Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 3131362Sperrin ZIL_MIN_BLKSZ, &blk, txg); 3141362Sperrin } else { 3151362Sperrin blk = zilog->zl_header->zh_log; 3161362Sperrin error = 0; 3171362Sperrin } 318789Sahrens if (error == 0) { 319789Sahrens ZIO_SET_CHECKSUM(&blk.blk_cksum, 320789Sahrens spa_get_random(-1ULL), spa_get_random(-1ULL), 321789Sahrens dmu_objset_id(zilog->zl_os), 1ULL); 322789Sahrens 323789Sahrens /* 324789Sahrens * Allocate a log write buffer (lwb) for the first log block. 325789Sahrens */ 326789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 327789Sahrens lwb->lwb_zilog = zilog; 328789Sahrens lwb->lwb_blk = blk; 329789Sahrens lwb->lwb_nused = 0; 330789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 331789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 332789Sahrens lwb->lwb_max_txg = txg; 333789Sahrens lwb->lwb_seq = 0; 334789Sahrens lwb->lwb_state = UNWRITTEN; 335789Sahrens mutex_enter(&zilog->zl_lock); 336789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 337789Sahrens mutex_exit(&zilog->zl_lock); 338789Sahrens } 339789Sahrens 340789Sahrens dmu_tx_commit(tx); 3411362Sperrin if (no_blk) 3421362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 343789Sahrens } 344789Sahrens 345789Sahrens /* 346789Sahrens * In one tx, free all log blocks and clear the log header. 347789Sahrens */ 348789Sahrens void 349789Sahrens zil_destroy(zilog_t *zilog) 350789Sahrens { 351789Sahrens dmu_tx_t *tx; 352789Sahrens uint64_t txg; 353789Sahrens 354789Sahrens mutex_enter(&zilog->zl_destroy_lock); 355789Sahrens 356789Sahrens if (BP_IS_HOLE(&zilog->zl_header->zh_log)) { 357789Sahrens mutex_exit(&zilog->zl_destroy_lock); 358789Sahrens return; 359789Sahrens } 360789Sahrens 361789Sahrens tx = dmu_tx_create(zilog->zl_os); 362789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 363789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 364789Sahrens txg = dmu_tx_get_txg(tx); 365789Sahrens 366789Sahrens zil_parse(zilog, zil_free_log_block, zil_free_log_record, tx, 367789Sahrens zilog->zl_header->zh_claim_txg); 3681362Sperrin /* 3691362Sperrin * zil_sync clears the zil header as soon as the zl_destroy_txg commits 3701362Sperrin */ 371789Sahrens zilog->zl_destroy_txg = txg; 372789Sahrens 373789Sahrens dmu_tx_commit(tx); 374789Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 375789Sahrens 376789Sahrens mutex_exit(&zilog->zl_destroy_lock); 377789Sahrens } 378789Sahrens 379789Sahrens void 380789Sahrens zil_claim(char *osname, void *txarg) 381789Sahrens { 382789Sahrens dmu_tx_t *tx = txarg; 383789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 384789Sahrens zilog_t *zilog; 385789Sahrens zil_header_t *zh; 386789Sahrens objset_t *os; 387789Sahrens int error; 388789Sahrens 389789Sahrens error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os); 390789Sahrens if (error) { 391789Sahrens cmn_err(CE_WARN, "can't process intent log for %s", osname); 392789Sahrens return; 393789Sahrens } 394789Sahrens 395789Sahrens zilog = dmu_objset_zil(os); 396789Sahrens zh = zilog->zl_header; 397789Sahrens 398789Sahrens /* 399789Sahrens * Claim all log blocks if we haven't already done so. 400789Sahrens */ 401789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 402789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 403789Sahrens zh->zh_claim_txg = first_txg; 404789Sahrens zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, 405789Sahrens tx, first_txg); 406789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 407789Sahrens } 408789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 409789Sahrens dmu_objset_close(os); 410789Sahrens } 411789Sahrens 412789Sahrens void 413789Sahrens zil_add_vdev(zilog_t *zilog, uint64_t vdev, uint64_t seq) 414789Sahrens { 415789Sahrens zil_vdev_t *zv; 416789Sahrens 417789Sahrens if (zil_noflush) 418789Sahrens return; 419789Sahrens 420789Sahrens ASSERT(MUTEX_HELD(&zilog->zl_lock)); 421789Sahrens zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP); 422789Sahrens zv->vdev = vdev; 423789Sahrens zv->seq = seq; 424789Sahrens list_insert_tail(&zilog->zl_vdev_list, zv); 425789Sahrens } 426789Sahrens 427789Sahrens void 428789Sahrens zil_flush_vdevs(zilog_t *zilog, uint64_t seq) 429789Sahrens { 430789Sahrens vdev_t *vd; 431789Sahrens zil_vdev_t *zv, *zv2; 432789Sahrens zio_t *zio; 433789Sahrens spa_t *spa; 434789Sahrens uint64_t vdev; 435789Sahrens 436789Sahrens if (zil_noflush) 437789Sahrens return; 438789Sahrens 439789Sahrens ASSERT(MUTEX_HELD(&zilog->zl_lock)); 440789Sahrens 441789Sahrens spa = zilog->zl_spa; 442789Sahrens zio = NULL; 443789Sahrens 444789Sahrens while ((zv = list_head(&zilog->zl_vdev_list)) != NULL && 445789Sahrens zv->seq <= seq) { 446789Sahrens vdev = zv->vdev; 447789Sahrens list_remove(&zilog->zl_vdev_list, zv); 448789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 449789Sahrens 450789Sahrens /* 451789Sahrens * remove all chained entries <= seq with same vdev 452789Sahrens */ 453789Sahrens zv = list_head(&zilog->zl_vdev_list); 454789Sahrens while (zv && zv->seq <= seq) { 455789Sahrens zv2 = list_next(&zilog->zl_vdev_list, zv); 456789Sahrens if (zv->vdev == vdev) { 457789Sahrens list_remove(&zilog->zl_vdev_list, zv); 458789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 459789Sahrens } 460789Sahrens zv = zv2; 461789Sahrens } 462789Sahrens 463789Sahrens /* flush the write cache for this vdev */ 464789Sahrens mutex_exit(&zilog->zl_lock); 465789Sahrens if (zio == NULL) 466789Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 467789Sahrens vd = vdev_lookup_top(spa, vdev); 468789Sahrens ASSERT(vd); 469789Sahrens (void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 470789Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 471789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 472789Sahrens mutex_enter(&zilog->zl_lock); 473789Sahrens } 474789Sahrens 475789Sahrens /* 476789Sahrens * Wait for all the flushes to complete. Not all devices actually 477789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 478789Sahrens */ 4791141Sperrin if (zio != NULL) { 4801141Sperrin mutex_exit(&zilog->zl_lock); 481789Sahrens (void) zio_wait(zio); 4821141Sperrin mutex_enter(&zilog->zl_lock); 4831141Sperrin } 484789Sahrens } 485789Sahrens 486789Sahrens /* 487789Sahrens * Function called when a log block write completes 488789Sahrens */ 489789Sahrens static void 490789Sahrens zil_lwb_write_done(zio_t *zio) 491789Sahrens { 492789Sahrens lwb_t *prev; 493789Sahrens lwb_t *lwb = zio->io_private; 494789Sahrens zilog_t *zilog = lwb->lwb_zilog; 495789Sahrens uint64_t max_seq; 496789Sahrens 497789Sahrens /* 498789Sahrens * Now that we've written this log block, we have a stable pointer 499789Sahrens * to the next block in the chain, so it's OK to let the txg in 500789Sahrens * which we allocated the next block sync. 501789Sahrens */ 502789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 503789Sahrens 504789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 505789Sahrens mutex_enter(&zilog->zl_lock); 506789Sahrens lwb->lwb_buf = NULL; 507789Sahrens if (zio->io_error) { 508789Sahrens zilog->zl_log_error = B_TRUE; 509789Sahrens mutex_exit(&zilog->zl_lock); 510789Sahrens cv_broadcast(&zilog->zl_cv_seq); 511789Sahrens return; 512789Sahrens } 513789Sahrens 514789Sahrens prev = list_prev(&zilog->zl_lwb_list, lwb); 515789Sahrens if (prev && prev->lwb_state != SEQ_COMPLETE) { 516789Sahrens /* There's an unwritten buffer in the chain before this one */ 517789Sahrens lwb->lwb_state = SEQ_INCOMPLETE; 518789Sahrens mutex_exit(&zilog->zl_lock); 519789Sahrens return; 520789Sahrens } 521789Sahrens 522789Sahrens max_seq = lwb->lwb_seq; 523789Sahrens lwb->lwb_state = SEQ_COMPLETE; 524789Sahrens /* 525789Sahrens * We must also follow up the chain for already written buffers 526789Sahrens * to see if we can set zl_ss_seq even higher. 527789Sahrens */ 528789Sahrens while (lwb = list_next(&zilog->zl_lwb_list, lwb)) { 529789Sahrens if (lwb->lwb_state != SEQ_INCOMPLETE) 530789Sahrens break; 531789Sahrens lwb->lwb_state = SEQ_COMPLETE; 532789Sahrens /* lwb_seq will be zero if we've written an empty buffer */ 533789Sahrens if (lwb->lwb_seq) { 534789Sahrens ASSERT3U(max_seq, <, lwb->lwb_seq); 535789Sahrens max_seq = lwb->lwb_seq; 536789Sahrens } 537789Sahrens } 538789Sahrens zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq); 539789Sahrens mutex_exit(&zilog->zl_lock); 540789Sahrens cv_broadcast(&zilog->zl_cv_seq); 541789Sahrens } 542789Sahrens 543789Sahrens /* 544789Sahrens * Start a log block write and advance to the next log block. 545789Sahrens * Calls are serialized. 546789Sahrens */ 547789Sahrens static lwb_t * 548789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 549789Sahrens { 550789Sahrens lwb_t *nlwb; 551789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 552789Sahrens uint64_t txg; 553789Sahrens uint64_t zil_blksz; 554789Sahrens int error; 555789Sahrens 556789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 557789Sahrens 558789Sahrens /* 559789Sahrens * Allocate the next block and save its address in this block 560789Sahrens * before writing it in order to establish the log chain. 561789Sahrens * Note that if the allocation of nlwb synced before we wrote 562789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 563789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 564789Sahrens */ 565789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 566789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 567789Sahrens 568789Sahrens /* 5691141Sperrin * Pick a ZIL blocksize. We request a size that is the 5701141Sperrin * maximum of the previous used size, the current used size and 5711141Sperrin * the amount waiting in the queue. 572789Sahrens */ 5731141Sperrin zil_blksz = MAX(zilog->zl_cur_used, zilog->zl_prev_used); 5741141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 5751141Sperrin zil_blksz = P2ROUNDUP(zil_blksz, ZIL_MIN_BLKSZ); 5761141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 5771141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 578789Sahrens 579789Sahrens error = zio_alloc_blk(zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 580789Sahrens zil_blksz, &ztp->zit_next_blk, txg); 581789Sahrens if (error) { 582789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 583789Sahrens return (NULL); 584789Sahrens } 585789Sahrens 586789Sahrens ASSERT3U(ztp->zit_next_blk.blk_birth, ==, txg); 587789Sahrens ztp->zit_nused = lwb->lwb_nused; 588789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 589789Sahrens ztp->zit_next_blk.blk_cksum = lwb->lwb_blk.blk_cksum; 590789Sahrens ztp->zit_next_blk.blk_cksum.zc_word[3]++; 591789Sahrens 592789Sahrens /* 593789Sahrens * Allocate a new log write buffer (lwb). 594789Sahrens */ 595789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 596789Sahrens 597789Sahrens nlwb->lwb_zilog = zilog; 598789Sahrens nlwb->lwb_blk = ztp->zit_next_blk; 599789Sahrens nlwb->lwb_nused = 0; 600789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 601789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 602789Sahrens nlwb->lwb_max_txg = txg; 603789Sahrens nlwb->lwb_seq = 0; 604789Sahrens nlwb->lwb_state = UNWRITTEN; 605789Sahrens 606789Sahrens /* 607789Sahrens * Put new lwb at the end of the log chain, 608789Sahrens * and record the vdev for later flushing 609789Sahrens */ 610789Sahrens mutex_enter(&zilog->zl_lock); 611789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 612789Sahrens zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk))), 613789Sahrens lwb->lwb_seq); 614789Sahrens mutex_exit(&zilog->zl_lock); 615789Sahrens 616789Sahrens /* 617789Sahrens * write the old log block 618789Sahrens */ 619789Sahrens dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 620789Sahrens zio_nowait(zio_rewrite(NULL, zilog->zl_spa, ZIO_CHECKSUM_ZILOG, 0, 621789Sahrens &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, zil_lwb_write_done, lwb, 622789Sahrens ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED)); 623789Sahrens 624789Sahrens return (nlwb); 625789Sahrens } 626789Sahrens 627789Sahrens static lwb_t * 628789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 629789Sahrens { 630789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 631789Sahrens uint64_t seq = lrc->lrc_seq; 632789Sahrens uint64_t txg = lrc->lrc_txg; 633789Sahrens uint64_t reclen = lrc->lrc_reclen; 634789Sahrens int error; 635789Sahrens 636789Sahrens if (lwb == NULL) 637789Sahrens return (NULL); 638789Sahrens ASSERT(lwb->lwb_buf != NULL); 639789Sahrens 640789Sahrens /* 641789Sahrens * If it's a write, fetch the data or get its blkptr as appropriate. 642789Sahrens */ 643789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 644789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 645789Sahrens if (txg > spa_freeze_txg(zilog->zl_spa)) 646789Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 647789Sahrens 648789Sahrens if (!itx->itx_data_copied && 649789Sahrens (error = zilog->zl_get_data(itx->itx_private, lr)) != 0) { 650789Sahrens if (error != ENOENT && error != EALREADY) { 651789Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 652789Sahrens mutex_enter(&zilog->zl_lock); 653789Sahrens zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq); 654789Sahrens zil_add_vdev(zilog, 655789Sahrens DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))), 656789Sahrens seq); 657789Sahrens mutex_exit(&zilog->zl_lock); 658789Sahrens return (lwb); 659789Sahrens } 660789Sahrens mutex_enter(&zilog->zl_lock); 661789Sahrens zil_add_vdev(zilog, 662789Sahrens DVA_GET_VDEV(BP_IDENTITY(&(lr->lr_blkptr))), seq); 663789Sahrens mutex_exit(&zilog->zl_lock); 664789Sahrens return (lwb); 665789Sahrens } 666789Sahrens } 667789Sahrens 6681141Sperrin zilog->zl_cur_used += reclen; 6691141Sperrin 670789Sahrens /* 671789Sahrens * If this record won't fit in the current log block, start a new one. 672789Sahrens */ 673789Sahrens if (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)) { 674789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 675789Sahrens if (lwb == NULL) 676789Sahrens return (NULL); 677789Sahrens if (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)) { 678789Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 679789Sahrens mutex_enter(&zilog->zl_lock); 680789Sahrens zilog->zl_ss_seq = MAX(seq, zilog->zl_ss_seq); 681789Sahrens mutex_exit(&zilog->zl_lock); 682789Sahrens return (lwb); 683789Sahrens } 684789Sahrens } 685789Sahrens 686789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 687789Sahrens lwb->lwb_nused += reclen; 688789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 689789Sahrens ASSERT3U(lwb->lwb_seq, <, seq); 690789Sahrens lwb->lwb_seq = seq; 691789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 692789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 693789Sahrens 694789Sahrens return (lwb); 695789Sahrens } 696789Sahrens 697789Sahrens itx_t * 698789Sahrens zil_itx_create(int txtype, size_t lrsize) 699789Sahrens { 700789Sahrens itx_t *itx; 701789Sahrens 702789Sahrens lrsize = P2ROUNDUP(lrsize, sizeof (uint64_t)); 703789Sahrens 704789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 705789Sahrens itx->itx_lr.lrc_txtype = txtype; 706789Sahrens itx->itx_lr.lrc_reclen = lrsize; 707789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 708789Sahrens 709789Sahrens return (itx); 710789Sahrens } 711789Sahrens 712789Sahrens uint64_t 713789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 714789Sahrens { 715789Sahrens uint64_t seq; 716789Sahrens 717789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 718789Sahrens 719789Sahrens mutex_enter(&zilog->zl_lock); 720789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 721789Sahrens zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen; 722789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 723789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 724789Sahrens mutex_exit(&zilog->zl_lock); 725789Sahrens 726789Sahrens return (seq); 727789Sahrens } 728789Sahrens 729789Sahrens /* 730789Sahrens * Free up all in-memory intent log transactions that have now been synced. 731789Sahrens */ 732789Sahrens static void 733789Sahrens zil_itx_clean(zilog_t *zilog) 734789Sahrens { 735789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 736789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 737789Sahrens uint64_t max_seq = 0; 738789Sahrens itx_t *itx; 739789Sahrens 740789Sahrens mutex_enter(&zilog->zl_lock); 741789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 742789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 743789Sahrens list_remove(&zilog->zl_itx_list, itx); 744789Sahrens zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen; 745789Sahrens ASSERT3U(max_seq, <, itx->itx_lr.lrc_seq); 746789Sahrens max_seq = itx->itx_lr.lrc_seq; 747789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 748789Sahrens + itx->itx_lr.lrc_reclen); 749789Sahrens } 750789Sahrens if (max_seq > zilog->zl_ss_seq) { 751789Sahrens zilog->zl_ss_seq = max_seq; 752789Sahrens cv_broadcast(&zilog->zl_cv_seq); 753789Sahrens } 754789Sahrens mutex_exit(&zilog->zl_lock); 755789Sahrens } 756789Sahrens 757789Sahrens void 758789Sahrens zil_clean(zilog_t *zilog) 759789Sahrens { 760789Sahrens /* 761789Sahrens * Check for any log blocks that can be freed. 762789Sahrens * Log blocks are only freed when the log block allocation and 763789Sahrens * log records contained within are both known to be committed. 764789Sahrens */ 765789Sahrens mutex_enter(&zilog->zl_lock); 766789Sahrens if (list_head(&zilog->zl_itx_list) != NULL) 767789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 768789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 769789Sahrens mutex_exit(&zilog->zl_lock); 770789Sahrens } 771789Sahrens 772789Sahrens /* 773789Sahrens * Push zfs transactions to stable storage up to the supplied sequence number. 774789Sahrens */ 775789Sahrens void 776789Sahrens zil_commit(zilog_t *zilog, uint64_t seq, int ioflag) 777789Sahrens { 778789Sahrens uint64_t txg; 779789Sahrens uint64_t max_seq; 780789Sahrens uint64_t reclen; 781789Sahrens itx_t *itx; 782789Sahrens lwb_t *lwb; 783789Sahrens spa_t *spa; 784789Sahrens 785789Sahrens if (zilog == NULL || seq == 0 || 786789Sahrens ((ioflag & (FSYNC | FDSYNC | FRSYNC)) == 0 && !zil_always)) 787789Sahrens return; 788789Sahrens 789789Sahrens spa = zilog->zl_spa; 790789Sahrens mutex_enter(&zilog->zl_lock); 791789Sahrens 792789Sahrens seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 793789Sahrens 794789Sahrens for (;;) { 795789Sahrens if (zilog->zl_ss_seq >= seq) { /* already on stable storage */ 796789Sahrens mutex_exit(&zilog->zl_lock); 797789Sahrens return; 798789Sahrens } 799789Sahrens 800789Sahrens if (zilog->zl_writer == B_FALSE) /* no one writing, do it */ 801789Sahrens break; 802789Sahrens 803789Sahrens cv_wait(&zilog->zl_cv_write, &zilog->zl_lock); 804789Sahrens } 805789Sahrens 806789Sahrens zilog->zl_writer = B_TRUE; 807789Sahrens max_seq = 0; 808789Sahrens 809789Sahrens if (zilog->zl_suspend) { 810789Sahrens lwb = NULL; 811789Sahrens } else { 812789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 813789Sahrens if (lwb == NULL) { 814789Sahrens mutex_exit(&zilog->zl_lock); 815789Sahrens zil_create(zilog); 816789Sahrens mutex_enter(&zilog->zl_lock); 817789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 818789Sahrens } 819789Sahrens } 820789Sahrens 821789Sahrens /* 822789Sahrens * Loop through in-memory log transactions filling log blocks, 823789Sahrens * until we reach the given sequence number and there's no more 824789Sahrens * room in the write buffer. 825789Sahrens */ 826789Sahrens for (;;) { 827789Sahrens itx = list_head(&zilog->zl_itx_list); 828789Sahrens if (itx == NULL) 829789Sahrens break; 830789Sahrens 831789Sahrens reclen = itx->itx_lr.lrc_reclen; 832789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 833789Sahrens ((lwb == NULL) || (lwb->lwb_nused + reclen > 834789Sahrens ZIL_BLK_DATA_SZ(lwb)))) 835789Sahrens break; 836789Sahrens 837789Sahrens list_remove(&zilog->zl_itx_list, itx); 838789Sahrens txg = itx->itx_lr.lrc_txg; 839789Sahrens ASSERT(txg); 840789Sahrens 841789Sahrens mutex_exit(&zilog->zl_lock); 842789Sahrens if (txg > spa_last_synced_txg(spa) || 843789Sahrens txg > spa_freeze_txg(spa)) 844789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 845789Sahrens else 846789Sahrens max_seq = itx->itx_lr.lrc_seq; 847789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 848789Sahrens + itx->itx_lr.lrc_reclen); 849789Sahrens mutex_enter(&zilog->zl_lock); 850789Sahrens zilog->zl_itx_list_sz -= reclen; 851789Sahrens } 852789Sahrens 853789Sahrens mutex_exit(&zilog->zl_lock); 854789Sahrens 855789Sahrens /* write the last block out */ 856789Sahrens if (lwb != NULL && lwb->lwb_nused != 0) 857789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 858789Sahrens 8591141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 8601141Sperrin zilog->zl_cur_used = 0; 8611141Sperrin 862789Sahrens mutex_enter(&zilog->zl_lock); 863789Sahrens if (max_seq > zilog->zl_ss_seq) { 864789Sahrens zilog->zl_ss_seq = max_seq; 865789Sahrens cv_broadcast(&zilog->zl_cv_seq); 866789Sahrens } 867789Sahrens /* 868789Sahrens * Wait if necessary for our seq to be committed. 869789Sahrens */ 870789Sahrens if (lwb) { 871789Sahrens while (zilog->zl_ss_seq < seq && zilog->zl_log_error == 0) 872789Sahrens cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock); 873789Sahrens zil_flush_vdevs(zilog, seq); 874789Sahrens } 8751141Sperrin 876789Sahrens if (zilog->zl_log_error || lwb == NULL) { 877789Sahrens zilog->zl_log_error = 0; 878789Sahrens max_seq = zilog->zl_itx_seq; 879789Sahrens mutex_exit(&zilog->zl_lock); 880789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 881789Sahrens mutex_enter(&zilog->zl_lock); 882789Sahrens zilog->zl_ss_seq = MAX(max_seq, zilog->zl_ss_seq); 883789Sahrens cv_broadcast(&zilog->zl_cv_seq); 884789Sahrens } 8851141Sperrin /* wake up others waiting to start a write */ 8861141Sperrin zilog->zl_writer = B_FALSE; 887789Sahrens mutex_exit(&zilog->zl_lock); 888*1472Sperrin cv_broadcast(&zilog->zl_cv_write); 889789Sahrens } 890789Sahrens 891789Sahrens /* 892789Sahrens * Called in syncing context to free committed log blocks and update log header. 893789Sahrens */ 894789Sahrens void 895789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 896789Sahrens { 897789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 898789Sahrens spa_t *spa = zilog->zl_spa; 899789Sahrens lwb_t *lwb; 900789Sahrens 901789Sahrens ASSERT(zilog->zl_stop_sync == 0); 902789Sahrens 903789Sahrens zilog->zl_header->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 904789Sahrens 905789Sahrens if (zilog->zl_destroy_txg == txg) { 906789Sahrens bzero(zilog->zl_header, sizeof (zil_header_t)); 907789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 908789Sahrens zilog->zl_destroy_txg = 0; 909789Sahrens } 910789Sahrens 911789Sahrens mutex_enter(&zilog->zl_lock); 912789Sahrens for (;;) { 913789Sahrens lwb = list_head(&zilog->zl_lwb_list); 914789Sahrens if (lwb == NULL) { 915789Sahrens mutex_exit(&zilog->zl_lock); 916789Sahrens return; 917789Sahrens } 918789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 919789Sahrens break; 920789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 921789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 922789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 923789Sahrens } 924789Sahrens zilog->zl_header->zh_log = lwb->lwb_blk; 925789Sahrens mutex_exit(&zilog->zl_lock); 926789Sahrens } 927789Sahrens 928789Sahrens void 929789Sahrens zil_init(void) 930789Sahrens { 931789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 932789Sahrens sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0); 933789Sahrens } 934789Sahrens 935789Sahrens void 936789Sahrens zil_fini(void) 937789Sahrens { 938789Sahrens kmem_cache_destroy(zil_lwb_cache); 939789Sahrens } 940789Sahrens 941789Sahrens zilog_t * 942789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 943789Sahrens { 944789Sahrens zilog_t *zilog; 945789Sahrens 946789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 947789Sahrens 948789Sahrens zilog->zl_header = zh_phys; 949789Sahrens zilog->zl_os = os; 950789Sahrens zilog->zl_spa = dmu_objset_spa(os); 951789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 952789Sahrens 953789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 954789Sahrens offsetof(itx_t, itx_node)); 955789Sahrens 956789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 957789Sahrens offsetof(lwb_t, lwb_node)); 958789Sahrens 959789Sahrens list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t), 960789Sahrens offsetof(zil_vdev_t, vdev_seq_node)); 961789Sahrens 962789Sahrens return (zilog); 963789Sahrens } 964789Sahrens 965789Sahrens void 966789Sahrens zil_free(zilog_t *zilog) 967789Sahrens { 968789Sahrens lwb_t *lwb; 969789Sahrens zil_vdev_t *zv; 970789Sahrens 971789Sahrens zilog->zl_stop_sync = 1; 972789Sahrens 973789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 974789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 975789Sahrens if (lwb->lwb_buf != NULL) 976789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 977789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 978789Sahrens } 979789Sahrens list_destroy(&zilog->zl_lwb_list); 980789Sahrens 981789Sahrens while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 982789Sahrens list_remove(&zilog->zl_vdev_list, zv); 983789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 984789Sahrens } 985789Sahrens list_destroy(&zilog->zl_vdev_list); 986789Sahrens 987789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 988789Sahrens list_destroy(&zilog->zl_itx_list); 989789Sahrens 990789Sahrens kmem_free(zilog, sizeof (zilog_t)); 991789Sahrens } 992789Sahrens 993789Sahrens /* 9941362Sperrin * return true if there is a valid initial zil log block 9951362Sperrin */ 9961362Sperrin static int 9971362Sperrin zil_empty(zilog_t *zilog) 9981362Sperrin { 9991362Sperrin blkptr_t blk; 10001362Sperrin char *lrbuf; 10011362Sperrin int error; 10021362Sperrin 10031362Sperrin blk = zilog->zl_header->zh_log; 10041362Sperrin if (BP_IS_HOLE(&blk)) 10051362Sperrin return (1); 10061362Sperrin 10071362Sperrin lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); 10081362Sperrin error = zil_read_log_block(zilog, &blk, lrbuf); 10091362Sperrin zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); 10101362Sperrin return (error ? 1 : 0); 10111362Sperrin } 10121362Sperrin 10131362Sperrin /* 1014789Sahrens * Open an intent log. 1015789Sahrens */ 1016789Sahrens zilog_t * 1017789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1018789Sahrens { 1019789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1020789Sahrens 1021789Sahrens zilog->zl_get_data = get_data; 1022789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1023789Sahrens 2, 2, TASKQ_PREPOPULATE); 1024789Sahrens 1025789Sahrens return (zilog); 1026789Sahrens } 1027789Sahrens 1028789Sahrens /* 1029789Sahrens * Close an intent log. 1030789Sahrens */ 1031789Sahrens void 1032789Sahrens zil_close(zilog_t *zilog) 1033789Sahrens { 10341362Sperrin if (!zil_empty(zilog)) 10351362Sperrin txg_wait_synced(zilog->zl_dmu_pool, 0); 1036789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1037789Sahrens zilog->zl_clean_taskq = NULL; 1038789Sahrens zilog->zl_get_data = NULL; 1039789Sahrens 1040789Sahrens zil_itx_clean(zilog); 1041789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1042789Sahrens } 1043789Sahrens 1044789Sahrens /* 1045789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1046789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1047789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1048789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1049789Sahrens */ 1050789Sahrens int 1051789Sahrens zil_suspend(zilog_t *zilog) 1052789Sahrens { 1053789Sahrens lwb_t *lwb; 1054789Sahrens 1055789Sahrens mutex_enter(&zilog->zl_lock); 1056789Sahrens if (zilog->zl_header->zh_claim_txg != 0) { /* unplayed log */ 1057789Sahrens mutex_exit(&zilog->zl_lock); 1058789Sahrens return (EBUSY); 1059789Sahrens } 1060789Sahrens zilog->zl_suspend++; 1061789Sahrens mutex_exit(&zilog->zl_lock); 1062789Sahrens 1063789Sahrens zil_commit(zilog, UINT64_MAX, FSYNC); 1064789Sahrens 1065789Sahrens mutex_enter(&zilog->zl_lock); 1066789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1067789Sahrens if (lwb->lwb_buf != NULL) { 1068789Sahrens /* 1069789Sahrens * Wait for the buffer if it's in the process of 1070789Sahrens * being written. 1071789Sahrens */ 1072789Sahrens if ((lwb->lwb_seq != 0) && 1073789Sahrens (lwb->lwb_state != SEQ_COMPLETE)) { 1074789Sahrens cv_wait(&zilog->zl_cv_seq, &zilog->zl_lock); 1075789Sahrens continue; 1076789Sahrens } 1077789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1078789Sahrens } 1079789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1080789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1081789Sahrens } 1082789Sahrens mutex_exit(&zilog->zl_lock); 1083789Sahrens 1084789Sahrens zil_destroy(zilog); 1085789Sahrens 1086789Sahrens return (0); 1087789Sahrens } 1088789Sahrens 1089789Sahrens void 1090789Sahrens zil_resume(zilog_t *zilog) 1091789Sahrens { 1092789Sahrens mutex_enter(&zilog->zl_lock); 1093789Sahrens ASSERT(zilog->zl_suspend != 0); 1094789Sahrens zilog->zl_suspend--; 1095789Sahrens mutex_exit(&zilog->zl_lock); 1096789Sahrens } 1097789Sahrens 1098789Sahrens typedef struct zil_replay_arg { 1099789Sahrens objset_t *zr_os; 1100789Sahrens zil_replay_func_t **zr_replay; 1101789Sahrens void *zr_arg; 1102789Sahrens void (*zr_rm_sync)(void *arg); 1103789Sahrens uint64_t *zr_txgp; 1104789Sahrens boolean_t zr_byteswap; 1105789Sahrens char *zr_lrbuf; 1106789Sahrens } zil_replay_arg_t; 1107789Sahrens 1108789Sahrens static void 1109789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1110789Sahrens { 1111789Sahrens zil_replay_arg_t *zr = zra; 1112789Sahrens zil_header_t *zh = zilog->zl_header; 1113789Sahrens uint64_t reclen = lr->lrc_reclen; 1114789Sahrens uint64_t txtype = lr->lrc_txtype; 1115789Sahrens int pass, error; 1116789Sahrens 1117789Sahrens if (zilog->zl_stop_replay) 1118789Sahrens return; 1119789Sahrens 1120789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1121789Sahrens return; 1122789Sahrens 1123789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1124789Sahrens return; 1125789Sahrens 1126789Sahrens /* 1127789Sahrens * Make a copy of the data so we can revise and extend it. 1128789Sahrens */ 1129789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1130789Sahrens 1131789Sahrens /* 1132789Sahrens * The log block containing this lr may have been byteswapped 1133789Sahrens * so that we can easily examine common fields like lrc_txtype. 1134789Sahrens * However, the log is a mix of different data types, and only the 1135789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1136789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1137789Sahrens */ 1138789Sahrens if (zr->zr_byteswap) 1139789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1140789Sahrens 1141789Sahrens /* 1142789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1143789Sahrens */ 1144789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1145789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1146789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1147789Sahrens uint64_t wlen = lrw->lr_length; 1148789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1149789Sahrens 1150789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1151789Sahrens bzero(wbuf, wlen); 1152789Sahrens } else { 1153789Sahrens /* 1154789Sahrens * A subsequent write may have overwritten this block, 1155789Sahrens * in which case wbp may have been been freed and 1156789Sahrens * reallocated, and our read of wbp may fail with a 1157789Sahrens * checksum error. We can safely ignore this because 1158789Sahrens * the later write will provide the correct data. 1159789Sahrens */ 1160789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1161789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1162789Sahrens ZIO_PRIORITY_SYNC_READ, 1163789Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE)); 1164789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1165789Sahrens } 1166789Sahrens } 1167789Sahrens 1168789Sahrens /* 1169789Sahrens * We must now do two things atomically: replay this log record, 1170789Sahrens * and update the log header to reflect the fact that we did so. 1171789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1172789Sahrens */ 1173789Sahrens for (pass = 1; /* CONSTANTCONDITION */; pass++) { 1174789Sahrens uint64_t replay_txg; 1175789Sahrens dmu_tx_t *replay_tx; 1176789Sahrens 1177789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1178789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1179789Sahrens if (error) { 1180789Sahrens dmu_tx_abort(replay_tx); 1181789Sahrens break; 1182789Sahrens } 1183789Sahrens 1184789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1185789Sahrens 1186789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1187789Sahrens error = EINVAL; 1188789Sahrens } else { 1189789Sahrens /* 1190789Sahrens * On the first pass, arrange for the replay vector 1191789Sahrens * to fail its dmu_tx_assign(). That's the only way 1192789Sahrens * to ensure that those code paths remain well tested. 1193789Sahrens */ 1194789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1195789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1196789Sahrens zr->zr_byteswap); 1197789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1198789Sahrens } 1199789Sahrens 1200789Sahrens if (error == 0) { 1201789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1202789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1203789Sahrens lr->lrc_seq; 1204789Sahrens } 1205789Sahrens 1206789Sahrens dmu_tx_commit(replay_tx); 1207789Sahrens 1208789Sahrens if (error != ERESTART) 1209789Sahrens break; 1210789Sahrens 1211789Sahrens if (pass != 1) 1212789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1213789Sahrens replay_txg + 1); 1214789Sahrens 1215789Sahrens dprintf("pass %d, retrying\n", pass); 1216789Sahrens } 1217789Sahrens 1218789Sahrens if (error) { 1219789Sahrens char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1220789Sahrens dmu_objset_name(zr->zr_os, name); 1221789Sahrens cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1222789Sahrens "dataset %s, seq 0x%llx, txtype %llu\n", 1223789Sahrens error, name, 1224789Sahrens (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype); 1225789Sahrens zilog->zl_stop_replay = 1; 1226789Sahrens kmem_free(name, MAXNAMELEN); 1227789Sahrens } 1228789Sahrens 1229789Sahrens /* 1230789Sahrens * The DMU's dnode layer doesn't see removes until the txg commits, 1231789Sahrens * so a subsequent claim can spuriously fail with EEXIST. 1232789Sahrens * To prevent this, if we might have removed an object, 1233789Sahrens * wait for the delete thread to delete it, and then 1234789Sahrens * wait for the transaction group to sync. 1235789Sahrens */ 1236789Sahrens if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) { 1237789Sahrens if (zr->zr_rm_sync != NULL) 1238789Sahrens zr->zr_rm_sync(zr->zr_arg); 1239789Sahrens txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1240789Sahrens } 1241789Sahrens } 1242789Sahrens 1243789Sahrens /* 12441362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1245789Sahrens */ 1246789Sahrens void 1247789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 1248789Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg)) 1249789Sahrens { 1250789Sahrens zilog_t *zilog = dmu_objset_zil(os); 12511362Sperrin zil_replay_arg_t zr; 12521362Sperrin 12531362Sperrin if (zil_empty(zilog)) { 12541362Sperrin /* 12551362Sperrin * Initialise the log header but don't free the log block 12561362Sperrin * which will get reused. 12571362Sperrin */ 12581362Sperrin zilog->zl_header->zh_claim_txg = 0; 12591362Sperrin zilog->zl_header->zh_replay_seq = 0; 12601362Sperrin return; 12611362Sperrin } 1262789Sahrens 1263789Sahrens zr.zr_os = os; 1264789Sahrens zr.zr_replay = replay_func; 1265789Sahrens zr.zr_arg = arg; 1266789Sahrens zr.zr_rm_sync = rm_sync; 1267789Sahrens zr.zr_txgp = txgp; 1268789Sahrens zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zilog->zl_header->zh_log); 1269789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1270789Sahrens 1271789Sahrens /* 1272789Sahrens * Wait for in-progress removes to sync before starting replay. 1273789Sahrens */ 1274789Sahrens if (rm_sync != NULL) 1275789Sahrens rm_sync(arg); 1276789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1277789Sahrens 1278789Sahrens zilog->zl_stop_replay = 0; 1279789Sahrens zil_parse(zilog, NULL, zil_replay_log_record, &zr, 1280789Sahrens zilog->zl_header->zh_claim_txg); 1281789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1282789Sahrens 1283789Sahrens zil_destroy(zilog); 1284789Sahrens } 1285