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