Improve zvol symlink handling.

Change the zvol helper program to replace any embedded spaces
in the pool or dataset names with '+' to ensure we have valid
symlinks.

The '+' character was choosen because it is not a valid character
for a dataset name but it is allowed by udev.  This ensures that
all dataset names with an embedded space will be translated to
a unique /dev/zvol/ symlink.

Signed-off-by: Dan Swartzendruber <dswartz@druber.com>
Signed-off-by: Darik Horn <dajhorn@vanadac.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #2834
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 the current 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).
  + initiator   Allow only this initiator to bind to target.
                Currently only availible for LIO, STGT and SCST.
  + authname    Global user to use in binds on targets.
  + authpass    Password for global user.
* 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.
+ Split iSCSI implementation into their own separate source files
+ 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.
= The 'initiator', 'authname' and 'authpass' option might have some issues:
  It will make the compatibility between the different iSCSI implementations
  questionable - can't switch between them easily (it will ONLY be availible
  in ZoL). I COULD make the option be silently ignored (instead of forcibly
  rejected if not availible).

  But that still doesn't solve the (possible) problem between ZoL and
  OpenZFS/Illumos (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
* 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...
2790: turbo/sharenfs_rewrite - Rewrite of nfs.c to keep options per host separated.

Example: Setting sharenfs to

  "rw=@192.168.69.8,no_subtree_check,no_root_squash,ro=@192.168.69.45,no_subtree_check,root_squash"

will create the option string

  sec=sys,ro,no_subtree_check,no_root_squash,mountpoint,no_subtree_check,no_root_squash,no_subtree_check,root_squash

which is then run on _each host_!. Part of this is "default" options.

Instead, go through the option string, look for hosts and then make
sure options for each host is kept apart by using a linked list
(maintained by the ZFS list_* functions). Also, the default options
should only be added to the list of options if setting sharenfs to
"on".

Closes: #2773
2087: turbo/initramfs - Initramfs scripts for ZoL.
These are taken from the Debian GNU/Linux packages distributed by ZoL, but have been
merged with the dracut scripts - one code base for basically the same thing.

* Supports 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.
* Support all currently used kernel command line arguments
  All the different distributions have their own standard on what to specify
  on the kernel command line to boot of a ZFS filesystem.
* Allows mounting a rootfs with mountpoint=legacy set
* Don't auto import pools when loading module - keep better control of the imports.
* Only try to import pool if it haven't already been imported
  + This will negate the need to force import a pool that have not been exported cleanly.
  + Support exclusion of pools to import by setting ZFS_POOL_EXCEPTIONS in /etc/default/zfs.
* Support mounting additional filesystems (such as /var and /usr etc) by
  setting ZFS_INITRD_ADDITIONAL_DATASETS in /etc/default/zfs.
* Include /etc/modprobe.d/{zfs,spl}.conf in the initrd if they exist.
* 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.
  + Add /dev as a last ditch attempt.
* Use /sbin/modprobe instead of built-in (BusyBox) modprobe.
  This gets rid of the message "modprobe: can't load module zcommon".
  Thanx to pcoultha for finding this.
2106: turbo/fixes.init_total_rewrite - 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. Include /dev as a last ditch attempt.
+ 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.
+ Check to make sure we're root (or have root access - sudo).
+ Use 'free -m' instead of 'free -h' which don't seem to be available everywhere.
+ Get the whole dmesg log, just for completeness
+ Modify SPL/ZFS version retrieval.
2378: turbo/issue-1082 - Support parallel build trees (VPATH 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.

Closes: #1082

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
2376: turbo/issue-2033 - Remove the 'remove modules.* file(s) after modules installed.

Exclude the /lib/modules/*/modules.* files - they are regenerated
at module pkg install by depmod.
2512: turbo/issue-2448 - 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
2674: 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
2669: 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>
2767: turbo/illumos-5213 - panic in metaslab_init due to space_map_open returning ENXIO

Reviewed by: Matthew Ahrens mahrens@delphix.com
Reviewed by: George Wilson george.wilson@delphix.com

References:
    https://www.illumos.org/issues/5213
    https://reviews.csiden.org/r/110

Porting notes:

For the Linux port, KM_SLEEP was repplaced with KM_PUSHPAGE.

Ported by: Turbo Fredriksson <turbo@bayour.com>
1867: behlendorf/issue-1350 - Adds support for property overrides (WIP)

Adds support for property overrides (-o property=value), property
excludes (-x property) and dataset limits (-l <volume|filesystem>)
to zfs receive.

Both -o and -x options mirror the functionality already available
in Oracle's ZFS implementation which is also mentioned in the
upstream feature request #2745:

The -l option allows receive to be limited to specific datasets within
the stream effectively allowing partial restores from a multi dataset
stream.

References:
  https://www.illumos.org/issues/2745

Ported-by: Milan-Benes
Issue #1350

NOTES: Man page updates missing and must be added, see patch:
http://blog.multiplay.co.uk/dropzone/freebsd/zfs-recv-properties.patch
2479: bprotopopov/zvol-scale - zvol_create_minors(): check snapdev before traversing snapshots
2129: tuxoko/abd - Move ARC data buffers out of vmalloc

* Add compatibility layer for {kmap,kunmap}_atomic
  Starting from linux-2.6.37, {kmap,kunmap}_atomic takes 1 argument instead of 2.
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Introduce ABD: linear/scatter dual typed buffer for ARC
  zfsolinux currently uses vmalloc backed slab for ARC buffers. There are some
  major problems with this approach. One is that 32-bit system have only a
  handful of vmalloc space. Another is that the fragmentation in slab will easily
  trigger OOM in busy system.

  With ABD, we use scatterlist to allocate data buffers. In this approach we can
  allocate in HIGHMEM, which alleviates vmalloc space pressure on 32-bit. Also,
  we don't have to rely on slab, so there's no fragmentation issue.

  But for metadata buffers, we still uses linear buffer from slab. The reason for
  this is that there are a lot of *_phys pointers directly point to metadata
  buffers. So it's kind of impractical to change all those code. And metadata
  should only be a small percentage so we probably don't have to worry about
  the 32-bit and fragmentation issue, right?

  Currently, ABD is not enabled and its API will treat them as normal buffers.
  We will enable it once all relevant code is modified to use the API.
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Modify/Add incremental checksum function for abd_iterate_rfunc
  Modify/Add incremental fletcher function prototype to match abd_iterate_rfunc
  callback type. Also, reduce duplicated code a bit in zfs_fletcher.c.
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Use abd_t in arc.h, dmu.h and zio.h
  1. Use abd_t in arc_buf_t->b_data, dmu_buf_t->db_data, zio_t->io_data and
     zio_transform_t->zt_orig_data
  2. zio_* function take abd_t for data
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Convert zio_checksum to ABD version
  1. Add checksum function for abd_t
  2. Use abd_t version checksum function in zio_checksum_table
  3. Make zio_checksum_compute and zio_checksum_error handle abd_t
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in arc.c, bpobj.c and bptree.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in dbuf.c, ddt.c, dmu*.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in dnode*.c, dsl_*.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in sa_impl.h, sa.c, space_map.c, spa.c, spa_history.c and zil.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in metaslab.c, zap*.c, zfs_fuid.c, zfs_sa.c and zfs_vnops.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in zio.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in vdev*.c sans vdev_raidz.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle abd_t in vdev_raidz.c
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Handle ABD in ztest and zdb
  Use ABD API on related pointers and functions.(b_data, db_data, zio_*(), etc.)
  Note that the userspace still only support linear ABD.
  Suggested-by: DHE <git@dehacked.net>
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>

* Enable ABD
  Signed-off-by: Chunwei Chen <tuxoko@gmail.com>
2784: ryao/illumos-4950 - Files sometimes can't be removed from a full filesystem

Reviewed by: Adam Leventhal <adam.leventhal@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed by: Boris Protopopov <bprotopopov@hotmail.com>
Approved by: Dan McDonald <danmcd@omniti.com>
Ported-by: Richard Yao <richard.yao@clusterhq.com>

Porting notes:
1. ZoL currently does not log discards to zvols, so the portion of this patch that
modifies the discard logging to mark it as freeing space has been discarded.

2. may_delete_now had been removed from zfs_remove() in ZoL. It has been
reintroduced.