zpios: Replace current_kernel_time() with getnstimeofday()

current_kernel_time() is not meant for performance measurement. This
should use getnstimeofday(), which is equivalent to the gethrestime()
function on Solaris.

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>
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>
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. This patch modifies
the autotools checks, userland code and kernel code to use C89.

Note that the Linux kernel passes -Wdeclaration-after-statement, so we
must pass -Wno-declaration-after-statement to counteract that. We also
do a little cleanup to simplify the makefiles.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Illumos #3742

3742 zfs comments need cleaner, more consistent style
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Eric Schrock <eric.schrock@delphix.com>
Approved by: Christopher Siden <christopher.siden@delphix.com>
Ported-by: Richard Yao <ryao@gentoo.org>

References:

https://www.illumos.org/issues/3742
illumos/illumos-gate@f7170741490edba9d1d9c697c177c887172bc741

Porting notes:

The change to zfs_vfsops.c was dropped because it involves
zfs_mount_label_policy, which does not exist in the Linux port.
Use bitfields for boolean values in znode_t

znode_t uses 1312 bytes in length on Linux 3.9.6 amd64. Switching to
bitfields for boolean values reduces the size to 1296 bytes. Further
reductions should be possible if padding were removed, but saving 16
bytes per struct can translate into megabytes of savings when hundreds
of thousands of znodes are cached. While the savings is relatively
small in comparison to memory used, the small efficiency gain is
practically free.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Remove unused option -r from zpool

Signed-off-by: Richard Yao <ryao@gentoo.org>
Implement -t option to zpool import for temporary pool names

Specifying newname to a pool import currently changes the pool name. We
implement option -t. Specifying option -t in conjuction with newpool to
zpool import will make the name change temporary. In specific, the
original pool name will be used in all label updates and therefore is
retained upon export.

This is useful in any situation where maintenance on a system's root
pool is done from another system. A good example is environments using
virtualization, where it is more desireable to do rollback of the root
dataset of a guest's root pool than it is to rollback of the zvol
hosting the guest's root pool when an issue prevents the guest from
booting properly. Other examples include maintenance on root pools
hosted on iSCSI and operating system installations on USB keys that are
used to rescue non-bootable sytems.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add pkg-config file for libzfs

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add MUTEX_SPIN
Enable ARC to reap from dnode cache

dnode space usage is counted against ARC via other_size, but ARC has no
way to reclaim from it. Lets provide a hook so that it can reap from
that cache.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add zexec_helper

/sbin/zexec_helper is a small helper program that will replace /sbin/sh
in call_usermodehelper() calls because GRSecurity 3.0 no longer permits
call_usermodehelper() to call things outside of /sbin and unspecified
"system library" paths.

Signed-off-by: Richard Yao <ryao@gentoo.org>
GrSecurity 3.0 compat: Make ctldir use zexec_helper

Signed-off-by: Richard Yao <ryao@gentoo.org>
Provide guidelines for reporting system hangs to the ZoL developers

Signed-off-by: Richard Yao <ryao@gentoo.org>
Inform OpenRC that ZFS uses mtab

p_l in #zfsonlinux reported that he had issues mounting filesystems that
were resolved by adding rc_need="mtab" to /etc/init.d/zfs. Closer
inspection revealed that we do have a race, but it is not clear how this
race caused mounting to fail. What is clear is that this race should be
fixed, so lets add the proper `use mtab` line to handle it.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Switch ztest mmap asserts to verifies so we fail early on systems where mmap() is not functioning

Signed-off-by: Richard Yao <ryao@gentoo.org>
Add option to zpool status to print guid

This provides a more straightfoward method of obtaining vdev GUIDs than
zdb. This option is the only method by which a system administrator is
able to obtain vdev GUIDs for missing cache devices because zdb does
print information about them (see zfsonlinux/zfs#1530).

The use of vdev GUIDs are a necessary workaround in edge cases where the
names provided by `zpool status` are not accepted by the zpool
detach/offline/remove/replace commands make the use of vdev GUIDs
necessary. Linux's /dev design makes it difficult to anticipate all
possible edge cases where the device names would not be accepted.
Providing a simple way of obtaining vdev GUIDs will minimize the pain of
system administrators in such edge cases.

Note that this adds a new boolean parameter to zpool_vdev_name, which
changes the libzfs interface.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Improve partition detection on lesser used devices

The format strings in efi_get_info() are intended to extract both the
main device and partition number. However, this is only done correctly
for hd, sd and vd devices. The format strings for ram, dm-, md and loop
devices misparse the input. This causes the partition device to be
incorrectly labelled as the main device with the partition being
labelled 0.

Reported-by: ilovezfs <ilovezfs@icloud.com>
Signed-off-by: Richard Yao <ryao@gentoo.org>
Refactor efi_parse_dev_path()

I have refactored efi_parse_dev_path to reduce code duplication and
avoid the use of constants. The latter was a criticism by ilovezfs of my
patch to fix the bug he reported during his review of it.

The previous code suffered from theoretical buffer overflows in strcpy()
and sscanf(). For instance, /dev/sdaaaaaaaaaaaaaa1 would cause an
overflow, despite being valid. The new code replaces strcpy() with
strlcpy() to truncate instead of overflowing, returns EINVAL on
truncation, and does a calculation to prove that the scanf() has enough
room to operate without overflowing, returning EINVAL otherwise.

This does not properly handle names where the partition number exceeds
the precision of an ushort_t. An example would be /dev/sda100000.
Should we ever encounter a situation where the partition number exceeds
the precision of an ushort_t, ZFS' internal partition number for this
device will be incorrect, but it should still operate. As such, the cost
to benefit ratio of handling this case to be too low to merit a fix
unless an attempt is made to produce a formally verified codebase.

Reviewed-by: ilovezfs <ilovezfs@icloud.com>
Signed-off-by: Richard Yao <ryao@gentoo.org>