MFC: kernel-mode NFS lock manager.

This commit is contained in:
dfr 2008-04-24 10:46:25 +00:00
parent 5f5afc3996
commit 0536363c85
48 changed files with 2888 additions and 618 deletions

View File

@ -4,7 +4,8 @@
# machine-independent gen sources
.PATH: ${.CURDIR}/${MACHINE_ARCH}/gen ${.CURDIR}/gen
SRCS+= __xuname.c _pthread_stubs.c _rand48.c _spinlock_stub.c _thread_init.c \
SRCS+= __getosreldate.c __xuname.c \
_pthread_stubs.c _rand48.c _spinlock_stub.c _thread_init.c \
alarm.c arc4random.c assert.c basename.c check_utility_compat.c \
clock.c closedir.c confstr.c \
crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \

View File

@ -74,7 +74,7 @@ lockf(filedes, function, size)
fl.l_type = F_WRLCK;
if (_fcntl(filedes, F_GETLK, &fl) == -1)
return (-1);
if (fl.l_type == F_UNLCK || fl.l_pid == getpid())
if (fl.l_type == F_UNLCK || (fl.l_sysid == 0 && fl.l_pid == getpid()))
return (0);
errno = EAGAIN;
return (-1);

View File

@ -135,4 +135,13 @@ extern const char *__progname;
*/
extern struct _spinlock *__malloc_lock;
/*
* Get kern.osreldate to detect ABI revisions. Explicitly
* ignores value of $OSVERSION and caches result.
*/
extern int __getosreldate(void);
/* Without back-compat translation */
extern int __sys_fcntl(int, int, ...);
#endif /* _LIBC_PRIVATE_H_ */

View File

@ -602,10 +602,11 @@ svc_vc_recv(xprt, msg)
if (cd->nonblock) {
if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
return FALSE;
} else {
(void)xdrrec_skiprecord(xdrs);
}
xdrs->x_op = XDR_DECODE;
(void)xdrrec_skiprecord(xdrs);
if (xdr_callmsg(xdrs, msg)) {
cd->x_id = msg->rm_xid;
return (TRUE);

View File

@ -19,6 +19,10 @@
# Sources common to both syscall interfaces:
SRCS+= ftruncate.c lseek.c mmap.c pread.c pwrite.c truncate.c __error.c
.if !defined(WITHOUT_SYSCALL_COMPAT)
SRCS+= fcntl.c
PSEUDO+= _fcntl.o
.endif
# Add machine dependent asm sources:
SRCS+=${MDASM}

View File

@ -181,6 +181,7 @@ struct flock {
pid_t l_pid; /* lock owner */
short l_type; /* lock type: read/write, etc. */
short l_whence; /* type of l_start */
int l_sysid; /* remote system id or zero for local */
};
.Ed
The commands available for advisory record locking are as follows:
@ -268,9 +269,13 @@ is negative,
means end edge of the region.
The
.Fa l_pid
field is only used with
and
.Fa l_sysid
fields are only used with
.Dv F_GETLK
to return the process ID of the process holding a blocking lock.
to return the process ID of the process holding a blocking lock and
the system ID of the system that owns that process.
Locks created by the local system will have a system ID of zero.
After a successful
.Dv F_GETLK
request, the value of

View File

@ -47,7 +47,7 @@ fcntl(int fd, int cmd, ...)
arg = va_arg(args, long);
va_end(args);
if (__getosreldate() >= 800028) {
if (__getosreldate() >= 603102) {
return (__sys_fcntl(fd, cmd, arg));
} else {
if (cmd == F_GETLK || cmd == F_SETLK || cmd == F_SETLKW) {

View File

@ -574,6 +574,12 @@ __xdrrec_getrec(xdrs, statp, expectdata)
rstrm->in_header &= ~LAST_FRAG;
rstrm->last_frag = TRUE;
}
/*
* We can only reasonably expect to read once from a
* non-blocking stream. Reading the fragment header
* may have drained the stream.
*/
expectdata = FALSE;
}
n = rstrm->readit(rstrm->tcp_handle,

View File

@ -38,6 +38,7 @@ options UFS_DIRHASH # Improve performance on big directories
options MD_ROOT # MD is a potential root device
options NFSCLIENT # Network Filesystem Client
options NFSSERVER # Network Filesystem Server
options NFSLOCKD # Network Lock Manager
options NFS_ROOT # NFS usable as /, requires NFSCLIENT
options NTFS # NT File System
options MSDOSFS # MSDOS Filesystem

View File

@ -57,6 +57,7 @@ options UFS_ACL #Support for access control lists
options UFS_DIRHASH #Improve performance on big directories
options NFSCLIENT #Network Filesystem Client
options NFSSERVER #Network Filesystem Server
options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as /, requires NFSCLIENT
#options MSDOSFS #MSDOS Filesystem
options CD9660 #ISO 9660 Filesystem

View File

@ -49,6 +49,7 @@ options FFS #Berkeley Fast Filesystem
#options ROOTDEVNAME=\"ufs:md0\"
options NFSCLIENT #Network Filesystem Client
#options NFSSERVER #Network Filesystem Server
#options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as /, requires NFSCLIENT
options BOOTP_NFSROOT
options BOOTP

View File

@ -46,6 +46,7 @@ options UFS_ACL #Support for access control lists
options UFS_DIRHASH #Improve performance on big directories
options NFSCLIENT #Network Filesystem Client
options NFSSERVER #Network Filesystem Server
options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as /, requires NFSCLIENT
#options MSDOSFS #MSDOS Filesystem
options CD9660 #ISO 9660 Filesystem

View File

@ -43,6 +43,7 @@ options MD_ROOT #MD is a potential root device
options ROOTDEVNAME=\"ufs:md0\"
options NFSCLIENT #Network Filesystem Client
options NFSSERVER #Network Filesystem Server
options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as /, requires NFSCLIENT
#options MSDOSFS #MSDOS Filesystem
options CD9660 #ISO 9660 Filesystem

View File

@ -47,6 +47,7 @@ options MD_ROOT_SIZE=4096
options ROOTDEVNAME=\"ufs:md0\"
options NFSCLIENT #Network Filesystem Client
options NFSSERVER #Network Filesystem Server
options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as /, requires NFSCLIENT
#options MSDOSFS #MSDOS Filesystem
options CD9660 #ISO 9660 Filesystem

View File

@ -279,7 +279,8 @@
151 AUE_NULL UNIMPL sem_lock (BSD/OS 2.x)
152 AUE_NULL UNIMPL sem_wakeup (BSD/OS 2.x)
153 AUE_NULL UNIMPL asyncdaemon (BSD/OS 2.x)
154 AUE_NULL UNIMPL nosys
; 154 is initialised by the NLM code, if present.
154 AUE_NULL UNIMPL nlm_syscall
; 155 is initialized by the NFS code, if present.
; XXX this is a problem!!!
155 AUE_NFS_SVC UNIMPL nfssvc

View File

@ -960,6 +960,8 @@ linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
bsd_flock->l_start = (off_t)linux_flock->l_start;
bsd_flock->l_len = (off_t)linux_flock->l_len;
bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
bsd_flock->l_sysid = 0;
bsd_flock->l_sysid = 0;
}
static void

View File

@ -189,7 +189,7 @@ svr4_to_bsd_flock(iflp, oflp)
oflp->l_start = (off_t) iflp->l_start;
oflp->l_len = (off_t) iflp->l_len;
oflp->l_pid = (pid_t) iflp->l_pid;
oflp->l_sysid = iflp->l_sysid;
}
static void
@ -215,7 +215,7 @@ bsd_to_svr4_flock64(iflp, oflp)
oflp->l_whence = (short) iflp->l_whence;
oflp->l_start = (svr4_off64_t) iflp->l_start;
oflp->l_len = (svr4_off64_t) iflp->l_len;
oflp->l_sysid = 0;
oflp->l_sysid = iflp->l_sysid;
oflp->l_pid = (svr4_pid_t) iflp->l_pid;
}

View File

@ -881,6 +881,8 @@ options FDESCFS #File descriptor filesystem
options HPFS #OS/2 File system
options MSDOSFS #MS DOS File System (FAT, FAT32)
options NFSSERVER #Network File System server
# XXX don't build NFSLOCKD since it conflicts with ATM_SPANS
#options NFSLOCKD #Network Lock Manager
options NTFS #NT File System
options NULLFS #NULL filesystem
# Broken (depends on NCP):

View File

@ -1893,6 +1893,12 @@ nfsserver/nfs_srvsock.c optional nfsserver
nfsserver/nfs_srvcache.c optional nfsserver
nfsserver/nfs_srvsubs.c optional nfsserver
nfsserver/nfs_syscalls.c optional nfsserver
nlm/nlm_prot_clnt.c optional nfslockd
nlm/nlm_prot_impl.c optional nfslockd
nlm/nlm_prot_server.c optional nfslockd
nlm/nlm_prot_svc.c optional nfslockd
nlm/nlm_prot_xdr.c optional nfslockd
nlm/sm_inter_xdr.c optional nfslockd
# crypto support
opencrypto/cast.c optional crypto
opencrypto/cast.c optional ipsec ipsec_esp
@ -1932,7 +1938,27 @@ pci/xrpu.c optional xrpu pci
posix4/ksched.c optional _kposix_priority_scheduling
posix4/p1003_1b.c standard
posix4/posix4_mib.c standard
rpc/auth_none.c optional nfslockd
rpc/auth_unix.c optional nfslockd
rpc/authunix_prot.c optional nfslockd
rpc/clnt_dg.c optional nfslockd
rpc/clnt_rc.c optional nfslockd
rpc/clnt_vc.c optional nfslockd
rpc/getnetconfig.c optional nfslockd
rpc/inet_ntop.c optional nfslockd
rpc/inet_pton.c optional nfslockd
rpc/rpc_callmsg.c optional nfslockd
rpc/rpc_generic.c optional nfslockd
rpc/rpc_prot.c optional nfslockd
rpc/rpcb_clnt.c optional nfslockd
rpc/rpcb_prot.c optional nfslockd
rpc/rpcclnt.c optional nfsclient
rpc/svc.c optional nfslockd
rpc/svc_auth.c optional nfslockd
rpc/svc_auth_unix.c optional nfslockd
rpc/svc_dg.c optional nfslockd
rpc/svc_generic.c optional nfslockd
rpc/svc_vc.c optional nfslockd
security/audit/audit.c optional audit
security/audit/audit_arg.c optional audit
security/audit/audit_bsm.c optional audit
@ -2007,3 +2033,9 @@ vm/vm_pager.c standard
vm/vm_unix.c standard
vm/vm_zeroidle.c standard
vm/vnode_pager.c standard
xdr/xdr.c optional nfslockd
xdr/xdr_array.c optional nfslockd
xdr/xdr_mbuf.c optional nfslockd
xdr/xdr_mem.c optional nfslockd
xdr/xdr_reference.c optional nfslockd
xdr/xdr_sizeof.c optional nfslockd

View File

@ -389,6 +389,8 @@ TCP_SIGNATURE opt_inet.h
TCP_SACK_DEBUG opt_tcp_sack.h
TCP_DROP_SYNFIN opt_tcp_input.h
XBONEHACK
KRPC
NFSLOCKD
# Netgraph(4). Use option NETGRAPH to enable the base netgraph code.
# Each netgraph node type can be either be compiled into the kernel

View File

@ -84,6 +84,7 @@
* Prototypes for MSDOSFS vnode operations
*/
static vop_advlock_t msdosfs_advlock;
static vop_advlockasync_t msdosfs_advlockasync;
static vop_create_t msdosfs_create;
static vop_mknod_t msdosfs_mknod;
static vop_open_t msdosfs_open;
@ -1889,12 +1890,29 @@ msdosfs_advlock(ap)
return (lf_advlock(ap, &dep->de_lockf, dep->de_FileSize));
}
static int
msdosfs_advlockasync(ap)
struct vop_advlockasync_args /* {
struct vnode *a_vp;
u_char a_id;
int a_op;
struct flock *a_fl;
int a_flags;
struct task *a_task;
} */ *ap;
{
struct denode *dep = VTODE(ap->a_vp);
return (lf_advlockasync(ap, &dep->de_lockf, dep->de_FileSize));
}
/* Global vfs data structures for msdosfs */
struct vop_vector msdosfs_vnodeops = {
.vop_default = &default_vnodeops,
.vop_access = msdosfs_access,
.vop_advlock = msdosfs_advlock,
.vop_advlockasync = msdosfs_advlockasync,
.vop_bmap = msdosfs_bmap,
.vop_cachedlookup = msdosfs_lookup,
.vop_open = msdosfs_open,

View File

@ -998,8 +998,11 @@ smbfs_advlock(ap)
lkop = SMB_LOCK_EXCL;
error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
if (error) {
int oldtype = fl->l_type;
fl->l_type = F_UNLCK;
ap->a_op = F_UNLCK;
lf_advlock(ap, &np->n_lockf, size);
fl->l_type = oldtype;
}
break;
case F_UNLCK:

View File

@ -40,6 +40,7 @@ options UFS_DIRHASH # Improve performance on big directories
options MD_ROOT # MD is a potential root device
options NFSCLIENT # Network Filesystem Client
options NFSSERVER # Network Filesystem Server
options NFSLOCKD # Network Lock Manager
options NFS_ROOT # NFS usable as /, requires NFSCLIENT
options MSDOSFS # MSDOS Filesystem
options CD9660 # ISO 9660 Filesystem
@ -58,6 +59,13 @@ options _KPOSIX_PRIORITY_SCHEDULING # POSIX P1003_1B real-time extensions
options KBD_INSTALL_CDEV # install a CDEV entry in /dev
options ADAPTIVE_GIANT # Giant mutex is adaptive.
options KDB
options KDB_TRACE
options KDB_UNATTENDED
options DDB
options DDB_NUMSYM
options GDB
device apic # I/O APIC
# Bus support.

View File

@ -30,6 +30,7 @@ options SOFTUPDATES # Enable FFS soft updates support
#options MD_ROOT # MD is a potential root device
options NFSCLIENT # Network Filesystem Client
#options NFSSERVER # Network Filesystem Server
#options NFSLOCKD # Network Lock Manager
#options NFS_ROOT # NFS usable as /, requires NFSCLIENT
#options MSDOSFS # MSDOS Filesystem
options CD9660 # ISO 9660 Filesystem

View File

@ -93,7 +93,7 @@ cvt_flock2iflock(flp, iflp)
iflp->l_whence = (short)flp->l_whence;
iflp->l_start = (ibcs2_off_t)flp->l_start;
iflp->l_len = (ibcs2_off_t)flp->l_len;
iflp->l_sysid = 0;
iflp->l_sysid = flp->l_sysid;
iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
}
@ -127,6 +127,7 @@ cvt_iflock2flock(iflp, flp)
break;
}
flp->l_whence = iflp->l_whence;
flp->l_sysid = iflp->l_sysid;
}
/* convert iBCS2 mode into NetBSD mode */

View File

@ -40,6 +40,7 @@ options MD_ROOT # MD usable as root device
options MSDOSFS # MSDOS Filesystem
options NFSCLIENT # Network Filesystem Client
options NFSSERVER # Network Filesystem Server
options NFSLOCKD # Network Lock Manager
options NFS_ROOT # NFS usable as root device
options PROCFS # Process filesystem (/proc)
options PSEUDOFS # Pseudo-filesystem framework

View File

@ -321,28 +321,67 @@ int
fcntl(struct thread *td, struct fcntl_args *uap)
{
struct flock fl;
struct oflock ofl;
intptr_t arg;
int error;
int cmd;
error = 0;
cmd = uap->cmd;
switch (uap->cmd) {
case F_GETLK:
case F_SETLK:
case F_SETLKW:
error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
case F_OGETLK:
case F_OSETLK:
case F_OSETLKW:
/*
* Convert old flock structure to new.
*/
error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
fl.l_start = ofl.l_start;
fl.l_len = ofl.l_len;
fl.l_pid = ofl.l_pid;
fl.l_type = ofl.l_type;
fl.l_whence = ofl.l_whence;
fl.l_sysid = 0;
switch (uap->cmd) {
case F_OGETLK:
cmd = F_GETLK;
break;
case F_OSETLK:
cmd = F_SETLK;
break;
case F_OSETLKW:
cmd = F_SETLKW;
break;
}
arg = (intptr_t)&fl;
break;
case F_GETLK:
case F_SETLK:
case F_SETLKW:
case F_SETLK_REMOTE:
error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
arg = (intptr_t)&fl;
break;
default:
arg = uap->arg;
break;
}
if (error)
return (error);
error = kern_fcntl(td, uap->fd, uap->cmd, arg);
error = kern_fcntl(td, uap->fd, cmd, arg);
if (error)
return (error);
if (uap->cmd == F_GETLK)
if (uap->cmd == F_OGETLK) {
ofl.l_start = fl.l_start;
ofl.l_len = fl.l_len;
ofl.l_pid = fl.l_pid;
ofl.l_type = fl.l_type;
ofl.l_whence = fl.l_whence;
error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
} else if (uap->cmd == F_GETLK) {
error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
}
return (error);
}
@ -473,12 +512,21 @@ kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
fdrop(fp, td);
break;
case F_SETLK_REMOTE:
mtx_assert(&Giant, MA_OWNED);
error = suser(td);
if (error)
return (error);
flg = F_REMOTE;
goto do_setlk;
case F_SETLKW:
mtx_assert(&Giant, MA_OWNED);
flg |= F_WAIT;
/* FALLTHROUGH F_SETLK */
case F_SETLK:
do_setlk:
mtx_assert(&Giant, MA_OWNED);
if (fp->f_type != DTYPE_VNODE) {
FILEDESC_UNLOCK(fdp);
@ -530,7 +578,19 @@ kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
break;
case F_UNLCK:
error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
flp, F_POSIX);
flp, flg);
break;
case F_UNLCKSYS:
/*
* Temporary api for testing remote lock
* infrastructure.
*/
if (flg != F_REMOTE) {
error = EINVAL;
break;
}
error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
F_UNLCKSYS, flp, flg);
break;
default:
error = EINVAL;

File diff suppressed because it is too large Load Diff

View File

@ -301,7 +301,8 @@
151 AUE_NULL UNIMPL sem_lock (BSD/OS 2.x)
152 AUE_NULL UNIMPL sem_wakeup (BSD/OS 2.x)
153 AUE_NULL UNIMPL asyncdaemon (BSD/OS 2.x)
154 AUE_NULL UNIMPL nosys
; 154 is initialised by the NLM code, if present.
154 AUE_NULL MNOSTD { int nlm_syscall(int debug_level, int grace_period, int addr_count, char **addrs); }
; 155 is initialized by the NFS code, if present.
155 AUE_NFS_SVC MNOIMPL { int nfssvc(int flag, caddr_t argp); }
156 AUE_GETDIRENTRIES COMPAT { int getdirentries(int fd, char *buf, \

View File

@ -446,6 +446,19 @@ vop_advlock {
IN int flags;
};
#
#% advlockasync vp U U U
#
vop_advlockasync {
IN struct vnode *vp;
IN void *id;
IN int op;
IN struct flock *fl;
IN int flags;
IN struct task *task;
INOUT void **cookiep;
};
#
#% reallocblks vp E E E
#

View File

@ -136,6 +136,7 @@ SUBDIR= ${_3dfx} \
${_ixgb} \
joy \
kbdmux \
krpc \
kue \
le \
lge \
@ -178,6 +179,7 @@ SUBDIR= ${_3dfx} \
${_ndis} \
netgraph \
nfsclient \
nfslockd \
nfsserver \
nge \
nmdm \

View File

@ -158,6 +158,7 @@ static int nfs4_sillyrename(struct vnode *, struct vnode *,
static vop_readlink_t nfs4_readlink;
static vop_print_t nfs4_print;
static vop_advlock_t nfs4_advlock;
static vop_advlockasync_t nfs4_advlockasync;
/*
* Global vfs data structures for nfs
@ -166,6 +167,7 @@ struct vop_vector nfs4_vnodeops = {
.vop_default = &default_vnodeops,
.vop_access = nfs4_access,
.vop_advlock = nfs4_advlock,
.vop_advlockasync = nfs4_advlockasync,
.vop_close = nfs4_close,
.vop_create = nfs4_create,
.vop_fsync = nfs4_fsync,
@ -2776,6 +2778,22 @@ nfs4_advlock(struct vop_advlock_args *ap)
return (nfs_dolock(ap));
}
/*
* NFS advisory byte-level locks.
*/
static int
nfs4_advlockasync(struct vop_advlockasync_args *ap)
{
return (EPERM);
if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
struct nfsnode *np = VTONFS(ap->a_vp);
return (lf_advlockasync(ap, &(np->n_lockf), np->n_size));
}
return (EOPNOTSUPP);
}
/*
* Print out the contents of an nfsnode.
*/

View File

@ -319,6 +319,7 @@ nfs_dolock(struct vop_advlock_args *ap)
if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
if (p->p_nlminfo->set_getlk_pid) {
fl->l_sysid = 0; /* XXX */
fl->l_pid = p->p_nlminfo->getlk_pid;
} else {
fl->l_type = F_UNLCK;

View File

@ -129,6 +129,7 @@ static vop_access_t nfsspec_access;
static vop_readlink_t nfs_readlink;
static vop_print_t nfs_print;
static vop_advlock_t nfs_advlock;
static vop_advlockasync_t nfs_advlockasync;
/*
* Global vfs data structures for nfs
@ -137,6 +138,7 @@ struct vop_vector nfs_vnodeops = {
.vop_default = &default_vnodeops,
.vop_access = nfs_access,
.vop_advlock = nfs_advlock,
.vop_advlockasync = nfs_advlockasync,
.vop_close = nfs_close,
.vop_create = nfs_create,
.vop_fsync = nfs_fsync,
@ -2907,6 +2909,27 @@ nfs_advlock(struct vop_advlock_args *ap)
return (nfs_dolock(ap));
}
/*
* NFS advisory byte-level locks.
*/
static int
nfs_advlockasync(struct vop_advlockasync_args *ap)
{
int error;
mtx_lock(&Giant);
if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
struct nfsnode *np = VTONFS(ap->a_vp);
error = lf_advlockasync(ap, &(np->n_lockf), np->n_size);
goto out;
}
error = EOPNOTSUPP;
out:
mtx_unlock(&Giant);
return (error);
}
/*
* Print out the contents of an nfsnode.
*/

View File

@ -667,14 +667,17 @@ nfsrv_getstream(struct nfssvc_sock *slp, int waitflag)
NFSD_UNLOCK();
rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC,
waitflag == M_DONTWAIT ? M_NOWAIT : M_WAITOK);
NFSD_LOCK();
if (!rec) {
m_freem(slp->ns_frag);
} else {
if (rec) {
nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
rec->nr_address = NULL;
rec->nr_packet = slp->ns_frag;
NFSD_LOCK();
STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
} else {
NFSD_LOCK();
}
if (!rec) {
m_freem(slp->ns_frag);
}
slp->ns_frag = NULL;
}

View File

@ -40,6 +40,7 @@ options UFS_DIRHASH # Improve performance on big directories
options MD_ROOT # MD is a potential root device
options NFSCLIENT # Network Filesystem Client
options NFSSERVER # Network Filesystem Server
options NFSLOCKD # Network Lock Manager
options NFS_ROOT # NFS usable as /, requires NFSCLIENT
options MSDOSFS # MSDOS Filesystem
options CD9660 # ISO 9660 Filesystem

View File

@ -41,6 +41,7 @@ options UFS_DIRHASH #Improve performance on big directories
options MD_ROOT #MD is a potential root device
options NFSCLIENT #Network Filesystem Client
options NFSSERVER #Network Filesystem Server
options NFSLOCKD #Network Lock Manager
options NFS_ROOT #NFS usable as root device
options MSDOSFS #MSDOS Filesystem
options CD9660 #ISO 9660 Filesystem

View File

@ -43,6 +43,7 @@ options UFS_DIRHASH # Improve performance on big directories
options MD_ROOT # MD is a potential root device
options NFSCLIENT # Network Filesystem Client
options NFSSERVER # Network Filesystem Server
options NFSLOCKD # Network Lock Manager
options NFS_ROOT # NFS usable as /, requires NFSCLIENT
#options MSDOSFS # MSDOS Filesystem
options CD9660 # ISO 9660 Filesystem

View File

@ -173,9 +173,14 @@ typedef __pid_t pid_t;
#define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */
#define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */
#endif
#define F_GETLK 7 /* get record locking information */
#define F_SETLK 8 /* set record locking information */
#define F_SETLKW 9 /* F_SETLK; wait if blocked */
#define F_OGETLK 7 /* get record locking information */
#define F_OSETLK 8 /* set record locking information */
#define F_OSETLKW 9 /* F_SETLK; wait if blocked */
/* 10 reserved for F_DUP2FD */
#define F_GETLK 11 /* get record locking information */
#define F_SETLK 12 /* set record locking information */
#define F_SETLKW 13 /* F_SETLK; wait if blocked */
#define F_SETLK_REMOTE 14 /* debugging support for remote locks */
/* file descriptor flags (F_GETFD, F_SETFD) */
#define FD_CLOEXEC 1 /* close-on-exec flag */
@ -184,10 +189,13 @@ typedef __pid_t pid_t;
#define F_RDLCK 1 /* shared or read lock */
#define F_UNLCK 2 /* unlock */
#define F_WRLCK 3 /* exclusive or write lock */
#define F_UNLCKSYS 4 /* purge locks for a given system ID */
#define F_CANCEL 5 /* cancel an async lock request */
#ifdef _KERNEL
#define F_WAIT 0x010 /* Wait until lock is granted */
#define F_FLOCK 0x020 /* Use flock(2) semantics for lock */
#define F_POSIX 0x040 /* Use POSIX semantics for lock */
#define F_REMOTE 0x080 /* Lock owner is remote NFS client */
#endif
/*
@ -200,6 +208,19 @@ struct flock {
pid_t l_pid; /* lock owner */
short l_type; /* lock type: read/write, etc. */
short l_whence; /* type of l_start */
int l_sysid; /* remote system id or zero for local */
};
/*
* Old advisory file segment locking data type,
* before adding l_sysid.
*/
struct oflock {
off_t l_start; /* starting offset */
off_t l_len; /* len = 0 means until end of file */
pid_t l_pid; /* lock owner */
short l_type; /* lock type: read/write, etc. */
short l_whence; /* type of l_start */
};

View File

@ -94,7 +94,7 @@ hash32_stre(const void *buf, int end, char **ep, uint32_t hash)
hash = HASHSTEP(hash, *p++);
if (ep)
*ep = (char *)p;
*ep = (char *)(uintptr_t)p;
return hash;
}
@ -113,7 +113,7 @@ hash32_strne(const void *buf, size_t len, int end, char **ep, uint32_t hash)
hash = HASHSTEP(hash, *p++);
if (ep)
*ep = (char *)p;
*ep = (char *)(uintptr_t)p;
return hash;
}

View File

@ -37,33 +37,85 @@
#define _SYS_LOCKF_H_
#include <sys/queue.h>
#include <sys/_lock.h>
#include <sys/_sx.h>
struct vop_advlock_args;
struct vop_advlockasync_args;
/*
* The lockf structure is a kernel structure which contains the information
* associated with a byte range lock. The lockf structures are linked into
* the inode structure. Locks are sorted by the starting byte of the lock for
* efficiency.
* The lockf_entry structure is a kernel structure which contains the
* information associated with a byte range lock. The lockf_entry
* structures are linked into the inode structure. Locks are sorted by
* the starting byte of the lock for efficiency.
*
* Active and pending locks on a vnode are organised into a
* graph. Each pending lock has an out-going edge to each active lock
* that blocks it.
*
* Locks:
* (i) locked by the vnode interlock
* (s) locked by state->ls_lock
* (S) locked by lf_lock_states_lock
* (c) const until freeing
*/
TAILQ_HEAD(locklist, lockf);
struct lockf {
short lf_flags; /* Semantics: F_POSIX, F_FLOCK, F_WAIT */
short lf_type; /* Lock type: F_RDLCK, F_WRLCK */
off_t lf_start; /* Byte # of the start of the lock */
off_t lf_end; /* Byte # of the end of the lock (-1=EOF) */
caddr_t lf_id; /* Id of the resource holding the lock */
struct lockf **lf_head; /* Back pointer to the head of the locf list */
struct inode *lf_inode; /* Back pointer to the inode */
struct lockf *lf_next; /* Pointer to the next lock on this inode */
struct locklist lf_blkhd; /* List of requests blocked on this lock */
TAILQ_ENTRY(lockf) lf_block;/* A request waiting for a lock */
struct lockf_edge {
LIST_ENTRY(lockf_edge) le_outlink; /* (s) link from's out-edge list */
LIST_ENTRY(lockf_edge) le_inlink; /* (s) link to's in-edge list */
struct lockf_entry *le_from; /* (c) out-going from here */
struct lockf_entry *le_to; /* (s) in-coming to here */
};
LIST_HEAD(lockf_edge_list, lockf_edge);
/* Maximum length of sleep chains to traverse to try and detect deadlock. */
#define MAXDEPTH 50
struct lockf_entry {
short lf_flags; /* (c) Semantics: F_POSIX, F_FLOCK, F_WAIT */
short lf_type; /* (s) Lock type: F_RDLCK, F_WRLCK */
off_t lf_start; /* (s) Byte # of the start of the lock */
off_t lf_end; /* (s) Byte # of the end of the lock (OFF_MAX=EOF) */
struct lock_owner *lf_owner; /* (c) Owner of the lock */
struct vnode *lf_vnode; /* (c) File being locked (only valid for active lock) */
struct inode *lf_inode; /* (c) Back pointer to the inode */
struct task *lf_async_task;/* (c) Async lock callback */
LIST_ENTRY(lockf_entry) lf_link; /* (s) Linkage for lock lists */
struct lockf_edge_list lf_outedges; /* (s) list of out-edges */
struct lockf_edge_list lf_inedges; /* (s) list of out-edges */
};
LIST_HEAD(lockf_entry_list, lockf_entry);
/*
* Filesystem private node structures should include space for a
* pointer to a struct lockf_state. This pointer is used by the lock
* manager to track the locking state for a file.
*
* The ls_active list contains the set of active locks on the file. It
* is strictly ordered by the lock's lf_start value. Each active lock
* will have in-coming edges to any pending lock which it blocks.
*
* Lock requests which are blocked by some other active lock are
* listed in ls_pending with newer requests first in the list. Lock
* requests in this list will have out-going edges to each active lock
* that blocks then. They will also have out-going edges to each
* pending lock that is older in the queue - this helps to ensure
* fairness when several processes are contenting to lock the same
* record.
* The value of ls_threads is the number of threads currently using
* the state structure (typically either setting/clearing locks or
* sleeping waiting to do so). This is used to defer freeing the
* structure while some thread is still using it.
*/
struct lockf {
LIST_ENTRY(lockf) ls_link; /* (S) all active lockf states */
struct sx ls_lock;
struct lockf_entry_list ls_active; /* (s) Active locks */
struct lockf_entry_list ls_pending; /* (s) Pending locks */
int ls_threads; /* (i) Thread count */
};
LIST_HEAD(lockf_list, lockf);
int lf_advlock(struct vop_advlock_args *, struct lockf **, u_quad_t);
int lf_advlockasync(struct vop_advlockasync_args *, struct lockf **, u_quad_t);
int lf_countlocks(int sysid);
void lf_clearremotesys(int sysid);
#endif /* !_SYS_LOCKF_H_ */

View File

@ -554,6 +554,15 @@ m_chtype(struct mbuf *m, short new_type)
m->m_type = new_type;
}
static __inline struct mbuf *
m_last(struct mbuf *m)
{
while (m->m_next)
m = m->m_next;
return (m);
}
/*
* mbuf, cluster, and external object allocation macros
* (for compatibility purposes).

View File

@ -57,7 +57,7 @@
* is created, otherwise 1.
*/
#undef __FreeBSD_version
#define __FreeBSD_version 603100 /* Master, propagated to newvers */
#define __FreeBSD_version 603102 /* Master, propagated to newvers */
#ifndef LOCORE
#include <sys/types.h>

View File

@ -87,6 +87,7 @@ __FBSDID("$FreeBSD$");
static vop_access_t ufs_access;
static vop_advlock_t ufs_advlock;
static vop_advlockasync_t ufs_advlockasync;
static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
static vop_close_t ufs_close;
@ -2157,6 +2158,25 @@ ufs_advlock(ap)
return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
}
/*
* Advisory record locking support
*/
static int
ufs_advlockasync(ap)
struct vop_advlockasync_args /* {
struct vnode *a_vp;
caddr_t a_id;
int a_op;
struct flock *a_fl;
int a_flags;
struct task *a_task;
} */ *ap;
{
struct inode *ip = VTOI(ap->a_vp);
return (lf_advlockasync(ap, &(ip->i_lockf), ip->i_size));
}
/*
* Initialize the vnode associated with a new inode, handle aliased
* vnodes.
@ -2425,6 +2445,7 @@ struct vop_vector ufs_vnodeops = {
.vop_write = VOP_PANIC,
.vop_access = ufs_access,
.vop_advlock = ufs_advlock,
.vop_advlockasync = ufs_advlockasync,
.vop_bmap = ufs_bmap,
.vop_cachedlookup = ufs_lookup,
.vop_close = ufs_close,

View File

@ -29,6 +29,7 @@
#include <sys/time.h>
#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include <sys/wait.h>
@ -46,6 +47,12 @@
#if __FreeBSD_version >= 800028
#define HAVE_SYSID
#endif
#if __FreeBSD_version < 800000 && __FreeBSD_version >= 700103
#define HAVE_SYSID
#endif
#if __FreeBSD_version < 700000 && __FreeBSD_version >= 603102
#define HAVE_SYSID
#endif
#include <sys/cdefs.h>
#else
#ifndef __unused

View File

@ -30,6 +30,7 @@ SUBDIR= ac \
chown \
chroot \
ckdist \
clear_locks \
config \
cron \
crunch \

View File

@ -46,8 +46,14 @@ __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
* The actual program logic is in the file lock_proc.c
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/linker.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <stdio.h>
@ -59,6 +65,7 @@ __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
#include <unistd.h>
#include <libutil.h>
#include <netconfig.h>
#include <netdb.h>
#include <rpc/rpc.h>
#include <rpc/rpc_com.h>
@ -72,37 +79,46 @@ int _rpcsvcdirty = 0;
int grace_expired;
int nsm_state;
int kernel_lockd;
pid_t client_pid;
struct mon mon_host;
char **hosts, *svcport_str = NULL;
int nhosts = 0;
int xcreated = 0;
char **addrs; /* actually (netid, uaddr) pairs */
int naddrs; /* count of how many (netid, uaddr) pairs */
void create_service(struct netconfig *nconf);
void lookup_addresses(struct netconfig *nconf);
void init_nsm(void);
void nlm_prog_0(struct svc_req *, SVCXPRT *);
void nlm_prog_1(struct svc_req *, SVCXPRT *);
void nlm_prog_3(struct svc_req *, SVCXPRT *);
void nlm_prog_4(struct svc_req *, SVCXPRT *);
void out_of_mem(void);
void usage(void);
void sigalarm_handler(void);
const char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
/*
* XXX move to some header file.
*/
#define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
int
main(argc, argv)
int argc;
char **argv;
main(int argc, char **argv)
{
SVCXPRT *transp;
int ch, i, maxindex, r, s, sock;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
char *endptr;
int ch, i, s;
void *nc_handle;
char *endptr, **hosts_bak;
struct sigaction sigalarm;
int grace_period = 30;
struct netconfig *nconf;
int have_v6 = 1;
int maxrec = RPC_MAXDATASIZE;
in_port_t svcport = 0;
while ((ch = getopt(argc, argv, "d:g:p:")) != (-1)) {
while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
switch (ch) {
case 'd':
debug_level = atoi(optarg);
@ -118,12 +134,34 @@ main(argc, argv)
/* NOTREACHED */
}
break;
case 'h':
++nhosts;
hosts_bak = hosts;
hosts_bak = realloc(hosts, nhosts * sizeof(char *));
if (hosts_bak == NULL) {
if (hosts != NULL) {
for (i = 0; i < nhosts; i++)
free(hosts[i]);
free(hosts);
out_of_mem();
}
}
hosts = hosts_bak;
hosts[nhosts - 1] = strdup(optarg);
if (hosts[nhosts - 1] == NULL) {
for (i = 0; i < (nhosts - 1); i++)
free(hosts[i]);
free(hosts);
out_of_mem();
}
break;
case 'p':
endptr = NULL;
svcport = (in_port_t)strtoul(optarg, &endptr, 10);
if (endptr == NULL || *endptr != '\0' ||
svcport == 0 || svcport >= IPPORT_MAX)
usage();
svcport_str = strdup(optarg);
break;
default:
case '?':
@ -136,6 +174,17 @@ main(argc, argv)
exit(1);
}
kernel_lockd = FALSE;
if (modfind("nfslockd") < 0) {
if (kldload("nfslockd") < 0) {
fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
} else {
kernel_lockd = TRUE;
}
} else {
kernel_lockd = TRUE;
}
(void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
(void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
(void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
@ -146,112 +195,125 @@ main(argc, argv)
*/
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0)
maxindex = 2;
else {
have_v6 = 0;
else
close(s);
maxindex = 4;
}
if (svcport != 0) {
bzero(&sin, sizeof(struct sockaddr_in));
sin.sin_len = sizeof(struct sockaddr_in);
sin.sin_family = AF_INET;
sin.sin_port = htons(svcport);
bzero(&sin6, sizeof(struct sockaddr_in6));
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(svcport);
}
rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
for (i = 0; i < maxindex; i++) {
nconf = getnetconfigent(transports[i]);
if (nconf == NULL)
errx(1, "cannot get %s netconf: %s.", transports[i],
nc_sperror());
/*
* If no hosts were specified, add a wildcard entry to bind to
* INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
* list.
*/
if (nhosts == 0) {
hosts = malloc(sizeof(char**));
if (hosts == NULL)
out_of_mem();
if (svcport != 0) {
if (strcmp(nconf->nc_netid, "udp6") == 0) {
sock = socket(AF_INET6, SOCK_DGRAM,
IPPROTO_UDP);
if (sock != -1) {
r = bindresvport_sa(sock,
(struct sockaddr *)&sin6);
if (r != 0) {
syslog(LOG_ERR, "bindresvport: %m");
exit(1);
}
}
}
else if (strcmp(nconf->nc_netid, "udp") == 0) {
sock = socket(AF_INET, SOCK_DGRAM,
IPPROTO_UDP);
if (sock != -1) {
r = bindresvport(sock, &sin);
if (r != 0) {
syslog(LOG_ERR, "bindresvport: %m");
exit(1);
}
}
}
else if (strcmp(nconf->nc_netid, "tcp6") == 0) {
sock = socket(AF_INET6, SOCK_STREAM,
IPPROTO_TCP);
if (sock != -1) {
r = bindresvport_sa(sock,
(struct sockaddr *)&sin6);
if (r != 0) {
syslog(LOG_ERR, "bindresvport: %m");
exit(1);
}
}
}
else if (strcmp(nconf->nc_netid, "tcp") == 0) {
sock = socket(AF_INET, SOCK_STREAM,
IPPROTO_TCP);
if (sock != -1) {
r = bindresvport(sock, &sin);
if (r != 0) {
syslog(LOG_ERR, "bindresvport: %m");
exit(1);
}
}
}
hosts[0] = "*";
nhosts = 1;
} else {
hosts_bak = hosts;
if (have_v6) {
hosts_bak = realloc(hosts, (nhosts + 2) *
sizeof(char *));
if (hosts_bak == NULL) {
for (i = 0; i < nhosts; i++)
free(hosts[i]);
free(hosts);
out_of_mem();
} else
hosts = hosts_bak;
transp = svc_tli_create(sock, nconf, NULL,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
nhosts += 2;
hosts[nhosts - 2] = "::1";
} else {
transp = svc_tli_create(RPC_ANYFD, nconf, NULL,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
if (hosts_bak == NULL) {
for (i = 0; i < nhosts; i++)
free(hosts[i]);
free(hosts);
out_of_mem();
} else {
nhosts += 1;
hosts = hosts_bak;
}
}
hosts[nhosts - 1] = "127.0.0.1";
}
if (kernel_lockd) {
/*
* For the kernel lockd case, we run a cut-down RPC
* service on a local-domain socket. The kernel's RPC
* server will pass what it can't handle (mainly
* client replies) down to us. This can go away
* entirely if/when we move the client side of NFS
* locking into the kernel.
*/
struct sockaddr_un sun;
int fd, oldmask;
SVCXPRT *xprt;
memset(&sun, 0, sizeof sun);
sun.sun_family = AF_LOCAL;
unlink(_PATH_RPCLOCKDSOCK);
strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
sun.sun_len = SUN_LEN(&sun);
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (!fd) {
err(1, "Can't create local lockd socket");
}
oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
err(1, "Can't bind local lockd socket");
}
umask(oldmask);
if (listen(fd, SOMAXCONN) < 0) {
err(1, "Can't listen on local lockd socket");
}
xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
if (!xprt) {
err(1, "Can't create transport for local lockd socket");
}
if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
err(1, "Can't register service for local lockd socket");
}
if (transp == NULL) {
errx(1, "cannot create %s service.", transports[i]);
/* NOTREACHED */
/*
* We need to look up the addresses so that we can
* hand uaddrs (ascii encoded address+port strings) to
* the kernel.
*/
nc_handle = setnetconfig();
while ((nconf = getnetconfig(nc_handle))) {
/* We want to listen only on udp6, tcp6, udp, tcp transports */
if (nconf->nc_flag & NC_VISIBLE) {
/* Skip if there's no IPv6 support */
if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
/* DO NOTHING */
} else {
lookup_addresses(nconf);
}
}
}
if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
transports[i]);
/* NOTREACHED */
endnetconfig(nc_handle);
} else {
nc_handle = setnetconfig();
while ((nconf = getnetconfig(nc_handle))) {
/* We want to listen only on udp6, tcp6, udp, tcp transports */
if (nconf->nc_flag & NC_VISIBLE) {
/* Skip if there's no IPv6 support */
if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
/* DO NOTHING */
} else {
create_service(nconf);
}
}
}
if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
transports[i]);
/* NOTREACHED */
}
if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
transports[i]);
/* NOTREACHED */
}
if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) {
errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
transports[i]);
/* NOTREACHED */
}
freenetconfigent(nconf);
endnetconfig(nc_handle);
}
/*
@ -278,17 +340,412 @@ main(argc, argv)
strerror(errno));
exit(1);
}
grace_expired = 0;
alarm(grace_period);
init_nsm();
if (kernel_lockd) {
client_pid = client_request();
client_pid = client_request();
/*
* Create a child process to enter the kernel and then
* wait for RPCs on our local domain socket.
*/
if (!fork())
nlm_syscall(debug_level, grace_period, naddrs, addrs);
else
svc_run();
} else {
grace_expired = 0;
alarm(grace_period);
svc_run(); /* Should never return */
init_nsm();
client_pid = client_request();
svc_run(); /* Should never return */
}
exit(1);
}
/*
* This routine creates and binds sockets on the appropriate
* addresses. It gets called one time for each transport and
* registrates the service with rpcbind on that trasport.
*/
void
create_service(struct netconfig *nconf)
{
struct addrinfo hints, *res = NULL;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct __rpc_sockinfo si;
struct netbuf servaddr;
SVCXPRT *transp = NULL;
int aicode;
int fd;
int nhostsbak;
int r;
int registered = 0;
u_int32_t host_addr[4]; /* IPv4 or IPv6 */
if ((nconf->nc_semantics != NC_TPI_CLTS) &&
(nconf->nc_semantics != NC_TPI_COTS) &&
(nconf->nc_semantics != NC_TPI_COTS_ORD))
return; /* not my type */
/*
* XXX - using RPC library internal functions.
*/
if (!__rpc_nconf2sockinfo(nconf, &si)) {
syslog(LOG_ERR, "cannot get information for %s",
nconf->nc_netid);
return;
}
/* Get rpc.statd's address on this transport */
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = si.si_af;
hints.ai_socktype = si.si_socktype;
hints.ai_protocol = si.si_proto;
/*
* Bind to specific IPs if asked to
*/
nhostsbak = nhosts;
while (nhostsbak > 0) {
--nhostsbak;
/*
* XXX - using RPC library internal functions.
*/
if ((fd = __rpc_nconf2fd(nconf)) < 0) {
syslog(LOG_ERR, "cannot create socket for %s",
nconf->nc_netid);
continue;
}
switch (hints.ai_family) {
case AF_INET:
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
/*
* Skip if we have an AF_INET6 address.
*/
if (inet_pton(AF_INET6, hosts[nhostsbak],
host_addr) == 1) {
close(fd);
continue;
}
}
break;
case AF_INET6:
if (inet_pton(AF_INET6, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
/*
* Skip if we have an AF_INET address.
*/
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
close(fd);
continue;
}
}
break;
default:
break;
}
/*
* If no hosts were specified, just bind to INADDR_ANY
*/
if (strcmp("*", hosts[nhostsbak]) == 0) {
if (svcport_str == NULL) {
res = malloc(sizeof(struct addrinfo));
if (res == NULL)
out_of_mem();
res->ai_flags = hints.ai_flags;
res->ai_family = hints.ai_family;
res->ai_protocol = hints.ai_protocol;
switch (res->ai_family) {
case AF_INET:
sin = malloc(sizeof(struct sockaddr_in));
if (sin == NULL)
out_of_mem();
sin->sin_family = AF_INET;
sin->sin_port = htons(0);
sin->sin_addr.s_addr = htonl(INADDR_ANY);
res->ai_addr = (struct sockaddr*) sin;
res->ai_addrlen = (socklen_t)
sizeof(res->ai_addr);
break;
case AF_INET6:
sin6 = malloc(sizeof(struct sockaddr_in6));
if (sin6 == NULL)
out_of_mem();
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(0);
sin6->sin6_addr = in6addr_any;
res->ai_addr = (struct sockaddr*) sin6;
res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
break;
default:
break;
}
} else {
if ((aicode = getaddrinfo(NULL, svcport_str,
&hints, &res)) != 0) {
syslog(LOG_ERR,
"cannot get local address for %s: %s",
nconf->nc_netid,
gai_strerror(aicode));
continue;
}
}
} else {
if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
&hints, &res)) != 0) {
syslog(LOG_ERR,
"cannot get local address for %s: %s",
nconf->nc_netid, gai_strerror(aicode));
continue;
}
}
r = bindresvport_sa(fd, res->ai_addr);
if (r != 0) {
syslog(LOG_ERR, "bindresvport_sa: %m");
exit(1);
}
if (nconf->nc_semantics != NC_TPI_CLTS)
listen(fd, SOMAXCONN);
transp = svc_tli_create(fd, nconf, NULL,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
if (transp != (SVCXPRT *) NULL) {
if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
NULL))
syslog(LOG_ERR,
"can't register %s NLM_PROG, NLM_SM service",
nconf->nc_netid);
if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
NULL))
syslog(LOG_ERR,
"can't register %s NLM_PROG, NLM_VERS service",
nconf->nc_netid);
if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
NULL))
syslog(LOG_ERR,
"can't register %s NLM_PROG, NLM_VERSX service",
nconf->nc_netid);
if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
NULL))
syslog(LOG_ERR,
"can't register %s NLM_PROG, NLM_VERS4 service",
nconf->nc_netid);
} else
syslog(LOG_WARNING, "can't create %s services",
nconf->nc_netid);
if (registered == 0) {
registered = 1;
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = si.si_af;
hints.ai_socktype = si.si_socktype;
hints.ai_protocol = si.si_proto;
if (svcport_str == NULL) {
svcport_str = malloc(NI_MAXSERV * sizeof(char));
if (svcport_str == NULL)
out_of_mem();
if (getnameinfo(res->ai_addr,
res->ai_addr->sa_len, NULL, NI_MAXHOST,
svcport_str, NI_MAXSERV * sizeof(char),
NI_NUMERICHOST | NI_NUMERICSERV))
errx(1, "Cannot get port number");
}
if((aicode = getaddrinfo(NULL, svcport_str, &hints,
&res)) != 0) {
syslog(LOG_ERR, "cannot get local address: %s",
gai_strerror(aicode));
exit(1);
}
servaddr.buf = malloc(res->ai_addrlen);
memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
servaddr.len = res->ai_addrlen;
rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
xcreated++;
freeaddrinfo(res);
}
} /* end while */
}
/*
* Look up addresses for the kernel to create transports for.
*/
void
lookup_addresses(struct netconfig *nconf)
{
struct addrinfo hints, *res = NULL;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
struct __rpc_sockinfo si;
struct netbuf servaddr;
SVCXPRT *transp = NULL;
int aicode;
int nhostsbak;
int r;
int registered = 0;
u_int32_t host_addr[4]; /* IPv4 or IPv6 */
char *uaddr;
if ((nconf->nc_semantics != NC_TPI_CLTS) &&
(nconf->nc_semantics != NC_TPI_COTS) &&
(nconf->nc_semantics != NC_TPI_COTS_ORD))
return; /* not my type */
/*
* XXX - using RPC library internal functions.
*/
if (!__rpc_nconf2sockinfo(nconf, &si)) {
syslog(LOG_ERR, "cannot get information for %s",
nconf->nc_netid);
return;
}
/* Get rpc.statd's address on this transport */
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = si.si_af;
hints.ai_socktype = si.si_socktype;
hints.ai_protocol = si.si_proto;
/*
* Bind to specific IPs if asked to
*/
nhostsbak = nhosts;
while (nhostsbak > 0) {
--nhostsbak;
switch (hints.ai_family) {
case AF_INET:
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
/*
* Skip if we have an AF_INET6 address.
*/
if (inet_pton(AF_INET6, hosts[nhostsbak],
host_addr) == 1) {
continue;
}
}
break;
case AF_INET6:
if (inet_pton(AF_INET6, hosts[nhostsbak],
host_addr) == 1) {
hints.ai_flags &= AI_NUMERICHOST;
} else {
/*
* Skip if we have an AF_INET address.
*/
if (inet_pton(AF_INET, hosts[nhostsbak],
host_addr) == 1) {
continue;
}
}
break;
default:
break;
}
/*
* If no hosts were specified, just bind to INADDR_ANY
*/
if (strcmp("*", hosts[nhostsbak]) == 0) {
if (svcport_str == NULL) {
res = malloc(sizeof(struct addrinfo));
if (res == NULL)
out_of_mem();
res->ai_flags = hints.ai_flags;
res->ai_family = hints.ai_family;
res->ai_protocol = hints.ai_protocol;
switch (res->ai_family) {
case AF_INET:
sin = malloc(sizeof(struct sockaddr_in));
if (sin == NULL)
out_of_mem();
sin->sin_family = AF_INET;
sin->sin_port = htons(0);
sin->sin_addr.s_addr = htonl(INADDR_ANY);
res->ai_addr = (struct sockaddr*) sin;
res->ai_addrlen = (socklen_t)
sizeof(res->ai_addr);
break;
case AF_INET6:
sin6 = malloc(sizeof(struct sockaddr_in6));
if (sin6 == NULL)
out_of_mem();
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(0);
sin6->sin6_addr = in6addr_any;
res->ai_addr = (struct sockaddr*) sin6;
res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
break;
default:
break;
}
} else {
if ((aicode = getaddrinfo(NULL, svcport_str,
&hints, &res)) != 0) {
syslog(LOG_ERR,
"cannot get local address for %s: %s",
nconf->nc_netid,
gai_strerror(aicode));
continue;
}
}
} else {
if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
&hints, &res)) != 0) {
syslog(LOG_ERR,
"cannot get local address for %s: %s",
nconf->nc_netid, gai_strerror(aicode));
continue;
}
}
servaddr.len = servaddr.maxlen = res->ai_addr->sa_len;
servaddr.buf = res->ai_addr;
uaddr = taddr2uaddr(nconf, &servaddr);
addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
if (!addrs)
out_of_mem();
addrs[2 * naddrs] = strdup(nconf->nc_netid);
addrs[2 * naddrs + 1] = uaddr;
naddrs++;
} /* end while */
}
void
sigalarm_handler(void)
{
@ -300,7 +757,7 @@ void
usage()
{
errx(1, "usage: rpc.lockd [-d <debuglevel>]"
" [-g <grace period>] [-p <port>]");
" [-g <grace period>] [-h <bindip>] [-p <port>]");
}
/*
@ -353,3 +810,12 @@ init_nsm(void)
mon_host.mon_id.my_id.my_vers = NLM_SM;
mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
}
/*
* Out of memory, fatal
*/
void out_of_mem()
{
syslog(LOG_ERR, "out of memory");
exit(2);
}

View File

@ -167,6 +167,9 @@ main(int argc, char **argv)
}
}
if (nconf->nc_semantics != NC_TPI_CLTS)
listen(sock, SOMAXCONN);
transp = svc_tli_create(sock, nconf, NULL,
RPC_MAXDATASIZE, RPC_MAXDATASIZE);
} else {