xref: /onnv-gate/usr/src/stand/lib/fs/nfs/lookup.c (revision 9694:78fafb281255)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9694SScott.Rotondo@Sun.COM  * Common Development and Distribution License (the "License").
6*9694SScott.Rotondo@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*9694SScott.Rotondo@Sun.COM 
220Sstevel@tonic-gate /*
23*9694SScott.Rotondo@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * This file contains the file lookup code for NFS.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <rpc/rpc.h>
400Sstevel@tonic-gate #include "brpc.h"
410Sstevel@tonic-gate #include <rpc/types.h>
420Sstevel@tonic-gate #include <rpc/auth.h>
430Sstevel@tonic-gate #include <rpc/xdr.h>
440Sstevel@tonic-gate #include <rpc/rpc_msg.h>
450Sstevel@tonic-gate #include <sys/t_lock.h>
460Sstevel@tonic-gate #include "clnt.h"
470Sstevel@tonic-gate #include <rpcsvc/mount.h>
48*9694SScott.Rotondo@Sun.COM #include <st_pathname.h>
490Sstevel@tonic-gate #include <sys/errno.h>
500Sstevel@tonic-gate #include <sys/promif.h>
510Sstevel@tonic-gate #include "nfs_inet.h"
520Sstevel@tonic-gate #include "socket_inet.h"
530Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
540Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <sys/salib.h>
570Sstevel@tonic-gate #include <sys/sacache.h>
580Sstevel@tonic-gate #include <sys/stat.h>
590Sstevel@tonic-gate #include <sys/bootvfs.h>
600Sstevel@tonic-gate #include <sys/bootdebug.h>
610Sstevel@tonic-gate #include "mac.h"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int root_inum = 1;	/* Dummy i-node number for root */
640Sstevel@tonic-gate static int next_inum = 1;	/* Next dummy i-node number	*/
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	dprintf	if (boothowto & RB_DEBUG) printf
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * starting at current directory (root for us), lookup the pathname.
700Sstevel@tonic-gate  * return the file handle of said file.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
73*9694SScott.Rotondo@Sun.COM static int stlookuppn(struct st_pathname *pnp, struct nfs_file *cfile,
740Sstevel@tonic-gate 			bool_t needroothandle);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * For NFSv4 we may be calling lookup in the context of evaluating the
780Sstevel@tonic-gate  * root path.  In this case we set needroothandle to TRUE.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate int
lookup(char * pathname,struct nfs_file * cur_file,bool_t needroothandle)810Sstevel@tonic-gate lookup(char *pathname, struct nfs_file *cur_file, bool_t needroothandle)
820Sstevel@tonic-gate {
83*9694SScott.Rotondo@Sun.COM 	struct st_pathname pnp;
840Sstevel@tonic-gate 	int error;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	static char lkup_path[NFS_MAXPATHLEN];	/* pn_alloc doesn't */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	pnp.pn_buf = &lkup_path[0];
890Sstevel@tonic-gate 	bzero(pnp.pn_buf, NFS_MAXPATHLEN);
90*9694SScott.Rotondo@Sun.COM 	error = stpn_get(pathname, &pnp);
910Sstevel@tonic-gate 	if (error)
920Sstevel@tonic-gate 		return (error);
93*9694SScott.Rotondo@Sun.COM 	error = stlookuppn(&pnp, cur_file, needroothandle);
940Sstevel@tonic-gate 	return (error);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static int
stlookuppn(struct st_pathname * pnp,struct nfs_file * cfile,bool_t needroothandle)98*9694SScott.Rotondo@Sun.COM stlookuppn(struct st_pathname *pnp, struct nfs_file *cfile,
99*9694SScott.Rotondo@Sun.COM bool_t needroothandle)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	char component[NFS_MAXNAMLEN+1];	/* buffer for component */
1020Sstevel@tonic-gate 	int nlink = 0;
1030Sstevel@tonic-gate 	int error = 0;
1040Sstevel@tonic-gate 	int dino, cino;
1050Sstevel@tonic-gate 	struct nfs_file *cdp = NULL;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	*cfile = roothandle;	/* structure copy - start at the root. */
1080Sstevel@tonic-gate 	dino = root_inum;
1090Sstevel@tonic-gate begin:
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * Each time we begin a new name interpretation (e.g.
1120Sstevel@tonic-gate 	 * when first called and after each symbolic link is
1130Sstevel@tonic-gate 	 * substituted), we allow the search to start at the
1140Sstevel@tonic-gate 	 * root directory if the name starts with a '/', otherwise
1150Sstevel@tonic-gate 	 * continuing from the current directory.
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	component[0] = '\0';
118*9694SScott.Rotondo@Sun.COM 	if (stpn_peekchar(pnp) == '/') {
1190Sstevel@tonic-gate 		if (!needroothandle)
1200Sstevel@tonic-gate 			*cfile = roothandle;
1210Sstevel@tonic-gate 		dino = root_inum;
122*9694SScott.Rotondo@Sun.COM 		stpn_skipslash(pnp);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate next:
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * Make sure we have a directory.
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate 	if (!cfile_is_dir(cfile)) {
1300Sstevel@tonic-gate 		error = ENOTDIR;
1310Sstevel@tonic-gate 		goto bad;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * Process the next component of the pathname.
1350Sstevel@tonic-gate 	 */
136*9694SScott.Rotondo@Sun.COM 	error = stpn_stripcomponent(pnp, component);
1370Sstevel@tonic-gate 	if (error)
1380Sstevel@tonic-gate 		goto bad;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Check for degenerate name (e.g. / or "")
1420Sstevel@tonic-gate 	 * which is a way of talking about a directory,
1430Sstevel@tonic-gate 	 * e.g. "/." or ".".
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	if (component[0] == '\0')
1460Sstevel@tonic-gate 		return (0);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Handle "..": two special cases.
1500Sstevel@tonic-gate 	 * 1. If at root directory (e.g. after chroot)
1510Sstevel@tonic-gate 	 *    then ignore it so can't get out.
1520Sstevel@tonic-gate 	 * 2. If this vnode is the root of a mounted
1530Sstevel@tonic-gate 	 *    file system, then replace it with the
1540Sstevel@tonic-gate 	 *    vnode which was mounted on so we take the
1550Sstevel@tonic-gate 	 *    .. in the other file system.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	if (strcmp(component, "..") == 0) {
1580Sstevel@tonic-gate 		if (cfile == &roothandle)
1590Sstevel@tonic-gate 			goto skip;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * Perform a lookup in the current directory.
1640Sstevel@tonic-gate 	 * We create a simple negative lookup cache by storing
1650Sstevel@tonic-gate 	 * inode -1 to indicate file not found.
1660Sstevel@tonic-gate 	 */
1670Sstevel@tonic-gate 	cino = get_dcache(mac_get_dev(), component, dino);
1680Sstevel@tonic-gate 	if (cino == -1)
1690Sstevel@tonic-gate 		return (ENOENT);
1700Sstevel@tonic-gate #ifdef DEBUG
1710Sstevel@tonic-gate 	dprintf("lookup: component %s pathleft %s\n", component, pnp->pn_path);
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate 	if ((cino == 0) ||
174*9694SScott.Rotondo@Sun.COM 	    ((cdp = (struct nfs_file *)get_icache(mac_get_dev(), cino)) == 0)) {
1750Sstevel@tonic-gate 		struct nfs_file *lkp;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		/*
1780Sstevel@tonic-gate 		 * If an RPC error occurs, error is not changed,
1790Sstevel@tonic-gate 		 * else it is the NFS error if NULL is returned.
1800Sstevel@tonic-gate 		 */
1810Sstevel@tonic-gate 		error = -1;
1820Sstevel@tonic-gate 		switch (cfile->version) {
1830Sstevel@tonic-gate 		case NFS_VERSION:
1840Sstevel@tonic-gate 			lkp = nfslookup(cfile, component, &error);
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 		case NFS_V3:
1870Sstevel@tonic-gate 			lkp = nfs3lookup(cfile, component, &error);
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		case NFS_V4:
1900Sstevel@tonic-gate 			lkp = nfs4lookup(cfile, component, &error);
1910Sstevel@tonic-gate 			break;
1920Sstevel@tonic-gate 		default:
1930Sstevel@tonic-gate 			printf("lookup: NFS Version %d not supported\n",
194*9694SScott.Rotondo@Sun.COM 			    cfile->version);
1950Sstevel@tonic-gate 			lkp = NULL;
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * Check for RPC error
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		if (error == -1) {
2030Sstevel@tonic-gate 			printf("lookup: lookup RPC error\n");
2040Sstevel@tonic-gate 			return (error);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * Check for NFS error
2090Sstevel@tonic-gate 		 */
2100Sstevel@tonic-gate 		if (lkp == NULL) {
2110Sstevel@tonic-gate 			if ((error != NFSERR_NOENT) &&
2120Sstevel@tonic-gate 			    (error != NFS3ERR_NOENT) &&
2130Sstevel@tonic-gate 			    (error != NFS4ERR_NOENT)) {
2140Sstevel@tonic-gate #ifdef DEBUG
2150Sstevel@tonic-gate 			dprintf("lookup: lkp is NULL with error %d\n", error);
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate 				return (error);
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate #ifdef DEBUG
2200Sstevel@tonic-gate 			dprintf("lookup: lkp is NULL with error %d\n", error);
2210Sstevel@tonic-gate #endif
2220Sstevel@tonic-gate 			/*
2230Sstevel@tonic-gate 			 * File not found so set cached inode to -1
2240Sstevel@tonic-gate 			 */
2250Sstevel@tonic-gate 			set_dcache(mac_get_dev(), component, dino, -1);
2260Sstevel@tonic-gate 			return (error);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		if (cdp = (struct nfs_file *)
2300Sstevel@tonic-gate 		    bkmem_alloc(sizeof (struct nfs_file))) {
2310Sstevel@tonic-gate 			/*
2320Sstevel@tonic-gate 			 *  Save this entry in cache for next time ...
2330Sstevel@tonic-gate 			 */
2340Sstevel@tonic-gate 			if (!cino)
2350Sstevel@tonic-gate 				cino = ++next_inum;
2360Sstevel@tonic-gate 			*cdp = *lkp;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 			set_dcache(mac_get_dev(), component, dino, cino);
2390Sstevel@tonic-gate 			set_icache(mac_get_dev(), cino, cdp,
2400Sstevel@tonic-gate 						sizeof (struct nfs_file));
2410Sstevel@tonic-gate 		} else {
2420Sstevel@tonic-gate 			/*
2430Sstevel@tonic-gate 			 *  Out of memory, clear cache keys so we don't get
2440Sstevel@tonic-gate 			 *  confused later.
2450Sstevel@tonic-gate 			 */
2460Sstevel@tonic-gate 			cino = 0;
2470Sstevel@tonic-gate 			cdp = lkp;
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	dino = cino;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * If we hit a symbolic link and there is more path to be
2540Sstevel@tonic-gate 	 * translated or this operation does not wish to apply
2550Sstevel@tonic-gate 	 * to a link, then place the contents of the link at the
2560Sstevel@tonic-gate 	 * front of the remaining pathname.
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	if (cfile_is_lnk(cdp)) {
259*9694SScott.Rotondo@Sun.COM 		struct st_pathname linkpath;
2600Sstevel@tonic-gate 		static char path_tmp[NFS_MAXPATHLEN];	/* used for symlinks */
2610Sstevel@tonic-gate 		char *pathp;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 		linkpath.pn_buf = &path_tmp[0];
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		nlink++;
2660Sstevel@tonic-gate 		if (nlink > MAXSYMLINKS) {
2670Sstevel@tonic-gate 			error = ELOOP;
2680Sstevel@tonic-gate 			goto bad;
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 		switch (cdp->version) {
2710Sstevel@tonic-gate 		case NFS_VERSION:
2720Sstevel@tonic-gate 			error = nfsgetsymlink(cdp, &pathp);
2730Sstevel@tonic-gate 			break;
2740Sstevel@tonic-gate 		case NFS_V3:
2750Sstevel@tonic-gate 			error = nfs3getsymlink(cdp, &pathp);
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 		case NFS_V4:
2780Sstevel@tonic-gate 			error = nfs4getsymlink(cdp, &pathp);
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 		default:
2810Sstevel@tonic-gate 			printf("getsymlink: NFS Version %d not supported\n",
282*9694SScott.Rotondo@Sun.COM 			    cdp->version);
2830Sstevel@tonic-gate 			error = ENOTSUP;
2840Sstevel@tonic-gate 			break;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		if (error)
2880Sstevel@tonic-gate 			goto bad;
2890Sstevel@tonic-gate 
290*9694SScott.Rotondo@Sun.COM 		stpn_get(pathp, &linkpath);
2910Sstevel@tonic-gate 
292*9694SScott.Rotondo@Sun.COM 		if (stpn_pathleft(&linkpath) == 0)
293*9694SScott.Rotondo@Sun.COM 			(void) stpn_set(&linkpath, ".");
294*9694SScott.Rotondo@Sun.COM 		error = stpn_combine(pnp, &linkpath); /* linkpath before pn */
2950Sstevel@tonic-gate 		if (error)
2960Sstevel@tonic-gate 			goto bad;
2970Sstevel@tonic-gate 		goto begin;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (needroothandle) {
3010Sstevel@tonic-gate 		roothandle = *cdp;
3020Sstevel@tonic-gate 		needroothandle = FALSE;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	*cfile = *cdp;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate skip:
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * Skip to next component of the pathname.
3090Sstevel@tonic-gate 	 * If no more components, return last directory (if wanted)  and
3100Sstevel@tonic-gate 	 * last component (if wanted).
3110Sstevel@tonic-gate 	 */
312*9694SScott.Rotondo@Sun.COM 	if (stpn_pathleft(pnp) == 0) {
313*9694SScott.Rotondo@Sun.COM 		(void) stpn_set(pnp, component);
3140Sstevel@tonic-gate 		return (0);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * skip over slashes from end of last component
3180Sstevel@tonic-gate 	 */
319*9694SScott.Rotondo@Sun.COM 	stpn_skipslash(pnp);
3200Sstevel@tonic-gate 	goto next;
3210Sstevel@tonic-gate bad:
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Error.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	return (error);
3260Sstevel@tonic-gate }
327