xref: /freebsd/usr.sbin/rpc.lockd/lockd_lock.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
14945c132SAlfred Perlstein /*	$NetBSD: lockd_lock.c,v 1.5 2000/11/21 03:47:41 enami Exp $	*/
24945c132SAlfred Perlstein 
3df57947fSPedro F. Giffuni /*-
4df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
5df57947fSPedro F. Giffuni  *
64945c132SAlfred Perlstein  * Copyright (c) 2001 Andrew P. Lentvorski, Jr.
74945c132SAlfred Perlstein  * Copyright (c) 2000 Manuel Bouyer.
84945c132SAlfred Perlstein  *
94945c132SAlfred Perlstein  * Redistribution and use in source and binary forms, with or without
104945c132SAlfred Perlstein  * modification, are permitted provided that the following conditions
114945c132SAlfred Perlstein  * are met:
124945c132SAlfred Perlstein  * 1. Redistributions of source code must retain the above copyright
134945c132SAlfred Perlstein  *    notice, this list of conditions and the following disclaimer.
144945c132SAlfred Perlstein  * 2. Redistributions in binary form must reproduce the above copyright
154945c132SAlfred Perlstein  *    notice, this list of conditions and the following disclaimer in the
164945c132SAlfred Perlstein  *    documentation and/or other materials provided with the distribution.
174945c132SAlfred Perlstein  * 3. All advertising materials mentioning features or use of this software
184945c132SAlfred Perlstein  *    must display the following acknowledgement:
194945c132SAlfred Perlstein  *	This product includes software developed by the University of
204945c132SAlfred Perlstein  *	California, Berkeley and its contributors.
214945c132SAlfred Perlstein  * 4. Neither the name of the University nor the names of its contributors
224945c132SAlfred Perlstein  *    may be used to endorse or promote products derived from this software
234945c132SAlfred Perlstein  *    without specific prior written permission.
244945c132SAlfred Perlstein  *
254945c132SAlfred Perlstein  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264945c132SAlfred Perlstein  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274945c132SAlfred Perlstein  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284945c132SAlfred Perlstein  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294945c132SAlfred Perlstein  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304945c132SAlfred Perlstein  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314945c132SAlfred Perlstein  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324945c132SAlfred Perlstein  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334945c132SAlfred Perlstein  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344945c132SAlfred Perlstein  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354945c132SAlfred Perlstein  * SUCH DAMAGE.
364945c132SAlfred Perlstein  *
374945c132SAlfred Perlstein  */
384945c132SAlfred Perlstein 
397838f9ddSPhilippe Charnier #include <sys/cdefs.h>
40b6dc41baSAlfred Perlstein #define LOCKD_DEBUG
414945c132SAlfred Perlstein 
424945c132SAlfred Perlstein #include <stdio.h>
43b6dc41baSAlfred Perlstein #ifdef LOCKD_DEBUG
444945c132SAlfred Perlstein #include <stdarg.h>
454945c132SAlfred Perlstein #endif
464945c132SAlfred Perlstein #include <stdlib.h>
474945c132SAlfred Perlstein #include <unistd.h>
484945c132SAlfred Perlstein #include <fcntl.h>
494945c132SAlfred Perlstein #include <syslog.h>
504945c132SAlfred Perlstein #include <errno.h>
514945c132SAlfred Perlstein #include <string.h>
524945c132SAlfred Perlstein #include <signal.h>
534945c132SAlfred Perlstein #include <rpc/rpc.h>
544945c132SAlfred Perlstein #include <sys/types.h>
554945c132SAlfred Perlstein #include <sys/stat.h>
564945c132SAlfred Perlstein #include <sys/socket.h>
574945c132SAlfred Perlstein #include <sys/param.h>
584945c132SAlfred Perlstein #include <sys/mount.h>
594945c132SAlfred Perlstein #include <sys/wait.h>
604945c132SAlfred Perlstein #include <rpcsvc/sm_inter.h>
614945c132SAlfred Perlstein #include <rpcsvc/nlm_prot.h>
624945c132SAlfred Perlstein #include "lockd_lock.h"
634945c132SAlfred Perlstein #include "lockd.h"
644945c132SAlfred Perlstein 
654945c132SAlfred Perlstein #define MAXOBJECTSIZE 64
664945c132SAlfred Perlstein #define MAXBUFFERSIZE 1024
674945c132SAlfred Perlstein 
684945c132SAlfred Perlstein /*
694945c132SAlfred Perlstein  * A set of utilities for managing file locking
704945c132SAlfred Perlstein  *
714945c132SAlfred Perlstein  * XXX: All locks are in a linked list, a better structure should be used
723df5ecacSUlrich Spörlein  * to improve search/access efficiency.
734945c132SAlfred Perlstein  */
744945c132SAlfred Perlstein 
754945c132SAlfred Perlstein /* struct describing a lock */
764945c132SAlfred Perlstein struct file_lock {
774945c132SAlfred Perlstein 	LIST_ENTRY(file_lock) nfslocklist;
784945c132SAlfred Perlstein 	fhandle_t filehandle; /* NFS filehandle */
794945c132SAlfred Perlstein 	struct sockaddr *addr;
804945c132SAlfred Perlstein 	struct nlm4_holder client; /* lock holder */
815f07c7b2SAlfred Perlstein 	/* XXX: client_cookie used *only* in send_granted */
824945c132SAlfred Perlstein 	netobj client_cookie; /* cookie sent by the client */
834945c132SAlfred Perlstein 	int nsm_status; /* status from the remote lock manager */
844945c132SAlfred Perlstein 	int status; /* lock status, see below */
854945c132SAlfred Perlstein 	int flags; /* lock flags, see lockd_lock.h */
867e589b0bSAlfred Perlstein 	int blocking; /* blocking lock or not */
873d81d1adSMichael Reifenberger 	char client_name[SM_MAXSTRLEN];	/* client_name is really variable
883d81d1adSMichael Reifenberger 					   length and must be last! */
894945c132SAlfred Perlstein };
904945c132SAlfred Perlstein 
917e589b0bSAlfred Perlstein LIST_HEAD(nfslocklist_head, file_lock);
927e589b0bSAlfred Perlstein struct nfslocklist_head nfslocklist_head = LIST_HEAD_INITIALIZER(nfslocklist_head);
937e589b0bSAlfred Perlstein 
947e589b0bSAlfred Perlstein LIST_HEAD(blockedlocklist_head, file_lock);
957e589b0bSAlfred Perlstein struct blockedlocklist_head blockedlocklist_head = LIST_HEAD_INITIALIZER(blockedlocklist_head);
967e589b0bSAlfred Perlstein 
974945c132SAlfred Perlstein /* lock status */
984945c132SAlfred Perlstein #define LKST_LOCKED	1 /* lock is locked */
994945c132SAlfred Perlstein /* XXX: Is this flag file specific or lock specific? */
1004945c132SAlfred Perlstein #define LKST_WAITING	2 /* file is already locked by another host */
1013df5ecacSUlrich Spörlein #define LKST_PROCESSING	3 /* child is trying to acquire the lock */
1024945c132SAlfred Perlstein #define LKST_DYING	4 /* must dies when we get news from the child */
1034945c132SAlfred Perlstein 
1047e589b0bSAlfred Perlstein /* struct describing a monitored host */
1054945c132SAlfred Perlstein struct host {
1064945c132SAlfred Perlstein 	LIST_ENTRY(host) hostlst;
1074945c132SAlfred Perlstein 	int refcnt;
1083d81d1adSMichael Reifenberger 	char name[SM_MAXSTRLEN]; /* name is really variable length and
1093d81d1adSMichael Reifenberger                                     must be last! */
1104945c132SAlfred Perlstein };
1117e589b0bSAlfred Perlstein /* list of hosts we monitor */
1127e589b0bSAlfred Perlstein LIST_HEAD(hostlst_head, host);
1137e589b0bSAlfred Perlstein struct hostlst_head hostlst_head = LIST_HEAD_INITIALIZER(hostlst_head);
1147e589b0bSAlfred Perlstein 
1157e589b0bSAlfred Perlstein /*
1167e589b0bSAlfred Perlstein  * File monitoring handlers
1177e589b0bSAlfred Perlstein  * XXX: These might be able to be removed when kevent support
1187e589b0bSAlfred Perlstein  * is placed into the hardware lock/unlock routines.  (ie.
1197e589b0bSAlfred Perlstein  * let the kernel do all the file monitoring)
1207e589b0bSAlfred Perlstein  */
1217e589b0bSAlfred Perlstein 
1227e589b0bSAlfred Perlstein /* Struct describing a monitored file */
1237e589b0bSAlfred Perlstein struct monfile {
1247e589b0bSAlfred Perlstein 	LIST_ENTRY(monfile) monfilelist;
1257e589b0bSAlfred Perlstein 	fhandle_t filehandle; /* Local access filehandle */
1267e589b0bSAlfred Perlstein 	int fd; /* file descriptor: remains open until unlock! */
1277e589b0bSAlfred Perlstein 	int refcount;
1287e589b0bSAlfred Perlstein 	int exclusive;
1297e589b0bSAlfred Perlstein };
1307e589b0bSAlfred Perlstein 
1317e589b0bSAlfred Perlstein /* List of files we monitor */
1327e589b0bSAlfred Perlstein LIST_HEAD(monfilelist_head, monfile);
1337e589b0bSAlfred Perlstein struct monfilelist_head monfilelist_head = LIST_HEAD_INITIALIZER(monfilelist_head);
1344945c132SAlfred Perlstein 
1354945c132SAlfred Perlstein static int debugdelay = 0;
1364945c132SAlfred Perlstein 
1377e589b0bSAlfred Perlstein enum nfslock_status { NFS_GRANTED = 0, NFS_GRANTED_DUPLICATE,
1387e589b0bSAlfred Perlstein 		      NFS_DENIED, NFS_DENIED_NOLOCK,
1397e589b0bSAlfred Perlstein 		      NFS_RESERR };
1407e589b0bSAlfred Perlstein 
1417e589b0bSAlfred Perlstein enum hwlock_status { HW_GRANTED = 0, HW_GRANTED_DUPLICATE,
1427e589b0bSAlfred Perlstein 		     HW_DENIED, HW_DENIED_NOLOCK,
1437e589b0bSAlfred Perlstein 		     HW_STALEFH, HW_READONLY, HW_RESERR };
1444945c132SAlfred Perlstein 
1454945c132SAlfred Perlstein enum partialfilelock_status { PFL_GRANTED=0, PFL_GRANTED_DUPLICATE, PFL_DENIED,
1467e589b0bSAlfred Perlstein 			      PFL_NFSDENIED, PFL_NFSBLOCKED, PFL_NFSDENIED_NOLOCK, PFL_NFSRESERR,
1477e589b0bSAlfred Perlstein 			      PFL_HWDENIED,  PFL_HWBLOCKED,  PFL_HWDENIED_NOLOCK, PFL_HWRESERR};
1484945c132SAlfred Perlstein 
1495f07c7b2SAlfred Perlstein enum LFLAGS {LEDGE_LEFT, LEDGE_LBOUNDARY, LEDGE_INSIDE, LEDGE_RBOUNDARY, LEDGE_RIGHT};
1505f07c7b2SAlfred Perlstein enum RFLAGS {REDGE_LEFT, REDGE_LBOUNDARY, REDGE_INSIDE, REDGE_RBOUNDARY, REDGE_RIGHT};
1515f07c7b2SAlfred Perlstein /* XXX: WARNING! I HAVE OVERLOADED THIS STATUS ENUM!  SPLIT IT APART INTO TWO */
1525f07c7b2SAlfred Perlstein enum split_status {SPL_DISJOINT=0, SPL_LOCK1=1, SPL_LOCK2=2, SPL_CONTAINED=4, SPL_RESERR=8};
1535f07c7b2SAlfred Perlstein 
1547e589b0bSAlfred Perlstein enum partialfilelock_status lock_partialfilelock(struct file_lock *fl);
1557e589b0bSAlfred Perlstein 
1567e589b0bSAlfred Perlstein void send_granted(struct file_lock *fl, int opcode);
1574945c132SAlfred Perlstein void siglock(void);
1584945c132SAlfred Perlstein void sigunlock(void);
159a4a8a04dSAlfred Perlstein void monitor_lock_host(const char *hostname);
16075e40e46SPeter Wemm void unmonitor_lock_host(char *hostname);
1614945c132SAlfred Perlstein 
162b6dc41baSAlfred Perlstein void	copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock *src,
163b6dc41baSAlfred Perlstein     const bool_t exclusive, struct nlm4_holder *dest);
164815ac496SAlfred Perlstein struct file_lock *	allocate_file_lock(const netobj *lockowner,
1653d81d1adSMichael Reifenberger 					   const netobj *matchcookie,
1663d81d1adSMichael Reifenberger 					   const struct sockaddr *addr,
1673d81d1adSMichael Reifenberger 					   const char *caller_name);
168b6dc41baSAlfred Perlstein void	deallocate_file_lock(struct file_lock *fl);
169815ac496SAlfred Perlstein void	fill_file_lock(struct file_lock *fl, const fhandle_t *fh,
1703d81d1adSMichael Reifenberger 		       const bool_t exclusive, const int32_t svid,
1713d81d1adSMichael Reifenberger     const u_int64_t offset, const u_int64_t len,
172815ac496SAlfred Perlstein     const int state, const int status, const int flags, const int blocking);
173b6dc41baSAlfred Perlstein int	regions_overlap(const u_int64_t start1, const u_int64_t len1,
174a7526044SStefan Farfeleder     const u_int64_t start2, const u_int64_t len2);
1755f07c7b2SAlfred Perlstein enum split_status  region_compare(const u_int64_t starte, const u_int64_t lene,
1765f07c7b2SAlfred Perlstein     const u_int64_t startu, const u_int64_t lenu,
1775f07c7b2SAlfred Perlstein     u_int64_t *start1, u_int64_t *len1, u_int64_t *start2, u_int64_t *len2);
178b6dc41baSAlfred Perlstein int	same_netobj(const netobj *n0, const netobj *n1);
179b6dc41baSAlfred Perlstein int	same_filelock_identity(const struct file_lock *fl0,
180b6dc41baSAlfred Perlstein     const struct file_lock *fl2);
181b6dc41baSAlfred Perlstein 
182b6dc41baSAlfred Perlstein static void debuglog(char const *fmt, ...);
183b6dc41baSAlfred Perlstein void dump_static_object(const unsigned char* object, const int sizeof_object,
184b6dc41baSAlfred Perlstein                         unsigned char* hbuff, const int sizeof_hbuff,
185b6dc41baSAlfred Perlstein                         unsigned char* cbuff, const int sizeof_cbuff);
186b6dc41baSAlfred Perlstein void dump_netobj(const struct netobj *nobj);
187b6dc41baSAlfred Perlstein void dump_filelock(const struct file_lock *fl);
188b6dc41baSAlfred Perlstein struct file_lock *	get_lock_matching_unlock(const struct file_lock *fl);
189b6dc41baSAlfred Perlstein enum nfslock_status	test_nfslock(const struct file_lock *fl,
190b6dc41baSAlfred Perlstein     struct file_lock **conflicting_fl);
191b6dc41baSAlfred Perlstein enum nfslock_status	lock_nfslock(struct file_lock *fl);
192b6dc41baSAlfred Perlstein enum nfslock_status	delete_nfslock(struct file_lock *fl);
193b6dc41baSAlfred Perlstein enum nfslock_status	unlock_nfslock(const struct file_lock *fl,
1945f07c7b2SAlfred Perlstein     struct file_lock **released_lock, struct file_lock **left_lock,
1955f07c7b2SAlfred Perlstein     struct file_lock **right_lock);
196b6dc41baSAlfred Perlstein enum hwlock_status lock_hwlock(struct file_lock *fl);
197b6dc41baSAlfred Perlstein enum split_status split_nfslock(const struct file_lock *exist_lock,
1985f07c7b2SAlfred Perlstein     const struct file_lock *unlock_lock, struct file_lock **left_lock,
1995f07c7b2SAlfred Perlstein     struct file_lock **right_lock);
200091c4c86SUlrich Spörlein int	duplicate_block(struct file_lock *fl);
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 
2194945c132SAlfred Perlstein void
debuglog(char const * fmt,...)2204945c132SAlfred Perlstein debuglog(char const *fmt, ...)
2214945c132SAlfred Perlstein {
2224945c132SAlfred Perlstein 	va_list ap;
2234945c132SAlfred Perlstein 
2247e589b0bSAlfred Perlstein 	if (debug_level < 1) {
2254945c132SAlfred Perlstein 		return;
2267e589b0bSAlfred Perlstein 	}
2274945c132SAlfred Perlstein 
2284945c132SAlfred Perlstein 	sleep(debugdelay);
2294945c132SAlfred Perlstein 
2304945c132SAlfred Perlstein 	va_start(ap, fmt);
2314945c132SAlfred Perlstein 	vsyslog(LOG_DEBUG, fmt, ap);
2324945c132SAlfred Perlstein 	va_end(ap);
2334945c132SAlfred Perlstein }
2344945c132SAlfred Perlstein 
235b6dc41baSAlfred Perlstein void
dump_static_object(const unsigned char * object,const int size_object,unsigned char * hbuff,const int size_hbuff,unsigned char * cbuff,const int size_cbuff)2362541accbSJohn Baldwin dump_static_object(const unsigned char *object, const int size_object,
2372541accbSJohn Baldwin     unsigned char *hbuff, const int size_hbuff, unsigned char *cbuff,
2382541accbSJohn Baldwin     const int size_cbuff)
2394945c132SAlfred Perlstein {
2404945c132SAlfred Perlstein 	int i, objectsize;
2414945c132SAlfred Perlstein 
2427e589b0bSAlfred Perlstein 	if (debug_level < 2) {
2434945c132SAlfred Perlstein 		return;
2444945c132SAlfred Perlstein 	}
2454945c132SAlfred Perlstein 
246b6dc41baSAlfred Perlstein 	objectsize = size_object;
2477e589b0bSAlfred Perlstein 
2487e589b0bSAlfred Perlstein 	if (objectsize == 0) {
2497e589b0bSAlfred Perlstein 		debuglog("object is size 0\n");
2507e589b0bSAlfred Perlstein 	} else {
2514945c132SAlfred Perlstein 		if (objectsize > MAXOBJECTSIZE) {
2524945c132SAlfred Perlstein 			debuglog("Object of size %d being clamped"
2534945c132SAlfred Perlstein 			    "to size %d\n", objectsize, MAXOBJECTSIZE);
2544945c132SAlfred Perlstein 			objectsize = MAXOBJECTSIZE;
2554945c132SAlfred Perlstein 		}
2564945c132SAlfred Perlstein 
2574945c132SAlfred Perlstein 		if (hbuff != NULL) {
258b6dc41baSAlfred Perlstein 			if (size_hbuff < objectsize*2+1) {
2597e589b0bSAlfred Perlstein 				debuglog("Hbuff not large enough."
2607e589b0bSAlfred Perlstein 				    "  Increase size\n");
2614945c132SAlfred Perlstein 			} else {
2624945c132SAlfred Perlstein 				for(i=0;i<objectsize;i++) {
2634945c132SAlfred Perlstein 					sprintf(hbuff+i*2,"%02x",*(object+i));
2644945c132SAlfred Perlstein 				}
2654945c132SAlfred Perlstein 				*(hbuff+i*2) = '\0';
2664945c132SAlfred Perlstein 			}
2674945c132SAlfred Perlstein 		}
2684945c132SAlfred Perlstein 
2694945c132SAlfred Perlstein 		if (cbuff != NULL) {
270b6dc41baSAlfred Perlstein 			if (size_cbuff < objectsize+1) {
2714945c132SAlfred Perlstein 				debuglog("Cbuff not large enough."
2724945c132SAlfred Perlstein 				    "  Increase Size\n");
2734945c132SAlfred Perlstein 			}
2744945c132SAlfred Perlstein 
2754945c132SAlfred Perlstein 			for(i=0;i<objectsize;i++) {
2764945c132SAlfred Perlstein 				if (*(object+i) >= 32 && *(object+i) <= 127) {
2774945c132SAlfred Perlstein 					*(cbuff+i) = *(object+i);
2784945c132SAlfred Perlstein 				} else {
2794945c132SAlfred Perlstein 					*(cbuff+i) = '.';
2804945c132SAlfred Perlstein 				}
2814945c132SAlfred Perlstein 			}
2824945c132SAlfred Perlstein 			*(cbuff+i) = '\0';
2834945c132SAlfred Perlstein 		}
2844945c132SAlfred Perlstein 	}
2857e589b0bSAlfred Perlstein }
2864945c132SAlfred Perlstein 
287b6dc41baSAlfred Perlstein void
dump_netobj(const struct netobj * nobj)288b6dc41baSAlfred Perlstein dump_netobj(const struct netobj *nobj)
2894945c132SAlfred Perlstein {
2904945c132SAlfred Perlstein 	char hbuff[MAXBUFFERSIZE*2];
2914945c132SAlfred Perlstein 	char cbuff[MAXBUFFERSIZE];
2924945c132SAlfred Perlstein 
2937e589b0bSAlfred Perlstein 	if (debug_level < 2) {
2944945c132SAlfred Perlstein 		return;
2957e589b0bSAlfred Perlstein 	}
2964945c132SAlfred Perlstein 
2974945c132SAlfred Perlstein 	if (nobj == NULL) {
2984945c132SAlfred Perlstein 		debuglog("Null netobj pointer\n");
2997e589b0bSAlfred Perlstein 	}
3007e589b0bSAlfred Perlstein 	else if (nobj->n_len == 0) {
3014945c132SAlfred Perlstein 		debuglog("Size zero netobj\n");
3024945c132SAlfred Perlstein 	} else {
3034945c132SAlfred Perlstein 		dump_static_object(nobj->n_bytes, nobj->n_len,
3044945c132SAlfred Perlstein 		    hbuff, sizeof(hbuff), cbuff, sizeof(cbuff));
3054945c132SAlfred Perlstein 		debuglog("netobj: len: %d  data: %s :::  %s\n",
3064945c132SAlfred Perlstein 		    nobj->n_len, hbuff, cbuff);
3074945c132SAlfred Perlstein 	}
3084945c132SAlfred Perlstein }
3094945c132SAlfred Perlstein 
310c4bc47baSAlfred Perlstein /* #define DUMP_FILELOCK_VERBOSE */
311b6dc41baSAlfred Perlstein void
dump_filelock(const struct file_lock * fl)312b6dc41baSAlfred Perlstein dump_filelock(const struct file_lock *fl)
3134945c132SAlfred Perlstein {
314c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE
3154945c132SAlfred Perlstein 	char hbuff[MAXBUFFERSIZE*2];
3164945c132SAlfred Perlstein 	char cbuff[MAXBUFFERSIZE];
317c4bc47baSAlfred Perlstein #endif
3184945c132SAlfred Perlstein 
3197e589b0bSAlfred Perlstein 	if (debug_level < 2) {
3204945c132SAlfred Perlstein 		return;
3214945c132SAlfred Perlstein 	}
3224945c132SAlfred Perlstein 
3237e589b0bSAlfred Perlstein 	if (fl != NULL) {
3245f07c7b2SAlfred Perlstein 		debuglog("Dumping file lock structure @ %p\n", fl);
3254945c132SAlfred Perlstein 
326c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE
3274945c132SAlfred Perlstein 		dump_static_object((unsigned char *)&fl->filehandle,
3284945c132SAlfred Perlstein 		    sizeof(fl->filehandle), hbuff, sizeof(hbuff),
3294945c132SAlfred Perlstein 		    cbuff, sizeof(cbuff));
3304945c132SAlfred Perlstein 		debuglog("Filehandle: %8s  :::  %8s\n", hbuff, cbuff);
331c4bc47baSAlfred Perlstein #endif
3324945c132SAlfred Perlstein 
333b6dc41baSAlfred Perlstein 		debuglog("Dumping nlm4_holder:\n"
334b6dc41baSAlfred Perlstein 		    "exc: %x  svid: %x  offset:len %llx:%llx\n",
3354945c132SAlfred Perlstein 		    fl->client.exclusive, fl->client.svid,
3364945c132SAlfred Perlstein 		    fl->client.l_offset, fl->client.l_len);
3374945c132SAlfred Perlstein 
338c4bc47baSAlfred Perlstein #ifdef DUMP_FILELOCK_VERBOSE
3394945c132SAlfred Perlstein 		debuglog("Dumping client identity:\n");
3404945c132SAlfred Perlstein 		dump_netobj(&fl->client.oh);
3414945c132SAlfred Perlstein 
3424945c132SAlfred Perlstein 		debuglog("Dumping client cookie:\n");
3434945c132SAlfred Perlstein 		dump_netobj(&fl->client_cookie);
3444945c132SAlfred Perlstein 
345856f34f9SJun Kuriyama 		debuglog("nsm: %d  status: %d  flags: %d  svid: %x"
346856f34f9SJun Kuriyama 		    "  client_name: %s\n", fl->nsm_status, fl->status,
347856f34f9SJun Kuriyama 		    fl->flags, fl->client.svid, fl->client_name);
348c4bc47baSAlfred Perlstein #endif
3497e589b0bSAlfred Perlstein 	} else {
3507e589b0bSAlfred Perlstein 		debuglog("NULL file lock structure\n");
3517e589b0bSAlfred Perlstein 	}
3527e589b0bSAlfred Perlstein }
3537e589b0bSAlfred Perlstein 
3544945c132SAlfred Perlstein void
copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock * src,const bool_t exclusive,struct nlm4_holder * dest)3552541accbSJohn Baldwin copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock *src,
3562541accbSJohn Baldwin     const bool_t exclusive, struct nlm4_holder *dest)
3574945c132SAlfred Perlstein {
358b6dc41baSAlfred Perlstein 
3594945c132SAlfred Perlstein 	dest->exclusive = exclusive;
3604945c132SAlfred Perlstein 	dest->oh.n_len = src->oh.n_len;
3614945c132SAlfred Perlstein 	dest->oh.n_bytes = src->oh.n_bytes;
3624945c132SAlfred Perlstein 	dest->svid = src->svid;
3634945c132SAlfred Perlstein 	dest->l_offset = src->l_offset;
3644945c132SAlfred Perlstein 	dest->l_len = src->l_len;
3654945c132SAlfred Perlstein }
3664945c132SAlfred Perlstein 
3675f07c7b2SAlfred Perlstein /*
3685f07c7b2SAlfred Perlstein  * allocate_file_lock: Create a lock with the given parameters
3695f07c7b2SAlfred Perlstein  */
3705f07c7b2SAlfred Perlstein 
3715f07c7b2SAlfred Perlstein struct file_lock *
allocate_file_lock(const netobj * lockowner,const netobj * matchcookie,const struct sockaddr * addr,const char * caller_name)3723d81d1adSMichael Reifenberger allocate_file_lock(const netobj *lockowner, const netobj *matchcookie,
3733d81d1adSMichael Reifenberger 		   const struct sockaddr *addr, const char *caller_name)
3745f07c7b2SAlfred Perlstein {
3755f07c7b2SAlfred Perlstein 	struct file_lock *newfl;
3763d81d1adSMichael Reifenberger 	size_t n;
3775f07c7b2SAlfred Perlstein 
3783d81d1adSMichael Reifenberger 	/* Beware of rubbish input! */
3793d81d1adSMichael Reifenberger 	n = strnlen(caller_name, SM_MAXSTRLEN);
3803d81d1adSMichael Reifenberger 	if (n == SM_MAXSTRLEN) {
3813d81d1adSMichael Reifenberger 		return NULL;
3823d81d1adSMichael Reifenberger 	}
3833d81d1adSMichael Reifenberger 
3843d81d1adSMichael Reifenberger 	newfl = malloc(sizeof(*newfl) - sizeof(newfl->client_name) + n + 1);
3855f07c7b2SAlfred Perlstein 	if (newfl == NULL) {
3865f07c7b2SAlfred Perlstein 		return NULL;
3875f07c7b2SAlfred Perlstein 	}
3883d81d1adSMichael Reifenberger 	bzero(newfl, sizeof(*newfl) - sizeof(newfl->client_name));
3893d81d1adSMichael Reifenberger 	memcpy(newfl->client_name, caller_name, n);
3903d81d1adSMichael Reifenberger 	newfl->client_name[n] = 0;
3915f07c7b2SAlfred Perlstein 
3925f07c7b2SAlfred Perlstein 	newfl->client.oh.n_bytes = malloc(lockowner->n_len);
3935f07c7b2SAlfred Perlstein 	if (newfl->client.oh.n_bytes == NULL) {
3945f07c7b2SAlfred Perlstein 		free(newfl);
3955f07c7b2SAlfred Perlstein 		return NULL;
3965f07c7b2SAlfred Perlstein 	}
3975f07c7b2SAlfred Perlstein 	newfl->client.oh.n_len = lockowner->n_len;
3985f07c7b2SAlfred Perlstein 	bcopy(lockowner->n_bytes, newfl->client.oh.n_bytes, lockowner->n_len);
3995f07c7b2SAlfred Perlstein 
4005f07c7b2SAlfred Perlstein 	newfl->client_cookie.n_bytes = malloc(matchcookie->n_len);
4015f07c7b2SAlfred Perlstein 	if (newfl->client_cookie.n_bytes == NULL) {
4025f07c7b2SAlfred Perlstein 		free(newfl->client.oh.n_bytes);
4035f07c7b2SAlfred Perlstein 		free(newfl);
4045f07c7b2SAlfred Perlstein 		return NULL;
4055f07c7b2SAlfred Perlstein 	}
4065f07c7b2SAlfred Perlstein 	newfl->client_cookie.n_len = matchcookie->n_len;
4075f07c7b2SAlfred Perlstein 	bcopy(matchcookie->n_bytes, newfl->client_cookie.n_bytes, matchcookie->n_len);
4085f07c7b2SAlfred Perlstein 
4093d81d1adSMichael Reifenberger 	newfl->addr = malloc(addr->sa_len);
4103d81d1adSMichael Reifenberger 	if (newfl->addr == NULL) {
4113d81d1adSMichael Reifenberger 		free(newfl->client_cookie.n_bytes);
4123d81d1adSMichael Reifenberger 		free(newfl->client.oh.n_bytes);
4133d81d1adSMichael Reifenberger 		free(newfl);
4143d81d1adSMichael Reifenberger 		return NULL;
4153d81d1adSMichael Reifenberger 	}
4163d81d1adSMichael Reifenberger 	memcpy(newfl->addr, addr, addr->sa_len);
4173d81d1adSMichael Reifenberger 
4185f07c7b2SAlfred Perlstein 	return newfl;
4195f07c7b2SAlfred Perlstein }
4205f07c7b2SAlfred Perlstein 
4215f07c7b2SAlfred Perlstein /*
4225f07c7b2SAlfred Perlstein  * file_file_lock: Force creation of a valid file lock
4235f07c7b2SAlfred Perlstein  */
4245f07c7b2SAlfred Perlstein void
fill_file_lock(struct file_lock * fl,const fhandle_t * fh,const bool_t exclusive,const int32_t svid,const u_int64_t offset,const u_int64_t len,const int state,const int status,const int flags,const int blocking)425815ac496SAlfred Perlstein fill_file_lock(struct file_lock *fl, const fhandle_t *fh,
4263d81d1adSMichael Reifenberger     const bool_t exclusive, const int32_t svid,
4273d81d1adSMichael Reifenberger     const u_int64_t offset, const u_int64_t len,
428815ac496SAlfred Perlstein     const int state, const int status, const int flags, const int blocking)
4295f07c7b2SAlfred Perlstein {
4305f07c7b2SAlfred Perlstein 	bcopy(fh, &fl->filehandle, sizeof(fhandle_t));
4315f07c7b2SAlfred Perlstein 
4325f07c7b2SAlfred Perlstein 	fl->client.exclusive = exclusive;
4335f07c7b2SAlfred Perlstein 	fl->client.svid = svid;
4345f07c7b2SAlfred Perlstein 	fl->client.l_offset = offset;
4355f07c7b2SAlfred Perlstein 	fl->client.l_len = len;
4365f07c7b2SAlfred Perlstein 
4375f07c7b2SAlfred Perlstein 	fl->nsm_status = state;
4385f07c7b2SAlfred Perlstein 	fl->status = status;
4395f07c7b2SAlfred Perlstein 	fl->flags = flags;
4405f07c7b2SAlfred Perlstein 	fl->blocking = blocking;
4415f07c7b2SAlfred Perlstein }
4425f07c7b2SAlfred Perlstein 
4434945c132SAlfred Perlstein /*
4444945c132SAlfred Perlstein  * deallocate_file_lock: Free all storage associated with a file lock
4454945c132SAlfred Perlstein  */
4464945c132SAlfred Perlstein void
deallocate_file_lock(struct file_lock * fl)4474945c132SAlfred Perlstein deallocate_file_lock(struct file_lock *fl)
4484945c132SAlfred Perlstein {
4493d81d1adSMichael Reifenberger 	free(fl->addr);
4504945c132SAlfred Perlstein 	free(fl->client.oh.n_bytes);
4514945c132SAlfred Perlstein 	free(fl->client_cookie.n_bytes);
4524945c132SAlfred Perlstein 	free(fl);
4534945c132SAlfred Perlstein }
4544945c132SAlfred Perlstein 
4554945c132SAlfred Perlstein /*
4564945c132SAlfred Perlstein  * regions_overlap(): This function examines the two provided regions for
4575f07c7b2SAlfred Perlstein  * overlap.
4584945c132SAlfred Perlstein  */
4594945c132SAlfred Perlstein int
regions_overlap(const u_int64_t start1,const u_int64_t len1,const u_int64_t start2,const u_int64_t len2)4602541accbSJohn Baldwin regions_overlap(const u_int64_t start1, const u_int64_t len1,
4612541accbSJohn Baldwin     const u_int64_t start2, const u_int64_t len2)
4624945c132SAlfred Perlstein {
4635f07c7b2SAlfred Perlstein 	u_int64_t d1,d2,d3,d4;
4645f07c7b2SAlfred Perlstein 	enum split_status result;
4657e589b0bSAlfred Perlstein 
4664945c132SAlfred Perlstein 	debuglog("Entering region overlap with vals: %llu:%llu--%llu:%llu\n",
4674945c132SAlfred Perlstein 		 start1, len1, start2, len2);
4684945c132SAlfred Perlstein 
4695f07c7b2SAlfred Perlstein 	result = region_compare(start1, len1, start2, len2,
4705f07c7b2SAlfred Perlstein 	    &d1, &d2, &d3, &d4);
4714945c132SAlfred Perlstein 
4727e589b0bSAlfred Perlstein 	debuglog("Exiting region overlap with val: %d\n",result);
4737e589b0bSAlfred Perlstein 
4745f07c7b2SAlfred Perlstein 	if (result == SPL_DISJOINT) {
4755f07c7b2SAlfred Perlstein 		return 0;
4765f07c7b2SAlfred Perlstein 	} else {
4775f07c7b2SAlfred Perlstein 		return 1;
4785f07c7b2SAlfred Perlstein 	}
4797e589b0bSAlfred Perlstein }
4807e589b0bSAlfred Perlstein 
4814945c132SAlfred Perlstein /*
4825f07c7b2SAlfred Perlstein  * region_compare(): Examine lock regions and split appropriately
4835f07c7b2SAlfred Perlstein  *
4845f07c7b2SAlfred Perlstein  * XXX: Fix 64 bit overflow problems
4855f07c7b2SAlfred Perlstein  * XXX: Check to make sure I got *ALL* the cases.
4865f07c7b2SAlfred Perlstein  * XXX: This DESPERATELY needs a regression test.
4875f07c7b2SAlfred Perlstein  */
4885f07c7b2SAlfred Perlstein enum split_status
region_compare(const u_int64_t starte,const u_int64_t lene,const u_int64_t startu,const u_int64_t lenu,u_int64_t * start1,u_int64_t * len1,u_int64_t * start2,u_int64_t * len2)4892541accbSJohn Baldwin region_compare(const u_int64_t starte, const u_int64_t lene,
4902541accbSJohn Baldwin     const u_int64_t startu, const u_int64_t lenu, u_int64_t *start1,
4912541accbSJohn Baldwin     u_int64_t *len1, u_int64_t *start2, u_int64_t *len2)
4925f07c7b2SAlfred Perlstein {
4935f07c7b2SAlfred Perlstein 	/*
4945f07c7b2SAlfred Perlstein 	 * Please pay attention to the sequential exclusions
4955f07c7b2SAlfred Perlstein 	 * of the if statements!!!
4965f07c7b2SAlfred Perlstein 	 */
4975f07c7b2SAlfred Perlstein 	enum LFLAGS lflags;
4985f07c7b2SAlfred Perlstein 	enum RFLAGS rflags;
4995f07c7b2SAlfred Perlstein 	enum split_status retval;
5005f07c7b2SAlfred Perlstein 
5015f07c7b2SAlfred Perlstein 	retval = SPL_DISJOINT;
5025f07c7b2SAlfred Perlstein 
5035f07c7b2SAlfred Perlstein 	if (lene == 0 && lenu == 0) {
5045f07c7b2SAlfred Perlstein 		/* Examine left edge of locker */
5057838f9ddSPhilippe Charnier 		lflags = LEDGE_INSIDE;
5065f07c7b2SAlfred Perlstein 		if (startu < starte) {
5075f07c7b2SAlfred Perlstein 			lflags = LEDGE_LEFT;
5085f07c7b2SAlfred Perlstein 		} else if (startu == starte) {
5095f07c7b2SAlfred Perlstein 			lflags = LEDGE_LBOUNDARY;
5105f07c7b2SAlfred Perlstein 		}
5115f07c7b2SAlfred Perlstein 
5125f07c7b2SAlfred Perlstein 		rflags = REDGE_RBOUNDARY; /* Both are infiinite */
5135f07c7b2SAlfred Perlstein 
5145f07c7b2SAlfred Perlstein 		if (lflags == LEDGE_INSIDE) {
5155f07c7b2SAlfred Perlstein 			*start1 = starte;
5165f07c7b2SAlfred Perlstein 			*len1 = startu - starte;
5175f07c7b2SAlfred Perlstein 		}
5185f07c7b2SAlfred Perlstein 
5195f07c7b2SAlfred Perlstein 		if (lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) {
5205f07c7b2SAlfred Perlstein 			retval = SPL_CONTAINED;
5215f07c7b2SAlfred Perlstein 		} else {
5225f07c7b2SAlfred Perlstein 			retval = SPL_LOCK1;
5235f07c7b2SAlfred Perlstein 		}
5245f07c7b2SAlfred Perlstein 	} else if (lene == 0 && lenu != 0) {
5255f07c7b2SAlfred Perlstein 		/* Established lock is infinite */
5265f07c7b2SAlfred Perlstein 		/* Examine left edge of unlocker */
5277838f9ddSPhilippe Charnier 		lflags = LEDGE_INSIDE;
5285f07c7b2SAlfred Perlstein 		if (startu < starte) {
5295f07c7b2SAlfred Perlstein 			lflags = LEDGE_LEFT;
5305f07c7b2SAlfred Perlstein 		} else if (startu == starte) {
5315f07c7b2SAlfred Perlstein 			lflags = LEDGE_LBOUNDARY;
5325f07c7b2SAlfred Perlstein 		}
5335f07c7b2SAlfred Perlstein 
5345f07c7b2SAlfred Perlstein 		/* Examine right edge of unlocker */
5355f07c7b2SAlfred Perlstein 		if (startu + lenu < starte) {
5365f07c7b2SAlfred Perlstein 			/* Right edge of unlocker left of established lock */
5375f07c7b2SAlfred Perlstein 			rflags = REDGE_LEFT;
5385f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
5395f07c7b2SAlfred Perlstein 		} else if (startu + lenu == starte) {
5405f07c7b2SAlfred Perlstein 			/* Right edge of unlocker on start of established lock */
5415f07c7b2SAlfred Perlstein 			rflags = REDGE_LBOUNDARY;
5425f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
5435f07c7b2SAlfred Perlstein 		} else { /* Infinifty is right of finity */
5445f07c7b2SAlfred Perlstein 			/* Right edge of unlocker inside established lock */
5455f07c7b2SAlfred Perlstein 			rflags = REDGE_INSIDE;
5465f07c7b2SAlfred Perlstein 		}
5475f07c7b2SAlfred Perlstein 
5485f07c7b2SAlfred Perlstein 		if (lflags == LEDGE_INSIDE) {
5495f07c7b2SAlfred Perlstein 			*start1 = starte;
5505f07c7b2SAlfred Perlstein 			*len1 = startu - starte;
5515f07c7b2SAlfred Perlstein 			retval |= SPL_LOCK1;
5525f07c7b2SAlfred Perlstein 		}
5535f07c7b2SAlfred Perlstein 
5545f07c7b2SAlfred Perlstein 		if (rflags == REDGE_INSIDE) {
5555f07c7b2SAlfred Perlstein 			/* Create right lock */
5565f07c7b2SAlfred Perlstein 			*start2 = startu+lenu;
5575f07c7b2SAlfred Perlstein 			*len2 = 0;
5585f07c7b2SAlfred Perlstein 			retval |= SPL_LOCK2;
5595f07c7b2SAlfred Perlstein 		}
5605f07c7b2SAlfred Perlstein 	} else if (lene != 0 && lenu == 0) {
5615f07c7b2SAlfred Perlstein 		/* Unlocker is infinite */
5625f07c7b2SAlfred Perlstein 		/* Examine left edge of unlocker */
5637838f9ddSPhilippe Charnier 		lflags = LEDGE_RIGHT;
5645f07c7b2SAlfred Perlstein 		if (startu < starte) {
5655f07c7b2SAlfred Perlstein 			lflags = LEDGE_LEFT;
5665f07c7b2SAlfred Perlstein 			retval = SPL_CONTAINED;
5675f07c7b2SAlfred Perlstein 			return retval;
5685f07c7b2SAlfred Perlstein 		} else if (startu == starte) {
5695f07c7b2SAlfred Perlstein 			lflags = LEDGE_LBOUNDARY;
5705f07c7b2SAlfred Perlstein 			retval = SPL_CONTAINED;
5715f07c7b2SAlfred Perlstein 			return retval;
5725f07c7b2SAlfred Perlstein 		} else if ((startu > starte) && (startu < starte + lene - 1)) {
5735f07c7b2SAlfred Perlstein 			lflags = LEDGE_INSIDE;
5745f07c7b2SAlfred Perlstein 		} else if (startu == starte + lene - 1) {
5755f07c7b2SAlfred Perlstein 			lflags = LEDGE_RBOUNDARY;
5765f07c7b2SAlfred Perlstein 		} else { /* startu > starte + lene -1 */
5775f07c7b2SAlfred Perlstein 			lflags = LEDGE_RIGHT;
5785f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
5795f07c7b2SAlfred Perlstein 		}
5805f07c7b2SAlfred Perlstein 
5815f07c7b2SAlfred Perlstein 		rflags = REDGE_RIGHT; /* Infinity is right of finity */
5825f07c7b2SAlfred Perlstein 
5835f07c7b2SAlfred Perlstein 		if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) {
5845f07c7b2SAlfred Perlstein 			*start1 = starte;
5855f07c7b2SAlfred Perlstein 			*len1 = startu - starte;
5865f07c7b2SAlfred Perlstein 			retval |= SPL_LOCK1;
5875f07c7b2SAlfred Perlstein 			return retval;
5885f07c7b2SAlfred Perlstein 		}
5895f07c7b2SAlfred Perlstein 	} else {
5905f07c7b2SAlfred Perlstein 		/* Both locks are finite */
5915f07c7b2SAlfred Perlstein 
5925f07c7b2SAlfred Perlstein 		/* Examine left edge of unlocker */
5937838f9ddSPhilippe Charnier 		lflags = LEDGE_RIGHT;
5945f07c7b2SAlfred Perlstein 		if (startu < starte) {
5955f07c7b2SAlfred Perlstein 			lflags = LEDGE_LEFT;
5965f07c7b2SAlfred Perlstein 		} else if (startu == starte) {
5975f07c7b2SAlfred Perlstein 			lflags = LEDGE_LBOUNDARY;
5985f07c7b2SAlfred Perlstein 		} else if ((startu > starte) && (startu < starte + lene - 1)) {
5995f07c7b2SAlfred Perlstein 			lflags = LEDGE_INSIDE;
6005f07c7b2SAlfred Perlstein 		} else if (startu == starte + lene - 1) {
6015f07c7b2SAlfred Perlstein 			lflags = LEDGE_RBOUNDARY;
6025f07c7b2SAlfred Perlstein 		} else { /* startu > starte + lene -1 */
6035f07c7b2SAlfred Perlstein 			lflags = LEDGE_RIGHT;
6045f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
6055f07c7b2SAlfred Perlstein 		}
6065f07c7b2SAlfred Perlstein 
6075f07c7b2SAlfred Perlstein 		/* Examine right edge of unlocker */
6085f07c7b2SAlfred Perlstein 		if (startu + lenu < starte) {
6095f07c7b2SAlfred Perlstein 			/* Right edge of unlocker left of established lock */
6105f07c7b2SAlfred Perlstein 			rflags = REDGE_LEFT;
6115f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
6125f07c7b2SAlfred Perlstein 		} else if (startu + lenu == starte) {
6135f07c7b2SAlfred Perlstein 			/* Right edge of unlocker on start of established lock */
6145f07c7b2SAlfred Perlstein 			rflags = REDGE_LBOUNDARY;
6155f07c7b2SAlfred Perlstein 			return SPL_DISJOINT;
6165f07c7b2SAlfred Perlstein 		} else if (startu + lenu < starte + lene) {
6175f07c7b2SAlfred Perlstein 			/* Right edge of unlocker inside established lock */
6185f07c7b2SAlfred Perlstein 			rflags = REDGE_INSIDE;
6195f07c7b2SAlfred Perlstein 		} else if (startu + lenu == starte + lene) {
6205f07c7b2SAlfred Perlstein 			/* Right edge of unlocker on right edge of established lock */
6215f07c7b2SAlfred Perlstein 			rflags = REDGE_RBOUNDARY;
6225f07c7b2SAlfred Perlstein 		} else { /* startu + lenu > starte + lene */
6235f07c7b2SAlfred Perlstein 			/* Right edge of unlocker is right of established lock */
6245f07c7b2SAlfred Perlstein 			rflags = REDGE_RIGHT;
6255f07c7b2SAlfred Perlstein 		}
6265f07c7b2SAlfred Perlstein 
6275f07c7b2SAlfred Perlstein 		if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) {
6285f07c7b2SAlfred Perlstein 			/* Create left lock */
6295f07c7b2SAlfred Perlstein 			*start1 = starte;
6305f07c7b2SAlfred Perlstein 			*len1 = (startu - starte);
6315f07c7b2SAlfred Perlstein 			retval |= SPL_LOCK1;
6325f07c7b2SAlfred Perlstein 		}
6335f07c7b2SAlfred Perlstein 
6345f07c7b2SAlfred Perlstein 		if (rflags == REDGE_INSIDE) {
6355f07c7b2SAlfred Perlstein 			/* Create right lock */
6365f07c7b2SAlfred Perlstein 			*start2 = startu+lenu;
6375f07c7b2SAlfred Perlstein 			*len2 = starte+lene-(startu+lenu);
6385f07c7b2SAlfred Perlstein 			retval |= SPL_LOCK2;
6395f07c7b2SAlfred Perlstein 		}
6405f07c7b2SAlfred Perlstein 
6415f07c7b2SAlfred Perlstein 		if ((lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) &&
6425f07c7b2SAlfred Perlstein 		    (rflags == REDGE_RBOUNDARY || rflags == REDGE_RIGHT)) {
6435f07c7b2SAlfred Perlstein 			retval = SPL_CONTAINED;
6445f07c7b2SAlfred Perlstein 		}
6455f07c7b2SAlfred Perlstein 	}
6465f07c7b2SAlfred Perlstein 	return retval;
6475f07c7b2SAlfred Perlstein }
6485f07c7b2SAlfred Perlstein 
6495f07c7b2SAlfred Perlstein /*
6504945c132SAlfred Perlstein  * same_netobj: Compares the apprpriate bits of a netobj for identity
6514945c132SAlfred Perlstein  */
6524945c132SAlfred Perlstein int
same_netobj(const netobj * n0,const netobj * n1)6534945c132SAlfred Perlstein same_netobj(const netobj *n0, const netobj *n1)
6544945c132SAlfred Perlstein {
6554945c132SAlfred Perlstein 	int retval;
6564945c132SAlfred Perlstein 
6574945c132SAlfred Perlstein 	retval = 0;
6584945c132SAlfred Perlstein 
6594945c132SAlfred Perlstein 	debuglog("Entering netobj identity check\n");
6604945c132SAlfred Perlstein 
6614945c132SAlfred Perlstein 	if (n0->n_len == n1->n_len) {
6624945c132SAlfred Perlstein 		debuglog("Preliminary length check passed\n");
663b6dc41baSAlfred Perlstein 		retval = !bcmp(n0->n_bytes, n1->n_bytes, n0->n_len);
664b6dc41baSAlfred Perlstein 		debuglog("netobj %smatch\n", retval ? "" : "mis");
6654945c132SAlfred Perlstein 	}
6664945c132SAlfred Perlstein 
667b6dc41baSAlfred Perlstein 	return (retval);
6684945c132SAlfred Perlstein }
6694945c132SAlfred Perlstein 
6704945c132SAlfred Perlstein /*
6714945c132SAlfred Perlstein  * same_filelock_identity: Compares the appropriate bits of a file_lock
6724945c132SAlfred Perlstein  */
6734945c132SAlfred Perlstein int
same_filelock_identity(const struct file_lock * fl0,const struct file_lock * fl1)6742541accbSJohn Baldwin same_filelock_identity(const struct file_lock *fl0, const struct file_lock *fl1)
6754945c132SAlfred Perlstein {
6764945c132SAlfred Perlstein 	int retval;
6774945c132SAlfred Perlstein 
6784945c132SAlfred Perlstein 	retval = 0;
6794945c132SAlfred Perlstein 
6804945c132SAlfred Perlstein 	debuglog("Checking filelock identity\n");
6814945c132SAlfred Perlstein 
682b6dc41baSAlfred Perlstein 	/*
683b6dc41baSAlfred Perlstein 	 * Check process ids and host information.
684b6dc41baSAlfred Perlstein 	 */
685b6dc41baSAlfred Perlstein 	retval = (fl0->client.svid == fl1->client.svid &&
686b6dc41baSAlfred Perlstein 	    same_netobj(&(fl0->client.oh), &(fl1->client.oh)));
6874945c132SAlfred Perlstein 
6884945c132SAlfred Perlstein 	debuglog("Exiting checking filelock identity: retval: %d\n",retval);
6894945c132SAlfred Perlstein 
690b6dc41baSAlfred Perlstein 	return (retval);
6914945c132SAlfred Perlstein }
6924945c132SAlfred Perlstein 
6934945c132SAlfred Perlstein /*
6944945c132SAlfred Perlstein  * Below here are routines associated with manipulating the NFS
6954945c132SAlfred Perlstein  * lock list.
6964945c132SAlfred Perlstein  */
6974945c132SAlfred Perlstein 
6984945c132SAlfred Perlstein /*
6994945c132SAlfred Perlstein  * get_lock_matching_unlock: Return a lock which matches the given unlock lock
7004945c132SAlfred Perlstein  *                           or NULL otehrwise
701b6dc41baSAlfred Perlstein  * XXX: It is a shame that this duplicates so much code from test_nfslock.
7024945c132SAlfred Perlstein  */
7034945c132SAlfred Perlstein struct file_lock *
get_lock_matching_unlock(const struct file_lock * fl)7044945c132SAlfred Perlstein get_lock_matching_unlock(const struct file_lock *fl)
7054945c132SAlfred Perlstein {
7064945c132SAlfred Perlstein 	struct file_lock *ifl; /* Iterator */
7074945c132SAlfred Perlstein 
708856f34f9SJun Kuriyama 	debuglog("Entering get_lock_matching_unlock\n");
7094945c132SAlfred Perlstein 	debuglog("********Dump of fl*****************\n");
7104945c132SAlfred Perlstein 	dump_filelock(fl);
7114945c132SAlfred Perlstein 
712b6dc41baSAlfred Perlstein 	LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) {
7134945c132SAlfred Perlstein 		debuglog("Pointer to file lock: %p\n",ifl);
7144945c132SAlfred Perlstein 
7154945c132SAlfred Perlstein 		debuglog("****Dump of ifl****\n");
7164945c132SAlfred Perlstein 		dump_filelock(ifl);
7174945c132SAlfred Perlstein 		debuglog("*******************\n");
7184945c132SAlfred Perlstein 
7194945c132SAlfred Perlstein 		/*
7204945c132SAlfred Perlstein 		 * XXX: It is conceivable that someone could use the NLM RPC
7214945c132SAlfred Perlstein 		 * system to directly access filehandles.  This may be a
7224945c132SAlfred Perlstein 		 * security hazard as the filehandle code may bypass normal
7234945c132SAlfred Perlstein 		 * file access controls
7244945c132SAlfred Perlstein 		 */
725b6dc41baSAlfred Perlstein 		if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t)))
726b6dc41baSAlfred Perlstein 			continue;
727b6dc41baSAlfred Perlstein 
728856f34f9SJun Kuriyama 		debuglog("get_lock_matching_unlock: Filehandles match, "
729b6dc41baSAlfred Perlstein 		    "checking regions\n");
7304945c132SAlfred Perlstein 
7314945c132SAlfred Perlstein 		/* Filehandles match, check for region overlap */
732b6dc41baSAlfred Perlstein 		if (!regions_overlap(fl->client.l_offset, fl->client.l_len,
733b6dc41baSAlfred Perlstein 			ifl->client.l_offset, ifl->client.l_len))
734b6dc41baSAlfred Perlstein 			continue;
735b6dc41baSAlfred Perlstein 
736856f34f9SJun Kuriyama 		debuglog("get_lock_matching_unlock: Region overlap"
7374945c132SAlfred Perlstein 		    " found %llu : %llu -- %llu : %llu\n",
7384945c132SAlfred Perlstein 		    fl->client.l_offset,fl->client.l_len,
7394945c132SAlfred Perlstein 		    ifl->client.l_offset,ifl->client.l_len);
7404945c132SAlfred Perlstein 
7414945c132SAlfred Perlstein 		/* Regions overlap, check the identity */
742b6dc41baSAlfred Perlstein 		if (!same_filelock_identity(fl,ifl))
743b6dc41baSAlfred Perlstein 			continue;
744b6dc41baSAlfred Perlstein 
745856f34f9SJun Kuriyama 		debuglog("get_lock_matching_unlock: Duplicate lock id.  Granting\n");
746b6dc41baSAlfred Perlstein 		return (ifl);
7474945c132SAlfred Perlstein 	}
7484945c132SAlfred Perlstein 
749856f34f9SJun Kuriyama 	debuglog("Exiting bet_lock_matching_unlock\n");
7504945c132SAlfred Perlstein 
751b6dc41baSAlfred Perlstein 	return (NULL);
7524945c132SAlfred Perlstein }
7534945c132SAlfred Perlstein 
7544945c132SAlfred Perlstein /*
7554945c132SAlfred Perlstein  * test_nfslock: check for NFS lock in lock list
7564945c132SAlfred Perlstein  *
7574945c132SAlfred Perlstein  * This routine makes the following assumptions:
7584945c132SAlfred Perlstein  *    1) Nothing will adjust the lock list during a lookup
7594945c132SAlfred Perlstein  *
760*6372f2c8SElyes Haouas  * This routine has an interesting quirk which bit me hard.
7614945c132SAlfred Perlstein  * The conflicting_fl is the pointer to the conflicting lock.
7624945c132SAlfred Perlstein  * However, to modify the "*pointer* to the conflicting lock" rather
7634945c132SAlfred Perlstein  * that the "conflicting lock itself" one must pass in a "pointer to
7644945c132SAlfred Perlstein  * the pointer of the conflicting lock".  Gross.
7654945c132SAlfred Perlstein  */
7664945c132SAlfred Perlstein 
7674945c132SAlfred Perlstein enum nfslock_status
test_nfslock(const struct file_lock * fl,struct file_lock ** conflicting_fl)7684945c132SAlfred Perlstein test_nfslock(const struct file_lock *fl, struct file_lock **conflicting_fl)
7694945c132SAlfred Perlstein {
7704945c132SAlfred Perlstein 	struct file_lock *ifl; /* Iterator */
7714945c132SAlfred Perlstein 	enum nfslock_status retval;
7724945c132SAlfred Perlstein 
7737e589b0bSAlfred Perlstein 	debuglog("Entering test_nfslock\n");
7744945c132SAlfred Perlstein 
7754945c132SAlfred Perlstein 	retval = NFS_GRANTED;
7764945c132SAlfred Perlstein 	(*conflicting_fl) = NULL;
7774945c132SAlfred Perlstein 
7784945c132SAlfred Perlstein 	debuglog("Entering lock search loop\n");
7794945c132SAlfred Perlstein 
7804945c132SAlfred Perlstein 	debuglog("***********************************\n");
7814945c132SAlfred Perlstein 	debuglog("Dumping match filelock\n");
7824945c132SAlfred Perlstein 	debuglog("***********************************\n");
7834945c132SAlfred Perlstein 	dump_filelock(fl);
7844945c132SAlfred Perlstein 	debuglog("***********************************\n");
7854945c132SAlfred Perlstein 
786b6dc41baSAlfred Perlstein 	LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) {
787b6dc41baSAlfred Perlstein 		if (retval == NFS_DENIED)
788b6dc41baSAlfred Perlstein 			break;
789b6dc41baSAlfred Perlstein 
7904945c132SAlfred Perlstein 		debuglog("Top of lock loop\n");
7914945c132SAlfred Perlstein 		debuglog("Pointer to file lock: %p\n",ifl);
7924945c132SAlfred Perlstein 
7934945c132SAlfred Perlstein 		debuglog("***********************************\n");
7944945c132SAlfred Perlstein 		debuglog("Dumping test filelock\n");
7954945c132SAlfred Perlstein 		debuglog("***********************************\n");
7964945c132SAlfred Perlstein 		dump_filelock(ifl);
7974945c132SAlfred Perlstein 		debuglog("***********************************\n");
7984945c132SAlfred Perlstein 
7994945c132SAlfred Perlstein 		/*
8004945c132SAlfred Perlstein 		 * XXX: It is conceivable that someone could use the NLM RPC
8014945c132SAlfred Perlstein 		 * system to directly access filehandles.  This may be a
8024945c132SAlfred Perlstein 		 * security hazard as the filehandle code may bypass normal
8034945c132SAlfred Perlstein 		 * file access controls
8044945c132SAlfred Perlstein 		 */
805b6dc41baSAlfred Perlstein 		if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t)))
806b6dc41baSAlfred Perlstein 			continue;
807b6dc41baSAlfred Perlstein 
8084945c132SAlfred Perlstein 		debuglog("test_nfslock: filehandle match found\n");
8094945c132SAlfred Perlstein 
8104945c132SAlfred Perlstein 		/* Filehandles match, check for region overlap */
811b6dc41baSAlfred Perlstein 		if (!regions_overlap(fl->client.l_offset, fl->client.l_len,
812b6dc41baSAlfred Perlstein 			ifl->client.l_offset, ifl->client.l_len))
813b6dc41baSAlfred Perlstein 			continue;
814b6dc41baSAlfred Perlstein 
8154945c132SAlfred Perlstein 		debuglog("test_nfslock: Region overlap found"
8164945c132SAlfred Perlstein 		    " %llu : %llu -- %llu : %llu\n",
8174945c132SAlfred Perlstein 		    fl->client.l_offset,fl->client.l_len,
8184945c132SAlfred Perlstein 		    ifl->client.l_offset,ifl->client.l_len);
8194945c132SAlfred Perlstein 
8204945c132SAlfred Perlstein 		/* Regions overlap, check the exclusivity */
821b6dc41baSAlfred Perlstein 		if (!(fl->client.exclusive || ifl->client.exclusive))
822b6dc41baSAlfred Perlstein 			continue;
823b6dc41baSAlfred Perlstein 
824b6dc41baSAlfred Perlstein 		debuglog("test_nfslock: Exclusivity failure: %d %d\n",
8254945c132SAlfred Perlstein 		    fl->client.exclusive,
8264945c132SAlfred Perlstein 		    ifl->client.exclusive);
8274945c132SAlfred Perlstein 
8284945c132SAlfred Perlstein 		if (same_filelock_identity(fl,ifl)) {
829b6dc41baSAlfred Perlstein 			debuglog("test_nfslock: Duplicate id.  Granting\n");
8304945c132SAlfred Perlstein 			(*conflicting_fl) = ifl;
8314945c132SAlfred Perlstein 			retval = NFS_GRANTED_DUPLICATE;
8324945c132SAlfred Perlstein 		} else {
8334945c132SAlfred Perlstein 			/* locking attempt fails */
834b6dc41baSAlfred Perlstein 			debuglog("test_nfslock: Lock attempt failed\n");
8354945c132SAlfred Perlstein 			debuglog("Desired lock\n");
8364945c132SAlfred Perlstein 			dump_filelock(fl);
8374945c132SAlfred Perlstein 			debuglog("Conflicting lock\n");
8384945c132SAlfred Perlstein 			dump_filelock(ifl);
8394945c132SAlfred Perlstein 			(*conflicting_fl) = ifl;
8404945c132SAlfred Perlstein 			retval = NFS_DENIED;
8414945c132SAlfred Perlstein 		}
8424945c132SAlfred Perlstein 	}
8434945c132SAlfred Perlstein 
8444945c132SAlfred Perlstein 	debuglog("Dumping file locks\n");
8454945c132SAlfred Perlstein 	debuglog("Exiting test_nfslock\n");
8464945c132SAlfred Perlstein 
847b6dc41baSAlfred Perlstein 	return (retval);
8484945c132SAlfred Perlstein }
8494945c132SAlfred Perlstein 
8504945c132SAlfred Perlstein /*
8514945c132SAlfred Perlstein  * lock_nfslock: attempt to create a lock in the NFS lock list
8524945c132SAlfred Perlstein  *
8534945c132SAlfred Perlstein  * This routine tests whether the lock will be granted and then adds
8544945c132SAlfred Perlstein  * the entry to the lock list if so.
8554945c132SAlfred Perlstein  *
8564945c132SAlfred Perlstein  * Argument fl gets modified as its list housekeeping entries get modified
8574945c132SAlfred Perlstein  * upon insertion into the NFS lock list
8584945c132SAlfred Perlstein  *
8594945c132SAlfred Perlstein  * This routine makes several assumptions:
8604945c132SAlfred Perlstein  *    1) It is perfectly happy to grant a duplicate lock from the same pid.
8614945c132SAlfred Perlstein  *       While this seems to be intuitively wrong, it is required for proper
8624945c132SAlfred Perlstein  *       Posix semantics during unlock.  It is absolutely imperative to not
8634945c132SAlfred Perlstein  *       unlock the main lock before the two child locks are established. Thus,
864dae3a64fSEitan Adler  *       one has to be able to create duplicate locks over an existing lock
8654945c132SAlfred Perlstein  *    2) It currently accepts duplicate locks from the same id,pid
8664945c132SAlfred Perlstein  */
8674945c132SAlfred Perlstein 
8684945c132SAlfred Perlstein enum nfslock_status
lock_nfslock(struct file_lock * fl)8694945c132SAlfred Perlstein lock_nfslock(struct file_lock *fl)
8704945c132SAlfred Perlstein {
8714945c132SAlfred Perlstein 	enum nfslock_status retval;
8724945c132SAlfred Perlstein 	struct file_lock *dummy_fl;
8734945c132SAlfred Perlstein 
8744945c132SAlfred Perlstein 	dummy_fl = NULL;
8754945c132SAlfred Perlstein 
8764945c132SAlfred Perlstein 	debuglog("Entering lock_nfslock...\n");
8774945c132SAlfred Perlstein 
8784945c132SAlfred Perlstein 	retval = test_nfslock(fl,&dummy_fl);
8794945c132SAlfred Perlstein 
8804945c132SAlfred Perlstein 	if (retval == NFS_GRANTED || retval == NFS_GRANTED_DUPLICATE) {
8814945c132SAlfred Perlstein 		debuglog("Inserting lock...\n");
8824945c132SAlfred Perlstein 		dump_filelock(fl);
8834945c132SAlfred Perlstein 		LIST_INSERT_HEAD(&nfslocklist_head, fl, nfslocklist);
8844945c132SAlfred Perlstein 	}
8854945c132SAlfred Perlstein 
8864945c132SAlfred Perlstein 	debuglog("Exiting lock_nfslock...\n");
8874945c132SAlfred Perlstein 
888b6dc41baSAlfred Perlstein 	return (retval);
8894945c132SAlfred Perlstein }
8904945c132SAlfred Perlstein 
8914945c132SAlfred Perlstein /*
8924945c132SAlfred Perlstein  * delete_nfslock: delete an NFS lock list entry
8934945c132SAlfred Perlstein  *
8944945c132SAlfred Perlstein  * This routine is used to delete a lock out of the NFS lock list
8954945c132SAlfred Perlstein  * without regard to status, underlying locks, regions or anything else
8964945c132SAlfred Perlstein  *
8977e589b0bSAlfred Perlstein  * Note that this routine *does not deallocate memory* of the lock.
8987e589b0bSAlfred Perlstein  * It just disconnects it from the list.  The lock can then be used
8997e589b0bSAlfred Perlstein  * by other routines without fear of trashing the list.
9004945c132SAlfred Perlstein  */
9014945c132SAlfred Perlstein 
9024945c132SAlfred Perlstein enum nfslock_status
delete_nfslock(struct file_lock * fl)9037e589b0bSAlfred Perlstein delete_nfslock(struct file_lock *fl)
9044945c132SAlfred Perlstein {
905b6dc41baSAlfred Perlstein 
9067e589b0bSAlfred Perlstein 	LIST_REMOVE(fl, nfslocklist);
9077e589b0bSAlfred Perlstein 
908b6dc41baSAlfred Perlstein 	return (NFS_GRANTED);
9094945c132SAlfred Perlstein }
9104945c132SAlfred Perlstein 
9114945c132SAlfred Perlstein enum split_status
split_nfslock(const struct file_lock * exist_lock,const struct file_lock * unlock_lock,struct file_lock ** left_lock,struct file_lock ** right_lock)9122541accbSJohn Baldwin split_nfslock(const struct file_lock *exist_lock,
9132541accbSJohn Baldwin     const struct file_lock *unlock_lock, struct file_lock **left_lock,
9142541accbSJohn Baldwin     struct file_lock **right_lock)
9154945c132SAlfred Perlstein {
9165f07c7b2SAlfred Perlstein 	u_int64_t start1, len1, start2, len2;
9175f07c7b2SAlfred Perlstein 	enum split_status spstatus;
918b6dc41baSAlfred Perlstein 
9195f07c7b2SAlfred Perlstein 	spstatus = region_compare(exist_lock->client.l_offset, exist_lock->client.l_len,
9205f07c7b2SAlfred Perlstein 	    unlock_lock->client.l_offset, unlock_lock->client.l_len,
9215f07c7b2SAlfred Perlstein 	    &start1, &len1, &start2, &len2);
9225f07c7b2SAlfred Perlstein 
9235f07c7b2SAlfred Perlstein 	if ((spstatus & SPL_LOCK1) != 0) {
9243d81d1adSMichael Reifenberger 		*left_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie, exist_lock->addr, exist_lock->client_name);
9255f07c7b2SAlfred Perlstein 		if (*left_lock == NULL) {
9265f07c7b2SAlfred Perlstein 			debuglog("Unable to allocate resource for split 1\n");
9275f07c7b2SAlfred Perlstein 			return SPL_RESERR;
9285f07c7b2SAlfred Perlstein 		}
9295f07c7b2SAlfred Perlstein 
930815ac496SAlfred Perlstein 		fill_file_lock(*left_lock, &exist_lock->filehandle,
9315f07c7b2SAlfred Perlstein 		    exist_lock->client.exclusive, exist_lock->client.svid,
9325f07c7b2SAlfred Perlstein 		    start1, len1,
9333d81d1adSMichael Reifenberger 		    exist_lock->nsm_status,
9345f07c7b2SAlfred Perlstein 		    exist_lock->status, exist_lock->flags, exist_lock->blocking);
9355f07c7b2SAlfred Perlstein 	}
9365f07c7b2SAlfred Perlstein 
9375f07c7b2SAlfred Perlstein 	if ((spstatus & SPL_LOCK2) != 0) {
9383d81d1adSMichael Reifenberger 		*right_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie, exist_lock->addr, exist_lock->client_name);
9395f07c7b2SAlfred Perlstein 		if (*right_lock == NULL) {
9405f07c7b2SAlfred Perlstein 			debuglog("Unable to allocate resource for split 1\n");
9415f07c7b2SAlfred Perlstein 			if (*left_lock != NULL) {
9425f07c7b2SAlfred Perlstein 				deallocate_file_lock(*left_lock);
9435f07c7b2SAlfred Perlstein 			}
9445f07c7b2SAlfred Perlstein 			return SPL_RESERR;
9455f07c7b2SAlfred Perlstein 		}
9465f07c7b2SAlfred Perlstein 
947815ac496SAlfred Perlstein 		fill_file_lock(*right_lock, &exist_lock->filehandle,
9485f07c7b2SAlfred Perlstein 		    exist_lock->client.exclusive, exist_lock->client.svid,
9495f07c7b2SAlfred Perlstein 		    start2, len2,
9503d81d1adSMichael Reifenberger 		    exist_lock->nsm_status,
9515f07c7b2SAlfred Perlstein 		    exist_lock->status, exist_lock->flags, exist_lock->blocking);
9525f07c7b2SAlfred Perlstein 	}
9535f07c7b2SAlfred Perlstein 
9545f07c7b2SAlfred Perlstein 	return spstatus;
9554945c132SAlfred Perlstein }
9564945c132SAlfred Perlstein 
9574945c132SAlfred Perlstein enum nfslock_status
unlock_nfslock(const struct file_lock * fl,struct file_lock ** released_lock,struct file_lock ** left_lock,struct file_lock ** right_lock)9582541accbSJohn Baldwin unlock_nfslock(const struct file_lock *fl, struct file_lock **released_lock,
9592541accbSJohn Baldwin     struct file_lock **left_lock, struct file_lock **right_lock)
9604945c132SAlfred Perlstein {
9614945c132SAlfred Perlstein 	struct file_lock *mfl; /* Matching file lock */
9624945c132SAlfred Perlstein 	enum nfslock_status retval;
9635f07c7b2SAlfred Perlstein 	enum split_status spstatus;
9644945c132SAlfred Perlstein 
9654945c132SAlfred Perlstein 	debuglog("Entering unlock_nfslock\n");
9664945c132SAlfred Perlstein 
9677e589b0bSAlfred Perlstein 	*released_lock = NULL;
9685f07c7b2SAlfred Perlstein 	*left_lock = NULL;
9695f07c7b2SAlfred Perlstein 	*right_lock = NULL;
9707e589b0bSAlfred Perlstein 
9714945c132SAlfred Perlstein 	retval = NFS_DENIED_NOLOCK;
9724945c132SAlfred Perlstein 
9733d81d1adSMichael Reifenberger 	debuglog("Attempting to match lock...\n");
9744945c132SAlfred Perlstein 	mfl = get_lock_matching_unlock(fl);
9754945c132SAlfred Perlstein 
9767e589b0bSAlfred Perlstein 	if (mfl != NULL) {
9775f07c7b2SAlfred Perlstein 		debuglog("Unlock matched.  Querying for split\n");
9785f07c7b2SAlfred Perlstein 
9795f07c7b2SAlfred Perlstein 		spstatus = split_nfslock(mfl, fl, left_lock, right_lock);
9805f07c7b2SAlfred Perlstein 
9815f07c7b2SAlfred Perlstein 		debuglog("Split returned %d %p %p %p %p\n",spstatus,mfl,fl,*left_lock,*right_lock);
9825f07c7b2SAlfred Perlstein 		debuglog("********Split dumps********");
9835f07c7b2SAlfred Perlstein 		dump_filelock(mfl);
9845f07c7b2SAlfred Perlstein 		dump_filelock(fl);
9855f07c7b2SAlfred Perlstein 		dump_filelock(*left_lock);
9865f07c7b2SAlfred Perlstein 		dump_filelock(*right_lock);
9875f07c7b2SAlfred Perlstein 		debuglog("********End Split dumps********");
9885f07c7b2SAlfred Perlstein 
9895f07c7b2SAlfred Perlstein 		if (spstatus == SPL_RESERR) {
9905f07c7b2SAlfred Perlstein 			if (*left_lock != NULL) {
9915f07c7b2SAlfred Perlstein 				deallocate_file_lock(*left_lock);
9925f07c7b2SAlfred Perlstein 				*left_lock = NULL;
9935f07c7b2SAlfred Perlstein 			}
9945f07c7b2SAlfred Perlstein 
9955f07c7b2SAlfred Perlstein 			if (*right_lock != NULL) {
9965f07c7b2SAlfred Perlstein 				deallocate_file_lock(*right_lock);
9975f07c7b2SAlfred Perlstein 				*right_lock = NULL;
9985f07c7b2SAlfred Perlstein 			}
9995f07c7b2SAlfred Perlstein 
10005f07c7b2SAlfred Perlstein 			return NFS_RESERR;
10015f07c7b2SAlfred Perlstein 		}
10025f07c7b2SAlfred Perlstein 
10035f07c7b2SAlfred Perlstein 		/* Insert new locks from split if required */
10045f07c7b2SAlfred Perlstein 		if (*left_lock != NULL) {
10055f07c7b2SAlfred Perlstein 			debuglog("Split left activated\n");
10065f07c7b2SAlfred Perlstein 			LIST_INSERT_HEAD(&nfslocklist_head, *left_lock, nfslocklist);
10075f07c7b2SAlfred Perlstein 		}
10085f07c7b2SAlfred Perlstein 
10095f07c7b2SAlfred Perlstein 		if (*right_lock != NULL) {
10105f07c7b2SAlfred Perlstein 			debuglog("Split right activated\n");
10115f07c7b2SAlfred Perlstein 			LIST_INSERT_HEAD(&nfslocklist_head, *right_lock, nfslocklist);
10125f07c7b2SAlfred Perlstein 		}
10135f07c7b2SAlfred Perlstein 
10147e589b0bSAlfred Perlstein 		/* Unlock the lock since it matches identity */
10154945c132SAlfred Perlstein 		LIST_REMOVE(mfl, nfslocklist);
10167e589b0bSAlfred Perlstein 		*released_lock = mfl;
10174945c132SAlfred Perlstein 		retval = NFS_GRANTED;
10184945c132SAlfred Perlstein 	}
10194945c132SAlfred Perlstein 
10204945c132SAlfred Perlstein 	debuglog("Exiting unlock_nfslock\n");
10214945c132SAlfred Perlstein 
10224945c132SAlfred Perlstein 	return retval;
10234945c132SAlfred Perlstein }
10244945c132SAlfred Perlstein 
10257e589b0bSAlfred Perlstein /*
10267e589b0bSAlfred Perlstein  * Below here are the routines for manipulating the file lock directly
10277e589b0bSAlfred Perlstein  * on the disk hardware itself
10287e589b0bSAlfred Perlstein  */
10297e589b0bSAlfred Perlstein enum hwlock_status
lock_hwlock(struct file_lock * fl)10307e589b0bSAlfred Perlstein lock_hwlock(struct file_lock *fl)
10317e589b0bSAlfred Perlstein {
10327e589b0bSAlfred Perlstein 	struct monfile *imf,*nmf;
10337e589b0bSAlfred Perlstein 	int lflags, flerror;
10347e589b0bSAlfred Perlstein 
10357e589b0bSAlfred Perlstein 	/* Scan to see if filehandle already present */
10367e589b0bSAlfred Perlstein 	LIST_FOREACH(imf, &monfilelist_head, monfilelist) {
10377e589b0bSAlfred Perlstein 		if (bcmp(&fl->filehandle, &imf->filehandle,
10387e589b0bSAlfred Perlstein 			sizeof(fl->filehandle)) == 0) {
10397e589b0bSAlfred Perlstein 			/* imf is the correct filehandle */
10407e589b0bSAlfred Perlstein 			break;
10417e589b0bSAlfred Perlstein 		}
10427e589b0bSAlfred Perlstein 	}
10437e589b0bSAlfred Perlstein 
1044b6dc41baSAlfred Perlstein 	/*
1045b6dc41baSAlfred Perlstein 	 * Filehandle already exists (we control the file)
1046b6dc41baSAlfred Perlstein 	 * *AND* NFS has already cleared the lock for availability
1047b6dc41baSAlfred Perlstein 	 * Grant it and bump the refcount.
1048b6dc41baSAlfred Perlstein 	 */
1049b6dc41baSAlfred Perlstein 	if (imf != NULL) {
1050b6dc41baSAlfred Perlstein 		++(imf->refcount);
1051b6dc41baSAlfred Perlstein 		return (HW_GRANTED);
1052b6dc41baSAlfred Perlstein 	}
1053b6dc41baSAlfred Perlstein 
10547e589b0bSAlfred Perlstein 	/* No filehandle found, create and go */
10557e589b0bSAlfred Perlstein 	nmf = malloc(sizeof(struct monfile));
10567e589b0bSAlfred Perlstein 	if (nmf == NULL) {
10577e589b0bSAlfred Perlstein 		debuglog("hwlock resource allocation failure\n");
1058b6dc41baSAlfred Perlstein 		return (HW_RESERR);
10597e589b0bSAlfred Perlstein 	}
10607e589b0bSAlfred Perlstein 
10617e589b0bSAlfred Perlstein 	/* XXX: Is O_RDWR always the correct mode? */
10627e589b0bSAlfred Perlstein 	nmf->fd = fhopen(&fl->filehandle, O_RDWR);
10637e589b0bSAlfred Perlstein 	if (nmf->fd < 0) {
10647e589b0bSAlfred Perlstein 		debuglog("fhopen failed (from %16s): %32s\n",
10657e589b0bSAlfred Perlstein 		    fl->client_name, strerror(errno));
10667e589b0bSAlfred Perlstein 		free(nmf);
1067b6dc41baSAlfred Perlstein 		switch (errno) {
1068b6dc41baSAlfred Perlstein 		case ESTALE:
1069b6dc41baSAlfred Perlstein 			return (HW_STALEFH);
1070b6dc41baSAlfred Perlstein 		case EROFS:
1071b6dc41baSAlfred Perlstein 			return (HW_READONLY);
1072b6dc41baSAlfred Perlstein 		default:
1073b6dc41baSAlfred Perlstein 			return (HW_RESERR);
1074b6dc41baSAlfred Perlstein 		}
1075b6dc41baSAlfred Perlstein 	}
1076b6dc41baSAlfred Perlstein 
10777e589b0bSAlfred Perlstein 	/* File opened correctly, fill the monitor struct */
10787e589b0bSAlfred Perlstein 	bcopy(&fl->filehandle, &nmf->filehandle, sizeof(fl->filehandle));
10797e589b0bSAlfred Perlstein 	nmf->refcount = 1;
10807e589b0bSAlfred Perlstein 	nmf->exclusive = fl->client.exclusive;
10817e589b0bSAlfred Perlstein 
10827e589b0bSAlfred Perlstein 	lflags = (nmf->exclusive == 1) ?
10837e589b0bSAlfred Perlstein 	    (LOCK_EX | LOCK_NB) : (LOCK_SH | LOCK_NB);
10847e589b0bSAlfred Perlstein 
10857e589b0bSAlfred Perlstein 	flerror = flock(nmf->fd, lflags);
10867e589b0bSAlfred Perlstein 
10877e589b0bSAlfred Perlstein 	if (flerror != 0) {
10887e589b0bSAlfred Perlstein 		debuglog("flock failed (from %16s): %32s\n",
10897e589b0bSAlfred Perlstein 		    fl->client_name, strerror(errno));
10907e589b0bSAlfred Perlstein 		close(nmf->fd);
10917e589b0bSAlfred Perlstein 		free(nmf);
1092b6dc41baSAlfred Perlstein 		switch (errno) {
1093b6dc41baSAlfred Perlstein 		case EAGAIN:
1094b6dc41baSAlfred Perlstein 			return (HW_DENIED);
1095b6dc41baSAlfred Perlstein 		case ESTALE:
1096b6dc41baSAlfred Perlstein 			return (HW_STALEFH);
1097b6dc41baSAlfred Perlstein 		case EROFS:
1098b6dc41baSAlfred Perlstein 			return (HW_READONLY);
1099b6dc41baSAlfred Perlstein 		default:
1100b6dc41baSAlfred Perlstein 			return (HW_RESERR);
1101b6dc41baSAlfred Perlstein 			break;
1102b6dc41baSAlfred Perlstein 		}
1103b6dc41baSAlfred Perlstein 	}
1104b6dc41baSAlfred Perlstein 
11057e589b0bSAlfred Perlstein 	/* File opened and locked */
11067e589b0bSAlfred Perlstein 	LIST_INSERT_HEAD(&monfilelist_head, nmf, monfilelist);
11077e589b0bSAlfred Perlstein 
1108b6dc41baSAlfred Perlstein 	debuglog("flock succeeded (from %16s)\n", fl->client_name);
1109b6dc41baSAlfred Perlstein 	return (HW_GRANTED);
11107e589b0bSAlfred Perlstein }
11117e589b0bSAlfred Perlstein 
11127e589b0bSAlfred Perlstein enum hwlock_status
unlock_hwlock(const struct file_lock * fl)11137e589b0bSAlfred Perlstein unlock_hwlock(const struct file_lock *fl)
11147e589b0bSAlfred Perlstein {
11157e589b0bSAlfred Perlstein 	struct monfile *imf;
11167e589b0bSAlfred Perlstein 
11177e589b0bSAlfred Perlstein 	debuglog("Entering unlock_hwlock\n");
11187e589b0bSAlfred Perlstein 	debuglog("Entering loop interation\n");
11197e589b0bSAlfred Perlstein 
11207e589b0bSAlfred Perlstein 	/* Scan to see if filehandle already present */
11217e589b0bSAlfred Perlstein 	LIST_FOREACH(imf, &monfilelist_head, monfilelist) {
11227e589b0bSAlfred Perlstein 		if (bcmp(&fl->filehandle, &imf->filehandle,
11237e589b0bSAlfred Perlstein 			sizeof(fl->filehandle)) == 0) {
11247e589b0bSAlfred Perlstein 			/* imf is the correct filehandle */
11257e589b0bSAlfred Perlstein 			break;
11267e589b0bSAlfred Perlstein 		}
11277e589b0bSAlfred Perlstein 	}
11287e589b0bSAlfred Perlstein 
11297e589b0bSAlfred Perlstein 	debuglog("Completed iteration.  Proceeding\n");
11307e589b0bSAlfred Perlstein 
11317e589b0bSAlfred Perlstein 	if (imf == NULL) {
11327e589b0bSAlfred Perlstein 		/* No lock found */
1133b6dc41baSAlfred Perlstein 		debuglog("Exiting unlock_hwlock (HW_DENIED_NOLOCK)\n");
1134b6dc41baSAlfred Perlstein 		return (HW_DENIED_NOLOCK);
1135b6dc41baSAlfred Perlstein 	}
1136b6dc41baSAlfred Perlstein 
11377e589b0bSAlfred Perlstein 	/* Lock found */
11387e589b0bSAlfred Perlstein 	--imf->refcount;
11397e589b0bSAlfred Perlstein 
11407e589b0bSAlfred Perlstein 	if (imf->refcount < 0) {
11417e589b0bSAlfred Perlstein 		debuglog("Negative hardware reference count\n");
11427e589b0bSAlfred Perlstein 	}
11437e589b0bSAlfred Perlstein 
11447e589b0bSAlfred Perlstein 	if (imf->refcount <= 0) {
11457e589b0bSAlfred Perlstein 		close(imf->fd);
11467e589b0bSAlfred Perlstein 		LIST_REMOVE(imf, monfilelist);
11477e589b0bSAlfred Perlstein 		free(imf);
11487e589b0bSAlfred Perlstein 	}
1149b6dc41baSAlfred Perlstein 	debuglog("Exiting unlock_hwlock (HW_GRANTED)\n");
1150b6dc41baSAlfred Perlstein 	return (HW_GRANTED);
11517e589b0bSAlfred Perlstein }
11527e589b0bSAlfred Perlstein 
11537e589b0bSAlfred Perlstein enum hwlock_status
test_hwlock(const struct file_lock * fl __unused,struct file_lock ** conflicting_fl __unused)11542541accbSJohn Baldwin test_hwlock(const struct file_lock *fl __unused,
11552541accbSJohn Baldwin     struct file_lock **conflicting_fl __unused)
11567e589b0bSAlfred Perlstein {
1157b6dc41baSAlfred Perlstein 
11587e589b0bSAlfred Perlstein 	/*
11597e589b0bSAlfred Perlstein 	 * XXX: lock tests on hardware are not required until
11607e589b0bSAlfred Perlstein 	 * true partial file testing is done on the underlying file
11617e589b0bSAlfred Perlstein 	 */
1162b6dc41baSAlfred Perlstein 	return (HW_RESERR);
11637e589b0bSAlfred Perlstein }
11647e589b0bSAlfred Perlstein 
11657e589b0bSAlfred Perlstein 
11667e589b0bSAlfred Perlstein 
11677e589b0bSAlfred Perlstein /*
11687e589b0bSAlfred Perlstein  * Below here are routines for manipulating blocked lock requests
11697e589b0bSAlfred Perlstein  * They should only be called from the XXX_partialfilelock routines
11707e589b0bSAlfred Perlstein  * if at all possible
11717e589b0bSAlfred Perlstein  */
11727e589b0bSAlfred Perlstein 
117396f8e17cSMatt Jacob int
duplicate_block(struct file_lock * fl)117496f8e17cSMatt Jacob duplicate_block(struct file_lock *fl)
117596f8e17cSMatt Jacob {
1176091c4c86SUlrich Spörlein 	struct file_lock *ifl;
117796f8e17cSMatt Jacob 	int retval = 0;
117896f8e17cSMatt Jacob 
117996f8e17cSMatt Jacob 	debuglog("Entering duplicate_block");
118096f8e17cSMatt Jacob 
118196f8e17cSMatt Jacob 	/*
118296f8e17cSMatt Jacob 	 * Is this lock request already on the blocking list?
1183fce89853SChristian Brueffer 	 * Consider it a dupe if the file handles, offset, length,
118496f8e17cSMatt Jacob 	 * exclusivity and client match.
118596f8e17cSMatt Jacob 	 */
118696f8e17cSMatt Jacob 	LIST_FOREACH(ifl, &blockedlocklist_head, nfslocklist) {
118796f8e17cSMatt Jacob 		if (!bcmp(&fl->filehandle, &ifl->filehandle,
118896f8e17cSMatt Jacob 			sizeof(fhandle_t)) &&
118996f8e17cSMatt Jacob 		    fl->client.exclusive == ifl->client.exclusive &&
119096f8e17cSMatt Jacob 		    fl->client.l_offset == ifl->client.l_offset &&
119196f8e17cSMatt Jacob 		    fl->client.l_len == ifl->client.l_len &&
119296f8e17cSMatt Jacob 		    same_filelock_identity(fl, ifl)) {
119396f8e17cSMatt Jacob 			retval = 1;
119496f8e17cSMatt Jacob 			break;
119596f8e17cSMatt Jacob 		}
119696f8e17cSMatt Jacob 	}
119796f8e17cSMatt Jacob 
119896f8e17cSMatt Jacob 	debuglog("Exiting duplicate_block: %s\n", retval ? "already blocked"
119996f8e17cSMatt Jacob 	    : "not already blocked");
120096f8e17cSMatt Jacob 	return retval;
120196f8e17cSMatt Jacob }
120296f8e17cSMatt Jacob 
12037e589b0bSAlfred Perlstein void
add_blockingfilelock(struct file_lock * fl)12047e589b0bSAlfred Perlstein add_blockingfilelock(struct file_lock *fl)
12057e589b0bSAlfred Perlstein {
12067e589b0bSAlfred Perlstein 	debuglog("Entering add_blockingfilelock\n");
12077e589b0bSAlfred Perlstein 
12087e589b0bSAlfred Perlstein 	/*
120996f8e17cSMatt Jacob 	 * A blocking lock request _should_ never be duplicated as a client
121096f8e17cSMatt Jacob 	 * that is already blocked shouldn't be able to request another
121196f8e17cSMatt Jacob 	 * lock. Alas, there are some buggy clients that do request the same
121296f8e17cSMatt Jacob 	 * lock repeatedly. Make sure only unique locks are on the blocked
121396f8e17cSMatt Jacob 	 * lock list.
121496f8e17cSMatt Jacob 	 */
121596f8e17cSMatt Jacob 	if (duplicate_block(fl)) {
121696f8e17cSMatt Jacob 		debuglog("Exiting add_blockingfilelock: already blocked\n");
121796f8e17cSMatt Jacob 		return;
121896f8e17cSMatt Jacob 	}
121996f8e17cSMatt Jacob 
122096f8e17cSMatt Jacob 	/*
12217e589b0bSAlfred Perlstein 	 * Clear the blocking flag so that it can be reused without
12227e589b0bSAlfred Perlstein 	 * adding it to the blocking queue a second time
12237e589b0bSAlfred Perlstein 	 */
12247e589b0bSAlfred Perlstein 
12257e589b0bSAlfred Perlstein 	fl->blocking = 0;
12267e589b0bSAlfred Perlstein 	LIST_INSERT_HEAD(&blockedlocklist_head, fl, nfslocklist);
12277e589b0bSAlfred Perlstein 
122896f8e17cSMatt Jacob 	debuglog("Exiting add_blockingfilelock: added blocked lock\n");
12297e589b0bSAlfred Perlstein }
12307e589b0bSAlfred Perlstein 
12317e589b0bSAlfred Perlstein void
remove_blockingfilelock(struct file_lock * fl)12327e589b0bSAlfred Perlstein remove_blockingfilelock(struct file_lock *fl)
12337e589b0bSAlfred Perlstein {
1234b6dc41baSAlfred Perlstein 
12357e589b0bSAlfred Perlstein 	debuglog("Entering remove_blockingfilelock\n");
12367e589b0bSAlfred Perlstein 
12377e589b0bSAlfred Perlstein 	LIST_REMOVE(fl, nfslocklist);
12387e589b0bSAlfred Perlstein 
12397e589b0bSAlfred Perlstein 	debuglog("Exiting remove_blockingfilelock\n");
12407e589b0bSAlfred Perlstein }
12417e589b0bSAlfred Perlstein 
12427e589b0bSAlfred Perlstein void
clear_blockingfilelock(const char * hostname)1243a4a8a04dSAlfred Perlstein clear_blockingfilelock(const char *hostname)
1244a4a8a04dSAlfred Perlstein {
1245a4a8a04dSAlfred Perlstein 	struct file_lock *ifl,*nfl;
1246a4a8a04dSAlfred Perlstein 
1247a4a8a04dSAlfred Perlstein 	/*
1248a4a8a04dSAlfred Perlstein 	 * Normally, LIST_FOREACH is called for, but since
1249a4a8a04dSAlfred Perlstein 	 * the current element *is* the iterator, deleting it
1250a4a8a04dSAlfred Perlstein 	 * would mess up the iteration.  Thus, a next element
1251a4a8a04dSAlfred Perlstein 	 * must be used explicitly
1252a4a8a04dSAlfred Perlstein 	 */
1253a4a8a04dSAlfred Perlstein 
1254a4a8a04dSAlfred Perlstein 	ifl = LIST_FIRST(&blockedlocklist_head);
1255a4a8a04dSAlfred Perlstein 
1256a4a8a04dSAlfred Perlstein 	while (ifl != NULL) {
1257a4a8a04dSAlfred Perlstein 		nfl = LIST_NEXT(ifl, nfslocklist);
1258a4a8a04dSAlfred Perlstein 
1259a4a8a04dSAlfred Perlstein 		if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) {
1260a4a8a04dSAlfred Perlstein 			remove_blockingfilelock(ifl);
1261a4a8a04dSAlfred Perlstein 			deallocate_file_lock(ifl);
1262a4a8a04dSAlfred Perlstein 		}
1263a4a8a04dSAlfred Perlstein 
1264a4a8a04dSAlfred Perlstein 		ifl = nfl;
1265a4a8a04dSAlfred Perlstein 	}
1266a4a8a04dSAlfred Perlstein }
1267a4a8a04dSAlfred Perlstein 
1268a4a8a04dSAlfred Perlstein void
retry_blockingfilelocklist(void)12697e589b0bSAlfred Perlstein retry_blockingfilelocklist(void)
12707e589b0bSAlfred Perlstein {
12717e589b0bSAlfred Perlstein 	/* Retry all locks in the blocked list */
12723d81d1adSMichael Reifenberger 	struct file_lock *ifl, *nfl; /* Iterator */
12737e589b0bSAlfred Perlstein 	enum partialfilelock_status pflstatus;
12747e589b0bSAlfred Perlstein 
12757e589b0bSAlfred Perlstein 	debuglog("Entering retry_blockingfilelocklist\n");
12767e589b0bSAlfred Perlstein 
12773d81d1adSMichael Reifenberger 	LIST_FOREACH_SAFE(ifl, &blockedlocklist_head, nfslocklist, nfl) {
12787e589b0bSAlfred Perlstein 		debuglog("Iterator choice %p\n",ifl);
12797e589b0bSAlfred Perlstein 		debuglog("Next iterator choice %p\n",nfl);
12807e589b0bSAlfred Perlstein 
12817e589b0bSAlfred Perlstein 		/*
12827e589b0bSAlfred Perlstein 		 * SUBTLE BUG: The file_lock must be removed from the
12837e589b0bSAlfred Perlstein 		 * old list so that it's list pointers get disconnected
12847e589b0bSAlfred Perlstein 		 * before being allowed to participate in the new list
12857e589b0bSAlfred Perlstein 		 * which will automatically add it in if necessary.
12867e589b0bSAlfred Perlstein 		 */
12877e589b0bSAlfred Perlstein 
12887e589b0bSAlfred Perlstein 		LIST_REMOVE(ifl, nfslocklist);
12897e589b0bSAlfred Perlstein 		pflstatus = lock_partialfilelock(ifl);
12907e589b0bSAlfred Perlstein 
12917e589b0bSAlfred Perlstein 		if (pflstatus == PFL_GRANTED || pflstatus == PFL_GRANTED_DUPLICATE) {
12927e589b0bSAlfred Perlstein 			debuglog("Granted blocked lock\n");
12937e589b0bSAlfred Perlstein 			/* lock granted and is now being used */
12947e589b0bSAlfred Perlstein 			send_granted(ifl,0);
12957e589b0bSAlfred Perlstein 		} else {
12963d81d1adSMichael Reifenberger 			/* Reinsert lock back into blocked list */
12977e589b0bSAlfred Perlstein 			debuglog("Replacing blocked lock\n");
12984b4ec9b8SAlfred Perlstein 			LIST_INSERT_HEAD(&blockedlocklist_head, ifl, nfslocklist);
12997e589b0bSAlfred Perlstein 		}
13007e589b0bSAlfred Perlstein 	}
13017e589b0bSAlfred Perlstein 
13027e589b0bSAlfred Perlstein 	debuglog("Exiting retry_blockingfilelocklist\n");
13037e589b0bSAlfred Perlstein }
13044945c132SAlfred Perlstein 
13054945c132SAlfred Perlstein /*
13064945c132SAlfred Perlstein  * Below here are routines associated with manipulating all
13074945c132SAlfred Perlstein  * aspects of the partial file locking system (list, hardware, etc.)
13084945c132SAlfred Perlstein  */
13094945c132SAlfred Perlstein 
13104945c132SAlfred Perlstein /*
1311a4a8a04dSAlfred Perlstein  * Please note that lock monitoring must be done at this level which
1312a4a8a04dSAlfred Perlstein  * keeps track of *individual* lock requests on lock and unlock
1313a4a8a04dSAlfred Perlstein  *
1314a4a8a04dSAlfred Perlstein  * XXX: Split unlocking is going to make the unlock code miserable
1315a4a8a04dSAlfred Perlstein  */
1316a4a8a04dSAlfred Perlstein 
1317a4a8a04dSAlfred Perlstein /*
13184945c132SAlfred Perlstein  * lock_partialfilelock:
13194945c132SAlfred Perlstein  *
13204945c132SAlfred Perlstein  * Argument fl gets modified as its list housekeeping entries get modified
13214945c132SAlfred Perlstein  * upon insertion into the NFS lock list
13224945c132SAlfred Perlstein  *
13234945c132SAlfred Perlstein  * This routine makes several assumptions:
1324a4a8a04dSAlfred Perlstein  * 1) It (will) pass locks through to flock to lock the entire underlying file
1325a4a8a04dSAlfred Perlstein  *     and then parcel out NFS locks if it gets control of the file.
1326a4a8a04dSAlfred Perlstein  *         This matches the old rpc.lockd file semantics (except where it
1327a4a8a04dSAlfred Perlstein  *         is now more correct).  It is the safe solution, but will cause
1328a4a8a04dSAlfred Perlstein  *         overly restrictive blocking if someone is trying to use the
1329a4a8a04dSAlfred Perlstein  *         underlying files without using NFS.  This appears to be an
1330a4a8a04dSAlfred Perlstein  *         acceptable tradeoff since most people use standalone NFS servers.
1331a4a8a04dSAlfred Perlstein  * XXX: The right solution is probably kevent combined with fcntl
13324945c132SAlfred Perlstein  *
1333a4a8a04dSAlfred Perlstein  *    2) Nothing modifies the lock lists between testing and granting
1334a4a8a04dSAlfred Perlstein  *           I have no idea whether this is a useful assumption or not
13354945c132SAlfred Perlstein  */
13364945c132SAlfred Perlstein 
13374945c132SAlfred Perlstein enum partialfilelock_status
lock_partialfilelock(struct file_lock * fl)13384945c132SAlfred Perlstein lock_partialfilelock(struct file_lock *fl)
13394945c132SAlfred Perlstein {
13404945c132SAlfred Perlstein 	enum partialfilelock_status retval;
13414945c132SAlfred Perlstein 	enum nfslock_status lnlstatus;
13427e589b0bSAlfred Perlstein 	enum hwlock_status hwstatus;
13434945c132SAlfred Perlstein 
13444945c132SAlfred Perlstein 	debuglog("Entering lock_partialfilelock\n");
13454945c132SAlfred Perlstein 
13464945c132SAlfred Perlstein 	retval = PFL_DENIED;
13474945c132SAlfred Perlstein 
13484945c132SAlfred Perlstein 	/*
13494945c132SAlfred Perlstein 	 * Execute the NFS lock first, if possible, as it is significantly
13504945c132SAlfred Perlstein 	 * easier and less expensive to undo than the filesystem lock
13514945c132SAlfred Perlstein 	 */
13524945c132SAlfred Perlstein 
13534945c132SAlfred Perlstein 	lnlstatus = lock_nfslock(fl);
13544945c132SAlfred Perlstein 
13557e589b0bSAlfred Perlstein 	switch (lnlstatus) {
13567e589b0bSAlfred Perlstein 	case NFS_GRANTED:
13577e589b0bSAlfred Perlstein 	case NFS_GRANTED_DUPLICATE:
13587e589b0bSAlfred Perlstein 		/*
13597e589b0bSAlfred Perlstein 		 * At this point, the NFS lock is allocated and active.
13607e589b0bSAlfred Perlstein 		 * Remember to clean it up if the hardware lock fails
13617e589b0bSAlfred Perlstein 		 */
13627e589b0bSAlfred Perlstein 		hwstatus = lock_hwlock(fl);
13637e589b0bSAlfred Perlstein 
13647e589b0bSAlfred Perlstein 		switch (hwstatus) {
13657e589b0bSAlfred Perlstein 		case HW_GRANTED:
13667e589b0bSAlfred Perlstein 		case HW_GRANTED_DUPLICATE:
13677e589b0bSAlfred Perlstein 			debuglog("HW GRANTED\n");
13687e589b0bSAlfred Perlstein 			/*
13697e589b0bSAlfred Perlstein 			 * XXX: Fixme: Check hwstatus for duplicate when
13707e589b0bSAlfred Perlstein 			 * true partial file locking and accounting is
137184292608SMatt Jacob 			 * done on the hardware.
13727e589b0bSAlfred Perlstein 			 */
13737e589b0bSAlfred Perlstein 			if (lnlstatus == NFS_GRANTED_DUPLICATE) {
13747e589b0bSAlfred Perlstein 				retval = PFL_GRANTED_DUPLICATE;
13754945c132SAlfred Perlstein 			} else {
13767e589b0bSAlfred Perlstein 				retval = PFL_GRANTED;
13774945c132SAlfred Perlstein 			}
1378a4a8a04dSAlfred Perlstein 			monitor_lock_host(fl->client_name);
13797e589b0bSAlfred Perlstein 			break;
13807e589b0bSAlfred Perlstein 		case HW_RESERR:
13817e589b0bSAlfred Perlstein 			debuglog("HW RESERR\n");
13827e589b0bSAlfred Perlstein 			retval = PFL_HWRESERR;
13837e589b0bSAlfred Perlstein 			break;
13847e589b0bSAlfred Perlstein 		case HW_DENIED:
13857e589b0bSAlfred Perlstein 			debuglog("HW DENIED\n");
13867e589b0bSAlfred Perlstein 			retval = PFL_HWDENIED;
13877e589b0bSAlfred Perlstein 			break;
13887e589b0bSAlfred Perlstein 		default:
13897e589b0bSAlfred Perlstein 			debuglog("Unmatched hwstatus %d\n",hwstatus);
13907e589b0bSAlfred Perlstein 			break;
13917e589b0bSAlfred Perlstein 		}
13927e589b0bSAlfred Perlstein 
13937e589b0bSAlfred Perlstein 		if (retval != PFL_GRANTED &&
13947e589b0bSAlfred Perlstein 		    retval != PFL_GRANTED_DUPLICATE) {
13957e589b0bSAlfred Perlstein 			/* Clean up the NFS lock */
13967e589b0bSAlfred Perlstein 			debuglog("Deleting trial NFS lock\n");
13977e589b0bSAlfred Perlstein 			delete_nfslock(fl);
13987e589b0bSAlfred Perlstein 		}
13997e589b0bSAlfred Perlstein 		break;
14007e589b0bSAlfred Perlstein 	case NFS_DENIED:
14014945c132SAlfred Perlstein 		retval = PFL_NFSDENIED;
14027e589b0bSAlfred Perlstein 		break;
14037e589b0bSAlfred Perlstein 	case NFS_RESERR:
14047e589b0bSAlfred Perlstein 		retval = PFL_NFSRESERR;
140547c5f8f2SEnji Cooper 		break;
14067e589b0bSAlfred Perlstein 	default:
14077e589b0bSAlfred Perlstein 		debuglog("Unmatched lnlstatus %d\n");
14087e589b0bSAlfred Perlstein 		retval = PFL_NFSDENIED_NOLOCK;
14097e589b0bSAlfred Perlstein 		break;
14107e589b0bSAlfred Perlstein 	}
14117e589b0bSAlfred Perlstein 
14127e589b0bSAlfred Perlstein 	/*
14137e589b0bSAlfred Perlstein 	 * By the time fl reaches here, it is completely free again on
14147e589b0bSAlfred Perlstein 	 * failure.  The NFS lock done before attempting the
14157e589b0bSAlfred Perlstein 	 * hardware lock has been backed out
14167e589b0bSAlfred Perlstein 	 */
14177e589b0bSAlfred Perlstein 
14187e589b0bSAlfred Perlstein 	if (retval == PFL_NFSDENIED || retval == PFL_HWDENIED) {
14197e589b0bSAlfred Perlstein 		/* Once last chance to check the lock */
14207e589b0bSAlfred Perlstein 		if (fl->blocking == 1) {
142186eb7cbaSRobert Watson 			if (retval == PFL_NFSDENIED) {
14227e589b0bSAlfred Perlstein 				/* Queue the lock */
14237e589b0bSAlfred Perlstein 				debuglog("BLOCKING LOCK RECEIVED\n");
142486eb7cbaSRobert Watson 				retval = PFL_NFSBLOCKED;
14257e589b0bSAlfred Perlstein 				add_blockingfilelock(fl);
14264945c132SAlfred Perlstein 				dump_filelock(fl);
14277e589b0bSAlfred Perlstein 			} else {
142886eb7cbaSRobert Watson 				/* retval is okay as PFL_HWDENIED */
142986eb7cbaSRobert Watson 				debuglog("BLOCKING LOCK DENIED IN HARDWARE\n");
143086eb7cbaSRobert Watson 				dump_filelock(fl);
143186eb7cbaSRobert Watson 			}
143286eb7cbaSRobert Watson 		} else {
14337e589b0bSAlfred Perlstein 			/* Leave retval alone, it's already correct */
14347e589b0bSAlfred Perlstein 			debuglog("Lock denied.  Non-blocking failure\n");
14357e589b0bSAlfred Perlstein 			dump_filelock(fl);
14367e589b0bSAlfred Perlstein 		}
14374945c132SAlfred Perlstein 	}
14384945c132SAlfred Perlstein 
14394945c132SAlfred Perlstein 	debuglog("Exiting lock_partialfilelock\n");
14404945c132SAlfred Perlstein 
14414945c132SAlfred Perlstein 	return retval;
14424945c132SAlfred Perlstein }
14434945c132SAlfred Perlstein 
14444945c132SAlfred Perlstein /*
14454945c132SAlfred Perlstein  * unlock_partialfilelock:
1446a4a8a04dSAlfred Perlstein  *
1447a4a8a04dSAlfred Perlstein  * Given a file_lock, unlock all locks which match.
1448a4a8a04dSAlfred Perlstein  *
1449a4a8a04dSAlfred Perlstein  * Note that a given lock might have to unlock ITSELF!  See
1450a4a8a04dSAlfred Perlstein  * clear_partialfilelock for example.
14514945c132SAlfred Perlstein  */
14524945c132SAlfred Perlstein 
14534945c132SAlfred Perlstein enum partialfilelock_status
unlock_partialfilelock(const struct file_lock * fl)14544945c132SAlfred Perlstein unlock_partialfilelock(const struct file_lock *fl)
14554945c132SAlfred Perlstein {
1456a4a8a04dSAlfred Perlstein 	struct file_lock *lfl,*rfl,*releasedfl,*selffl;
14574945c132SAlfred Perlstein 	enum partialfilelock_status retval;
14587e589b0bSAlfred Perlstein 	enum nfslock_status unlstatus;
14595f07c7b2SAlfred Perlstein 	enum hwlock_status unlhwstatus, lhwstatus;
14604945c132SAlfred Perlstein 
14614945c132SAlfred Perlstein 	debuglog("Entering unlock_partialfilelock\n");
14624945c132SAlfred Perlstein 
1463a4a8a04dSAlfred Perlstein 	selffl = NULL;
14645f07c7b2SAlfred Perlstein 	lfl = NULL;
14655f07c7b2SAlfred Perlstein 	rfl = NULL;
14667e589b0bSAlfred Perlstein 	releasedfl = NULL;
14674945c132SAlfred Perlstein 	retval = PFL_DENIED;
14684945c132SAlfred Perlstein 
14697e589b0bSAlfred Perlstein 	/*
14705f07c7b2SAlfred Perlstein 	 * There are significant overlap and atomicity issues
14715f07c7b2SAlfred Perlstein 	 * with partially releasing a lock.  For example, releasing
14725f07c7b2SAlfred Perlstein 	 * part of an NFS shared lock does *not* always release the
14735f07c7b2SAlfred Perlstein 	 * corresponding part of the file since there is only one
14745f07c7b2SAlfred Perlstein 	 * rpc.lockd UID but multiple users could be requesting it
14755f07c7b2SAlfred Perlstein 	 * from NFS.  Also, an unlock request should never allow
14765f07c7b2SAlfred Perlstein 	 * another process to gain a lock on the remaining parts.
14775f07c7b2SAlfred Perlstein 	 * ie. Always apply the new locks before releasing the
14785f07c7b2SAlfred Perlstein 	 * old one
14795f07c7b2SAlfred Perlstein 	 */
14805f07c7b2SAlfred Perlstein 
14815f07c7b2SAlfred Perlstein 	/*
14827e589b0bSAlfred Perlstein 	 * Loop is required since multiple little locks
14837e589b0bSAlfred Perlstein 	 * can be allocated and then deallocated with one
14847e589b0bSAlfred Perlstein 	 * big unlock.
14857e589b0bSAlfred Perlstein 	 *
14867e589b0bSAlfred Perlstein 	 * The loop is required to be here so that the nfs &
14877e589b0bSAlfred Perlstein 	 * hw subsystems do not need to communicate with one
14887e589b0bSAlfred Perlstein 	 * one another
14897e589b0bSAlfred Perlstein 	 */
14904945c132SAlfred Perlstein 
14917e589b0bSAlfred Perlstein 	do {
14927e589b0bSAlfred Perlstein 		debuglog("Value of releasedfl: %p\n",releasedfl);
14935f07c7b2SAlfred Perlstein 		/* lfl&rfl are created *AND* placed into the NFS lock list if required */
14945f07c7b2SAlfred Perlstein 		unlstatus = unlock_nfslock(fl, &releasedfl, &lfl, &rfl);
14957e589b0bSAlfred Perlstein 		debuglog("Value of releasedfl: %p\n",releasedfl);
14967e589b0bSAlfred Perlstein 
14975f07c7b2SAlfred Perlstein 
14985f07c7b2SAlfred Perlstein 		/* XXX: This is grungy.  It should be refactored to be cleaner */
14995f07c7b2SAlfred Perlstein 		if (lfl != NULL) {
15005f07c7b2SAlfred Perlstein 			lhwstatus = lock_hwlock(lfl);
15015f07c7b2SAlfred Perlstein 			if (lhwstatus != HW_GRANTED &&
15025f07c7b2SAlfred Perlstein 			    lhwstatus != HW_GRANTED_DUPLICATE) {
15035f07c7b2SAlfred Perlstein 				debuglog("HW duplicate lock failure for left split\n");
15045f07c7b2SAlfred Perlstein 			}
15055f07c7b2SAlfred Perlstein 			monitor_lock_host(lfl->client_name);
15065f07c7b2SAlfred Perlstein 		}
15075f07c7b2SAlfred Perlstein 
15085f07c7b2SAlfred Perlstein 		if (rfl != NULL) {
15095f07c7b2SAlfred Perlstein 			lhwstatus = lock_hwlock(rfl);
15105f07c7b2SAlfred Perlstein 			if (lhwstatus != HW_GRANTED &&
15115f07c7b2SAlfred Perlstein 			    lhwstatus != HW_GRANTED_DUPLICATE) {
15125f07c7b2SAlfred Perlstein 				debuglog("HW duplicate lock failure for right split\n");
15135f07c7b2SAlfred Perlstein 			}
15145f07c7b2SAlfred Perlstein 			monitor_lock_host(rfl->client_name);
15155f07c7b2SAlfred Perlstein 		}
15165f07c7b2SAlfred Perlstein 
15177e589b0bSAlfred Perlstein 		switch (unlstatus) {
15187e589b0bSAlfred Perlstein 		case NFS_GRANTED:
15197e589b0bSAlfred Perlstein 			/* Attempt to unlock on the hardware */
15207e589b0bSAlfred Perlstein 			debuglog("NFS unlock granted.  Attempting hardware unlock\n");
15217e589b0bSAlfred Perlstein 
15225f07c7b2SAlfred Perlstein 			/* This call *MUST NOT* unlock the two newly allocated locks */
15237e589b0bSAlfred Perlstein 			unlhwstatus = unlock_hwlock(fl);
15247e589b0bSAlfred Perlstein 			debuglog("HW unlock returned with code %d\n",unlhwstatus);
15257e589b0bSAlfred Perlstein 
15267e589b0bSAlfred Perlstein 			switch (unlhwstatus) {
15277e589b0bSAlfred Perlstein 			case HW_GRANTED:
15287e589b0bSAlfred Perlstein 				debuglog("HW unlock granted\n");
1529a4a8a04dSAlfred Perlstein 				unmonitor_lock_host(releasedfl->client_name);
15304945c132SAlfred Perlstein 				retval = PFL_GRANTED;
15317e589b0bSAlfred Perlstein 				break;
15327e589b0bSAlfred Perlstein 			case HW_DENIED_NOLOCK:
15337e589b0bSAlfred Perlstein 				/* Huh?!?!  This shouldn't happen */
15347e589b0bSAlfred Perlstein 				debuglog("HW unlock denied no lock\n");
15357e589b0bSAlfred Perlstein 				retval = PFL_HWRESERR;
15367e589b0bSAlfred Perlstein 				/* Break out of do-while */
15377e589b0bSAlfred Perlstein 				unlstatus = NFS_RESERR;
15387e589b0bSAlfred Perlstein 				break;
15397e589b0bSAlfred Perlstein 			default:
15407e589b0bSAlfred Perlstein 				debuglog("HW unlock failed\n");
15417e589b0bSAlfred Perlstein 				retval = PFL_HWRESERR;
15427e589b0bSAlfred Perlstein 				/* Break out of do-while */
15437e589b0bSAlfred Perlstein 				unlstatus = NFS_RESERR;
15447e589b0bSAlfred Perlstein 				break;
15454945c132SAlfred Perlstein 			}
15464945c132SAlfred Perlstein 
15477e589b0bSAlfred Perlstein 			debuglog("Exiting with status retval: %d\n",retval);
15487e589b0bSAlfred Perlstein 
15497e589b0bSAlfred Perlstein 			retry_blockingfilelocklist();
15507e589b0bSAlfred Perlstein 			break;
15517e589b0bSAlfred Perlstein 		case NFS_DENIED_NOLOCK:
15527e589b0bSAlfred Perlstein 			retval = PFL_GRANTED;
15537e589b0bSAlfred Perlstein 			debuglog("All locks cleaned out\n");
15547e589b0bSAlfred Perlstein 			break;
15557e589b0bSAlfred Perlstein 		default:
15567e589b0bSAlfred Perlstein 			retval = PFL_NFSRESERR;
15577e589b0bSAlfred Perlstein 			debuglog("NFS unlock failure\n");
15587e589b0bSAlfred Perlstein 			dump_filelock(fl);
15597e589b0bSAlfred Perlstein 			break;
15607e589b0bSAlfred Perlstein 		}
1561a4a8a04dSAlfred Perlstein 
1562a4a8a04dSAlfred Perlstein 		if (releasedfl != NULL) {
1563a4a8a04dSAlfred Perlstein 			if (fl == releasedfl) {
1564a4a8a04dSAlfred Perlstein 				/*
1565a4a8a04dSAlfred Perlstein 				 * XXX: YECHHH!!! Attempt to unlock self succeeded
1566a4a8a04dSAlfred Perlstein 				 * but we can't deallocate the space yet.  This is what
1567a4a8a04dSAlfred Perlstein 				 * happens when you don't write malloc and free together
1568a4a8a04dSAlfred Perlstein 				 */
1569a4a8a04dSAlfred Perlstein 				debuglog("Attempt to unlock self\n");
1570a4a8a04dSAlfred Perlstein 				selffl = releasedfl;
1571a4a8a04dSAlfred Perlstein 			} else {
15725f07c7b2SAlfred Perlstein 				/*
15735f07c7b2SAlfred Perlstein 				 * XXX: this deallocation *still* needs to migrate closer
15745f07c7b2SAlfred Perlstein 				 * to the allocation code way up in get_lock or the allocation
15755f07c7b2SAlfred Perlstein 				 * code needs to migrate down (violation of "When you write
15765f07c7b2SAlfred Perlstein 				 * malloc you must write free")
15775f07c7b2SAlfred Perlstein 				 */
15785f07c7b2SAlfred Perlstein 
1579a4a8a04dSAlfred Perlstein 				deallocate_file_lock(releasedfl);
1580cec77ff4SDon Lewis 				releasedfl = NULL;
1581a4a8a04dSAlfred Perlstein 			}
1582a4a8a04dSAlfred Perlstein 		}
1583a4a8a04dSAlfred Perlstein 
15847e589b0bSAlfred Perlstein 	} while (unlstatus == NFS_GRANTED);
15857e589b0bSAlfred Perlstein 
1586a4a8a04dSAlfred Perlstein 	if (selffl != NULL) {
1587a4a8a04dSAlfred Perlstein 		/*
1588a4a8a04dSAlfred Perlstein 		 * This statement wipes out the incoming file lock (fl)
1589a4a8a04dSAlfred Perlstein 		 * in spite of the fact that it is declared const
1590a4a8a04dSAlfred Perlstein 		 */
1591a4a8a04dSAlfred Perlstein 		debuglog("WARNING!  Destroying incoming lock pointer\n");
1592a4a8a04dSAlfred Perlstein 		deallocate_file_lock(selffl);
1593a4a8a04dSAlfred Perlstein 	}
1594a4a8a04dSAlfred Perlstein 
15954945c132SAlfred Perlstein 	debuglog("Exiting unlock_partialfilelock\n");
15964945c132SAlfred Perlstein 
15974945c132SAlfred Perlstein 	return retval;
15984945c132SAlfred Perlstein }
15994945c132SAlfred Perlstein 
16004945c132SAlfred Perlstein /*
1601a4a8a04dSAlfred Perlstein  * clear_partialfilelock
1602a4a8a04dSAlfred Perlstein  *
1603a4a8a04dSAlfred Perlstein  * Normally called in response to statd state number change.
1604a4a8a04dSAlfred Perlstein  * Wipe out all locks held by a host.  As a bonus, the act of
1605a4a8a04dSAlfred Perlstein  * doing so should automatically clear their statd entries and
1606a4a8a04dSAlfred Perlstein  * unmonitor the host.
1607a4a8a04dSAlfred Perlstein  */
1608a4a8a04dSAlfred Perlstein 
1609a4a8a04dSAlfred Perlstein void
clear_partialfilelock(const char * hostname)1610a4a8a04dSAlfred Perlstein clear_partialfilelock(const char *hostname)
1611a4a8a04dSAlfred Perlstein {
1612a4a8a04dSAlfred Perlstein 	struct file_lock *ifl, *nfl;
1613a4a8a04dSAlfred Perlstein 
1614a4a8a04dSAlfred Perlstein 	/* Clear blocking file lock list */
1615a4a8a04dSAlfred Perlstein 	clear_blockingfilelock(hostname);
1616a4a8a04dSAlfred Perlstein 
1617a4a8a04dSAlfred Perlstein 	/* do all required unlocks */
1618a4a8a04dSAlfred Perlstein 	/* Note that unlock can smash the current pointer to a lock */
1619a4a8a04dSAlfred Perlstein 
1620a4a8a04dSAlfred Perlstein 	/*
1621a4a8a04dSAlfred Perlstein 	 * Normally, LIST_FOREACH is called for, but since
1622a4a8a04dSAlfred Perlstein 	 * the current element *is* the iterator, deleting it
1623a4a8a04dSAlfred Perlstein 	 * would mess up the iteration.  Thus, a next element
1624a4a8a04dSAlfred Perlstein 	 * must be used explicitly
1625a4a8a04dSAlfred Perlstein 	 */
1626a4a8a04dSAlfred Perlstein 
1627a4a8a04dSAlfred Perlstein 	ifl = LIST_FIRST(&nfslocklist_head);
1628a4a8a04dSAlfred Perlstein 
1629a4a8a04dSAlfred Perlstein 	while (ifl != NULL) {
1630a4a8a04dSAlfred Perlstein 		nfl = LIST_NEXT(ifl, nfslocklist);
1631a4a8a04dSAlfred Perlstein 
1632a4a8a04dSAlfred Perlstein 		if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) {
1633a4a8a04dSAlfred Perlstein 			/* Unlock destroys ifl out from underneath */
1634a4a8a04dSAlfred Perlstein 			unlock_partialfilelock(ifl);
1635a4a8a04dSAlfred Perlstein 			/* ifl is NO LONGER VALID AT THIS POINT */
1636a4a8a04dSAlfred Perlstein 		}
1637a4a8a04dSAlfred Perlstein 		ifl = nfl;
1638a4a8a04dSAlfred Perlstein 	}
1639a4a8a04dSAlfred Perlstein }
1640a4a8a04dSAlfred Perlstein 
1641a4a8a04dSAlfred Perlstein /*
16424945c132SAlfred Perlstein  * test_partialfilelock:
16434945c132SAlfred Perlstein  */
16444945c132SAlfred Perlstein enum partialfilelock_status
test_partialfilelock(const struct file_lock * fl,struct file_lock ** conflicting_fl)16454945c132SAlfred Perlstein test_partialfilelock(const struct file_lock *fl,
16464945c132SAlfred Perlstein     struct file_lock **conflicting_fl)
16474945c132SAlfred Perlstein {
16484945c132SAlfred Perlstein 	enum partialfilelock_status retval;
16494945c132SAlfred Perlstein 	enum nfslock_status teststatus;
16504945c132SAlfred Perlstein 
16514945c132SAlfred Perlstein 	debuglog("Entering testpartialfilelock...\n");
16524945c132SAlfred Perlstein 
16534945c132SAlfred Perlstein 	retval = PFL_DENIED;
16544945c132SAlfred Perlstein 
16554945c132SAlfred Perlstein 	teststatus = test_nfslock(fl, conflicting_fl);
16564945c132SAlfred Perlstein 	debuglog("test_partialfilelock: teststatus %d\n",teststatus);
16574945c132SAlfred Perlstein 
16584945c132SAlfred Perlstein 	if (teststatus == NFS_GRANTED || teststatus == NFS_GRANTED_DUPLICATE) {
16594945c132SAlfred Perlstein 		/* XXX: Add the underlying filesystem locking code */
16604945c132SAlfred Perlstein 		retval = (teststatus == NFS_GRANTED) ?
16614945c132SAlfred Perlstein 		    PFL_GRANTED : PFL_GRANTED_DUPLICATE;
16624945c132SAlfred Perlstein 		debuglog("Dumping locks...\n");
16634945c132SAlfred Perlstein 		dump_filelock(fl);
16644945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
16654945c132SAlfred Perlstein 		debuglog("Done dumping locks...\n");
16664945c132SAlfred Perlstein 	} else {
16674945c132SAlfred Perlstein 		retval = PFL_NFSDENIED;
16684945c132SAlfred Perlstein 		debuglog("NFS test denied.\n");
16694945c132SAlfred Perlstein 		dump_filelock(fl);
16704945c132SAlfred Perlstein 		debuglog("Conflicting.\n");
16714945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
16724945c132SAlfred Perlstein 	}
16734945c132SAlfred Perlstein 
16744945c132SAlfred Perlstein 	debuglog("Exiting testpartialfilelock...\n");
16754945c132SAlfred Perlstein 
16764945c132SAlfred Perlstein 	return retval;
16774945c132SAlfred Perlstein }
16784945c132SAlfred Perlstein 
16794945c132SAlfred Perlstein /*
16804945c132SAlfred Perlstein  * Below here are routines associated with translating the partial file locking
16814945c132SAlfred Perlstein  * codes into useful codes to send back to the NFS RPC messaging system
16824945c132SAlfred Perlstein  */
16834945c132SAlfred Perlstein 
16847e589b0bSAlfred Perlstein /*
16857e589b0bSAlfred Perlstein  * These routines translate the (relatively) useful return codes back onto
16867e589b0bSAlfred Perlstein  * the few return codes which the nlm subsystems wishes to trasmit
16877e589b0bSAlfred Perlstein  */
16887e589b0bSAlfred Perlstein 
16894945c132SAlfred Perlstein enum nlm_stats
do_test(struct file_lock * fl,struct file_lock ** conflicting_fl)16904945c132SAlfred Perlstein do_test(struct file_lock *fl, struct file_lock **conflicting_fl)
16914945c132SAlfred Perlstein {
16924945c132SAlfred Perlstein 	enum partialfilelock_status pfsret;
16934945c132SAlfred Perlstein 	enum nlm_stats retval;
16944945c132SAlfred Perlstein 
16954945c132SAlfred Perlstein 	debuglog("Entering do_test...\n");
16964945c132SAlfred Perlstein 
16974945c132SAlfred Perlstein 	pfsret = test_partialfilelock(fl,conflicting_fl);
16984945c132SAlfred Perlstein 
16997e589b0bSAlfred Perlstein 	switch (pfsret) {
17007e589b0bSAlfred Perlstein 	case PFL_GRANTED:
17017e589b0bSAlfred Perlstein 		debuglog("PFL test lock granted\n");
17024945c132SAlfred Perlstein 		dump_filelock(fl);
17034945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
17044945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
17057e589b0bSAlfred Perlstein 		break;
17067e589b0bSAlfred Perlstein 	case PFL_GRANTED_DUPLICATE:
17077e589b0bSAlfred Perlstein 		debuglog("PFL test lock granted--duplicate id detected\n");
17084945c132SAlfred Perlstein 		dump_filelock(fl);
17094945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
17104945c132SAlfred Perlstein 		debuglog("Clearing conflicting_fl for call semantics\n");
17114945c132SAlfred Perlstein 		*conflicting_fl = NULL;
17124945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
17137e589b0bSAlfred Perlstein 		break;
17147e589b0bSAlfred Perlstein 	case PFL_NFSDENIED:
17157e589b0bSAlfred Perlstein 	case PFL_HWDENIED:
17167e589b0bSAlfred Perlstein 		debuglog("PFL test lock denied\n");
17174945c132SAlfred Perlstein 		dump_filelock(fl);
17184945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
17194945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied;
17207e589b0bSAlfred Perlstein 		break;
17217e589b0bSAlfred Perlstein 	case PFL_NFSRESERR:
17227e589b0bSAlfred Perlstein 	case PFL_HWRESERR:
17237e589b0bSAlfred Perlstein 		debuglog("PFL test lock resource fail\n");
17247e589b0bSAlfred Perlstein 		dump_filelock(fl);
17257e589b0bSAlfred Perlstein 		dump_filelock(*conflicting_fl);
17267e589b0bSAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks;
17277e589b0bSAlfred Perlstein 		break;
17287e589b0bSAlfred Perlstein 	default:
17297e589b0bSAlfred Perlstein 		debuglog("PFL test lock *FAILED*\n");
17304945c132SAlfred Perlstein 		dump_filelock(fl);
17314945c132SAlfred Perlstein 		dump_filelock(*conflicting_fl);
17324945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied;
17337e589b0bSAlfred Perlstein 		break;
17344945c132SAlfred Perlstein 	}
17354945c132SAlfred Perlstein 
17364945c132SAlfred Perlstein 	debuglog("Exiting do_test...\n");
17374945c132SAlfred Perlstein 
17384945c132SAlfred Perlstein 	return retval;
17394945c132SAlfred Perlstein }
17404945c132SAlfred Perlstein 
17414945c132SAlfred Perlstein /*
17424945c132SAlfred Perlstein  * do_lock: Try to acquire a lock
17434945c132SAlfred Perlstein  *
17444945c132SAlfred Perlstein  * This routine makes a distinction between NLM versions.  I am pretty
17454945c132SAlfred Perlstein  * convinced that this should be abstracted out and bounced up a level
17464945c132SAlfred Perlstein  */
17474945c132SAlfred Perlstein 
17484945c132SAlfred Perlstein enum nlm_stats
do_lock(struct file_lock * fl)17494945c132SAlfred Perlstein do_lock(struct file_lock *fl)
17504945c132SAlfred Perlstein {
17514945c132SAlfred Perlstein 	enum partialfilelock_status pfsret;
17524945c132SAlfred Perlstein 	enum nlm_stats retval;
17534945c132SAlfred Perlstein 
17544945c132SAlfred Perlstein 	debuglog("Entering do_lock...\n");
17554945c132SAlfred Perlstein 
17564945c132SAlfred Perlstein 	pfsret = lock_partialfilelock(fl);
17574945c132SAlfred Perlstein 
17587e589b0bSAlfred Perlstein 	switch (pfsret) {
17597e589b0bSAlfred Perlstein 	case PFL_GRANTED:
17604945c132SAlfred Perlstein 		debuglog("PFL lock granted");
17614945c132SAlfred Perlstein 		dump_filelock(fl);
17624945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
17637e589b0bSAlfred Perlstein 		break;
17647e589b0bSAlfred Perlstein 	case PFL_GRANTED_DUPLICATE:
17654945c132SAlfred Perlstein 		debuglog("PFL lock granted--duplicate id detected");
17664945c132SAlfred Perlstein 		dump_filelock(fl);
17674945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
17687e589b0bSAlfred Perlstein 		break;
17697e589b0bSAlfred Perlstein 	case PFL_NFSDENIED:
17707e589b0bSAlfred Perlstein 	case PFL_HWDENIED:
17714945c132SAlfred Perlstein 		debuglog("PFL_NFS lock denied");
17724945c132SAlfred Perlstein 		dump_filelock(fl);
17734945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied;
17747e589b0bSAlfred Perlstein 		break;
17757e589b0bSAlfred Perlstein 	case PFL_NFSBLOCKED:
17767e589b0bSAlfred Perlstein 	case PFL_HWBLOCKED:
17777e589b0bSAlfred Perlstein 		debuglog("PFL_NFS blocking lock denied.  Queued.\n");
17787e589b0bSAlfred Perlstein 		dump_filelock(fl);
17797e589b0bSAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_blocked : nlm_blocked;
17807e589b0bSAlfred Perlstein 		break;
17817e589b0bSAlfred Perlstein 	case PFL_NFSRESERR:
17827e589b0bSAlfred Perlstein 	case PFL_HWRESERR:
17837e589b0bSAlfred Perlstein 		debuglog("PFL lock resource alocation fail\n");
17847e589b0bSAlfred Perlstein 		dump_filelock(fl);
17857e589b0bSAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks;
17867e589b0bSAlfred Perlstein 		break;
17877e589b0bSAlfred Perlstein 	default:
17884945c132SAlfred Perlstein 		debuglog("PFL lock *FAILED*");
17894945c132SAlfred Perlstein 		dump_filelock(fl);
17904945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied;
17917e589b0bSAlfred Perlstein 		break;
17924945c132SAlfred Perlstein 	}
17934945c132SAlfred Perlstein 
17944945c132SAlfred Perlstein 	debuglog("Exiting do_lock...\n");
17954945c132SAlfred Perlstein 
17964945c132SAlfred Perlstein 	return retval;
17974945c132SAlfred Perlstein }
17984945c132SAlfred Perlstein 
17994945c132SAlfred Perlstein enum nlm_stats
do_unlock(struct file_lock * fl)18004945c132SAlfred Perlstein do_unlock(struct file_lock *fl)
18014945c132SAlfred Perlstein {
18024945c132SAlfred Perlstein 	enum partialfilelock_status pfsret;
18034945c132SAlfred Perlstein 	enum nlm_stats retval;
18044945c132SAlfred Perlstein 
18054945c132SAlfred Perlstein 	debuglog("Entering do_unlock...\n");
18064945c132SAlfred Perlstein 	pfsret = unlock_partialfilelock(fl);
18074945c132SAlfred Perlstein 
18087e589b0bSAlfred Perlstein 	switch (pfsret) {
18097e589b0bSAlfred Perlstein 	case PFL_GRANTED:
18104945c132SAlfred Perlstein 		debuglog("PFL unlock granted");
18114945c132SAlfred Perlstein 		dump_filelock(fl);
18124945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
18137e589b0bSAlfred Perlstein 		break;
18147e589b0bSAlfred Perlstein 	case PFL_NFSDENIED:
18157e589b0bSAlfred Perlstein 	case PFL_HWDENIED:
18164945c132SAlfred Perlstein 		debuglog("PFL_NFS unlock denied");
18174945c132SAlfred Perlstein 		dump_filelock(fl);
18184945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied;
18197e589b0bSAlfred Perlstein 		break;
18207e589b0bSAlfred Perlstein 	case PFL_NFSDENIED_NOLOCK:
18217e589b0bSAlfred Perlstein 	case PFL_HWDENIED_NOLOCK:
18224945c132SAlfred Perlstein 		debuglog("PFL_NFS no lock found\n");
18234945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
18247e589b0bSAlfred Perlstein 		break;
18257e589b0bSAlfred Perlstein 	case PFL_NFSRESERR:
18267e589b0bSAlfred Perlstein 	case PFL_HWRESERR:
18277e589b0bSAlfred Perlstein 		debuglog("PFL unlock resource failure");
18287e589b0bSAlfred Perlstein 		dump_filelock(fl);
18297e589b0bSAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks;
18307e589b0bSAlfred Perlstein 		break;
18317e589b0bSAlfred Perlstein 	default:
18324945c132SAlfred Perlstein 		debuglog("PFL unlock *FAILED*");
18334945c132SAlfred Perlstein 		dump_filelock(fl);
18344945c132SAlfred Perlstein 		retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied;
18357e589b0bSAlfred Perlstein 		break;
18364945c132SAlfred Perlstein 	}
18374945c132SAlfred Perlstein 
18384945c132SAlfred Perlstein 	debuglog("Exiting do_unlock...\n");
18394945c132SAlfred Perlstein 
18404945c132SAlfred Perlstein 	return retval;
18414945c132SAlfred Perlstein }
18424945c132SAlfred Perlstein 
18434945c132SAlfred Perlstein /*
1844a4a8a04dSAlfred Perlstein  * do_clear
1845a4a8a04dSAlfred Perlstein  *
1846a4a8a04dSAlfred Perlstein  * This routine is non-existent because it doesn't have a return code.
1847a4a8a04dSAlfred Perlstein  * It is here for completeness in case someone *does* need to do return
1848a4a8a04dSAlfred Perlstein  * codes later.  A decent compiler should optimize this away.
1849a4a8a04dSAlfred Perlstein  */
1850a4a8a04dSAlfred Perlstein 
1851a4a8a04dSAlfred Perlstein void
do_clear(const char * hostname)1852a4a8a04dSAlfred Perlstein do_clear(const char *hostname)
1853a4a8a04dSAlfred Perlstein {
1854b6dc41baSAlfred Perlstein 
1855a4a8a04dSAlfred Perlstein 	clear_partialfilelock(hostname);
1856a4a8a04dSAlfred Perlstein }
1857a4a8a04dSAlfred Perlstein 
1858a4a8a04dSAlfred Perlstein /*
18594945c132SAlfred Perlstein  * The following routines are all called from the code which the
18604945c132SAlfred Perlstein  * RPC layer invokes
18614945c132SAlfred Perlstein  */
18624945c132SAlfred Perlstein 
18634945c132SAlfred Perlstein /*
18644945c132SAlfred Perlstein  * testlock(): inform the caller if the requested lock would be granted
18654945c132SAlfred Perlstein  *
18664945c132SAlfred Perlstein  * returns NULL if lock would granted
18674945c132SAlfred Perlstein  * returns pointer to a conflicting nlm4_holder if not
18684945c132SAlfred Perlstein  */
18694945c132SAlfred Perlstein 
18704945c132SAlfred Perlstein struct nlm4_holder *
testlock(struct nlm4_lock * lock,bool_t exclusive,int flags __unused)1871c4bc47baSAlfred Perlstein testlock(struct nlm4_lock *lock, bool_t exclusive, int flags __unused)
18724945c132SAlfred Perlstein {
18734945c132SAlfred Perlstein 	struct file_lock test_fl, *conflicting_fl;
18744945c132SAlfred Perlstein 
18754945c132SAlfred Perlstein 	bzero(&test_fl, sizeof(test_fl));
18764945c132SAlfred Perlstein 
18774945c132SAlfred Perlstein 	bcopy(lock->fh.n_bytes, &(test_fl.filehandle), sizeof(fhandle_t));
18784945c132SAlfred Perlstein 	copy_nlm4_lock_to_nlm4_holder(lock, exclusive, &test_fl.client);
18794945c132SAlfred Perlstein 
18804945c132SAlfred Perlstein 	siglock();
18814945c132SAlfred Perlstein 	do_test(&test_fl, &conflicting_fl);
18824945c132SAlfred Perlstein 
18834945c132SAlfred Perlstein 	if (conflicting_fl == NULL) {
18844945c132SAlfred Perlstein 		debuglog("No conflicting lock found\n");
18854945c132SAlfred Perlstein 		sigunlock();
18864945c132SAlfred Perlstein 		return NULL;
18874945c132SAlfred Perlstein 	} else {
18884945c132SAlfred Perlstein 		debuglog("Found conflicting lock\n");
18894945c132SAlfred Perlstein 		dump_filelock(conflicting_fl);
18904945c132SAlfred Perlstein 		sigunlock();
18914945c132SAlfred Perlstein 		return (&conflicting_fl->client);
18924945c132SAlfred Perlstein 	}
18934945c132SAlfred Perlstein }
18944945c132SAlfred Perlstein 
18954945c132SAlfred Perlstein /*
18963df5ecacSUlrich Spörlein  * getlock: try to acquire the lock.
18974945c132SAlfred Perlstein  * If file is already locked and we can sleep, put the lock in the list with
18984945c132SAlfred Perlstein  * status LKST_WAITING; it'll be processed later.
18994945c132SAlfred Perlstein  * Otherwise try to lock. If we're allowed to block, fork a child which
19004945c132SAlfred Perlstein  * will do the blocking lock.
19014945c132SAlfred Perlstein  */
19025f07c7b2SAlfred Perlstein 
19034945c132SAlfred Perlstein enum nlm_stats
getlock(nlm4_lockargs * lckarg,struct svc_req * rqstp,const int flags)19044945c132SAlfred Perlstein getlock(nlm4_lockargs *lckarg, struct svc_req *rqstp, const int flags)
19054945c132SAlfred Perlstein {
19064945c132SAlfred Perlstein 	struct file_lock *newfl;
19074945c132SAlfred Perlstein 	enum nlm_stats retval;
19084945c132SAlfred Perlstein 
19094945c132SAlfred Perlstein 	debuglog("Entering getlock...\n");
19104945c132SAlfred Perlstein 
19114945c132SAlfred Perlstein 	if (grace_expired == 0 && lckarg->reclaim == 0)
19124945c132SAlfred Perlstein 		return (flags & LOCK_V4) ?
19134945c132SAlfred Perlstein 		    nlm4_denied_grace_period : nlm_denied_grace_period;
19144945c132SAlfred Perlstein 
19154945c132SAlfred Perlstein 	/* allocate new file_lock for this request */
19163d81d1adSMichael Reifenberger 	newfl = allocate_file_lock(&lckarg->alock.oh, &lckarg->cookie,
19173d81d1adSMichael Reifenberger 				   (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf, lckarg->alock.caller_name);
19184945c132SAlfred Perlstein 	if (newfl == NULL) {
19195f07c7b2SAlfred Perlstein 		syslog(LOG_NOTICE, "lock allocate failed: %s", strerror(errno));
19204945c132SAlfred Perlstein 		/* failed */
19214945c132SAlfred Perlstein 		return (flags & LOCK_V4) ?
19224945c132SAlfred Perlstein 		    nlm4_denied_nolocks : nlm_denied_nolocks;
19234945c132SAlfred Perlstein 	}
19244945c132SAlfred Perlstein 
19254945c132SAlfred Perlstein 	if (lckarg->alock.fh.n_len != sizeof(fhandle_t)) {
19263df5ecacSUlrich Spörlein 		debuglog("received fhandle size %d, local size %d",
19274945c132SAlfred Perlstein 		    lckarg->alock.fh.n_len, (int)sizeof(fhandle_t));
19284945c132SAlfred Perlstein 	}
19294945c132SAlfred Perlstein 
19305f07c7b2SAlfred Perlstein 	fill_file_lock(newfl, (fhandle_t *)lckarg->alock.fh.n_bytes,
1931815ac496SAlfred Perlstein 	    lckarg->exclusive, lckarg->alock.svid, lckarg->alock.l_offset,
1932815ac496SAlfred Perlstein 	    lckarg->alock.l_len,
19333d81d1adSMichael Reifenberger 	    lckarg->state, 0, flags, lckarg->block);
19344945c132SAlfred Perlstein 
19354945c132SAlfred Perlstein 	/*
19364945c132SAlfred Perlstein 	 * newfl is now fully constructed and deallocate_file_lock
19374945c132SAlfred Perlstein 	 * can now be used to delete it
19384945c132SAlfred Perlstein 	 */
19394945c132SAlfred Perlstein 
19404945c132SAlfred Perlstein 	siglock();
19414945c132SAlfred Perlstein 	debuglog("Pointer to new lock is %p\n",newfl);
19427e589b0bSAlfred Perlstein 
19434945c132SAlfred Perlstein 	retval = do_lock(newfl);
19447e589b0bSAlfred Perlstein 
19454945c132SAlfred Perlstein 	debuglog("Pointer to new lock is %p\n",newfl);
19464945c132SAlfred Perlstein 	sigunlock();
19474945c132SAlfred Perlstein 
19484945c132SAlfred Perlstein 	switch (retval)
19494945c132SAlfred Perlstein 		{
19504945c132SAlfred Perlstein 		case nlm4_granted:
19514945c132SAlfred Perlstein 			/* case nlm_granted: is the same as nlm4_granted */
19524945c132SAlfred Perlstein 			/* do_mon(lckarg->alock.caller_name); */
19534945c132SAlfred Perlstein 			break;
19544945c132SAlfred Perlstein 		case nlm4_blocked:
19554945c132SAlfred Perlstein 			/* case nlm_blocked: is the same as nlm4_blocked */
19564945c132SAlfred Perlstein 			/* do_mon(lckarg->alock.caller_name); */
19574945c132SAlfred Perlstein 			break;
19584945c132SAlfred Perlstein 		default:
19594945c132SAlfred Perlstein 			deallocate_file_lock(newfl);
19604945c132SAlfred Perlstein 			break;
19614945c132SAlfred Perlstein 		}
19624945c132SAlfred Perlstein 
19634945c132SAlfred Perlstein 	debuglog("Exiting getlock...\n");
19644945c132SAlfred Perlstein 
19657e589b0bSAlfred Perlstein 	return retval;
19664945c132SAlfred Perlstein }
19674945c132SAlfred Perlstein 
19684945c132SAlfred Perlstein 
19694945c132SAlfred Perlstein /* unlock a filehandle */
19704945c132SAlfred Perlstein enum nlm_stats
unlock(nlm4_lock * lock,const int flags __unused)1971c4bc47baSAlfred Perlstein unlock(nlm4_lock *lock, const int flags __unused)
19724945c132SAlfred Perlstein {
19734945c132SAlfred Perlstein 	struct file_lock fl;
19744945c132SAlfred Perlstein 	enum nlm_stats err;
19754945c132SAlfred Perlstein 
19764945c132SAlfred Perlstein 	siglock();
19774945c132SAlfred Perlstein 
19784945c132SAlfred Perlstein 	debuglog("Entering unlock...\n");
19794945c132SAlfred Perlstein 
19804945c132SAlfred Perlstein 	bzero(&fl,sizeof(struct file_lock));
19814945c132SAlfred Perlstein 	bcopy(lock->fh.n_bytes, &fl.filehandle, sizeof(fhandle_t));
19824945c132SAlfred Perlstein 
19834945c132SAlfred Perlstein 	copy_nlm4_lock_to_nlm4_holder(lock, 0, &fl.client);
19844945c132SAlfred Perlstein 
19854945c132SAlfred Perlstein 	err = do_unlock(&fl);
19864945c132SAlfred Perlstein 
19874945c132SAlfred Perlstein 	sigunlock();
19884945c132SAlfred Perlstein 
19894945c132SAlfred Perlstein 	debuglog("Exiting unlock...\n");
19904945c132SAlfred Perlstein 
19914945c132SAlfred Perlstein 	return err;
19924945c132SAlfred Perlstein }
19934945c132SAlfred Perlstein 
19944945c132SAlfred Perlstein /*
19954945c132SAlfred Perlstein  * XXX: The following monitor/unmonitor routines
1996a4a8a04dSAlfred Perlstein  * have not been extensively tested (ie. no regression
1997a4a8a04dSAlfred Perlstein  * script exists like for the locking sections
19984945c132SAlfred Perlstein  */
19994945c132SAlfred Perlstein 
20004945c132SAlfred Perlstein /*
20014945c132SAlfred Perlstein  * monitor_lock_host: monitor lock hosts locally with a ref count and
20024945c132SAlfred Perlstein  * inform statd
20034945c132SAlfred Perlstein  */
20044945c132SAlfred Perlstein void
monitor_lock_host(const char * hostname)20054945c132SAlfred Perlstein monitor_lock_host(const char *hostname)
20064945c132SAlfred Perlstein {
20074945c132SAlfred Perlstein 	struct host *ihp, *nhp;
20084945c132SAlfred Perlstein 	struct mon smon;
20094945c132SAlfred Perlstein 	struct sm_stat_res sres;
20104945c132SAlfred Perlstein 	int rpcret, statflag;
20113d81d1adSMichael Reifenberger 	size_t n;
20124945c132SAlfred Perlstein 
20134945c132SAlfred Perlstein 	rpcret = 0;
20144945c132SAlfred Perlstein 	statflag = 0;
20154945c132SAlfred Perlstein 
2016b6dc41baSAlfred Perlstein 	LIST_FOREACH(ihp, &hostlst_head, hostlst) {
20174945c132SAlfred Perlstein 		if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) {
20184945c132SAlfred Perlstein 			/* Host is already monitored, bump refcount */
20194945c132SAlfred Perlstein 			++ihp->refcnt;
20204945c132SAlfred Perlstein 			/* Host should only be in the monitor list once */
2021b6dc41baSAlfred Perlstein 			return;
20224945c132SAlfred Perlstein 		}
20234945c132SAlfred Perlstein 	}
20244945c132SAlfred Perlstein 
20254945c132SAlfred Perlstein 	/* Host is not yet monitored, add it */
20263d81d1adSMichael Reifenberger 	n = strnlen(hostname, SM_MAXSTRLEN);
20273d81d1adSMichael Reifenberger 	if (n == SM_MAXSTRLEN) {
20283d81d1adSMichael Reifenberger 		return;
20293d81d1adSMichael Reifenberger 	}
20303d81d1adSMichael Reifenberger 	nhp = malloc(sizeof(*nhp) - sizeof(nhp->name) + n + 1);
20314945c132SAlfred Perlstein 	if (nhp == NULL) {
20324945c132SAlfred Perlstein 		debuglog("Unable to allocate entry for statd mon\n");
2033a4a8a04dSAlfred Perlstein 		return;
2034b6dc41baSAlfred Perlstein 	}
2035b6dc41baSAlfred Perlstein 
20364945c132SAlfred Perlstein 	/* Allocated new host entry, now fill the fields */
20373d81d1adSMichael Reifenberger 	memcpy(nhp->name, hostname, n);
20383d81d1adSMichael Reifenberger 	nhp->name[n] = 0;
20394945c132SAlfred Perlstein 	nhp->refcnt = 1;
20404945c132SAlfred Perlstein 	debuglog("Locally Monitoring host %16s\n",hostname);
20417e589b0bSAlfred Perlstein 
20424945c132SAlfred Perlstein 	debuglog("Attempting to tell statd\n");
20437e589b0bSAlfred Perlstein 
2044a4a8a04dSAlfred Perlstein 	bzero(&smon,sizeof(smon));
20457e589b0bSAlfred Perlstein 
20464945c132SAlfred Perlstein 	smon.mon_id.mon_name = nhp->name;
2047bc1f62deSThomas Quinot 	smon.mon_id.my_id.my_name = "localhost";
20484945c132SAlfred Perlstein 	smon.mon_id.my_id.my_prog = NLM_PROG;
20494945c132SAlfred Perlstein 	smon.mon_id.my_id.my_vers = NLM_SM;
20504945c132SAlfred Perlstein 	smon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
20514945c132SAlfred Perlstein 
205275e40e46SPeter Wemm 	rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_MON,
205375e40e46SPeter Wemm 	    (xdrproc_t)xdr_mon, &smon,
205475e40e46SPeter Wemm 	    (xdrproc_t)xdr_sm_stat_res, &sres);
20554945c132SAlfred Perlstein 
2056a4a8a04dSAlfred Perlstein 	if (rpcret == 0) {
20574945c132SAlfred Perlstein 		if (sres.res_stat == stat_fail) {
20584945c132SAlfred Perlstein 			debuglog("Statd call failed\n");
20594945c132SAlfred Perlstein 			statflag = 0;
20604945c132SAlfred Perlstein 		} else {
20614945c132SAlfred Perlstein 			statflag = 1;
20624945c132SAlfred Perlstein 		}
20634945c132SAlfred Perlstein 	} else {
2064b6dc41baSAlfred Perlstein 		debuglog("Rpc call to statd failed with return value: %d\n",
2065b6dc41baSAlfred Perlstein 		    rpcret);
20664945c132SAlfred Perlstein 		statflag = 0;
20674945c132SAlfred Perlstein 	}
20684945c132SAlfred Perlstein 
20694945c132SAlfred Perlstein 	if (statflag == 1) {
20704945c132SAlfred Perlstein 		LIST_INSERT_HEAD(&hostlst_head, nhp, hostlst);
20714945c132SAlfred Perlstein 	} else {
20724945c132SAlfred Perlstein 		free(nhp);
20734945c132SAlfred Perlstein 	}
2074b6dc41baSAlfred Perlstein 
20757e589b0bSAlfred Perlstein }
20764945c132SAlfred Perlstein 
20774945c132SAlfred Perlstein /*
20784945c132SAlfred Perlstein  * unmonitor_lock_host: clear monitor ref counts and inform statd when gone
20794945c132SAlfred Perlstein  */
20804945c132SAlfred Perlstein void
unmonitor_lock_host(char * hostname)208175e40e46SPeter Wemm unmonitor_lock_host(char *hostname)
20824945c132SAlfred Perlstein {
2083a4a8a04dSAlfred Perlstein 	struct host *ihp;
20844945c132SAlfred Perlstein 	struct mon_id smon_id;
20854945c132SAlfred Perlstein 	struct sm_stat smstat;
20864945c132SAlfred Perlstein 	int rpcret;
20874945c132SAlfred Perlstein 
20884945c132SAlfred Perlstein 	rpcret = 0;
20894945c132SAlfred Perlstein 
20907e589b0bSAlfred Perlstein 	for( ihp=LIST_FIRST(&hostlst_head); ihp != NULL;
20917e589b0bSAlfred Perlstein 	     ihp=LIST_NEXT(ihp, hostlst)) {
20924945c132SAlfred Perlstein 		if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) {
20934945c132SAlfred Perlstein 			/* Host is monitored, bump refcount */
20944945c132SAlfred Perlstein 			--ihp->refcnt;
20954945c132SAlfred Perlstein 			/* Host should only be in the monitor list once */
20964945c132SAlfred Perlstein 			break;
20974945c132SAlfred Perlstein 		}
20984945c132SAlfred Perlstein 	}
20994945c132SAlfred Perlstein 
21004945c132SAlfred Perlstein 	if (ihp == NULL) {
21014945c132SAlfred Perlstein 		debuglog("Could not find host %16s in mon list\n", hostname);
2102b6dc41baSAlfred Perlstein 		return;
2103b6dc41baSAlfred Perlstein 	}
2104b6dc41baSAlfred Perlstein 
2105b6dc41baSAlfred Perlstein 	if (ihp->refcnt > 0)
2106b6dc41baSAlfred Perlstein 		return;
2107b6dc41baSAlfred Perlstein 
21087e589b0bSAlfred Perlstein 	if (ihp->refcnt < 0) {
21097e589b0bSAlfred Perlstein 		debuglog("Negative refcount!: %d\n",
21107e589b0bSAlfred Perlstein 		    ihp->refcnt);
21114945c132SAlfred Perlstein 	}
21124945c132SAlfred Perlstein 
2113b6dc41baSAlfred Perlstein 	debuglog("Attempting to unmonitor host %16s\n", hostname);
21144945c132SAlfred Perlstein 
21154945c132SAlfred Perlstein 	bzero(&smon_id,sizeof(smon_id));
21167e589b0bSAlfred Perlstein 
21170d28a40aSAlfred Perlstein 	smon_id.mon_name = hostname;
21184945c132SAlfred Perlstein 	smon_id.my_id.my_name = "localhost";
21194945c132SAlfred Perlstein 	smon_id.my_id.my_prog = NLM_PROG;
21204945c132SAlfred Perlstein 	smon_id.my_id.my_vers = NLM_SM;
21214945c132SAlfred Perlstein 	smon_id.my_id.my_proc = NLM_SM_NOTIFY;
21227e589b0bSAlfred Perlstein 
212375e40e46SPeter Wemm 	rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON,
2124aba63820SAlfred Perlstein 	    (xdrproc_t)xdr_mon_id, &smon_id,
2125aba63820SAlfred Perlstein 	    (xdrproc_t)xdr_sm_stat, &smstat);
21267e589b0bSAlfred Perlstein 
2127a4a8a04dSAlfred Perlstein 	if (rpcret != 0) {
2128b6dc41baSAlfred Perlstein 		debuglog("Rpc call to unmonitor statd failed with "
2129b6dc41baSAlfred Perlstein 		   " return value: %d\n", rpcret);
21307e589b0bSAlfred Perlstein 	}
21317e589b0bSAlfred Perlstein 
21324945c132SAlfred Perlstein 	LIST_REMOVE(ihp, hostlst);
21334945c132SAlfred Perlstein 	free(ihp);
21344945c132SAlfred Perlstein }
21354945c132SAlfred Perlstein 
21364945c132SAlfred Perlstein /*
21374945c132SAlfred Perlstein  * notify: Clear all locks from a host if statd complains
21384945c132SAlfred Perlstein  *
2139a4a8a04dSAlfred Perlstein  * XXX: This routine has not been thoroughly tested.  However, neither
2140a4a8a04dSAlfred Perlstein  * had the old one been.  It used to compare the statd crash state counter
2141a4a8a04dSAlfred Perlstein  * to the current lock state.  The upshot of this was that it basically
2142a4a8a04dSAlfred Perlstein  * cleared all locks from the specified host 99% of the time (with the
2143a4a8a04dSAlfred Perlstein  * other 1% being a bug).  Consequently, the assumption is that clearing
2144a4a8a04dSAlfred Perlstein  * all locks from a host when notified by statd is acceptable.
21454945c132SAlfred Perlstein  *
2146a4a8a04dSAlfred Perlstein  * Please note that this routine skips the usual level of redirection
2147a4a8a04dSAlfred Perlstein  * through a do_* type routine.  This introduces a possible level of
2148a4a8a04dSAlfred Perlstein  * error and might better be written as do_notify and take this one out.
2149a4a8a04dSAlfred Perlstein 
21504945c132SAlfred Perlstein  */
21514945c132SAlfred Perlstein 
21524945c132SAlfred Perlstein void
notify(const char * hostname,const int state)21534945c132SAlfred Perlstein notify(const char *hostname, const int state)
21544945c132SAlfred Perlstein {
21554945c132SAlfred Perlstein 	debuglog("notify from %s, new state %d", hostname, state);
21564945c132SAlfred Perlstein 
21574945c132SAlfred Perlstein 	siglock();
2158a4a8a04dSAlfred Perlstein 	do_clear(hostname);
21594945c132SAlfred Perlstein 	sigunlock();
2160a4a8a04dSAlfred Perlstein 
2161a4a8a04dSAlfred Perlstein 	debuglog("Leaving notify\n");
21624945c132SAlfred Perlstein }
21634945c132SAlfred Perlstein 
21644945c132SAlfred Perlstein void
send_granted(struct file_lock * fl,int opcode __unused)21652541accbSJohn Baldwin send_granted(struct file_lock *fl, int opcode __unused)
21664945c132SAlfred Perlstein {
21674945c132SAlfred Perlstein 	CLIENT *cli;
21684945c132SAlfred Perlstein 	static char dummy;
21694945c132SAlfred Perlstein 	struct timeval timeo;
21704945c132SAlfred Perlstein 	int success;
21714945c132SAlfred Perlstein 	static struct nlm_res retval;
21724945c132SAlfred Perlstein 	static struct nlm4_res retval4;
21734945c132SAlfred Perlstein 
21747e589b0bSAlfred Perlstein 	debuglog("About to send granted on blocked lock\n");
21757e589b0bSAlfred Perlstein 
21764945c132SAlfred Perlstein 	cli = get_client(fl->addr,
21774945c132SAlfred Perlstein 	    (fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS);
21784945c132SAlfred Perlstein 	if (cli == NULL) {
21794945c132SAlfred Perlstein 		syslog(LOG_NOTICE, "failed to get CLIENT for %s",
21804945c132SAlfred Perlstein 		    fl->client_name);
21814945c132SAlfred Perlstein 		/*
21824945c132SAlfred Perlstein 		 * We fail to notify remote that the lock has been granted.
21834945c132SAlfred Perlstein 		 * The client will timeout and retry, the lock will be
21844945c132SAlfred Perlstein 		 * granted at this time.
21854945c132SAlfred Perlstein 		 */
21864945c132SAlfred Perlstein 		return;
21874945c132SAlfred Perlstein 	}
21884945c132SAlfred Perlstein 	timeo.tv_sec = 0;
21894945c132SAlfred Perlstein 	timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */
21904945c132SAlfred Perlstein 
21914945c132SAlfred Perlstein 	if (fl->flags & LOCK_V4) {
21924945c132SAlfred Perlstein 		static nlm4_testargs res;
21934945c132SAlfred Perlstein 		res.cookie = fl->client_cookie;
21944945c132SAlfred Perlstein 		res.exclusive = fl->client.exclusive;
21954945c132SAlfred Perlstein 		res.alock.caller_name = fl->client_name;
21964945c132SAlfred Perlstein 		res.alock.fh.n_len = sizeof(fhandle_t);
21974945c132SAlfred Perlstein 		res.alock.fh.n_bytes = (char*)&fl->filehandle;
21984945c132SAlfred Perlstein 		res.alock.oh = fl->client.oh;
21994945c132SAlfred Perlstein 		res.alock.svid = fl->client.svid;
22004945c132SAlfred Perlstein 		res.alock.l_offset = fl->client.l_offset;
22014945c132SAlfred Perlstein 		res.alock.l_len = fl->client.l_len;
22024945c132SAlfred Perlstein 		debuglog("sending v4 reply%s",
22034945c132SAlfred Perlstein 			 (fl->flags & LOCK_ASYNC) ? " (async)":"");
22044945c132SAlfred Perlstein 		if (fl->flags & LOCK_ASYNC) {
22054945c132SAlfred Perlstein 			success = clnt_call(cli, NLM4_GRANTED_MSG,
220675e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm4_testargs, &res,
220775e40e46SPeter Wemm 			    (xdrproc_t)xdr_void, &dummy, timeo);
22084945c132SAlfred Perlstein 		} else {
22094945c132SAlfred Perlstein 			success = clnt_call(cli, NLM4_GRANTED,
221075e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm4_testargs, &res,
221175e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm4_res, &retval4, timeo);
22124945c132SAlfred Perlstein 		}
22134945c132SAlfred Perlstein 	} else {
22144945c132SAlfred Perlstein 		static nlm_testargs res;
22154945c132SAlfred Perlstein 
22164945c132SAlfred Perlstein 		res.cookie = fl->client_cookie;
22174945c132SAlfred Perlstein 		res.exclusive = fl->client.exclusive;
22184945c132SAlfred Perlstein 		res.alock.caller_name = fl->client_name;
22194945c132SAlfred Perlstein 		res.alock.fh.n_len = sizeof(fhandle_t);
22204945c132SAlfred Perlstein 		res.alock.fh.n_bytes = (char*)&fl->filehandle;
22214945c132SAlfred Perlstein 		res.alock.oh = fl->client.oh;
22224945c132SAlfred Perlstein 		res.alock.svid = fl->client.svid;
22234945c132SAlfred Perlstein 		res.alock.l_offset = fl->client.l_offset;
22244945c132SAlfred Perlstein 		res.alock.l_len = fl->client.l_len;
22254945c132SAlfred Perlstein 		debuglog("sending v1 reply%s",
22264945c132SAlfred Perlstein 			 (fl->flags & LOCK_ASYNC) ? " (async)":"");
22274945c132SAlfred Perlstein 		if (fl->flags & LOCK_ASYNC) {
22284945c132SAlfred Perlstein 			success = clnt_call(cli, NLM_GRANTED_MSG,
222975e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm_testargs, &res,
223075e40e46SPeter Wemm 			    (xdrproc_t)xdr_void, &dummy, timeo);
22314945c132SAlfred Perlstein 		} else {
22324945c132SAlfred Perlstein 			success = clnt_call(cli, NLM_GRANTED,
223375e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm_testargs, &res,
223475e40e46SPeter Wemm 			    (xdrproc_t)xdr_nlm_res, &retval, timeo);
22354945c132SAlfred Perlstein 		}
22364945c132SAlfred Perlstein 	}
22374945c132SAlfred Perlstein 	if (debug_level > 2)
22387e589b0bSAlfred Perlstein 		debuglog("clnt_call returns %d(%s) for granted",
22394945c132SAlfred Perlstein 			 success, clnt_sperrno(success));
22404945c132SAlfred Perlstein 
22414945c132SAlfred Perlstein }
22424945c132SAlfred Perlstein 
22434945c132SAlfred Perlstein /*
2244a4a8a04dSAlfred Perlstein  * Routines below here have not been modified in the overhaul
2245a4a8a04dSAlfred Perlstein  */
2246a4a8a04dSAlfred Perlstein 
2247a4a8a04dSAlfred Perlstein /*
22487e589b0bSAlfred Perlstein  * Are these two routines still required since lockd is not spawning off
22497e589b0bSAlfred Perlstein  * children to service locks anymore?  Presumably they were originally
22507e589b0bSAlfred Perlstein  * put in place to prevent a one child from changing the lock list out
22517e589b0bSAlfred Perlstein  * from under another one.
22524945c132SAlfred Perlstein  */
22537e589b0bSAlfred Perlstein 
22544945c132SAlfred Perlstein void
siglock(void)22554945c132SAlfred Perlstein siglock(void)
22564945c132SAlfred Perlstein {
22574945c132SAlfred Perlstein   sigset_t block;
22584945c132SAlfred Perlstein 
22594945c132SAlfred Perlstein   sigemptyset(&block);
22604945c132SAlfred Perlstein   sigaddset(&block, SIGCHLD);
22614945c132SAlfred Perlstein 
22627e589b0bSAlfred Perlstein   if (sigprocmask(SIG_BLOCK, &block, NULL) < 0) {
22634945c132SAlfred Perlstein     syslog(LOG_WARNING, "siglock failed: %s", strerror(errno));
22644945c132SAlfred Perlstein   }
22654945c132SAlfred Perlstein }
22664945c132SAlfred Perlstein 
22674945c132SAlfred Perlstein void
sigunlock(void)22684945c132SAlfred Perlstein sigunlock(void)
22694945c132SAlfred Perlstein {
22704945c132SAlfred Perlstein   sigset_t block;
22714945c132SAlfred Perlstein 
22724945c132SAlfred Perlstein   sigemptyset(&block);
22734945c132SAlfred Perlstein   sigaddset(&block, SIGCHLD);
22744945c132SAlfred Perlstein 
22757e589b0bSAlfred Perlstein   if (sigprocmask(SIG_UNBLOCK, &block, NULL) < 0) {
22764945c132SAlfred Perlstein     syslog(LOG_WARNING, "sigunlock failed: %s", strerror(errno));
22774945c132SAlfred Perlstein   }
22784945c132SAlfred Perlstein }
2279