summaryrefslogtreecommitdiff
path: root/libnetfs
diff options
context:
space:
mode:
authorSvante Signell <svante.signell@gmail.com>2019-10-30 02:33:27 +0100
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2019-10-30 02:33:27 +0100
commit40ab805df48b2f65d5028ba59063e2a36f19f452 (patch)
treec2f28545962193b271251dd54cbc2b237b68f435 /libnetfs
parent3a8234d2440a464f5401b3444d454ac913d6b3d6 (diff)
libnetfs: Support for file record locking
2019-03-04 Svante Signell <svante.signell@gmail.com> * file-lock.c: Make flock work regardless of the mode in which the file was opened. 2019-02-12 Svante Signell <svante.signell@gmail.com> * file-lock.c: Comment out "Make flock work without R or W mode" 2019-02-01 Svante Signell <svante.signell@gmail.com> * Update copyright years. * file-record-lock.c(netfs_S_file_record_lock): Don't set rendezvous to MACH_PORT_NULL. 2017-01-05 Svante Signell <svante.signell@gmail.com> * Update copyright years and headers. * dir-lookup.c(netfs_S_dir_lookup): Call fshelp_rlock_tweak() with new last argument rendezvous = MACH_PORT_NULL. * file-lock.c(netfs_S_file_lock): Likewise. * file-record-lock.c(netfs_S_file_record_lock): Likewise. (netfs_S_file_record_lock): Add new argument mach_port_t rendezvous. 2016-05-23 Svante Signell <svante.signell@gmail.com> * netfs.h (struct peropen): Change the type of rlock_status from an int to a struct rlock_peropen. (struct node): Change the type of userbox from a struct lock_box to a struct rlock_box. * dir-lookup.c (netfs_S_dir_lookup): Use fshelp_rlock_tweak. * file-lock-stat.c (netfs_S_file_lock_stat): Total rewrite around the new record locking functions. * file-lock.c (netfs_S_file_lock): Likewise. * file-record-lock.c (netfs_S_file_record_lock): Likewise. * make-node.c (netfs_make_node): Initialize userbox with fshelp_rlock_init. * make-peropen.c (netfs_make_peropen): Initialize lock_status using fshelp_rlock_po_init. (netfs_make_peropen): Add a comment that po->refcnt starts at zero. * relese-peropen.c (netfs_release_peropen): Release lock_status using fshelp_rlock_drop_peropen. 2001-04-11 Neal H Walfield <neal@cs.uml.edu> * file-record-lock.c: New file. Implement netfs_S_file_record_lock. * Makefile (SRCS): Add file-record-lock.c
Diffstat (limited to 'libnetfs')
-rw-r--r--libnetfs/Makefile12
-rw-r--r--libnetfs/dir-lookup.c56
-rw-r--r--libnetfs/file-lock-stat.c25
-rw-r--r--libnetfs/file-lock.c48
-rw-r--r--libnetfs/file-record-lock.c44
-rw-r--r--libnetfs/make-node.c7
-rw-r--r--libnetfs/make-peropen.c14
-rw-r--r--libnetfs/netfs.h12
-rw-r--r--libnetfs/release-peropen.c9
9 files changed, 167 insertions, 60 deletions
diff --git a/libnetfs/Makefile b/libnetfs/Makefile
index de06816e..0d4205d5 100644
--- a/libnetfs/Makefile
+++ b/libnetfs/Makefile
@@ -1,5 +1,5 @@
-# Copyright (C) 1995, 1996, 1997, 1999, 2001, 2002, 2008, 2012 Free Software
-# Foundation
+# Copyright (C) 1995-1997, 1999, 2001-2002, 2008, 2012, 2016-2019
+# Free Software Foundation
#
# Written by Michael I. Bushnell.
#
@@ -14,10 +14,9 @@
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
-#
+
# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>.
dir := libnetfs
makemode := library
@@ -34,7 +33,8 @@ FSSRCS= dir-link.c dir-lookup.c dir-mkdir.c dir-mkfile.c \
file-get-translator.c file-getcontrol.c file-getlinknode.c \
file-lock-stat.c file-lock.c file-set-size.c \
file-set-translator.c file-statfs.c file-sync.c file-syncfs.c \
- file-utimes.c file-reparent.c fsstubs.c file-get-transcntl.c
+ file-utimes.c file-record-lock.c file-reparent.c fsstubs.c \
+ file-get-transcntl.c
IOSRCS= io-read.c io-readable.c io-seek.c io-write.c io-stat.c io-async.c \
io-set-all-openmodes.c io-get-openmodes.c io-set-some-openmodes.c \
diff --git a/libnetfs/dir-lookup.c b/libnetfs/dir-lookup.c
index a135917f..23adeb33 100644
--- a/libnetfs/dir-lookup.c
+++ b/libnetfs/dir-lookup.c
@@ -1,6 +1,5 @@
/*
- Copyright (C) 1995,96,97,98,99,2000,01,02,13,14
- Free Software Foundation, Inc.
+ Copyright (C) 1995-2002, 2013-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -16,8 +15,7 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include <fcntl.h>
#include <assert-backtrace.h>
@@ -438,24 +436,50 @@ netfs_S_dir_lookup (struct protid *dircred,
goto out;
}
- free (newpi->po->path);
- if (dircred->po->path == NULL || !strcmp (dircred->po->path,"."))
+ mach_port_t rendezvous = MACH_PORT_NULL;
+ struct flock64 lock =
{
- /* dircred is the root directory. */
- newpi->po->path = relpath;
- relpath = NULL; /* Do not free relpath. */
+ l_start: 0,
+ l_len: 0,
+ l_whence: SEEK_SET
+ };
+
+ if (flags & O_EXLOCK)
+ {
+ lock.l_type = F_WRLCK;
+ err = fshelp_rlock_tweak (&np->userlock, &np->lock,
+ &newpi->po->lock_status, flags, 0, 0,
+ F_SETLK64, &lock, rendezvous);
}
- else
+ else if (flags & O_SHLOCK)
{
- newpi->po->path = NULL;
- asprintf (&newpi->po->path, "%s/%s", dircred->po->path, relpath);
+ lock.l_type = F_RDLCK;
+ err = fshelp_rlock_tweak (&np->userlock, &np->lock,
+ &newpi->po->lock_status, flags, 0, 0,
+ F_SETLK64, &lock, rendezvous);
}
- if (! newpi->po->path)
- err = errno;
+ if (! err)
+ {
+ free (newpi->po->path);
+ if (dircred->po->path == NULL || !strcmp (dircred->po->path,"."))
+ {
+ /* dircred is the root directory. */
+ newpi->po->path = relpath;
+ relpath = NULL; /* Do not free relpath. */
+ }
+ else
+ {
+ newpi->po->path = NULL;
+ asprintf (&newpi->po->path, "%s/%s", dircred->po->path, relpath);
+ }
+
+ if (! newpi->po->path)
+ err = errno;
- *retry_port = ports_get_right (newpi);
- ports_port_deref (newpi);
+ *retry_port = ports_get_right (newpi);
+ ports_port_deref (newpi);
+ }
out:
if (np)
diff --git a/libnetfs/file-lock-stat.c b/libnetfs/file-lock-stat.c
index 49529cf4..c2145918 100644
--- a/libnetfs/file-lock-stat.c
+++ b/libnetfs/file-lock-stat.c
@@ -1,5 +1,5 @@
-/*
- Copyright (C) 1995 Free Software Foundation, Inc.
+/*
+ Copyright (C) 1995, 2015-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -15,23 +15,30 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include "netfs.h"
#include "fs_S.h"
+#include <fcntl.h>
+#include <sys/file.h>
+
error_t
netfs_S_file_lock_stat (struct protid *user,
int *mystatus,
int *otherstatus)
{
+
+ struct node *node;
+
if (!user)
return EOPNOTSUPP;
-
- pthread_mutex_lock (&user->po->np->lock);
- *mystatus = user->po->lock_status;
- *otherstatus = user->po->np->userlock.type;
- pthread_mutex_unlock (&user->po->np->lock);
+
+ node = user->po->np;
+ pthread_mutex_lock (&node->lock);
+ *mystatus = fshelp_rlock_peropen_status (&user->po->lock_status);
+ *otherstatus = fshelp_rlock_node_status (&node->userlock);
+ pthread_mutex_unlock (&node->lock);
+
return 0;
}
diff --git a/libnetfs/file-lock.c b/libnetfs/file-lock.c
index 0010d4be..7bb81d43 100644
--- a/libnetfs/file-lock.c
+++ b/libnetfs/file-lock.c
@@ -1,5 +1,5 @@
-/*
- Copyright (C) 1995 Free Software Foundation, Inc.
+/*
+ Copyright (C) 1995, 2015-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -15,22 +15,54 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include "netfs.h"
#include "fs_S.h"
+#include <fcntl.h>
+#include <sys/file.h>
+
error_t
netfs_S_file_lock (struct protid *user,
int flags)
{
error_t err;
+ struct flock64 lock;
+ struct node *node;
+ int openstat = user->po->openstat;
+ mach_port_t rendezvous = MACH_PORT_NULL;
+
if (!user)
return EOPNOTSUPP;
- pthread_mutex_lock (&user->po->np->lock);
- err = fshelp_acquire_lock (&user->po->np->userlock, &user->po->lock_status,
- &user->po->np->lock, flags);
- pthread_mutex_unlock (&user->po->np->lock);
+
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 0;
+
+ if (flags & LOCK_UN)
+ lock.l_type = F_UNLCK;
+ else if (flags & LOCK_SH)
+ lock.l_type = F_RDLCK;
+ else if (flags & LOCK_EX)
+ lock.l_type = F_WRLCK;
+ else
+ return EINVAL;
+
+ /*
+ XXX: Fix for flock(2) calling fcntl(2)
+ From flock(2): A shared or exclusive lock can be placed on a file
+ regardless of the mode in which the file was opened.
+ */
+ if (openstat & (O_RDONLY|O_WRONLY|O_EXEC)) openstat |= O_RDONLY|O_WRONLY;
+
+ node = user->po->np;
+ pthread_mutex_lock (&node->lock);
+ err = fshelp_rlock_tweak (&node->userlock, &node->lock,
+ &user->po->lock_status, openstat,
+ 0, 0, flags & LOCK_NB ? F_SETLK64 : F_SETLKW64,
+ &lock, rendezvous);
+ pthread_mutex_unlock (&node->lock);
+
return err;
}
diff --git a/libnetfs/file-record-lock.c b/libnetfs/file-record-lock.c
new file mode 100644
index 00000000..049fc139
--- /dev/null
+++ b/libnetfs/file-record-lock.c
@@ -0,0 +1,44 @@
+/* Copyright (C) 2001, 2014-2019 Free Software Foundation, Inc.
+
+ Written by Neal H Walfield <neal@cs.uml.edu>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "netfs.h"
+
+#include <hurd/fshelp.h>
+
+error_t
+netfs_S_file_record_lock (struct protid *cred,
+ int cmd,
+ struct flock64 *lock,
+ mach_port_t rendezvous)
+{
+ struct node *node;
+ error_t err;
+
+ if (! cred)
+ return EOPNOTSUPP;
+
+ node = cred->po->np;
+ pthread_mutex_lock (&node->lock);
+ err = fshelp_rlock_tweak (&node->userlock, &node->lock,
+ &cred->po->lock_status, cred->po->openstat,
+ node->nn_stat.st_size, cred->po->filepointer,
+ cmd, lock, rendezvous);
+ pthread_mutex_unlock (&node->lock);
+ return err;
+}
diff --git a/libnetfs/make-node.c b/libnetfs/make-node.c
index a292dc62..ab3b8850 100644
--- a/libnetfs/make-node.c
+++ b/libnetfs/make-node.c
@@ -1,5 +1,5 @@
/*
- Copyright (C) 1995, 1996, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1995, 1996, 2000, 2015-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -15,8 +15,7 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include "netfs.h"
#include <hurd/fshelp.h>
@@ -32,7 +31,7 @@ init_node (struct node *np, struct netnode *nn)
np->owner = 0;
fshelp_transbox_init (&np->transbox, &np->lock, np);
- fshelp_lock_init (&np->userlock);
+ fshelp_rlock_init (&np->userlock);
return np;
}
diff --git a/libnetfs/make-peropen.c b/libnetfs/make-peropen.c
index 413e9140..4bd74740 100644
--- a/libnetfs/make-peropen.c
+++ b/libnetfs/make-peropen.c
@@ -1,5 +1,5 @@
-/*
- Copyright (C) 1995, 1997 Free Software Foundation, Inc.
+/*
+ Copyright (C) 1995, 1997, 2015-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -15,22 +15,26 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include "netfs.h"
+#include <errno.h>
+#include <stdlib.h>
#include <sys/file.h>
struct peropen *
netfs_make_peropen (struct node *np, int flags, struct peropen *context)
{
+ error_t err;
struct peropen *po = malloc (sizeof (struct peropen));
if (!po)
return NULL;
po->filepointer = 0;
- po->lock_status = LOCK_UN;
+ err = fshelp_rlock_po_init (&po->lock_status);
+ if (err)
+ return NULL;
refcount_init (&po->refcnt, 1);
po->openstat = flags;
po->np = np;
diff --git a/libnetfs/netfs.h b/libnetfs/netfs.h
index 625aa963..d6a13bb1 100644
--- a/libnetfs/netfs.h
+++ b/libnetfs/netfs.h
@@ -1,6 +1,6 @@
/*
-
- Copyright (C) 1994,95,96,97,99,2000,02,13 Free Software Foundation, Inc.
+ Copyright (C) 1994-1997, 1999, 2000, 2002, 2013-2019
+ Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
@@ -13,8 +13,7 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _HURD_NETFS_H_
#define _HURD_NETFS_H_
@@ -24,6 +23,7 @@
#include <hurd/iohelp.h>
#include <assert-backtrace.h>
#include <pthread.h>
+#include <refcount.h>
/* This library supports client-side network file system
implementations. It is analogous to the diskfs library provided for
@@ -50,7 +50,7 @@ struct protid
struct peropen
{
loff_t filepointer;
- int lock_status;
+ struct rlock_peropen lock_status;
refcount_t refcnt;
int openstat;
@@ -93,7 +93,7 @@ struct node
struct transbox transbox;
- struct lock_box userlock;
+ struct rlock_box userlock;
struct conch conch;
diff --git a/libnetfs/release-peropen.c b/libnetfs/release-peropen.c
index 01af97dd..2ef83183 100644
--- a/libnetfs/release-peropen.c
+++ b/libnetfs/release-peropen.c
@@ -1,5 +1,5 @@
/*
- Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+ Copyright (C) 1996, 1997, 2015-2019 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
@@ -15,8 +15,7 @@
General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#include <sys/file.h>
#include "netfs.h"
@@ -39,9 +38,7 @@ netfs_release_peropen (struct peropen *po)
if (po->shadow_root_parent)
mach_port_deallocate (mach_task_self (), po->shadow_root_parent);
- if (po->lock_status != LOCK_UN)
- fshelp_acquire_lock (&po->np->userlock, &po->lock_status,
- &po->np->lock, LOCK_UN);
+ fshelp_rlock_drop_peropen (&po->lock_status);
netfs_nput (po->np);