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*40553Smckusick * @(#)nfs_vfsops.c 7.18 (Berkeley) 03/20/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 */ 15140353Smckusick mp->m_stat.f_fsid.val[0] = makedev(nblkdev, 0); 15240353Smckusick 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; 163*40553Smckusick m_freem(saddr); 16439757Smckusick goto bad; 16539757Smckusick } 16640353Smckusick mp->m_stat.f_fsid.val[0] = tfsid.val[0]; 16738414Smckusick nmp->nm_mountp = mp; 16838414Smckusick nmp->nm_flag = argp->flags; 16940120Smckusick nmp->nm_rto = NFS_TIMEO; 17040120Smckusick nmp->nm_rtt = -1; 17140120Smckusick nmp->nm_rttvar = nmp->nm_rto << 1; 17240120Smckusick nmp->nm_retry = NFS_RETRANS; 17340120Smckusick nmp->nm_wsize = NFS_WSIZE; 17440120Smckusick nmp->nm_rsize = NFS_RSIZE; 17540120Smckusick bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t)); 17640353Smckusick bcopy(hst, mp->m_stat.f_mntfromname, MNAMELEN); 17740353Smckusick bcopy(pth, mp->m_stat.f_mntonname, MNAMELEN); 17840120Smckusick 17940120Smckusick if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 18040120Smckusick nmp->nm_rto = argp->timeo; 18140120Smckusick /* NFS timeouts are specified in 1/10 sec. */ 18240120Smckusick nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ; 18340120Smckusick if (nmp->nm_rto < NFS_MINTIMEO) 18440120Smckusick nmp->nm_rto = NFS_MINTIMEO; 18540120Smckusick else if (nmp->nm_rto > NFS_MAXTIMEO) 18640120Smckusick nmp->nm_rto = NFS_MAXTIMEO; 18740120Smckusick nmp->nm_rttvar = nmp->nm_rto << 1; 18840120Smckusick } 18940120Smckusick 19040120Smckusick if ((argp->flags & NFSMNT_RETRANS) && argp->retrans >= 0) { 19140120Smckusick nmp->nm_retry = argp->retrans; 19240120Smckusick if (nmp->nm_retry > NFS_MAXREXMIT) 19340120Smckusick nmp->nm_retry = NFS_MAXREXMIT; 19440120Smckusick } 19540120Smckusick 19640120Smckusick if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 19738414Smckusick nmp->nm_wsize = argp->wsize; 19840120Smckusick /* Round down to multiple of blocksize */ 19940120Smckusick nmp->nm_wsize &= ~0x1ff; 20040120Smckusick if (nmp->nm_wsize <= 0) 20140120Smckusick nmp->nm_wsize = 512; 20240120Smckusick else if (nmp->nm_wsize > NFS_MAXDATA) 20340120Smckusick nmp->nm_wsize = NFS_MAXDATA; 20440120Smckusick } 20540120Smckusick 20640120Smckusick if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 20738414Smckusick nmp->nm_rsize = argp->rsize; 20840120Smckusick /* Round down to multiple of blocksize */ 20940120Smckusick nmp->nm_rsize &= ~0x1ff; 21040120Smckusick if (nmp->nm_rsize <= 0) 21140120Smckusick nmp->nm_rsize = 512; 21240120Smckusick else if (nmp->nm_rsize > NFS_MAXDATA) 21340120Smckusick nmp->nm_rsize = NFS_MAXDATA; 21440120Smckusick } 21540120Smckusick /* Set up the sockets and per-host congestion */ 216*40553Smckusick if (error = nfs_connect(nmp, saddr)) { 217*40553Smckusick m_freem(saddr); 21840120Smckusick goto bad; 219*40553Smckusick } 22040120Smckusick 22140353Smckusick if (error = nfs_statfs(mp, &mp->m_stat)) 22240353Smckusick goto bad; 22338414Smckusick /* 22440010Smckusick * A reference count is needed on the nfsnode representing the 22540010Smckusick * remote root. If this object is not persistent, then backward 22640010Smckusick * traversals of the mount point (i.e. "..") will not work if 22740010Smckusick * the nfsnode gets flushed out of the cache. Ufs does not have 22840010Smckusick * this problem, because one can identify root inodes by their 22940010Smckusick * number == ROOTINO (2). 23040010Smckusick */ 23140010Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 23240010Smckusick goto bad; 23340010Smckusick /* 23440010Smckusick * Unlock it, but keep the reference count. 23540010Smckusick */ 23640010Smckusick nfs_unlock(NFSTOV(np)); 23740353Smckusick return (0); 23840120Smckusick 23938414Smckusick bad: 24040120Smckusick nfs_disconnect(nmp); 24140120Smckusick FREE(nmp, M_NFSMNT); 24238414Smckusick return (error); 24338414Smckusick } 24438414Smckusick 24538414Smckusick /* 24638414Smckusick * unmount system call 24738414Smckusick */ 24838414Smckusick nfs_unmount(mp, flags) 24938414Smckusick struct mount *mp; 25038414Smckusick int flags; 25138414Smckusick { 25238414Smckusick register struct nfsmount *nmp; 25338414Smckusick register struct nfsreq *rep; 25438414Smckusick struct nfsreq *rep2; 25540010Smckusick struct nfsnode *np; 25640120Smckusick struct vnode *vp; 25738414Smckusick int error; 25838414Smckusick int s; 25938414Smckusick 26038414Smckusick if (flags & MNT_FORCE) 26138414Smckusick return (EINVAL); 26238414Smckusick nmp = vfs_to_nfs(mp); 26338414Smckusick /* 26438884Smacklem * Clear out the buffer cache 26538884Smacklem */ 26639669Smckusick mntflushbuf(mp, 0); 26739669Smckusick if (mntinvalbuf(mp)) 26838884Smacklem return (EBUSY); 26938884Smacklem /* 27038414Smckusick * Goes something like this.. 27140120Smckusick * - Check for activity on the root vnode (other than ourselves). 27240120Smckusick * - Call vflush() to clear out vnodes for this file system, 27340120Smckusick * except for the root vnode. 27440120Smckusick * - Decrement reference on the vnode representing remote root. 27538414Smckusick * - Close the socket 27638414Smckusick * - Free up the data structures 27738414Smckusick */ 27840010Smckusick /* 27940010Smckusick * We need to decrement the ref. count on the nfsnode representing 28040010Smckusick * the remote root. See comment in mountnfs(). The VFS unmount() 28140010Smckusick * has done vput on this vnode, otherwise we would get deadlock! 28240010Smckusick */ 28340010Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 28440010Smckusick return(error); 28540120Smckusick vp = NFSTOV(np); 28640120Smckusick if (vp->v_usecount > 2) { 28740120Smckusick vput(vp); 28840120Smckusick return (EBUSY); 28940120Smckusick } 29040120Smckusick if (error = vflush(mp, vp, flags)) { 29140120Smckusick vput(vp); 29240120Smckusick return (error); 29340120Smckusick } 29440010Smckusick /* 29540010Smckusick * Get rid of two reference counts, and unlock it on the second. 29640010Smckusick */ 29740120Smckusick vrele(vp); 29840120Smckusick vput(vp); 29940120Smckusick nfs_disconnect(nmp); 30038414Smckusick free((caddr_t)nmp, M_NFSMNT); 30138414Smckusick return (0); 30238414Smckusick } 30338414Smckusick 30438414Smckusick /* 30538414Smckusick * Return root of a filesystem 30638414Smckusick */ 30738414Smckusick nfs_root(mp, vpp) 30838414Smckusick struct mount *mp; 30938414Smckusick struct vnode **vpp; 31038414Smckusick { 31138414Smckusick register struct vnode *vp; 31238414Smckusick struct nfsmount *nmp; 31338414Smckusick struct nfsnode *np; 31438414Smckusick int error; 31538414Smckusick 31638414Smckusick nmp = vfs_to_nfs(mp); 31738414Smckusick if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 31838414Smckusick return (error); 31938414Smckusick vp = NFSTOV(np); 32038414Smckusick vp->v_type = VDIR; 32138414Smckusick vp->v_flag = VROOT; 32238414Smckusick *vpp = vp; 32338414Smckusick return (0); 32438414Smckusick } 32538414Smckusick 32638884Smacklem extern int syncprt; 32738884Smacklem 32838414Smckusick /* 32938884Smacklem * Flush out the buffer cache 33038414Smckusick */ 33139494Smckusick /* ARGSUSED */ 33238414Smckusick nfs_sync(mp, waitfor) 33338414Smckusick struct mount *mp; 33438414Smckusick int waitfor; 33538414Smckusick { 33638884Smacklem if (syncprt) 33738884Smacklem bufstats(); 33838884Smacklem /* 33938884Smacklem * Force stale buffer cache information to be flushed. 34038884Smacklem */ 34140035Smckusick mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0); 34238414Smckusick return (0); 34338414Smckusick } 34438414Smckusick 34538414Smckusick /* 34638414Smckusick * At this point, this should never happen 34738414Smckusick */ 34839494Smckusick /* ARGSUSED */ 34938414Smckusick nfs_fhtovp(mp, fhp, vpp) 35038414Smckusick struct mount *mp; 35138414Smckusick struct fid *fhp; 35238414Smckusick struct vnode **vpp; 35338414Smckusick { 35439494Smckusick 35538414Smckusick return (EINVAL); 35638414Smckusick } 35738414Smckusick 35838414Smckusick /* 35938414Smckusick * Vnode pointer to File handle, should never happen either 36038414Smckusick */ 36139494Smckusick /* ARGSUSED */ 36238414Smckusick nfs_vptofh(mp, fhp, vpp) 36338414Smckusick struct mount *mp; 36438414Smckusick struct fid *fhp; 36538414Smckusick struct vnode **vpp; 36638414Smckusick { 36739494Smckusick 36838414Smckusick return (EINVAL); 36938414Smckusick } 37038884Smacklem 37138884Smacklem /* 37238884Smacklem * Vfs start routine, a no-op. 37338884Smacklem */ 37439494Smckusick /* ARGSUSED */ 37538884Smacklem nfs_start(mp, flags) 37638884Smacklem struct mount *mp; 37738884Smacklem int flags; 37838884Smacklem { 37939494Smckusick 38038884Smacklem return (0); 38138884Smacklem } 382