Override default SPA config location via environment

When using zdb with non-default SPA config file it is not convenient
to add -U <non-default-config-file-path> all the time. This commit
introduces support for setting/overriding SPA config location via
environment variable 'SPA_CONFIG_PATH'.

If -U flag is specified in the command line it will override any other
value as usual.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1545
Fix zfs_sb_teardown/zfs_resume_fs NULL dereference

Fix a pair of conditions in which a concurrent umount can cause
NULL pointer dereferences:

* zfs_sb_teardown - prevent a NULL dereference by not calling
                    dmu_objset_pool with a null z_os.

* zfs_resume_fs - don't try to unmount with a null z_os.  This
                  change makes the ZoL code more consistent
                  with both Illumos and FreeBSD.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1543
kmem_zalloc(..., KM_SLEEP) will never fail

By definitition these allocations will never fail.  For
consistency with the rest of the code remove this dead error
handling code.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1558
Readd zfs_holey() from OpenSolaris

This patch restores the zfs_holey() function from OpenSolaris.
This was removed by commit 3558fd7 because it wasn't clear we
had a use for it in ZoL.  However, this functionality is a
prerequisite for adding SEEK_DATA/SEEK_HOLE support to the ZPL.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <ryao@gentoo.org>
Issue #1384
Add SEEK_DATA/SEEK_HOLE to lseek()/llseek()

The approach taken was the rework zfs_holey() as little as
possible and then just wrap the code as needed to ensure
correct locking and error handling.

Tested with xfstests 285 and 286.  All tests pass except for
7-9 of 285 which try to reserve blocks first via fallocate(2)
and fail because fallocate(2) is not yet supported.

Note that the filp->f_lock spinlock did not exist prior to
Linux 2.6.30, but we avoid the need for autotools check by
virtue of the fact that SEEK_DATA/SEEK_HOLE support was not
added until Linux 3.1.

An autoconf check was added for lseek_execute() which is
currently a private function but the expectation is that it
will be exported perhaps as early as Linux 3.11.

Reviewed-by: Richard Laager <rlaager@wiktel.com>
Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1384
Add zfs_sync_pass_* tunable parameters

Commit 55d85d5a8c45c4559a4a0e675c37b0c3afb19c2f (backport of
the upstream changes) replaced three hardcoded constants:

    #define SYNC_PASS_DEFERRED_FREE 2 /* defer frees after this pass */
    #define SYNC_PASS_DONT_COMPRESS 4 /* don't compress after this pass */
    #define SYNC_PASS_REWRITE       1 /* rewrite new bps after this pass */

with a tunable parameters:

    int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
    int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
    int zfs_sync_pass_rewrite = 2;       /* rewrite new bps starting in this pass */

This commit makes these tunables available as module parameters
in Linux.  They should only be used for performance analysis
because changing them can result in subtle and pathological
performance problems.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1562
Use MAXPATHLEN instead of sizeof in snprintf

This silences a GCC 4.8.0 warning by fixing a programming error
caught by static analysis:

../../cmd/ztest/ztest.c: In function ‘ztest_vdev_aux_add_remove’:
../../cmd/ztest/ztest.c:2584:33: error: argument to ‘sizeof’
  in ‘snprintf’ call is the same expression as the destination;
  did you mean to provide an explicit length?
  [-Werror=sizeof-pointer-memaccess]
  (void) snprintf(path, sizeof (path), ztest_aux_template,
                                 ^

Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1480
Illumos #3122 zfs destroy filesystem should prefetch blocks

3122 zfs destroy filesystem should prefetch blocks
Reviewed by: Christopher Siden <chris.siden@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Approved by: Garrett D'Amore <garrett@damore.org>

References:
  illumos/illumos-gate@b4709335aa83dcbfd0dba33c9be21fcabebd28e4
  https://www.illumos.org/issues/3122

Ported-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1565
Illumos #3498 panic in arc_read()

3498 panic in arc_read(): !refcount_is_zero(&pbuf->b_hdr->b_refcnt)
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Approved by: Richard Lowe <richlowe@richlowe.net>

References:
  illumos/illumos-gate@1b912ec7100c10e7243bf0879af0fe580e08c73d
  https://www.illumos.org/issues/3498

Ported-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1249
Fix parse_dataset error handling

A mount failure was accidentally introduced by commit 0c1171d
which reworked the parse_dataset() function to read pool names
from devices.  The error case where a label is read from the
device but the pool name/value pair doesn't exist was not
handled properly.  In this case we should fall back to the
previous behavior.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1560
Call zvol_create_minors() in spa_open_common() when initializing pool

There is an extremely odd bug that causes zvols to fail to appear on
some systems, but not others. Recently, I was able to consistently
reproduce this issue over a period of 1 month. The issue disappeared
after I applied this change from FreeBSD.

This is from FreeBSD's pool version 28 import, which occurred in
revision 219089.

Ported-by: Richard Yao <ryao@cs.stonybrook.edu>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #441
Issue #599
Cleanup zvol initialization code

The following error will occur on some (possibly all) kernels
because blk_init_queue() will try to take the spinlock before
we initialize it.

  BUG: spinlock bad magic on CPU#0, zpool/4054
   lock: 0xffff88021a73de60, .magic: 00000000,
   .owner: <none>/-1, .owner_cpu: 0
  Pid: 4054, comm: zpool Not tainted 3.9.3 #11
  Call Trace:
   [<ffffffff81478ef8>] spin_dump+0x8c/0x91
   [<ffffffff81478f1e>] spin_bug+0x21/0x26
   [<ffffffff812da097>] do_raw_spin_lock+0x127/0x130
   [<ffffffff8147d851>] _raw_spin_lock_irq+0x21/0x30
   [<ffffffff812c2c1e>] cfq_init_queue+0x1fe/0x350
   [<ffffffff812aacb8>] elevator_init+0x78/0x140
   [<ffffffff812b2677>] blk_init_allocated_queue+0x87/0xb0
   [<ffffffff812b26d5>] blk_init_queue_node+0x35/0x70
   [<ffffffff812b271e>] blk_init_queue+0xe/0x10
   [<ffffffff8125211b>] __zvol_create_minor+0x24b/0x620
   [<ffffffff81253264>] zvol_create_minors_cb+0x24/0x30
   [<ffffffff811bd9ca>] dmu_objset_find_spa+0xea/0x510
   [<ffffffff811bda71>] dmu_objset_find_spa+0x191/0x510
   [<ffffffff81253ea2>] zvol_create_minors+0x92/0x180
   [<ffffffff811f8d80>] spa_open_common+0x250/0x380
   [<ffffffff811f8ece>] spa_open+0xe/0x10
   [<ffffffff8122817e>] pool_status_check.part.22+0x1e/0x80
   [<ffffffff81228a55>] zfsdev_ioctl+0x155/0x190
   [<ffffffff8116a695>] do_vfs_ioctl+0x325/0x5a0
   [<ffffffff8116a950>] sys_ioctl+0x40/0x80
   [<ffffffff814812c9>] ? do_page_fault+0x9/0x10
   [<ffffffff81483929>] system_call_fastpath+0x16/0x1b
   zd0: unknown partition table

We fix this by calling spin_lock_init before blk_init_queue.

The manner in which zvol_init() initializes structures is
suspectible to a race between initialization and a probe on
a zvol. We reorganize zvol_init() to prevent that.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Open pools asynchronously after module load

One of the side effects of calling zvol_create_minors() in
zvol_init() is that all pools listed in the cache file will
be opened.  Depending on the state and contents of your pool
this operation can take a considerable length of time.

Doing this at load time is undesirable because the kernel
is holding a global module lock.  This prevents other modules
from loading and can serialize an otherwise parallel boot
process.  Doing this after module inititialization also
reduces the chances of accidentally introducing a race
during module init.

To ensure that /dev/zvol/<pool>/<dataset> devices are
still automatically created after the module load completes
a udev rules has been added.  When udev notices that the
/dev/zfs device has been create the 'zpool list' command
will be run.  This then will cause all the pools listed
in the zpool.cache file to be opened.

Because this process in now driven asynchronously by udev
there is the risk of problems in downstream distributions.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #756
Issue #1020
Issue #1234
Remove zfs-dracut and zfs-test dependencies

Remove from the zfs package the depenencies on the zfs-dracut and
zfs-test subpackages.  Neither of these packages are required for
normal operation and they bring in many unnecessary dependencies
during installation.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1395
Add conditional chkconfig to packaging

Prior to adopting the kmod style packaging the zfs packages
would conditionally invoke /sbin/chkconfig to create the
proper links for the init script.  This is done conditionally
because many distributions are moving away from SysV style
init scripts and we don't want to cause errors on those.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1376
Add conditional chkconfig to packaging

Unconditionally exit with zero to avoid returning failures
from the scriptlets.  This should have been part of the
previous ba661a6 commit.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1376
3.10 API change: block_device_operations->release() returns void

Linux kernel commit torvalds/linux@db2a144 changed the return type
of block_device_operations->release() to void.  Detect the expected
prototype and defined our callout accordingly.

Signed-off-by: Chris Dunlop <chris@onthe.net.au>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1494
Explicitly flush output at end of each zevent

For "zpool events -f" flush stdout to ensure the last zevent
is always printed immediately.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1568
Add zfs_autoimport_disable tunable

There are times when it is desirable for zfs to not automatically
populate the spa namespace at module load time using the pools
in the /etc/zfs/zpool.cache file.  The zfs_autoimport_disable
module option has been added to control this behavior.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #330
Fix the comment in zfs.h

The path to code is also changed in zfsonlinux.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issues #1566