Add display.h and safe display buffers

A struct display_buffer describes a string buffer and the current position
within it so that a string can be built of various components. This is
especially useful for nested display functions for data types, where
the top level display function may call display functions for sub-data types.

While building a complex string, users SHOULD check the return value from
each display function and exit if it is <= 0, however, continuing to call
display functions will be totally safe.

The only things that MUST be done if building a new display primitive is
to call display_start at the beginning and display_finish at the end. A
display primitive is a function that uses a non-display function (such as
strcat, memcpy, sprintf) to copy bytes into the buffer. Such primitives must
assure that any such routines do not overflow the buffer, and then the
primitive must manage the b_current. display_finish will handle proper
indication of a full buffer or buffer overflow.

The core routines:

display_start validate and prepare to start appending to the buffer.
display_finish wrap up after appending to the buffer.
display_reset_buffer reset a buffer for re-use to build a new string.
display_printf append to the string using printf formatting
display_opaque_value format an opaque value into the buffer
display_cat append a simple string to the buffer

Additional documentation may be found in the Doxygen comments within
the source.

Ported from 1.5 21956e8e5457441dc7638916820572b35c7dc2bd

Signed-off-by: Frank Filz <ffilzlnx@mindspring.com>
Add simple log macro to log opaque values

Ported from 1.5 e243f94f20b19bbb777b3f1ca2a6548acfab97ff

Signed-off-by: Frank Filz <ffilzlnx@mindspring.com>
Increase number of pseudo fs nodes to 2048 and check limits

Also clean up some dead code.

The limit of 100 pseudo fs nodes is rather insufficient for 1000 exports...

To make it somewhat sane to test, I increased the number of path elements in
a pseudo path to 128.

Moved set up of pseudo fs until after creating root entries. This assures
that the pseudo fs only contains valid exports (previously, an export for
a path that didn't actually exist in the FSAL would be instantiated in the
pseudo fs, and then the export would have been discarded, leaving a dangling
pointer for someone to trip on).

Also improved the logging within the pseudo fs.

Also check pseudofs_id in pseudo fs handle before dereferencing array.

Ported from 1.5: a0de6fb4a764ca4d049436eda750f9f7cce71275

Apparently 2.0 somehow wound up with two calls to nfs4_ExportToPseudoFS.
The 1.5 patch moved the call to later in init, so this patch removes the
earlier call to match the 1.5 code.

Signed-off-by: Frank Filz <ffilzlnx@mindspring.com>
Fix up READDIR in pseudo fs so it handles NFS4ERR_WRONGSEC properly

Allow READDIR with just MOUNTED_ON_FILEID to succeed even if NFS4ERR_WRONGSEC.

Also clean up some out of memory handling for READDIR, don't bother trying
to recover, just LogFatal.

Ported from 1.5: b37c3372278a5c07a4b6192ebdda4d723648d2d4

Signed-off-by: Frank Filz <ffilz@us.ibm.com>
Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Don't check timestamp or length in creds when comparing v4 clientid verifiier

Confirmed ACE's comment. Clients create new creds so timestamp is a problem.

New creds might have a different group list, which changes length of cred, if
we don't care about group list, shouldn't care about cred length either.

Ported from 1.5: 2c309a762a38d58e660783ea7c264de02a3974d7

Signed-off-by: Frank Filz <ffilz@us.ibm.com>
Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Clean up use of NFS4ERR_BADHANDLE vs NFS4ERR_STALE

A handle that COULD be a valid Ganesha handle, that happens to not be
valid currently (for example, exportid doesn't reference a currently
valid export), or a PseudoFS ID that is in range, but not currently
valid should result in NFS4ERR_STALE.

NFS4ERR_BADHANDLE should be reserved for handles that are badly formed
or otherwise could not ever be a valid Ganesha handle. We also more
carefullly check handle validity, and make checks in order such that
there won't be any surprises, like indexing beyond end of provided
handle.

We must also properly validate PseudoFS handles by the same criteria.

NFS v3 and NLM validate handles during nfs_rpc_execute. If the handle
is bad or the exportid is bad, an SVC AUTH error was sent. This is
wrong, we must send NFS3ERR_BADHANDLE for bad handles and
NFS4ERR_STALE for invalid exportid. Because NLM has ASYNC procedures,
we allow a NULL pexport to be passed to NLM procedures that take
handles, and let the procedure handle responding. NFS procedures will
be responded to within nfs_rpc_execute.

Additionally, remove CACHE_INODE_UNAPPROPRIATED_KEY and
STATE_UNAPPROPRIATED_KEY since these errors are never used (and
were translated into NFS4ERR_BADHANDLE which is probably not the right
error if we ever did have that return code.

Several handle validation routines return an NFS4ERR, we now use the
returned value in most cases (except for RESTOREFH which has a specific
error code to report empty SavedFH.

Added nfs4_sanity_check_SavedFH to simplify checking of SavedFH and
remove duplicate code.

Fix PUBFH. We were not setting the length of it, so it was taking
maximum length and this was not equal to ROOTFH. The length of a
PseudoFS handle is also now computed the same way a normal handle
length is computed.

NFS v2/v3 LINK and RENAME operations should properly validate
the destination directory handle and properly report errors.

Clean up how handle errors are logged. LogInfo is used so that
sysadmins can debug handle issues without having to see gory
code internals.

Replace nfs4_sprint_fhandle with sprint_fhandle4 (remove duplicate
code).

Replace print_buff with new routine print_fhandle_fsal for FSAL handles
to be more informative.

Ported from 1.5 95bb50ea9cfe743c312cb325fe5739c67dc44d17

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Fix a logging bug in fattr_xdr_result encode_acl when no ACL present

Also fix up a few log messsage components.

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Return NFS4ERR_SYMNLINK when trying to open special files

Per this linux-nfs post:

http://marc.info/?l=linux-nfs&m=131342421825436&w=2

NFS4ERR_SYMLINK is required for client to distinguish wrong file type so
it can refresh it's cache and retry with a LOOKUP instead of an OPEN.

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Return NFS4ERR_ROFS for NFS v4 open for write on read-only export

Ported from 1.5 3c179ffba7d4c2318cc7540796229edf23ab724b

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Return NFS4ERR_NOENT instead of NFS4ERR_ACCESS on export client has no access to

If a client doesn't have any access to an EXPORT (is not listed in any
Access list for the EXPORT or covered by any wild card), we already
hide that export in READDIR. We should also return NFS4ERR_NOENT rather
than NFS4ERR_ACCESS to match.

Ported from 1.5 cee1437b2d0890f7da1a107991d6b86a37af57d5

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Make SECINFO not always return NFS4ERR_WRONGSEC

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Handle errors in PseudoFS READDIR and LOOKUP gracefully

If there is a problem with a junction, we should handle it gracefully,
not bomb out the whole READDIR, In this case, we skip the entry.

This could occur because the exported directory has been removed or
the attributes could not be fetched. There are permissions fixes that
will prevent the ACE4_READ_ATTR from blocking reading attributes
though ACE4_READ_ACL could still come into play if a client did a
READDIR asking for ACLs (which I don't expect will ever be done).

Also, return more sensible errors in other places.

Ported from 1.5 dd054f1d38dea6d82c5643af28569e27c226853e

Fixed up a some additional error handling.

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Make NFSv3 FSID unique/less collision prone
By performing xor on filesystem_id major and rotated minor numbers.
This allows client to mount one export on top of other even if the
major numbers of the exported filesystem is same.

Ported from 1.5 b78bb90a389beaaf5381397ece37c75748c92e1b

Signed-off-by: Venkateswararao Jujjuri "<jvrao@linux.vnet.ibm.com>
Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Multilock tool: Fix for using it with AIX.

Ported from 1.5 2e9e37f10e8c4d6f2f68994f2e8304d63cd7219a

Signed-off-by: suhrud <supatank@in.ibm.com>
Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
Update porting doc to reflect EXP work mostly completed and up to 1.5.0.rev13

Signed-off-by: Frank S. Filz <ffilzlnx@mindspring.com>
CID#18499 Resource leak

Error handling could lead to a resource leak of
the struct pointed by task if allocation of mul
failed. The fix is a bit longer than the previous
version but it's more readable and avoids a
resource leak.
CID#18466 Missing break in switch

Missing break for LAYOUTRETURN4_FSID case leading
to a fallthrough to the next case.
CID#18465 Missing break in switch

Missing break for case 123 leads to a
fallthrough to the default case used
for error handling.
CID#18453 Explicit null dereferenced

son_owner_val struct member of oname wasn't
initialized before calling memcpy. Thus, leading
to a segfault.
Moreover, this section of the code is only used
if 9P support was activated at build time. oname
isn't used at all in the general case.
CID#18447 Logically dead code

Whatever the course of action, found is set
to TRUE (1) anyway. The last conditionnal
statement isn't needed. This makes the code
a little more simple.