Print this page
5056 ZFS deadlock on db_mtx and dn_holds
Reviewed by: Will Andrews <willa@spectralogic.com>
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Approved by: Dan McDonald <danmcd@omniti.com>


   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
  25  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  26  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  27  * Copyright 2013 DEY Storage Systems, Inc.
  28  * Copyright 2014 HybridCluster. All rights reserved.

  29  */
  30 
  31 /* Portions Copyright 2010 Robert Milkowski */
  32 
  33 #ifndef _SYS_DMU_H
  34 #define _SYS_DMU_H
  35 
  36 /*
  37  * This file describes the interface that the DMU provides for its
  38  * consumers.
  39  *
  40  * The DMU also interacts with the SPA.  That interface is described in
  41  * dmu_spa.h.
  42  */
  43 

  44 #include <sys/inttypes.h>
  45 #include <sys/types.h>
  46 #include <sys/param.h>
  47 #include <sys/cred.h>
  48 #include <sys/time.h>
  49 #include <sys/fs/zfs.h>
  50 
  51 #ifdef  __cplusplus
  52 extern "C" {
  53 #endif
  54 
  55 struct uio;
  56 struct xuio;
  57 struct page;
  58 struct vnode;
  59 struct spa;
  60 struct zilog;
  61 struct zio;
  62 struct blkptr;
  63 struct zap_cursor;
  64 struct dsl_dataset;
  65 struct dsl_pool;
  66 struct dnode;
  67 struct drr_begin;
  68 struct drr_end;


 274 int dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
 275     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg);
 276 int dmu_objset_clone(const char *name, const char *origin);
 277 int dsl_destroy_snapshots_nvl(struct nvlist *snaps, boolean_t defer,
 278     struct nvlist *errlist);
 279 int dmu_objset_snapshot_one(const char *fsname, const char *snapname);
 280 int dmu_objset_snapshot_tmp(const char *, const char *, int);
 281 int dmu_objset_find(char *name, int func(const char *, void *), void *arg,
 282     int flags);
 283 void dmu_objset_byteswap(void *buf, size_t size);
 284 int dsl_dataset_rename_snapshot(const char *fsname,
 285     const char *oldsnapname, const char *newsnapname, boolean_t recursive);
 286 
 287 typedef struct dmu_buf {
 288         uint64_t db_object;             /* object that this buffer is part of */
 289         uint64_t db_offset;             /* byte offset in this object */
 290         uint64_t db_size;               /* size of buffer in bytes */
 291         void *db_data;                  /* data in buffer */
 292 } dmu_buf_t;
 293 
 294 typedef void dmu_buf_evict_func_t(struct dmu_buf *db, void *user_ptr);
 295 
 296 /*
 297  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
 298  */
 299 #define DMU_POOL_DIRECTORY_OBJECT       1
 300 #define DMU_POOL_CONFIG                 "config"
 301 #define DMU_POOL_FEATURES_FOR_WRITE     "features_for_write"
 302 #define DMU_POOL_FEATURES_FOR_READ      "features_for_read"
 303 #define DMU_POOL_FEATURE_DESCRIPTIONS   "feature_descriptions"
 304 #define DMU_POOL_FEATURE_ENABLED_TXG    "feature_enabled_txg"
 305 #define DMU_POOL_ROOT_DATASET           "root_dataset"
 306 #define DMU_POOL_SYNC_BPOBJ             "sync_bplist"
 307 #define DMU_POOL_ERRLOG_SCRUB           "errlog_scrub"
 308 #define DMU_POOL_ERRLOG_LAST            "errlog_last"
 309 #define DMU_POOL_SPARES                 "spares"
 310 #define DMU_POOL_DEFLATE                "deflate"
 311 #define DMU_POOL_HISTORY                "history"
 312 #define DMU_POOL_PROPS                  "pool_props"
 313 #define DMU_POOL_L2CACHE                "l2cache"
 314 #define DMU_POOL_TMP_USERREFS           "tmp_userrefs"
 315 #define DMU_POOL_DDT                    "DDT-%s-%s-%s"


 461 int dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
 462     void *tag, dmu_buf_t **, int flags);
 463 void dmu_buf_add_ref(dmu_buf_t *db, void* tag);
 464 void dmu_buf_rele(dmu_buf_t *db, void *tag);
 465 uint64_t dmu_buf_refcount(dmu_buf_t *db);
 466 
 467 /*
 468  * dmu_buf_hold_array holds the DMU buffers which contain all bytes in a
 469  * range of an object.  A pointer to an array of dmu_buf_t*'s is
 470  * returned (in *dbpp).
 471  *
 472  * dmu_buf_rele_array releases the hold on an array of dmu_buf_t*'s, and
 473  * frees the array.  The hold on the array of buffers MUST be released
 474  * with dmu_buf_rele_array.  You can NOT release the hold on each buffer
 475  * individually with dmu_buf_rele.
 476  */
 477 int dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
 478     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp);
 479 void dmu_buf_rele_array(dmu_buf_t **, int numbufs, void *tag);
 480 


 481 /*
 482  * Returns NULL on success, or the existing user ptr if it's already
 483  * been set.





 484  *
 485  * user_ptr is for use by the user and can be obtained via dmu_buf_get_user().








 486  *
 487  * If non-NULL, pageout func will be called when this buffer is being
 488  * excised from the cache, so that you can clean up the data structure
 489  * pointed to by user_ptr.

 490  *
 491  * dmu_evict_user() will call the pageout func for all buffers in a
 492  * objset with a given pageout func.

 493  */
 494 void *dmu_buf_set_user(dmu_buf_t *db, void *user_ptr,
 495     dmu_buf_evict_func_t *pageout_func);


















 496 /*
 497  * set_user_ie is the same as set_user, but request immediate eviction
 498  * when hold count goes to zero.



 499  */
 500 void *dmu_buf_set_user_ie(dmu_buf_t *db, void *user_ptr,
 501     dmu_buf_evict_func_t *pageout_func);
 502 void *dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr,
 503     void *user_ptr, dmu_buf_evict_func_t *pageout_func);
 504 void dmu_evict_user(objset_t *os, dmu_buf_evict_func_t *func);













 505 
 506 /*
 507  * Returns the user_ptr set with dmu_buf_set_user(), or NULL if not set.




 508  */
































 509 void *dmu_buf_get_user(dmu_buf_t *db);
 510 



 511 /*
 512  * Returns the blkptr associated with this dbuf, or NULL if not set.
 513  */
 514 struct blkptr *dmu_buf_get_blkptr(dmu_buf_t *db);
 515 
 516 /*
 517  * Indicate that you are going to modify the buffer's data (db_data).
 518  *
 519  * The transaction (tx) must be assigned to a txg (ie. you've called
 520  * dmu_tx_assign()).  The buffer's object must be held in the tx
 521  * (ie. you've called dmu_tx_hold_object(tx, db->db_object)).
 522  */
 523 void dmu_buf_will_dirty(dmu_buf_t *db, dmu_tx_t *tx);
 524 
 525 /*
 526  * Tells if the given dbuf is freeable.
 527  */
 528 boolean_t dmu_buf_freeable(dmu_buf_t *);
 529 
 530 /*




   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
  25  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  26  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  27  * Copyright 2013 DEY Storage Systems, Inc.
  28  * Copyright 2014 HybridCluster. All rights reserved.
  29  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  30  */
  31 
  32 /* Portions Copyright 2010 Robert Milkowski */
  33 
  34 #ifndef _SYS_DMU_H
  35 #define _SYS_DMU_H
  36 
  37 /*
  38  * This file describes the interface that the DMU provides for its
  39  * consumers.
  40  *
  41  * The DMU also interacts with the SPA.  That interface is described in
  42  * dmu_spa.h.
  43  */
  44 
  45 #include <sys/zfs_context.h>
  46 #include <sys/inttypes.h>


  47 #include <sys/cred.h>

  48 #include <sys/fs/zfs.h>
  49 
  50 #ifdef  __cplusplus
  51 extern "C" {
  52 #endif
  53 
  54 struct uio;
  55 struct xuio;
  56 struct page;
  57 struct vnode;
  58 struct spa;
  59 struct zilog;
  60 struct zio;
  61 struct blkptr;
  62 struct zap_cursor;
  63 struct dsl_dataset;
  64 struct dsl_pool;
  65 struct dnode;
  66 struct drr_begin;
  67 struct drr_end;


 273 int dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
 274     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg);
 275 int dmu_objset_clone(const char *name, const char *origin);
 276 int dsl_destroy_snapshots_nvl(struct nvlist *snaps, boolean_t defer,
 277     struct nvlist *errlist);
 278 int dmu_objset_snapshot_one(const char *fsname, const char *snapname);
 279 int dmu_objset_snapshot_tmp(const char *, const char *, int);
 280 int dmu_objset_find(char *name, int func(const char *, void *), void *arg,
 281     int flags);
 282 void dmu_objset_byteswap(void *buf, size_t size);
 283 int dsl_dataset_rename_snapshot(const char *fsname,
 284     const char *oldsnapname, const char *newsnapname, boolean_t recursive);
 285 
 286 typedef struct dmu_buf {
 287         uint64_t db_object;             /* object that this buffer is part of */
 288         uint64_t db_offset;             /* byte offset in this object */
 289         uint64_t db_size;               /* size of buffer in bytes */
 290         void *db_data;                  /* data in buffer */
 291 } dmu_buf_t;
 292 


 293 /*
 294  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
 295  */
 296 #define DMU_POOL_DIRECTORY_OBJECT       1
 297 #define DMU_POOL_CONFIG                 "config"
 298 #define DMU_POOL_FEATURES_FOR_WRITE     "features_for_write"
 299 #define DMU_POOL_FEATURES_FOR_READ      "features_for_read"
 300 #define DMU_POOL_FEATURE_DESCRIPTIONS   "feature_descriptions"
 301 #define DMU_POOL_FEATURE_ENABLED_TXG    "feature_enabled_txg"
 302 #define DMU_POOL_ROOT_DATASET           "root_dataset"
 303 #define DMU_POOL_SYNC_BPOBJ             "sync_bplist"
 304 #define DMU_POOL_ERRLOG_SCRUB           "errlog_scrub"
 305 #define DMU_POOL_ERRLOG_LAST            "errlog_last"
 306 #define DMU_POOL_SPARES                 "spares"
 307 #define DMU_POOL_DEFLATE                "deflate"
 308 #define DMU_POOL_HISTORY                "history"
 309 #define DMU_POOL_PROPS                  "pool_props"
 310 #define DMU_POOL_L2CACHE                "l2cache"
 311 #define DMU_POOL_TMP_USERREFS           "tmp_userrefs"
 312 #define DMU_POOL_DDT                    "DDT-%s-%s-%s"


 458 int dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
 459     void *tag, dmu_buf_t **, int flags);
 460 void dmu_buf_add_ref(dmu_buf_t *db, void* tag);
 461 void dmu_buf_rele(dmu_buf_t *db, void *tag);
 462 uint64_t dmu_buf_refcount(dmu_buf_t *db);
 463 
 464 /*
 465  * dmu_buf_hold_array holds the DMU buffers which contain all bytes in a
 466  * range of an object.  A pointer to an array of dmu_buf_t*'s is
 467  * returned (in *dbpp).
 468  *
 469  * dmu_buf_rele_array releases the hold on an array of dmu_buf_t*'s, and
 470  * frees the array.  The hold on the array of buffers MUST be released
 471  * with dmu_buf_rele_array.  You can NOT release the hold on each buffer
 472  * individually with dmu_buf_rele.
 473  */
 474 int dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
 475     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp);
 476 void dmu_buf_rele_array(dmu_buf_t **, int numbufs, void *tag);
 477 
 478 typedef void dmu_buf_evict_func_t(void *user_ptr);
 479 
 480 /*
 481  * A DMU buffer user object may be associated with a dbuf for the
 482  * duration of its lifetime.  This allows the user of a dbuf (client)
 483  * to attach private data to a dbuf (e.g. in-core only data such as a
 484  * dnode_children_t, zap_t, or zap_leaf_t) and be optionally notified
 485  * when that dbuf has been evicted.  Clients typically respond to the
 486  * eviction notification by freeing their private data, thus ensuring
 487  * the same lifetime for both dbuf and private data.
 488  *
 489  * The mapping from a dmu_buf_user_t to any client private data is the
 490  * client's responsibility.  All current consumers of the API with private
 491  * data embed a dmu_buf_user_t as the first member of the structure for
 492  * their private data.  This allows conversions between the two types
 493  * with a simple cast.  Since the DMU buf user API never needs access
 494  * to the private data, other strategies can be employed if necessary
 495  * or convenient for the client (e.g. using container_of() to do the
 496  * conversion for private data that cannot have the dmu_buf_user_t as
 497  * its first member).
 498  *
 499  * Eviction callbacks are executed without the dbuf mutex held or any
 500  * other type of mechanism to guarantee that the dbuf is still available.
 501  * For this reason, users must assume the dbuf has already been freed
 502  * and not reference the dbuf from the callback context.
 503  *
 504  * Users requesting "immediate eviction" are notified as soon as the dbuf
 505  * is only referenced by dirty records (dirties == holds).  Otherwise the
 506  * notification occurs after eviction processing for the dbuf begins.
 507  */
 508 typedef struct dmu_buf_user {
 509         /*
 510          * Asynchronous user eviction callback state.
 511          */
 512         taskq_ent_t     dbu_tqent;
 513 
 514         /* This instance's eviction function pointer. */
 515         dmu_buf_evict_func_t *dbu_evict_func;
 516 #ifdef ZFS_DEBUG
 517         /*
 518          * Pointer to user's dbuf pointer.  NULL for clients that do
 519          * not associate a dbuf with their user data.
 520          *
 521          * The dbuf pointer is cleared upon eviction so as to catch
 522          * use-after-evict bugs in clients.
 523          */
 524         dmu_buf_t **dbu_clear_on_evict_dbufp;
 525 #endif
 526 } dmu_buf_user_t;
 527 
 528 /*
 529  * Initialize the given dmu_buf_user_t instance with the eviction function
 530  * evict_func, to be called when the user is evicted.
 531  *
 532  * NOTE: This function should only be called once on a given dmu_buf_user_t.
 533  *       To allow enforcement of this, dbu must already be zeroed on entry.
 534  */
 535 #ifdef __lint
 536 /* Very ugly, but it beats issuing suppression directives in many Makefiles. */
 537 extern void
 538 dmu_buf_init_user(dmu_buf_user_t *dbu, dmu_buf_evict_func_t *evict_func,
 539     dmu_buf_t **clear_on_evict_dbufp);
 540 #else /* __lint */
 541 inline void
 542 dmu_buf_init_user(dmu_buf_user_t *dbu, dmu_buf_evict_func_t *evict_func,
 543     dmu_buf_t **clear_on_evict_dbufp)
 544 {
 545         ASSERT(dbu->dbu_evict_func == NULL);
 546         ASSERT(evict_func != NULL);
 547         dbu->dbu_evict_func = evict_func;
 548 #ifdef ZFS_DEBUG
 549         dbu->dbu_clear_on_evict_dbufp = clear_on_evict_dbufp;
 550 #endif
 551 }
 552 #endif /* __lint */
 553 
 554 /*
 555  * Attach user data to a dbuf and mark it for normal (when the dbuf's
 556  * data is cleared or its reference count goes to zero) eviction processing.
 557  *
 558  * Returns NULL on success, or the existing user if another user currently
 559  * owns the buffer.
 560  */
 561 void *dmu_buf_set_user(dmu_buf_t *db, dmu_buf_user_t *user);
 562 
 563 /*
 564  * Attach user data to a dbuf and mark it for immediate (its dirty and
 565  * reference counts are equal) eviction processing.
 566  *
 567  * Returns NULL on success, or the existing user if another user currently
 568  * owns the buffer.
 569  */
 570 void *dmu_buf_set_user_ie(dmu_buf_t *db, dmu_buf_user_t *user);
 571 
 572 /*
 573  * Replace the current user of a dbuf.
 574  *
 575  * If given the current user of a dbuf, replaces the dbuf's user with
 576  * "new_user" and returns the user data pointer that was replaced.
 577  * Otherwise returns the current, and unmodified, dbuf user pointer.
 578  */
 579 void *dmu_buf_replace_user(dmu_buf_t *db,
 580     dmu_buf_user_t *old_user, dmu_buf_user_t *new_user);
 581 
 582 /*
 583  * Remove the specified user data for a DMU buffer.
 584  *
 585  * Returns the user that was removed on success, or the current user if
 586  * another user currently owns the buffer.
 587  */
 588 void *dmu_buf_remove_user(dmu_buf_t *db, dmu_buf_user_t *user);
 589 
 590 /*
 591  * Returns the user data (dmu_buf_user_t *) associated with this dbuf.
 592  */
 593 void *dmu_buf_get_user(dmu_buf_t *db);
 594 
 595 /* Block until any in-progress dmu buf user evictions complete. */
 596 void dmu_buf_user_evict_wait(void);
 597 
 598 /*
 599  * Returns the blkptr associated with this dbuf, or NULL if not set.
 600  */
 601 struct blkptr *dmu_buf_get_blkptr(dmu_buf_t *db);
 602 
 603 /*
 604  * Indicate that you are going to modify the buffer's data (db_data).
 605  *
 606  * The transaction (tx) must be assigned to a txg (ie. you've called
 607  * dmu_tx_assign()).  The buffer's object must be held in the tx
 608  * (ie. you've called dmu_tx_hold_object(tx, db->db_object)).
 609  */
 610 void dmu_buf_will_dirty(dmu_buf_t *db, dmu_tx_t *tx);
 611 
 612 /*
 613  * Tells if the given dbuf is freeable.
 614  */
 615 boolean_t dmu_buf_freeable(dmu_buf_t *);
 616 
 617 /*