host and path are now found in m_stat
[unix-history] / usr / src / sys / nfs / nfs_vnops.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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 are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * @(#)nfs_vnops.c 7.32 (Berkeley) %G%
21 */
22
23/*
24 * vnode op calls for sun nfs version 2
25 */
26
27#include "machine/pte.h"
28#include "machine/mtpr.h"
29#include "param.h"
30#include "user.h"
31#include "proc.h"
32#include "kernel.h"
33#include "mount.h"
34#include "buf.h"
35#include "vm.h"
36#include "malloc.h"
37#include "mbuf.h"
38#include "errno.h"
39#include "file.h"
40#include "conf.h"
41#include "vnode.h"
42#include "text.h"
43#include "map.h"
44#include "nfsv2.h"
45#include "nfs.h"
46#include "nfsnode.h"
47#include "nfsmount.h"
48#include "xdr_subs.h"
49#include "nfsm_subs.h"
50#include "nfsiom.h"
51
52/* Defs */
53#define TRUE 1
54#define FALSE 0
55
56/* Global vars */
57int nfs_lookup(),
58 nfs_create(),
59 nfs_mknod(),
60 nfs_open(),
61 nfs_close(),
62 nfs_access(),
63 nfs_getattr(),
64 nfs_setattr(),
65 nfs_read(),
66 nfs_write(),
67 vfs_noop(),
68 vfs_nullop(),
69 nfs_remove(),
70 nfs_link(),
71 nfs_rename(),
72 nfs_mkdir(),
73 nfs_rmdir(),
74 nfs_symlink(),
75 nfs_readdir(),
76 nfs_readlink(),
77 nfs_abortop(),
78 nfs_lock(),
79 nfs_unlock(),
80 nfs_bmap(),
81 nfs_strategy(),
82 nfs_fsync(),
83 nfs_inactive(),
84 nfs_reclaim(),
85 nfs_print(),
86 nfs_islocked();
87
88struct vnodeops nfsv2_vnodeops = {
89 nfs_lookup, /* lookup */
90 nfs_create, /* create */
91 nfs_mknod, /* mknod */
92 nfs_open, /* open */
93 nfs_close, /* close */
94 nfs_access, /* access */
95 nfs_getattr, /* getattr */
96 nfs_setattr, /* setattr */
97 nfs_read, /* read */
98 nfs_write, /* write */
99 vfs_noop, /* ioctl */
100 vfs_noop, /* select */
101 vfs_noop, /* mmap */
102 nfs_fsync, /* fsync */
103 vfs_nullop, /* seek */
104 nfs_remove, /* remove */
105 nfs_link, /* link */
106 nfs_rename, /* rename */
107 nfs_mkdir, /* mkdir */
108 nfs_rmdir, /* rmdir */
109 nfs_symlink, /* symlink */
110 nfs_readdir, /* readdir */
111 nfs_readlink, /* readlink */
112 nfs_abortop, /* abortop */
113 nfs_inactive, /* inactive */
114 nfs_reclaim, /* reclaim */
115 nfs_lock, /* lock */
116 nfs_unlock, /* unlock */
117 nfs_bmap, /* bmap */
118 nfs_strategy, /* strategy */
119 nfs_print, /* print */
120 nfs_islocked, /* islocked */
121};
122
123/* Special device vnode ops */
124int spec_lookup(),
125 spec_open(),
126 spec_read(),
127 spec_write(),
128 spec_strategy(),
129 spec_bmap(),
130 spec_ioctl(),
131 spec_select(),
132 spec_close(),
133 spec_badop(),
134 spec_nullop();
135
136struct vnodeops spec_nfsv2nodeops = {
137 spec_lookup, /* lookup */
138 spec_badop, /* create */
139 spec_badop, /* mknod */
140 spec_open, /* open */
141 spec_close, /* close */
142 nfs_access, /* access */
143 nfs_getattr, /* getattr */
144 nfs_setattr, /* setattr */
145 spec_read, /* read */
146 spec_write, /* write */
147 spec_ioctl, /* ioctl */
148 spec_select, /* select */
149 spec_badop, /* mmap */
150 spec_nullop, /* fsync */
151 spec_badop, /* seek */
152 spec_badop, /* remove */
153 spec_badop, /* link */
154 spec_badop, /* rename */
155 spec_badop, /* mkdir */
156 spec_badop, /* rmdir */
157 spec_badop, /* symlink */
158 spec_badop, /* readdir */
159 spec_badop, /* readlink */
160 spec_badop, /* abortop */
161 nfs_inactive, /* inactive */
162 nfs_reclaim, /* reclaim */
163 nfs_lock, /* lock */
164 nfs_unlock, /* unlock */
165 spec_bmap, /* bmap */
166 spec_strategy, /* strategy */
167 nfs_print, /* print */
168 nfs_islocked, /* islocked */
169};
170
171#ifdef FIFO
172int fifo_lookup(),
173 fifo_open(),
174 fifo_read(),
175 fifo_write(),
176 fifo_bmap(),
177 fifo_ioctl(),
178 fifo_select(),
179 fifo_close(),
180 fifo_print(),
181 fifo_badop(),
182 fifo_nullop();
183
184struct vnodeops fifo_nfsv2nodeops = {
185 fifo_lookup, /* lookup */
186 fifo_badop, /* create */
187 fifo_badop, /* mknod */
188 fifo_open, /* open */
189 fifo_close, /* close */
190 nfs_access, /* access */
191 nfs_getattr, /* getattr */
192 nfs_setattr, /* setattr */
193 fifo_read, /* read */
194 fifo_write, /* write */
195 fifo_ioctl, /* ioctl */
196 fifo_select, /* select */
197 fifo_badop, /* mmap */
198 fifo_nullop, /* fsync */
199 fifo_badop, /* seek */
200 fifo_badop, /* remove */
201 fifo_badop, /* link */
202 fifo_badop, /* rename */
203 fifo_badop, /* mkdir */
204 fifo_badop, /* rmdir */
205 fifo_badop, /* symlink */
206 fifo_badop, /* readdir */
207 fifo_badop, /* readlink */
208 fifo_badop, /* abortop */
209 nfs_inactive, /* inactive */
210 nfs_reclaim, /* reclaim */
211 nfs_lock, /* lock */
212 nfs_unlock, /* unlock */
213 fifo_bmap, /* bmap */
214 fifo_badop, /* strategy */
215 nfs_print, /* print */
216 nfs_islocked, /* islocked */
217};
218#endif /* FIFO */
219
220extern u_long nfs_procids[NFS_NPROCS];
221extern u_long nfs_prog, nfs_vers;
222extern char nfsiobuf[MAXPHYS+NBPG];
223extern int nonidempotent[NFS_NPROCS];
224struct map nfsmap[NFS_MSIZ];
225enum vtype v_type[NFLNK+1];
226struct buf nfs_bqueue; /* Queue head for nfsiod's */
227int nfs_asyncdaemons = 0;
228struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
229static int nfsmap_want = 0;
230
231/*
232 * nfs null call from vfs.
233 */
234nfs_null(vp, cred)
235 struct vnode *vp;
236 struct ucred *cred;
237{
238 caddr_t bpos, dpos;
239 u_long xid;
240 int error = 0;
241 struct mbuf *mreq, *mrep, *md, *mb;
242
243 nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0);
244 nfsm_request(vp, nonidempotent[NFSPROC_NULL]);
245 nfsm_reqdone;
246 return (error);
247}
248
249/*
250 * nfs access vnode op.
251 * Essentially just get vattr and then imitate iaccess()
252 */
253nfs_access(vp, mode, cred)
254 struct vnode *vp;
255 int mode;
256 register struct ucred *cred;
257{
258 register struct vattr *vap;
259 register gid_t *gp;
260 struct vattr vattr;
261 register int i;
262 int error;
263
264 /*
265 * If you're the super-user,
266 * you always get access.
267 */
268 if (cred->cr_uid == 0)
269 return (0);
270 vap = &vattr;
271 if (error = nfs_getattr(vp, vap, cred))
272 return (error);
273 /*
274 * Access check is based on only one of owner, group, public.
275 * If not owner, then check group. If not a member of the
276 * group, then check public access.
277 */
278 if (cred->cr_uid != vap->va_uid) {
279 mode >>= 3;
280 gp = cred->cr_groups;
281 for (i = 0; i < cred->cr_ngroups; i++, gp++)
282 if (vap->va_gid == *gp)
283 goto found;
284 mode >>= 3;
285found:
286 ;
287 }
288 if ((vap->va_mode & mode) != 0)
289 return (0);
290 return (EACCES);
291}
292
293/*
294 * nfs open vnode op
295 * Just check to see if the type is ok
296 */
297/* ARGSUSED */
298nfs_open(vp, mode, cred)
299 struct vnode *vp;
300 int mode;
301 struct ucred *cred;
302{
303 register enum vtype vtyp;
304
305 vtyp = vp->v_type;
306 if (vtyp == VREG || vtyp == VDIR || vtyp == VLNK)
307 return (0);
308 else
309 return (EACCES);
310}
311
312/*
313 * nfs close vnode op
314 * For reg files, invalidate any buffer cache entries.
315 */
316/* ARGSUSED */
317nfs_close(vp, fflags, cred)
318 register struct vnode *vp;
319 int fflags;
320 struct ucred *cred;
321{
322 register struct nfsnode *np = VTONFS(vp);
323 int error = 0;
324
325 if (vp->v_type == VREG && ((np->n_flag & NMODIFIED) ||
326 ((np->n_flag & NBUFFERED) && np->n_sillyrename))) {
327 nfs_lock(vp);
328 np->n_flag &= ~(NMODIFIED|NBUFFERED);
329 vinvalbuf(vp, TRUE);
330 if (np->n_flag & NWRITEERR) {
331 np->n_flag &= ~NWRITEERR;
332 error = np->n_error;
333 }
334 nfs_unlock(vp);
335 }
336 return (error);
337}
338
339/*
340 * nfs getattr call from vfs.
341 */
342nfs_getattr(vp, vap, cred)
343 register struct vnode *vp;
344 struct vattr *vap;
345 struct ucred *cred;
346{
347 register caddr_t cp;
348 register long t1;
349 caddr_t bpos, dpos;
350 u_long xid;
351 int error = 0;
352 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
353
354 /* First look in the cache.. */
355 if (nfs_getattrcache(vp, vap) == 0)
356 return (0);
357 nfsstats.rpccnt[NFSPROC_GETATTR]++;
358 nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH);
359 nfsm_fhtom(vp);
360 nfsm_request(vp, nonidempotent[NFSPROC_GETATTR]);
361 nfsm_loadattr(vp, vap);
362 nfsm_reqdone;
363 return (error);
364}
365
366/*
367 * nfs setattr call.
368 */
369nfs_setattr(vp, vap, cred)
370 register struct vnode *vp;
371 register struct vattr *vap;
372 struct ucred *cred;
373{
374 register struct nfsv2_sattr *sp;
375 register caddr_t cp;
376 register long t1;
377 caddr_t bpos, dpos;
378 u_long xid;
379 int error = 0;
380 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
381 struct nfsnode *np;
382
383 nfsstats.rpccnt[NFSPROC_SETATTR]++;
384 nfsm_reqhead(nfs_procids[NFSPROC_SETATTR], cred, NFSX_FH+NFSX_SATTR);
385 nfsm_fhtom(vp);
386 nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
387 if (vap->va_mode == 0xffff)
388 sp->sa_mode = VNOVAL;
389 else
390 sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode);
391 if (vap->va_uid == 0xffff)
392 sp->sa_uid = VNOVAL;
393 else
394 sp->sa_uid = txdr_unsigned(vap->va_uid);
395 if (vap->va_gid == 0xffff)
396 sp->sa_gid = VNOVAL;
397 else
398 sp->sa_gid = txdr_unsigned(vap->va_gid);
399 sp->sa_size = txdr_unsigned(vap->va_size);
400 if (vap->va_size != VNOVAL) {
401 np = VTONFS(vp);
402 if (np->n_flag & NMODIFIED) {
403 np->n_flag &= ~NMODIFIED;
404 vinvalbuf(vp, TRUE);
405 }
406 }
407 sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
408 sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
409 txdr_time(&vap->va_mtime, &sp->sa_mtime);
410 nfsm_request(vp, nonidempotent[NFSPROC_SETATTR]);
411 nfsm_loadattr(vp, (struct vattr *)0);
412 /* should we fill in any vap fields ?? */
413 nfsm_reqdone;
414 return (error);
415}
416
417/*
418 * nfs lookup call, one step at a time...
419 * First look in cache
420 * If not found, unlock the directory nfsnode and do the rpc
421 */
422nfs_lookup(vp, ndp)
423 register struct vnode *vp;
424 register struct nameidata *ndp;
425{
426 register struct vnode *vdp;
427 register u_long *p;
428 register caddr_t cp;
429 register long t1, t2;
430 caddr_t bpos, dpos, cp2;
431 u_long xid;
432 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
433 struct vnode *newvp;
434 long len;
435 nfsv2fh_t *fhp;
436 struct nfsnode *np;
437 int lockparent, wantparent, flag, error = 0;
438
439 ndp->ni_dvp = vp;
440 ndp->ni_vp = NULL;
441 if (vp->v_type != VDIR)
442 return (ENOTDIR);
443 lockparent = ndp->ni_nameiop & LOCKPARENT;
444 flag = ndp->ni_nameiop & OPFLAG;
445 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
446 if ((error = cache_lookup(ndp)) && error != ENOENT) {
447 struct vattr vattr;
448 int vpid;
449
450 if (vp == ndp->ni_rdir && ndp->ni_isdotdot)
451 panic("nfs_lookup: .. through root");
452 vdp = ndp->ni_vp;
453 vpid = vdp->v_id;
454 /*
455 * See the comment starting `Step through' in ufs/ufs_lookup.c
456 * for an explanation of the locking protocol
457 */
458 if (vp == vdp) {
459 VREF(vdp);
460 error = 0;
461 } else if (ndp->ni_isdotdot) {
462 nfs_unlock(vp);
463 error = vget(vdp);
464 } else {
465 error = vget(vdp);
466 nfs_unlock(vp);
467 }
468 if (!error) {
469 if (vpid == vdp->v_id) {
470 if (!nfs_getattr(vdp, &vattr, ndp->ni_cred) &&
471 vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) {
472 nfsstats.lookupcache_hits++;
473 return (0);
474 } else {
475 cache_purge(vdp);
476 nfs_nput(vdp);
477 }
478 } else {
479 nfs_nput(vdp);
480 }
481 }
482 ndp->ni_vp = (struct vnode *)0;
483 } else
484 nfs_unlock(vp);
485 error = 0;
486 nfsstats.lookupcache_misses++;
487 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
488 len = ndp->ni_namelen;
489 nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
490 nfsm_fhtom(vp);
491 nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN);
492 nfsm_request(vp, nonidempotent[NFSPROC_LOOKUP]);
493nfsmout:
494 if (error) {
495 if (lockparent || (flag != CREATE && flag != RENAME) ||
496 *ndp->ni_next != 0)
497 nfs_lock(vp);
498 return (ENOENT);
499 }
500 nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH);
501
502 /*
503 * Handle DELETE and RENAME cases...
504 */
505 if (flag == DELETE && *ndp->ni_next == 0) {
506 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
507 VREF(vp);
508 newvp = vp;
509 np = VTONFS(vp);
510 } else {
511 if (error = nfs_nget(vp->v_mount, fhp, &np)) {
512 nfs_lock(vp);
513 m_freem(mrep);
514 return (error);
515 }
516 newvp = NFSTOV(np);
517 }
518 if (error =
519 nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
520 nfs_lock(vp);
521 if (newvp != vp)
522 nfs_nput(newvp);
523 else
524 vrele(vp);
525 m_freem(mrep);
526 return (error);
527 }
528 ndp->ni_vp = newvp;
529 if (lockparent || vp == newvp)
530 nfs_lock(vp);
531 m_freem(mrep);
532 return (0);
533 }
534
535 if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
536 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
537 nfs_lock(vp);
538 m_freem(mrep);
539 return (EISDIR);
540 }
541 if (error = nfs_nget(vp->v_mount, fhp, &np)) {
542 nfs_lock(vp);
543 m_freem(mrep);
544 return (error);
545 }
546 newvp = NFSTOV(np);
547 if (error =
548 nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
549 nfs_lock(vp);
550 nfs_nput(newvp);
551 m_freem(mrep);
552 return (error);
553 }
554 ndp->ni_vp = newvp;
555 if (lockparent)
556 nfs_lock(vp);
557 return (0);
558 }
559
560 if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) {
561 VREF(vp);
562 newvp = vp;
563 np = VTONFS(vp);
564 } else if (ndp->ni_isdotdot) {
565 if (error = nfs_nget(vp->v_mount, fhp, &np)) {
566 nfs_lock(vp);
567 m_freem(mrep);
568 return (error);
569 }
570 newvp = NFSTOV(np);
571 } else {
572 if (error = nfs_nget(vp->v_mount, fhp, &np)) {
573 nfs_lock(vp);
574 m_freem(mrep);
575 return (error);
576 }
577 newvp = NFSTOV(np);
578 }
579 if (error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) {
580 nfs_lock(vp);
581 if (newvp != vp)
582 nfs_nput(newvp);
583 else
584 vrele(vp);
585 m_freem(mrep);
586 return (error);
587 }
588 m_freem(mrep);
589
590 if (vp == newvp || (lockparent && *ndp->ni_next == '\0'))
591 nfs_lock(vp);
592 ndp->ni_vp = newvp;
593 if (error == 0 && ndp->ni_makeentry) {
594 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
595 cache_enter(ndp);
596 }
597 return (error);
598}
599
600/*
601 * nfs readlink call
602 */
603nfs_readlink(vp, uiop, cred)
604 register struct vnode *vp;
605 struct uio *uiop;
606 struct ucred *cred;
607{
608 register u_long *p;
609 register caddr_t cp;
610 register long t1;
611 caddr_t bpos, dpos, cp2;
612 u_long xid;
613 int error = 0;
614 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
615 long len;
616
617 nfsstats.rpccnt[NFSPROC_READLINK]++;
618 nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH);
619 nfsm_fhtom(vp);
620 nfsm_request(vp, nonidempotent[NFSPROC_READLINK]);
621 nfsm_strsiz(len, NFS_MAXPATHLEN);
622 nfsm_mtouio(uiop, len);
623 nfsm_reqdone;
624 return (error);
625}
626
627/*
628 * nfs read call
629 */
630nfs_readrpc(vp, uiop, cred)
631 register struct vnode *vp;
632 struct uio *uiop;
633 struct ucred *cred;
634{
635 register u_long *p;
636 register caddr_t cp;
637 register long t1;
638 caddr_t bpos, dpos, cp2;
639 u_long xid;
640 int error = 0;
641 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
642 struct nfsmount *nmp;
643 long len, retlen, tsiz;
644
645 nmp = vfs_to_nfs(vp->v_mount);
646 tsiz = uiop->uio_resid;
647 while (tsiz > 0) {
648 nfsstats.rpccnt[NFSPROC_READ]++;
649 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
650 nfsm_reqhead(nfs_procids[NFSPROC_READ], cred, NFSX_FH+NFSX_UNSIGNED*3);
651 nfsm_fhtom(vp);
652 nfsm_build(p, u_long *, NFSX_UNSIGNED*3);
653 *p++ = txdr_unsigned(uiop->uio_offset);
654 *p++ = txdr_unsigned(len);
655 *p = 0;
656 nfsm_request(vp, nonidempotent[NFSPROC_READ]);
657 nfsm_loadattr(vp, (struct vattr *)0);
658 nfsm_strsiz(retlen, nmp->nm_rsize);
659 nfsm_mtouio(uiop, retlen);
660 m_freem(mrep);
661 if (retlen < len)
662 tsiz = 0;
663 else
664 tsiz -= len;
665 }
666nfsmout:
667 return (error);
668}
669
670/*
671 * nfs write call
672 */
673nfs_writerpc(vp, uiop, cred)
674 register struct vnode *vp;
675 struct uio *uiop;
676 struct ucred *cred;
677{
678 register u_long *p;
679 register caddr_t cp;
680 register long t1;
681 caddr_t bpos, dpos;
682 u_long xid;
683 int error = 0;
684 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
685 struct nfsmount *nmp;
686 long len, tsiz;
687
688 nmp = vfs_to_nfs(vp->v_mount);
689 tsiz = uiop->uio_resid;
690 while (tsiz > 0) {
691 nfsstats.rpccnt[NFSPROC_WRITE]++;
692 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
693 nfsm_reqhead(nfs_procids[NFSPROC_WRITE], cred,
694 NFSX_FH+NFSX_UNSIGNED*4);
695 nfsm_fhtom(vp);
696 nfsm_build(p, u_long *, NFSX_UNSIGNED*4);
697 *(p+1) = txdr_unsigned(uiop->uio_offset);
698 *(p+3) = txdr_unsigned(len);
699 nfsm_uiotom(uiop, len);
700 nfsm_request(vp, nonidempotent[NFSPROC_WRITE]);
701 nfsm_loadattr(vp, (struct vattr *)0);
702 m_freem(mrep);
703 tsiz -= len;
704 }
705nfsmout:
706 return (error);
707}
708
709/*
710 * nfs mknod call
711 * This call is currently not supported.
712 */
713/* ARGSUSED */
714nfs_mknod(ndp, vap, cred)
715 struct nameidata *ndp;
716 struct ucred *cred;
717 struct vattr *vap;
718{
719
720 nfs_abortop(ndp);
721 return (EOPNOTSUPP);
722}
723
724/*
725 * nfs file create call
726 */
727nfs_create(ndp, vap)
728 register struct nameidata *ndp;
729 register struct vattr *vap;
730{
731 register struct nfsv2_sattr *sp;
732 register u_long *p;
733 register caddr_t cp;
734 register long t1, t2;
735 caddr_t bpos, dpos, cp2;
736 u_long xid;
737 int error = 0;
738 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
739
740 nfsstats.rpccnt[NFSPROC_CREATE]++;
741 nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred,
742 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
743 nfsm_fhtom(ndp->ni_dvp);
744 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
745 nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
746 sp->sa_mode = vtonfs_mode(VREG, vap->va_mode);
747 sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
748 sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
749 sp->sa_size = txdr_unsigned(0);
750 /* or should these be VNOVAL ?? */
751 txdr_time(&vap->va_atime, &sp->sa_atime);
752 txdr_time(&vap->va_mtime, &sp->sa_mtime);
753 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_CREATE]);
754 nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
755 nfsm_reqdone;
756 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
757 nfs_nput(ndp->ni_dvp);
758 return (error);
759}
760
761/*
762 * nfs file remove call
763 * To try and make nfs semantics closer to vfs semantics, a file that has
764 * other references to the vnode is renamed instead of removed and then
765 * removed later on the last close.
766 * Unfortunately you must flush the buffer cache and cmap to get rid of
767 * all extraneous vnode references before you check the reference cnt.
768 * 1 - If the file could have blocks in the buffer cache
769 * flush them out and invalidate them
770 * mpurge the vnode to flush out cmap references
771 * (This is necessary to update the vnode ref cnt as well as sensible
772 * for actual removes, to free up the buffers)
773 * 2 - If v_usecount > 1
774 * If a rename is not already in the works
775 * call nfs_sillyrename() to set it up
776 * else
777 * do the remove rpc
778 */
779nfs_remove(ndp)
780 register struct nameidata *ndp;
781{
782 register struct vnode *vp = ndp->ni_vp;
783 register struct nfsnode *np = VTONFS(ndp->ni_vp);
784 register u_long *p;
785 register caddr_t cp;
786 register long t1, t2;
787 caddr_t bpos, dpos;
788 u_long xid;
789 int error = 0;
790 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
791
792 if (vp->v_type == VREG) {
793 if (np->n_flag & (NMODIFIED|NBUFFERED)) {
794 np->n_flag &= ~(NMODIFIED|NBUFFERED);
795 vinvalbuf(vp, TRUE);
796 }
797 if (np->n_flag & NPAGEDON) {
798 np->n_flag &= ~NPAGEDON;
799 mpurge(vp); /* In case cmap entries still ref it */
800 }
801 }
802 if (vp->v_usecount > 1) {
803 if (!np->n_sillyrename)
804 error = nfs_sillyrename(ndp, REMOVE);
805 } else {
806 nfsstats.rpccnt[NFSPROC_REMOVE]++;
807 nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
808 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
809 nfsm_fhtom(ndp->ni_dvp);
810 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
811 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_REMOVE]);
812 nfsm_reqdone;
813 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
814 /*
815 * Kludge City: If the first reply to the remove rpc is lost..
816 * the reply to the retransmitted request will be ENOENT
817 * since the file was in fact removed
818 * Therefore, we cheat and return success.
819 */
820 if (error == ENOENT)
821 error = 0;
822 }
823 np->n_attrstamp = 0;
824 if (ndp->ni_dvp == vp)
825 vrele(vp);
826 else
827 nfs_nput(ndp->ni_dvp);
828 nfs_nput(vp);
829 return (error);
830}
831
832/*
833 * nfs file remove rpc called from nfs_inactive
834 */
835nfs_removeit(ndp)
836 register struct nameidata *ndp;
837{
838 register u_long *p;
839 register caddr_t cp;
840 register long t1, t2;
841 caddr_t bpos, dpos;
842 u_long xid;
843 int error = 0;
844 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
845
846 nfsstats.rpccnt[NFSPROC_REMOVE]++;
847 nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred,
848 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
849 nfsm_fhtom(ndp->ni_dvp);
850 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
851 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_REMOVE]);
852 nfsm_reqdone;
853 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
854 return (error);
855}
856
857/*
858 * nfs file rename call
859 */
860nfs_rename(sndp, tndp)
861 register struct nameidata *sndp, *tndp;
862{
863 register u_long *p;
864 register caddr_t cp;
865 register long t1, t2;
866 caddr_t bpos, dpos;
867 u_long xid;
868 int error = 0;
869 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
870
871 nfsstats.rpccnt[NFSPROC_RENAME]++;
872 nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
873 (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
874 nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
875 nfsm_fhtom(sndp->ni_dvp);
876 nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
877 nfsm_fhtom(tndp->ni_dvp);
878 nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
879 nfsm_request(sndp->ni_dvp, nonidempotent[NFSPROC_RENAME]);
880 nfsm_reqdone;
881 VTONFS(sndp->ni_dvp)->n_direofstamp = 0;
882 VTONFS(tndp->ni_dvp)->n_direofstamp = 0;
883 if (sndp->ni_vp->v_type == VDIR) {
884 if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR)
885 cache_purge(tndp->ni_dvp);
886 cache_purge(sndp->ni_dvp);
887 }
888 nfs_abortop(sndp);
889 nfs_abortop(tndp);
890 /*
891 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
892 */
893 if (error == ENOENT)
894 error = 0;
895 return (error);
896}
897
898/*
899 * nfs file rename rpc called from above
900 */
901nfs_renameit(sndp, tndp)
902 register struct nameidata *sndp, *tndp;
903{
904 register u_long *p;
905 register caddr_t cp;
906 register long t1, t2;
907 caddr_t bpos, dpos;
908 u_long xid;
909 int error = 0;
910 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
911
912 nfsstats.rpccnt[NFSPROC_RENAME]++;
913 nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred,
914 (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+
915 nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/
916 nfsm_fhtom(sndp->ni_dvp);
917 nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
918 nfsm_fhtom(tndp->ni_dvp);
919 nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
920 nfsm_request(sndp->ni_dvp, nonidempotent[NFSPROC_RENAME]);
921 nfsm_reqdone;
922 VTONFS(sndp->ni_dvp)->n_direofstamp = 0;
923 VTONFS(tndp->ni_dvp)->n_direofstamp = 0;
924 return (error);
925}
926
927/*
928 * nfs hard link create call
929 */
930nfs_link(vp, ndp)
931 register struct vnode *vp;
932 register struct nameidata *ndp;
933{
934 register u_long *p;
935 register caddr_t cp;
936 register long t1, t2;
937 caddr_t bpos, dpos;
938 u_long xid;
939 int error = 0;
940 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
941
942 if (ndp->ni_dvp != vp)
943 nfs_lock(vp);
944 nfsstats.rpccnt[NFSPROC_LINK]++;
945 nfsm_reqhead(nfs_procids[NFSPROC_LINK], ndp->ni_cred,
946 NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
947 nfsm_fhtom(vp);
948 nfsm_fhtom(ndp->ni_dvp);
949 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
950 nfsm_request(vp, nonidempotent[NFSPROC_LINK]);
951 nfsm_reqdone;
952 VTONFS(vp)->n_attrstamp = 0;
953 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
954 if (ndp->ni_dvp != vp)
955 nfs_unlock(vp);
956 nfs_nput(ndp->ni_dvp);
957 /*
958 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
959 */
960 if (error == EEXIST)
961 error = 0;
962 return (error);
963}
964
965/*
966 * nfs symbolic link create call
967 */
968nfs_symlink(ndp, vap, nm)
969 struct nameidata *ndp;
970 struct vattr *vap;
971 char *nm; /* is this the path ?? */
972{
973 register struct nfsv2_sattr *sp;
974 register u_long *p;
975 register caddr_t cp;
976 register long t1, t2;
977 caddr_t bpos, dpos;
978 u_long xid;
979 int error = 0;
980 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
981
982 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
983 nfsm_reqhead(nfs_procids[NFSPROC_SYMLINK], ndp->ni_cred,
984 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_UNSIGNED);
985 nfsm_fhtom(ndp->ni_dvp);
986 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
987 nfsm_strtom(nm, strlen(nm), NFS_MAXPATHLEN);
988 nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
989 sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode);
990 sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
991 sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
992 sp->sa_size = txdr_unsigned(VNOVAL);
993 txdr_time(&vap->va_atime, &sp->sa_atime); /* or VNOVAL ?? */
994 txdr_time(&vap->va_mtime, &sp->sa_mtime); /* or VNOVAL ?? */
995 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_SYMLINK]);
996 nfsm_reqdone;
997 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
998 nfs_nput(ndp->ni_dvp);
999 /*
1000 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1001 */
1002 if (error == EEXIST)
1003 error = 0;
1004 return (error);
1005}
1006
1007/*
1008 * nfs make dir call
1009 */
1010nfs_mkdir(ndp, vap)
1011 register struct nameidata *ndp;
1012 struct vattr *vap;
1013{
1014 register struct nfsv2_sattr *sp;
1015 register u_long *p;
1016 register caddr_t cp;
1017 register long t1, t2;
1018 caddr_t bpos, dpos, cp2;
1019 u_long xid;
1020 int error = 0;
1021 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1022
1023 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1024 nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred,
1025 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
1026 nfsm_fhtom(ndp->ni_dvp);
1027 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
1028 nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
1029 sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
1030 sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
1031 sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
1032 sp->sa_size = txdr_unsigned(VNOVAL);
1033 txdr_time(&vap->va_atime, &sp->sa_atime); /* or VNOVAL ?? */
1034 txdr_time(&vap->va_mtime, &sp->sa_mtime); /* or VNOVAL ?? */
1035 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_MKDIR]);
1036 nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
1037 nfsm_reqdone;
1038 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
1039 nfs_nput(ndp->ni_dvp);
1040 /*
1041 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry.
1042 */
1043 if (error == EEXIST)
1044 error = 0;
1045 return (error);
1046}
1047
1048/*
1049 * nfs remove directory call
1050 */
1051nfs_rmdir(ndp)
1052 register struct nameidata *ndp;
1053{
1054 register u_long *p;
1055 register caddr_t cp;
1056 register long t1, t2;
1057 caddr_t bpos, dpos;
1058 u_long xid;
1059 int error = 0;
1060 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1061
1062 if (ndp->ni_dvp == ndp->ni_vp) {
1063 vrele(ndp->ni_dvp);
1064 nfs_nput(ndp->ni_dvp);
1065 return (EINVAL);
1066 }
1067 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1068 nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred,
1069 NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
1070 nfsm_fhtom(ndp->ni_dvp);
1071 nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
1072 nfsm_request(ndp->ni_dvp, nonidempotent[NFSPROC_RMDIR]);
1073 nfsm_reqdone;
1074 VTONFS(ndp->ni_dvp)->n_direofstamp = 0;
1075 cache_purge(ndp->ni_dvp);
1076 cache_purge(ndp->ni_vp);
1077 nfs_nput(ndp->ni_vp);
1078 nfs_nput(ndp->ni_dvp);
1079 /*
1080 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1081 */
1082 if (error == ENOENT)
1083 error = 0;
1084 return (error);
1085}
1086
1087/*
1088 * nfs readdir call
1089 * Although cookie is defined as opaque, I translate it to/from net byte
1090 * order so that it looks more sensible. This appears consistent with the
1091 * Ultrix implementation of NFS.
1092 */
1093nfs_readdir(vp, uiop, cred, eofflagp)
1094 register struct vnode *vp;
1095 struct uio *uiop;
1096 struct ucred *cred;
1097 int *eofflagp;
1098{
1099 register long len;
1100 register struct direct *dp;
1101 register u_long *p;
1102 register caddr_t cp;
1103 register long t1;
1104 long tlen;
1105 caddr_t bpos, dpos, cp2;
1106 u_long xid;
1107 int error = 0;
1108 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1109 struct mbuf *md2;
1110 caddr_t dpos2;
1111 int siz;
1112 int more_dirs = 1;
1113 off_t off, savoff;
1114 struct direct *savdp;
1115 struct nfsmount *nmp;
1116 struct nfsnode *np = VTONFS(vp);
1117 long tresid;
1118
1119 /*
1120 * First, check for hit on the EOF offset cache
1121 */
1122 if (uiop->uio_offset != 0 && uiop->uio_offset == np->n_direofoffset &&
1123 (time.tv_sec - np->n_direofstamp) < NFS_ATTRTIMEO) {
1124 *eofflagp = 1;
1125 nfsstats.direofcache_hits++;
1126 return (0);
1127 }
1128
1129 nmp = vfs_to_nfs(vp->v_mount);
1130 tresid = uiop->uio_resid;
1131 /*
1132 * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
1133 * whichever is smaller, truncated to a multiple of DIRBLKSIZ.
1134 * The stopping criteria is EOF or less than DIRBLKSIZ of the
1135 * request left.
1136 */
1137 while (more_dirs && uiop->uio_resid >= DIRBLKSIZ) {
1138 nfsstats.rpccnt[NFSPROC_READDIR]++;
1139 nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid);
1140 nfsm_fhtom(vp);
1141 nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
1142 *p++ = txdr_unsigned(uiop->uio_offset);
1143 *p = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
1144 nmp->nm_rsize : uiop->uio_resid) & ~(DIRBLKSIZ-1));
1145 nfsm_request(vp, nonidempotent[NFSPROC_READDIR]);
1146 siz = 0;
1147 nfsm_disect(p, u_long *, NFSX_UNSIGNED);
1148 more_dirs = fxdr_unsigned(int, *p);
1149
1150 /* Save the position so that we can do nfsm_mtouio() later */
1151 dpos2 = dpos;
1152 md2 = md;
1153
1154 /* loop thru the dir entries, doctoring them to 4bsd form */
1155 savoff = off = 0;
1156 savdp = dp = NULL;
1157 while (more_dirs && siz < uiop->uio_resid) {
1158 savoff = off; /* Hold onto offset and dp */
1159 savdp = dp;
1160 nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
1161 dp = (struct direct *)p;
1162 dp->d_ino = fxdr_unsigned(u_long, *p++);
1163 len = fxdr_unsigned(int, *p);
1164 if (len <= 0 || len > NFS_MAXNAMLEN) {
1165 error = EBADRPC;
1166 m_freem(mrep);
1167 goto nfsmout;
1168 }
1169 dp->d_namlen = (u_short)len;
1170 nfsm_adv(len); /* Point past name */
1171 tlen = nfsm_rndup(len);
1172 /*
1173 * This should not be necessary, but some servers have
1174 * broken XDR such that these bytes are not null filled.
1175 */
1176 if (tlen != len) {
1177 *dpos = '\0'; /* Null-terminate */
1178 nfsm_adv(tlen - len);
1179 len = tlen;
1180 }
1181 nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
1182 off = fxdr_unsigned(off_t, *p);
1183 *p++ = 0; /* Ensures null termination of name */
1184 more_dirs = fxdr_unsigned(int, *p);
1185 dp->d_reclen = len+4*NFSX_UNSIGNED;
1186 siz += dp->d_reclen;
1187 }
1188 /*
1189 * If at end of rpc data, get the eof boolean
1190 */
1191 if (!more_dirs) {
1192 nfsm_disecton(p, u_long *, NFSX_UNSIGNED);
1193 more_dirs = (fxdr_unsigned(int, *p) == 0);
1194
1195 /*
1196 * If at EOF, cache directory offset
1197 */
1198 if (!more_dirs) {
1199 np->n_direofoffset = off;
1200 np->n_direofstamp = time.tv_sec;
1201 }
1202 }
1203 /*
1204 * If there is too much to fit in the data buffer, use savoff and
1205 * savdp to trim off the last record.
1206 * --> we are not at eof
1207 */
1208 if (siz > uiop->uio_resid) {
1209 off = savoff;
1210 siz -= dp->d_reclen;
1211 dp = savdp;
1212 more_dirs = 0; /* Paranoia */
1213 }
1214 if (siz > 0) {
1215 md = md2;
1216 dpos = dpos2;
1217 nfsm_mtouio(uiop, siz);
1218 uiop->uio_offset = off;
1219 } else
1220 more_dirs = 0; /* Ugh, never happens, but in case.. */
1221 m_freem(mrep);
1222 }
1223nfsmout:
1224 if (tresid == uiop->uio_resid) {
1225 *eofflagp = 1;
1226 nfsstats.direofcache_misses++;
1227 } else {
1228 *eofflagp = 0;
1229 }
1230 return (error);
1231}
1232
1233/*
1234 * nfs statfs call
1235 * (Actually a vfsop, not a vnode op)
1236 */
1237nfs_statfs(mp, sbp)
1238 struct mount *mp;
1239 register struct statfs *sbp;
1240{
1241 register struct vnode *vp;
1242 register struct nfsv2_statfs *sfp;
1243 register caddr_t cp;
1244 register long t1;
1245 caddr_t bpos, dpos, cp2;
1246 u_long xid;
1247 int error = 0;
1248 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1249 struct nfsmount *nmp;
1250 struct ucred *cred;
1251 struct nfsnode *np;
1252
1253 nmp = vfs_to_nfs(mp);
1254 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
1255 return (error);
1256 vp = NFSTOV(np);
1257 nfsstats.rpccnt[NFSPROC_STATFS]++;
1258 cred = crget();
1259 cred->cr_ngroups = 1;
1260 nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH);
1261 nfsm_fhtom(vp);
1262 nfsm_request(vp, nonidempotent[NFSPROC_STATFS]);
1263 nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS);
1264 sbp->f_type = MOUNT_NFS;
1265 sbp->f_flags = nmp->nm_flag;
1266 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_tsize);
1267 sbp->f_fsize = fxdr_unsigned(long, sfp->sf_bsize);
1268 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
1269 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
1270 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
1271 sbp->f_files = 0;
1272 sbp->f_ffree = 0;
1273 sbp->f_fsid.val[0] = mp->m_fsid.val[0];
1274 sbp->f_fsid.val[1] = mp->m_fsid.val[1];
1275 bcopy(nmp->nm_path, sbp->f_mntonname, MNAMELEN);
1276 bcopy(nmp->nm_host, sbp->f_mntfromname, MNAMELEN);
1277 nfsm_reqdone;
1278 nfs_nput(vp);
1279 crfree(cred);
1280 return (error);
1281}
1282
1283static char hextoasc[] = "0123456789abcdef";
1284
1285/*
1286 * Silly rename. To make the NFS filesystem that is stateless look a little
1287 * more like the "ufs" a remove of an active vnode is translated to a rename
1288 * to a funny looking filename that is removed by nfs_inactive on the
1289 * nfsnode. There is the potential for another process on a different client
1290 * to create the same funny name between the nfs_lookitup() fails and the
1291 * nfs_rename() completes, but...
1292 */
1293nfs_sillyrename(ndp, flag)
1294 register struct nameidata *ndp;
1295 int flag;
1296{
1297 register struct nfsnode *np;
1298 register struct sillyrename *sp;
1299 register struct nameidata *tndp;
1300 int error;
1301 short pid;
1302
1303 np = VTONFS(ndp->ni_dvp);
1304 cache_purge(ndp->ni_dvp);
1305 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
1306 M_TEMP, M_WAITOK);
1307 sp->s_flag = flag;
1308 bcopy((caddr_t)&np->n_fh, (caddr_t)&sp->s_fh, NFSX_FH);
1309 np = VTONFS(ndp->ni_vp);
1310 tndp = &sp->s_namei;
1311 tndp->ni_cred = crdup(ndp->ni_cred);
1312
1313 /* Fudge together a funny name */
1314 pid = u.u_procp->p_pid;
1315 bcopy(".nfsAxxxx4.4", tndp->ni_dent.d_name, 13);
1316 tndp->ni_dent.d_namlen = 12;
1317 tndp->ni_dent.d_name[8] = hextoasc[pid & 0xf];
1318 tndp->ni_dent.d_name[7] = hextoasc[(pid >> 4) & 0xf];
1319 tndp->ni_dent.d_name[6] = hextoasc[(pid >> 8) & 0xf];
1320 tndp->ni_dent.d_name[5] = hextoasc[(pid >> 12) & 0xf];
1321
1322 /* Try lookitups until we get one that isn't there */
1323 while (nfs_lookitup(ndp->ni_dvp, tndp, (nfsv2fh_t *)0) == 0) {
1324 tndp->ni_dent.d_name[4]++;
1325 if (tndp->ni_dent.d_name[4] > 'z') {
1326 error = EINVAL;
1327 goto bad;
1328 }
1329 }
1330 if (error = nfs_renameit(ndp, tndp))
1331 goto bad;
1332 nfs_lookitup(ndp->ni_dvp, tndp, &np->n_fh);
1333 np->n_sillyrename = sp;
1334 return (0);
1335bad:
1336 crfree(tndp->ni_cred);
1337 free((caddr_t)sp, M_TEMP);
1338 return (error);
1339}
1340
1341/*
1342 * Look up a file name for silly rename stuff.
1343 * Just like nfs_lookup() except that it doesn't load returned values
1344 * into the nfsnode table.
1345 * If fhp != NULL it copies the returned file handle out
1346 */
1347nfs_lookitup(vp, ndp, fhp)
1348 register struct vnode *vp;
1349 register struct nameidata *ndp;
1350 nfsv2fh_t *fhp;
1351{
1352 register u_long *p;
1353 register caddr_t cp;
1354 register long t1, t2;
1355 caddr_t bpos, dpos, cp2;
1356 u_long xid;
1357 int error = 0;
1358 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1359 long len;
1360
1361 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1362 ndp->ni_dvp = vp;
1363 ndp->ni_vp = NULL;
1364 len = ndp->ni_dent.d_namlen;
1365 nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
1366 nfsm_fhtom(vp);
1367 nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
1368 nfsm_request(vp, nonidempotent[NFSPROC_LOOKUP]);
1369 if (fhp != NULL) {
1370 nfsm_disect(cp, caddr_t, NFSX_FH);
1371 bcopy(cp, (caddr_t)fhp, NFSX_FH);
1372 }
1373 nfsm_reqdone;
1374 return (error);
1375}
1376
1377/*
1378 * Kludge City..
1379 * - make nfs_bmap() essentially a no-op that does no translation
1380 * - do nfs_strategy() by faking physical I/O with nfs_readit/nfs_writeit
1381 * after mapping the physical addresses into Kernel Virtual space in the
1382 * nfsiobuf area.
1383 * (Maybe I could use the process's page mapping, but I was concerned that
1384 * Kernel Write might not be enabled and also figured copyout() would do
1385 * a lot more work than bcopy() and also it currently happens in the
1386 * context of the swapper process (2).
1387 */
1388nfs_bmap(vp, bn, vpp, bnp)
1389 struct vnode *vp;
1390 daddr_t bn;
1391 struct vnode **vpp;
1392 daddr_t *bnp;
1393{
1394 if (vpp != NULL)
1395 *vpp = vp;
1396 if (bnp != NULL)
1397 *bnp = bn * btodb(vp->v_mount->m_bsize);
1398 return (0);
1399}
1400
1401/*
1402 * Strategy routine for phys. i/o
1403 * If the biod's are running, queue a request
1404 * otherwise just call nfs_doio() to get it done
1405 */
1406nfs_strategy(bp)
1407 register struct buf *bp;
1408{
1409 register struct buf *dp;
1410 register int i;
1411 struct proc *rp;
1412 int error = 0;
1413 int fnd = 0;
1414
1415 /*
1416 * If an i/o daemon is waiting
1417 * queue the request, wake it up and wait for completion
1418 * otherwise just do it ourselves
1419 */
1420 for (i = 0; i < nfs_asyncdaemons; i++) {
1421 if (rp = nfs_iodwant[i]) {
1422 /*
1423 * Ensure that the async_daemon is still waiting here
1424 */
1425 if (rp->p_stat != SSLEEP ||
1426 rp->p_wchan != ((caddr_t)&nfs_iodwant[i])) {
1427 nfs_iodwant[i] = (struct proc *)0;
1428 continue;
1429 }
1430 dp = &nfs_bqueue;
1431 if (dp->b_actf == NULL) {
1432 dp->b_actl = bp;
1433 bp->b_actf = dp;
1434 } else {
1435 dp->b_actf->b_actl = bp;
1436 bp->b_actf = dp->b_actf;
1437 }
1438 dp->b_actf = bp;
1439 bp->b_actl = dp;
1440 fnd++;
1441 nfs_iodwant[i] = (struct proc *)0;
1442 wakeup((caddr_t)&nfs_iodwant[i]);
1443 break;
1444 }
1445 }
1446 if (!fnd)
1447 error = nfs_doio(bp);
1448 return (error);
1449}
1450
1451/*
1452 * Fun and games with i/o
1453 * Essentially play ubasetup() and disk interrupt service routine by
1454 * mapping the data buffer into kernel virtual space and doing the
1455 * nfs read or write rpc's from it.
1456 * If the biod's are not running, this is just called from nfs_strategy(),
1457 * otherwise it is called by the biod's to do what would normally be
1458 * partially disk interrupt driven.
1459 */
1460nfs_doio(bp)
1461 register struct buf *bp;
1462{
1463 register struct pte *pte, *ppte;
1464 register caddr_t vaddr;
1465 register struct uio *uiop;
1466 register struct vnode *vp;
1467 struct nfsnode *np;
1468 struct ucred *cr;
1469 int npf, npf2;
1470 int reg;
1471 caddr_t vbase;
1472 unsigned v;
1473 struct proc *rp;
1474 int o, error;
1475 struct uio uio;
1476 struct iovec io;
1477
1478 vp = bp->b_vp;
1479 np = VTONFS(vp);
1480 uiop = &uio;
1481 uiop->uio_iov = &io;
1482 uiop->uio_iovcnt = 1;
1483 uiop->uio_segflg = UIO_SYSSPACE;
1484
1485 /*
1486 * For phys i/o, map the b_addr into kernel virtual space using
1487 * the Nfsiomap pte's
1488 * Also, add a temporary b_rcred for reading using the process's uid
1489 * and a guess at a group
1490 */
1491 if (bp->b_flags & B_PHYS) {
1492 np->n_flag |= NPAGEDON;
1493 bp->b_rcred = cr = crget();
1494 rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc;
1495 cr->cr_uid = rp->p_uid;
1496 cr->cr_gid = 0; /* Anything ?? */
1497 cr->cr_ngroups = 1;
1498 o = (int)bp->b_un.b_addr & PGOFSET;
1499 npf2 = npf = btoc(bp->b_bcount + o);
1500
1501 /*
1502 * Get some mapping page table entries
1503 */
1504 while ((reg = rmalloc(nfsmap, (long)npf)) == 0) {
1505 nfsmap_want++;
1506 sleep((caddr_t)&nfsmap_want, PZERO-1);
1507 }
1508 reg--;
1509 if (bp->b_flags & B_PAGET)
1510 pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
1511 else {
1512 v = btop(bp->b_un.b_addr);
1513 if (bp->b_flags & B_UAREA)
1514 pte = &rp->p_addr[v];
1515 else
1516 pte = vtopte(rp, v);
1517 }
1518
1519 /*
1520 * Play vmaccess() but with the Nfsiomap page table
1521 */
1522 ppte = &Nfsiomap[reg];
1523 vbase = vaddr = &nfsiobuf[reg*NBPG];
1524 while (npf != 0) {
1525 mapin(ppte, (u_int)vaddr, pte->pg_pfnum, (int)(PG_V|PG_KW));
1526#if defined(tahoe)
1527 mtpr(P1DC, vaddr);
1528#endif
1529 ppte++;
1530 pte++;
1531 vaddr += NBPG;
1532 --npf;
1533 }
1534
1535 /*
1536 * And do the i/o rpc
1537 */
1538 io.iov_base = vbase+o;
1539 io.iov_len = uiop->uio_resid = bp->b_bcount;
1540 uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1541 if (bp->b_flags & B_READ) {
1542 uiop->uio_rw = UIO_READ;
1543 nfsstats.read_physios++;
1544 bp->b_error = error = nfs_readrpc(vp, uiop, bp->b_rcred);
1545 /*
1546 * If a text file has been modified since it was exec'd
1547 * blow the process' out of the water. This is the
1548 * closest we can come to "Text File Busy" in good old
1549 * stateless nfs.
1550 */
1551 if ((vp->v_flag & VTEXT) &&
1552 (vp->v_text->x_mtime != np->n_vattr.va_mtime.tv_sec))
1553 xinval(vp);
1554 } else {
1555 uiop->uio_rw = UIO_WRITE;
1556 nfsstats.write_physios++;
1557 bp->b_error = error = nfs_writerpc(vp, uiop, bp->b_wcred);
1558 }
1559
1560 /*
1561 * Finally, release pte's used by physical i/o
1562 */
1563 crfree(cr);
1564 rmfree(nfsmap, (long)npf2, (long)++reg);
1565 if (nfsmap_want) {
1566 nfsmap_want = 0;
1567 wakeup((caddr_t)&nfsmap_want);
1568 }
1569 } else {
1570 if (bp->b_flags & B_READ) {
1571 io.iov_len = uiop->uio_resid = bp->b_bcount;
1572 uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
1573 io.iov_base = bp->b_un.b_addr;
1574 uiop->uio_rw = UIO_READ;
1575 nfsstats.read_bios++;
1576 bp->b_error = error = nfs_readrpc(vp, uiop, bp->b_rcred);
1577 } else {
1578 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1579 - bp->b_dirtyoff;
1580 uiop->uio_offset = (bp->b_blkno * DEV_BSIZE)
1581 + bp->b_dirtyoff;
1582 io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff;
1583 uiop->uio_rw = UIO_WRITE;
1584 nfsstats.write_bios++;
1585 bp->b_error = error = nfs_writerpc(vp, uiop, bp->b_wcred);
1586 if (error) {
1587 np->n_error = error;
1588 np->n_flag |= NWRITEERR;
1589 }
1590 bp->b_dirtyoff = bp->b_dirtyend = 0;
1591 }
1592 }
1593 if (error)
1594 bp->b_flags |= B_ERROR;
1595 bp->b_resid = uiop->uio_resid;
1596 biodone(bp);
1597 return (error);
1598}
1599
1600/*
1601 * Flush all the blocks associated with a vnode.
1602 * Walk through the buffer pool and push any dirty pages
1603 * associated with the vnode.
1604 */
1605/* ARGSUSED */
1606nfs_fsync(vp, fflags, cred, waitfor)
1607 register struct vnode *vp;
1608 int fflags;
1609 struct ucred *cred;
1610 int waitfor;
1611{
1612 register struct nfsnode *np = VTONFS(vp);
1613 int error = 0;
1614
1615 if (np->n_flag & NMODIFIED) {
1616 np->n_flag &= ~NMODIFIED;
1617 vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
1618 }
1619 if (!error && (np->n_flag & NWRITEERR))
1620 error = np->n_error;
1621 return (error);
1622}
1623
1624/*
1625 * Print out the contents of an nfsnode.
1626 */
1627nfs_print(vp)
1628 struct vnode *vp;
1629{
1630 register struct nfsnode *np = VTONFS(vp);
1631
1632 printf("tag VT_NFS, fileid %d fsid 0x%x",
1633 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
1634#ifdef FIFO
1635 if (vp->v_type == VFIFO)
1636 fifo_printinfo(vp);
1637#endif /* FIFO */
1638 printf("%s\n", (np->n_flag & NLOCKED) ? " (LOCKED)" : "");
1639 if (np->n_lockholder == 0)
1640 return;
1641 printf("\towner pid %d", np->n_lockholder);
1642 if (np->n_lockwaiter)
1643 printf(" waiting pid %d", np->n_lockwaiter);
1644 printf("\n");
1645}