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 /* 38 * These functions support the macros and help fiddle mbuf chains for 39 * the nfs op functions. They do things like create the rpc header and 40 * copy data between mbuf chains and uio lists. 41 */ 42 #include <fs/nfs/nfsport.h> 43 44 extern struct nfsstatsv1 nfsstatsv1; 45 extern int ncl_mbuf_mlen; 46 extern __enum_uint8(vtype) newnv2tov_type[8]; 47 extern __enum_uint8(vtype) nv34tov_type[8]; 48 NFSCLSTATEMUTEX; 49 50 /* 51 * copies a uio scatter/gather list to an mbuf chain. 52 * NOTE: can only handle iovcnt == 1 53 */ 54 void 55 nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz) 56 { 57 char *uiocp; 58 struct mbuf *mp, *mp2; 59 int xfer, left, mlen; 60 int uiosiz, clflg, rem; 61 char *mcp, *tcp; 62 63 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1")); 64 65 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */ 66 clflg = 1; 67 else 68 clflg = 0; 69 rem = NFSM_RNDUP(siz) - siz; 70 mp = mp2 = nd->nd_mb; 71 mcp = nd->nd_bpos; 72 while (siz > 0) { 73 KASSERT((nd->nd_flag & ND_EXTPG) != 0 || mcp == 74 mtod(mp, char *) + mp->m_len, ("nfsm_uiombuf: mcp wrong")); 75 left = uiop->uio_iov->iov_len; 76 uiocp = uiop->uio_iov->iov_base; 77 if (left > siz) 78 left = siz; 79 uiosiz = left; 80 while (left > 0) { 81 if ((nd->nd_flag & ND_EXTPG) != 0) 82 mlen = nd->nd_bextpgsiz; 83 else 84 mlen = M_TRAILINGSPACE(mp); 85 if (mlen == 0) { 86 if ((nd->nd_flag & ND_EXTPG) != 0) { 87 mp = nfsm_add_ext_pgs(mp, 88 nd->nd_maxextsiz, &nd->nd_bextpg); 89 mcp = (char *)(void *)PHYS_TO_DMAP( 90 mp->m_epg_pa[nd->nd_bextpg]); 91 nd->nd_bextpgsiz = mlen = PAGE_SIZE; 92 } else { 93 if (clflg) 94 NFSMCLGET(mp, M_WAITOK); 95 else 96 NFSMGET(mp); 97 mp->m_len = 0; 98 mlen = M_TRAILINGSPACE(mp); 99 mcp = mtod(mp, char *); 100 mp2->m_next = mp; 101 mp2 = mp; 102 } 103 } 104 xfer = (left > mlen) ? mlen : left; 105 if (uiop->uio_segflg == UIO_SYSSPACE) 106 NFSBCOPY(uiocp, mcp, xfer); 107 else 108 copyin(uiocp, mcp, xfer); 109 mp->m_len += xfer; 110 left -= xfer; 111 uiocp += xfer; 112 mcp += xfer; 113 if ((nd->nd_flag & ND_EXTPG) != 0) { 114 nd->nd_bextpgsiz -= xfer; 115 mp->m_epg_last_len += xfer; 116 } 117 uiop->uio_offset += xfer; 118 uiop->uio_resid -= xfer; 119 } 120 tcp = (char *)uiop->uio_iov->iov_base; 121 tcp += uiosiz; 122 uiop->uio_iov->iov_base = (void *)tcp; 123 uiop->uio_iov->iov_len -= uiosiz; 124 siz -= uiosiz; 125 } 126 if (rem > 0) { 127 if ((nd->nd_flag & ND_EXTPG) == 0 && rem > 128 M_TRAILINGSPACE(mp)) { 129 NFSMGET(mp); 130 mp->m_len = 0; 131 mp2->m_next = mp; 132 mcp = mtod(mp, char *); 133 } else if ((nd->nd_flag & ND_EXTPG) != 0 && rem > 134 nd->nd_bextpgsiz) { 135 mp = nfsm_add_ext_pgs(mp, nd->nd_maxextsiz, 136 &nd->nd_bextpg); 137 mcp = (char *)(void *) 138 PHYS_TO_DMAP(mp->m_epg_pa[nd->nd_bextpg]); 139 nd->nd_bextpgsiz = PAGE_SIZE; 140 } 141 for (left = 0; left < rem; left++) 142 *mcp++ = '\0'; 143 mp->m_len += rem; 144 if ((nd->nd_flag & ND_EXTPG) != 0) { 145 nd->nd_bextpgsiz -= rem; 146 mp->m_epg_last_len += rem; 147 } 148 } 149 nd->nd_bpos = mcp; 150 nd->nd_mb = mp; 151 } 152 153 /* 154 * copies a uio scatter/gather list to an mbuf chain. 155 * This version returns the mbuf list and does not use "nd". 156 * NOTE: can only handle iovcnt == 1 157 */ 158 struct mbuf * 159 nfsm_uiombuflist(struct uio *uiop, int siz, u_int maxext) 160 { 161 char *uiocp; 162 struct mbuf *mp, *mp2, *firstmp; 163 int extpg, extpgsiz = 0, i, left, mlen, rem, xfer; 164 int uiosiz, clflg; 165 char *mcp, *tcp; 166 167 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1")); 168 169 if (maxext > 0) { 170 mp = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK); 171 mcp = (char *)(void *)PHYS_TO_DMAP(mp->m_epg_pa[0]); 172 extpg = 0; 173 extpgsiz = PAGE_SIZE; 174 } else { 175 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */ 176 clflg = 1; 177 else 178 clflg = 0; 179 if (clflg != 0) 180 NFSMCLGET(mp, M_WAITOK); 181 else 182 NFSMGET(mp); 183 mcp = mtod(mp, char *); 184 } 185 mp->m_len = 0; 186 firstmp = mp2 = mp; 187 rem = NFSM_RNDUP(siz) - siz; 188 while (siz > 0) { 189 left = uiop->uio_iov->iov_len; 190 uiocp = uiop->uio_iov->iov_base; 191 if (left > siz) 192 left = siz; 193 uiosiz = left; 194 while (left > 0) { 195 if (maxext > 0) 196 mlen = extpgsiz; 197 else 198 mlen = M_TRAILINGSPACE(mp); 199 if (mlen == 0) { 200 if (maxext > 0) { 201 mp = nfsm_add_ext_pgs(mp, maxext, 202 &extpg); 203 mlen = extpgsiz = PAGE_SIZE; 204 mcp = (char *)(void *)PHYS_TO_DMAP( 205 mp->m_epg_pa[extpg]); 206 } else { 207 if (clflg) 208 NFSMCLGET(mp, M_WAITOK); 209 else 210 NFSMGET(mp); 211 mcp = mtod(mp, char *); 212 mlen = M_TRAILINGSPACE(mp); 213 mp->m_len = 0; 214 mp2->m_next = mp; 215 mp2 = mp; 216 } 217 } 218 xfer = (left > mlen) ? mlen : left; 219 if (uiop->uio_segflg == UIO_SYSSPACE) 220 NFSBCOPY(uiocp, mcp, xfer); 221 else 222 copyin(uiocp, mcp, xfer); 223 mp->m_len += xfer; 224 mcp += xfer; 225 if (maxext > 0) { 226 extpgsiz -= xfer; 227 mp->m_epg_last_len += xfer; 228 } 229 left -= xfer; 230 uiocp += xfer; 231 uiop->uio_offset += xfer; 232 uiop->uio_resid -= xfer; 233 } 234 tcp = (char *)uiop->uio_iov->iov_base; 235 tcp += uiosiz; 236 uiop->uio_iov->iov_base = (void *)tcp; 237 uiop->uio_iov->iov_len -= uiosiz; 238 siz -= uiosiz; 239 } 240 if (rem > 0) { 241 KASSERT((mp->m_flags & M_EXTPG) != 0 || 242 rem <= M_TRAILINGSPACE(mp), 243 ("nfsm_uiombuflist: no space for padding")); 244 for (i = 0; i < rem; i++) 245 *mcp++ = '\0'; 246 mp->m_len += rem; 247 if (maxext > 0) 248 mp->m_epg_last_len += rem; 249 } 250 return (firstmp); 251 } 252 253 /* 254 * Load vnode attributes from the xdr file attributes. 255 * Returns EBADRPC if they can't be parsed, 0 otherwise. 256 */ 257 int 258 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap) 259 { 260 struct nfs_fattr *fp; 261 int error = 0; 262 263 if (nd->nd_flag & ND_NFSV4) { 264 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, 265 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 266 } else if (nd->nd_flag & ND_NFSV3) { 267 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR); 268 nap->na_type = nfsv34tov_type(fp->fa_type); 269 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode); 270 nap->na_rdev = NFSMAKEDEV( 271 fxdr_unsigned(int, fp->fa3_rdev.specdata1), 272 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 273 nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink); 274 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid); 275 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid); 276 nap->na_size = fxdr_hyper(&fp->fa3_size); 277 nap->na_blocksize = NFS_FABLKSIZE; 278 nap->na_bytes = fxdr_hyper(&fp->fa3_used); 279 nap->na_fileid = fxdr_hyper(&fp->fa3_fileid); 280 fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime); 281 fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime); 282 fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime); 283 nap->na_btime.tv_sec = -1; 284 nap->na_btime.tv_nsec = 0; 285 nap->na_flags = 0; 286 nap->na_gen = 0; 287 nap->na_filerev = 0; 288 } else { 289 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR); 290 nap->na_type = nfsv2tov_type(fp->fa_type); 291 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode); 292 if (nap->na_type == VNON || nap->na_type == VREG) 293 nap->na_type = IFTOVT(nap->na_mode); 294 nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev); 295 296 /* 297 * Really ugly NFSv2 kludge. 298 */ 299 if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1)) 300 nap->na_type = VFIFO; 301 nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 302 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid); 303 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid); 304 nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 305 nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 306 nap->na_bytes = 307 (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) * 308 NFS_FABLKSIZE; 309 nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid); 310 fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime); 311 fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime); 312 nap->na_flags = 0; 313 nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t, 314 fp->fa2_ctime.nfsv2_sec); 315 nap->na_ctime.tv_nsec = 0; 316 nap->na_btime.tv_sec = -1; 317 nap->na_btime.tv_nsec = 0; 318 nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 319 nap->na_filerev = 0; 320 } 321 nfsmout: 322 return (error); 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