xref: /onnv-gate/usr/src/uts/common/fs/pathname.c (revision 5331:3047ad28a67b)
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*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * 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  */
210Sstevel@tonic-gate /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/systm.h>
450Sstevel@tonic-gate #include <sys/uio.h>
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/pathname.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/cred.h>
500Sstevel@tonic-gate #include <sys/vnode.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Pathname utilities.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * In translating file names we copy each argument file
570Sstevel@tonic-gate  * name into a pathname structure where we operate on it.
580Sstevel@tonic-gate  * Each pathname structure can hold "pn_bufsize" characters
590Sstevel@tonic-gate  * including a terminating null, and operations here support
600Sstevel@tonic-gate  * allocating and freeing pathname structures, fetching
610Sstevel@tonic-gate  * strings from user space, getting the next character from
620Sstevel@tonic-gate  * a pathname, combining two pathnames (used in symbolic
630Sstevel@tonic-gate  * link processing), and peeling off the first component
640Sstevel@tonic-gate  * of a pathname.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Allocate contents of pathname structure.  Structure is typically
690Sstevel@tonic-gate  * an automatic variable in calling routine for convenience.
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * May sleep in the call to kmem_alloc() and so must not be called
720Sstevel@tonic-gate  * from interrupt level.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate void
pn_alloc(struct pathname * pnp)750Sstevel@tonic-gate pn_alloc(struct pathname *pnp)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
780Sstevel@tonic-gate 	pnp->pn_pathlen = 0;
790Sstevel@tonic-gate 	pnp->pn_bufsize = MAXPATHLEN;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Free pathname resources.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate void
pn_free(struct pathname * pnp)860Sstevel@tonic-gate pn_free(struct pathname *pnp)
870Sstevel@tonic-gate {
88274Sdmick 	/* pn_bufsize is usually MAXPATHLEN, but may not be */
89274Sdmick 	kmem_free(pnp->pn_buf, pnp->pn_bufsize);
900Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf = NULL;
910Sstevel@tonic-gate 	pnp->pn_pathlen = pnp->pn_bufsize = 0;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * Pull a path name from user or kernel space.
960Sstevel@tonic-gate  * Called from pn_get() after allocation of a MAXPATHLEN buffer.
970Sstevel@tonic-gate  * Also called directly with a TYPICALMAXPATHLEN-size buffer
980Sstevel@tonic-gate  * on the stack as a local optimization.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate int
pn_get_buf(char * str,enum uio_seg seg,struct pathname * pnp,void * buf,size_t bufsize)1010Sstevel@tonic-gate pn_get_buf(char *str, enum uio_seg seg, struct pathname *pnp,
1020Sstevel@tonic-gate 	void *buf, size_t bufsize)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	int error;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf = buf;
1070Sstevel@tonic-gate 	pnp->pn_bufsize = bufsize;
1080Sstevel@tonic-gate 	if (seg == UIO_USERSPACE)
1090Sstevel@tonic-gate 		error = copyinstr(str, pnp->pn_path, bufsize, &pnp->pn_pathlen);
1100Sstevel@tonic-gate 	else
1110Sstevel@tonic-gate 		error = copystr(str, pnp->pn_path, bufsize, &pnp->pn_pathlen);
1120Sstevel@tonic-gate 	if (error)
1130Sstevel@tonic-gate 		return (error);
1140Sstevel@tonic-gate 	pnp->pn_pathlen--;		/* don't count null byte */
1150Sstevel@tonic-gate 	return (0);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Pull a path name from user or kernel space.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate int
pn_get(char * str,enum uio_seg seg,struct pathname * pnp)1220Sstevel@tonic-gate pn_get(char *str, enum uio_seg seg, struct pathname *pnp)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	int error;
1250Sstevel@tonic-gate 	void *buf;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1280Sstevel@tonic-gate 	if ((error = pn_get_buf(str, seg, pnp, buf, MAXPATHLEN)) != 0)
1290Sstevel@tonic-gate 		pn_free(pnp);
1300Sstevel@tonic-gate 	return (error);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Set path name to argument string.  Storage has already been allocated
1350Sstevel@tonic-gate  * and pn_buf points to it.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * On error, all fields except pn_buf will be undefined.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate int
pn_set(struct pathname * pnp,char * path)1400Sstevel@tonic-gate pn_set(struct pathname *pnp, char *path)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int error;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	pnp->pn_path = pnp->pn_buf;
1450Sstevel@tonic-gate 	error = copystr(path, pnp->pn_path, pnp->pn_bufsize, &pnp->pn_pathlen);
1460Sstevel@tonic-gate 	pnp->pn_pathlen--;		/* don't count null byte */
1470Sstevel@tonic-gate 	return (error);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Combine two argument path names by putting the second argument
1520Sstevel@tonic-gate  * before the first in the first's buffer.  This isn't very general;
1530Sstevel@tonic-gate  * it is designed specifically for symbolic link processing.
1540Sstevel@tonic-gate  * This function copies the symlink in-place in the pathname.  This is to
1550Sstevel@tonic-gate  * ensure that vnode path caching remains correct.  At the point where this is
1560Sstevel@tonic-gate  * called (from lookuppnvp), we have called pn_getcomponent(), found it is a
1570Sstevel@tonic-gate  * symlink, and are now replacing the contents.  The complen parameter indicates
1580Sstevel@tonic-gate  * how much of the pathname to replace.  If the symlink is an absolute path,
1590Sstevel@tonic-gate  * then we overwrite the entire contents of the pathname.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate int
pn_insert(struct pathname * pnp,struct pathname * sympnp,size_t complen)1620Sstevel@tonic-gate pn_insert(struct pathname *pnp, struct pathname *sympnp, size_t complen)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (*sympnp->pn_path == '/') {
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * Full path, replace everything
1680Sstevel@tonic-gate 		 */
1690Sstevel@tonic-gate 		if (pnp->pn_pathlen + sympnp->pn_pathlen >= pnp->pn_bufsize)
1700Sstevel@tonic-gate 			return (ENAMETOOLONG);
1710Sstevel@tonic-gate 		if (pnp->pn_pathlen != 0)
1720Sstevel@tonic-gate 			ovbcopy(pnp->pn_path, pnp->pn_buf + sympnp->pn_pathlen,
1730Sstevel@tonic-gate 			    pnp->pn_pathlen);
1740Sstevel@tonic-gate 		bcopy(sympnp->pn_path, pnp->pn_buf, sympnp->pn_pathlen);
1750Sstevel@tonic-gate 		pnp->pn_pathlen += sympnp->pn_pathlen;
1760Sstevel@tonic-gate 		pnp->pn_buf[pnp->pn_pathlen] = '\0';
1770Sstevel@tonic-gate 		pnp->pn_path = pnp->pn_buf;
1780Sstevel@tonic-gate 	} else {
1790Sstevel@tonic-gate 		/*
1800Sstevel@tonic-gate 		 * Partial path, replace only last component
1810Sstevel@tonic-gate 		 */
1820Sstevel@tonic-gate 		if ((pnp->pn_path - pnp->pn_buf) - complen +
1830Sstevel@tonic-gate 		    pnp->pn_pathlen + sympnp->pn_pathlen >= pnp->pn_bufsize)
1840Sstevel@tonic-gate 			return (ENAMETOOLONG);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		if (pnp->pn_pathlen != 0)
1870Sstevel@tonic-gate 			ovbcopy(pnp->pn_path, pnp->pn_path - complen +
1880Sstevel@tonic-gate 			    sympnp->pn_pathlen, pnp->pn_pathlen + 1);
1890Sstevel@tonic-gate 		pnp->pn_path -= complen;
1900Sstevel@tonic-gate 		bcopy(sympnp->pn_path, pnp->pn_path, sympnp->pn_pathlen);
1910Sstevel@tonic-gate 		pnp->pn_pathlen += sympnp->pn_pathlen;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	return (0);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate int
pn_getsymlink(vnode_t * vp,struct pathname * pnp,cred_t * crp)1980Sstevel@tonic-gate pn_getsymlink(vnode_t *vp, struct pathname *pnp, cred_t *crp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	struct iovec aiov;
2010Sstevel@tonic-gate 	struct uio auio;
2020Sstevel@tonic-gate 	int error;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	aiov.iov_base = pnp->pn_path = pnp->pn_buf;
2050Sstevel@tonic-gate 	aiov.iov_len = pnp->pn_bufsize;
2060Sstevel@tonic-gate 	auio.uio_iov = &aiov;
2070Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
2080Sstevel@tonic-gate 	auio.uio_loffset = 0;
2090Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
2100Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
2110Sstevel@tonic-gate 	auio.uio_resid = pnp->pn_bufsize;
212*5331Samw 	if ((error = VOP_READLINK(vp, &auio, crp, NULL)) == 0) {
2130Sstevel@tonic-gate 		pnp->pn_pathlen = pnp->pn_bufsize - auio.uio_resid;
2140Sstevel@tonic-gate 		if (pnp->pn_pathlen == pnp->pn_bufsize)
2150Sstevel@tonic-gate 			error = ENAMETOOLONG;
2160Sstevel@tonic-gate 		else
2170Sstevel@tonic-gate 			pnp->pn_path[pnp->pn_pathlen] = '\0';
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	return (error);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * Get next component from a path name and leave in
2240Sstevel@tonic-gate  * buffer "component" which should have room for
2250Sstevel@tonic-gate  * MAXNAMELEN bytes (including a null terminator character).
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate int
pn_getcomponent(struct pathname * pnp,char * component)2280Sstevel@tonic-gate pn_getcomponent(struct pathname *pnp, char *component)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	char c, *cp, *path, saved;
2310Sstevel@tonic-gate 	size_t pathlen;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	path = pnp->pn_path;
2340Sstevel@tonic-gate 	pathlen = pnp->pn_pathlen;
2350Sstevel@tonic-gate 	if (pathlen >= MAXNAMELEN) {
2360Sstevel@tonic-gate 		saved = path[MAXNAMELEN];
2370Sstevel@tonic-gate 		path[MAXNAMELEN] = '/';	/* guarantees loop termination */
2380Sstevel@tonic-gate 		for (cp = path; (c = *cp) != '/'; cp++)
2390Sstevel@tonic-gate 			*component++ = c;
2400Sstevel@tonic-gate 		path[MAXNAMELEN] = saved;
2410Sstevel@tonic-gate 		if (cp - path == MAXNAMELEN)
2420Sstevel@tonic-gate 			return (ENAMETOOLONG);
2430Sstevel@tonic-gate 	} else {
2440Sstevel@tonic-gate 		path[pathlen] = '/';	/* guarantees loop termination */
2450Sstevel@tonic-gate 		for (cp = path; (c = *cp) != '/'; cp++)
2460Sstevel@tonic-gate 			*component++ = c;
2470Sstevel@tonic-gate 		path[pathlen] = '\0';
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	pnp->pn_path = cp;
2510Sstevel@tonic-gate 	pnp->pn_pathlen = pathlen - (cp - path);
2520Sstevel@tonic-gate 	*component = '\0';
2530Sstevel@tonic-gate 	return (0);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Skip over consecutive slashes in the path name.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate void
pn_skipslash(struct pathname * pnp)2600Sstevel@tonic-gate pn_skipslash(struct pathname *pnp)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 	while (pnp->pn_pathlen > 0 && *pnp->pn_path == '/') {
2630Sstevel@tonic-gate 		pnp->pn_path++;
2640Sstevel@tonic-gate 		pnp->pn_pathlen--;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * Sets pn_path to the last component in the pathname, updating
2700Sstevel@tonic-gate  * pn_pathlen.  If pathname is empty, or degenerate, leaves pn_path
2710Sstevel@tonic-gate  * pointing at NULL char.  The pathname is explicitly null-terminated
2720Sstevel@tonic-gate  * so that any trailing slashes are effectively removed.
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate void
pn_setlast(struct pathname * pnp)2750Sstevel@tonic-gate pn_setlast(struct pathname *pnp)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	char *buf = pnp->pn_buf;
2780Sstevel@tonic-gate 	char *path = pnp->pn_path + pnp->pn_pathlen - 1;
2790Sstevel@tonic-gate 	char *endpath;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	while (path > buf && *path == '/')
2820Sstevel@tonic-gate 		--path;
2830Sstevel@tonic-gate 	endpath = path + 1;
2840Sstevel@tonic-gate 	while (path > buf && *path != '/')
2850Sstevel@tonic-gate 		--path;
2860Sstevel@tonic-gate 	if (*path == '/')
2870Sstevel@tonic-gate 		path++;
2880Sstevel@tonic-gate 	*endpath = '\0';
2890Sstevel@tonic-gate 	pnp->pn_path = path;
2900Sstevel@tonic-gate 	pnp->pn_pathlen = endpath - path;
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * Eliminate any trailing slashes in the pathname.
2950Sstevel@tonic-gate  * Return non-zero iff there were any trailing slashes.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate int
pn_fixslash(struct pathname * pnp)2980Sstevel@tonic-gate pn_fixslash(struct pathname *pnp)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	char *start = pnp->pn_path;
3010Sstevel@tonic-gate 	char *end = start + pnp->pn_pathlen;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	while (end > start && *(end - 1) == '/')
3040Sstevel@tonic-gate 		end--;
3050Sstevel@tonic-gate 	if (pnp->pn_pathlen == end - start)
3060Sstevel@tonic-gate 		return (0);
3070Sstevel@tonic-gate 	*end = '\0';
3080Sstevel@tonic-gate 	pnp->pn_pathlen = end - start;
3090Sstevel@tonic-gate 	return (1);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Add a slash to the end of the pathname, if it will fit.
3140Sstevel@tonic-gate  * Return ENAMETOOLONG if it won't.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate int
pn_addslash(struct pathname * pnp)3170Sstevel@tonic-gate pn_addslash(struct pathname *pnp)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	if (pnp->pn_path + pnp->pn_pathlen + 1 >=
3200Sstevel@tonic-gate 	    pnp->pn_buf + pnp->pn_bufsize) {
3210Sstevel@tonic-gate 		if (pnp->pn_pathlen + 1 >= pnp->pn_bufsize)	/* no room */
3220Sstevel@tonic-gate 			return (ENAMETOOLONG);
3230Sstevel@tonic-gate 		/*
3240Sstevel@tonic-gate 		 * Move the component to the start of the buffer
3250Sstevel@tonic-gate 		 * so we have room to add the trailing slash.
3260Sstevel@tonic-gate 		 */
3270Sstevel@tonic-gate 		ovbcopy(pnp->pn_path, pnp->pn_buf, pnp->pn_pathlen);
3280Sstevel@tonic-gate 		pnp->pn_path = pnp->pn_buf;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	pnp->pn_path[pnp->pn_pathlen++] = '/';
3310Sstevel@tonic-gate 	pnp->pn_path[pnp->pn_pathlen] = '\0';
3320Sstevel@tonic-gate 	return (0);
3330Sstevel@tonic-gate }
334