1 /* $NetBSD: lockd_lock.c,v 1.5 2000/11/21 03:47:41 enami Exp $ */
2 /* $FreeBSD: src/usr.sbin/rpc.lockd/lockd_lock.c,v 1.10 2002/03/22 19:57:09 alfred Exp $ */
5 * Copyright (c) 2001 Andrew P. Lentvorski, Jr.
6 * Copyright (c) 2000 Manuel Bouyer.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/param.h>
56 #include <sys/mount.h>
58 #include <rpcsvc/sm_inter.h>
59 #include <rpcsvc/nlm_prot.h>
62 #include "lockd_lock.h"
64 #define MAXOBJECTSIZE 64
65 #define MAXBUFFERSIZE 1024
68 * A set of utilities for managing file locking
70 * XXX: All locks are in a linked list, a better structure should be used
71 * to improve search/access effeciency.
74 /* struct describing a lock */
76 LIST_ENTRY(file_lock
) nfslocklist
;
77 netobj filehandle
; /* NFS filehandle */
78 struct sockaddr
*addr
;
79 struct nlm4_holder client
; /* lock holder */
80 u_int64_t granted_cookie
;
81 int nsm_status
; /* status from the remote lock manager */
82 int status
; /* lock status, see below */
83 int flags
; /* lock flags, see lockd_lock.h */
84 int blocking
; /* blocking lock or not */
85 char client_name
[SM_MAXSTRLEN
]; /* client_name is really variable length and must be last! */
88 LIST_HEAD(nfslocklist_head
, file_lock
);
89 struct nfslocklist_head nfslocklist_head
= LIST_HEAD_INITIALIZER(nfslocklist_head
);
91 LIST_HEAD(blockedlocklist_head
, file_lock
);
92 struct blockedlocklist_head blockedlocklist_head
= LIST_HEAD_INITIALIZER(blockedlocklist_head
);
94 /* struct describing a share reservation */
96 LIST_ENTRY(file_share
) nfssharelist
;
97 netobj oh
; /* share holder */
100 char client_name
[SM_MAXSTRLEN
]; /* name is really variable length and must be last! */
102 LIST_HEAD(nfssharelist_head
, file_share
);
104 /* Struct describing a file with share reservations */
106 LIST_ENTRY(sharefile
) sharefilelist
;
107 netobj filehandle
; /* Local access filehandle */
108 int fd
; /* file descriptor: remains open until no more shares */
110 struct nfssharelist_head sharelist_head
;
112 LIST_HEAD(nfssharefilelist_head
, sharefile
);
113 struct nfssharefilelist_head nfssharefilelist_head
= LIST_HEAD_INITIALIZER(nfssharefilelist_head
);
116 #define LKST_LOCKED 1 /* lock is locked */
117 /* XXX: Is this flag file specific or lock specific? */
118 #define LKST_WAITING 2 /* file is already locked by another host */
119 #define LKST_PROCESSING 3 /* child is trying to aquire the lock */
120 #define LKST_DYING 4 /* must dies when we get news from the child */
122 /* struct describing a monitored host */
124 TAILQ_ENTRY(host
) hostlst
;
127 struct sockaddr addr
;
128 char *name
; /* host name provided by client via caller_name */
129 char *revname
; /* host name mapped from addr */
131 /* list of hosts we monitor */
132 TAILQ_HEAD(hostlst_head
, host
);
133 struct hostlst_head hostlst_head
= TAILQ_HEAD_INITIALIZER(hostlst_head
);
134 struct hostlst_head hostlst_unref
= TAILQ_HEAD_INITIALIZER(hostlst_unref
);
136 int host_expire
= 60; /* seconds */
138 u_int64_t send_granted_cookie
= 0;
141 * File monitoring handlers
142 * XXX: These might be able to be removed when kevent support
143 * is placed into the hardware lock/unlock routines. (ie.
144 * let the kernel do all the file monitoring)
147 /* Struct describing a monitored file */
149 LIST_ENTRY(monfile
) monfilelist
;
150 netobj filehandle
; /* Local access filehandle */
151 int fd
; /* file descriptor: remains open until unlock! */
156 /* List of files we monitor */
157 LIST_HEAD(monfilelist_head
, monfile
);
158 struct monfilelist_head monfilelist_head
= LIST_HEAD_INITIALIZER(monfilelist_head
);
160 static int debugdelay
= 0;
162 enum nfslock_status
{ NFS_GRANTED
= 0, NFS_GRANTED_DUPLICATE
,
163 NFS_DENIED
, NFS_DENIED_NOLOCK
,
166 enum hwlock_status
{ HW_GRANTED
= 0, HW_GRANTED_DUPLICATE
,
167 HW_DENIED
, HW_DENIED_NOLOCK
,
168 HW_STALEFH
, HW_READONLY
, HW_RESERR
};
170 enum partialfilelock_status
{ PFL_GRANTED
=0, PFL_GRANTED_DUPLICATE
, PFL_DENIED
,
171 PFL_NFSDENIED
, PFL_NFSBLOCKED
, PFL_NFSDENIED_NOLOCK
, PFL_NFSRESERR
,
172 PFL_HWDENIED
, PFL_HWBLOCKED
, PFL_HWDENIED_NOLOCK
, PFL_HWRESERR
,
173 PFL_HWDENIED_STALEFH
, PFL_HWDENIED_READONLY
};
175 enum LFLAGS
{LEDGE_LEFT
, LEDGE_LBOUNDARY
, LEDGE_INSIDE
, LEDGE_RBOUNDARY
, LEDGE_RIGHT
};
176 enum RFLAGS
{REDGE_LEFT
, REDGE_LBOUNDARY
, REDGE_INSIDE
, REDGE_RBOUNDARY
, REDGE_RIGHT
};
177 /* XXX: WARNING! I HAVE OVERLOADED THIS STATUS ENUM! SPLIT IT APART INTO TWO */
178 enum split_status
{SPL_DISJOINT
=0, SPL_LOCK1
=1, SPL_LOCK2
=2, SPL_CONTAINED
=4, SPL_RESERR
=8};
180 enum partialfilelock_status
lock_partialfilelock(struct file_lock
*fl
);
182 int send_granted(struct file_lock
*fl
, int opcode
);
184 void sigunlock(void);
185 void destroy_lock_host(struct host
*ihp
);
186 static void monitor_lock_host(const char *hostname
, const struct sockaddr
*addr
);
188 void copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock
*src
,
189 const bool_t exclusive
, struct nlm4_holder
*dest
);
190 struct file_lock
* allocate_file_lock(const netobj
*lockowner
,
191 const netobj
*filehandle
, const struct sockaddr
*addr
,
192 const char *caller_name
);
193 void deallocate_file_lock(struct file_lock
*fl
);
194 void fill_file_lock(struct file_lock
*fl
,
195 const bool_t exclusive
, const int32_t svid
,
196 const u_int64_t offset
, const u_int64_t len
,
197 const int state
, const int status
, const int flags
, const int blocking
);
198 int regions_overlap(const u_int64_t start1
, const u_int64_t len1
,
199 const u_int64_t start2
, const u_int64_t len2
);;
200 enum split_status
region_compare(const u_int64_t starte
, const u_int64_t lene
,
201 const u_int64_t startu
, const u_int64_t lenu
,
202 u_int64_t
*start1
, u_int64_t
*len1
, u_int64_t
*start2
, u_int64_t
*len2
);
203 int same_netobj(const netobj
*n0
, const netobj
*n1
);
204 int same_filelock_identity(const struct file_lock
*fl0
,
205 const struct file_lock
*fl2
);
207 static void debuglog(char const *fmt
, ...);
208 void dump_static_object(const unsigned char* object
, const int sizeof_object
,
209 unsigned char* hbuff
, const int sizeof_hbuff
,
210 unsigned char* cbuff
, const int sizeof_cbuff
);
211 void dump_netobj(const struct netobj
*nobj
);
212 void dump_filelock(const struct file_lock
*fl
);
213 struct file_lock
* get_lock_matching_unlock(const struct file_lock
*fl
);
214 enum nfslock_status
test_nfslock(const struct file_lock
*fl
,
215 struct file_lock
**conflicting_fl
);
216 enum nfslock_status
lock_nfslock(struct file_lock
*fl
);
217 enum nfslock_status
delete_nfslock(struct file_lock
*fl
);
218 enum nfslock_status
unlock_nfslock(const struct file_lock
*fl
,
219 struct file_lock
**released_lock
, struct file_lock
**left_lock
,
220 struct file_lock
**right_lock
);
221 enum hwlock_status
lock_hwlock(struct file_lock
*fl
);
222 enum split_status
split_nfslock(const struct file_lock
*exist_lock
,
223 const struct file_lock
*unlock_lock
, struct file_lock
**left_lock
,
224 struct file_lock
**right_lock
);
225 void add_blockingfilelock(struct file_lock
*fl
);
226 enum hwlock_status
unlock_hwlock(const struct file_lock
*fl
);
227 enum hwlock_status
test_hwlock(const struct file_lock
*fl
,
228 struct file_lock
**conflicting_fl
);
229 void remove_blockingfilelock(struct file_lock
*fl
);
230 void clear_blockingfilelock(const char *hostname
);
231 void retry_blockingfilelocklist(netobj
*fh
);
232 enum partialfilelock_status
unlock_partialfilelock(
233 const struct file_lock
*fl
);
234 void clear_partialfilelock(const char *hostname
);
235 enum partialfilelock_status
test_partialfilelock(
236 const struct file_lock
*fl
, struct file_lock
**conflicting_fl
);
237 enum nlm4_stats
do_test(struct file_lock
*fl
, struct file_lock
**conflicting_fl
);
238 enum nlm4_stats
do_unlock(struct file_lock
*fl
);
239 enum nlm4_stats
do_lock(struct file_lock
*fl
);
240 void do_clear(const char *hostname
);
244 debuglog(char const *fmt
, ...)
248 if (debug_level
< 1) {
255 vsyslog(LOG_DEBUG
, fmt
, ap
);
260 dump_static_object(object
, size_object
, hbuff
, size_hbuff
, cbuff
, size_cbuff
)
261 const unsigned char *object
;
262 const int size_object
;
263 unsigned char *hbuff
;
264 const int size_hbuff
;
265 unsigned char *cbuff
;
266 const int size_cbuff
;
270 if (debug_level
< 2) {
274 objectsize
= size_object
;
276 if (objectsize
== 0) {
277 debuglog("object is size 0\n");
279 if (objectsize
> MAXOBJECTSIZE
) {
280 debuglog("Object of size %d being clamped"
281 "to size %d\n", objectsize
, MAXOBJECTSIZE
);
282 objectsize
= MAXOBJECTSIZE
;
286 if (size_hbuff
< objectsize
*2+1) {
287 debuglog("Hbuff not large enough."
290 for(i
=0;i
<objectsize
;i
++) {
291 sprintf(hbuff
+i
*2,"%02x",*(object
+i
));
298 if (size_cbuff
< objectsize
+1) {
299 debuglog("Cbuff not large enough."
303 for(i
=0;i
<objectsize
;i
++) {
304 if (*(object
+i
) >= 32 && *(object
+i
) <= 127) {
305 *(cbuff
+i
) = *(object
+i
);
316 dump_netobj(const struct netobj
*nobj
)
318 char hbuff
[MAXBUFFERSIZE
*2];
319 char cbuff
[MAXBUFFERSIZE
];
321 if (debug_level
< 2) {
326 debuglog("Null netobj pointer\n");
328 else if (nobj
->n_len
== 0) {
329 debuglog("Size zero netobj\n");
331 dump_static_object(nobj
->n_bytes
, nobj
->n_len
,
332 hbuff
, sizeof(hbuff
), cbuff
, sizeof(cbuff
));
333 debuglog("netobj: len: %d data: %s ::: %s\n",
334 nobj
->n_len
, hbuff
, cbuff
);
338 /* #define DUMP_FILELOCK_VERBOSE */
340 dump_filelock(const struct file_lock
*fl
)
342 #ifdef DUMP_FILELOCK_VERBOSE
343 char hbuff
[MAXBUFFERSIZE
*2];
344 char cbuff
[MAXBUFFERSIZE
];
347 if (debug_level
< 2) {
352 debuglog("Dumping file lock structure @ %p\n", fl
);
354 #ifdef DUMP_FILELOCK_VERBOSE
355 dump_static_object((unsigned char *)&fl
->filehandle
.n_bytes
,
356 fl
->filehandle
.n_len
, hbuff
, sizeof(hbuff
),
357 cbuff
, sizeof(cbuff
));
358 debuglog("Filehandle: %8s ::: %8s\n", hbuff
, cbuff
);
361 debuglog("Dumping nlm4_holder:\n"
362 "exc: %x svid: %x offset:len %llx:%llx\n",
363 fl
->client
.exclusive
, fl
->client
.svid
,
364 fl
->client
.l_offset
, fl
->client
.l_len
);
366 #ifdef DUMP_FILELOCK_VERBOSE
367 debuglog("Dumping client identity:\n");
368 dump_netobj(&fl
->client
.oh
);
370 debuglog("nsm: %d status: %d flags: %d locker: %d"
371 " fd: %d\n", fl
->nsm_status
, fl
->status
,
372 fl
->flags
, fl
->locker
, fl
->fd
);
375 debuglog("NULL file lock structure\n");
380 copy_nlm4_lock_to_nlm4_holder(src
, exclusive
, dest
)
381 const struct nlm4_lock
*src
;
382 const bool_t exclusive
;
383 struct nlm4_holder
*dest
;
386 dest
->exclusive
= exclusive
;
387 dest
->oh
.n_len
= src
->oh
.n_len
;
388 dest
->oh
.n_bytes
= src
->oh
.n_bytes
;
389 dest
->svid
= src
->svid
;
390 dest
->l_offset
= src
->l_offset
;
391 dest
->l_len
= src
->l_len
;
396 strnlen(const char *s
, size_t len
)
400 for (n
= 0; s
[n
] != 0 && n
< len
; n
++)
406 * allocate_file_lock: Create a lock with the given parameters
410 allocate_file_lock(const netobj
*lockowner
, const netobj
*filehandle
,
411 const struct sockaddr
*addr
, const char *caller_name
)
413 struct file_lock
*newfl
;
416 /* Beware of rubbish input! */
417 n
= strnlen(caller_name
, SM_MAXSTRLEN
);
418 if (n
== SM_MAXSTRLEN
) {
422 newfl
= malloc(sizeof(*newfl
) - sizeof(newfl
->client_name
) + n
+ 1);
426 bzero(newfl
, sizeof(*newfl
) - sizeof(newfl
->client_name
));
427 memcpy(newfl
->client_name
, caller_name
, n
);
428 newfl
->client_name
[n
] = 0;
430 newfl
->client
.oh
.n_bytes
= malloc(lockowner
->n_len
);
431 if (newfl
->client
.oh
.n_bytes
== NULL
) {
435 newfl
->client
.oh
.n_len
= lockowner
->n_len
;
436 bcopy(lockowner
->n_bytes
, newfl
->client
.oh
.n_bytes
, lockowner
->n_len
);
438 newfl
->filehandle
.n_bytes
= malloc(filehandle
->n_len
);
439 if (newfl
->filehandle
.n_bytes
== NULL
) {
440 free(newfl
->client
.oh
.n_bytes
);
444 newfl
->filehandle
.n_len
= filehandle
->n_len
;
445 bcopy(filehandle
->n_bytes
, newfl
->filehandle
.n_bytes
, filehandle
->n_len
);
447 newfl
->addr
= malloc(addr
->sa_len
);
448 if (newfl
->addr
== NULL
) {
449 free(newfl
->client
.oh
.n_bytes
);
453 memcpy(newfl
->addr
, addr
, addr
->sa_len
);
459 * file_file_lock: Force creation of a valid file lock
462 fill_file_lock(struct file_lock
*fl
,
463 const bool_t exclusive
, const int32_t svid
,
464 const u_int64_t offset
, const u_int64_t len
,
465 const int state
, const int status
, const int flags
, const int blocking
)
467 fl
->client
.exclusive
= exclusive
;
468 fl
->client
.svid
= svid
;
469 fl
->client
.l_offset
= offset
;
470 fl
->client
.l_len
= len
;
472 fl
->nsm_status
= state
;
475 fl
->blocking
= blocking
;
479 * deallocate_file_lock: Free all storage associated with a file lock
482 deallocate_file_lock(struct file_lock
*fl
)
485 free(fl
->client
.oh
.n_bytes
);
486 free(fl
->filehandle
.n_bytes
);
491 * regions_overlap(): This function examines the two provided regions for
495 regions_overlap(start1
, len1
, start2
, len2
)
496 const u_int64_t start1
, len1
, start2
, len2
;
498 u_int64_t d1
,d2
,d3
,d4
;
499 enum split_status result
;
501 debuglog("Entering region overlap with vals: %llu:%llu--%llu:%llu\n",
502 start1
, len1
, start2
, len2
);
504 result
= region_compare(start1
, len1
, start2
, len2
,
507 debuglog("Exiting region overlap with val: %d\n",result
);
509 if (result
== SPL_DISJOINT
) {
519 * region_compare(): Examine lock regions and split appropriately
521 * XXX: Fix 64 bit overflow problems
522 * XXX: Check to make sure I got *ALL* the cases.
523 * XXX: This DESPERATELY needs a regression test.
526 region_compare(starte
, lene
, startu
, lenu
,
527 start1
, len1
, start2
, len2
)
528 const u_int64_t starte
, lene
, startu
, lenu
;
529 u_int64_t
*start1
, *len1
, *start2
, *len2
;
532 * Please pay attention to the sequential exclusions
533 * of the if statements!!!
537 enum split_status retval
;
539 retval
= SPL_DISJOINT
;
541 if (lene
== 0 && lenu
== 0) {
542 /* Examine left edge of locker */
543 if (startu
< starte
) {
545 } else if (startu
== starte
) {
546 lflags
= LEDGE_LBOUNDARY
;
548 lflags
= LEDGE_INSIDE
;
551 rflags
= REDGE_RBOUNDARY
; /* Both are infiinite */
553 if (lflags
== LEDGE_INSIDE
) {
555 *len1
= startu
- starte
;
558 if (lflags
== LEDGE_LEFT
|| lflags
== LEDGE_LBOUNDARY
) {
559 retval
= SPL_CONTAINED
;
563 } else if (lene
== 0 && lenu
!= 0) {
564 /* Established lock is infinite */
565 /* Examine left edge of unlocker */
566 if (startu
< starte
) {
568 } else if (startu
== starte
) {
569 lflags
= LEDGE_LBOUNDARY
;
570 } else if (startu
> starte
) {
571 lflags
= LEDGE_INSIDE
;
574 /* Examine right edge of unlocker */
575 if (startu
+ lenu
< starte
) {
576 /* Right edge of unlocker left of established lock */
579 } else if (startu
+ lenu
== starte
) {
580 /* Right edge of unlocker on start of established lock */
581 rflags
= REDGE_LBOUNDARY
;
583 } else { /* Infinifty is right of finity */
584 /* Right edge of unlocker inside established lock */
585 rflags
= REDGE_INSIDE
;
588 if (lflags
== LEDGE_INSIDE
) {
590 *len1
= startu
- starte
;
594 if (rflags
== REDGE_INSIDE
) {
595 /* Create right lock */
596 *start2
= startu
+lenu
;
600 } else if (lene
!= 0 && lenu
== 0) {
601 /* Unlocker is infinite */
602 /* Examine left edge of unlocker */
603 if (startu
< starte
) {
605 retval
= SPL_CONTAINED
;
607 } else if (startu
== starte
) {
608 lflags
= LEDGE_LBOUNDARY
;
609 retval
= SPL_CONTAINED
;
611 } else if ((startu
> starte
) && (startu
< starte
+ lene
- 1)) {
612 lflags
= LEDGE_INSIDE
;
613 } else if (startu
== starte
+ lene
- 1) {
614 lflags
= LEDGE_RBOUNDARY
;
615 } else { /* startu > starte + lene -1 */
616 lflags
= LEDGE_RIGHT
;
620 rflags
= REDGE_RIGHT
; /* Infinity is right of finity */
622 if (lflags
== LEDGE_INSIDE
|| lflags
== LEDGE_RBOUNDARY
) {
624 *len1
= startu
- starte
;
630 /* Both locks are finite */
632 /* Examine left edge of unlocker */
633 if (startu
< starte
) {
635 } else if (startu
== starte
) {
636 lflags
= LEDGE_LBOUNDARY
;
637 } else if ((startu
> starte
) && (startu
< starte
+ lene
- 1)) {
638 lflags
= LEDGE_INSIDE
;
639 } else if (startu
== starte
+ lene
- 1) {
640 lflags
= LEDGE_RBOUNDARY
;
641 } else { /* startu > starte + lene -1 */
642 lflags
= LEDGE_RIGHT
;
646 /* Examine right edge of unlocker */
647 if (startu
+ lenu
< starte
) {
648 /* Right edge of unlocker left of established lock */
651 } else if (startu
+ lenu
== starte
) {
652 /* Right edge of unlocker on start of established lock */
653 rflags
= REDGE_LBOUNDARY
;
655 } else if (startu
+ lenu
< starte
+ lene
) {
656 /* Right edge of unlocker inside established lock */
657 rflags
= REDGE_INSIDE
;
658 } else if (startu
+ lenu
== starte
+ lene
) {
659 /* Right edge of unlocker on right edge of established lock */
660 rflags
= REDGE_RBOUNDARY
;
661 } else { /* startu + lenu > starte + lene */
662 /* Right edge of unlocker is right of established lock */
663 rflags
= REDGE_RIGHT
;
666 if (lflags
== LEDGE_INSIDE
|| lflags
== LEDGE_RBOUNDARY
) {
667 /* Create left lock */
669 *len1
= (startu
- starte
);
673 if (rflags
== REDGE_INSIDE
) {
674 /* Create right lock */
675 *start2
= startu
+lenu
;
676 *len2
= starte
+lene
-(startu
+lenu
);
680 if ((lflags
== LEDGE_LEFT
|| lflags
== LEDGE_LBOUNDARY
) &&
681 (rflags
== REDGE_RBOUNDARY
|| rflags
== REDGE_RIGHT
)) {
682 retval
= SPL_CONTAINED
;
690 * same_netobj: Compares the apprpriate bits of a netobj for identity
693 same_netobj(const netobj
*n0
, const netobj
*n1
)
699 debuglog("Entering netobj identity check\n");
701 if (n0
->n_len
== n1
->n_len
) {
702 debuglog("Preliminary length check passed\n");
703 retval
= !bcmp(n0
->n_bytes
, n1
->n_bytes
, n0
->n_len
);
704 debuglog("netobj %smatch\n", retval
? "" : "mis");
711 * same_filelock_identity: Compares the appropriate bits of a file_lock
714 same_filelock_identity(fl0
, fl1
)
715 const struct file_lock
*fl0
, *fl1
;
721 debuglog("Checking filelock identity\n");
724 * Check process ids and host information.
726 retval
= (fl0
->client
.svid
== fl1
->client
.svid
&&
727 same_netobj(&(fl0
->client
.oh
), &(fl1
->client
.oh
)));
729 debuglog("Exiting checking filelock identity: retval: %d\n",retval
);
735 * Below here are routines associated with manipulating the NFS
740 * get_lock_matching_unlock: Return a lock which matches the given unlock lock
742 * XXX: It is a shame that this duplicates so much code from test_nfslock.
745 get_lock_matching_unlock(const struct file_lock
*fl
)
747 struct file_lock
*ifl
; /* Iterator */
749 debuglog("Entering lock_matching_unlock\n");
750 debuglog("********Dump of fl*****************\n");
753 LIST_FOREACH(ifl
, &nfslocklist_head
, nfslocklist
) {
754 debuglog("Pointer to file lock: %p\n",ifl
);
756 debuglog("****Dump of ifl****\n");
758 debuglog("*******************\n");
761 * XXX: It is conceivable that someone could use the NLM RPC
762 * system to directly access filehandles. This may be a
763 * security hazard as the filehandle code may bypass normal
764 * file access controls
766 if (fl
->filehandle
.n_len
!= ifl
->filehandle
.n_len
)
768 if (bcmp(fl
->filehandle
.n_bytes
, ifl
->filehandle
.n_bytes
,
769 fl
->filehandle
.n_len
))
772 debuglog("matching_unlock: Filehandles match, "
773 "checking regions\n");
775 /* Filehandles match, check for region overlap */
776 if (!regions_overlap(fl
->client
.l_offset
, fl
->client
.l_len
,
777 ifl
->client
.l_offset
, ifl
->client
.l_len
))
780 debuglog("matching_unlock: Region overlap"
781 " found %llu : %llu -- %llu : %llu\n",
782 fl
->client
.l_offset
,fl
->client
.l_len
,
783 ifl
->client
.l_offset
,ifl
->client
.l_len
);
785 /* Regions overlap, check the identity */
786 if (!same_filelock_identity(fl
,ifl
))
789 debuglog("matching_unlock: Duplicate lock id. Granting\n");
793 debuglog("Exiting lock_matching_unlock\n");
799 * test_nfslock: check for NFS lock in lock list
801 * This routine makes the following assumptions:
802 * 1) Nothing will adjust the lock list during a lookup
804 * This routine has an intersting quirk which bit me hard.
805 * The conflicting_fl is the pointer to the conflicting lock.
806 * However, to modify the "*pointer* to the conflicting lock" rather
807 * that the "conflicting lock itself" one must pass in a "pointer to
808 * the pointer of the conflicting lock". Gross.
812 test_nfslock(const struct file_lock
*fl
, struct file_lock
**conflicting_fl
)
814 struct file_lock
*ifl
; /* Iterator */
815 enum nfslock_status retval
;
817 debuglog("Entering test_nfslock\n");
819 retval
= NFS_GRANTED
;
820 (*conflicting_fl
) = NULL
;
822 debuglog("Entering lock search loop\n");
824 debuglog("***********************************\n");
825 debuglog("Dumping match filelock\n");
826 debuglog("***********************************\n");
828 debuglog("***********************************\n");
830 LIST_FOREACH(ifl
, &nfslocklist_head
, nfslocklist
) {
831 if (retval
== NFS_DENIED
)
834 debuglog("Top of lock loop\n");
835 debuglog("Pointer to file lock: %p\n",ifl
);
837 debuglog("***********************************\n");
838 debuglog("Dumping test filelock\n");
839 debuglog("***********************************\n");
841 debuglog("***********************************\n");
844 * XXX: It is conceivable that someone could use the NLM RPC
845 * system to directly access filehandles. This may be a
846 * security hazard as the filehandle code may bypass normal
847 * file access controls
849 if (fl
->filehandle
.n_len
!= ifl
->filehandle
.n_len
)
851 if (bcmp(fl
->filehandle
.n_bytes
, ifl
->filehandle
.n_bytes
,
852 fl
->filehandle
.n_len
))
855 debuglog("test_nfslock: filehandle match found\n");
857 /* Filehandles match, check for region overlap */
858 if (!regions_overlap(fl
->client
.l_offset
, fl
->client
.l_len
,
859 ifl
->client
.l_offset
, ifl
->client
.l_len
))
862 debuglog("test_nfslock: Region overlap found"
863 " %llu : %llu -- %llu : %llu\n",
864 fl
->client
.l_offset
,fl
->client
.l_len
,
865 ifl
->client
.l_offset
,ifl
->client
.l_len
);
867 /* Regions overlap, check the exclusivity */
868 if (!(fl
->client
.exclusive
|| ifl
->client
.exclusive
))
871 debuglog("test_nfslock: Exclusivity failure: %d %d\n",
872 fl
->client
.exclusive
,
873 ifl
->client
.exclusive
);
875 if (same_filelock_identity(fl
,ifl
)) {
876 debuglog("test_nfslock: Duplicate id. Granting\n");
877 (*conflicting_fl
) = ifl
;
878 retval
= NFS_GRANTED_DUPLICATE
;
880 /* locking attempt fails */
881 debuglog("test_nfslock: Lock attempt failed\n");
882 debuglog("Desired lock\n");
884 debuglog("Conflicting lock\n");
886 (*conflicting_fl
) = ifl
;
891 debuglog("Dumping file locks\n");
892 debuglog("Exiting test_nfslock\n");
898 * lock_nfslock: attempt to create a lock in the NFS lock list
900 * This routine tests whether the lock will be granted and then adds
901 * the entry to the lock list if so.
903 * Argument fl gets modified as its list housekeeping entries get modified
904 * upon insertion into the NFS lock list
906 * This routine makes several assumptions:
907 * 1) It is perfectly happy to grant a duplicate lock from the same pid.
908 * While this seems to be intuitively wrong, it is required for proper
909 * Posix semantics during unlock. It is absolutely imperative to not
910 * unlock the main lock before the two child locks are established. Thus,
911 * one has be be able to create duplicate locks over an existing lock
912 * 2) It currently accepts duplicate locks from the same id,pid
916 lock_nfslock(struct file_lock
*fl
)
918 enum nfslock_status retval
;
919 struct file_lock
*dummy_fl
;
923 debuglog("Entering lock_nfslock...\n");
925 retval
= test_nfslock(fl
,&dummy_fl
);
927 if (retval
== NFS_GRANTED
|| retval
== NFS_GRANTED_DUPLICATE
) {
928 debuglog("Inserting lock...\n");
930 LIST_INSERT_HEAD(&nfslocklist_head
, fl
, nfslocklist
);
933 debuglog("Exiting lock_nfslock...\n");
939 * delete_nfslock: delete an NFS lock list entry
941 * This routine is used to delete a lock out of the NFS lock list
942 * without regard to status, underlying locks, regions or anything else
944 * Note that this routine *does not deallocate memory* of the lock.
945 * It just disconnects it from the list. The lock can then be used
946 * by other routines without fear of trashing the list.
950 delete_nfslock(struct file_lock
*fl
)
953 LIST_REMOVE(fl
, nfslocklist
);
955 return (NFS_GRANTED
);
959 split_nfslock(exist_lock
, unlock_lock
, left_lock
, right_lock
)
960 const struct file_lock
*exist_lock
, *unlock_lock
;
961 struct file_lock
**left_lock
, **right_lock
;
963 u_int64_t start1
, len1
, start2
, len2
;
964 enum split_status spstatus
;
966 spstatus
= region_compare(exist_lock
->client
.l_offset
, exist_lock
->client
.l_len
,
967 unlock_lock
->client
.l_offset
, unlock_lock
->client
.l_len
,
968 &start1
, &len1
, &start2
, &len2
);
970 if ((spstatus
& SPL_LOCK1
) != 0) {
971 *left_lock
= allocate_file_lock(&exist_lock
->client
.oh
, &exist_lock
->filehandle
, exist_lock
->addr
, exist_lock
->client_name
);
972 if (*left_lock
== NULL
) {
973 debuglog("Unable to allocate resource for split 1\n");
977 fill_file_lock(*left_lock
,
978 exist_lock
->client
.exclusive
, exist_lock
->client
.svid
,
980 exist_lock
->nsm_status
,
981 exist_lock
->status
, exist_lock
->flags
, exist_lock
->blocking
);
984 if ((spstatus
& SPL_LOCK2
) != 0) {
985 *right_lock
= allocate_file_lock(&exist_lock
->client
.oh
, &exist_lock
->filehandle
, exist_lock
->addr
, exist_lock
->client_name
);
986 if (*right_lock
== NULL
) {
987 debuglog("Unable to allocate resource for split 1\n");
988 if (*left_lock
!= NULL
) {
989 deallocate_file_lock(*left_lock
);
994 fill_file_lock(*right_lock
,
995 exist_lock
->client
.exclusive
, exist_lock
->client
.svid
,
997 exist_lock
->nsm_status
,
998 exist_lock
->status
, exist_lock
->flags
, exist_lock
->blocking
);
1005 unlock_nfslock(fl
, released_lock
, left_lock
, right_lock
)
1006 const struct file_lock
*fl
;
1007 struct file_lock
**released_lock
;
1008 struct file_lock
**left_lock
;
1009 struct file_lock
**right_lock
;
1011 struct file_lock
*mfl
; /* Matching file lock */
1012 enum nfslock_status retval
;
1013 enum split_status spstatus
;
1015 debuglog("Entering unlock_nfslock\n");
1017 *released_lock
= NULL
;
1021 retval
= NFS_DENIED_NOLOCK
;
1023 debuglog("Attempting to match lock...\n");
1024 mfl
= get_lock_matching_unlock(fl
);
1027 debuglog("Unlock matched. Querying for split\n");
1029 spstatus
= split_nfslock(mfl
, fl
, left_lock
, right_lock
);
1031 debuglog("Split returned %d %p %p %p %p\n",spstatus
,mfl
,fl
,*left_lock
,*right_lock
);
1032 debuglog("********Split dumps********");
1035 dump_filelock(*left_lock
);
1036 dump_filelock(*right_lock
);
1037 debuglog("********End Split dumps********");
1039 if (spstatus
== SPL_RESERR
) {
1040 if (*left_lock
!= NULL
) {
1041 deallocate_file_lock(*left_lock
);
1045 if (*right_lock
!= NULL
) {
1046 deallocate_file_lock(*right_lock
);
1053 /* Insert new locks from split if required */
1054 if (*left_lock
!= NULL
) {
1055 debuglog("Split left activated\n");
1056 LIST_INSERT_HEAD(&nfslocklist_head
, *left_lock
, nfslocklist
);
1059 if (*right_lock
!= NULL
) {
1060 debuglog("Split right activated\n");
1061 LIST_INSERT_HEAD(&nfslocklist_head
, *right_lock
, nfslocklist
);
1064 /* Unlock the lock since it matches identity */
1065 LIST_REMOVE(mfl
, nfslocklist
);
1066 *released_lock
= mfl
;
1067 retval
= NFS_GRANTED
;
1070 debuglog("Exiting unlock_nfslock\n");
1076 * Below here are the routines for manipulating the file lock directly
1077 * on the disk hardware itself
1080 lock_hwlock(struct file_lock
*fl
)
1082 struct monfile
*imf
,*nmf
;
1083 int lflags
, flerror
;
1086 /* Scan to see if filehandle already present */
1087 LIST_FOREACH(imf
, &monfilelist_head
, monfilelist
) {
1088 if ((fl
->filehandle
.n_len
== imf
->filehandle
.n_len
) &&
1089 (bcmp(fl
->filehandle
.n_bytes
, imf
->filehandle
.n_bytes
,
1090 fl
->filehandle
.n_len
) == 0)) {
1091 /* imf is the correct filehandle */
1097 * Filehandle already exists (we control the file)
1098 * *AND* NFS has already cleared the lock for availability
1099 * Grant it and bump the refcount.
1103 return (HW_GRANTED
);
1106 /* No filehandle found, create and go */
1107 nmf
= malloc(sizeof(struct monfile
));
1109 debuglog("hwlock resource allocation failure\n");
1112 nmf
->filehandle
.n_bytes
= malloc(fl
->filehandle
.n_len
);
1113 if (nmf
->filehandle
.n_bytes
== NULL
) {
1114 debuglog("hwlock resource allocation failure\n");
1119 if (fl
->filehandle
.n_len
> NFS_MAX_FH_SIZE
) {
1120 debuglog("hwlock: bad fh length %d (from %16s): %32s\n",
1121 fl
->filehandle
.n_len
, fl
->client_name
, strerror(errno
));
1122 free(nmf
->filehandle
.n_bytes
);
1124 return (HW_STALEFH
);
1126 fh
.fh_len
= fl
->filehandle
.n_len
;
1127 bcopy(fl
->filehandle
.n_bytes
, fh
.fh_data
, fh
.fh_len
);
1129 /* XXX: Is O_RDWR always the correct mode? */
1130 nmf
->fd
= fhopen(&fh
, O_RDWR
);
1132 debuglog("fhopen failed (from %16s): %32s\n",
1133 fl
->client_name
, strerror(errno
));
1134 free(nmf
->filehandle
.n_bytes
);
1138 return (HW_STALEFH
);
1140 return (HW_READONLY
);
1146 /* File opened correctly, fill the monitor struct */
1147 nmf
->filehandle
.n_len
= fl
->filehandle
.n_len
;
1148 bcopy(fl
->filehandle
.n_bytes
, nmf
->filehandle
.n_bytes
, fl
->filehandle
.n_len
);
1150 nmf
->exclusive
= fl
->client
.exclusive
;
1152 lflags
= (nmf
->exclusive
== 1) ?
1153 (LOCK_EX
| LOCK_NB
) : (LOCK_SH
| LOCK_NB
);
1155 flerror
= flock(nmf
->fd
, lflags
);
1158 debuglog("flock failed (from %16s): %32s\n",
1159 fl
->client_name
, strerror(errno
));
1161 free(nmf
->filehandle
.n_bytes
);
1167 return (HW_STALEFH
);
1169 return (HW_READONLY
);
1176 /* File opened and locked */
1177 LIST_INSERT_HEAD(&monfilelist_head
, nmf
, monfilelist
);
1179 debuglog("flock succeeded (from %16s)\n", fl
->client_name
);
1180 return (HW_GRANTED
);
1184 unlock_hwlock(const struct file_lock
*fl
)
1186 struct monfile
*imf
;
1188 debuglog("Entering unlock_hwlock\n");
1189 debuglog("Entering loop interation\n");
1191 /* Scan to see if filehandle already present */
1192 LIST_FOREACH(imf
, &monfilelist_head
, monfilelist
) {
1193 if ((fl
->filehandle
.n_len
== imf
->filehandle
.n_len
) &&
1194 (bcmp(fl
->filehandle
.n_bytes
, imf
->filehandle
.n_bytes
,
1195 fl
->filehandle
.n_len
) == 0)) {
1196 /* imf is the correct filehandle */
1201 debuglog("Completed iteration. Proceeding\n");
1205 debuglog("Exiting unlock_hwlock (HW_DENIED_NOLOCK)\n");
1206 return (HW_DENIED_NOLOCK
);
1212 if (imf
->refcount
< 0) {
1213 debuglog("Negative hardware reference count\n");
1216 if (imf
->refcount
<= 0) {
1218 LIST_REMOVE(imf
, monfilelist
);
1219 free(imf
->filehandle
.n_bytes
);
1222 debuglog("Exiting unlock_hwlock (HW_GRANTED)\n");
1223 return (HW_GRANTED
);
1227 test_hwlock(fl
, conflicting_fl
)
1228 const struct file_lock
*fl __unused
;
1229 struct file_lock
**conflicting_fl __unused
;
1233 * XXX: lock tests on hardware are not required until
1234 * true partial file testing is done on the underlying file
1242 * Below here are routines for manipulating blocked lock requests
1243 * They should only be called from the XXX_partialfilelock routines
1244 * if at all possible
1248 add_blockingfilelock(struct file_lock
*fl
)
1250 struct file_lock
*ifl
, *nfl
;
1252 debuglog("Entering add_blockingfilelock\n");
1255 * Check for a duplicate lock request.
1256 * If found, deallocate the older request.
1258 ifl
= LIST_FIRST(&blockedlocklist_head
);
1259 for (; ifl
!= NULL
; ifl
= nfl
) {
1260 debuglog("Pointer to file lock: %p\n",ifl
);
1261 debuglog("****Dump of ifl****\n");
1263 debuglog("*******************\n");
1265 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1267 if (fl
->filehandle
.n_len
!= ifl
->filehandle
.n_len
)
1269 if (bcmp(fl
->filehandle
.n_bytes
, ifl
->filehandle
.n_bytes
,
1270 fl
->filehandle
.n_len
))
1273 /* Filehandles match, check region */
1274 if ((fl
->client
.l_offset
!= ifl
->client
.l_offset
) ||
1275 (fl
->client
.l_len
!= ifl
->client
.l_len
))
1278 /* Regions match, check the identity */
1279 if (!same_filelock_identity(fl
,ifl
))
1282 debuglog("add_blockingfilelock: removing duplicate lock request.\n");
1283 remove_blockingfilelock(ifl
);
1284 deallocate_file_lock(ifl
);
1289 * Clear the blocking flag so that it can be reused without
1290 * adding it to the blocking queue a second time
1294 LIST_INSERT_HEAD(&blockedlocklist_head
, fl
, nfslocklist
);
1296 debuglog("Exiting add_blockingfilelock\n");
1300 remove_blockingfilelock(struct file_lock
*fl
)
1303 debuglog("Entering remove_blockingfilelock\n");
1305 LIST_REMOVE(fl
, nfslocklist
);
1307 debuglog("Exiting remove_blockingfilelock\n");
1311 clear_blockingfilelock(const char *hostname
)
1313 struct file_lock
*ifl
,*nfl
;
1316 * Normally, LIST_FOREACH is called for, but since
1317 * the current element *is* the iterator, deleting it
1318 * would mess up the iteration. Thus, a next element
1319 * must be used explicitly
1322 ifl
= LIST_FIRST(&blockedlocklist_head
);
1324 while (ifl
!= NULL
) {
1325 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1327 if (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0) {
1328 remove_blockingfilelock(ifl
);
1329 deallocate_file_lock(ifl
);
1336 int need_retry_blocked_locks
= 0; /* need to call retry_blockingfilelocklist() */
1339 retry_blockingfilelocklist(netobj
*fh
)
1342 * If fh is given, then retry just the locks with the
1343 * same filehandle in the blocked list.
1344 * Otherwise, simply retry all locks in the blocked list.
1346 struct file_lock
*ifl
, *nfl
, *pfl
; /* Iterator */
1347 enum partialfilelock_status pflstatus
;
1350 debuglog("Entering retry_blockingfilelocklist\n");
1352 need_retry_blocked_locks
= 0;
1355 ifl
= LIST_FIRST(&blockedlocklist_head
);
1356 debuglog("Iterator choice %p\n",ifl
);
1358 while (ifl
!= NULL
) {
1360 * SUBTLE BUG: The next element must be worked out before the
1361 * current element has been moved
1363 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1364 debuglog("Iterator choice %p\n",ifl
);
1365 debuglog("Prev iterator choice %p\n",pfl
);
1366 debuglog("Next iterator choice %p\n",nfl
);
1368 /* if given a filehandle, only retry locks for the same filehandle */
1369 if (fh
&& !same_netobj(fh
, &ifl
->filehandle
)) {
1375 * SUBTLE BUG: The file_lock must be removed from the
1376 * old list so that it's list pointers get disconnected
1377 * before being allowed to participate in the new list
1378 * which will automatically add it in if necessary.
1381 LIST_REMOVE(ifl
, nfslocklist
);
1382 pflstatus
= lock_partialfilelock(ifl
);
1384 if (pflstatus
== PFL_GRANTED
|| pflstatus
== PFL_GRANTED_DUPLICATE
) {
1385 debuglog("Granted blocked lock\n");
1386 /* lock granted and is now being used */
1387 rv
= send_granted(ifl
, 0);
1390 * Uh oh... the NLM_GRANTED message failed.
1391 * About the only thing we can do is drop the lock.
1392 * Note: this could be bad if the error was only
1393 * transient. Hopefully, if the client is still
1394 * waiting for the lock, they will resend the request.
1397 /* ifl is NO LONGER VALID AT THIS POINT */
1399 } else if (pflstatus
== PFL_HWDENIED_STALEFH
) {
1402 * It would be nice if we could inform the client of
1403 * this error. Unfortunately, there's no way to do
1404 * that in the NLM protocol (can't send "granted"
1405 * message with an error and there's no "never going
1406 * to be granted" message).
1408 * Since there's no chance of this blocked request ever
1409 * succeeding, we drop the lock request rather than
1410 * needlessly keeping it around just to rot forever in
1411 * the blocked lock list.
1413 * Hopefully, if the client is still waiting for the lock,
1414 * they will resend the request (and get an error then).
1416 * XXX Note: PFL_HWDENIED_READONLY could potentially
1417 * be handled this way as well, although that would
1418 * only be an issue if a file system changed from
1419 * read-write to read-only out from under a blocked
1420 * lock request, and that's far less likely than a
1421 * file disappearing out from under such a request.
1423 deallocate_file_lock(ifl
);
1424 /* ifl is NO LONGER VALID AT THIS POINT */
1426 /* Reinsert lock back into same place in blocked list */
1427 debuglog("Replacing blocked lock\n");
1429 LIST_INSERT_AFTER(pfl
, ifl
, nfslocklist
);
1431 /* ifl is the only elem. in the list */
1432 LIST_INSERT_HEAD(&blockedlocklist_head
, ifl
, nfslocklist
);
1435 if (pflstatus
== PFL_GRANTED
|| pflstatus
== PFL_GRANTED_DUPLICATE
||
1436 pflstatus
== PFL_HWDENIED_STALEFH
) {
1437 /* If ifl was permanently removed from the list, (e.g it */
1438 /* was granted or dropped), pfl should remain where it's at. */
1440 /* If ifl was left in the list, (e.g it was reinserted back */
1441 /* in place), pfl should simply be moved forward to be ifl */
1444 /* Valid increment behavior regardless of state of ifl */
1448 debuglog("Exiting retry_blockingfilelocklist\n");
1452 * Below here are routines associated with manipulating all
1453 * aspects of the partial file locking system (list, hardware, etc.)
1457 * Please note that lock monitoring must be done at this level which
1458 * keeps track of *individual* lock requests on lock and unlock
1460 * XXX: Split unlocking is going to make the unlock code miserable
1464 * lock_partialfilelock:
1466 * Argument fl gets modified as its list housekeeping entries get modified
1467 * upon insertion into the NFS lock list
1469 * This routine makes several assumptions:
1470 * 1) It (will) pass locks through to flock to lock the entire underlying file
1471 * and then parcel out NFS locks if it gets control of the file.
1472 * This matches the old rpc.lockd file semantics (except where it
1473 * is now more correct). It is the safe solution, but will cause
1474 * overly restrictive blocking if someone is trying to use the
1475 * underlying files without using NFS. This appears to be an
1476 * acceptable tradeoff since most people use standalone NFS servers.
1477 * XXX: The right solution is probably kevent combined with fcntl
1479 * 2) Nothing modifies the lock lists between testing and granting
1480 * I have no idea whether this is a useful assumption or not
1483 enum partialfilelock_status
1484 lock_partialfilelock(struct file_lock
*fl
)
1486 enum partialfilelock_status retval
;
1487 enum nfslock_status lnlstatus
;
1488 enum hwlock_status hwstatus
;
1490 debuglog("Entering lock_partialfilelock\n");
1492 retval
= PFL_DENIED
;
1495 * Execute the NFS lock first, if possible, as it is significantly
1496 * easier and less expensive to undo than the filesystem lock
1499 lnlstatus
= lock_nfslock(fl
);
1501 switch (lnlstatus
) {
1503 case NFS_GRANTED_DUPLICATE
:
1505 * At this point, the NFS lock is allocated and active.
1506 * Remember to clean it up if the hardware lock fails
1508 hwstatus
= lock_hwlock(fl
);
1512 case HW_GRANTED_DUPLICATE
:
1513 debuglog("HW GRANTED\n");
1515 * XXX: Fixme: Check hwstatus for duplicate when
1516 * true partial file locking and accounting is
1517 * done on the hardware
1519 if (lnlstatus
== NFS_GRANTED_DUPLICATE
) {
1520 retval
= PFL_GRANTED_DUPLICATE
;
1522 retval
= PFL_GRANTED
;
1524 if (fl
->flags
& LOCK_MON
)
1525 monitor_lock_host_by_name(fl
->client_name
, fl
->addr
);
1528 debuglog("HW RESERR\n");
1529 retval
= PFL_HWRESERR
;
1532 debuglog("HW DENIED\n");
1533 retval
= PFL_HWDENIED
;
1535 case HW_DENIED_NOLOCK
:
1536 debuglog("HW DENIED NOLOCK\n");
1537 retval
= PFL_HWDENIED_NOLOCK
;
1540 debuglog("HW STALE FH\n");
1541 retval
= PFL_HWDENIED_STALEFH
;
1544 debuglog("HW READONLY\n");
1545 retval
= PFL_HWDENIED_READONLY
;
1548 debuglog("Unmatched hwstatus %d\n",hwstatus
);
1552 if (retval
!= PFL_GRANTED
&&
1553 retval
!= PFL_GRANTED_DUPLICATE
) {
1554 /* Clean up the NFS lock */
1555 debuglog("Deleting trial NFS lock\n");
1560 retval
= PFL_NFSDENIED
;
1563 retval
= PFL_NFSRESERR
;
1565 debuglog("Unmatched lnlstatus %d\n");
1566 retval
= PFL_NFSDENIED_NOLOCK
;
1571 * By the time fl reaches here, it is completely free again on
1572 * failure. The NFS lock done before attempting the
1573 * hardware lock has been backed out
1576 if (retval
== PFL_NFSDENIED
|| retval
== PFL_HWDENIED
) {
1577 /* Once last chance to check the lock */
1578 if (fl
->blocking
== 1) {
1579 if (retval
== PFL_NFSDENIED
) {
1580 /* Queue the lock */
1581 debuglog("BLOCKING LOCK RECEIVED\n");
1582 retval
= PFL_NFSBLOCKED
;
1583 add_blockingfilelock(fl
);
1586 /* retval is okay as PFL_HWDENIED */
1587 debuglog("BLOCKING LOCK DENIED IN HARDWARE\n");
1591 /* Leave retval alone, it's already correct */
1592 debuglog("Lock denied. Non-blocking failure\n");
1597 debuglog("Exiting lock_partialfilelock\n");
1603 * unlock_partialfilelock:
1605 * Given a file_lock, unlock all locks which match.
1607 * Note that a given lock might have to unlock ITSELF! See
1608 * clear_partialfilelock for example.
1611 enum partialfilelock_status
1612 unlock_partialfilelock(const struct file_lock
*fl
)
1614 struct file_lock
*lfl
,*rfl
,*releasedfl
,*selffl
;
1615 enum partialfilelock_status retval
;
1616 enum nfslock_status unlstatus
;
1617 enum hwlock_status unlhwstatus
, lhwstatus
;
1619 debuglog("Entering unlock_partialfilelock\n");
1625 retval
= PFL_DENIED
;
1628 * There are significant overlap and atomicity issues
1629 * with partially releasing a lock. For example, releasing
1630 * part of an NFS shared lock does *not* always release the
1631 * corresponding part of the file since there is only one
1632 * rpc.lockd UID but multiple users could be requesting it
1633 * from NFS. Also, an unlock request should never allow
1634 * another process to gain a lock on the remaining parts.
1635 * ie. Always apply the new locks before releasing the
1640 * Loop is required since multiple little locks
1641 * can be allocated and then deallocated with one
1644 * The loop is required to be here so that the nfs &
1645 * hw subsystems do not need to communicate with one
1650 debuglog("Value of releasedfl: %p\n",releasedfl
);
1651 /* lfl&rfl are created *AND* placed into the NFS lock list if required */
1652 unlstatus
= unlock_nfslock(fl
, &releasedfl
, &lfl
, &rfl
);
1653 debuglog("Value of releasedfl: %p\n",releasedfl
);
1656 /* XXX: This is grungy. It should be refactored to be cleaner */
1658 lhwstatus
= lock_hwlock(lfl
);
1659 if (lhwstatus
!= HW_GRANTED
&&
1660 lhwstatus
!= HW_GRANTED_DUPLICATE
) {
1661 debuglog("HW duplicate lock failure for left split\n");
1663 if (lfl
->flags
& LOCK_MON
)
1664 monitor_lock_host_by_name(lfl
->client_name
, lfl
->addr
);
1668 lhwstatus
= lock_hwlock(rfl
);
1669 if (lhwstatus
!= HW_GRANTED
&&
1670 lhwstatus
!= HW_GRANTED_DUPLICATE
) {
1671 debuglog("HW duplicate lock failure for right split\n");
1673 if (rfl
->flags
& LOCK_MON
)
1674 monitor_lock_host_by_name(rfl
->client_name
, rfl
->addr
);
1677 switch (unlstatus
) {
1679 /* Attempt to unlock on the hardware */
1680 debuglog("NFS unlock granted. Attempting hardware unlock\n");
1682 /* This call *MUST NOT* unlock the two newly allocated locks */
1683 unlhwstatus
= unlock_hwlock(fl
);
1684 debuglog("HW unlock returned with code %d\n",unlhwstatus
);
1686 switch (unlhwstatus
) {
1688 debuglog("HW unlock granted\n");
1689 if (releasedfl
->flags
& LOCK_MON
)
1690 unmonitor_lock_host(releasedfl
->client_name
);
1691 retval
= PFL_GRANTED
;
1693 case HW_DENIED_NOLOCK
:
1694 /* Huh?!?! This shouldn't happen */
1695 debuglog("HW unlock denied no lock\n");
1696 retval
= PFL_HWRESERR
;
1697 /* Break out of do-while */
1698 unlstatus
= NFS_RESERR
;
1701 debuglog("HW unlock failed\n");
1702 retval
= PFL_HWRESERR
;
1703 /* Break out of do-while */
1704 unlstatus
= NFS_RESERR
;
1708 debuglog("Exiting with status retval: %d\n",retval
);
1710 // XXX sending granted messages before unlock response
1711 // XXX causes unlock response to be corrupted?
1712 // XXX Workaround is to move this to nlm_prot_svc.c
1713 // XXX after the unlock response is sent.
1714 // retry_blockingfilelocklist();
1715 need_retry_blocked_locks
= 1;
1717 case NFS_DENIED_NOLOCK
:
1718 retval
= PFL_GRANTED
;
1719 debuglog("All locks cleaned out\n");
1722 retval
= PFL_NFSRESERR
;
1723 debuglog("NFS unlock failure\n");
1728 if (releasedfl
!= NULL
) {
1729 if (fl
== releasedfl
) {
1731 * XXX: YECHHH!!! Attempt to unlock self succeeded
1732 * but we can't deallocate the space yet. This is what
1733 * happens when you don't write malloc and free together
1735 debuglog("Attempt to unlock self\n");
1736 selffl
= releasedfl
;
1739 * XXX: this deallocation *still* needs to migrate closer
1740 * to the allocation code way up in get_lock or the allocation
1741 * code needs to migrate down (violation of "When you write
1742 * malloc you must write free")
1745 deallocate_file_lock(releasedfl
);
1749 } while (unlstatus
== NFS_GRANTED
);
1751 if (selffl
!= NULL
) {
1753 * This statement wipes out the incoming file lock (fl)
1754 * in spite of the fact that it is declared const
1756 debuglog("WARNING! Destroying incoming lock pointer\n");
1757 deallocate_file_lock(selffl
);
1760 debuglog("Exiting unlock_partialfilelock\n");
1766 * clear_partialfilelock
1768 * Normally called in response to statd state number change.
1769 * Wipe out all locks held by a host. As a bonus, the act of
1770 * doing so should automatically clear their statd entries and
1771 * unmonitor the host.
1775 clear_partialfilelock(const char *hostname
)
1777 struct file_lock
*ifl
, *nfl
;
1778 enum partialfilelock_status pfsret
;
1782 * Check if the name we got from statd is
1783 * actually one reverse-mapped from the client's
1784 * address. If so, use the name provided as
1785 * the caller_name in lock requests instead so that
1786 * we can correctly identify the client's locks.
1788 TAILQ_FOREACH(ihp
, &hostlst_head
, hostlst
) {
1789 if (ihp
->revname
&& strncmp(hostname
, ihp
->revname
,
1790 SM_MAXSTRLEN
) == 0) {
1791 hostname
= ihp
->name
;
1792 debuglog("Clearing locks for %s (%s)\n",
1793 hostname
, ihp
->revname
);
1798 /* Clear blocking file lock list */
1799 clear_blockingfilelock(hostname
);
1801 /* do all required unlocks */
1802 /* Note that unlock can smash the current pointer to a lock */
1805 * Normally, LIST_FOREACH is called for, but since
1806 * the current element *is* the iterator, deleting it
1807 * would mess up the iteration. Thus, a next element
1808 * must be used explicitly
1811 ifl
= LIST_FIRST(&nfslocklist_head
);
1813 while (ifl
!= NULL
) {
1814 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1816 if (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0) {
1817 /* Unlock destroys ifl out from underneath */
1818 pfsret
= unlock_partialfilelock(ifl
);
1819 if (pfsret
!= PFL_GRANTED
) {
1820 /* Uh oh... there was some sort of problem. */
1821 /* If we restart the loop, we may get */
1822 /* stuck here forever getting errors. */
1823 /* So, let's just abort the whole scan. */
1824 syslog(LOG_WARNING
, "lock clearing for %s failed: %d",
1828 /* ifl is NO LONGER VALID AT THIS POINT */
1829 /* Note: the unlock may deallocate several existing locks. */
1830 /* Therefore, we need to restart the scanning of the list, */
1831 /* because nfl could be pointing to a freed lock. */
1839 * test_partialfilelock:
1841 enum partialfilelock_status
1842 test_partialfilelock(const struct file_lock
*fl
,
1843 struct file_lock
**conflicting_fl
)
1845 enum partialfilelock_status retval
;
1846 enum nfslock_status teststatus
;
1848 debuglog("Entering testpartialfilelock...\n");
1850 retval
= PFL_DENIED
;
1852 teststatus
= test_nfslock(fl
, conflicting_fl
);
1853 debuglog("test_partialfilelock: teststatus %d\n",teststatus
);
1855 if (teststatus
== NFS_GRANTED
|| teststatus
== NFS_GRANTED_DUPLICATE
) {
1856 /* XXX: Add the underlying filesystem locking code */
1857 retval
= (teststatus
== NFS_GRANTED
) ?
1858 PFL_GRANTED
: PFL_GRANTED_DUPLICATE
;
1859 debuglog("Dumping locks...\n");
1861 dump_filelock(*conflicting_fl
);
1862 debuglog("Done dumping locks...\n");
1864 retval
= PFL_NFSDENIED
;
1865 debuglog("NFS test denied.\n");
1867 debuglog("Conflicting.\n");
1868 dump_filelock(*conflicting_fl
);
1871 debuglog("Exiting testpartialfilelock...\n");
1877 * Below here are routines associated with translating the partial file locking
1878 * codes into useful codes to send back to the NFS RPC messaging system
1882 * These routines translate the (relatively) useful return codes back onto
1883 * the few return codes which the nlm subsystems wishes to trasmit
1887 do_test(struct file_lock
*fl
, struct file_lock
**conflicting_fl
)
1889 enum partialfilelock_status pfsret
;
1890 enum nlm4_stats retval
;
1892 debuglog("Entering do_test...\n");
1894 pfsret
= test_partialfilelock(fl
,conflicting_fl
);
1898 debuglog("PFL test lock granted\n");
1900 dump_filelock(*conflicting_fl
);
1901 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1903 case PFL_GRANTED_DUPLICATE
:
1904 debuglog("PFL test lock granted--duplicate id detected\n");
1906 dump_filelock(*conflicting_fl
);
1907 debuglog("Clearing conflicting_fl for call semantics\n");
1908 *conflicting_fl
= NULL
;
1909 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1913 debuglog("PFL test lock denied\n");
1915 dump_filelock(*conflicting_fl
);
1916 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
1920 debuglog("PFL test lock resource fail\n");
1922 dump_filelock(*conflicting_fl
);
1923 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
1926 debuglog("PFL test lock *FAILED*\n");
1928 dump_filelock(*conflicting_fl
);
1929 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
1933 debuglog("Exiting do_test...\n");
1939 * do_lock: Try to acquire a lock
1941 * This routine makes a distinction between NLM versions. I am pretty
1942 * convinced that this should be abstracted out and bounced up a level
1946 do_lock(struct file_lock
*fl
)
1948 enum partialfilelock_status pfsret
;
1949 enum nlm4_stats retval
;
1951 debuglog("Entering do_lock...\n");
1953 pfsret
= lock_partialfilelock(fl
);
1957 debuglog("PFL lock granted");
1959 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1961 case PFL_GRANTED_DUPLICATE
:
1962 debuglog("PFL lock granted--duplicate id detected");
1964 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1968 debuglog("PFL_NFS lock denied");
1970 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
1972 case PFL_NFSBLOCKED
:
1974 debuglog("PFL_NFS blocking lock denied. Queued.\n");
1976 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_blocked
: nlm_blocked
;
1980 case PFL_NFSDENIED_NOLOCK
:
1981 case PFL_HWDENIED_NOLOCK
:
1982 debuglog("PFL lock resource alocation fail\n");
1984 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
1986 case PFL_HWDENIED_STALEFH
:
1987 debuglog("PFL_NFS lock denied STALEFH");
1989 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_stale_fh
: nlm_denied
;
1991 case PFL_HWDENIED_READONLY
:
1992 debuglog("PFL_NFS lock denied READONLY");
1994 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_rofs
: nlm_denied
;
1997 debuglog("PFL lock *FAILED*");
1999 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2003 debuglog("Exiting do_lock...\n");
2009 do_unlock(struct file_lock
*fl
)
2011 enum partialfilelock_status pfsret
;
2012 enum nlm4_stats retval
;
2014 debuglog("Entering do_unlock...\n");
2015 pfsret
= unlock_partialfilelock(fl
);
2019 debuglog("PFL unlock granted");
2021 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2025 debuglog("PFL_NFS unlock denied");
2027 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
2029 case PFL_NFSDENIED_NOLOCK
:
2030 case PFL_HWDENIED_NOLOCK
:
2031 debuglog("PFL_NFS no lock found\n");
2032 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2036 debuglog("PFL unlock resource failure");
2038 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
2041 debuglog("PFL unlock *FAILED*");
2043 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2047 debuglog("Exiting do_unlock...\n");
2055 * This routine is non-existent because it doesn't have a return code.
2056 * It is here for completeness in case someone *does* need to do return
2057 * codes later. A decent compiler should optimize this away.
2061 do_clear(const char *hostname
)
2064 clear_partialfilelock(hostname
);
2068 * The following routines are all called from the code which the
2073 * testlock(): inform the caller if the requested lock would be granted
2075 * returns NULL if lock would granted
2076 * returns pointer to a conflicting nlm4_holder if not
2079 struct nlm4_holder
*
2080 testlock(struct nlm4_lock
*lock
, bool_t exclusive
, int flags __unused
)
2082 struct file_lock test_fl
, *conflicting_fl
;
2084 if (lock
->fh
.n_len
> NFS_MAX_FH_SIZE
) {
2085 debuglog("received fhandle size %d, max size %d",
2086 lock
->fh
.n_len
, NFS_MAX_FH_SIZE
);
2090 bzero(&test_fl
, sizeof(test_fl
));
2092 test_fl
.filehandle
.n_len
= lock
->fh
.n_len
;
2093 test_fl
.filehandle
.n_bytes
= lock
->fh
.n_bytes
;
2094 copy_nlm4_lock_to_nlm4_holder(lock
, exclusive
, &test_fl
.client
);
2097 do_test(&test_fl
, &conflicting_fl
);
2099 if (conflicting_fl
== NULL
) {
2100 debuglog("No conflicting lock found\n");
2104 debuglog("Found conflicting lock\n");
2105 dump_filelock(conflicting_fl
);
2107 return (&conflicting_fl
->client
);
2112 * getlock: try to aquire the lock.
2113 * If file is already locked and we can sleep, put the lock in the list with
2114 * status LKST_WAITING; it'll be processed later.
2115 * Otherwise try to lock. If we're allowed to block, fork a child which
2116 * will do the blocking lock.
2120 getlock(nlm4_lockargs
*lckarg
, struct svc_req
*rqstp
, const int flags
)
2122 struct file_lock
*newfl
;
2123 enum nlm4_stats retval
;
2125 debuglog("Entering getlock...\n");
2127 if (grace_expired
== 0 && lckarg
->reclaim
== 0)
2128 return (flags
& LOCK_V4
) ?
2129 nlm4_denied_grace_period
: nlm_denied_grace_period
;
2131 if (lckarg
->alock
.fh
.n_len
> NFS_MAX_FH_SIZE
) {
2132 debuglog("received fhandle size %d, max size %d",
2133 lckarg
->alock
.fh
.n_len
, NFS_MAX_FH_SIZE
);
2134 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2137 /* allocate new file_lock for this request */
2138 newfl
= allocate_file_lock(&lckarg
->alock
.oh
, &lckarg
->alock
.fh
,
2139 (struct sockaddr
*)svc_getcaller(rqstp
->rq_xprt
),
2140 lckarg
->alock
.caller_name
);
2141 if (newfl
== NULL
) {
2142 syslog(LOG_NOTICE
, "lock allocate failed: %s", strerror(errno
));
2144 return (flags
& LOCK_V4
) ?
2145 nlm4_denied_nolocks
: nlm_denied_nolocks
;
2148 fill_file_lock(newfl
,
2149 lckarg
->exclusive
, lckarg
->alock
.svid
, lckarg
->alock
.l_offset
,
2150 lckarg
->alock
.l_len
,
2151 lckarg
->state
, 0, flags
, lckarg
->block
);
2154 * newfl is now fully constructed and deallocate_file_lock
2155 * can now be used to delete it
2159 debuglog("Pointer to new lock is %p\n",newfl
);
2161 retval
= do_lock(newfl
);
2163 debuglog("Pointer to new lock is %p\n",newfl
);
2169 /* case nlm_granted: is the same as nlm4_granted */
2170 /* do_mon(lckarg->alock.caller_name); */
2173 /* case nlm_blocked: is the same as nlm4_blocked */
2174 /* do_mon(lckarg->alock.caller_name); */
2177 deallocate_file_lock(newfl
);
2181 debuglog("Exiting getlock...\n");
2187 /* unlock a filehandle */
2189 unlock(nlm4_lock
*lock
, const int flags
)
2191 struct file_lock fl
;
2192 enum nlm4_stats err
;
2194 debuglog("Entering unlock...\n");
2196 if (lock
->fh
.n_len
> NFS_MAX_FH_SIZE
) {
2197 debuglog("received fhandle size %d, max size %d",
2198 lock
->fh
.n_len
, NFS_MAX_FH_SIZE
);
2199 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2204 bzero(&fl
,sizeof(struct file_lock
));
2205 fl
.filehandle
.n_len
= lock
->fh
.n_len
;
2206 fl
.filehandle
.n_bytes
= lock
->fh
.n_bytes
;
2208 copy_nlm4_lock_to_nlm4_holder(lock
, 0, &fl
.client
);
2210 err
= do_unlock(&fl
);
2214 debuglog("Exiting unlock...\n");
2219 /* cancel a blocked lock request */
2221 cancellock(nlm4_cancargs
*args
, const int flags
)
2223 struct file_lock
*ifl
, *nfl
;
2224 enum nlm4_stats err
;
2226 debuglog("Entering cancellock...\n");
2228 if (args
->alock
.fh
.n_len
> NFS_MAX_FH_SIZE
) {
2229 debuglog("received fhandle size %d, max size %d",
2230 args
->alock
.fh
.n_len
, NFS_MAX_FH_SIZE
);
2231 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2236 err
= (flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
2239 * scan blocked lock list for matching request and remove/destroy
2241 ifl
= LIST_FIRST(&blockedlocklist_head
);
2242 for ( ; ifl
!= NULL
; ifl
= nfl
) {
2243 nfl
= LIST_NEXT(ifl
, nfslocklist
);
2245 /* compare lock fh - filehandle */
2246 if (!same_netobj(&args
->alock
.fh
, &ifl
->filehandle
))
2249 /* compare lock caller_name - client_name */
2250 if (strncmp(args
->alock
.caller_name
, ifl
->client_name
, SM_MAXSTRLEN
))
2253 /* Note: done't compare cookie - client_cookie */
2254 /* The cookie may be specific to the cancel request */
2255 /* and not be the same as the one in the original lock request. */
2257 /* compare lock oh - client.oh */
2258 if (!same_netobj(&args
->alock
.oh
, &ifl
->client
.oh
))
2261 /* compare lock svid - client.svid */
2262 if (args
->alock
.svid
!= ifl
->client
.svid
)
2265 /* compare lock l_offset - client.l_offset */
2266 if (args
->alock
.l_offset
!= ifl
->client
.l_offset
)
2269 /* compare lock l_len - client.l_len */
2270 if (args
->alock
.l_len
!= ifl
->client
.l_len
)
2273 /* compare exclusive - client.exclusive */
2274 if (args
->exclusive
!= ifl
->client
.exclusive
)
2278 remove_blockingfilelock(ifl
);
2279 deallocate_file_lock(ifl
);
2280 err
= (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2286 debuglog("Exiting cancellock...\n");
2293 * XXX: The following monitor/unmonitor routines
2294 * have not been extensively tested (ie. no regression
2295 * script exists like for the locking sections)
2299 * Find a lock host on a queue. If found:
2301 * bump the access time,
2302 * dequeue it from the queue it was found on,
2303 * enqueue it at the front of the "in use" queue.
2306 get_lock_host(struct hostlst_head
*hd
, const char *hostname
, const struct sockaddr
*saddr
)
2310 if (!hostname
&& !saddr
)
2313 debuglog("get_lock_host %s\n", hostname
? hostname
: "addr");
2314 TAILQ_FOREACH(ihp
, hd
, hostlst
) {
2315 if (hostname
&& (strncmp(hostname
, ihp
->name
, SM_MAXSTRLEN
) != 0))
2317 if (saddr
&& addrcmp(saddr
, &ihp
->addr
))
2319 TAILQ_REMOVE(hd
, ihp
, hostlst
);
2321 * Host is already monitored, so just bump the
2322 * reference count. But don't bump the reference
2323 * count if we're adding additional client-side
2324 * references. Client-side monitors are done by
2325 * address, are never unmonitored, and should only
2326 * take one refcount. Otherwise, repeated calls
2327 * could cause the refcount to wrap.
2329 if (!saddr
|| !ihp
->addr
.sa_len
)
2331 ihp
->lastuse
= currsec
;
2332 /* Host should only be in the monitor list once */
2333 TAILQ_INSERT_HEAD(&hostlst_head
, ihp
, hostlst
);
2336 debuglog("get_lock_host %s %s\n",
2337 ihp
== NULL
? "did not find" : "found", hostname
? hostname
: "addr");
2342 * monitor_lock_host: monitor lock hosts locally with a ref count and
2346 monitor_lock_host_by_name(const char *hostname
, const struct sockaddr
*saddr
)
2350 debuglog("monitor_lock_host: %s\n", hostname
);
2351 ihp
= get_lock_host(&hostlst_head
, hostname
, NULL
);
2353 ihp
= get_lock_host(&hostlst_unref
, hostname
, NULL
);
2356 debuglog("Monitor_lock_host: %s - %s (cached)\n",
2357 ihp
->name
, ihp
->revname
);
2359 debuglog("Monitor_lock_host: %s (cached)\n", ihp
->name
);
2363 monitor_lock_host(hostname
, saddr
);
2367 monitor_lock_host_by_addr(const struct sockaddr
*saddr
)
2371 char hostaddr
[SM_MAXSTRLEN
];
2372 struct sockaddr_in
*sin
= (struct sockaddr_in
*)saddr
;
2374 if (getnameinfo(saddr
, saddr
->sa_len
, hostaddr
, sizeof(hostaddr
),
2375 NULL
, 0, NI_NUMERICHOST
)) {
2376 debuglog("monitor_lock_host: bad address\n");
2379 debuglog("monitor_lock_host: %s\n", hostaddr
);
2380 ihp
= get_lock_host(&hostlst_head
, NULL
, saddr
);
2382 ihp
= get_lock_host(&hostlst_unref
, NULL
, saddr
);
2384 debuglog("Monitor_lock_host: %s (cached)\n", ihp
->name
);
2388 hp
= gethostbyaddr((char*)&sin
->sin_addr
, sizeof(sin
->sin_addr
), AF_INET
);
2390 monitor_lock_host(hp
->h_name
, saddr
);
2392 // herror(hostaddr);
2393 monitor_lock_host(hostaddr
, saddr
);
2398 monitor_lock_host(const char *hostname
, const struct sockaddr
*saddr
)
2401 struct hostent
*hp
= NULL
;
2403 struct sm_stat_res sres
;
2407 struct sockaddr_in
*sin
= (struct sockaddr_in
*) saddr
;
2411 /* Host is not yet monitored, add it */
2412 debuglog("Monitor_lock_host: %s (creating)\n", hostname
);
2413 n
= strnlen(hostname
, SM_MAXSTRLEN
);
2414 if (n
== SM_MAXSTRLEN
) {
2415 debuglog("monitor_lock_host: hostname too long\n");
2418 nhp
= (struct host
*) malloc(sizeof(struct host
));
2420 debuglog("Unable to allocate entry for statd mon\n");
2424 /* Allocated new host entry, now fill the fields */
2425 nhp
->name
= strdup(hostname
);
2426 if (nhp
->name
== NULL
) {
2427 debuglog("Unable to allocate entry name for statd mon\n");
2431 nhp
->revname
= NULL
;
2433 nhp
->lastuse
= currsec
;
2435 bcopy(saddr
, &nhp
->addr
, saddr
->sa_len
);
2437 nhp
->addr
.sa_len
= 0;
2439 debuglog("Locally Monitoring host '%s'\n", hostname
);
2441 debuglog("Attempting to tell statd\n");
2443 bzero(&smon
,sizeof(smon
));
2445 smon
.mon_id
.mon_name
= nhp
->name
;
2446 smon
.mon_id
.my_id
.my_name
= "localhost\0";
2448 smon
.mon_id
.my_id
.my_prog
= NLM_PROG
;
2449 smon
.mon_id
.my_id
.my_vers
= NLM_SM
;
2450 smon
.mon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
;
2453 rpcret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_MON
, xdr_mon
,
2454 &smon
, xdr_sm_stat_res
, &sres
);
2457 if (sres
.res_stat
== stat_fail
&& !retrying
) {
2458 debuglog("Statd call failed\n");
2460 * It's possible that the hostname provided
2461 * by the client isn't valid. Retry with a
2462 * a hostname reverse-mapped from the client's
2463 * address (if provided) and stash this name
2464 * in the host entry so that we can identify it
2465 * with this name later when we ask statd to
2469 hp
= gethostbyaddr((char *) &sin
->sin_addr
,
2470 sizeof(sin
->sin_addr
), AF_INET
);
2471 if (hp
!= NULL
&& strcmp(nhp
->name
, hp
->h_name
) != 0) {
2472 debuglog("Statd retry with '%s'\n", hp
->h_name
);
2473 smon
.mon_id
.mon_name
= hp
->h_name
;
2474 nhp
->revname
= strdup(hp
->h_name
);
2475 if (nhp
->revname
== NULL
) {
2476 debuglog("No memory for revname\n");
2486 debuglog("Rpc call to statd failed with return value: %d\n",
2491 * Insert host in the monitor list, even if statd
2492 * doesn't like it. If the name is unacceptable
2493 * to statd, at least we'll avoid subsequent rejection
2494 * on every lock request.
2496 TAILQ_INSERT_HEAD(&hostlst_head
, nhp
, hostlst
);
2500 * unmonitor_lock_host: clear monitor ref counts and inform statd when gone
2503 unmonitor_lock_host(const char *hostname
)
2507 TAILQ_FOREACH(ihp
, &hostlst_head
, hostlst
) {
2508 if (strncmp(hostname
, ihp
->name
, SM_MAXSTRLEN
) == 0) {
2509 /* Host is unmonitored, drop refcount */
2511 /* Host should only be in the monitor list once */
2517 debuglog("Could not find host %16s in mon list\n", hostname
);
2521 if (ihp
->refcnt
> 0)
2524 if (ihp
->refcnt
< 0) {
2525 debuglog("Negative refcount!: %d\n", ihp
->refcnt
);
2528 TAILQ_REMOVE(&hostlst_head
, ihp
, hostlst
);
2529 TAILQ_INSERT_HEAD(&hostlst_unref
, ihp
, hostlst
);
2530 if (host_expire
<= 0)
2531 destroy_lock_host(ihp
);
2535 destroy_lock_host(struct host
*ihp
)
2537 struct mon_id smon_id
;
2538 struct sm_stat smstat
;
2543 * If the client was monitored with a hostname obtained from
2544 * its address, then use that instead of the possibly invalid
2545 * hostname provided in caller_name.
2547 name
= ihp
->revname
? ihp
->revname
: ihp
->name
;
2548 debuglog("Attempting to unmonitor host %16s\n", name
);
2550 bzero(&smon_id
,sizeof(smon_id
));
2551 smon_id
.mon_name
= name
;
2552 smon_id
.my_id
.my_name
= "localhost";
2553 smon_id
.my_id
.my_prog
= NLM_PROG
;
2554 smon_id
.my_id
.my_vers
= NLM_SM
;
2555 smon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
;
2557 rpcret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON
, xdr_mon_id
,
2558 &smon_id
, xdr_sm_stat
, &smstat
);
2561 debuglog("Rpc call to unmonitor statd failed with "
2562 " return value: %d: %s", rpcret
, clnt_sperrno(rpcret
));
2564 debuglog("Succeeded unmonitoring %16s\n", ihp
->name
);
2567 TAILQ_REMOVE(&hostlst_unref
, ihp
, hostlst
);
2576 * returns 1 if there are hosts to expire or 0 if there are none.
2579 expire_lock_hosts(void)
2583 debuglog("expire_lock_hosts: called\n");
2585 ihp
= TAILQ_LAST(&hostlst_unref
, hostlst_head
);
2588 if (host_expire
> 0 && ihp
->lastuse
>= currsec
- host_expire
)
2590 debuglog("expire_lock_hosts: expiring %s %d %d %d\n",
2591 ihp
->name
, (int)ihp
->lastuse
,
2592 (int)currsec
, (int)currsec
- host_expire
);
2593 destroy_lock_host(ihp
);
2595 return (TAILQ_LAST(&hostlst_unref
, hostlst_head
) != NULL
);
2599 * notify: Clear all locks from a host if statd complains
2601 * XXX: This routine has not been thoroughly tested. However, neither
2602 * had the old one been. It used to compare the statd crash state counter
2603 * to the current lock state. The upshot of this was that it basically
2604 * cleared all locks from the specified host 99% of the time (with the
2605 * other 1% being a bug). Consequently, the assumption is that clearing
2606 * all locks from a host when notified by statd is acceptable.
2608 * Please note that this routine skips the usual level of redirection
2609 * through a do_* type routine. This introduces a possible level of
2610 * error and might better be written as do_notify and take this one out.
2615 notify(const char *hostname
, const int state
)
2617 debuglog("notify from %s, new state %d", hostname
, state
);
2623 debuglog("Leaving notify\n");
2627 send_granted(fl
, opcode
)
2628 struct file_lock
*fl
;
2629 int opcode __unused
;
2633 struct timeval timeo
;
2635 static struct nlm_res retval
;
2636 static struct nlm4_res retval4
;
2638 debuglog("About to send granted on blocked lock\n");
2640 cli
= get_client(fl
->addr
,
2641 (fl
->flags
& LOCK_V4
) ? NLM_VERS4
: NLM_VERS
);
2643 syslog(LOG_NOTICE
, "failed to get CLIENT for %s",
2646 * We fail to notify remote that the lock has been granted.
2647 * The client will timeout and retry, the lock will be
2648 * granted at this time.
2653 timeo
.tv_usec
= (fl
->flags
& LOCK_ASYNC
) ? 0 : 500000; /* 0.5s */
2655 fl
->granted_cookie
= ++send_granted_cookie
;
2656 if (!send_granted_cookie
)
2657 send_granted_cookie
++;
2659 if (fl
->flags
& LOCK_V4
) {
2660 static nlm4_testargs res
;
2661 res
.cookie
.n_len
= sizeof(fl
->granted_cookie
);
2662 res
.cookie
.n_bytes
= (char*)&fl
->granted_cookie
;
2663 res
.exclusive
= fl
->client
.exclusive
;
2664 res
.alock
.caller_name
= fl
->client_name
;
2665 res
.alock
.fh
.n_len
= fl
->filehandle
.n_len
;
2666 res
.alock
.fh
.n_bytes
= fl
->filehandle
.n_bytes
;
2667 res
.alock
.oh
= fl
->client
.oh
;
2668 res
.alock
.svid
= fl
->client
.svid
;
2669 res
.alock
.l_offset
= fl
->client
.l_offset
;
2670 res
.alock
.l_len
= fl
->client
.l_len
;
2671 debuglog("sending v4 reply%s",
2672 (fl
->flags
& LOCK_ASYNC
) ? " (async)":"");
2673 if (fl
->flags
& LOCK_ASYNC
) {
2674 rv
= clnt_call(cli
, NLM4_GRANTED_MSG
,
2675 xdr_nlm4_testargs
, &res
, xdr_void
, &dummy
, timeo
);
2677 rv
= clnt_call(cli
, NLM4_GRANTED
,
2678 xdr_nlm4_testargs
, &res
, xdr_nlm4_res
,
2682 static nlm_testargs res
;
2684 res
.cookie
.n_len
= sizeof(fl
->granted_cookie
);
2685 res
.cookie
.n_bytes
= (char*)&fl
->granted_cookie
;
2686 res
.exclusive
= fl
->client
.exclusive
;
2687 res
.alock
.caller_name
= fl
->client_name
;
2688 res
.alock
.fh
.n_len
= fl
->filehandle
.n_len
;
2689 res
.alock
.fh
.n_bytes
= fl
->filehandle
.n_bytes
;
2690 res
.alock
.oh
= fl
->client
.oh
;
2691 res
.alock
.svid
= fl
->client
.svid
;
2692 res
.alock
.l_offset
= fl
->client
.l_offset
;
2693 res
.alock
.l_len
= fl
->client
.l_len
;
2694 debuglog("sending v1 reply%s",
2695 (fl
->flags
& LOCK_ASYNC
) ? " (async)":"");
2696 if (fl
->flags
& LOCK_ASYNC
) {
2697 rv
= clnt_call(cli
, NLM_GRANTED_MSG
,
2698 xdr_nlm_testargs
, &res
, xdr_void
, &dummy
, timeo
);
2700 rv
= clnt_call(cli
, NLM_GRANTED
,
2701 xdr_nlm_testargs
, &res
, xdr_nlm_res
,
2705 if (debug_level
> 2)
2706 debuglog("clnt_call returns %d(%s) for granted",
2707 rv
, clnt_sperrno(rv
));
2709 if ((rv
!= RPC_SUCCESS
) &&
2710 !((fl
->flags
& LOCK_ASYNC
) && (rv
== RPC_TIMEDOUT
)))
2716 * granted_failed: remove a granted lock that wasn't successfully
2717 * accepted by the client
2720 granted_failed(nlm4_res
*arg
)
2723 struct file_lock
*ifl
;
2725 debuglog("Entering granted_failed, status %d\n", arg
->stat
.stat
);
2727 if (arg
->cookie
.n_len
!= sizeof(cookie
)) {
2728 debuglog("Exiting granted_failed: bogus cookie size %d\n",
2732 bcopy(arg
->cookie
.n_bytes
, &cookie
, sizeof(cookie
));
2733 debuglog("granted_failed, cookie 0x%llx\n", cookie
);
2735 LIST_FOREACH(ifl
, &nfslocklist_head
, nfslocklist
) {
2736 debuglog("Pointer to file lock: %p\n",ifl
);
2738 debuglog("****Dump of ifl****\n");
2741 if (ifl
->granted_cookie
!= cookie
)
2744 debuglog("granted_failed: cookie found\n");
2750 /* ifl is NO LONGER VALID AT THIS POINT */
2752 debuglog("granted_failed: cookie NOT FOUND\n");
2755 debuglog("Exiting granted_failed\n");
2759 * getshare: try to acquire a share reservation
2762 getshare(nlm_shareargs
*shrarg
, struct svc_req
*rqstp
, const int flags
)
2764 struct sharefile
*shrfile
;
2765 struct file_share
*sh
;
2768 debuglog("Entering getshare...\n");
2770 if (grace_expired
== 0 && shrarg
->reclaim
== 0) {
2771 debuglog("getshare denied - grace period\n");
2772 return (flags
& LOCK_V4
) ?
2773 nlm4_denied_grace_period
:
2774 nlm_denied_grace_period
;
2777 if (shrarg
->share
.fh
.n_len
> NFS_MAX_FH_SIZE
) {
2778 debuglog("received fhandle size %d, max size %d",
2779 shrarg
->share
.fh
.n_len
, NFS_MAX_FH_SIZE
);
2780 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2783 /* find file in list of share files */
2784 LIST_FOREACH(shrfile
, &nfssharefilelist_head
, sharefilelist
) {
2785 if ((shrarg
->share
.fh
.n_len
== shrfile
->filehandle
.n_len
) &&
2786 (bcmp(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2787 shrarg
->share
.fh
.n_len
) == 0)) {
2788 /* shrfile is the correct file */
2793 /* if share file not found, create a new share file */
2797 fh
.fh_len
= shrarg
->share
.fh
.n_len
;
2798 bcopy(shrarg
->share
.fh
.n_bytes
, fh
.fh_data
, fh
.fh_len
);
2799 fd
= fhopen(&fh
, O_RDONLY
);
2801 debuglog("fhopen failed (from %16s): %32s\n",
2802 shrarg
->share
.caller_name
, strerror(errno
));
2803 if ((flags
& LOCK_V4
) == 0)
2807 return nlm4_stale_fh
;
2812 shrfile
= malloc(sizeof(struct sharefile
));
2814 debuglog("getshare failed: can't allocate sharefile\n");
2816 return (flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
2818 shrfile
->filehandle
.n_len
= shrarg
->share
.fh
.n_len
;
2819 shrfile
->filehandle
.n_bytes
= malloc(shrarg
->share
.fh
.n_len
);
2820 if (!shrfile
->filehandle
.n_bytes
) {
2821 debuglog("getshare failed: can't allocate sharefile filehandle\n");
2824 return (flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
2826 bcopy(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2827 shrarg
->share
.fh
.n_len
);
2829 shrfile
->refcount
= 0;
2830 shrfile
->sharelist_head
.lh_first
= NULL
;
2831 LIST_INSERT_HEAD(&nfssharefilelist_head
, shrfile
, sharefilelist
);
2834 /* compare request mode/access to current shares */
2835 LIST_FOREACH(sh
, &shrfile
->sharelist_head
, nfssharelist
) {
2836 /* if request host/owner matches a current share... */
2837 if ((strncmp(shrarg
->share
.caller_name
, sh
->client_name
, SM_MAXSTRLEN
) == 0) &&
2838 same_netobj(&shrarg
->share
.oh
, &sh
->oh
)) {
2839 /* ...then just update share mode/access */
2840 sh
->mode
= shrarg
->share
.mode
;
2841 sh
->access
= shrarg
->share
.access
;
2842 debuglog("getshare: updated existing share\n");
2843 return (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2845 if (((shrarg
->share
.mode
& sh
->access
) != 0) ||
2846 ((shrarg
->share
.access
& sh
->mode
) != 0)) {
2847 /* share request conflicts with existing share */
2848 debuglog("getshare: conflicts with existing share\n");
2849 return (flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
2853 /* create/init new share */
2854 n
= strnlen(shrarg
->share
.caller_name
, SM_MAXSTRLEN
);
2855 if (n
< SM_MAXSTRLEN
) {
2856 sh
= malloc(sizeof(*sh
) - sizeof(sh
->client_name
) + n
+ 1);
2858 debuglog("getshare failed: hostname too long\n");
2862 debuglog("getshare failed: can't allocate share\n");
2863 if (!shrfile
->refcount
) {
2864 LIST_REMOVE(shrfile
, sharefilelist
);
2866 free(shrfile
->filehandle
.n_bytes
);
2869 return (flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
2871 bzero(sh
, sizeof(*sh
) - sizeof(sh
->client_name
));
2872 sh
->oh
.n_len
= shrarg
->share
.oh
.n_len
;
2873 sh
->oh
.n_bytes
= malloc(sh
->oh
.n_len
);
2874 if (!sh
->oh
.n_bytes
) {
2875 debuglog("getshare failed: can't allocate share owner handle\n");
2877 if (!shrfile
->refcount
) {
2878 LIST_REMOVE(shrfile
, sharefilelist
);
2880 free(shrfile
->filehandle
.n_bytes
);
2883 return (flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
2885 memcpy(sh
->client_name
, shrarg
->share
.caller_name
, n
);
2886 sh
->client_name
[n
] = 0;
2887 sh
->mode
= shrarg
->share
.mode
;
2888 sh
->access
= shrarg
->share
.access
;
2890 /* insert new share into file's share list */
2891 LIST_INSERT_HEAD(&shrfile
->sharelist_head
, sh
, nfssharelist
);
2892 shrfile
->refcount
++;
2894 debuglog("Exiting getshare...\n");
2896 return (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2900 /* remove a share reservation */
2902 unshare(nlm_shareargs
*shrarg
, struct svc_req
*rqstp
, const int flags
)
2904 struct sharefile
*shrfile
;
2905 struct file_share
*sh
;
2907 debuglog("Entering unshare...\n");
2909 if (shrarg
->share
.fh
.n_len
> NFS_MAX_FH_SIZE
) {
2910 debuglog("received fhandle size %d, max size %d",
2911 shrarg
->share
.fh
.n_len
, NFS_MAX_FH_SIZE
);
2912 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2915 /* find file in list of share files */
2916 LIST_FOREACH(shrfile
, &nfssharefilelist_head
, sharefilelist
) {
2917 if ((shrarg
->share
.fh
.n_len
== shrfile
->filehandle
.n_len
) &&
2918 (bcmp(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2919 shrarg
->share
.fh
.n_len
) == 0)) {
2920 /* shrfile is the correct file */
2925 /* if share file not found, return success (per spec) */
2927 debuglog("unshare: no such share file\n");
2928 return (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2932 LIST_FOREACH(sh
, &shrfile
->sharelist_head
, nfssharelist
) {
2933 /* if request host/owner matches a current share... */
2934 if ((strncmp(shrarg
->share
.caller_name
, sh
->client_name
, SM_MAXSTRLEN
) == 0) &&
2935 same_netobj(&shrarg
->share
.oh
, &sh
->oh
))
2939 /* if share not found, return success (per spec) */
2941 debuglog("unshare: no such share\n");
2942 return (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2945 /* remove share from file and deallocate */
2946 shrfile
->refcount
--;
2947 LIST_REMOVE(sh
, nfssharelist
);
2948 free(sh
->oh
.n_bytes
);
2951 /* if file has no more shares, deallocate share file */
2952 if (!shrfile
->refcount
) {
2953 debuglog("unshare: file has no more shares\n");
2954 LIST_REMOVE(shrfile
, sharefilelist
);
2956 free(shrfile
->filehandle
.n_bytes
);
2960 debuglog("Exiting unshare...\n");
2962 return (flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
2968 * Wipe out all non-monitored locks and shares held by a host.
2972 do_free_all(const char *hostname
)
2974 struct file_lock
*ifl
, *nfl
;
2975 struct sharefile
*shrfile
, *nshrfile
;
2976 struct file_share
*ifs
, *nfs
;
2977 enum partialfilelock_status pfsret
;
2979 /* clear non-monitored blocking file locks */
2980 ifl
= LIST_FIRST(&blockedlocklist_head
);
2981 while (ifl
!= NULL
) {
2982 nfl
= LIST_NEXT(ifl
, nfslocklist
);
2984 if (((ifl
->flags
& LOCK_MON
) == 0) &&
2985 (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0)) {
2986 remove_blockingfilelock(ifl
);
2987 deallocate_file_lock(ifl
);
2993 /* clear non-monitored file locks */
2995 ifl
= LIST_FIRST(&nfslocklist_head
);
2996 while (ifl
!= NULL
) {
2997 nfl
= LIST_NEXT(ifl
, nfslocklist
);
2999 if (((ifl
->flags
& LOCK_MON
) == 0) &&
3000 (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0)) {
3001 /* Unlock destroys ifl out from underneath */
3002 pfsret
= unlock_partialfilelock(ifl
);
3003 if (pfsret
!= PFL_GRANTED
) {
3004 /* Uh oh... there was some sort of problem. */
3005 /* If we restart the loop, we may get */
3006 /* stuck here forever getting errors. */
3007 /* So, let's just abort the whole scan. */
3008 syslog(LOG_WARNING
, "unmonitored lock clearing for %s failed: %d",
3012 /* ifl is NO LONGER VALID AT THIS POINT */
3013 /* Note: the unlock may deallocate several existing locks. */
3014 /* Therefore, we need to restart the scanning of the list, */
3015 /* because nfl could be pointing to a freed lock. */
3023 shrfile
= LIST_FIRST(&nfssharefilelist_head
);
3024 while (shrfile
!= NULL
) {
3025 nshrfile
= LIST_NEXT(shrfile
, sharefilelist
);
3027 ifs
= LIST_FIRST(&shrfile
->sharelist_head
);
3028 while (ifs
!= NULL
) {
3029 nfs
= LIST_NEXT(ifs
, nfssharelist
);
3031 if (strncmp(hostname
, ifs
->client_name
, SM_MAXSTRLEN
) == 0) {
3032 shrfile
->refcount
--;
3033 LIST_REMOVE(ifs
, nfssharelist
);
3034 free(ifs
->oh
.n_bytes
);
3041 if (!shrfile
->refcount
) {
3042 LIST_REMOVE(shrfile
, sharefilelist
);
3044 free(shrfile
->filehandle
.n_bytes
);
3056 * Routines below here have not been modified in the overhaul
3060 * Are these two routines still required since lockd is not spawning off
3061 * children to service locks anymore? Presumably they were originally
3062 * put in place to prevent a one child from changing the lock list out
3063 * from under another one.
3071 sigemptyset(&block
);
3072 sigaddset(&block
, SIGCHLD
);
3074 if (sigprocmask(SIG_BLOCK
, &block
, NULL
) < 0) {
3075 syslog(LOG_WARNING
, "siglock failed: %s", strerror(errno
));
3084 sigemptyset(&block
);
3085 sigaddset(&block
, SIGCHLD
);
3087 if (sigprocmask(SIG_UNBLOCK
, &block
, NULL
) < 0) {
3088 syslog(LOG_WARNING
, "sigunlock failed: %s", strerror(errno
));