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 #ifndef APPLEKEXT 45 #include <fs/nfs/nfsport.h> 46 47 extern struct nfsstatsv1 nfsstatsv1; 48 extern int ncl_mbuf_mlen; 49 extern enum vtype newnv2tov_type[8]; 50 extern enum vtype nv34tov_type[8]; 51 NFSCLSTATEMUTEX; 52 #endif /* !APPLEKEXT */ 53 54 static nfsuint64 nfs_nullcookie = {{ 0, 0 }}; 55 56 /* 57 * copies a uio scatter/gather list to an mbuf chain. 58 * NOTE: can ony handle iovcnt == 1 59 */ 60 APPLESTATIC void 61 nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz) 62 { 63 char *uiocp; 64 struct mbuf *mp, *mp2; 65 int xfer, left, mlen; 66 int uiosiz, clflg, rem; 67 char *cp, *tcp; 68 69 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1")); 70 71 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */ 72 clflg = 1; 73 else 74 clflg = 0; 75 rem = NFSM_RNDUP(siz) - siz; 76 mp = mp2 = nd->nd_mb; 77 while (siz > 0) { 78 left = uiop->uio_iov->iov_len; 79 uiocp = uiop->uio_iov->iov_base; 80 if (left > siz) 81 left = siz; 82 uiosiz = left; 83 while (left > 0) { 84 mlen = M_TRAILINGSPACE(mp); 85 if (mlen == 0) { 86 if (clflg) 87 NFSMCLGET(mp, M_WAITOK); 88 else 89 NFSMGET(mp); 90 mp->m_len = 0; 91 mp2->m_next = mp; 92 mp2 = mp; 93 mlen = M_TRAILINGSPACE(mp); 94 } 95 xfer = (left > mlen) ? mlen : left; 96 #ifdef notdef 97 /* Not Yet.. */ 98 if (uiop->uio_iov->iov_op != NULL) 99 (*(uiop->uio_iov->iov_op)) 100 (uiocp, mtod(mp, caddr_t) + mp->m_len, 101 xfer); 102 else 103 #endif 104 if (uiop->uio_segflg == UIO_SYSSPACE) 105 NFSBCOPY(uiocp, mtod(mp, caddr_t) + mp->m_len, 106 xfer); 107 else 108 copyin(uiocp, mtod(mp, caddr_t) + mp->m_len, xfer); 109 mp->m_len += xfer; 110 left -= xfer; 111 uiocp += xfer; 112 uiop->uio_offset += xfer; 113 uiop->uio_resid -= xfer; 114 } 115 tcp = (char *)uiop->uio_iov->iov_base; 116 tcp += uiosiz; 117 uiop->uio_iov->iov_base = (void *)tcp; 118 uiop->uio_iov->iov_len -= uiosiz; 119 siz -= uiosiz; 120 } 121 if (rem > 0) { 122 if (rem > M_TRAILINGSPACE(mp)) { 123 NFSMGET(mp); 124 mp->m_len = 0; 125 mp2->m_next = mp; 126 } 127 cp = mtod(mp, caddr_t) + mp->m_len; 128 for (left = 0; left < rem; left++) 129 *cp++ = '\0'; 130 mp->m_len += rem; 131 nd->nd_bpos = cp; 132 } else 133 nd->nd_bpos = mtod(mp, caddr_t) + mp->m_len; 134 nd->nd_mb = mp; 135 } 136 137 /* 138 * copies a uio scatter/gather list to an mbuf chain. 139 * This version returns the mbuf list and does not use "nd". 140 * NOTE: can ony handle iovcnt == 1 141 */ 142 struct mbuf * 143 nfsm_uiombuflist(struct uio *uiop, int siz, struct mbuf **mbp, char **cpp) 144 { 145 char *uiocp; 146 struct mbuf *mp, *mp2, *firstmp; 147 int xfer, left, mlen; 148 int uiosiz, clflg; 149 char *tcp; 150 151 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1")); 152 153 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */ 154 clflg = 1; 155 else 156 clflg = 0; 157 if (clflg != 0) 158 NFSMCLGET(mp, M_WAITOK); 159 else 160 NFSMGET(mp); 161 mp->m_len = 0; 162 firstmp = mp2 = mp; 163 while (siz > 0) { 164 left = uiop->uio_iov->iov_len; 165 uiocp = uiop->uio_iov->iov_base; 166 if (left > siz) 167 left = siz; 168 uiosiz = left; 169 while (left > 0) { 170 mlen = M_TRAILINGSPACE(mp); 171 if (mlen == 0) { 172 if (clflg) 173 NFSMCLGET(mp, M_WAITOK); 174 else 175 NFSMGET(mp); 176 mp->m_len = 0; 177 mp2->m_next = mp; 178 mp2 = mp; 179 mlen = M_TRAILINGSPACE(mp); 180 } 181 xfer = (left > mlen) ? mlen : left; 182 if (uiop->uio_segflg == UIO_SYSSPACE) 183 NFSBCOPY(uiocp, mtod(mp, caddr_t) + 184 mp->m_len, xfer); 185 else 186 copyin(uiocp, mtod(mp, caddr_t) + 187 mp->m_len, xfer); 188 mp->m_len += xfer; 189 left -= xfer; 190 uiocp += xfer; 191 uiop->uio_offset += xfer; 192 uiop->uio_resid -= xfer; 193 } 194 tcp = (char *)uiop->uio_iov->iov_base; 195 tcp += uiosiz; 196 uiop->uio_iov->iov_base = (void *)tcp; 197 uiop->uio_iov->iov_len -= uiosiz; 198 siz -= uiosiz; 199 } 200 if (cpp != NULL) 201 *cpp = mtod(mp, caddr_t) + mp->m_len; 202 if (mbp != NULL) 203 *mbp = mp; 204 return (firstmp); 205 } 206 207 /* 208 * Load vnode attributes from the xdr file attributes. 209 * Returns EBADRPC if they can't be parsed, 0 otherwise. 210 */ 211 APPLESTATIC int 212 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap) 213 { 214 struct nfs_fattr *fp; 215 int error = 0; 216 217 if (nd->nd_flag & ND_NFSV4) { 218 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, 219 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 220 } else if (nd->nd_flag & ND_NFSV3) { 221 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR); 222 nap->na_type = nfsv34tov_type(fp->fa_type); 223 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode); 224 nap->na_rdev = NFSMAKEDEV( 225 fxdr_unsigned(int, fp->fa3_rdev.specdata1), 226 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 227 nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink); 228 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid); 229 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid); 230 nap->na_size = fxdr_hyper(&fp->fa3_size); 231 nap->na_blocksize = NFS_FABLKSIZE; 232 nap->na_bytes = fxdr_hyper(&fp->fa3_used); 233 nap->na_fileid = fxdr_hyper(&fp->fa3_fileid); 234 fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime); 235 fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime); 236 fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime); 237 nap->na_flags = 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 APPLESTATIC 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 APPLESTATIC 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 APPLESTATIC 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 APPLESTATIC 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 APPLESTATIC void 417 nfscl_lockunlock(struct nfsv4lock *lckp) 418 { 419 420 nfsv4_unlock(lckp, 0); 421 } 422 423 /* 424 * Called to derefernce a lock on a stateid (delegation or open owner). 425 */ 426 APPLESTATIC 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 439