]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/nfs/nfs_vfsops.c
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
index 1df71854cc36f8c1f2e927e17385249b05d7145f..882c56996d26f6a748b852c60025a080f495f3a3 100644 (file)
@@ -1,21 +1,24 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
  *
  *     @(#)nfs_vfsops.c        8.12 (Berkeley) 5/20/95
  * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
- *
- *  History:
- *
- *
- *  23-May-97  Umesh Vaishampayan  (umeshv@apple.com)
- *     Added the ability to mount "/private" separately.
- *     Fixed bug which caused incorrect reporting of "mounted on"
- *     directory name in case of nfs root.
  */
 
 #include <sys/param.h>
@@ -171,34 +166,6 @@ struct vfsops nfs_vfsops = {
 VFS_SET(nfs_vfsops, nfs, MOUNT_NFS, VFCF_NETWORK);
 #endif
 
-/*
- * This structure must be filled in by a primary bootstrap or bootstrap
- * server for a diskless/dataless machine. It is initialized below just
- * to ensure that it is allocated to initialized data (.data not .bss).
- */
-struct nfs_diskless nfs_diskless = { 0 };
-int nfs_diskless_valid = 0;
-
-/* XXX CSM 11/25/97 Upgrade sysctl.h someday */
-#ifdef notyet
-SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD, 
-       &nfs_diskless_valid, 0, "");
-
-SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
-       nfs_diskless.root_hostnam, 0, "");
-
-SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
-       &nfs_diskless.root_saddr, sizeof nfs_diskless.root_saddr,
-       "%Ssockaddr_in", "");
-
-SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
-       nfs_diskless.swap_hostnam, 0, "");
-
-SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
-       &nfs_diskless.swap_saddr, sizeof nfs_diskless.swap_saddr, 
-       "%Ssockaddr_in","");
-#endif
-
 
 void nfsargs_ntoh __P((struct nfs_args *));
 static int
@@ -228,12 +195,8 @@ static int nfs_iosize(nmp)
         * space.
         */
        iosize = max(nmp->nm_rsize, nmp->nm_wsize);
-       if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
-#if 0
-       /* XXX UPL changes for UBC do not support multiple pages */
-       iosize = PAGE_SIZE; /* XXX FIXME */
-#endif
-        /* return iosize; */
+       if (iosize < PAGE_SIZE)
+               iosize = PAGE_SIZE;
        return (trunc_page(iosize));
 }
 
@@ -282,12 +245,14 @@ nfs_statfs(mp, sbp, p)
        struct ucred *cred;
        u_quad_t tquad;
        extern int nfs_mount_type;
+       u_int64_t xid;
 
 #ifndef nolint
        sfp = (struct nfs_statfs *)0;
 #endif
        vp = nmp->nm_dvp;
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
+       if (error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p))
+               return(error);
        cred = crget();
        cred->cr_ngroups = 1;
        if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
@@ -295,9 +260,9 @@ nfs_statfs(mp, sbp, p)
        nfsstats.rpccnt[NFSPROC_FSSTAT]++;
        nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
        nfsm_fhtom(vp, v3);
-       nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
+       nfsm_request(vp, NFSPROC_FSSTAT, p, cred, &xid);
        if (v3)
-               nfsm_postop_attr(vp, retattr);
+               nfsm_postop_attr(vp, retattr, &xid);
        nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
 
 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
@@ -355,12 +320,13 @@ nfs_fsinfo(nmp, vp, cred, p)
        caddr_t bpos, dpos, cp2;
        int error = 0, retattr;
        struct mbuf *mreq, *mrep, *md, *mb, *mb2;
+       u_int64_t xid;
 
        nfsstats.rpccnt[NFSPROC_FSINFO]++;
        nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
        nfsm_fhtom(vp, 1);
-       nfsm_request(vp, NFSPROC_FSINFO, p, cred);
-       nfsm_postop_attr(vp, retattr);
+       nfsm_request(vp, NFSPROC_FSINFO, p, cred, &xid);
+       nfsm_postop_attr(vp, retattr, &xid);
        if (!error) {
                nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
                pref = fxdr_unsigned(u_long, fsp->fs_wtpref);
@@ -422,15 +388,34 @@ nfs_mountroot()
        struct mount *mppriv;
        struct vnode *vppriv;
 #endif /* NO_MOUNT_PRIVATE */
+       int v3;
 
        procp = current_proc(); /* XXX */
 
        /*
         * Call nfs_boot_init() to fill in the nfs_diskless struct.
-        * Side effect:  Finds and configures a network interface.
+        * Note: networking must already have been configured before
+        * we're called.
         */
        bzero((caddr_t) &nd, sizeof(nd));
-       nfs_boot_init(&nd, procp);
+       error = nfs_boot_init(&nd, procp);
+       if (error) {
+               panic("nfs_boot_init failed with %d\n", error);
+       }
+
+       /* try NFSv3 first, if that fails then try NFSv2 */
+       v3 = 1;
+
+tryagain:
+       error = nfs_boot_getfh(&nd, procp, v3);
+       if (error) {
+               if (v3) {
+                       printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error);
+                       v3 = 0;
+                       goto tryagain;
+               }
+               panic("nfs_boot_getfh(v2) failed with %d\n", error);
+       }
 
        /*
         * Create the root mount point.
@@ -440,7 +425,12 @@ nfs_mountroot()
 #else
        if (error = nfs_mount_diskless(&nd.nd_root, "/", NULL, &vp, &mp)) {
 #endif /* NO_MOUNT_PRIVATE */
-               return(error);
+               if (v3) {
+                       printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error);
+                       v3 = 0;
+                       goto tryagain;
+               }
+               panic("nfs_mount_diskless root failed with %d\n", error);
        }
        printf("root on %s\n", (char *)&nd.nd_root.ndm_host);
 
@@ -454,8 +444,9 @@ nfs_mountroot()
        if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
            error = nfs_mount_diskless_private(&nd.nd_private, "/private",
                                               NULL, &vppriv, &mppriv);
-           if (error)
-               return(error);
+           if (error) {
+               panic("nfs_mount_diskless private failed with %d\n", error);
+           }
            printf("private on %s\n", (char *)&nd.nd_private.ndm_host);
            
            simple_lock(&mountlist_slock);
@@ -466,6 +457,11 @@ nfs_mountroot()
 
 #endif /* NO_MOUNT_PRIVATE */
 
+       if (nd.nd_root.ndm_path)
+               FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
+       if (nd.nd_private.ndm_path)
+               FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
+
        /* Get root attributes (for the time). */
        error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
        if (error) panic("nfs_mountroot: getattr for root");
@@ -505,9 +501,11 @@ nfs_mount_diskless(ndmntp, mntname, mntflag, vpp, mpp)
        args.addrlen  = args.addr->sa_len;
        args.sotype   = SOCK_DGRAM;
        args.fh       = ndmntp->ndm_fh;
-       args.fhsize   = NFSX_V2FH;
+       args.fhsize   = ndmntp->ndm_fhlen;
        args.hostname = ndmntp->ndm_host;
        args.flags    = NFSMNT_RESVPORT;
+       if (ndmntp->ndm_nfsv3)
+               args.flags |= NFSMNT_NFSV3;
 
        MGET(m, M_DONTWAIT, MT_SONAME);
        bcopy((caddr_t)args.addr, mtod(m, caddr_t), 
@@ -562,8 +560,8 @@ nfs_mount_diskless_private(ndmntp, mntname, mntflag, vpp, mpp)
                /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
                if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
                        panic("cannot find root vnode");
+               VREF(rootvnode);
                fdp->fd_cdir = rootvnode;
-               VREF(fdp->fd_cdir);
                VOP_UNLOCK(rootvnode, 0, procp);
                fdp->fd_rdir = NULL;
        }
@@ -608,6 +606,11 @@ nfs_mount_diskless_private(ndmntp, mntname, mntflag, vpp, mpp)
         */
        mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
        bzero((char *)mp, (u_long)sizeof(struct mount));
+
+    /* Initialize the default IO constraints */
+    mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
+    mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
+
        lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
        (void)vfs_busy(mp, LK_NOWAIT, 0, procp);
        LIST_INIT(&mp->mnt_vnodelist);
@@ -630,9 +633,11 @@ nfs_mount_diskless_private(ndmntp, mntname, mntflag, vpp, mpp)
        args.addrlen  = args.addr->sa_len;
        args.sotype   = SOCK_DGRAM;
        args.fh       = ndmntp->ndm_fh;
-       args.fhsize   = NFSX_V2FH;
+       args.fhsize   = ndmntp->ndm_fhlen;
        args.hostname = ndmntp->ndm_host;
        args.flags    = NFSMNT_RESVPORT;
+       if (ndmntp->ndm_nfsv3)
+               args.flags |= NFSMNT_NFSV3;
 
        MGET(m, M_DONTWAIT, MT_SONAME);
        bcopy((caddr_t)args.addr, mtod(m, caddr_t), 
@@ -936,34 +941,38 @@ nfs_unmount(mp, mntflags, p)
        nmp = VFSTONFS(mp);
        /*
         * Goes something like this..
-        * - Check for activity on the root vnode (other than ourselves).
         * - Call vflush() to clear out vnodes for this file system,
-        *   except for the root vnode.
+        *   except for the swap files. Deal with them in 2nd pass.
+        *   It will do vgone making the vnode VBAD at that time.
         * - Decrement reference on the vnode representing remote root.
         * - Close the socket
         * - Free up the data structures
         */
-       /*
-        * We need to decrement the ref. count on the nfsnode representing
-        * the remote root.  See comment in mountnfs().  The VFS unmount()
-        * has done vput on this vnode, otherwise we would get deadlock!
-        */
        vp = nmp->nm_dvp;
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
-       if (vp->v_usecount > 1) {
-               VOP_UNLOCK(vp, 0, p);
-               return (EBUSY);
-       }
-
+       
        /*
         * Must handshake with nqnfs_clientd() if it is active.
         */
        nmp->nm_flag |= NFSMNT_DISMINPROG;
        while (nmp->nm_inprog != NULLVP)
                (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
-       error = vflush(mp, vp, flags);
+       /*
+        * vflush will check for busy vnodes on mountpoint. 
+        * Will do the right thing for MNT_FORCE. That is, we should
+        * not get EBUSY back.
+        */
+       error = vflush(mp, vp, SKIPSWAP | flags);
+       if (mntflags & MNT_FORCE) 
+               error = vflush(mp, NULLVP, flags); /* locks vp in the process */
+       else {
+               if (vp->v_usecount > 1) {
+                       nmp->nm_flag &= ~NFSMNT_DISMINPROG;
+                       return (EBUSY);
+               }
+               error = vflush(mp, vp, flags);
+       }
+
        if (error) {
-               VOP_UNLOCK(vp, 0, p);
                nmp->nm_flag &= ~NFSMNT_DISMINPROG;
                return (error);
        }
@@ -977,9 +986,14 @@ nfs_unmount(mp, mntflags, p)
 
        /*
         * Release the root vnode reference held by mountnfs()
+        * vflush did the vgone for us when we didn't skip over
+        * it in the MNT_FORCE case. (Thus vp can't be locked when
+        * called vflush in non-skip vp case.)
         */
-       vput(vp);
-       vgone(vp);
+       vrele(vp);
+       if (!(mntflags & MNT_FORCE))
+               vgone(vp);
+       mp->mnt_data = 0; /* don't want to end up using stale vp */
        nfs_disconnect(nmp);
        m_freem(nmp->nm_nam);
 
@@ -1056,6 +1070,7 @@ loop:
        for (vp = mp->mnt_vnodelist.lh_first;
             vp != NULL;
             vp = vp->v_mntvnodes.le_next) {
+                int didhold = 0;
                /*
                 * If the vnode that we are about to sync is no longer
                 * associated with this mount point, start over.
@@ -1066,10 +1081,14 @@ loop:
                        continue;
                if (vget(vp, LK_EXCLUSIVE, p))
                        goto loop;
+               didhold = ubc_hold(vp);
                error = VOP_FSYNC(vp, cred, waitfor, p);
                if (error)
                        allerror = error;
-               vput(vp);
+               VOP_UNLOCK(vp, 0, p);
+               if (didhold)
+                       ubc_rele(vp);
+               vrele(vp);
        }
        return (allerror);
 }