4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / sys / nfs / nfs_vfsops.c
CommitLineData
a2907882 1/*
99315dca
KB
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
a2907882
KM
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
dbf0c423 8 * %sccs.include.redist.c%
a2907882 9 *
99315dca 10 * @(#)nfs_vfsops.c 8.1 (Berkeley) %G%
a2907882
KM
11 */
12
5548a02f
KB
13#include <sys/param.h>
14#include <sys/conf.h>
15#include <sys/ioctl.h>
16#include <sys/signal.h>
17#include <sys/proc.h>
18#include <sys/namei.h>
19#include <sys/vnode.h>
20#include <sys/kernel.h>
21#include <sys/mount.h>
22#include <sys/buf.h>
23#include <sys/mbuf.h>
24#include <sys/socket.h>
25#include <sys/systm.h>
058dee65 26
5548a02f
KB
27#include <net/if.h>
28#include <net/route.h>
29#include <netinet/in.h>
058dee65 30
5548a02f
KB
31#include <nfs/rpcv2.h>
32#include <nfs/nfsv2.h>
33#include <nfs/nfsnode.h>
34#include <nfs/nfsmount.h>
35#include <nfs/nfs.h>
36#include <nfs/xdr_subs.h>
37#include <nfs/nfsm_subs.h>
38#include <nfs/nfsdiskless.h>
39#include <nfs/nqnfs.h>
a2907882 40
a2907882
KM
41/*
42 * nfs vfs operations.
43 */
a2907882
KM
44struct vfsops nfs_vfsops = {
45 nfs_mount,
b3226026 46 nfs_start,
a2907882
KM
47 nfs_unmount,
48 nfs_root,
56cf2c46 49 nfs_quotactl,
a2907882
KM
50 nfs_statfs,
51 nfs_sync,
2cd82738 52 nfs_vget,
a2907882
KM
53 nfs_fhtovp,
54 nfs_vptofh,
363ac00e 55 nfs_init,
a2907882
KM
56};
57
2c5b44a2
KM
58/*
59 * This structure must be filled in by a primary bootstrap or bootstrap
60 * server for a diskless/dataless machine. It is initialized below just
61 * to ensure that it is allocated to initialized data (.data not .bss).
62 */
63struct nfs_diskless nfs_diskless = { 0 };
64
e8540f59 65static u_char nfs_mntid;
f0f1cbaa
KM
66extern u_long nfs_procids[NFS_NPROCS];
67extern u_long nfs_prog, nfs_vers;
c465e0e7
CT
68void nfs_disconnect __P((struct nfsmount *));
69void nfsargs_ntoh __P((struct nfs_args *));
70static struct mount *nfs_mountdiskless __P((char *, char *, int,
71 struct sockaddr_in *, struct nfs_args *, register struct vnode **));
f0f1cbaa
KM
72
73#define TRUE 1
74#define FALSE 0
75
76/*
77 * nfs statfs call
78 */
c465e0e7 79int
4ebfcce2 80nfs_statfs(mp, sbp, p)
f0f1cbaa
KM
81 struct mount *mp;
82 register struct statfs *sbp;
4ebfcce2 83 struct proc *p;
f0f1cbaa
KM
84{
85 register struct vnode *vp;
86 register struct nfsv2_statfs *sfp;
87 register caddr_t cp;
88 register long t1;
89 caddr_t bpos, dpos, cp2;
41f343df 90 int error = 0, isnq;
f0f1cbaa
KM
91 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
92 struct nfsmount *nmp;
93 struct ucred *cred;
94 struct nfsnode *np;
95
96 nmp = VFSTONFS(mp);
41f343df 97 isnq = (nmp->nm_flag & NFSMNT_NQNFS);
f0f1cbaa
KM
98 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
99 return (error);
100 vp = NFSTOV(np);
101 nfsstats.rpccnt[NFSPROC_STATFS]++;
102 cred = crget();
103 cred->cr_ngroups = 1;
2c5b44a2 104 nfsm_reqhead(vp, NFSPROC_STATFS, NFSX_FH);
f0f1cbaa 105 nfsm_fhtom(vp);
2c5b44a2 106 nfsm_request(vp, NFSPROC_STATFS, p, cred);
41f343df 107 nfsm_dissect(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
f0f1cbaa
KM
108 sbp->f_type = MOUNT_NFS;
109 sbp->f_flags = nmp->nm_flag;
2c5b44a2 110 sbp->f_iosize = NFS_MAXDGRAMDATA;
a22e809c 111 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
f0f1cbaa
KM
112 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
113 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
114 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
41f343df
KM
115 if (isnq) {
116 sbp->f_files = fxdr_unsigned(long, sfp->sf_files);
117 sbp->f_ffree = fxdr_unsigned(long, sfp->sf_ffree);
118 } else {
119 sbp->f_files = 0;
120 sbp->f_ffree = 0;
121 }
f0f1cbaa
KM
122 if (sbp != &mp->mnt_stat) {
123 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
124 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
125 }
126 nfsm_reqdone;
2c5b44a2 127 vrele(vp);
f0f1cbaa
KM
128 crfree(cred);
129 return (error);
130}
a2907882
KM
131
132/*
1c89915d
KM
133 * Mount a remote root fs via. nfs. This depends on the info in the
134 * nfs_diskless structure that has been filled in properly by some primary
135 * bootstrap.
136 * It goes something like this:
137 * - do enough of "ifconfig" by calling ifioctl() so that the system
138 * can talk to the server
139 * - If nfs_diskless.mygateway is filled in, use that address as
140 * a default gateway.
1c89915d 141 * - hand craft the swap nfs vnode hanging off a fake mount point
2c5b44a2 142 * if swdevt[0].sw_dev == NODEV
1c89915d 143 * - build the rootfs mount point and call mountnfs() to do the rest.
a2907882 144 */
c465e0e7 145int
a2907882
KM
146nfs_mountroot()
147{
1c89915d 148 register struct mount *mp;
c465e0e7 149 register struct nfs_diskless *nd = &nfs_diskless;
1c89915d
KM
150 struct socket *so;
151 struct vnode *vp;
c465e0e7 152 struct proc *p = curproc; /* XXX */
968f7e55 153 int error, i, s;
1c89915d 154
c465e0e7
CT
155 /*
156 * XXX time must be non-zero when we init the interface or else
157 * the arp code will wedge...
158 */
159 if (time.tv_sec == 0)
160 time.tv_sec = 1;
161
162#ifdef notyet
163 /* Set up swap credentials. */
c33e9e8b
KM
164 proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
165 proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
166 if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
167 NGROUPS)
168 proc0.p_ucred->cr_ngroups = NGROUPS;
169 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
170 proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
c465e0e7
CT
171#endif
172
1c89915d 173 /*
2c5b44a2 174 * Do enough of ifconfig(8) so that the critical net interface can
1c89915d
KM
175 * talk to the server.
176 */
c465e0e7
CT
177 if (error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0))
178 panic("nfs_mountroot: socreate: %d", error);
179 if (error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p))
180 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
1c89915d
KM
181 soclose(so);
182
183 /*
184 * If the gateway field is filled in, set it as the default route.
185 */
c465e0e7 186 if (nd->mygateway.sin_len != 0) {
968f7e55 187 struct sockaddr_in mask, sin;
2c5b44a2 188
968f7e55
KS
189 bzero((caddr_t)&mask, sizeof(mask));
190 sin = mask;
2c5b44a2 191 sin.sin_family = AF_INET;
968f7e55 192 sin.sin_len = sizeof(sin);
c465e0e7
CT
193 if (error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
194 (struct sockaddr *)&nd->mygateway,
968f7e55 195 (struct sockaddr *)&mask,
c465e0e7
CT
196 RTF_UP | RTF_GATEWAY, (struct rtentry **)0))
197 panic("nfs_mountroot: RTM_ADD: %d", error);
1c89915d 198 }
1c89915d
KM
199
200 /*
201 * If swapping to an nfs node (indicated by swdevt[0].sw_dev == NODEV):
202 * Create a fake mount point just for the swap vnode so that the
203 * swap file can be on a different server from the rootfs.
204 */
205 if (swdevt[0].sw_dev == NODEV) {
c465e0e7
CT
206 nd->swap_args.fh = (nfsv2fh_t *)nd->swap_fh;
207 (void) nfs_mountdiskless(nd->swap_hostnam, "/swap", 0,
208 &nd->swap_saddr, &nd->swap_args, &vp);
1c89915d
KM
209
210 /*
1c89915d
KM
211 * Since the swap file is not the root dir of a file system,
212 * hack it to a regular file.
213 */
1c89915d
KM
214 vp->v_type = VREG;
215 vp->v_flag = 0;
216 swapdev_vp = vp;
217 VREF(vp);
218 swdevt[0].sw_vp = vp;
c465e0e7 219 swdevt[0].sw_nblks = ntohl(nd->swap_nblks);
751df505
KM
220 } else if (bdevvp(swapdev, &swapdev_vp))
221 panic("nfs_mountroot: can't setup swapdev_vp");
1c89915d
KM
222
223 /*
224 * Create the rootfs mount point.
225 */
c465e0e7
CT
226 nd->root_args.fh = (nfsv2fh_t *)nd->root_fh;
227 mp = nfs_mountdiskless(nd->root_hostnam, "/", MNT_RDONLY,
228 &nd->root_saddr, &nd->root_args, &vp);
1c89915d 229
1c89915d 230 if (vfs_lock(mp))
c465e0e7 231 panic("nfs_mountroot: vfs_lock");
1c89915d
KM
232 rootfs = mp;
233 mp->mnt_next = mp;
234 mp->mnt_prev = mp;
235 mp->mnt_vnodecovered = NULLVP;
236 vfs_unlock(mp);
237 rootvp = vp;
26e1e7cf
KM
238
239 /*
240 * This is not really an nfs issue, but it is much easier to
241 * set hostname here and then let the "/etc/rc.xxx" files
242 * mount the right /var based upon its preset value.
243 */
c465e0e7 244 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
26e1e7cf
KM
245 hostname[MAXHOSTNAMELEN - 1] = '\0';
246 for (i = 0; i < MAXHOSTNAMELEN; i++)
247 if (hostname[i] == '\0')
248 break;
249 hostnamelen = i;
c33e9e8b 250 inittodr(ntohl(nd->root_time));
1c89915d 251 return (0);
a2907882
KM
252}
253
c465e0e7
CT
254/*
255 * Internal version of mount system call for diskless setup.
256 */
257static struct mount *
258nfs_mountdiskless(path, which, mountflag, sin, args, vpp)
259 char *path;
260 char *which;
261 int mountflag;
262 struct sockaddr_in *sin;
263 struct nfs_args *args;
264 register struct vnode **vpp;
265{
266 register struct mount *mp;
267 register struct mbuf *m;
268 register int error;
269
270 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
271 M_MOUNT, M_NOWAIT);
272 if (mp == NULL)
273 panic("nfs_mountroot: %s mount malloc", which);
274 mp->mnt_op = &nfs_vfsops;
275 mp->mnt_flag = mountflag;
276 mp->mnt_mounth = NULLVP;
277
278 MGET(m, MT_SONAME, M_DONTWAIT);
279 if (m == NULL)
280 panic("nfs_mountroot: %s mount mbuf", which);
281 bcopy((caddr_t)sin, mtod(m, caddr_t), sin->sin_len);
282 m->m_len = sin->sin_len;
283 nfsargs_ntoh(args);
284 if (error = mountnfs(args, mp, m, which, path, vpp))
285 panic("nfs_mountroot: mount %s on %s: %d", path, which, error);
286
287 return (mp);
288}
289
2c5b44a2
KM
290/*
291 * Convert the integer fields of the nfs_args structure from net byte order
292 * to host byte order. Called by nfs_mountroot() above.
293 */
294void
295nfsargs_ntoh(nfsp)
296 register struct nfs_args *nfsp;
297{
298
299 NTOHL(nfsp->sotype);
300 NTOHL(nfsp->proto);
301 NTOHL(nfsp->flags);
302 NTOHL(nfsp->wsize);
303 NTOHL(nfsp->rsize);
304 NTOHL(nfsp->timeo);
305 NTOHL(nfsp->retrans);
306 NTOHL(nfsp->maxgrouplist);
307 NTOHL(nfsp->readahead);
308 NTOHL(nfsp->leaseterm);
309 NTOHL(nfsp->deadthresh);
310}
311
a2907882
KM
312/*
313 * VFS Operations.
314 *
315 * mount system call
316 * It seems a bit dumb to copyinstr() the host and path here and then
317 * bcopy() them in mountnfs(), but I wanted to detect errors before
318 * doing the sockargs() call because sockargs() allocates an mbuf and
319 * an error after that means that I have to release the mbuf.
320 */
0bd503ad 321/* ARGSUSED */
c465e0e7 322int
4ebfcce2 323nfs_mount(mp, path, data, ndp, p)
a2907882
KM
324 struct mount *mp;
325 char *path;
326 caddr_t data;
327 struct nameidata *ndp;
4ebfcce2 328 struct proc *p;
a2907882
KM
329{
330 int error;
331 struct nfs_args args;
f0f1cbaa 332 struct mbuf *nam;
1c89915d 333 struct vnode *vp;
a2907882 334 char pth[MNAMELEN], hst[MNAMELEN];
4f76a9f0 335 u_int len;
a2907882
KM
336 nfsv2fh_t nfh;
337
338 if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)))
339 return (error);
4f76a9f0 340 if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t)))
a2907882
KM
341 return (error);
342 if (error = copyinstr(path, pth, MNAMELEN-1, &len))
343 return (error);
4f76a9f0 344 bzero(&pth[len], MNAMELEN - len);
a2907882
KM
345 if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len))
346 return (error);
4f76a9f0 347 bzero(&hst[len], MNAMELEN - len);
a2907882 348 /* sockargs() call must be after above copyin() calls */
f0f1cbaa 349 if (error = sockargs(&nam, (caddr_t)args.addr,
2c5b44a2 350 args.addrlen, MT_SONAME))
a2907882
KM
351 return (error);
352 args.fh = &nfh;
1c89915d 353 error = mountnfs(&args, mp, nam, pth, hst, &vp);
a2907882
KM
354 return (error);
355}
356
357/*
358 * Common code for mount and mountroot
359 */
c465e0e7 360int
1c89915d 361mountnfs(argp, mp, nam, pth, hst, vpp)
a2907882
KM
362 register struct nfs_args *argp;
363 register struct mount *mp;
f0f1cbaa 364 struct mbuf *nam;
a2907882 365 char *pth, *hst;
1c89915d 366 struct vnode **vpp;
a2907882
KM
367{
368 register struct nfsmount *nmp;
9fa46e39 369 struct nfsnode *np;
a2907882 370 int error;
98fb8dd3 371 fsid_t tfsid;
a2907882 372
c465e0e7
CT
373 if (mp->mnt_flag & MNT_UPDATE) {
374 nmp = VFSTONFS(mp);
375 /* update paths, file handles, etc, here XXX */
376 m_freem(nam);
377 return (0);
378 } else {
379 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
380 M_NFSMNT, M_WAITOK);
381 bzero((caddr_t)nmp, sizeof (struct nfsmount));
382 mp->mnt_data = (qaddr_t)nmp;
383 }
6b9617ff 384 getnewfsid(mp, MOUNT_NFS);
a2907882
KM
385 nmp->nm_mountp = mp;
386 nmp->nm_flag = argp->flags;
2c5b44a2
KM
387 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_MYWRITE)) ==
388 (NFSMNT_NQNFS | NFSMNT_MYWRITE)) {
389 error = EPERM;
390 goto bad;
391 }
11488b46 392 if (nmp->nm_flag & NFSMNT_NQNFS)
460cf449
KM
393 /*
394 * We have to set mnt_maxsymlink to a non-zero value so
395 * that COMPAT_43 routines will know that we are setting
396 * the d_type field in directories (and can zero it for
397 * unsuspecting binaries).
398 */
399 mp->mnt_maxsymlinklen = 1;
2c5b44a2 400 nmp->nm_timeo = NFS_TIMEO;
98fb8dd3
KM
401 nmp->nm_retry = NFS_RETRANS;
402 nmp->nm_wsize = NFS_WSIZE;
403 nmp->nm_rsize = NFS_RSIZE;
2c5b44a2
KM
404 nmp->nm_numgrps = NFS_MAXGRPS;
405 nmp->nm_readahead = NFS_DEFRAHEAD;
406 nmp->nm_leaseterm = NQ_DEFLEASE;
407 nmp->nm_deadthresh = NQ_DEADTHRESH;
408 nmp->nm_tnext = (struct nfsnode *)nmp;
409 nmp->nm_tprev = (struct nfsnode *)nmp;
410 nmp->nm_inprog = NULLVP;
98fb8dd3 411 bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t));
54fb9dc2
KM
412 mp->mnt_stat.f_type = MOUNT_NFS;
413 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
414 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
f0f1cbaa 415 nmp->nm_nam = nam;
98fb8dd3
KM
416
417 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
2c5b44a2
KM
418 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
419 if (nmp->nm_timeo < NFS_MINTIMEO)
420 nmp->nm_timeo = NFS_MINTIMEO;
421 else if (nmp->nm_timeo > NFS_MAXTIMEO)
422 nmp->nm_timeo = NFS_MAXTIMEO;
98fb8dd3
KM
423 }
424
d8792713 425 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
98fb8dd3
KM
426 nmp->nm_retry = argp->retrans;
427 if (nmp->nm_retry > NFS_MAXREXMIT)
428 nmp->nm_retry = NFS_MAXREXMIT;
429 }
430
431 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
a2907882 432 nmp->nm_wsize = argp->wsize;
98fb8dd3
KM
433 /* Round down to multiple of blocksize */
434 nmp->nm_wsize &= ~0x1ff;
435 if (nmp->nm_wsize <= 0)
436 nmp->nm_wsize = 512;
437 else if (nmp->nm_wsize > NFS_MAXDATA)
438 nmp->nm_wsize = NFS_MAXDATA;
439 }
d8792713
KM
440 if (nmp->nm_wsize > MAXBSIZE)
441 nmp->nm_wsize = MAXBSIZE;
98fb8dd3
KM
442
443 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
a2907882 444 nmp->nm_rsize = argp->rsize;
98fb8dd3
KM
445 /* Round down to multiple of blocksize */
446 nmp->nm_rsize &= ~0x1ff;
447 if (nmp->nm_rsize <= 0)
448 nmp->nm_rsize = 512;
449 else if (nmp->nm_rsize > NFS_MAXDATA)
450 nmp->nm_rsize = NFS_MAXDATA;
451 }
d8792713
KM
452 if (nmp->nm_rsize > MAXBSIZE)
453 nmp->nm_rsize = MAXBSIZE;
2c5b44a2
KM
454 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
455 argp->maxgrouplist <= NFS_MAXGRPS)
456 nmp->nm_numgrps = argp->maxgrouplist;
457 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
458 argp->readahead <= NFS_MAXRAHEAD)
459 nmp->nm_readahead = argp->readahead;
460 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
461 argp->leaseterm <= NQ_MAXLEASE)
462 nmp->nm_leaseterm = argp->leaseterm;
463 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
464 argp->deadthresh <= NQ_NEVERDEAD)
465 nmp->nm_deadthresh = argp->deadthresh;
98fb8dd3 466 /* Set up the sockets and per-host congestion */
f0f1cbaa
KM
467 nmp->nm_sotype = argp->sotype;
468 nmp->nm_soproto = argp->proto;
98fb8dd3 469
2c5b44a2
KM
470 /*
471 * For Connection based sockets (TCP,...) defer the connect until
472 * the first request, in case the server is not responding.
473 */
474 if (nmp->nm_sotype == SOCK_DGRAM &&
475 (error = nfs_connect(nmp, (struct nfsreq *)0)))
efaa4294 476 goto bad;
2c5b44a2
KM
477
478 /*
479 * This is silly, but it has to be set so that vinifod() works.
480 * We do not want to do an nfs_statfs() here since we can get
481 * stuck on a dead server and we are holding a lock on the mount
482 * point.
483 */
484 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
9fa46e39
KM
485 /*
486 * A reference count is needed on the nfsnode representing the
487 * remote root. If this object is not persistent, then backward
488 * traversals of the mount point (i.e. "..") will not work if
489 * the nfsnode gets flushed out of the cache. Ufs does not have
490 * this problem, because one can identify root inodes by their
491 * number == ROOTINO (2).
492 */
493 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
494 goto bad;
1c89915d 495 *vpp = NFSTOV(np);
efaa4294 496
f0f1cbaa 497 return (0);
a2907882 498bad:
98fb8dd3 499 nfs_disconnect(nmp);
2c5b44a2 500 free((caddr_t)nmp, M_NFSMNT);
f0f1cbaa 501 m_freem(nam);
a2907882
KM
502 return (error);
503}
504
505/*
506 * unmount system call
507 */
c465e0e7 508int
4ebfcce2 509nfs_unmount(mp, mntflags, p)
a2907882 510 struct mount *mp;
56cf2c46 511 int mntflags;
4ebfcce2 512 struct proc *p;
a2907882
KM
513{
514 register struct nfsmount *nmp;
9fa46e39 515 struct nfsnode *np;
98fb8dd3 516 struct vnode *vp;
6556ebbd
KM
517 int error, flags = 0;
518 extern int doforce;
a2907882 519
87be6db2 520 if (mntflags & MNT_FORCE) {
6556ebbd 521 if (!doforce || mp == rootfs)
87be6db2 522 return (EINVAL);
56cf2c46 523 flags |= FORCECLOSE;
87be6db2 524 }
54fb9dc2 525 nmp = VFSTONFS(mp);
a2907882
KM
526 /*
527 * Goes something like this..
98fb8dd3
KM
528 * - Check for activity on the root vnode (other than ourselves).
529 * - Call vflush() to clear out vnodes for this file system,
530 * except for the root vnode.
531 * - Decrement reference on the vnode representing remote root.
a2907882
KM
532 * - Close the socket
533 * - Free up the data structures
534 */
9fa46e39
KM
535 /*
536 * We need to decrement the ref. count on the nfsnode representing
537 * the remote root. See comment in mountnfs(). The VFS unmount()
538 * has done vput on this vnode, otherwise we would get deadlock!
539 */
540 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
541 return(error);
98fb8dd3
KM
542 vp = NFSTOV(np);
543 if (vp->v_usecount > 2) {
544 vput(vp);
545 return (EBUSY);
546 }
2c5b44a2
KM
547
548 /*
549 * Must handshake with nqnfs_clientd() if it is active.
550 */
551 nmp->nm_flag |= NFSMNT_DISMINPROG;
552 while (nmp->nm_inprog != NULLVP)
553 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
98fb8dd3
KM
554 if (error = vflush(mp, vp, flags)) {
555 vput(vp);
2c5b44a2 556 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
a2907882 557 return (error);
98fb8dd3 558 }
2c5b44a2
KM
559
560 /*
561 * We are now committed to the unmount.
562 * For NQNFS, let the server daemon free the nfsmount structure.
563 */
564 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
565 nmp->nm_flag |= NFSMNT_DISMNT;
566
a2907882 567 /*
2c5b44a2 568 * There are two reference counts to get rid of here.
a2907882 569 */
98fb8dd3 570 vrele(vp);
2c5b44a2 571 vrele(vp);
8473f8d7 572 vgone(vp);
98fb8dd3 573 nfs_disconnect(nmp);
f0f1cbaa 574 m_freem(nmp->nm_nam);
2c5b44a2
KM
575
576 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
577 free((caddr_t)nmp, M_NFSMNT);
a2907882
KM
578 return (0);
579}
580
581/*
582 * Return root of a filesystem
583 */
c465e0e7 584int
a2907882
KM
585nfs_root(mp, vpp)
586 struct mount *mp;
587 struct vnode **vpp;
588{
589 register struct vnode *vp;
590 struct nfsmount *nmp;
591 struct nfsnode *np;
592 int error;
593
54fb9dc2 594 nmp = VFSTONFS(mp);
a2907882
KM
595 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
596 return (error);
597 vp = NFSTOV(np);
598 vp->v_type = VDIR;
599 vp->v_flag = VROOT;
600 *vpp = vp;
601 return (0);
602}
603
9238aa59
RM
604extern int syncprt;
605
a2907882 606/*
9238aa59 607 * Flush out the buffer cache
a2907882 608 */
0bd503ad 609/* ARGSUSED */
c465e0e7 610int
050651c3 611nfs_sync(mp, waitfor, cred, p)
a2907882
KM
612 struct mount *mp;
613 int waitfor;
050651c3
KM
614 struct ucred *cred;
615 struct proc *p;
a2907882 616{
050651c3
KM
617 register struct vnode *vp;
618 int error, allerror = 0;
619
9238aa59
RM
620 /*
621 * Force stale buffer cache information to be flushed.
622 */
050651c3
KM
623loop:
624 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
625 /*
626 * If the vnode that we are about to sync is no longer
627 * associated with this mount point, start over.
628 */
629 if (vp->v_mount != mp)
630 goto loop;
bd60d0f0 631 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.le_next == NULL)
050651c3
KM
632 continue;
633 if (vget(vp))
634 goto loop;
635 if (error = VOP_FSYNC(vp, cred, waitfor, p))
636 allerror = error;
637 vput(vp);
638 }
639 return (allerror);
a2907882
KM
640}
641
2cd82738
KM
642/*
643 * NFS flat namespace lookup.
644 * Currently unsupported.
645 */
646/* ARGSUSED */
647int
648nfs_vget(mp, ino, vpp)
649 struct mount *mp;
650 ino_t ino;
651 struct vnode **vpp;
652{
653
654 return (EOPNOTSUPP);
655}
656
a2907882
KM
657/*
658 * At this point, this should never happen
659 */
0bd503ad 660/* ARGSUSED */
c465e0e7 661int
9580c523
KM
662nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
663 register struct mount *mp;
a2907882 664 struct fid *fhp;
9580c523 665 struct mbuf *nam;
a2907882 666 struct vnode **vpp;
9580c523
KM
667 int *exflagsp;
668 struct ucred **credanonp;
a2907882 669{
0bd503ad 670
a2907882
KM
671 return (EINVAL);
672}
673
674/*
675 * Vnode pointer to File handle, should never happen either
676 */
0bd503ad 677/* ARGSUSED */
c465e0e7 678int
4ebfcce2
KM
679nfs_vptofh(vp, fhp)
680 struct vnode *vp;
a2907882 681 struct fid *fhp;
a2907882 682{
0bd503ad 683
a2907882
KM
684 return (EINVAL);
685}
9238aa59
RM
686
687/*
688 * Vfs start routine, a no-op.
689 */
0bd503ad 690/* ARGSUSED */
c465e0e7 691int
4ebfcce2 692nfs_start(mp, flags, p)
9238aa59
RM
693 struct mount *mp;
694 int flags;
4ebfcce2 695 struct proc *p;
9238aa59 696{
0bd503ad 697
9238aa59
RM
698 return (0);
699}
56cf2c46
KM
700
701/*
702 * Do operations associated with quotas, not supported
703 */
e0f6df7b 704/* ARGSUSED */
c465e0e7 705int
4ebfcce2 706nfs_quotactl(mp, cmd, uid, arg, p)
56cf2c46
KM
707 struct mount *mp;
708 int cmd;
050651c3 709 uid_t uid;
56cf2c46 710 caddr_t arg;
4ebfcce2 711 struct proc *p;
56cf2c46 712{
e0f6df7b 713
56cf2c46
KM
714 return (EOPNOTSUPP);
715}