2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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.
44 #include <fs/nfs/nfsport.h>
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];
52 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
55 * copies a uio scatter/gather list to an mbuf chain.
56 * NOTE: can ony handle iovcnt == 1
59 nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
62 struct mbuf *mp, *mp2;
64 int uiosiz, clflg, rem;
67 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
69 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
73 rem = NFSM_RNDUP(siz) - siz;
77 KASSERT((nd->nd_flag & ND_EXTPG) != 0 || mcp ==
78 mtod(mp, char *) + mp->m_len, ("nfsm_uiombuf: mcp wrong"));
79 left = uiop->uio_iov->iov_len;
80 uiocp = uiop->uio_iov->iov_base;
85 if ((nd->nd_flag & ND_EXTPG) != 0)
86 mlen = nd->nd_bextpgsiz;
88 mlen = M_TRAILINGSPACE(mp);
90 if ((nd->nd_flag & ND_EXTPG) != 0) {
91 mp = nfsm_add_ext_pgs(mp,
92 nd->nd_maxextsiz, &nd->nd_bextpg);
93 mcp = (char *)(void *)PHYS_TO_DMAP(
94 mp->m_epg_pa[nd->nd_bextpg]);
95 nd->nd_bextpgsiz = mlen = PAGE_SIZE;
98 NFSMCLGET(mp, M_WAITOK);
102 mlen = M_TRAILINGSPACE(mp);
103 mcp = mtod(mp, char *);
108 xfer = (left > mlen) ? mlen : left;
109 if (uiop->uio_segflg == UIO_SYSSPACE)
110 NFSBCOPY(uiocp, mcp, xfer);
112 copyin(uiocp, mcp, xfer);
117 if ((nd->nd_flag & ND_EXTPG) != 0) {
118 nd->nd_bextpgsiz -= xfer;
119 mp->m_epg_last_len += xfer;
121 uiop->uio_offset += xfer;
122 uiop->uio_resid -= xfer;
124 tcp = (char *)uiop->uio_iov->iov_base;
126 uiop->uio_iov->iov_base = (void *)tcp;
127 uiop->uio_iov->iov_len -= uiosiz;
131 if ((nd->nd_flag & ND_EXTPG) == 0 && rem >
132 M_TRAILINGSPACE(mp)) {
136 mcp = mtod(mp, char *);
137 } else if ((nd->nd_flag & ND_EXTPG) != 0 && rem >
139 mp = nfsm_add_ext_pgs(mp, nd->nd_maxextsiz,
141 mcp = (char *)(void *)
142 PHYS_TO_DMAP(mp->m_epg_pa[nd->nd_bextpg]);
143 nd->nd_bextpgsiz = PAGE_SIZE;
145 for (left = 0; left < rem; left++)
148 if ((nd->nd_flag & ND_EXTPG) != 0) {
149 nd->nd_bextpgsiz -= rem;
150 mp->m_epg_last_len += rem;
158 * copies a uio scatter/gather list to an mbuf chain.
159 * This version returns the mbuf list and does not use "nd".
160 * NOTE: can ony handle iovcnt == 1
163 nfsm_uiombuflist(struct uio *uiop, int siz, u_int maxext)
166 struct mbuf *mp, *mp2, *firstmp;
167 int extpg, extpgsiz = 0, i, left, mlen, rem, xfer;
171 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
174 mp = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
175 mcp = (char *)(void *)PHYS_TO_DMAP(mp->m_epg_pa[0]);
177 extpgsiz = PAGE_SIZE;
179 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
184 NFSMCLGET(mp, M_WAITOK);
187 mcp = mtod(mp, char *);
191 rem = NFSM_RNDUP(siz) - siz;
193 left = uiop->uio_iov->iov_len;
194 uiocp = uiop->uio_iov->iov_base;
202 mlen = M_TRAILINGSPACE(mp);
205 mp = nfsm_add_ext_pgs(mp, maxext,
207 mlen = extpgsiz = PAGE_SIZE;
208 mcp = (char *)(void *)PHYS_TO_DMAP(
209 mp->m_epg_pa[extpg]);
212 NFSMCLGET(mp, M_WAITOK);
215 mcp = mtod(mp, char *);
216 mlen = M_TRAILINGSPACE(mp);
222 xfer = (left > mlen) ? mlen : left;
223 if (uiop->uio_segflg == UIO_SYSSPACE)
224 NFSBCOPY(uiocp, mcp, xfer);
226 copyin(uiocp, mcp, xfer);
231 mp->m_epg_last_len += xfer;
235 uiop->uio_offset += xfer;
236 uiop->uio_resid -= xfer;
238 tcp = (char *)uiop->uio_iov->iov_base;
240 uiop->uio_iov->iov_base = (void *)tcp;
241 uiop->uio_iov->iov_len -= uiosiz;
245 KASSERT((mp->m_flags & M_EXTPG) != 0 ||
246 rem <= M_TRAILINGSPACE(mp),
247 ("nfsm_uiombuflist: no space for padding"));
248 for (i = 0; i < rem; i++)
252 mp->m_epg_last_len += rem;
258 * Load vnode attributes from the xdr file attributes.
259 * Returns EBADRPC if they can't be parsed, 0 otherwise.
262 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
264 struct nfs_fattr *fp;
267 if (nd->nd_flag & ND_NFSV4) {
268 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
269 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
270 } else if (nd->nd_flag & ND_NFSV3) {
271 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
272 nap->na_type = nfsv34tov_type(fp->fa_type);
273 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
274 nap->na_rdev = NFSMAKEDEV(
275 fxdr_unsigned(int, fp->fa3_rdev.specdata1),
276 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
277 nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
278 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
279 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
280 nap->na_size = fxdr_hyper(&fp->fa3_size);
281 nap->na_blocksize = NFS_FABLKSIZE;
282 nap->na_bytes = fxdr_hyper(&fp->fa3_used);
283 nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
284 fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
285 fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
286 fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
287 nap->na_btime.tv_sec = -1;
288 nap->na_btime.tv_nsec = 0;
293 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
294 nap->na_type = nfsv2tov_type(fp->fa_type);
295 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
296 if (nap->na_type == VNON || nap->na_type == VREG)
297 nap->na_type = IFTOVT(nap->na_mode);
298 nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
301 * Really ugly NFSv2 kludge.
303 if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
304 nap->na_type = VFIFO;
305 nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
306 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
307 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
308 nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
309 nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
311 (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
313 nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
314 fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
315 fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
317 nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
318 fp->fa2_ctime.nfsv2_sec);
319 nap->na_ctime.tv_nsec = 0;
320 nap->na_btime.tv_sec = -1;
321 nap->na_btime.tv_nsec = 0;
322 nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
330 * This function finds the directory cookie that corresponds to the
331 * logical byte offset given.
334 nfscl_getcookie(struct nfsnode *np, off_t off, int add)
336 struct nfsdmap *dp, *dp2;
339 pos = off / NFS_DIRBLKSIZ;
341 KASSERT(!add, ("nfs getcookie add at 0"));
342 return (&nfs_nullcookie);
345 dp = LIST_FIRST(&np->n_cookies);
348 dp = malloc(sizeof (struct nfsdmap),
349 M_NFSDIROFF, M_WAITOK);
350 dp->ndm_eocookie = 0;
351 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
355 while (pos >= NFSNUMCOOKIES) {
356 pos -= NFSNUMCOOKIES;
357 if (LIST_NEXT(dp, ndm_list) != NULL) {
358 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
359 pos >= dp->ndm_eocookie)
361 dp = LIST_NEXT(dp, ndm_list);
363 dp2 = malloc(sizeof (struct nfsdmap),
364 M_NFSDIROFF, M_WAITOK);
365 dp2->ndm_eocookie = 0;
366 LIST_INSERT_AFTER(dp, dp2, ndm_list);
371 if (pos >= dp->ndm_eocookie) {
373 dp->ndm_eocookie = pos + 1;
377 return (&dp->ndm_cookies[pos]);
381 * Gets a file handle out of an nfs reply sent to the client and returns
382 * the file handle and the file's attributes.
383 * For V4, it assumes that Getfh and Getattr Op's results are here.
386 nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
387 struct nfsvattr *nap, int *attrflagp)
390 int error = 0, flag = 1;
395 * First get the file handle and vnode.
397 if (nd->nd_flag & ND_NFSV3) {
398 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
399 flag = fxdr_unsigned(int, *tl);
400 } else if (nd->nd_flag & ND_NFSV4) {
401 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
402 /* If the GetFH failed, clear flag. */
404 nd->nd_flag |= ND_NOMOREDATA;
406 error = ENXIO; /* Return ENXIO so *nfhpp isn't used. */
410 error = nfsm_getfh(nd, nfhpp);
416 * Now, get the attributes.
418 if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) {
419 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
421 nd->nd_flag |= ND_NOMOREDATA;
424 } else if (nd->nd_flag & ND_NFSV3) {
425 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
427 flag = fxdr_unsigned(int, *tl);
428 } else if (fxdr_unsigned(int, *tl)) {
429 error = nfsm_advance(nd, NFSX_V3FATTR, -1);
435 error = nfsm_loadattr(nd, nap);
444 * Initialize the owner/delegation sleep lock.
447 nfscl_lockinit(struct nfsv4lock *lckp)
450 lckp->nfslock_usecnt = 0;
451 lckp->nfslock_lock = 0;
455 * Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
456 * thread for each posix process in the kernel.)
459 nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
464 igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
469 * Release an exclusive lock.
472 nfscl_lockunlock(struct nfsv4lock *lckp)
475 nfsv4_unlock(lckp, 0);
479 * Called to derefernce a lock on a stateid (delegation or open owner).
482 nfscl_lockderef(struct nfsv4lock *lckp)
486 lckp->nfslock_usecnt--;
487 if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) {
488 lckp->nfslock_lock &= ~NFSV4LOCK_WANTED;
489 wakeup((caddr_t)lckp);