1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40 * These functions support the macros and help fiddle mbuf chains for
41 * the nfs op functions. They do things like create the rpc header and
42 * copy data between mbuf chains and uio lists.
43 */
44 #include <fs/nfs/nfsport.h>
45
46 extern struct nfsstatsv1 nfsstatsv1;
47 extern int ncl_mbuf_mlen;
48 extern enum vtype newnv2tov_type[8];
49 extern enum vtype nv34tov_type[8];
50 NFSCLSTATEMUTEX;
51
52 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
53
54 /*
55 * copies a uio scatter/gather list to an mbuf chain.
56 * NOTE: can only handle iovcnt == 1
57 */
58 void
59 nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
60 {
61 char *uiocp;
62 struct mbuf *mp, *mp2;
63 int xfer, left, mlen;
64 int uiosiz, clflg, rem;
65 char *cp, *tcp;
66
67 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
68
69 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
70 clflg = 1;
71 else
72 clflg = 0;
73 rem = NFSM_RNDUP(siz) - siz;
74 mp = mp2 = nd->nd_mb;
75 while (siz > 0) {
76 left = uiop->uio_iov->iov_len;
77 uiocp = uiop->uio_iov->iov_base;
78 if (left > siz)
79 left = siz;
80 uiosiz = left;
81 while (left > 0) {
82 mlen = M_TRAILINGSPACE(mp);
83 if (mlen == 0) {
84 if (clflg)
85 NFSMCLGET(mp, M_WAITOK);
86 else
87 NFSMGET(mp);
88 mbuf_setlen(mp, 0);
89 mbuf_setnext(mp2, mp);
90 mp2 = mp;
91 mlen = M_TRAILINGSPACE(mp);
92 }
93 xfer = (left > mlen) ? mlen : left;
94 #ifdef notdef
95 /* Not Yet.. */
96 if (uiop->uio_iov->iov_op != NULL)
97 (*(uiop->uio_iov->iov_op))
98 (uiocp, NFSMTOD(mp, caddr_t) + mbuf_len(mp),
99 xfer);
100 else
101 #endif
102 if (uiop->uio_segflg == UIO_SYSSPACE)
103 NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) + mbuf_len(mp),
104 xfer);
105 else
106 copyin(CAST_USER_ADDR_T(uiocp), NFSMTOD(mp, caddr_t)
107 + mbuf_len(mp), xfer);
108 mbuf_setlen(mp, mbuf_len(mp) + xfer);
109 left -= xfer;
110 uiocp += xfer;
111 uiop->uio_offset += xfer;
112 uiop->uio_resid -= xfer;
113 }
114 tcp = (char *)uiop->uio_iov->iov_base;
115 tcp += uiosiz;
116 uiop->uio_iov->iov_base = (void *)tcp;
117 uiop->uio_iov->iov_len -= uiosiz;
118 siz -= uiosiz;
119 }
120 if (rem > 0) {
121 if (rem > M_TRAILINGSPACE(mp)) {
122 NFSMGET(mp);
123 mbuf_setlen(mp, 0);
124 mbuf_setnext(mp2, mp);
125 }
126 cp = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
127 for (left = 0; left < rem; left++)
128 *cp++ = '\0';
129 mbuf_setlen(mp, mbuf_len(mp) + rem);
130 nd->nd_bpos = cp;
131 } else
132 nd->nd_bpos = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
133 nd->nd_mb = mp;
134 }
135
136 /*
137 * copies a uio scatter/gather list to an mbuf chain.
138 * This version returns the mbuf list and does not use "nd".
139 * NOTE: can only handle iovcnt == 1
140 */
141 struct mbuf *
142 nfsm_uiombuflist(struct uio *uiop, int siz, struct mbuf **mbp, char **cpp)
143 {
144 char *uiocp;
145 struct mbuf *mp, *mp2, *firstmp;
146 int xfer, left, mlen;
147 int uiosiz, clflg;
148 char *tcp;
149
150 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
151
152 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
153 clflg = 1;
154 else
155 clflg = 0;
156 if (clflg != 0)
157 NFSMCLGET(mp, M_WAITOK);
158 else
159 NFSMGET(mp);
160 mbuf_setlen(mp, 0);
161 firstmp = mp2 = mp;
162 while (siz > 0) {
163 left = uiop->uio_iov->iov_len;
164 uiocp = uiop->uio_iov->iov_base;
165 if (left > siz)
166 left = siz;
167 uiosiz = left;
168 while (left > 0) {
169 mlen = M_TRAILINGSPACE(mp);
170 if (mlen == 0) {
171 if (clflg)
172 NFSMCLGET(mp, M_WAITOK);
173 else
174 NFSMGET(mp);
175 mbuf_setlen(mp, 0);
176 mbuf_setnext(mp2, mp);
177 mp2 = mp;
178 mlen = M_TRAILINGSPACE(mp);
179 }
180 xfer = (left > mlen) ? mlen : left;
181 if (uiop->uio_segflg == UIO_SYSSPACE)
182 NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) +
183 mbuf_len(mp), xfer);
184 else
185 copyin(uiocp, NFSMTOD(mp, caddr_t) +
186 mbuf_len(mp), xfer);
187 mbuf_setlen(mp, mbuf_len(mp) + xfer);
188 left -= xfer;
189 uiocp += xfer;
190 uiop->uio_offset += xfer;
191 uiop->uio_resid -= xfer;
192 }
193 tcp = (char *)uiop->uio_iov->iov_base;
194 tcp += uiosiz;
195 uiop->uio_iov->iov_base = (void *)tcp;
196 uiop->uio_iov->iov_len -= uiosiz;
197 siz -= uiosiz;
198 }
199 if (cpp != NULL)
200 *cpp = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
201 if (mbp != NULL)
202 *mbp = mp;
203 return (firstmp);
204 }
205
206 /*
207 * Load vnode attributes from the xdr file attributes.
208 * Returns EBADRPC if they can't be parsed, 0 otherwise.
209 */
210 int
211 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
212 {
213 struct nfs_fattr *fp;
214 int error = 0;
215
216 if (nd->nd_flag & ND_NFSV4) {
217 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
218 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
219 } else if (nd->nd_flag & ND_NFSV3) {
220 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
221 nap->na_type = nfsv34tov_type(fp->fa_type);
222 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
223 nap->na_rdev = NFSMAKEDEV(
224 fxdr_unsigned(int, fp->fa3_rdev.specdata1),
225 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
226 nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
227 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
228 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
229 nap->na_size = fxdr_hyper(&fp->fa3_size);
230 nap->na_blocksize = NFS_FABLKSIZE;
231 nap->na_bytes = fxdr_hyper(&fp->fa3_used);
232 nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
233 fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
234 fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
235 fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
236 nap->na_flags = 0;
237 nap->na_gen = 0;
238 nap->na_filerev = 0;
239 } else {
240 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
241 nap->na_type = nfsv2tov_type(fp->fa_type);
242 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
243 if (nap->na_type == VNON || nap->na_type == VREG)
244 nap->na_type = IFTOVT(nap->na_mode);
245 nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
246
247 /*
248 * Really ugly NFSv2 kludge.
249 */
250 if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
251 nap->na_type = VFIFO;
252 nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
253 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
254 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
255 nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
256 nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
257 nap->na_bytes =
258 (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
259 NFS_FABLKSIZE;
260 nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
261 fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
262 fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
263 nap->na_flags = 0;
264 nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
265 fp->fa2_ctime.nfsv2_sec);
266 nap->na_ctime.tv_nsec = 0;
267 nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
268 nap->na_filerev = 0;
269 }
270 nfsmout:
271 return (error);
272 }
273
274 /*
275 * This function finds the directory cookie that corresponds to the
276 * logical byte offset given.
277 */
278 nfsuint64 *
279 nfscl_getcookie(struct nfsnode *np, off_t off, int add)
280 {
281 struct nfsdmap *dp, *dp2;
282 int pos;
283
284 pos = off / NFS_DIRBLKSIZ;
285 if (pos == 0) {
286 KASSERT(!add, ("nfs getcookie add at 0"));
287 return (&nfs_nullcookie);
288 }
289 pos--;
290 dp = LIST_FIRST(&np->n_cookies);
291 if (!dp) {
292 if (add) {
293 dp = malloc(sizeof (struct nfsdmap),
294 M_NFSDIROFF, M_WAITOK);
295 dp->ndm_eocookie = 0;
296 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
297 } else
298 return (NULL);
299 }
300 while (pos >= NFSNUMCOOKIES) {
301 pos -= NFSNUMCOOKIES;
302 if (LIST_NEXT(dp, ndm_list) != NULL) {
303 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
304 pos >= dp->ndm_eocookie)
305 return (NULL);
306 dp = LIST_NEXT(dp, ndm_list);
307 } else if (add) {
308 dp2 = malloc(sizeof (struct nfsdmap),
309 M_NFSDIROFF, M_WAITOK);
310 dp2->ndm_eocookie = 0;
311 LIST_INSERT_AFTER(dp, dp2, ndm_list);
312 dp = dp2;
313 } else
314 return (NULL);
315 }
316 if (pos >= dp->ndm_eocookie) {
317 if (add)
318 dp->ndm_eocookie = pos + 1;
319 else
320 return (NULL);
321 }
322 return (&dp->ndm_cookies[pos]);
323 }
324
325 /*
326 * Gets a file handle out of an nfs reply sent to the client and returns
327 * the file handle and the file's attributes.
328 * For V4, it assumes that Getfh and Getattr Op's results are here.
329 */
330 int
331 nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
332 struct nfsvattr *nap, int *attrflagp)
333 {
334 u_int32_t *tl;
335 int error = 0, flag = 1;
336
337 *nfhpp = NULL;
338 *attrflagp = 0;
339 /*
340 * First get the file handle and vnode.
341 */
342 if (nd->nd_flag & ND_NFSV3) {
343 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
344 flag = fxdr_unsigned(int, *tl);
345 } else if (nd->nd_flag & ND_NFSV4) {
346 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
347 /* If the GetFH failed, clear flag. */
348 if (*++tl != 0) {
349 nd->nd_flag |= ND_NOMOREDATA;
350 flag = 0;
351 error = ENXIO; /* Return ENXIO so *nfhpp isn't used. */
352 }
353 }
354 if (flag) {
355 error = nfsm_getfh(nd, nfhpp);
356 if (error)
357 return (error);
358 }
359
360 /*
361 * Now, get the attributes.
362 */
363 if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) {
364 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
365 if (*++tl != 0) {
366 nd->nd_flag |= ND_NOMOREDATA;
367 flag = 0;
368 }
369 } else if (nd->nd_flag & ND_NFSV3) {
370 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
371 if (flag) {
372 flag = fxdr_unsigned(int, *tl);
373 } else if (fxdr_unsigned(int, *tl)) {
374 error = nfsm_advance(nd, NFSX_V3FATTR, -1);
375 if (error)
376 return (error);
377 }
378 }
379 if (flag) {
380 error = nfsm_loadattr(nd, nap);
381 if (!error)
382 *attrflagp = 1;
383 }
384 nfsmout:
385 return (error);
386 }
387
388 /*
389 * Initialize the owner/delegation sleep lock.
390 */
391 void
392 nfscl_lockinit(struct nfsv4lock *lckp)
393 {
394
395 lckp->nfslock_usecnt = 0;
396 lckp->nfslock_lock = 0;
397 }
398
399 /*
400 * Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
401 * thread for each posix process in the kernel.)
402 */
403 void
404 nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
405 {
406 int igotlock;
407
408 do {
409 igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
410 } while (!igotlock);
411 }
412
413 /*
414 * Release an exclusive lock.
415 */
416 void
417 nfscl_lockunlock(struct nfsv4lock *lckp)
418 {
419
420 nfsv4_unlock(lckp, 0);
421 }
422
423 /*
424 * Called to dereference a lock on a stateid (delegation or open owner).
425 */
426 void
427 nfscl_lockderef(struct nfsv4lock *lckp)
428 {
429
430 NFSLOCKCLSTATE();
431 lckp->nfslock_usecnt--;
432 if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) {
433 lckp->nfslock_lock &= ~NFSV4LOCK_WANTED;
434 wakeup((caddr_t)lckp);
435 }
436 NFSUNLOCKCLSTATE();
437 }
438
Cache object: 8be7928e91f42d3ee7a15aea400c2d04
|