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 * SM_MAXSTRLEN is usually 1024. This means that lock requests and
69 * host name monitoring entries are *MUCH* larger than they should be
73 * A set of utilities for managing file locking
75 * XXX: All locks are in a linked list, a better structure should be used
76 * to improve search/access effeciency.
79 /* struct describing a lock */
81 LIST_ENTRY(file_lock
) nfslocklist
;
82 netobj filehandle
; /* NFS filehandle */
83 struct sockaddr
*addr
;
84 struct nlm4_holder client
; /* lock holder */
85 /* XXX: client_cookie used *only* in send_granted */
86 netobj client_cookie
; /* cookie sent by the client */
87 char client_name
[SM_MAXSTRLEN
];
88 int nsm_status
; /* status from the remote lock manager */
89 int status
; /* lock status, see below */
90 int flags
; /* lock flags, see lockd_lock.h */
91 int blocking
; /* blocking lock or not */
92 pid_t locker
; /* pid of the child process trying to get the lock */
93 int fd
; /* file descriptor for this lock */
96 LIST_HEAD(nfslocklist_head
, file_lock
);
97 struct nfslocklist_head nfslocklist_head
= LIST_HEAD_INITIALIZER(nfslocklist_head
);
99 LIST_HEAD(blockedlocklist_head
, file_lock
);
100 struct blockedlocklist_head blockedlocklist_head
= LIST_HEAD_INITIALIZER(blockedlocklist_head
);
102 /* struct describing a share reservation */
104 LIST_ENTRY(file_share
) nfssharelist
;
105 netobj oh
; /* share holder */
106 char client_name
[SM_MAXSTRLEN
];
110 LIST_HEAD(nfssharelist_head
, file_share
);
112 /* Struct describing a file with share reservations */
114 LIST_ENTRY(sharefile
) sharefilelist
;
115 netobj filehandle
; /* Local access filehandle */
116 int fd
; /* file descriptor: remains open until no more shares */
118 struct nfssharelist_head sharelist_head
;
120 LIST_HEAD(nfssharefilelist_head
, sharefile
);
121 struct nfssharefilelist_head nfssharefilelist_head
= LIST_HEAD_INITIALIZER(nfssharefilelist_head
);
124 #define LKST_LOCKED 1 /* lock is locked */
125 /* XXX: Is this flag file specific or lock specific? */
126 #define LKST_WAITING 2 /* file is already locked by another host */
127 #define LKST_PROCESSING 3 /* child is trying to aquire the lock */
128 #define LKST_DYING 4 /* must dies when we get news from the child */
130 /* struct describing a monitored host */
132 LIST_ENTRY(host
) hostlst
;
133 char name
[SM_MAXSTRLEN
];
136 /* list of hosts we monitor */
137 LIST_HEAD(hostlst_head
, host
);
138 struct hostlst_head hostlst_head
= LIST_HEAD_INITIALIZER(hostlst_head
);
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
};
174 enum LFLAGS
{LEDGE_LEFT
, LEDGE_LBOUNDARY
, LEDGE_INSIDE
, LEDGE_RBOUNDARY
, LEDGE_RIGHT
};
175 enum RFLAGS
{REDGE_LEFT
, REDGE_LBOUNDARY
, REDGE_INSIDE
, REDGE_RBOUNDARY
, REDGE_RIGHT
};
176 /* XXX: WARNING! I HAVE OVERLOADED THIS STATUS ENUM! SPLIT IT APART INTO TWO */
177 enum split_status
{SPL_DISJOINT
=0, SPL_LOCK1
=1, SPL_LOCK2
=2, SPL_CONTAINED
=4, SPL_RESERR
=8};
179 enum partialfilelock_status
lock_partialfilelock(struct file_lock
*fl
);
181 void send_granted(struct file_lock
*fl
, int opcode
);
183 void sigunlock(void);
184 void monitor_lock_host(const char *hostname
);
185 void unmonitor_lock_host(const char *hostname
);
187 void copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock
*src
,
188 const bool_t exclusive
, struct nlm4_holder
*dest
);
189 struct file_lock
* allocate_file_lock(const netobj
*lockowner
,
190 const netobj
*matchcookie
, const netobj
*filehandle
);
191 void deallocate_file_lock(struct file_lock
*fl
);
192 void fill_file_lock(struct file_lock
*fl
,
193 struct sockaddr
*addr
, const bool_t exclusive
, const int32_t svid
,
194 const u_int64_t offset
, const u_int64_t len
, const char *caller_name
,
195 const int state
, const int status
, const int flags
, const int blocking
);
196 int regions_overlap(const u_int64_t start1
, const u_int64_t len1
,
197 const u_int64_t start2
, const u_int64_t len2
);;
198 enum split_status
region_compare(const u_int64_t starte
, const u_int64_t lene
,
199 const u_int64_t startu
, const u_int64_t lenu
,
200 u_int64_t
*start1
, u_int64_t
*len1
, u_int64_t
*start2
, u_int64_t
*len2
);
201 int same_netobj(const netobj
*n0
, const netobj
*n1
);
202 int same_filelock_identity(const struct file_lock
*fl0
,
203 const struct file_lock
*fl2
);
205 static void debuglog(char const *fmt
, ...);
206 void dump_static_object(const unsigned char* object
, const int sizeof_object
,
207 unsigned char* hbuff
, const int sizeof_hbuff
,
208 unsigned char* cbuff
, const int sizeof_cbuff
);
209 void dump_netobj(const struct netobj
*nobj
);
210 void dump_filelock(const struct file_lock
*fl
);
211 struct file_lock
* get_lock_matching_unlock(const struct file_lock
*fl
);
212 enum nfslock_status
test_nfslock(const struct file_lock
*fl
,
213 struct file_lock
**conflicting_fl
);
214 enum nfslock_status
lock_nfslock(struct file_lock
*fl
);
215 enum nfslock_status
delete_nfslock(struct file_lock
*fl
);
216 enum nfslock_status
unlock_nfslock(const struct file_lock
*fl
,
217 struct file_lock
**released_lock
, struct file_lock
**left_lock
,
218 struct file_lock
**right_lock
);
219 enum hwlock_status
lock_hwlock(struct file_lock
*fl
);
220 enum split_status
split_nfslock(const struct file_lock
*exist_lock
,
221 const struct file_lock
*unlock_lock
, struct file_lock
**left_lock
,
222 struct file_lock
**right_lock
);
223 void add_blockingfilelock(struct file_lock
*fl
);
224 enum hwlock_status
unlock_hwlock(const struct file_lock
*fl
);
225 enum hwlock_status
test_hwlock(const struct file_lock
*fl
,
226 struct file_lock
**conflicting_fl
);
227 void remove_blockingfilelock(struct file_lock
*fl
);
228 void clear_blockingfilelock(const char *hostname
);
229 void retry_blockingfilelocklist(void);
230 enum partialfilelock_status
unlock_partialfilelock(
231 const struct file_lock
*fl
);
232 void clear_partialfilelock(const char *hostname
);
233 enum partialfilelock_status
test_partialfilelock(
234 const struct file_lock
*fl
, struct file_lock
**conflicting_fl
);
235 enum nlm_stats
do_test(struct file_lock
*fl
,
236 struct file_lock
**conflicting_fl
);
237 enum nlm_stats
do_unlock(struct file_lock
*fl
);
238 enum nlm_stats
do_lock(struct file_lock
*fl
);
239 void do_clear(const char *hostname
);
243 debuglog(char const *fmt
, ...)
247 if (debug_level
< 1) {
254 vsyslog(LOG_DEBUG
, fmt
, ap
);
259 dump_static_object(object
, size_object
, hbuff
, size_hbuff
, cbuff
, size_cbuff
)
260 const unsigned char *object
;
261 const int size_object
;
262 unsigned char *hbuff
;
263 const int size_hbuff
;
264 unsigned char *cbuff
;
265 const int size_cbuff
;
269 if (debug_level
< 2) {
273 objectsize
= size_object
;
275 if (objectsize
== 0) {
276 debuglog("object is size 0\n");
278 if (objectsize
> MAXOBJECTSIZE
) {
279 debuglog("Object of size %d being clamped"
280 "to size %d\n", objectsize
, MAXOBJECTSIZE
);
281 objectsize
= MAXOBJECTSIZE
;
285 if (size_hbuff
< objectsize
*2+1) {
286 debuglog("Hbuff not large enough."
289 for(i
=0;i
<objectsize
;i
++) {
290 sprintf(hbuff
+i
*2,"%02x",*(object
+i
));
297 if (size_cbuff
< objectsize
+1) {
298 debuglog("Cbuff not large enough."
302 for(i
=0;i
<objectsize
;i
++) {
303 if (*(object
+i
) >= 32 && *(object
+i
) <= 127) {
304 *(cbuff
+i
) = *(object
+i
);
315 dump_netobj(const struct netobj
*nobj
)
317 char hbuff
[MAXBUFFERSIZE
*2];
318 char cbuff
[MAXBUFFERSIZE
];
320 if (debug_level
< 2) {
325 debuglog("Null netobj pointer\n");
327 else if (nobj
->n_len
== 0) {
328 debuglog("Size zero netobj\n");
330 dump_static_object(nobj
->n_bytes
, nobj
->n_len
,
331 hbuff
, sizeof(hbuff
), cbuff
, sizeof(cbuff
));
332 debuglog("netobj: len: %d data: %s ::: %s\n",
333 nobj
->n_len
, hbuff
, cbuff
);
337 /* #define DUMP_FILELOCK_VERBOSE */
339 dump_filelock(const struct file_lock
*fl
)
341 #ifdef DUMP_FILELOCK_VERBOSE
342 char hbuff
[MAXBUFFERSIZE
*2];
343 char cbuff
[MAXBUFFERSIZE
];
346 if (debug_level
< 2) {
351 debuglog("Dumping file lock structure @ %p\n", fl
);
353 #ifdef DUMP_FILELOCK_VERBOSE
354 dump_static_object((unsigned char *)&fl
->filehandle
.n_bytes
,
355 fl
->filehandle
.n_len
, hbuff
, sizeof(hbuff
),
356 cbuff
, sizeof(cbuff
));
357 debuglog("Filehandle: %8s ::: %8s\n", hbuff
, cbuff
);
360 debuglog("Dumping nlm4_holder:\n"
361 "exc: %x svid: %x offset:len %llx:%llx\n",
362 fl
->client
.exclusive
, fl
->client
.svid
,
363 fl
->client
.l_offset
, fl
->client
.l_len
);
365 #ifdef DUMP_FILELOCK_VERBOSE
366 debuglog("Dumping client identity:\n");
367 dump_netobj(&fl
->client
.oh
);
369 debuglog("Dumping client cookie:\n");
370 dump_netobj(&fl
->client_cookie
);
372 debuglog("nsm: %d status: %d flags: %d locker: %d"
373 " fd: %d\n", fl
->nsm_status
, fl
->status
,
374 fl
->flags
, fl
->locker
, fl
->fd
);
377 debuglog("NULL file lock structure\n");
382 copy_nlm4_lock_to_nlm4_holder(src
, exclusive
, dest
)
383 const struct nlm4_lock
*src
;
384 const bool_t exclusive
;
385 struct nlm4_holder
*dest
;
388 dest
->exclusive
= exclusive
;
389 dest
->oh
.n_len
= src
->oh
.n_len
;
390 dest
->oh
.n_bytes
= src
->oh
.n_bytes
;
391 dest
->svid
= src
->svid
;
392 dest
->l_offset
= src
->l_offset
;
393 dest
->l_len
= src
->l_len
;
398 * allocate_file_lock: Create a lock with the given parameters
402 allocate_file_lock(const netobj
*lockowner
, const netobj
*matchcookie
, const netobj
*filehandle
)
404 struct file_lock
*newfl
;
406 newfl
= malloc(sizeof(struct file_lock
));
410 bzero(newfl
, sizeof(newfl
));
412 newfl
->client
.oh
.n_bytes
= malloc(lockowner
->n_len
);
413 if (newfl
->client
.oh
.n_bytes
== NULL
) {
417 newfl
->client
.oh
.n_len
= lockowner
->n_len
;
418 bcopy(lockowner
->n_bytes
, newfl
->client
.oh
.n_bytes
, lockowner
->n_len
);
420 newfl
->client_cookie
.n_bytes
= malloc(matchcookie
->n_len
);
421 if (newfl
->client_cookie
.n_bytes
== NULL
) {
422 free(newfl
->client
.oh
.n_bytes
);
426 newfl
->client_cookie
.n_len
= matchcookie
->n_len
;
427 bcopy(matchcookie
->n_bytes
, newfl
->client_cookie
.n_bytes
, matchcookie
->n_len
);
429 newfl
->filehandle
.n_bytes
= malloc(filehandle
->n_len
);
430 if (newfl
->filehandle
.n_bytes
== NULL
) {
431 free(newfl
->client_cookie
.n_bytes
);
432 free(newfl
->client
.oh
.n_bytes
);
436 newfl
->filehandle
.n_len
= filehandle
->n_len
;
437 bcopy(filehandle
->n_bytes
, newfl
->filehandle
.n_bytes
, filehandle
->n_len
);
443 * file_file_lock: Force creation of a valid file lock
446 fill_file_lock(struct file_lock
*fl
,
447 struct sockaddr
*addr
, const bool_t exclusive
, const int32_t svid
,
448 const u_int64_t offset
, const u_int64_t len
, const char *caller_name
,
449 const int state
, const int status
, const int flags
, const int blocking
)
453 fl
->client
.exclusive
= exclusive
;
454 fl
->client
.svid
= svid
;
455 fl
->client
.l_offset
= offset
;
456 fl
->client
.l_len
= len
;
458 strncpy(fl
->client_name
, caller_name
, SM_MAXSTRLEN
);
460 fl
->nsm_status
= state
;
463 fl
->blocking
= blocking
;
467 * deallocate_file_lock: Free all storage associated with a file lock
470 deallocate_file_lock(struct file_lock
*fl
)
472 free(fl
->client
.oh
.n_bytes
);
473 free(fl
->client_cookie
.n_bytes
);
474 free(fl
->filehandle
.n_bytes
);
479 * regions_overlap(): This function examines the two provided regions for
483 regions_overlap(start1
, len1
, start2
, len2
)
484 const u_int64_t start1
, len1
, start2
, len2
;
486 u_int64_t d1
,d2
,d3
,d4
;
487 enum split_status result
;
489 debuglog("Entering region overlap with vals: %llu:%llu--%llu:%llu\n",
490 start1
, len1
, start2
, len2
);
492 result
= region_compare(start1
, len1
, start2
, len2
,
495 debuglog("Exiting region overlap with val: %d\n",result
);
497 if (result
== SPL_DISJOINT
) {
507 * region_compare(): Examine lock regions and split appropriately
509 * XXX: Fix 64 bit overflow problems
510 * XXX: Check to make sure I got *ALL* the cases.
511 * XXX: This DESPERATELY needs a regression test.
514 region_compare(starte
, lene
, startu
, lenu
,
515 start1
, len1
, start2
, len2
)
516 const u_int64_t starte
, lene
, startu
, lenu
;
517 u_int64_t
*start1
, *len1
, *start2
, *len2
;
520 * Please pay attention to the sequential exclusions
521 * of the if statements!!!
525 enum split_status retval
;
527 retval
= SPL_DISJOINT
;
529 if (lene
== 0 && lenu
== 0) {
530 /* Examine left edge of locker */
531 if (startu
< starte
) {
533 } else if (startu
== starte
) {
534 lflags
= LEDGE_LBOUNDARY
;
536 lflags
= LEDGE_INSIDE
;
539 rflags
= REDGE_RBOUNDARY
; /* Both are infiinite */
541 if (lflags
== LEDGE_INSIDE
) {
543 *len1
= startu
- starte
;
546 if (lflags
== LEDGE_LEFT
|| lflags
== LEDGE_LBOUNDARY
) {
547 retval
= SPL_CONTAINED
;
551 } else if (lene
== 0 && lenu
!= 0) {
552 /* Established lock is infinite */
553 /* Examine left edge of unlocker */
554 if (startu
< starte
) {
556 } else if (startu
== starte
) {
557 lflags
= LEDGE_LBOUNDARY
;
558 } else if (startu
> starte
) {
559 lflags
= LEDGE_INSIDE
;
562 /* Examine right edge of unlocker */
563 if (startu
+ lenu
< starte
) {
564 /* Right edge of unlocker left of established lock */
567 } else if (startu
+ lenu
== starte
) {
568 /* Right edge of unlocker on start of established lock */
569 rflags
= REDGE_LBOUNDARY
;
571 } else { /* Infinifty is right of finity */
572 /* Right edge of unlocker inside established lock */
573 rflags
= REDGE_INSIDE
;
576 if (lflags
== LEDGE_INSIDE
) {
578 *len1
= startu
- starte
;
582 if (rflags
== REDGE_INSIDE
) {
583 /* Create right lock */
584 *start2
= startu
+lenu
;
588 } else if (lene
!= 0 && lenu
== 0) {
589 /* Unlocker is infinite */
590 /* Examine left edge of unlocker */
591 if (startu
< starte
) {
593 retval
= SPL_CONTAINED
;
595 } else if (startu
== starte
) {
596 lflags
= LEDGE_LBOUNDARY
;
597 retval
= SPL_CONTAINED
;
599 } else if ((startu
> starte
) && (startu
< starte
+ lene
- 1)) {
600 lflags
= LEDGE_INSIDE
;
601 } else if (startu
== starte
+ lene
- 1) {
602 lflags
= LEDGE_RBOUNDARY
;
603 } else { /* startu > starte + lene -1 */
604 lflags
= LEDGE_RIGHT
;
608 rflags
= REDGE_RIGHT
; /* Infinity is right of finity */
610 if (lflags
== LEDGE_INSIDE
|| lflags
== LEDGE_RBOUNDARY
) {
612 *len1
= startu
- starte
;
618 /* Both locks are finite */
620 /* Examine left edge of unlocker */
621 if (startu
< starte
) {
623 } else if (startu
== starte
) {
624 lflags
= LEDGE_LBOUNDARY
;
625 } else if ((startu
> starte
) && (startu
< starte
+ lene
- 1)) {
626 lflags
= LEDGE_INSIDE
;
627 } else if (startu
== starte
+ lene
- 1) {
628 lflags
= LEDGE_RBOUNDARY
;
629 } else { /* startu > starte + lene -1 */
630 lflags
= LEDGE_RIGHT
;
634 /* Examine right edge of unlocker */
635 if (startu
+ lenu
< starte
) {
636 /* Right edge of unlocker left of established lock */
639 } else if (startu
+ lenu
== starte
) {
640 /* Right edge of unlocker on start of established lock */
641 rflags
= REDGE_LBOUNDARY
;
643 } else if (startu
+ lenu
< starte
+ lene
) {
644 /* Right edge of unlocker inside established lock */
645 rflags
= REDGE_INSIDE
;
646 } else if (startu
+ lenu
== starte
+ lene
) {
647 /* Right edge of unlocker on right edge of established lock */
648 rflags
= REDGE_RBOUNDARY
;
649 } else { /* startu + lenu > starte + lene */
650 /* Right edge of unlocker is right of established lock */
651 rflags
= REDGE_RIGHT
;
654 if (lflags
== LEDGE_INSIDE
|| lflags
== LEDGE_RBOUNDARY
) {
655 /* Create left lock */
657 *len1
= (startu
- starte
);
661 if (rflags
== REDGE_INSIDE
) {
662 /* Create right lock */
663 *start2
= startu
+lenu
;
664 *len2
= starte
+lene
-(startu
+lenu
);
668 if ((lflags
== LEDGE_LEFT
|| lflags
== LEDGE_LBOUNDARY
) &&
669 (rflags
== REDGE_RBOUNDARY
|| rflags
== REDGE_RIGHT
)) {
670 retval
= SPL_CONTAINED
;
678 * same_netobj: Compares the apprpriate bits of a netobj for identity
681 same_netobj(const netobj
*n0
, const netobj
*n1
)
687 debuglog("Entering netobj identity check\n");
689 if (n0
->n_len
== n1
->n_len
) {
690 debuglog("Preliminary length check passed\n");
691 retval
= !bcmp(n0
->n_bytes
, n1
->n_bytes
, n0
->n_len
);
692 debuglog("netobj %smatch\n", retval
? "" : "mis");
699 * same_filelock_identity: Compares the appropriate bits of a file_lock
702 same_filelock_identity(fl0
, fl1
)
703 const struct file_lock
*fl0
, *fl1
;
709 debuglog("Checking filelock identity\n");
712 * Check process ids and host information.
714 retval
= (fl0
->client
.svid
== fl1
->client
.svid
&&
715 same_netobj(&(fl0
->client
.oh
), &(fl1
->client
.oh
)));
717 debuglog("Exiting checking filelock identity: retval: %d\n",retval
);
723 * Below here are routines associated with manipulating the NFS
728 * get_lock_matching_unlock: Return a lock which matches the given unlock lock
730 * XXX: It is a shame that this duplicates so much code from test_nfslock.
733 get_lock_matching_unlock(const struct file_lock
*fl
)
735 struct file_lock
*ifl
; /* Iterator */
737 debuglog("Entering lock_matching_unlock\n");
738 debuglog("********Dump of fl*****************\n");
741 LIST_FOREACH(ifl
, &nfslocklist_head
, nfslocklist
) {
742 debuglog("Pointer to file lock: %p\n",ifl
);
744 debuglog("****Dump of ifl****\n");
746 debuglog("*******************\n");
749 * XXX: It is conceivable that someone could use the NLM RPC
750 * system to directly access filehandles. This may be a
751 * security hazard as the filehandle code may bypass normal
752 * file access controls
754 if (fl
->filehandle
.n_len
!= ifl
->filehandle
.n_len
)
756 if (bcmp(fl
->filehandle
.n_bytes
, ifl
->filehandle
.n_bytes
,
757 fl
->filehandle
.n_len
))
760 debuglog("matching_unlock: Filehandles match, "
761 "checking regions\n");
763 /* Filehandles match, check for region overlap */
764 if (!regions_overlap(fl
->client
.l_offset
, fl
->client
.l_len
,
765 ifl
->client
.l_offset
, ifl
->client
.l_len
))
768 debuglog("matching_unlock: Region overlap"
769 " found %llu : %llu -- %llu : %llu\n",
770 fl
->client
.l_offset
,fl
->client
.l_len
,
771 ifl
->client
.l_offset
,ifl
->client
.l_len
);
773 /* Regions overlap, check the identity */
774 if (!same_filelock_identity(fl
,ifl
))
777 debuglog("matching_unlock: Duplicate lock id. Granting\n");
781 debuglog("Exiting lock_matching_unlock\n");
787 * test_nfslock: check for NFS lock in lock list
789 * This routine makes the following assumptions:
790 * 1) Nothing will adjust the lock list during a lookup
792 * This routine has an intersting quirk which bit me hard.
793 * The conflicting_fl is the pointer to the conflicting lock.
794 * However, to modify the "*pointer* to the conflicting lock" rather
795 * that the "conflicting lock itself" one must pass in a "pointer to
796 * the pointer of the conflicting lock". Gross.
800 test_nfslock(const struct file_lock
*fl
, struct file_lock
**conflicting_fl
)
802 struct file_lock
*ifl
; /* Iterator */
803 enum nfslock_status retval
;
805 debuglog("Entering test_nfslock\n");
807 retval
= NFS_GRANTED
;
808 (*conflicting_fl
) = NULL
;
810 debuglog("Entering lock search loop\n");
812 debuglog("***********************************\n");
813 debuglog("Dumping match filelock\n");
814 debuglog("***********************************\n");
816 debuglog("***********************************\n");
818 LIST_FOREACH(ifl
, &nfslocklist_head
, nfslocklist
) {
819 if (retval
== NFS_DENIED
)
822 debuglog("Top of lock loop\n");
823 debuglog("Pointer to file lock: %p\n",ifl
);
825 debuglog("***********************************\n");
826 debuglog("Dumping test filelock\n");
827 debuglog("***********************************\n");
829 debuglog("***********************************\n");
832 * XXX: It is conceivable that someone could use the NLM RPC
833 * system to directly access filehandles. This may be a
834 * security hazard as the filehandle code may bypass normal
835 * file access controls
837 if (fl
->filehandle
.n_len
!= ifl
->filehandle
.n_len
)
839 if (bcmp(fl
->filehandle
.n_bytes
, ifl
->filehandle
.n_bytes
,
840 fl
->filehandle
.n_len
))
843 debuglog("test_nfslock: filehandle match found\n");
845 /* Filehandles match, check for region overlap */
846 if (!regions_overlap(fl
->client
.l_offset
, fl
->client
.l_len
,
847 ifl
->client
.l_offset
, ifl
->client
.l_len
))
850 debuglog("test_nfslock: Region overlap found"
851 " %llu : %llu -- %llu : %llu\n",
852 fl
->client
.l_offset
,fl
->client
.l_len
,
853 ifl
->client
.l_offset
,ifl
->client
.l_len
);
855 /* Regions overlap, check the exclusivity */
856 if (!(fl
->client
.exclusive
|| ifl
->client
.exclusive
))
859 debuglog("test_nfslock: Exclusivity failure: %d %d\n",
860 fl
->client
.exclusive
,
861 ifl
->client
.exclusive
);
863 if (same_filelock_identity(fl
,ifl
)) {
864 debuglog("test_nfslock: Duplicate id. Granting\n");
865 (*conflicting_fl
) = ifl
;
866 retval
= NFS_GRANTED_DUPLICATE
;
868 /* locking attempt fails */
869 debuglog("test_nfslock: Lock attempt failed\n");
870 debuglog("Desired lock\n");
872 debuglog("Conflicting lock\n");
874 (*conflicting_fl
) = ifl
;
879 debuglog("Dumping file locks\n");
880 debuglog("Exiting test_nfslock\n");
886 * lock_nfslock: attempt to create a lock in the NFS lock list
888 * This routine tests whether the lock will be granted and then adds
889 * the entry to the lock list if so.
891 * Argument fl gets modified as its list housekeeping entries get modified
892 * upon insertion into the NFS lock list
894 * This routine makes several assumptions:
895 * 1) It is perfectly happy to grant a duplicate lock from the same pid.
896 * While this seems to be intuitively wrong, it is required for proper
897 * Posix semantics during unlock. It is absolutely imperative to not
898 * unlock the main lock before the two child locks are established. Thus,
899 * one has be be able to create duplicate locks over an existing lock
900 * 2) It currently accepts duplicate locks from the same id,pid
904 lock_nfslock(struct file_lock
*fl
)
906 enum nfslock_status retval
;
907 struct file_lock
*dummy_fl
;
911 debuglog("Entering lock_nfslock...\n");
913 retval
= test_nfslock(fl
,&dummy_fl
);
915 if (retval
== NFS_GRANTED
|| retval
== NFS_GRANTED_DUPLICATE
) {
916 debuglog("Inserting lock...\n");
918 LIST_INSERT_HEAD(&nfslocklist_head
, fl
, nfslocklist
);
921 debuglog("Exiting lock_nfslock...\n");
927 * delete_nfslock: delete an NFS lock list entry
929 * This routine is used to delete a lock out of the NFS lock list
930 * without regard to status, underlying locks, regions or anything else
932 * Note that this routine *does not deallocate memory* of the lock.
933 * It just disconnects it from the list. The lock can then be used
934 * by other routines without fear of trashing the list.
938 delete_nfslock(struct file_lock
*fl
)
941 LIST_REMOVE(fl
, nfslocklist
);
943 return (NFS_GRANTED
);
947 split_nfslock(exist_lock
, unlock_lock
, left_lock
, right_lock
)
948 const struct file_lock
*exist_lock
, *unlock_lock
;
949 struct file_lock
**left_lock
, **right_lock
;
951 u_int64_t start1
, len1
, start2
, len2
;
952 enum split_status spstatus
;
954 spstatus
= region_compare(exist_lock
->client
.l_offset
, exist_lock
->client
.l_len
,
955 unlock_lock
->client
.l_offset
, unlock_lock
->client
.l_len
,
956 &start1
, &len1
, &start2
, &len2
);
958 if ((spstatus
& SPL_LOCK1
) != 0) {
959 *left_lock
= allocate_file_lock(&exist_lock
->client
.oh
, &exist_lock
->client_cookie
,
960 &exist_lock
->filehandle
);
961 if (*left_lock
== NULL
) {
962 debuglog("Unable to allocate resource for split 1\n");
966 fill_file_lock(*left_lock
,
968 exist_lock
->client
.exclusive
, exist_lock
->client
.svid
,
970 exist_lock
->client_name
, exist_lock
->nsm_status
,
971 exist_lock
->status
, exist_lock
->flags
, exist_lock
->blocking
);
974 if ((spstatus
& SPL_LOCK2
) != 0) {
975 *right_lock
= allocate_file_lock(&exist_lock
->client
.oh
, &exist_lock
->client_cookie
,
976 &exist_lock
->filehandle
);
977 if (*right_lock
== NULL
) {
978 debuglog("Unable to allocate resource for split 1\n");
979 if (*left_lock
!= NULL
) {
980 deallocate_file_lock(*left_lock
);
985 fill_file_lock(*right_lock
,
987 exist_lock
->client
.exclusive
, exist_lock
->client
.svid
,
989 exist_lock
->client_name
, exist_lock
->nsm_status
,
990 exist_lock
->status
, exist_lock
->flags
, exist_lock
->blocking
);
997 unlock_nfslock(fl
, released_lock
, left_lock
, right_lock
)
998 const struct file_lock
*fl
;
999 struct file_lock
**released_lock
;
1000 struct file_lock
**left_lock
;
1001 struct file_lock
**right_lock
;
1003 struct file_lock
*mfl
; /* Matching file lock */
1004 enum nfslock_status retval
;
1005 enum split_status spstatus
;
1007 debuglog("Entering unlock_nfslock\n");
1009 *released_lock
= NULL
;
1013 retval
= NFS_DENIED_NOLOCK
;
1015 printf("Attempting to match lock...\n");
1016 mfl
= get_lock_matching_unlock(fl
);
1019 debuglog("Unlock matched. Querying for split\n");
1021 spstatus
= split_nfslock(mfl
, fl
, left_lock
, right_lock
);
1023 debuglog("Split returned %d %p %p %p %p\n",spstatus
,mfl
,fl
,*left_lock
,*right_lock
);
1024 debuglog("********Split dumps********");
1027 dump_filelock(*left_lock
);
1028 dump_filelock(*right_lock
);
1029 debuglog("********End Split dumps********");
1031 if (spstatus
== SPL_RESERR
) {
1032 if (*left_lock
!= NULL
) {
1033 deallocate_file_lock(*left_lock
);
1037 if (*right_lock
!= NULL
) {
1038 deallocate_file_lock(*right_lock
);
1045 /* Insert new locks from split if required */
1046 if (*left_lock
!= NULL
) {
1047 debuglog("Split left activated\n");
1048 LIST_INSERT_HEAD(&nfslocklist_head
, *left_lock
, nfslocklist
);
1051 if (*right_lock
!= NULL
) {
1052 debuglog("Split right activated\n");
1053 LIST_INSERT_HEAD(&nfslocklist_head
, *right_lock
, nfslocklist
);
1056 /* Unlock the lock since it matches identity */
1057 LIST_REMOVE(mfl
, nfslocklist
);
1058 *released_lock
= mfl
;
1059 retval
= NFS_GRANTED
;
1062 debuglog("Exiting unlock_nfslock\n");
1068 * Below here are the routines for manipulating the file lock directly
1069 * on the disk hardware itself
1072 lock_hwlock(struct file_lock
*fl
)
1074 struct monfile
*imf
,*nmf
;
1075 int lflags
, flerror
;
1077 /* Scan to see if filehandle already present */
1078 LIST_FOREACH(imf
, &monfilelist_head
, monfilelist
) {
1079 if ((fl
->filehandle
.n_len
== imf
->filehandle
.n_len
) &&
1080 (bcmp(fl
->filehandle
.n_bytes
, imf
->filehandle
.n_bytes
,
1081 fl
->filehandle
.n_len
) == 0)) {
1082 /* imf is the correct filehandle */
1088 * Filehandle already exists (we control the file)
1089 * *AND* NFS has already cleared the lock for availability
1090 * Grant it and bump the refcount.
1094 return (HW_GRANTED
);
1097 /* No filehandle found, create and go */
1098 nmf
= malloc(sizeof(struct monfile
));
1100 debuglog("hwlock resource allocation failure\n");
1103 nmf
->filehandle
.n_bytes
= malloc(fl
->filehandle
.n_len
);
1105 debuglog("hwlock resource allocation failure\n");
1110 /* XXX: Is O_RDWR always the correct mode? */
1111 nmf
->fd
= fhopen((fhandle_t
*)fl
->filehandle
.n_bytes
, O_RDWR
);
1113 debuglog("fhopen failed (from %16s): %32s\n",
1114 fl
->client_name
, strerror(errno
));
1118 return (HW_STALEFH
);
1120 return (HW_READONLY
);
1126 /* File opened correctly, fill the monitor struct */
1127 nmf
->filehandle
.n_len
= fl
->filehandle
.n_len
;
1128 bcopy(fl
->filehandle
.n_bytes
, nmf
->filehandle
.n_bytes
, fl
->filehandle
.n_len
);
1130 nmf
->exclusive
= fl
->client
.exclusive
;
1132 lflags
= (nmf
->exclusive
== 1) ?
1133 (LOCK_EX
| LOCK_NB
) : (LOCK_SH
| LOCK_NB
);
1135 flerror
= flock(nmf
->fd
, lflags
);
1138 debuglog("flock failed (from %16s): %32s\n",
1139 fl
->client_name
, strerror(errno
));
1146 return (HW_STALEFH
);
1148 return (HW_READONLY
);
1155 /* File opened and locked */
1156 LIST_INSERT_HEAD(&monfilelist_head
, nmf
, monfilelist
);
1158 debuglog("flock succeeded (from %16s)\n", fl
->client_name
);
1159 return (HW_GRANTED
);
1163 unlock_hwlock(const struct file_lock
*fl
)
1165 struct monfile
*imf
;
1167 debuglog("Entering unlock_hwlock\n");
1168 debuglog("Entering loop interation\n");
1170 /* Scan to see if filehandle already present */
1171 LIST_FOREACH(imf
, &monfilelist_head
, monfilelist
) {
1172 if ((fl
->filehandle
.n_len
== imf
->filehandle
.n_len
) &&
1173 (bcmp(fl
->filehandle
.n_bytes
, imf
->filehandle
.n_bytes
,
1174 fl
->filehandle
.n_len
) == 0)) {
1175 /* imf is the correct filehandle */
1180 debuglog("Completed iteration. Proceeding\n");
1184 debuglog("Exiting unlock_hwlock (HW_DENIED_NOLOCK)\n");
1185 return (HW_DENIED_NOLOCK
);
1191 if (imf
->refcount
< 0) {
1192 debuglog("Negative hardware reference count\n");
1195 if (imf
->refcount
<= 0) {
1197 LIST_REMOVE(imf
, monfilelist
);
1200 debuglog("Exiting unlock_hwlock (HW_GRANTED)\n");
1201 return (HW_GRANTED
);
1205 test_hwlock(fl
, conflicting_fl
)
1206 const struct file_lock
*fl __unused
;
1207 struct file_lock
**conflicting_fl __unused
;
1211 * XXX: lock tests on hardware are not required until
1212 * true partial file testing is done on the underlying file
1220 * Below here are routines for manipulating blocked lock requests
1221 * They should only be called from the XXX_partialfilelock routines
1222 * if at all possible
1226 add_blockingfilelock(struct file_lock
*fl
)
1229 debuglog("Entering add_blockingfilelock\n");
1232 * Clear the blocking flag so that it can be reused without
1233 * adding it to the blocking queue a second time
1237 LIST_INSERT_HEAD(&blockedlocklist_head
, fl
, nfslocklist
);
1239 debuglog("Exiting add_blockingfilelock\n");
1243 remove_blockingfilelock(struct file_lock
*fl
)
1246 debuglog("Entering remove_blockingfilelock\n");
1248 LIST_REMOVE(fl
, nfslocklist
);
1250 debuglog("Exiting remove_blockingfilelock\n");
1254 clear_blockingfilelock(const char *hostname
)
1256 struct file_lock
*ifl
,*nfl
;
1259 * Normally, LIST_FOREACH is called for, but since
1260 * the current element *is* the iterator, deleting it
1261 * would mess up the iteration. Thus, a next element
1262 * must be used explicitly
1265 ifl
= LIST_FIRST(&blockedlocklist_head
);
1267 while (ifl
!= NULL
) {
1268 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1270 if (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0) {
1271 remove_blockingfilelock(ifl
);
1272 deallocate_file_lock(ifl
);
1280 retry_blockingfilelocklist(void)
1282 /* Retry all locks in the blocked list */
1283 struct file_lock
*ifl
, *nfl
, *pfl
; /* Iterator */
1284 enum partialfilelock_status pflstatus
;
1286 debuglog("Entering retry_blockingfilelocklist\n");
1289 ifl
= LIST_FIRST(&blockedlocklist_head
);
1290 debuglog("Iterator choice %p\n",ifl
);
1292 while (ifl
!= NULL
) {
1294 * SUBTLE BUG: The next element must be worked out before the
1295 * current element has been moved
1297 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1298 debuglog("Iterator choice %p\n",ifl
);
1299 debuglog("Prev iterator choice %p\n",pfl
);
1300 debuglog("Next iterator choice %p\n",nfl
);
1303 * SUBTLE BUG: The file_lock must be removed from the
1304 * old list so that it's list pointers get disconnected
1305 * before being allowed to participate in the new list
1306 * which will automatically add it in if necessary.
1309 LIST_REMOVE(ifl
, nfslocklist
);
1310 pflstatus
= lock_partialfilelock(ifl
);
1312 if (pflstatus
== PFL_GRANTED
|| pflstatus
== PFL_GRANTED_DUPLICATE
) {
1313 debuglog("Granted blocked lock\n");
1314 /* lock granted and is now being used */
1315 send_granted(ifl
,0);
1316 /* XXX should undo lock if send_granted fails */
1318 /* Reinsert lock back into same place in blocked list */
1319 debuglog("Replacing blocked lock\n");
1321 LIST_INSERT_AFTER(pfl
, ifl
, nfslocklist
);
1323 /* ifl is the only elem. in the list */
1324 LIST_INSERT_HEAD(&blockedlocklist_head
, ifl
, nfslocklist
);
1327 /* Valid increment behavior regardless of state of ifl */
1329 /* if a lock was granted incrementing pfl would make it nfl */
1330 if (pfl
!= NULL
&& (LIST_NEXT(pfl
, nfslocklist
) != nfl
))
1331 pfl
= LIST_NEXT(pfl
, nfslocklist
);
1333 pfl
= LIST_FIRST(&blockedlocklist_head
);
1336 debuglog("Exiting retry_blockingfilelocklist\n");
1340 * Below here are routines associated with manipulating all
1341 * aspects of the partial file locking system (list, hardware, etc.)
1345 * Please note that lock monitoring must be done at this level which
1346 * keeps track of *individual* lock requests on lock and unlock
1348 * XXX: Split unlocking is going to make the unlock code miserable
1352 * lock_partialfilelock:
1354 * Argument fl gets modified as its list housekeeping entries get modified
1355 * upon insertion into the NFS lock list
1357 * This routine makes several assumptions:
1358 * 1) It (will) pass locks through to flock to lock the entire underlying file
1359 * and then parcel out NFS locks if it gets control of the file.
1360 * This matches the old rpc.lockd file semantics (except where it
1361 * is now more correct). It is the safe solution, but will cause
1362 * overly restrictive blocking if someone is trying to use the
1363 * underlying files without using NFS. This appears to be an
1364 * acceptable tradeoff since most people use standalone NFS servers.
1365 * XXX: The right solution is probably kevent combined with fcntl
1367 * 2) Nothing modifies the lock lists between testing and granting
1368 * I have no idea whether this is a useful assumption or not
1371 enum partialfilelock_status
1372 lock_partialfilelock(struct file_lock
*fl
)
1374 enum partialfilelock_status retval
;
1375 enum nfslock_status lnlstatus
;
1376 enum hwlock_status hwstatus
;
1378 debuglog("Entering lock_partialfilelock\n");
1380 retval
= PFL_DENIED
;
1383 * Execute the NFS lock first, if possible, as it is significantly
1384 * easier and less expensive to undo than the filesystem lock
1387 lnlstatus
= lock_nfslock(fl
);
1389 switch (lnlstatus
) {
1391 case NFS_GRANTED_DUPLICATE
:
1393 * At this point, the NFS lock is allocated and active.
1394 * Remember to clean it up if the hardware lock fails
1396 hwstatus
= lock_hwlock(fl
);
1400 case HW_GRANTED_DUPLICATE
:
1401 debuglog("HW GRANTED\n");
1403 * XXX: Fixme: Check hwstatus for duplicate when
1404 * true partial file locking and accounting is
1405 * done on the hardware
1407 if (lnlstatus
== NFS_GRANTED_DUPLICATE
) {
1408 retval
= PFL_GRANTED_DUPLICATE
;
1410 retval
= PFL_GRANTED
;
1412 if (fl
->flags
& LOCK_MON
)
1413 monitor_lock_host(fl
->client_name
);
1416 debuglog("HW RESERR\n");
1417 retval
= PFL_HWRESERR
;
1420 debuglog("HW DENIED\n");
1421 retval
= PFL_HWDENIED
;
1424 debuglog("Unmatched hwstatus %d\n",hwstatus
);
1428 if (retval
!= PFL_GRANTED
&&
1429 retval
!= PFL_GRANTED_DUPLICATE
) {
1430 /* Clean up the NFS lock */
1431 debuglog("Deleting trial NFS lock\n");
1436 retval
= PFL_NFSDENIED
;
1439 retval
= PFL_NFSRESERR
;
1441 debuglog("Unmatched lnlstatus %d\n");
1442 retval
= PFL_NFSDENIED_NOLOCK
;
1447 * By the time fl reaches here, it is completely free again on
1448 * failure. The NFS lock done before attempting the
1449 * hardware lock has been backed out
1452 if (retval
== PFL_NFSDENIED
|| retval
== PFL_HWDENIED
) {
1453 /* Once last chance to check the lock */
1454 if (fl
->blocking
== 1) {
1455 /* Queue the lock */
1456 debuglog("BLOCKING LOCK RECEIVED\n");
1457 retval
= (retval
== PFL_NFSDENIED
?
1458 PFL_NFSBLOCKED
: PFL_HWBLOCKED
);
1459 add_blockingfilelock(fl
);
1462 /* Leave retval alone, it's already correct */
1463 debuglog("Lock denied. Non-blocking failure\n");
1468 debuglog("Exiting lock_partialfilelock\n");
1474 * unlock_partialfilelock:
1476 * Given a file_lock, unlock all locks which match.
1478 * Note that a given lock might have to unlock ITSELF! See
1479 * clear_partialfilelock for example.
1482 enum partialfilelock_status
1483 unlock_partialfilelock(const struct file_lock
*fl
)
1485 struct file_lock
*lfl
,*rfl
,*releasedfl
,*selffl
;
1486 enum partialfilelock_status retval
;
1487 enum nfslock_status unlstatus
;
1488 enum hwlock_status unlhwstatus
, lhwstatus
;
1490 debuglog("Entering unlock_partialfilelock\n");
1496 retval
= PFL_DENIED
;
1499 * There are significant overlap and atomicity issues
1500 * with partially releasing a lock. For example, releasing
1501 * part of an NFS shared lock does *not* always release the
1502 * corresponding part of the file since there is only one
1503 * rpc.lockd UID but multiple users could be requesting it
1504 * from NFS. Also, an unlock request should never allow
1505 * another process to gain a lock on the remaining parts.
1506 * ie. Always apply the new locks before releasing the
1511 * Loop is required since multiple little locks
1512 * can be allocated and then deallocated with one
1515 * The loop is required to be here so that the nfs &
1516 * hw subsystems do not need to communicate with one
1521 debuglog("Value of releasedfl: %p\n",releasedfl
);
1522 /* lfl&rfl are created *AND* placed into the NFS lock list if required */
1523 unlstatus
= unlock_nfslock(fl
, &releasedfl
, &lfl
, &rfl
);
1524 debuglog("Value of releasedfl: %p\n",releasedfl
);
1527 /* XXX: This is grungy. It should be refactored to be cleaner */
1529 lhwstatus
= lock_hwlock(lfl
);
1530 if (lhwstatus
!= HW_GRANTED
&&
1531 lhwstatus
!= HW_GRANTED_DUPLICATE
) {
1532 debuglog("HW duplicate lock failure for left split\n");
1534 if (lfl
->flags
& LOCK_MON
)
1535 monitor_lock_host(lfl
->client_name
);
1539 lhwstatus
= lock_hwlock(rfl
);
1540 if (lhwstatus
!= HW_GRANTED
&&
1541 lhwstatus
!= HW_GRANTED_DUPLICATE
) {
1542 debuglog("HW duplicate lock failure for right split\n");
1544 if (rfl
->flags
& LOCK_MON
)
1545 monitor_lock_host(rfl
->client_name
);
1548 switch (unlstatus
) {
1550 /* Attempt to unlock on the hardware */
1551 debuglog("NFS unlock granted. Attempting hardware unlock\n");
1553 /* This call *MUST NOT* unlock the two newly allocated locks */
1554 unlhwstatus
= unlock_hwlock(fl
);
1555 debuglog("HW unlock returned with code %d\n",unlhwstatus
);
1557 switch (unlhwstatus
) {
1559 debuglog("HW unlock granted\n");
1560 if (releasedfl
->flags
& LOCK_MON
)
1561 unmonitor_lock_host(releasedfl
->client_name
);
1562 retval
= PFL_GRANTED
;
1564 case HW_DENIED_NOLOCK
:
1565 /* Huh?!?! This shouldn't happen */
1566 debuglog("HW unlock denied no lock\n");
1567 retval
= PFL_HWRESERR
;
1568 /* Break out of do-while */
1569 unlstatus
= NFS_RESERR
;
1572 debuglog("HW unlock failed\n");
1573 retval
= PFL_HWRESERR
;
1574 /* Break out of do-while */
1575 unlstatus
= NFS_RESERR
;
1579 debuglog("Exiting with status retval: %d\n",retval
);
1581 // XXX sending granted messages before unlock response
1582 // XXX causes unlock response to be corrupted?
1583 // XXX Workaround is to move this to nlm_prot_svc.c
1584 // XXX after the unlock response is sent.
1585 // retry_blockingfilelocklist();
1587 case NFS_DENIED_NOLOCK
:
1588 retval
= PFL_GRANTED
;
1589 debuglog("All locks cleaned out\n");
1592 retval
= PFL_NFSRESERR
;
1593 debuglog("NFS unlock failure\n");
1598 if (releasedfl
!= NULL
) {
1599 if (fl
== releasedfl
) {
1601 * XXX: YECHHH!!! Attempt to unlock self succeeded
1602 * but we can't deallocate the space yet. This is what
1603 * happens when you don't write malloc and free together
1605 debuglog("Attempt to unlock self\n");
1606 selffl
= releasedfl
;
1609 * XXX: this deallocation *still* needs to migrate closer
1610 * to the allocation code way up in get_lock or the allocation
1611 * code needs to migrate down (violation of "When you write
1612 * malloc you must write free")
1615 deallocate_file_lock(releasedfl
);
1619 } while (unlstatus
== NFS_GRANTED
);
1621 if (selffl
!= NULL
) {
1623 * This statement wipes out the incoming file lock (fl)
1624 * in spite of the fact that it is declared const
1626 debuglog("WARNING! Destroying incoming lock pointer\n");
1627 deallocate_file_lock(selffl
);
1630 debuglog("Exiting unlock_partialfilelock\n");
1636 * clear_partialfilelock
1638 * Normally called in response to statd state number change.
1639 * Wipe out all locks held by a host. As a bonus, the act of
1640 * doing so should automatically clear their statd entries and
1641 * unmonitor the host.
1645 clear_partialfilelock(const char *hostname
)
1647 struct file_lock
*ifl
, *nfl
;
1649 /* Clear blocking file lock list */
1650 clear_blockingfilelock(hostname
);
1652 /* do all required unlocks */
1653 /* Note that unlock can smash the current pointer to a lock */
1656 * Normally, LIST_FOREACH is called for, but since
1657 * the current element *is* the iterator, deleting it
1658 * would mess up the iteration. Thus, a next element
1659 * must be used explicitly
1662 ifl
= LIST_FIRST(&nfslocklist_head
);
1664 while (ifl
!= NULL
) {
1665 nfl
= LIST_NEXT(ifl
, nfslocklist
);
1667 if (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0) {
1668 /* Unlock destroys ifl out from underneath */
1669 unlock_partialfilelock(ifl
);
1670 /* ifl is NO LONGER VALID AT THIS POINT */
1677 * test_partialfilelock:
1679 enum partialfilelock_status
1680 test_partialfilelock(const struct file_lock
*fl
,
1681 struct file_lock
**conflicting_fl
)
1683 enum partialfilelock_status retval
;
1684 enum nfslock_status teststatus
;
1686 debuglog("Entering testpartialfilelock...\n");
1688 retval
= PFL_DENIED
;
1690 teststatus
= test_nfslock(fl
, conflicting_fl
);
1691 debuglog("test_partialfilelock: teststatus %d\n",teststatus
);
1693 if (teststatus
== NFS_GRANTED
|| teststatus
== NFS_GRANTED_DUPLICATE
) {
1694 /* XXX: Add the underlying filesystem locking code */
1695 retval
= (teststatus
== NFS_GRANTED
) ?
1696 PFL_GRANTED
: PFL_GRANTED_DUPLICATE
;
1697 debuglog("Dumping locks...\n");
1699 dump_filelock(*conflicting_fl
);
1700 debuglog("Done dumping locks...\n");
1702 retval
= PFL_NFSDENIED
;
1703 debuglog("NFS test denied.\n");
1705 debuglog("Conflicting.\n");
1706 dump_filelock(*conflicting_fl
);
1709 debuglog("Exiting testpartialfilelock...\n");
1715 * Below here are routines associated with translating the partial file locking
1716 * codes into useful codes to send back to the NFS RPC messaging system
1720 * These routines translate the (relatively) useful return codes back onto
1721 * the few return codes which the nlm subsystems wishes to trasmit
1725 do_test(struct file_lock
*fl
, struct file_lock
**conflicting_fl
)
1727 enum partialfilelock_status pfsret
;
1728 enum nlm_stats retval
;
1730 debuglog("Entering do_test...\n");
1732 pfsret
= test_partialfilelock(fl
,conflicting_fl
);
1736 debuglog("PFL test lock granted\n");
1738 dump_filelock(*conflicting_fl
);
1739 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1741 case PFL_GRANTED_DUPLICATE
:
1742 debuglog("PFL test lock granted--duplicate id detected\n");
1744 dump_filelock(*conflicting_fl
);
1745 debuglog("Clearing conflicting_fl for call semantics\n");
1746 *conflicting_fl
= NULL
;
1747 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1751 debuglog("PFL test lock denied\n");
1753 dump_filelock(*conflicting_fl
);
1754 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
1758 debuglog("PFL test lock resource fail\n");
1760 dump_filelock(*conflicting_fl
);
1761 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
1764 debuglog("PFL test lock *FAILED*\n");
1766 dump_filelock(*conflicting_fl
);
1767 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
1771 debuglog("Exiting do_test...\n");
1777 * do_lock: Try to acquire a lock
1779 * This routine makes a distinction between NLM versions. I am pretty
1780 * convinced that this should be abstracted out and bounced up a level
1784 do_lock(struct file_lock
*fl
)
1786 enum partialfilelock_status pfsret
;
1787 enum nlm_stats retval
;
1789 debuglog("Entering do_lock...\n");
1791 pfsret
= lock_partialfilelock(fl
);
1795 debuglog("PFL lock granted");
1797 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1799 case PFL_GRANTED_DUPLICATE
:
1800 debuglog("PFL lock granted--duplicate id detected");
1802 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1806 debuglog("PFL_NFS lock denied");
1808 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
1810 case PFL_NFSBLOCKED
:
1812 debuglog("PFL_NFS blocking lock denied. Queued.\n");
1814 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_blocked
: nlm_blocked
;
1818 debuglog("PFL lock resource alocation fail\n");
1820 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
1823 debuglog("PFL lock *FAILED*");
1825 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
1829 debuglog("Exiting do_lock...\n");
1835 do_unlock(struct file_lock
*fl
)
1837 enum partialfilelock_status pfsret
;
1838 enum nlm_stats retval
;
1840 debuglog("Entering do_unlock...\n");
1841 pfsret
= unlock_partialfilelock(fl
);
1845 debuglog("PFL unlock granted");
1847 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1851 debuglog("PFL_NFS unlock denied");
1853 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied
: nlm_denied
;
1855 case PFL_NFSDENIED_NOLOCK
:
1856 case PFL_HWDENIED_NOLOCK
:
1857 debuglog("PFL_NFS no lock found\n");
1858 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_granted
: nlm_granted
;
1862 debuglog("PFL unlock resource failure");
1864 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_denied_nolocks
: nlm_denied_nolocks
;
1867 debuglog("PFL unlock *FAILED*");
1869 retval
= (fl
->flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
1873 debuglog("Exiting do_unlock...\n");
1881 * This routine is non-existent because it doesn't have a return code.
1882 * It is here for completeness in case someone *does* need to do return
1883 * codes later. A decent compiler should optimize this away.
1887 do_clear(const char *hostname
)
1890 clear_partialfilelock(hostname
);
1894 * The following routines are all called from the code which the
1899 * testlock(): inform the caller if the requested lock would be granted
1901 * returns NULL if lock would granted
1902 * returns pointer to a conflicting nlm4_holder if not
1905 struct nlm4_holder
*
1906 testlock(struct nlm4_lock
*lock
, bool_t exclusive
, int flags __unused
)
1908 struct file_lock test_fl
, *conflicting_fl
;
1910 bzero(&test_fl
, sizeof(test_fl
));
1912 test_fl
.filehandle
.n_len
= lock
->fh
.n_len
;
1913 test_fl
.filehandle
.n_bytes
= lock
->fh
.n_bytes
;
1914 copy_nlm4_lock_to_nlm4_holder(lock
, exclusive
, &test_fl
.client
);
1917 do_test(&test_fl
, &conflicting_fl
);
1919 if (conflicting_fl
== NULL
) {
1920 debuglog("No conflicting lock found\n");
1924 debuglog("Found conflicting lock\n");
1925 dump_filelock(conflicting_fl
);
1927 return (&conflicting_fl
->client
);
1932 * getlock: try to aquire the lock.
1933 * If file is already locked and we can sleep, put the lock in the list with
1934 * status LKST_WAITING; it'll be processed later.
1935 * Otherwise try to lock. If we're allowed to block, fork a child which
1936 * will do the blocking lock.
1940 getlock(nlm4_lockargs
*lckarg
, struct svc_req
*rqstp
, const int flags
)
1942 struct file_lock
*newfl
;
1943 enum nlm_stats retval
;
1945 debuglog("Entering getlock...\n");
1947 if (grace_expired
== 0 && lckarg
->reclaim
== 0)
1948 return (flags
& LOCK_V4
) ?
1949 nlm4_denied_grace_period
: nlm_denied_grace_period
;
1951 /* allocate new file_lock for this request */
1952 newfl
= allocate_file_lock(&lckarg
->alock
.oh
, &lckarg
->cookie
, &lckarg
->alock
.fh
);
1953 if (newfl
== NULL
) {
1954 syslog(LOG_NOTICE
, "lock allocate failed: %s", strerror(errno
));
1956 return (flags
& LOCK_V4
) ?
1957 nlm4_denied_nolocks
: nlm_denied_nolocks
;
1960 if (lckarg
->alock
.fh
.n_len
!= sizeof(fhandle_t
)) {
1961 debuglog("recieved fhandle size %d, local size %d",
1962 lckarg
->alock
.fh
.n_len
, (int)sizeof(fhandle_t
));
1965 fill_file_lock(newfl
,
1966 (struct sockaddr
*)svc_getcaller(rqstp
->rq_xprt
),
1967 lckarg
->exclusive
, lckarg
->alock
.svid
, lckarg
->alock
.l_offset
,
1968 lckarg
->alock
.l_len
,
1969 lckarg
->alock
.caller_name
, lckarg
->state
, 0, flags
, lckarg
->block
);
1972 * newfl is now fully constructed and deallocate_file_lock
1973 * can now be used to delete it
1977 debuglog("Pointer to new lock is %p\n",newfl
);
1979 retval
= do_lock(newfl
);
1981 debuglog("Pointer to new lock is %p\n",newfl
);
1987 /* case nlm_granted: is the same as nlm4_granted */
1988 /* do_mon(lckarg->alock.caller_name); */
1991 /* case nlm_blocked: is the same as nlm4_blocked */
1992 /* do_mon(lckarg->alock.caller_name); */
1995 deallocate_file_lock(newfl
);
1999 debuglog("Exiting getlock...\n");
2005 /* unlock a filehandle */
2007 unlock(nlm4_lock
*lock
, const int flags __unused
)
2009 struct file_lock fl
;
2014 debuglog("Entering unlock...\n");
2016 bzero(&fl
,sizeof(struct file_lock
));
2017 fl
.filehandle
.n_len
= lock
->fh
.n_len
;
2018 fl
.filehandle
.n_bytes
= lock
->fh
.n_bytes
;
2020 copy_nlm4_lock_to_nlm4_holder(lock
, 0, &fl
.client
);
2022 err
= do_unlock(&fl
);
2026 debuglog("Exiting unlock...\n");
2032 * XXX: The following monitor/unmonitor routines
2033 * have not been extensively tested (ie. no regression
2034 * script exists like for the locking sections
2038 * monitor_lock_host: monitor lock hosts locally with a ref count and
2042 monitor_lock_host(const char *hostname
)
2044 struct host
*ihp
, *nhp
;
2046 struct sm_stat_res sres
;
2047 int rpcret
, statflag
;
2052 LIST_FOREACH(ihp
, &hostlst_head
, hostlst
) {
2053 if (strncmp(hostname
, ihp
->name
, SM_MAXSTRLEN
) == 0) {
2054 /* Host is already monitored, bump refcount */
2056 /* Host should only be in the monitor list once */
2061 /* Host is not yet monitored, add it */
2062 nhp
= malloc(sizeof(struct host
));
2065 debuglog("Unable to allocate entry for statd mon\n");
2069 /* Allocated new host entry, now fill the fields */
2070 strncpy(nhp
->name
, hostname
, SM_MAXSTRLEN
);
2072 debuglog("Locally Monitoring host %16s\n",hostname
);
2074 debuglog("Attempting to tell statd\n");
2076 bzero(&smon
,sizeof(smon
));
2078 smon
.mon_id
.mon_name
= nhp
->name
;
2079 smon
.mon_id
.my_id
.my_name
= "localhost\0";
2081 smon
.mon_id
.my_id
.my_prog
= NLM_PROG
;
2082 smon
.mon_id
.my_id
.my_vers
= NLM_SM
;
2083 smon
.mon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
;
2085 rpcret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_MON
, xdr_mon
,
2086 &smon
, xdr_sm_stat_res
, &sres
);
2089 if (sres
.res_stat
== stat_fail
) {
2090 debuglog("Statd call failed\n");
2096 debuglog("Rpc call to statd failed with return value: %d\n",
2101 if (statflag
== 1) {
2102 LIST_INSERT_HEAD(&hostlst_head
, nhp
, hostlst
);
2110 * unmonitor_lock_host: clear monitor ref counts and inform statd when gone
2113 unmonitor_lock_host(const char *hostname
)
2116 struct mon_id smon_id
;
2117 struct sm_stat smstat
;
2122 for( ihp
=LIST_FIRST(&hostlst_head
); ihp
!= NULL
;
2123 ihp
=LIST_NEXT(ihp
, hostlst
)) {
2124 if (strncmp(hostname
, ihp
->name
, SM_MAXSTRLEN
) == 0) {
2125 /* Host is monitored, bump refcount */
2127 /* Host should only be in the monitor list once */
2133 debuglog("Could not find host %16s in mon list\n", hostname
);
2137 if (ihp
->refcnt
> 0)
2140 if (ihp
->refcnt
< 0) {
2141 debuglog("Negative refcount!: %d\n",
2145 debuglog("Attempting to unmonitor host %16s\n", hostname
);
2147 bzero(&smon_id
,sizeof(smon_id
));
2149 smon_id
.mon_name
= (char *)hostname
;
2150 smon_id
.my_id
.my_name
= "localhost";
2151 smon_id
.my_id
.my_prog
= NLM_PROG
;
2152 smon_id
.my_id
.my_vers
= NLM_SM
;
2153 smon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
;
2155 rpcret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON
, xdr_mon
,
2156 &smon_id
, xdr_sm_stat_res
, &smstat
);
2159 debuglog("Rpc call to unmonitor statd failed with "
2160 " return value: %d\n", rpcret
);
2163 LIST_REMOVE(ihp
, hostlst
);
2168 * notify: Clear all locks from a host if statd complains
2170 * XXX: This routine has not been thoroughly tested. However, neither
2171 * had the old one been. It used to compare the statd crash state counter
2172 * to the current lock state. The upshot of this was that it basically
2173 * cleared all locks from the specified host 99% of the time (with the
2174 * other 1% being a bug). Consequently, the assumption is that clearing
2175 * all locks from a host when notified by statd is acceptable.
2177 * Please note that this routine skips the usual level of redirection
2178 * through a do_* type routine. This introduces a possible level of
2179 * error and might better be written as do_notify and take this one out.
2184 notify(const char *hostname
, const int state
)
2186 debuglog("notify from %s, new state %d", hostname
, state
);
2192 debuglog("Leaving notify\n");
2196 send_granted(fl
, opcode
)
2197 struct file_lock
*fl
;
2198 int opcode __unused
;
2202 struct timeval timeo
;
2204 static struct nlm_res retval
;
2205 static struct nlm4_res retval4
;
2207 debuglog("About to send granted on blocked lock\n");
2209 debuglog("Blowing off return send\n");
2211 cli
= get_client(fl
->addr
,
2212 (fl
->flags
& LOCK_V4
) ? NLM_VERS4
: NLM_VERS
);
2214 syslog(LOG_NOTICE
, "failed to get CLIENT for %s",
2217 * We fail to notify remote that the lock has been granted.
2218 * The client will timeout and retry, the lock will be
2219 * granted at this time.
2224 timeo
.tv_usec
= (fl
->flags
& LOCK_ASYNC
) ? 0 : 500000; /* 0.5s */
2226 if (fl
->flags
& LOCK_V4
) {
2227 static nlm4_testargs res
;
2228 res
.cookie
= fl
->client_cookie
;
2229 res
.exclusive
= fl
->client
.exclusive
;
2230 res
.alock
.caller_name
= fl
->client_name
;
2231 res
.alock
.fh
.n_len
= fl
->filehandle
.n_len
;
2232 res
.alock
.fh
.n_bytes
= fl
->filehandle
.n_bytes
;
2233 res
.alock
.oh
= fl
->client
.oh
;
2234 res
.alock
.svid
= fl
->client
.svid
;
2235 res
.alock
.l_offset
= fl
->client
.l_offset
;
2236 res
.alock
.l_len
= fl
->client
.l_len
;
2237 debuglog("sending v4 reply%s",
2238 (fl
->flags
& LOCK_ASYNC
) ? " (async)":"");
2239 if (fl
->flags
& LOCK_ASYNC
) {
2240 success
= clnt_call(cli
, NLM4_GRANTED_MSG
,
2241 xdr_nlm4_testargs
, &res
, xdr_void
, &dummy
, timeo
);
2243 success
= clnt_call(cli
, NLM4_GRANTED
,
2244 xdr_nlm4_testargs
, &res
, xdr_nlm4_res
,
2248 static nlm_testargs res
;
2250 res
.cookie
= fl
->client_cookie
;
2251 res
.exclusive
= fl
->client
.exclusive
;
2252 res
.alock
.caller_name
= fl
->client_name
;
2253 res
.alock
.fh
.n_len
= fl
->filehandle
.n_len
;
2254 res
.alock
.fh
.n_bytes
= fl
->filehandle
.n_bytes
;
2255 res
.alock
.oh
= fl
->client
.oh
;
2256 res
.alock
.svid
= fl
->client
.svid
;
2257 res
.alock
.l_offset
= fl
->client
.l_offset
;
2258 res
.alock
.l_len
= fl
->client
.l_len
;
2259 debuglog("sending v1 reply%s",
2260 (fl
->flags
& LOCK_ASYNC
) ? " (async)":"");
2261 if (fl
->flags
& LOCK_ASYNC
) {
2262 success
= clnt_call(cli
, NLM_GRANTED_MSG
,
2263 xdr_nlm_testargs
, &res
, xdr_void
, &dummy
, timeo
);
2265 success
= clnt_call(cli
, NLM_GRANTED
,
2266 xdr_nlm_testargs
, &res
, xdr_nlm_res
,
2270 if (debug_level
> 2)
2271 debuglog("clnt_call returns %d(%s) for granted",
2272 success
, clnt_sperrno(success
));
2277 * getshare: try to acquire a share reservation
2280 getshare(nlm_shareargs
*shrarg
, struct svc_req
*rqstp
, const int flags
)
2282 struct sharefile
*shrfile
;
2283 struct file_share
*sh
;
2285 debuglog("Entering getshare...\n");
2287 if (grace_expired
== 0 && shrarg
->reclaim
== 0) {
2288 debuglog("getshare denied - grace period\n");
2289 return (flags
& LOCK_V4
) ?
2290 nlm4_denied_grace_period
:
2291 nlm_denied_grace_period
;
2294 /* find file in list of share files */
2295 LIST_FOREACH(shrfile
, &nfssharefilelist_head
, sharefilelist
) {
2296 if ((shrarg
->share
.fh
.n_len
== shrfile
->filehandle
.n_len
) &&
2297 (bcmp(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2298 shrarg
->share
.fh
.n_len
) == 0)) {
2299 /* shrfile is the correct file */
2304 /* if share file not found, create a new share file */
2307 fd
= fhopen((fhandle_t
*)shrarg
->share
.fh
.n_bytes
, O_RDONLY
);
2309 debuglog("fhopen failed (from %16s): %32s\n",
2310 shrarg
->share
.caller_name
, strerror(errno
));
2311 if ((flags
& LOCK_V4
) == 0)
2315 return nlm4_stale_fh
;
2320 shrfile
= malloc(sizeof(struct sharefile
));
2322 debuglog("getshare failed: can't allocate sharefile\n");
2324 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2326 shrfile
->filehandle
.n_len
= shrarg
->share
.fh
.n_len
;
2327 shrfile
->filehandle
.n_bytes
= malloc(shrarg
->share
.fh
.n_len
);
2328 if (!shrfile
->filehandle
.n_bytes
) {
2329 debuglog("getshare failed: can't allocate sharefile filehandle\n");
2332 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2334 bcopy(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2335 shrarg
->share
.fh
.n_len
);
2337 shrfile
->refcount
= 0;
2338 shrfile
->sharelist_head
.lh_first
= NULL
;
2339 LIST_INSERT_HEAD(&nfssharefilelist_head
, shrfile
, sharefilelist
);
2342 /* compare request mode/access to current shares */
2343 LIST_FOREACH(sh
, &shrfile
->sharelist_head
, nfssharelist
) {
2344 /* if request host/owner matches a current share... */
2345 if ((strncmp(shrarg
->share
.caller_name
, sh
->client_name
, SM_MAXSTRLEN
) == 0) &&
2346 same_netobj(&shrarg
->share
.oh
, &sh
->oh
)) {
2347 /* ...then just update share mode/access */
2348 sh
->mode
= shrarg
->share
.mode
;
2349 sh
->access
= shrarg
->share
.access
;
2350 debuglog("getshare: updated existing share\n");
2353 if (((shrarg
->share
.mode
& sh
->access
) != 0) ||
2354 ((shrarg
->share
.access
& sh
->mode
) != 0)) {
2355 /* share request conflicts with existing share */
2356 debuglog("getshare: conflicts with existing share\n");
2361 /* create/init new share */
2362 sh
= malloc(sizeof(struct file_share
));
2364 debuglog("getshare failed: can't allocate share\n");
2365 if (!shrfile
->refcount
) {
2366 LIST_REMOVE(shrfile
, sharefilelist
);
2368 free(shrfile
->filehandle
.n_bytes
);
2371 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2373 sh
->oh
.n_len
= shrarg
->share
.oh
.n_len
;
2374 sh
->oh
.n_bytes
= malloc(sh
->oh
.n_len
);
2375 if (!sh
->oh
.n_bytes
) {
2376 debuglog("getshare failed: can't allocate share owner handle\n");
2378 if (!shrfile
->refcount
) {
2379 LIST_REMOVE(shrfile
, sharefilelist
);
2381 free(shrfile
->filehandle
.n_bytes
);
2384 return (flags
& LOCK_V4
) ? nlm4_failed
: nlm_denied
;
2386 strncpy(sh
->client_name
, shrarg
->share
.caller_name
, SM_MAXSTRLEN
);
2387 sh
->mode
= shrarg
->share
.mode
;
2388 sh
->access
= shrarg
->share
.access
;
2390 /* insert new share into file's share list */
2391 LIST_INSERT_HEAD(&shrfile
->sharelist_head
, sh
, nfssharelist
);
2392 shrfile
->refcount
++;
2394 debuglog("Exiting getshare...\n");
2400 /* remove a share reservation */
2402 unshare(nlm_shareargs
*shrarg
, struct svc_req
*rqstp
)
2404 struct sharefile
*shrfile
;
2405 struct file_share
*sh
;
2407 debuglog("Entering unshare...\n");
2409 /* find file in list of share files */
2410 LIST_FOREACH(shrfile
, &nfssharefilelist_head
, sharefilelist
) {
2411 if ((shrarg
->share
.fh
.n_len
== shrfile
->filehandle
.n_len
) &&
2412 (bcmp(shrarg
->share
.fh
.n_bytes
, shrfile
->filehandle
.n_bytes
,
2413 shrarg
->share
.fh
.n_len
) == 0)) {
2414 /* shrfile is the correct file */
2419 /* if share file not found, return success (per spec) */
2421 debuglog("unshare: no such share file\n");
2426 LIST_FOREACH(sh
, &shrfile
->sharelist_head
, nfssharelist
) {
2427 /* if request host/owner matches a current share... */
2428 if ((strncmp(shrarg
->share
.caller_name
, sh
->client_name
, SM_MAXSTRLEN
) == 0) &&
2429 same_netobj(&shrarg
->share
.oh
, &sh
->oh
))
2433 /* if share not found, return success (per spec) */
2435 debuglog("unshare: no such share\n");
2439 /* remove share from file and deallocate */
2440 shrfile
->refcount
--;
2441 LIST_REMOVE(sh
, nfssharelist
);
2442 free(sh
->oh
.n_bytes
);
2445 /* if file has no more shares, deallocate share file */
2446 if (!shrfile
->refcount
) {
2447 debuglog("unshare: file has no more shares\n");
2448 LIST_REMOVE(shrfile
, sharefilelist
);
2450 free(shrfile
->filehandle
.n_bytes
);
2454 debuglog("Exiting unshare...\n");
2462 * Wipe out all non-monitored locks and shares held by a host.
2466 do_free_all(const char *hostname
)
2468 struct file_lock
*ifl
, *nfl
;
2469 struct sharefile
*shrfile
, *nshrfile
;
2470 struct file_share
*ifs
, *nfs
;
2472 /* clear non-monitored blocking file locks */
2473 ifl
= LIST_FIRST(&blockedlocklist_head
);
2474 while (ifl
!= NULL
) {
2475 nfl
= LIST_NEXT(ifl
, nfslocklist
);
2477 if (((ifl
->flags
& LOCK_MON
) == 0) &&
2478 (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0)) {
2479 remove_blockingfilelock(ifl
);
2480 deallocate_file_lock(ifl
);
2486 /* clear non-monitored file locks */
2487 ifl
= LIST_FIRST(&nfslocklist_head
);
2488 while (ifl
!= NULL
) {
2489 nfl
= LIST_NEXT(ifl
, nfslocklist
);
2491 if (((ifl
->flags
& LOCK_MON
) == 0) &&
2492 (strncmp(hostname
, ifl
->client_name
, SM_MAXSTRLEN
) == 0)) {
2493 /* Unlock destroys ifl out from underneath */
2494 unlock_partialfilelock(ifl
);
2495 /* ifl is NO LONGER VALID AT THIS POINT */
2502 shrfile
= LIST_FIRST(&nfssharefilelist_head
);
2503 while (shrfile
!= NULL
) {
2504 nshrfile
= LIST_NEXT(shrfile
, sharefilelist
);
2506 ifs
= LIST_FIRST(&shrfile
->sharelist_head
);
2507 while (ifs
!= NULL
) {
2508 nfs
= LIST_NEXT(ifs
, nfssharelist
);
2510 if (strncmp(hostname
, ifs
->client_name
, SM_MAXSTRLEN
) == 0) {
2511 shrfile
->refcount
--;
2512 LIST_REMOVE(ifs
, nfssharelist
);
2513 free(ifs
->oh
.n_bytes
);
2520 if (!shrfile
->refcount
) {
2521 LIST_REMOVE(shrfile
, sharefilelist
);
2523 free(shrfile
->filehandle
.n_bytes
);
2535 * Routines below here have not been modified in the overhaul
2539 * Are these two routines still required since lockd is not spawning off
2540 * children to service locks anymore? Presumably they were originally
2541 * put in place to prevent a one child from changing the lock list out
2542 * from under another one.
2550 sigemptyset(&block
);
2551 sigaddset(&block
, SIGCHLD
);
2553 if (sigprocmask(SIG_BLOCK
, &block
, NULL
) < 0) {
2554 syslog(LOG_WARNING
, "siglock failed: %s", strerror(errno
));
2563 sigemptyset(&block
);
2564 sigaddset(&block
, SIGCHLD
);
2566 if (sigprocmask(SIG_UNBLOCK
, &block
, NULL
) < 0) {
2567 syslog(LOG_WARNING
, "sigunlock failed: %s", strerror(errno
));