1 /*-
2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
14 * written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: releng/5.4/sys/nfsclient/nfs_lock.c 122698 2003-11-14 20:54:10Z alfred $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h> /* for hz */
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
42 #include <sys/mbuf.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/socket.h>
48 #include <sys/socket.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
51
52 #include <net/if.h>
53
54 #include <rpc/rpcclnt.h>
55
56 #include <nfs/rpcv2.h>
57 #include <nfs/nfsproto.h>
58 #include <nfsclient/nfs.h>
59 #include <nfsclient/nfsmount.h>
60 #include <nfsclient/nfsnode.h>
61 #include <nfsclient/nfs_lock.h>
62 #include <nfsclient/nlminfo.h>
63
64 /*
65 * XXX
66 * We have to let the process know if the call succeeded. I'm using an extra
67 * field in the p_nlminfo field in the proc structure, as it is already for
68 * lockd stuff.
69 */
70
71 /*
72 * nfs_advlock --
73 * NFS advisory byte-level locks.
74 */
75 int
76 nfs_dolock(struct vop_advlock_args *ap)
77 {
78 LOCKD_MSG msg;
79 struct nameidata nd;
80 struct thread *td;
81 struct vnode *vp, *wvp;
82 int error, error1;
83 struct flock *fl;
84 int fmode, ioflg;
85 struct proc *p;
86
87 td = curthread;
88 p = td->td_proc;
89
90 vp = ap->a_vp;
91 fl = ap->a_fl;
92
93 /*
94 * the NLM protocol doesn't allow the server to return an error
95 * on ranges, so we do it.
96 */
97 if (fl->l_whence != SEEK_END) {
98 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
99 fl->l_start < 0 ||
100 (fl->l_len < 0 &&
101 (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
102 return (EINVAL);
103 if (fl->l_len > 0 &&
104 (fl->l_len - 1 > OFF_MAX - fl->l_start))
105 return (EOVERFLOW);
106 }
107
108 /*
109 * Fill in the information structure.
110 */
111 msg.lm_version = LOCKD_MSG_VERSION;
112 msg.lm_msg_ident.pid = p->p_pid;
113 /*
114 * if there is no nfsowner table yet, allocate one.
115 */
116 if (p->p_nlminfo == NULL) {
117 MALLOC(p->p_nlminfo, struct nlminfo *,
118 sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO);
119 p->p_nlminfo->pid_start = p->p_stats->p_start;
120 timevaladd(&p->p_nlminfo->pid_start, &boottime);
121 }
122 msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
123 msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
124
125 msg.lm_fl = *fl;
126 msg.lm_wait = ap->a_flags & F_WAIT;
127 msg.lm_getlk = ap->a_op == F_GETLK;
128 bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
129 min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
130 msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
131 bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
132 msg.lm_nfsv3 = NFS_ISV3(vp);
133 cru2x(td->td_ucred, &msg.lm_cred);
134
135 /*
136 * Open the lock fifo. If for any reason we don't find the fifo, it
137 * means that the lock daemon isn't running. Translate any missing
138 * file error message for the user, otherwise the application will
139 * complain that the user's file is missing, which isn't the case.
140 * Note that we use proc0's cred, so the fifo is opened as root.
141 *
142 * XXX: Note that this behavior is relative to the root directory
143 * of the current process, and this may result in a variety of
144 * {functional, security} problems in chroot() environments.
145 */
146 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td);
147
148 fmode = FFLAGS(O_WRONLY | O_NONBLOCK);
149 error = vn_open_cred(&nd, &fmode, 0, thread0.td_ucred, -1);
150 switch (error) {
151 case ENOENT:
152 case ENXIO:
153 /*
154 * Map a failure to find the fifo or no listener on the
155 * fifo to locking not being supported.
156 */
157 return (EOPNOTSUPP);
158 case 0:
159 break;
160 default:
161 return (error);
162 }
163 wvp = nd.ni_vp;
164 VOP_UNLOCK(wvp, 0, td); /* vn_open leaves it locked */
165
166
167 ioflg = IO_UNIT | IO_NOMACCHECK;
168 for (;;) {
169 VOP_LEASE(wvp, td, thread0.td_ucred, LEASE_WRITE);
170
171 error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0,
172 UIO_SYSSPACE, ioflg, thread0.td_ucred, NOCRED, NULL, td);
173
174 if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) {
175 break;
176 }
177 /*
178 * If we're locking a file, wait for an answer. Unlocks succeed
179 * immediately.
180 */
181 if (fl->l_type == F_UNLCK)
182 /*
183 * XXX this isn't exactly correct. The client side
184 * needs to continue sending it's unlock until
185 * it gets a responce back.
186 */
187 break;
188
189 /*
190 * retry after 20 seconds if we haven't gotten a responce yet.
191 * This number was picked out of thin air... but is longer
192 * then even a reasonably loaded system should take (at least
193 * on a local network). XXX Probably should use a back-off
194 * scheme.
195 *
196 * XXX: No PCATCH here since we currently have no useful
197 * way to signal to the userland rpc.lockd that the request
198 * has been aborted. Once the rpc.lockd implementation
199 * can handle aborts, and we report them properly,
200 * PCATCH can be put back. In the mean time, if we did
201 * permit aborting, the lock attempt would "get lost"
202 * and the lock would get stuck in the locked state.
203 */
204 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
205 if (error != 0) {
206 if (error == EWOULDBLOCK) {
207 /*
208 * We timed out, so we rewrite the request
209 * to the fifo, but only if it isn't already
210 * full.
211 */
212 ioflg |= IO_NDELAY;
213 continue;
214 }
215
216 break;
217 }
218
219 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
220 if (p->p_nlminfo->set_getlk_pid) {
221 fl->l_pid = p->p_nlminfo->getlk_pid;
222 } else {
223 fl->l_type = F_UNLCK;
224 }
225 }
226 error = p->p_nlminfo->retcode;
227 break;
228 }
229
230 error1 = vn_close(wvp, FWRITE, thread0.td_ucred, td);
231 /* prefer any previous 'error' to our vn_close 'error1'. */
232 return (error != 0 ? error : error1);
233 }
234
235 /*
236 * nfslockdans --
237 * NFS advisory byte-level locks answer from the lock daemon.
238 */
239 int
240 nfslockdans(struct thread *td, struct lockd_ans *ansp)
241 {
242 struct proc *targetp;
243 int error;
244
245 /* Let root, or someone who once was root (lockd generally
246 * switches to the daemon uid once it is done setting up) make
247 * this call.
248 *
249 * XXX This authorization check is probably not right.
250 */
251 if ((error = suser(td)) != 0 &&
252 td->td_ucred->cr_svuid != 0)
253 return (error);
254
255 /* the version should match, or we're out of sync */
256 if (ansp->la_vers != LOCKD_ANS_VERSION)
257 return (EINVAL);
258
259 /* Find the process, set its return errno and wake it up. */
260 if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
261 return (ESRCH);
262
263 /* verify the pid hasn't been reused (if we can), and it isn't waiting
264 * for an answer from a more recent request. We return an EPIPE if
265 * the match fails, because we've already used ESRCH above, and this
266 * is sort of like writing on a pipe after the reader has closed it.
267 */
268 if (targetp->p_nlminfo == NULL ||
269 ((ansp->la_msg_ident.msg_seq != -1) &&
270 (timevalcmp(&targetp->p_nlminfo->pid_start,
271 &ansp->la_msg_ident.pid_start, !=) ||
272 targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
273 PROC_UNLOCK(targetp);
274 return (EPIPE);
275 }
276
277 targetp->p_nlminfo->retcode = ansp->la_errno;
278 targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
279 targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
280
281 wakeup(targetp->p_nlminfo);
282
283 PROC_UNLOCK(targetp);
284 return (0);
285 }
Cache object: 2f37ec2e93542c381dc8d15018352e87
|