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...
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>
* 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).
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.
Add a unique "eid" value to all zevents

Tagging each zevent with a unique monotonically increasing EID
(Event IDentifier) provides the required infrastructure for a user
space daemon to reliably process zevents.  By writing the EID to
persistent storage the daemon can safely resume where it left off
in the event stream when it's restarted.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Add zpool_events_seek() functionality

The ZFS_IOC_EVENTS_SEEK ioctl was added to allow user space callers
to seek around the zevent file descriptor by EID.  When a specific
EID is passed and it exists the cursor will be positioned there.
If the EID is no longer cached by the kernel ENOENT is returned.
The caller may also pass ZEVENT_SEEK_START or ZEVENT_SEEK_END to seek
to those respective locations.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Clarify zpool_events_next() comment

Due to the very poorly chosen argument name 'cleanup_fd' it was
completely unclear that this file descriptor is used to track the
current cursor location.  When the file descriptor is created by
opening ZFS_DEV a private cursor is created in the kernel for the
returned file descriptor.  Subsequent calls to zpool_events_next()
and zpool_events_seek() then require the file descriptor as an
argument to reposition the cursor.  When the file descriptor is
closed the kernel state tracking the cursor is destroyed.

This patch contains no functional change, it just changes a
few variable names and clarifies the documentation.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Add defs for makefile installation dir vars

Add macro definitions to AM_CPPFLAGS to propagate makefile installation
directory variables for libexecdir, runstatedir, sbindir, and
sysconfdir.

https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Installation-Directory-Variables.html

  A corollary is that you should not use these variables except
  in makefiles. For instance, instead of trying to evaluate
  datadir in configure and hard-coding it in makefiles using e.g.,
  'AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])',
  you should add -DDATADIR='$(datadir)' to your makefile's definition
  of CPPFLAGS (AM_CPPFLAGS if you are also using Automake).

The runstatedir directory is for "installing data files which the
programs modify while they run, that pertain to one specific machine,
and which need not persist longer than the execution of the program".

https://www.gnu.org/prep/standards/html_node/Directory-Variables.html

It will be defined by autoconf 2.70 or later, and default to
"$(localstatedir)/run".

http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commit;h=a197431414088a417b407b9b20583b2e8f7363bd

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Initial implementation of zed (ZFS Event Daemon)

zed monitors ZFS events.  When a zevent is posted, zed will run any
scripts that have been enabled for the corresponding zevent class.
Multiple scripts may be invoked for a given zevent.  The zevent
nvpairs are passed to the scripts as environment variables.

Initial scripts have been developed to log events to syslog and send
email in response to checksum, resilver.finish, and scrub.finish
events.  By default, email will only be sent if the ZED_EMAIL variable
is configured in zed.rc.

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Issue #2

Make command line guid parsing more tolerant

Several of the zfs utilities allow you to pass a vdev's guid rather
than the device name.  However, the utilities are not consistent in
how they parse that guid.  For example, 'zinject' expects the guid
to be passed as a hex value while 'zpool replace' wants it as a
decimal.  The user is forced to just know what format to use.

This patch improve things by making the parsing more tolerant.
When strtol(3) is called using 0 for the base, rather than say
10 or 16, it will then accept hex, decimal, or octal input based
on the prefix.  From the man page.

    If base is zero or 16, the string may then include a "0x"
    prefix, and  the number  will  be read in base 16; otherwise,
    a zero base is taken as 10 (decimal) unless the next character
    is '0', in which case it  is  taken as 8 (octal).

NOTE: There may be additional conversions not caught be this patch.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Add missing DATA_TYPE_STRING_ARRAY output

This functionality has always been missing.  But until now there
were no zevents which included an array of strings so it wasn't
missed.  However, that's now changed so to ensure this information
is output correctly by 'zpool events -v' the DATA_TYPE_STRING_ARRAY
has been implemented.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Add automatic hot spare functionality

When a vdev starts getting I/O or checksum errors it is now
possible to automatically rebuild to a hot spare device.

To cleanly support this functionality in a shell script some
additional information was added to all zevent ereports which
include a vdev.  This covers both io and checksum zevents but
may be used but other scripts.

In the Illumos FMA solution the same information is required
but it is retrieved through the libzfs library interface.
Specifically the following members were added:

  vdev_spare_paths  - List of vdev paths for all hot spares.
  vdev_spare_guids  - List of vdev guids for all hot spares.
  vdev_read_errors  - Read errors for the problematic vdev
  vdev_write_errors - Write errors for the problematic vdev
  vdev_cksum_errors - Checksum errors for the problematic vdev.

By default the required hot spare scripts are installed but this
functionality is disabled.  To enable hot sparing uncomment the
ZED_SPARE_ON_IO_ERRORS and ZED_SPARE_ON_CHECKSUM_ERRORS in the
/etc/zfs/zed.d/zed.rc configuration file.

These scripts do no add support for the autoexpand property. At
a minimum this requires adding a new udev rule to detect when
a new device is added to the system.  It also requires that the
autoexpand policy be ported from Illumos, see:

  https://github.com/illumos/illumos-gate/blob/master/usr/src/cmd/syseventd/modules/zfs_mod/zfs_mod.c

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>

Fix zed read of new state file

This fixes zed_conf_read_state() when reading a new state file,
arising from either the initial run or from use of the '-Z'
command-line option.  A read of 0 bytes is not considered an error,
and now resets the eid to 0.

Create a ZED init script

Create a script that mails admin (if ZFS_EMAIL is set) if a vdev fails probe.

Create a script to notify admin(s) that a vdev have been removed.

Make sure we detect/find a device which may (or may not) have '-part?' in it's name.

If email is configured, send a mail after replace is complete.
Take both success and failure of a replace into account when sending email.

Don't close STDERR. This only works when zed runs in the forground.
That might be enough to iron out any problems in the script.

Fix 'cat | mail' pipe.
Kept getting:

/etc/zfs/zed.d/scrub.finish-email.sh: 15: /etc/zfs/zed.d/scrub.finish-email.sh: A: not found
/etc/zfs/zed.d/scrub.finish-email.sh: 17: /etc/zfs/zed.d/scrub.finish-email.sh: eid:: not found
/etc/zfs/zed.d/scrub.finish-email.sh: 18: /etc/zfs/zed.d/scrub.finish-email.sh: host:: not found
/etc/zfs/zed.d/scrub.finish-email.sh: 19: /etc/zfs/zed.d/scrub.finish-email.sh: pool:: not found
/etc/zfs/zed.d/scrub.finish-email.sh: 20: /etc/zfs/zed.d/scrub.finish-email.sh: EOF: not found

ZED_SPARE_ON_IO_ERRORS is now a boolean - I/O errors is bad!
Add a support script with useful functions.

Include the class in the log message when invoking a script.

Create a script to notify admin(s) that a pool have been destroyed.

Initial attempt to document events (subclasses) and their payload.
In no way complete - most have been trial and error and some deducing
what they could mean. It needs more information, from someone that
knows the code better. But it's a start...

Create a zpool.export event (with corresponding script)
that's triggered when a pool is exported.

Create a zpool.import event (with corresponding script)
that's triggered when a pool is imported.

Create a script to notify admin(s) that a pool have data errors.

Detaching a device should issue a vdev.detach, not a vdev.remove.
Issue a vdev.remove when a vdev have been removed from a pool.