From dd7447b87c6d6d1afaf25553b0f911853dcc6683 Mon Sep 17 00:00:00 2001 From: Neal H Walfield Date: Tue, 29 Oct 2019 23:56:23 +0100 Subject: libdiskfs: Support for file record locking 2019-03-04 Svante Signell * file-lock.c: Make flock work regardless of the mode in which the file was opened. 2019-02-12 Svante Signell * file-lock.c: Comment out "Make flock work without R or W mode" 2019-02-01 Svante Signell * Update copyright years. * file-record-lock.c(diskfs_S_file_record_lock): Don't set rendezvous to MACH_PORT_NULL. 2018-12-07 Svante Signell * Update copyright years. * dir-lookup.c(diskfs_S_dir_lookup): Call fshelp_rlock_tweak() with new last argument rendezvous = MACH_PORT_NULL. * file-lock.c(diskfs_S_file_lock): Likewise. * file-record-lock.c(diskfs_S_file_record_lock): Likewise. (diskfs_S_file_record_lock): Add new argument mach_port_t rendezvous. 2017-01-05 Svante Signell * Update copyright years and headers. 2016-05-23 Svante Signell * file-lock-stat.c: Port from cthreads to libpthread. * file-lock.c: Likewise. * file-record-lock: Likewise. 2001-04-11 Neal H Walfield * Makefile (FSSRCS): Add file-record-lock.c. * diskfs.h (struct peropen): Change the type of lock_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 (diskfs_S_dir_lookup): Use fshelp_rlock_tweak as fshelp_acquire_lock is now depreciated. * file-lock-stat.c (diskfs_S_file_lock_stat): Total rewrite around the new record locking functions. * file-lock.c (diskfs_S_file_lock): Total rewrite around the new record locking functions. * file-record-lock.c: New file. Implement diskfs_S_file_record_lock. * node-make.c (diskfs_make_node): Initialize userbox with fshelp_rlock_init. * peropen-make.c (diskfs_make_peropen): Initialize lock_status using fshelp_rlock_po_init. * peropen-rele.c (diskfs_release_peropen): Release lock_status using fshelp_rlock_drop_peropen. --- libdiskfs/Makefile | 8 +++--- libdiskfs/dir-lookup.c | 32 +++++++++++++++------ libdiskfs/diskfs.h | 11 ++++--- libdiskfs/file-lock-stat.c | 44 +++++++++++++++------------- libdiskfs/file-lock.c | 68 +++++++++++++++++++++++++++++++------------- libdiskfs/file-record-lock.c | 45 +++++++++++++++++++++++++++++ libdiskfs/node-make.c | 7 ++--- libdiskfs/peropen-make.c | 14 ++++++--- libdiskfs/peropen-rele.c | 18 ++++-------- 9 files changed, 169 insertions(+), 78 deletions(-) create mode 100644 libdiskfs/file-record-lock.c (limited to 'libdiskfs') diff --git a/libdiskfs/Makefile b/libdiskfs/Makefile index 5716ac5b..66c9f53e 100644 --- a/libdiskfs/Makefile +++ b/libdiskfs/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1994,95,96,97,98,99,2000,01,2006,2012 +# Copyright (C) 1994,95,96,97,98,99,2000,01,2006,2012,2016-2019 # Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or @@ -12,8 +12,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 . dir := libdiskfs makemode := library @@ -26,7 +25,8 @@ FSSRCS= dir-chg.c dir-link.c dir-lookup.c dir-mkdir.c dir-mkfile.c \ file-get-trans.c file-get-transcntl.c file-getcontrol.c \ file-getfh.c file-getlinknode.c file-lock-stat.c \ file-lock.c file-set-size.c file-set-trans.c file-statfs.c \ - file-sync.c file-syncfs.c file-utimes.c file-reparent.c + file-sync.c file-syncfs.c file-utimes.c file-record-lock.c \ + file-reparent.c IOSRCS= io-async-icky.c io-async.c io-duplicate.c io-get-conch.c io-revoke.c \ io-map-cntl.c io-map.c io-modes-get.c io-modes-off.c \ io-modes-on.c io-modes-set.c io-owner-mod.c io-owner-get.c \ diff --git a/libdiskfs/dir-lookup.c b/libdiskfs/dir-lookup.c index 9ebe3b45..16eb22e3 100644 --- a/libdiskfs/dir-lookup.c +++ b/libdiskfs/dir-lookup.c @@ -1,6 +1,7 @@ /* libdiskfs implementation of fs.defs:dir_lookup - Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, - 2002, 2008, 2013, 2014 Free Software Foundation, Inc. + + Copyright (C) 1992-2002, 2008, 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 +14,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 . */ #include #include @@ -499,12 +499,28 @@ diskfs_S_dir_lookup (struct protid *dircred, if (! err) { newpo = 0; + mach_port_t rendezvous = MACH_PORT_NULL; + struct flock64 lock = + { + l_start: 0, + l_len: 0, + l_whence: SEEK_SET + }; + if (flags & O_EXLOCK) - err = fshelp_acquire_lock (&np->userlock, &newpi->po->lock_status, - &np->lock, LOCK_EX); + { + 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 if (flags & O_SHLOCK) - err = fshelp_acquire_lock (&np->userlock, &newpi->po->lock_status, - &np->lock, LOCK_SH); + { + 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 (! err) diff --git a/libdiskfs/diskfs.h b/libdiskfs/diskfs.h index c949ec07..272dd57e 100644 --- a/libdiskfs/diskfs.h +++ b/libdiskfs/diskfs.h @@ -1,7 +1,7 @@ /* Definitions for fileserver helper functions - Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2007, 2008, - 2009, 2013 Free Software Foundation, Inc. + Copyright (C) 1994-1999, 2001, 2002, 2007-2009, 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 @@ -14,8 +14,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 . */ #ifndef _HURD_DISKFS #define _HURD_DISKFS @@ -58,7 +57,7 @@ struct protid struct peropen { loff_t filepointer; - int lock_status; + struct rlock_peropen lock_status; refcount_t refcnt; int openstat; @@ -113,7 +112,7 @@ struct node struct transbox transbox; - struct lock_box userlock; + struct rlock_box userlock; struct conch conch; diff --git a/libdiskfs/file-lock-stat.c b/libdiskfs/file-lock-stat.c index a31ad9a2..c6990c46 100644 --- a/libdiskfs/file-lock-stat.c +++ b/libdiskfs/file-lock-stat.c @@ -1,38 +1,42 @@ -/* - Copyright (C) 1994, 1995 Free Software Foundation +/* Copyright (C) 1994-1995, 2001, 2014-2019 Free Software Foundation, Inc. - This file is part of the GNU Hurd. + Written by Neal H Walfield - 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. + This program 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. + This program 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; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/* Written by Michael I. Bushnell. */ + along with the GNU Hurd. If not, see . */ #include "priv.h" #include "fs_S.h" +#include +#include + kern_return_t diskfs_S_file_lock_stat (struct protid *cred, int *mystatus, int *otherstatus) { + struct node *node; + if (!cred) return EOPNOTSUPP; - - pthread_mutex_lock (&cred->po->np->lock); - *mystatus = cred->po->lock_status; - *otherstatus = cred->po->np->userlock.type; - pthread_mutex_unlock (&cred->po->np->lock); + + node = cred->po->np; + + pthread_mutex_lock (&node->lock); + *mystatus = fshelp_rlock_peropen_status (&cred->po->lock_status); + *otherstatus = fshelp_rlock_node_status (&node->userlock); + pthread_mutex_unlock (&node->lock); + return 0; } diff --git a/libdiskfs/file-lock.c b/libdiskfs/file-lock.c index ab536e86..b95c3f0c 100644 --- a/libdiskfs/file-lock.c +++ b/libdiskfs/file-lock.c @@ -1,36 +1,64 @@ -/* - Copyright (C) 1993, 1994 Free Software Foundation +/* Copyright (C) 1993-1994, 2001, 2014-2019 Free Software Foundation, Inc. - This file is part of the GNU Hurd. + Written by Neal H Walfield - 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. + This program 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. + This program 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; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/* Written by Michael I. Bushnell. */ + along with the GNU Hurd. If not, see . */ #include "priv.h" #include "fs_S.h" +#include +#include + kern_return_t diskfs_S_file_lock (struct protid *cred, int flags) { error_t err; - if (!cred) + struct flock64 lock; + struct node *node; + int openstat = cred->po->openstat; + mach_port_t rendezvous = MACH_PORT_NULL; + + if (! cred) return EOPNOTSUPP; - pthread_mutex_lock (&cred->po->np->lock); - err = fshelp_acquire_lock (&cred->po->np->userlock, &cred->po->lock_status, - &cred->po->np->lock, flags); - pthread_mutex_unlock (&cred->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 = cred->po->np; + pthread_mutex_lock (&node->lock); + err = fshelp_rlock_tweak (&node->userlock, &node->lock, + &cred->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/libdiskfs/file-record-lock.c b/libdiskfs/file-record-lock.c new file mode 100644 index 00000000..a3ce9f42 --- /dev/null +++ b/libdiskfs/file-record-lock.c @@ -0,0 +1,45 @@ +/* Copyright (C) 2001, 2014-2019 Free Software Foundation, Inc. + + Written by Neal H Walfield + + This program 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. + + This program 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 . */ + +#include "priv.h" +#include "diskfs.h" +#include "fs_S.h" + +#include +#include + +error_t +diskfs_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->dn_stat.st_size, cred->po->filepointer, + cmd, lock, rendezvous); + pthread_mutex_unlock (&node->lock); + return err; +} diff --git a/libdiskfs/node-make.c b/libdiskfs/node-make.c index 7bc1d854..386714c2 100644 --- a/libdiskfs/node-make.c +++ b/libdiskfs/node-make.c @@ -1,5 +1,5 @@ /* - Copyright (C) 1994,95,96,2002 Free Software Foundation, Inc. + Copyright (C) 1994-1996, 2002, 2014-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 @@ -12,8 +12,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 . */ #include "priv.h" #include @@ -41,7 +40,7 @@ init_node (struct node *np, struct disknode *dn) fshelp_transbox_init (&np->transbox, &np->lock, np); iohelp_initialize_conch (&np->conch, &np->lock); - fshelp_lock_init (&np->userlock); + fshelp_rlock_init (&np->userlock); return np; } diff --git a/libdiskfs/peropen-make.c b/libdiskfs/peropen-make.c index 788b9a7a..827aa2d8 100644 --- a/libdiskfs/peropen-make.c +++ b/libdiskfs/peropen-make.c @@ -1,5 +1,5 @@ /* - Copyright (C) 1994,97,99,2001,02 Free Software Foundation + Copyright (C) 1994,97,99,2001-2002,2014-2019 Free Software Foundation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -12,11 +12,13 @@ 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 . */ #include "priv.h" +#include +#include #include +#include /* Create and return a new peropen structure on node NP with open flags FLAGS. */ @@ -24,13 +26,17 @@ error_t diskfs_make_peropen (struct node *np, int flags, struct peropen *context, struct peropen **ppo) { + error_t err; struct peropen *po = *ppo = malloc (sizeof (struct peropen)); if (! po) return ENOMEM; + err = fshelp_rlock_po_init (&po->lock_status); + if (err) + return err; + po->filepointer = 0; - po->lock_status = LOCK_UN; refcount_init (&po->refcnt, 1); po->openstat = flags; po->np = np; diff --git a/libdiskfs/peropen-rele.c b/libdiskfs/peropen-rele.c index 877137bb..e8d83b55 100644 --- a/libdiskfs/peropen-rele.c +++ b/libdiskfs/peropen-rele.c @@ -1,5 +1,5 @@ -/* - Copyright (C) 1994, 1996, 1997 Free Software Foundation +/* + Copyright (C) 1994, 1996, 1997, 2014-2019 Free Software Foundation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -12,8 +12,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 . */ #include #include "priv.h" @@ -33,14 +32,9 @@ diskfs_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) - { - pthread_mutex_lock (&po->np->lock); - fshelp_acquire_lock (&po->np->userlock, &po->lock_status, - &po->np->lock, LOCK_UN); - diskfs_nput (po->np); - } + fshelp_rlock_drop_peropen (&po->lock_status); + if (fshelp_rlock_peropen_status(&po->lock_status) != LOCK_UN) + diskfs_nput (po->np); else diskfs_nrele (po->np); -- cgit v1.2.3