Replace zed's use of malloc with calloc

When zed allocates memory via malloc(), it typically follows that
with a memset().  However, calloc() implementations can often perform
optimizations when zeroing memory:

https://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc

This commit replaces zed's use of malloc() with calloc().

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #2736
Refer to ZED's scripts as ZEDLETs

The executables invoked by the ZED in response to a given zevent
have been generically referred to as "scripts".  By convention,
these scripts have aimed to be /bin/sh compatible for reasons of
portability and comprehensibility.  However, the ZED only requires
they be executable and (ideally) capable of reading environment
variables.  As such, these scripts are now referred to as ZEDLETs
(ZFS Event Daemon Linkage for Executable Tasks).

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #2735
Perform whole-page page truncation for hole-punching under a range lock

As an attempt to perform the page truncation more optimally, the
hole-punching support added in 223df0161fad50f53a8fa5ffeea8cc4f8137d522
truncated performed the operation in two steps: first, sub-page "stubs"
were zeroed under the range lock in zfs_free_range() using the new
zfs_zero_partial_page() function and then the whole pages were truncated
within zfs_freesp().  This left a window of opportunity during which
the full pages could be touched.

This patch closes the window by moving the whole-page truncation into
zfs_free_range() under the range lock.

Signed-off-by: Tim Chase <tim@chase2k.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #2733
=> 1099: turbo/iscsi - This is iSCSI sharing for ZoL (shareiscsi).
* Supports the following iSCSI implementations (in this order of discovery):
  + IET		http://iscsitarget.sourceforge.net
  + STGT	http://stgt.berlios.de
  + SCST	http://scst.sourceforge.net
    + Requires that SCST was compiled with /sys support (the default).
  + LIO		http://linux-iscsi.org
* Will refuse to unshares an active target (one with sessions).
* Supports the following options to the 'shareiscsi' property:
  + name/iqn	Full iSCSI Qualified Name (IQN), including identifier.
		This is generated by iscsi_generate_target():
		+ Uses the content of an optional /etc/iscsi_target_id file:
		  Example: iqn.YYYY-MM.tld.domain
		+ If this file doesn't exist, it uses todays year, month and
		  domain name to generate the iqn.
		+ The dataset name is appended at the end of the iqn, with
		  slashes replaced with dots.
		  => <iqn>:<dataset>
  + lun		LUN (0-16384)
		Default: 0 (1 for STGT)
  + type	Share mode (fileio, blockio, nullio, disk, tape)
		STGT: ssc, pt
		Default: blockio (disk for STGT)
  + iomode	IO mode (wb, wt, ro)
		STGT: rdwr, aio, mmap, sg, ssc
		Default: wt (rdwr for STGT)
  + blocksize	Logical block size (512, 1024, 2048, 4096)
		Default: Volume blocksize, 4096 if not usable.
		NOTE: Currently not supported for STGT (doesn't seem to be
		      an option for it in tgtadm).
* If not called with a 'name/iqn' value, then force setting it.
  This so that the IQN doesn't change every month (when it is
  regenerated again). It will be generated by iscsi_generate_target()
  (see above).

NOTE:
+ Moved nfs.c:foreach_nfs_shareopt() to libshare.c:foreach_shareopt()
  so that it can be (re)used in smb.c and iscsi.c.
+ Use the list_{create,insert}() etc for keeping tabs of the linked lists
  instead of using a home-made version.
+ A half second delay had to be added in lib/libzfs/libzfs_mount.c:zfs_unmount()
  after the successful unshare. This to avoid 'dataset busy' when destroying
  recursivly.
+ Fix some gcc-4.8.1 compile fixes. Thanx to munix9.
+ Fix error in manpage.
+ Don't set last char in domainname to NULL willy-nilly. Only if newline.
+ Instead of blindingly generate an IQN, use opts->name if it's set.
+ Split iSCSI implementation into their own separate source files
+ Add an additional 'initiator' option to shareiscsi that is only available for LIO.
  One possible problem with this commit: It will make the compatibility
  between the different iSCSI implementations questionable - can't
  switch between them easily (it will ONLY be availible in ZoL/LIO).  I
  COULD make the option be silently ignored (instead of forcibly
  rejected if used and not LIO).

  But that still doesn't solve the (possible) problem between ZoL and
  OpenZFS (setting the option, will possibly introduce problems when
  importing the pool on something else than ZoL).

  So some more discussion might be needed..
+ Misc iSCSI fixes/cleanups/attempts to get 'zfs rename' to work.
  !! Doesn't - a 'zfs rename' works, but most often than not, the rename
  !! IOCTL hangs. Not every time and not always on the first dataset being
  !! renamed... Seems a little to random to me.
=> 1476: turbo/smbfs_registry-shares - Rewrite of the Libshare/SMBFS sharing property (sharesmb).

Rewrite of the Libshare/SMBFS
* Add support for options to 'sharesmb'.
  Fixes: #1182
  + Move nfs.c:foreach_nfs_shareopt() to libshare.c:foreach_shareopt()
    so that it can be (re)used in smb.c (and later iscsi.c).
  + Call net(8) with the guest_ok option.
  + Rewrite the sharesmb part of the zfs(8) manpage.
    + Add more examples
    + Inform about the (new) options to sharesmb.
  + Add STDERR_VERBOSE to libzfs_run_process() so that we
    can catch any errors from net(8).
  + Don't call smb_retrieve_shares() after enabling a share.
    No point, it's done when really needed anyway.
  + Support 'y', 'yes' and 'true' for guest_ok.
  * If sharesmb=on, just exit cleanly directly at the top of
    get_smb_shareopts_cb().
  * Remove param 2 and 3 from smb_enable_share_one(). That is
    availible in the first param - the impl_share struct.
  * Extra debugging added in smb_enable_share_one() - print
    all our options just before we call net(8).
* Add support for optional, extra script /sbin/zfs_share_smbfs
  to run after shareing is done.
  + Add an example zfs_share_smbfs script
* The option sharesmb should not be inherited by childs.
  Samba is perfectly able to show child file systems...
* Use dataset name in comment, not the smb share name.
* Use REGISTRY SHARES instead of USER SHARES
  + These are a lot more configurable (more possibility to customize the share).
  + Remove the 'acl' option and replace it with 'writeable' instead.
  + On some machines, there is a "global" share. This isn't really a share,
    but the possibility to modify global options. So first get a line of
    _real_ shares using 'net conf listshares' and _then_ cyckle through
    them retrieving info with 'net conf showshare <share>'.
  + Add an example on how to modify a share after ZFS share and what man
    pages to use for more info.
* Speedups (less share retreivals) and make sure the 'net' command is executable.
  + Since smb_shares is global, we only retreive shares if it's NULL
    in smb_retrieve_shares(). It should limit the number of times we
    fork() and read shares (which takes time).
  + If the net command don't exist (is executable), don't even register
    the smb fstype in libshare_smb_init().
  + access() fix - check if NET_CMD_PATH is _executable_ not just 'exists'.
* Run cstyle.pl to fix any code style problems.
* Output error if 'net' can't talk to samba.
* Improve error message if 'net' isn't executable.
* Move the checks from smb_available() to libshare_smb_init()
  to avoid doing them multiple times.
* Convert homemade linked list to the ZFS versions list_* functions (which uses a link_t).
* Script to list SMB shares added.
* Comment out a call to zfs_unshare_smb(). Don't seem to be needed.
  This because zfs_unshare_smb() is called twice if setting 'sharesmb=off' - once
  'somewhere else' and once here (this one AFTER the share have been unshared). That
  makes this one fail! Don't know if this is correct, but it works to comment out this...
=> 2087: turbo/initramfs - Initramfs scripts for ZoL.

Initramfs scripts for ZoL.
* Put in /usr/share/initramfs-tools/ or /etc/initramfs-tools/.
* With pull request #1476 (not yet merged) comes a verbose warning
  if /usr/bin/net doesn't exist or isn't executable. Just create
  a dummy...
* If user haven't specified pool, but bootfs then use the first part
  of the bootfs path as pool name.
* Add support for booting of a ZFS snapshot.
  Do this by cloning the snapshot into a dataset. If this the resulting
  dataset already exists, destroy it. Then set mountpoint=/ on that
  dataset and mount it on root.
  + If snapshot does not exist, use base dataset (the part before '@')
    as boot filesystem instead.
* Add support for more kernel command line arguments (ideas taken from
  the Fedora/Redhat dracut script):
  + (zfs_force|zfs.force|zfsforce)=(on|yes|1) (ignore case).
  + root=zfs:AUTO => try to find rootfs automatically.
  + root=zfs:<dataset>
  + root=ZFS=<dataset>
* Remove the existing '-f' option to 'zpool import', instead let
  this be controlled on the kernel command line.
* Do not force-set mountpoint=/ - should (??) not be nessesary.
* Support mounting a rootfs with mountpoint=legacy
* Support both RPM based and DEB based system by removing the logic
  from the dracut script and instead use the logic in scripts/zfs-initramfs/scripts/zfs
  for both Debian GNU/Linux (etc) and RedHat/Fedora (etc).
* Only run the local-* script(s) if/when it/they exists.
* Don't auto import pools when loading module - keep better control of the imports.
* If called with only 'rpool' (or only 'zfs-bootfs'), then fake it by setting 'root=zfs:AUTO'
  (to avoid duplication of code etc) so that it will be auto detected later.
  + If pool is specified and we're auto discovering, don't go through ALL pools, only the one we specified.
* Rewrite the auto detection of bootfs in the auto detector.
* Only try to import pool if it haven't already been imported (in the auto detector).
* Add support for only having 'root=pool/dataset' option.
* Remove the use of setting 'ZFS_RPOOL=rpool'. No longer nessesary.
* Check if /scripts/local-* is either file or directory - should be dir, but just to be safe check both.
* Make wait_for_udev wait for 10 seconds.
* Add a configurable ZFS_INITRD_POST_MODPROBE_SLEEP (set in /etc/default/zfs) after the modprobe.
* Merge https://github.com/zfsonlinux/zfs/pull/2196.
  Remove lines that contain only a hyphen (match '^-$' instead of '-').
* Add a 'default' file to go in '/etc/default/zfs'.
* Support mounting additional filesystems (such as /var and /usr etc)
  in the initrd using ZFS_INITRD_ADDITIONAL_DATASETS set in /etc/defaults/zfs.
* Add exceptions to pool imports.
  + Both for init and initrd script.
* Make sure that the zpool.cache file is actually copied onto the initrd!!
* Include /etc/modprobe.d/{zfs,spl}.conf in the initrd if they exist.
* Export all pools found after the modprobe - zfs_autoimport_disable=1'
  don't seem to be working in >= 0.6.3.
  + This snippet will go away as soon as I can figure out why it doesn't,
    it's ugly!!
* Only try /dev/disk/by-* in the initrd if USE_DISK_BY_ID is set.
  + Try this first, then fallback to using the cache file if that exist.
=> 2106: turbo/fixes.init_total_rewrite - Base scripts on the init scripts included with Debian GNU/Linux

Base scripts on the init scripts included with Debian GNU/Linux
then take code from the already existing ones, trying to merge
them into one.
+ NFS server service is just called 'nfs' in Redhat based system, and
  nfs-kernel-server on Debian GNU/Linux based.
+ Merge https://github.com/zfsonlinux/zfs/pull/2148 - Inform OpenRC that ZFS uses mtab.
+ Stop after umountfs
+ When stopping, don't check for LOCKDIR/zfs-mount - it won't be mounted,
  so the file won't exist making the script not run.
+ Add a configurable ZFS_INITRD_POST_MODPROBE_SLEEP used in the initrd to sleep after the modprobe.
+ The import function, do_import(), imports pools by name instead of '-a' [all].
  + Test all '/dev/disk/by-*' dirs for import.
+ Fallback on importing the pool using the cache file (if it exists) if the
  'by-id' didn't work.
+ Add exceptions to pool imports.
=> 2726: turbo/debug_documentation - Remove *BSD example on how to retrieve debug log with working Linux commands.
=> 2727: turbo/zol-bug-report - Initial version of a ZoL "report issue" script.
=> 1082: turbo/issue-1082 - Support 'out of tree' builds

* Support 'out of tree' builds by adding the 'src' and 'obj' variables
  and removing the absolute path to the build results to the Makefile.in
  files below the module directory.

* For VPATH builds (addition for #1082):
  + Add '../../spl' to spl source directory list to search
  + Do a find in the found spl source for spl_config.h

Closes: #1082
=> 2376: turbo/issue-2033 - Exclude the /usr/lib/modules/*/modules.* files
                            they are regenerated at module pkg install by depmod.
=> 2512: turbo/issue-2448 - Default to --with-blkid in RPM packaging
                          - Make linking with and finding libblkid required

Default to --with-blkid in RPM packaging

Support for libblkid has been in the ZoL for years but has been
disabled by default in the packaging.  When libblkid is disabled
the 'zpool create' command will not warn you if you're going to
overwrite an existing filesystem.  For this reason the default
behavior has been changed to require libblkid.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #2448

Make linking with and finding libblkid required (so that creating
pools on devices with existing filesystem will get a warning).
Use '--without-blkid' to explicitly disable this.

Signed-off-by: Turbo Fredriksson <turbo@bayour.com>
Closes: #2448
=> 2683: turbo/issue-2562 - Add a vdev_size payload to the ereport if vdev_path is set.

Signed-off-by: Turbo Fredriksson <turbo@bayour.com>

Closes: #2562
=> 2668: turbo/issue-2666 - Allow for "zfs receive" to skip existing snapshots
                            by adding the option "-s" to zfs receive.

Signed-off-by: Turbo Fredriksson <turbo@bayour.com>

Closes: #2666
=> 2673: turbo/issue-2673 - Mount filesystem as soon as it's been received

Instead of storing the filesystem just received in top_zfs,
mount and share it directly in zfs_receive_one().

This will make a filesystem be mounted as soon as it's been received,
instead of waiting to the very end - which might take days.

Signed-off-by: Turbo Fredriksson <turbo@bayour.com>

Closes: #2673
=> 3693: turbo/illumos-3693 - restore_object uses at least two transactions to restore an object

restore_object uses at least two transactions to restore an object:

  * one transaction is used for dmu_object_claim
  * another transaction is used to set compression, checksum and most
    importantly bonus data
  * furthermore dmu_object_reclaim internally uses multiple transactions
  * dmu_free_long_range frees chunks in separate transactions
  * dnode_reallocate is executed in a distinct transaction

The fact the dnode_allocate/dnode_reallocate are executed in one
transaction and bonus (re-)population is executed in a different
transaction may lead to violation of ZFS consistency assertions if the
transactions are assigned to different transaction groups.  Also, if
the first transaction group is successfully written to a permanent
storage, but the second transaction is lost, then an invalid dnode may
be created on the stable storage.

Reviewed by: Christopher Siden christopher.siden@delphix.com
Reviewed by: George Wilson george.wilson@delphix.com

Original authors: Matthew Ahrens and Andriy Gapon

References:
  https://www.illumos.org/projects/illumos-gate//issues/3693
  https://reviews.csiden.org/r/90

Ported by: Turbo Fredriksson <turbo@bayour.com>
=> 5134: turbo/illumos-5134 - if ZFS_DEBUG or debug= is set, libzpool should enable debug prints

Reviewed by: Christopher Siden christopher.siden@delphix.com
Reviewed by: Adam Leventhal adam.leventhal@delphix.com

References:
  https://www.illumos.org/projects/illumos-gate/issues/5134
  https://github.com/illumos/illumos-gate/commit/7fa49ea

Porting notes:
  Run dprintf_setup() in main.

Ported by: Turbo Fredriksson <turbo@bayour.com>
=> 5150: turbo/illumos-5150 - zfs clone of a defer_destroy snapshot causes strangeness

When a clone is created of a snapshot that has been marked for
deferred destroy (with "zfs destroy -d"), the clone "inherits" the
defer_destroy flag from the origin, and any snapshots of the clone
"inherit" the defer_destroy flag from the clone. This causes a strange
situation where the clone's snapshots are marked for defer_destroy but
they have no holds or clones. If the clone's snapshot gets a hold or
clone, which is then deleted, we will honor the incorrectly-set
defer_destroy flag and delete the snapshot!

Steps to reproduce:

  * zpool create test c1t1d0
  * zfs create test/fs
  * zfs snapshot test/fs@a
  * zfs clone test/fs@a test/clone
  * zfs destroy -d test/fs@a
  * zfs clone test/fs@a test/clone2
  * zfs snapshot test/clone2@a
  * zfs hold hld test/clone2@a
  * zfs release hld test/clone2@a
  * zfs list -r -t all test

  <test/clone2@a has been destroyed>

We noticed that this causes dcenter to get very confused, because it
treats snapshots that are marked defer_destroy as not existing. So it
won't see any snapshots of the clone that's marked defer_destroy.

Reviewed by: George Wilson george.wilson@delphix.com
Reviewed by: Christopher Siden christopher.siden@delphix.com
Reviewed by: Max Grossman max.grossman@delphix.com

Original author: Matthew Ahrens

References:
  https://www.illumos.org/projects/illumos-gate//issues/5150
  https://reviews.csiden.org/r/93/

Ported by: Turbo Fredriksson <turbo@bayour.com>
=> 5162: turbo/illumos-5162 - zfs recv should use loaned arc buffer to avoid copy

5162 zfs recv should use loaned arc buffer to avoid copy
Reviewed by: George Wilson george.wilson@delphix.com\
Reviewed by: Christopher Siden christopher.siden@delphix.com\

References:
  https://www.illumos.org/issues/5162
  https://reviews.csiden.org/r/96/

Porting notes:
  Fix spelling error 's/arena/area/' in dmu.c.

Ported by: Turbo Fredriksson <turbo@bayour.com>
=> 4958: turbo/illumos-5164+5165 - zdb trips assert on pools with ashift >= 0xe

Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Max Grossman <max.grossman@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Approved by: Garrett D'Amore <garrett@damore.org>

References:
  https://www.illumos.org/issues/4958
  https://github.com/illumos/illumos-gate/commit/2a104a5

Porting notes:

Keep the ZIO_FLAG_FASTWRITE define, because that is used in
Linux but not in *BSD.

Ported by: Turbo Fredriksson <turbo@bayour.com>

Illumos 5164+5165 - space_map fixes

5164 space_map_max_blksz causes panic, does not work
5165 zdb fails assertion when run on pool with recently-enabled spacemap_histogram feature
Reviewed by: George Wilson george.wilson@delphix.com
Reviewed by: Christopher Siden christopher.siden@delphix.com

References:
  https://www.illumos.org/issues/5164
  https://www.illumos.org/issues/5165
  https://github.com/illumos/illumos-gate/commit/b1be289

Ported by: Turbo Fredriksson <turbo@bayour.com>