Commit Graph

382 Commits (49eaca0c42a15e1053355af4c96a4bee11cd4be3)

Author SHA1 Message Date
Ronnie Sahlberg d827447242 Don't leak a rpde_cb_data structure on each opendir()
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2016-01-16 18:37:07 -08:00
Ronnie Sahlberg 66876e2879 Merge pull request #131 from jbkempf/win32_mingw
Support Win32 compilation with Mingw
2016-01-16 08:51:31 -08:00
Jean-Baptiste Kempf d02d5d0f5e The correct NDK define for Android is __ANDROID__
The ANDROID macro is not recommended
2016-01-09 13:35:11 +01:00
Jean-Baptiste Kempf 4f0d26258e Win32: Fix nfs_stat declaration for Win32
So that the definition and declaration match.

There might be another way, with stat64i32 but I'm not confident it will
work with MSVC
2016-01-08 23:41:56 +01:00
Jean-Baptiste Kempf 3ca22dcfdd Win32: Add win32_compat.h include 2016-01-08 23:23:00 +01:00
Ronnie Sahlberg 6f4ff8621f Handle POLLERR/POLLHUP properly and try to reconnect on failure
POLLERR and POLLHUP handling in rpc_service() could not deal with
session failures or auto reconnect.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-28 08:02:15 -08:00
Ronnie Sahlberg 034c277c71 Make rpc->connect_cb a one-shot callback
It makes no sense to have socket.c keep invoking this callback over and over.
Just change it to become one-shot.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-24 18:01:19 -08:00
Ronnie Sahlberg aea2810c1e Do not use ioctl(FIONREAD) for UDP sockets
The linux kernel does not check the UDP checksum until the application tries
to read if from the socket.

This means that the socket might be readable, but when we try to read
the data, or inspect how much data is available, the packets will be discarded
by the kernel.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-24 12:21:33 -08:00
Ronnie Sahlberg 1ec2d0c5b7 Add check that we have the full RM before starting to read the PDU data
If we are trying to read (part of?) the RM, we can not assume that as long
as recv() returned non-error that we have the full RM.
We must check before we proceed to try to read the actual PDU data.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-24 12:06:45 -08:00
Ronnie Sahlberg f681a2c167 Revert some commits that cause subtle API issues
We can not have a static rpc->inbuf buffer since that will no longer guarantee
that the received buffer is valid for the duration of callbacks.

One of the problems is that if we issue new (sync) RPCs from within a
callback, that will overwrite and invalidate the receive buffer that
we passed to the callback.

Revert "init: do not leak rpc->inbuf"
This reverts commit f7bc4c8bb1.

Revert "socket: we have to use memmove in rpc_read_from_socket"
This reverts commit 24429e95b8.

Revert "socket: make rpc->inbuf static and simplify receive logic"
This reverts commit 7000a0aa04.
2015-12-24 12:06:22 -08:00
Ronnie Sahlberg d73d4f3305 rpc_reconnect_requeue: return -1 if autoreconnect is not enabled
This funciton is called from rpc_service when it has detected that
a socket has errored out during reading/writing.
However, since this fucntion returns 0 (==success) for the case where
autoreconnect is not enabled, this means that for an errored socket we
will return 0 (==success) from rpc_service() back to the application.

Change rpc_reconnect_requeue to return -1 when invoked and autoreconnect
is disabled so that applications will receive an error back from rpc_service.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-10-19 18:57:17 -07:00
Peter Lieven ddd9e2f7e9 socket: keep fd the same across reconnects
There is no guarantee that we get the same fd again when
reestablishing a session. But if the fd changes during a
reconnect we might up with a client application busy polling
on the old fd.

Qemu registers a read handler on the current fd, but is
not realizing fd changes. So we busy poll on the old fd for good.
Things are working (except for the busy polling) until
a drain all is issued. At this point Qemu deadlocks.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-09-22 14:54:48 +02:00
Peter Lieven 7b7aef6b6d socket: keep reconnecting if a reconnect fails
otherwise we end up eating up all socket errors in rpc_service and then
believe we are connected, but the next call to rpc_read_from_socket
fails because the socket is closed. we then reconnect anyway.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-09-22 14:40:59 +02:00
Ronnie Sahlberg 2a95006567 Merge pull request #122 from plieven/fix_recv
socket: also reconnect on write errors
2015-09-06 06:57:32 -07:00
Peter Lieven 5c7a0f04e6 socket: fix deadlock in rpc_reconnect_requeue
the requeueing code is broken because we access pdu->next
after we mangled it in rpc_return_to_queue.

This leads to losing of waitqueue elements and more severe
a deadlock as soon as more than one waitpdu queue has elements.

Reason for that is that the first elements of the first
two queues are linked to each other.

Example:
waitpdu[0]->head = pduA ; pduA->next = pduB; pduB->next = NULL;
waitpdu[1]->head = pduC ; pduC->next = NULL;
outqueue->head = NULL;

After the for loop for waitpdu[0] queue the outqueue looks like

outqueue->head = pduA; pduA->next = NULL;

At this point pduB is lost!

In the for loop for waitpdu[1] queue the outqueue looks like this
after the first iteration:

outqueue->head = pduC; pduC->next = pduA; pduA->next = NULL;

We now fetch pdu->next of pduC which is pduA.

In the next iteration we put pduA in front of pduC. pduA->next
is then pduC and pduC->next is pduA. => Deadlock.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-08-28 21:13:37 +02:00
Peter Lieven 29ab169d2b init: do not write to stderr in rpc_set_error
only log error if debug >= 1.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-08-06 10:12:04 +02:00
Peter Lieven aacee393ca socket: also reconnect on write errors
This also return -1 for rpc_service if rpc_reconnect_requeue fails.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-08-06 09:53:49 +02:00
Peter Lieven b319b976dc socket: handle count == 0 in rpc_read_from_socket
An EOF is signalled through a POLLIN event and subsequen recvs return
always 0. Handle this condition and reconnect. Otherwise we might
deadlock here.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-08-04 14:55:28 +02:00
Ronnie Sahlberg 8f06fc9d92 New version 1.9.8
- Disable multithreading in fuse_nfs
- Add -Wall and -Werror compiler flags (and fix issues found by it)
- Add nfs-cat utility
- Switch to using nfs_[f]stat64 instead of the deprecated nfs_[f]stat call
  in all examples
- If the server does not return any atttributes for entries in READDIRPLUS
  then try to fetch them using lookup instead.
- Reconnection fixes
- Enforce the max pdu size and add sanity checks when reading PDUs from
  the socket.
- Stop using ioctl(FIONREAD) to find out how many bytes to read, and treat
  0 as an indication of a problem. Some applications call their POLLIN handlers
  spuriosly even when there is no data to read, which breaks this check in
  libnfs.
- Add basic support to do logging.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-08-02 10:18:25 -07:00
Peter Lieven 9bff07490f add basic support for setting a log level
only logging to stderr is supported at the moment. Per default
there is no output. Its possible to set the log level via
debug url parameter.

Example:
 nfs-ls nfs://127.0.0.1/export?debug=2

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-23 08:55:11 +02:00
Peter Lieven f7bc4c8bb1 init: do not leak rpc->inbuf
additionally free(NULL) is a NOP. So simplify the code

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 16:22:20 +02:00
Peter Lieven 24429e95b8 socket: we have to use memmove in rpc_read_from_socket
I was errornously believing that the areas could not overlap.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 15:54:54 +02:00
Peter Lieven beaa838637 fix some compiler warnings
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 13:49:17 +02:00
Peter Lieven 7000a0aa04 socket: make rpc->inbuf static and simplify receive logic
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 13:45:26 +02:00
Peter Lieven 82aec93f12 socket: limit memory allocation when reading from socket
the write limit of libnfs has been 1M since a long time.
Restrict rtmax and wrmax to 1M and error out otherwise.

Limit the PDU size when reading from socket to rule out
malicious servers forcing us to allocate a lot of memory.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 13:45:20 +02:00
Peter Lieven dc70b92dc6 socket: reset written bytes for head outqueue element
We could be in the middle of writing a PDU while we start reconnecting.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 13:45:15 +02:00
Peter Lieven cf420d32d0 socket: use FIONREAD ioctl only for UDP
under Linux poll might return POLLIN even if there are no bytes available for read.
See select(2) manpage for surious readiness under BUGS.

As a consequence we start dropping TCP connections which are still alive.

Signed-off-by: Peter Lieven <pl@kamp.de>

Conflicts:
	lib/socket.c
2015-06-19 13:45:03 +02:00
Peter Lieven 99ae3c01c4 add some debug output
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-06-19 13:43:55 +02:00
Ronnie Sahlberg 68b0298102 Add missing NULLing of data->continue_data for READDIR
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-05-26 19:50:00 -07:00
Ronnie Sahlberg d8ba04d9c6 READDIRPLUS: use lookup on entries where the server did not return any attr
Some servers sometimes do not return attrivbutes for files in the RDP
replies. So we need to fallback to using LOOKUPs for these entries
just like we always have to do in the READDIR case.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-05-25 16:53:56 -07:00
Ronnie Sahlberg 63d40b1737 Revert "Remove unused variable from socket.c"
This reverts commit d74a938775.
2015-03-12 20:52:52 -07:00
MilhouseVH d74a938775 Remove unused variable from socket.c 2015-03-12 05:29:56 +00:00
Ronnie Sahlberg 84607f4821 Add -Wall -Werror and friends
Update the configure to add some sanity -W arguments.
A good start is probably :
  -Wall -Werror -Wshadow -Wno-write-strings -Wstrict-prototypes
  -Wpointer-arith -Wcast-align -Wno-strict-aliasing

Fixup the paces in the code that triggers.
(one of which is readahead code which is perhaps broken?)

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-03-10 21:06:38 -07:00
Ronnie Sahlberg e775160243 new version 1.9.7
- Auto-traverse mounts. With this option (default to on) libnfs will
   autodiscover and handle any nested submounts.
 - Remove nfs_get_current_offset. Applications should use seek instead of this function.
 - Add umask() support.
 - Change set_tcp_sockopt() to be static.
 - Android fix for nfs-ls
 - Make S_IFLNK available on windows.
 - Fix a use after free.
 - Fix a bug where truncate() treated offset as 32bit.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-02-09 19:16:56 -08:00
Ross Lagerwall bf52e348ad libnfs: Fix use after free
Signed-off-by: Ross Lagerwall <rosslagerwall@gmail.com>
2015-02-02 23:32:32 +00:00
Ronnie Sahlberg f6e7505baa libnfs: make continue_int 64 bit to avoid sign extension bug for truncate()
Make the continue_int field we use through the internal callbacks a uint64_t
instead of an int.
This fixes a bug for the truncate function where we pass the offset to truncate
to via this field.

The bug is that otherwise we first truncate the field to an int and then
in the callback we cast this int back to a uint64_t again.

If the user called truncate with an offset that is >= 2^31
then :
IF the 1<<31 bit is cleared then we would truncate to (offset & 0xffffffff)
IF the 1<<31 bit is set, we would instead truncate to (offset | 0xffffffff00000000)

Reported-by: doktorstick at github
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-26 17:11:10 -08:00
Ross Lagerwall 368ee352a6 opendir_cb: Retrieve attributes for a top-level submount correctly
Fix retrieving the attributes for a submount in nfs_opendir when
the submount is an entry of '/'.

Signed-off-by: Ross Lagerwall <rosslagerwall@gmail.com>
2015-01-18 22:18:18 +00:00
Ronnie Sahlberg d1c2c47ff0 Add auto-traverse-mount URL argument and default it to TRUE
Add a URL argument to enable/disable the use of automatic traversal of nested
mounts for a libnfs context. Default it to enabled.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:49:08 -08:00
Ronnie Sahlberg 1eea1c4647 opendir_cb: use attributes from the nested mount in opendir
When opendir_cb encounters an entry that refers to a nested mount, then
replace the attributes with those attributes we collected for this export
during the mount and return those instead.

This makes traversing a director that crosses into a different filesystem
on the server transparent to the client.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:32:31 -08:00
Ronnie Sahlberg 27348486f6 nfs_lookuppath_async: replace path components with nested mount filehandles
Durng nfs_lookuppath_async, check if the requested path traverses into
a nested mountpoint and if so, skip resolving that part of thre path and just
use the filehandle for the nested mount.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:29:14 -08:00
Ronnie Sahlberg c7190d3078 nested_mounts: collect fattr3 data for all nested mounts
During the mount process, once we have connected to NFSd we need to collect
the file attributes for all the filehandles that are associated with
nested mounts.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:24:17 -08:00
Ronnie Sahlberg 08ad1b80c0 nested_mounts: collect filehandles for all nested mounts during nfs_mount()
During the mount process, call MOUNT3 EXPORT and collect a list of all
exports on the server. For any export that is a nested mount to the current
directory we are trying to mount call out to MOUNT3 MNT and collect the
filehandle for those mounts.

Track all nested mounts and their filehandles in a list from the nfs_context.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:19:13 -08:00
Ronnie Sahlberg 7378a13fa6 nfs_mount: rename nfs_mount_10/9/8_cb to nfs_mount_11/10/9_cb
Rename these callback functions to make space for two new functions
nfs_mount_7/8_cb which we will be using to collect information about
nested mounts in a later patch.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:09:37 -08:00
Ronnie Sahlberg 63d893575c opendir_cb: use a fattr3 pointer instead of dereferencing every time
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-17 11:00:48 -08:00
Ronnie Sahlberg 7b46c2bc3e READDIR: Bake the file type into the mode we return
NFS reports type in a separate to mode, where mode only contains
the protection bits. This makes it inconvenient to use when porting programs
since under posix the type is supposed to be part of the mode bits (S_IFMT)

When unmarshalling the directory entries into a nfsdirent structure, bake
the S_IF* file type into where the S_IFMT bits would be.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-12 20:32:29 -08:00
Ronnie Sahlberg d18b335622 Merge branch 'master' of github.com:sahlberg/libnfs 2015-01-08 07:29:35 -08:00
Mike Cui ac76768170 set_tcp_sockopt should be static, otherwise it prevents linking with libiscsi
which has function of the same name, also not static.

Same fix needs to go in to libiscsi.
2015-01-05 09:25:27 -08:00
Ronnie Sahlberg 0c1a5c464c libnfs.c: add support for nfs_umask
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2014-12-09 14:00:15 -08:00
Ronnie Sahlberg b53cd4d76c libnfs.c: remove function nfs_get_current_offset
Remove this function (that no one uses) since this is what
lseek(SEEK_CUR, 0) does.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2014-12-09 13:41:28 -08:00
Ronnie Sahlberg 5f6442d1b2 libnfs.c: fix typo, it is readahead not readahaed
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2014-12-05 08:44:15 -08:00