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: src/sys/nfsclient/nfs_subs.c,v 1.158 2008/11/03 10:38:00 dfr Exp $");
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 * Note that stdarg.h and the ANSI style va_start macro is used for both
80 * ANSI and traditional C compilers.
81 */
82 #include <machine/stdarg.h>
83
84 /*
85 * Data items converted to xdr at startup, since they are constant
86 * This is kinda hokey, but may save a little time doing byte swaps
87 */
88 u_int32_t nfs_xdrneg1;
89 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
90 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
91 u_int32_t nfs_true, nfs_false;
92
93 /* And other global data */
94 static u_int32_t nfs_xid = 0;
95 static enum vtype nv2tov_type[8]= {
96 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
97 };
98
99 int nfs_ticks;
100 int nfs_pbuf_freecnt = -1; /* start out unlimited */
101
102 #ifdef NFS_LEGACYRPC
103 struct nfs_reqq nfs_reqq;
104 struct mtx nfs_reqq_mtx;
105 #endif
106 struct nfs_bufq nfs_bufq;
107 static struct mtx nfs_xid_mtx;
108
109 /*
110 * and the reverse mapping from generic to Version 2 procedure numbers
111 */
112 int nfsv2_procid[NFS_NPROCS] = {
113 NFSV2PROC_NULL,
114 NFSV2PROC_GETATTR,
115 NFSV2PROC_SETATTR,
116 NFSV2PROC_LOOKUP,
117 NFSV2PROC_NOOP,
118 NFSV2PROC_READLINK,
119 NFSV2PROC_READ,
120 NFSV2PROC_WRITE,
121 NFSV2PROC_CREATE,
122 NFSV2PROC_MKDIR,
123 NFSV2PROC_SYMLINK,
124 NFSV2PROC_CREATE,
125 NFSV2PROC_REMOVE,
126 NFSV2PROC_RMDIR,
127 NFSV2PROC_RENAME,
128 NFSV2PROC_LINK,
129 NFSV2PROC_READDIR,
130 NFSV2PROC_NOOP,
131 NFSV2PROC_STATFS,
132 NFSV2PROC_NOOP,
133 NFSV2PROC_NOOP,
134 NFSV2PROC_NOOP,
135 NFSV2PROC_NOOP,
136 };
137
138 LIST_HEAD(nfsnodehashhead, nfsnode);
139
140 u_int32_t
141 nfs_xid_gen(void)
142 {
143 uint32_t xid;
144
145 mtx_lock(&nfs_xid_mtx);
146
147 /* Get a pretty random xid to start with */
148 if (!nfs_xid)
149 nfs_xid = random();
150 /*
151 * Skip zero xid if it should ever happen.
152 */
153 if (++nfs_xid == 0)
154 nfs_xid++;
155 xid = nfs_xid;
156 mtx_unlock(&nfs_xid_mtx);
157 return xid;
158 }
159
160 /*
161 * Create the header for an rpc request packet
162 * The hsiz is the size of the rest of the nfs request header.
163 * (just used to decide if a cluster is a good idea)
164 */
165 struct mbuf *
166 nfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
167 {
168 struct mbuf *mb;
169
170 MGET(mb, M_WAIT, MT_DATA);
171 if (hsiz >= MINCLSIZE)
172 MCLGET(mb, M_WAIT);
173 mb->m_len = 0;
174 return (mb);
175 }
176
177 /*
178 * Build the RPC header and fill in the authorization info.
179 * The authorization string argument is only used when the credentials
180 * come from outside of the kernel.
181 * Returns the head of the mbuf list.
182 */
183 struct mbuf *
184 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
185 int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
186 u_int32_t **xidpp)
187 {
188 struct mbuf *mb;
189 u_int32_t *tl;
190 caddr_t bpos;
191 int i;
192 struct mbuf *mreq;
193 int grpsiz, authsiz;
194
195 authsiz = nfsm_rndup(auth_len);
196 MGETHDR(mb, M_WAIT, MT_DATA);
197 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
198 MCLGET(mb, M_WAIT);
199 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
200 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
201 } else {
202 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
203 }
204 mb->m_len = 0;
205 mreq = mb;
206 bpos = mtod(mb, caddr_t);
207
208 /*
209 * First the RPC header.
210 */
211 tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED);
212
213 *xidpp = tl;
214 *tl++ = txdr_unsigned(nfs_xid_gen());
215 *tl++ = rpc_call;
216 *tl++ = rpc_vers;
217 *tl++ = txdr_unsigned(NFS_PROG);
218 if (nmflag & NFSMNT_NFSV3) {
219 *tl++ = txdr_unsigned(NFS_VER3);
220 *tl++ = txdr_unsigned(procid);
221 } else {
222 *tl++ = txdr_unsigned(NFS_VER2);
223 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
224 }
225
226 /*
227 * And then the authorization cred.
228 */
229 *tl++ = txdr_unsigned(auth_type);
230 *tl = txdr_unsigned(authsiz);
231 switch (auth_type) {
232 case RPCAUTH_UNIX:
233 tl = nfsm_build(u_int32_t *, auth_len);
234 *tl++ = 0; /* stamp ?? */
235 *tl++ = 0; /* NULL hostname */
236 *tl++ = txdr_unsigned(cr->cr_uid);
237 *tl++ = txdr_unsigned(cr->cr_groups[0]);
238 grpsiz = (auth_len >> 2) - 5;
239 *tl++ = txdr_unsigned(grpsiz);
240 for (i = 1; i <= grpsiz; i++)
241 *tl++ = txdr_unsigned(cr->cr_groups[i]);
242 break;
243 }
244
245 /*
246 * And the verifier...
247 */
248 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
249 *tl++ = txdr_unsigned(RPCAUTH_NULL);
250 *tl = 0;
251 mb->m_next = mrest;
252 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
253 mreq->m_pkthdr.rcvif = NULL;
254 *mbp = mb;
255 return (mreq);
256 }
257
258 /*
259 * copies a uio scatter/gather list to an mbuf chain.
260 * NOTE: can ony handle iovcnt == 1
261 */
262 int
263 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
264 {
265 char *uiocp;
266 struct mbuf *mp, *mp2;
267 int xfer, left, mlen;
268 int uiosiz, clflg, rem;
269 char *cp;
270
271 #ifdef DIAGNOSTIC
272 if (uiop->uio_iovcnt != 1)
273 panic("nfsm_uiotombuf: iovcnt != 1");
274 #endif
275
276 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
277 clflg = 1;
278 else
279 clflg = 0;
280 rem = nfsm_rndup(siz)-siz;
281 mp = mp2 = *mq;
282 while (siz > 0) {
283 left = uiop->uio_iov->iov_len;
284 uiocp = uiop->uio_iov->iov_base;
285 if (left > siz)
286 left = siz;
287 uiosiz = left;
288 while (left > 0) {
289 mlen = M_TRAILINGSPACE(mp);
290 if (mlen == 0) {
291 MGET(mp, M_WAIT, MT_DATA);
292 if (clflg)
293 MCLGET(mp, M_WAIT);
294 mp->m_len = 0;
295 mp2->m_next = mp;
296 mp2 = mp;
297 mlen = M_TRAILINGSPACE(mp);
298 }
299 xfer = (left > mlen) ? mlen : left;
300 #ifdef notdef
301 /* Not Yet.. */
302 if (uiop->uio_iov->iov_op != NULL)
303 (*(uiop->uio_iov->iov_op))
304 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
305 else
306 #endif
307 if (uiop->uio_segflg == UIO_SYSSPACE)
308 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
309 else
310 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
311 mp->m_len += xfer;
312 left -= xfer;
313 uiocp += xfer;
314 uiop->uio_offset += xfer;
315 uiop->uio_resid -= xfer;
316 }
317 uiop->uio_iov->iov_base =
318 (char *)uiop->uio_iov->iov_base + uiosiz;
319 uiop->uio_iov->iov_len -= uiosiz;
320 siz -= uiosiz;
321 }
322 if (rem > 0) {
323 if (rem > M_TRAILINGSPACE(mp)) {
324 MGET(mp, M_WAIT, MT_DATA);
325 mp->m_len = 0;
326 mp2->m_next = mp;
327 }
328 cp = mtod(mp, caddr_t)+mp->m_len;
329 for (left = 0; left < rem; left++)
330 *cp++ = '\0';
331 mp->m_len += rem;
332 *bpos = cp;
333 } else
334 *bpos = mtod(mp, caddr_t)+mp->m_len;
335 *mq = mp;
336 return (0);
337 }
338
339 /*
340 * Copy a string into mbufs for the hard cases...
341 */
342 int
343 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
344 {
345 struct mbuf *m1 = NULL, *m2;
346 long left, xfer, len, tlen;
347 u_int32_t *tl;
348 int putsize;
349
350 putsize = 1;
351 m2 = *mb;
352 left = M_TRAILINGSPACE(m2);
353 if (left > 0) {
354 tl = ((u_int32_t *)(*bpos));
355 *tl++ = txdr_unsigned(siz);
356 putsize = 0;
357 left -= NFSX_UNSIGNED;
358 m2->m_len += NFSX_UNSIGNED;
359 if (left > 0) {
360 bcopy(cp, (caddr_t) tl, left);
361 siz -= left;
362 cp += left;
363 m2->m_len += left;
364 left = 0;
365 }
366 }
367 /* Loop around adding mbufs */
368 while (siz > 0) {
369 MGET(m1, M_WAIT, MT_DATA);
370 if (siz > MLEN)
371 MCLGET(m1, M_WAIT);
372 m1->m_len = NFSMSIZ(m1);
373 m2->m_next = m1;
374 m2 = m1;
375 tl = mtod(m1, u_int32_t *);
376 tlen = 0;
377 if (putsize) {
378 *tl++ = txdr_unsigned(siz);
379 m1->m_len -= NFSX_UNSIGNED;
380 tlen = NFSX_UNSIGNED;
381 putsize = 0;
382 }
383 if (siz < m1->m_len) {
384 len = nfsm_rndup(siz);
385 xfer = siz;
386 if (xfer < len)
387 *(tl+(xfer>>2)) = 0;
388 } else {
389 xfer = len = m1->m_len;
390 }
391 bcopy(cp, (caddr_t) tl, xfer);
392 m1->m_len = len+tlen;
393 siz -= xfer;
394 cp += xfer;
395 }
396 *mb = m1;
397 *bpos = mtod(m1, caddr_t)+m1->m_len;
398 return (0);
399 }
400
401 /*
402 * Called once to initialize data structures...
403 */
404 int
405 nfs_init(struct vfsconf *vfsp)
406 {
407 int i;
408
409 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
410 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
411 rpc_vers = txdr_unsigned(RPC_VER2);
412 rpc_call = txdr_unsigned(RPC_CALL);
413 rpc_reply = txdr_unsigned(RPC_REPLY);
414 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
415 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
416 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
417 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
418 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
419 nfs_true = txdr_unsigned(TRUE);
420 nfs_false = txdr_unsigned(FALSE);
421 nfs_xdrneg1 = txdr_unsigned(-1);
422 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
423 if (nfs_ticks < 1)
424 nfs_ticks = 1;
425 /* Ensure async daemons disabled */
426 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
427 nfs_iodwant[i] = NULL;
428 nfs_iodmount[i] = NULL;
429 }
430 nfs_nhinit(); /* Init the nfsnode table */
431
432 /*
433 * Initialize reply list and start timer
434 */
435 #ifdef NFS_LEGACYRPC
436 TAILQ_INIT(&nfs_reqq);
437 mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF);
438 callout_init(&nfs_callout, CALLOUT_MPSAFE);
439 #endif
440 mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
441 mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
442
443 nfs_pbuf_freecnt = nswbuf / 2 + 1;
444
445 return (0);
446 }
447
448 int
449 nfs_uninit(struct vfsconf *vfsp)
450 {
451 int i;
452
453 #ifdef NFS_LEGACYRPC
454 callout_stop(&nfs_callout);
455
456 KASSERT(TAILQ_EMPTY(&nfs_reqq),
457 ("nfs_uninit: request queue not empty"));
458 #endif
459
460 /*
461 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
462 * any sleeping nfsiods so they check nfs_iodmax and exit.
463 */
464 mtx_lock(&nfs_iod_mtx);
465 nfs_iodmax = 0;
466 for (i = 0; i < nfs_numasync; i++)
467 if (nfs_iodwant[i])
468 wakeup(&nfs_iodwant[i]);
469 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
470 while (nfs_numasync)
471 msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
472 mtx_unlock(&nfs_iod_mtx);
473 nfs_nhuninit();
474 uma_zdestroy(nfsmount_zone);
475 return (0);
476 }
477
478 void
479 nfs_dircookie_lock(struct nfsnode *np)
480 {
481 mtx_lock(&np->n_mtx);
482 while (np->n_flag & NDIRCOOKIELK)
483 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
484 np->n_flag |= NDIRCOOKIELK;
485 mtx_unlock(&np->n_mtx);
486 }
487
488 void
489 nfs_dircookie_unlock(struct nfsnode *np)
490 {
491 mtx_lock(&np->n_mtx);
492 np->n_flag &= ~NDIRCOOKIELK;
493 wakeup(&np->n_flag);
494 mtx_unlock(&np->n_mtx);
495 }
496
497 int
498 nfs_upgrade_vnlock(struct vnode *vp)
499 {
500 int old_lock;
501
502 if ((old_lock = VOP_ISLOCKED(vp)) != LK_EXCLUSIVE) {
503 if (old_lock == LK_SHARED) {
504 /* Upgrade to exclusive lock, this might block */
505 vn_lock(vp, LK_UPGRADE | LK_RETRY);
506 } else {
507 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
508 }
509 }
510 return old_lock;
511 }
512
513 void
514 nfs_downgrade_vnlock(struct vnode *vp, int old_lock)
515 {
516 if (old_lock != LK_EXCLUSIVE) {
517 if (old_lock == LK_SHARED) {
518 /* Downgrade from exclusive lock, this might block */
519 vn_lock(vp, LK_DOWNGRADE);
520 } else {
521 VOP_UNLOCK(vp, 0);
522 }
523 }
524 }
525
526 void
527 nfs_printf(const char *fmt, ...)
528 {
529 va_list ap;
530
531 mtx_lock(&Giant);
532 va_start(ap, fmt);
533 printf(fmt, ap);
534 va_end(ap);
535 mtx_unlock(&Giant);
536 }
537
538 /*
539 * Attribute cache routines.
540 * nfs_loadattrcache() - loads or updates the cache contents from attributes
541 * that are on the mbuf list
542 * nfs_getattrcache() - returns valid attributes if found in cache, returns
543 * error otherwise
544 */
545
546 /*
547 * Load the attribute cache (that lives in the nfsnode entry) with
548 * the values on the mbuf list and
549 * Iff vap not NULL
550 * copy the attributes to *vaper
551 */
552 int
553 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
554 struct vattr *vaper, int dontshrink)
555 {
556 struct vnode *vp = *vpp;
557 struct vattr *vap;
558 struct nfs_fattr *fp;
559 struct nfsnode *np;
560 int32_t t1;
561 caddr_t cp2;
562 int rdev;
563 struct mbuf *md;
564 enum vtype vtyp;
565 u_short vmode;
566 struct timespec mtime, mtime_save;
567 int v3 = NFS_ISV3(vp);
568 struct thread *td = curthread;
569
570 md = *mdp;
571 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
572 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAIT);
573 if (cp2 == NULL)
574 return EBADRPC;
575 fp = (struct nfs_fattr *)cp2;
576 if (v3) {
577 vtyp = nfsv3tov_type(fp->fa_type);
578 vmode = fxdr_unsigned(u_short, fp->fa_mode);
579 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
580 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
581 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
582 } else {
583 vtyp = nfsv2tov_type(fp->fa_type);
584 vmode = fxdr_unsigned(u_short, fp->fa_mode);
585 /*
586 * XXX
587 *
588 * The duplicate information returned in fa_type and fa_mode
589 * is an ambiguity in the NFS version 2 protocol.
590 *
591 * VREG should be taken literally as a regular file. If a
592 * server intents to return some type information differently
593 * in the upper bits of the mode field (e.g. for sockets, or
594 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
595 * leave the examination of the mode bits even in the VREG
596 * case to avoid breakage for bogus servers, but we make sure
597 * that there are actually type bits set in the upper part of
598 * fa_mode (and failing that, trust the va_type field).
599 *
600 * NFSv3 cleared the issue, and requires fa_mode to not
601 * contain any type information (while also introduing sockets
602 * and FIFOs for fa_type).
603 */
604 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
605 vtyp = IFTOVT(vmode);
606 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
607 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
608
609 /*
610 * Really ugly NFSv2 kludge.
611 */
612 if (vtyp == VCHR && rdev == 0xffffffff)
613 vtyp = VFIFO;
614 }
615
616 /*
617 * If v_type == VNON it is a new node, so fill in the v_type,
618 * n_mtime fields. Check to see if it represents a special
619 * device, and if so, check for a possible alias. Once the
620 * correct vnode has been obtained, fill in the rest of the
621 * information.
622 */
623 np = VTONFS(vp);
624 mtx_lock(&np->n_mtx);
625 if (vp->v_type != vtyp) {
626 vp->v_type = vtyp;
627 if (vp->v_type == VFIFO)
628 vp->v_op = &nfs_fifoops;
629 np->n_mtime = mtime;
630 }
631 vap = &np->n_vattr;
632 vap->va_type = vtyp;
633 vap->va_mode = (vmode & 07777);
634 vap->va_rdev = rdev;
635 mtime_save = vap->va_mtime;
636 vap->va_mtime = mtime;
637 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
638 if (v3) {
639 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
640 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
641 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
642 vap->va_size = fxdr_hyper(&fp->fa3_size);
643 vap->va_blocksize = NFS_FABLKSIZE;
644 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
645 vap->va_fileid = fxdr_unsigned(int32_t,
646 fp->fa3_fileid.nfsuquad[1]);
647 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
648 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
649 vap->va_flags = 0;
650 vap->va_filerev = 0;
651 } else {
652 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
653 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
654 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
655 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
656 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
657 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
658 * NFS_FABLKSIZE;
659 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
660 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
661 vap->va_flags = 0;
662 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
663 fp->fa2_ctime.nfsv2_sec);
664 vap->va_ctime.tv_nsec = 0;
665 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
666 vap->va_filerev = 0;
667 }
668 np->n_attrstamp = time_second;
669 /* Timestamp the NFS otw getattr fetch */
670 if (td->td_proc) {
671 np->n_ac_ts_tid = td->td_tid;
672 np->n_ac_ts_pid = td->td_proc->p_pid;
673 np->n_ac_ts_syscalls = td->td_syscalls;
674 } else
675 bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
676
677 if (vap->va_size != np->n_size) {
678 if (vap->va_type == VREG) {
679 if (dontshrink && vap->va_size < np->n_size) {
680 /*
681 * We've been told not to shrink the file;
682 * zero np->n_attrstamp to indicate that
683 * the attributes are stale.
684 */
685 vap->va_size = np->n_size;
686 np->n_attrstamp = 0;
687 } else if (np->n_flag & NMODIFIED) {
688 /*
689 * We've modified the file: Use the larger
690 * of our size, and the server's size.
691 */
692 if (vap->va_size < np->n_size) {
693 vap->va_size = np->n_size;
694 } else {
695 np->n_size = vap->va_size;
696 np->n_flag |= NSIZECHANGED;
697 }
698 } else {
699 np->n_size = vap->va_size;
700 np->n_flag |= NSIZECHANGED;
701 }
702 vnode_pager_setsize(vp, np->n_size);
703 } else {
704 np->n_size = vap->va_size;
705 }
706 }
707 /*
708 * The following checks are added to prevent a race between (say)
709 * a READDIR+ and a WRITE.
710 * READDIR+, WRITE requests sent out.
711 * READDIR+ resp, WRITE resp received on client.
712 * However, the WRITE resp was handled before the READDIR+ resp
713 * causing the post op attrs from the write to be loaded first
714 * and the attrs from the READDIR+ to be loaded later. If this
715 * happens, we have stale attrs loaded into the attrcache.
716 * We detect this by for the mtime moving back. We invalidate the
717 * attrcache when this happens.
718 */
719 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
720 /* Size changed or mtime went backwards */
721 np->n_attrstamp = 0;
722 if (vaper != NULL) {
723 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
724 if (np->n_flag & NCHG) {
725 if (np->n_flag & NACC)
726 vaper->va_atime = np->n_atim;
727 if (np->n_flag & NUPD)
728 vaper->va_mtime = np->n_mtim;
729 }
730 }
731 mtx_unlock(&np->n_mtx);
732 return (0);
733 }
734
735 #ifdef NFS_ACDEBUG
736 #include <sys/sysctl.h>
737 SYSCTL_DECL(_vfs_nfs);
738 static int nfs_acdebug;
739 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
740 "Toggle acdebug (access cache debug) flag");
741 #endif
742
743 /*
744 * Check the time stamp
745 * If the cache is valid, copy contents to *vap and return 0
746 * otherwise return an error
747 */
748 int
749 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
750 {
751 struct nfsnode *np;
752 struct vattr *vap;
753 struct nfsmount *nmp;
754 int timeo;
755
756 np = VTONFS(vp);
757 vap = &np->n_vattr;
758 nmp = VFSTONFS(vp->v_mount);
759 #ifdef NFS_ACDEBUG
760 mtx_lock(&Giant); /* nfs_printf() */
761 #endif
762 mtx_lock(&np->n_mtx);
763 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
764 timeo = (time_second - np->n_mtime.tv_sec) / 10;
765
766 #ifdef NFS_ACDEBUG
767 if (nfs_acdebug>1)
768 nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
769 #endif
770
771 if (vap->va_type == VDIR) {
772 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
773 timeo = nmp->nm_acdirmin;
774 else if (timeo > nmp->nm_acdirmax)
775 timeo = nmp->nm_acdirmax;
776 } else {
777 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
778 timeo = nmp->nm_acregmin;
779 else if (timeo > nmp->nm_acregmax)
780 timeo = nmp->nm_acregmax;
781 }
782
783 #ifdef NFS_ACDEBUG
784 if (nfs_acdebug > 2)
785 nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
786 nmp->nm_acregmin, nmp->nm_acregmax,
787 nmp->nm_acdirmin, nmp->nm_acdirmax);
788
789 if (nfs_acdebug)
790 nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
791 (time_second - np->n_attrstamp), timeo);
792 #endif
793
794 if ((time_second - np->n_attrstamp) >= timeo) {
795 nfsstats.attrcache_misses++;
796 mtx_unlock(&np->n_mtx);
797 return( ENOENT);
798 }
799 nfsstats.attrcache_hits++;
800 if (vap->va_size != np->n_size) {
801 if (vap->va_type == VREG) {
802 if (np->n_flag & NMODIFIED) {
803 if (vap->va_size < np->n_size)
804 vap->va_size = np->n_size;
805 else
806 np->n_size = vap->va_size;
807 } else {
808 np->n_size = vap->va_size;
809 }
810 vnode_pager_setsize(vp, np->n_size);
811 } else {
812 np->n_size = vap->va_size;
813 }
814 }
815 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
816 if (np->n_flag & NCHG) {
817 if (np->n_flag & NACC)
818 vaper->va_atime = np->n_atim;
819 if (np->n_flag & NUPD)
820 vaper->va_mtime = np->n_mtim;
821 }
822 mtx_unlock(&np->n_mtx);
823 #ifdef NFS_ACDEBUG
824 mtx_unlock(&Giant); /* nfs_printf() */
825 #endif
826 return (0);
827 }
828
829 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
830 /*
831 * This function finds the directory cookie that corresponds to the
832 * logical byte offset given.
833 */
834 nfsuint64 *
835 nfs_getcookie(struct nfsnode *np, off_t off, int add)
836 {
837 struct nfsdmap *dp, *dp2;
838 int pos;
839 nfsuint64 *retval = NULL;
840
841 pos = (uoff_t)off / NFS_DIRBLKSIZ;
842 if (pos == 0 || off < 0) {
843 #ifdef DIAGNOSTIC
844 if (add)
845 panic("nfs getcookie add at <= 0");
846 #endif
847 return (&nfs_nullcookie);
848 }
849 pos--;
850 dp = LIST_FIRST(&np->n_cookies);
851 if (!dp) {
852 if (add) {
853 dp = malloc(sizeof (struct nfsdmap),
854 M_NFSDIROFF, M_WAITOK);
855 dp->ndm_eocookie = 0;
856 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
857 } else
858 goto out;
859 }
860 while (pos >= NFSNUMCOOKIES) {
861 pos -= NFSNUMCOOKIES;
862 if (LIST_NEXT(dp, ndm_list)) {
863 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
864 pos >= dp->ndm_eocookie)
865 goto out;
866 dp = LIST_NEXT(dp, ndm_list);
867 } else if (add) {
868 dp2 = malloc(sizeof (struct nfsdmap),
869 M_NFSDIROFF, M_WAITOK);
870 dp2->ndm_eocookie = 0;
871 LIST_INSERT_AFTER(dp, dp2, ndm_list);
872 dp = dp2;
873 } else
874 goto out;
875 }
876 if (pos >= dp->ndm_eocookie) {
877 if (add)
878 dp->ndm_eocookie = pos + 1;
879 else
880 goto out;
881 }
882 retval = &dp->ndm_cookies[pos];
883 out:
884 return (retval);
885 }
886
887 /*
888 * Invalidate cached directory information, except for the actual directory
889 * blocks (which are invalidated separately).
890 * Done mainly to avoid the use of stale offset cookies.
891 */
892 void
893 nfs_invaldir(struct vnode *vp)
894 {
895 struct nfsnode *np = VTONFS(vp);
896
897 #ifdef DIAGNOSTIC
898 if (vp->v_type != VDIR)
899 panic("nfs: invaldir not dir");
900 #endif
901 nfs_dircookie_lock(np);
902 np->n_direofoffset = 0;
903 np->n_cookieverf.nfsuquad[0] = 0;
904 np->n_cookieverf.nfsuquad[1] = 0;
905 if (LIST_FIRST(&np->n_cookies))
906 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
907 nfs_dircookie_unlock(np);
908 }
909
910 /*
911 * The write verifier has changed (probably due to a server reboot), so all
912 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
913 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
914 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
915 * mount point.
916 *
917 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
918 * writes are not clusterable.
919 */
920 void
921 nfs_clearcommit(struct mount *mp)
922 {
923 struct vnode *vp, *nvp;
924 struct buf *bp, *nbp;
925 struct bufobj *bo;
926
927 MNT_ILOCK(mp);
928 MNT_VNODE_FOREACH(vp, mp, nvp) {
929 bo = &vp->v_bufobj;
930 VI_LOCK(vp);
931 if (vp->v_iflag & VI_DOOMED) {
932 VI_UNLOCK(vp);
933 continue;
934 }
935 vholdl(vp);
936 VI_UNLOCK(vp);
937 MNT_IUNLOCK(mp);
938 BO_LOCK(bo);
939 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
940 if (!BUF_ISLOCKED(bp) &&
941 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
942 == (B_DELWRI | B_NEEDCOMMIT))
943 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
944 }
945 BO_UNLOCK(bo);
946 vdrop(vp);
947 MNT_ILOCK(mp);
948 }
949 MNT_IUNLOCK(mp);
950 }
951
952 /*
953 * Helper functions for former macros. Some of these should be
954 * moved to their callers.
955 */
|