138414Smckusick /* 238414Smckusick * Copyright (c) 1989 The Regents of the University of California. 338414Smckusick * All rights reserved. 438414Smckusick * 538414Smckusick * This code is derived from software contributed to Berkeley by 638414Smckusick * Rick Macklem at The University of Guelph. 738414Smckusick * 838414Smckusick * Redistribution and use in source and binary forms are permitted 938414Smckusick * provided that the above copyright notice and this paragraph are 1038414Smckusick * duplicated in all such forms and that any documentation, 1138414Smckusick * advertising materials, and other materials related to such 1238414Smckusick * distribution and use acknowledge that the software was developed 1338414Smckusick * by the University of California, Berkeley. The name of the 1438414Smckusick * University may not be used to endorse or promote products derived 1538414Smckusick * from this software without specific prior written permission. 1638414Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1738414Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1838414Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1938414Smckusick * 20*40353Smckusick * @(#)nfs_vfsops.c 7.17 (Berkeley) 03/06/90 2138414Smckusick */ 2238414Smckusick 2338414Smckusick #include "param.h" 2438414Smckusick #include "signal.h" 2538414Smckusick #include "user.h" 2638414Smckusick #include "proc.h" 2738414Smckusick #include "vnode.h" 2838414Smckusick #include "mount.h" 2938414Smckusick #include "errno.h" 3040120Smckusick #include "buf.h" 3138414Smckusick #include "mbuf.h" 3238414Smckusick #undef m_data 3338414Smckusick #include "socket.h" 3440120Smckusick #include "systm.h" 3538414Smckusick #include "nfsv2.h" 3638414Smckusick #include "nfsnode.h" 3738414Smckusick #include "nfsmount.h" 3838414Smckusick #include "nfs.h" 3938414Smckusick 4038414Smckusick /* 4138414Smckusick * nfs vfs operations. 4238414Smckusick */ 4338414Smckusick int nfs_mount(); 4438874Smckusick int nfs_start(); 4538414Smckusick int nfs_unmount(); 4638414Smckusick int nfs_root(); 4739443Smckusick int nfs_statfs(); 4838414Smckusick int nfs_sync(); 4938414Smckusick int nfs_fhtovp(); 5038414Smckusick int nfs_vptofh(); 5139443Smckusick int nfs_init(); 5238414Smckusick 5338414Smckusick struct vfsops nfs_vfsops = { 5438414Smckusick nfs_mount, 5538874Smckusick nfs_start, 5638414Smckusick nfs_unmount, 5738414Smckusick nfs_root, 5838414Smckusick nfs_statfs, 5938414Smckusick nfs_sync, 6038414Smckusick nfs_fhtovp, 6138414Smckusick nfs_vptofh, 6239443Smckusick nfs_init, 6338414Smckusick }; 6438414Smckusick 6539757Smckusick static u_char nfs_mntid; 6638414Smckusick 6738414Smckusick /* 6838414Smckusick * Called by vfs_mountroot when nfs is going to be mounted as root 6938414Smckusick * Not Yet (By a LONG shot) 7038414Smckusick */ 7138414Smckusick nfs_mountroot() 7238414Smckusick { 7338414Smckusick return (ENODEV); 7438414Smckusick } 7538414Smckusick 7638414Smckusick /* 7738414Smckusick * VFS Operations. 7838414Smckusick * 7938414Smckusick * mount system call 8038414Smckusick * It seems a bit dumb to copyinstr() the host and path here and then 8138414Smckusick * bcopy() them in mountnfs(), but I wanted to detect errors before 8238414Smckusick * doing the sockargs() call because sockargs() allocates an mbuf and 8338414Smckusick * an error after that means that I have to release the mbuf. 8438414Smckusick */ 8539494Smckusick /* ARGSUSED */ 8638414Smckusick nfs_mount(mp, path, data, ndp) 8738414Smckusick struct mount *mp; 8838414Smckusick char *path; 8938414Smckusick caddr_t data; 9038414Smckusick struct nameidata *ndp; 9138414Smckusick { 9238414Smckusick int error; 9338414Smckusick struct nfs_args args; 9438414Smckusick struct mbuf *saddr; 9538414Smckusick char pth[MNAMELEN], hst[MNAMELEN]; 9638414Smckusick int len; 9738414Smckusick nfsv2fh_t nfh; 9838414Smckusick 9939460Smckusick if (mp->m_flag & M_UPDATE) 10039460Smckusick return (0); 10138414Smckusick if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args))) 10238414Smckusick return (error); 10338414Smckusick if (error=copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t))) 10438414Smckusick return (error); 10538414Smckusick if (error = copyinstr(path, pth, MNAMELEN-1, &len)) 10638414Smckusick return (error); 10738414Smckusick bzero(&pth[len], MNAMELEN-len); 10838414Smckusick if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len)) 10938414Smckusick return (error); 11038414Smckusick bzero(&hst[len], MNAMELEN-len); 11138414Smckusick /* sockargs() call must be after above copyin() calls */ 11238414Smckusick if (error = sockargs(&saddr, (caddr_t)args.addr, 11340120Smckusick sizeof (struct sockaddr), MT_SONAME)) 11438414Smckusick return (error); 11538414Smckusick args.fh = &nfh; 11638414Smckusick error = mountnfs(&args, mp, saddr, pth, hst); 11738414Smckusick return (error); 11838414Smckusick } 11938414Smckusick 12038414Smckusick /* 12138414Smckusick * Common code for mount and mountroot 12238414Smckusick */ 12338414Smckusick mountnfs(argp, mp, saddr, pth, hst) 12438414Smckusick register struct nfs_args *argp; 12538414Smckusick register struct mount *mp; 12638414Smckusick register struct mbuf *saddr; 12738414Smckusick char *pth, *hst; 12838414Smckusick { 12938414Smckusick register struct nfsmount *nmp; 13040010Smckusick struct nfsnode *np; 13140120Smckusick int error; 13239757Smckusick fsid_t tfsid; 13338414Smckusick 13440120Smckusick MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK); 13540120Smckusick bzero((caddr_t)nmp, sizeof *nmp); 13638414Smckusick mp->m_data = (qaddr_t)nmp; 13739757Smckusick /* 13839757Smckusick * Generate a unique nfs mount id. The problem is that a dev number 13939757Smckusick * is not unique across multiple systems. The techique is as follows: 14039757Smckusick * 1) Set to nblkdev,0 which will never be used otherwise 14139757Smckusick * 2) Generate a first guess as nblkdev,nfs_mntid where nfs_mntid is 14239757Smckusick * NOT 0 14339757Smckusick * 3) Loop searching the mount list for another one with same id 14439757Smckusick * If a match, increment val[0] and try again 14539757Smckusick * NB: I increment val[0] { a long } instead of nfs_mntid { a u_char } 14639757Smckusick * so that nfs is not limited to 255 mount points 14739757Smckusick * Incrementing the high order bits does no real harm, since it 14839757Smckusick * simply makes the major dev number tick up. The upper bound is 14939757Smckusick * set to major dev 127 to avoid any sign extention problems 15039757Smckusick */ 151*40353Smckusick mp->m_stat.f_fsid.val[0] = makedev(nblkdev, 0); 152*40353Smckusick mp->m_stat.f_fsid.val[1] = MOUNT_NFS; 15339757Smckusick if (++nfs_mntid == 0) 15439757Smckusick ++nfs_mntid; 15539757Smckusick tfsid.val[0] = makedev(nblkdev, nfs_mntid); 15639757Smckusick tfsid.val[1] = MOUNT_NFS; 15739757Smckusick while (getvfs(&tfsid)) { 15839757Smckusick tfsid.val[0]++; 15939757Smckusick nfs_mntid++; 16039757Smckusick } 16139757Smckusick if (major(tfsid.val[0]) > 127) { 16239757Smckusick error = ENOENT; 16339757Smckusick goto bad; 16439757Smckusick } 165*40353Smckusick mp->m_stat.f_fsid.val[0] = tfsid.val[0]; 16638414Smckusick nmp->nm_mountp = mp; 16738414Smckusick nmp->nm_flag = argp->flags; 16840120Smckusick nmp->nm_rto = NFS_TIMEO; 16940120Smckusick nmp->nm_rtt = -1; 17040120Smckusick nmp->nm_rttvar = nmp->nm_rto << 1; 17140120Smckusick nmp->nm_retry = NFS_RETRANS; 17240120Smckusick nmp->nm_wsize = NFS_WSIZE; 17340120Smckusick nmp->nm_rsize = NFS_RSIZE; 17440120Smckusick bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t)); 175*40353Smckusick bcopy(hst, mp->m_stat.f_mntfromname, MNAMELEN); 176*40353Smckusick bcopy(pth, mp->m_stat.f_mntonname, MNAMELEN); 17740120Smckusick 17840120Smckusick if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 17940120Smckusick nmp->nm_rto = argp->timeo; 18040120Smckusick /* NFS timeouts are specified in 1/10 sec. */ 18140120Smckusick nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ; 18240120Smckusick if (nmp->nm_rto < NFS_MINTIMEO) 18340120Smckusick nmp->nm_rto = NFS_MINTIMEO; 18440120Smckusick else if (nmp->nm_rto > NFS_MAXTIMEO) 18540120Smckusick nmp->nm_rto = NFS_MAXTIMEO; 18640120Smckusick nmp->nm_rttvar = nmp->nm_rto << 1; 18740120Smckusick } 18840120Smckusick 18940120Smckusick if ((argp->flags & NFSMNT_RETRANS) && argp->retrans >= 0) { 19040120Smckusick nmp->nm_retry = argp->retrans; 19140120Smckusick if (nmp->nm_retry > NFS_MAXREXMIT) 19240120Smckusick nmp->nm_retry = NFS_MAXREXMIT; 19340120Smckusick } 19440120Smckusick 19540120Smckusick if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 19638414Smckusick nmp->nm_wsize = argp->wsize; 19740120Smckusick /* Round down to multiple of blocksize */ 19840120Smckusick nmp->nm_wsize &= ~0x1ff; 19940120Smckusick if (nmp->nm_wsize <= 0) 20040120Smckusick nmp->nm_wsize = 512; 20140120Smckusick else if (nmp->nm_wsize > NFS_MAXDATA) 20240120Smckusick nmp->nm_wsize = NFS_MAXDATA; 20340120Smckusick } 20440120Smckusick 20540120Smckusick if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 20638414Smckusick nmp->nm_rsize = argp->rsize; 20740120Smckusick /* Round down to multiple of blocksize */ 20840120Smckusick nmp->nm_rsize &= ~0x1ff; 20940120Smckusick if (nmp->nm_rsize <= 0) 21040120Smckusick nmp->nm_rsize = 512; 21140120Smckusick else if (nmp->nm_rsize > NFS_MAXDATA) 21240120Smckusick nmp->nm_rsize = NFS_MAXDATA; 21340120Smckusick } 21440120Smckusick /* Set up the sockets and per-host congestion */ 21540120Smckusick if (error = nfs_connect(nmp, saddr)) 21640120Smckusick goto bad; 21740120Smckusick 218*40353Smckusick if (error = nfs_statfs(mp, &mp->m_stat)) 219*40353Smckusick goto bad; 22038414Smckusick /* 22140010Smckusick * A reference count is needed on the nfsnode representing the 22240010Smckusick * remote root. If this object is not persistent, then backward 22340010Smckusick * traversals of the mount point (i.e. "..") will not work if 22440010Smckusick * the nfsnode gets flushed out of the cache. Ufs does not have 22540010Smckusick * this problem, because one can identify root inodes by their 22640010Smckusick * number == ROOTINO (2). 22740010Smckusick */ 22840010Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 22940010Smckusick goto bad; 23040010Smckusick /* 23140010Smckusick * Unlock it, but keep the reference count. 23240010Smckusick */ 23340010Smckusick nfs_unlock(NFSTOV(np)); 234*40353Smckusick return (0); 23540120Smckusick 23638414Smckusick bad: 23740120Smckusick nfs_disconnect(nmp); 23840120Smckusick FREE(nmp, M_NFSMNT); 23938414Smckusick m_freem(saddr); 24038414Smckusick return (error); 24138414Smckusick } 24238414Smckusick 24338414Smckusick /* 24438414Smckusick * unmount system call 24538414Smckusick */ 24638414Smckusick nfs_unmount(mp, flags) 24738414Smckusick struct mount *mp; 24838414Smckusick int flags; 24938414Smckusick { 25038414Smckusick register struct nfsmount *nmp; 25138414Smckusick register struct nfsreq *rep; 25238414Smckusick struct nfsreq *rep2; 25340010Smckusick struct nfsnode *np; 25440120Smckusick struct vnode *vp; 25538414Smckusick int error; 25638414Smckusick int s; 25738414Smckusick 25838414Smckusick if (flags & MNT_FORCE) 25938414Smckusick return (EINVAL); 26038414Smckusick nmp = vfs_to_nfs(mp); 26138414Smckusick /* 26238884Smacklem * Clear out the buffer cache 26338884Smacklem */ 26439669Smckusick mntflushbuf(mp, 0); 26539669Smckusick if (mntinvalbuf(mp)) 26638884Smacklem return (EBUSY); 26738884Smacklem /* 26838414Smckusick * Goes something like this.. 26940120Smckusick * - Check for activity on the root vnode (other than ourselves). 27040120Smckusick * - Call vflush() to clear out vnodes for this file system, 27140120Smckusick * except for the root vnode. 27240120Smckusick * - Decrement reference on the vnode representing remote root. 27338414Smckusick * - Close the socket 27438414Smckusick * - Free up the data structures 27538414Smckusick */ 27640010Smckusick /* 27740010Smckusick * We need to decrement the ref. count on the nfsnode representing 27840010Smckusick * the remote root. See comment in mountnfs(). The VFS unmount() 27940010Smckusick * has done vput on this vnode, otherwise we would get deadlock! 28040010Smckusick */ 28140010Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 28240010Smckusick return(error); 28340120Smckusick vp = NFSTOV(np); 28440120Smckusick if (vp->v_usecount > 2) { 28540120Smckusick vput(vp); 28640120Smckusick return (EBUSY); 28740120Smckusick } 28840120Smckusick if (error = vflush(mp, vp, flags)) { 28940120Smckusick vput(vp); 29040120Smckusick return (error); 29140120Smckusick } 29240010Smckusick /* 29340010Smckusick * Get rid of two reference counts, and unlock it on the second. 29440010Smckusick */ 29540120Smckusick vrele(vp); 29640120Smckusick vput(vp); 29740120Smckusick nfs_disconnect(nmp); 29838414Smckusick free((caddr_t)nmp, M_NFSMNT); 29938414Smckusick return (0); 30038414Smckusick } 30138414Smckusick 30238414Smckusick /* 30338414Smckusick * Return root of a filesystem 30438414Smckusick */ 30538414Smckusick nfs_root(mp, vpp) 30638414Smckusick struct mount *mp; 30738414Smckusick struct vnode **vpp; 30838414Smckusick { 30938414Smckusick register struct vnode *vp; 31038414Smckusick struct nfsmount *nmp; 31138414Smckusick struct nfsnode *np; 31238414Smckusick int error; 31338414Smckusick 31438414Smckusick nmp = vfs_to_nfs(mp); 31538414Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 31638414Smckusick return (error); 31738414Smckusick vp = NFSTOV(np); 31838414Smckusick vp->v_type = VDIR; 31938414Smckusick vp->v_flag = VROOT; 32038414Smckusick *vpp = vp; 32138414Smckusick return (0); 32238414Smckusick } 32338414Smckusick 32438884Smacklem extern int syncprt; 32538884Smacklem 32638414Smckusick /* 32738884Smacklem * Flush out the buffer cache 32838414Smckusick */ 32939494Smckusick /* ARGSUSED */ 33038414Smckusick nfs_sync(mp, waitfor) 33138414Smckusick struct mount *mp; 33238414Smckusick int waitfor; 33338414Smckusick { 33438884Smacklem if (syncprt) 33538884Smacklem bufstats(); 33638884Smacklem /* 33738884Smacklem * Force stale buffer cache information to be flushed. 33838884Smacklem */ 33940035Smckusick mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0); 34038414Smckusick return (0); 34138414Smckusick } 34238414Smckusick 34338414Smckusick /* 34438414Smckusick * At this point, this should never happen 34538414Smckusick */ 34639494Smckusick /* ARGSUSED */ 34738414Smckusick nfs_fhtovp(mp, fhp, vpp) 34838414Smckusick struct mount *mp; 34938414Smckusick struct fid *fhp; 35038414Smckusick struct vnode **vpp; 35138414Smckusick { 35239494Smckusick 35338414Smckusick return (EINVAL); 35438414Smckusick } 35538414Smckusick 35638414Smckusick /* 35738414Smckusick * Vnode pointer to File handle, should never happen either 35838414Smckusick */ 35939494Smckusick /* ARGSUSED */ 36038414Smckusick nfs_vptofh(mp, fhp, vpp) 36138414Smckusick struct mount *mp; 36238414Smckusick struct fid *fhp; 36338414Smckusick struct vnode **vpp; 36438414Smckusick { 36539494Smckusick 36638414Smckusick return (EINVAL); 36738414Smckusick } 36838884Smacklem 36938884Smacklem /* 37038884Smacklem * Vfs start routine, a no-op. 37138884Smacklem */ 37239494Smckusick /* ARGSUSED */ 37338884Smacklem nfs_start(mp, flags) 37438884Smacklem struct mount *mp; 37538884Smacklem int flags; 37638884Smacklem { 37739494Smckusick 37838884Smacklem return (0); 37938884Smacklem } 380