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 mbuf_setlen(mp, 0); 91 mbuf_setnext(mp2, 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, NFSMTOD(mp, caddr_t) + mbuf_len(mp), 101 xfer); 102 else 103 #endif 104 if (uiop->uio_segflg == UIO_SYSSPACE) 105 NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) + mbuf_len(mp), 106 xfer); 107 else 108 copyin(CAST_USER_ADDR_T(uiocp), NFSMTOD(mp, caddr_t) 109 + mbuf_len(mp), xfer); 110 mbuf_setlen(mp, mbuf_len(mp) + xfer); 111 left -= xfer; 112 uiocp += xfer; 113 uiop->uio_offset += xfer; 114 uiop->uio_resid -= xfer; 115 } 116 tcp = (char *)uiop->uio_iov->iov_base; 117 tcp += uiosiz; 118 uiop->uio_iov->iov_base = (void *)tcp; 119 uiop->uio_iov->iov_len -= uiosiz; 120 siz -= uiosiz; 121 } 122 if (rem > 0) { 123 if (rem > M_TRAILINGSPACE(mp)) { 124 NFSMGET(mp); 125 mbuf_setlen(mp, 0); 126 mbuf_setnext(mp2, mp); 127 } 128 cp = NFSMTOD(mp, caddr_t) + mbuf_len(mp); 129 for (left = 0; left < rem; left++) 130 *cp++ = '\0'; 131 mbuf_setlen(mp, mbuf_len(mp) + rem); 132 nd->nd_bpos = cp; 133 } else 134 nd->nd_bpos = NFSMTOD(mp, caddr_t) + mbuf_len(mp); 135 nd->nd_mb = mp; 136 } 137 138 /* 139 * copies a uio scatter/gather list to an mbuf chain. 140 * This version returns the mbuf list and does not use "nd". 141 * NOTE: can ony handle iovcnt == 1 142 */ 143 struct mbuf * 144 nfsm_uiombuflist(struct uio *uiop, int siz, struct mbuf **mbp, char **cpp) 145 { 146 char *uiocp; 147 struct mbuf *mp, *mp2, *firstmp; 148 int xfer, left, mlen; 149 int uiosiz, clflg; 150 char *tcp; 151 152 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1")); 153 154 if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */ 155 clflg = 1; 156 else 157 clflg = 0; 158 if (clflg != 0) 159 NFSMCLGET(mp, M_WAITOK); 160 else 161 NFSMGET(mp); 162 mbuf_setlen(mp, 0); 163 firstmp = mp2 = mp; 164 while (siz > 0) { 165 left = uiop->uio_iov->iov_len; 166 uiocp = uiop->uio_iov->iov_base; 167 if (left > siz) 168 left = siz; 169 uiosiz = left; 170 while (left > 0) { 171 mlen = M_TRAILINGSPACE(mp); 172 if (mlen == 0) { 173 if (clflg) 174 NFSMCLGET(mp, M_WAITOK); 175 else 176 NFSMGET(mp); 177 mbuf_setlen(mp, 0); 178 mbuf_setnext(mp2, mp); 179 mp2 = mp; 180 mlen = M_TRAILINGSPACE(mp); 181 } 182 xfer = (left > mlen) ? mlen : left; 183 if (uiop->uio_segflg == UIO_SYSSPACE) 184 NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) + 185 mbuf_len(mp), xfer); 186 else 187 copyin(uiocp, NFSMTOD(mp, caddr_t) + 188 mbuf_len(mp), xfer); 189 mbuf_setlen(mp, mbuf_len(mp) + xfer); 190 left -= xfer; 191 uiocp += xfer; 192 uiop->uio_offset += xfer; 193 uiop->uio_resid -= xfer; 194 } 195 tcp = (char *)uiop->uio_iov->iov_base; 196 tcp += uiosiz; 197 uiop->uio_iov->iov_base = (void *)tcp; 198 uiop->uio_iov->iov_len -= uiosiz; 199 siz -= uiosiz; 200 } 201 if (cpp != NULL) 202 *cpp = NFSMTOD(mp, caddr_t) + mbuf_len(mp); 203 if (mbp != NULL) 204 *mbp = mp; 205 return (firstmp); 206 } 207 208 /* 209 * Load vnode attributes from the xdr file attributes. 210 * Returns EBADRPC if they can't be parsed, 0 otherwise. 211 */ 212 APPLESTATIC int 213 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap) 214 { 215 struct nfs_fattr *fp; 216 int error = 0; 217 218 if (nd->nd_flag & ND_NFSV4) { 219 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, 220 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 221 } else if (nd->nd_flag & ND_NFSV3) { 222 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR); 223 nap->na_type = nfsv34tov_type(fp->fa_type); 224 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode); 225 nap->na_rdev = NFSMAKEDEV( 226 fxdr_unsigned(int, fp->fa3_rdev.specdata1), 227 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 228 nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink); 229 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid); 230 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid); 231 nap->na_size = fxdr_hyper(&fp->fa3_size); 232 nap->na_blocksize = NFS_FABLKSIZE; 233 nap->na_bytes = fxdr_hyper(&fp->fa3_used); 234 nap->na_fileid = fxdr_hyper(&fp->fa3_fileid); 235 fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime); 236 fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime); 237 fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime); 238 nap->na_flags = 0; 239 nap->na_filerev = 0; 240 } else { 241 NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR); 242 nap->na_type = nfsv2tov_type(fp->fa_type); 243 nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode); 244 if (nap->na_type == VNON || nap->na_type == VREG) 245 nap->na_type = IFTOVT(nap->na_mode); 246 nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev); 247 248 /* 249 * Really ugly NFSv2 kludge. 250 */ 251 if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1)) 252 nap->na_type = VFIFO; 253 nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 254 nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid); 255 nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid); 256 nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 257 nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 258 nap->na_bytes = 259 (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) * 260 NFS_FABLKSIZE; 261 nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid); 262 fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime); 263 fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime); 264 nap->na_flags = 0; 265 nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t, 266 fp->fa2_ctime.nfsv2_sec); 267 nap->na_ctime.tv_nsec = 0; 268 nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 269 nap->na_filerev = 0; 270 } 271 nfsmout: 272 return (error); 273 } 274 275 /* 276 * This function finds the directory cookie that corresponds to the 277 * logical byte offset given. 278 */ 279 APPLESTATIC nfsuint64 * 280 nfscl_getcookie(struct nfsnode *np, off_t off, int add) 281 { 282 struct nfsdmap *dp, *dp2; 283 int pos; 284 285 pos = off / NFS_DIRBLKSIZ; 286 if (pos == 0) { 287 KASSERT(!add, ("nfs getcookie add at 0")); 288 return (&nfs_nullcookie); 289 } 290 pos--; 291 dp = LIST_FIRST(&np->n_cookies); 292 if (!dp) { 293 if (add) { 294 dp = malloc(sizeof (struct nfsdmap), 295 M_NFSDIROFF, M_WAITOK); 296 dp->ndm_eocookie = 0; 297 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 298 } else 299 return (NULL); 300 } 301 while (pos >= NFSNUMCOOKIES) { 302 pos -= NFSNUMCOOKIES; 303 if (LIST_NEXT(dp, ndm_list) != NULL) { 304 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 305 pos >= dp->ndm_eocookie) 306 return (NULL); 307 dp = LIST_NEXT(dp, ndm_list); 308 } else if (add) { 309 dp2 = malloc(sizeof (struct nfsdmap), 310 M_NFSDIROFF, M_WAITOK); 311 dp2->ndm_eocookie = 0; 312 LIST_INSERT_AFTER(dp, dp2, ndm_list); 313 dp = dp2; 314 } else 315 return (NULL); 316 } 317 if (pos >= dp->ndm_eocookie) { 318 if (add) 319 dp->ndm_eocookie = pos + 1; 320 else 321 return (NULL); 322 } 323 return (&dp->ndm_cookies[pos]); 324 } 325 326 /* 327 * Gets a file handle out of an nfs reply sent to the client and returns 328 * the file handle and the file's attributes. 329 * For V4, it assumes that Getfh and Getattr Op's results are here. 330 */ 331 APPLESTATIC int 332 nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp, 333 struct nfsvattr *nap, int *attrflagp) 334 { 335 u_int32_t *tl; 336 int error = 0, flag = 1; 337 338 *nfhpp = NULL; 339 *attrflagp = 0; 340 /* 341 * First get the file handle and vnode. 342 */ 343 if (nd->nd_flag & ND_NFSV3) { 344 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 345 flag = fxdr_unsigned(int, *tl); 346 } else if (nd->nd_flag & ND_NFSV4) { 347 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 348 /* If the GetFH failed, clear flag. */ 349 if (*++tl != 0) { 350 nd->nd_flag |= ND_NOMOREDATA; 351 flag = 0; 352 error = ENXIO; /* Return ENXIO so *nfhpp isn't used. */ 353 } 354 } 355 if (flag) { 356 error = nfsm_getfh(nd, nfhpp); 357 if (error) 358 return (error); 359 } 360 361 /* 362 * Now, get the attributes. 363 */ 364 if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) { 365 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 366 if (*++tl != 0) { 367 nd->nd_flag |= ND_NOMOREDATA; 368 flag = 0; 369 } 370 } else if (nd->nd_flag & ND_NFSV3) { 371 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 372 if (flag) { 373 flag = fxdr_unsigned(int, *tl); 374 } else if (fxdr_unsigned(int, *tl)) { 375 error = nfsm_advance(nd, NFSX_V3FATTR, -1); 376 if (error) 377 return (error); 378 } 379 } 380 if (flag) { 381 error = nfsm_loadattr(nd, nap); 382 if (!error) 383 *attrflagp = 1; 384 } 385 nfsmout: 386 return (error); 387 } 388 389 /* 390 * Initialize the owner/delegation sleep lock. 391 */ 392 APPLESTATIC void 393 nfscl_lockinit(struct nfsv4lock *lckp) 394 { 395 396 lckp->nfslock_usecnt = 0; 397 lckp->nfslock_lock = 0; 398 } 399 400 /* 401 * Get an exclusive lock. (Not needed for OpenBSD4, since there is only one 402 * thread for each posix process in the kernel.) 403 */ 404 APPLESTATIC void 405 nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex) 406 { 407 int igotlock; 408 409 do { 410 igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL); 411 } while (!igotlock); 412 } 413 414 /* 415 * Release an exclusive lock. 416 */ 417 APPLESTATIC void 418 nfscl_lockunlock(struct nfsv4lock *lckp) 419 { 420 421 nfsv4_unlock(lckp, 0); 422 } 423 424 /* 425 * Called to derefernce a lock on a stateid (delegation or open owner). 426 */ 427 APPLESTATIC void 428 nfscl_lockderef(struct nfsv4lock *lckp) 429 { 430 431 NFSLOCKCLSTATE(); 432 lckp->nfslock_usecnt--; 433 if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) { 434 lckp->nfslock_lock &= ~NFSV4LOCK_WANTED; 435 wakeup((caddr_t)lckp); 436 } 437 NFSUNLOCKCLSTATE(); 438 } 439 440