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.3/sys/nfsclient/nfs_subs.c 172724 2007-10-17 16:07:10Z jhb $");
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 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 **xidpp)
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 *xidpp = tl;
195 *tl++ = txdr_unsigned(nfs_xid);
196 *tl++ = rpc_call;
197 *tl++ = rpc_vers;
198 *tl++ = txdr_unsigned(NFS_PROG);
199 if (nmflag & NFSMNT_NFSV3) {
200 *tl++ = txdr_unsigned(NFS_VER3);
201 *tl++ = txdr_unsigned(procid);
202 } else {
203 *tl++ = txdr_unsigned(NFS_VER2);
204 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
205 }
206
207 /*
208 * And then the authorization cred.
209 */
210 *tl++ = txdr_unsigned(auth_type);
211 *tl = txdr_unsigned(authsiz);
212 switch (auth_type) {
213 case RPCAUTH_UNIX:
214 tl = nfsm_build(u_int32_t *, auth_len);
215 *tl++ = 0; /* stamp ?? */
216 *tl++ = 0; /* NULL hostname */
217 *tl++ = txdr_unsigned(cr->cr_uid);
218 *tl++ = txdr_unsigned(cr->cr_groups[0]);
219 grpsiz = (auth_len >> 2) - 5;
220 *tl++ = txdr_unsigned(grpsiz);
221 for (i = 1; i <= grpsiz; i++)
222 *tl++ = txdr_unsigned(cr->cr_groups[i]);
223 break;
224 }
225
226 /*
227 * And the verifier...
228 */
229 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
230 *tl++ = txdr_unsigned(RPCAUTH_NULL);
231 *tl = 0;
232 mb->m_next = mrest;
233 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
234 mreq->m_pkthdr.rcvif = NULL;
235 *mbp = mb;
236 return (mreq);
237 }
238
239 /*
240 * copies a uio scatter/gather list to an mbuf chain.
241 * NOTE: can ony handle iovcnt == 1
242 */
243 int
244 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
245 {
246 char *uiocp;
247 struct mbuf *mp, *mp2;
248 int xfer, left, mlen;
249 int uiosiz, clflg, rem;
250 char *cp;
251
252 #ifdef DIAGNOSTIC
253 if (uiop->uio_iovcnt != 1)
254 panic("nfsm_uiotombuf: iovcnt != 1");
255 #endif
256
257 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
258 clflg = 1;
259 else
260 clflg = 0;
261 rem = nfsm_rndup(siz)-siz;
262 mp = mp2 = *mq;
263 while (siz > 0) {
264 left = uiop->uio_iov->iov_len;
265 uiocp = uiop->uio_iov->iov_base;
266 if (left > siz)
267 left = siz;
268 uiosiz = left;
269 while (left > 0) {
270 mlen = M_TRAILINGSPACE(mp);
271 if (mlen == 0) {
272 MGET(mp, M_TRYWAIT, MT_DATA);
273 if (clflg)
274 MCLGET(mp, M_TRYWAIT);
275 mp->m_len = 0;
276 mp2->m_next = mp;
277 mp2 = mp;
278 mlen = M_TRAILINGSPACE(mp);
279 }
280 xfer = (left > mlen) ? mlen : left;
281 #ifdef notdef
282 /* Not Yet.. */
283 if (uiop->uio_iov->iov_op != NULL)
284 (*(uiop->uio_iov->iov_op))
285 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
286 else
287 #endif
288 if (uiop->uio_segflg == UIO_SYSSPACE)
289 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
290 else
291 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
292 mp->m_len += xfer;
293 left -= xfer;
294 uiocp += xfer;
295 uiop->uio_offset += xfer;
296 uiop->uio_resid -= xfer;
297 }
298 uiop->uio_iov->iov_base =
299 (char *)uiop->uio_iov->iov_base + uiosiz;
300 uiop->uio_iov->iov_len -= uiosiz;
301 siz -= uiosiz;
302 }
303 if (rem > 0) {
304 if (rem > M_TRAILINGSPACE(mp)) {
305 MGET(mp, M_TRYWAIT, MT_DATA);
306 mp->m_len = 0;
307 mp2->m_next = mp;
308 }
309 cp = mtod(mp, caddr_t)+mp->m_len;
310 for (left = 0; left < rem; left++)
311 *cp++ = '\0';
312 mp->m_len += rem;
313 *bpos = cp;
314 } else
315 *bpos = mtod(mp, caddr_t)+mp->m_len;
316 *mq = mp;
317 return (0);
318 }
319
320 /*
321 * Copy a string into mbufs for the hard cases...
322 */
323 int
324 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
325 {
326 struct mbuf *m1 = NULL, *m2;
327 long left, xfer, len, tlen;
328 u_int32_t *tl;
329 int putsize;
330
331 putsize = 1;
332 m2 = *mb;
333 left = M_TRAILINGSPACE(m2);
334 if (left > 0) {
335 tl = ((u_int32_t *)(*bpos));
336 *tl++ = txdr_unsigned(siz);
337 putsize = 0;
338 left -= NFSX_UNSIGNED;
339 m2->m_len += NFSX_UNSIGNED;
340 if (left > 0) {
341 bcopy(cp, (caddr_t) tl, left);
342 siz -= left;
343 cp += left;
344 m2->m_len += left;
345 left = 0;
346 }
347 }
348 /* Loop around adding mbufs */
349 while (siz > 0) {
350 MGET(m1, M_TRYWAIT, MT_DATA);
351 if (siz > MLEN)
352 MCLGET(m1, M_TRYWAIT);
353 m1->m_len = NFSMSIZ(m1);
354 m2->m_next = m1;
355 m2 = m1;
356 tl = mtod(m1, u_int32_t *);
357 tlen = 0;
358 if (putsize) {
359 *tl++ = txdr_unsigned(siz);
360 m1->m_len -= NFSX_UNSIGNED;
361 tlen = NFSX_UNSIGNED;
362 putsize = 0;
363 }
364 if (siz < m1->m_len) {
365 len = nfsm_rndup(siz);
366 xfer = siz;
367 if (xfer < len)
368 *(tl+(xfer>>2)) = 0;
369 } else {
370 xfer = len = m1->m_len;
371 }
372 bcopy(cp, (caddr_t) tl, xfer);
373 m1->m_len = len+tlen;
374 siz -= xfer;
375 cp += xfer;
376 }
377 *mb = m1;
378 *bpos = mtod(m1, caddr_t)+m1->m_len;
379 return (0);
380 }
381
382 /*
383 * Called once to initialize data structures...
384 */
385 int
386 nfs_init(struct vfsconf *vfsp)
387 {
388 int i;
389
390 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
391 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
392 rpc_vers = txdr_unsigned(RPC_VER2);
393 rpc_call = txdr_unsigned(RPC_CALL);
394 rpc_reply = txdr_unsigned(RPC_REPLY);
395 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
396 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
397 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
398 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
399 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
400 nfs_true = txdr_unsigned(TRUE);
401 nfs_false = txdr_unsigned(FALSE);
402 nfs_xdrneg1 = txdr_unsigned(-1);
403 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
404 if (nfs_ticks < 1)
405 nfs_ticks = 1;
406 /* Ensure async daemons disabled */
407 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
408 nfs_iodwant[i] = NULL;
409 nfs_iodmount[i] = NULL;
410 }
411 nfs_nhinit(); /* Init the nfsnode table */
412
413 /*
414 * Initialize reply list and start timer
415 */
416 TAILQ_INIT(&nfs_reqq);
417 callout_init(&nfs_callout, 0);
418 mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF);
419 mtx_init(&nfs_reply_mtx, "Synch NFS reply posting", NULL, MTX_DEF);
420
421 nfs_pbuf_freecnt = nswbuf / 2 + 1;
422
423 return (0);
424 }
425
426 int
427 nfs_uninit(struct vfsconf *vfsp)
428 {
429 int i;
430
431 callout_stop(&nfs_callout);
432
433 KASSERT(TAILQ_EMPTY(&nfs_reqq),
434 ("nfs_uninit: request queue not empty"));
435
436 /*
437 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
438 * any sleeping nfsiods so they check nfs_iodmax and exit.
439 */
440 nfs_iodmax = 0;
441 for (i = 0; i < nfs_numasync; i++)
442 if (nfs_iodwant[i])
443 wakeup(&nfs_iodwant[i]);
444 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
445 while (nfs_numasync)
446 tsleep(&nfs_numasync, PWAIT, "ioddie", 0);
447
448 nfs_nhuninit();
449 uma_zdestroy(nfsmount_zone);
450 return (0);
451 }
452
453 /*
454 * Attribute cache routines.
455 * nfs_loadattrcache() - loads or updates the cache contents from attributes
456 * that are on the mbuf list
457 * nfs_getattrcache() - returns valid attributes if found in cache, returns
458 * error otherwise
459 */
460
461 /*
462 * Load the attribute cache (that lives in the nfsnode entry) with
463 * the values on the mbuf list and
464 * Iff vap not NULL
465 * copy the attributes to *vaper
466 */
467 int
468 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
469 struct vattr *vaper, int dontshrink)
470 {
471 struct vnode *vp = *vpp;
472 struct vattr *vap;
473 struct nfs_fattr *fp;
474 struct nfsnode *np;
475 int32_t t1;
476 caddr_t cp2;
477 int rdev;
478 struct mbuf *md;
479 enum vtype vtyp;
480 u_short vmode;
481 struct timespec mtime, mtime_save;
482 int v3 = NFS_ISV3(vp);
483
484 md = *mdp;
485 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
486 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_TRYWAIT);
487 if (cp2 == NULL)
488 return EBADRPC;
489 fp = (struct nfs_fattr *)cp2;
490 if (v3) {
491 vtyp = nfsv3tov_type(fp->fa_type);
492 vmode = fxdr_unsigned(u_short, fp->fa_mode);
493 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
494 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
495 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
496 } else {
497 vtyp = nfsv2tov_type(fp->fa_type);
498 vmode = fxdr_unsigned(u_short, fp->fa_mode);
499 /*
500 * XXX
501 *
502 * The duplicate information returned in fa_type and fa_mode
503 * is an ambiguity in the NFS version 2 protocol.
504 *
505 * VREG should be taken literally as a regular file. If a
506 * server intents to return some type information differently
507 * in the upper bits of the mode field (e.g. for sockets, or
508 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
509 * leave the examination of the mode bits even in the VREG
510 * case to avoid breakage for bogus servers, but we make sure
511 * that there are actually type bits set in the upper part of
512 * fa_mode (and failing that, trust the va_type field).
513 *
514 * NFSv3 cleared the issue, and requires fa_mode to not
515 * contain any type information (while also introduing sockets
516 * and FIFOs for fa_type).
517 */
518 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
519 vtyp = IFTOVT(vmode);
520 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
521 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
522
523 /*
524 * Really ugly NFSv2 kludge.
525 */
526 if (vtyp == VCHR && rdev == 0xffffffff)
527 vtyp = VFIFO;
528 }
529
530 /*
531 * If v_type == VNON it is a new node, so fill in the v_type,
532 * n_mtime fields. Check to see if it represents a special
533 * device, and if so, check for a possible alias. Once the
534 * correct vnode has been obtained, fill in the rest of the
535 * information.
536 */
537 np = VTONFS(vp);
538 if (vp->v_type != vtyp) {
539 vp->v_type = vtyp;
540 if (vp->v_type == VFIFO)
541 vp->v_op = &nfs_fifoops;
542 np->n_mtime = mtime;
543 }
544 vap = &np->n_vattr;
545 vap->va_type = vtyp;
546 vap->va_mode = (vmode & 07777);
547 vap->va_rdev = rdev;
548 mtime_save = vap->va_mtime;
549 vap->va_mtime = mtime;
550 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
551 if (v3) {
552 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
553 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
554 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
555 vap->va_size = fxdr_hyper(&fp->fa3_size);
556 vap->va_blocksize = NFS_FABLKSIZE;
557 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
558 vap->va_fileid = fxdr_unsigned(int32_t,
559 fp->fa3_fileid.nfsuquad[1]);
560 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
561 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
562 vap->va_flags = 0;
563 vap->va_filerev = 0;
564 } else {
565 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
566 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
567 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
568 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
569 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
570 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
571 * NFS_FABLKSIZE;
572 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
573 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
574 vap->va_flags = 0;
575 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
576 fp->fa2_ctime.nfsv2_sec);
577 vap->va_ctime.tv_nsec = 0;
578 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
579 vap->va_filerev = 0;
580 }
581 np->n_attrstamp = time_second;
582 if (vap->va_size != np->n_size) {
583 if (vap->va_type == VREG) {
584 if (dontshrink && vap->va_size < np->n_size) {
585 /*
586 * We've been told not to shrink the file;
587 * zero np->n_attrstamp to indicate that
588 * the attributes are stale.
589 */
590 vap->va_size = np->n_size;
591 np->n_attrstamp = 0;
592 } else if (np->n_flag & NMODIFIED) {
593 /*
594 * We've modified the file: Use the larger
595 * of our size, and the server's size.
596 */
597 if (vap->va_size < np->n_size) {
598 vap->va_size = np->n_size;
599 } else {
600 np->n_size = vap->va_size;
601 np->n_flag |= NSIZECHANGED;
602 }
603 } else {
604 np->n_size = vap->va_size;
605 np->n_flag |= NSIZECHANGED;
606 }
607 vnode_pager_setsize(vp, np->n_size);
608 } else {
609 np->n_size = vap->va_size;
610 }
611 }
612 /*
613 * The following checks are added to prevent a race between (say)
614 * a READDIR+ and a WRITE.
615 * READDIR+, WRITE requests sent out.
616 * READDIR+ resp, WRITE resp received on client.
617 * However, the WRITE resp was handled before the READDIR+ resp
618 * causing the post op attrs from the write to be loaded first
619 * and the attrs from the READDIR+ to be loaded later. If this
620 * happens, we have stale attrs loaded into the attrcache.
621 * We detect this by for the mtime moving back. We invalidate the
622 * attrcache when this happens.
623 */
624 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
625 /* Size changed or mtime went backwards */
626 np->n_attrstamp = 0;
627 if (vaper != NULL) {
628 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
629 if (np->n_flag & NCHG) {
630 if (np->n_flag & NACC)
631 vaper->va_atime = np->n_atim;
632 if (np->n_flag & NUPD)
633 vaper->va_mtime = np->n_mtim;
634 }
635 }
636 return (0);
637 }
638
639 #ifdef NFS_ACDEBUG
640 #include <sys/sysctl.h>
641 SYSCTL_DECL(_vfs_nfs);
642 static int nfs_acdebug;
643 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
644 #endif
645
646 /*
647 * Check the time stamp
648 * If the cache is valid, copy contents to *vap and return 0
649 * otherwise return an error
650 */
651 int
652 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
653 {
654 struct nfsnode *np;
655 struct vattr *vap;
656 struct nfsmount *nmp;
657 int timeo;
658
659 np = VTONFS(vp);
660 vap = &np->n_vattr;
661 nmp = VFSTONFS(vp->v_mount);
662 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
663 timeo = (time_second - np->n_mtime.tv_sec) / 10;
664
665 #ifdef NFS_ACDEBUG
666 if (nfs_acdebug>1)
667 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
668 #endif
669
670 if (vap->va_type == VDIR) {
671 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
672 timeo = nmp->nm_acdirmin;
673 else if (timeo > nmp->nm_acdirmax)
674 timeo = nmp->nm_acdirmax;
675 } else {
676 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
677 timeo = nmp->nm_acregmin;
678 else if (timeo > nmp->nm_acregmax)
679 timeo = nmp->nm_acregmax;
680 }
681
682 #ifdef NFS_ACDEBUG
683 if (nfs_acdebug > 2)
684 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
685 nmp->nm_acregmin, nmp->nm_acregmax,
686 nmp->nm_acdirmin, nmp->nm_acdirmax);
687
688 if (nfs_acdebug)
689 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
690 (time_second - np->n_attrstamp), timeo);
691 #endif
692
693 if ((time_second - np->n_attrstamp) >= timeo) {
694 nfsstats.attrcache_misses++;
695 return (ENOENT);
696 }
697 nfsstats.attrcache_hits++;
698 if (vap->va_size != np->n_size) {
699 if (vap->va_type == VREG) {
700 if (np->n_flag & NMODIFIED) {
701 if (vap->va_size < np->n_size)
702 vap->va_size = np->n_size;
703 else
704 np->n_size = vap->va_size;
705 } else {
706 np->n_size = vap->va_size;
707 }
708 vnode_pager_setsize(vp, np->n_size);
709 } else {
710 np->n_size = vap->va_size;
711 }
712 }
713 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
714 if (np->n_flag & NCHG) {
715 if (np->n_flag & NACC)
716 vaper->va_atime = np->n_atim;
717 if (np->n_flag & NUPD)
718 vaper->va_mtime = np->n_mtim;
719 }
720 return (0);
721 }
722
723 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
724 /*
725 * This function finds the directory cookie that corresponds to the
726 * logical byte offset given.
727 */
728 nfsuint64 *
729 nfs_getcookie(struct nfsnode *np, off_t off, int add)
730 {
731 struct nfsdmap *dp, *dp2;
732 int pos;
733
734 pos = (uoff_t)off / NFS_DIRBLKSIZ;
735 if (pos == 0 || off < 0) {
736 #ifdef DIAGNOSTIC
737 if (add)
738 panic("nfs getcookie add at <= 0");
739 #endif
740 return (&nfs_nullcookie);
741 }
742 pos--;
743 dp = LIST_FIRST(&np->n_cookies);
744 if (!dp) {
745 if (add) {
746 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
747 M_NFSDIROFF, M_WAITOK);
748 dp->ndm_eocookie = 0;
749 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
750 } else
751 return (NULL);
752 }
753 while (pos >= NFSNUMCOOKIES) {
754 pos -= NFSNUMCOOKIES;
755 if (LIST_NEXT(dp, ndm_list)) {
756 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
757 pos >= dp->ndm_eocookie)
758 return (NULL);
759 dp = LIST_NEXT(dp, ndm_list);
760 } else if (add) {
761 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
762 M_NFSDIROFF, M_WAITOK);
763 dp2->ndm_eocookie = 0;
764 LIST_INSERT_AFTER(dp, dp2, ndm_list);
765 dp = dp2;
766 } else
767 return (NULL);
768 }
769 if (pos >= dp->ndm_eocookie) {
770 if (add)
771 dp->ndm_eocookie = pos + 1;
772 else
773 return (NULL);
774 }
775 return (&dp->ndm_cookies[pos]);
776 }
777
778 /*
779 * Invalidate cached directory information, except for the actual directory
780 * blocks (which are invalidated separately).
781 * Done mainly to avoid the use of stale offset cookies.
782 */
783 void
784 nfs_invaldir(struct vnode *vp)
785 {
786 struct nfsnode *np = VTONFS(vp);
787
788 #ifdef DIAGNOSTIC
789 if (vp->v_type != VDIR)
790 panic("nfs: invaldir not dir");
791 #endif
792 np->n_direofoffset = 0;
793 np->n_cookieverf.nfsuquad[0] = 0;
794 np->n_cookieverf.nfsuquad[1] = 0;
795 if (LIST_FIRST(&np->n_cookies))
796 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
797 }
798
799 /*
800 * The write verifier has changed (probably due to a server reboot), so all
801 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
802 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
803 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
804 * mount point.
805 *
806 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
807 * writes are not clusterable.
808 */
809 void
810 nfs_clearcommit(struct mount *mp)
811 {
812 struct vnode *vp, *nvp;
813 struct buf *bp, *nbp;
814 int s;
815
816 GIANT_REQUIRED;
817
818 s = splbio();
819 MNT_ILOCK(mp);
820 MNT_VNODE_FOREACH(vp, mp, nvp) {
821 VI_LOCK(vp);
822 if (vp->v_iflag & VI_DOOMED) {
823 VI_UNLOCK(vp);
824 continue;
825 }
826 MNT_IUNLOCK(mp);
827 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
828 if (BUF_REFCNT(bp) == 0 &&
829 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
830 == (B_DELWRI | B_NEEDCOMMIT))
831 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
832 }
833 VI_UNLOCK(vp);
834 MNT_ILOCK(mp);
835 }
836 MNT_IUNLOCK(mp);
837 splx(s);
838 }
839
840 /*
841 * Helper functions for former macros. Some of these should be
842 * moved to their callers.
843 */
844
845 int
846 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
847 struct mbuf **md, caddr_t *dpos)
848 {
849 struct nfsnode *ttnp;
850 struct vnode *ttvp;
851 nfsfh_t *ttfhp;
852 u_int32_t *tl;
853 int ttfhsize;
854 int t1;
855
856 if (v3) {
857 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
858 if (tl == NULL)
859 return EBADRPC;
860 *f = fxdr_unsigned(int, *tl);
861 } else
862 *f = 1;
863 if (*f) {
864 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
865 if (t1 != 0)
866 return t1;
867 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
868 if (t1 != 0)
869 return t1;
870 *v = NFSTOV(ttnp);
871 }
872 if (v3) {
873 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
874 if (tl == NULL)
875 return EBADRPC;
876 if (*f)
877 *f = fxdr_unsigned(int, *tl);
878 else if (fxdr_unsigned(int, *tl))
879 nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
880 }
881 if (*f) {
882 ttvp = *v;
883 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
884 if (t1)
885 return t1;
886 *v = ttvp;
887 }
888 return 0;
889 }
890
891 int
892 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
893 {
894 u_int32_t *tl;
895
896 if (v3) {
897 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
898 if (tl == NULL)
899 return EBADRPC;
900 *s = fxdr_unsigned(int, *tl);
901 if (*s <= 0 || *s > NFSX_V3FHMAX)
902 return EBADRPC;
903 } else
904 *s = NFSX_V2FH;
905 *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
906 if (*f == NULL)
907 return EBADRPC;
908 else
909 return 0;
910 }
911
912
913 int
914 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
915 caddr_t *dpos)
916 {
917 int t1;
918
919 struct vnode *ttvp = *v;
920 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
921 if (t1 != 0)
922 return t1;
923 *v = ttvp;
924 return 0;
925 }
926
927 int
928 nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
929 caddr_t *dpos)
930 {
931 u_int32_t *tl;
932 int t1;
933
934 struct vnode *ttvp = *v;
935 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
936 if (tl == NULL)
937 return EBADRPC;
938 *f = fxdr_unsigned(int, *tl);
939 if (*f != 0) {
940 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
941 if (t1 != 0) {
942 *f = 0;
943 return t1;
944 }
945 *v = ttvp;
946 }
947 return 0;
948 }
949
950 int
951 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
952 {
953 u_int32_t *tl;
954 int ttattrf, ttretf = 0;
955 int t1;
956
957 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
958 if (tl == NULL)
959 return EBADRPC;
960 if (*tl == nfs_true) {
961 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
962 if (tl == NULL)
963 return EBADRPC;
964 if (*f)
965 ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
966 VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
967 }
968 t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
969 if (t1)
970 return t1;
971 if (*f)
972 *f = ttretf;
973 else
974 *f = ttattrf;
975 return 0;
976 }
977
978 int
979 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
980 {
981 u_int32_t *tl;
982 int t1;
983
984 if (s > m)
985 return ENAMETOOLONG;
986 t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
987 if (t1 <= M_TRAILINGSPACE(*mb)) {
988 tl = nfsm_build_xx(t1, mb, bpos);
989 *tl++ = txdr_unsigned(s);
990 *(tl + ((t1 >> 2) - 2)) = 0;
991 bcopy(a, tl, s);
992 } else {
993 t1 = nfsm_strtmbuf(mb, bpos, a, s);
994 if (t1 != 0)
995 return t1;
996 }
997 return 0;
998 }
999
1000 int
1001 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
1002 {
1003 u_int32_t *tl;
1004 int t1;
1005 caddr_t cp;
1006
1007 if (v3) {
1008 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
1009 if (t1 < M_TRAILINGSPACE(*mb)) {
1010 tl = nfsm_build_xx(t1, mb, bpos);
1011 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
1012 *(tl + ((t1 >> 2) - 2)) = 0;
1013 bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
1014 } else {
1015 t1 = nfsm_strtmbuf(mb, bpos,
1016 (const char *)VTONFS(v)->n_fhp,
1017 VTONFS(v)->n_fhsize);
1018 if (t1 != 0)
1019 return t1;
1020 }
1021 } else {
1022 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1023 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1024 }
1025 return 0;
1026 }
1027
1028 void
1029 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1030 caddr_t *bpos)
1031 {
1032 u_int32_t *tl;
1033
1034 if (va->va_mode != (mode_t)VNOVAL) {
1035 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1036 *tl++ = nfs_true;
1037 *tl = txdr_unsigned(va->va_mode);
1038 } else {
1039 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1040 *tl = nfs_false;
1041 }
1042 if (full && va->va_uid != (uid_t)VNOVAL) {
1043 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1044 *tl++ = nfs_true;
1045 *tl = txdr_unsigned(va->va_uid);
1046 } else {
1047 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1048 *tl = nfs_false;
1049 }
1050 if (full && va->va_gid != (gid_t)VNOVAL) {
1051 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1052 *tl++ = nfs_true;
1053 *tl = txdr_unsigned(va->va_gid);
1054 } else {
1055 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1056 *tl = nfs_false;
1057 }
1058 if (full && va->va_size != VNOVAL) {
1059 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1060 *tl++ = nfs_true;
1061 txdr_hyper(va->va_size, tl);
1062 } else {
1063 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1064 *tl = nfs_false;
1065 }
1066 if (va->va_atime.tv_sec != VNOVAL) {
1067 if (va->va_atime.tv_sec != time_second) {
1068 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1069 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1070 txdr_nfsv3time(&va->va_atime, tl);
1071 } else {
1072 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1073 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1074 }
1075 } else {
1076 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1077 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1078 }
1079 if (va->va_mtime.tv_sec != VNOVAL) {
1080 if (va->va_mtime.tv_sec != time_second) {
1081 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1082 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1083 txdr_nfsv3time(&va->va_mtime, tl);
1084 } else {
1085 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1086 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1087 }
1088 } else {
1089 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1090 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1091 }
1092 }
Cache object: d042b798d0214cb1b06b838490989b61
|