14945c132SAlfred Perlstein /* $NetBSD: lockd_lock.c,v 1.5 2000/11/21 03:47:41 enami Exp $ */ 24945c132SAlfred Perlstein /* $FreeBSD$ */ 34945c132SAlfred Perlstein 44945c132SAlfred Perlstein /* 54945c132SAlfred Perlstein * Copyright (c) 2001 Andrew P. Lentvorski, Jr. 64945c132SAlfred Perlstein * Copyright (c) 2000 Manuel Bouyer. 74945c132SAlfred Perlstein * 84945c132SAlfred Perlstein * Redistribution and use in source and binary forms, with or without 94945c132SAlfred Perlstein * modification, are permitted provided that the following conditions 104945c132SAlfred Perlstein * are met: 114945c132SAlfred Perlstein * 1. Redistributions of source code must retain the above copyright 124945c132SAlfred Perlstein * notice, this list of conditions and the following disclaimer. 134945c132SAlfred Perlstein * 2. Redistributions in binary form must reproduce the above copyright 144945c132SAlfred Perlstein * notice, this list of conditions and the following disclaimer in the 154945c132SAlfred Perlstein * documentation and/or other materials provided with the distribution. 164945c132SAlfred Perlstein * 3. All advertising materials mentioning features or use of this software 174945c132SAlfred Perlstein * must display the following acknowledgement: 184945c132SAlfred Perlstein * This product includes software developed by the University of 194945c132SAlfred Perlstein * California, Berkeley and its contributors. 204945c132SAlfred Perlstein * 4. Neither the name of the University nor the names of its contributors 214945c132SAlfred Perlstein * may be used to endorse or promote products derived from this software 224945c132SAlfred Perlstein * without specific prior written permission. 234945c132SAlfred Perlstein * 244945c132SAlfred Perlstein * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254945c132SAlfred Perlstein * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264945c132SAlfred Perlstein * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274945c132SAlfred Perlstein * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284945c132SAlfred Perlstein * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294945c132SAlfred Perlstein * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304945c132SAlfred Perlstein * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314945c132SAlfred Perlstein * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324945c132SAlfred Perlstein * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334945c132SAlfred Perlstein * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344945c132SAlfred Perlstein * SUCH DAMAGE. 354945c132SAlfred Perlstein * 364945c132SAlfred Perlstein */ 374945c132SAlfred Perlstein 38b6dc41baSAlfred Perlstein #define LOCKD_DEBUG 394945c132SAlfred Perlstein 404945c132SAlfred Perlstein #include <stdio.h> 41b6dc41baSAlfred Perlstein #ifdef LOCKD_DEBUG 424945c132SAlfred Perlstein #include <stdarg.h> 434945c132SAlfred Perlstein #endif 444945c132SAlfred Perlstein #include <stdlib.h> 454945c132SAlfred Perlstein #include <unistd.h> 464945c132SAlfred Perlstein #include <fcntl.h> 474945c132SAlfred Perlstein #include <syslog.h> 484945c132SAlfred Perlstein #include <errno.h> 494945c132SAlfred Perlstein #include <string.h> 504945c132SAlfred Perlstein #include <signal.h> 514945c132SAlfred Perlstein #include <rpc/rpc.h> 524945c132SAlfred Perlstein #include <sys/types.h> 534945c132SAlfred Perlstein #include <sys/stat.h> 544945c132SAlfred Perlstein #include <sys/socket.h> 554945c132SAlfred Perlstein #include <sys/param.h> 564945c132SAlfred Perlstein #include <sys/mount.h> 574945c132SAlfred Perlstein #include <sys/wait.h> 584945c132SAlfred Perlstein #include <rpcsvc/sm_inter.h> 594945c132SAlfred Perlstein #include <rpcsvc/nlm_prot.h> 604945c132SAlfred Perlstein #include "lockd_lock.h" 614945c132SAlfred Perlstein #include "lockd.h" 624945c132SAlfred Perlstein 634945c132SAlfred Perlstein #define MAXOBJECTSIZE 64 644945c132SAlfred Perlstein #define MAXBUFFERSIZE 1024 654945c132SAlfred Perlstein 664945c132SAlfred Perlstein /* 674945c132SAlfred Perlstein * SM_MAXSTRLEN is usually 1024. This means that lock requests and 684945c132SAlfred Perlstein * host name monitoring entries are *MUCH* larger than they should be 694945c132SAlfred Perlstein */ 704945c132SAlfred Perlstein 714945c132SAlfred Perlstein /* 724945c132SAlfred Perlstein * A set of utilities for managing file locking 734945c132SAlfred Perlstein * 744945c132SAlfred Perlstein * XXX: All locks are in a linked list, a better structure should be used 754945c132SAlfred Perlstein * to improve search/access effeciency. 764945c132SAlfred Perlstein */ 774945c132SAlfred Perlstein 784945c132SAlfred Perlstein /* struct describing a lock */ 794945c132SAlfred Perlstein struct file_lock { 804945c132SAlfred Perlstein LIST_ENTRY(file_lock) nfslocklist; 814945c132SAlfred Perlstein fhandle_t filehandle; /* NFS filehandle */ 824945c132SAlfred Perlstein struct sockaddr *addr; 834945c132SAlfred Perlstein struct nlm4_holder client; /* lock holder */ 845f07c7b2SAlfred Perlstein /* XXX: client_cookie used *only* in send_granted */ 854945c132SAlfred Perlstein netobj client_cookie; /* cookie sent by the client */ 864945c132SAlfred Perlstein char client_name[SM_MAXSTRLEN]; 874945c132SAlfred Perlstein int nsm_status; /* status from the remote lock manager */ 884945c132SAlfred Perlstein int status; /* lock status, see below */ 894945c132SAlfred Perlstein int flags; /* lock flags, see lockd_lock.h */ 907e589b0bSAlfred Perlstein int blocking; /* blocking lock or not */ 914945c132SAlfred Perlstein pid_t locker; /* pid of the child process trying to get the lock */ 924945c132SAlfred Perlstein int fd; /* file descriptor for this lock */ 934945c132SAlfred Perlstein }; 944945c132SAlfred Perlstein 957e589b0bSAlfred Perlstein LIST_HEAD(nfslocklist_head, file_lock); 967e589b0bSAlfred Perlstein struct nfslocklist_head nfslocklist_head = LIST_HEAD_INITIALIZER(nfslocklist_head); 977e589b0bSAlfred Perlstein 987e589b0bSAlfred Perlstein LIST_HEAD(blockedlocklist_head, file_lock); 997e589b0bSAlfred Perlstein struct blockedlocklist_head blockedlocklist_head = LIST_HEAD_INITIALIZER(blockedlocklist_head); 1007e589b0bSAlfred Perlstein 1014945c132SAlfred Perlstein /* lock status */ 1024945c132SAlfred Perlstein #define LKST_LOCKED 1 /* lock is locked */ 1034945c132SAlfred Perlstein /* XXX: Is this flag file specific or lock specific? */ 1044945c132SAlfred Perlstein #define LKST_WAITING 2 /* file is already locked by another host */ 1054945c132SAlfred Perlstein #define LKST_PROCESSING 3 /* child is trying to aquire the lock */ 1064945c132SAlfred Perlstein #define LKST_DYING 4 /* must dies when we get news from the child */ 1074945c132SAlfred Perlstein 1087e589b0bSAlfred Perlstein /* struct describing a monitored host */ 1094945c132SAlfred Perlstein struct host { 1104945c132SAlfred Perlstein LIST_ENTRY(host) hostlst; 1114945c132SAlfred Perlstein char name[SM_MAXSTRLEN]; 1124945c132SAlfred Perlstein int refcnt; 1134945c132SAlfred Perlstein }; 1147e589b0bSAlfred Perlstein /* list of hosts we monitor */ 1157e589b0bSAlfred Perlstein LIST_HEAD(hostlst_head, host); 1167e589b0bSAlfred Perlstein struct hostlst_head hostlst_head = LIST_HEAD_INITIALIZER(hostlst_head); 1177e589b0bSAlfred Perlstein 1187e589b0bSAlfred Perlstein /* 1197e589b0bSAlfred Perlstein * File monitoring handlers 1207e589b0bSAlfred Perlstein * XXX: These might be able to be removed when kevent support 1217e589b0bSAlfred Perlstein * is placed into the hardware lock/unlock routines. (ie. 1227e589b0bSAlfred Perlstein * let the kernel do all the file monitoring) 1237e589b0bSAlfred Perlstein */ 1247e589b0bSAlfred Perlstein 1257e589b0bSAlfred Perlstein /* Struct describing a monitored file */ 1267e589b0bSAlfred Perlstein struct monfile { 1277e589b0bSAlfred Perlstein LIST_ENTRY(monfile) monfilelist; 1287e589b0bSAlfred Perlstein fhandle_t filehandle; /* Local access filehandle */ 1297e589b0bSAlfred Perlstein int fd; /* file descriptor: remains open until unlock! */ 1307e589b0bSAlfred Perlstein int refcount; 1317e589b0bSAlfred Perlstein int exclusive; 1327e589b0bSAlfred Perlstein }; 1337e589b0bSAlfred Perlstein 1347e589b0bSAlfred Perlstein /* List of files we monitor */ 1357e589b0bSAlfred Perlstein LIST_HEAD(monfilelist_head, monfile); 1367e589b0bSAlfred Perlstein struct monfilelist_head monfilelist_head = LIST_HEAD_INITIALIZER(monfilelist_head); 1374945c132SAlfred Perlstein 1384945c132SAlfred Perlstein static int debugdelay = 0; 1394945c132SAlfred Perlstein 1407e589b0bSAlfred Perlstein enum nfslock_status { NFS_GRANTED = 0, NFS_GRANTED_DUPLICATE, 1417e589b0bSAlfred Perlstein NFS_DENIED, NFS_DENIED_NOLOCK, 1427e589b0bSAlfred Perlstein NFS_RESERR }; 1437e589b0bSAlfred Perlstein 1447e589b0bSAlfred Perlstein enum hwlock_status { HW_GRANTED = 0, HW_GRANTED_DUPLICATE, 1457e589b0bSAlfred Perlstein HW_DENIED, HW_DENIED_NOLOCK, 1467e589b0bSAlfred Perlstein HW_STALEFH, HW_READONLY, HW_RESERR }; 1474945c132SAlfred Perlstein 1484945c132SAlfred Perlstein enum partialfilelock_status { PFL_GRANTED=0, PFL_GRANTED_DUPLICATE, PFL_DENIED, 1497e589b0bSAlfred Perlstein PFL_NFSDENIED, PFL_NFSBLOCKED, PFL_NFSDENIED_NOLOCK, PFL_NFSRESERR, 1507e589b0bSAlfred Perlstein PFL_HWDENIED, PFL_HWBLOCKED, PFL_HWDENIED_NOLOCK, PFL_HWRESERR}; 1514945c132SAlfred Perlstein 1525f07c7b2SAlfred Perlstein enum LFLAGS {LEDGE_LEFT, LEDGE_LBOUNDARY, LEDGE_INSIDE, LEDGE_RBOUNDARY, LEDGE_RIGHT}; 1535f07c7b2SAlfred Perlstein enum RFLAGS {REDGE_LEFT, REDGE_LBOUNDARY, REDGE_INSIDE, REDGE_RBOUNDARY, REDGE_RIGHT}; 1545f07c7b2SAlfred Perlstein /* XXX: WARNING! I HAVE OVERLOADED THIS STATUS ENUM! SPLIT IT APART INTO TWO */ 1555f07c7b2SAlfred Perlstein enum split_status {SPL_DISJOINT=0, SPL_LOCK1=1, SPL_LOCK2=2, SPL_CONTAINED=4, SPL_RESERR=8}; 1565f07c7b2SAlfred Perlstein 1577e589b0bSAlfred Perlstein enum partialfilelock_status lock_partialfilelock(struct file_lock *fl); 1587e589b0bSAlfred Perlstein 1597e589b0bSAlfred Perlstein void send_granted(struct file_lock *fl, int opcode); 1604945c132SAlfred Perlstein void siglock(void); 1614945c132SAlfred Perlstein void sigunlock(void); 162a4a8a04dSAlfred Perlstein void monitor_lock_host(const char *hostname); 163a4a8a04dSAlfred Perlstein void unmonitor_lock_host(const char *hostname); 1644945c132SAlfred Perlstein 165b6dc41baSAlfred Perlstein void copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock *src, 166b6dc41baSAlfred Perlstein const bool_t exclusive, struct nlm4_holder *dest); 167815ac496SAlfred Perlstein struct file_lock * allocate_file_lock(const netobj *lockowner, 168815ac496SAlfred Perlstein const netobj *matchcookie); 169b6dc41baSAlfred Perlstein void deallocate_file_lock(struct file_lock *fl); 170815ac496SAlfred Perlstein void fill_file_lock(struct file_lock *fl, const fhandle_t *fh, 171815ac496SAlfred Perlstein struct sockaddr *addr, const bool_t exclusive, const int32_t svid, 172815ac496SAlfred Perlstein const u_int64_t offset, const u_int64_t len, const char *caller_name, 173815ac496SAlfred Perlstein const int state, const int status, const int flags, const int blocking); 174b6dc41baSAlfred Perlstein int regions_overlap(const u_int64_t start1, const u_int64_t len1, 175b6dc41baSAlfred Perlstein const u_int64_t start2, const u_int64_t len2);; 1765f07c7b2SAlfred Perlstein enum split_status region_compare(const u_int64_t starte, const u_int64_t lene, 1775f07c7b2SAlfred Perlstein const u_int64_t startu, const u_int64_t lenu, 1785f07c7b2SAlfred Perlstein u_int64_t *start1, u_int64_t *len1, u_int64_t *start2, u_int64_t *len2); 179b6dc41baSAlfred Perlstein int same_netobj(const netobj *n0, const netobj *n1); 180b6dc41baSAlfred Perlstein int same_filelock_identity(const struct file_lock *fl0, 181b6dc41baSAlfred Perlstein const struct file_lock *fl2); 182b6dc41baSAlfred Perlstein 183b6dc41baSAlfred Perlstein static void debuglog(char const *fmt, ...); 184b6dc41baSAlfred Perlstein void dump_static_object(const unsigned char* object, const int sizeof_object, 185b6dc41baSAlfred Perlstein unsigned char* hbuff, const int sizeof_hbuff, 186b6dc41baSAlfred Perlstein unsigned char* cbuff, const int sizeof_cbuff); 187b6dc41baSAlfred Perlstein void dump_netobj(const struct netobj *nobj); 188b6dc41baSAlfred Perlstein void dump_filelock(const struct file_lock *fl); 189b6dc41baSAlfred Perlstein struct file_lock * get_lock_matching_unlock(const struct file_lock *fl); 190b6dc41baSAlfred Perlstein enum nfslock_status test_nfslock(const struct file_lock *fl, 191b6dc41baSAlfred Perlstein struct file_lock **conflicting_fl); 192b6dc41baSAlfred Perlstein enum nfslock_status lock_nfslock(struct file_lock *fl); 193b6dc41baSAlfred Perlstein enum nfslock_status delete_nfslock(struct file_lock *fl); 194b6dc41baSAlfred Perlstein enum nfslock_status unlock_nfslock(const struct file_lock *fl, 1955f07c7b2SAlfred Perlstein struct file_lock **released_lock, struct file_lock **left_lock, 1965f07c7b2SAlfred Perlstein struct file_lock **right_lock); 197b6dc41baSAlfred Perlstein enum hwlock_status lock_hwlock(struct file_lock *fl); 198b6dc41baSAlfred Perlstein enum split_status split_nfslock(const struct file_lock *exist_lock, 1995f07c7b2SAlfred Perlstein const struct file_lock *unlock_lock, struct file_lock **left_lock, 2005f07c7b2SAlfred Perlstein struct file_lock **right_lock); 201b6dc41baSAlfred Perlstein void add_blockingfilelock(struct file_lock *fl); 202b6dc41baSAlfred Perlstein enum hwlock_status unlock_hwlock(const struct file_lock *fl); 203b6dc41baSAlfred Perlstein enum hwlock_status test_hwlock(const struct file_lock *fl, 204b6dc41baSAlfred Perlstein struct file_lock **conflicting_fl); 205b6dc41baSAlfred Perlstein void remove_blockingfilelock(struct file_lock *fl); 206b6dc41baSAlfred Perlstein void clear_blockingfilelock(const char *hostname); 207b6dc41baSAlfred Perlstein void retry_blockingfilelocklist(void); 208b6dc41baSAlfred Perlstein enum partialfilelock_status unlock_partialfilelock( 209b6dc41baSAlfred Perlstein const struct file_lock *fl); 210b6dc41baSAlfred Perlstein void clear_partialfilelock(const char *hostname); 211b6dc41baSAlfred Perlstein enum partialfilelock_status test_partialfilelock( 212b6dc41baSAlfred Perlstein const struct file_lock *fl, struct file_lock **conflicting_fl); 213b6dc41baSAlfred Perlstein enum nlm_stats do_test(struct file_lock *fl, 214b6dc41baSAlfred Perlstein struct file_lock **conflicting_fl); 215b6dc41baSAlfred Perlstein enum nlm_stats do_unlock(struct file_lock *fl); 216b6dc41baSAlfred Perlstein enum nlm_stats do_lock(struct file_lock *fl); 217b6dc41baSAlfred Perlstein void do_clear(const char *hostname); 218b6dc41baSAlfred Perlstein 219b6dc41baSAlfred Perlstein 2204945c132SAlfred Perlstein void 2214945c132SAlfred Perlstein debuglog(char const *fmt, ...) 2224945c132SAlfred Perlstein { 2234945c132SAlfred Perlstein va_list ap; 2244945c132SAlfred Perlstein 2257e589b0bSAlfred Perlstein if (debug_level < 1) { 2264945c132SAlfred Perlstein return; 2277e589b0bSAlfred Perlstein } 2284945c132SAlfred Perlstein 2294945c132SAlfred Perlstein sleep(debugdelay); 2304945c132SAlfred Perlstein 2314945c132SAlfred Perlstein va_start(ap, fmt); 2324945c132SAlfred Perlstein vsyslog(LOG_DEBUG, fmt, ap); 2334945c132SAlfred Perlstein va_end(ap); 2344945c132SAlfred Perlstein } 2354945c132SAlfred Perlstein 236b6dc41baSAlfred Perlstein void 237b6dc41baSAlfred Perlstein dump_static_object(object, size_object, hbuff, size_hbuff, cbuff, size_cbuff) 238b6dc41baSAlfred Perlstein const unsigned char *object; 239b6dc41baSAlfred Perlstein const int size_object; 240b6dc41baSAlfred Perlstein unsigned char *hbuff; 241b6dc41baSAlfred Perlstein const int size_hbuff; 242b6dc41baSAlfred Perlstein unsigned char *cbuff; 243b6dc41baSAlfred Perlstein const int size_cbuff; 2444945c132SAlfred Perlstein { 2454945c132SAlfred Perlstein int i, objectsize; 2464945c132SAlfred Perlstein 2477e589b0bSAlfred Perlstein if (debug_level < 2) { 2484945c132SAlfred Perlstein return; 2494945c132SAlfred Perlstein } 2504945c132SAlfred Perlstein 251b6dc41baSAlfred Perlstein objectsize = size_object; 2527e589b0bSAlfred Perlstein 2537e589b0bSAlfred Perlstein if (objectsize == 0) { 2547e589b0bSAlfred Perlstein debuglog("object is size 0\n"); 2557e589b0bSAlfred Perlstein } else { 2564945c132SAlfred Perlstein if (objectsize > MAXOBJECTSIZE) { 2574945c132SAlfred Perlstein debuglog("Object of size %d being clamped" 2584945c132SAlfred Perlstein "to size %d\n", objectsize, MAXOBJECTSIZE); 2594945c132SAlfred Perlstein objectsize = MAXOBJECTSIZE; 2604945c132SAlfred Perlstein } 2614945c132SAlfred Perlstein 2624945c132SAlfred Perlstein if (hbuff != NULL) { 263b6dc41baSAlfred Perlstein if (size_hbuff < objectsize*2+1) { 2647e589b0bSAlfred Perlstein debuglog("Hbuff not large enough." 2657e589b0bSAlfred Perlstein " Increase size\n"); 2664945c132SAlfred Perlstein } else { 2674945c132SAlfred Perlstein for(i=0;i<objectsize;i++) { 2684945c132SAlfred Perlstein sprintf(hbuff+i*2,"%02x",*(object+i)); 2694945c132SAlfred Perlstein } 2704945c132SAlfred Perlstein *(hbuff+i*2) = '\0'; 2714945c132SAlfred Perlstein } 2724945c132SAlfred Perlstein } 2734945c132SAlfred Perlstein 2744945c132SAlfred Perlstein if (cbuff != NULL) { 275b6dc41baSAlfred Perlstein if (size_cbuff < objectsize+1) { 2764945c132SAlfred Perlstein debuglog("Cbuff not large enough." 2774945c132SAlfred Perlstein " Increase Size\n"); 2784945c132SAlfred Perlstein } 2794945c132SAlfred Perlstein 2804945c132SAlfred Perlstein for(i=0;i<objectsize;i++) { 2814945c132SAlfred Perlstein if (*(object+i) >= 32 && *(object+i) <= 127) { 2824945c132SAlfred Perlstein *(cbuff+i) = *(object+i); 2834945c132SAlfred Perlstein } else { 2844945c132SAlfred Perlstein *(cbuff+i) = '.'; 2854945c132SAlfred Perlstein } 2864945c132SAlfred Perlstein } 2874945c132SAlfred Perlstein *(cbuff+i) = '\0'; 2884945c132SAlfred Perlstein } 2894945c132SAlfred Perlstein } 2907e589b0bSAlfred Perlstein } 2914945c132SAlfred Perlstein 292b6dc41baSAlfred Perlstein void 293b6dc41baSAlfred Perlstein dump_netobj(const struct netobj *nobj) 2944945c132SAlfred Perlstein { 2954945c132SAlfred Perlstein char hbuff[MAXBUFFERSIZE*2]; 2964945c132SAlfred Perlstein char cbuff[MAXBUFFERSIZE]; 2974945c132SAlfred Perlstein 2987e589b0bSAlfred Perlstein if (debug_level < 2) { 2994945c132SAlfred Perlstein return; 3007e589b0bSAlfred Perlstein } 3014945c132SAlfred Perlstein 3024945c132SAlfred Perlstein if (nobj == NULL) { 3034945c132SAlfred Perlstein debuglog("Null netobj pointer\n"); 3047e589b0bSAlfred Perlstein } 3057e589b0bSAlfred Perlstein else if (nobj->n_len == 0) { 3064945c132SAlfred Perlstein debuglog("Size zero netobj\n"); 3074945c132SAlfred Perlstein } else { 3084945c132SAlfred Perlstein dump_static_object(nobj->n_bytes, nobj->n_len, 3094945c132SAlfred Perlstein hbuff, sizeof(hbuff), cbuff, sizeof(cbuff)); 3104945c132SAlfred Perlstein debuglog("netobj: len: %d data: %s ::: %s\n", 3114945c132SAlfred Perlstein nobj->n_len, hbuff, cbuff); 3124945c132SAlfred Perlstein } 3134945c132SAlfred Perlstein } 3144945c132SAlfred Perlstein 315c4bc47baSAlfred Perlstein /* #define DUMP_FILELOCK_VERBOSE */ 316b6dc41baSAlfred Perlstein void 317b6dc41baSAlfred Perlstein dump_filelock(const struct file_lock *fl) 3184945c132SAlfred Perlstein { 319c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE 3204945c132SAlfred Perlstein char hbuff[MAXBUFFERSIZE*2]; 3214945c132SAlfred Perlstein char cbuff[MAXBUFFERSIZE]; 322c4bc47baSAlfred Perlstein #endif 3234945c132SAlfred Perlstein 3247e589b0bSAlfred Perlstein if (debug_level < 2) { 3254945c132SAlfred Perlstein return; 3264945c132SAlfred Perlstein } 3274945c132SAlfred Perlstein 3287e589b0bSAlfred Perlstein if (fl != NULL) { 3295f07c7b2SAlfred Perlstein debuglog("Dumping file lock structure @ %p\n", fl); 3304945c132SAlfred Perlstein 331c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE 3324945c132SAlfred Perlstein dump_static_object((unsigned char *)&fl->filehandle, 3334945c132SAlfred Perlstein sizeof(fl->filehandle), hbuff, sizeof(hbuff), 3344945c132SAlfred Perlstein cbuff, sizeof(cbuff)); 3354945c132SAlfred Perlstein debuglog("Filehandle: %8s ::: %8s\n", hbuff, cbuff); 336c4bc47baSAlfred Perlstein #endif 3374945c132SAlfred Perlstein 338b6dc41baSAlfred Perlstein debuglog("Dumping nlm4_holder:\n" 339b6dc41baSAlfred Perlstein "exc: %x svid: %x offset:len %llx:%llx\n", 3404945c132SAlfred Perlstein fl->client.exclusive, fl->client.svid, 3414945c132SAlfred Perlstein fl->client.l_offset, fl->client.l_len); 3424945c132SAlfred Perlstein 343c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE 3444945c132SAlfred Perlstein debuglog("Dumping client identity:\n"); 3454945c132SAlfred Perlstein dump_netobj(&fl->client.oh); 3464945c132SAlfred Perlstein 3474945c132SAlfred Perlstein debuglog("Dumping client cookie:\n"); 3484945c132SAlfred Perlstein dump_netobj(&fl->client_cookie); 3494945c132SAlfred Perlstein 3504945c132SAlfred Perlstein debuglog("nsm: %d status: %d flags: %d locker: %d" 3514945c132SAlfred Perlstein " fd: %d\n", fl->nsm_status, fl->status, 3524945c132SAlfred Perlstein fl->flags, fl->locker, fl->fd); 353c4bc47baSAlfred Perlstein #endif 3547e589b0bSAlfred Perlstein } else { 3557e589b0bSAlfred Perlstein debuglog("NULL file lock structure\n"); 3567e589b0bSAlfred Perlstein } 3577e589b0bSAlfred Perlstein } 3587e589b0bSAlfred Perlstein 3594945c132SAlfred Perlstein void 360b6dc41baSAlfred Perlstein copy_nlm4_lock_to_nlm4_holder(src, exclusive, dest) 361b6dc41baSAlfred Perlstein const struct nlm4_lock *src; 362b6dc41baSAlfred Perlstein const bool_t exclusive; 363b6dc41baSAlfred Perlstein struct nlm4_holder *dest; 3644945c132SAlfred Perlstein { 365b6dc41baSAlfred Perlstein 3664945c132SAlfred Perlstein dest->exclusive = exclusive; 3674945c132SAlfred Perlstein dest->oh.n_len = src->oh.n_len; 3684945c132SAlfred Perlstein dest->oh.n_bytes = src->oh.n_bytes; 3694945c132SAlfred Perlstein dest->svid = src->svid; 3704945c132SAlfred Perlstein dest->l_offset = src->l_offset; 3714945c132SAlfred Perlstein dest->l_len = src->l_len; 3724945c132SAlfred Perlstein } 3734945c132SAlfred Perlstein 3745f07c7b2SAlfred Perlstein 3755f07c7b2SAlfred Perlstein /* 3765f07c7b2SAlfred Perlstein * allocate_file_lock: Create a lock with the given parameters 3775f07c7b2SAlfred Perlstein */ 3785f07c7b2SAlfred Perlstein 3795f07c7b2SAlfred Perlstein struct file_lock * 3805f07c7b2SAlfred Perlstein allocate_file_lock(const netobj *lockowner, const netobj *matchcookie) 3815f07c7b2SAlfred Perlstein { 3825f07c7b2SAlfred Perlstein struct file_lock *newfl; 3835f07c7b2SAlfred Perlstein 3845f07c7b2SAlfred Perlstein newfl = malloc(sizeof(struct file_lock)); 3855f07c7b2SAlfred Perlstein if (newfl == NULL) { 3865f07c7b2SAlfred Perlstein return NULL; 3875f07c7b2SAlfred Perlstein } 3885f07c7b2SAlfred Perlstein bzero(newfl, sizeof(newfl)); 3895f07c7b2SAlfred Perlstein 3905f07c7b2SAlfred Perlstein newfl->client.oh.n_bytes = malloc(lockowner->n_len); 3915f07c7b2SAlfred Perlstein if (newfl->client.oh.n_bytes == NULL) { 3925f07c7b2SAlfred Perlstein free(newfl); 3935f07c7b2SAlfred Perlstein return NULL; 3945f07c7b2SAlfred Perlstein } 3955f07c7b2SAlfred Perlstein newfl->client.oh.n_len = lockowner->n_len; 3965f07c7b2SAlfred Perlstein bcopy(lockowner->n_bytes, newfl->client.oh.n_bytes, lockowner->n_len); 3975f07c7b2SAlfred Perlstein 3985f07c7b2SAlfred Perlstein newfl->client_cookie.n_bytes = malloc(matchcookie->n_len); 3995f07c7b2SAlfred Perlstein if (newfl->client_cookie.n_bytes == NULL) { 4005f07c7b2SAlfred Perlstein free(newfl->client.oh.n_bytes); 4015f07c7b2SAlfred Perlstein free(newfl); 4025f07c7b2SAlfred Perlstein return NULL; 4035f07c7b2SAlfred Perlstein } 4045f07c7b2SAlfred Perlstein newfl->client_cookie.n_len = matchcookie->n_len; 4055f07c7b2SAlfred Perlstein bcopy(matchcookie->n_bytes, newfl->client_cookie.n_bytes, matchcookie->n_len); 4065f07c7b2SAlfred Perlstein 4075f07c7b2SAlfred Perlstein return newfl; 4085f07c7b2SAlfred Perlstein } 4095f07c7b2SAlfred Perlstein 4105f07c7b2SAlfred Perlstein /* 4115f07c7b2SAlfred Perlstein * file_file_lock: Force creation of a valid file lock 4125f07c7b2SAlfred Perlstein */ 4135f07c7b2SAlfred Perlstein void 414815ac496SAlfred Perlstein fill_file_lock(struct file_lock *fl, const fhandle_t *fh, 415815ac496SAlfred Perlstein struct sockaddr *addr, const bool_t exclusive, const int32_t svid, 416815ac496SAlfred Perlstein const u_int64_t offset, const u_int64_t len, const char *caller_name, 417815ac496SAlfred Perlstein const int state, const int status, const int flags, const int blocking) 4185f07c7b2SAlfred Perlstein { 4195f07c7b2SAlfred Perlstein bcopy(fh, &fl->filehandle, sizeof(fhandle_t)); 4205f07c7b2SAlfred Perlstein fl->addr = addr; 4215f07c7b2SAlfred Perlstein 4225f07c7b2SAlfred Perlstein fl->client.exclusive = exclusive; 4235f07c7b2SAlfred Perlstein fl->client.svid = svid; 4245f07c7b2SAlfred Perlstein fl->client.l_offset = offset; 4255f07c7b2SAlfred Perlstein fl->client.l_len = len; 4265f07c7b2SAlfred Perlstein 4275f07c7b2SAlfred Perlstein strncpy(fl->client_name, caller_name, SM_MAXSTRLEN); 4285f07c7b2SAlfred Perlstein 4295f07c7b2SAlfred Perlstein fl->nsm_status = state; 4305f07c7b2SAlfred Perlstein fl->status = status; 4315f07c7b2SAlfred Perlstein fl->flags = flags; 4325f07c7b2SAlfred Perlstein fl->blocking = blocking; 4335f07c7b2SAlfred Perlstein } 4345f07c7b2SAlfred Perlstein 4354945c132SAlfred Perlstein /* 4364945c132SAlfred Perlstein * deallocate_file_lock: Free all storage associated with a file lock 4374945c132SAlfred Perlstein */ 4384945c132SAlfred Perlstein void 4394945c132SAlfred Perlstein deallocate_file_lock(struct file_lock *fl) 4404945c132SAlfred Perlstein { 4414945c132SAlfred Perlstein free(fl->client.oh.n_bytes); 4424945c132SAlfred Perlstein free(fl->client_cookie.n_bytes); 4434945c132SAlfred Perlstein free(fl); 4444945c132SAlfred Perlstein } 4454945c132SAlfred Perlstein 4464945c132SAlfred Perlstein /* 4474945c132SAlfred Perlstein * regions_overlap(): This function examines the two provided regions for 4485f07c7b2SAlfred Perlstein * overlap. 4494945c132SAlfred Perlstein */ 4504945c132SAlfred Perlstein int 451b6dc41baSAlfred Perlstein regions_overlap(start1, len1, start2, len2) 452b6dc41baSAlfred Perlstein const u_int64_t start1, len1, start2, len2; 4534945c132SAlfred Perlstein { 4545f07c7b2SAlfred Perlstein u_int64_t d1,d2,d3,d4; 4555f07c7b2SAlfred Perlstein enum split_status result; 4567e589b0bSAlfred Perlstein 4574945c132SAlfred Perlstein debuglog("Entering region overlap with vals: %llu:%llu--%llu:%llu\n", 4584945c132SAlfred Perlstein start1, len1, start2, len2); 4594945c132SAlfred Perlstein 4605f07c7b2SAlfred Perlstein result = region_compare(start1, len1, start2, len2, 4615f07c7b2SAlfred Perlstein &d1, &d2, &d3, &d4); 4624945c132SAlfred Perlstein 4637e589b0bSAlfred Perlstein debuglog("Exiting region overlap with val: %d\n",result); 4647e589b0bSAlfred Perlstein 4655f07c7b2SAlfred Perlstein if (result == SPL_DISJOINT) { 4665f07c7b2SAlfred Perlstein return 0; 4675f07c7b2SAlfred Perlstein } else { 4685f07c7b2SAlfred Perlstein return 1; 4695f07c7b2SAlfred Perlstein } 4705f07c7b2SAlfred Perlstein 4717e589b0bSAlfred Perlstein return (result); 4727e589b0bSAlfred Perlstein } 4737e589b0bSAlfred Perlstein 4744945c132SAlfred Perlstein /* 4755f07c7b2SAlfred Perlstein * region_compare(): Examine lock regions and split appropriately 4765f07c7b2SAlfred Perlstein * 4775f07c7b2SAlfred Perlstein * XXX: Fix 64 bit overflow problems 4785f07c7b2SAlfred Perlstein * XXX: Check to make sure I got *ALL* the cases. 4795f07c7b2SAlfred Perlstein * XXX: This DESPERATELY needs a regression test. 4805f07c7b2SAlfred Perlstein */ 4815f07c7b2SAlfred Perlstein enum split_status 4825f07c7b2SAlfred Perlstein region_compare(starte, lene, startu, lenu, 4835f07c7b2SAlfred Perlstein start1, len1, start2, len2) 4845f07c7b2SAlfred Perlstein const u_int64_t starte, lene, startu, lenu; 4855f07c7b2SAlfred Perlstein u_int64_t *start1, *len1, *start2, *len2; 4865f07c7b2SAlfred Perlstein { 4875f07c7b2SAlfred Perlstein /* 4885f07c7b2SAlfred Perlstein * Please pay attention to the sequential exclusions 4895f07c7b2SAlfred Perlstein * of the if statements!!! 4905f07c7b2SAlfred Perlstein */ 4915f07c7b2SAlfred Perlstein enum LFLAGS lflags; 4925f07c7b2SAlfred Perlstein enum RFLAGS rflags; 4935f07c7b2SAlfred Perlstein enum split_status retval; 4945f07c7b2SAlfred Perlstein 4955f07c7b2SAlfred Perlstein retval = SPL_DISJOINT; 4965f07c7b2SAlfred Perlstein 4975f07c7b2SAlfred Perlstein if (lene == 0 && lenu == 0) { 4985f07c7b2SAlfred Perlstein /* Examine left edge of locker */ 4995f07c7b2SAlfred Perlstein if (startu < starte) { 5005f07c7b2SAlfred Perlstein lflags = LEDGE_LEFT; 5015f07c7b2SAlfred Perlstein } else if (startu == starte) { 5025f07c7b2SAlfred Perlstein lflags = LEDGE_LBOUNDARY; 5035f07c7b2SAlfred Perlstein } else { 5045f07c7b2SAlfred Perlstein lflags = LEDGE_INSIDE; 5055f07c7b2SAlfred Perlstein } 5065f07c7b2SAlfred Perlstein 5075f07c7b2SAlfred Perlstein rflags = REDGE_RBOUNDARY; /* Both are infiinite */ 5085f07c7b2SAlfred Perlstein 5095f07c7b2SAlfred Perlstein if (lflags == LEDGE_INSIDE) { 5105f07c7b2SAlfred Perlstein *start1 = starte; 5115f07c7b2SAlfred Perlstein *len1 = startu - starte; 5125f07c7b2SAlfred Perlstein } 5135f07c7b2SAlfred Perlstein 5145f07c7b2SAlfred Perlstein if (lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) { 5155f07c7b2SAlfred Perlstein retval = SPL_CONTAINED; 5165f07c7b2SAlfred Perlstein } else { 5175f07c7b2SAlfred Perlstein retval = SPL_LOCK1; 5185f07c7b2SAlfred Perlstein } 5195f07c7b2SAlfred Perlstein } else if (lene == 0 && lenu != 0) { 5205f07c7b2SAlfred Perlstein /* Established lock is infinite */ 5215f07c7b2SAlfred Perlstein /* Examine left edge of unlocker */ 5225f07c7b2SAlfred Perlstein if (startu < starte) { 5235f07c7b2SAlfred Perlstein lflags = LEDGE_LEFT; 5245f07c7b2SAlfred Perlstein } else if (startu == starte) { 5255f07c7b2SAlfred Perlstein lflags = LEDGE_LBOUNDARY; 5265f07c7b2SAlfred Perlstein } else if (startu > starte) { 5275f07c7b2SAlfred Perlstein lflags = LEDGE_INSIDE; 5285f07c7b2SAlfred Perlstein } 5295f07c7b2SAlfred Perlstein 5305f07c7b2SAlfred Perlstein /* Examine right edge of unlocker */ 5315f07c7b2SAlfred Perlstein if (startu + lenu < starte) { 5325f07c7b2SAlfred Perlstein /* Right edge of unlocker left of established lock */ 5335f07c7b2SAlfred Perlstein rflags = REDGE_LEFT; 5345f07c7b2SAlfred Perlstein return SPL_DISJOINT; 5355f07c7b2SAlfred Perlstein } else if (startu + lenu == starte) { 5365f07c7b2SAlfred Perlstein /* Right edge of unlocker on start of established lock */ 5375f07c7b2SAlfred Perlstein rflags = REDGE_LBOUNDARY; 5385f07c7b2SAlfred Perlstein return SPL_DISJOINT; 5395f07c7b2SAlfred Perlstein } else { /* Infinifty is right of finity */ 5405f07c7b2SAlfred Perlstein /* Right edge of unlocker inside established lock */ 5415f07c7b2SAlfred Perlstein rflags = REDGE_INSIDE; 5425f07c7b2SAlfred Perlstein } 5435f07c7b2SAlfred Perlstein 5445f07c7b2SAlfred Perlstein if (lflags == LEDGE_INSIDE) { 5455f07c7b2SAlfred Perlstein *start1 = starte; 5465f07c7b2SAlfred Perlstein *len1 = startu - starte; 5475f07c7b2SAlfred Perlstein retval |= SPL_LOCK1; 5485f07c7b2SAlfred Perlstein } 5495f07c7b2SAlfred Perlstein 5505f07c7b2SAlfred Perlstein if (rflags == REDGE_INSIDE) { 5515f07c7b2SAlfred Perlstein /* Create right lock */ 5525f07c7b2SAlfred Perlstein *start2 = startu+lenu; 5535f07c7b2SAlfred Perlstein *len2 = 0; 5545f07c7b2SAlfred Perlstein retval |= SPL_LOCK2; 5555f07c7b2SAlfred Perlstein } 5565f07c7b2SAlfred Perlstein } else if (lene != 0 && lenu == 0) { 5575f07c7b2SAlfred Perlstein /* Unlocker is infinite */ 5585f07c7b2SAlfred Perlstein /* Examine left edge of unlocker */ 5595f07c7b2SAlfred Perlstein if (startu < starte) { 5605f07c7b2SAlfred Perlstein lflags = LEDGE_LEFT; 5615f07c7b2SAlfred Perlstein retval = SPL_CONTAINED; 5625f07c7b2SAlfred Perlstein return retval; 5635f07c7b2SAlfred Perlstein } else if (startu == starte) { 5645f07c7b2SAlfred Perlstein lflags = LEDGE_LBOUNDARY; 5655f07c7b2SAlfred Perlstein retval = SPL_CONTAINED; 5665f07c7b2SAlfred Perlstein return retval; 5675f07c7b2SAlfred Perlstein } else if ((startu > starte) && (startu < starte + lene - 1)) { 5685f07c7b2SAlfred Perlstein lflags = LEDGE_INSIDE; 5695f07c7b2SAlfred Perlstein } else if (startu == starte + lene - 1) { 5705f07c7b2SAlfred Perlstein lflags = LEDGE_RBOUNDARY; 5715f07c7b2SAlfred Perlstein } else { /* startu > starte + lene -1 */ 5725f07c7b2SAlfred Perlstein lflags = LEDGE_RIGHT; 5735f07c7b2SAlfred Perlstein return SPL_DISJOINT; 5745f07c7b2SAlfred Perlstein } 5755f07c7b2SAlfred Perlstein 5765f07c7b2SAlfred Perlstein rflags = REDGE_RIGHT; /* Infinity is right of finity */ 5775f07c7b2SAlfred Perlstein 5785f07c7b2SAlfred Perlstein if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) { 5795f07c7b2SAlfred Perlstein *start1 = starte; 5805f07c7b2SAlfred Perlstein *len1 = startu - starte; 5815f07c7b2SAlfred Perlstein retval |= SPL_LOCK1; 5825f07c7b2SAlfred Perlstein return retval; 5835f07c7b2SAlfred Perlstein } 5845f07c7b2SAlfred Perlstein 5855f07c7b2SAlfred Perlstein } else { 5865f07c7b2SAlfred Perlstein /* Both locks are finite */ 5875f07c7b2SAlfred Perlstein 5885f07c7b2SAlfred Perlstein /* Examine left edge of unlocker */ 5895f07c7b2SAlfred Perlstein if (startu < starte) { 5905f07c7b2SAlfred Perlstein lflags = LEDGE_LEFT; 5915f07c7b2SAlfred Perlstein } else if (startu == starte) { 5925f07c7b2SAlfred Perlstein lflags = LEDGE_LBOUNDARY; 5935f07c7b2SAlfred Perlstein } else if ((startu > starte) && (startu < starte + lene - 1)) { 5945f07c7b2SAlfred Perlstein lflags = LEDGE_INSIDE; 5955f07c7b2SAlfred Perlstein } else if (startu == starte + lene - 1) { 5965f07c7b2SAlfred Perlstein lflags = LEDGE_RBOUNDARY; 5975f07c7b2SAlfred Perlstein } else { /* startu > starte + lene -1 */ 5985f07c7b2SAlfred Perlstein lflags = LEDGE_RIGHT; 5995f07c7b2SAlfred Perlstein return SPL_DISJOINT; 6005f07c7b2SAlfred Perlstein } 6015f07c7b2SAlfred Perlstein 6025f07c7b2SAlfred Perlstein /* Examine right edge of unlocker */ 6035f07c7b2SAlfred Perlstein if (startu + lenu < starte) { 6045f07c7b2SAlfred Perlstein /* Right edge of unlocker left of established lock */ 6055f07c7b2SAlfred Perlstein rflags = REDGE_LEFT; 6065f07c7b2SAlfred Perlstein return SPL_DISJOINT; 6075f07c7b2SAlfred Perlstein } else if (startu + lenu == starte) { 6085f07c7b2SAlfred Perlstein /* Right edge of unlocker on start of established lock */ 6095f07c7b2SAlfred Perlstein rflags = REDGE_LBOUNDARY; 6105f07c7b2SAlfred Perlstein return SPL_DISJOINT; 6115f07c7b2SAlfred Perlstein } else if (startu + lenu < starte + lene) { 6125f07c7b2SAlfred Perlstein /* Right edge of unlocker inside established lock */ 6135f07c7b2SAlfred Perlstein rflags = REDGE_INSIDE; 6145f07c7b2SAlfred Perlstein } else if (startu + lenu == starte + lene) { 6155f07c7b2SAlfred Perlstein /* Right edge of unlocker on right edge of established lock */ 6165f07c7b2SAlfred Perlstein rflags = REDGE_RBOUNDARY; 6175f07c7b2SAlfred Perlstein } else { /* startu + lenu > starte + lene */ 6185f07c7b2SAlfred Perlstein /* Right edge of unlocker is right of established lock */ 6195f07c7b2SAlfred Perlstein rflags = REDGE_RIGHT; 6205f07c7b2SAlfred Perlstein } 6215f07c7b2SAlfred Perlstein 6225f07c7b2SAlfred Perlstein if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) { 6235f07c7b2SAlfred Perlstein /* Create left lock */ 6245f07c7b2SAlfred Perlstein *start1 = starte; 6255f07c7b2SAlfred Perlstein *len1 = (startu - starte); 6265f07c7b2SAlfred Perlstein retval |= SPL_LOCK1; 6275f07c7b2SAlfred Perlstein } 6285f07c7b2SAlfred Perlstein 6295f07c7b2SAlfred Perlstein if (rflags == REDGE_INSIDE) { 6305f07c7b2SAlfred Perlstein /* Create right lock */ 6315f07c7b2SAlfred Perlstein *start2 = startu+lenu; 6325f07c7b2SAlfred Perlstein *len2 = starte+lene-(startu+lenu); 6335f07c7b2SAlfred Perlstein retval |= SPL_LOCK2; 6345f07c7b2SAlfred Perlstein } 6355f07c7b2SAlfred Perlstein 6365f07c7b2SAlfred Perlstein if ((lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) && 6375f07c7b2SAlfred Perlstein (rflags == REDGE_RBOUNDARY || rflags == REDGE_RIGHT)) { 6385f07c7b2SAlfred Perlstein retval = SPL_CONTAINED; 6395f07c7b2SAlfred Perlstein } 6405f07c7b2SAlfred Perlstein } 6415f07c7b2SAlfred Perlstein 6425f07c7b2SAlfred Perlstein return retval; 6435f07c7b2SAlfred Perlstein } 6445f07c7b2SAlfred Perlstein 6455f07c7b2SAlfred Perlstein /* 6464945c132SAlfred Perlstein * same_netobj: Compares the apprpriate bits of a netobj for identity 6474945c132SAlfred Perlstein */ 6484945c132SAlfred Perlstein int 6494945c132SAlfred Perlstein same_netobj(const netobj *n0, const netobj *n1) 6504945c132SAlfred Perlstein { 6514945c132SAlfred Perlstein int retval; 6524945c132SAlfred Perlstein 6534945c132SAlfred Perlstein retval = 0; 6544945c132SAlfred Perlstein 6554945c132SAlfred Perlstein debuglog("Entering netobj identity check\n"); 6564945c132SAlfred Perlstein 6574945c132SAlfred Perlstein if (n0->n_len == n1->n_len) { 6584945c132SAlfred Perlstein debuglog("Preliminary length check passed\n"); 659b6dc41baSAlfred Perlstein retval = !bcmp(n0->n_bytes, n1->n_bytes, n0->n_len); 660b6dc41baSAlfred Perlstein debuglog("netobj %smatch\n", retval ? "" : "mis"); 6614945c132SAlfred Perlstein } 6624945c132SAlfred Perlstein 663b6dc41baSAlfred Perlstein return (retval); 6644945c132SAlfred Perlstein } 6654945c132SAlfred Perlstein 6664945c132SAlfred Perlstein /* 6674945c132SAlfred Perlstein * same_filelock_identity: Compares the appropriate bits of a file_lock 6684945c132SAlfred Perlstein */ 6694945c132SAlfred Perlstein int 670b6dc41baSAlfred Perlstein same_filelock_identity(fl0, fl1) 671b6dc41baSAlfred Perlstein const struct file_lock *fl0, *fl1; 6724945c132SAlfred Perlstein { 6734945c132SAlfred Perlstein int retval; 6744945c132SAlfred Perlstein 6754945c132SAlfred Perlstein retval = 0; 6764945c132SAlfred Perlstein 6774945c132SAlfred Perlstein debuglog("Checking filelock identity\n"); 6784945c132SAlfred Perlstein 679b6dc41baSAlfred Perlstein /* 680b6dc41baSAlfred Perlstein * Check process ids and host information. 681b6dc41baSAlfred Perlstein */ 682b6dc41baSAlfred Perlstein retval = (fl0->client.svid == fl1->client.svid && 683b6dc41baSAlfred Perlstein same_netobj(&(fl0->client.oh), &(fl1->client.oh))); 6844945c132SAlfred Perlstein 6854945c132SAlfred Perlstein debuglog("Exiting checking filelock identity: retval: %d\n",retval); 6864945c132SAlfred Perlstein 687b6dc41baSAlfred Perlstein return (retval); 6884945c132SAlfred Perlstein } 6894945c132SAlfred Perlstein 6904945c132SAlfred Perlstein /* 6914945c132SAlfred Perlstein * Below here are routines associated with manipulating the NFS 6924945c132SAlfred Perlstein * lock list. 6934945c132SAlfred Perlstein */ 6944945c132SAlfred Perlstein 6954945c132SAlfred Perlstein /* 6964945c132SAlfred Perlstein * get_lock_matching_unlock: Return a lock which matches the given unlock lock 6974945c132SAlfred Perlstein * or NULL otehrwise 698b6dc41baSAlfred Perlstein * XXX: It is a shame that this duplicates so much code from test_nfslock. 6994945c132SAlfred Perlstein */ 7004945c132SAlfred Perlstein struct file_lock * 7014945c132SAlfred Perlstein get_lock_matching_unlock(const struct file_lock *fl) 7024945c132SAlfred Perlstein { 7034945c132SAlfred Perlstein struct file_lock *ifl; /* Iterator */ 7044945c132SAlfred Perlstein 7054945c132SAlfred Perlstein debuglog("Entering lock_matching_unlock\n"); 7064945c132SAlfred Perlstein debuglog("********Dump of fl*****************\n"); 7074945c132SAlfred Perlstein dump_filelock(fl); 7084945c132SAlfred Perlstein 709b6dc41baSAlfred Perlstein LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) { 7104945c132SAlfred Perlstein debuglog("Pointer to file lock: %p\n",ifl); 7114945c132SAlfred Perlstein 7124945c132SAlfred Perlstein debuglog("****Dump of ifl****\n"); 7134945c132SAlfred Perlstein dump_filelock(ifl); 7144945c132SAlfred Perlstein debuglog("*******************\n"); 7154945c132SAlfred Perlstein 7164945c132SAlfred Perlstein /* 7174945c132SAlfred Perlstein * XXX: It is conceivable that someone could use the NLM RPC 7184945c132SAlfred Perlstein * system to directly access filehandles. This may be a 7194945c132SAlfred Perlstein * security hazard as the filehandle code may bypass normal 7204945c132SAlfred Perlstein * file access controls 7214945c132SAlfred Perlstein */ 722b6dc41baSAlfred Perlstein if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t))) 723b6dc41baSAlfred Perlstein continue; 724b6dc41baSAlfred Perlstein 725b6dc41baSAlfred Perlstein debuglog("matching_unlock: Filehandles match, " 726b6dc41baSAlfred Perlstein "checking regions\n"); 7274945c132SAlfred Perlstein 7284945c132SAlfred Perlstein /* Filehandles match, check for region overlap */ 729b6dc41baSAlfred Perlstein if (!regions_overlap(fl->client.l_offset, fl->client.l_len, 730b6dc41baSAlfred Perlstein ifl->client.l_offset, ifl->client.l_len)) 731b6dc41baSAlfred Perlstein continue; 732b6dc41baSAlfred Perlstein 7334945c132SAlfred Perlstein debuglog("matching_unlock: Region overlap" 7344945c132SAlfred Perlstein " found %llu : %llu -- %llu : %llu\n", 7354945c132SAlfred Perlstein fl->client.l_offset,fl->client.l_len, 7364945c132SAlfred Perlstein ifl->client.l_offset,ifl->client.l_len); 7374945c132SAlfred Perlstein 7384945c132SAlfred Perlstein /* Regions overlap, check the identity */ 739b6dc41baSAlfred Perlstein if (!same_filelock_identity(fl,ifl)) 740b6dc41baSAlfred Perlstein continue; 741b6dc41baSAlfred Perlstein 742b6dc41baSAlfred Perlstein debuglog("matching_unlock: Duplicate lock id. Granting\n"); 743b6dc41baSAlfred Perlstein return (ifl); 7444945c132SAlfred Perlstein } 7454945c132SAlfred Perlstein 7464945c132SAlfred Perlstein debuglog("Exiting lock_matching_unlock\n"); 7474945c132SAlfred Perlstein 748b6dc41baSAlfred Perlstein return (NULL); 7494945c132SAlfred Perlstein } 7504945c132SAlfred Perlstein 7514945c132SAlfred Perlstein /* 7524945c132SAlfred Perlstein * test_nfslock: check for NFS lock in lock list 7534945c132SAlfred Perlstein * 7544945c132SAlfred Perlstein * This routine makes the following assumptions: 7554945c132SAlfred Perlstein * 1) Nothing will adjust the lock list during a lookup 7564945c132SAlfred Perlstein * 7574945c132SAlfred Perlstein * This routine has an intersting quirk which bit me hard. 7584945c132SAlfred Perlstein * The conflicting_fl is the pointer to the conflicting lock. 7594945c132SAlfred Perlstein * However, to modify the "*pointer* to the conflicting lock" rather 7604945c132SAlfred Perlstein * that the "conflicting lock itself" one must pass in a "pointer to 7614945c132SAlfred Perlstein * the pointer of the conflicting lock". Gross. 7624945c132SAlfred Perlstein */ 7634945c132SAlfred Perlstein 7644945c132SAlfred Perlstein enum nfslock_status 7654945c132SAlfred Perlstein test_nfslock(const struct file_lock *fl, struct file_lock **conflicting_fl) 7664945c132SAlfred Perlstein { 7674945c132SAlfred Perlstein struct file_lock *ifl; /* Iterator */ 7684945c132SAlfred Perlstein enum nfslock_status retval; 7694945c132SAlfred Perlstein 7707e589b0bSAlfred Perlstein debuglog("Entering test_nfslock\n"); 7714945c132SAlfred Perlstein 7724945c132SAlfred Perlstein retval = NFS_GRANTED; 7734945c132SAlfred Perlstein (*conflicting_fl) = NULL; 7744945c132SAlfred Perlstein 7754945c132SAlfred Perlstein debuglog("Entering lock search loop\n"); 7764945c132SAlfred Perlstein 7774945c132SAlfred Perlstein debuglog("***********************************\n"); 7784945c132SAlfred Perlstein debuglog("Dumping match filelock\n"); 7794945c132SAlfred Perlstein debuglog("***********************************\n"); 7804945c132SAlfred Perlstein dump_filelock(fl); 7814945c132SAlfred Perlstein debuglog("***********************************\n"); 7824945c132SAlfred Perlstein 783b6dc41baSAlfred Perlstein LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) { 784b6dc41baSAlfred Perlstein if (retval == NFS_DENIED) 785b6dc41baSAlfred Perlstein break; 786b6dc41baSAlfred Perlstein 7874945c132SAlfred Perlstein debuglog("Top of lock loop\n"); 7884945c132SAlfred Perlstein debuglog("Pointer to file lock: %p\n",ifl); 7894945c132SAlfred Perlstein 7904945c132SAlfred Perlstein debuglog("***********************************\n"); 7914945c132SAlfred Perlstein debuglog("Dumping test filelock\n"); 7924945c132SAlfred Perlstein debuglog("***********************************\n"); 7934945c132SAlfred Perlstein dump_filelock(ifl); 7944945c132SAlfred Perlstein debuglog("***********************************\n"); 7954945c132SAlfred Perlstein 7964945c132SAlfred Perlstein /* 7974945c132SAlfred Perlstein * XXX: It is conceivable that someone could use the NLM RPC 7984945c132SAlfred Perlstein * system to directly access filehandles. This may be a 7994945c132SAlfred Perlstein * security hazard as the filehandle code may bypass normal 8004945c132SAlfred Perlstein * file access controls 8014945c132SAlfred Perlstein */ 802b6dc41baSAlfred Perlstein if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t))) 803b6dc41baSAlfred Perlstein continue; 804b6dc41baSAlfred Perlstein 8054945c132SAlfred Perlstein debuglog("test_nfslock: filehandle match found\n"); 8064945c132SAlfred Perlstein 8074945c132SAlfred Perlstein /* Filehandles match, check for region overlap */ 808b6dc41baSAlfred Perlstein if (!regions_overlap(fl->client.l_offset, fl->client.l_len, 809b6dc41baSAlfred Perlstein ifl->client.l_offset, ifl->client.l_len)) 810b6dc41baSAlfred Perlstein continue; 811b6dc41baSAlfred Perlstein 8124945c132SAlfred Perlstein debuglog("test_nfslock: Region overlap found" 8134945c132SAlfred Perlstein " %llu : %llu -- %llu : %llu\n", 8144945c132SAlfred Perlstein fl->client.l_offset,fl->client.l_len, 8154945c132SAlfred Perlstein ifl->client.l_offset,ifl->client.l_len); 8164945c132SAlfred Perlstein 8174945c132SAlfred Perlstein /* Regions overlap, check the exclusivity */ 818b6dc41baSAlfred Perlstein if (!(fl->client.exclusive || ifl->client.exclusive)) 819b6dc41baSAlfred Perlstein continue; 820b6dc41baSAlfred Perlstein 821b6dc41baSAlfred Perlstein debuglog("test_nfslock: Exclusivity failure: %d %d\n", 8224945c132SAlfred Perlstein fl->client.exclusive, 8234945c132SAlfred Perlstein ifl->client.exclusive); 8244945c132SAlfred Perlstein 8254945c132SAlfred Perlstein if (same_filelock_identity(fl,ifl)) { 826b6dc41baSAlfred Perlstein debuglog("test_nfslock: Duplicate id. Granting\n"); 8274945c132SAlfred Perlstein (*conflicting_fl) = ifl; 8284945c132SAlfred Perlstein retval = NFS_GRANTED_DUPLICATE; 8294945c132SAlfred Perlstein } else { 8304945c132SAlfred Perlstein /* locking attempt fails */ 831b6dc41baSAlfred Perlstein debuglog("test_nfslock: Lock attempt failed\n"); 8324945c132SAlfred Perlstein debuglog("Desired lock\n"); 8334945c132SAlfred Perlstein dump_filelock(fl); 8344945c132SAlfred Perlstein debuglog("Conflicting lock\n"); 8354945c132SAlfred Perlstein dump_filelock(ifl); 8364945c132SAlfred Perlstein (*conflicting_fl) = ifl; 8374945c132SAlfred Perlstein retval = NFS_DENIED; 8384945c132SAlfred Perlstein } 8394945c132SAlfred Perlstein } 8404945c132SAlfred Perlstein 8414945c132SAlfred Perlstein debuglog("Dumping file locks\n"); 8424945c132SAlfred Perlstein debuglog("Exiting test_nfslock\n"); 8434945c132SAlfred Perlstein 844b6dc41baSAlfred Perlstein return (retval); 8454945c132SAlfred Perlstein } 8464945c132SAlfred Perlstein 8474945c132SAlfred Perlstein /* 8484945c132SAlfred Perlstein * lock_nfslock: attempt to create a lock in the NFS lock list 8494945c132SAlfred Perlstein * 8504945c132SAlfred Perlstein * This routine tests whether the lock will be granted and then adds 8514945c132SAlfred Perlstein * the entry to the lock list if so. 8524945c132SAlfred Perlstein * 8534945c132SAlfred Perlstein * Argument fl gets modified as its list housekeeping entries get modified 8544945c132SAlfred Perlstein * upon insertion into the NFS lock list 8554945c132SAlfred Perlstein * 8564945c132SAlfred Perlstein * This routine makes several assumptions: 8574945c132SAlfred Perlstein * 1) It is perfectly happy to grant a duplicate lock from the same pid. 8584945c132SAlfred Perlstein * While this seems to be intuitively wrong, it is required for proper 8594945c132SAlfred Perlstein * Posix semantics during unlock. It is absolutely imperative to not 8604945c132SAlfred Perlstein * unlock the main lock before the two child locks are established. Thus, 8614945c132SAlfred Perlstein * one has be be able to create duplicate locks over an existing lock 8624945c132SAlfred Perlstein * 2) It currently accepts duplicate locks from the same id,pid 8634945c132SAlfred Perlstein */ 8644945c132SAlfred Perlstein 8654945c132SAlfred Perlstein enum nfslock_status 8664945c132SAlfred Perlstein lock_nfslock(struct file_lock *fl) 8674945c132SAlfred Perlstein { 8684945c132SAlfred Perlstein enum nfslock_status retval; 8694945c132SAlfred Perlstein struct file_lock *dummy_fl; 8704945c132SAlfred Perlstein 8714945c132SAlfred Perlstein dummy_fl = NULL; 8724945c132SAlfred Perlstein 8734945c132SAlfred Perlstein debuglog("Entering lock_nfslock...\n"); 8744945c132SAlfred Perlstein 8754945c132SAlfred Perlstein retval = test_nfslock(fl,&dummy_fl); 8764945c132SAlfred Perlstein 8774945c132SAlfred Perlstein if (retval == NFS_GRANTED || retval == NFS_GRANTED_DUPLICATE) { 8784945c132SAlfred Perlstein debuglog("Inserting lock...\n"); 8794945c132SAlfred Perlstein dump_filelock(fl); 8804945c132SAlfred Perlstein LIST_INSERT_HEAD(&nfslocklist_head, fl, nfslocklist); 8814945c132SAlfred Perlstein } 8824945c132SAlfred Perlstein 8834945c132SAlfred Perlstein debuglog("Exiting lock_nfslock...\n"); 8844945c132SAlfred Perlstein 885b6dc41baSAlfred Perlstein return (retval); 8864945c132SAlfred Perlstein } 8874945c132SAlfred Perlstein 8884945c132SAlfred Perlstein /* 8894945c132SAlfred Perlstein * delete_nfslock: delete an NFS lock list entry 8904945c132SAlfred Perlstein * 8914945c132SAlfred Perlstein * This routine is used to delete a lock out of the NFS lock list 8924945c132SAlfred Perlstein * without regard to status, underlying locks, regions or anything else 8934945c132SAlfred Perlstein * 8947e589b0bSAlfred Perlstein * Note that this routine *does not deallocate memory* of the lock. 8957e589b0bSAlfred Perlstein * It just disconnects it from the list. The lock can then be used 8967e589b0bSAlfred Perlstein * by other routines without fear of trashing the list. 8974945c132SAlfred Perlstein */ 8984945c132SAlfred Perlstein 8994945c132SAlfred Perlstein enum nfslock_status 9007e589b0bSAlfred Perlstein delete_nfslock(struct file_lock *fl) 9014945c132SAlfred Perlstein { 902b6dc41baSAlfred Perlstein 9037e589b0bSAlfred Perlstein LIST_REMOVE(fl, nfslocklist); 9047e589b0bSAlfred Perlstein 905b6dc41baSAlfred Perlstein return (NFS_GRANTED); 9064945c132SAlfred Perlstein } 9074945c132SAlfred Perlstein 9084945c132SAlfred Perlstein enum split_status 9095f07c7b2SAlfred Perlstein split_nfslock(exist_lock, unlock_lock, left_lock, right_lock) 9105f07c7b2SAlfred Perlstein const struct file_lock *exist_lock, *unlock_lock; 9115f07c7b2SAlfred Perlstein struct file_lock **left_lock, **right_lock; 9124945c132SAlfred Perlstein { 9135f07c7b2SAlfred Perlstein u_int64_t start1, len1, start2, len2; 9145f07c7b2SAlfred Perlstein enum split_status spstatus; 915b6dc41baSAlfred Perlstein 9165f07c7b2SAlfred Perlstein spstatus = region_compare(exist_lock->client.l_offset, exist_lock->client.l_len, 9175f07c7b2SAlfred Perlstein unlock_lock->client.l_offset, unlock_lock->client.l_len, 9185f07c7b2SAlfred Perlstein &start1, &len1, &start2, &len2); 9195f07c7b2SAlfred Perlstein 9205f07c7b2SAlfred Perlstein if ((spstatus & SPL_LOCK1) != 0) { 9215f07c7b2SAlfred Perlstein *left_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie); 9225f07c7b2SAlfred Perlstein if (*left_lock == NULL) { 9235f07c7b2SAlfred Perlstein debuglog("Unable to allocate resource for split 1\n"); 9245f07c7b2SAlfred Perlstein return SPL_RESERR; 9255f07c7b2SAlfred Perlstein } 9265f07c7b2SAlfred Perlstein 927815ac496SAlfred Perlstein fill_file_lock(*left_lock, &exist_lock->filehandle, 928815ac496SAlfred Perlstein exist_lock->addr, 9295f07c7b2SAlfred Perlstein exist_lock->client.exclusive, exist_lock->client.svid, 9305f07c7b2SAlfred Perlstein start1, len1, 9315f07c7b2SAlfred Perlstein exist_lock->client_name, exist_lock->nsm_status, 9325f07c7b2SAlfred Perlstein exist_lock->status, exist_lock->flags, exist_lock->blocking); 9335f07c7b2SAlfred Perlstein } 9345f07c7b2SAlfred Perlstein 9355f07c7b2SAlfred Perlstein if ((spstatus & SPL_LOCK2) != 0) { 9365f07c7b2SAlfred Perlstein *right_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie); 9375f07c7b2SAlfred Perlstein if (*right_lock == NULL) { 9385f07c7b2SAlfred Perlstein debuglog("Unable to allocate resource for split 1\n"); 9395f07c7b2SAlfred Perlstein if (*left_lock != NULL) { 9405f07c7b2SAlfred Perlstein deallocate_file_lock(*left_lock); 9415f07c7b2SAlfred Perlstein } 9425f07c7b2SAlfred Perlstein return SPL_RESERR; 9435f07c7b2SAlfred Perlstein } 9445f07c7b2SAlfred Perlstein 945815ac496SAlfred Perlstein fill_file_lock(*right_lock, &exist_lock->filehandle, 946815ac496SAlfred Perlstein exist_lock->addr, 9475f07c7b2SAlfred Perlstein exist_lock->client.exclusive, exist_lock->client.svid, 9485f07c7b2SAlfred Perlstein start2, len2, 9495f07c7b2SAlfred Perlstein exist_lock->client_name, exist_lock->nsm_status, 9505f07c7b2SAlfred Perlstein exist_lock->status, exist_lock->flags, exist_lock->blocking); 9515f07c7b2SAlfred Perlstein } 9525f07c7b2SAlfred Perlstein 9535f07c7b2SAlfred Perlstein return spstatus; 9544945c132SAlfred Perlstein } 9554945c132SAlfred Perlstein 9564945c132SAlfred Perlstein enum nfslock_status 957b6dc41baSAlfred Perlstein unlock_nfslock(fl, released_lock, left_lock, right_lock) 958b6dc41baSAlfred Perlstein const struct file_lock *fl; 959b6dc41baSAlfred Perlstein struct file_lock **released_lock; 9605f07c7b2SAlfred Perlstein struct file_lock **left_lock; 9615f07c7b2SAlfred Perlstein struct file_lock **right_lock; 9624945c132SAlfred Perlstein { 9634945c132SAlfred Perlstein struct file_lock *mfl; /* Matching file lock */ 9644945c132SAlfred Perlstein enum nfslock_status retval; 9655f07c7b2SAlfred Perlstein enum split_status spstatus; 9664945c132SAlfred Perlstein 9674945c132SAlfred Perlstein debuglog("Entering unlock_nfslock\n"); 9684945c132SAlfred Perlstein 9697e589b0bSAlfred Perlstein *released_lock = NULL; 9705f07c7b2SAlfred Perlstein *left_lock = NULL; 9715f07c7b2SAlfred Perlstein *right_lock = NULL; 9727e589b0bSAlfred Perlstein 9734945c132SAlfred Perlstein retval = NFS_DENIED_NOLOCK; 9744945c132SAlfred Perlstein 9754945c132SAlfred Perlstein printf("Attempting to match lock...\n"); 9764945c132SAlfred Perlstein mfl = get_lock_matching_unlock(fl); 9774945c132SAlfred Perlstein 9787e589b0bSAlfred Perlstein if (mfl != NULL) { 9795f07c7b2SAlfred Perlstein debuglog("Unlock matched. Querying for split\n"); 9805f07c7b2SAlfred Perlstein 9815f07c7b2SAlfred Perlstein spstatus = split_nfslock(mfl, fl, left_lock, right_lock); 9825f07c7b2SAlfred Perlstein 9835f07c7b2SAlfred Perlstein debuglog("Split returned %d %p %p %p %p\n",spstatus,mfl,fl,*left_lock,*right_lock); 9845f07c7b2SAlfred Perlstein debuglog("********Split dumps********"); 9855f07c7b2SAlfred Perlstein dump_filelock(mfl); 9865f07c7b2SAlfred Perlstein dump_filelock(fl); 9875f07c7b2SAlfred Perlstein dump_filelock(*left_lock); 9885f07c7b2SAlfred Perlstein dump_filelock(*right_lock); 9895f07c7b2SAlfred Perlstein debuglog("********End Split dumps********"); 9905f07c7b2SAlfred Perlstein 9915f07c7b2SAlfred Perlstein if (spstatus == SPL_RESERR) { 9925f07c7b2SAlfred Perlstein if (*left_lock != NULL) { 9935f07c7b2SAlfred Perlstein deallocate_file_lock(*left_lock); 9945f07c7b2SAlfred Perlstein *left_lock = NULL; 9955f07c7b2SAlfred Perlstein } 9965f07c7b2SAlfred Perlstein 9975f07c7b2SAlfred Perlstein if (*right_lock != NULL) { 9985f07c7b2SAlfred Perlstein deallocate_file_lock(*right_lock); 9995f07c7b2SAlfred Perlstein *right_lock = NULL; 10005f07c7b2SAlfred Perlstein } 10015f07c7b2SAlfred Perlstein 10025f07c7b2SAlfred Perlstein return NFS_RESERR; 10035f07c7b2SAlfred Perlstein } 10045f07c7b2SAlfred Perlstein 10055f07c7b2SAlfred Perlstein /* Insert new locks from split if required */ 10065f07c7b2SAlfred Perlstein if (*left_lock != NULL) { 10075f07c7b2SAlfred Perlstein debuglog("Split left activated\n"); 10085f07c7b2SAlfred Perlstein LIST_INSERT_HEAD(&nfslocklist_head, *left_lock, nfslocklist); 10095f07c7b2SAlfred Perlstein } 10105f07c7b2SAlfred Perlstein 10115f07c7b2SAlfred Perlstein if (*right_lock != NULL) { 10125f07c7b2SAlfred Perlstein debuglog("Split right activated\n"); 10135f07c7b2SAlfred Perlstein LIST_INSERT_HEAD(&nfslocklist_head, *right_lock, nfslocklist); 10145f07c7b2SAlfred Perlstein } 10155f07c7b2SAlfred Perlstein 10167e589b0bSAlfred Perlstein /* Unlock the lock since it matches identity */ 10174945c132SAlfred Perlstein LIST_REMOVE(mfl, nfslocklist); 10187e589b0bSAlfred Perlstein *released_lock = mfl; 10194945c132SAlfred Perlstein retval = NFS_GRANTED; 10204945c132SAlfred Perlstein } 10214945c132SAlfred Perlstein 10224945c132SAlfred Perlstein debuglog("Exiting unlock_nfslock\n"); 10234945c132SAlfred Perlstein 10244945c132SAlfred Perlstein return retval; 10254945c132SAlfred Perlstein } 10264945c132SAlfred Perlstein 10277e589b0bSAlfred Perlstein /* 10287e589b0bSAlfred Perlstein * Below here are the routines for manipulating the file lock directly 10297e589b0bSAlfred Perlstein * on the disk hardware itself 10307e589b0bSAlfred Perlstein */ 10317e589b0bSAlfred Perlstein enum hwlock_status 10327e589b0bSAlfred Perlstein lock_hwlock(struct file_lock *fl) 10337e589b0bSAlfred Perlstein { 10347e589b0bSAlfred Perlstein struct monfile *imf,*nmf; 10357e589b0bSAlfred Perlstein int lflags, flerror; 10367e589b0bSAlfred Perlstein 10377e589b0bSAlfred Perlstein /* Scan to see if filehandle already present */ 10387e589b0bSAlfred Perlstein LIST_FOREACH(imf, &monfilelist_head, monfilelist) { 10397e589b0bSAlfred Perlstein if (bcmp(&fl->filehandle, &imf->filehandle, 10407e589b0bSAlfred Perlstein sizeof(fl->filehandle)) == 0) { 10417e589b0bSAlfred Perlstein /* imf is the correct filehandle */ 10427e589b0bSAlfred Perlstein break; 10437e589b0bSAlfred Perlstein } 10447e589b0bSAlfred Perlstein } 10457e589b0bSAlfred Perlstein 1046b6dc41baSAlfred Perlstein /* 1047b6dc41baSAlfred Perlstein * Filehandle already exists (we control the file) 1048b6dc41baSAlfred Perlstein * *AND* NFS has already cleared the lock for availability 1049b6dc41baSAlfred Perlstein * Grant it and bump the refcount. 1050b6dc41baSAlfred Perlstein */ 1051b6dc41baSAlfred Perlstein if (imf != NULL) { 1052b6dc41baSAlfred Perlstein ++(imf->refcount); 1053b6dc41baSAlfred Perlstein return (HW_GRANTED); 1054b6dc41baSAlfred Perlstein } 1055b6dc41baSAlfred Perlstein 10567e589b0bSAlfred Perlstein /* No filehandle found, create and go */ 10577e589b0bSAlfred Perlstein nmf = malloc(sizeof(struct monfile)); 10587e589b0bSAlfred Perlstein if (nmf == NULL) { 10597e589b0bSAlfred Perlstein debuglog("hwlock resource allocation failure\n"); 1060b6dc41baSAlfred Perlstein return (HW_RESERR); 10617e589b0bSAlfred Perlstein } 10627e589b0bSAlfred Perlstein 10637e589b0bSAlfred Perlstein /* XXX: Is O_RDWR always the correct mode? */ 10647e589b0bSAlfred Perlstein nmf->fd = fhopen(&fl->filehandle, O_RDWR); 10657e589b0bSAlfred Perlstein if (nmf->fd < 0) { 10667e589b0bSAlfred Perlstein debuglog("fhopen failed (from %16s): %32s\n", 10677e589b0bSAlfred Perlstein fl->client_name, strerror(errno)); 10687e589b0bSAlfred Perlstein free(nmf); 1069b6dc41baSAlfred Perlstein switch (errno) { 1070b6dc41baSAlfred Perlstein case ESTALE: 1071b6dc41baSAlfred Perlstein return (HW_STALEFH); 1072b6dc41baSAlfred Perlstein case EROFS: 1073b6dc41baSAlfred Perlstein return (HW_READONLY); 1074b6dc41baSAlfred Perlstein default: 1075b6dc41baSAlfred Perlstein return (HW_RESERR); 1076b6dc41baSAlfred Perlstein } 1077b6dc41baSAlfred Perlstein } 1078b6dc41baSAlfred Perlstein 10797e589b0bSAlfred Perlstein /* File opened correctly, fill the monitor struct */ 10807e589b0bSAlfred Perlstein bcopy(&fl->filehandle, &nmf->filehandle, sizeof(fl->filehandle)); 10817e589b0bSAlfred Perlstein nmf->refcount = 1; 10827e589b0bSAlfred Perlstein nmf->exclusive = fl->client.exclusive; 10837e589b0bSAlfred Perlstein 10847e589b0bSAlfred Perlstein lflags = (nmf->exclusive == 1) ? 10857e589b0bSAlfred Perlstein (LOCK_EX | LOCK_NB) : (LOCK_SH | LOCK_NB); 10867e589b0bSAlfred Perlstein 10877e589b0bSAlfred Perlstein flerror = flock(nmf->fd, lflags); 10887e589b0bSAlfred Perlstein 10897e589b0bSAlfred Perlstein if (flerror != 0) { 10907e589b0bSAlfred Perlstein debuglog("flock failed (from %16s): %32s\n", 10917e589b0bSAlfred Perlstein fl->client_name, strerror(errno)); 10927e589b0bSAlfred Perlstein close(nmf->fd); 10937e589b0bSAlfred Perlstein free(nmf); 1094b6dc41baSAlfred Perlstein switch (errno) { 1095b6dc41baSAlfred Perlstein case EAGAIN: 1096b6dc41baSAlfred Perlstein return (HW_DENIED); 1097b6dc41baSAlfred Perlstein case ESTALE: 1098b6dc41baSAlfred Perlstein return (HW_STALEFH); 1099b6dc41baSAlfred Perlstein case EROFS: 1100b6dc41baSAlfred Perlstein return (HW_READONLY); 1101b6dc41baSAlfred Perlstein default: 1102b6dc41baSAlfred Perlstein return (HW_RESERR); 1103b6dc41baSAlfred Perlstein break; 1104b6dc41baSAlfred Perlstein } 1105b6dc41baSAlfred Perlstein } 1106b6dc41baSAlfred Perlstein 11077e589b0bSAlfred Perlstein /* File opened and locked */ 11087e589b0bSAlfred Perlstein LIST_INSERT_HEAD(&monfilelist_head, nmf, monfilelist); 11097e589b0bSAlfred Perlstein 1110b6dc41baSAlfred Perlstein debuglog("flock succeeded (from %16s)\n", fl->client_name); 1111b6dc41baSAlfred Perlstein return (HW_GRANTED); 11127e589b0bSAlfred Perlstein } 11137e589b0bSAlfred Perlstein 11147e589b0bSAlfred Perlstein enum hwlock_status 11157e589b0bSAlfred Perlstein unlock_hwlock(const struct file_lock *fl) 11167e589b0bSAlfred Perlstein { 11177e589b0bSAlfred Perlstein struct monfile *imf; 11187e589b0bSAlfred Perlstein 11197e589b0bSAlfred Perlstein debuglog("Entering unlock_hwlock\n"); 11207e589b0bSAlfred Perlstein debuglog("Entering loop interation\n"); 11217e589b0bSAlfred Perlstein 11227e589b0bSAlfred Perlstein /* Scan to see if filehandle already present */ 11237e589b0bSAlfred Perlstein LIST_FOREACH(imf, &monfilelist_head, monfilelist) { 11247e589b0bSAlfred Perlstein if (bcmp(&fl->filehandle, &imf->filehandle, 11257e589b0bSAlfred Perlstein sizeof(fl->filehandle)) == 0) { 11267e589b0bSAlfred Perlstein /* imf is the correct filehandle */ 11277e589b0bSAlfred Perlstein break; 11287e589b0bSAlfred Perlstein } 11297e589b0bSAlfred Perlstein } 11307e589b0bSAlfred Perlstein 11317e589b0bSAlfred Perlstein debuglog("Completed iteration. Proceeding\n"); 11327e589b0bSAlfred Perlstein 11337e589b0bSAlfred Perlstein if (imf == NULL) { 11347e589b0bSAlfred Perlstein /* No lock found */ 1135b6dc41baSAlfred Perlstein debuglog("Exiting unlock_hwlock (HW_DENIED_NOLOCK)\n"); 1136b6dc41baSAlfred Perlstein return (HW_DENIED_NOLOCK); 1137b6dc41baSAlfred Perlstein } 1138b6dc41baSAlfred Perlstein 11397e589b0bSAlfred Perlstein /* Lock found */ 11407e589b0bSAlfred Perlstein --imf->refcount; 11417e589b0bSAlfred Perlstein 11427e589b0bSAlfred Perlstein if (imf->refcount < 0) { 11437e589b0bSAlfred Perlstein debuglog("Negative hardware reference count\n"); 11447e589b0bSAlfred Perlstein } 11457e589b0bSAlfred Perlstein 11467e589b0bSAlfred Perlstein if (imf->refcount <= 0) { 11477e589b0bSAlfred Perlstein close(imf->fd); 11487e589b0bSAlfred Perlstein LIST_REMOVE(imf, monfilelist); 11497e589b0bSAlfred Perlstein free(imf); 11507e589b0bSAlfred Perlstein } 1151b6dc41baSAlfred Perlstein debuglog("Exiting unlock_hwlock (HW_GRANTED)\n"); 1152b6dc41baSAlfred Perlstein return (HW_GRANTED); 11537e589b0bSAlfred Perlstein } 11547e589b0bSAlfred Perlstein 11557e589b0bSAlfred Perlstein enum hwlock_status 1156c4bc47baSAlfred Perlstein test_hwlock(fl, conflicting_fl) 1157c4bc47baSAlfred Perlstein const struct file_lock *fl __unused; 1158c4bc47baSAlfred Perlstein struct file_lock **conflicting_fl __unused; 11597e589b0bSAlfred Perlstein { 1160b6dc41baSAlfred Perlstein 11617e589b0bSAlfred Perlstein /* 11627e589b0bSAlfred Perlstein * XXX: lock tests on hardware are not required until 11637e589b0bSAlfred Perlstein * true partial file testing is done on the underlying file 11647e589b0bSAlfred Perlstein */ 1165b6dc41baSAlfred Perlstein return (HW_RESERR); 11667e589b0bSAlfred Perlstein } 11677e589b0bSAlfred Perlstein 11687e589b0bSAlfred Perlstein 11697e589b0bSAlfred Perlstein 11707e589b0bSAlfred Perlstein /* 11717e589b0bSAlfred Perlstein * Below here are routines for manipulating blocked lock requests 11727e589b0bSAlfred Perlstein * They should only be called from the XXX_partialfilelock routines 11737e589b0bSAlfred Perlstein * if at all possible 11747e589b0bSAlfred Perlstein */ 11757e589b0bSAlfred Perlstein 11767e589b0bSAlfred Perlstein void 11777e589b0bSAlfred Perlstein add_blockingfilelock(struct file_lock *fl) 11787e589b0bSAlfred Perlstein { 1179b6dc41baSAlfred Perlstein 11807e589b0bSAlfred Perlstein debuglog("Entering add_blockingfilelock\n"); 11817e589b0bSAlfred Perlstein 11827e589b0bSAlfred Perlstein /* 11837e589b0bSAlfred Perlstein * Clear the blocking flag so that it can be reused without 11847e589b0bSAlfred Perlstein * adding it to the blocking queue a second time 11857e589b0bSAlfred Perlstein */ 11867e589b0bSAlfred Perlstein 11877e589b0bSAlfred Perlstein fl->blocking = 0; 11887e589b0bSAlfred Perlstein LIST_INSERT_HEAD(&blockedlocklist_head, fl, nfslocklist); 11897e589b0bSAlfred Perlstein 11907e589b0bSAlfred Perlstein debuglog("Exiting add_blockingfilelock\n"); 11917e589b0bSAlfred Perlstein } 11927e589b0bSAlfred Perlstein 11937e589b0bSAlfred Perlstein void 11947e589b0bSAlfred Perlstein remove_blockingfilelock(struct file_lock *fl) 11957e589b0bSAlfred Perlstein { 1196b6dc41baSAlfred Perlstein 11977e589b0bSAlfred Perlstein debuglog("Entering remove_blockingfilelock\n"); 11987e589b0bSAlfred Perlstein 11997e589b0bSAlfred Perlstein LIST_REMOVE(fl, nfslocklist); 12007e589b0bSAlfred Perlstein 12017e589b0bSAlfred Perlstein debuglog("Exiting remove_blockingfilelock\n"); 12027e589b0bSAlfred Perlstein } 12037e589b0bSAlfred Perlstein 12047e589b0bSAlfred Perlstein void 1205a4a8a04dSAlfred Perlstein clear_blockingfilelock(const char *hostname) 1206a4a8a04dSAlfred Perlstein { 1207a4a8a04dSAlfred Perlstein struct file_lock *ifl,*nfl; 1208a4a8a04dSAlfred Perlstein 1209a4a8a04dSAlfred Perlstein /* 1210a4a8a04dSAlfred Perlstein * Normally, LIST_FOREACH is called for, but since 1211a4a8a04dSAlfred Perlstein * the current element *is* the iterator, deleting it 1212a4a8a04dSAlfred Perlstein * would mess up the iteration. Thus, a next element 1213a4a8a04dSAlfred Perlstein * must be used explicitly 1214a4a8a04dSAlfred Perlstein */ 1215a4a8a04dSAlfred Perlstein 1216a4a8a04dSAlfred Perlstein ifl = LIST_FIRST(&blockedlocklist_head); 1217a4a8a04dSAlfred Perlstein 1218a4a8a04dSAlfred Perlstein while (ifl != NULL) { 1219a4a8a04dSAlfred Perlstein nfl = LIST_NEXT(ifl, nfslocklist); 1220a4a8a04dSAlfred Perlstein 1221a4a8a04dSAlfred Perlstein if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) { 1222a4a8a04dSAlfred Perlstein remove_blockingfilelock(ifl); 1223a4a8a04dSAlfred Perlstein deallocate_file_lock(ifl); 1224a4a8a04dSAlfred Perlstein } 1225a4a8a04dSAlfred Perlstein 1226a4a8a04dSAlfred Perlstein ifl = nfl; 1227a4a8a04dSAlfred Perlstein } 1228a4a8a04dSAlfred Perlstein } 1229a4a8a04dSAlfred Perlstein 1230a4a8a04dSAlfred Perlstein void 12317e589b0bSAlfred Perlstein retry_blockingfilelocklist(void) 12327e589b0bSAlfred Perlstein { 12337e589b0bSAlfred Perlstein /* Retry all locks in the blocked list */ 12344b4ec9b8SAlfred Perlstein struct file_lock *ifl, *nfl, *pfl; /* Iterator */ 12357e589b0bSAlfred Perlstein enum partialfilelock_status pflstatus; 12367e589b0bSAlfred Perlstein 12377e589b0bSAlfred Perlstein debuglog("Entering retry_blockingfilelocklist\n"); 12387e589b0bSAlfred Perlstein 12394b4ec9b8SAlfred Perlstein pfl = NULL; 12407e589b0bSAlfred Perlstein ifl = LIST_FIRST(&blockedlocklist_head); 12417e589b0bSAlfred Perlstein debuglog("Iterator choice %p\n",ifl); 12427e589b0bSAlfred Perlstein 12437e589b0bSAlfred Perlstein while (ifl != NULL) { 12447e589b0bSAlfred Perlstein /* 12457e589b0bSAlfred Perlstein * SUBTLE BUG: The next element must be worked out before the 12467e589b0bSAlfred Perlstein * current element has been moved 12477e589b0bSAlfred Perlstein */ 12487e589b0bSAlfred Perlstein nfl = LIST_NEXT(ifl, nfslocklist); 12497e589b0bSAlfred Perlstein debuglog("Iterator choice %p\n",ifl); 12504b4ec9b8SAlfred Perlstein debuglog("Prev iterator choice %p\n",pfl); 12517e589b0bSAlfred Perlstein debuglog("Next iterator choice %p\n",nfl); 12527e589b0bSAlfred Perlstein 12537e589b0bSAlfred Perlstein /* 12547e589b0bSAlfred Perlstein * SUBTLE BUG: The file_lock must be removed from the 12557e589b0bSAlfred Perlstein * old list so that it's list pointers get disconnected 12567e589b0bSAlfred Perlstein * before being allowed to participate in the new list 12577e589b0bSAlfred Perlstein * which will automatically add it in if necessary. 12587e589b0bSAlfred Perlstein */ 12597e589b0bSAlfred Perlstein 12607e589b0bSAlfred Perlstein LIST_REMOVE(ifl, nfslocklist); 12617e589b0bSAlfred Perlstein pflstatus = lock_partialfilelock(ifl); 12627e589b0bSAlfred Perlstein 12637e589b0bSAlfred Perlstein if (pflstatus == PFL_GRANTED || pflstatus == PFL_GRANTED_DUPLICATE) { 12647e589b0bSAlfred Perlstein debuglog("Granted blocked lock\n"); 12657e589b0bSAlfred Perlstein /* lock granted and is now being used */ 12667e589b0bSAlfred Perlstein send_granted(ifl,0); 12677e589b0bSAlfred Perlstein } else { 12687e589b0bSAlfred Perlstein /* Reinsert lock back into same place in blocked list */ 12697e589b0bSAlfred Perlstein debuglog("Replacing blocked lock\n"); 12704b4ec9b8SAlfred Perlstein if (pfl != NULL) 12714b4ec9b8SAlfred Perlstein LIST_INSERT_AFTER(pfl, ifl, nfslocklist); 12724b4ec9b8SAlfred Perlstein else 12734b4ec9b8SAlfred Perlstein /* ifl is the only elem. in the list */ 12744b4ec9b8SAlfred Perlstein LIST_INSERT_HEAD(&blockedlocklist_head, ifl, nfslocklist); 12757e589b0bSAlfred Perlstein } 12767e589b0bSAlfred Perlstein 12777e589b0bSAlfred Perlstein /* Valid increment behavior regardless of state of ifl */ 12787e589b0bSAlfred Perlstein ifl = nfl; 12794b4ec9b8SAlfred Perlstein /* if a lock was granted incrementing pfl would make it nfl */ 12804b4ec9b8SAlfred Perlstein if (pfl != NULL && (LIST_NEXT(pfl, nfslocklist) != nfl)) 12814b4ec9b8SAlfred Perlstein pfl = LIST_NEXT(pfl, nfslocklist); 12824b4ec9b8SAlfred Perlstein else 12834b4ec9b8SAlfred Perlstein pfl = LIST_FIRST(&blockedlocklist_head); 12847e589b0bSAlfred Perlstein } 12857e589b0bSAlfred Perlstein 12867e589b0bSAlfred Perlstein debuglog("Exiting retry_blockingfilelocklist\n"); 12877e589b0bSAlfred Perlstein } 12884945c132SAlfred Perlstein 12894945c132SAlfred Perlstein /* 12904945c132SAlfred Perlstein * Below here are routines associated with manipulating all 12914945c132SAlfred Perlstein * aspects of the partial file locking system (list, hardware, etc.) 12924945c132SAlfred Perlstein */ 12934945c132SAlfred Perlstein 12944945c132SAlfred Perlstein /* 1295a4a8a04dSAlfred Perlstein * Please note that lock monitoring must be done at this level which 1296a4a8a04dSAlfred Perlstein * keeps track of *individual* lock requests on lock and unlock 1297a4a8a04dSAlfred Perlstein * 1298a4a8a04dSAlfred Perlstein * XXX: Split unlocking is going to make the unlock code miserable 1299a4a8a04dSAlfred Perlstein */ 1300a4a8a04dSAlfred Perlstein 1301a4a8a04dSAlfred Perlstein /* 13024945c132SAlfred Perlstein * lock_partialfilelock: 13034945c132SAlfred Perlstein * 13044945c132SAlfred Perlstein * Argument fl gets modified as its list housekeeping entries get modified 13054945c132SAlfred Perlstein * upon insertion into the NFS lock list 13064945c132SAlfred Perlstein * 13074945c132SAlfred Perlstein * This routine makes several assumptions: 1308a4a8a04dSAlfred Perlstein * 1) It (will) pass locks through to flock to lock the entire underlying file 1309a4a8a04dSAlfred Perlstein * and then parcel out NFS locks if it gets control of the file. 1310a4a8a04dSAlfred Perlstein * This matches the old rpc.lockd file semantics (except where it 1311a4a8a04dSAlfred Perlstein * is now more correct). It is the safe solution, but will cause 1312a4a8a04dSAlfred Perlstein * overly restrictive blocking if someone is trying to use the 1313a4a8a04dSAlfred Perlstein * underlying files without using NFS. This appears to be an 1314a4a8a04dSAlfred Perlstein * acceptable tradeoff since most people use standalone NFS servers. 1315a4a8a04dSAlfred Perlstein * XXX: The right solution is probably kevent combined with fcntl 13164945c132SAlfred Perlstein * 1317a4a8a04dSAlfred Perlstein * 2) Nothing modifies the lock lists between testing and granting 1318a4a8a04dSAlfred Perlstein * I have no idea whether this is a useful assumption or not 13194945c132SAlfred Perlstein */ 13204945c132SAlfred Perlstein 13214945c132SAlfred Perlstein enum partialfilelock_status 13224945c132SAlfred Perlstein lock_partialfilelock(struct file_lock *fl) 13234945c132SAlfred Perlstein { 13244945c132SAlfred Perlstein enum partialfilelock_status retval; 13254945c132SAlfred Perlstein enum nfslock_status lnlstatus; 13267e589b0bSAlfred Perlstein enum hwlock_status hwstatus; 13274945c132SAlfred Perlstein 13284945c132SAlfred Perlstein debuglog("Entering lock_partialfilelock\n"); 13294945c132SAlfred Perlstein 13304945c132SAlfred Perlstein retval = PFL_DENIED; 13314945c132SAlfred Perlstein 13324945c132SAlfred Perlstein /* 13334945c132SAlfred Perlstein * Execute the NFS lock first, if possible, as it is significantly 13344945c132SAlfred Perlstein * easier and less expensive to undo than the filesystem lock 13354945c132SAlfred Perlstein */ 13364945c132SAlfred Perlstein 13374945c132SAlfred Perlstein lnlstatus = lock_nfslock(fl); 13384945c132SAlfred Perlstein 13397e589b0bSAlfred Perlstein switch (lnlstatus) { 13407e589b0bSAlfred Perlstein case NFS_GRANTED: 13417e589b0bSAlfred Perlstein case NFS_GRANTED_DUPLICATE: 13427e589b0bSAlfred Perlstein /* 13437e589b0bSAlfred Perlstein * At this point, the NFS lock is allocated and active. 13447e589b0bSAlfred Perlstein * Remember to clean it up if the hardware lock fails 13457e589b0bSAlfred Perlstein */ 13467e589b0bSAlfred Perlstein hwstatus = lock_hwlock(fl); 13477e589b0bSAlfred Perlstein 13487e589b0bSAlfred Perlstein switch (hwstatus) { 13497e589b0bSAlfred Perlstein case HW_GRANTED: 13507e589b0bSAlfred Perlstein case HW_GRANTED_DUPLICATE: 13517e589b0bSAlfred Perlstein debuglog("HW GRANTED\n"); 13527e589b0bSAlfred Perlstein /* 13537e589b0bSAlfred Perlstein * XXX: Fixme: Check hwstatus for duplicate when 13547e589b0bSAlfred Perlstein * true partial file locking and accounting is 13557e589b0bSAlfred Perlstein * done on the hardware 13567e589b0bSAlfred Perlstein */ 13577e589b0bSAlfred Perlstein if (lnlstatus == NFS_GRANTED_DUPLICATE) { 13587e589b0bSAlfred Perlstein retval = PFL_GRANTED_DUPLICATE; 13594945c132SAlfred Perlstein } else { 13607e589b0bSAlfred Perlstein retval = PFL_GRANTED; 13614945c132SAlfred Perlstein } 1362a4a8a04dSAlfred Perlstein monitor_lock_host(fl->client_name); 13637e589b0bSAlfred Perlstein break; 13647e589b0bSAlfred Perlstein case HW_RESERR: 13657e589b0bSAlfred Perlstein debuglog("HW RESERR\n"); 13667e589b0bSAlfred Perlstein retval = PFL_HWRESERR; 13677e589b0bSAlfred Perlstein break; 13687e589b0bSAlfred Perlstein case HW_DENIED: 13697e589b0bSAlfred Perlstein debuglog("HW DENIED\n"); 13707e589b0bSAlfred Perlstein retval = PFL_HWDENIED; 13717e589b0bSAlfred Perlstein break; 13727e589b0bSAlfred Perlstein default: 13737e589b0bSAlfred Perlstein debuglog("Unmatched hwstatus %d\n",hwstatus); 13747e589b0bSAlfred Perlstein break; 13757e589b0bSAlfred Perlstein } 13767e589b0bSAlfred Perlstein 13777e589b0bSAlfred Perlstein if (retval != PFL_GRANTED && 13787e589b0bSAlfred Perlstein retval != PFL_GRANTED_DUPLICATE) { 13797e589b0bSAlfred Perlstein /* Clean up the NFS lock */ 13807e589b0bSAlfred Perlstein debuglog("Deleting trial NFS lock\n"); 13817e589b0bSAlfred Perlstein delete_nfslock(fl); 13827e589b0bSAlfred Perlstein } 13837e589b0bSAlfred Perlstein break; 13847e589b0bSAlfred Perlstein case NFS_DENIED: 13854945c132SAlfred Perlstein retval = PFL_NFSDENIED; 13867e589b0bSAlfred Perlstein break; 13877e589b0bSAlfred Perlstein case NFS_RESERR: 13887e589b0bSAlfred Perlstein retval = PFL_NFSRESERR; 13897e589b0bSAlfred Perlstein default: 13907e589b0bSAlfred Perlstein debuglog("Unmatched lnlstatus %d\n"); 13917e589b0bSAlfred Perlstein retval = PFL_NFSDENIED_NOLOCK; 13927e589b0bSAlfred Perlstein break; 13937e589b0bSAlfred Perlstein } 13947e589b0bSAlfred Perlstein 13957e589b0bSAlfred Perlstein /* 13967e589b0bSAlfred Perlstein * By the time fl reaches here, it is completely free again on 13977e589b0bSAlfred Perlstein * failure. The NFS lock done before attempting the 13987e589b0bSAlfred Perlstein * hardware lock has been backed out 13997e589b0bSAlfred Perlstein */ 14007e589b0bSAlfred Perlstein 14017e589b0bSAlfred Perlstein if (retval == PFL_NFSDENIED || retval == PFL_HWDENIED) { 14027e589b0bSAlfred Perlstein /* Once last chance to check the lock */ 14037e589b0bSAlfred Perlstein if (fl->blocking == 1) { 14047e589b0bSAlfred Perlstein /* Queue the lock */ 14057e589b0bSAlfred Perlstein debuglog("BLOCKING LOCK RECEIVED\n"); 14067e589b0bSAlfred Perlstein retval = (retval == PFL_NFSDENIED ? 14077e589b0bSAlfred Perlstein PFL_NFSBLOCKED : PFL_HWBLOCKED); 14087e589b0bSAlfred Perlstein add_blockingfilelock(fl); 14094945c132SAlfred Perlstein dump_filelock(fl); 14107e589b0bSAlfred Perlstein } else { 14117e589b0bSAlfred Perlstein /* Leave retval alone, it's already correct */ 14127e589b0bSAlfred Perlstein debuglog("Lock denied. Non-blocking failure\n"); 14137e589b0bSAlfred Perlstein dump_filelock(fl); 14147e589b0bSAlfred Perlstein } 14154945c132SAlfred Perlstein } 14164945c132SAlfred Perlstein 14174945c132SAlfred Perlstein debuglog("Exiting lock_partialfilelock\n"); 14184945c132SAlfred Perlstein 14194945c132SAlfred Perlstein return retval; 14204945c132SAlfred Perlstein } 14214945c132SAlfred Perlstein 14224945c132SAlfred Perlstein /* 14234945c132SAlfred Perlstein * unlock_partialfilelock: 1424a4a8a04dSAlfred Perlstein * 1425a4a8a04dSAlfred Perlstein * Given a file_lock, unlock all locks which match. 1426a4a8a04dSAlfred Perlstein * 1427a4a8a04dSAlfred Perlstein * Note that a given lock might have to unlock ITSELF! See 1428a4a8a04dSAlfred Perlstein * clear_partialfilelock for example. 14294945c132SAlfred Perlstein */ 14304945c132SAlfred Perlstein 14314945c132SAlfred Perlstein enum partialfilelock_status 14324945c132SAlfred Perlstein unlock_partialfilelock(const struct file_lock *fl) 14334945c132SAlfred Perlstein { 1434a4a8a04dSAlfred Perlstein struct file_lock *lfl,*rfl,*releasedfl,*selffl; 14354945c132SAlfred Perlstein enum partialfilelock_status retval; 14367e589b0bSAlfred Perlstein enum nfslock_status unlstatus; 14375f07c7b2SAlfred Perlstein enum hwlock_status unlhwstatus, lhwstatus; 14384945c132SAlfred Perlstein 14394945c132SAlfred Perlstein debuglog("Entering unlock_partialfilelock\n"); 14404945c132SAlfred Perlstein 1441a4a8a04dSAlfred Perlstein selffl = NULL; 14425f07c7b2SAlfred Perlstein lfl = NULL; 14435f07c7b2SAlfred Perlstein rfl = NULL; 14447e589b0bSAlfred Perlstein releasedfl = NULL; 14454945c132SAlfred Perlstein retval = PFL_DENIED; 14464945c132SAlfred Perlstein 14477e589b0bSAlfred Perlstein /* 14485f07c7b2SAlfred Perlstein * There are significant overlap and atomicity issues 14495f07c7b2SAlfred Perlstein * with partially releasing a lock. For example, releasing 14505f07c7b2SAlfred Perlstein * part of an NFS shared lock does *not* always release the 14515f07c7b2SAlfred Perlstein * corresponding part of the file since there is only one 14525f07c7b2SAlfred Perlstein * rpc.lockd UID but multiple users could be requesting it 14535f07c7b2SAlfred Perlstein * from NFS. Also, an unlock request should never allow 14545f07c7b2SAlfred Perlstein * another process to gain a lock on the remaining parts. 14555f07c7b2SAlfred Perlstein * ie. Always apply the new locks before releasing the 14565f07c7b2SAlfred Perlstein * old one 14575f07c7b2SAlfred Perlstein */ 14585f07c7b2SAlfred Perlstein 14595f07c7b2SAlfred Perlstein /* 14607e589b0bSAlfred Perlstein * Loop is required since multiple little locks 14617e589b0bSAlfred Perlstein * can be allocated and then deallocated with one 14627e589b0bSAlfred Perlstein * big unlock. 14637e589b0bSAlfred Perlstein * 14647e589b0bSAlfred Perlstein * The loop is required to be here so that the nfs & 14657e589b0bSAlfred Perlstein * hw subsystems do not need to communicate with one 14667e589b0bSAlfred Perlstein * one another 14677e589b0bSAlfred Perlstein */ 14684945c132SAlfred Perlstein 14697e589b0bSAlfred Perlstein do { 14707e589b0bSAlfred Perlstein debuglog("Value of releasedfl: %p\n",releasedfl); 14715f07c7b2SAlfred Perlstein /* lfl&rfl are created *AND* placed into the NFS lock list if required */ 14725f07c7b2SAlfred Perlstein unlstatus = unlock_nfslock(fl, &releasedfl, &lfl, &rfl); 14737e589b0bSAlfred Perlstein debuglog("Value of releasedfl: %p\n",releasedfl); 14747e589b0bSAlfred Perlstein 14755f07c7b2SAlfred Perlstein 14765f07c7b2SAlfred Perlstein /* XXX: This is grungy. It should be refactored to be cleaner */ 14775f07c7b2SAlfred Perlstein if (lfl != NULL) { 14785f07c7b2SAlfred Perlstein lhwstatus = lock_hwlock(lfl); 14795f07c7b2SAlfred Perlstein if (lhwstatus != HW_GRANTED && 14805f07c7b2SAlfred Perlstein lhwstatus != HW_GRANTED_DUPLICATE) { 14815f07c7b2SAlfred Perlstein debuglog("HW duplicate lock failure for left split\n"); 14825f07c7b2SAlfred Perlstein } 14835f07c7b2SAlfred Perlstein monitor_lock_host(lfl->client_name); 14845f07c7b2SAlfred Perlstein } 14855f07c7b2SAlfred Perlstein 14865f07c7b2SAlfred Perlstein if (rfl != NULL) { 14875f07c7b2SAlfred Perlstein lhwstatus = lock_hwlock(rfl); 14885f07c7b2SAlfred Perlstein if (lhwstatus != HW_GRANTED && 14895f07c7b2SAlfred Perlstein lhwstatus != HW_GRANTED_DUPLICATE) { 14905f07c7b2SAlfred Perlstein debuglog("HW duplicate lock failure for right split\n"); 14915f07c7b2SAlfred Perlstein } 14925f07c7b2SAlfred Perlstein monitor_lock_host(rfl->client_name); 14935f07c7b2SAlfred Perlstein } 14945f07c7b2SAlfred Perlstein 14957e589b0bSAlfred Perlstein switch (unlstatus) { 14967e589b0bSAlfred Perlstein case NFS_GRANTED: 14977e589b0bSAlfred Perlstein /* Attempt to unlock on the hardware */ 14987e589b0bSAlfred Perlstein debuglog("NFS unlock granted. Attempting hardware unlock\n"); 14997e589b0bSAlfred Perlstein 15005f07c7b2SAlfred Perlstein /* This call *MUST NOT* unlock the two newly allocated locks */ 15017e589b0bSAlfred Perlstein unlhwstatus = unlock_hwlock(fl); 15027e589b0bSAlfred Perlstein debuglog("HW unlock returned with code %d\n",unlhwstatus); 15037e589b0bSAlfred Perlstein 15047e589b0bSAlfred Perlstein switch (unlhwstatus) { 15057e589b0bSAlfred Perlstein case HW_GRANTED: 15067e589b0bSAlfred Perlstein debuglog("HW unlock granted\n"); 1507a4a8a04dSAlfred Perlstein unmonitor_lock_host(releasedfl->client_name); 15084945c132SAlfred Perlstein retval = PFL_GRANTED; 15097e589b0bSAlfred Perlstein break; 15107e589b0bSAlfred Perlstein case HW_DENIED_NOLOCK: 15117e589b0bSAlfred Perlstein /* Huh?!?! This shouldn't happen */ 15127e589b0bSAlfred Perlstein debuglog("HW unlock denied no lock\n"); 15137e589b0bSAlfred Perlstein retval = PFL_HWRESERR; 15147e589b0bSAlfred Perlstein /* Break out of do-while */ 15157e589b0bSAlfred Perlstein unlstatus = NFS_RESERR; 15167e589b0bSAlfred Perlstein break; 15177e589b0bSAlfred Perlstein default: 15187e589b0bSAlfred Perlstein debuglog("HW unlock failed\n"); 15197e589b0bSAlfred Perlstein retval = PFL_HWRESERR; 15207e589b0bSAlfred Perlstein /* Break out of do-while */ 15217e589b0bSAlfred Perlstein unlstatus = NFS_RESERR; 15227e589b0bSAlfred Perlstein break; 15234945c132SAlfred Perlstein } 15244945c132SAlfred Perlstein 15257e589b0bSAlfred Perlstein debuglog("Exiting with status retval: %d\n",retval); 15267e589b0bSAlfred Perlstein 15277e589b0bSAlfred Perlstein retry_blockingfilelocklist(); 15287e589b0bSAlfred Perlstein break; 15297e589b0bSAlfred Perlstein case NFS_DENIED_NOLOCK: 15307e589b0bSAlfred Perlstein retval = PFL_GRANTED; 15317e589b0bSAlfred Perlstein debuglog("All locks cleaned out\n"); 15327e589b0bSAlfred Perlstein break; 15337e589b0bSAlfred Perlstein default: 15347e589b0bSAlfred Perlstein retval = PFL_NFSRESERR; 15357e589b0bSAlfred Perlstein debuglog("NFS unlock failure\n"); 15367e589b0bSAlfred Perlstein dump_filelock(fl); 15377e589b0bSAlfred Perlstein break; 15387e589b0bSAlfred Perlstein } 1539a4a8a04dSAlfred Perlstein 1540a4a8a04dSAlfred Perlstein if (releasedfl != NULL) { 1541a4a8a04dSAlfred Perlstein if (fl == releasedfl) { 1542a4a8a04dSAlfred Perlstein /* 1543a4a8a04dSAlfred Perlstein * XXX: YECHHH!!! Attempt to unlock self succeeded 1544a4a8a04dSAlfred Perlstein * but we can't deallocate the space yet. This is what 1545a4a8a04dSAlfred Perlstein * happens when you don't write malloc and free together 1546a4a8a04dSAlfred Perlstein */ 1547a4a8a04dSAlfred Perlstein debuglog("Attempt to unlock self\n"); 1548a4a8a04dSAlfred Perlstein selffl = releasedfl; 1549a4a8a04dSAlfred Perlstein } else { 15505f07c7b2SAlfred Perlstein /* 15515f07c7b2SAlfred Perlstein * XXX: this deallocation *still* needs to migrate closer 15525f07c7b2SAlfred Perlstein * to the allocation code way up in get_lock or the allocation 15535f07c7b2SAlfred Perlstein * code needs to migrate down (violation of "When you write 15545f07c7b2SAlfred Perlstein * malloc you must write free") 15555f07c7b2SAlfred Perlstein */ 15565f07c7b2SAlfred Perlstein 1557a4a8a04dSAlfred Perlstein deallocate_file_lock(releasedfl); 1558a4a8a04dSAlfred Perlstein } 1559a4a8a04dSAlfred Perlstein } 1560a4a8a04dSAlfred Perlstein 15617e589b0bSAlfred Perlstein } while (unlstatus == NFS_GRANTED); 15627e589b0bSAlfred Perlstein 1563a4a8a04dSAlfred Perlstein if (selffl != NULL) { 1564a4a8a04dSAlfred Perlstein /* 1565a4a8a04dSAlfred Perlstein * This statement wipes out the incoming file lock (fl) 1566a4a8a04dSAlfred Perlstein * in spite of the fact that it is declared const 1567a4a8a04dSAlfred Perlstein */ 1568a4a8a04dSAlfred Perlstein debuglog("WARNING! Destroying incoming lock pointer\n"); 1569a4a8a04dSAlfred Perlstein deallocate_file_lock(selffl); 1570a4a8a04dSAlfred Perlstein } 1571a4a8a04dSAlfred Perlstein 15724945c132SAlfred Perlstein debuglog("Exiting unlock_partialfilelock\n"); 15734945c132SAlfred Perlstein 15744945c132SAlfred Perlstein return retval; 15754945c132SAlfred Perlstein } 15764945c132SAlfred Perlstein 15774945c132SAlfred Perlstein /* 1578a4a8a04dSAlfred Perlstein * clear_partialfilelock 1579a4a8a04dSAlfred Perlstein * 1580a4a8a04dSAlfred Perlstein * Normally called in response to statd state number change. 1581a4a8a04dSAlfred Perlstein * Wipe out all locks held by a host. As a bonus, the act of 1582a4a8a04dSAlfred Perlstein * doing so should automatically clear their statd entries and 1583a4a8a04dSAlfred Perlstein * unmonitor the host. 1584a4a8a04dSAlfred Perlstein */ 1585a4a8a04dSAlfred Perlstein 1586a4a8a04dSAlfred Perlstein void 1587a4a8a04dSAlfred Perlstein clear_partialfilelock(const char *hostname) 1588a4a8a04dSAlfred Perlstein { 1589a4a8a04dSAlfred Perlstein struct file_lock *ifl, *nfl; 1590a4a8a04dSAlfred Perlstein 1591a4a8a04dSAlfred Perlstein /* Clear blocking file lock list */ 1592a4a8a04dSAlfred Perlstein clear_blockingfilelock(hostname); 1593a4a8a04dSAlfred Perlstein 1594a4a8a04dSAlfred Perlstein /* do all required unlocks */ 1595a4a8a04dSAlfred Perlstein /* Note that unlock can smash the current pointer to a lock */ 1596a4a8a04dSAlfred Perlstein 1597a4a8a04dSAlfred Perlstein /* 1598a4a8a04dSAlfred Perlstein * Normally, LIST_FOREACH is called for, but since 1599a4a8a04dSAlfred Perlstein * the current element *is* the iterator, deleting it 1600a4a8a04dSAlfred Perlstein * would mess up the iteration. Thus, a next element 1601a4a8a04dSAlfred Perlstein * must be used explicitly 1602a4a8a04dSAlfred Perlstein */ 1603a4a8a04dSAlfred Perlstein 1604a4a8a04dSAlfred Perlstein ifl = LIST_FIRST(&nfslocklist_head); 1605a4a8a04dSAlfred Perlstein 1606a4a8a04dSAlfred Perlstein while (ifl != NULL) { 1607a4a8a04dSAlfred Perlstein nfl = LIST_NEXT(ifl, nfslocklist); 1608a4a8a04dSAlfred Perlstein 1609a4a8a04dSAlfred Perlstein if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) { 1610a4a8a04dSAlfred Perlstein /* Unlock destroys ifl out from underneath */ 1611a4a8a04dSAlfred Perlstein unlock_partialfilelock(ifl); 1612a4a8a04dSAlfred Perlstein /* ifl is NO LONGER VALID AT THIS POINT */ 1613a4a8a04dSAlfred Perlstein } 1614a4a8a04dSAlfred Perlstein ifl = nfl; 1615a4a8a04dSAlfred Perlstein } 1616a4a8a04dSAlfred Perlstein } 1617a4a8a04dSAlfred Perlstein 1618a4a8a04dSAlfred Perlstein /* 16194945c132SAlfred Perlstein * test_partialfilelock: 16204945c132SAlfred Perlstein */ 16214945c132SAlfred Perlstein enum partialfilelock_status 16224945c132SAlfred Perlstein test_partialfilelock(const struct file_lock *fl, 16234945c132SAlfred Perlstein struct file_lock **conflicting_fl) 16244945c132SAlfred Perlstein { 16254945c132SAlfred Perlstein enum partialfilelock_status retval; 16264945c132SAlfred Perlstein enum nfslock_status teststatus; 16274945c132SAlfred Perlstein 16284945c132SAlfred Perlstein debuglog("Entering testpartialfilelock...\n"); 16294945c132SAlfred Perlstein 16304945c132SAlfred Perlstein retval = PFL_DENIED; 16314945c132SAlfred Perlstein 16324945c132SAlfred Perlstein teststatus = test_nfslock(fl, conflicting_fl); 16334945c132SAlfred Perlstein debuglog("test_partialfilelock: teststatus %d\n",teststatus); 16344945c132SAlfred Perlstein 16354945c132SAlfred Perlstein if (teststatus == NFS_GRANTED || teststatus == NFS_GRANTED_DUPLICATE) { 16364945c132SAlfred Perlstein /* XXX: Add the underlying filesystem locking code */ 16374945c132SAlfred Perlstein retval = (teststatus == NFS_GRANTED) ? 16384945c132SAlfred Perlstein PFL_GRANTED : PFL_GRANTED_DUPLICATE; 16394945c132SAlfred Perlstein debuglog("Dumping locks...\n"); 16404945c132SAlfred Perlstein dump_filelock(fl); 16414945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 16424945c132SAlfred Perlstein debuglog("Done dumping locks...\n"); 16434945c132SAlfred Perlstein } else { 16444945c132SAlfred Perlstein retval = PFL_NFSDENIED; 16454945c132SAlfred Perlstein debuglog("NFS test denied.\n"); 16464945c132SAlfred Perlstein dump_filelock(fl); 16474945c132SAlfred Perlstein debuglog("Conflicting.\n"); 16484945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 16494945c132SAlfred Perlstein } 16504945c132SAlfred Perlstein 16514945c132SAlfred Perlstein debuglog("Exiting testpartialfilelock...\n"); 16524945c132SAlfred Perlstein 16534945c132SAlfred Perlstein return retval; 16544945c132SAlfred Perlstein } 16554945c132SAlfred Perlstein 16564945c132SAlfred Perlstein /* 16574945c132SAlfred Perlstein * Below here are routines associated with translating the partial file locking 16584945c132SAlfred Perlstein * codes into useful codes to send back to the NFS RPC messaging system 16594945c132SAlfred Perlstein */ 16604945c132SAlfred Perlstein 16617e589b0bSAlfred Perlstein /* 16627e589b0bSAlfred Perlstein * These routines translate the (relatively) useful return codes back onto 16637e589b0bSAlfred Perlstein * the few return codes which the nlm subsystems wishes to trasmit 16647e589b0bSAlfred Perlstein */ 16657e589b0bSAlfred Perlstein 16664945c132SAlfred Perlstein enum nlm_stats 16674945c132SAlfred Perlstein do_test(struct file_lock *fl, struct file_lock **conflicting_fl) 16684945c132SAlfred Perlstein { 16694945c132SAlfred Perlstein enum partialfilelock_status pfsret; 16704945c132SAlfred Perlstein enum nlm_stats retval; 16714945c132SAlfred Perlstein 16724945c132SAlfred Perlstein debuglog("Entering do_test...\n"); 16734945c132SAlfred Perlstein 16744945c132SAlfred Perlstein pfsret = test_partialfilelock(fl,conflicting_fl); 16754945c132SAlfred Perlstein 16767e589b0bSAlfred Perlstein switch (pfsret) { 16777e589b0bSAlfred Perlstein case PFL_GRANTED: 16787e589b0bSAlfred Perlstein debuglog("PFL test lock granted\n"); 16794945c132SAlfred Perlstein dump_filelock(fl); 16804945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 16814945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 16827e589b0bSAlfred Perlstein break; 16837e589b0bSAlfred Perlstein case PFL_GRANTED_DUPLICATE: 16847e589b0bSAlfred Perlstein debuglog("PFL test lock granted--duplicate id detected\n"); 16854945c132SAlfred Perlstein dump_filelock(fl); 16864945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 16874945c132SAlfred Perlstein debuglog("Clearing conflicting_fl for call semantics\n"); 16884945c132SAlfred Perlstein *conflicting_fl = NULL; 16894945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 16907e589b0bSAlfred Perlstein break; 16917e589b0bSAlfred Perlstein case PFL_NFSDENIED: 16927e589b0bSAlfred Perlstein case PFL_HWDENIED: 16937e589b0bSAlfred Perlstein debuglog("PFL test lock denied\n"); 16944945c132SAlfred Perlstein dump_filelock(fl); 16954945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 16964945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; 16977e589b0bSAlfred Perlstein break; 16987e589b0bSAlfred Perlstein case PFL_NFSRESERR: 16997e589b0bSAlfred Perlstein case PFL_HWRESERR: 17007e589b0bSAlfred Perlstein debuglog("PFL test lock resource fail\n"); 17017e589b0bSAlfred Perlstein dump_filelock(fl); 17027e589b0bSAlfred Perlstein dump_filelock(*conflicting_fl); 17037e589b0bSAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; 17047e589b0bSAlfred Perlstein break; 17057e589b0bSAlfred Perlstein default: 17067e589b0bSAlfred Perlstein debuglog("PFL test lock *FAILED*\n"); 17074945c132SAlfred Perlstein dump_filelock(fl); 17084945c132SAlfred Perlstein dump_filelock(*conflicting_fl); 17094945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; 17107e589b0bSAlfred Perlstein break; 17114945c132SAlfred Perlstein } 17124945c132SAlfred Perlstein 17134945c132SAlfred Perlstein debuglog("Exiting do_test...\n"); 17144945c132SAlfred Perlstein 17154945c132SAlfred Perlstein return retval; 17164945c132SAlfred Perlstein } 17174945c132SAlfred Perlstein 17184945c132SAlfred Perlstein /* 17194945c132SAlfred Perlstein * do_lock: Try to acquire a lock 17204945c132SAlfred Perlstein * 17214945c132SAlfred Perlstein * This routine makes a distinction between NLM versions. I am pretty 17224945c132SAlfred Perlstein * convinced that this should be abstracted out and bounced up a level 17234945c132SAlfred Perlstein */ 17244945c132SAlfred Perlstein 17254945c132SAlfred Perlstein enum nlm_stats 17264945c132SAlfred Perlstein do_lock(struct file_lock *fl) 17274945c132SAlfred Perlstein { 17284945c132SAlfred Perlstein enum partialfilelock_status pfsret; 17294945c132SAlfred Perlstein enum nlm_stats retval; 17304945c132SAlfred Perlstein 17314945c132SAlfred Perlstein debuglog("Entering do_lock...\n"); 17324945c132SAlfred Perlstein 17334945c132SAlfred Perlstein pfsret = lock_partialfilelock(fl); 17344945c132SAlfred Perlstein 17357e589b0bSAlfred Perlstein switch (pfsret) { 17367e589b0bSAlfred Perlstein case PFL_GRANTED: 17374945c132SAlfred Perlstein debuglog("PFL lock granted"); 17384945c132SAlfred Perlstein dump_filelock(fl); 17394945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 17407e589b0bSAlfred Perlstein break; 17417e589b0bSAlfred Perlstein case PFL_GRANTED_DUPLICATE: 17424945c132SAlfred Perlstein debuglog("PFL lock granted--duplicate id detected"); 17434945c132SAlfred Perlstein dump_filelock(fl); 17444945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 17457e589b0bSAlfred Perlstein break; 17467e589b0bSAlfred Perlstein case PFL_NFSDENIED: 17477e589b0bSAlfred Perlstein case PFL_HWDENIED: 17484945c132SAlfred Perlstein debuglog("PFL_NFS lock denied"); 17494945c132SAlfred Perlstein dump_filelock(fl); 17504945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; 17517e589b0bSAlfred Perlstein break; 17527e589b0bSAlfred Perlstein case PFL_NFSBLOCKED: 17537e589b0bSAlfred Perlstein case PFL_HWBLOCKED: 17547e589b0bSAlfred Perlstein debuglog("PFL_NFS blocking lock denied. Queued.\n"); 17557e589b0bSAlfred Perlstein dump_filelock(fl); 17567e589b0bSAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_blocked : nlm_blocked; 17577e589b0bSAlfred Perlstein break; 17587e589b0bSAlfred Perlstein case PFL_NFSRESERR: 17597e589b0bSAlfred Perlstein case PFL_HWRESERR: 17607e589b0bSAlfred Perlstein debuglog("PFL lock resource alocation fail\n"); 17617e589b0bSAlfred Perlstein dump_filelock(fl); 17627e589b0bSAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; 17637e589b0bSAlfred Perlstein break; 17647e589b0bSAlfred Perlstein default: 17654945c132SAlfred Perlstein debuglog("PFL lock *FAILED*"); 17664945c132SAlfred Perlstein dump_filelock(fl); 17674945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; 17687e589b0bSAlfred Perlstein break; 17694945c132SAlfred Perlstein } 17704945c132SAlfred Perlstein 17714945c132SAlfred Perlstein debuglog("Exiting do_lock...\n"); 17724945c132SAlfred Perlstein 17734945c132SAlfred Perlstein return retval; 17744945c132SAlfred Perlstein } 17754945c132SAlfred Perlstein 17764945c132SAlfred Perlstein enum nlm_stats 17774945c132SAlfred Perlstein do_unlock(struct file_lock *fl) 17784945c132SAlfred Perlstein { 17794945c132SAlfred Perlstein enum partialfilelock_status pfsret; 17804945c132SAlfred Perlstein enum nlm_stats retval; 17814945c132SAlfred Perlstein 17824945c132SAlfred Perlstein debuglog("Entering do_unlock...\n"); 17834945c132SAlfred Perlstein pfsret = unlock_partialfilelock(fl); 17844945c132SAlfred Perlstein 17857e589b0bSAlfred Perlstein switch (pfsret) { 17867e589b0bSAlfred Perlstein case PFL_GRANTED: 17874945c132SAlfred Perlstein debuglog("PFL unlock granted"); 17884945c132SAlfred Perlstein dump_filelock(fl); 17894945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 17907e589b0bSAlfred Perlstein break; 17917e589b0bSAlfred Perlstein case PFL_NFSDENIED: 17927e589b0bSAlfred Perlstein case PFL_HWDENIED: 17934945c132SAlfred Perlstein debuglog("PFL_NFS unlock denied"); 17944945c132SAlfred Perlstein dump_filelock(fl); 17954945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; 17967e589b0bSAlfred Perlstein break; 17977e589b0bSAlfred Perlstein case PFL_NFSDENIED_NOLOCK: 17987e589b0bSAlfred Perlstein case PFL_HWDENIED_NOLOCK: 17994945c132SAlfred Perlstein debuglog("PFL_NFS no lock found\n"); 18004945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; 18017e589b0bSAlfred Perlstein break; 18027e589b0bSAlfred Perlstein case PFL_NFSRESERR: 18037e589b0bSAlfred Perlstein case PFL_HWRESERR: 18047e589b0bSAlfred Perlstein debuglog("PFL unlock resource failure"); 18057e589b0bSAlfred Perlstein dump_filelock(fl); 18067e589b0bSAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; 18077e589b0bSAlfred Perlstein break; 18087e589b0bSAlfred Perlstein default: 18094945c132SAlfred Perlstein debuglog("PFL unlock *FAILED*"); 18104945c132SAlfred Perlstein dump_filelock(fl); 18114945c132SAlfred Perlstein retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; 18127e589b0bSAlfred Perlstein break; 18134945c132SAlfred Perlstein } 18144945c132SAlfred Perlstein 18154945c132SAlfred Perlstein debuglog("Exiting do_unlock...\n"); 18164945c132SAlfred Perlstein 18174945c132SAlfred Perlstein return retval; 18184945c132SAlfred Perlstein } 18194945c132SAlfred Perlstein 18204945c132SAlfred Perlstein /* 1821a4a8a04dSAlfred Perlstein * do_clear 1822a4a8a04dSAlfred Perlstein * 1823a4a8a04dSAlfred Perlstein * This routine is non-existent because it doesn't have a return code. 1824a4a8a04dSAlfred Perlstein * It is here for completeness in case someone *does* need to do return 1825a4a8a04dSAlfred Perlstein * codes later. A decent compiler should optimize this away. 1826a4a8a04dSAlfred Perlstein */ 1827a4a8a04dSAlfred Perlstein 1828a4a8a04dSAlfred Perlstein void 1829a4a8a04dSAlfred Perlstein do_clear(const char *hostname) 1830a4a8a04dSAlfred Perlstein { 1831b6dc41baSAlfred Perlstein 1832a4a8a04dSAlfred Perlstein clear_partialfilelock(hostname); 1833a4a8a04dSAlfred Perlstein } 1834a4a8a04dSAlfred Perlstein 1835a4a8a04dSAlfred Perlstein /* 18364945c132SAlfred Perlstein * The following routines are all called from the code which the 18374945c132SAlfred Perlstein * RPC layer invokes 18384945c132SAlfred Perlstein */ 18394945c132SAlfred Perlstein 18404945c132SAlfred Perlstein /* 18414945c132SAlfred Perlstein * testlock(): inform the caller if the requested lock would be granted 18424945c132SAlfred Perlstein * 18434945c132SAlfred Perlstein * returns NULL if lock would granted 18444945c132SAlfred Perlstein * returns pointer to a conflicting nlm4_holder if not 18454945c132SAlfred Perlstein */ 18464945c132SAlfred Perlstein 18474945c132SAlfred Perlstein struct nlm4_holder * 1848c4bc47baSAlfred Perlstein testlock(struct nlm4_lock *lock, bool_t exclusive, int flags __unused) 18494945c132SAlfred Perlstein { 18504945c132SAlfred Perlstein struct file_lock test_fl, *conflicting_fl; 18514945c132SAlfred Perlstein 18524945c132SAlfred Perlstein bzero(&test_fl, sizeof(test_fl)); 18534945c132SAlfred Perlstein 18544945c132SAlfred Perlstein bcopy(lock->fh.n_bytes, &(test_fl.filehandle), sizeof(fhandle_t)); 18554945c132SAlfred Perlstein copy_nlm4_lock_to_nlm4_holder(lock, exclusive, &test_fl.client); 18564945c132SAlfred Perlstein 18574945c132SAlfred Perlstein siglock(); 18584945c132SAlfred Perlstein do_test(&test_fl, &conflicting_fl); 18594945c132SAlfred Perlstein 18604945c132SAlfred Perlstein if (conflicting_fl == NULL) { 18614945c132SAlfred Perlstein debuglog("No conflicting lock found\n"); 18624945c132SAlfred Perlstein sigunlock(); 18634945c132SAlfred Perlstein return NULL; 18644945c132SAlfred Perlstein } else { 18654945c132SAlfred Perlstein debuglog("Found conflicting lock\n"); 18664945c132SAlfred Perlstein dump_filelock(conflicting_fl); 18674945c132SAlfred Perlstein sigunlock(); 18684945c132SAlfred Perlstein return (&conflicting_fl->client); 18694945c132SAlfred Perlstein } 18704945c132SAlfred Perlstein } 18714945c132SAlfred Perlstein 18724945c132SAlfred Perlstein /* 18734945c132SAlfred Perlstein * getlock: try to aquire the lock. 18744945c132SAlfred Perlstein * If file is already locked and we can sleep, put the lock in the list with 18754945c132SAlfred Perlstein * status LKST_WAITING; it'll be processed later. 18764945c132SAlfred Perlstein * Otherwise try to lock. If we're allowed to block, fork a child which 18774945c132SAlfred Perlstein * will do the blocking lock. 18784945c132SAlfred Perlstein */ 18795f07c7b2SAlfred Perlstein 18804945c132SAlfred Perlstein enum nlm_stats 18814945c132SAlfred Perlstein getlock(nlm4_lockargs *lckarg, struct svc_req *rqstp, const int flags) 18824945c132SAlfred Perlstein { 18834945c132SAlfred Perlstein struct file_lock *newfl; 18844945c132SAlfred Perlstein enum nlm_stats retval; 18854945c132SAlfred Perlstein 18864945c132SAlfred Perlstein debuglog("Entering getlock...\n"); 18874945c132SAlfred Perlstein 18884945c132SAlfred Perlstein if (grace_expired == 0 && lckarg->reclaim == 0) 18894945c132SAlfred Perlstein return (flags & LOCK_V4) ? 18904945c132SAlfred Perlstein nlm4_denied_grace_period : nlm_denied_grace_period; 18914945c132SAlfred Perlstein 18924945c132SAlfred Perlstein /* allocate new file_lock for this request */ 18935f07c7b2SAlfred Perlstein newfl = allocate_file_lock(&lckarg->alock.oh, &lckarg->cookie); 18944945c132SAlfred Perlstein if (newfl == NULL) { 18955f07c7b2SAlfred Perlstein syslog(LOG_NOTICE, "lock allocate failed: %s", strerror(errno)); 18964945c132SAlfred Perlstein /* failed */ 18974945c132SAlfred Perlstein return (flags & LOCK_V4) ? 18984945c132SAlfred Perlstein nlm4_denied_nolocks : nlm_denied_nolocks; 18994945c132SAlfred Perlstein } 19004945c132SAlfred Perlstein 19014945c132SAlfred Perlstein if (lckarg->alock.fh.n_len != sizeof(fhandle_t)) { 19024945c132SAlfred Perlstein debuglog("recieved fhandle size %d, local size %d", 19034945c132SAlfred Perlstein lckarg->alock.fh.n_len, (int)sizeof(fhandle_t)); 19044945c132SAlfred Perlstein } 19054945c132SAlfred Perlstein 19065f07c7b2SAlfred Perlstein fill_file_lock(newfl, (fhandle_t *)lckarg->alock.fh.n_bytes, 19075f07c7b2SAlfred Perlstein (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf, 1908815ac496SAlfred Perlstein lckarg->exclusive, lckarg->alock.svid, lckarg->alock.l_offset, 1909815ac496SAlfred Perlstein lckarg->alock.l_len, 19105f07c7b2SAlfred Perlstein lckarg->alock.caller_name, lckarg->state, 0, flags, lckarg->block); 19114945c132SAlfred Perlstein 19124945c132SAlfred Perlstein /* 19134945c132SAlfred Perlstein * newfl is now fully constructed and deallocate_file_lock 19144945c132SAlfred Perlstein * can now be used to delete it 19154945c132SAlfred Perlstein */ 19164945c132SAlfred Perlstein 19174945c132SAlfred Perlstein siglock(); 19184945c132SAlfred Perlstein debuglog("Pointer to new lock is %p\n",newfl); 19197e589b0bSAlfred Perlstein 19204945c132SAlfred Perlstein retval = do_lock(newfl); 19217e589b0bSAlfred Perlstein 19224945c132SAlfred Perlstein debuglog("Pointer to new lock is %p\n",newfl); 19234945c132SAlfred Perlstein sigunlock(); 19244945c132SAlfred Perlstein 19254945c132SAlfred Perlstein switch (retval) 19264945c132SAlfred Perlstein { 19274945c132SAlfred Perlstein case nlm4_granted: 19284945c132SAlfred Perlstein /* case nlm_granted: is the same as nlm4_granted */ 19294945c132SAlfred Perlstein /* do_mon(lckarg->alock.caller_name); */ 19304945c132SAlfred Perlstein break; 19314945c132SAlfred Perlstein case nlm4_blocked: 19324945c132SAlfred Perlstein /* case nlm_blocked: is the same as nlm4_blocked */ 19334945c132SAlfred Perlstein /* do_mon(lckarg->alock.caller_name); */ 19344945c132SAlfred Perlstein break; 19354945c132SAlfred Perlstein default: 19364945c132SAlfred Perlstein deallocate_file_lock(newfl); 19374945c132SAlfred Perlstein break; 19384945c132SAlfred Perlstein } 19394945c132SAlfred Perlstein 19404945c132SAlfred Perlstein debuglog("Exiting getlock...\n"); 19414945c132SAlfred Perlstein 19427e589b0bSAlfred Perlstein return retval; 19434945c132SAlfred Perlstein } 19444945c132SAlfred Perlstein 19454945c132SAlfred Perlstein 19464945c132SAlfred Perlstein /* unlock a filehandle */ 19474945c132SAlfred Perlstein enum nlm_stats 1948c4bc47baSAlfred Perlstein unlock(nlm4_lock *lock, const int flags __unused) 19494945c132SAlfred Perlstein { 19504945c132SAlfred Perlstein struct file_lock fl; 19514945c132SAlfred Perlstein enum nlm_stats err; 19524945c132SAlfred Perlstein 19534945c132SAlfred Perlstein siglock(); 19544945c132SAlfred Perlstein 19554945c132SAlfred Perlstein debuglog("Entering unlock...\n"); 19564945c132SAlfred Perlstein 19574945c132SAlfred Perlstein bzero(&fl,sizeof(struct file_lock)); 19584945c132SAlfred Perlstein bcopy(lock->fh.n_bytes, &fl.filehandle, sizeof(fhandle_t)); 19594945c132SAlfred Perlstein 19604945c132SAlfred Perlstein copy_nlm4_lock_to_nlm4_holder(lock, 0, &fl.client); 19614945c132SAlfred Perlstein 19624945c132SAlfred Perlstein err = do_unlock(&fl); 19634945c132SAlfred Perlstein 19644945c132SAlfred Perlstein sigunlock(); 19654945c132SAlfred Perlstein 19664945c132SAlfred Perlstein debuglog("Exiting unlock...\n"); 19674945c132SAlfred Perlstein 19684945c132SAlfred Perlstein return err; 19694945c132SAlfred Perlstein } 19704945c132SAlfred Perlstein 19714945c132SAlfred Perlstein /* 19724945c132SAlfred Perlstein * XXX: The following monitor/unmonitor routines 1973a4a8a04dSAlfred Perlstein * have not been extensively tested (ie. no regression 1974a4a8a04dSAlfred Perlstein * script exists like for the locking sections 19754945c132SAlfred Perlstein */ 19764945c132SAlfred Perlstein 19774945c132SAlfred Perlstein /* 19784945c132SAlfred Perlstein * monitor_lock_host: monitor lock hosts locally with a ref count and 19794945c132SAlfred Perlstein * inform statd 19804945c132SAlfred Perlstein */ 19814945c132SAlfred Perlstein void 19824945c132SAlfred Perlstein monitor_lock_host(const char *hostname) 19834945c132SAlfred Perlstein { 19844945c132SAlfred Perlstein struct host *ihp, *nhp; 19854945c132SAlfred Perlstein struct mon smon; 19864945c132SAlfred Perlstein struct sm_stat_res sres; 19874945c132SAlfred Perlstein int rpcret, statflag; 19884945c132SAlfred Perlstein 19894945c132SAlfred Perlstein rpcret = 0; 19904945c132SAlfred Perlstein statflag = 0; 19914945c132SAlfred Perlstein 1992b6dc41baSAlfred Perlstein LIST_FOREACH(ihp, &hostlst_head, hostlst) { 19934945c132SAlfred Perlstein if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) { 19944945c132SAlfred Perlstein /* Host is already monitored, bump refcount */ 19954945c132SAlfred Perlstein ++ihp->refcnt; 19964945c132SAlfred Perlstein /* Host should only be in the monitor list once */ 1997b6dc41baSAlfred Perlstein return; 19984945c132SAlfred Perlstein } 19994945c132SAlfred Perlstein } 20004945c132SAlfred Perlstein 20014945c132SAlfred Perlstein /* Host is not yet monitored, add it */ 20024945c132SAlfred Perlstein nhp = malloc(sizeof(struct host)); 20034945c132SAlfred Perlstein 20044945c132SAlfred Perlstein if (nhp == NULL) { 20054945c132SAlfred Perlstein debuglog("Unable to allocate entry for statd mon\n"); 2006a4a8a04dSAlfred Perlstein return; 2007b6dc41baSAlfred Perlstein } 2008b6dc41baSAlfred Perlstein 20094945c132SAlfred Perlstein /* Allocated new host entry, now fill the fields */ 20104945c132SAlfred Perlstein strncpy(nhp->name, hostname, SM_MAXSTRLEN); 20114945c132SAlfred Perlstein nhp->refcnt = 1; 20124945c132SAlfred Perlstein debuglog("Locally Monitoring host %16s\n",hostname); 20137e589b0bSAlfred Perlstein 20144945c132SAlfred Perlstein debuglog("Attempting to tell statd\n"); 20157e589b0bSAlfred Perlstein 2016a4a8a04dSAlfred Perlstein bzero(&smon,sizeof(smon)); 20177e589b0bSAlfred Perlstein 20184945c132SAlfred Perlstein smon.mon_id.mon_name = nhp->name; 2019a4a8a04dSAlfred Perlstein smon.mon_id.my_id.my_name = "localhost\0"; 2020a4a8a04dSAlfred Perlstein 20214945c132SAlfred Perlstein smon.mon_id.my_id.my_prog = NLM_PROG; 20224945c132SAlfred Perlstein smon.mon_id.my_id.my_vers = NLM_SM; 20234945c132SAlfred Perlstein smon.mon_id.my_id.my_proc = NLM_SM_NOTIFY; 20244945c132SAlfred Perlstein 20257e589b0bSAlfred Perlstein rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_MON, xdr_mon, 20267e589b0bSAlfred Perlstein &smon, xdr_sm_stat_res, &sres); 20274945c132SAlfred Perlstein 2028a4a8a04dSAlfred Perlstein if (rpcret == 0) { 20294945c132SAlfred Perlstein if (sres.res_stat == stat_fail) { 20304945c132SAlfred Perlstein debuglog("Statd call failed\n"); 20314945c132SAlfred Perlstein statflag = 0; 20324945c132SAlfred Perlstein } else { 20334945c132SAlfred Perlstein statflag = 1; 20344945c132SAlfred Perlstein } 20354945c132SAlfred Perlstein } else { 2036b6dc41baSAlfred Perlstein debuglog("Rpc call to statd failed with return value: %d\n", 2037b6dc41baSAlfred Perlstein rpcret); 20384945c132SAlfred Perlstein statflag = 0; 20394945c132SAlfred Perlstein } 20404945c132SAlfred Perlstein 20414945c132SAlfred Perlstein if (statflag == 1) { 20424945c132SAlfred Perlstein LIST_INSERT_HEAD(&hostlst_head, nhp, hostlst); 20434945c132SAlfred Perlstein } else { 20444945c132SAlfred Perlstein free(nhp); 20454945c132SAlfred Perlstein } 2046b6dc41baSAlfred Perlstein 20477e589b0bSAlfred Perlstein } 20484945c132SAlfred Perlstein 20494945c132SAlfred Perlstein /* 20504945c132SAlfred Perlstein * unmonitor_lock_host: clear monitor ref counts and inform statd when gone 20514945c132SAlfred Perlstein */ 20524945c132SAlfred Perlstein void 20534945c132SAlfred Perlstein unmonitor_lock_host(const char *hostname) 20544945c132SAlfred Perlstein { 2055a4a8a04dSAlfred Perlstein struct host *ihp; 20564945c132SAlfred Perlstein struct mon_id smon_id; 20574945c132SAlfred Perlstein struct sm_stat smstat; 20584945c132SAlfred Perlstein int rpcret; 20594945c132SAlfred Perlstein 20604945c132SAlfred Perlstein rpcret = 0; 20614945c132SAlfred Perlstein 20627e589b0bSAlfred Perlstein for( ihp=LIST_FIRST(&hostlst_head); ihp != NULL; 20637e589b0bSAlfred Perlstein ihp=LIST_NEXT(ihp, hostlst)) { 20644945c132SAlfred Perlstein if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) { 20654945c132SAlfred Perlstein /* Host is monitored, bump refcount */ 20664945c132SAlfred Perlstein --ihp->refcnt; 20674945c132SAlfred Perlstein /* Host should only be in the monitor list once */ 20684945c132SAlfred Perlstein break; 20694945c132SAlfred Perlstein } 20704945c132SAlfred Perlstein } 20714945c132SAlfred Perlstein 20724945c132SAlfred Perlstein if (ihp == NULL) { 20734945c132SAlfred Perlstein debuglog("Could not find host %16s in mon list\n", hostname); 2074b6dc41baSAlfred Perlstein return; 2075b6dc41baSAlfred Perlstein } 2076b6dc41baSAlfred Perlstein 2077b6dc41baSAlfred Perlstein if (ihp->refcnt > 0) 2078b6dc41baSAlfred Perlstein return; 2079b6dc41baSAlfred Perlstein 20807e589b0bSAlfred Perlstein if (ihp->refcnt < 0) { 20817e589b0bSAlfred Perlstein debuglog("Negative refcount!: %d\n", 20827e589b0bSAlfred Perlstein ihp->refcnt); 20834945c132SAlfred Perlstein } 20844945c132SAlfred Perlstein 2085b6dc41baSAlfred Perlstein debuglog("Attempting to unmonitor host %16s\n", hostname); 20864945c132SAlfred Perlstein 20874945c132SAlfred Perlstein bzero(&smon_id,sizeof(smon_id)); 20887e589b0bSAlfred Perlstein 2089a4a8a04dSAlfred Perlstein smon_id.mon_name = (char *)hostname; 20904945c132SAlfred Perlstein smon_id.my_id.my_name = "localhost"; 20914945c132SAlfred Perlstein smon_id.my_id.my_prog = NLM_PROG; 20924945c132SAlfred Perlstein smon_id.my_id.my_vers = NLM_SM; 20934945c132SAlfred Perlstein smon_id.my_id.my_proc = NLM_SM_NOTIFY; 20947e589b0bSAlfred Perlstein 20957e589b0bSAlfred Perlstein rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON, xdr_mon, 20967e589b0bSAlfred Perlstein &smon_id, xdr_sm_stat_res, &smstat); 20977e589b0bSAlfred Perlstein 2098a4a8a04dSAlfred Perlstein if (rpcret != 0) { 2099b6dc41baSAlfred Perlstein debuglog("Rpc call to unmonitor statd failed with " 2100b6dc41baSAlfred Perlstein " return value: %d\n", rpcret); 21017e589b0bSAlfred Perlstein } 21027e589b0bSAlfred Perlstein 21034945c132SAlfred Perlstein LIST_REMOVE(ihp, hostlst); 21044945c132SAlfred Perlstein free(ihp); 21054945c132SAlfred Perlstein } 21064945c132SAlfred Perlstein 21074945c132SAlfred Perlstein /* 21084945c132SAlfred Perlstein * notify: Clear all locks from a host if statd complains 21094945c132SAlfred Perlstein * 2110a4a8a04dSAlfred Perlstein * XXX: This routine has not been thoroughly tested. However, neither 2111a4a8a04dSAlfred Perlstein * had the old one been. It used to compare the statd crash state counter 2112a4a8a04dSAlfred Perlstein * to the current lock state. The upshot of this was that it basically 2113a4a8a04dSAlfred Perlstein * cleared all locks from the specified host 99% of the time (with the 2114a4a8a04dSAlfred Perlstein * other 1% being a bug). Consequently, the assumption is that clearing 2115a4a8a04dSAlfred Perlstein * all locks from a host when notified by statd is acceptable. 21164945c132SAlfred Perlstein * 2117a4a8a04dSAlfred Perlstein * Please note that this routine skips the usual level of redirection 2118a4a8a04dSAlfred Perlstein * through a do_* type routine. This introduces a possible level of 2119a4a8a04dSAlfred Perlstein * error and might better be written as do_notify and take this one out. 2120a4a8a04dSAlfred Perlstein 21214945c132SAlfred Perlstein */ 21224945c132SAlfred Perlstein 21234945c132SAlfred Perlstein void 21244945c132SAlfred Perlstein notify(const char *hostname, const int state) 21254945c132SAlfred Perlstein { 21264945c132SAlfred Perlstein debuglog("notify from %s, new state %d", hostname, state); 21274945c132SAlfred Perlstein 21284945c132SAlfred Perlstein siglock(); 2129a4a8a04dSAlfred Perlstein do_clear(hostname); 21304945c132SAlfred Perlstein sigunlock(); 2131a4a8a04dSAlfred Perlstein 2132a4a8a04dSAlfred Perlstein debuglog("Leaving notify\n"); 21334945c132SAlfred Perlstein } 21344945c132SAlfred Perlstein 21354945c132SAlfred Perlstein void 21364945c132SAlfred Perlstein send_granted(fl, opcode) 21374945c132SAlfred Perlstein struct file_lock *fl; 2138c4bc47baSAlfred Perlstein int opcode __unused; 21394945c132SAlfred Perlstein { 21404945c132SAlfred Perlstein CLIENT *cli; 21414945c132SAlfred Perlstein static char dummy; 21424945c132SAlfred Perlstein struct timeval timeo; 21434945c132SAlfred Perlstein int success; 21444945c132SAlfred Perlstein static struct nlm_res retval; 21454945c132SAlfred Perlstein static struct nlm4_res retval4; 21464945c132SAlfred Perlstein 21477e589b0bSAlfred Perlstein debuglog("About to send granted on blocked lock\n"); 21487e589b0bSAlfred Perlstein sleep(1); 21497e589b0bSAlfred Perlstein debuglog("Blowing off return send\n"); 21507e589b0bSAlfred Perlstein 21514945c132SAlfred Perlstein cli = get_client(fl->addr, 21524945c132SAlfred Perlstein (fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS); 21534945c132SAlfred Perlstein if (cli == NULL) { 21544945c132SAlfred Perlstein syslog(LOG_NOTICE, "failed to get CLIENT for %s", 21554945c132SAlfred Perlstein fl->client_name); 21564945c132SAlfred Perlstein /* 21574945c132SAlfred Perlstein * We fail to notify remote that the lock has been granted. 21584945c132SAlfred Perlstein * The client will timeout and retry, the lock will be 21594945c132SAlfred Perlstein * granted at this time. 21604945c132SAlfred Perlstein */ 21614945c132SAlfred Perlstein return; 21624945c132SAlfred Perlstein } 21634945c132SAlfred Perlstein timeo.tv_sec = 0; 21644945c132SAlfred Perlstein timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */ 21654945c132SAlfred Perlstein 21664945c132SAlfred Perlstein if (fl->flags & LOCK_V4) { 21674945c132SAlfred Perlstein static nlm4_testargs res; 21684945c132SAlfred Perlstein res.cookie = fl->client_cookie; 21694945c132SAlfred Perlstein res.exclusive = fl->client.exclusive; 21704945c132SAlfred Perlstein res.alock.caller_name = fl->client_name; 21714945c132SAlfred Perlstein res.alock.fh.n_len = sizeof(fhandle_t); 21724945c132SAlfred Perlstein res.alock.fh.n_bytes = (char*)&fl->filehandle; 21734945c132SAlfred Perlstein res.alock.oh = fl->client.oh; 21744945c132SAlfred Perlstein res.alock.svid = fl->client.svid; 21754945c132SAlfred Perlstein res.alock.l_offset = fl->client.l_offset; 21764945c132SAlfred Perlstein res.alock.l_len = fl->client.l_len; 21774945c132SAlfred Perlstein debuglog("sending v4 reply%s", 21784945c132SAlfred Perlstein (fl->flags & LOCK_ASYNC) ? " (async)":""); 21794945c132SAlfred Perlstein if (fl->flags & LOCK_ASYNC) { 21804945c132SAlfred Perlstein success = clnt_call(cli, NLM4_GRANTED_MSG, 21814945c132SAlfred Perlstein xdr_nlm4_testargs, &res, xdr_void, &dummy, timeo); 21824945c132SAlfred Perlstein } else { 21834945c132SAlfred Perlstein success = clnt_call(cli, NLM4_GRANTED, 21844945c132SAlfred Perlstein xdr_nlm4_testargs, &res, xdr_nlm4_res, 21854945c132SAlfred Perlstein &retval4, timeo); 21864945c132SAlfred Perlstein } 21874945c132SAlfred Perlstein } else { 21884945c132SAlfred Perlstein static nlm_testargs res; 21894945c132SAlfred Perlstein 21904945c132SAlfred Perlstein res.cookie = fl->client_cookie; 21914945c132SAlfred Perlstein res.exclusive = fl->client.exclusive; 21924945c132SAlfred Perlstein res.alock.caller_name = fl->client_name; 21934945c132SAlfred Perlstein res.alock.fh.n_len = sizeof(fhandle_t); 21944945c132SAlfred Perlstein res.alock.fh.n_bytes = (char*)&fl->filehandle; 21954945c132SAlfred Perlstein res.alock.oh = fl->client.oh; 21964945c132SAlfred Perlstein res.alock.svid = fl->client.svid; 21974945c132SAlfred Perlstein res.alock.l_offset = fl->client.l_offset; 21984945c132SAlfred Perlstein res.alock.l_len = fl->client.l_len; 21994945c132SAlfred Perlstein debuglog("sending v1 reply%s", 22004945c132SAlfred Perlstein (fl->flags & LOCK_ASYNC) ? " (async)":""); 22014945c132SAlfred Perlstein if (fl->flags & LOCK_ASYNC) { 22024945c132SAlfred Perlstein success = clnt_call(cli, NLM_GRANTED_MSG, 22034945c132SAlfred Perlstein xdr_nlm_testargs, &res, xdr_void, &dummy, timeo); 22044945c132SAlfred Perlstein } else { 22054945c132SAlfred Perlstein success = clnt_call(cli, NLM_GRANTED, 22064945c132SAlfred Perlstein xdr_nlm_testargs, &res, xdr_nlm_res, 22074945c132SAlfred Perlstein &retval, timeo); 22084945c132SAlfred Perlstein } 22094945c132SAlfred Perlstein } 22104945c132SAlfred Perlstein if (debug_level > 2) 22117e589b0bSAlfred Perlstein debuglog("clnt_call returns %d(%s) for granted", 22124945c132SAlfred Perlstein success, clnt_sperrno(success)); 22134945c132SAlfred Perlstein 22144945c132SAlfred Perlstein } 22154945c132SAlfred Perlstein 22164945c132SAlfred Perlstein /* 2217a4a8a04dSAlfred Perlstein * Routines below here have not been modified in the overhaul 2218a4a8a04dSAlfred Perlstein */ 2219a4a8a04dSAlfred Perlstein 2220a4a8a04dSAlfred Perlstein /* 22217e589b0bSAlfred Perlstein * Are these two routines still required since lockd is not spawning off 22227e589b0bSAlfred Perlstein * children to service locks anymore? Presumably they were originally 22237e589b0bSAlfred Perlstein * put in place to prevent a one child from changing the lock list out 22247e589b0bSAlfred Perlstein * from under another one. 22254945c132SAlfred Perlstein */ 22267e589b0bSAlfred Perlstein 22274945c132SAlfred Perlstein void 22284945c132SAlfred Perlstein siglock(void) 22294945c132SAlfred Perlstein { 22304945c132SAlfred Perlstein sigset_t block; 22314945c132SAlfred Perlstein 22324945c132SAlfred Perlstein sigemptyset(&block); 22334945c132SAlfred Perlstein sigaddset(&block, SIGCHLD); 22344945c132SAlfred Perlstein 22357e589b0bSAlfred Perlstein if (sigprocmask(SIG_BLOCK, &block, NULL) < 0) { 22364945c132SAlfred Perlstein syslog(LOG_WARNING, "siglock failed: %s", strerror(errno)); 22374945c132SAlfred Perlstein } 22384945c132SAlfred Perlstein } 22394945c132SAlfred Perlstein 22404945c132SAlfred Perlstein void 22414945c132SAlfred Perlstein sigunlock(void) 22424945c132SAlfred Perlstein { 22434945c132SAlfred Perlstein sigset_t block; 22444945c132SAlfred Perlstein 22454945c132SAlfred Perlstein sigemptyset(&block); 22464945c132SAlfred Perlstein sigaddset(&block, SIGCHLD); 22474945c132SAlfred Perlstein 22487e589b0bSAlfred Perlstein if (sigprocmask(SIG_UNBLOCK, &block, NULL) < 0) { 22494945c132SAlfred Perlstein syslog(LOG_WARNING, "sigunlock failed: %s", strerror(errno)); 22504945c132SAlfred Perlstein } 22514945c132SAlfred Perlstein } 22527e589b0bSAlfred Perlstein 22537e589b0bSAlfred Perlstein 2254