1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/6.0/sys/nfsclient/nfs_subs.c 143510 2005-03-13 12:14:56Z jeff $");
37
38 /*
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/stat.h>
56 #include <sys/malloc.h>
57 #include <sys/sysent.h>
58 #include <sys/syscall.h>
59 #include <sys/sysproto.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
64 #include <vm/uma.h>
65
66 #include <rpc/rpcclnt.h>
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsclient/nfs.h>
71 #include <nfsclient/nfsnode.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfsclient/nfsm_subs.h>
74 #include <nfsclient/nfsmount.h>
75
76 #include <netinet/in.h>
77
78 /*
79 * Data items converted to xdr at startup, since they are constant
80 * This is kinda hokey, but may save a little time doing byte swaps
81 */
82 u_int32_t nfs_xdrneg1;
83 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
84 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
85 u_int32_t nfs_true, nfs_false;
86
87 /* And other global data */
88 static u_int32_t nfs_xid = 0;
89 static enum vtype nv2tov_type[8]= {
90 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
91 };
92
93 int nfs_ticks;
94 int nfs_pbuf_freecnt = -1; /* start out unlimited */
95
96 struct nfs_reqq nfs_reqq;
97 struct mtx nfs_reqq_mtx;
98 struct mtx nfs_reply_mtx;
99 struct nfs_bufq nfs_bufq;
100
101 /*
102 * and the reverse mapping from generic to Version 2 procedure numbers
103 */
104 int nfsv2_procid[NFS_NPROCS] = {
105 NFSV2PROC_NULL,
106 NFSV2PROC_GETATTR,
107 NFSV2PROC_SETATTR,
108 NFSV2PROC_LOOKUP,
109 NFSV2PROC_NOOP,
110 NFSV2PROC_READLINK,
111 NFSV2PROC_READ,
112 NFSV2PROC_WRITE,
113 NFSV2PROC_CREATE,
114 NFSV2PROC_MKDIR,
115 NFSV2PROC_SYMLINK,
116 NFSV2PROC_CREATE,
117 NFSV2PROC_REMOVE,
118 NFSV2PROC_RMDIR,
119 NFSV2PROC_RENAME,
120 NFSV2PROC_LINK,
121 NFSV2PROC_READDIR,
122 NFSV2PROC_NOOP,
123 NFSV2PROC_STATFS,
124 NFSV2PROC_NOOP,
125 NFSV2PROC_NOOP,
126 NFSV2PROC_NOOP,
127 NFSV2PROC_NOOP,
128 };
129
130 LIST_HEAD(nfsnodehashhead, nfsnode);
131
132 /*
133 * Create the header for an rpc request packet
134 * The hsiz is the size of the rest of the nfs request header.
135 * (just used to decide if a cluster is a good idea)
136 */
137 struct mbuf *
138 nfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
139 {
140 struct mbuf *mb;
141
142 MGET(mb, M_TRYWAIT, MT_DATA);
143 if (hsiz >= MINCLSIZE)
144 MCLGET(mb, M_TRYWAIT);
145 mb->m_len = 0;
146 return (mb);
147 }
148
149 /*
150 * Build the RPC header and fill in the authorization info.
151 * The authorization string argument is only used when the credentials
152 * come from outside of the kernel.
153 * Returns the head of the mbuf list.
154 */
155 struct mbuf *
156 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
157 int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
158 u_int32_t *xidp)
159 {
160 struct mbuf *mb;
161 u_int32_t *tl;
162 caddr_t bpos;
163 int i;
164 struct mbuf *mreq;
165 int grpsiz, authsiz;
166
167 authsiz = nfsm_rndup(auth_len);
168 MGETHDR(mb, M_TRYWAIT, MT_DATA);
169 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
170 MCLGET(mb, M_TRYWAIT);
171 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
172 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
173 } else {
174 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
175 }
176 mb->m_len = 0;
177 mreq = mb;
178 bpos = mtod(mb, caddr_t);
179
180 /*
181 * First the RPC header.
182 */
183 tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED);
184
185 /* Get a pretty random xid to start with */
186 if (!nfs_xid)
187 nfs_xid = random();
188 /*
189 * Skip zero xid if it should ever happen.
190 */
191 if (++nfs_xid == 0)
192 nfs_xid++;
193
194 *tl++ = *xidp = txdr_unsigned(nfs_xid);
195 *tl++ = rpc_call;
196 *tl++ = rpc_vers;
197 *tl++ = txdr_unsigned(NFS_PROG);
198 if (nmflag & NFSMNT_NFSV3) {
199 *tl++ = txdr_unsigned(NFS_VER3);
200 *tl++ = txdr_unsigned(procid);
201 } else {
202 *tl++ = txdr_unsigned(NFS_VER2);
203 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
204 }
205
206 /*
207 * And then the authorization cred.
208 */
209 *tl++ = txdr_unsigned(auth_type);
210 *tl = txdr_unsigned(authsiz);
211 switch (auth_type) {
212 case RPCAUTH_UNIX:
213 tl = nfsm_build(u_int32_t *, auth_len);
214 *tl++ = 0; /* stamp ?? */
215 *tl++ = 0; /* NULL hostname */
216 *tl++ = txdr_unsigned(cr->cr_uid);
217 *tl++ = txdr_unsigned(cr->cr_groups[0]);
218 grpsiz = (auth_len >> 2) - 5;
219 *tl++ = txdr_unsigned(grpsiz);
220 for (i = 1; i <= grpsiz; i++)
221 *tl++ = txdr_unsigned(cr->cr_groups[i]);
222 break;
223 }
224
225 /*
226 * And the verifier...
227 */
228 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
229 *tl++ = txdr_unsigned(RPCAUTH_NULL);
230 *tl = 0;
231 mb->m_next = mrest;
232 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
233 mreq->m_pkthdr.rcvif = NULL;
234 *mbp = mb;
235 return (mreq);
236 }
237
238 /*
239 * copies a uio scatter/gather list to an mbuf chain.
240 * NOTE: can ony handle iovcnt == 1
241 */
242 int
243 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
244 {
245 char *uiocp;
246 struct mbuf *mp, *mp2;
247 int xfer, left, mlen;
248 int uiosiz, clflg, rem;
249 char *cp;
250
251 #ifdef DIAGNOSTIC
252 if (uiop->uio_iovcnt != 1)
253 panic("nfsm_uiotombuf: iovcnt != 1");
254 #endif
255
256 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
257 clflg = 1;
258 else
259 clflg = 0;
260 rem = nfsm_rndup(siz)-siz;
261 mp = mp2 = *mq;
262 while (siz > 0) {
263 left = uiop->uio_iov->iov_len;
264 uiocp = uiop->uio_iov->iov_base;
265 if (left > siz)
266 left = siz;
267 uiosiz = left;
268 while (left > 0) {
269 mlen = M_TRAILINGSPACE(mp);
270 if (mlen == 0) {
271 MGET(mp, M_TRYWAIT, MT_DATA);
272 if (clflg)
273 MCLGET(mp, M_TRYWAIT);
274 mp->m_len = 0;
275 mp2->m_next = mp;
276 mp2 = mp;
277 mlen = M_TRAILINGSPACE(mp);
278 }
279 xfer = (left > mlen) ? mlen : left;
280 #ifdef notdef
281 /* Not Yet.. */
282 if (uiop->uio_iov->iov_op != NULL)
283 (*(uiop->uio_iov->iov_op))
284 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
285 else
286 #endif
287 if (uiop->uio_segflg == UIO_SYSSPACE)
288 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
289 else
290 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
291 mp->m_len += xfer;
292 left -= xfer;
293 uiocp += xfer;
294 uiop->uio_offset += xfer;
295 uiop->uio_resid -= xfer;
296 }
297 uiop->uio_iov->iov_base =
298 (char *)uiop->uio_iov->iov_base + uiosiz;
299 uiop->uio_iov->iov_len -= uiosiz;
300 siz -= uiosiz;
301 }
302 if (rem > 0) {
303 if (rem > M_TRAILINGSPACE(mp)) {
304 MGET(mp, M_TRYWAIT, MT_DATA);
305 mp->m_len = 0;
306 mp2->m_next = mp;
307 }
308 cp = mtod(mp, caddr_t)+mp->m_len;
309 for (left = 0; left < rem; left++)
310 *cp++ = '\0';
311 mp->m_len += rem;
312 *bpos = cp;
313 } else
314 *bpos = mtod(mp, caddr_t)+mp->m_len;
315 *mq = mp;
316 return (0);
317 }
318
319 /*
320 * Copy a string into mbufs for the hard cases...
321 */
322 int
323 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
324 {
325 struct mbuf *m1 = NULL, *m2;
326 long left, xfer, len, tlen;
327 u_int32_t *tl;
328 int putsize;
329
330 putsize = 1;
331 m2 = *mb;
332 left = M_TRAILINGSPACE(m2);
333 if (left > 0) {
334 tl = ((u_int32_t *)(*bpos));
335 *tl++ = txdr_unsigned(siz);
336 putsize = 0;
337 left -= NFSX_UNSIGNED;
338 m2->m_len += NFSX_UNSIGNED;
339 if (left > 0) {
340 bcopy(cp, (caddr_t) tl, left);
341 siz -= left;
342 cp += left;
343 m2->m_len += left;
344 left = 0;
345 }
346 }
347 /* Loop around adding mbufs */
348 while (siz > 0) {
349 MGET(m1, M_TRYWAIT, MT_DATA);
350 if (siz > MLEN)
351 MCLGET(m1, M_TRYWAIT);
352 m1->m_len = NFSMSIZ(m1);
353 m2->m_next = m1;
354 m2 = m1;
355 tl = mtod(m1, u_int32_t *);
356 tlen = 0;
357 if (putsize) {
358 *tl++ = txdr_unsigned(siz);
359 m1->m_len -= NFSX_UNSIGNED;
360 tlen = NFSX_UNSIGNED;
361 putsize = 0;
362 }
363 if (siz < m1->m_len) {
364 len = nfsm_rndup(siz);
365 xfer = siz;
366 if (xfer < len)
367 *(tl+(xfer>>2)) = 0;
368 } else {
369 xfer = len = m1->m_len;
370 }
371 bcopy(cp, (caddr_t) tl, xfer);
372 m1->m_len = len+tlen;
373 siz -= xfer;
374 cp += xfer;
375 }
376 *mb = m1;
377 *bpos = mtod(m1, caddr_t)+m1->m_len;
378 return (0);
379 }
380
381 /*
382 * Called once to initialize data structures...
383 */
384 int
385 nfs_init(struct vfsconf *vfsp)
386 {
387 int i;
388
389 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
390 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
391 rpc_vers = txdr_unsigned(RPC_VER2);
392 rpc_call = txdr_unsigned(RPC_CALL);
393 rpc_reply = txdr_unsigned(RPC_REPLY);
394 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
395 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
396 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
397 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
398 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
399 nfs_true = txdr_unsigned(TRUE);
400 nfs_false = txdr_unsigned(FALSE);
401 nfs_xdrneg1 = txdr_unsigned(-1);
402 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
403 if (nfs_ticks < 1)
404 nfs_ticks = 1;
405 /* Ensure async daemons disabled */
406 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
407 nfs_iodwant[i] = NULL;
408 nfs_iodmount[i] = NULL;
409 }
410 nfs_nhinit(); /* Init the nfsnode table */
411
412 /*
413 * Initialize reply list and start timer
414 */
415 TAILQ_INIT(&nfs_reqq);
416 callout_init(&nfs_callout, 0);
417 mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF);
418 mtx_init(&nfs_reply_mtx, "Synch NFS reply posting", NULL, MTX_DEF);
419
420 nfs_pbuf_freecnt = nswbuf / 2 + 1;
421
422 return (0);
423 }
424
425 int
426 nfs_uninit(struct vfsconf *vfsp)
427 {
428 int i;
429
430 callout_stop(&nfs_callout);
431
432 KASSERT(TAILQ_EMPTY(&nfs_reqq),
433 ("nfs_uninit: request queue not empty"));
434
435 /*
436 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
437 * any sleeping nfsiods so they check nfs_iodmax and exit.
438 */
439 nfs_iodmax = 0;
440 for (i = 0; i < nfs_numasync; i++)
441 if (nfs_iodwant[i])
442 wakeup(&nfs_iodwant[i]);
443 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
444 while (nfs_numasync)
445 tsleep(&nfs_numasync, PWAIT, "ioddie", 0);
446
447 nfs_nhuninit();
448 uma_zdestroy(nfsmount_zone);
449 return (0);
450 }
451
452 /*
453 * Attribute cache routines.
454 * nfs_loadattrcache() - loads or updates the cache contents from attributes
455 * that are on the mbuf list
456 * nfs_getattrcache() - returns valid attributes if found in cache, returns
457 * error otherwise
458 */
459
460 /*
461 * Load the attribute cache (that lives in the nfsnode entry) with
462 * the values on the mbuf list and
463 * Iff vap not NULL
464 * copy the attributes to *vaper
465 */
466 int
467 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
468 struct vattr *vaper, int dontshrink)
469 {
470 struct vnode *vp = *vpp;
471 struct vattr *vap;
472 struct nfs_fattr *fp;
473 struct nfsnode *np;
474 int32_t t1;
475 caddr_t cp2;
476 int rdev;
477 struct mbuf *md;
478 enum vtype vtyp;
479 u_short vmode;
480 struct timespec mtime;
481 int v3 = NFS_ISV3(vp);
482
483 md = *mdp;
484 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
485 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_TRYWAIT);
486 if (cp2 == NULL)
487 return EBADRPC;
488 fp = (struct nfs_fattr *)cp2;
489 if (v3) {
490 vtyp = nfsv3tov_type(fp->fa_type);
491 vmode = fxdr_unsigned(u_short, fp->fa_mode);
492 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
493 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
494 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
495 } else {
496 vtyp = nfsv2tov_type(fp->fa_type);
497 vmode = fxdr_unsigned(u_short, fp->fa_mode);
498 /*
499 * XXX
500 *
501 * The duplicate information returned in fa_type and fa_mode
502 * is an ambiguity in the NFS version 2 protocol.
503 *
504 * VREG should be taken literally as a regular file. If a
505 * server intents to return some type information differently
506 * in the upper bits of the mode field (e.g. for sockets, or
507 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
508 * leave the examination of the mode bits even in the VREG
509 * case to avoid breakage for bogus servers, but we make sure
510 * that there are actually type bits set in the upper part of
511 * fa_mode (and failing that, trust the va_type field).
512 *
513 * NFSv3 cleared the issue, and requires fa_mode to not
514 * contain any type information (while also introduing sockets
515 * and FIFOs for fa_type).
516 */
517 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
518 vtyp = IFTOVT(vmode);
519 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
520 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
521
522 /*
523 * Really ugly NFSv2 kludge.
524 */
525 if (vtyp == VCHR && rdev == 0xffffffff)
526 vtyp = VFIFO;
527 }
528
529 /*
530 * If v_type == VNON it is a new node, so fill in the v_type,
531 * n_mtime fields. Check to see if it represents a special
532 * device, and if so, check for a possible alias. Once the
533 * correct vnode has been obtained, fill in the rest of the
534 * information.
535 */
536 np = VTONFS(vp);
537 if (vp->v_type != vtyp) {
538 vp->v_type = vtyp;
539 if (vp->v_type == VFIFO)
540 vp->v_op = &nfs_fifoops;
541 np->n_mtime = mtime;
542 }
543 vap = &np->n_vattr;
544 vap->va_type = vtyp;
545 vap->va_mode = (vmode & 07777);
546 vap->va_rdev = rdev;
547 vap->va_mtime = mtime;
548 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
549 if (v3) {
550 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
551 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
552 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
553 vap->va_size = fxdr_hyper(&fp->fa3_size);
554 vap->va_blocksize = NFS_FABLKSIZE;
555 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
556 vap->va_fileid = fxdr_unsigned(int32_t,
557 fp->fa3_fileid.nfsuquad[1]);
558 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
559 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
560 vap->va_flags = 0;
561 vap->va_filerev = 0;
562 } else {
563 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
564 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
565 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
566 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
567 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
568 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
569 * NFS_FABLKSIZE;
570 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
571 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
572 vap->va_flags = 0;
573 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
574 fp->fa2_ctime.nfsv2_sec);
575 vap->va_ctime.tv_nsec = 0;
576 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
577 vap->va_filerev = 0;
578 }
579 np->n_attrstamp = time_second;
580 if (vap->va_size != np->n_size) {
581 if (vap->va_type == VREG) {
582 if (dontshrink && vap->va_size < np->n_size) {
583 /*
584 * We've been told not to shrink the file;
585 * zero np->n_attrstamp to indicate that
586 * the attributes are stale.
587 */
588 vap->va_size = np->n_size;
589 np->n_attrstamp = 0;
590 } else if (np->n_flag & NMODIFIED) {
591 /*
592 * We've modified the file: Use the larger
593 * of our size, and the server's size.
594 */
595 if (vap->va_size < np->n_size) {
596 vap->va_size = np->n_size;
597 } else {
598 np->n_size = vap->va_size;
599 np->n_flag |= NSIZECHANGED;
600 }
601 } else {
602 np->n_size = vap->va_size;
603 np->n_flag |= NSIZECHANGED;
604 }
605 vnode_pager_setsize(vp, np->n_size);
606 } else {
607 np->n_size = vap->va_size;
608 }
609 }
610 if (vaper != NULL) {
611 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
612 if (np->n_flag & NCHG) {
613 if (np->n_flag & NACC)
614 vaper->va_atime = np->n_atim;
615 if (np->n_flag & NUPD)
616 vaper->va_mtime = np->n_mtim;
617 }
618 }
619 return (0);
620 }
621
622 #ifdef NFS_ACDEBUG
623 #include <sys/sysctl.h>
624 SYSCTL_DECL(_vfs_nfs);
625 static int nfs_acdebug;
626 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
627 #endif
628
629 /*
630 * Check the time stamp
631 * If the cache is valid, copy contents to *vap and return 0
632 * otherwise return an error
633 */
634 int
635 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
636 {
637 struct nfsnode *np;
638 struct vattr *vap;
639 struct nfsmount *nmp;
640 int timeo;
641
642 np = VTONFS(vp);
643 vap = &np->n_vattr;
644 nmp = VFSTONFS(vp->v_mount);
645 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
646 timeo = (time_second - np->n_mtime.tv_sec) / 10;
647
648 #ifdef NFS_ACDEBUG
649 if (nfs_acdebug>1)
650 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
651 #endif
652
653 if (vap->va_type == VDIR) {
654 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
655 timeo = nmp->nm_acdirmin;
656 else if (timeo > nmp->nm_acdirmax)
657 timeo = nmp->nm_acdirmax;
658 } else {
659 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
660 timeo = nmp->nm_acregmin;
661 else if (timeo > nmp->nm_acregmax)
662 timeo = nmp->nm_acregmax;
663 }
664
665 #ifdef NFS_ACDEBUG
666 if (nfs_acdebug > 2)
667 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
668 nmp->nm_acregmin, nmp->nm_acregmax,
669 nmp->nm_acdirmin, nmp->nm_acdirmax);
670
671 if (nfs_acdebug)
672 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
673 (time_second - np->n_attrstamp), timeo);
674 #endif
675
676 if ((time_second - np->n_attrstamp) >= timeo) {
677 nfsstats.attrcache_misses++;
678 return (ENOENT);
679 }
680 nfsstats.attrcache_hits++;
681 if (vap->va_size != np->n_size) {
682 if (vap->va_type == VREG) {
683 if (np->n_flag & NMODIFIED) {
684 if (vap->va_size < np->n_size)
685 vap->va_size = np->n_size;
686 else
687 np->n_size = vap->va_size;
688 } else {
689 np->n_size = vap->va_size;
690 }
691 vnode_pager_setsize(vp, np->n_size);
692 } else {
693 np->n_size = vap->va_size;
694 }
695 }
696 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
697 if (np->n_flag & NCHG) {
698 if (np->n_flag & NACC)
699 vaper->va_atime = np->n_atim;
700 if (np->n_flag & NUPD)
701 vaper->va_mtime = np->n_mtim;
702 }
703 return (0);
704 }
705
706 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
707 /*
708 * This function finds the directory cookie that corresponds to the
709 * logical byte offset given.
710 */
711 nfsuint64 *
712 nfs_getcookie(struct nfsnode *np, off_t off, int add)
713 {
714 struct nfsdmap *dp, *dp2;
715 int pos;
716
717 pos = (uoff_t)off / NFS_DIRBLKSIZ;
718 if (pos == 0 || off < 0) {
719 #ifdef DIAGNOSTIC
720 if (add)
721 panic("nfs getcookie add at <= 0");
722 #endif
723 return (&nfs_nullcookie);
724 }
725 pos--;
726 dp = LIST_FIRST(&np->n_cookies);
727 if (!dp) {
728 if (add) {
729 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
730 M_NFSDIROFF, M_WAITOK);
731 dp->ndm_eocookie = 0;
732 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
733 } else
734 return (NULL);
735 }
736 while (pos >= NFSNUMCOOKIES) {
737 pos -= NFSNUMCOOKIES;
738 if (LIST_NEXT(dp, ndm_list)) {
739 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
740 pos >= dp->ndm_eocookie)
741 return (NULL);
742 dp = LIST_NEXT(dp, ndm_list);
743 } else if (add) {
744 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
745 M_NFSDIROFF, M_WAITOK);
746 dp2->ndm_eocookie = 0;
747 LIST_INSERT_AFTER(dp, dp2, ndm_list);
748 dp = dp2;
749 } else
750 return (NULL);
751 }
752 if (pos >= dp->ndm_eocookie) {
753 if (add)
754 dp->ndm_eocookie = pos + 1;
755 else
756 return (NULL);
757 }
758 return (&dp->ndm_cookies[pos]);
759 }
760
761 /*
762 * Invalidate cached directory information, except for the actual directory
763 * blocks (which are invalidated separately).
764 * Done mainly to avoid the use of stale offset cookies.
765 */
766 void
767 nfs_invaldir(struct vnode *vp)
768 {
769 struct nfsnode *np = VTONFS(vp);
770
771 #ifdef DIAGNOSTIC
772 if (vp->v_type != VDIR)
773 panic("nfs: invaldir not dir");
774 #endif
775 np->n_direofoffset = 0;
776 np->n_cookieverf.nfsuquad[0] = 0;
777 np->n_cookieverf.nfsuquad[1] = 0;
778 if (LIST_FIRST(&np->n_cookies))
779 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
780 }
781
782 /*
783 * The write verifier has changed (probably due to a server reboot), so all
784 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
785 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
786 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
787 * mount point.
788 *
789 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
790 * writes are not clusterable.
791 */
792 void
793 nfs_clearcommit(struct mount *mp)
794 {
795 struct vnode *vp, *nvp;
796 struct buf *bp, *nbp;
797 int s;
798
799 GIANT_REQUIRED;
800
801 s = splbio();
802 MNT_ILOCK(mp);
803 MNT_VNODE_FOREACH(vp, mp, nvp) {
804 VI_LOCK(vp);
805 if (vp->v_iflag & VI_DOOMED) {
806 VI_UNLOCK(vp);
807 continue;
808 }
809 MNT_IUNLOCK(mp);
810 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
811 if (BUF_REFCNT(bp) == 0 &&
812 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
813 == (B_DELWRI | B_NEEDCOMMIT))
814 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
815 }
816 VI_UNLOCK(vp);
817 MNT_ILOCK(mp);
818 }
819 MNT_IUNLOCK(mp);
820 splx(s);
821 }
822
823 /*
824 * Helper functions for former macros. Some of these should be
825 * moved to their callers.
826 */
827
828 int
829 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
830 struct mbuf **md, caddr_t *dpos)
831 {
832 struct nfsnode *ttnp;
833 struct vnode *ttvp;
834 nfsfh_t *ttfhp;
835 u_int32_t *tl;
836 int ttfhsize;
837 int t1;
838
839 if (v3) {
840 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
841 if (tl == NULL)
842 return EBADRPC;
843 *f = fxdr_unsigned(int, *tl);
844 } else
845 *f = 1;
846 if (*f) {
847 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
848 if (t1 != 0)
849 return t1;
850 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp);
851 if (t1 != 0)
852 return t1;
853 *v = NFSTOV(ttnp);
854 }
855 if (v3) {
856 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
857 if (tl == NULL)
858 return EBADRPC;
859 if (*f)
860 *f = fxdr_unsigned(int, *tl);
861 else if (fxdr_unsigned(int, *tl))
862 nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
863 }
864 if (*f) {
865 ttvp = *v;
866 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
867 if (t1)
868 return t1;
869 *v = ttvp;
870 }
871 return 0;
872 }
873
874 int
875 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
876 {
877 u_int32_t *tl;
878
879 if (v3) {
880 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
881 if (tl == NULL)
882 return EBADRPC;
883 *s = fxdr_unsigned(int, *tl);
884 if (*s <= 0 || *s > NFSX_V3FHMAX)
885 return EBADRPC;
886 } else
887 *s = NFSX_V2FH;
888 *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
889 if (*f == NULL)
890 return EBADRPC;
891 else
892 return 0;
893 }
894
895
896 int
897 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
898 caddr_t *dpos)
899 {
900 int t1;
901
902 struct vnode *ttvp = *v;
903 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
904 if (t1 != 0)
905 return t1;
906 *v = ttvp;
907 return 0;
908 }
909
910 int
911 nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
912 caddr_t *dpos)
913 {
914 u_int32_t *tl;
915 int t1;
916
917 struct vnode *ttvp = *v;
918 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
919 if (tl == NULL)
920 return EBADRPC;
921 *f = fxdr_unsigned(int, *tl);
922 if (*f != 0) {
923 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
924 if (t1 != 0) {
925 *f = 0;
926 return t1;
927 }
928 *v = ttvp;
929 }
930 return 0;
931 }
932
933 int
934 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
935 {
936 u_int32_t *tl;
937 int ttattrf, ttretf = 0;
938 int t1;
939
940 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
941 if (tl == NULL)
942 return EBADRPC;
943 if (*tl == nfs_true) {
944 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
945 if (tl == NULL)
946 return EBADRPC;
947 if (*f)
948 ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
949 VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
950 }
951 t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
952 if (t1)
953 return t1;
954 if (*f)
955 *f = ttretf;
956 else
957 *f = ttattrf;
958 return 0;
959 }
960
961 int
962 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
963 {
964 u_int32_t *tl;
965 int t1;
966
967 if (s > m)
968 return ENAMETOOLONG;
969 t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
970 if (t1 <= M_TRAILINGSPACE(*mb)) {
971 tl = nfsm_build_xx(t1, mb, bpos);
972 *tl++ = txdr_unsigned(s);
973 *(tl + ((t1 >> 2) - 2)) = 0;
974 bcopy(a, tl, s);
975 } else {
976 t1 = nfsm_strtmbuf(mb, bpos, a, s);
977 if (t1 != 0)
978 return t1;
979 }
980 return 0;
981 }
982
983 int
984 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
985 {
986 u_int32_t *tl;
987 int t1;
988 caddr_t cp;
989
990 if (v3) {
991 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
992 if (t1 < M_TRAILINGSPACE(*mb)) {
993 tl = nfsm_build_xx(t1, mb, bpos);
994 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
995 *(tl + ((t1 >> 2) - 2)) = 0;
996 bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
997 } else {
998 t1 = nfsm_strtmbuf(mb, bpos,
999 (const char *)VTONFS(v)->n_fhp,
1000 VTONFS(v)->n_fhsize);
1001 if (t1 != 0)
1002 return t1;
1003 }
1004 } else {
1005 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1006 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1007 }
1008 return 0;
1009 }
1010
1011 void
1012 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1013 caddr_t *bpos)
1014 {
1015 u_int32_t *tl;
1016
1017 if (va->va_mode != (mode_t)VNOVAL) {
1018 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1019 *tl++ = nfs_true;
1020 *tl = txdr_unsigned(va->va_mode);
1021 } else {
1022 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1023 *tl = nfs_false;
1024 }
1025 if (full && va->va_uid != (uid_t)VNOVAL) {
1026 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1027 *tl++ = nfs_true;
1028 *tl = txdr_unsigned(va->va_uid);
1029 } else {
1030 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1031 *tl = nfs_false;
1032 }
1033 if (full && va->va_gid != (gid_t)VNOVAL) {
1034 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1035 *tl++ = nfs_true;
1036 *tl = txdr_unsigned(va->va_gid);
1037 } else {
1038 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1039 *tl = nfs_false;
1040 }
1041 if (full && va->va_size != VNOVAL) {
1042 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1043 *tl++ = nfs_true;
1044 txdr_hyper(va->va_size, tl);
1045 } else {
1046 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1047 *tl = nfs_false;
1048 }
1049 if (va->va_atime.tv_sec != VNOVAL) {
1050 if (va->va_atime.tv_sec != time_second) {
1051 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1052 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1053 txdr_nfsv3time(&va->va_atime, tl);
1054 } else {
1055 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1056 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1057 }
1058 } else {
1059 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1060 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1061 }
1062 if (va->va_mtime.tv_sec != VNOVAL) {
1063 if (va->va_mtime.tv_sec != time_second) {
1064 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1065 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1066 txdr_nfsv3time(&va->va_mtime, tl);
1067 } else {
1068 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1069 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1070 }
1071 } else {
1072 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1073 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1074 }
1075 }
Cache object: b1981a331a257dbe1763c84e99a94f1c
|