Add MUTEX_SPIN to zfs_context.h

Signed-off-by: Richard Yao <ryao@gentoo.org>
Implement sgbufs

sgbufs use pages to store buffers. They are intended as a replacement
for slab allocation in ZIO buffers and will be proposed for inclusion
into Illumos once they are ready.

Adoption of sgbufs for buffers provides the following benefits:

1. The elimination of memory loss to SLAB fragmentation, which has been
observed to exceed 10% of system memory on some systems.

2. The ability to mmap() directly into ARC.

3. Opportunities to use page table manipulation when doing copies or
zeroes that involve page alignment.

Additional patches will be required to obtain each of these benefits.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Set zfs_arc_min to 4MB

Decrease the mimimum ARC size from 1/32 of total system memory
(or 64MB) to a much smaller 4MB.

1) Large systems with over a 1TB of memory are being deployed
   and reserving 1/32 of this memory (32GB) as the mimimum
   requirement is overkill.

2) Tiny systems like the raspberry pi may only have 256MB of
   memory in which case 64MB is far too large.

The ARC should be reclaimable if the VFS determines it needs
the memory for some other purpose.  If you want to ensure the
ARC is never completely reclaimed due to memory pressure you
may still set a larger value with zfs_arc_min.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Restructure zfs_readdir to fix regressions

This does the following:

1. It creates a uint8_t type value, which is initialized to DT_DIR on
dot directories and ZFS_DIRENT_TYPE(zap.za_first_integer) otherwise.
This resolves a regression where we return unintialized values as the
directory entry type on dot directories. It was introduced by commit
8170d281263e52ff33d7fba93ab625196844df36.

2. It restructures zfs_readdir code to use `uint64_t offset` like Illumos
instead of `loff_t *pos`. This resolves a regression where regression
where negative ZAP cursors were treated as if they were dot directories.

3. It restructures the function to more closely match the equivalent
function in Illumos and removes the unused variable outcount, which was
only used on Illumos.

Closes zfsonlinux/zfs#1750

Signed-off-by: Richard Yao <ryao@gentoo.org>
Implement File Attribute Support

We add support for lsattr and chattr. Only attributes common to both
Solaris and Linux are supported. These are 'a', 'd' and 'i'. File
attributes exclusive to Solaris are present in the ZFS code, but cannot
be accessed or modified through this method. That was the case prior to
this patch.

This commit removes the ZFS_IOC_GETFLAGS and ZFS_IOC_SETFLAGS macros in
recognition that this is not equivalent to the Solaris operation. The
resolution of issue zfsonlinux/zfs#229 should implement something
equivalent that will permit access and modification of Solaris-specific
attributes.

This resolves a regression caused by
88c283952f0bfeab54612f9ce666601d83c4244f that broke python's
xattr.list(). This broke Gentoo Portage's FEATURES=xattr, which depended
on this.

https://bugs.gentoo.org/show_bug.cgi?id=483516
Issue zfsonlinux/zfs#1691

Original-patch-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <ryao@gentoo.org>
Remove mc_fastwrite_lock

This lock unnecessarily serializes an important path. The fast write
variables are updated using atomic operations, which need no protection.
The code snippet being protected is already protected by the spa_lock.
We remove mc_fastwrite_lock and add an assertion to verify that we have
a spa_lock. This permits multiple threads to run through the
metaslab_alloc_dva() function in parallel as they do on other
implementations.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Reintroduce uio_prefaultpages()

This was accidentally removed by overzealous commenting.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Fix incorrect usage of strdup() in zfs_unmount_snap()

Modifying the length of a string returned by strdup() is incorrect
because strfree() is allowed to use strlen() to determine which slab
cache was used to do the allocation.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Fix order of function calls in zio_free_sync()

The resolution of a merge conflict when merging Illumos #3464 caused us
to invert the order couple of function calls in zio_free_sync() versus
what they are in Illumos.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add missing copyright notices from Illumos

This resolves merge conflicts when merging Illumos #3588 and Illumos #4047.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Switch to C99

Upstream Illumos uses C99, but we have been building as C89. This
required backporting patches from Illumos from C99 to C89. There are no
known ABI compatibility issues when mixing C89 and C99 code. so lets
just build as C99 and save ourselves some trouble.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add missing code to zfs_debug.{c,h}

Signed-off-by: Richard Yao <ryao@gentoo.org>
6939941 problem with moving files in zfs

Porting notes:

This commit was so old that only two lines applied to the modern
codebase.

Signed-off-by: Richard Yao <ryao@gentoo.org>
6977619 NULL pointer deference in sa_handle_get_from_db()

Ported-by: Richard Yao <ryao@gentoo.org>
Illumos #3006

3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument
     is zero

Reviewed by Matt Ahrens <matthew.ahrens@delphix.com>
Reviewed by George Wilson <george.wilson@delphix.com>
Approved by Eric Schrock <eric.schrock@delphix.com>

References:
  illumos/illumos-gate@fb09f5aad449c97fe309678f3f604982b563a96f
  https://illumos.org/issues/3006

Ported-by: Brian Behlendorf <behlendorf1@llnl.gov>
Illumos #3522

3522 zfs module should not allow uninitialized variables
Reviewed by: Sebastien Roy <seb@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Approved by: Garrett D'Amore <garrett@damore.org>
Ported-by: Richard Yao <ryao@gentoo.org>

References:

https://www.illumos.org/issues/3522
illumos/illumos-gate@d5285cae913f4e01ffa0e6693a6d8ef1fbea30ba

Porting notes:

1. ZFSOnLinux had already addressed many of these issues because of its use of
-Wall. However, the manner in which they were addressed differed. The illumos
fixes replace the ones previously made in ZFSOnLinux to reduce code
differences.

2. Part of the upstream patch made a small change to arc.c that might address
zfsonlinux/zfs#1334.

3. The initialization of aclsize in zfs_log_create() differs because vsecp is a
NULL pointer on ZFSOnLinux.

4. The changes to zfs_register_callbacks() were dropped because it has diverged
and needs to be resynced.
Illumos #3537

3537 want pool io kstats

Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Eric Schrock <eric.schrock@delphix.com>
Reviewed by: Sa?o Kiselkov <skiselkov.ml@gmail.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Brendan Gregg <brendan.gregg@joyent.com>
Approved by: Gordon Ross <gwr@nexenta.com>

References:
	http://www.illumos.org/issues/3537
	illumos/illumos-gate@c3a6601

Ported by: Cyril Plisko <cyril.plisko@mountall.com>
Illumos #3588

3588 provide zfs properties for logical (uncompressed) space used and referenced
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Dan McDonald <danmcd@nexenta.com>
Reviewed by: Richard Elling <richard.elling@dey-sys.com>
Approved by: Richard Lowe <richlowe@richlowe.net>
Ported-by: Richard Yao <ryao@gentoo.org>

References:

https://www.illumos.org/issues/3588
illumos/illumos-gate@77372cb0f35e8d3615ca2e16044f033397e88e21
Illumos #3582, #3584

3582 zfs_delay() should support a variable resolution
3584 DTrace sdt probes for ZFS txg states

Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Dan McDonald <danmcd@nexenta.com>
Reviewed by: Richard Elling <richard.elling@dey-sys.com>
Approved by: Garrett D'Amore <garrett@damore.org>

References:
    https://www.illumos.org/issues/3582
    illumos/illumos-gate@0689f76

Ported by: Ned Bass <bass6@llnl.gov>
Illumos #3603, #3604: bobj improvements

3603 panic from bpobj_enqueue_subobj()
3604 zdb should print bpobjs more verbosely
3871 GCC 4.5.3 does not like issue 3604 patch

https://www.illumos.org/issues/3603
https://www.illumos.org/issues/3604
https://www.illumos.org/issues/3871

Reviewed by: Henrik Mattson <henrik.mattson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Approved by: Dan McDonald <danmcd@nexenta.com>
Ported-by: Richard Yao <ryao@gentoo.org>

Note that the patch from Illumos issue 3871 is not accepted into Illumos
at the time of this writing. It is something that I wrote when porting
this. Documentation is in the Illumos issue.